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 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
385 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
387 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
389 /* mc here must be the regular mc,
390 * reset and ready for init */
391 rc = meta_back_init_one_conn( op, rs,
392 mt, mc, msc, LDAP_BACK_CONN_ISPRIV( mc ),
393 candidate == mc->mc_authz_target,
394 LDAP_BACK_DONTSEND );
397 /* can't do anything about it */
401 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
404 if ( nretries > 0 ) {
407 ldap_pvt_thread_yield();
414 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
415 NULL, NULL, NULL, NULL, 1 );
416 if ( rc != LDAP_SUCCESS ) {
423 if ( rs->sr_err != LDAP_SUCCESS ) {
424 rs->sr_err = slap_map_api2result( rs );
428 ber_bvreplace( &msc->msc_bound_ndn, &op->o_req_dn );
429 LDAP_BACK_CONN_ISBOUND_SET( msc );
430 mc->mc_authz_target = candidate;
432 if ( LDAP_BACK_SAVECRED( mi ) ) {
433 ber_bvreplace( &msc->msc_cred, &op->orb_cred );
434 ldap_set_rebind_proc( msc->msc_ld, meta_back_rebind_f, msc );
437 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED
438 && op->o_req_ndn.bv_len != 0 )
440 ( void )meta_dncache_update_entry( &mi->mi_cache,
441 &op->o_req_ndn, candidate );
445 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
453 * meta_back_single_dobind
456 meta_back_single_dobind(
461 ldap_back_send_t sendok,
465 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
466 metatarget_t *mt = &mi->mi_targets[ candidate ];
467 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
469 static struct berval cred = BER_BVC( "" );
472 save_nretries = nretries;
474 assert( !LDAP_BACK_CONN_ISBOUND( msc ) );
477 * Otherwise an anonymous bind is performed
478 * (note: if the target was already bound, the anonymous
479 * bind clears the previous bind).
481 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
482 ber_memfree( msc->msc_bound_ndn.bv_val );
483 BER_BVZERO( &msc->msc_bound_ndn );
486 if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &msc->msc_cred ) ) {
487 /* destroy sensitive data */
488 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
489 ber_memfree( msc->msc_cred.bv_val );
490 BER_BVZERO( &msc->msc_cred );
493 /* FIXME: should we check if at least some of the op->o_ctrls
494 * can/should be passed? */
496 rc = ldap_sasl_bind( msc->msc_ld, "", LDAP_SASL_SIMPLE, &cred,
497 NULL, NULL, &msgid );
498 if ( rc == LDAP_SUCCESS ) {
502 LDAP_BACK_TV_SET( &tv );
509 tv.tv_usec = META_BIND_TIMEOUT;
510 switch ( ldap_result( msc->msc_ld, msgid, 0, &tv, &res ) ) {
512 Debug( LDAP_DEBUG_ANY,
513 "%s meta_back_single_dobind: "
514 "ldap_result=0 nretries=%d%s\n",
515 op->o_log_prefix, nretries,
516 rebinding ? " rebinding" : "" );
518 if ( nretries != META_RETRY_NEVER ) {
519 ldap_pvt_thread_yield();
520 if ( nretries > 0 ) {
523 LDAP_BACK_TV_SET( &tv );
529 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
533 /* FIXME: some times the request times out
534 * while the other party is not willing to
535 * send a response any more. Give it a second
536 * chance with a freshly bound connection */
538 nretries = save_nretries;
542 ldap_get_option( msc->msc_ld,
543 LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
546 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
549 Debug( LDAP_DEBUG_ANY,
550 "### %s meta_back_single_dobind: "
551 "err=%d nretries=%d\n",
552 op->o_log_prefix, rs->sr_err, nretries );
554 rc = slap_map_api2result( rs );
555 if ( rc == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
557 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
560 if ( mc->mc_refcnt == 1 ) {
561 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
563 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
565 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
567 /* mc here must be the regular mc,
568 * reset and ready for init */
569 rc = meta_back_init_one_conn( op, rs,
571 LDAP_BACK_CONN_ISPRIV( mc ),
572 candidate == mc->mc_authz_target,
573 LDAP_BACK_DONTSEND );
577 /* can't do anything about it */
578 rc = LDAP_UNAVAILABLE;
582 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
585 if ( rc == LDAP_SUCCESS ) {
586 ldap_pvt_thread_yield();
587 if ( nretries > 0 ) {
596 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
597 NULL, NULL, NULL, NULL, 1 );
598 if ( rc == LDAP_SUCCESS ) {
599 rc = slap_map_api2result( rs );
606 rc = slap_map_api2result( rs );
610 if ( rc != LDAP_SUCCESS && ( sendok & LDAP_BACK_SENDERR ) ) {
611 send_ldap_result( op, rs );
625 ldap_back_send_t sendok )
627 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
633 SlapReply *candidates = meta_back_candidates_get( op );
635 if ( be_isroot( op ) ) {
639 Debug( LDAP_DEBUG_TRACE,
640 "%s meta_back_dobind: conn=%ld%s\n",
642 LDAP_BACK_PCONN_ID( mc->mc_conn ),
643 isroot ? " (isroot)" : "" );
645 ldap_pvt_thread_mutex_lock( &mc->mc_mutex );
648 * all the targets are bound as pseudoroot
650 if ( mc->mc_authz_target == META_BOUND_ALL ) {
655 for ( i = 0; i < mi->mi_ntargets; i++ ) {
656 metatarget_t *mt = &mi->mi_targets[ i ];
657 metasingleconn_t *msc = &mc->mc_conns[ i ];
664 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
668 assert( msc->msc_ld != NULL );
671 * If the target is already bound it is skipped
673 if ( LDAP_BACK_CONN_ISBOUND( msc ) || LDAP_BACK_CONN_ISANON( msc ) ) {
678 if ( isroot && !BER_BVISNULL( &mi->mi_targets[ i ].mt_pseudorootdn ) )
682 op2.o_tag = LDAP_REQ_BIND;
683 op2.o_req_dn = mi->mi_targets[ i ].mt_pseudorootdn;
684 op2.o_req_ndn = mi->mi_targets[ i ].mt_pseudorootdn;
685 op2.orb_cred = mi->mi_targets[ i ].mt_pseudorootpw;
686 op2.orb_method = LDAP_AUTH_SIMPLE;
688 rootdn = mi->mi_targets[ i ].mt_pseudorootdn.bv_val;
690 rc = meta_back_single_bind( &op2, rs, mc, i );
693 rc = meta_back_single_dobind( op, rs, mc, i,
694 LDAP_BACK_DONTSEND, mt->mt_nretries, 1 );
697 if ( rc != LDAP_SUCCESS ) {
698 char buf[ SLAP_TEXT_BUFLEN ];
700 snprintf( buf, sizeof( buf ),
701 "meta_back_dobind[%d]: (%s) err=%d.",
702 i, rootdn ? rootdn : "anonymous", rc );
703 Debug( LDAP_DEBUG_ANY,
705 op->o_log_prefix, buf, 0 );
708 * null cred bind should always succeed
709 * as anonymous, so a failure means
710 * the target is no longer candidate possibly
711 * due to technical reasons (remote host down?)
712 * so better clear the handle
714 /* leave the target candidate, but record the error for later use */
715 candidates[ i ].sr_err = rc;
716 if ( META_BACK_ONERR_STOP( mi ) ) {
723 Debug( LDAP_DEBUG_TRACE,
724 "%s meta_back_dobind[%d]: "
727 rootdn ? rootdn : "anonymous" );
730 LDAP_BACK_CONN_ISBOUND_SET( msc );
732 LDAP_BACK_CONN_ISANON_SET( msc );
738 ldap_pvt_thread_mutex_unlock( &mc->mc_mutex );
740 Debug( LDAP_DEBUG_TRACE,
741 "%s meta_back_dobind: conn=%ld bound=%d\n",
742 op->o_log_prefix, LDAP_BACK_PCONN_ID( mc->mc_conn ), bound );
745 meta_back_release_conn( op, mc );
747 if ( sendok & LDAP_BACK_SENDERR ) {
748 if ( rs->sr_err == LDAP_SUCCESS ) {
749 rs->sr_err = LDAP_BUSY;
751 send_ldap_result( op, rs );
759 * meta_back_default_rebind
761 * This is a callback used for chasing referrals using the same
762 * credentials as the original user on this session.
765 meta_back_default_rebind(
767 LDAP_CONST char *url,
772 metasingleconn_t *msc = ( metasingleconn_t * )params;
774 return ldap_sasl_bind_s( ld, msc->msc_bound_ndn.bv_val,
775 LDAP_SASL_SIMPLE, &msc->msc_cred,
780 * FIXME: error return must be handled in a cleaner way ...
789 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
795 const char *save_rmsg = NULL,
797 void *rmatch_ctx = NULL;
799 if ( candidate != META_TARGET_NONE ) {
800 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
802 rs->sr_err = LDAP_SUCCESS;
804 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
805 if ( rs->sr_err != LDAP_SUCCESS ) {
807 * better check the type of error. In some cases
808 * (search ?) it might be better to return a
809 * success if at least one of the targets gave
810 * positive result ...
812 ldap_get_option( msc->msc_ld,
813 LDAP_OPT_ERROR_STRING, &rmsg );
814 if ( rmsg != NULL && rmsg[ 0 ] == '\0' ) {
815 ldap_memfree( rmsg );
819 ldap_get_option( msc->msc_ld,
820 LDAP_OPT_MATCHED_DN, &rmatch );
821 if ( rmatch != NULL && rmatch[ 0 ] == '\0' ) {
822 ldap_memfree( rmatch );
826 rerr = rs->sr_err = slap_map_api2result( rs );
828 Debug(LDAP_DEBUG_ANY,
829 "==> meta_back_op_result: target"
830 " <%d> sending msg \"%s\""
831 " (matched \"%s\")\n",
832 candidate, ( rmsg ? rmsg : "" ),
833 ( rmatch ? rmatch : "" ) );
837 for ( i = 0; i < mi->mi_ntargets; i++ ) {
838 metasingleconn_t *msc = &mc->mc_conns[ i ];
842 rs->sr_err = LDAP_SUCCESS;
844 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
845 if ( rs->sr_err != LDAP_SUCCESS ) {
847 * better check the type of error. In some cases
848 * (search ?) it might be better to return a
849 * success if at least one of the targets gave
850 * positive result ...
852 ldap_get_option( msc->msc_ld,
853 LDAP_OPT_ERROR_STRING, &msg );
854 if ( msg != NULL && msg[ 0 ] == '\0' ) {
859 ldap_get_option( msc->msc_ld,
860 LDAP_OPT_MATCHED_DN, &match );
861 if ( match != NULL && match[ 0 ] == '\0' ) {
862 ldap_memfree( match );
866 rs->sr_err = slap_map_api2result( rs );
868 Debug(LDAP_DEBUG_ANY,
869 "==> meta_back_op_result: target"
870 " <%d> sending msg \"%s\""
871 " (matched \"%s\")\n",
872 i, ( msg ? msg : "" ),
873 ( match ? match : "" ) );
876 * FIXME: need to rewrite "match" (need rwinfo)
878 switch ( rs->sr_err ) {
883 ldap_memfree( rmsg );
888 if ( match != NULL ) {
890 ldap_memfree( rmatch );
903 ldap_memfree( match );
910 if ( rmsg != NULL ) {
911 save_rmsg = rs->sr_text;
914 if ( rmatch != NULL ) {
915 struct berval dn, pdn;
917 ber_str2bv( rmatch, 0, 0, &dn );
918 if ( dnPretty( NULL, &dn, &pdn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
919 ldap_memfree( rmatch );
920 rmatch_ctx = op->o_tmpmemctx;
923 save_rmatch = rs->sr_matched;
924 rs->sr_matched = rmatch;
926 send_ldap_result( op, rs );
927 if ( rmsg != NULL ) {
929 rs->sr_text = save_rmsg;
931 if ( rmatch != NULL ) {
932 ber_memfree_x( rmatch, rmatch_ctx );
933 rs->sr_matched = save_rmatch;
936 return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );