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);
604 #endif /* HAVE_TLS */
606 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
607 rs->sr_err = ldap_initialize( &ld, li->li_uri );
608 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
609 if ( rs->sr_err != LDAP_SUCCESS ) {
613 /* Set LDAP version. This will always succeed: If the client
614 * bound with a particular version, then so can we.
616 if ( li->li_version != 0 ) {
617 version = li->li_version;
619 } else if ( op->o_protocol != 0 ) {
620 version = op->o_protocol;
623 /* assume it's an internal op; set to LDAPv3 */
624 version = LDAP_VERSION3;
626 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
628 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
629 ldap_set_option( ld, LDAP_OPT_REFERRALS,
630 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
632 if ( li->li_network_timeout > 0 ) {
635 tv.tv_sec = li->li_network_timeout;
637 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
641 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
642 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
643 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
644 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
645 if ( rs->sr_err != LDAP_SUCCESS ) {
646 ldap_unbind_ext( ld, NULL, NULL );
647 rs->sr_text = "Start TLS failed";
650 } else if ( li->li_idle_timeout ) {
651 /* only touch when activity actually took place... */
652 lc_time = op->o_time;
654 #endif /* HAVE_TLS */
660 LDAP_BACK_CONN_ISTLS_SET( lc );
662 LDAP_BACK_CONN_ISTLS_CLEAR( lc );
664 if ( lc_time != (time_t)(-1) ) {
665 lc->lc_time = lc_time;
667 #endif /* HAVE_TLS */
670 if ( rs->sr_err != LDAP_SUCCESS ) {
671 rs->sr_err = slap_map_api2result( rs );
672 if ( sendok & LDAP_BACK_SENDERR ) {
673 if ( rs->sr_text == NULL ) {
674 rs->sr_text = "Proxy connection initialization failed";
676 send_ldap_result( op, rs );
680 if ( li->li_conn_ttl > 0 ) {
681 lc->lc_create_time = op->o_time;
692 ldap_back_send_t sendok,
693 struct berval *binddn,
694 struct berval *bindcred )
696 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
697 ldapconn_t *lc = NULL,
700 lookupconn = !( sendok & LDAP_BACK_BINDING );
702 /* if the server is quarantined, and
703 * - the current interval did not expire yet, or
704 * - no more retries should occur,
705 * don't return the connection */
706 if ( li->li_isquarantined ) {
707 slap_retry_info_t *ri = &li->li_quarantine;
710 if ( li->li_quarantine.ri_interval ) {
711 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
712 if ( li->li_isquarantined == LDAP_BACK_FQ_YES ) {
713 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
714 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
716 Debug( LDAP_DEBUG_ANY,
717 "%s: ldap_back_getconn quarantine "
718 "retry block #%d try #%d.\n",
719 op->o_log_prefix, ri->ri_idx, ri->ri_count );
720 li->li_isquarantined = LDAP_BACK_FQ_RETRYING;
723 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
727 rs->sr_err = LDAP_UNAVAILABLE;
728 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
729 rs->sr_text = "Target is quarantined";
730 send_ldap_result( op, rs );
736 /* Internal searches are privileged and shared. So is root. */
737 if ( op->o_do_not_cache || be_isroot( op ) ) {
738 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
739 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
740 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
743 struct berval tmpbinddn,
750 if ( binddn == NULL ) {
753 if ( bindcred == NULL ) {
754 bindcred = &tmpbindcred;
756 if ( op->o_tag == LDAP_REQ_BIND ) {
757 save_o_dn = op->o_dn;
758 save_o_ndn = op->o_ndn;
759 op->o_dn = op->o_req_dn;
760 op->o_ndn = op->o_req_ndn;
762 isproxyauthz = ldap_back_is_proxy_authz( op, rs, sendok, binddn, bindcred );
763 if ( isproxyauthz == -1 ) {
766 if ( op->o_tag == LDAP_REQ_BIND ) {
767 op->o_dn = save_o_dn;
768 op->o_ndn = save_o_ndn;
771 lc_curr.lc_local_ndn = op->o_ndn;
772 /* Explicit binds must not be shared;
773 * however, explicit binds are piped in a special connection
774 * when idassert is to occur with "override" set */
775 if ( op->o_tag == LDAP_REQ_BIND && !isproxyauthz ) {
776 lc_curr.lc_conn = op->o_conn;
779 if ( isproxyauthz && !( sendok & LDAP_BACK_BINDING ) ) {
780 lc_curr.lc_local_ndn = *binddn;
781 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
782 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
784 } else if ( isproxyauthz && ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) {
785 lc_curr.lc_local_ndn = slap_empty_bv;
786 LDAP_BACK_PCONN_BIND_SET( &lc_curr, op );
787 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
790 } else if ( SLAP_IS_AUTHZ_BACKEND( op ) ) {
791 lc_curr.lc_conn = op->o_conn;
794 LDAP_BACK_PCONN_ANON_SET( &lc_curr, op );
799 /* Explicit Bind requests always get their own conn */
802 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
803 if ( LDAP_BACK_PCONN_ISPRIV( &lc_curr ) ) {
804 /* lookup a conn that's not binding */
805 LDAP_TAILQ_FOREACH( lc,
806 &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv,
809 if ( !LDAP_BACK_CONN_BINDING( lc ) && lc->lc_refcnt == 0 ) {
815 if ( lc != LDAP_TAILQ_LAST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
818 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
820 LDAP_TAILQ_ENTRY_INIT( lc, lc_q );
821 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
825 } else if ( !LDAP_BACK_USE_TEMPORARIES( li )
826 && li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_num == li->li_conn_priv_max )
828 lc = LDAP_TAILQ_FIRST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv );
833 /* Searches for a ldapconn in the avl tree */
834 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
835 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
839 /* Don't reuse connections while they're still binding */
840 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
841 if ( !LDAP_BACK_USE_TEMPORARIES( li ) ) {
842 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
844 ldap_pvt_thread_yield();
851 if ( op->o_tag == LDAP_REQ_BIND ) {
852 /* right now, this is the only possible case */
853 assert( ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) );
854 LDAP_BACK_CONN_BINDING_SET( lc );
857 refcnt = ++lc->lc_refcnt;
860 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
863 /* Looks like we didn't get a bind. Open a new session... */
865 lc = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
866 lc->lc_flags = li->li_flags;
867 lc->lc_lcflags = lc_curr.lc_lcflags;
868 if ( ldap_back_prepare_conn( lc, op, rs, sendok ) != LDAP_SUCCESS ) {
873 if ( sendok & LDAP_BACK_BINDING ) {
874 LDAP_BACK_CONN_BINDING_SET( lc );
877 lc->lc_conn = lc_curr.lc_conn;
878 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
881 * the rationale is: connections as the rootdn are privileged,
882 * so acl_authcDN is to be used; however, in some cases
883 * one already configured identity assertion with a highly
884 * privileged idassert_authcDN, so if acl_authcDN is NULL
885 * and idassert_authcDN is not, use the second instead.
887 * might change in the future, because it's preferable
888 * to make clear what identity is being used, since
889 * the only drawback is that one risks to configure
890 * the same identity twice...
892 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
893 if ( BER_BVISNULL( &li->li_acl_authcDN ) && !BER_BVISNULL( &li->li_idassert_authcDN ) ) {
894 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
895 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
898 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
899 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
901 LDAP_BACK_CONN_ISPRIV_SET( lc );
903 } else if ( LDAP_BACK_CONN_ISIDASSERT( &lc_curr ) ) {
904 if ( !LDAP_BACK_PCONN_ISBIND( &lc_curr ) ) {
905 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
906 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
908 LDAP_BACK_CONN_ISIDASSERT_SET( lc );
911 BER_BVZERO( &lc->lc_cred );
912 BER_BVZERO( &lc->lc_bound_ndn );
913 if ( !BER_BVISEMPTY( &op->o_ndn )
914 && SLAP_IS_AUTHZ_BACKEND( op ) )
916 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
921 /* if start TLS failed but it was not mandatory,
922 * check if the non-TLS connection was already
923 * in cache; in case, destroy the newly created
924 * connection and use the existing one */
925 if ( LDAP_BACK_PCONN_ISTLS( lc )
926 && !ldap_tls_inplace( lc->lc_ld ) )
928 ldapconn_t *tmplc = NULL;
929 int idx = LDAP_BACK_CONN2PRIV( &lc_curr ) - 1;
931 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
932 LDAP_TAILQ_FOREACH( tmplc,
933 &li->li_conn_priv[ idx ].lic_priv,
936 if ( !LDAP_BACK_CONN_BINDING( tmplc ) ) {
941 if ( tmplc != NULL ) {
942 refcnt = ++tmplc->lc_refcnt;
943 ldap_back_conn_free( lc );
946 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
948 if ( tmplc != NULL ) {
952 #endif /* HAVE_TLS */
954 /* Inserts the newly created ldapconn in the avl tree */
955 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
957 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
959 assert( lc->lc_refcnt == 1 );
961 #if LDAP_BACK_PRINT_CONNTREE > 0
962 ldap_back_print_conntree( li, ">>> ldap_back_getconn(insert)" );
963 #endif /* LDAP_BACK_PRINT_CONNTREE */
965 if ( LDAP_BACK_PCONN_ISPRIV( lc ) ) {
966 if ( li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num < li->li_conn_priv_max ) {
967 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv, lc, lc_q );
968 li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num++;
969 LDAP_BACK_CONN_CACHED_SET( lc );
972 LDAP_BACK_CONN_TAINTED_SET( lc );
977 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
978 ldap_back_conndn_cmp, ldap_back_conndn_dup );
979 LDAP_BACK_CONN_CACHED_SET( lc );
982 #if LDAP_BACK_PRINT_CONNTREE > 0
983 ldap_back_print_conntree( li, "<<< ldap_back_getconn(insert)" );
984 #endif /* LDAP_BACK_PRINT_CONNTREE */
986 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
988 if ( StatslogTest( LDAP_DEBUG_TRACE ) ) {
989 char buf[ SLAP_TEXT_BUFLEN ];
991 snprintf( buf, sizeof( buf ),
992 "lc=%p inserted refcnt=%u rc=%d",
993 (void *)lc, refcnt, rs->sr_err );
995 Debug( LDAP_DEBUG_TRACE,
996 "=>ldap_back_getconn: %s: %s\n",
997 op->o_log_prefix, buf, 0 );
1000 if ( !LDAP_BACK_PCONN_ISPRIV( lc ) ) {
1001 /* Err could be -1 in case a duplicate ldapconn is inserted */
1002 switch ( rs->sr_err ) {
1007 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1008 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( li ) ) {
1009 /* duplicate: free and try to get the newly created one */
1010 ldap_back_conn_free( lc );
1015 /* taint connection, so that it'll be freed when released */
1016 LDAP_BACK_CONN_TAINTED_SET( lc );
1020 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1021 ldap_back_conn_free( lc );
1022 rs->sr_err = LDAP_OTHER;
1023 rs->sr_text = "Proxy bind collision";
1024 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1025 send_ldap_result( op, rs );
1034 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
1035 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
1039 /* let it be used, but taint/delete it so that
1040 * no-one else can look it up any further */
1041 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1043 #if LDAP_BACK_PRINT_CONNTREE > 0
1044 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1045 #endif /* LDAP_BACK_PRINT_CONNTREE */
1047 (void)ldap_back_conn_delete( li, lc );
1048 LDAP_BACK_CONN_TAINTED_SET( lc );
1050 #if LDAP_BACK_PRINT_CONNTREE > 0
1051 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1052 #endif /* LDAP_BACK_PRINT_CONNTREE */
1054 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1057 if ( StatslogTest( LDAP_DEBUG_TRACE ) ) {
1058 char buf[ SLAP_TEXT_BUFLEN ];
1060 snprintf( buf, sizeof( buf ),
1061 "conn %p fetched refcnt=%u%s",
1063 expiring ? " expiring" : "" );
1064 Debug( LDAP_DEBUG_TRACE,
1065 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
1071 #endif /* HAVE_TLS */
1077 ldap_back_release_conn_lock(
1083 ldapconn_t *lc = *lcp;
1086 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1088 assert( lc->lc_refcnt > 0 );
1089 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1091 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
1092 ldap_back_freeconn( li, lc, 0 );
1096 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1101 ldap_back_quarantine(
1105 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1107 slap_retry_info_t *ri = &li->li_quarantine;
1109 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
1111 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1112 time_t new_last = slap_get_time();
1114 switch ( li->li_isquarantined ) {
1115 case LDAP_BACK_FQ_NO:
1116 if ( ri->ri_last == new_last ) {
1120 Debug( LDAP_DEBUG_ANY,
1121 "%s: ldap_back_quarantine enter.\n",
1122 op->o_log_prefix, 0, 0 );
1128 case LDAP_BACK_FQ_RETRYING:
1129 Debug( LDAP_DEBUG_ANY,
1130 "%s: ldap_back_quarantine block #%d try #%d failed.\n",
1131 op->o_log_prefix, ri->ri_idx, ri->ri_count );
1134 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1135 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1146 li->li_isquarantined = LDAP_BACK_FQ_YES;
1147 ri->ri_last = new_last;
1149 } else if ( li->li_isquarantined != LDAP_BACK_FQ_NO ) {
1150 if ( ri->ri_last == slap_get_time() ) {
1154 Debug( LDAP_DEBUG_ANY,
1155 "%s: ldap_back_quarantine exit (%d) err=%d.\n",
1156 op->o_log_prefix, li->li_isquarantined, rs->sr_err );
1158 if ( li->li_quarantine_f ) {
1159 (void)li->li_quarantine_f( li, li->li_quarantine_p );
1164 li->li_isquarantined = LDAP_BACK_FQ_NO;
1168 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
1172 * ldap_back_dobind_int
1174 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1177 ldap_back_dobind_int(
1181 ldap_back_send_t sendok,
1185 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1188 struct berval binddn = slap_empty_bv,
1189 bindcred = slap_empty_bv;
1196 assert( lcp != NULL );
1197 assert( retries >= 0 );
1199 if ( sendok & LDAP_BACK_GETCONN ) {
1200 assert( *lcp == NULL );
1202 lc = ldap_back_getconn( op, rs, sendok, &binddn, &bindcred );
1212 assert( lc != NULL );
1216 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1219 if ( binding == 0 ) {
1220 /* check if already bound */
1221 rc = isbound = LDAP_BACK_CONN_ISBOUND( lc );
1224 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1229 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
1230 /* if someone else is about to bind it, give up and retry */
1232 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1234 ldap_pvt_thread_yield();
1238 /* otherwise this thread will bind it */
1239 LDAP_BACK_CONN_BINDING_SET( lc );
1245 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1249 * FIXME: we need to let clients use proxyAuthz
1250 * otherwise we cannot do symmetric pools of servers;
1251 * we have to live with the fact that a user can
1252 * authorize itself as any ID that is allowed
1253 * by the authzTo directive of the "proxyauthzdn".
1256 * NOTE: current Proxy Authorization specification
1257 * and implementation do not allow proxy authorization
1258 * control to be provided with Bind requests
1261 * if no bind took place yet, but the connection is bound
1262 * and the "idassert-authcDN" (or other ID) is set,
1263 * then bind as the asserting identity and explicitly
1264 * add the proxyAuthz control to every operation with the
1265 * dn bound to the connection as control value.
1266 * This is done also if this is the authrizing backend,
1267 * but the "override" flag is given to idassert.
1268 * It allows to use SASL bind and yet proxyAuthz users
1270 if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
1271 if ( BER_BVISEMPTY( &binddn ) && BER_BVISEMPTY( &bindcred ) ) {
1272 /* if we got here, it shouldn't return result */
1273 rc = ldap_back_is_proxy_authz( op, rs,
1274 LDAP_BACK_DONTSEND, &binddn, &bindcred );
1277 rc = ldap_back_proxy_authz_bind( lc, op, rs, sendok, &binddn, &bindcred );
1281 #ifdef HAVE_CYRUS_SASL
1282 if ( LDAP_BACK_CONN_ISPRIV( lc )
1283 && li->li_acl_authmethod == LDAP_AUTH_SASL )
1285 void *defaults = NULL;
1287 if ( li->li_acl_secprops != NULL ) {
1288 rc = ldap_set_option( lc->lc_ld,
1289 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops );
1291 if ( rc != LDAP_OPT_SUCCESS ) {
1292 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
1293 "(SECPROPS,\"%s\") failed!\n",
1294 li->li_acl_secprops, 0, 0 );
1299 defaults = lutil_sasl_defaults( lc->lc_ld,
1300 li->li_acl_sasl_mech.bv_val,
1301 li->li_acl_sasl_realm.bv_val,
1302 li->li_acl_authcID.bv_val,
1303 li->li_acl_passwd.bv_val,
1306 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
1307 li->li_acl_authcDN.bv_val,
1308 li->li_acl_sasl_mech.bv_val, NULL, NULL,
1309 LDAP_SASL_QUIET, lutil_sasl_interact,
1312 lutil_sasl_freedefs( defaults );
1314 rs->sr_err = slap_map_api2result( rs );
1315 if ( rs->sr_err != LDAP_SUCCESS ) {
1316 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1317 if ( sendok & LDAP_BACK_SENDERR ) {
1318 send_ldap_result( op, rs );
1322 LDAP_BACK_CONN_ISBOUND_SET( lc );
1325 if ( LDAP_BACK_QUARANTINE( li ) ) {
1326 ldap_back_quarantine( op, rs );
1331 #endif /* HAVE_CYRUS_SASL */
1334 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1335 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1336 LDAP_SASL_SIMPLE, &lc->lc_cred,
1337 NULL, NULL, &msgid );
1339 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
1340 if ( retries != LDAP_BACK_RETRY_NEVER ) {
1342 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1345 assert( lc->lc_refcnt > 0 );
1346 if ( lc->lc_refcnt == 1 ) {
1347 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1350 /* lc here must be the regular lc, reset and ready for init */
1351 rs->sr_err = ldap_back_prepare_conn( lc, op, rs, sendok );
1352 if ( rs->sr_err != LDAP_SUCCESS ) {
1353 sendok &= ~LDAP_BACK_SENDERR;
1359 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1362 if ( rs->sr_err == LDAP_SUCCESS ) {
1363 if ( retries > 0 ) {
1370 assert( lc->lc_refcnt == 1 );
1372 ldap_back_freeconn( li, lc, dolock );
1374 rs->sr_err = slap_map_api2result( rs );
1376 if ( LDAP_BACK_QUARANTINE( li ) ) {
1377 ldap_back_quarantine( op, rs );
1380 if ( rs->sr_err != LDAP_SUCCESS &&
1381 ( sendok & LDAP_BACK_SENDERR ) )
1383 rs->sr_text = "Internal proxy bind failure";
1384 send_ldap_result( op, rs );
1390 rc = ldap_back_op_result( lc, op, rs, msgid,
1391 -1, ( sendok | LDAP_BACK_BINDING ) );
1392 if ( rc == LDAP_SUCCESS ) {
1393 LDAP_BACK_CONN_ISBOUND_SET( lc );
1397 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1398 rc = LDAP_BACK_CONN_ISBOUND( lc );
1400 ldap_back_release_conn_lock( li, lcp, dolock );
1402 } else if ( LDAP_BACK_SAVECRED( li ) ) {
1403 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1412 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1415 ldap_back_dobind( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1417 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1419 return ldap_back_dobind_int( lcp, op, rs,
1420 ( sendok | LDAP_BACK_GETCONN ), li->li_nretries, 1 );
1424 * ldap_back_default_rebind
1426 * This is a callback used for chasing referrals using the same
1427 * credentials as the original user on this session.
1430 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1431 ber_int_t msgid, void *params )
1433 ldapconn_t *lc = (ldapconn_t *)params;
1436 /* ... otherwise we couldn't get here */
1437 assert( lc != NULL );
1439 if ( !ldap_tls_inplace( ld ) ) {
1440 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1442 const char *text = NULL;
1444 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1445 LDAP_BACK_RETRY_DEFAULT, &text );
1446 if ( rc != LDAP_SUCCESS ) {
1450 #endif /* HAVE_TLS */
1452 /* FIXME: add checks on the URL/identity? */
1454 return ldap_sasl_bind_s( ld,
1455 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1456 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1465 ldap_back_send_t sendok )
1467 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1469 /* default behavior */
1470 if ( LDAP_BACK_ABANDON( li ) ) {
1471 return ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1474 if ( LDAP_BACK_CANCEL( li ) ) {
1475 /* FIXME: asynchronous? */
1476 return ldap_cancel_s( lc->lc_ld, msgid, NULL, NULL );
1485 ldap_back_op_result(
1491 ldap_back_send_t sendok )
1493 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1498 LDAPControl **ctrls = NULL;
1500 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
1503 rs->sr_matched = NULL;
1505 rs->sr_ctrls = NULL;
1507 /* if the error recorded in the reply corresponds
1508 * to a successful state, get the error from the
1509 * remote server response */
1510 if ( ERR_OK( rs->sr_err ) ) {
1513 LDAPMessage *res = NULL;
1514 time_t stoptime = (time_t)(-1);
1515 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
1516 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
1517 const char *timeout_text = "Operation timed out";
1519 /* if timeout is not specified, compute and use
1520 * the one specific to the ongoing operation */
1521 if ( timeout == (time_t)(-1) ) {
1522 slap_op_t opidx = slap_req2op( op->o_tag );
1524 if ( opidx == SLAP_OP_SEARCH ) {
1525 if ( op->ors_tlimit <= 0 ) {
1529 timeout = op->ors_tlimit;
1530 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
1531 timeout_text = NULL;
1535 timeout = li->li_timeout[ opidx ];
1539 /* better than nothing :) */
1540 if ( timeout == 0 ) {
1541 if ( li->li_idle_timeout ) {
1542 timeout = li->li_idle_timeout;
1544 } else if ( li->li_conn_ttl ) {
1545 timeout = li->li_conn_ttl;
1550 stoptime = op->o_time + timeout;
1553 LDAP_BACK_TV_SET( &tv );
1556 /* if result parsing fails, note the failure reason */
1557 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1560 if ( timeout && slap_get_time() > stoptime ) {
1561 if ( sendok & LDAP_BACK_BINDING ) {
1562 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1565 /* let it be used, but taint/delete it so that
1566 * no-one else can look it up any further */
1567 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1569 #if LDAP_BACK_PRINT_CONNTREE > 0
1570 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1571 #endif /* LDAP_BACK_PRINT_CONNTREE */
1573 (void)ldap_back_conn_delete( li, lc );
1574 LDAP_BACK_CONN_TAINTED_SET( lc );
1576 #if LDAP_BACK_PRINT_CONNTREE > 0
1577 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1578 #endif /* LDAP_BACK_PRINT_CONNTREE */
1579 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1582 (void)ldap_back_cancel( lc, op, rs, msgid, sendok );
1584 rs->sr_err = timeout_err;
1585 rs->sr_text = timeout_text;
1590 LDAP_BACK_TV_SET( &tv );
1591 ldap_pvt_thread_yield();
1595 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1600 /* otherwise get the result; if it is not
1601 * LDAP_SUCCESS, record it in the reply
1602 * structure (this includes
1603 * LDAP_COMPARE_{TRUE|FALSE}) */
1605 /* only touch when activity actually took place... */
1606 if ( li->li_idle_timeout && lc ) {
1607 lc->lc_time = op->o_time;
1610 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1611 &match, &text, &refs, &ctrls, 1 );
1613 if ( rc != LDAP_SUCCESS ) {
1617 /* RFC 4511: referrals can only appear
1618 * if result code is LDAP_REFERRAL */
1620 && refs[ 0 ] != NULL
1621 && refs[ 0 ][ 0 ] != '\0' )
1623 if ( rs->sr_err != LDAP_REFERRAL ) {
1624 Debug( LDAP_DEBUG_ANY,
1625 "%s ldap_back_op_result: "
1626 "got referrals with err=%d\n",
1633 for ( i = 0; refs[ i ] != NULL; i++ )
1635 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1637 for ( i = 0; refs[ i ] != NULL; i++ ) {
1638 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1640 BER_BVZERO( &rs->sr_ref[ i ] );
1643 } else if ( rs->sr_err == LDAP_REFERRAL ) {
1644 Debug( LDAP_DEBUG_ANY,
1645 "%s ldap_back_op_result: "
1646 "got err=%d with null "
1647 "or empty referrals\n",
1651 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1654 if ( ctrls != NULL ) {
1655 rs->sr_ctrls = ctrls;
1660 /* if the error in the reply structure is not
1661 * LDAP_SUCCESS, try to map it from client
1662 * to server error */
1663 if ( !ERR_OK( rs->sr_err ) ) {
1664 rs->sr_err = slap_map_api2result( rs );
1666 /* internal ops ( op->o_conn == NULL )
1667 * must not reply to client */
1668 if ( op->o_conn && !op->o_do_not_cache && match ) {
1670 /* record the (massaged) matched
1671 * DN into the reply structure */
1672 rs->sr_matched = match;
1676 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1677 if ( !( sendok & LDAP_BACK_RETRYING ) ) {
1678 if ( LDAP_BACK_QUARANTINE( li ) ) {
1679 ldap_back_quarantine( op, rs );
1681 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1682 if ( rs->sr_text == NULL ) rs->sr_text = "Proxy operation retry failed";
1683 send_ldap_result( op, rs );
1687 } else if ( op->o_conn &&
1688 ( ( ( sendok & LDAP_BACK_SENDOK ) && ERR_OK( rs->sr_err ) )
1689 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1691 send_ldap_result( op, rs );
1695 if ( rs->sr_matched != match ) {
1696 free( (char *)rs->sr_matched );
1698 rs->sr_matched = NULL;
1699 ldap_memfree( match );
1703 ldap_memfree( text );
1708 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1713 ber_memvfree( (void **)refs );
1717 assert( rs->sr_ctrls != NULL );
1718 ldap_controls_free( ctrls );
1719 rs->sr_ctrls = NULL;
1722 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1725 /* return true if bound, false if failed */
1727 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1729 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1732 assert( lcp != NULL );
1733 assert( *lcp != NULL );
1735 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1737 if ( (*lcp)->lc_refcnt == 1 ) {
1738 int binding = LDAP_BACK_CONN_BINDING( *lcp );
1740 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1741 Debug( LDAP_DEBUG_ANY,
1742 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1743 op->o_log_prefix, li->li_uri,
1744 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1745 "" : (*lcp)->lc_bound_ndn.bv_val );
1746 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1748 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1749 (*lcp)->lc_ld = NULL;
1750 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1752 /* lc here must be the regular lc, reset and ready for init */
1753 rc = ldap_back_prepare_conn( *lcp, op, rs, sendok );
1754 if ( rc != LDAP_SUCCESS ) {
1755 /* freeit, because lc_refcnt == 1 */
1756 (*lcp)->lc_refcnt = 0;
1757 (void)ldap_back_freeconn( li, *lcp, 0 );
1761 } else if ( ( sendok & LDAP_BACK_BINDING ) ) {
1763 LDAP_BACK_CONN_BINDING_SET( *lcp );
1768 rc = ldap_back_dobind_int( lcp, op, rs, sendok, 0, 0 );
1769 if ( rc == 0 && *lcp != NULL ) {
1770 /* freeit, because lc_refcnt == 1 */
1771 (*lcp)->lc_refcnt = 0;
1772 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1773 (void)ldap_back_freeconn( li, *lcp, 0 );
1779 Debug( LDAP_DEBUG_TRACE,
1780 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1781 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1783 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1784 ldap_back_release_conn_lock( li, lcp, 0 );
1785 assert( *lcp == NULL );
1787 if ( sendok & LDAP_BACK_SENDERR ) {
1788 rs->sr_err = LDAP_UNAVAILABLE;
1789 rs->sr_text = "Unable to retry";
1790 send_ldap_result( op, rs );
1794 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1800 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
1801 struct berval *binddn, struct berval *bindcred )
1803 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1807 if ( op->o_conn == NULL || op->o_do_not_cache ) {
1811 /* don't proxyAuthz if protocol is not LDAPv3 */
1812 switch ( li->li_version ) {
1817 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1823 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1824 if ( sendok & LDAP_BACK_SENDERR ) {
1825 send_ldap_result( op, rs );
1832 *binddn = slap_empty_bv;
1833 *bindcred = slap_empty_bv;
1835 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1836 ndn = op->o_conn->c_ndn;
1842 switch ( li->li_idassert_mode ) {
1843 case LDAP_BACK_IDASSERT_LEGACY:
1844 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1845 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1847 *binddn = li->li_idassert_authcDN;
1848 *bindcred = li->li_idassert_passwd;
1855 /* NOTE: rootdn can always idassert */
1856 if ( BER_BVISNULL( &ndn )
1857 && li->li_idassert_authz == NULL
1858 && !( li->li_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL ) )
1860 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1861 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1862 if ( sendok & LDAP_BACK_SENDERR ) {
1863 send_ldap_result( op, rs );
1868 rs->sr_err = LDAP_SUCCESS;
1869 *binddn = slap_empty_bv;
1870 *bindcred = slap_empty_bv;
1876 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1877 struct berval authcDN;
1879 if ( BER_BVISNULL( &ndn ) ) {
1880 authcDN = slap_empty_bv;
1885 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1886 &authcDN, &authcDN );
1887 if ( rs->sr_err != LDAP_SUCCESS ) {
1888 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1889 if ( sendok & LDAP_BACK_SENDERR ) {
1890 send_ldap_result( op, rs );
1895 rs->sr_err = LDAP_SUCCESS;
1896 *binddn = slap_empty_bv;
1897 *bindcred = slap_empty_bv;
1905 *binddn = li->li_idassert_authcDN;
1906 *bindcred = li->li_idassert_passwd;
1916 ldap_back_proxy_authz_bind(
1920 ldap_back_send_t sendok,
1921 struct berval *binddn,
1922 struct berval *bindcred )
1924 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1929 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1930 ndn = op->o_conn->c_ndn;
1936 if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1937 #ifdef HAVE_CYRUS_SASL
1938 void *defaults = NULL;
1939 struct berval authzID = BER_BVNULL;
1942 /* if SASL supports native authz, prepare for it */
1943 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1944 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1946 switch ( li->li_idassert_mode ) {
1947 case LDAP_BACK_IDASSERT_OTHERID:
1948 case LDAP_BACK_IDASSERT_OTHERDN:
1949 authzID = li->li_idassert_authzID;
1952 case LDAP_BACK_IDASSERT_ANONYMOUS:
1953 BER_BVSTR( &authzID, "dn:" );
1956 case LDAP_BACK_IDASSERT_SELF:
1957 if ( BER_BVISNULL( &ndn ) ) {
1958 /* connection is not authc'd, so don't idassert */
1959 BER_BVSTR( &authzID, "dn:" );
1962 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1963 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1964 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1965 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1966 ndn.bv_val, ndn.bv_len + 1 );
1975 if ( li->li_idassert_secprops != NULL ) {
1976 rs->sr_err = ldap_set_option( lc->lc_ld,
1977 LDAP_OPT_X_SASL_SECPROPS,
1978 (void *)li->li_idassert_secprops );
1980 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1981 rs->sr_err = LDAP_OTHER;
1982 if ( sendok & LDAP_BACK_SENDERR ) {
1983 send_ldap_result( op, rs );
1985 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1990 defaults = lutil_sasl_defaults( lc->lc_ld,
1991 li->li_idassert_sasl_mech.bv_val,
1992 li->li_idassert_sasl_realm.bv_val,
1993 li->li_idassert_authcID.bv_val,
1994 li->li_idassert_passwd.bv_val,
1997 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn->bv_val,
1998 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1999 LDAP_SASL_QUIET, lutil_sasl_interact,
2002 rs->sr_err = slap_map_api2result( rs );
2003 if ( rs->sr_err != LDAP_SUCCESS ) {
2004 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2005 if ( sendok & LDAP_BACK_SENDERR ) {
2006 send_ldap_result( op, rs );
2010 LDAP_BACK_CONN_ISBOUND_SET( lc );
2013 lutil_sasl_freedefs( defaults );
2015 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
2019 #endif /* HAVE_CYRUS_SASL */
2022 switch ( li->li_idassert_authmethod ) {
2023 case LDAP_AUTH_NONE:
2024 /* FIXME: do we really need this? */
2025 BER_BVSTR( binddn, "" );
2026 BER_BVSTR( bindcred, "" );
2029 case LDAP_AUTH_SIMPLE:
2030 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
2031 binddn->bv_val, LDAP_SASL_SIMPLE,
2032 bindcred, NULL, NULL, &msgid );
2033 rc = ldap_back_op_result( lc, op, rs, msgid,
2034 -1, ( sendok | LDAP_BACK_BINDING ) );
2039 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2040 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
2041 if ( sendok & LDAP_BACK_SENDERR ) {
2042 send_ldap_result( op, rs );
2047 if ( rc == LDAP_SUCCESS ) {
2048 /* set rebind stuff in case of successful proxyAuthz bind,
2049 * so that referral chasing is attempted using the right
2051 LDAP_BACK_CONN_ISBOUND_SET( lc );
2052 ber_bvreplace( &lc->lc_bound_ndn, binddn );
2054 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
2055 memset( lc->lc_cred.bv_val, 0,
2056 lc->lc_cred.bv_len );
2059 if ( LDAP_BACK_SAVECRED( li ) ) {
2060 ber_bvreplace( &lc->lc_cred, bindcred );
2061 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
2064 lc->lc_cred.bv_len = 0;
2068 return LDAP_BACK_CONN_ISBOUND( lc );
2072 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
2073 * to existing server-side controls if required; if not,
2074 * the existing server-side controls are placed in *pctrls.
2075 * The caller, after using the controls in client API
2076 * operations, if ( *pctrls != op->o_ctrls ), should
2077 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
2078 * The function returns success if the control could
2079 * be added if required, or if it did nothing; in the future,
2080 * it might return some error if it failed.
2082 * if no bind took place yet, but the connection is bound
2083 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
2084 * and explicitly add proxyAuthz the control to every operation
2085 * with the dn bound to the connection as control value.
2087 * If no server-side controls are defined for the operation,
2088 * simply add the proxyAuthz control; otherwise, if the
2089 * proxyAuthz control is not already set, add it as
2092 * FIXME: is controls order significant for security?
2093 * ANSWER: controls ordering and interoperability
2094 * must be indicated by the specs of each control; if none
2095 * is specified, the order is irrelevant.
2098 ldap_back_proxy_authz_ctrl(
2099 struct berval *bound_ndn,
2101 slap_idassert_t *si,
2104 LDAPControl ***pctrls )
2106 LDAPControl **ctrls = NULL;
2108 slap_idassert_mode_t mode;
2109 struct berval assertedID,
2115 rs->sr_err = LDAP_SUCCESS;
2117 /* don't proxyAuthz if protocol is not LDAPv3 */
2118 switch ( version ) {
2123 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2132 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
2133 * but if it is not set this test fails. We need a different
2134 * means to detect if idassert is enabled */
2135 if ( ( BER_BVISNULL( &si->si_bc.sb_authcId ) || BER_BVISEMPTY( &si->si_bc.sb_authcId ) )
2136 && ( BER_BVISNULL( &si->si_bc.sb_binddn ) || BER_BVISEMPTY( &si->si_bc.sb_binddn ) ) )
2141 if ( !op->o_conn || op->o_do_not_cache || ( isroot = be_isroot( op ) ) ) {
2145 if ( op->o_tag == LDAP_REQ_BIND ) {
2146 ndn = op->o_req_ndn;
2148 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2149 ndn = op->o_conn->c_ndn;
2155 if ( si->si_mode == LDAP_BACK_IDASSERT_LEGACY ) {
2156 if ( op->o_proxy_authz ) {
2158 * FIXME: we do not want to perform proxyAuthz
2159 * on behalf of the client, because this would
2160 * be performed with "proxyauthzdn" privileges.
2162 * This might actually be too strict, since
2163 * the "proxyauthzdn" authzTo, and each entry's
2164 * authzFrom attributes may be crafted
2165 * to avoid unwanted proxyAuthz to take place.
2168 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2169 rs->sr_text = "proxyAuthz not allowed within namingContext";
2174 if ( !BER_BVISNULL( bound_ndn ) ) {
2178 if ( BER_BVISNULL( &ndn ) ) {
2182 if ( BER_BVISNULL( &si->si_bc.sb_binddn ) ) {
2186 } else if ( si->si_bc.sb_method == LDAP_AUTH_SASL ) {
2187 if ( ( si->si_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2189 /* already asserted in SASL via native authz */
2193 } else if ( si->si_authz && !isroot ) {
2195 struct berval authcDN;
2197 if ( BER_BVISNULL( &ndn ) ) {
2198 authcDN = slap_empty_bv;
2202 rc = slap_sasl_matches( op, si->si_authz,
2203 &authcDN, & authcDN );
2204 if ( rc != LDAP_SUCCESS ) {
2205 if ( si->si_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2206 /* ndn is not authorized
2207 * to use idassert */
2214 if ( op->o_proxy_authz ) {
2217 * 1) ignore the already set proxyAuthz control
2218 * 2) leave it in place, and don't set ours
2220 * 4) reject the operation
2222 * option (4) is very drastic
2223 * option (3) will make the remote server reject
2224 * the operation, thus being equivalent to (4)
2225 * option (2) will likely break the idassert
2226 * assumptions, so we cannot accept it;
2227 * option (1) means that we are contradicting
2228 * the client's reques.
2230 * I think (4) is the only correct choice.
2232 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2233 rs->sr_text = "proxyAuthz not allowed within namingContext";
2236 if ( op->o_is_auth_check ) {
2237 mode = LDAP_BACK_IDASSERT_NOASSERT;
2244 case LDAP_BACK_IDASSERT_LEGACY:
2245 /* original behavior:
2246 * assert the client's identity */
2247 case LDAP_BACK_IDASSERT_SELF:
2251 case LDAP_BACK_IDASSERT_ANONYMOUS:
2252 /* assert "anonymous" */
2253 assertedID = slap_empty_bv;
2256 case LDAP_BACK_IDASSERT_NOASSERT:
2257 /* don't assert; bind as proxyauthzdn */
2260 case LDAP_BACK_IDASSERT_OTHERID:
2261 case LDAP_BACK_IDASSERT_OTHERDN:
2262 /* assert idassert DN */
2263 assertedID = si->si_bc.sb_authzId;
2270 /* if we got here, "" is allowed to proxyAuthz */
2271 if ( BER_BVISNULL( &assertedID ) ) {
2272 assertedID = slap_empty_bv;
2275 /* don't idassert the bound DN (ITS#4497) */
2276 if ( dn_match( &assertedID, bound_ndn ) ) {
2280 if ( op->o_ctrls ) {
2281 for ( i = 0; op->o_ctrls[ i ]; i++ )
2282 /* just count ctrls */ ;
2285 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
2287 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
2289 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
2290 ctrls[ 0 ]->ldctl_iscritical = 1;
2292 switch ( si->si_mode ) {
2293 /* already in u:ID or dn:DN form */
2294 case LDAP_BACK_IDASSERT_OTHERID:
2295 case LDAP_BACK_IDASSERT_OTHERDN:
2296 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
2299 /* needs the dn: prefix */
2301 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
2302 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
2304 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
2305 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
2306 assertedID.bv_val, assertedID.bv_len + 1 );
2310 /* Older versions of <draft-weltman-ldapv3-proxy> required
2311 * to encode the value of the authzID (and called it proxyDN);
2312 * this hack provides compatibility with those DSAs that
2313 * implement it this way */
2314 if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
2315 struct berval authzID = ctrls[ 0 ]->ldctl_value;
2316 BerElementBuffer berbuf;
2317 BerElement *ber = (BerElement *)&berbuf;
2320 ber_init2( ber, 0, LBER_USE_DER );
2321 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2323 tag = ber_printf( ber, "O", &authzID );
2324 if ( tag == LBER_ERROR ) {
2325 rs->sr_err = LDAP_OTHER;
2329 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
2330 rs->sr_err = LDAP_OTHER;
2335 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2336 ber_free_buf( ber );
2338 if ( rs->sr_err != LDAP_SUCCESS ) {
2339 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2344 } else if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
2345 struct berval authzID = ctrls[ 0 ]->ldctl_value,
2347 BerElementBuffer berbuf;
2348 BerElement *ber = (BerElement *)&berbuf;
2351 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
2352 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
2353 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2355 rs->sr_err = LDAP_PROTOCOL_ERROR;
2360 tmp.bv_val += STRLENOF( "dn:" );
2361 tmp.bv_len -= STRLENOF( "dn:" );
2363 ber_init2( ber, 0, LBER_USE_DER );
2364 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2366 /* apparently, Mozilla API encodes this
2367 * as "SEQUENCE { LDAPDN }" */
2368 tag = ber_printf( ber, "{O}", &tmp );
2369 if ( tag == LBER_ERROR ) {
2370 rs->sr_err = LDAP_OTHER;
2374 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
2375 rs->sr_err = LDAP_OTHER;
2380 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2381 ber_free_buf( ber );
2383 if ( rs->sr_err != LDAP_SUCCESS ) {
2384 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2389 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
2392 if ( op->o_ctrls ) {
2393 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
2394 ctrls[ i + 1 ] = op->o_ctrls[ i ];
2397 ctrls[ i + 1 ] = NULL;
2400 if ( ctrls == NULL ) {
2401 ctrls = op->o_ctrls;
2410 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
2412 LDAPControl **ctrls = *pctrls;
2414 /* we assume that the first control is the proxyAuthz
2415 * added by back-ldap, so it's the only one we explicitly
2417 if ( ctrls && ctrls != op->o_ctrls ) {
2418 assert( ctrls[ 0 ] != NULL );
2420 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
2421 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
2424 op->o_tmpfree( ctrls, op->o_tmpmemctx );