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"
35 #undef ldap_debug /* silence a warning in ldap-int.h */
36 #include "../../../libraries/libldap/ldap-int.h"
38 #include "lutil_ldap.h"
40 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ "2.16.840.1.113730.3.4.12"
42 #if LDAP_BACK_PRINT_CONNTREE > 0
44 ravl_print( Avlnode *root, int depth )
53 ravl_print( root->avl_right, depth+1 );
55 for ( i = 0; i < depth; i++ ) {
56 fprintf( stderr, "-" );
60 fprintf( stderr, "lc=%p local=\"%s\" conn=%p %s refcnt=%d\n",
62 lc->lc_local_ndn.bv_val ? lc->lc_local_ndn.bv_val : "",
64 avl_bf2str( root->avl_bf ), lc->lc_refcnt );
66 ravl_print( root->avl_left, depth+1 );
70 ldap_back_print_conntree( Avlnode *root, char *msg )
72 fprintf( stderr, "========> %s\n", msg );
75 fprintf( stderr, "\t(empty)\n" );
78 ravl_print( root, 0 );
81 fprintf( stderr, "<======== %s\n", msg );
83 #endif /* LDAP_BACK_PRINT_CONNTREE */
86 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
87 struct berval *binddn, struct berval *bindcred );
90 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
91 struct berval *binddn, struct berval *bindcred );
94 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs,
95 ldap_back_send_t sendok, struct berval *binddn, struct berval *bindcred );
98 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs,
99 ldap_back_send_t sendok );
102 ldap_back_conndnlc_cmp( const void *c1, const void *c2 );
105 ldap_back_bind( Operation *op, SlapReply *rs )
107 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
113 lc = ldap_back_getconn( op, rs, LDAP_BACK_BIND_SERR, NULL, NULL );
118 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
119 ch_free( lc->lc_bound_ndn.bv_val );
120 BER_BVZERO( &lc->lc_bound_ndn );
122 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
124 /* method is always LDAP_AUTH_SIMPLE if we got here */
125 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
127 &op->orb_cred, op->o_ctrls, NULL, &msgid );
128 rc = ldap_back_op_result( lc, op, rs, msgid,
129 li->li_timeout[ SLAP_OP_BIND ],
130 LDAP_BACK_BIND_SERR );
131 if ( rc == LDAP_SUCCESS ) {
132 /* If defined, proxyAuthz will be used also when
133 * back-ldap is the authorizing backend; for this
134 * purpose, after a successful bind the connection
135 * is trashed and further operations will use
136 * a default connections with identity assertion */
137 /* NOTE: use with care */
138 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
139 LDAP_BACK_CONN_TAINTED_SET( lc );
140 ldap_back_release_conn( op, rs, lc );
145 /* rebind is now done inside ldap_back_proxy_authz_bind()
146 * in case of success */
147 LDAP_BACK_CONN_ISBOUND_SET( lc );
148 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
150 if ( LDAP_BACK_SAVECRED( li ) ) {
151 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
152 memset( lc->lc_cred.bv_val, 0,
153 lc->lc_cred.bv_len );
155 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
156 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
160 assert( lc->lc_binding == 1 );
163 /* must re-insert if local DN changed as result of bind */
164 if ( !LDAP_BACK_CONN_ISBOUND( lc )
165 || ( LDAP_BACK_CONN_ISBOUND( lc )
166 && !dn_match( &op->o_req_ndn, &lc->lc_local_ndn ) ) )
171 /* wait for all other ops to release the connection */
173 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
174 if ( lc->lc_refcnt > 1 ) {
175 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
176 ldap_pvt_thread_yield();
180 #if LDAP_BACK_PRINT_CONNTREE > 0
181 ldap_back_print_conntree( li->li_conninfo.lai_tree, ">>> ldap_back_bind" );
182 #endif /* LDAP_BACK_PRINT_CONNTREE */
184 assert( lc->lc_refcnt == 1 );
185 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
186 ldap_back_conndnlc_cmp );
187 assert( tmplc == NULL || lc == tmplc );
189 /* delete all cached connections with the current connection */
190 if ( LDAP_BACK_SINGLECONN( li ) ) {
191 while ( ( tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc, ldap_back_conn_cmp ) ) != NULL )
193 Debug( LDAP_DEBUG_TRACE,
194 "=>ldap_back_bind: destroying conn %ld (refcnt=%u)\n",
195 LDAP_BACK_PCONN_ID( lc ), lc->lc_refcnt, 0 );
197 if ( tmplc->lc_refcnt != 0 ) {
199 LDAP_BACK_CONN_TAINTED_SET( tmplc );
203 * Needs a test because the handler may be corrupted,
204 * and calling ldap_unbind on a corrupted header results
205 * in a segmentation fault
207 ldap_back_conn_free( tmplc );
212 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
213 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
214 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
215 lc->lc_conn = LDAP_BACK_PCONN_SET( op );
217 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
218 ldap_back_conndn_cmp, ldap_back_conndn_dup );
221 #if LDAP_BACK_PRINT_CONNTREE > 0
222 ldap_back_print_conntree( li->li_conninfo.lai_tree, "<<< ldap_back_bind" );
223 #endif /* LDAP_BACK_PRINT_CONNTREE */
225 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
231 /* duplicate; someone else successfully bound
232 * on the same connection with the same identity;
233 * we can do this because lc_refcnt == 1 */
234 ldap_back_conn_free( lc );
240 ldap_back_release_conn( op, rs, lc );
247 * ldap_back_conndn_cmp
249 * compares two ldapconn_t based on the value of the conn pointer
250 * and of the local DN; used by avl stuff for insert, lookup
254 ldap_back_conndn_cmp( const void *c1, const void *c2 )
256 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
257 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
260 /* If local DNs don't match, it is definitely not a match */
261 /* For shared sessions, conn is NULL. Only explicitly
262 * bound sessions will have non-NULL conn.
264 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
266 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
273 * ldap_back_conndnlc_cmp
275 * compares two ldapconn_t based on the value of the conn pointer,
276 * the local DN and the lc pointer; used by avl stuff for insert, lookup
280 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
282 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
283 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
286 /* If local DNs don't match, it is definitely not a match */
287 /* For shared sessions, conn is NULL. Only explicitly
288 * bound sessions will have non-NULL conn.
290 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
292 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
294 rc = SLAP_PTRCMP( lc1, lc2 );
304 * compares two ldapconn_t based on the value of the conn pointer;
305 * used by avl stuff for delete of all conns with the same connid
308 ldap_back_conn_cmp( const void *c1, const void *c2 )
310 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
311 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
313 /* For shared sessions, conn is NULL. Only explicitly
314 * bound sessions will have non-NULL conn.
316 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
320 * ldap_back_conndn_dup
322 * returns -1 in case a duplicate ldapconn_t has been inserted;
326 ldap_back_conndn_dup( void *c1, void *c2 )
328 ldapconn_t *lc1 = (ldapconn_t *)c1;
329 ldapconn_t *lc2 = (ldapconn_t *)c2;
331 /* Cannot have more than one shared session with same DN */
332 if ( lc1->lc_conn == lc2->lc_conn &&
333 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
342 ldap_back_freeconn( Operation *op, ldapconn_t *lc, int dolock )
344 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
348 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
351 #if LDAP_BACK_PRINT_CONNTREE > 0
352 ldap_back_print_conntree( li->li_conninfo.lai_tree, ">>> ldap_back_freeconn" );
353 #endif /* LDAP_BACK_PRINT_CONNTREE */
355 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
356 ldap_back_conndnlc_cmp );
357 assert( LDAP_BACK_CONN_TAINTED( lc ) || tmplc == lc );
358 if ( lc->lc_refcnt == 0 ) {
359 ldap_back_conn_free( (void *)lc );
362 #if LDAP_BACK_PRINT_CONNTREE > 0
363 ldap_back_print_conntree( li->li_conninfo.lai_tree, "<<< ldap_back_freeconn" );
364 #endif /* LDAP_BACK_PRINT_CONNTREE */
367 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
384 int rc = LDAP_SUCCESS;
387 /* this is ridiculous... */
388 dummy.li_flags = flags;
390 /* start TLS ("tls-[try-]{start,propagate}" statements) */
391 if ( ( LDAP_BACK_USE_TLS( &dummy ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS( &dummy ) ) )
392 && !ldap_is_ldaps_url( url ) )
394 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
396 * use asynchronous StartTLS
397 * in case, chase referral (not implemented yet)
401 if ( protocol == 0 ) {
402 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
406 if ( protocol < LDAP_VERSION3 ) {
407 /* we should rather bail out... */
408 rc = LDAP_UNWILLING_TO_PERFORM;
409 *text = "invalid protocol version";
412 if ( rc == LDAP_SUCCESS ) {
413 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
416 if ( rc == LDAP_SUCCESS ) {
417 LDAPMessage *res = NULL;
420 LDAP_BACK_TV_SET( &tv );
423 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
425 rc = LDAP_UNAVAILABLE;
427 } else if ( rc == 0 ) {
428 if ( retries != LDAP_BACK_RETRY_NEVER ) {
429 ldap_pvt_thread_yield();
433 LDAP_BACK_TV_SET( &tv );
436 rc = LDAP_UNAVAILABLE;
438 } else if ( rc == LDAP_RES_EXTENDED ) {
439 struct berval *data = NULL;
441 rc = ldap_parse_extended_result( ld, res,
443 if ( rc == LDAP_SUCCESS ) {
445 rc = ldap_parse_result( ld, res, &err,
446 NULL, NULL, NULL, NULL, 1 );
447 if ( rc == LDAP_SUCCESS ) {
452 /* FIXME: in case a referral
453 * is returned, should we try
454 * using it instead of the
456 if ( rc == LDAP_SUCCESS ) {
457 rc = ldap_install_tls( ld );
459 } else if ( rc == LDAP_REFERRAL ) {
460 rc = LDAP_UNWILLING_TO_PERFORM;
461 *text = "unwilling to chase referral returned by Start TLS exop";
465 if ( data->bv_val ) {
466 ber_memfree( data->bv_val );
480 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
482 * use synchronous StartTLS
484 rc = ldap_start_tls_s( ld, NULL, NULL );
485 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
487 /* if StartTLS is requested, only attempt it if the URL
488 * is not "ldaps://"; this may occur not only in case
489 * of misconfiguration, but also when used in the chain
490 * overlay, where the "uri" can be parsed out of a referral */
496 case LDAP_SERVER_DOWN:
500 if ( LDAP_BACK_TLS_CRITICAL( &dummy ) ) {
501 *text = "could not start TLS";
505 /* in case Start TLS is not critical */
517 #endif /* HAVE_TLS */
520 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
522 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
526 int is_tls = op->o_conn->c_is_tls;
527 time_t lc_time = (time_t)(-1);
528 #endif /* HAVE_TLS */
530 assert( lcp != NULL );
532 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
533 rs->sr_err = ldap_initialize( &ld, li->li_uri );
534 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
535 if ( rs->sr_err != LDAP_SUCCESS ) {
539 if ( li->li_urllist_f ) {
540 ldap_set_urllist_proc( ld, li->li_urllist_f, li->li_urllist_p );
543 /* Set LDAP version. This will always succeed: If the client
544 * bound with a particular version, then so can we.
546 if ( li->li_version != 0 ) {
547 version = li->li_version;
549 } else if ( op->o_protocol != 0 ) {
550 version = op->o_protocol;
553 /* assume it's an internal op; set to LDAPv3 */
554 version = LDAP_VERSION3;
556 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
558 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
559 ldap_set_option( ld, LDAP_OPT_REFERRALS,
560 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
562 if ( li->li_network_timeout > 0 ) {
565 tv.tv_sec = li->li_network_timeout;
567 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
571 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
572 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
573 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
574 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
575 if ( rs->sr_err != LDAP_SUCCESS ) {
576 ldap_unbind_ext( ld, NULL, NULL );
579 } else if ( li->li_idle_timeout ) {
580 /* only touch when activity actually took place... */
581 lc_time = op->o_time;
583 #endif /* HAVE_TLS */
585 if ( *lcp == NULL ) {
586 *lcp = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
587 (*lcp)->lc_flags = li->li_flags;
590 (*lcp)->lc_refcnt = 1;
591 (*lcp)->lc_binding = 1;
594 LDAP_BACK_CONN_ISTLS_SET( *lcp );
596 LDAP_BACK_CONN_ISTLS_CLEAR( *lcp );
598 if ( lc_time != (time_t)(-1) ) {
599 (*lcp)->lc_time = lc_time;
601 #endif /* HAVE_TLS */
604 if ( rs->sr_err != LDAP_SUCCESS ) {
605 rs->sr_err = slap_map_api2result( rs );
606 if ( sendok & LDAP_BACK_SENDERR ) {
607 if ( rs->sr_text == NULL ) {
608 rs->sr_text = "ldap_initialize() failed";
610 send_ldap_result( op, rs );
615 if ( li->li_conn_ttl > 0 ) {
616 (*lcp)->lc_create_time = op->o_time;
627 ldap_back_send_t sendok,
628 struct berval *binddn,
629 struct berval *bindcred )
631 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
632 ldapconn_t *lc = NULL,
637 /* if the server is quarantined, and
638 * - the current interval did not expire yet, or
639 * - no more retries should occur,
640 * don't return the connection */
641 if ( li->li_isquarantined ) {
642 slap_retry_info_t *ri = &li->li_quarantine;
645 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
646 if ( li->li_isquarantined == LDAP_BACK_FQ_YES ) {
647 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
648 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
650 Debug( LDAP_DEBUG_ANY,
651 "%s: ldap_back_getconn quarantine "
652 "retry block #%d try #%d.\n",
653 op->o_log_prefix, ri->ri_idx, ri->ri_count );
654 li->li_isquarantined = LDAP_BACK_FQ_RETRYING;
657 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
660 rs->sr_err = LDAP_UNAVAILABLE;
661 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
662 send_ldap_result( op, rs );
668 /* Internal searches are privileged and shared. So is root. */
669 if ( op->o_do_not_cache || be_isroot( op ) ) {
670 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
671 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
672 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
675 lc_curr.lc_local_ndn = op->o_ndn;
676 /* Explicit binds must not be shared */
677 if ( op->o_tag == LDAP_REQ_BIND ) {
678 lc_curr.lc_conn = op->o_conn;
681 if ( !( sendok & LDAP_BACK_BINDING ) &&
682 ldap_back_is_proxy_authz( op, rs, sendok, binddn, bindcred ) )
684 lc_curr.lc_local_ndn = *binddn;
685 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
686 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
688 } else if ( SLAP_IS_AUTHZ_BACKEND( op ) ) {
689 lc_curr.lc_conn = op->o_conn;
692 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
697 /* Explicit Bind requests always get their own conn */
698 if ( !( sendok & LDAP_BACK_BINDING ) ) {
699 /* Searches for a ldapconn in the avl tree */
701 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
703 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
704 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
706 /* Don't reuse connections while they're still binding */
707 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
708 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
709 ldap_pvt_thread_yield();
713 refcnt = ++lc->lc_refcnt;
714 binding = ++lc->lc_binding;
716 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
719 /* Looks like we didn't get a bind. Open a new session... */
721 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
725 if ( sendok & LDAP_BACK_BINDING ) {
726 LDAP_BACK_CONN_BINDING_SET( lc );
729 lc->lc_conn = lc_curr.lc_conn;
730 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
733 * the rationale is: connections as the rootdn are privileged,
734 * so acl_authcDN is to be used; however, in some cases
735 * one already configured identity assertion with a highly
736 * privileged idassert_authcDN, so if acl_authcDN is NULL
737 * and idassert_authcDN is not, use the second instead.
739 * might change in the future, because it's preferable
740 * to make clear what identity is being used, since
741 * the only drawback is that one risks to configure
742 * the same identity twice...
744 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
745 if ( BER_BVISNULL( &li->li_acl_authcDN ) && !BER_BVISNULL( &li->li_idassert_authcDN ) ) {
746 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
747 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
750 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
751 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
753 LDAP_BACK_CONN_ISPRIV_SET( lc );
755 } else if ( LDAP_BACK_CONN_ISIDASSERT( &lc_curr ) ) {
756 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
757 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
758 LDAP_BACK_CONN_ISIDASSERT_SET( lc );
761 BER_BVZERO( &lc->lc_cred );
762 BER_BVZERO( &lc->lc_bound_ndn );
763 if ( !BER_BVISEMPTY( &op->o_ndn )
764 && SLAP_IS_AUTHZ_BACKEND( op ) )
766 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
771 /* if start TLS failed but it was not mandatory,
772 * check if the non-TLS connection was already
773 * in cache; in case, destroy the newly created
774 * connection and use the existing one */
775 if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
776 && !ldap_tls_inplace( lc->lc_ld ) )
780 lc_curr.lc_conn = LDAP_BACK_PCONN;
781 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
782 tmplc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
783 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
784 if ( tmplc != NULL ) {
785 refcnt = ++tmplc->lc_refcnt;
786 binding = ++tmplc->lc_binding;
787 ldap_back_conn_free( lc );
790 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
792 if ( tmplc != NULL ) {
796 #endif /* HAVE_TLS */
798 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
800 /* Inserts the newly created ldapconn in the avl tree */
801 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
803 #if LDAP_BACK_PRINT_CONNTREE > 0
804 ldap_back_print_conntree( li->li_conninfo.lai_tree, ">>> ldap_back_getconn(insert)" );
805 #endif /* LDAP_BACK_PRINT_CONNTREE */
807 assert( lc->lc_refcnt == 1 );
808 assert( lc->lc_binding == 1 );
809 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
810 ldap_back_conndn_cmp, ldap_back_conndn_dup );
812 #if LDAP_BACK_PRINT_CONNTREE > 0
813 ldap_back_print_conntree( li->li_conninfo.lai_tree, "<<< ldap_back_getconn(insert)" );
814 #endif /* LDAP_BACK_PRINT_CONNTREE */
816 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
818 Debug( LDAP_DEBUG_TRACE,
819 "=>ldap_back_getconn: conn %p inserted refcnt=%u binding=%u\n",
820 (void *)lc, refcnt, binding );
822 /* Err could be -1 in case a duplicate ldapconn is inserted */
823 switch ( rs->sr_err ) {
828 if ( !( sendok & LDAP_BACK_BINDING ) ) {
829 /* duplicate: free and try to get the newly created one */
832 /* taint connection, so that it'll be freed when released */
833 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
834 #if LDAP_BACK_PRINT_CONNTREE > 0
835 ldap_back_print_conntree( li->li_conninfo.lai_tree, ">>> ldap_back_getconn(delete)" );
836 #endif /* LDAP_BACK_PRINT_CONNTREE */
837 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
838 ldap_back_conndnlc_cmp );
839 #if LDAP_BACK_PRINT_CONNTREE > 0
840 ldap_back_print_conntree( li->li_conninfo.lai_tree, "<<< ldap_back_getconn(delete)" );
841 #endif /* LDAP_BACK_PRINT_CONNTREE */
842 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
843 LDAP_BACK_CONN_TAINTED_SET( lc );
847 ldap_back_conn_free( lc );
848 rs->sr_err = LDAP_OTHER;
849 rs->sr_text = "proxy bind collision";
850 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
851 send_ldap_result( op, rs );
860 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
861 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
865 /* let it be used, but taint/delete it so that
866 * no-one else can look it up any further */
867 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
868 #if LDAP_BACK_PRINT_CONNTREE > 0
869 ldap_back_print_conntree( li->li_conninfo.lai_tree, ">>> ldap_back_getconn(timeout)" );
870 #endif /* LDAP_BACK_PRINT_CONNTREE */
871 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
872 ldap_back_conndnlc_cmp );
873 #if LDAP_BACK_PRINT_CONNTREE > 0
874 ldap_back_print_conntree( li->li_conninfo.lai_tree, "<<< ldap_back_getconn(timeout)" );
875 #endif /* LDAP_BACK_PRINT_CONNTREE */
876 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
877 LDAP_BACK_CONN_TAINTED_SET( lc );
880 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
881 char buf[ SLAP_TEXT_BUFLEN ];
883 snprintf( buf, sizeof( buf ),
884 "conn %p fetched refcnt=%u binding=%u%s",
885 (void *)lc, refcnt, binding, expiring ? " expiring" : "" );
886 Debug( LDAP_DEBUG_TRACE,
887 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
893 #endif /* HAVE_TLS */
899 ldap_back_release_conn_lock(
905 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
907 ldapconn_t *lc = *lcp;
910 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
912 assert( lc->lc_refcnt > 0 );
913 LDAP_BACK_CONN_BINDING_CLEAR( lc );
915 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
916 ldap_back_freeconn( op, lc, 0 );
920 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
925 ldap_back_quarantine(
929 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
931 slap_retry_info_t *ri = &li->li_quarantine;
933 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
935 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
936 time_t new_last = slap_get_time();
938 switch ( li->li_isquarantined ) {
939 case LDAP_BACK_FQ_NO:
940 if ( ri->ri_last == new_last ) {
944 Debug( LDAP_DEBUG_ANY,
945 "%s: ldap_back_quarantine enter.\n",
946 op->o_log_prefix, 0, 0 );
952 case LDAP_BACK_FQ_RETRYING:
953 Debug( LDAP_DEBUG_ANY,
954 "%s: ldap_back_quarantine block #%d try #%d failed.\n",
955 op->o_log_prefix, ri->ri_idx, ri->ri_count );
958 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
959 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
970 li->li_isquarantined = LDAP_BACK_FQ_YES;
971 ri->ri_last = new_last;
973 } else if ( li->li_isquarantined != LDAP_BACK_FQ_NO ) {
974 Debug( LDAP_DEBUG_ANY,
975 "%s: ldap_back_quarantine exit.\n",
976 op->o_log_prefix, ri->ri_idx, ri->ri_count );
978 if ( li->li_quarantine_f ) {
979 (void)li->li_quarantine_f( li, li->li_quarantine_p );
984 li->li_isquarantined = LDAP_BACK_FQ_NO;
988 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
992 * ldap_back_dobind_int
994 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
997 ldap_back_dobind_int(
1001 ldap_back_send_t sendok,
1005 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1008 struct berval binddn = slap_empty_bv,
1009 bindcred = slap_empty_bv;
1016 assert( lcp != NULL );
1017 assert( retries >= 0 );
1019 if ( sendok & LDAP_BACK_GETCONN ) {
1020 assert( *lcp == NULL );
1022 lc = ldap_back_getconn( op, rs, sendok, &binddn, &bindcred );
1032 assert( lc != NULL );
1036 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1039 if ( binding == 0 ) {
1040 /* check if already bound */
1041 rc = isbound = LDAP_BACK_CONN_ISBOUND( lc );
1045 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1050 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
1051 /* if someone else is about to bind it, give up and retry */
1053 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1055 ldap_pvt_thread_yield();
1059 /* otherwise this thread will bind it */
1060 LDAP_BACK_CONN_BINDING_SET( lc );
1065 /* wait for pending operations to finish */
1066 /* FIXME: may become a bottleneck! */
1067 if ( lc->lc_refcnt != lc->lc_binding ) {
1069 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1071 ldap_pvt_thread_yield();
1076 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1080 * FIXME: we need to let clients use proxyAuthz
1081 * otherwise we cannot do symmetric pools of servers;
1082 * we have to live with the fact that a user can
1083 * authorize itself as any ID that is allowed
1084 * by the authzTo directive of the "proxyauthzdn".
1087 * NOTE: current Proxy Authorization specification
1088 * and implementation do not allow proxy authorization
1089 * control to be provided with Bind requests
1092 * if no bind took place yet, but the connection is bound
1093 * and the "idassert-authcDN" (or other ID) is set,
1094 * then bind as the asserting identity and explicitly
1095 * add the proxyAuthz control to every operation with the
1096 * dn bound to the connection as control value.
1097 * This is done also if this is the authrizing backend,
1098 * but the "override" flag is given to idassert.
1099 * It allows to use SASL bind and yet proxyAuthz users
1101 if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
1102 if ( BER_BVISEMPTY( &binddn ) && BER_BVISEMPTY( &bindcred ) ) {
1103 ldap_back_is_proxy_authz( op, rs, sendok, &binddn, &bindcred );
1105 (void)ldap_back_proxy_authz_bind( lc, op, rs, sendok, &binddn, &bindcred );
1109 #ifdef HAVE_CYRUS_SASL
1110 if ( LDAP_BACK_CONN_ISPRIV( lc )
1111 && li->li_acl_authmethod == LDAP_AUTH_SASL )
1113 void *defaults = NULL;
1115 if ( li->li_acl_secprops != NULL ) {
1116 rc = ldap_set_option( lc->lc_ld,
1117 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops );
1119 if ( rc != LDAP_OPT_SUCCESS ) {
1120 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
1121 "(SECPROPS,\"%s\") failed!\n",
1122 li->li_acl_secprops, 0, 0 );
1127 defaults = lutil_sasl_defaults( lc->lc_ld,
1128 li->li_acl_sasl_mech.bv_val,
1129 li->li_acl_sasl_realm.bv_val,
1130 li->li_acl_authcID.bv_val,
1131 li->li_acl_passwd.bv_val,
1134 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
1135 li->li_acl_authcDN.bv_val,
1136 li->li_acl_sasl_mech.bv_val, NULL, NULL,
1137 LDAP_SASL_QUIET, lutil_sasl_interact,
1140 lutil_sasl_freedefs( defaults );
1142 rs->sr_err = slap_map_api2result( rs );
1143 if ( rs->sr_err != LDAP_SUCCESS ) {
1144 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1145 if ( sendok & LDAP_BACK_SENDERR ) {
1146 send_ldap_result( op, rs );
1150 LDAP_BACK_CONN_ISBOUND_SET( lc );
1153 if ( LDAP_BACK_QUARANTINE( li ) ) {
1154 ldap_back_quarantine( op, rs );
1159 #endif /* HAVE_CYRUS_SASL */
1162 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1163 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1164 LDAP_SASL_SIMPLE, &lc->lc_cred,
1165 NULL, NULL, &msgid );
1167 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
1168 if ( retries != LDAP_BACK_RETRY_NEVER ) {
1170 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1173 assert( lc->lc_refcnt > 0 );
1174 if ( lc->lc_refcnt == 1 ) {
1175 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1178 /* lc here must be the regular lc, reset and ready for init */
1179 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
1180 if ( rs->sr_err != LDAP_SUCCESS ) {
1187 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1190 if ( rs->sr_err == LDAP_SUCCESS ) {
1191 if ( retries > 0 ) {
1199 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1203 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1207 /* FIXME: one binding-- too many? */
1209 ldap_back_freeconn( op, lc, dolock );
1211 rs->sr_err = slap_map_api2result( rs );
1213 if ( LDAP_BACK_QUARANTINE( li ) ) {
1214 ldap_back_quarantine( op, rs );
1217 if ( rs->sr_err != LDAP_SUCCESS &&
1218 ( sendok & LDAP_BACK_SENDERR ) )
1220 send_ldap_result( op, rs );
1226 rc = ldap_back_op_result( lc, op, rs, msgid,
1227 -1, (sendok|LDAP_BACK_BINDING) );
1228 if ( rc == LDAP_SUCCESS ) {
1229 LDAP_BACK_CONN_ISBOUND_SET( lc );
1234 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1235 rc = LDAP_BACK_CONN_ISBOUND( lc );
1237 ldap_back_release_conn_lock( op, rs, lcp, dolock );
1239 } else if ( LDAP_BACK_SAVECRED( li ) ) {
1240 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1249 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1252 ldap_back_dobind( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1254 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1256 return ldap_back_dobind_int( lcp, op, rs,
1257 ( sendok | LDAP_BACK_GETCONN ), li->li_nretries, 1 );
1261 * ldap_back_default_rebind
1263 * This is a callback used for chasing referrals using the same
1264 * credentials as the original user on this session.
1267 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1268 ber_int_t msgid, void *params )
1270 ldapconn_t *lc = (ldapconn_t *)params;
1273 /* ... otherwise we couldn't get here */
1274 assert( lc != NULL );
1276 if ( !ldap_tls_inplace( ld ) ) {
1277 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1279 const char *text = NULL;
1281 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1282 LDAP_BACK_RETRY_DEFAULT, &text );
1283 if ( rc != LDAP_SUCCESS ) {
1287 #endif /* HAVE_TLS */
1289 /* FIXME: add checks on the URL/identity? */
1291 return ldap_sasl_bind_s( ld,
1292 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1293 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1297 * ldap_back_default_urllist
1300 ldap_back_default_urllist(
1302 LDAPURLDesc **urllist,
1306 ldapinfo_t *li = (ldapinfo_t *)params;
1307 LDAPURLDesc **urltail;
1309 if ( urllist == url ) {
1310 return LDAP_SUCCESS;
1313 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1316 *urltail = *urllist;
1320 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1322 ch_free( li->li_uri );
1325 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1326 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1328 return LDAP_SUCCESS;
1337 ldap_back_send_t sendok )
1339 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1341 /* default behavior */
1342 if ( LDAP_BACK_ABANDON( li ) ) {
1343 return ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1346 if ( LDAP_BACK_IGNORE( li ) ) {
1347 return ldap_int_discard( lc->lc_ld, msgid );
1350 if ( LDAP_BACK_CANCEL( li ) ) {
1351 /* FIXME: asynchronous? */
1352 return ldap_cancel_s( lc->lc_ld, msgid, NULL, NULL );
1361 ldap_back_op_result(
1367 ldap_back_send_t sendok )
1369 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1374 LDAPControl **ctrls = NULL;
1376 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
1379 rs->sr_matched = NULL;
1381 rs->sr_ctrls = NULL;
1383 /* if the error recorded in the reply corresponds
1384 * to a successful state, get the error from the
1385 * remote server response */
1386 if ( ERR_OK( rs->sr_err ) ) {
1389 LDAPMessage *res = NULL;
1390 time_t stoptime = (time_t)(-1);
1391 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
1392 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
1393 const char *timeout_text = "Operation timed out";
1395 /* if timeout is not specified, compute and use
1396 * the one specific to the ongoing operation */
1397 if ( timeout == (time_t)(-1) ) {
1398 slap_op_t opidx = slap_req2op( op->o_tag );
1400 if ( opidx == SLAP_OP_SEARCH ) {
1401 if ( op->ors_tlimit <= 0 ) {
1405 timeout = op->ors_tlimit;
1406 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
1407 timeout_text = NULL;
1411 timeout = li->li_timeout[ opidx ];
1415 /* better than nothing :) */
1416 if ( timeout == 0 ) {
1417 if ( li->li_idle_timeout ) {
1418 timeout = li->li_idle_timeout;
1420 } else if ( li->li_conn_ttl ) {
1421 timeout = li->li_conn_ttl;
1426 stoptime = op->o_time + timeout;
1429 LDAP_BACK_TV_SET( &tv );
1432 /* if result parsing fails, note the failure reason */
1433 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1436 if ( timeout && slap_get_time() > stoptime ) {
1437 if ( sendok & LDAP_BACK_BINDING ) {
1438 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1440 LDAP_BACK_CONN_TAINTED_SET( lc );
1443 (void)ldap_back_cancel( lc, op, rs, msgid, sendok );
1445 rs->sr_err = timeout_err;
1446 rs->sr_text = timeout_text;
1451 LDAP_BACK_TV_SET( &tv );
1452 ldap_pvt_thread_yield();
1456 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1461 /* otherwise get the result; if it is not
1462 * LDAP_SUCCESS, record it in the reply
1463 * structure (this includes
1464 * LDAP_COMPARE_{TRUE|FALSE}) */
1466 /* only touch when activity actually took place... */
1467 if ( li->li_idle_timeout ) {
1468 lc->lc_time = op->o_time;
1471 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1472 &match, &text, &refs, &ctrls, 1 );
1474 if ( rc != LDAP_SUCCESS ) {
1477 if ( refs != NULL ) {
1480 for ( i = 0; refs[ i ] != NULL; i++ )
1482 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1484 for ( i = 0; refs[ i ] != NULL; i++ ) {
1485 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1487 BER_BVZERO( &rs->sr_ref[ i ] );
1489 if ( ctrls != NULL ) {
1490 rs->sr_ctrls = ctrls;
1495 /* if the error in the reply structure is not
1496 * LDAP_SUCCESS, try to map it from client
1497 * to server error */
1498 if ( !ERR_OK( rs->sr_err ) ) {
1499 rs->sr_err = slap_map_api2result( rs );
1501 /* internal ops ( op->o_conn == NULL )
1502 * must not reply to client */
1503 if ( op->o_conn && !op->o_do_not_cache && match ) {
1505 /* record the (massaged) matched
1506 * DN into the reply structure */
1507 rs->sr_matched = match;
1511 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1512 if ( !( sendok & LDAP_BACK_RETRYING ) ) {
1513 if ( LDAP_BACK_QUARANTINE( li ) ) {
1514 ldap_back_quarantine( op, rs );
1516 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1517 send_ldap_result( op, rs );
1521 } else if ( op->o_conn &&
1522 ( ( ( sendok & LDAP_BACK_SENDOK ) && ERR_OK( rs->sr_err ) )
1523 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1525 send_ldap_result( op, rs );
1529 if ( rs->sr_matched != match ) {
1530 free( (char *)rs->sr_matched );
1532 rs->sr_matched = NULL;
1533 ldap_memfree( match );
1537 ldap_memfree( text );
1542 assert( refs != NULL );
1543 ber_memvfree( (void **)refs );
1544 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1549 assert( rs->sr_ctrls != NULL );
1550 ldap_controls_free( ctrls );
1551 rs->sr_ctrls = NULL;
1554 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1557 /* return true if bound, false if failed */
1559 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1562 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1564 assert( lcp != NULL );
1565 assert( *lcp != NULL );
1567 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1569 if ( (*lcp)->lc_refcnt == 1 ) {
1570 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1571 Debug( LDAP_DEBUG_ANY,
1572 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1573 op->o_log_prefix, li->li_uri,
1574 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1575 "" : (*lcp)->lc_bound_ndn.bv_val );
1576 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1578 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1579 (*lcp)->lc_ld = NULL;
1580 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1582 /* lc here must be the regular lc, reset and ready for init */
1583 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
1584 if ( rc != LDAP_SUCCESS ) {
1585 /* freeit, because lc_refcnt == 1 */
1586 (*lcp)->lc_refcnt = 0;
1587 (void)ldap_back_freeconn( op, *lcp, 0 );
1592 rc = ldap_back_dobind_int( lcp, op, rs, sendok, 0, 0 );
1593 if ( rc == 0 && *lcp != NULL ) {
1594 /* freeit, because lc_refcnt == 1 */
1595 (*lcp)->lc_refcnt = 0;
1596 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1597 (void)ldap_back_freeconn( op, *lcp, 0 );
1603 Debug( LDAP_DEBUG_TRACE,
1604 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1605 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1607 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1608 ldap_back_release_conn_lock( op, rs, lcp, 0 );
1609 assert( *lcp == NULL );
1612 rs->sr_err = LDAP_UNAVAILABLE;
1613 rs->sr_text = "unable to retry";
1614 send_ldap_result( op, rs );
1618 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1624 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
1625 struct berval *binddn, struct berval *bindcred )
1627 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1631 if ( op->o_conn == NULL || op->o_do_not_cache ) {
1635 /* don't proxyAuthz if protocol is not LDAPv3 */
1636 switch ( li->li_version ) {
1641 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1647 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1648 if ( sendok & LDAP_BACK_SENDERR ) {
1649 send_ldap_result( op, rs );
1655 *binddn = slap_empty_bv;
1656 *bindcred = slap_empty_bv;
1658 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1659 ndn = op->o_conn->c_ndn;
1665 switch ( li->li_idassert_mode ) {
1666 case LDAP_BACK_IDASSERT_LEGACY:
1667 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1668 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1670 *binddn = li->li_idassert_authcDN;
1671 *bindcred = li->li_idassert_passwd;
1678 /* NOTE: rootdn can always idassert */
1679 if ( BER_BVISNULL( &ndn ) && li->li_idassert_authz == NULL ) {
1680 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1681 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1682 if ( sendok & LDAP_BACK_SENDERR ) {
1683 send_ldap_result( op, rs );
1687 rs->sr_err = LDAP_SUCCESS;
1688 *binddn = slap_empty_bv;
1689 *bindcred = slap_empty_bv;
1695 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1696 struct berval authcDN;
1698 if ( BER_BVISNULL( &ndn ) ) {
1699 authcDN = slap_empty_bv;
1704 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1705 &authcDN, &authcDN );
1706 if ( rs->sr_err != LDAP_SUCCESS ) {
1707 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1708 if ( sendok & LDAP_BACK_SENDERR ) {
1709 send_ldap_result( op, rs );
1713 rs->sr_err = LDAP_SUCCESS;
1714 *binddn = slap_empty_bv;
1715 *bindcred = slap_empty_bv;
1723 *binddn = li->li_idassert_authcDN;
1724 *bindcred = li->li_idassert_passwd;
1734 ldap_back_proxy_authz_bind(
1738 ldap_back_send_t sendok,
1739 struct berval *binddn,
1740 struct berval *bindcred )
1742 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1747 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1748 ndn = op->o_conn->c_ndn;
1754 if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1755 #ifdef HAVE_CYRUS_SASL
1756 void *defaults = NULL;
1757 struct berval authzID = BER_BVNULL;
1760 /* if SASL supports native authz, prepare for it */
1761 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1762 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1764 switch ( li->li_idassert_mode ) {
1765 case LDAP_BACK_IDASSERT_OTHERID:
1766 case LDAP_BACK_IDASSERT_OTHERDN:
1767 authzID = li->li_idassert_authzID;
1770 case LDAP_BACK_IDASSERT_ANONYMOUS:
1771 BER_BVSTR( &authzID, "dn:" );
1774 case LDAP_BACK_IDASSERT_SELF:
1775 if ( BER_BVISNULL( &ndn ) ) {
1776 /* connection is not authc'd, so don't idassert */
1777 BER_BVSTR( &authzID, "dn:" );
1780 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1781 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1782 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1783 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1784 ndn.bv_val, ndn.bv_len + 1 );
1793 if ( li->li_idassert_secprops != NULL ) {
1794 rs->sr_err = ldap_set_option( lc->lc_ld,
1795 LDAP_OPT_X_SASL_SECPROPS,
1796 (void *)li->li_idassert_secprops );
1798 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1799 rs->sr_err = LDAP_OTHER;
1800 if ( sendok & LDAP_BACK_SENDERR ) {
1801 send_ldap_result( op, rs );
1803 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1808 defaults = lutil_sasl_defaults( lc->lc_ld,
1809 li->li_idassert_sasl_mech.bv_val,
1810 li->li_idassert_sasl_realm.bv_val,
1811 li->li_idassert_authcID.bv_val,
1812 li->li_idassert_passwd.bv_val,
1815 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn->bv_val,
1816 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1817 LDAP_SASL_QUIET, lutil_sasl_interact,
1820 rs->sr_err = slap_map_api2result( rs );
1821 if ( rs->sr_err != LDAP_SUCCESS ) {
1822 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1823 if ( sendok & LDAP_BACK_SENDERR ) {
1824 send_ldap_result( op, rs );
1828 LDAP_BACK_CONN_ISBOUND_SET( lc );
1831 lutil_sasl_freedefs( defaults );
1833 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1837 #endif /* HAVE_CYRUS_SASL */
1840 switch ( li->li_idassert_authmethod ) {
1841 case LDAP_AUTH_NONE:
1842 /* FIXME: do we really need this? */
1843 BER_BVSTR( binddn, "" );
1844 BER_BVSTR( bindcred, "" );
1847 case LDAP_AUTH_SIMPLE:
1848 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1849 binddn->bv_val, LDAP_SASL_SIMPLE,
1850 bindcred, NULL, NULL, &msgid );
1851 rc = ldap_back_op_result( lc, op, rs, msgid,
1852 -1, (sendok|LDAP_BACK_BINDING) );
1857 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1858 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1859 if ( sendok & LDAP_BACK_SENDERR ) {
1860 send_ldap_result( op, rs );
1865 if ( rc == LDAP_SUCCESS ) {
1866 /* set rebind stuff in case of successful proxyAuthz bind,
1867 * so that referral chasing is attempted using the right
1869 LDAP_BACK_CONN_ISBOUND_SET( lc );
1870 ber_bvreplace( &lc->lc_bound_ndn, binddn );
1872 if ( LDAP_BACK_SAVECRED( li ) ) {
1873 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
1874 memset( lc->lc_cred.bv_val, 0,
1875 lc->lc_cred.bv_len );
1877 ber_bvreplace( &lc->lc_cred, bindcred );
1878 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1882 return LDAP_BACK_CONN_ISBOUND( lc );
1886 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1887 * to existing server-side controls if required; if not,
1888 * the existing server-side controls are placed in *pctrls.
1889 * The caller, after using the controls in client API
1890 * operations, if ( *pctrls != op->o_ctrls ), should
1891 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1892 * The function returns success if the control could
1893 * be added if required, or if it did nothing; in the future,
1894 * it might return some error if it failed.
1896 * if no bind took place yet, but the connection is bound
1897 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1898 * and explicitly add proxyAuthz the control to every operation
1899 * with the dn bound to the connection as control value.
1901 * If no server-side controls are defined for the operation,
1902 * simply add the proxyAuthz control; otherwise, if the
1903 * proxyAuthz control is not already set, add it as
1906 * FIXME: is controls order significant for security?
1907 * ANSWER: controls ordering and interoperability
1908 * must be indicated by the specs of each control; if none
1909 * is specified, the order is irrelevant.
1912 ldap_back_proxy_authz_ctrl(
1913 struct berval *bound_ndn,
1915 slap_idassert_t *si,
1918 LDAPControl ***pctrls )
1920 LDAPControl **ctrls = NULL;
1922 slap_idassert_mode_t mode;
1923 struct berval assertedID,
1928 rs->sr_err = LDAP_SUCCESS;
1930 /* don't proxyAuthz if protocol is not LDAPv3 */
1931 switch ( version ) {
1936 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1945 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1946 * but if it is not set this test fails. We need a different
1947 * means to detect if idassert is enabled */
1948 if ( ( BER_BVISNULL( &si->si_bc.sb_authcId ) || BER_BVISEMPTY( &si->si_bc.sb_authcId ) )
1949 && ( BER_BVISNULL( &si->si_bc.sb_binddn ) || BER_BVISEMPTY( &si->si_bc.sb_binddn ) ) )
1954 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1958 if ( op->o_tag == LDAP_REQ_BIND ) {
1959 ndn = op->o_req_ndn;
1961 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1962 ndn = op->o_conn->c_ndn;
1968 if ( si->si_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1969 if ( op->o_proxy_authz ) {
1971 * FIXME: we do not want to perform proxyAuthz
1972 * on behalf of the client, because this would
1973 * be performed with "proxyauthzdn" privileges.
1975 * This might actually be too strict, since
1976 * the "proxyauthzdn" authzTo, and each entry's
1977 * authzFrom attributes may be crafted
1978 * to avoid unwanted proxyAuthz to take place.
1981 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1982 rs->sr_text = "proxyAuthz not allowed within namingContext";
1987 if ( !BER_BVISNULL( bound_ndn ) ) {
1991 if ( BER_BVISNULL( &ndn ) ) {
1995 if ( BER_BVISNULL( &si->si_bc.sb_binddn ) ) {
1999 } else if ( si->si_bc.sb_method == LDAP_AUTH_SASL ) {
2000 if ( ( si->si_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2002 /* already asserted in SASL via native authz */
2006 } else if ( si->si_authz && !be_isroot( op ) ) {
2008 struct berval authcDN;
2010 if ( BER_BVISNULL( &ndn ) ) {
2011 authcDN = slap_empty_bv;
2015 rc = slap_sasl_matches( op, si->si_authz,
2016 &authcDN, & authcDN );
2017 if ( rc != LDAP_SUCCESS ) {
2018 if ( si->si_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2019 /* ndn is not authorized
2020 * to use idassert */
2027 if ( op->o_proxy_authz ) {
2030 * 1) ignore the already set proxyAuthz control
2031 * 2) leave it in place, and don't set ours
2033 * 4) reject the operation
2035 * option (4) is very drastic
2036 * option (3) will make the remote server reject
2037 * the operation, thus being equivalent to (4)
2038 * option (2) will likely break the idassert
2039 * assumptions, so we cannot accept it;
2040 * option (1) means that we are contradicting
2041 * the client's reques.
2043 * I think (4) is the only correct choice.
2045 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2046 rs->sr_text = "proxyAuthz not allowed within namingContext";
2049 if ( op->o_is_auth_check ) {
2050 mode = LDAP_BACK_IDASSERT_NOASSERT;
2057 case LDAP_BACK_IDASSERT_SELF:
2058 if ( BER_BVISNULL( &ndn ) ) {
2064 case LDAP_BACK_IDASSERT_LEGACY:
2065 /* original behavior:
2066 * assert the client's identity */
2067 if ( BER_BVISNULL( &ndn ) ) {
2068 assertedID = slap_empty_bv;
2074 case LDAP_BACK_IDASSERT_ANONYMOUS:
2075 /* assert "anonymous" */
2076 assertedID = slap_empty_bv;
2079 case LDAP_BACK_IDASSERT_NOASSERT:
2080 /* don't assert; bind as proxyauthzdn */
2083 case LDAP_BACK_IDASSERT_OTHERID:
2084 case LDAP_BACK_IDASSERT_OTHERDN:
2085 /* assert idassert DN */
2086 assertedID = si->si_bc.sb_authzId;
2093 if ( BER_BVISNULL( &assertedID ) ) {
2094 assertedID = slap_empty_bv;
2097 /* don't idassert the bound DN (ITS#4497) */
2098 if ( dn_match( &assertedID, bound_ndn ) ) {
2102 if ( op->o_ctrls ) {
2103 for ( i = 0; op->o_ctrls[ i ]; i++ )
2104 /* just count ctrls */ ;
2107 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
2109 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
2111 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
2112 ctrls[ 0 ]->ldctl_iscritical = 1;
2114 switch ( si->si_mode ) {
2115 /* already in u:ID or dn:DN form */
2116 case LDAP_BACK_IDASSERT_OTHERID:
2117 case LDAP_BACK_IDASSERT_OTHERDN:
2118 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
2121 /* needs the dn: prefix */
2123 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
2124 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
2126 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
2127 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
2128 assertedID.bv_val, assertedID.bv_len + 1 );
2132 /* Older versions of <draft-weltman-ldapv3-proxy> required
2133 * to encode the value of the authzID (and called it proxyDN);
2134 * this hack provides compatibility with those DSAs that
2135 * implement it this way */
2136 if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
2137 struct berval authzID = ctrls[ 0 ]->ldctl_value;
2138 BerElementBuffer berbuf;
2139 BerElement *ber = (BerElement *)&berbuf;
2142 ber_init2( ber, 0, LBER_USE_DER );
2143 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2145 tag = ber_printf( ber, "O", &authzID );
2146 if ( tag == LBER_ERROR ) {
2147 rs->sr_err = LDAP_OTHER;
2151 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
2152 rs->sr_err = LDAP_OTHER;
2157 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2158 ber_free_buf( ber );
2160 if ( rs->sr_err != LDAP_SUCCESS ) {
2161 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2166 } else if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
2167 struct berval authzID = ctrls[ 0 ]->ldctl_value,
2169 BerElementBuffer berbuf;
2170 BerElement *ber = (BerElement *)&berbuf;
2173 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
2174 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
2175 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2177 rs->sr_err = LDAP_PROTOCOL_ERROR;
2182 tmp.bv_val += STRLENOF( "dn:" );
2183 tmp.bv_len -= STRLENOF( "dn:" );
2185 ber_init2( ber, 0, LBER_USE_DER );
2186 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2188 /* apparently, Mozilla API encodes this
2189 * as "SEQUENCE { LDAPDN }" */
2190 tag = ber_printf( ber, "{O}", &tmp );
2191 if ( tag == LBER_ERROR ) {
2192 rs->sr_err = LDAP_OTHER;
2196 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
2197 rs->sr_err = LDAP_OTHER;
2202 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2203 ber_free_buf( ber );
2205 if ( rs->sr_err != LDAP_SUCCESS ) {
2206 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2211 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
2214 if ( op->o_ctrls ) {
2215 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
2216 ctrls[ i + 1 ] = op->o_ctrls[ i ];
2219 ctrls[ i + 1 ] = NULL;
2222 if ( ctrls == NULL ) {
2223 ctrls = op->o_ctrls;
2232 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
2234 LDAPControl **ctrls = *pctrls;
2236 /* we assume that the first control is the proxyAuthz
2237 * added by back-ldap, so it's the only one we explicitly
2239 if ( ctrls && ctrls != op->o_ctrls ) {
2240 assert( ctrls[ 0 ] != NULL );
2242 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
2243 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
2246 op->o_tmpfree( ctrls, op->o_tmpmemctx );