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
27 #include <ac/socket.h>
28 #include <ac/string.h>
33 #include "../back-ldap/back-ldap.h"
34 #include "back-meta.h"
36 static LDAP_REBIND_PROC meta_back_rebind;
39 meta_back_single_bind(
46 meta_back_bind( Operation *op, SlapReply *rs )
48 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
52 i, gotit = 0, isroot = 0;
54 SlapReply *candidates = meta_back_candidates_get( op );
56 rs->sr_err = LDAP_SUCCESS;
58 Debug( LDAP_DEBUG_ARGS, "meta_back_bind: dn: %s.\n%s%s",
59 op->o_req_dn.bv_val, "", "" );
61 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
63 ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ) );
66 /* we need meta_back_getconn() not send result even on error,
67 * because we want to intercept the error and make it
68 * invalidCredentials */
69 mc = meta_back_getconn( op, rs, NULL, LDAP_BACK_DONTSEND );
71 Debug( LDAP_DEBUG_ANY,
72 "meta_back_bind: no target "
73 "for dn \"%s\" (%d: %s).\n",
74 op->o_req_dn.bv_val, rs->sr_err,
75 rs->sr_text ? rs->sr_text : "" );
76 /* FIXME: there might be cases where we don't want
77 * to map the error onto invalidCredentials */
78 switch ( rs->sr_err ) {
79 case LDAP_NO_SUCH_OBJECT:
80 case LDAP_UNWILLING_TO_PERFORM:
81 rs->sr_err = LDAP_INVALID_CREDENTIALS;
85 send_ldap_result( op, rs );
90 * Each target is scanned ...
92 mc->mc_auth_target = META_BOUND_NONE;
93 for ( i = 0; i < mi->mi_ntargets; i++ ) {
100 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
107 } else if ( isroot == 0 ) {
109 * A bind operation is expected to have
110 * ONE CANDIDATE ONLY!
112 Debug( LDAP_DEBUG_ANY,
113 "==>meta_back_bind: more than one"
114 " candidate is trying to bind...\n",
118 if ( isroot && !BER_BVISNULL( &mi->mi_targets[ i ]->mt_pseudorootdn ) )
120 op2.o_req_dn = mi->mi_targets[ i ]->mt_pseudorootdn;
121 op2.o_req_ndn = mi->mi_targets[ i ]->mt_pseudorootdn;
122 op2.orb_cred = mi->mi_targets[ i ]->mt_pseudorootpw;
123 op2.orb_method = LDAP_AUTH_SIMPLE;
126 lerr = meta_back_single_bind( &op2, rs, mc, i );
127 if ( lerr != LDAP_SUCCESS ) {
129 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
137 mc->mc_auth_target = META_BOUND_ALL;
141 * rc is LDAP_SUCCESS if at least one bind succeeded,
142 * err is the last error that occurred during a bind;
143 * if at least (and at most?) one bind succeedes, fine.
145 if ( rc != LDAP_SUCCESS ) {
148 * deal with bind failure ...
152 * no target was found within the naming context,
153 * so bind must fail with invalid credentials
155 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
156 rs->sr_err = LDAP_INVALID_CREDENTIALS;
159 rs->sr_err = slap_map_api2result( rs );
160 send_ldap_result( op, rs );
168 * meta_back_single_bind
170 * attempts to perform a bind with creds
173 meta_back_single_bind(
179 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
180 metatarget_t *mt = mi->mi_targets[ candidate ];
181 struct berval mdn = BER_BVNULL;
183 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
187 * Rewrite the bind dn if needed
189 dc.rwmap = &mi->mi_targets[ candidate ]->mt_rwmap;
190 dc.conn = op->o_conn;
194 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
195 send_ldap_result( op, rs );
199 /* FIXME: this fixes the bind problem right now; we need
200 * to use the asynchronous version to get the "matched"
201 * and more in case of failure ... */
202 /* FIXME: should be check if at least some of the op->o_ctrls
203 * can/should be passed? */
205 rs->sr_err = ldap_sasl_bind( msc->msc_ld, mdn.bv_val,
206 LDAP_SASL_SIMPLE, &op->orb_cred,
207 op->o_ctrls, NULL, &msgid );
208 if ( rs->sr_err == LDAP_SUCCESS ) {
212 int nretries = mt->mt_nretries;
219 tv.tv_usec = META_BIND_TIMEOUT;
220 switch ( ldap_result( msc->msc_ld, msgid, 0, &tv, &res ) ) {
222 Debug( LDAP_DEBUG_ANY, "%s meta_back_single_bind: ldap_result=%d nretries=%d\n",
223 op->o_log_prefix, 0, nretries );
225 if ( nretries != META_RETRY_NEVER ) {
226 ldap_pvt_thread_yield();
227 if ( nretries > 0 ) {
232 rs->sr_err = LDAP_BUSY;
236 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
239 Debug( LDAP_DEBUG_ANY, "### %s meta_back_single_bind: err=%d nretries=%d\n",
240 op->o_log_prefix, rs->sr_err, nretries );
242 rc = slap_map_api2result( rs );
243 if ( rs->sr_err == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
244 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
248 /* mc here must be the regular mc, reset and ready for init */
249 rc = meta_back_init_one_conn( op, rs, mt, msc,
250 LDAP_BACK_DONTSEND );
252 if ( nretries > 0 ) {
255 ldap_pvt_thread_yield();
262 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
263 NULL, NULL, NULL, NULL, 1 );
264 if ( rc != LDAP_SUCCESS ) {
271 if ( rs->sr_err != LDAP_SUCCESS ) {
272 rs->sr_err = slap_map_api2result( rs );
276 ber_bvreplace( &msc->msc_bound_ndn, &op->o_req_dn );
277 msc->msc_bound = META_BOUND;
278 mc->mc_auth_target = candidate;
280 if ( LDAP_BACK_SAVECRED( mi ) ) {
281 ber_bvreplace( &msc->msc_cred, &op->orb_cred );
282 ldap_set_rebind_proc( msc->msc_ld, meta_back_rebind, msc );
285 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED
286 && op->o_req_ndn.bv_len != 0 )
288 ( void )meta_dncache_update_entry( &mi->mi_cache,
289 &op->o_req_ndn, candidate );
293 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
301 * meta_back_single_dobind
304 meta_back_single_dobind(
309 ldap_back_send_t sendok,
312 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
313 metatarget_t *mt = mi->mi_targets[ candidate ];
314 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
316 struct berval cred = BER_BVC( "" );
320 * Otherwise an anonymous bind is performed
321 * (note: if the target was already bound, the anonymous
322 * bind clears the previous bind).
324 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
325 ber_memfree( msc->msc_bound_ndn.bv_val );
326 BER_BVZERO( &msc->msc_bound_ndn );
329 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
330 /* destroy sensitive data */
331 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
332 ber_memfree( msc->msc_cred.bv_val );
333 BER_BVZERO( &msc->msc_cred );
336 /* FIXME: should we check if at least some of the op->o_ctrls
337 * can/should be passed? */
339 rc = ldap_sasl_bind( msc->msc_ld, "", LDAP_SASL_SIMPLE, &cred,
340 NULL, NULL, &msgid );
341 if ( rc == LDAP_SUCCESS ) {
350 tv.tv_usec = META_BIND_TIMEOUT;
351 switch ( ldap_result( msc->msc_ld, msgid, 0, &tv, &res ) ) {
353 Debug( LDAP_DEBUG_ANY, "%s meta_back_single_dobind: ldap_result=%d nretries=%d\n",
354 op->o_log_prefix, 0, nretries );
356 if ( nretries != META_RETRY_NEVER ) {
357 ldap_pvt_thread_yield();
358 if ( nretries > 0 ) {
368 ldap_get_option( msc->msc_ld,
369 LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
371 Debug( LDAP_DEBUG_ANY, "### %s meta_back_single_dobind: err=%d nretries=%d\n",
372 op->o_log_prefix, rs->sr_err, nretries );
374 rc = slap_map_api2result( rs );
375 if ( rc == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
376 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
380 /* mc here must be the regular mc, reset and ready for init */
381 rc = meta_back_init_one_conn( op, rs, mt, msc, LDAP_BACK_DONTSEND );
383 if ( rc == LDAP_SUCCESS ) {
384 ldap_pvt_thread_yield();
385 if ( nretries > 0 ) {
394 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
395 NULL, NULL, NULL, NULL, 1 );
396 if ( rc == LDAP_SUCCESS ) {
397 rc = slap_map_api2result( rs );
404 if ( rc != LDAP_SUCCESS && ( sendok & LDAP_BACK_SENDERR ) ) {
405 send_ldap_result( op, rs );
419 ldap_back_send_t sendok )
421 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
423 metasingleconn_t *msc;
426 SlapReply *candidates = meta_back_candidates_get( op );
428 ldap_pvt_thread_mutex_lock( &mc->mc_mutex );
431 * all the targets are bound as pseudoroot
433 if ( mc->mc_auth_target == META_BOUND_ALL ) {
438 for ( i = 0, msc = &mc->mc_conns[ 0 ]; !META_LAST( msc ); ++i, ++msc ) {
439 metatarget_t *mt = mi->mi_targets[ i ];
443 * Not a candidate or something wrong with this target ...
445 if ( msc->msc_ld == NULL ) {
450 * If the target is already bound it is skipped
452 if ( msc->msc_bound == META_BOUND && mc->mc_auth_target == i ) {
457 rc = meta_back_single_dobind( op, rs, mc, i,
458 LDAP_BACK_DONTSEND, mt->mt_nretries );
459 if ( rc != LDAP_SUCCESS ) {
460 Debug( LDAP_DEBUG_ANY, "%s meta_back_dobind[%d]: "
461 "(anonymous) err=%d\n",
462 op->o_log_prefix, i, rc );
465 * null cred bind should always succeed
466 * as anonymous, so a failure means
467 * the target is no longer candidate possibly
468 * due to technical reasons (remote host down?)
469 * so better clear the handle
471 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
473 ( void )meta_clear_one_candidate( msc );
478 candidates[ i ].sr_tag = META_CANDIDATE;
479 msc->msc_bound = META_ANONYMOUS;
484 ldap_pvt_thread_mutex_unlock( &mc->mc_mutex );
486 if ( bound == 0 && sendok & LDAP_BACK_SENDERR ) {
487 if ( rs->sr_err == LDAP_SUCCESS ) {
488 rs->sr_err = LDAP_BUSY;
490 send_ldap_result( op, rs );
499 * This is a callback used for chasing referrals using the same
500 * credentials as the original user on this session.
505 LDAP_CONST char *url,
510 metasingleconn_t *msc = ( metasingleconn_t * )params;
512 return ldap_sasl_bind_s( ld, msc->msc_bound_ndn.bv_val,
513 LDAP_SASL_SIMPLE, &msc->msc_cred,
518 * FIXME: error return must be handled in a cleaner way ...
529 metasingleconn_t *msc;
535 if ( candidate != META_TARGET_NONE ) {
536 msc = &mc->mc_conns[ candidate ];
538 rs->sr_err = LDAP_SUCCESS;
540 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
541 if ( rs->sr_err != LDAP_SUCCESS ) {
543 * better check the type of error. In some cases
544 * (search ?) it might be better to return a
545 * success if at least one of the targets gave
546 * positive result ...
548 ldap_get_option( msc->msc_ld,
549 LDAP_OPT_ERROR_STRING, &rmsg );
550 ldap_get_option( msc->msc_ld,
551 LDAP_OPT_MATCHED_DN, &rmatch );
552 rerr = rs->sr_err = slap_map_api2result( rs );
561 Debug(LDAP_DEBUG_ANY,
562 "==> meta_back_op_result: target"
563 " <%d> sending msg \"%s\""
564 " (matched \"%s\")\n",
565 candidate, ( rmsg ? rmsg : "" ),
566 ( rmatch ? rmatch : "" ) );
570 for ( i = 0, msc = &mc->mc_conns[ 0 ]; !META_LAST( msc ); ++i, ++msc ) {
574 rs->sr_err = LDAP_SUCCESS;
576 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
577 if ( rs->sr_err != LDAP_SUCCESS ) {
579 * better check the type of error. In some cases
580 * (search ?) it might be better to return a
581 * success if at least one of the targets gave
582 * positive result ...
584 ldap_get_option( msc->msc_ld,
585 LDAP_OPT_ERROR_STRING, &msg );
586 ldap_get_option( msc->msc_ld,
587 LDAP_OPT_MATCHED_DN, &match );
588 rs->sr_err = slap_map_api2result( rs );
590 Debug(LDAP_DEBUG_ANY,
591 "==> meta_back_op_result: target"
592 " <%d> sending msg \"%s\""
593 " (matched \"%s\")\n",
594 i, ( msg ? msg : "" ),
595 ( match ? match : "" ) );
598 * FIXME: need to rewrite "match" (need rwinfo)
600 switch ( rs->sr_err ) {
610 ber_memfree( rmatch );
618 /* better test the pointers before freeing? */
631 rs->sr_matched = rmatch;
632 send_ldap_result( op, rs );
637 ber_memfree( rmatch );
640 rs->sr_matched = NULL;
642 return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );