Debian Codegraph @

servers/slapd/back-meta/search.c

raw
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 *
4 * Copyright 1999-2024 The OpenLDAP Foundation.
5 * Portions Copyright 2001-2003 Pierangelo Masarati.
6 * Portions Copyright 1999-2003 Howard Chu.
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 /* ACKNOWLEDGEMENTS:
18 * This work was initially developed by the Howard Chu for inclusion
19 * in OpenLDAP Software and subsequently enhanced by Pierangelo
20 * Masarati.
21 */
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
31 #include "lutil.h"
32 #include "slap.h"
33 #include "../back-ldap/back-ldap.h"
34 #include "back-meta.h"
35 #include "../../../libraries/liblber/lber-int.h"
36
37 /* IGNORE means that target does not (no longer) participate
38 * in the search;
39 * NOTREADY means the search on that target has not been initialized yet
40 */
41 #define META_MSGID_IGNORE (-1)
42 #define META_MSGID_NEED_BIND (-2)
43 #define META_MSGID_CONNECTING (-3)
44
45 static int
46 meta_send_entry(
47 Operation *op,
48 SlapReply *rs,
49 metaconn_t *mc,
50 int i,
51 LDAPMessage *e );
52
53 typedef enum meta_search_candidate_t {
54 META_SEARCH_UNDEFINED = -2,
55 META_SEARCH_ERR = -1,
56 META_SEARCH_NOT_CANDIDATE,
57 META_SEARCH_CANDIDATE,
58 META_SEARCH_BINDING,
59 META_SEARCH_NEED_BIND,
60 META_SEARCH_CONNECTING
61 } meta_search_candidate_t;
62
63 /*
64 * meta_search_dobind_init()
65 *
66 * initiates bind for a candidate target of a search.
67 */
68 static meta_search_candidate_t
69 meta_search_dobind_init(
70 Operation *op,
71 SlapReply *rs,
72 metaconn_t **mcp,
73 int candidate,
74 SlapReply *candidates )
75 {
76 metaconn_t *mc = *mcp;
77 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
78 metatarget_t *mt = mi->mi_targets[ candidate ];
79 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
80
81 struct berval binddn = msc->msc_bound_ndn,
82 cred = msc->msc_cred;
83 int method;
84
85 int rc;
86
87 meta_search_candidate_t retcode;
88
89 Debug( LDAP_DEBUG_TRACE, "%s >>> meta_search_dobind_init[%d]\n",
90 op->o_log_prefix, candidate );
91
92 /*
93 * all the targets are already bound as pseudoroot
94 */
95 if ( mc->mc_authz_target == META_BOUND_ALL ) {
96 return META_SEARCH_CANDIDATE;
97 }
98
99 retcode = META_SEARCH_BINDING;
100 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
101 if ( LDAP_BACK_CONN_ISBOUND( msc ) || LDAP_BACK_CONN_ISANON( msc ) ) {
102 /* already bound (or anonymous) */
103
104 #ifdef DEBUG_205
105 int bound = 0;
106
107 if ( LDAP_BACK_CONN_ISBOUND( msc ) ) {
108 bound = 1;
109 }
110
111 Debug(LDAP_DEBUG_ANY,
112 "### %s meta_search_dobind_init[%d] mc=%p ld=%p%s DN=\"%s\"\n",
113 op->o_log_prefix, candidate, (void *)mc,
114 (void *)msc->msc_ld, bound ? " bound" : " anonymous",
115 bound == 0 ? "" : msc->msc_bound_ndn.bv_val );
116 #endif /* DEBUG_205 */
117
118 retcode = META_SEARCH_CANDIDATE;
119
120 } else if ( META_BACK_CONN_CREATING( msc ) || LDAP_BACK_CONN_BINDING( msc ) ) {
121 /* another thread is binding the target for this conn; wait */
122
123 #ifdef DEBUG_205
124 Debug(LDAP_DEBUG_ANY,
125 "### %s meta_search_dobind_init[%d] mc=%p ld=%p needbind\n",
126 op->o_log_prefix, candidate, (void *)mc,
127 (void *)msc->msc_ld );
128 #endif /* DEBUG_205 */
129
130 candidates[ candidate ].sr_msgid = META_MSGID_NEED_BIND;
131 retcode = META_SEARCH_NEED_BIND;
132
133 } else {
134 /* we'll need to bind the target for this conn */
135
136 #ifdef DEBUG_205
137 Debug(LDAP_DEBUG_ANY,
138 "### %s meta_search_dobind_init[%d] mc=%p ld=%p binding\n",
139 op->o_log_prefix, candidate, (void *)mc,
140 (void *)msc->msc_ld );
141 #endif /* DEBUG_205 */
142
143 if ( msc->msc_ld == NULL ) {
144 /* for some reason (e.g. because formerly in "binding"
145 * state, with eventual connection expiration or invalidation)
146 * it was not initialized as expected */
147
148 Debug( LDAP_DEBUG_ANY, "%s meta_search_dobind_init[%d] mc=%p ld=NULL\n",
149 op->o_log_prefix, candidate, (void *)mc );
150
151 rc = meta_back_init_one_conn( op, rs, *mcp, candidate,
152 LDAP_BACK_CONN_ISPRIV( *mcp ), LDAP_BACK_DONTSEND, 0 );
153 switch ( rc ) {
154 case LDAP_SUCCESS:
155 assert( msc->msc_ld != NULL );
156 break;
157
158 case LDAP_SERVER_DOWN:
159 case LDAP_UNAVAILABLE:
160 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
161 goto down;
162
163 default:
164 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
165 goto other;
166 }
167 }
168
169 LDAP_BACK_CONN_BINDING_SET( msc );
170 }
171
172 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
173
174 if ( retcode != META_SEARCH_BINDING ) {
175 return retcode;
176 }
177
178 /* NOTE: this obsoletes pseudorootdn */
179 if ( op->o_conn != NULL &&
180 ( BER_BVISNULL( &msc->msc_bound_ndn ) ||
181 BER_BVISEMPTY( &msc->msc_bound_ndn ) ||
182 ( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
183 {
184 rc = meta_back_proxy_authz_cred( mc, candidate, op, rs, LDAP_BACK_DONTSEND, &binddn, &cred, &method );
185 switch ( rc ) {
186 case LDAP_SUCCESS:
187 break;
188 case LDAP_UNAVAILABLE:
189 goto down;
190 default:
191 goto other;
192 }
193
194 /* NOTE: we copy things here, even if bind didn't succeed yet,
195 * because the connection is not shared until bind is over */
196 if ( !BER_BVISNULL( &binddn ) ) {
197 ber_bvreplace( &msc->msc_bound_ndn, &binddn );
198 if ( META_BACK_TGT_SAVECRED( mt ) && !BER_BVISNULL( &cred ) ) {
199 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
200 memset( msc->msc_cred.bv_val, 0,
201 msc->msc_cred.bv_len );
202 }
203 ber_bvreplace( &msc->msc_cred, &cred );
204 }
205 }
206
207 if ( LDAP_BACK_CONN_ISBOUND( msc ) ) {
208 /* apparently, idassert was configured with SASL bind,
209 * so bind occurred inside meta_back_proxy_authz_cred() */
210 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
211 LDAP_BACK_CONN_BINDING_CLEAR( msc );
212 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
213 return META_SEARCH_CANDIDATE;
214 }
215
216 /* paranoid */
217 switch ( method ) {
218 case LDAP_AUTH_NONE:
219 case LDAP_AUTH_SIMPLE:
220 /* do a simple bind with binddn, cred */
221 break;
222
223 default:
224 assert( 0 );
225 break;
226 }
227 }
228
229 assert( msc->msc_ld != NULL );
230
231 /* connect must be async only the first time... */
232 ldap_set_option( msc->msc_ld, LDAP_OPT_CONNECT_ASYNC, LDAP_OPT_ON );
233
234 retry:;
235 if ( !BER_BVISEMPTY( &binddn ) && BER_BVISEMPTY( &cred ) ) {
236 /* bind anonymously? */
237 Debug( LDAP_DEBUG_ANY, "%s meta_search_dobind_init[%d] mc=%p: "
238 "non-empty dn with empty cred; binding anonymously\n",
239 op->o_log_prefix, candidate, (void *)mc );
240 cred = slap_empty_bv;
241
242 } else if ( BER_BVISEMPTY( &binddn ) && !BER_BVISEMPTY( &cred ) ) {
243 /* error */
244 Debug( LDAP_DEBUG_ANY, "%s meta_search_dobind_init[%d] mc=%p: "
245 "empty dn with non-empty cred: error\n",
246 op->o_log_prefix, candidate, (void *)mc );
247 rc = LDAP_OTHER;
248 goto other;
249 }
250
251 rc = ldap_sasl_bind( msc->msc_ld, binddn.bv_val, LDAP_SASL_SIMPLE, &cred,
252 NULL, NULL, &candidates[ candidate ].sr_msgid );
253
254 #ifdef DEBUG_205
255 Debug(LDAP_DEBUG_ANY,
256 "### %s meta_search_dobind_init[%d] mc=%p ld=%p rc=%d\n",
257 op->o_log_prefix, candidate, (void *)mc,
258 (void *)mc->mc_conns[candidate].msc_ld, rc );
259 #endif /* DEBUG_205 */
260
261 switch ( rc ) {
262 case LDAP_SUCCESS:
263 assert( candidates[ candidate ].sr_msgid >= 0 );
264 META_BINDING_SET( &candidates[ candidate ] );
265 return META_SEARCH_BINDING;
266
267 case LDAP_X_CONNECTING:
268 /* must retry, same conn */
269 candidates[ candidate ].sr_msgid = META_MSGID_CONNECTING;
270 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
271 LDAP_BACK_CONN_BINDING_CLEAR( msc );
272 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
273 return META_SEARCH_CONNECTING;
274
275 case LDAP_SERVER_DOWN:
276 down:;
277 /* This is the worst thing that could happen:
278 * the search will wait until the retry is over. */
279 if ( !META_IS_RETRYING( &candidates[ candidate ] ) ) {
280 META_RETRYING_SET( &candidates[ candidate ] );
281
282 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
283
284 assert( mc->mc_refcnt > 0 );
285 if ( LogTest( LDAP_DEBUG_ANY ) ) {
286 /* this lock is required; however,
287 * it's invoked only when logging is on */
288 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
289 Debug(LDAP_DEBUG_ANY,
290 "%s meta_search_dobind_init[%d]: retrying URI=\"%s\" DN=\"%s\".\n",
291 op->o_log_prefix, candidate, mt->mt_uri,
292 BER_BVISNULL(&msc->msc_bound_ndn) ? "" : msc->msc_bound_ndn.bv_val );
293 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
294 }
295
296 meta_clear_one_candidate( op, mc, candidate );
297 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
298
299 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
300
301 /* mc here must be the regular mc, reset and ready for init */
302 rc = meta_back_init_one_conn( op, rs, mc, candidate,
303 LDAP_BACK_CONN_ISPRIV( mc ), LDAP_BACK_DONTSEND, 0 );
304
305 if ( rc == LDAP_SUCCESS ) {
306 LDAP_BACK_CONN_BINDING_SET( msc );
307 }
308
309 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
310
311 if ( rc == LDAP_SUCCESS ) {
312 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
313 binddn = msc->msc_bound_ndn;
314 cred = msc->msc_cred;
315 goto retry;
316 }
317 }
318
319 if ( *mcp == NULL ) {
320 retcode = META_SEARCH_ERR;
321 rc = LDAP_UNAVAILABLE;
322 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
323 break;
324 }
325 /* fall thru */
326
327 default:
328 other:;
329 /* convert rc to the correct LDAP error and send it back to the client:
330 assign the error to rs, so we can use it as argument to slap_map_api2result
331 and then assign the output back to rs->sr_err */
332 rs->sr_err = rc;
333 rs->sr_err = slap_map_api2result( rs );
334
335 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
336 meta_clear_one_candidate( op, mc, candidate );
337 candidates[ candidate ].sr_err = rs->sr_err;
338 if ( META_BACK_ONERR_STOP( mi ) ) {
339 LDAP_BACK_CONN_TAINTED_SET( mc );
340 meta_back_release_conn_lock( mi, mc, 0 );
341 *mcp = NULL;
342
343 retcode = META_SEARCH_ERR;
344
345 } else {
346 retcode = META_SEARCH_NOT_CANDIDATE;
347 }
348 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
349 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
350 break;
351 }
352
353 return retcode;
354 }
355
356 static meta_search_candidate_t
357 meta_search_dobind_result(
358 Operation *op,
359 SlapReply *rs,
360 metaconn_t **mcp,
361 int candidate,
362 SlapReply *candidates,
363 LDAPMessage *res )
364 {
365 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
366 metatarget_t *mt = mi->mi_targets[ candidate ];
367 metaconn_t *mc = *mcp;
368 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
369
370 meta_search_candidate_t retcode = META_SEARCH_NOT_CANDIDATE;
371 int rc;
372
373 assert( msc->msc_ld != NULL );
374
375 /* FIXME: matched? referrals? response controls? */
376 rc = ldap_parse_result( msc->msc_ld, res,
377 &candidates[ candidate ].sr_err,
378 NULL, NULL, NULL, NULL, 0 );
379 if ( rc != LDAP_SUCCESS ) {
380 candidates[ candidate ].sr_err = rc;
381 }
382 rc = slap_map_api2result( &candidates[ candidate ] );
383
384 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
385 LDAP_BACK_CONN_BINDING_CLEAR( msc );
386 if ( rc != LDAP_SUCCESS ) {
387 meta_clear_one_candidate( op, mc, candidate );
388 candidates[ candidate ].sr_err = rc;
389 if ( META_BACK_ONERR_STOP( mi ) ) {
390 LDAP_BACK_CONN_TAINTED_SET( mc );
391 meta_back_release_conn_lock( mi, mc, 0 );
392 *mcp = NULL;
393 retcode = META_SEARCH_ERR;
394 rs->sr_err = rc;
395 }
396
397 } else {
398 /* FIXME: check if bound as idassert authcDN! */
399 if ( BER_BVISNULL( &msc->msc_bound_ndn )
400 || BER_BVISEMPTY( &msc->msc_bound_ndn ) )
401 {
402 LDAP_BACK_CONN_ISANON_SET( msc );
403
404 } else {
405 if ( META_BACK_TGT_SAVECRED( mt ) &&
406 !BER_BVISNULL( &msc->msc_cred ) &&
407 !BER_BVISEMPTY( &msc->msc_cred ) )
408 {
409 ldap_set_rebind_proc( msc->msc_ld, mt->mt_rebind_f, msc );
410 }
411 LDAP_BACK_CONN_ISBOUND_SET( msc );
412 }
413 retcode = META_SEARCH_CANDIDATE;
414
415 /* connect must be async */
416 ldap_set_option( msc->msc_ld, LDAP_OPT_CONNECT_ASYNC, LDAP_OPT_OFF );
417 }
418
419 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
420 META_BINDING_CLEAR( &candidates[ candidate ] );
421
422 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
423
424 return retcode;
425 }
426
427 static meta_search_candidate_t
428 meta_back_search_start(
429 Operation *op,
430 SlapReply *rs,
431 dncookie *dc,
432 metaconn_t **mcp,
433 int candidate,
434 SlapReply *candidates,
435 struct berval *prcookie,
436 ber_int_t prsize )
437 {
438 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
439 metatarget_t *mt = mi->mi_targets[ candidate ];
440 metasingleconn_t *msc = &(*mcp)->mc_conns[ candidate ];
441 struct berval realbase = op->o_req_dn;
442 int realscope = op->ors_scope;
443 struct berval mbase = BER_BVNULL;
444 struct berval mfilter = BER_BVNULL;
445 char **mapped_attrs = NULL;
446 int rc;
447 meta_search_candidate_t retcode;
448 struct timeval tv, *tvp = NULL;
449 int nretries = 1;
450 LDAPControl **ctrls = NULL;
451 #ifdef SLAPD_META_CLIENT_PR
452 LDAPControl **save_ctrls = NULL;
453 #endif /* SLAPD_META_CLIENT_PR */
454
455 /* this should not happen; just in case... */
456 if ( msc->msc_ld == NULL ) {
457 Debug( LDAP_DEBUG_ANY,
458 "%s: meta_back_search_start candidate=%d ld=NULL%s.\n",
459 op->o_log_prefix, candidate,
460 META_BACK_ONERR_STOP( mi ) ? "" : " (ignored)" );
461 candidates[ candidate ].sr_err = LDAP_OTHER;
462 if ( META_BACK_ONERR_STOP( mi ) ) {
463 return META_SEARCH_ERR;
464 }
465 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
466 return META_SEARCH_NOT_CANDIDATE;
467 }
468
469 Debug( LDAP_DEBUG_TRACE, "%s >>> meta_back_search_start[%d]\n", op->o_log_prefix, candidate );
470
471 /*
472 * modifies the base according to the scope, if required
473 */
474 if ( mt->mt_nsuffix.bv_len > op->o_req_ndn.bv_len ) {
475 switch ( op->ors_scope ) {
476 case LDAP_SCOPE_SUBTREE:
477 /*
478 * make the target suffix the new base
479 * FIXME: this is very forgiving, because
480 * "illegal" searchBases may be turned
481 * into the suffix of the target; however,
482 * the requested searchBase already passed
483 * thru the candidate analyzer...
484 */
485 if ( dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) ) {
486 realbase = mt->mt_nsuffix;
487 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
488 realscope = LDAP_SCOPE_SUBORDINATE;
489 }
490
491 } else {
492 /*
493 * this target is no longer candidate
494 */
495 retcode = META_SEARCH_NOT_CANDIDATE;
496 goto doreturn;
497 }
498 break;
499
500 case LDAP_SCOPE_SUBORDINATE:
501 case LDAP_SCOPE_ONELEVEL:
502 {
503 struct berval rdn = mt->mt_nsuffix;
504 rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
505 if ( dnIsOneLevelRDN( &rdn )
506 && dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) )
507 {
508 /*
509 * if there is exactly one level,
510 * make the target suffix the new
511 * base, and make scope "base"
512 */
513 realbase = mt->mt_nsuffix;
514 if ( op->ors_scope == LDAP_SCOPE_SUBORDINATE ) {
515 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
516 realscope = LDAP_SCOPE_SUBORDINATE;
517 } else {
518 realscope = LDAP_SCOPE_SUBTREE;
519 }
520 } else {
521 realscope = LDAP_SCOPE_BASE;
522 }
523 break;
524 } /* else continue with the next case */
525 }
526
527 case LDAP_SCOPE_BASE:
528 /*
529 * this target is no longer candidate
530 */
531 retcode = META_SEARCH_NOT_CANDIDATE;
532 goto doreturn;
533 }
534 }
535
536 /* check filter expression */
537 if ( mt->mt_filter ) {
538 metafilter_t *mf;
539 for ( mf = mt->mt_filter; mf; mf = mf->mf_next ) {
540 if ( regexec( &mf->mf_regex, op->ors_filterstr.bv_val, 0, NULL, 0 ) == 0 )
541 break;
542 }
543 /* nothing matched, this target is no longer a candidate */
544 if ( !mf ) {
545 retcode = META_SEARCH_NOT_CANDIDATE;
546 goto doreturn;
547 }
548 }
549
550 /* initiate dobind */
551 retcode = meta_search_dobind_init( op, rs, mcp, candidate, candidates );
552
553 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%d]=%d\n", op->o_log_prefix, candidate, retcode );
554
555 if ( retcode != META_SEARCH_CANDIDATE ) {
556 goto doreturn;
557 }
558
559 /*
560 * Rewrite the search base, if required
561 */
562 dc->target = mt;
563 dc->ctx = "searchBase";
564 switch ( ldap_back_dn_massage( dc, &realbase, &mbase ) ) {
565 case LDAP_SUCCESS:
566 break;
567
568 case LDAP_UNWILLING_TO_PERFORM:
569 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
570 rs->sr_text = "Operation not allowed";
571 send_ldap_result( op, rs );
572 retcode = META_SEARCH_ERR;
573 goto doreturn;
574
575 default:
576
577 /*
578 * this target is no longer candidate
579 */
580 retcode = META_SEARCH_NOT_CANDIDATE;
581 goto doreturn;
582 }
583
584 /*
585 * Maps filter
586 */
587 rc = ldap_back_filter_map_rewrite( dc, op->ors_filter,
588 &mfilter, BACKLDAP_MAP, op->o_tmpmemctx );
589 switch ( rc ) {
590 case LDAP_SUCCESS:
591 break;
592
593 case LDAP_COMPARE_FALSE:
594 default:
595 /*
596 * this target is no longer candidate
597 */
598 retcode = META_SEARCH_NOT_CANDIDATE;
599 goto done;
600 }
601
602 /*
603 * Maps required attributes
604 */
605 rc = ldap_back_map_attrs( op, &mt->mt_rwmap.rwm_at,
606 op->ors_attrs, BACKLDAP_MAP, &mapped_attrs );
607 if ( rc != LDAP_SUCCESS ) {
608 /*
609 * this target is no longer candidate
610 */
611 retcode = META_SEARCH_NOT_CANDIDATE;
612 goto done;
613 }
614
615 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
616 tv.tv_sec = op->ors_tlimit > 0 ? op->ors_tlimit : 1;
617 tv.tv_usec = 0;
618 tvp = &tv;
619 }
620
621 #ifdef SLAPD_META_CLIENT_PR
622 save_ctrls = op->o_ctrls;
623 {
624 LDAPControl *pr_c = NULL;
625 int i = 0, nc = 0;
626
627 if ( save_ctrls ) {
628 for ( ; save_ctrls[i] != NULL; i++ );
629 nc = i;
630 pr_c = ldap_control_find( LDAP_CONTROL_PAGEDRESULTS, save_ctrls, NULL );
631 }
632
633 if ( pr_c != NULL ) nc--;
634 if ( mt->mt_ps > 0 || prcookie != NULL ) nc++;
635
636 if ( mt->mt_ps > 0 || prcookie != NULL || pr_c != NULL ) {
637 int src = 0, dst = 0;
638 BerElementBuffer berbuf;
639 BerElement *ber = (BerElement *)&berbuf;
640 struct berval val = BER_BVNULL;
641 ber_len_t len;
642
643 len = sizeof( LDAPControl * )*( nc + 1 ) + sizeof( LDAPControl );
644
645 if ( mt->mt_ps > 0 || prcookie != NULL ) {
646 struct berval nullcookie = BER_BVNULL;
647 ber_tag_t tag;
648
649 if ( prsize == 0 && mt->mt_ps > 0 ) prsize = mt->mt_ps;
650 if ( prcookie == NULL ) prcookie = &nullcookie;
651
652 ber_init2( ber, NULL, LBER_USE_DER );
653 tag = ber_printf( ber, "{iO}", prsize, prcookie );
654 if ( tag == LBER_ERROR ) {
655 /* error */
656 (void) ber_free_buf( ber );
657 goto done_pr;
658 }
659
660 tag = ber_flatten2( ber, &val, 0 );
661 if ( tag == LBER_ERROR ) {
662 /* error */
663 (void) ber_free_buf( ber );
664 goto done_pr;
665 }
666
667 len += val.bv_len + 1;
668 }
669
670 op->o_ctrls = op->o_tmpalloc( len, op->o_tmpmemctx );
671 if ( save_ctrls ) {
672 for ( ; save_ctrls[ src ] != NULL; src++ ) {
673 if ( save_ctrls[ src ] != pr_c ) {
674 op->o_ctrls[ dst ] = save_ctrls[ src ];
675 dst++;
676 }
677 }
678 }
679
680 if ( mt->mt_ps > 0 || prcookie != NULL ) {
681 op->o_ctrls[ dst ] = (LDAPControl *)&op->o_ctrls[ nc + 1 ];
682
683 op->o_ctrls[ dst ]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
684 op->o_ctrls[ dst ]->ldctl_iscritical = 1;
685
686 op->o_ctrls[ dst ]->ldctl_value.bv_val = (char *)&op->o_ctrls[ dst ][ 1 ];
687 AC_MEMCPY( op->o_ctrls[ dst ]->ldctl_value.bv_val, val.bv_val, val.bv_len + 1 );
688 op->o_ctrls[ dst ]->ldctl_value.bv_len = val.bv_len;
689 dst++;
690
691 (void)ber_free_buf( ber );
692 }
693
694 op->o_ctrls[ dst ] = NULL;
695 }
696 done_pr:;
697 }
698 #endif /* SLAPD_META_CLIENT_PR */
699
700 retry:;
701 ctrls = op->o_ctrls;
702 if ( meta_back_controls_add( op, rs, *mcp, candidate, &ctrls )
703 != LDAP_SUCCESS )
704 {
705 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
706 retcode = META_SEARCH_NOT_CANDIDATE;
707 goto done;
708 }
709
710 /*
711 * Starts the search
712 */
713 assert( msc->msc_ld != NULL );
714 rc = ldap_pvt_search( msc->msc_ld,
715 mbase.bv_val, realscope, mfilter.bv_val,
716 mapped_attrs, op->ors_attrsonly,
717 ctrls, NULL, tvp, op->ors_slimit, op->ors_deref,
718 &candidates[ candidate ].sr_msgid );
719 switch ( rc ) {
720 case LDAP_SUCCESS:
721 retcode = META_SEARCH_CANDIDATE;
722 break;
723
724 case LDAP_SERVER_DOWN:
725 if ( nretries && meta_back_retry( op, rs, mcp, candidate, LDAP_BACK_DONTSEND, candidates ) ) {
726 nretries = 0;
727 /* if the identity changed, there might be need to re-authz */
728 (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
729 goto retry;
730 }
731
732 if ( *mcp == NULL ) {
733 retcode = META_SEARCH_ERR;
734 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
735 break;
736 }
737 /* fall thru */
738
739 default:
740 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
741 retcode = META_SEARCH_NOT_CANDIDATE;
742 }
743
744 done:;
745 (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
746 #ifdef SLAPD_META_CLIENT_PR
747 if ( save_ctrls != op->o_ctrls ) {
748 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
749 op->o_ctrls = save_ctrls;
750 }
751 #endif /* SLAPD_META_CLIENT_PR */
752
753 if ( mapped_attrs ) {
754 ber_memfree_x( mapped_attrs, op->o_tmpmemctx );
755 }
756 if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
757 ber_memfree_x( mfilter.bv_val, op->o_tmpmemctx );
758 }
759 if ( mbase.bv_val != realbase.bv_val ) {
760 free( mbase.bv_val );
761 }
762
763 doreturn:;
764 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_back_search_start[%d]=%d\n", op->o_log_prefix, candidate, retcode );
765
766 return retcode;
767 }
768
769 int
770 meta_back_search( Operation *op, SlapReply *rs )
771 {
772 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
773 metaconn_t *mc;
774 struct timeval save_tv = { 0, 0 },
775 tv;
776 time_t stoptime = (time_t)(-1),
777 lastres_time = slap_get_time(),
778 timeout = 0;
779 int rc = 0, sres = LDAP_SUCCESS;
780 char *matched = NULL;
781 int last = 0, ncandidates = 0,
782 initial_candidates = 0, candidate_match = 0,
783 needbind = 0;
784 ldap_back_send_t sendok = LDAP_BACK_SENDERR;
785 long i;
786 dncookie dc;
787 int is_ok = 0;
788 void *savepriv;
789 SlapReply *candidates = NULL;
790 int do_taint = 0;
791
792 rs_assert_ready( rs );
793 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia, we can set rs = non-entry */
794
795 /*
796 * controls are set in ldap_back_dobind()
797 *
798 * FIXME: in case of values return filter, we might want
799 * to map attrs and maybe rewrite value
800 */
801 candidates = meta_back_candidates_get( op );
802 getconn:;
803 mc = meta_back_getconn( op, rs, NULL, sendok, candidates );
804 if ( !mc ) {
805 return rs->sr_err;
806 }
807
808 dc.conn = op->o_conn;
809 dc.rs = rs;
810
811 /*
812 * Inits searches
813 */
814 for ( i = 0; i < mi->mi_ntargets; i++ ) {
815 /* reset sr_msgid; it is used in most loops
816 * to check if that target is still to be considered */
817 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
818
819 /* a target is marked as candidate by meta_back_getconn();
820 * if for any reason (an error, it's over or so) it is
821 * no longer active, sr_msgid is set to META_MSGID_IGNORE
822 * but it remains candidate, which means it has been active
823 * at some point during the operation. This allows to
824 * use its response code and more to compute the final
825 * response */
826 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
827 continue;
828 }
829
830 candidates[ i ].sr_matched = NULL;
831 candidates[ i ].sr_text = NULL;
832 candidates[ i ].sr_ref = NULL;
833 candidates[ i ].sr_ctrls = NULL;
834 candidates[ i ].sr_nentries = 0;
835
836 /* get largest timeout among candidates */
837 if ( mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ]
838 && mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ] > timeout )
839 {
840 timeout = mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ];
841 }
842 }
843
844 for ( i = 0; i < mi->mi_ntargets; i++ ) {
845 if ( !META_IS_CANDIDATE( &candidates[ i ] )
846 || candidates[ i ].sr_err != LDAP_SUCCESS )
847 {
848 continue;
849 }
850
851 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates, NULL, 0 ) )
852 {
853 case META_SEARCH_NOT_CANDIDATE:
854 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
855 break;
856
857 case META_SEARCH_NEED_BIND:
858 ++needbind;
859 /* fallthru */
860
861 case META_SEARCH_CONNECTING:
862 case META_SEARCH_CANDIDATE:
863 case META_SEARCH_BINDING:
864 candidates[ i ].sr_type = REP_INTERMEDIATE;
865 ++ncandidates;
866 break;
867
868 case META_SEARCH_ERR:
869 savepriv = op->o_private;
870 op->o_private = (void *)i;
871 send_ldap_result( op, rs );
872 op->o_private = savepriv;
873 rc = -1;
874 goto finish;
875
876 default:
877 assert( 0 );
878 break;
879 }
880 }
881
882 if ( ncandidates > 0 && needbind == ncandidates ) {
883 /*
884 * give up the second time...
885 *
886 * NOTE: this should not occur the second time, since a fresh
887 * connection has ben created; however, targets may also
888 * need bind because the bind timed out or so.
889 */
890 if ( sendok & LDAP_BACK_BINDING ) {
891 Debug( LDAP_DEBUG_ANY,
892 "%s meta_back_search: unable to initialize conn\n",
893 op->o_log_prefix );
894 rs->sr_err = LDAP_UNAVAILABLE;
895 rs->sr_text = "unable to initialize connection to remote targets";
896 send_ldap_result( op, rs );
897 rc = -1;
898 goto finish;
899 }
900
901 /* FIXME: better create a separate connection? */
902 sendok |= LDAP_BACK_BINDING;
903
904 #ifdef DEBUG_205
905 Debug( LDAP_DEBUG_ANY, "*** %s drop mc=%p create new connection\n",
906 op->o_log_prefix, (void *)mc );
907 #endif /* DEBUG_205 */
908
909 meta_back_release_conn( mi, mc );
910 mc = NULL;
911
912 needbind = 0;
913 ncandidates = 0;
914
915 goto getconn;
916 }
917
918 initial_candidates = ncandidates;
919
920 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
921 char cnd[ SLAP_TEXT_BUFLEN ];
922 int c;
923
924 for ( c = 0; c < mi->mi_ntargets; c++ ) {
925 if ( META_IS_CANDIDATE( &candidates[ c ] ) ) {
926 cnd[ c ] = '*';
927 } else {
928 cnd[ c ] = ' ';
929 }
930 }
931 cnd[ c ] = '\0';
932
933 Debug( LDAP_DEBUG_TRACE, "%s meta_back_search: ncandidates=%d "
934 "cnd=\"%s\"\n", op->o_log_prefix, ncandidates, cnd );
935 }
936
937 if ( initial_candidates == 0 ) {
938 /* NOTE: here we are not sending any matchedDN;
939 * this is intended, because if the back-meta
940 * is serving this search request, but no valid
941 * candidate could be looked up, it means that
942 * there is a hole in the mapping of the targets
943 * and thus no knowledge of any remote superior
944 * is available */
945 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: "
946 "base=\"%s\" scope=%d: "
947 "no candidate could be selected\n",
948 op->o_log_prefix, op->o_req_dn.bv_val,
949 op->ors_scope );
950
951 /* FIXME: we're sending the first error we encounter;
952 * maybe we should pick the worst... */
953 rc = LDAP_NO_SUCH_OBJECT;
954 for ( i = 0; i < mi->mi_ntargets; i++ ) {
955 if ( META_IS_CANDIDATE( &candidates[ i ] )
956 && candidates[ i ].sr_err != LDAP_SUCCESS )
957 {
958 rc = candidates[ i ].sr_err;
959 break;
960 }
961 }
962
963 send_ldap_error( op, rs, rc, NULL );
964
965 goto finish;
966 }
967
968 /* We pull apart the ber result, stuff it into a slapd entry, and
969 * let send_search_entry stuff it back into ber format. Slow & ugly,
970 * but this is necessary for version matching, and for ACL processing.
971 */
972
973 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
974 stoptime = op->o_time + op->ors_tlimit;
975 }
976
977 /*
978 * In case there are no candidates, no cycle takes place...
979 *
980 * FIXME: we might use a queue, to better balance the load
981 * among the candidates
982 */
983 for ( rc = 0; ncandidates > 0; ) {
984 int gotit = 0,
985 doabandon = 0,
986 alreadybound = ncandidates;
987
988 /* check timeout */
989 if ( timeout && lastres_time > 0
990 && ( slap_get_time() - lastres_time ) > timeout )
991 {
992 doabandon = 1;
993 rs->sr_text = "Operation timed out";
994 rc = rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
995 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
996 savepriv = op->o_private;
997 op->o_private = (void *)i;
998 send_ldap_result( op, rs );
999 op->o_private = savepriv;
1000 goto finish;
1001 }
1002
1003 /* check time limit */
1004 if ( op->ors_tlimit != SLAP_NO_LIMIT
1005 && slap_get_time() > stoptime )
1006 {
1007 doabandon = 1;
1008 rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
1009 savepriv = op->o_private;
1010 op->o_private = (void *)i;
1011 send_ldap_result( op, rs );
1012 op->o_private = savepriv;
1013 goto finish;
1014 }
1015
1016 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1017 meta_search_candidate_t retcode = META_SEARCH_UNDEFINED;
1018 metasingleconn_t *msc = &mc->mc_conns[ i ];
1019 LDAPMessage *res = NULL, *msg;
1020
1021 /* if msgid is invalid, don't ldap_result() */
1022 if ( candidates[ i ].sr_msgid == META_MSGID_IGNORE ) {
1023 continue;
1024 }
1025
1026 /* if target still needs bind, retry */
1027 if ( candidates[ i ].sr_msgid == META_MSGID_NEED_BIND
1028 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1029 {
1030 /* initiate dobind */
1031 retcode = meta_search_dobind_init( op, rs, &mc, i, candidates );
1032
1033 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%ld]=%d\n",
1034 op->o_log_prefix, i, retcode );
1035
1036 switch ( retcode ) {
1037 case META_SEARCH_NEED_BIND:
1038 alreadybound--;
1039 /* fallthru */
1040
1041 case META_SEARCH_CONNECTING:
1042 case META_SEARCH_BINDING:
1043 break;
1044
1045 case META_SEARCH_ERR:
1046 candidates[ i ].sr_err = rs->sr_err;
1047 if ( META_BACK_ONERR_STOP( mi ) ) {
1048 savepriv = op->o_private;
1049 op->o_private = (void *)i;
1050 send_ldap_result( op, rs );
1051 op->o_private = savepriv;
1052 goto finish;
1053 }
1054 /* fallthru */
1055
1056 case META_SEARCH_NOT_CANDIDATE:
1057 /*
1058 * When no candidates are left,
1059 * the outer cycle finishes
1060 */
1061 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1062 assert( ncandidates > 0 );
1063 --ncandidates;
1064 break;
1065
1066 case META_SEARCH_CANDIDATE:
1067 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1068 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates, NULL, 0 ) )
1069 {
1070 case META_SEARCH_CANDIDATE:
1071 assert( candidates[ i ].sr_msgid >= 0 );
1072 break;
1073
1074 case META_SEARCH_ERR:
1075 candidates[ i ].sr_err = rs->sr_err;
1076 if ( META_BACK_ONERR_STOP( mi ) ) {
1077 savepriv = op->o_private;
1078 op->o_private = (void *)i;
1079 send_ldap_result( op, rs );
1080 op->o_private = savepriv;
1081 goto finish;
1082 }
1083 /* fallthru */
1084
1085 case META_SEARCH_NOT_CANDIDATE:
1086 /* means that meta_back_search_start()
1087 * failed but onerr == continue */
1088 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1089 assert( ncandidates > 0 );
1090 --ncandidates;
1091 break;
1092
1093 default:
1094 /* impossible */
1095 assert( 0 );
1096 break;
1097 }
1098 break;
1099
1100 default:
1101 /* impossible */
1102 assert( 0 );
1103 break;
1104 }
1105 continue;
1106 }
1107
1108 /* check for abandon */
1109 if ( op->o_abandon || LDAP_BACK_CONN_ABANDON( mc ) ) {
1110 break;
1111 }
1112
1113 #ifdef DEBUG_205
1114 if ( msc->msc_ld == NULL ) {
1115 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1116 Debug(LDAP_DEBUG_ANY,
1117 "!!! %s meta_back_search[%ld] mc=%p msgid=%d%s%s%s\n\n",
1118 op->o_log_prefix, (long)i, (void *)mc,
1119 candidates[i].sr_msgid,
1120 META_IS_BINDING(&candidates[i]) ? " binding" : "",
1121 LDAP_BACK_CONN_BINDING(&mc->mc_conns[i]) ? " connbinding" : "",
1122 META_BACK_CONN_CREATING(&mc->mc_conns[i]) ? " conncreating" : "" );
1123 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1124 }
1125 #endif /* DEBUG_205 */
1126
1127 /*
1128 * FIXME: handle time limit as well?
1129 * Note that target servers are likely
1130 * to handle it, so at some time we'll
1131 * get a LDAP_TIMELIMIT_EXCEEDED from
1132 * one of them ...
1133 */
1134 tv = save_tv;
1135 rc = ldap_result( msc->msc_ld, candidates[ i ].sr_msgid,
1136 LDAP_MSG_RECEIVED, &tv, &res );
1137 switch ( rc ) {
1138 case 0:
1139 /* FIXME: res should not need to be freed */
1140 assert( res == NULL );
1141 continue;
1142
1143 case -1:
1144 really_bad:;
1145 /* something REALLY bad happened! */
1146 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1147 candidates[ i ].sr_type = REP_RESULT;
1148
1149 if ( meta_back_retry( op, rs, &mc, i, LDAP_BACK_DONTSEND, candidates ) ) {
1150 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1151 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates, NULL, 0 ) )
1152 {
1153 /* means that failed but onerr == continue */
1154 case META_SEARCH_NOT_CANDIDATE:
1155 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1156
1157 assert( ncandidates > 0 );
1158 --ncandidates;
1159
1160 candidates[ i ].sr_err = rs->sr_err;
1161 if ( META_BACK_ONERR_STOP( mi ) ) {
1162 savepriv = op->o_private;
1163 op->o_private = (void *)i;
1164 send_ldap_result( op, rs );
1165 op->o_private = savepriv;
1166 goto finish;
1167 }
1168 /* fall thru */
1169
1170 case META_SEARCH_CANDIDATE:
1171 /* get back into business... */
1172 continue;
1173
1174 case META_SEARCH_BINDING:
1175 case META_SEARCH_CONNECTING:
1176 case META_SEARCH_NEED_BIND:
1177 case META_SEARCH_UNDEFINED:
1178 assert( 0 );
1179
1180 default:
1181 /* unrecoverable error */
1182 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1183 rc = rs->sr_err = LDAP_OTHER;
1184 goto finish;
1185 }
1186 }
1187
1188 candidates[ i ].sr_err = rs->sr_err;
1189 if ( META_BACK_ONERR_STOP( mi ) ) {
1190 savepriv = op->o_private;
1191 op->o_private = (void *)i;
1192 send_ldap_result( op, rs );
1193 op->o_private = savepriv;
1194 goto finish;
1195 }
1196 }
1197
1198 /*
1199 * When no candidates are left,
1200 * the outer cycle finishes
1201 */
1202 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1203 assert( ncandidates > 0 );
1204 --ncandidates;
1205 rs->sr_err = candidates[ i ].sr_err;
1206 continue;
1207
1208 default:
1209 lastres_time = slap_get_time();
1210
1211 /* only touch when activity actually took place... */
1212 if ( mi->mi_idle_timeout != 0 && msc->msc_time < lastres_time ) {
1213 msc->msc_time = lastres_time;
1214 }
1215 break;
1216 }
1217
1218 for ( msg = ldap_first_message( msc->msc_ld, res );
1219 msg != NULL;
1220 msg = ldap_next_message( msc->msc_ld, msg ) )
1221 {
1222 rc = ldap_msgtype( msg );
1223 if ( rc == LDAP_RES_SEARCH_ENTRY ) {
1224 LDAPMessage *e;
1225
1226 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1227 /* don't retry any more... */
1228 candidates[ i ].sr_type = REP_RESULT;
1229 }
1230
1231 /* count entries returned by target */
1232 candidates[ i ].sr_nentries++;
1233
1234 is_ok++;
1235
1236 e = ldap_first_entry( msc->msc_ld, msg );
1237 savepriv = op->o_private;
1238 op->o_private = (void *)i;
1239 rs->sr_err = meta_send_entry( op, rs, mc, i, e );
1240
1241 switch ( rs->sr_err ) {
1242 case LDAP_SIZELIMIT_EXCEEDED:
1243 savepriv = op->o_private;
1244 op->o_private = (void *)i;
1245 send_ldap_result( op, rs );
1246 op->o_private = savepriv;
1247 rs->sr_err = LDAP_SUCCESS;
1248 ldap_msgfree( res );
1249 res = NULL;
1250 goto finish;
1251
1252 case LDAP_UNAVAILABLE:
1253 rs->sr_err = LDAP_OTHER;
1254 ldap_msgfree( res );
1255 res = NULL;
1256 goto finish;
1257 }
1258 op->o_private = savepriv;
1259
1260 /* don't wait any longer... */
1261 gotit = 1;
1262 save_tv.tv_sec = 0;
1263 save_tv.tv_usec = 0;
1264
1265 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
1266 char **references = NULL;
1267 int cnt;
1268
1269 if ( META_BACK_TGT_NOREFS( mi->mi_targets[ i ] ) ) {
1270 continue;
1271 }
1272
1273 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1274 /* don't retry any more... */
1275 candidates[ i ].sr_type = REP_RESULT;
1276 }
1277
1278 is_ok++;
1279
1280 rc = ldap_parse_reference( msc->msc_ld, msg,
1281 &references, &rs->sr_ctrls, 0 );
1282
1283 if ( rc != LDAP_SUCCESS ) {
1284 continue;
1285 }
1286
1287 if ( references == NULL ) {
1288 continue;
1289 }
1290
1291 dc.ctx = "referralDN";
1292
1293 /* FIXME: merge all and return at the end */
1294
1295 for ( cnt = 0; references[ cnt ]; cnt++ )
1296 ;
1297
1298 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( cnt + 1 ),
1299 op->o_tmpmemctx );
1300
1301 for ( cnt = 0; references[ cnt ]; cnt++ ) {
1302 ber_str2bv_x( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ],
1303 op->o_tmpmemctx );
1304 }
1305 BER_BVZERO( &rs->sr_ref[ cnt ] );
1306
1307 ( void )ldap_back_referral_result_rewrite( &dc, rs->sr_ref,
1308 op->o_tmpmemctx );
1309
1310 if ( rs->sr_ref != NULL && !BER_BVISNULL( &rs->sr_ref[ 0 ] ) ) {
1311 /* ignore return value by now */
1312 savepriv = op->o_private;
1313 op->o_private = (void *)i;
1314 ( void )send_search_reference( op, rs );
1315 op->o_private = savepriv;
1316
1317 ber_bvarray_free_x( rs->sr_ref, op->o_tmpmemctx );
1318 rs->sr_ref = NULL;
1319 }
1320
1321 /* cleanup */
1322 if ( references ) {
1323 ber_memvfree( (void **)references );
1324 }
1325
1326 if ( rs->sr_ctrls ) {
1327 ldap_controls_free( rs->sr_ctrls );
1328 rs->sr_ctrls = NULL;
1329 }
1330
1331 } else if ( rc == LDAP_RES_INTERMEDIATE ) {
1332 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1333 /* don't retry any more... */
1334 candidates[ i ].sr_type = REP_RESULT;
1335 }
1336
1337 /* FIXME: response controls
1338 * are passed without checks */
1339 rs->sr_err = ldap_parse_intermediate( msc->msc_ld,
1340 msg,
1341 (char **)&rs->sr_rspoid,
1342 &rs->sr_rspdata,
1343 &rs->sr_ctrls,
1344 0 );
1345 if ( rs->sr_err != LDAP_SUCCESS ) {
1346 candidates[ i ].sr_type = REP_RESULT;
1347 ldap_msgfree( res );
1348 res = NULL;
1349 goto really_bad;
1350 }
1351
1352 slap_send_ldap_intermediate( op, rs );
1353
1354 if ( rs->sr_rspoid != NULL ) {
1355 ber_memfree( (char *)rs->sr_rspoid );
1356 rs->sr_rspoid = NULL;
1357 }
1358
1359 if ( rs->sr_rspdata != NULL ) {
1360 ber_bvfree( rs->sr_rspdata );
1361 rs->sr_rspdata = NULL;
1362 }
1363
1364 if ( rs->sr_ctrls != NULL ) {
1365 ldap_controls_free( rs->sr_ctrls );
1366 rs->sr_ctrls = NULL;
1367 }
1368
1369 } else if ( rc == LDAP_RES_SEARCH_RESULT ) {
1370 char **references = NULL;
1371 LDAPControl **ctrls = NULL;
1372
1373 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1374 /* don't retry any more... */
1375 candidates[ i ].sr_type = REP_RESULT;
1376 }
1377
1378 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1379
1380 /* NOTE: ignores response controls
1381 * (and intermediate response controls
1382 * as well, except for those with search
1383 * references); this may not be correct,
1384 * but if they're not ignored then
1385 * back-meta would need to merge them
1386 * consistently (think of pagedResults...)
1387 */
1388 /* FIXME: response controls? */
1389 rs->sr_err = ldap_parse_result( msc->msc_ld,
1390 msg,
1391 &candidates[ i ].sr_err,
1392 (char **)&candidates[ i ].sr_matched,
1393 (char **)&candidates[ i ].sr_text,
1394 &references,
1395 &ctrls /* &candidates[ i ].sr_ctrls (unused) */ ,
1396 0 );
1397 if ( rs->sr_err != LDAP_SUCCESS ) {
1398 candidates[ i ].sr_err = rs->sr_err;
1399 sres = slap_map_api2result( &candidates[ i ] );
1400 candidates[ i ].sr_type = REP_RESULT;
1401 ldap_msgfree( res );
1402 res = NULL;
1403 goto really_bad;
1404 }
1405
1406 rs->sr_err = candidates[ i ].sr_err;
1407
1408 /* massage matchedDN if need be */
1409 if ( candidates[ i ].sr_matched != NULL ) {
1410 struct berval match, mmatch;
1411
1412 ber_str2bv( candidates[ i ].sr_matched,
1413 0, 0, &match );
1414 candidates[ i ].sr_matched = NULL;
1415
1416 dc.ctx = "matchedDN";
1417 dc.target = mi->mi_targets[ i ];
1418 if ( !ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
1419 if ( mmatch.bv_val == match.bv_val ) {
1420 candidates[ i ].sr_matched
1421 = ch_strdup( mmatch.bv_val );
1422
1423 } else {
1424 candidates[ i ].sr_matched = mmatch.bv_val;
1425 }
1426
1427 candidate_match++;
1428 }
1429 ldap_memfree( match.bv_val );
1430 }
1431
1432 /* add references to array */
1433 /* RFC 4511: referrals can only appear
1434 * if result code is LDAP_REFERRAL */
1435 if ( references != NULL
1436 && references[ 0 ] != NULL
1437 && references[ 0 ][ 0 ] != '\0' )
1438 {
1439 if ( rs->sr_err != LDAP_REFERRAL ) {
1440 Debug( LDAP_DEBUG_ANY,
1441 "%s meta_back_search[%ld]: "
1442 "got referrals with err=%d\n",
1443 op->o_log_prefix,
1444 i, rs->sr_err );
1445
1446 } else {
1447 BerVarray sr_ref;
1448 int cnt;
1449
1450 for ( cnt = 0; references[ cnt ]; cnt++ )
1451 ;
1452
1453 sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( cnt + 1 ),
1454 op->o_tmpmemctx );
1455
1456 for ( cnt = 0; references[ cnt ]; cnt++ ) {
1457 ber_str2bv_x( references[ cnt ], 0, 1, &sr_ref[ cnt ],
1458 op->o_tmpmemctx );
1459 }
1460 BER_BVZERO( &sr_ref[ cnt ] );
1461
1462 ( void )ldap_back_referral_result_rewrite( &dc, sr_ref,
1463 op->o_tmpmemctx );
1464
1465 if ( rs->sr_v2ref == NULL ) {
1466 rs->sr_v2ref = sr_ref;
1467
1468 } else {
1469 for ( cnt = 0; !BER_BVISNULL( &sr_ref[ cnt ] ); cnt++ ) {
1470 ber_bvarray_add_x( &rs->sr_v2ref, &sr_ref[ cnt ],
1471 op->o_tmpmemctx );
1472 }
1473 ber_memfree_x( sr_ref, op->o_tmpmemctx );
1474 }
1475 }
1476
1477 } else if ( rs->sr_err == LDAP_REFERRAL ) {
1478 Debug( LDAP_DEBUG_ANY,
1479 "%s meta_back_search[%ld]: "
1480 "got err=%d with null "
1481 "or empty referrals\n",
1482 op->o_log_prefix,
1483 i, rs->sr_err );
1484
1485 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1486 }
1487
1488 /* cleanup */
1489 ber_memvfree( (void **)references );
1490
1491 sres = slap_map_api2result( rs );
1492
1493 if ( LogTest( LDAP_DEBUG_TRACE | LDAP_DEBUG_ANY ) ) {
1494 char buf[ SLAP_TEXT_BUFLEN ];
1495 snprintf( buf, sizeof( buf ),
1496 "%s meta_back_search[%ld] "
1497 "match=\"%s\" err=%ld",
1498 op->o_log_prefix, i,
1499 candidates[ i ].sr_matched ? candidates[ i ].sr_matched : "",
1500 (long) candidates[ i ].sr_err );
1501 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
1502 Debug( LDAP_DEBUG_TRACE, "%s.\n", buf );
1503
1504 } else {
1505 Debug( LDAP_DEBUG_ANY, "%s (%s) text=\"%s\".\n",
1506 buf, ldap_err2string( candidates[ i ].sr_err ),
1507 candidates[ i ].sr_text ? candidates[i].sr_text : "" );
1508 }
1509 }
1510
1511 switch ( sres ) {
1512 case LDAP_NO_SUCH_OBJECT:
1513 /* is_ok is touched any time a valid
1514 * (even intermediate) result is
1515 * returned; as a consequence, if
1516 * a candidate returns noSuchObject
1517 * it is ignored and the candidate
1518 * is simply demoted. */
1519 if ( is_ok ) {
1520 sres = LDAP_SUCCESS;
1521 }
1522 break;
1523
1524 case LDAP_SUCCESS:
1525 if ( ctrls != NULL && ctrls[0] != NULL ) {
1526 #ifdef SLAPD_META_CLIENT_PR
1527 LDAPControl *pr_c;
1528
1529 pr_c = ldap_control_find( LDAP_CONTROL_PAGEDRESULTS, ctrls, NULL );
1530 if ( pr_c != NULL ) {
1531 BerElementBuffer berbuf;
1532 BerElement *ber = (BerElement *)&berbuf;
1533 ber_tag_t tag;
1534 ber_int_t prsize;
1535 struct berval prcookie;
1536
1537 /* unsolicited, do not accept */
1538 if ( mi->mi_targets[i]->mt_ps == 0 ) {
1539 rs->sr_err = LDAP_OTHER;
1540 goto err_pr;
1541 }
1542
1543 ber_init2( ber, &pr_c->ldctl_value, LBER_USE_DER );
1544
1545 tag = ber_scanf( ber, "{im}", &prsize, &prcookie );
1546 if ( tag == LBER_ERROR ) {
1547 rs->sr_err = LDAP_OTHER;
1548 goto err_pr;
1549 }
1550
1551 /* more pages? new search request */
1552 if ( !BER_BVISNULL( &prcookie ) && !BER_BVISEMPTY( &prcookie ) ) {
1553 if ( mi->mi_targets[i]->mt_ps > 0 ) {
1554 /* ignore size if specified */
1555 prsize = 0;
1556
1557 } else if ( prsize == 0 ) {
1558 /* guess the page size from the entries returned so far */
1559 prsize = candidates[ i ].sr_nentries;
1560 }
1561
1562 candidates[ i ].sr_nentries = 0;
1563 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1564 candidates[ i ].sr_type = REP_INTERMEDIATE;
1565
1566 assert( candidates[ i ].sr_matched == NULL );
1567 assert( candidates[ i ].sr_text == NULL );
1568 assert( candidates[ i ].sr_ref == NULL );
1569
1570 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates, &prcookie, prsize ) )
1571 {
1572 case META_SEARCH_CANDIDATE:
1573 assert( candidates[ i ].sr_msgid >= 0 );
1574 ldap_controls_free( ctrls );
1575 goto free_message;
1576
1577 case META_SEARCH_ERR:
1578 err_pr:;
1579 candidates[ i ].sr_err = rs->sr_err;
1580 if ( META_BACK_ONERR_STOP( mi ) ) {
1581 savepriv = op->o_private;
1582 op->o_private = (void *)i;
1583 send_ldap_result( op, rs );
1584 op->o_private = savepriv;
1585 ldap_controls_free( ctrls );
1586 goto finish;
1587 }
1588 /* fallthru */
1589
1590 case META_SEARCH_NOT_CANDIDATE:
1591 /* means that meta_back_search_start()
1592 * failed but onerr == continue */
1593 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1594 assert( ncandidates > 0 );
1595 --ncandidates;
1596 break;
1597
1598 default:
1599 /* impossible */
1600 assert( 0 );
1601 break;
1602 }
1603 break;
1604 }
1605 }
1606 #endif /* SLAPD_META_CLIENT_PR */
1607 }
1608 /* fallthru */
1609
1610 case LDAP_REFERRAL:
1611 is_ok++;
1612 break;
1613
1614 case LDAP_SIZELIMIT_EXCEEDED:
1615 /* if a target returned sizelimitExceeded
1616 * and the entry count is equal to the
1617 * proxy's limit, the target would have
1618 * returned more, and the error must be
1619 * propagated to the client; otherwise,
1620 * the target enforced a limit lower
1621 * than what requested by the proxy;
1622 * ignore it */
1623 candidates[ i ].sr_err = rs->sr_err;
1624 if ( rs->sr_nentries == op->ors_slimit
1625 || META_BACK_ONERR_STOP( mi ) )
1626 {
1627 const char *save_text;
1628 got_err:
1629 save_text = rs->sr_text;
1630 savepriv = op->o_private;
1631 op->o_private = (void *)i;
1632 rs->sr_text = candidates[ i ].sr_text;
1633 send_ldap_result( op, rs );
1634 rs->sr_text = save_text;
1635 op->o_private = savepriv;
1636 ldap_msgfree( res );
1637 res = NULL;
1638 ldap_controls_free( ctrls );
1639 goto finish;
1640 }
1641 break;
1642
1643 default:
1644 candidates[ i ].sr_err = rs->sr_err;
1645 if ( META_BACK_ONERR_STOP( mi ) )
1646 goto got_err;
1647 break;
1648 }
1649
1650 ldap_controls_free( ctrls );
1651 last = i;
1652 rc = 0;
1653
1654 /*
1655 * When no candidates are left,
1656 * the outer cycle finishes
1657 */
1658 assert( ncandidates > 0 );
1659 --ncandidates;
1660
1661 } else if ( rc == LDAP_RES_BIND ) {
1662 meta_search_candidate_t retcode;
1663
1664 retcode = meta_search_dobind_result( op, rs, &mc, i, candidates, msg );
1665 if ( retcode == META_SEARCH_CANDIDATE ) {
1666 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1667 retcode = meta_back_search_start( op, rs, &dc, &mc, i, candidates, NULL, 0 );
1668 }
1669
1670 switch ( retcode ) {
1671 case META_SEARCH_CANDIDATE:
1672 break;
1673
1674 /* means that failed but onerr == continue */
1675 case META_SEARCH_NOT_CANDIDATE:
1676 case META_SEARCH_ERR:
1677 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1678 assert( ncandidates > 0 );
1679 --ncandidates;
1680
1681 candidates[ i ].sr_err = rs->sr_err;
1682 if ( META_BACK_ONERR_STOP( mi ) ) {
1683 savepriv = op->o_private;
1684 op->o_private = (void *)i;
1685 send_ldap_result( op, rs );
1686 op->o_private = savepriv;
1687 ldap_msgfree( res );
1688 res = NULL;
1689 goto finish;
1690 }
1691 goto free_message;
1692
1693 default:
1694 assert( 0 );
1695 break;
1696 }
1697
1698 } else {
1699 Debug( LDAP_DEBUG_ANY,
1700 "%s meta_back_search[%ld]: "
1701 "unrecognized response message tag=%d\n",
1702 op->o_log_prefix,
1703 i, rc );
1704
1705 ldap_msgfree( res );
1706 res = NULL;
1707 goto really_bad;
1708 }
1709 }
1710
1711 free_message:;
1712 ldap_msgfree( res );
1713 res = NULL;
1714 }
1715
1716 /* check for abandon */
1717 if ( op->o_abandon || LDAP_BACK_CONN_ABANDON( mc ) ) {
1718 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1719 if ( candidates[ i ].sr_msgid >= 0
1720 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1721 {
1722 if ( META_IS_BINDING( &candidates[ i ] )
1723 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1724 {
1725 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1726 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] )
1727 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1728 {
1729 /* if still binding, destroy */
1730
1731 #ifdef DEBUG_205
1732 Debug(LDAP_DEBUG_ANY,
1733 "### %s meta_back_search(abandon) " "ldap_unbind_ext[%ld] mc=%p ld=%p\n",
1734 op->o_log_prefix,
1735 i, (void *)mc,
1736 (void *)mc->mc_conns[i].msc_ld );
1737 #endif /* DEBUG_205 */
1738
1739 meta_clear_one_candidate( op, mc, i );
1740 }
1741 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1742 META_BINDING_CLEAR( &candidates[ i ] );
1743
1744 } else {
1745 (void)meta_back_cancel( mc, op, rs,
1746 candidates[ i ].sr_msgid, i,
1747 LDAP_BACK_DONTSEND );
1748 }
1749
1750 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1751 assert( ncandidates > 0 );
1752 --ncandidates;
1753 }
1754 }
1755
1756 if ( op->o_abandon ) {
1757 rc = SLAPD_ABANDON;
1758 }
1759
1760 /* let send_ldap_result play cleanup handlers (ITS#4645) */
1761 break;
1762 }
1763
1764 /* if no entry was found during this loop,
1765 * set a minimal timeout */
1766 if ( ncandidates > 0 && gotit == 0 ) {
1767 if ( save_tv.tv_sec == 0 && save_tv.tv_usec == 0 ) {
1768 save_tv.tv_usec = LDAP_BACK_RESULT_UTIMEOUT/initial_candidates;
1769
1770 /* arbitrarily limit to something between 1 and 2 minutes */
1771 } else if ( ( stoptime == -1 && save_tv.tv_sec < 60 )
1772 || save_tv.tv_sec < ( stoptime - slap_get_time() ) / ( 2 * ncandidates ) )
1773 {
1774 /* double the timeout */
1775 lutil_timermul( &save_tv, 2, &save_tv );
1776 }
1777
1778 if ( alreadybound == 0 ) {
1779 tv = save_tv;
1780 (void)select( 0, NULL, NULL, NULL, &tv );
1781
1782 } else {
1783 ldap_pvt_thread_yield();
1784 }
1785 }
1786 }
1787
1788 if ( rc == -1 ) {
1789 /*
1790 * FIXME: need a better strategy to handle errors
1791 */
1792 if ( mc ) {
1793 rc = meta_back_op_result( mc, op, rs, META_TARGET_NONE,
1794 -1, stoptime != -1 ? (stoptime - slap_get_time()) : 0,
1795 LDAP_BACK_SENDERR );
1796 } else {
1797 rc = rs->sr_err;
1798 }
1799 goto finish;
1800 }
1801
1802 /*
1803 * Rewrite the matched portion of the search base, if required
1804 *
1805 * FIXME: only the last one gets caught!
1806 */
1807 savepriv = op->o_private;
1808 op->o_private = (void *)(long)mi->mi_ntargets;
1809 if ( candidate_match > 0 ) {
1810 struct berval pmatched = BER_BVNULL;
1811
1812 /* we use the first one */
1813 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1814 if ( META_IS_CANDIDATE( &candidates[ i ] )
1815 && candidates[ i ].sr_matched != NULL )
1816 {
1817 struct berval bv, pbv;
1818 int rc;
1819
1820 /* if we got success, and this target
1821 * returned noSuchObject, and its suffix
1822 * is a superior of the searchBase,
1823 * ignore the matchedDN */
1824 if ( sres == LDAP_SUCCESS
1825 && candidates[ i ].sr_err == LDAP_NO_SUCH_OBJECT
1826 && op->o_req_ndn.bv_len > mi->mi_targets[ i ]->mt_nsuffix.bv_len )
1827 {
1828 free( (char *)candidates[ i ].sr_matched );
1829 candidates[ i ].sr_matched = NULL;
1830 continue;
1831 }
1832
1833 ber_str2bv( candidates[ i ].sr_matched, 0, 0, &bv );
1834 rc = dnPretty( NULL, &bv, &pbv, op->o_tmpmemctx );
1835
1836 if ( rc == LDAP_SUCCESS ) {
1837
1838 /* NOTE: if they all are superiors
1839 * of the baseDN, the shorter is also
1840 * superior of the longer... */
1841 if ( pbv.bv_len > pmatched.bv_len ) {
1842 if ( !BER_BVISNULL( &pmatched ) ) {
1843 op->o_tmpfree( pmatched.bv_val, op->o_tmpmemctx );
1844 }
1845 pmatched = pbv;
1846 op->o_private = (void *)i;
1847
1848 } else {
1849 op->o_tmpfree( pbv.bv_val, op->o_tmpmemctx );
1850 }
1851 }
1852
1853 if ( candidates[ i ].sr_matched != NULL ) {
1854 free( (char *)candidates[ i ].sr_matched );
1855 candidates[ i ].sr_matched = NULL;
1856 }
1857 }
1858 }
1859
1860 if ( !BER_BVISNULL( &pmatched ) ) {
1861 matched = pmatched.bv_val;
1862 }
1863
1864 } else if ( sres == LDAP_NO_SUCH_OBJECT ) {
1865 matched = op->o_bd->be_suffix[ 0 ].bv_val;
1866 }
1867
1868 /*
1869 * In case we returned at least one entry, we return LDAP_SUCCESS
1870 * otherwise, the latter error code we got
1871 */
1872
1873 if ( sres == LDAP_SUCCESS ) {
1874 if ( rs->sr_v2ref ) {
1875 sres = LDAP_REFERRAL;
1876 }
1877
1878 if ( META_BACK_ONERR_REPORT( mi ) ) {
1879 /*
1880 * Report errors, if any
1881 *
1882 * FIXME: we should handle error codes and return the more
1883 * important/reasonable
1884 */
1885 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1886 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1887 continue;
1888 }
1889
1890 if ( candidates[ i ].sr_err != LDAP_SUCCESS
1891 && candidates[ i ].sr_err != LDAP_NO_SUCH_OBJECT )
1892 {
1893 sres = candidates[ i ].sr_err;
1894 break;
1895 }
1896 }
1897 }
1898 }
1899
1900 rs->sr_err = sres;
1901 rs->sr_matched = ( sres == LDAP_SUCCESS ? NULL : matched );
1902 rs->sr_ref = ( sres == LDAP_REFERRAL ? rs->sr_v2ref : NULL );
1903 send_ldap_result( op, rs );
1904 op->o_private = savepriv;
1905 rs->sr_matched = NULL;
1906 rs->sr_ref = NULL;
1907
1908 finish:;
1909 if ( matched && matched != op->o_bd->be_suffix[ 0 ].bv_val ) {
1910 op->o_tmpfree( matched, op->o_tmpmemctx );
1911 }
1912
1913 if ( rs->sr_v2ref ) {
1914 ber_bvarray_free_x( rs->sr_v2ref, op->o_tmpmemctx );
1915 }
1916
1917 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1918 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1919 continue;
1920 }
1921
1922 if ( mc ) {
1923 if ( META_IS_BINDING( &candidates[ i ] )
1924 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1925 {
1926 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1927 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] )
1928 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1929 {
1930 assert( candidates[ i ].sr_msgid >= 0
1931 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING );
1932 assert( mc->mc_conns[ i ].msc_ld != NULL );
1933
1934 #ifdef DEBUG_205
1935 Debug( LDAP_DEBUG_ANY, "### %s meta_back_search(cleanup) "
1936 "ldap_unbind_ext[%ld] ld=%p\n",
1937 op->o_log_prefix, i, (void *)mc->mc_conns[i].msc_ld );
1938 #endif /* DEBUG_205 */
1939
1940 /* if still binding, destroy */
1941 meta_clear_one_candidate( op, mc, i );
1942 }
1943 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1944 META_BINDING_CLEAR( &candidates[ i ] );
1945
1946 } else if ( candidates[ i ].sr_msgid >= 0 ) {
1947 (void)meta_back_cancel( mc, op, rs,
1948 candidates[ i ].sr_msgid, i,
1949 LDAP_BACK_DONTSEND );
1950 }
1951 }
1952
1953 if ( candidates[ i ].sr_matched ) {
1954 free( (char *)candidates[ i ].sr_matched );
1955 candidates[ i ].sr_matched = NULL;
1956 }
1957
1958 if ( candidates[ i ].sr_text ) {
1959 ldap_memfree( (char *)candidates[ i ].sr_text );
1960 candidates[ i ].sr_text = NULL;
1961 }
1962
1963 if ( candidates[ i ].sr_ref ) {
1964 ber_bvarray_free( candidates[ i ].sr_ref );
1965 candidates[ i ].sr_ref = NULL;
1966 }
1967
1968 if ( candidates[ i ].sr_ctrls ) {
1969 ldap_controls_free( candidates[ i ].sr_ctrls );
1970 candidates[ i ].sr_ctrls = NULL;
1971 }
1972
1973 if ( META_BACK_TGT_QUARANTINE( mi->mi_targets[ i ] ) ) {
1974 meta_back_quarantine( op, &candidates[ i ], i );
1975 }
1976
1977 /* only in case of timelimit exceeded, if the timelimit exceeded because
1978 * one contacted target never responded, invalidate the connection
1979 * NOTE: should we quarantine the target as well? right now, the connection
1980 * is invalidated; the next time it will be recreated and the target
1981 * will be quarantined if it cannot be contacted */
1982 if ( mi->mi_idle_timeout != 0
1983 && rs->sr_err == LDAP_TIMELIMIT_EXCEEDED
1984 && op->o_time > mc->mc_conns[ i ].msc_time )
1985 {
1986 /* don't let anyone else use this expired connection */
1987 do_taint++;
1988 }
1989 }
1990
1991 if ( mc ) {
1992 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1993 if ( do_taint ) {
1994 LDAP_BACK_CONN_TAINTED_SET( mc );
1995 }
1996 meta_back_release_conn_lock( mi, mc, 0 );
1997 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1998 }
1999
2000 op->o_tmpfree( candidates, op->o_tmpmemctx );
2001 return rs->sr_err;
2002 }
2003
2004 static int
2005 meta_send_entry(
2006 Operation *op,
2007 SlapReply *rs,
2008 metaconn_t *mc,
2009 int target,
2010 LDAPMessage *e )
2011 {
2012 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
2013 struct berval a, mapped;
2014 int check_duplicate_attrs = 0;
2015 int check_sorted_attrs = 0;
2016 Entry ent = { 0 };
2017 BerElement ber = *ldap_get_message_ber( e );
2018 Attribute *attr, **attrp;
2019 struct berval bdn,
2020 dn = BER_BVNULL;
2021 const char *text;
2022 dncookie dc;
2023 ber_len_t len;
2024 int rc;
2025
2026 if ( ber_scanf( &ber, "l{", &len ) == LBER_ERROR ) {
2027 return LDAP_DECODING_ERROR;
2028 }
2029
2030 if ( ber_set_option( &ber, LBER_OPT_REMAINING_BYTES, &len ) != LBER_OPT_SUCCESS ) {
2031 return LDAP_OTHER;
2032 }
2033
2034 if ( ber_scanf( &ber, "m{", &bdn ) == LBER_ERROR ) {
2035 return LDAP_DECODING_ERROR;
2036 }
2037
2038 /*
2039 * Rewrite the dn of the result, if needed
2040 */
2041 dc.target = mi->mi_targets[ target ];
2042 dc.conn = op->o_conn;
2043 dc.rs = rs;
2044 dc.ctx = "searchResult";
2045
2046 rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &dn );
2047 if ( rs->sr_err != LDAP_SUCCESS) {
2048 return rs->sr_err;
2049 }
2050
2051 /*
2052 * Note: this may fail if the target host(s) schema differs
2053 * from the one known to the meta, and a DN with unknown
2054 * attributes is returned.
2055 *
2056 * FIXME: should we log anything, or delegate to dnNormalize?
2057 */
2058 rc = dnPrettyNormal( NULL, &dn, &ent.e_name, &ent.e_nname,
2059 op->o_tmpmemctx );
2060 if ( dn.bv_val != bdn.bv_val ) {
2061 free( dn.bv_val );
2062 }
2063 BER_BVZERO( &dn );
2064
2065 if ( rc != LDAP_SUCCESS ) {
2066 Debug( LDAP_DEBUG_ANY,
2067 "%s meta_send_entry(\"%s\"): "
2068 "invalid DN syntax\n",
2069 op->o_log_prefix, ent.e_name.bv_val );
2070 rc = LDAP_INVALID_DN_SYNTAX;
2071 goto done;
2072 }
2073
2074 /*
2075 * cache dn
2076 */
2077 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
2078 ( void )meta_dncache_update_entry( &mi->mi_cache,
2079 &ent.e_nname, target );
2080 }
2081
2082 attrp = &ent.e_attrs;
2083
2084 dc.ctx = "searchAttrDN";
2085 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
2086 int last = 0;
2087 slap_syntax_validate_func *validate;
2088 slap_syntax_transform_func *pretty;
2089
2090 if ( ber_pvt_ber_remaining( &ber ) < 0 ) {
2091 Debug( LDAP_DEBUG_ANY,
2092 "%s meta_send_entry(\"%s\"): "
2093 "unable to parse attr \"%s\".\n",
2094 op->o_log_prefix, ent.e_name.bv_val, a.bv_val );
2095
2096 rc = LDAP_OTHER;
2097 goto done;
2098 }
2099
2100 if ( ber_pvt_ber_remaining( &ber ) == 0 ) {
2101 break;
2102 }
2103
2104 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_at,
2105 &a, &mapped, BACKLDAP_REMAP );
2106 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
2107 ( void )ber_scanf( &ber, "x" /* [W] */ );
2108 continue;
2109 }
2110 if ( mapped.bv_val != a.bv_val ) {
2111 /* will need to check for duplicate attrs */
2112 check_duplicate_attrs++;
2113 }
2114 attr = attr_alloc( NULL );
2115 if ( attr == NULL ) {
2116 rc = LDAP_OTHER;
2117 goto done;
2118 }
2119 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
2120 != LDAP_SUCCESS) {
2121 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text,
2122 SLAP_AD_PROXIED ) != LDAP_SUCCESS )
2123 {
2124 Debug(LDAP_DEBUG_ANY,
2125 "%s meta_send_entry(\"%s\"): " "slap_bv2undef_ad(%s): %s\n",
2126 op->o_log_prefix, ent.e_name.bv_val,
2127 mapped.bv_val, text );
2128 ( void )ber_scanf( &ber, "x" /* [W] */ );
2129 attr_free( attr );
2130 continue;
2131 }
2132 }
2133
2134 if ( attr->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL )
2135 check_sorted_attrs = 1;
2136
2137 /* no subschemaSubentry */
2138 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
2139 || attr->a_desc == slap_schema.si_ad_entryDN )
2140 {
2141
2142 /*
2143 * We eat target's subschemaSubentry because
2144 * a search for this value is likely not
2145 * to resolve to the appropriate backend;
2146 * later, the local subschemaSubentry is
2147 * added.
2148 *
2149 * We also eat entryDN because the frontend
2150 * will reattach it without checking if already
2151 * present...
2152 */
2153 ( void )ber_scanf( &ber, "x" /* [W] */ );
2154 attr_free(attr);
2155 continue;
2156 }
2157
2158 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
2159 || attr->a_vals == NULL )
2160 {
2161 attr->a_vals = (struct berval *)&slap_dummy_bv;
2162
2163 } else {
2164 for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); ++last )
2165 ;
2166 }
2167 attr->a_numvals = last;
2168
2169 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
2170 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
2171
2172 if ( !validate && !pretty ) {
2173 attr_free( attr );
2174 goto next_attr;
2175 }
2176
2177 if ( attr->a_desc == slap_schema.si_ad_objectClass
2178 || attr->a_desc == slap_schema.si_ad_structuralObjectClass )
2179 {
2180 struct berval *bv;
2181
2182 for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
2183 ObjectClass *oc;
2184
2185 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_oc,
2186 bv, &mapped, BACKLDAP_REMAP );
2187 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0') {
2188 remove_oc:;
2189 free( bv->bv_val );
2190 BER_BVZERO( bv );
2191 if ( --last < 0 ) {
2192 break;
2193 }
2194 *bv = attr->a_vals[ last ];
2195 BER_BVZERO( &attr->a_vals[ last ] );
2196 bv--;
2197
2198 } else if ( mapped.bv_val != bv->bv_val ) {
2199 int i;
2200
2201 for ( i = 0; !BER_BVISNULL( &attr->a_vals[ i ] ); i++ ) {
2202 if ( &attr->a_vals[ i ] == bv ) {
2203 continue;
2204 }
2205
2206 if ( ber_bvstrcasecmp( &mapped, &attr->a_vals[ i ] ) == 0 ) {
2207 break;
2208 }
2209 }
2210
2211 if ( !BER_BVISNULL( &attr->a_vals[ i ] ) ) {
2212 goto remove_oc;
2213 }
2214
2215 ber_bvreplace( bv, &mapped );
2216
2217 } else if ( ( oc = oc_bvfind_undef( bv ) ) == NULL ) {
2218 goto remove_oc;
2219
2220 } else {
2221 ber_bvreplace( bv, &oc->soc_cname );
2222 }
2223 }
2224 /*
2225 * It is necessary to try to rewrite attributes with
2226 * dn syntax because they might be used in ACLs as
2227 * members of groups; since ACLs are applied to the
2228 * rewritten stuff, no dn-based subecj clause could
2229 * be used at the ldap backend side (see
2230 * http://www.OpenLDAP.org/faq/data/cache/452.html)
2231 * The problem can be overcome by moving the dn-based
2232 * ACLs to the target directory server, and letting
2233 * everything pass thru the ldap backend.
2234 */
2235 } else {
2236 int i;
2237
2238 if ( attr->a_desc->ad_type->sat_syntax ==
2239 slap_schema.si_syn_distinguishedName )
2240 {
2241 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
2242
2243 } else if ( attr->a_desc == slap_schema.si_ad_ref ) {
2244 ldap_back_referral_result_rewrite( &dc, attr->a_vals, NULL );
2245
2246 }
2247
2248 for ( i = 0; i < last; i++ ) {
2249 struct berval pval;
2250 int rc;
2251
2252 if ( pretty ) {
2253 rc = ordered_value_pretty( attr->a_desc,
2254 &attr->a_vals[i], &pval, NULL );
2255
2256 } else {
2257 rc = ordered_value_validate( attr->a_desc,
2258 &attr->a_vals[i], 0 );
2259 }
2260
2261 if ( rc ) {
2262 ber_memfree( attr->a_vals[i].bv_val );
2263 if ( --last == i ) {
2264 BER_BVZERO( &attr->a_vals[ i ] );
2265 break;
2266 }
2267 attr->a_vals[i] = attr->a_vals[last];
2268 BER_BVZERO( &attr->a_vals[last] );
2269 i--;
2270 continue;
2271 }
2272
2273 if ( pretty ) {
2274 ber_memfree( attr->a_vals[i].bv_val );
2275 attr->a_vals[i] = pval;
2276 }
2277 }
2278
2279 if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
2280 attr_free( attr );
2281 goto next_attr;
2282 }
2283 }
2284
2285 if ( last && attr->a_desc->ad_type->sat_equality &&
2286 attr->a_desc->ad_type->sat_equality->smr_normalize )
2287 {
2288 int i;
2289
2290 attr->a_nvals = ch_malloc( ( last + 1 ) * sizeof( struct berval ) );
2291 for ( i = 0; i<last; i++ ) {
2292 /* if normalizer fails, drop this value */
2293 if ( ordered_value_normalize(
2294 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2295 attr->a_desc,
2296 attr->a_desc->ad_type->sat_equality,
2297 &attr->a_vals[i], &attr->a_nvals[i],
2298 NULL )) {
2299 ber_memfree( attr->a_vals[i].bv_val );
2300 if ( --last == i ) {
2301 BER_BVZERO( &attr->a_vals[ i ] );
2302 break;
2303 }
2304 attr->a_vals[i] = attr->a_vals[last];
2305 BER_BVZERO( &attr->a_vals[last] );
2306 i--;
2307 }
2308 }
2309 BER_BVZERO( &attr->a_nvals[i] );
2310 if ( last == 0 ) {
2311 attr_free( attr );
2312 goto next_attr;
2313 }
2314
2315 } else {
2316 attr->a_nvals = attr->a_vals;
2317 }
2318
2319 attr->a_numvals = last;
2320 *attrp = attr;
2321 attrp = &attr->a_next;
2322 next_attr:;
2323 }
2324
2325 /* only check if some mapping occurred */
2326 if ( check_duplicate_attrs ) {
2327 Attribute **ap;
2328
2329 for ( ap = &ent.e_attrs; *ap != NULL; ap = &(*ap)->a_next ) {
2330 Attribute **tap;
2331
2332 for ( tap = &(*ap)->a_next; *tap != NULL; ) {
2333 if ( (*tap)->a_desc == (*ap)->a_desc ) {
2334 Entry e = { 0 };
2335 Modification mod = { 0 };
2336 const char *text = NULL;
2337 char textbuf[ SLAP_TEXT_BUFLEN ];
2338 Attribute *next = (*tap)->a_next;
2339
2340 BER_BVSTR( &e.e_name, "" );
2341 BER_BVSTR( &e.e_nname, "" );
2342 e.e_attrs = *ap;
2343 mod.sm_op = LDAP_MOD_ADD;
2344 mod.sm_desc = (*ap)->a_desc;
2345 mod.sm_type = mod.sm_desc->ad_cname;
2346 mod.sm_numvals = (*ap)->a_numvals;
2347 mod.sm_values = (*tap)->a_vals;
2348 if ( (*tap)->a_nvals != (*tap)->a_vals ) {
2349 mod.sm_nvalues = (*tap)->a_nvals;
2350 }
2351
2352 (void)modify_add_values( &e, &mod,
2353 /* permissive */ 1,
2354 &text, textbuf, sizeof( textbuf ) );
2355
2356 /* should not insert new attrs! */
2357 assert( e.e_attrs == *ap );
2358
2359 attr_free( *tap );
2360 *tap = next;
2361
2362 } else {
2363 tap = &(*tap)->a_next;
2364 }
2365 }
2366 }
2367 }
2368
2369 /* Check for sorted attributes */
2370 if ( check_sorted_attrs ) {
2371 for ( attr = ent.e_attrs; attr; attr = attr->a_next ) {
2372 if ( attr->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL ) {
2373 while ( attr->a_numvals > 1 ) {
2374 int i;
2375 int rc = slap_sort_vals( (Modifications *)attr, &text, &i, op->o_tmpmemctx );
2376 if ( rc != LDAP_TYPE_OR_VALUE_EXISTS )
2377 break;
2378
2379 /* Strip duplicate values */
2380 if ( attr->a_nvals != attr->a_vals )
2381 ber_memfree( attr->a_nvals[i].bv_val );
2382 ber_memfree( attr->a_vals[i].bv_val );
2383 attr->a_numvals--;
2384 if ( (unsigned)i < attr->a_numvals ) {
2385 attr->a_vals[i] = attr->a_vals[attr->a_numvals];
2386 if ( attr->a_nvals != attr->a_vals )
2387 attr->a_nvals[i] = attr->a_nvals[attr->a_numvals];
2388 }
2389 BER_BVZERO(&attr->a_vals[attr->a_numvals]);
2390 if ( attr->a_nvals != attr->a_vals )
2391 BER_BVZERO(&attr->a_nvals[attr->a_numvals]);
2392 }
2393 attr->a_flags |= SLAP_ATTR_SORTED_VALS;
2394 }
2395 }
2396 }
2397
2398 ldap_get_entry_controls( mc->mc_conns[target].msc_ld,
2399 e, &rs->sr_ctrls );
2400 rs->sr_entry = &ent;
2401 rs->sr_attrs = op->ors_attrs;
2402 rs->sr_operational_attrs = NULL;
2403 rs->sr_flags = mi->mi_targets[ target ]->mt_rep_flags;
2404 rs->sr_err = LDAP_SUCCESS;
2405 rc = send_search_entry( op, rs );
2406 switch ( rc ) {
2407 case LDAP_UNAVAILABLE:
2408 rc = LDAP_OTHER;
2409 break;
2410 }
2411
2412 done:;
2413 rs->sr_entry = NULL;
2414 rs->sr_attrs = NULL;
2415 if ( rs->sr_ctrls != NULL ) {
2416 ldap_controls_free( rs->sr_ctrls );
2417 rs->sr_ctrls = NULL;
2418 }
2419 if ( !BER_BVISNULL( &ent.e_name ) ) {
2420 free( ent.e_name.bv_val );
2421 BER_BVZERO( &ent.e_name );
2422 }
2423 if ( !BER_BVISNULL( &ent.e_nname ) ) {
2424 free( ent.e_nname.bv_val );
2425 BER_BVZERO( &ent.e_nname );
2426 }
2427 entry_clean( &ent );
2428
2429 return rc;
2430 }
2431