2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2007 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"
36 #undef ldap_debug /* silence a warning in ldap-int.h */
37 #include "../../../libraries/libldap/ldap-int.h"
39 #include "lutil_ldap.h"
42 meta_back_proxy_authz_bind(
47 ldap_back_send_t sendok );
50 meta_back_single_bind(
57 meta_back_bind( Operation *op, SlapReply *rs )
59 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
60 metaconn_t *mc = NULL;
67 SlapReply *candidates;
69 rs->sr_err = LDAP_SUCCESS;
71 Debug( LDAP_DEBUG_ARGS, "%s meta_back_bind: dn=\"%s\".\n",
72 op->o_log_prefix, op->o_req_dn.bv_val, 0 );
74 /* the test on the bind method should be superfluous */
75 switch ( be_rootdn_bind( op, rs ) ) {
77 if ( META_BACK_DEFER_ROOTDN_BIND( mi ) ) {
78 /* frontend will return success */
85 case SLAP_CB_CONTINUE:
89 /* be_rootdn_bind() sent result */
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 );
125 candidates = meta_back_candidates_get( op );
128 * Each target is scanned ...
130 mc->mc_authz_target = META_BOUND_NONE;
131 for ( i = 0; i < mi->mi_ntargets; i++ ) {
132 metatarget_t *mt = mi->mi_targets[ i ];
136 * Skip non-candidates
138 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
143 /* set rc to LDAP_SUCCESS only if at least
144 * one candidate has been tried */
148 } else if ( isroot == 0 ) {
150 * A bind operation is expected to have
151 * ONE CANDIDATE ONLY!
153 Debug( LDAP_DEBUG_ANY,
154 "### %s meta_back_bind: more than one"
155 " candidate selected...\n",
156 op->o_log_prefix, 0, 0 );
160 if ( mt->mt_idassert_authmethod == LDAP_AUTH_NONE
161 || BER_BVISNULL( &mt->mt_idassert_authcDN ) )
163 metasingleconn_t *msc = &mc->mc_conns[ i ];
165 /* skip the target if no pseudorootdn is provided */
166 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
167 ch_free( msc->msc_bound_ndn.bv_val );
168 BER_BVZERO( &msc->msc_bound_ndn );
171 if ( !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;
193 /* FIXME: in some cases (e.g. unavailable)
194 * do not assume it's not candidate; rather
195 * mark this as an error to be eventually
196 * reported to client */
197 META_CANDIDATE_CLEAR( &candidates[ i ] );
202 /* must re-insert if local DN changed as result of bind */
203 if ( rc == LDAP_SUCCESS ) {
205 mc->mc_authz_target = META_BOUND_ALL;
208 if ( !LDAP_BACK_PCONN_ISPRIV( mc )
209 && !dn_match( &op->o_req_ndn, &mc->mc_local_ndn ) )
213 /* wait for all other ops to release the connection */
214 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
215 assert( mc->mc_refcnt == 1 );
216 #if META_BACK_PRINT_CONNTREE > 0
217 meta_back_print_conntree( mi, ">>> meta_back_bind" );
218 #endif /* META_BACK_PRINT_CONNTREE */
220 /* delete all cached connections with the current connection */
221 if ( LDAP_BACK_SINGLECONN( mi ) ) {
224 while ( ( tmpmc = avl_delete( &mi->mi_conninfo.lai_tree, (caddr_t)mc, meta_back_conn_cmp ) ) != NULL )
226 Debug( LDAP_DEBUG_TRACE,
227 "=>meta_back_bind: destroying conn %ld (refcnt=%u)\n",
228 LDAP_BACK_PCONN_ID( mc ), mc->mc_refcnt, 0 );
230 if ( tmpmc->mc_refcnt != 0 ) {
232 LDAP_BACK_CONN_TAINTED_SET( tmpmc );
236 * Needs a test because the handler may be corrupted,
237 * and calling ldap_unbind on a corrupted header results
238 * in a segmentation fault
240 meta_back_conn_free( tmpmc );
245 ber_bvreplace( &mc->mc_local_ndn, &op->o_req_ndn );
246 lerr = avl_insert( &mi->mi_conninfo.lai_tree, (caddr_t)mc,
247 meta_back_conndn_cmp, meta_back_conndn_dup );
248 #if META_BACK_PRINT_CONNTREE > 0
249 meta_back_print_conntree( mi, "<<< meta_back_bind" );
250 #endif /* META_BACK_PRINT_CONNTREE */
253 LDAP_BACK_CONN_ISPRIV_SET( mc );
254 LDAP_BACK_PCONN_SET( mc, op );
256 LDAP_BACK_CONN_CACHED_SET( mc );
259 LDAP_BACK_CONN_CACHED_CLEAR( mc );
261 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
266 meta_back_release_conn( mi, mc );
270 * rc is LDAP_SUCCESS if at least one bind succeeded,
271 * err is the last error that occurred during a bind;
272 * if at least (and at most?) one bind succeeds, fine.
274 if ( rc != LDAP_SUCCESS ) {
277 * deal with bind failure ...
281 * no target was found within the naming context,
282 * so bind must fail with invalid credentials
284 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
285 rs->sr_err = LDAP_INVALID_CREDENTIALS;
287 rs->sr_err = slap_map_api2result( rs );
289 send_ldap_result( op, rs );
298 meta_back_bind_op_result(
304 ldap_back_send_t sendok )
306 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
307 metatarget_t *mt = mi->mi_targets[ candidate ];
308 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
312 int nretries = mt->mt_nretries;
313 char buf[ SLAP_TEXT_BUFLEN ];
315 Debug( LDAP_DEBUG_TRACE,
316 ">>> %s meta_back_bind_op_result[%d]\n",
317 op->o_log_prefix, candidate, 0 );
319 /* make sure this is clean */
320 assert( rs->sr_ctrls == NULL );
322 if ( rs->sr_err == LDAP_SUCCESS ) {
323 time_t stoptime = (time_t)(-1),
325 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
326 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
327 const char *timeout_text = "Operation timed out";
328 slap_op_t opidx = slap_req2op( op->o_tag );
330 /* since timeout is not specified, compute and use
331 * the one specific to the ongoing operation */
332 if ( opidx == LDAP_REQ_SEARCH ) {
333 if ( op->ors_tlimit <= 0 ) {
337 timeout = op->ors_tlimit;
338 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
343 timeout = mt->mt_timeout[ opidx ];
346 /* better than nothing :) */
347 if ( timeout == 0 ) {
348 if ( mi->mi_idle_timeout ) {
349 timeout = mi->mi_idle_timeout;
351 } else if ( mi->mi_conn_ttl ) {
352 timeout = mi->mi_conn_ttl;
357 stoptime = op->o_time + timeout;
360 LDAP_BACK_TV_SET( &tv );
366 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
369 if ( nretries != META_RETRY_NEVER
370 || ( timeout && slap_get_time() <= stoptime ) )
372 ldap_pvt_thread_yield();
373 if ( nretries > 0 ) {
376 tv = mt->mt_bind_timeout;
380 /* don't let anyone else use this handler,
381 * because there's a pending bind that will not
383 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
384 assert( LDAP_BACK_CONN_BINDING( msc ) );
387 Debug( LDAP_DEBUG_ANY, "### %s meta_back_bind_op_result ldap_unbind_ext[%d] ld=%p\n",
388 op->o_log_prefix, candidate, (void *)msc->msc_ld );
389 #endif /* DEBUG_205 */
391 meta_clear_one_candidate( op, mc, candidate );
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 ];
453 struct berval save_o_dn;
454 int save_o_do_not_cache;
455 LDAPControl **ctrls = NULL;
457 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
458 ch_free( msc->msc_bound_ndn.bv_val );
459 BER_BVZERO( &msc->msc_bound_ndn );
462 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
463 /* destroy sensitive data */
464 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
465 ch_free( msc->msc_cred.bv_val );
466 BER_BVZERO( &msc->msc_cred );
470 * Rewrite the bind dn if needed
473 dc.conn = op->o_conn;
477 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
478 rs->sr_text = "DN rewrite error";
479 rs->sr_err = LDAP_OTHER;
483 /* don't add proxyAuthz; set the bindDN */
484 save_o_dn = op->o_dn;
485 save_o_do_not_cache = op->o_do_not_cache;
486 op->o_do_not_cache = 1;
487 op->o_dn = op->o_req_dn;
490 rs->sr_err = meta_back_controls_add( op, rs, mc, candidate, &ctrls );
491 op->o_dn = save_o_dn;
492 op->o_do_not_cache = save_o_do_not_cache;
493 if ( rs->sr_err != LDAP_SUCCESS ) {
497 /* FIXME: this fixes the bind problem right now; we need
498 * to use the asynchronous version to get the "matched"
499 * and more in case of failure ... */
500 /* FIXME: should we check if at least some of the op->o_ctrls
501 * can/should be passed? */
503 rs->sr_err = ldap_sasl_bind( msc->msc_ld, mdn.bv_val,
504 LDAP_SASL_SIMPLE, &op->orb_cred,
505 ctrls, NULL, &msgid );
506 if ( rs->sr_err != LDAP_X_CONNECTING ) {
509 ldap_pvt_thread_yield();
512 ldap_back_controls_free( op, rs, &ctrls );
514 meta_back_bind_op_result( op, rs, mc, candidate, msgid, LDAP_BACK_DONTSEND );
515 if ( rs->sr_err != LDAP_SUCCESS ) {
519 /* If defined, proxyAuthz will be used also when
520 * back-ldap is the authorizing backend; for this
521 * purpose, a successful bind is followed by a
522 * bind with the configured identity assertion */
523 /* NOTE: use with care */
524 if ( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
525 meta_back_proxy_authz_bind( mc, candidate, op, rs, LDAP_BACK_SENDERR );
526 if ( !LDAP_BACK_CONN_ISBOUND( msc ) ) {
532 ber_bvreplace( &msc->msc_bound_ndn, &op->o_req_ndn );
533 LDAP_BACK_CONN_ISBOUND_SET( msc );
534 mc->mc_authz_target = candidate;
536 if ( LDAP_BACK_SAVECRED( mi ) ) {
537 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
538 memset( msc->msc_cred.bv_val, 0,
539 msc->msc_cred.bv_len );
541 ber_bvreplace( &msc->msc_cred, &op->orb_cred );
542 ldap_set_rebind_proc( msc->msc_ld, mt->mt_rebind_f, msc );
546 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED
547 && !BER_BVISEMPTY( &op->o_req_ndn ) )
549 ( void )meta_dncache_update_entry( &mi->mi_cache,
550 &op->o_req_ndn, candidate );
554 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
558 if ( META_BACK_TGT_QUARANTINE( mt ) ) {
559 meta_back_quarantine( op, rs, candidate );
566 * meta_back_single_dobind
569 meta_back_single_dobind(
574 ldap_back_send_t sendok,
578 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
579 metatarget_t *mt = mi->mi_targets[ candidate ];
580 metaconn_t *mc = *mcp;
581 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
582 static struct berval cred = BER_BVC( "" );
585 assert( !LDAP_BACK_CONN_ISBOUND( msc ) );
587 /* NOTE: this obsoletes pseudorootdn */
588 if ( op->o_conn != NULL &&
589 !op->o_do_not_cache &&
590 ( BER_BVISNULL( &msc->msc_bound_ndn ) ||
591 BER_BVISEMPTY( &msc->msc_bound_ndn ) ||
592 ( LDAP_BACK_CONN_ISPRIV( mc ) && dn_match( &msc->msc_bound_ndn, &mt->mt_idassert_authcDN ) ) ||
593 ( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
595 (void)meta_back_proxy_authz_bind( mc, candidate, op, rs, sendok );
599 /* FIXME: should we check if at least some of the op->o_ctrls
600 * can/should be passed? */
602 rs->sr_err = ldap_sasl_bind( msc->msc_ld,
603 "", LDAP_SASL_SIMPLE, &cred,
604 NULL, NULL, &msgid );
605 if ( rs->sr_err != LDAP_X_CONNECTING ) {
608 ldap_pvt_thread_yield();
611 rs->sr_err = meta_back_bind_op_result( op, rs, mc, candidate, msgid, sendok );
614 if ( rs->sr_err != LDAP_SUCCESS ) {
616 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
618 LDAP_BACK_CONN_BINDING_CLEAR( msc );
619 if ( META_BACK_ONERR_STOP( mi ) ) {
620 LDAP_BACK_CONN_TAINTED_SET( mc );
621 meta_back_release_conn_lock( mi, mc, 0 );
625 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
629 if ( META_BACK_TGT_QUARANTINE( mt ) ) {
630 meta_back_quarantine( op, rs, candidate );
644 ldap_back_send_t sendok )
646 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
652 SlapReply *candidates;
654 if ( be_isroot( op ) ) {
658 Debug( LDAP_DEBUG_TRACE,
659 "%s meta_back_dobind: conn=%ld%s\n",
661 LDAP_BACK_PCONN_ID( mc ),
662 isroot ? " (isroot)" : "" );
665 * all the targets are bound as pseudoroot
667 if ( mc->mc_authz_target == META_BOUND_ALL ) {
672 candidates = meta_back_candidates_get( op );
674 for ( i = 0; i < mi->mi_ntargets; i++ ) {
675 metatarget_t *mt = mi->mi_targets[ i ];
676 metasingleconn_t *msc = &mc->mc_conns[ i ];
682 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
686 assert( msc->msc_ld != NULL );
689 * If the target is already bound it is skipped
693 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
694 if ( LDAP_BACK_CONN_ISBOUND( msc )
695 || ( LDAP_BACK_CONN_ISANON( msc )
696 && mt->mt_idassert_authmethod == LDAP_AUTH_NONE ) )
698 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
702 } else if ( META_BACK_CONN_CREATING( msc ) || LDAP_BACK_CONN_BINDING( msc ) )
704 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
705 ldap_pvt_thread_yield();
710 LDAP_BACK_CONN_BINDING_SET( msc );
711 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
713 rc = meta_back_single_dobind( op, rs, &mc, i,
714 LDAP_BACK_DONTSEND, mt->mt_nretries, 1 );
716 * NOTE: meta_back_single_dobind() already retries;
717 * in case of failure, it resets mc...
719 if ( rc != LDAP_SUCCESS ) {
720 char buf[ SLAP_TEXT_BUFLEN ];
723 /* meta_back_single_dobind() already sent
724 * response and released connection */
729 if ( rc == LDAP_UNAVAILABLE ) {
730 /* FIXME: meta_back_retry() already re-calls
731 * meta_back_single_dobind() */
732 if ( meta_back_retry( op, rs, &mc, i, sendok ) ) {
737 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
738 LDAP_BACK_CONN_BINDING_CLEAR( msc );
739 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
740 meta_back_release_conn( mi, mc );
746 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
747 LDAP_BACK_CONN_BINDING_CLEAR( msc );
748 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
750 snprintf( buf, sizeof( buf ),
751 "meta_back_dobind[%d]: (%s) err=%d (%s).",
752 i, isroot ? op->o_bd->be_rootdn.bv_val : "anonymous",
753 rc, ldap_err2string( rc ) );
754 Debug( LDAP_DEBUG_ANY,
756 op->o_log_prefix, buf, 0 );
759 * null cred bind should always succeed
760 * as anonymous, so a failure means
761 * the target is no longer candidate possibly
762 * due to technical reasons (remote host down?)
763 * so better clear the handle
765 /* leave the target candidate, but record the error for later use */
766 candidates[ i ].sr_err = rc;
767 if ( META_BACK_ONERR_STOP( mi ) ) {
776 Debug( LDAP_DEBUG_TRACE,
777 "%s meta_back_dobind[%d]: "
780 isroot ? op->o_bd->be_rootdn.bv_val : "anonymous" );
782 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
783 LDAP_BACK_CONN_BINDING_CLEAR( msc );
785 LDAP_BACK_CONN_ISBOUND_SET( msc );
787 LDAP_BACK_CONN_ISANON_SET( msc );
789 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
794 Debug( LDAP_DEBUG_TRACE,
795 "%s meta_back_dobind: conn=%ld bound=%d\n",
796 op->o_log_prefix, LDAP_BACK_PCONN_ID( mc ), bound );
799 meta_back_release_conn( mi, mc );
802 if ( sendok & LDAP_BACK_SENDERR ) {
803 if ( rs->sr_err == LDAP_SUCCESS ) {
804 rs->sr_err = LDAP_BUSY;
806 send_ldap_result( op, rs );
812 return ( bound > 0 );
816 * meta_back_default_rebind
818 * This is a callback used for chasing referrals using the same
819 * credentials as the original user on this session.
822 meta_back_default_rebind(
824 LDAP_CONST char *url,
829 metasingleconn_t *msc = ( metasingleconn_t * )params;
831 return ldap_sasl_bind_s( ld, msc->msc_bound_ndn.bv_val,
832 LDAP_SASL_SIMPLE, &msc->msc_cred,
837 * meta_back_default_urllist
839 * This is a callback used for mucking with the urllist
842 meta_back_default_urllist(
844 LDAPURLDesc **urllist,
848 metatarget_t *mt = (metatarget_t *)params;
849 LDAPURLDesc **urltail;
851 if ( urllist == url ) {
855 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
862 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
864 ch_free( mt->mt_uri );
867 ldap_get_option( ld, LDAP_OPT_URI, (void *)&mt->mt_uri );
868 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
880 ldap_back_send_t sendok )
882 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
884 metatarget_t *mt = mi->mi_targets[ candidate ];
885 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
889 Debug( LDAP_DEBUG_TRACE, ">>> %s meta_back_cancel[%d] msgid=%d\n",
890 op->o_log_prefix, candidate, msgid );
892 /* default behavior */
893 if ( META_BACK_TGT_ABANDON( mt ) ) {
894 rc = ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
896 } else if ( META_BACK_TGT_IGNORE( mt ) ) {
897 rc = ldap_pvt_discard( msc->msc_ld, msgid );
899 } else if ( META_BACK_TGT_CANCEL( mt ) ) {
900 rc = ldap_cancel_s( msc->msc_ld, msgid, NULL, NULL );
906 Debug( LDAP_DEBUG_TRACE, "<<< %s meta_back_cancel[%d] err=%d\n",
907 op->o_log_prefix, candidate, rc );
915 * FIXME: error return must be handled in a cleaner way ...
925 ldap_back_send_t sendok )
927 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
929 const char *save_text = rs->sr_text,
930 *save_matched = rs->sr_matched;
931 BerVarray save_ref = rs->sr_ref;
932 LDAPControl **save_ctrls = rs->sr_ctrls;
933 void *matched_ctx = NULL;
935 char *matched = NULL;
938 LDAPControl **ctrls = NULL;
940 assert( mc != NULL );
943 rs->sr_matched = NULL;
947 if ( candidate != META_TARGET_NONE ) {
948 metatarget_t *mt = mi->mi_targets[ candidate ];
949 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
951 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
953 if ( ERR_OK( rs->sr_err ) ) {
956 LDAPMessage *res = NULL;
957 time_t stoptime = (time_t)(-1);
958 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
959 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
960 const char *timeout_text = "Operation timed out";
962 /* if timeout is not specified, compute and use
963 * the one specific to the ongoing operation */
964 if ( timeout == (time_t)(-1) ) {
965 slap_op_t opidx = slap_req2op( op->o_tag );
967 if ( opidx == SLAP_OP_SEARCH ) {
968 if ( op->ors_tlimit <= 0 ) {
972 timeout = op->ors_tlimit;
973 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
978 timeout = mt->mt_timeout[ opidx ];
982 /* better than nothing :) */
983 if ( timeout == 0 ) {
984 if ( mi->mi_idle_timeout ) {
985 timeout = mi->mi_idle_timeout;
987 } else if ( mi->mi_conn_ttl ) {
988 timeout = mi->mi_conn_ttl;
993 stoptime = op->o_time + timeout;
996 LDAP_BACK_TV_SET( &tv );
999 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1002 if ( timeout && slap_get_time() > stoptime ) {
1003 (void)meta_back_cancel( mc, op, rs, msgid, candidate, sendok );
1004 rs->sr_err = timeout_err;
1005 rs->sr_text = timeout_text;
1009 LDAP_BACK_TV_SET( &tv );
1010 ldap_pvt_thread_yield();
1014 ldap_get_option( msc->msc_ld, LDAP_OPT_RESULT_CODE,
1019 /* otherwise get the result; if it is not
1020 * LDAP_SUCCESS, record it in the reply
1021 * structure (this includes
1022 * LDAP_COMPARE_{TRUE|FALSE}) */
1024 /* only touch when activity actually took place... */
1025 if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
1026 msc->msc_time = op->o_time;
1029 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
1030 &matched, &text, &refs, &ctrls, 1 );
1033 if ( rc != LDAP_SUCCESS ) {
1037 /* RFC 4511: referrals can only appear
1038 * if result code is LDAP_REFERRAL */
1040 && refs[ 0 ] != NULL
1041 && refs[ 0 ][ 0 ] != '\0' )
1043 if ( rs->sr_err != LDAP_REFERRAL ) {
1044 Debug( LDAP_DEBUG_ANY,
1045 "%s meta_back_op_result[%d]: "
1046 "got referrals with err=%d\n",
1048 candidate, rs->sr_err );
1053 for ( i = 0; refs[ i ] != NULL; i++ )
1055 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1057 for ( i = 0; refs[ i ] != NULL; i++ ) {
1058 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1060 BER_BVZERO( &rs->sr_ref[ i ] );
1063 } else if ( rs->sr_err == LDAP_REFERRAL ) {
1064 Debug( LDAP_DEBUG_ANY,
1065 "%s meta_back_op_result[%d]: "
1066 "got err=%d with null "
1067 "or empty referrals\n",
1069 candidate, rs->sr_err );
1071 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1074 if ( ctrls != NULL ) {
1075 rs->sr_ctrls = ctrls;
1079 assert( res == NULL );
1082 /* if the error in the reply structure is not
1083 * LDAP_SUCCESS, try to map it from client
1084 * to server error */
1085 if ( !ERR_OK( rs->sr_err ) ) {
1086 rs->sr_err = slap_map_api2result( rs );
1088 /* internal ops ( op->o_conn == NULL )
1089 * must not reply to client */
1090 if ( op->o_conn && !op->o_do_not_cache && matched ) {
1092 /* record the (massaged) matched
1093 * DN into the reply structure */
1094 rs->sr_matched = matched;
1098 if ( META_BACK_TGT_QUARANTINE( mt ) ) {
1099 meta_back_quarantine( op, rs, candidate );
1106 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1107 metasingleconn_t *msc = &mc->mc_conns[ i ];
1109 char *xmatched = NULL;
1111 rs->sr_err = LDAP_SUCCESS;
1113 ldap_get_option( msc->msc_ld, LDAP_OPT_RESULT_CODE, &rs->sr_err );
1114 if ( rs->sr_err != LDAP_SUCCESS ) {
1116 * better check the type of error. In some cases
1117 * (search ?) it might be better to return a
1118 * success if at least one of the targets gave
1119 * positive result ...
1121 ldap_get_option( msc->msc_ld,
1122 LDAP_OPT_DIAGNOSTIC_MESSAGE, &xtext );
1123 if ( xtext != NULL && xtext [ 0 ] == '\0' ) {
1124 ldap_memfree( xtext );
1128 ldap_get_option( msc->msc_ld,
1129 LDAP_OPT_MATCHED_DN, &xmatched );
1130 if ( xmatched != NULL && xmatched[ 0 ] == '\0' ) {
1131 ldap_memfree( xmatched );
1135 rs->sr_err = slap_map_api2result( rs );
1137 if ( LogTest( LDAP_DEBUG_ANY ) ) {
1138 char buf[ SLAP_TEXT_BUFLEN ];
1140 snprintf( buf, sizeof( buf ),
1141 "meta_back_op_result[%d] "
1142 "err=%d text=\"%s\" matched=\"%s\"",
1144 ( xtext ? xtext : "" ),
1145 ( xmatched ? xmatched : "" ) );
1146 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
1147 op->o_log_prefix, buf, 0 );
1151 * FIXME: need to rewrite "match" (need rwinfo)
1153 switch ( rs->sr_err ) {
1156 if ( xtext != NULL ) {
1158 ldap_memfree( text );
1163 if ( xmatched != NULL ) {
1165 ldap_memfree( matched );
1174 ldap_memfree( xtext );
1178 ldap_memfree( xmatched );
1182 if ( META_BACK_TGT_QUARANTINE( mi->mi_targets[ i ] ) ) {
1183 meta_back_quarantine( op, rs, i );
1187 if ( err != LDAP_SUCCESS ) {
1192 if ( matched != NULL ) {
1193 struct berval dn, pdn;
1195 ber_str2bv( matched, 0, 0, &dn );
1196 if ( dnPretty( NULL, &dn, &pdn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
1197 ldap_memfree( matched );
1198 matched_ctx = op->o_tmpmemctx;
1199 matched = pdn.bv_val;
1201 rs->sr_matched = matched;
1205 ( ( sendok & LDAP_BACK_SENDOK )
1206 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1208 send_ldap_result( op, rs );
1211 op->o_tmpfree( (char *)rs->sr_matched, matched_ctx );
1214 ldap_memfree( text );
1217 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1221 ber_memvfree( (void **)refs );
1224 assert( rs->sr_ctrls != NULL );
1225 ldap_controls_free( ctrls );
1228 rs->sr_text = save_text;
1229 rs->sr_matched = save_matched;
1230 rs->sr_ref = save_ref;
1231 rs->sr_ctrls = save_ctrls;
1233 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1237 * meta_back_proxy_authz_cred()
1239 * prepares credentials & method for meta_back_proxy_authz_bind();
1240 * or, if method is SASL, performs the SASL bind directly.
1243 meta_back_proxy_authz_cred(
1248 ldap_back_send_t sendok,
1249 struct berval *binddn,
1250 struct berval *bindcred,
1253 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
1254 metatarget_t *mt = mi->mi_targets[ candidate ];
1255 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
1259 /* don't proxyAuthz if protocol is not LDAPv3 */
1260 switch ( mt->mt_version ) {
1265 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1271 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1272 if ( sendok & LDAP_BACK_SENDERR ) {
1273 send_ldap_result( op, rs );
1275 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1279 if ( op->o_tag == LDAP_REQ_BIND ) {
1280 ndn = op->o_req_ndn;
1282 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1283 ndn = op->o_conn->c_ndn;
1290 * FIXME: we need to let clients use proxyAuthz
1291 * otherwise we cannot do symmetric pools of servers;
1292 * we have to live with the fact that a user can
1293 * authorize itself as any ID that is allowed
1294 * by the authzTo directive of the "proxyauthzdn".
1297 * NOTE: current Proxy Authorization specification
1298 * and implementation do not allow proxy authorization
1299 * control to be provided with Bind requests
1302 * if no bind took place yet, but the connection is bound
1303 * and the "proxyauthzdn" is set, then bind as
1304 * "proxyauthzdn" and explicitly add the proxyAuthz
1305 * control to every operation with the dn bound
1306 * to the connection as control value.
1309 /* bind as proxyauthzdn only if no idassert mode
1310 * is requested, or if the client's identity
1312 switch ( mt->mt_idassert_mode ) {
1313 case LDAP_BACK_IDASSERT_LEGACY:
1314 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1315 if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) && !BER_BVISEMPTY( &mt->mt_idassert_authcDN ) )
1317 *binddn = mt->mt_idassert_authcDN;
1318 *bindcred = mt->mt_idassert_passwd;
1325 /* NOTE: rootdn can always idassert */
1326 if ( BER_BVISNULL( &ndn )
1327 && mt->mt_idassert_authz == NULL
1328 && !( mt->mt_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL ) )
1330 if ( mt->mt_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1331 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1332 if ( sendok & LDAP_BACK_SENDERR ) {
1333 send_ldap_result( op, rs );
1335 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1340 rs->sr_err = LDAP_SUCCESS;
1341 *binddn = slap_empty_bv;
1342 *bindcred = slap_empty_bv;
1345 } else if ( mt->mt_idassert_authz && !be_isroot( op ) ) {
1346 struct berval authcDN;
1348 if ( BER_BVISNULL( &ndn ) ) {
1349 authcDN = slap_empty_bv;
1354 rs->sr_err = slap_sasl_matches( op, mt->mt_idassert_authz,
1355 &authcDN, &authcDN );
1356 if ( rs->sr_err != LDAP_SUCCESS ) {
1357 if ( mt->mt_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1358 if ( sendok & LDAP_BACK_SENDERR ) {
1359 send_ldap_result( op, rs );
1361 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1365 rs->sr_err = LDAP_SUCCESS;
1366 *binddn = slap_empty_bv;
1367 *bindcred = slap_empty_bv;
1372 *binddn = mt->mt_idassert_authcDN;
1373 *bindcred = mt->mt_idassert_passwd;
1378 if ( dobind && mt->mt_idassert_authmethod == LDAP_AUTH_SASL ) {
1379 #ifdef HAVE_CYRUS_SASL
1380 void *defaults = NULL;
1381 struct berval authzID = BER_BVNULL;
1384 /* if SASL supports native authz, prepare for it */
1385 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1386 ( mt->mt_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1388 switch ( mt->mt_idassert_mode ) {
1389 case LDAP_BACK_IDASSERT_OTHERID:
1390 case LDAP_BACK_IDASSERT_OTHERDN:
1391 authzID = mt->mt_idassert_authzID;
1394 case LDAP_BACK_IDASSERT_ANONYMOUS:
1395 BER_BVSTR( &authzID, "dn:" );
1398 case LDAP_BACK_IDASSERT_SELF:
1399 if ( BER_BVISNULL( &ndn ) ) {
1400 /* connection is not authc'd, so don't idassert */
1401 BER_BVSTR( &authzID, "dn:" );
1404 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1405 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1406 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1407 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1408 ndn.bv_val, ndn.bv_len + 1 );
1417 if ( mt->mt_idassert_secprops != NULL ) {
1418 rs->sr_err = ldap_set_option( msc->msc_ld,
1419 LDAP_OPT_X_SASL_SECPROPS,
1420 (void *)mt->mt_idassert_secprops );
1422 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1423 rs->sr_err = LDAP_OTHER;
1424 if ( sendok & LDAP_BACK_SENDERR ) {
1425 send_ldap_result( op, rs );
1427 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1432 defaults = lutil_sasl_defaults( msc->msc_ld,
1433 mt->mt_idassert_sasl_mech.bv_val,
1434 mt->mt_idassert_sasl_realm.bv_val,
1435 mt->mt_idassert_authcID.bv_val,
1436 mt->mt_idassert_passwd.bv_val,
1438 if ( defaults == NULL ) {
1439 rs->sr_err = LDAP_OTHER;
1440 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1441 if ( sendok & LDAP_BACK_SENDERR ) {
1442 send_ldap_result( op, rs );
1447 rs->sr_err = ldap_sasl_interactive_bind_s( msc->msc_ld, binddn->bv_val,
1448 mt->mt_idassert_sasl_mech.bv_val, NULL, NULL,
1449 LDAP_SASL_QUIET, lutil_sasl_interact,
1452 rs->sr_err = slap_map_api2result( rs );
1453 if ( rs->sr_err != LDAP_SUCCESS ) {
1454 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1455 if ( sendok & LDAP_BACK_SENDERR ) {
1456 send_ldap_result( op, rs );
1460 LDAP_BACK_CONN_ISBOUND_SET( msc );
1463 lutil_sasl_freedefs( defaults );
1465 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1469 #endif /* HAVE_CYRUS_SASL */
1472 *method = mt->mt_idassert_authmethod;
1473 switch ( mt->mt_idassert_authmethod ) {
1474 case LDAP_AUTH_NONE:
1475 BER_BVSTR( binddn, "" );
1476 BER_BVSTR( bindcred, "" );
1479 case LDAP_AUTH_SIMPLE:
1484 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1485 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1486 if ( sendok & LDAP_BACK_SENDERR ) {
1487 send_ldap_result( op, rs );
1497 meta_back_proxy_authz_bind( metaconn_t *mc, int candidate, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1499 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
1500 metatarget_t *mt = mi->mi_targets[ candidate ];
1501 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
1502 struct berval binddn = BER_BVC( "" ),
1503 cred = BER_BVC( "" );
1504 int method = LDAP_AUTH_NONE,
1507 rc = meta_back_proxy_authz_cred( mc, candidate, op, rs, sendok, &binddn, &cred, &method );
1508 if ( rc == LDAP_SUCCESS && !LDAP_BACK_CONN_ISBOUND( msc ) ) {
1512 case LDAP_AUTH_NONE:
1513 case LDAP_AUTH_SIMPLE:
1515 rs->sr_err = ldap_sasl_bind( msc->msc_ld,
1516 binddn.bv_val, LDAP_SASL_SIMPLE,
1517 &cred, NULL, NULL, &msgid );
1518 if ( rs->sr_err != LDAP_X_CONNECTING ) {
1521 ldap_pvt_thread_yield();
1523 rc = meta_back_bind_op_result( op, rs, mc, candidate, msgid, sendok );
1524 if ( rc == LDAP_SUCCESS ) {
1525 /* set rebind stuff in case of successful proxyAuthz bind,
1526 * so that referral chasing is attempted using the right
1528 LDAP_BACK_CONN_ISBOUND_SET( msc );
1529 ber_bvreplace( &msc->msc_bound_ndn, &binddn );
1531 if ( LDAP_BACK_SAVECRED( mi ) ) {
1532 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
1533 memset( msc->msc_cred.bv_val, 0,
1534 msc->msc_cred.bv_len );
1536 ber_bvreplace( &msc->msc_cred, &cred );
1537 ldap_set_rebind_proc( msc->msc_ld, mt->mt_rebind_f, msc );
1548 return LDAP_BACK_CONN_ISBOUND( msc );
1554 * if any needs to be added, it is prepended to existing ones,
1555 * in a newly allocated array. The companion function
1556 * ldap_back_controls_free() must be used to restore the original
1557 * status of op->o_ctrls.
1560 meta_back_controls_add(
1565 LDAPControl ***pctrls )
1567 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
1568 metatarget_t *mt = mi->mi_targets[ candidate ];
1569 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
1571 LDAPControl **ctrls = NULL;
1572 /* set to the maximum number of controls this backend can add */
1573 LDAPControl c[ 2 ] = { 0 };
1574 int n = 0, i, j1 = 0, j2 = 0;
1578 rs->sr_err = LDAP_SUCCESS;
1580 /* don't add controls if protocol is not LDAPv3 */
1581 switch ( mt->mt_version ) {
1586 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1595 /* put controls that go __before__ existing ones here */
1597 /* proxyAuthz for identity assertion */
1598 switch ( ldap_back_proxy_authz_ctrl( op, rs, &msc->msc_bound_ndn,
1599 mt->mt_version, &mt->mt_idassert, &c[ j1 ] ) )
1601 case SLAP_CB_CONTINUE:
1612 /* put controls that go __after__ existing ones here */
1614 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
1615 /* session tracking */
1616 if ( META_BACK_TGT_ST_REQUEST( mt ) ) {
1617 switch ( slap_ctrl_session_tracking_request_add( op, rs, &c[ j1 + j2 ] ) ) {
1618 case SLAP_CB_CONTINUE:
1629 #endif /* SLAP_CONTROL_X_SESSION_TRACKING */
1631 if ( rs->sr_err == SLAP_CB_CONTINUE ) {
1632 rs->sr_err = LDAP_SUCCESS;
1635 /* if nothing to do, just bail out */
1636 if ( j1 == 0 && j2 == 0 ) {
1640 assert( j1 + j1 <= sizeof( c )/sizeof(LDAPControl) );
1642 if ( op->o_ctrls ) {
1643 for ( n = 0; op->o_ctrls[ n ]; n++ )
1644 /* just count ctrls */ ;
1647 ctrls = op->o_tmpalloc( (n + j1 + j2 + 1) * sizeof( LDAPControl * ) + ( j1 + j2 ) * sizeof( LDAPControl ),
1650 ctrls[ 0 ] = (LDAPControl *)&ctrls[ n + j1 + j2 + 1 ];
1651 *ctrls[ 0 ] = c[ 0 ];
1652 for ( i = 1; i < j1; i++ ) {
1653 ctrls[ i ] = &ctrls[ 0 ][ i ];
1654 *ctrls[ i ] = c[ i ];
1659 if ( op->o_ctrls ) {
1660 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1661 ctrls[ i + j1 ] = op->o_ctrls[ i ];
1667 ctrls[ n ] = (LDAPControl *)&ctrls[ n + j2 + 1 ] + j1;
1668 *ctrls[ n ] = c[ j1 ];
1669 for ( i = 1; i < j2; i++ ) {
1670 ctrls[ n + i ] = &ctrls[ n ][ i ];
1671 *ctrls[ n + i ] = c[ i ];
1675 ctrls[ n + j2 ] = NULL;
1678 if ( ctrls == NULL ) {
1679 ctrls = op->o_ctrls;