]> git.sur5r.net Git - openldap/blob - libraries/libldap/search.c
Extern URI code to support literal IPv6 addresses
[openldap] / libraries / libldap / search.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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  *  search.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
26 static int ldap_is_attr_oid LDAP_P((
27         const char *attr ));
28
29 static int ldap_is_attr_desc LDAP_P((
30         const char *attr ));
31
32 static int hex2value LDAP_P((
33         int c ));
34
35 static char *find_right_paren LDAP_P((
36         char *s ));
37
38 static char *put_complex_filter LDAP_P((
39         BerElement *ber,
40         char *str,
41         ber_tag_t tag,
42         int not ));
43
44 static int put_filter LDAP_P((
45         BerElement *ber,
46         char *str ));
47
48 static int put_simple_filter LDAP_P((
49         BerElement *ber,
50         char *str ));
51
52 static int put_substring_filter LDAP_P((
53         BerElement *ber,
54         char *type,
55         char *str ));
56
57 static int put_filter_list LDAP_P((
58         BerElement *ber,
59         char *str ));
60
61 /*
62  * ldap_search_ext - initiate an ldap search operation.
63  *
64  * Parameters:
65  *
66  *      ld              LDAP descriptor
67  *      base            DN of the base object
68  *      scope           the search scope - one of LDAP_SCOPE_BASE,
69  *                          LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE
70  *      filter          a string containing the search filter
71  *                      (e.g., "(|(cn=bob)(sn=bob))")
72  *      attrs           list of attribute types to return for matches
73  *      attrsonly       1 => attributes only 0 => attributes and values
74  *
75  * Example:
76  *      char    *attrs[] = { "mail", "title", 0 };
77  *      ldap_search_ext( ld, "c=us,o=UM", LDAP_SCOPE_SUBTREE, "cn~=bob",
78  *          attrs, attrsonly, sctrls, ctrls, timeout, sizelimit,
79  *              &msgid );
80  */
81 int
82 ldap_search_ext(
83         LDAP *ld,
84         LDAP_CONST char *base,
85         int scope,
86         LDAP_CONST char *filter,
87         char **attrs,
88         int attrsonly,
89         LDAPControl **sctrls,
90         LDAPControl **cctrls,
91         struct timeval *timeout,
92         int sizelimit,
93         int *msgidp )
94 {
95         BerElement      *ber;
96         int timelimit;
97
98         Debug( LDAP_DEBUG_TRACE, "ldap_search_ext\n", 0, 0, 0 );
99
100         /*
101          * if timeout is provided, both tv_sec and tv_usec must
102          * be non-zero
103          */
104         if( timeout != NULL ) {
105                 if( timeout->tv_sec == 0 && timeout->tv_usec == 0 ) {
106                         return LDAP_PARAM_ERROR;
107                 }
108
109                 /* timelimit must be non-zero if timeout is provided */
110                 timelimit = timeout->tv_sec != 0 ? timeout->tv_sec : 1;
111
112         } else {
113                 /* no timeout, no timelimit */
114                 timelimit = -1;
115         }
116
117         ber = ldap_build_search_req( ld, base, scope, filter, attrs,
118             attrsonly, sctrls, cctrls, timelimit, sizelimit ); 
119
120         if ( ber == NULL ) {
121                 return ld->ld_errno;
122         }
123
124 #ifndef LDAP_NOCACHE
125         if ( ld->ld_cache != NULL ) {
126                 if ( ldap_check_cache( ld, LDAP_REQ_SEARCH, ber ) == 0 ) {
127                         ber_free( ber, 1 );
128                         ld->ld_errno = LDAP_SUCCESS;
129                         *msgidp = ld->ld_msgid;
130                         return ld->ld_errno;
131                 }
132                 ldap_add_request_to_cache( ld, LDAP_REQ_SEARCH, ber );
133         }
134 #endif /* LDAP_NOCACHE */
135
136         /* send the message */
137         *msgidp = ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber );
138
139         if( *msgidp < 0 )
140                 return ld->ld_errno;
141
142         return LDAP_SUCCESS;
143 }
144
145 int
146 ldap_search_ext_s(
147         LDAP *ld,
148         LDAP_CONST char *base,
149         int scope,
150         LDAP_CONST char *filter,
151         char **attrs,
152         int attrsonly,
153         LDAPControl **sctrls,
154         LDAPControl **cctrls,
155         struct timeval *timeout,
156         int sizelimit,
157         LDAPMessage **res )
158 {
159         int rc;
160         int     msgid;
161
162         rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
163                 sctrls, cctrls, timeout, sizelimit, &msgid );
164
165         if ( rc != LDAP_SUCCESS ) {
166                 return( rc );
167         }
168
169         rc = ldap_result( ld, msgid, 1, timeout, res );
170
171         if( rc <= 0 ) {
172                 /* error(-1) or timeout(0) */
173                 return( ld->ld_errno );
174         }
175
176         if( rc == LDAP_RES_SEARCH_REFERENCE) {
177                 return( ld->ld_errno );
178         }
179
180         return( ldap_result2error( ld, *res, 0 ) );
181 }
182
183 /*
184  * ldap_search - initiate an ldap search operation.
185  *
186  * Parameters:
187  *
188  *      ld              LDAP descriptor
189  *      base            DN of the base object
190  *      scope           the search scope - one of LDAP_SCOPE_BASE,
191  *                          LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE
192  *      filter          a string containing the search filter
193  *                      (e.g., "(|(cn=bob)(sn=bob))")
194  *      attrs           list of attribute types to return for matches
195  *      attrsonly       1 => attributes only 0 => attributes and values
196  *
197  * Example:
198  *      char    *attrs[] = { "mail", "title", 0 };
199  *      msgid = ldap_search( ld, "c=us@o=UM", LDAP_SCOPE_SUBTREE, "cn~=bob",
200  *          attrs, attrsonly );
201  */
202 int
203 ldap_search(
204         LDAP *ld, LDAP_CONST char *base, int scope, LDAP_CONST char *filter,
205         char **attrs, int attrsonly )
206 {
207         BerElement      *ber;
208
209         Debug( LDAP_DEBUG_TRACE, "ldap_search\n", 0, 0, 0 );
210
211         ber = ldap_build_search_req( ld, base, scope, filter, attrs,
212             attrsonly, NULL, NULL, -1, -1 ); 
213
214         if ( ber == NULL ) {
215                 return( -1 );
216         }
217
218 #ifndef LDAP_NOCACHE
219         if ( ld->ld_cache != NULL ) {
220                 if ( ldap_check_cache( ld, LDAP_REQ_SEARCH, ber ) == 0 ) {
221                         ber_free( ber, 1 );
222                         ld->ld_errno = LDAP_SUCCESS;
223                         return( ld->ld_msgid );
224                 }
225                 ldap_add_request_to_cache( ld, LDAP_REQ_SEARCH, ber );
226         }
227 #endif /* LDAP_NOCACHE */
228
229         /* send the message */
230         return ( ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber ));
231 }
232
233
234 BerElement *
235 ldap_build_search_req(
236         LDAP *ld,
237         LDAP_CONST char *base,
238         ber_int_t scope,
239         LDAP_CONST char *filter_in,
240         char **attrs,
241         ber_int_t attrsonly,
242         LDAPControl **sctrls,
243         LDAPControl **cctrls,
244         ber_int_t timelimit,
245         ber_int_t sizelimit )
246 {
247         BerElement      *ber;
248         int             err;
249         char    *filter;
250
251         /*
252          * Create the search request.  It looks like this:
253          *      SearchRequest := [APPLICATION 3] SEQUENCE {
254          *              baseObject      DistinguishedName,
255          *              scope           ENUMERATED {
256          *                      baseObject      (0),
257          *                      singleLevel     (1),
258          *                      wholeSubtree    (2)
259          *              },
260          *              derefAliases    ENUMERATED {
261          *                      neverDerefaliases       (0),
262          *                      derefInSearching        (1),
263          *                      derefFindingBaseObj     (2),
264          *                      alwaysDerefAliases      (3)
265          *              },
266          *              sizelimit       INTEGER (0 .. 65535),
267          *              timelimit       INTEGER (0 .. 65535),
268          *              attrsOnly       BOOLEAN,
269          *              filter          Filter,
270          *              attributes      SEQUENCE OF AttributeType
271          *      }
272          * wrapped in an ldap message.
273          */
274
275         /* create a message to send */
276         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
277                 return( NULL );
278         }
279
280         if ( base == NULL ) {
281                 /* no base provided, use session default base */
282                 base = ld->ld_options.ldo_defbase;
283
284                 if ( base == NULL ) {
285                         /* no session default base, use top */
286                         base = "";
287                 }
288         }
289
290 #ifdef LDAP_CONNECTIONLESS
291         if ( ld->ld_cldapnaddr > 0 ) {
292             err = ber_printf( ber, "{ist{seeiib", ++ld->ld_msgid,
293                         ld->ld_cldapdn, LDAP_REQ_SEARCH, base, scope, ld->ld_deref,
294                         (sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
295                         (timelimit < 0) ? ld->ld_timelimit : timelimit,
296                         attrsonly );
297         } else {
298 #endif /* LDAP_CONNECTIONLESS */
299                 err = ber_printf( ber, "{it{seeiib", ++ld->ld_msgid,
300                     LDAP_REQ_SEARCH, base, (ber_int_t) scope, ld->ld_deref,
301                         (sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
302                         (timelimit < 0) ? ld->ld_timelimit : timelimit,
303                     attrsonly );
304 #ifdef LDAP_CONNECTIONLESS
305         }
306 #endif /* LDAP_CONNECTIONLESS */
307
308         if ( err == -1 ) {
309                 ld->ld_errno = LDAP_ENCODING_ERROR;
310                 ber_free( ber, 1 );
311                 return( NULL );
312         }
313
314         if( filter_in != NULL ) {
315                 filter = LDAP_STRDUP( filter_in );
316         } else {
317                 filter = LDAP_STRDUP( "(objectclass=*)" );
318         }
319         err = put_filter( ber, filter );
320         LDAP_FREE( filter );
321
322         if ( err  == -1 ) {
323                 ld->ld_errno = LDAP_FILTER_ERROR;
324                 ber_free( ber, 1 );
325                 return( NULL );
326         }
327
328         if ( ber_printf( ber, /*{*/ "{v}}", attrs ) == -1 ) {
329                 ld->ld_errno = LDAP_ENCODING_ERROR;
330                 ber_free( ber, 1 );
331                 return( NULL );
332         }
333
334         /* Put Server Controls */
335         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
336                 ber_free( ber, 1 );
337                 return( NULL );
338         }
339
340         if ( ber_printf( ber, /*{*/ "}", attrs ) == -1 ) {
341                 ld->ld_errno = LDAP_ENCODING_ERROR;
342                 ber_free( ber, 1 );
343                 return( NULL );
344         }
345
346         return( ber );
347 }
348
349 static int ldap_is_attr_oid ( const char *attr )
350 {
351         int i, c, digit=0;
352
353         for( i = 0; (c = attr[i]) != 0; i++ ) {
354                 if( c >= '0' && c <= '9' ) {
355                         digit=1;
356
357                 } else if ( c != '.' ) {
358                         /* not digit nor '.' */
359                         return 0;
360
361                 } else if ( !digit ) {
362                         /* '.' but prev not digit */
363                         return 0;
364
365                 } else {
366                         /* '.' */
367                         digit = 0;
368                 }
369         }
370
371         return digit;
372 }
373
374 static int ldap_is_attr_desc ( const char *attr )
375 {
376         /* cheap attribute description check */
377         int i, c;
378
379         for( i = 0; (c = attr[i]) != 0; i++ ) {
380                 if (( c >= '0' && c <= '9' )
381                         || ( c >= 'A' && c <= 'Z' )
382                         || ( c >= 'a' && c <= 'z' )
383                         || ( c == '.' || c == '-' )
384                         || ( c == ';' )) continue;
385
386                 return 0;
387         }
388
389         return i > 0;
390 }
391
392 static char *
393 find_right_paren( char *s )
394 {
395         int     balance, escape;
396
397         balance = 1;
398         escape = 0;
399         while ( *s && balance ) {
400                 if ( escape == 0 ) {
401                         if ( *s == '(' )
402                                 balance++;
403                         else if ( *s == ')' )
404                                 balance--;
405                 }
406                 if ( *s == '\\' && ! escape )
407                         escape = 1;
408                 else
409                         escape = 0;
410                 if ( balance )
411                         s++;
412         }
413
414         return( *s ? s : NULL );
415 }
416
417 static int hex2value( int c )
418 {
419         if( c >= '0' && c <= '9' ) {
420                 return c - '0';
421         }
422
423         if( c >= 'A' && c <= 'F' ) {
424                 return c + (10 - (int) 'A');
425         }
426
427         if( c >= 'a' && c <= 'f' ) {
428                 return c + (10 - (int) 'a');
429         }
430
431         return -1;
432 }
433
434 char *
435 ldap_pvt_find_wildcard( const char *s )
436 {
437         for( ; *s != '\0' ; s++ ) {
438                 switch( *s ) {
439                 case '*':       /* found wildcard */
440                         return (char *) s;
441
442                 case '\\':
443                         s++; /* skip over escape */
444                         if ( *s == '\0' )
445                                 return NULL;    /* escape at end of string */
446                 }
447         }
448
449         return NULL;
450 }
451
452 /* unescape filter value */
453 /* support both LDAP v2 and v3 escapes */
454 /* output can include nul characters */
455 ber_slen_t
456 ldap_pvt_filter_value_unescape( char *fval )
457 {
458         ber_slen_t r, v;
459         int v1, v2;
460
461         for( r=v=0; fval[v] != '\0'; v++ ) {
462                 switch( fval[v] ) {
463                 case '\\':
464                         /* escape */
465                         v++;
466
467                         if ( fval[v] == '\0' ) {
468                                 /* escape at end of string */
469                                 return -1;
470
471                         }
472
473                         if (( v1 = hex2value( fval[v] )) >= 0 ) {
474                                 /* LDAPv3 escape */
475
476                                 if (( v2 = hex2value( fval[v+1] )) < 0 ) {
477                                         /* must be two digit code */
478                                         return -1;
479                                 }
480
481                                 fval[r++] = v1 * 16 + v2;
482                                 v++;
483
484                         } else {
485                                 /* LDAPv2 escape */
486                                 fval[r++] = fval[v];
487                         }
488
489                         break;
490
491                 default:
492                         fval[r++] = fval[v];
493                 }
494         }
495
496         fval[r] = '\0';
497         return r;
498 }
499
500 static char *
501 put_complex_filter( BerElement *ber, char *str, ber_tag_t tag, int not )
502 {
503         char    *next;
504
505         /*
506          * We have (x(filter)...) with str sitting on
507          * the x.  We have to find the paren matching
508          * the one before the x and put the intervening
509          * filters by calling put_filter_list().
510          */
511
512         /* put explicit tag */
513         if ( ber_printf( ber, "t{" /*}*/, tag ) == -1 )
514                 return( NULL );
515
516         str++;
517         if ( (next = find_right_paren( str )) == NULL )
518                 return( NULL );
519
520         *next = '\0';
521         if ( put_filter_list( ber, str ) == -1 )
522                 return( NULL );
523         *next++ = ')';
524
525         /* flush explicit tagged thang */
526         if ( ber_printf( ber, /*{*/ "}" ) == -1 )
527                 return( NULL );
528
529         return( next );
530 }
531
532 static int
533 put_filter( BerElement *ber, char *str )
534 {
535         char    *next;
536         int     parens, balance, escape;
537
538         /*
539          * A Filter looks like this:
540          *      Filter ::= CHOICE {
541          *              and             [0]     SET OF Filter,
542          *              or              [1]     SET OF Filter,
543          *              not             [2]     Filter,
544          *              equalityMatch   [3]     AttributeValueAssertion,
545          *              substrings      [4]     SubstringFilter,
546          *              greaterOrEqual  [5]     AttributeValueAssertion,
547          *              lessOrEqual     [6]     AttributeValueAssertion,
548          *              present         [7]     AttributeType,
549          *              approxMatch     [8]     AttributeValueAssertion,
550          *                              extensibleMatch [9]             MatchingRuleAssertion -- LDAPv3
551          *      }
552          *
553          *      SubstringFilter ::= SEQUENCE {
554          *              type               AttributeType,
555          *              SEQUENCE OF CHOICE {
556          *                      initial          [0] IA5String,
557          *                      any              [1] IA5String,
558          *                      final            [2] IA5String
559          *              }
560          *      }
561          *
562          *              MatchingRuleAssertion ::= SEQUENCE {    -- LDAPv3
563          *                      matchingRule    [1] MatchingRuleId OPTIONAL,
564          *                      type            [2] AttributeDescription OPTIONAL,
565          *                      matchValue      [3] AssertionValue,
566          *                      dnAttributes    [4] BOOLEAN DEFAULT FALSE }
567          *
568          * Note: tags in a choice are always explicit
569          */
570
571         Debug( LDAP_DEBUG_TRACE, "put_filter \"%s\"\n", str, 0, 0 );
572
573         parens = 0;
574         while ( *str ) {
575                 switch ( *str ) {
576                 case '(':
577                         str++;
578                         parens++;
579
580                         /* skip spaces */
581                         while( isspace( *str ) ) str++;
582
583                         switch ( *str ) {
584                         case '&':
585                                 Debug( LDAP_DEBUG_TRACE, "put_filter: AND\n",
586                                     0, 0, 0 );
587
588                                 if ( (str = put_complex_filter( ber, str,
589                                     LDAP_FILTER_AND, 0 )) == NULL )
590                                         return( -1 );
591
592                                 parens--;
593                                 break;
594
595                         case '|':
596                                 Debug( LDAP_DEBUG_TRACE, "put_filter: OR\n",
597                                     0, 0, 0 );
598
599                                 if ( (str = put_complex_filter( ber, str,
600                                     LDAP_FILTER_OR, 0 )) == NULL )
601                                         return( -1 );
602
603                                 parens--;
604                                 break;
605
606                         case '!':
607                                 Debug( LDAP_DEBUG_TRACE, "put_filter: NOT\n",
608                                     0, 0, 0 );
609
610                                 if ( (str = put_complex_filter( ber, str,
611                                     LDAP_FILTER_NOT, 1 )) == NULL )
612                                         return( -1 );
613
614                                 parens--;
615                                 break;
616
617                         default:
618                                 Debug( LDAP_DEBUG_TRACE, "put_filter: simple\n",
619                                     0, 0, 0 );
620
621                                 balance = 1;
622                                 escape = 0;
623                                 next = str;
624                                 while ( *next && balance ) {
625                                         if ( escape == 0 ) {
626                                                 if ( *next == '(' )
627                                                         balance++;
628                                                 else if ( *next == ')' )
629                                                         balance--;
630                                         }
631                                         if ( *next == '\\' && ! escape )
632                                                 escape = 1;
633                                         else
634                                                 escape = 0;
635                                         if ( balance )
636                                                 next++;
637                                 }
638                                 if ( balance != 0 )
639                                         return( -1 );
640
641                                 *next = '\0';
642                                 if ( put_simple_filter( ber, str ) == -1 ) {
643                                         return( -1 );
644                                 }
645                                 *next++ = ')';
646                                 str = next;
647                                 parens--;
648                                 break;
649                         }
650                         break;
651
652                 case ')':
653                         Debug( LDAP_DEBUG_TRACE, "put_filter: end\n", 0, 0,
654                             0 );
655                         if ( ber_printf( ber, /*[*/ "]" ) == -1 )
656                                 return( -1 );
657                         str++;
658                         parens--;
659                         break;
660
661                 case ' ':
662                         str++;
663                         break;
664
665                 default:        /* assume it's a simple type=value filter */
666                         Debug( LDAP_DEBUG_TRACE, "put_filter: default\n", 0, 0,
667                             0 );
668                         next = strchr( str, '\0' );
669                         if ( put_simple_filter( ber, str ) == -1 ) {
670                                 return( -1 );
671                         }
672                         str = next;
673                         break;
674                 }
675         }
676
677         return( parens ? -1 : 0 );
678 }
679
680 /*
681  * Put a list of filters like this "(filter1)(filter2)..."
682  */
683
684 static int
685 put_filter_list( BerElement *ber, char *str )
686 {
687         char    *next;
688         char    save;
689
690         Debug( LDAP_DEBUG_TRACE, "put_filter_list \"%s\"\n", str, 0, 0 );
691
692         while ( *str ) {
693                 while ( *str && isspace( (unsigned char) *str ) )
694                         str++;
695                 if ( *str == '\0' )
696                         break;
697
698                 if ( (next = find_right_paren( str + 1 )) == NULL )
699                         return( -1 );
700                 save = *++next;
701
702                 /* now we have "(filter)" with str pointing to it */
703                 *next = '\0';
704                 if ( put_filter( ber, str ) == -1 )
705                         return( -1 );
706                 *next = save;
707
708                 str = next;
709         }
710
711         return( 0 );
712 }
713
714 static int
715 put_simple_filter(
716         BerElement *ber,
717         char *str )
718 {
719         char            *s;
720         char            *value;
721         ber_tag_t       ftype;
722         int             rc = -1;
723
724         Debug( LDAP_DEBUG_TRACE, "put_simple_filter \"%s\"\n", str, 0, 0 );
725
726         str = LDAP_STRDUP( str );
727         if( str == NULL ) return -1;
728
729         if ( (s = strchr( str, '=' )) == NULL ) {
730                 goto done;
731         }
732
733         value = s + 1;
734         *s-- = '\0';
735
736         switch ( *s ) {
737         case '<':
738                 ftype = LDAP_FILTER_LE;
739                 *s = '\0';
740                 if(! ldap_is_attr_desc( str ) ) goto done;
741                 break;
742
743         case '>':
744                 ftype = LDAP_FILTER_GE;
745                 *s = '\0';
746                 if(! ldap_is_attr_desc( str ) ) goto done;
747                 break;
748
749         case '~':
750                 ftype = LDAP_FILTER_APPROX;
751                 *s = '\0';
752                 if(! ldap_is_attr_desc( str ) ) goto done;
753                 break;
754
755         case ':':
756                 /* RFC2254 extensible filters are off the form:
757                  *              type [:dn] [:rule] := value
758                  * or   [:dn]:rule := value             
759                  */
760                 ftype = LDAP_FILTER_EXT;
761                 *s = '\0';
762
763                 {
764                         char *dn = strchr( str, ':' );
765                         char *rule = NULL;
766
767                         if( dn == NULL ) {
768                                 if(! ldap_is_attr_desc( str ) ) goto done;
769                                 break;
770                         }
771
772                         *dn++ = '\0';
773                         rule = strchr( dn, ':' );
774
775                         if( rule == NULL ) {
776                                 /* one colon */
777                                 if ( strcmp(dn, "dn") == 0 ) {
778                                         /* must have attribute */
779                                         if( !ldap_is_attr_desc( str ) ) {
780                                                 goto done;
781                                         }
782
783                                         rule = "";
784
785                                 } else {
786                                         rule = dn;
787                                         dn = NULL;
788                                 }
789                                 
790                         } else {
791                                 /* two colons */
792                                 *rule++ = '\0';
793
794                                 if ( strcmp(dn, "dn") != 0 ) {
795                                         /* must have "dn" */
796                                         goto done;
797                                 }
798                         }
799
800                         if ( *str == '\0' && *rule == '\0' ) {
801                                 /* must have either type or rule */
802                                 goto done;
803                         }
804
805                         if ( *str != '\0' && !ldap_is_attr_desc( str ) ) {
806                                 goto done;
807                         }
808
809                         if ( *rule != '\0' && !ldap_is_attr_oid( rule ) ) {
810                                 goto done;
811                         }
812
813                         rc = ber_printf( ber, "t{" /*}*/, ftype );
814
815                         if( rc != -1 && *rule != '\0' ) {
816                                 rc = ber_printf( ber, "ts", LDAP_FILTER_EXT_OID, rule );
817                         }
818                         if( rc != -1 && *str != '\0' ) {
819                                 rc = ber_printf( ber, "ts", LDAP_FILTER_EXT_TYPE, str );
820                         }
821
822                         if( rc != -1 ) {
823                                 ber_slen_t len = ldap_pvt_filter_value_unescape( value );
824
825                                 if( len >= 0 ) {
826                                         rc = ber_printf( ber, "totb}",
827                                                 LDAP_FILTER_EXT_VALUE, value, len,
828                                                 LDAP_FILTER_EXT_DNATTRS, dn != NULL);
829                                 } else {
830                                         rc = -1;
831                                 }
832                         }
833                 }
834                 break;
835
836         default:
837                 if ( ldap_pvt_find_wildcard( value ) == NULL ) {
838                         ftype = LDAP_FILTER_EQUALITY;
839                 } else if ( strcmp( value, "*" ) == 0 ) {
840                         ftype = LDAP_FILTER_PRESENT;
841                 } else {
842                         rc = put_substring_filter( ber, str, value );
843                         goto done;
844                 }
845                 break;
846         }
847
848         if ( ftype == LDAP_FILTER_PRESENT ) {
849                 rc = ber_printf( ber, "ts", ftype, str );
850
851         } else {
852                 ber_slen_t len = ldap_pvt_filter_value_unescape( value );
853
854                 if( len >= 0 ) {
855                         rc = ber_printf( ber, "t{so}",
856                                 ftype, str, value, len );
857                 }
858         }
859
860         if( rc != -1 ) rc = 0;
861
862 done:
863         LDAP_FREE( str );
864         return rc;
865 }
866
867 static int
868 put_substring_filter( BerElement *ber, char *type, char *val )
869 {
870         char            *nextstar, gotstar = 0;
871         ber_tag_t       ftype = LDAP_FILTER_SUBSTRINGS;
872
873         Debug( LDAP_DEBUG_TRACE, "put_substring_filter \"%s=%s\"\n", type,
874             val, 0 );
875
876         if ( ber_printf( ber, "t{s{", ftype, type ) == -1 )
877                 return( -1 );
878
879         for( ; val != NULL; val=nextstar ) {
880                 if ( (nextstar = ldap_pvt_find_wildcard( val )) != NULL )
881                         *nextstar++ = '\0';
882
883                 if ( gotstar == 0 ) {
884                         ftype = LDAP_SUBSTRING_INITIAL;
885                 } else if ( nextstar == NULL ) {
886                         ftype = LDAP_SUBSTRING_FINAL;
887                 } else {
888                         ftype = LDAP_SUBSTRING_ANY;
889                 }
890
891                 if ( *val != '\0' ) {
892                         ber_slen_t len = ldap_pvt_filter_value_unescape( val );
893
894                         if ( len < 0  ) {
895                                 return -1;
896                         }
897
898                         if ( ber_printf( ber, "to", ftype, val, len ) == -1 ) {
899                                 return( -1 );
900                         }
901                 }
902
903                 gotstar = 1;
904         }
905
906         if ( ber_printf( ber, /* {{ */ "}}" ) == -1 )
907                 return( -1 );
908
909         return( 0 );
910 }
911
912 int
913 ldap_search_st(
914         LDAP *ld, LDAP_CONST char *base, int scope,
915         LDAP_CONST char *filter, char **attrs,
916         int attrsonly, struct timeval *timeout, LDAPMessage **res )
917 {
918         int     msgid;
919
920         if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
921             == -1 )
922                 return( ld->ld_errno );
923
924         if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 )
925                 return( ld->ld_errno );
926
927         if ( ld->ld_errno == LDAP_TIMEOUT ) {
928                 (void) ldap_abandon( ld, msgid );
929                 ld->ld_errno = LDAP_TIMEOUT;
930                 return( ld->ld_errno );
931         }
932
933         return( ldap_result2error( ld, *res, 0 ) );
934 }
935
936 int
937 ldap_search_s(
938         LDAP *ld,
939         LDAP_CONST char *base,
940         int scope,
941         LDAP_CONST char *filter,
942         char **attrs,
943         int attrsonly,
944         LDAPMessage **res )
945 {
946         int     msgid;
947
948         if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
949             == -1 )
950                 return( ld->ld_errno );
951
952         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, res ) == -1 )
953                 return( ld->ld_errno );
954
955         return( ldap_result2error( ld, *res, 0 ) );
956 }
957