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 /* we should rather bail out... */
358 rc = LDAP_UNWILLING_TO_PERFORM;
359 *text = "invalid protocol version";
362 if ( rc == LDAP_SUCCESS ) {
363 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
366 if ( rc == LDAP_SUCCESS ) {
367 LDAPMessage *res = NULL;
370 LDAP_BACK_TV_SET( &tv );
373 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
375 rc = LDAP_UNAVAILABLE;
377 } else if ( rc == 0 ) {
378 if ( retries != LDAP_BACK_RETRY_NEVER ) {
379 ldap_pvt_thread_yield();
383 LDAP_BACK_TV_SET( &tv );
386 rc = LDAP_UNAVAILABLE;
388 } else if ( rc == LDAP_RES_EXTENDED ) {
389 struct berval *data = NULL;
391 rc = ldap_parse_extended_result( ld, res,
393 if ( rc == LDAP_SUCCESS ) {
395 rc = ldap_parse_result( ld, res, &err,
396 NULL, NULL, NULL, NULL, 1 );
397 if ( rc == LDAP_SUCCESS ) {
402 /* FIXME: in case a referral
403 * is returned, should we try
404 * using it instead of the
406 if ( rc == LDAP_SUCCESS ) {
407 rc = ldap_install_tls( ld );
409 } else if ( rc == LDAP_REFERRAL ) {
410 rc = LDAP_UNWILLING_TO_PERFORM;
411 *text = "unwilling to chase referral returned by Start TLS exop";
415 if ( data->bv_val ) {
416 ber_memfree( data->bv_val );
430 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
432 * use synchronous StartTLS
434 rc = ldap_start_tls_s( ld, NULL, NULL );
435 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
437 /* if StartTLS is requested, only attempt it if the URL
438 * is not "ldaps://"; this may occur not only in case
439 * of misconfiguration, but also when used in the chain
440 * overlay, where the "uri" can be parsed out of a referral */
446 case LDAP_SERVER_DOWN:
450 if ( LDAP_BACK_TLS_CRITICAL( &dummy ) ) {
451 *text = "could not start TLS";
455 /* in case Start TLS is not critical */
467 #endif /* HAVE_TLS */
470 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
472 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
476 int is_tls = op->o_conn->c_is_tls;
477 #endif /* HAVE_TLS */
479 assert( lcp != NULL );
481 rs->sr_err = ldap_initialize( &ld, li->li_uri );
482 if ( rs->sr_err != LDAP_SUCCESS ) {
486 /* Set LDAP version. This will always succeed: If the client
487 * bound with a particular version, then so can we.
489 if ( li->li_version != 0 ) {
490 version = li->li_version;
492 } else if ( op->o_protocol != 0 ) {
493 version = op->o_protocol;
496 /* assume it's an internal op; set to LDAPv3 */
497 version = LDAP_VERSION3;
499 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
501 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
502 ldap_set_option( ld, LDAP_OPT_REFERRALS,
503 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
505 if ( li->li_network_timeout > 0 ) {
508 tv.tv_sec = li->li_network_timeout;
510 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
514 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
515 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
516 if ( rs->sr_err != LDAP_SUCCESS ) {
517 ldap_unbind_ext( ld, NULL, NULL );
520 #endif /* HAVE_TLS */
522 if ( *lcp == NULL ) {
523 *lcp = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
524 (*lcp)->lc_flags= li->li_flags;
527 (*lcp)->lc_refcnt = 1;
528 (*lcp)->lc_binding = 1;
531 LDAP_BACK_CONN_ISTLS_SET( *lcp );
533 LDAP_BACK_CONN_ISTLS_CLEAR( *lcp );
535 #endif /* HAVE_TLS */
538 if ( rs->sr_err != LDAP_SUCCESS ) {
539 rs->sr_err = slap_map_api2result( rs );
540 if ( sendok & LDAP_BACK_SENDERR ) {
541 if ( rs->sr_text == NULL ) {
542 rs->sr_text = "ldap_initialize() failed";
544 send_ldap_result( op, rs );
549 if ( li->li_conn_ttl > 0 ) {
550 (*lcp)->lc_create_time = op->o_time;
558 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
560 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
561 ldapconn_t *lc = NULL,
563 int refcnt = 1, binding = 1;
565 /* Internal searches are privileged and shared. So is root. */
566 if ( op->o_do_not_cache || be_isroot( op ) ) {
567 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
568 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
569 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
572 lc_curr.lc_local_ndn = op->o_ndn;
573 /* Explicit binds must not be shared */
574 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
575 lc_curr.lc_conn = op->o_conn;
578 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
582 /* Explicit Bind requests always get their own conn */
583 if ( !( sendok & LDAP_BACK_BINDING ) ) {
584 /* Searches for a ldapconn in the avl tree */
586 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
588 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
589 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
591 /* Don't reuse connections while they're still binding */
592 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
593 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
594 ldap_pvt_thread_yield();
597 refcnt = ++lc->lc_refcnt;
598 binding = ++lc->lc_binding;
600 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
603 /* Looks like we didn't get a bind. Open a new session... */
605 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
608 if ( sendok & LDAP_BACK_BINDING ) {
609 LDAP_BACK_CONN_BINDING_SET( lc );
611 lc->lc_conn = lc_curr.lc_conn;
612 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
614 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
615 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
616 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
617 LDAP_BACK_CONN_ISPRIV_SET( lc );
620 BER_BVZERO( &lc->lc_cred );
621 BER_BVZERO( &lc->lc_bound_ndn );
623 /* FIXME: if we set lc_bound_ndn = o_ndn
624 * we end up with a bind with DN but no password! */
625 if ( !BER_BVISEMPTY( &op->o_ndn )
626 && SLAP_IS_AUTHZ_BACKEND( op ) )
628 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
634 /* if start TLS failed but it was not mandatory,
635 * check if the non-TLS connection was already
636 * in cache; in case, destroy the newly created
637 * connection and use the existing one */
638 if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
639 && !ldap_tls_inplace( lc->lc_ld ) )
643 lc_curr.lc_conn = LDAP_BACK_PCONN;
644 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
645 tmplc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
646 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
647 if ( tmplc != NULL ) {
648 refcnt = ++tmplc->lc_refcnt;
649 binding = ++tmplc->lc_binding;
650 ldap_back_conn_free( lc );
653 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
655 if ( tmplc != NULL ) {
659 #endif /* HAVE_TLS */
661 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
663 /* Inserts the newly created ldapconn in the avl tree */
664 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
666 assert( lc->lc_refcnt == 1 );
667 assert( lc->lc_binding == 1 );
668 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
669 ldap_back_conndn_cmp, ldap_back_conndn_dup );
671 #if PRINT_CONNTREE > 0
672 myprint( li->li_conninfo.lai_tree );
673 #endif /* PRINT_CONNTREE */
675 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
677 Debug( LDAP_DEBUG_TRACE,
678 "=>ldap_back_getconn: conn %p inserted refcnt=%u binding=%u\n",
679 (void *)lc, refcnt, binding );
681 /* Err could be -1 in case a duplicate ldapconn is inserted */
682 switch ( rs->sr_err ) {
687 if ( !( sendok & LDAP_BACK_BINDING ) ) {
688 /* duplicate: free and try to get the newly created one */
691 /* taint connection, so that it'll be freed when released */
692 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
693 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
694 ldap_back_conndnlc_cmp );
695 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
696 LDAP_BACK_CONN_TAINTED_SET( lc );
700 ldap_back_conn_free( lc );
701 rs->sr_err = LDAP_OTHER;
702 rs->sr_text = "proxy bind collision";
703 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
704 send_ldap_result( op, rs );
711 char buf[ SLAP_TEXT_BUFLEN ];
714 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
715 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
719 /* let it be used, but taint/delete it so that
720 * no-one else can look it up any further */
721 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
722 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
723 ldap_back_conndnlc_cmp );
724 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
725 LDAP_BACK_CONN_TAINTED_SET( lc );
728 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
729 snprintf( buf, sizeof( buf ),
730 "conn %p fetched refcnt=%u binding=%u%s",
731 (void *)lc, refcnt, binding, expiring ? " expiring" : "" );
732 Debug( LDAP_DEBUG_TRACE,
733 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
739 #endif /* HAVE_TLS */
740 if ( li->li_idle_timeout && lc ) {
741 lc->lc_time = op->o_time;
748 ldap_back_release_conn_lock(
754 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
757 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
759 assert( lc->lc_refcnt > 0 );
760 LDAP_BACK_CONN_BINDING_CLEAR( lc );
762 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
763 ldap_back_freeconn( op, lc, 0 );
766 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
773 * Note: as the check for the value of lc->lc_bound was already here, I removed
774 * it from all the callers, and I made the function return the flag, so
775 * it can be used to simplify the check.
777 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
780 ldap_back_dobind_int(
784 ldap_back_send_t sendok,
788 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
793 assert( retries >= 0 );
797 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
800 if ( binding == 0 ) {
801 /* check if already bound */
802 rc = LDAP_BACK_CONN_ISBOUND( lc );
806 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
811 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
812 /* if someone else is about to bind it, give up and retry */
814 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
816 ldap_pvt_thread_yield();
820 /* otherwise this thread will bind it */
821 LDAP_BACK_CONN_BINDING_SET( lc );
826 /* wait for pending operations to finish */
827 /* FIXME: may become a bottleneck! */
828 if ( lc->lc_refcnt != lc->lc_binding ) {
830 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
832 ldap_pvt_thread_yield();
837 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
841 while ( lc->lc_refcnt > 1 ) {
842 ldap_pvt_thread_yield();
843 rc = LDAP_BACK_CONN_ISBOUND( lc );
850 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
852 LDAP_BACK_CONN_BINDING_SET( lc );
854 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
859 * FIXME: we need to let clients use proxyAuthz
860 * otherwise we cannot do symmetric pools of servers;
861 * we have to live with the fact that a user can
862 * authorize itself as any ID that is allowed
863 * by the authzTo directive of the "proxyauthzdn".
866 * NOTE: current Proxy Authorization specification
867 * and implementation do not allow proxy authorization
868 * control to be provided with Bind requests
871 * if no bind took place yet, but the connection is bound
872 * and the "idassert-authcDN" (or other ID) is set,
873 * then bind as the asserting identity and explicitly
874 * add the proxyAuthz control to every operation with the
875 * dn bound to the connection as control value.
876 * This is done also if this is the authrizing backend,
877 * but the "override" flag is given to idassert.
878 * It allows to use SASL bind and yet proxyAuthz users
880 if ( op->o_conn != NULL &&
881 !op->o_do_not_cache &&
882 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
883 ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
885 (void)ldap_back_proxy_authz_bind( lc, op, rs, sendok );
889 #ifdef HAVE_CYRUS_SASL
890 if ( LDAP_BACK_CONN_ISPRIV( lc )
891 && li->li_acl_authmethod == LDAP_AUTH_SASL )
893 void *defaults = NULL;
895 if ( li->li_acl_secprops != NULL ) {
896 rc = ldap_set_option( lc->lc_ld,
897 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops);
899 if ( rc != LDAP_OPT_SUCCESS ) {
900 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
901 "(%s,SECPROPS,\"%s\") failed!\n",
902 li->li_uri, li->li_acl_secprops, 0 );
907 defaults = lutil_sasl_defaults( lc->lc_ld,
908 li->li_acl_sasl_mech.bv_val,
909 li->li_acl_sasl_realm.bv_val,
910 li->li_acl_authcID.bv_val,
911 li->li_acl_passwd.bv_val,
914 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
915 li->li_acl_authcDN.bv_val,
916 li->li_acl_sasl_mech.bv_val, NULL, NULL,
917 LDAP_SASL_QUIET, lutil_sasl_interact,
920 lutil_sasl_freedefs( defaults );
922 rs->sr_err = slap_map_api2result( rs );
923 if ( rs->sr_err != LDAP_SUCCESS ) {
924 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
925 send_ldap_result( op, rs );
928 LDAP_BACK_CONN_ISBOUND_SET( lc );
932 #endif /* HAVE_CYRUS_SASL */
935 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
936 lc->lc_bound_ndn.bv_val,
937 LDAP_SASL_SIMPLE, &lc->lc_cred,
938 NULL, NULL, &msgid );
940 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
941 if ( retries != LDAP_BACK_RETRY_NEVER ) {
943 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
946 assert( lc->lc_refcnt > 0 );
947 if ( lc->lc_refcnt == 1 ) {
948 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
951 /* lc here must be the regular lc, reset and ready for init */
952 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
953 if ( rs->sr_err != LDAP_SUCCESS ) {
960 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
963 if ( rs->sr_err == LDAP_SUCCESS ) {
972 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
976 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
980 ldap_back_freeconn( op, lc, dolock );
981 rs->sr_err = slap_map_api2result( rs );
986 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
987 if ( rc == LDAP_SUCCESS ) {
988 LDAP_BACK_CONN_ISBOUND_SET( lc );
993 LDAP_BACK_CONN_BINDING_CLEAR( lc );
994 rc = LDAP_BACK_CONN_ISBOUND( lc );
996 ldap_back_release_conn_lock( op, rs, lc, dolock );
1003 ldap_back_dobind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1005 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1007 return ldap_back_dobind_int( lc, op, rs, sendok, li->li_nretries, 1 );
1011 * ldap_back_default_rebind
1013 * This is a callback used for chasing referrals using the same
1014 * credentials as the original user on this session.
1017 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1018 ber_int_t msgid, void *params )
1020 ldapconn_t *lc = (ldapconn_t *)params;
1023 /* ... otherwise we couldn't get here */
1024 assert( lc != NULL );
1026 if ( !ldap_tls_inplace( ld ) ) {
1027 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1029 const char *text = NULL;
1031 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1032 LDAP_BACK_RETRY_DEFAULT, &text );
1033 if ( rc != LDAP_SUCCESS ) {
1037 #endif /* HAVE_TLS */
1039 /* FIXME: add checks on the URL/identity? */
1041 return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
1042 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1046 ldap_back_op_result(
1052 ldap_back_send_t sendok )
1055 LDAPMessage *res = NULL;
1058 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
1061 rs->sr_matched = NULL;
1063 /* if the error recorded in the reply corresponds
1064 * to a successful state, get the error from the
1065 * remote server response */
1066 if ( ERR_OK( rs->sr_err ) ) {
1071 tv.tv_sec = timeout;
1075 LDAP_BACK_TV_SET( &tv );
1079 /* if result parsing fails, note the failure reason */
1080 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1084 (void)ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1085 rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
1086 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
1087 rs->sr_text = "Operation timed out";
1091 LDAP_BACK_TV_SET( &tv );
1092 ldap_pvt_thread_yield();
1096 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1101 /* otherwise get the result; if it is not
1102 * LDAP_SUCCESS, record it in the reply
1103 * structure (this includes
1104 * LDAP_COMPARE_{TRUE|FALSE}) */
1106 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1107 &match, &text, NULL, NULL, 1 );
1109 if ( rc != LDAP_SUCCESS ) {
1115 /* if the error in the reply structure is not
1116 * LDAP_SUCCESS, try to map it from client
1117 * to server error */
1118 if ( !ERR_OK( rs->sr_err ) ) {
1119 rs->sr_err = slap_map_api2result( rs );
1121 /* internal ops ( op->o_conn == NULL )
1122 * must not reply to client */
1123 if ( op->o_conn && !op->o_do_not_cache && match ) {
1125 /* record the (massaged) matched
1126 * DN into the reply structure */
1127 rs->sr_matched = match;
1131 ( ( sendok & LDAP_BACK_SENDOK )
1132 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1134 send_ldap_result( op, rs );
1137 if ( rs->sr_matched != match ) {
1138 free( (char *)rs->sr_matched );
1140 rs->sr_matched = NULL;
1141 ldap_memfree( match );
1144 ldap_memfree( text );
1147 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1150 /* return true if bound, false if failed */
1152 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1155 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1157 assert( lcp != NULL );
1158 assert( *lcp != NULL );
1160 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1162 if ( (*lcp)->lc_refcnt == 1 ) {
1163 Debug( LDAP_DEBUG_ANY,
1164 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1165 op->o_log_prefix, li->li_uri,
1166 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1167 "" : (*lcp)->lc_bound_ndn.bv_val );
1169 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1170 (*lcp)->lc_ld = NULL;
1171 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1173 /* lc here must be the regular lc, reset and ready for init */
1174 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
1175 if ( rc != LDAP_SUCCESS ) {
1180 rc = ldap_back_dobind_int( *lcp, op, rs, sendok, 0, 0 );
1187 Debug( LDAP_DEBUG_TRACE,
1188 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1189 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1191 ldap_back_release_conn_lock( op, rs, *lcp, 0 );
1195 rs->sr_err = LDAP_UNAVAILABLE;
1196 rs->sr_text = "unable to retry";
1197 send_ldap_result( op, rs );
1201 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1207 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1209 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1210 struct berval binddn = slap_empty_bv;
1211 struct berval bindcred = slap_empty_bv;
1217 /* don't proxyAuthz if protocol is not LDAPv3 */
1218 switch ( li->li_version ) {
1223 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1232 if ( op->o_tag == LDAP_REQ_BIND ) {
1233 ndn = op->o_req_ndn;
1235 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1236 ndn = op->o_conn->c_ndn;
1243 * FIXME: we need to let clients use proxyAuthz
1244 * otherwise we cannot do symmetric pools of servers;
1245 * we have to live with the fact that a user can
1246 * authorize itself as any ID that is allowed
1247 * by the authzTo directive of the "proxyauthzdn".
1250 * NOTE: current Proxy Authorization specification
1251 * and implementation do not allow proxy authorization
1252 * control to be provided with Bind requests
1255 * if no bind took place yet, but the connection is bound
1256 * and the "proxyauthzdn" is set, then bind as
1257 * "proxyauthzdn" and explicitly add the proxyAuthz
1258 * control to every operation with the dn bound
1259 * to the connection as control value.
1262 /* bind as proxyauthzdn only if no idassert mode
1263 * is requested, or if the client's identity
1265 switch ( li->li_idassert_mode ) {
1266 case LDAP_BACK_IDASSERT_LEGACY:
1267 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1268 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1270 binddn = li->li_idassert_authcDN;
1271 bindcred = li->li_idassert_passwd;
1278 /* NOTE: rootdn can always idassert */
1279 if ( BER_BVISNULL( &ndn ) && li->li_idassert_authz == NULL ) {
1280 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1281 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1282 if ( sendok & LDAP_BACK_SENDERR ) {
1283 send_ldap_result( op, rs );
1285 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1288 rs->sr_err = LDAP_SUCCESS;
1289 binddn = slap_empty_bv;
1290 bindcred = slap_empty_bv;
1296 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1297 struct berval authcDN;
1299 if ( BER_BVISNULL( &ndn ) ) {
1300 authcDN = slap_empty_bv;
1305 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1306 &authcDN, &authcDN );
1307 if ( rs->sr_err != LDAP_SUCCESS ) {
1308 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1309 if ( sendok & LDAP_BACK_SENDERR ) {
1310 send_ldap_result( op, rs );
1312 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1315 rs->sr_err = LDAP_SUCCESS;
1316 binddn = slap_empty_bv;
1317 bindcred = slap_empty_bv;
1325 binddn = li->li_idassert_authcDN;
1326 bindcred = li->li_idassert_passwd;
1331 if ( dobind && li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1332 #ifdef HAVE_CYRUS_SASL
1333 void *defaults = NULL;
1334 struct berval authzID = BER_BVNULL;
1337 /* if SASL supports native authz, prepare for it */
1338 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1339 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1341 switch ( li->li_idassert_mode ) {
1342 case LDAP_BACK_IDASSERT_OTHERID:
1343 case LDAP_BACK_IDASSERT_OTHERDN:
1344 authzID = li->li_idassert_authzID;
1347 case LDAP_BACK_IDASSERT_ANONYMOUS:
1348 BER_BVSTR( &authzID, "dn:" );
1351 case LDAP_BACK_IDASSERT_SELF:
1352 if ( BER_BVISNULL( &ndn ) ) {
1353 /* connection is not authc'd, so don't idassert */
1354 BER_BVSTR( &authzID, "dn:" );
1357 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1358 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1359 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1360 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1361 ndn.bv_val, ndn.bv_len + 1 );
1370 if ( li->li_idassert_secprops != NULL ) {
1371 rs->sr_err = ldap_set_option( lc->lc_ld,
1372 LDAP_OPT_X_SASL_SECPROPS,
1373 (void *)li->li_idassert_secprops );
1375 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1376 rs->sr_err = LDAP_OTHER;
1377 if ( sendok & LDAP_BACK_SENDERR ) {
1378 send_ldap_result( op, rs );
1380 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1385 defaults = lutil_sasl_defaults( lc->lc_ld,
1386 li->li_idassert_sasl_mech.bv_val,
1387 li->li_idassert_sasl_realm.bv_val,
1388 li->li_idassert_authcID.bv_val,
1389 li->li_idassert_passwd.bv_val,
1392 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1393 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1394 LDAP_SASL_QUIET, lutil_sasl_interact,
1397 rs->sr_err = slap_map_api2result( rs );
1398 if ( rs->sr_err != LDAP_SUCCESS ) {
1399 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1400 if ( sendok & LDAP_BACK_SENDERR ) {
1401 send_ldap_result( op, rs );
1405 LDAP_BACK_CONN_ISBOUND_SET( lc );
1408 lutil_sasl_freedefs( defaults );
1410 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1414 #endif /* HAVE_CYRUS_SASL */
1417 switch ( li->li_idassert_authmethod ) {
1418 case LDAP_AUTH_NONE:
1419 LDAP_BACK_CONN_ISBOUND_SET( lc );
1422 case LDAP_AUTH_SIMPLE:
1423 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1424 binddn.bv_val, LDAP_SASL_SIMPLE,
1425 &bindcred, NULL, NULL, &msgid );
1430 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1431 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1432 if ( sendok & LDAP_BACK_SENDERR ) {
1433 send_ldap_result( op, rs );
1438 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
1439 if ( rc == LDAP_SUCCESS ) {
1440 LDAP_BACK_CONN_ISBOUND_SET( lc );
1443 return LDAP_BACK_CONN_ISBOUND( lc );
1447 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1448 * to existing server-side controls if required; if not,
1449 * the existing server-side controls are placed in *pctrls.
1450 * The caller, after using the controls in client API
1451 * operations, if ( *pctrls != op->o_ctrls ), should
1452 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1453 * The function returns success if the control could
1454 * be added if required, or if it did nothing; in the future,
1455 * it might return some error if it failed.
1457 * if no bind took place yet, but the connection is bound
1458 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1459 * and explicitly add proxyAuthz the control to every operation
1460 * with the dn bound to the connection as control value.
1462 * If no server-side controls are defined for the operation,
1463 * simply add the proxyAuthz control; otherwise, if the
1464 * proxyAuthz control is not already set, add it as
1467 * FIXME: is controls order significant for security?
1468 * ANSWER: controls ordering and interoperability
1469 * must be indicated by the specs of each control; if none
1470 * is specified, the order is irrelevant.
1473 ldap_back_proxy_authz_ctrl(
1477 LDAPControl ***pctrls )
1479 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
1480 LDAPControl **ctrls = NULL;
1483 struct berval assertedID,
1488 rs->sr_err = LDAP_SUCCESS;
1490 /* don't proxyAuthz if protocol is not LDAPv3 */
1491 switch ( li->li_version ) {
1496 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1505 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1506 * but if it is not set this test fails. We need a different
1507 * means to detect if idassert is enabled */
1508 if ( ( BER_BVISNULL( &li->li_idassert_authcID ) || BER_BVISEMPTY( &li->li_idassert_authcID ) )
1509 && ( BER_BVISNULL( &li->li_idassert_authcDN ) || BER_BVISEMPTY( &li->li_idassert_authcDN ) ) )
1514 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1518 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1519 ndn = op->o_conn->c_ndn;
1525 if ( li->li_idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1526 if ( op->o_proxy_authz ) {
1528 * FIXME: we do not want to perform proxyAuthz
1529 * on behalf of the client, because this would
1530 * be performed with "proxyauthzdn" privileges.
1532 * This might actually be too strict, since
1533 * the "proxyauthzdn" authzTo, and each entry's
1534 * authzFrom attributes may be crafted
1535 * to avoid unwanted proxyAuthz to take place.
1538 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1539 rs->sr_text = "proxyAuthz not allowed within namingContext";
1544 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1548 if ( BER_BVISNULL( &ndn ) ) {
1552 if ( BER_BVISNULL( &li->li_idassert_authcDN ) ) {
1556 } else if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1557 if ( ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1558 /* && ( !BER_BVISNULL( &ndn )
1559 || LDAP_BACK_CONN_ISBOUND( lc ) ) */ )
1561 /* already asserted in SASL via native authz */
1562 /* NOTE: the test on lc->lc_bound is used to trap
1563 * native authorization of anonymous users,
1564 * since in that case ndn is NULL */
1568 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1570 struct berval authcDN;
1572 if ( BER_BVISNULL( &ndn ) ) {
1573 authcDN = slap_empty_bv;
1577 rc = slap_sasl_matches( op, li->li_idassert_authz,
1578 &authcDN, & authcDN );
1579 if ( rc != LDAP_SUCCESS ) {
1580 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1582 /* ndn is not authorized
1583 * to use idassert */
1590 if ( op->o_proxy_authz ) {
1593 * 1) ignore the already set proxyAuthz control
1594 * 2) leave it in place, and don't set ours
1596 * 4) reject the operation
1598 * option (4) is very drastic
1599 * option (3) will make the remote server reject
1600 * the operation, thus being equivalent to (4)
1601 * option (2) will likely break the idassert
1602 * assumptions, so we cannot accept it;
1603 * option (1) means that we are contradicting
1604 * the client's reques.
1606 * I think (4) is the only correct choice.
1608 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1609 rs->sr_text = "proxyAuthz not allowed within namingContext";
1612 if ( op->o_is_auth_check ) {
1613 mode = LDAP_BACK_IDASSERT_NOASSERT;
1616 mode = li->li_idassert_mode;
1620 case LDAP_BACK_IDASSERT_SELF:
1621 if ( BER_BVISNULL( &ndn ) ) {
1627 case LDAP_BACK_IDASSERT_LEGACY:
1628 /* original behavior:
1629 * assert the client's identity */
1630 if ( BER_BVISNULL( &ndn ) ) {
1631 assertedID = slap_empty_bv;
1637 case LDAP_BACK_IDASSERT_ANONYMOUS:
1638 /* assert "anonymous" */
1639 assertedID = slap_empty_bv;
1642 case LDAP_BACK_IDASSERT_NOASSERT:
1643 /* don't assert; bind as proxyauthzdn */
1646 case LDAP_BACK_IDASSERT_OTHERID:
1647 case LDAP_BACK_IDASSERT_OTHERDN:
1648 /* assert idassert DN */
1649 assertedID = li->li_idassert_authzID;
1656 if ( BER_BVISNULL( &assertedID ) ) {
1657 assertedID = slap_empty_bv;
1660 /* don't idassert the bound DN (ITS#4497) */
1661 if ( dn_match( &assertedID, &lc->lc_bound_ndn ) ) {
1665 if ( op->o_ctrls ) {
1666 for ( i = 0; op->o_ctrls[ i ]; i++ )
1667 /* just count ctrls */ ;
1670 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
1672 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
1674 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1675 ctrls[ 0 ]->ldctl_iscritical = 1;
1677 switch ( li->li_idassert_mode ) {
1678 /* already in u:ID or dn:DN form */
1679 case LDAP_BACK_IDASSERT_OTHERID:
1680 case LDAP_BACK_IDASSERT_OTHERDN:
1681 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
1684 /* needs the dn: prefix */
1686 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1687 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
1689 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1690 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1691 assertedID.bv_val, assertedID.bv_len + 1 );
1695 /* Older versions of <draft-weltman-ldapv3-proxy> required
1696 * to encode the value of the authzID (and called it proxyDN);
1697 * this hack provides compatibility with those DSAs that
1698 * implement it this way */
1699 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
1700 struct berval authzID = ctrls[ 0 ]->ldctl_value;
1701 BerElementBuffer berbuf;
1702 BerElement *ber = (BerElement *)&berbuf;
1705 ber_init2( ber, 0, LBER_USE_DER );
1706 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1708 tag = ber_printf( ber, "O", &authzID );
1709 if ( tag == LBER_ERROR ) {
1710 rs->sr_err = LDAP_OTHER;
1714 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
1715 rs->sr_err = LDAP_OTHER;
1720 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
1721 ber_free_buf( ber );
1723 if ( rs->sr_err != LDAP_SUCCESS ) {
1724 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1729 } else if ( li->li_idassert_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
1730 struct berval authzID = ctrls[ 0 ]->ldctl_value,
1732 BerElementBuffer berbuf;
1733 BerElement *ber = (BerElement *)&berbuf;
1736 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
1737 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1738 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1740 rs->sr_err = LDAP_PROTOCOL_ERROR;
1745 tmp.bv_val += STRLENOF( "dn:" );
1746 tmp.bv_len -= STRLENOF( "dn:" );
1748 ber_init2( ber, 0, LBER_USE_DER );
1749 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1751 /* apparently, Mozilla API encodes this
1752 * as "SEQUENCE { LDAPDN }" */
1753 tag = ber_printf( ber, "{O}", &tmp );
1754 if ( tag == LBER_ERROR ) {
1755 rs->sr_err = LDAP_OTHER;
1759 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
1760 rs->sr_err = LDAP_OTHER;
1765 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
1766 ber_free_buf( ber );
1768 if ( rs->sr_err != LDAP_SUCCESS ) {
1769 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1774 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1777 if ( op->o_ctrls ) {
1778 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1779 ctrls[ i + 1 ] = op->o_ctrls[ i ];
1782 ctrls[ i + 1 ] = NULL;
1785 if ( ctrls == NULL ) {
1786 ctrls = op->o_ctrls;
1795 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1797 LDAPControl **ctrls = *pctrls;
1799 /* we assume that the first control is the proxyAuthz
1800 * added by back-ldap, so it's the only one we explicitly
1802 if ( ctrls && ctrls != op->o_ctrls ) {
1803 assert( ctrls[ 0 ] != NULL );
1805 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1806 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1809 op->o_tmpfree( ctrls, op->o_tmpmemctx );