]> git.sur5r.net Git - openldap/blob - servers/slapd/search.c
Don't reeval expression
[openldap] / servers / slapd / search.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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 #include "slapi_common.h"
20
21 #include <stdio.h>
22
23 #include <ac/string.h>
24 #include <ac/socket.h>
25
26 #include "ldap_pvt.h"
27 #include "lutil.h"
28 #include "slap.h"
29 #include "slapi.h"
30
31 int
32 do_search(
33     Connection  *conn,  /* where to send results */
34     Operation   *op     /* info about the op to which we're responding */
35 ) {
36         ber_int_t       scope, deref, attrsonly;
37         ber_int_t       sizelimit, timelimit;
38         struct berval base = { 0, NULL };
39         struct berval pbase = { 0, NULL };
40         struct berval nbase = { 0, NULL };
41         struct berval   fstr = { 0, NULL };
42         Filter          *filter = NULL;
43         AttributeName   *an = NULL;
44         ber_len_t       siz, off, i;
45         Backend         *be;
46         int                     rc;
47         const char      *text;
48         int                     manageDSAit;
49
50         Slapi_PBlock *pb = op->o_pb;
51
52 #ifdef NEW_LOGGING
53         LDAP_LOG( OPERATION, ENTRY, "do_search: conn %d\n", conn->c_connid, 0, 0 );
54 #else
55         Debug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );
56 #endif
57
58         /*
59          * Parse the search request.  It looks like this:
60          *
61          *      SearchRequest := [APPLICATION 3] SEQUENCE {
62          *              baseObject      DistinguishedName,
63          *              scope           ENUMERATED {
64          *                      baseObject      (0),
65          *                      singleLevel     (1),
66          *                      wholeSubtree    (2)
67          *              },
68          *              derefAliases    ENUMERATED {
69          *                      neverDerefaliases       (0),
70          *                      derefInSearching        (1),
71          *                      derefFindingBaseObj     (2),
72          *                      alwaysDerefAliases      (3)
73          *              },
74          *              sizelimit       INTEGER (0 .. 65535),
75          *              timelimit       INTEGER (0 .. 65535),
76          *              attrsOnly       BOOLEAN,
77          *              filter          Filter,
78          *              attributes      SEQUENCE OF AttributeType
79          *      }
80          */
81
82         /* baseObject, scope, derefAliases, sizelimit, timelimit, attrsOnly */
83         if ( ber_scanf( op->o_ber, "{miiiib" /*}*/,
84                 &base, &scope, &deref, &sizelimit,
85             &timelimit, &attrsonly ) == LBER_ERROR )
86         {
87                 send_ldap_disconnect( conn, op,
88                         LDAP_PROTOCOL_ERROR, "decoding error" );
89                 rc = SLAPD_DISCONNECT;
90                 goto return_results;
91         }
92
93         switch( scope ) {
94         case LDAP_SCOPE_BASE:
95         case LDAP_SCOPE_ONELEVEL:
96         case LDAP_SCOPE_SUBTREE:
97                 break;
98         default:
99                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
100                         NULL, "invalid scope", NULL, NULL );
101                 goto return_results;
102         }
103
104         switch( deref ) {
105         case LDAP_DEREF_NEVER:
106         case LDAP_DEREF_FINDING:
107         case LDAP_DEREF_SEARCHING:
108         case LDAP_DEREF_ALWAYS:
109                 break;
110         default:
111                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
112                         NULL, "invalid deref", NULL, NULL );
113                 goto return_results;
114         }
115
116         rc = dnPrettyNormal( NULL, &base, &pbase, &nbase );
117         if( rc != LDAP_SUCCESS ) {
118 #ifdef NEW_LOGGING
119                 LDAP_LOG( OPERATION, ERR, 
120                         "do_search: conn %d  invalid dn (%s)\n",
121                         conn->c_connid, base.bv_val, 0 );
122 #else
123                 Debug( LDAP_DEBUG_ANY,
124                         "do_search: invalid dn (%s)\n", base.bv_val, 0, 0 );
125 #endif
126                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
127                     "invalid DN", NULL, NULL );
128                 goto return_results;
129         }
130
131 #ifdef NEW_LOGGING
132         LDAP_LOG( OPERATION, ARGS, "SRCH \"%s\" %d %d",
133                 base.bv_val, scope, deref );
134         LDAP_LOG( OPERATION, ARGS, "    %d %d %d\n",
135                 sizelimit, timelimit, attrsonly);
136 #else
137         Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d",
138                 base.bv_val, scope, deref );
139         Debug( LDAP_DEBUG_ARGS, "    %d %d %d\n",
140                 sizelimit, timelimit, attrsonly);
141 #endif
142
143         /* filter - returns a "normalized" version */
144         rc = get_filter( conn, op->o_ber, &filter, &text );
145         if( rc != LDAP_SUCCESS ) {
146                 if( rc == SLAPD_DISCONNECT ) {
147                         send_ldap_disconnect( conn, op,
148                                 LDAP_PROTOCOL_ERROR, text );
149                 } else {
150                         send_ldap_result( conn, op, rc, 
151                                         NULL, text, NULL, NULL );
152                 }
153                 goto return_results;
154         }
155         filter2bv( filter, &fstr );
156
157 #ifdef NEW_LOGGING
158         LDAP_LOG( OPERATION, ARGS, 
159                 "do_search: conn %d     filter: %s\n", 
160                 conn->c_connid, fstr.bv_len ? fstr.bv_val : "empty", 0 );
161 #else
162         Debug( LDAP_DEBUG_ARGS, "    filter: %s\n",
163                 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
164 #endif
165
166         /* attributes */
167         siz = sizeof(AttributeName);
168         off = 0;
169         if ( ber_scanf( op->o_ber, "{M}}", &an, &siz, off ) == LBER_ERROR ) {
170                 send_ldap_disconnect( conn, op,
171                         LDAP_PROTOCOL_ERROR, "decoding attrs error" );
172                 rc = SLAPD_DISCONNECT;
173                 goto return_results;
174         }
175         for ( i=0; i<siz; i++ ) {
176                 an[i].an_desc = NULL;
177                 an[i].an_oc = NULL;
178                 slap_bv2ad(&an[i].an_name, &an[i].an_desc, &text);
179         }
180
181         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
182 #ifdef NEW_LOGGING
183                 LDAP_LOG( OPERATION, INFO, 
184                         "do_search: conn %d  get_ctrls failed (%d)\n",
185                         conn->c_connid, rc, 0 );
186 #else
187                 Debug( LDAP_DEBUG_ANY, "do_search: get_ctrls failed\n", 0, 0, 0 );
188 #endif
189
190                 goto return_results;
191         }
192
193 #ifdef NEW_LOGGING
194         LDAP_LOG( OPERATION, ARGS, 
195                 "do_search: conn %d     attrs:", conn->c_connid, 0, 0 );
196 #else
197         Debug( LDAP_DEBUG_ARGS, "    attrs:", 0, 0, 0 );
198 #endif
199
200         if ( siz != 0 ) {
201                 for ( i = 0; i<siz; i++ ) {
202 #ifdef NEW_LOGGING
203                         LDAP_LOG( OPERATION, ARGS, 
204                                 "do_search: %s", an[i].an_name.bv_val, 0, 0 );
205 #else
206                         Debug( LDAP_DEBUG_ARGS, " %s", an[i].an_name.bv_val, 0, 0 );
207 #endif
208                 }
209         }
210
211 #ifdef NEW_LOGGING
212         LDAP_LOG( OPERATION, ARGS, "\n" , 0, 0, 0 );
213 #else
214         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
215 #endif
216
217         if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
218                 char abuf[BUFSIZ/2], *ptr = abuf;
219                 int len = 0;
220
221                 Statslog( LDAP_DEBUG_STATS,
222                         "conn=%lu op=%lu SRCH base=\"%s\" scope=%d filter=\"%s\"\n",
223                         op->o_connid, op->o_opid, pbase.bv_val, scope, fstr.bv_val );
224
225                 for ( i = 0; i<siz; i++ ) {
226                         if (len + 1 + an[i].an_name.bv_len > sizeof(abuf)) {
227                                 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu SRCH attr=%s\n",
228                                     op->o_connid, op->o_opid, abuf, 0, 0 );
229                                 len = 0;
230                                 ptr = abuf;
231                         }
232                         if (len) {
233                                 *ptr++ = ' ';
234                                 len++;
235                         }
236                         ptr = lutil_strcopy(ptr, an[i].an_name.bv_val);
237                         len += an[i].an_name.bv_len;
238                 }
239                 if (len) {
240                         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu SRCH attr=%s\n",
241                                 op->o_connid, op->o_opid, abuf, 0, 0 );
242                 }
243         }
244
245         manageDSAit = get_manageDSAit( op );
246
247         if ( scope == LDAP_SCOPE_BASE ) {
248                 Entry *entry = NULL;
249
250                 if ( nbase.bv_len == 0 ) {
251 #ifdef LDAP_CONNECTIONLESS
252                         /* Ignore LDAPv2 CLDAP Root DSE queries */
253                         if (op->o_protocol==LDAP_VERSION2 && conn->c_is_udp) {
254                                 goto return_results;
255                         }
256 #endif
257                         /* check restrictions */
258                         rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
259                         if( rc != LDAP_SUCCESS ) {
260                                 send_ldap_result( conn, op, rc,
261                                         NULL, text, NULL, NULL );
262                                 goto return_results;
263                         }
264
265                         rc = root_dse_info( conn, &entry, &text );
266
267                 } else if ( bvmatch( &nbase, &global_schemandn ) ) {
268                         /* check restrictions */
269                         rc = backend_check_restrictions( NULL, conn, op, NULL, &text ) ;
270                         if( rc != LDAP_SUCCESS ) {
271                                 send_ldap_result( conn, op, rc,
272                                         NULL, text, NULL, NULL );
273                                 goto return_results;
274                         }
275
276                         rc = schema_info( &entry, &text );
277                 }
278
279                 if( rc != LDAP_SUCCESS ) {
280                         send_ldap_result( conn, op, rc,
281                                 NULL, text, NULL, NULL );
282                         goto return_results;
283
284                 } else if ( entry != NULL ) {
285                         rc = test_filter( NULL, conn, op,
286                                 entry, filter );
287
288                         if( rc == LDAP_COMPARE_TRUE ) {
289                                 send_search_entry( NULL, conn, op,
290                                         entry, an, attrsonly, NULL );
291                         }
292                         entry_free( entry );
293
294                         send_ldap_result( conn, op, LDAP_SUCCESS,
295                                 NULL, NULL, NULL, NULL );
296
297                         goto return_results;
298                 }
299         }
300
301         if( !nbase.bv_len && default_search_nbase.bv_len ) {
302                 ch_free( pbase.bv_val );
303                 ch_free( nbase.bv_val );
304
305                 ber_dupbv( &pbase, &default_search_base );
306                 ber_dupbv( &nbase, &default_search_nbase );
307         }
308
309         /*
310          * We could be serving multiple database backends.  Select the
311          * appropriate one, or send a referral to our "referral server"
312          * if we don't hold it.
313          */
314         if ( (be = select_backend( &nbase, manageDSAit, 1 )) == NULL ) {
315                 BerVarray ref = referral_rewrite( default_referral,
316                         NULL, &pbase, scope );
317
318                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
319                         NULL, NULL, ref ? ref : default_referral, NULL );
320
321                 ber_bvarray_free( ref );
322                 goto return_results;
323         }
324
325         /* check restrictions */
326         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
327         if( rc != LDAP_SUCCESS ) {
328                 send_ldap_result( conn, op, rc,
329                         NULL, text, NULL, NULL );
330                 goto return_results;
331         }
332
333         /* check for referrals */
334         rc = backend_check_referrals( be, conn, op, &pbase, &nbase );
335         if ( rc != LDAP_SUCCESS ) {
336                 goto return_results;
337         }
338
339         /* deref the base if needed */
340         suffix_alias( be, &nbase );
341
342 #if defined( LDAP_SLAPI )
343         slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
344         slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
345         slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
346         slapi_pblock_set( pb, SLAPI_BIND_TARGET, (void *)base.bv_val );
347         slapi_pblock_set( pb, SLAPI_SEARCH_SCOPE, (void *)scope );
348         slapi_pblock_set( pb, SLAPI_SEARCH_DEREF, (void *)deref );
349         slapi_pblock_set( pb, SLAPI_SEARCH_SIZELIMIT, (void *)sizelimit );
350         slapi_pblock_set( pb, SLAPI_SEARCH_TIMELIMIT, (void *)timelimit );
351         slapi_pblock_set( pb, SLAPI_SEARCH_FILTER, (void *)filter );
352         slapi_pblock_set( pb, SLAPI_SEARCH_STRFILTER, (void *)fstr.bv_val );
353         slapi_pblock_set( pb, SLAPI_SEARCH_ATTRSONLY, (void *)attrsonly );
354         slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
355         slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)(1) );
356
357         rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_SEARCH_FN, pb );
358         if ( rc != 0 && rc != LDAP_OTHER ) {
359                 /*
360                  * either there is no preOp (search) plugins
361                  * or a plugin failed. Just log it
362                  *
363                  * FIXME: is this correct?
364                  */
365 #ifdef NEW_LOGGING
366                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO, "do_search: search preOps failed\n"));
367 #else
368                 Debug(LDAP_DEBUG_TRACE, "search preOps failed.\n", 0, 0, 0);
369 #endif
370     }
371 #endif /* defined( LDAP_SLAPI ) */
372
373         /* actually do the search and send the result(s) */
374         if ( be->be_search ) {
375                 (*be->be_search)( be, conn, op, &pbase, &nbase,
376                         scope, deref, sizelimit,
377                         timelimit, filter, &fstr, an, attrsonly );
378         } else {
379                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
380                         NULL, "operation not supported within namingContext",
381                         NULL, NULL );
382         }
383
384 #if defined( LDAP_SLAPI )
385         rc = doPluginFNs( be, SLAPI_PLUGIN_POST_SEARCH_FN, pb );
386         if ( rc != 0 && rc != LDAP_OTHER ) {
387                 /*
388                  * either there is no postOp (search) plugins
389                  * or a plugin failed. Just log it
390                  *
391                  * FIXME: is this correct?
392                  */
393 #ifdef NEW_LOGGING
394                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO, "do_search: search postOps failed\n"));
395 #else
396                 Debug (LDAP_DEBUG_TRACE, " search postOps failed.\n", 0, 0, 0);
397 #endif
398     }
399 #endif /* defined( LDAP_SLAPI ) */
400
401 return_results:;
402 #ifdef LDAP_CLIENT_UPDATE
403         if ( !( op->o_clientupdate_type & SLAP_LCUP_PERSIST ) )
404 #endif /* LDAP_CLIENT_UPDATE */
405         {
406                 if( pbase.bv_val != NULL) free( pbase.bv_val );
407                 if( nbase.bv_val != NULL) free( nbase.bv_val );
408
409                 if( fstr.bv_val != NULL) free( fstr.bv_val );
410                 if( filter != NULL) filter_free( filter );
411                 if( an != NULL ) free( an );
412         }
413
414         return rc;
415 }