1 /* bind.c - ldap backend bind function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2006 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"
36 #include <lutil_ldap.h>
38 #ifndef PRINT_CONNTREE
39 #define PRINT_CONNTREE 0
40 #endif /* !PRINT_CONNTREE */
42 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ "2.16.840.1.113730.3.4.12"
44 static LDAP_REBIND_PROC ldap_back_default_rebind;
46 LDAP_REBIND_PROC *ldap_back_rebind_f = ldap_back_default_rebind;
49 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
52 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
55 ldap_back_conndnlc_cmp( const void *c1, const void *c2 );
58 ldap_back_bind( Operation *op, SlapReply *rs )
60 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
66 lc = ldap_back_getconn( op, rs, LDAP_BACK_BIND_SERR );
71 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
72 ch_free( lc->lc_bound_ndn.bv_val );
73 BER_BVZERO( &lc->lc_bound_ndn );
75 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
77 /* method is always LDAP_AUTH_SIMPLE if we got here */
78 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
80 &op->orb_cred, op->o_ctrls, NULL, &msgid );
81 rc = ldap_back_op_result( lc, op, rs, msgid, 0, LDAP_BACK_SENDERR );
83 if ( rc == LDAP_SUCCESS ) {
84 /* If defined, proxyAuthz will be used also when
85 * back-ldap is the authorizing backend; for this
86 * purpose, a successful bind is followed by a
87 * bind with the configured identity assertion */
88 /* NOTE: use with care */
89 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
90 ldap_back_proxy_authz_bind( lc, op, rs, LDAP_BACK_SENDERR );
91 if ( !LDAP_BACK_CONN_ISBOUND( lc ) ) {
97 LDAP_BACK_CONN_ISBOUND_SET( lc );
98 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
100 if ( LDAP_BACK_SAVECRED( li ) ) {
101 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
102 memset( lc->lc_cred.bv_val, 0,
103 lc->lc_cred.bv_len );
105 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
106 ldap_set_rebind_proc( lc->lc_ld, ldap_back_rebind_f, lc );
111 assert( lc->lc_binding == 1 );
114 /* must re-insert if local DN changed as result of bind */
115 if ( !LDAP_BACK_CONN_ISBOUND( lc )
116 || ( LDAP_BACK_CONN_ISBOUND( lc )
117 && !dn_match( &op->o_req_ndn, &lc->lc_local_ndn ) ) )
122 /* wait for all other ops to release the connection */
124 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
125 if ( lc->lc_refcnt > 1 ) {
126 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
127 ldap_pvt_thread_yield();
131 assert( lc->lc_refcnt == 1 );
132 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
133 ldap_back_conndnlc_cmp );
134 assert( tmplc == NULL || lc == tmplc );
136 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
137 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
138 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
139 ldap_back_conndn_cmp, ldap_back_conndn_dup );
142 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
148 /* duplicate; someone else successfully bound
149 * on the same connection with the same identity;
150 * we can do this because lc_refcnt == 1 */
151 ldap_back_conn_free( lc );
157 ldap_back_release_conn( op, rs, lc );
164 * ldap_back_conndn_cmp
166 * compares two ldapconn_t based on the value of the conn pointer
167 * and of the local DN; used by avl stuff for insert, lookup
171 ldap_back_conndn_cmp( const void *c1, const void *c2 )
173 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
174 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
177 /* If local DNs don't match, it is definitely not a match */
178 /* For shared sessions, conn is NULL. Only explicitly
179 * bound sessions will have non-NULL conn.
181 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
183 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
190 * ldap_back_conndnlc_cmp
192 * compares two ldapconn_t based on the value of the conn pointer,
193 * the local DN and the lc pointer; used by avl stuff for insert, lookup
197 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
199 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
200 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
203 /* If local DNs don't match, it is definitely not a match */
204 /* For shared sessions, conn is NULL. Only explicitly
205 * bound sessions will have non-NULL conn.
207 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
209 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
211 rc = SLAP_PTRCMP( lc1, lc2 );
221 * compares two ldapconn_t based on the value of the conn pointer;
222 * used by avl stuff for delete of all conns with the same connid
225 ldap_back_conn_cmp( const void *c1, const void *c2 )
227 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
228 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
230 /* For shared sessions, conn is NULL. Only explicitly
231 * bound sessions will have non-NULL conn.
233 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
237 * ldap_back_conndn_dup
239 * returns -1 in case a duplicate ldapconn_t has been inserted;
243 ldap_back_conndn_dup( void *c1, void *c2 )
245 ldapconn_t *lc1 = (ldapconn_t *)c1;
246 ldapconn_t *lc2 = (ldapconn_t *)c2;
248 /* Cannot have more than one shared session with same DN */
249 if ( lc1->lc_conn == lc2->lc_conn &&
250 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
258 #if PRINT_CONNTREE > 0
260 ravl_print( Avlnode *root, int depth )
269 ravl_print( root->avl_right, depth+1 );
271 for ( i = 0; i < depth; i++ ) {
272 fprintf( stderr, "-" );
276 fprintf( stderr, "lc=%p local=\"%s\" conn=%p %s refcnt=%d\n",
277 (void *)lc, lc->lc_local_ndn.bv_val, (void *)lc->lc_conn,
278 avl_bf2str( root->avl_bf ), lc->lc_refcnt );
280 ravl_print( root->avl_left, depth+1 );
284 myprint( Avlnode *root )
286 fprintf( stderr, "========>\n" );
289 fprintf( stderr, "\tNULL\n" );
292 ravl_print( root, 0 );
295 fprintf( stderr, "<========\n" );
297 #endif /* PRINT_CONNTREE */
300 ldap_back_freeconn( Operation *op, ldapconn_t *lc, int dolock )
302 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
306 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
309 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
310 ldap_back_conndnlc_cmp );
311 assert( LDAP_BACK_CONN_TAINTED( lc ) || tmplc == lc );
312 if ( lc->lc_refcnt == 0 ) {
313 ldap_back_conn_free( (void *)lc );
317 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
334 int rc = LDAP_SUCCESS;
337 /* this is ridiculous... */
338 dummy.li_flags = flags;
340 /* start TLS ("tls-[try-]{start,propagate}" statements) */
341 if ( ( LDAP_BACK_USE_TLS( &dummy ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS( &dummy ) ) )
342 && !ldap_is_ldaps_url( url ) )
344 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
346 * use asynchronous StartTLS
347 * in case, chase referral (not implemented yet)
351 if ( protocol == 0 ) {
352 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
356 if ( protocol < LDAP_VERSION3 ) {
357 protocol = LDAP_VERSION3;
358 /* Set LDAP version */
359 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
360 (const void *)&protocol );
363 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
364 if ( rc == LDAP_SUCCESS ) {
365 LDAPMessage *res = NULL;
368 LDAP_BACK_TV_SET( &tv );
371 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
373 rc = LDAP_UNAVAILABLE;
375 } else if ( rc == 0 ) {
376 if ( retries != LDAP_BACK_RETRY_NEVER ) {
377 ldap_pvt_thread_yield();
381 LDAP_BACK_TV_SET( &tv );
384 rc = LDAP_UNAVAILABLE;
386 } else if ( rc == LDAP_RES_EXTENDED ) {
387 struct berval *data = NULL;
389 rc = ldap_parse_extended_result( ld, res,
391 if ( rc == LDAP_SUCCESS ) {
393 rc = ldap_parse_result( ld, res, &err,
394 NULL, NULL, NULL, NULL, 1 );
395 if ( rc == LDAP_SUCCESS ) {
400 /* FIXME: in case a referral
401 * is returned, should we try
402 * using it instead of the
404 if ( rc == LDAP_SUCCESS ) {
405 rc = ldap_install_tls( ld );
407 } else if ( rc == LDAP_REFERRAL ) {
408 rc = LDAP_UNWILLING_TO_PERFORM;
409 *text = "unwilling to chase referral returned by Start TLS exop";
413 if ( data->bv_val ) {
414 ber_memfree( data->bv_val );
428 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
430 * use synchronous StartTLS
432 rc = ldap_start_tls_s( ld, NULL, NULL );
433 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
435 /* if StartTLS is requested, only attempt it if the URL
436 * is not "ldaps://"; this may occur not only in case
437 * of misconfiguration, but also when used in the chain
438 * overlay, where the "uri" can be parsed out of a referral */
444 case LDAP_SERVER_DOWN:
448 if ( LDAP_BACK_TLS_CRITICAL( &dummy ) ) {
449 *text = "could not start TLS";
453 /* in case Start TLS is not critical */
465 #endif /* HAVE_TLS */
468 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
470 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
471 int vers = op->o_protocol;
474 int is_tls = op->o_conn->c_is_tls;
475 #endif /* HAVE_TLS */
477 assert( lcp != NULL );
479 rs->sr_err = ldap_initialize( &ld, li->li_uri );
480 if ( rs->sr_err != LDAP_SUCCESS ) {
484 /* Set LDAP version. This will always succeed: If the client
485 * bound with a particular version, then so can we.
488 /* assume it's an internal op; set to LDAPv3 */
489 vers = LDAP_VERSION3;
491 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&vers );
493 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
494 ldap_set_option( ld, LDAP_OPT_REFERRALS,
495 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
497 if ( li->li_network_timeout > 0 ) {
500 tv.tv_sec = li->li_network_timeout;
502 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
506 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
507 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
508 if ( rs->sr_err != LDAP_SUCCESS ) {
509 ldap_unbind_ext( ld, NULL, NULL );
512 #endif /* HAVE_TLS */
514 if ( *lcp == NULL ) {
515 *lcp = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
516 (*lcp)->lc_flags= li->li_flags;
519 (*lcp)->lc_refcnt = 1;
520 (*lcp)->lc_binding = 1;
523 LDAP_BACK_CONN_ISTLS_SET( *lcp );
525 LDAP_BACK_CONN_ISTLS_CLEAR( *lcp );
527 #endif /* HAVE_TLS */
530 if ( rs->sr_err != LDAP_SUCCESS ) {
531 rs->sr_err = slap_map_api2result( rs );
532 if ( sendok & LDAP_BACK_SENDERR ) {
533 if ( rs->sr_text == NULL ) {
534 rs->sr_text = "ldap_initialize() failed";
536 send_ldap_result( op, rs );
541 if ( li->li_conn_ttl > 0 ) {
542 (*lcp)->lc_create_time = op->o_time;
550 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
552 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
553 ldapconn_t *lc = NULL,
555 int refcnt = 1, binding = 1;
557 /* Internal searches are privileged and shared. So is root. */
558 if ( op->o_do_not_cache || be_isroot( op ) ) {
559 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
560 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
561 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
564 lc_curr.lc_local_ndn = op->o_ndn;
565 /* Explicit binds must not be shared */
566 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
567 lc_curr.lc_conn = op->o_conn;
570 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
574 /* Explicit Bind requests always get their own conn */
575 if ( !( sendok & LDAP_BACK_BINDING ) ) {
576 /* Searches for a ldapconn in the avl tree */
578 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
580 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
581 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
583 /* Don't reuse connections while they're still binding */
584 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
585 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
586 ldap_pvt_thread_yield();
589 refcnt = ++lc->lc_refcnt;
590 binding = ++lc->lc_binding;
592 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
595 /* Looks like we didn't get a bind. Open a new session... */
597 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
600 if ( sendok & LDAP_BACK_BINDING ) {
601 LDAP_BACK_CONN_BINDING_SET( lc );
603 lc->lc_conn = lc_curr.lc_conn;
604 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
606 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
607 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
608 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
609 LDAP_BACK_CONN_ISPRIV_SET( lc );
612 BER_BVZERO( &lc->lc_cred );
613 BER_BVZERO( &lc->lc_bound_ndn );
615 /* FIXME: if we set lc_bound_ndn = o_ndn
616 * we end up with a bind with DN but no password! */
617 if ( !BER_BVISEMPTY( &op->o_ndn )
618 && SLAP_IS_AUTHZ_BACKEND( op ) )
620 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
626 /* if start TLS failed but it was not mandatory,
627 * check if the non-TLS connection was already
628 * in cache; in case, destroy the newly created
629 * connection and use the existing one */
630 if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
631 && !ldap_tls_inplace( lc->lc_ld ) )
635 lc_curr.lc_conn = LDAP_BACK_PCONN;
636 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
637 tmplc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
638 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
639 if ( tmplc != NULL ) {
640 refcnt = ++tmplc->lc_refcnt;
641 binding = ++tmplc->lc_binding;
642 ldap_back_conn_free( lc );
645 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
647 if ( tmplc != NULL ) {
651 #endif /* HAVE_TLS */
653 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
655 /* Inserts the newly created ldapconn in the avl tree */
656 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
658 assert( lc->lc_refcnt == 1 );
659 assert( lc->lc_binding == 1 );
660 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
661 ldap_back_conndn_cmp, ldap_back_conndn_dup );
663 #if PRINT_CONNTREE > 0
664 myprint( li->li_conninfo.lai_tree );
665 #endif /* PRINT_CONNTREE */
667 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
669 Debug( LDAP_DEBUG_TRACE,
670 "=>ldap_back_getconn: conn %p inserted refcnt=%u binding=%u\n",
671 (void *)lc, refcnt, binding );
673 /* Err could be -1 in case a duplicate ldapconn is inserted */
674 switch ( rs->sr_err ) {
679 if ( !( sendok & LDAP_BACK_BINDING ) ) {
680 /* duplicate: free and try to get the newly created one */
683 /* taint connection, so that it'll be freed when released */
684 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
685 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
686 ldap_back_conndnlc_cmp );
687 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
688 LDAP_BACK_CONN_TAINTED_SET( lc );
692 ldap_back_conn_free( lc );
693 rs->sr_err = LDAP_OTHER;
694 rs->sr_text = "proxy bind collision";
695 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
696 send_ldap_result( op, rs );
703 char buf[ SLAP_TEXT_BUFLEN ];
706 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
707 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
711 /* let it be used, but taint/delete it so that
712 * no-one else can look it up any further */
713 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
714 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
715 ldap_back_conndnlc_cmp );
716 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
717 LDAP_BACK_CONN_TAINTED_SET( lc );
721 snprintf( buf, sizeof( buf ),
722 "conn %p fetched refcnt=%u binding=%u%s",
723 (void *)lc, refcnt, binding, expiring ? " expiring" : "" );
724 Debug( LDAP_DEBUG_TRACE,
725 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
732 #endif /* HAVE_TLS */
733 if ( li->li_idle_timeout && lc ) {
734 lc->lc_time = op->o_time;
741 ldap_back_release_conn_lock(
747 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
750 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
752 assert( lc->lc_refcnt > 0 );
753 LDAP_BACK_CONN_BINDING_CLEAR( lc );
755 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
756 ldap_back_freeconn( op, lc, 0 );
759 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
766 * Note: as the check for the value of lc->lc_bound was already here, I removed
767 * it from all the callers, and I made the function return the flag, so
768 * it can be used to simplify the check.
770 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
773 ldap_back_dobind_int(
777 ldap_back_send_t sendok,
781 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
786 assert( retries >= 0 );
790 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
793 if ( binding == 0 ) {
794 /* check if already bound */
795 rc = LDAP_BACK_CONN_ISBOUND( lc );
799 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
804 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
805 /* if someone else is about to bind it, give up and retry */
807 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
809 ldap_pvt_thread_yield();
813 /* otherwise this thread will bind it */
814 LDAP_BACK_CONN_BINDING_SET( lc );
819 /* wait for pending operations to finish */
820 /* FIXME: may become a bottleneck! */
821 if ( lc->lc_refcnt != lc->lc_binding ) {
823 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
825 ldap_pvt_thread_yield();
830 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
834 while ( lc->lc_refcnt > 1 ) {
835 ldap_pvt_thread_yield();
836 rc = LDAP_BACK_CONN_ISBOUND( lc );
843 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
845 LDAP_BACK_CONN_BINDING_SET( lc );
847 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
852 * FIXME: we need to let clients use proxyAuthz
853 * otherwise we cannot do symmetric pools of servers;
854 * we have to live with the fact that a user can
855 * authorize itself as any ID that is allowed
856 * by the authzTo directive of the "proxyauthzdn".
859 * NOTE: current Proxy Authorization specification
860 * and implementation do not allow proxy authorization
861 * control to be provided with Bind requests
864 * if no bind took place yet, but the connection is bound
865 * and the "idassert-authcDN" (or other ID) is set,
866 * then bind as the asserting identity and explicitly
867 * add the proxyAuthz control to every operation with the
868 * dn bound to the connection as control value.
869 * This is done also if this is the authrizing backend,
870 * but the "override" flag is given to idassert.
871 * It allows to use SASL bind and yet proxyAuthz users
873 if ( op->o_conn != NULL &&
874 !op->o_do_not_cache &&
875 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
876 ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
878 (void)ldap_back_proxy_authz_bind( lc, op, rs, sendok );
882 #ifdef HAVE_CYRUS_SASL
883 if ( LDAP_BACK_CONN_ISPRIV( lc )
884 && li->li_acl_authmethod == LDAP_AUTH_SASL )
886 void *defaults = NULL;
888 if ( li->li_acl_secprops != NULL ) {
889 rc = ldap_set_option( lc->lc_ld,
890 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops);
892 if ( rc != LDAP_OPT_SUCCESS ) {
893 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
894 "(%s,SECPROPS,\"%s\") failed!\n",
895 li->li_uri, li->li_acl_secprops, 0 );
900 defaults = lutil_sasl_defaults( lc->lc_ld,
901 li->li_acl_sasl_mech.bv_val,
902 li->li_acl_sasl_realm.bv_val,
903 li->li_acl_authcID.bv_val,
904 li->li_acl_passwd.bv_val,
907 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
908 li->li_acl_authcDN.bv_val,
909 li->li_acl_sasl_mech.bv_val, NULL, NULL,
910 LDAP_SASL_QUIET, lutil_sasl_interact,
913 lutil_sasl_freedefs( defaults );
915 rs->sr_err = slap_map_api2result( rs );
916 if ( rs->sr_err != LDAP_SUCCESS ) {
917 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
918 send_ldap_result( op, rs );
921 LDAP_BACK_CONN_ISBOUND_SET( lc );
925 #endif /* HAVE_CYRUS_SASL */
928 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
929 lc->lc_bound_ndn.bv_val,
930 LDAP_SASL_SIMPLE, &lc->lc_cred,
931 NULL, NULL, &msgid );
933 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
934 if ( retries != LDAP_BACK_RETRY_NEVER ) {
936 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
939 assert( lc->lc_refcnt > 0 );
940 if ( lc->lc_refcnt == 1 ) {
941 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
944 /* lc here must be the regular lc, reset and ready for init */
945 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
946 if ( rs->sr_err != LDAP_SUCCESS ) {
953 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
956 if ( rs->sr_err == LDAP_SUCCESS ) {
965 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
969 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
973 ldap_back_freeconn( op, lc, dolock );
974 rs->sr_err = slap_map_api2result( rs );
979 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
980 if ( rc == LDAP_SUCCESS ) {
981 LDAP_BACK_CONN_ISBOUND_SET( lc );
986 LDAP_BACK_CONN_BINDING_CLEAR( lc );
987 rc = LDAP_BACK_CONN_ISBOUND( lc );
989 ldap_back_release_conn_lock( op, rs, lc, dolock );
996 ldap_back_dobind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
998 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1000 return ldap_back_dobind_int( lc, op, rs, sendok, li->li_nretries, 1 );
1004 * ldap_back_default_rebind
1006 * This is a callback used for chasing referrals using the same
1007 * credentials as the original user on this session.
1010 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1011 ber_int_t msgid, void *params )
1013 ldapconn_t *lc = (ldapconn_t *)params;
1016 /* ... otherwise we couldn't get here */
1017 assert( lc != NULL );
1019 if ( !ldap_tls_inplace( ld ) ) {
1020 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1022 const char *text = NULL;
1024 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1025 LDAP_BACK_RETRY_DEFAULT, &text );
1026 if ( rc != LDAP_SUCCESS ) {
1030 #endif /* HAVE_TLS */
1032 /* FIXME: add checks on the URL/identity? */
1034 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
1035 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1039 ldap_back_op_result(
1045 ldap_back_send_t sendok )
1048 LDAPMessage *res = NULL;
1051 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
1054 rs->sr_matched = NULL;
1056 /* if the error recorded in the reply corresponds
1057 * to a successful state, get the error from the
1058 * remote server response */
1059 if ( ERR_OK( rs->sr_err ) ) {
1064 tv.tv_sec = timeout;
1068 LDAP_BACK_TV_SET( &tv );
1072 /* if result parsing fails, note the failure reason */
1073 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1077 (void)ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1078 rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
1079 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
1080 rs->sr_text = "Operation timed out";
1084 LDAP_BACK_TV_SET( &tv );
1085 ldap_pvt_thread_yield();
1089 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1094 /* otherwise get the result; if it is not
1095 * LDAP_SUCCESS, record it in the reply
1096 * structure (this includes
1097 * LDAP_COMPARE_{TRUE|FALSE}) */
1099 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1100 &match, &text, NULL, NULL, 1 );
1101 #ifndef LDAP_NULL_IS_NULL
1102 if ( match != NULL && match[ 0 ] == '\0' ) {
1103 ldap_memfree( match );
1106 if ( text != NULL && text[ 0 ] == '\0' ) {
1107 ldap_memfree( text );
1110 #endif /* LDAP_NULL_IS_NULL */
1112 if ( rc != LDAP_SUCCESS ) {
1118 /* if the error in the reply structure is not
1119 * LDAP_SUCCESS, try to map it from client
1120 * to server error */
1121 if ( !ERR_OK( rs->sr_err ) ) {
1122 rs->sr_err = slap_map_api2result( rs );
1124 /* internal ops ( op->o_conn == NULL )
1125 * must not reply to client */
1126 if ( op->o_conn && !op->o_do_not_cache && match ) {
1128 /* record the (massaged) matched
1129 * DN into the reply structure */
1130 rs->sr_matched = match;
1134 ( ( sendok & LDAP_BACK_SENDOK )
1135 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1137 send_ldap_result( op, rs );
1140 if ( rs->sr_matched != match ) {
1141 free( (char *)rs->sr_matched );
1143 rs->sr_matched = NULL;
1144 ldap_memfree( match );
1147 ldap_memfree( text );
1150 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1153 /* return true if bound, false if failed */
1155 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1158 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1160 assert( lcp != NULL );
1161 assert( *lcp != NULL );
1163 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1165 if ( (*lcp)->lc_refcnt == 1 ) {
1166 Debug( LDAP_DEBUG_ANY,
1167 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1168 op->o_log_prefix, li->li_uri,
1169 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1170 "" : (*lcp)->lc_bound_ndn.bv_val );
1172 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1173 (*lcp)->lc_ld = NULL;
1174 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1176 /* lc here must be the regular lc, reset and ready for init */
1177 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
1178 if ( rc != LDAP_SUCCESS ) {
1183 rc = ldap_back_dobind_int( *lcp, op, rs, sendok, 0, 0 );
1190 Debug( LDAP_DEBUG_TRACE,
1191 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1192 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1194 ldap_back_release_conn_lock( op, rs, *lcp, 0 );
1198 rs->sr_err = LDAP_UNAVAILABLE;
1199 rs->sr_text = "unable to retry";
1200 send_ldap_result( op, rs );
1204 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1210 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1212 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1213 struct berval binddn = slap_empty_bv;
1214 struct berval bindcred = slap_empty_bv;
1220 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1221 ndn = op->o_conn->c_ndn;
1228 * FIXME: we need to let clients use proxyAuthz
1229 * otherwise we cannot do symmetric pools of servers;
1230 * we have to live with the fact that a user can
1231 * authorize itself as any ID that is allowed
1232 * by the authzTo directive of the "proxyauthzdn".
1235 * NOTE: current Proxy Authorization specification
1236 * and implementation do not allow proxy authorization
1237 * control to be provided with Bind requests
1240 * if no bind took place yet, but the connection is bound
1241 * and the "proxyauthzdn" is set, then bind as
1242 * "proxyauthzdn" and explicitly add the proxyAuthz
1243 * control to every operation with the dn bound
1244 * to the connection as control value.
1247 /* bind as proxyauthzdn only if no idassert mode
1248 * is requested, or if the client's identity
1250 switch ( li->li_idassert_mode ) {
1251 case LDAP_BACK_IDASSERT_LEGACY:
1252 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1253 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1255 binddn = li->li_idassert_authcDN;
1256 bindcred = li->li_idassert_passwd;
1263 /* NOTE: rootdn can always idassert */
1264 if ( BER_BVISNULL( &ndn ) && li->li_idassert_authz == NULL ) {
1265 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1266 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1267 if ( sendok & LDAP_BACK_SENDERR ) {
1268 send_ldap_result( op, rs );
1270 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1273 rs->sr_err = LDAP_SUCCESS;
1274 binddn = slap_empty_bv;
1275 bindcred = slap_empty_bv;
1281 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1282 struct berval authcDN;
1284 if ( BER_BVISNULL( &ndn ) ) {
1285 authcDN = slap_empty_bv;
1290 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1291 &authcDN, &authcDN );
1292 if ( rs->sr_err != LDAP_SUCCESS ) {
1293 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1294 if ( sendok & LDAP_BACK_SENDERR ) {
1295 send_ldap_result( op, rs );
1297 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1300 rs->sr_err = LDAP_SUCCESS;
1301 binddn = slap_empty_bv;
1302 bindcred = slap_empty_bv;
1310 binddn = li->li_idassert_authcDN;
1311 bindcred = li->li_idassert_passwd;
1316 if ( dobind && li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1317 #ifdef HAVE_CYRUS_SASL
1318 void *defaults = NULL;
1319 struct berval authzID = BER_BVNULL;
1322 /* if SASL supports native authz, prepare for it */
1323 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1324 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1326 switch ( li->li_idassert_mode ) {
1327 case LDAP_BACK_IDASSERT_OTHERID:
1328 case LDAP_BACK_IDASSERT_OTHERDN:
1329 authzID = li->li_idassert_authzID;
1332 case LDAP_BACK_IDASSERT_ANONYMOUS:
1333 BER_BVSTR( &authzID, "dn:" );
1336 case LDAP_BACK_IDASSERT_SELF:
1337 if ( BER_BVISNULL( &ndn ) ) {
1338 /* connection is not authc'd, so don't idassert */
1339 BER_BVSTR( &authzID, "dn:" );
1342 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1343 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1344 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1345 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1346 ndn.bv_val, ndn.bv_len + 1 );
1355 if ( li->li_idassert_secprops != NULL ) {
1356 rs->sr_err = ldap_set_option( lc->lc_ld,
1357 LDAP_OPT_X_SASL_SECPROPS,
1358 (void *)li->li_idassert_secprops );
1360 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1361 rs->sr_err = LDAP_OTHER;
1362 if ( sendok & LDAP_BACK_SENDERR ) {
1363 send_ldap_result( op, rs );
1365 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1370 defaults = lutil_sasl_defaults( lc->lc_ld,
1371 li->li_idassert_sasl_mech.bv_val,
1372 li->li_idassert_sasl_realm.bv_val,
1373 li->li_idassert_authcID.bv_val,
1374 li->li_idassert_passwd.bv_val,
1377 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1378 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1379 LDAP_SASL_QUIET, lutil_sasl_interact,
1382 rs->sr_err = slap_map_api2result( rs );
1383 if ( rs->sr_err != LDAP_SUCCESS ) {
1384 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1385 if ( sendok & LDAP_BACK_SENDERR ) {
1386 send_ldap_result( op, rs );
1390 LDAP_BACK_CONN_ISBOUND_SET( lc );
1393 lutil_sasl_freedefs( defaults );
1395 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1399 #endif /* HAVE_CYRUS_SASL */
1402 switch ( li->li_idassert_authmethod ) {
1403 case LDAP_AUTH_NONE:
1404 LDAP_BACK_CONN_ISBOUND_SET( lc );
1407 case LDAP_AUTH_SIMPLE:
1408 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1409 binddn.bv_val, LDAP_SASL_SIMPLE,
1410 &bindcred, NULL, NULL, &msgid );
1415 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1416 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1417 if ( sendok & LDAP_BACK_SENDERR ) {
1418 send_ldap_result( op, rs );
1423 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
1424 if ( rc == LDAP_SUCCESS ) {
1425 LDAP_BACK_CONN_ISBOUND_SET( lc );
1428 return LDAP_BACK_CONN_ISBOUND( lc );
1432 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1433 * to existing server-side controls if required; if not,
1434 * the existing server-side controls are placed in *pctrls.
1435 * The caller, after using the controls in client API
1436 * operations, if ( *pctrls != op->o_ctrls ), should
1437 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1438 * The function returns success if the control could
1439 * be added if required, or if it did nothing; in the future,
1440 * it might return some error if it failed.
1442 * if no bind took place yet, but the connection is bound
1443 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1444 * and explicitly add proxyAuthz the control to every operation
1445 * with the dn bound to the connection as control value.
1447 * If no server-side controls are defined for the operation,
1448 * simply add the proxyAuthz control; otherwise, if the
1449 * proxyAuthz control is not already set, add it as
1452 * FIXME: is controls order significant for security?
1453 * ANSWER: controls ordering and interoperability
1454 * must be indicated by the specs of each control; if none
1455 * is specified, the order is irrelevant.
1458 ldap_back_proxy_authz_ctrl(
1462 LDAPControl ***pctrls )
1464 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
1465 LDAPControl **ctrls = NULL;
1468 struct berval assertedID,
1473 rs->sr_err = LDAP_SUCCESS;
1475 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1476 * but if it is not set this test fails. We need a different
1477 * means to detect if idassert is enabled */
1478 if ( ( BER_BVISNULL( &li->li_idassert_authcID ) || BER_BVISEMPTY( &li->li_idassert_authcID ) )
1479 && ( BER_BVISNULL( &li->li_idassert_authcDN ) || BER_BVISEMPTY( &li->li_idassert_authcDN ) ) )
1484 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1488 if ( op->o_tag == LDAP_REQ_BIND ) {
1489 ndn = op->o_req_ndn;
1491 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1492 ndn = op->o_conn->c_ndn;
1498 if ( li->li_idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1499 if ( op->o_proxy_authz ) {
1501 * FIXME: we do not want to perform proxyAuthz
1502 * on behalf of the client, because this would
1503 * be performed with "proxyauthzdn" privileges.
1505 * This might actually be too strict, since
1506 * the "proxyauthzdn" authzTo, and each entry's
1507 * authzFrom attributes may be crafted
1508 * to avoid unwanted proxyAuthz to take place.
1511 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1512 rs->sr_text = "proxyAuthz not allowed within namingContext";
1517 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1521 if ( BER_BVISNULL( &ndn ) ) {
1525 if ( BER_BVISNULL( &li->li_idassert_authcDN ) ) {
1529 } else if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1530 if ( ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1531 /* && ( !BER_BVISNULL( &ndn )
1532 || LDAP_BACK_CONN_ISBOUND( lc ) ) */ )
1534 /* already asserted in SASL via native authz */
1535 /* NOTE: the test on lc->lc_bound is used to trap
1536 * native authorization of anonymous users,
1537 * since in that case ndn is NULL */
1541 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1543 struct berval authcDN;
1545 if ( BER_BVISNULL( &ndn ) ) {
1546 authcDN = slap_empty_bv;
1550 rc = slap_sasl_matches( op, li->li_idassert_authz,
1551 &authcDN, & authcDN );
1552 if ( rc != LDAP_SUCCESS ) {
1553 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1555 /* ndn is not authorized
1556 * to use idassert */
1563 if ( op->o_proxy_authz ) {
1566 * 1) ignore the already set proxyAuthz control
1567 * 2) leave it in place, and don't set ours
1569 * 4) reject the operation
1571 * option (4) is very drastic
1572 * option (3) will make the remote server reject
1573 * the operation, thus being equivalent to (4)
1574 * option (2) will likely break the idassert
1575 * assumptions, so we cannot accept it;
1576 * option (1) means that we are contradicting
1577 * the client's reques.
1579 * I think (4) is the only correct choice.
1581 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1582 rs->sr_text = "proxyAuthz not allowed within namingContext";
1585 if ( op->o_is_auth_check ) {
1586 mode = LDAP_BACK_IDASSERT_NOASSERT;
1589 mode = li->li_idassert_mode;
1593 case LDAP_BACK_IDASSERT_SELF:
1594 if ( BER_BVISNULL( &ndn ) ) {
1600 case LDAP_BACK_IDASSERT_LEGACY:
1601 /* original behavior:
1602 * assert the client's identity */
1603 if ( BER_BVISNULL( &ndn ) ) {
1604 assertedID = slap_empty_bv;
1610 case LDAP_BACK_IDASSERT_ANONYMOUS:
1611 /* assert "anonymous" */
1612 assertedID = slap_empty_bv;
1615 case LDAP_BACK_IDASSERT_NOASSERT:
1616 /* don't assert; bind as proxyauthzdn */
1619 case LDAP_BACK_IDASSERT_OTHERID:
1620 case LDAP_BACK_IDASSERT_OTHERDN:
1621 /* assert idassert DN */
1622 assertedID = li->li_idassert_authzID;
1629 if ( BER_BVISNULL( &assertedID ) ) {
1630 assertedID = slap_empty_bv;
1633 /* don't idassert the bound DN (ITS#4497) */
1634 if ( dn_match( &assertedID, &lc->lc_bound_ndn ) ) {
1638 if ( op->o_ctrls ) {
1639 for ( i = 0; op->o_ctrls[ i ]; i++ )
1640 /* just count ctrls */ ;
1643 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
1645 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
1647 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1648 ctrls[ 0 ]->ldctl_iscritical = 1;
1650 switch ( li->li_idassert_mode ) {
1651 /* already in u:ID or dn:DN form */
1652 case LDAP_BACK_IDASSERT_OTHERID:
1653 case LDAP_BACK_IDASSERT_OTHERDN:
1654 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
1657 /* needs the dn: prefix */
1659 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1660 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
1662 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1663 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1664 assertedID.bv_val, assertedID.bv_len + 1 );
1668 if ( op->o_ctrls ) {
1669 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1670 ctrls[ i + 1 ] = op->o_ctrls[ i ];
1673 ctrls[ i + 1 ] = NULL;
1676 if ( ctrls == NULL ) {
1677 ctrls = op->o_ctrls;
1686 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1688 LDAPControl **ctrls = *pctrls;
1690 /* we assume that the first control is the proxyAuthz
1691 * added by back-ldap, so it's the only one we explicitly
1693 if ( ctrls && ctrls != op->o_ctrls ) {
1694 assert( ctrls[ 0 ] != NULL );
1696 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1697 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1700 op->o_tmpfree( ctrls, op->o_tmpmemctx );