]> git.sur5r.net Git - openldap/blob - libraries/libldap/search.c
aa699ccce9a97c0e85bebb9b7d93373d93f9e136
[openldap] / libraries / libldap / search.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2004 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
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>.
14  */
15 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
16  * All rights reserved.
17  */
18
19 #include "portable.h"
20
21 #include <stdio.h>
22
23 #include <ac/stdlib.h>
24
25 #include <ac/socket.h>
26 #include <ac/string.h>
27 #include <ac/time.h>
28
29 #include "ldap-int.h"
30 #include "ldap_log.h"
31
32 /*
33  * ldap_search_ext - initiate an ldap search operation.
34  *
35  * Parameters:
36  *
37  *      ld              LDAP descriptor
38  *      base            DN of the base object
39  *      scope           the search scope - one of
40  *                              LDAP_SCOPE_BASE (baseObject),
41  *                          LDAP_SCOPE_ONELEVEL (oneLevel),
42  *                              LDAP_SCOPE_SUBTREE (subtree), or
43  *                              LDAP_SCOPE_SUBORDINATE (children) -- OpenLDAP extension
44  *      filter          a string containing the search filter
45  *                      (e.g., "(|(cn=bob)(sn=bob))")
46  *      attrs           list of attribute types to return for matches
47  *      attrsonly       1 => attributes only 0 => attributes and values
48  *
49  * Example:
50  *      char    *attrs[] = { "mail", "title", 0 };
51  *      ldap_search_ext( ld, "dc=example,dc=com", LDAP_SCOPE_SUBTREE, "cn~=bob",
52  *          attrs, attrsonly, sctrls, ctrls, timeout, sizelimit,
53  *              &msgid );
54  */
55 int
56 ldap_search_ext(
57         LDAP *ld,
58         LDAP_CONST char *base,
59         int scope,
60         LDAP_CONST char *filter,
61         char **attrs,
62         int attrsonly,
63         LDAPControl **sctrls,
64         LDAPControl **cctrls,
65         struct timeval *timeout,
66         int sizelimit,
67         int *msgidp )
68 {
69         int rc;
70         BerElement      *ber;
71         int timelimit;
72         ber_int_t id;
73
74 #ifdef NEW_LOGGING
75         LDAP_LOG ( OPERATION, ENTRY, "ldap_search_ext\n", 0, 0, 0 );
76 #else
77         Debug( LDAP_DEBUG_TRACE, "ldap_search_ext\n", 0, 0, 0 );
78 #endif
79
80         assert( ld != NULL );
81         assert( LDAP_VALID( ld ) );
82
83         /* check client controls */
84         rc = ldap_int_client_controls( ld, cctrls );
85         if( rc != LDAP_SUCCESS ) return rc;
86
87         /*
88          * if timeout is provided, both tv_sec and tv_usec must
89          * not be zero
90          */
91         if( timeout != NULL ) {
92                 if( timeout->tv_sec == 0 && timeout->tv_usec == 0 ) {
93                         return LDAP_PARAM_ERROR;
94                 }
95
96                 /* timelimit must be non-zero if timeout is provided */
97                 timelimit = timeout->tv_sec != 0 ? timeout->tv_sec : 1;
98
99         } else {
100                 /* no timeout, no timelimit */
101                 timelimit = -1;
102         }
103
104         ber = ldap_build_search_req( ld, base, scope, filter, attrs,
105             attrsonly, sctrls, cctrls, timelimit, sizelimit, &id ); 
106
107         if ( ber == NULL ) {
108                 return ld->ld_errno;
109         }
110
111
112         /* send the message */
113         *msgidp = ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber, id );
114
115         if( *msgidp < 0 )
116                 return ld->ld_errno;
117
118         return LDAP_SUCCESS;
119 }
120
121 int
122 ldap_search_ext_s(
123         LDAP *ld,
124         LDAP_CONST char *base,
125         int scope,
126         LDAP_CONST char *filter,
127         char **attrs,
128         int attrsonly,
129         LDAPControl **sctrls,
130         LDAPControl **cctrls,
131         struct timeval *timeout,
132         int sizelimit,
133         LDAPMessage **res )
134 {
135         int rc;
136         int     msgid;
137
138         rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
139                 sctrls, cctrls, timeout, sizelimit, &msgid );
140
141         if ( rc != LDAP_SUCCESS ) {
142                 return( rc );
143         }
144
145         rc = ldap_result( ld, msgid, 1, timeout, res );
146
147         if( rc <= 0 ) {
148                 /* error(-1) or timeout(0) */
149                 return( ld->ld_errno );
150         }
151
152         if( rc == LDAP_RES_SEARCH_REFERENCE || rc == LDAP_RES_INTERMEDIATE ) {
153                 return( ld->ld_errno );
154         }
155
156         return( ldap_result2error( ld, *res, 0 ) );
157 }
158
159 /*
160  * ldap_search - initiate an ldap search operation.
161  *
162  * Parameters:
163  *
164  *      ld              LDAP descriptor
165  *      base            DN of the base object
166  *      scope           the search scope - one of
167  *                              LDAP_SCOPE_BASE (baseObject),
168  *                          LDAP_SCOPE_ONELEVEL (oneLevel),
169  *                              LDAP_SCOPE_SUBTREE (subtree), or
170  *                              LDAP_SCOPE_SUBORDINATE (children) -- OpenLDAP extension
171  *      filter          a string containing the search filter
172  *                      (e.g., "(|(cn=bob)(sn=bob))")
173  *      attrs           list of attribute types to return for matches
174  *      attrsonly       1 => attributes only 0 => attributes and values
175  *
176  * Example:
177  *      char    *attrs[] = { "mail", "title", 0 };
178  *      msgid = ldap_search( ld, "dc=example,dc=com", LDAP_SCOPE_SUBTREE, "cn~=bob",
179  *          attrs, attrsonly );
180  */
181 int
182 ldap_search(
183         LDAP *ld, LDAP_CONST char *base, int scope, LDAP_CONST char *filter,
184         char **attrs, int attrsonly )
185 {
186         BerElement      *ber;
187         ber_int_t       id;
188
189 #ifdef NEW_LOGGING
190         LDAP_LOG ( OPERATION, ENTRY, "ldap_search\n", 0, 0, 0 );
191 #else
192         Debug( LDAP_DEBUG_TRACE, "ldap_search\n", 0, 0, 0 );
193 #endif
194
195         assert( ld != NULL );
196         assert( LDAP_VALID( ld ) );
197
198         ber = ldap_build_search_req( ld, base, scope, filter, attrs,
199             attrsonly, NULL, NULL, -1, -1, &id ); 
200
201         if ( ber == NULL ) {
202                 return( -1 );
203         }
204
205
206         /* send the message */
207         return ( ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber, id ));
208 }
209
210
211 BerElement *
212 ldap_build_search_req(
213         LDAP *ld,
214         LDAP_CONST char *base,
215         ber_int_t scope,
216         LDAP_CONST char *filter,
217         char **attrs,
218         ber_int_t attrsonly,
219         LDAPControl **sctrls,
220         LDAPControl **cctrls,
221         ber_int_t timelimit,
222         ber_int_t sizelimit,
223         ber_int_t *idp)
224 {
225         BerElement      *ber;
226         int             err;
227
228         /*
229          * Create the search request.  It looks like this:
230          *      SearchRequest := [APPLICATION 3] SEQUENCE {
231          *              baseObject      DistinguishedName,
232          *              scope           ENUMERATED {
233          *                      baseObject      (0),
234          *                      singleLevel     (1),
235          *                      wholeSubtree    (2)
236          *              },
237          *              derefAliases    ENUMERATED {
238          *                      neverDerefaliases       (0),
239          *                      derefInSearching        (1),
240          *                      derefFindingBaseObj     (2),
241          *                      alwaysDerefAliases      (3)
242          *              },
243          *              sizelimit       INTEGER (0 .. 65535),
244          *              timelimit       INTEGER (0 .. 65535),
245          *              attrsOnly       BOOLEAN,
246          *              filter          Filter,
247          *              attributes      SEQUENCE OF AttributeType
248          *      }
249          * wrapped in an ldap message.
250          */
251
252         /* create a message to send */
253         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
254                 return( NULL );
255         }
256
257         if ( base == NULL ) {
258                 /* no base provided, use session default base */
259                 base = ld->ld_options.ldo_defbase;
260
261                 if ( base == NULL ) {
262                         /* no session default base, use top */
263                         base = "";
264                 }
265         }
266
267         LDAP_NEXT_MSGID( ld, *idp );
268 #ifdef LDAP_CONNECTIONLESS
269         if ( LDAP_IS_UDP(ld) ) {
270             err = ber_write( ber, ld->ld_options.ldo_peer,
271                     sizeof(struct sockaddr), 0);
272         }
273         if ( LDAP_IS_UDP(ld) && ld->ld_options.ldo_version == LDAP_VERSION2) {
274             char *dn = ld->ld_options.ldo_cldapdn;
275             if (!dn) dn = "";
276             err = ber_printf( ber, "{ist{seeiib", *idp, dn,
277                 LDAP_REQ_SEARCH, base, (ber_int_t) scope, ld->ld_deref,
278                 (sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
279                 (timelimit < 0) ? ld->ld_timelimit : timelimit,
280                 attrsonly );
281         } else
282 #endif
283         {
284             err = ber_printf( ber, "{it{seeiib", *idp,
285                 LDAP_REQ_SEARCH, base, (ber_int_t) scope, ld->ld_deref,
286                 (sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
287                 (timelimit < 0) ? ld->ld_timelimit : timelimit,
288                 attrsonly );
289         }
290
291         if ( err == -1 ) {
292                 ld->ld_errno = LDAP_ENCODING_ERROR;
293                 ber_free( ber, 1 );
294                 return( NULL );
295         }
296
297         if( filter == NULL ) {
298                 filter = "(objectclass=*)";
299         }
300
301         err = ldap_pvt_put_filter( ber, filter );
302
303         if ( err  == -1 ) {
304                 ld->ld_errno = LDAP_FILTER_ERROR;
305                 ber_free( ber, 1 );
306                 return( NULL );
307         }
308
309         if ( ber_printf( ber, /*{*/ "{v}N}", attrs ) == -1 ) {
310                 ld->ld_errno = LDAP_ENCODING_ERROR;
311                 ber_free( ber, 1 );
312                 return( NULL );
313         }
314
315         /* Put Server Controls */
316         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
317                 ber_free( ber, 1 );
318                 return( NULL );
319         }
320
321         if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
322                 ld->ld_errno = LDAP_ENCODING_ERROR;
323                 ber_free( ber, 1 );
324                 return( NULL );
325         }
326
327         return( ber );
328 }
329
330 int
331 ldap_search_st(
332         LDAP *ld, LDAP_CONST char *base, int scope,
333         LDAP_CONST char *filter, char **attrs,
334         int attrsonly, struct timeval *timeout, LDAPMessage **res )
335 {
336         int     msgid;
337
338         if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
339             == -1 )
340                 return( ld->ld_errno );
341
342         if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 )
343                 return( ld->ld_errno );
344
345         if ( ld->ld_errno == LDAP_TIMEOUT ) {
346                 (void) ldap_abandon( ld, msgid );
347                 ld->ld_errno = LDAP_TIMEOUT;
348                 return( ld->ld_errno );
349         }
350
351         return( ldap_result2error( ld, *res, 0 ) );
352 }
353
354 int
355 ldap_search_s(
356         LDAP *ld,
357         LDAP_CONST char *base,
358         int scope,
359         LDAP_CONST char *filter,
360         char **attrs,
361         int attrsonly,
362         LDAPMessage **res )
363 {
364         int     msgid;
365
366         if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
367             == -1 )
368                 return( ld->ld_errno );
369
370         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, res ) == -1 )
371                 return( ld->ld_errno );
372
373         return( ldap_result2error( ld, *res, 0 ) );
374 }
375