2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2008 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 ) {
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 /* NOTE: a connection cannot be privileged
254 * and be in the avl tree at the same time
257 LDAP_BACK_CONN_ISPRIV_SET( mc );
258 LDAP_BACK_PCONN_SET( mc, op );
261 LDAP_BACK_CONN_CACHED_SET( mc );
264 LDAP_BACK_CONN_CACHED_CLEAR( mc );
266 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
271 meta_back_release_conn( mi, mc );
275 * rc is LDAP_SUCCESS if at least one bind succeeded,
276 * err is the last error that occurred during a bind;
277 * if at least (and at most?) one bind succeeds, fine.
279 if ( rc != LDAP_SUCCESS ) {
282 * deal with bind failure ...
286 * no target was found within the naming context,
287 * so bind must fail with invalid credentials
289 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
290 rs->sr_err = LDAP_INVALID_CREDENTIALS;
292 rs->sr_err = slap_map_api2result( rs );
294 send_ldap_result( op, rs );
303 meta_back_bind_op_result(
309 ldap_back_send_t sendok )
311 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
312 metatarget_t *mt = mi->mi_targets[ candidate ];
313 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
317 int nretries = mt->mt_nretries;
318 char buf[ SLAP_TEXT_BUFLEN ];
320 Debug( LDAP_DEBUG_TRACE,
321 ">>> %s meta_back_bind_op_result[%d]\n",
322 op->o_log_prefix, candidate, 0 );
324 /* make sure this is clean */
325 assert( rs->sr_ctrls == NULL );
327 if ( rs->sr_err == LDAP_SUCCESS ) {
328 time_t stoptime = (time_t)(-1),
330 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
331 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
332 const char *timeout_text = "Operation timed out";
333 slap_op_t opidx = slap_req2op( op->o_tag );
335 /* since timeout is not specified, compute and use
336 * the one specific to the ongoing operation */
337 if ( opidx == LDAP_REQ_SEARCH ) {
338 if ( op->ors_tlimit <= 0 ) {
342 timeout = op->ors_tlimit;
343 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
348 timeout = mt->mt_timeout[ opidx ];
351 /* better than nothing :) */
352 if ( timeout == 0 ) {
353 if ( mi->mi_idle_timeout ) {
354 timeout = mi->mi_idle_timeout;
356 } else if ( mi->mi_conn_ttl ) {
357 timeout = mi->mi_conn_ttl;
362 stoptime = op->o_time + timeout;
365 LDAP_BACK_TV_SET( &tv );
371 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
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 assert( LDAP_BACK_CONN_BINDING( msc ) );
392 Debug( LDAP_DEBUG_ANY, "### %s meta_back_bind_op_result ldap_unbind_ext[%d] ld=%p\n",
393 op->o_log_prefix, candidate, (void *)msc->msc_ld );
394 #endif /* DEBUG_205 */
396 meta_clear_one_candidate( op, mc, candidate );
397 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
399 rs->sr_err = timeout_err;
400 rs->sr_text = timeout_text;
404 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
407 snprintf( buf, sizeof( buf ),
408 "err=%d (%s) nretries=%d",
409 rs->sr_err, ldap_err2string( rs->sr_err ), nretries );
410 Debug( LDAP_DEBUG_ANY,
411 "### %s meta_back_bind_op_result[%d]: %s.\n",
412 op->o_log_prefix, candidate, buf );
416 /* only touch when activity actually took place... */
417 if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
418 msc->msc_time = op->o_time;
421 /* FIXME: matched? referrals? response controls? */
422 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
423 NULL, NULL, NULL, NULL, 1 );
424 if ( rc != LDAP_SUCCESS ) {
431 rs->sr_err = slap_map_api2result( rs );
433 Debug( LDAP_DEBUG_TRACE,
434 "<<< %s meta_back_bind_op_result[%d] err=%d\n",
435 op->o_log_prefix, candidate, rs->sr_err );
441 * meta_back_single_bind
443 * attempts to perform a bind with creds
446 meta_back_single_bind(
452 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
453 metatarget_t *mt = mi->mi_targets[ candidate ];
454 struct berval mdn = BER_BVNULL;
455 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
458 struct berval save_o_dn;
459 int save_o_do_not_cache;
460 LDAPControl **ctrls = NULL;
462 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
463 ch_free( msc->msc_bound_ndn.bv_val );
464 BER_BVZERO( &msc->msc_bound_ndn );
467 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
468 /* destroy sensitive data */
469 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
470 ch_free( msc->msc_cred.bv_val );
471 BER_BVZERO( &msc->msc_cred );
475 * Rewrite the bind dn if needed
478 dc.conn = op->o_conn;
482 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
483 rs->sr_text = "DN rewrite error";
484 rs->sr_err = LDAP_OTHER;
488 /* don't add proxyAuthz; set the bindDN */
489 save_o_dn = op->o_dn;
490 save_o_do_not_cache = op->o_do_not_cache;
491 op->o_do_not_cache = 1;
492 op->o_dn = op->o_req_dn;
495 rs->sr_err = meta_back_controls_add( op, rs, mc, candidate, &ctrls );
496 op->o_dn = save_o_dn;
497 op->o_do_not_cache = save_o_do_not_cache;
498 if ( rs->sr_err != LDAP_SUCCESS ) {
502 /* FIXME: this fixes the bind problem right now; we need
503 * to use the asynchronous version to get the "matched"
504 * and more in case of failure ... */
505 /* FIXME: should we check if at least some of the op->o_ctrls
506 * can/should be passed? */
508 rs->sr_err = ldap_sasl_bind( msc->msc_ld, mdn.bv_val,
509 LDAP_SASL_SIMPLE, &op->orb_cred,
510 ctrls, NULL, &msgid );
511 if ( rs->sr_err != LDAP_X_CONNECTING ) {
514 ldap_pvt_thread_yield();
517 mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
519 meta_back_bind_op_result( op, rs, mc, candidate, msgid, LDAP_BACK_DONTSEND );
520 if ( rs->sr_err != LDAP_SUCCESS ) {
524 /* If defined, proxyAuthz will be used also when
525 * back-ldap is the authorizing backend; for this
526 * purpose, a successful bind is followed by a
527 * bind with the configured identity assertion */
528 /* NOTE: use with care */
529 if ( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
530 meta_back_proxy_authz_bind( mc, candidate, op, rs, LDAP_BACK_SENDERR );
531 if ( !LDAP_BACK_CONN_ISBOUND( msc ) ) {
537 ber_bvreplace( &msc->msc_bound_ndn, &op->o_req_ndn );
538 LDAP_BACK_CONN_ISBOUND_SET( msc );
539 mc->mc_authz_target = candidate;
541 if ( LDAP_BACK_SAVECRED( mi ) ) {
542 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
543 memset( msc->msc_cred.bv_val, 0,
544 msc->msc_cred.bv_len );
546 ber_bvreplace( &msc->msc_cred, &op->orb_cred );
547 ldap_set_rebind_proc( msc->msc_ld, mt->mt_rebind_f, msc );
551 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED
552 && !BER_BVISEMPTY( &op->o_req_ndn ) )
554 ( void )meta_dncache_update_entry( &mi->mi_cache,
555 &op->o_req_ndn, candidate );
559 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
563 if ( META_BACK_TGT_QUARANTINE( mt ) ) {
564 meta_back_quarantine( op, rs, candidate );
571 * meta_back_single_dobind
574 meta_back_single_dobind(
579 ldap_back_send_t sendok,
583 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
584 metatarget_t *mt = mi->mi_targets[ candidate ];
585 metaconn_t *mc = *mcp;
586 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
587 static struct berval cred = BER_BVC( "" );
590 assert( !LDAP_BACK_CONN_ISBOUND( msc ) );
592 /* NOTE: this obsoletes pseudorootdn */
593 if ( op->o_conn != NULL &&
594 !op->o_do_not_cache &&
595 ( BER_BVISNULL( &msc->msc_bound_ndn ) ||
596 BER_BVISEMPTY( &msc->msc_bound_ndn ) ||
597 ( LDAP_BACK_CONN_ISPRIV( mc ) && dn_match( &msc->msc_bound_ndn, &mt->mt_idassert_authcDN ) ) ||
598 ( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
600 (void)meta_back_proxy_authz_bind( mc, candidate, op, rs, sendok );
604 /* FIXME: should we check if at least some of the op->o_ctrls
605 * can/should be passed? */
607 rs->sr_err = ldap_sasl_bind( msc->msc_ld,
608 "", LDAP_SASL_SIMPLE, &cred,
609 NULL, NULL, &msgid );
610 if ( rs->sr_err != LDAP_X_CONNECTING ) {
613 ldap_pvt_thread_yield();
616 rs->sr_err = meta_back_bind_op_result( op, rs, mc, candidate, msgid, sendok );
619 if ( rs->sr_err != LDAP_SUCCESS ) {
621 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
623 LDAP_BACK_CONN_BINDING_CLEAR( msc );
624 if ( META_BACK_ONERR_STOP( mi ) ) {
625 LDAP_BACK_CONN_TAINTED_SET( mc );
626 meta_back_release_conn_lock( mi, mc, 0 );
630 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
634 if ( META_BACK_TGT_QUARANTINE( mt ) ) {
635 meta_back_quarantine( op, rs, candidate );
649 ldap_back_send_t sendok )
651 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
657 SlapReply *candidates;
659 if ( be_isroot( op ) ) {
663 Debug( LDAP_DEBUG_TRACE,
664 "%s meta_back_dobind: conn=%ld%s\n",
666 LDAP_BACK_PCONN_ID( mc ),
667 isroot ? " (isroot)" : "" );
670 * all the targets are bound as pseudoroot
672 if ( mc->mc_authz_target == META_BOUND_ALL ) {
677 candidates = meta_back_candidates_get( op );
679 for ( i = 0; i < mi->mi_ntargets; i++ ) {
680 metatarget_t *mt = mi->mi_targets[ i ];
681 metasingleconn_t *msc = &mc->mc_conns[ i ];
687 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
691 assert( msc->msc_ld != NULL );
694 * If the target is already bound it is skipped
698 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
699 if ( LDAP_BACK_CONN_ISBOUND( msc )
700 || ( LDAP_BACK_CONN_ISANON( msc )
701 && mt->mt_idassert_authmethod == LDAP_AUTH_NONE ) )
703 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
707 } else if ( META_BACK_CONN_CREATING( msc ) || LDAP_BACK_CONN_BINDING( msc ) )
709 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
710 ldap_pvt_thread_yield();
715 LDAP_BACK_CONN_BINDING_SET( msc );
716 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
718 rc = meta_back_single_dobind( op, rs, &mc, i,
719 LDAP_BACK_DONTSEND, mt->mt_nretries, 1 );
721 * NOTE: meta_back_single_dobind() already retries;
722 * in case of failure, it resets mc...
724 if ( rc != LDAP_SUCCESS ) {
725 char buf[ SLAP_TEXT_BUFLEN ];
728 /* meta_back_single_dobind() already sent
729 * response and released connection */
734 if ( rc == LDAP_UNAVAILABLE ) {
735 /* FIXME: meta_back_retry() already re-calls
736 * meta_back_single_dobind() */
737 if ( meta_back_retry( op, rs, &mc, i, sendok ) ) {
742 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
743 LDAP_BACK_CONN_BINDING_CLEAR( msc );
744 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
745 meta_back_release_conn( mi, mc );
751 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
752 LDAP_BACK_CONN_BINDING_CLEAR( msc );
753 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
755 snprintf( buf, sizeof( buf ),
756 "meta_back_dobind[%d]: (%s) err=%d (%s).",
757 i, isroot ? op->o_bd->be_rootdn.bv_val : "anonymous",
758 rc, ldap_err2string( rc ) );
759 Debug( LDAP_DEBUG_ANY,
761 op->o_log_prefix, buf, 0 );
764 * null cred bind should always succeed
765 * as anonymous, so a failure means
766 * the target is no longer candidate possibly
767 * due to technical reasons (remote host down?)
768 * so better clear the handle
770 /* leave the target candidate, but record the error for later use */
771 candidates[ i ].sr_err = rc;
772 if ( META_BACK_ONERR_STOP( mi ) ) {
781 Debug( LDAP_DEBUG_TRACE,
782 "%s meta_back_dobind[%d]: "
785 isroot ? op->o_bd->be_rootdn.bv_val : "anonymous" );
787 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
788 LDAP_BACK_CONN_BINDING_CLEAR( msc );
790 LDAP_BACK_CONN_ISBOUND_SET( msc );
792 LDAP_BACK_CONN_ISANON_SET( msc );
794 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
799 Debug( LDAP_DEBUG_TRACE,
800 "%s meta_back_dobind: conn=%ld bound=%d\n",
801 op->o_log_prefix, LDAP_BACK_PCONN_ID( mc ), bound );
804 meta_back_release_conn( mi, mc );
807 if ( sendok & LDAP_BACK_SENDERR ) {
808 if ( rs->sr_err == LDAP_SUCCESS ) {
809 rs->sr_err = LDAP_BUSY;
811 send_ldap_result( op, rs );
817 return ( bound > 0 );
821 * meta_back_default_rebind
823 * This is a callback used for chasing referrals using the same
824 * credentials as the original user on this session.
827 meta_back_default_rebind(
829 LDAP_CONST char *url,
834 metasingleconn_t *msc = ( metasingleconn_t * )params;
836 return ldap_sasl_bind_s( ld, msc->msc_bound_ndn.bv_val,
837 LDAP_SASL_SIMPLE, &msc->msc_cred,
842 * meta_back_default_urllist
844 * This is a callback used for mucking with the urllist
847 meta_back_default_urllist(
849 LDAPURLDesc **urllist,
853 metatarget_t *mt = (metatarget_t *)params;
854 LDAPURLDesc **urltail;
856 if ( urllist == url ) {
860 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
867 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
869 ch_free( mt->mt_uri );
872 ldap_get_option( ld, LDAP_OPT_URI, (void *)&mt->mt_uri );
873 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
885 ldap_back_send_t sendok )
887 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
889 metatarget_t *mt = mi->mi_targets[ candidate ];
890 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
894 Debug( LDAP_DEBUG_TRACE, ">>> %s meta_back_cancel[%d] msgid=%d\n",
895 op->o_log_prefix, candidate, msgid );
897 /* default behavior */
898 if ( META_BACK_TGT_ABANDON( mt ) ) {
899 rc = ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
901 } else if ( META_BACK_TGT_IGNORE( mt ) ) {
902 rc = ldap_pvt_discard( msc->msc_ld, msgid );
904 } else if ( META_BACK_TGT_CANCEL( mt ) ) {
905 rc = ldap_cancel_s( msc->msc_ld, msgid, NULL, NULL );
911 Debug( LDAP_DEBUG_TRACE, "<<< %s meta_back_cancel[%d] err=%d\n",
912 op->o_log_prefix, candidate, rc );
920 * FIXME: error return must be handled in a cleaner way ...
930 ldap_back_send_t sendok )
932 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
934 const char *save_text = rs->sr_text,
935 *save_matched = rs->sr_matched;
936 BerVarray save_ref = rs->sr_ref;
937 LDAPControl **save_ctrls = rs->sr_ctrls;
938 void *matched_ctx = NULL;
940 char *matched = NULL;
943 LDAPControl **ctrls = NULL;
945 assert( mc != NULL );
948 rs->sr_matched = NULL;
952 if ( candidate != META_TARGET_NONE ) {
953 metatarget_t *mt = mi->mi_targets[ candidate ];
954 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
956 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
958 if ( ERR_OK( rs->sr_err ) ) {
961 LDAPMessage *res = NULL;
962 time_t stoptime = (time_t)(-1);
963 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
964 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
965 const char *timeout_text = "Operation timed out";
967 /* if timeout is not specified, compute and use
968 * the one specific to the ongoing operation */
969 if ( timeout == (time_t)(-1) ) {
970 slap_op_t opidx = slap_req2op( op->o_tag );
972 if ( opidx == SLAP_OP_SEARCH ) {
973 if ( op->ors_tlimit <= 0 ) {
977 timeout = op->ors_tlimit;
978 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
983 timeout = mt->mt_timeout[ opidx ];
987 /* better than nothing :) */
988 if ( timeout == 0 ) {
989 if ( mi->mi_idle_timeout ) {
990 timeout = mi->mi_idle_timeout;
992 } else if ( mi->mi_conn_ttl ) {
993 timeout = mi->mi_conn_ttl;
998 stoptime = op->o_time + timeout;
1001 LDAP_BACK_TV_SET( &tv );
1004 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1007 if ( timeout && slap_get_time() > stoptime ) {
1008 (void)meta_back_cancel( mc, op, rs, msgid, candidate, sendok );
1009 rs->sr_err = timeout_err;
1010 rs->sr_text = timeout_text;
1014 LDAP_BACK_TV_SET( &tv );
1015 ldap_pvt_thread_yield();
1019 ldap_get_option( msc->msc_ld, LDAP_OPT_RESULT_CODE,
1024 /* otherwise get the result; if it is not
1025 * LDAP_SUCCESS, record it in the reply
1026 * structure (this includes
1027 * LDAP_COMPARE_{TRUE|FALSE}) */
1029 /* only touch when activity actually took place... */
1030 if ( mi->mi_idle_timeout != 0 && msc->msc_time < op->o_time ) {
1031 msc->msc_time = op->o_time;
1034 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
1035 &matched, &text, &refs, &ctrls, 1 );
1038 if ( rc != LDAP_SUCCESS ) {
1042 /* RFC 4511: referrals can only appear
1043 * if result code is LDAP_REFERRAL */
1045 && refs[ 0 ] != NULL
1046 && refs[ 0 ][ 0 ] != '\0' )
1048 if ( rs->sr_err != LDAP_REFERRAL ) {
1049 Debug( LDAP_DEBUG_ANY,
1050 "%s meta_back_op_result[%d]: "
1051 "got referrals with err=%d\n",
1053 candidate, rs->sr_err );
1058 for ( i = 0; refs[ i ] != NULL; i++ )
1060 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1062 for ( i = 0; refs[ i ] != NULL; i++ ) {
1063 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1065 BER_BVZERO( &rs->sr_ref[ i ] );
1068 } else if ( rs->sr_err == LDAP_REFERRAL ) {
1069 Debug( LDAP_DEBUG_ANY,
1070 "%s meta_back_op_result[%d]: "
1071 "got err=%d with null "
1072 "or empty referrals\n",
1074 candidate, rs->sr_err );
1076 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1079 if ( ctrls != NULL ) {
1080 rs->sr_ctrls = ctrls;
1084 assert( res == NULL );
1087 /* if the error in the reply structure is not
1088 * LDAP_SUCCESS, try to map it from client
1089 * to server error */
1090 if ( !ERR_OK( rs->sr_err ) ) {
1091 rs->sr_err = slap_map_api2result( rs );
1093 /* internal ops ( op->o_conn == NULL )
1094 * must not reply to client */
1095 if ( op->o_conn && !op->o_do_not_cache && matched ) {
1097 /* record the (massaged) matched
1098 * DN into the reply structure */
1099 rs->sr_matched = matched;
1103 if ( META_BACK_TGT_QUARANTINE( mt ) ) {
1104 meta_back_quarantine( op, rs, candidate );
1111 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1112 metasingleconn_t *msc = &mc->mc_conns[ i ];
1114 char *xmatched = NULL;
1116 rs->sr_err = LDAP_SUCCESS;
1118 ldap_get_option( msc->msc_ld, LDAP_OPT_RESULT_CODE, &rs->sr_err );
1119 if ( rs->sr_err != LDAP_SUCCESS ) {
1121 * better check the type of error. In some cases
1122 * (search ?) it might be better to return a
1123 * success if at least one of the targets gave
1124 * positive result ...
1126 ldap_get_option( msc->msc_ld,
1127 LDAP_OPT_DIAGNOSTIC_MESSAGE, &xtext );
1128 if ( xtext != NULL && xtext [ 0 ] == '\0' ) {
1129 ldap_memfree( xtext );
1133 ldap_get_option( msc->msc_ld,
1134 LDAP_OPT_MATCHED_DN, &xmatched );
1135 if ( xmatched != NULL && xmatched[ 0 ] == '\0' ) {
1136 ldap_memfree( xmatched );
1140 rs->sr_err = slap_map_api2result( rs );
1142 if ( LogTest( LDAP_DEBUG_ANY ) ) {
1143 char buf[ SLAP_TEXT_BUFLEN ];
1145 snprintf( buf, sizeof( buf ),
1146 "meta_back_op_result[%d] "
1147 "err=%d text=\"%s\" matched=\"%s\"",
1149 ( xtext ? xtext : "" ),
1150 ( xmatched ? xmatched : "" ) );
1151 Debug( LDAP_DEBUG_ANY, "%s %s.\n",
1152 op->o_log_prefix, buf, 0 );
1156 * FIXME: need to rewrite "match" (need rwinfo)
1158 switch ( rs->sr_err ) {
1161 if ( xtext != NULL ) {
1163 ldap_memfree( text );
1168 if ( xmatched != NULL ) {
1170 ldap_memfree( matched );
1179 ldap_memfree( xtext );
1183 ldap_memfree( xmatched );
1187 if ( META_BACK_TGT_QUARANTINE( mi->mi_targets[ i ] ) ) {
1188 meta_back_quarantine( op, rs, i );
1192 if ( err != LDAP_SUCCESS ) {
1197 if ( matched != NULL ) {
1198 struct berval dn, pdn;
1200 ber_str2bv( matched, 0, 0, &dn );
1201 if ( dnPretty( NULL, &dn, &pdn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
1202 ldap_memfree( matched );
1203 matched_ctx = op->o_tmpmemctx;
1204 matched = pdn.bv_val;
1206 rs->sr_matched = matched;
1210 ( ( sendok & LDAP_BACK_SENDOK )
1211 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1213 send_ldap_result( op, rs );
1216 op->o_tmpfree( (char *)rs->sr_matched, matched_ctx );
1219 ldap_memfree( text );
1222 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1226 ber_memvfree( (void **)refs );
1229 assert( rs->sr_ctrls != NULL );
1230 ldap_controls_free( ctrls );
1233 rs->sr_text = save_text;
1234 rs->sr_matched = save_matched;
1235 rs->sr_ref = save_ref;
1236 rs->sr_ctrls = save_ctrls;
1238 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1242 * meta_back_proxy_authz_cred()
1244 * prepares credentials & method for meta_back_proxy_authz_bind();
1245 * or, if method is SASL, performs the SASL bind directly.
1248 meta_back_proxy_authz_cred(
1253 ldap_back_send_t sendok,
1254 struct berval *binddn,
1255 struct berval *bindcred,
1258 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
1259 metatarget_t *mt = mi->mi_targets[ candidate ];
1260 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
1264 /* don't proxyAuthz if protocol is not LDAPv3 */
1265 switch ( mt->mt_version ) {
1270 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1276 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1277 if ( sendok & LDAP_BACK_SENDERR ) {
1278 send_ldap_result( op, rs );
1280 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1284 if ( op->o_tag == LDAP_REQ_BIND ) {
1285 ndn = op->o_req_ndn;
1287 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1288 ndn = op->o_conn->c_ndn;
1295 * FIXME: we need to let clients use proxyAuthz
1296 * otherwise we cannot do symmetric pools of servers;
1297 * we have to live with the fact that a user can
1298 * authorize itself as any ID that is allowed
1299 * by the authzTo directive of the "proxyauthzdn".
1302 * NOTE: current Proxy Authorization specification
1303 * and implementation do not allow proxy authorization
1304 * control to be provided with Bind requests
1307 * if no bind took place yet, but the connection is bound
1308 * and the "proxyauthzdn" is set, then bind as
1309 * "proxyauthzdn" and explicitly add the proxyAuthz
1310 * control to every operation with the dn bound
1311 * to the connection as control value.
1314 /* bind as proxyauthzdn only if no idassert mode
1315 * is requested, or if the client's identity
1317 switch ( mt->mt_idassert_mode ) {
1318 case LDAP_BACK_IDASSERT_LEGACY:
1319 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1320 if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) && !BER_BVISEMPTY( &mt->mt_idassert_authcDN ) )
1322 *binddn = mt->mt_idassert_authcDN;
1323 *bindcred = mt->mt_idassert_passwd;
1330 /* NOTE: rootdn can always idassert */
1331 if ( BER_BVISNULL( &ndn )
1332 && mt->mt_idassert_authz == NULL
1333 && !( mt->mt_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL ) )
1335 if ( mt->mt_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1336 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1337 if ( sendok & LDAP_BACK_SENDERR ) {
1338 send_ldap_result( op, rs );
1340 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1345 rs->sr_err = LDAP_SUCCESS;
1346 *binddn = slap_empty_bv;
1347 *bindcred = slap_empty_bv;
1350 } else if ( mt->mt_idassert_authz && !be_isroot( op ) ) {
1351 struct berval authcDN;
1353 if ( BER_BVISNULL( &ndn ) ) {
1354 authcDN = slap_empty_bv;
1359 rs->sr_err = slap_sasl_matches( op, mt->mt_idassert_authz,
1360 &authcDN, &authcDN );
1361 if ( rs->sr_err != LDAP_SUCCESS ) {
1362 if ( mt->mt_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1363 if ( sendok & LDAP_BACK_SENDERR ) {
1364 send_ldap_result( op, rs );
1366 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1370 rs->sr_err = LDAP_SUCCESS;
1371 *binddn = slap_empty_bv;
1372 *bindcred = slap_empty_bv;
1377 *binddn = mt->mt_idassert_authcDN;
1378 *bindcred = mt->mt_idassert_passwd;
1383 if ( dobind && mt->mt_idassert_authmethod == LDAP_AUTH_SASL ) {
1384 #ifdef HAVE_CYRUS_SASL
1385 void *defaults = NULL;
1386 struct berval authzID = BER_BVNULL;
1389 /* if SASL supports native authz, prepare for it */
1390 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1391 ( mt->mt_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1393 switch ( mt->mt_idassert_mode ) {
1394 case LDAP_BACK_IDASSERT_OTHERID:
1395 case LDAP_BACK_IDASSERT_OTHERDN:
1396 authzID = mt->mt_idassert_authzID;
1399 case LDAP_BACK_IDASSERT_ANONYMOUS:
1400 BER_BVSTR( &authzID, "dn:" );
1403 case LDAP_BACK_IDASSERT_SELF:
1404 if ( BER_BVISNULL( &ndn ) ) {
1405 /* connection is not authc'd, so don't idassert */
1406 BER_BVSTR( &authzID, "dn:" );
1409 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1410 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1411 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1412 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1413 ndn.bv_val, ndn.bv_len + 1 );
1422 if ( mt->mt_idassert_secprops != NULL ) {
1423 rs->sr_err = ldap_set_option( msc->msc_ld,
1424 LDAP_OPT_X_SASL_SECPROPS,
1425 (void *)mt->mt_idassert_secprops );
1427 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1428 rs->sr_err = LDAP_OTHER;
1429 if ( sendok & LDAP_BACK_SENDERR ) {
1430 send_ldap_result( op, rs );
1432 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1437 defaults = lutil_sasl_defaults( msc->msc_ld,
1438 mt->mt_idassert_sasl_mech.bv_val,
1439 mt->mt_idassert_sasl_realm.bv_val,
1440 mt->mt_idassert_authcID.bv_val,
1441 mt->mt_idassert_passwd.bv_val,
1443 if ( defaults == NULL ) {
1444 rs->sr_err = LDAP_OTHER;
1445 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1446 if ( sendok & LDAP_BACK_SENDERR ) {
1447 send_ldap_result( op, rs );
1452 rs->sr_err = ldap_sasl_interactive_bind_s( msc->msc_ld, binddn->bv_val,
1453 mt->mt_idassert_sasl_mech.bv_val, NULL, NULL,
1454 LDAP_SASL_QUIET, lutil_sasl_interact,
1457 rs->sr_err = slap_map_api2result( rs );
1458 if ( rs->sr_err != LDAP_SUCCESS ) {
1459 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1460 if ( sendok & LDAP_BACK_SENDERR ) {
1461 send_ldap_result( op, rs );
1465 LDAP_BACK_CONN_ISBOUND_SET( msc );
1468 lutil_sasl_freedefs( defaults );
1470 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1474 #endif /* HAVE_CYRUS_SASL */
1477 *method = mt->mt_idassert_authmethod;
1478 switch ( mt->mt_idassert_authmethod ) {
1479 case LDAP_AUTH_NONE:
1480 BER_BVSTR( binddn, "" );
1481 BER_BVSTR( bindcred, "" );
1484 case LDAP_AUTH_SIMPLE:
1489 LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
1490 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1491 if ( sendok & LDAP_BACK_SENDERR ) {
1492 send_ldap_result( op, rs );
1502 meta_back_proxy_authz_bind( metaconn_t *mc, int candidate, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1504 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
1505 metatarget_t *mt = mi->mi_targets[ candidate ];
1506 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
1507 struct berval binddn = BER_BVC( "" ),
1508 cred = BER_BVC( "" );
1509 int method = LDAP_AUTH_NONE,
1512 rc = meta_back_proxy_authz_cred( mc, candidate, op, rs, sendok, &binddn, &cred, &method );
1513 if ( rc == LDAP_SUCCESS && !LDAP_BACK_CONN_ISBOUND( msc ) ) {
1517 case LDAP_AUTH_NONE:
1518 case LDAP_AUTH_SIMPLE:
1520 rs->sr_err = ldap_sasl_bind( msc->msc_ld,
1521 binddn.bv_val, LDAP_SASL_SIMPLE,
1522 &cred, NULL, NULL, &msgid );
1523 if ( rs->sr_err != LDAP_X_CONNECTING ) {
1526 ldap_pvt_thread_yield();
1528 rc = meta_back_bind_op_result( op, rs, mc, candidate, msgid, sendok );
1529 if ( rc == LDAP_SUCCESS ) {
1530 /* set rebind stuff in case of successful proxyAuthz bind,
1531 * so that referral chasing is attempted using the right
1533 LDAP_BACK_CONN_ISBOUND_SET( msc );
1534 ber_bvreplace( &msc->msc_bound_ndn, &binddn );
1536 if ( LDAP_BACK_SAVECRED( mi ) ) {
1537 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
1538 memset( msc->msc_cred.bv_val, 0,
1539 msc->msc_cred.bv_len );
1541 ber_bvreplace( &msc->msc_cred, &cred );
1542 ldap_set_rebind_proc( msc->msc_ld, mt->mt_rebind_f, msc );
1553 return LDAP_BACK_CONN_ISBOUND( msc );
1559 * if any needs to be added, it is prepended to existing ones,
1560 * in a newly allocated array. The companion function
1561 * mi->mi_ldap_extra->controls_free() must be used to restore the original
1562 * status of op->o_ctrls.
1565 meta_back_controls_add(
1570 LDAPControl ***pctrls )
1572 metainfo_t *mi = (metainfo_t *)op->o_bd->be_private;
1573 metatarget_t *mt = mi->mi_targets[ candidate ];
1574 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
1576 LDAPControl **ctrls = NULL;
1577 /* set to the maximum number of controls this backend can add */
1578 LDAPControl c[ 2 ] = { 0 };
1579 int n = 0, i, j1 = 0, j2 = 0;
1583 rs->sr_err = LDAP_SUCCESS;
1585 /* don't add controls if protocol is not LDAPv3 */
1586 switch ( mt->mt_version ) {
1591 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1600 /* put controls that go __before__ existing ones here */
1602 /* proxyAuthz for identity assertion */
1603 switch ( mi->mi_ldap_extra->proxy_authz_ctrl( op, rs, &msc->msc_bound_ndn,
1604 mt->mt_version, &mt->mt_idassert, &c[ j1 ] ) )
1606 case SLAP_CB_CONTINUE:
1617 /* put controls that go __after__ existing ones here */
1619 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
1620 /* session tracking */
1621 if ( META_BACK_TGT_ST_REQUEST( mt ) ) {
1622 switch ( slap_ctrl_session_tracking_request_add( op, rs, &c[ j1 + j2 ] ) ) {
1623 case SLAP_CB_CONTINUE:
1634 #endif /* SLAP_CONTROL_X_SESSION_TRACKING */
1636 if ( rs->sr_err == SLAP_CB_CONTINUE ) {
1637 rs->sr_err = LDAP_SUCCESS;
1640 /* if nothing to do, just bail out */
1641 if ( j1 == 0 && j2 == 0 ) {
1645 assert( j1 + j1 <= sizeof( c )/sizeof(LDAPControl) );
1647 if ( op->o_ctrls ) {
1648 for ( n = 0; op->o_ctrls[ n ]; n++ )
1649 /* just count ctrls */ ;
1652 ctrls = op->o_tmpalloc( (n + j1 + j2 + 1) * sizeof( LDAPControl * ) + ( j1 + j2 ) * sizeof( LDAPControl ),
1655 ctrls[ 0 ] = (LDAPControl *)&ctrls[ n + j1 + j2 + 1 ];
1656 *ctrls[ 0 ] = c[ 0 ];
1657 for ( i = 1; i < j1; i++ ) {
1658 ctrls[ i ] = &ctrls[ 0 ][ i ];
1659 *ctrls[ i ] = c[ i ];
1664 if ( op->o_ctrls ) {
1665 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1666 ctrls[ i + j1 ] = op->o_ctrls[ i ];
1672 ctrls[ n ] = (LDAPControl *)&ctrls[ n + j2 + 1 ] + j1;
1673 *ctrls[ n ] = c[ j1 ];
1674 for ( i = 1; i < j2; i++ ) {
1675 ctrls[ n + i ] = &ctrls[ n ][ i ];
1676 *ctrls[ n + i ] = c[ i ];
1680 ctrls[ n + j2 ] = NULL;
1683 if ( ctrls == NULL ) {
1684 ctrls = op->o_ctrls;