3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1990 Regents of the University of Michigan.
17 #include <ac/stdlib.h>
19 #include <ac/socket.h>
20 #include <ac/string.h>
27 * ldap_search_ext - initiate an ldap search operation.
32 * base DN of the base object
33 * scope the search scope - one of LDAP_SCOPE_BASE,
34 * LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE
35 * filter a string containing the search filter
36 * (e.g., "(|(cn=bob)(sn=bob))")
37 * attrs list of attribute types to return for matches
38 * attrsonly 1 => attributes only 0 => attributes and values
41 * char *attrs[] = { "mail", "title", 0 };
42 * ldap_search_ext( ld, "dc=example,dc=com", LDAP_SCOPE_SUBTREE, "cn~=bob",
43 * attrs, attrsonly, sctrls, ctrls, timeout, sizelimit,
49 LDAP_CONST char *base,
51 LDAP_CONST char *filter,
56 struct timeval *timeout,
64 Debug( LDAP_DEBUG_TRACE, "ldap_search_ext\n", 0, 0, 0 );
67 assert( LDAP_VALID( ld ) );
69 /* check client controls */
70 rc = ldap_int_client_controls( ld, cctrls );
71 if( rc != LDAP_SUCCESS ) return rc;
74 * if timeout is provided, both tv_sec and tv_usec must
77 if( timeout != NULL ) {
78 if( timeout->tv_sec == 0 && timeout->tv_usec == 0 ) {
79 return LDAP_PARAM_ERROR;
82 /* timelimit must be non-zero if timeout is provided */
83 timelimit = timeout->tv_sec != 0 ? timeout->tv_sec : 1;
86 /* no timeout, no timelimit */
90 ber = ldap_build_search_req( ld, base, scope, filter, attrs,
91 attrsonly, sctrls, cctrls, timelimit, sizelimit );
98 if ( ld->ld_cache != NULL ) {
99 if ( ldap_check_cache( ld, LDAP_REQ_SEARCH, ber ) == 0 ) {
101 ld->ld_errno = LDAP_SUCCESS;
102 *msgidp = ld->ld_msgid;
105 ldap_add_request_to_cache( ld, LDAP_REQ_SEARCH, ber );
107 #endif /* LDAP_NOCACHE */
109 /* send the message */
110 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber );
121 LDAP_CONST char *base,
123 LDAP_CONST char *filter,
126 LDAPControl **sctrls,
127 LDAPControl **cctrls,
128 struct timeval *timeout,
135 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
136 sctrls, cctrls, timeout, sizelimit, &msgid );
138 if ( rc != LDAP_SUCCESS ) {
142 rc = ldap_result( ld, msgid, 1, timeout, res );
145 /* error(-1) or timeout(0) */
146 return( ld->ld_errno );
149 if( rc == LDAP_RES_SEARCH_REFERENCE || rc == LDAP_RES_EXTENDED_PARTIAL ) {
150 return( ld->ld_errno );
153 return( ldap_result2error( ld, *res, 0 ) );
157 * ldap_search - initiate an ldap search operation.
162 * base DN of the base object
163 * scope the search scope - one of LDAP_SCOPE_BASE,
164 * LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE
165 * filter a string containing the search filter
166 * (e.g., "(|(cn=bob)(sn=bob))")
167 * attrs list of attribute types to return for matches
168 * attrsonly 1 => attributes only 0 => attributes and values
171 * char *attrs[] = { "mail", "title", 0 };
172 * msgid = ldap_search( ld, "dc=example,dc=com", LDAP_SCOPE_SUBTREE, "cn~=bob",
173 * attrs, attrsonly );
177 LDAP *ld, LDAP_CONST char *base, int scope, LDAP_CONST char *filter,
178 char **attrs, int attrsonly )
182 Debug( LDAP_DEBUG_TRACE, "ldap_search\n", 0, 0, 0 );
184 assert( ld != NULL );
185 assert( LDAP_VALID( ld ) );
187 ber = ldap_build_search_req( ld, base, scope, filter, attrs,
188 attrsonly, NULL, NULL, -1, -1 );
195 if ( ld->ld_cache != NULL ) {
196 if ( ldap_check_cache( ld, LDAP_REQ_SEARCH, ber ) == 0 ) {
198 ld->ld_errno = LDAP_SUCCESS;
199 return( ld->ld_msgid );
201 ldap_add_request_to_cache( ld, LDAP_REQ_SEARCH, ber );
203 #endif /* LDAP_NOCACHE */
205 /* send the message */
206 return ( ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber ));
211 ldap_build_search_req(
213 LDAP_CONST char *base,
215 LDAP_CONST char *filter_in,
218 LDAPControl **sctrls,
219 LDAPControl **cctrls,
221 ber_int_t sizelimit )
228 * Create the search request. It looks like this:
229 * SearchRequest := [APPLICATION 3] SEQUENCE {
230 * baseObject DistinguishedName,
236 * derefAliases ENUMERATED {
237 * neverDerefaliases (0),
238 * derefInSearching (1),
239 * derefFindingBaseObj (2),
240 * alwaysDerefAliases (3)
242 * sizelimit INTEGER (0 .. 65535),
243 * timelimit INTEGER (0 .. 65535),
246 * attributes SEQUENCE OF AttributeType
248 * wrapped in an ldap message.
251 /* create a message to send */
252 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
256 if ( base == NULL ) {
257 /* no base provided, use session default base */
258 base = ld->ld_options.ldo_defbase;
260 if ( base == NULL ) {
261 /* no session default base, use top */
266 #ifdef LDAP_CONNECTIONLESS
267 if ( LDAP_IS_UDP(ld) ) {
268 err = ber_write( ber, ld->ld_options.ldo_peer,
269 sizeof(struct sockaddr), 0);
271 if ( LDAP_IS_UDP(ld) && ld->ld_options.ldo_version == LDAP_VERSION2) {
272 char *dn = ld->ld_options.ldo_cldapdn;
274 err = ber_printf( ber, "{ist{seeiib", ++ld->ld_msgid, dn,
275 LDAP_REQ_SEARCH, base, (ber_int_t) scope, ld->ld_deref,
276 (sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
277 (timelimit < 0) ? ld->ld_timelimit : timelimit,
282 err = ber_printf( ber, "{it{seeiib", ++ld->ld_msgid,
283 LDAP_REQ_SEARCH, base, (ber_int_t) scope, ld->ld_deref,
284 (sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
285 (timelimit < 0) ? ld->ld_timelimit : timelimit,
290 ld->ld_errno = LDAP_ENCODING_ERROR;
295 if( filter_in != NULL ) {
296 filter = LDAP_STRDUP( filter_in );
298 filter = LDAP_STRDUP( "(objectclass=*)" );
300 err = ldap_int_put_filter( ber, filter );
304 ld->ld_errno = LDAP_FILTER_ERROR;
309 if ( ber_printf( ber, /*{*/ "{v}N}", attrs ) == -1 ) {
310 ld->ld_errno = LDAP_ENCODING_ERROR;
315 /* Put Server Controls */
316 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
321 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
322 ld->ld_errno = LDAP_ENCODING_ERROR;
332 LDAP *ld, LDAP_CONST char *base, int scope,
333 LDAP_CONST char *filter, char **attrs,
334 int attrsonly, struct timeval *timeout, LDAPMessage **res )
338 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
340 return( ld->ld_errno );
342 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 )
343 return( ld->ld_errno );
345 if ( ld->ld_errno == LDAP_TIMEOUT ) {
346 (void) ldap_abandon( ld, msgid );
347 ld->ld_errno = LDAP_TIMEOUT;
348 return( ld->ld_errno );
351 return( ldap_result2error( ld, *res, 0 ) );
357 LDAP_CONST char *base,
359 LDAP_CONST char *filter,
366 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
368 return( ld->ld_errno );
370 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, res ) == -1 )
371 return( ld->ld_errno );
373 return( ldap_result2error( ld, *res, 0 ) );