3 * Copyright 1998-2002 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,
218 LDAPControl **sctrls,
219 LDAPControl **cctrls,
221 ber_int_t sizelimit )
227 * Create the search request. It looks like this:
228 * SearchRequest := [APPLICATION 3] SEQUENCE {
229 * baseObject DistinguishedName,
235 * derefAliases ENUMERATED {
236 * neverDerefaliases (0),
237 * derefInSearching (1),
238 * derefFindingBaseObj (2),
239 * alwaysDerefAliases (3)
241 * sizelimit INTEGER (0 .. 65535),
242 * timelimit INTEGER (0 .. 65535),
245 * attributes SEQUENCE OF AttributeType
247 * wrapped in an ldap message.
250 /* create a message to send */
251 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
255 if ( base == NULL ) {
256 /* no base provided, use session default base */
257 base = ld->ld_options.ldo_defbase;
259 if ( base == NULL ) {
260 /* no session default base, use top */
265 #ifdef LDAP_CONNECTIONLESS
266 if ( LDAP_IS_UDP(ld) ) {
267 err = ber_write( ber, ld->ld_options.ldo_peer,
268 sizeof(struct sockaddr), 0);
270 if ( LDAP_IS_UDP(ld) && ld->ld_options.ldo_version == LDAP_VERSION2) {
271 char *dn = ld->ld_options.ldo_cldapdn;
273 err = ber_printf( ber, "{ist{seeiib", ++ld->ld_msgid, dn,
274 LDAP_REQ_SEARCH, base, (ber_int_t) scope, ld->ld_deref,
275 (sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
276 (timelimit < 0) ? ld->ld_timelimit : timelimit,
281 err = ber_printf( ber, "{it{seeiib", ++ld->ld_msgid,
282 LDAP_REQ_SEARCH, base, (ber_int_t) scope, ld->ld_deref,
283 (sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
284 (timelimit < 0) ? ld->ld_timelimit : timelimit,
289 ld->ld_errno = LDAP_ENCODING_ERROR;
294 if( filter == NULL ) {
295 filter = "(objectclass=*)";
298 err = ldap_int_put_filter( ber, filter );
301 ld->ld_errno = LDAP_FILTER_ERROR;
306 if ( ber_printf( ber, /*{*/ "{v}N}", attrs ) == -1 ) {
307 ld->ld_errno = LDAP_ENCODING_ERROR;
312 /* Put Server Controls */
313 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
318 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
319 ld->ld_errno = LDAP_ENCODING_ERROR;
329 LDAP *ld, LDAP_CONST char *base, int scope,
330 LDAP_CONST char *filter, char **attrs,
331 int attrsonly, struct timeval *timeout, LDAPMessage **res )
335 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
337 return( ld->ld_errno );
339 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 )
340 return( ld->ld_errno );
342 if ( ld->ld_errno == LDAP_TIMEOUT ) {
343 (void) ldap_abandon( ld, msgid );
344 ld->ld_errno = LDAP_TIMEOUT;
345 return( ld->ld_errno );
348 return( ldap_result2error( ld, *res, 0 ) );
354 LDAP_CONST char *base,
356 LDAP_CONST char *filter,
363 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
365 return( ld->ld_errno );
367 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, res ) == -1 )
368 return( ld->ld_errno );
370 return( ldap_result2error( ld, *res, 0 ) );