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 */
387 #endif /* HAVE_TLS */
390 ldap_back_prepare_conn( struct ldapconn **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
392 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
393 int vers = op->o_protocol;
396 int is_tls = op->o_conn->c_is_tls;
397 #endif /* HAVE_TLS */
399 assert( lcp != NULL );
401 rs->sr_err = ldap_initialize( &ld, li->url );
402 if ( rs->sr_err != LDAP_SUCCESS ) {
406 /* Set LDAP version. This will always succeed: If the client
407 * bound with a particular version, then so can we.
409 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&vers );
411 /* automatically chase referrals ("[dont-]chase-referrals" statement) */
412 if ( LDAP_BACK_CHASE_REFERRALS( li ) ) {
413 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
417 rs->sr_err = ldap_back_start_tls( ld,
418 op->o_protocol, &is_tls,
419 li->url, li->flags, &rs->sr_text );
420 if ( rs->sr_err != LDAP_SUCCESS ) {
421 ldap_unbind_ext( ld, NULL, NULL );
424 #endif /* HAVE_TLS */
426 if ( *lcp == NULL ) {
427 *lcp = (struct ldapconn *)ch_calloc( 1, sizeof( struct ldapconn ) );
428 (*lcp)->lc_flags= li->flags;
431 (*lcp)->lc_refcnt = 1;
433 (*lcp)->lc_is_tls = is_tls;
434 #endif /* HAVE_TLS */
437 if ( rs->sr_err != LDAP_SUCCESS ) {
438 rs->sr_err = slap_map_api2result( rs );
439 if ( sendok & LDAP_BACK_SENDERR ) {
440 if ( rs->sr_text == NULL ) {
441 rs->sr_text = "ldap_initialize() failed";
443 send_ldap_result( op, rs );
452 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
454 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
459 /* Searches for a ldapconn in the avl tree */
461 /* Explicit binds must not be shared */
462 if ( op->o_tag == LDAP_REQ_BIND
464 && op->o_conn->c_authz_backend
465 && op->o_bd->be_private == op->o_conn->c_authz_backend->be_private ) )
467 lc_curr.lc_conn = op->o_conn;
471 if ( op->o_conn->c_is_tls ) {
472 lc_curr.lc_conn = LDAP_BACK_PRIV_CONN_TLS;
474 #endif /* HAVE_TLS */
476 lc_curr.lc_conn = LDAP_BACK_PRIV_CONN;
480 /* Internal searches are privileged and shared. So is root. */
481 /* FIXME: there seem to be concurrency issues */
482 if ( op->o_do_not_cache || be_isroot( op ) ) {
483 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
485 if ( op->o_conn->c_is_tls ) {
486 lc_curr.lc_conn = LDAP_BACK_PRIV_CONN_TLS;
488 #endif /* HAVE_TLS */
490 lc_curr.lc_conn = LDAP_BACK_PRIV_CONN;
492 lc_curr.lc_ispriv = 1;
495 lc_curr.lc_local_ndn = op->o_ndn;
498 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
500 lc = (struct ldapconn *)avl_find( li->conntree,
501 (caddr_t)&lc_curr, ldap_back_conn_cmp );
503 refcnt = ++lc->lc_refcnt;
505 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
507 /* Looks like we didn't get a bind. Open a new session... */
509 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
513 lc->lc_conn = lc_curr.lc_conn;
514 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
516 if ( lc_curr.lc_ispriv ) {
517 ber_dupbv( &lc->lc_cred, &li->acl_passwd );
518 ber_dupbv( &lc->lc_bound_ndn, &li->acl_authcDN );
519 lc->lc_ispriv = lc_curr.lc_ispriv;
522 BER_BVZERO( &lc->lc_cred );
523 BER_BVZERO( &lc->lc_bound_ndn );
524 if ( op->o_conn && !BER_BVISEMPTY( &op->o_ndn )
525 && op->o_bd->be_private == op->o_conn->c_authz_backend->be_private )
527 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
532 /* if start TLS failed but it was not mandatory,
533 * check if the non-TLS connection was already
534 * in cache; in case, destroy the newly created
535 * connection and use the existing one */
536 if ( lc->lc_conn == LDAP_BACK_PRIV_CONN_TLS
537 && !ldap_tls_inplace( lc->lc_ld ) )
539 struct ldapconn *tmplc;
541 lc_curr.lc_conn = LDAP_BACK_PRIV_CONN;
542 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
543 tmplc = (struct ldapconn *)avl_find( li->conntree,
544 (caddr_t)&lc_curr, ldap_back_conn_cmp );
545 if ( tmplc != NULL ) {
546 refcnt = ++tmplc->lc_refcnt;
547 ldap_back_conn_free( lc );
550 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
552 if ( tmplc != NULL ) {
556 #endif /* HAVE_TLS */
560 /* Inserts the newly created ldapconn in the avl tree */
561 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
563 assert( lc->lc_refcnt == 1 );
564 rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
565 ldap_back_conn_cmp, ldap_back_conn_dup );
567 #if PRINT_CONNTREE > 0
568 myprint( li->conntree );
569 #endif /* PRINT_CONNTREE */
571 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
573 Debug( LDAP_DEBUG_TRACE,
574 "=>ldap_back_getconn: conn %p inserted (refcnt=%u)\n",
575 (void *)lc, refcnt, 0 );
577 /* Err could be -1 in case a duplicate ldapconn is inserted */
578 if ( rs->sr_err != 0 ) {
579 ldap_back_conn_free( lc );
580 rs->sr_err = LDAP_OTHER;
581 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
582 send_ldap_error( op, rs, LDAP_OTHER,
583 "internal server error" );
589 Debug( LDAP_DEBUG_TRACE,
590 "=>ldap_back_getconn: conn %p fetched (refcnt=%u)\n",
591 (void *)lc, refcnt, 0 );
599 ldap_back_release_conn(
602 struct ldapconn *lc )
604 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
606 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
607 assert( lc->lc_refcnt > 0 );
609 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
615 * Note: as the check for the value of lc->lc_bound was already here, I removed
616 * it from all the callers, and I made the function return the flag, so
617 * it can be used to simplify the check.
619 * Note: dolock indicates whether li->conn_mutex must be locked or not
622 ldap_back_dobind_int(
626 ldap_back_send_t sendok,
633 assert( retries >= 0 );
635 if ( !lc->lc_bound ) {
636 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
639 * FIXME: we need to let clients use proxyAuthz
640 * otherwise we cannot do symmetric pools of servers;
641 * we have to live with the fact that a user can
642 * authorize itself as any ID that is allowed
643 * by the authzTo directive of the "proxyauthzdn".
646 * NOTE: current Proxy Authorization specification
647 * and implementation do not allow proxy authorization
648 * control to be provided with Bind requests
651 * if no bind took place yet, but the connection is bound
652 * and the "idassert-authcDN" (or other ID) is set,
653 * then bind as the asserting identity and explicitly
654 * add the proxyAuthz control to every operation with the
655 * dn bound to the connection as control value.
656 * This is done also if this is the authrizing backend,
657 * but the "override" flag is given to idassert.
658 * It allows to use SASL bind and yet proxyAuthz users
660 if ( op->o_conn != NULL &&
661 !op->o_do_not_cache &&
662 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
663 ( li->idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
665 (void)ldap_back_proxy_authz_bind( lc, op, rs );
669 #ifdef HAVE_CYRUS_SASL
670 if ( lc->lc_ispriv && li->acl_authmethod == LDAP_AUTH_SASL ) {
671 void *defaults = NULL;
673 #if 1 /* will deal with this later... */
674 if ( li->acl_secprops != NULL ) {
675 rc = ldap_set_option( lc->lc_ld,
676 LDAP_OPT_X_SASL_SECPROPS, li->acl_secprops);
678 if( rc != LDAP_OPT_SUCCESS ) {
679 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
680 "(%s,SECPROPS,\"%s\") failed!\n",
681 li->url, li->acl_secprops, 0 );
687 defaults = lutil_sasl_defaults( lc->lc_ld,
688 li->acl_sasl_mech.bv_val,
689 li->acl_sasl_realm.bv_val,
690 li->acl_authcID.bv_val,
691 li->acl_passwd.bv_val,
694 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
695 li->acl_authcDN.bv_val,
696 li->acl_sasl_mech.bv_val, NULL, NULL,
697 LDAP_SASL_QUIET, lutil_sasl_interact,
700 lutil_sasl_freedefs( defaults );
702 rs->sr_err = slap_map_api2result( rs );
703 if ( rs->sr_err != LDAP_SUCCESS ) {
705 send_ldap_result( op, rs );
712 #endif /* HAVE_CYRUS_SASL */
715 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
716 lc->lc_bound_ndn.bv_val,
717 LDAP_SASL_SIMPLE, &lc->lc_cred,
718 NULL, NULL, &msgid );
720 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
723 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
726 assert( lc->lc_refcnt > 0 );
727 if ( lc->lc_refcnt == 1 ) {
728 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
731 /* lc here must be the regular lc, reset and ready for init */
732 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
735 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
737 if ( rs->sr_err == LDAP_SUCCESS ) {
743 ldap_back_freeconn( op, lc );
744 rs->sr_err = slap_map_api2result( rs );
749 rc = ldap_back_op_result( lc, op, rs, msgid, sendok );
750 if ( rc == LDAP_SUCCESS ) {
761 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
763 return ldap_back_dobind_int( lc, op, rs, sendok, 1, 1 );
769 * This is a callback used for chasing referrals using the same
770 * credentials as the original user on this session.
773 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
774 ber_int_t msgid, void *params )
776 struct ldapconn *lc = (struct ldapconn *)params;
779 /* ... otherwise we couldn't get here */
780 assert( lc != NULL );
782 if ( !ldap_tls_inplace( ld ) ) {
783 int is_tls = lc->lc_is_tls,
785 const char *text = NULL;
787 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags, &text );
788 if ( rc != LDAP_SUCCESS ) {
792 #endif /* HAVE_TLS */
794 /* FIXME: add checks on the URL/identity? */
796 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
797 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
806 ldap_back_send_t sendok )
809 LDAPMessage *res = NULL;
812 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
815 rs->sr_matched = NULL;
817 /* if the error recorded in the reply corresponds
818 * to a successful state, get the error from the
819 * remote server response */
820 if ( ERR_OK( rs->sr_err ) ) {
822 struct timeval tv = { 0, 0 };
825 /* if result parsing fails, note the failure reason */
826 switch ( ldap_result( lc->lc_ld, msgid, 1, &tv, &res ) ) {
829 tv.tv_usec = 100000; /* 0.1 s */
830 ldap_pvt_thread_yield();
834 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
839 /* otherwise get the result; if it is not
840 * LDAP_SUCCESS, record it in the reply
841 * structure (this includes
842 * LDAP_COMPARE_{TRUE|FALSE}) */
844 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
845 &match, &text, NULL, NULL, 1 );
847 if ( rc != LDAP_SUCCESS ) {
853 /* if the error in the reply structure is not
854 * LDAP_SUCCESS, try to map it from client
856 if ( !ERR_OK( rs->sr_err ) ) {
857 rs->sr_err = slap_map_api2result( rs );
859 /* internal ops ( op->o_conn == NULL )
860 * must not reply to client */
861 if ( op->o_conn && !op->o_do_not_cache && match ) {
863 /* record the (massaged) matched
864 * DN into the reply structure */
865 rs->sr_matched = match;
869 ( ( sendok & LDAP_BACK_SENDOK )
870 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
872 send_ldap_result( op, rs );
875 if ( rs->sr_matched != match ) {
876 free( (char *)rs->sr_matched );
878 rs->sr_matched = NULL;
879 ldap_memfree( match );
882 ldap_memfree( text );
885 return( ERR_OK( rs->sr_err ) ? 0 : -1 );
888 /* return true if bound, false if failed */
890 ldap_back_retry( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
893 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
895 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
897 if ( lc->lc_refcnt == 1 ) {
898 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
902 /* lc here must be the regular lc, reset and ready for init */
903 rc = ldap_back_prepare_conn( &lc, op, rs, sendok );
904 if ( rc == LDAP_SUCCESS ) {
905 rc = ldap_back_dobind_int( lc, op, rs, sendok, 0, 0 );
909 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
915 ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs )
917 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
918 struct berval binddn = slap_empty_bv;
919 struct berval bindcred = slap_empty_bv;
925 * FIXME: we need to let clients use proxyAuthz
926 * otherwise we cannot do symmetric pools of servers;
927 * we have to live with the fact that a user can
928 * authorize itself as any ID that is allowed
929 * by the authzTo directive of the "proxyauthzdn".
932 * NOTE: current Proxy Authorization specification
933 * and implementation do not allow proxy authorization
934 * control to be provided with Bind requests
937 * if no bind took place yet, but the connection is bound
938 * and the "proxyauthzdn" is set, then bind as
939 * "proxyauthzdn" and explicitly add the proxyAuthz
940 * control to every operation with the dn bound
941 * to the connection as control value.
944 /* bind as proxyauthzdn only if no idassert mode
945 * is requested, or if the client's identity
947 switch ( li->idassert_mode ) {
948 case LDAP_BACK_IDASSERT_LEGACY:
949 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) && !BER_BVISEMPTY( &op->o_conn->c_ndn ) ) {
950 if ( !BER_BVISNULL( &li->idassert_authcDN ) && !BER_BVISEMPTY( &li->idassert_authcDN ) )
952 binddn = li->idassert_authcDN;
953 bindcred = li->idassert_passwd;
960 /* NOTE: rootdn can always idassert */
961 if ( li->idassert_authz && !be_isroot( op ) ) {
962 struct berval authcDN;
964 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
965 authcDN = slap_empty_bv;
968 authcDN = op->o_conn->c_ndn;
970 rs->sr_err = slap_sasl_matches( op, li->idassert_authz,
971 &authcDN, &authcDN );
972 if ( rs->sr_err != LDAP_SUCCESS ) {
973 if ( li->idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
974 send_ldap_result( op, rs );
978 rs->sr_err = LDAP_SUCCESS;
979 binddn = slap_empty_bv;
980 bindcred = slap_empty_bv;
988 binddn = li->idassert_authcDN;
989 bindcred = li->idassert_passwd;
994 if ( dobind && li->idassert_authmethod == LDAP_AUTH_SASL ) {
995 #ifdef HAVE_CYRUS_SASL
996 void *defaults = NULL;
997 struct berval authzID = BER_BVNULL;
1000 /* if SASL supports native authz, prepare for it */
1001 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1002 ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1004 switch ( li->idassert_mode ) {
1005 case LDAP_BACK_IDASSERT_OTHERID:
1006 case LDAP_BACK_IDASSERT_OTHERDN:
1007 authzID = li->idassert_authzID;
1010 case LDAP_BACK_IDASSERT_ANONYMOUS:
1011 BER_BVSTR( &authzID, "dn:" );
1014 case LDAP_BACK_IDASSERT_SELF:
1015 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1016 /* connection is not authc'd, so don't idassert */
1017 BER_BVSTR( &authzID, "dn:" );
1020 authzID.bv_len = STRLENOF( "dn:" ) + op->o_conn->c_ndn.bv_len;
1021 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1022 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1023 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1024 op->o_conn->c_ndn.bv_val, op->o_conn->c_ndn.bv_len + 1 );
1033 #if 0 /* will deal with this later... */
1034 if ( sasl_secprops != NULL ) {
1035 rs->sr_err = ldap_set_option( lc->lc_ld, LDAP_OPT_X_SASL_SECPROPS,
1036 (void *) sasl_secprops );
1038 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1039 send_ldap_result( op, rs );
1046 defaults = lutil_sasl_defaults( lc->lc_ld,
1047 li->idassert_sasl_mech.bv_val,
1048 li->idassert_sasl_realm.bv_val,
1049 li->idassert_authcID.bv_val,
1050 li->idassert_passwd.bv_val,
1053 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1054 li->idassert_sasl_mech.bv_val, NULL, NULL,
1055 LDAP_SASL_QUIET, lutil_sasl_interact,
1058 lutil_sasl_freedefs( defaults );
1060 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1063 rs->sr_err = slap_map_api2result( rs );
1064 if ( rs->sr_err != LDAP_SUCCESS ) {
1066 send_ldap_result( op, rs );
1072 #endif /* HAVE_CYRUS_SASL */
1075 switch ( li->idassert_authmethod ) {
1076 case LDAP_AUTH_SIMPLE:
1077 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1078 binddn.bv_val, LDAP_SASL_SIMPLE,
1079 &bindcred, NULL, NULL, &msgid );
1082 case LDAP_AUTH_NONE:
1089 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1090 send_ldap_result( op, rs );
1094 rc = ldap_back_op_result( lc, op, rs, msgid, LDAP_BACK_SENDERR );
1095 if ( rc == LDAP_SUCCESS ) {
1099 return lc->lc_bound;
1103 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1104 * to existing server-side controls if required; if not,
1105 * the existing server-side controls are placed in *pctrls.
1106 * The caller, after using the controls in client API
1107 * operations, if ( *pctrls != op->o_ctrls ), should
1108 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1109 * The function returns success if the control could
1110 * be added if required, or if it did nothing; in the future,
1111 * it might return some error if it failed.
1113 * if no bind took place yet, but the connection is bound
1114 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1115 * and explicitly add proxyAuthz the control to every operation
1116 * with the dn bound to the connection as control value.
1118 * If no server-side controls are defined for the operation,
1119 * simply add the proxyAuthz control; otherwise, if the
1120 * proxyAuthz control is not already set, add it as
1123 * FIXME: is controls order significant for security?
1124 * ANSWER: controls ordering and interoperability
1125 * must be indicated by the specs of each control; if none
1126 * is specified, the order is irrelevant.
1129 ldap_back_proxy_authz_ctrl(
1130 struct ldapconn *lc,
1133 LDAPControl ***pctrls )
1135 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
1136 LDAPControl **ctrls = NULL;
1139 struct berval assertedID;
1143 rs->sr_err = LDAP_SUCCESS;
1145 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1146 * but if it is not set this test fails. We need a different
1147 * means to detect if idassert is enabled */
1148 if ( ( BER_BVISNULL( &li->idassert_authcID ) || BER_BVISEMPTY( &li->idassert_authcID ) )
1149 && ( BER_BVISNULL( &li->idassert_authcDN ) || BER_BVISEMPTY( &li->idassert_authcDN ) ) )
1154 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1158 if ( li->idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1159 if ( op->o_proxy_authz ) {
1161 * FIXME: we do not want to perform proxyAuthz
1162 * on behalf of the client, because this would
1163 * be performed with "proxyauthzdn" privileges.
1165 * This might actually be too strict, since
1166 * the "proxyauthzdn" authzTo, and each entry's
1167 * authzFrom attributes may be crafted
1168 * to avoid unwanted proxyAuthz to take place.
1171 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1172 rs->sr_text = "proxyAuthz not allowed within namingContext";
1177 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1181 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1185 if ( BER_BVISNULL( &li->idassert_authcDN ) ) {
1189 } else if ( li->idassert_authmethod == LDAP_AUTH_SASL ) {
1190 if ( ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1191 /* && ( !BER_BVISNULL( &op->o_conn->c_ndn ) || lc->lc_bound ) */ )
1193 /* already asserted in SASL via native authz */
1194 /* NOTE: the test on lc->lc_bound is used to trap
1195 * native authorization of anonymous users,
1196 * since in that case op->o_conn->c_ndn is NULL */
1200 } else if ( li->idassert_authz && !be_isroot( op ) ) {
1202 struct berval authcDN;
1204 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1205 authcDN = slap_empty_bv;
1207 authcDN = op->o_conn->c_ndn;
1209 rc = slap_sasl_matches( op, li->idassert_authz,
1210 &authcDN, & authcDN );
1211 if ( rc != LDAP_SUCCESS ) {
1212 if ( li->idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1214 /* op->o_conn->c_ndn is not authorized
1215 * to use idassert */
1222 if ( op->o_proxy_authz ) {
1225 * 1) ignore the already set proxyAuthz control
1226 * 2) leave it in place, and don't set ours
1228 * 4) reject the operation
1230 * option (4) is very drastic
1231 * option (3) will make the remote server reject
1232 * the operation, thus being equivalent to (4)
1233 * option (2) will likely break the idassert
1234 * assumptions, so we cannot accept it;
1235 * option (1) means that we are contradicting
1236 * the client's reques.
1238 * I think (4) is the only correct choice.
1240 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1241 rs->sr_text = "proxyAuthz not allowed within namingContext";
1244 if ( op->o_do_not_cache && op->o_is_auth_check ) {
1245 mode = LDAP_BACK_IDASSERT_NOASSERT;
1248 mode = li->idassert_mode;
1252 case LDAP_BACK_IDASSERT_LEGACY:
1253 case LDAP_BACK_IDASSERT_SELF:
1254 /* original behavior:
1255 * assert the client's identity */
1256 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1257 assertedID = slap_empty_bv;
1259 assertedID = op->o_conn->c_ndn;
1263 case LDAP_BACK_IDASSERT_ANONYMOUS:
1264 /* assert "anonymous" */
1265 assertedID = slap_empty_bv;
1268 case LDAP_BACK_IDASSERT_NOASSERT:
1269 /* don't assert; bind as proxyauthzdn */
1272 case LDAP_BACK_IDASSERT_OTHERID:
1273 case LDAP_BACK_IDASSERT_OTHERDN:
1274 /* assert idassert DN */
1275 assertedID = li->idassert_authzID;
1282 if ( BER_BVISNULL( &assertedID ) ) {
1283 assertedID = slap_empty_bv;
1286 if ( op->o_ctrls ) {
1287 for ( i = 0; op->o_ctrls[ i ]; i++ )
1288 /* just count ctrls */ ;
1291 ctrls = ch_malloc( sizeof( LDAPControl * ) * (i + 2) );
1292 ctrls[ 0 ] = ch_malloc( sizeof( LDAPControl ) );
1294 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1295 ctrls[ 0 ]->ldctl_iscritical = 1;
1297 switch ( li->idassert_mode ) {
1298 /* already in u:ID or dn:DN form */
1299 case LDAP_BACK_IDASSERT_OTHERID:
1300 case LDAP_BACK_IDASSERT_OTHERDN:
1301 ber_dupbv( &ctrls[ 0 ]->ldctl_value, &assertedID );
1304 /* needs the dn: prefix */
1306 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1307 ctrls[ 0 ]->ldctl_value.bv_val = ch_malloc( ctrls[ 0 ]->ldctl_value.bv_len + 1 );
1308 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1309 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1310 assertedID.bv_val, assertedID.bv_len + 1 );
1314 if ( op->o_ctrls ) {
1315 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1316 ctrls[ i + 1 ] = op->o_ctrls[ i ];
1319 ctrls[ i + 1 ] = NULL;
1322 if ( ctrls == NULL ) {
1323 ctrls = op->o_ctrls;
1332 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1334 LDAPControl **ctrls = *pctrls;
1336 /* we assume that the first control is the proxyAuthz
1337 * added by back-ldap, so it's the only one we explicitly
1339 if ( ctrls && ctrls != op->o_ctrls ) {
1340 assert( ctrls[ 0 ] != NULL );
1342 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1343 free( ctrls[ 0 ]->ldctl_value.bv_val );