1 /* bind.c - ldap backend bind function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2006 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_BIND_SERR );
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 );
304 rc = LDAP_UNAVAILABLE;
306 } else if ( rc == 0 ) {
307 if ( retries != LDAP_BACK_RETRY_NEVER ) {
308 ldap_pvt_thread_yield();
312 LDAP_BACK_TV_SET( &tv );
315 rc = LDAP_UNAVAILABLE;
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 ) {
339 rc = LDAP_UNWILLING_TO_PERFORM;
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.
419 /* assume it's an internal op; set to LDAPv3 */
420 vers = LDAP_VERSION3;
422 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&vers );
424 /* automatically chase referrals ("[dont-]chase-referrals" statement) */
425 if ( LDAP_BACK_CHASE_REFERRALS( li ) ) {
426 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
430 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
431 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
432 if ( rs->sr_err != LDAP_SUCCESS ) {
433 ldap_unbind_ext( ld, NULL, NULL );
436 #endif /* HAVE_TLS */
438 if ( *lcp == NULL ) {
439 *lcp = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
440 (*lcp)->lc_flags= li->li_flags;
443 (*lcp)->lc_refcnt = 1;
446 LDAP_BACK_CONN_ISTLS_SET( *lcp );
448 LDAP_BACK_CONN_ISTLS_CLEAR( *lcp );
450 #endif /* HAVE_TLS */
453 if ( rs->sr_err != LDAP_SUCCESS ) {
454 rs->sr_err = slap_map_api2result( rs );
455 if ( sendok & LDAP_BACK_SENDERR ) {
456 if ( rs->sr_text == NULL ) {
457 rs->sr_text = "ldap_initialize() failed";
459 send_ldap_result( op, rs );
468 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
470 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
471 ldapconn_t *lc = NULL,
475 /* Internal searches are privileged and shared. So is root. */
476 if ( op->o_do_not_cache || be_isroot( op ) ) {
477 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
478 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
479 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
482 lc_curr.lc_local_ndn = op->o_ndn;
483 /* Explicit binds must not be shared */
484 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
485 lc_curr.lc_conn = op->o_conn;
488 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
492 /* Explicit Bind requests always get their own conn */
493 if ( !( sendok & LDAP_BACK_BINDING ) ) {
494 /* Searches for a ldapconn in the avl tree */
496 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
498 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
499 (caddr_t)&lc_curr, ldap_back_conn_cmp );
501 /* Don't reuse connections while they're still binding */
502 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
503 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
504 ldap_pvt_thread_yield();
507 refcnt = ++lc->lc_refcnt;
509 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
512 /* Looks like we didn't get a bind. Open a new session... */
514 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
517 if ( sendok & LDAP_BACK_BINDING ) {
518 LDAP_BACK_CONN_BINDING_SET( lc );
520 lc->lc_conn = lc_curr.lc_conn;
521 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
523 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
524 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
525 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
526 LDAP_BACK_CONN_ISPRIV_SET( lc );
529 BER_BVZERO( &lc->lc_cred );
530 BER_BVZERO( &lc->lc_bound_ndn );
532 /* FIXME: if we set lc_bound_ndn = o_ndn
533 * we end up with a bind with DN but no password! */
534 if ( !BER_BVISEMPTY( &op->o_ndn )
535 && SLAP_IS_AUTHZ_BACKEND( op ) )
537 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
543 /* if start TLS failed but it was not mandatory,
544 * check if the non-TLS connection was already
545 * in cache; in case, destroy the newly created
546 * connection and use the existing one */
547 if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
548 && !ldap_tls_inplace( lc->lc_ld ) )
552 lc_curr.lc_conn = LDAP_BACK_PCONN;
553 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
554 tmplc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
555 (caddr_t)&lc_curr, ldap_back_conn_cmp );
556 if ( tmplc != NULL ) {
557 refcnt = ++tmplc->lc_refcnt;
558 ldap_back_conn_free( lc );
561 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
563 if ( tmplc != NULL ) {
567 #endif /* HAVE_TLS */
569 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
571 /* Inserts the newly created ldapconn in the avl tree */
572 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
574 assert( lc->lc_refcnt == 1 );
575 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
576 ldap_back_conn_cmp, ldap_back_conn_dup );
578 #if PRINT_CONNTREE > 0
579 myprint( li->li_conninfo.lai_tree );
580 #endif /* PRINT_CONNTREE */
582 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
584 Debug( LDAP_DEBUG_TRACE,
585 "=>ldap_back_getconn: conn %p inserted (refcnt=%u)\n",
586 (void *)lc, refcnt, 0 );
588 /* Err could be -1 in case a duplicate ldapconn is inserted */
589 if ( rs->sr_err != 0 ) {
590 ldap_back_conn_free( lc );
591 rs->sr_err = LDAP_OTHER;
592 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
593 send_ldap_error( op, rs, LDAP_OTHER,
594 "internal server error" );
600 if ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout ) {
601 /* in case of failure, it frees/taints lc and sets it to NULL */
602 if ( ldap_back_retry( &lc, op, rs, sendok ) ) {
608 Debug( LDAP_DEBUG_TRACE,
609 "=>ldap_back_getconn: conn %p fetched (refcnt=%u)\n",
610 (void *)lc, refcnt, 0 );
614 if ( li->li_idle_timeout && lc ) {
615 lc->lc_time = op->o_time;
623 ldap_back_release_conn(
628 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
630 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
631 assert( lc->lc_refcnt > 0 );
633 LDAP_BACK_CONN_BINDING_CLEAR( lc );
634 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
640 * Note: as the check for the value of lc->lc_bound was already here, I removed
641 * it from all the callers, and I made the function return the flag, so
642 * it can be used to simplify the check.
644 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
647 ldap_back_dobind_int(
651 ldap_back_send_t sendok,
655 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
657 int rc = LDAP_BACK_CONN_ISBOUND( lc );
660 assert( retries >= 0 );
666 while ( lc->lc_refcnt > 1 ) {
667 ldap_pvt_thread_yield();
668 if (( rc = LDAP_BACK_CONN_ISBOUND( lc )))
673 * FIXME: we need to let clients use proxyAuthz
674 * otherwise we cannot do symmetric pools of servers;
675 * we have to live with the fact that a user can
676 * authorize itself as any ID that is allowed
677 * by the authzTo directive of the "proxyauthzdn".
680 * NOTE: current Proxy Authorization specification
681 * and implementation do not allow proxy authorization
682 * control to be provided with Bind requests
685 * if no bind took place yet, but the connection is bound
686 * and the "idassert-authcDN" (or other ID) is set,
687 * then bind as the asserting identity and explicitly
688 * add the proxyAuthz control to every operation with the
689 * dn bound to the connection as control value.
690 * This is done also if this is the authrizing backend,
691 * but the "override" flag is given to idassert.
692 * It allows to use SASL bind and yet proxyAuthz users
694 if ( op->o_conn != NULL &&
695 !op->o_do_not_cache &&
696 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
697 ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
699 (void)ldap_back_proxy_authz_bind( lc, op, rs );
703 #ifdef HAVE_CYRUS_SASL
704 if ( LDAP_BACK_CONN_ISPRIV( lc )
705 && li->li_acl_authmethod == LDAP_AUTH_SASL )
707 void *defaults = NULL;
709 if ( li->li_acl_secprops != NULL ) {
710 rc = ldap_set_option( lc->lc_ld,
711 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops);
713 if ( rc != LDAP_OPT_SUCCESS ) {
714 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
715 "(%s,SECPROPS,\"%s\") failed!\n",
716 li->li_uri, li->li_acl_secprops, 0 );
721 defaults = lutil_sasl_defaults( lc->lc_ld,
722 li->li_acl_sasl_mech.bv_val,
723 li->li_acl_sasl_realm.bv_val,
724 li->li_acl_authcID.bv_val,
725 li->li_acl_passwd.bv_val,
728 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
729 li->li_acl_authcDN.bv_val,
730 li->li_acl_sasl_mech.bv_val, NULL, NULL,
731 LDAP_SASL_QUIET, lutil_sasl_interact,
734 lutil_sasl_freedefs( defaults );
736 rs->sr_err = slap_map_api2result( rs );
737 if ( rs->sr_err != LDAP_SUCCESS ) {
738 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
739 send_ldap_result( op, rs );
742 LDAP_BACK_CONN_ISBOUND_SET( lc );
746 #endif /* HAVE_CYRUS_SASL */
749 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
750 lc->lc_bound_ndn.bv_val,
751 LDAP_SASL_SIMPLE, &lc->lc_cred,
752 NULL, NULL, &msgid );
754 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
755 if ( retries != LDAP_BACK_RETRY_NEVER ) {
757 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
760 assert( lc->lc_refcnt > 0 );
761 if ( lc->lc_refcnt == 1 ) {
762 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
765 /* lc here must be the regular lc, reset and ready for init */
766 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
769 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
771 if ( rs->sr_err == LDAP_SUCCESS ) {
779 ldap_back_freeconn( op, lc, dolock );
780 rs->sr_err = slap_map_api2result( rs );
785 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
786 if ( rc == LDAP_SUCCESS ) {
787 LDAP_BACK_CONN_ISBOUND_SET( lc );
790 ldap_back_release_conn( op, rs, lc );
794 rc = LDAP_BACK_CONN_ISBOUND( lc );
800 ldap_back_dobind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
802 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
804 return ldap_back_dobind_int( lc, op, rs, sendok, li->li_nretries, 1 );
808 * ldap_back_default_rebind
810 * This is a callback used for chasing referrals using the same
811 * credentials as the original user on this session.
814 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
815 ber_int_t msgid, void *params )
817 ldapconn_t *lc = (ldapconn_t *)params;
820 /* ... otherwise we couldn't get here */
821 assert( lc != NULL );
823 if ( !ldap_tls_inplace( ld ) ) {
824 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
826 const char *text = NULL;
828 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
829 LDAP_BACK_RETRY_DEFAULT, &text );
830 if ( rc != LDAP_SUCCESS ) {
834 #endif /* HAVE_TLS */
836 /* FIXME: add checks on the URL/identity? */
838 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
839 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
849 ldap_back_send_t sendok )
852 LDAPMessage *res = NULL;
855 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
858 rs->sr_matched = NULL;
860 /* if the error recorded in the reply corresponds
861 * to a successful state, get the error from the
862 * remote server response */
863 if ( ERR_OK( rs->sr_err ) ) {
872 LDAP_BACK_TV_SET( &tv );
876 /* if result parsing fails, note the failure reason */
877 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
881 (void)ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
882 rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
883 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
884 rs->sr_text = "Operation timed out";
888 LDAP_BACK_TV_SET( &tv );
889 ldap_pvt_thread_yield();
893 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
898 /* otherwise get the result; if it is not
899 * LDAP_SUCCESS, record it in the reply
900 * structure (this includes
901 * LDAP_COMPARE_{TRUE|FALSE}) */
903 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
904 &match, &text, NULL, NULL, 1 );
905 #ifndef LDAP_NULL_IS_NULL
906 if ( match != NULL && match[ 0 ] == '\0' ) {
907 ldap_memfree( match );
910 if ( text != NULL && text[ 0 ] == '\0' ) {
911 ldap_memfree( text );
914 #endif /* LDAP_NULL_IS_NULL */
916 if ( rc != LDAP_SUCCESS ) {
922 /* if the error in the reply structure is not
923 * LDAP_SUCCESS, try to map it from client
925 if ( !ERR_OK( rs->sr_err ) ) {
926 rs->sr_err = slap_map_api2result( rs );
928 /* internal ops ( op->o_conn == NULL )
929 * must not reply to client */
930 if ( op->o_conn && !op->o_do_not_cache && match ) {
932 /* record the (massaged) matched
933 * DN into the reply structure */
934 rs->sr_matched = match;
938 ( ( sendok & LDAP_BACK_SENDOK )
939 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
941 send_ldap_result( op, rs );
944 if ( rs->sr_matched != match ) {
945 free( (char *)rs->sr_matched );
947 rs->sr_matched = NULL;
948 ldap_memfree( match );
951 ldap_memfree( text );
954 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
957 /* return true if bound, false if failed */
959 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
962 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
964 assert( lcp != NULL );
965 assert( *lcp != NULL );
967 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
969 if ( (*lcp)->lc_refcnt == 1 ) {
970 Debug( LDAP_DEBUG_ANY,
971 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
972 op->o_log_prefix, li->li_uri,
973 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
974 "" : (*lcp)->lc_bound_ndn.bv_val );
976 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
977 (*lcp)->lc_ld = NULL;
978 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
980 /* lc here must be the regular lc, reset and ready for init */
981 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
982 if ( rc != LDAP_SUCCESS ) {
987 rc = ldap_back_dobind_int( *lcp, op, rs, sendok, 0, 0 );
994 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1000 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs )
1002 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1003 struct berval binddn = slap_empty_bv;
1004 struct berval bindcred = slap_empty_bv;
1010 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1011 ndn = op->o_conn->c_ndn;
1018 * FIXME: we need to let clients use proxyAuthz
1019 * otherwise we cannot do symmetric pools of servers;
1020 * we have to live with the fact that a user can
1021 * authorize itself as any ID that is allowed
1022 * by the authzTo directive of the "proxyauthzdn".
1025 * NOTE: current Proxy Authorization specification
1026 * and implementation do not allow proxy authorization
1027 * control to be provided with Bind requests
1030 * if no bind took place yet, but the connection is bound
1031 * and the "proxyauthzdn" is set, then bind as
1032 * "proxyauthzdn" and explicitly add the proxyAuthz
1033 * control to every operation with the dn bound
1034 * to the connection as control value.
1037 /* bind as proxyauthzdn only if no idassert mode
1038 * is requested, or if the client's identity
1040 switch ( li->li_idassert_mode ) {
1041 case LDAP_BACK_IDASSERT_LEGACY:
1042 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1043 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1045 binddn = li->li_idassert_authcDN;
1046 bindcred = li->li_idassert_passwd;
1053 /* NOTE: rootdn can always idassert */
1054 if ( BER_BVISNULL( &ndn ) && li->li_idassert_authz == NULL ) {
1055 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1056 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1057 send_ldap_result( op, rs );
1058 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1061 rs->sr_err = LDAP_SUCCESS;
1062 binddn = slap_empty_bv;
1063 bindcred = slap_empty_bv;
1069 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1070 struct berval authcDN;
1072 if ( BER_BVISNULL( &ndn ) ) {
1073 authcDN = slap_empty_bv;
1078 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1079 &authcDN, &authcDN );
1080 if ( rs->sr_err != LDAP_SUCCESS ) {
1081 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1082 send_ldap_result( op, rs );
1083 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1086 rs->sr_err = LDAP_SUCCESS;
1087 binddn = slap_empty_bv;
1088 bindcred = slap_empty_bv;
1096 binddn = li->li_idassert_authcDN;
1097 bindcred = li->li_idassert_passwd;
1102 if ( dobind && li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1103 #ifdef HAVE_CYRUS_SASL
1104 void *defaults = NULL;
1105 struct berval authzID = BER_BVNULL;
1108 /* if SASL supports native authz, prepare for it */
1109 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1110 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1112 switch ( li->li_idassert_mode ) {
1113 case LDAP_BACK_IDASSERT_OTHERID:
1114 case LDAP_BACK_IDASSERT_OTHERDN:
1115 authzID = li->li_idassert_authzID;
1118 case LDAP_BACK_IDASSERT_ANONYMOUS:
1119 BER_BVSTR( &authzID, "dn:" );
1122 case LDAP_BACK_IDASSERT_SELF:
1123 if ( BER_BVISNULL( &ndn ) ) {
1124 /* connection is not authc'd, so don't idassert */
1125 BER_BVSTR( &authzID, "dn:" );
1128 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1129 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1130 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1131 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1132 ndn.bv_val, ndn.bv_len + 1 );
1141 if ( li->li_idassert_secprops != NULL ) {
1142 rs->sr_err = ldap_set_option( lc->lc_ld,
1143 LDAP_OPT_X_SASL_SECPROPS,
1144 (void *)li->li_idassert_secprops );
1146 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1147 send_ldap_result( op, rs );
1148 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1153 defaults = lutil_sasl_defaults( lc->lc_ld,
1154 li->li_idassert_sasl_mech.bv_val,
1155 li->li_idassert_sasl_realm.bv_val,
1156 li->li_idassert_authcID.bv_val,
1157 li->li_idassert_passwd.bv_val,
1160 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1161 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1162 LDAP_SASL_QUIET, lutil_sasl_interact,
1165 rs->sr_err = slap_map_api2result( rs );
1166 if ( rs->sr_err != LDAP_SUCCESS ) {
1167 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1168 send_ldap_result( op, rs );
1171 LDAP_BACK_CONN_ISBOUND_SET( lc );
1174 lutil_sasl_freedefs( defaults );
1176 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1180 #endif /* HAVE_CYRUS_SASL */
1183 switch ( li->li_idassert_authmethod ) {
1184 case LDAP_AUTH_NONE:
1185 LDAP_BACK_CONN_ISBOUND_SET( lc );
1188 case LDAP_AUTH_SIMPLE:
1189 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1190 binddn.bv_val, LDAP_SASL_SIMPLE,
1191 &bindcred, NULL, NULL, &msgid );
1196 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1197 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1198 send_ldap_result( op, rs );
1202 rc = ldap_back_op_result( lc, op, rs, msgid, 0, LDAP_BACK_SENDERR );
1203 if ( rc == LDAP_SUCCESS ) {
1204 LDAP_BACK_CONN_ISBOUND_SET( lc );
1207 return LDAP_BACK_CONN_ISBOUND( lc );
1211 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1212 * to existing server-side controls if required; if not,
1213 * the existing server-side controls are placed in *pctrls.
1214 * The caller, after using the controls in client API
1215 * operations, if ( *pctrls != op->o_ctrls ), should
1216 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1217 * The function returns success if the control could
1218 * be added if required, or if it did nothing; in the future,
1219 * it might return some error if it failed.
1221 * if no bind took place yet, but the connection is bound
1222 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1223 * and explicitly add proxyAuthz the control to every operation
1224 * with the dn bound to the connection as control value.
1226 * If no server-side controls are defined for the operation,
1227 * simply add the proxyAuthz control; otherwise, if the
1228 * proxyAuthz control is not already set, add it as
1231 * FIXME: is controls order significant for security?
1232 * ANSWER: controls ordering and interoperability
1233 * must be indicated by the specs of each control; if none
1234 * is specified, the order is irrelevant.
1237 ldap_back_proxy_authz_ctrl(
1241 LDAPControl ***pctrls )
1243 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
1244 LDAPControl **ctrls = NULL;
1247 struct berval assertedID,
1252 rs->sr_err = LDAP_SUCCESS;
1254 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1255 * but if it is not set this test fails. We need a different
1256 * means to detect if idassert is enabled */
1257 if ( ( BER_BVISNULL( &li->li_idassert_authcID ) || BER_BVISEMPTY( &li->li_idassert_authcID ) )
1258 && ( BER_BVISNULL( &li->li_idassert_authcDN ) || BER_BVISEMPTY( &li->li_idassert_authcDN ) ) )
1263 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1267 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1268 ndn = op->o_conn->c_ndn;
1274 if ( li->li_idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1275 if ( op->o_proxy_authz ) {
1277 * FIXME: we do not want to perform proxyAuthz
1278 * on behalf of the client, because this would
1279 * be performed with "proxyauthzdn" privileges.
1281 * This might actually be too strict, since
1282 * the "proxyauthzdn" authzTo, and each entry's
1283 * authzFrom attributes may be crafted
1284 * to avoid unwanted proxyAuthz to take place.
1287 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1288 rs->sr_text = "proxyAuthz not allowed within namingContext";
1293 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1297 if ( BER_BVISNULL( &ndn ) ) {
1301 if ( BER_BVISNULL( &li->li_idassert_authcDN ) ) {
1305 } else if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1306 if ( ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1307 /* && ( !BER_BVISNULL( &ndn )
1308 || LDAP_BACK_CONN_ISBOUND( lc ) ) */ )
1310 /* already asserted in SASL via native authz */
1311 /* NOTE: the test on lc->lc_bound is used to trap
1312 * native authorization of anonymous users,
1313 * since in that case ndn is NULL */
1317 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1319 struct berval authcDN;
1321 if ( BER_BVISNULL( &ndn ) ) {
1322 authcDN = slap_empty_bv;
1326 rc = slap_sasl_matches( op, li->li_idassert_authz,
1327 &authcDN, & authcDN );
1328 if ( rc != LDAP_SUCCESS ) {
1329 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1331 /* ndn is not authorized
1332 * to use idassert */
1339 if ( op->o_proxy_authz ) {
1342 * 1) ignore the already set proxyAuthz control
1343 * 2) leave it in place, and don't set ours
1345 * 4) reject the operation
1347 * option (4) is very drastic
1348 * option (3) will make the remote server reject
1349 * the operation, thus being equivalent to (4)
1350 * option (2) will likely break the idassert
1351 * assumptions, so we cannot accept it;
1352 * option (1) means that we are contradicting
1353 * the client's reques.
1355 * I think (4) is the only correct choice.
1357 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1358 rs->sr_text = "proxyAuthz not allowed within namingContext";
1361 if ( op->o_is_auth_check ) {
1362 mode = LDAP_BACK_IDASSERT_NOASSERT;
1365 mode = li->li_idassert_mode;
1369 case LDAP_BACK_IDASSERT_LEGACY:
1370 case LDAP_BACK_IDASSERT_SELF:
1371 /* original behavior:
1372 * assert the client's identity */
1373 if ( BER_BVISNULL( &ndn ) ) {
1374 assertedID = slap_empty_bv;
1380 case LDAP_BACK_IDASSERT_ANONYMOUS:
1381 /* assert "anonymous" */
1382 assertedID = slap_empty_bv;
1385 case LDAP_BACK_IDASSERT_NOASSERT:
1386 /* don't assert; bind as proxyauthzdn */
1389 case LDAP_BACK_IDASSERT_OTHERID:
1390 case LDAP_BACK_IDASSERT_OTHERDN:
1391 /* assert idassert DN */
1392 assertedID = li->li_idassert_authzID;
1399 if ( BER_BVISNULL( &assertedID ) ) {
1400 assertedID = slap_empty_bv;
1403 if ( op->o_ctrls ) {
1404 for ( i = 0; op->o_ctrls[ i ]; i++ )
1405 /* just count ctrls */ ;
1408 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
1410 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
1412 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1413 ctrls[ 0 ]->ldctl_iscritical = 1;
1415 switch ( li->li_idassert_mode ) {
1416 /* already in u:ID or dn:DN form */
1417 case LDAP_BACK_IDASSERT_OTHERID:
1418 case LDAP_BACK_IDASSERT_OTHERDN:
1419 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
1422 /* needs the dn: prefix */
1424 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1425 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
1427 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1428 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1429 assertedID.bv_val, assertedID.bv_len + 1 );
1433 if ( op->o_ctrls ) {
1434 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1435 ctrls[ i + 1 ] = op->o_ctrls[ i ];
1438 ctrls[ i + 1 ] = NULL;
1441 if ( ctrls == NULL ) {
1442 ctrls = op->o_ctrls;
1451 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1453 LDAPControl **ctrls = *pctrls;
1455 /* we assume that the first control is the proxyAuthz
1456 * added by back-ldap, so it's the only one we explicitly
1458 if ( ctrls && ctrls != op->o_ctrls ) {
1459 assert( ctrls[ 0 ] != NULL );
1461 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1462 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1465 op->o_tmpfree( ctrls, op->o_tmpmemctx );