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 char buf[ SLAP_TEXT_BUFLEN ];
83 snprintf( buf, sizeof( buf ),
84 "meta_back_bind: no target "
85 "for dn \"%s\" (%d%s%s).",
86 op->o_req_dn.bv_val, rs->sr_err,
87 rs->sr_text ? ". " : "",
88 rs->sr_text ? rs->sr_text : "" );
89 Debug( LDAP_DEBUG_ANY,
91 op->o_log_prefix, buf, 0 );
93 /* FIXME: there might be cases where we don't want
94 * to map the error onto invalidCredentials */
95 switch ( rs->sr_err ) {
96 case LDAP_NO_SUCH_OBJECT:
97 case LDAP_UNWILLING_TO_PERFORM:
98 rs->sr_err = LDAP_INVALID_CREDENTIALS;
102 send_ldap_result( op, rs );
107 * Each target is scanned ...
109 mc->mc_authz_target = META_BOUND_NONE;
110 for ( i = 0; i < mi->mi_ntargets; i++ ) {
111 metatarget_t *mt = mi->mi_targets[ i ];
117 * Skip non-candidates
119 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
124 /* set rc to LDAP_SUCCESS only if at least
125 * one candidate has been tried */
129 } else if ( isroot == 0 ) {
131 * A bind operation is expected to have
132 * ONE CANDIDATE ONLY!
134 Debug( LDAP_DEBUG_ANY,
135 "### %s meta_back_bind: more than one"
136 " candidate selected...\n",
137 op->o_log_prefix, 0, 0 );
141 if ( BER_BVISNULL( &mt->mt_pseudorootdn ) )
143 metasingleconn_t *msc = &mc->mc_conns[ i ];
145 /* skip the target if no pseudorootdn is provided */
146 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
147 ch_free( msc->msc_bound_ndn.bv_val );
148 BER_BVZERO( &msc->msc_bound_ndn );
151 if ( LDAP_BACK_SAVECRED( mi ) &&
152 !BER_BVISNULL( &msc->msc_cred ) )
154 /* destroy sensitive data */
155 memset( msc->msc_cred.bv_val, 0,
156 msc->msc_cred.bv_len );
157 ch_free( msc->msc_cred.bv_val );
158 BER_BVZERO( &msc->msc_cred );
164 op2.o_req_dn = mt->mt_pseudorootdn;
165 op2.o_req_ndn = mt->mt_pseudorootdn;
166 op2.orb_cred = mt->mt_pseudorootpw;
167 op2.orb_method = LDAP_AUTH_SIMPLE;
172 lerr = meta_back_single_bind( &op2, rs, mc, i, massage );
174 if ( lerr != LDAP_SUCCESS ) {
175 rc = rs->sr_err = lerr;
176 /* FIXME: in some cases (e.g. unavailable)
177 * do not assume it's not candidate; rather
178 * mark this as an error to be eventually
179 * reported to client */
180 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
185 /* must re-insert if local DN changed as result of bind */
186 if ( rc == LDAP_SUCCESS ) {
188 mc->mc_authz_target = META_BOUND_ALL;
189 ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ) );
192 if ( !dn_match( &op->o_req_ndn, &mc->mc_local_ndn ) ) {
196 /* wait for all other ops to release the connection */
198 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
199 if ( mc->mc_refcnt > 1 ) {
200 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
201 ldap_pvt_thread_yield();
205 assert( mc->mc_refcnt == 1 );
206 tmpmc = avl_delete( &mi->mi_conninfo.lai_tree, (caddr_t)mc,
207 meta_back_conndn_cmp );
208 assert( tmpmc == mc );
210 /* delete all cached connections with the current connection */
211 if ( LDAP_BACK_SINGLECONN( mi ) ) {
212 while ( ( tmpmc = avl_delete( &mi->mi_conninfo.lai_tree, (caddr_t)mc, meta_back_conn_cmp ) ) != NULL )
214 Debug( LDAP_DEBUG_TRACE,
215 "=>meta_back_bind: destroying conn %ld (refcnt=%u)\n",
216 LDAP_BACK_PCONN_ID( mc->mc_conn ), mc->mc_refcnt, 0 );
218 if ( mc->mc_refcnt != 0 ) {
220 LDAP_BACK_CONN_TAINTED_SET( tmpmc );
224 * Needs a test because the handler may be corrupted,
225 * and calling ldap_unbind on a corrupted header results
226 * in a segmentation fault
228 meta_back_conn_free( tmpmc );
233 ber_bvreplace( &mc->mc_local_ndn, &op->o_req_ndn );
234 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
235 mc->mc_conn = LDAP_BACK_PCONN_SET( op );
237 lerr = avl_insert( &mi->mi_conninfo.lai_tree, (caddr_t)mc,
238 meta_back_conndn_cmp, meta_back_conndn_dup );
239 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
241 meta_clear_candidates( op, mc );
243 /* we can do this because mc_refcnt == 1 */
245 meta_back_conn_free( mc );
252 meta_back_release_conn( op, mc );
256 * rc is LDAP_SUCCESS if at least one bind succeeded,
257 * err is the last error that occurred during a bind;
258 * if at least (and at most?) one bind succeeds, fine.
260 if ( rc != LDAP_SUCCESS ) {
263 * deal with bind failure ...
267 * no target was found within the naming context,
268 * so bind must fail with invalid credentials
270 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
271 rs->sr_err = LDAP_INVALID_CREDENTIALS;
273 rs->sr_err = slap_map_api2result( rs );
275 send_ldap_result( op, rs );
284 * meta_back_single_bind
286 * attempts to perform a bind with creds
289 meta_back_single_bind(
296 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
297 metatarget_t *mt = mi->mi_targets[ candidate ];
298 struct berval mdn = BER_BVNULL;
299 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
304 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
305 ch_free( msc->msc_bound_ndn.bv_val );
306 BER_BVZERO( &msc->msc_bound_ndn );
309 if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &msc->msc_cred ) ) {
310 /* destroy sensitive data */
311 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
312 ch_free( msc->msc_cred.bv_val );
313 BER_BVZERO( &msc->msc_cred );
317 * Rewrite the bind dn if needed
323 dc.conn = op->o_conn;
327 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
328 rs->sr_text = "DN rewrite error";
329 rs->sr_err = LDAP_OTHER;
337 /* FIXME: this fixes the bind problem right now; we need
338 * to use the asynchronous version to get the "matched"
339 * and more in case of failure ... */
340 /* FIXME: should we check if at least some of the op->o_ctrls
341 * can/should be passed? */
343 rs->sr_err = ldap_sasl_bind( msc->msc_ld, mdn.bv_val,
344 LDAP_SASL_SIMPLE, &op->orb_cred,
345 op->o_ctrls, NULL, &msgid );
346 if ( rs->sr_err == LDAP_SUCCESS ) {
350 int nretries = mt->mt_nretries;
351 char buf[ SLAP_TEXT_BUFLEN ];
353 LDAP_BACK_TV_SET( &tv );
359 switch ( ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
361 snprintf( buf, sizeof( buf ),
362 "ldap_result=0 nretries=%d%s",
363 nretries, rebinding ? " rebinding" : "" );
364 Debug( LDAP_DEBUG_ANY,
365 "%s meta_back_single_bind[%d]: %s.\n",
366 op->o_log_prefix, candidate, buf );
368 if ( nretries != META_RETRY_NEVER ) {
369 ldap_pvt_thread_yield();
370 if ( nretries > 0 ) {
373 tv = mt->mt_bind_timeout;
377 rs->sr_err = LDAP_BUSY;
379 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
383 /* FIXME: some times the request times out
384 * while the other party is not willing to
385 * send a response any more. Give it a second
386 * chance with a freshly bound connection */
388 nretries = mt->mt_nretries;
392 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
396 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
399 snprintf( buf, sizeof( buf ),
400 "err=%d (%s) nretries=%d",
401 rs->sr_err, ldap_err2string( rs->sr_err ), nretries );
402 Debug( LDAP_DEBUG_ANY,
403 "### %s meta_back_single_bind[%d]: %s.\n",
404 op->o_log_prefix, candidate, buf );
406 rc = slap_map_api2result( rs );
407 if ( rs->sr_err == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
408 rc = meta_back_retry( op, rs, &mc, candidate, LDAP_BACK_DONTSEND );
410 if ( nretries > 0 ) {
413 ldap_pvt_thread_yield();
421 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
422 NULL, NULL, NULL, NULL, 1 );
423 if ( rc != LDAP_SUCCESS ) {
430 if ( rs->sr_err != LDAP_SUCCESS ) {
431 rs->sr_err = slap_map_api2result( rs );
435 ber_bvreplace( &msc->msc_bound_ndn, &op->o_req_dn );
436 LDAP_BACK_CONN_ISBOUND_SET( msc );
437 mc->mc_authz_target = candidate;
439 if ( LDAP_BACK_SAVECRED( mi ) ) {
440 ber_bvreplace( &msc->msc_cred, &op->orb_cred );
441 ldap_set_rebind_proc( msc->msc_ld, mt->mt_rebind_f, msc );
444 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED
445 && op->o_req_ndn.bv_len != 0 )
447 ( void )meta_dncache_update_entry( &mi->mi_cache,
448 &op->o_req_ndn, candidate );
452 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
460 * meta_back_single_dobind
463 meta_back_single_dobind(
468 ldap_back_send_t sendok,
472 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
473 metatarget_t *mt = mi->mi_targets[ candidate ];
474 metaconn_t *mc = *mcp;
475 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
477 static struct berval cred = BER_BVC( "" );
480 save_nretries = nretries;
482 assert( !LDAP_BACK_CONN_ISBOUND( msc ) );
485 * meta_back_single_dobind() calls meta_back_single_bind()
488 if ( be_isroot( op ) && !BER_BVISNULL( &mt->mt_pseudorootdn ) )
492 op2.o_tag = LDAP_REQ_BIND;
493 op2.o_req_dn = mt->mt_pseudorootdn;
494 op2.o_req_ndn = mt->mt_pseudorootdn;
495 op2.orb_cred = mt->mt_pseudorootpw;
496 op2.orb_method = LDAP_AUTH_SIMPLE;
498 rc = meta_back_single_bind( &op2, rs, *mcp, candidate, 0 );
503 * Otherwise an anonymous bind is performed
504 * (note: if the target was already bound, the anonymous
505 * bind clears the previous bind).
507 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
508 ber_memfree( msc->msc_bound_ndn.bv_val );
509 BER_BVZERO( &msc->msc_bound_ndn );
512 if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &msc->msc_cred ) ) {
513 /* destroy sensitive data */
514 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
515 ber_memfree( msc->msc_cred.bv_val );
516 BER_BVZERO( &msc->msc_cred );
519 /* FIXME: should we check if at least some of the op->o_ctrls
520 * can/should be passed? */
522 rc = ldap_sasl_bind( msc->msc_ld, "", LDAP_SASL_SIMPLE, &cred,
523 NULL, NULL, &msgid );
524 if ( rc == LDAP_SUCCESS ) {
527 char buf[ SLAP_TEXT_BUFLEN ];
529 LDAP_BACK_TV_SET( &tv );
535 switch ( ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
537 snprintf( buf, sizeof( buf ),
538 "ldap_result=0 nretries=%d%s",
539 nretries, rebinding ? " rebinding" : "" );
540 Debug( LDAP_DEBUG_ANY,
541 "%s meta_back_single_dobind[%d]: %s.\n",
542 op->o_log_prefix, candidate, buf );
544 if ( nretries != META_RETRY_NEVER ) {
545 ldap_pvt_thread_yield();
546 if ( nretries > 0 ) {
549 tv = mt->mt_bind_timeout;
555 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
559 /* FIXME: some times the request times out
560 * while the other party is not willing to
561 * send a response any more. Give it a second
562 * chance with a freshly bound connection */
564 nretries = save_nretries;
568 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
572 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
575 snprintf( buf, sizeof( buf ),
576 "err=%d (%s) nretries=%d",
577 rs->sr_err, ldap_err2string( rs->sr_err ), nretries );
578 Debug( LDAP_DEBUG_ANY,
579 "### %s meta_back_single_dobind[%d]: %s.\n",
580 op->o_log_prefix, candidate, buf );
582 rc = slap_map_api2result( rs );
583 if ( rc == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
585 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
588 if ( mc->mc_refcnt == 1 ) {
589 meta_clear_one_candidate( msc );
590 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
592 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
594 /* mc here must be the regular mc,
595 * reset and ready for init */
596 rc = meta_back_init_one_conn( op, rs,
598 LDAP_BACK_CONN_ISPRIV( mc ),
599 LDAP_BACK_DONTSEND );
600 if ( rc == LDAP_SUCCESS ) {
601 LDAP_BACK_CONN_BINDING_SET( msc );
605 /* can't do anything about it */
606 rc = LDAP_UNAVAILABLE;
610 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
613 if ( rc == LDAP_SUCCESS ) {
614 ldap_pvt_thread_yield();
615 if ( nretries > 0 ) {
624 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
625 NULL, NULL, NULL, NULL, 1 );
626 if ( rc == LDAP_SUCCESS ) {
627 rc = slap_map_api2result( rs );
634 rc = slap_map_api2result( rs );
639 if ( rc != LDAP_SUCCESS && META_BACK_ONERR_STOP( mi ) ) {
640 LDAP_BACK_CONN_BINDING_CLEAR( msc );
641 LDAP_BACK_CONN_TAINTED_SET( mc );
642 meta_back_release_conn_lock( op, mc, dolock );
645 if ( sendok & LDAP_BACK_SENDERR ) {
646 send_ldap_result( op, rs );
661 ldap_back_send_t sendok )
663 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
669 SlapReply *candidates = meta_back_candidates_get( op );
671 if ( be_isroot( op ) ) {
675 Debug( LDAP_DEBUG_TRACE,
676 "%s meta_back_dobind: conn=%ld%s\n",
678 LDAP_BACK_PCONN_ID( mc->mc_conn ),
679 isroot ? " (isroot)" : "" );
682 * all the targets are bound as pseudoroot
684 if ( mc->mc_authz_target == META_BOUND_ALL ) {
689 for ( i = 0; i < mi->mi_ntargets; i++ ) {
690 metatarget_t *mt = mi->mi_targets[ i ];
691 metasingleconn_t *msc = &mc->mc_conns[ i ];
692 int rc, do_retry = 1;
697 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
701 assert( msc->msc_ld != NULL );
704 * If the target is already bound it is skipped
708 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
709 if ( LDAP_BACK_CONN_ISBOUND( msc ) || LDAP_BACK_CONN_ISANON( msc ) ) {
710 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
714 } else if ( LDAP_BACK_CONN_BINDING( msc ) ) {
715 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
716 ldap_pvt_thread_yield();
720 LDAP_BACK_CONN_BINDING_SET( msc );
721 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
725 rc = meta_back_single_dobind( op, rs, &mc, i,
726 LDAP_BACK_DONTSEND, mt->mt_nretries, 1 );
728 * NOTE: meta_back_single_dobind() already retries;
729 * in case of failure, it resets mc...
731 if ( rc != LDAP_SUCCESS ) {
732 char buf[ SLAP_TEXT_BUFLEN ];
735 /* meta_back_single_dobind() already sent
736 * response and released connection */
741 if ( rc == LDAP_UNAVAILABLE && do_retry ) {
743 if ( meta_back_retry( op, rs, &mc, i, sendok ) ) {
748 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
749 LDAP_BACK_CONN_BINDING_CLEAR( msc );
750 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
756 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
757 LDAP_BACK_CONN_BINDING_CLEAR( msc );
758 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
760 snprintf( buf, sizeof( buf ),
761 "meta_back_dobind[%d]: (%s) err=%d (%s).",
762 i, isroot ? op->o_bd->be_rootdn.bv_val : "anonymous",
763 rc, ldap_err2string( rc ) );
764 Debug( LDAP_DEBUG_ANY,
766 op->o_log_prefix, buf, 0 );
769 * null cred bind should always succeed
770 * as anonymous, so a failure means
771 * the target is no longer candidate possibly
772 * due to technical reasons (remote host down?)
773 * so better clear the handle
775 /* leave the target candidate, but record the error for later use */
776 candidates[ i ].sr_err = rc;
777 if ( META_BACK_ONERR_STOP( mi ) ) {
785 Debug( LDAP_DEBUG_TRACE,
786 "%s meta_back_dobind[%d]: "
789 isroot ? op->o_bd->be_rootdn.bv_val : "anonymous" );
791 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
792 LDAP_BACK_CONN_BINDING_CLEAR( msc );
794 LDAP_BACK_CONN_ISBOUND_SET( msc );
796 LDAP_BACK_CONN_ISANON_SET( msc );
798 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
803 Debug( LDAP_DEBUG_TRACE,
804 "%s meta_back_dobind: conn=%ld bound=%d\n",
805 op->o_log_prefix, LDAP_BACK_PCONN_ID( mc->mc_conn ), bound );
808 meta_back_release_conn( op, mc );
811 if ( sendok & LDAP_BACK_SENDERR ) {
812 if ( rs->sr_err == LDAP_SUCCESS ) {
813 rs->sr_err = LDAP_BUSY;
815 send_ldap_result( op, rs );
821 return ( bound > 0 );
825 * meta_back_default_rebind
827 * This is a callback used for chasing referrals using the same
828 * credentials as the original user on this session.
831 meta_back_default_rebind(
833 LDAP_CONST char *url,
838 metasingleconn_t *msc = ( metasingleconn_t * )params;
840 return ldap_sasl_bind_s( ld, msc->msc_bound_ndn.bv_val,
841 LDAP_SASL_SIMPLE, &msc->msc_cred,
846 * meta_back_default_urllist
848 * This is a callback used for mucking with the urllist
851 meta_back_default_urllist(
853 LDAPURLDesc **urllist,
857 metatarget_t *mt = (metatarget_t *)params;
858 LDAPURLDesc **urltail;
860 if ( urllist == url ) {
864 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
871 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
873 ch_free( mt->mt_uri );
876 ldap_get_option( ld, LDAP_OPT_URI, (void *)&mt->mt_uri );
877 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
883 * FIXME: error return must be handled in a cleaner way ...
892 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
898 const char *save_rmsg = NULL,
900 void *rmatch_ctx = NULL;
902 assert( mc != NULL );
904 if ( candidate != META_TARGET_NONE ) {
905 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
907 rs->sr_err = LDAP_SUCCESS;
909 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
910 if ( rs->sr_err != LDAP_SUCCESS ) {
912 * better check the type of error. In some cases
913 * (search ?) it might be better to return a
914 * success if at least one of the targets gave
915 * positive result ...
917 ldap_get_option( msc->msc_ld,
918 LDAP_OPT_ERROR_STRING, &rmsg );
919 if ( rmsg != NULL && rmsg[ 0 ] == '\0' ) {
920 ldap_memfree( rmsg );
924 ldap_get_option( msc->msc_ld,
925 LDAP_OPT_MATCHED_DN, &rmatch );
926 if ( rmatch != NULL && rmatch[ 0 ] == '\0' ) {
927 ldap_memfree( rmatch );
931 rerr = rs->sr_err = slap_map_api2result( rs );
933 Debug(LDAP_DEBUG_ANY,
934 "==> meta_back_op_result: target"
935 " <%d> sending msg \"%s\""
936 " (matched \"%s\")\n",
937 candidate, ( rmsg ? rmsg : "" ),
938 ( rmatch ? rmatch : "" ) );
942 for ( i = 0; i < mi->mi_ntargets; i++ ) {
943 metasingleconn_t *msc = &mc->mc_conns[ i ];
947 rs->sr_err = LDAP_SUCCESS;
949 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
950 if ( rs->sr_err != LDAP_SUCCESS ) {
952 * better check the type of error. In some cases
953 * (search ?) it might be better to return a
954 * success if at least one of the targets gave
955 * positive result ...
957 ldap_get_option( msc->msc_ld,
958 LDAP_OPT_ERROR_STRING, &msg );
959 if ( msg != NULL && msg[ 0 ] == '\0' ) {
964 ldap_get_option( msc->msc_ld,
965 LDAP_OPT_MATCHED_DN, &match );
966 if ( match != NULL && match[ 0 ] == '\0' ) {
967 ldap_memfree( match );
971 rs->sr_err = slap_map_api2result( rs );
973 Debug(LDAP_DEBUG_ANY,
974 "==> meta_back_op_result: target"
975 " <%d> sending msg \"%s\""
976 " (matched \"%s\")\n",
977 i, ( msg ? msg : "" ),
978 ( match ? match : "" ) );
981 * FIXME: need to rewrite "match" (need rwinfo)
983 switch ( rs->sr_err ) {
988 ldap_memfree( rmsg );
993 if ( match != NULL ) {
995 ldap_memfree( rmatch );
1004 ldap_memfree( msg );
1008 ldap_memfree( match );
1015 if ( rmsg != NULL ) {
1016 save_rmsg = rs->sr_text;
1019 if ( rmatch != NULL ) {
1020 struct berval dn, pdn;
1022 ber_str2bv( rmatch, 0, 0, &dn );
1023 if ( dnPretty( NULL, &dn, &pdn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
1024 ldap_memfree( rmatch );
1025 rmatch_ctx = op->o_tmpmemctx;
1026 rmatch = pdn.bv_val;
1028 save_rmatch = rs->sr_matched;
1029 rs->sr_matched = rmatch;
1031 send_ldap_result( op, rs );
1032 if ( rmsg != NULL ) {
1033 ber_memfree( rmsg );
1034 rs->sr_text = save_rmsg;
1036 if ( rmatch != NULL ) {
1037 ber_memfree_x( rmatch, rmatch_ctx );
1038 rs->sr_matched = save_rmatch;
1041 return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );