2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2005 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_rebind;
40 meta_back_single_bind(
47 meta_back_bind( Operation *op, SlapReply *rs )
49 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
50 metaconn_t *mc = NULL;
53 i, gotit = 0, isroot = 0;
55 SlapReply *candidates = meta_back_candidates_get( op );
57 rs->sr_err = LDAP_SUCCESS;
59 Debug( LDAP_DEBUG_ARGS, "meta_back_bind: dn: %s.\n%s%s",
60 op->o_req_dn.bv_val, "", "" );
62 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
64 ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ) );
67 /* we need meta_back_getconn() not send result even on error,
68 * because we want to intercept the error and make it
69 * invalidCredentials */
70 mc = meta_back_getconn( op, rs, NULL, LDAP_BACK_DONTSEND );
72 Debug( LDAP_DEBUG_ANY,
73 "meta_back_bind: no target "
74 "for dn \"%s\" (%d: %s).\n",
75 op->o_req_dn.bv_val, rs->sr_err,
76 rs->sr_text ? rs->sr_text : "" );
77 /* FIXME: there might be cases where we don't want
78 * to map the error onto invalidCredentials */
79 switch ( rs->sr_err ) {
80 case LDAP_NO_SUCH_OBJECT:
81 case LDAP_UNWILLING_TO_PERFORM:
82 rs->sr_err = LDAP_INVALID_CREDENTIALS;
86 send_ldap_result( op, rs );
91 * Each target is scanned ...
93 mc->mc_auth_target = META_BOUND_NONE;
94 for ( i = 0; i < mi->mi_ntargets; i++ ) {
101 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
108 } else if ( isroot == 0 ) {
110 * A bind operation is expected to have
111 * ONE CANDIDATE ONLY!
113 Debug( LDAP_DEBUG_ANY,
114 "==>meta_back_bind: more than one"
115 " candidate is trying to bind...\n",
119 if ( isroot && !BER_BVISNULL( &mi->mi_targets[ i ].mt_pseudorootdn ) )
121 op2.o_req_dn = mi->mi_targets[ i ].mt_pseudorootdn;
122 op2.o_req_ndn = mi->mi_targets[ i ].mt_pseudorootdn;
123 op2.orb_cred = mi->mi_targets[ i ].mt_pseudorootpw;
124 op2.orb_method = LDAP_AUTH_SIMPLE;
127 lerr = meta_back_single_bind( &op2, rs, mc, i );
128 if ( lerr != LDAP_SUCCESS ) {
130 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
132 if ( META_BACK_ONERR_STOP( mi ) ) {
143 mc->mc_auth_target = META_BOUND_ALL;
146 meta_back_release_conn( op, mc );
149 * rc is LDAP_SUCCESS if at least one bind succeeded,
150 * err is the last error that occurred during a bind;
151 * if at least (and at most?) one bind succeedes, fine.
153 if ( rc != LDAP_SUCCESS ) {
156 * deal with bind failure ...
160 * no target was found within the naming context,
161 * so bind must fail with invalid credentials
163 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
164 rs->sr_err = LDAP_INVALID_CREDENTIALS;
167 rs->sr_err = slap_map_api2result( rs );
168 send_ldap_result( op, rs );
176 * meta_back_single_bind
178 * attempts to perform a bind with creds
181 meta_back_single_bind(
187 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
188 metatarget_t *mt = &mi->mi_targets[ candidate ];
189 struct berval mdn = BER_BVNULL;
191 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
196 * Rewrite the bind dn if needed
198 dc.target = &mi->mi_targets[ candidate ];
199 dc.conn = op->o_conn;
203 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
204 send_ldap_result( op, rs );
208 /* FIXME: this fixes the bind problem right now; we need
209 * to use the asynchronous version to get the "matched"
210 * and more in case of failure ... */
211 /* FIXME: should be check if at least some of the op->o_ctrls
212 * can/should be passed? */
214 rs->sr_err = ldap_sasl_bind( msc->msc_ld, mdn.bv_val,
215 LDAP_SASL_SIMPLE, &op->orb_cred,
216 op->o_ctrls, NULL, &msgid );
217 if ( rs->sr_err == LDAP_SUCCESS ) {
219 struct timeval tv = { 0, 100000 };
221 int nretries = mt->mt_nretries;
228 tv.tv_usec = META_BIND_TIMEOUT;
229 switch ( ldap_result( msc->msc_ld, msgid, 0, &tv, &res ) ) {
231 Debug( LDAP_DEBUG_ANY,
232 "%s meta_back_single_bind: "
233 "ldap_result=0 nretries=%d%s\n",
234 op->o_log_prefix, nretries,
235 rebinding ? " rebinding" : "" );
237 if ( nretries != META_RETRY_NEVER ) {
238 ldap_pvt_thread_yield();
239 if ( nretries > 0 ) {
246 rs->sr_err = LDAP_BUSY;
248 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
252 /* FIXME: some times the request times out
253 * while the other party is not willing to
254 * send a response any more. Give it a second
255 * chance with a freshly bound connection */
257 nretries = mt->mt_nretries;
261 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
265 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
268 Debug( LDAP_DEBUG_ANY,
269 "### %s meta_back_single_bind: "
270 "err=%d nretries=%d\n",
271 op->o_log_prefix, rs->sr_err, nretries );
273 rc = slap_map_api2result( rs );
274 if ( rs->sr_err == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
275 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
276 if ( mc->mc_refcnt == 1 ) {
277 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
281 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
283 /* mc here must be the regular mc,
284 * reset and ready for init */
285 rc = meta_back_init_one_conn( op, rs,
286 mt, msc, LDAP_BACK_DONTSEND );
289 /* can't do anything about it */
293 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
296 if ( nretries > 0 ) {
299 ldap_pvt_thread_yield();
306 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
307 NULL, NULL, NULL, NULL, 1 );
308 if ( rc != LDAP_SUCCESS ) {
315 if ( rs->sr_err != LDAP_SUCCESS ) {
316 rs->sr_err = slap_map_api2result( rs );
320 ber_bvreplace( &msc->msc_bound_ndn, &op->o_req_dn );
321 msc->msc_bound = META_BOUND;
322 mc->mc_auth_target = candidate;
324 if ( LDAP_BACK_SAVECRED( mi ) ) {
325 ber_bvreplace( &msc->msc_cred, &op->orb_cred );
326 ldap_set_rebind_proc( msc->msc_ld, meta_back_rebind, msc );
329 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED
330 && op->o_req_ndn.bv_len != 0 )
332 ( void )meta_dncache_update_entry( &mi->mi_cache,
333 &op->o_req_ndn, candidate );
337 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
345 * meta_back_single_dobind
348 meta_back_single_dobind(
353 ldap_back_send_t sendok,
357 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
358 metatarget_t *mt = &mi->mi_targets[ candidate ];
359 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
361 struct berval cred = BER_BVC( "" );
364 save_nretries = nretries;
367 * Otherwise an anonymous bind is performed
368 * (note: if the target was already bound, the anonymous
369 * bind clears the previous bind).
371 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
372 ber_memfree( msc->msc_bound_ndn.bv_val );
373 BER_BVZERO( &msc->msc_bound_ndn );
376 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
377 /* destroy sensitive data */
378 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
379 ber_memfree( msc->msc_cred.bv_val );
380 BER_BVZERO( &msc->msc_cred );
383 /* FIXME: should we check if at least some of the op->o_ctrls
384 * can/should be passed? */
386 rc = ldap_sasl_bind( msc->msc_ld, "", LDAP_SASL_SIMPLE, &cred,
387 NULL, NULL, &msgid );
388 if ( rc == LDAP_SUCCESS ) {
390 struct timeval tv = { 0, 100000 };
397 tv.tv_usec = META_BIND_TIMEOUT;
398 switch ( ldap_result( msc->msc_ld, msgid, 0, &tv, &res ) ) {
400 Debug( LDAP_DEBUG_ANY,
401 "%s meta_back_single_dobind: "
402 "ldap_result=0 nretries=%d%s\n",
403 op->o_log_prefix, nretries,
404 rebinding ? " rebinding" : "" );
406 if ( nretries != META_RETRY_NEVER ) {
407 ldap_pvt_thread_yield();
408 if ( nretries > 0 ) {
418 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
422 /* FIXME: some times the request times out
423 * while the other party is not willing to
424 * send a response any more. Give it a second
425 * chance with a freshly bound connection */
427 nretries = save_nretries;
431 ldap_get_option( msc->msc_ld,
432 LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
435 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
438 Debug( LDAP_DEBUG_ANY,
439 "### %s meta_back_single_dobind: "
440 "err=%d nretries=%d\n",
441 op->o_log_prefix, rs->sr_err, nretries );
443 rc = slap_map_api2result( rs );
444 if ( rc == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
446 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
449 if ( mc->mc_refcnt == 1 ) {
450 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
454 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
456 /* mc here must be the regular mc,
457 * reset and ready for init */
458 rc = meta_back_init_one_conn( op, rs,
459 mt, msc, LDAP_BACK_DONTSEND );
463 /* can't do anything about it */
464 rc = LDAP_UNAVAILABLE;
468 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
471 if ( rc == LDAP_SUCCESS ) {
472 ldap_pvt_thread_yield();
473 if ( nretries > 0 ) {
482 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
483 NULL, NULL, NULL, NULL, 1 );
484 if ( rc == LDAP_SUCCESS ) {
485 rc = slap_map_api2result( rs );
491 rc = slap_map_api2result( rs );
495 if ( rc != LDAP_SUCCESS && ( sendok & LDAP_BACK_SENDERR ) ) {
496 send_ldap_result( op, rs );
510 ldap_back_send_t sendok )
512 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
518 SlapReply *candidates = meta_back_candidates_get( op );
520 if ( be_isroot( op ) ) {
524 Debug( LDAP_DEBUG_TRACE,
525 "%s meta_back_dobind: conn=%ld%s\n",
526 op->o_log_prefix, mc->mc_conn->c_connid,
527 isroot ? " (isroot)" : "" );
529 ldap_pvt_thread_mutex_lock( &mc->mc_mutex );
532 * all the targets are bound as pseudoroot
534 if ( mc->mc_auth_target == META_BOUND_ALL ) {
539 for ( i = 0; i < mi->mi_ntargets; i++ ) {
540 metatarget_t *mt = &mi->mi_targets[ i ];
541 metasingleconn_t *msc = &mc->mc_conns[ i ];
547 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
551 assert( msc->msc_ld != NULL );
554 * If the target is already bound it is skipped
556 if ( msc->msc_bound == META_BOUND && mc->mc_auth_target == i ) {
559 Debug( LDAP_DEBUG_TRACE, "%s meta_back_dobind[%d]: "
561 op->o_log_prefix, i, 0 );
565 if ( isroot && !BER_BVISNULL( &mi->mi_targets[ i ].mt_pseudorootdn ) )
569 op2.o_tag = LDAP_REQ_BIND;
570 op2.o_req_dn = mi->mi_targets[ i ].mt_pseudorootdn;
571 op2.o_req_ndn = mi->mi_targets[ i ].mt_pseudorootdn;
572 op2.orb_cred = mi->mi_targets[ i ].mt_pseudorootpw;
573 op2.orb_method = LDAP_AUTH_SIMPLE;
575 rc = meta_back_single_bind( &op2, rs, mc, i );
578 rc = meta_back_single_dobind( op, rs, mc, i,
579 LDAP_BACK_DONTSEND, mt->mt_nretries, 1 );
582 if ( rc != LDAP_SUCCESS ) {
583 rs->sr_err = slap_map_api2result( rs );
585 Debug( LDAP_DEBUG_ANY, "%s meta_back_dobind[%d]: "
586 "(anonymous) err=%d\n",
587 op->o_log_prefix, i, rc );
590 * null cred bind should always succeed
591 * as anonymous, so a failure means
592 * the target is no longer candidate possibly
593 * due to technical reasons (remote host down?)
594 * so better clear the handle
596 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
597 if ( META_BACK_ONERR_STOP( mi ) ) {
604 Debug( LDAP_DEBUG_TRACE, "%s meta_back_dobind[%d]: "
606 op->o_log_prefix, i, 0 );
608 msc->msc_bound = META_ANONYMOUS;
613 ldap_pvt_thread_mutex_unlock( &mc->mc_mutex );
615 Debug( LDAP_DEBUG_TRACE,
616 "%s meta_back_dobind: conn=%ld bound=%d\n",
617 op->o_log_prefix, mc->mc_conn->c_connid, bound );
620 meta_back_release_conn( op, mc );
622 if ( sendok & LDAP_BACK_SENDERR ) {
623 if ( rs->sr_err == LDAP_SUCCESS ) {
624 rs->sr_err = LDAP_BUSY;
626 send_ldap_result( op, rs );
636 * This is a callback used for chasing referrals using the same
637 * credentials as the original user on this session.
642 LDAP_CONST char *url,
647 metasingleconn_t *msc = ( metasingleconn_t * )params;
649 return ldap_sasl_bind_s( ld, msc->msc_bound_ndn.bv_val,
650 LDAP_SASL_SIMPLE, &msc->msc_cred,
655 * FIXME: error return must be handled in a cleaner way ...
664 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
671 if ( candidate != META_TARGET_NONE ) {
672 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
674 rs->sr_err = LDAP_SUCCESS;
676 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
677 if ( rs->sr_err != LDAP_SUCCESS ) {
679 * better check the type of error. In some cases
680 * (search ?) it might be better to return a
681 * success if at least one of the targets gave
682 * positive result ...
684 ldap_get_option( msc->msc_ld,
685 LDAP_OPT_ERROR_STRING, &rmsg );
686 if ( rmsg != NULL && rmsg[ 0 ] == '\0' ) {
687 ldap_memfree( rmsg );
691 ldap_get_option( msc->msc_ld,
692 LDAP_OPT_MATCHED_DN, &rmatch );
693 if ( rmatch != NULL && rmatch[ 0 ] == '\0' ) {
694 ldap_memfree( rmatch );
698 rerr = rs->sr_err = slap_map_api2result( rs );
700 Debug(LDAP_DEBUG_ANY,
701 "==> meta_back_op_result: target"
702 " <%d> sending msg \"%s\""
703 " (matched \"%s\")\n",
704 candidate, ( rmsg ? rmsg : "" ),
705 ( rmatch ? rmatch : "" ) );
709 for ( i = 0; i < mi->mi_ntargets; i++ ) {
710 metasingleconn_t *msc = &mc->mc_conns[ i ];
714 rs->sr_err = LDAP_SUCCESS;
716 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
717 if ( rs->sr_err != LDAP_SUCCESS ) {
719 * better check the type of error. In some cases
720 * (search ?) it might be better to return a
721 * success if at least one of the targets gave
722 * positive result ...
724 ldap_get_option( msc->msc_ld,
725 LDAP_OPT_ERROR_STRING, &msg );
726 if ( msg != NULL && msg[ 0 ] == '\0' ) {
731 ldap_get_option( msc->msc_ld,
732 LDAP_OPT_MATCHED_DN, &match );
733 if ( match != NULL && match[ 0 ] == '\0' ) {
734 ldap_memfree( match );
738 rs->sr_err = slap_map_api2result( rs );
740 Debug(LDAP_DEBUG_ANY,
741 "==> meta_back_op_result: target"
742 " <%d> sending msg \"%s\""
743 " (matched \"%s\")\n",
744 i, ( msg ? msg : "" ),
745 ( match ? match : "" ) );
748 * FIXME: need to rewrite "match" (need rwinfo)
750 switch ( rs->sr_err ) {
755 ldap_memfree( rmsg );
760 if ( match != NULL ) {
762 ldap_memfree( rmatch );
775 ldap_memfree( match );
783 if ( rmatch != NULL ) {
784 struct berval dn, pdn;
786 ber_str2bv( rmatch, 0, 0, &dn );
787 if ( dnPretty( NULL, &dn, &pdn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
788 rs->sr_matched = pdn.bv_val;
789 ldap_memfree( rmatch );
792 rs->sr_matched = rmatch;
796 rs->sr_matched = NULL;
798 send_ldap_result( op, rs );
799 if ( rmsg != NULL ) {
802 if ( rs->sr_matched != NULL ) {
803 if ( rmatch == NULL ) {
804 ber_memfree_x( rs->sr_matched, op->o_tmpmemctx );
807 ldap_memfree( rmatch );
809 rs->sr_matched = NULL;
813 return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );