2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2003 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
16 * All rights reserved.
23 #include <ac/stdlib.h>
25 #include <ac/socket.h>
26 #include <ac/string.h>
33 * ldap_search_ext - initiate an ldap search operation.
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 * ldap_search_ext( ld, "dc=example,dc=com", LDAP_SCOPE_SUBTREE, "cn~=bob",
49 * attrs, attrsonly, sctrls, ctrls, timeout, sizelimit,
55 LDAP_CONST char *base,
57 LDAP_CONST char *filter,
62 struct timeval *timeout,
72 LDAP_LOG ( OPERATION, ENTRY, "ldap_search_ext\n", 0, 0, 0 );
74 Debug( LDAP_DEBUG_TRACE, "ldap_search_ext\n", 0, 0, 0 );
78 assert( LDAP_VALID( ld ) );
80 /* check client controls */
81 rc = ldap_int_client_controls( ld, cctrls );
82 if( rc != LDAP_SUCCESS ) return rc;
85 * if timeout is provided, both tv_sec and tv_usec must
88 if( timeout != NULL ) {
89 if( timeout->tv_sec == 0 && timeout->tv_usec == 0 ) {
90 return LDAP_PARAM_ERROR;
93 /* timelimit must be non-zero if timeout is provided */
94 timelimit = timeout->tv_sec != 0 ? timeout->tv_sec : 1;
97 /* no timeout, no timelimit */
101 ber = ldap_build_search_req( ld, base, scope, filter, attrs,
102 attrsonly, sctrls, cctrls, timelimit, sizelimit, &id );
109 /* send the message */
110 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber, id );
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_INTERMEDIATE ) {
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 )
184 LDAP_LOG ( OPERATION, ENTRY, "ldap_search\n", 0, 0, 0 );
186 Debug( LDAP_DEBUG_TRACE, "ldap_search\n", 0, 0, 0 );
189 assert( ld != NULL );
190 assert( LDAP_VALID( ld ) );
192 ber = ldap_build_search_req( ld, base, scope, filter, attrs,
193 attrsonly, NULL, NULL, -1, -1, &id );
200 /* send the message */
201 return ( ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber, id ));
206 ldap_build_search_req(
208 LDAP_CONST char *base,
210 LDAP_CONST char *filter,
213 LDAPControl **sctrls,
214 LDAPControl **cctrls,
223 * Create the search request. It looks like this:
224 * SearchRequest := [APPLICATION 3] SEQUENCE {
225 * baseObject DistinguishedName,
231 * derefAliases ENUMERATED {
232 * neverDerefaliases (0),
233 * derefInSearching (1),
234 * derefFindingBaseObj (2),
235 * alwaysDerefAliases (3)
237 * sizelimit INTEGER (0 .. 65535),
238 * timelimit INTEGER (0 .. 65535),
241 * attributes SEQUENCE OF AttributeType
243 * wrapped in an ldap message.
246 /* create a message to send */
247 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
251 if ( base == NULL ) {
252 /* no base provided, use session default base */
253 base = ld->ld_options.ldo_defbase;
255 if ( base == NULL ) {
256 /* no session default base, use top */
261 LDAP_NEXT_MSGID( ld, *idp );
262 #ifdef LDAP_CONNECTIONLESS
263 if ( LDAP_IS_UDP(ld) ) {
264 err = ber_write( ber, ld->ld_options.ldo_peer,
265 sizeof(struct sockaddr), 0);
267 if ( LDAP_IS_UDP(ld) && ld->ld_options.ldo_version == LDAP_VERSION2) {
268 char *dn = ld->ld_options.ldo_cldapdn;
270 err = ber_printf( ber, "{ist{seeiib", *idp, dn,
271 LDAP_REQ_SEARCH, base, (ber_int_t) scope, ld->ld_deref,
272 (sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
273 (timelimit < 0) ? ld->ld_timelimit : timelimit,
278 err = ber_printf( ber, "{it{seeiib", *idp,
279 LDAP_REQ_SEARCH, base, (ber_int_t) scope, ld->ld_deref,
280 (sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
281 (timelimit < 0) ? ld->ld_timelimit : timelimit,
286 ld->ld_errno = LDAP_ENCODING_ERROR;
291 if( filter == NULL ) {
292 filter = "(objectclass=*)";
295 err = ldap_pvt_put_filter( ber, filter );
298 ld->ld_errno = LDAP_FILTER_ERROR;
303 if ( ber_printf( ber, /*{*/ "{v}N}", attrs ) == -1 ) {
304 ld->ld_errno = LDAP_ENCODING_ERROR;
309 /* Put Server Controls */
310 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
315 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
316 ld->ld_errno = LDAP_ENCODING_ERROR;
326 LDAP *ld, LDAP_CONST char *base, int scope,
327 LDAP_CONST char *filter, char **attrs,
328 int attrsonly, struct timeval *timeout, LDAPMessage **res )
332 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
334 return( ld->ld_errno );
336 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 )
337 return( ld->ld_errno );
339 if ( ld->ld_errno == LDAP_TIMEOUT ) {
340 (void) ldap_abandon( ld, msgid );
341 ld->ld_errno = LDAP_TIMEOUT;
342 return( ld->ld_errno );
345 return( ldap_result2error( ld, *res, 0 ) );
351 LDAP_CONST char *base,
353 LDAP_CONST char *filter,
360 if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
362 return( ld->ld_errno );
364 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, res ) == -1 )
365 return( ld->ld_errno );
367 return( ldap_result2error( ld, *res, 0 ) );