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 #ifndef PRINT_CONNTREE
39 #define PRINT_CONNTREE 0
40 #endif /* !PRINT_CONNTREE */
42 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ "2.16.840.1.113730.3.4.12"
44 static LDAP_REBIND_PROC ldap_back_default_rebind;
46 LDAP_REBIND_PROC *ldap_back_rebind_f = ldap_back_default_rebind;
49 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
52 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
55 ldap_back_bind( Operation *op, SlapReply *rs )
57 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
63 lc = ldap_back_getconn( op, rs, LDAP_BACK_BIND_SERR );
68 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
69 ch_free( lc->lc_bound_ndn.bv_val );
70 BER_BVZERO( &lc->lc_bound_ndn );
72 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
74 /* method is always LDAP_AUTH_SIMPLE if we got here */
75 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
77 &op->orb_cred, op->o_ctrls, NULL, &msgid );
78 rc = ldap_back_op_result( lc, op, rs, msgid, 0, LDAP_BACK_SENDERR );
80 if ( rc == LDAP_SUCCESS ) {
81 /* If defined, proxyAuthz will be used also when
82 * back-ldap is the authorizing backend; for this
83 * purpose, a successful bind is followed by a
84 * bind with the configured identity assertion */
85 /* NOTE: use with care */
86 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
87 ldap_back_proxy_authz_bind( lc, op, rs, LDAP_BACK_SENDERR );
88 if ( !LDAP_BACK_CONN_ISBOUND( lc ) ) {
94 LDAP_BACK_CONN_ISBOUND_SET( lc );
95 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
97 if ( LDAP_BACK_SAVECRED( li ) ) {
98 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
99 memset( lc->lc_cred.bv_val, 0,
100 lc->lc_cred.bv_len );
102 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
103 ldap_set_rebind_proc( lc->lc_ld, ldap_back_rebind_f, lc );
108 /* must re-insert if local DN changed as result of bind */
109 if ( LDAP_BACK_CONN_ISBOUND( lc )
110 && !dn_match( &op->o_req_ndn, &lc->lc_local_ndn ) )
114 /* wait for all other ops to release the connection */
116 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
117 if ( lc->lc_refcnt > 1 ) {
118 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
119 ldap_pvt_thread_yield();
123 assert( lc->lc_refcnt == 1 );
124 lc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
125 ldap_back_conn_cmp );
126 assert( lc != NULL );
128 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
129 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
130 ldap_back_conn_cmp, ldap_back_conn_dup );
131 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
133 /* we can do this because lc_refcnt == 1 */
134 ldap_back_conn_free( lc );
140 ldap_back_release_conn( op, rs, lc );
149 * compares two ldapconn_t based on the value of the conn pointer;
153 ldap_back_conn_cmp( const void *c1, const void *c2 )
155 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
156 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
159 /* If local DNs don't match, it is definitely not a match */
160 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
165 /* For shared sessions, conn is NULL. Only explicitly
166 * bound sessions will have non-NULL conn.
168 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
174 * returns -1 in case a duplicate ldapconn_t has been inserted;
178 ldap_back_conn_dup( void *c1, void *c2 )
180 ldapconn_t *lc1 = (ldapconn_t *)c1;
181 ldapconn_t *lc2 = (ldapconn_t *)c2;
183 /* Cannot have more than one shared session with same DN */
184 if ( dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) &&
185 lc1->lc_conn == lc2->lc_conn )
193 #if PRINT_CONNTREE > 0
195 ravl_print( Avlnode *root, int depth )
204 ravl_print( root->avl_right, depth+1 );
206 for ( i = 0; i < depth; i++ ) {
211 printf( "lc(%lx) local(%s) conn(%lx) %s\n",
212 lc, lc->lc_local_ndn.bv_val, lc->lc_conn,
213 avl_bf2str( root->avl_bf) );
215 ravl_print( root->avl_left, depth+1 );
219 myprint( Avlnode *root )
221 printf( "********\n" );
224 printf( "\tNULL\n" );
227 ravl_print( root, 0 );
230 printf( "********\n" );
232 #endif /* PRINT_CONNTREE */
235 ldap_back_freeconn( Operation *op, ldapconn_t *lc, int dolock )
237 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
240 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
243 assert( lc->lc_refcnt > 0 );
244 if ( --lc->lc_refcnt == 0 ) {
245 lc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
246 ldap_back_conn_cmp );
247 assert( lc != NULL );
249 ldap_back_conn_free( (void *)lc );
253 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
270 int rc = LDAP_SUCCESS;
273 /* this is ridiculous... */
274 dummy.li_flags = flags;
276 /* start TLS ("tls-[try-]{start,propagate}" statements) */
277 if ( ( LDAP_BACK_USE_TLS( &dummy ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS( &dummy ) ) )
278 && !ldap_is_ldaps_url( url ) )
280 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
282 * use asynchronous StartTLS
283 * in case, chase referral (not implemented yet)
287 if ( protocol == 0 ) {
288 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
292 if ( protocol < LDAP_VERSION3 ) {
293 protocol = LDAP_VERSION3;
294 /* Set LDAP version */
295 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
296 (const void *)&protocol );
299 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
300 if ( rc == LDAP_SUCCESS ) {
301 LDAPMessage *res = NULL;
304 LDAP_BACK_TV_SET( &tv );
307 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
309 rc = LDAP_UNAVAILABLE;
311 } else if ( rc == 0 ) {
312 if ( retries != LDAP_BACK_RETRY_NEVER ) {
313 ldap_pvt_thread_yield();
317 LDAP_BACK_TV_SET( &tv );
320 rc = LDAP_UNAVAILABLE;
322 } else if ( rc == LDAP_RES_EXTENDED ) {
323 struct berval *data = NULL;
325 rc = ldap_parse_extended_result( ld, res,
327 if ( rc == LDAP_SUCCESS ) {
329 rc = ldap_parse_result( ld, res, &err,
330 NULL, NULL, NULL, NULL, 1 );
331 if ( rc == LDAP_SUCCESS ) {
336 /* FIXME: in case a referral
337 * is returned, should we try
338 * using it instead of the
340 if ( rc == LDAP_SUCCESS ) {
341 rc = ldap_install_tls( ld );
343 } else if ( rc == LDAP_REFERRAL ) {
344 rc = LDAP_UNWILLING_TO_PERFORM;
345 *text = "unwilling to chase referral returned by Start TLS exop";
349 if ( data->bv_val ) {
350 ber_memfree( data->bv_val );
364 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
366 * use synchronous StartTLS
368 rc = ldap_start_tls_s( ld, NULL, NULL );
369 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
371 /* if StartTLS is requested, only attempt it if the URL
372 * is not "ldaps://"; this may occur not only in case
373 * of misconfiguration, but also when used in the chain
374 * overlay, where the "uri" can be parsed out of a referral */
380 case LDAP_SERVER_DOWN:
384 if ( LDAP_BACK_TLS_CRITICAL( &dummy ) ) {
385 *text = "could not start TLS";
389 /* in case Start TLS is not critical */
401 #endif /* HAVE_TLS */
404 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
406 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
407 int vers = op->o_protocol;
410 int is_tls = op->o_conn->c_is_tls;
411 #endif /* HAVE_TLS */
413 assert( lcp != NULL );
415 rs->sr_err = ldap_initialize( &ld, li->li_uri );
416 if ( rs->sr_err != LDAP_SUCCESS ) {
420 /* Set LDAP version. This will always succeed: If the client
421 * bound with a particular version, then so can we.
424 /* assume it's an internal op; set to LDAPv3 */
425 vers = LDAP_VERSION3;
427 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&vers );
429 /* automatically chase referrals ("[dont-]chase-referrals" statement) */
430 if ( LDAP_BACK_CHASE_REFERRALS( li ) ) {
431 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
435 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
436 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
437 if ( rs->sr_err != LDAP_SUCCESS ) {
438 ldap_unbind_ext( ld, NULL, NULL );
441 #endif /* HAVE_TLS */
443 if ( *lcp == NULL ) {
444 *lcp = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
445 (*lcp)->lc_flags= li->li_flags;
448 (*lcp)->lc_refcnt = 1;
451 LDAP_BACK_CONN_ISTLS_SET( *lcp );
453 LDAP_BACK_CONN_ISTLS_CLEAR( *lcp );
455 #endif /* HAVE_TLS */
458 if ( rs->sr_err != LDAP_SUCCESS ) {
459 rs->sr_err = slap_map_api2result( rs );
460 if ( sendok & LDAP_BACK_SENDERR ) {
461 if ( rs->sr_text == NULL ) {
462 rs->sr_text = "ldap_initialize() failed";
464 send_ldap_result( op, rs );
469 if ( li->li_conn_ttl > 0 ) {
470 (*lcp)->lc_create_time = op->o_time;
478 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
480 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
481 ldapconn_t *lc = NULL,
485 /* Internal searches are privileged and shared. So is root. */
486 if ( op->o_do_not_cache || be_isroot( op ) ) {
487 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
488 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
489 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
492 lc_curr.lc_local_ndn = op->o_ndn;
493 /* Explicit binds must not be shared */
494 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
495 lc_curr.lc_conn = op->o_conn;
498 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
502 /* Explicit Bind requests always get their own conn */
503 if ( !( sendok & LDAP_BACK_BINDING ) ) {
504 /* Searches for a ldapconn in the avl tree */
506 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
508 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
509 (caddr_t)&lc_curr, ldap_back_conn_cmp );
511 /* Don't reuse connections while they're still binding */
512 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
513 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
514 ldap_pvt_thread_yield();
517 refcnt = ++lc->lc_refcnt;
519 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
522 /* Looks like we didn't get a bind. Open a new session... */
524 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
527 if ( sendok & LDAP_BACK_BINDING ) {
528 LDAP_BACK_CONN_BINDING_SET( lc );
530 lc->lc_conn = lc_curr.lc_conn;
531 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
533 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
534 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
535 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
536 LDAP_BACK_CONN_ISPRIV_SET( lc );
539 BER_BVZERO( &lc->lc_cred );
540 BER_BVZERO( &lc->lc_bound_ndn );
542 /* FIXME: if we set lc_bound_ndn = o_ndn
543 * we end up with a bind with DN but no password! */
544 if ( !BER_BVISEMPTY( &op->o_ndn )
545 && SLAP_IS_AUTHZ_BACKEND( op ) )
547 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
553 /* if start TLS failed but it was not mandatory,
554 * check if the non-TLS connection was already
555 * in cache; in case, destroy the newly created
556 * connection and use the existing one */
557 if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
558 && !ldap_tls_inplace( lc->lc_ld ) )
562 lc_curr.lc_conn = LDAP_BACK_PCONN;
563 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
564 tmplc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
565 (caddr_t)&lc_curr, ldap_back_conn_cmp );
566 if ( tmplc != NULL ) {
567 refcnt = ++tmplc->lc_refcnt;
568 ldap_back_conn_free( lc );
571 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
573 if ( tmplc != NULL ) {
577 #endif /* HAVE_TLS */
579 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
581 /* Inserts the newly created ldapconn in the avl tree */
582 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
584 assert( lc->lc_refcnt == 1 );
585 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
586 ldap_back_conn_cmp, ldap_back_conn_dup );
588 #if PRINT_CONNTREE > 0
589 myprint( li->li_conninfo.lai_tree );
590 #endif /* PRINT_CONNTREE */
592 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
594 Debug( LDAP_DEBUG_TRACE,
595 "=>ldap_back_getconn: conn %p inserted (refcnt=%u)\n",
596 (void *)lc, refcnt, 0 );
598 /* Err could be -1 in case a duplicate ldapconn is inserted */
599 if ( rs->sr_err != 0 ) {
600 ldap_back_conn_free( lc );
601 rs->sr_err = LDAP_OTHER;
602 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
603 send_ldap_error( op, rs, LDAP_OTHER,
604 "internal server error" );
610 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
611 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
613 /* in case of failure, it frees/taints lc and sets it to NULL */
614 if ( !ldap_back_retry( &lc, op, rs, sendok ) ) {
620 Debug( LDAP_DEBUG_TRACE,
621 "=>ldap_back_getconn: conn %p fetched (refcnt=%u)\n",
622 (void *)lc, refcnt, 0 );
626 if ( li->li_idle_timeout && lc ) {
627 lc->lc_time = op->o_time;
635 ldap_back_release_conn_lock(
641 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
644 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
646 assert( lc->lc_refcnt > 0 );
648 LDAP_BACK_CONN_BINDING_CLEAR( lc );
650 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
657 * Note: as the check for the value of lc->lc_bound was already here, I removed
658 * it from all the callers, and I made the function return the flag, so
659 * it can be used to simplify the check.
661 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
664 ldap_back_dobind_int(
668 ldap_back_send_t sendok,
672 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
674 int rc = LDAP_BACK_CONN_ISBOUND( lc );
677 assert( retries >= 0 );
683 while ( lc->lc_refcnt > 1 ) {
684 ldap_pvt_thread_yield();
685 rc = LDAP_BACK_CONN_ISBOUND( lc );
692 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
694 LDAP_BACK_CONN_BINDING_SET( lc );
696 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
700 * FIXME: we need to let clients use proxyAuthz
701 * otherwise we cannot do symmetric pools of servers;
702 * we have to live with the fact that a user can
703 * authorize itself as any ID that is allowed
704 * by the authzTo directive of the "proxyauthzdn".
707 * NOTE: current Proxy Authorization specification
708 * and implementation do not allow proxy authorization
709 * control to be provided with Bind requests
712 * if no bind took place yet, but the connection is bound
713 * and the "idassert-authcDN" (or other ID) is set,
714 * then bind as the asserting identity and explicitly
715 * add the proxyAuthz control to every operation with the
716 * dn bound to the connection as control value.
717 * This is done also if this is the authrizing backend,
718 * but the "override" flag is given to idassert.
719 * It allows to use SASL bind and yet proxyAuthz users
721 if ( op->o_conn != NULL &&
722 !op->o_do_not_cache &&
723 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
724 ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
726 (void)ldap_back_proxy_authz_bind( lc, op, rs, sendok );
730 #ifdef HAVE_CYRUS_SASL
731 if ( LDAP_BACK_CONN_ISPRIV( lc )
732 && li->li_acl_authmethod == LDAP_AUTH_SASL )
734 void *defaults = NULL;
736 if ( li->li_acl_secprops != NULL ) {
737 rc = ldap_set_option( lc->lc_ld,
738 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops);
740 if ( rc != LDAP_OPT_SUCCESS ) {
741 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
742 "(%s,SECPROPS,\"%s\") failed!\n",
743 li->li_uri, li->li_acl_secprops, 0 );
748 defaults = lutil_sasl_defaults( lc->lc_ld,
749 li->li_acl_sasl_mech.bv_val,
750 li->li_acl_sasl_realm.bv_val,
751 li->li_acl_authcID.bv_val,
752 li->li_acl_passwd.bv_val,
755 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
756 li->li_acl_authcDN.bv_val,
757 li->li_acl_sasl_mech.bv_val, NULL, NULL,
758 LDAP_SASL_QUIET, lutil_sasl_interact,
761 lutil_sasl_freedefs( defaults );
763 rs->sr_err = slap_map_api2result( rs );
764 if ( rs->sr_err != LDAP_SUCCESS ) {
765 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
766 send_ldap_result( op, rs );
769 LDAP_BACK_CONN_ISBOUND_SET( lc );
773 #endif /* HAVE_CYRUS_SASL */
776 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
777 lc->lc_bound_ndn.bv_val,
778 LDAP_SASL_SIMPLE, &lc->lc_cred,
779 NULL, NULL, &msgid );
781 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
782 if ( retries != LDAP_BACK_RETRY_NEVER ) {
784 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
787 assert( lc->lc_refcnt > 0 );
788 if ( lc->lc_refcnt == 1 ) {
789 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
792 /* lc here must be the regular lc, reset and ready for init */
793 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
796 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
798 if ( rs->sr_err == LDAP_SUCCESS ) {
806 ldap_back_freeconn( op, lc, dolock );
807 rs->sr_err = slap_map_api2result( rs );
812 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
813 if ( rc == LDAP_SUCCESS ) {
814 LDAP_BACK_CONN_ISBOUND_SET( lc );
818 LDAP_BACK_CONN_BINDING_CLEAR( lc );
819 rc = LDAP_BACK_CONN_ISBOUND( lc );
821 ldap_back_release_conn_lock( op, rs, lc, dolock );
828 ldap_back_dobind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
830 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
832 return ldap_back_dobind_int( lc, op, rs, sendok, li->li_nretries, 1 );
836 * ldap_back_default_rebind
838 * This is a callback used for chasing referrals using the same
839 * credentials as the original user on this session.
842 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
843 ber_int_t msgid, void *params )
845 ldapconn_t *lc = (ldapconn_t *)params;
848 /* ... otherwise we couldn't get here */
849 assert( lc != NULL );
851 if ( !ldap_tls_inplace( ld ) ) {
852 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
854 const char *text = NULL;
856 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
857 LDAP_BACK_RETRY_DEFAULT, &text );
858 if ( rc != LDAP_SUCCESS ) {
862 #endif /* HAVE_TLS */
864 /* FIXME: add checks on the URL/identity? */
866 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
867 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
877 ldap_back_send_t sendok )
880 LDAPMessage *res = NULL;
883 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
886 rs->sr_matched = NULL;
888 /* if the error recorded in the reply corresponds
889 * to a successful state, get the error from the
890 * remote server response */
891 if ( ERR_OK( rs->sr_err ) ) {
900 LDAP_BACK_TV_SET( &tv );
904 /* if result parsing fails, note the failure reason */
905 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
909 (void)ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
910 rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
911 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
912 rs->sr_text = "Operation timed out";
916 LDAP_BACK_TV_SET( &tv );
917 ldap_pvt_thread_yield();
921 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
926 /* otherwise get the result; if it is not
927 * LDAP_SUCCESS, record it in the reply
928 * structure (this includes
929 * LDAP_COMPARE_{TRUE|FALSE}) */
931 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
932 &match, &text, NULL, NULL, 1 );
933 #ifndef LDAP_NULL_IS_NULL
934 if ( match != NULL && match[ 0 ] == '\0' ) {
935 ldap_memfree( match );
938 if ( text != NULL && text[ 0 ] == '\0' ) {
939 ldap_memfree( text );
942 #endif /* LDAP_NULL_IS_NULL */
944 if ( rc != LDAP_SUCCESS ) {
950 /* if the error in the reply structure is not
951 * LDAP_SUCCESS, try to map it from client
953 if ( !ERR_OK( rs->sr_err ) ) {
954 rs->sr_err = slap_map_api2result( rs );
956 /* internal ops ( op->o_conn == NULL )
957 * must not reply to client */
958 if ( op->o_conn && !op->o_do_not_cache && match ) {
960 /* record the (massaged) matched
961 * DN into the reply structure */
962 rs->sr_matched = match;
966 ( ( sendok & LDAP_BACK_SENDOK )
967 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
969 send_ldap_result( op, rs );
972 if ( rs->sr_matched != match ) {
973 free( (char *)rs->sr_matched );
975 rs->sr_matched = NULL;
976 ldap_memfree( match );
979 ldap_memfree( text );
982 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
985 /* return true if bound, false if failed */
987 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
990 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
992 assert( lcp != NULL );
993 assert( *lcp != NULL );
995 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
997 if ( (*lcp)->lc_refcnt == 1 ) {
998 Debug( LDAP_DEBUG_ANY,
999 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1000 op->o_log_prefix, li->li_uri,
1001 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1002 "" : (*lcp)->lc_bound_ndn.bv_val );
1004 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1005 (*lcp)->lc_ld = NULL;
1006 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1008 /* lc here must be the regular lc, reset and ready for init */
1009 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
1010 if ( rc != LDAP_SUCCESS ) {
1015 rc = ldap_back_dobind_int( *lcp, op, rs, sendok, 0, 0 );
1022 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1028 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1030 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1031 struct berval binddn = slap_empty_bv;
1032 struct berval bindcred = slap_empty_bv;
1038 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1039 ndn = op->o_conn->c_ndn;
1046 * FIXME: we need to let clients use proxyAuthz
1047 * otherwise we cannot do symmetric pools of servers;
1048 * we have to live with the fact that a user can
1049 * authorize itself as any ID that is allowed
1050 * by the authzTo directive of the "proxyauthzdn".
1053 * NOTE: current Proxy Authorization specification
1054 * and implementation do not allow proxy authorization
1055 * control to be provided with Bind requests
1058 * if no bind took place yet, but the connection is bound
1059 * and the "proxyauthzdn" is set, then bind as
1060 * "proxyauthzdn" and explicitly add the proxyAuthz
1061 * control to every operation with the dn bound
1062 * to the connection as control value.
1065 /* bind as proxyauthzdn only if no idassert mode
1066 * is requested, or if the client's identity
1068 switch ( li->li_idassert_mode ) {
1069 case LDAP_BACK_IDASSERT_LEGACY:
1070 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1071 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1073 binddn = li->li_idassert_authcDN;
1074 bindcred = li->li_idassert_passwd;
1081 /* NOTE: rootdn can always idassert */
1082 if ( BER_BVISNULL( &ndn ) && li->li_idassert_authz == NULL ) {
1083 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1084 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1085 if ( sendok & LDAP_BACK_SENDERR ) {
1086 send_ldap_result( op, rs );
1088 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1091 rs->sr_err = LDAP_SUCCESS;
1092 binddn = slap_empty_bv;
1093 bindcred = slap_empty_bv;
1099 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1100 struct berval authcDN;
1102 if ( BER_BVISNULL( &ndn ) ) {
1103 authcDN = slap_empty_bv;
1108 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1109 &authcDN, &authcDN );
1110 if ( rs->sr_err != LDAP_SUCCESS ) {
1111 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1112 if ( sendok & LDAP_BACK_SENDERR ) {
1113 send_ldap_result( op, rs );
1115 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1118 rs->sr_err = LDAP_SUCCESS;
1119 binddn = slap_empty_bv;
1120 bindcred = slap_empty_bv;
1128 binddn = li->li_idassert_authcDN;
1129 bindcred = li->li_idassert_passwd;
1134 if ( dobind && li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1135 #ifdef HAVE_CYRUS_SASL
1136 void *defaults = NULL;
1137 struct berval authzID = BER_BVNULL;
1140 /* if SASL supports native authz, prepare for it */
1141 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1142 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1144 switch ( li->li_idassert_mode ) {
1145 case LDAP_BACK_IDASSERT_OTHERID:
1146 case LDAP_BACK_IDASSERT_OTHERDN:
1147 authzID = li->li_idassert_authzID;
1150 case LDAP_BACK_IDASSERT_ANONYMOUS:
1151 BER_BVSTR( &authzID, "dn:" );
1154 case LDAP_BACK_IDASSERT_SELF:
1155 if ( BER_BVISNULL( &ndn ) ) {
1156 /* connection is not authc'd, so don't idassert */
1157 BER_BVSTR( &authzID, "dn:" );
1160 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1161 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1162 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1163 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1164 ndn.bv_val, ndn.bv_len + 1 );
1173 if ( li->li_idassert_secprops != NULL ) {
1174 rs->sr_err = ldap_set_option( lc->lc_ld,
1175 LDAP_OPT_X_SASL_SECPROPS,
1176 (void *)li->li_idassert_secprops );
1178 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1179 rs->sr_err = LDAP_OTHER;
1180 if ( sendok & LDAP_BACK_SENDERR ) {
1181 send_ldap_result( op, rs );
1183 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1188 defaults = lutil_sasl_defaults( lc->lc_ld,
1189 li->li_idassert_sasl_mech.bv_val,
1190 li->li_idassert_sasl_realm.bv_val,
1191 li->li_idassert_authcID.bv_val,
1192 li->li_idassert_passwd.bv_val,
1195 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1196 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1197 LDAP_SASL_QUIET, lutil_sasl_interact,
1200 rs->sr_err = slap_map_api2result( rs );
1201 if ( rs->sr_err != LDAP_SUCCESS ) {
1202 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1203 if ( sendok & LDAP_BACK_SENDERR ) {
1204 send_ldap_result( op, rs );
1208 LDAP_BACK_CONN_ISBOUND_SET( lc );
1211 lutil_sasl_freedefs( defaults );
1213 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1217 #endif /* HAVE_CYRUS_SASL */
1220 switch ( li->li_idassert_authmethod ) {
1221 case LDAP_AUTH_NONE:
1222 LDAP_BACK_CONN_ISBOUND_SET( lc );
1225 case LDAP_AUTH_SIMPLE:
1226 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1227 binddn.bv_val, LDAP_SASL_SIMPLE,
1228 &bindcred, NULL, NULL, &msgid );
1233 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1234 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1235 if ( sendok & LDAP_BACK_SENDERR ) {
1236 send_ldap_result( op, rs );
1241 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
1242 if ( rc == LDAP_SUCCESS ) {
1243 LDAP_BACK_CONN_ISBOUND_SET( lc );
1246 return LDAP_BACK_CONN_ISBOUND( lc );
1250 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1251 * to existing server-side controls if required; if not,
1252 * the existing server-side controls are placed in *pctrls.
1253 * The caller, after using the controls in client API
1254 * operations, if ( *pctrls != op->o_ctrls ), should
1255 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1256 * The function returns success if the control could
1257 * be added if required, or if it did nothing; in the future,
1258 * it might return some error if it failed.
1260 * if no bind took place yet, but the connection is bound
1261 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1262 * and explicitly add proxyAuthz the control to every operation
1263 * with the dn bound to the connection as control value.
1265 * If no server-side controls are defined for the operation,
1266 * simply add the proxyAuthz control; otherwise, if the
1267 * proxyAuthz control is not already set, add it as
1270 * FIXME: is controls order significant for security?
1271 * ANSWER: controls ordering and interoperability
1272 * must be indicated by the specs of each control; if none
1273 * is specified, the order is irrelevant.
1276 ldap_back_proxy_authz_ctrl(
1280 LDAPControl ***pctrls )
1282 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
1283 LDAPControl **ctrls = NULL;
1286 struct berval assertedID,
1291 rs->sr_err = LDAP_SUCCESS;
1293 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1294 * but if it is not set this test fails. We need a different
1295 * means to detect if idassert is enabled */
1296 if ( ( BER_BVISNULL( &li->li_idassert_authcID ) || BER_BVISEMPTY( &li->li_idassert_authcID ) )
1297 && ( BER_BVISNULL( &li->li_idassert_authcDN ) || BER_BVISEMPTY( &li->li_idassert_authcDN ) ) )
1302 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1306 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1307 ndn = op->o_conn->c_ndn;
1313 if ( li->li_idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1314 if ( op->o_proxy_authz ) {
1316 * FIXME: we do not want to perform proxyAuthz
1317 * on behalf of the client, because this would
1318 * be performed with "proxyauthzdn" privileges.
1320 * This might actually be too strict, since
1321 * the "proxyauthzdn" authzTo, and each entry's
1322 * authzFrom attributes may be crafted
1323 * to avoid unwanted proxyAuthz to take place.
1326 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1327 rs->sr_text = "proxyAuthz not allowed within namingContext";
1332 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1336 if ( BER_BVISNULL( &ndn ) ) {
1340 if ( BER_BVISNULL( &li->li_idassert_authcDN ) ) {
1344 } else if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1345 if ( ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1346 /* && ( !BER_BVISNULL( &ndn )
1347 || LDAP_BACK_CONN_ISBOUND( lc ) ) */ )
1349 /* already asserted in SASL via native authz */
1350 /* NOTE: the test on lc->lc_bound is used to trap
1351 * native authorization of anonymous users,
1352 * since in that case ndn is NULL */
1356 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1358 struct berval authcDN;
1360 if ( BER_BVISNULL( &ndn ) ) {
1361 authcDN = slap_empty_bv;
1365 rc = slap_sasl_matches( op, li->li_idassert_authz,
1366 &authcDN, & authcDN );
1367 if ( rc != LDAP_SUCCESS ) {
1368 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1370 /* ndn is not authorized
1371 * to use idassert */
1378 if ( op->o_proxy_authz ) {
1381 * 1) ignore the already set proxyAuthz control
1382 * 2) leave it in place, and don't set ours
1384 * 4) reject the operation
1386 * option (4) is very drastic
1387 * option (3) will make the remote server reject
1388 * the operation, thus being equivalent to (4)
1389 * option (2) will likely break the idassert
1390 * assumptions, so we cannot accept it;
1391 * option (1) means that we are contradicting
1392 * the client's reques.
1394 * I think (4) is the only correct choice.
1396 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1397 rs->sr_text = "proxyAuthz not allowed within namingContext";
1400 if ( op->o_is_auth_check ) {
1401 mode = LDAP_BACK_IDASSERT_NOASSERT;
1404 mode = li->li_idassert_mode;
1408 case LDAP_BACK_IDASSERT_SELF:
1409 if ( BER_BVISNULL( &ndn ) ) {
1415 case LDAP_BACK_IDASSERT_LEGACY:
1416 /* original behavior:
1417 * assert the client's identity */
1418 if ( BER_BVISNULL( &ndn ) ) {
1419 assertedID = slap_empty_bv;
1425 case LDAP_BACK_IDASSERT_ANONYMOUS:
1426 /* assert "anonymous" */
1427 assertedID = slap_empty_bv;
1430 case LDAP_BACK_IDASSERT_NOASSERT:
1431 /* don't assert; bind as proxyauthzdn */
1434 case LDAP_BACK_IDASSERT_OTHERID:
1435 case LDAP_BACK_IDASSERT_OTHERDN:
1436 /* assert idassert DN */
1437 assertedID = li->li_idassert_authzID;
1444 if ( BER_BVISNULL( &assertedID ) ) {
1445 assertedID = slap_empty_bv;
1448 if ( op->o_ctrls ) {
1449 for ( i = 0; op->o_ctrls[ i ]; i++ )
1450 /* just count ctrls */ ;
1453 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
1455 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
1457 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1458 ctrls[ 0 ]->ldctl_iscritical = 1;
1460 switch ( li->li_idassert_mode ) {
1461 /* already in u:ID or dn:DN form */
1462 case LDAP_BACK_IDASSERT_OTHERID:
1463 case LDAP_BACK_IDASSERT_OTHERDN:
1464 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
1467 /* needs the dn: prefix */
1469 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1470 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
1472 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1473 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1474 assertedID.bv_val, assertedID.bv_len + 1 );
1478 if ( op->o_ctrls ) {
1479 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1480 ctrls[ i + 1 ] = op->o_ctrls[ i ];
1483 ctrls[ i + 1 ] = NULL;
1486 if ( ctrls == NULL ) {
1487 ctrls = op->o_ctrls;
1496 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1498 LDAPControl **ctrls = *pctrls;
1500 /* we assume that the first control is the proxyAuthz
1501 * added by back-ldap, so it's the only one we explicitly
1503 if ( ctrls && ctrls != op->o_ctrls ) {
1504 assert( ctrls[ 0 ] != NULL );
1506 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1507 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1510 op->o_tmpfree( ctrls, op->o_tmpmemctx );