2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright (c) 1990 Regents of the University of Michigan.
16 #include <ac/stdlib.h>
19 #include <ac/socket.h>
20 #include <ac/string.h>
25 static int ldap_is_attr_oid LDAP_P((
28 static int ldap_is_attr_desc LDAP_P((
31 static int hex2value LDAP_P((
34 static ber_slen_t filter_value_unescape LDAP_P((
37 static char *find_right_paren LDAP_P((
40 static char *find_wildcard LDAP_P((
43 static char *put_complex_filter LDAP_P((
49 static int put_filter LDAP_P((
53 static int put_simple_filter LDAP_P((
57 static int put_substring_filter LDAP_P((
62 static int put_filter_list LDAP_P((
67 * ldap_search_ext - initiate an ldap search operation.
72 * base DN of the base object
73 * scope the search scope - one of LDAP_SCOPE_BASE,
74 * LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE
75 * filter a string containing the search filter
76 * (e.g., "(|(cn=bob)(sn=bob))")
77 * attrs list of attribute types to return for matches
78 * attrsonly 1 => attributes only 0 => attributes and values
81 * char *attrs[] = { "mail", "title", 0 };
82 * ldap_search_ext( ld, "c=us,o=UM", LDAP_SCOPE_SUBTREE, "cn~=bob",
83 * attrs, attrsonly, sctrls, ctrls, timeout, sizelimit,
89 LDAP_CONST char *base,
91 LDAP_CONST char *filter,
96 struct timeval *timeout,
103 Debug( LDAP_DEBUG_TRACE, "ldap_search_ext\n", 0, 0, 0 );
106 * if timeout is provided, use only tv_sec as timelimit.
107 * otherwise, use default.
109 timelimit = (timeout != NULL)
113 ber = ldap_build_search_req( ld, base, scope, filter, attrs,
114 attrsonly, sctrls, cctrls, timelimit, sizelimit );
121 if ( ld->ld_cache != NULL ) {
122 if ( ldap_check_cache( ld, LDAP_REQ_SEARCH, ber ) == 0 ) {
124 ld->ld_errno = LDAP_SUCCESS;
125 *msgidp = ld->ld_msgid;
128 ldap_add_request_to_cache( ld, LDAP_REQ_SEARCH, ber );
130 #endif /* LDAP_NOCACHE */
132 /* send the message */
133 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber );
144 LDAP_CONST char *base,
146 LDAP_CONST char *filter,
149 LDAPControl **sctrls,
150 LDAPControl **cctrls,
151 struct timeval *timeout,
158 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
159 sctrls, cctrls, timeout, sizelimit, &msgid );
161 if ( rc != LDAP_SUCCESS ) {
165 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 )
166 return( ld->ld_errno );
168 return( ldap_result2error( ld, *res, 0 ) );
172 * ldap_search - initiate an ldap search operation.
177 * base DN of the base object
178 * scope the search scope - one of LDAP_SCOPE_BASE,
179 * LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE
180 * filter a string containing the search filter
181 * (e.g., "(|(cn=bob)(sn=bob))")
182 * attrs list of attribute types to return for matches
183 * attrsonly 1 => attributes only 0 => attributes and values
186 * char *attrs[] = { "mail", "title", 0 };
187 * msgid = ldap_search( ld, "c=us@o=UM", LDAP_SCOPE_SUBTREE, "cn~=bob",
188 * attrs, attrsonly );
192 LDAP *ld, LDAP_CONST char *base, int scope, LDAP_CONST char *filter,
193 char **attrs, int attrsonly )
197 Debug( LDAP_DEBUG_TRACE, "ldap_search\n", 0, 0, 0 );
199 ber = ldap_build_search_req( ld, base, scope, filter, attrs,
200 attrsonly, NULL, NULL, -1, -1 );
207 if ( ld->ld_cache != NULL ) {
208 if ( ldap_check_cache( ld, LDAP_REQ_SEARCH, ber ) == 0 ) {
210 ld->ld_errno = LDAP_SUCCESS;
211 return( ld->ld_msgid );
213 ldap_add_request_to_cache( ld, LDAP_REQ_SEARCH, ber );
215 #endif /* LDAP_NOCACHE */
217 /* send the message */
218 return ( ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber ));
223 ldap_build_search_req(
225 LDAP_CONST char *base,
227 LDAP_CONST char *filter_in,
230 LDAPControl **sctrls,
231 LDAPControl **cctrls,
233 ber_int_t sizelimit )
240 * Create the search request. It looks like this:
241 * SearchRequest := [APPLICATION 3] SEQUENCE {
242 * baseObject DistinguishedName,
248 * derefAliases ENUMERATED {
249 * neverDerefaliases (0),
250 * derefInSearching (1),
251 * derefFindingBaseObj (2),
252 * alwaysDerefAliases (3)
254 * sizelimit INTEGER (0 .. 65535),
255 * timelimit INTEGER (0 .. 65535),
258 * attributes SEQUENCE OF AttributeType
260 * wrapped in an ldap message.
263 /* create a message to send */
264 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
268 if ( base == NULL ) {
269 /* no base provided, use session default base */
270 base = ld->ld_options.ldo_defbase;
272 if ( base == NULL ) {
273 /* no session default base, use top */
278 #ifdef LDAP_CONNECTIONLESS
279 if ( ld->ld_cldapnaddr > 0 ) {
280 err = ber_printf( ber, "{ist{seeiib", ++ld->ld_msgid,
281 ld->ld_cldapdn, LDAP_REQ_SEARCH, base, scope, ld->ld_deref,
282 (sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
283 (timelimit < 0) ? ld->ld_timelimit : timelimit,
286 #endif /* LDAP_CONNECTIONLESS */
287 err = ber_printf( ber, "{it{seeiib", ++ld->ld_msgid,
288 LDAP_REQ_SEARCH, base, (ber_int_t) scope, ld->ld_deref,
289 (sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
290 (timelimit < 0) ? ld->ld_timelimit : timelimit,
292 #ifdef LDAP_CONNECTIONLESS
294 #endif /* LDAP_CONNECTIONLESS */
297 ld->ld_errno = LDAP_ENCODING_ERROR;
302 filter = LDAP_STRDUP( filter_in );
303 err = put_filter( ber, filter );
307 ld->ld_errno = LDAP_FILTER_ERROR;
312 if ( ber_printf( ber, /*{*/ "{v}}", attrs ) == -1 ) {
313 ld->ld_errno = LDAP_ENCODING_ERROR;
318 /* Put Server Controls */
319 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
324 if ( ber_printf( ber, /*{*/ "}", attrs ) == -1 ) {
325 ld->ld_errno = LDAP_ENCODING_ERROR;
333 static int ldap_is_attr_oid ( const char *attr )
337 for( i=0 ; c = attr[i] ; i++ ) {
338 if( c >= '0' && c <= '9' ) {
341 } else if ( c != '.' ) {
342 /* not digit nor '.' */
345 } else if ( !digit ) {
346 /* '.' but prev not digit */
359 static int ldap_is_attr_desc ( const char *attr )
361 /* cheap attribute description check */
364 for( i=0; c = attr[i]; i++ ) {
365 if (( c >= '0' && c <= '9' )
366 || ( c >= 'A' && c <= 'Z' )
367 || ( c >= 'a' && c <= 'z' )
368 || ( c == '.' || c == '-' )
369 || ( c == ';' )) continue;
378 find_right_paren( char *s )
384 while ( *s && balance ) {
388 else if ( *s == ')' )
391 if ( *s == '\\' && ! escape )
399 return( *s ? s : NULL );
402 static int hex2value( int c )
404 if( c >= '0' && c <= '9' ) {
408 if( c >= 'A' && c <= 'F' ) {
409 return c + (10 - (int) 'A');
412 if( c >= 'a' && c <= 'f' ) {
413 return c + (10 - (int) 'a');
420 find_wildcard( char *s )
422 for( ; *s != '\0' ; s++ ) {
424 case '*': /* found wildcard */
428 s++; /* skip over escape */
429 if( hex2value( s[0] ) >= 0 && hex2value( s[1] ) >= 0 ) {
430 /* skip over lead digit of two hex digit code */
439 /* unescape filter value */
440 /* support both LDAP v2 and v3 escapes */
441 /* output can include nul characters */
443 filter_value_unescape( char *fval )
448 for( r=v=0; fval[v] != '\0'; v++ ) {
454 if ( fval[v] == '\0' ) {
455 /* escape at end of string */
460 if (( v1 = hex2value( fval[v] )) >= 0 ) {
463 if (( v2 = hex2value( fval[v+1] )) < 0 ) {
464 /* must be two digit code */
468 fval[r++] = v1 * 16 + v2;
488 put_complex_filter( BerElement *ber, char *str, ber_tag_t tag, int not )
493 * We have (x(filter)...) with str sitting on
494 * the x. We have to find the paren matching
495 * the one before the x and put the intervening
496 * filters by calling put_filter_list().
499 /* put explicit tag */
500 if ( ber_printf( ber, "t{" /*}*/, tag ) == -1 )
504 if ( (next = find_right_paren( str )) == NULL )
508 if ( put_filter_list( ber, str ) == -1 )
512 /* flush explicit tagged thang */
513 if ( ber_printf( ber, /*{*/ "}" ) == -1 )
520 put_filter( BerElement *ber, char *str )
523 int parens, balance, escape;
526 * A Filter looks like this:
527 * Filter ::= CHOICE {
528 * and [0] SET OF Filter,
529 * or [1] SET OF Filter,
531 * equalityMatch [3] AttributeValueAssertion,
532 * substrings [4] SubstringFilter,
533 * greaterOrEqual [5] AttributeValueAssertion,
534 * lessOrEqual [6] AttributeValueAssertion,
535 * present [7] AttributeType,
536 * approxMatch [8] AttributeValueAssertion,
537 * extensibleMatch [9] MatchingRuleAssertion -- LDAPv3
540 * SubstringFilter ::= SEQUENCE {
541 * type AttributeType,
542 * SEQUENCE OF CHOICE {
543 * initial [0] IA5String,
545 * final [2] IA5String
549 * MatchingRuleAssertion ::= SEQUENCE { -- LDAPv3
550 * matchingRule [1] MatchingRuleId OPTIONAL,
551 * type [2] AttributeDescription OPTIONAL,
552 * matchValue [3] AssertionValue,
553 * dnAttributes [4] BOOLEAN DEFAULT FALSE }
555 * Note: tags in a choice are always explicit
558 Debug( LDAP_DEBUG_TRACE, "put_filter \"%s\"\n", str, 0, 0 );
568 Debug( LDAP_DEBUG_TRACE, "put_filter: AND\n",
571 if ( (str = put_complex_filter( ber, str,
572 LDAP_FILTER_AND, 0 )) == NULL )
579 Debug( LDAP_DEBUG_TRACE, "put_filter: OR\n",
582 if ( (str = put_complex_filter( ber, str,
583 LDAP_FILTER_OR, 0 )) == NULL )
590 Debug( LDAP_DEBUG_TRACE, "put_filter: NOT\n",
593 if ( (str = put_complex_filter( ber, str,
594 LDAP_FILTER_NOT, 1 )) == NULL )
601 Debug( LDAP_DEBUG_TRACE, "put_filter: simple\n",
607 while ( *next && balance ) {
611 else if ( *next == ')' )
614 if ( *next == '\\' && ! escape )
625 if ( put_simple_filter( ber, str ) == -1 ) {
636 Debug( LDAP_DEBUG_TRACE, "put_filter: end\n", 0, 0,
638 if ( ber_printf( ber, /*[*/ "]" ) == -1 )
648 default: /* assume it's a simple type=value filter */
649 Debug( LDAP_DEBUG_TRACE, "put_filter: default\n", 0, 0,
651 next = strchr( str, '\0' );
652 if ( put_simple_filter( ber, str ) == -1 ) {
660 return( parens ? -1 : 0 );
664 * Put a list of filters like this "(filter1)(filter2)..."
668 put_filter_list( BerElement *ber, char *str )
673 Debug( LDAP_DEBUG_TRACE, "put_filter_list \"%s\"\n", str, 0, 0 );
676 while ( *str && isspace( (unsigned char) *str ) )
681 if ( (next = find_right_paren( str + 1 )) == NULL )
685 /* now we have "(filter)" with str pointing to it */
687 if ( put_filter( ber, str ) == -1 )
707 Debug( LDAP_DEBUG_TRACE, "put_simple_filter \"%s\"\n", str, 0, 0 );
709 str = LDAP_STRDUP( str );
710 if( str == NULL ) return -1;
712 if ( (s = strchr( str, '=' )) == NULL ) {
721 ftype = LDAP_FILTER_LE;
723 if(! ldap_is_attr_desc( str ) ) goto done;
727 ftype = LDAP_FILTER_GE;
729 if(! ldap_is_attr_desc( str ) ) goto done;
733 ftype = LDAP_FILTER_APPROX;
735 if(! ldap_is_attr_desc( str ) ) goto done;
739 /* RFC2254 extensible filters are off the form:
740 * type [:dn] [:rule] := value
741 * or [:dn]:rule := value
743 ftype = LDAP_FILTER_EXT;
747 char *dn = strchr( str, ':' );
751 if(! ldap_is_attr_desc( str ) ) goto done;
756 rule = strchr( dn, ':' );
760 if ( strcmp(dn, "dn") == 0 ) {
761 /* must have attribute */
762 if( !ldap_is_attr_desc( str ) ) {
777 if ( strcmp(dn, "dn") != 0 ) {
783 if ( *str == '\0' && *rule == '\0' ) {
784 /* must have either type or rule */
788 if ( *str != '\0' && !ldap_is_attr_desc( str ) ) {
792 if ( *rule != '\0' && !ldap_is_attr_oid( rule ) ) {
796 rc = ber_printf( ber, "t{" /*}*/, ftype );
798 if( rc != -1 && *rule != '\0' ) {
799 rc = ber_printf( ber, "ts", LDAP_FILTER_EXT_OID, rule );
801 if( rc != -1 && *str != '\0' ) {
802 rc = ber_printf( ber, "ts", LDAP_FILTER_EXT_TYPE, str );
806 ber_slen_t len = filter_value_unescape( value );
809 rc = ber_printf( ber, "totb}",
810 LDAP_FILTER_EXT_VALUE, value, len,
811 LDAP_FILTER_EXT_DNATTRS, dn != NULL);
820 if ( find_wildcard( value ) == NULL ) {
821 ftype = LDAP_FILTER_EQUALITY;
822 } else if ( strcmp( value, "*" ) == 0 ) {
823 ftype = LDAP_FILTER_PRESENT;
825 rc = put_substring_filter( ber, str, value );
831 if ( ftype == LDAP_FILTER_PRESENT ) {
832 rc = ber_printf( ber, "ts", ftype, str );
835 ber_slen_t len = filter_value_unescape( value );
838 rc = ber_printf( ber, "t{so}",
839 ftype, str, value, len );
843 if( rc != -1 ) rc = 0;
851 put_substring_filter( BerElement *ber, char *type, char *val )
853 char *nextstar, gotstar = 0;
854 ber_tag_t ftype = LDAP_FILTER_SUBSTRINGS;
856 Debug( LDAP_DEBUG_TRACE, "put_substring_filter \"%s=%s\"\n", type,
859 if ( ber_printf( ber, "t{s{", ftype, type ) == -1 )
862 for( ; val != NULL; val=nextstar ) {
863 if ( (nextstar = find_wildcard( val )) != NULL )
866 if ( gotstar == 0 ) {
867 ftype = LDAP_SUBSTRING_INITIAL;
868 } else if ( nextstar == NULL ) {
869 ftype = LDAP_SUBSTRING_FINAL;
871 ftype = LDAP_SUBSTRING_ANY;
874 if ( *val != '\0' ) {
875 ber_slen_t len = filter_value_unescape( val );
881 if ( ber_printf( ber, "to", ftype, val, len ) == -1 ) {
889 if ( ber_printf( ber, /* {{ */ "}}" ) == -1 )
897 LDAP *ld, LDAP_CONST char *base, int scope,
898 LDAP_CONST char *filter, char **attrs,
899 int attrsonly, struct timeval *timeout, LDAPMessage **res )
903 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
905 return( ld->ld_errno );
907 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 )
908 return( ld->ld_errno );
910 if ( ld->ld_errno == LDAP_TIMEOUT ) {
911 (void) ldap_abandon( ld, msgid );
912 ld->ld_errno = LDAP_TIMEOUT;
913 return( ld->ld_errno );
916 return( ldap_result2error( ld, *res, 0 ) );
922 LDAP_CONST char *base,
924 LDAP_CONST char *filter,
931 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
933 return( ld->ld_errno );
935 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, res ) == -1 )
936 return( ld->ld_errno );
938 return( ldap_result2error( ld, *res, 0 ) );