2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright (c) 1990 Regents of the University of Michigan.
18 #include <ac/socket.h>
19 #include <ac/string.h>
24 static char *find_right_paren LDAP_P(( char *s ));
25 static char *put_complex_filter LDAP_P(( BerElement *ber, char *str,
26 unsigned long tag, int not ));
27 static int put_filter LDAP_P(( BerElement *ber, char *str ));
28 static int put_simple_filter LDAP_P(( BerElement *ber, char *str ));
29 static int put_substring_filter LDAP_P(( BerElement *ber, char *type, char *str ));
30 static int put_filter_list LDAP_P(( BerElement *ber, char *str ));
33 * ldap_search - initiate an ldap (and X.500) search operation. Parameters:
36 * base DN of the base object
37 * scope the search scope - one of LDAP_SCOPE_BASE,
38 * LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE
39 * filter a string containing the search filter
40 * (e.g., "(|(cn=bob)(sn=bob))")
41 * attrs list of attribute types to return for matches
42 * attrsonly 1 => attributes only 0 => attributes and values
45 * char *attrs[] = { "mail", "title", 0 };
46 * msgid = ldap_search( ld, "c=us@o=UM", LDAP_SCOPE_SUBTREE, "cn~=bob",
50 ldap_search( LDAP *ld, char *base, int scope, char *filter,
51 char **attrs, int attrsonly )
55 Debug( LDAP_DEBUG_TRACE, "ldap_search\n", 0, 0, 0 );
57 if (( ber = ldap_build_search_req( ld, base, scope, filter, attrs,
58 attrsonly )) == NULLBER ) {
63 if ( ld->ld_cache != NULL ) {
64 if ( ldap_check_cache( ld, LDAP_REQ_SEARCH, ber ) == 0 ) {
66 ld->ld_errno = LDAP_SUCCESS;
67 return( ld->ld_msgid );
69 ldap_add_request_to_cache( ld, LDAP_REQ_SEARCH, ber );
71 #endif /* LDAP_NOCACHE */
73 /* send the message */
74 return ( ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber ));
79 ldap_build_search_req( LDAP *ld, char *base, int scope, char *filter,
80 char **attrs, int attrsonly )
86 * Create the search request. It looks like this:
87 * SearchRequest := [APPLICATION 3] SEQUENCE {
88 * baseObject DistinguishedName,
94 * derefAliases ENUMERATED {
95 * neverDerefaliases (0),
96 * derefInSearching (1),
97 * derefFindingBaseObj (2),
98 * alwaysDerefAliases (3)
100 * sizelimit INTEGER (0 .. 65535),
101 * timelimit INTEGER (0 .. 65535),
104 * attributes SEQUENCE OF AttributeType
106 * wrapped in an ldap message.
109 /* create a message to send */
110 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
114 if ( base == NULL ) {
115 /* no base provided, use session default base */
116 base = ld->ld_options.ldo_defbase;
119 if ( base == NULL ) {
120 /* no session default base, use top */
124 #ifdef LDAP_CONNECTIONLESS
125 if ( ld->ld_sb.sb_naddr > 0 ) {
126 err = ber_printf( ber, "{ist{seeiib", ++ld->ld_msgid,
127 ld->ld_cldapdn, LDAP_REQ_SEARCH, base, scope, ld->ld_deref,
128 ld->ld_sizelimit, ld->ld_timelimit, attrsonly );
130 #endif /* LDAP_CONNECTIONLESS */
131 err = ber_printf( ber, "{it{seeiib", ++ld->ld_msgid,
132 LDAP_REQ_SEARCH, base, scope, ld->ld_deref,
133 ld->ld_sizelimit, ld->ld_timelimit, attrsonly );
134 #ifdef LDAP_CONNECTIONLESS
136 #endif /* LDAP_CONNECTIONLESS */
139 ld->ld_errno = LDAP_ENCODING_ERROR;
144 filter = strdup( filter );
145 err = put_filter( ber, filter );
149 ld->ld_errno = LDAP_FILTER_ERROR;
154 if ( ber_printf( ber, "{v}}}", attrs ) == -1 ) {
155 ld->ld_errno = LDAP_ENCODING_ERROR;
164 find_right_paren( char *s )
170 while ( *s && balance ) {
174 else if ( *s == ')' )
177 if ( *s == '\\' && ! escape )
185 return( *s ? s : NULL );
189 put_complex_filter( BerElement *ber, char *str, unsigned long tag, int not )
194 * We have (x(filter)...) with str sitting on
195 * the x. We have to find the paren matching
196 * the one before the x and put the intervening
197 * filters by calling put_filter_list().
200 /* put explicit tag */
201 if ( ber_printf( ber, "t{", tag ) == -1 )
204 if ( !not && ber_printf( ber, "{" ) == -1 )
209 if ( (next = find_right_paren( str )) == NULL )
213 if ( put_filter_list( ber, str ) == -1 )
217 /* flush explicit tagged thang */
218 if ( ber_printf( ber, "}" ) == -1 )
221 if ( !not && ber_printf( ber, "}" ) == -1 )
229 put_filter( BerElement *ber, char *str )
231 char *next, *tmp, *s, *d;
232 int parens, balance, escape, gotescape;
235 * A Filter looks like this:
236 * Filter ::= CHOICE {
237 * and [0] SET OF Filter,
238 * or [1] SET OF Filter,
240 * equalityMatch [3] AttributeValueAssertion,
241 * substrings [4] SubstringFilter,
242 * greaterOrEqual [5] AttributeValueAssertion,
243 * lessOrEqual [6] AttributeValueAssertion,
244 * present [7] AttributeType,,
245 * approxMatch [8] AttributeValueAssertion
248 * SubstringFilter ::= SEQUENCE {
249 * type AttributeType,
250 * SEQUENCE OF CHOICE {
251 * initial [0] IA5String,
253 * final [2] IA5String
256 * Note: tags in a choice are always explicit
259 Debug( LDAP_DEBUG_TRACE, "put_filter \"%s\"\n", str, 0, 0 );
261 gotescape = parens = 0;
269 Debug( LDAP_DEBUG_TRACE, "put_filter: AND\n",
272 if ( (str = put_complex_filter( ber, str,
273 LDAP_FILTER_AND, 0 )) == NULL )
280 Debug( LDAP_DEBUG_TRACE, "put_filter: OR\n",
283 if ( (str = put_complex_filter( ber, str,
284 LDAP_FILTER_OR, 0 )) == NULL )
291 Debug( LDAP_DEBUG_TRACE, "put_filter: NOT\n",
294 if ( (str = put_complex_filter( ber, str,
295 LDAP_FILTER_NOT, 1 )) == NULL )
302 Debug( LDAP_DEBUG_TRACE, "put_filter: simple\n",
308 while ( *next && balance ) {
312 else if ( *next == ')' )
315 if ( *next == '\\' && ! escape )
316 gotescape = escape = 1;
329 for ( s = d = tmp; *s; s++ ) {
330 if ( *s != '\\' || escape ) {
339 if ( put_simple_filter( ber, tmp ) == -1 ) {
352 Debug( LDAP_DEBUG_TRACE, "put_filter: end\n", 0, 0,
354 if ( ber_printf( ber, "]" ) == -1 )
364 default: /* assume it's a simple type=value filter */
365 Debug( LDAP_DEBUG_TRACE, "put_filter: default\n", 0, 0,
367 next = strchr( str, '\0' );
369 if ( strchr( tmp, '\\' ) != NULL ) {
371 for ( s = d = tmp; *s; s++ ) {
372 if ( *s != '\\' || escape ) {
381 if ( put_simple_filter( ber, tmp ) == -1 ) {
391 return( parens ? -1 : 0 );
395 * Put a list of filters like this "(filter1)(filter2)..."
399 put_filter_list( BerElement *ber, char *str )
404 Debug( LDAP_DEBUG_TRACE, "put_filter_list \"%s\"\n", str, 0, 0 );
407 while ( *str && isspace( (unsigned char) *str ) )
412 if ( (next = find_right_paren( str + 1 )) == NULL )
416 /* now we have "(filter)" with str pointing to it */
418 if ( put_filter( ber, str ) == -1 )
429 put_simple_filter( BerElement *ber, char *str )
432 char *value, savechar;
436 Debug( LDAP_DEBUG_TRACE, "put_simple_filter \"%s\"\n", str, 0, 0 );
438 if ( (s = strchr( str, '=' )) == NULL )
446 ftype = LDAP_FILTER_LE;
450 ftype = LDAP_FILTER_GE;
454 ftype = LDAP_FILTER_APPROX;
458 if ( strchr( value, '*' ) == NULL ) {
459 ftype = LDAP_FILTER_EQUALITY;
460 } else if ( strcmp( value, "*" ) == 0 ) {
461 ftype = LDAP_FILTER_PRESENT;
463 rc = put_substring_filter( ber, str, value );
470 if ( ftype == LDAP_FILTER_PRESENT ) {
471 rc = ber_printf( ber, "ts", ftype, str );
473 rc = ber_printf( ber, "t{ss}", ftype, str, value );
478 return( rc == -1 ? rc : 0 );
482 put_substring_filter( BerElement *ber, char *type, char *val )
484 char *nextstar, gotstar = 0;
487 Debug( LDAP_DEBUG_TRACE, "put_substring_filter \"%s=%s\"\n", type,
490 if ( ber_printf( ber, "t{s{", LDAP_FILTER_SUBSTRINGS, type ) == -1 )
493 while ( val != NULL ) {
494 if ( (nextstar = strchr( val, '*' )) != NULL )
497 if ( gotstar == 0 ) {
498 ftype = LDAP_SUBSTRING_INITIAL;
499 } else if ( nextstar == NULL ) {
500 ftype = LDAP_SUBSTRING_FINAL;
502 ftype = LDAP_SUBSTRING_ANY;
504 if ( *val != '\0' ) {
505 if ( ber_printf( ber, "ts", ftype, val ) == -1 )
510 if ( nextstar != NULL )
515 if ( ber_printf( ber, "}}" ) == -1 )
522 ldap_search_st( LDAP *ld, char *base, int scope, char *filter, char **attrs,
523 int attrsonly, struct timeval *timeout, LDAPMessage **res )
527 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
529 return( ld->ld_errno );
531 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 )
532 return( ld->ld_errno );
534 if ( ld->ld_errno == LDAP_TIMEOUT ) {
535 (void) ldap_abandon( ld, msgid );
536 ld->ld_errno = LDAP_TIMEOUT;
537 return( ld->ld_errno );
540 return( ldap_result2error( ld, *res, 0 ) );
544 ldap_search_s( LDAP *ld, char *base, int scope, char *filter, char **attrs,
545 int attrsonly, LDAPMessage **res )
549 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
551 return( ld->ld_errno );
553 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, res ) == -1 )
554 return( ld->ld_errno );
556 return( ldap_result2error( ld, *res, 0 ) );