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