1 /* bind.c - ldap backend bind function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2005 The OpenLDAP Foundation.
6 * Portions Copyright 2000-2003 Pierangelo Masarati.
7 * Portions Copyright 1999-2003 Howard Chu.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
19 * This work was initially developed by Howard Chu for inclusion
20 * in OpenLDAP Software and subsequently enhanced by Pierangelo
29 #include <ac/socket.h>
30 #include <ac/string.h>
34 #include "back-ldap.h"
36 #include <lutil_ldap.h>
38 #define PRINT_CONNTREE 0
40 static LDAP_REBIND_PROC ldap_back_rebind;
43 ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs );
46 ldap_back_prepare_conn( struct ldapconn **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
49 ldap_back_bind( Operation *op, SlapReply *rs )
51 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
57 lc = ldap_back_getconn( op, rs, LDAP_BACK_SENDERR );
62 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
63 ch_free( lc->lc_bound_ndn.bv_val );
64 BER_BVZERO( &lc->lc_bound_ndn );
68 /* method is always LDAP_AUTH_SIMPLE if we got here */
69 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
71 &op->orb_cred, op->o_ctrls, NULL, &msgid );
72 rc = ldap_back_op_result( lc, op, rs, msgid, LDAP_BACK_SENDERR );
74 if ( rc == LDAP_SUCCESS ) {
75 /* If defined, proxyAuthz will be used also when
76 * back-ldap is the authorizing backend; for this
77 * purpose, a successful bind is followed by a
78 * bind with the configured identity assertion */
79 /* NOTE: use with care */
80 if ( li->idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
81 ldap_back_proxy_authz_bind( lc, op, rs );
82 if ( lc->lc_bound == 0 ) {
89 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
91 if ( LDAP_BACK_SAVECRED( li ) ) {
92 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
93 memset( lc->lc_cred.bv_val, 0,
96 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
97 ldap_set_rebind_proc( lc->lc_ld, ldap_back_rebind, lc );
102 /* must re-insert if local DN changed as result of bind */
103 if ( lc->lc_bound && !dn_match( &op->o_req_ndn, &lc->lc_local_ndn ) ) {
106 /* wait for all other ops to release the connection */
108 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
109 if ( lc->lc_refcnt > 1 ) {
110 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
111 ldap_pvt_thread_yield();
115 assert( lc->lc_refcnt == 1 );
116 lc = avl_delete( &li->conntree, (caddr_t)lc,
117 ldap_back_conn_cmp );
118 assert( lc != NULL );
120 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
121 lerr = avl_insert( &li->conntree, (caddr_t)lc,
122 ldap_back_conn_cmp, ldap_back_conn_dup );
123 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
125 /* we can do this because lc_refcnt == 1 */
126 ldap_back_conn_free( lc );
132 ldap_back_release_conn( op, rs, lc );
141 * compares two struct ldapconn based on the value of the conn pointer;
145 ldap_back_conn_cmp( const void *c1, const void *c2 )
147 const struct ldapconn *lc1 = (const struct ldapconn *)c1;
148 const struct ldapconn *lc2 = (const struct ldapconn *)c2;
151 /* If local DNs don't match, it is definitely not a match */
152 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
157 /* For shared sessions, conn is NULL. Only explicitly
158 * bound sessions will have non-NULL conn.
160 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
166 * returns -1 in case a duplicate struct ldapconn has been inserted;
170 ldap_back_conn_dup( void *c1, void *c2 )
172 struct ldapconn *lc1 = (struct ldapconn *)c1;
173 struct ldapconn *lc2 = (struct ldapconn *)c2;
175 /* Cannot have more than one shared session with same DN */
176 if ( dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) &&
177 lc1->lc_conn == lc2->lc_conn )
185 #if PRINT_CONNTREE > 0
187 ravl_print( Avlnode *root, int depth )
196 ravl_print( root->avl_right, depth+1 );
198 for ( i = 0; i < depth; i++ ) {
203 printf( "lc(%lx) local(%s) conn(%lx) %d\n",
204 lc, lc->lc_local_ndn.bv_val, lc->lc_conn, root->avl_bf );
206 ravl_print( root->avl_left, depth+1 );
210 myprint( Avlnode *root )
212 printf( "********\n" );
215 printf( "\tNULL\n" );
218 ravl_print( root, 0 );
221 printf( "********\n" );
223 #endif /* PRINT_CONNTREE */
226 ldap_back_freeconn( Operation *op, struct ldapconn *lc )
228 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
230 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
232 assert( lc->lc_refcnt > 0 );
233 if ( --lc->lc_refcnt == 0 ) {
234 lc = avl_delete( &li->conntree, (caddr_t)lc,
235 ldap_back_conn_cmp );
236 assert( lc != NULL );
238 ldap_back_conn_free( (void *)lc );
241 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
256 int rc = LDAP_SUCCESS;
259 /* this is ridicolous... */
262 /* start TLS ("tls-[try-]{start,propagate}" statements) */
263 if ( ( LDAP_BACK_USE_TLS( &li ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS( &li ) ) )
264 && !ldap_is_ldaps_url( url ) )
266 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
268 * use asynchronous StartTLS
269 * in case, chase referral (not implemented yet)
273 if ( protocol == 0 ) {
274 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
278 if ( protocol < LDAP_VERSION3 ) {
279 protocol = LDAP_VERSION3;
280 /* Set LDAP version */
281 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
282 (const void *)&protocol );
285 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
286 if ( rc == LDAP_SUCCESS ) {
287 LDAPMessage *res = NULL;
289 struct timeval tv = { 0, 100000 };
292 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
296 } else if ( rc == 0 ) {
305 } else if ( rc == LDAP_RES_EXTENDED ) {
306 struct berval *data = NULL;
308 rc = ldap_parse_extended_result( ld, res,
310 if ( rc == LDAP_SUCCESS ) {
311 rc = ldap_result2error( ld, res, 1 );
314 /* FIXME: in case a referral
315 * is returned, should we try
316 * using it instead of the
318 if ( rc == LDAP_SUCCESS ) {
319 rc = ldap_install_tls( ld );
321 } else if ( rc == LDAP_REFERRAL ) {
323 *text = "unwilling to chase referral returned by Start TLS exop";
327 if ( data->bv_val ) {
328 ber_memfree( data->bv_val );
342 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
344 * use synchronous StartTLS
346 rc = ldap_start_tls_s( ld, NULL, NULL );
347 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
349 /* if StartTLS is requested, only attempt it if the URL
350 * is not "ldaps://"; this may occur not only in case
351 * of misconfiguration, but also when used in the chain
352 * overlay, where the "uri" can be parsed out of a referral */
358 case LDAP_SERVER_DOWN:
362 if ( LDAP_BACK_TLS_CRITICAL( &li ) ) {
363 *text = "could not start TLS";
367 /* in case Start TLS is not critical */
379 #endif /* HAVE_TLS */
382 ldap_back_prepare_conn( struct ldapconn **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
384 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
385 int vers = op->o_protocol;
388 int is_tls = op->o_conn->c_is_tls;
389 #endif /* HAVE_TLS */
391 assert( lcp != NULL );
393 rs->sr_err = ldap_initialize( &ld, li->url );
394 if ( rs->sr_err != LDAP_SUCCESS ) {
398 /* Set LDAP version. This will always succeed: If the client
399 * bound with a particular version, then so can we.
401 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&vers );
403 /* automatically chase referrals ("[dont-]chase-referrals" statement) */
404 if ( LDAP_BACK_CHASE_REFERRALS( li ) ) {
405 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
409 rs->sr_err = ldap_back_start_tls( ld,
410 op->o_protocol, &is_tls,
411 li->url, li->flags, &rs->sr_text );
412 if ( rs->sr_err != LDAP_SUCCESS ) {
413 ldap_unbind_ext( ld, NULL, NULL );
416 #endif /* HAVE_TLS */
418 if ( *lcp == NULL ) {
419 *lcp = (struct ldapconn *)ch_calloc( 1, sizeof( struct ldapconn ) );
420 (*lcp)->lc_flags= li->flags;
423 (*lcp)->lc_refcnt = 1;
425 (*lcp)->lc_is_tls = is_tls;
426 #endif /* HAVE_TLS */
429 if ( rs->sr_err != LDAP_SUCCESS ) {
430 rs->sr_err = slap_map_api2result( rs );
431 if ( sendok & LDAP_BACK_SENDERR ) {
432 if ( rs->sr_text == NULL ) {
433 rs->sr_text = "ldap_initialize() failed";
435 send_ldap_result( op, rs );
444 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
446 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
451 /* Searches for a ldapconn in the avl tree */
453 /* Explicit binds must not be shared */
454 if ( op->o_tag == LDAP_REQ_BIND
456 && op->o_conn->c_authz_backend
457 && op->o_bd->be_private == op->o_conn->c_authz_backend->be_private ) )
459 lc_curr.lc_conn = op->o_conn;
463 if ( op->o_conn->c_is_tls ) {
464 lc_curr.lc_conn = LDAP_BACK_PRIV_CONN_TLS;
466 #endif /* HAVE_TLS */
468 lc_curr.lc_conn = LDAP_BACK_PRIV_CONN;
472 /* Internal searches are privileged and shared. So is root. */
473 /* FIXME: there seem to be concurrency issues */
474 if ( op->o_do_not_cache || be_isroot( op ) ) {
475 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
477 if ( op->o_conn->c_is_tls ) {
478 lc_curr.lc_conn = LDAP_BACK_PRIV_CONN_TLS;
480 #endif /* HAVE_TLS */
482 lc_curr.lc_conn = LDAP_BACK_PRIV_CONN;
484 lc_curr.lc_ispriv = 1;
487 lc_curr.lc_local_ndn = op->o_ndn;
490 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
492 lc = (struct ldapconn *)avl_find( li->conntree,
493 (caddr_t)&lc_curr, ldap_back_conn_cmp );
495 refcnt = ++lc->lc_refcnt;
497 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
499 /* Looks like we didn't get a bind. Open a new session... */
501 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
505 lc->lc_conn = lc_curr.lc_conn;
506 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
508 if ( lc_curr.lc_ispriv ) {
509 ber_dupbv( &lc->lc_cred, &li->acl_passwd );
510 ber_dupbv( &lc->lc_bound_ndn, &li->acl_authcDN );
511 lc->lc_ispriv = lc_curr.lc_ispriv;
514 BER_BVZERO( &lc->lc_cred );
515 BER_BVZERO( &lc->lc_bound_ndn );
516 if ( op->o_conn && !BER_BVISEMPTY( &op->o_ndn )
517 && op->o_bd->be_private == op->o_conn->c_authz_backend->be_private )
519 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
524 /* if start TLS failed but it was not mandatory,
525 * check if the non-TLS connection was already
526 * in cache; in case, destroy the newly created
527 * connection and use the existing one */
528 if ( lc->lc_conn == LDAP_BACK_PRIV_CONN_TLS
529 && !ldap_tls_inplace( lc->lc_ld ) )
531 struct ldapconn *tmplc;
533 lc_curr.lc_conn = LDAP_BACK_PRIV_CONN;
534 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
535 tmplc = (struct ldapconn *)avl_find( li->conntree,
536 (caddr_t)&lc_curr, ldap_back_conn_cmp );
537 if ( tmplc != NULL ) {
538 refcnt = ++tmplc->lc_refcnt;
539 ldap_back_conn_free( lc );
542 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
544 if ( tmplc != NULL ) {
548 #endif /* HAVE_TLS */
552 /* Inserts the newly created ldapconn in the avl tree */
553 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
555 assert( lc->lc_refcnt == 1 );
556 rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
557 ldap_back_conn_cmp, ldap_back_conn_dup );
559 #if PRINT_CONNTREE > 0
560 myprint( li->conntree );
561 #endif /* PRINT_CONNTREE */
563 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
565 Debug( LDAP_DEBUG_TRACE,
566 "=>ldap_back_getconn: conn %p inserted (refcnt=%u)\n",
567 (void *)lc, refcnt, 0 );
569 /* Err could be -1 in case a duplicate ldapconn is inserted */
570 if ( rs->sr_err != 0 ) {
571 ldap_back_conn_free( lc );
572 rs->sr_err = LDAP_OTHER;
573 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
574 send_ldap_error( op, rs, LDAP_OTHER,
575 "internal server error" );
581 Debug( LDAP_DEBUG_TRACE,
582 "=>ldap_back_getconn: conn %p fetched (refcnt=%u)\n",
583 (void *)lc, refcnt, 0 );
591 ldap_back_release_conn(
594 struct ldapconn *lc )
596 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
598 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
599 assert( lc->lc_refcnt > 0 );
601 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
607 * Note: as the check for the value of lc->lc_bound was already here, I removed
608 * it from all the callers, and I made the function return the flag, so
609 * it can be used to simplify the check.
611 * Note: dolock indicates whether li->conn_mutex must be locked or not
614 ldap_back_dobind_int(
618 ldap_back_send_t sendok,
625 assert( retries >= 0 );
627 if ( !lc->lc_bound ) {
628 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
631 * FIXME: we need to let clients use proxyAuthz
632 * otherwise we cannot do symmetric pools of servers;
633 * we have to live with the fact that a user can
634 * authorize itself as any ID that is allowed
635 * by the authzTo directive of the "proxyauthzdn".
638 * NOTE: current Proxy Authorization specification
639 * and implementation do not allow proxy authorization
640 * control to be provided with Bind requests
643 * if no bind took place yet, but the connection is bound
644 * and the "idassert-authcDN" (or other ID) is set,
645 * then bind as the asserting identity and explicitly
646 * add the proxyAuthz control to every operation with the
647 * dn bound to the connection as control value.
648 * This is done also if this is the authrizing backend,
649 * but the "override" flag is given to idassert.
650 * It allows to use SASL bind and yet proxyAuthz users
652 if ( op->o_conn != NULL &&
653 !op->o_do_not_cache &&
654 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
655 ( li->idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
657 (void)ldap_back_proxy_authz_bind( lc, op, rs );
661 #ifdef HAVE_CYRUS_SASL
662 if ( lc->lc_ispriv && li->acl_authmethod == LDAP_AUTH_SASL ) {
663 void *defaults = NULL;
665 #if 1 /* will deal with this later... */
666 if ( li->acl_secprops != NULL ) {
667 rc = ldap_set_option( lc->lc_ld,
668 LDAP_OPT_X_SASL_SECPROPS, li->acl_secprops);
670 if( rc != LDAP_OPT_SUCCESS ) {
671 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
672 "(%s,SECPROPS,\"%s\") failed!\n",
673 li->url, li->acl_secprops, 0 );
679 defaults = lutil_sasl_defaults( lc->lc_ld,
680 li->acl_sasl_mech.bv_val,
681 li->acl_sasl_realm.bv_val,
682 li->acl_authcID.bv_val,
683 li->acl_passwd.bv_val,
686 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
687 li->acl_authcDN.bv_val,
688 li->acl_sasl_mech.bv_val, NULL, NULL,
689 LDAP_SASL_QUIET, lutil_sasl_interact,
692 lutil_sasl_freedefs( defaults );
694 rs->sr_err = slap_map_api2result( rs );
695 if ( rs->sr_err != LDAP_SUCCESS ) {
697 send_ldap_result( op, rs );
704 #endif /* HAVE_CYRUS_SASL */
707 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
708 lc->lc_bound_ndn.bv_val,
709 LDAP_SASL_SIMPLE, &lc->lc_cred,
710 NULL, NULL, &msgid );
712 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
715 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
718 assert( lc->lc_refcnt > 0 );
719 if ( lc->lc_refcnt == 1 ) {
720 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
723 /* lc here must be the regular lc, reset and ready for init */
724 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
727 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
729 if ( rs->sr_err == LDAP_SUCCESS ) {
735 ldap_back_freeconn( op, lc );
736 rs->sr_err = slap_map_api2result( rs );
741 rc = ldap_back_op_result( lc, op, rs, msgid, sendok );
742 if ( rc == LDAP_SUCCESS ) {
746 ldap_back_release_conn( op, rs, lc );
756 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
758 return ldap_back_dobind_int( lc, op, rs, sendok, 1, 1 );
764 * This is a callback used for chasing referrals using the same
765 * credentials as the original user on this session.
768 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
769 ber_int_t msgid, void *params )
771 struct ldapconn *lc = (struct ldapconn *)params;
774 /* ... otherwise we couldn't get here */
775 assert( lc != NULL );
777 if ( !ldap_tls_inplace( ld ) ) {
778 int is_tls = lc->lc_is_tls,
780 const char *text = NULL;
782 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags, &text );
783 if ( rc != LDAP_SUCCESS ) {
787 #endif /* HAVE_TLS */
789 /* FIXME: add checks on the URL/identity? */
791 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
792 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
801 ldap_back_send_t sendok )
804 LDAPMessage *res = NULL;
807 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
810 rs->sr_matched = NULL;
812 /* if the error recorded in the reply corresponds
813 * to a successful state, get the error from the
814 * remote server response */
815 if ( ERR_OK( rs->sr_err ) ) {
817 struct timeval tv = { 0, 100000 };
820 /* if result parsing fails, note the failure reason */
821 switch ( ldap_result( lc->lc_ld, msgid, 1, &tv, &res ) ) {
824 tv.tv_usec = 100000; /* 0.1 s */
825 ldap_pvt_thread_yield();
829 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
834 /* otherwise get the result; if it is not
835 * LDAP_SUCCESS, record it in the reply
836 * structure (this includes
837 * LDAP_COMPARE_{TRUE|FALSE}) */
839 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
840 &match, &text, NULL, NULL, 1 );
842 if ( rc != LDAP_SUCCESS ) {
848 /* if the error in the reply structure is not
849 * LDAP_SUCCESS, try to map it from client
851 if ( !ERR_OK( rs->sr_err ) ) {
852 rs->sr_err = slap_map_api2result( rs );
854 /* internal ops ( op->o_conn == NULL )
855 * must not reply to client */
856 if ( op->o_conn && !op->o_do_not_cache && match ) {
858 /* record the (massaged) matched
859 * DN into the reply structure */
860 rs->sr_matched = match;
864 ( ( sendok & LDAP_BACK_SENDOK )
865 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
867 send_ldap_result( op, rs );
870 if ( rs->sr_matched != match ) {
871 free( (char *)rs->sr_matched );
873 rs->sr_matched = NULL;
874 ldap_memfree( match );
877 ldap_memfree( text );
880 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
883 /* return true if bound, false if failed */
885 ldap_back_retry( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
888 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
890 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
892 if ( lc->lc_refcnt == 1 ) {
893 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
897 /* lc here must be the regular lc, reset and ready for init */
898 rc = ldap_back_prepare_conn( &lc, op, rs, sendok );
899 if ( rc == LDAP_SUCCESS ) {
900 rc = ldap_back_dobind_int( lc, op, rs, sendok, 0, 0 );
904 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
910 ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs )
912 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
913 struct berval binddn = slap_empty_bv;
914 struct berval bindcred = slap_empty_bv;
920 * FIXME: we need to let clients use proxyAuthz
921 * otherwise we cannot do symmetric pools of servers;
922 * we have to live with the fact that a user can
923 * authorize itself as any ID that is allowed
924 * by the authzTo directive of the "proxyauthzdn".
927 * NOTE: current Proxy Authorization specification
928 * and implementation do not allow proxy authorization
929 * control to be provided with Bind requests
932 * if no bind took place yet, but the connection is bound
933 * and the "proxyauthzdn" is set, then bind as
934 * "proxyauthzdn" and explicitly add the proxyAuthz
935 * control to every operation with the dn bound
936 * to the connection as control value.
939 /* bind as proxyauthzdn only if no idassert mode
940 * is requested, or if the client's identity
942 switch ( li->idassert_mode ) {
943 case LDAP_BACK_IDASSERT_LEGACY:
944 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) && !BER_BVISEMPTY( &op->o_conn->c_ndn ) ) {
945 if ( !BER_BVISNULL( &li->idassert_authcDN ) && !BER_BVISEMPTY( &li->idassert_authcDN ) )
947 binddn = li->idassert_authcDN;
948 bindcred = li->idassert_passwd;
955 /* NOTE: rootdn can always idassert */
956 if ( li->idassert_authz && !be_isroot( op ) ) {
957 struct berval authcDN;
959 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
960 authcDN = slap_empty_bv;
963 authcDN = op->o_conn->c_ndn;
965 rs->sr_err = slap_sasl_matches( op, li->idassert_authz,
966 &authcDN, &authcDN );
967 if ( rs->sr_err != LDAP_SUCCESS ) {
968 if ( li->idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
969 send_ldap_result( op, rs );
973 rs->sr_err = LDAP_SUCCESS;
974 binddn = slap_empty_bv;
975 bindcred = slap_empty_bv;
983 binddn = li->idassert_authcDN;
984 bindcred = li->idassert_passwd;
989 if ( dobind && li->idassert_authmethod == LDAP_AUTH_SASL ) {
990 #ifdef HAVE_CYRUS_SASL
991 void *defaults = NULL;
992 struct berval authzID = BER_BVNULL;
995 /* if SASL supports native authz, prepare for it */
996 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
997 ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
999 switch ( li->idassert_mode ) {
1000 case LDAP_BACK_IDASSERT_OTHERID:
1001 case LDAP_BACK_IDASSERT_OTHERDN:
1002 authzID = li->idassert_authzID;
1005 case LDAP_BACK_IDASSERT_ANONYMOUS:
1006 BER_BVSTR( &authzID, "dn:" );
1009 case LDAP_BACK_IDASSERT_SELF:
1010 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1011 /* connection is not authc'd, so don't idassert */
1012 BER_BVSTR( &authzID, "dn:" );
1015 authzID.bv_len = STRLENOF( "dn:" ) + op->o_conn->c_ndn.bv_len;
1016 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1017 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1018 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1019 op->o_conn->c_ndn.bv_val, op->o_conn->c_ndn.bv_len + 1 );
1028 #if 0 /* will deal with this later... */
1029 if ( sasl_secprops != NULL ) {
1030 rs->sr_err = ldap_set_option( lc->lc_ld, LDAP_OPT_X_SASL_SECPROPS,
1031 (void *) sasl_secprops );
1033 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1034 send_ldap_result( op, rs );
1041 defaults = lutil_sasl_defaults( lc->lc_ld,
1042 li->idassert_sasl_mech.bv_val,
1043 li->idassert_sasl_realm.bv_val,
1044 li->idassert_authcID.bv_val,
1045 li->idassert_passwd.bv_val,
1048 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1049 li->idassert_sasl_mech.bv_val, NULL, NULL,
1050 LDAP_SASL_QUIET, lutil_sasl_interact,
1053 lutil_sasl_freedefs( defaults );
1055 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1058 rs->sr_err = slap_map_api2result( rs );
1059 if ( rs->sr_err != LDAP_SUCCESS ) {
1061 send_ldap_result( op, rs );
1067 #endif /* HAVE_CYRUS_SASL */
1070 switch ( li->idassert_authmethod ) {
1071 case LDAP_AUTH_SIMPLE:
1072 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1073 binddn.bv_val, LDAP_SASL_SIMPLE,
1074 &bindcred, NULL, NULL, &msgid );
1077 case LDAP_AUTH_NONE:
1084 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1085 send_ldap_result( op, rs );
1089 rc = ldap_back_op_result( lc, op, rs, msgid, LDAP_BACK_SENDERR );
1090 if ( rc == LDAP_SUCCESS ) {
1094 return lc->lc_bound;
1098 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1099 * to existing server-side controls if required; if not,
1100 * the existing server-side controls are placed in *pctrls.
1101 * The caller, after using the controls in client API
1102 * operations, if ( *pctrls != op->o_ctrls ), should
1103 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1104 * The function returns success if the control could
1105 * be added if required, or if it did nothing; in the future,
1106 * it might return some error if it failed.
1108 * if no bind took place yet, but the connection is bound
1109 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1110 * and explicitly add proxyAuthz the control to every operation
1111 * with the dn bound to the connection as control value.
1113 * If no server-side controls are defined for the operation,
1114 * simply add the proxyAuthz control; otherwise, if the
1115 * proxyAuthz control is not already set, add it as
1118 * FIXME: is controls order significant for security?
1119 * ANSWER: controls ordering and interoperability
1120 * must be indicated by the specs of each control; if none
1121 * is specified, the order is irrelevant.
1124 ldap_back_proxy_authz_ctrl(
1125 struct ldapconn *lc,
1128 LDAPControl ***pctrls )
1130 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
1131 LDAPControl **ctrls = NULL;
1134 struct berval assertedID;
1138 rs->sr_err = LDAP_SUCCESS;
1140 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1141 * but if it is not set this test fails. We need a different
1142 * means to detect if idassert is enabled */
1143 if ( ( BER_BVISNULL( &li->idassert_authcID ) || BER_BVISEMPTY( &li->idassert_authcID ) )
1144 && ( BER_BVISNULL( &li->idassert_authcDN ) || BER_BVISEMPTY( &li->idassert_authcDN ) ) )
1149 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1153 if ( li->idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1154 if ( op->o_proxy_authz ) {
1156 * FIXME: we do not want to perform proxyAuthz
1157 * on behalf of the client, because this would
1158 * be performed with "proxyauthzdn" privileges.
1160 * This might actually be too strict, since
1161 * the "proxyauthzdn" authzTo, and each entry's
1162 * authzFrom attributes may be crafted
1163 * to avoid unwanted proxyAuthz to take place.
1166 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1167 rs->sr_text = "proxyAuthz not allowed within namingContext";
1172 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1176 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1180 if ( BER_BVISNULL( &li->idassert_authcDN ) ) {
1184 } else if ( li->idassert_authmethod == LDAP_AUTH_SASL ) {
1185 if ( ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1186 /* && ( !BER_BVISNULL( &op->o_conn->c_ndn ) || lc->lc_bound ) */ )
1188 /* already asserted in SASL via native authz */
1189 /* NOTE: the test on lc->lc_bound is used to trap
1190 * native authorization of anonymous users,
1191 * since in that case op->o_conn->c_ndn is NULL */
1195 } else if ( li->idassert_authz && !be_isroot( op ) ) {
1197 struct berval authcDN;
1199 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1200 authcDN = slap_empty_bv;
1202 authcDN = op->o_conn->c_ndn;
1204 rc = slap_sasl_matches( op, li->idassert_authz,
1205 &authcDN, & authcDN );
1206 if ( rc != LDAP_SUCCESS ) {
1207 if ( li->idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1209 /* op->o_conn->c_ndn is not authorized
1210 * to use idassert */
1217 if ( op->o_proxy_authz ) {
1220 * 1) ignore the already set proxyAuthz control
1221 * 2) leave it in place, and don't set ours
1223 * 4) reject the operation
1225 * option (4) is very drastic
1226 * option (3) will make the remote server reject
1227 * the operation, thus being equivalent to (4)
1228 * option (2) will likely break the idassert
1229 * assumptions, so we cannot accept it;
1230 * option (1) means that we are contradicting
1231 * the client's reques.
1233 * I think (4) is the only correct choice.
1235 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1236 rs->sr_text = "proxyAuthz not allowed within namingContext";
1239 if ( op->o_do_not_cache && op->o_is_auth_check ) {
1240 mode = LDAP_BACK_IDASSERT_NOASSERT;
1243 mode = li->idassert_mode;
1247 case LDAP_BACK_IDASSERT_LEGACY:
1248 case LDAP_BACK_IDASSERT_SELF:
1249 /* original behavior:
1250 * assert the client's identity */
1251 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1252 assertedID = slap_empty_bv;
1254 assertedID = op->o_conn->c_ndn;
1258 case LDAP_BACK_IDASSERT_ANONYMOUS:
1259 /* assert "anonymous" */
1260 assertedID = slap_empty_bv;
1263 case LDAP_BACK_IDASSERT_NOASSERT:
1264 /* don't assert; bind as proxyauthzdn */
1267 case LDAP_BACK_IDASSERT_OTHERID:
1268 case LDAP_BACK_IDASSERT_OTHERDN:
1269 /* assert idassert DN */
1270 assertedID = li->idassert_authzID;
1277 if ( BER_BVISNULL( &assertedID ) ) {
1278 assertedID = slap_empty_bv;
1281 if ( op->o_ctrls ) {
1282 for ( i = 0; op->o_ctrls[ i ]; i++ )
1283 /* just count ctrls */ ;
1286 ctrls = ch_malloc( sizeof( LDAPControl * ) * (i + 2) );
1287 ctrls[ 0 ] = ch_malloc( sizeof( LDAPControl ) );
1289 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1290 ctrls[ 0 ]->ldctl_iscritical = 1;
1292 switch ( li->idassert_mode ) {
1293 /* already in u:ID or dn:DN form */
1294 case LDAP_BACK_IDASSERT_OTHERID:
1295 case LDAP_BACK_IDASSERT_OTHERDN:
1296 ber_dupbv( &ctrls[ 0 ]->ldctl_value, &assertedID );
1299 /* needs the dn: prefix */
1301 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1302 ctrls[ 0 ]->ldctl_value.bv_val = ch_malloc( ctrls[ 0 ]->ldctl_value.bv_len + 1 );
1303 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1304 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1305 assertedID.bv_val, assertedID.bv_len + 1 );
1309 if ( op->o_ctrls ) {
1310 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1311 ctrls[ i + 1 ] = op->o_ctrls[ i ];
1314 ctrls[ i + 1 ] = NULL;
1317 if ( ctrls == NULL ) {
1318 ctrls = op->o_ctrls;
1327 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1329 LDAPControl **ctrls = *pctrls;
1331 /* we assume that the first control is the proxyAuthz
1332 * added by back-ldap, so it's the only one we explicitly
1334 if ( ctrls && ctrls != op->o_ctrls ) {
1335 assert( ctrls[ 0 ] != NULL );
1337 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1338 free( ctrls[ 0 ]->ldctl_value.bv_val );