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 #ifdef LDAP_CONNECTIONLESS
301 if ( LDAP_IS_UDP(ld) ) {
302 err = ber_write( ber, ld->ld_options.ldo_peer,
303 sizeof(struct sockaddr), 0);
304 if (err == sizeof(struct sockaddr)) {
305 char *dn = ld->ld_options.ldo_cldapdn;
307 err = ber_printf( ber, "{ist{seeiib", ++ld->ld_msgid, dn,
308 LDAP_REQ_SEARCH, base, (ber_int_t) scope, ld->ld_deref,
309 (sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
310 (timelimit < 0) ? ld->ld_timelimit : timelimit,
316 err = ber_printf( ber, "{it{seeiib", ++ld->ld_msgid,
317 LDAP_REQ_SEARCH, base, (ber_int_t) scope, ld->ld_deref,
318 (sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
319 (timelimit < 0) ? ld->ld_timelimit : timelimit,
324 ld->ld_errno = LDAP_ENCODING_ERROR;
329 if( filter_in != NULL ) {
330 filter = LDAP_STRDUP( filter_in );
332 filter = LDAP_STRDUP( "(objectclass=*)" );
334 err = put_filter( ber, filter );
338 ld->ld_errno = LDAP_FILTER_ERROR;
343 if ( ber_printf( ber, /*{*/ "{v}N}", attrs ) == -1 ) {
344 ld->ld_errno = LDAP_ENCODING_ERROR;
349 /* Put Server Controls */
350 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
355 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
356 ld->ld_errno = LDAP_ENCODING_ERROR;
364 static int ldap_is_attr_oid ( const char *attr )
368 for( i = 0; (c = attr[i]) != 0; i++ ) {
369 if( c >= '0' && c <= '9' ) {
372 } else if ( c != '.' ) {
373 /* not digit nor '.' */
376 } else if ( !digit ) {
377 /* '.' but prev not digit */
389 static int ldap_is_attr_desc ( const char *attr )
391 /* cheap attribute description check */
394 for( i = 0; (c = attr[i]) != 0; i++ ) {
395 if (( c >= '0' && c <= '9' )
396 || ( c >= 'A' && c <= 'Z' )
397 || ( c >= 'a' && c <= 'z' )
398 || ( c == '.' || c == '-' )
399 || ( c == ';' )) continue;
408 find_right_paren( char *s )
414 while ( *s && balance ) {
418 else if ( *s == ')' )
421 if ( *s == '\\' && ! escape )
429 return( *s ? s : NULL );
432 static int hex2value( int c )
434 if( c >= '0' && c <= '9' ) {
438 if( c >= 'A' && c <= 'F' ) {
439 return c + (10 - (int) 'A');
442 if( c >= 'a' && c <= 'f' ) {
443 return c + (10 - (int) 'a');
450 ldap_pvt_find_wildcard( const char *s )
452 for( ; *s != '\0' ; s++ ) {
454 case '*': /* found wildcard */
458 s++; /* skip over escape */
460 return NULL; /* escape at end of string */
467 /* unescape filter value */
468 /* support both LDAP v2 and v3 escapes */
469 /* output can include nul characters */
471 ldap_pvt_filter_value_unescape( char *fval )
476 for( r=v=0; fval[v] != '\0'; v++ ) {
482 if ( fval[v] == '\0' ) {
483 /* escape at end of string */
488 if (( v1 = hex2value( fval[v] )) >= 0 ) {
491 if (( v2 = hex2value( fval[v+1] )) < 0 ) {
492 /* must be two digit code */
496 fval[r++] = v1 * 16 + v2;
516 put_complex_filter( BerElement *ber, char *str, ber_tag_t tag, int not )
521 * We have (x(filter)...) with str sitting on
522 * the x. We have to find the paren matching
523 * the one before the x and put the intervening
524 * filters by calling put_filter_list().
527 /* put explicit tag */
528 if ( ber_printf( ber, "t{" /*}*/, tag ) == -1 )
532 if ( (next = find_right_paren( str )) == NULL )
536 if ( put_filter_list( ber, str ) == -1 )
540 /* flush explicit tagged thang */
541 if ( ber_printf( ber, /*{*/ "N}" ) == -1 )
548 put_filter( BerElement *ber, char *str )
551 int parens, balance, escape;
554 * A Filter looks like this:
555 * Filter ::= CHOICE {
556 * and [0] SET OF Filter,
557 * or [1] SET OF Filter,
559 * equalityMatch [3] AttributeValueAssertion,
560 * substrings [4] SubstringFilter,
561 * greaterOrEqual [5] AttributeValueAssertion,
562 * lessOrEqual [6] AttributeValueAssertion,
563 * present [7] AttributeType,
564 * approxMatch [8] AttributeValueAssertion,
565 * extensibleMatch [9] MatchingRuleAssertion -- LDAPv3
568 * SubstringFilter ::= SEQUENCE {
569 * type AttributeType,
570 * SEQUENCE OF CHOICE {
571 * initial [0] IA5String,
573 * final [2] IA5String
577 * MatchingRuleAssertion ::= SEQUENCE { -- LDAPv3
578 * matchingRule [1] MatchingRuleId OPTIONAL,
579 * type [2] AttributeDescription OPTIONAL,
580 * matchValue [3] AssertionValue,
581 * dnAttributes [4] BOOLEAN DEFAULT FALSE }
583 * Note: tags in a choice are always explicit
586 Debug( LDAP_DEBUG_TRACE, "put_filter \"%s\"\n", str, 0, 0 );
596 while( LDAP_SPACE( *str ) ) str++;
600 Debug( LDAP_DEBUG_TRACE, "put_filter: AND\n",
603 if ( (str = put_complex_filter( ber, str,
604 LDAP_FILTER_AND, 0 )) == NULL )
611 Debug( LDAP_DEBUG_TRACE, "put_filter: OR\n",
614 if ( (str = put_complex_filter( ber, str,
615 LDAP_FILTER_OR, 0 )) == NULL )
622 Debug( LDAP_DEBUG_TRACE, "put_filter: NOT\n",
625 if ( (str = put_complex_filter( ber, str,
626 LDAP_FILTER_NOT, 1 )) == NULL )
633 Debug( LDAP_DEBUG_TRACE, "put_filter: simple\n",
639 while ( *next && balance ) {
643 else if ( *next == ')' )
646 if ( *next == '\\' && ! escape )
657 if ( put_simple_filter( ber, str ) == -1 ) {
668 Debug( LDAP_DEBUG_TRACE, "put_filter: end\n", 0, 0,
670 if ( ber_printf( ber, /*[*/ "]" ) == -1 )
680 default: /* assume it's a simple type=value filter */
681 Debug( LDAP_DEBUG_TRACE, "put_filter: default\n", 0, 0,
683 next = strchr( str, '\0' );
684 if ( put_simple_filter( ber, str ) == -1 ) {
692 return( parens ? -1 : 0 );
696 * Put a list of filters like this "(filter1)(filter2)..."
700 put_filter_list( BerElement *ber, char *str )
705 Debug( LDAP_DEBUG_TRACE, "put_filter_list \"%s\"\n", str, 0, 0 );
708 while ( *str && LDAP_SPACE( (unsigned char) *str ) )
713 if ( (next = find_right_paren( str + 1 )) == NULL )
717 /* now we have "(filter)" with str pointing to it */
719 if ( put_filter( ber, str ) == -1 )
739 Debug( LDAP_DEBUG_TRACE, "put_simple_filter \"%s\"\n", str, 0, 0 );
741 str = LDAP_STRDUP( str );
742 if( str == NULL ) return -1;
744 if ( (s = strchr( str, '=' )) == NULL ) {
753 ftype = LDAP_FILTER_LE;
755 if(! ldap_is_attr_desc( str ) ) goto done;
759 ftype = LDAP_FILTER_GE;
761 if(! ldap_is_attr_desc( str ) ) goto done;
765 ftype = LDAP_FILTER_APPROX;
767 if(! ldap_is_attr_desc( str ) ) goto done;
771 /* RFC2254 extensible filters are off the form:
772 * type [:dn] [:rule] := value
773 * or [:dn]:rule := value
775 ftype = LDAP_FILTER_EXT;
779 char *dn = strchr( str, ':' );
783 if(! ldap_is_attr_desc( str ) ) goto done;
787 rule = strchr( dn, ':' );
791 if ( strcmp(dn, "dn") == 0 ) {
792 /* must have attribute */
793 if( !ldap_is_attr_desc( str ) ) {
808 if ( strcmp(dn, "dn") != 0 ) {
816 if ( *str == '\0' && ( !rule || *rule == '\0' ) ) {
817 /* must have either type or rule */
821 if ( *str != '\0' && !ldap_is_attr_desc( str ) ) {
825 if ( rule && *rule != '\0' && !ldap_is_attr_oid( rule ) ) {
829 rc = ber_printf( ber, "t{" /*}*/, ftype );
831 if( rc != -1 && rule && *rule != '\0' ) {
832 rc = ber_printf( ber, "ts", LDAP_FILTER_EXT_OID, rule );
834 if( rc != -1 && *str != '\0' ) {
835 rc = ber_printf( ber, "ts", LDAP_FILTER_EXT_TYPE, str );
839 ber_slen_t len = ldap_pvt_filter_value_unescape( value );
842 rc = ber_printf( ber, "totbN}",
843 LDAP_FILTER_EXT_VALUE, value, len,
844 LDAP_FILTER_EXT_DNATTRS, dn != NULL);
853 if ( ldap_pvt_find_wildcard( value ) == NULL ) {
854 ftype = LDAP_FILTER_EQUALITY;
855 } else if ( strcmp( value, "*" ) == 0 ) {
856 ftype = LDAP_FILTER_PRESENT;
858 rc = put_substring_filter( ber, str, value );
864 if ( ftype == LDAP_FILTER_PRESENT ) {
865 rc = ber_printf( ber, "ts", ftype, str );
868 ber_slen_t len = ldap_pvt_filter_value_unescape( value );
871 rc = ber_printf( ber, "t{soN}",
872 ftype, str, value, len );
876 if( rc != -1 ) rc = 0;
884 put_substring_filter( BerElement *ber, char *type, char *val )
886 char *nextstar, gotstar = 0;
887 ber_tag_t ftype = LDAP_FILTER_SUBSTRINGS;
889 Debug( LDAP_DEBUG_TRACE, "put_substring_filter \"%s=%s\"\n", type,
892 if ( ber_printf( ber, "t{s{", ftype, type ) == -1 )
895 for( ; val != NULL; val=nextstar ) {
896 if ( (nextstar = ldap_pvt_find_wildcard( val )) != NULL )
899 if ( gotstar == 0 ) {
900 ftype = LDAP_SUBSTRING_INITIAL;
901 } else if ( nextstar == NULL ) {
902 ftype = LDAP_SUBSTRING_FINAL;
904 ftype = LDAP_SUBSTRING_ANY;
907 if ( *val != '\0' ) {
908 ber_slen_t len = ldap_pvt_filter_value_unescape( val );
914 if ( ber_printf( ber, "to", ftype, val, len ) == -1 ) {
922 if ( ber_printf( ber, /* {{ */ "N}N}" ) == -1 )
930 LDAP *ld, LDAP_CONST char *base, int scope,
931 LDAP_CONST char *filter, char **attrs,
932 int attrsonly, struct timeval *timeout, LDAPMessage **res )
936 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
938 return( ld->ld_errno );
940 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 )
941 return( ld->ld_errno );
943 if ( ld->ld_errno == LDAP_TIMEOUT ) {
944 (void) ldap_abandon( ld, msgid );
945 ld->ld_errno = LDAP_TIMEOUT;
946 return( ld->ld_errno );
949 return( ldap_result2error( ld, *res, 0 ) );
955 LDAP_CONST char *base,
957 LDAP_CONST char *filter,
964 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
966 return( ld->ld_errno );
968 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, res ) == -1 )
969 return( ld->ld_errno );
971 return( ldap_result2error( ld, *res, 0 ) );