2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2006 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 #include "lutil_ldap.h"
40 meta_back_proxy_authz_bind(
45 ldap_back_send_t sendok );
48 meta_back_single_bind(
55 meta_back_bind( Operation *op, SlapReply *rs )
57 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
58 metaconn_t *mc = NULL;
65 SlapReply *candidates = meta_back_candidates_get( op );
67 rs->sr_err = LDAP_SUCCESS;
69 Debug( LDAP_DEBUG_ARGS, "%s meta_back_bind: dn=\"%s\".\n",
70 op->o_log_prefix, op->o_req_dn.bv_val, 0 );
72 /* the test on the bind method should be superfluous */
73 if ( op->orb_method == LDAP_AUTH_SIMPLE
74 && be_isroot_dn( op->o_bd, &op->o_req_ndn ) )
76 if ( !be_isroot_pw( op ) ) {
77 rs->sr_err = LDAP_INVALID_CREDENTIALS;
79 send_ldap_result( op, rs );
83 if ( META_BACK_DEFER_ROOTDN_BIND( mi ) ) {
84 rs->sr_err = LDAP_SUCCESS;
86 /* frontend will return success */
93 /* we need meta_back_getconn() not send result even on error,
94 * because we want to intercept the error and make it
95 * invalidCredentials */
96 mc = meta_back_getconn( op, rs, NULL, LDAP_BACK_BIND_DONTSEND );
98 if ( LogTest( LDAP_DEBUG_ANY ) ) {
99 char buf[ SLAP_TEXT_BUFLEN ];
101 snprintf( buf, sizeof( buf ),
102 "meta_back_bind: no target "
103 "for dn \"%s\" (%d%s%s).",
104 op->o_req_dn.bv_val, rs->sr_err,
105 rs->sr_text ? ". " : "",
106 rs->sr_text ? rs->sr_text : "" );
107 Debug( LDAP_DEBUG_ANY,
109 op->o_log_prefix, buf, 0 );
112 /* FIXME: there might be cases where we don't want
113 * to map the error onto invalidCredentials */
114 switch ( rs->sr_err ) {
115 case LDAP_NO_SUCH_OBJECT:
116 case LDAP_UNWILLING_TO_PERFORM:
117 rs->sr_err = LDAP_INVALID_CREDENTIALS;
121 send_ldap_result( op, rs );
126 * Each target is scanned ...
128 mc->mc_authz_target = META_BOUND_NONE;
129 for ( i = 0; i < mi->mi_ntargets; i++ ) {
130 metatarget_t *mt = mi->mi_targets[ i ];
134 * Skip non-candidates
136 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
141 /* set rc to LDAP_SUCCESS only if at least
142 * one candidate has been tried */
146 } else if ( isroot == 0 ) {
148 * A bind operation is expected to have
149 * ONE CANDIDATE ONLY!
151 Debug( LDAP_DEBUG_ANY,
152 "### %s meta_back_bind: more than one"
153 " candidate selected...\n",
154 op->o_log_prefix, 0, 0 );
158 if ( mt->mt_idassert_authmethod == LDAP_AUTH_NONE
159 || BER_BVISNULL( &mt->mt_idassert_authcDN ) )
161 metasingleconn_t *msc = &mc->mc_conns[ i ];
163 /* skip the target if no pseudorootdn is provided */
164 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
165 ch_free( msc->msc_bound_ndn.bv_val );
166 BER_BVZERO( &msc->msc_bound_ndn );
169 if ( LDAP_BACK_SAVECRED( mi ) &&
170 !BER_BVISNULL( &msc->msc_cred ) )
172 /* destroy sensitive data */
173 memset( msc->msc_cred.bv_val, 0,
174 msc->msc_cred.bv_len );
175 ch_free( msc->msc_cred.bv_val );
176 BER_BVZERO( &msc->msc_cred );
183 (void)meta_back_proxy_authz_bind( mc, i, op, rs, LDAP_BACK_DONTSEND );
187 lerr = meta_back_single_bind( op, rs, mc, i );
190 if ( lerr != LDAP_SUCCESS ) {
191 rc = rs->sr_err = lerr;
192 /* FIXME: in some cases (e.g. unavailable)
193 * do not assume it's not candidate; rather
194 * mark this as an error to be eventually
195 * reported to client */
196 META_CANDIDATE_CLEAR( &candidates[ i ] );
201 /* must re-insert if local DN changed as result of bind */
202 if ( rc == LDAP_SUCCESS ) {
204 mc->mc_authz_target = META_BOUND_ALL;
205 ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ) );
208 if ( !dn_match( &op->o_req_ndn, &mc->mc_local_ndn ) ) {
212 /* wait for all other ops to release the connection */
214 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
215 if ( mc->mc_refcnt > 1 ) {
216 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
217 ldap_pvt_thread_yield();
221 assert( mc->mc_refcnt == 1 );
222 tmpmc = avl_delete( &mi->mi_conninfo.lai_tree, (caddr_t)mc,
223 meta_back_conndn_cmp );
224 assert( tmpmc == mc );
226 /* delete all cached connections with the current connection */
227 if ( LDAP_BACK_SINGLECONN( mi ) ) {
228 while ( ( tmpmc = avl_delete( &mi->mi_conninfo.lai_tree, (caddr_t)mc, meta_back_conn_cmp ) ) != NULL )
230 Debug( LDAP_DEBUG_TRACE,
231 "=>meta_back_bind: destroying conn %ld (refcnt=%u)\n",
232 LDAP_BACK_PCONN_ID( mc->mc_conn ), mc->mc_refcnt, 0 );
234 if ( tmpmc->mc_refcnt != 0 ) {
236 LDAP_BACK_CONN_TAINTED_SET( tmpmc );
240 * Needs a test because the handler may be corrupted,
241 * and calling ldap_unbind on a corrupted header results
242 * in a segmentation fault
244 meta_back_conn_free( tmpmc );
249 ber_bvreplace( &mc->mc_local_ndn, &op->o_req_ndn );
251 mc->mc_conn = LDAP_BACK_PCONN_SET( op );
253 lerr = avl_insert( &mi->mi_conninfo.lai_tree, (caddr_t)mc,
254 meta_back_conndn_cmp, meta_back_conndn_dup );
255 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
257 meta_clear_candidates( op, mc );
259 /* we can do this because mc_refcnt == 1 */
261 meta_back_conn_free( mc );
268 meta_back_release_conn( op, mc );
272 * rc is LDAP_SUCCESS if at least one bind succeeded,
273 * err is the last error that occurred during a bind;
274 * if at least (and at most?) one bind succeeds, fine.
276 if ( rc != LDAP_SUCCESS ) {
279 * deal with bind failure ...
283 * no target was found within the naming context,
284 * so bind must fail with invalid credentials
286 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
287 rs->sr_err = LDAP_INVALID_CREDENTIALS;
289 rs->sr_err = slap_map_api2result( rs );
291 send_ldap_result( op, rs );
300 meta_back_bind_op_result(
306 ldap_back_send_t sendok )
308 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
309 metatarget_t *mt = mi->mi_targets[ candidate ];
310 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
314 int nretries = mt->mt_nretries;
315 char buf[ SLAP_TEXT_BUFLEN ];
317 Debug( LDAP_DEBUG_TRACE,
318 ">>> %s meta_back_bind_op_result[%d]\n",
319 op->o_log_prefix, candidate, 0 );
321 if ( rs->sr_err == LDAP_SUCCESS ) {
322 time_t stoptime = (time_t)(-1),
324 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
325 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
326 const char *timeout_text = "Operation timed out";
327 slap_op_t opidx = slap_req2op( op->o_tag );
329 /* since timeout is not specified, compute and use
330 * the one specific to the ongoing operation */
331 if ( opidx == LDAP_REQ_SEARCH ) {
332 if ( op->ors_tlimit <= 0 ) {
336 timeout = op->ors_tlimit;
337 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
342 timeout = mt->mt_timeout[ opidx ];
345 /* better than nothing :) */
346 if ( timeout == 0 ) {
347 if ( mi->mi_idle_timeout ) {
348 timeout = mi->mi_idle_timeout;
350 } else if ( mi->mi_conn_ttl ) {
351 timeout = mi->mi_conn_ttl;
356 stoptime = op->o_time + timeout;
359 LDAP_BACK_TV_SET( &tv );
365 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
369 Debug( LDAP_DEBUG_ANY,
370 "%s meta_back_bind_op_result[%d]: ldap_result=0 nretries=%d.\n",
371 op->o_log_prefix, candidate, nretries );
374 if ( nretries != META_RETRY_NEVER
375 || ( timeout && slap_get_time() <= stoptime ) )
377 ldap_pvt_thread_yield();
378 if ( nretries > 0 ) {
381 tv = mt->mt_bind_timeout;
385 /* don't let anyone else use this handler,
386 * because there's a pending bind that will not
388 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
389 ldap_unbind_ext( msc->msc_ld, NULL, NULL );
391 LDAP_BACK_CONN_BINDING_CLEAR( msc );
392 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
394 rs->sr_err = timeout_err;
395 rs->sr_text = timeout_text;
399 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
402 snprintf( buf, sizeof( buf ),
403 "err=%d (%s) nretries=%d",
404 rs->sr_err, ldap_err2string( rs->sr_err ), nretries );
405 Debug( LDAP_DEBUG_ANY,
406 "### %s meta_back_bind_op_result[%d]: %s.\n",
407 op->o_log_prefix, candidate, buf );
411 /* only touch when activity actually took place... */
412 if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
413 msc->msc_time = op->o_time;
416 /* FIXME: matched? referrals? response controls? */
417 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
418 NULL, NULL, NULL, NULL, 1 );
419 if ( rc != LDAP_SUCCESS ) {
426 rs->sr_err = slap_map_api2result( rs );
428 Debug( LDAP_DEBUG_TRACE,
429 "<<< %s meta_back_bind_op_result[%d] err=%d\n",
430 op->o_log_prefix, candidate, rs->sr_err );
436 * meta_back_single_bind
438 * attempts to perform a bind with creds
441 meta_back_single_bind(
447 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
448 metatarget_t *mt = mi->mi_targets[ candidate ];
449 struct berval mdn = BER_BVNULL;
450 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
454 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
455 ch_free( msc->msc_bound_ndn.bv_val );
456 BER_BVZERO( &msc->msc_bound_ndn );
459 if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &msc->msc_cred ) ) {
460 /* destroy sensitive data */
461 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
462 ch_free( msc->msc_cred.bv_val );
463 BER_BVZERO( &msc->msc_cred );
467 * Rewrite the bind dn if needed
470 dc.conn = op->o_conn;
474 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
475 rs->sr_text = "DN rewrite error";
476 rs->sr_err = LDAP_OTHER;
480 /* FIXME: this fixes the bind problem right now; we need
481 * to use the asynchronous version to get the "matched"
482 * and more in case of failure ... */
483 /* FIXME: should we check if at least some of the op->o_ctrls
484 * can/should be passed? */
485 rs->sr_err = ldap_sasl_bind( msc->msc_ld, mdn.bv_val,
486 LDAP_SASL_SIMPLE, &op->orb_cred,
487 op->o_ctrls, NULL, &msgid );
488 meta_back_bind_op_result( op, rs, mc, candidate, msgid, LDAP_BACK_DONTSEND );
489 if ( rs->sr_err != LDAP_SUCCESS ) {
493 /* If defined, proxyAuthz will be used also when
494 * back-ldap is the authorizing backend; for this
495 * purpose, a successful bind is followed by a
496 * bind with the configured identity assertion */
497 /* NOTE: use with care */
498 if ( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
499 meta_back_proxy_authz_bind( mc, candidate, op, rs, LDAP_BACK_SENDERR );
500 if ( !LDAP_BACK_CONN_ISBOUND( msc ) ) {
506 ber_bvreplace( &msc->msc_bound_ndn, &op->o_req_ndn );
507 LDAP_BACK_CONN_ISBOUND_SET( msc );
508 mc->mc_authz_target = candidate;
510 if ( LDAP_BACK_SAVECRED( mi ) ) {
511 ber_bvreplace( &msc->msc_cred, &op->orb_cred );
512 ldap_set_rebind_proc( msc->msc_ld, mt->mt_rebind_f, msc );
516 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED
517 && !BER_BVISEMPTY( &op->o_req_ndn ) )
519 ( void )meta_dncache_update_entry( &mi->mi_cache,
520 &op->o_req_ndn, candidate );
524 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
528 if ( META_BACK_TGT_QUARANTINE( mt ) ) {
529 meta_back_quarantine( op, rs, candidate );
536 * meta_back_single_dobind
539 meta_back_single_dobind(
544 ldap_back_send_t sendok,
548 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
549 metatarget_t *mt = mi->mi_targets[ candidate ];
550 metaconn_t *mc = *mcp;
551 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
553 static struct berval cred = BER_BVC( "" );
556 assert( !LDAP_BACK_CONN_ISBOUND( msc ) );
558 /* NOTE: this obsoletes pseudorootdn */
559 if ( op->o_conn != NULL &&
560 !op->o_do_not_cache &&
561 ( BER_BVISNULL( &msc->msc_bound_ndn ) ||
562 BER_BVISEMPTY( &msc->msc_bound_ndn ) ||
563 ( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
565 (void)meta_back_proxy_authz_bind( mc, candidate, op, rs, sendok );
570 /* FIXME: should we check if at least some of the op->o_ctrls
571 * can/should be passed? */
572 rs->sr_err = ldap_sasl_bind( msc->msc_ld, "", LDAP_SASL_SIMPLE, &cred,
573 NULL, NULL, &msgid );
574 rc = meta_back_bind_op_result( op, rs, mc, candidate, msgid, sendok );
578 if ( rc != LDAP_SUCCESS ) {
580 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
582 LDAP_BACK_CONN_BINDING_CLEAR( msc );
583 if ( META_BACK_ONERR_STOP( mi ) ) {
584 LDAP_BACK_CONN_TAINTED_SET( mc );
585 meta_back_release_conn_lock( op, mc, 0 );
589 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
592 if ( META_BACK_ONERR_STOP( mi ) && ( sendok & LDAP_BACK_SENDERR ) ) {
593 send_ldap_result( op, rs );
597 if ( META_BACK_TGT_QUARANTINE( mt ) ) {
598 meta_back_quarantine( op, rs, candidate );
612 ldap_back_send_t sendok )
614 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
620 SlapReply *candidates = meta_back_candidates_get( op );
622 if ( be_isroot( op ) ) {
626 Debug( LDAP_DEBUG_TRACE,
627 "%s meta_back_dobind: conn=%ld%s\n",
629 LDAP_BACK_PCONN_ID( mc->mc_conn ),
630 isroot ? " (isroot)" : "" );
633 * all the targets are bound as pseudoroot
635 if ( mc->mc_authz_target == META_BOUND_ALL ) {
640 for ( i = 0; i < mi->mi_ntargets; i++ ) {
641 metatarget_t *mt = mi->mi_targets[ i ];
642 metasingleconn_t *msc = &mc->mc_conns[ i ];
648 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
652 assert( msc->msc_ld != NULL );
655 * If the target is already bound it is skipped
659 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
660 if ( LDAP_BACK_CONN_ISBOUND( msc )
661 || ( LDAP_BACK_CONN_ISANON( msc )
662 && mt->mt_idassert_authmethod == LDAP_AUTH_NONE ) )
664 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
668 } else if ( LDAP_BACK_CONN_BINDING( msc ) ) {
669 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
670 ldap_pvt_thread_yield();
675 LDAP_BACK_CONN_BINDING_SET( msc );
676 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
678 rc = meta_back_single_dobind( op, rs, &mc, i,
679 LDAP_BACK_DONTSEND, mt->mt_nretries, 1 );
681 * NOTE: meta_back_single_dobind() already retries;
682 * in case of failure, it resets mc...
684 if ( rc != LDAP_SUCCESS ) {
685 char buf[ SLAP_TEXT_BUFLEN ];
688 /* meta_back_single_dobind() already sent
689 * response and released connection */
694 if ( rc == LDAP_UNAVAILABLE ) {
695 /* FIXME: meta_back_retry() already calls
696 * meta_back_single_dobind() */
697 if ( meta_back_retry( op, rs, &mc, i, sendok ) ) {
702 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
703 LDAP_BACK_CONN_BINDING_CLEAR( msc );
704 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
710 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
711 LDAP_BACK_CONN_BINDING_CLEAR( msc );
712 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
714 snprintf( buf, sizeof( buf ),
715 "meta_back_dobind[%d]: (%s) err=%d (%s).",
716 i, isroot ? op->o_bd->be_rootdn.bv_val : "anonymous",
717 rc, ldap_err2string( rc ) );
718 Debug( LDAP_DEBUG_ANY,
720 op->o_log_prefix, buf, 0 );
723 * null cred bind should always succeed
724 * as anonymous, so a failure means
725 * the target is no longer candidate possibly
726 * due to technical reasons (remote host down?)
727 * so better clear the handle
729 /* leave the target candidate, but record the error for later use */
730 candidates[ i ].sr_err = rc;
731 if ( META_BACK_ONERR_STOP( mi ) ) {
740 Debug( LDAP_DEBUG_TRACE,
741 "%s meta_back_dobind[%d]: "
744 isroot ? op->o_bd->be_rootdn.bv_val : "anonymous" );
746 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
747 LDAP_BACK_CONN_BINDING_CLEAR( msc );
749 LDAP_BACK_CONN_ISBOUND_SET( msc );
751 LDAP_BACK_CONN_ISANON_SET( msc );
753 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
758 Debug( LDAP_DEBUG_TRACE,
759 "%s meta_back_dobind: conn=%ld bound=%d\n",
760 op->o_log_prefix, LDAP_BACK_PCONN_ID( mc->mc_conn ), bound );
763 meta_back_release_conn( op, mc );
766 if ( sendok & LDAP_BACK_SENDERR ) {
767 if ( rs->sr_err == LDAP_SUCCESS ) {
768 rs->sr_err = LDAP_BUSY;
770 send_ldap_result( op, rs );
776 return ( bound > 0 );
780 * meta_back_default_rebind
782 * This is a callback used for chasing referrals using the same
783 * credentials as the original user on this session.
786 meta_back_default_rebind(
788 LDAP_CONST char *url,
793 metasingleconn_t *msc = ( metasingleconn_t * )params;
795 return ldap_sasl_bind_s( ld, msc->msc_bound_ndn.bv_val,
796 LDAP_SASL_SIMPLE, &msc->msc_cred,
801 * meta_back_default_urllist
803 * This is a callback used for mucking with the urllist
806 meta_back_default_urllist(
808 LDAPURLDesc **urllist,
812 metatarget_t *mt = (metatarget_t *)params;
813 LDAPURLDesc **urltail;
815 if ( urllist == url ) {
819 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
826 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
828 ch_free( mt->mt_uri );
831 ldap_get_option( ld, LDAP_OPT_URI, (void *)&mt->mt_uri );
832 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
844 ldap_back_send_t sendok )
846 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
848 metatarget_t *mt = mi->mi_targets[ candidate ];
849 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
853 Debug( LDAP_DEBUG_TRACE, ">>> %s meta_back_cancel[%d] msgid=%d\n",
854 op->o_log_prefix, candidate, msgid );
856 /* default behavior */
857 if ( META_BACK_TGT_ABANDON( mt ) ) {
858 rc = ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
860 } else if ( META_BACK_TGT_IGNORE( mt ) ) {
863 } else if ( META_BACK_TGT_CANCEL( mt ) ) {
864 rc = ldap_cancel_s( msc->msc_ld, msgid, NULL, NULL );
870 Debug( LDAP_DEBUG_TRACE, "<<< %s meta_back_cancel[%d] err=%d\n",
871 op->o_log_prefix, candidate, rc );
879 * FIXME: error return must be handled in a cleaner way ...
889 ldap_back_send_t sendok )
891 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
893 const char *save_text = rs->sr_text,
894 *save_matched = rs->sr_matched;
895 BerVarray save_ref = rs->sr_ref;
896 LDAPControl **save_ctrls = rs->sr_ctrls;
897 void *matched_ctx = NULL;
899 char *matched = NULL;
902 LDAPControl **ctrls = NULL;
904 assert( mc != NULL );
907 rs->sr_matched = NULL;
911 if ( candidate != META_TARGET_NONE ) {
912 metatarget_t *mt = mi->mi_targets[ candidate ];
913 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
915 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
917 if ( ERR_OK( rs->sr_err ) ) {
920 LDAPMessage *res = NULL;
921 time_t stoptime = (time_t)(-1);
922 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
923 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
924 const char *timeout_text = "Operation timed out";
926 /* if timeout is not specified, compute and use
927 * the one specific to the ongoing operation */
928 if ( timeout == (time_t)(-1) ) {
929 slap_op_t opidx = slap_req2op( op->o_tag );
931 if ( opidx == SLAP_OP_SEARCH ) {
932 if ( op->ors_tlimit <= 0 ) {
936 timeout = op->ors_tlimit;
937 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
942 timeout = mt->mt_timeout[ opidx ];
946 /* better than nothing :) */
947 if ( timeout == 0 ) {
948 if ( mi->mi_idle_timeout ) {
949 timeout = mi->mi_idle_timeout;
951 } else if ( mi->mi_conn_ttl ) {
952 timeout = mi->mi_conn_ttl;
957 stoptime = op->o_time + timeout;
960 LDAP_BACK_TV_SET( &tv );
963 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
966 if ( timeout && slap_get_time() > stoptime ) {
967 (void)meta_back_cancel( mc, op, rs, msgid, candidate, sendok );
968 rs->sr_err = timeout_err;
969 rs->sr_text = timeout_text;
973 LDAP_BACK_TV_SET( &tv );
974 ldap_pvt_thread_yield();
978 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
983 /* otherwise get the result; if it is not
984 * LDAP_SUCCESS, record it in the reply
985 * structure (this includes
986 * LDAP_COMPARE_{TRUE|FALSE}) */
988 /* only touch when activity actually took place... */
989 if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
990 msc->msc_time = op->o_time;
993 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
994 &matched, &text, &refs, &ctrls, 1 );
997 if ( rc != LDAP_SUCCESS ) {
1000 if ( refs != NULL ) {
1003 for ( i = 0; refs[ i ] != NULL; i++ )
1005 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1007 for ( i = 0; refs[ i ] != NULL; i++ ) {
1008 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1010 BER_BVZERO( &rs->sr_ref[ i ] );
1012 if ( ctrls != NULL ) {
1013 rs->sr_ctrls = ctrls;
1017 assert( res == NULL );
1020 /* if the error in the reply structure is not
1021 * LDAP_SUCCESS, try to map it from client
1022 * to server error */
1023 if ( !ERR_OK( rs->sr_err ) ) {
1024 rs->sr_err = slap_map_api2result( rs );
1026 /* internal ops ( op->o_conn == NULL )
1027 * must not reply to client */
1028 if ( op->o_conn && !op->o_do_not_cache && matched ) {
1030 /* record the (massaged) matched
1031 * DN into the reply structure */
1032 rs->sr_matched = matched;
1036 if ( META_BACK_TGT_QUARANTINE( mt ) ) {
1037 meta_back_quarantine( op, rs, candidate );
1044 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1045 metasingleconn_t *msc = &mc->mc_conns[ i ];
1047 char *xmatched = NULL;
1049 rs->sr_err = LDAP_SUCCESS;
1051 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
1052 if ( rs->sr_err != LDAP_SUCCESS ) {
1054 * better check the type of error. In some cases
1055 * (search ?) it might be better to return a
1056 * success if at least one of the targets gave
1057 * positive result ...
1059 ldap_get_option( msc->msc_ld,
1060 LDAP_OPT_ERROR_STRING, &xtext );
1061 if ( xtext != NULL && xtext [ 0 ] == '\0' ) {
1062 ldap_memfree( xtext );
1066 ldap_get_option( msc->msc_ld,
1067 LDAP_OPT_MATCHED_DN, &xmatched );
1068 if ( xmatched != NULL && xmatched[ 0 ] == '\0' ) {
1069 ldap_memfree( xmatched );
1073 rs->sr_err = slap_map_api2result( rs );
1075 if ( LogTest( LDAP_DEBUG_ANY ) ) {
1076 char buf[ SLAP_TEXT_BUFLEN ];
1078 snprintf( buf, sizeof( buf ),
1079 "meta_back_op_result[%d] "
1080 "err=%d text=\"%s\" matched=\"%s\"",
1082 ( xtext ? xtext : "" ),
1083 ( xmatched ? xmatched : "" ) );
1084 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
1085 op->o_log_prefix, buf, 0 );
1089 * FIXME: need to rewrite "match" (need rwinfo)
1091 switch ( rs->sr_err ) {
1094 if ( xtext != NULL ) {
1096 ldap_memfree( text );
1101 if ( xmatched != NULL ) {
1103 ldap_memfree( matched );
1112 ldap_memfree( xtext );
1116 ldap_memfree( xmatched );
1120 if ( META_BACK_TGT_QUARANTINE( mi->mi_targets[ i ] ) ) {
1121 meta_back_quarantine( op, rs, i );
1125 if ( err != LDAP_SUCCESS ) {
1130 if ( matched != NULL ) {
1131 struct berval dn, pdn;
1133 ber_str2bv( matched, 0, 0, &dn );
1134 if ( dnPretty( NULL, &dn, &pdn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
1135 ldap_memfree( matched );
1136 matched_ctx = op->o_tmpmemctx;
1137 matched = pdn.bv_val;
1139 rs->sr_matched = matched;
1143 ( ( sendok & LDAP_BACK_SENDOK )
1144 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1146 send_ldap_result( op, rs );
1149 op->o_tmpfree( (char *)rs->sr_matched, matched_ctx );
1152 ldap_memfree( text );
1155 assert( refs != NULL );
1156 ber_memvfree( (void **)refs );
1157 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1160 assert( rs->sr_ctrls != NULL );
1161 ldap_controls_free( ctrls );
1164 rs->sr_text = save_text;
1165 rs->sr_matched = save_matched;
1166 rs->sr_ref = save_ref;
1167 rs->sr_ctrls = save_ctrls;
1169 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1173 * meta_back_proxy_authz_cred()
1175 * prepares credentials & method for meta_back_proxy_authz_bind();
1176 * or, if method is SASL, performs the SASL bind directly.
1179 meta_back_proxy_authz_cred(
1184 ldap_back_send_t sendok,
1185 struct berval *binddn,
1186 struct berval *bindcred,
1189 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
1190 metatarget_t *mt = mi->mi_targets[ candidate ];
1191 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
1195 /* don't proxyAuthz if protocol is not LDAPv3 */
1196 switch ( mt->mt_version ) {
1201 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1207 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1208 if ( sendok & LDAP_BACK_SENDERR ) {
1209 send_ldap_result( op, rs );
1211 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1215 if ( op->o_tag == LDAP_REQ_BIND ) {
1216 ndn = op->o_req_ndn;
1218 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1219 ndn = op->o_conn->c_ndn;
1226 * FIXME: we need to let clients use proxyAuthz
1227 * otherwise we cannot do symmetric pools of servers;
1228 * we have to live with the fact that a user can
1229 * authorize itself as any ID that is allowed
1230 * by the authzTo directive of the "proxyauthzdn".
1233 * NOTE: current Proxy Authorization specification
1234 * and implementation do not allow proxy authorization
1235 * control to be provided with Bind requests
1238 * if no bind took place yet, but the connection is bound
1239 * and the "proxyauthzdn" is set, then bind as
1240 * "proxyauthzdn" and explicitly add the proxyAuthz
1241 * control to every operation with the dn bound
1242 * to the connection as control value.
1245 /* bind as proxyauthzdn only if no idassert mode
1246 * is requested, or if the client's identity
1248 switch ( mt->mt_idassert_mode ) {
1249 case LDAP_BACK_IDASSERT_LEGACY:
1250 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1251 if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) && !BER_BVISEMPTY( &mt->mt_idassert_authcDN ) )
1253 *binddn = mt->mt_idassert_authcDN;
1254 *bindcred = mt->mt_idassert_passwd;
1261 /* NOTE: rootdn can always idassert */
1262 if ( BER_BVISNULL( &ndn ) && mt->mt_idassert_authz == NULL ) {
1263 if ( mt->mt_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1264 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1265 if ( sendok & LDAP_BACK_SENDERR ) {
1266 send_ldap_result( op, rs );
1268 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1271 rs->sr_err = LDAP_SUCCESS;
1272 *binddn = slap_empty_bv;
1273 *bindcred = slap_empty_bv;
1279 } else if ( mt->mt_idassert_authz && !be_isroot( op ) ) {
1280 struct berval authcDN;
1282 if ( BER_BVISNULL( &ndn ) ) {
1283 authcDN = slap_empty_bv;
1288 rs->sr_err = slap_sasl_matches( op, mt->mt_idassert_authz,
1289 &authcDN, &authcDN );
1290 if ( rs->sr_err != LDAP_SUCCESS ) {
1291 if ( mt->mt_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1292 if ( sendok & LDAP_BACK_SENDERR ) {
1293 send_ldap_result( op, rs );
1295 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1298 rs->sr_err = LDAP_SUCCESS;
1299 *binddn = slap_empty_bv;
1300 *bindcred = slap_empty_bv;
1308 *binddn = mt->mt_idassert_authcDN;
1309 *bindcred = mt->mt_idassert_passwd;
1314 if ( dobind && mt->mt_idassert_authmethod == LDAP_AUTH_SASL ) {
1315 #ifdef HAVE_CYRUS_SASL
1316 void *defaults = NULL;
1317 struct berval authzID = BER_BVNULL;
1320 /* if SASL supports native authz, prepare for it */
1321 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1322 ( mt->mt_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1324 switch ( mt->mt_idassert_mode ) {
1325 case LDAP_BACK_IDASSERT_OTHERID:
1326 case LDAP_BACK_IDASSERT_OTHERDN:
1327 authzID = mt->mt_idassert_authzID;
1330 case LDAP_BACK_IDASSERT_ANONYMOUS:
1331 BER_BVSTR( &authzID, "dn:" );
1334 case LDAP_BACK_IDASSERT_SELF:
1335 if ( BER_BVISNULL( &ndn ) ) {
1336 /* connection is not authc'd, so don't idassert */
1337 BER_BVSTR( &authzID, "dn:" );
1340 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1341 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1342 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1343 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1344 ndn.bv_val, ndn.bv_len + 1 );
1353 if ( mt->mt_idassert_secprops != NULL ) {
1354 rs->sr_err = ldap_set_option( msc->msc_ld,
1355 LDAP_OPT_X_SASL_SECPROPS,
1356 (void *)mt->mt_idassert_secprops );
1358 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1359 rs->sr_err = LDAP_OTHER;
1360 if ( sendok & LDAP_BACK_SENDERR ) {
1361 send_ldap_result( op, rs );
1363 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1368 defaults = lutil_sasl_defaults( msc->msc_ld,
1369 mt->mt_idassert_sasl_mech.bv_val,
1370 mt->mt_idassert_sasl_realm.bv_val,
1371 mt->mt_idassert_authcID.bv_val,
1372 mt->mt_idassert_passwd.bv_val,
1375 rs->sr_err = ldap_sasl_interactive_bind_s( msc->msc_ld, binddn->bv_val,
1376 mt->mt_idassert_sasl_mech.bv_val, NULL, NULL,
1377 LDAP_SASL_QUIET, lutil_sasl_interact,
1380 rs->sr_err = slap_map_api2result( rs );
1381 if ( rs->sr_err != LDAP_SUCCESS ) {
1382 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1383 if ( sendok & LDAP_BACK_SENDERR ) {
1384 send_ldap_result( op, rs );
1388 LDAP_BACK_CONN_ISBOUND_SET( msc );
1391 lutil_sasl_freedefs( defaults );
1393 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1397 #endif /* HAVE_CYRUS_SASL */
1400 *method = mt->mt_idassert_authmethod;
1401 switch ( mt->mt_idassert_authmethod ) {
1402 case LDAP_AUTH_NONE:
1403 BER_BVSTR( binddn, "" );
1404 BER_BVSTR( bindcred, "" );
1407 case LDAP_AUTH_SIMPLE:
1412 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1413 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1414 if ( sendok & LDAP_BACK_SENDERR ) {
1415 send_ldap_result( op, rs );
1425 meta_back_proxy_authz_bind( metaconn_t *mc, int candidate, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1427 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
1428 metatarget_t *mt = mi->mi_targets[ candidate ];
1429 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
1430 struct berval binddn = BER_BVC( "" ),
1431 cred = BER_BVC( "" );
1432 int method = LDAP_AUTH_NONE,
1435 rc = meta_back_proxy_authz_cred( mc, candidate, op, rs, sendok, &binddn, &cred, &method );
1436 if ( rc == LDAP_SUCCESS && !LDAP_BACK_CONN_ISBOUND( msc ) ) {
1440 case LDAP_AUTH_NONE:
1441 case LDAP_AUTH_SIMPLE:
1442 rs->sr_err = ldap_sasl_bind( msc->msc_ld,
1443 binddn.bv_val, LDAP_SASL_SIMPLE,
1444 &cred, NULL, NULL, &msgid );
1445 rc = meta_back_bind_op_result( op, rs, mc, candidate, msgid, sendok );
1446 if ( rc == LDAP_SUCCESS ) {
1447 /* set rebind stuff in case of successful proxyAuthz bind,
1448 * so that referral chasing is attempted using the right
1450 LDAP_BACK_CONN_ISBOUND_SET( msc );
1451 ber_bvreplace( &msc->msc_bound_ndn, &binddn );
1453 if ( LDAP_BACK_SAVECRED( mi ) ) {
1454 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
1455 memset( msc->msc_cred.bv_val, 0,
1456 msc->msc_cred.bv_len );
1458 ber_bvreplace( &msc->msc_cred, &cred );
1459 ldap_set_rebind_proc( msc->msc_ld, mt->mt_rebind_f, msc );
1470 return LDAP_BACK_CONN_ISBOUND( msc );