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,
97 Debug( LDAP_DEBUG_TRACE, "ldap_search_ext\n", 0, 0, 0 );
100 assert( LDAP_VALID( ld ) );
103 * if timeout is provided, both tv_sec and tv_usec must
106 if( timeout != NULL ) {
107 if( timeout->tv_sec == 0 && timeout->tv_usec == 0 ) {
108 return LDAP_PARAM_ERROR;
111 /* timelimit must be non-zero if timeout is provided */
112 timelimit = timeout->tv_sec != 0 ? timeout->tv_sec : 1;
115 /* no timeout, no timelimit */
119 ber = ldap_build_search_req( ld, base, scope, filter, attrs,
120 attrsonly, sctrls, cctrls, timelimit, sizelimit );
127 if ( ld->ld_cache != NULL ) {
128 if ( ldap_check_cache( ld, LDAP_REQ_SEARCH, ber ) == 0 ) {
130 ld->ld_errno = LDAP_SUCCESS;
131 *msgidp = ld->ld_msgid;
134 ldap_add_request_to_cache( ld, LDAP_REQ_SEARCH, ber );
136 #endif /* LDAP_NOCACHE */
138 /* send the message */
139 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber );
150 LDAP_CONST char *base,
152 LDAP_CONST char *filter,
155 LDAPControl **sctrls,
156 LDAPControl **cctrls,
157 struct timeval *timeout,
164 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
165 sctrls, cctrls, timeout, sizelimit, &msgid );
167 if ( rc != LDAP_SUCCESS ) {
171 rc = ldap_result( ld, msgid, 1, timeout, res );
174 /* error(-1) or timeout(0) */
175 return( ld->ld_errno );
178 if( rc == LDAP_RES_SEARCH_REFERENCE || rc == LDAP_RES_EXTENDED_PARTIAL ) {
179 return( ld->ld_errno );
182 return( ldap_result2error( ld, *res, 0 ) );
186 * ldap_search - initiate an ldap search operation.
191 * base DN of the base object
192 * scope the search scope - one of LDAP_SCOPE_BASE,
193 * LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE
194 * filter a string containing the search filter
195 * (e.g., "(|(cn=bob)(sn=bob))")
196 * attrs list of attribute types to return for matches
197 * attrsonly 1 => attributes only 0 => attributes and values
200 * char *attrs[] = { "mail", "title", 0 };
201 * msgid = ldap_search( ld, "c=us@o=UM", LDAP_SCOPE_SUBTREE, "cn~=bob",
202 * attrs, attrsonly );
206 LDAP *ld, LDAP_CONST char *base, int scope, LDAP_CONST char *filter,
207 char **attrs, int attrsonly )
211 Debug( LDAP_DEBUG_TRACE, "ldap_search\n", 0, 0, 0 );
213 assert( ld != NULL );
214 assert( LDAP_VALID( ld ) );
216 ber = ldap_build_search_req( ld, base, scope, filter, attrs,
217 attrsonly, NULL, NULL, -1, -1 );
224 if ( ld->ld_cache != NULL ) {
225 if ( ldap_check_cache( ld, LDAP_REQ_SEARCH, ber ) == 0 ) {
227 ld->ld_errno = LDAP_SUCCESS;
228 return( ld->ld_msgid );
230 ldap_add_request_to_cache( ld, LDAP_REQ_SEARCH, ber );
232 #endif /* LDAP_NOCACHE */
234 /* send the message */
235 return ( ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber ));
240 ldap_build_search_req(
242 LDAP_CONST char *base,
244 LDAP_CONST char *filter_in,
247 LDAPControl **sctrls,
248 LDAPControl **cctrls,
250 ber_int_t sizelimit )
257 * Create the search request. It looks like this:
258 * SearchRequest := [APPLICATION 3] SEQUENCE {
259 * baseObject DistinguishedName,
265 * derefAliases ENUMERATED {
266 * neverDerefaliases (0),
267 * derefInSearching (1),
268 * derefFindingBaseObj (2),
269 * alwaysDerefAliases (3)
271 * sizelimit INTEGER (0 .. 65535),
272 * timelimit INTEGER (0 .. 65535),
275 * attributes SEQUENCE OF AttributeType
277 * wrapped in an ldap message.
280 /* create a message to send */
281 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
285 if ( base == NULL ) {
286 /* no base provided, use session default base */
287 base = ld->ld_options.ldo_defbase;
289 if ( base == NULL ) {
290 /* no session default base, use top */
295 err = ber_printf( ber, "{it{seeiib", ++ld->ld_msgid,
296 LDAP_REQ_SEARCH, base, (ber_int_t) scope, ld->ld_deref,
297 (sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
298 (timelimit < 0) ? ld->ld_timelimit : timelimit,
302 ld->ld_errno = LDAP_ENCODING_ERROR;
307 if( filter_in != NULL ) {
308 filter = LDAP_STRDUP( filter_in );
310 filter = LDAP_STRDUP( "(objectclass=*)" );
312 err = put_filter( ber, filter );
316 ld->ld_errno = LDAP_FILTER_ERROR;
321 if ( ber_printf( ber, /*{*/ "{v}N}", attrs ) == -1 ) {
322 ld->ld_errno = LDAP_ENCODING_ERROR;
327 /* Put Server Controls */
328 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
333 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
334 ld->ld_errno = LDAP_ENCODING_ERROR;
342 static int ldap_is_attr_oid ( const char *attr )
346 for( i = 0; (c = attr[i]) != 0; i++ ) {
347 if( c >= '0' && c <= '9' ) {
350 } else if ( c != '.' ) {
351 /* not digit nor '.' */
354 } else if ( !digit ) {
355 /* '.' but prev not digit */
367 static int ldap_is_attr_desc ( const char *attr )
369 /* cheap attribute description check */
372 for( i = 0; (c = attr[i]) != 0; i++ ) {
373 if (( c >= '0' && c <= '9' )
374 || ( c >= 'A' && c <= 'Z' )
375 || ( c >= 'a' && c <= 'z' )
376 || ( c == '.' || c == '-' )
377 || ( c == ';' )) continue;
386 find_right_paren( char *s )
392 while ( *s && balance ) {
396 else if ( *s == ')' )
399 if ( *s == '\\' && ! escape )
407 return( *s ? s : NULL );
410 static int hex2value( int c )
412 if( c >= '0' && c <= '9' ) {
416 if( c >= 'A' && c <= 'F' ) {
417 return c + (10 - (int) 'A');
420 if( c >= 'a' && c <= 'f' ) {
421 return c + (10 - (int) 'a');
428 ldap_pvt_find_wildcard( const char *s )
430 for( ; *s != '\0' ; s++ ) {
432 case '*': /* found wildcard */
436 s++; /* skip over escape */
438 return NULL; /* escape at end of string */
445 /* unescape filter value */
446 /* support both LDAP v2 and v3 escapes */
447 /* output can include nul characters */
449 ldap_pvt_filter_value_unescape( char *fval )
454 for( r=v=0; fval[v] != '\0'; v++ ) {
460 if ( fval[v] == '\0' ) {
461 /* escape at end of string */
466 if (( v1 = hex2value( fval[v] )) >= 0 ) {
469 if (( v2 = hex2value( fval[v+1] )) < 0 ) {
470 /* must be two digit code */
474 fval[r++] = v1 * 16 + v2;
494 put_complex_filter( BerElement *ber, char *str, ber_tag_t tag, int not )
499 * We have (x(filter)...) with str sitting on
500 * the x. We have to find the paren matching
501 * the one before the x and put the intervening
502 * filters by calling put_filter_list().
505 /* put explicit tag */
506 if ( ber_printf( ber, "t{" /*}*/, tag ) == -1 )
510 if ( (next = find_right_paren( str )) == NULL )
514 if ( put_filter_list( ber, str ) == -1 )
518 /* flush explicit tagged thang */
519 if ( ber_printf( ber, /*{*/ "N}" ) == -1 )
526 put_filter( BerElement *ber, char *str )
529 int parens, balance, escape;
532 * A Filter looks like this:
533 * Filter ::= CHOICE {
534 * and [0] SET OF Filter,
535 * or [1] SET OF Filter,
537 * equalityMatch [3] AttributeValueAssertion,
538 * substrings [4] SubstringFilter,
539 * greaterOrEqual [5] AttributeValueAssertion,
540 * lessOrEqual [6] AttributeValueAssertion,
541 * present [7] AttributeType,
542 * approxMatch [8] AttributeValueAssertion,
543 * extensibleMatch [9] MatchingRuleAssertion -- LDAPv3
546 * SubstringFilter ::= SEQUENCE {
547 * type AttributeType,
548 * SEQUENCE OF CHOICE {
549 * initial [0] IA5String,
551 * final [2] IA5String
555 * MatchingRuleAssertion ::= SEQUENCE { -- LDAPv3
556 * matchingRule [1] MatchingRuleId OPTIONAL,
557 * type [2] AttributeDescription OPTIONAL,
558 * matchValue [3] AssertionValue,
559 * dnAttributes [4] BOOLEAN DEFAULT FALSE }
561 * Note: tags in a choice are always explicit
564 Debug( LDAP_DEBUG_TRACE, "put_filter \"%s\"\n", str, 0, 0 );
574 while( LDAP_SPACE( *str ) ) str++;
578 Debug( LDAP_DEBUG_TRACE, "put_filter: AND\n",
581 if ( (str = put_complex_filter( ber, str,
582 LDAP_FILTER_AND, 0 )) == NULL )
589 Debug( LDAP_DEBUG_TRACE, "put_filter: OR\n",
592 if ( (str = put_complex_filter( ber, str,
593 LDAP_FILTER_OR, 0 )) == NULL )
600 Debug( LDAP_DEBUG_TRACE, "put_filter: NOT\n",
603 if ( (str = put_complex_filter( ber, str,
604 LDAP_FILTER_NOT, 1 )) == NULL )
611 Debug( LDAP_DEBUG_TRACE, "put_filter: simple\n",
617 while ( *next && balance ) {
621 else if ( *next == ')' )
624 if ( *next == '\\' && ! escape )
635 if ( put_simple_filter( ber, str ) == -1 ) {
646 Debug( LDAP_DEBUG_TRACE, "put_filter: end\n", 0, 0,
648 if ( ber_printf( ber, /*[*/ "]" ) == -1 )
658 default: /* assume it's a simple type=value filter */
659 Debug( LDAP_DEBUG_TRACE, "put_filter: default\n", 0, 0,
661 next = strchr( str, '\0' );
662 if ( put_simple_filter( ber, str ) == -1 ) {
670 return( parens ? -1 : 0 );
674 * Put a list of filters like this "(filter1)(filter2)..."
678 put_filter_list( BerElement *ber, char *str )
683 Debug( LDAP_DEBUG_TRACE, "put_filter_list \"%s\"\n", str, 0, 0 );
686 while ( *str && LDAP_SPACE( (unsigned char) *str ) )
691 if ( (next = find_right_paren( str + 1 )) == NULL )
695 /* now we have "(filter)" with str pointing to it */
697 if ( put_filter( ber, str ) == -1 )
717 Debug( LDAP_DEBUG_TRACE, "put_simple_filter \"%s\"\n", str, 0, 0 );
719 str = LDAP_STRDUP( str );
720 if( str == NULL ) return -1;
722 if ( (s = strchr( str, '=' )) == NULL ) {
731 ftype = LDAP_FILTER_LE;
733 if(! ldap_is_attr_desc( str ) ) goto done;
737 ftype = LDAP_FILTER_GE;
739 if(! ldap_is_attr_desc( str ) ) goto done;
743 ftype = LDAP_FILTER_APPROX;
745 if(! ldap_is_attr_desc( str ) ) goto done;
749 /* RFC2254 extensible filters are off the form:
750 * type [:dn] [:rule] := value
751 * or [:dn]:rule := value
753 ftype = LDAP_FILTER_EXT;
757 char *dn = strchr( str, ':' );
761 if(! ldap_is_attr_desc( str ) ) goto done;
765 rule = strchr( dn, ':' );
769 if ( strcmp(dn, "dn") == 0 ) {
770 /* must have attribute */
771 if( !ldap_is_attr_desc( str ) ) {
786 if ( strcmp(dn, "dn") != 0 ) {
794 if ( *str == '\0' && ( !rule || *rule == '\0' ) ) {
795 /* must have either type or rule */
799 if ( *str != '\0' && !ldap_is_attr_desc( str ) ) {
803 if ( rule && *rule != '\0' && !ldap_is_attr_oid( rule ) ) {
807 rc = ber_printf( ber, "t{" /*}*/, ftype );
809 if( rc != -1 && rule && *rule != '\0' ) {
810 rc = ber_printf( ber, "ts", LDAP_FILTER_EXT_OID, rule );
812 if( rc != -1 && *str != '\0' ) {
813 rc = ber_printf( ber, "ts", LDAP_FILTER_EXT_TYPE, str );
817 ber_slen_t len = ldap_pvt_filter_value_unescape( value );
820 rc = ber_printf( ber, "totbN}",
821 LDAP_FILTER_EXT_VALUE, value, len,
822 LDAP_FILTER_EXT_DNATTRS, dn != NULL);
831 if ( ldap_pvt_find_wildcard( value ) == NULL ) {
832 ftype = LDAP_FILTER_EQUALITY;
833 } else if ( strcmp( value, "*" ) == 0 ) {
834 ftype = LDAP_FILTER_PRESENT;
836 rc = put_substring_filter( ber, str, value );
842 if ( ftype == LDAP_FILTER_PRESENT ) {
843 rc = ber_printf( ber, "ts", ftype, str );
846 ber_slen_t len = ldap_pvt_filter_value_unescape( value );
849 rc = ber_printf( ber, "t{soN}",
850 ftype, str, value, len );
854 if( rc != -1 ) rc = 0;
862 put_substring_filter( BerElement *ber, char *type, char *val )
864 char *nextstar, gotstar = 0;
865 ber_tag_t ftype = LDAP_FILTER_SUBSTRINGS;
867 Debug( LDAP_DEBUG_TRACE, "put_substring_filter \"%s=%s\"\n", type,
870 if ( ber_printf( ber, "t{s{", ftype, type ) == -1 )
873 for( ; val != NULL; val=nextstar ) {
874 if ( (nextstar = ldap_pvt_find_wildcard( val )) != NULL )
877 if ( gotstar == 0 ) {
878 ftype = LDAP_SUBSTRING_INITIAL;
879 } else if ( nextstar == NULL ) {
880 ftype = LDAP_SUBSTRING_FINAL;
882 ftype = LDAP_SUBSTRING_ANY;
885 if ( *val != '\0' ) {
886 ber_slen_t len = ldap_pvt_filter_value_unescape( val );
892 if ( ber_printf( ber, "to", ftype, val, len ) == -1 ) {
900 if ( ber_printf( ber, /* {{ */ "N}N}" ) == -1 )
908 LDAP *ld, LDAP_CONST char *base, int scope,
909 LDAP_CONST char *filter, char **attrs,
910 int attrsonly, struct timeval *timeout, LDAPMessage **res )
914 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
916 return( ld->ld_errno );
918 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 )
919 return( ld->ld_errno );
921 if ( ld->ld_errno == LDAP_TIMEOUT ) {
922 (void) ldap_abandon( ld, msgid );
923 ld->ld_errno = LDAP_TIMEOUT;
924 return( ld->ld_errno );
927 return( ldap_result2error( ld, *res, 0 ) );
933 LDAP_CONST char *base,
935 LDAP_CONST char *filter,
942 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
944 return( ld->ld_errno );
946 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, res ) == -1 )
947 return( ld->ld_errno );
949 return( ldap_result2error( ld, *res, 0 ) );