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 switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
109 case LDAP_PVT_THREAD_EBUSY:
111 ldap_pvt_thread_yield();
115 if ( lc->lc_refcnt > 1 ) {
116 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
117 ldap_pvt_thread_yield();
123 assert( lc->lc_refcnt == 1 );
124 lc = avl_delete( &li->conntree, (caddr_t)lc,
125 ldap_back_conn_cmp );
126 assert( lc != NULL );
128 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
129 lerr = avl_insert( &li->conntree, (caddr_t)lc,
130 ldap_back_conn_cmp, ldap_back_conn_dup );
131 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
133 /* we can do this because lc_refcnt == 1 */
134 ldap_back_conn_free( lc );
140 ldap_back_release_conn( op, rs, lc );
149 * compares two struct ldapconn based on the value of the conn pointer;
153 ldap_back_conn_cmp( const void *c1, const void *c2 )
155 const struct ldapconn *lc1 = (const struct ldapconn *)c1;
156 const struct ldapconn *lc2 = (const struct ldapconn *)c2;
159 /* If local DNs don't match, it is definitely not a match */
160 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
165 /* For shared sessions, conn is NULL. Only explicitly
166 * bound sessions will have non-NULL conn.
168 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
174 * returns -1 in case a duplicate struct ldapconn has been inserted;
178 ldap_back_conn_dup( void *c1, void *c2 )
180 struct ldapconn *lc1 = (struct ldapconn *)c1;
181 struct ldapconn *lc2 = (struct ldapconn *)c2;
183 /* Cannot have more than one shared session with same DN */
184 if ( dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) &&
185 lc1->lc_conn == lc2->lc_conn )
193 #if PRINT_CONNTREE > 0
195 ravl_print( Avlnode *root, int depth )
204 ravl_print( root->avl_right, depth+1 );
206 for ( i = 0; i < depth; i++ ) {
211 printf( "lc(%lx) local(%s) conn(%lx) %d\n",
212 lc, lc->lc_local_ndn.bv_val, lc->lc_conn, root->avl_bf );
214 ravl_print( root->avl_left, depth+1 );
218 myprint( Avlnode *root )
220 printf( "********\n" );
223 printf( "\tNULL\n" );
226 ravl_print( root, 0 );
229 printf( "********\n" );
231 #endif /* PRINT_CONNTREE */
234 ldap_back_freeconn( Operation *op, struct ldapconn *lc )
236 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
238 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
240 assert( lc->lc_refcnt > 0 );
241 if ( --lc->lc_refcnt == 0 ) {
242 lc = avl_delete( &li->conntree, (caddr_t)lc,
243 ldap_back_conn_cmp );
244 assert( lc != NULL );
246 ldap_back_conn_free( (void *)lc );
249 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
264 int rc = LDAP_SUCCESS;
267 /* this is ridicolous... */
270 /* start TLS ("tls-[try-]{start,propagate}" statements) */
271 if ( ( LDAP_BACK_USE_TLS( &li ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS( &li ) ) )
272 && !ldap_is_ldaps_url( url ) )
274 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
276 * use asynchronous StartTLS
277 * in case, chase referral (not implemented yet)
281 if ( protocol == 0 ) {
282 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
286 if ( protocol < LDAP_VERSION3 ) {
287 protocol = LDAP_VERSION3;
288 /* Set LDAP version */
289 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
290 (const void *)&protocol );
293 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
294 if ( rc == LDAP_SUCCESS ) {
295 LDAPMessage *res = NULL;
297 struct timeval tv = { 0, 0 };
300 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
304 } else if ( rc == 0 ) {
313 } else if ( rc == LDAP_RES_EXTENDED ) {
314 struct berval *data = NULL;
316 rc = ldap_parse_extended_result( ld, res,
318 if ( rc == LDAP_SUCCESS ) {
319 rc = ldap_result2error( ld, res, 1 );
322 /* FIXME: in case a referral
323 * is returned, should we try
324 * using it instead of the
326 if ( rc == LDAP_SUCCESS ) {
327 rc = ldap_install_tls( ld );
329 } else if ( rc == LDAP_REFERRAL ) {
331 *text = "unwilling to chase referral returned by Start TLS exop";
335 if ( data->bv_val ) {
336 ber_memfree( data->bv_val );
350 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
352 * use synchronous StartTLS
354 rc = ldap_start_tls_s( ld, NULL, NULL );
355 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
357 /* if StartTLS is requested, only attempt it if the URL
358 * is not "ldaps://"; this may occur not only in case
359 * of misconfiguration, but also when used in the chain
360 * overlay, where the "uri" can be parsed out of a referral */
366 case LDAP_SERVER_DOWN:
370 if ( LDAP_BACK_TLS_CRITICAL( &li ) ) {
371 *text = "could not start TLS";
375 /* in case Start TLS is not critical */
388 #endif /* HAVE_TLS */
391 ldap_back_prepare_conn( struct ldapconn **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
393 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
394 int vers = op->o_protocol;
396 int is_tls = op->o_conn->c_is_tls;
398 assert( lcp != NULL );
400 rs->sr_err = ldap_initialize( &ld, li->url );
401 if ( rs->sr_err != LDAP_SUCCESS ) {
405 /* Set LDAP version. This will always succeed: If the client
406 * bound with a particular version, then so can we.
408 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&vers );
410 /* automatically chase referrals ("[dont-]chase-referrals" statement) */
411 if ( LDAP_BACK_CHASE_REFERRALS( li ) ) {
412 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
416 rs->sr_err = ldap_back_start_tls( ld,
417 op->o_protocol, &is_tls,
418 li->url, li->flags, &rs->sr_text );
419 if ( rs->sr_err != LDAP_SUCCESS ) {
420 ldap_unbind_ext( ld, NULL, NULL );
423 #endif /* HAVE_TLS */
425 if ( *lcp == NULL ) {
426 *lcp = (struct ldapconn *)ch_calloc( 1, sizeof( struct ldapconn ) );
427 (*lcp)->lc_flags= li->flags;
430 (*lcp)->lc_refcnt = 1;
431 (*lcp)->lc_is_tls = is_tls;
434 if ( rs->sr_err != LDAP_SUCCESS ) {
435 rs->sr_err = slap_map_api2result( rs );
436 if ( sendok & LDAP_BACK_SENDERR ) {
437 if ( rs->sr_text == NULL ) {
438 rs->sr_text = "ldap_initialize() failed";
440 send_ldap_result( op, rs );
449 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
451 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
456 /* Searches for a ldapconn in the avl tree */
458 /* Explicit binds must not be shared */
459 if ( op->o_tag == LDAP_REQ_BIND
461 && op->o_conn->c_authz_backend
462 && op->o_bd->be_private == op->o_conn->c_authz_backend->be_private ) )
464 lc_curr.lc_conn = op->o_conn;
467 lc_curr.lc_conn = op->o_conn->c_is_tls ?
468 LDAP_BACK_PRIV_CONN_TLS : LDAP_BACK_PRIV_CONN;
471 /* Internal searches are privileged and shared. So is root. */
472 /* FIXME: there seem to be concurrency issues */
473 if ( op->o_do_not_cache || be_isroot( op ) ) {
474 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
475 lc_curr.lc_conn = op->o_conn->c_is_tls ?
476 LDAP_BACK_PRIV_CONN_TLS : LDAP_BACK_PRIV_CONN;
477 lc_curr.lc_ispriv = 1;
480 lc_curr.lc_local_ndn = op->o_ndn;
483 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
485 lc = (struct ldapconn *)avl_find( li->conntree,
486 (caddr_t)&lc_curr, ldap_back_conn_cmp );
488 refcnt = ++lc->lc_refcnt;
490 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
492 /* Looks like we didn't get a bind. Open a new session... */
494 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
498 lc->lc_conn = lc_curr.lc_conn;
499 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
501 if ( lc_curr.lc_ispriv ) {
502 ber_dupbv( &lc->lc_cred, &li->acl_passwd );
503 ber_dupbv( &lc->lc_bound_ndn, &li->acl_authcDN );
504 lc->lc_ispriv = lc_curr.lc_ispriv;
507 BER_BVZERO( &lc->lc_cred );
508 BER_BVZERO( &lc->lc_bound_ndn );
509 if ( op->o_conn && !BER_BVISEMPTY( &op->o_ndn )
510 && op->o_bd->be_private == op->o_conn->c_authz_backend->be_private )
512 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
516 /* if start TLS failed but it was not mandatory,
517 * check if the non-TLS connection was already
518 * in cache; in case, destroy the newly created
519 * connection and use the existing one */
520 if ( lc->lc_conn == LDAP_BACK_PRIV_CONN_TLS
521 && !ldap_tls_inplace( lc->lc_ld ) )
523 struct ldapconn *tmplc;
525 lc_curr.lc_conn = LDAP_BACK_PRIV_CONN;
526 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
527 tmplc = (struct ldapconn *)avl_find( li->conntree,
528 (caddr_t)&lc_curr, ldap_back_conn_cmp );
529 if ( tmplc != NULL ) {
530 refcnt = ++tmplc->lc_refcnt;
531 ldap_back_conn_free( lc );
534 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
536 if ( tmplc != NULL ) {
543 /* Inserts the newly created ldapconn in the avl tree */
544 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
546 assert( lc->lc_refcnt == 1 );
547 rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
548 ldap_back_conn_cmp, ldap_back_conn_dup );
550 #if PRINT_CONNTREE > 0
551 myprint( li->conntree );
552 #endif /* PRINT_CONNTREE */
554 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
556 Debug( LDAP_DEBUG_TRACE,
557 "=>ldap_back_getconn: conn %p inserted (refcnt=%u)\n",
558 (void *)lc, refcnt, 0 );
560 /* Err could be -1 in case a duplicate ldapconn is inserted */
561 if ( rs->sr_err != 0 ) {
562 ldap_back_conn_free( lc );
563 rs->sr_err = LDAP_OTHER;
564 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
565 send_ldap_error( op, rs, LDAP_OTHER,
566 "internal server error" );
572 Debug( LDAP_DEBUG_TRACE,
573 "=>ldap_back_getconn: conn %p fetched (refcnt=%u)\n",
574 (void *)lc, refcnt, 0 );
582 ldap_back_release_conn(
585 struct ldapconn *lc )
587 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
589 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
590 assert( lc->lc_refcnt > 0 );
592 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
598 * Note: as the check for the value of lc->lc_bound was already here, I removed
599 * it from all the callers, and I made the function return the flag, so
600 * it can be used to simplify the check.
602 * Note: dolock indicates whether li->conn_mutex must be locked or not
605 ldap_back_dobind_int(
609 ldap_back_send_t sendok,
616 assert( retries >= 0 );
618 if ( !lc->lc_bound ) {
619 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
622 * FIXME: we need to let clients use proxyAuthz
623 * otherwise we cannot do symmetric pools of servers;
624 * we have to live with the fact that a user can
625 * authorize itself as any ID that is allowed
626 * by the authzTo directive of the "proxyauthzdn".
629 * NOTE: current Proxy Authorization specification
630 * and implementation do not allow proxy authorization
631 * control to be provided with Bind requests
634 * if no bind took place yet, but the connection is bound
635 * and the "idassert-authcDN" (or other ID) is set,
636 * then bind as the asserting identity and explicitly
637 * add the proxyAuthz control to every operation with the
638 * dn bound to the connection as control value.
639 * This is done also if this is the authrizing backend,
640 * but the "override" flag is given to idassert.
641 * It allows to use SASL bind and yet proxyAuthz users
643 if ( op->o_conn != NULL &&
644 !op->o_do_not_cache &&
645 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
646 ( li->idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
648 (void)ldap_back_proxy_authz_bind( lc, op, rs );
652 #ifdef HAVE_CYRUS_SASL
653 if ( lc->lc_ispriv && li->acl_authmethod == LDAP_AUTH_SASL ) {
654 void *defaults = NULL;
656 #if 1 /* will deal with this later... */
657 if ( li->acl_secprops != NULL ) {
658 rc = ldap_set_option( lc->lc_ld,
659 LDAP_OPT_X_SASL_SECPROPS, li->acl_secprops);
661 if( rc != LDAP_OPT_SUCCESS ) {
662 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
663 "(%s,SECPROPS,\"%s\") failed!\n",
664 li->url, li->acl_secprops, 0 );
670 defaults = lutil_sasl_defaults( lc->lc_ld,
671 li->acl_sasl_mech.bv_val,
672 li->acl_sasl_realm.bv_val,
673 li->acl_authcID.bv_val,
674 li->acl_passwd.bv_val,
677 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
678 li->acl_authcDN.bv_val,
679 li->acl_sasl_mech.bv_val, NULL, NULL,
680 LDAP_SASL_QUIET, lutil_sasl_interact,
683 lutil_sasl_freedefs( defaults );
685 rs->sr_err = slap_map_api2result( rs );
686 if ( rs->sr_err != LDAP_SUCCESS ) {
688 send_ldap_result( op, rs );
695 #endif /* HAVE_CYRUS_SASL */
698 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
699 lc->lc_bound_ndn.bv_val,
700 LDAP_SASL_SIMPLE, &lc->lc_cred,
701 NULL, NULL, &msgid );
703 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
706 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
709 assert( lc->lc_refcnt > 0 );
710 if ( lc->lc_refcnt == 1 ) {
711 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
714 /* lc here must be the regular lc, reset and ready for init */
715 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
718 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
720 if ( rs->sr_err == LDAP_SUCCESS ) {
726 ldap_back_freeconn( op, lc );
727 rs->sr_err = slap_map_api2result( rs );
732 rc = ldap_back_op_result( lc, op, rs, msgid, sendok );
733 if ( rc == LDAP_SUCCESS ) {
744 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
746 return ldap_back_dobind_int( lc, op, rs, sendok, 1, 1 );
752 * This is a callback used for chasing referrals using the same
753 * credentials as the original user on this session.
756 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
757 ber_int_t msgid, void *params )
759 struct ldapconn *lc = (struct ldapconn *)params;
762 /* ... otherwise we couldn't get here */
763 assert( lc != NULL );
765 if ( !ldap_tls_inplace( ld ) ) {
766 int is_tls = lc->lc_is_tls,
768 const char *text = NULL;
770 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags, &text );
771 if ( rc != LDAP_SUCCESS ) {
775 #endif /* HAVE_TLS */
777 /* FIXME: add checks on the URL/identity? */
779 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
780 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
789 ldap_back_send_t sendok )
792 LDAPMessage *res = NULL;
795 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
798 rs->sr_matched = NULL;
800 /* if the error recorded in the reply corresponds
801 * to a successful state, get the error from the
802 * remote server response */
803 if ( ERR_OK( rs->sr_err ) ) {
805 struct timeval tv = { 0, 0 };
808 /* if result parsing fails, note the failure reason */
809 switch ( ldap_result( lc->lc_ld, msgid, 1, &tv, &res ) ) {
812 tv.tv_usec = 100000; /* 0.1 s */
813 ldap_pvt_thread_yield();
817 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
822 /* otherwise get the result; if it is not
823 * LDAP_SUCCESS, record it in the reply
824 * structure (this includes
825 * LDAP_COMPARE_{TRUE|FALSE}) */
827 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
828 &match, &text, NULL, NULL, 1 );
830 if ( rc != LDAP_SUCCESS ) {
836 /* if the error in the reply structure is not
837 * LDAP_SUCCESS, try to map it from client
839 if ( !ERR_OK( rs->sr_err ) ) {
840 rs->sr_err = slap_map_api2result( rs );
842 /* internal ops ( op->o_conn == NULL )
843 * must not reply to client */
844 if ( op->o_conn && !op->o_do_not_cache && match ) {
846 /* record the (massaged) matched
847 * DN into the reply structure */
848 rs->sr_matched = match;
852 ( ( sendok & LDAP_BACK_SENDOK )
853 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
855 send_ldap_result( op, rs );
858 if ( rs->sr_matched != match ) {
859 free( (char *)rs->sr_matched );
861 rs->sr_matched = NULL;
862 ldap_memfree( match );
865 ldap_memfree( text );
868 return( ERR_OK( rs->sr_err ) ? 0 : -1 );
871 /* return true if bound, false if failed */
873 ldap_back_retry( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
876 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
878 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
880 if ( lc->lc_refcnt == 1 ) {
881 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
885 /* lc here must be the regular lc, reset and ready for init */
886 rc = ldap_back_prepare_conn( &lc, op, rs, sendok );
887 if ( rc == LDAP_SUCCESS ) {
888 rc = ldap_back_dobind_int( lc, op, rs, sendok, 0, 0 );
892 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
898 ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs )
900 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
901 struct berval binddn = slap_empty_bv;
902 struct berval bindcred = slap_empty_bv;
908 * FIXME: we need to let clients use proxyAuthz
909 * otherwise we cannot do symmetric pools of servers;
910 * we have to live with the fact that a user can
911 * authorize itself as any ID that is allowed
912 * by the authzTo directive of the "proxyauthzdn".
915 * NOTE: current Proxy Authorization specification
916 * and implementation do not allow proxy authorization
917 * control to be provided with Bind requests
920 * if no bind took place yet, but the connection is bound
921 * and the "proxyauthzdn" is set, then bind as
922 * "proxyauthzdn" and explicitly add the proxyAuthz
923 * control to every operation with the dn bound
924 * to the connection as control value.
927 /* bind as proxyauthzdn only if no idassert mode
928 * is requested, or if the client's identity
930 switch ( li->idassert_mode ) {
931 case LDAP_BACK_IDASSERT_LEGACY:
932 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) && !BER_BVISEMPTY( &op->o_conn->c_ndn ) ) {
933 if ( !BER_BVISNULL( &li->idassert_authcDN ) && !BER_BVISEMPTY( &li->idassert_authcDN ) )
935 binddn = li->idassert_authcDN;
936 bindcred = li->idassert_passwd;
943 /* NOTE: rootdn can always idassert */
944 if ( li->idassert_authz && !be_isroot( op ) ) {
945 struct berval authcDN;
947 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
948 authcDN = slap_empty_bv;
951 authcDN = op->o_conn->c_ndn;
953 rs->sr_err = slap_sasl_matches( op, li->idassert_authz,
954 &authcDN, &authcDN );
955 if ( rs->sr_err != LDAP_SUCCESS ) {
956 if ( li->idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
957 send_ldap_result( op, rs );
961 rs->sr_err = LDAP_SUCCESS;
962 binddn = slap_empty_bv;
963 bindcred = slap_empty_bv;
971 binddn = li->idassert_authcDN;
972 bindcred = li->idassert_passwd;
977 if ( dobind && li->idassert_authmethod == LDAP_AUTH_SASL ) {
978 #ifdef HAVE_CYRUS_SASL
979 void *defaults = NULL;
980 struct berval authzID = BER_BVNULL;
983 /* if SASL supports native authz, prepare for it */
984 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
985 ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
987 switch ( li->idassert_mode ) {
988 case LDAP_BACK_IDASSERT_OTHERID:
989 case LDAP_BACK_IDASSERT_OTHERDN:
990 authzID = li->idassert_authzID;
993 case LDAP_BACK_IDASSERT_ANONYMOUS:
994 BER_BVSTR( &authzID, "dn:" );
997 case LDAP_BACK_IDASSERT_SELF:
998 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
999 /* connection is not authc'd, so don't idassert */
1000 BER_BVSTR( &authzID, "dn:" );
1003 authzID.bv_len = STRLENOF( "dn:" ) + op->o_conn->c_ndn.bv_len;
1004 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1005 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1006 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1007 op->o_conn->c_ndn.bv_val, op->o_conn->c_ndn.bv_len + 1 );
1016 #if 0 /* will deal with this later... */
1017 if ( sasl_secprops != NULL ) {
1018 rs->sr_err = ldap_set_option( lc->lc_ld, LDAP_OPT_X_SASL_SECPROPS,
1019 (void *) sasl_secprops );
1021 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1022 send_ldap_result( op, rs );
1029 defaults = lutil_sasl_defaults( lc->lc_ld,
1030 li->idassert_sasl_mech.bv_val,
1031 li->idassert_sasl_realm.bv_val,
1032 li->idassert_authcID.bv_val,
1033 li->idassert_passwd.bv_val,
1036 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1037 li->idassert_sasl_mech.bv_val, NULL, NULL,
1038 LDAP_SASL_QUIET, lutil_sasl_interact,
1041 lutil_sasl_freedefs( defaults );
1043 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1046 rs->sr_err = slap_map_api2result( rs );
1047 if ( rs->sr_err != LDAP_SUCCESS ) {
1049 send_ldap_result( op, rs );
1055 #endif /* HAVE_CYRUS_SASL */
1058 switch ( li->idassert_authmethod ) {
1059 case LDAP_AUTH_SIMPLE:
1060 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1061 binddn.bv_val, LDAP_SASL_SIMPLE,
1062 &bindcred, NULL, NULL, &msgid );
1065 case LDAP_AUTH_NONE:
1072 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1073 send_ldap_result( op, rs );
1077 rc = ldap_back_op_result( lc, op, rs, msgid, LDAP_BACK_SENDERR );
1078 if ( rc == LDAP_SUCCESS ) {
1082 return lc->lc_bound;
1086 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1087 * to existing server-side controls if required; if not,
1088 * the existing server-side controls are placed in *pctrls.
1089 * The caller, after using the controls in client API
1090 * operations, if ( *pctrls != op->o_ctrls ), should
1091 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1092 * The function returns success if the control could
1093 * be added if required, or if it did nothing; in the future,
1094 * it might return some error if it failed.
1096 * if no bind took place yet, but the connection is bound
1097 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1098 * and explicitly add proxyAuthz the control to every operation
1099 * with the dn bound to the connection as control value.
1101 * If no server-side controls are defined for the operation,
1102 * simply add the proxyAuthz control; otherwise, if the
1103 * proxyAuthz control is not already set, add it as
1106 * FIXME: is controls order significant for security?
1107 * ANSWER: controls ordering and interoperability
1108 * must be indicated by the specs of each control; if none
1109 * is specified, the order is irrelevant.
1112 ldap_back_proxy_authz_ctrl(
1113 struct ldapconn *lc,
1116 LDAPControl ***pctrls )
1118 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
1119 LDAPControl **ctrls = NULL;
1122 struct berval assertedID;
1126 rs->sr_err = LDAP_SUCCESS;
1128 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1129 * but if it is not set this test fails. We need a different
1130 * means to detect if idassert is enabled */
1131 if ( ( BER_BVISNULL( &li->idassert_authcID ) || BER_BVISEMPTY( &li->idassert_authcID ) )
1132 && ( BER_BVISNULL( &li->idassert_authcDN ) || BER_BVISEMPTY( &li->idassert_authcDN ) ) )
1137 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1141 if ( li->idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1142 if ( op->o_proxy_authz ) {
1144 * FIXME: we do not want to perform proxyAuthz
1145 * on behalf of the client, because this would
1146 * be performed with "proxyauthzdn" privileges.
1148 * This might actually be too strict, since
1149 * the "proxyauthzdn" authzTo, and each entry's
1150 * authzFrom attributes may be crafted
1151 * to avoid unwanted proxyAuthz to take place.
1154 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1155 rs->sr_text = "proxyAuthz not allowed within namingContext";
1160 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1164 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1168 if ( BER_BVISNULL( &li->idassert_authcDN ) ) {
1172 } else if ( li->idassert_authmethod == LDAP_AUTH_SASL ) {
1173 if ( ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1174 /* && ( !BER_BVISNULL( &op->o_conn->c_ndn ) || lc->lc_bound ) */ )
1176 /* already asserted in SASL via native authz */
1177 /* NOTE: the test on lc->lc_bound is used to trap
1178 * native authorization of anonymous users,
1179 * since in that case op->o_conn->c_ndn is NULL */
1183 } else if ( li->idassert_authz && !be_isroot( op ) ) {
1185 struct berval authcDN;
1187 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1188 authcDN = slap_empty_bv;
1190 authcDN = op->o_conn->c_ndn;
1192 rc = slap_sasl_matches( op, li->idassert_authz,
1193 &authcDN, & authcDN );
1194 if ( rc != LDAP_SUCCESS ) {
1195 if ( li->idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1197 /* op->o_conn->c_ndn is not authorized
1198 * to use idassert */
1205 if ( op->o_proxy_authz ) {
1208 * 1) ignore the already set proxyAuthz control
1209 * 2) leave it in place, and don't set ours
1211 * 4) reject the operation
1213 * option (4) is very drastic
1214 * option (3) will make the remote server reject
1215 * the operation, thus being equivalent to (4)
1216 * option (2) will likely break the idassert
1217 * assumptions, so we cannot accept it;
1218 * option (1) means that we are contradicting
1219 * the client's reques.
1221 * I think (4) is the only correct choice.
1223 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1224 rs->sr_text = "proxyAuthz not allowed within namingContext";
1227 if ( op->o_do_not_cache && op->o_is_auth_check ) {
1228 mode = LDAP_BACK_IDASSERT_NOASSERT;
1231 mode = li->idassert_mode;
1235 case LDAP_BACK_IDASSERT_LEGACY:
1236 case LDAP_BACK_IDASSERT_SELF:
1237 /* original behavior:
1238 * assert the client's identity */
1239 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1240 assertedID = slap_empty_bv;
1242 assertedID = op->o_conn->c_ndn;
1246 case LDAP_BACK_IDASSERT_ANONYMOUS:
1247 /* assert "anonymous" */
1248 assertedID = slap_empty_bv;
1251 case LDAP_BACK_IDASSERT_NOASSERT:
1252 /* don't assert; bind as proxyauthzdn */
1255 case LDAP_BACK_IDASSERT_OTHERID:
1256 case LDAP_BACK_IDASSERT_OTHERDN:
1257 /* assert idassert DN */
1258 assertedID = li->idassert_authzID;
1265 if ( BER_BVISNULL( &assertedID ) ) {
1266 assertedID = slap_empty_bv;
1269 if ( op->o_ctrls ) {
1270 for ( i = 0; op->o_ctrls[ i ]; i++ )
1271 /* just count ctrls */ ;
1274 ctrls = ch_malloc( sizeof( LDAPControl * ) * (i + 2) );
1275 ctrls[ 0 ] = ch_malloc( sizeof( LDAPControl ) );
1277 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1278 ctrls[ 0 ]->ldctl_iscritical = 1;
1280 switch ( li->idassert_mode ) {
1281 /* already in u:ID or dn:DN form */
1282 case LDAP_BACK_IDASSERT_OTHERID:
1283 case LDAP_BACK_IDASSERT_OTHERDN:
1284 ber_dupbv( &ctrls[ 0 ]->ldctl_value, &assertedID );
1287 /* needs the dn: prefix */
1289 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1290 ctrls[ 0 ]->ldctl_value.bv_val = ch_malloc( ctrls[ 0 ]->ldctl_value.bv_len + 1 );
1291 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1292 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1293 assertedID.bv_val, assertedID.bv_len + 1 );
1297 if ( op->o_ctrls ) {
1298 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1299 ctrls[ i + 1 ] = op->o_ctrls[ i ];
1302 ctrls[ i + 1 ] = NULL;
1305 if ( ctrls == NULL ) {
1306 ctrls = op->o_ctrls;
1315 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1317 LDAPControl **ctrls = *pctrls;
1319 /* we assume that the first control is the proxyAuthz
1320 * added by back-ldap, so it's the only one we explicitly
1322 if ( ctrls && ctrls != op->o_ctrls ) {
1323 assert( ctrls[ 0 ] != NULL );
1325 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1326 free( ctrls[ 0 ]->ldctl_value.bv_val );