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