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;
55 Debug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );
58 * Parse the search request. It looks like this:
60 * SearchRequest := [APPLICATION 3] SEQUENCE {
61 * baseObject DistinguishedName,
66 * subordinate (3) -- OpenLDAP extension
68 * derefAliases ENUMERATED {
69 * neverDerefaliases (0),
70 * derefInSearching (1),
71 * derefFindingBaseObj (2),
72 * alwaysDerefAliases (3)
74 * sizelimit INTEGER (0 .. 65535),
75 * timelimit INTEGER (0 .. 65535),
78 * attributes SEQUENCE OF AttributeType
82 /* baseObject, scope, derefAliases, sizelimit, timelimit, attrsOnly */
83 if ( ber_scanf( op->o_ber, "{miiiib" /*}*/,
84 &base, &op->ors_scope, &op->ors_deref, &op->ors_slimit,
85 &op->ors_tlimit, &op->ors_attrsonly ) == LBER_ERROR )
87 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
88 rs->sr_err = SLAPD_DISCONNECT;
92 if ( op->ors_tlimit < 0 || op->ors_tlimit > SLAP_MAX_LIMIT ) {
93 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid time limit" );
97 if ( op->ors_slimit < 0 || op->ors_slimit > SLAP_MAX_LIMIT ) {
98 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid size limit" );
102 switch( op->ors_scope ) {
103 case LDAP_SCOPE_BASE:
104 case LDAP_SCOPE_ONELEVEL:
105 case LDAP_SCOPE_SUBTREE:
106 #ifdef LDAP_SCOPE_SUBORDINATE
107 case LDAP_SCOPE_SUBORDINATE:
111 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid scope" );
115 switch( op->ors_deref ) {
116 case LDAP_DEREF_NEVER:
117 case LDAP_DEREF_FINDING:
118 case LDAP_DEREF_SEARCHING:
119 case LDAP_DEREF_ALWAYS:
122 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid deref" );
126 rs->sr_err = dnPrettyNormal( NULL, &base, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
127 if( rs->sr_err != LDAP_SUCCESS ) {
128 Debug( LDAP_DEBUG_ANY,
129 "do_search: invalid dn (%s)\n", base.bv_val, 0, 0 );
130 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
134 Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d",
135 base.bv_val, op->ors_scope, op->ors_deref );
136 Debug( LDAP_DEBUG_ARGS, " %d %d %d\n",
137 op->ors_slimit, op->ors_tlimit, op->ors_attrsonly);
139 /* filter - returns a "normalized" version */
140 rs->sr_err = get_filter( op, op->o_ber, &op->ors_filter, &rs->sr_text );
141 if( rs->sr_err != LDAP_SUCCESS ) {
142 if( rs->sr_err == SLAPD_DISCONNECT ) {
143 rs->sr_err = LDAP_PROTOCOL_ERROR;
144 send_ldap_disconnect( op, rs );
145 rs->sr_err = SLAPD_DISCONNECT;
147 send_ldap_result( op, rs );
151 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
153 Debug( LDAP_DEBUG_ARGS, " filter: %s\n",
154 !BER_BVISEMPTY( &op->ors_filterstr ) ? op->ors_filterstr.bv_val : "empty", 0, 0 );
157 siz = sizeof(AttributeName);
158 off = offsetof(AttributeName,an_name);
159 if ( ber_scanf( op->o_ber, "{M}}", &op->ors_attrs, &siz, off ) == LBER_ERROR ) {
160 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding attrs error" );
161 rs->sr_err = SLAPD_DISCONNECT;
164 for ( i=0; i<siz; i++ ) {
165 const char *dummy; /* ignore msgs from bv2ad */
166 op->ors_attrs[i].an_desc = NULL;
167 op->ors_attrs[i].an_oc = NULL;
168 op->ors_attrs[i].an_oc_exclude = 0;
169 slap_bv2ad(&op->ors_attrs[i].an_name, &op->ors_attrs[i].an_desc, &dummy);
172 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
173 Debug( LDAP_DEBUG_ANY, "do_search: get_ctrls failed\n", 0, 0, 0 );
178 Debug( LDAP_DEBUG_ARGS, " attrs:", 0, 0, 0 );
181 for ( i = 0; i<siz; i++ ) {
182 Debug( LDAP_DEBUG_ARGS, " %s", op->ors_attrs[i].an_name.bv_val, 0, 0 );
186 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
188 if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
189 char abuf[BUFSIZ/2], *ptr = abuf;
192 sprintf(abuf, "scope=%d deref=%d", op->ors_scope, op->ors_deref);
193 Statslog( LDAP_DEBUG_STATS,
194 "conn=%lu op=%lu SRCH base=\"%s\" %s filter=\"%s\"\n",
195 op->o_connid, op->o_opid, op->o_req_dn.bv_val, abuf,
196 op->ors_filterstr.bv_val );
198 for ( i = 0; i<siz; i++ ) {
199 alen = op->ors_attrs[i].an_name.bv_len;
200 if (alen >= sizeof(abuf)) {
201 alen = sizeof(abuf)-1;
203 if (len && (len + 1 + alen >= sizeof(abuf))) {
204 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu SRCH attr=%s\n",
205 op->o_connid, op->o_opid, abuf, 0, 0 );
213 ptr = lutil_strncopy(ptr, op->ors_attrs[i].an_name.bv_val, alen);
218 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu SRCH attr=%s\n",
219 op->o_connid, op->o_opid, abuf, 0, 0 );
223 op->o_bd = frontendDB;
224 rs->sr_err = frontendDB->be_search( op, rs );
227 if ( ( op->o_sync_mode & SLAP_SYNC_PERSIST ) ) {
230 if ( ( op->o_sync_slog_size != -1 ) ) {
233 if ( !BER_BVISNULL( &op->o_req_dn ) ) {
234 slap_sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
236 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
237 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
239 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
240 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
242 if ( op->ors_filter != NULL) {
243 filter_free_x( op, op->ors_filter );
245 if ( op->ors_attrs != NULL ) {
246 op->o_tmpfree( op->ors_attrs, op->o_tmpmemctx );
253 fe_op_search( Operation *op, SlapReply *rs )
261 manageDSAit = get_manageDSAit( op );
263 /* fake while loop to allow breaking out */
264 while ( op->ors_scope == LDAP_SCOPE_BASE ) {
267 if ( BER_BVISEMPTY( &op->o_req_ndn ) ) {
268 #ifdef LDAP_CONNECTIONLESS
269 /* Ignore LDAPv2 CLDAP Root DSE queries */
270 if (op->o_protocol == LDAP_VERSION2 && op->o_conn->c_is_udp) {
274 /* check restrictions */
275 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
276 send_ldap_result( op, rs );
282 attrs = anlist2charray( op, op->ors_attrs );
283 init_search_pblock( op, attrs, manageDSAit );
284 rs->sr_err = call_search_preop_plugins( op );
285 if ( rs->sr_err ) break;
286 call_search_rewrite_plugins( op );
288 #endif /* LDAP_SLAPI */
289 rs->sr_err = root_dse_info( op->o_conn, &entry, &rs->sr_text );
291 } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
292 /* check restrictions */
293 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
294 send_ldap_result( op, rs );
300 attrs = anlist2charray( op, op->ors_attrs );
301 init_search_pblock( op, attrs, manageDSAit );
302 rs->sr_err = call_search_preop_plugins( op );
303 if ( rs->sr_err ) break;
304 call_search_rewrite_plugins( op );
306 #endif /* LDAP_SLAPI */
307 rs->sr_err = schema_info( &entry, &rs->sr_text );
310 if( rs->sr_err != LDAP_SUCCESS ) {
311 send_ldap_result( op, rs );
313 if ( op->o_pb ) call_search_postop_plugins( op );
314 #endif /* LDAP_SLAPI */
317 } else if ( entry != NULL ) {
318 rs->sr_err = test_filter( op, entry, op->ors_filter );
320 if( rs->sr_err == LDAP_COMPARE_TRUE ) {
321 rs->sr_entry = entry;
322 rs->sr_attrs = op->ors_attrs;
323 rs->sr_operational_attrs = NULL;
324 send_search_entry( op, rs );
326 rs->sr_operational_attrs = NULL;
330 rs->sr_err = LDAP_SUCCESS;
331 send_ldap_result( op, rs );
333 if ( op->o_pb ) call_search_postop_plugins( op );
334 #endif /* LDAP_SLAPI */
340 if( BER_BVISEMPTY( &op->o_req_ndn ) && !BER_BVISEMPTY( &default_search_nbase ) ) {
341 slap_sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
342 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
344 ber_dupbv_x( &op->o_req_dn, &default_search_base, op->o_tmpmemctx );
345 ber_dupbv_x( &op->o_req_ndn, &default_search_nbase, op->o_tmpmemctx );
349 * We could be serving multiple database backends. Select the
350 * appropriate one, or send a referral to our "referral server"
351 * if we don't hold it.
354 /* Sync control overrides manageDSAit */
356 if ( manageDSAit != SLAP_NO_CONTROL ) {
357 if ( op->o_sync_mode & SLAP_SYNC_REFRESH ) {
358 be_manageDSAit = SLAP_NO_CONTROL;
360 be_manageDSAit = manageDSAit;
363 be_manageDSAit = manageDSAit;
366 op->o_bd = select_backend( &op->o_req_ndn, be_manageDSAit, 1 );
367 if ( op->o_bd == NULL ) {
368 rs->sr_ref = referral_rewrite( default_referral,
369 NULL, &op->o_req_dn, op->ors_scope );
371 if (!rs->sr_ref) rs->sr_ref = default_referral;
372 rs->sr_err = LDAP_REFERRAL;
373 send_ldap_result( op, rs );
375 if (rs->sr_ref != default_referral)
376 ber_bvarray_free( rs->sr_ref );
381 /* check restrictions */
382 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
383 send_ldap_result( op, rs );
387 /* check for referrals */
388 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
394 attrs = anlist2charray( op, op->ors_attrs );
395 init_search_pblock( op, attrs, manageDSAit );
396 rs->sr_err = call_search_preop_plugins( op );
397 if ( rs->sr_err != LDAP_SUCCESS ) {
401 call_search_rewrite_plugins( op );
403 #endif /* LDAP_SLAPI */
405 /* actually do the search and send the result(s) */
406 if ( op->o_bd->be_search ) {
407 if ( limits_check( op, rs ) == 0 ) {
408 (op->o_bd->be_search)( op, rs );
410 /* else limits_check() sends error */
413 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
414 "operation not supported within namingContext" );
418 if ( op->o_pb ) call_search_postop_plugins( op );
419 #endif /* LDAP_SLAPI */
422 if( attrs != NULL) op->o_tmpfree( attrs, op->o_tmpmemctx );
423 #endif /* LDAP_SLAPI */
431 static char **anlist2charray( Operation *op, AttributeName *an )
437 for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
439 attrs = (char **)op->o_tmpalloc( (i + 1) * sizeof(char *), op->o_tmpmemctx );
440 for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
441 attrs[i] = an[i].an_name.bv_val;
451 static void init_search_pblock( Operation *op,
452 char **attrs, int managedsait )
454 slapi_int_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 call_search_preop_plugins( Operation *op )
471 rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_SEARCH_FN, op->o_pb );
474 * A preoperation plugin failure will abort the
477 Debug(LDAP_DEBUG_TRACE, "call_search_preop_plugins: search preoperation plugin "
478 "returned %d.\n", rc, 0, 0);
479 if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 ) ||
480 rc == LDAP_SUCCESS ) {
490 static int call_search_rewrite_plugins( Operation *op )
492 if ( slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, op->o_pb ) == 0 ) {
496 * The plugin can set the SLAPI_SEARCH_FILTER.
497 * SLAPI_SEARCH_STRFILER is not normative.
499 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_FILTER, (void *)&op->ors_filter );
500 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
501 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
504 * Also permit other search parameters to be reset. One thing
505 * this doesn't (yet) deal with is plugins that change a root
506 * DSE search to a non-root DSE search...
508 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_TARGET, (void **)&op->o_req_dn.bv_val );
509 op->o_req_dn.bv_len = strlen( op->o_req_dn.bv_val );
511 if( !BER_BVISNULL( &op->o_req_ndn ) ) {
512 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
514 rc = dnNormalize( 0, NULL, NULL, &op->o_req_dn, &op->o_req_ndn,
516 if ( rc != LDAP_SUCCESS ) {
520 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_SCOPE, (void **)&op->ors_scope );
521 slapi_pblock_get( op->o_pb, SLAPI_SEARCH_DEREF, (void **)&op->ors_deref );
523 Debug( LDAP_DEBUG_ARGS, " after compute_rewrite_search filter: %s\n",
524 !BER_BVISEMPTY( &op->ors_filterstr ) ? op->ors_filterstr.bv_val : "empty", 0, 0 );
530 static void call_search_postop_plugins( Operation *op )
532 if ( slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_SEARCH_FN, op->o_pb ) < 0 ) {
533 Debug(LDAP_DEBUG_TRACE, "call_search_postop_plugins: search postoperation plugins "
534 "failed.\n", 0, 0, 0);
538 void slapi_int_dummy(void)
541 * XXX slapi_search_internal() was no getting pulled
542 * in; all manner of linker flags failed to link it.
545 slapi_search_internal( NULL, 0, NULL, NULL, NULL, 0 );
547 #endif /* LDAP_SLAPI */