2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2006 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
28 #include <ac/socket.h>
29 #include <ac/string.h>
34 #include "../back-ldap/back-ldap.h"
35 #include "back-meta.h"
38 meta_back_bind( Operation *op, SlapReply *rs )
40 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
41 metaconn_t *mc = NULL;
48 SlapReply *candidates = meta_back_candidates_get( op );
50 rs->sr_err = LDAP_SUCCESS;
52 Debug( LDAP_DEBUG_ARGS, "%s meta_back_bind: dn=\"%s\".\n",
53 op->o_log_prefix, op->o_req_dn.bv_val, 0 );
55 /* the test on the bind method should be superfluous */
56 if ( op->orb_method == LDAP_AUTH_SIMPLE
57 && be_isroot_dn( op->o_bd, &op->o_req_ndn ) )
59 if ( !be_isroot_pw( op ) ) {
60 rs->sr_err = LDAP_INVALID_CREDENTIALS;
62 send_ldap_result( op, rs );
66 if ( META_BACK_DEFER_ROOTDN_BIND( mi ) ) {
67 rs->sr_err = LDAP_SUCCESS;
69 /* frontend will return success */
76 /* we need meta_back_getconn() not send result even on error,
77 * because we want to intercept the error and make it
78 * invalidCredentials */
79 mc = meta_back_getconn( op, rs, NULL, LDAP_BACK_BIND_DONTSEND );
81 if ( LogTest( LDAP_DEBUG_ANY ) ) {
82 char buf[ SLAP_TEXT_BUFLEN ];
84 snprintf( buf, sizeof( buf ),
85 "meta_back_bind: no target "
86 "for dn \"%s\" (%d%s%s).",
87 op->o_req_dn.bv_val, rs->sr_err,
88 rs->sr_text ? ". " : "",
89 rs->sr_text ? rs->sr_text : "" );
90 Debug( LDAP_DEBUG_ANY,
92 op->o_log_prefix, buf, 0 );
95 /* FIXME: there might be cases where we don't want
96 * to map the error onto invalidCredentials */
97 switch ( rs->sr_err ) {
98 case LDAP_NO_SUCH_OBJECT:
99 case LDAP_UNWILLING_TO_PERFORM:
100 rs->sr_err = LDAP_INVALID_CREDENTIALS;
104 send_ldap_result( op, rs );
109 * Each target is scanned ...
111 mc->mc_authz_target = META_BOUND_NONE;
112 for ( i = 0; i < mi->mi_ntargets; i++ ) {
113 metatarget_t *mt = mi->mi_targets[ i ];
119 * Skip non-candidates
121 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
126 /* set rc to LDAP_SUCCESS only if at least
127 * one candidate has been tried */
131 } else if ( isroot == 0 ) {
133 * A bind operation is expected to have
134 * ONE CANDIDATE ONLY!
136 Debug( LDAP_DEBUG_ANY,
137 "### %s meta_back_bind: more than one"
138 " candidate selected...\n",
139 op->o_log_prefix, 0, 0 );
143 if ( BER_BVISNULL( &mt->mt_pseudorootdn ) )
145 metasingleconn_t *msc = &mc->mc_conns[ i ];
147 /* skip the target if no pseudorootdn is provided */
148 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
149 ch_free( msc->msc_bound_ndn.bv_val );
150 BER_BVZERO( &msc->msc_bound_ndn );
153 if ( LDAP_BACK_SAVECRED( mi ) &&
154 !BER_BVISNULL( &msc->msc_cred ) )
156 /* destroy sensitive data */
157 memset( msc->msc_cred.bv_val, 0,
158 msc->msc_cred.bv_len );
159 ch_free( msc->msc_cred.bv_val );
160 BER_BVZERO( &msc->msc_cred );
166 op2.o_req_dn = mt->mt_pseudorootdn;
167 op2.o_req_ndn = mt->mt_pseudorootdn;
168 op2.orb_cred = mt->mt_pseudorootpw;
169 op2.orb_method = LDAP_AUTH_SIMPLE;
174 lerr = meta_back_single_bind( &op2, rs, mc, i, massage );
176 if ( lerr != LDAP_SUCCESS ) {
177 rc = rs->sr_err = lerr;
178 /* FIXME: in some cases (e.g. unavailable)
179 * do not assume it's not candidate; rather
180 * mark this as an error to be eventually
181 * reported to client */
182 META_CANDIDATE_CLEAR( &candidates[ i ] );
187 /* must re-insert if local DN changed as result of bind */
188 if ( rc == LDAP_SUCCESS ) {
190 mc->mc_authz_target = META_BOUND_ALL;
191 ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ) );
194 if ( !dn_match( &op->o_req_ndn, &mc->mc_local_ndn ) ) {
198 /* wait for all other ops to release the connection */
200 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
201 if ( mc->mc_refcnt > 1 ) {
202 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
203 ldap_pvt_thread_yield();
207 assert( mc->mc_refcnt == 1 );
208 tmpmc = avl_delete( &mi->mi_conninfo.lai_tree, (caddr_t)mc,
209 meta_back_conndn_cmp );
210 assert( tmpmc == mc );
212 /* delete all cached connections with the current connection */
213 if ( LDAP_BACK_SINGLECONN( mi ) ) {
214 while ( ( tmpmc = avl_delete( &mi->mi_conninfo.lai_tree, (caddr_t)mc, meta_back_conn_cmp ) ) != NULL )
216 Debug( LDAP_DEBUG_TRACE,
217 "=>meta_back_bind: destroying conn %ld (refcnt=%u)\n",
218 LDAP_BACK_PCONN_ID( mc->mc_conn ), mc->mc_refcnt, 0 );
220 if ( mc->mc_refcnt != 0 ) {
222 LDAP_BACK_CONN_TAINTED_SET( tmpmc );
226 * Needs a test because the handler may be corrupted,
227 * and calling ldap_unbind on a corrupted header results
228 * in a segmentation fault
230 meta_back_conn_free( tmpmc );
235 ber_bvreplace( &mc->mc_local_ndn, &op->o_req_ndn );
236 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
237 mc->mc_conn = LDAP_BACK_PCONN_SET( op );
239 lerr = avl_insert( &mi->mi_conninfo.lai_tree, (caddr_t)mc,
240 meta_back_conndn_cmp, meta_back_conndn_dup );
241 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
243 meta_clear_candidates( op, mc );
245 /* we can do this because mc_refcnt == 1 */
247 meta_back_conn_free( mc );
254 meta_back_release_conn( op, mc );
258 * rc is LDAP_SUCCESS if at least one bind succeeded,
259 * err is the last error that occurred during a bind;
260 * if at least (and at most?) one bind succeeds, fine.
262 if ( rc != LDAP_SUCCESS ) {
265 * deal with bind failure ...
269 * no target was found within the naming context,
270 * so bind must fail with invalid credentials
272 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
273 rs->sr_err = LDAP_INVALID_CREDENTIALS;
275 rs->sr_err = slap_map_api2result( rs );
277 send_ldap_result( op, rs );
286 * meta_back_single_bind
288 * attempts to perform a bind with creds
291 meta_back_single_bind(
298 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
299 metatarget_t *mt = mi->mi_targets[ candidate ];
300 struct berval mdn = BER_BVNULL;
301 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
306 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
307 ch_free( msc->msc_bound_ndn.bv_val );
308 BER_BVZERO( &msc->msc_bound_ndn );
311 if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &msc->msc_cred ) ) {
312 /* destroy sensitive data */
313 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
314 ch_free( msc->msc_cred.bv_val );
315 BER_BVZERO( &msc->msc_cred );
319 * Rewrite the bind dn if needed
325 dc.conn = op->o_conn;
329 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
330 rs->sr_text = "DN rewrite error";
331 rs->sr_err = LDAP_OTHER;
339 /* FIXME: this fixes the bind problem right now; we need
340 * to use the asynchronous version to get the "matched"
341 * and more in case of failure ... */
342 /* FIXME: should we check if at least some of the op->o_ctrls
343 * can/should be passed? */
345 rs->sr_err = ldap_sasl_bind( msc->msc_ld, mdn.bv_val,
346 LDAP_SASL_SIMPLE, &op->orb_cred,
347 op->o_ctrls, NULL, &msgid );
348 if ( rs->sr_err == LDAP_SUCCESS ) {
352 int nretries = mt->mt_nretries;
353 char buf[ SLAP_TEXT_BUFLEN ];
355 LDAP_BACK_TV_SET( &tv );
361 switch ( ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
363 snprintf( buf, sizeof( buf ),
364 "ldap_result=0 nretries=%d%s",
365 nretries, rebinding ? " rebinding" : "" );
366 Debug( LDAP_DEBUG_ANY,
367 "%s meta_back_single_bind[%d]: %s.\n",
368 op->o_log_prefix, candidate, buf );
370 if ( nretries != META_RETRY_NEVER ) {
371 ldap_pvt_thread_yield();
372 if ( nretries > 0 ) {
375 tv = mt->mt_bind_timeout;
379 rs->sr_err = LDAP_BUSY;
381 (void)meta_back_cancel( mc, op, rs, msgid, candidate, LDAP_BACK_DONTSEND );
385 /* FIXME: some times the request times out
386 * while the other party is not willing to
387 * send a response any more. Give it a second
388 * chance with a freshly bound connection */
390 nretries = mt->mt_nretries;
394 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
398 (void)meta_back_cancel( mc, op, rs, msgid, candidate, LDAP_BACK_DONTSEND );
401 snprintf( buf, sizeof( buf ),
402 "err=%d (%s) nretries=%d",
403 rs->sr_err, ldap_err2string( rs->sr_err ), nretries );
404 Debug( LDAP_DEBUG_ANY,
405 "### %s meta_back_single_bind[%d]: %s.\n",
406 op->o_log_prefix, candidate, buf );
408 rc = slap_map_api2result( rs );
409 if ( rs->sr_err == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
410 rc = meta_back_retry( op, rs, &mc, candidate, LDAP_BACK_DONTSEND );
412 if ( nretries > 0 ) {
415 ldap_pvt_thread_yield();
423 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
424 NULL, NULL, NULL, NULL, 1 );
425 if ( rc != LDAP_SUCCESS ) {
432 if ( rs->sr_err != LDAP_SUCCESS ) {
433 rs->sr_err = slap_map_api2result( rs );
437 ber_bvreplace( &msc->msc_bound_ndn, &op->o_req_dn );
438 LDAP_BACK_CONN_ISBOUND_SET( msc );
439 mc->mc_authz_target = candidate;
441 if ( LDAP_BACK_SAVECRED( mi ) ) {
442 ber_bvreplace( &msc->msc_cred, &op->orb_cred );
443 ldap_set_rebind_proc( msc->msc_ld, mt->mt_rebind_f, msc );
446 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED
447 && !BER_BVISEMPTY( &op->o_req_ndn ) )
449 ( void )meta_dncache_update_entry( &mi->mi_cache,
450 &op->o_req_ndn, candidate );
454 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
458 if ( META_BACK_TGT_QUARANTINE( mt ) ) {
459 meta_back_quarantine( op, rs, candidate );
466 * meta_back_single_dobind
469 meta_back_single_dobind(
474 ldap_back_send_t sendok,
478 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
479 metatarget_t *mt = mi->mi_targets[ candidate ];
480 metaconn_t *mc = *mcp;
481 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
483 static struct berval cred = BER_BVC( "" );
486 save_nretries = nretries;
488 assert( !LDAP_BACK_CONN_ISBOUND( msc ) );
491 * meta_back_single_dobind() calls meta_back_single_bind()
494 if ( be_isroot( op ) && !BER_BVISNULL( &mt->mt_pseudorootdn ) )
498 op2.o_tag = LDAP_REQ_BIND;
499 op2.o_req_dn = mt->mt_pseudorootdn;
500 op2.o_req_ndn = mt->mt_pseudorootdn;
501 op2.orb_cred = mt->mt_pseudorootpw;
502 op2.orb_method = LDAP_AUTH_SIMPLE;
504 rc = meta_back_single_bind( &op2, rs, *mcp, candidate, 0 );
509 * Otherwise an anonymous bind is performed
510 * (note: if the target was already bound, the anonymous
511 * bind clears the previous bind).
513 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
514 ber_memfree( msc->msc_bound_ndn.bv_val );
515 BER_BVZERO( &msc->msc_bound_ndn );
518 if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &msc->msc_cred ) ) {
519 /* destroy sensitive data */
520 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
521 ber_memfree( msc->msc_cred.bv_val );
522 BER_BVZERO( &msc->msc_cred );
525 /* FIXME: should we check if at least some of the op->o_ctrls
526 * can/should be passed? */
528 rc = ldap_sasl_bind( msc->msc_ld, "", LDAP_SASL_SIMPLE, &cred,
529 NULL, NULL, &msgid );
530 if ( rc == LDAP_SUCCESS ) {
533 char buf[ SLAP_TEXT_BUFLEN ];
535 LDAP_BACK_TV_SET( &tv );
541 switch ( ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
543 snprintf( buf, sizeof( buf ),
544 "ldap_result=0 nretries=%d%s",
545 nretries, rebinding ? " rebinding" : "" );
546 Debug( LDAP_DEBUG_ANY,
547 "%s meta_back_single_dobind[%d]: %s.\n",
548 op->o_log_prefix, candidate, buf );
550 if ( nretries != META_RETRY_NEVER ) {
551 ldap_pvt_thread_yield();
552 if ( nretries > 0 ) {
555 tv = mt->mt_bind_timeout;
561 (void)meta_back_cancel( mc, op, rs, msgid, candidate, LDAP_BACK_DONTSEND );
565 /* FIXME: some times the request times out
566 * while the other party is not willing to
567 * send a response any more. Give it a second
568 * chance with a freshly bound connection */
570 nretries = save_nretries;
574 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
578 (void)meta_back_cancel( mc, op, rs, msgid, candidate, LDAP_BACK_DONTSEND );
581 snprintf( buf, sizeof( buf ),
582 "err=%d (%s) nretries=%d",
583 rs->sr_err, ldap_err2string( rs->sr_err ), nretries );
584 Debug( LDAP_DEBUG_ANY,
585 "### %s meta_back_single_dobind[%d]: %s.\n",
586 op->o_log_prefix, candidate, buf );
588 rc = slap_map_api2result( rs );
589 if ( rc == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
591 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
594 if ( mc->mc_refcnt == 1 ) {
595 meta_clear_one_candidate( msc );
596 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
598 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
600 /* mc here must be the regular mc,
601 * reset and ready for init */
602 rc = meta_back_init_one_conn( op, rs,
604 LDAP_BACK_CONN_ISPRIV( mc ),
605 LDAP_BACK_DONTSEND );
606 if ( rc == LDAP_SUCCESS ) {
607 LDAP_BACK_CONN_BINDING_SET( msc );
611 /* can't do anything about it */
612 rc = LDAP_UNAVAILABLE;
616 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
619 if ( rc == LDAP_SUCCESS ) {
620 ldap_pvt_thread_yield();
621 if ( nretries > 0 ) {
630 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
631 NULL, NULL, NULL, NULL, 1 );
632 if ( rc == LDAP_SUCCESS ) {
633 rc = slap_map_api2result( rs );
640 rc = slap_map_api2result( rs );
645 if ( rc != LDAP_SUCCESS ) {
647 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
649 LDAP_BACK_CONN_BINDING_CLEAR( msc );
650 if ( META_BACK_ONERR_STOP( mi ) ) {
651 LDAP_BACK_CONN_TAINTED_SET( mc );
652 meta_back_release_conn_lock( op, mc, 0 );
656 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
659 if ( META_BACK_ONERR_STOP( mi ) && ( sendok & LDAP_BACK_SENDERR ) ) {
660 send_ldap_result( op, rs );
664 if ( META_BACK_TGT_QUARANTINE( mt ) ) {
665 meta_back_quarantine( op, rs, candidate );
679 ldap_back_send_t sendok )
681 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
687 SlapReply *candidates = meta_back_candidates_get( op );
689 if ( be_isroot( op ) ) {
693 Debug( LDAP_DEBUG_TRACE,
694 "%s meta_back_dobind: conn=%ld%s\n",
696 LDAP_BACK_PCONN_ID( mc->mc_conn ),
697 isroot ? " (isroot)" : "" );
700 * all the targets are bound as pseudoroot
702 if ( mc->mc_authz_target == META_BOUND_ALL ) {
707 for ( i = 0; i < mi->mi_ntargets; i++ ) {
708 metatarget_t *mt = mi->mi_targets[ i ];
709 metasingleconn_t *msc = &mc->mc_conns[ i ];
710 int rc, do_retry = 1;
715 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
719 assert( msc->msc_ld != NULL );
722 * If the target is already bound it is skipped
726 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
727 if ( LDAP_BACK_CONN_ISBOUND( msc ) || LDAP_BACK_CONN_ISANON( msc ) ) {
728 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
732 } else if ( LDAP_BACK_CONN_BINDING( msc ) ) {
733 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
734 ldap_pvt_thread_yield();
739 LDAP_BACK_CONN_BINDING_SET( msc );
740 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
743 rc = meta_back_single_dobind( op, rs, &mc, i,
744 LDAP_BACK_DONTSEND, mt->mt_nretries, 1 );
746 * NOTE: meta_back_single_dobind() already retries;
747 * in case of failure, it resets mc...
749 if ( rc != LDAP_SUCCESS ) {
750 char buf[ SLAP_TEXT_BUFLEN ];
753 /* meta_back_single_dobind() already sent
754 * response and released connection */
759 if ( rc == LDAP_UNAVAILABLE && do_retry ) {
761 if ( meta_back_retry( op, rs, &mc, i, sendok ) ) {
766 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
767 LDAP_BACK_CONN_BINDING_CLEAR( msc );
768 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
774 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
775 LDAP_BACK_CONN_BINDING_CLEAR( msc );
776 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
778 snprintf( buf, sizeof( buf ),
779 "meta_back_dobind[%d]: (%s) err=%d (%s).",
780 i, isroot ? op->o_bd->be_rootdn.bv_val : "anonymous",
781 rc, ldap_err2string( rc ) );
782 Debug( LDAP_DEBUG_ANY,
784 op->o_log_prefix, buf, 0 );
787 * null cred bind should always succeed
788 * as anonymous, so a failure means
789 * the target is no longer candidate possibly
790 * due to technical reasons (remote host down?)
791 * so better clear the handle
793 /* leave the target candidate, but record the error for later use */
794 candidates[ i ].sr_err = rc;
795 if ( META_BACK_ONERR_STOP( mi ) ) {
803 Debug( LDAP_DEBUG_TRACE,
804 "%s meta_back_dobind[%d]: "
807 isroot ? op->o_bd->be_rootdn.bv_val : "anonymous" );
809 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
810 LDAP_BACK_CONN_BINDING_CLEAR( msc );
812 LDAP_BACK_CONN_ISBOUND_SET( msc );
814 LDAP_BACK_CONN_ISANON_SET( msc );
816 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
821 Debug( LDAP_DEBUG_TRACE,
822 "%s meta_back_dobind: conn=%ld bound=%d\n",
823 op->o_log_prefix, LDAP_BACK_PCONN_ID( mc->mc_conn ), bound );
826 meta_back_release_conn( op, mc );
829 if ( sendok & LDAP_BACK_SENDERR ) {
830 if ( rs->sr_err == LDAP_SUCCESS ) {
831 rs->sr_err = LDAP_BUSY;
833 send_ldap_result( op, rs );
839 return ( bound > 0 );
843 * meta_back_default_rebind
845 * This is a callback used for chasing referrals using the same
846 * credentials as the original user on this session.
849 meta_back_default_rebind(
851 LDAP_CONST char *url,
856 metasingleconn_t *msc = ( metasingleconn_t * )params;
858 return ldap_sasl_bind_s( ld, msc->msc_bound_ndn.bv_val,
859 LDAP_SASL_SIMPLE, &msc->msc_cred,
864 * meta_back_default_urllist
866 * This is a callback used for mucking with the urllist
869 meta_back_default_urllist(
871 LDAPURLDesc **urllist,
875 metatarget_t *mt = (metatarget_t *)params;
876 LDAPURLDesc **urltail;
878 if ( urllist == url ) {
882 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
889 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
891 ch_free( mt->mt_uri );
894 ldap_get_option( ld, LDAP_OPT_URI, (void *)&mt->mt_uri );
895 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
907 ldap_back_send_t sendok )
909 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
911 metatarget_t *mt = mi->mi_targets[ candidate ];
912 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
914 /* default behavior */
915 if ( META_BACK_TGT_ABANDON( mt ) ) {
916 return ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
919 if ( META_BACK_TGT_IGNORE( mt ) ) {
923 if ( META_BACK_TGT_CANCEL( mt ) ) {
924 /* FIXME: asynchronous? */
925 return ldap_cancel_s( msc->msc_ld, msgid, NULL, NULL );
936 * FIXME: error return must be handled in a cleaner way ...
945 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
951 const char *save_rmsg = NULL,
953 void *rmatch_ctx = NULL;
955 assert( mc != NULL );
957 if ( candidate != META_TARGET_NONE ) {
958 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
960 rs->sr_err = LDAP_SUCCESS;
962 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
963 if ( rs->sr_err != LDAP_SUCCESS ) {
965 * better check the type of error. In some cases
966 * (search ?) it might be better to return a
967 * success if at least one of the targets gave
968 * positive result ...
970 ldap_get_option( msc->msc_ld,
971 LDAP_OPT_ERROR_STRING, &rmsg );
972 if ( rmsg != NULL && rmsg[ 0 ] == '\0' ) {
973 ldap_memfree( rmsg );
977 ldap_get_option( msc->msc_ld,
978 LDAP_OPT_MATCHED_DN, &rmatch );
979 if ( rmatch != NULL && rmatch[ 0 ] == '\0' ) {
980 ldap_memfree( rmatch );
984 rerr = rs->sr_err = slap_map_api2result( rs );
986 Debug(LDAP_DEBUG_ANY,
987 "==> meta_back_op_result[%d] "
988 "text=\"%s\" matched=\"%s\"\n",
989 candidate, ( rmsg ? rmsg : "" ),
990 ( rmatch ? rmatch : "" ) );
993 if ( META_BACK_TGT_QUARANTINE( mi->mi_targets[ candidate ] ) ) {
994 meta_back_quarantine( op, rs, candidate );
998 for ( i = 0; i < mi->mi_ntargets; i++ ) {
999 metasingleconn_t *msc = &mc->mc_conns[ i ];
1003 rs->sr_err = LDAP_SUCCESS;
1005 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
1006 if ( rs->sr_err != LDAP_SUCCESS ) {
1008 * better check the type of error. In some cases
1009 * (search ?) it might be better to return a
1010 * success if at least one of the targets gave
1011 * positive result ...
1013 ldap_get_option( msc->msc_ld,
1014 LDAP_OPT_ERROR_STRING, &msg );
1015 if ( msg != NULL && msg[ 0 ] == '\0' ) {
1016 ldap_memfree( msg );
1020 ldap_get_option( msc->msc_ld,
1021 LDAP_OPT_MATCHED_DN, &match );
1022 if ( match != NULL && match[ 0 ] == '\0' ) {
1023 ldap_memfree( match );
1027 rs->sr_err = slap_map_api2result( rs );
1029 Debug(LDAP_DEBUG_ANY,
1030 "==> meta_back_op_result[%d] "
1031 "text=\"%s\" matched=\"%s\"\n",
1032 i, ( msg ? msg : "" ),
1033 ( match ? match : "" ) );
1036 * FIXME: need to rewrite "match" (need rwinfo)
1038 switch ( rs->sr_err ) {
1041 if ( msg != NULL ) {
1043 ldap_memfree( rmsg );
1048 if ( match != NULL ) {
1050 ldap_memfree( rmatch );
1059 ldap_memfree( msg );
1063 ldap_memfree( match );
1067 if ( META_BACK_TGT_QUARANTINE( mi->mi_targets[ i ] ) ) {
1068 meta_back_quarantine( op, rs, i );
1074 if ( rmsg != NULL ) {
1075 save_rmsg = rs->sr_text;
1078 if ( rmatch != NULL ) {
1079 struct berval dn, pdn;
1081 ber_str2bv( rmatch, 0, 0, &dn );
1082 if ( dnPretty( NULL, &dn, &pdn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
1083 ldap_memfree( rmatch );
1084 rmatch_ctx = op->o_tmpmemctx;
1085 rmatch = pdn.bv_val;
1087 save_rmatch = rs->sr_matched;
1088 rs->sr_matched = rmatch;
1090 send_ldap_result( op, rs );
1091 if ( rmsg != NULL ) {
1092 ber_memfree( rmsg );
1093 rs->sr_text = save_rmsg;
1095 if ( rmatch != NULL ) {
1096 ber_memfree_x( rmatch, rmatch_ctx );
1097 rs->sr_matched = save_rmatch;
1100 return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );