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 lc = ldap_back_getconn( op, rs, LDAP_BACK_BIND_SERR, NULL, NULL );
185 /* we can do (almost) whatever we want with this conn,
186 * because either it's temporary, or it's marked as binding */
187 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
188 ch_free( lc->lc_bound_ndn.bv_val );
189 BER_BVZERO( &lc->lc_bound_ndn );
191 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
192 memset( lc->lc_cred.bv_val, 0, lc->lc_cred.bv_len );
193 ch_free( lc->lc_cred.bv_val );
194 BER_BVZERO( &lc->lc_cred );
196 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
199 /* method is always LDAP_AUTH_SIMPLE if we got here */
200 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
202 &op->orb_cred, op->o_ctrls, NULL, &msgid );
203 /* FIXME: should we always retry, or only when piping the bind
204 * in the "override" connection pool? */
205 rc = ldap_back_op_result( lc, op, rs, msgid,
206 li->li_timeout[ SLAP_OP_BIND ],
207 LDAP_BACK_BIND_SERR | retrying );
208 if ( rc == LDAP_UNAVAILABLE && retrying ) {
209 retrying &= ~LDAP_BACK_RETRYING;
210 if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_BIND_SERR ) ) {
215 if ( rc == LDAP_SUCCESS ) {
216 /* If defined, proxyAuthz will be used also when
217 * back-ldap is the authorizing backend; for this
218 * purpose, after a successful bind the connection
219 * is left for further binds, and further operations
220 * on this client connection will use a default
221 * connection with identity assertion */
222 /* NOTE: use with care */
223 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
224 ldap_back_release_conn( li, lc );
228 /* rebind is now done inside ldap_back_proxy_authz_bind()
229 * in case of success */
230 LDAP_BACK_CONN_ISBOUND_SET( lc );
231 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
233 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
234 memset( lc->lc_cred.bv_val, 0,
235 lc->lc_cred.bv_len );
238 if ( LDAP_BACK_SAVECRED( li ) ) {
239 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
240 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
243 lc->lc_cred.bv_len = 0;
247 /* must re-insert if local DN changed as result of bind */
248 if ( !LDAP_BACK_CONN_ISBOUND( lc )
249 || ( !dn_match( &op->o_req_ndn, &lc->lc_local_ndn )
250 && !LDAP_BACK_PCONN_ISPRIV( lc ) ) )
255 /* wait for all other ops to release the connection */
257 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
258 if ( lc->lc_refcnt > 1 ) {
259 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
260 ldap_pvt_thread_yield();
264 #if LDAP_BACK_PRINT_CONNTREE > 0
265 ldap_back_print_conntree( li, ">>> ldap_back_bind" );
266 #endif /* LDAP_BACK_PRINT_CONNTREE */
268 assert( lc->lc_refcnt == 1 );
269 ldap_back_conn_delete( li, lc );
271 /* delete all cached connections with the current connection */
272 if ( LDAP_BACK_SINGLECONN( li ) ) {
273 while ( ( tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc, ldap_back_conn_cmp ) ) != NULL )
275 Debug( LDAP_DEBUG_TRACE,
276 "=>ldap_back_bind: destroying conn %ld (refcnt=%u)\n",
277 LDAP_BACK_PCONN_ID( lc ), lc->lc_refcnt, 0 );
279 if ( tmplc->lc_refcnt != 0 ) {
281 LDAP_BACK_CONN_TAINTED_SET( tmplc );
282 LDAP_BACK_CONN_CACHED_CLEAR( tmplc );
286 * Needs a test because the handler may be corrupted,
287 * and calling ldap_unbind on a corrupted header results
288 * in a segmentation fault
290 ldap_back_conn_free( tmplc );
295 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
296 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
297 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
298 LDAP_BACK_PCONN_ROOTDN_SET( lc, op );
300 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
301 ldap_back_conndn_cmp, ldap_back_conndn_dup );
304 #if LDAP_BACK_PRINT_CONNTREE > 0
305 ldap_back_print_conntree( li, "<<< ldap_back_bind" );
306 #endif /* LDAP_BACK_PRINT_CONNTREE */
308 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
311 LDAP_BACK_CONN_CACHED_SET( lc );
315 /* duplicate; someone else successfully bound
316 * on the same connection with the same identity;
317 * we can do this because lc_refcnt == 1 */
318 ldap_back_conn_free( lc );
324 ldap_back_release_conn( li, lc );
331 * ldap_back_conndn_cmp
333 * compares two ldapconn_t based on the value of the conn pointer
334 * and of the local DN; used by avl stuff for insert, lookup
338 ldap_back_conndn_cmp( const void *c1, const void *c2 )
340 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
341 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
344 /* If local DNs don't match, it is definitely not a match */
345 /* For shared sessions, conn is NULL. Only explicitly
346 * bound sessions will have non-NULL conn.
348 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
350 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
357 * ldap_back_conndnlc_cmp
359 * compares two ldapconn_t based on the value of the conn pointer,
360 * the local DN and the lc pointer; used by avl stuff for insert, lookup
364 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
366 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
367 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
370 /* If local DNs don't match, it is definitely not a match */
371 /* For shared sessions, conn is NULL. Only explicitly
372 * bound sessions will have non-NULL conn.
374 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
376 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
378 rc = SLAP_PTRCMP( lc1, lc2 );
388 * compares two ldapconn_t based on the value of the conn pointer;
389 * used by avl stuff for delete of all conns with the same connid
392 ldap_back_conn_cmp( const void *c1, const void *c2 )
394 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
395 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
397 /* For shared sessions, conn is NULL. Only explicitly
398 * bound sessions will have non-NULL conn.
400 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
404 * ldap_back_conndn_dup
406 * returns -1 in case a duplicate ldapconn_t has been inserted;
410 ldap_back_conndn_dup( void *c1, void *c2 )
412 ldapconn_t *lc1 = (ldapconn_t *)c1;
413 ldapconn_t *lc2 = (ldapconn_t *)c2;
415 /* Cannot have more than one shared session with same DN */
416 if ( lc1->lc_conn == lc2->lc_conn &&
417 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
426 ldap_back_freeconn( ldapinfo_t *li, ldapconn_t *lc, int dolock )
429 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
432 #if LDAP_BACK_PRINT_CONNTREE > 0
433 ldap_back_print_conntree( li, ">>> ldap_back_freeconn" );
434 #endif /* LDAP_BACK_PRINT_CONNTREE */
436 (void)ldap_back_conn_delete( li, lc );
438 if ( lc->lc_refcnt == 0 ) {
439 ldap_back_conn_free( (void *)lc );
442 #if LDAP_BACK_PRINT_CONNTREE > 0
443 ldap_back_print_conntree( li, "<<< ldap_back_freeconn" );
444 #endif /* LDAP_BACK_PRINT_CONNTREE */
447 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
464 int rc = LDAP_SUCCESS;
466 /* start TLS ("tls-[try-]{start,propagate}" statements) */
467 if ( ( LDAP_BACK_USE_TLS_F( flags ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS_F( flags ) ) )
468 && !ldap_is_ldaps_url( url ) )
470 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
472 * use asynchronous StartTLS
473 * in case, chase referral (not implemented yet)
477 if ( protocol == 0 ) {
478 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
482 if ( protocol < LDAP_VERSION3 ) {
483 /* we should rather bail out... */
484 rc = LDAP_UNWILLING_TO_PERFORM;
485 *text = "invalid protocol version";
488 if ( rc == LDAP_SUCCESS ) {
489 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
492 if ( rc == LDAP_SUCCESS ) {
493 LDAPMessage *res = NULL;
496 LDAP_BACK_TV_SET( &tv );
499 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
501 rc = LDAP_UNAVAILABLE;
503 } else if ( rc == 0 ) {
504 if ( retries != LDAP_BACK_RETRY_NEVER ) {
505 ldap_pvt_thread_yield();
509 LDAP_BACK_TV_SET( &tv );
512 rc = LDAP_UNAVAILABLE;
514 } else if ( rc == LDAP_RES_EXTENDED ) {
515 struct berval *data = NULL;
517 rc = ldap_parse_extended_result( ld, res,
519 if ( rc == LDAP_SUCCESS ) {
521 rc = ldap_parse_result( ld, res, &err,
522 NULL, NULL, NULL, NULL, 1 );
523 if ( rc == LDAP_SUCCESS ) {
528 /* FIXME: in case a referral
529 * is returned, should we try
530 * using it instead of the
532 if ( rc == LDAP_SUCCESS ) {
533 rc = ldap_install_tls( ld );
535 } else if ( rc == LDAP_REFERRAL ) {
536 rc = LDAP_UNWILLING_TO_PERFORM;
537 *text = "unwilling to chase referral returned by Start TLS exop";
541 if ( data->bv_val ) {
542 ber_memfree( data->bv_val );
556 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
558 * use synchronous StartTLS
560 rc = ldap_start_tls_s( ld, NULL, NULL );
561 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
563 /* if StartTLS is requested, only attempt it if the URL
564 * is not "ldaps://"; this may occur not only in case
565 * of misconfiguration, but also when used in the chain
566 * overlay, where the "uri" can be parsed out of a referral */
572 case LDAP_SERVER_DOWN:
576 if ( LDAP_BACK_TLS_CRITICAL_F( flags ) ) {
577 *text = "could not start TLS";
581 /* in case Start TLS is not critical */
593 #endif /* HAVE_TLS */
596 ldap_back_prepare_conn( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
598 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
602 int is_tls = op->o_conn->c_is_tls;
603 time_t lc_time = (time_t)(-1);
605 #endif /* HAVE_TLS */
607 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
608 rs->sr_err = ldap_initialize( &ld, li->li_uri );
609 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
610 if ( rs->sr_err != LDAP_SUCCESS ) {
614 if ( li->li_urllist_f ) {
615 ldap_set_urllist_proc( ld, li->li_urllist_f, li->li_urllist_p );
618 /* Set LDAP version. This will always succeed: If the client
619 * bound with a particular version, then so can we.
621 if ( li->li_version != 0 ) {
622 version = li->li_version;
624 } else if ( op->o_protocol != 0 ) {
625 version = op->o_protocol;
628 /* assume it's an internal op; set to LDAPv3 */
629 version = LDAP_VERSION3;
631 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
633 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
634 ldap_set_option( ld, LDAP_OPT_REFERRALS,
635 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
637 if ( li->li_network_timeout > 0 ) {
640 tv.tv_sec = li->li_network_timeout;
642 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
646 if ( LDAP_BACK_CONN_ISPRIV( lc ) ) {
649 } else if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
650 sb = &li->li_idassert.si_bc;
656 if ( sb->sb_tls_do_init ) {
657 bindconf_tls_set( sb, ld );
658 } else if ( sb->sb_tls_ctx ) {
659 ldap_set_option( ld, LDAP_OPT_X_TLS_CTX, sb->sb_tls_ctx );
662 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
663 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
664 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
665 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
666 if ( rs->sr_err != LDAP_SUCCESS ) {
667 ldap_unbind_ext( ld, NULL, NULL );
668 rs->sr_text = "Start TLS failed";
671 } else if ( li->li_idle_timeout ) {
672 /* only touch when activity actually took place... */
673 lc_time = op->o_time;
675 #endif /* HAVE_TLS */
681 LDAP_BACK_CONN_ISTLS_SET( lc );
683 LDAP_BACK_CONN_ISTLS_CLEAR( lc );
685 if ( lc_time != (time_t)(-1) ) {
686 lc->lc_time = lc_time;
688 #endif /* HAVE_TLS */
691 if ( rs->sr_err != LDAP_SUCCESS ) {
692 rs->sr_err = slap_map_api2result( rs );
693 if ( sendok & LDAP_BACK_SENDERR ) {
694 if ( rs->sr_text == NULL ) {
695 rs->sr_text = "Proxy connection initialization failed";
697 send_ldap_result( op, rs );
701 if ( li->li_conn_ttl > 0 ) {
702 lc->lc_create_time = op->o_time;
713 ldap_back_send_t sendok,
714 struct berval *binddn,
715 struct berval *bindcred )
717 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
718 ldapconn_t *lc = NULL,
721 lookupconn = !( sendok & LDAP_BACK_BINDING );
723 /* if the server is quarantined, and
724 * - the current interval did not expire yet, or
725 * - no more retries should occur,
726 * don't return the connection */
727 if ( li->li_isquarantined ) {
728 slap_retry_info_t *ri = &li->li_quarantine;
731 if ( li->li_quarantine.ri_interval ) {
732 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
733 if ( li->li_isquarantined == LDAP_BACK_FQ_YES ) {
734 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
735 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
737 Debug( LDAP_DEBUG_ANY,
738 "%s: ldap_back_getconn quarantine "
739 "retry block #%d try #%d.\n",
740 op->o_log_prefix, ri->ri_idx, ri->ri_count );
741 li->li_isquarantined = LDAP_BACK_FQ_RETRYING;
744 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
748 rs->sr_err = LDAP_UNAVAILABLE;
749 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
750 rs->sr_text = "Target is quarantined";
751 send_ldap_result( op, rs );
757 /* Internal searches are privileged and shared. So is root. */
758 if ( op->o_do_not_cache || be_isroot( op ) ) {
759 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
760 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
761 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
764 struct berval tmpbinddn,
771 if ( binddn == NULL ) {
774 if ( bindcred == NULL ) {
775 bindcred = &tmpbindcred;
777 if ( op->o_tag == LDAP_REQ_BIND ) {
778 save_o_dn = op->o_dn;
779 save_o_ndn = op->o_ndn;
780 op->o_dn = op->o_req_dn;
781 op->o_ndn = op->o_req_ndn;
783 isproxyauthz = ldap_back_is_proxy_authz( op, rs, sendok, binddn, bindcred );
784 if ( op->o_tag == LDAP_REQ_BIND ) {
785 op->o_dn = save_o_dn;
786 op->o_ndn = save_o_ndn;
788 if ( isproxyauthz == -1 ) {
792 lc_curr.lc_local_ndn = op->o_ndn;
793 /* Explicit binds must not be shared;
794 * however, explicit binds are piped in a special connection
795 * when idassert is to occur with "override" set */
796 if ( op->o_tag == LDAP_REQ_BIND && !isproxyauthz ) {
797 lc_curr.lc_conn = op->o_conn;
800 if ( isproxyauthz && !( sendok & LDAP_BACK_BINDING ) ) {
801 lc_curr.lc_local_ndn = *binddn;
802 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
803 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
805 } else if ( isproxyauthz && ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) {
806 lc_curr.lc_local_ndn = slap_empty_bv;
807 LDAP_BACK_PCONN_BIND_SET( &lc_curr, op );
808 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
811 } else if ( SLAP_IS_AUTHZ_BACKEND( op ) ) {
812 lc_curr.lc_conn = op->o_conn;
815 LDAP_BACK_PCONN_ANON_SET( &lc_curr, op );
820 /* Explicit Bind requests always get their own conn */
823 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
824 if ( LDAP_BACK_PCONN_ISPRIV( &lc_curr ) ) {
825 /* lookup a conn that's not binding */
826 LDAP_TAILQ_FOREACH( lc,
827 &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv,
830 if ( !LDAP_BACK_CONN_BINDING( lc ) && lc->lc_refcnt == 0 ) {
836 if ( lc != LDAP_TAILQ_LAST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
839 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
841 LDAP_TAILQ_ENTRY_INIT( lc, lc_q );
842 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
846 } else if ( !LDAP_BACK_USE_TEMPORARIES( li )
847 && li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_num == li->li_conn_priv_max )
849 lc = LDAP_TAILQ_FIRST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv );
854 /* Searches for a ldapconn in the avl tree */
855 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
856 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
860 /* Don't reuse connections while they're still binding */
861 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
862 if ( !LDAP_BACK_USE_TEMPORARIES( li ) ) {
863 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
865 ldap_pvt_thread_yield();
872 if ( op->o_tag == LDAP_REQ_BIND ) {
873 /* right now, this is the only possible case */
874 assert( ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) );
875 LDAP_BACK_CONN_BINDING_SET( lc );
878 refcnt = ++lc->lc_refcnt;
881 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
884 /* Looks like we didn't get a bind. Open a new session... */
886 lc = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
887 lc->lc_flags = li->li_flags;
888 lc->lc_lcflags = lc_curr.lc_lcflags;
889 if ( ldap_back_prepare_conn( lc, op, rs, sendok ) != LDAP_SUCCESS ) {
894 if ( sendok & LDAP_BACK_BINDING ) {
895 LDAP_BACK_CONN_BINDING_SET( lc );
898 lc->lc_conn = lc_curr.lc_conn;
899 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
902 * the rationale is: connections as the rootdn are privileged,
903 * so acl_authcDN is to be used; however, in some cases
904 * one already configured identity assertion with a highly
905 * privileged idassert_authcDN, so if acl_authcDN is NULL
906 * and idassert_authcDN is not, use the second instead.
908 * might change in the future, because it's preferable
909 * to make clear what identity is being used, since
910 * the only drawback is that one risks to configure
911 * the same identity twice...
913 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
914 if ( BER_BVISNULL( &li->li_acl_authcDN ) && !BER_BVISNULL( &li->li_idassert_authcDN ) ) {
915 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
916 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
919 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
920 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
922 LDAP_BACK_CONN_ISPRIV_SET( lc );
924 } else if ( LDAP_BACK_CONN_ISIDASSERT( &lc_curr ) ) {
925 if ( !LDAP_BACK_PCONN_ISBIND( &lc_curr ) ) {
926 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
927 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
929 LDAP_BACK_CONN_ISIDASSERT_SET( lc );
932 BER_BVZERO( &lc->lc_cred );
933 BER_BVZERO( &lc->lc_bound_ndn );
934 if ( !BER_BVISEMPTY( &op->o_ndn )
935 && SLAP_IS_AUTHZ_BACKEND( op ) )
937 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
942 /* if start TLS failed but it was not mandatory,
943 * check if the non-TLS connection was already
944 * in cache; in case, destroy the newly created
945 * connection and use the existing one */
946 if ( LDAP_BACK_PCONN_ISTLS( lc )
947 && !ldap_tls_inplace( lc->lc_ld ) )
949 ldapconn_t *tmplc = NULL;
950 int idx = LDAP_BACK_CONN2PRIV( &lc_curr ) - 1;
952 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
953 LDAP_TAILQ_FOREACH( tmplc,
954 &li->li_conn_priv[ idx ].lic_priv,
957 if ( !LDAP_BACK_CONN_BINDING( tmplc ) ) {
962 if ( tmplc != NULL ) {
963 refcnt = ++tmplc->lc_refcnt;
964 ldap_back_conn_free( lc );
967 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
969 if ( tmplc != NULL ) {
973 #endif /* HAVE_TLS */
975 /* Inserts the newly created ldapconn in the avl tree */
976 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
978 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
980 assert( lc->lc_refcnt == 1 );
982 #if LDAP_BACK_PRINT_CONNTREE > 0
983 ldap_back_print_conntree( li, ">>> ldap_back_getconn(insert)" );
984 #endif /* LDAP_BACK_PRINT_CONNTREE */
986 if ( LDAP_BACK_PCONN_ISPRIV( lc ) ) {
987 if ( li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num < li->li_conn_priv_max ) {
988 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv, lc, lc_q );
989 li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num++;
990 LDAP_BACK_CONN_CACHED_SET( lc );
993 LDAP_BACK_CONN_TAINTED_SET( lc );
998 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
999 ldap_back_conndn_cmp, ldap_back_conndn_dup );
1000 LDAP_BACK_CONN_CACHED_SET( lc );
1003 #if LDAP_BACK_PRINT_CONNTREE > 0
1004 ldap_back_print_conntree( li, "<<< ldap_back_getconn(insert)" );
1005 #endif /* LDAP_BACK_PRINT_CONNTREE */
1007 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1009 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1010 char buf[ SLAP_TEXT_BUFLEN ];
1012 snprintf( buf, sizeof( buf ),
1013 "lc=%p inserted refcnt=%u rc=%d",
1014 (void *)lc, refcnt, rs->sr_err );
1016 Debug( LDAP_DEBUG_TRACE,
1017 "=>ldap_back_getconn: %s: %s\n",
1018 op->o_log_prefix, buf, 0 );
1021 if ( !LDAP_BACK_PCONN_ISPRIV( lc ) ) {
1022 /* Err could be -1 in case a duplicate ldapconn is inserted */
1023 switch ( rs->sr_err ) {
1028 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1029 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( li ) ) {
1030 /* duplicate: free and try to get the newly created one */
1031 ldap_back_conn_free( lc );
1036 /* taint connection, so that it'll be freed when released */
1037 LDAP_BACK_CONN_TAINTED_SET( lc );
1041 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1042 ldap_back_conn_free( lc );
1043 rs->sr_err = LDAP_OTHER;
1044 rs->sr_text = "Proxy bind collision";
1045 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1046 send_ldap_result( op, rs );
1055 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
1056 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
1060 /* let it be used, but taint/delete it so that
1061 * no-one else can look it up any further */
1062 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1064 #if LDAP_BACK_PRINT_CONNTREE > 0
1065 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1066 #endif /* LDAP_BACK_PRINT_CONNTREE */
1068 (void)ldap_back_conn_delete( li, lc );
1069 LDAP_BACK_CONN_TAINTED_SET( lc );
1071 #if LDAP_BACK_PRINT_CONNTREE > 0
1072 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1073 #endif /* LDAP_BACK_PRINT_CONNTREE */
1075 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1078 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1079 char buf[ SLAP_TEXT_BUFLEN ];
1081 snprintf( buf, sizeof( buf ),
1082 "conn %p fetched refcnt=%u%s",
1084 expiring ? " expiring" : "" );
1085 Debug( LDAP_DEBUG_TRACE,
1086 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
1092 #endif /* HAVE_TLS */
1098 ldap_back_release_conn_lock(
1104 ldapconn_t *lc = *lcp;
1107 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1109 assert( lc->lc_refcnt > 0 );
1110 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1112 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
1113 ldap_back_freeconn( li, lc, 0 );
1117 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1122 ldap_back_quarantine(
1126 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1128 slap_retry_info_t *ri = &li->li_quarantine;
1130 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
1132 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1133 time_t new_last = slap_get_time();
1135 switch ( li->li_isquarantined ) {
1136 case LDAP_BACK_FQ_NO:
1137 if ( ri->ri_last == new_last ) {
1141 Debug( LDAP_DEBUG_ANY,
1142 "%s: ldap_back_quarantine enter.\n",
1143 op->o_log_prefix, 0, 0 );
1149 case LDAP_BACK_FQ_RETRYING:
1150 Debug( LDAP_DEBUG_ANY,
1151 "%s: ldap_back_quarantine block #%d try #%d failed.\n",
1152 op->o_log_prefix, ri->ri_idx, ri->ri_count );
1155 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1156 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1167 li->li_isquarantined = LDAP_BACK_FQ_YES;
1168 ri->ri_last = new_last;
1170 } else if ( li->li_isquarantined != LDAP_BACK_FQ_NO ) {
1171 if ( ri->ri_last == slap_get_time() ) {
1175 Debug( LDAP_DEBUG_ANY,
1176 "%s: ldap_back_quarantine exit (%d) err=%d.\n",
1177 op->o_log_prefix, li->li_isquarantined, rs->sr_err );
1179 if ( li->li_quarantine_f ) {
1180 (void)li->li_quarantine_f( li, li->li_quarantine_p );
1185 li->li_isquarantined = LDAP_BACK_FQ_NO;
1189 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
1193 ldap_back_dobind_cb(
1198 ber_tag_t *tptr = op->o_callback->sc_private;
1200 return SLAP_CB_CONTINUE;
1204 * ldap_back_dobind_int
1206 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1209 ldap_back_dobind_int(
1213 ldap_back_send_t sendok,
1217 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1220 struct berval binddn = slap_empty_bv,
1221 bindcred = slap_empty_bv;
1227 ber_tag_t o_tag = op->o_tag;
1228 slap_callback cb = {0};
1230 assert( lcp != NULL );
1231 assert( retries >= 0 );
1233 if ( sendok & LDAP_BACK_GETCONN ) {
1234 assert( *lcp == NULL );
1236 lc = ldap_back_getconn( op, rs, sendok, &binddn, &bindcred );
1246 assert( lc != NULL );
1250 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1253 if ( binding == 0 ) {
1254 /* check if already bound */
1255 rc = isbound = LDAP_BACK_CONN_ISBOUND( lc );
1258 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1263 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
1264 /* if someone else is about to bind it, give up and retry */
1266 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1268 ldap_pvt_thread_yield();
1272 /* otherwise this thread will bind it */
1273 LDAP_BACK_CONN_BINDING_SET( lc );
1279 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1283 * FIXME: we need to let clients use proxyAuthz
1284 * otherwise we cannot do symmetric pools of servers;
1285 * we have to live with the fact that a user can
1286 * authorize itself as any ID that is allowed
1287 * by the authzTo directive of the "proxyauthzdn".
1290 * NOTE: current Proxy Authorization specification
1291 * and implementation do not allow proxy authorization
1292 * control to be provided with Bind requests
1295 * if no bind took place yet, but the connection is bound
1296 * and the "idassert-authcDN" (or other ID) is set,
1297 * then bind as the asserting identity and explicitly
1298 * add the proxyAuthz control to every operation with the
1299 * dn bound to the connection as control value.
1300 * This is done also if this is the authrizing backend,
1301 * but the "override" flag is given to idassert.
1302 * It allows to use SASL bind and yet proxyAuthz users
1304 op->o_tag = LDAP_REQ_BIND;
1305 cb.sc_next = op->o_callback;
1306 cb.sc_private = &o_tag;
1307 cb.sc_response = ldap_back_dobind_cb;
1308 op->o_callback = &cb;
1310 if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
1311 if ( BER_BVISEMPTY( &binddn ) && BER_BVISEMPTY( &bindcred ) ) {
1312 /* if we got here, it shouldn't return result */
1313 rc = ldap_back_is_proxy_authz( op, rs,
1314 LDAP_BACK_DONTSEND, &binddn, &bindcred );
1317 rc = ldap_back_proxy_authz_bind( lc, op, rs, sendok, &binddn, &bindcred );
1321 #ifdef HAVE_CYRUS_SASL
1322 if ( LDAP_BACK_CONN_ISPRIV( lc )
1323 && li->li_acl_authmethod == LDAP_AUTH_SASL )
1325 void *defaults = NULL;
1327 if ( li->li_acl_secprops != NULL ) {
1328 rc = ldap_set_option( lc->lc_ld,
1329 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops );
1331 if ( rc != LDAP_OPT_SUCCESS ) {
1332 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
1333 "(SECPROPS,\"%s\") failed!\n",
1334 li->li_acl_secprops, 0, 0 );
1339 defaults = lutil_sasl_defaults( lc->lc_ld,
1340 li->li_acl_sasl_mech.bv_val,
1341 li->li_acl_sasl_realm.bv_val,
1342 li->li_acl_authcID.bv_val,
1343 li->li_acl_passwd.bv_val,
1346 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
1347 li->li_acl_authcDN.bv_val,
1348 li->li_acl_sasl_mech.bv_val, NULL, NULL,
1349 LDAP_SASL_QUIET, lutil_sasl_interact,
1352 lutil_sasl_freedefs( defaults );
1354 rs->sr_err = slap_map_api2result( rs );
1355 if ( rs->sr_err != LDAP_SUCCESS ) {
1356 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1357 if ( sendok & LDAP_BACK_SENDERR ) {
1358 send_ldap_result( op, rs );
1362 LDAP_BACK_CONN_ISBOUND_SET( lc );
1365 if ( LDAP_BACK_QUARANTINE( li ) ) {
1366 ldap_back_quarantine( op, rs );
1371 #endif /* HAVE_CYRUS_SASL */
1374 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1375 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1376 LDAP_SASL_SIMPLE, &lc->lc_cred,
1377 NULL, NULL, &msgid );
1379 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
1380 if ( retries != LDAP_BACK_RETRY_NEVER ) {
1382 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1385 assert( lc->lc_refcnt > 0 );
1386 if ( lc->lc_refcnt == 1 ) {
1387 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1390 /* lc here must be the regular lc, reset and ready for init */
1391 rs->sr_err = ldap_back_prepare_conn( lc, op, rs, sendok );
1392 if ( rs->sr_err != LDAP_SUCCESS ) {
1393 sendok &= ~LDAP_BACK_SENDERR;
1399 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1402 if ( rs->sr_err == LDAP_SUCCESS ) {
1403 if ( retries > 0 ) {
1410 assert( lc->lc_refcnt == 1 );
1412 ldap_back_freeconn( li, lc, dolock );
1414 rs->sr_err = slap_map_api2result( rs );
1416 if ( LDAP_BACK_QUARANTINE( li ) ) {
1417 ldap_back_quarantine( op, rs );
1420 if ( rs->sr_err != LDAP_SUCCESS &&
1421 ( sendok & LDAP_BACK_SENDERR ) )
1423 rs->sr_text = "Internal proxy bind failure";
1424 send_ldap_result( op, rs );
1431 rc = ldap_back_op_result( lc, op, rs, msgid,
1432 -1, ( sendok | LDAP_BACK_BINDING ) );
1433 if ( rc == LDAP_SUCCESS ) {
1434 LDAP_BACK_CONN_ISBOUND_SET( lc );
1438 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1439 rc = LDAP_BACK_CONN_ISBOUND( lc );
1441 ldap_back_release_conn_lock( li, lcp, dolock );
1443 } else if ( LDAP_BACK_SAVECRED( li ) ) {
1444 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1448 if ( op->o_callback == &cb )
1449 op->o_callback = cb.sc_next;
1458 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1461 ldap_back_dobind( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1463 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1465 return ldap_back_dobind_int( lcp, op, rs,
1466 ( sendok | LDAP_BACK_GETCONN ), li->li_nretries, 1 );
1470 * ldap_back_default_rebind
1472 * This is a callback used for chasing referrals using the same
1473 * credentials as the original user on this session.
1476 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1477 ber_int_t msgid, void *params )
1479 ldapconn_t *lc = (ldapconn_t *)params;
1482 /* ... otherwise we couldn't get here */
1483 assert( lc != NULL );
1485 if ( !ldap_tls_inplace( ld ) ) {
1486 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1488 const char *text = NULL;
1490 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1491 LDAP_BACK_RETRY_DEFAULT, &text );
1492 if ( rc != LDAP_SUCCESS ) {
1496 #endif /* HAVE_TLS */
1498 /* FIXME: add checks on the URL/identity? */
1500 return ldap_sasl_bind_s( ld,
1501 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1502 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1506 * ldap_back_default_urllist
1509 ldap_back_default_urllist(
1511 LDAPURLDesc **urllist,
1515 ldapinfo_t *li = (ldapinfo_t *)params;
1516 LDAPURLDesc **urltail;
1518 if ( urllist == url ) {
1519 return LDAP_SUCCESS;
1522 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1525 *urltail = *urllist;
1529 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1531 ch_free( li->li_uri );
1534 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1535 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1537 return LDAP_SUCCESS;
1546 ldap_back_send_t sendok )
1548 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1550 /* default behavior */
1551 if ( LDAP_BACK_ABANDON( li ) ) {
1552 return ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1555 if ( LDAP_BACK_IGNORE( li ) ) {
1556 return ldap_pvt_discard( lc->lc_ld, msgid );
1559 if ( LDAP_BACK_CANCEL( li ) ) {
1560 /* FIXME: asynchronous? */
1561 return ldap_cancel_s( lc->lc_ld, msgid, NULL, NULL );
1570 ldap_back_op_result(
1576 ldap_back_send_t sendok )
1578 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1583 LDAPControl **ctrls = NULL;
1585 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
1588 rs->sr_matched = NULL;
1590 rs->sr_ctrls = NULL;
1592 /* if the error recorded in the reply corresponds
1593 * to a successful state, get the error from the
1594 * remote server response */
1595 if ( ERR_OK( rs->sr_err ) ) {
1598 LDAPMessage *res = NULL;
1599 time_t stoptime = (time_t)(-1);
1600 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
1601 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
1602 const char *timeout_text = "Operation timed out";
1604 /* if timeout is not specified, compute and use
1605 * the one specific to the ongoing operation */
1606 if ( timeout == (time_t)(-1) ) {
1607 slap_op_t opidx = slap_req2op( op->o_tag );
1609 if ( opidx == SLAP_OP_SEARCH ) {
1610 if ( op->ors_tlimit <= 0 ) {
1614 timeout = op->ors_tlimit;
1615 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
1616 timeout_text = NULL;
1620 timeout = li->li_timeout[ opidx ];
1624 /* better than nothing :) */
1625 if ( timeout == 0 ) {
1626 if ( li->li_idle_timeout ) {
1627 timeout = li->li_idle_timeout;
1629 } else if ( li->li_conn_ttl ) {
1630 timeout = li->li_conn_ttl;
1635 stoptime = op->o_time + timeout;
1638 LDAP_BACK_TV_SET( &tv );
1641 /* if result parsing fails, note the failure reason */
1642 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1645 if ( timeout && slap_get_time() > stoptime ) {
1646 if ( sendok & LDAP_BACK_BINDING ) {
1647 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1650 /* let it be used, but taint/delete it so that
1651 * no-one else can look it up any further */
1652 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1654 #if LDAP_BACK_PRINT_CONNTREE > 0
1655 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1656 #endif /* LDAP_BACK_PRINT_CONNTREE */
1658 (void)ldap_back_conn_delete( li, lc );
1659 LDAP_BACK_CONN_TAINTED_SET( lc );
1661 #if LDAP_BACK_PRINT_CONNTREE > 0
1662 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1663 #endif /* LDAP_BACK_PRINT_CONNTREE */
1664 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1667 (void)ldap_back_cancel( lc, op, rs, msgid, sendok );
1669 rs->sr_err = timeout_err;
1670 rs->sr_text = timeout_text;
1675 LDAP_BACK_TV_SET( &tv );
1676 ldap_pvt_thread_yield();
1680 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1685 /* otherwise get the result; if it is not
1686 * LDAP_SUCCESS, record it in the reply
1687 * structure (this includes
1688 * LDAP_COMPARE_{TRUE|FALSE}) */
1690 /* only touch when activity actually took place... */
1691 if ( li->li_idle_timeout && lc ) {
1692 lc->lc_time = op->o_time;
1695 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1696 &match, &text, &refs, &ctrls, 1 );
1698 if ( rc != LDAP_SUCCESS ) {
1702 /* RFC 4511: referrals can only appear
1703 * if result code is LDAP_REFERRAL */
1705 && refs[ 0 ] != NULL
1706 && refs[ 0 ][ 0 ] != '\0' )
1708 if ( rs->sr_err != LDAP_REFERRAL ) {
1709 Debug( LDAP_DEBUG_ANY,
1710 "%s ldap_back_op_result: "
1711 "got referrals with err=%d\n",
1718 for ( i = 0; refs[ i ] != NULL; i++ )
1720 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1722 for ( i = 0; refs[ i ] != NULL; i++ ) {
1723 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1725 BER_BVZERO( &rs->sr_ref[ i ] );
1728 } else if ( rs->sr_err == LDAP_REFERRAL ) {
1729 Debug( LDAP_DEBUG_ANY,
1730 "%s ldap_back_op_result: "
1731 "got err=%d with null "
1732 "or empty referrals\n",
1736 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1739 if ( ctrls != NULL ) {
1740 rs->sr_ctrls = ctrls;
1745 /* if the error in the reply structure is not
1746 * LDAP_SUCCESS, try to map it from client
1747 * to server error */
1748 if ( !ERR_OK( rs->sr_err ) ) {
1749 rs->sr_err = slap_map_api2result( rs );
1751 /* internal ops ( op->o_conn == NULL )
1752 * must not reply to client */
1753 if ( op->o_conn && !op->o_do_not_cache && match ) {
1755 /* record the (massaged) matched
1756 * DN into the reply structure */
1757 rs->sr_matched = match;
1761 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1762 if ( !( sendok & LDAP_BACK_RETRYING ) ) {
1763 if ( LDAP_BACK_QUARANTINE( li ) ) {
1764 ldap_back_quarantine( op, rs );
1766 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1767 if ( rs->sr_text == NULL ) rs->sr_text = "Proxy operation retry failed";
1768 send_ldap_result( op, rs );
1772 } else if ( op->o_conn &&
1773 ( ( ( sendok & LDAP_BACK_SENDOK ) && ERR_OK( rs->sr_err ) )
1774 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1776 send_ldap_result( op, rs );
1780 if ( rs->sr_matched != match ) {
1781 free( (char *)rs->sr_matched );
1783 rs->sr_matched = NULL;
1784 ldap_memfree( match );
1788 ldap_memfree( text );
1793 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1798 ber_memvfree( (void **)refs );
1802 assert( rs->sr_ctrls != NULL );
1803 ldap_controls_free( ctrls );
1804 rs->sr_ctrls = NULL;
1807 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1810 /* return true if bound, false if failed */
1812 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1814 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1817 assert( lcp != NULL );
1818 assert( *lcp != NULL );
1820 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1822 if ( (*lcp)->lc_refcnt == 1 ) {
1823 int binding = LDAP_BACK_CONN_BINDING( *lcp );
1825 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1826 Debug( LDAP_DEBUG_ANY,
1827 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1828 op->o_log_prefix, li->li_uri,
1829 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1830 "" : (*lcp)->lc_bound_ndn.bv_val );
1831 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1833 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1834 (*lcp)->lc_ld = NULL;
1835 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1837 /* lc here must be the regular lc, reset and ready for init */
1838 rc = ldap_back_prepare_conn( *lcp, op, rs, sendok );
1839 if ( rc != LDAP_SUCCESS ) {
1840 /* freeit, because lc_refcnt == 1 */
1841 (*lcp)->lc_refcnt = 0;
1842 (void)ldap_back_freeconn( li, *lcp, 0 );
1846 } else if ( ( sendok & LDAP_BACK_BINDING ) ) {
1848 LDAP_BACK_CONN_BINDING_SET( *lcp );
1853 rc = ldap_back_dobind_int( lcp, op, rs, sendok, 0, 0 );
1854 if ( rc == 0 && *lcp != NULL ) {
1855 /* freeit, because lc_refcnt == 1 */
1856 (*lcp)->lc_refcnt = 0;
1857 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1858 (void)ldap_back_freeconn( li, *lcp, 0 );
1864 Debug( LDAP_DEBUG_TRACE,
1865 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1866 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1868 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1869 ldap_back_release_conn_lock( li, lcp, 0 );
1870 assert( *lcp == NULL );
1872 if ( sendok & LDAP_BACK_SENDERR ) {
1873 rs->sr_err = LDAP_UNAVAILABLE;
1874 rs->sr_text = "Unable to retry";
1875 send_ldap_result( op, rs );
1879 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1885 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
1886 struct berval *binddn, struct berval *bindcred )
1888 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1892 if ( op->o_conn == NULL || op->o_do_not_cache ) {
1896 /* don't proxyAuthz if protocol is not LDAPv3 */
1897 switch ( li->li_version ) {
1902 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1908 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1909 if ( sendok & LDAP_BACK_SENDERR ) {
1910 send_ldap_result( op, rs );
1917 *binddn = slap_empty_bv;
1918 *bindcred = slap_empty_bv;
1920 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1921 ndn = op->o_conn->c_ndn;
1927 switch ( li->li_idassert_mode ) {
1928 case LDAP_BACK_IDASSERT_LEGACY:
1929 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1930 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1932 *binddn = li->li_idassert_authcDN;
1933 *bindcred = li->li_idassert_passwd;
1940 /* NOTE: rootdn can always idassert */
1941 if ( BER_BVISNULL( &ndn )
1942 && li->li_idassert_authz == NULL
1943 && !( li->li_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL ) )
1945 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1946 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1947 if ( sendok & LDAP_BACK_SENDERR ) {
1948 send_ldap_result( op, rs );
1953 rs->sr_err = LDAP_SUCCESS;
1954 *binddn = slap_empty_bv;
1955 *bindcred = slap_empty_bv;
1961 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1962 struct berval authcDN;
1964 if ( BER_BVISNULL( &ndn ) ) {
1965 authcDN = slap_empty_bv;
1970 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1971 &authcDN, &authcDN );
1972 if ( rs->sr_err != LDAP_SUCCESS ) {
1973 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1974 if ( sendok & LDAP_BACK_SENDERR ) {
1975 send_ldap_result( op, rs );
1980 rs->sr_err = LDAP_SUCCESS;
1981 *binddn = slap_empty_bv;
1982 *bindcred = slap_empty_bv;
1990 *binddn = li->li_idassert_authcDN;
1991 *bindcred = li->li_idassert_passwd;
2001 ldap_back_proxy_authz_bind(
2005 ldap_back_send_t sendok,
2006 struct berval *binddn,
2007 struct berval *bindcred )
2009 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2014 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2015 ndn = op->o_conn->c_ndn;
2021 if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
2022 #ifdef HAVE_CYRUS_SASL
2023 void *defaults = NULL;
2024 struct berval authzID = BER_BVNULL;
2027 /* if SASL supports native authz, prepare for it */
2028 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
2029 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2031 switch ( li->li_idassert_mode ) {
2032 case LDAP_BACK_IDASSERT_OTHERID:
2033 case LDAP_BACK_IDASSERT_OTHERDN:
2034 authzID = li->li_idassert_authzID;
2037 case LDAP_BACK_IDASSERT_ANONYMOUS:
2038 BER_BVSTR( &authzID, "dn:" );
2041 case LDAP_BACK_IDASSERT_SELF:
2042 if ( BER_BVISNULL( &ndn ) ) {
2043 /* connection is not authc'd, so don't idassert */
2044 BER_BVSTR( &authzID, "dn:" );
2047 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
2048 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
2049 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
2050 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
2051 ndn.bv_val, ndn.bv_len + 1 );
2060 if ( li->li_idassert_secprops != NULL ) {
2061 rs->sr_err = ldap_set_option( lc->lc_ld,
2062 LDAP_OPT_X_SASL_SECPROPS,
2063 (void *)li->li_idassert_secprops );
2065 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
2066 rs->sr_err = LDAP_OTHER;
2067 if ( sendok & LDAP_BACK_SENDERR ) {
2068 send_ldap_result( op, rs );
2070 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2075 defaults = lutil_sasl_defaults( lc->lc_ld,
2076 li->li_idassert_sasl_mech.bv_val,
2077 li->li_idassert_sasl_realm.bv_val,
2078 li->li_idassert_authcID.bv_val,
2079 li->li_idassert_passwd.bv_val,
2082 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn->bv_val,
2083 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
2084 LDAP_SASL_QUIET, lutil_sasl_interact,
2087 rs->sr_err = slap_map_api2result( rs );
2088 if ( rs->sr_err != LDAP_SUCCESS ) {
2089 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2090 if ( sendok & LDAP_BACK_SENDERR ) {
2091 send_ldap_result( op, rs );
2095 LDAP_BACK_CONN_ISBOUND_SET( lc );
2098 lutil_sasl_freedefs( defaults );
2100 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
2104 #endif /* HAVE_CYRUS_SASL */
2107 switch ( li->li_idassert_authmethod ) {
2108 case LDAP_AUTH_NONE:
2109 /* FIXME: do we really need this? */
2110 BER_BVSTR( binddn, "" );
2111 BER_BVSTR( bindcred, "" );
2114 case LDAP_AUTH_SIMPLE:
2115 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
2116 binddn->bv_val, LDAP_SASL_SIMPLE,
2117 bindcred, NULL, NULL, &msgid );
2118 rc = ldap_back_op_result( lc, op, rs, msgid,
2119 -1, ( sendok | LDAP_BACK_BINDING ) );
2124 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2125 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
2126 if ( sendok & LDAP_BACK_SENDERR ) {
2127 send_ldap_result( op, rs );
2132 if ( rc == LDAP_SUCCESS ) {
2133 /* set rebind stuff in case of successful proxyAuthz bind,
2134 * so that referral chasing is attempted using the right
2136 LDAP_BACK_CONN_ISBOUND_SET( lc );
2137 ber_bvreplace( &lc->lc_bound_ndn, binddn );
2139 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
2140 memset( lc->lc_cred.bv_val, 0,
2141 lc->lc_cred.bv_len );
2144 if ( LDAP_BACK_SAVECRED( li ) ) {
2145 ber_bvreplace( &lc->lc_cred, bindcred );
2146 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
2149 lc->lc_cred.bv_len = 0;
2153 return LDAP_BACK_CONN_ISBOUND( lc );
2157 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
2158 * to existing server-side controls if required; if not,
2159 * the existing server-side controls are placed in *pctrls.
2160 * The caller, after using the controls in client API
2161 * operations, if ( *pctrls != op->o_ctrls ), should
2162 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
2163 * The function returns success if the control could
2164 * be added if required, or if it did nothing; in the future,
2165 * it might return some error if it failed.
2167 * if no bind took place yet, but the connection is bound
2168 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
2169 * and explicitly add proxyAuthz the control to every operation
2170 * with the dn bound to the connection as control value.
2172 * If no server-side controls are defined for the operation,
2173 * simply add the proxyAuthz control; otherwise, if the
2174 * proxyAuthz control is not already set, add it as
2177 * FIXME: is controls order significant for security?
2178 * ANSWER: controls ordering and interoperability
2179 * must be indicated by the specs of each control; if none
2180 * is specified, the order is irrelevant.
2183 ldap_back_proxy_authz_ctrl(
2184 struct berval *bound_ndn,
2186 slap_idassert_t *si,
2189 LDAPControl ***pctrls )
2191 LDAPControl **ctrls = NULL;
2193 slap_idassert_mode_t mode;
2194 struct berval assertedID,
2200 rs->sr_err = LDAP_SUCCESS;
2202 /* don't proxyAuthz if protocol is not LDAPv3 */
2203 switch ( version ) {
2208 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2217 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
2218 * but if it is not set this test fails. We need a different
2219 * means to detect if idassert is enabled */
2220 if ( ( BER_BVISNULL( &si->si_bc.sb_authcId ) || BER_BVISEMPTY( &si->si_bc.sb_authcId ) )
2221 && ( BER_BVISNULL( &si->si_bc.sb_binddn ) || BER_BVISEMPTY( &si->si_bc.sb_binddn ) ) )
2226 if ( !op->o_conn || op->o_do_not_cache || ( isroot = be_isroot( op ) ) ) {
2230 if ( op->o_tag == LDAP_REQ_BIND ) {
2231 ndn = op->o_req_ndn;
2233 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2234 ndn = op->o_conn->c_ndn;
2240 if ( si->si_mode == LDAP_BACK_IDASSERT_LEGACY ) {
2241 if ( op->o_proxy_authz ) {
2243 * FIXME: we do not want to perform proxyAuthz
2244 * on behalf of the client, because this would
2245 * be performed with "proxyauthzdn" privileges.
2247 * This might actually be too strict, since
2248 * the "proxyauthzdn" authzTo, and each entry's
2249 * authzFrom attributes may be crafted
2250 * to avoid unwanted proxyAuthz to take place.
2253 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2254 rs->sr_text = "proxyAuthz not allowed within namingContext";
2259 if ( !BER_BVISNULL( bound_ndn ) ) {
2263 if ( BER_BVISNULL( &ndn ) ) {
2267 if ( BER_BVISNULL( &si->si_bc.sb_binddn ) ) {
2271 } else if ( si->si_bc.sb_method == LDAP_AUTH_SASL ) {
2272 if ( ( si->si_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2274 /* already asserted in SASL via native authz */
2278 } else if ( si->si_authz && !isroot ) {
2280 struct berval authcDN;
2282 if ( BER_BVISNULL( &ndn ) ) {
2283 authcDN = slap_empty_bv;
2287 rc = slap_sasl_matches( op, si->si_authz,
2288 &authcDN, & authcDN );
2289 if ( rc != LDAP_SUCCESS ) {
2290 if ( si->si_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2291 /* ndn is not authorized
2292 * to use idassert */
2299 if ( op->o_proxy_authz ) {
2302 * 1) ignore the already set proxyAuthz control
2303 * 2) leave it in place, and don't set ours
2305 * 4) reject the operation
2307 * option (4) is very drastic
2308 * option (3) will make the remote server reject
2309 * the operation, thus being equivalent to (4)
2310 * option (2) will likely break the idassert
2311 * assumptions, so we cannot accept it;
2312 * option (1) means that we are contradicting
2313 * the client's reques.
2315 * I think (4) is the only correct choice.
2317 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2318 rs->sr_text = "proxyAuthz not allowed within namingContext";
2321 if ( op->o_is_auth_check ) {
2322 mode = LDAP_BACK_IDASSERT_NOASSERT;
2329 case LDAP_BACK_IDASSERT_LEGACY:
2330 /* original behavior:
2331 * assert the client's identity */
2332 case LDAP_BACK_IDASSERT_SELF:
2336 case LDAP_BACK_IDASSERT_ANONYMOUS:
2337 /* assert "anonymous" */
2338 assertedID = slap_empty_bv;
2341 case LDAP_BACK_IDASSERT_NOASSERT:
2342 /* don't assert; bind as proxyauthzdn */
2345 case LDAP_BACK_IDASSERT_OTHERID:
2346 case LDAP_BACK_IDASSERT_OTHERDN:
2347 /* assert idassert DN */
2348 assertedID = si->si_bc.sb_authzId;
2355 /* if we got here, "" is allowed to proxyAuthz */
2356 if ( BER_BVISNULL( &assertedID ) ) {
2357 assertedID = slap_empty_bv;
2360 /* don't idassert the bound DN (ITS#4497) */
2361 if ( dn_match( &assertedID, bound_ndn ) ) {
2365 if ( op->o_ctrls ) {
2366 for ( i = 0; op->o_ctrls[ i ]; i++ )
2367 /* just count ctrls */ ;
2370 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
2372 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
2374 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
2375 ctrls[ 0 ]->ldctl_iscritical = 1;
2377 switch ( si->si_mode ) {
2378 /* already in u:ID or dn:DN form */
2379 case LDAP_BACK_IDASSERT_OTHERID:
2380 case LDAP_BACK_IDASSERT_OTHERDN:
2381 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
2384 /* needs the dn: prefix */
2386 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
2387 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
2389 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
2390 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
2391 assertedID.bv_val, assertedID.bv_len + 1 );
2395 /* Older versions of <draft-weltman-ldapv3-proxy> required
2396 * to encode the value of the authzID (and called it proxyDN);
2397 * this hack provides compatibility with those DSAs that
2398 * implement it this way */
2399 if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
2400 struct berval authzID = ctrls[ 0 ]->ldctl_value;
2401 BerElementBuffer berbuf;
2402 BerElement *ber = (BerElement *)&berbuf;
2405 ber_init2( ber, 0, LBER_USE_DER );
2406 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2408 tag = ber_printf( ber, "O", &authzID );
2409 if ( tag == LBER_ERROR ) {
2410 rs->sr_err = LDAP_OTHER;
2414 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
2415 rs->sr_err = LDAP_OTHER;
2420 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2421 ber_free_buf( ber );
2423 if ( rs->sr_err != LDAP_SUCCESS ) {
2424 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2429 } else if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
2430 struct berval authzID = ctrls[ 0 ]->ldctl_value,
2432 BerElementBuffer berbuf;
2433 BerElement *ber = (BerElement *)&berbuf;
2436 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
2437 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
2438 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2440 rs->sr_err = LDAP_PROTOCOL_ERROR;
2445 tmp.bv_val += STRLENOF( "dn:" );
2446 tmp.bv_len -= STRLENOF( "dn:" );
2448 ber_init2( ber, 0, LBER_USE_DER );
2449 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2451 /* apparently, Mozilla API encodes this
2452 * as "SEQUENCE { LDAPDN }" */
2453 tag = ber_printf( ber, "{O}", &tmp );
2454 if ( tag == LBER_ERROR ) {
2455 rs->sr_err = LDAP_OTHER;
2459 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
2460 rs->sr_err = LDAP_OTHER;
2465 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2466 ber_free_buf( ber );
2468 if ( rs->sr_err != LDAP_SUCCESS ) {
2469 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2474 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
2477 if ( op->o_ctrls ) {
2478 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
2479 ctrls[ i + 1 ] = op->o_ctrls[ i ];
2482 ctrls[ i + 1 ] = NULL;
2485 if ( ctrls == NULL ) {
2486 ctrls = op->o_ctrls;
2495 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
2497 LDAPControl **ctrls = *pctrls;
2499 /* we assume that the first control is the proxyAuthz
2500 * added by back-ldap, so it's the only one we explicitly
2502 if ( ctrls && ctrls != op->o_ctrls ) {
2503 assert( ctrls[ 0 ] != NULL );
2505 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
2506 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
2509 op->o_tmpfree( ctrls, op->o_tmpmemctx );