2 * Copyright (c) 1990 Regents of the University of Michigan.
11 static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
15 #include <ac/string.h>
19 #include <ac/socket.h>
23 #include "ldapconfig.h"
25 typedef int (*cancelptype) LDAP_P(( void *cancelparm ));
27 static int ldap_ufn_search_ctx LDAP_P(( LDAP *ld, char **ufncomp, int ncomp,
28 char *prefix, char **attrs, int attrsonly, LDAPMessage **res,
29 cancelptype cancelproc, void *cancelparm, char *tag1, char *tag2,
31 static LDAPMessage *ldap_msg_merge LDAP_P(( LDAP *ld, LDAPMessage *a, LDAPMessage *b ));
32 static LDAPMessage *ldap_ufn_expand LDAP_P(( LDAP *ld, cancelptype cancelproc,
33 void *cancelparm, char **dns, char *filter, int scope,
34 char **attrs, int aonly, int *err ));
37 * ldap_ufn_search_ctx - do user friendly searching; provide cancel feature;
38 * specify ldapfilter.conf tags for each phase of search
41 * ufncomp the exploded user friendly name to look for
42 * ncomp number of elements in ufncomp
43 * prefix where to start searching
44 * attrs list of attribute types to return for matches
45 * attrsonly 1 => attributes only 0 => attributes and values
46 * res will contain the result of the search
47 * cancelproc routine that returns non-zero if operation should be
48 * cancelled. This can be NULL. If it is non-NULL, the
49 * routine will be called periodically.
50 * cancelparm void * that is passed to cancelproc
51 * tag[123] the ldapfilter.conf tag that will be used in phases
52 * 1, 2, and 3 of the search, respectively
55 * char *attrs[] = { "mail", "title", 0 };
56 * char *ufncomp[] = { "howes", "umich", "us", 0 }
58 * error = ldap_ufn_search_ctx( ld, ufncomp, 3, NULL, attrs, attrsonly,
59 * &res, acancelproc, along, "ufn first",
60 * "ufn intermediate", "ufn last" );
64 ldap_ufn_search_ctx( LDAP *ld, char **ufncomp, int ncomp, char *prefix,
65 char **attrs, int attrsonly, LDAPMessage **res, cancelptype cancelproc,
66 void *cancelparm, char *tag1, char *tag2, char *tag3 )
68 char *dn, *ftag = NULL;
70 int max, i, err, scope = 0, phase, tries;
73 LDAPMessage *candidates;
74 LDAPMessage *ldap_msg_merge(), *ldap_ufn_expand();
75 static char *objattrs[] = { "objectClass", NULL };
78 * look up ufn components from most to least significant.
80 * phase 1 search the root for orgs or countries
81 * phase 2 search for orgs
82 * phase 3 search for a person
83 * in phases 1 and 2, we are building a list of candidate DNs,
84 * below which we will search for the final component of the ufn.
85 * for each component we try the filters listed in the
86 * filterconfig file, first one-level (except the last compoment),
87 * then subtree. if any of them produce any results, we go on to
94 for ( ncomp--; ncomp != -1; ncomp-- ) {
95 if ( *ufncomp[ncomp] == '"' ) {
98 if ( (quote = strrchr( ufncomp[ncomp], '"' )) != NULL )
100 strcpy( ufncomp[ncomp], ufncomp[ncomp] + 1 );
108 scope = LDAP_SCOPE_ONELEVEL;
112 scope = LDAP_SCOPE_ONELEVEL;
116 scope = LDAP_SCOPE_SUBTREE;
121 * construct an array of DN's to search below from the
122 * list of candidates.
125 if ( candidates == NULL ) {
126 if ( prefix != NULL ) {
127 if ( (dns = (char **) malloc( sizeof(char *)
129 return( ld->ld_errno = LDAP_NO_MEMORY );
131 dns[0] = strdup( prefix );
138 for ( tmpcand = candidates; tmpcand != NULL &&
139 tmpcand->lm_msgtype != LDAP_RES_SEARCH_RESULT;
140 tmpcand = tmpcand->lm_chain )
142 if ( (dn = ldap_get_dn( ld, tmpcand )) == NULL )
146 if ( (dns = (char **) malloc(
147 sizeof(char *) * 8 )) == NULL ) {
148 ld->ld_errno = LDAP_NO_MEMORY;
149 return( LDAP_NO_MEMORY );
152 } else if ( i >= max ) {
153 if ( (dns = (char **) realloc( dns,
154 sizeof(char *) * 2 * max ))
157 ld->ld_errno = LDAP_NO_MEMORY;
158 return( LDAP_NO_MEMORY );
165 ldap_msgfree( candidates );
171 for ( fi = ldap_getfirstfilter( ld->ld_filtd, ftag,
172 ufncomp[ncomp] ); fi != NULL;
173 fi = ldap_getnextfilter( ld->ld_filtd ) )
175 if ( (candidates = ldap_ufn_expand( ld, cancelproc,
176 cancelparm, dns, fi->lfi_filter, scope,
177 phase == 3 ? attrs : objattrs,
178 phase == 3 ? attrsonly : 1, &err )) != NULL )
183 if ( err == -1 || err == LDAP_USER_CANCELLED ) {
185 ldap_value_free( dns );
192 if ( candidates == NULL ) {
193 if ( tries < 2 && phase != 3 ) {
194 scope = LDAP_SCOPE_SUBTREE;
198 ldap_value_free( dns );
205 /* go on to the next component */
209 ldap_value_free( dns );
219 ldap_ufn_search_ct( LDAP *ld, char *ufn, char **attrs, int attrsonly,
220 LDAPMessage **res, cancelptype cancelproc, void *cancelparm,
221 char *tag1, char *tag2, char *tag3 )
223 char **ufncomp, **prefixcomp;
225 int ncomp, pcomp, i, err = 0;
227 /* initialize the getfilter stuff if it's not already */
228 if ( ld->ld_filtd == NULL && ldap_ufn_setfilter( ld, FILTERFILE )
230 return( ld->ld_errno = LDAP_LOCAL_ERROR );
233 /* call ldap_explode_dn() to break the ufn into its components */
234 if ( (ufncomp = ldap_explode_dn( ufn, 0 )) == NULL )
235 return( ld->ld_errno = LDAP_LOCAL_ERROR );
236 for ( ncomp = 0; ufncomp[ncomp] != NULL; ncomp++ )
239 /* more than two components => try it fully qualified first */
240 if ( ncomp > 2 || ld->ld_ufnprefix == NULL ) {
241 err = ldap_ufn_search_ctx( ld, ufncomp, ncomp, NULL, attrs,
242 attrsonly, res, cancelproc, cancelparm, tag1, tag2, tag3 );
244 if ( ldap_count_entries( ld, *res ) > 0 ) {
245 ldap_value_free( ufncomp );
248 ldap_msgfree( *res );
253 if ( ld->ld_ufnprefix == NULL ) {
254 ldap_value_free( ufncomp );
258 /* if that failed, or < 2 components, use the prefix */
259 if ( (prefixcomp = ldap_explode_dn( ld->ld_ufnprefix, 0 )) == NULL ) {
260 ldap_value_free( ufncomp );
261 return( ld->ld_errno = LDAP_LOCAL_ERROR );
263 for ( pcomp = 0; prefixcomp[pcomp] != NULL; pcomp++ )
265 if ( (pbuf = (char *) malloc( strlen( ld->ld_ufnprefix ) + 1 ))
267 ldap_value_free( ufncomp );
268 ldap_value_free( prefixcomp );
269 return( ld->ld_errno = LDAP_NO_MEMORY );
272 for ( i = 0; i < pcomp; i++ ) {
276 for ( j = i; j < pcomp; j++ ) {
277 strcat( pbuf, prefixcomp[j] );
281 err = ldap_ufn_search_ctx( ld, ufncomp, ncomp, pbuf, attrs,
282 attrsonly, res, cancelproc, cancelparm, tag1, tag2, tag3 );
284 if ( ldap_count_entries( ld, *res ) > 0 ) {
287 ldap_msgfree( *res );
292 ldap_value_free( ufncomp );
293 ldap_value_free( prefixcomp );
300 * same as ldap_ufn_search_ct, except without the ability to specify
301 * ldapfilter.conf tags.
304 ldap_ufn_search_c( LDAP *ld, char *ufn, char **attrs, int attrsonly,
305 LDAPMessage **res, cancelptype cancelproc, void *cancelparm )
307 return( ldap_ufn_search_ct( ld, ufn, attrs, attrsonly, res, cancelproc,
308 cancelparm, "ufn first", "ufn intermediate", "ufn last" ) );
312 * same as ldap_ufn_search_c without the cancel function
315 ldap_ufn_search_s( LDAP *ld, char *ufn, char **attrs, int attrsonly,
320 tv.tv_sec = ld->ld_timelimit;
322 return( ldap_ufn_search_ct( ld, ufn, attrs, attrsonly, res,
323 ld->ld_timelimit ? ldap_ufn_timeout : NULL,
324 ld->ld_timelimit ? (void *) &tv : NULL,
325 "ufn first", "ufn intermediate", "ufn last" ) );
330 * ldap_msg_merge - merge two ldap search result chains. the more
331 * serious of the two error result codes is kept.
335 ldap_msg_merge( LDAP *ld, LDAPMessage *a, LDAPMessage *b )
337 LDAPMessage *end, *aprev, *aend, *bprev, *bend;
345 /* find the ends of the a and b chains */
347 for ( aend = a; aend->lm_chain != NULL; aend = aend->lm_chain )
350 for ( bend = b; bend->lm_chain != NULL; bend = bend->lm_chain )
354 if ( ldap_result2error( ld, aend, 0 ) != LDAP_SUCCESS ) {
355 /* remove result b */
356 ldap_msgfree( bend );
358 bprev->lm_chain = NULL;
363 aprev->lm_chain = NULL;
368 /* remove result a */
369 ldap_msgfree( aend );
371 aprev->lm_chain = NULL;
376 bprev->lm_chain = NULL;
381 if ( (a == NULL && b == NULL) || (a == NULL && bprev == NULL) ||
382 (b == NULL && aprev == NULL) )
386 bprev->lm_chain = end;
388 } else if ( b == NULL ) {
389 aprev->lm_chain = end;
392 bprev->lm_chain = end;
399 ldap_ufn_expand( LDAP *ld, cancelptype cancelproc, void *cancelparm,
400 char **dns, char *filter, int scope, char **attrs, int aonly,
403 LDAPMessage *tmpcand, *tmpres;
408 /* search for this component below the current candidates */
417 if (( msgid = ldap_search( ld, dn, scope, filter, attrs,
419 ldap_msgfree( tmpcand );
425 tv.tv_usec = 100000; /* 1/10 of a second */
428 *err = ldap_result( ld, msgid, 1, &tv, &tmpres );
429 if ( *err == 0 && cancelproc != NULL &&
430 (*cancelproc)( cancelparm ) != 0 ) {
431 ldap_abandon( ld, msgid );
432 *err = LDAP_USER_CANCELLED;
433 ld->ld_errno = LDAP_USER_CANCELLED;
435 } while ( *err == 0 );
437 if ( *err == LDAP_USER_CANCELLED || *err < 0 ||
438 ( *err = ldap_result2error( ld, tmpres, 0 )) == -1 ) {
439 ldap_msgfree( tmpcand );
443 tmpcand = ldap_msg_merge( ld, tmpcand, tmpres );
446 } while ( dns != NULL && dns[i] != NULL );
448 if ( ldap_count_entries( ld, tmpcand ) > 0 ) {
451 ldap_msgfree( tmpcand );
457 * ldap_ufn_setfilter - set the filter config file used in ufn searching
461 ldap_ufn_setfilter( LDAP *ld, char *fname )
463 if ( ld->ld_filtd != NULL )
464 ldap_getfilter_free( ld->ld_filtd );
466 return( ld->ld_filtd = ldap_init_getfilter( fname ) );
470 ldap_ufn_setprefix( LDAP *ld, char *prefix )
472 if ( ld->ld_ufnprefix != NULL )
473 free( ld->ld_ufnprefix );
475 ld->ld_ufnprefix = strdup( prefix );
479 ldap_ufn_timeout( void *tvparam )
483 tv = (struct timeval *)tvparam;
485 if ( tv->tv_sec != 0 ) {
486 tv->tv_usec = tv->tv_sec * 1000000; /* sec => micro sec */
489 tv->tv_usec -= 100000; /* 1/10 of a second */
491 return( tv->tv_usec <= 0 ? 1 : 0 );