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>
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 ) {
118 #ifdef LDAP_CONNECTIONLESS
119 if ( ld->ld_sb.sb_naddr > 0 ) {
120 err = ber_printf( ber, "{ist{seeiib", ++ld->ld_msgid,
121 ld->ld_cldapdn, LDAP_REQ_SEARCH, base, scope, ld->ld_deref,
122 ld->ld_sizelimit, ld->ld_timelimit, attrsonly );
124 #endif /* LDAP_CONNECTIONLESS */
125 err = ber_printf( ber, "{it{seeiib", ++ld->ld_msgid,
126 LDAP_REQ_SEARCH, base, scope, ld->ld_deref,
127 ld->ld_sizelimit, ld->ld_timelimit, attrsonly );
128 #ifdef LDAP_CONNECTIONLESS
130 #endif /* LDAP_CONNECTIONLESS */
133 ld->ld_errno = LDAP_ENCODING_ERROR;
138 filter = ldap_strdup( filter );
139 err = put_filter( ber, filter );
143 ld->ld_errno = LDAP_FILTER_ERROR;
148 if ( ber_printf( ber, "{v}}}", attrs ) == -1 ) {
149 ld->ld_errno = LDAP_ENCODING_ERROR;
158 find_right_paren( char *s )
164 while ( *s && balance ) {
168 else if ( *s == ')' )
171 if ( *s == '\\' && ! escape )
179 return( *s ? s : NULL );
183 put_complex_filter( BerElement *ber, char *str, unsigned long tag, int not )
188 * We have (x(filter)...) with str sitting on
189 * the x. We have to find the paren matching
190 * the one before the x and put the intervening
191 * filters by calling put_filter_list().
194 /* put explicit tag */
195 if ( ber_printf( ber, "t{", tag ) == -1 )
198 if ( !not && ber_printf( ber, "{" ) == -1 )
203 if ( (next = find_right_paren( str )) == NULL )
207 if ( put_filter_list( ber, str ) == -1 )
211 /* flush explicit tagged thang */
212 if ( ber_printf( ber, "}" ) == -1 )
215 if ( !not && ber_printf( ber, "}" ) == -1 )
223 put_filter( BerElement *ber, char *str )
225 char *next, *tmp, *s, *d;
226 int parens, balance, escape, gotescape;
229 * A Filter looks like this:
230 * Filter ::= CHOICE {
231 * and [0] SET OF Filter,
232 * or [1] SET OF Filter,
234 * equalityMatch [3] AttributeValueAssertion,
235 * substrings [4] SubstringFilter,
236 * greaterOrEqual [5] AttributeValueAssertion,
237 * lessOrEqual [6] AttributeValueAssertion,
238 * present [7] AttributeType,,
239 * approxMatch [8] AttributeValueAssertion
242 * SubstringFilter ::= SEQUENCE {
243 * type AttributeType,
244 * SEQUENCE OF CHOICE {
245 * initial [0] IA5String,
247 * final [2] IA5String
250 * Note: tags in a choice are always explicit
253 Debug( LDAP_DEBUG_TRACE, "put_filter \"%s\"\n", str, 0, 0 );
255 gotescape = parens = 0;
263 Debug( LDAP_DEBUG_TRACE, "put_filter: AND\n",
266 if ( (str = put_complex_filter( ber, str,
267 LDAP_FILTER_AND, 0 )) == NULL )
274 Debug( LDAP_DEBUG_TRACE, "put_filter: OR\n",
277 if ( (str = put_complex_filter( ber, str,
278 LDAP_FILTER_OR, 0 )) == NULL )
285 Debug( LDAP_DEBUG_TRACE, "put_filter: NOT\n",
288 if ( (str = put_complex_filter( ber, str,
289 LDAP_FILTER_NOT, 1 )) == NULL )
296 Debug( LDAP_DEBUG_TRACE, "put_filter: simple\n",
302 while ( *next && balance ) {
306 else if ( *next == ')' )
309 if ( *next == '\\' && ! escape )
310 gotescape = escape = 1;
320 tmp = ldap_strdup( str );
323 for ( s = d = tmp; *s; s++ ) {
324 if ( *s != '\\' || escape ) {
333 if ( put_simple_filter( ber, tmp ) == -1 ) {
346 Debug( LDAP_DEBUG_TRACE, "put_filter: end\n", 0, 0,
348 if ( ber_printf( ber, "]" ) == -1 )
358 default: /* assume it's a simple type=value filter */
359 Debug( LDAP_DEBUG_TRACE, "put_filter: default\n", 0, 0,
361 next = strchr( str, '\0' );
362 tmp = ldap_strdup( str );
363 if ( strchr( tmp, '\\' ) != NULL ) {
365 for ( s = d = tmp; *s; s++ ) {
366 if ( *s != '\\' || escape ) {
375 if ( put_simple_filter( ber, tmp ) == -1 ) {
385 return( parens ? -1 : 0 );
389 * Put a list of filters like this "(filter1)(filter2)..."
393 put_filter_list( BerElement *ber, char *str )
398 Debug( LDAP_DEBUG_TRACE, "put_filter_list \"%s\"\n", str, 0, 0 );
401 while ( *str && isspace( *str ) )
406 if ( (next = find_right_paren( str + 1 )) == NULL )
410 /* now we have "(filter)" with str pointing to it */
412 if ( put_filter( ber, str ) == -1 )
423 put_simple_filter( BerElement *ber, char *str )
426 char *value, savechar;
430 Debug( LDAP_DEBUG_TRACE, "put_simple_filter \"%s\"\n", str, 0, 0 );
432 if ( (s = strchr( str, '=' )) == NULL )
440 ftype = LDAP_FILTER_LE;
444 ftype = LDAP_FILTER_GE;
448 ftype = LDAP_FILTER_APPROX;
452 if ( strchr( value, '*' ) == NULL ) {
453 ftype = LDAP_FILTER_EQUALITY;
454 } else if ( strcmp( value, "*" ) == 0 ) {
455 ftype = LDAP_FILTER_PRESENT;
457 rc = put_substring_filter( ber, str, value );
464 if ( ftype == LDAP_FILTER_PRESENT ) {
465 rc = ber_printf( ber, "ts", ftype, str );
467 rc = ber_printf( ber, "t{ss}", ftype, str, value );
472 return( rc == -1 ? rc : 0 );
476 put_substring_filter( BerElement *ber, char *type, char *val )
478 char *nextstar, gotstar = 0;
481 Debug( LDAP_DEBUG_TRACE, "put_substring_filter \"%s=%s\"\n", type,
484 if ( ber_printf( ber, "t{s{", LDAP_FILTER_SUBSTRINGS, type ) == -1 )
487 while ( val != NULL ) {
488 if ( (nextstar = strchr( val, '*' )) != NULL )
491 if ( gotstar == 0 ) {
492 ftype = LDAP_SUBSTRING_INITIAL;
493 } else if ( nextstar == NULL ) {
494 ftype = LDAP_SUBSTRING_FINAL;
496 ftype = LDAP_SUBSTRING_ANY;
498 if ( *val != '\0' ) {
499 if ( ber_printf( ber, "ts", ftype, val ) == -1 )
504 if ( nextstar != NULL )
509 if ( ber_printf( ber, "}}" ) == -1 )
516 ldap_search_st( LDAP *ld, char *base, int scope, char *filter, char **attrs,
517 int attrsonly, struct timeval *timeout, LDAPMessage **res )
521 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
523 return( ld->ld_errno );
525 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 )
526 return( ld->ld_errno );
528 if ( ld->ld_errno == LDAP_TIMEOUT ) {
529 (void) ldap_abandon( ld, msgid );
530 ld->ld_errno = LDAP_TIMEOUT;
531 return( ld->ld_errno );
534 return( ldap_result2error( ld, *res, 0 ) );
538 ldap_search_s( LDAP *ld, char *base, int scope, char *filter, char **attrs,
539 int attrsonly, LDAPMessage **res )
543 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
545 return( ld->ld_errno );
547 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, res ) == -1 )
548 return( ld->ld_errno );
550 return( ldap_result2error( ld, *res, 0 ) );