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_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(
52 meta_back_bind( Operation *op, SlapReply *rs )
54 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
55 metaconn_t *mc = NULL;
62 SlapReply *candidates = meta_back_candidates_get( op );
64 rs->sr_err = LDAP_SUCCESS;
66 Debug( LDAP_DEBUG_ARGS, "%s meta_back_bind: dn=\"%s\".\n",
67 op->o_log_prefix, op->o_req_dn.bv_val, 0 );
69 /* the test on the bind method should be superfluous */
70 if ( op->orb_method == LDAP_AUTH_SIMPLE
71 && be_isroot_dn( op->o_bd, &op->o_req_ndn ) )
73 if ( !be_isroot_pw( op ) ) {
74 rs->sr_err = LDAP_INVALID_CREDENTIALS;
76 send_ldap_result( op, rs );
80 if ( META_BACK_DEFER_ROOTDN_BIND( mi ) ) {
81 rs->sr_err = LDAP_SUCCESS;
83 /* frontend will return success */
90 /* we need meta_back_getconn() not send result even on error,
91 * because we want to intercept the error and make it
92 * invalidCredentials */
93 mc = meta_back_getconn( op, rs, NULL, LDAP_BACK_DONTSEND );
95 char buf[ SLAP_TEXT_BUFLEN ];
97 snprintf( buf, sizeof( buf ),
98 "meta_back_bind: no target "
99 "for dn \"%s\" (%d%s%s).",
100 op->o_req_dn.bv_val, rs->sr_err,
101 rs->sr_text ? ". " : "",
102 rs->sr_text ? rs->sr_text : "" );
103 Debug( LDAP_DEBUG_ANY,
105 op->o_log_prefix, buf, 0 );
107 /* FIXME: there might be cases where we don't want
108 * to map the error onto invalidCredentials */
109 switch ( rs->sr_err ) {
110 case LDAP_NO_SUCH_OBJECT:
111 case LDAP_UNWILLING_TO_PERFORM:
112 rs->sr_err = LDAP_INVALID_CREDENTIALS;
116 send_ldap_result( op, rs );
121 * Each target is scanned ...
123 mc->mc_authz_target = META_BOUND_NONE;
124 for ( i = 0; i < mi->mi_ntargets; i++ ) {
129 * Skip non-candidates
131 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
136 /* set rc to LDAP_SUCCESS only if at least
137 * one candidate has been tried */
141 } else if ( isroot == 0 ) {
143 * A bind operation is expected to have
144 * ONE CANDIDATE ONLY!
146 Debug( LDAP_DEBUG_ANY,
147 "### %s meta_back_bind: more than one"
148 " candidate is trying to bind...\n",
149 op->o_log_prefix, 0, 0 );
153 if ( BER_BVISNULL( &mi->mi_targets[ i ].mt_pseudorootdn ) )
155 metasingleconn_t *msc = &mc->mc_conns[ i ];
157 /* skip the target if no pseudorootdn is provided */
158 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
159 ch_free( msc->msc_bound_ndn.bv_val );
160 BER_BVZERO( &msc->msc_bound_ndn );
163 if ( LDAP_BACK_SAVECRED( mi ) &&
164 !BER_BVISNULL( &msc->msc_cred ) )
166 /* destroy sensitive data */
167 memset( msc->msc_cred.bv_val, 0,
168 msc->msc_cred.bv_len );
169 ch_free( msc->msc_cred.bv_val );
170 BER_BVZERO( &msc->msc_cred );
176 op2.o_req_dn = mi->mi_targets[ i ].mt_pseudorootdn;
177 op2.o_req_ndn = mi->mi_targets[ i ].mt_pseudorootdn;
178 op2.orb_cred = mi->mi_targets[ i ].mt_pseudorootpw;
179 op2.orb_method = LDAP_AUTH_SIMPLE;
182 lerr = meta_back_single_bind( &op2, rs, mc, i );
183 if ( lerr != LDAP_SUCCESS ) {
185 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
192 /* must re-insert if local DN changed as result of bind */
193 if ( rc == LDAP_SUCCESS ) {
195 mc->mc_authz_target = META_BOUND_ALL;
196 ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ) );
199 if ( !dn_match( &op->o_req_ndn, &mc->mc_local_ndn ) ) {
202 /* wait for all other ops to release the connection */
204 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
205 if ( mc->mc_refcnt > 1 ) {
206 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
207 ldap_pvt_thread_yield();
211 assert( mc->mc_refcnt == 1 );
212 mc = avl_delete( &mi->mi_conntree, (caddr_t)mc,
213 meta_back_conn_cmp );
214 assert( mc != NULL );
216 ber_bvreplace( &mc->mc_local_ndn, &op->o_req_ndn );
217 lerr = avl_insert( &mi->mi_conntree, (caddr_t)mc,
218 meta_back_conn_cmp, meta_back_conn_dup );
219 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
221 for ( i = 0; i < mi->mi_ntargets; ++i ) {
222 if ( mc->mc_conns[ i ].msc_ld != NULL ) {
223 meta_clear_one_candidate( &mc->mc_conns[ i ] );
227 /* we can do this because mc_refcnt == 1 */
229 meta_back_conn_free( mc );
236 meta_back_release_conn( op, mc );
240 * rc is LDAP_SUCCESS if at least one bind succeeded,
241 * err is the last error that occurred during a bind;
242 * if at least (and at most?) one bind succeeds, fine.
244 if ( rc != LDAP_SUCCESS ) {
247 * deal with bind failure ...
251 * no target was found within the naming context,
252 * so bind must fail with invalid credentials
254 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
255 rs->sr_err = LDAP_INVALID_CREDENTIALS;
257 rs->sr_err = slap_map_api2result( rs );
259 send_ldap_result( op, rs );
268 * meta_back_single_bind
270 * attempts to perform a bind with creds
273 meta_back_single_bind(
279 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
280 metatarget_t *mt = &mi->mi_targets[ candidate ];
281 struct berval mdn = BER_BVNULL;
283 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
288 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
289 ch_free( msc->msc_bound_ndn.bv_val );
290 BER_BVZERO( &msc->msc_bound_ndn );
293 if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &msc->msc_cred ) ) {
294 /* destroy sensitive data */
295 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
296 ch_free( msc->msc_cred.bv_val );
297 BER_BVZERO( &msc->msc_cred );
301 * Rewrite the bind dn if needed
304 dc.conn = op->o_conn;
308 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
309 send_ldap_result( op, rs );
313 /* FIXME: this fixes the bind problem right now; we need
314 * to use the asynchronous version to get the "matched"
315 * and more in case of failure ... */
316 /* FIXME: should we check if at least some of the op->o_ctrls
317 * can/should be passed? */
319 rs->sr_err = ldap_sasl_bind( msc->msc_ld, mdn.bv_val,
320 LDAP_SASL_SIMPLE, &op->orb_cred,
321 op->o_ctrls, NULL, &msgid );
322 if ( rs->sr_err == LDAP_SUCCESS ) {
326 int nretries = mt->mt_nretries;
328 LDAP_BACK_TV_SET( &tv );
335 tv.tv_usec = META_BIND_TIMEOUT;
336 switch ( ldap_result( msc->msc_ld, msgid, 0, &tv, &res ) ) {
338 Debug( LDAP_DEBUG_ANY,
339 "%s meta_back_single_bind: "
340 "ldap_result=0 nretries=%d%s\n",
341 op->o_log_prefix, nretries,
342 rebinding ? " rebinding" : "" );
344 if ( nretries != META_RETRY_NEVER ) {
345 ldap_pvt_thread_yield();
346 if ( nretries > 0 ) {
349 LDAP_BACK_TV_SET( &tv );
352 rs->sr_err = LDAP_BUSY;
354 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
358 /* FIXME: some times the request times out
359 * while the other party is not willing to
360 * send a response any more. Give it a second
361 * chance with a freshly bound connection */
363 nretries = mt->mt_nretries;
367 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
371 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
374 Debug( LDAP_DEBUG_ANY,
375 "### %s meta_back_single_bind: "
376 "err=%d nretries=%d\n",
377 op->o_log_prefix, rs->sr_err, nretries );
379 rc = slap_map_api2result( rs );
380 if ( rs->sr_err == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
381 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
382 if ( mc->mc_refcnt == 1 ) {
383 meta_clear_one_candidate( msc );
384 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
386 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
388 /* mc here must be the regular mc,
389 * reset and ready for init */
390 rc = meta_back_init_one_conn( op, rs,
391 mt, mc, msc, LDAP_BACK_CONN_ISPRIV( mc ),
392 candidate == mc->mc_authz_target,
393 LDAP_BACK_DONTSEND );
396 /* can't do anything about it */
400 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
403 if ( nretries > 0 ) {
406 ldap_pvt_thread_yield();
413 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
414 NULL, NULL, NULL, NULL, 1 );
415 if ( rc != LDAP_SUCCESS ) {
422 if ( rs->sr_err != LDAP_SUCCESS ) {
423 rs->sr_err = slap_map_api2result( rs );
427 ber_bvreplace( &msc->msc_bound_ndn, &op->o_req_dn );
428 LDAP_BACK_CONN_ISBOUND_SET( msc );
429 mc->mc_authz_target = candidate;
431 if ( LDAP_BACK_SAVECRED( mi ) ) {
432 ber_bvreplace( &msc->msc_cred, &op->orb_cred );
433 ldap_set_rebind_proc( msc->msc_ld, meta_back_rebind_f, msc );
436 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED
437 && op->o_req_ndn.bv_len != 0 )
439 ( void )meta_dncache_update_entry( &mi->mi_cache,
440 &op->o_req_ndn, candidate );
444 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
452 * meta_back_single_dobind
455 meta_back_single_dobind(
460 ldap_back_send_t sendok,
464 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
465 metatarget_t *mt = &mi->mi_targets[ candidate ];
466 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
468 static struct berval cred = BER_BVC( "" );
471 save_nretries = nretries;
473 assert( !LDAP_BACK_CONN_ISBOUND( msc ) );
476 * Otherwise an anonymous bind is performed
477 * (note: if the target was already bound, the anonymous
478 * bind clears the previous bind).
480 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
481 ber_memfree( msc->msc_bound_ndn.bv_val );
482 BER_BVZERO( &msc->msc_bound_ndn );
485 if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &msc->msc_cred ) ) {
486 /* destroy sensitive data */
487 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
488 ber_memfree( msc->msc_cred.bv_val );
489 BER_BVZERO( &msc->msc_cred );
492 /* FIXME: should we check if at least some of the op->o_ctrls
493 * can/should be passed? */
495 rc = ldap_sasl_bind( msc->msc_ld, "", LDAP_SASL_SIMPLE, &cred,
496 NULL, NULL, &msgid );
497 if ( rc == LDAP_SUCCESS ) {
501 LDAP_BACK_TV_SET( &tv );
508 tv.tv_usec = META_BIND_TIMEOUT;
509 switch ( ldap_result( msc->msc_ld, msgid, 0, &tv, &res ) ) {
511 Debug( LDAP_DEBUG_ANY,
512 "%s meta_back_single_dobind: "
513 "ldap_result=0 nretries=%d%s\n",
514 op->o_log_prefix, nretries,
515 rebinding ? " rebinding" : "" );
517 if ( nretries != META_RETRY_NEVER ) {
518 ldap_pvt_thread_yield();
519 if ( nretries > 0 ) {
522 LDAP_BACK_TV_SET( &tv );
528 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
532 /* FIXME: some times the request times out
533 * while the other party is not willing to
534 * send a response any more. Give it a second
535 * chance with a freshly bound connection */
537 nretries = save_nretries;
541 ldap_get_option( msc->msc_ld,
542 LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
545 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
548 Debug( LDAP_DEBUG_ANY,
549 "### %s meta_back_single_dobind: "
550 "err=%d nretries=%d\n",
551 op->o_log_prefix, rs->sr_err, nretries );
553 rc = slap_map_api2result( rs );
554 if ( rc == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
556 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
559 if ( mc->mc_refcnt == 1 ) {
560 meta_clear_one_candidate( msc );
561 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
563 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
565 /* mc here must be the regular mc,
566 * reset and ready for init */
567 rc = meta_back_init_one_conn( op, rs,
569 LDAP_BACK_CONN_ISPRIV( mc ),
570 candidate == mc->mc_authz_target,
571 LDAP_BACK_DONTSEND );
575 /* can't do anything about it */
576 rc = LDAP_UNAVAILABLE;
580 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
583 if ( rc == LDAP_SUCCESS ) {
584 ldap_pvt_thread_yield();
585 if ( nretries > 0 ) {
594 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
595 NULL, NULL, NULL, NULL, 1 );
596 if ( rc == LDAP_SUCCESS ) {
597 rc = slap_map_api2result( rs );
604 rc = slap_map_api2result( rs );
608 if ( rc != LDAP_SUCCESS && ( sendok & LDAP_BACK_SENDERR ) ) {
609 send_ldap_result( op, rs );
623 ldap_back_send_t sendok )
625 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
631 SlapReply *candidates = meta_back_candidates_get( op );
633 if ( be_isroot( op ) ) {
637 Debug( LDAP_DEBUG_TRACE,
638 "%s meta_back_dobind: conn=%ld%s\n",
640 LDAP_BACK_PCONN_ID( mc->mc_conn ),
641 isroot ? " (isroot)" : "" );
643 ldap_pvt_thread_mutex_lock( &mc->mc_mutex );
646 * all the targets are bound as pseudoroot
648 if ( mc->mc_authz_target == META_BOUND_ALL ) {
653 for ( i = 0; i < mi->mi_ntargets; i++ ) {
654 metatarget_t *mt = &mi->mi_targets[ i ];
655 metasingleconn_t *msc = &mc->mc_conns[ i ];
662 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
666 assert( msc->msc_ld != NULL );
669 * If the target is already bound it is skipped
671 if ( LDAP_BACK_CONN_ISBOUND( msc ) || LDAP_BACK_CONN_ISANON( msc ) ) {
676 if ( isroot && !BER_BVISNULL( &mi->mi_targets[ i ].mt_pseudorootdn ) )
680 op2.o_tag = LDAP_REQ_BIND;
681 op2.o_req_dn = mi->mi_targets[ i ].mt_pseudorootdn;
682 op2.o_req_ndn = mi->mi_targets[ i ].mt_pseudorootdn;
683 op2.orb_cred = mi->mi_targets[ i ].mt_pseudorootpw;
684 op2.orb_method = LDAP_AUTH_SIMPLE;
686 rootdn = mi->mi_targets[ i ].mt_pseudorootdn.bv_val;
688 rc = meta_back_single_bind( &op2, rs, mc, i );
691 rc = meta_back_single_dobind( op, rs, mc, i,
692 LDAP_BACK_DONTSEND, mt->mt_nretries, 1 );
695 if ( rc != LDAP_SUCCESS ) {
696 char buf[ SLAP_TEXT_BUFLEN ];
698 snprintf( buf, sizeof( buf ),
699 "meta_back_dobind[%d]: (%s) err=%d.",
700 i, rootdn ? rootdn : "anonymous", rc );
701 Debug( LDAP_DEBUG_ANY,
703 op->o_log_prefix, buf, 0 );
706 * null cred bind should always succeed
707 * as anonymous, so a failure means
708 * the target is no longer candidate possibly
709 * due to technical reasons (remote host down?)
710 * so better clear the handle
712 /* leave the target candidate, but record the error for later use */
713 candidates[ i ].sr_err = rc;
714 if ( META_BACK_ONERR_STOP( mi ) ) {
721 Debug( LDAP_DEBUG_TRACE,
722 "%s meta_back_dobind[%d]: "
725 rootdn ? rootdn : "anonymous" );
728 LDAP_BACK_CONN_ISBOUND_SET( msc );
730 LDAP_BACK_CONN_ISANON_SET( msc );
736 ldap_pvt_thread_mutex_unlock( &mc->mc_mutex );
738 Debug( LDAP_DEBUG_TRACE,
739 "%s meta_back_dobind: conn=%ld bound=%d\n",
740 op->o_log_prefix, LDAP_BACK_PCONN_ID( mc->mc_conn ), bound );
743 meta_back_release_conn( op, mc );
745 if ( sendok & LDAP_BACK_SENDERR ) {
746 if ( rs->sr_err == LDAP_SUCCESS ) {
747 rs->sr_err = LDAP_BUSY;
749 send_ldap_result( op, rs );
757 * meta_back_default_rebind
759 * This is a callback used for chasing referrals using the same
760 * credentials as the original user on this session.
763 meta_back_default_rebind(
765 LDAP_CONST char *url,
770 metasingleconn_t *msc = ( metasingleconn_t * )params;
772 return ldap_sasl_bind_s( ld, msc->msc_bound_ndn.bv_val,
773 LDAP_SASL_SIMPLE, &msc->msc_cred,
778 * FIXME: error return must be handled in a cleaner way ...
787 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
793 const char *save_rmsg = NULL,
795 void *rmatch_ctx = NULL;
797 if ( candidate != META_TARGET_NONE ) {
798 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
800 rs->sr_err = LDAP_SUCCESS;
802 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
803 if ( rs->sr_err != LDAP_SUCCESS ) {
805 * better check the type of error. In some cases
806 * (search ?) it might be better to return a
807 * success if at least one of the targets gave
808 * positive result ...
810 ldap_get_option( msc->msc_ld,
811 LDAP_OPT_ERROR_STRING, &rmsg );
812 if ( rmsg != NULL && rmsg[ 0 ] == '\0' ) {
813 ldap_memfree( rmsg );
817 ldap_get_option( msc->msc_ld,
818 LDAP_OPT_MATCHED_DN, &rmatch );
819 if ( rmatch != NULL && rmatch[ 0 ] == '\0' ) {
820 ldap_memfree( rmatch );
824 rerr = rs->sr_err = slap_map_api2result( rs );
826 Debug(LDAP_DEBUG_ANY,
827 "==> meta_back_op_result: target"
828 " <%d> sending msg \"%s\""
829 " (matched \"%s\")\n",
830 candidate, ( rmsg ? rmsg : "" ),
831 ( rmatch ? rmatch : "" ) );
835 for ( i = 0; i < mi->mi_ntargets; i++ ) {
836 metasingleconn_t *msc = &mc->mc_conns[ i ];
840 rs->sr_err = LDAP_SUCCESS;
842 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
843 if ( rs->sr_err != LDAP_SUCCESS ) {
845 * better check the type of error. In some cases
846 * (search ?) it might be better to return a
847 * success if at least one of the targets gave
848 * positive result ...
850 ldap_get_option( msc->msc_ld,
851 LDAP_OPT_ERROR_STRING, &msg );
852 if ( msg != NULL && msg[ 0 ] == '\0' ) {
857 ldap_get_option( msc->msc_ld,
858 LDAP_OPT_MATCHED_DN, &match );
859 if ( match != NULL && match[ 0 ] == '\0' ) {
860 ldap_memfree( match );
864 rs->sr_err = slap_map_api2result( rs );
866 Debug(LDAP_DEBUG_ANY,
867 "==> meta_back_op_result: target"
868 " <%d> sending msg \"%s\""
869 " (matched \"%s\")\n",
870 i, ( msg ? msg : "" ),
871 ( match ? match : "" ) );
874 * FIXME: need to rewrite "match" (need rwinfo)
876 switch ( rs->sr_err ) {
881 ldap_memfree( rmsg );
886 if ( match != NULL ) {
888 ldap_memfree( rmatch );
901 ldap_memfree( match );
908 if ( rmsg != NULL ) {
909 save_rmsg = rs->sr_text;
912 if ( rmatch != NULL ) {
913 struct berval dn, pdn;
915 ber_str2bv( rmatch, 0, 0, &dn );
916 if ( dnPretty( NULL, &dn, &pdn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
917 ldap_memfree( rmatch );
918 rmatch_ctx = op->o_tmpmemctx;
921 save_rmatch = rs->sr_matched;
922 rs->sr_matched = rmatch;
924 send_ldap_result( op, rs );
925 if ( rmsg != NULL ) {
927 rs->sr_text = save_rmsg;
929 if ( rmatch != NULL ) {
930 ber_memfree_x( rmatch, rmatch_ctx );
931 rs->sr_matched = save_rmatch;
934 return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );