]> git.sur5r.net Git - openldap/blob - libraries/libldap/ufn.c
Add OpenLDAP RCSid to *.[ch] in clients, libraries, and servers.
[openldap] / libraries / libldap / ufn.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1990 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  ufn.c
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16
17 #include <ac/stdlib.h>
18
19 #include <ac/ctype.h>
20 #include <ac/socket.h>
21 #include <ac/string.h>
22 #include <ac/time.h>
23
24 #include "ldap-int.h"
25 #include "ldap_defaults.h"
26
27 typedef int (*cancelptype) LDAP_P(( void *cancelparm ));
28
29 /* local functions */
30 static int ldap_ufn_search_ctx LDAP_P(( LDAP *ld, char **ufncomp, int ncomp, 
31         char *prefix, char **attrs, int attrsonly, LDAPMessage **res, 
32         cancelptype cancelproc, void *cancelparm, char *tag1, char *tag2,
33         char *tag3 ));
34 static LDAPMessage *ldap_msg_merge LDAP_P(( LDAP *ld, LDAPMessage *a, LDAPMessage *b ));
35 static LDAPMessage *ldap_ufn_expand LDAP_P(( LDAP *ld, cancelptype cancelproc,
36         void *cancelparm, char **dns, char *filter, int scope,
37         char **attrs, int aonly, int *err ));
38
39 /*
40  * ldap_ufn_search_ctx - do user friendly searching; provide cancel feature;
41  *                      specify ldapfilter.conf tags for each phase of search
42  *
43  *      ld              LDAP descriptor
44  *      ufncomp         the exploded user friendly name to look for
45  *      ncomp           number of elements in ufncomp
46  *      prefix          where to start searching
47  *      attrs           list of attribute types to return for matches
48  *      attrsonly       1 => attributes only 0 => attributes and values
49  *      res             will contain the result of the search
50  *      cancelproc      routine that returns non-zero if operation should be
51  *                      cancelled.  This can be a null function pointer.  If
52  *                      it is not 0, the routine will be called periodically.
53  *      cancelparm      void * that is passed to cancelproc
54  *      tag[123]        the ldapfilter.conf tag that will be used in phases
55  *                      1, 2, and 3 of the search, respectively
56  *
57  * Example:
58  *      char            *attrs[] = { "mail", "title", 0 };
59  *      char            *ufncomp[] = { "howes", "umich", "us", 0 }
60  *      LDAPMessage     *res;
61  *      error = ldap_ufn_search_ctx( ld, ufncomp, 3, NULL, attrs, attrsonly,
62  *                      &res, acancelproc, along, "ufn first",
63  *                      "ufn intermediate", "ufn last" );
64  */
65
66 static int
67 ldap_ufn_search_ctx( LDAP *ld, char **ufncomp, int ncomp, char *prefix,
68         char **attrs, int attrsonly, LDAPMessage **res, cancelptype cancelproc,
69         void *cancelparm, char *tag1, char *tag2, char *tag3 )
70 {
71         char            *dn, *ftag = NULL;
72         char            **dns = NULL;
73         int             max, i, err, scope = 0, phase, tries;
74         LDAPFiltInfo    *fi;
75         LDAPMessage     *tmpcand;
76         LDAPMessage     *candidates;
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                         SAFEMEMCPY( ufncomp[ncomp], ufncomp[ncomp] + 1,
103                                     strlen( ufncomp[ncomp] + 1 ) + 1 );
104                 }
105                 if ( ncomp == 0 )
106                         phase = 3;
107
108                 switch ( phase ) {
109                 case 1:
110                         ftag = tag1;
111                         scope = LDAP_SCOPE_ONELEVEL;
112                         break;
113                 case 2:
114                         ftag = tag2;
115                         scope = LDAP_SCOPE_ONELEVEL;
116                         break;
117                 case 3:
118                         ftag = tag3;
119                         scope = LDAP_SCOPE_SUBTREE;
120                         break;
121                 }
122
123                 /*
124                  * construct an array of DN's to search below from the
125                  * list of candidates.
126                  */
127
128                 if ( candidates == NULL ) {
129                         if ( prefix != NULL ) {
130                                 if ( (dns = (char **) LDAP_MALLOC( sizeof(char *)
131                                     * 2 )) == NULL ) {
132                                         return( ld->ld_errno = LDAP_NO_MEMORY );
133                                 }
134                                 dns[0] = LDAP_STRDUP( prefix );
135                                 dns[1] = NULL;
136                         } else {
137                                 dns = NULL;
138                         }
139                 } else {
140                         i = 0, max = 0;
141                         for ( tmpcand = candidates; tmpcand != NULL &&
142                             tmpcand->lm_msgtype != LDAP_RES_SEARCH_RESULT;
143                             tmpcand = tmpcand->lm_chain )
144                         {
145                                 if ( (dn = ldap_get_dn( ld, tmpcand )) == NULL )
146                                         continue;
147
148                                 if ( dns == NULL ) {
149                                         if ( (dns = (char **) LDAP_MALLOC(
150                                             sizeof(char *) * 8 )) == NULL ) {
151                                                 ld->ld_errno = LDAP_NO_MEMORY;
152                                                 return( LDAP_NO_MEMORY );
153                                         }
154                                         max = 8;
155                                 } else if ( i >= max ) {
156                                         if ( (dns = (char **) LDAP_REALLOC( dns,
157                                             sizeof(char *) * 2 * max ))
158                                             == NULL )
159                                         {
160                                                 ld->ld_errno = LDAP_NO_MEMORY;
161                                                 return( LDAP_NO_MEMORY );
162                                         }
163                                         max *= 2;
164                                 }
165                                 dns[i++] = dn;
166                                 dns[i] = NULL;
167                         }
168                         ldap_msgfree( candidates );
169                         candidates = NULL;
170                 }
171                 tries = 0;
172         tryagain:
173                 tries++;
174                 for ( fi = ldap_getfirstfilter( ld->ld_filtd, ftag,
175                     ufncomp[ncomp] ); fi != NULL;
176                     fi = ldap_getnextfilter( ld->ld_filtd ) )
177                 {
178                         if ( (candidates = ldap_ufn_expand( ld, cancelproc,
179                             cancelparm, dns, fi->lfi_filter, scope,
180                             phase == 3 ? attrs : objattrs,
181                             phase == 3 ? attrsonly : 1, &err )) != NULL )
182                         {
183                                 break;
184                         }
185
186                         if ( err == -1 || err == LDAP_USER_CANCELLED ) {
187                                 if ( dns != NULL ) {
188                                         ldap_value_free( dns );
189                                         dns = NULL;
190                                 }
191                                 return( err );
192                         }
193                 }
194
195                 if ( candidates == NULL ) {
196                         if ( tries < 2 && phase != 3 ) {
197                                 scope = LDAP_SCOPE_SUBTREE;
198                                 goto tryagain;
199                         } else {
200                                 if ( dns != NULL ) {
201                                         ldap_value_free( dns );
202                                         dns = NULL;
203                                 }
204                                 return( err );
205                         }
206                 }
207
208                 /* go on to the next component */
209                 if ( phase == 1 )
210                         phase++;
211                 if ( dns != NULL ) {
212                         ldap_value_free( dns );
213                         dns = NULL;
214                 }
215         }
216         *res = candidates;
217
218         return( err );
219 }
220
221 int
222 ldap_ufn_search_ct(
223         LDAP *ld, LDAP_CONST 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 *) LDAP_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         LDAP_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(
309         LDAP *ld, LDAP_CONST char *ufn, char **attrs, int attrsonly,
310         LDAPMessage **res, cancelptype cancelproc, void *cancelparm )
311 {
312         return( ldap_ufn_search_ct( ld, ufn, attrs, attrsonly, res, cancelproc,
313             cancelparm, "ufn first", "ufn intermediate", "ufn last" ) );
314 }
315
316 /*
317  * same as ldap_ufn_search_c without the cancel function
318  */
319 int
320 ldap_ufn_search_s(
321         LDAP *ld, LDAP_CONST char *ufn, char **attrs, int attrsonly,
322         LDAPMessage **res )
323 {
324         struct timeval  tv;
325
326         tv.tv_sec = ld->ld_timelimit;
327
328         return( ldap_ufn_search_ct( ld, ufn, attrs, attrsonly, res,
329                 ld->ld_timelimit ? ldap_ufn_timeout : NULL,
330                 ld->ld_timelimit ? (void *) &tv : NULL,
331                 "ufn first", "ufn intermediate", "ufn last" ) );
332 }
333
334
335 /*
336  * ldap_msg_merge - merge two ldap search result chains.  the more
337  * serious of the two error result codes is kept.
338  */
339
340 static LDAPMessage *
341 ldap_msg_merge( LDAP *ld, LDAPMessage *a, LDAPMessage *b )
342 {
343         LDAPMessage     *end, *aprev, *aend, *bprev, *bend;
344
345         if ( a == NULL )
346                 return( b );
347
348         if ( b == NULL )
349                 return( a );
350
351         /* find the ends of the a and b chains */
352         aprev = NULL;
353         for ( aend = a; aend->lm_chain != NULL; aend = aend->lm_chain )
354                 aprev = aend;
355         bprev = NULL;
356         for ( bend = b; bend->lm_chain != NULL; bend = bend->lm_chain )
357                 bprev = bend;
358
359         /* keep result a */
360         if ( ldap_result2error( ld, aend, 0 ) != LDAP_SUCCESS ) {
361                 /* remove result b */
362                 ldap_msgfree( bend );
363                 if ( bprev != NULL )
364                         bprev->lm_chain = NULL;
365                 else
366                         b = NULL;
367                 end = aend;
368                 if ( aprev != NULL )
369                         aprev->lm_chain = NULL;
370                 else
371                         a = NULL;
372         /* keep result b */
373         } else {
374                 /* remove result a */
375                 ldap_msgfree( aend );
376                 if ( aprev != NULL )
377                         aprev->lm_chain = NULL;
378                 else
379                         a = NULL;
380                 end = bend;
381                 if ( bprev != NULL )
382                         bprev->lm_chain = NULL;
383                 else
384                         b = NULL;
385         }
386
387         if ( (a == NULL && b == NULL) || (a == NULL && bprev == NULL) ||
388             (b == NULL && aprev == NULL) )
389                 return( end );
390
391         if ( a == NULL ) {
392                 bprev->lm_chain = end;
393                 return( b );
394         } else if ( b == NULL ) {
395                 aprev->lm_chain = end;
396                 return( a );
397         } else {
398                 bprev->lm_chain = end;
399                 aprev->lm_chain = b;
400                 return( a );
401         }
402 }
403
404 static LDAPMessage *
405 ldap_ufn_expand( LDAP *ld, cancelptype cancelproc, void *cancelparm,
406         char **dns, char *filter, int scope, char **attrs, int aonly,
407         int *err )
408 {
409         LDAPMessage     *tmpcand, *tmpres;
410         char            *dn;
411         int             i, msgid;
412         struct timeval  tv;
413
414         /* search for this component below the current candidates */
415         tmpcand = NULL;
416         i = 0;
417         do {
418                 if ( dns != NULL )
419                         dn = dns[i];
420                 else
421                         dn = "";
422
423                 if (( msgid = ldap_search( ld, dn, scope, filter, attrs,
424                     aonly )) == -1 ) {
425                         ldap_msgfree( tmpcand );
426                         *err = ld->ld_errno;
427                         return( NULL );
428                 }
429
430                 tv.tv_sec = 0;
431                 tv.tv_usec = 100000;    /* 1/10 of a second */
432
433                 do {
434                         *err = ldap_result( ld, msgid, 1, &tv, &tmpres );
435                         if ( *err == 0 && cancelproc != 0 &&
436                             (*cancelproc)( cancelparm ) != 0 ) {
437                                 ldap_abandon( ld, msgid );
438                                 *err = LDAP_USER_CANCELLED;
439                                 ld->ld_errno = LDAP_USER_CANCELLED;
440                         }
441                 } while ( *err == 0 );
442
443                 if ( *err == LDAP_USER_CANCELLED || *err < 0 ||
444                     ( *err = ldap_result2error( ld, tmpres, 0 )) == -1 ) {
445                         ldap_msgfree( tmpcand );
446                         return( NULL );
447                 }
448                 
449                 tmpcand = ldap_msg_merge( ld, tmpcand, tmpres );
450
451                 i++;
452         } while ( dns != NULL && dns[i] != NULL );
453
454         if ( ldap_count_entries( ld, tmpcand ) > 0 ) {
455                 return( tmpcand );
456         } else {
457                 ldap_msgfree( tmpcand );
458                 return( NULL );
459         }
460 }
461
462 /*
463  * ldap_ufn_setfilter - set the filter config file used in ufn searching
464  */
465
466 LDAPFiltDesc *
467 ldap_ufn_setfilter( LDAP *ld, LDAP_CONST char *fname )
468 {
469         if ( ld->ld_filtd != NULL )
470                 ldap_getfilter_free( ld->ld_filtd );
471
472         return( ld->ld_filtd = ldap_init_getfilter( fname ) );
473 }
474
475 void
476 ldap_ufn_setprefix( LDAP *ld, LDAP_CONST char *prefix )
477 {
478         if ( ld->ld_ufnprefix != NULL )
479                 LDAP_FREE( ld->ld_ufnprefix );
480
481         ld->ld_ufnprefix = LDAP_STRDUP( prefix );
482 }
483
484 int
485 ldap_ufn_timeout( void *tvparam )
486 {
487         struct timeval  *tv;
488
489         tv = (struct timeval *)tvparam;
490
491         if ( tv->tv_sec != 0 ) {
492                 tv->tv_usec = tv->tv_sec * 1000000;     /* sec => micro sec */
493                 tv->tv_sec = 0;
494         }
495         tv->tv_usec -= 100000;  /* 1/10 of a second */
496
497         return( tv->tv_usec <= 0 ? 1 : 0 );
498 }