2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2014 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 /* check filter expression */
552 if ( mt->mt_filter ) {
554 for ( mf = mt->mt_filter; mf; mf = mf->mf_next ) {
555 if ( regexec( &mf->mf_regex, op->ors_filterstr.bv_val, 0, NULL, 0 ) == 0 )
558 /* nothing matched, this target is no longer a candidate */
560 retcode = META_SEARCH_NOT_CANDIDATE;
565 /* initiate dobind */
566 retcode = meta_search_dobind_init( op, rs, mcp, candidate, candidates );
568 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%d]=%d\n", op->o_log_prefix, candidate, retcode );
570 if ( retcode != META_SEARCH_CANDIDATE ) {
575 * Rewrite the search base, if required
578 dc->ctx = "searchBase";
579 switch ( ldap_back_dn_massage( dc, &realbase, &mbase ) ) {
583 case LDAP_UNWILLING_TO_PERFORM:
584 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
585 rs->sr_text = "Operation not allowed";
586 send_ldap_result( op, rs );
587 retcode = META_SEARCH_ERR;
593 * this target is no longer candidate
595 retcode = META_SEARCH_NOT_CANDIDATE;
602 rc = ldap_back_filter_map_rewrite( dc, op->ors_filter,
603 &mfilter, BACKLDAP_MAP, op->o_tmpmemctx );
608 case LDAP_COMPARE_FALSE:
611 * this target is no longer candidate
613 retcode = META_SEARCH_NOT_CANDIDATE;
618 * Maps required attributes
620 rc = ldap_back_map_attrs( op, &mt->mt_rwmap.rwm_at,
621 op->ors_attrs, BACKLDAP_MAP, &mapped_attrs );
622 if ( rc != LDAP_SUCCESS ) {
624 * this target is no longer candidate
626 retcode = META_SEARCH_NOT_CANDIDATE;
630 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
631 tv.tv_sec = op->ors_tlimit > 0 ? op->ors_tlimit : 1;
636 #ifdef SLAPD_META_CLIENT_PR
637 save_ctrls = op->o_ctrls;
639 LDAPControl *pr_c = NULL;
643 for ( ; save_ctrls[i] != NULL; i++ );
645 pr_c = ldap_control_find( LDAP_CONTROL_PAGEDRESULTS, save_ctrls, NULL );
648 if ( pr_c != NULL ) nc--;
649 if ( mt->mt_ps > 0 || prcookie != NULL ) nc++;
651 if ( mt->mt_ps > 0 || prcookie != NULL || pr_c != NULL ) {
652 int src = 0, dst = 0;
653 BerElementBuffer berbuf;
654 BerElement *ber = (BerElement *)&berbuf;
655 struct berval val = BER_BVNULL;
658 len = sizeof( LDAPControl * )*( nc + 1 ) + sizeof( LDAPControl );
660 if ( mt->mt_ps > 0 || prcookie != NULL ) {
661 struct berval nullcookie = BER_BVNULL;
664 if ( prsize == 0 && mt->mt_ps > 0 ) prsize = mt->mt_ps;
665 if ( prcookie == NULL ) prcookie = &nullcookie;
667 ber_init2( ber, NULL, LBER_USE_DER );
668 tag = ber_printf( ber, "{iO}", prsize, prcookie );
669 if ( tag == LBER_ERROR ) {
671 (void) ber_free_buf( ber );
675 tag = ber_flatten2( ber, &val, 0 );
676 if ( tag == LBER_ERROR ) {
678 (void) ber_free_buf( ber );
682 len += val.bv_len + 1;
685 op->o_ctrls = op->o_tmpalloc( len, op->o_tmpmemctx );
687 for ( ; save_ctrls[ src ] != NULL; src++ ) {
688 if ( save_ctrls[ src ] != pr_c ) {
689 op->o_ctrls[ dst ] = save_ctrls[ src ];
695 if ( mt->mt_ps > 0 || prcookie != NULL ) {
696 op->o_ctrls[ dst ] = (LDAPControl *)&op->o_ctrls[ nc + 1 ];
698 op->o_ctrls[ dst ]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
699 op->o_ctrls[ dst ]->ldctl_iscritical = 1;
701 op->o_ctrls[ dst ]->ldctl_value.bv_val = (char *)&op->o_ctrls[ dst ][ 1 ];
702 AC_MEMCPY( op->o_ctrls[ dst ]->ldctl_value.bv_val, val.bv_val, val.bv_len + 1 );
703 op->o_ctrls[ dst ]->ldctl_value.bv_len = val.bv_len;
706 (void)ber_free_buf( ber );
709 op->o_ctrls[ dst ] = NULL;
713 #endif /* SLAPD_META_CLIENT_PR */
717 if ( meta_back_controls_add( op, rs, *mcp, candidate, &ctrls )
720 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
721 retcode = META_SEARCH_NOT_CANDIDATE;
728 assert( msc->msc_ld != NULL );
729 rc = ldap_pvt_search( msc->msc_ld,
730 mbase.bv_val, realscope, mfilter.bv_val,
731 mapped_attrs, op->ors_attrsonly,
732 ctrls, NULL, tvp, op->ors_slimit, op->ors_deref,
733 &candidates[ candidate ].sr_msgid );
736 retcode = META_SEARCH_CANDIDATE;
739 case LDAP_SERVER_DOWN:
740 if ( nretries && meta_back_retry( op, rs, mcp, candidate, LDAP_BACK_DONTSEND ) ) {
742 /* if the identity changed, there might be need to re-authz */
743 (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
747 if ( *mcp == NULL ) {
748 retcode = META_SEARCH_ERR;
749 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
755 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
756 retcode = META_SEARCH_NOT_CANDIDATE;
760 (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
761 #ifdef SLAPD_META_CLIENT_PR
762 if ( save_ctrls != op->o_ctrls ) {
763 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
764 op->o_ctrls = save_ctrls;
766 #endif /* SLAPD_META_CLIENT_PR */
768 if ( mapped_attrs ) {
769 ber_memfree_x( mapped_attrs, op->o_tmpmemctx );
771 if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
772 ber_memfree_x( mfilter.bv_val, op->o_tmpmemctx );
774 if ( mbase.bv_val != realbase.bv_val ) {
775 free( mbase.bv_val );
779 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_back_search_start[%d]=%d\n", op->o_log_prefix, candidate, retcode );
785 meta_back_search( Operation *op, SlapReply *rs )
787 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
789 struct timeval save_tv = { 0, 0 },
791 time_t stoptime = (time_t)(-1),
792 lastres_time = slap_get_time(),
794 int rc = 0, sres = LDAP_SUCCESS;
795 char *matched = NULL;
796 int last = 0, ncandidates = 0,
797 initial_candidates = 0, candidate_match = 0,
799 ldap_back_send_t sendok = LDAP_BACK_SENDERR;
804 SlapReply *candidates = NULL;
807 rs_assert_ready( rs );
808 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia, we can set rs = non-entry */
811 * controls are set in ldap_back_dobind()
813 * FIXME: in case of values return filter, we might want
814 * to map attrs and maybe rewrite value
817 mc = meta_back_getconn( op, rs, NULL, sendok );
822 dc.conn = op->o_conn;
825 if ( candidates == NULL ) candidates = meta_back_candidates_get( op );
829 for ( i = 0; i < mi->mi_ntargets; i++ ) {
830 /* reset sr_msgid; it is used in most loops
831 * to check if that target is still to be considered */
832 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
834 /* a target is marked as candidate by meta_back_getconn();
835 * if for any reason (an error, it's over or so) it is
836 * no longer active, sr_msgid is set to META_MSGID_IGNORE
837 * but it remains candidate, which means it has been active
838 * at some point during the operation. This allows to
839 * use its response code and more to compute the final
841 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
845 candidates[ i ].sr_matched = NULL;
846 candidates[ i ].sr_text = NULL;
847 candidates[ i ].sr_ref = NULL;
848 candidates[ i ].sr_ctrls = NULL;
849 candidates[ i ].sr_nentries = 0;
851 /* get largest timeout among candidates */
852 if ( mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ]
853 && mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ] > timeout )
855 timeout = mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ];
859 for ( i = 0; i < mi->mi_ntargets; i++ ) {
860 if ( !META_IS_CANDIDATE( &candidates[ i ] )
861 || candidates[ i ].sr_err != LDAP_SUCCESS )
866 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates, NULL, 0 ) )
868 case META_SEARCH_NOT_CANDIDATE:
869 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
872 case META_SEARCH_NEED_BIND:
876 case META_SEARCH_CONNECTING:
877 case META_SEARCH_CANDIDATE:
878 case META_SEARCH_BINDING:
879 candidates[ i ].sr_type = REP_INTERMEDIATE;
883 case META_SEARCH_ERR:
884 savepriv = op->o_private;
885 op->o_private = (void *)i;
886 send_ldap_result( op, rs );
887 op->o_private = savepriv;
897 if ( ncandidates > 0 && needbind == ncandidates ) {
899 * give up the second time...
901 * NOTE: this should not occur the second time, since a fresh
902 * connection has ben created; however, targets may also
903 * need bind because the bind timed out or so.
905 if ( sendok & LDAP_BACK_BINDING ) {
906 Debug( LDAP_DEBUG_ANY,
907 "%s meta_back_search: unable to initialize conn\n",
908 op->o_log_prefix, 0, 0 );
909 rs->sr_err = LDAP_UNAVAILABLE;
910 rs->sr_text = "unable to initialize connection to remote targets";
911 send_ldap_result( op, rs );
916 /* FIXME: better create a separate connection? */
917 sendok |= LDAP_BACK_BINDING;
920 Debug( LDAP_DEBUG_ANY, "*** %s drop mc=%p create new connection\n",
921 op->o_log_prefix, (void *)mc, 0 );
922 #endif /* DEBUG_205 */
924 meta_back_release_conn( mi, mc );
933 initial_candidates = ncandidates;
935 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
936 char cnd[ SLAP_TEXT_BUFLEN ];
939 for ( c = 0; c < mi->mi_ntargets; c++ ) {
940 if ( META_IS_CANDIDATE( &candidates[ c ] ) ) {
948 Debug( LDAP_DEBUG_TRACE, "%s meta_back_search: ncandidates=%d "
949 "cnd=\"%s\"\n", op->o_log_prefix, ncandidates, cnd );
952 if ( initial_candidates == 0 ) {
953 /* NOTE: here we are not sending any matchedDN;
954 * this is intended, because if the back-meta
955 * is serving this search request, but no valid
956 * candidate could be looked up, it means that
957 * there is a hole in the mapping of the targets
958 * and thus no knowledge of any remote superior
960 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: "
961 "base=\"%s\" scope=%d: "
962 "no candidate could be selected\n",
963 op->o_log_prefix, op->o_req_dn.bv_val,
966 /* FIXME: we're sending the first error we encounter;
967 * maybe we should pick the worst... */
968 rc = LDAP_NO_SUCH_OBJECT;
969 for ( i = 0; i < mi->mi_ntargets; i++ ) {
970 if ( META_IS_CANDIDATE( &candidates[ i ] )
971 && candidates[ i ].sr_err != LDAP_SUCCESS )
973 rc = candidates[ i ].sr_err;
978 send_ldap_error( op, rs, rc, NULL );
983 /* We pull apart the ber result, stuff it into a slapd entry, and
984 * let send_search_entry stuff it back into ber format. Slow & ugly,
985 * but this is necessary for version matching, and for ACL processing.
988 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
989 stoptime = op->o_time + op->ors_tlimit;
993 * In case there are no candidates, no cycle takes place...
995 * FIXME: we might use a queue, to better balance the load
996 * among the candidates
998 for ( rc = 0; ncandidates > 0; ) {
1001 alreadybound = ncandidates;
1004 if ( timeout && lastres_time > 0
1005 && ( slap_get_time() - lastres_time ) > timeout )
1008 rs->sr_text = "Operation timed out";
1009 rc = rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
1010 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
1011 savepriv = op->o_private;
1012 op->o_private = (void *)i;
1013 send_ldap_result( op, rs );
1014 op->o_private = savepriv;
1018 /* check time limit */
1019 if ( op->ors_tlimit != SLAP_NO_LIMIT
1020 && slap_get_time() > stoptime )
1023 rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
1024 savepriv = op->o_private;
1025 op->o_private = (void *)i;
1026 send_ldap_result( op, rs );
1027 op->o_private = savepriv;
1031 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1032 meta_search_candidate_t retcode = META_SEARCH_UNDEFINED;
1033 metasingleconn_t *msc = &mc->mc_conns[ i ];
1034 LDAPMessage *res = NULL, *msg;
1036 /* if msgid is invalid, don't ldap_result() */
1037 if ( candidates[ i ].sr_msgid == META_MSGID_IGNORE ) {
1041 /* if target still needs bind, retry */
1042 if ( candidates[ i ].sr_msgid == META_MSGID_NEED_BIND
1043 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1045 /* initiate dobind */
1046 retcode = meta_search_dobind_init( op, rs, &mc, i, candidates );
1048 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%ld]=%d\n",
1049 op->o_log_prefix, i, retcode );
1051 switch ( retcode ) {
1052 case META_SEARCH_NEED_BIND:
1056 case META_SEARCH_CONNECTING:
1057 case META_SEARCH_BINDING:
1060 case META_SEARCH_ERR:
1061 candidates[ i ].sr_err = rs->sr_err;
1062 if ( META_BACK_ONERR_STOP( mi ) ) {
1063 savepriv = op->o_private;
1064 op->o_private = (void *)i;
1065 send_ldap_result( op, rs );
1066 op->o_private = savepriv;
1071 case META_SEARCH_NOT_CANDIDATE:
1073 * When no candidates are left,
1074 * the outer cycle finishes
1076 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1077 assert( ncandidates > 0 );
1081 case META_SEARCH_CANDIDATE:
1082 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1083 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates, NULL, 0 ) )
1085 case META_SEARCH_CANDIDATE:
1086 assert( candidates[ i ].sr_msgid >= 0 );
1089 case META_SEARCH_ERR:
1090 candidates[ i ].sr_err = rs->sr_err;
1091 if ( META_BACK_ONERR_STOP( mi ) ) {
1092 savepriv = op->o_private;
1093 op->o_private = (void *)i;
1094 send_ldap_result( op, rs );
1095 op->o_private = savepriv;
1100 case META_SEARCH_NOT_CANDIDATE:
1101 /* means that meta_back_search_start()
1102 * failed but onerr == continue */
1103 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1104 assert( ncandidates > 0 );
1123 /* check for abandon */
1124 if ( op->o_abandon || LDAP_BACK_CONN_ABANDON( mc ) ) {
1129 if ( msc->msc_ld == NULL ) {
1130 char buf[ SLAP_TEXT_BUFLEN ];
1132 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1133 snprintf( buf, sizeof( buf ),
1134 "%s meta_back_search[%ld] mc=%p msgid=%d%s%s%s\n",
1135 op->o_log_prefix, (long)i, (void *)mc,
1136 candidates[ i ].sr_msgid,
1137 META_IS_BINDING( &candidates[ i ] ) ? " binding" : "",
1138 LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ? " connbinding" : "",
1139 META_BACK_CONN_CREATING( &mc->mc_conns[ i ] ) ? " conncreating" : "" );
1140 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1142 Debug( LDAP_DEBUG_ANY, "!!! %s\n", buf, 0, 0 );
1144 #endif /* DEBUG_205 */
1147 * FIXME: handle time limit as well?
1148 * Note that target servers are likely
1149 * to handle it, so at some time we'll
1150 * get a LDAP_TIMELIMIT_EXCEEDED from
1154 rc = ldap_result( msc->msc_ld, candidates[ i ].sr_msgid,
1155 LDAP_MSG_RECEIVED, &tv, &res );
1158 /* FIXME: res should not need to be freed */
1159 assert( res == NULL );
1164 /* something REALLY bad happened! */
1165 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1166 candidates[ i ].sr_type = REP_RESULT;
1168 if ( meta_back_retry( op, rs, &mc, i, LDAP_BACK_DONTSEND ) ) {
1169 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1170 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates, NULL, 0 ) )
1172 /* means that failed but onerr == continue */
1173 case META_SEARCH_NOT_CANDIDATE:
1174 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1176 assert( ncandidates > 0 );
1179 candidates[ i ].sr_err = rs->sr_err;
1180 if ( META_BACK_ONERR_STOP( mi ) ) {
1181 savepriv = op->o_private;
1182 op->o_private = (void *)i;
1183 send_ldap_result( op, rs );
1184 op->o_private = savepriv;
1189 case META_SEARCH_CANDIDATE:
1190 /* get back into business... */
1193 case META_SEARCH_BINDING:
1194 case META_SEARCH_CONNECTING:
1195 case META_SEARCH_NEED_BIND:
1196 case META_SEARCH_UNDEFINED:
1200 /* unrecoverable error */
1201 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1202 rc = rs->sr_err = LDAP_OTHER;
1207 candidates[ i ].sr_err = rs->sr_err;
1208 if ( META_BACK_ONERR_STOP( mi ) ) {
1209 savepriv = op->o_private;
1210 op->o_private = (void *)i;
1211 send_ldap_result( op, rs );
1212 op->o_private = savepriv;
1218 * When no candidates are left,
1219 * the outer cycle finishes
1221 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1222 assert( ncandidates > 0 );
1224 rs->sr_err = candidates[ i ].sr_err;
1228 lastres_time = slap_get_time();
1230 /* only touch when activity actually took place... */
1231 if ( mi->mi_idle_timeout != 0 && msc->msc_time < lastres_time ) {
1232 msc->msc_time = lastres_time;
1237 for ( msg = ldap_first_message( msc->msc_ld, res );
1239 msg = ldap_next_message( msc->msc_ld, msg ) )
1241 rc = ldap_msgtype( msg );
1242 if ( rc == LDAP_RES_SEARCH_ENTRY ) {
1245 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1246 /* don't retry any more... */
1247 candidates[ i ].sr_type = REP_RESULT;
1250 /* count entries returned by target */
1251 candidates[ i ].sr_nentries++;
1255 e = ldap_first_entry( msc->msc_ld, msg );
1256 savepriv = op->o_private;
1257 op->o_private = (void *)i;
1258 rs->sr_err = meta_send_entry( op, rs, mc, i, e );
1260 switch ( rs->sr_err ) {
1261 case LDAP_SIZELIMIT_EXCEEDED:
1262 savepriv = op->o_private;
1263 op->o_private = (void *)i;
1264 send_ldap_result( op, rs );
1265 op->o_private = savepriv;
1266 rs->sr_err = LDAP_SUCCESS;
1267 ldap_msgfree( res );
1271 case LDAP_UNAVAILABLE:
1272 rs->sr_err = LDAP_OTHER;
1273 ldap_msgfree( res );
1277 op->o_private = savepriv;
1279 /* don't wait any longer... */
1282 save_tv.tv_usec = 0;
1284 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
1285 char **references = NULL;
1288 if ( META_BACK_TGT_NOREFS( mi->mi_targets[ i ] ) ) {
1292 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1293 /* don't retry any more... */
1294 candidates[ i ].sr_type = REP_RESULT;
1299 rc = ldap_parse_reference( msc->msc_ld, msg,
1300 &references, &rs->sr_ctrls, 0 );
1302 if ( rc != LDAP_SUCCESS ) {
1306 if ( references == NULL ) {
1310 #ifdef ENABLE_REWRITE
1311 dc.ctx = "referralDN";
1312 #else /* ! ENABLE_REWRITE */
1315 #endif /* ! ENABLE_REWRITE */
1317 /* FIXME: merge all and return at the end */
1319 for ( cnt = 0; references[ cnt ]; cnt++ )
1322 rs->sr_ref = ber_memalloc_x( sizeof( struct berval ) * ( cnt + 1 ),
1325 for ( cnt = 0; references[ cnt ]; cnt++ ) {
1326 ber_str2bv_x( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ],
1329 BER_BVZERO( &rs->sr_ref[ cnt ] );
1331 ( void )ldap_back_referral_result_rewrite( &dc, rs->sr_ref,
1334 if ( rs->sr_ref != NULL && !BER_BVISNULL( &rs->sr_ref[ 0 ] ) ) {
1335 /* ignore return value by now */
1336 savepriv = op->o_private;
1337 op->o_private = (void *)i;
1338 ( void )send_search_reference( op, rs );
1339 op->o_private = savepriv;
1341 ber_bvarray_free_x( rs->sr_ref, op->o_tmpmemctx );
1347 ber_memvfree( (void **)references );
1350 if ( rs->sr_ctrls ) {
1351 ldap_controls_free( rs->sr_ctrls );
1352 rs->sr_ctrls = NULL;
1355 } else if ( rc == LDAP_RES_INTERMEDIATE ) {
1356 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1357 /* don't retry any more... */
1358 candidates[ i ].sr_type = REP_RESULT;
1361 /* FIXME: response controls
1362 * are passed without checks */
1363 rs->sr_err = ldap_parse_intermediate( msc->msc_ld,
1365 (char **)&rs->sr_rspoid,
1369 if ( rs->sr_err != LDAP_SUCCESS ) {
1370 candidates[ i ].sr_type = REP_RESULT;
1371 ldap_msgfree( res );
1376 slap_send_ldap_intermediate( op, rs );
1378 if ( rs->sr_rspoid != NULL ) {
1379 ber_memfree( (char *)rs->sr_rspoid );
1380 rs->sr_rspoid = NULL;
1383 if ( rs->sr_rspdata != NULL ) {
1384 ber_bvfree( rs->sr_rspdata );
1385 rs->sr_rspdata = NULL;
1388 if ( rs->sr_ctrls != NULL ) {
1389 ldap_controls_free( rs->sr_ctrls );
1390 rs->sr_ctrls = NULL;
1393 } else if ( rc == LDAP_RES_SEARCH_RESULT ) {
1394 char buf[ SLAP_TEXT_BUFLEN ];
1395 char **references = NULL;
1396 LDAPControl **ctrls = NULL;
1398 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1399 /* don't retry any more... */
1400 candidates[ i ].sr_type = REP_RESULT;
1403 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1405 /* NOTE: ignores response controls
1406 * (and intermediate response controls
1407 * as well, except for those with search
1408 * references); this may not be correct,
1409 * but if they're not ignored then
1410 * back-meta would need to merge them
1411 * consistently (think of pagedResults...)
1413 /* FIXME: response controls? */
1414 rs->sr_err = ldap_parse_result( msc->msc_ld,
1416 &candidates[ i ].sr_err,
1417 (char **)&candidates[ i ].sr_matched,
1418 (char **)&candidates[ i ].sr_text,
1420 &ctrls /* &candidates[ i ].sr_ctrls (unused) */ ,
1422 if ( rs->sr_err != LDAP_SUCCESS ) {
1423 candidates[ i ].sr_err = rs->sr_err;
1424 sres = slap_map_api2result( &candidates[ i ] );
1425 candidates[ i ].sr_type = REP_RESULT;
1426 ldap_msgfree( res );
1431 rs->sr_err = candidates[ i ].sr_err;
1433 /* massage matchedDN if need be */
1434 if ( candidates[ i ].sr_matched != NULL ) {
1435 struct berval match, mmatch;
1437 ber_str2bv( candidates[ i ].sr_matched,
1439 candidates[ i ].sr_matched = NULL;
1441 dc.ctx = "matchedDN";
1442 dc.target = mi->mi_targets[ i ];
1443 if ( !ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
1444 if ( mmatch.bv_val == match.bv_val ) {
1445 candidates[ i ].sr_matched
1446 = ch_strdup( mmatch.bv_val );
1449 candidates[ i ].sr_matched = mmatch.bv_val;
1454 ldap_memfree( match.bv_val );
1457 /* add references to array */
1458 /* RFC 4511: referrals can only appear
1459 * if result code is LDAP_REFERRAL */
1460 if ( references != NULL
1461 && references[ 0 ] != NULL
1462 && references[ 0 ][ 0 ] != '\0' )
1464 if ( rs->sr_err != LDAP_REFERRAL ) {
1465 Debug( LDAP_DEBUG_ANY,
1466 "%s meta_back_search[%ld]: "
1467 "got referrals with err=%d\n",
1475 for ( cnt = 0; references[ cnt ]; cnt++ )
1478 sr_ref = ber_memalloc_x( sizeof( struct berval ) * ( cnt + 1 ),
1481 for ( cnt = 0; references[ cnt ]; cnt++ ) {
1482 ber_str2bv_x( references[ cnt ], 0, 1, &sr_ref[ cnt ],
1485 BER_BVZERO( &sr_ref[ cnt ] );
1487 ( void )ldap_back_referral_result_rewrite( &dc, sr_ref,
1490 if ( rs->sr_v2ref == NULL ) {
1491 rs->sr_v2ref = sr_ref;
1494 for ( cnt = 0; !BER_BVISNULL( &sr_ref[ cnt ] ); cnt++ ) {
1495 ber_bvarray_add_x( &rs->sr_v2ref, &sr_ref[ cnt ],
1498 ber_memfree_x( sr_ref, op->o_tmpmemctx );
1502 } else if ( rs->sr_err == LDAP_REFERRAL ) {
1503 Debug( LDAP_DEBUG_ANY,
1504 "%s meta_back_search[%ld]: "
1505 "got err=%d with null "
1506 "or empty referrals\n",
1510 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1514 ber_memvfree( (void **)references );
1516 sres = slap_map_api2result( rs );
1518 if ( LogTest( LDAP_DEBUG_TRACE | LDAP_DEBUG_ANY ) ) {
1519 snprintf( buf, sizeof( buf ),
1520 "%s meta_back_search[%ld] "
1521 "match=\"%s\" err=%ld",
1522 op->o_log_prefix, i,
1523 candidates[ i ].sr_matched ? candidates[ i ].sr_matched : "",
1524 (long) candidates[ i ].sr_err );
1525 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
1526 Debug( LDAP_DEBUG_TRACE, "%s.\n", buf, 0, 0 );
1529 Debug( LDAP_DEBUG_ANY, "%s (%s).\n",
1530 buf, ldap_err2string( candidates[ i ].sr_err ), 0 );
1535 case LDAP_NO_SUCH_OBJECT:
1536 /* is_ok is touched any time a valid
1537 * (even intermediate) result is
1538 * returned; as a consequence, if
1539 * a candidate returns noSuchObject
1540 * it is ignored and the candidate
1541 * is simply demoted. */
1543 sres = LDAP_SUCCESS;
1548 if ( ctrls != NULL && ctrls[0] != NULL ) {
1549 #ifdef SLAPD_META_CLIENT_PR
1552 pr_c = ldap_control_find( LDAP_CONTROL_PAGEDRESULTS, ctrls, NULL );
1553 if ( pr_c != NULL ) {
1554 BerElementBuffer berbuf;
1555 BerElement *ber = (BerElement *)&berbuf;
1558 struct berval prcookie;
1560 /* unsolicited, do not accept */
1561 if ( mi->mi_targets[i]->mt_ps == 0 ) {
1562 rs->sr_err = LDAP_OTHER;
1566 ber_init2( ber, &pr_c->ldctl_value, LBER_USE_DER );
1568 tag = ber_scanf( ber, "{im}", &prsize, &prcookie );
1569 if ( tag == LBER_ERROR ) {
1570 rs->sr_err = LDAP_OTHER;
1574 /* more pages? new search request */
1575 if ( !BER_BVISNULL( &prcookie ) && !BER_BVISEMPTY( &prcookie ) ) {
1576 if ( mi->mi_targets[i]->mt_ps > 0 ) {
1577 /* ignore size if specified */
1580 } else if ( prsize == 0 ) {
1581 /* guess the page size from the entries returned so far */
1582 prsize = candidates[ i ].sr_nentries;
1585 candidates[ i ].sr_nentries = 0;
1586 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1587 candidates[ i ].sr_type = REP_INTERMEDIATE;
1589 assert( candidates[ i ].sr_matched == NULL );
1590 assert( candidates[ i ].sr_text == NULL );
1591 assert( candidates[ i ].sr_ref == NULL );
1593 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates, &prcookie, prsize ) )
1595 case META_SEARCH_CANDIDATE:
1596 assert( candidates[ i ].sr_msgid >= 0 );
1597 ldap_controls_free( ctrls );
1600 case META_SEARCH_ERR:
1602 candidates[ i ].sr_err = rs->sr_err;
1603 if ( META_BACK_ONERR_STOP( mi ) ) {
1604 savepriv = op->o_private;
1605 op->o_private = (void *)i;
1606 send_ldap_result( op, rs );
1607 op->o_private = savepriv;
1608 ldap_controls_free( ctrls );
1613 case META_SEARCH_NOT_CANDIDATE:
1614 /* means that meta_back_search_start()
1615 * failed but onerr == continue */
1616 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1617 assert( ncandidates > 0 );
1629 #endif /* SLAPD_META_CLIENT_PR */
1631 ldap_controls_free( ctrls );
1639 case LDAP_SIZELIMIT_EXCEEDED:
1640 /* if a target returned sizelimitExceeded
1641 * and the entry count is equal to the
1642 * proxy's limit, the target would have
1643 * returned more, and the error must be
1644 * propagated to the client; otherwise,
1645 * the target enforced a limit lower
1646 * than what requested by the proxy;
1648 candidates[ i ].sr_err = rs->sr_err;
1649 if ( rs->sr_nentries == op->ors_slimit
1650 || META_BACK_ONERR_STOP( mi ) )
1652 const char *save_text = rs->sr_text;
1653 savepriv = op->o_private;
1654 op->o_private = (void *)i;
1655 rs->sr_text = candidates[ i ].sr_text;
1656 send_ldap_result( op, rs );
1657 rs->sr_text = save_text;
1658 op->o_private = savepriv;
1659 ldap_msgfree( res );
1666 candidates[ i ].sr_err = rs->sr_err;
1667 if ( META_BACK_ONERR_STOP( mi ) ) {
1668 const char *save_text = rs->sr_text;
1669 savepriv = op->o_private;
1670 op->o_private = (void *)i;
1671 rs->sr_text = candidates[ i ].sr_text;
1672 send_ldap_result( op, rs );
1673 rs->sr_text = save_text;
1674 op->o_private = savepriv;
1675 ldap_msgfree( res );
1686 * When no candidates are left,
1687 * the outer cycle finishes
1689 assert( ncandidates > 0 );
1692 } else if ( rc == LDAP_RES_BIND ) {
1693 meta_search_candidate_t retcode;
1695 retcode = meta_search_dobind_result( op, rs, &mc, i, candidates, msg );
1696 if ( retcode == META_SEARCH_CANDIDATE ) {
1697 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1698 retcode = meta_back_search_start( op, rs, &dc, &mc, i, candidates, NULL, 0 );
1701 switch ( retcode ) {
1702 case META_SEARCH_CANDIDATE:
1705 /* means that failed but onerr == continue */
1706 case META_SEARCH_NOT_CANDIDATE:
1707 case META_SEARCH_ERR:
1708 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1709 assert( ncandidates > 0 );
1712 candidates[ i ].sr_err = rs->sr_err;
1713 if ( META_BACK_ONERR_STOP( mi ) ) {
1714 savepriv = op->o_private;
1715 op->o_private = (void *)i;
1716 send_ldap_result( op, rs );
1717 op->o_private = savepriv;
1718 ldap_msgfree( res );
1730 Debug( LDAP_DEBUG_ANY,
1731 "%s meta_back_search[%ld]: "
1732 "unrecognized response message tag=%d\n",
1736 ldap_msgfree( res );
1743 ldap_msgfree( res );
1747 /* check for abandon */
1748 if ( op->o_abandon || LDAP_BACK_CONN_ABANDON( mc ) ) {
1749 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1750 if ( candidates[ i ].sr_msgid >= 0
1751 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1753 if ( META_IS_BINDING( &candidates[ i ] )
1754 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1756 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1757 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] )
1758 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1760 /* if still binding, destroy */
1763 char buf[ SLAP_TEXT_BUFLEN ];
1765 snprintf( buf, sizeof( buf), "%s meta_back_search(abandon) "
1766 "ldap_unbind_ext[%ld] mc=%p ld=%p",
1767 op->o_log_prefix, i, (void *)mc,
1768 (void *)mc->mc_conns[i].msc_ld );
1770 Debug( LDAP_DEBUG_ANY, "### %s\n", buf, 0, 0 );
1771 #endif /* DEBUG_205 */
1773 meta_clear_one_candidate( op, mc, i );
1775 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1776 META_BINDING_CLEAR( &candidates[ i ] );
1779 (void)meta_back_cancel( mc, op, rs,
1780 candidates[ i ].sr_msgid, i,
1781 LDAP_BACK_DONTSEND );
1784 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1785 assert( ncandidates > 0 );
1790 if ( op->o_abandon ) {
1794 /* let send_ldap_result play cleanup handlers (ITS#4645) */
1798 /* if no entry was found during this loop,
1799 * set a minimal timeout */
1800 if ( ncandidates > 0 && gotit == 0 ) {
1801 if ( save_tv.tv_sec == 0 && save_tv.tv_usec == 0 ) {
1802 save_tv.tv_usec = LDAP_BACK_RESULT_UTIMEOUT/initial_candidates;
1804 /* arbitrarily limit to something between 1 and 2 minutes */
1805 } else if ( ( stoptime == -1 && save_tv.tv_sec < 60 )
1806 || save_tv.tv_sec < ( stoptime - slap_get_time() ) / ( 2 * ncandidates ) )
1808 /* double the timeout */
1809 lutil_timermul( &save_tv, 2, &save_tv );
1812 if ( alreadybound == 0 ) {
1814 (void)select( 0, NULL, NULL, NULL, &tv );
1817 ldap_pvt_thread_yield();
1824 * FIXME: need a better strategy to handle errors
1827 rc = meta_back_op_result( mc, op, rs, META_TARGET_NONE,
1828 -1, stoptime != -1 ? (stoptime - slap_get_time()) : 0,
1829 LDAP_BACK_SENDERR );
1837 * Rewrite the matched portion of the search base, if required
1839 * FIXME: only the last one gets caught!
1841 savepriv = op->o_private;
1842 op->o_private = (void *)(long)mi->mi_ntargets;
1843 if ( candidate_match > 0 ) {
1844 struct berval pmatched = BER_BVNULL;
1846 /* we use the first one */
1847 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1848 if ( META_IS_CANDIDATE( &candidates[ i ] )
1849 && candidates[ i ].sr_matched != NULL )
1851 struct berval bv, pbv;
1854 /* if we got success, and this target
1855 * returned noSuchObject, and its suffix
1856 * is a superior of the searchBase,
1857 * ignore the matchedDN */
1858 if ( sres == LDAP_SUCCESS
1859 && candidates[ i ].sr_err == LDAP_NO_SUCH_OBJECT
1860 && op->o_req_ndn.bv_len > mi->mi_targets[ i ]->mt_nsuffix.bv_len )
1862 free( (char *)candidates[ i ].sr_matched );
1863 candidates[ i ].sr_matched = NULL;
1867 ber_str2bv( candidates[ i ].sr_matched, 0, 0, &bv );
1868 rc = dnPretty( NULL, &bv, &pbv, op->o_tmpmemctx );
1870 if ( rc == LDAP_SUCCESS ) {
1872 /* NOTE: if they all are superiors
1873 * of the baseDN, the shorter is also
1874 * superior of the longer... */
1875 if ( pbv.bv_len > pmatched.bv_len ) {
1876 if ( !BER_BVISNULL( &pmatched ) ) {
1877 op->o_tmpfree( pmatched.bv_val, op->o_tmpmemctx );
1880 op->o_private = (void *)i;
1883 op->o_tmpfree( pbv.bv_val, op->o_tmpmemctx );
1887 if ( candidates[ i ].sr_matched != NULL ) {
1888 free( (char *)candidates[ i ].sr_matched );
1889 candidates[ i ].sr_matched = NULL;
1894 if ( !BER_BVISNULL( &pmatched ) ) {
1895 matched = pmatched.bv_val;
1898 } else if ( sres == LDAP_NO_SUCH_OBJECT ) {
1899 matched = op->o_bd->be_suffix[ 0 ].bv_val;
1903 * In case we returned at least one entry, we return LDAP_SUCCESS
1904 * otherwise, the latter error code we got
1907 if ( sres == LDAP_SUCCESS ) {
1908 if ( rs->sr_v2ref ) {
1909 sres = LDAP_REFERRAL;
1912 if ( META_BACK_ONERR_REPORT( mi ) ) {
1914 * Report errors, if any
1916 * FIXME: we should handle error codes and return the more
1917 * important/reasonable
1919 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1920 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1924 if ( candidates[ i ].sr_err != LDAP_SUCCESS
1925 && candidates[ i ].sr_err != LDAP_NO_SUCH_OBJECT )
1927 sres = candidates[ i ].sr_err;
1935 rs->sr_matched = ( sres == LDAP_SUCCESS ? NULL : matched );
1936 rs->sr_ref = ( sres == LDAP_REFERRAL ? rs->sr_v2ref : NULL );
1937 send_ldap_result( op, rs );
1938 op->o_private = savepriv;
1939 rs->sr_matched = NULL;
1943 if ( matched && matched != op->o_bd->be_suffix[ 0 ].bv_val ) {
1944 op->o_tmpfree( matched, op->o_tmpmemctx );
1947 if ( rs->sr_v2ref ) {
1948 ber_bvarray_free_x( rs->sr_v2ref, op->o_tmpmemctx );
1951 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1952 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1957 if ( META_IS_BINDING( &candidates[ i ] )
1958 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1960 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1961 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] )
1962 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1964 assert( candidates[ i ].sr_msgid >= 0
1965 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING );
1966 assert( mc->mc_conns[ i ].msc_ld != NULL );
1969 Debug( LDAP_DEBUG_ANY, "### %s meta_back_search(cleanup) "
1970 "ldap_unbind_ext[%ld] ld=%p\n",
1971 op->o_log_prefix, i, (void *)mc->mc_conns[i].msc_ld );
1972 #endif /* DEBUG_205 */
1974 /* if still binding, destroy */
1975 meta_clear_one_candidate( op, mc, i );
1977 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1978 META_BINDING_CLEAR( &candidates[ i ] );
1980 } else if ( candidates[ i ].sr_msgid >= 0 ) {
1981 (void)meta_back_cancel( mc, op, rs,
1982 candidates[ i ].sr_msgid, i,
1983 LDAP_BACK_DONTSEND );
1987 if ( candidates[ i ].sr_matched ) {
1988 free( (char *)candidates[ i ].sr_matched );
1989 candidates[ i ].sr_matched = NULL;
1992 if ( candidates[ i ].sr_text ) {
1993 ldap_memfree( (char *)candidates[ i ].sr_text );
1994 candidates[ i ].sr_text = NULL;
1997 if ( candidates[ i ].sr_ref ) {
1998 ber_bvarray_free( candidates[ i ].sr_ref );
1999 candidates[ i ].sr_ref = NULL;
2002 if ( candidates[ i ].sr_ctrls ) {
2003 ldap_controls_free( candidates[ i ].sr_ctrls );
2004 candidates[ i ].sr_ctrls = NULL;
2007 if ( META_BACK_TGT_QUARANTINE( mi->mi_targets[ i ] ) ) {
2008 meta_back_quarantine( op, &candidates[ i ], i );
2011 /* only in case of timelimit exceeded, if the timelimit exceeded because
2012 * one contacted target never responded, invalidate the connection
2013 * NOTE: should we quarantine the target as well? right now, the connection
2014 * is invalidated; the next time it will be recreated and the target
2015 * will be quarantined if it cannot be contacted */
2016 if ( mi->mi_idle_timeout != 0
2017 && rs->sr_err == LDAP_TIMELIMIT_EXCEEDED
2018 && op->o_time > mc->mc_conns[ i ].msc_time )
2020 /* don't let anyone else use this expired connection */
2026 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
2028 LDAP_BACK_CONN_TAINTED_SET( mc );
2030 meta_back_release_conn_lock( mi, mc, 0 );
2031 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
2045 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
2046 struct berval a, mapped;
2047 int check_duplicate_attrs = 0;
2048 int check_sorted_attrs = 0;
2050 BerElement ber = *ldap_get_message_ber( e );
2051 Attribute *attr, **attrp;
2059 if ( ber_scanf( &ber, "l{", &len ) == LBER_ERROR ) {
2060 return LDAP_DECODING_ERROR;
2063 if ( ber_set_option( &ber, LBER_OPT_REMAINING_BYTES, &len ) != LBER_OPT_SUCCESS ) {
2067 if ( ber_scanf( &ber, "m{", &bdn ) == LBER_ERROR ) {
2068 return LDAP_DECODING_ERROR;
2072 * Rewrite the dn of the result, if needed
2074 dc.target = mi->mi_targets[ target ];
2075 dc.conn = op->o_conn;
2077 dc.ctx = "searchResult";
2079 rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &dn );
2080 if ( rs->sr_err != LDAP_SUCCESS) {
2085 * Note: this may fail if the target host(s) schema differs
2086 * from the one known to the meta, and a DN with unknown
2087 * attributes is returned.
2089 * FIXME: should we log anything, or delegate to dnNormalize?
2091 rc = dnPrettyNormal( NULL, &dn, &ent.e_name, &ent.e_nname,
2093 if ( dn.bv_val != bdn.bv_val ) {
2098 if ( rc != LDAP_SUCCESS ) {
2099 Debug( LDAP_DEBUG_ANY,
2100 "%s meta_send_entry(\"%s\"): "
2101 "invalid DN syntax\n",
2102 op->o_log_prefix, ent.e_name.bv_val, 0 );
2103 rc = LDAP_INVALID_DN_SYNTAX;
2110 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
2111 ( void )meta_dncache_update_entry( &mi->mi_cache,
2112 &ent.e_nname, target );
2115 attrp = &ent.e_attrs;
2117 dc.ctx = "searchAttrDN";
2118 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
2120 slap_syntax_validate_func *validate;
2121 slap_syntax_transform_func *pretty;
2123 if ( ber_pvt_ber_remaining( &ber ) < 0 ) {
2124 Debug( LDAP_DEBUG_ANY,
2125 "%s meta_send_entry(\"%s\"): "
2126 "unable to parse attr \"%s\".\n",
2127 op->o_log_prefix, ent.e_name.bv_val, a.bv_val );
2133 if ( ber_pvt_ber_remaining( &ber ) == 0 ) {
2137 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_at,
2138 &a, &mapped, BACKLDAP_REMAP );
2139 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
2140 ( void )ber_scanf( &ber, "x" /* [W] */ );
2143 if ( mapped.bv_val != a.bv_val ) {
2144 /* will need to check for duplicate attrs */
2145 check_duplicate_attrs++;
2147 attr = attr_alloc( NULL );
2148 if ( attr == NULL ) {
2152 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
2154 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text,
2155 SLAP_AD_PROXIED ) != LDAP_SUCCESS )
2157 char buf[ SLAP_TEXT_BUFLEN ];
2159 snprintf( buf, sizeof( buf ),
2160 "%s meta_send_entry(\"%s\"): "
2161 "slap_bv2undef_ad(%s): %s\n",
2162 op->o_log_prefix, ent.e_name.bv_val,
2163 mapped.bv_val, text );
2165 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
2166 ( void )ber_scanf( &ber, "x" /* [W] */ );
2172 if ( attr->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL )
2173 check_sorted_attrs = 1;
2175 /* no subschemaSubentry */
2176 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
2177 || attr->a_desc == slap_schema.si_ad_entryDN )
2181 * We eat target's subschemaSubentry because
2182 * a search for this value is likely not
2183 * to resolve to the appropriate backend;
2184 * later, the local subschemaSubentry is
2187 * We also eat entryDN because the frontend
2188 * will reattach it without checking if already
2191 ( void )ber_scanf( &ber, "x" /* [W] */ );
2196 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
2197 || attr->a_vals == NULL )
2199 attr->a_vals = (struct berval *)&slap_dummy_bv;
2202 for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); ++last )
2205 attr->a_numvals = last;
2207 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
2208 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
2210 if ( !validate && !pretty ) {
2215 if ( attr->a_desc == slap_schema.si_ad_objectClass
2216 || attr->a_desc == slap_schema.si_ad_structuralObjectClass )
2220 for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
2223 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_oc,
2224 bv, &mapped, BACKLDAP_REMAP );
2225 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0') {
2232 *bv = attr->a_vals[ last ];
2233 BER_BVZERO( &attr->a_vals[ last ] );
2236 } else if ( mapped.bv_val != bv->bv_val ) {
2239 for ( i = 0; !BER_BVISNULL( &attr->a_vals[ i ] ); i++ ) {
2240 if ( &attr->a_vals[ i ] == bv ) {
2244 if ( ber_bvstrcasecmp( &mapped, &attr->a_vals[ i ] ) == 0 ) {
2249 if ( !BER_BVISNULL( &attr->a_vals[ i ] ) ) {
2253 ber_bvreplace( bv, &mapped );
2255 } else if ( ( oc = oc_bvfind_undef( bv ) ) == NULL ) {
2259 ber_bvreplace( bv, &oc->soc_cname );
2263 * It is necessary to try to rewrite attributes with
2264 * dn syntax because they might be used in ACLs as
2265 * members of groups; since ACLs are applied to the
2266 * rewritten stuff, no dn-based subecj clause could
2267 * be used at the ldap backend side (see
2268 * http://www.OpenLDAP.org/faq/data/cache/452.html)
2269 * The problem can be overcome by moving the dn-based
2270 * ACLs to the target directory server, and letting
2271 * everything pass thru the ldap backend.
2276 if ( attr->a_desc->ad_type->sat_syntax ==
2277 slap_schema.si_syn_distinguishedName )
2279 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
2281 } else if ( attr->a_desc == slap_schema.si_ad_ref ) {
2282 ldap_back_referral_result_rewrite( &dc, attr->a_vals, NULL );
2286 for ( i = 0; i < last; i++ ) {
2291 rc = ordered_value_pretty( attr->a_desc,
2292 &attr->a_vals[i], &pval, NULL );
2295 rc = ordered_value_validate( attr->a_desc,
2296 &attr->a_vals[i], 0 );
2300 ber_memfree( attr->a_vals[i].bv_val );
2301 if ( --last == i ) {
2302 BER_BVZERO( &attr->a_vals[ i ] );
2305 attr->a_vals[i] = attr->a_vals[last];
2306 BER_BVZERO( &attr->a_vals[last] );
2312 ber_memfree( attr->a_vals[i].bv_val );
2313 attr->a_vals[i] = pval;
2317 if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
2323 if ( last && attr->a_desc->ad_type->sat_equality &&
2324 attr->a_desc->ad_type->sat_equality->smr_normalize )
2328 attr->a_nvals = ch_malloc( ( last + 1 ) * sizeof( struct berval ) );
2329 for ( i = 0; i<last; i++ ) {
2330 /* if normalizer fails, drop this value */
2331 if ( ordered_value_normalize(
2332 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2334 attr->a_desc->ad_type->sat_equality,
2335 &attr->a_vals[i], &attr->a_nvals[i],
2337 ber_memfree( attr->a_vals[i].bv_val );
2338 if ( --last == i ) {
2339 BER_BVZERO( &attr->a_vals[ i ] );
2342 attr->a_vals[i] = attr->a_vals[last];
2343 BER_BVZERO( &attr->a_vals[last] );
2347 BER_BVZERO( &attr->a_nvals[i] );
2354 attr->a_nvals = attr->a_vals;
2357 attr->a_numvals = last;
2359 attrp = &attr->a_next;
2363 /* only check if some mapping occurred */
2364 if ( check_duplicate_attrs ) {
2367 for ( ap = &ent.e_attrs; *ap != NULL; ap = &(*ap)->a_next ) {
2370 for ( tap = &(*ap)->a_next; *tap != NULL; ) {
2371 if ( (*tap)->a_desc == (*ap)->a_desc ) {
2373 Modification mod = { 0 };
2374 const char *text = NULL;
2375 char textbuf[ SLAP_TEXT_BUFLEN ];
2376 Attribute *next = (*tap)->a_next;
2378 BER_BVSTR( &e.e_name, "" );
2379 BER_BVSTR( &e.e_nname, "" );
2381 mod.sm_op = LDAP_MOD_ADD;
2382 mod.sm_desc = (*ap)->a_desc;
2383 mod.sm_type = mod.sm_desc->ad_cname;
2384 mod.sm_numvals = (*ap)->a_numvals;
2385 mod.sm_values = (*tap)->a_vals;
2386 if ( (*tap)->a_nvals != (*tap)->a_vals ) {
2387 mod.sm_nvalues = (*tap)->a_nvals;
2390 (void)modify_add_values( &e, &mod,
2392 &text, textbuf, sizeof( textbuf ) );
2394 /* should not insert new attrs! */
2395 assert( e.e_attrs == *ap );
2401 tap = &(*tap)->a_next;
2407 /* Check for sorted attributes */
2408 if ( check_sorted_attrs ) {
2409 for ( attr = ent.e_attrs; attr; attr = attr->a_next ) {
2410 if ( attr->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL ) {
2411 while ( attr->a_numvals > 1 ) {
2413 int rc = slap_sort_vals( (Modifications *)attr, &text, &i, op->o_tmpmemctx );
2414 if ( rc != LDAP_TYPE_OR_VALUE_EXISTS )
2417 /* Strip duplicate values */
2418 if ( attr->a_nvals != attr->a_vals )
2419 ber_memfree( attr->a_nvals[i].bv_val );
2420 ber_memfree( attr->a_vals[i].bv_val );
2422 if ( (unsigned)i < attr->a_numvals ) {
2423 attr->a_vals[i] = attr->a_vals[attr->a_numvals];
2424 if ( attr->a_nvals != attr->a_vals )
2425 attr->a_nvals[i] = attr->a_nvals[attr->a_numvals];
2427 BER_BVZERO(&attr->a_vals[attr->a_numvals]);
2428 if ( attr->a_nvals != attr->a_vals )
2429 BER_BVZERO(&attr->a_nvals[attr->a_numvals]);
2431 attr->a_flags |= SLAP_ATTR_SORTED_VALS;
2436 ldap_get_entry_controls( mc->mc_conns[target].msc_ld,
2438 rs->sr_entry = &ent;
2439 rs->sr_attrs = op->ors_attrs;
2440 rs->sr_operational_attrs = NULL;
2441 rs->sr_flags = mi->mi_targets[ target ]->mt_rep_flags;
2442 rs->sr_err = LDAP_SUCCESS;
2443 rc = send_search_entry( op, rs );
2445 case LDAP_UNAVAILABLE:
2451 rs->sr_entry = NULL;
2452 rs->sr_attrs = NULL;
2453 if ( rs->sr_ctrls != NULL ) {
2454 ldap_controls_free( rs->sr_ctrls );
2455 rs->sr_ctrls = NULL;
2457 if ( !BER_BVISNULL( &ent.e_name ) ) {
2458 free( ent.e_name.bv_val );
2459 BER_BVZERO( &ent.e_name );
2461 if ( !BER_BVISNULL( &ent.e_nname ) ) {
2462 free( ent.e_nname.bv_val );
2463 BER_BVZERO( &ent.e_nname );
2465 entry_clean( &ent );