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