3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1990 Regents of the University of Michigan.
17 #include <ac/stdlib.h>
20 #include <ac/socket.h>
21 #include <ac/string.h>
25 #include "ldap_defaults.h"
27 typedef int (*cancelptype) LDAP_P(( void *cancelparm ));
30 static int ldap_ufn_search_ctx LDAP_P(( LDAP *ld, char **ufncomp, int ncomp,
31 char *prefix, char **attrs, int attrsonly, LDAPMessage **res,
32 cancelptype cancelproc, void *cancelparm, char *tag1, char *tag2,
34 static LDAPMessage *ldap_msg_merge LDAP_P(( LDAP *ld, LDAPMessage *a, LDAPMessage *b ));
35 static LDAPMessage *ldap_ufn_expand LDAP_P(( LDAP *ld, cancelptype cancelproc,
36 void *cancelparm, char **dns, char *filter, int scope,
37 char **attrs, int aonly, int *err ));
40 * ldap_ufn_search_ctx - do user friendly searching; provide cancel feature;
41 * specify ldapfilter.conf tags for each phase of search
44 * ufncomp the exploded user friendly name to look for
45 * ncomp number of elements in ufncomp
46 * prefix where to start searching
47 * attrs list of attribute types to return for matches
48 * attrsonly 1 => attributes only 0 => attributes and values
49 * res will contain the result of the search
50 * cancelproc routine that returns non-zero if operation should be
51 * cancelled. This can be a null function pointer. If
52 * it is not 0, the routine will be called periodically.
53 * cancelparm void * that is passed to cancelproc
54 * tag[123] the ldapfilter.conf tag that will be used in phases
55 * 1, 2, and 3 of the search, respectively
58 * char *attrs[] = { "mail", "title", 0 };
59 * char *ufncomp[] = { "howes", "umich", "us", 0 }
61 * error = ldap_ufn_search_ctx( ld, ufncomp, 3, NULL, attrs, attrsonly,
62 * &res, acancelproc, along, "ufn first",
63 * "ufn intermediate", "ufn last" );
67 ldap_ufn_search_ctx( LDAP *ld, char **ufncomp, int ncomp, char *prefix,
68 char **attrs, int attrsonly, LDAPMessage **res, cancelptype cancelproc,
69 void *cancelparm, char *tag1, char *tag2, char *tag3 )
71 char *dn, *ftag = NULL;
73 int max, i, err, scope = 0, phase, tries;
76 LDAPMessage *candidates;
77 static char *objattrs[] = { "objectClass", NULL };
80 * look up ufn components from most to least significant.
82 * phase 1 search the root for orgs or countries
83 * phase 2 search for orgs
84 * phase 3 search for a person
85 * in phases 1 and 2, we are building a list of candidate DNs,
86 * below which we will search for the final component of the ufn.
87 * for each component we try the filters listed in the
88 * filterconfig file, first one-level (except the last compoment),
89 * then subtree. if any of them produce any results, we go on to
96 for ( ncomp--; ncomp != -1; ncomp-- ) {
97 if ( *ufncomp[ncomp] == '"' ) {
100 if ( (quote = strrchr( ufncomp[ncomp], '"' )) != NULL )
102 SAFEMEMCPY( ufncomp[ncomp], ufncomp[ncomp] + 1,
103 strlen( ufncomp[ncomp] + 1 ) + 1 );
111 scope = LDAP_SCOPE_ONELEVEL;
115 scope = LDAP_SCOPE_ONELEVEL;
119 scope = LDAP_SCOPE_SUBTREE;
124 * construct an array of DN's to search below from the
125 * list of candidates.
128 if ( candidates == NULL ) {
129 if ( prefix != NULL ) {
130 if ( (dns = (char **) LDAP_MALLOC( sizeof(char *)
132 return( ld->ld_errno = LDAP_NO_MEMORY );
134 dns[0] = LDAP_STRDUP( prefix );
141 for ( tmpcand = candidates; tmpcand != NULL &&
142 tmpcand->lm_msgtype != LDAP_RES_SEARCH_RESULT;
143 tmpcand = tmpcand->lm_chain )
145 if ( (dn = ldap_get_dn( ld, tmpcand )) == NULL )
149 if ( (dns = (char **) LDAP_MALLOC(
150 sizeof(char *) * 8 )) == NULL ) {
151 ld->ld_errno = LDAP_NO_MEMORY;
152 return( LDAP_NO_MEMORY );
155 } else if ( i >= max ) {
156 if ( (dns = (char **) LDAP_REALLOC( dns,
157 sizeof(char *) * 2 * max ))
160 ld->ld_errno = LDAP_NO_MEMORY;
161 return( LDAP_NO_MEMORY );
168 ldap_msgfree( candidates );
174 for ( fi = ldap_getfirstfilter( ld->ld_filtd, ftag,
175 ufncomp[ncomp] ); fi != NULL;
176 fi = ldap_getnextfilter( ld->ld_filtd ) )
178 if ( (candidates = ldap_ufn_expand( ld, cancelproc,
179 cancelparm, dns, fi->lfi_filter, scope,
180 phase == 3 ? attrs : objattrs,
181 phase == 3 ? attrsonly : 1, &err )) != NULL )
186 if ( err == -1 || err == LDAP_USER_CANCELLED ) {
188 ldap_value_free( dns );
195 if ( candidates == NULL ) {
196 if ( tries < 2 && phase != 3 ) {
197 scope = LDAP_SCOPE_SUBTREE;
201 ldap_value_free( dns );
208 /* go on to the next component */
212 ldap_value_free( dns );
223 LDAP *ld, LDAP_CONST char *ufn, char **attrs, int attrsonly,
224 LDAPMessage **res, cancelptype cancelproc, void *cancelparm,
225 char *tag1, char *tag2, char *tag3 )
227 char **ufncomp, **prefixcomp;
229 int ncomp, pcomp, i, err = 0;
231 /* initialize the getfilter stuff if it's not already */
232 if ( ld->ld_filtd == NULL && ldap_ufn_setfilter( ld, FILTERFILE )
234 return( ld->ld_errno = LDAP_LOCAL_ERROR );
237 /* call ldap_explode_dn() to break the ufn into its components */
238 if ( (ufncomp = ldap_explode_dn( ufn, 0 )) == NULL )
239 return( ld->ld_errno = LDAP_LOCAL_ERROR );
240 for ( ncomp = 0; ufncomp[ncomp] != NULL; ncomp++ )
243 /* more than two components => try it fully qualified first */
244 if ( ncomp > 2 || ld->ld_ufnprefix == NULL ) {
245 err = ldap_ufn_search_ctx( ld, ufncomp, ncomp, NULL, attrs,
246 attrsonly, res, cancelproc, cancelparm, tag1, tag2, tag3 );
248 if ( ldap_count_entries( ld, *res ) > 0 ) {
249 ldap_value_free( ufncomp );
252 ldap_msgfree( *res );
257 if ( ld->ld_ufnprefix == NULL ) {
258 ldap_value_free( ufncomp );
262 /* if that failed, or < 2 components, use the prefix */
263 if ( (prefixcomp = ldap_explode_dn( ld->ld_ufnprefix, 0 )) == NULL ) {
264 ldap_value_free( ufncomp );
265 return( ld->ld_errno = LDAP_LOCAL_ERROR );
267 for ( pcomp = 0; prefixcomp[pcomp] != NULL; pcomp++ )
269 if ( (pbuf = (char *) LDAP_MALLOC( strlen( ld->ld_ufnprefix ) + 1 ))
271 ldap_value_free( ufncomp );
272 ldap_value_free( prefixcomp );
273 return( ld->ld_errno = LDAP_NO_MEMORY );
276 for ( i = 0; i < pcomp; i++ ) {
280 for ( j = i; j < pcomp; j++ ) {
281 strcat( pbuf, prefixcomp[j] );
285 err = ldap_ufn_search_ctx( ld, ufncomp, ncomp, pbuf, attrs,
286 attrsonly, res, cancelproc, cancelparm, tag1, tag2, tag3 );
288 if ( ldap_count_entries( ld, *res ) > 0 ) {
291 ldap_msgfree( *res );
296 ldap_value_free( ufncomp );
297 ldap_value_free( prefixcomp );
304 * same as ldap_ufn_search_ct, except without the ability to specify
305 * ldapfilter.conf tags.
309 LDAP *ld, LDAP_CONST char *ufn, char **attrs, int attrsonly,
310 LDAPMessage **res, cancelptype cancelproc, void *cancelparm )
312 return( ldap_ufn_search_ct( ld, ufn, attrs, attrsonly, res, cancelproc,
313 cancelparm, "ufn first", "ufn intermediate", "ufn last" ) );
317 * same as ldap_ufn_search_c without the cancel function
321 LDAP *ld, LDAP_CONST char *ufn, char **attrs, int attrsonly,
326 tv.tv_sec = ld->ld_timelimit;
328 return( ldap_ufn_search_ct( ld, ufn, attrs, attrsonly, res,
329 ld->ld_timelimit ? ldap_ufn_timeout : NULL,
330 ld->ld_timelimit ? (void *) &tv : NULL,
331 "ufn first", "ufn intermediate", "ufn last" ) );
336 * ldap_msg_merge - merge two ldap search result chains. the more
337 * serious of the two error result codes is kept.
341 ldap_msg_merge( LDAP *ld, LDAPMessage *a, LDAPMessage *b )
343 LDAPMessage *end, *aprev, *aend, *bprev, *bend;
351 /* find the ends of the a and b chains */
353 for ( aend = a; aend->lm_chain != NULL; aend = aend->lm_chain )
356 for ( bend = b; bend->lm_chain != NULL; bend = bend->lm_chain )
360 if ( ldap_result2error( ld, aend, 0 ) != LDAP_SUCCESS ) {
361 /* remove result b */
362 ldap_msgfree( bend );
364 bprev->lm_chain = NULL;
369 aprev->lm_chain = NULL;
374 /* remove result a */
375 ldap_msgfree( aend );
377 aprev->lm_chain = NULL;
382 bprev->lm_chain = NULL;
387 if ( (a == NULL && b == NULL) || (a == NULL && bprev == NULL) ||
388 (b == NULL && aprev == NULL) )
392 bprev->lm_chain = end;
394 } else if ( b == NULL ) {
395 aprev->lm_chain = end;
398 bprev->lm_chain = end;
405 ldap_ufn_expand( LDAP *ld, cancelptype cancelproc, void *cancelparm,
406 char **dns, char *filter, int scope, char **attrs, int aonly,
409 LDAPMessage *tmpcand, *tmpres;
414 /* search for this component below the current candidates */
423 if (( msgid = ldap_search( ld, dn, scope, filter, attrs,
425 ldap_msgfree( tmpcand );
431 tv.tv_usec = 100000; /* 1/10 of a second */
434 *err = ldap_result( ld, msgid, 1, &tv, &tmpres );
435 if ( *err == 0 && cancelproc != 0 &&
436 (*cancelproc)( cancelparm ) != 0 ) {
437 ldap_abandon( ld, msgid );
438 *err = LDAP_USER_CANCELLED;
439 ld->ld_errno = LDAP_USER_CANCELLED;
441 } while ( *err == 0 );
443 if ( *err == LDAP_USER_CANCELLED || *err < 0 ||
444 ( *err = ldap_result2error( ld, tmpres, 0 )) == -1 ) {
445 ldap_msgfree( tmpcand );
449 tmpcand = ldap_msg_merge( ld, tmpcand, tmpres );
452 } while ( dns != NULL && dns[i] != NULL );
454 if ( ldap_count_entries( ld, tmpcand ) > 0 ) {
457 ldap_msgfree( tmpcand );
463 * ldap_ufn_setfilter - set the filter config file used in ufn searching
467 ldap_ufn_setfilter( LDAP *ld, LDAP_CONST char *fname )
469 if ( ld->ld_filtd != NULL )
470 ldap_getfilter_free( ld->ld_filtd );
472 return( ld->ld_filtd = ldap_init_getfilter( fname ) );
476 ldap_ufn_setprefix( LDAP *ld, LDAP_CONST char *prefix )
478 if ( ld->ld_ufnprefix != NULL )
479 LDAP_FREE( ld->ld_ufnprefix );
481 ld->ld_ufnprefix = LDAP_STRDUP( prefix );
485 ldap_ufn_timeout( void *tvparam )
489 tv = (struct timeval *)tvparam;
491 if ( tv->tv_sec != 0 ) {
492 tv->tv_usec = tv->tv_sec * 1000000; /* sec => micro sec */
495 tv->tv_usec -= 100000; /* 1/10 of a second */
497 return( tv->tv_usec <= 0 ? 1 : 0 );