2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
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>.
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16 * All rights reserved.
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.
30 #include <ac/string.h>
31 #include <ac/socket.h>
38 #include "slapi/slapi.h"
40 static char **anlist2charray( Operation *op, AttributeName *an );
41 static void init_search_pblock( Operation *op, char **attrs, int managedsait );
42 static int call_search_preop_plugins( Operation *op );
43 static int call_search_rewrite_plugins( Operation *op );
44 static void call_search_postop_plugins( Operation *op );
45 #endif /* LDAPI_SLAPI */
49 Operation *op, /* info about the op to which we're responding */
50 SlapReply *rs /* all the response data we'll send */ )
52 struct berval base = BER_BVNULL;
53 ber_len_t siz, off, i;
61 LDAP_LOG( OPERATION, ENTRY, "do_search: conn %d\n", op->o_connid, 0, 0 );
63 Debug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );
67 * Parse the search request. It looks like this:
69 * SearchRequest := [APPLICATION 3] SEQUENCE {
70 * baseObject DistinguishedName,
76 * derefAliases ENUMERATED {
77 * neverDerefaliases (0),
78 * derefInSearching (1),
79 * derefFindingBaseObj (2),
80 * alwaysDerefAliases (3)
82 * sizelimit INTEGER (0 .. 65535),
83 * timelimit INTEGER (0 .. 65535),
86 * attributes SEQUENCE OF AttributeType
90 /* baseObject, scope, derefAliases, sizelimit, timelimit, attrsOnly */
91 if ( ber_scanf( op->o_ber, "{miiiib" /*}*/,
92 &base, &op->ors_scope, &op->ors_deref, &op->ors_slimit,
93 &op->ors_tlimit, &op->ors_attrsonly ) == LBER_ERROR )
95 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
96 rs->sr_err = SLAPD_DISCONNECT;
100 if ( op->ors_tlimit < 0 || op->ors_tlimit > SLAP_MAX_LIMIT ) {
101 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid time limit" );
105 if ( op->ors_slimit < 0 || op->ors_slimit > SLAP_MAX_LIMIT ) {
106 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid size limit" );
110 switch( op->ors_scope ) {
111 case LDAP_SCOPE_BASE:
112 case LDAP_SCOPE_ONELEVEL:
113 case LDAP_SCOPE_SUBTREE:
116 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid scope" );
120 switch( op->ors_deref ) {
121 case LDAP_DEREF_NEVER:
122 case LDAP_DEREF_FINDING:
123 case LDAP_DEREF_SEARCHING:
124 case LDAP_DEREF_ALWAYS:
127 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid deref" );
131 rs->sr_err = dnPrettyNormal( NULL, &base, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
132 if( rs->sr_err != LDAP_SUCCESS ) {
134 LDAP_LOG( OPERATION, ERR,
135 "do_search: conn %d invalid dn (%s)\n",
136 op->o_connid, base.bv_val, 0 );
138 Debug( LDAP_DEBUG_ANY,
139 "do_search: invalid dn (%s)\n", base.bv_val, 0, 0 );
141 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
146 LDAP_LOG( OPERATION, ARGS, "SRCH \"%s\" %d %d",
147 base.bv_val, op->ors_scope, op->ors_deref );
148 LDAP_LOG( OPERATION, ARGS, " %d %d %d\n",
149 op->ors_slimit, op->ors_tlimit, op->ors_attrsonly);
151 Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d",
152 base.bv_val, op->ors_scope, op->ors_deref );
153 Debug( LDAP_DEBUG_ARGS, " %d %d %d\n",
154 op->ors_slimit, op->ors_tlimit, op->ors_attrsonly);
157 /* filter - returns a "normalized" version */
158 rs->sr_err = get_filter( op, op->o_ber, &op->ors_filter, &rs->sr_text );
159 if( rs->sr_err != LDAP_SUCCESS ) {
160 if( rs->sr_err == SLAPD_DISCONNECT ) {
161 rs->sr_err = LDAP_PROTOCOL_ERROR;
162 send_ldap_disconnect( op, rs );
163 rs->sr_err = SLAPD_DISCONNECT;
165 send_ldap_result( op, rs );
169 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
172 LDAP_LOG( OPERATION, ARGS,
173 "do_search: conn %d filter: %s\n",
174 op->o_connid, op->ors_filterstr.bv_len ? op->ors_filterstr.bv_val : "empty", 0 );
176 Debug( LDAP_DEBUG_ARGS, " filter: %s\n",
177 op->ors_filterstr.bv_len ? op->ors_filterstr.bv_val : "empty", 0, 0 );
181 siz = sizeof(AttributeName);
182 off = offsetof(AttributeName,an_name);
183 if ( ber_scanf( op->o_ber, "{M}}", &op->ors_attrs, &siz, off ) == LBER_ERROR ) {
184 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding attrs error" );
185 rs->sr_err = SLAPD_DISCONNECT;
188 for ( i=0; i<siz; i++ ) {
189 const char *dummy; /* ignore msgs from bv2ad */
190 op->ors_attrs[i].an_desc = NULL;
191 op->ors_attrs[i].an_oc = NULL;
192 op->ors_attrs[i].an_oc_exclude = 0;
193 slap_bv2ad(&op->ors_attrs[i].an_name, &op->ors_attrs[i].an_desc, &dummy);
196 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
198 LDAP_LOG( OPERATION, INFO,
199 "do_search: conn %d get_ctrls failed (%d)\n",
200 op->o_connid, rs->sr_err, 0 );
202 Debug( LDAP_DEBUG_ANY, "do_search: get_ctrls failed\n", 0, 0, 0 );
209 LDAP_LOG( OPERATION, ARGS,
210 "do_search: conn %d attrs:", op->o_connid, 0, 0 );
212 Debug( LDAP_DEBUG_ARGS, " attrs:", 0, 0, 0 );
216 for ( i = 0; i<siz; i++ ) {
218 LDAP_LOG( OPERATION, ARGS,
219 "do_search: %s", op->ors_attrs[i].an_name.bv_val, 0, 0 );
221 Debug( LDAP_DEBUG_ARGS, " %s", op->ors_attrs[i].an_name.bv_val, 0, 0 );
227 LDAP_LOG( OPERATION, ARGS, "\n" , 0, 0, 0 );
229 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
232 if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
233 char abuf[BUFSIZ/2], *ptr = abuf;
236 sprintf(abuf, "scope=%d deref=%d", op->ors_scope, op->ors_deref);
237 Statslog( LDAP_DEBUG_STATS,
238 "conn=%lu op=%lu SRCH base=\"%s\" %s filter=\"%s\"\n",
239 op->o_connid, op->o_opid, op->o_req_dn.bv_val, abuf,
240 op->ors_filterstr.bv_val );
242 for ( i = 0; i<siz; i++ ) {
243 alen = op->ors_attrs[i].an_name.bv_len;
244 if (alen >= sizeof(abuf)) {
245 alen = sizeof(abuf)-1;
247 if (len && (len + 1 + alen >= sizeof(abuf))) {
248 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu SRCH attr=%s\n",
249 op->o_connid, op->o_opid, abuf, 0, 0 );
257 ptr = lutil_strncopy(ptr, op->ors_attrs[i].an_name.bv_val, alen);
262 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu SRCH attr=%s\n",
263 op->o_connid, op->o_opid, abuf, 0, 0 );
267 manageDSAit = get_manageDSAit( op );
269 /* fake while loop to allow breaking out */
270 while ( op->ors_scope == LDAP_SCOPE_BASE ) {
273 if ( op->o_req_ndn.bv_len == 0 ) {
274 #ifdef LDAP_CONNECTIONLESS
275 /* Ignore LDAPv2 CLDAP Root DSE queries */
276 if (op->o_protocol == LDAP_VERSION2 && op->o_conn->c_is_udp) {
280 /* check restrictions */
281 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
282 send_ldap_result( op, rs );
288 attrs = anlist2charray( op, op->ors_attrs );
289 init_search_pblock( op, attrs, manageDSAit );
290 rs->sr_err = call_search_preop_plugins( op );
291 if ( rs->sr_err ) break;
292 call_search_rewrite_plugins( op );
294 #endif /* LDAP_SLAPI */
295 rs->sr_err = root_dse_info( op->o_conn, &entry, &rs->sr_text );
297 } else if ( bvmatch( &op->o_req_ndn, &global_schemandn ) ) {
298 /* check restrictions */
299 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
300 send_ldap_result( op, rs );
306 attrs = anlist2charray( op, op->ors_attrs );
307 init_search_pblock( op, attrs, manageDSAit );
308 rs->sr_err = call_search_preop_plugins( op );
309 if ( rs->sr_err ) break;
310 call_search_rewrite_plugins( op );
312 #endif /* LDAP_SLAPI */
313 rs->sr_err = schema_info( &entry, &rs->sr_text );
316 if( rs->sr_err != LDAP_SUCCESS ) {
317 send_ldap_result( op, rs );
319 if ( op->o_pb ) call_search_postop_plugins( op );
320 #endif /* LDAP_SLAPI */
323 } else if ( entry != NULL ) {
324 rs->sr_err = test_filter( op, entry, op->ors_filter );
326 if( rs->sr_err == LDAP_COMPARE_TRUE ) {
327 rs->sr_entry = entry;
328 rs->sr_attrs = op->ors_attrs;
329 send_search_entry( op, rs );
334 rs->sr_err = LDAP_SUCCESS;
335 send_ldap_result( op, rs );
337 if ( op->o_pb ) call_search_postop_plugins( op );
338 #endif /* LDAP_SLAPI */
344 if( !op->o_req_ndn.bv_len && default_search_nbase.bv_len ) {
345 sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
346 sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
348 ber_dupbv_x( &op->o_req_dn, &default_search_base, op->o_tmpmemctx );
349 ber_dupbv_x( &op->o_req_ndn, &default_search_nbase, op->o_tmpmemctx );
353 * We could be serving multiple database backends. Select the
354 * appropriate one, or send a referral to our "referral server"
355 * if we don't hold it.
358 /* Sync control overrides manageDSAit */
360 if ( manageDSAit != SLAP_NO_CONTROL ) {
361 if ( op->o_sync_mode & SLAP_SYNC_REFRESH ) {
362 be_manageDSAit = SLAP_NO_CONTROL;
364 be_manageDSAit = manageDSAit;
367 be_manageDSAit = manageDSAit;
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 );
375 if (!rs->sr_ref) rs->sr_ref = default_referral;
376 rs->sr_err = LDAP_REFERRAL;
377 send_ldap_result( op, rs );
379 if (rs->sr_ref != default_referral)
380 ber_bvarray_free( rs->sr_ref );
385 /* check restrictions */
386 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
387 send_ldap_result( op, rs );
391 /* check for referrals */
392 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
398 attrs = anlist2charray( op, op->ors_attrs );
399 init_search_pblock( op, attrs, manageDSAit );
400 rs->sr_err = call_search_preop_plugins( op );
401 if ( rs->sr_err != LDAP_SUCCESS ) {
405 call_search_rewrite_plugins( op );
407 #endif /* LDAP_SLAPI */
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 );
414 /* else limits_check() sends error */
417 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
418 "operation not supported within namingContext" );
422 if ( op->o_pb ) call_search_postop_plugins( op );
423 #endif /* LDAP_SLAPI */
426 if ( ( op->o_sync_mode & SLAP_SYNC_PERSIST ) ) return rs->sr_err;
427 if ( ( op->o_sync_slog_size != -1 ) ) return rs->sr_err;
429 if( !BER_BVISNULL( &op->o_req_dn ) ) {
430 slap_sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
432 if( !BER_BVISNULL( &op->o_req_ndn ) ) {
433 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
436 if( !BER_BVISNULL( &op->ors_filterstr ) ) {
437 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
439 if( op->ors_filter != NULL) filter_free_x( op, op->ors_filter );
440 if( op->ors_attrs != NULL ) {
441 op->o_tmpfree( op->ors_attrs, op->o_tmpmemctx );
445 if( attrs != NULL) op->o_tmpfree( attrs, op->o_tmpmemctx );
446 #endif /* LDAP_SLAPI */
453 static char **anlist2charray( Operation *op, AttributeName *an )
459 for ( i = 0; an[i].an_name.bv_val != NULL; i++ )
461 attrs = (char **)op->o_tmpalloc( (i + 1) * sizeof(char *), op->o_tmpmemctx );
462 for ( i = 0; an[i].an_name.bv_val != NULL; i++ ) {
463 attrs[i] = an[i].an_name.bv_val;
473 static void init_search_pblock( Operation *op,
474 char **attrs, int managedsait )
476 slapi_int_pblock_set_operation( op->o_pb, op );
477 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_TARGET, (void *)op->o_req_dn.bv_val );
478 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_SCOPE, (void *)op->ors_scope );
479 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_DEREF, (void *)op->ors_deref );
480 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_SIZELIMIT, (void *)op->ors_slimit );
481 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_TIMELIMIT, (void *)op->ors_tlimit );
482 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_FILTER, (void *)op->ors_filter );
483 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_STRFILTER, (void *)op->ors_filterstr.bv_val );
484 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_ATTRS, (void *)attrs );
485 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_ATTRSONLY, (void *)op->ors_attrsonly );
486 slapi_pblock_set( op->o_pb, SLAPI_MANAGEDSAIT, (void *)managedsait );
489 static int call_search_preop_plugins( Operation *op )
493 rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_SEARCH_FN, op->o_pb );
496 * A preoperation plugin failure will abort the
500 LDAP_LOG( OPERATION, INFO, "call_search_preop_plugins: search preoperation plugin "
501 "returned %d\n", rc, 0, 0 );
503 Debug(LDAP_DEBUG_TRACE, "call_search_preop_plugins: search preoperation plugin "
504 "returned %d.\n", rc, 0, 0);
506 if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 ) ||
507 rc == LDAP_SUCCESS ) {
517 static int call_search_rewrite_plugins( Operation *op )
519 if ( slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, op->o_pb ) == 0 ) {
523 * The plugin can set the SLAPI_SEARCH_FILTER.
524 * SLAPI_SEARCH_STRFILER is not normative.
526 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_FILTER, (void *)&op->ors_filter );
527 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
528 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
531 * Also permit other search parameters to be reset. One thing
532 * this doesn't (yet) deal with is plugins that change a root
533 * DSE search to a non-root DSE search...
535 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_TARGET, (void **)&op->o_req_dn.bv_val );
536 op->o_req_dn.bv_len = strlen( op->o_req_dn.bv_val );
538 if( op->o_req_ndn.bv_val != NULL) {
539 sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
541 rc = dnNormalize( 0, NULL, NULL, &op->o_req_dn, &op->o_req_ndn,
543 if ( rc != LDAP_SUCCESS ) {
547 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_SCOPE, (void **)&op->ors_scope );
548 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_DEREF, (void **)&op->ors_deref );
551 LDAP_LOG( OPERATION, ARGS,
552 "call_search_rewrite_plugins: after compute_rewrite_search filter: %s\n",
553 op->ors_filterstr.bv_len ? op->ors_filterstr.bv_val : "empty", 0, 0 );
555 Debug( LDAP_DEBUG_ARGS, " after compute_rewrite_search filter: %s\n",
556 op->ors_filterstr.bv_len ? op->ors_filterstr.bv_val : "empty", 0, 0 );
563 static void call_search_postop_plugins( Operation *op )
565 if ( slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_SEARCH_FN, op->o_pb ) < 0 ) {
567 LDAP_LOG( OPERATION, INFO, "call_search_postop_plugins: search postoperation plugins "
568 "failed\n", 0, 0, 0 );
570 Debug(LDAP_DEBUG_TRACE, "call_search_postop_plugins: search postoperation plugins "
571 "failed.\n", 0, 0, 0);
576 void slapi_int_dummy(void)
579 * XXX slapi_search_internal() was no getting pulled
580 * in; all manner of linker flags failed to link it.
583 slapi_search_internal( NULL, 0, NULL, NULL, NULL, 0 );
585 #endif /* LDAP_SLAPI */