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>
26 static char *find_right_paren LDAP_P(( char *s ));
27 static char *put_complex_filter LDAP_P(( BerElement *ber, char *str,
28 unsigned long tag, int not ));
29 static int put_filter LDAP_P(( BerElement *ber, char *str ));
30 static int put_simple_filter LDAP_P(( BerElement *ber, char *str ));
31 static int put_substring_filter LDAP_P(( BerElement *ber, char *type, char *str ));
32 static int put_filter_list LDAP_P(( BerElement *ber, char *str ));
35 * ldap_search - initiate an ldap (and X.500) search operation. Parameters:
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, char *base, int scope, 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 )) == 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( LDAP *ld, char *base, int scope, char *filter,
82 char **attrs, int attrsonly )
88 * Create the search request. It looks like this:
89 * SearchRequest := [APPLICATION 3] SEQUENCE {
90 * baseObject DistinguishedName,
96 * derefAliases ENUMERATED {
97 * neverDerefaliases (0),
98 * derefInSearching (1),
99 * derefFindingBaseObj (2),
100 * alwaysDerefAliases (3)
102 * sizelimit INTEGER (0 .. 65535),
103 * timelimit INTEGER (0 .. 65535),
106 * attributes SEQUENCE OF AttributeType
108 * wrapped in an ldap message.
111 /* create a message to send */
112 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
116 if ( base == NULL ) {
117 /* no base provided, use global default base */
118 base = openldap_ldap_global_options.ldo_defbase;
121 if ( base == NULL ) {
122 /* no session default base, use top */
126 #ifdef LDAP_CONNECTIONLESS
127 if ( ld->ld_sb.sb_naddr > 0 ) {
128 err = ber_printf( ber, "{ist{seeiib", ++ld->ld_msgid,
129 ld->ld_cldapdn, LDAP_REQ_SEARCH, base, scope, ld->ld_deref,
130 ld->ld_sizelimit, ld->ld_timelimit, attrsonly );
132 #endif /* LDAP_CONNECTIONLESS */
133 err = ber_printf( ber, "{it{seeiib", ++ld->ld_msgid,
134 LDAP_REQ_SEARCH, base, scope, ld->ld_deref,
135 ld->ld_sizelimit, ld->ld_timelimit, attrsonly );
136 #ifdef LDAP_CONNECTIONLESS
138 #endif /* LDAP_CONNECTIONLESS */
141 ld->ld_errno = LDAP_ENCODING_ERROR;
146 filter = ldap_strdup( filter );
147 err = put_filter( ber, filter );
151 ld->ld_errno = LDAP_FILTER_ERROR;
156 if ( ber_printf( ber, "{v}}}", attrs ) == -1 ) {
157 ld->ld_errno = LDAP_ENCODING_ERROR;
166 find_right_paren( char *s )
172 while ( *s && balance ) {
176 else if ( *s == ')' )
179 if ( *s == '\\' && ! escape )
187 return( *s ? s : NULL );
191 put_complex_filter( BerElement *ber, char *str, unsigned long tag, int not )
196 * We have (x(filter)...) with str sitting on
197 * the x. We have to find the paren matching
198 * the one before the x and put the intervening
199 * filters by calling put_filter_list().
202 /* put explicit tag */
203 if ( ber_printf( ber, "t{", tag ) == -1 )
206 if ( !not && ber_printf( ber, "{" ) == -1 )
211 if ( (next = find_right_paren( str )) == NULL )
215 if ( put_filter_list( ber, str ) == -1 )
219 /* flush explicit tagged thang */
220 if ( ber_printf( ber, "}" ) == -1 )
223 if ( !not && ber_printf( ber, "}" ) == -1 )
231 put_filter( BerElement *ber, char *str )
233 char *next, *tmp, *s, *d;
234 int parens, balance, escape, gotescape;
237 * A Filter looks like this:
238 * Filter ::= CHOICE {
239 * and [0] SET OF Filter,
240 * or [1] SET OF Filter,
242 * equalityMatch [3] AttributeValueAssertion,
243 * substrings [4] SubstringFilter,
244 * greaterOrEqual [5] AttributeValueAssertion,
245 * lessOrEqual [6] AttributeValueAssertion,
246 * present [7] AttributeType,,
247 * approxMatch [8] AttributeValueAssertion
250 * SubstringFilter ::= SEQUENCE {
251 * type AttributeType,
252 * SEQUENCE OF CHOICE {
253 * initial [0] IA5String,
255 * final [2] IA5String
258 * Note: tags in a choice are always explicit
261 Debug( LDAP_DEBUG_TRACE, "put_filter \"%s\"\n", str, 0, 0 );
263 gotescape = parens = 0;
271 Debug( LDAP_DEBUG_TRACE, "put_filter: AND\n",
274 if ( (str = put_complex_filter( ber, str,
275 LDAP_FILTER_AND, 0 )) == NULL )
282 Debug( LDAP_DEBUG_TRACE, "put_filter: OR\n",
285 if ( (str = put_complex_filter( ber, str,
286 LDAP_FILTER_OR, 0 )) == NULL )
293 Debug( LDAP_DEBUG_TRACE, "put_filter: NOT\n",
296 if ( (str = put_complex_filter( ber, str,
297 LDAP_FILTER_NOT, 1 )) == NULL )
304 Debug( LDAP_DEBUG_TRACE, "put_filter: simple\n",
310 while ( *next && balance ) {
314 else if ( *next == ')' )
317 if ( *next == '\\' && ! escape )
318 gotescape = escape = 1;
328 tmp = ldap_strdup( str );
331 for ( s = d = tmp; *s; s++ ) {
332 if ( *s != '\\' || escape ) {
341 if ( put_simple_filter( ber, tmp ) == -1 ) {
354 Debug( LDAP_DEBUG_TRACE, "put_filter: end\n", 0, 0,
356 if ( ber_printf( ber, "]" ) == -1 )
366 default: /* assume it's a simple type=value filter */
367 Debug( LDAP_DEBUG_TRACE, "put_filter: default\n", 0, 0,
369 next = strchr( str, '\0' );
370 tmp = ldap_strdup( str );
371 if ( strchr( tmp, '\\' ) != NULL ) {
373 for ( s = d = tmp; *s; s++ ) {
374 if ( *s != '\\' || escape ) {
383 if ( put_simple_filter( ber, tmp ) == -1 ) {
393 return( parens ? -1 : 0 );
397 * Put a list of filters like this "(filter1)(filter2)..."
401 put_filter_list( BerElement *ber, char *str )
406 Debug( LDAP_DEBUG_TRACE, "put_filter_list \"%s\"\n", str, 0, 0 );
409 while ( *str && isspace( *str ) )
414 if ( (next = find_right_paren( str + 1 )) == NULL )
418 /* now we have "(filter)" with str pointing to it */
420 if ( put_filter( ber, str ) == -1 )
431 put_simple_filter( BerElement *ber, char *str )
434 char *value, savechar;
438 Debug( LDAP_DEBUG_TRACE, "put_simple_filter \"%s\"\n", str, 0, 0 );
440 if ( (s = strchr( str, '=' )) == NULL )
448 ftype = LDAP_FILTER_LE;
452 ftype = LDAP_FILTER_GE;
456 ftype = LDAP_FILTER_APPROX;
460 if ( strchr( value, '*' ) == NULL ) {
461 ftype = LDAP_FILTER_EQUALITY;
462 } else if ( strcmp( value, "*" ) == 0 ) {
463 ftype = LDAP_FILTER_PRESENT;
465 rc = put_substring_filter( ber, str, value );
472 if ( ftype == LDAP_FILTER_PRESENT ) {
473 rc = ber_printf( ber, "ts", ftype, str );
475 rc = ber_printf( ber, "t{ss}", ftype, str, value );
480 return( rc == -1 ? rc : 0 );
484 put_substring_filter( BerElement *ber, char *type, char *val )
486 char *nextstar, gotstar = 0;
489 Debug( LDAP_DEBUG_TRACE, "put_substring_filter \"%s=%s\"\n", type,
492 if ( ber_printf( ber, "t{s{", LDAP_FILTER_SUBSTRINGS, type ) == -1 )
495 while ( val != NULL ) {
496 if ( (nextstar = strchr( val, '*' )) != NULL )
499 if ( gotstar == 0 ) {
500 ftype = LDAP_SUBSTRING_INITIAL;
501 } else if ( nextstar == NULL ) {
502 ftype = LDAP_SUBSTRING_FINAL;
504 ftype = LDAP_SUBSTRING_ANY;
506 if ( *val != '\0' ) {
507 if ( ber_printf( ber, "ts", ftype, val ) == -1 )
512 if ( nextstar != NULL )
517 if ( ber_printf( ber, "}}" ) == -1 )
524 ldap_search_st( LDAP *ld, char *base, int scope, char *filter, char **attrs,
525 int attrsonly, struct timeval *timeout, LDAPMessage **res )
529 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
531 return( ld->ld_errno );
533 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 )
534 return( ld->ld_errno );
536 if ( ld->ld_errno == LDAP_TIMEOUT ) {
537 (void) ldap_abandon( ld, msgid );
538 ld->ld_errno = LDAP_TIMEOUT;
539 return( ld->ld_errno );
542 return( ldap_result2error( ld, *res, 0 ) );
546 ldap_search_s( LDAP *ld, char *base, int scope, char *filter, char **attrs,
547 int attrsonly, LDAPMessage **res )
551 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
553 return( ld->ld_errno );
555 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, res ) == -1 )
556 return( ld->ld_errno );
558 return( ldap_result2error( ld, *res, 0 ) );