2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright (c) 1990 Regents of the University of Michigan.
15 #include <ac/string.h>
19 #include <ac/socket.h>
26 #include "ldapconfig.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 NULL. If it is non-NULL, the
53 * 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 SAFEMEMCPY( 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 **) 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 **) 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 **) 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 ) {
189 ldap_value_free( dns );
196 if ( candidates == NULL ) {
197 if ( tries < 2 && phase != 3 ) {
198 scope = LDAP_SCOPE_SUBTREE;
202 ldap_value_free( dns );
209 /* go on to the next component */
213 ldap_value_free( dns );
223 ldap_ufn_search_ct( LDAP *ld, 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 *) 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.
308 ldap_ufn_search_c( LDAP *ld, char *ufn, char **attrs, int attrsonly,
309 LDAPMessage **res, cancelptype cancelproc, void *cancelparm )
311 return( ldap_ufn_search_ct( ld, ufn, attrs, attrsonly, res, cancelproc,
312 cancelparm, "ufn first", "ufn intermediate", "ufn last" ) );
316 * same as ldap_ufn_search_c without the cancel function
319 ldap_ufn_search_s( LDAP *ld, char *ufn, char **attrs, int attrsonly,
324 tv.tv_sec = ld->ld_timelimit;
326 return( ldap_ufn_search_ct( ld, ufn, attrs, attrsonly, res,
327 ld->ld_timelimit ? ldap_ufn_timeout : NULL,
328 ld->ld_timelimit ? (void *) &tv : NULL,
329 "ufn first", "ufn intermediate", "ufn last" ) );
334 * ldap_msg_merge - merge two ldap search result chains. the more
335 * serious of the two error result codes is kept.
339 ldap_msg_merge( LDAP *ld, LDAPMessage *a, LDAPMessage *b )
341 LDAPMessage *end, *aprev, *aend, *bprev, *bend;
349 /* find the ends of the a and b chains */
351 for ( aend = a; aend->lm_chain != NULL; aend = aend->lm_chain )
354 for ( bend = b; bend->lm_chain != NULL; bend = bend->lm_chain )
358 if ( ldap_result2error( ld, aend, 0 ) != LDAP_SUCCESS ) {
359 /* remove result b */
360 ldap_msgfree( bend );
362 bprev->lm_chain = NULL;
367 aprev->lm_chain = NULL;
372 /* remove result a */
373 ldap_msgfree( aend );
375 aprev->lm_chain = NULL;
380 bprev->lm_chain = NULL;
385 if ( (a == NULL && b == NULL) || (a == NULL && bprev == NULL) ||
386 (b == NULL && aprev == NULL) )
390 bprev->lm_chain = end;
392 } else if ( b == NULL ) {
393 aprev->lm_chain = end;
396 bprev->lm_chain = end;
403 ldap_ufn_expand( LDAP *ld, cancelptype cancelproc, void *cancelparm,
404 char **dns, char *filter, int scope, char **attrs, int aonly,
407 LDAPMessage *tmpcand, *tmpres;
412 /* search for this component below the current candidates */
421 if (( msgid = ldap_search( ld, dn, scope, filter, attrs,
423 ldap_msgfree( tmpcand );
429 tv.tv_usec = 100000; /* 1/10 of a second */
432 *err = ldap_result( ld, msgid, 1, &tv, &tmpres );
433 if ( *err == 0 && cancelproc != NULL &&
434 (*cancelproc)( cancelparm ) != 0 ) {
435 ldap_abandon( ld, msgid );
436 *err = LDAP_USER_CANCELLED;
437 ld->ld_errno = LDAP_USER_CANCELLED;
439 } while ( *err == 0 );
441 if ( *err == LDAP_USER_CANCELLED || *err < 0 ||
442 ( *err = ldap_result2error( ld, tmpres, 0 )) == -1 ) {
443 ldap_msgfree( tmpcand );
447 tmpcand = ldap_msg_merge( ld, tmpcand, tmpres );
450 } while ( dns != NULL && dns[i] != NULL );
452 if ( ldap_count_entries( ld, tmpcand ) > 0 ) {
455 ldap_msgfree( tmpcand );
461 * ldap_ufn_setfilter - set the filter config file used in ufn searching
465 ldap_ufn_setfilter( LDAP *ld, char *fname )
467 if ( ld->ld_filtd != NULL )
468 ldap_getfilter_free( ld->ld_filtd );
470 return( ld->ld_filtd = ldap_init_getfilter( fname ) );
474 ldap_ufn_setprefix( LDAP *ld, char *prefix )
476 if ( ld->ld_ufnprefix != NULL )
477 free( ld->ld_ufnprefix );
479 ld->ld_ufnprefix = ldap_strdup( prefix );
483 ldap_ufn_timeout( void *tvparam )
487 tv = (struct timeval *)tvparam;
489 if ( tv->tv_sec != 0 ) {
490 tv->tv_usec = tv->tv_sec * 1000000; /* sec => micro sec */
493 tv->tv_usec -= 100000; /* 1/10 of a second */
495 return( tv->tv_usec <= 0 ? 1 : 0 );