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,
97 Debug( LDAP_DEBUG_TRACE, "ldap_search_ext\n", 0, 0, 0 );
100 * if timeout is provided, both tv_sec and tv_usec must
103 if( timeout != NULL ) {
104 if( timeout->tv_sec == 0 && timeout->tv_usec == 0 ) {
105 return LDAP_PARAM_ERROR;
108 /* timelimit must be non-zero if timeout is provided */
109 timelimit = timeout->tv_sec != 0 ? timeout->tv_sec : 1;
112 /* no timeout, no timelimit */
116 ber = ldap_build_search_req( ld, base, scope, filter, attrs,
117 attrsonly, sctrls, cctrls, timelimit, sizelimit );
124 if ( ld->ld_cache != NULL ) {
125 if ( ldap_check_cache( ld, LDAP_REQ_SEARCH, ber ) == 0 ) {
127 ld->ld_errno = LDAP_SUCCESS;
128 *msgidp = ld->ld_msgid;
131 ldap_add_request_to_cache( ld, LDAP_REQ_SEARCH, ber );
133 #endif /* LDAP_NOCACHE */
135 /* send the message */
136 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber );
147 LDAP_CONST char *base,
149 LDAP_CONST char *filter,
152 LDAPControl **sctrls,
153 LDAPControl **cctrls,
154 struct timeval *timeout,
161 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
162 sctrls, cctrls, timeout, sizelimit, &msgid );
164 if ( rc != LDAP_SUCCESS ) {
168 rc = ldap_result( ld, msgid, 1, timeout, res );
171 /* error(-1) or timeout(0) */
172 return( ld->ld_errno );
175 if( rc == LDAP_RES_SEARCH_REFERENCE || rc == LDAP_RES_EXTENDED_PARTIAL ) {
176 return( ld->ld_errno );
179 return( ldap_result2error( ld, *res, 0 ) );
183 * ldap_search - initiate an ldap search operation.
188 * base DN of the base object
189 * scope the search scope - one of LDAP_SCOPE_BASE,
190 * LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE
191 * filter a string containing the search filter
192 * (e.g., "(|(cn=bob)(sn=bob))")
193 * attrs list of attribute types to return for matches
194 * attrsonly 1 => attributes only 0 => attributes and values
197 * char *attrs[] = { "mail", "title", 0 };
198 * msgid = ldap_search( ld, "c=us@o=UM", LDAP_SCOPE_SUBTREE, "cn~=bob",
199 * attrs, attrsonly );
203 LDAP *ld, LDAP_CONST char *base, int scope, LDAP_CONST char *filter,
204 char **attrs, int attrsonly )
208 Debug( LDAP_DEBUG_TRACE, "ldap_search\n", 0, 0, 0 );
210 ber = ldap_build_search_req( ld, base, scope, filter, attrs,
211 attrsonly, NULL, NULL, -1, -1 );
218 if ( ld->ld_cache != NULL ) {
219 if ( ldap_check_cache( ld, LDAP_REQ_SEARCH, ber ) == 0 ) {
221 ld->ld_errno = LDAP_SUCCESS;
222 return( ld->ld_msgid );
224 ldap_add_request_to_cache( ld, LDAP_REQ_SEARCH, ber );
226 #endif /* LDAP_NOCACHE */
228 /* send the message */
229 return ( ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber ));
234 ldap_build_search_req(
236 LDAP_CONST char *base,
238 LDAP_CONST char *filter_in,
241 LDAPControl **sctrls,
242 LDAPControl **cctrls,
244 ber_int_t sizelimit )
251 * Create the search request. It looks like this:
252 * SearchRequest := [APPLICATION 3] SEQUENCE {
253 * baseObject DistinguishedName,
259 * derefAliases ENUMERATED {
260 * neverDerefaliases (0),
261 * derefInSearching (1),
262 * derefFindingBaseObj (2),
263 * alwaysDerefAliases (3)
265 * sizelimit INTEGER (0 .. 65535),
266 * timelimit INTEGER (0 .. 65535),
269 * attributes SEQUENCE OF AttributeType
271 * wrapped in an ldap message.
274 /* create a message to send */
275 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
279 if ( base == NULL ) {
280 /* no base provided, use session default base */
281 base = ld->ld_options.ldo_defbase;
283 if ( base == NULL ) {
284 /* no session default base, use top */
289 err = ber_printf( ber, "{it{seeiib", ++ld->ld_msgid,
290 LDAP_REQ_SEARCH, base, (ber_int_t) scope, ld->ld_deref,
291 (sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
292 (timelimit < 0) ? ld->ld_timelimit : timelimit,
296 ld->ld_errno = LDAP_ENCODING_ERROR;
301 if( filter_in != NULL ) {
302 filter = LDAP_STRDUP( filter_in );
304 filter = LDAP_STRDUP( "(objectclass=*)" );
306 err = put_filter( ber, filter );
310 ld->ld_errno = LDAP_FILTER_ERROR;
315 if ( ber_printf( ber, /*{*/ "{v}N}", attrs ) == -1 ) {
316 ld->ld_errno = LDAP_ENCODING_ERROR;
321 /* Put Server Controls */
322 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
327 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
328 ld->ld_errno = LDAP_ENCODING_ERROR;
336 static int ldap_is_attr_oid ( const char *attr )
340 for( i = 0; (c = attr[i]) != 0; i++ ) {
341 if( c >= '0' && c <= '9' ) {
344 } else if ( c != '.' ) {
345 /* not digit nor '.' */
348 } else if ( !digit ) {
349 /* '.' but prev not digit */
361 static int ldap_is_attr_desc ( const char *attr )
363 /* cheap attribute description check */
366 for( i = 0; (c = attr[i]) != 0; i++ ) {
367 if (( c >= '0' && c <= '9' )
368 || ( c >= 'A' && c <= 'Z' )
369 || ( c >= 'a' && c <= 'z' )
370 || ( c == '.' || c == '-' )
371 || ( c == ';' )) continue;
380 find_right_paren( char *s )
386 while ( *s && balance ) {
390 else if ( *s == ')' )
393 if ( *s == '\\' && ! escape )
401 return( *s ? s : NULL );
404 static int hex2value( int c )
406 if( c >= '0' && c <= '9' ) {
410 if( c >= 'A' && c <= 'F' ) {
411 return c + (10 - (int) 'A');
414 if( c >= 'a' && c <= 'f' ) {
415 return c + (10 - (int) 'a');
422 ldap_pvt_find_wildcard( const char *s )
424 for( ; *s != '\0' ; s++ ) {
426 case '*': /* found wildcard */
430 s++; /* skip over escape */
432 return NULL; /* escape at end of string */
439 /* unescape filter value */
440 /* support both LDAP v2 and v3 escapes */
441 /* output can include nul characters */
443 ldap_pvt_filter_value_unescape( char *fval )
448 for( r=v=0; fval[v] != '\0'; v++ ) {
454 if ( fval[v] == '\0' ) {
455 /* escape at end of string */
460 if (( v1 = hex2value( fval[v] )) >= 0 ) {
463 if (( v2 = hex2value( fval[v+1] )) < 0 ) {
464 /* must be two digit code */
468 fval[r++] = v1 * 16 + v2;
488 put_complex_filter( BerElement *ber, char *str, ber_tag_t tag, int not )
493 * We have (x(filter)...) with str sitting on
494 * the x. We have to find the paren matching
495 * the one before the x and put the intervening
496 * filters by calling put_filter_list().
499 /* put explicit tag */
500 if ( ber_printf( ber, "t{" /*}*/, tag ) == -1 )
504 if ( (next = find_right_paren( str )) == NULL )
508 if ( put_filter_list( ber, str ) == -1 )
512 /* flush explicit tagged thang */
513 if ( ber_printf( ber, /*{*/ "N}" ) == -1 )
520 put_filter( BerElement *ber, char *str )
523 int parens, balance, escape;
526 * A Filter looks like this:
527 * Filter ::= CHOICE {
528 * and [0] SET OF Filter,
529 * or [1] SET OF Filter,
531 * equalityMatch [3] AttributeValueAssertion,
532 * substrings [4] SubstringFilter,
533 * greaterOrEqual [5] AttributeValueAssertion,
534 * lessOrEqual [6] AttributeValueAssertion,
535 * present [7] AttributeType,
536 * approxMatch [8] AttributeValueAssertion,
537 * extensibleMatch [9] MatchingRuleAssertion -- LDAPv3
540 * SubstringFilter ::= SEQUENCE {
541 * type AttributeType,
542 * SEQUENCE OF CHOICE {
543 * initial [0] IA5String,
545 * final [2] IA5String
549 * MatchingRuleAssertion ::= SEQUENCE { -- LDAPv3
550 * matchingRule [1] MatchingRuleId OPTIONAL,
551 * type [2] AttributeDescription OPTIONAL,
552 * matchValue [3] AssertionValue,
553 * dnAttributes [4] BOOLEAN DEFAULT FALSE }
555 * Note: tags in a choice are always explicit
558 Debug( LDAP_DEBUG_TRACE, "put_filter \"%s\"\n", str, 0, 0 );
568 while( LDAP_SPACE( *str ) ) str++;
572 Debug( LDAP_DEBUG_TRACE, "put_filter: AND\n",
575 if ( (str = put_complex_filter( ber, str,
576 LDAP_FILTER_AND, 0 )) == NULL )
583 Debug( LDAP_DEBUG_TRACE, "put_filter: OR\n",
586 if ( (str = put_complex_filter( ber, str,
587 LDAP_FILTER_OR, 0 )) == NULL )
594 Debug( LDAP_DEBUG_TRACE, "put_filter: NOT\n",
597 if ( (str = put_complex_filter( ber, str,
598 LDAP_FILTER_NOT, 1 )) == NULL )
605 Debug( LDAP_DEBUG_TRACE, "put_filter: simple\n",
611 while ( *next && balance ) {
615 else if ( *next == ')' )
618 if ( *next == '\\' && ! escape )
629 if ( put_simple_filter( ber, str ) == -1 ) {
640 Debug( LDAP_DEBUG_TRACE, "put_filter: end\n", 0, 0,
642 if ( ber_printf( ber, /*[*/ "]" ) == -1 )
652 default: /* assume it's a simple type=value filter */
653 Debug( LDAP_DEBUG_TRACE, "put_filter: default\n", 0, 0,
655 next = strchr( str, '\0' );
656 if ( put_simple_filter( ber, str ) == -1 ) {
664 return( parens ? -1 : 0 );
668 * Put a list of filters like this "(filter1)(filter2)..."
672 put_filter_list( BerElement *ber, char *str )
677 Debug( LDAP_DEBUG_TRACE, "put_filter_list \"%s\"\n", str, 0, 0 );
680 while ( *str && LDAP_SPACE( (unsigned char) *str ) )
685 if ( (next = find_right_paren( str + 1 )) == NULL )
689 /* now we have "(filter)" with str pointing to it */
691 if ( put_filter( ber, str ) == -1 )
711 Debug( LDAP_DEBUG_TRACE, "put_simple_filter \"%s\"\n", str, 0, 0 );
713 str = LDAP_STRDUP( str );
714 if( str == NULL ) return -1;
716 if ( (s = strchr( str, '=' )) == NULL ) {
725 ftype = LDAP_FILTER_LE;
727 if(! ldap_is_attr_desc( str ) ) goto done;
731 ftype = LDAP_FILTER_GE;
733 if(! ldap_is_attr_desc( str ) ) goto done;
737 ftype = LDAP_FILTER_APPROX;
739 if(! ldap_is_attr_desc( str ) ) goto done;
743 /* RFC2254 extensible filters are off the form:
744 * type [:dn] [:rule] := value
745 * or [:dn]:rule := value
747 ftype = LDAP_FILTER_EXT;
751 char *dn = strchr( str, ':' );
755 if(! ldap_is_attr_desc( str ) ) goto done;
760 rule = strchr( dn, ':' );
764 if ( strcmp(dn, "dn") == 0 ) {
765 /* must have attribute */
766 if( !ldap_is_attr_desc( str ) ) {
781 if ( strcmp(dn, "dn") != 0 ) {
787 if ( *str == '\0' && *rule == '\0' ) {
788 /* must have either type or rule */
792 if ( *str != '\0' && !ldap_is_attr_desc( str ) ) {
796 if ( *rule != '\0' && !ldap_is_attr_oid( rule ) ) {
800 rc = ber_printf( ber, "t{" /*}*/, ftype );
802 if( rc != -1 && *rule != '\0' ) {
803 rc = ber_printf( ber, "ts", LDAP_FILTER_EXT_OID, rule );
805 if( rc != -1 && *str != '\0' ) {
806 rc = ber_printf( ber, "ts", LDAP_FILTER_EXT_TYPE, str );
810 ber_slen_t len = ldap_pvt_filter_value_unescape( value );
813 rc = ber_printf( ber, "totbN}",
814 LDAP_FILTER_EXT_VALUE, value, len,
815 LDAP_FILTER_EXT_DNATTRS, dn != NULL);
824 if ( ldap_pvt_find_wildcard( value ) == NULL ) {
825 ftype = LDAP_FILTER_EQUALITY;
826 } else if ( strcmp( value, "*" ) == 0 ) {
827 ftype = LDAP_FILTER_PRESENT;
829 rc = put_substring_filter( ber, str, value );
835 if ( ftype == LDAP_FILTER_PRESENT ) {
836 rc = ber_printf( ber, "ts", ftype, str );
839 ber_slen_t len = ldap_pvt_filter_value_unescape( value );
842 rc = ber_printf( ber, "t{soN}",
843 ftype, str, value, len );
847 if( rc != -1 ) rc = 0;
855 put_substring_filter( BerElement *ber, char *type, char *val )
857 char *nextstar, gotstar = 0;
858 ber_tag_t ftype = LDAP_FILTER_SUBSTRINGS;
860 Debug( LDAP_DEBUG_TRACE, "put_substring_filter \"%s=%s\"\n", type,
863 if ( ber_printf( ber, "t{s{", ftype, type ) == -1 )
866 for( ; val != NULL; val=nextstar ) {
867 if ( (nextstar = ldap_pvt_find_wildcard( val )) != NULL )
870 if ( gotstar == 0 ) {
871 ftype = LDAP_SUBSTRING_INITIAL;
872 } else if ( nextstar == NULL ) {
873 ftype = LDAP_SUBSTRING_FINAL;
875 ftype = LDAP_SUBSTRING_ANY;
878 if ( *val != '\0' ) {
879 ber_slen_t len = ldap_pvt_filter_value_unescape( val );
885 if ( ber_printf( ber, "to", ftype, val, len ) == -1 ) {
893 if ( ber_printf( ber, /* {{ */ "N}N}" ) == -1 )
901 LDAP *ld, LDAP_CONST char *base, int scope,
902 LDAP_CONST char *filter, char **attrs,
903 int attrsonly, struct timeval *timeout, LDAPMessage **res )
907 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
909 return( ld->ld_errno );
911 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 )
912 return( ld->ld_errno );
914 if ( ld->ld_errno == LDAP_TIMEOUT ) {
915 (void) ldap_abandon( ld, msgid );
916 ld->ld_errno = LDAP_TIMEOUT;
917 return( ld->ld_errno );
920 return( ldap_result2error( ld, *res, 0 ) );
926 LDAP_CONST char *base,
928 LDAP_CONST char *filter,
935 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
937 return( ld->ld_errno );
939 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, res ) == -1 )
940 return( ld->ld_errno );
942 return( ldap_result2error( ld, *res, 0 ) );