]> git.sur5r.net Git - openldap/blob - libraries/libldap/ufn.c
rename ldap_pvt_init_utils() to ldap_int_utils_init() and provide
[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 "ldap-int.h"
23 #include "ldapconfig.h"
24
25 typedef int (*cancelptype) LDAP_P(( void *cancelparm ));
26
27 /* local functions */
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
37 /*
38  * ldap_ufn_search_ctx - do user friendly searching; provide cancel feature;
39  *                      specify ldapfilter.conf tags for each phase of search
40  *
41  *      ld              LDAP descriptor
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
54  *
55  * Example:
56  *      char            *attrs[] = { "mail", "title", 0 };
57  *      char            *ufncomp[] = { "howes", "umich", "us", 0 }
58  *      LDAPMessage     *res;
59  *      error = ldap_ufn_search_ctx( ld, ufncomp, 3, NULL, attrs, attrsonly,
60  *                      &res, acancelproc, along, "ufn first",
61  *                      "ufn intermediate", "ufn last" );
62  */
63
64 static int
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 )
68 {
69         char            *dn, *ftag = NULL;
70         char            **dns = NULL;
71         int             max, i, err, scope = 0, phase, tries;
72         LDAPFiltInfo    *fi;
73         LDAPMessage     *tmpcand;
74         LDAPMessage     *candidates;
75         static char     *objattrs[] = { "objectClass", NULL };
76
77         /* 
78          * look up ufn components from most to least significant.
79          * there are 3 phases.  
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
88          * the next component.
89          */
90
91         *res = NULL;
92         candidates = NULL;
93         phase = 1;
94         for ( ncomp--; ncomp != -1; ncomp-- ) {
95                 if ( *ufncomp[ncomp] == '"' ) {
96                         char    *quote;
97
98                         if ( (quote = strrchr( ufncomp[ncomp], '"' )) != NULL )
99                                 *quote = '\0';
100                         SAFEMEMCPY( ufncomp[ncomp], ufncomp[ncomp] + 1,
101                                     strlen( ufncomp[ncomp] + 1 ) + 1 );
102                 }
103                 if ( ncomp == 0 )
104                         phase = 3;
105
106                 switch ( phase ) {
107                 case 1:
108                         ftag = tag1;
109                         scope = LDAP_SCOPE_ONELEVEL;
110                         break;
111                 case 2:
112                         ftag = tag2;
113                         scope = LDAP_SCOPE_ONELEVEL;
114                         break;
115                 case 3:
116                         ftag = tag3;
117                         scope = LDAP_SCOPE_SUBTREE;
118                         break;
119                 }
120
121                 /*
122                  * construct an array of DN's to search below from the
123                  * list of candidates.
124                  */
125
126                 if ( candidates == NULL ) {
127                         if ( prefix != NULL ) {
128                                 if ( (dns = (char **) malloc( sizeof(char *)
129                                     * 2 )) == NULL ) {
130                                         return( ld->ld_errno = LDAP_NO_MEMORY );
131                                 }
132                                 dns[0] = strdup( prefix );
133                                 dns[1] = NULL;
134                         } else {
135                                 dns = NULL;
136                         }
137                 } else {
138                         i = 0, max = 0;
139                         for ( tmpcand = candidates; tmpcand != NULL &&
140                             tmpcand->lm_msgtype != LDAP_RES_SEARCH_RESULT;
141                             tmpcand = tmpcand->lm_chain )
142                         {
143                                 if ( (dn = ldap_get_dn( ld, tmpcand )) == NULL )
144                                         continue;
145
146                                 if ( dns == NULL ) {
147                                         if ( (dns = (char **) malloc(
148                                             sizeof(char *) * 8 )) == NULL ) {
149                                                 ld->ld_errno = LDAP_NO_MEMORY;
150                                                 return( LDAP_NO_MEMORY );
151                                         }
152                                         max = 8;
153                                 } else if ( i >= max ) {
154                                         if ( (dns = (char **) realloc( dns,
155                                             sizeof(char *) * 2 * max ))
156                                             == NULL )
157                                         {
158                                                 ld->ld_errno = LDAP_NO_MEMORY;
159                                                 return( LDAP_NO_MEMORY );
160                                         }
161                                         max *= 2;
162                                 }
163                                 dns[i++] = dn;
164                                 dns[i] = NULL;
165                         }
166                         ldap_msgfree( candidates );
167                         candidates = NULL;
168                 }
169                 tries = 0;
170         tryagain:
171                 tries++;
172                 for ( fi = ldap_getfirstfilter( ld->ld_filtd, ftag,
173                     ufncomp[ncomp] ); fi != NULL;
174                     fi = ldap_getnextfilter( ld->ld_filtd ) )
175                 {
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 )
180                         {
181                                 break;
182                         }
183
184                         if ( err == -1 || err == LDAP_USER_CANCELLED ) {
185                                 if ( dns != NULL ) {
186                                         ldap_value_free( dns );
187                                         dns = NULL;
188                                 }
189                                 return( err );
190                         }
191                 }
192
193                 if ( candidates == NULL ) {
194                         if ( tries < 2 && phase != 3 ) {
195                                 scope = LDAP_SCOPE_SUBTREE;
196                                 goto tryagain;
197                         } else {
198                                 if ( dns != NULL ) {
199                                         ldap_value_free( dns );
200                                         dns = NULL;
201                                 }
202                                 return( err );
203                         }
204                 }
205
206                 /* go on to the next component */
207                 if ( phase == 1 )
208                         phase++;
209                 if ( dns != NULL ) {
210                         ldap_value_free( dns );
211                         dns = NULL;
212                 }
213         }
214         *res = candidates;
215
216         return( err );
217 }
218
219 int
220 ldap_ufn_search_ct(
221         LDAP *ld, LDAP_CONST 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(
307         LDAP *ld, LDAP_CONST char *ufn, char **attrs, int attrsonly,
308         LDAPMessage **res, cancelptype cancelproc, void *cancelparm )
309 {
310         return( ldap_ufn_search_ct( ld, ufn, attrs, attrsonly, res, cancelproc,
311             cancelparm, "ufn first", "ufn intermediate", "ufn last" ) );
312 }
313
314 /*
315  * same as ldap_ufn_search_c without the cancel function
316  */
317 int
318 ldap_ufn_search_s(
319         LDAP *ld, LDAP_CONST 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 != 0 &&
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, LDAP_CONST 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, LDAP_CONST char *prefix )
475 {
476         if ( ld->ld_ufnprefix != NULL )
477                 free( ld->ld_ufnprefix );
478
479         ld->ld_ufnprefix = 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 }