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,
1356 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
1357 li->li_acl_authcDN.bv_val,
1358 li->li_acl_sasl_mech.bv_val, NULL, NULL,
1359 LDAP_SASL_QUIET, lutil_sasl_interact,
1362 lutil_sasl_freedefs( defaults );
1364 rs->sr_err = slap_map_api2result( rs );
1365 if ( rs->sr_err != LDAP_SUCCESS ) {
1366 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1367 if ( sendok & LDAP_BACK_SENDERR ) {
1368 send_ldap_result( op, rs );
1372 LDAP_BACK_CONN_ISBOUND_SET( lc );
1375 if ( LDAP_BACK_QUARANTINE( li ) ) {
1376 ldap_back_quarantine( op, rs );
1381 #endif /* HAVE_CYRUS_SASL */
1384 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1385 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1386 LDAP_SASL_SIMPLE, &lc->lc_cred,
1387 NULL, NULL, &msgid );
1389 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
1390 if ( retries != LDAP_BACK_RETRY_NEVER ) {
1392 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1395 assert( lc->lc_refcnt > 0 );
1396 if ( lc->lc_refcnt == 1 ) {
1397 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1400 /* lc here must be the regular lc, reset and ready for init */
1401 rs->sr_err = ldap_back_prepare_conn( lc, op, rs, sendok );
1402 if ( rs->sr_err != LDAP_SUCCESS ) {
1403 sendok &= ~LDAP_BACK_SENDERR;
1409 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1412 if ( rs->sr_err == LDAP_SUCCESS ) {
1413 if ( retries > 0 ) {
1420 assert( lc->lc_refcnt == 1 );
1422 ldap_back_freeconn( li, lc, dolock );
1424 rs->sr_err = slap_map_api2result( rs );
1426 if ( LDAP_BACK_QUARANTINE( li ) ) {
1427 ldap_back_quarantine( op, rs );
1430 if ( rs->sr_err != LDAP_SUCCESS &&
1431 ( sendok & LDAP_BACK_SENDERR ) )
1433 if ( op->o_callback == &cb )
1434 op->o_callback = cb.sc_next;
1436 rs->sr_text = "Internal proxy bind failure";
1437 send_ldap_result( op, rs );
1444 rc = ldap_back_op_result( lc, op, rs, msgid,
1445 -1, ( sendok | LDAP_BACK_BINDING ) );
1446 if ( rc == LDAP_SUCCESS ) {
1447 LDAP_BACK_CONN_ISBOUND_SET( lc );
1451 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1452 rc = LDAP_BACK_CONN_ISBOUND( lc );
1454 ldap_back_release_conn_lock( li, lcp, dolock );
1456 } else if ( LDAP_BACK_SAVECRED( li ) ) {
1457 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1461 if ( op->o_callback == &cb )
1462 op->o_callback = cb.sc_next;
1471 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1474 ldap_back_dobind( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1476 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1478 return ldap_back_dobind_int( lcp, op, rs,
1479 ( sendok | LDAP_BACK_GETCONN ), li->li_nretries, 1 );
1483 * ldap_back_default_rebind
1485 * This is a callback used for chasing referrals using the same
1486 * credentials as the original user on this session.
1489 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1490 ber_int_t msgid, void *params )
1492 ldapconn_t *lc = (ldapconn_t *)params;
1495 /* ... otherwise we couldn't get here */
1496 assert( lc != NULL );
1498 if ( !ldap_tls_inplace( ld ) ) {
1499 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1501 const char *text = NULL;
1503 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1504 LDAP_BACK_RETRY_DEFAULT, &text );
1505 if ( rc != LDAP_SUCCESS ) {
1509 #endif /* HAVE_TLS */
1511 /* FIXME: add checks on the URL/identity? */
1513 return ldap_sasl_bind_s( ld,
1514 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1515 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1519 * ldap_back_default_urllist
1522 ldap_back_default_urllist(
1524 LDAPURLDesc **urllist,
1528 ldapinfo_t *li = (ldapinfo_t *)params;
1529 LDAPURLDesc **urltail;
1531 if ( urllist == url ) {
1532 return LDAP_SUCCESS;
1535 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1538 *urltail = *urllist;
1542 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1544 ch_free( li->li_uri );
1547 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1548 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1550 return LDAP_SUCCESS;
1559 ldap_back_send_t sendok )
1561 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1563 /* default behavior */
1564 if ( LDAP_BACK_ABANDON( li ) ) {
1565 return ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1568 if ( LDAP_BACK_IGNORE( li ) ) {
1569 return ldap_pvt_discard( lc->lc_ld, msgid );
1572 if ( LDAP_BACK_CANCEL( li ) ) {
1573 /* FIXME: asynchronous? */
1574 return ldap_cancel_s( lc->lc_ld, msgid, NULL, NULL );
1583 ldap_back_op_result(
1589 ldap_back_send_t sendok )
1591 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1596 LDAPControl **ctrls = NULL;
1598 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
1601 rs->sr_matched = NULL;
1603 rs->sr_ctrls = NULL;
1605 /* if the error recorded in the reply corresponds
1606 * to a successful state, get the error from the
1607 * remote server response */
1608 if ( ERR_OK( rs->sr_err ) ) {
1611 LDAPMessage *res = NULL;
1612 time_t stoptime = (time_t)(-1);
1613 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
1614 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
1615 const char *timeout_text = "Operation timed out";
1617 /* if timeout is not specified, compute and use
1618 * the one specific to the ongoing operation */
1619 if ( timeout == (time_t)(-1) ) {
1620 slap_op_t opidx = slap_req2op( op->o_tag );
1622 if ( opidx == SLAP_OP_SEARCH ) {
1623 if ( op->ors_tlimit <= 0 ) {
1627 timeout = op->ors_tlimit;
1628 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
1629 timeout_text = NULL;
1633 timeout = li->li_timeout[ opidx ];
1637 /* better than nothing :) */
1638 if ( timeout == 0 ) {
1639 if ( li->li_idle_timeout ) {
1640 timeout = li->li_idle_timeout;
1642 } else if ( li->li_conn_ttl ) {
1643 timeout = li->li_conn_ttl;
1648 stoptime = op->o_time + timeout;
1651 LDAP_BACK_TV_SET( &tv );
1654 /* if result parsing fails, note the failure reason */
1655 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1658 if ( timeout && slap_get_time() > stoptime ) {
1659 if ( sendok & LDAP_BACK_BINDING ) {
1660 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1663 /* let it be used, but taint/delete it so that
1664 * no-one else can look it up any further */
1665 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1667 #if LDAP_BACK_PRINT_CONNTREE > 0
1668 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1669 #endif /* LDAP_BACK_PRINT_CONNTREE */
1671 (void)ldap_back_conn_delete( li, lc );
1672 LDAP_BACK_CONN_TAINTED_SET( lc );
1674 #if LDAP_BACK_PRINT_CONNTREE > 0
1675 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1676 #endif /* LDAP_BACK_PRINT_CONNTREE */
1677 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1680 (void)ldap_back_cancel( lc, op, rs, msgid, sendok );
1682 rs->sr_err = timeout_err;
1683 rs->sr_text = timeout_text;
1688 LDAP_BACK_TV_SET( &tv );
1689 ldap_pvt_thread_yield();
1693 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1698 /* otherwise get the result; if it is not
1699 * LDAP_SUCCESS, record it in the reply
1700 * structure (this includes
1701 * LDAP_COMPARE_{TRUE|FALSE}) */
1703 /* only touch when activity actually took place... */
1704 if ( li->li_idle_timeout && lc ) {
1705 lc->lc_time = op->o_time;
1708 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1709 &match, &text, &refs, &ctrls, 1 );
1711 if ( rc != LDAP_SUCCESS ) {
1715 /* RFC 4511: referrals can only appear
1716 * if result code is LDAP_REFERRAL */
1718 && refs[ 0 ] != NULL
1719 && refs[ 0 ][ 0 ] != '\0' )
1721 if ( rs->sr_err != LDAP_REFERRAL ) {
1722 Debug( LDAP_DEBUG_ANY,
1723 "%s ldap_back_op_result: "
1724 "got referrals with err=%d\n",
1731 for ( i = 0; refs[ i ] != NULL; i++ )
1733 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1735 for ( i = 0; refs[ i ] != NULL; i++ ) {
1736 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1738 BER_BVZERO( &rs->sr_ref[ i ] );
1741 } else if ( rs->sr_err == LDAP_REFERRAL ) {
1742 Debug( LDAP_DEBUG_ANY,
1743 "%s ldap_back_op_result: "
1744 "got err=%d with null "
1745 "or empty referrals\n",
1749 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1752 if ( ctrls != NULL ) {
1753 rs->sr_ctrls = ctrls;
1758 /* if the error in the reply structure is not
1759 * LDAP_SUCCESS, try to map it from client
1760 * to server error */
1761 if ( !ERR_OK( rs->sr_err ) ) {
1762 rs->sr_err = slap_map_api2result( rs );
1764 /* internal ops ( op->o_conn == NULL )
1765 * must not reply to client */
1766 if ( op->o_conn && !op->o_do_not_cache && match ) {
1768 /* record the (massaged) matched
1769 * DN into the reply structure */
1770 rs->sr_matched = match;
1774 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1775 if ( !( sendok & LDAP_BACK_RETRYING ) ) {
1776 if ( LDAP_BACK_QUARANTINE( li ) ) {
1777 ldap_back_quarantine( op, rs );
1779 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1780 if ( rs->sr_text == NULL ) rs->sr_text = "Proxy operation retry failed";
1781 send_ldap_result( op, rs );
1785 } else if ( op->o_conn &&
1786 ( ( ( sendok & LDAP_BACK_SENDOK ) && ERR_OK( rs->sr_err ) )
1787 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1789 send_ldap_result( op, rs );
1793 if ( rs->sr_matched != match ) {
1794 free( (char *)rs->sr_matched );
1796 rs->sr_matched = NULL;
1797 ldap_memfree( match );
1801 ldap_memfree( text );
1806 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1811 ber_memvfree( (void **)refs );
1815 assert( rs->sr_ctrls != NULL );
1816 ldap_controls_free( ctrls );
1817 rs->sr_ctrls = NULL;
1820 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1823 /* return true if bound, false if failed */
1825 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1827 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1830 assert( lcp != NULL );
1831 assert( *lcp != NULL );
1833 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1835 if ( (*lcp)->lc_refcnt == 1 ) {
1836 int binding = LDAP_BACK_CONN_BINDING( *lcp );
1838 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1839 Debug( LDAP_DEBUG_ANY,
1840 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1841 op->o_log_prefix, li->li_uri,
1842 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1843 "" : (*lcp)->lc_bound_ndn.bv_val );
1844 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1846 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1847 (*lcp)->lc_ld = NULL;
1848 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1850 /* lc here must be the regular lc, reset and ready for init */
1851 rc = ldap_back_prepare_conn( *lcp, op, rs, sendok );
1852 if ( rc != LDAP_SUCCESS ) {
1853 /* freeit, because lc_refcnt == 1 */
1854 (*lcp)->lc_refcnt = 0;
1855 (void)ldap_back_freeconn( li, *lcp, 0 );
1859 } else if ( ( sendok & LDAP_BACK_BINDING ) ) {
1861 LDAP_BACK_CONN_BINDING_SET( *lcp );
1866 rc = ldap_back_dobind_int( lcp, op, rs, sendok, 0, 0 );
1867 if ( rc == 0 && *lcp != NULL ) {
1868 /* freeit, because lc_refcnt == 1 */
1869 (*lcp)->lc_refcnt = 0;
1870 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1871 (void)ldap_back_freeconn( li, *lcp, 0 );
1877 Debug( LDAP_DEBUG_TRACE,
1878 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1879 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1881 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1882 ldap_back_release_conn_lock( li, lcp, 0 );
1883 assert( *lcp == NULL );
1885 if ( sendok & LDAP_BACK_SENDERR ) {
1886 rs->sr_err = LDAP_UNAVAILABLE;
1887 rs->sr_text = "Unable to retry";
1888 send_ldap_result( op, rs );
1892 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1898 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
1899 struct berval *binddn, struct berval *bindcred )
1901 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1905 if ( op->o_conn == NULL || op->o_do_not_cache ) {
1909 /* don't proxyAuthz if protocol is not LDAPv3 */
1910 switch ( li->li_version ) {
1915 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1921 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1922 if ( sendok & LDAP_BACK_SENDERR ) {
1923 send_ldap_result( op, rs );
1930 *binddn = slap_empty_bv;
1931 *bindcred = slap_empty_bv;
1933 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1934 ndn = op->o_conn->c_ndn;
1940 switch ( li->li_idassert_mode ) {
1941 case LDAP_BACK_IDASSERT_LEGACY:
1942 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1943 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1945 *binddn = li->li_idassert_authcDN;
1946 *bindcred = li->li_idassert_passwd;
1953 /* NOTE: rootdn can always idassert */
1954 if ( BER_BVISNULL( &ndn )
1955 && li->li_idassert_authz == NULL
1956 && !( li->li_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL ) )
1958 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1959 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1960 if ( sendok & LDAP_BACK_SENDERR ) {
1961 send_ldap_result( op, rs );
1966 rs->sr_err = LDAP_SUCCESS;
1967 *binddn = slap_empty_bv;
1968 *bindcred = slap_empty_bv;
1974 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1975 struct berval authcDN;
1977 if ( BER_BVISNULL( &ndn ) ) {
1978 authcDN = slap_empty_bv;
1983 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1984 &authcDN, &authcDN );
1985 if ( rs->sr_err != LDAP_SUCCESS ) {
1986 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1987 if ( sendok & LDAP_BACK_SENDERR ) {
1988 send_ldap_result( op, rs );
1993 rs->sr_err = LDAP_SUCCESS;
1994 *binddn = slap_empty_bv;
1995 *bindcred = slap_empty_bv;
2003 *binddn = li->li_idassert_authcDN;
2004 *bindcred = li->li_idassert_passwd;
2014 ldap_back_proxy_authz_bind(
2018 ldap_back_send_t sendok,
2019 struct berval *binddn,
2020 struct berval *bindcred )
2022 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2027 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2028 ndn = op->o_conn->c_ndn;
2034 if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
2035 #ifdef HAVE_CYRUS_SASL
2036 void *defaults = NULL;
2037 struct berval authzID = BER_BVNULL;
2040 /* if SASL supports native authz, prepare for it */
2041 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
2042 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2044 switch ( li->li_idassert_mode ) {
2045 case LDAP_BACK_IDASSERT_OTHERID:
2046 case LDAP_BACK_IDASSERT_OTHERDN:
2047 authzID = li->li_idassert_authzID;
2050 case LDAP_BACK_IDASSERT_ANONYMOUS:
2051 BER_BVSTR( &authzID, "dn:" );
2054 case LDAP_BACK_IDASSERT_SELF:
2055 if ( BER_BVISNULL( &ndn ) ) {
2056 /* connection is not authc'd, so don't idassert */
2057 BER_BVSTR( &authzID, "dn:" );
2060 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
2061 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
2062 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
2063 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
2064 ndn.bv_val, ndn.bv_len + 1 );
2073 if ( li->li_idassert_secprops != NULL ) {
2074 rs->sr_err = ldap_set_option( lc->lc_ld,
2075 LDAP_OPT_X_SASL_SECPROPS,
2076 (void *)li->li_idassert_secprops );
2078 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
2079 rs->sr_err = LDAP_OTHER;
2080 if ( sendok & LDAP_BACK_SENDERR ) {
2081 send_ldap_result( op, rs );
2083 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2088 defaults = lutil_sasl_defaults( lc->lc_ld,
2089 li->li_idassert_sasl_mech.bv_val,
2090 li->li_idassert_sasl_realm.bv_val,
2091 li->li_idassert_authcID.bv_val,
2092 li->li_idassert_passwd.bv_val,
2095 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn->bv_val,
2096 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
2097 LDAP_SASL_QUIET, lutil_sasl_interact,
2100 rs->sr_err = slap_map_api2result( rs );
2101 if ( rs->sr_err != LDAP_SUCCESS ) {
2102 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2103 if ( sendok & LDAP_BACK_SENDERR ) {
2104 send_ldap_result( op, rs );
2108 LDAP_BACK_CONN_ISBOUND_SET( lc );
2111 lutil_sasl_freedefs( defaults );
2113 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
2117 #endif /* HAVE_CYRUS_SASL */
2120 switch ( li->li_idassert_authmethod ) {
2121 case LDAP_AUTH_NONE:
2122 /* FIXME: do we really need this? */
2123 BER_BVSTR( binddn, "" );
2124 BER_BVSTR( bindcred, "" );
2127 case LDAP_AUTH_SIMPLE:
2128 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
2129 binddn->bv_val, LDAP_SASL_SIMPLE,
2130 bindcred, NULL, NULL, &msgid );
2131 rc = ldap_back_op_result( lc, op, rs, msgid,
2132 -1, ( sendok | LDAP_BACK_BINDING ) );
2137 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2138 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
2139 if ( sendok & LDAP_BACK_SENDERR ) {
2140 send_ldap_result( op, rs );
2145 if ( rc == LDAP_SUCCESS ) {
2146 /* set rebind stuff in case of successful proxyAuthz bind,
2147 * so that referral chasing is attempted using the right
2149 LDAP_BACK_CONN_ISBOUND_SET( lc );
2150 ber_bvreplace( &lc->lc_bound_ndn, binddn );
2152 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
2153 memset( lc->lc_cred.bv_val, 0,
2154 lc->lc_cred.bv_len );
2157 if ( LDAP_BACK_SAVECRED( li ) ) {
2158 ber_bvreplace( &lc->lc_cred, bindcred );
2159 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
2162 lc->lc_cred.bv_len = 0;
2166 return LDAP_BACK_CONN_ISBOUND( lc );
2170 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
2171 * to existing server-side controls if required; if not,
2172 * the existing server-side controls are placed in *pctrls.
2173 * The caller, after using the controls in client API
2174 * operations, if ( *pctrls != op->o_ctrls ), should
2175 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
2176 * The function returns success if the control could
2177 * be added if required, or if it did nothing; in the future,
2178 * it might return some error if it failed.
2180 * if no bind took place yet, but the connection is bound
2181 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
2182 * and explicitly add proxyAuthz the control to every operation
2183 * with the dn bound to the connection as control value.
2185 * If no server-side controls are defined for the operation,
2186 * simply add the proxyAuthz control; otherwise, if the
2187 * proxyAuthz control is not already set, add it as
2190 * FIXME: is controls order significant for security?
2191 * ANSWER: controls ordering and interoperability
2192 * must be indicated by the specs of each control; if none
2193 * is specified, the order is irrelevant.
2196 ldap_back_proxy_authz_ctrl(
2197 struct berval *bound_ndn,
2199 slap_idassert_t *si,
2202 LDAPControl ***pctrls )
2204 LDAPControl **ctrls = NULL;
2206 slap_idassert_mode_t mode;
2207 struct berval assertedID,
2213 rs->sr_err = LDAP_SUCCESS;
2215 /* don't proxyAuthz if protocol is not LDAPv3 */
2216 switch ( version ) {
2221 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2230 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
2231 * but if it is not set this test fails. We need a different
2232 * means to detect if idassert is enabled */
2233 if ( ( BER_BVISNULL( &si->si_bc.sb_authcId ) || BER_BVISEMPTY( &si->si_bc.sb_authcId ) )
2234 && ( BER_BVISNULL( &si->si_bc.sb_binddn ) || BER_BVISEMPTY( &si->si_bc.sb_binddn ) ) )
2239 if ( !op->o_conn || op->o_do_not_cache || ( isroot = be_isroot( op ) ) ) {
2243 if ( op->o_tag == LDAP_REQ_BIND ) {
2244 ndn = op->o_req_ndn;
2246 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2247 ndn = op->o_conn->c_ndn;
2253 if ( si->si_mode == LDAP_BACK_IDASSERT_LEGACY ) {
2254 if ( op->o_proxy_authz ) {
2256 * FIXME: we do not want to perform proxyAuthz
2257 * on behalf of the client, because this would
2258 * be performed with "proxyauthzdn" privileges.
2260 * This might actually be too strict, since
2261 * the "proxyauthzdn" authzTo, and each entry's
2262 * authzFrom attributes may be crafted
2263 * to avoid unwanted proxyAuthz to take place.
2266 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2267 rs->sr_text = "proxyAuthz not allowed within namingContext";
2272 if ( !BER_BVISNULL( bound_ndn ) ) {
2276 if ( BER_BVISNULL( &ndn ) ) {
2280 if ( BER_BVISNULL( &si->si_bc.sb_binddn ) ) {
2284 } else if ( si->si_bc.sb_method == LDAP_AUTH_SASL ) {
2285 if ( ( si->si_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2287 /* already asserted in SASL via native authz */
2291 } else if ( si->si_authz && !isroot ) {
2293 struct berval authcDN;
2295 if ( BER_BVISNULL( &ndn ) ) {
2296 authcDN = slap_empty_bv;
2300 rc = slap_sasl_matches( op, si->si_authz,
2301 &authcDN, & authcDN );
2302 if ( rc != LDAP_SUCCESS ) {
2303 if ( si->si_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2304 /* ndn is not authorized
2305 * to use idassert */
2312 if ( op->o_proxy_authz ) {
2315 * 1) ignore the already set proxyAuthz control
2316 * 2) leave it in place, and don't set ours
2318 * 4) reject the operation
2320 * option (4) is very drastic
2321 * option (3) will make the remote server reject
2322 * the operation, thus being equivalent to (4)
2323 * option (2) will likely break the idassert
2324 * assumptions, so we cannot accept it;
2325 * option (1) means that we are contradicting
2326 * the client's reques.
2328 * I think (4) is the only correct choice.
2330 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2331 rs->sr_text = "proxyAuthz not allowed within namingContext";
2334 if ( op->o_is_auth_check ) {
2335 mode = LDAP_BACK_IDASSERT_NOASSERT;
2342 case LDAP_BACK_IDASSERT_LEGACY:
2343 /* original behavior:
2344 * assert the client's identity */
2345 case LDAP_BACK_IDASSERT_SELF:
2349 case LDAP_BACK_IDASSERT_ANONYMOUS:
2350 /* assert "anonymous" */
2351 assertedID = slap_empty_bv;
2354 case LDAP_BACK_IDASSERT_NOASSERT:
2355 /* don't assert; bind as proxyauthzdn */
2358 case LDAP_BACK_IDASSERT_OTHERID:
2359 case LDAP_BACK_IDASSERT_OTHERDN:
2360 /* assert idassert DN */
2361 assertedID = si->si_bc.sb_authzId;
2368 /* if we got here, "" is allowed to proxyAuthz */
2369 if ( BER_BVISNULL( &assertedID ) ) {
2370 assertedID = slap_empty_bv;
2373 /* don't idassert the bound DN (ITS#4497) */
2374 if ( dn_match( &assertedID, bound_ndn ) ) {
2378 if ( op->o_ctrls ) {
2379 for ( i = 0; op->o_ctrls[ i ]; i++ )
2380 /* just count ctrls */ ;
2383 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
2385 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
2387 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
2388 ctrls[ 0 ]->ldctl_iscritical = 1;
2390 switch ( si->si_mode ) {
2391 /* already in u:ID or dn:DN form */
2392 case LDAP_BACK_IDASSERT_OTHERID:
2393 case LDAP_BACK_IDASSERT_OTHERDN:
2394 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
2397 /* needs the dn: prefix */
2399 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
2400 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
2402 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
2403 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
2404 assertedID.bv_val, assertedID.bv_len + 1 );
2408 /* Older versions of <draft-weltman-ldapv3-proxy> required
2409 * to encode the value of the authzID (and called it proxyDN);
2410 * this hack provides compatibility with those DSAs that
2411 * implement it this way */
2412 if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
2413 struct berval authzID = ctrls[ 0 ]->ldctl_value;
2414 BerElementBuffer berbuf;
2415 BerElement *ber = (BerElement *)&berbuf;
2418 ber_init2( ber, 0, LBER_USE_DER );
2419 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2421 tag = ber_printf( ber, "O", &authzID );
2422 if ( tag == LBER_ERROR ) {
2423 rs->sr_err = LDAP_OTHER;
2427 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
2428 rs->sr_err = LDAP_OTHER;
2433 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2434 ber_free_buf( ber );
2436 if ( rs->sr_err != LDAP_SUCCESS ) {
2437 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2442 } else if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
2443 struct berval authzID = ctrls[ 0 ]->ldctl_value,
2445 BerElementBuffer berbuf;
2446 BerElement *ber = (BerElement *)&berbuf;
2449 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
2450 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
2451 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2453 rs->sr_err = LDAP_PROTOCOL_ERROR;
2458 tmp.bv_val += STRLENOF( "dn:" );
2459 tmp.bv_len -= STRLENOF( "dn:" );
2461 ber_init2( ber, 0, LBER_USE_DER );
2462 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2464 /* apparently, Mozilla API encodes this
2465 * as "SEQUENCE { LDAPDN }" */
2466 tag = ber_printf( ber, "{O}", &tmp );
2467 if ( tag == LBER_ERROR ) {
2468 rs->sr_err = LDAP_OTHER;
2472 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
2473 rs->sr_err = LDAP_OTHER;
2478 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2479 ber_free_buf( ber );
2481 if ( rs->sr_err != LDAP_SUCCESS ) {
2482 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2487 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
2490 if ( op->o_ctrls ) {
2491 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
2492 ctrls[ i + 1 ] = op->o_ctrls[ i ];
2495 ctrls[ i + 1 ] = NULL;
2498 if ( ctrls == NULL ) {
2499 ctrls = op->o_ctrls;
2508 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
2510 LDAPControl **ctrls = *pctrls;
2512 /* we assume that the first control is the proxyAuthz
2513 * added by back-ldap, so it's the only one we explicitly
2515 if ( ctrls && ctrls != op->o_ctrls ) {
2516 assert( ctrls[ 0 ] != NULL );
2518 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
2519 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
2522 op->o_tmpfree( ctrls, op->o_tmpmemctx );