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, sendok, binddn, bindcred );
708 if ( isproxyauthz == -1 ) {
711 if ( op->o_tag == LDAP_REQ_BIND ) {
712 op->o_dn = save_o_dn;
713 op->o_ndn = save_o_ndn;
716 lc_curr.lc_local_ndn = op->o_ndn;
717 /* Explicit binds must not be shared;
718 * however, explicit binds are piped in a special connection
719 * when idassert is to occur with "override" set */
720 if ( op->o_tag == LDAP_REQ_BIND && !isproxyauthz ) {
721 lc_curr.lc_conn = op->o_conn;
724 if ( isproxyauthz && !( sendok & LDAP_BACK_BINDING ) ) {
725 lc_curr.lc_local_ndn = *binddn;
726 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
727 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
729 } else if ( isproxyauthz && ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) {
730 lc_curr.lc_local_ndn = slap_empty_bv;
731 lc_curr.lc_conn = LDAP_BACK_PCONN_BIND_SET( op );
732 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
735 } else if ( SLAP_IS_AUTHZ_BACKEND( op ) ) {
736 lc_curr.lc_conn = op->o_conn;
739 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
744 /* Explicit Bind requests always get their own conn */
746 /* Searches for a ldapconn in the avl tree */
748 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
750 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
751 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
753 /* Don't reuse connections while they're still binding */
754 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
755 if ( !LDAP_BACK_USE_TEMPORARIES( li ) ) {
756 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
758 ldap_pvt_thread_yield();
766 if ( op->o_tag == LDAP_REQ_BIND ) {
767 /* right now, this is the only possible case */
768 assert( ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) );
769 LDAP_BACK_CONN_BINDING_SET( lc );
772 refcnt = ++lc->lc_refcnt;
773 binding = ++lc->lc_binding;
776 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
779 /* Looks like we didn't get a bind. Open a new session... */
781 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
785 if ( sendok & LDAP_BACK_BINDING ) {
786 LDAP_BACK_CONN_BINDING_SET( lc );
789 lc->lc_conn = lc_curr.lc_conn;
790 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
793 * the rationale is: connections as the rootdn are privileged,
794 * so acl_authcDN is to be used; however, in some cases
795 * one already configured identity assertion with a highly
796 * privileged idassert_authcDN, so if acl_authcDN is NULL
797 * and idassert_authcDN is not, use the second instead.
799 * might change in the future, because it's preferable
800 * to make clear what identity is being used, since
801 * the only drawback is that one risks to configure
802 * the same identity twice...
804 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
805 if ( BER_BVISNULL( &li->li_acl_authcDN ) && !BER_BVISNULL( &li->li_idassert_authcDN ) ) {
806 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
807 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
810 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
811 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
813 LDAP_BACK_CONN_ISPRIV_SET( lc );
815 } else if ( LDAP_BACK_CONN_ISIDASSERT( &lc_curr ) ) {
816 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
817 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
818 LDAP_BACK_CONN_ISIDASSERT_SET( lc );
821 BER_BVZERO( &lc->lc_cred );
822 BER_BVZERO( &lc->lc_bound_ndn );
823 if ( !BER_BVISEMPTY( &op->o_ndn )
824 && SLAP_IS_AUTHZ_BACKEND( op ) )
826 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
831 /* if start TLS failed but it was not mandatory,
832 * check if the non-TLS connection was already
833 * in cache; in case, destroy the newly created
834 * connection and use the existing one */
835 if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
836 && !ldap_tls_inplace( lc->lc_ld ) )
840 lc_curr.lc_conn = LDAP_BACK_PCONN;
841 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
842 tmplc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
843 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
844 if ( tmplc != NULL ) {
845 refcnt = ++tmplc->lc_refcnt;
846 binding = ++tmplc->lc_binding;
847 ldap_back_conn_free( lc );
850 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
852 if ( tmplc != NULL ) {
856 #endif /* HAVE_TLS */
858 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
860 /* Inserts the newly created ldapconn in the avl tree */
861 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
863 #if LDAP_BACK_PRINT_CONNTREE > 0
864 ldap_back_print_conntree( li->li_conninfo.lai_tree, ">>> ldap_back_getconn(insert)" );
865 #endif /* LDAP_BACK_PRINT_CONNTREE */
867 assert( lc->lc_refcnt == 1 );
868 assert( lc->lc_binding == 1 );
869 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
870 ldap_back_conndn_cmp, ldap_back_conndn_dup );
872 #if LDAP_BACK_PRINT_CONNTREE > 0
873 ldap_back_print_conntree( li->li_conninfo.lai_tree, "<<< ldap_back_getconn(insert)" );
874 #endif /* LDAP_BACK_PRINT_CONNTREE */
876 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
878 Debug( LDAP_DEBUG_TRACE,
879 "=>ldap_back_getconn: conn %p inserted refcnt=%u binding=%u\n",
880 (void *)lc, refcnt, binding );
882 /* Err could be -1 in case a duplicate ldapconn is inserted */
883 switch ( rs->sr_err ) {
888 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( li ) ) {
889 /* duplicate: free and try to get the newly created one */
890 ldap_back_conn_free( lc );
895 /* taint connection, so that it'll be freed when released */
896 LDAP_BACK_CONN_TAINTED_SET( lc );
900 ldap_back_conn_free( lc );
901 rs->sr_err = LDAP_OTHER;
902 rs->sr_text = "proxy bind collision";
903 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
904 send_ldap_result( op, rs );
913 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
914 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
918 /* let it be used, but taint/delete it so that
919 * no-one else can look it up any further */
920 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
921 #if LDAP_BACK_PRINT_CONNTREE > 0
922 ldap_back_print_conntree( li->li_conninfo.lai_tree, ">>> ldap_back_getconn(timeout)" );
923 #endif /* LDAP_BACK_PRINT_CONNTREE */
924 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
925 ldap_back_conndnlc_cmp );
926 #if LDAP_BACK_PRINT_CONNTREE > 0
927 ldap_back_print_conntree( li->li_conninfo.lai_tree, "<<< ldap_back_getconn(timeout)" );
928 #endif /* LDAP_BACK_PRINT_CONNTREE */
929 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
930 LDAP_BACK_CONN_TAINTED_SET( lc );
933 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
934 char buf[ SLAP_TEXT_BUFLEN ];
936 snprintf( buf, sizeof( buf ),
937 "conn %p fetched refcnt=%u binding=%u%s",
938 (void *)lc, refcnt, binding, expiring ? " expiring" : "" );
939 Debug( LDAP_DEBUG_TRACE,
940 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
946 #endif /* HAVE_TLS */
952 ldap_back_release_conn_lock(
958 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
960 ldapconn_t *lc = *lcp;
963 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
965 assert( lc->lc_refcnt > 0 );
966 LDAP_BACK_CONN_BINDING_CLEAR( lc );
968 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
969 ldap_back_freeconn( op, lc, 0 );
973 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
978 ldap_back_quarantine(
982 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
984 slap_retry_info_t *ri = &li->li_quarantine;
986 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
988 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
989 time_t new_last = slap_get_time();
991 switch ( li->li_isquarantined ) {
992 case LDAP_BACK_FQ_NO:
993 if ( ri->ri_last == new_last ) {
997 Debug( LDAP_DEBUG_ANY,
998 "%s: ldap_back_quarantine enter.\n",
999 op->o_log_prefix, 0, 0 );
1005 case LDAP_BACK_FQ_RETRYING:
1006 Debug( LDAP_DEBUG_ANY,
1007 "%s: ldap_back_quarantine block #%d try #%d failed.\n",
1008 op->o_log_prefix, ri->ri_idx, ri->ri_count );
1011 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1012 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1023 li->li_isquarantined = LDAP_BACK_FQ_YES;
1024 ri->ri_last = new_last;
1026 } else if ( li->li_isquarantined != LDAP_BACK_FQ_NO ) {
1027 if ( ri->ri_last == slap_get_time() ) {
1031 Debug( LDAP_DEBUG_ANY,
1032 "%s: ldap_back_quarantine exit (%d) err=%d.\n",
1033 op->o_log_prefix, li->li_isquarantined, rs->sr_err );
1035 if ( li->li_quarantine_f ) {
1036 (void)li->li_quarantine_f( li, li->li_quarantine_p );
1041 li->li_isquarantined = LDAP_BACK_FQ_NO;
1045 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
1049 * ldap_back_dobind_int
1051 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1054 ldap_back_dobind_int(
1058 ldap_back_send_t sendok,
1062 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1065 struct berval binddn = slap_empty_bv,
1066 bindcred = slap_empty_bv;
1073 assert( lcp != NULL );
1074 assert( retries >= 0 );
1076 if ( sendok & LDAP_BACK_GETCONN ) {
1077 assert( *lcp == NULL );
1079 lc = ldap_back_getconn( op, rs, sendok, &binddn, &bindcred );
1089 assert( lc != NULL );
1093 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1096 if ( binding == 0 ) {
1097 /* check if already bound */
1098 rc = isbound = LDAP_BACK_CONN_ISBOUND( lc );
1102 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1107 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
1108 /* if someone else is about to bind it, give up and retry */
1110 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1112 ldap_pvt_thread_yield();
1116 /* otherwise this thread will bind it */
1117 LDAP_BACK_CONN_BINDING_SET( lc );
1122 /* wait for pending operations to finish */
1123 /* FIXME: may become a bottleneck! */
1124 if ( lc->lc_refcnt != lc->lc_binding ) {
1126 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1128 ldap_pvt_thread_yield();
1133 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1137 * FIXME: we need to let clients use proxyAuthz
1138 * otherwise we cannot do symmetric pools of servers;
1139 * we have to live with the fact that a user can
1140 * authorize itself as any ID that is allowed
1141 * by the authzTo directive of the "proxyauthzdn".
1144 * NOTE: current Proxy Authorization specification
1145 * and implementation do not allow proxy authorization
1146 * control to be provided with Bind requests
1149 * if no bind took place yet, but the connection is bound
1150 * and the "idassert-authcDN" (or other ID) is set,
1151 * then bind as the asserting identity and explicitly
1152 * add the proxyAuthz control to every operation with the
1153 * dn bound to the connection as control value.
1154 * This is done also if this is the authrizing backend,
1155 * but the "override" flag is given to idassert.
1156 * It allows to use SASL bind and yet proxyAuthz users
1158 if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
1159 if ( BER_BVISEMPTY( &binddn ) && BER_BVISEMPTY( &bindcred ) ) {
1160 /* if we got here, it shouldn't return result */
1161 rc = ldap_back_is_proxy_authz( op, rs,
1162 LDAP_BACK_DONTSEND, &binddn, &bindcred );
1165 rc = ldap_back_proxy_authz_bind( lc, op, rs, sendok, &binddn, &bindcred );
1169 #ifdef HAVE_CYRUS_SASL
1170 if ( LDAP_BACK_CONN_ISPRIV( lc )
1171 && li->li_acl_authmethod == LDAP_AUTH_SASL )
1173 void *defaults = NULL;
1175 if ( li->li_acl_secprops != NULL ) {
1176 rc = ldap_set_option( lc->lc_ld,
1177 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops );
1179 if ( rc != LDAP_OPT_SUCCESS ) {
1180 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
1181 "(SECPROPS,\"%s\") failed!\n",
1182 li->li_acl_secprops, 0, 0 );
1187 defaults = lutil_sasl_defaults( lc->lc_ld,
1188 li->li_acl_sasl_mech.bv_val,
1189 li->li_acl_sasl_realm.bv_val,
1190 li->li_acl_authcID.bv_val,
1191 li->li_acl_passwd.bv_val,
1194 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
1195 li->li_acl_authcDN.bv_val,
1196 li->li_acl_sasl_mech.bv_val, NULL, NULL,
1197 LDAP_SASL_QUIET, lutil_sasl_interact,
1200 lutil_sasl_freedefs( defaults );
1202 rs->sr_err = slap_map_api2result( rs );
1203 if ( rs->sr_err != LDAP_SUCCESS ) {
1204 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1205 if ( sendok & LDAP_BACK_SENDERR ) {
1206 send_ldap_result( op, rs );
1210 LDAP_BACK_CONN_ISBOUND_SET( lc );
1213 if ( LDAP_BACK_QUARANTINE( li ) ) {
1214 ldap_back_quarantine( op, rs );
1219 #endif /* HAVE_CYRUS_SASL */
1222 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1223 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1224 LDAP_SASL_SIMPLE, &lc->lc_cred,
1225 NULL, NULL, &msgid );
1227 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
1228 if ( retries != LDAP_BACK_RETRY_NEVER ) {
1230 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1233 assert( lc->lc_refcnt > 0 );
1234 if ( lc->lc_refcnt == 1 ) {
1235 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1238 /* lc here must be the regular lc, reset and ready for init */
1239 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
1240 if ( rs->sr_err != LDAP_SUCCESS ) {
1247 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1250 if ( rs->sr_err == LDAP_SUCCESS ) {
1251 if ( retries > 0 ) {
1259 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1263 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1267 /* FIXME: one binding-- too many? */
1269 assert( lc->lc_refcnt == 1 );
1271 ldap_back_freeconn( op, lc, dolock );
1273 rs->sr_err = slap_map_api2result( rs );
1275 if ( LDAP_BACK_QUARANTINE( li ) ) {
1276 ldap_back_quarantine( op, rs );
1279 if ( rs->sr_err != LDAP_SUCCESS &&
1280 ( sendok & LDAP_BACK_SENDERR ) )
1282 send_ldap_result( op, rs );
1288 rc = ldap_back_op_result( lc, op, rs, msgid,
1289 -1, (sendok|LDAP_BACK_BINDING) );
1290 if ( rc == LDAP_SUCCESS ) {
1291 LDAP_BACK_CONN_ISBOUND_SET( lc );
1296 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1297 rc = LDAP_BACK_CONN_ISBOUND( lc );
1299 ldap_back_release_conn_lock( op, rs, lcp, dolock );
1301 } else if ( LDAP_BACK_SAVECRED( li ) ) {
1302 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1311 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1314 ldap_back_dobind( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1316 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1318 return ldap_back_dobind_int( lcp, op, rs,
1319 ( sendok | LDAP_BACK_GETCONN ), li->li_nretries, 1 );
1323 * ldap_back_default_rebind
1325 * This is a callback used for chasing referrals using the same
1326 * credentials as the original user on this session.
1329 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1330 ber_int_t msgid, void *params )
1332 ldapconn_t *lc = (ldapconn_t *)params;
1335 /* ... otherwise we couldn't get here */
1336 assert( lc != NULL );
1338 if ( !ldap_tls_inplace( ld ) ) {
1339 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1341 const char *text = NULL;
1343 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1344 LDAP_BACK_RETRY_DEFAULT, &text );
1345 if ( rc != LDAP_SUCCESS ) {
1349 #endif /* HAVE_TLS */
1351 /* FIXME: add checks on the URL/identity? */
1353 return ldap_sasl_bind_s( ld,
1354 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1355 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1359 * ldap_back_default_urllist
1362 ldap_back_default_urllist(
1364 LDAPURLDesc **urllist,
1368 ldapinfo_t *li = (ldapinfo_t *)params;
1369 LDAPURLDesc **urltail;
1371 if ( urllist == url ) {
1372 return LDAP_SUCCESS;
1375 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1378 *urltail = *urllist;
1382 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1384 ch_free( li->li_uri );
1387 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1388 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1390 return LDAP_SUCCESS;
1399 ldap_back_send_t sendok )
1401 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1403 /* default behavior */
1404 if ( LDAP_BACK_ABANDON( li ) ) {
1405 return ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1408 if ( LDAP_BACK_IGNORE( li ) ) {
1409 return ldap_pvt_discard( lc->lc_ld, msgid );
1412 if ( LDAP_BACK_CANCEL( li ) ) {
1413 /* FIXME: asynchronous? */
1414 return ldap_cancel_s( lc->lc_ld, msgid, NULL, NULL );
1423 ldap_back_op_result(
1429 ldap_back_send_t sendok )
1431 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1436 LDAPControl **ctrls = NULL;
1438 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
1441 rs->sr_matched = NULL;
1443 rs->sr_ctrls = NULL;
1445 /* if the error recorded in the reply corresponds
1446 * to a successful state, get the error from the
1447 * remote server response */
1448 if ( ERR_OK( rs->sr_err ) ) {
1451 LDAPMessage *res = NULL;
1452 time_t stoptime = (time_t)(-1);
1453 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
1454 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
1455 const char *timeout_text = "Operation timed out";
1457 /* if timeout is not specified, compute and use
1458 * the one specific to the ongoing operation */
1459 if ( timeout == (time_t)(-1) ) {
1460 slap_op_t opidx = slap_req2op( op->o_tag );
1462 if ( opidx == SLAP_OP_SEARCH ) {
1463 if ( op->ors_tlimit <= 0 ) {
1467 timeout = op->ors_tlimit;
1468 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
1469 timeout_text = NULL;
1473 timeout = li->li_timeout[ opidx ];
1477 /* better than nothing :) */
1478 if ( timeout == 0 ) {
1479 if ( li->li_idle_timeout ) {
1480 timeout = li->li_idle_timeout;
1482 } else if ( li->li_conn_ttl ) {
1483 timeout = li->li_conn_ttl;
1488 stoptime = op->o_time + timeout;
1491 LDAP_BACK_TV_SET( &tv );
1494 /* if result parsing fails, note the failure reason */
1495 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1498 if ( timeout && slap_get_time() > stoptime ) {
1499 if ( sendok & LDAP_BACK_BINDING ) {
1500 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1502 LDAP_BACK_CONN_TAINTED_SET( lc );
1505 (void)ldap_back_cancel( lc, op, rs, msgid, sendok );
1507 rs->sr_err = timeout_err;
1508 rs->sr_text = timeout_text;
1513 LDAP_BACK_TV_SET( &tv );
1514 ldap_pvt_thread_yield();
1518 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1523 /* otherwise get the result; if it is not
1524 * LDAP_SUCCESS, record it in the reply
1525 * structure (this includes
1526 * LDAP_COMPARE_{TRUE|FALSE}) */
1528 /* only touch when activity actually took place... */
1529 if ( li->li_idle_timeout && lc ) {
1530 lc->lc_time = op->o_time;
1533 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1534 &match, &text, &refs, &ctrls, 1 );
1536 if ( rc != LDAP_SUCCESS ) {
1539 if ( refs != NULL ) {
1542 for ( i = 0; refs[ i ] != NULL; i++ )
1544 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1546 for ( i = 0; refs[ i ] != NULL; i++ ) {
1547 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1549 BER_BVZERO( &rs->sr_ref[ i ] );
1551 if ( ctrls != NULL ) {
1552 rs->sr_ctrls = ctrls;
1557 /* if the error in the reply structure is not
1558 * LDAP_SUCCESS, try to map it from client
1559 * to server error */
1560 if ( !ERR_OK( rs->sr_err ) ) {
1561 rs->sr_err = slap_map_api2result( rs );
1563 /* internal ops ( op->o_conn == NULL )
1564 * must not reply to client */
1565 if ( op->o_conn && !op->o_do_not_cache && match ) {
1567 /* record the (massaged) matched
1568 * DN into the reply structure */
1569 rs->sr_matched = match;
1573 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1574 if ( !( sendok & LDAP_BACK_RETRYING ) ) {
1575 if ( LDAP_BACK_QUARANTINE( li ) ) {
1576 ldap_back_quarantine( op, rs );
1578 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1579 send_ldap_result( op, rs );
1583 } else if ( op->o_conn &&
1584 ( ( ( sendok & LDAP_BACK_SENDOK ) && ERR_OK( rs->sr_err ) )
1585 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1587 send_ldap_result( op, rs );
1591 if ( rs->sr_matched != match ) {
1592 free( (char *)rs->sr_matched );
1594 rs->sr_matched = NULL;
1595 ldap_memfree( match );
1599 ldap_memfree( text );
1604 assert( refs != NULL );
1605 ber_memvfree( (void **)refs );
1606 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1611 assert( rs->sr_ctrls != NULL );
1612 ldap_controls_free( ctrls );
1613 rs->sr_ctrls = NULL;
1616 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1619 /* return true if bound, false if failed */
1621 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1624 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1626 assert( lcp != NULL );
1627 assert( *lcp != NULL );
1629 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1631 if ( (*lcp)->lc_refcnt == 1 ) {
1632 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1633 Debug( LDAP_DEBUG_ANY,
1634 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1635 op->o_log_prefix, li->li_uri,
1636 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1637 "" : (*lcp)->lc_bound_ndn.bv_val );
1638 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1640 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1641 (*lcp)->lc_ld = NULL;
1642 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1644 /* lc here must be the regular lc, reset and ready for init */
1645 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
1646 if ( rc != LDAP_SUCCESS ) {
1647 /* freeit, because lc_refcnt == 1 */
1648 (*lcp)->lc_refcnt = 0;
1649 (void)ldap_back_freeconn( op, *lcp, 0 );
1653 } else if ( ( sendok & LDAP_BACK_BINDING ) ) {
1657 rc = ldap_back_dobind_int( lcp, op, rs, sendok, 0, 0 );
1658 if ( rc == 0 && *lcp != NULL ) {
1659 /* freeit, because lc_refcnt == 1 */
1660 (*lcp)->lc_refcnt = 0;
1661 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1662 (void)ldap_back_freeconn( op, *lcp, 0 );
1668 Debug( LDAP_DEBUG_TRACE,
1669 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1670 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1672 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1673 ldap_back_release_conn_lock( op, rs, lcp, 0 );
1674 assert( *lcp == NULL );
1677 rs->sr_err = LDAP_UNAVAILABLE;
1678 rs->sr_text = "unable to retry";
1679 send_ldap_result( op, rs );
1683 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1689 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
1690 struct berval *binddn, struct berval *bindcred )
1692 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1696 if ( op->o_conn == NULL || op->o_do_not_cache ) {
1700 /* don't proxyAuthz if protocol is not LDAPv3 */
1701 switch ( li->li_version ) {
1706 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1712 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1713 if ( sendok & LDAP_BACK_SENDERR ) {
1714 send_ldap_result( op, rs );
1721 *binddn = slap_empty_bv;
1722 *bindcred = slap_empty_bv;
1724 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1725 ndn = op->o_conn->c_ndn;
1731 switch ( li->li_idassert_mode ) {
1732 case LDAP_BACK_IDASSERT_LEGACY:
1733 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1734 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1736 *binddn = li->li_idassert_authcDN;
1737 *bindcred = li->li_idassert_passwd;
1744 /* NOTE: rootdn can always idassert */
1745 if ( BER_BVISNULL( &ndn ) && li->li_idassert_authz == NULL ) {
1746 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1747 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1748 if ( sendok & LDAP_BACK_SENDERR ) {
1749 send_ldap_result( op, rs );
1754 rs->sr_err = LDAP_SUCCESS;
1755 *binddn = slap_empty_bv;
1756 *bindcred = slap_empty_bv;
1762 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1763 struct berval authcDN;
1765 if ( BER_BVISNULL( &ndn ) ) {
1766 authcDN = slap_empty_bv;
1771 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1772 &authcDN, &authcDN );
1773 if ( rs->sr_err != LDAP_SUCCESS ) {
1774 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1775 if ( sendok & LDAP_BACK_SENDERR ) {
1776 send_ldap_result( op, rs );
1781 rs->sr_err = LDAP_SUCCESS;
1782 *binddn = slap_empty_bv;
1783 *bindcred = slap_empty_bv;
1791 *binddn = li->li_idassert_authcDN;
1792 *bindcred = li->li_idassert_passwd;
1802 ldap_back_proxy_authz_bind(
1806 ldap_back_send_t sendok,
1807 struct berval *binddn,
1808 struct berval *bindcred )
1810 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1815 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1816 ndn = op->o_conn->c_ndn;
1822 if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1823 #ifdef HAVE_CYRUS_SASL
1824 void *defaults = NULL;
1825 struct berval authzID = BER_BVNULL;
1828 /* if SASL supports native authz, prepare for it */
1829 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1830 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1832 switch ( li->li_idassert_mode ) {
1833 case LDAP_BACK_IDASSERT_OTHERID:
1834 case LDAP_BACK_IDASSERT_OTHERDN:
1835 authzID = li->li_idassert_authzID;
1838 case LDAP_BACK_IDASSERT_ANONYMOUS:
1839 BER_BVSTR( &authzID, "dn:" );
1842 case LDAP_BACK_IDASSERT_SELF:
1843 if ( BER_BVISNULL( &ndn ) ) {
1844 /* connection is not authc'd, so don't idassert */
1845 BER_BVSTR( &authzID, "dn:" );
1848 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1849 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1850 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1851 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1852 ndn.bv_val, ndn.bv_len + 1 );
1861 if ( li->li_idassert_secprops != NULL ) {
1862 rs->sr_err = ldap_set_option( lc->lc_ld,
1863 LDAP_OPT_X_SASL_SECPROPS,
1864 (void *)li->li_idassert_secprops );
1866 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1867 rs->sr_err = LDAP_OTHER;
1868 if ( sendok & LDAP_BACK_SENDERR ) {
1869 send_ldap_result( op, rs );
1871 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1876 defaults = lutil_sasl_defaults( lc->lc_ld,
1877 li->li_idassert_sasl_mech.bv_val,
1878 li->li_idassert_sasl_realm.bv_val,
1879 li->li_idassert_authcID.bv_val,
1880 li->li_idassert_passwd.bv_val,
1883 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn->bv_val,
1884 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1885 LDAP_SASL_QUIET, lutil_sasl_interact,
1888 rs->sr_err = slap_map_api2result( rs );
1889 if ( rs->sr_err != LDAP_SUCCESS ) {
1890 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1891 if ( sendok & LDAP_BACK_SENDERR ) {
1892 send_ldap_result( op, rs );
1896 LDAP_BACK_CONN_ISBOUND_SET( lc );
1899 lutil_sasl_freedefs( defaults );
1901 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1905 #endif /* HAVE_CYRUS_SASL */
1908 switch ( li->li_idassert_authmethod ) {
1909 case LDAP_AUTH_NONE:
1910 /* FIXME: do we really need this? */
1911 BER_BVSTR( binddn, "" );
1912 BER_BVSTR( bindcred, "" );
1915 case LDAP_AUTH_SIMPLE:
1916 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1917 binddn->bv_val, LDAP_SASL_SIMPLE,
1918 bindcred, NULL, NULL, &msgid );
1919 rc = ldap_back_op_result( lc, op, rs, msgid,
1920 -1, (sendok|LDAP_BACK_BINDING) );
1925 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1926 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1927 if ( sendok & LDAP_BACK_SENDERR ) {
1928 send_ldap_result( op, rs );
1933 if ( rc == LDAP_SUCCESS ) {
1934 /* set rebind stuff in case of successful proxyAuthz bind,
1935 * so that referral chasing is attempted using the right
1937 LDAP_BACK_CONN_ISBOUND_SET( lc );
1938 ber_bvreplace( &lc->lc_bound_ndn, binddn );
1940 if ( LDAP_BACK_SAVECRED( li ) ) {
1941 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
1942 memset( lc->lc_cred.bv_val, 0,
1943 lc->lc_cred.bv_len );
1945 ber_bvreplace( &lc->lc_cred, bindcred );
1946 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1950 return LDAP_BACK_CONN_ISBOUND( lc );
1954 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1955 * to existing server-side controls if required; if not,
1956 * the existing server-side controls are placed in *pctrls.
1957 * The caller, after using the controls in client API
1958 * operations, if ( *pctrls != op->o_ctrls ), should
1959 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1960 * The function returns success if the control could
1961 * be added if required, or if it did nothing; in the future,
1962 * it might return some error if it failed.
1964 * if no bind took place yet, but the connection is bound
1965 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1966 * and explicitly add proxyAuthz the control to every operation
1967 * with the dn bound to the connection as control value.
1969 * If no server-side controls are defined for the operation,
1970 * simply add the proxyAuthz control; otherwise, if the
1971 * proxyAuthz control is not already set, add it as
1974 * FIXME: is controls order significant for security?
1975 * ANSWER: controls ordering and interoperability
1976 * must be indicated by the specs of each control; if none
1977 * is specified, the order is irrelevant.
1980 ldap_back_proxy_authz_ctrl(
1981 struct berval *bound_ndn,
1983 slap_idassert_t *si,
1986 LDAPControl ***pctrls )
1988 LDAPControl **ctrls = NULL;
1990 slap_idassert_mode_t mode;
1991 struct berval assertedID,
1996 rs->sr_err = LDAP_SUCCESS;
1998 /* don't proxyAuthz if protocol is not LDAPv3 */
1999 switch ( version ) {
2004 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2013 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
2014 * but if it is not set this test fails. We need a different
2015 * means to detect if idassert is enabled */
2016 if ( ( BER_BVISNULL( &si->si_bc.sb_authcId ) || BER_BVISEMPTY( &si->si_bc.sb_authcId ) )
2017 && ( BER_BVISNULL( &si->si_bc.sb_binddn ) || BER_BVISEMPTY( &si->si_bc.sb_binddn ) ) )
2022 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
2026 if ( op->o_tag == LDAP_REQ_BIND ) {
2027 ndn = op->o_req_ndn;
2029 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2030 ndn = op->o_conn->c_ndn;
2036 if ( si->si_mode == LDAP_BACK_IDASSERT_LEGACY ) {
2037 if ( op->o_proxy_authz ) {
2039 * FIXME: we do not want to perform proxyAuthz
2040 * on behalf of the client, because this would
2041 * be performed with "proxyauthzdn" privileges.
2043 * This might actually be too strict, since
2044 * the "proxyauthzdn" authzTo, and each entry's
2045 * authzFrom attributes may be crafted
2046 * to avoid unwanted proxyAuthz to take place.
2049 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2050 rs->sr_text = "proxyAuthz not allowed within namingContext";
2055 if ( !BER_BVISNULL( bound_ndn ) ) {
2059 if ( BER_BVISNULL( &ndn ) ) {
2063 if ( BER_BVISNULL( &si->si_bc.sb_binddn ) ) {
2067 } else if ( si->si_bc.sb_method == LDAP_AUTH_SASL ) {
2068 if ( ( si->si_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2070 /* already asserted in SASL via native authz */
2074 } else if ( si->si_authz && !be_isroot( op ) ) {
2076 struct berval authcDN;
2078 if ( BER_BVISNULL( &ndn ) ) {
2079 authcDN = slap_empty_bv;
2083 rc = slap_sasl_matches( op, si->si_authz,
2084 &authcDN, & authcDN );
2085 if ( rc != LDAP_SUCCESS ) {
2086 if ( si->si_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2087 /* ndn is not authorized
2088 * to use idassert */
2095 if ( op->o_proxy_authz ) {
2098 * 1) ignore the already set proxyAuthz control
2099 * 2) leave it in place, and don't set ours
2101 * 4) reject the operation
2103 * option (4) is very drastic
2104 * option (3) will make the remote server reject
2105 * the operation, thus being equivalent to (4)
2106 * option (2) will likely break the idassert
2107 * assumptions, so we cannot accept it;
2108 * option (1) means that we are contradicting
2109 * the client's reques.
2111 * I think (4) is the only correct choice.
2113 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2114 rs->sr_text = "proxyAuthz not allowed within namingContext";
2117 if ( op->o_is_auth_check ) {
2118 mode = LDAP_BACK_IDASSERT_NOASSERT;
2125 case LDAP_BACK_IDASSERT_SELF:
2126 if ( BER_BVISNULL( &ndn ) ) {
2132 case LDAP_BACK_IDASSERT_LEGACY:
2133 /* original behavior:
2134 * assert the client's identity */
2135 if ( BER_BVISNULL( &ndn ) ) {
2136 assertedID = slap_empty_bv;
2142 case LDAP_BACK_IDASSERT_ANONYMOUS:
2143 /* assert "anonymous" */
2144 assertedID = slap_empty_bv;
2147 case LDAP_BACK_IDASSERT_NOASSERT:
2148 /* don't assert; bind as proxyauthzdn */
2151 case LDAP_BACK_IDASSERT_OTHERID:
2152 case LDAP_BACK_IDASSERT_OTHERDN:
2153 /* assert idassert DN */
2154 assertedID = si->si_bc.sb_authzId;
2161 if ( BER_BVISNULL( &assertedID ) ) {
2162 assertedID = slap_empty_bv;
2165 /* don't idassert the bound DN (ITS#4497) */
2166 if ( dn_match( &assertedID, bound_ndn ) ) {
2170 if ( op->o_ctrls ) {
2171 for ( i = 0; op->o_ctrls[ i ]; i++ )
2172 /* just count ctrls */ ;
2175 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
2177 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
2179 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
2180 ctrls[ 0 ]->ldctl_iscritical = 1;
2182 switch ( si->si_mode ) {
2183 /* already in u:ID or dn:DN form */
2184 case LDAP_BACK_IDASSERT_OTHERID:
2185 case LDAP_BACK_IDASSERT_OTHERDN:
2186 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
2189 /* needs the dn: prefix */
2191 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
2192 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
2194 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
2195 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
2196 assertedID.bv_val, assertedID.bv_len + 1 );
2200 /* Older versions of <draft-weltman-ldapv3-proxy> required
2201 * to encode the value of the authzID (and called it proxyDN);
2202 * this hack provides compatibility with those DSAs that
2203 * implement it this way */
2204 if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
2205 struct berval authzID = ctrls[ 0 ]->ldctl_value;
2206 BerElementBuffer berbuf;
2207 BerElement *ber = (BerElement *)&berbuf;
2210 ber_init2( ber, 0, LBER_USE_DER );
2211 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2213 tag = ber_printf( ber, "O", &authzID );
2214 if ( tag == LBER_ERROR ) {
2215 rs->sr_err = LDAP_OTHER;
2219 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
2220 rs->sr_err = LDAP_OTHER;
2225 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2226 ber_free_buf( ber );
2228 if ( rs->sr_err != LDAP_SUCCESS ) {
2229 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2234 } else if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
2235 struct berval authzID = ctrls[ 0 ]->ldctl_value,
2237 BerElementBuffer berbuf;
2238 BerElement *ber = (BerElement *)&berbuf;
2241 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
2242 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
2243 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2245 rs->sr_err = LDAP_PROTOCOL_ERROR;
2250 tmp.bv_val += STRLENOF( "dn:" );
2251 tmp.bv_len -= STRLENOF( "dn:" );
2253 ber_init2( ber, 0, LBER_USE_DER );
2254 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2256 /* apparently, Mozilla API encodes this
2257 * as "SEQUENCE { LDAPDN }" */
2258 tag = ber_printf( ber, "{O}", &tmp );
2259 if ( tag == LBER_ERROR ) {
2260 rs->sr_err = LDAP_OTHER;
2264 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
2265 rs->sr_err = LDAP_OTHER;
2270 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2271 ber_free_buf( ber );
2273 if ( rs->sr_err != LDAP_SUCCESS ) {
2274 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2279 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
2282 if ( op->o_ctrls ) {
2283 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
2284 ctrls[ i + 1 ] = op->o_ctrls[ i ];
2287 ctrls[ i + 1 ] = NULL;
2290 if ( ctrls == NULL ) {
2291 ctrls = op->o_ctrls;
2300 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
2302 LDAPControl **ctrls = *pctrls;
2304 /* we assume that the first control is the proxyAuthz
2305 * added by back-ldap, so it's the only one we explicitly
2307 if ( ctrls && ctrls != op->o_ctrls ) {
2308 assert( ctrls[ 0 ] != NULL );
2310 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
2311 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
2314 op->o_tmpfree( ctrls, op->o_tmpmemctx );