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;
138 mc->mc_auth_target = META_BOUND_ALL;
141 meta_back_release_conn( op, mc );
144 * rc is LDAP_SUCCESS if at least one bind succeeded,
145 * err is the last error that occurred during a bind;
146 * if at least (and at most?) one bind succeedes, fine.
148 if ( rc != LDAP_SUCCESS ) {
151 * deal with bind failure ...
155 * no target was found within the naming context,
156 * so bind must fail with invalid credentials
158 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
159 rs->sr_err = LDAP_INVALID_CREDENTIALS;
162 rs->sr_err = slap_map_api2result( rs );
163 send_ldap_result( op, rs );
171 * meta_back_single_bind
173 * attempts to perform a bind with creds
176 meta_back_single_bind(
182 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
183 metatarget_t *mt = &mi->mi_targets[ candidate ];
184 struct berval mdn = BER_BVNULL;
186 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
191 * Rewrite the bind dn if needed
193 dc.target = &mi->mi_targets[ candidate ];
194 dc.conn = op->o_conn;
198 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
199 send_ldap_result( op, rs );
203 /* FIXME: this fixes the bind problem right now; we need
204 * to use the asynchronous version to get the "matched"
205 * and more in case of failure ... */
206 /* FIXME: should be check if at least some of the op->o_ctrls
207 * can/should be passed? */
209 rs->sr_err = ldap_sasl_bind( msc->msc_ld, mdn.bv_val,
210 LDAP_SASL_SIMPLE, &op->orb_cred,
211 op->o_ctrls, NULL, &msgid );
212 if ( rs->sr_err == LDAP_SUCCESS ) {
216 int nretries = mt->mt_nretries;
223 tv.tv_usec = META_BIND_TIMEOUT;
224 switch ( ldap_result( msc->msc_ld, msgid, 0, &tv, &res ) ) {
226 Debug( LDAP_DEBUG_ANY,
227 "%s meta_back_single_bind: "
228 "ldap_result=0 nretries=%d%s\n",
229 op->o_log_prefix, nretries,
230 rebinding ? " rebinding" : "" );
232 if ( nretries != META_RETRY_NEVER ) {
233 ldap_pvt_thread_yield();
234 if ( nretries > 0 ) {
239 rs->sr_err = LDAP_BUSY;
241 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
245 /* FIXME: some times the request times out
246 * while the other party is not willing to
247 * send a response any more. Give it a second
248 * chance with a freshly bound connection */
250 nretries = mt->mt_nretries;
254 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
258 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
261 Debug( LDAP_DEBUG_ANY,
262 "### %s meta_back_single_bind: "
263 "err=%d nretries=%d\n",
264 op->o_log_prefix, rs->sr_err, nretries );
266 rc = slap_map_api2result( rs );
267 if ( rs->sr_err == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
269 switch ( ldap_pvt_thread_mutex_trylock( &mi->mi_conn_mutex ) ) {
270 case LDAP_PVT_THREAD_EBUSY:
272 ldap_pvt_thread_yield();
279 if ( mc->mc_refcnt == 1 ) {
280 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
284 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
286 /* mc here must be the regular mc,
287 * reset and ready for init */
288 rc = meta_back_init_one_conn( op, rs,
289 mt, msc, LDAP_BACK_DONTSEND );
292 /* can't do anything about it */
296 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
299 if ( nretries > 0 ) {
302 ldap_pvt_thread_yield();
309 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
310 NULL, NULL, NULL, NULL, 1 );
311 if ( rc != LDAP_SUCCESS ) {
318 if ( rs->sr_err != LDAP_SUCCESS ) {
319 rs->sr_err = slap_map_api2result( rs );
323 ber_bvreplace( &msc->msc_bound_ndn, &op->o_req_dn );
324 msc->msc_bound = META_BOUND;
325 mc->mc_auth_target = candidate;
327 if ( LDAP_BACK_SAVECRED( mi ) ) {
328 ber_bvreplace( &msc->msc_cred, &op->orb_cred );
329 ldap_set_rebind_proc( msc->msc_ld, meta_back_rebind, msc );
332 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED
333 && op->o_req_ndn.bv_len != 0 )
335 ( void )meta_dncache_update_entry( &mi->mi_cache,
336 &op->o_req_ndn, candidate );
340 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
348 * meta_back_single_dobind
351 meta_back_single_dobind(
356 ldap_back_send_t sendok,
360 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
361 metatarget_t *mt = &mi->mi_targets[ candidate ];
362 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
364 struct berval cred = BER_BVC( "" );
367 save_nretries = nretries;
370 * Otherwise an anonymous bind is performed
371 * (note: if the target was already bound, the anonymous
372 * bind clears the previous bind).
374 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
375 ber_memfree( msc->msc_bound_ndn.bv_val );
376 BER_BVZERO( &msc->msc_bound_ndn );
379 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
380 /* destroy sensitive data */
381 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
382 ber_memfree( msc->msc_cred.bv_val );
383 BER_BVZERO( &msc->msc_cred );
386 /* FIXME: should we check if at least some of the op->o_ctrls
387 * can/should be passed? */
389 rc = ldap_sasl_bind( msc->msc_ld, "", LDAP_SASL_SIMPLE, &cred,
390 NULL, NULL, &msgid );
391 if ( rc == LDAP_SUCCESS ) {
400 tv.tv_usec = META_BIND_TIMEOUT;
401 switch ( ldap_result( msc->msc_ld, msgid, 0, &tv, &res ) ) {
403 Debug( LDAP_DEBUG_ANY,
404 "%s meta_back_single_dobind: "
405 "ldap_result=0 nretries=%d%s\n",
406 op->o_log_prefix, nretries,
407 rebinding ? " rebinding" : "" );
409 if ( nretries != META_RETRY_NEVER ) {
410 ldap_pvt_thread_yield();
411 if ( nretries > 0 ) {
419 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
423 /* FIXME: some times the request times out
424 * while the other party is not willing to
425 * send a response any more. Give it a second
426 * chance with a freshly bound connection */
428 nretries = save_nretries;
432 ldap_get_option( msc->msc_ld,
433 LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
436 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
439 Debug( LDAP_DEBUG_ANY,
440 "### %s meta_back_single_dobind: "
441 "err=%d nretries=%d\n",
442 op->o_log_prefix, rs->sr_err, nretries );
444 rc = slap_map_api2result( rs );
445 if ( rc == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
448 switch ( ldap_pvt_thread_mutex_trylock( &mi->mi_conn_mutex ) ) {
449 case LDAP_PVT_THREAD_EBUSY:
451 ldap_pvt_thread_yield();
459 if ( mc->mc_refcnt == 1 ) {
460 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
464 ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
466 /* mc here must be the regular mc,
467 * reset and ready for init */
468 rc = meta_back_init_one_conn( op, rs,
469 mt, msc, LDAP_BACK_DONTSEND );
473 /* can't do anything about it */
474 rc = LDAP_UNAVAILABLE;
478 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
481 if ( rc == LDAP_SUCCESS ) {
482 ldap_pvt_thread_yield();
483 if ( nretries > 0 ) {
492 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
493 NULL, NULL, NULL, NULL, 1 );
494 if ( rc == LDAP_SUCCESS ) {
495 rc = slap_map_api2result( rs );
501 rc = slap_map_api2result( rs );
505 if ( rc != LDAP_SUCCESS && ( sendok & LDAP_BACK_SENDERR ) ) {
506 send_ldap_result( op, rs );
520 ldap_back_send_t sendok )
522 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
526 SlapReply *candidates = meta_back_candidates_get( op );
528 ldap_pvt_thread_mutex_lock( &mc->mc_mutex );
530 Debug( LDAP_DEBUG_TRACE,
531 "%s meta_back_dobind: conn=%ld\n",
532 op->o_log_prefix, mc->mc_conn->c_connid, 0 );
535 * all the targets are bound as pseudoroot
537 if ( mc->mc_auth_target == META_BOUND_ALL ) {
542 for ( i = 0; i < mi->mi_ntargets; i++ ) {
543 metatarget_t *mt = &mi->mi_targets[ i ];
544 metasingleconn_t *msc = &mc->mc_conns[ i ];
550 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
554 assert( msc->msc_ld != NULL );
557 * If the target is already bound it is skipped
559 if ( msc->msc_bound == META_BOUND && mc->mc_auth_target == i ) {
562 Debug( LDAP_DEBUG_TRACE, "%s meta_back_dobind[%d]: "
564 op->o_log_prefix, i, 0 );
568 rc = meta_back_single_dobind( op, rs, mc, i,
569 LDAP_BACK_DONTSEND, mt->mt_nretries, 1 );
570 if ( rc != LDAP_SUCCESS ) {
571 rs->sr_err = slap_map_api2result( rs );
573 Debug( LDAP_DEBUG_ANY, "%s meta_back_dobind[%d]: "
574 "(anonymous) err=%d\n",
575 op->o_log_prefix, i, rc );
578 * null cred bind should always succeed
579 * as anonymous, so a failure means
580 * the target is no longer candidate possibly
581 * due to technical reasons (remote host down?)
582 * so better clear the handle
584 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
588 Debug( LDAP_DEBUG_TRACE, "%s meta_back_dobind[%d]: "
590 op->o_log_prefix, i, 0 );
592 msc->msc_bound = META_ANONYMOUS;
597 ldap_pvt_thread_mutex_unlock( &mc->mc_mutex );
599 Debug( LDAP_DEBUG_TRACE,
600 "%s meta_back_dobind: conn=%ld bound=%d\n",
601 op->o_log_prefix, mc->mc_conn->c_connid, bound );
604 meta_back_release_conn( op, mc );
606 if ( sendok & LDAP_BACK_SENDERR ) {
607 if ( rs->sr_err == LDAP_SUCCESS ) {
608 rs->sr_err = LDAP_BUSY;
610 send_ldap_result( op, rs );
620 * This is a callback used for chasing referrals using the same
621 * credentials as the original user on this session.
626 LDAP_CONST char *url,
631 metasingleconn_t *msc = ( metasingleconn_t * )params;
633 return ldap_sasl_bind_s( ld, msc->msc_bound_ndn.bv_val,
634 LDAP_SASL_SIMPLE, &msc->msc_cred,
639 * FIXME: error return must be handled in a cleaner way ...
648 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
657 if ( candidate != META_TARGET_NONE ) {
658 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
660 rs->sr_err = LDAP_SUCCESS;
662 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
663 if ( rs->sr_err != LDAP_SUCCESS ) {
665 * better check the type of error. In some cases
666 * (search ?) it might be better to return a
667 * success if at least one of the targets gave
668 * positive result ...
670 ldap_get_option( msc->msc_ld,
671 LDAP_OPT_ERROR_STRING, &rmsg );
672 ldap_get_option( msc->msc_ld,
673 LDAP_OPT_MATCHED_DN, &rmatch );
674 rerr = rs->sr_err = slap_map_api2result( rs );
683 Debug(LDAP_DEBUG_ANY,
684 "==> meta_back_op_result: target"
685 " <%d> sending msg \"%s\""
686 " (matched \"%s\")\n",
687 candidate, ( rmsg ? rmsg : "" ),
688 ( rmatch ? rmatch : "" ) );
692 for ( i = 0; i < mi->mi_ntargets; i++ ) {
693 metasingleconn_t *msc = &mc->mc_conns[ i ];
697 rs->sr_err = LDAP_SUCCESS;
699 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
700 if ( rs->sr_err != LDAP_SUCCESS ) {
702 * better check the type of error. In some cases
703 * (search ?) it might be better to return a
704 * success if at least one of the targets gave
705 * positive result ...
707 ldap_get_option( msc->msc_ld,
708 LDAP_OPT_ERROR_STRING, &msg );
709 ldap_get_option( msc->msc_ld,
710 LDAP_OPT_MATCHED_DN, &match );
711 rs->sr_err = slap_map_api2result( rs );
713 Debug(LDAP_DEBUG_ANY,
714 "==> meta_back_op_result: target"
715 " <%d> sending msg \"%s\""
716 " (matched \"%s\")\n",
717 i, ( msg ? msg : "" ),
718 ( match ? match : "" ) );
721 * FIXME: need to rewrite "match" (need rwinfo)
723 switch ( rs->sr_err ) {
733 ber_memfree( rmatch );
741 /* better test the pointers before freeing? */
754 rs->sr_matched = rmatch;
755 send_ldap_result( op, rs );
760 ber_memfree( rmatch );
763 rs->sr_matched = NULL;
765 return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );