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"
37 static LDAP_REBIND_PROC meta_back_default_rebind;
40 * a module could register a replacement for this function
42 LDAP_REBIND_PROC *meta_back_rebind_f = meta_back_default_rebind;
45 meta_back_single_bind(
53 meta_back_bind( Operation *op, SlapReply *rs )
55 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
56 metaconn_t *mc = NULL;
63 SlapReply *candidates = meta_back_candidates_get( op );
65 rs->sr_err = LDAP_SUCCESS;
67 Debug( LDAP_DEBUG_ARGS, "%s meta_back_bind: dn=\"%s\".\n",
68 op->o_log_prefix, op->o_req_dn.bv_val, 0 );
70 /* the test on the bind method should be superfluous */
71 if ( op->orb_method == LDAP_AUTH_SIMPLE
72 && be_isroot_dn( op->o_bd, &op->o_req_ndn ) )
74 if ( !be_isroot_pw( op ) ) {
75 rs->sr_err = LDAP_INVALID_CREDENTIALS;
77 send_ldap_result( op, rs );
81 if ( META_BACK_DEFER_ROOTDN_BIND( mi ) ) {
82 rs->sr_err = LDAP_SUCCESS;
84 /* frontend will return success */
91 /* we need meta_back_getconn() not send result even on error,
92 * because we want to intercept the error and make it
93 * invalidCredentials */
94 mc = meta_back_getconn( op, rs, NULL, LDAP_BACK_BIND_SERR );
96 char buf[ SLAP_TEXT_BUFLEN ];
98 snprintf( buf, sizeof( buf ),
99 "meta_back_bind: no target "
100 "for dn \"%s\" (%d%s%s).",
101 op->o_req_dn.bv_val, rs->sr_err,
102 rs->sr_text ? ". " : "",
103 rs->sr_text ? rs->sr_text : "" );
104 Debug( LDAP_DEBUG_ANY,
106 op->o_log_prefix, buf, 0 );
108 /* FIXME: there might be cases where we don't want
109 * to map the error onto invalidCredentials */
110 switch ( rs->sr_err ) {
111 case LDAP_NO_SUCH_OBJECT:
112 case LDAP_UNWILLING_TO_PERFORM:
113 rs->sr_err = LDAP_INVALID_CREDENTIALS;
117 send_ldap_result( op, rs );
122 * Each target is scanned ...
124 mc->mc_authz_target = META_BOUND_NONE;
125 for ( i = 0; i < mi->mi_ntargets; i++ ) {
131 * Skip non-candidates
133 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
138 /* set rc to LDAP_SUCCESS only if at least
139 * one candidate has been tried */
143 } else if ( isroot == 0 ) {
145 * A bind operation is expected to have
146 * ONE CANDIDATE ONLY!
148 Debug( LDAP_DEBUG_ANY,
149 "### %s meta_back_bind: more than one"
150 " candidate selected...\n",
151 op->o_log_prefix, 0, 0 );
155 if ( BER_BVISNULL( &mi->mi_targets[ i ].mt_pseudorootdn ) )
157 metasingleconn_t *msc = &mc->mc_conns[ i ];
159 /* skip the target if no pseudorootdn is provided */
160 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
161 ch_free( msc->msc_bound_ndn.bv_val );
162 BER_BVZERO( &msc->msc_bound_ndn );
165 if ( LDAP_BACK_SAVECRED( mi ) &&
166 !BER_BVISNULL( &msc->msc_cred ) )
168 /* destroy sensitive data */
169 memset( msc->msc_cred.bv_val, 0,
170 msc->msc_cred.bv_len );
171 ch_free( msc->msc_cred.bv_val );
172 BER_BVZERO( &msc->msc_cred );
178 op2.o_req_dn = mi->mi_targets[ i ].mt_pseudorootdn;
179 op2.o_req_ndn = mi->mi_targets[ i ].mt_pseudorootdn;
180 op2.orb_cred = mi->mi_targets[ i ].mt_pseudorootpw;
181 op2.orb_method = LDAP_AUTH_SIMPLE;
186 lerr = meta_back_single_bind( &op2, rs, mc, i, massage );
188 if ( lerr != LDAP_SUCCESS ) {
189 rc = rs->sr_err = lerr;
190 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
195 /* must re-insert if local DN changed as result of bind */
196 if ( rc == LDAP_SUCCESS ) {
198 mc->mc_authz_target = META_BOUND_ALL;
199 ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ) );
202 if ( !dn_match( &op->o_req_ndn, &mc->mc_local_ndn ) ) {
205 /* wait for all other ops to release the connection */
207 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
208 if ( mc->mc_refcnt > 1 ) {
209 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
210 ldap_pvt_thread_yield();
214 assert( mc->mc_refcnt == 1 );
215 mc = avl_delete( &mi->mi_conninfo.lai_tree, (caddr_t)mc,
216 meta_back_conn_cmp );
217 assert( mc != NULL );
219 ber_bvreplace( &mc->mc_local_ndn, &op->o_req_ndn );
220 lerr = avl_insert( &mi->mi_conninfo.lai_tree, (caddr_t)mc,
221 meta_back_conn_cmp, meta_back_conn_dup );
222 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
224 for ( i = 0; i < mi->mi_ntargets; ++i ) {
225 if ( mc->mc_conns[ i ].msc_ld != NULL ) {
226 meta_clear_one_candidate( &mc->mc_conns[ i ] );
230 /* we can do this because mc_refcnt == 1 */
232 meta_back_conn_free( mc );
239 meta_back_release_conn( op, mc );
243 * rc is LDAP_SUCCESS if at least one bind succeeded,
244 * err is the last error that occurred during a bind;
245 * if at least (and at most?) one bind succeeds, fine.
247 if ( rc != LDAP_SUCCESS ) {
250 * deal with bind failure ...
254 * no target was found within the naming context,
255 * so bind must fail with invalid credentials
257 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
258 rs->sr_err = LDAP_INVALID_CREDENTIALS;
260 rs->sr_err = slap_map_api2result( rs );
262 send_ldap_result( op, rs );
271 * meta_back_single_bind
273 * attempts to perform a bind with creds
276 meta_back_single_bind(
283 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
284 metatarget_t *mt = &mi->mi_targets[ candidate ];
285 struct berval mdn = BER_BVNULL;
286 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
291 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
292 ch_free( msc->msc_bound_ndn.bv_val );
293 BER_BVZERO( &msc->msc_bound_ndn );
296 if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &msc->msc_cred ) ) {
297 /* destroy sensitive data */
298 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
299 ch_free( msc->msc_cred.bv_val );
300 BER_BVZERO( &msc->msc_cred );
304 * Rewrite the bind dn if needed
310 dc.conn = op->o_conn;
314 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
315 send_ldap_result( op, rs );
323 /* FIXME: this fixes the bind problem right now; we need
324 * to use the asynchronous version to get the "matched"
325 * and more in case of failure ... */
326 /* FIXME: should we check if at least some of the op->o_ctrls
327 * can/should be passed? */
329 rs->sr_err = ldap_sasl_bind( msc->msc_ld, mdn.bv_val,
330 LDAP_SASL_SIMPLE, &op->orb_cred,
331 op->o_ctrls, NULL, &msgid );
332 if ( rs->sr_err == LDAP_SUCCESS ) {
336 int nretries = mt->mt_nretries;
337 char buf[ SLAP_TEXT_BUFLEN ];
339 LDAP_BACK_TV_SET( &tv );
345 switch ( ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
347 snprintf( buf, sizeof( buf ),
348 "ldap_result=0 nretries=%d%s\n",
349 nretries, rebinding ? " rebinding" : "" );
350 Debug( LDAP_DEBUG_ANY,
351 "%s meta_back_single_bind[%d]: %s.\n",
352 op->o_log_prefix, candidate, buf );
354 if ( nretries != META_RETRY_NEVER ) {
355 ldap_pvt_thread_yield();
356 if ( nretries > 0 ) {
359 tv = mt->mt_bind_timeout;
362 rs->sr_err = LDAP_BUSY;
364 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
368 /* FIXME: some times the request times out
369 * while the other party is not willing to
370 * send a response any more. Give it a second
371 * chance with a freshly bound connection */
373 nretries = mt->mt_nretries;
377 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
381 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
384 snprintf( buf, sizeof( buf ),
385 "err=%d nretries=%d",
386 rs->sr_err, nretries );
387 Debug( LDAP_DEBUG_ANY,
388 "### %s meta_back_single_bind[%d]: %s.\n",
389 op->o_log_prefix, candidate, buf );
391 rc = slap_map_api2result( rs );
392 if ( rs->sr_err == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
393 rc = meta_back_retry( op, rs, mc, candidate, LDAP_BACK_DONTSEND );
395 if ( nretries > 0 ) {
398 ldap_pvt_thread_yield();
405 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
406 NULL, NULL, NULL, NULL, 1 );
407 if ( rc != LDAP_SUCCESS ) {
414 if ( rs->sr_err != LDAP_SUCCESS ) {
415 rs->sr_err = slap_map_api2result( rs );
419 ber_bvreplace( &msc->msc_bound_ndn, &op->o_req_dn );
420 LDAP_BACK_CONN_ISBOUND_SET( msc );
421 mc->mc_authz_target = candidate;
423 if ( LDAP_BACK_SAVECRED( mi ) ) {
424 ber_bvreplace( &msc->msc_cred, &op->orb_cred );
425 ldap_set_rebind_proc( msc->msc_ld, meta_back_rebind_f, msc );
428 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED
429 && op->o_req_ndn.bv_len != 0 )
431 ( void )meta_dncache_update_entry( &mi->mi_cache,
432 &op->o_req_ndn, candidate );
436 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
444 * meta_back_single_dobind
447 meta_back_single_dobind(
452 ldap_back_send_t sendok,
456 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
457 metatarget_t *mt = &mi->mi_targets[ candidate ];
458 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
460 static struct berval cred = BER_BVC( "" );
463 save_nretries = nretries;
465 assert( !LDAP_BACK_CONN_ISBOUND( msc ) );
468 * Otherwise an anonymous bind is performed
469 * (note: if the target was already bound, the anonymous
470 * bind clears the previous bind).
472 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
473 ber_memfree( msc->msc_bound_ndn.bv_val );
474 BER_BVZERO( &msc->msc_bound_ndn );
477 if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &msc->msc_cred ) ) {
478 /* destroy sensitive data */
479 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
480 ber_memfree( msc->msc_cred.bv_val );
481 BER_BVZERO( &msc->msc_cred );
484 /* FIXME: should we check if at least some of the op->o_ctrls
485 * can/should be passed? */
487 rc = ldap_sasl_bind( msc->msc_ld, "", LDAP_SASL_SIMPLE, &cred,
488 NULL, NULL, &msgid );
489 if ( rc == LDAP_SUCCESS ) {
492 char buf[ SLAP_TEXT_BUFLEN ];
494 LDAP_BACK_TV_SET( &tv );
500 switch ( ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
502 snprintf( buf, sizeof( buf ),
503 "ldap_result=0 nretries=%d%s",
504 nretries, rebinding ? " rebinding" : "" );
505 Debug( LDAP_DEBUG_ANY,
506 "%s meta_back_single_dobind[%d]: %s.\n",
507 op->o_log_prefix, candidate, buf );
509 if ( nretries != META_RETRY_NEVER ) {
510 ldap_pvt_thread_yield();
511 if ( nretries > 0 ) {
514 tv = mt->mt_bind_timeout;
520 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
524 /* FIXME: some times the request times out
525 * while the other party is not willing to
526 * send a response any more. Give it a second
527 * chance with a freshly bound connection */
529 nretries = save_nretries;
533 ldap_get_option( msc->msc_ld,
534 LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
537 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
540 snprintf( buf, sizeof( buf ),
541 "err=%d nretries=%d",
542 rs->sr_err, nretries );
543 Debug( LDAP_DEBUG_ANY,
544 "### %s meta_back_single_dobind[%d]: %s.\n",
545 op->o_log_prefix, candidate, buf );
547 rc = slap_map_api2result( rs );
548 if ( rc == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
549 /* NOTE: we do not use meta_back_retry() here
550 * to avoid circular loops; mc_mutex is set
553 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
556 if ( mc->mc_refcnt == 1 ) {
557 meta_clear_one_candidate( msc );
558 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
560 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
562 /* mc here must be the regular mc,
563 * reset and ready for init */
564 rc = meta_back_init_one_conn( op, rs,
566 LDAP_BACK_CONN_ISPRIV( mc ),
567 LDAP_BACK_DONTSEND );
570 /* can't do anything about it */
571 rc = LDAP_UNAVAILABLE;
575 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
578 if ( rc == LDAP_SUCCESS ) {
579 ldap_pvt_thread_yield();
580 if ( nretries > 0 ) {
589 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
590 NULL, NULL, NULL, NULL, 1 );
591 if ( rc == LDAP_SUCCESS ) {
592 rc = slap_map_api2result( rs );
599 rc = slap_map_api2result( rs );
603 if ( rc != LDAP_SUCCESS && ( sendok & LDAP_BACK_SENDERR ) ) {
604 send_ldap_result( op, rs );
618 ldap_back_send_t sendok )
620 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
626 SlapReply *candidates = meta_back_candidates_get( op );
628 if ( be_isroot( op ) ) {
632 Debug( LDAP_DEBUG_TRACE,
633 "%s meta_back_dobind: conn=%ld%s\n",
635 LDAP_BACK_PCONN_ID( mc->mc_conn ),
636 isroot ? " (isroot)" : "" );
638 ldap_pvt_thread_mutex_lock( &mc->mc_mutex );
641 * all the targets are bound as pseudoroot
643 if ( mc->mc_authz_target == META_BOUND_ALL ) {
648 for ( i = 0; i < mi->mi_ntargets; i++ ) {
649 metatarget_t *mt = &mi->mi_targets[ i ];
650 metasingleconn_t *msc = &mc->mc_conns[ i ];
657 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
661 assert( msc->msc_ld != NULL );
664 * If the target is already bound it is skipped
666 if ( LDAP_BACK_CONN_ISBOUND( msc ) || LDAP_BACK_CONN_ISANON( msc ) ) {
671 if ( isroot && !BER_BVISNULL( &mi->mi_targets[ i ].mt_pseudorootdn ) )
675 op2.o_tag = LDAP_REQ_BIND;
676 op2.o_req_dn = mi->mi_targets[ i ].mt_pseudorootdn;
677 op2.o_req_ndn = mi->mi_targets[ i ].mt_pseudorootdn;
678 op2.orb_cred = mi->mi_targets[ i ].mt_pseudorootpw;
679 op2.orb_method = LDAP_AUTH_SIMPLE;
681 rootdn = mi->mi_targets[ i ].mt_pseudorootdn.bv_val;
683 rc = meta_back_single_bind( &op2, rs, mc, i, 0 );
686 rc = meta_back_single_dobind( op, rs, mc, i,
687 LDAP_BACK_DONTSEND, mt->mt_nretries, 1 );
690 if ( rc != LDAP_SUCCESS ) {
691 char buf[ SLAP_TEXT_BUFLEN ];
693 snprintf( buf, sizeof( buf ),
694 "meta_back_dobind[%d]: (%s) err=%d.",
695 i, rootdn ? rootdn : "anonymous", rc );
696 Debug( LDAP_DEBUG_ANY,
698 op->o_log_prefix, buf, 0 );
701 * null cred bind should always succeed
702 * as anonymous, so a failure means
703 * the target is no longer candidate possibly
704 * due to technical reasons (remote host down?)
705 * so better clear the handle
707 /* leave the target candidate, but record the error for later use */
708 candidates[ i ].sr_err = rc;
709 if ( META_BACK_ONERR_STOP( mi ) ) {
716 Debug( LDAP_DEBUG_TRACE,
717 "%s meta_back_dobind[%d]: "
720 rootdn ? rootdn : "anonymous" );
723 LDAP_BACK_CONN_ISBOUND_SET( msc );
725 LDAP_BACK_CONN_ISANON_SET( msc );
731 ldap_pvt_thread_mutex_unlock( &mc->mc_mutex );
733 Debug( LDAP_DEBUG_TRACE,
734 "%s meta_back_dobind: conn=%ld bound=%d\n",
735 op->o_log_prefix, LDAP_BACK_PCONN_ID( mc->mc_conn ), bound );
738 meta_back_release_conn( op, mc );
740 if ( sendok & LDAP_BACK_SENDERR ) {
741 if ( rs->sr_err == LDAP_SUCCESS ) {
742 rs->sr_err = LDAP_BUSY;
744 send_ldap_result( op, rs );
752 * meta_back_default_rebind
754 * This is a callback used for chasing referrals using the same
755 * credentials as the original user on this session.
758 meta_back_default_rebind(
760 LDAP_CONST char *url,
765 metasingleconn_t *msc = ( metasingleconn_t * )params;
767 return ldap_sasl_bind_s( ld, msc->msc_bound_ndn.bv_val,
768 LDAP_SASL_SIMPLE, &msc->msc_cred,
773 * FIXME: error return must be handled in a cleaner way ...
782 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
788 const char *save_rmsg = NULL,
790 void *rmatch_ctx = NULL;
792 if ( candidate != META_TARGET_NONE ) {
793 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
795 rs->sr_err = LDAP_SUCCESS;
797 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
798 if ( rs->sr_err != LDAP_SUCCESS ) {
800 * better check the type of error. In some cases
801 * (search ?) it might be better to return a
802 * success if at least one of the targets gave
803 * positive result ...
805 ldap_get_option( msc->msc_ld,
806 LDAP_OPT_ERROR_STRING, &rmsg );
807 if ( rmsg != NULL && rmsg[ 0 ] == '\0' ) {
808 ldap_memfree( rmsg );
812 ldap_get_option( msc->msc_ld,
813 LDAP_OPT_MATCHED_DN, &rmatch );
814 if ( rmatch != NULL && rmatch[ 0 ] == '\0' ) {
815 ldap_memfree( rmatch );
819 rerr = rs->sr_err = slap_map_api2result( rs );
821 Debug(LDAP_DEBUG_ANY,
822 "==> meta_back_op_result: target"
823 " <%d> sending msg \"%s\""
824 " (matched \"%s\")\n",
825 candidate, ( rmsg ? rmsg : "" ),
826 ( rmatch ? rmatch : "" ) );
830 for ( i = 0; i < mi->mi_ntargets; i++ ) {
831 metasingleconn_t *msc = &mc->mc_conns[ i ];
835 rs->sr_err = LDAP_SUCCESS;
837 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
838 if ( rs->sr_err != LDAP_SUCCESS ) {
840 * better check the type of error. In some cases
841 * (search ?) it might be better to return a
842 * success if at least one of the targets gave
843 * positive result ...
845 ldap_get_option( msc->msc_ld,
846 LDAP_OPT_ERROR_STRING, &msg );
847 if ( msg != NULL && msg[ 0 ] == '\0' ) {
852 ldap_get_option( msc->msc_ld,
853 LDAP_OPT_MATCHED_DN, &match );
854 if ( match != NULL && match[ 0 ] == '\0' ) {
855 ldap_memfree( match );
859 rs->sr_err = slap_map_api2result( rs );
861 Debug(LDAP_DEBUG_ANY,
862 "==> meta_back_op_result: target"
863 " <%d> sending msg \"%s\""
864 " (matched \"%s\")\n",
865 i, ( msg ? msg : "" ),
866 ( match ? match : "" ) );
869 * FIXME: need to rewrite "match" (need rwinfo)
871 switch ( rs->sr_err ) {
876 ldap_memfree( rmsg );
881 if ( match != NULL ) {
883 ldap_memfree( rmatch );
896 ldap_memfree( match );
903 if ( rmsg != NULL ) {
904 save_rmsg = rs->sr_text;
907 if ( rmatch != NULL ) {
908 struct berval dn, pdn;
910 ber_str2bv( rmatch, 0, 0, &dn );
911 if ( dnPretty( NULL, &dn, &pdn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
912 ldap_memfree( rmatch );
913 rmatch_ctx = op->o_tmpmemctx;
916 save_rmatch = rs->sr_matched;
917 rs->sr_matched = rmatch;
919 send_ldap_result( op, rs );
920 if ( rmsg != NULL ) {
922 rs->sr_text = save_rmsg;
924 if ( rmatch != NULL ) {
925 ber_memfree_x( rmatch, rmatch_ctx );
926 rs->sr_matched = save_rmatch;
929 return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );