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 search operation.
38 * base DN of the base object
39 * scope the search scope - one of LDAP_SCOPE_BASE,
40 * LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE
41 * filter a string containing the search filter
42 * (e.g., "(|(cn=bob)(sn=bob))")
43 * attrs list of attribute types to return for matches
44 * attrsonly 1 => attributes only 0 => attributes and values
47 * char *attrs[] = { "mail", "title", 0 };
48 * msgid = ldap_search( ld, "c=us@o=UM", LDAP_SCOPE_SUBTREE, "cn~=bob",
52 ldap_search( LDAP *ld, LDAP_CONST char *base, int scope, LDAP_CONST char *filter,
53 char **attrs, int attrsonly )
57 Debug( LDAP_DEBUG_TRACE, "ldap_search\n", 0, 0, 0 );
59 if (( ber = ldap_build_search_req( ld, base, scope, filter, attrs,
60 attrsonly, NULL, NULL )) == NULLBER ) {
65 if ( ld->ld_cache != NULL ) {
66 if ( ldap_check_cache( ld, LDAP_REQ_SEARCH, ber ) == 0 ) {
68 ld->ld_errno = LDAP_SUCCESS;
69 return( ld->ld_msgid );
71 ldap_add_request_to_cache( ld, LDAP_REQ_SEARCH, ber );
73 #endif /* LDAP_NOCACHE */
75 /* send the message */
76 return ( ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber ));
81 ldap_build_search_req(
83 LDAP_CONST char *base_in,
85 LDAP_CONST char *filter_in,
89 LDAPControl **cctrls )
97 * Create the search request. It looks like this:
98 * SearchRequest := [APPLICATION 3] SEQUENCE {
99 * baseObject DistinguishedName,
105 * derefAliases ENUMERATED {
106 * neverDerefaliases (0),
107 * derefInSearching (1),
108 * derefFindingBaseObj (2),
109 * alwaysDerefAliases (3)
111 * sizelimit INTEGER (0 .. 65535),
112 * timelimit INTEGER (0 .. 65535),
115 * attributes SEQUENCE OF AttributeType
117 * wrapped in an ldap message.
120 /* create a message to send */
121 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
125 if ( base_in == NULL ) {
126 /* no base provided, use session default base */
127 base = ld->ld_options.ldo_defbase;
129 base = (char *) base_in;
132 if ( base == NULL ) {
133 /* no session default base, use top */
137 #ifdef LDAP_CONNECTIONLESS
138 if ( ld->ld_cldapnaddr > 0 ) {
139 err = ber_printf( ber, "{ist{seeiib", ++ld->ld_msgid,
140 ld->ld_cldapdn, LDAP_REQ_SEARCH, base, scope, ld->ld_deref,
141 ld->ld_sizelimit, ld->ld_timelimit, attrsonly );
143 #endif /* LDAP_CONNECTIONLESS */
144 err = ber_printf( ber, "{it{seeiib", ++ld->ld_msgid,
145 LDAP_REQ_SEARCH, base, scope, ld->ld_deref,
146 ld->ld_sizelimit, ld->ld_timelimit, attrsonly );
147 #ifdef LDAP_CONNECTIONLESS
149 #endif /* LDAP_CONNECTIONLESS */
152 ld->ld_errno = LDAP_ENCODING_ERROR;
157 filter = strdup( filter_in );
158 err = put_filter( ber, filter );
162 ld->ld_errno = LDAP_FILTER_ERROR;
167 if ( ber_printf( ber, "{v}}", attrs ) == -1 ) {
168 ld->ld_errno = LDAP_ENCODING_ERROR;
173 /* Put Server Controls */
174 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
179 if ( ber_printf( ber, "}", attrs ) == -1 ) {
180 ld->ld_errno = LDAP_ENCODING_ERROR;
189 find_right_paren( char *s )
195 while ( *s && balance ) {
199 else if ( *s == ')' )
202 if ( *s == '\\' && ! escape )
210 return( *s ? s : NULL );
214 put_complex_filter( BerElement *ber, char *str, unsigned long tag, int not )
219 * We have (x(filter)...) with str sitting on
220 * the x. We have to find the paren matching
221 * the one before the x and put the intervening
222 * filters by calling put_filter_list().
225 /* put explicit tag */
226 if ( ber_printf( ber, "t{", tag ) == -1 )
229 if ( !not && ber_printf( ber, "{" ) == -1 )
234 if ( (next = find_right_paren( str )) == NULL )
238 if ( put_filter_list( ber, str ) == -1 )
242 /* flush explicit tagged thang */
243 if ( ber_printf( ber, "}" ) == -1 )
246 if ( !not && ber_printf( ber, "}" ) == -1 )
254 put_filter( BerElement *ber, char *str )
256 char *next, *tmp, *s, *d;
257 int parens, balance, escape, gotescape;
260 * A Filter looks like this:
261 * Filter ::= CHOICE {
262 * and [0] SET OF Filter,
263 * or [1] SET OF Filter,
265 * equalityMatch [3] AttributeValueAssertion,
266 * substrings [4] SubstringFilter,
267 * greaterOrEqual [5] AttributeValueAssertion,
268 * lessOrEqual [6] AttributeValueAssertion,
269 * present [7] AttributeType,,
270 * approxMatch [8] AttributeValueAssertion
273 * SubstringFilter ::= SEQUENCE {
274 * type AttributeType,
275 * SEQUENCE OF CHOICE {
276 * initial [0] IA5String,
278 * final [2] IA5String
281 * Note: tags in a choice are always explicit
284 Debug( LDAP_DEBUG_TRACE, "put_filter \"%s\"\n", str, 0, 0 );
286 gotescape = parens = 0;
294 Debug( LDAP_DEBUG_TRACE, "put_filter: AND\n",
297 if ( (str = put_complex_filter( ber, str,
298 LDAP_FILTER_AND, 0 )) == NULL )
305 Debug( LDAP_DEBUG_TRACE, "put_filter: OR\n",
308 if ( (str = put_complex_filter( ber, str,
309 LDAP_FILTER_OR, 0 )) == NULL )
316 Debug( LDAP_DEBUG_TRACE, "put_filter: NOT\n",
319 if ( (str = put_complex_filter( ber, str,
320 LDAP_FILTER_NOT, 1 )) == NULL )
327 Debug( LDAP_DEBUG_TRACE, "put_filter: simple\n",
333 while ( *next && balance ) {
337 else if ( *next == ')' )
340 if ( *next == '\\' && ! escape )
341 gotescape = escape = 1;
354 for ( s = d = tmp; *s; s++ ) {
355 if ( *s != '\\' || escape ) {
364 if ( put_simple_filter( ber, tmp ) == -1 ) {
377 Debug( LDAP_DEBUG_TRACE, "put_filter: end\n", 0, 0,
379 if ( ber_printf( ber, "]" ) == -1 )
389 default: /* assume it's a simple type=value filter */
390 Debug( LDAP_DEBUG_TRACE, "put_filter: default\n", 0, 0,
392 next = strchr( str, '\0' );
394 if ( strchr( tmp, '\\' ) != NULL ) {
396 for ( s = d = tmp; *s; s++ ) {
397 if ( *s != '\\' || escape ) {
406 if ( put_simple_filter( ber, tmp ) == -1 ) {
416 return( parens ? -1 : 0 );
420 * Put a list of filters like this "(filter1)(filter2)..."
424 put_filter_list( BerElement *ber, char *str )
429 Debug( LDAP_DEBUG_TRACE, "put_filter_list \"%s\"\n", str, 0, 0 );
432 while ( *str && isspace( (unsigned char) *str ) )
437 if ( (next = find_right_paren( str + 1 )) == NULL )
441 /* now we have "(filter)" with str pointing to it */
443 if ( put_filter( ber, str ) == -1 )
454 put_simple_filter( BerElement *ber, char *str )
457 char *value, savechar;
461 Debug( LDAP_DEBUG_TRACE, "put_simple_filter \"%s\"\n", str, 0, 0 );
463 if ( (s = strchr( str, '=' )) == NULL )
471 ftype = LDAP_FILTER_LE;
475 ftype = LDAP_FILTER_GE;
479 ftype = LDAP_FILTER_APPROX;
483 if ( strchr( value, '*' ) == NULL ) {
484 ftype = LDAP_FILTER_EQUALITY;
485 } else if ( strcmp( value, "*" ) == 0 ) {
486 ftype = LDAP_FILTER_PRESENT;
488 rc = put_substring_filter( ber, str, value );
495 if ( ftype == LDAP_FILTER_PRESENT ) {
496 rc = ber_printf( ber, "ts", ftype, str );
498 rc = ber_printf( ber, "t{ss}", ftype, str, value );
503 return( rc == -1 ? rc : 0 );
507 put_substring_filter( BerElement *ber, char *type, char *val )
509 char *nextstar, gotstar = 0;
512 Debug( LDAP_DEBUG_TRACE, "put_substring_filter \"%s=%s\"\n", type,
515 if ( ber_printf( ber, "t{s{", LDAP_FILTER_SUBSTRINGS, type ) == -1 )
518 while ( val != NULL ) {
519 if ( (nextstar = strchr( val, '*' )) != NULL )
522 if ( gotstar == 0 ) {
523 ftype = LDAP_SUBSTRING_INITIAL;
524 } else if ( nextstar == NULL ) {
525 ftype = LDAP_SUBSTRING_FINAL;
527 ftype = LDAP_SUBSTRING_ANY;
529 if ( *val != '\0' ) {
530 if ( ber_printf( ber, "ts", ftype, val ) == -1 )
535 if ( nextstar != NULL )
540 if ( ber_printf( ber, "}}" ) == -1 )
548 LDAP *ld, LDAP_CONST char *base, int scope,
549 LDAP_CONST char *filter, char **attrs,
550 int attrsonly, struct timeval *timeout, LDAPMessage **res )
554 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
556 return( ld->ld_errno );
558 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 )
559 return( ld->ld_errno );
561 if ( ld->ld_errno == LDAP_TIMEOUT ) {
562 (void) ldap_abandon( ld, msgid );
563 ld->ld_errno = LDAP_TIMEOUT;
564 return( ld->ld_errno );
567 return( ldap_result2error( ld, *res, 0 ) );
573 LDAP_CONST char *base,
575 LDAP_CONST char *filter,
582 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
584 return( ld->ld_errno );
586 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, res ) == -1 )
587 return( ld->ld_errno );
589 return( ldap_result2error( ld, *res, 0 ) );