2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 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;
56 LDAP_LOG( OPERATION, ENTRY, "do_search: conn %d\n", op->o_connid, 0, 0 );
58 Debug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );
62 * Parse the search request. It looks like this:
64 * SearchRequest := [APPLICATION 3] SEQUENCE {
65 * baseObject DistinguishedName,
70 * subordinate (3) -- OpenLDAP extension
72 * derefAliases ENUMERATED {
73 * neverDerefaliases (0),
74 * derefInSearching (1),
75 * derefFindingBaseObj (2),
76 * alwaysDerefAliases (3)
78 * sizelimit INTEGER (0 .. 65535),
79 * timelimit INTEGER (0 .. 65535),
82 * attributes SEQUENCE OF AttributeType
86 /* baseObject, scope, derefAliases, sizelimit, timelimit, attrsOnly */
87 if ( ber_scanf( op->o_ber, "{miiiib" /*}*/,
88 &base, &op->ors_scope, &op->ors_deref, &op->ors_slimit,
89 &op->ors_tlimit, &op->ors_attrsonly ) == LBER_ERROR )
91 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
92 rs->sr_err = SLAPD_DISCONNECT;
96 if ( op->ors_tlimit < 0 || op->ors_tlimit > SLAP_MAX_LIMIT ) {
97 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid time limit" );
101 if ( op->ors_slimit < 0 || op->ors_slimit > SLAP_MAX_LIMIT ) {
102 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid size limit" );
106 switch( op->ors_scope ) {
107 case LDAP_SCOPE_BASE:
108 case LDAP_SCOPE_ONELEVEL:
109 case LDAP_SCOPE_SUBTREE:
110 #ifdef LDAP_SCOPE_SUBORDINATE
111 case LDAP_SCOPE_SUBORDINATE:
115 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid scope" );
119 switch( op->ors_deref ) {
120 case LDAP_DEREF_NEVER:
121 case LDAP_DEREF_FINDING:
122 case LDAP_DEREF_SEARCHING:
123 case LDAP_DEREF_ALWAYS:
126 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid deref" );
130 rs->sr_err = dnPrettyNormal( NULL, &base, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
131 if( rs->sr_err != LDAP_SUCCESS ) {
133 LDAP_LOG( OPERATION, ERR,
134 "do_search: conn %d invalid dn (%s)\n",
135 op->o_connid, base.bv_val, 0 );
137 Debug( LDAP_DEBUG_ANY,
138 "do_search: invalid dn (%s)\n", base.bv_val, 0, 0 );
140 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
145 LDAP_LOG( OPERATION, ARGS, "SRCH \"%s\" %d %d",
146 base.bv_val, op->ors_scope, op->ors_deref );
147 LDAP_LOG( OPERATION, ARGS, " %d %d %d\n",
148 op->ors_slimit, op->ors_tlimit, op->ors_attrsonly);
150 Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d",
151 base.bv_val, op->ors_scope, op->ors_deref );
152 Debug( LDAP_DEBUG_ARGS, " %d %d %d\n",
153 op->ors_slimit, op->ors_tlimit, op->ors_attrsonly);
156 /* filter - returns a "normalized" version */
157 rs->sr_err = get_filter( op, op->o_ber, &op->ors_filter, &rs->sr_text );
158 if( rs->sr_err != LDAP_SUCCESS ) {
159 if( rs->sr_err == SLAPD_DISCONNECT ) {
160 rs->sr_err = LDAP_PROTOCOL_ERROR;
161 send_ldap_disconnect( op, rs );
162 rs->sr_err = SLAPD_DISCONNECT;
164 send_ldap_result( op, rs );
168 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
171 LDAP_LOG( OPERATION, ARGS,
172 "do_search: conn %d filter: %s\n",
173 op->o_connid, !BER_BVISEMPTY( &op->ors_filterstr ) ? op->ors_filterstr.bv_val : "empty",
176 Debug( LDAP_DEBUG_ARGS, " filter: %s\n",
177 !BER_BVISEMPTY( &op->ors_filterstr ) ? 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 op->o_bd = frontendDB;
268 rs->sr_err = frontendDB->be_search( op, rs );
271 if ( ( op->o_sync_mode & SLAP_SYNC_PERSIST ) ) {
274 if ( ( op->o_sync_slog_size != -1 ) ) {
277 if ( !BER_BVISNULL( &op->o_req_dn ) ) {
278 slap_sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
280 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
281 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
283 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
284 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
286 if ( op->ors_filter != NULL) {
287 filter_free_x( op, op->ors_filter );
289 if ( op->ors_attrs != NULL ) {
290 op->o_tmpfree( op->ors_attrs, op->o_tmpmemctx );
297 fe_op_search( Operation *op, SlapReply *rs )
305 manageDSAit = get_manageDSAit( op );
307 /* fake while loop to allow breaking out */
308 while ( op->ors_scope == LDAP_SCOPE_BASE ) {
311 if ( BER_BVISEMPTY( &op->o_req_ndn ) ) {
312 #ifdef LDAP_CONNECTIONLESS
313 /* Ignore LDAPv2 CLDAP Root DSE queries */
314 if (op->o_protocol == LDAP_VERSION2 && op->o_conn->c_is_udp) {
318 /* check restrictions */
319 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
320 send_ldap_result( op, rs );
326 attrs = anlist2charray( op, op->ors_attrs );
327 init_search_pblock( op, attrs, manageDSAit );
328 rs->sr_err = call_search_preop_plugins( op );
329 if ( rs->sr_err ) break;
330 call_search_rewrite_plugins( op );
332 #endif /* LDAP_SLAPI */
333 rs->sr_err = root_dse_info( op->o_conn, &entry, &rs->sr_text );
335 } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
336 /* check restrictions */
337 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
338 send_ldap_result( op, rs );
344 attrs = anlist2charray( op, op->ors_attrs );
345 init_search_pblock( op, attrs, manageDSAit );
346 rs->sr_err = call_search_preop_plugins( op );
347 if ( rs->sr_err ) break;
348 call_search_rewrite_plugins( op );
350 #endif /* LDAP_SLAPI */
351 rs->sr_err = schema_info( &entry, &rs->sr_text );
354 if( rs->sr_err != LDAP_SUCCESS ) {
355 send_ldap_result( op, rs );
357 if ( op->o_pb ) call_search_postop_plugins( op );
358 #endif /* LDAP_SLAPI */
361 } else if ( entry != NULL ) {
362 rs->sr_err = test_filter( op, entry, op->ors_filter );
364 if( rs->sr_err == LDAP_COMPARE_TRUE ) {
365 rs->sr_entry = entry;
366 rs->sr_attrs = op->ors_attrs;
367 rs->sr_operational_attrs = NULL;
368 send_search_entry( op, rs );
370 rs->sr_operational_attrs = NULL;
374 rs->sr_err = LDAP_SUCCESS;
375 send_ldap_result( op, rs );
377 if ( op->o_pb ) call_search_postop_plugins( op );
378 #endif /* LDAP_SLAPI */
384 if( BER_BVISEMPTY( &op->o_req_ndn ) && !BER_BVISEMPTY( &default_search_nbase ) ) {
385 slap_sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
386 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
388 ber_dupbv_x( &op->o_req_dn, &default_search_base, op->o_tmpmemctx );
389 ber_dupbv_x( &op->o_req_ndn, &default_search_nbase, op->o_tmpmemctx );
393 * We could be serving multiple database backends. Select the
394 * appropriate one, or send a referral to our "referral server"
395 * if we don't hold it.
398 /* Sync control overrides manageDSAit */
400 if ( manageDSAit != SLAP_NO_CONTROL ) {
401 if ( op->o_sync_mode & SLAP_SYNC_REFRESH ) {
402 be_manageDSAit = SLAP_NO_CONTROL;
404 be_manageDSAit = manageDSAit;
407 be_manageDSAit = manageDSAit;
410 op->o_bd = select_backend( &op->o_req_ndn, be_manageDSAit, 1 );
411 if ( op->o_bd == NULL ) {
412 rs->sr_ref = referral_rewrite( default_referral,
413 NULL, &op->o_req_dn, op->ors_scope );
415 if (!rs->sr_ref) rs->sr_ref = default_referral;
416 rs->sr_err = LDAP_REFERRAL;
417 send_ldap_result( op, rs );
419 if (rs->sr_ref != default_referral)
420 ber_bvarray_free( rs->sr_ref );
425 /* check restrictions */
426 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
427 send_ldap_result( op, rs );
431 /* check for referrals */
432 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
438 attrs = anlist2charray( op, op->ors_attrs );
439 init_search_pblock( op, attrs, manageDSAit );
440 rs->sr_err = call_search_preop_plugins( op );
441 if ( rs->sr_err != LDAP_SUCCESS ) {
445 call_search_rewrite_plugins( op );
447 #endif /* LDAP_SLAPI */
449 /* actually do the search and send the result(s) */
450 if ( op->o_bd->be_search ) {
451 if ( limits_check( op, rs ) == 0 ) {
452 (op->o_bd->be_search)( op, rs );
454 /* else limits_check() sends error */
457 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
458 "operation not supported within namingContext" );
462 if ( op->o_pb ) call_search_postop_plugins( op );
463 #endif /* LDAP_SLAPI */
466 if( attrs != NULL) op->o_tmpfree( attrs, op->o_tmpmemctx );
467 #endif /* LDAP_SLAPI */
475 static char **anlist2charray( Operation *op, AttributeName *an )
481 for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
483 attrs = (char **)op->o_tmpalloc( (i + 1) * sizeof(char *), op->o_tmpmemctx );
484 for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
485 attrs[i] = an[i].an_name.bv_val;
495 static void init_search_pblock( Operation *op,
496 char **attrs, int managedsait )
498 slapi_int_pblock_set_operation( op->o_pb, op );
499 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_TARGET, (void *)op->o_req_dn.bv_val );
500 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_SCOPE, (void *)op->ors_scope );
501 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_DEREF, (void *)op->ors_deref );
502 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_SIZELIMIT, (void *)op->ors_slimit );
503 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_TIMELIMIT, (void *)op->ors_tlimit );
504 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_FILTER, (void *)op->ors_filter );
505 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_STRFILTER, (void *)op->ors_filterstr.bv_val );
506 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_ATTRS, (void *)attrs );
507 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_ATTRSONLY, (void *)op->ors_attrsonly );
508 slapi_pblock_set( op->o_pb, SLAPI_MANAGEDSAIT, (void *)managedsait );
511 static int call_search_preop_plugins( Operation *op )
515 rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_SEARCH_FN, op->o_pb );
518 * A preoperation plugin failure will abort the
522 LDAP_LOG( OPERATION, INFO, "call_search_preop_plugins: search preoperation plugin "
523 "returned %d\n", rc, 0, 0 );
525 Debug(LDAP_DEBUG_TRACE, "call_search_preop_plugins: search preoperation plugin "
526 "returned %d.\n", rc, 0, 0);
528 if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 ) ||
529 rc == LDAP_SUCCESS ) {
539 static int call_search_rewrite_plugins( Operation *op )
541 if ( slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, op->o_pb ) == 0 ) {
545 * The plugin can set the SLAPI_SEARCH_FILTER.
546 * SLAPI_SEARCH_STRFILER is not normative.
548 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_FILTER, (void *)&op->ors_filter );
549 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
550 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
553 * Also permit other search parameters to be reset. One thing
554 * this doesn't (yet) deal with is plugins that change a root
555 * DSE search to a non-root DSE search...
557 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_TARGET, (void **)&op->o_req_dn.bv_val );
558 op->o_req_dn.bv_len = strlen( op->o_req_dn.bv_val );
560 if( !BER_BVISNULL( &op->o_req_ndn ) ) {
561 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
563 rc = dnNormalize( 0, NULL, NULL, &op->o_req_dn, &op->o_req_ndn,
565 if ( rc != LDAP_SUCCESS ) {
569 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_SCOPE, (void **)&op->ors_scope );
570 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_DEREF, (void **)&op->ors_deref );
573 LDAP_LOG( OPERATION, ARGS,
574 "call_search_rewrite_plugins: after compute_rewrite_search filter: %s\n",
575 !BER_BVISEMPTY( &op->ors_filterstr ) ? op->ors_filterstr.bv_val : "empty", 0, 0 );
577 Debug( LDAP_DEBUG_ARGS, " after compute_rewrite_search filter: %s\n",
578 !BER_BVISEMPTY( &op->ors_filterstr ) ? op->ors_filterstr.bv_val : "empty", 0, 0 );
585 static void call_search_postop_plugins( Operation *op )
587 if ( slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_SEARCH_FN, op->o_pb ) < 0 ) {
589 LDAP_LOG( OPERATION, INFO, "call_search_postop_plugins: search postoperation plugins "
590 "failed\n", 0, 0, 0 );
592 Debug(LDAP_DEBUG_TRACE, "call_search_postop_plugins: search postoperation plugins "
593 "failed.\n", 0, 0, 0);
598 void slapi_int_dummy(void)
601 * XXX slapi_search_internal() was no getting pulled
602 * in; all manner of linker flags failed to link it.
605 slapi_search_internal( NULL, 0, NULL, NULL, NULL, 0 );
607 #endif /* LDAP_SLAPI */