]> git.sur5r.net Git - openldap/blob - servers/slapd/search.c
30532922b9522a66837d07b8453bf5dbd5927bb0
[openldap] / servers / slapd / 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) 1995 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25
26 #include "portable.h"
27
28 #include <stdio.h>
29
30 #include <ac/string.h>
31 #include <ac/socket.h>
32
33 #include "ldap_pvt.h"
34 #include "lutil.h"
35 #include "slap.h"
36
37 #ifdef LDAP_SLAPI
38 #include "slapi/slapi.h"
39
40 static void init_search_pblock( Operation *op, char **attrs, int managedsait );
41 static int call_search_preop_plugins( Operation *op );
42 static int call_search_rewrite_plugins( Operation *op );
43 static void call_search_postop_plugins( Operation *op );
44 #endif /* LDAPI_SLAPI */
45
46 int
47 do_search(
48     Operation   *op,    /* info about the op to which we're responding */
49     SlapReply   *rs     /* all the response data we'll send */ )
50 {
51         struct berval base = BER_BVNULL;
52         ber_len_t       siz, off, i;
53
54         Debug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );
55
56         /*
57          * Parse the search request.  It looks like this:
58          *
59          *      SearchRequest := [APPLICATION 3] SEQUENCE {
60          *              baseObject      DistinguishedName,
61          *              scope           ENUMERATED {
62          *                      baseObject      (0),
63          *                      singleLevel     (1),
64          *                      wholeSubtree (2),
65          *          subordinate (3)  -- OpenLDAP extension
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, &op->ors_scope, &op->ors_deref, &op->ors_slimit,
84             &op->ors_tlimit, &op->ors_attrsonly ) == LBER_ERROR )
85         {
86                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
87                 rs->sr_err = SLAPD_DISCONNECT;
88                 goto return_results;
89         }
90
91         if ( op->ors_tlimit < 0 || op->ors_tlimit > SLAP_MAX_LIMIT ) {
92                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid time limit" );
93                 goto return_results;
94         }
95
96         if ( op->ors_slimit < 0 || op->ors_slimit > SLAP_MAX_LIMIT ) {
97                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid size limit" );
98                 goto return_results;
99         }
100
101         switch( op->ors_scope ) {
102         case LDAP_SCOPE_BASE:
103         case LDAP_SCOPE_ONELEVEL:
104         case LDAP_SCOPE_SUBTREE:
105 #ifdef LDAP_SCOPE_SUBORDINATE
106         case LDAP_SCOPE_SUBORDINATE:
107 #endif
108                 break;
109         default:
110                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid scope" );
111                 goto return_results;
112         }
113
114         switch( op->ors_deref ) {
115         case LDAP_DEREF_NEVER:
116         case LDAP_DEREF_FINDING:
117         case LDAP_DEREF_SEARCHING:
118         case LDAP_DEREF_ALWAYS:
119                 break;
120         default:
121                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid deref" );
122                 goto return_results;
123         }
124
125         rs->sr_err = dnPrettyNormal( NULL, &base, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
126         if( rs->sr_err != LDAP_SUCCESS ) {
127                 Debug( LDAP_DEBUG_ANY,
128                         "do_search: invalid dn (%s)\n", base.bv_val, 0, 0 );
129                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
130                 goto return_results;
131         }
132
133         Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d",
134                 base.bv_val, op->ors_scope, op->ors_deref );
135         Debug( LDAP_DEBUG_ARGS, "    %d %d %d\n",
136                 op->ors_slimit, op->ors_tlimit, op->ors_attrsonly);
137
138         /* filter - returns a "normalized" version */
139         rs->sr_err = get_filter( op, op->o_ber, &op->ors_filter, &rs->sr_text );
140         if( rs->sr_err != LDAP_SUCCESS ) {
141                 if( rs->sr_err == SLAPD_DISCONNECT ) {
142                         rs->sr_err = LDAP_PROTOCOL_ERROR;
143                         send_ldap_disconnect( op, rs );
144                         rs->sr_err = SLAPD_DISCONNECT;
145                 } else {
146                         send_ldap_result( op, rs );
147                 }
148                 goto return_results;
149         }
150         filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
151
152         Debug( LDAP_DEBUG_ARGS, "    filter: %s\n",
153                 !BER_BVISEMPTY( &op->ors_filterstr ) ? op->ors_filterstr.bv_val : "empty", 0, 0 );
154
155         /* attributes */
156         siz = sizeof(AttributeName);
157         off = offsetof(AttributeName,an_name);
158         if ( ber_scanf( op->o_ber, "{M}}", &op->ors_attrs, &siz, off ) == LBER_ERROR ) {
159                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding attrs error" );
160                 rs->sr_err = SLAPD_DISCONNECT;
161                 goto return_results;
162         }
163         for ( i=0; i<siz; i++ ) {
164                 const char *dummy;      /* ignore msgs from bv2ad */
165                 op->ors_attrs[i].an_desc = NULL;
166                 op->ors_attrs[i].an_oc = NULL;
167                 op->ors_attrs[i].an_oc_exclude = 0;
168                 slap_bv2ad(&op->ors_attrs[i].an_name, &op->ors_attrs[i].an_desc, &dummy);
169         }
170
171         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
172                 Debug( LDAP_DEBUG_ANY, "do_search: get_ctrls failed\n", 0, 0, 0 );
173
174                 goto return_results;
175         }
176
177         Debug( LDAP_DEBUG_ARGS, "    attrs:", 0, 0, 0 );
178
179         if ( siz != 0 ) {
180                 for ( i = 0; i<siz; i++ ) {
181                         Debug( LDAP_DEBUG_ARGS, " %s", op->ors_attrs[i].an_name.bv_val, 0, 0 );
182                 }
183         }
184
185         Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
186
187         if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
188                 char abuf[BUFSIZ/2], *ptr = abuf;
189                 int len = 0, alen;
190
191                 sprintf(abuf, "scope=%d deref=%d", op->ors_scope, op->ors_deref);
192                 Statslog( LDAP_DEBUG_STATS,
193                         "conn=%lu op=%lu SRCH base=\"%s\" %s filter=\"%s\"\n",
194                         op->o_connid, op->o_opid, op->o_req_dn.bv_val, abuf,
195                         op->ors_filterstr.bv_val );
196
197                 for ( i = 0; i<siz; i++ ) {
198                         alen = op->ors_attrs[i].an_name.bv_len;
199                         if (alen >= sizeof(abuf)) {
200                                 alen = sizeof(abuf)-1;
201                         }
202                         if (len && (len + 1 + alen >= sizeof(abuf))) {
203                                 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu SRCH attr=%s\n",
204                                     op->o_connid, op->o_opid, abuf, 0, 0 );
205                                 len = 0;
206                                 ptr = abuf;
207                         }
208                         if (len) {
209                                 *ptr++ = ' ';
210                                 len++;
211                         }
212                         ptr = lutil_strncopy(ptr, op->ors_attrs[i].an_name.bv_val, alen);
213                         len += alen;
214                         *ptr = '\0';
215                 }
216                 if (len) {
217                         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu SRCH attr=%s\n",
218                                 op->o_connid, op->o_opid, abuf, 0, 0 );
219                 }
220         }
221
222         op->o_bd = frontendDB;
223         rs->sr_err = frontendDB->be_search( op, rs );
224
225 return_results:;
226         if ( ( op->o_sync_mode & SLAP_SYNC_PERSIST ) ) {
227                 return rs->sr_err;
228         }
229         if ( ( op->o_sync_slog_size != -1 ) ) {
230                 return rs->sr_err;
231         }
232         if ( !BER_BVISNULL( &op->o_req_dn ) ) {
233                 slap_sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
234         }
235         if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
236                 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
237         }
238         if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
239                 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
240         }
241         if ( op->ors_filter != NULL) {
242                 filter_free_x( op, op->ors_filter );
243         }
244         if ( op->ors_attrs != NULL ) {
245                 op->o_tmpfree( op->ors_attrs, op->o_tmpmemctx );
246         }
247
248         return rs->sr_err;
249 }
250
251 int
252 fe_op_search( Operation *op, SlapReply *rs )
253 {
254         int                     manageDSAit;
255         int                     be_manageDSAit;
256 #ifdef LDAP_SLAPI
257         char                    **attrs = NULL;
258 #endif
259
260         manageDSAit = get_manageDSAit( op );
261
262         /* fake while loop to allow breaking out */
263         while ( op->ors_scope == LDAP_SCOPE_BASE ) {
264                 Entry *entry = NULL;
265
266                 if ( BER_BVISEMPTY( &op->o_req_ndn ) ) {
267 #ifdef LDAP_CONNECTIONLESS
268                         /* Ignore LDAPv2 CLDAP Root DSE queries */
269                         if (op->o_protocol == LDAP_VERSION2 && op->o_conn->c_is_udp) {
270                                 goto return_results;
271                         }
272 #endif
273                         /* check restrictions */
274                         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
275                                 send_ldap_result( op, rs );
276                                 goto return_results;
277                         }
278
279 #ifdef LDAP_SLAPI
280                         if ( op->o_pb ) {
281                                 attrs = anlist2charray_x( op->ors_attrs, 0, op->o_tmpmemctx );
282                                 init_search_pblock( op, attrs, manageDSAit );
283                                 rs->sr_err = call_search_preop_plugins( op );
284                                 if ( rs->sr_err ) break;
285                                 call_search_rewrite_plugins( op );
286                         }
287 #endif /* LDAP_SLAPI */
288                         rs->sr_err = root_dse_info( op->o_conn, &entry, &rs->sr_text );
289
290                 } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
291                         /* check restrictions */
292                         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
293                                 send_ldap_result( op, rs );
294                                 goto return_results;
295                         }
296
297 #ifdef LDAP_SLAPI
298                         if ( op->o_pb ) {
299                                 attrs = anlist2charray_x( op->ors_attrs, 0, op->o_tmpmemctx );
300                                 init_search_pblock( op, attrs, manageDSAit );
301                                 rs->sr_err = call_search_preop_plugins( op );
302                                 if ( rs->sr_err ) break;
303                                 call_search_rewrite_plugins( op );
304                         }
305 #endif /* LDAP_SLAPI */
306                         rs->sr_err = schema_info( &entry, &rs->sr_text );
307                 }
308
309                 if( rs->sr_err != LDAP_SUCCESS ) {
310                         send_ldap_result( op, rs );
311 #ifdef LDAP_SLAPI
312                         if ( op->o_pb ) call_search_postop_plugins( op );
313 #endif /* LDAP_SLAPI */
314                         goto return_results;
315
316                 } else if ( entry != NULL ) {
317                         rs->sr_err = test_filter( op, entry, op->ors_filter );
318
319                         if( rs->sr_err == LDAP_COMPARE_TRUE ) {
320                                 rs->sr_entry = entry;
321                                 rs->sr_attrs = op->ors_attrs;
322                                 rs->sr_operational_attrs = NULL;
323                                 send_search_entry( op, rs );
324                                 rs->sr_entry = NULL;
325                                 rs->sr_operational_attrs = NULL;
326                         }
327                         entry_free( entry );
328
329                         rs->sr_err = LDAP_SUCCESS;
330                         send_ldap_result( op, rs );
331 #ifdef LDAP_SLAPI
332                         if ( op->o_pb ) call_search_postop_plugins( op );
333 #endif /* LDAP_SLAPI */
334                         goto return_results;
335                 }
336                 break;
337         }
338
339         if( BER_BVISEMPTY( &op->o_req_ndn ) && !BER_BVISEMPTY( &default_search_nbase ) ) {
340                 slap_sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
341                 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
342
343                 ber_dupbv_x( &op->o_req_dn, &default_search_base, op->o_tmpmemctx );
344                 ber_dupbv_x( &op->o_req_ndn, &default_search_nbase, op->o_tmpmemctx );
345         }
346
347         /*
348          * We could be serving multiple database backends.  Select the
349          * appropriate one, or send a referral to our "referral server"
350          * if we don't hold it.
351          */
352
353         /* Sync control overrides manageDSAit */
354
355         if ( manageDSAit != SLAP_NO_CONTROL ) {
356                 if ( op->o_sync_mode & SLAP_SYNC_REFRESH ) {
357                         be_manageDSAit = SLAP_NO_CONTROL;
358                 } else {
359                         be_manageDSAit = manageDSAit;
360                 }
361         } else {
362                 be_manageDSAit = manageDSAit;
363         }
364
365         op->o_bd = select_backend( &op->o_req_ndn, be_manageDSAit, 1 );
366         if ( op->o_bd == NULL ) {
367                 rs->sr_ref = referral_rewrite( default_referral,
368                         NULL, &op->o_req_dn, op->ors_scope );
369
370                 if (!rs->sr_ref) rs->sr_ref = default_referral;
371                 rs->sr_err = LDAP_REFERRAL;
372                 send_ldap_result( op, rs );
373
374                 if (rs->sr_ref != default_referral)
375                 ber_bvarray_free( rs->sr_ref );
376                 rs->sr_ref = NULL;
377                 goto return_results;
378         }
379
380         /* check restrictions */
381         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
382                 send_ldap_result( op, rs );
383                 goto return_results;
384         }
385
386         /* check for referrals */
387         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
388                 goto return_results;
389         }
390
391 #ifdef LDAP_SLAPI
392         if ( op->o_pb ) {
393                 attrs = anlist2charray_x( op->ors_attrs, 0, op->o_tmpmemctx );
394                 init_search_pblock( op, attrs, manageDSAit );
395                 rs->sr_err = call_search_preop_plugins( op );
396                 if ( rs->sr_err != LDAP_SUCCESS ) {
397                         goto return_results;
398                 }
399
400                 call_search_rewrite_plugins( op );
401         }
402 #endif /* LDAP_SLAPI */
403
404         /* actually do the search and send the result(s) */
405         if ( op->o_bd->be_search ) {
406                 if ( limits_check( op, rs ) == 0 ) {
407                         (op->o_bd->be_search)( op, rs );
408                 }
409                 /* else limits_check() sends error */
410
411         } else {
412                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
413                         "operation not supported within namingContext" );
414         }
415
416 #ifdef LDAP_SLAPI
417         if ( op->o_pb ) call_search_postop_plugins( op );
418 #endif /* LDAP_SLAPI */
419
420 #ifdef LDAP_SLAPI
421         if( attrs != NULL) op->o_tmpfree( attrs, op->o_tmpmemctx );
422 #endif /* LDAP_SLAPI */
423
424 return_results:;
425         return rs->sr_err;
426 }
427
428 #ifdef LDAP_SLAPI
429
430 static void init_search_pblock( Operation *op,
431         char **attrs, int managedsait )
432 {
433         slapi_int_pblock_set_operation( op->o_pb, op );
434         slapi_pblock_set( op->o_pb, SLAPI_SEARCH_TARGET, (void *)op->o_req_dn.bv_val );
435         slapi_pblock_set( op->o_pb, SLAPI_SEARCH_SCOPE, (void *)op->ors_scope );
436         slapi_pblock_set( op->o_pb, SLAPI_SEARCH_DEREF, (void *)op->ors_deref );
437         slapi_pblock_set( op->o_pb, SLAPI_SEARCH_SIZELIMIT, (void *)op->ors_slimit );
438         slapi_pblock_set( op->o_pb, SLAPI_SEARCH_TIMELIMIT, (void *)op->ors_tlimit );
439         slapi_pblock_set( op->o_pb, SLAPI_SEARCH_FILTER, (void *)op->ors_filter );
440         slapi_pblock_set( op->o_pb, SLAPI_SEARCH_STRFILTER, (void *)op->ors_filterstr.bv_val );
441         slapi_pblock_set( op->o_pb, SLAPI_SEARCH_ATTRS, (void *)attrs );
442         slapi_pblock_set( op->o_pb, SLAPI_SEARCH_ATTRSONLY, (void *)op->ors_attrsonly );
443         slapi_pblock_set( op->o_pb, SLAPI_MANAGEDSAIT, (void *)managedsait );
444 }
445
446 static int call_search_preop_plugins( Operation *op )
447 {
448         int rc;
449
450         rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_SEARCH_FN, op->o_pb );
451         if ( rc < 0 ) {
452                 /*
453                  * A preoperation plugin failure will abort the
454                  * entire operation.
455                  */
456                 Debug(LDAP_DEBUG_TRACE, "call_search_preop_plugins: search preoperation plugin "
457                                 "returned %d.\n", rc, 0, 0);
458                 if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 ) ||
459                      rc == LDAP_SUCCESS ) {
460                         rc = LDAP_OTHER;
461                 }
462         } else {
463                 rc = LDAP_SUCCESS;
464         }
465
466         return rc;
467 }
468
469 static int call_search_rewrite_plugins( Operation *op )
470 {
471         if ( slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, op->o_pb ) == 0 ) {
472                 int rc;
473
474                 /*
475                  * The plugin can set the SLAPI_SEARCH_FILTER.
476                  * SLAPI_SEARCH_STRFILER is not normative.
477                  */
478                 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_FILTER, (void *)&op->ors_filter );
479                 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
480                 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
481
482                 /*
483                  * Also permit other search parameters to be reset. One thing
484                  * this doesn't (yet) deal with is plugins that change a root
485                  * DSE search to a non-root DSE search...
486                  */
487                 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_TARGET, (void **)&op->o_req_dn.bv_val );
488                 op->o_req_dn.bv_len = strlen( op->o_req_dn.bv_val );
489
490                 if( !BER_BVISNULL( &op->o_req_ndn ) ) {
491                         slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
492                 }
493                 rc = dnNormalize( 0, NULL, NULL, &op->o_req_dn, &op->o_req_ndn,
494                         op->o_tmpmemctx );
495                 if ( rc != LDAP_SUCCESS ) {
496                         return rc;
497                 }
498
499                 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_SCOPE, (void **)&op->ors_scope );
500                 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_DEREF, (void **)&op->ors_deref );
501
502                 Debug( LDAP_DEBUG_ARGS, "    after compute_rewrite_search filter: %s\n",
503                         !BER_BVISEMPTY( &op->ors_filterstr ) ? op->ors_filterstr.bv_val : "empty", 0, 0 );
504         }
505
506         return LDAP_SUCCESS;
507 }
508
509 static void call_search_postop_plugins( Operation *op )
510 {
511         if ( slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_SEARCH_FN, op->o_pb ) < 0 ) {
512                 Debug(LDAP_DEBUG_TRACE, "call_search_postop_plugins: search postoperation plugins "
513                                 "failed.\n", 0, 0, 0);
514         }
515 }
516
517 void slapi_int_dummy(void)
518 {
519         /*
520          * XXX slapi_search_internal() was no getting pulled
521          * in; all manner of linker flags failed to link it.
522          * FIXME
523          */
524         slapi_search_internal( NULL, 0, NULL, NULL, NULL, 0 );
525 }
526 #endif /* LDAP_SLAPI */
527