]> git.sur5r.net Git - openldap/blob - servers/slapd/search.c
197126f6a107ffec7c4be89ba8aac075db462bad
[openldap] / servers / slapd / search.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* Portions
7  * Copyright (c) 1995 Regents of the University of Michigan.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms are permitted
11  * provided that this notice is preserved and that due credit is given
12  * to the University of Michigan at Ann Arbor. The name of the University
13  * may not be used to endorse or promote products derived from this
14  * software without specific prior written permission. This software
15  * is provided ``as is'' without express or implied warranty.
16  */
17
18 #include "portable.h"
19
20 #include <stdio.h>
21
22 #include <ac/string.h>
23 #include <ac/socket.h>
24
25 #include "ldap_pvt.h"
26 #include "slap.h"
27
28 int
29 do_search(
30     Connection  *conn,  /* where to send results */
31     Operation   *op     /* info about the op to which we're responding */
32 ) {
33         int             i;
34         ber_int_t       scope, deref, attrsonly;
35         ber_int_t       sizelimit, timelimit;
36         struct berval base = { 0, NULL };
37         char            *nbase = NULL, *fstr = NULL;
38         Filter          *filter = NULL;
39         char            **attrs = NULL;
40         Backend         *be;
41         int                     rc;
42         const char      *text;
43         int                     manageDSAit;
44
45 #ifdef NEW_LOGGING
46         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
47                 "do_search: conn %d\n", conn->c_connid ));
48 #else
49         Debug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );
50 #endif
51
52         /*
53          * Parse the search request.  It looks like this:
54          *
55          *      SearchRequest := [APPLICATION 3] SEQUENCE {
56          *              baseObject      DistinguishedName,
57          *              scope           ENUMERATED {
58          *                      baseObject      (0),
59          *                      singleLevel     (1),
60          *                      wholeSubtree    (2)
61          *              },
62          *              derefAliases    ENUMERATED {
63          *                      neverDerefaliases       (0),
64          *                      derefInSearching        (1),
65          *                      derefFindingBaseObj     (2),
66          *                      alwaysDerefAliases      (3)
67          *              },
68          *              sizelimit       INTEGER (0 .. 65535),
69          *              timelimit       INTEGER (0 .. 65535),
70          *              attrsOnly       BOOLEAN,
71          *              filter          Filter,
72          *              attributes      SEQUENCE OF AttributeType
73          *      }
74          */
75
76         /* baseObject, scope, derefAliases, sizelimit, timelimit, attrsOnly */
77         if ( ber_scanf( op->o_ber, "{oiiiib" /*}*/,
78                 &base, &scope, &deref, &sizelimit,
79             &timelimit, &attrsonly ) == LBER_ERROR )
80         {
81                 send_ldap_disconnect( conn, op,
82                         LDAP_PROTOCOL_ERROR, "decoding error" );
83                 rc = SLAPD_DISCONNECT;
84                 goto return_results;
85         }
86
87         switch( scope ) {
88         case LDAP_SCOPE_BASE:
89         case LDAP_SCOPE_ONELEVEL:
90         case LDAP_SCOPE_SUBTREE:
91                 break;
92         default:
93                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
94                         NULL, "invalid scope", NULL, NULL );
95                 goto return_results;
96         }
97
98         switch( deref ) {
99         case LDAP_DEREF_NEVER:
100         case LDAP_DEREF_FINDING:
101         case LDAP_DEREF_SEARCHING:
102         case LDAP_DEREF_ALWAYS:
103                 break;
104         default:
105                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
106                         NULL, "invalid deref", NULL, NULL );
107                 goto return_results;
108         }
109
110         nbase = ch_strdup( base.bv_val );
111
112         if( dn_normalize( nbase ) == NULL ) {
113                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX,
114                         NULL, "invalid DN", NULL, NULL );
115                 goto return_results;
116         }
117
118 #ifdef NEW_LOGGING
119         LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
120                 "do_search \"%s\" %d %d %d %d %d\n", base.bv_val, scope,
121                 deref, sizelimit, timelimit, attrsonly ));
122 #else
123         Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d", base.bv_val, scope, deref );
124         Debug( LDAP_DEBUG_ARGS, "    %d %d %d\n", sizelimit, timelimit,
125             attrsonly);
126 #endif
127
128         /* filter - returns a "normalized" version */
129         rc = get_filter( conn, op->o_ber, &filter, &fstr, &text );
130         if( rc != LDAP_SUCCESS ) {
131                 if( rc == SLAPD_DISCONNECT ) {
132                         send_ldap_disconnect( conn, op,
133                                 LDAP_PROTOCOL_ERROR, text );
134                 } else {
135                         send_ldap_result( conn, op, rc,
136                                 NULL, text, NULL, NULL );
137                 }
138                 goto return_results;
139         }
140
141 #ifdef NEW_LOGGING
142         LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
143                 "do_search: conn %d     filter: %s\n", conn->c_connid, fstr ));
144 #else
145         Debug( LDAP_DEBUG_ARGS, "    filter: %s\n", fstr, 0, 0 );
146 #endif
147
148
149         /* attributes */
150         if ( ber_scanf( op->o_ber, /*{*/ "{v}}", &attrs ) == LBER_ERROR ) {
151                 send_ldap_disconnect( conn, op,
152                         LDAP_PROTOCOL_ERROR, "decoding attrs error" );
153                 rc = SLAPD_DISCONNECT;
154                 goto return_results;
155         }
156
157         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
158 #ifdef NEW_LOGGING
159                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
160                         "do_search: conn %d  get_ctrls failed (%d)\n",
161                         conn->c_connid, rc ));
162 #else
163                 Debug( LDAP_DEBUG_ANY, "do_search: get_ctrls failed\n", 0, 0, 0 );
164 #endif
165
166                 goto return_results;
167         } 
168
169         rc = LDAP_SUCCESS;
170
171 #ifdef NEW_LOGGING
172         LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
173                 "do_search: conn %d     attrs:", conn->c_connid ));
174 #else
175         Debug( LDAP_DEBUG_ARGS, "    attrs:", 0, 0, 0 );
176 #endif
177
178
179         if ( attrs != NULL ) {
180                 for ( i = 0; attrs[i] != NULL; i++ ) {
181 #ifdef NEW_LOGGING
182                         LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
183                                 "do_search:        %s", attrs[i] ));
184 #else
185                         Debug( LDAP_DEBUG_ARGS, " %s", attrs[i], 0, 0 );
186 #endif
187
188                 }
189         }
190
191 #ifdef NEW_LOGGING
192         LDAP_LOG(( "operation", LDAP_LEVEL_ARGS, "\n" ));
193 #else
194         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
195 #endif
196
197         Statslog( LDAP_DEBUG_STATS,
198             "conn=%ld op=%d SRCH base=\"%s\" scope=%d filter=\"%s\"\n",
199             op->o_connid, op->o_opid, base.bv_val, scope, fstr );
200
201         manageDSAit = get_manageDSAit( op );
202
203         if ( scope == LDAP_SCOPE_BASE ) {
204                 Entry *entry = NULL;
205
206                 if ( strcasecmp( nbase, LDAP_ROOT_DSE ) == 0 ) {
207 #ifdef LDAP_CONNECTIONLESS
208                         /* Ignore LDAPv2 CLDAP DSE queries */
209                         if (op->o_protocol==LDAP_VERSION2 && conn->c_is_udp) {
210                                 goto return_results;
211                         }
212 #endif
213                         /* check restrictions */
214                         rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
215                         if( rc != LDAP_SUCCESS ) {
216                                 send_ldap_result( conn, op, rc,
217                                         NULL, text, NULL, NULL );
218                                 goto return_results;
219                         }
220
221                         rc = root_dse_info( conn, &entry, &text );
222                 }
223
224 #if defined( SLAPD_SCHEMA_DN )
225                 else if ( strcasecmp( nbase, SLAPD_SCHEMA_DN ) == 0 ) {
226                         /* check restrictions */
227                         rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
228                         if( rc != LDAP_SUCCESS ) {
229                                 send_ldap_result( conn, op, rc,
230                                         NULL, text, NULL, NULL );
231                                 goto return_results;
232                         }
233
234                         rc = schema_info( &entry, &text );
235                 }
236 #endif
237
238                 if( rc != LDAP_SUCCESS ) {
239                         send_ldap_result( conn, op, rc,
240                                 NULL, text, NULL, NULL );
241                         goto return_results;
242
243                 } else if ( entry != NULL ) {
244                         rc = test_filter( NULL, conn, op,
245                                 entry, filter );
246
247                         if( rc == LDAP_COMPARE_TRUE ) {
248                                 send_search_entry( NULL, conn, op,
249                                         entry, attrs, attrsonly, NULL );
250                         }
251                         entry_free( entry );
252
253                         send_ldap_result( conn, op, LDAP_SUCCESS,
254                                 NULL, NULL, NULL, NULL );
255
256                         goto return_results;
257                 }
258         }
259
260         if( nbase[0] == '\0' && default_search_nbase != NULL ) {
261                 ch_free( base.bv_val );
262                 ch_free( nbase );
263                 base.bv_val = ch_strdup( default_search_base );
264                 base.bv_len = strlen( default_search_nbase );
265                 nbase = ch_strdup( default_search_nbase );
266         }
267
268         /*
269          * We could be serving multiple database backends.  Select the
270          * appropriate one, or send a referral to our "referral server"
271          * if we don't hold it.
272          */
273         if ( (be = select_backend( nbase, manageDSAit, 1 )) == NULL ) {
274                 struct berval **ref = referral_rewrite( default_referral,
275                         NULL, base.bv_val, scope );
276
277                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
278                         NULL, NULL, ref ? ref : default_referral, NULL );
279
280                 ber_bvecfree( ref );
281                 goto return_results;
282         }
283
284         /* check restrictions */
285         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
286         if( rc != LDAP_SUCCESS ) {
287                 send_ldap_result( conn, op, rc,
288                         NULL, text, NULL, NULL );
289                 goto return_results;
290         }
291
292         /* check for referrals */
293         rc = backend_check_referrals( be, conn, op, base.bv_val, nbase );
294         if ( rc != LDAP_SUCCESS ) {
295                 goto return_results;
296         }
297
298         /* deref the base if needed */
299         nbase = suffix_alias( be, nbase );
300
301         /* actually do the search and send the result(s) */
302         if ( be->be_search ) {
303                 (*be->be_search)( be, conn, op, base.bv_val, nbase,
304                         scope, deref, sizelimit,
305                     timelimit, filter, fstr, attrs, attrsonly );
306         } else {
307                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
308                         NULL, "operation not supported within namingContext", NULL, NULL );
309         }
310
311 return_results:;
312         free( base.bv_val );
313         if( nbase != NULL) free( nbase );
314         if( fstr != NULL) free( fstr );
315         if( filter != NULL) filter_free( filter );
316         if ( attrs != NULL ) {
317                 charray_free( attrs );
318         }
319
320         return rc;
321 }