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,
74 * subordinate (3) -- OpenLDAP extension
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 switch( op->ors_scope ) {
101 case LDAP_SCOPE_BASE:
102 case LDAP_SCOPE_ONELEVEL:
103 case LDAP_SCOPE_SUBTREE:
104 case LDAP_SCOPE_SUBORDINATE:
107 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid scope" );
111 switch( op->ors_deref ) {
112 case LDAP_DEREF_NEVER:
113 case LDAP_DEREF_FINDING:
114 case LDAP_DEREF_SEARCHING:
115 case LDAP_DEREF_ALWAYS:
118 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid deref" );
122 rs->sr_err = dnPrettyNormal( NULL, &base, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
123 if( rs->sr_err != LDAP_SUCCESS ) {
125 LDAP_LOG( OPERATION, ERR,
126 "do_search: conn %d invalid dn (%s)\n",
127 op->o_connid, base.bv_val, 0 );
129 Debug( LDAP_DEBUG_ANY,
130 "do_search: invalid dn (%s)\n", base.bv_val, 0, 0 );
132 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
137 LDAP_LOG( OPERATION, ARGS, "SRCH \"%s\" %d %d",
138 base.bv_val, op->ors_scope, op->ors_deref );
139 LDAP_LOG( OPERATION, ARGS, " %d %d %d\n",
140 op->ors_slimit, op->ors_tlimit, op->ors_attrsonly);
142 Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d",
143 base.bv_val, op->ors_scope, op->ors_deref );
144 Debug( LDAP_DEBUG_ARGS, " %d %d %d\n",
145 op->ors_slimit, op->ors_tlimit, op->ors_attrsonly);
148 /* filter - returns a "normalized" version */
149 rs->sr_err = get_filter( op, op->o_ber, &op->ors_filter, &rs->sr_text );
150 if( rs->sr_err != LDAP_SUCCESS ) {
151 if( rs->sr_err == SLAPD_DISCONNECT ) {
152 rs->sr_err = LDAP_PROTOCOL_ERROR;
153 send_ldap_disconnect( op, rs );
155 send_ldap_result( op, rs );
159 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
162 LDAP_LOG( OPERATION, ARGS,
163 "do_search: conn %d filter: %s\n",
164 op->o_connid, op->ors_filterstr.bv_len ? op->ors_filterstr.bv_val : "empty", 0 );
166 Debug( LDAP_DEBUG_ARGS, " filter: %s\n",
167 op->ors_filterstr.bv_len ? op->ors_filterstr.bv_val : "empty", 0, 0 );
171 siz = sizeof(AttributeName);
172 off = offsetof(AttributeName,an_name);
173 if ( ber_scanf( op->o_ber, "{M}}", &op->ors_attrs, &siz, off ) == LBER_ERROR ) {
174 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding attrs error" );
175 rs->sr_err = SLAPD_DISCONNECT;
178 for ( i=0; i<siz; i++ ) {
179 const char *dummy; /* ignore msgs from bv2ad */
180 op->ors_attrs[i].an_desc = NULL;
181 op->ors_attrs[i].an_oc = NULL;
182 op->ors_attrs[i].an_oc_exclude = 0;
183 slap_bv2ad(&op->ors_attrs[i].an_name, &op->ors_attrs[i].an_desc, &dummy);
186 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
188 LDAP_LOG( OPERATION, INFO,
189 "do_search: conn %d get_ctrls failed (%d)\n",
190 op->o_connid, rs->sr_err, 0 );
192 Debug( LDAP_DEBUG_ANY, "do_search: get_ctrls failed\n", 0, 0, 0 );
199 LDAP_LOG( OPERATION, ARGS,
200 "do_search: conn %d attrs:", op->o_connid, 0, 0 );
202 Debug( LDAP_DEBUG_ARGS, " attrs:", 0, 0, 0 );
206 for ( i = 0; i<siz; i++ ) {
208 LDAP_LOG( OPERATION, ARGS,
209 "do_search: %s", op->ors_attrs[i].an_name.bv_val, 0, 0 );
211 Debug( LDAP_DEBUG_ARGS, " %s", op->ors_attrs[i].an_name.bv_val, 0, 0 );
217 LDAP_LOG( OPERATION, ARGS, "\n" , 0, 0, 0 );
219 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
222 if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
223 char abuf[BUFSIZ/2], *ptr = abuf;
226 sprintf(abuf, "scope=%d deref=%d", op->ors_scope, op->ors_deref);
227 Statslog( LDAP_DEBUG_STATS,
228 "conn=%lu op=%lu SRCH base=\"%s\" %s filter=\"%s\"\n",
229 op->o_connid, op->o_opid, op->o_req_dn.bv_val, abuf,
230 op->ors_filterstr.bv_val );
232 for ( i = 0; i<siz; i++ ) {
233 alen = op->ors_attrs[i].an_name.bv_len;
234 if (alen >= sizeof(abuf)) {
235 alen = sizeof(abuf)-1;
237 if (len && (len + 1 + alen >= sizeof(abuf))) {
238 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu SRCH attr=%s\n",
239 op->o_connid, op->o_opid, abuf, 0, 0 );
247 ptr = lutil_strncopy(ptr, op->ors_attrs[i].an_name.bv_val, alen);
252 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu SRCH attr=%s\n",
253 op->o_connid, op->o_opid, abuf, 0, 0 );
257 manageDSAit = get_manageDSAit( op );
259 /* fake while loop to allow breaking out */
260 while ( op->ors_scope == LDAP_SCOPE_BASE ) {
263 if ( op->o_req_ndn.bv_len == 0 ) {
264 #ifdef LDAP_CONNECTIONLESS
265 /* Ignore LDAPv2 CLDAP Root DSE queries */
266 if (op->o_protocol == LDAP_VERSION2 && op->o_conn->c_is_udp) {
270 /* check restrictions */
271 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
272 send_ldap_result( op, rs );
278 attrs = anlist2charray( op, op->ors_attrs );
279 initSearchPlugin( op, attrs, manageDSAit );
280 rs->sr_err = doPreSearchPluginFNs( op );
281 if ( rs->sr_err ) break;
282 doSearchRewriteFNs( op );
284 #endif /* LDAP_SLAPI */
285 rs->sr_err = root_dse_info( op->o_conn, &entry, &rs->sr_text );
287 } else if ( bvmatch( &op->o_req_ndn, &global_schemandn ) ) {
288 /* check restrictions */
289 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
290 send_ldap_result( op, rs );
296 attrs = anlist2charray( op, op->ors_attrs );
297 initSearchPlugin( op, attrs, manageDSAit );
298 rs->sr_err = doPreSearchPluginFNs( op );
299 if ( rs->sr_err ) break;
300 doSearchRewriteFNs( op );
302 #endif /* LDAP_SLAPI */
303 rs->sr_err = schema_info( &entry, &rs->sr_text );
306 if( rs->sr_err != LDAP_SUCCESS ) {
307 send_ldap_result( op, rs );
309 if ( op->o_pb ) doPostSearchPluginFNs( op );
310 #endif /* LDAP_SLAPI */
313 } else if ( entry != NULL ) {
314 rs->sr_err = test_filter( op, entry, op->ors_filter );
316 if( rs->sr_err == LDAP_COMPARE_TRUE ) {
317 rs->sr_entry = entry;
318 rs->sr_attrs = op->ors_attrs;
319 send_search_entry( op, rs );
324 rs->sr_err = LDAP_SUCCESS;
325 send_ldap_result( op, rs );
327 if ( op->o_pb ) doPostSearchPluginFNs( op );
328 #endif /* LDAP_SLAPI */
334 if( !op->o_req_ndn.bv_len && default_search_nbase.bv_len ) {
335 sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
336 sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
338 ber_dupbv_x( &op->o_req_dn, &default_search_base, op->o_tmpmemctx );
339 ber_dupbv_x( &op->o_req_ndn, &default_search_nbase, op->o_tmpmemctx );
343 * We could be serving multiple database backends. Select the
344 * appropriate one, or send a referral to our "referral server"
345 * if we don't hold it.
348 /* Sync control overrides manageDSAit */
350 if ( manageDSAit != SLAP_NO_CONTROL ) {
351 if ( op->o_sync_mode & SLAP_SYNC_REFRESH ) {
352 be_manageDSAit = SLAP_NO_CONTROL;
354 be_manageDSAit = manageDSAit;
357 be_manageDSAit = manageDSAit;
360 op->o_bd = select_backend( &op->o_req_ndn, be_manageDSAit, 1 );
361 if ( op->o_bd == NULL ) {
362 rs->sr_ref = referral_rewrite( default_referral,
363 NULL, &op->o_req_dn, op->ors_scope );
365 if (!rs->sr_ref) rs->sr_ref = default_referral;
366 rs->sr_err = LDAP_REFERRAL;
367 send_ldap_result( op, rs );
369 if (rs->sr_ref != default_referral)
370 ber_bvarray_free( rs->sr_ref );
375 /* check restrictions */
376 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
377 send_ldap_result( op, rs );
381 /* check for referrals */
382 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
388 attrs = anlist2charray( op, op->ors_attrs );
389 initSearchPlugin( op, attrs, manageDSAit );
390 rs->sr_err = doPreSearchPluginFNs( op );
391 if ( rs->sr_err != LDAP_SUCCESS ) {
395 doSearchRewriteFNs( op );
397 #endif /* LDAP_SLAPI */
399 /* actually do the search and send the result(s) */
400 if ( op->o_bd->be_search ) {
401 (op->o_bd->be_search)( op, rs );
403 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
404 "operation not supported within namingContext" );
408 if ( op->o_pb ) doPostSearchPluginFNs( op );
409 #endif /* LDAP_SLAPI */
413 if ( ( op->o_sync_mode & SLAP_SYNC_PERSIST ) )
416 if ( ( op->o_sync_slog_size != -1 ) )
419 if( op->o_req_dn.bv_val != NULL) sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
420 if( op->o_req_ndn.bv_val != NULL) sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
422 if( op->ors_filterstr.bv_val != NULL) op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
423 if( op->ors_filter != NULL) filter_free_x( op, op->ors_filter );
424 if( op->ors_attrs != NULL ) op->o_tmpfree( op->ors_attrs, op->o_tmpmemctx );
426 if( attrs != NULL) op->o_tmpfree( attrs, op->o_tmpmemctx );
427 #endif /* LDAP_SLAPI */
434 static char **anlist2charray( Operation *op, AttributeName *an )
440 for ( i = 0; an[i].an_name.bv_val != NULL; i++ )
442 attrs = (char **)op->o_tmpalloc( (i + 1) * sizeof(char *), op->o_tmpmemctx );
443 for ( i = 0; an[i].an_name.bv_val != NULL; i++ ) {
444 attrs[i] = an[i].an_name.bv_val;
454 static void initSearchPlugin( Operation *op,
455 char **attrs, int managedsait )
457 slapi_int_pblock_set_operation( op->o_pb, op );
458 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_TARGET, (void *)op->o_req_dn.bv_val );
459 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_SCOPE, (void *)op->ors_scope );
460 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_DEREF, (void *)op->ors_deref );
461 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_SIZELIMIT, (void *)op->ors_slimit );
462 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_TIMELIMIT, (void *)op->ors_tlimit );
463 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_FILTER, (void *)op->ors_filter );
464 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_STRFILTER, (void *)op->ors_filterstr.bv_val );
465 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_ATTRS, (void *)attrs );
466 slapi_pblock_set( op->o_pb, SLAPI_SEARCH_ATTRSONLY, (void *)op->ors_attrsonly );
467 slapi_pblock_set( op->o_pb, SLAPI_MANAGEDSAIT, (void *)managedsait );
470 static int doPreSearchPluginFNs( Operation *op )
474 rc = doPluginFNs( op->o_bd, SLAPI_PLUGIN_PRE_SEARCH_FN, op->o_pb );
477 * A preoperation plugin failure will abort the
481 LDAP_LOG( OPERATION, INFO, "doPreSearchPluginFNs: search preoperation plugin "
482 "returned %d\n", rc, 0, 0 );
484 Debug(LDAP_DEBUG_TRACE, "doPreSearchPluginFNs: search preoperation plugin "
485 "returned %d.\n", rc, 0, 0);
487 if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 ) ||
488 rc == LDAP_SUCCESS ) {
498 static int doSearchRewriteFNs( Operation *op )
500 if ( doPluginFNs( op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, op->o_pb ) == 0 ) {
504 * The plugin can set the SLAPI_SEARCH_FILTER.
505 * SLAPI_SEARCH_STRFILER is not normative.
507 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_FILTER, (void *)&op->ors_filter );
508 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
509 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
512 * Also permit other search parameters to be reset. One thing
513 * this doesn't (yet) deal with is plugins that change a root
514 * DSE search to a non-root DSE search...
516 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_TARGET, (void **)&op->o_req_dn.bv_val );
517 op->o_req_dn.bv_len = strlen( op->o_req_dn.bv_val );
519 if( op->o_req_ndn.bv_val != NULL) {
520 sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
522 rc = dnNormalize( 0, NULL, NULL, &op->o_req_dn, &op->o_req_ndn,
524 if ( rc != LDAP_SUCCESS ) {
528 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_SCOPE, (void **)&op->ors_scope );
529 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_DEREF, (void **)&op->ors_deref );
532 LDAP_LOG( OPERATION, ARGS,
533 "doSearchRewriteFNs: after compute_rewrite_search filter: %s\n",
534 op->ors_filterstr.bv_len ? op->ors_filterstr.bv_val : "empty", 0, 0 );
536 Debug( LDAP_DEBUG_ARGS, " after compute_rewrite_search filter: %s\n",
537 op->ors_filterstr.bv_len ? op->ors_filterstr.bv_val : "empty", 0, 0 );
544 static void doPostSearchPluginFNs( Operation *op )
546 if ( doPluginFNs( op->o_bd, SLAPI_PLUGIN_POST_SEARCH_FN, op->o_pb ) < 0 ) {
548 LDAP_LOG( OPERATION, INFO, "doPostSearchPluginFNs: search postoperation plugins "
549 "failed\n", 0, 0, 0 );
551 Debug(LDAP_DEBUG_TRACE, "doPostSearchPluginFNs: search postoperation plugins "
552 "failed.\n", 0, 0, 0);
560 * XXX slapi_search_internal() was no getting pulled
561 * in; all manner of linker flags failed to link it.
564 slapi_search_internal( NULL, 0, NULL, NULL, NULL, 0 );
566 #endif /* LDAP_SLAPI */