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 */
430 return NULL; /* escape at end of string */
431 if( hex2value( s[0] ) >= 0 && hex2value( s[1] ) >= 0 ) {
432 /* skip over lead digit of two hex digit code */
441 /* unescape filter value */
442 /* support both LDAP v2 and v3 escapes */
443 /* output can include nul characters */
445 filter_value_unescape( char *fval )
450 for( r=v=0; fval[v] != '\0'; v++ ) {
456 if ( fval[v] == '\0' ) {
457 /* escape at end of string */
462 if (( v1 = hex2value( fval[v] )) >= 0 ) {
465 if (( v2 = hex2value( fval[v+1] )) < 0 ) {
466 /* must be two digit code */
470 fval[r++] = v1 * 16 + v2;
490 put_complex_filter( BerElement *ber, char *str, ber_tag_t tag, int not )
495 * We have (x(filter)...) with str sitting on
496 * the x. We have to find the paren matching
497 * the one before the x and put the intervening
498 * filters by calling put_filter_list().
501 /* put explicit tag */
502 if ( ber_printf( ber, "t{" /*}*/, tag ) == -1 )
506 if ( (next = find_right_paren( str )) == NULL )
510 if ( put_filter_list( ber, str ) == -1 )
514 /* flush explicit tagged thang */
515 if ( ber_printf( ber, /*{*/ "}" ) == -1 )
522 put_filter( BerElement *ber, char *str )
525 int parens, balance, escape;
528 * A Filter looks like this:
529 * Filter ::= CHOICE {
530 * and [0] SET OF Filter,
531 * or [1] SET OF Filter,
533 * equalityMatch [3] AttributeValueAssertion,
534 * substrings [4] SubstringFilter,
535 * greaterOrEqual [5] AttributeValueAssertion,
536 * lessOrEqual [6] AttributeValueAssertion,
537 * present [7] AttributeType,
538 * approxMatch [8] AttributeValueAssertion,
539 * extensibleMatch [9] MatchingRuleAssertion -- LDAPv3
542 * SubstringFilter ::= SEQUENCE {
543 * type AttributeType,
544 * SEQUENCE OF CHOICE {
545 * initial [0] IA5String,
547 * final [2] IA5String
551 * MatchingRuleAssertion ::= SEQUENCE { -- LDAPv3
552 * matchingRule [1] MatchingRuleId OPTIONAL,
553 * type [2] AttributeDescription OPTIONAL,
554 * matchValue [3] AssertionValue,
555 * dnAttributes [4] BOOLEAN DEFAULT FALSE }
557 * Note: tags in a choice are always explicit
560 Debug( LDAP_DEBUG_TRACE, "put_filter \"%s\"\n", str, 0, 0 );
570 Debug( LDAP_DEBUG_TRACE, "put_filter: AND\n",
573 if ( (str = put_complex_filter( ber, str,
574 LDAP_FILTER_AND, 0 )) == NULL )
581 Debug( LDAP_DEBUG_TRACE, "put_filter: OR\n",
584 if ( (str = put_complex_filter( ber, str,
585 LDAP_FILTER_OR, 0 )) == NULL )
592 Debug( LDAP_DEBUG_TRACE, "put_filter: NOT\n",
595 if ( (str = put_complex_filter( ber, str,
596 LDAP_FILTER_NOT, 1 )) == NULL )
603 Debug( LDAP_DEBUG_TRACE, "put_filter: simple\n",
609 while ( *next && balance ) {
613 else if ( *next == ')' )
616 if ( *next == '\\' && ! escape )
627 if ( put_simple_filter( ber, str ) == -1 ) {
638 Debug( LDAP_DEBUG_TRACE, "put_filter: end\n", 0, 0,
640 if ( ber_printf( ber, /*[*/ "]" ) == -1 )
650 default: /* assume it's a simple type=value filter */
651 Debug( LDAP_DEBUG_TRACE, "put_filter: default\n", 0, 0,
653 next = strchr( str, '\0' );
654 if ( put_simple_filter( ber, str ) == -1 ) {
662 return( parens ? -1 : 0 );
666 * Put a list of filters like this "(filter1)(filter2)..."
670 put_filter_list( BerElement *ber, char *str )
675 Debug( LDAP_DEBUG_TRACE, "put_filter_list \"%s\"\n", str, 0, 0 );
678 while ( *str && isspace( (unsigned char) *str ) )
683 if ( (next = find_right_paren( str + 1 )) == NULL )
687 /* now we have "(filter)" with str pointing to it */
689 if ( put_filter( ber, str ) == -1 )
709 Debug( LDAP_DEBUG_TRACE, "put_simple_filter \"%s\"\n", str, 0, 0 );
711 str = LDAP_STRDUP( str );
712 if( str == NULL ) return -1;
714 if ( (s = strchr( str, '=' )) == NULL ) {
723 ftype = LDAP_FILTER_LE;
725 if(! ldap_is_attr_desc( str ) ) goto done;
729 ftype = LDAP_FILTER_GE;
731 if(! ldap_is_attr_desc( str ) ) goto done;
735 ftype = LDAP_FILTER_APPROX;
737 if(! ldap_is_attr_desc( str ) ) goto done;
741 /* RFC2254 extensible filters are off the form:
742 * type [:dn] [:rule] := value
743 * or [:dn]:rule := value
745 ftype = LDAP_FILTER_EXT;
749 char *dn = strchr( str, ':' );
753 if(! ldap_is_attr_desc( str ) ) goto done;
758 rule = strchr( dn, ':' );
762 if ( strcmp(dn, "dn") == 0 ) {
763 /* must have attribute */
764 if( !ldap_is_attr_desc( str ) ) {
779 if ( strcmp(dn, "dn") != 0 ) {
785 if ( *str == '\0' && *rule == '\0' ) {
786 /* must have either type or rule */
790 if ( *str != '\0' && !ldap_is_attr_desc( str ) ) {
794 if ( *rule != '\0' && !ldap_is_attr_oid( rule ) ) {
798 rc = ber_printf( ber, "t{" /*}*/, ftype );
800 if( rc != -1 && *rule != '\0' ) {
801 rc = ber_printf( ber, "ts", LDAP_FILTER_EXT_OID, rule );
803 if( rc != -1 && *str != '\0' ) {
804 rc = ber_printf( ber, "ts", LDAP_FILTER_EXT_TYPE, str );
808 ber_slen_t len = filter_value_unescape( value );
811 rc = ber_printf( ber, "totb}",
812 LDAP_FILTER_EXT_VALUE, value, len,
813 LDAP_FILTER_EXT_DNATTRS, dn != NULL);
822 if ( find_wildcard( value ) == NULL ) {
823 ftype = LDAP_FILTER_EQUALITY;
824 } else if ( strcmp( value, "*" ) == 0 ) {
825 ftype = LDAP_FILTER_PRESENT;
827 rc = put_substring_filter( ber, str, value );
833 if ( ftype == LDAP_FILTER_PRESENT ) {
834 rc = ber_printf( ber, "ts", ftype, str );
837 ber_slen_t len = filter_value_unescape( value );
840 rc = ber_printf( ber, "t{so}",
841 ftype, str, value, len );
845 if( rc != -1 ) rc = 0;
853 put_substring_filter( BerElement *ber, char *type, char *val )
855 char *nextstar, gotstar = 0;
856 ber_tag_t ftype = LDAP_FILTER_SUBSTRINGS;
858 Debug( LDAP_DEBUG_TRACE, "put_substring_filter \"%s=%s\"\n", type,
861 if ( ber_printf( ber, "t{s{", ftype, type ) == -1 )
864 for( ; val != NULL; val=nextstar ) {
865 if ( (nextstar = find_wildcard( val )) != NULL )
868 if ( gotstar == 0 ) {
869 ftype = LDAP_SUBSTRING_INITIAL;
870 } else if ( nextstar == NULL ) {
871 ftype = LDAP_SUBSTRING_FINAL;
873 ftype = LDAP_SUBSTRING_ANY;
876 if ( *val != '\0' ) {
877 ber_slen_t len = filter_value_unescape( val );
883 if ( ber_printf( ber, "to", ftype, val, len ) == -1 ) {
891 if ( ber_printf( ber, /* {{ */ "}}" ) == -1 )
899 LDAP *ld, LDAP_CONST char *base, int scope,
900 LDAP_CONST char *filter, char **attrs,
901 int attrsonly, struct timeval *timeout, LDAPMessage **res )
905 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
907 return( ld->ld_errno );
909 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 )
910 return( ld->ld_errno );
912 if ( ld->ld_errno == LDAP_TIMEOUT ) {
913 (void) ldap_abandon( ld, msgid );
914 ld->ld_errno = LDAP_TIMEOUT;
915 return( ld->ld_errno );
918 return( ldap_result2error( ld, *res, 0 ) );
924 LDAP_CONST char *base,
926 LDAP_CONST char *filter,
933 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
935 return( ld->ld_errno );
937 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, res ) == -1 )
938 return( ld->ld_errno );
940 return( ldap_result2error( ld, *res, 0 ) );