Debian Codegraph @

servers/slapd/back-asyncmeta/search.c

raw
1 /* search.c - search request handler for back-asyncmeta */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2016-2024 The OpenLDAP Foundation.
6 * Portions Copyright 2016 Symas Corporation.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
11 * Public License.
12 *
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
16 */
17
18 /* ACKNOWLEDGEMENTS:
19 * This work was developed by Symas Corporation
20 * based on back-meta module for inclusion in OpenLDAP Software.
21 * This work was sponsored by Ericsson. */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/socket.h>
28 #include <ac/string.h>
29 #include <ac/time.h>
30 #include "slap.h"
31 #include "../../../libraries/liblber/lber-int.h"
32 #include "../../../libraries/libldap/ldap-int.h"
33 #include "lutil.h"
34 #include "../back-ldap/back-ldap.h"
35 #include "back-asyncmeta.h"
36
37 static void
38 asyncmeta_handle_onerr_stop(Operation *op,
39 SlapReply *rs,
40 a_metaconn_t *mc,
41 bm_context_t *bc,
42 int candidate)
43 {
44 a_metainfo_t *mi = mc->mc_info;
45 int j;
46 ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex);
47 if (asyncmeta_bc_in_queue(mc,bc) == NULL || bc->bc_active > 1) {
48 bc->bc_active--;
49 ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);
50 return;
51 }
52 asyncmeta_drop_bc(mc, bc);
53 for (j=0; j<mi->mi_ntargets; j++) {
54 if (j != candidate && bc->candidates[j].sr_msgid >= 0
55 && mc->mc_conns[j].msc_ld != NULL && !META_BACK_CONN_CREATING( &mc->mc_conns[j] )) {
56 asyncmeta_back_cancel( mc, op,
57 bc->candidates[ j ].sr_msgid, j );
58 }
59 }
60 slap_sl_mem_setctx(op->o_threadctx, op->o_tmpmemctx);
61 operation_counter_init( op, op->o_threadctx );
62 ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);
63 send_ldap_result(op, rs);
64 }
65
66 static int
67 asyncmeta_int_filter2bv( a_dncookie *dc,
68 Filter *f,
69 struct berval *fstr )
70 {
71 int i;
72 Filter *p;
73 struct berval atmp,
74 vtmp,
75 ntmp,
76 *tmp;
77 static struct berval
78 /* better than nothing... */
79 ber_bvfalse = BER_BVC( "(!(objectClass=*))" ),
80 ber_bvtf_false = BER_BVC( "(|)" ),
81 /* better than nothing... */
82 ber_bvtrue = BER_BVC( "(objectClass=*)" ),
83 ber_bvtf_true = BER_BVC( "(&)" ),
84 ber_bverror = BER_BVC( "(?=error)" ),
85 ber_bvunknown = BER_BVC( "(?=unknown)" ),
86 ber_bvnone = BER_BVC( "(?=none)" );
87 ber_len_t len;
88 void *memctx = dc->memctx;
89
90 assert( fstr != NULL );
91 BER_BVZERO( fstr );
92
93 if ( f == NULL ) {
94 ber_dupbv_x( fstr, &ber_bvnone, memctx );
95 return LDAP_OTHER;
96 }
97
98 switch ( ( f->f_choice & SLAPD_FILTER_MASK ) ) {
99 case LDAP_FILTER_EQUALITY:
100 if ( f->f_av_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) {
101 asyncmeta_dn_massage( dc, &f->f_av_value, &vtmp );
102 } else {
103 vtmp = f->f_av_value;
104 }
105
106 filter_escape_value_x( &vtmp, &ntmp, memctx );
107 fstr->bv_len = f->f_av_desc->ad_cname.bv_len + ntmp.bv_len
108 + ( sizeof("(=)") - 1 );
109 fstr->bv_val = dc->op->o_tmpalloc( fstr->bv_len + 1, memctx );
110
111 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
112 f->f_av_desc->ad_cname.bv_val, ntmp.bv_len ? ntmp.bv_val : "" );
113
114 ber_memfree_x( ntmp.bv_val, memctx );
115 break;
116
117 case LDAP_FILTER_GE:
118 filter_escape_value_x( &f->f_av_value, &ntmp, memctx );
119 fstr->bv_len = f->f_av_desc->ad_cname.bv_len + ntmp.bv_len
120 + ( sizeof("(>=)") - 1 );
121 fstr->bv_val = dc->op->o_tmpalloc( fstr->bv_len + 1, memctx );
122
123 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
124 f->f_av_desc->ad_cname.bv_val, ntmp.bv_len ? ntmp.bv_val : "" );
125
126 ber_memfree_x( ntmp.bv_val, memctx );
127 break;
128
129 case LDAP_FILTER_LE:
130 filter_escape_value_x( &f->f_av_value, &ntmp, memctx );
131 fstr->bv_len = f->f_av_desc->ad_cname.bv_len + ntmp.bv_len
132 + ( sizeof("(<=)") - 1 );
133 fstr->bv_val = dc->op->o_tmpalloc( fstr->bv_len + 1, memctx );
134
135 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
136 f->f_av_desc->ad_cname.bv_val, ntmp.bv_len ? ntmp.bv_val : "" );
137
138 ber_memfree_x( ntmp.bv_val, memctx );
139 break;
140
141 case LDAP_FILTER_APPROX:
142 filter_escape_value_x( &f->f_av_value, &ntmp, memctx );
143 fstr->bv_len = f->f_av_desc->ad_cname.bv_len + ntmp.bv_len
144 + ( sizeof("(~=)") - 1 );
145 fstr->bv_val = dc->op->o_tmpalloc( fstr->bv_len + 1, memctx );
146
147 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
148 f->f_av_desc->ad_cname.bv_val, ntmp.bv_len ? ntmp.bv_val : "" );
149
150 ber_memfree_x( ntmp.bv_val, memctx );
151 break;
152
153 case LDAP_FILTER_SUBSTRINGS:
154 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len + ( STRLENOF( "(=*)" ) );
155 fstr->bv_val = dc->op->o_tmpalloc( fstr->bv_len + 128, memctx ); /* FIXME: why 128 ? */
156
157 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
158 f->f_sub_desc->ad_cname.bv_val );
159
160 if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
161 len = fstr->bv_len;
162
163 filter_escape_value_x( &f->f_sub_initial, &ntmp, memctx );
164
165 fstr->bv_len += ntmp.bv_len;
166 fstr->bv_val = dc->op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, memctx );
167
168 snprintf( &fstr->bv_val[len - 2], ntmp.bv_len + 3,
169 /* "(attr=" */ "%s*)",
170 ntmp.bv_len ? ntmp.bv_val : "" );
171
172 ber_memfree_x( ntmp.bv_val, memctx );
173 }
174
175 if ( f->f_sub_any != NULL ) {
176 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
177 len = fstr->bv_len;
178 filter_escape_value_x( &f->f_sub_any[i], &ntmp, memctx );
179
180 fstr->bv_len += ntmp.bv_len + 1;
181 fstr->bv_val = dc->op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, memctx );
182
183 snprintf( &fstr->bv_val[len - 1], ntmp.bv_len + 3,
184 /* "(attr=[init]*[any*]" */ "%s*)",
185 ntmp.bv_len ? ntmp.bv_val : "" );
186 ber_memfree_x( ntmp.bv_val, memctx );
187 }
188 }
189
190 if ( !BER_BVISNULL( &f->f_sub_final ) ) {
191 len = fstr->bv_len;
192
193 filter_escape_value_x( &f->f_sub_final, &ntmp, memctx );
194
195 fstr->bv_len += ntmp.bv_len;
196 fstr->bv_val = dc->op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, memctx );
197
198 snprintf( &fstr->bv_val[len - 1], ntmp.bv_len + 3,
199 /* "(attr=[init*][any*]" */ "%s)",
200 ntmp.bv_len ? ntmp.bv_val : "" );
201
202 ber_memfree_x( ntmp.bv_val, memctx );
203 }
204
205 break;
206
207 case LDAP_FILTER_PRESENT:
208 fstr->bv_len = f->f_desc->ad_cname.bv_len + ( STRLENOF( "(=*)" ) );
209 fstr->bv_val = dc->op->o_tmpalloc( fstr->bv_len + 1, memctx );
210
211 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
212 f->f_desc->ad_cname.bv_val );
213 break;
214
215 case LDAP_FILTER_AND:
216 case LDAP_FILTER_OR:
217 case LDAP_FILTER_NOT:
218 fstr->bv_len = STRLENOF( "(%)" );
219 fstr->bv_val = dc->op->o_tmpalloc( fstr->bv_len + 128, memctx ); /* FIXME: why 128? */
220
221 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
222 f->f_choice == LDAP_FILTER_AND ? '&' :
223 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
224
225 for ( p = f->f_list; p != NULL; p = p->f_next ) {
226 int rc;
227
228 len = fstr->bv_len;
229
230 rc = asyncmeta_int_filter2bv( dc, p, &vtmp );
231 if ( rc != LDAP_SUCCESS ) {
232 return rc;
233 }
234
235 fstr->bv_len += vtmp.bv_len;
236 fstr->bv_val = dc->op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, memctx );
237
238 snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2,
239 /*"("*/ "%s)", vtmp.bv_len ? vtmp.bv_val : "" );
240
241 ber_memfree_x( vtmp.bv_val, memctx );
242 }
243
244 break;
245
246 case LDAP_FILTER_EXT:
247 if ( f->f_mr_desc ) {
248 atmp = f->f_mr_desc->ad_cname;
249
250 } else {
251 BER_BVSTR( &atmp, "" );
252 }
253 filter_escape_value_x( &f->f_mr_value, &ntmp, memctx );
254
255 /* FIXME: cleanup (less ?: operators...) */
256 fstr->bv_len = atmp.bv_len +
257 ( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) +
258 ( !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_len + 1 : 0 ) +
259 ntmp.bv_len + ( STRLENOF( "(:=)" ) );
260 fstr->bv_val = dc->op->o_tmpalloc( fstr->bv_len + 1, memctx );
261
262 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
263 atmp.bv_val,
264 f->f_mr_dnattrs ? ":dn" : "",
265 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "",
266 !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "",
267 ntmp.bv_len ? ntmp.bv_val : "" );
268 ber_memfree_x( ntmp.bv_val, memctx );
269 break;
270
271 case SLAPD_FILTER_COMPUTED:
272 switch ( f->f_result ) {
273 /* FIXME: treat UNDEFINED as FALSE */
274 case SLAPD_COMPARE_UNDEFINED:
275 if ( META_BACK_TGT_NOUNDEFFILTER( dc->target ) ) {
276 return LDAP_COMPARE_FALSE;
277 }
278 /* fallthru */
279
280 case LDAP_COMPARE_FALSE:
281 if ( META_BACK_TGT_T_F( dc->target ) ) {
282 tmp = &ber_bvtf_false;
283 break;
284 }
285 tmp = &ber_bvfalse;
286 break;
287
288 case LDAP_COMPARE_TRUE:
289 if ( META_BACK_TGT_T_F( dc->target ) ) {
290 tmp = &ber_bvtf_true;
291 break;
292 }
293
294 tmp = &ber_bvtrue;
295 break;
296
297 default:
298 tmp = &ber_bverror;
299 break;
300 }
301
302 ber_dupbv_x( fstr, tmp, memctx );
303 break;
304
305 default:
306 ber_dupbv_x( fstr, &ber_bvunknown, memctx );
307 break;
308 }
309
310 return 0;
311 }
312 meta_search_candidate_t
313 asyncmeta_back_search_start(
314 Operation *op,
315 SlapReply *rs,
316 a_metaconn_t *mc,
317 bm_context_t *bc,
318 int candidate,
319 struct berval *prcookie,
320 ber_int_t prsize,
321 int do_lock)
322 {
323 SlapReply *candidates = bc->candidates;
324 a_metainfo_t *mi = ( a_metainfo_t * )mc->mc_info;
325 a_metatarget_t *mt = mi->mi_targets[ candidate ];
326 a_metasingleconn_t *msc = &mc->mc_conns[ candidate ];
327 a_dncookie dc;
328 struct berval realbase = op->o_req_dn;
329 char **attrs;
330 int realscope = op->ors_scope;
331 struct berval mbase = BER_BVNULL;
332 int rc;
333 struct berval filterbv = BER_BVNULL;
334 meta_search_candidate_t retcode;
335 int timelimit;
336 LDAPControl **ctrls = NULL;
337 BerElement *ber = NULL;
338 ber_int_t msgid;
339 ber_socket_t s = -1;
340 #ifdef SLAPD_META_CLIENT_PR
341 LDAPControl **save_ctrls = NULL;
342 #endif /* SLAPD_META_CLIENT_PR */
343
344 /* this should not happen; just in case... */
345 if ( msc->msc_ld == NULL ) {
346 Debug( LDAP_DEBUG_ANY,
347 "%s: asyncmeta_back_search_start candidate=%d ld=NULL%s.\n",
348 op->o_log_prefix, candidate,
349 META_BACK_ONERR_STOP( mi ) ? "" : " (ignored)" );
350 candidates[ candidate ].sr_err = LDAP_OTHER;
351 if ( META_BACK_ONERR_STOP( mi ) ) {
352 return META_SEARCH_ERR;
353 }
354 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
355 return META_SEARCH_NOT_CANDIDATE;
356 }
357
358 Debug( LDAP_DEBUG_TRACE, "%s >>> asyncmeta_back_search_start: dn=%s filter=%s\n",
359 op->o_log_prefix, op->o_req_dn.bv_val, op->ors_filterstr.bv_val );
360 /*
361 * modifies the base according to the scope, if required
362 */
363 if ( mt->mt_nsuffix.bv_len > op->o_req_ndn.bv_len ) {
364 switch ( op->ors_scope ) {
365 case LDAP_SCOPE_SUBTREE:
366 /*
367 * make the target suffix the new base
368 * FIXME: this is very forgiving, because
369 * "illegal" searchBases may be turned
370 * into the suffix of the target; however,
371 * the requested searchBase already passed
372 * thru the candidate analyzer...
373 */
374 if ( dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) ) {
375 realbase = mt->mt_nsuffix;
376 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
377 realscope = LDAP_SCOPE_SUBORDINATE;
378 }
379
380 } else {
381 /*
382 * this target is no longer candidate
383 */
384 retcode = META_SEARCH_NOT_CANDIDATE;
385 goto doreturn;
386 }
387 break;
388
389 case LDAP_SCOPE_SUBORDINATE:
390 case LDAP_SCOPE_ONELEVEL:
391 {
392 struct berval rdn = mt->mt_nsuffix;
393 rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
394 if ( dnIsOneLevelRDN( &rdn )
395 && dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) )
396 {
397 /*
398 * if there is exactly one level,
399 * make the target suffix the new
400 * base, and make scope "base"
401 */
402 realbase = mt->mt_nsuffix;
403 if ( op->ors_scope == LDAP_SCOPE_SUBORDINATE ) {
404 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
405 realscope = LDAP_SCOPE_SUBORDINATE;
406 } else {
407 realscope = LDAP_SCOPE_SUBTREE;
408 }
409 } else {
410 realscope = LDAP_SCOPE_BASE;
411 }
412 break;
413 } /* else continue with the next case */
414 }
415
416 case LDAP_SCOPE_BASE:
417 /*
418 * this target is no longer candidate
419 */
420 retcode = META_SEARCH_NOT_CANDIDATE;
421 goto doreturn;
422 }
423 }
424
425 /* check filter expression */
426 if ( mt->mt_filter ) {
427 metafilter_t *mf;
428 for ( mf = mt->mt_filter; mf; mf = mf->mf_next ) {
429 if ( regexec( &mf->mf_regex, op->ors_filterstr.bv_val, 0, NULL, 0 ) == 0 )
430 break;
431 }
432 /* nothing matched, this target is no longer a candidate */
433 if ( !mf ) {
434 retcode = META_SEARCH_NOT_CANDIDATE;
435 goto doreturn;
436 }
437 }
438
439 /*
440 * Rewrite the search base, if required
441 */
442 dc.op = op;
443 dc.target = mt;
444 dc.memctx = op->o_tmpmemctx;
445 dc.to_from = MASSAGE_REQ;
446 asyncmeta_dn_massage( &dc, &realbase, &mbase );
447
448 attrs = anlist2charray_x( op->ors_attrs, 0, op->o_tmpmemctx );
449
450 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
451 timelimit = op->ors_tlimit > 0 ? op->ors_tlimit : 1;
452 } else {
453 timelimit = -1; /* no limit */
454 }
455
456 #ifdef SLAPD_META_CLIENT_PR
457 save_ctrls = op->o_ctrls;
458 {
459 LDAPControl *pr_c = NULL;
460 int i = 0, nc = 0;
461
462 if ( save_ctrls ) {
463 for ( ; save_ctrls[i] != NULL; i++ );
464 nc = i;
465 pr_c = ldap_control_find( LDAP_CONTROL_PAGEDRESULTS, save_ctrls, NULL );
466 }
467
468 if ( pr_c != NULL ) nc--;
469 if ( mt->mt_ps > 0 || prcookie != NULL ) nc++;
470
471 if ( mt->mt_ps > 0 || prcookie != NULL || pr_c != NULL ) {
472 int src = 0, dst = 0;
473 BerElementBuffer berbuf;
474 BerElement *ber = (BerElement *)&berbuf;
475 struct berval val = BER_BVNULL;
476 ber_len_t len;
477
478 len = sizeof( LDAPControl * )*( nc + 1 ) + sizeof( LDAPControl );
479
480 if ( mt->mt_ps > 0 || prcookie != NULL ) {
481 struct berval nullcookie = BER_BVNULL;
482 ber_tag_t tag;
483
484 if ( prsize == 0 && mt->mt_ps > 0 ) prsize = mt->mt_ps;
485 if ( prcookie == NULL ) prcookie = &nullcookie;
486
487 ber_init2( ber, NULL, LBER_USE_DER );
488 tag = ber_printf( ber, "{iO}", prsize, prcookie );
489 if ( tag == LBER_ERROR ) {
490 /* error */
491 (void) ber_free_buf( ber );
492 goto done_pr;
493 }
494
495 tag = ber_flatten2( ber, &val, 0 );
496 if ( tag == LBER_ERROR ) {
497 /* error */
498 (void) ber_free_buf( ber );
499 goto done_pr;
500 }
501
502 len += val.bv_len + 1;
503 }
504
505 op->o_ctrls = op->o_tmpalloc( len, op->o_tmpmemctx );
506 if ( save_ctrls ) {
507 for ( ; save_ctrls[ src ] != NULL; src++ ) {
508 if ( save_ctrls[ src ] != pr_c ) {
509 op->o_ctrls[ dst ] = save_ctrls[ src ];
510 dst++;
511 }
512 }
513 }
514
515 if ( mt->mt_ps > 0 || prcookie != NULL ) {
516 op->o_ctrls[ dst ] = (LDAPControl *)&op->o_ctrls[ nc + 1 ];
517
518 op->o_ctrls[ dst ]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
519 op->o_ctrls[ dst ]->ldctl_iscritical = 1;
520
521 op->o_ctrls[ dst ]->ldctl_value.bv_val = (char *)&op->o_ctrls[ dst ][ 1 ];
522 AC_MEMCPY( op->o_ctrls[ dst ]->ldctl_value.bv_val, val.bv_val, val.bv_len + 1 );
523 op->o_ctrls[ dst ]->ldctl_value.bv_len = val.bv_len;
524 dst++;
525
526 (void)ber_free_buf( ber );
527 }
528
529 op->o_ctrls[ dst ] = NULL;
530 }
531 done_pr:;
532 }
533 #endif /* SLAPD_META_CLIENT_PR */
534
535 asyncmeta_set_msc_time(msc);
536 ctrls = op->o_ctrls;
537
538 if ( asyncmeta_controls_add( op, rs, mc, candidate, bc->is_root, &ctrls )
539 != LDAP_SUCCESS )
540 {
541 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
542 retcode = META_SEARCH_NOT_CANDIDATE;
543 goto done;
544 }
545
546 /*
547 * Starts the search
548 */
549 /* someone reset the connection */
550 if (!( LDAP_BACK_CONN_ISBOUND( msc )
551 || LDAP_BACK_CONN_ISANON( msc )) || msc->msc_ld == NULL ) {
552 Debug( asyncmeta_debug, "msc %p not initialized at %s:%d\n", msc, __FILE__, __LINE__ );
553 goto error_unavailable;
554 }
555 rc = asyncmeta_int_filter2bv( &dc, op->ors_filter, &filterbv );
556 if ( rc ) {
557 retcode = META_SEARCH_ERR;
558 goto done;
559 }
560
561 ber = ldap_build_search_req( msc->msc_ld,
562 mbase.bv_val, realscope, filterbv.bv_val,
563 attrs, op->ors_attrsonly,
564 ctrls, NULL, timelimit, op->ors_slimit, op->ors_deref,
565 &msgid );
566 if (!ber) {
567 Debug( asyncmeta_debug, "%s asyncmeta_back_search_start: Operation encoding failed with errno %d\n",
568 op->o_log_prefix, msc->msc_ld->ld_errno );
569 rs->sr_err = LDAP_OPERATIONS_ERROR;
570 rs->sr_text = "Failed to encode proxied request";
571 retcode = META_SEARCH_ERR;
572 goto done;
573 }
574
575 if (ber) {
576 struct timeval tv = {0, mt->mt_network_timeout*1000};
577
578 if (!( LDAP_BACK_CONN_ISBOUND( msc )
579 || LDAP_BACK_CONN_ISANON( msc )) || msc->msc_ld == NULL ) {
580 Debug( asyncmeta_debug, "msc %p not initialized at %s:%d\n", msc, __FILE__, __LINE__ );
581 goto error_unavailable;
582 }
583
584 ldap_get_option( msc->msc_ld, LDAP_OPT_DESC, &s );
585 if (s < 0) {
586 Debug( asyncmeta_debug, "msc %p not initialized at %s:%d\n", msc, __FILE__, __LINE__ );
587 goto error_unavailable;
588 }
589
590 rc = ldap_int_poll( msc->msc_ld, s, &tv, 1);
591 if (rc < 0) {
592 Debug( asyncmeta_debug, "msc %p not writable within network timeout %s:%d\n", msc, __FILE__, __LINE__ );
593 if ((msc->msc_result_time + META_BACK_RESULT_INTERVAL) < slap_get_time()) {
594 rc = LDAP_SERVER_DOWN;
595 } else {
596 goto error_unavailable;
597 }
598 } else {
599 candidates[ candidate ].sr_msgid = msgid;
600 rc = ldap_send_initial_request( msc->msc_ld, LDAP_REQ_SEARCH,
601 mbase.bv_val, ber, msgid );
602 if (rc == msgid)
603 rc = LDAP_SUCCESS;
604 else
605 rc = LDAP_SERVER_DOWN;
606 ber = NULL;
607 }
608
609 switch ( rc ) {
610 case LDAP_SUCCESS:
611 retcode = META_SEARCH_CANDIDATE;
612 asyncmeta_set_msc_time(msc);
613 goto done;
614
615 case LDAP_SERVER_DOWN:
616 /* do not lock if called from asyncmeta_handle_bind_result. Also do not reset the connection */
617 if (do_lock > 0) {
618 ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex);
619 asyncmeta_reset_msc(NULL, mc, candidate, 0, __FUNCTION__);
620 ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);
621 }
622 Debug( asyncmeta_debug, "msc %p ldap_send_initial_request failed. %s:%d\n", msc, __FILE__, __LINE__ );
623 goto error_unavailable;
624
625 default:
626 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
627 retcode = META_SEARCH_NOT_CANDIDATE;
628 goto done;
629 }
630 }
631
632 error_unavailable:
633 if (ber)
634 ber_free(ber, 1);
635 switch (bc->nretries[candidate]) {
636 case -1: /* nretries = forever */
637 retcode = META_SEARCH_NEED_BIND;
638 ldap_pvt_thread_yield();
639 break;
640 case 0: /* no retries left */
641 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
642 rs->sr_err = LDAP_UNAVAILABLE;
643 rs->sr_text = "Unable to send search request to target";
644 retcode = META_SEARCH_ERR;
645 break;
646 default: /* more retries left - try to rebind and go again */
647 retcode = META_SEARCH_NEED_BIND;
648 bc->nretries[candidate]--;
649 ldap_pvt_thread_yield();
650 break;
651 }
652 done:;
653 #if 0
654 (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
655 #endif
656 #ifdef SLAPD_META_CLIENT_PR
657 if ( save_ctrls != op->o_ctrls ) {
658 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
659 op->o_ctrls = save_ctrls;
660 }
661 #endif /* SLAPD_META_CLIENT_PR */
662
663 if ( mbase.bv_val != realbase.bv_val ) {
664 op->o_tmpfree( mbase.bv_val, op->o_tmpmemctx );
665 }
666
667 doreturn:;
668 Debug( LDAP_DEBUG_TRACE, "%s <<< asyncmeta_back_search_start[%p] (fd %d)=%d\n", op->o_log_prefix, msc, s, candidates[candidate].sr_msgid );
669 return retcode;
670 }
671
672 int
673 asyncmeta_back_search( Operation *op, SlapReply *rs )
674 {
675 a_metainfo_t *mi = ( a_metainfo_t * )op->o_bd->be_private;
676 time_t timeout = 0;
677 int rc = 0;
678 int ncandidates = 0, initial_candidates = 0;
679 long i;
680 SlapReply *candidates = NULL;
681 void *thrctx = op->o_threadctx;
682 bm_context_t *bc;
683 a_metaconn_t *mc;
684 int msc_decr = 0;
685 int max_pending_ops = (mi->mi_max_pending_ops == 0) ? META_BACK_CFG_MAX_PENDING_OPS : mi->mi_max_pending_ops;
686 int check_bind = 0;
687
688 rs_assert_ready( rs );
689 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia, we can set rs = non-entry */
690
691 if ( mi->mi_ntargets == 0 ) {
692 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
693 rs->sr_text = "No targets are configured for this database";
694 send_ldap_result(op, rs);
695 return rs->sr_err;
696 }
697
698 /*
699 * controls are set in ldap_back_dobind()
700 *
701 * FIXME: in case of values return filter, we might want
702 * to map attrs and maybe rewrite value
703 */
704
705 asyncmeta_new_bm_context(op, rs, &bc, mi->mi_ntargets, mi );
706 if (bc == NULL) {
707 rs->sr_err = LDAP_OTHER;
708 send_ldap_result(op, rs);
709 return rs->sr_err;
710 }
711
712 candidates = bc->candidates;
713 mc = asyncmeta_getconn( op, rs, candidates, NULL, LDAP_BACK_DONTSEND, 0);
714 if ( !mc || rs->sr_err != LDAP_SUCCESS) {
715 send_ldap_result(op, rs);
716 return rs->sr_err;
717 }
718
719 /*
720 * Inits searches
721 */
722
723 for ( i = 0; i < mi->mi_ntargets; i++ ) {
724 /* reset sr_msgid; it is used in most loops
725 * to check if that target is still to be considered */
726 candidates[i].sr_msgid = META_MSGID_UNDEFINED;
727 /* a target is marked as candidate by asyncmeta_getconn();
728 * if for any reason (an error, it's over or so) it is
729 * no longer active, sr_msgid is set to META_MSGID_IGNORE
730 * but it remains candidate, which means it has been active
731 * at some point during the operation. This allows to
732 * use its response code and more to compute the final
733 * response */
734 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
735 continue;
736 }
737
738 candidates[ i ].sr_matched = NULL;
739 candidates[ i ].sr_text = NULL;
740 candidates[ i ].sr_ref = NULL;
741 candidates[ i ].sr_ctrls = NULL;
742 candidates[ i ].sr_nentries = 0;
743 candidates[ i ].sr_type = -1;
744
745 /* get largest timeout among candidates */
746 if ( mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ]
747 && mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ] > timeout )
748 {
749 timeout = mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ];
750 }
751 }
752
753 if ( op->ors_tlimit != SLAP_NO_LIMIT && (timeout == 0 || op->ors_tlimit < timeout)) {
754 bc->searchtime = 1;
755 bc->timeout = op->ors_tlimit;
756 } else {
757 bc->timeout = timeout;
758 }
759
760 bc->stoptime = op->o_time + bc->timeout;
761 bc->bc_active = 1;
762
763 if (mc->pending_ops >= max_pending_ops) {
764 rs->sr_err = LDAP_BUSY;
765 rs->sr_text = "Maximum pending ops limit exceeded";
766 send_ldap_result(op, rs);
767 return rs->sr_err;
768 }
769
770 ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex);
771 rc = asyncmeta_add_message_queue(mc, bc);
772 for ( i = 0; i < mi->mi_ntargets; i++ ) {
773 mc->mc_conns[i].msc_active++;
774 }
775 ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);
776
777 if (rc != LDAP_SUCCESS) {
778 rs->sr_err = LDAP_BUSY;
779 rs->sr_text = "Maximum pending ops limit exceeded";
780 send_ldap_result(op, rs);
781 goto finish;
782 }
783
784 for ( i = 0; i < mi->mi_ntargets; i++ ) {
785 if ( !META_IS_CANDIDATE( &candidates[ i ] )
786 || candidates[ i ].sr_err != LDAP_SUCCESS )
787 {
788 continue;
789 }
790 retry:
791 if (bc->timeout && bc->stoptime < slap_get_time() && META_BACK_ONERR_STOP( mi )) {
792 int timeout_err;
793 const char *timeout_text;
794 if (bc->searchtime) {
795 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
796 timeout_text = NULL;
797 } else {
798 timeout_err = op->o_protocol >= LDAP_VERSION3 ?
799 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
800 timeout_text = "Operation timed out before it was sent to target";
801 }
802 rs->sr_err = timeout_err;
803 rs->sr_text = timeout_text;
804 asyncmeta_handle_onerr_stop(op,rs,mc,bc,i);
805 goto finish;
806
807 }
808
809 if (op->o_abandon) {
810 rs->sr_err = SLAPD_ABANDON;
811 asyncmeta_handle_onerr_stop(op,rs,mc,bc,i);
812 goto finish;
813 }
814
815 rc = asyncmeta_dobind_init_with_retry(op, rs, bc, mc, i);
816 switch (rc)
817 {
818 case META_SEARCH_CANDIDATE:
819 /* target is already bound, just send the search request */
820 ncandidates++;
821 Debug( LDAP_DEBUG_TRACE, "%s asyncmeta_back_search: IS_CANDIDATE "
822 "cnd=\"%ld\"\n", op->o_log_prefix, i );
823
824 rc = asyncmeta_back_search_start( op, rs, mc, bc, i, NULL, 0 , 1);
825 if (rc == META_SEARCH_ERR) {
826 META_CANDIDATE_CLEAR(&candidates[i]);
827 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
828 if ( META_BACK_ONERR_STOP( mi ) ) {
829 asyncmeta_handle_onerr_stop(op,rs,mc,bc,i);
830 goto finish;
831 }
832 else {
833 continue;
834 }
835 } else if (rc == META_SEARCH_NEED_BIND) {
836 goto retry;
837 }
838 break;
839 case META_SEARCH_NOT_CANDIDATE:
840 Debug( LDAP_DEBUG_TRACE, "%s asyncmeta_back_search: NOT_CANDIDATE "
841 "cnd=\"%ld\"\n", op->o_log_prefix, i );
842 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
843 break;
844
845 case META_SEARCH_NEED_BIND:
846 case META_SEARCH_BINDING:
847 Debug( LDAP_DEBUG_TRACE, "%s asyncmeta_back_search: BINDING "
848 "cnd=\"%ld\" mc %p msc %p\n", op->o_log_prefix, i , mc, &mc->mc_conns[i]);
849 check_bind++;
850 ncandidates++;
851 /* Todo add the context to the message queue but do not send the request
852 the receiver must send this when we are done binding */
853 /* question - how would do receiver know to which targets??? */
854 break;
855
856 case META_SEARCH_ERR:
857 Debug( LDAP_DEBUG_TRACE, "%s asyncmeta_back_search: SEARCH_ERR "
858 "cnd=\"%ldd\"\n", op->o_log_prefix, i );
859 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
860 candidates[ i ].sr_type = REP_RESULT;
861
862 if ( META_BACK_ONERR_STOP( mi ) ) {
863 asyncmeta_handle_onerr_stop(op,rs,mc,bc,i);
864 goto finish;
865 }
866 else {
867 continue;
868 }
869 break;
870
871 default:
872 assert( 0 );
873 break;
874 }
875 }
876
877 initial_candidates = ncandidates;
878
879 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
880 char cnd[ SLAP_TEXT_BUFLEN ];
881 int c;
882
883 for ( c = 0; c < mi->mi_ntargets; c++ ) {
884 if ( META_IS_CANDIDATE( &candidates[ c ] ) ) {
885 cnd[ c ] = '*';
886 } else {
887 cnd[ c ] = ' ';
888 }
889 }
890 cnd[ c ] = '\0';
891
892 Debug( LDAP_DEBUG_TRACE, "%s asyncmeta_back_search: ncandidates=%d "
893 "cnd=\"%s\"\n", op->o_log_prefix, ncandidates, cnd );
894 }
895
896 if ( initial_candidates == 0 ) {
897 /* NOTE: here we are not sending any matchedDN;
898 * this is intended, because if the back-meta
899 * is serving this search request, but no valid
900 * candidate could be looked up, it means that
901 * there is a hole in the mapping of the targets
902 * and thus no knowledge of any remote superior
903 * is available */
904 Debug( LDAP_DEBUG_ANY, "%s asyncmeta_back_search: "
905 "base=\"%s\" scope=%d: "
906 "no candidate could be selected\n",
907 op->o_log_prefix, op->o_req_dn.bv_val,
908 op->ors_scope );
909
910 /* FIXME: we're sending the first error we encounter;
911 * maybe we should pick the worst... */
912 rc = LDAP_NO_SUCH_OBJECT;
913 for ( i = 0; i < mi->mi_ntargets; i++ ) {
914 if ( META_IS_CANDIDATE( &candidates[ i ] )
915 && candidates[ i ].sr_err != LDAP_SUCCESS )
916 {
917 rc = candidates[ i ].sr_err;
918 break;
919 }
920 }
921 rs->sr_err = rc;
922 ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex);
923 asyncmeta_drop_bc(mc, bc);
924 ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);
925 send_ldap_result(op, rs);
926 goto finish;
927 }
928
929 /* If we were processing many targets the result from a pending Bind
930 * on an earlier target may have arrived while we were sending to a
931 * later target. See if we can now send our pending request.
932 */
933 if ( check_bind ) {
934 for ( i = 0; i < mi->mi_ntargets; i++ ) {
935 if ( candidates[ i ].sr_msgid == META_MSGID_GOT_BIND ) {
936 rc = asyncmeta_back_search_start( op, rs, mc, bc, i, NULL, 0, 1 );
937 if ( rc == META_SEARCH_ERR ) {
938 META_CANDIDATE_CLEAR( &candidates[i] );
939 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
940 if ( META_BACK_ONERR_STOP( mi ) ) {
941 asyncmeta_handle_onerr_stop(op,rs,mc,bc,i);
942 goto finish;
943 }
944 }
945 }
946 }
947 }
948
949 ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex);
950 for ( i = 0; i < mi->mi_ntargets; i++ ) {
951 mc->mc_conns[i].msc_active--;
952 }
953 msc_decr = 1;
954
955 asyncmeta_start_listeners(mc, candidates, bc);
956 bc->bc_active--;
957 ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);
958 rs->sr_err = SLAPD_ASYNCOP;
959
960 finish:
961 /* we ended up straight here due to error and need to reset the msc_active*/
962 if (msc_decr == 0) {
963 ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex);
964 for ( i = 0; i < mi->mi_ntargets; i++ ) {
965 mc->mc_conns[i].msc_active--;
966 }
967 ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);
968 }
969 return rs->sr_err;
970 }