2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2011 The OpenLDAP Foundation.
5 * Portions Copyright 2001-2003 Pierangelo Masarati.
6 * Portions Copyright 1999-2003 Howard Chu.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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>.
18 * This work was initially developed by the Howard Chu for inclusion
19 * in OpenLDAP Software and subsequently enhanced by Pierangelo
27 #include <ac/socket.h>
28 #include <ac/string.h>
33 #include "../back-ldap/back-ldap.h"
34 #include "back-meta.h"
35 #include "../../../libraries/liblber/lber-int.h"
37 /* IGNORE means that target does not (no longer) participate
39 * NOTREADY means the search on that target has not been initialized yet
41 #define META_MSGID_IGNORE (-1)
42 #define META_MSGID_NEED_BIND (-2)
43 #define META_MSGID_CONNECTING (-3)
53 typedef enum meta_search_candidate_t {
54 META_SEARCH_UNDEFINED = -2,
56 META_SEARCH_NOT_CANDIDATE,
57 META_SEARCH_CANDIDATE,
59 META_SEARCH_NEED_BIND,
60 META_SEARCH_CONNECTING
61 } meta_search_candidate_t;
64 * meta_search_dobind_init()
66 * initiates bind for a candidate target of a search.
68 static meta_search_candidate_t
69 meta_search_dobind_init(
74 SlapReply *candidates )
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 ];
81 struct berval binddn = msc->msc_bound_ndn,
87 meta_search_candidate_t retcode;
89 Debug( LDAP_DEBUG_TRACE, "%s >>> meta_search_dobind_init[%d]\n",
90 op->o_log_prefix, candidate, 0 );
93 * all the targets are already bound as pseudoroot
95 if ( mc->mc_authz_target == META_BOUND_ALL ) {
96 return META_SEARCH_CANDIDATE;
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) */
105 char buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
108 if ( LDAP_BACK_CONN_ISBOUND( msc ) ) {
112 snprintf( buf, sizeof( buf ), " mc=%p ld=%p%s DN=\"%s\"",
113 (void *)mc, (void *)msc->msc_ld,
114 bound ? " bound" : " anonymous",
115 bound == 0 ? "" : msc->msc_bound_ndn.bv_val );
116 Debug( LDAP_DEBUG_ANY, "### %s meta_search_dobind_init[%d]%s\n",
117 op->o_log_prefix, candidate, buf );
118 #endif /* DEBUG_205 */
120 retcode = META_SEARCH_CANDIDATE;
122 } else if ( META_BACK_CONN_CREATING( msc ) || LDAP_BACK_CONN_BINDING( msc ) ) {
123 /* another thread is binding the target for this conn; wait */
126 char buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
128 snprintf( buf, sizeof( buf ), " mc=%p ld=%p needbind",
129 (void *)mc, (void *)msc->msc_ld );
130 Debug( LDAP_DEBUG_ANY, "### %s meta_search_dobind_init[%d]%s\n",
131 op->o_log_prefix, candidate, buf );
132 #endif /* DEBUG_205 */
134 candidates[ candidate ].sr_msgid = META_MSGID_NEED_BIND;
135 retcode = META_SEARCH_NEED_BIND;
138 /* we'll need to bind the target for this conn */
141 char buf[ SLAP_TEXT_BUFLEN ];
143 snprintf( buf, sizeof( buf ), " mc=%p ld=%p binding",
144 (void *)mc, (void *)msc->msc_ld );
145 Debug( LDAP_DEBUG_ANY, "### %s meta_search_dobind_init[%d]%s\n",
146 op->o_log_prefix, candidate, buf );
147 #endif /* DEBUG_205 */
149 if ( msc->msc_ld == NULL ) {
150 /* for some reason (e.g. because formerly in "binding"
151 * state, with eventual connection expiration or invalidation)
152 * it was not initialized as expected */
154 Debug( LDAP_DEBUG_ANY, "%s meta_search_dobind_init[%d] mc=%p ld=NULL\n",
155 op->o_log_prefix, candidate, (void *)mc );
157 rc = meta_back_init_one_conn( op, rs, *mcp, candidate,
158 LDAP_BACK_CONN_ISPRIV( *mcp ), LDAP_BACK_DONTSEND, 0 );
161 assert( msc->msc_ld != NULL );
164 case LDAP_SERVER_DOWN:
165 case LDAP_UNAVAILABLE:
166 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
170 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
175 LDAP_BACK_CONN_BINDING_SET( msc );
178 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
180 if ( retcode != META_SEARCH_BINDING ) {
184 /* NOTE: this obsoletes pseudorootdn */
185 if ( op->o_conn != NULL &&
186 !op->o_do_not_cache &&
187 ( BER_BVISNULL( &msc->msc_bound_ndn ) ||
188 BER_BVISEMPTY( &msc->msc_bound_ndn ) ||
189 ( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
191 rc = meta_back_proxy_authz_cred( mc, candidate, op, rs, LDAP_BACK_DONTSEND, &binddn, &cred, &method );
195 case LDAP_UNAVAILABLE:
201 /* NOTE: we copy things here, even if bind didn't succeed yet,
202 * because the connection is not shared until bind is over */
203 if ( !BER_BVISNULL( &binddn ) ) {
204 ber_bvreplace( &msc->msc_bound_ndn, &binddn );
205 if ( META_BACK_TGT_SAVECRED( mt ) && !BER_BVISNULL( &cred ) ) {
206 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
207 memset( msc->msc_cred.bv_val, 0,
208 msc->msc_cred.bv_len );
210 ber_bvreplace( &msc->msc_cred, &cred );
214 if ( LDAP_BACK_CONN_ISBOUND( msc ) ) {
215 /* apparently, idassert was configured with SASL bind,
216 * so bind occurred inside meta_back_proxy_authz_cred() */
217 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
218 LDAP_BACK_CONN_BINDING_CLEAR( msc );
219 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
220 return META_SEARCH_CANDIDATE;
226 case LDAP_AUTH_SIMPLE:
227 /* do a simple bind with binddn, cred */
236 assert( msc->msc_ld != NULL );
238 /* connect must be async only the first time... */
239 ldap_set_option( msc->msc_ld, LDAP_OPT_CONNECT_ASYNC, LDAP_OPT_ON );
242 if ( !BER_BVISEMPTY( &binddn ) && BER_BVISEMPTY( &cred ) ) {
243 /* bind anonymously? */
244 Debug( LDAP_DEBUG_ANY, "%s meta_search_dobind_init[%d] mc=%p: "
245 "non-empty dn with empty cred; binding anonymously\n",
246 op->o_log_prefix, candidate, (void *)mc );
247 cred = slap_empty_bv;
249 } else if ( BER_BVISEMPTY( &binddn ) && !BER_BVISEMPTY( &cred ) ) {
251 Debug( LDAP_DEBUG_ANY, "%s meta_search_dobind_init[%d] mc=%p: "
252 "empty dn with non-empty cred: error\n",
253 op->o_log_prefix, candidate, (void *)mc );
257 rc = ldap_sasl_bind( msc->msc_ld, binddn.bv_val, LDAP_SASL_SIMPLE, &cred,
258 NULL, NULL, &candidates[ candidate ].sr_msgid );
262 char buf[ SLAP_TEXT_BUFLEN ];
264 snprintf( buf, sizeof( buf ), "meta_search_dobind_init[%d] mc=%p ld=%p rc=%d",
265 candidate, (void *)mc, (void *)mc->mc_conns[ candidate ].msc_ld, rc );
266 Debug( LDAP_DEBUG_ANY, "### %s %s\n",
267 op->o_log_prefix, buf, 0 );
269 #endif /* DEBUG_205 */
273 assert( candidates[ candidate ].sr_msgid >= 0 );
274 META_BINDING_SET( &candidates[ candidate ] );
275 return META_SEARCH_BINDING;
277 case LDAP_X_CONNECTING:
278 /* must retry, same conn */
279 candidates[ candidate ].sr_msgid = META_MSGID_CONNECTING;
280 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
281 LDAP_BACK_CONN_BINDING_CLEAR( msc );
282 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
283 return META_SEARCH_CONNECTING;
285 case LDAP_SERVER_DOWN:
287 /* This is the worst thing that could happen:
288 * the search will wait until the retry is over. */
289 if ( !META_IS_RETRYING( &candidates[ candidate ] ) ) {
290 META_RETRYING_SET( &candidates[ candidate ] );
292 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
294 assert( mc->mc_refcnt > 0 );
295 if ( LogTest( LDAP_DEBUG_ANY ) ) {
296 char buf[ SLAP_TEXT_BUFLEN ];
298 /* this lock is required; however,
299 * it's invoked only when logging is on */
300 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
301 snprintf( buf, sizeof( buf ),
302 "retrying URI=\"%s\" DN=\"%s\"",
304 BER_BVISNULL( &msc->msc_bound_ndn ) ?
305 "" : msc->msc_bound_ndn.bv_val );
306 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
308 Debug( LDAP_DEBUG_ANY,
309 "%s meta_search_dobind_init[%d]: %s.\n",
310 op->o_log_prefix, candidate, buf );
313 meta_clear_one_candidate( op, mc, candidate );
314 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
316 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
318 /* mc here must be the regular mc, reset and ready for init */
319 rc = meta_back_init_one_conn( op, rs, mc, candidate,
320 LDAP_BACK_CONN_ISPRIV( mc ), LDAP_BACK_DONTSEND, 0 );
322 if ( rc == LDAP_SUCCESS ) {
323 LDAP_BACK_CONN_BINDING_SET( msc );
326 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
328 if ( rc == LDAP_SUCCESS ) {
329 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
330 binddn = msc->msc_bound_ndn;
331 cred = msc->msc_cred;
336 if ( *mcp == NULL ) {
337 retcode = META_SEARCH_ERR;
338 rs->sr_err = LDAP_UNAVAILABLE;
339 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
347 rc = slap_map_api2result( rs );
349 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
350 meta_clear_one_candidate( op, mc, candidate );
351 candidates[ candidate ].sr_err = rc;
352 if ( META_BACK_ONERR_STOP( mi ) ) {
353 LDAP_BACK_CONN_TAINTED_SET( mc );
354 meta_back_release_conn_lock( mi, mc, 0 );
358 retcode = META_SEARCH_ERR;
361 retcode = META_SEARCH_NOT_CANDIDATE;
363 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
364 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
371 static meta_search_candidate_t
372 meta_search_dobind_result(
377 SlapReply *candidates,
380 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
381 metatarget_t *mt = mi->mi_targets[ candidate ];
382 metaconn_t *mc = *mcp;
383 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
385 meta_search_candidate_t retcode = META_SEARCH_NOT_CANDIDATE;
388 assert( msc->msc_ld != NULL );
390 /* FIXME: matched? referrals? response controls? */
391 rc = ldap_parse_result( msc->msc_ld, res,
392 &candidates[ candidate ].sr_err,
393 NULL, NULL, NULL, NULL, 0 );
394 if ( rc != LDAP_SUCCESS ) {
395 candidates[ candidate ].sr_err = rc;
397 rc = slap_map_api2result( &candidates[ candidate ] );
399 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
400 LDAP_BACK_CONN_BINDING_CLEAR( msc );
401 if ( rc != LDAP_SUCCESS ) {
402 meta_clear_one_candidate( op, mc, candidate );
403 candidates[ candidate ].sr_err = rc;
404 if ( META_BACK_ONERR_STOP( mi ) ) {
405 LDAP_BACK_CONN_TAINTED_SET( mc );
406 meta_back_release_conn_lock( mi, mc, 0 );
408 retcode = META_SEARCH_ERR;
413 /* FIXME: check if bound as idassert authcDN! */
414 if ( BER_BVISNULL( &msc->msc_bound_ndn )
415 || BER_BVISEMPTY( &msc->msc_bound_ndn ) )
417 LDAP_BACK_CONN_ISANON_SET( msc );
420 if ( META_BACK_TGT_SAVECRED( mt ) &&
421 !BER_BVISNULL( &msc->msc_cred ) &&
422 !BER_BVISEMPTY( &msc->msc_cred ) )
424 ldap_set_rebind_proc( msc->msc_ld, mt->mt_rebind_f, msc );
426 LDAP_BACK_CONN_ISBOUND_SET( msc );
428 retcode = META_SEARCH_CANDIDATE;
430 /* connect must be async */
431 ldap_set_option( msc->msc_ld, LDAP_OPT_CONNECT_ASYNC, LDAP_OPT_OFF );
434 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
435 META_BINDING_CLEAR( &candidates[ candidate ] );
437 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
442 static meta_search_candidate_t
443 meta_back_search_start(
449 SlapReply *candidates,
450 struct berval *prcookie,
453 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
454 metatarget_t *mt = mi->mi_targets[ candidate ];
455 metasingleconn_t *msc = &(*mcp)->mc_conns[ candidate ];
456 struct berval realbase = op->o_req_dn;
457 int realscope = op->ors_scope;
458 struct berval mbase = BER_BVNULL;
459 struct berval mfilter = BER_BVNULL;
460 char **mapped_attrs = NULL;
462 meta_search_candidate_t retcode;
463 struct timeval tv, *tvp = NULL;
465 LDAPControl **ctrls = NULL;
466 #ifdef SLAPD_META_CLIENT_PR
467 LDAPControl **save_ctrls = NULL;
468 #endif /* SLAPD_META_CLIENT_PR */
470 /* this should not happen; just in case... */
471 if ( msc->msc_ld == NULL ) {
472 Debug( LDAP_DEBUG_ANY,
473 "%s: meta_back_search_start candidate=%d ld=NULL%s.\n",
474 op->o_log_prefix, candidate,
475 META_BACK_ONERR_STOP( mi ) ? "" : " (ignored)" );
476 candidates[ candidate ].sr_err = LDAP_OTHER;
477 if ( META_BACK_ONERR_STOP( mi ) ) {
478 return META_SEARCH_ERR;
480 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
481 return META_SEARCH_NOT_CANDIDATE;
484 Debug( LDAP_DEBUG_TRACE, "%s >>> meta_back_search_start[%d]\n", op->o_log_prefix, candidate, 0 );
487 * modifies the base according to the scope, if required
489 if ( mt->mt_nsuffix.bv_len > op->o_req_ndn.bv_len ) {
490 switch ( op->ors_scope ) {
491 case LDAP_SCOPE_SUBTREE:
493 * make the target suffix the new base
494 * FIXME: this is very forgiving, because
495 * "illegal" searchBases may be turned
496 * into the suffix of the target; however,
497 * the requested searchBase already passed
498 * thru the candidate analyzer...
500 if ( dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) ) {
501 realbase = mt->mt_nsuffix;
502 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
503 realscope = LDAP_SCOPE_SUBORDINATE;
508 * this target is no longer candidate
510 retcode = META_SEARCH_NOT_CANDIDATE;
515 case LDAP_SCOPE_SUBORDINATE:
516 case LDAP_SCOPE_ONELEVEL:
518 struct berval rdn = mt->mt_nsuffix;
519 rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
520 if ( dnIsOneLevelRDN( &rdn )
521 && dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) )
524 * if there is exactly one level,
525 * make the target suffix the new
526 * base, and make scope "base"
528 realbase = mt->mt_nsuffix;
529 if ( op->ors_scope == LDAP_SCOPE_SUBORDINATE ) {
530 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
531 realscope = LDAP_SCOPE_SUBORDINATE;
533 realscope = LDAP_SCOPE_SUBTREE;
536 realscope = LDAP_SCOPE_BASE;
539 } /* else continue with the next case */
542 case LDAP_SCOPE_BASE:
544 * this target is no longer candidate
546 retcode = META_SEARCH_NOT_CANDIDATE;
551 /* initiate dobind */
552 retcode = meta_search_dobind_init( op, rs, mcp, candidate, candidates );
554 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%d]=%d\n", op->o_log_prefix, candidate, retcode );
556 if ( retcode != META_SEARCH_CANDIDATE ) {
561 * Rewrite the search base, if required
564 dc->ctx = "searchBase";
565 switch ( ldap_back_dn_massage( dc, &realbase, &mbase ) ) {
569 case LDAP_UNWILLING_TO_PERFORM:
570 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
571 rs->sr_text = "Operation not allowed";
572 send_ldap_result( op, rs );
573 retcode = META_SEARCH_ERR;
579 * this target is no longer candidate
581 retcode = META_SEARCH_NOT_CANDIDATE;
588 rc = ldap_back_filter_map_rewrite( dc, op->ors_filter,
589 &mfilter, BACKLDAP_MAP, op->o_tmpmemctx );
594 case LDAP_COMPARE_FALSE:
597 * this target is no longer candidate
599 retcode = META_SEARCH_NOT_CANDIDATE;
604 * Maps required attributes
606 rc = ldap_back_map_attrs( op, &mt->mt_rwmap.rwm_at,
607 op->ors_attrs, BACKLDAP_MAP, &mapped_attrs );
608 if ( rc != LDAP_SUCCESS ) {
610 * this target is no longer candidate
612 retcode = META_SEARCH_NOT_CANDIDATE;
616 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
617 tv.tv_sec = op->ors_tlimit > 0 ? op->ors_tlimit : 1;
622 #ifdef SLAPD_META_CLIENT_PR
623 save_ctrls = op->o_ctrls;
625 LDAPControl *pr_c = NULL;
629 for ( ; save_ctrls[i] != NULL; i++ );
631 pr_c = ldap_control_find( LDAP_CONTROL_PAGEDRESULTS, save_ctrls, NULL );
634 if ( pr_c != NULL ) nc--;
635 if ( mt->mt_ps > 0 || prcookie != NULL ) nc++;
637 if ( mt->mt_ps > 0 || prcookie != NULL || pr_c != NULL ) {
638 int src = 0, dst = 0;
639 BerElementBuffer berbuf;
640 BerElement *ber = (BerElement *)&berbuf;
641 struct berval val = BER_BVNULL;
644 len = sizeof( LDAPControl * )*( nc + 1 ) + sizeof( LDAPControl );
646 if ( mt->mt_ps > 0 || prcookie != NULL ) {
647 struct berval nullcookie = BER_BVNULL;
650 if ( prsize == 0 && mt->mt_ps > 0 ) prsize = mt->mt_ps;
651 if ( prcookie == NULL ) prcookie = &nullcookie;
653 ber_init2( ber, NULL, LBER_USE_DER );
654 tag = ber_printf( ber, "{iO}", prsize, prcookie );
655 if ( tag == LBER_ERROR ) {
657 (void) ber_free_buf( ber );
661 tag = ber_flatten2( ber, &val, 0 );
662 if ( tag == LBER_ERROR ) {
664 (void) ber_free_buf( ber );
668 len += val.bv_len + 1;
671 op->o_ctrls = op->o_tmpalloc( len, op->o_tmpmemctx );
673 for ( ; save_ctrls[ src ] != NULL; src++ ) {
674 if ( save_ctrls[ src ] != pr_c ) {
675 op->o_ctrls[ dst ] = save_ctrls[ src ];
681 if ( mt->mt_ps > 0 || prcookie != NULL ) {
682 op->o_ctrls[ dst ] = (LDAPControl *)&op->o_ctrls[ nc + 1 ];
684 op->o_ctrls[ dst ]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
685 op->o_ctrls[ dst ]->ldctl_iscritical = 1;
687 op->o_ctrls[ dst ]->ldctl_value.bv_val = (char *)&op->o_ctrls[ dst ][ 1 ];
688 AC_MEMCPY( op->o_ctrls[ dst ]->ldctl_value.bv_val, val.bv_val, val.bv_len + 1 );
689 op->o_ctrls[ dst ]->ldctl_value.bv_len = val.bv_len;
692 (void)ber_free_buf( ber );
695 op->o_ctrls[ dst ] = NULL;
699 #endif /* SLAPD_META_CLIENT_PR */
703 if ( meta_back_controls_add( op, rs, *mcp, candidate, &ctrls )
706 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
707 retcode = META_SEARCH_NOT_CANDIDATE;
714 assert( msc->msc_ld != NULL );
715 rc = ldap_pvt_search( msc->msc_ld,
716 mbase.bv_val, realscope, mfilter.bv_val,
717 mapped_attrs, op->ors_attrsonly,
718 ctrls, NULL, tvp, op->ors_slimit, op->ors_deref,
719 &candidates[ candidate ].sr_msgid );
722 retcode = META_SEARCH_CANDIDATE;
725 case LDAP_SERVER_DOWN:
726 if ( nretries && meta_back_retry( op, rs, mcp, candidate, LDAP_BACK_DONTSEND ) ) {
728 /* if the identity changed, there might be need to re-authz */
729 (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
733 if ( *mcp == NULL ) {
734 retcode = META_SEARCH_ERR;
735 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
741 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
742 retcode = META_SEARCH_NOT_CANDIDATE;
746 (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
747 #ifdef SLAPD_META_CLIENT_PR
748 if ( save_ctrls != op->o_ctrls ) {
749 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
750 op->o_ctrls = save_ctrls;
752 #endif /* SLAPD_META_CLIENT_PR */
754 if ( mapped_attrs ) {
755 ber_memfree_x( mapped_attrs, op->o_tmpmemctx );
757 if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
758 ber_memfree_x( mfilter.bv_val, op->o_tmpmemctx );
760 if ( mbase.bv_val != realbase.bv_val ) {
761 free( mbase.bv_val );
765 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_back_search_start[%d]=%d\n", op->o_log_prefix, candidate, retcode );
771 meta_back_search( Operation *op, SlapReply *rs )
773 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
775 struct timeval save_tv = { 0, 0 },
777 time_t stoptime = (time_t)(-1),
778 lastres_time = slap_get_time(),
780 int rc = 0, sres = LDAP_SUCCESS;
781 char *matched = NULL;
782 int last = 0, ncandidates = 0,
783 initial_candidates = 0, candidate_match = 0,
785 ldap_back_send_t sendok = LDAP_BACK_SENDERR;
790 SlapReply *candidates = NULL;
793 rs_assert_ready( rs );
794 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia, we can set rs = non-entry */
797 * controls are set in ldap_back_dobind()
799 * FIXME: in case of values return filter, we might want
800 * to map attrs and maybe rewrite value
803 mc = meta_back_getconn( op, rs, NULL, sendok );
808 dc.conn = op->o_conn;
811 if ( candidates == NULL ) candidates = meta_back_candidates_get( op );
815 for ( i = 0; i < mi->mi_ntargets; i++ ) {
816 /* reset sr_msgid; it is used in most loops
817 * to check if that target is still to be considered */
818 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
820 /* a target is marked as candidate by meta_back_getconn();
821 * if for any reason (an error, it's over or so) it is
822 * no longer active, sr_msgid is set to META_MSGID_IGNORE
823 * but it remains candidate, which means it has been active
824 * at some point during the operation. This allows to
825 * use its response code and more to compute the final
827 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
831 candidates[ i ].sr_matched = NULL;
832 candidates[ i ].sr_text = NULL;
833 candidates[ i ].sr_ref = NULL;
834 candidates[ i ].sr_ctrls = NULL;
835 candidates[ i ].sr_nentries = 0;
837 /* get largest timeout among candidates */
838 if ( mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ]
839 && mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ] > timeout )
841 timeout = mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ];
845 for ( i = 0; i < mi->mi_ntargets; i++ ) {
846 if ( !META_IS_CANDIDATE( &candidates[ i ] )
847 || candidates[ i ].sr_err != LDAP_SUCCESS )
852 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates, NULL, 0 ) )
854 case META_SEARCH_NOT_CANDIDATE:
855 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
858 case META_SEARCH_NEED_BIND:
862 case META_SEARCH_CONNECTING:
863 case META_SEARCH_CANDIDATE:
864 case META_SEARCH_BINDING:
865 candidates[ i ].sr_type = REP_INTERMEDIATE;
869 case META_SEARCH_ERR:
870 savepriv = op->o_private;
871 op->o_private = (void *)i;
872 send_ldap_result( op, rs );
873 op->o_private = savepriv;
883 if ( ncandidates > 0 && needbind == ncandidates ) {
885 * give up the second time...
887 * NOTE: this should not occur the second time, since a fresh
888 * connection has ben created; however, targets may also
889 * need bind because the bind timed out or so.
891 if ( sendok & LDAP_BACK_BINDING ) {
892 Debug( LDAP_DEBUG_ANY,
893 "%s meta_back_search: unable to initialize conn\n",
894 op->o_log_prefix, 0, 0 );
895 rs->sr_err = LDAP_UNAVAILABLE;
896 rs->sr_text = "unable to initialize connection to remote targets";
897 send_ldap_result( op, rs );
902 /* FIXME: better create a separate connection? */
903 sendok |= LDAP_BACK_BINDING;
906 Debug( LDAP_DEBUG_ANY, "*** %s drop mc=%p create new connection\n",
907 op->o_log_prefix, (void *)mc, 0 );
908 #endif /* DEBUG_205 */
910 meta_back_release_conn( mi, mc );
919 initial_candidates = ncandidates;
921 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
922 char cnd[ SLAP_TEXT_BUFLEN ];
925 for ( c = 0; c < mi->mi_ntargets; c++ ) {
926 if ( META_IS_CANDIDATE( &candidates[ c ] ) ) {
934 Debug( LDAP_DEBUG_TRACE, "%s meta_back_search: ncandidates=%d "
935 "cnd=\"%s\"\n", op->o_log_prefix, ncandidates, cnd );
938 if ( initial_candidates == 0 ) {
939 /* NOTE: here we are not sending any matchedDN;
940 * this is intended, because if the back-meta
941 * is serving this search request, but no valid
942 * candidate could be looked up, it means that
943 * there is a hole in the mapping of the targets
944 * and thus no knowledge of any remote superior
946 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: "
947 "base=\"%s\" scope=%d: "
948 "no candidate could be selected\n",
949 op->o_log_prefix, op->o_req_dn.bv_val,
952 /* FIXME: we're sending the first error we encounter;
953 * maybe we should pick the worst... */
954 rc = LDAP_NO_SUCH_OBJECT;
955 for ( i = 0; i < mi->mi_ntargets; i++ ) {
956 if ( META_IS_CANDIDATE( &candidates[ i ] )
957 && candidates[ i ].sr_err != LDAP_SUCCESS )
959 rc = candidates[ i ].sr_err;
964 send_ldap_error( op, rs, rc, NULL );
969 /* We pull apart the ber result, stuff it into a slapd entry, and
970 * let send_search_entry stuff it back into ber format. Slow & ugly,
971 * but this is necessary for version matching, and for ACL processing.
974 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
975 stoptime = op->o_time + op->ors_tlimit;
979 * In case there are no candidates, no cycle takes place...
981 * FIXME: we might use a queue, to better balance the load
982 * among the candidates
984 for ( rc = 0; ncandidates > 0; ) {
987 alreadybound = ncandidates;
990 if ( timeout && lastres_time > 0
991 && ( slap_get_time() - lastres_time ) > timeout )
994 rs->sr_text = "Operation timed out";
995 rc = rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
996 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
997 savepriv = op->o_private;
998 op->o_private = (void *)i;
999 send_ldap_result( op, rs );
1000 op->o_private = savepriv;
1004 /* check time limit */
1005 if ( op->ors_tlimit != SLAP_NO_LIMIT
1006 && slap_get_time() > stoptime )
1009 rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
1010 savepriv = op->o_private;
1011 op->o_private = (void *)i;
1012 send_ldap_result( op, rs );
1013 op->o_private = savepriv;
1017 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1018 meta_search_candidate_t retcode = META_SEARCH_UNDEFINED;
1019 metasingleconn_t *msc = &mc->mc_conns[ i ];
1020 LDAPMessage *res = NULL, *msg;
1022 /* if msgid is invalid, don't ldap_result() */
1023 if ( candidates[ i ].sr_msgid == META_MSGID_IGNORE ) {
1027 /* if target still needs bind, retry */
1028 if ( candidates[ i ].sr_msgid == META_MSGID_NEED_BIND
1029 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1031 /* initiate dobind */
1032 retcode = meta_search_dobind_init( op, rs, &mc, i, candidates );
1034 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%ld]=%d\n",
1035 op->o_log_prefix, i, retcode );
1037 switch ( retcode ) {
1038 case META_SEARCH_NEED_BIND:
1042 case META_SEARCH_CONNECTING:
1043 case META_SEARCH_BINDING:
1046 case META_SEARCH_ERR:
1047 candidates[ i ].sr_err = rs->sr_err;
1048 if ( META_BACK_ONERR_STOP( mi ) ) {
1049 savepriv = op->o_private;
1050 op->o_private = (void *)i;
1051 send_ldap_result( op, rs );
1052 op->o_private = savepriv;
1057 case META_SEARCH_NOT_CANDIDATE:
1059 * When no candidates are left,
1060 * the outer cycle finishes
1062 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1063 assert( ncandidates > 0 );
1067 case META_SEARCH_CANDIDATE:
1068 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1069 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates, NULL, 0 ) )
1071 case META_SEARCH_CANDIDATE:
1072 assert( candidates[ i ].sr_msgid >= 0 );
1075 case META_SEARCH_ERR:
1076 candidates[ i ].sr_err = rs->sr_err;
1077 if ( META_BACK_ONERR_STOP( mi ) ) {
1078 savepriv = op->o_private;
1079 op->o_private = (void *)i;
1080 send_ldap_result( op, rs );
1081 op->o_private = savepriv;
1086 case META_SEARCH_NOT_CANDIDATE:
1087 /* means that meta_back_search_start()
1088 * failed but onerr == continue */
1089 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1090 assert( ncandidates > 0 );
1109 /* check for abandon */
1110 if ( op->o_abandon || LDAP_BACK_CONN_ABANDON( mc ) ) {
1115 if ( msc->msc_ld == NULL ) {
1116 char buf[ SLAP_TEXT_BUFLEN ];
1118 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1119 snprintf( buf, sizeof( buf ),
1120 "%s meta_back_search[%ld] mc=%p msgid=%d%s%s%s\n",
1121 op->o_log_prefix, (long)i, (void *)mc,
1122 candidates[ i ].sr_msgid,
1123 META_IS_BINDING( &candidates[ i ] ) ? " binding" : "",
1124 LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ? " connbinding" : "",
1125 META_BACK_CONN_CREATING( &mc->mc_conns[ i ] ) ? " conncreating" : "" );
1126 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1128 Debug( LDAP_DEBUG_ANY, "!!! %s\n", buf, 0, 0 );
1130 #endif /* DEBUG_205 */
1133 * FIXME: handle time limit as well?
1134 * Note that target servers are likely
1135 * to handle it, so at some time we'll
1136 * get a LDAP_TIMELIMIT_EXCEEDED from
1140 rc = ldap_result( msc->msc_ld, candidates[ i ].sr_msgid,
1141 LDAP_MSG_RECEIVED, &tv, &res );
1144 /* FIXME: res should not need to be freed */
1145 assert( res == NULL );
1150 /* something REALLY bad happened! */
1151 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1152 candidates[ i ].sr_type = REP_RESULT;
1154 if ( meta_back_retry( op, rs, &mc, i, LDAP_BACK_DONTSEND ) ) {
1155 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1156 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates, NULL, 0 ) )
1158 /* means that failed but onerr == continue */
1159 case META_SEARCH_NOT_CANDIDATE:
1160 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1162 assert( ncandidates > 0 );
1165 candidates[ i ].sr_err = rs->sr_err;
1166 if ( META_BACK_ONERR_STOP( mi ) ) {
1167 savepriv = op->o_private;
1168 op->o_private = (void *)i;
1169 send_ldap_result( op, rs );
1170 op->o_private = savepriv;
1175 case META_SEARCH_CANDIDATE:
1176 /* get back into business... */
1179 case META_SEARCH_BINDING:
1180 case META_SEARCH_CONNECTING:
1181 case META_SEARCH_NEED_BIND:
1182 case META_SEARCH_UNDEFINED:
1186 /* unrecoverable error */
1187 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1188 rc = rs->sr_err = LDAP_OTHER;
1193 candidates[ i ].sr_err = rs->sr_err;
1194 if ( META_BACK_ONERR_STOP( mi ) ) {
1195 savepriv = op->o_private;
1196 op->o_private = (void *)i;
1197 send_ldap_result( op, rs );
1198 op->o_private = savepriv;
1204 * When no candidates are left,
1205 * the outer cycle finishes
1207 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1208 assert( ncandidates > 0 );
1210 rs->sr_err = candidates[ i ].sr_err;
1214 lastres_time = slap_get_time();
1216 /* only touch when activity actually took place... */
1217 if ( mi->mi_idle_timeout != 0 && msc->msc_time < lastres_time ) {
1218 msc->msc_time = lastres_time;
1223 for ( msg = ldap_first_message( msc->msc_ld, res );
1225 msg = ldap_next_message( msc->msc_ld, msg ) )
1227 rc = ldap_msgtype( msg );
1228 if ( rc == LDAP_RES_SEARCH_ENTRY ) {
1231 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1232 /* don't retry any more... */
1233 candidates[ i ].sr_type = REP_RESULT;
1236 /* count entries returned by target */
1237 candidates[ i ].sr_nentries++;
1241 e = ldap_first_entry( msc->msc_ld, msg );
1242 savepriv = op->o_private;
1243 op->o_private = (void *)i;
1244 rs->sr_err = meta_send_entry( op, rs, mc, i, e );
1246 switch ( rs->sr_err ) {
1247 case LDAP_SIZELIMIT_EXCEEDED:
1248 savepriv = op->o_private;
1249 op->o_private = (void *)i;
1250 send_ldap_result( op, rs );
1251 op->o_private = savepriv;
1252 rs->sr_err = LDAP_SUCCESS;
1253 ldap_msgfree( res );
1257 case LDAP_UNAVAILABLE:
1258 rs->sr_err = LDAP_OTHER;
1259 ldap_msgfree( res );
1263 op->o_private = savepriv;
1265 /* don't wait any longer... */
1268 save_tv.tv_usec = 0;
1270 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
1271 char **references = NULL;
1274 if ( META_BACK_TGT_NOREFS( mi->mi_targets[ i ] ) ) {
1278 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1279 /* don't retry any more... */
1280 candidates[ i ].sr_type = REP_RESULT;
1285 rc = ldap_parse_reference( msc->msc_ld, msg,
1286 &references, &rs->sr_ctrls, 0 );
1288 if ( rc != LDAP_SUCCESS ) {
1292 if ( references == NULL ) {
1296 #ifdef ENABLE_REWRITE
1297 dc.ctx = "referralDN";
1298 #else /* ! ENABLE_REWRITE */
1301 #endif /* ! ENABLE_REWRITE */
1303 /* FIXME: merge all and return at the end */
1305 for ( cnt = 0; references[ cnt ]; cnt++ )
1308 rs->sr_ref = ber_memalloc_x( sizeof( struct berval ) * ( cnt + 1 ),
1311 for ( cnt = 0; references[ cnt ]; cnt++ ) {
1312 ber_str2bv_x( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ],
1315 BER_BVZERO( &rs->sr_ref[ cnt ] );
1317 ( void )ldap_back_referral_result_rewrite( &dc, rs->sr_ref,
1320 if ( rs->sr_ref != NULL && !BER_BVISNULL( &rs->sr_ref[ 0 ] ) ) {
1321 /* ignore return value by now */
1322 savepriv = op->o_private;
1323 op->o_private = (void *)i;
1324 ( void )send_search_reference( op, rs );
1325 op->o_private = savepriv;
1327 ber_bvarray_free_x( rs->sr_ref, op->o_tmpmemctx );
1333 ber_memvfree( (void **)references );
1336 if ( rs->sr_ctrls ) {
1337 ldap_controls_free( rs->sr_ctrls );
1338 rs->sr_ctrls = NULL;
1341 } else if ( rc == LDAP_RES_INTERMEDIATE ) {
1342 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1343 /* don't retry any more... */
1344 candidates[ i ].sr_type = REP_RESULT;
1347 /* FIXME: response controls
1348 * are passed without checks */
1349 rs->sr_err = ldap_parse_intermediate( msc->msc_ld,
1351 (char **)&rs->sr_rspoid,
1355 if ( rs->sr_err != LDAP_SUCCESS ) {
1356 candidates[ i ].sr_type = REP_RESULT;
1357 ldap_msgfree( res );
1362 slap_send_ldap_intermediate( op, rs );
1364 if ( rs->sr_rspoid != NULL ) {
1365 ber_memfree( (char *)rs->sr_rspoid );
1366 rs->sr_rspoid = NULL;
1369 if ( rs->sr_rspdata != NULL ) {
1370 ber_bvfree( rs->sr_rspdata );
1371 rs->sr_rspdata = NULL;
1374 if ( rs->sr_ctrls != NULL ) {
1375 ldap_controls_free( rs->sr_ctrls );
1376 rs->sr_ctrls = NULL;
1379 } else if ( rc == LDAP_RES_SEARCH_RESULT ) {
1380 char buf[ SLAP_TEXT_BUFLEN ];
1381 char **references = NULL;
1382 LDAPControl **ctrls = NULL;
1384 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1385 /* don't retry any more... */
1386 candidates[ i ].sr_type = REP_RESULT;
1389 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1391 /* NOTE: ignores response controls
1392 * (and intermediate response controls
1393 * as well, except for those with search
1394 * references); this may not be correct,
1395 * but if they're not ignored then
1396 * back-meta would need to merge them
1397 * consistently (think of pagedResults...)
1399 /* FIXME: response controls? */
1400 rs->sr_err = ldap_parse_result( msc->msc_ld,
1402 &candidates[ i ].sr_err,
1403 (char **)&candidates[ i ].sr_matched,
1404 (char **)&candidates[ i ].sr_text,
1406 &ctrls /* &candidates[ i ].sr_ctrls (unused) */ ,
1408 if ( rs->sr_err != LDAP_SUCCESS ) {
1409 candidates[ i ].sr_err = rs->sr_err;
1410 sres = slap_map_api2result( &candidates[ i ] );
1411 candidates[ i ].sr_type = REP_RESULT;
1412 ldap_msgfree( res );
1417 rs->sr_err = candidates[ i ].sr_err;
1419 /* massage matchedDN if need be */
1420 if ( candidates[ i ].sr_matched != NULL ) {
1421 struct berval match, mmatch;
1423 ber_str2bv( candidates[ i ].sr_matched,
1425 candidates[ i ].sr_matched = NULL;
1427 dc.ctx = "matchedDN";
1428 dc.target = mi->mi_targets[ i ];
1429 if ( !ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
1430 if ( mmatch.bv_val == match.bv_val ) {
1431 candidates[ i ].sr_matched
1432 = ch_strdup( mmatch.bv_val );
1435 candidates[ i ].sr_matched = mmatch.bv_val;
1440 ldap_memfree( match.bv_val );
1443 /* add references to array */
1444 /* RFC 4511: referrals can only appear
1445 * if result code is LDAP_REFERRAL */
1446 if ( references != NULL
1447 && references[ 0 ] != NULL
1448 && references[ 0 ][ 0 ] != '\0' )
1450 if ( rs->sr_err != LDAP_REFERRAL ) {
1451 Debug( LDAP_DEBUG_ANY,
1452 "%s meta_back_search[%ld]: "
1453 "got referrals with err=%d\n",
1461 for ( cnt = 0; references[ cnt ]; cnt++ )
1464 sr_ref = ber_memalloc_x( sizeof( struct berval ) * ( cnt + 1 ),
1467 for ( cnt = 0; references[ cnt ]; cnt++ ) {
1468 ber_str2bv_x( references[ cnt ], 0, 1, &sr_ref[ cnt ],
1471 BER_BVZERO( &sr_ref[ cnt ] );
1473 ( void )ldap_back_referral_result_rewrite( &dc, sr_ref,
1476 if ( rs->sr_v2ref == NULL ) {
1477 rs->sr_v2ref = sr_ref;
1480 for ( cnt = 0; !BER_BVISNULL( &sr_ref[ cnt ] ); cnt++ ) {
1481 ber_bvarray_add_x( &rs->sr_v2ref, &sr_ref[ cnt ],
1484 ber_memfree_x( sr_ref, op->o_tmpmemctx );
1488 } else if ( rs->sr_err == LDAP_REFERRAL ) {
1489 Debug( LDAP_DEBUG_ANY,
1490 "%s meta_back_search[%ld]: "
1491 "got err=%d with null "
1492 "or empty referrals\n",
1496 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1500 ber_memvfree( (void **)references );
1502 sres = slap_map_api2result( rs );
1504 if ( LogTest( LDAP_DEBUG_TRACE | LDAP_DEBUG_ANY ) ) {
1505 snprintf( buf, sizeof( buf ),
1506 "%s meta_back_search[%ld] "
1507 "match=\"%s\" err=%ld",
1508 op->o_log_prefix, i,
1509 candidates[ i ].sr_matched ? candidates[ i ].sr_matched : "",
1510 (long) candidates[ i ].sr_err );
1511 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
1512 Debug( LDAP_DEBUG_TRACE, "%s.\n", buf, 0, 0 );
1515 Debug( LDAP_DEBUG_ANY, "%s (%s).\n",
1516 buf, ldap_err2string( candidates[ i ].sr_err ), 0 );
1521 case LDAP_NO_SUCH_OBJECT:
1522 /* is_ok is touched any time a valid
1523 * (even intermediate) result is
1524 * returned; as a consequence, if
1525 * a candidate returns noSuchObject
1526 * it is ignored and the candidate
1527 * is simply demoted. */
1529 sres = LDAP_SUCCESS;
1534 if ( ctrls != NULL && ctrls[0] != NULL ) {
1535 #ifdef SLAPD_META_CLIENT_PR
1538 pr_c = ldap_control_find( LDAP_CONTROL_PAGEDRESULTS, ctrls, NULL );
1539 if ( pr_c != NULL ) {
1540 BerElementBuffer berbuf;
1541 BerElement *ber = (BerElement *)&berbuf;
1544 struct berval prcookie;
1546 /* unsolicited, do not accept */
1547 if ( mi->mi_targets[i]->mt_ps == 0 ) {
1548 rs->sr_err = LDAP_OTHER;
1552 ber_init2( ber, &pr_c->ldctl_value, LBER_USE_DER );
1554 tag = ber_scanf( ber, "{im}", &prsize, &prcookie );
1555 if ( tag == LBER_ERROR ) {
1556 rs->sr_err = LDAP_OTHER;
1560 /* more pages? new search request */
1561 if ( !BER_BVISNULL( &prcookie ) && !BER_BVISEMPTY( &prcookie ) ) {
1562 if ( mi->mi_targets[i]->mt_ps > 0 ) {
1563 /* ignore size if specified */
1566 } else if ( prsize == 0 ) {
1567 /* guess the page size from the entries returned so far */
1568 prsize = candidates[ i ].sr_nentries;
1571 candidates[ i ].sr_nentries = 0;
1572 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1573 candidates[ i ].sr_type = REP_INTERMEDIATE;
1575 assert( candidates[ i ].sr_matched == NULL );
1576 assert( candidates[ i ].sr_text == NULL );
1577 assert( candidates[ i ].sr_ref == NULL );
1579 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates, &prcookie, prsize ) )
1581 case META_SEARCH_CANDIDATE:
1582 assert( candidates[ i ].sr_msgid >= 0 );
1583 ldap_controls_free( ctrls );
1586 case META_SEARCH_ERR:
1588 candidates[ i ].sr_err = rs->sr_err;
1589 if ( META_BACK_ONERR_STOP( mi ) ) {
1590 savepriv = op->o_private;
1591 op->o_private = (void *)i;
1592 send_ldap_result( op, rs );
1593 op->o_private = savepriv;
1594 ldap_controls_free( ctrls );
1599 case META_SEARCH_NOT_CANDIDATE:
1600 /* means that meta_back_search_start()
1601 * failed but onerr == continue */
1602 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1603 assert( ncandidates > 0 );
1615 #endif /* SLAPD_META_CLIENT_PR */
1617 ldap_controls_free( ctrls );
1625 case LDAP_SIZELIMIT_EXCEEDED:
1626 /* if a target returned sizelimitExceeded
1627 * and the entry count is equal to the
1628 * proxy's limit, the target would have
1629 * returned more, and the error must be
1630 * propagated to the client; otherwise,
1631 * the target enforced a limit lower
1632 * than what requested by the proxy;
1634 candidates[ i ].sr_err = rs->sr_err;
1635 if ( rs->sr_nentries == op->ors_slimit
1636 || META_BACK_ONERR_STOP( mi ) )
1638 const char *save_text = rs->sr_text;
1639 savepriv = op->o_private;
1640 op->o_private = (void *)i;
1641 rs->sr_text = candidates[ i ].sr_text;
1642 send_ldap_result( op, rs );
1643 rs->sr_text = save_text;
1644 op->o_private = savepriv;
1645 ldap_msgfree( res );
1652 candidates[ i ].sr_err = rs->sr_err;
1653 if ( META_BACK_ONERR_STOP( mi ) ) {
1654 const char *save_text = rs->sr_text;
1655 savepriv = op->o_private;
1656 op->o_private = (void *)i;
1657 rs->sr_text = candidates[ i ].sr_text;
1658 send_ldap_result( op, rs );
1659 rs->sr_text = save_text;
1660 op->o_private = savepriv;
1661 ldap_msgfree( res );
1672 * When no candidates are left,
1673 * the outer cycle finishes
1675 assert( ncandidates > 0 );
1678 } else if ( rc == LDAP_RES_BIND ) {
1679 meta_search_candidate_t retcode;
1681 retcode = meta_search_dobind_result( op, rs, &mc, i, candidates, msg );
1682 if ( retcode == META_SEARCH_CANDIDATE ) {
1683 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1684 retcode = meta_back_search_start( op, rs, &dc, &mc, i, candidates, NULL, 0 );
1687 switch ( retcode ) {
1688 case META_SEARCH_CANDIDATE:
1691 /* means that failed but onerr == continue */
1692 case META_SEARCH_NOT_CANDIDATE:
1693 case META_SEARCH_ERR:
1694 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1695 assert( ncandidates > 0 );
1698 candidates[ i ].sr_err = rs->sr_err;
1699 if ( META_BACK_ONERR_STOP( mi ) ) {
1700 savepriv = op->o_private;
1701 op->o_private = (void *)i;
1702 send_ldap_result( op, rs );
1703 op->o_private = savepriv;
1704 ldap_msgfree( res );
1716 Debug( LDAP_DEBUG_ANY,
1717 "%s meta_back_search[%ld]: "
1718 "unrecognized response message tag=%d\n",
1722 ldap_msgfree( res );
1729 ldap_msgfree( res );
1733 /* check for abandon */
1734 if ( op->o_abandon || LDAP_BACK_CONN_ABANDON( mc ) ) {
1735 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1736 if ( candidates[ i ].sr_msgid >= 0
1737 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1739 if ( META_IS_BINDING( &candidates[ i ] )
1740 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1742 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1743 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] )
1744 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1746 /* if still binding, destroy */
1749 char buf[ SLAP_TEXT_BUFLEN ];
1751 snprintf( buf, sizeof( buf), "%s meta_back_search(abandon) "
1752 "ldap_unbind_ext[%ld] mc=%p ld=%p",
1753 op->o_log_prefix, i, (void *)mc,
1754 (void *)mc->mc_conns[i].msc_ld );
1756 Debug( LDAP_DEBUG_ANY, "### %s\n", buf, 0, 0 );
1757 #endif /* DEBUG_205 */
1759 meta_clear_one_candidate( op, mc, i );
1761 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1762 META_BINDING_CLEAR( &candidates[ i ] );
1765 (void)meta_back_cancel( mc, op, rs,
1766 candidates[ i ].sr_msgid, i,
1767 LDAP_BACK_DONTSEND );
1770 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1771 assert( ncandidates > 0 );
1776 if ( op->o_abandon ) {
1780 /* let send_ldap_result play cleanup handlers (ITS#4645) */
1784 /* if no entry was found during this loop,
1785 * set a minimal timeout */
1786 if ( ncandidates > 0 && gotit == 0 ) {
1787 if ( save_tv.tv_sec == 0 && save_tv.tv_usec == 0 ) {
1788 save_tv.tv_usec = LDAP_BACK_RESULT_UTIMEOUT/initial_candidates;
1790 /* arbitrarily limit to something between 1 and 2 minutes */
1791 } else if ( ( stoptime == -1 && save_tv.tv_sec < 60 )
1792 || save_tv.tv_sec < ( stoptime - slap_get_time() ) / ( 2 * ncandidates ) )
1794 /* double the timeout */
1795 lutil_timermul( &save_tv, 2, &save_tv );
1798 if ( alreadybound == 0 ) {
1800 (void)select( 0, NULL, NULL, NULL, &tv );
1803 ldap_pvt_thread_yield();
1810 * FIXME: need a better strategy to handle errors
1813 rc = meta_back_op_result( mc, op, rs, META_TARGET_NONE,
1814 -1, stoptime != -1 ? (stoptime - slap_get_time()) : 0,
1815 LDAP_BACK_SENDERR );
1823 * Rewrite the matched portion of the search base, if required
1825 * FIXME: only the last one gets caught!
1827 savepriv = op->o_private;
1828 op->o_private = (void *)(long)mi->mi_ntargets;
1829 if ( candidate_match > 0 ) {
1830 struct berval pmatched = BER_BVNULL;
1832 /* we use the first one */
1833 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1834 if ( META_IS_CANDIDATE( &candidates[ i ] )
1835 && candidates[ i ].sr_matched != NULL )
1837 struct berval bv, pbv;
1840 /* if we got success, and this target
1841 * returned noSuchObject, and its suffix
1842 * is a superior of the searchBase,
1843 * ignore the matchedDN */
1844 if ( sres == LDAP_SUCCESS
1845 && candidates[ i ].sr_err == LDAP_NO_SUCH_OBJECT
1846 && op->o_req_ndn.bv_len > mi->mi_targets[ i ]->mt_nsuffix.bv_len )
1848 free( (char *)candidates[ i ].sr_matched );
1849 candidates[ i ].sr_matched = NULL;
1853 ber_str2bv( candidates[ i ].sr_matched, 0, 0, &bv );
1854 rc = dnPretty( NULL, &bv, &pbv, op->o_tmpmemctx );
1856 if ( rc == LDAP_SUCCESS ) {
1858 /* NOTE: if they all are superiors
1859 * of the baseDN, the shorter is also
1860 * superior of the longer... */
1861 if ( pbv.bv_len > pmatched.bv_len ) {
1862 if ( !BER_BVISNULL( &pmatched ) ) {
1863 op->o_tmpfree( pmatched.bv_val, op->o_tmpmemctx );
1866 op->o_private = (void *)i;
1869 op->o_tmpfree( pbv.bv_val, op->o_tmpmemctx );
1873 if ( candidates[ i ].sr_matched != NULL ) {
1874 free( (char *)candidates[ i ].sr_matched );
1875 candidates[ i ].sr_matched = NULL;
1880 if ( !BER_BVISNULL( &pmatched ) ) {
1881 matched = pmatched.bv_val;
1884 } else if ( sres == LDAP_NO_SUCH_OBJECT ) {
1885 matched = op->o_bd->be_suffix[ 0 ].bv_val;
1889 * In case we returned at least one entry, we return LDAP_SUCCESS
1890 * otherwise, the latter error code we got
1893 if ( sres == LDAP_SUCCESS ) {
1894 if ( rs->sr_v2ref ) {
1895 sres = LDAP_REFERRAL;
1898 if ( META_BACK_ONERR_REPORT( mi ) ) {
1900 * Report errors, if any
1902 * FIXME: we should handle error codes and return the more
1903 * important/reasonable
1905 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1906 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1910 if ( candidates[ i ].sr_err != LDAP_SUCCESS
1911 && candidates[ i ].sr_err != LDAP_NO_SUCH_OBJECT )
1913 sres = candidates[ i ].sr_err;
1921 rs->sr_matched = ( sres == LDAP_SUCCESS ? NULL : matched );
1922 rs->sr_ref = ( sres == LDAP_REFERRAL ? rs->sr_v2ref : NULL );
1923 send_ldap_result( op, rs );
1924 op->o_private = savepriv;
1925 rs->sr_matched = NULL;
1929 if ( matched && matched != op->o_bd->be_suffix[ 0 ].bv_val ) {
1930 op->o_tmpfree( matched, op->o_tmpmemctx );
1933 if ( rs->sr_v2ref ) {
1934 ber_bvarray_free_x( rs->sr_v2ref, op->o_tmpmemctx );
1937 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1938 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1943 if ( META_IS_BINDING( &candidates[ i ] )
1944 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1946 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1947 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] )
1948 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1950 assert( candidates[ i ].sr_msgid >= 0
1951 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING );
1952 assert( mc->mc_conns[ i ].msc_ld != NULL );
1955 Debug( LDAP_DEBUG_ANY, "### %s meta_back_search(cleanup) "
1956 "ldap_unbind_ext[%ld] ld=%p\n",
1957 op->o_log_prefix, i, (void *)mc->mc_conns[i].msc_ld );
1958 #endif /* DEBUG_205 */
1960 /* if still binding, destroy */
1961 meta_clear_one_candidate( op, mc, i );
1963 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1964 META_BINDING_CLEAR( &candidates[ i ] );
1966 } else if ( candidates[ i ].sr_msgid >= 0 ) {
1967 (void)meta_back_cancel( mc, op, rs,
1968 candidates[ i ].sr_msgid, i,
1969 LDAP_BACK_DONTSEND );
1973 if ( candidates[ i ].sr_matched ) {
1974 free( (char *)candidates[ i ].sr_matched );
1975 candidates[ i ].sr_matched = NULL;
1978 if ( candidates[ i ].sr_text ) {
1979 ldap_memfree( (char *)candidates[ i ].sr_text );
1980 candidates[ i ].sr_text = NULL;
1983 if ( candidates[ i ].sr_ref ) {
1984 ber_bvarray_free( candidates[ i ].sr_ref );
1985 candidates[ i ].sr_ref = NULL;
1988 if ( candidates[ i ].sr_ctrls ) {
1989 ldap_controls_free( candidates[ i ].sr_ctrls );
1990 candidates[ i ].sr_ctrls = NULL;
1993 if ( META_BACK_TGT_QUARANTINE( mi->mi_targets[ i ] ) ) {
1994 meta_back_quarantine( op, &candidates[ i ], i );
1997 /* only in case of timelimit exceeded, if the timelimit exceeded because
1998 * one contacted target never responded, invalidate the connection
1999 * NOTE: should we quarantine the target as well? right now, the connection
2000 * is invalidated; the next time it will be recreated and the target
2001 * will be quarantined if it cannot be contacted */
2002 if ( mi->mi_idle_timeout != 0
2003 && rs->sr_err == LDAP_TIMELIMIT_EXCEEDED
2004 && op->o_time > mc->mc_conns[ i ].msc_time )
2006 /* don't let anyone else use this expired connection */
2012 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
2014 LDAP_BACK_CONN_TAINTED_SET( mc );
2016 meta_back_release_conn_lock( mi, mc, 0 );
2017 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
2031 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
2032 struct berval a, mapped;
2033 int check_duplicate_attrs = 0;
2034 int check_sorted_attrs = 0;
2036 BerElement ber = *ldap_get_message_ber( e );
2037 Attribute *attr, **attrp;
2045 if ( ber_scanf( &ber, "l{", &len ) == LBER_ERROR ) {
2046 return LDAP_DECODING_ERROR;
2049 if ( ber_set_option( &ber, LBER_OPT_REMAINING_BYTES, &len ) != LBER_OPT_SUCCESS ) {
2053 if ( ber_scanf( &ber, "m{", &bdn ) == LBER_ERROR ) {
2054 return LDAP_DECODING_ERROR;
2058 * Rewrite the dn of the result, if needed
2060 dc.target = mi->mi_targets[ target ];
2061 dc.conn = op->o_conn;
2063 dc.ctx = "searchResult";
2065 rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &dn );
2066 if ( rs->sr_err != LDAP_SUCCESS) {
2071 * Note: this may fail if the target host(s) schema differs
2072 * from the one known to the meta, and a DN with unknown
2073 * attributes is returned.
2075 * FIXME: should we log anything, or delegate to dnNormalize?
2077 rc = dnPrettyNormal( NULL, &dn, &ent.e_name, &ent.e_nname,
2079 if ( dn.bv_val != bdn.bv_val ) {
2084 if ( rc != LDAP_SUCCESS ) {
2085 Debug( LDAP_DEBUG_ANY,
2086 "%s meta_send_entry(\"%s\"): "
2087 "invalid DN syntax\n",
2088 op->o_log_prefix, ent.e_name.bv_val, 0 );
2089 rc = LDAP_INVALID_DN_SYNTAX;
2096 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
2097 ( void )meta_dncache_update_entry( &mi->mi_cache,
2098 &ent.e_nname, target );
2101 attrp = &ent.e_attrs;
2103 dc.ctx = "searchAttrDN";
2104 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
2106 slap_syntax_validate_func *validate;
2107 slap_syntax_transform_func *pretty;
2109 if ( ber_pvt_ber_remaining( &ber ) < 0 ) {
2110 Debug( LDAP_DEBUG_ANY,
2111 "%s meta_send_entry(\"%s\"): "
2112 "unable to parse attr \"%s\".\n",
2113 op->o_log_prefix, ent.e_name.bv_val, a.bv_val );
2119 if ( ber_pvt_ber_remaining( &ber ) == 0 ) {
2123 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_at,
2124 &a, &mapped, BACKLDAP_REMAP );
2125 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
2126 ( void )ber_scanf( &ber, "x" /* [W] */ );
2129 if ( mapped.bv_val != a.bv_val ) {
2130 /* will need to check for duplicate attrs */
2131 check_duplicate_attrs++;
2133 attr = attr_alloc( NULL );
2134 if ( attr == NULL ) {
2138 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
2140 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text,
2141 SLAP_AD_PROXIED ) != LDAP_SUCCESS )
2143 char buf[ SLAP_TEXT_BUFLEN ];
2145 snprintf( buf, sizeof( buf ),
2146 "%s meta_send_entry(\"%s\"): "
2147 "slap_bv2undef_ad(%s): %s\n",
2148 op->o_log_prefix, ent.e_name.bv_val,
2149 mapped.bv_val, text );
2151 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
2152 ( void )ber_scanf( &ber, "x" /* [W] */ );
2158 if ( attr->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL )
2159 check_sorted_attrs = 1;
2161 /* no subschemaSubentry */
2162 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
2163 || attr->a_desc == slap_schema.si_ad_entryDN )
2167 * We eat target's subschemaSubentry because
2168 * a search for this value is likely not
2169 * to resolve to the appropriate backend;
2170 * later, the local subschemaSubentry is
2173 * We also eat entryDN because the frontend
2174 * will reattach it without checking if already
2177 ( void )ber_scanf( &ber, "x" /* [W] */ );
2182 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
2183 || attr->a_vals == NULL )
2185 attr->a_vals = (struct berval *)&slap_dummy_bv;
2188 for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); ++last )
2191 attr->a_numvals = last;
2193 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
2194 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
2196 if ( !validate && !pretty ) {
2201 if ( attr->a_desc == slap_schema.si_ad_objectClass
2202 || attr->a_desc == slap_schema.si_ad_structuralObjectClass )
2206 for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
2209 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_oc,
2210 bv, &mapped, BACKLDAP_REMAP );
2211 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0') {
2218 *bv = attr->a_vals[ last ];
2219 BER_BVZERO( &attr->a_vals[ last ] );
2222 } else if ( mapped.bv_val != bv->bv_val ) {
2225 for ( i = 0; !BER_BVISNULL( &attr->a_vals[ i ] ); i++ ) {
2226 if ( &attr->a_vals[ i ] == bv ) {
2230 if ( ber_bvstrcasecmp( &mapped, &attr->a_vals[ i ] ) == 0 ) {
2235 if ( !BER_BVISNULL( &attr->a_vals[ i ] ) ) {
2239 ber_bvreplace( bv, &mapped );
2241 } else if ( ( oc = oc_bvfind_undef( bv ) ) == NULL ) {
2245 ber_bvreplace( bv, &oc->soc_cname );
2249 * It is necessary to try to rewrite attributes with
2250 * dn syntax because they might be used in ACLs as
2251 * members of groups; since ACLs are applied to the
2252 * rewritten stuff, no dn-based subecj clause could
2253 * be used at the ldap backend side (see
2254 * http://www.OpenLDAP.org/faq/data/cache/452.html)
2255 * The problem can be overcome by moving the dn-based
2256 * ACLs to the target directory server, and letting
2257 * everything pass thru the ldap backend.
2262 if ( attr->a_desc->ad_type->sat_syntax ==
2263 slap_schema.si_syn_distinguishedName )
2265 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
2267 } else if ( attr->a_desc == slap_schema.si_ad_ref ) {
2268 ldap_back_referral_result_rewrite( &dc, attr->a_vals, NULL );
2272 for ( i = 0; i < last; i++ ) {
2277 rc = ordered_value_pretty( attr->a_desc,
2278 &attr->a_vals[i], &pval, NULL );
2281 rc = ordered_value_validate( attr->a_desc,
2282 &attr->a_vals[i], 0 );
2286 ber_memfree( attr->a_vals[i].bv_val );
2287 if ( --last == i ) {
2288 BER_BVZERO( &attr->a_vals[ i ] );
2291 attr->a_vals[i] = attr->a_vals[last];
2292 BER_BVZERO( &attr->a_vals[last] );
2298 ber_memfree( attr->a_vals[i].bv_val );
2299 attr->a_vals[i] = pval;
2303 if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
2309 if ( last && attr->a_desc->ad_type->sat_equality &&
2310 attr->a_desc->ad_type->sat_equality->smr_normalize )
2314 attr->a_nvals = ch_malloc( ( last + 1 ) * sizeof( struct berval ) );
2315 for ( i = 0; i<last; i++ ) {
2316 /* if normalizer fails, drop this value */
2317 if ( ordered_value_normalize(
2318 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2320 attr->a_desc->ad_type->sat_equality,
2321 &attr->a_vals[i], &attr->a_nvals[i],
2323 ber_memfree( attr->a_vals[i].bv_val );
2324 if ( --last == i ) {
2325 BER_BVZERO( &attr->a_vals[ i ] );
2328 attr->a_vals[i] = attr->a_vals[last];
2329 BER_BVZERO( &attr->a_vals[last] );
2333 BER_BVZERO( &attr->a_nvals[i] );
2340 attr->a_nvals = attr->a_vals;
2343 attr->a_numvals = last;
2345 attrp = &attr->a_next;
2349 /* only check if some mapping occurred */
2350 if ( check_duplicate_attrs ) {
2353 for ( ap = &ent.e_attrs; *ap != NULL; ap = &(*ap)->a_next ) {
2356 for ( tap = &(*ap)->a_next; *tap != NULL; ) {
2357 if ( (*tap)->a_desc == (*ap)->a_desc ) {
2359 Modification mod = { 0 };
2360 const char *text = NULL;
2361 char textbuf[ SLAP_TEXT_BUFLEN ];
2362 Attribute *next = (*tap)->a_next;
2364 BER_BVSTR( &e.e_name, "" );
2365 BER_BVSTR( &e.e_nname, "" );
2367 mod.sm_op = LDAP_MOD_ADD;
2368 mod.sm_desc = (*ap)->a_desc;
2369 mod.sm_type = mod.sm_desc->ad_cname;
2370 mod.sm_numvals = (*ap)->a_numvals;
2371 mod.sm_values = (*tap)->a_vals;
2372 if ( (*tap)->a_nvals != (*tap)->a_vals ) {
2373 mod.sm_nvalues = (*tap)->a_nvals;
2376 (void)modify_add_values( &e, &mod,
2378 &text, textbuf, sizeof( textbuf ) );
2380 /* should not insert new attrs! */
2381 assert( e.e_attrs == *ap );
2387 tap = &(*tap)->a_next;
2393 /* Check for sorted attributes */
2394 if ( check_sorted_attrs ) {
2395 for ( attr = ent.e_attrs; attr; attr = attr->a_next ) {
2396 if ( attr->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL ) {
2397 while ( attr->a_numvals > 1 ) {
2399 int rc = slap_sort_vals( (Modifications *)attr, &text, &i, op->o_tmpmemctx );
2400 if ( rc != LDAP_TYPE_OR_VALUE_EXISTS )
2403 /* Strip duplicate values */
2404 if ( attr->a_nvals != attr->a_vals )
2405 ber_memfree( attr->a_nvals[i].bv_val );
2406 ber_memfree( attr->a_vals[i].bv_val );
2408 if ( (unsigned)i < attr->a_numvals ) {
2409 attr->a_vals[i] = attr->a_vals[attr->a_numvals];
2410 if ( attr->a_nvals != attr->a_vals )
2411 attr->a_nvals[i] = attr->a_nvals[attr->a_numvals];
2413 BER_BVZERO(&attr->a_vals[attr->a_numvals]);
2414 if ( attr->a_nvals != attr->a_vals )
2415 BER_BVZERO(&attr->a_nvals[attr->a_numvals]);
2417 attr->a_flags |= SLAP_ATTR_SORTED_VALS;
2422 ldap_get_entry_controls( mc->mc_conns[target].msc_ld,
2424 rs->sr_entry = &ent;
2425 rs->sr_attrs = op->ors_attrs;
2426 rs->sr_operational_attrs = NULL;
2427 rs->sr_flags = mi->mi_targets[ target ]->mt_rep_flags;
2428 rs->sr_err = LDAP_SUCCESS;
2429 rc = send_search_entry( op, rs );
2431 case LDAP_UNAVAILABLE:
2437 rs->sr_entry = NULL;
2438 rs->sr_attrs = NULL;
2439 if ( rs->sr_ctrls != NULL ) {
2440 ldap_controls_free( rs->sr_ctrls );
2441 rs->sr_ctrls = NULL;
2443 if ( !BER_BVISNULL( &ent.e_name ) ) {
2444 free( ent.e_name.bv_val );
2445 BER_BVZERO( &ent.e_name );
2447 if ( !BER_BVISNULL( &ent.e_nname ) ) {
2448 free( ent.e_nname.bv_val );
2449 BER_BVZERO( &ent.e_nname );
2451 entry_clean( &ent );