]> git.sur5r.net Git - openldap/blob - servers/slapd/search.c
Remove unused int i, j
[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         ber_int_t       scope, deref, attrsonly;
34         ber_int_t       sizelimit, timelimit;
35         struct berval base = { 0, NULL };
36         struct berval pbase = { 0, NULL };
37         struct berval nbase = { 0, NULL };
38         struct berval   fstr = { 0, NULL };
39         Filter          *filter = NULL;
40         AttributeName   an, *al = NULL, *alast, *anew;
41         Backend         *be;
42         int                     rc;
43         const char      *text;
44         int                     manageDSAit;
45
46 #ifdef NEW_LOGGING
47         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
48                 "do_search: conn %d\n", conn->c_connid ));
49 #else
50         Debug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );
51 #endif
52
53         /*
54          * Parse the search request.  It looks like this:
55          *
56          *      SearchRequest := [APPLICATION 3] SEQUENCE {
57          *              baseObject      DistinguishedName,
58          *              scope           ENUMERATED {
59          *                      baseObject      (0),
60          *                      singleLevel     (1),
61          *                      wholeSubtree    (2)
62          *              },
63          *              derefAliases    ENUMERATED {
64          *                      neverDerefaliases       (0),
65          *                      derefInSearching        (1),
66          *                      derefFindingBaseObj     (2),
67          *                      alwaysDerefAliases      (3)
68          *              },
69          *              sizelimit       INTEGER (0 .. 65535),
70          *              timelimit       INTEGER (0 .. 65535),
71          *              attrsOnly       BOOLEAN,
72          *              filter          Filter,
73          *              attributes      SEQUENCE OF AttributeType
74          *      }
75          */
76
77         /* baseObject, scope, derefAliases, sizelimit, timelimit, attrsOnly */
78         if ( ber_scanf( op->o_ber, "{oiiiib" /*}*/,
79                 &base, &scope, &deref, &sizelimit,
80             &timelimit, &attrsonly ) == LBER_ERROR )
81         {
82                 send_ldap_disconnect( conn, op,
83                         LDAP_PROTOCOL_ERROR, "decoding error" );
84                 rc = SLAPD_DISCONNECT;
85                 goto return_results;
86         }
87
88         switch( scope ) {
89         case LDAP_SCOPE_BASE:
90         case LDAP_SCOPE_ONELEVEL:
91         case LDAP_SCOPE_SUBTREE:
92                 break;
93         default:
94                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
95                         NULL, "invalid scope", NULL, NULL );
96                 goto return_results;
97         }
98
99         switch( deref ) {
100         case LDAP_DEREF_NEVER:
101         case LDAP_DEREF_FINDING:
102         case LDAP_DEREF_SEARCHING:
103         case LDAP_DEREF_ALWAYS:
104                 break;
105         default:
106                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
107                         NULL, "invalid deref", NULL, NULL );
108                 goto return_results;
109         }
110
111         rc = dnPrettyNormal( NULL, &base, &pbase, &nbase );
112         if( rc != LDAP_SUCCESS ) {
113 #ifdef NEW_LOGGING
114                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
115                         "do_search: conn %d  invalid dn (%s)\n",
116                         conn->c_connid, base.bv_val ));
117 #else
118                 Debug( LDAP_DEBUG_ANY,
119                         "do_search: invalid dn (%s)\n", base.bv_val, 0, 0 );
120 #endif
121                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
122                     "invalid DN", NULL, NULL );
123                 goto return_results;
124         }
125
126 #ifdef NEW_LOGGING
127         LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
128                 "do_search \"%s\" %d %d %d %d %d\n", base.bv_val, scope,
129                 deref, sizelimit, timelimit, attrsonly ));
130 #else
131         Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d",
132                 base.bv_val, scope, deref );
133         Debug( LDAP_DEBUG_ARGS, "    %d %d %d\n",
134                 sizelimit, timelimit, attrsonly);
135 #endif
136
137         /* filter - returns a "normalized" version */
138         rc = get_filter( conn, op->o_ber, &filter, &fstr, &text );
139         if( rc != LDAP_SUCCESS ) {
140                 if( rc == SLAPD_DISCONNECT ) {
141                         send_ldap_disconnect( conn, op,
142                                 LDAP_PROTOCOL_ERROR, text );
143                 } else {
144                         send_ldap_result( conn, op, rc,
145                                 NULL, text, NULL, NULL );
146                 }
147                 goto return_results;
148         }
149
150 #ifdef NEW_LOGGING
151         LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
152                 "do_search: conn %d     filter: %s\n", conn->c_connid, fstr.bv_val ));
153 #else
154         Debug( LDAP_DEBUG_ARGS, "    filter: %s\n", fstr.bv_val, 0, 0 );
155 #endif
156
157
158         /* attributes */
159         if ( ber_scanf( op->o_ber, "{" /*}*/ ) == LBER_ERROR ) {
160                 send_ldap_disconnect( conn, op,
161                         LDAP_PROTOCOL_ERROR, "decoding attrs error" );
162                 rc = SLAPD_DISCONNECT;
163                 goto return_results;
164         }
165         while ( ber_scanf( op->o_ber, "o", &an.an_name ) != LBER_ERROR) {
166                 anew = ch_malloc(sizeof(AttributeName));
167                 anew->an_next = NULL;
168                 anew->an_name = an.an_name;
169                 anew->an_desc = NULL;
170                 slap_bv2ad( &anew->an_name, &anew->an_desc, &text );
171                 if (!al) {
172                         al = anew;
173                 } else {
174                         alast->an_next = anew;
175                 }
176                 alast = anew;
177         }
178         if ( ber_scanf( op->o_ber, /*{{*/ "}}" ) == LBER_ERROR ) {
179                 send_ldap_disconnect( conn, op,
180                         LDAP_PROTOCOL_ERROR, "decoding attrs error" );
181                 rc = SLAPD_DISCONNECT;
182                 goto return_results;
183         }
184
185         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
186 #ifdef NEW_LOGGING
187                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
188                         "do_search: conn %d  get_ctrls failed (%d)\n",
189                         conn->c_connid, rc ));
190 #else
191                 Debug( LDAP_DEBUG_ANY, "do_search: get_ctrls failed\n", 0, 0, 0 );
192 #endif
193
194                 goto return_results;
195         } 
196
197         rc = LDAP_SUCCESS;
198
199 #ifdef NEW_LOGGING
200         LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
201                 "do_search: conn %d     attrs:", conn->c_connid ));
202 #else
203         Debug( LDAP_DEBUG_ARGS, "    attrs:", 0, 0, 0 );
204 #endif
205
206
207         if ( al != NULL ) {
208                 for ( anew = al; anew; anew=anew->an_next ) {
209 #ifdef NEW_LOGGING
210                         LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
211                                 "do_search:        %s", anew->an_name.bv_val ));
212 #else
213                         Debug( LDAP_DEBUG_ARGS, " %s", anew->an_name.bv_val, 0, 0 );
214 #endif
215
216                 }
217         }
218
219 #ifdef NEW_LOGGING
220         LDAP_LOG(( "operation", LDAP_LEVEL_ARGS, "\n" ));
221 #else
222         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
223 #endif
224
225         Statslog( LDAP_DEBUG_STATS,
226             "conn=%ld op=%d SRCH base=\"%s\" scope=%d filter=\"%s\"\n",
227             op->o_connid, op->o_opid, pbase.bv_val, scope, fstr.bv_val );
228
229         manageDSAit = get_manageDSAit( op );
230
231         if ( scope == LDAP_SCOPE_BASE ) {
232                 Entry *entry = NULL;
233
234                 if ( strcasecmp( nbase.bv_val, LDAP_ROOT_DSE ) == 0 ) {
235 #ifdef LDAP_CONNECTIONLESS
236                         /* Ignore LDAPv2 CLDAP DSE queries */
237                         if (op->o_protocol==LDAP_VERSION2 && conn->c_is_udp) {
238                                 goto return_results;
239                         }
240 #endif
241                         /* check restrictions */
242                         rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
243                         if( rc != LDAP_SUCCESS ) {
244                                 send_ldap_result( conn, op, rc,
245                                         NULL, text, NULL, NULL );
246                                 goto return_results;
247                         }
248
249                         rc = root_dse_info( conn, &entry, &text );
250                 }
251
252 #if defined( SLAPD_SCHEMA_DN )
253                 else if ( strcasecmp( nbase.bv_val, SLAPD_SCHEMA_DN ) == 0 ) {
254                         /* check restrictions */
255                         rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
256                         if( rc != LDAP_SUCCESS ) {
257                                 send_ldap_result( conn, op, rc,
258                                         NULL, text, NULL, NULL );
259                                 goto return_results;
260                         }
261
262                         rc = schema_info( &entry, &text );
263                 }
264 #endif
265
266                 if( rc != LDAP_SUCCESS ) {
267                         send_ldap_result( conn, op, rc,
268                                 NULL, text, NULL, NULL );
269                         goto return_results;
270
271                 } else if ( entry != NULL ) {
272                         rc = test_filter( NULL, conn, op,
273                                 entry, filter );
274
275                         if( rc == LDAP_COMPARE_TRUE ) {
276                                 send_search_entry( NULL, conn, op,
277                                         entry, al, attrsonly, NULL );
278                         }
279                         entry_free( entry );
280
281                         send_ldap_result( conn, op, LDAP_SUCCESS,
282                                 NULL, NULL, NULL, NULL );
283
284                         goto return_results;
285                 }
286         }
287
288         if( !nbase.bv_len && default_search_nbase.bv_len ) {
289                 ch_free( pbase.bv_val );
290                 ch_free( nbase.bv_val );
291
292                 ber_dupbv( &pbase, &default_search_base );
293                 ber_dupbv( &nbase, &default_search_nbase );
294         }
295
296         /*
297          * We could be serving multiple database backends.  Select the
298          * appropriate one, or send a referral to our "referral server"
299          * if we don't hold it.
300          */
301         if ( (be = select_backend( &nbase, manageDSAit, 1 )) == NULL ) {
302                 BVarray ref = referral_rewrite( default_referral,
303                         NULL, &pbase, scope );
304
305                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
306                         NULL, NULL, ref ? ref : default_referral, NULL );
307
308                 bvarray_free( ref );
309                 goto return_results;
310         }
311
312         /* check restrictions */
313         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
314         if( rc != LDAP_SUCCESS ) {
315                 send_ldap_result( conn, op, rc,
316                         NULL, text, NULL, NULL );
317                 goto return_results;
318         }
319
320         /* check for referrals */
321         rc = backend_check_referrals( be, conn, op, &pbase, &nbase );
322         if ( rc != LDAP_SUCCESS ) {
323                 goto return_results;
324         }
325
326         /* deref the base if needed */
327         suffix_alias( be, &nbase );
328
329         /* actually do the search and send the result(s) */
330         if ( be->be_search ) {
331                 (*be->be_search)( be, conn, op, &pbase, &nbase,
332                         scope, deref, sizelimit,
333                     timelimit, filter, &fstr, al, attrsonly );
334         } else {
335                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
336                         NULL, "operation not supported within namingContext", NULL, NULL );
337         }
338
339 return_results:;
340         free( base.bv_val );
341         if( pbase.bv_val != NULL) free( pbase.bv_val );
342         if( nbase.bv_val != NULL) free( nbase.bv_val );
343
344         if( fstr.bv_val != NULL) free( fstr.bv_val );
345         if( filter != NULL) filter_free( filter );
346         for (; al; al=anew ) {
347                 anew = al->an_next;
348                 free(al->an_name.bv_val);
349                 free(al);
350         }
351
352         return rc;
353 }