2 * Copyright (c) 1990 Regents of the University of Michigan.
11 static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
18 #include <ac/socket.h>
19 #include <ac/string.h>
21 extern char *strdup (const char *);
25 static char *find_right_paren LDAP_P(( char *s ));
26 static char *put_complex_filter LDAP_P(( BerElement *ber, char *str,
27 unsigned long tag, int not ));
28 static int put_filter LDAP_P(( BerElement *ber, char *str ));
29 static int put_simple_filter LDAP_P(( BerElement *ber, char *str ));
30 static int put_substring_filter LDAP_P(( BerElement *ber, char *type, char *str ));
31 static int put_filter_list LDAP_P(( BerElement *ber, char *str ));
34 * ldap_search - initiate an ldap (and X.500) search operation. Parameters:
37 * base DN of the base object
38 * scope the search scope - one of LDAP_SCOPE_BASE,
39 * LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE
40 * filter a string containing the search filter
41 * (e.g., "(|(cn=bob)(sn=bob))")
42 * attrs list of attribute types to return for matches
43 * attrsonly 1 => attributes only 0 => attributes and values
46 * char *attrs[] = { "mail", "title", 0 };
47 * msgid = ldap_search( ld, "c=us@o=UM", LDAP_SCOPE_SUBTREE, "cn~=bob",
51 ldap_search( LDAP *ld, char *base, int scope, char *filter,
52 char **attrs, int attrsonly )
56 Debug( LDAP_DEBUG_TRACE, "ldap_search\n", 0, 0, 0 );
58 if (( ber = ldap_build_search_req( ld, base, scope, filter, attrs,
59 attrsonly )) == NULLBER ) {
64 if ( ld->ld_cache != NULL ) {
65 if ( ldap_check_cache( ld, LDAP_REQ_SEARCH, ber ) == 0 ) {
67 ld->ld_errno = LDAP_SUCCESS;
68 return( ld->ld_msgid );
70 ldap_add_request_to_cache( ld, LDAP_REQ_SEARCH, ber );
72 #endif /* LDAP_NOCACHE */
74 /* send the message */
75 return ( ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber ));
80 ldap_build_search_req( LDAP *ld, char *base, int scope, char *filter,
81 char **attrs, int attrsonly )
87 * Create the search request. It looks like this:
88 * SearchRequest := [APPLICATION 3] SEQUENCE {
89 * baseObject DistinguishedName,
95 * derefAliases ENUMERATED {
96 * neverDerefaliases (0),
97 * derefInSearching (1),
98 * derefFindingBaseObj (2),
99 * alwaysDerefAliases (3)
101 * sizelimit INTEGER (0 .. 65535),
102 * timelimit INTEGER (0 .. 65535),
105 * attributes SEQUENCE OF AttributeType
107 * wrapped in an ldap message.
110 /* create a message to send */
111 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
115 if ( base == NULL ) {
119 #ifdef LDAP_CONNECTIONLESS
120 if ( ld->ld_sb.sb_naddr > 0 ) {
121 err = ber_printf( ber, "{ist{seeiib", ++ld->ld_msgid,
122 ld->ld_cldapdn, LDAP_REQ_SEARCH, base, scope, ld->ld_deref,
123 ld->ld_sizelimit, ld->ld_timelimit, attrsonly );
125 #endif /* LDAP_CONNECTIONLESS */
126 err = ber_printf( ber, "{it{seeiib", ++ld->ld_msgid,
127 LDAP_REQ_SEARCH, base, scope, ld->ld_deref,
128 ld->ld_sizelimit, ld->ld_timelimit, attrsonly );
129 #ifdef LDAP_CONNECTIONLESS
131 #endif /* LDAP_CONNECTIONLESS */
134 ld->ld_errno = LDAP_ENCODING_ERROR;
139 filter = strdup( filter );
140 err = put_filter( ber, filter );
144 ld->ld_errno = LDAP_FILTER_ERROR;
149 if ( ber_printf( ber, "{v}}}", attrs ) == -1 ) {
150 ld->ld_errno = LDAP_ENCODING_ERROR;
159 find_right_paren( char *s )
165 while ( *s && balance ) {
169 else if ( *s == ')' )
172 if ( *s == '\\' && ! escape )
180 return( *s ? s : NULL );
184 put_complex_filter( BerElement *ber, char *str, unsigned long tag, int not )
189 * We have (x(filter)...) with str sitting on
190 * the x. We have to find the paren matching
191 * the one before the x and put the intervening
192 * filters by calling put_filter_list().
195 /* put explicit tag */
196 if ( ber_printf( ber, "t{", tag ) == -1 )
199 if ( !not && ber_printf( ber, "{" ) == -1 )
204 if ( (next = find_right_paren( str )) == NULL )
208 if ( put_filter_list( ber, str ) == -1 )
212 /* flush explicit tagged thang */
213 if ( ber_printf( ber, "}" ) == -1 )
216 if ( !not && ber_printf( ber, "}" ) == -1 )
224 put_filter( BerElement *ber, char *str )
226 char *next, *tmp, *s, *d;
227 int parens, balance, escape, gotescape;
230 * A Filter looks like this:
231 * Filter ::= CHOICE {
232 * and [0] SET OF Filter,
233 * or [1] SET OF Filter,
235 * equalityMatch [3] AttributeValueAssertion,
236 * substrings [4] SubstringFilter,
237 * greaterOrEqual [5] AttributeValueAssertion,
238 * lessOrEqual [6] AttributeValueAssertion,
239 * present [7] AttributeType,,
240 * approxMatch [8] AttributeValueAssertion
243 * SubstringFilter ::= SEQUENCE {
244 * type AttributeType,
245 * SEQUENCE OF CHOICE {
246 * initial [0] IA5String,
248 * final [2] IA5String
251 * Note: tags in a choice are always explicit
254 Debug( LDAP_DEBUG_TRACE, "put_filter \"%s\"\n", str, 0, 0 );
256 gotescape = parens = 0;
264 Debug( LDAP_DEBUG_TRACE, "put_filter: AND\n",
267 if ( (str = put_complex_filter( ber, str,
268 LDAP_FILTER_AND, 0 )) == NULL )
275 Debug( LDAP_DEBUG_TRACE, "put_filter: OR\n",
278 if ( (str = put_complex_filter( ber, str,
279 LDAP_FILTER_OR, 0 )) == NULL )
286 Debug( LDAP_DEBUG_TRACE, "put_filter: NOT\n",
289 if ( (str = put_complex_filter( ber, str,
290 LDAP_FILTER_NOT, 1 )) == NULL )
297 Debug( LDAP_DEBUG_TRACE, "put_filter: simple\n",
303 while ( *next && balance ) {
307 else if ( *next == ')' )
310 if ( *next == '\\' && ! escape )
311 gotescape = escape = 1;
324 for ( s = d = tmp; *s; s++ ) {
325 if ( *s != '\\' || escape ) {
334 if ( put_simple_filter( ber, tmp ) == -1 ) {
347 Debug( LDAP_DEBUG_TRACE, "put_filter: end\n", 0, 0,
349 if ( ber_printf( ber, "]" ) == -1 )
359 default: /* assume it's a simple type=value filter */
360 Debug( LDAP_DEBUG_TRACE, "put_filter: default\n", 0, 0,
362 next = strchr( str, '\0' );
364 if ( strchr( tmp, '\\' ) != NULL ) {
366 for ( s = d = tmp; *s; s++ ) {
367 if ( *s != '\\' || escape ) {
376 if ( put_simple_filter( ber, tmp ) == -1 ) {
386 return( parens ? -1 : 0 );
390 * Put a list of filters like this "(filter1)(filter2)..."
394 put_filter_list( BerElement *ber, char *str )
399 Debug( LDAP_DEBUG_TRACE, "put_filter_list \"%s\"\n", str, 0, 0 );
402 while ( *str && isspace( *str ) )
407 if ( (next = find_right_paren( str + 1 )) == NULL )
411 /* now we have "(filter)" with str pointing to it */
413 if ( put_filter( ber, str ) == -1 )
424 put_simple_filter( BerElement *ber, char *str )
427 char *value, savechar;
431 Debug( LDAP_DEBUG_TRACE, "put_simple_filter \"%s\"\n", str, 0, 0 );
433 if ( (s = strchr( str, '=' )) == NULL )
441 ftype = LDAP_FILTER_LE;
445 ftype = LDAP_FILTER_GE;
449 ftype = LDAP_FILTER_APPROX;
453 if ( strchr( value, '*' ) == NULL ) {
454 ftype = LDAP_FILTER_EQUALITY;
455 } else if ( strcmp( value, "*" ) == 0 ) {
456 ftype = LDAP_FILTER_PRESENT;
458 rc = put_substring_filter( ber, str, value );
465 if ( ftype == LDAP_FILTER_PRESENT ) {
466 rc = ber_printf( ber, "ts", ftype, str );
468 rc = ber_printf( ber, "t{ss}", ftype, str, value );
473 return( rc == -1 ? rc : 0 );
477 put_substring_filter( BerElement *ber, char *type, char *val )
479 char *nextstar, gotstar = 0;
482 Debug( LDAP_DEBUG_TRACE, "put_substring_filter \"%s=%s\"\n", type,
485 if ( ber_printf( ber, "t{s{", LDAP_FILTER_SUBSTRINGS, type ) == -1 )
488 while ( val != NULL ) {
489 if ( (nextstar = strchr( val, '*' )) != NULL )
492 if ( gotstar == 0 ) {
493 ftype = LDAP_SUBSTRING_INITIAL;
494 } else if ( nextstar == NULL ) {
495 ftype = LDAP_SUBSTRING_FINAL;
497 ftype = LDAP_SUBSTRING_ANY;
499 if ( *val != '\0' ) {
500 if ( ber_printf( ber, "ts", ftype, val ) == -1 )
505 if ( nextstar != NULL )
510 if ( ber_printf( ber, "}}" ) == -1 )
517 ldap_search_st( LDAP *ld, char *base, int scope, char *filter, char **attrs,
518 int attrsonly, struct timeval *timeout, LDAPMessage **res )
522 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
524 return( ld->ld_errno );
526 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 )
527 return( ld->ld_errno );
529 if ( ld->ld_errno == LDAP_TIMEOUT ) {
530 (void) ldap_abandon( ld, msgid );
531 ld->ld_errno = LDAP_TIMEOUT;
532 return( ld->ld_errno );
535 return( ldap_result2error( ld, *res, 0 ) );
539 ldap_search_s( LDAP *ld, char *base, int scope, char *filter, char **attrs,
540 int attrsonly, LDAPMessage **res )
544 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
546 return( ld->ld_errno );
548 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, res ) == -1 )
549 return( ld->ld_errno );
551 return( ldap_result2error( ld, *res, 0 ) );