3 * Copyright 1998-1999 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>
20 #include <ac/socket.h>
21 #include <ac/string.h>
26 static int ldap_is_attr_oid LDAP_P((
29 static int ldap_is_attr_desc LDAP_P((
32 static int hex2value LDAP_P((
35 static char *find_right_paren LDAP_P((
38 static char *put_complex_filter LDAP_P((
44 static int put_filter LDAP_P((
48 static int put_simple_filter LDAP_P((
52 static int put_substring_filter LDAP_P((
57 static int put_filter_list LDAP_P((
62 * ldap_search_ext - initiate an ldap search operation.
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
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,
84 LDAP_CONST char *base,
86 LDAP_CONST char *filter,
91 struct timeval *timeout,
98 Debug( LDAP_DEBUG_TRACE, "ldap_search_ext\n", 0, 0, 0 );
101 * if timeout is provided, both tv_sec and tv_usec must
104 if( timeout != NULL ) {
105 if( timeout->tv_sec == 0 && timeout->tv_usec == 0 ) {
106 return LDAP_PARAM_ERROR;
109 /* timelimit must be non-zero if timeout is provided */
110 timelimit = timeout->tv_sec != 0 ? timeout->tv_sec : 1;
113 /* no timeout, no timelimit */
117 ber = ldap_build_search_req( ld, base, scope, filter, attrs,
118 attrsonly, sctrls, cctrls, timelimit, sizelimit );
125 if ( ld->ld_cache != NULL ) {
126 if ( ldap_check_cache( ld, LDAP_REQ_SEARCH, ber ) == 0 ) {
128 ld->ld_errno = LDAP_SUCCESS;
129 *msgidp = ld->ld_msgid;
132 ldap_add_request_to_cache( ld, LDAP_REQ_SEARCH, ber );
134 #endif /* LDAP_NOCACHE */
136 /* send the message */
137 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber );
148 LDAP_CONST char *base,
150 LDAP_CONST char *filter,
153 LDAPControl **sctrls,
154 LDAPControl **cctrls,
155 struct timeval *timeout,
162 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
163 sctrls, cctrls, timeout, sizelimit, &msgid );
165 if ( rc != LDAP_SUCCESS ) {
169 rc = ldap_result( ld, msgid, 1, timeout, res );
172 /* error(-1) or timeout(0) */
173 return( ld->ld_errno );
176 return( ldap_result2error( ld, *res, 0 ) );
180 * ldap_search - initiate an ldap search operation.
185 * base DN of the base object
186 * scope the search scope - one of LDAP_SCOPE_BASE,
187 * LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE
188 * filter a string containing the search filter
189 * (e.g., "(|(cn=bob)(sn=bob))")
190 * attrs list of attribute types to return for matches
191 * attrsonly 1 => attributes only 0 => attributes and values
194 * char *attrs[] = { "mail", "title", 0 };
195 * msgid = ldap_search( ld, "c=us@o=UM", LDAP_SCOPE_SUBTREE, "cn~=bob",
196 * attrs, attrsonly );
200 LDAP *ld, LDAP_CONST char *base, int scope, LDAP_CONST char *filter,
201 char **attrs, int attrsonly )
205 Debug( LDAP_DEBUG_TRACE, "ldap_search\n", 0, 0, 0 );
207 ber = ldap_build_search_req( ld, base, scope, filter, attrs,
208 attrsonly, NULL, NULL, -1, -1 );
215 if ( ld->ld_cache != NULL ) {
216 if ( ldap_check_cache( ld, LDAP_REQ_SEARCH, ber ) == 0 ) {
218 ld->ld_errno = LDAP_SUCCESS;
219 return( ld->ld_msgid );
221 ldap_add_request_to_cache( ld, LDAP_REQ_SEARCH, ber );
223 #endif /* LDAP_NOCACHE */
225 /* send the message */
226 return ( ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber ));
231 ldap_build_search_req(
233 LDAP_CONST char *base,
235 LDAP_CONST char *filter_in,
238 LDAPControl **sctrls,
239 LDAPControl **cctrls,
241 ber_int_t sizelimit )
248 * Create the search request. It looks like this:
249 * SearchRequest := [APPLICATION 3] SEQUENCE {
250 * baseObject DistinguishedName,
256 * derefAliases ENUMERATED {
257 * neverDerefaliases (0),
258 * derefInSearching (1),
259 * derefFindingBaseObj (2),
260 * alwaysDerefAliases (3)
262 * sizelimit INTEGER (0 .. 65535),
263 * timelimit INTEGER (0 .. 65535),
266 * attributes SEQUENCE OF AttributeType
268 * wrapped in an ldap message.
271 /* create a message to send */
272 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
276 if ( base == NULL ) {
277 /* no base provided, use session default base */
278 base = ld->ld_options.ldo_defbase;
280 if ( base == NULL ) {
281 /* no session default base, use top */
286 #ifdef LDAP_CONNECTIONLESS
287 if ( ld->ld_cldapnaddr > 0 ) {
288 err = ber_printf( ber, "{ist{seeiib", ++ld->ld_msgid,
289 ld->ld_cldapdn, LDAP_REQ_SEARCH, base, scope, ld->ld_deref,
290 (sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
291 (timelimit < 0) ? ld->ld_timelimit : timelimit,
294 #endif /* LDAP_CONNECTIONLESS */
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,
300 #ifdef LDAP_CONNECTIONLESS
302 #endif /* LDAP_CONNECTIONLESS */
305 ld->ld_errno = LDAP_ENCODING_ERROR;
310 filter = LDAP_STRDUP( filter_in );
311 err = put_filter( ber, filter );
315 ld->ld_errno = LDAP_FILTER_ERROR;
320 if ( ber_printf( ber, /*{*/ "{v}}", attrs ) == -1 ) {
321 ld->ld_errno = LDAP_ENCODING_ERROR;
326 /* Put Server Controls */
327 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
332 if ( ber_printf( ber, /*{*/ "}", attrs ) == -1 ) {
333 ld->ld_errno = LDAP_ENCODING_ERROR;
341 static int ldap_is_attr_oid ( const char *attr )
345 for( i = 0; (c = attr[i]) != 0; i++ ) {
346 if( c >= '0' && c <= '9' ) {
349 } else if ( c != '.' ) {
350 /* not digit nor '.' */
353 } else if ( !digit ) {
354 /* '.' but prev not digit */
366 static int ldap_is_attr_desc ( const char *attr )
368 /* cheap attribute description check */
371 for( i = 0; (c = attr[i]) != 0; i++ ) {
372 if (( c >= '0' && c <= '9' )
373 || ( c >= 'A' && c <= 'Z' )
374 || ( c >= 'a' && c <= 'z' )
375 || ( c == '.' || c == '-' )
376 || ( c == ';' )) continue;
385 find_right_paren( char *s )
391 while ( *s && balance ) {
395 else if ( *s == ')' )
398 if ( *s == '\\' && ! escape )
406 return( *s ? s : NULL );
409 static int hex2value( int c )
411 if( c >= '0' && c <= '9' ) {
415 if( c >= 'A' && c <= 'F' ) {
416 return c + (10 - (int) 'A');
419 if( c >= 'a' && c <= 'f' ) {
420 return c + (10 - (int) 'a');
427 ldap_pvt_find_wildcard( char *s )
429 for( ; *s != '\0' ; s++ ) {
431 case '*': /* found wildcard */
435 s++; /* skip over escape */
437 return NULL; /* escape at end of string */
444 /* unescape filter value */
445 /* support both LDAP v2 and v3 escapes */
446 /* output can include nul characters */
448 ldap_pvt_filter_value_unescape( char *fval )
453 for( r=v=0; fval[v] != '\0'; v++ ) {
459 if ( fval[v] == '\0' ) {
460 /* escape at end of string */
465 if (( v1 = hex2value( fval[v] )) >= 0 ) {
468 if (( v2 = hex2value( fval[v+1] )) < 0 ) {
469 /* must be two digit code */
473 fval[r++] = v1 * 16 + v2;
493 put_complex_filter( BerElement *ber, char *str, ber_tag_t tag, int not )
498 * We have (x(filter)...) with str sitting on
499 * the x. We have to find the paren matching
500 * the one before the x and put the intervening
501 * filters by calling put_filter_list().
504 /* put explicit tag */
505 if ( ber_printf( ber, "t{" /*}*/, tag ) == -1 )
509 if ( (next = find_right_paren( str )) == NULL )
513 if ( put_filter_list( ber, str ) == -1 )
517 /* flush explicit tagged thang */
518 if ( ber_printf( ber, /*{*/ "}" ) == -1 )
525 put_filter( BerElement *ber, char *str )
528 int parens, balance, escape;
531 * A Filter looks like this:
532 * Filter ::= CHOICE {
533 * and [0] SET OF Filter,
534 * or [1] SET OF Filter,
536 * equalityMatch [3] AttributeValueAssertion,
537 * substrings [4] SubstringFilter,
538 * greaterOrEqual [5] AttributeValueAssertion,
539 * lessOrEqual [6] AttributeValueAssertion,
540 * present [7] AttributeType,
541 * approxMatch [8] AttributeValueAssertion,
542 * extensibleMatch [9] MatchingRuleAssertion -- LDAPv3
545 * SubstringFilter ::= SEQUENCE {
546 * type AttributeType,
547 * SEQUENCE OF CHOICE {
548 * initial [0] IA5String,
550 * final [2] IA5String
554 * MatchingRuleAssertion ::= SEQUENCE { -- LDAPv3
555 * matchingRule [1] MatchingRuleId OPTIONAL,
556 * type [2] AttributeDescription OPTIONAL,
557 * matchValue [3] AssertionValue,
558 * dnAttributes [4] BOOLEAN DEFAULT FALSE }
560 * Note: tags in a choice are always explicit
563 Debug( LDAP_DEBUG_TRACE, "put_filter \"%s\"\n", str, 0, 0 );
573 Debug( LDAP_DEBUG_TRACE, "put_filter: AND\n",
576 if ( (str = put_complex_filter( ber, str,
577 LDAP_FILTER_AND, 0 )) == NULL )
584 Debug( LDAP_DEBUG_TRACE, "put_filter: OR\n",
587 if ( (str = put_complex_filter( ber, str,
588 LDAP_FILTER_OR, 0 )) == NULL )
595 Debug( LDAP_DEBUG_TRACE, "put_filter: NOT\n",
598 if ( (str = put_complex_filter( ber, str,
599 LDAP_FILTER_NOT, 1 )) == NULL )
606 Debug( LDAP_DEBUG_TRACE, "put_filter: simple\n",
612 while ( *next && balance ) {
616 else if ( *next == ')' )
619 if ( *next == '\\' && ! escape )
630 if ( put_simple_filter( ber, str ) == -1 ) {
641 Debug( LDAP_DEBUG_TRACE, "put_filter: end\n", 0, 0,
643 if ( ber_printf( ber, /*[*/ "]" ) == -1 )
653 default: /* assume it's a simple type=value filter */
654 Debug( LDAP_DEBUG_TRACE, "put_filter: default\n", 0, 0,
656 next = strchr( str, '\0' );
657 if ( put_simple_filter( ber, str ) == -1 ) {
665 return( parens ? -1 : 0 );
669 * Put a list of filters like this "(filter1)(filter2)..."
673 put_filter_list( BerElement *ber, char *str )
678 Debug( LDAP_DEBUG_TRACE, "put_filter_list \"%s\"\n", str, 0, 0 );
681 while ( *str && isspace( (unsigned char) *str ) )
686 if ( (next = find_right_paren( str + 1 )) == NULL )
690 /* now we have "(filter)" with str pointing to it */
692 if ( put_filter( ber, str ) == -1 )
712 Debug( LDAP_DEBUG_TRACE, "put_simple_filter \"%s\"\n", str, 0, 0 );
714 str = LDAP_STRDUP( str );
715 if( str == NULL ) return -1;
717 if ( (s = strchr( str, '=' )) == NULL ) {
726 ftype = LDAP_FILTER_LE;
728 if(! ldap_is_attr_desc( str ) ) goto done;
732 ftype = LDAP_FILTER_GE;
734 if(! ldap_is_attr_desc( str ) ) goto done;
738 ftype = LDAP_FILTER_APPROX;
740 if(! ldap_is_attr_desc( str ) ) goto done;
744 /* RFC2254 extensible filters are off the form:
745 * type [:dn] [:rule] := value
746 * or [:dn]:rule := value
748 ftype = LDAP_FILTER_EXT;
752 char *dn = strchr( str, ':' );
756 if(! ldap_is_attr_desc( str ) ) goto done;
761 rule = strchr( dn, ':' );
765 if ( strcmp(dn, "dn") == 0 ) {
766 /* must have attribute */
767 if( !ldap_is_attr_desc( str ) ) {
782 if ( strcmp(dn, "dn") != 0 ) {
788 if ( *str == '\0' && *rule == '\0' ) {
789 /* must have either type or rule */
793 if ( *str != '\0' && !ldap_is_attr_desc( str ) ) {
797 if ( *rule != '\0' && !ldap_is_attr_oid( rule ) ) {
801 rc = ber_printf( ber, "t{" /*}*/, ftype );
803 if( rc != -1 && *rule != '\0' ) {
804 rc = ber_printf( ber, "ts", LDAP_FILTER_EXT_OID, rule );
806 if( rc != -1 && *str != '\0' ) {
807 rc = ber_printf( ber, "ts", LDAP_FILTER_EXT_TYPE, str );
811 ber_slen_t len = ldap_pvt_filter_value_unescape( value );
814 rc = ber_printf( ber, "totb}",
815 LDAP_FILTER_EXT_VALUE, value, len,
816 LDAP_FILTER_EXT_DNATTRS, dn != NULL);
825 if ( ldap_pvt_find_wildcard( value ) == NULL ) {
826 ftype = LDAP_FILTER_EQUALITY;
827 } else if ( strcmp( value, "*" ) == 0 ) {
828 ftype = LDAP_FILTER_PRESENT;
830 rc = put_substring_filter( ber, str, value );
836 if ( ftype == LDAP_FILTER_PRESENT ) {
837 rc = ber_printf( ber, "ts", ftype, str );
840 ber_slen_t len = ldap_pvt_filter_value_unescape( value );
843 rc = ber_printf( ber, "t{so}",
844 ftype, str, value, len );
848 if( rc != -1 ) rc = 0;
856 put_substring_filter( BerElement *ber, char *type, char *val )
858 char *nextstar, gotstar = 0;
859 ber_tag_t ftype = LDAP_FILTER_SUBSTRINGS;
861 Debug( LDAP_DEBUG_TRACE, "put_substring_filter \"%s=%s\"\n", type,
864 if ( ber_printf( ber, "t{s{", ftype, type ) == -1 )
867 for( ; val != NULL; val=nextstar ) {
868 if ( (nextstar = ldap_pvt_find_wildcard( val )) != NULL )
871 if ( gotstar == 0 ) {
872 ftype = LDAP_SUBSTRING_INITIAL;
873 } else if ( nextstar == NULL ) {
874 ftype = LDAP_SUBSTRING_FINAL;
876 ftype = LDAP_SUBSTRING_ANY;
879 if ( *val != '\0' ) {
880 ber_slen_t len = ldap_pvt_filter_value_unescape( val );
886 if ( ber_printf( ber, "to", ftype, val, len ) == -1 ) {
894 if ( ber_printf( ber, /* {{ */ "}}" ) == -1 )
902 LDAP *ld, LDAP_CONST char *base, int scope,
903 LDAP_CONST char *filter, char **attrs,
904 int attrsonly, struct timeval *timeout, LDAPMessage **res )
908 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
910 return( ld->ld_errno );
912 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 )
913 return( ld->ld_errno );
915 if ( ld->ld_errno == LDAP_TIMEOUT ) {
916 (void) ldap_abandon( ld, msgid );
917 ld->ld_errno = LDAP_TIMEOUT;
918 return( ld->ld_errno );
921 return( ldap_result2error( ld, *res, 0 ) );
927 LDAP_CONST char *base,
929 LDAP_CONST char *filter,
936 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
938 return( ld->ld_errno );
940 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, res ) == -1 )
941 return( ld->ld_errno );
943 return( ldap_result2error( ld, *res, 0 ) );