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>
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 if( filter_in != NULL ) {
311 filter = LDAP_STRDUP( filter_in );
313 filter = LDAP_STRDUP( "(objectclass=*)" );
315 err = put_filter( ber, filter );
319 ld->ld_errno = LDAP_FILTER_ERROR;
324 if ( ber_printf( ber, /*{*/ "{v}}", attrs ) == -1 ) {
325 ld->ld_errno = LDAP_ENCODING_ERROR;
330 /* Put Server Controls */
331 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
336 if ( ber_printf( ber, /*{*/ "}", attrs ) == -1 ) {
337 ld->ld_errno = LDAP_ENCODING_ERROR;
345 static int ldap_is_attr_oid ( const char *attr )
349 for( i = 0; (c = attr[i]) != 0; i++ ) {
350 if( c >= '0' && c <= '9' ) {
353 } else if ( c != '.' ) {
354 /* not digit nor '.' */
357 } else if ( !digit ) {
358 /* '.' but prev not digit */
370 static int ldap_is_attr_desc ( const char *attr )
372 /* cheap attribute description check */
375 for( i = 0; (c = attr[i]) != 0; i++ ) {
376 if (( c >= '0' && c <= '9' )
377 || ( c >= 'A' && c <= 'Z' )
378 || ( c >= 'a' && c <= 'z' )
379 || ( c == '.' || c == '-' )
380 || ( c == ';' )) continue;
389 find_right_paren( char *s )
395 while ( *s && balance ) {
399 else if ( *s == ')' )
402 if ( *s == '\\' && ! escape )
410 return( *s ? s : NULL );
413 static int hex2value( int c )
415 if( c >= '0' && c <= '9' ) {
419 if( c >= 'A' && c <= 'F' ) {
420 return c + (10 - (int) 'A');
423 if( c >= 'a' && c <= 'f' ) {
424 return c + (10 - (int) 'a');
431 ldap_pvt_find_wildcard( const char *s )
433 for( ; *s != '\0' ; s++ ) {
435 case '*': /* found wildcard */
439 s++; /* skip over escape */
441 return NULL; /* escape at end of string */
448 /* unescape filter value */
449 /* support both LDAP v2 and v3 escapes */
450 /* output can include nul characters */
452 ldap_pvt_filter_value_unescape( char *fval )
457 for( r=v=0; fval[v] != '\0'; v++ ) {
463 if ( fval[v] == '\0' ) {
464 /* escape at end of string */
469 if (( v1 = hex2value( fval[v] )) >= 0 ) {
472 if (( v2 = hex2value( fval[v+1] )) < 0 ) {
473 /* must be two digit code */
477 fval[r++] = v1 * 16 + v2;
497 put_complex_filter( BerElement *ber, char *str, ber_tag_t tag, int not )
502 * We have (x(filter)...) with str sitting on
503 * the x. We have to find the paren matching
504 * the one before the x and put the intervening
505 * filters by calling put_filter_list().
508 /* put explicit tag */
509 if ( ber_printf( ber, "t{" /*}*/, tag ) == -1 )
513 if ( (next = find_right_paren( str )) == NULL )
517 if ( put_filter_list( ber, str ) == -1 )
521 /* flush explicit tagged thang */
522 if ( ber_printf( ber, /*{*/ "}" ) == -1 )
529 put_filter( BerElement *ber, char *str )
532 int parens, balance, escape;
535 * A Filter looks like this:
536 * Filter ::= CHOICE {
537 * and [0] SET OF Filter,
538 * or [1] SET OF Filter,
540 * equalityMatch [3] AttributeValueAssertion,
541 * substrings [4] SubstringFilter,
542 * greaterOrEqual [5] AttributeValueAssertion,
543 * lessOrEqual [6] AttributeValueAssertion,
544 * present [7] AttributeType,
545 * approxMatch [8] AttributeValueAssertion,
546 * extensibleMatch [9] MatchingRuleAssertion -- LDAPv3
549 * SubstringFilter ::= SEQUENCE {
550 * type AttributeType,
551 * SEQUENCE OF CHOICE {
552 * initial [0] IA5String,
554 * final [2] IA5String
558 * MatchingRuleAssertion ::= SEQUENCE { -- LDAPv3
559 * matchingRule [1] MatchingRuleId OPTIONAL,
560 * type [2] AttributeDescription OPTIONAL,
561 * matchValue [3] AssertionValue,
562 * dnAttributes [4] BOOLEAN DEFAULT FALSE }
564 * Note: tags in a choice are always explicit
567 Debug( LDAP_DEBUG_TRACE, "put_filter \"%s\"\n", str, 0, 0 );
577 while( isspace( *str ) ) str++;
581 Debug( LDAP_DEBUG_TRACE, "put_filter: AND\n",
584 if ( (str = put_complex_filter( ber, str,
585 LDAP_FILTER_AND, 0 )) == NULL )
592 Debug( LDAP_DEBUG_TRACE, "put_filter: OR\n",
595 if ( (str = put_complex_filter( ber, str,
596 LDAP_FILTER_OR, 0 )) == NULL )
603 Debug( LDAP_DEBUG_TRACE, "put_filter: NOT\n",
606 if ( (str = put_complex_filter( ber, str,
607 LDAP_FILTER_NOT, 1 )) == NULL )
614 Debug( LDAP_DEBUG_TRACE, "put_filter: simple\n",
620 while ( *next && balance ) {
624 else if ( *next == ')' )
627 if ( *next == '\\' && ! escape )
638 if ( put_simple_filter( ber, str ) == -1 ) {
649 Debug( LDAP_DEBUG_TRACE, "put_filter: end\n", 0, 0,
651 if ( ber_printf( ber, /*[*/ "]" ) == -1 )
661 default: /* assume it's a simple type=value filter */
662 Debug( LDAP_DEBUG_TRACE, "put_filter: default\n", 0, 0,
664 next = strchr( str, '\0' );
665 if ( put_simple_filter( ber, str ) == -1 ) {
673 return( parens ? -1 : 0 );
677 * Put a list of filters like this "(filter1)(filter2)..."
681 put_filter_list( BerElement *ber, char *str )
686 Debug( LDAP_DEBUG_TRACE, "put_filter_list \"%s\"\n", str, 0, 0 );
689 while ( *str && isspace( (unsigned char) *str ) )
694 if ( (next = find_right_paren( str + 1 )) == NULL )
698 /* now we have "(filter)" with str pointing to it */
700 if ( put_filter( ber, str ) == -1 )
720 Debug( LDAP_DEBUG_TRACE, "put_simple_filter \"%s\"\n", str, 0, 0 );
722 str = LDAP_STRDUP( str );
723 if( str == NULL ) return -1;
725 if ( (s = strchr( str, '=' )) == NULL ) {
734 ftype = LDAP_FILTER_LE;
736 if(! ldap_is_attr_desc( str ) ) goto done;
740 ftype = LDAP_FILTER_GE;
742 if(! ldap_is_attr_desc( str ) ) goto done;
746 ftype = LDAP_FILTER_APPROX;
748 if(! ldap_is_attr_desc( str ) ) goto done;
752 /* RFC2254 extensible filters are off the form:
753 * type [:dn] [:rule] := value
754 * or [:dn]:rule := value
756 ftype = LDAP_FILTER_EXT;
760 char *dn = strchr( str, ':' );
764 if(! ldap_is_attr_desc( str ) ) goto done;
769 rule = strchr( dn, ':' );
773 if ( strcmp(dn, "dn") == 0 ) {
774 /* must have attribute */
775 if( !ldap_is_attr_desc( str ) ) {
790 if ( strcmp(dn, "dn") != 0 ) {
796 if ( *str == '\0' && *rule == '\0' ) {
797 /* must have either type or rule */
801 if ( *str != '\0' && !ldap_is_attr_desc( str ) ) {
805 if ( *rule != '\0' && !ldap_is_attr_oid( rule ) ) {
809 rc = ber_printf( ber, "t{" /*}*/, ftype );
811 if( rc != -1 && *rule != '\0' ) {
812 rc = ber_printf( ber, "ts", LDAP_FILTER_EXT_OID, rule );
814 if( rc != -1 && *str != '\0' ) {
815 rc = ber_printf( ber, "ts", LDAP_FILTER_EXT_TYPE, str );
819 ber_slen_t len = ldap_pvt_filter_value_unescape( value );
822 rc = ber_printf( ber, "totb}",
823 LDAP_FILTER_EXT_VALUE, value, len,
824 LDAP_FILTER_EXT_DNATTRS, dn != NULL);
833 if ( ldap_pvt_find_wildcard( value ) == NULL ) {
834 ftype = LDAP_FILTER_EQUALITY;
835 } else if ( strcmp( value, "*" ) == 0 ) {
836 ftype = LDAP_FILTER_PRESENT;
838 rc = put_substring_filter( ber, str, value );
844 if ( ftype == LDAP_FILTER_PRESENT ) {
845 rc = ber_printf( ber, "ts", ftype, str );
848 ber_slen_t len = ldap_pvt_filter_value_unescape( value );
851 rc = ber_printf( ber, "t{so}",
852 ftype, str, value, len );
856 if( rc != -1 ) rc = 0;
864 put_substring_filter( BerElement *ber, char *type, char *val )
866 char *nextstar, gotstar = 0;
867 ber_tag_t ftype = LDAP_FILTER_SUBSTRINGS;
869 Debug( LDAP_DEBUG_TRACE, "put_substring_filter \"%s=%s\"\n", type,
872 if ( ber_printf( ber, "t{s{", ftype, type ) == -1 )
875 for( ; val != NULL; val=nextstar ) {
876 if ( (nextstar = ldap_pvt_find_wildcard( val )) != NULL )
879 if ( gotstar == 0 ) {
880 ftype = LDAP_SUBSTRING_INITIAL;
881 } else if ( nextstar == NULL ) {
882 ftype = LDAP_SUBSTRING_FINAL;
884 ftype = LDAP_SUBSTRING_ANY;
887 if ( *val != '\0' ) {
888 ber_slen_t len = ldap_pvt_filter_value_unescape( val );
894 if ( ber_printf( ber, "to", ftype, val, len ) == -1 ) {
902 if ( ber_printf( ber, /* {{ */ "}}" ) == -1 )
910 LDAP *ld, LDAP_CONST char *base, int scope,
911 LDAP_CONST char *filter, char **attrs,
912 int attrsonly, struct timeval *timeout, LDAPMessage **res )
916 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
918 return( ld->ld_errno );
920 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 )
921 return( ld->ld_errno );
923 if ( ld->ld_errno == LDAP_TIMEOUT ) {
924 (void) ldap_abandon( ld, msgid );
925 ld->ld_errno = LDAP_TIMEOUT;
926 return( ld->ld_errno );
929 return( ldap_result2error( ld, *res, 0 ) );
935 LDAP_CONST char *base,
937 LDAP_CONST char *filter,
944 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
946 return( ld->ld_errno );
948 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, res ) == -1 )
949 return( ld->ld_errno );
951 return( ldap_result2error( ld, *res, 0 ) );