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;
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,
75 * subordinate (3) -- OpenLDAP extension
77 * derefAliases ENUMERATED {
78 * neverDerefaliases (0),
79 * derefInSearching (1),
80 * derefFindingBaseObj (2),
81 * alwaysDerefAliases (3)
83 * sizelimit INTEGER (0 .. 65535),
84 * timelimit INTEGER (0 .. 65535),
87 * attributes SEQUENCE OF AttributeType
91 /* baseObject, scope, derefAliases, sizelimit, timelimit, attrsOnly */
92 if ( ber_scanf( op->o_ber, "{miiiib" /*}*/,
93 &base, &op->ors_scope, &op->ors_deref, &op->ors_slimit,
94 &op->ors_tlimit, &op->ors_attrsonly ) == LBER_ERROR )
96 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
97 rs->sr_err = SLAPD_DISCONNECT;
101 if ( op->ors_tlimit < 0 || op->ors_tlimit > SLAP_MAX_LIMIT ) {
102 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid time limit" );
106 if ( op->ors_slimit < 0 || op->ors_slimit > SLAP_MAX_LIMIT ) {
107 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid size limit" );
111 switch( op->ors_scope ) {
112 case LDAP_SCOPE_BASE:
113 case LDAP_SCOPE_ONELEVEL:
114 case LDAP_SCOPE_SUBTREE:
115 #ifdef LDAP_SCOPE_SUBORDINATE
116 case LDAP_SCOPE_SUBORDINATE:
120 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid scope" );
124 switch( op->ors_deref ) {
125 case LDAP_DEREF_NEVER:
126 case LDAP_DEREF_FINDING:
127 case LDAP_DEREF_SEARCHING:
128 case LDAP_DEREF_ALWAYS:
131 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid deref" );
135 rs->sr_err = dnPrettyNormal( NULL, &base, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
136 if( rs->sr_err != LDAP_SUCCESS ) {
138 LDAP_LOG( OPERATION, ERR,
139 "do_search: conn %d invalid dn (%s)\n",
140 op->o_connid, base.bv_val, 0 );
142 Debug( LDAP_DEBUG_ANY,
143 "do_search: invalid dn (%s)\n", base.bv_val, 0, 0 );
145 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
150 LDAP_LOG( OPERATION, ARGS, "SRCH \"%s\" %d %d",
151 base.bv_val, op->ors_scope, op->ors_deref );
152 LDAP_LOG( OPERATION, ARGS, " %d %d %d\n",
153 op->ors_slimit, op->ors_tlimit, op->ors_attrsonly);
155 Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d",
156 base.bv_val, op->ors_scope, op->ors_deref );
157 Debug( LDAP_DEBUG_ARGS, " %d %d %d\n",
158 op->ors_slimit, op->ors_tlimit, op->ors_attrsonly);
161 /* filter - returns a "normalized" version */
162 rs->sr_err = get_filter( op, op->o_ber, &op->ors_filter, &rs->sr_text );
163 if( rs->sr_err != LDAP_SUCCESS ) {
164 if( rs->sr_err == SLAPD_DISCONNECT ) {
165 rs->sr_err = LDAP_PROTOCOL_ERROR;
166 send_ldap_disconnect( op, rs );
167 rs->sr_err = SLAPD_DISCONNECT;
169 send_ldap_result( op, rs );
173 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
176 LDAP_LOG( OPERATION, ARGS,
177 "do_search: conn %d filter: %s\n",
178 op->o_connid, !BER_BVISEMPTY( &op->ors_filterstr ) ? op->ors_filterstr.bv_val : "empty",
181 Debug( LDAP_DEBUG_ARGS, " filter: %s\n",
182 !BER_BVISEMPTY( &op->ors_filterstr ) ? op->ors_filterstr.bv_val : "empty", 0, 0 );
186 siz = sizeof(AttributeName);
187 off = offsetof(AttributeName,an_name);
188 if ( ber_scanf( op->o_ber, "{M}}", &op->ors_attrs, &siz, off ) == LBER_ERROR ) {
189 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding attrs error" );
190 rs->sr_err = SLAPD_DISCONNECT;
193 for ( i=0; i<siz; i++ ) {
194 const char *dummy; /* ignore msgs from bv2ad */
195 op->ors_attrs[i].an_desc = NULL;
196 op->ors_attrs[i].an_oc = NULL;
197 op->ors_attrs[i].an_oc_exclude = 0;
198 slap_bv2ad(&op->ors_attrs[i].an_name, &op->ors_attrs[i].an_desc, &dummy);
201 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
203 LDAP_LOG( OPERATION, INFO,
204 "do_search: conn %d get_ctrls failed (%d)\n",
205 op->o_connid, rs->sr_err, 0 );
207 Debug( LDAP_DEBUG_ANY, "do_search: get_ctrls failed\n", 0, 0, 0 );
214 LDAP_LOG( OPERATION, ARGS,
215 "do_search: conn %d attrs:", op->o_connid, 0, 0 );
217 Debug( LDAP_DEBUG_ARGS, " attrs:", 0, 0, 0 );
221 for ( i = 0; i<siz; i++ ) {
223 LDAP_LOG( OPERATION, ARGS,
224 "do_search: %s", op->ors_attrs[i].an_name.bv_val, 0, 0 );
226 Debug( LDAP_DEBUG_ARGS, " %s", op->ors_attrs[i].an_name.bv_val, 0, 0 );
232 LDAP_LOG( OPERATION, ARGS, "\n" , 0, 0, 0 );
234 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
237 if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
238 char abuf[BUFSIZ/2], *ptr = abuf;
241 sprintf(abuf, "scope=%d deref=%d", op->ors_scope, op->ors_deref);
242 Statslog( LDAP_DEBUG_STATS,
243 "conn=%lu op=%lu SRCH base=\"%s\" %s filter=\"%s\"\n",
244 op->o_connid, op->o_opid, op->o_req_dn.bv_val, abuf,
245 op->ors_filterstr.bv_val );
247 for ( i = 0; i<siz; i++ ) {
248 alen = op->ors_attrs[i].an_name.bv_len;
249 if (alen >= sizeof(abuf)) {
250 alen = sizeof(abuf)-1;
252 if (len && (len + 1 + alen >= sizeof(abuf))) {
253 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu SRCH attr=%s\n",
254 op->o_connid, op->o_opid, abuf, 0, 0 );
262 ptr = lutil_strncopy(ptr, op->ors_attrs[i].an_name.bv_val, alen);
267 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu SRCH attr=%s\n",
268 op->o_connid, op->o_opid, abuf, 0, 0 );
272 manageDSAit = get_manageDSAit( op );
274 /* fake while loop to allow breaking out */
275 while ( op->ors_scope == LDAP_SCOPE_BASE ) {
278 if ( BER_BVISEMPTY( &op->o_req_ndn ) ) {
279 #ifdef LDAP_CONNECTIONLESS
280 /* Ignore LDAPv2 CLDAP Root DSE queries */
281 if (op->o_protocol == LDAP_VERSION2 && op->o_conn->c_is_udp) {
285 /* check restrictions */
286 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
287 send_ldap_result( op, rs );
293 attrs = anlist2charray( op, op->ors_attrs );
294 init_search_pblock( op, attrs, manageDSAit );
295 rs->sr_err = call_search_preop_plugins( op );
296 if ( rs->sr_err ) break;
297 call_search_rewrite_plugins( op );
299 #endif /* LDAP_SLAPI */
300 rs->sr_err = root_dse_info( op->o_conn, &entry, &rs->sr_text );
302 } else if ( bvmatch( &op->o_req_ndn, &global_schemandn ) ) {
303 /* check restrictions */
304 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
305 send_ldap_result( op, rs );
311 attrs = anlist2charray( op, op->ors_attrs );
312 init_search_pblock( op, attrs, manageDSAit );
313 rs->sr_err = call_search_preop_plugins( op );
314 if ( rs->sr_err ) break;
315 call_search_rewrite_plugins( op );
317 #endif /* LDAP_SLAPI */
318 rs->sr_err = schema_info( &entry, &rs->sr_text );
321 if( rs->sr_err != LDAP_SUCCESS ) {
322 send_ldap_result( op, rs );
324 if ( op->o_pb ) call_search_postop_plugins( op );
325 #endif /* LDAP_SLAPI */
328 } else if ( entry != NULL ) {
329 rs->sr_err = test_filter( op, entry, op->ors_filter );
331 if( rs->sr_err == LDAP_COMPARE_TRUE ) {
332 rs->sr_entry = entry;
333 rs->sr_attrs = op->ors_attrs;
334 rs->sr_operational_attrs = NULL;
335 send_search_entry( op, rs );
337 rs->sr_operational_attrs = NULL;
341 rs->sr_err = LDAP_SUCCESS;
342 send_ldap_result( op, rs );
344 if ( op->o_pb ) call_search_postop_plugins( op );
345 #endif /* LDAP_SLAPI */
351 if( BER_BVISEMPTY( &op->o_req_ndn ) && !BER_BVISEMPTY( &default_search_nbase ) ) {
352 slap_sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
353 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
355 ber_dupbv_x( &op->o_req_dn, &default_search_base, op->o_tmpmemctx );
356 ber_dupbv_x( &op->o_req_ndn, &default_search_nbase, op->o_tmpmemctx );
360 * We could be serving multiple database backends. Select the
361 * appropriate one, or send a referral to our "referral server"
362 * if we don't hold it.
365 /* Sync control overrides manageDSAit */
367 if ( manageDSAit != SLAP_NO_CONTROL ) {
368 if ( op->o_sync_mode & SLAP_SYNC_REFRESH ) {
369 be_manageDSAit = SLAP_NO_CONTROL;
371 be_manageDSAit = manageDSAit;
374 be_manageDSAit = manageDSAit;
377 op->o_bd = select_backend( &op->o_req_ndn, be_manageDSAit, 1 );
378 if ( op->o_bd == NULL ) {
379 rs->sr_ref = referral_rewrite( default_referral,
380 NULL, &op->o_req_dn, op->ors_scope );
382 if (!rs->sr_ref) rs->sr_ref = default_referral;
383 rs->sr_err = LDAP_REFERRAL;
384 send_ldap_result( op, rs );
386 if (rs->sr_ref != default_referral)
387 ber_bvarray_free( rs->sr_ref );
392 /* check restrictions */
393 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
394 send_ldap_result( op, rs );
398 /* check for referrals */
399 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
405 attrs = anlist2charray( op, op->ors_attrs );
406 init_search_pblock( op, attrs, manageDSAit );
407 rs->sr_err = call_search_preop_plugins( op );
408 if ( rs->sr_err != LDAP_SUCCESS ) {
412 call_search_rewrite_plugins( op );
414 #endif /* LDAP_SLAPI */
416 /* actually do the search and send the result(s) */
417 if ( op->o_bd->be_search ) {
418 if ( limits_check( op, rs ) == 0 ) {
419 (op->o_bd->be_search)( op, rs );
421 /* else limits_check() sends error */
424 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
425 "operation not supported within namingContext" );
429 if ( op->o_pb ) call_search_postop_plugins( op );
430 #endif /* LDAP_SLAPI */
433 if ( ( op->o_sync_mode & SLAP_SYNC_PERSIST ) ) return rs->sr_err;
434 if ( ( op->o_sync_slog_size != -1 ) ) return rs->sr_err;
436 if( !BER_BVISNULL( &op->o_req_dn ) ) {
437 slap_sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
439 if( !BER_BVISNULL( &op->o_req_ndn ) ) {
440 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
443 if( !BER_BVISNULL( &op->ors_filterstr ) ) {
444 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
446 if( op->ors_filter != NULL) filter_free_x( op, op->ors_filter );
447 if( op->ors_attrs != NULL ) {
448 op->o_tmpfree( op->ors_attrs, op->o_tmpmemctx );
452 if( attrs != NULL) op->o_tmpfree( attrs, op->o_tmpmemctx );
453 #endif /* LDAP_SLAPI */
460 static char **anlist2charray( Operation *op, AttributeName *an )
466 for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
468 attrs = (char **)op->o_tmpalloc( (i + 1) * sizeof(char *), op->o_tmpmemctx );
469 for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
470 attrs[i] = an[i].an_name.bv_val;
480 static void init_search_pblock( Operation *op,
481 char **attrs, int managedsait )
483 slapi_int_pblock_set_operation( op->o_pb, op );
484 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_TARGET, (void *)op->o_req_dn.bv_val );
485 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_SCOPE, (void *)op->ors_scope );
486 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_DEREF, (void *)op->ors_deref );
487 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_SIZELIMIT, (void *)op->ors_slimit );
488 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_TIMELIMIT, (void *)op->ors_tlimit );
489 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_FILTER, (void *)op->ors_filter );
490 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_STRFILTER, (void *)op->ors_filterstr.bv_val );
491 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_ATTRS, (void *)attrs );
492 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_ATTRSONLY, (void *)op->ors_attrsonly );
493 slapi_pblock_set( op->o_pb, SLAPI_MANAGEDSAIT, (void *)managedsait );
496 static int call_search_preop_plugins( Operation *op )
500 rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_SEARCH_FN, op->o_pb );
503 * A preoperation plugin failure will abort the
507 LDAP_LOG( OPERATION, INFO, "call_search_preop_plugins: search preoperation plugin "
508 "returned %d\n", rc, 0, 0 );
510 Debug(LDAP_DEBUG_TRACE, "call_search_preop_plugins: search preoperation plugin "
511 "returned %d.\n", rc, 0, 0);
513 if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 ) ||
514 rc == LDAP_SUCCESS ) {
524 static int call_search_rewrite_plugins( Operation *op )
526 if ( slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, op->o_pb ) == 0 ) {
530 * The plugin can set the SLAPI_SEARCH_FILTER.
531 * SLAPI_SEARCH_STRFILER is not normative.
533 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_FILTER, (void *)&op->ors_filter );
534 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
535 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
538 * Also permit other search parameters to be reset. One thing
539 * this doesn't (yet) deal with is plugins that change a root
540 * DSE search to a non-root DSE search...
542 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_TARGET, (void **)&op->o_req_dn.bv_val );
543 op->o_req_dn.bv_len = strlen( op->o_req_dn.bv_val );
545 if( !BER_BVISNULL( &op->o_req_ndn ) ) {
546 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
548 rc = dnNormalize( 0, NULL, NULL, &op->o_req_dn, &op->o_req_ndn,
550 if ( rc != LDAP_SUCCESS ) {
554 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_SCOPE, (void **)&op->ors_scope );
555 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_DEREF, (void **)&op->ors_deref );
558 LDAP_LOG( OPERATION, ARGS,
559 "call_search_rewrite_plugins: after compute_rewrite_search filter: %s\n",
560 !BER_BVISEMPTY( &op->ors_filterstr ) ? op->ors_filterstr.bv_val : "empty", 0, 0 );
562 Debug( LDAP_DEBUG_ARGS, " after compute_rewrite_search filter: %s\n",
563 !BER_BVISEMPTY( &op->ors_filterstr ) ? op->ors_filterstr.bv_val : "empty", 0, 0 );
570 static void call_search_postop_plugins( Operation *op )
572 if ( slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_SEARCH_FN, op->o_pb ) < 0 ) {
574 LDAP_LOG( OPERATION, INFO, "call_search_postop_plugins: search postoperation plugins "
575 "failed\n", 0, 0, 0 );
577 Debug(LDAP_DEBUG_TRACE, "call_search_postop_plugins: search postoperation plugins "
578 "failed.\n", 0, 0, 0);
583 void slapi_int_dummy(void)
586 * XXX slapi_search_internal() was no getting pulled
587 * in; all manner of linker flags failed to link it.
590 slapi_search_internal( NULL, 0, NULL, NULL, NULL, 0 );
592 #endif /* LDAP_SLAPI */