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, "dc=example,dc=com", 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, "dc=example,dc=com", 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);
305 if ( LDAP_IS_UDP(ld) && ld->ld_options.ldo_version == LDAP_VERSION2) {
306 char *dn = ld->ld_options.ldo_cldapdn;
308 err = ber_printf( ber, "{ist{seeiib", ++ld->ld_msgid, dn,
309 LDAP_REQ_SEARCH, base, (ber_int_t) scope, ld->ld_deref,
310 (sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
311 (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 ) {
490 if (( v2 = hex2value( fval[v+1] )) < 0 ) {
491 /* must be two digit code */
495 fval[r++] = v1 * 16 + v2;
524 put_complex_filter( BerElement *ber, char *str, ber_tag_t tag, int not )
529 * We have (x(filter)...) with str sitting on
530 * the x. We have to find the paren matching
531 * the one before the x and put the intervening
532 * filters by calling put_filter_list().
535 /* put explicit tag */
536 if ( ber_printf( ber, "t{" /*}*/, tag ) == -1 )
540 if ( (next = find_right_paren( str )) == NULL )
544 if ( put_filter_list( ber, str ) == -1 )
548 /* flush explicit tagged thang */
549 if ( ber_printf( ber, /*{*/ "N}" ) == -1 )
556 put_filter( BerElement *ber, char *str )
559 int parens, balance, escape;
562 * A Filter looks like this:
563 * Filter ::= CHOICE {
564 * and [0] SET OF Filter,
565 * or [1] SET OF Filter,
567 * equalityMatch [3] AttributeValueAssertion,
568 * substrings [4] SubstringFilter,
569 * greaterOrEqual [5] AttributeValueAssertion,
570 * lessOrEqual [6] AttributeValueAssertion,
571 * present [7] AttributeType,
572 * approxMatch [8] AttributeValueAssertion,
573 * extensibleMatch [9] MatchingRuleAssertion -- LDAPv3
576 * SubstringFilter ::= SEQUENCE {
577 * type AttributeType,
578 * SEQUENCE OF CHOICE {
579 * initial [0] IA5String,
581 * final [2] IA5String
585 * MatchingRuleAssertion ::= SEQUENCE { -- LDAPv3
586 * matchingRule [1] MatchingRuleId OPTIONAL,
587 * type [2] AttributeDescription OPTIONAL,
588 * matchValue [3] AssertionValue,
589 * dnAttributes [4] BOOLEAN DEFAULT FALSE }
591 * Note: tags in a choice are always explicit
594 Debug( LDAP_DEBUG_TRACE, "put_filter \"%s\"\n", str, 0, 0 );
604 while( LDAP_SPACE( *str ) ) str++;
608 Debug( LDAP_DEBUG_TRACE, "put_filter: AND\n",
611 if ( (str = put_complex_filter( ber, str,
612 LDAP_FILTER_AND, 0 )) == NULL )
619 Debug( LDAP_DEBUG_TRACE, "put_filter: OR\n",
622 if ( (str = put_complex_filter( ber, str,
623 LDAP_FILTER_OR, 0 )) == NULL )
630 Debug( LDAP_DEBUG_TRACE, "put_filter: NOT\n",
633 if ( (str = put_complex_filter( ber, str,
634 LDAP_FILTER_NOT, 1 )) == NULL )
641 Debug( LDAP_DEBUG_TRACE, "put_filter: simple\n",
647 while ( *next && balance ) {
651 else if ( *next == ')' )
654 if ( *next == '\\' && ! escape )
665 if ( put_simple_filter( ber, str ) == -1 ) {
676 Debug( LDAP_DEBUG_TRACE, "put_filter: end\n", 0, 0,
678 if ( ber_printf( ber, /*[*/ "]" ) == -1 )
688 default: /* assume it's a simple type=value filter */
689 Debug( LDAP_DEBUG_TRACE, "put_filter: default\n", 0, 0,
691 next = strchr( str, '\0' );
692 if ( put_simple_filter( ber, str ) == -1 ) {
700 return( parens ? -1 : 0 );
704 * Put a list of filters like this "(filter1)(filter2)..."
708 put_filter_list( BerElement *ber, char *str )
713 Debug( LDAP_DEBUG_TRACE, "put_filter_list \"%s\"\n", str, 0, 0 );
716 while ( *str && LDAP_SPACE( (unsigned char) *str ) )
721 if ( (next = find_right_paren( str + 1 )) == NULL )
725 /* now we have "(filter)" with str pointing to it */
727 if ( put_filter( ber, str ) == -1 )
747 Debug( LDAP_DEBUG_TRACE, "put_simple_filter \"%s\"\n", str, 0, 0 );
749 str = LDAP_STRDUP( str );
750 if( str == NULL ) return -1;
752 if ( (s = strchr( str, '=' )) == NULL ) {
761 ftype = LDAP_FILTER_LE;
763 if(! ldap_is_attr_desc( str ) ) goto done;
767 ftype = LDAP_FILTER_GE;
769 if(! ldap_is_attr_desc( str ) ) goto done;
773 ftype = LDAP_FILTER_APPROX;
775 if(! ldap_is_attr_desc( str ) ) goto done;
779 /* RFC2254 extensible filters are off the form:
780 * type [:dn] [:rule] := value
781 * or [:dn]:rule := value
783 ftype = LDAP_FILTER_EXT;
787 char *dn = strchr( str, ':' );
791 if(! ldap_is_attr_desc( str ) ) goto done;
795 rule = strchr( dn, ':' );
799 if ( strcmp(dn, "dn") == 0 ) {
800 /* must have attribute */
801 if( !ldap_is_attr_desc( str ) ) {
816 if ( strcmp(dn, "dn") != 0 ) {
824 if ( *str == '\0' && ( !rule || *rule == '\0' ) ) {
825 /* must have either type or rule */
829 if ( *str != '\0' && !ldap_is_attr_desc( str ) ) {
833 if ( rule && *rule != '\0' && !ldap_is_attr_oid( rule ) ) {
837 rc = ber_printf( ber, "t{" /*}*/, ftype );
839 if( rc != -1 && rule && *rule != '\0' ) {
840 rc = ber_printf( ber, "ts", LDAP_FILTER_EXT_OID, rule );
842 if( rc != -1 && *str != '\0' ) {
843 rc = ber_printf( ber, "ts", LDAP_FILTER_EXT_TYPE, str );
847 ber_slen_t len = ldap_pvt_filter_value_unescape( value );
850 rc = ber_printf( ber, "totbN}",
851 LDAP_FILTER_EXT_VALUE, value, len,
852 LDAP_FILTER_EXT_DNATTRS, dn != NULL);
861 if ( ldap_pvt_find_wildcard( value ) == NULL ) {
862 ftype = LDAP_FILTER_EQUALITY;
863 } else if ( strcmp( value, "*" ) == 0 ) {
864 ftype = LDAP_FILTER_PRESENT;
866 rc = put_substring_filter( ber, str, value );
872 if ( ftype == LDAP_FILTER_PRESENT ) {
873 rc = ber_printf( ber, "ts", ftype, str );
876 ber_slen_t len = ldap_pvt_filter_value_unescape( value );
879 rc = ber_printf( ber, "t{soN}",
880 ftype, str, value, len );
884 if( rc != -1 ) rc = 0;
892 put_substring_filter( BerElement *ber, char *type, char *val )
894 char *nextstar, gotstar = 0;
895 ber_tag_t ftype = LDAP_FILTER_SUBSTRINGS;
897 Debug( LDAP_DEBUG_TRACE, "put_substring_filter \"%s=%s\"\n", type,
900 if ( ber_printf( ber, "t{s{", ftype, type ) == -1 )
903 for( ; val != NULL; val=nextstar ) {
904 if ( (nextstar = ldap_pvt_find_wildcard( val )) != NULL )
907 if ( gotstar == 0 ) {
908 ftype = LDAP_SUBSTRING_INITIAL;
909 } else if ( nextstar == NULL ) {
910 ftype = LDAP_SUBSTRING_FINAL;
912 ftype = LDAP_SUBSTRING_ANY;
915 if ( *val != '\0' ) {
916 ber_slen_t len = ldap_pvt_filter_value_unescape( val );
922 if ( ber_printf( ber, "to", ftype, val, len ) == -1 ) {
930 if ( ber_printf( ber, /* {{ */ "N}N}" ) == -1 )
938 LDAP *ld, LDAP_CONST char *base, int scope,
939 LDAP_CONST char *filter, char **attrs,
940 int attrsonly, struct timeval *timeout, LDAPMessage **res )
944 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
946 return( ld->ld_errno );
948 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 )
949 return( ld->ld_errno );
951 if ( ld->ld_errno == LDAP_TIMEOUT ) {
952 (void) ldap_abandon( ld, msgid );
953 ld->ld_errno = LDAP_TIMEOUT;
954 return( ld->ld_errno );
957 return( ldap_result2error( ld, *res, 0 ) );
963 LDAP_CONST char *base,
965 LDAP_CONST char *filter,
972 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
974 return( ld->ld_errno );
976 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, res ) == -1 )
977 return( ld->ld_errno );
979 return( ldap_result2error( ld, *res, 0 ) );