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 assert( lc->lc_refcnt == 1 );
1211 ldap_back_freeconn( op, lc, dolock );
1213 rs->sr_err = slap_map_api2result( rs );
1215 if ( LDAP_BACK_QUARANTINE( li ) ) {
1216 ldap_back_quarantine( op, rs );
1219 if ( rs->sr_err != LDAP_SUCCESS &&
1220 ( sendok & LDAP_BACK_SENDERR ) )
1222 send_ldap_result( op, rs );
1228 rc = ldap_back_op_result( lc, op, rs, msgid,
1229 -1, (sendok|LDAP_BACK_BINDING) );
1230 if ( rc == LDAP_SUCCESS ) {
1231 LDAP_BACK_CONN_ISBOUND_SET( lc );
1236 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1237 rc = LDAP_BACK_CONN_ISBOUND( lc );
1239 ldap_back_release_conn_lock( op, rs, lcp, dolock );
1241 } else if ( LDAP_BACK_SAVECRED( li ) ) {
1242 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1251 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1254 ldap_back_dobind( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1256 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1258 return ldap_back_dobind_int( lcp, op, rs,
1259 ( sendok | LDAP_BACK_GETCONN ), li->li_nretries, 1 );
1263 * ldap_back_default_rebind
1265 * This is a callback used for chasing referrals using the same
1266 * credentials as the original user on this session.
1269 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1270 ber_int_t msgid, void *params )
1272 ldapconn_t *lc = (ldapconn_t *)params;
1275 /* ... otherwise we couldn't get here */
1276 assert( lc != NULL );
1278 if ( !ldap_tls_inplace( ld ) ) {
1279 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1281 const char *text = NULL;
1283 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1284 LDAP_BACK_RETRY_DEFAULT, &text );
1285 if ( rc != LDAP_SUCCESS ) {
1289 #endif /* HAVE_TLS */
1291 /* FIXME: add checks on the URL/identity? */
1293 return ldap_sasl_bind_s( ld,
1294 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1295 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1299 * ldap_back_default_urllist
1302 ldap_back_default_urllist(
1304 LDAPURLDesc **urllist,
1308 ldapinfo_t *li = (ldapinfo_t *)params;
1309 LDAPURLDesc **urltail;
1311 if ( urllist == url ) {
1312 return LDAP_SUCCESS;
1315 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1318 *urltail = *urllist;
1322 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1324 ch_free( li->li_uri );
1327 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1328 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1330 return LDAP_SUCCESS;
1339 ldap_back_send_t sendok )
1341 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1343 /* default behavior */
1344 if ( LDAP_BACK_ABANDON( li ) ) {
1345 return ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1348 if ( LDAP_BACK_IGNORE( li ) ) {
1349 return ldap_pvt_discard( lc->lc_ld, msgid );
1352 if ( LDAP_BACK_CANCEL( li ) ) {
1353 /* FIXME: asynchronous? */
1354 return ldap_cancel_s( lc->lc_ld, msgid, NULL, NULL );
1363 ldap_back_op_result(
1369 ldap_back_send_t sendok )
1371 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1376 LDAPControl **ctrls = NULL;
1378 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
1381 rs->sr_matched = NULL;
1383 rs->sr_ctrls = NULL;
1385 /* if the error recorded in the reply corresponds
1386 * to a successful state, get the error from the
1387 * remote server response */
1388 if ( ERR_OK( rs->sr_err ) ) {
1391 LDAPMessage *res = NULL;
1392 time_t stoptime = (time_t)(-1);
1393 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
1394 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
1395 const char *timeout_text = "Operation timed out";
1397 /* if timeout is not specified, compute and use
1398 * the one specific to the ongoing operation */
1399 if ( timeout == (time_t)(-1) ) {
1400 slap_op_t opidx = slap_req2op( op->o_tag );
1402 if ( opidx == SLAP_OP_SEARCH ) {
1403 if ( op->ors_tlimit <= 0 ) {
1407 timeout = op->ors_tlimit;
1408 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
1409 timeout_text = NULL;
1413 timeout = li->li_timeout[ opidx ];
1417 /* better than nothing :) */
1418 if ( timeout == 0 ) {
1419 if ( li->li_idle_timeout ) {
1420 timeout = li->li_idle_timeout;
1422 } else if ( li->li_conn_ttl ) {
1423 timeout = li->li_conn_ttl;
1428 stoptime = op->o_time + timeout;
1431 LDAP_BACK_TV_SET( &tv );
1434 /* if result parsing fails, note the failure reason */
1435 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1438 if ( timeout && slap_get_time() > stoptime ) {
1439 if ( sendok & LDAP_BACK_BINDING ) {
1440 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1442 LDAP_BACK_CONN_TAINTED_SET( lc );
1445 (void)ldap_back_cancel( lc, op, rs, msgid, sendok );
1447 rs->sr_err = timeout_err;
1448 rs->sr_text = timeout_text;
1453 LDAP_BACK_TV_SET( &tv );
1454 ldap_pvt_thread_yield();
1458 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1463 /* otherwise get the result; if it is not
1464 * LDAP_SUCCESS, record it in the reply
1465 * structure (this includes
1466 * LDAP_COMPARE_{TRUE|FALSE}) */
1468 /* only touch when activity actually took place... */
1469 if ( li->li_idle_timeout ) {
1470 lc->lc_time = op->o_time;
1473 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1474 &match, &text, &refs, &ctrls, 1 );
1476 if ( rc != LDAP_SUCCESS ) {
1479 if ( refs != NULL ) {
1482 for ( i = 0; refs[ i ] != NULL; i++ )
1484 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1486 for ( i = 0; refs[ i ] != NULL; i++ ) {
1487 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1489 BER_BVZERO( &rs->sr_ref[ i ] );
1491 if ( ctrls != NULL ) {
1492 rs->sr_ctrls = ctrls;
1497 /* if the error in the reply structure is not
1498 * LDAP_SUCCESS, try to map it from client
1499 * to server error */
1500 if ( !ERR_OK( rs->sr_err ) ) {
1501 rs->sr_err = slap_map_api2result( rs );
1503 /* internal ops ( op->o_conn == NULL )
1504 * must not reply to client */
1505 if ( op->o_conn && !op->o_do_not_cache && match ) {
1507 /* record the (massaged) matched
1508 * DN into the reply structure */
1509 rs->sr_matched = match;
1513 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1514 if ( !( sendok & LDAP_BACK_RETRYING ) ) {
1515 if ( LDAP_BACK_QUARANTINE( li ) ) {
1516 ldap_back_quarantine( op, rs );
1518 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1519 send_ldap_result( op, rs );
1523 } else if ( op->o_conn &&
1524 ( ( ( sendok & LDAP_BACK_SENDOK ) && ERR_OK( rs->sr_err ) )
1525 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1527 send_ldap_result( op, rs );
1531 if ( rs->sr_matched != match ) {
1532 free( (char *)rs->sr_matched );
1534 rs->sr_matched = NULL;
1535 ldap_memfree( match );
1539 ldap_memfree( text );
1544 assert( refs != NULL );
1545 ber_memvfree( (void **)refs );
1546 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1551 assert( rs->sr_ctrls != NULL );
1552 ldap_controls_free( ctrls );
1553 rs->sr_ctrls = NULL;
1556 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1559 /* return true if bound, false if failed */
1561 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1564 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1566 assert( lcp != NULL );
1567 assert( *lcp != NULL );
1569 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1571 if ( (*lcp)->lc_refcnt == 1 ) {
1572 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1573 Debug( LDAP_DEBUG_ANY,
1574 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1575 op->o_log_prefix, li->li_uri,
1576 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1577 "" : (*lcp)->lc_bound_ndn.bv_val );
1578 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1580 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1581 (*lcp)->lc_ld = NULL;
1582 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1584 /* lc here must be the regular lc, reset and ready for init */
1585 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
1586 if ( rc != LDAP_SUCCESS ) {
1587 /* freeit, because lc_refcnt == 1 */
1588 (*lcp)->lc_refcnt = 0;
1589 (void)ldap_back_freeconn( op, *lcp, 0 );
1594 rc = ldap_back_dobind_int( lcp, op, rs, sendok, 0, 0 );
1595 if ( rc == 0 && *lcp != NULL ) {
1596 /* freeit, because lc_refcnt == 1 */
1597 (*lcp)->lc_refcnt = 0;
1598 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1599 (void)ldap_back_freeconn( op, *lcp, 0 );
1605 Debug( LDAP_DEBUG_TRACE,
1606 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1607 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1609 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1610 ldap_back_release_conn_lock( op, rs, lcp, 0 );
1611 assert( *lcp == NULL );
1614 rs->sr_err = LDAP_UNAVAILABLE;
1615 rs->sr_text = "unable to retry";
1616 send_ldap_result( op, rs );
1620 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1626 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
1627 struct berval *binddn, struct berval *bindcred )
1629 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1633 if ( op->o_conn == NULL || op->o_do_not_cache ) {
1637 /* don't proxyAuthz if protocol is not LDAPv3 */
1638 switch ( li->li_version ) {
1643 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1649 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1650 if ( sendok & LDAP_BACK_SENDERR ) {
1651 send_ldap_result( op, rs );
1657 *binddn = slap_empty_bv;
1658 *bindcred = slap_empty_bv;
1660 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1661 ndn = op->o_conn->c_ndn;
1667 switch ( li->li_idassert_mode ) {
1668 case LDAP_BACK_IDASSERT_LEGACY:
1669 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1670 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1672 *binddn = li->li_idassert_authcDN;
1673 *bindcred = li->li_idassert_passwd;
1680 /* NOTE: rootdn can always idassert */
1681 if ( BER_BVISNULL( &ndn ) && li->li_idassert_authz == NULL ) {
1682 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1683 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1684 if ( sendok & LDAP_BACK_SENDERR ) {
1685 send_ldap_result( op, rs );
1689 rs->sr_err = LDAP_SUCCESS;
1690 *binddn = slap_empty_bv;
1691 *bindcred = slap_empty_bv;
1697 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1698 struct berval authcDN;
1700 if ( BER_BVISNULL( &ndn ) ) {
1701 authcDN = slap_empty_bv;
1706 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1707 &authcDN, &authcDN );
1708 if ( rs->sr_err != LDAP_SUCCESS ) {
1709 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1710 if ( sendok & LDAP_BACK_SENDERR ) {
1711 send_ldap_result( op, rs );
1715 rs->sr_err = LDAP_SUCCESS;
1716 *binddn = slap_empty_bv;
1717 *bindcred = slap_empty_bv;
1725 *binddn = li->li_idassert_authcDN;
1726 *bindcred = li->li_idassert_passwd;
1736 ldap_back_proxy_authz_bind(
1740 ldap_back_send_t sendok,
1741 struct berval *binddn,
1742 struct berval *bindcred )
1744 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1749 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1750 ndn = op->o_conn->c_ndn;
1756 if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1757 #ifdef HAVE_CYRUS_SASL
1758 void *defaults = NULL;
1759 struct berval authzID = BER_BVNULL;
1762 /* if SASL supports native authz, prepare for it */
1763 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1764 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1766 switch ( li->li_idassert_mode ) {
1767 case LDAP_BACK_IDASSERT_OTHERID:
1768 case LDAP_BACK_IDASSERT_OTHERDN:
1769 authzID = li->li_idassert_authzID;
1772 case LDAP_BACK_IDASSERT_ANONYMOUS:
1773 BER_BVSTR( &authzID, "dn:" );
1776 case LDAP_BACK_IDASSERT_SELF:
1777 if ( BER_BVISNULL( &ndn ) ) {
1778 /* connection is not authc'd, so don't idassert */
1779 BER_BVSTR( &authzID, "dn:" );
1782 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1783 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1784 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1785 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1786 ndn.bv_val, ndn.bv_len + 1 );
1795 if ( li->li_idassert_secprops != NULL ) {
1796 rs->sr_err = ldap_set_option( lc->lc_ld,
1797 LDAP_OPT_X_SASL_SECPROPS,
1798 (void *)li->li_idassert_secprops );
1800 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1801 rs->sr_err = LDAP_OTHER;
1802 if ( sendok & LDAP_BACK_SENDERR ) {
1803 send_ldap_result( op, rs );
1805 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1810 defaults = lutil_sasl_defaults( lc->lc_ld,
1811 li->li_idassert_sasl_mech.bv_val,
1812 li->li_idassert_sasl_realm.bv_val,
1813 li->li_idassert_authcID.bv_val,
1814 li->li_idassert_passwd.bv_val,
1817 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn->bv_val,
1818 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1819 LDAP_SASL_QUIET, lutil_sasl_interact,
1822 rs->sr_err = slap_map_api2result( rs );
1823 if ( rs->sr_err != LDAP_SUCCESS ) {
1824 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1825 if ( sendok & LDAP_BACK_SENDERR ) {
1826 send_ldap_result( op, rs );
1830 LDAP_BACK_CONN_ISBOUND_SET( lc );
1833 lutil_sasl_freedefs( defaults );
1835 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1839 #endif /* HAVE_CYRUS_SASL */
1842 switch ( li->li_idassert_authmethod ) {
1843 case LDAP_AUTH_NONE:
1844 /* FIXME: do we really need this? */
1845 BER_BVSTR( binddn, "" );
1846 BER_BVSTR( bindcred, "" );
1849 case LDAP_AUTH_SIMPLE:
1850 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1851 binddn->bv_val, LDAP_SASL_SIMPLE,
1852 bindcred, NULL, NULL, &msgid );
1853 rc = ldap_back_op_result( lc, op, rs, msgid,
1854 -1, (sendok|LDAP_BACK_BINDING) );
1859 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1860 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1861 if ( sendok & LDAP_BACK_SENDERR ) {
1862 send_ldap_result( op, rs );
1867 if ( rc == LDAP_SUCCESS ) {
1868 /* set rebind stuff in case of successful proxyAuthz bind,
1869 * so that referral chasing is attempted using the right
1871 LDAP_BACK_CONN_ISBOUND_SET( lc );
1872 ber_bvreplace( &lc->lc_bound_ndn, binddn );
1874 if ( LDAP_BACK_SAVECRED( li ) ) {
1875 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
1876 memset( lc->lc_cred.bv_val, 0,
1877 lc->lc_cred.bv_len );
1879 ber_bvreplace( &lc->lc_cred, bindcred );
1880 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1884 return LDAP_BACK_CONN_ISBOUND( lc );
1888 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1889 * to existing server-side controls if required; if not,
1890 * the existing server-side controls are placed in *pctrls.
1891 * The caller, after using the controls in client API
1892 * operations, if ( *pctrls != op->o_ctrls ), should
1893 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1894 * The function returns success if the control could
1895 * be added if required, or if it did nothing; in the future,
1896 * it might return some error if it failed.
1898 * if no bind took place yet, but the connection is bound
1899 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1900 * and explicitly add proxyAuthz the control to every operation
1901 * with the dn bound to the connection as control value.
1903 * If no server-side controls are defined for the operation,
1904 * simply add the proxyAuthz control; otherwise, if the
1905 * proxyAuthz control is not already set, add it as
1908 * FIXME: is controls order significant for security?
1909 * ANSWER: controls ordering and interoperability
1910 * must be indicated by the specs of each control; if none
1911 * is specified, the order is irrelevant.
1914 ldap_back_proxy_authz_ctrl(
1915 struct berval *bound_ndn,
1917 slap_idassert_t *si,
1920 LDAPControl ***pctrls )
1922 LDAPControl **ctrls = NULL;
1924 slap_idassert_mode_t mode;
1925 struct berval assertedID,
1930 rs->sr_err = LDAP_SUCCESS;
1932 /* don't proxyAuthz if protocol is not LDAPv3 */
1933 switch ( version ) {
1938 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1947 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1948 * but if it is not set this test fails. We need a different
1949 * means to detect if idassert is enabled */
1950 if ( ( BER_BVISNULL( &si->si_bc.sb_authcId ) || BER_BVISEMPTY( &si->si_bc.sb_authcId ) )
1951 && ( BER_BVISNULL( &si->si_bc.sb_binddn ) || BER_BVISEMPTY( &si->si_bc.sb_binddn ) ) )
1956 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1960 if ( op->o_tag == LDAP_REQ_BIND ) {
1961 ndn = op->o_req_ndn;
1963 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1964 ndn = op->o_conn->c_ndn;
1970 if ( si->si_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1971 if ( op->o_proxy_authz ) {
1973 * FIXME: we do not want to perform proxyAuthz
1974 * on behalf of the client, because this would
1975 * be performed with "proxyauthzdn" privileges.
1977 * This might actually be too strict, since
1978 * the "proxyauthzdn" authzTo, and each entry's
1979 * authzFrom attributes may be crafted
1980 * to avoid unwanted proxyAuthz to take place.
1983 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1984 rs->sr_text = "proxyAuthz not allowed within namingContext";
1989 if ( !BER_BVISNULL( bound_ndn ) ) {
1993 if ( BER_BVISNULL( &ndn ) ) {
1997 if ( BER_BVISNULL( &si->si_bc.sb_binddn ) ) {
2001 } else if ( si->si_bc.sb_method == LDAP_AUTH_SASL ) {
2002 if ( ( si->si_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2004 /* already asserted in SASL via native authz */
2008 } else if ( si->si_authz && !be_isroot( op ) ) {
2010 struct berval authcDN;
2012 if ( BER_BVISNULL( &ndn ) ) {
2013 authcDN = slap_empty_bv;
2017 rc = slap_sasl_matches( op, si->si_authz,
2018 &authcDN, & authcDN );
2019 if ( rc != LDAP_SUCCESS ) {
2020 if ( si->si_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2021 /* ndn is not authorized
2022 * to use idassert */
2029 if ( op->o_proxy_authz ) {
2032 * 1) ignore the already set proxyAuthz control
2033 * 2) leave it in place, and don't set ours
2035 * 4) reject the operation
2037 * option (4) is very drastic
2038 * option (3) will make the remote server reject
2039 * the operation, thus being equivalent to (4)
2040 * option (2) will likely break the idassert
2041 * assumptions, so we cannot accept it;
2042 * option (1) means that we are contradicting
2043 * the client's reques.
2045 * I think (4) is the only correct choice.
2047 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2048 rs->sr_text = "proxyAuthz not allowed within namingContext";
2051 if ( op->o_is_auth_check ) {
2052 mode = LDAP_BACK_IDASSERT_NOASSERT;
2059 case LDAP_BACK_IDASSERT_SELF:
2060 if ( BER_BVISNULL( &ndn ) ) {
2066 case LDAP_BACK_IDASSERT_LEGACY:
2067 /* original behavior:
2068 * assert the client's identity */
2069 if ( BER_BVISNULL( &ndn ) ) {
2070 assertedID = slap_empty_bv;
2076 case LDAP_BACK_IDASSERT_ANONYMOUS:
2077 /* assert "anonymous" */
2078 assertedID = slap_empty_bv;
2081 case LDAP_BACK_IDASSERT_NOASSERT:
2082 /* don't assert; bind as proxyauthzdn */
2085 case LDAP_BACK_IDASSERT_OTHERID:
2086 case LDAP_BACK_IDASSERT_OTHERDN:
2087 /* assert idassert DN */
2088 assertedID = si->si_bc.sb_authzId;
2095 if ( BER_BVISNULL( &assertedID ) ) {
2096 assertedID = slap_empty_bv;
2099 /* don't idassert the bound DN (ITS#4497) */
2100 if ( dn_match( &assertedID, bound_ndn ) ) {
2104 if ( op->o_ctrls ) {
2105 for ( i = 0; op->o_ctrls[ i ]; i++ )
2106 /* just count ctrls */ ;
2109 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
2111 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
2113 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
2114 ctrls[ 0 ]->ldctl_iscritical = 1;
2116 switch ( si->si_mode ) {
2117 /* already in u:ID or dn:DN form */
2118 case LDAP_BACK_IDASSERT_OTHERID:
2119 case LDAP_BACK_IDASSERT_OTHERDN:
2120 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
2123 /* needs the dn: prefix */
2125 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
2126 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
2128 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
2129 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
2130 assertedID.bv_val, assertedID.bv_len + 1 );
2134 /* Older versions of <draft-weltman-ldapv3-proxy> required
2135 * to encode the value of the authzID (and called it proxyDN);
2136 * this hack provides compatibility with those DSAs that
2137 * implement it this way */
2138 if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
2139 struct berval authzID = ctrls[ 0 ]->ldctl_value;
2140 BerElementBuffer berbuf;
2141 BerElement *ber = (BerElement *)&berbuf;
2144 ber_init2( ber, 0, LBER_USE_DER );
2145 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2147 tag = ber_printf( ber, "O", &authzID );
2148 if ( tag == LBER_ERROR ) {
2149 rs->sr_err = LDAP_OTHER;
2153 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
2154 rs->sr_err = LDAP_OTHER;
2159 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2160 ber_free_buf( ber );
2162 if ( rs->sr_err != LDAP_SUCCESS ) {
2163 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2168 } else if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
2169 struct berval authzID = ctrls[ 0 ]->ldctl_value,
2171 BerElementBuffer berbuf;
2172 BerElement *ber = (BerElement *)&berbuf;
2175 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
2176 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
2177 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2179 rs->sr_err = LDAP_PROTOCOL_ERROR;
2184 tmp.bv_val += STRLENOF( "dn:" );
2185 tmp.bv_len -= STRLENOF( "dn:" );
2187 ber_init2( ber, 0, LBER_USE_DER );
2188 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2190 /* apparently, Mozilla API encodes this
2191 * as "SEQUENCE { LDAPDN }" */
2192 tag = ber_printf( ber, "{O}", &tmp );
2193 if ( tag == LBER_ERROR ) {
2194 rs->sr_err = LDAP_OTHER;
2198 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
2199 rs->sr_err = LDAP_OTHER;
2204 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2205 ber_free_buf( ber );
2207 if ( rs->sr_err != LDAP_SUCCESS ) {
2208 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2213 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
2216 if ( op->o_ctrls ) {
2217 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
2218 ctrls[ i + 1 ] = op->o_ctrls[ i ];
2221 ctrls[ i + 1 ] = NULL;
2224 if ( ctrls == NULL ) {
2225 ctrls = op->o_ctrls;
2234 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
2236 LDAPControl **ctrls = *pctrls;
2238 /* we assume that the first control is the proxyAuthz
2239 * added by back-ldap, so it's the only one we explicitly
2241 if ( ctrls && ctrls != op->o_ctrls ) {
2242 assert( ctrls[ 0 ] != NULL );
2244 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
2245 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
2248 op->o_tmpfree( ctrls, op->o_tmpmemctx );