2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2003 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>
39 static char **anlist2charray( Operation *op, AttributeName *an );
40 static void initSearchPlugin( Operation *op, char **attrs, int managedsait );
41 static int doPreSearchPluginFNs( Operation *op );
42 static int doSearchRewriteFNs( Operation *op );
43 static void doPostSearchPluginFNs( Operation *op );
44 #endif /* LDAPI_SLAPI */
48 Operation *op, /* info about the op to which we're responding */
49 SlapReply *rs /* all the response data we'll send */
51 struct berval base = { 0, NULL };
52 ber_len_t siz, off, i;
60 LDAP_LOG( OPERATION, ENTRY, "do_search: conn %d\n", op->o_connid, 0, 0 );
62 Debug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );
66 * Parse the search request. It looks like this:
68 * SearchRequest := [APPLICATION 3] SEQUENCE {
69 * baseObject DistinguishedName,
75 * derefAliases ENUMERATED {
76 * neverDerefaliases (0),
77 * derefInSearching (1),
78 * derefFindingBaseObj (2),
79 * alwaysDerefAliases (3)
81 * sizelimit INTEGER (0 .. 65535),
82 * timelimit INTEGER (0 .. 65535),
85 * attributes SEQUENCE OF AttributeType
89 /* baseObject, scope, derefAliases, sizelimit, timelimit, attrsOnly */
90 if ( ber_scanf( op->o_ber, "{miiiib" /*}*/,
91 &base, &op->ors_scope, &op->ors_deref, &op->ors_slimit,
92 &op->ors_tlimit, &op->ors_attrsonly ) == LBER_ERROR )
94 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
95 rs->sr_err = SLAPD_DISCONNECT;
99 switch( op->ors_scope ) {
100 case LDAP_SCOPE_BASE:
101 case LDAP_SCOPE_ONELEVEL:
102 case LDAP_SCOPE_SUBTREE:
105 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid scope" );
109 switch( op->ors_deref ) {
110 case LDAP_DEREF_NEVER:
111 case LDAP_DEREF_FINDING:
112 case LDAP_DEREF_SEARCHING:
113 case LDAP_DEREF_ALWAYS:
116 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid deref" );
120 rs->sr_err = dnPrettyNormal( NULL, &base, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
121 if( rs->sr_err != LDAP_SUCCESS ) {
123 LDAP_LOG( OPERATION, ERR,
124 "do_search: conn %d invalid dn (%s)\n",
125 op->o_connid, base.bv_val, 0 );
127 Debug( LDAP_DEBUG_ANY,
128 "do_search: invalid dn (%s)\n", base.bv_val, 0, 0 );
130 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
135 LDAP_LOG( OPERATION, ARGS, "SRCH \"%s\" %d %d",
136 base.bv_val, op->ors_scope, op->ors_deref );
137 LDAP_LOG( OPERATION, ARGS, " %d %d %d\n",
138 op->ors_slimit, op->ors_tlimit, op->ors_attrsonly);
140 Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d",
141 base.bv_val, op->ors_scope, op->ors_deref );
142 Debug( LDAP_DEBUG_ARGS, " %d %d %d\n",
143 op->ors_slimit, op->ors_tlimit, op->ors_attrsonly);
146 /* filter - returns a "normalized" version */
147 rs->sr_err = get_filter( op, op->o_ber, &op->ors_filter, &rs->sr_text );
148 if( rs->sr_err != LDAP_SUCCESS ) {
149 if( rs->sr_err == SLAPD_DISCONNECT ) {
150 rs->sr_err = LDAP_PROTOCOL_ERROR;
151 send_ldap_disconnect( op, rs );
153 send_ldap_result( op, rs );
157 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
160 LDAP_LOG( OPERATION, ARGS,
161 "do_search: conn %d filter: %s\n",
162 op->o_connid, op->ors_filterstr.bv_len ? op->ors_filterstr.bv_val : "empty", 0 );
164 Debug( LDAP_DEBUG_ARGS, " filter: %s\n",
165 op->ors_filterstr.bv_len ? op->ors_filterstr.bv_val : "empty", 0, 0 );
169 siz = sizeof(AttributeName);
170 off = offsetof(AttributeName,an_name);
171 if ( ber_scanf( op->o_ber, "{M}}", &op->ors_attrs, &siz, off ) == LBER_ERROR ) {
172 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding attrs error" );
173 rs->sr_err = SLAPD_DISCONNECT;
176 for ( i=0; i<siz; i++ ) {
177 const char *dummy; /* ignore msgs from bv2ad */
178 op->ors_attrs[i].an_desc = NULL;
179 op->ors_attrs[i].an_oc = NULL;
180 op->ors_attrs[i].an_oc_exclude = 0;
181 slap_bv2ad(&op->ors_attrs[i].an_name, &op->ors_attrs[i].an_desc, &dummy);
184 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
186 LDAP_LOG( OPERATION, INFO,
187 "do_search: conn %d get_ctrls failed (%d)\n",
188 op->o_connid, rs->sr_err, 0 );
190 Debug( LDAP_DEBUG_ANY, "do_search: get_ctrls failed\n", 0, 0, 0 );
197 LDAP_LOG( OPERATION, ARGS,
198 "do_search: conn %d attrs:", op->o_connid, 0, 0 );
200 Debug( LDAP_DEBUG_ARGS, " attrs:", 0, 0, 0 );
204 for ( i = 0; i<siz; i++ ) {
206 LDAP_LOG( OPERATION, ARGS,
207 "do_search: %s", op->ors_attrs[i].an_name.bv_val, 0, 0 );
209 Debug( LDAP_DEBUG_ARGS, " %s", op->ors_attrs[i].an_name.bv_val, 0, 0 );
215 LDAP_LOG( OPERATION, ARGS, "\n" , 0, 0, 0 );
217 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
220 if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
221 char abuf[BUFSIZ/2], *ptr = abuf;
224 sprintf(abuf, "scope=%d deref=%d", op->ors_scope, op->ors_deref);
225 Statslog( LDAP_DEBUG_STATS,
226 "conn=%lu op=%lu SRCH base=\"%s\" %s filter=\"%s\"\n",
227 op->o_connid, op->o_opid, op->o_req_dn.bv_val, abuf,
228 op->ors_filterstr.bv_val );
230 for ( i = 0; i<siz; i++ ) {
231 alen = op->ors_attrs[i].an_name.bv_len;
232 if (alen >= sizeof(abuf)) {
233 alen = sizeof(abuf)-1;
235 if (len && (len + 1 + alen >= sizeof(abuf))) {
236 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu SRCH attr=%s\n",
237 op->o_connid, op->o_opid, abuf, 0, 0 );
245 ptr = lutil_strncopy(ptr, op->ors_attrs[i].an_name.bv_val, alen);
250 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu SRCH attr=%s\n",
251 op->o_connid, op->o_opid, abuf, 0, 0 );
255 manageDSAit = get_manageDSAit( op );
257 /* fake while loop to allow breaking out */
258 while ( op->ors_scope == LDAP_SCOPE_BASE ) {
261 if ( op->o_req_ndn.bv_len == 0 ) {
262 #ifdef LDAP_CONNECTIONLESS
263 /* Ignore LDAPv2 CLDAP Root DSE queries */
264 if (op->o_protocol == LDAP_VERSION2 && op->o_conn->c_is_udp) {
268 /* check restrictions */
269 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
270 send_ldap_result( op, rs );
276 attrs = anlist2charray( op, op->ors_attrs );
277 initSearchPlugin( op, attrs, manageDSAit );
278 rs->sr_err = doPreSearchPluginFNs( op );
279 if ( rs->sr_err ) break;
280 doSearchRewriteFNs( op );
282 #endif /* LDAP_SLAPI */
283 rs->sr_err = root_dse_info( op->o_conn, &entry, &rs->sr_text );
285 } else if ( bvmatch( &op->o_req_ndn, &global_schemandn ) ) {
286 /* check restrictions */
287 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
288 send_ldap_result( op, rs );
294 attrs = anlist2charray( op, op->ors_attrs );
295 initSearchPlugin( op, attrs, manageDSAit );
296 rs->sr_err = doPreSearchPluginFNs( op );
297 if ( rs->sr_err ) break;
298 doSearchRewriteFNs( op );
300 #endif /* LDAP_SLAPI */
301 rs->sr_err = schema_info( &entry, &rs->sr_text );
304 if( rs->sr_err != LDAP_SUCCESS ) {
305 send_ldap_result( op, rs );
307 if ( op->o_pb ) doPostSearchPluginFNs( op );
308 #endif /* LDAP_SLAPI */
311 } else if ( entry != NULL ) {
312 rs->sr_err = test_filter( op, entry, op->ors_filter );
314 if( rs->sr_err == LDAP_COMPARE_TRUE ) {
315 rs->sr_entry = entry;
316 rs->sr_attrs = op->ors_attrs;
317 send_search_entry( op, rs );
322 rs->sr_err = LDAP_SUCCESS;
323 send_ldap_result( op, rs );
325 if ( op->o_pb ) doPostSearchPluginFNs( op );
326 #endif /* LDAP_SLAPI */
332 if( !op->o_req_ndn.bv_len && default_search_nbase.bv_len ) {
333 sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
334 sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
336 ber_dupbv_x( &op->o_req_dn, &default_search_base, op->o_tmpmemctx );
337 ber_dupbv_x( &op->o_req_ndn, &default_search_nbase, op->o_tmpmemctx );
341 * We could be serving multiple database backends. Select the
342 * appropriate one, or send a referral to our "referral server"
343 * if we don't hold it.
346 /* Sync control overrides manageDSAit */
348 if ( manageDSAit != SLAP_NO_CONTROL ) {
349 if ( op->o_sync_mode & SLAP_SYNC_REFRESH ) {
350 be_manageDSAit = SLAP_NO_CONTROL;
352 be_manageDSAit = manageDSAit;
355 be_manageDSAit = manageDSAit;
358 if ( (op->o_bd = select_backend( &op->o_req_ndn, be_manageDSAit, 1 )) == NULL ) {
359 rs->sr_ref = referral_rewrite( default_referral,
360 NULL, &op->o_req_dn, op->ors_scope );
362 if (!rs->sr_ref) rs->sr_ref = default_referral;
363 rs->sr_err = LDAP_REFERRAL;
364 send_ldap_result( op, rs );
366 if (rs->sr_ref != default_referral)
367 ber_bvarray_free( rs->sr_ref );
372 /* check restrictions */
373 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
374 send_ldap_result( op, rs );
378 /* check for referrals */
379 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
385 attrs = anlist2charray( op, op->ors_attrs );
386 initSearchPlugin( op, attrs, manageDSAit );
387 rs->sr_err = doPreSearchPluginFNs( op );
388 if ( rs->sr_err != LDAP_SUCCESS ) {
392 doSearchRewriteFNs( op );
394 #endif /* LDAP_SLAPI */
396 /* actually do the search and send the result(s) */
397 if ( op->o_bd->be_search ) {
398 (op->o_bd->be_search)( op, rs );
400 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
401 "operation not supported within namingContext" );
405 if ( op->o_pb ) doPostSearchPluginFNs( op );
406 #endif /* LDAP_SLAPI */
410 if ( ( op->o_sync_mode & SLAP_SYNC_PERSIST ) )
413 if ( ( op->o_sync_slog_size != -1 ) )
416 if( op->o_req_dn.bv_val != NULL) sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
417 if( op->o_req_ndn.bv_val != NULL) sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
419 if( op->ors_filterstr.bv_val != NULL) op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
420 if( op->ors_filter != NULL) filter_free_x( op, op->ors_filter );
421 if( op->ors_attrs != NULL ) op->o_tmpfree( op->ors_attrs, op->o_tmpmemctx );
423 if( attrs != NULL) op->o_tmpfree( attrs, op->o_tmpmemctx );
424 #endif /* LDAP_SLAPI */
431 static char **anlist2charray( Operation *op, AttributeName *an )
437 for ( i = 0; an[i].an_name.bv_val != NULL; i++ )
439 attrs = (char **)op->o_tmpalloc( (i + 1) * sizeof(char *), op->o_tmpmemctx );
440 for ( i = 0; an[i].an_name.bv_val != NULL; i++ ) {
441 attrs[i] = an[i].an_name.bv_val;
451 static void initSearchPlugin( Operation *op,
452 char **attrs, int managedsait )
454 slapi_x_pblock_set_operation( op->o_pb, op );
455 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_TARGET, (void *)op->o_req_dn.bv_val );
456 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_SCOPE, (void *)op->ors_scope );
457 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_DEREF, (void *)op->ors_deref );
458 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_SIZELIMIT, (void *)op->ors_slimit );
459 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_TIMELIMIT, (void *)op->ors_tlimit );
460 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_FILTER, (void *)op->ors_filter );
461 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_STRFILTER, (void *)op->ors_filterstr.bv_val );
462 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_ATTRS, (void *)attrs );
463 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_ATTRSONLY, (void *)op->ors_attrsonly );
464 slapi_pblock_set( op->o_pb, SLAPI_MANAGEDSAIT, (void *)managedsait );
467 static int doPreSearchPluginFNs( Operation *op )
471 rc = doPluginFNs( op->o_bd, SLAPI_PLUGIN_PRE_SEARCH_FN, op->o_pb );
474 * A preoperation plugin failure will abort the
478 LDAP_LOG( OPERATION, INFO, "doPreSearchPluginFNs: search preoperation plugin "
479 "returned %d\n", rc, 0, 0 );
481 Debug(LDAP_DEBUG_TRACE, "doPreSearchPluginFNs: search preoperation plugin "
482 "returned %d.\n", rc, 0, 0);
484 if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 ) ||
485 rc == LDAP_SUCCESS ) {
495 static int doSearchRewriteFNs( Operation *op )
497 if ( doPluginFNs( op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, op->o_pb ) == 0 ) {
501 * The plugin can set the SLAPI_SEARCH_FILTER.
502 * SLAPI_SEARCH_STRFILER is not normative.
504 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_FILTER, (void *)&op->ors_filter );
505 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
506 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
509 * Also permit other search parameters to be reset. One thing
510 * this doesn't (yet) deal with is plugins that change a root
511 * DSE search to a non-root DSE search...
513 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_TARGET, (void **)&op->o_req_dn.bv_val );
514 op->o_req_dn.bv_len = strlen( op->o_req_dn.bv_val );
516 if( op->o_req_ndn.bv_val != NULL) {
517 sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
519 rc = dnNormalize( 0, NULL, NULL, &op->o_req_dn, &op->o_req_ndn,
521 if ( rc != LDAP_SUCCESS ) {
525 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_SCOPE, (void **)&op->ors_scope );
526 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_DEREF, (void **)&op->ors_deref );
529 LDAP_LOG( OPERATION, ARGS,
530 "doSearchRewriteFNs: after compute_rewrite_search filter: %s\n",
531 op->ors_filterstr.bv_len ? op->ors_filterstr.bv_val : "empty", 0, 0 );
533 Debug( LDAP_DEBUG_ARGS, " after compute_rewrite_search filter: %s\n",
534 op->ors_filterstr.bv_len ? op->ors_filterstr.bv_val : "empty", 0, 0 );
541 static void doPostSearchPluginFNs( Operation *op )
543 if ( doPluginFNs( op->o_bd, SLAPI_PLUGIN_POST_SEARCH_FN, op->o_pb ) < 0 ) {
545 LDAP_LOG( OPERATION, INFO, "doPostSearchPluginFNs: search postoperation plugins "
546 "failed\n", 0, 0, 0 );
548 Debug(LDAP_DEBUG_TRACE, "doPostSearchPluginFNs: search postoperation plugins "
549 "failed.\n", 0, 0, 0);
557 * XXX slapi_search_internal() was no getting pulled
558 * in; all manner of linker flags failed to link it.
561 slapi_search_internal( NULL, 0, NULL, NULL, NULL, 0 );
563 #endif /* LDAP_SLAPI */