1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * 3*0Sstevel@tonic-gate * Portions Copyright %G% Sun Microsystems, Inc. 4*0Sstevel@tonic-gate * All Rights Reserved 5*0Sstevel@tonic-gate * 6*0Sstevel@tonic-gate */ 7*0Sstevel@tonic-gate 8*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 9*0Sstevel@tonic-gate 10*0Sstevel@tonic-gate /* 11*0Sstevel@tonic-gate * Copyright (c) 1990 Regents of the University of Michigan. 12*0Sstevel@tonic-gate * All rights reserved. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * ufn.c 15*0Sstevel@tonic-gate */ 16*0Sstevel@tonic-gate 17*0Sstevel@tonic-gate #ifndef lint 18*0Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n"; 19*0Sstevel@tonic-gate #endif 20*0Sstevel@tonic-gate 21*0Sstevel@tonic-gate #include <stdio.h> 22*0Sstevel@tonic-gate #include <string.h> 23*0Sstevel@tonic-gate #include <ctype.h> 24*0Sstevel@tonic-gate #include <stdlib.h> /* malloc(), realloc(), free() */ 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gate #ifdef MACOS 27*0Sstevel@tonic-gate #include <stdlib.h> 28*0Sstevel@tonic-gate #include "macos.h" 29*0Sstevel@tonic-gate #else /* MACOS */ 30*0Sstevel@tonic-gate #if defined( DOS ) || defined( _WIN32 ) 31*0Sstevel@tonic-gate #include "msdos.h" 32*0Sstevel@tonic-gate #else /* DOS */ 33*0Sstevel@tonic-gate #include <sys/time.h> 34*0Sstevel@tonic-gate #include <sys/types.h> 35*0Sstevel@tonic-gate #include <sys/socket.h> 36*0Sstevel@tonic-gate #endif /* DOS */ 37*0Sstevel@tonic-gate #endif /* MACOS */ 38*0Sstevel@tonic-gate 39*0Sstevel@tonic-gate #include "lber.h" 40*0Sstevel@tonic-gate #include "ldap.h" 41*0Sstevel@tonic-gate #include "ldap-private.h" 42*0Sstevel@tonic-gate #include "ldap-int.h" 43*0Sstevel@tonic-gate #ifdef SUN 44*0Sstevel@tonic-gate /* 45*0Sstevel@tonic-gate * to include definition of FILTERFILE and or TEMPLATEFILE 46*0Sstevel@tonic-gate */ 47*0Sstevel@tonic-gate #include "ldapconfig.h" 48*0Sstevel@tonic-gate #endif 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate #ifdef NEEDPROTOS 51*0Sstevel@tonic-gate typedef int (*cancelptype)( void *cancelparm ); 52*0Sstevel@tonic-gate #else /* NEEDPROTOS */ 53*0Sstevel@tonic-gate typedef int (*cancelptype)(); 54*0Sstevel@tonic-gate #endif /* NEEDPROTOS */ 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate #ifdef NEEDPROTOS 57*0Sstevel@tonic-gate static int ldap_ufn_search_ctx( LDAP *ld, char **ufncomp, int ncomp, 58*0Sstevel@tonic-gate char *prefix, char **attrs, int attrsonly, LDAPMessage **res, 59*0Sstevel@tonic-gate cancelptype cancelproc, void *cancelparm, char *tag1, char *tag2, 60*0Sstevel@tonic-gate char *tag3 ); 61*0Sstevel@tonic-gate static LDAPMessage *ldap_msg_merge( LDAP *ld, LDAPMessage *a, LDAPMessage *b ); 62*0Sstevel@tonic-gate static LDAPMessage *ldap_ufn_expand( LDAP *ld, cancelptype cancelproc, 63*0Sstevel@tonic-gate void *cancelparm, char **dns, char *filter, int scope, 64*0Sstevel@tonic-gate char **attrs, int aonly, int *err ); 65*0Sstevel@tonic-gate LDAPFiltDesc *ldap_ufn_setfilter( LDAP *ld, char *fname ); 66*0Sstevel@tonic-gate #else /* NEEDPROTOS */ 67*0Sstevel@tonic-gate static LDAPMessage *ldap_msg_merge(); 68*0Sstevel@tonic-gate static LDAPMessage *ldap_ufn_expand(); 69*0Sstevel@tonic-gate LDAPFiltDesc *ldap_ufn_setfilter(); 70*0Sstevel@tonic-gate #endif /* NEEDPROTOS */ 71*0Sstevel@tonic-gate static LDAPMessage *ldap_msg_merge(); 72*0Sstevel@tonic-gate static LDAPMessage *ldap_ufn_expand(); 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate /* 75*0Sstevel@tonic-gate * ldap_ufn_search_ctx - do user friendly searching; provide cancel feature; 76*0Sstevel@tonic-gate * specify ldapfilter.conf tags for each phase of search 77*0Sstevel@tonic-gate * 78*0Sstevel@tonic-gate * ld LDAP descriptor 79*0Sstevel@tonic-gate * ufncomp the exploded user friendly name to look for 80*0Sstevel@tonic-gate * ncomp number of elements in ufncomp 81*0Sstevel@tonic-gate * prefix where to start searching 82*0Sstevel@tonic-gate * attrs list of attribute types to return for matches 83*0Sstevel@tonic-gate * attrsonly 1 => attributes only 0 => attributes and values 84*0Sstevel@tonic-gate * res will contain the result of the search 85*0Sstevel@tonic-gate * cancelproc routine that returns non-zero if operation should be 86*0Sstevel@tonic-gate * cancelled. This can be NULL. If it is non-NULL, the 87*0Sstevel@tonic-gate * routine will be called periodically. 88*0Sstevel@tonic-gate * cancelparm void * that is passed to cancelproc 89*0Sstevel@tonic-gate * tag[123] the ldapfilter.conf tag that will be used in phases 90*0Sstevel@tonic-gate * 1, 2, and 3 of the search, respectively 91*0Sstevel@tonic-gate * 92*0Sstevel@tonic-gate * Example: 93*0Sstevel@tonic-gate * char *attrs[] = { "mail", "title", 0 }; 94*0Sstevel@tonic-gate * char *ufncomp[] = { "howes", "umich", "us", 0 } 95*0Sstevel@tonic-gate * LDAPMessage *res; 96*0Sstevel@tonic-gate * error = ldap_ufn_search_ctx( ld, ufncomp, 3, NULL, attrs, attrsonly, 97*0Sstevel@tonic-gate * &res, acancelproc, along, "ufn first", 98*0Sstevel@tonic-gate * "ufn intermediate", "ufn last" ); 99*0Sstevel@tonic-gate */ 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate static int 102*0Sstevel@tonic-gate ldap_ufn_search_ctx( LDAP *ld, char **ufncomp, int ncomp, char *prefix, 103*0Sstevel@tonic-gate char **attrs, int attrsonly, LDAPMessage **res, cancelptype cancelproc, 104*0Sstevel@tonic-gate void *cancelparm, char *tag1, char *tag2, char *tag3 ) 105*0Sstevel@tonic-gate { 106*0Sstevel@tonic-gate char *dn, *ftag; 107*0Sstevel@tonic-gate char **dns; 108*0Sstevel@tonic-gate int max, i, err, scope, phase, tries; 109*0Sstevel@tonic-gate LDAPFiltInfo *fi; 110*0Sstevel@tonic-gate LDAPMessage *tmpcand; 111*0Sstevel@tonic-gate LDAPMessage *candidates; 112*0Sstevel@tonic-gate /* LDAPMessage *ldap_msg_merge(), *ldap_ufn_expand(); */ 113*0Sstevel@tonic-gate static char *objattrs[] = { "objectClass", NULL }; 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate /* 116*0Sstevel@tonic-gate * look up ufn components from most to least significant. 117*0Sstevel@tonic-gate * there are 3 phases. 118*0Sstevel@tonic-gate * phase 1 search the root for orgs or countries 119*0Sstevel@tonic-gate * phase 2 search for orgs 120*0Sstevel@tonic-gate * phase 3 search for a person 121*0Sstevel@tonic-gate * in phases 1 and 2, we are building a list of candidate DNs, 122*0Sstevel@tonic-gate * below which we will search for the final component of the ufn. 123*0Sstevel@tonic-gate * for each component we try the filters listed in the 124*0Sstevel@tonic-gate * filterconfig file, first one-level (except the last compoment), 125*0Sstevel@tonic-gate * then subtree. if any of them produce any results, we go on to 126*0Sstevel@tonic-gate * the next component. 127*0Sstevel@tonic-gate */ 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT ) 130*0Sstevel@tonic-gate LOCK_LDAP(ld); 131*0Sstevel@tonic-gate #endif 132*0Sstevel@tonic-gate *res = NULL; 133*0Sstevel@tonic-gate candidates = NULL; 134*0Sstevel@tonic-gate phase = 1; 135*0Sstevel@tonic-gate for ( ncomp--; ncomp != -1; ncomp-- ) { 136*0Sstevel@tonic-gate if ( *ufncomp[ncomp] == '"' ) { 137*0Sstevel@tonic-gate char *quote; 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gate if ( (quote = strrchr( ufncomp[ncomp], '"' )) != NULL ) 140*0Sstevel@tonic-gate *quote = '\0'; 141*0Sstevel@tonic-gate (void) strcpy( ufncomp[ncomp], ufncomp[ncomp] + 1 ); 142*0Sstevel@tonic-gate } 143*0Sstevel@tonic-gate if ( ncomp == 0 ) 144*0Sstevel@tonic-gate phase = 3; 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate switch ( phase ) { 147*0Sstevel@tonic-gate case 1: 148*0Sstevel@tonic-gate ftag = tag1; 149*0Sstevel@tonic-gate scope = LDAP_SCOPE_ONELEVEL; 150*0Sstevel@tonic-gate break; 151*0Sstevel@tonic-gate case 2: 152*0Sstevel@tonic-gate ftag = tag2; 153*0Sstevel@tonic-gate scope = LDAP_SCOPE_ONELEVEL; 154*0Sstevel@tonic-gate break; 155*0Sstevel@tonic-gate case 3: 156*0Sstevel@tonic-gate ftag = tag3; 157*0Sstevel@tonic-gate scope = LDAP_SCOPE_SUBTREE; 158*0Sstevel@tonic-gate break; 159*0Sstevel@tonic-gate } 160*0Sstevel@tonic-gate 161*0Sstevel@tonic-gate /* 162*0Sstevel@tonic-gate * construct an array of DN's to search below from the 163*0Sstevel@tonic-gate * list of candidates. 164*0Sstevel@tonic-gate */ 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate if ( candidates == NULL ) { 167*0Sstevel@tonic-gate if ( prefix != NULL ) { 168*0Sstevel@tonic-gate if ( (dns = (char **) malloc( sizeof(char *) 169*0Sstevel@tonic-gate * 2 )) == NULL ) { 170*0Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT ) 171*0Sstevel@tonic-gate UNLOCK_LDAP(ld); 172*0Sstevel@tonic-gate #endif 173*0Sstevel@tonic-gate return( ld->ld_errno = LDAP_NO_MEMORY ); 174*0Sstevel@tonic-gate } 175*0Sstevel@tonic-gate dns[0] = strdup( prefix ); 176*0Sstevel@tonic-gate dns[1] = NULL; 177*0Sstevel@tonic-gate } else { 178*0Sstevel@tonic-gate dns = NULL; 179*0Sstevel@tonic-gate } 180*0Sstevel@tonic-gate } else { 181*0Sstevel@tonic-gate i = 0, max = 0; 182*0Sstevel@tonic-gate for ( tmpcand = candidates; tmpcand != NULL && 183*0Sstevel@tonic-gate tmpcand->lm_msgtype != LDAP_RES_SEARCH_RESULT; 184*0Sstevel@tonic-gate tmpcand = tmpcand->lm_chain ) 185*0Sstevel@tonic-gate { 186*0Sstevel@tonic-gate if ( (dn = ldap_get_dn( ld, tmpcand )) == NULL ) 187*0Sstevel@tonic-gate continue; 188*0Sstevel@tonic-gate 189*0Sstevel@tonic-gate if ( dns == NULL ) { 190*0Sstevel@tonic-gate if ( (dns = (char **) malloc( 191*0Sstevel@tonic-gate sizeof(char *) * 8 )) == NULL ) { 192*0Sstevel@tonic-gate ld->ld_errno = LDAP_NO_MEMORY; 193*0Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT ) 194*0Sstevel@tonic-gate UNLOCK_LDAP(ld); 195*0Sstevel@tonic-gate #endif 196*0Sstevel@tonic-gate return( LDAP_NO_MEMORY ); 197*0Sstevel@tonic-gate } 198*0Sstevel@tonic-gate max = 8; 199*0Sstevel@tonic-gate } else if ( i >= max ) { 200*0Sstevel@tonic-gate if ( (dns = (char **) realloc( dns, 201*0Sstevel@tonic-gate sizeof(char *) * 2 * max )) 202*0Sstevel@tonic-gate == NULL ) 203*0Sstevel@tonic-gate { 204*0Sstevel@tonic-gate ld->ld_errno = LDAP_NO_MEMORY; 205*0Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT ) 206*0Sstevel@tonic-gate UNLOCK_LDAP(ld); 207*0Sstevel@tonic-gate #endif 208*0Sstevel@tonic-gate return( LDAP_NO_MEMORY ); 209*0Sstevel@tonic-gate } 210*0Sstevel@tonic-gate max *= 2; 211*0Sstevel@tonic-gate } 212*0Sstevel@tonic-gate dns[i++] = dn; 213*0Sstevel@tonic-gate dns[i] = NULL; 214*0Sstevel@tonic-gate } 215*0Sstevel@tonic-gate ldap_msgfree( candidates ); 216*0Sstevel@tonic-gate candidates = NULL; 217*0Sstevel@tonic-gate } 218*0Sstevel@tonic-gate tries = 0; 219*0Sstevel@tonic-gate tryagain: 220*0Sstevel@tonic-gate tries++; 221*0Sstevel@tonic-gate for ( fi = ldap_getfirstfilter( ld->ld_filtd, ftag, 222*0Sstevel@tonic-gate ufncomp[ncomp] ); fi != NULL; 223*0Sstevel@tonic-gate fi = ldap_getnextfilter( ld->ld_filtd ) ) 224*0Sstevel@tonic-gate { 225*0Sstevel@tonic-gate if ( (candidates = ldap_ufn_expand( ld, cancelproc, 226*0Sstevel@tonic-gate cancelparm, dns, fi->lfi_filter, scope, 227*0Sstevel@tonic-gate phase == 3 ? attrs : objattrs, 228*0Sstevel@tonic-gate phase == 3 ? attrsonly : 1, &err )) != NULL ) 229*0Sstevel@tonic-gate { 230*0Sstevel@tonic-gate break; 231*0Sstevel@tonic-gate } 232*0Sstevel@tonic-gate 233*0Sstevel@tonic-gate if ( err == -1 || err == LDAP_USER_CANCELLED ) { 234*0Sstevel@tonic-gate if ( dns != NULL ) { 235*0Sstevel@tonic-gate ldap_value_free( dns ); 236*0Sstevel@tonic-gate dns = NULL; 237*0Sstevel@tonic-gate } 238*0Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT ) 239*0Sstevel@tonic-gate UNLOCK_LDAP(ld); 240*0Sstevel@tonic-gate #endif 241*0Sstevel@tonic-gate return( err ); 242*0Sstevel@tonic-gate } 243*0Sstevel@tonic-gate } 244*0Sstevel@tonic-gate 245*0Sstevel@tonic-gate if ( candidates == NULL ) { 246*0Sstevel@tonic-gate if ( tries < 2 && phase != 3 ) { 247*0Sstevel@tonic-gate scope = LDAP_SCOPE_SUBTREE; 248*0Sstevel@tonic-gate goto tryagain; 249*0Sstevel@tonic-gate } else { 250*0Sstevel@tonic-gate if ( dns != NULL ) { 251*0Sstevel@tonic-gate ldap_value_free( dns ); 252*0Sstevel@tonic-gate dns = NULL; 253*0Sstevel@tonic-gate } 254*0Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT ) 255*0Sstevel@tonic-gate UNLOCK_LDAP(ld); 256*0Sstevel@tonic-gate #endif 257*0Sstevel@tonic-gate return( err ); 258*0Sstevel@tonic-gate } 259*0Sstevel@tonic-gate } 260*0Sstevel@tonic-gate 261*0Sstevel@tonic-gate /* go on to the next component */ 262*0Sstevel@tonic-gate if ( phase == 1 ) 263*0Sstevel@tonic-gate phase++; 264*0Sstevel@tonic-gate if ( dns != NULL ) { 265*0Sstevel@tonic-gate ldap_value_free( dns ); 266*0Sstevel@tonic-gate dns = NULL; 267*0Sstevel@tonic-gate } 268*0Sstevel@tonic-gate } 269*0Sstevel@tonic-gate *res = candidates; 270*0Sstevel@tonic-gate 271*0Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT ) 272*0Sstevel@tonic-gate UNLOCK_LDAP(ld); 273*0Sstevel@tonic-gate #endif 274*0Sstevel@tonic-gate return( err ); 275*0Sstevel@tonic-gate } 276*0Sstevel@tonic-gate 277*0Sstevel@tonic-gate int 278*0Sstevel@tonic-gate ldap_ufn_search_ct( LDAP *ld, char *ufn, char **attrs, int attrsonly, 279*0Sstevel@tonic-gate LDAPMessage **res, cancelptype cancelproc, void *cancelparm, 280*0Sstevel@tonic-gate char *tag1, char *tag2, char *tag3 ) 281*0Sstevel@tonic-gate { 282*0Sstevel@tonic-gate char **ufncomp, **prefixcomp; 283*0Sstevel@tonic-gate char *pbuf; 284*0Sstevel@tonic-gate int ncomp, pcomp, i, err; 285*0Sstevel@tonic-gate 286*0Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT ) 287*0Sstevel@tonic-gate LOCK_LDAP(ld); 288*0Sstevel@tonic-gate #endif 289*0Sstevel@tonic-gate /* initialize the getfilter stuff if it's not already */ 290*0Sstevel@tonic-gate if ( ld->ld_filtd == NULL && ldap_ufn_setfilter( ld, FILTERFILE ) 291*0Sstevel@tonic-gate == NULL ) { 292*0Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT ) 293*0Sstevel@tonic-gate UNLOCK_LDAP(ld); 294*0Sstevel@tonic-gate #endif 295*0Sstevel@tonic-gate return( ld->ld_errno = LDAP_LOCAL_ERROR ); 296*0Sstevel@tonic-gate } 297*0Sstevel@tonic-gate 298*0Sstevel@tonic-gate /* call ldap_explode_dn() to break the ufn into its components */ 299*0Sstevel@tonic-gate if ( (ufncomp = ldap_explode_dn( ufn, 0 )) == NULL ) { 300*0Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT ) 301*0Sstevel@tonic-gate UNLOCK_LDAP(ld); 302*0Sstevel@tonic-gate #endif 303*0Sstevel@tonic-gate return( ld->ld_errno = LDAP_LOCAL_ERROR ); 304*0Sstevel@tonic-gate } 305*0Sstevel@tonic-gate for ( ncomp = 0; ufncomp[ncomp] != NULL; ncomp++ ) 306*0Sstevel@tonic-gate ; /* NULL */ 307*0Sstevel@tonic-gate 308*0Sstevel@tonic-gate /* more than two components => try it fully qualified first */ 309*0Sstevel@tonic-gate if ( ncomp > 2 || ld->ld_ufnprefix == NULL ) { 310*0Sstevel@tonic-gate err = ldap_ufn_search_ctx( ld, ufncomp, ncomp, NULL, attrs, 311*0Sstevel@tonic-gate attrsonly, res, cancelproc, cancelparm, tag1, tag2, tag3 ); 312*0Sstevel@tonic-gate 313*0Sstevel@tonic-gate if ( ldap_count_entries( ld, *res ) > 0 ) { 314*0Sstevel@tonic-gate ldap_value_free( ufncomp ); 315*0Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT ) 316*0Sstevel@tonic-gate UNLOCK_LDAP(ld); 317*0Sstevel@tonic-gate #endif 318*0Sstevel@tonic-gate return( err ); 319*0Sstevel@tonic-gate } else { 320*0Sstevel@tonic-gate ldap_msgfree( *res ); 321*0Sstevel@tonic-gate *res = NULL; 322*0Sstevel@tonic-gate } 323*0Sstevel@tonic-gate } 324*0Sstevel@tonic-gate 325*0Sstevel@tonic-gate if ( ld->ld_ufnprefix == NULL ) { 326*0Sstevel@tonic-gate ldap_value_free( ufncomp ); 327*0Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT ) 328*0Sstevel@tonic-gate UNLOCK_LDAP(ld); 329*0Sstevel@tonic-gate #endif 330*0Sstevel@tonic-gate return( err ); 331*0Sstevel@tonic-gate } 332*0Sstevel@tonic-gate 333*0Sstevel@tonic-gate /* if that failed, or < 2 components, use the prefix */ 334*0Sstevel@tonic-gate if ( (prefixcomp = ldap_explode_dn( ld->ld_ufnprefix, 0 )) == NULL ) { 335*0Sstevel@tonic-gate ldap_value_free( ufncomp ); 336*0Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT ) 337*0Sstevel@tonic-gate UNLOCK_LDAP(ld); 338*0Sstevel@tonic-gate #endif 339*0Sstevel@tonic-gate return( ld->ld_errno = LDAP_LOCAL_ERROR ); 340*0Sstevel@tonic-gate } 341*0Sstevel@tonic-gate for ( pcomp = 0; prefixcomp[pcomp] != NULL; pcomp++ ) 342*0Sstevel@tonic-gate ; /* NULL */ 343*0Sstevel@tonic-gate if ( (pbuf = (char *) malloc( strlen( ld->ld_ufnprefix ) + 1 )) 344*0Sstevel@tonic-gate == NULL ) { 345*0Sstevel@tonic-gate ldap_value_free( ufncomp ); 346*0Sstevel@tonic-gate ldap_value_free( prefixcomp ); 347*0Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT ) 348*0Sstevel@tonic-gate UNLOCK_LDAP(ld); 349*0Sstevel@tonic-gate #endif 350*0Sstevel@tonic-gate return( ld->ld_errno = LDAP_NO_MEMORY ); 351*0Sstevel@tonic-gate } 352*0Sstevel@tonic-gate 353*0Sstevel@tonic-gate for ( i = 0; i < pcomp; i++ ) { 354*0Sstevel@tonic-gate int j; 355*0Sstevel@tonic-gate 356*0Sstevel@tonic-gate *pbuf = '\0'; 357*0Sstevel@tonic-gate for ( j = i; j < pcomp; j++ ) { 358*0Sstevel@tonic-gate (void) strcat( pbuf, prefixcomp[j] ); 359*0Sstevel@tonic-gate if ( j + 1 < pcomp ) 360*0Sstevel@tonic-gate (void) strcat( pbuf, "," ); 361*0Sstevel@tonic-gate } 362*0Sstevel@tonic-gate err = ldap_ufn_search_ctx( ld, ufncomp, ncomp, pbuf, attrs, 363*0Sstevel@tonic-gate attrsonly, res, cancelproc, cancelparm, tag1, tag2, tag3 ); 364*0Sstevel@tonic-gate 365*0Sstevel@tonic-gate if ( ldap_count_entries( ld, *res ) > 0 ) { 366*0Sstevel@tonic-gate break; 367*0Sstevel@tonic-gate } else { 368*0Sstevel@tonic-gate ldap_msgfree( *res ); 369*0Sstevel@tonic-gate *res = NULL; 370*0Sstevel@tonic-gate } 371*0Sstevel@tonic-gate } 372*0Sstevel@tonic-gate 373*0Sstevel@tonic-gate ldap_value_free( ufncomp ); 374*0Sstevel@tonic-gate ldap_value_free( prefixcomp ); 375*0Sstevel@tonic-gate free( pbuf ); 376*0Sstevel@tonic-gate 377*0Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT ) 378*0Sstevel@tonic-gate UNLOCK_LDAP(ld); 379*0Sstevel@tonic-gate #endif 380*0Sstevel@tonic-gate return( err ); 381*0Sstevel@tonic-gate } 382*0Sstevel@tonic-gate 383*0Sstevel@tonic-gate /* 384*0Sstevel@tonic-gate * same as ldap_ufn_search_ct, except without the ability to specify 385*0Sstevel@tonic-gate * ldapfilter.conf tags. 386*0Sstevel@tonic-gate */ 387*0Sstevel@tonic-gate int 388*0Sstevel@tonic-gate ldap_ufn_search_c( LDAP *ld, char *ufn, char **attrs, int attrsonly, 389*0Sstevel@tonic-gate LDAPMessage **res, cancelptype cancelproc, void *cancelparm ) 390*0Sstevel@tonic-gate { 391*0Sstevel@tonic-gate return( ldap_ufn_search_ct( ld, ufn, attrs, attrsonly, res, cancelproc, 392*0Sstevel@tonic-gate cancelparm, "ufn first", "ufn intermediate", "ufn last" ) ); 393*0Sstevel@tonic-gate } 394*0Sstevel@tonic-gate 395*0Sstevel@tonic-gate /* 396*0Sstevel@tonic-gate * same as ldap_ufn_search_c without the cancel function 397*0Sstevel@tonic-gate */ 398*0Sstevel@tonic-gate int 399*0Sstevel@tonic-gate ldap_ufn_search_s( LDAP *ld, char *ufn, char **attrs, int attrsonly, 400*0Sstevel@tonic-gate LDAPMessage **res ) 401*0Sstevel@tonic-gate { 402*0Sstevel@tonic-gate struct timeval tv; 403*0Sstevel@tonic-gate 404*0Sstevel@tonic-gate tv.tv_sec = ld->ld_timelimit; 405*0Sstevel@tonic-gate 406*0Sstevel@tonic-gate return( ldap_ufn_search_ct( ld, ufn, attrs, attrsonly, res, 407*0Sstevel@tonic-gate ld->ld_timelimit ? ldap_ufn_timeout : NULL, 408*0Sstevel@tonic-gate ld->ld_timelimit ? (void *) &tv : NULL, 409*0Sstevel@tonic-gate "ufn first", "ufn intermediate", "ufn last" ) ); 410*0Sstevel@tonic-gate } 411*0Sstevel@tonic-gate 412*0Sstevel@tonic-gate 413*0Sstevel@tonic-gate /* 414*0Sstevel@tonic-gate * ldap_msg_merge - merge two ldap search result chains. the more 415*0Sstevel@tonic-gate * serious of the two error result codes is kept. 416*0Sstevel@tonic-gate */ 417*0Sstevel@tonic-gate 418*0Sstevel@tonic-gate static LDAPMessage * 419*0Sstevel@tonic-gate ldap_msg_merge( LDAP *ld, LDAPMessage *a, LDAPMessage *b ) 420*0Sstevel@tonic-gate { 421*0Sstevel@tonic-gate LDAPMessage *end, *aprev, *aend, *bprev, *bend; 422*0Sstevel@tonic-gate 423*0Sstevel@tonic-gate if ( a == NULL ) 424*0Sstevel@tonic-gate return( b ); 425*0Sstevel@tonic-gate 426*0Sstevel@tonic-gate if ( b == NULL ) 427*0Sstevel@tonic-gate return( a ); 428*0Sstevel@tonic-gate 429*0Sstevel@tonic-gate /* find the ends of the a and b chains */ 430*0Sstevel@tonic-gate aprev = NULL; 431*0Sstevel@tonic-gate for ( aend = a; aend->lm_chain != NULL; aend = aend->lm_chain ) 432*0Sstevel@tonic-gate aprev = aend; 433*0Sstevel@tonic-gate bprev = NULL; 434*0Sstevel@tonic-gate for ( bend = b; bend->lm_chain != NULL; bend = bend->lm_chain ) 435*0Sstevel@tonic-gate bprev = bend; 436*0Sstevel@tonic-gate 437*0Sstevel@tonic-gate /* keep result a */ 438*0Sstevel@tonic-gate if ( ldap_result2error( ld, aend, 0 ) != LDAP_SUCCESS ) { 439*0Sstevel@tonic-gate /* remove result b */ 440*0Sstevel@tonic-gate ldap_msgfree( bend ); 441*0Sstevel@tonic-gate if ( bprev != NULL ) 442*0Sstevel@tonic-gate bprev->lm_chain = NULL; 443*0Sstevel@tonic-gate else 444*0Sstevel@tonic-gate b = NULL; 445*0Sstevel@tonic-gate end = aend; 446*0Sstevel@tonic-gate if ( aprev != NULL ) 447*0Sstevel@tonic-gate aprev->lm_chain = NULL; 448*0Sstevel@tonic-gate else 449*0Sstevel@tonic-gate a = NULL; 450*0Sstevel@tonic-gate /* keep result b */ 451*0Sstevel@tonic-gate } else { 452*0Sstevel@tonic-gate /* remove result a */ 453*0Sstevel@tonic-gate ldap_msgfree( aend ); 454*0Sstevel@tonic-gate if ( aprev != NULL ) 455*0Sstevel@tonic-gate aprev->lm_chain = NULL; 456*0Sstevel@tonic-gate else 457*0Sstevel@tonic-gate a = NULL; 458*0Sstevel@tonic-gate end = bend; 459*0Sstevel@tonic-gate if ( bprev != NULL ) 460*0Sstevel@tonic-gate bprev->lm_chain = NULL; 461*0Sstevel@tonic-gate else 462*0Sstevel@tonic-gate b = NULL; 463*0Sstevel@tonic-gate } 464*0Sstevel@tonic-gate 465*0Sstevel@tonic-gate if ( (a == NULL && b == NULL) || (a == NULL && bprev == NULL) || 466*0Sstevel@tonic-gate (b == NULL && aprev == NULL) ) 467*0Sstevel@tonic-gate return( end ); 468*0Sstevel@tonic-gate 469*0Sstevel@tonic-gate if ( a == NULL ) { 470*0Sstevel@tonic-gate bprev->lm_chain = end; 471*0Sstevel@tonic-gate return( b ); 472*0Sstevel@tonic-gate } else if ( b == NULL ) { 473*0Sstevel@tonic-gate aprev->lm_chain = end; 474*0Sstevel@tonic-gate return( a ); 475*0Sstevel@tonic-gate } else { 476*0Sstevel@tonic-gate bprev->lm_chain = end; 477*0Sstevel@tonic-gate aprev->lm_chain = b; 478*0Sstevel@tonic-gate return( a ); 479*0Sstevel@tonic-gate } 480*0Sstevel@tonic-gate } 481*0Sstevel@tonic-gate 482*0Sstevel@tonic-gate static LDAPMessage * 483*0Sstevel@tonic-gate ldap_ufn_expand( LDAP *ld, cancelptype cancelproc, void *cancelparm, 484*0Sstevel@tonic-gate char **dns, char *filter, int scope, char **attrs, int aonly, 485*0Sstevel@tonic-gate int *err ) 486*0Sstevel@tonic-gate { 487*0Sstevel@tonic-gate LDAPMessage *tmpcand, *tmpres; 488*0Sstevel@tonic-gate char *dn; 489*0Sstevel@tonic-gate int i, msgid; 490*0Sstevel@tonic-gate struct timeval tv; 491*0Sstevel@tonic-gate 492*0Sstevel@tonic-gate /* search for this component below the current candidates */ 493*0Sstevel@tonic-gate tmpcand = NULL; 494*0Sstevel@tonic-gate i = 0; 495*0Sstevel@tonic-gate do { 496*0Sstevel@tonic-gate if ( dns != NULL ) 497*0Sstevel@tonic-gate dn = dns[i]; 498*0Sstevel@tonic-gate else 499*0Sstevel@tonic-gate dn = ""; 500*0Sstevel@tonic-gate 501*0Sstevel@tonic-gate if (( msgid = ldap_search( ld, dn, scope, filter, attrs, 502*0Sstevel@tonic-gate aonly )) == -1 ) { 503*0Sstevel@tonic-gate ldap_msgfree( tmpcand ); 504*0Sstevel@tonic-gate *err = ld->ld_errno; 505*0Sstevel@tonic-gate return( NULL ); 506*0Sstevel@tonic-gate } 507*0Sstevel@tonic-gate 508*0Sstevel@tonic-gate tv.tv_sec = 0; 509*0Sstevel@tonic-gate tv.tv_usec = 100000; /* 1/10 of a second */ 510*0Sstevel@tonic-gate 511*0Sstevel@tonic-gate do { 512*0Sstevel@tonic-gate *err = ldap_result( ld, msgid, 1, &tv, &tmpres ); 513*0Sstevel@tonic-gate if ( *err == 0 && cancelproc != NULL && 514*0Sstevel@tonic-gate (*cancelproc)( cancelparm ) != 0 ) { 515*0Sstevel@tonic-gate ldap_abandon( ld, msgid ); 516*0Sstevel@tonic-gate *err = LDAP_USER_CANCELLED; 517*0Sstevel@tonic-gate ld->ld_errno = LDAP_USER_CANCELLED; 518*0Sstevel@tonic-gate } 519*0Sstevel@tonic-gate } while ( *err == 0 ); 520*0Sstevel@tonic-gate 521*0Sstevel@tonic-gate if ( *err == LDAP_USER_CANCELLED || *err < 0 || 522*0Sstevel@tonic-gate ( *err = ldap_result2error( ld, tmpres, 0 )) == -1 ) { 523*0Sstevel@tonic-gate ldap_msgfree( tmpcand ); 524*0Sstevel@tonic-gate return( NULL ); 525*0Sstevel@tonic-gate } 526*0Sstevel@tonic-gate 527*0Sstevel@tonic-gate tmpcand = ldap_msg_merge( ld, tmpcand, tmpres ); 528*0Sstevel@tonic-gate 529*0Sstevel@tonic-gate i++; 530*0Sstevel@tonic-gate } while ( dns != NULL && dns[i] != NULL ); 531*0Sstevel@tonic-gate 532*0Sstevel@tonic-gate if ( ldap_count_entries( ld, tmpcand ) > 0 ) { 533*0Sstevel@tonic-gate return( tmpcand ); 534*0Sstevel@tonic-gate } else { 535*0Sstevel@tonic-gate ldap_msgfree( tmpcand ); 536*0Sstevel@tonic-gate return( NULL ); 537*0Sstevel@tonic-gate } 538*0Sstevel@tonic-gate } 539*0Sstevel@tonic-gate 540*0Sstevel@tonic-gate /* 541*0Sstevel@tonic-gate * ldap_ufn_setfilter - set the filter config file used in ufn searching 542*0Sstevel@tonic-gate */ 543*0Sstevel@tonic-gate 544*0Sstevel@tonic-gate LDAPFiltDesc * 545*0Sstevel@tonic-gate ldap_ufn_setfilter( LDAP *ld, char *fname ) 546*0Sstevel@tonic-gate { 547*0Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT ) 548*0Sstevel@tonic-gate LDAPFiltDesc *rv; 549*0Sstevel@tonic-gate 550*0Sstevel@tonic-gate LOCK_LDAP(ld); 551*0Sstevel@tonic-gate #endif 552*0Sstevel@tonic-gate if ( ld->ld_filtd != NULL ) 553*0Sstevel@tonic-gate ldap_getfilter_free( ld->ld_filtd ); 554*0Sstevel@tonic-gate 555*0Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT ) 556*0Sstevel@tonic-gate ld->ld_filtd = ldap_init_getfilter( fname ); 557*0Sstevel@tonic-gate rv = ld->ld_filtd; 558*0Sstevel@tonic-gate UNLOCK_LDAP(ld); 559*0Sstevel@tonic-gate return( rv ); 560*0Sstevel@tonic-gate #else 561*0Sstevel@tonic-gate return( ld->ld_filtd = ldap_init_getfilter( fname ) ); 562*0Sstevel@tonic-gate #endif 563*0Sstevel@tonic-gate } 564*0Sstevel@tonic-gate 565*0Sstevel@tonic-gate void 566*0Sstevel@tonic-gate ldap_ufn_setprefix( LDAP *ld, char *prefix ) 567*0Sstevel@tonic-gate { 568*0Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT ) 569*0Sstevel@tonic-gate LOCK_LDAP(ld); 570*0Sstevel@tonic-gate #endif 571*0Sstevel@tonic-gate if ( ld->ld_ufnprefix != NULL ) 572*0Sstevel@tonic-gate free( ld->ld_ufnprefix ); 573*0Sstevel@tonic-gate 574*0Sstevel@tonic-gate ld->ld_ufnprefix = strdup( prefix ); 575*0Sstevel@tonic-gate #if defined( SUN ) && defined( _REENTRANT ) 576*0Sstevel@tonic-gate UNLOCK_LDAP(ld); 577*0Sstevel@tonic-gate #endif 578*0Sstevel@tonic-gate } 579*0Sstevel@tonic-gate 580*0Sstevel@tonic-gate int 581*0Sstevel@tonic-gate ldap_ufn_timeout( void *tvparam ) 582*0Sstevel@tonic-gate { 583*0Sstevel@tonic-gate struct timeval *tv; 584*0Sstevel@tonic-gate 585*0Sstevel@tonic-gate tv = (struct timeval *)tvparam; 586*0Sstevel@tonic-gate 587*0Sstevel@tonic-gate if ( tv->tv_sec != 0 ) { 588*0Sstevel@tonic-gate tv->tv_usec = tv->tv_sec * 1000000; /* sec => micro sec */ 589*0Sstevel@tonic-gate tv->tv_sec = 0; 590*0Sstevel@tonic-gate } 591*0Sstevel@tonic-gate tv->tv_usec -= 100000; /* 1/10 of a second */ 592*0Sstevel@tonic-gate 593*0Sstevel@tonic-gate return( tv->tv_usec <= 0 ? 1 : 0 ); 594*0Sstevel@tonic-gate } 595