1 /* bind.c - ldap backend bind function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2007 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 ldap_back_ravl_print( Avlnode *root, int depth )
53 ldap_back_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 flags=0x%08x\n",
62 lc->lc_local_ndn.bv_val ? lc->lc_local_ndn.bv_val : "",
64 avl_bf2str( root->avl_bf ), lc->lc_refcnt, lc->lc_lcflags );
66 ldap_back_ravl_print( root->avl_left, depth+1 );
69 static char* priv2str[] = {
80 ldap_back_print_conntree( ldapinfo_t *li, char *msg )
84 fprintf( stderr, "========> %s\n", msg );
86 for ( c = LDAP_BACK_PCONN_FIRST; c < LDAP_BACK_PCONN_LAST; c++ ) {
90 fprintf( stderr, " %s[%d]\n", priv2str[ c ], li->li_conn_priv[ c ].lic_num );
92 LDAP_TAILQ_FOREACH( lc, &li->li_conn_priv[ c ].lic_priv, lc_q )
94 fprintf( stderr, " [%d] lc=%p local=\"%s\" conn=%p refcnt=%d flags=0x%08x\n",
97 lc->lc_local_ndn.bv_val ? lc->lc_local_ndn.bv_val : "",
98 (void *)lc->lc_conn, lc->lc_refcnt, lc->lc_lcflags );
103 if ( li->li_conninfo.lai_tree == 0 ) {
104 fprintf( stderr, "\t(empty)\n" );
107 ldap_back_ravl_print( li->li_conninfo.lai_tree, 0 );
110 fprintf( stderr, "<======== %s\n", msg );
112 #endif /* LDAP_BACK_PRINT_CONNTREE */
115 ldap_back_freeconn( ldapinfo_t *li, ldapconn_t *lc, int dolock );
118 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
119 struct berval *binddn, struct berval *bindcred );
122 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
123 struct berval *binddn, struct berval *bindcred );
126 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs,
127 ldap_back_send_t sendok, struct berval *binddn, struct berval *bindcred );
130 ldap_back_prepare_conn( ldapconn_t *lc, Operation *op, SlapReply *rs,
131 ldap_back_send_t sendok );
134 ldap_back_conndnlc_cmp( const void *c1, const void *c2 );
137 ldap_back_conn_delete( ldapinfo_t *li, ldapconn_t *lc )
139 if ( LDAP_BACK_PCONN_ISPRIV( lc ) ) {
140 if ( LDAP_BACK_CONN_CACHED( lc ) ) {
141 assert( lc->lc_q.tqe_prev != NULL );
142 assert( li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num > 0 );
143 li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num--;
144 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv, lc, lc_q );
145 LDAP_TAILQ_ENTRY_INIT( lc, lc_q );
146 LDAP_BACK_CONN_CACHED_CLEAR( lc );
149 assert( LDAP_BACK_CONN_TAINTED( lc ) );
150 assert( lc->lc_q.tqe_prev == NULL );
154 ldapconn_t *tmplc = NULL;
156 if ( LDAP_BACK_CONN_CACHED( lc ) ) {
157 assert( !LDAP_BACK_CONN_TAINTED( lc ) );
158 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
159 ldap_back_conndnlc_cmp );
160 assert( tmplc == lc );
161 LDAP_BACK_CONN_CACHED_CLEAR( lc );
164 assert( LDAP_BACK_CONN_TAINTED( lc ) || tmplc == lc );
171 ldap_back_bind( Operation *op, SlapReply *rs )
173 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
178 ldap_back_send_t retrying = LDAP_BACK_RETRYING;
180 /* allow rootdn as a means to auth without the need to actually
181 * contact the proxied DSA */
182 switch ( be_rootdn_bind( op, rs ) ) {
183 case SLAP_CB_CONTINUE:
190 lc = ldap_back_getconn( op, rs, LDAP_BACK_BIND_SERR, NULL, NULL );
195 /* we can do (almost) whatever we want with this conn,
196 * because either it's temporary, or it's marked as binding */
197 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
198 ch_free( lc->lc_bound_ndn.bv_val );
199 BER_BVZERO( &lc->lc_bound_ndn );
201 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
202 memset( lc->lc_cred.bv_val, 0, lc->lc_cred.bv_len );
203 ch_free( lc->lc_cred.bv_val );
204 BER_BVZERO( &lc->lc_cred );
206 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
209 /* method is always LDAP_AUTH_SIMPLE if we got here */
210 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
212 &op->orb_cred, op->o_ctrls, NULL, &msgid );
213 /* FIXME: should we always retry, or only when piping the bind
214 * in the "override" connection pool? */
215 rc = ldap_back_op_result( lc, op, rs, msgid,
216 li->li_timeout[ SLAP_OP_BIND ],
217 LDAP_BACK_BIND_SERR | retrying );
218 if ( rc == LDAP_UNAVAILABLE && retrying ) {
219 retrying &= ~LDAP_BACK_RETRYING;
220 if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_BIND_SERR ) ) {
225 if ( rc == LDAP_SUCCESS ) {
226 /* If defined, proxyAuthz will be used also when
227 * back-ldap is the authorizing backend; for this
228 * purpose, after a successful bind the connection
229 * is left for further binds, and further operations
230 * on this client connection will use a default
231 * connection with identity assertion */
232 /* NOTE: use with care */
233 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
234 ldap_back_release_conn( li, lc );
238 /* rebind is now done inside ldap_back_proxy_authz_bind()
239 * in case of success */
240 LDAP_BACK_CONN_ISBOUND_SET( lc );
241 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
243 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
244 memset( lc->lc_cred.bv_val, 0,
245 lc->lc_cred.bv_len );
248 if ( LDAP_BACK_SAVECRED( li ) ) {
249 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
250 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
253 lc->lc_cred.bv_len = 0;
257 /* must re-insert if local DN changed as result of bind */
258 if ( !LDAP_BACK_CONN_ISBOUND( lc )
259 || ( !dn_match( &op->o_req_ndn, &lc->lc_local_ndn )
260 && !LDAP_BACK_PCONN_ISPRIV( lc ) ) )
265 /* wait for all other ops to release the connection */
267 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
268 if ( lc->lc_refcnt > 1 ) {
269 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
270 ldap_pvt_thread_yield();
274 #if LDAP_BACK_PRINT_CONNTREE > 0
275 ldap_back_print_conntree( li, ">>> ldap_back_bind" );
276 #endif /* LDAP_BACK_PRINT_CONNTREE */
278 assert( lc->lc_refcnt == 1 );
279 ldap_back_conn_delete( li, lc );
281 /* delete all cached connections with the current connection */
282 if ( LDAP_BACK_SINGLECONN( li ) ) {
283 while ( ( tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc, ldap_back_conn_cmp ) ) != NULL )
285 Debug( LDAP_DEBUG_TRACE,
286 "=>ldap_back_bind: destroying conn %ld (refcnt=%u)\n",
287 LDAP_BACK_PCONN_ID( lc ), lc->lc_refcnt, 0 );
289 if ( tmplc->lc_refcnt != 0 ) {
291 LDAP_BACK_CONN_TAINTED_SET( tmplc );
292 LDAP_BACK_CONN_CACHED_CLEAR( tmplc );
296 * Needs a test because the handler may be corrupted,
297 * and calling ldap_unbind on a corrupted header results
298 * in a segmentation fault
300 ldap_back_conn_free( tmplc );
305 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
306 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
307 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
308 LDAP_BACK_PCONN_ROOTDN_SET( lc, op );
310 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
311 ldap_back_conndn_cmp, ldap_back_conndn_dup );
314 #if LDAP_BACK_PRINT_CONNTREE > 0
315 ldap_back_print_conntree( li, "<<< ldap_back_bind" );
316 #endif /* LDAP_BACK_PRINT_CONNTREE */
318 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
321 LDAP_BACK_CONN_CACHED_SET( lc );
325 /* duplicate; someone else successfully bound
326 * on the same connection with the same identity;
327 * we can do this because lc_refcnt == 1 */
328 ldap_back_conn_free( lc );
334 ldap_back_release_conn( li, lc );
341 * ldap_back_conndn_cmp
343 * compares two ldapconn_t based on the value of the conn pointer
344 * and of the local DN; used by avl stuff for insert, lookup
348 ldap_back_conndn_cmp( const void *c1, const void *c2 )
350 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
351 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
354 /* If local DNs don't match, it is definitely not a match */
355 /* For shared sessions, conn is NULL. Only explicitly
356 * bound sessions will have non-NULL conn.
358 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
360 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
367 * ldap_back_conndnlc_cmp
369 * compares two ldapconn_t based on the value of the conn pointer,
370 * the local DN and the lc pointer; used by avl stuff for insert, lookup
374 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
376 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
377 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
380 /* If local DNs don't match, it is definitely not a match */
381 /* For shared sessions, conn is NULL. Only explicitly
382 * bound sessions will have non-NULL conn.
384 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
386 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
388 rc = SLAP_PTRCMP( lc1, lc2 );
398 * compares two ldapconn_t based on the value of the conn pointer;
399 * used by avl stuff for delete of all conns with the same connid
402 ldap_back_conn_cmp( const void *c1, const void *c2 )
404 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
405 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
407 /* For shared sessions, conn is NULL. Only explicitly
408 * bound sessions will have non-NULL conn.
410 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
414 * ldap_back_conndn_dup
416 * returns -1 in case a duplicate ldapconn_t has been inserted;
420 ldap_back_conndn_dup( void *c1, void *c2 )
422 ldapconn_t *lc1 = (ldapconn_t *)c1;
423 ldapconn_t *lc2 = (ldapconn_t *)c2;
425 /* Cannot have more than one shared session with same DN */
426 if ( lc1->lc_conn == lc2->lc_conn &&
427 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
436 ldap_back_freeconn( ldapinfo_t *li, ldapconn_t *lc, int dolock )
439 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
442 #if LDAP_BACK_PRINT_CONNTREE > 0
443 ldap_back_print_conntree( li, ">>> ldap_back_freeconn" );
444 #endif /* LDAP_BACK_PRINT_CONNTREE */
446 (void)ldap_back_conn_delete( li, lc );
448 if ( lc->lc_refcnt == 0 ) {
449 ldap_back_conn_free( (void *)lc );
452 #if LDAP_BACK_PRINT_CONNTREE > 0
453 ldap_back_print_conntree( li, "<<< ldap_back_freeconn" );
454 #endif /* LDAP_BACK_PRINT_CONNTREE */
457 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
474 int rc = LDAP_SUCCESS;
476 /* start TLS ("tls-[try-]{start,propagate}" statements) */
477 if ( ( LDAP_BACK_USE_TLS_F( flags ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS_F( flags ) ) )
478 && !ldap_is_ldaps_url( url ) )
480 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
482 * use asynchronous StartTLS
483 * in case, chase referral (not implemented yet)
487 if ( protocol == 0 ) {
488 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
492 if ( protocol < LDAP_VERSION3 ) {
493 /* we should rather bail out... */
494 rc = LDAP_UNWILLING_TO_PERFORM;
495 *text = "invalid protocol version";
498 if ( rc == LDAP_SUCCESS ) {
499 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
502 if ( rc == LDAP_SUCCESS ) {
503 LDAPMessage *res = NULL;
506 LDAP_BACK_TV_SET( &tv );
509 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
511 rc = LDAP_UNAVAILABLE;
513 } else if ( rc == 0 ) {
514 if ( retries != LDAP_BACK_RETRY_NEVER ) {
515 ldap_pvt_thread_yield();
519 LDAP_BACK_TV_SET( &tv );
522 rc = LDAP_UNAVAILABLE;
524 } else if ( rc == LDAP_RES_EXTENDED ) {
525 struct berval *data = NULL;
527 rc = ldap_parse_extended_result( ld, res,
529 if ( rc == LDAP_SUCCESS ) {
531 rc = ldap_parse_result( ld, res, &err,
532 NULL, NULL, NULL, NULL, 1 );
533 if ( rc == LDAP_SUCCESS ) {
538 /* FIXME: in case a referral
539 * is returned, should we try
540 * using it instead of the
542 if ( rc == LDAP_SUCCESS ) {
543 rc = ldap_install_tls( ld );
545 } else if ( rc == LDAP_REFERRAL ) {
546 rc = LDAP_UNWILLING_TO_PERFORM;
547 *text = "unwilling to chase referral returned by Start TLS exop";
551 if ( data->bv_val ) {
552 ber_memfree( data->bv_val );
566 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
568 * use synchronous StartTLS
570 rc = ldap_start_tls_s( ld, NULL, NULL );
571 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
573 /* if StartTLS is requested, only attempt it if the URL
574 * is not "ldaps://"; this may occur not only in case
575 * of misconfiguration, but also when used in the chain
576 * overlay, where the "uri" can be parsed out of a referral */
582 case LDAP_SERVER_DOWN:
586 if ( LDAP_BACK_TLS_CRITICAL_F( flags ) ) {
587 *text = "could not start TLS";
591 /* in case Start TLS is not critical */
603 #endif /* HAVE_TLS */
606 ldap_back_prepare_conn( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
608 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
612 int is_tls = op->o_conn->c_is_tls;
613 time_t lc_time = (time_t)(-1);
615 #endif /* HAVE_TLS */
617 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
618 rs->sr_err = ldap_initialize( &ld, li->li_uri );
619 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
620 if ( rs->sr_err != LDAP_SUCCESS ) {
624 if ( li->li_urllist_f ) {
625 ldap_set_urllist_proc( ld, li->li_urllist_f, li->li_urllist_p );
628 /* Set LDAP version. This will always succeed: If the client
629 * bound with a particular version, then so can we.
631 if ( li->li_version != 0 ) {
632 version = li->li_version;
634 } else if ( op->o_protocol != 0 ) {
635 version = op->o_protocol;
638 /* assume it's an internal op; set to LDAPv3 */
639 version = LDAP_VERSION3;
641 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
643 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
644 ldap_set_option( ld, LDAP_OPT_REFERRALS,
645 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
647 if ( li->li_network_timeout > 0 ) {
650 tv.tv_sec = li->li_network_timeout;
652 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
656 if ( LDAP_BACK_CONN_ISPRIV( lc ) ) {
659 } else if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
660 sb = &li->li_idassert.si_bc;
666 if ( sb->sb_tls_do_init ) {
667 bindconf_tls_set( sb, ld );
668 } else if ( sb->sb_tls_ctx ) {
669 ldap_set_option( ld, LDAP_OPT_X_TLS_CTX, sb->sb_tls_ctx );
672 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
673 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
674 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
675 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
676 if ( rs->sr_err != LDAP_SUCCESS ) {
677 ldap_unbind_ext( ld, NULL, NULL );
678 rs->sr_text = "Start TLS failed";
681 } else if ( li->li_idle_timeout ) {
682 /* only touch when activity actually took place... */
683 lc_time = op->o_time;
685 #endif /* HAVE_TLS */
691 LDAP_BACK_CONN_ISTLS_SET( lc );
693 LDAP_BACK_CONN_ISTLS_CLEAR( lc );
695 if ( lc_time != (time_t)(-1) ) {
696 lc->lc_time = lc_time;
698 #endif /* HAVE_TLS */
701 if ( rs->sr_err != LDAP_SUCCESS ) {
702 rs->sr_err = slap_map_api2result( rs );
703 if ( sendok & LDAP_BACK_SENDERR ) {
704 if ( rs->sr_text == NULL ) {
705 rs->sr_text = "Proxy connection initialization failed";
707 send_ldap_result( op, rs );
711 if ( li->li_conn_ttl > 0 ) {
712 lc->lc_create_time = op->o_time;
723 ldap_back_send_t sendok,
724 struct berval *binddn,
725 struct berval *bindcred )
727 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
728 ldapconn_t *lc = NULL,
731 lookupconn = !( sendok & LDAP_BACK_BINDING );
733 /* if the server is quarantined, and
734 * - the current interval did not expire yet, or
735 * - no more retries should occur,
736 * don't return the connection */
737 if ( li->li_isquarantined ) {
738 slap_retry_info_t *ri = &li->li_quarantine;
741 if ( li->li_quarantine.ri_interval ) {
742 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
743 if ( li->li_isquarantined == LDAP_BACK_FQ_YES ) {
744 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
745 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
747 Debug( LDAP_DEBUG_ANY,
748 "%s: ldap_back_getconn quarantine "
749 "retry block #%d try #%d.\n",
750 op->o_log_prefix, ri->ri_idx, ri->ri_count );
751 li->li_isquarantined = LDAP_BACK_FQ_RETRYING;
754 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
758 rs->sr_err = LDAP_UNAVAILABLE;
759 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
760 rs->sr_text = "Target is quarantined";
761 send_ldap_result( op, rs );
767 /* Internal searches are privileged and shared. So is root. */
768 if ( op->o_do_not_cache || be_isroot( op ) ) {
769 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
770 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
771 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
774 struct berval tmpbinddn,
781 if ( binddn == NULL ) {
784 if ( bindcred == NULL ) {
785 bindcred = &tmpbindcred;
787 if ( op->o_tag == LDAP_REQ_BIND ) {
788 save_o_dn = op->o_dn;
789 save_o_ndn = op->o_ndn;
790 op->o_dn = op->o_req_dn;
791 op->o_ndn = op->o_req_ndn;
793 isproxyauthz = ldap_back_is_proxy_authz( op, rs, sendok, binddn, bindcred );
794 if ( op->o_tag == LDAP_REQ_BIND ) {
795 op->o_dn = save_o_dn;
796 op->o_ndn = save_o_ndn;
798 if ( isproxyauthz == -1 ) {
802 lc_curr.lc_local_ndn = op->o_ndn;
803 /* Explicit binds must not be shared;
804 * however, explicit binds are piped in a special connection
805 * when idassert is to occur with "override" set */
806 if ( op->o_tag == LDAP_REQ_BIND && !isproxyauthz ) {
807 lc_curr.lc_conn = op->o_conn;
810 if ( isproxyauthz && !( sendok & LDAP_BACK_BINDING ) ) {
811 lc_curr.lc_local_ndn = *binddn;
812 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
813 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
815 } else if ( isproxyauthz && ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) {
816 lc_curr.lc_local_ndn = slap_empty_bv;
817 LDAP_BACK_PCONN_BIND_SET( &lc_curr, op );
818 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
821 } else if ( SLAP_IS_AUTHZ_BACKEND( op ) ) {
822 lc_curr.lc_conn = op->o_conn;
825 LDAP_BACK_PCONN_ANON_SET( &lc_curr, op );
830 /* Explicit Bind requests always get their own conn */
833 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
834 if ( LDAP_BACK_PCONN_ISPRIV( &lc_curr ) ) {
835 /* lookup a conn that's not binding */
836 LDAP_TAILQ_FOREACH( lc,
837 &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv,
840 if ( !LDAP_BACK_CONN_BINDING( lc ) && lc->lc_refcnt == 0 ) {
846 if ( lc != LDAP_TAILQ_LAST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
849 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
851 LDAP_TAILQ_ENTRY_INIT( lc, lc_q );
852 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
856 } else if ( !LDAP_BACK_USE_TEMPORARIES( li )
857 && li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_num == li->li_conn_priv_max )
859 lc = LDAP_TAILQ_FIRST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv );
864 /* Searches for a ldapconn in the avl tree */
865 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
866 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
870 /* Don't reuse connections while they're still binding */
871 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
872 if ( !LDAP_BACK_USE_TEMPORARIES( li ) ) {
873 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
875 ldap_pvt_thread_yield();
882 if ( op->o_tag == LDAP_REQ_BIND ) {
883 /* right now, this is the only possible case */
884 assert( ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) );
885 LDAP_BACK_CONN_BINDING_SET( lc );
888 refcnt = ++lc->lc_refcnt;
891 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
894 /* Looks like we didn't get a bind. Open a new session... */
896 lc = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
897 lc->lc_flags = li->li_flags;
898 lc->lc_lcflags = lc_curr.lc_lcflags;
899 if ( ldap_back_prepare_conn( lc, op, rs, sendok ) != LDAP_SUCCESS ) {
904 if ( sendok & LDAP_BACK_BINDING ) {
905 LDAP_BACK_CONN_BINDING_SET( lc );
908 lc->lc_conn = lc_curr.lc_conn;
909 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
912 * the rationale is: connections as the rootdn are privileged,
913 * so acl_authcDN is to be used; however, in some cases
914 * one already configured identity assertion with a highly
915 * privileged idassert_authcDN, so if acl_authcDN is NULL
916 * and idassert_authcDN is not, use the second instead.
918 * might change in the future, because it's preferable
919 * to make clear what identity is being used, since
920 * the only drawback is that one risks to configure
921 * the same identity twice...
923 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
924 if ( BER_BVISNULL( &li->li_acl_authcDN ) && !BER_BVISNULL( &li->li_idassert_authcDN ) ) {
925 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
926 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
929 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
930 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
932 LDAP_BACK_CONN_ISPRIV_SET( lc );
934 } else if ( LDAP_BACK_CONN_ISIDASSERT( &lc_curr ) ) {
935 if ( !LDAP_BACK_PCONN_ISBIND( &lc_curr ) ) {
936 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
937 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
939 LDAP_BACK_CONN_ISIDASSERT_SET( lc );
942 BER_BVZERO( &lc->lc_cred );
943 BER_BVZERO( &lc->lc_bound_ndn );
944 if ( !BER_BVISEMPTY( &op->o_ndn )
945 && SLAP_IS_AUTHZ_BACKEND( op ) )
947 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
952 /* if start TLS failed but it was not mandatory,
953 * check if the non-TLS connection was already
954 * in cache; in case, destroy the newly created
955 * connection and use the existing one */
956 if ( LDAP_BACK_PCONN_ISTLS( lc )
957 && !ldap_tls_inplace( lc->lc_ld ) )
959 ldapconn_t *tmplc = NULL;
960 int idx = LDAP_BACK_CONN2PRIV( &lc_curr ) - 1;
962 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
963 LDAP_TAILQ_FOREACH( tmplc,
964 &li->li_conn_priv[ idx ].lic_priv,
967 if ( !LDAP_BACK_CONN_BINDING( tmplc ) ) {
972 if ( tmplc != NULL ) {
973 refcnt = ++tmplc->lc_refcnt;
974 ldap_back_conn_free( lc );
977 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
979 if ( tmplc != NULL ) {
983 #endif /* HAVE_TLS */
985 /* Inserts the newly created ldapconn in the avl tree */
986 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
988 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
990 assert( lc->lc_refcnt == 1 );
992 #if LDAP_BACK_PRINT_CONNTREE > 0
993 ldap_back_print_conntree( li, ">>> ldap_back_getconn(insert)" );
994 #endif /* LDAP_BACK_PRINT_CONNTREE */
996 if ( LDAP_BACK_PCONN_ISPRIV( lc ) ) {
997 if ( li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num < li->li_conn_priv_max ) {
998 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv, lc, lc_q );
999 li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num++;
1000 LDAP_BACK_CONN_CACHED_SET( lc );
1003 LDAP_BACK_CONN_TAINTED_SET( lc );
1008 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
1009 ldap_back_conndn_cmp, ldap_back_conndn_dup );
1010 LDAP_BACK_CONN_CACHED_SET( lc );
1013 #if LDAP_BACK_PRINT_CONNTREE > 0
1014 ldap_back_print_conntree( li, "<<< ldap_back_getconn(insert)" );
1015 #endif /* LDAP_BACK_PRINT_CONNTREE */
1017 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1019 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1020 char buf[ SLAP_TEXT_BUFLEN ];
1022 snprintf( buf, sizeof( buf ),
1023 "lc=%p inserted refcnt=%u rc=%d",
1024 (void *)lc, refcnt, rs->sr_err );
1026 Debug( LDAP_DEBUG_TRACE,
1027 "=>ldap_back_getconn: %s: %s\n",
1028 op->o_log_prefix, buf, 0 );
1031 if ( !LDAP_BACK_PCONN_ISPRIV( lc ) ) {
1032 /* Err could be -1 in case a duplicate ldapconn is inserted */
1033 switch ( rs->sr_err ) {
1038 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1039 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( li ) ) {
1040 /* duplicate: free and try to get the newly created one */
1041 ldap_back_conn_free( lc );
1046 /* taint connection, so that it'll be freed when released */
1047 LDAP_BACK_CONN_TAINTED_SET( lc );
1051 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1052 ldap_back_conn_free( lc );
1053 rs->sr_err = LDAP_OTHER;
1054 rs->sr_text = "Proxy bind collision";
1055 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1056 send_ldap_result( op, rs );
1065 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
1066 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
1070 /* let it be used, but taint/delete it so that
1071 * no-one else can look it up any further */
1072 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1074 #if LDAP_BACK_PRINT_CONNTREE > 0
1075 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1076 #endif /* LDAP_BACK_PRINT_CONNTREE */
1078 (void)ldap_back_conn_delete( li, lc );
1079 LDAP_BACK_CONN_TAINTED_SET( lc );
1081 #if LDAP_BACK_PRINT_CONNTREE > 0
1082 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1083 #endif /* LDAP_BACK_PRINT_CONNTREE */
1085 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1088 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1089 char buf[ SLAP_TEXT_BUFLEN ];
1091 snprintf( buf, sizeof( buf ),
1092 "conn %p fetched refcnt=%u%s",
1094 expiring ? " expiring" : "" );
1095 Debug( LDAP_DEBUG_TRACE,
1096 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
1102 #endif /* HAVE_TLS */
1108 ldap_back_release_conn_lock(
1114 ldapconn_t *lc = *lcp;
1117 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1119 assert( lc->lc_refcnt > 0 );
1120 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1122 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
1123 ldap_back_freeconn( li, lc, 0 );
1127 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1132 ldap_back_quarantine(
1136 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1138 slap_retry_info_t *ri = &li->li_quarantine;
1140 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
1142 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1143 time_t new_last = slap_get_time();
1145 switch ( li->li_isquarantined ) {
1146 case LDAP_BACK_FQ_NO:
1147 if ( ri->ri_last == new_last ) {
1151 Debug( LDAP_DEBUG_ANY,
1152 "%s: ldap_back_quarantine enter.\n",
1153 op->o_log_prefix, 0, 0 );
1159 case LDAP_BACK_FQ_RETRYING:
1160 Debug( LDAP_DEBUG_ANY,
1161 "%s: ldap_back_quarantine block #%d try #%d failed.\n",
1162 op->o_log_prefix, ri->ri_idx, ri->ri_count );
1165 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1166 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1177 li->li_isquarantined = LDAP_BACK_FQ_YES;
1178 ri->ri_last = new_last;
1180 } else if ( li->li_isquarantined != LDAP_BACK_FQ_NO ) {
1181 if ( ri->ri_last == slap_get_time() ) {
1185 Debug( LDAP_DEBUG_ANY,
1186 "%s: ldap_back_quarantine exit (%d) err=%d.\n",
1187 op->o_log_prefix, li->li_isquarantined, rs->sr_err );
1189 if ( li->li_quarantine_f ) {
1190 (void)li->li_quarantine_f( li, li->li_quarantine_p );
1195 li->li_isquarantined = LDAP_BACK_FQ_NO;
1199 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
1203 ldap_back_dobind_cb(
1208 ber_tag_t *tptr = op->o_callback->sc_private;
1210 return SLAP_CB_CONTINUE;
1214 * ldap_back_dobind_int
1216 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1219 ldap_back_dobind_int(
1223 ldap_back_send_t sendok,
1227 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1230 struct berval binddn = slap_empty_bv,
1231 bindcred = slap_empty_bv;
1237 ber_tag_t o_tag = op->o_tag;
1238 slap_callback cb = {0};
1240 assert( lcp != NULL );
1241 assert( retries >= 0 );
1243 if ( sendok & LDAP_BACK_GETCONN ) {
1244 assert( *lcp == NULL );
1246 lc = ldap_back_getconn( op, rs, sendok, &binddn, &bindcred );
1256 assert( lc != NULL );
1260 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1263 if ( binding == 0 ) {
1264 /* check if already bound */
1265 rc = isbound = LDAP_BACK_CONN_ISBOUND( lc );
1268 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1273 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
1274 /* if someone else is about to bind it, give up and retry */
1276 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1278 ldap_pvt_thread_yield();
1282 /* otherwise this thread will bind it */
1283 LDAP_BACK_CONN_BINDING_SET( lc );
1289 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1293 * FIXME: we need to let clients use proxyAuthz
1294 * otherwise we cannot do symmetric pools of servers;
1295 * we have to live with the fact that a user can
1296 * authorize itself as any ID that is allowed
1297 * by the authzTo directive of the "proxyauthzdn".
1300 * NOTE: current Proxy Authorization specification
1301 * and implementation do not allow proxy authorization
1302 * control to be provided with Bind requests
1305 * if no bind took place yet, but the connection is bound
1306 * and the "idassert-authcDN" (or other ID) is set,
1307 * then bind as the asserting identity and explicitly
1308 * add the proxyAuthz control to every operation with the
1309 * dn bound to the connection as control value.
1310 * This is done also if this is the authorizing backend,
1311 * but the "override" flag is given to idassert.
1312 * It allows to use SASL bind and yet proxyAuthz users
1314 op->o_tag = LDAP_REQ_BIND;
1315 cb.sc_next = op->o_callback;
1316 cb.sc_private = &o_tag;
1317 cb.sc_response = ldap_back_dobind_cb;
1318 op->o_callback = &cb;
1320 if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
1321 if ( BER_BVISEMPTY( &binddn ) && BER_BVISEMPTY( &bindcred ) ) {
1322 /* if we got here, it shouldn't return result */
1323 rc = ldap_back_is_proxy_authz( op, rs,
1324 LDAP_BACK_DONTSEND, &binddn, &bindcred );
1327 rc = ldap_back_proxy_authz_bind( lc, op, rs, sendok, &binddn, &bindcred );
1331 #ifdef HAVE_CYRUS_SASL
1332 if ( LDAP_BACK_CONN_ISPRIV( lc )
1333 && li->li_acl_authmethod == LDAP_AUTH_SASL )
1335 void *defaults = NULL;
1337 if ( li->li_acl_secprops != NULL ) {
1338 rc = ldap_set_option( lc->lc_ld,
1339 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops );
1341 if ( rc != LDAP_OPT_SUCCESS ) {
1342 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
1343 "(SECPROPS,\"%s\") failed!\n",
1344 li->li_acl_secprops, 0, 0 );
1349 defaults = lutil_sasl_defaults( lc->lc_ld,
1350 li->li_acl_sasl_mech.bv_val,
1351 li->li_acl_sasl_realm.bv_val,
1352 li->li_acl_authcID.bv_val,
1353 li->li_acl_passwd.bv_val,
1355 if ( defaults == NULL ) {
1356 rs->sr_err = LDAP_OTHER;
1357 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1358 if ( sendok & LDAP_BACK_SENDERR ) {
1359 send_ldap_result( op, rs );
1364 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
1365 li->li_acl_authcDN.bv_val,
1366 li->li_acl_sasl_mech.bv_val, NULL, NULL,
1367 LDAP_SASL_QUIET, lutil_sasl_interact,
1370 lutil_sasl_freedefs( defaults );
1372 rs->sr_err = slap_map_api2result( rs );
1373 if ( rs->sr_err != LDAP_SUCCESS ) {
1374 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1375 if ( sendok & LDAP_BACK_SENDERR ) {
1376 send_ldap_result( op, rs );
1380 LDAP_BACK_CONN_ISBOUND_SET( lc );
1383 if ( LDAP_BACK_QUARANTINE( li ) ) {
1384 ldap_back_quarantine( op, rs );
1389 #endif /* HAVE_CYRUS_SASL */
1392 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1393 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1394 LDAP_SASL_SIMPLE, &lc->lc_cred,
1395 NULL, NULL, &msgid );
1397 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
1398 if ( retries != LDAP_BACK_RETRY_NEVER ) {
1400 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1403 assert( lc->lc_refcnt > 0 );
1404 if ( lc->lc_refcnt == 1 ) {
1405 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1408 /* lc here must be the regular lc, reset and ready for init */
1409 rs->sr_err = ldap_back_prepare_conn( lc, op, rs, sendok );
1410 if ( rs->sr_err != LDAP_SUCCESS ) {
1411 sendok &= ~LDAP_BACK_SENDERR;
1417 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1420 if ( rs->sr_err == LDAP_SUCCESS ) {
1421 if ( retries > 0 ) {
1428 assert( lc->lc_refcnt == 1 );
1430 ldap_back_freeconn( li, lc, dolock );
1432 rs->sr_err = slap_map_api2result( rs );
1434 if ( LDAP_BACK_QUARANTINE( li ) ) {
1435 ldap_back_quarantine( op, rs );
1438 if ( rs->sr_err != LDAP_SUCCESS &&
1439 ( sendok & LDAP_BACK_SENDERR ) )
1441 if ( op->o_callback == &cb )
1442 op->o_callback = cb.sc_next;
1444 rs->sr_text = "Internal proxy bind failure";
1445 send_ldap_result( op, rs );
1452 rc = ldap_back_op_result( lc, op, rs, msgid,
1453 -1, ( sendok | LDAP_BACK_BINDING ) );
1454 if ( rc == LDAP_SUCCESS ) {
1455 LDAP_BACK_CONN_ISBOUND_SET( lc );
1459 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1460 rc = LDAP_BACK_CONN_ISBOUND( lc );
1462 ldap_back_release_conn_lock( li, lcp, dolock );
1464 } else if ( LDAP_BACK_SAVECRED( li ) ) {
1465 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1469 if ( op->o_callback == &cb )
1470 op->o_callback = cb.sc_next;
1479 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1482 ldap_back_dobind( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1484 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1486 return ldap_back_dobind_int( lcp, op, rs,
1487 ( sendok | LDAP_BACK_GETCONN ), li->li_nretries, 1 );
1491 * ldap_back_default_rebind
1493 * This is a callback used for chasing referrals using the same
1494 * credentials as the original user on this session.
1497 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1498 ber_int_t msgid, void *params )
1500 ldapconn_t *lc = (ldapconn_t *)params;
1503 /* ... otherwise we couldn't get here */
1504 assert( lc != NULL );
1506 if ( !ldap_tls_inplace( ld ) ) {
1507 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1509 const char *text = NULL;
1511 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1512 LDAP_BACK_RETRY_DEFAULT, &text );
1513 if ( rc != LDAP_SUCCESS ) {
1517 #endif /* HAVE_TLS */
1519 /* FIXME: add checks on the URL/identity? */
1521 return ldap_sasl_bind_s( ld,
1522 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1523 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1527 * ldap_back_default_urllist
1530 ldap_back_default_urllist(
1532 LDAPURLDesc **urllist,
1536 ldapinfo_t *li = (ldapinfo_t *)params;
1537 LDAPURLDesc **urltail;
1539 if ( urllist == url ) {
1540 return LDAP_SUCCESS;
1543 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1546 *urltail = *urllist;
1550 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1552 ch_free( li->li_uri );
1555 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1556 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1558 return LDAP_SUCCESS;
1567 ldap_back_send_t sendok )
1569 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1571 /* default behavior */
1572 if ( LDAP_BACK_ABANDON( li ) ) {
1573 return ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1576 if ( LDAP_BACK_IGNORE( li ) ) {
1577 return ldap_pvt_discard( lc->lc_ld, msgid );
1580 if ( LDAP_BACK_CANCEL( li ) ) {
1581 /* FIXME: asynchronous? */
1582 return ldap_cancel_s( lc->lc_ld, msgid, NULL, NULL );
1591 ldap_back_op_result(
1597 ldap_back_send_t sendok )
1599 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1604 LDAPControl **ctrls = NULL;
1606 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
1609 rs->sr_matched = NULL;
1611 rs->sr_ctrls = NULL;
1613 /* if the error recorded in the reply corresponds
1614 * to a successful state, get the error from the
1615 * remote server response */
1616 if ( ERR_OK( rs->sr_err ) ) {
1619 LDAPMessage *res = NULL;
1620 time_t stoptime = (time_t)(-1);
1621 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
1622 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
1623 const char *timeout_text = "Operation timed out";
1625 /* if timeout is not specified, compute and use
1626 * the one specific to the ongoing operation */
1627 if ( timeout == (time_t)(-1) ) {
1628 slap_op_t opidx = slap_req2op( op->o_tag );
1630 if ( opidx == SLAP_OP_SEARCH ) {
1631 if ( op->ors_tlimit <= 0 ) {
1635 timeout = op->ors_tlimit;
1636 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
1637 timeout_text = NULL;
1641 timeout = li->li_timeout[ opidx ];
1645 /* better than nothing :) */
1646 if ( timeout == 0 ) {
1647 if ( li->li_idle_timeout ) {
1648 timeout = li->li_idle_timeout;
1650 } else if ( li->li_conn_ttl ) {
1651 timeout = li->li_conn_ttl;
1656 stoptime = op->o_time + timeout;
1659 LDAP_BACK_TV_SET( &tv );
1662 /* if result parsing fails, note the failure reason */
1663 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1666 if ( timeout && slap_get_time() > stoptime ) {
1667 if ( sendok & LDAP_BACK_BINDING ) {
1668 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1671 /* let it be used, but taint/delete it so that
1672 * no-one else can look it up any further */
1673 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1675 #if LDAP_BACK_PRINT_CONNTREE > 0
1676 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1677 #endif /* LDAP_BACK_PRINT_CONNTREE */
1679 (void)ldap_back_conn_delete( li, lc );
1680 LDAP_BACK_CONN_TAINTED_SET( lc );
1682 #if LDAP_BACK_PRINT_CONNTREE > 0
1683 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1684 #endif /* LDAP_BACK_PRINT_CONNTREE */
1685 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1688 (void)ldap_back_cancel( lc, op, rs, msgid, sendok );
1690 rs->sr_err = timeout_err;
1691 rs->sr_text = timeout_text;
1696 LDAP_BACK_TV_SET( &tv );
1697 ldap_pvt_thread_yield();
1701 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1706 /* otherwise get the result; if it is not
1707 * LDAP_SUCCESS, record it in the reply
1708 * structure (this includes
1709 * LDAP_COMPARE_{TRUE|FALSE}) */
1711 /* only touch when activity actually took place... */
1712 if ( li->li_idle_timeout && lc ) {
1713 lc->lc_time = op->o_time;
1716 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1717 &match, &text, &refs, &ctrls, 1 );
1719 if ( rc != LDAP_SUCCESS ) {
1723 /* RFC 4511: referrals can only appear
1724 * if result code is LDAP_REFERRAL */
1726 && refs[ 0 ] != NULL
1727 && refs[ 0 ][ 0 ] != '\0' )
1729 if ( rs->sr_err != LDAP_REFERRAL ) {
1730 Debug( LDAP_DEBUG_ANY,
1731 "%s ldap_back_op_result: "
1732 "got referrals with err=%d\n",
1739 for ( i = 0; refs[ i ] != NULL; i++ )
1741 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1743 for ( i = 0; refs[ i ] != NULL; i++ ) {
1744 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1746 BER_BVZERO( &rs->sr_ref[ i ] );
1749 } else if ( rs->sr_err == LDAP_REFERRAL ) {
1750 Debug( LDAP_DEBUG_ANY,
1751 "%s ldap_back_op_result: "
1752 "got err=%d with null "
1753 "or empty referrals\n",
1757 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1760 if ( ctrls != NULL ) {
1761 rs->sr_ctrls = ctrls;
1766 /* if the error in the reply structure is not
1767 * LDAP_SUCCESS, try to map it from client
1768 * to server error */
1769 if ( !ERR_OK( rs->sr_err ) ) {
1770 rs->sr_err = slap_map_api2result( rs );
1772 /* internal ops ( op->o_conn == NULL )
1773 * must not reply to client */
1774 if ( op->o_conn && !op->o_do_not_cache && match ) {
1776 /* record the (massaged) matched
1777 * DN into the reply structure */
1778 rs->sr_matched = match;
1782 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1783 if ( !( sendok & LDAP_BACK_RETRYING ) ) {
1784 if ( LDAP_BACK_QUARANTINE( li ) ) {
1785 ldap_back_quarantine( op, rs );
1787 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1788 if ( rs->sr_text == NULL ) rs->sr_text = "Proxy operation retry failed";
1789 send_ldap_result( op, rs );
1793 } else if ( op->o_conn &&
1794 ( ( ( sendok & LDAP_BACK_SENDOK ) && ERR_OK( rs->sr_err ) )
1795 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1797 send_ldap_result( op, rs );
1801 if ( rs->sr_matched != match ) {
1802 free( (char *)rs->sr_matched );
1804 rs->sr_matched = NULL;
1805 ldap_memfree( match );
1809 ldap_memfree( text );
1814 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1819 ber_memvfree( (void **)refs );
1823 assert( rs->sr_ctrls != NULL );
1824 ldap_controls_free( ctrls );
1825 rs->sr_ctrls = NULL;
1828 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1831 /* return true if bound, false if failed */
1833 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1835 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1838 assert( lcp != NULL );
1839 assert( *lcp != NULL );
1841 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1843 if ( (*lcp)->lc_refcnt == 1 ) {
1844 int binding = LDAP_BACK_CONN_BINDING( *lcp );
1846 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1847 Debug( LDAP_DEBUG_ANY,
1848 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1849 op->o_log_prefix, li->li_uri,
1850 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1851 "" : (*lcp)->lc_bound_ndn.bv_val );
1852 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1854 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1855 (*lcp)->lc_ld = NULL;
1856 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1858 /* lc here must be the regular lc, reset and ready for init */
1859 rc = ldap_back_prepare_conn( *lcp, op, rs, sendok );
1860 if ( rc != LDAP_SUCCESS ) {
1861 /* freeit, because lc_refcnt == 1 */
1862 (*lcp)->lc_refcnt = 0;
1863 (void)ldap_back_freeconn( li, *lcp, 0 );
1867 } else if ( ( sendok & LDAP_BACK_BINDING ) ) {
1869 LDAP_BACK_CONN_BINDING_SET( *lcp );
1874 rc = ldap_back_dobind_int( lcp, op, rs, sendok, 0, 0 );
1875 if ( rc == 0 && *lcp != NULL ) {
1876 /* freeit, because lc_refcnt == 1 */
1877 (*lcp)->lc_refcnt = 0;
1878 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1879 (void)ldap_back_freeconn( li, *lcp, 0 );
1885 Debug( LDAP_DEBUG_TRACE,
1886 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1887 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1889 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1890 ldap_back_release_conn_lock( li, lcp, 0 );
1891 assert( *lcp == NULL );
1893 if ( sendok & LDAP_BACK_SENDERR ) {
1894 rs->sr_err = LDAP_UNAVAILABLE;
1895 rs->sr_text = "Unable to retry";
1896 send_ldap_result( op, rs );
1900 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1906 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
1907 struct berval *binddn, struct berval *bindcred )
1909 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1913 if ( op->o_conn == NULL || op->o_do_not_cache ) {
1917 /* don't proxyAuthz if protocol is not LDAPv3 */
1918 switch ( li->li_version ) {
1923 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1929 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1930 if ( sendok & LDAP_BACK_SENDERR ) {
1931 send_ldap_result( op, rs );
1938 *binddn = slap_empty_bv;
1939 *bindcred = slap_empty_bv;
1941 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1942 ndn = op->o_conn->c_ndn;
1948 switch ( li->li_idassert_mode ) {
1949 case LDAP_BACK_IDASSERT_LEGACY:
1950 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1951 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1953 *binddn = li->li_idassert_authcDN;
1954 *bindcred = li->li_idassert_passwd;
1961 /* NOTE: rootdn can always idassert */
1962 if ( BER_BVISNULL( &ndn )
1963 && li->li_idassert_authz == NULL
1964 && !( li->li_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL ) )
1966 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1967 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1968 if ( sendok & LDAP_BACK_SENDERR ) {
1969 send_ldap_result( op, rs );
1974 rs->sr_err = LDAP_SUCCESS;
1975 *binddn = slap_empty_bv;
1976 *bindcred = slap_empty_bv;
1982 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1983 struct berval authcDN;
1985 if ( BER_BVISNULL( &ndn ) ) {
1986 authcDN = slap_empty_bv;
1991 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1992 &authcDN, &authcDN );
1993 if ( rs->sr_err != LDAP_SUCCESS ) {
1994 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1995 if ( sendok & LDAP_BACK_SENDERR ) {
1996 send_ldap_result( op, rs );
2001 rs->sr_err = LDAP_SUCCESS;
2002 *binddn = slap_empty_bv;
2003 *bindcred = slap_empty_bv;
2011 *binddn = li->li_idassert_authcDN;
2012 *bindcred = li->li_idassert_passwd;
2022 ldap_back_proxy_authz_bind(
2026 ldap_back_send_t sendok,
2027 struct berval *binddn,
2028 struct berval *bindcred )
2030 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2035 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2036 ndn = op->o_conn->c_ndn;
2042 if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
2043 #ifdef HAVE_CYRUS_SASL
2044 void *defaults = NULL;
2045 struct berval authzID = BER_BVNULL;
2048 /* if SASL supports native authz, prepare for it */
2049 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
2050 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2052 switch ( li->li_idassert_mode ) {
2053 case LDAP_BACK_IDASSERT_OTHERID:
2054 case LDAP_BACK_IDASSERT_OTHERDN:
2055 authzID = li->li_idassert_authzID;
2058 case LDAP_BACK_IDASSERT_ANONYMOUS:
2059 BER_BVSTR( &authzID, "dn:" );
2062 case LDAP_BACK_IDASSERT_SELF:
2063 if ( BER_BVISNULL( &ndn ) ) {
2064 /* connection is not authc'd, so don't idassert */
2065 BER_BVSTR( &authzID, "dn:" );
2068 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
2069 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
2070 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
2071 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
2072 ndn.bv_val, ndn.bv_len + 1 );
2081 if ( li->li_idassert_secprops != NULL ) {
2082 rs->sr_err = ldap_set_option( lc->lc_ld,
2083 LDAP_OPT_X_SASL_SECPROPS,
2084 (void *)li->li_idassert_secprops );
2086 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
2087 rs->sr_err = LDAP_OTHER;
2088 if ( sendok & LDAP_BACK_SENDERR ) {
2089 send_ldap_result( op, rs );
2091 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2096 defaults = lutil_sasl_defaults( lc->lc_ld,
2097 li->li_idassert_sasl_mech.bv_val,
2098 li->li_idassert_sasl_realm.bv_val,
2099 li->li_idassert_authcID.bv_val,
2100 li->li_idassert_passwd.bv_val,
2102 if ( defaults == NULL ) {
2103 rs->sr_err = LDAP_OTHER;
2104 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2105 if ( sendok & LDAP_BACK_SENDERR ) {
2106 send_ldap_result( op, rs );
2111 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn->bv_val,
2112 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
2113 LDAP_SASL_QUIET, lutil_sasl_interact,
2116 rs->sr_err = slap_map_api2result( rs );
2117 if ( rs->sr_err != LDAP_SUCCESS ) {
2118 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2119 if ( sendok & LDAP_BACK_SENDERR ) {
2120 send_ldap_result( op, rs );
2124 LDAP_BACK_CONN_ISBOUND_SET( lc );
2127 lutil_sasl_freedefs( defaults );
2129 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
2133 #endif /* HAVE_CYRUS_SASL */
2136 switch ( li->li_idassert_authmethod ) {
2137 case LDAP_AUTH_NONE:
2138 /* FIXME: do we really need this? */
2139 BER_BVSTR( binddn, "" );
2140 BER_BVSTR( bindcred, "" );
2143 case LDAP_AUTH_SIMPLE:
2144 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
2145 binddn->bv_val, LDAP_SASL_SIMPLE,
2146 bindcred, NULL, NULL, &msgid );
2147 rc = ldap_back_op_result( lc, op, rs, msgid,
2148 -1, ( sendok | LDAP_BACK_BINDING ) );
2153 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2154 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
2155 if ( sendok & LDAP_BACK_SENDERR ) {
2156 send_ldap_result( op, rs );
2161 if ( rc == LDAP_SUCCESS ) {
2162 /* set rebind stuff in case of successful proxyAuthz bind,
2163 * so that referral chasing is attempted using the right
2165 LDAP_BACK_CONN_ISBOUND_SET( lc );
2166 ber_bvreplace( &lc->lc_bound_ndn, binddn );
2168 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
2169 memset( lc->lc_cred.bv_val, 0,
2170 lc->lc_cred.bv_len );
2173 if ( LDAP_BACK_SAVECRED( li ) ) {
2174 ber_bvreplace( &lc->lc_cred, bindcred );
2175 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
2178 lc->lc_cred.bv_len = 0;
2182 return LDAP_BACK_CONN_ISBOUND( lc );
2186 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
2187 * to existing server-side controls if required; if not,
2188 * the existing server-side controls are placed in *pctrls.
2189 * The caller, after using the controls in client API
2190 * operations, if ( *pctrls != op->o_ctrls ), should
2191 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
2192 * The function returns success if the control could
2193 * be added if required, or if it did nothing; in the future,
2194 * it might return some error if it failed.
2196 * if no bind took place yet, but the connection is bound
2197 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
2198 * and explicitly add proxyAuthz the control to every operation
2199 * with the dn bound to the connection as control value.
2201 * If no server-side controls are defined for the operation,
2202 * simply add the proxyAuthz control; otherwise, if the
2203 * proxyAuthz control is not already set, add it as
2206 * FIXME: is controls order significant for security?
2207 * ANSWER: controls ordering and interoperability
2208 * must be indicated by the specs of each control; if none
2209 * is specified, the order is irrelevant.
2212 ldap_back_proxy_authz_ctrl(
2213 struct berval *bound_ndn,
2215 slap_idassert_t *si,
2218 LDAPControl ***pctrls )
2220 LDAPControl **ctrls = NULL;
2222 slap_idassert_mode_t mode;
2223 struct berval assertedID,
2229 rs->sr_err = LDAP_SUCCESS;
2231 /* don't proxyAuthz if protocol is not LDAPv3 */
2232 switch ( version ) {
2237 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2246 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
2247 * but if it is not set this test fails. We need a different
2248 * means to detect if idassert is enabled */
2249 if ( ( BER_BVISNULL( &si->si_bc.sb_authcId ) || BER_BVISEMPTY( &si->si_bc.sb_authcId ) )
2250 && ( BER_BVISNULL( &si->si_bc.sb_binddn ) || BER_BVISEMPTY( &si->si_bc.sb_binddn ) ) )
2255 if ( !op->o_conn || op->o_do_not_cache || ( isroot = be_isroot( op ) ) ) {
2259 if ( op->o_tag == LDAP_REQ_BIND ) {
2260 ndn = op->o_req_ndn;
2262 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2263 ndn = op->o_conn->c_ndn;
2269 if ( si->si_mode == LDAP_BACK_IDASSERT_LEGACY ) {
2270 if ( op->o_proxy_authz ) {
2272 * FIXME: we do not want to perform proxyAuthz
2273 * on behalf of the client, because this would
2274 * be performed with "proxyauthzdn" privileges.
2276 * This might actually be too strict, since
2277 * the "proxyauthzdn" authzTo, and each entry's
2278 * authzFrom attributes may be crafted
2279 * to avoid unwanted proxyAuthz to take place.
2282 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2283 rs->sr_text = "proxyAuthz not allowed within namingContext";
2288 if ( !BER_BVISNULL( bound_ndn ) ) {
2292 if ( BER_BVISNULL( &ndn ) ) {
2296 if ( BER_BVISNULL( &si->si_bc.sb_binddn ) ) {
2300 } else if ( si->si_bc.sb_method == LDAP_AUTH_SASL ) {
2301 if ( ( si->si_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2303 /* already asserted in SASL via native authz */
2307 } else if ( si->si_authz && !isroot ) {
2309 struct berval authcDN;
2311 if ( BER_BVISNULL( &ndn ) ) {
2312 authcDN = slap_empty_bv;
2316 rc = slap_sasl_matches( op, si->si_authz,
2317 &authcDN, & authcDN );
2318 if ( rc != LDAP_SUCCESS ) {
2319 if ( si->si_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2320 /* ndn is not authorized
2321 * to use idassert */
2328 if ( op->o_proxy_authz ) {
2331 * 1) ignore the already set proxyAuthz control
2332 * 2) leave it in place, and don't set ours
2334 * 4) reject the operation
2336 * option (4) is very drastic
2337 * option (3) will make the remote server reject
2338 * the operation, thus being equivalent to (4)
2339 * option (2) will likely break the idassert
2340 * assumptions, so we cannot accept it;
2341 * option (1) means that we are contradicting
2342 * the client's reques.
2344 * I think (4) is the only correct choice.
2346 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2347 rs->sr_text = "proxyAuthz not allowed within namingContext";
2350 if ( op->o_is_auth_check ) {
2351 mode = LDAP_BACK_IDASSERT_NOASSERT;
2358 case LDAP_BACK_IDASSERT_LEGACY:
2359 /* original behavior:
2360 * assert the client's identity */
2361 case LDAP_BACK_IDASSERT_SELF:
2365 case LDAP_BACK_IDASSERT_ANONYMOUS:
2366 /* assert "anonymous" */
2367 assertedID = slap_empty_bv;
2370 case LDAP_BACK_IDASSERT_NOASSERT:
2371 /* don't assert; bind as proxyauthzdn */
2374 case LDAP_BACK_IDASSERT_OTHERID:
2375 case LDAP_BACK_IDASSERT_OTHERDN:
2376 /* assert idassert DN */
2377 assertedID = si->si_bc.sb_authzId;
2384 /* if we got here, "" is allowed to proxyAuthz */
2385 if ( BER_BVISNULL( &assertedID ) ) {
2386 assertedID = slap_empty_bv;
2389 /* don't idassert the bound DN (ITS#4497) */
2390 if ( dn_match( &assertedID, bound_ndn ) ) {
2394 if ( op->o_ctrls ) {
2395 for ( i = 0; op->o_ctrls[ i ]; i++ )
2396 /* just count ctrls */ ;
2399 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
2401 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
2403 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
2404 ctrls[ 0 ]->ldctl_iscritical = 1;
2406 switch ( si->si_mode ) {
2407 /* already in u:ID or dn:DN form */
2408 case LDAP_BACK_IDASSERT_OTHERID:
2409 case LDAP_BACK_IDASSERT_OTHERDN:
2410 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
2413 /* needs the dn: prefix */
2415 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
2416 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
2418 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
2419 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
2420 assertedID.bv_val, assertedID.bv_len + 1 );
2424 /* Older versions of <draft-weltman-ldapv3-proxy> required
2425 * to encode the value of the authzID (and called it proxyDN);
2426 * this hack provides compatibility with those DSAs that
2427 * implement it this way */
2428 if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
2429 struct berval authzID = ctrls[ 0 ]->ldctl_value;
2430 BerElementBuffer berbuf;
2431 BerElement *ber = (BerElement *)&berbuf;
2434 ber_init2( ber, 0, LBER_USE_DER );
2435 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2437 tag = ber_printf( ber, "O", &authzID );
2438 if ( tag == LBER_ERROR ) {
2439 rs->sr_err = LDAP_OTHER;
2443 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
2444 rs->sr_err = LDAP_OTHER;
2449 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2450 ber_free_buf( ber );
2452 if ( rs->sr_err != LDAP_SUCCESS ) {
2453 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2458 } else if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
2459 struct berval authzID = ctrls[ 0 ]->ldctl_value,
2461 BerElementBuffer berbuf;
2462 BerElement *ber = (BerElement *)&berbuf;
2465 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
2466 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
2467 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2469 rs->sr_err = LDAP_PROTOCOL_ERROR;
2474 tmp.bv_val += STRLENOF( "dn:" );
2475 tmp.bv_len -= STRLENOF( "dn:" );
2477 ber_init2( ber, 0, LBER_USE_DER );
2478 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2480 /* apparently, Mozilla API encodes this
2481 * as "SEQUENCE { LDAPDN }" */
2482 tag = ber_printf( ber, "{O}", &tmp );
2483 if ( tag == LBER_ERROR ) {
2484 rs->sr_err = LDAP_OTHER;
2488 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
2489 rs->sr_err = LDAP_OTHER;
2494 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2495 ber_free_buf( ber );
2497 if ( rs->sr_err != LDAP_SUCCESS ) {
2498 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2503 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
2506 if ( op->o_ctrls ) {
2507 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
2508 ctrls[ i + 1 ] = op->o_ctrls[ i ];
2511 ctrls[ i + 1 ] = NULL;
2514 if ( ctrls == NULL ) {
2515 ctrls = op->o_ctrls;
2524 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
2526 LDAPControl **ctrls = *pctrls;
2528 /* we assume that the first control is the proxyAuthz
2529 * added by back-ldap, so it's the only one we explicitly
2531 if ( ctrls && ctrls != op->o_ctrls ) {
2532 assert( ctrls[ 0 ] != NULL );
2534 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
2535 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
2538 op->o_tmpfree( ctrls, op->o_tmpmemctx );