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>
23 #include "ldapconfig.h"
25 typedef int (*cancelptype) LDAP_P(( void *cancelparm ));
28 static int ldap_ufn_search_ctx LDAP_P(( LDAP *ld, char **ufncomp, int ncomp,
29 char *prefix, char **attrs, int attrsonly, LDAPMessage **res,
30 cancelptype cancelproc, void *cancelparm, char *tag1, char *tag2,
32 static LDAPMessage *ldap_msg_merge LDAP_P(( LDAP *ld, LDAPMessage *a, LDAPMessage *b ));
33 static LDAPMessage *ldap_ufn_expand LDAP_P(( LDAP *ld, cancelptype cancelproc,
34 void *cancelparm, char **dns, char *filter, int scope,
35 char **attrs, int aonly, int *err ));
38 * ldap_ufn_search_ctx - do user friendly searching; provide cancel feature;
39 * specify ldapfilter.conf tags for each phase of search
42 * ufncomp the exploded user friendly name to look for
43 * ncomp number of elements in ufncomp
44 * prefix where to start searching
45 * attrs list of attribute types to return for matches
46 * attrsonly 1 => attributes only 0 => attributes and values
47 * res will contain the result of the search
48 * cancelproc routine that returns non-zero if operation should be
49 * cancelled. This can be a null function pointer. If
50 * it is not 0, the routine will be called periodically.
51 * cancelparm void * that is passed to cancelproc
52 * tag[123] the ldapfilter.conf tag that will be used in phases
53 * 1, 2, and 3 of the search, respectively
56 * char *attrs[] = { "mail", "title", 0 };
57 * char *ufncomp[] = { "howes", "umich", "us", 0 }
59 * error = ldap_ufn_search_ctx( ld, ufncomp, 3, NULL, attrs, attrsonly,
60 * &res, acancelproc, along, "ufn first",
61 * "ufn intermediate", "ufn last" );
65 ldap_ufn_search_ctx( LDAP *ld, char **ufncomp, int ncomp, char *prefix,
66 char **attrs, int attrsonly, LDAPMessage **res, cancelptype cancelproc,
67 void *cancelparm, char *tag1, char *tag2, char *tag3 )
69 char *dn, *ftag = NULL;
71 int max, i, err, scope = 0, phase, tries;
74 LDAPMessage *candidates;
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 SAFEMEMCPY( ufncomp[ncomp], ufncomp[ncomp] + 1,
101 strlen( ufncomp[ncomp] + 1 ) + 1 );
109 scope = LDAP_SCOPE_ONELEVEL;
113 scope = LDAP_SCOPE_ONELEVEL;
117 scope = LDAP_SCOPE_SUBTREE;
122 * construct an array of DN's to search below from the
123 * list of candidates.
126 if ( candidates == NULL ) {
127 if ( prefix != NULL ) {
128 if ( (dns = (char **) malloc( sizeof(char *)
130 return( ld->ld_errno = LDAP_NO_MEMORY );
132 dns[0] = strdup( prefix );
139 for ( tmpcand = candidates; tmpcand != NULL &&
140 tmpcand->lm_msgtype != LDAP_RES_SEARCH_RESULT;
141 tmpcand = tmpcand->lm_chain )
143 if ( (dn = ldap_get_dn( ld, tmpcand )) == NULL )
147 if ( (dns = (char **) malloc(
148 sizeof(char *) * 8 )) == NULL ) {
149 ld->ld_errno = LDAP_NO_MEMORY;
150 return( LDAP_NO_MEMORY );
153 } else if ( i >= max ) {
154 if ( (dns = (char **) realloc( dns,
155 sizeof(char *) * 2 * max ))
158 ld->ld_errno = LDAP_NO_MEMORY;
159 return( LDAP_NO_MEMORY );
166 ldap_msgfree( candidates );
172 for ( fi = ldap_getfirstfilter( ld->ld_filtd, ftag,
173 ufncomp[ncomp] ); fi != NULL;
174 fi = ldap_getnextfilter( ld->ld_filtd ) )
176 if ( (candidates = ldap_ufn_expand( ld, cancelproc,
177 cancelparm, dns, fi->lfi_filter, scope,
178 phase == 3 ? attrs : objattrs,
179 phase == 3 ? attrsonly : 1, &err )) != NULL )
184 if ( err == -1 || err == LDAP_USER_CANCELLED ) {
186 ldap_value_free( dns );
193 if ( candidates == NULL ) {
194 if ( tries < 2 && phase != 3 ) {
195 scope = LDAP_SCOPE_SUBTREE;
199 ldap_value_free( dns );
206 /* go on to the next component */
210 ldap_value_free( dns );
220 ldap_ufn_search_ct( LDAP *ld, char *ufn, char **attrs, int attrsonly,
221 LDAPMessage **res, cancelptype cancelproc, void *cancelparm,
222 char *tag1, char *tag2, char *tag3 )
224 char **ufncomp, **prefixcomp;
226 int ncomp, pcomp, i, err = 0;
228 /* initialize the getfilter stuff if it's not already */
229 if ( ld->ld_filtd == NULL && ldap_ufn_setfilter( ld, FILTERFILE )
231 return( ld->ld_errno = LDAP_LOCAL_ERROR );
234 /* call ldap_explode_dn() to break the ufn into its components */
235 if ( (ufncomp = ldap_explode_dn( ufn, 0 )) == NULL )
236 return( ld->ld_errno = LDAP_LOCAL_ERROR );
237 for ( ncomp = 0; ufncomp[ncomp] != NULL; ncomp++ )
240 /* more than two components => try it fully qualified first */
241 if ( ncomp > 2 || ld->ld_ufnprefix == NULL ) {
242 err = ldap_ufn_search_ctx( ld, ufncomp, ncomp, NULL, attrs,
243 attrsonly, res, cancelproc, cancelparm, tag1, tag2, tag3 );
245 if ( ldap_count_entries( ld, *res ) > 0 ) {
246 ldap_value_free( ufncomp );
249 ldap_msgfree( *res );
254 if ( ld->ld_ufnprefix == NULL ) {
255 ldap_value_free( ufncomp );
259 /* if that failed, or < 2 components, use the prefix */
260 if ( (prefixcomp = ldap_explode_dn( ld->ld_ufnprefix, 0 )) == NULL ) {
261 ldap_value_free( ufncomp );
262 return( ld->ld_errno = LDAP_LOCAL_ERROR );
264 for ( pcomp = 0; prefixcomp[pcomp] != NULL; pcomp++ )
266 if ( (pbuf = (char *) malloc( strlen( ld->ld_ufnprefix ) + 1 ))
268 ldap_value_free( ufncomp );
269 ldap_value_free( prefixcomp );
270 return( ld->ld_errno = LDAP_NO_MEMORY );
273 for ( i = 0; i < pcomp; i++ ) {
277 for ( j = i; j < pcomp; j++ ) {
278 strcat( pbuf, prefixcomp[j] );
282 err = ldap_ufn_search_ctx( ld, ufncomp, ncomp, pbuf, attrs,
283 attrsonly, res, cancelproc, cancelparm, tag1, tag2, tag3 );
285 if ( ldap_count_entries( ld, *res ) > 0 ) {
288 ldap_msgfree( *res );
293 ldap_value_free( ufncomp );
294 ldap_value_free( prefixcomp );
301 * same as ldap_ufn_search_ct, except without the ability to specify
302 * ldapfilter.conf tags.
305 ldap_ufn_search_c( LDAP *ld, char *ufn, char **attrs, int attrsonly,
306 LDAPMessage **res, cancelptype cancelproc, void *cancelparm )
308 return( ldap_ufn_search_ct( ld, ufn, attrs, attrsonly, res, cancelproc,
309 cancelparm, "ufn first", "ufn intermediate", "ufn last" ) );
313 * same as ldap_ufn_search_c without the cancel function
316 ldap_ufn_search_s( LDAP *ld, char *ufn, char **attrs, int attrsonly,
321 tv.tv_sec = ld->ld_timelimit;
323 return( ldap_ufn_search_ct( ld, ufn, attrs, attrsonly, res,
324 ld->ld_timelimit ? ldap_ufn_timeout : NULL,
325 ld->ld_timelimit ? (void *) &tv : NULL,
326 "ufn first", "ufn intermediate", "ufn last" ) );
331 * ldap_msg_merge - merge two ldap search result chains. the more
332 * serious of the two error result codes is kept.
336 ldap_msg_merge( LDAP *ld, LDAPMessage *a, LDAPMessage *b )
338 LDAPMessage *end, *aprev, *aend, *bprev, *bend;
346 /* find the ends of the a and b chains */
348 for ( aend = a; aend->lm_chain != NULL; aend = aend->lm_chain )
351 for ( bend = b; bend->lm_chain != NULL; bend = bend->lm_chain )
355 if ( ldap_result2error( ld, aend, 0 ) != LDAP_SUCCESS ) {
356 /* remove result b */
357 ldap_msgfree( bend );
359 bprev->lm_chain = NULL;
364 aprev->lm_chain = NULL;
369 /* remove result a */
370 ldap_msgfree( aend );
372 aprev->lm_chain = NULL;
377 bprev->lm_chain = NULL;
382 if ( (a == NULL && b == NULL) || (a == NULL && bprev == NULL) ||
383 (b == NULL && aprev == NULL) )
387 bprev->lm_chain = end;
389 } else if ( b == NULL ) {
390 aprev->lm_chain = end;
393 bprev->lm_chain = end;
400 ldap_ufn_expand( LDAP *ld, cancelptype cancelproc, void *cancelparm,
401 char **dns, char *filter, int scope, char **attrs, int aonly,
404 LDAPMessage *tmpcand, *tmpres;
409 /* search for this component below the current candidates */
418 if (( msgid = ldap_search( ld, dn, scope, filter, attrs,
420 ldap_msgfree( tmpcand );
426 tv.tv_usec = 100000; /* 1/10 of a second */
429 *err = ldap_result( ld, msgid, 1, &tv, &tmpres );
430 if ( *err == 0 && cancelproc != 0 &&
431 (*cancelproc)( cancelparm ) != 0 ) {
432 ldap_abandon( ld, msgid );
433 *err = LDAP_USER_CANCELLED;
434 ld->ld_errno = LDAP_USER_CANCELLED;
436 } while ( *err == 0 );
438 if ( *err == LDAP_USER_CANCELLED || *err < 0 ||
439 ( *err = ldap_result2error( ld, tmpres, 0 )) == -1 ) {
440 ldap_msgfree( tmpcand );
444 tmpcand = ldap_msg_merge( ld, tmpcand, tmpres );
447 } while ( dns != NULL && dns[i] != NULL );
449 if ( ldap_count_entries( ld, tmpcand ) > 0 ) {
452 ldap_msgfree( tmpcand );
458 * ldap_ufn_setfilter - set the filter config file used in ufn searching
462 ldap_ufn_setfilter( LDAP *ld, char *fname )
464 if ( ld->ld_filtd != NULL )
465 ldap_getfilter_free( ld->ld_filtd );
467 return( ld->ld_filtd = ldap_init_getfilter( fname ) );
471 ldap_ufn_setprefix( LDAP *ld, char *prefix )
473 if ( ld->ld_ufnprefix != NULL )
474 free( ld->ld_ufnprefix );
476 ld->ld_ufnprefix = strdup( prefix );
480 ldap_ufn_timeout( void *tvparam )
484 tv = (struct timeval *)tvparam;
486 if ( tv->tv_sec != 0 ) {
487 tv->tv_usec = tv->tv_sec * 1000000; /* sec => micro sec */
490 tv->tv_usec -= 100000; /* 1/10 of a second */
492 return( tv->tv_usec <= 0 ? 1 : 0 );