3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1990 Regents of the University of Michigan.
19 #include <ac/stdlib.h>
21 #include <ac/socket.h>
22 #include <ac/string.h>
26 #include "ldap_defaults.h"
28 typedef int (*cancelptype) LDAP_P(( void *cancelparm ));
31 static int ldap_ufn_search_ctx LDAP_P(( LDAP *ld, char **ufncomp, int ncomp,
32 char *prefix, char **attrs, int attrsonly, LDAPMessage **res,
33 cancelptype cancelproc, void *cancelparm, char *tag1, char *tag2,
35 static LDAPMessage *ldap_msg_merge LDAP_P(( LDAP *ld, LDAPMessage *a, LDAPMessage *b ));
36 static LDAPMessage *ldap_ufn_expand LDAP_P(( LDAP *ld, cancelptype cancelproc,
37 void *cancelparm, char **dns, char *filter, int scope,
38 char **attrs, int aonly, int *err ));
41 * ldap_ufn_search_ctx - do user friendly searching; provide cancel feature;
42 * specify ldapfilter.conf tags for each phase of search
45 * ufncomp the exploded user friendly name to look for
46 * ncomp number of elements in ufncomp
47 * prefix where to start searching
48 * attrs list of attribute types to return for matches
49 * attrsonly 1 => attributes only 0 => attributes and values
50 * res will contain the result of the search
51 * cancelproc routine that returns non-zero if operation should be
52 * cancelled. This can be a null function pointer. If
53 * it is not 0, the routine will be called periodically.
54 * cancelparm void * that is passed to cancelproc
55 * tag[123] the ldapfilter.conf tag that will be used in phases
56 * 1, 2, and 3 of the search, respectively
59 * char *attrs[] = { "mail", "title", 0 };
60 * char *ufncomp[] = { "howes", "umich", "us", 0 }
62 * error = ldap_ufn_search_ctx( ld, ufncomp, 3, NULL, attrs, attrsonly,
63 * &res, acancelproc, along, "ufn first",
64 * "ufn intermediate", "ufn last" );
68 ldap_ufn_search_ctx( LDAP *ld, char **ufncomp, int ncomp, char *prefix,
69 char **attrs, int attrsonly, LDAPMessage **res, cancelptype cancelproc,
70 void *cancelparm, char *tag1, char *tag2, char *tag3 )
72 char *dn, *ftag = NULL;
74 int max, i, err, scope = 0, phase, tries;
77 LDAPMessage *candidates;
78 static char *objattrs[] = { "objectClass", NULL };
81 * look up ufn components from most to least significant.
83 * phase 1 search the root for orgs or countries
84 * phase 2 search for orgs
85 * phase 3 search for a person
86 * in phases 1 and 2, we are building a list of candidate DNs,
87 * below which we will search for the final component of the ufn.
88 * for each component we try the filters listed in the
89 * filterconfig file, first one-level (except the last compoment),
90 * then subtree. if any of them produce any results, we go on to
97 for ( ncomp--; ncomp != -1; ncomp-- ) {
98 if ( *ufncomp[ncomp] == '"' ) {
101 if ( (quote = strrchr( ufncomp[ncomp], '"' )) != NULL )
103 AC_MEMCPY( ufncomp[ncomp], ufncomp[ncomp] + 1,
104 strlen( ufncomp[ncomp] + 1 ) + 1 );
112 scope = LDAP_SCOPE_ONELEVEL;
116 scope = LDAP_SCOPE_ONELEVEL;
120 scope = LDAP_SCOPE_SUBTREE;
125 * construct an array of DN's to search below from the
126 * list of candidates.
129 if ( candidates == NULL ) {
130 if ( prefix != NULL ) {
131 if ( (dns = (char **) LDAP_MALLOC( sizeof(char *)
133 return( ld->ld_errno = LDAP_NO_MEMORY );
135 dns[0] = LDAP_STRDUP( prefix );
142 for ( tmpcand = candidates; tmpcand != NULL &&
143 tmpcand->lm_msgtype != LDAP_RES_SEARCH_RESULT;
144 tmpcand = tmpcand->lm_chain )
146 if ( (dn = ldap_get_dn( ld, tmpcand )) == NULL )
150 if ( (dns = (char **) LDAP_MALLOC(
151 sizeof(char *) * 8 )) == NULL ) {
152 ld->ld_errno = LDAP_NO_MEMORY;
153 return( LDAP_NO_MEMORY );
156 } else if ( i >= max ) {
157 if ( (dns = (char **) LDAP_REALLOC( dns,
158 sizeof(char *) * 2 * max ))
161 ld->ld_errno = LDAP_NO_MEMORY;
162 return( LDAP_NO_MEMORY );
169 ldap_msgfree( candidates );
175 for ( fi = ldap_getfirstfilter( ld->ld_filtd, ftag,
176 ufncomp[ncomp] ); fi != NULL;
177 fi = ldap_getnextfilter( ld->ld_filtd ) )
179 if ( (candidates = ldap_ufn_expand( ld, cancelproc,
180 cancelparm, dns, fi->lfi_filter, scope,
181 phase == 3 ? attrs : objattrs,
182 phase == 3 ? attrsonly : 1, &err )) != NULL )
187 if ( err == -1 || err == LDAP_USER_CANCELLED ) {
196 if ( candidates == NULL ) {
197 if ( tries < 2 && phase != 3 ) {
198 scope = LDAP_SCOPE_SUBTREE;
209 /* go on to the next component */
224 LDAP *ld, LDAP_CONST char *ufn, char **attrs, int attrsonly,
225 LDAPMessage **res, cancelptype cancelproc, void *cancelparm,
226 char *tag1, char *tag2, char *tag3 )
228 char **ufncomp, **prefixcomp;
230 int ncomp, pcomp, i, err = 0;
232 /* initialize the getfilter stuff if it's not already */
233 if ( ld->ld_filtd == NULL && ldap_ufn_setfilter( ld, FILTERFILE )
235 return( ld->ld_errno = LDAP_LOCAL_ERROR );
238 /* call ldap_explode_dn() to break the ufn into its components */
239 if ( (ufncomp = ldap_explode_dn( ufn, 0 )) == NULL )
240 return( ld->ld_errno = LDAP_LOCAL_ERROR );
241 for ( ncomp = 0; ufncomp[ncomp] != NULL; ncomp++ )
244 /* more than two components => try it fully qualified first */
245 if ( ncomp > 2 || ld->ld_ufnprefix == NULL ) {
246 err = ldap_ufn_search_ctx( ld, ufncomp, ncomp, NULL, attrs,
247 attrsonly, res, cancelproc, cancelparm, tag1, tag2, tag3 );
249 if ( ldap_count_entries( ld, *res ) > 0 ) {
250 LDAP_VFREE( ufncomp );
253 ldap_msgfree( *res );
258 if ( ld->ld_ufnprefix == NULL ) {
259 LDAP_VFREE( ufncomp );
263 /* if that failed, or < 2 components, use the prefix */
264 if ( (prefixcomp = ldap_explode_dn( ld->ld_ufnprefix, 0 )) == NULL ) {
265 LDAP_VFREE( ufncomp );
266 return( ld->ld_errno = LDAP_LOCAL_ERROR );
268 for ( pcomp = 0; prefixcomp[pcomp] != NULL; pcomp++ )
270 if ( (pbuf = (char *) LDAP_MALLOC( strlen( ld->ld_ufnprefix ) + 1 ))
272 LDAP_VFREE( ufncomp );
273 LDAP_VFREE( prefixcomp );
274 return( ld->ld_errno = LDAP_NO_MEMORY );
277 for ( i = 0; i < pcomp; i++ ) {
281 for ( j = i; j < pcomp; j++ ) {
282 strcat( pbuf, prefixcomp[j] );
286 err = ldap_ufn_search_ctx( ld, ufncomp, ncomp, pbuf, attrs,
287 attrsonly, res, cancelproc, cancelparm, tag1, tag2, tag3 );
289 if ( ldap_count_entries( ld, *res ) > 0 ) {
292 ldap_msgfree( *res );
297 LDAP_VFREE( ufncomp );
298 LDAP_VFREE( prefixcomp );
305 * same as ldap_ufn_search_ct, except without the ability to specify
306 * ldapfilter.conf tags.
310 LDAP *ld, LDAP_CONST char *ufn, char **attrs, int attrsonly,
311 LDAPMessage **res, cancelptype cancelproc, void *cancelparm )
313 return( ldap_ufn_search_ct( ld, ufn, attrs, attrsonly, res, cancelproc,
314 cancelparm, "ufn first", "ufn intermediate", "ufn last" ) );
318 * same as ldap_ufn_search_c without the cancel function
322 LDAP *ld, LDAP_CONST char *ufn, char **attrs, int attrsonly,
327 tv.tv_sec = ld->ld_timelimit;
329 return( ldap_ufn_search_ct( ld, ufn, attrs, attrsonly, res,
330 ld->ld_timelimit ? ldap_ufn_timeout : NULL,
331 ld->ld_timelimit ? (void *) &tv : NULL,
332 "ufn first", "ufn intermediate", "ufn last" ) );
337 * ldap_msg_merge - merge two ldap search result chains. the more
338 * serious of the two error result codes is kept.
342 ldap_msg_merge( LDAP *ld, LDAPMessage *a, LDAPMessage *b )
344 LDAPMessage *end, *aprev, *aend, *bprev, *bend;
352 /* find the ends of the a and b chains */
354 for ( aend = a; aend->lm_chain != NULL; aend = aend->lm_chain )
357 for ( bend = b; bend->lm_chain != NULL; bend = bend->lm_chain )
361 if ( ldap_result2error( ld, aend, 0 ) != LDAP_SUCCESS ) {
362 /* remove result b */
363 ldap_msgfree( bend );
365 bprev->lm_chain = NULL;
370 aprev->lm_chain = NULL;
375 /* remove result a */
376 ldap_msgfree( aend );
378 aprev->lm_chain = NULL;
383 bprev->lm_chain = NULL;
388 if ( (a == NULL && b == NULL) || (a == NULL && bprev == NULL) ||
389 (b == NULL && aprev == NULL) )
393 bprev->lm_chain = end;
395 } else if ( b == NULL ) {
396 aprev->lm_chain = end;
399 bprev->lm_chain = end;
406 ldap_ufn_expand( LDAP *ld, cancelptype cancelproc, void *cancelparm,
407 char **dns, char *filter, int scope, char **attrs, int aonly,
410 LDAPMessage *tmpcand, *tmpres;
415 /* search for this component below the current candidates */
424 if (( msgid = ldap_search( ld, dn, scope, filter, attrs,
426 ldap_msgfree( tmpcand );
432 tv.tv_usec = 100000; /* 1/10 of a second */
435 *err = ldap_result( ld, msgid, 1, &tv, &tmpres );
436 if ( *err == 0 && cancelproc != 0 &&
437 (*cancelproc)( cancelparm ) != 0 ) {
438 ldap_abandon( ld, msgid );
439 *err = LDAP_USER_CANCELLED;
440 ld->ld_errno = LDAP_USER_CANCELLED;
442 } while ( *err == 0 );
444 if ( *err == LDAP_USER_CANCELLED || *err < 0 ||
445 ( *err = ldap_result2error( ld, tmpres, 0 )) == -1 ) {
446 ldap_msgfree( tmpcand );
450 tmpcand = ldap_msg_merge( ld, tmpcand, tmpres );
453 } while ( dns != NULL && dns[i] != NULL );
455 if ( ldap_count_entries( ld, tmpcand ) > 0 ) {
458 ldap_msgfree( tmpcand );
464 * ldap_ufn_setfilter - set the filter config file used in ufn searching
468 ldap_ufn_setfilter( LDAP *ld, LDAP_CONST char *fname )
470 if ( ld->ld_filtd != NULL )
471 ldap_getfilter_free( ld->ld_filtd );
473 return( ld->ld_filtd = ldap_init_getfilter( fname ) );
477 ldap_ufn_setprefix( LDAP *ld, LDAP_CONST char *prefix )
479 if ( ld->ld_ufnprefix != NULL )
480 LDAP_FREE( ld->ld_ufnprefix );
482 ld->ld_ufnprefix = prefix == NULL
483 ? NULL : LDAP_STRDUP( prefix );
487 ldap_ufn_timeout( void *tvparam )
491 tv = (struct timeval *)tvparam;
493 if ( tv->tv_sec != 0 ) {
494 tv->tv_usec = tv->tv_sec * 1000000; /* sec => micro sec */
497 tv->tv_usec -= 100000; /* 1/10 of a second */
499 return( tv->tv_usec <= 0 ? 1 : 0 );