]> git.sur5r.net Git - openldap/blob - libraries/libldap/ufn.c
f87af4d87a710fb1a55990e6b1ce8e28970d190d
[openldap] / libraries / libldap / ufn.c
1 /*
2  *  Copyright (c) 1990 Regents of the University of Michigan.
3  *  All rights reserved.
4  *
5  *  ufn.c
6  */
7
8 #include "portable.h"
9
10 #ifndef lint 
11 static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
12 #endif
13
14 #include <stdio.h>
15 #include <ac/string.h>
16 #include <ctype.h>
17 #include <stdlib.h>
18
19 #include <ac/socket.h>
20
21 #include "lber.h"
22 #include "ldap.h"
23
24 #include "ldapconfig.h"
25
26 typedef int (*cancelptype) LDAP_P(( void *cancelparm ));
27
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,
31         char *tag3 ));
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 ));
36 LDAPFiltDesc *ldap_ufn_setfilter LDAP_P(( LDAP *ld, char *fname ));
37
38 /*
39  * ldap_ufn_search_ctx - do user friendly searching; provide cancel feature;
40  *                      specify ldapfilter.conf tags for each phase of search
41  *
42  *      ld              LDAP descriptor
43  *      ufncomp         the exploded user friendly name to look for
44  *      ncomp           number of elements in ufncomp
45  *      prefix          where to start searching
46  *      attrs           list of attribute types to return for matches
47  *      attrsonly       1 => attributes only 0 => attributes and values
48  *      res             will contain the result of the search
49  *      cancelproc      routine that returns non-zero if operation should be
50  *                      cancelled.  This can be NULL.  If it is non-NULL, the
51  *                      routine will be called periodically.
52  *      cancelparm      void * that is passed to cancelproc
53  *      tag[123]        the ldapfilter.conf tag that will be used in phases
54  *                      1, 2, and 3 of the search, respectively
55  *
56  * Example:
57  *      char            *attrs[] = { "mail", "title", 0 };
58  *      char            *ufncomp[] = { "howes", "umich", "us", 0 }
59  *      LDAPMessage     *res;
60  *      error = ldap_ufn_search_ctx( ld, ufncomp, 3, NULL, attrs, attrsonly,
61  *                      &res, acancelproc, along, "ufn first",
62  *                      "ufn intermediate", "ufn last" );
63  */
64
65 static int
66 ldap_ufn_search_ctx( LDAP *ld, char **ufncomp, int ncomp, char *prefix,
67         char **attrs, int attrsonly, LDAPMessage **res, cancelptype cancelproc,
68         void *cancelparm, char *tag1, char *tag2, char *tag3 )
69 {
70         char            *dn, *ftag = NULL;
71         char            **dns = NULL;
72         int             max, i, err, scope = 0, phase, tries;
73         LDAPFiltInfo    *fi;
74         LDAPMessage     *tmpcand;
75         LDAPMessage     *candidates;
76         LDAPMessage     *ldap_msg_merge(), *ldap_ufn_expand();
77         static char     *objattrs[] = { "objectClass", NULL };
78
79         /* 
80          * look up ufn components from most to least significant.
81          * there are 3 phases.  
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
90          * the next component.
91          */
92
93         *res = NULL;
94         candidates = NULL;
95         phase = 1;
96         for ( ncomp--; ncomp != -1; ncomp-- ) {
97                 if ( *ufncomp[ncomp] == '"' ) {
98                         char    *quote;
99
100                         if ( (quote = strrchr( ufncomp[ncomp], '"' )) != NULL )
101                                 *quote = '\0';
102                         strcpy( ufncomp[ncomp], ufncomp[ncomp] + 1 );
103                 }
104                 if ( ncomp == 0 )
105                         phase = 3;
106
107                 switch ( phase ) {
108                 case 1:
109                         ftag = tag1;
110                         scope = LDAP_SCOPE_ONELEVEL;
111                         break;
112                 case 2:
113                         ftag = tag2;
114                         scope = LDAP_SCOPE_ONELEVEL;
115                         break;
116                 case 3:
117                         ftag = tag3;
118                         scope = LDAP_SCOPE_SUBTREE;
119                         break;
120                 }
121
122                 /*
123                  * construct an array of DN's to search below from the
124                  * list of candidates.
125                  */
126
127                 if ( candidates == NULL ) {
128                         if ( prefix != NULL ) {
129                                 if ( (dns = (char **) malloc( sizeof(char *)
130                                     * 2 )) == NULL ) {
131                                         return( ld->ld_errno = LDAP_NO_MEMORY );
132                                 }
133                                 dns[0] = strdup( prefix );
134                                 dns[1] = NULL;
135                         } else {
136                                 dns = NULL;
137                         }
138                 } else {
139                         i = 0, max = 0;
140                         for ( tmpcand = candidates; tmpcand != NULL &&
141                             tmpcand->lm_msgtype != LDAP_RES_SEARCH_RESULT;
142                             tmpcand = tmpcand->lm_chain )
143                         {
144                                 if ( (dn = ldap_get_dn( ld, tmpcand )) == NULL )
145                                         continue;
146
147                                 if ( dns == NULL ) {
148                                         if ( (dns = (char **) malloc(
149                                             sizeof(char *) * 8 )) == NULL ) {
150                                                 ld->ld_errno = LDAP_NO_MEMORY;
151                                                 return( LDAP_NO_MEMORY );
152                                         }
153                                         max = 8;
154                                 } else if ( i >= max ) {
155                                         if ( (dns = (char **) realloc( dns,
156                                             sizeof(char *) * 2 * max ))
157                                             == NULL )
158                                         {
159                                                 ld->ld_errno = LDAP_NO_MEMORY;
160                                                 return( LDAP_NO_MEMORY );
161                                         }
162                                         max *= 2;
163                                 }
164                                 dns[i++] = dn;
165                                 dns[i] = NULL;
166                         }
167                         ldap_msgfree( candidates );
168                         candidates = NULL;
169                 }
170                 tries = 0;
171         tryagain:
172                 tries++;
173                 for ( fi = ldap_getfirstfilter( ld->ld_filtd, ftag,
174                     ufncomp[ncomp] ); fi != NULL;
175                     fi = ldap_getnextfilter( ld->ld_filtd ) )
176                 {
177                         if ( (candidates = ldap_ufn_expand( ld, cancelproc,
178                             cancelparm, dns, fi->lfi_filter, scope,
179                             phase == 3 ? attrs : objattrs,
180                             phase == 3 ? attrsonly : 1, &err )) != NULL )
181                         {
182                                 break;
183                         }
184
185                         if ( err == -1 || err == LDAP_USER_CANCELLED ) {
186                                 if ( dns != NULL ) {
187                                         ldap_value_free( dns );
188                                         dns = NULL;
189                                 }
190                                 return( err );
191                         }
192                 }
193
194                 if ( candidates == NULL ) {
195                         if ( tries < 2 && phase != 3 ) {
196                                 scope = LDAP_SCOPE_SUBTREE;
197                                 goto tryagain;
198                         } else {
199                                 if ( dns != NULL ) {
200                                         ldap_value_free( dns );
201                                         dns = NULL;
202                                 }
203                                 return( err );
204                         }
205                 }
206
207                 /* go on to the next component */
208                 if ( phase == 1 )
209                         phase++;
210                 if ( dns != NULL ) {
211                         ldap_value_free( dns );
212                         dns = NULL;
213                 }
214         }
215         *res = candidates;
216
217         return( err );
218 }
219
220 int
221 ldap_ufn_search_ct( LDAP *ld, char *ufn, char **attrs, int attrsonly,
222         LDAPMessage **res, cancelptype cancelproc, void *cancelparm,
223         char *tag1, char *tag2, char *tag3 )
224 {
225         char    **ufncomp, **prefixcomp;
226         char    *pbuf;
227         int     ncomp, pcomp, i, err = 0;
228
229         /* initialize the getfilter stuff if it's not already */
230         if ( ld->ld_filtd == NULL && ldap_ufn_setfilter( ld, FILTERFILE )
231             == NULL ) {
232                 return( ld->ld_errno = LDAP_LOCAL_ERROR );
233         }
234
235         /* call ldap_explode_dn() to break the ufn into its components */
236         if ( (ufncomp = ldap_explode_dn( ufn, 0 )) == NULL )
237                 return( ld->ld_errno = LDAP_LOCAL_ERROR );
238         for ( ncomp = 0; ufncomp[ncomp] != NULL; ncomp++ )
239                 ;       /* NULL */
240
241         /* more than two components => try it fully qualified first */
242         if ( ncomp > 2 || ld->ld_ufnprefix == NULL ) {
243                 err = ldap_ufn_search_ctx( ld, ufncomp, ncomp, NULL, attrs,
244                     attrsonly, res, cancelproc, cancelparm, tag1, tag2, tag3 );
245
246                 if ( ldap_count_entries( ld, *res ) > 0 ) {
247                         ldap_value_free( ufncomp );
248                         return( err );
249                 } else {
250                         ldap_msgfree( *res );
251                         *res = NULL;
252                 }
253         }
254
255         if ( ld->ld_ufnprefix == NULL ) {
256                 ldap_value_free( ufncomp );
257                 return( err );
258         }
259
260         /* if that failed, or < 2 components, use the prefix */
261         if ( (prefixcomp = ldap_explode_dn( ld->ld_ufnprefix, 0 )) == NULL ) {
262                 ldap_value_free( ufncomp );
263                 return( ld->ld_errno = LDAP_LOCAL_ERROR );
264         }
265         for ( pcomp = 0; prefixcomp[pcomp] != NULL; pcomp++ )
266                 ;       /* NULL */
267         if ( (pbuf = (char *) malloc( strlen( ld->ld_ufnprefix ) + 1 ))
268             == NULL ) { 
269                 ldap_value_free( ufncomp );
270                 ldap_value_free( prefixcomp );
271                 return( ld->ld_errno = LDAP_NO_MEMORY );
272         }
273
274         for ( i = 0; i < pcomp; i++ ) {
275                 int     j;
276
277                 *pbuf = '\0';
278                 for ( j = i; j < pcomp; j++ ) {
279                         strcat( pbuf, prefixcomp[j] );
280                         if ( j + 1 < pcomp )
281                                 strcat( pbuf, "," );
282                 }
283                 err = ldap_ufn_search_ctx( ld, ufncomp, ncomp, pbuf, attrs,
284                     attrsonly, res, cancelproc, cancelparm, tag1, tag2, tag3 );
285
286                 if ( ldap_count_entries( ld, *res ) > 0 ) {
287                         break;
288                 } else {
289                         ldap_msgfree( *res );
290                         *res = NULL;
291                 }
292         }
293
294         ldap_value_free( ufncomp );
295         ldap_value_free( prefixcomp );
296         free( pbuf );
297
298         return( err );
299 }
300
301 /*
302  * same as ldap_ufn_search_ct, except without the ability to specify
303  * ldapfilter.conf tags.
304  */
305 int
306 ldap_ufn_search_c( LDAP *ld, char *ufn, char **attrs, int attrsonly,
307         LDAPMessage **res, cancelptype cancelproc, void *cancelparm )
308 {
309         return( ldap_ufn_search_ct( ld, ufn, attrs, attrsonly, res, cancelproc,
310             cancelparm, "ufn first", "ufn intermediate", "ufn last" ) );
311 }
312
313 /*
314  * same as ldap_ufn_search_c without the cancel function
315  */
316 int
317 ldap_ufn_search_s( LDAP *ld, char *ufn, char **attrs, int attrsonly,
318         LDAPMessage **res )
319 {
320         struct timeval  tv;
321
322         tv.tv_sec = ld->ld_timelimit;
323
324         return( ldap_ufn_search_ct( ld, ufn, attrs, attrsonly, res,
325                 ld->ld_timelimit ? ldap_ufn_timeout : NULL,
326                 ld->ld_timelimit ? (void *) &tv : NULL,
327                 "ufn first", "ufn intermediate", "ufn last" ) );
328 }
329
330
331 /*
332  * ldap_msg_merge - merge two ldap search result chains.  the more
333  * serious of the two error result codes is kept.
334  */
335
336 static LDAPMessage *
337 ldap_msg_merge( LDAP *ld, LDAPMessage *a, LDAPMessage *b )
338 {
339         LDAPMessage     *end, *aprev, *aend, *bprev, *bend;
340
341         if ( a == NULL )
342                 return( b );
343
344         if ( b == NULL )
345                 return( a );
346
347         /* find the ends of the a and b chains */
348         aprev = NULL;
349         for ( aend = a; aend->lm_chain != NULL; aend = aend->lm_chain )
350                 aprev = aend;
351         bprev = NULL;
352         for ( bend = b; bend->lm_chain != NULL; bend = bend->lm_chain )
353                 bprev = bend;
354
355         /* keep result a */
356         if ( ldap_result2error( ld, aend, 0 ) != LDAP_SUCCESS ) {
357                 /* remove result b */
358                 ldap_msgfree( bend );
359                 if ( bprev != NULL )
360                         bprev->lm_chain = NULL;
361                 else
362                         b = NULL;
363                 end = aend;
364                 if ( aprev != NULL )
365                         aprev->lm_chain = NULL;
366                 else
367                         a = NULL;
368         /* keep result b */
369         } else {
370                 /* remove result a */
371                 ldap_msgfree( aend );
372                 if ( aprev != NULL )
373                         aprev->lm_chain = NULL;
374                 else
375                         a = NULL;
376                 end = bend;
377                 if ( bprev != NULL )
378                         bprev->lm_chain = NULL;
379                 else
380                         b = NULL;
381         }
382
383         if ( (a == NULL && b == NULL) || (a == NULL && bprev == NULL) ||
384             (b == NULL && aprev == NULL) )
385                 return( end );
386
387         if ( a == NULL ) {
388                 bprev->lm_chain = end;
389                 return( b );
390         } else if ( b == NULL ) {
391                 aprev->lm_chain = end;
392                 return( a );
393         } else {
394                 bprev->lm_chain = end;
395                 aprev->lm_chain = b;
396                 return( a );
397         }
398 }
399
400 static LDAPMessage *
401 ldap_ufn_expand( LDAP *ld, cancelptype cancelproc, void *cancelparm,
402         char **dns, char *filter, int scope, char **attrs, int aonly,
403         int *err )
404 {
405         LDAPMessage     *tmpcand, *tmpres;
406         char            *dn;
407         int             i, msgid;
408         struct timeval  tv;
409
410         /* search for this component below the current candidates */
411         tmpcand = NULL;
412         i = 0;
413         do {
414                 if ( dns != NULL )
415                         dn = dns[i];
416                 else
417                         dn = "";
418
419                 if (( msgid = ldap_search( ld, dn, scope, filter, attrs,
420                     aonly )) == -1 ) {
421                         ldap_msgfree( tmpcand );
422                         *err = ld->ld_errno;
423                         return( NULL );
424                 }
425
426                 tv.tv_sec = 0;
427                 tv.tv_usec = 100000;    /* 1/10 of a second */
428
429                 do {
430                         *err = ldap_result( ld, msgid, 1, &tv, &tmpres );
431                         if ( *err == 0 && cancelproc != NULL &&
432                             (*cancelproc)( cancelparm ) != 0 ) {
433                                 ldap_abandon( ld, msgid );
434                                 *err = LDAP_USER_CANCELLED;
435                                 ld->ld_errno = LDAP_USER_CANCELLED;
436                         }
437                 } while ( *err == 0 );
438
439                 if ( *err == LDAP_USER_CANCELLED || *err < 0 ||
440                     ( *err = ldap_result2error( ld, tmpres, 0 )) == -1 ) {
441                         ldap_msgfree( tmpcand );
442                         return( NULL );
443                 }
444                 
445                 tmpcand = ldap_msg_merge( ld, tmpcand, tmpres );
446
447                 i++;
448         } while ( dns != NULL && dns[i] != NULL );
449
450         if ( ldap_count_entries( ld, tmpcand ) > 0 ) {
451                 return( tmpcand );
452         } else {
453                 ldap_msgfree( tmpcand );
454                 return( NULL );
455         }
456 }
457
458 /*
459  * ldap_ufn_setfilter - set the filter config file used in ufn searching
460  */
461
462 LDAPFiltDesc *
463 ldap_ufn_setfilter( LDAP *ld, char *fname )
464 {
465         if ( ld->ld_filtd != NULL )
466                 ldap_getfilter_free( ld->ld_filtd );
467
468         return( ld->ld_filtd = ldap_init_getfilter( fname ) );
469 }
470
471 void
472 ldap_ufn_setprefix( LDAP *ld, char *prefix )
473 {
474         if ( ld->ld_ufnprefix != NULL )
475                 free( ld->ld_ufnprefix );
476
477         ld->ld_ufnprefix = strdup( prefix );
478 }
479
480 int
481 ldap_ufn_timeout( void *tvparam )
482 {
483         struct timeval  *tv;
484
485         tv = (struct timeval *)tvparam;
486
487         if ( tv->tv_sec != 0 ) {
488                 tv->tv_usec = tv->tv_sec * 1000000;     /* sec => micro sec */
489                 tv->tv_sec = 0;
490         }
491         tv->tv_usec -= 100000;  /* 1/10 of a second */
492
493         return( tv->tv_usec <= 0 ? 1 : 0 );
494 }