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_default_rebind;
42 LDAP_REBIND_PROC *ldap_back_rebind_f = ldap_back_default_rebind;
45 ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs );
48 ldap_back_prepare_conn( struct ldapconn **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
51 ldap_back_bind( Operation *op, SlapReply *rs )
53 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
59 lc = ldap_back_getconn( op, rs, LDAP_BACK_SENDERR );
64 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
65 ch_free( lc->lc_bound_ndn.bv_val );
66 BER_BVZERO( &lc->lc_bound_ndn );
68 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
70 /* method is always LDAP_AUTH_SIMPLE if we got here */
71 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
73 &op->orb_cred, op->o_ctrls, NULL, &msgid );
74 rc = ldap_back_op_result( lc, op, rs, msgid, 0, LDAP_BACK_SENDERR );
76 if ( rc == LDAP_SUCCESS ) {
77 /* If defined, proxyAuthz will be used also when
78 * back-ldap is the authorizing backend; for this
79 * purpose, a successful bind is followed by a
80 * bind with the configured identity assertion */
81 /* NOTE: use with care */
82 if ( li->idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
83 ldap_back_proxy_authz_bind( lc, op, rs );
84 if ( !LDAP_BACK_CONN_ISBOUND( lc ) ) {
90 LDAP_BACK_CONN_ISBOUND_SET( lc );
91 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
93 if ( LDAP_BACK_SAVECRED( li ) ) {
94 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
95 memset( lc->lc_cred.bv_val, 0,
98 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
99 ldap_set_rebind_proc( lc->lc_ld, ldap_back_rebind_f, lc );
104 /* must re-insert if local DN changed as result of bind */
105 if ( LDAP_BACK_CONN_ISBOUND( lc )
106 && !dn_match( &op->o_req_ndn, &lc->lc_local_ndn ) )
110 /* wait for all other ops to release the connection */
112 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
113 if ( lc->lc_refcnt > 1 ) {
114 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
115 ldap_pvt_thread_yield();
119 assert( lc->lc_refcnt == 1 );
120 lc = avl_delete( &li->conntree, (caddr_t)lc,
121 ldap_back_conn_cmp );
122 assert( lc != NULL );
124 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
125 lerr = avl_insert( &li->conntree, (caddr_t)lc,
126 ldap_back_conn_cmp, ldap_back_conn_dup );
127 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
129 /* we can do this because lc_refcnt == 1 */
130 ldap_back_conn_free( lc );
136 ldap_back_release_conn( op, rs, lc );
145 * compares two struct ldapconn based on the value of the conn pointer;
149 ldap_back_conn_cmp( const void *c1, const void *c2 )
151 const struct ldapconn *lc1 = (const struct ldapconn *)c1;
152 const struct ldapconn *lc2 = (const struct ldapconn *)c2;
155 /* If local DNs don't match, it is definitely not a match */
156 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
161 /* For shared sessions, conn is NULL. Only explicitly
162 * bound sessions will have non-NULL conn.
164 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
170 * returns -1 in case a duplicate struct ldapconn has been inserted;
174 ldap_back_conn_dup( void *c1, void *c2 )
176 struct ldapconn *lc1 = (struct ldapconn *)c1;
177 struct ldapconn *lc2 = (struct ldapconn *)c2;
179 /* Cannot have more than one shared session with same DN */
180 if ( dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) &&
181 lc1->lc_conn == lc2->lc_conn )
189 #if PRINT_CONNTREE > 0
191 ravl_print( Avlnode *root, int depth )
200 ravl_print( root->avl_right, depth+1 );
202 for ( i = 0; i < depth; i++ ) {
207 printf( "lc(%lx) local(%s) conn(%lx) %d\n",
208 lc, lc->lc_local_ndn.bv_val, lc->lc_conn, root->avl_bf );
210 ravl_print( root->avl_left, depth+1 );
214 myprint( Avlnode *root )
216 printf( "********\n" );
219 printf( "\tNULL\n" );
222 ravl_print( root, 0 );
225 printf( "********\n" );
227 #endif /* PRINT_CONNTREE */
230 ldap_back_freeconn( Operation *op, struct ldapconn *lc )
232 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
234 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
236 assert( lc->lc_refcnt > 0 );
237 if ( --lc->lc_refcnt == 0 ) {
238 lc = avl_delete( &li->conntree, (caddr_t)lc,
239 ldap_back_conn_cmp );
240 assert( lc != NULL );
242 ldap_back_conn_free( (void *)lc );
245 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
261 int rc = LDAP_SUCCESS;
262 struct ldapinfo dummy;
264 /* this is ridiculous... */
267 /* start TLS ("tls-[try-]{start,propagate}" statements) */
268 if ( ( LDAP_BACK_USE_TLS( &dummy ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS( &dummy ) ) )
269 && !ldap_is_ldaps_url( url ) )
271 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
273 * use asynchronous StartTLS
274 * in case, chase referral (not implemented yet)
278 if ( protocol == 0 ) {
279 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
283 if ( protocol < LDAP_VERSION3 ) {
284 protocol = LDAP_VERSION3;
285 /* Set LDAP version */
286 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
287 (const void *)&protocol );
290 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
291 if ( rc == LDAP_SUCCESS ) {
292 LDAPMessage *res = NULL;
295 LDAP_BACK_TV_SET( &tv );
298 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
302 } else if ( rc == 0 ) {
303 if ( retries != LDAP_BACK_RETRY_NEVER ) {
304 ldap_pvt_thread_yield();
308 LDAP_BACK_TV_SET( &tv );
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 ) {
320 rc = ldap_parse_result( ld, res, &err,
321 NULL, NULL, NULL, NULL, 1 );
322 if ( rc == LDAP_SUCCESS ) {
327 /* FIXME: in case a referral
328 * is returned, should we try
329 * using it instead of the
331 if ( rc == LDAP_SUCCESS ) {
332 rc = ldap_install_tls( ld );
334 } else if ( rc == LDAP_REFERRAL ) {
336 *text = "unwilling to chase referral returned by Start TLS exop";
340 if ( data->bv_val ) {
341 ber_memfree( data->bv_val );
355 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
357 * use synchronous StartTLS
359 rc = ldap_start_tls_s( ld, NULL, NULL );
360 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
362 /* if StartTLS is requested, only attempt it if the URL
363 * is not "ldaps://"; this may occur not only in case
364 * of misconfiguration, but also when used in the chain
365 * overlay, where the "uri" can be parsed out of a referral */
371 case LDAP_SERVER_DOWN:
375 if ( LDAP_BACK_TLS_CRITICAL( &dummy ) ) {
376 *text = "could not start TLS";
380 /* in case Start TLS is not critical */
392 #endif /* HAVE_TLS */
395 ldap_back_prepare_conn( struct ldapconn **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
397 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
398 int vers = op->o_protocol;
401 int is_tls = op->o_conn->c_is_tls;
402 #endif /* HAVE_TLS */
404 assert( lcp != NULL );
406 rs->sr_err = ldap_initialize( &ld, li->url );
407 if ( rs->sr_err != LDAP_SUCCESS ) {
411 /* Set LDAP version. This will always succeed: If the client
412 * bound with a particular version, then so can we.
414 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&vers );
416 /* automatically chase referrals ("[dont-]chase-referrals" statement) */
417 if ( LDAP_BACK_CHASE_REFERRALS( li ) ) {
418 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
422 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
423 li->url, li->flags, li->nretries, &rs->sr_text );
424 if ( rs->sr_err != LDAP_SUCCESS ) {
425 ldap_unbind_ext( ld, NULL, NULL );
428 #endif /* HAVE_TLS */
430 if ( *lcp == NULL ) {
431 *lcp = (struct ldapconn *)ch_calloc( 1, sizeof( struct ldapconn ) );
432 (*lcp)->lc_flags= li->flags;
435 (*lcp)->lc_refcnt = 1;
438 LDAP_BACK_CONN_ISTLS_SET( *lcp );
440 LDAP_BACK_CONN_ISTLS_CLEAR( *lcp );
442 #endif /* HAVE_TLS */
445 if ( rs->sr_err != LDAP_SUCCESS ) {
446 rs->sr_err = slap_map_api2result( rs );
447 if ( sendok & LDAP_BACK_SENDERR ) {
448 if ( rs->sr_text == NULL ) {
449 rs->sr_text = "ldap_initialize() failed";
451 send_ldap_result( op, rs );
460 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
462 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
467 /* Internal searches are privileged and shared. So is root. */
468 if ( op->o_do_not_cache || be_isroot( op ) ) {
469 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
470 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
471 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
474 lc_curr.lc_local_ndn = op->o_ndn;
475 /* Explicit binds must not be shared */
476 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
477 lc_curr.lc_conn = op->o_conn;
480 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
484 /* Searches for a ldapconn in the avl tree */
485 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
487 lc = (struct ldapconn *)avl_find( li->conntree,
488 (caddr_t)&lc_curr, ldap_back_conn_cmp );
490 refcnt = ++lc->lc_refcnt;
492 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
494 /* Looks like we didn't get a bind. Open a new session... */
496 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
500 lc->lc_conn = lc_curr.lc_conn;
501 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
503 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
504 ber_dupbv( &lc->lc_cred, &li->acl_passwd );
505 ber_dupbv( &lc->lc_bound_ndn, &li->acl_authcDN );
506 LDAP_BACK_CONN_ISPRIV_SET( lc );
509 BER_BVZERO( &lc->lc_cred );
510 BER_BVZERO( &lc->lc_bound_ndn );
512 /* FIXME: if we set lc_bound_ndn = o_ndn
513 * we end up with a bind with DN but no password! */
514 if ( !BER_BVISEMPTY( &op->o_ndn )
515 && SLAP_IS_AUTHZ_BACKEND( op ) )
517 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
523 /* if start TLS failed but it was not mandatory,
524 * check if the non-TLS connection was already
525 * in cache; in case, destroy the newly created
526 * connection and use the existing one */
527 if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
528 && !ldap_tls_inplace( lc->lc_ld ) )
530 struct ldapconn *tmplc;
532 lc_curr.lc_conn = LDAP_BACK_PCONN;
533 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
534 tmplc = (struct ldapconn *)avl_find( li->conntree,
535 (caddr_t)&lc_curr, ldap_back_conn_cmp );
536 if ( tmplc != NULL ) {
537 refcnt = ++tmplc->lc_refcnt;
538 ldap_back_conn_free( lc );
541 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
543 if ( tmplc != NULL ) {
547 #endif /* HAVE_TLS */
549 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
551 /* Inserts the newly created ldapconn in the avl tree */
552 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
554 assert( lc->lc_refcnt == 1 );
555 rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
556 ldap_back_conn_cmp, ldap_back_conn_dup );
558 #if PRINT_CONNTREE > 0
559 myprint( li->conntree );
560 #endif /* PRINT_CONNTREE */
562 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
564 Debug( LDAP_DEBUG_TRACE,
565 "=>ldap_back_getconn: conn %p inserted (refcnt=%u)\n",
566 (void *)lc, refcnt, 0 );
568 /* Err could be -1 in case a duplicate ldapconn is inserted */
569 if ( rs->sr_err != 0 ) {
570 ldap_back_conn_free( lc );
571 rs->sr_err = LDAP_OTHER;
572 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
573 send_ldap_error( op, rs, LDAP_OTHER,
574 "internal server error" );
580 Debug( LDAP_DEBUG_TRACE,
581 "=>ldap_back_getconn: conn %p fetched (refcnt=%u)\n",
582 (void *)lc, refcnt, 0 );
590 ldap_back_release_conn(
593 struct ldapconn *lc )
595 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
597 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
598 assert( lc->lc_refcnt > 0 );
600 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
606 * Note: as the check for the value of lc->lc_bound was already here, I removed
607 * it from all the callers, and I made the function return the flag, so
608 * it can be used to simplify the check.
610 * Note: dolock indicates whether li->conn_mutex must be locked or not
613 ldap_back_dobind_int(
617 ldap_back_send_t sendok,
621 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
623 int rc = LDAP_BACK_CONN_ISBOUND( lc );
626 assert( retries >= 0 );
633 * FIXME: we need to let clients use proxyAuthz
634 * otherwise we cannot do symmetric pools of servers;
635 * we have to live with the fact that a user can
636 * authorize itself as any ID that is allowed
637 * by the authzTo directive of the "proxyauthzdn".
640 * NOTE: current Proxy Authorization specification
641 * and implementation do not allow proxy authorization
642 * control to be provided with Bind requests
645 * if no bind took place yet, but the connection is bound
646 * and the "idassert-authcDN" (or other ID) is set,
647 * then bind as the asserting identity and explicitly
648 * add the proxyAuthz control to every operation with the
649 * dn bound to the connection as control value.
650 * This is done also if this is the authrizing backend,
651 * but the "override" flag is given to idassert.
652 * It allows to use SASL bind and yet proxyAuthz users
654 if ( op->o_conn != NULL &&
655 !op->o_do_not_cache &&
656 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
657 ( li->idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
659 (void)ldap_back_proxy_authz_bind( lc, op, rs );
663 #ifdef HAVE_CYRUS_SASL
664 if ( LDAP_BACK_CONN_ISPRIV( lc )
665 && li->acl_authmethod == LDAP_AUTH_SASL )
667 void *defaults = NULL;
669 if ( li->acl_secprops != NULL ) {
670 rc = ldap_set_option( lc->lc_ld,
671 LDAP_OPT_X_SASL_SECPROPS, li->acl_secprops);
673 if ( rc != LDAP_OPT_SUCCESS ) {
674 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
675 "(%s,SECPROPS,\"%s\") failed!\n",
676 li->url, li->acl_secprops, 0 );
681 defaults = lutil_sasl_defaults( lc->lc_ld,
682 li->acl_sasl_mech.bv_val,
683 li->acl_sasl_realm.bv_val,
684 li->acl_authcID.bv_val,
685 li->acl_passwd.bv_val,
688 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
689 li->acl_authcDN.bv_val,
690 li->acl_sasl_mech.bv_val, NULL, NULL,
691 LDAP_SASL_QUIET, lutil_sasl_interact,
694 lutil_sasl_freedefs( defaults );
696 rs->sr_err = slap_map_api2result( rs );
697 if ( rs->sr_err != LDAP_SUCCESS ) {
698 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
699 send_ldap_result( op, rs );
702 LDAP_BACK_CONN_ISBOUND_SET( lc );
706 #endif /* HAVE_CYRUS_SASL */
709 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
710 lc->lc_bound_ndn.bv_val,
711 LDAP_SASL_SIMPLE, &lc->lc_cred,
712 NULL, NULL, &msgid );
714 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
715 if ( retries != LDAP_BACK_RETRY_NEVER ) {
717 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
720 assert( lc->lc_refcnt > 0 );
721 if ( lc->lc_refcnt == 1 ) {
722 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
725 /* lc here must be the regular lc, reset and ready for init */
726 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
729 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
731 if ( rs->sr_err == LDAP_SUCCESS ) {
739 ldap_back_freeconn( op, lc );
740 rs->sr_err = slap_map_api2result( rs );
745 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
746 if ( rc == LDAP_SUCCESS ) {
747 LDAP_BACK_CONN_ISBOUND_SET( lc );
750 ldap_back_release_conn( op, rs, lc );
754 rc = LDAP_BACK_CONN_ISBOUND( lc );
760 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
762 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
764 return ldap_back_dobind_int( lc, op, rs, sendok, li->nretries, 1 );
768 * ldap_back_default_rebind
770 * This is a callback used for chasing referrals using the same
771 * credentials as the original user on this session.
774 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
775 ber_int_t msgid, void *params )
777 struct ldapconn *lc = (struct ldapconn *)params;
780 /* ... otherwise we couldn't get here */
781 assert( lc != NULL );
783 if ( !ldap_tls_inplace( ld ) ) {
784 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
786 const char *text = NULL;
788 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
789 LDAP_BACK_RETRY_DEFAULT, &text );
790 if ( rc != LDAP_SUCCESS ) {
794 #endif /* HAVE_TLS */
796 /* FIXME: add checks on the URL/identity? */
798 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
799 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
809 ldap_back_send_t sendok )
812 LDAPMessage *res = NULL;
815 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
818 rs->sr_matched = NULL;
820 /* if the error recorded in the reply corresponds
821 * to a successful state, get the error from the
822 * remote server response */
823 if ( ERR_OK( rs->sr_err ) ) {
832 LDAP_BACK_TV_SET( &tv );
836 /* if result parsing fails, note the failure reason */
837 rc = ldap_result( lc->lc_ld, msgid, 1, &tv, &res );
841 (void)ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
842 rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
843 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
844 rs->sr_text = "Operation timed out";
848 LDAP_BACK_TV_SET( &tv );
849 ldap_pvt_thread_yield();
853 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
858 /* otherwise get the result; if it is not
859 * LDAP_SUCCESS, record it in the reply
860 * structure (this includes
861 * LDAP_COMPARE_{TRUE|FALSE}) */
863 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
864 &match, &text, NULL, NULL, 1 );
866 if ( rc != LDAP_SUCCESS ) {
872 /* if the error in the reply structure is not
873 * LDAP_SUCCESS, try to map it from client
875 if ( !ERR_OK( rs->sr_err ) ) {
876 rs->sr_err = slap_map_api2result( rs );
878 /* internal ops ( op->o_conn == NULL )
879 * must not reply to client */
880 if ( op->o_conn && !op->o_do_not_cache && match ) {
882 /* record the (massaged) matched
883 * DN into the reply structure */
884 rs->sr_matched = match;
888 ( ( sendok & LDAP_BACK_SENDOK )
889 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
891 send_ldap_result( op, rs );
894 if ( rs->sr_matched != match ) {
895 free( (char *)rs->sr_matched );
897 rs->sr_matched = NULL;
898 ldap_memfree( match );
901 ldap_memfree( text );
904 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
907 /* return true if bound, false if failed */
909 ldap_back_retry( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
912 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
914 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
916 if ( lc->lc_refcnt == 1 ) {
917 Debug( LDAP_DEBUG_ANY,
918 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
919 op->o_log_prefix, li->url,
920 BER_BVISNULL( &lc->lc_bound_ndn ) ?
921 "" : lc->lc_bound_ndn.bv_val );
923 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
925 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
927 /* lc here must be the regular lc, reset and ready for init */
928 rc = ldap_back_prepare_conn( &lc, op, rs, sendok );
929 if ( rc == LDAP_SUCCESS ) {
930 rc = ldap_back_dobind_int( lc, op, rs, sendok, 0, 0 );
934 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
940 ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs )
942 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
943 struct berval binddn = slap_empty_bv;
944 struct berval bindcred = slap_empty_bv;
950 * FIXME: we need to let clients use proxyAuthz
951 * otherwise we cannot do symmetric pools of servers;
952 * we have to live with the fact that a user can
953 * authorize itself as any ID that is allowed
954 * by the authzTo directive of the "proxyauthzdn".
957 * NOTE: current Proxy Authorization specification
958 * and implementation do not allow proxy authorization
959 * control to be provided with Bind requests
962 * if no bind took place yet, but the connection is bound
963 * and the "proxyauthzdn" is set, then bind as
964 * "proxyauthzdn" and explicitly add the proxyAuthz
965 * control to every operation with the dn bound
966 * to the connection as control value.
969 /* bind as proxyauthzdn only if no idassert mode
970 * is requested, or if the client's identity
972 switch ( li->idassert_mode ) {
973 case LDAP_BACK_IDASSERT_LEGACY:
974 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) && !BER_BVISEMPTY( &op->o_conn->c_ndn ) ) {
975 if ( !BER_BVISNULL( &li->idassert_authcDN ) && !BER_BVISEMPTY( &li->idassert_authcDN ) )
977 binddn = li->idassert_authcDN;
978 bindcred = li->idassert_passwd;
985 /* NOTE: rootdn can always idassert */
986 if ( li->idassert_authz && !be_isroot( op ) ) {
987 struct berval authcDN;
989 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
990 authcDN = slap_empty_bv;
993 authcDN = op->o_conn->c_ndn;
995 rs->sr_err = slap_sasl_matches( op, li->idassert_authz,
996 &authcDN, &authcDN );
997 if ( rs->sr_err != LDAP_SUCCESS ) {
998 if ( li->idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
999 send_ldap_result( op, rs );
1000 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1003 rs->sr_err = LDAP_SUCCESS;
1004 binddn = slap_empty_bv;
1005 bindcred = slap_empty_bv;
1013 binddn = li->idassert_authcDN;
1014 bindcred = li->idassert_passwd;
1019 if ( dobind && li->idassert_authmethod == LDAP_AUTH_SASL ) {
1020 #ifdef HAVE_CYRUS_SASL
1021 void *defaults = NULL;
1022 struct berval authzID = BER_BVNULL;
1025 /* if SASL supports native authz, prepare for it */
1026 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1027 ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1029 switch ( li->idassert_mode ) {
1030 case LDAP_BACK_IDASSERT_OTHERID:
1031 case LDAP_BACK_IDASSERT_OTHERDN:
1032 authzID = li->idassert_authzID;
1035 case LDAP_BACK_IDASSERT_ANONYMOUS:
1036 BER_BVSTR( &authzID, "dn:" );
1039 case LDAP_BACK_IDASSERT_SELF:
1040 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1041 /* connection is not authc'd, so don't idassert */
1042 BER_BVSTR( &authzID, "dn:" );
1045 authzID.bv_len = STRLENOF( "dn:" ) + op->o_conn->c_ndn.bv_len;
1046 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1047 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1048 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1049 op->o_conn->c_ndn.bv_val, op->o_conn->c_ndn.bv_len + 1 );
1058 if ( li->idassert_secprops != NULL ) {
1059 rs->sr_err = ldap_set_option( lc->lc_ld,
1060 LDAP_OPT_X_SASL_SECPROPS,
1061 (void *)li->idassert_secprops );
1063 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1064 send_ldap_result( op, rs );
1065 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1070 defaults = lutil_sasl_defaults( lc->lc_ld,
1071 li->idassert_sasl_mech.bv_val,
1072 li->idassert_sasl_realm.bv_val,
1073 li->idassert_authcID.bv_val,
1074 li->idassert_passwd.bv_val,
1077 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1078 li->idassert_sasl_mech.bv_val, NULL, NULL,
1079 LDAP_SASL_QUIET, lutil_sasl_interact,
1082 rs->sr_err = slap_map_api2result( rs );
1083 if ( rs->sr_err != LDAP_SUCCESS ) {
1084 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1085 send_ldap_result( op, rs );
1088 LDAP_BACK_CONN_ISBOUND_SET( lc );
1091 lutil_sasl_freedefs( defaults );
1093 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1097 #endif /* HAVE_CYRUS_SASL */
1100 switch ( li->idassert_authmethod ) {
1101 case LDAP_AUTH_NONE:
1102 LDAP_BACK_CONN_ISBOUND_SET( lc );
1105 case LDAP_AUTH_SIMPLE:
1106 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1107 binddn.bv_val, LDAP_SASL_SIMPLE,
1108 &bindcred, NULL, NULL, &msgid );
1113 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1114 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1115 send_ldap_result( op, rs );
1119 rc = ldap_back_op_result( lc, op, rs, msgid, 0, LDAP_BACK_SENDERR );
1120 if ( rc == LDAP_SUCCESS ) {
1121 LDAP_BACK_CONN_ISBOUND_SET( lc );
1124 return LDAP_BACK_CONN_ISBOUND( lc );
1128 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1129 * to existing server-side controls if required; if not,
1130 * the existing server-side controls are placed in *pctrls.
1131 * The caller, after using the controls in client API
1132 * operations, if ( *pctrls != op->o_ctrls ), should
1133 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1134 * The function returns success if the control could
1135 * be added if required, or if it did nothing; in the future,
1136 * it might return some error if it failed.
1138 * if no bind took place yet, but the connection is bound
1139 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1140 * and explicitly add proxyAuthz the control to every operation
1141 * with the dn bound to the connection as control value.
1143 * If no server-side controls are defined for the operation,
1144 * simply add the proxyAuthz control; otherwise, if the
1145 * proxyAuthz control is not already set, add it as
1148 * FIXME: is controls order significant for security?
1149 * ANSWER: controls ordering and interoperability
1150 * must be indicated by the specs of each control; if none
1151 * is specified, the order is irrelevant.
1154 ldap_back_proxy_authz_ctrl(
1155 struct ldapconn *lc,
1158 LDAPControl ***pctrls )
1160 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
1161 LDAPControl **ctrls = NULL;
1164 struct berval assertedID;
1168 rs->sr_err = LDAP_SUCCESS;
1170 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1171 * but if it is not set this test fails. We need a different
1172 * means to detect if idassert is enabled */
1173 if ( ( BER_BVISNULL( &li->idassert_authcID ) || BER_BVISEMPTY( &li->idassert_authcID ) )
1174 && ( BER_BVISNULL( &li->idassert_authcDN ) || BER_BVISEMPTY( &li->idassert_authcDN ) ) )
1179 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1183 if ( li->idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1184 if ( op->o_proxy_authz ) {
1186 * FIXME: we do not want to perform proxyAuthz
1187 * on behalf of the client, because this would
1188 * be performed with "proxyauthzdn" privileges.
1190 * This might actually be too strict, since
1191 * the "proxyauthzdn" authzTo, and each entry's
1192 * authzFrom attributes may be crafted
1193 * to avoid unwanted proxyAuthz to take place.
1196 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1197 rs->sr_text = "proxyAuthz not allowed within namingContext";
1202 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1206 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1210 if ( BER_BVISNULL( &li->idassert_authcDN ) ) {
1214 } else if ( li->idassert_authmethod == LDAP_AUTH_SASL ) {
1215 if ( ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1216 /* && ( !BER_BVISNULL( &op->o_conn->c_ndn )
1217 || LDAP_BACK_CONN_ISBOUND( lc ) ) */ )
1219 /* already asserted in SASL via native authz */
1220 /* NOTE: the test on lc->lc_bound is used to trap
1221 * native authorization of anonymous users,
1222 * since in that case op->o_conn->c_ndn is NULL */
1226 } else if ( li->idassert_authz && !be_isroot( op ) ) {
1228 struct berval authcDN;
1230 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1231 authcDN = slap_empty_bv;
1233 authcDN = op->o_conn->c_ndn;
1235 rc = slap_sasl_matches( op, li->idassert_authz,
1236 &authcDN, & authcDN );
1237 if ( rc != LDAP_SUCCESS ) {
1238 if ( li->idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1240 /* op->o_conn->c_ndn is not authorized
1241 * to use idassert */
1248 if ( op->o_proxy_authz ) {
1251 * 1) ignore the already set proxyAuthz control
1252 * 2) leave it in place, and don't set ours
1254 * 4) reject the operation
1256 * option (4) is very drastic
1257 * option (3) will make the remote server reject
1258 * the operation, thus being equivalent to (4)
1259 * option (2) will likely break the idassert
1260 * assumptions, so we cannot accept it;
1261 * option (1) means that we are contradicting
1262 * the client's reques.
1264 * I think (4) is the only correct choice.
1266 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1267 rs->sr_text = "proxyAuthz not allowed within namingContext";
1270 if ( op->o_is_auth_check ) {
1271 mode = LDAP_BACK_IDASSERT_NOASSERT;
1274 mode = li->idassert_mode;
1278 case LDAP_BACK_IDASSERT_LEGACY:
1279 case LDAP_BACK_IDASSERT_SELF:
1280 /* original behavior:
1281 * assert the client's identity */
1282 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1283 assertedID = slap_empty_bv;
1285 assertedID = op->o_conn->c_ndn;
1289 case LDAP_BACK_IDASSERT_ANONYMOUS:
1290 /* assert "anonymous" */
1291 assertedID = slap_empty_bv;
1294 case LDAP_BACK_IDASSERT_NOASSERT:
1295 /* don't assert; bind as proxyauthzdn */
1298 case LDAP_BACK_IDASSERT_OTHERID:
1299 case LDAP_BACK_IDASSERT_OTHERDN:
1300 /* assert idassert DN */
1301 assertedID = li->idassert_authzID;
1308 if ( BER_BVISNULL( &assertedID ) ) {
1309 assertedID = slap_empty_bv;
1312 if ( op->o_ctrls ) {
1313 for ( i = 0; op->o_ctrls[ i ]; i++ )
1314 /* just count ctrls */ ;
1317 ctrls = ch_malloc( sizeof( LDAPControl * ) * (i + 2) );
1318 ctrls[ 0 ] = ch_malloc( sizeof( LDAPControl ) );
1320 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1321 ctrls[ 0 ]->ldctl_iscritical = 1;
1323 switch ( li->idassert_mode ) {
1324 /* already in u:ID or dn:DN form */
1325 case LDAP_BACK_IDASSERT_OTHERID:
1326 case LDAP_BACK_IDASSERT_OTHERDN:
1327 ber_dupbv( &ctrls[ 0 ]->ldctl_value, &assertedID );
1330 /* needs the dn: prefix */
1332 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1333 ctrls[ 0 ]->ldctl_value.bv_val = ch_malloc( ctrls[ 0 ]->ldctl_value.bv_len + 1 );
1334 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1335 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1336 assertedID.bv_val, assertedID.bv_len + 1 );
1340 if ( op->o_ctrls ) {
1341 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1342 ctrls[ i + 1 ] = op->o_ctrls[ i ];
1345 ctrls[ i + 1 ] = NULL;
1348 if ( ctrls == NULL ) {
1349 ctrls = op->o_ctrls;
1358 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1360 LDAPControl **ctrls = *pctrls;
1362 /* we assume that the first control is the proxyAuthz
1363 * added by back-ldap, so it's the only one we explicitly
1365 if ( ctrls && ctrls != op->o_ctrls ) {
1366 assert( ctrls[ 0 ] != NULL );
1368 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1369 free( ctrls[ 0 ]->ldctl_value.bv_val );