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;
112 ldap_back_send_t retrying = LDAP_BACK_RETRYING;
114 lc = ldap_back_getconn( op, rs, LDAP_BACK_BIND_SERR, NULL, NULL );
119 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
120 ch_free( lc->lc_bound_ndn.bv_val );
121 BER_BVZERO( &lc->lc_bound_ndn );
123 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
126 /* method is always LDAP_AUTH_SIMPLE if we got here */
127 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
129 &op->orb_cred, op->o_ctrls, NULL, &msgid );
130 /* FIXME: should we always retry, or only when piping the bind
131 * in the "override" connection pool? */
132 rc = ldap_back_op_result( lc, op, rs, msgid,
133 li->li_timeout[ SLAP_OP_BIND ],
134 LDAP_BACK_BIND_SERR | retrying );
135 if ( rc == LDAP_UNAVAILABLE && retrying ) {
136 retrying &= ~LDAP_BACK_RETRYING;
137 if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_BIND_SERR ) ) {
142 if ( rc == LDAP_SUCCESS ) {
143 /* If defined, proxyAuthz will be used also when
144 * back-ldap is the authorizing backend; for this
145 * purpose, after a successful bind the connection
146 * is left for further binds, and further operations
147 * on this client connection will use a default
148 * connection with identity assertion */
149 /* NOTE: use with care */
150 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
151 assert( lc->lc_binding == 1 );
153 ldap_back_release_conn( op, rs, lc );
157 /* rebind is now done inside ldap_back_proxy_authz_bind()
158 * in case of success */
159 LDAP_BACK_CONN_ISBOUND_SET( lc );
160 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
162 if ( LDAP_BACK_SAVECRED( li ) ) {
163 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
164 memset( lc->lc_cred.bv_val, 0,
165 lc->lc_cred.bv_len );
167 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
168 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
172 assert( lc->lc_binding == 1 );
175 /* must re-insert if local DN changed as result of bind */
176 if ( !LDAP_BACK_CONN_ISBOUND( lc )
177 || ( LDAP_BACK_CONN_ISBOUND( lc )
178 && !dn_match( &op->o_req_ndn, &lc->lc_local_ndn ) ) )
183 /* wait for all other ops to release the connection */
185 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
186 if ( lc->lc_refcnt > 1 ) {
187 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
188 ldap_pvt_thread_yield();
192 #if LDAP_BACK_PRINT_CONNTREE > 0
193 ldap_back_print_conntree( li->li_conninfo.lai_tree, ">>> ldap_back_bind" );
194 #endif /* LDAP_BACK_PRINT_CONNTREE */
196 assert( lc->lc_refcnt == 1 );
197 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
198 ldap_back_conndnlc_cmp );
199 assert( tmplc == NULL || lc == tmplc );
201 /* delete all cached connections with the current connection */
202 if ( LDAP_BACK_SINGLECONN( li ) ) {
203 while ( ( tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc, ldap_back_conn_cmp ) ) != NULL )
205 Debug( LDAP_DEBUG_TRACE,
206 "=>ldap_back_bind: destroying conn %ld (refcnt=%u)\n",
207 LDAP_BACK_PCONN_ID( lc ), lc->lc_refcnt, 0 );
209 if ( tmplc->lc_refcnt != 0 ) {
211 LDAP_BACK_CONN_TAINTED_SET( tmplc );
215 * Needs a test because the handler may be corrupted,
216 * and calling ldap_unbind on a corrupted header results
217 * in a segmentation fault
219 ldap_back_conn_free( tmplc );
224 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
225 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
226 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
227 lc->lc_conn = LDAP_BACK_PCONN_SET( op );
229 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
230 ldap_back_conndn_cmp, ldap_back_conndn_dup );
233 #if LDAP_BACK_PRINT_CONNTREE > 0
234 ldap_back_print_conntree( li->li_conninfo.lai_tree, "<<< ldap_back_bind" );
235 #endif /* LDAP_BACK_PRINT_CONNTREE */
237 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
243 /* duplicate; someone else successfully bound
244 * on the same connection with the same identity;
245 * we can do this because lc_refcnt == 1 */
246 ldap_back_conn_free( lc );
252 ldap_back_release_conn( op, rs, lc );
259 * ldap_back_conndn_cmp
261 * compares two ldapconn_t based on the value of the conn pointer
262 * and of the local DN; used by avl stuff for insert, lookup
266 ldap_back_conndn_cmp( const void *c1, const void *c2 )
268 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
269 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
272 /* If local DNs don't match, it is definitely not a match */
273 /* For shared sessions, conn is NULL. Only explicitly
274 * bound sessions will have non-NULL conn.
276 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
278 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
285 * ldap_back_conndnlc_cmp
287 * compares two ldapconn_t based on the value of the conn pointer,
288 * the local DN and the lc pointer; used by avl stuff for insert, lookup
292 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
294 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
295 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
298 /* If local DNs don't match, it is definitely not a match */
299 /* For shared sessions, conn is NULL. Only explicitly
300 * bound sessions will have non-NULL conn.
302 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
304 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
306 rc = SLAP_PTRCMP( lc1, lc2 );
316 * compares two ldapconn_t based on the value of the conn pointer;
317 * used by avl stuff for delete of all conns with the same connid
320 ldap_back_conn_cmp( const void *c1, const void *c2 )
322 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
323 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
325 /* For shared sessions, conn is NULL. Only explicitly
326 * bound sessions will have non-NULL conn.
328 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
332 * ldap_back_conndn_dup
334 * returns -1 in case a duplicate ldapconn_t has been inserted;
338 ldap_back_conndn_dup( void *c1, void *c2 )
340 ldapconn_t *lc1 = (ldapconn_t *)c1;
341 ldapconn_t *lc2 = (ldapconn_t *)c2;
343 /* Cannot have more than one shared session with same DN */
344 if ( lc1->lc_conn == lc2->lc_conn &&
345 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
354 ldap_back_freeconn( Operation *op, ldapconn_t *lc, int dolock )
356 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
360 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
363 #if LDAP_BACK_PRINT_CONNTREE > 0
364 ldap_back_print_conntree( li->li_conninfo.lai_tree, ">>> ldap_back_freeconn" );
365 #endif /* LDAP_BACK_PRINT_CONNTREE */
367 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
368 ldap_back_conndnlc_cmp );
369 assert( LDAP_BACK_CONN_TAINTED( lc ) || tmplc == lc );
370 if ( lc->lc_refcnt == 0 ) {
371 ldap_back_conn_free( (void *)lc );
374 #if LDAP_BACK_PRINT_CONNTREE > 0
375 ldap_back_print_conntree( li->li_conninfo.lai_tree, "<<< ldap_back_freeconn" );
376 #endif /* LDAP_BACK_PRINT_CONNTREE */
379 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
396 int rc = LDAP_SUCCESS;
399 /* this is ridiculous... */
400 dummy.li_flags = flags;
402 /* start TLS ("tls-[try-]{start,propagate}" statements) */
403 if ( ( LDAP_BACK_USE_TLS( &dummy ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS( &dummy ) ) )
404 && !ldap_is_ldaps_url( url ) )
406 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
408 * use asynchronous StartTLS
409 * in case, chase referral (not implemented yet)
413 if ( protocol == 0 ) {
414 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
418 if ( protocol < LDAP_VERSION3 ) {
419 /* we should rather bail out... */
420 rc = LDAP_UNWILLING_TO_PERFORM;
421 *text = "invalid protocol version";
424 if ( rc == LDAP_SUCCESS ) {
425 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
428 if ( rc == LDAP_SUCCESS ) {
429 LDAPMessage *res = NULL;
432 LDAP_BACK_TV_SET( &tv );
435 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
437 rc = LDAP_UNAVAILABLE;
439 } else if ( rc == 0 ) {
440 if ( retries != LDAP_BACK_RETRY_NEVER ) {
441 ldap_pvt_thread_yield();
445 LDAP_BACK_TV_SET( &tv );
448 rc = LDAP_UNAVAILABLE;
450 } else if ( rc == LDAP_RES_EXTENDED ) {
451 struct berval *data = NULL;
453 rc = ldap_parse_extended_result( ld, res,
455 if ( rc == LDAP_SUCCESS ) {
457 rc = ldap_parse_result( ld, res, &err,
458 NULL, NULL, NULL, NULL, 1 );
459 if ( rc == LDAP_SUCCESS ) {
464 /* FIXME: in case a referral
465 * is returned, should we try
466 * using it instead of the
468 if ( rc == LDAP_SUCCESS ) {
469 rc = ldap_install_tls( ld );
471 } else if ( rc == LDAP_REFERRAL ) {
472 rc = LDAP_UNWILLING_TO_PERFORM;
473 *text = "unwilling to chase referral returned by Start TLS exop";
477 if ( data->bv_val ) {
478 ber_memfree( data->bv_val );
492 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
494 * use synchronous StartTLS
496 rc = ldap_start_tls_s( ld, NULL, NULL );
497 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
499 /* if StartTLS is requested, only attempt it if the URL
500 * is not "ldaps://"; this may occur not only in case
501 * of misconfiguration, but also when used in the chain
502 * overlay, where the "uri" can be parsed out of a referral */
508 case LDAP_SERVER_DOWN:
512 if ( LDAP_BACK_TLS_CRITICAL( &dummy ) ) {
513 *text = "could not start TLS";
517 /* in case Start TLS is not critical */
529 #endif /* HAVE_TLS */
532 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
534 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
538 int is_tls = op->o_conn->c_is_tls;
539 time_t lc_time = (time_t)(-1);
540 #endif /* HAVE_TLS */
542 assert( lcp != NULL );
544 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
545 rs->sr_err = ldap_initialize( &ld, li->li_uri );
546 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
547 if ( rs->sr_err != LDAP_SUCCESS ) {
551 if ( li->li_urllist_f ) {
552 ldap_set_urllist_proc( ld, li->li_urllist_f, li->li_urllist_p );
555 /* Set LDAP version. This will always succeed: If the client
556 * bound with a particular version, then so can we.
558 if ( li->li_version != 0 ) {
559 version = li->li_version;
561 } else if ( op->o_protocol != 0 ) {
562 version = op->o_protocol;
565 /* assume it's an internal op; set to LDAPv3 */
566 version = LDAP_VERSION3;
568 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
570 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
571 ldap_set_option( ld, LDAP_OPT_REFERRALS,
572 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
574 if ( li->li_network_timeout > 0 ) {
577 tv.tv_sec = li->li_network_timeout;
579 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
583 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
584 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
585 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
586 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
587 if ( rs->sr_err != LDAP_SUCCESS ) {
588 ldap_unbind_ext( ld, NULL, NULL );
591 } else if ( li->li_idle_timeout ) {
592 /* only touch when activity actually took place... */
593 lc_time = op->o_time;
595 #endif /* HAVE_TLS */
597 if ( *lcp == NULL ) {
598 *lcp = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
599 (*lcp)->lc_flags = li->li_flags;
602 (*lcp)->lc_refcnt = 1;
603 (*lcp)->lc_binding = 1;
606 LDAP_BACK_CONN_ISTLS_SET( *lcp );
608 LDAP_BACK_CONN_ISTLS_CLEAR( *lcp );
610 if ( lc_time != (time_t)(-1) ) {
611 (*lcp)->lc_time = lc_time;
613 #endif /* HAVE_TLS */
616 if ( rs->sr_err != LDAP_SUCCESS ) {
617 rs->sr_err = slap_map_api2result( rs );
618 if ( sendok & LDAP_BACK_SENDERR ) {
619 if ( rs->sr_text == NULL ) {
620 rs->sr_text = "ldap_initialize() failed";
622 send_ldap_result( op, rs );
627 if ( li->li_conn_ttl > 0 ) {
628 (*lcp)->lc_create_time = op->o_time;
639 ldap_back_send_t sendok,
640 struct berval *binddn,
641 struct berval *bindcred )
643 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
644 ldapconn_t *lc = NULL,
648 lookupconn = !( sendok & LDAP_BACK_BINDING );
650 /* if the server is quarantined, and
651 * - the current interval did not expire yet, or
652 * - no more retries should occur,
653 * don't return the connection */
654 if ( li->li_isquarantined ) {
655 slap_retry_info_t *ri = &li->li_quarantine;
658 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
659 if ( li->li_isquarantined == LDAP_BACK_FQ_YES ) {
660 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
661 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
663 Debug( LDAP_DEBUG_ANY,
664 "%s: ldap_back_getconn quarantine "
665 "retry block #%d try #%d.\n",
666 op->o_log_prefix, ri->ri_idx, ri->ri_count );
667 li->li_isquarantined = LDAP_BACK_FQ_RETRYING;
670 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
673 rs->sr_err = LDAP_UNAVAILABLE;
674 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
675 send_ldap_result( op, rs );
681 /* Internal searches are privileged and shared. So is root. */
682 if ( op->o_do_not_cache || be_isroot( op ) ) {
683 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
684 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
685 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
688 struct berval tmpbinddn,
695 if ( binddn == NULL ) {
698 if ( bindcred == NULL ) {
699 bindcred = &tmpbindcred;
701 if ( op->o_tag == LDAP_REQ_BIND ) {
702 save_o_dn = op->o_dn;
703 save_o_ndn = op->o_ndn;
704 op->o_dn = op->o_req_dn;
705 op->o_ndn = op->o_req_ndn;
707 isproxyauthz = ldap_back_is_proxy_authz( op, rs, 0, binddn, bindcred );
708 if ( op->o_tag == LDAP_REQ_BIND ) {
709 op->o_dn = save_o_dn;
710 op->o_ndn = save_o_ndn;
713 lc_curr.lc_local_ndn = op->o_ndn;
714 /* Explicit binds must not be shared;
715 * however, explicit binds are piped in a special connection
716 * when idassert is to occur with "override" set */
717 if ( op->o_tag == LDAP_REQ_BIND && !isproxyauthz ) {
718 lc_curr.lc_conn = op->o_conn;
721 if ( isproxyauthz && !( sendok & LDAP_BACK_BINDING ) ) {
722 lc_curr.lc_local_ndn = *binddn;
723 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
724 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
726 } else if ( isproxyauthz && ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) {
727 lc_curr.lc_local_ndn = slap_empty_bv;
728 lc_curr.lc_conn = LDAP_BACK_PCONN_BIND_SET( op );
729 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
732 } else if ( SLAP_IS_AUTHZ_BACKEND( op ) ) {
733 lc_curr.lc_conn = op->o_conn;
736 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
741 /* Explicit Bind requests always get their own conn */
743 /* Searches for a ldapconn in the avl tree */
745 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
747 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
748 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
750 /* Don't reuse connections while they're still binding */
751 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
752 if ( !LDAP_BACK_USE_TEMPORARIES( li ) ) {
753 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
755 ldap_pvt_thread_yield();
763 if ( op->o_tag == LDAP_REQ_BIND ) {
764 /* right now, this is the only possible case */
765 assert( ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) );
766 LDAP_BACK_CONN_BINDING_SET( lc );
769 refcnt = ++lc->lc_refcnt;
770 binding = ++lc->lc_binding;
773 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
776 /* Looks like we didn't get a bind. Open a new session... */
778 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
782 if ( sendok & LDAP_BACK_BINDING ) {
783 LDAP_BACK_CONN_BINDING_SET( lc );
786 lc->lc_conn = lc_curr.lc_conn;
787 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
790 * the rationale is: connections as the rootdn are privileged,
791 * so acl_authcDN is to be used; however, in some cases
792 * one already configured identity assertion with a highly
793 * privileged idassert_authcDN, so if acl_authcDN is NULL
794 * and idassert_authcDN is not, use the second instead.
796 * might change in the future, because it's preferable
797 * to make clear what identity is being used, since
798 * the only drawback is that one risks to configure
799 * the same identity twice...
801 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
802 if ( BER_BVISNULL( &li->li_acl_authcDN ) && !BER_BVISNULL( &li->li_idassert_authcDN ) ) {
803 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
804 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
807 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
808 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
810 LDAP_BACK_CONN_ISPRIV_SET( lc );
812 } else if ( LDAP_BACK_CONN_ISIDASSERT( &lc_curr ) ) {
813 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
814 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
815 LDAP_BACK_CONN_ISIDASSERT_SET( lc );
818 BER_BVZERO( &lc->lc_cred );
819 BER_BVZERO( &lc->lc_bound_ndn );
820 if ( !BER_BVISEMPTY( &op->o_ndn )
821 && SLAP_IS_AUTHZ_BACKEND( op ) )
823 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
828 /* if start TLS failed but it was not mandatory,
829 * check if the non-TLS connection was already
830 * in cache; in case, destroy the newly created
831 * connection and use the existing one */
832 if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
833 && !ldap_tls_inplace( lc->lc_ld ) )
837 lc_curr.lc_conn = LDAP_BACK_PCONN;
838 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
839 tmplc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
840 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
841 if ( tmplc != NULL ) {
842 refcnt = ++tmplc->lc_refcnt;
843 binding = ++tmplc->lc_binding;
844 ldap_back_conn_free( lc );
847 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
849 if ( tmplc != NULL ) {
853 #endif /* HAVE_TLS */
855 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
857 /* Inserts the newly created ldapconn in the avl tree */
858 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
860 #if LDAP_BACK_PRINT_CONNTREE > 0
861 ldap_back_print_conntree( li->li_conninfo.lai_tree, ">>> ldap_back_getconn(insert)" );
862 #endif /* LDAP_BACK_PRINT_CONNTREE */
864 assert( lc->lc_refcnt == 1 );
865 assert( lc->lc_binding == 1 );
866 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
867 ldap_back_conndn_cmp, ldap_back_conndn_dup );
869 #if LDAP_BACK_PRINT_CONNTREE > 0
870 ldap_back_print_conntree( li->li_conninfo.lai_tree, "<<< ldap_back_getconn(insert)" );
871 #endif /* LDAP_BACK_PRINT_CONNTREE */
873 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
875 Debug( LDAP_DEBUG_TRACE,
876 "=>ldap_back_getconn: conn %p inserted refcnt=%u binding=%u\n",
877 (void *)lc, refcnt, binding );
879 /* Err could be -1 in case a duplicate ldapconn is inserted */
880 switch ( rs->sr_err ) {
885 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( li ) ) {
886 /* duplicate: free and try to get the newly created one */
887 ldap_back_conn_free( lc );
892 /* taint connection, so that it'll be freed when released */
893 LDAP_BACK_CONN_TAINTED_SET( lc );
897 ldap_back_conn_free( lc );
898 rs->sr_err = LDAP_OTHER;
899 rs->sr_text = "proxy bind collision";
900 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
901 send_ldap_result( op, rs );
910 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
911 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
915 /* let it be used, but taint/delete it so that
916 * no-one else can look it up any further */
917 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
918 #if LDAP_BACK_PRINT_CONNTREE > 0
919 ldap_back_print_conntree( li->li_conninfo.lai_tree, ">>> ldap_back_getconn(timeout)" );
920 #endif /* LDAP_BACK_PRINT_CONNTREE */
921 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
922 ldap_back_conndnlc_cmp );
923 #if LDAP_BACK_PRINT_CONNTREE > 0
924 ldap_back_print_conntree( li->li_conninfo.lai_tree, "<<< ldap_back_getconn(timeout)" );
925 #endif /* LDAP_BACK_PRINT_CONNTREE */
926 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
927 LDAP_BACK_CONN_TAINTED_SET( lc );
930 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
931 char buf[ SLAP_TEXT_BUFLEN ];
933 snprintf( buf, sizeof( buf ),
934 "conn %p fetched refcnt=%u binding=%u%s",
935 (void *)lc, refcnt, binding, expiring ? " expiring" : "" );
936 Debug( LDAP_DEBUG_TRACE,
937 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
943 #endif /* HAVE_TLS */
949 ldap_back_release_conn_lock(
955 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
957 ldapconn_t *lc = *lcp;
960 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
962 assert( lc->lc_refcnt > 0 );
963 LDAP_BACK_CONN_BINDING_CLEAR( lc );
965 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
966 ldap_back_freeconn( op, lc, 0 );
970 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
975 ldap_back_quarantine(
979 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
981 slap_retry_info_t *ri = &li->li_quarantine;
983 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
985 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
986 time_t new_last = slap_get_time();
988 switch ( li->li_isquarantined ) {
989 case LDAP_BACK_FQ_NO:
990 if ( ri->ri_last == new_last ) {
994 Debug( LDAP_DEBUG_ANY,
995 "%s: ldap_back_quarantine enter.\n",
996 op->o_log_prefix, 0, 0 );
1002 case LDAP_BACK_FQ_RETRYING:
1003 Debug( LDAP_DEBUG_ANY,
1004 "%s: ldap_back_quarantine block #%d try #%d failed.\n",
1005 op->o_log_prefix, ri->ri_idx, ri->ri_count );
1008 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1009 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1020 li->li_isquarantined = LDAP_BACK_FQ_YES;
1021 ri->ri_last = new_last;
1023 } else if ( li->li_isquarantined != LDAP_BACK_FQ_NO ) {
1024 if ( ri->ri_last == slap_get_time() ) {
1028 Debug( LDAP_DEBUG_ANY,
1029 "%s: ldap_back_quarantine exit (%d) err=%d.\n",
1030 op->o_log_prefix, li->li_isquarantined, rs->sr_err );
1032 if ( li->li_quarantine_f ) {
1033 (void)li->li_quarantine_f( li, li->li_quarantine_p );
1038 li->li_isquarantined = LDAP_BACK_FQ_NO;
1042 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
1046 * ldap_back_dobind_int
1048 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1051 ldap_back_dobind_int(
1055 ldap_back_send_t sendok,
1059 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1062 struct berval binddn = slap_empty_bv,
1063 bindcred = slap_empty_bv;
1070 assert( lcp != NULL );
1071 assert( retries >= 0 );
1073 if ( sendok & LDAP_BACK_GETCONN ) {
1074 assert( *lcp == NULL );
1076 lc = ldap_back_getconn( op, rs, sendok, &binddn, &bindcred );
1086 assert( lc != NULL );
1090 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1093 if ( binding == 0 ) {
1094 /* check if already bound */
1095 rc = isbound = LDAP_BACK_CONN_ISBOUND( lc );
1099 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1104 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
1105 /* if someone else is about to bind it, give up and retry */
1107 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1109 ldap_pvt_thread_yield();
1113 /* otherwise this thread will bind it */
1114 LDAP_BACK_CONN_BINDING_SET( lc );
1119 /* wait for pending operations to finish */
1120 /* FIXME: may become a bottleneck! */
1121 if ( lc->lc_refcnt != lc->lc_binding ) {
1123 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1125 ldap_pvt_thread_yield();
1130 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1134 * FIXME: we need to let clients use proxyAuthz
1135 * otherwise we cannot do symmetric pools of servers;
1136 * we have to live with the fact that a user can
1137 * authorize itself as any ID that is allowed
1138 * by the authzTo directive of the "proxyauthzdn".
1141 * NOTE: current Proxy Authorization specification
1142 * and implementation do not allow proxy authorization
1143 * control to be provided with Bind requests
1146 * if no bind took place yet, but the connection is bound
1147 * and the "idassert-authcDN" (or other ID) is set,
1148 * then bind as the asserting identity and explicitly
1149 * add the proxyAuthz control to every operation with the
1150 * dn bound to the connection as control value.
1151 * This is done also if this is the authrizing backend,
1152 * but the "override" flag is given to idassert.
1153 * It allows to use SASL bind and yet proxyAuthz users
1155 if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
1156 if ( BER_BVISEMPTY( &binddn ) && BER_BVISEMPTY( &bindcred ) ) {
1157 ldap_back_is_proxy_authz( op, rs, 0, &binddn, &bindcred );
1159 (void)ldap_back_proxy_authz_bind( lc, op, rs, sendok, &binddn, &bindcred );
1163 #ifdef HAVE_CYRUS_SASL
1164 if ( LDAP_BACK_CONN_ISPRIV( lc )
1165 && li->li_acl_authmethod == LDAP_AUTH_SASL )
1167 void *defaults = NULL;
1169 if ( li->li_acl_secprops != NULL ) {
1170 rc = ldap_set_option( lc->lc_ld,
1171 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops );
1173 if ( rc != LDAP_OPT_SUCCESS ) {
1174 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
1175 "(SECPROPS,\"%s\") failed!\n",
1176 li->li_acl_secprops, 0, 0 );
1181 defaults = lutil_sasl_defaults( lc->lc_ld,
1182 li->li_acl_sasl_mech.bv_val,
1183 li->li_acl_sasl_realm.bv_val,
1184 li->li_acl_authcID.bv_val,
1185 li->li_acl_passwd.bv_val,
1188 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
1189 li->li_acl_authcDN.bv_val,
1190 li->li_acl_sasl_mech.bv_val, NULL, NULL,
1191 LDAP_SASL_QUIET, lutil_sasl_interact,
1194 lutil_sasl_freedefs( defaults );
1196 rs->sr_err = slap_map_api2result( rs );
1197 if ( rs->sr_err != LDAP_SUCCESS ) {
1198 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1199 if ( sendok & LDAP_BACK_SENDERR ) {
1200 send_ldap_result( op, rs );
1204 LDAP_BACK_CONN_ISBOUND_SET( lc );
1207 if ( LDAP_BACK_QUARANTINE( li ) ) {
1208 ldap_back_quarantine( op, rs );
1213 #endif /* HAVE_CYRUS_SASL */
1216 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1217 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1218 LDAP_SASL_SIMPLE, &lc->lc_cred,
1219 NULL, NULL, &msgid );
1221 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
1222 if ( retries != LDAP_BACK_RETRY_NEVER ) {
1224 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1227 assert( lc->lc_refcnt > 0 );
1228 if ( lc->lc_refcnt == 1 ) {
1229 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1232 /* lc here must be the regular lc, reset and ready for init */
1233 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
1234 if ( rs->sr_err != LDAP_SUCCESS ) {
1241 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1244 if ( rs->sr_err == LDAP_SUCCESS ) {
1245 if ( retries > 0 ) {
1253 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1257 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1261 /* FIXME: one binding-- too many? */
1263 assert( lc->lc_refcnt == 1 );
1265 ldap_back_freeconn( op, lc, dolock );
1267 rs->sr_err = slap_map_api2result( rs );
1269 if ( LDAP_BACK_QUARANTINE( li ) ) {
1270 ldap_back_quarantine( op, rs );
1273 if ( rs->sr_err != LDAP_SUCCESS &&
1274 ( sendok & LDAP_BACK_SENDERR ) )
1276 send_ldap_result( op, rs );
1282 rc = ldap_back_op_result( lc, op, rs, msgid,
1283 -1, (sendok|LDAP_BACK_BINDING) );
1284 if ( rc == LDAP_SUCCESS ) {
1285 LDAP_BACK_CONN_ISBOUND_SET( lc );
1290 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1291 rc = LDAP_BACK_CONN_ISBOUND( lc );
1293 ldap_back_release_conn_lock( op, rs, lcp, dolock );
1295 } else if ( LDAP_BACK_SAVECRED( li ) ) {
1296 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1305 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1308 ldap_back_dobind( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1310 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1312 return ldap_back_dobind_int( lcp, op, rs,
1313 ( sendok | LDAP_BACK_GETCONN ), li->li_nretries, 1 );
1317 * ldap_back_default_rebind
1319 * This is a callback used for chasing referrals using the same
1320 * credentials as the original user on this session.
1323 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1324 ber_int_t msgid, void *params )
1326 ldapconn_t *lc = (ldapconn_t *)params;
1329 /* ... otherwise we couldn't get here */
1330 assert( lc != NULL );
1332 if ( !ldap_tls_inplace( ld ) ) {
1333 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1335 const char *text = NULL;
1337 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1338 LDAP_BACK_RETRY_DEFAULT, &text );
1339 if ( rc != LDAP_SUCCESS ) {
1343 #endif /* HAVE_TLS */
1345 /* FIXME: add checks on the URL/identity? */
1347 return ldap_sasl_bind_s( ld,
1348 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1349 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1353 * ldap_back_default_urllist
1356 ldap_back_default_urllist(
1358 LDAPURLDesc **urllist,
1362 ldapinfo_t *li = (ldapinfo_t *)params;
1363 LDAPURLDesc **urltail;
1365 if ( urllist == url ) {
1366 return LDAP_SUCCESS;
1369 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1372 *urltail = *urllist;
1376 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1378 ch_free( li->li_uri );
1381 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1382 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1384 return LDAP_SUCCESS;
1393 ldap_back_send_t sendok )
1395 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1397 /* default behavior */
1398 if ( LDAP_BACK_ABANDON( li ) ) {
1399 return ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1402 if ( LDAP_BACK_IGNORE( li ) ) {
1403 return ldap_pvt_discard( lc->lc_ld, msgid );
1406 if ( LDAP_BACK_CANCEL( li ) ) {
1407 /* FIXME: asynchronous? */
1408 return ldap_cancel_s( lc->lc_ld, msgid, NULL, NULL );
1417 ldap_back_op_result(
1423 ldap_back_send_t sendok )
1425 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1430 LDAPControl **ctrls = NULL;
1432 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
1435 rs->sr_matched = NULL;
1437 rs->sr_ctrls = NULL;
1439 /* if the error recorded in the reply corresponds
1440 * to a successful state, get the error from the
1441 * remote server response */
1442 if ( ERR_OK( rs->sr_err ) ) {
1445 LDAPMessage *res = NULL;
1446 time_t stoptime = (time_t)(-1);
1447 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
1448 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
1449 const char *timeout_text = "Operation timed out";
1451 /* if timeout is not specified, compute and use
1452 * the one specific to the ongoing operation */
1453 if ( timeout == (time_t)(-1) ) {
1454 slap_op_t opidx = slap_req2op( op->o_tag );
1456 if ( opidx == SLAP_OP_SEARCH ) {
1457 if ( op->ors_tlimit <= 0 ) {
1461 timeout = op->ors_tlimit;
1462 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
1463 timeout_text = NULL;
1467 timeout = li->li_timeout[ opidx ];
1471 /* better than nothing :) */
1472 if ( timeout == 0 ) {
1473 if ( li->li_idle_timeout ) {
1474 timeout = li->li_idle_timeout;
1476 } else if ( li->li_conn_ttl ) {
1477 timeout = li->li_conn_ttl;
1482 stoptime = op->o_time + timeout;
1485 LDAP_BACK_TV_SET( &tv );
1488 /* if result parsing fails, note the failure reason */
1489 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1492 if ( timeout && slap_get_time() > stoptime ) {
1493 if ( sendok & LDAP_BACK_BINDING ) {
1494 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1496 LDAP_BACK_CONN_TAINTED_SET( lc );
1499 (void)ldap_back_cancel( lc, op, rs, msgid, sendok );
1501 rs->sr_err = timeout_err;
1502 rs->sr_text = timeout_text;
1507 LDAP_BACK_TV_SET( &tv );
1508 ldap_pvt_thread_yield();
1512 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1517 /* otherwise get the result; if it is not
1518 * LDAP_SUCCESS, record it in the reply
1519 * structure (this includes
1520 * LDAP_COMPARE_{TRUE|FALSE}) */
1522 /* only touch when activity actually took place... */
1523 if ( li->li_idle_timeout && lc ) {
1524 lc->lc_time = op->o_time;
1527 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1528 &match, &text, &refs, &ctrls, 1 );
1530 if ( rc != LDAP_SUCCESS ) {
1533 if ( refs != NULL ) {
1536 for ( i = 0; refs[ i ] != NULL; i++ )
1538 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1540 for ( i = 0; refs[ i ] != NULL; i++ ) {
1541 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1543 BER_BVZERO( &rs->sr_ref[ i ] );
1545 if ( ctrls != NULL ) {
1546 rs->sr_ctrls = ctrls;
1551 /* if the error in the reply structure is not
1552 * LDAP_SUCCESS, try to map it from client
1553 * to server error */
1554 if ( !ERR_OK( rs->sr_err ) ) {
1555 rs->sr_err = slap_map_api2result( rs );
1557 /* internal ops ( op->o_conn == NULL )
1558 * must not reply to client */
1559 if ( op->o_conn && !op->o_do_not_cache && match ) {
1561 /* record the (massaged) matched
1562 * DN into the reply structure */
1563 rs->sr_matched = match;
1567 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1568 if ( !( sendok & LDAP_BACK_RETRYING ) ) {
1569 if ( LDAP_BACK_QUARANTINE( li ) ) {
1570 ldap_back_quarantine( op, rs );
1572 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1573 send_ldap_result( op, rs );
1577 } else if ( op->o_conn &&
1578 ( ( ( sendok & LDAP_BACK_SENDOK ) && ERR_OK( rs->sr_err ) )
1579 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1581 send_ldap_result( op, rs );
1585 if ( rs->sr_matched != match ) {
1586 free( (char *)rs->sr_matched );
1588 rs->sr_matched = NULL;
1589 ldap_memfree( match );
1593 ldap_memfree( text );
1598 assert( refs != NULL );
1599 ber_memvfree( (void **)refs );
1600 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1605 assert( rs->sr_ctrls != NULL );
1606 ldap_controls_free( ctrls );
1607 rs->sr_ctrls = NULL;
1610 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1613 /* return true if bound, false if failed */
1615 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1618 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1620 assert( lcp != NULL );
1621 assert( *lcp != NULL );
1623 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1625 if ( (*lcp)->lc_refcnt == 1 ) {
1626 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1627 Debug( LDAP_DEBUG_ANY,
1628 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1629 op->o_log_prefix, li->li_uri,
1630 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1631 "" : (*lcp)->lc_bound_ndn.bv_val );
1632 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1634 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1635 (*lcp)->lc_ld = NULL;
1636 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1638 /* lc here must be the regular lc, reset and ready for init */
1639 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
1640 if ( rc != LDAP_SUCCESS ) {
1641 /* freeit, because lc_refcnt == 1 */
1642 (*lcp)->lc_refcnt = 0;
1643 (void)ldap_back_freeconn( op, *lcp, 0 );
1647 } else if ( ( sendok & LDAP_BACK_BINDING ) ) {
1651 rc = ldap_back_dobind_int( lcp, op, rs, sendok, 0, 0 );
1652 if ( rc == 0 && *lcp != NULL ) {
1653 /* freeit, because lc_refcnt == 1 */
1654 (*lcp)->lc_refcnt = 0;
1655 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1656 (void)ldap_back_freeconn( op, *lcp, 0 );
1662 Debug( LDAP_DEBUG_TRACE,
1663 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1664 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1666 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1667 ldap_back_release_conn_lock( op, rs, lcp, 0 );
1668 assert( *lcp == NULL );
1671 rs->sr_err = LDAP_UNAVAILABLE;
1672 rs->sr_text = "unable to retry";
1673 send_ldap_result( op, rs );
1677 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1683 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
1684 struct berval *binddn, struct berval *bindcred )
1686 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1690 if ( op->o_conn == NULL || op->o_do_not_cache ) {
1694 /* don't proxyAuthz if protocol is not LDAPv3 */
1695 switch ( li->li_version ) {
1700 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1706 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1707 if ( sendok & LDAP_BACK_SENDERR ) {
1708 send_ldap_result( op, rs );
1714 *binddn = slap_empty_bv;
1715 *bindcred = slap_empty_bv;
1717 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1718 ndn = op->o_conn->c_ndn;
1724 switch ( li->li_idassert_mode ) {
1725 case LDAP_BACK_IDASSERT_LEGACY:
1726 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1727 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1729 *binddn = li->li_idassert_authcDN;
1730 *bindcred = li->li_idassert_passwd;
1737 /* NOTE: rootdn can always idassert */
1738 if ( BER_BVISNULL( &ndn ) && li->li_idassert_authz == NULL ) {
1739 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1740 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1741 if ( sendok & LDAP_BACK_SENDERR ) {
1742 send_ldap_result( op, rs );
1746 rs->sr_err = LDAP_SUCCESS;
1747 *binddn = slap_empty_bv;
1748 *bindcred = slap_empty_bv;
1754 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1755 struct berval authcDN;
1757 if ( BER_BVISNULL( &ndn ) ) {
1758 authcDN = slap_empty_bv;
1763 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1764 &authcDN, &authcDN );
1765 if ( rs->sr_err != LDAP_SUCCESS ) {
1766 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1767 if ( sendok & LDAP_BACK_SENDERR ) {
1768 send_ldap_result( op, rs );
1772 rs->sr_err = LDAP_SUCCESS;
1773 *binddn = slap_empty_bv;
1774 *bindcred = slap_empty_bv;
1782 *binddn = li->li_idassert_authcDN;
1783 *bindcred = li->li_idassert_passwd;
1793 ldap_back_proxy_authz_bind(
1797 ldap_back_send_t sendok,
1798 struct berval *binddn,
1799 struct berval *bindcred )
1801 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1806 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1807 ndn = op->o_conn->c_ndn;
1813 if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1814 #ifdef HAVE_CYRUS_SASL
1815 void *defaults = NULL;
1816 struct berval authzID = BER_BVNULL;
1819 /* if SASL supports native authz, prepare for it */
1820 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1821 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1823 switch ( li->li_idassert_mode ) {
1824 case LDAP_BACK_IDASSERT_OTHERID:
1825 case LDAP_BACK_IDASSERT_OTHERDN:
1826 authzID = li->li_idassert_authzID;
1829 case LDAP_BACK_IDASSERT_ANONYMOUS:
1830 BER_BVSTR( &authzID, "dn:" );
1833 case LDAP_BACK_IDASSERT_SELF:
1834 if ( BER_BVISNULL( &ndn ) ) {
1835 /* connection is not authc'd, so don't idassert */
1836 BER_BVSTR( &authzID, "dn:" );
1839 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1840 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1841 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1842 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1843 ndn.bv_val, ndn.bv_len + 1 );
1852 if ( li->li_idassert_secprops != NULL ) {
1853 rs->sr_err = ldap_set_option( lc->lc_ld,
1854 LDAP_OPT_X_SASL_SECPROPS,
1855 (void *)li->li_idassert_secprops );
1857 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1858 rs->sr_err = LDAP_OTHER;
1859 if ( sendok & LDAP_BACK_SENDERR ) {
1860 send_ldap_result( op, rs );
1862 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1867 defaults = lutil_sasl_defaults( lc->lc_ld,
1868 li->li_idassert_sasl_mech.bv_val,
1869 li->li_idassert_sasl_realm.bv_val,
1870 li->li_idassert_authcID.bv_val,
1871 li->li_idassert_passwd.bv_val,
1874 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn->bv_val,
1875 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1876 LDAP_SASL_QUIET, lutil_sasl_interact,
1879 rs->sr_err = slap_map_api2result( rs );
1880 if ( rs->sr_err != LDAP_SUCCESS ) {
1881 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1882 if ( sendok & LDAP_BACK_SENDERR ) {
1883 send_ldap_result( op, rs );
1887 LDAP_BACK_CONN_ISBOUND_SET( lc );
1890 lutil_sasl_freedefs( defaults );
1892 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1896 #endif /* HAVE_CYRUS_SASL */
1899 switch ( li->li_idassert_authmethod ) {
1900 case LDAP_AUTH_NONE:
1901 /* FIXME: do we really need this? */
1902 BER_BVSTR( binddn, "" );
1903 BER_BVSTR( bindcred, "" );
1906 case LDAP_AUTH_SIMPLE:
1907 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1908 binddn->bv_val, LDAP_SASL_SIMPLE,
1909 bindcred, NULL, NULL, &msgid );
1910 rc = ldap_back_op_result( lc, op, rs, msgid,
1911 -1, (sendok|LDAP_BACK_BINDING) );
1916 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1917 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1918 if ( sendok & LDAP_BACK_SENDERR ) {
1919 send_ldap_result( op, rs );
1924 if ( rc == LDAP_SUCCESS ) {
1925 /* set rebind stuff in case of successful proxyAuthz bind,
1926 * so that referral chasing is attempted using the right
1928 LDAP_BACK_CONN_ISBOUND_SET( lc );
1929 ber_bvreplace( &lc->lc_bound_ndn, binddn );
1931 if ( LDAP_BACK_SAVECRED( li ) ) {
1932 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
1933 memset( lc->lc_cred.bv_val, 0,
1934 lc->lc_cred.bv_len );
1936 ber_bvreplace( &lc->lc_cred, bindcred );
1937 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1941 return LDAP_BACK_CONN_ISBOUND( lc );
1945 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1946 * to existing server-side controls if required; if not,
1947 * the existing server-side controls are placed in *pctrls.
1948 * The caller, after using the controls in client API
1949 * operations, if ( *pctrls != op->o_ctrls ), should
1950 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1951 * The function returns success if the control could
1952 * be added if required, or if it did nothing; in the future,
1953 * it might return some error if it failed.
1955 * if no bind took place yet, but the connection is bound
1956 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1957 * and explicitly add proxyAuthz the control to every operation
1958 * with the dn bound to the connection as control value.
1960 * If no server-side controls are defined for the operation,
1961 * simply add the proxyAuthz control; otherwise, if the
1962 * proxyAuthz control is not already set, add it as
1965 * FIXME: is controls order significant for security?
1966 * ANSWER: controls ordering and interoperability
1967 * must be indicated by the specs of each control; if none
1968 * is specified, the order is irrelevant.
1971 ldap_back_proxy_authz_ctrl(
1972 struct berval *bound_ndn,
1974 slap_idassert_t *si,
1977 LDAPControl ***pctrls )
1979 LDAPControl **ctrls = NULL;
1981 slap_idassert_mode_t mode;
1982 struct berval assertedID,
1987 rs->sr_err = LDAP_SUCCESS;
1989 /* don't proxyAuthz if protocol is not LDAPv3 */
1990 switch ( version ) {
1995 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2004 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
2005 * but if it is not set this test fails. We need a different
2006 * means to detect if idassert is enabled */
2007 if ( ( BER_BVISNULL( &si->si_bc.sb_authcId ) || BER_BVISEMPTY( &si->si_bc.sb_authcId ) )
2008 && ( BER_BVISNULL( &si->si_bc.sb_binddn ) || BER_BVISEMPTY( &si->si_bc.sb_binddn ) ) )
2013 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
2017 if ( op->o_tag == LDAP_REQ_BIND ) {
2018 ndn = op->o_req_ndn;
2020 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2021 ndn = op->o_conn->c_ndn;
2027 if ( si->si_mode == LDAP_BACK_IDASSERT_LEGACY ) {
2028 if ( op->o_proxy_authz ) {
2030 * FIXME: we do not want to perform proxyAuthz
2031 * on behalf of the client, because this would
2032 * be performed with "proxyauthzdn" privileges.
2034 * This might actually be too strict, since
2035 * the "proxyauthzdn" authzTo, and each entry's
2036 * authzFrom attributes may be crafted
2037 * to avoid unwanted proxyAuthz to take place.
2040 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2041 rs->sr_text = "proxyAuthz not allowed within namingContext";
2046 if ( !BER_BVISNULL( bound_ndn ) ) {
2050 if ( BER_BVISNULL( &ndn ) ) {
2054 if ( BER_BVISNULL( &si->si_bc.sb_binddn ) ) {
2058 } else if ( si->si_bc.sb_method == LDAP_AUTH_SASL ) {
2059 if ( ( si->si_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2061 /* already asserted in SASL via native authz */
2065 } else if ( si->si_authz && !be_isroot( op ) ) {
2067 struct berval authcDN;
2069 if ( BER_BVISNULL( &ndn ) ) {
2070 authcDN = slap_empty_bv;
2074 rc = slap_sasl_matches( op, si->si_authz,
2075 &authcDN, & authcDN );
2076 if ( rc != LDAP_SUCCESS ) {
2077 if ( si->si_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2078 /* ndn is not authorized
2079 * to use idassert */
2086 if ( op->o_proxy_authz ) {
2089 * 1) ignore the already set proxyAuthz control
2090 * 2) leave it in place, and don't set ours
2092 * 4) reject the operation
2094 * option (4) is very drastic
2095 * option (3) will make the remote server reject
2096 * the operation, thus being equivalent to (4)
2097 * option (2) will likely break the idassert
2098 * assumptions, so we cannot accept it;
2099 * option (1) means that we are contradicting
2100 * the client's reques.
2102 * I think (4) is the only correct choice.
2104 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2105 rs->sr_text = "proxyAuthz not allowed within namingContext";
2108 if ( op->o_is_auth_check ) {
2109 mode = LDAP_BACK_IDASSERT_NOASSERT;
2116 case LDAP_BACK_IDASSERT_SELF:
2117 if ( BER_BVISNULL( &ndn ) ) {
2123 case LDAP_BACK_IDASSERT_LEGACY:
2124 /* original behavior:
2125 * assert the client's identity */
2126 if ( BER_BVISNULL( &ndn ) ) {
2127 assertedID = slap_empty_bv;
2133 case LDAP_BACK_IDASSERT_ANONYMOUS:
2134 /* assert "anonymous" */
2135 assertedID = slap_empty_bv;
2138 case LDAP_BACK_IDASSERT_NOASSERT:
2139 /* don't assert; bind as proxyauthzdn */
2142 case LDAP_BACK_IDASSERT_OTHERID:
2143 case LDAP_BACK_IDASSERT_OTHERDN:
2144 /* assert idassert DN */
2145 assertedID = si->si_bc.sb_authzId;
2152 if ( BER_BVISNULL( &assertedID ) ) {
2153 assertedID = slap_empty_bv;
2156 /* don't idassert the bound DN (ITS#4497) */
2157 if ( dn_match( &assertedID, bound_ndn ) ) {
2161 if ( op->o_ctrls ) {
2162 for ( i = 0; op->o_ctrls[ i ]; i++ )
2163 /* just count ctrls */ ;
2166 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
2168 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
2170 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
2171 ctrls[ 0 ]->ldctl_iscritical = 1;
2173 switch ( si->si_mode ) {
2174 /* already in u:ID or dn:DN form */
2175 case LDAP_BACK_IDASSERT_OTHERID:
2176 case LDAP_BACK_IDASSERT_OTHERDN:
2177 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
2180 /* needs the dn: prefix */
2182 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
2183 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
2185 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
2186 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
2187 assertedID.bv_val, assertedID.bv_len + 1 );
2191 /* Older versions of <draft-weltman-ldapv3-proxy> required
2192 * to encode the value of the authzID (and called it proxyDN);
2193 * this hack provides compatibility with those DSAs that
2194 * implement it this way */
2195 if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
2196 struct berval authzID = ctrls[ 0 ]->ldctl_value;
2197 BerElementBuffer berbuf;
2198 BerElement *ber = (BerElement *)&berbuf;
2201 ber_init2( ber, 0, LBER_USE_DER );
2202 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2204 tag = ber_printf( ber, "O", &authzID );
2205 if ( tag == LBER_ERROR ) {
2206 rs->sr_err = LDAP_OTHER;
2210 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
2211 rs->sr_err = LDAP_OTHER;
2216 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2217 ber_free_buf( ber );
2219 if ( rs->sr_err != LDAP_SUCCESS ) {
2220 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2225 } else if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
2226 struct berval authzID = ctrls[ 0 ]->ldctl_value,
2228 BerElementBuffer berbuf;
2229 BerElement *ber = (BerElement *)&berbuf;
2232 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
2233 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
2234 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2236 rs->sr_err = LDAP_PROTOCOL_ERROR;
2241 tmp.bv_val += STRLENOF( "dn:" );
2242 tmp.bv_len -= STRLENOF( "dn:" );
2244 ber_init2( ber, 0, LBER_USE_DER );
2245 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2247 /* apparently, Mozilla API encodes this
2248 * as "SEQUENCE { LDAPDN }" */
2249 tag = ber_printf( ber, "{O}", &tmp );
2250 if ( tag == LBER_ERROR ) {
2251 rs->sr_err = LDAP_OTHER;
2255 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
2256 rs->sr_err = LDAP_OTHER;
2261 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2262 ber_free_buf( ber );
2264 if ( rs->sr_err != LDAP_SUCCESS ) {
2265 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2270 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
2273 if ( op->o_ctrls ) {
2274 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
2275 ctrls[ i + 1 ] = op->o_ctrls[ i ];
2278 ctrls[ i + 1 ] = NULL;
2281 if ( ctrls == NULL ) {
2282 ctrls = op->o_ctrls;
2291 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
2293 LDAPControl **ctrls = *pctrls;
2295 /* we assume that the first control is the proxyAuthz
2296 * added by back-ldap, so it's the only one we explicitly
2298 if ( ctrls && ctrls != op->o_ctrls ) {
2299 assert( ctrls[ 0 ] != NULL );
2301 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
2302 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
2305 op->o_tmpfree( ctrls, op->o_tmpmemctx );