3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1990 Regents of the University of Michigan.
17 #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 char *find_right_paren LDAP_P((
37 static char *put_complex_filter LDAP_P((
43 static int put_filter LDAP_P((
47 static int put_simple_filter LDAP_P((
51 static int put_substring_filter LDAP_P((
56 static int put_filter_list LDAP_P((
61 * ldap_search_ext - initiate an ldap search operation.
66 * base DN of the base object
67 * scope the search scope - one of LDAP_SCOPE_BASE,
68 * LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE
69 * filter a string containing the search filter
70 * (e.g., "(|(cn=bob)(sn=bob))")
71 * attrs list of attribute types to return for matches
72 * attrsonly 1 => attributes only 0 => attributes and values
75 * char *attrs[] = { "mail", "title", 0 };
76 * ldap_search_ext( ld, "c=us,o=UM", LDAP_SCOPE_SUBTREE, "cn~=bob",
77 * attrs, attrsonly, sctrls, ctrls, timeout, sizelimit,
83 LDAP_CONST char *base,
85 LDAP_CONST char *filter,
90 struct timeval *timeout,
98 Debug( LDAP_DEBUG_TRACE, "ldap_search_ext\n", 0, 0, 0 );
100 assert( ld != NULL );
101 assert( LDAP_VALID( ld ) );
103 /* check client controls */
104 rc = ldap_int_client_controls( ld, cctrls );
105 if( rc != LDAP_SUCCESS ) return rc;
108 * if timeout is provided, both tv_sec and tv_usec must
111 if( timeout != NULL ) {
112 if( timeout->tv_sec == 0 && timeout->tv_usec == 0 ) {
113 return LDAP_PARAM_ERROR;
116 /* timelimit must be non-zero if timeout is provided */
117 timelimit = timeout->tv_sec != 0 ? timeout->tv_sec : 1;
120 /* no timeout, no timelimit */
124 ber = ldap_build_search_req( ld, base, scope, filter, attrs,
125 attrsonly, sctrls, cctrls, timelimit, sizelimit );
132 if ( ld->ld_cache != NULL ) {
133 if ( ldap_check_cache( ld, LDAP_REQ_SEARCH, ber ) == 0 ) {
135 ld->ld_errno = LDAP_SUCCESS;
136 *msgidp = ld->ld_msgid;
139 ldap_add_request_to_cache( ld, LDAP_REQ_SEARCH, ber );
141 #endif /* LDAP_NOCACHE */
143 /* send the message */
144 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber );
155 LDAP_CONST char *base,
157 LDAP_CONST char *filter,
160 LDAPControl **sctrls,
161 LDAPControl **cctrls,
162 struct timeval *timeout,
169 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
170 sctrls, cctrls, timeout, sizelimit, &msgid );
172 if ( rc != LDAP_SUCCESS ) {
176 rc = ldap_result( ld, msgid, 1, timeout, res );
179 /* error(-1) or timeout(0) */
180 return( ld->ld_errno );
183 if( rc == LDAP_RES_SEARCH_REFERENCE || rc == LDAP_RES_EXTENDED_PARTIAL ) {
184 return( ld->ld_errno );
187 return( ldap_result2error( ld, *res, 0 ) );
191 * ldap_search - initiate an ldap search operation.
196 * base DN of the base object
197 * scope the search scope - one of LDAP_SCOPE_BASE,
198 * LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE
199 * filter a string containing the search filter
200 * (e.g., "(|(cn=bob)(sn=bob))")
201 * attrs list of attribute types to return for matches
202 * attrsonly 1 => attributes only 0 => attributes and values
205 * char *attrs[] = { "mail", "title", 0 };
206 * msgid = ldap_search( ld, "c=us@o=UM", LDAP_SCOPE_SUBTREE, "cn~=bob",
207 * attrs, attrsonly );
211 LDAP *ld, LDAP_CONST char *base, int scope, LDAP_CONST char *filter,
212 char **attrs, int attrsonly )
216 Debug( LDAP_DEBUG_TRACE, "ldap_search\n", 0, 0, 0 );
218 assert( ld != NULL );
219 assert( LDAP_VALID( ld ) );
221 ber = ldap_build_search_req( ld, base, scope, filter, attrs,
222 attrsonly, NULL, NULL, -1, -1 );
229 if ( ld->ld_cache != NULL ) {
230 if ( ldap_check_cache( ld, LDAP_REQ_SEARCH, ber ) == 0 ) {
232 ld->ld_errno = LDAP_SUCCESS;
233 return( ld->ld_msgid );
235 ldap_add_request_to_cache( ld, LDAP_REQ_SEARCH, ber );
237 #endif /* LDAP_NOCACHE */
239 /* send the message */
240 return ( ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber ));
245 ldap_build_search_req(
247 LDAP_CONST char *base,
249 LDAP_CONST char *filter_in,
252 LDAPControl **sctrls,
253 LDAPControl **cctrls,
255 ber_int_t sizelimit )
262 * Create the search request. It looks like this:
263 * SearchRequest := [APPLICATION 3] SEQUENCE {
264 * baseObject DistinguishedName,
270 * derefAliases ENUMERATED {
271 * neverDerefaliases (0),
272 * derefInSearching (1),
273 * derefFindingBaseObj (2),
274 * alwaysDerefAliases (3)
276 * sizelimit INTEGER (0 .. 65535),
277 * timelimit INTEGER (0 .. 65535),
280 * attributes SEQUENCE OF AttributeType
282 * wrapped in an ldap message.
285 /* create a message to send */
286 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
290 if ( base == NULL ) {
291 /* no base provided, use session default base */
292 base = ld->ld_options.ldo_defbase;
294 if ( base == NULL ) {
295 /* no session default base, use top */
300 err = ber_printf( ber, "{it{seeiib", ++ld->ld_msgid,
301 LDAP_REQ_SEARCH, base, (ber_int_t) scope, ld->ld_deref,
302 (sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
303 (timelimit < 0) ? ld->ld_timelimit : timelimit,
307 ld->ld_errno = LDAP_ENCODING_ERROR;
312 if( filter_in != NULL ) {
313 filter = LDAP_STRDUP( filter_in );
315 filter = LDAP_STRDUP( "(objectclass=*)" );
317 err = put_filter( ber, filter );
321 ld->ld_errno = LDAP_FILTER_ERROR;
326 if ( ber_printf( ber, /*{*/ "{v}N}", attrs ) == -1 ) {
327 ld->ld_errno = LDAP_ENCODING_ERROR;
332 /* Put Server Controls */
333 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
338 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
339 ld->ld_errno = LDAP_ENCODING_ERROR;
347 static int ldap_is_attr_oid ( const char *attr )
351 for( i = 0; (c = attr[i]) != 0; i++ ) {
352 if( c >= '0' && c <= '9' ) {
355 } else if ( c != '.' ) {
356 /* not digit nor '.' */
359 } else if ( !digit ) {
360 /* '.' but prev not digit */
372 static int ldap_is_attr_desc ( const char *attr )
374 /* cheap attribute description check */
377 for( i = 0; (c = attr[i]) != 0; i++ ) {
378 if (( c >= '0' && c <= '9' )
379 || ( c >= 'A' && c <= 'Z' )
380 || ( c >= 'a' && c <= 'z' )
381 || ( c == '.' || c == '-' )
382 || ( c == ';' )) continue;
391 find_right_paren( char *s )
397 while ( *s && balance ) {
401 else if ( *s == ')' )
404 if ( *s == '\\' && ! escape )
412 return( *s ? s : NULL );
415 static int hex2value( int c )
417 if( c >= '0' && c <= '9' ) {
421 if( c >= 'A' && c <= 'F' ) {
422 return c + (10 - (int) 'A');
425 if( c >= 'a' && c <= 'f' ) {
426 return c + (10 - (int) 'a');
433 ldap_pvt_find_wildcard( const char *s )
435 for( ; *s != '\0' ; s++ ) {
437 case '*': /* found wildcard */
441 s++; /* skip over escape */
443 return NULL; /* escape at end of string */
450 /* unescape filter value */
451 /* support both LDAP v2 and v3 escapes */
452 /* output can include nul characters */
454 ldap_pvt_filter_value_unescape( char *fval )
459 for( r=v=0; fval[v] != '\0'; v++ ) {
465 if ( fval[v] == '\0' ) {
466 /* escape at end of string */
471 if (( v1 = hex2value( fval[v] )) >= 0 ) {
474 if (( v2 = hex2value( fval[v+1] )) < 0 ) {
475 /* must be two digit code */
479 fval[r++] = v1 * 16 + v2;
499 put_complex_filter( BerElement *ber, char *str, ber_tag_t tag, int not )
504 * We have (x(filter)...) with str sitting on
505 * the x. We have to find the paren matching
506 * the one before the x and put the intervening
507 * filters by calling put_filter_list().
510 /* put explicit tag */
511 if ( ber_printf( ber, "t{" /*}*/, tag ) == -1 )
515 if ( (next = find_right_paren( str )) == NULL )
519 if ( put_filter_list( ber, str ) == -1 )
523 /* flush explicit tagged thang */
524 if ( ber_printf( ber, /*{*/ "N}" ) == -1 )
531 put_filter( BerElement *ber, char *str )
534 int parens, balance, escape;
537 * A Filter looks like this:
538 * Filter ::= CHOICE {
539 * and [0] SET OF Filter,
540 * or [1] SET OF Filter,
542 * equalityMatch [3] AttributeValueAssertion,
543 * substrings [4] SubstringFilter,
544 * greaterOrEqual [5] AttributeValueAssertion,
545 * lessOrEqual [6] AttributeValueAssertion,
546 * present [7] AttributeType,
547 * approxMatch [8] AttributeValueAssertion,
548 * extensibleMatch [9] MatchingRuleAssertion -- LDAPv3
551 * SubstringFilter ::= SEQUENCE {
552 * type AttributeType,
553 * SEQUENCE OF CHOICE {
554 * initial [0] IA5String,
556 * final [2] IA5String
560 * MatchingRuleAssertion ::= SEQUENCE { -- LDAPv3
561 * matchingRule [1] MatchingRuleId OPTIONAL,
562 * type [2] AttributeDescription OPTIONAL,
563 * matchValue [3] AssertionValue,
564 * dnAttributes [4] BOOLEAN DEFAULT FALSE }
566 * Note: tags in a choice are always explicit
569 Debug( LDAP_DEBUG_TRACE, "put_filter \"%s\"\n", str, 0, 0 );
579 while( LDAP_SPACE( *str ) ) str++;
583 Debug( LDAP_DEBUG_TRACE, "put_filter: AND\n",
586 if ( (str = put_complex_filter( ber, str,
587 LDAP_FILTER_AND, 0 )) == NULL )
594 Debug( LDAP_DEBUG_TRACE, "put_filter: OR\n",
597 if ( (str = put_complex_filter( ber, str,
598 LDAP_FILTER_OR, 0 )) == NULL )
605 Debug( LDAP_DEBUG_TRACE, "put_filter: NOT\n",
608 if ( (str = put_complex_filter( ber, str,
609 LDAP_FILTER_NOT, 1 )) == NULL )
616 Debug( LDAP_DEBUG_TRACE, "put_filter: simple\n",
622 while ( *next && balance ) {
626 else if ( *next == ')' )
629 if ( *next == '\\' && ! escape )
640 if ( put_simple_filter( ber, str ) == -1 ) {
651 Debug( LDAP_DEBUG_TRACE, "put_filter: end\n", 0, 0,
653 if ( ber_printf( ber, /*[*/ "]" ) == -1 )
663 default: /* assume it's a simple type=value filter */
664 Debug( LDAP_DEBUG_TRACE, "put_filter: default\n", 0, 0,
666 next = strchr( str, '\0' );
667 if ( put_simple_filter( ber, str ) == -1 ) {
675 return( parens ? -1 : 0 );
679 * Put a list of filters like this "(filter1)(filter2)..."
683 put_filter_list( BerElement *ber, char *str )
688 Debug( LDAP_DEBUG_TRACE, "put_filter_list \"%s\"\n", str, 0, 0 );
691 while ( *str && LDAP_SPACE( (unsigned char) *str ) )
696 if ( (next = find_right_paren( str + 1 )) == NULL )
700 /* now we have "(filter)" with str pointing to it */
702 if ( put_filter( ber, str ) == -1 )
722 Debug( LDAP_DEBUG_TRACE, "put_simple_filter \"%s\"\n", str, 0, 0 );
724 str = LDAP_STRDUP( str );
725 if( str == NULL ) return -1;
727 if ( (s = strchr( str, '=' )) == NULL ) {
736 ftype = LDAP_FILTER_LE;
738 if(! ldap_is_attr_desc( str ) ) goto done;
742 ftype = LDAP_FILTER_GE;
744 if(! ldap_is_attr_desc( str ) ) goto done;
748 ftype = LDAP_FILTER_APPROX;
750 if(! ldap_is_attr_desc( str ) ) goto done;
754 /* RFC2254 extensible filters are off the form:
755 * type [:dn] [:rule] := value
756 * or [:dn]:rule := value
758 ftype = LDAP_FILTER_EXT;
762 char *dn = strchr( str, ':' );
766 if(! ldap_is_attr_desc( str ) ) goto done;
770 rule = strchr( dn, ':' );
774 if ( strcmp(dn, "dn") == 0 ) {
775 /* must have attribute */
776 if( !ldap_is_attr_desc( str ) ) {
791 if ( strcmp(dn, "dn") != 0 ) {
799 if ( *str == '\0' && ( !rule || *rule == '\0' ) ) {
800 /* must have either type or rule */
804 if ( *str != '\0' && !ldap_is_attr_desc( str ) ) {
808 if ( rule && *rule != '\0' && !ldap_is_attr_oid( rule ) ) {
812 rc = ber_printf( ber, "t{" /*}*/, ftype );
814 if( rc != -1 && rule && *rule != '\0' ) {
815 rc = ber_printf( ber, "ts", LDAP_FILTER_EXT_OID, rule );
817 if( rc != -1 && *str != '\0' ) {
818 rc = ber_printf( ber, "ts", LDAP_FILTER_EXT_TYPE, str );
822 ber_slen_t len = ldap_pvt_filter_value_unescape( value );
825 rc = ber_printf( ber, "totbN}",
826 LDAP_FILTER_EXT_VALUE, value, len,
827 LDAP_FILTER_EXT_DNATTRS, dn != NULL);
836 if ( ldap_pvt_find_wildcard( value ) == NULL ) {
837 ftype = LDAP_FILTER_EQUALITY;
838 } else if ( strcmp( value, "*" ) == 0 ) {
839 ftype = LDAP_FILTER_PRESENT;
841 rc = put_substring_filter( ber, str, value );
847 if ( ftype == LDAP_FILTER_PRESENT ) {
848 rc = ber_printf( ber, "ts", ftype, str );
851 ber_slen_t len = ldap_pvt_filter_value_unescape( value );
854 rc = ber_printf( ber, "t{soN}",
855 ftype, str, value, len );
859 if( rc != -1 ) rc = 0;
867 put_substring_filter( BerElement *ber, char *type, char *val )
869 char *nextstar, gotstar = 0;
870 ber_tag_t ftype = LDAP_FILTER_SUBSTRINGS;
872 Debug( LDAP_DEBUG_TRACE, "put_substring_filter \"%s=%s\"\n", type,
875 if ( ber_printf( ber, "t{s{", ftype, type ) == -1 )
878 for( ; val != NULL; val=nextstar ) {
879 if ( (nextstar = ldap_pvt_find_wildcard( val )) != NULL )
882 if ( gotstar == 0 ) {
883 ftype = LDAP_SUBSTRING_INITIAL;
884 } else if ( nextstar == NULL ) {
885 ftype = LDAP_SUBSTRING_FINAL;
887 ftype = LDAP_SUBSTRING_ANY;
890 if ( *val != '\0' ) {
891 ber_slen_t len = ldap_pvt_filter_value_unescape( val );
897 if ( ber_printf( ber, "to", ftype, val, len ) == -1 ) {
905 if ( ber_printf( ber, /* {{ */ "N}N}" ) == -1 )
913 LDAP *ld, LDAP_CONST char *base, int scope,
914 LDAP_CONST char *filter, char **attrs,
915 int attrsonly, struct timeval *timeout, LDAPMessage **res )
919 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
921 return( ld->ld_errno );
923 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 )
924 return( ld->ld_errno );
926 if ( ld->ld_errno == LDAP_TIMEOUT ) {
927 (void) ldap_abandon( ld, msgid );
928 ld->ld_errno = LDAP_TIMEOUT;
929 return( ld->ld_errno );
932 return( ldap_result2error( ld, *res, 0 ) );
938 LDAP_CONST char *base,
940 LDAP_CONST char *filter,
947 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
949 return( ld->ld_errno );
951 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, res ) == -1 )
952 return( ld->ld_errno );
954 return( ldap_result2error( ld, *res, 0 ) );