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, use only tv_sec as timelimit.
102 * otherwise, use default.
104 timelimit = (timeout != NULL)
108 ber = ldap_build_search_req( ld, base, scope, filter, attrs,
109 attrsonly, sctrls, cctrls, timelimit, sizelimit );
116 if ( ld->ld_cache != NULL ) {
117 if ( ldap_check_cache( ld, LDAP_REQ_SEARCH, ber ) == 0 ) {
119 ld->ld_errno = LDAP_SUCCESS;
120 *msgidp = ld->ld_msgid;
123 ldap_add_request_to_cache( ld, LDAP_REQ_SEARCH, ber );
125 #endif /* LDAP_NOCACHE */
127 /* send the message */
128 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber );
139 LDAP_CONST char *base,
141 LDAP_CONST char *filter,
144 LDAPControl **sctrls,
145 LDAPControl **cctrls,
146 struct timeval *timeout,
153 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
154 sctrls, cctrls, timeout, sizelimit, &msgid );
156 if ( rc != LDAP_SUCCESS ) {
160 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 )
161 return( ld->ld_errno );
163 return( ldap_result2error( ld, *res, 0 ) );
167 * ldap_search - initiate an ldap search operation.
172 * base DN of the base object
173 * scope the search scope - one of LDAP_SCOPE_BASE,
174 * LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE
175 * filter a string containing the search filter
176 * (e.g., "(|(cn=bob)(sn=bob))")
177 * attrs list of attribute types to return for matches
178 * attrsonly 1 => attributes only 0 => attributes and values
181 * char *attrs[] = { "mail", "title", 0 };
182 * msgid = ldap_search( ld, "c=us@o=UM", LDAP_SCOPE_SUBTREE, "cn~=bob",
183 * attrs, attrsonly );
187 LDAP *ld, LDAP_CONST char *base, int scope, LDAP_CONST char *filter,
188 char **attrs, int attrsonly )
192 Debug( LDAP_DEBUG_TRACE, "ldap_search\n", 0, 0, 0 );
194 ber = ldap_build_search_req( ld, base, scope, filter, attrs,
195 attrsonly, NULL, NULL, -1, -1 );
202 if ( ld->ld_cache != NULL ) {
203 if ( ldap_check_cache( ld, LDAP_REQ_SEARCH, ber ) == 0 ) {
205 ld->ld_errno = LDAP_SUCCESS;
206 return( ld->ld_msgid );
208 ldap_add_request_to_cache( ld, LDAP_REQ_SEARCH, ber );
210 #endif /* LDAP_NOCACHE */
212 /* send the message */
213 return ( ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber ));
218 ldap_build_search_req(
220 LDAP_CONST char *base,
222 LDAP_CONST char *filter_in,
225 LDAPControl **sctrls,
226 LDAPControl **cctrls,
228 ber_int_t sizelimit )
235 * Create the search request. It looks like this:
236 * SearchRequest := [APPLICATION 3] SEQUENCE {
237 * baseObject DistinguishedName,
243 * derefAliases ENUMERATED {
244 * neverDerefaliases (0),
245 * derefInSearching (1),
246 * derefFindingBaseObj (2),
247 * alwaysDerefAliases (3)
249 * sizelimit INTEGER (0 .. 65535),
250 * timelimit INTEGER (0 .. 65535),
253 * attributes SEQUENCE OF AttributeType
255 * wrapped in an ldap message.
258 /* create a message to send */
259 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
263 if ( base == NULL ) {
264 /* no base provided, use session default base */
265 base = ld->ld_options.ldo_defbase;
267 if ( base == NULL ) {
268 /* no session default base, use top */
273 #ifdef LDAP_CONNECTIONLESS
274 if ( ld->ld_cldapnaddr > 0 ) {
275 err = ber_printf( ber, "{ist{seeiib", ++ld->ld_msgid,
276 ld->ld_cldapdn, LDAP_REQ_SEARCH, base, scope, ld->ld_deref,
277 (sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
278 (timelimit < 0) ? ld->ld_timelimit : timelimit,
281 #endif /* LDAP_CONNECTIONLESS */
282 err = ber_printf( ber, "{it{seeiib", ++ld->ld_msgid,
283 LDAP_REQ_SEARCH, base, (ber_int_t) scope, ld->ld_deref,
284 (sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
285 (timelimit < 0) ? ld->ld_timelimit : timelimit,
287 #ifdef LDAP_CONNECTIONLESS
289 #endif /* LDAP_CONNECTIONLESS */
292 ld->ld_errno = LDAP_ENCODING_ERROR;
297 filter = LDAP_STRDUP( filter_in );
298 err = put_filter( ber, filter );
302 ld->ld_errno = LDAP_FILTER_ERROR;
307 if ( ber_printf( ber, /*{*/ "{v}}", attrs ) == -1 ) {
308 ld->ld_errno = LDAP_ENCODING_ERROR;
313 /* Put Server Controls */
314 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
319 if ( ber_printf( ber, /*{*/ "}", attrs ) == -1 ) {
320 ld->ld_errno = LDAP_ENCODING_ERROR;
328 static int ldap_is_attr_oid ( const char *attr )
332 for( i = 0; (c = attr[i]) != 0; i++ ) {
333 if( c >= '0' && c <= '9' ) {
336 } else if ( c != '.' ) {
337 /* not digit nor '.' */
340 } else if ( !digit ) {
341 /* '.' but prev not digit */
354 static int ldap_is_attr_desc ( const char *attr )
356 /* cheap attribute description check */
359 for( i = 0; (c = attr[i]) != 0; i++ ) {
360 if (( c >= '0' && c <= '9' )
361 || ( c >= 'A' && c <= 'Z' )
362 || ( c >= 'a' && c <= 'z' )
363 || ( c == '.' || c == '-' )
364 || ( c == ';' )) continue;
373 find_right_paren( char *s )
379 while ( *s && balance ) {
383 else if ( *s == ')' )
386 if ( *s == '\\' && ! escape )
394 return( *s ? s : NULL );
397 static int hex2value( int c )
399 if( c >= '0' && c <= '9' ) {
403 if( c >= 'A' && c <= 'F' ) {
404 return c + (10 - (int) 'A');
407 if( c >= 'a' && c <= 'f' ) {
408 return c + (10 - (int) 'a');
415 ldap_pvt_find_wildcard( char *s )
417 for( ; *s != '\0' ; s++ ) {
419 case '*': /* found wildcard */
423 s++; /* skip over escape */
425 return NULL; /* escape at end of string */
432 /* unescape filter value */
433 /* support both LDAP v2 and v3 escapes */
434 /* output can include nul characters */
436 ldap_pvt_filter_value_unescape( char *fval )
441 for( r=v=0; fval[v] != '\0'; v++ ) {
447 if ( fval[v] == '\0' ) {
448 /* escape at end of string */
453 if (( v1 = hex2value( fval[v] )) >= 0 ) {
456 if (( v2 = hex2value( fval[v+1] )) < 0 ) {
457 /* must be two digit code */
461 fval[r++] = v1 * 16 + v2;
481 put_complex_filter( BerElement *ber, char *str, ber_tag_t tag, int not )
486 * We have (x(filter)...) with str sitting on
487 * the x. We have to find the paren matching
488 * the one before the x and put the intervening
489 * filters by calling put_filter_list().
492 /* put explicit tag */
493 if ( ber_printf( ber, "t{" /*}*/, tag ) == -1 )
497 if ( (next = find_right_paren( str )) == NULL )
501 if ( put_filter_list( ber, str ) == -1 )
505 /* flush explicit tagged thang */
506 if ( ber_printf( ber, /*{*/ "}" ) == -1 )
513 put_filter( BerElement *ber, char *str )
516 int parens, balance, escape;
519 * A Filter looks like this:
520 * Filter ::= CHOICE {
521 * and [0] SET OF Filter,
522 * or [1] SET OF Filter,
524 * equalityMatch [3] AttributeValueAssertion,
525 * substrings [4] SubstringFilter,
526 * greaterOrEqual [5] AttributeValueAssertion,
527 * lessOrEqual [6] AttributeValueAssertion,
528 * present [7] AttributeType,
529 * approxMatch [8] AttributeValueAssertion,
530 * extensibleMatch [9] MatchingRuleAssertion -- LDAPv3
533 * SubstringFilter ::= SEQUENCE {
534 * type AttributeType,
535 * SEQUENCE OF CHOICE {
536 * initial [0] IA5String,
538 * final [2] IA5String
542 * MatchingRuleAssertion ::= SEQUENCE { -- LDAPv3
543 * matchingRule [1] MatchingRuleId OPTIONAL,
544 * type [2] AttributeDescription OPTIONAL,
545 * matchValue [3] AssertionValue,
546 * dnAttributes [4] BOOLEAN DEFAULT FALSE }
548 * Note: tags in a choice are always explicit
551 Debug( LDAP_DEBUG_TRACE, "put_filter \"%s\"\n", str, 0, 0 );
561 Debug( LDAP_DEBUG_TRACE, "put_filter: AND\n",
564 if ( (str = put_complex_filter( ber, str,
565 LDAP_FILTER_AND, 0 )) == NULL )
572 Debug( LDAP_DEBUG_TRACE, "put_filter: OR\n",
575 if ( (str = put_complex_filter( ber, str,
576 LDAP_FILTER_OR, 0 )) == NULL )
583 Debug( LDAP_DEBUG_TRACE, "put_filter: NOT\n",
586 if ( (str = put_complex_filter( ber, str,
587 LDAP_FILTER_NOT, 1 )) == NULL )
594 Debug( LDAP_DEBUG_TRACE, "put_filter: simple\n",
600 while ( *next && balance ) {
604 else if ( *next == ')' )
607 if ( *next == '\\' && ! escape )
618 if ( put_simple_filter( ber, str ) == -1 ) {
629 Debug( LDAP_DEBUG_TRACE, "put_filter: end\n", 0, 0,
631 if ( ber_printf( ber, /*[*/ "]" ) == -1 )
641 default: /* assume it's a simple type=value filter */
642 Debug( LDAP_DEBUG_TRACE, "put_filter: default\n", 0, 0,
644 next = strchr( str, '\0' );
645 if ( put_simple_filter( ber, str ) == -1 ) {
653 return( parens ? -1 : 0 );
657 * Put a list of filters like this "(filter1)(filter2)..."
661 put_filter_list( BerElement *ber, char *str )
666 Debug( LDAP_DEBUG_TRACE, "put_filter_list \"%s\"\n", str, 0, 0 );
669 while ( *str && isspace( (unsigned char) *str ) )
674 if ( (next = find_right_paren( str + 1 )) == NULL )
678 /* now we have "(filter)" with str pointing to it */
680 if ( put_filter( ber, str ) == -1 )
700 Debug( LDAP_DEBUG_TRACE, "put_simple_filter \"%s\"\n", str, 0, 0 );
702 str = LDAP_STRDUP( str );
703 if( str == NULL ) return -1;
705 if ( (s = strchr( str, '=' )) == NULL ) {
714 ftype = LDAP_FILTER_LE;
716 if(! ldap_is_attr_desc( str ) ) goto done;
720 ftype = LDAP_FILTER_GE;
722 if(! ldap_is_attr_desc( str ) ) goto done;
726 ftype = LDAP_FILTER_APPROX;
728 if(! ldap_is_attr_desc( str ) ) goto done;
732 /* RFC2254 extensible filters are off the form:
733 * type [:dn] [:rule] := value
734 * or [:dn]:rule := value
736 ftype = LDAP_FILTER_EXT;
740 char *dn = strchr( str, ':' );
744 if(! ldap_is_attr_desc( str ) ) goto done;
749 rule = strchr( dn, ':' );
753 if ( strcmp(dn, "dn") == 0 ) {
754 /* must have attribute */
755 if( !ldap_is_attr_desc( str ) ) {
770 if ( strcmp(dn, "dn") != 0 ) {
776 if ( *str == '\0' && *rule == '\0' ) {
777 /* must have either type or rule */
781 if ( *str != '\0' && !ldap_is_attr_desc( str ) ) {
785 if ( *rule != '\0' && !ldap_is_attr_oid( rule ) ) {
789 rc = ber_printf( ber, "t{" /*}*/, ftype );
791 if( rc != -1 && *rule != '\0' ) {
792 rc = ber_printf( ber, "ts", LDAP_FILTER_EXT_OID, rule );
794 if( rc != -1 && *str != '\0' ) {
795 rc = ber_printf( ber, "ts", LDAP_FILTER_EXT_TYPE, str );
799 ber_slen_t len = ldap_pvt_filter_value_unescape( value );
802 rc = ber_printf( ber, "totb}",
803 LDAP_FILTER_EXT_VALUE, value, len,
804 LDAP_FILTER_EXT_DNATTRS, dn != NULL);
813 if ( ldap_pvt_find_wildcard( value ) == NULL ) {
814 ftype = LDAP_FILTER_EQUALITY;
815 } else if ( strcmp( value, "*" ) == 0 ) {
816 ftype = LDAP_FILTER_PRESENT;
818 rc = put_substring_filter( ber, str, value );
824 if ( ftype == LDAP_FILTER_PRESENT ) {
825 rc = ber_printf( ber, "ts", ftype, str );
828 ber_slen_t len = ldap_pvt_filter_value_unescape( value );
831 rc = ber_printf( ber, "t{so}",
832 ftype, str, value, len );
836 if( rc != -1 ) rc = 0;
844 put_substring_filter( BerElement *ber, char *type, char *val )
846 char *nextstar, gotstar = 0;
847 ber_tag_t ftype = LDAP_FILTER_SUBSTRINGS;
849 Debug( LDAP_DEBUG_TRACE, "put_substring_filter \"%s=%s\"\n", type,
852 if ( ber_printf( ber, "t{s{", ftype, type ) == -1 )
855 for( ; val != NULL; val=nextstar ) {
856 if ( (nextstar = ldap_pvt_find_wildcard( val )) != NULL )
859 if ( gotstar == 0 ) {
860 ftype = LDAP_SUBSTRING_INITIAL;
861 } else if ( nextstar == NULL ) {
862 ftype = LDAP_SUBSTRING_FINAL;
864 ftype = LDAP_SUBSTRING_ANY;
867 if ( *val != '\0' ) {
868 ber_slen_t len = ldap_pvt_filter_value_unescape( val );
874 if ( ber_printf( ber, "to", ftype, val, len ) == -1 ) {
882 if ( ber_printf( ber, /* {{ */ "}}" ) == -1 )
890 LDAP *ld, LDAP_CONST char *base, int scope,
891 LDAP_CONST char *filter, char **attrs,
892 int attrsonly, struct timeval *timeout, LDAPMessage **res )
896 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
898 return( ld->ld_errno );
900 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 )
901 return( ld->ld_errno );
903 if ( ld->ld_errno == LDAP_TIMEOUT ) {
904 (void) ldap_abandon( ld, msgid );
905 ld->ld_errno = LDAP_TIMEOUT;
906 return( ld->ld_errno );
909 return( ldap_result2error( ld, *res, 0 ) );
915 LDAP_CONST char *base,
917 LDAP_CONST char *filter,
924 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
926 return( ld->ld_errno );
928 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, res ) == -1 )
929 return( ld->ld_errno );
931 return( ldap_result2error( ld, *res, 0 ) );