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( ldapconn_t *lc, Operation *op, SlapReply *rs );
48 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
51 ldap_back_bind( Operation *op, SlapReply *rs )
53 ldapinfo_t *li = (ldapinfo_t *) 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->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->li_conninfo.lai_mutex );
113 if ( lc->lc_refcnt > 1 ) {
114 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
115 ldap_pvt_thread_yield();
119 assert( lc->lc_refcnt == 1 );
120 lc = avl_delete( &li->li_conninfo.lai_tree, (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->li_conninfo.lai_tree, (caddr_t)lc,
126 ldap_back_conn_cmp, ldap_back_conn_dup );
127 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_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 ldapconn_t based on the value of the conn pointer;
149 ldap_back_conn_cmp( const void *c1, const void *c2 )
151 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
152 const ldapconn_t *lc2 = (const ldapconn_t *)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 ldapconn_t has been inserted;
174 ldap_back_conn_dup( void *c1, void *c2 )
176 ldapconn_t *lc1 = (ldapconn_t *)c1;
177 ldapconn_t *lc2 = (ldapconn_t *)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, ldapconn_t *lc, int dolock )
232 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
235 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
238 assert( lc->lc_refcnt > 0 );
239 if ( --lc->lc_refcnt == 0 ) {
240 lc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
241 ldap_back_conn_cmp );
242 assert( lc != NULL );
244 ldap_back_conn_free( (void *)lc );
248 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
265 int rc = LDAP_SUCCESS;
268 /* this is ridiculous... */
269 dummy.li_flags = flags;
271 /* start TLS ("tls-[try-]{start,propagate}" statements) */
272 if ( ( LDAP_BACK_USE_TLS( &dummy ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS( &dummy ) ) )
273 && !ldap_is_ldaps_url( url ) )
275 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
277 * use asynchronous StartTLS
278 * in case, chase referral (not implemented yet)
282 if ( protocol == 0 ) {
283 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
287 if ( protocol < LDAP_VERSION3 ) {
288 protocol = LDAP_VERSION3;
289 /* Set LDAP version */
290 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
291 (const void *)&protocol );
294 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
295 if ( rc == LDAP_SUCCESS ) {
296 LDAPMessage *res = NULL;
299 LDAP_BACK_TV_SET( &tv );
302 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
306 } else if ( rc == 0 ) {
307 if ( retries != LDAP_BACK_RETRY_NEVER ) {
308 ldap_pvt_thread_yield();
312 LDAP_BACK_TV_SET( &tv );
317 } else if ( rc == LDAP_RES_EXTENDED ) {
318 struct berval *data = NULL;
320 rc = ldap_parse_extended_result( ld, res,
322 if ( rc == LDAP_SUCCESS ) {
324 rc = ldap_parse_result( ld, res, &err,
325 NULL, NULL, NULL, NULL, 1 );
326 if ( rc == LDAP_SUCCESS ) {
331 /* FIXME: in case a referral
332 * is returned, should we try
333 * using it instead of the
335 if ( rc == LDAP_SUCCESS ) {
336 rc = ldap_install_tls( ld );
338 } else if ( rc == LDAP_REFERRAL ) {
340 *text = "unwilling to chase referral returned by Start TLS exop";
344 if ( data->bv_val ) {
345 ber_memfree( data->bv_val );
359 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
361 * use synchronous StartTLS
363 rc = ldap_start_tls_s( ld, NULL, NULL );
364 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
366 /* if StartTLS is requested, only attempt it if the URL
367 * is not "ldaps://"; this may occur not only in case
368 * of misconfiguration, but also when used in the chain
369 * overlay, where the "uri" can be parsed out of a referral */
375 case LDAP_SERVER_DOWN:
379 if ( LDAP_BACK_TLS_CRITICAL( &dummy ) ) {
380 *text = "could not start TLS";
384 /* in case Start TLS is not critical */
396 #endif /* HAVE_TLS */
399 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
401 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
402 int vers = op->o_protocol;
405 int is_tls = op->o_conn->c_is_tls;
406 #endif /* HAVE_TLS */
408 assert( lcp != NULL );
410 rs->sr_err = ldap_initialize( &ld, li->li_uri );
411 if ( rs->sr_err != LDAP_SUCCESS ) {
415 /* Set LDAP version. This will always succeed: If the client
416 * bound with a particular version, then so can we.
418 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&vers );
420 /* automatically chase referrals ("[dont-]chase-referrals" statement) */
421 if ( LDAP_BACK_CHASE_REFERRALS( li ) ) {
422 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
426 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
427 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
428 if ( rs->sr_err != LDAP_SUCCESS ) {
429 ldap_unbind_ext( ld, NULL, NULL );
432 #endif /* HAVE_TLS */
434 if ( *lcp == NULL ) {
435 *lcp = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
436 (*lcp)->lc_flags= li->li_flags;
439 (*lcp)->lc_refcnt = 1;
442 LDAP_BACK_CONN_ISTLS_SET( *lcp );
444 LDAP_BACK_CONN_ISTLS_CLEAR( *lcp );
446 #endif /* HAVE_TLS */
449 if ( rs->sr_err != LDAP_SUCCESS ) {
450 rs->sr_err = slap_map_api2result( rs );
451 if ( sendok & LDAP_BACK_SENDERR ) {
452 if ( rs->sr_text == NULL ) {
453 rs->sr_text = "ldap_initialize() failed";
455 send_ldap_result( op, rs );
464 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
466 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
471 /* Internal searches are privileged and shared. So is root. */
472 if ( op->o_do_not_cache || be_isroot( op ) ) {
473 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
474 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
475 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
478 lc_curr.lc_local_ndn = op->o_ndn;
479 /* Explicit binds must not be shared */
480 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
481 lc_curr.lc_conn = op->o_conn;
484 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
488 /* Searches for a ldapconn in the avl tree */
489 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
491 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
492 (caddr_t)&lc_curr, ldap_back_conn_cmp );
494 refcnt = ++lc->lc_refcnt;
496 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
498 /* Looks like we didn't get a bind. Open a new session... */
500 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
504 lc->lc_conn = lc_curr.lc_conn;
505 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
507 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
508 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
509 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
510 LDAP_BACK_CONN_ISPRIV_SET( lc );
513 BER_BVZERO( &lc->lc_cred );
514 BER_BVZERO( &lc->lc_bound_ndn );
516 /* FIXME: if we set lc_bound_ndn = o_ndn
517 * we end up with a bind with DN but no password! */
518 if ( !BER_BVISEMPTY( &op->o_ndn )
519 && SLAP_IS_AUTHZ_BACKEND( op ) )
521 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
527 /* if start TLS failed but it was not mandatory,
528 * check if the non-TLS connection was already
529 * in cache; in case, destroy the newly created
530 * connection and use the existing one */
531 if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
532 && !ldap_tls_inplace( lc->lc_ld ) )
536 lc_curr.lc_conn = LDAP_BACK_PCONN;
537 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
538 tmplc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
539 (caddr_t)&lc_curr, ldap_back_conn_cmp );
540 if ( tmplc != NULL ) {
541 refcnt = ++tmplc->lc_refcnt;
542 ldap_back_conn_free( lc );
545 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
547 if ( tmplc != NULL ) {
551 #endif /* HAVE_TLS */
553 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
555 /* Inserts the newly created ldapconn in the avl tree */
556 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
558 assert( lc->lc_refcnt == 1 );
559 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
560 ldap_back_conn_cmp, ldap_back_conn_dup );
562 #if PRINT_CONNTREE > 0
563 myprint( li->li_conninfo.lai_tree );
564 #endif /* PRINT_CONNTREE */
566 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
568 Debug( LDAP_DEBUG_TRACE,
569 "=>ldap_back_getconn: conn %p inserted (refcnt=%u)\n",
570 (void *)lc, refcnt, 0 );
572 /* Err could be -1 in case a duplicate ldapconn is inserted */
573 if ( rs->sr_err != 0 ) {
574 ldap_back_conn_free( lc );
575 rs->sr_err = LDAP_OTHER;
576 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
577 send_ldap_error( op, rs, LDAP_OTHER,
578 "internal server error" );
584 Debug( LDAP_DEBUG_TRACE,
585 "=>ldap_back_getconn: conn %p fetched (refcnt=%u)\n",
586 (void *)lc, refcnt, 0 );
594 ldap_back_release_conn(
599 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
601 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
602 assert( lc->lc_refcnt > 0 );
604 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
610 * Note: as the check for the value of lc->lc_bound was already here, I removed
611 * it from all the callers, and I made the function return the flag, so
612 * it can be used to simplify the check.
614 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
617 ldap_back_dobind_int(
621 ldap_back_send_t sendok,
625 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
627 int rc = LDAP_BACK_CONN_ISBOUND( lc );
630 assert( retries >= 0 );
637 * FIXME: we need to let clients use proxyAuthz
638 * otherwise we cannot do symmetric pools of servers;
639 * we have to live with the fact that a user can
640 * authorize itself as any ID that is allowed
641 * by the authzTo directive of the "proxyauthzdn".
644 * NOTE: current Proxy Authorization specification
645 * and implementation do not allow proxy authorization
646 * control to be provided with Bind requests
649 * if no bind took place yet, but the connection is bound
650 * and the "idassert-authcDN" (or other ID) is set,
651 * then bind as the asserting identity and explicitly
652 * add the proxyAuthz control to every operation with the
653 * dn bound to the connection as control value.
654 * This is done also if this is the authrizing backend,
655 * but the "override" flag is given to idassert.
656 * It allows to use SASL bind and yet proxyAuthz users
658 if ( op->o_conn != NULL &&
659 !op->o_do_not_cache &&
660 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
661 ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
663 (void)ldap_back_proxy_authz_bind( lc, op, rs );
667 #ifdef HAVE_CYRUS_SASL
668 if ( LDAP_BACK_CONN_ISPRIV( lc )
669 && li->li_acl_authmethod == LDAP_AUTH_SASL )
671 void *defaults = NULL;
673 if ( li->li_acl_secprops != NULL ) {
674 rc = ldap_set_option( lc->lc_ld,
675 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops);
677 if ( rc != LDAP_OPT_SUCCESS ) {
678 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
679 "(%s,SECPROPS,\"%s\") failed!\n",
680 li->li_uri, li->li_acl_secprops, 0 );
685 defaults = lutil_sasl_defaults( lc->lc_ld,
686 li->li_acl_sasl_mech.bv_val,
687 li->li_acl_sasl_realm.bv_val,
688 li->li_acl_authcID.bv_val,
689 li->li_acl_passwd.bv_val,
692 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
693 li->li_acl_authcDN.bv_val,
694 li->li_acl_sasl_mech.bv_val, NULL, NULL,
695 LDAP_SASL_QUIET, lutil_sasl_interact,
698 lutil_sasl_freedefs( defaults );
700 rs->sr_err = slap_map_api2result( rs );
701 if ( rs->sr_err != LDAP_SUCCESS ) {
702 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
703 send_ldap_result( op, rs );
706 LDAP_BACK_CONN_ISBOUND_SET( lc );
710 #endif /* HAVE_CYRUS_SASL */
713 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
714 lc->lc_bound_ndn.bv_val,
715 LDAP_SASL_SIMPLE, &lc->lc_cred,
716 NULL, NULL, &msgid );
718 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
719 if ( retries != LDAP_BACK_RETRY_NEVER ) {
721 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
724 assert( lc->lc_refcnt > 0 );
725 if ( lc->lc_refcnt == 1 ) {
726 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
729 /* lc here must be the regular lc, reset and ready for init */
730 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
733 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
735 if ( rs->sr_err == LDAP_SUCCESS ) {
743 ldap_back_freeconn( op, lc, dolock );
744 rs->sr_err = slap_map_api2result( rs );
749 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
750 if ( rc == LDAP_SUCCESS ) {
751 LDAP_BACK_CONN_ISBOUND_SET( lc );
754 ldap_back_release_conn( op, rs, lc );
758 rc = LDAP_BACK_CONN_ISBOUND( lc );
764 ldap_back_dobind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
766 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
768 return ldap_back_dobind_int( lc, op, rs, sendok, li->li_nretries, 1 );
772 * ldap_back_default_rebind
774 * This is a callback used for chasing referrals using the same
775 * credentials as the original user on this session.
778 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
779 ber_int_t msgid, void *params )
781 ldapconn_t *lc = (ldapconn_t *)params;
784 /* ... otherwise we couldn't get here */
785 assert( lc != NULL );
787 if ( !ldap_tls_inplace( ld ) ) {
788 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
790 const char *text = NULL;
792 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
793 LDAP_BACK_RETRY_DEFAULT, &text );
794 if ( rc != LDAP_SUCCESS ) {
798 #endif /* HAVE_TLS */
800 /* FIXME: add checks on the URL/identity? */
802 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
803 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
813 ldap_back_send_t sendok )
816 LDAPMessage *res = NULL;
819 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
822 rs->sr_matched = NULL;
824 /* if the error recorded in the reply corresponds
825 * to a successful state, get the error from the
826 * remote server response */
827 if ( ERR_OK( rs->sr_err ) ) {
836 LDAP_BACK_TV_SET( &tv );
840 /* if result parsing fails, note the failure reason */
841 rc = ldap_result( lc->lc_ld, msgid, 1, &tv, &res );
845 (void)ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
846 rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
847 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
848 rs->sr_text = "Operation timed out";
852 LDAP_BACK_TV_SET( &tv );
853 ldap_pvt_thread_yield();
857 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
862 /* otherwise get the result; if it is not
863 * LDAP_SUCCESS, record it in the reply
864 * structure (this includes
865 * LDAP_COMPARE_{TRUE|FALSE}) */
867 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
868 &match, &text, NULL, NULL, 1 );
869 #ifndef LDAP_NULL_IS_NULL
870 if ( match != NULL && match[ 0 ] == '\0' ) {
871 ldap_memfree( match );
874 if ( text != NULL && text[ 0 ] == '\0' ) {
875 ldap_memfree( text );
878 #endif /* LDAP_NULL_IS_NULL */
880 if ( rc != LDAP_SUCCESS ) {
886 /* if the error in the reply structure is not
887 * LDAP_SUCCESS, try to map it from client
889 if ( !ERR_OK( rs->sr_err ) ) {
890 rs->sr_err = slap_map_api2result( rs );
892 /* internal ops ( op->o_conn == NULL )
893 * must not reply to client */
894 if ( op->o_conn && !op->o_do_not_cache && match ) {
896 /* record the (massaged) matched
897 * DN into the reply structure */
898 rs->sr_matched = match;
902 ( ( sendok & LDAP_BACK_SENDOK )
903 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
905 send_ldap_result( op, rs );
908 if ( rs->sr_matched != match ) {
909 free( (char *)rs->sr_matched );
911 rs->sr_matched = NULL;
912 ldap_memfree( match );
915 ldap_memfree( text );
918 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
921 /* return true if bound, false if failed */
923 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
926 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
928 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
930 if ( (*lcp)->lc_refcnt == 1 ) {
931 Debug( LDAP_DEBUG_ANY,
932 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
933 op->o_log_prefix, li->li_uri,
934 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
935 "" : (*lcp)->lc_bound_ndn.bv_val );
937 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
938 (*lcp)->lc_ld = NULL;
939 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
941 /* lc here must be the regular lc, reset and ready for init */
942 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
943 if ( rc == LDAP_SUCCESS ) {
944 rc = ldap_back_dobind_int( *lcp, op, rs, sendok, 0, 0 );
951 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
957 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs )
959 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
960 struct berval binddn = slap_empty_bv;
961 struct berval bindcred = slap_empty_bv;
967 * FIXME: we need to let clients use proxyAuthz
968 * otherwise we cannot do symmetric pools of servers;
969 * we have to live with the fact that a user can
970 * authorize itself as any ID that is allowed
971 * by the authzTo directive of the "proxyauthzdn".
974 * NOTE: current Proxy Authorization specification
975 * and implementation do not allow proxy authorization
976 * control to be provided with Bind requests
979 * if no bind took place yet, but the connection is bound
980 * and the "proxyauthzdn" is set, then bind as
981 * "proxyauthzdn" and explicitly add the proxyAuthz
982 * control to every operation with the dn bound
983 * to the connection as control value.
986 /* bind as proxyauthzdn only if no idassert mode
987 * is requested, or if the client's identity
989 switch ( li->li_idassert_mode ) {
990 case LDAP_BACK_IDASSERT_LEGACY:
991 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) && !BER_BVISEMPTY( &op->o_conn->c_ndn ) ) {
992 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
994 binddn = li->li_idassert_authcDN;
995 bindcred = li->li_idassert_passwd;
1002 /* NOTE: rootdn can always idassert */
1003 if ( li->li_idassert_authz && !be_isroot( op ) ) {
1004 struct berval authcDN;
1006 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1007 authcDN = slap_empty_bv;
1010 authcDN = op->o_conn->c_ndn;
1012 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1013 &authcDN, &authcDN );
1014 if ( rs->sr_err != LDAP_SUCCESS ) {
1015 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1016 send_ldap_result( op, rs );
1017 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1020 rs->sr_err = LDAP_SUCCESS;
1021 binddn = slap_empty_bv;
1022 bindcred = slap_empty_bv;
1030 binddn = li->li_idassert_authcDN;
1031 bindcred = li->li_idassert_passwd;
1036 if ( dobind && li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1037 #ifdef HAVE_CYRUS_SASL
1038 void *defaults = NULL;
1039 struct berval authzID = BER_BVNULL;
1042 /* if SASL supports native authz, prepare for it */
1043 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1044 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1046 switch ( li->li_idassert_mode ) {
1047 case LDAP_BACK_IDASSERT_OTHERID:
1048 case LDAP_BACK_IDASSERT_OTHERDN:
1049 authzID = li->li_idassert_authzID;
1052 case LDAP_BACK_IDASSERT_ANONYMOUS:
1053 BER_BVSTR( &authzID, "dn:" );
1056 case LDAP_BACK_IDASSERT_SELF:
1057 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1058 /* connection is not authc'd, so don't idassert */
1059 BER_BVSTR( &authzID, "dn:" );
1062 authzID.bv_len = STRLENOF( "dn:" ) + op->o_conn->c_ndn.bv_len;
1063 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1064 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1065 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1066 op->o_conn->c_ndn.bv_val, op->o_conn->c_ndn.bv_len + 1 );
1075 if ( li->li_idassert_secprops != NULL ) {
1076 rs->sr_err = ldap_set_option( lc->lc_ld,
1077 LDAP_OPT_X_SASL_SECPROPS,
1078 (void *)li->li_idassert_secprops );
1080 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1081 send_ldap_result( op, rs );
1082 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1087 defaults = lutil_sasl_defaults( lc->lc_ld,
1088 li->li_idassert_sasl_mech.bv_val,
1089 li->li_idassert_sasl_realm.bv_val,
1090 li->li_idassert_authcID.bv_val,
1091 li->li_idassert_passwd.bv_val,
1094 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1095 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1096 LDAP_SASL_QUIET, lutil_sasl_interact,
1099 rs->sr_err = slap_map_api2result( rs );
1100 if ( rs->sr_err != LDAP_SUCCESS ) {
1101 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1102 send_ldap_result( op, rs );
1105 LDAP_BACK_CONN_ISBOUND_SET( lc );
1108 lutil_sasl_freedefs( defaults );
1110 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1114 #endif /* HAVE_CYRUS_SASL */
1117 switch ( li->li_idassert_authmethod ) {
1118 case LDAP_AUTH_NONE:
1119 LDAP_BACK_CONN_ISBOUND_SET( lc );
1122 case LDAP_AUTH_SIMPLE:
1123 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1124 binddn.bv_val, LDAP_SASL_SIMPLE,
1125 &bindcred, NULL, NULL, &msgid );
1130 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1131 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1132 send_ldap_result( op, rs );
1136 rc = ldap_back_op_result( lc, op, rs, msgid, 0, LDAP_BACK_SENDERR );
1137 if ( rc == LDAP_SUCCESS ) {
1138 LDAP_BACK_CONN_ISBOUND_SET( lc );
1141 return LDAP_BACK_CONN_ISBOUND( lc );
1145 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1146 * to existing server-side controls if required; if not,
1147 * the existing server-side controls are placed in *pctrls.
1148 * The caller, after using the controls in client API
1149 * operations, if ( *pctrls != op->o_ctrls ), should
1150 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1151 * The function returns success if the control could
1152 * be added if required, or if it did nothing; in the future,
1153 * it might return some error if it failed.
1155 * if no bind took place yet, but the connection is bound
1156 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1157 * and explicitly add proxyAuthz the control to every operation
1158 * with the dn bound to the connection as control value.
1160 * If no server-side controls are defined for the operation,
1161 * simply add the proxyAuthz control; otherwise, if the
1162 * proxyAuthz control is not already set, add it as
1165 * FIXME: is controls order significant for security?
1166 * ANSWER: controls ordering and interoperability
1167 * must be indicated by the specs of each control; if none
1168 * is specified, the order is irrelevant.
1171 ldap_back_proxy_authz_ctrl(
1175 LDAPControl ***pctrls )
1177 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
1178 LDAPControl **ctrls = NULL;
1181 struct berval assertedID;
1185 rs->sr_err = LDAP_SUCCESS;
1187 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1188 * but if it is not set this test fails. We need a different
1189 * means to detect if idassert is enabled */
1190 if ( ( BER_BVISNULL( &li->li_idassert_authcID ) || BER_BVISEMPTY( &li->li_idassert_authcID ) )
1191 && ( BER_BVISNULL( &li->li_idassert_authcDN ) || BER_BVISEMPTY( &li->li_idassert_authcDN ) ) )
1196 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1200 if ( li->li_idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1201 if ( op->o_proxy_authz ) {
1203 * FIXME: we do not want to perform proxyAuthz
1204 * on behalf of the client, because this would
1205 * be performed with "proxyauthzdn" privileges.
1207 * This might actually be too strict, since
1208 * the "proxyauthzdn" authzTo, and each entry's
1209 * authzFrom attributes may be crafted
1210 * to avoid unwanted proxyAuthz to take place.
1213 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1214 rs->sr_text = "proxyAuthz not allowed within namingContext";
1219 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1223 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1227 if ( BER_BVISNULL( &li->li_idassert_authcDN ) ) {
1231 } else if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1232 if ( ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1233 /* && ( !BER_BVISNULL( &op->o_conn->c_ndn )
1234 || LDAP_BACK_CONN_ISBOUND( lc ) ) */ )
1236 /* already asserted in SASL via native authz */
1237 /* NOTE: the test on lc->lc_bound is used to trap
1238 * native authorization of anonymous users,
1239 * since in that case op->o_conn->c_ndn is NULL */
1243 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1245 struct berval authcDN;
1247 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1248 authcDN = slap_empty_bv;
1250 authcDN = op->o_conn->c_ndn;
1252 rc = slap_sasl_matches( op, li->li_idassert_authz,
1253 &authcDN, & authcDN );
1254 if ( rc != LDAP_SUCCESS ) {
1255 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1257 /* op->o_conn->c_ndn is not authorized
1258 * to use idassert */
1265 if ( op->o_proxy_authz ) {
1268 * 1) ignore the already set proxyAuthz control
1269 * 2) leave it in place, and don't set ours
1271 * 4) reject the operation
1273 * option (4) is very drastic
1274 * option (3) will make the remote server reject
1275 * the operation, thus being equivalent to (4)
1276 * option (2) will likely break the idassert
1277 * assumptions, so we cannot accept it;
1278 * option (1) means that we are contradicting
1279 * the client's reques.
1281 * I think (4) is the only correct choice.
1283 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1284 rs->sr_text = "proxyAuthz not allowed within namingContext";
1287 if ( op->o_is_auth_check ) {
1288 mode = LDAP_BACK_IDASSERT_NOASSERT;
1291 mode = li->li_idassert_mode;
1295 case LDAP_BACK_IDASSERT_LEGACY:
1296 case LDAP_BACK_IDASSERT_SELF:
1297 /* original behavior:
1298 * assert the client's identity */
1299 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1300 assertedID = slap_empty_bv;
1302 assertedID = op->o_conn->c_ndn;
1306 case LDAP_BACK_IDASSERT_ANONYMOUS:
1307 /* assert "anonymous" */
1308 assertedID = slap_empty_bv;
1311 case LDAP_BACK_IDASSERT_NOASSERT:
1312 /* don't assert; bind as proxyauthzdn */
1315 case LDAP_BACK_IDASSERT_OTHERID:
1316 case LDAP_BACK_IDASSERT_OTHERDN:
1317 /* assert idassert DN */
1318 assertedID = li->li_idassert_authzID;
1325 if ( BER_BVISNULL( &assertedID ) ) {
1326 assertedID = slap_empty_bv;
1329 if ( op->o_ctrls ) {
1330 for ( i = 0; op->o_ctrls[ i ]; i++ )
1331 /* just count ctrls */ ;
1334 ctrls = ch_malloc( sizeof( LDAPControl * ) * (i + 2) );
1335 ctrls[ 0 ] = ch_malloc( sizeof( LDAPControl ) );
1337 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1338 ctrls[ 0 ]->ldctl_iscritical = 1;
1340 switch ( li->li_idassert_mode ) {
1341 /* already in u:ID or dn:DN form */
1342 case LDAP_BACK_IDASSERT_OTHERID:
1343 case LDAP_BACK_IDASSERT_OTHERDN:
1344 ber_dupbv( &ctrls[ 0 ]->ldctl_value, &assertedID );
1347 /* needs the dn: prefix */
1349 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1350 ctrls[ 0 ]->ldctl_value.bv_val = ch_malloc( ctrls[ 0 ]->ldctl_value.bv_len + 1 );
1351 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1352 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1353 assertedID.bv_val, assertedID.bv_len + 1 );
1357 if ( op->o_ctrls ) {
1358 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1359 ctrls[ i + 1 ] = op->o_ctrls[ i ];
1362 ctrls[ i + 1 ] = NULL;
1365 if ( ctrls == NULL ) {
1366 ctrls = op->o_ctrls;
1375 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1377 LDAPControl **ctrls = *pctrls;
1379 /* we assume that the first control is the proxyAuthz
1380 * added by back-ldap, so it's the only one we explicitly
1382 if ( ctrls && ctrls != op->o_ctrls ) {
1383 assert( ctrls[ 0 ] != NULL );
1385 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1386 free( ctrls[ 0 ]->ldctl_value.bv_val );