1 /* bind.c - ldap backend bind function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2009 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;
176 LDAPControl **ctrls = NULL;
177 struct berval save_o_dn;
178 int save_o_do_not_cache,
181 ldap_back_send_t retrying = LDAP_BACK_RETRYING;
183 /* allow rootdn as a means to auth without the need to actually
184 * contact the proxied DSA */
185 switch ( be_rootdn_bind( op, rs ) ) {
186 case SLAP_CB_CONTINUE:
193 lc = ldap_back_getconn( op, rs, LDAP_BACK_BIND_SERR, NULL, NULL );
198 /* we can do (almost) whatever we want with this conn,
199 * because either it's temporary, or it's marked as binding */
200 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
201 ch_free( lc->lc_bound_ndn.bv_val );
202 BER_BVZERO( &lc->lc_bound_ndn );
204 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
205 memset( lc->lc_cred.bv_val, 0, lc->lc_cred.bv_len );
206 ch_free( lc->lc_cred.bv_val );
207 BER_BVZERO( &lc->lc_cred );
209 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
211 /* don't add proxyAuthz; set the bindDN */
212 save_o_dn = op->o_dn;
213 save_o_do_not_cache = op->o_do_not_cache;
214 op->o_dn = op->o_req_dn;
215 op->o_do_not_cache = 1;
218 rc = ldap_back_controls_add( op, rs, lc, &ctrls );
219 op->o_dn = save_o_dn;
220 op->o_do_not_cache = save_o_do_not_cache;
221 if ( rc != LDAP_SUCCESS ) {
222 send_ldap_result( op, rs );
223 ldap_back_release_conn( li, lc );
228 /* method is always LDAP_AUTH_SIMPLE if we got here */
229 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
231 &op->orb_cred, ctrls, NULL, &msgid );
232 /* FIXME: should we always retry, or only when piping the bind
233 * in the "override" connection pool? */
234 rc = ldap_back_op_result( lc, op, rs, msgid,
235 li->li_timeout[ SLAP_OP_BIND ],
236 LDAP_BACK_BIND_SERR | retrying );
237 if ( rc == LDAP_UNAVAILABLE && retrying ) {
238 retrying &= ~LDAP_BACK_RETRYING;
239 if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_BIND_SERR ) ) {
244 ldap_back_controls_free( op, rs, &ctrls );
246 if ( rc == LDAP_SUCCESS ) {
247 /* If defined, proxyAuthz will be used also when
248 * back-ldap is the authorizing backend; for this
249 * purpose, after a successful bind the connection
250 * is left for further binds, and further operations
251 * on this client connection will use a default
252 * connection with identity assertion */
253 /* NOTE: use with care */
254 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
255 ldap_back_release_conn( li, lc );
259 /* rebind is now done inside ldap_back_proxy_authz_bind()
260 * in case of success */
261 LDAP_BACK_CONN_ISBOUND_SET( lc );
262 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
264 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
265 memset( lc->lc_cred.bv_val, 0,
266 lc->lc_cred.bv_len );
269 if ( LDAP_BACK_SAVECRED( li ) ) {
270 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
271 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
274 lc->lc_cred.bv_len = 0;
278 /* must re-insert if local DN changed as result of bind */
279 if ( !LDAP_BACK_CONN_ISBOUND( lc )
280 || ( !dn_match( &op->o_req_ndn, &lc->lc_local_ndn )
281 && !LDAP_BACK_PCONN_ISPRIV( lc ) ) )
286 /* wait for all other ops to release the connection */
288 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
289 if ( lc->lc_refcnt > 1 ) {
290 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
291 ldap_pvt_thread_yield();
295 #if LDAP_BACK_PRINT_CONNTREE > 0
296 ldap_back_print_conntree( li, ">>> ldap_back_bind" );
297 #endif /* LDAP_BACK_PRINT_CONNTREE */
299 assert( lc->lc_refcnt == 1 );
300 ldap_back_conn_delete( li, lc );
302 /* delete all cached connections with the current connection */
303 if ( LDAP_BACK_SINGLECONN( li ) ) {
304 while ( ( tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc, ldap_back_conn_cmp ) ) != NULL )
306 Debug( LDAP_DEBUG_TRACE,
307 "=>ldap_back_bind: destroying conn %ld (refcnt=%u)\n",
308 LDAP_BACK_PCONN_ID( lc ), lc->lc_refcnt, 0 );
310 if ( tmplc->lc_refcnt != 0 ) {
312 LDAP_BACK_CONN_TAINTED_SET( tmplc );
313 LDAP_BACK_CONN_CACHED_CLEAR( tmplc );
317 * Needs a test because the handler may be corrupted,
318 * and calling ldap_unbind on a corrupted header results
319 * in a segmentation fault
321 ldap_back_conn_free( tmplc );
326 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
327 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
328 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
329 LDAP_BACK_PCONN_ROOTDN_SET( lc, op );
331 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
332 ldap_back_conndn_cmp, ldap_back_conndn_dup );
335 #if LDAP_BACK_PRINT_CONNTREE > 0
336 ldap_back_print_conntree( li, "<<< ldap_back_bind" );
337 #endif /* LDAP_BACK_PRINT_CONNTREE */
339 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
342 LDAP_BACK_CONN_CACHED_SET( lc );
346 /* duplicate; someone else successfully bound
347 * on the same connection with the same identity;
348 * we can do this because lc_refcnt == 1 */
349 ldap_back_conn_free( lc );
355 ldap_back_release_conn( li, lc );
362 * ldap_back_conndn_cmp
364 * compares two ldapconn_t based on the value of the conn pointer
365 * and of the local DN; used by avl stuff for insert, lookup
369 ldap_back_conndn_cmp( const void *c1, const void *c2 )
371 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
372 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
375 /* If local DNs don't match, it is definitely not a match */
376 /* For shared sessions, conn is NULL. Only explicitly
377 * bound sessions will have non-NULL conn.
379 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
381 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
388 * ldap_back_conndnlc_cmp
390 * compares two ldapconn_t based on the value of the conn pointer,
391 * the local DN and the lc pointer; used by avl stuff for insert, lookup
395 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
397 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
398 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
401 /* If local DNs don't match, it is definitely not a match */
402 /* For shared sessions, conn is NULL. Only explicitly
403 * bound sessions will have non-NULL conn.
405 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
407 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
409 rc = SLAP_PTRCMP( lc1, lc2 );
419 * compares two ldapconn_t based on the value of the conn pointer;
420 * used by avl stuff for delete of all conns with the same connid
423 ldap_back_conn_cmp( const void *c1, const void *c2 )
425 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
426 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
428 /* For shared sessions, conn is NULL. Only explicitly
429 * bound sessions will have non-NULL conn.
431 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
435 * ldap_back_conndn_dup
437 * returns -1 in case a duplicate ldapconn_t has been inserted;
441 ldap_back_conndn_dup( void *c1, void *c2 )
443 ldapconn_t *lc1 = (ldapconn_t *)c1;
444 ldapconn_t *lc2 = (ldapconn_t *)c2;
446 /* Cannot have more than one shared session with same DN */
447 if ( lc1->lc_conn == lc2->lc_conn &&
448 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
457 ldap_back_freeconn( ldapinfo_t *li, ldapconn_t *lc, int dolock )
460 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
463 #if LDAP_BACK_PRINT_CONNTREE > 0
464 ldap_back_print_conntree( li, ">>> ldap_back_freeconn" );
465 #endif /* LDAP_BACK_PRINT_CONNTREE */
467 (void)ldap_back_conn_delete( li, lc );
469 if ( lc->lc_refcnt == 0 ) {
470 ldap_back_conn_free( (void *)lc );
473 #if LDAP_BACK_PRINT_CONNTREE > 0
474 ldap_back_print_conntree( li, "<<< ldap_back_freeconn" );
475 #endif /* LDAP_BACK_PRINT_CONNTREE */
478 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
495 int rc = LDAP_SUCCESS;
497 /* start TLS ("tls-[try-]{start,propagate}" statements) */
498 if ( ( LDAP_BACK_USE_TLS_F( flags ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS_F( flags ) ) )
499 && !ldap_is_ldaps_url( url ) )
501 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
503 * use asynchronous StartTLS
504 * in case, chase referral (not implemented yet)
508 if ( protocol == 0 ) {
509 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
513 if ( protocol < LDAP_VERSION3 ) {
514 /* we should rather bail out... */
515 rc = LDAP_UNWILLING_TO_PERFORM;
516 *text = "invalid protocol version";
519 if ( rc == LDAP_SUCCESS ) {
520 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
523 if ( rc == LDAP_SUCCESS ) {
524 LDAPMessage *res = NULL;
527 LDAP_BACK_TV_SET( &tv );
530 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
532 rc = LDAP_UNAVAILABLE;
534 } else if ( rc == 0 ) {
535 if ( retries != LDAP_BACK_RETRY_NEVER ) {
536 ldap_pvt_thread_yield();
540 LDAP_BACK_TV_SET( &tv );
543 rc = LDAP_UNAVAILABLE;
545 } else if ( rc == LDAP_RES_EXTENDED ) {
546 struct berval *data = NULL;
548 rc = ldap_parse_extended_result( ld, res,
550 if ( rc == LDAP_SUCCESS ) {
552 rc = ldap_parse_result( ld, res, &err,
553 NULL, NULL, NULL, NULL, 1 );
554 if ( rc == LDAP_SUCCESS ) {
559 /* FIXME: in case a referral
560 * is returned, should we try
561 * using it instead of the
563 if ( rc == LDAP_SUCCESS ) {
564 rc = ldap_install_tls( ld );
566 } else if ( rc == LDAP_REFERRAL ) {
567 rc = LDAP_UNWILLING_TO_PERFORM;
568 *text = "unwilling to chase referral returned by Start TLS exop";
572 if ( data->bv_val ) {
573 ber_memfree( data->bv_val );
587 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
589 * use synchronous StartTLS
591 rc = ldap_start_tls_s( ld, NULL, NULL );
592 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
594 /* if StartTLS is requested, only attempt it if the URL
595 * is not "ldaps://"; this may occur not only in case
596 * of misconfiguration, but also when used in the chain
597 * overlay, where the "uri" can be parsed out of a referral */
603 case LDAP_SERVER_DOWN:
607 if ( LDAP_BACK_TLS_CRITICAL_F( flags ) ) {
608 *text = "could not start TLS";
612 /* in case Start TLS is not critical */
624 #endif /* HAVE_TLS */
627 ldap_back_prepare_conn( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
629 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
633 int is_tls = op->o_conn->c_is_tls;
634 time_t lc_time = (time_t)(-1);
636 #endif /* HAVE_TLS */
638 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
639 rs->sr_err = ldap_initialize( &ld, li->li_uri );
640 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
641 if ( rs->sr_err != LDAP_SUCCESS ) {
645 if ( li->li_urllist_f ) {
646 ldap_set_urllist_proc( ld, li->li_urllist_f, li->li_urllist_p );
649 /* Set LDAP version. This will always succeed: If the client
650 * bound with a particular version, then so can we.
652 if ( li->li_version != 0 ) {
653 version = li->li_version;
655 } else if ( op->o_protocol != 0 ) {
656 version = op->o_protocol;
659 /* assume it's an internal op; set to LDAPv3 */
660 version = LDAP_VERSION3;
662 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
664 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
665 ldap_set_option( ld, LDAP_OPT_REFERRALS,
666 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
668 if ( li->li_network_timeout > 0 ) {
671 tv.tv_sec = li->li_network_timeout;
673 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
677 if ( LDAP_BACK_CONN_ISPRIV( lc ) ) {
680 } else if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
681 sb = &li->li_idassert.si_bc;
687 if ( sb->sb_tls_do_init ) {
688 bindconf_tls_set( sb, ld );
689 } else if ( sb->sb_tls_ctx ) {
690 ldap_set_option( ld, LDAP_OPT_X_TLS_CTX, sb->sb_tls_ctx );
693 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
694 assert( li->li_uri_mutex_do_not_lock == 0 );
695 li->li_uri_mutex_do_not_lock = 1;
696 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
697 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
698 li->li_uri_mutex_do_not_lock = 0;
699 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
700 if ( rs->sr_err != LDAP_SUCCESS ) {
701 ldap_unbind_ext( ld, NULL, NULL );
702 rs->sr_text = "Start TLS failed";
705 } else if ( li->li_idle_timeout ) {
706 /* only touch when activity actually took place... */
707 lc_time = op->o_time;
709 #endif /* HAVE_TLS */
715 LDAP_BACK_CONN_ISTLS_SET( lc );
717 LDAP_BACK_CONN_ISTLS_CLEAR( lc );
719 if ( lc_time != (time_t)(-1) ) {
720 lc->lc_time = lc_time;
722 #endif /* HAVE_TLS */
725 if ( rs->sr_err != LDAP_SUCCESS ) {
726 rs->sr_err = slap_map_api2result( rs );
727 if ( sendok & LDAP_BACK_SENDERR ) {
728 if ( rs->sr_text == NULL ) {
729 rs->sr_text = "Proxy connection initialization failed";
731 send_ldap_result( op, rs );
735 if ( li->li_conn_ttl > 0 ) {
736 lc->lc_create_time = op->o_time;
747 ldap_back_send_t sendok,
748 struct berval *binddn,
749 struct berval *bindcred )
751 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
752 ldapconn_t *lc = NULL,
755 lookupconn = !( sendok & LDAP_BACK_BINDING );
757 /* if the server is quarantined, and
758 * - the current interval did not expire yet, or
759 * - no more retries should occur,
760 * don't return the connection */
761 if ( li->li_isquarantined ) {
762 slap_retry_info_t *ri = &li->li_quarantine;
765 if ( li->li_quarantine.ri_interval ) {
766 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
767 if ( li->li_isquarantined == LDAP_BACK_FQ_YES ) {
768 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
769 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
771 Debug( LDAP_DEBUG_ANY,
772 "%s: ldap_back_getconn quarantine "
773 "retry block #%d try #%d.\n",
774 op->o_log_prefix, ri->ri_idx, ri->ri_count );
775 li->li_isquarantined = LDAP_BACK_FQ_RETRYING;
778 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
782 rs->sr_err = LDAP_UNAVAILABLE;
783 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
784 rs->sr_text = "Target is quarantined";
785 send_ldap_result( op, rs );
791 /* Internal searches are privileged and shared. So is root. */
792 if ( op->o_do_not_cache || be_isroot( op ) ) {
793 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
794 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
795 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
798 struct berval tmpbinddn,
805 if ( binddn == NULL ) {
808 if ( bindcred == NULL ) {
809 bindcred = &tmpbindcred;
811 if ( op->o_tag == LDAP_REQ_BIND ) {
812 save_o_dn = op->o_dn;
813 save_o_ndn = op->o_ndn;
814 op->o_dn = op->o_req_dn;
815 op->o_ndn = op->o_req_ndn;
817 isproxyauthz = ldap_back_is_proxy_authz( op, rs, sendok, binddn, bindcred );
818 if ( op->o_tag == LDAP_REQ_BIND ) {
819 op->o_dn = save_o_dn;
820 op->o_ndn = save_o_ndn;
822 if ( isproxyauthz == -1 ) {
826 lc_curr.lc_local_ndn = op->o_ndn;
827 /* Explicit binds must not be shared;
828 * however, explicit binds are piped in a special connection
829 * when idassert is to occur with "override" set */
830 if ( op->o_tag == LDAP_REQ_BIND && !isproxyauthz ) {
831 lc_curr.lc_conn = op->o_conn;
834 if ( isproxyauthz && !( sendok & LDAP_BACK_BINDING ) ) {
835 lc_curr.lc_local_ndn = *binddn;
836 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
837 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
839 } else if ( isproxyauthz && ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) {
840 lc_curr.lc_local_ndn = slap_empty_bv;
841 LDAP_BACK_PCONN_BIND_SET( &lc_curr, op );
842 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
845 } else if ( SLAP_IS_AUTHZ_BACKEND( op ) ) {
846 lc_curr.lc_conn = op->o_conn;
849 LDAP_BACK_PCONN_ANON_SET( &lc_curr, op );
854 /* Explicit Bind requests always get their own conn */
857 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
858 if ( LDAP_BACK_PCONN_ISPRIV( &lc_curr ) ) {
859 /* lookup a conn that's not binding */
860 LDAP_TAILQ_FOREACH( lc,
861 &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv,
864 if ( !LDAP_BACK_CONN_BINDING( lc ) && lc->lc_refcnt == 0 ) {
870 if ( lc != LDAP_TAILQ_LAST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
873 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
875 LDAP_TAILQ_ENTRY_INIT( lc, lc_q );
876 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
880 } else if ( !LDAP_BACK_USE_TEMPORARIES( li )
881 && li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_num == li->li_conn_priv_max )
883 lc = LDAP_TAILQ_FIRST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv );
888 /* Searches for a ldapconn in the avl tree */
889 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
890 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
894 /* Don't reuse connections while they're still binding */
895 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
896 if ( !LDAP_BACK_USE_TEMPORARIES( li ) ) {
897 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
899 ldap_pvt_thread_yield();
906 if ( op->o_tag == LDAP_REQ_BIND ) {
907 /* right now, this is the only possible case */
908 assert( ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) );
909 LDAP_BACK_CONN_BINDING_SET( lc );
912 refcnt = ++lc->lc_refcnt;
915 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
918 /* Looks like we didn't get a bind. Open a new session... */
920 lc = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
921 lc->lc_flags = li->li_flags;
922 lc->lc_lcflags = lc_curr.lc_lcflags;
923 if ( ldap_back_prepare_conn( lc, op, rs, sendok ) != LDAP_SUCCESS ) {
928 if ( sendok & LDAP_BACK_BINDING ) {
929 LDAP_BACK_CONN_BINDING_SET( lc );
932 lc->lc_conn = lc_curr.lc_conn;
933 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
936 * the rationale is: connections as the rootdn are privileged,
937 * so acl_authcDN is to be used; however, in some cases
938 * one already configured identity assertion with a highly
939 * privileged idassert_authcDN, so if acl_authcDN is NULL
940 * and idassert_authcDN is not, use the second instead.
942 * might change in the future, because it's preferable
943 * to make clear what identity is being used, since
944 * the only drawback is that one risks to configure
945 * the same identity twice...
947 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
948 if ( BER_BVISNULL( &li->li_acl_authcDN ) && !BER_BVISNULL( &li->li_idassert_authcDN ) ) {
949 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
950 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
953 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
954 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
956 LDAP_BACK_CONN_ISPRIV_SET( lc );
958 } else if ( LDAP_BACK_CONN_ISIDASSERT( &lc_curr ) ) {
959 if ( !LDAP_BACK_PCONN_ISBIND( &lc_curr ) ) {
960 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
961 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
963 LDAP_BACK_CONN_ISIDASSERT_SET( lc );
966 BER_BVZERO( &lc->lc_cred );
967 BER_BVZERO( &lc->lc_bound_ndn );
968 if ( !BER_BVISEMPTY( &op->o_ndn )
969 && SLAP_IS_AUTHZ_BACKEND( op ) )
971 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
976 /* if start TLS failed but it was not mandatory,
977 * check if the non-TLS connection was already
978 * in cache; in case, destroy the newly created
979 * connection and use the existing one */
980 if ( LDAP_BACK_PCONN_ISTLS( lc )
981 && !ldap_tls_inplace( lc->lc_ld ) )
983 ldapconn_t *tmplc = NULL;
984 int idx = LDAP_BACK_CONN2PRIV( &lc_curr ) - 1;
986 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
987 LDAP_TAILQ_FOREACH( tmplc,
988 &li->li_conn_priv[ idx ].lic_priv,
991 if ( !LDAP_BACK_CONN_BINDING( tmplc ) ) {
996 if ( tmplc != NULL ) {
997 refcnt = ++tmplc->lc_refcnt;
998 ldap_back_conn_free( lc );
1001 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1003 if ( tmplc != NULL ) {
1007 #endif /* HAVE_TLS */
1009 /* Inserts the newly created ldapconn in the avl tree */
1010 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1012 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1014 assert( lc->lc_refcnt == 1 );
1016 #if LDAP_BACK_PRINT_CONNTREE > 0
1017 ldap_back_print_conntree( li, ">>> ldap_back_getconn(insert)" );
1018 #endif /* LDAP_BACK_PRINT_CONNTREE */
1020 if ( LDAP_BACK_PCONN_ISPRIV( lc ) ) {
1021 if ( li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num < li->li_conn_priv_max ) {
1022 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv, lc, lc_q );
1023 li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num++;
1024 LDAP_BACK_CONN_CACHED_SET( lc );
1027 LDAP_BACK_CONN_TAINTED_SET( lc );
1032 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
1033 ldap_back_conndn_cmp, ldap_back_conndn_dup );
1034 LDAP_BACK_CONN_CACHED_SET( lc );
1037 #if LDAP_BACK_PRINT_CONNTREE > 0
1038 ldap_back_print_conntree( li, "<<< ldap_back_getconn(insert)" );
1039 #endif /* LDAP_BACK_PRINT_CONNTREE */
1041 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1043 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1044 char buf[ SLAP_TEXT_BUFLEN ];
1046 snprintf( buf, sizeof( buf ),
1047 "lc=%p inserted refcnt=%u rc=%d",
1048 (void *)lc, refcnt, rs->sr_err );
1050 Debug( LDAP_DEBUG_TRACE,
1051 "=>ldap_back_getconn: %s: %s\n",
1052 op->o_log_prefix, buf, 0 );
1055 if ( !LDAP_BACK_PCONN_ISPRIV( lc ) ) {
1056 /* Err could be -1 in case a duplicate ldapconn is inserted */
1057 switch ( rs->sr_err ) {
1062 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1063 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( li ) ) {
1064 /* duplicate: free and try to get the newly created one */
1065 ldap_back_conn_free( lc );
1070 /* taint connection, so that it'll be freed when released */
1071 LDAP_BACK_CONN_TAINTED_SET( lc );
1075 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1076 ldap_back_conn_free( lc );
1077 rs->sr_err = LDAP_OTHER;
1078 rs->sr_text = "Proxy bind collision";
1079 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1080 send_ldap_result( op, rs );
1089 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
1090 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
1094 /* let it be used, but taint/delete it so that
1095 * no-one else can look it up any further */
1096 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1098 #if LDAP_BACK_PRINT_CONNTREE > 0
1099 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1100 #endif /* LDAP_BACK_PRINT_CONNTREE */
1102 (void)ldap_back_conn_delete( li, lc );
1103 LDAP_BACK_CONN_TAINTED_SET( lc );
1105 #if LDAP_BACK_PRINT_CONNTREE > 0
1106 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1107 #endif /* LDAP_BACK_PRINT_CONNTREE */
1109 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1112 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1113 char buf[ SLAP_TEXT_BUFLEN ];
1115 snprintf( buf, sizeof( buf ),
1116 "conn %p fetched refcnt=%u%s",
1118 expiring ? " expiring" : "" );
1119 Debug( LDAP_DEBUG_TRACE,
1120 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
1126 #endif /* HAVE_TLS */
1132 ldap_back_release_conn_lock(
1138 ldapconn_t *lc = *lcp;
1141 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1143 assert( lc->lc_refcnt > 0 );
1144 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1146 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
1147 ldap_back_freeconn( li, lc, 0 );
1151 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1156 ldap_back_quarantine(
1160 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1162 slap_retry_info_t *ri = &li->li_quarantine;
1164 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
1166 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1167 time_t new_last = slap_get_time();
1169 switch ( li->li_isquarantined ) {
1170 case LDAP_BACK_FQ_NO:
1171 if ( ri->ri_last == new_last ) {
1175 Debug( LDAP_DEBUG_ANY,
1176 "%s: ldap_back_quarantine enter.\n",
1177 op->o_log_prefix, 0, 0 );
1183 case LDAP_BACK_FQ_RETRYING:
1184 Debug( LDAP_DEBUG_ANY,
1185 "%s: ldap_back_quarantine block #%d try #%d failed.\n",
1186 op->o_log_prefix, ri->ri_idx, ri->ri_count );
1189 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1190 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1201 li->li_isquarantined = LDAP_BACK_FQ_YES;
1202 ri->ri_last = new_last;
1204 } else if ( li->li_isquarantined != LDAP_BACK_FQ_NO ) {
1205 if ( ri->ri_last == slap_get_time() ) {
1209 Debug( LDAP_DEBUG_ANY,
1210 "%s: ldap_back_quarantine exit (%d) err=%d.\n",
1211 op->o_log_prefix, li->li_isquarantined, rs->sr_err );
1213 if ( li->li_quarantine_f ) {
1214 (void)li->li_quarantine_f( li, li->li_quarantine_p );
1219 li->li_isquarantined = LDAP_BACK_FQ_NO;
1223 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
1227 ldap_back_dobind_cb(
1232 ber_tag_t *tptr = op->o_callback->sc_private;
1234 rs->sr_tag = slap_req2res( op->o_tag );
1236 return SLAP_CB_CONTINUE;
1240 * ldap_back_dobind_int
1242 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1245 ldap_back_dobind_int(
1249 ldap_back_send_t sendok,
1253 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1256 struct berval binddn = slap_empty_bv,
1257 bindcred = slap_empty_bv;
1263 ber_tag_t o_tag = op->o_tag;
1264 slap_callback cb = {0};
1266 assert( lcp != NULL );
1267 assert( retries >= 0 );
1269 if ( sendok & LDAP_BACK_GETCONN ) {
1270 assert( *lcp == NULL );
1272 lc = ldap_back_getconn( op, rs, sendok, &binddn, &bindcred );
1282 assert( lc != NULL );
1286 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1289 if ( binding == 0 ) {
1290 /* check if already bound */
1291 rc = isbound = LDAP_BACK_CONN_ISBOUND( lc );
1294 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1299 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
1300 /* if someone else is about to bind it, give up and retry */
1302 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1304 ldap_pvt_thread_yield();
1308 /* otherwise this thread will bind it */
1309 LDAP_BACK_CONN_BINDING_SET( lc );
1315 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1319 * FIXME: we need to let clients use proxyAuthz
1320 * otherwise we cannot do symmetric pools of servers;
1321 * we have to live with the fact that a user can
1322 * authorize itself as any ID that is allowed
1323 * by the authzTo directive of the "proxyauthzdn".
1326 * NOTE: current Proxy Authorization specification
1327 * and implementation do not allow proxy authorization
1328 * control to be provided with Bind requests
1331 * if no bind took place yet, but the connection is bound
1332 * and the "idassert-authcDN" (or other ID) is set,
1333 * then bind as the asserting identity and explicitly
1334 * add the proxyAuthz control to every operation with the
1335 * dn bound to the connection as control value.
1336 * This is done also if this is the authorizing backend,
1337 * but the "override" flag is given to idassert.
1338 * It allows to use SASL bind and yet proxyAuthz users
1340 op->o_tag = LDAP_REQ_BIND;
1341 cb.sc_next = op->o_callback;
1342 cb.sc_private = &o_tag;
1343 cb.sc_response = ldap_back_dobind_cb;
1344 op->o_callback = &cb;
1346 if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
1347 if ( BER_BVISEMPTY( &binddn ) && BER_BVISEMPTY( &bindcred ) ) {
1348 /* if we got here, it shouldn't return result */
1349 rc = ldap_back_is_proxy_authz( op, rs,
1350 LDAP_BACK_DONTSEND, &binddn, &bindcred );
1353 rc = ldap_back_proxy_authz_bind( lc, op, rs, sendok, &binddn, &bindcred );
1357 #ifdef HAVE_CYRUS_SASL
1358 if ( LDAP_BACK_CONN_ISPRIV( lc )
1359 && li->li_acl_authmethod == LDAP_AUTH_SASL )
1361 void *defaults = NULL;
1363 if ( li->li_acl_secprops != NULL ) {
1364 rc = ldap_set_option( lc->lc_ld,
1365 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops );
1367 if ( rc != LDAP_OPT_SUCCESS ) {
1368 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
1369 "(SECPROPS,\"%s\") failed!\n",
1370 li->li_acl_secprops, 0, 0 );
1375 defaults = lutil_sasl_defaults( lc->lc_ld,
1376 li->li_acl_sasl_mech.bv_val,
1377 li->li_acl_sasl_realm.bv_val,
1378 li->li_acl_authcID.bv_val,
1379 li->li_acl_passwd.bv_val,
1381 if ( defaults == NULL ) {
1382 rs->sr_err = LDAP_OTHER;
1383 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1384 if ( sendok & LDAP_BACK_SENDERR ) {
1385 send_ldap_result( op, rs );
1390 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
1391 li->li_acl_authcDN.bv_val,
1392 li->li_acl_sasl_mech.bv_val, NULL, NULL,
1393 LDAP_SASL_QUIET, lutil_sasl_interact,
1396 lutil_sasl_freedefs( defaults );
1398 switch ( rs->sr_err ) {
1400 LDAP_BACK_CONN_ISBOUND_SET( lc );
1403 case LDAP_LOCAL_ERROR:
1404 /* list client API error codes that require
1405 * to taint the connection */
1406 /* FIXME: should actually retry? */
1407 LDAP_BACK_CONN_TAINTED_SET( lc );
1412 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1413 rs->sr_err = slap_map_api2result( rs );
1414 if ( sendok & LDAP_BACK_SENDERR ) {
1415 send_ldap_result( op, rs );
1420 if ( LDAP_BACK_QUARANTINE( li ) ) {
1421 ldap_back_quarantine( op, rs );
1426 #endif /* HAVE_CYRUS_SASL */
1429 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1430 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1431 LDAP_SASL_SIMPLE, &lc->lc_cred,
1432 NULL, NULL, &msgid );
1434 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
1435 if ( retries != LDAP_BACK_RETRY_NEVER ) {
1437 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1440 assert( lc->lc_refcnt > 0 );
1441 if ( lc->lc_refcnt == 1 ) {
1442 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1445 /* lc here must be the regular lc, reset and ready for init */
1446 rs->sr_err = ldap_back_prepare_conn( lc, op, rs, sendok );
1447 if ( rs->sr_err != LDAP_SUCCESS ) {
1448 sendok &= ~LDAP_BACK_SENDERR;
1454 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1457 if ( rs->sr_err == LDAP_SUCCESS ) {
1458 if ( retries > 0 ) {
1465 assert( lc->lc_refcnt == 1 );
1467 ldap_back_freeconn( li, lc, dolock );
1469 rs->sr_err = slap_map_api2result( rs );
1471 if ( LDAP_BACK_QUARANTINE( li ) ) {
1472 ldap_back_quarantine( op, rs );
1475 if ( rs->sr_err != LDAP_SUCCESS &&
1476 ( sendok & LDAP_BACK_SENDERR ) )
1478 if ( op->o_callback == &cb )
1479 op->o_callback = cb.sc_next;
1481 rs->sr_text = "Proxy can't contact remote server";
1482 send_ldap_result( op, rs );
1489 rc = ldap_back_op_result( lc, op, rs, msgid,
1490 -1, ( sendok | LDAP_BACK_BINDING ) );
1491 if ( rc == LDAP_SUCCESS ) {
1492 LDAP_BACK_CONN_ISBOUND_SET( lc );
1496 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1497 rc = LDAP_BACK_CONN_ISBOUND( lc );
1499 ldap_back_release_conn_lock( li, lcp, dolock );
1501 } else if ( LDAP_BACK_SAVECRED( li ) ) {
1502 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1506 if ( op->o_callback == &cb )
1507 op->o_callback = cb.sc_next;
1516 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1519 ldap_back_dobind( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1521 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1523 return ldap_back_dobind_int( lcp, op, rs,
1524 ( sendok | LDAP_BACK_GETCONN ), li->li_nretries, 1 );
1528 * ldap_back_default_rebind
1530 * This is a callback used for chasing referrals using the same
1531 * credentials as the original user on this session.
1534 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1535 ber_int_t msgid, void *params )
1537 ldapconn_t *lc = (ldapconn_t *)params;
1540 /* ... otherwise we couldn't get here */
1541 assert( lc != NULL );
1543 if ( !ldap_tls_inplace( ld ) ) {
1544 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1546 const char *text = NULL;
1548 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1549 LDAP_BACK_RETRY_DEFAULT, &text );
1550 if ( rc != LDAP_SUCCESS ) {
1554 #endif /* HAVE_TLS */
1556 /* FIXME: add checks on the URL/identity? */
1558 return ldap_sasl_bind_s( ld,
1559 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1560 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1564 * ldap_back_default_urllist
1567 ldap_back_default_urllist(
1569 LDAPURLDesc **urllist,
1573 ldapinfo_t *li = (ldapinfo_t *)params;
1574 LDAPURLDesc **urltail;
1576 if ( urllist == url ) {
1577 return LDAP_SUCCESS;
1580 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1583 *urltail = *urllist;
1587 if ( !li->li_uri_mutex_do_not_lock ) {
1588 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1592 ch_free( li->li_uri );
1595 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1597 if ( !li->li_uri_mutex_do_not_lock ) {
1598 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1601 return LDAP_SUCCESS;
1610 ldap_back_send_t sendok )
1612 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1614 /* default behavior */
1615 if ( LDAP_BACK_ABANDON( li ) ) {
1616 return ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1619 if ( LDAP_BACK_IGNORE( li ) ) {
1620 return ldap_pvt_discard( lc->lc_ld, msgid );
1623 if ( LDAP_BACK_CANCEL( li ) ) {
1624 /* FIXME: asynchronous? */
1625 return ldap_cancel_s( lc->lc_ld, msgid, NULL, NULL );
1634 ldap_back_op_result(
1640 ldap_back_send_t sendok )
1642 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1647 LDAPControl **ctrls = NULL;
1650 rs->sr_matched = NULL;
1652 rs->sr_ctrls = NULL;
1654 /* if the error recorded in the reply corresponds
1655 * to a successful state, get the error from the
1656 * remote server response */
1657 if ( LDAP_ERR_OK( rs->sr_err ) ) {
1660 LDAPMessage *res = NULL;
1661 time_t stoptime = (time_t)(-1);
1662 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
1663 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
1664 const char *timeout_text = "Operation timed out";
1666 /* if timeout is not specified, compute and use
1667 * the one specific to the ongoing operation */
1668 if ( timeout == (time_t)(-1) ) {
1669 slap_op_t opidx = slap_req2op( op->o_tag );
1671 if ( opidx == SLAP_OP_SEARCH ) {
1672 if ( op->ors_tlimit <= 0 ) {
1676 timeout = op->ors_tlimit;
1677 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
1678 timeout_text = NULL;
1682 timeout = li->li_timeout[ opidx ];
1686 /* better than nothing :) */
1687 if ( timeout == 0 ) {
1688 if ( li->li_idle_timeout ) {
1689 timeout = li->li_idle_timeout;
1691 } else if ( li->li_conn_ttl ) {
1692 timeout = li->li_conn_ttl;
1697 stoptime = op->o_time + timeout;
1700 LDAP_BACK_TV_SET( &tv );
1703 /* if result parsing fails, note the failure reason */
1704 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1707 if ( timeout && slap_get_time() > stoptime ) {
1708 if ( sendok & LDAP_BACK_BINDING ) {
1709 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1712 /* let it be used, but taint/delete it so that
1713 * no-one else can look it up any further */
1714 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1716 #if LDAP_BACK_PRINT_CONNTREE > 0
1717 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1718 #endif /* LDAP_BACK_PRINT_CONNTREE */
1720 (void)ldap_back_conn_delete( li, lc );
1721 LDAP_BACK_CONN_TAINTED_SET( lc );
1723 #if LDAP_BACK_PRINT_CONNTREE > 0
1724 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1725 #endif /* LDAP_BACK_PRINT_CONNTREE */
1726 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1729 (void)ldap_back_cancel( lc, op, rs, msgid, sendok );
1731 rs->sr_err = timeout_err;
1732 rs->sr_text = timeout_text;
1737 LDAP_BACK_TV_SET( &tv );
1738 ldap_pvt_thread_yield();
1742 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1747 /* otherwise get the result; if it is not
1748 * LDAP_SUCCESS, record it in the reply
1749 * structure (this includes
1750 * LDAP_COMPARE_{TRUE|FALSE}) */
1752 /* only touch when activity actually took place... */
1753 if ( li->li_idle_timeout && lc ) {
1754 lc->lc_time = op->o_time;
1757 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1758 &match, &text, &refs, &ctrls, 1 );
1760 if ( rc != LDAP_SUCCESS ) {
1764 /* RFC 4511: referrals can only appear
1765 * if result code is LDAP_REFERRAL */
1767 && refs[ 0 ] != NULL
1768 && refs[ 0 ][ 0 ] != '\0' )
1770 if ( rs->sr_err != LDAP_REFERRAL ) {
1771 Debug( LDAP_DEBUG_ANY,
1772 "%s ldap_back_op_result: "
1773 "got referrals with err=%d\n",
1780 for ( i = 0; refs[ i ] != NULL; i++ )
1782 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1784 for ( i = 0; refs[ i ] != NULL; i++ ) {
1785 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1787 BER_BVZERO( &rs->sr_ref[ i ] );
1790 } else if ( rs->sr_err == LDAP_REFERRAL ) {
1791 Debug( LDAP_DEBUG_ANY,
1792 "%s ldap_back_op_result: "
1793 "got err=%d with null "
1794 "or empty referrals\n",
1798 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1801 if ( ctrls != NULL ) {
1802 rs->sr_ctrls = ctrls;
1807 /* if the error in the reply structure is not
1808 * LDAP_SUCCESS, try to map it from client
1809 * to server error */
1810 if ( !LDAP_ERR_OK( rs->sr_err ) ) {
1811 rs->sr_err = slap_map_api2result( rs );
1813 /* internal ops ( op->o_conn == NULL )
1814 * must not reply to client */
1815 if ( op->o_conn && !op->o_do_not_cache && match ) {
1817 /* record the (massaged) matched
1818 * DN into the reply structure */
1819 rs->sr_matched = match;
1823 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1824 if ( !( sendok & LDAP_BACK_RETRYING ) ) {
1825 if ( LDAP_BACK_QUARANTINE( li ) ) {
1826 ldap_back_quarantine( op, rs );
1828 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1829 if ( rs->sr_text == NULL ) rs->sr_text = "Proxy operation retry failed";
1830 send_ldap_result( op, rs );
1834 } else if ( op->o_conn &&
1835 ( ( ( sendok & LDAP_BACK_SENDOK ) && LDAP_ERR_OK( rs->sr_err ) )
1836 || ( ( sendok & LDAP_BACK_SENDERR ) && !LDAP_ERR_OK( rs->sr_err ) ) ) )
1838 send_ldap_result( op, rs );
1842 if ( rs->sr_matched != match ) {
1843 free( (char *)rs->sr_matched );
1845 rs->sr_matched = NULL;
1846 ldap_memfree( match );
1850 ldap_memfree( text );
1855 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1860 ber_memvfree( (void **)refs );
1864 assert( rs->sr_ctrls != NULL );
1865 ldap_controls_free( ctrls );
1866 rs->sr_ctrls = NULL;
1869 return( LDAP_ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1872 /* return true if bound, false if failed */
1874 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1876 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1879 assert( lcp != NULL );
1880 assert( *lcp != NULL );
1882 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1884 if ( (*lcp)->lc_refcnt == 1 ) {
1885 int binding = LDAP_BACK_CONN_BINDING( *lcp );
1887 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1888 Debug( LDAP_DEBUG_ANY,
1889 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1890 op->o_log_prefix, li->li_uri,
1891 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1892 "" : (*lcp)->lc_bound_ndn.bv_val );
1893 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1895 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1896 (*lcp)->lc_ld = NULL;
1897 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1899 /* lc here must be the regular lc, reset and ready for init */
1900 rc = ldap_back_prepare_conn( *lcp, op, rs, sendok );
1901 if ( rc != LDAP_SUCCESS ) {
1902 /* freeit, because lc_refcnt == 1 */
1903 (*lcp)->lc_refcnt = 0;
1904 (void)ldap_back_freeconn( li, *lcp, 0 );
1908 } else if ( ( sendok & LDAP_BACK_BINDING ) ) {
1910 LDAP_BACK_CONN_BINDING_SET( *lcp );
1915 rc = ldap_back_dobind_int( lcp, op, rs, sendok, 0, 0 );
1916 if ( rc == 0 && *lcp != NULL ) {
1917 /* freeit, because lc_refcnt == 1 */
1918 (*lcp)->lc_refcnt = 0;
1919 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1920 (void)ldap_back_freeconn( li, *lcp, 0 );
1926 Debug( LDAP_DEBUG_TRACE,
1927 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1928 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1930 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1931 ldap_back_release_conn_lock( li, lcp, 0 );
1932 assert( *lcp == NULL );
1934 if ( sendok & LDAP_BACK_SENDERR ) {
1935 rs->sr_err = LDAP_UNAVAILABLE;
1936 rs->sr_text = "Unable to retry";
1937 send_ldap_result( op, rs );
1941 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1947 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
1948 struct berval *binddn, struct berval *bindcred )
1950 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1954 if ( op->o_conn == NULL || op->o_do_not_cache ) {
1958 /* don't proxyAuthz if protocol is not LDAPv3 */
1959 switch ( li->li_version ) {
1964 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1970 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1971 if ( sendok & LDAP_BACK_SENDERR ) {
1972 send_ldap_result( op, rs );
1979 *binddn = slap_empty_bv;
1980 *bindcred = slap_empty_bv;
1982 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1983 ndn = op->o_conn->c_ndn;
1989 switch ( li->li_idassert_mode ) {
1990 case LDAP_BACK_IDASSERT_LEGACY:
1991 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1992 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1994 *binddn = li->li_idassert_authcDN;
1995 *bindcred = li->li_idassert_passwd;
2002 /* NOTE: rootdn can always idassert */
2003 if ( BER_BVISNULL( &ndn )
2004 && li->li_idassert_authz == NULL
2005 && !( li->li_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL ) )
2007 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2008 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
2009 if ( sendok & LDAP_BACK_SENDERR ) {
2010 send_ldap_result( op, rs );
2015 rs->sr_err = LDAP_SUCCESS;
2016 *binddn = slap_empty_bv;
2017 *bindcred = slap_empty_bv;
2023 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
2024 struct berval authcDN;
2026 if ( BER_BVISNULL( &ndn ) ) {
2027 authcDN = slap_empty_bv;
2032 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
2033 &authcDN, &authcDN );
2034 if ( rs->sr_err != LDAP_SUCCESS ) {
2035 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2036 if ( sendok & LDAP_BACK_SENDERR ) {
2037 send_ldap_result( op, rs );
2042 rs->sr_err = LDAP_SUCCESS;
2043 *binddn = slap_empty_bv;
2044 *bindcred = slap_empty_bv;
2052 *binddn = li->li_idassert_authcDN;
2053 *bindcred = li->li_idassert_passwd;
2063 ldap_back_proxy_authz_bind(
2067 ldap_back_send_t sendok,
2068 struct berval *binddn,
2069 struct berval *bindcred )
2071 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2076 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2077 ndn = op->o_conn->c_ndn;
2083 if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
2084 #ifdef HAVE_CYRUS_SASL
2085 void *defaults = NULL;
2086 struct berval authzID = BER_BVNULL;
2089 /* if SASL supports native authz, prepare for it */
2090 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
2091 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2093 switch ( li->li_idassert_mode ) {
2094 case LDAP_BACK_IDASSERT_OTHERID:
2095 case LDAP_BACK_IDASSERT_OTHERDN:
2096 authzID = li->li_idassert_authzID;
2099 case LDAP_BACK_IDASSERT_ANONYMOUS:
2100 BER_BVSTR( &authzID, "dn:" );
2103 case LDAP_BACK_IDASSERT_SELF:
2104 if ( BER_BVISNULL( &ndn ) ) {
2105 /* connection is not authc'd, so don't idassert */
2106 BER_BVSTR( &authzID, "dn:" );
2109 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
2110 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
2111 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
2112 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
2113 ndn.bv_val, ndn.bv_len + 1 );
2122 if ( li->li_idassert_secprops != NULL ) {
2123 rs->sr_err = ldap_set_option( lc->lc_ld,
2124 LDAP_OPT_X_SASL_SECPROPS,
2125 (void *)li->li_idassert_secprops );
2127 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
2128 rs->sr_err = LDAP_OTHER;
2129 if ( sendok & LDAP_BACK_SENDERR ) {
2130 send_ldap_result( op, rs );
2132 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2137 defaults = lutil_sasl_defaults( lc->lc_ld,
2138 li->li_idassert_sasl_mech.bv_val,
2139 li->li_idassert_sasl_realm.bv_val,
2140 li->li_idassert_authcID.bv_val,
2141 li->li_idassert_passwd.bv_val,
2143 if ( defaults == NULL ) {
2144 rs->sr_err = LDAP_OTHER;
2145 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2146 if ( sendok & LDAP_BACK_SENDERR ) {
2147 send_ldap_result( op, rs );
2152 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn->bv_val,
2153 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
2154 LDAP_SASL_QUIET, lutil_sasl_interact,
2157 switch ( rs->sr_err ) {
2159 LDAP_BACK_CONN_ISBOUND_SET( lc );
2162 case LDAP_LOCAL_ERROR:
2163 /* list client API error codes that require
2164 * to taint the connection */
2165 /* FIXME: should actually retry? */
2166 LDAP_BACK_CONN_TAINTED_SET( lc );
2171 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2172 rs->sr_err = slap_map_api2result( rs );
2173 if ( sendok & LDAP_BACK_SENDERR ) {
2174 send_ldap_result( op, rs );
2179 lutil_sasl_freedefs( defaults );
2181 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
2185 #endif /* HAVE_CYRUS_SASL */
2188 switch ( li->li_idassert_authmethod ) {
2189 case LDAP_AUTH_NONE:
2190 /* FIXME: do we really need this? */
2191 BER_BVSTR( binddn, "" );
2192 BER_BVSTR( bindcred, "" );
2195 case LDAP_AUTH_SIMPLE:
2196 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
2197 binddn->bv_val, LDAP_SASL_SIMPLE,
2198 bindcred, NULL, NULL, &msgid );
2199 rc = ldap_back_op_result( lc, op, rs, msgid,
2200 -1, ( sendok | LDAP_BACK_BINDING ) );
2205 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2206 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
2207 if ( sendok & LDAP_BACK_SENDERR ) {
2208 send_ldap_result( op, rs );
2213 if ( rc == LDAP_SUCCESS ) {
2214 /* set rebind stuff in case of successful proxyAuthz bind,
2215 * so that referral chasing is attempted using the right
2217 LDAP_BACK_CONN_ISBOUND_SET( lc );
2218 if ( !BER_BVISNULL( binddn ) ) {
2219 ber_bvreplace( &lc->lc_bound_ndn, binddn );
2222 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
2223 memset( lc->lc_cred.bv_val, 0,
2224 lc->lc_cred.bv_len );
2227 if ( LDAP_BACK_SAVECRED( li ) ) {
2228 if ( !BER_BVISNULL( bindcred ) ) {
2229 ber_bvreplace( &lc->lc_cred, bindcred );
2230 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
2234 lc->lc_cred.bv_len = 0;
2238 return LDAP_BACK_CONN_ISBOUND( lc );
2242 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
2243 * to existing server-side controls if required; if not,
2244 * the existing server-side controls are placed in *pctrls.
2245 * The caller, after using the controls in client API
2246 * operations, if ( *pctrls != op->o_ctrls ), should
2247 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
2248 * The function returns success if the control could
2249 * be added if required, or if it did nothing; in the future,
2250 * it might return some error if it failed.
2252 * if no bind took place yet, but the connection is bound
2253 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
2254 * and explicitly add proxyAuthz the control to every operation
2255 * with the dn bound to the connection as control value.
2257 * If no server-side controls are defined for the operation,
2258 * simply add the proxyAuthz control; otherwise, if the
2259 * proxyAuthz control is not already set, add it as
2262 * FIXME: is controls order significant for security?
2263 * ANSWER: controls ordering and interoperability
2264 * must be indicated by the specs of each control; if none
2265 * is specified, the order is irrelevant.
2268 ldap_back_proxy_authz_ctrl(
2271 struct berval *bound_ndn,
2273 slap_idassert_t *si,
2276 slap_idassert_mode_t mode;
2277 struct berval assertedID,
2281 rs->sr_err = SLAP_CB_CONTINUE;
2283 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
2284 * but if it is not set this test fails. We need a different
2285 * means to detect if idassert is enabled */
2286 if ( ( BER_BVISNULL( &si->si_bc.sb_authcId ) || BER_BVISEMPTY( &si->si_bc.sb_authcId ) )
2287 && ( BER_BVISNULL( &si->si_bc.sb_binddn ) || BER_BVISEMPTY( &si->si_bc.sb_binddn ) )
2288 && BER_BVISNULL( &si->si_bc.sb_saslmech ) )
2293 if ( !op->o_conn || op->o_do_not_cache || ( isroot = be_isroot( op ) ) ) {
2297 if ( op->o_tag == LDAP_REQ_BIND ) {
2298 ndn = op->o_req_ndn;
2300 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2301 ndn = op->o_conn->c_ndn;
2307 if ( si->si_mode == LDAP_BACK_IDASSERT_LEGACY ) {
2308 if ( op->o_proxy_authz ) {
2310 * FIXME: we do not want to perform proxyAuthz
2311 * on behalf of the client, because this would
2312 * be performed with "proxyauthzdn" privileges.
2314 * This might actually be too strict, since
2315 * the "proxyauthzdn" authzTo, and each entry's
2316 * authzFrom attributes may be crafted
2317 * to avoid unwanted proxyAuthz to take place.
2320 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2321 rs->sr_text = "proxyAuthz not allowed within namingContext";
2326 if ( !BER_BVISNULL( bound_ndn ) ) {
2330 if ( BER_BVISNULL( &ndn ) ) {
2334 if ( BER_BVISNULL( &si->si_bc.sb_binddn ) ) {
2338 } else if ( si->si_bc.sb_method == LDAP_AUTH_SASL ) {
2339 if ( ( si->si_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2341 /* already asserted in SASL via native authz */
2345 } else if ( si->si_authz && !isroot ) {
2347 struct berval authcDN;
2349 if ( BER_BVISNULL( &ndn ) ) {
2350 authcDN = slap_empty_bv;
2354 rc = slap_sasl_matches( op, si->si_authz,
2355 &authcDN, &authcDN );
2356 if ( rc != LDAP_SUCCESS ) {
2357 if ( si->si_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2358 /* ndn is not authorized
2359 * to use idassert */
2366 if ( op->o_proxy_authz ) {
2369 * 1) ignore the already set proxyAuthz control
2370 * 2) leave it in place, and don't set ours
2372 * 4) reject the operation
2374 * option (4) is very drastic
2375 * option (3) will make the remote server reject
2376 * the operation, thus being equivalent to (4)
2377 * option (2) will likely break the idassert
2378 * assumptions, so we cannot accept it;
2379 * option (1) means that we are contradicting
2380 * the client's reques.
2382 * I think (4) is the only correct choice.
2384 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2385 rs->sr_text = "proxyAuthz not allowed within namingContext";
2388 if ( op->o_is_auth_check ) {
2389 mode = LDAP_BACK_IDASSERT_NOASSERT;
2396 case LDAP_BACK_IDASSERT_LEGACY:
2397 /* original behavior:
2398 * assert the client's identity */
2399 case LDAP_BACK_IDASSERT_SELF:
2403 case LDAP_BACK_IDASSERT_ANONYMOUS:
2404 /* assert "anonymous" */
2405 assertedID = slap_empty_bv;
2408 case LDAP_BACK_IDASSERT_NOASSERT:
2409 /* don't assert; bind as proxyauthzdn */
2412 case LDAP_BACK_IDASSERT_OTHERID:
2413 case LDAP_BACK_IDASSERT_OTHERDN:
2414 /* assert idassert DN */
2415 assertedID = si->si_bc.sb_authzId;
2422 /* if we got here, "" is allowed to proxyAuthz */
2423 if ( BER_BVISNULL( &assertedID ) ) {
2424 assertedID = slap_empty_bv;
2427 /* don't idassert the bound DN (ITS#4497) */
2428 if ( dn_match( &assertedID, bound_ndn ) ) {
2432 ctrl->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
2434 switch ( si->si_mode ) {
2435 /* already in u:ID or dn:DN form */
2436 case LDAP_BACK_IDASSERT_OTHERID:
2437 case LDAP_BACK_IDASSERT_OTHERDN:
2438 ber_dupbv_x( &ctrl->ldctl_value, &assertedID, op->o_tmpmemctx );
2439 rs->sr_err = LDAP_SUCCESS;
2442 /* needs the dn: prefix */
2444 ctrl->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
2445 ctrl->ldctl_value.bv_val = op->o_tmpalloc( ctrl->ldctl_value.bv_len + 1,
2447 AC_MEMCPY( ctrl->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
2448 AC_MEMCPY( &ctrl->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
2449 assertedID.bv_val, assertedID.bv_len + 1 );
2450 rs->sr_err = LDAP_SUCCESS;
2454 /* Older versions of <draft-weltman-ldapv3-proxy> required
2455 * to encode the value of the authzID (and called it proxyDN);
2456 * this hack provides compatibility with those DSAs that
2457 * implement it this way */
2458 if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
2459 struct berval authzID = ctrl->ldctl_value;
2460 BerElementBuffer berbuf;
2461 BerElement *ber = (BerElement *)&berbuf;
2464 ber_init2( ber, 0, LBER_USE_DER );
2465 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2467 tag = ber_printf( ber, "O", &authzID );
2468 if ( tag == LBER_ERROR ) {
2469 rs->sr_err = LDAP_OTHER;
2473 if ( ber_flatten2( ber, &ctrl->ldctl_value, 1 ) == -1 ) {
2474 rs->sr_err = LDAP_OTHER;
2478 rs->sr_err = LDAP_SUCCESS;
2481 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2482 ber_free_buf( ber );
2484 if ( rs->sr_err != LDAP_SUCCESS ) {
2488 } else if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
2489 struct berval authzID = ctrl->ldctl_value,
2491 BerElementBuffer berbuf;
2492 BerElement *ber = (BerElement *)&berbuf;
2495 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
2496 rs->sr_err = LDAP_PROTOCOL_ERROR;
2501 tmp.bv_val += STRLENOF( "dn:" );
2502 tmp.bv_len -= STRLENOF( "dn:" );
2504 ber_init2( ber, 0, LBER_USE_DER );
2505 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2507 /* apparently, Mozilla API encodes this
2508 * as "SEQUENCE { LDAPDN }" */
2509 tag = ber_printf( ber, "{O}", &tmp );
2510 if ( tag == LBER_ERROR ) {
2511 rs->sr_err = LDAP_OTHER;
2515 if ( ber_flatten2( ber, &ctrl->ldctl_value, 1 ) == -1 ) {
2516 rs->sr_err = LDAP_OTHER;
2520 ctrl->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
2521 rs->sr_err = LDAP_SUCCESS;
2524 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2525 ber_free_buf( ber );
2527 if ( rs->sr_err != LDAP_SUCCESS ) {
2540 * if any needs to be added, it is prepended to existing ones,
2541 * in a newly allocated array. The companion function
2542 * ldap_back_controls_free() must be used to restore the original
2543 * status of op->o_ctrls.
2546 ldap_back_controls_add(
2550 LDAPControl ***pctrls )
2552 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2554 LDAPControl **ctrls = NULL;
2555 /* set to the maximum number of controls this backend can add */
2556 LDAPControl c[ 2 ] = { { 0 } };
2557 int n = 0, i, j1 = 0, j2 = 0;
2561 rs->sr_err = LDAP_SUCCESS;
2563 /* don't add controls if protocol is not LDAPv3 */
2564 switch ( li->li_version ) {
2569 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2578 /* put controls that go __before__ existing ones here */
2580 /* proxyAuthz for identity assertion */
2581 switch ( ldap_back_proxy_authz_ctrl( op, rs, &lc->lc_bound_ndn,
2582 li->li_version, &li->li_idassert, &c[ j1 ] ) )
2584 case SLAP_CB_CONTINUE:
2595 /* put controls that go __after__ existing ones here */
2597 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
2598 /* FIXME: according to <draft-wahl-ldap-session>,
2599 * the server should check if the control can be added
2600 * based on the identity of the client and so */
2602 /* session tracking */
2603 if ( LDAP_BACK_ST_REQUEST( li ) ) {
2604 switch ( slap_ctrl_session_tracking_request_add( op, rs, &c[ j1 + j2 ] ) ) {
2605 case SLAP_CB_CONTINUE:
2616 #endif /* SLAP_CONTROL_X_SESSION_TRACKING */
2618 if ( rs->sr_err == SLAP_CB_CONTINUE ) {
2619 rs->sr_err = LDAP_SUCCESS;
2622 /* if nothing to do, just bail out */
2623 if ( j1 == 0 && j2 == 0 ) {
2627 assert( j1 + j2 <= (int) (sizeof( c )/sizeof( c[0] )) );
2629 if ( op->o_ctrls ) {
2630 for ( n = 0; op->o_ctrls[ n ]; n++ )
2631 /* just count ctrls */ ;
2634 ctrls = op->o_tmpalloc( (n + j1 + j2 + 1) * sizeof( LDAPControl * ) + ( j1 + j2 ) * sizeof( LDAPControl ),
2637 ctrls[ 0 ] = (LDAPControl *)&ctrls[ n + j1 + j2 + 1 ];
2638 *ctrls[ 0 ] = c[ 0 ];
2639 for ( i = 1; i < j1; i++ ) {
2640 ctrls[ i ] = &ctrls[ 0 ][ i ];
2641 *ctrls[ i ] = c[ i ];
2646 if ( op->o_ctrls ) {
2647 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
2648 ctrls[ i + j1 ] = op->o_ctrls[ i ];
2654 ctrls[ n ] = (LDAPControl *)&ctrls[ n + j2 + 1 ] + j1;
2655 *ctrls[ n ] = c[ j1 ];
2656 for ( i = 1; i < j2; i++ ) {
2657 ctrls[ n + i ] = &ctrls[ n ][ i ];
2658 *ctrls[ n + i ] = c[ i ];
2662 ctrls[ n + j2 ] = NULL;
2665 if ( ctrls == NULL ) {
2666 ctrls = op->o_ctrls;
2675 ldap_back_controls_free( Operation *op, SlapReply *rs, LDAPControl ***pctrls )
2677 LDAPControl **ctrls = *pctrls;
2679 /* we assume that the controls added by the proxy come first,
2680 * so as soon as we find op->o_ctrls[ 0 ] we can stop */
2681 if ( ctrls && ctrls != op->o_ctrls ) {
2682 int i = 0, n = 0, n_added;
2683 LDAPControl *lower, *upper;
2685 assert( ctrls[ 0 ] != NULL );
2687 for ( n = 0; ctrls[ n ] != NULL; n++ )
2690 if ( op->o_ctrls ) {
2691 for ( i = 0; op->o_ctrls[ i ] != NULL; i++ )
2696 lower = (LDAPControl *)&ctrls[ n ];
2697 upper = &lower[ n_added ];
2699 for ( i = 0; ctrls[ i ] != NULL; i++ ) {
2700 if ( ctrls[ i ] < lower || ctrls[ i ] >= upper ) {
2701 /* original; don't touch */
2705 if ( !BER_BVISNULL( &ctrls[ i ]->ldctl_value ) ) {
2706 op->o_tmpfree( ctrls[ i ]->ldctl_value.bv_val, op->o_tmpmemctx );
2710 op->o_tmpfree( ctrls, op->o_tmpmemctx );