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 #if PRINT_CONNTREE > 0
46 ravl_print( Avlnode *root, int depth )
55 ravl_print( root->avl_right, depth+1 );
57 for ( i = 0; i < depth; i++ ) {
58 fprintf( stderr, "-" );
62 fprintf( stderr, "lc=%p local=\"%s\" conn=%p %s refcnt=%d\n",
64 lc->lc_local_ndn.bv_val ? lc->lc_local_ndn.bv_val : "",
66 avl_bf2str( root->avl_bf ), lc->lc_refcnt );
68 ravl_print( root->avl_left, depth+1 );
72 myprint( Avlnode *root, char *msg )
74 fprintf( stderr, "========> %s\n", msg );
77 fprintf( stderr, "\tNULL\n" );
80 ravl_print( root, 0 );
83 fprintf( stderr, "<======== %s\n", msg );
85 #endif /* PRINT_CONNTREE */
88 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
91 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
94 ldap_back_conndnlc_cmp( const void *c1, const void *c2 );
97 ldap_back_bind( Operation *op, SlapReply *rs )
99 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
105 lc = ldap_back_getconn( op, rs, LDAP_BACK_BIND_SERR );
110 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
111 ch_free( lc->lc_bound_ndn.bv_val );
112 BER_BVZERO( &lc->lc_bound_ndn );
114 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
116 /* method is always LDAP_AUTH_SIMPLE if we got here */
117 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
119 &op->orb_cred, op->o_ctrls, NULL, &msgid );
120 rc = ldap_back_op_result( lc, op, rs, msgid, 0, LDAP_BACK_SENDERR );
122 if ( rc == LDAP_SUCCESS ) {
123 /* If defined, proxyAuthz will be used also when
124 * back-ldap is the authorizing backend; for this
125 * purpose, a successful bind is followed by a
126 * bind with the configured identity assertion */
127 /* NOTE: use with care */
128 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
129 ldap_back_proxy_authz_bind( lc, op, rs, LDAP_BACK_SENDERR );
130 if ( !LDAP_BACK_CONN_ISBOUND( lc ) ) {
136 /* rebind is now done inside ldap_back_proxy_authz_bind()
137 * in case of success */
138 LDAP_BACK_CONN_ISBOUND_SET( lc );
139 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
141 if ( LDAP_BACK_SAVECRED( li ) ) {
142 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
143 memset( lc->lc_cred.bv_val, 0,
144 lc->lc_cred.bv_len );
146 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
147 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
152 assert( lc->lc_binding == 1 );
155 /* must re-insert if local DN changed as result of bind */
156 if ( !LDAP_BACK_CONN_ISBOUND( lc )
157 || ( LDAP_BACK_CONN_ISBOUND( lc )
158 && !dn_match( &op->o_req_ndn, &lc->lc_local_ndn ) ) )
163 /* wait for all other ops to release the connection */
165 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
166 if ( lc->lc_refcnt > 1 ) {
167 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
168 ldap_pvt_thread_yield();
172 assert( lc->lc_refcnt == 1 );
173 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
174 ldap_back_conndnlc_cmp );
175 assert( tmplc == NULL || lc == tmplc );
177 /* delete all cached connections with the current connection */
178 if ( LDAP_BACK_SINGLECONN( li ) ) {
179 while ( ( tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc, ldap_back_conn_cmp ) ) != NULL )
181 Debug( LDAP_DEBUG_TRACE,
182 "=>ldap_back_bind: destroying conn %ld (refcnt=%u)\n",
183 LDAP_BACK_PCONN_ID( lc->lc_conn ), lc->lc_refcnt, 0 );
185 if ( lc->lc_refcnt != 0 ) {
187 LDAP_BACK_CONN_TAINTED_SET( tmplc );
191 * Needs a test because the handler may be corrupted,
192 * and calling ldap_unbind on a corrupted header results
193 * in a segmentation fault
195 ldap_back_conn_free( tmplc );
200 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
201 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
202 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
203 lc->lc_conn = LDAP_BACK_PCONN_SET( op );
205 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
206 ldap_back_conndn_cmp, ldap_back_conndn_dup );
209 #if PRINT_CONNTREE > 0
210 myprint( li->li_conninfo.lai_tree, "ldap_back_bind" );
211 #endif /* PRINT_CONNTREE */
213 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
219 /* duplicate; someone else successfully bound
220 * on the same connection with the same identity;
221 * we can do this because lc_refcnt == 1 */
222 ldap_back_conn_free( lc );
228 ldap_back_release_conn( op, rs, lc );
235 * ldap_back_conndn_cmp
237 * compares two ldapconn_t based on the value of the conn pointer
238 * and of the local DN; used by avl stuff for insert, lookup
242 ldap_back_conndn_cmp( const void *c1, const void *c2 )
244 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
245 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
248 /* If local DNs don't match, it is definitely not a match */
249 /* For shared sessions, conn is NULL. Only explicitly
250 * bound sessions will have non-NULL conn.
252 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
254 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
261 * ldap_back_conndnlc_cmp
263 * compares two ldapconn_t based on the value of the conn pointer,
264 * the local DN and the lc pointer; used by avl stuff for insert, lookup
268 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
270 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
271 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
274 /* If local DNs don't match, it is definitely not a match */
275 /* For shared sessions, conn is NULL. Only explicitly
276 * bound sessions will have non-NULL conn.
278 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
280 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
282 rc = SLAP_PTRCMP( lc1, lc2 );
292 * compares two ldapconn_t based on the value of the conn pointer;
293 * used by avl stuff for delete of all conns with the same connid
296 ldap_back_conn_cmp( const void *c1, const void *c2 )
298 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
299 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
301 /* For shared sessions, conn is NULL. Only explicitly
302 * bound sessions will have non-NULL conn.
304 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
308 * ldap_back_conndn_dup
310 * returns -1 in case a duplicate ldapconn_t has been inserted;
314 ldap_back_conndn_dup( void *c1, void *c2 )
316 ldapconn_t *lc1 = (ldapconn_t *)c1;
317 ldapconn_t *lc2 = (ldapconn_t *)c2;
319 /* Cannot have more than one shared session with same DN */
320 if ( lc1->lc_conn == lc2->lc_conn &&
321 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
330 ldap_back_freeconn( Operation *op, ldapconn_t *lc, int dolock )
332 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
336 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
339 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
340 ldap_back_conndnlc_cmp );
341 assert( LDAP_BACK_CONN_TAINTED( lc ) || tmplc == lc );
342 if ( lc->lc_refcnt == 0 ) {
343 ldap_back_conn_free( (void *)lc );
347 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
364 int rc = LDAP_SUCCESS;
367 /* this is ridiculous... */
368 dummy.li_flags = flags;
370 /* start TLS ("tls-[try-]{start,propagate}" statements) */
371 if ( ( LDAP_BACK_USE_TLS( &dummy ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS( &dummy ) ) )
372 && !ldap_is_ldaps_url( url ) )
374 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
376 * use asynchronous StartTLS
377 * in case, chase referral (not implemented yet)
381 if ( protocol == 0 ) {
382 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
386 if ( protocol < LDAP_VERSION3 ) {
387 /* we should rather bail out... */
388 rc = LDAP_UNWILLING_TO_PERFORM;
389 *text = "invalid protocol version";
392 if ( rc == LDAP_SUCCESS ) {
393 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
396 if ( rc == LDAP_SUCCESS ) {
397 LDAPMessage *res = NULL;
400 LDAP_BACK_TV_SET( &tv );
403 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
405 rc = LDAP_UNAVAILABLE;
407 } else if ( rc == 0 ) {
408 if ( retries != LDAP_BACK_RETRY_NEVER ) {
409 ldap_pvt_thread_yield();
413 LDAP_BACK_TV_SET( &tv );
416 rc = LDAP_UNAVAILABLE;
418 } else if ( rc == LDAP_RES_EXTENDED ) {
419 struct berval *data = NULL;
421 rc = ldap_parse_extended_result( ld, res,
423 if ( rc == LDAP_SUCCESS ) {
425 rc = ldap_parse_result( ld, res, &err,
426 NULL, NULL, NULL, NULL, 1 );
427 if ( rc == LDAP_SUCCESS ) {
432 /* FIXME: in case a referral
433 * is returned, should we try
434 * using it instead of the
436 if ( rc == LDAP_SUCCESS ) {
437 rc = ldap_install_tls( ld );
439 } else if ( rc == LDAP_REFERRAL ) {
440 rc = LDAP_UNWILLING_TO_PERFORM;
441 *text = "unwilling to chase referral returned by Start TLS exop";
445 if ( data->bv_val ) {
446 ber_memfree( data->bv_val );
460 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
462 * use synchronous StartTLS
464 rc = ldap_start_tls_s( ld, NULL, NULL );
465 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
467 /* if StartTLS is requested, only attempt it if the URL
468 * is not "ldaps://"; this may occur not only in case
469 * of misconfiguration, but also when used in the chain
470 * overlay, where the "uri" can be parsed out of a referral */
476 case LDAP_SERVER_DOWN:
480 if ( LDAP_BACK_TLS_CRITICAL( &dummy ) ) {
481 *text = "could not start TLS";
485 /* in case Start TLS is not critical */
497 #endif /* HAVE_TLS */
500 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
502 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
506 int is_tls = op->o_conn->c_is_tls;
507 #endif /* HAVE_TLS */
509 assert( lcp != NULL );
511 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
512 rs->sr_err = ldap_initialize( &ld, li->li_uri );
513 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
514 if ( rs->sr_err != LDAP_SUCCESS ) {
518 if ( li->li_urllist_f ) {
519 ldap_set_urllist_proc( ld, li->li_urllist_f, li->li_urllist_p );
522 /* Set LDAP version. This will always succeed: If the client
523 * bound with a particular version, then so can we.
525 if ( li->li_version != 0 ) {
526 version = li->li_version;
528 } else if ( op->o_protocol != 0 ) {
529 version = op->o_protocol;
532 /* assume it's an internal op; set to LDAPv3 */
533 version = LDAP_VERSION3;
535 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
537 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
538 ldap_set_option( ld, LDAP_OPT_REFERRALS,
539 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
541 if ( li->li_network_timeout > 0 ) {
544 tv.tv_sec = li->li_network_timeout;
546 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
550 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
551 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
552 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
553 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
554 if ( rs->sr_err != LDAP_SUCCESS ) {
555 ldap_unbind_ext( ld, NULL, NULL );
558 #endif /* HAVE_TLS */
560 if ( *lcp == NULL ) {
561 *lcp = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
562 (*lcp)->lc_flags = li->li_flags;
565 (*lcp)->lc_refcnt = 1;
566 (*lcp)->lc_binding = 1;
569 LDAP_BACK_CONN_ISTLS_SET( *lcp );
571 LDAP_BACK_CONN_ISTLS_CLEAR( *lcp );
573 #endif /* HAVE_TLS */
576 if ( rs->sr_err != LDAP_SUCCESS ) {
577 rs->sr_err = slap_map_api2result( rs );
578 if ( sendok & LDAP_BACK_SENDERR ) {
579 if ( rs->sr_text == NULL ) {
580 rs->sr_text = "ldap_initialize() failed";
582 send_ldap_result( op, rs );
587 if ( li->li_conn_ttl > 0 ) {
588 (*lcp)->lc_create_time = op->o_time;
596 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
598 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
599 ldapconn_t *lc = NULL,
601 int refcnt = 1, binding = 1;
603 /* Internal searches are privileged and shared. So is root. */
604 if ( op->o_do_not_cache || be_isroot( op ) ) {
605 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
606 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
607 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
610 lc_curr.lc_local_ndn = op->o_ndn;
611 /* Explicit binds must not be shared */
612 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
613 lc_curr.lc_conn = op->o_conn;
616 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
620 /* Explicit Bind requests always get their own conn */
621 if ( !( sendok & LDAP_BACK_BINDING ) ) {
622 /* Searches for a ldapconn in the avl tree */
624 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
626 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
627 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
629 /* Don't reuse connections while they're still binding */
630 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
631 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
632 ldap_pvt_thread_yield();
635 refcnt = ++lc->lc_refcnt;
636 binding = ++lc->lc_binding;
638 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
641 /* Looks like we didn't get a bind. Open a new session... */
643 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
646 if ( sendok & LDAP_BACK_BINDING ) {
647 LDAP_BACK_CONN_BINDING_SET( lc );
649 lc->lc_conn = lc_curr.lc_conn;
650 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
652 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
653 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
654 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
655 LDAP_BACK_CONN_ISPRIV_SET( lc );
658 BER_BVZERO( &lc->lc_cred );
659 BER_BVZERO( &lc->lc_bound_ndn );
660 if ( !BER_BVISEMPTY( &op->o_ndn )
661 && SLAP_IS_AUTHZ_BACKEND( op ) )
663 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
668 /* if start TLS failed but it was not mandatory,
669 * check if the non-TLS connection was already
670 * in cache; in case, destroy the newly created
671 * connection and use the existing one */
672 if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
673 && !ldap_tls_inplace( lc->lc_ld ) )
677 lc_curr.lc_conn = LDAP_BACK_PCONN;
678 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
679 tmplc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
680 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
681 if ( tmplc != NULL ) {
682 refcnt = ++tmplc->lc_refcnt;
683 binding = ++tmplc->lc_binding;
684 ldap_back_conn_free( lc );
687 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
689 if ( tmplc != NULL ) {
693 #endif /* HAVE_TLS */
695 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
697 /* Inserts the newly created ldapconn in the avl tree */
698 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
700 assert( lc->lc_refcnt == 1 );
701 assert( lc->lc_binding == 1 );
702 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
703 ldap_back_conndn_cmp, ldap_back_conndn_dup );
705 #if PRINT_CONNTREE > 0
706 myprint( li->li_conninfo.lai_tree, "ldap_back_getconn" );
707 #endif /* PRINT_CONNTREE */
709 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
711 Debug( LDAP_DEBUG_TRACE,
712 "=>ldap_back_getconn: conn %p inserted refcnt=%u binding=%u\n",
713 (void *)lc, refcnt, binding );
715 /* Err could be -1 in case a duplicate ldapconn is inserted */
716 switch ( rs->sr_err ) {
721 if ( !( sendok & LDAP_BACK_BINDING ) ) {
722 /* duplicate: free and try to get the newly created one */
725 /* taint connection, so that it'll be freed when released */
726 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
727 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
728 ldap_back_conndnlc_cmp );
729 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
730 LDAP_BACK_CONN_TAINTED_SET( lc );
734 ldap_back_conn_free( lc );
735 rs->sr_err = LDAP_OTHER;
736 rs->sr_text = "proxy bind collision";
737 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
738 send_ldap_result( op, rs );
745 char buf[ SLAP_TEXT_BUFLEN ];
748 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
749 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
753 /* let it be used, but taint/delete it so that
754 * no-one else can look it up any further */
755 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
756 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
757 ldap_back_conndnlc_cmp );
758 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
759 LDAP_BACK_CONN_TAINTED_SET( lc );
762 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
763 snprintf( buf, sizeof( buf ),
764 "conn %p fetched refcnt=%u binding=%u%s",
765 (void *)lc, refcnt, binding, expiring ? " expiring" : "" );
766 Debug( LDAP_DEBUG_TRACE,
767 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
774 #endif /* HAVE_TLS */
775 if ( li->li_idle_timeout && lc ) {
776 lc->lc_time = op->o_time;
783 ldap_back_release_conn_lock(
789 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
792 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
794 assert( lc->lc_refcnt > 0 );
795 LDAP_BACK_CONN_BINDING_CLEAR( lc );
797 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
798 ldap_back_freeconn( op, lc, 0 );
801 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
808 * Note: as the check for the value of lc->lc_bound was already here, I removed
809 * it from all the callers, and I made the function return the flag, so
810 * it can be used to simplify the check.
812 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
815 ldap_back_dobind_int(
819 ldap_back_send_t sendok,
823 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
828 assert( retries >= 0 );
832 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
835 if ( binding == 0 ) {
836 /* check if already bound */
837 rc = LDAP_BACK_CONN_ISBOUND( lc );
841 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
846 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
847 /* if someone else is about to bind it, give up and retry */
849 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
851 ldap_pvt_thread_yield();
855 /* otherwise this thread will bind it */
856 LDAP_BACK_CONN_BINDING_SET( lc );
861 /* wait for pending operations to finish */
862 /* FIXME: may become a bottleneck! */
863 if ( lc->lc_refcnt != lc->lc_binding ) {
865 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
867 ldap_pvt_thread_yield();
872 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
876 while ( lc->lc_refcnt > 1 ) {
877 ldap_pvt_thread_yield();
878 rc = LDAP_BACK_CONN_ISBOUND( lc );
885 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
887 LDAP_BACK_CONN_BINDING_SET( lc );
889 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
894 * FIXME: we need to let clients use proxyAuthz
895 * otherwise we cannot do symmetric pools of servers;
896 * we have to live with the fact that a user can
897 * authorize itself as any ID that is allowed
898 * by the authzTo directive of the "proxyauthzdn".
901 * NOTE: current Proxy Authorization specification
902 * and implementation do not allow proxy authorization
903 * control to be provided with Bind requests
906 * if no bind took place yet, but the connection is bound
907 * and the "idassert-authcDN" (or other ID) is set,
908 * then bind as the asserting identity and explicitly
909 * add the proxyAuthz control to every operation with the
910 * dn bound to the connection as control value.
911 * This is done also if this is the authrizing backend,
912 * but the "override" flag is given to idassert.
913 * It allows to use SASL bind and yet proxyAuthz users
915 if ( op->o_conn != NULL &&
916 !op->o_do_not_cache &&
917 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
918 ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
920 (void)ldap_back_proxy_authz_bind( lc, op, rs, sendok );
924 #ifdef HAVE_CYRUS_SASL
925 if ( LDAP_BACK_CONN_ISPRIV( lc )
926 && li->li_acl_authmethod == LDAP_AUTH_SASL )
928 void *defaults = NULL;
930 if ( li->li_acl_secprops != NULL ) {
931 rc = ldap_set_option( lc->lc_ld,
932 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops);
934 if ( rc != LDAP_OPT_SUCCESS ) {
935 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
936 "(SECPROPS,\"%s\") failed!\n",
937 li->li_acl_secprops, 0, 0 );
942 defaults = lutil_sasl_defaults( lc->lc_ld,
943 li->li_acl_sasl_mech.bv_val,
944 li->li_acl_sasl_realm.bv_val,
945 li->li_acl_authcID.bv_val,
946 li->li_acl_passwd.bv_val,
949 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
950 li->li_acl_authcDN.bv_val,
951 li->li_acl_sasl_mech.bv_val, NULL, NULL,
952 LDAP_SASL_QUIET, lutil_sasl_interact,
955 lutil_sasl_freedefs( defaults );
957 rs->sr_err = slap_map_api2result( rs );
958 if ( rs->sr_err != LDAP_SUCCESS ) {
959 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
960 send_ldap_result( op, rs );
963 LDAP_BACK_CONN_ISBOUND_SET( lc );
967 #endif /* HAVE_CYRUS_SASL */
970 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
971 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
972 LDAP_SASL_SIMPLE, &lc->lc_cred,
973 NULL, NULL, &msgid );
975 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
976 if ( retries != LDAP_BACK_RETRY_NEVER ) {
978 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
981 assert( lc->lc_refcnt > 0 );
982 if ( lc->lc_refcnt == 1 ) {
983 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
986 /* lc here must be the regular lc, reset and ready for init */
987 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
988 if ( rs->sr_err != LDAP_SUCCESS ) {
995 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
998 if ( rs->sr_err == LDAP_SUCCESS ) {
1007 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1011 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1016 ldap_back_freeconn( op, lc, dolock );
1017 rs->sr_err = slap_map_api2result( rs );
1022 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
1023 if ( rc == LDAP_SUCCESS ) {
1024 LDAP_BACK_CONN_ISBOUND_SET( lc );
1029 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1030 rc = LDAP_BACK_CONN_ISBOUND( lc );
1032 ldap_back_release_conn_lock( op, rs, lc, dolock );
1039 ldap_back_dobind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1041 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1043 return ldap_back_dobind_int( lc, op, rs, sendok, li->li_nretries, 1 );
1047 * ldap_back_default_rebind
1049 * This is a callback used for chasing referrals using the same
1050 * credentials as the original user on this session.
1053 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1054 ber_int_t msgid, void *params )
1056 ldapconn_t *lc = (ldapconn_t *)params;
1059 /* ... otherwise we couldn't get here */
1060 assert( lc != NULL );
1062 if ( !ldap_tls_inplace( ld ) ) {
1063 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1065 const char *text = NULL;
1067 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1068 LDAP_BACK_RETRY_DEFAULT, &text );
1069 if ( rc != LDAP_SUCCESS ) {
1073 #endif /* HAVE_TLS */
1075 /* FIXME: add checks on the URL/identity? */
1077 return ldap_sasl_bind_s( ld,
1078 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1079 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1083 * ldap_back_default_urllist
1086 ldap_back_default_urllist(
1088 LDAPURLDesc **urllist,
1092 ldapinfo_t *li = (ldapinfo_t *)params;
1093 LDAPURLDesc **urltail;
1095 if ( urllist == url ) {
1096 return LDAP_SUCCESS;
1099 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1102 *urltail = *urllist;
1106 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1108 ch_free( li->li_uri );
1111 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1112 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1114 return LDAP_SUCCESS;
1118 ldap_back_op_result(
1124 ldap_back_send_t sendok )
1127 LDAPMessage *res = NULL;
1130 LDAPControl **ctrls = NULL;
1132 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
1135 rs->sr_matched = NULL;
1137 rs->sr_ctrls = NULL;
1139 /* if the error recorded in the reply corresponds
1140 * to a successful state, get the error from the
1141 * remote server response */
1142 if ( ERR_OK( rs->sr_err ) ) {
1147 tv.tv_sec = timeout;
1151 LDAP_BACK_TV_SET( &tv );
1155 /* if result parsing fails, note the failure reason */
1156 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1160 (void)ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1161 rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
1162 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
1163 rs->sr_text = "Operation timed out";
1167 LDAP_BACK_TV_SET( &tv );
1168 ldap_pvt_thread_yield();
1172 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1177 /* otherwise get the result; if it is not
1178 * LDAP_SUCCESS, record it in the reply
1179 * structure (this includes
1180 * LDAP_COMPARE_{TRUE|FALSE}) */
1182 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1183 &match, &text, &refs, &ctrls, 1 );
1185 if ( rc != LDAP_SUCCESS ) {
1188 if ( refs != NULL ) {
1191 for ( i = 0; refs[ i ] != NULL; i++ )
1193 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1195 for ( i = 0; refs[ i ] != NULL; i++ ) {
1196 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1198 BER_BVZERO( &rs->sr_ref[ i ] );
1200 if ( ctrls != NULL ) {
1201 rs->sr_ctrls = ctrls;
1206 /* if the error in the reply structure is not
1207 * LDAP_SUCCESS, try to map it from client
1208 * to server error */
1209 if ( !ERR_OK( rs->sr_err ) ) {
1210 rs->sr_err = slap_map_api2result( rs );
1212 /* internal ops ( op->o_conn == NULL )
1213 * must not reply to client */
1214 if ( op->o_conn && !op->o_do_not_cache && match ) {
1216 /* record the (massaged) matched
1217 * DN into the reply structure */
1218 rs->sr_matched = match;
1222 ( ( sendok & LDAP_BACK_SENDOK )
1223 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1225 send_ldap_result( op, rs );
1228 if ( rs->sr_matched != match ) {
1229 free( (char *)rs->sr_matched );
1231 rs->sr_matched = NULL;
1232 ldap_memfree( match );
1235 ldap_memfree( text );
1239 assert( refs != NULL );
1240 ber_memvfree( (void **)refs );
1241 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1245 assert( rs->sr_ctrls != NULL );
1246 ldap_controls_free( ctrls );
1247 rs->sr_ctrls = NULL;
1249 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1252 /* return true if bound, false if failed */
1254 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1257 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1259 assert( lcp != NULL );
1260 assert( *lcp != NULL );
1262 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1264 if ( (*lcp)->lc_refcnt == 1 ) {
1265 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1266 Debug( LDAP_DEBUG_ANY,
1267 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1268 op->o_log_prefix, li->li_uri,
1269 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1270 "" : (*lcp)->lc_bound_ndn.bv_val );
1271 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1273 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1274 (*lcp)->lc_ld = NULL;
1275 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1277 /* lc here must be the regular lc, reset and ready for init */
1278 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
1279 if ( rc != LDAP_SUCCESS ) {
1284 rc = ldap_back_dobind_int( *lcp, op, rs, sendok, 0, 0 );
1291 Debug( LDAP_DEBUG_TRACE,
1292 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1293 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1295 ldap_back_release_conn_lock( op, rs, *lcp, 0 );
1299 rs->sr_err = LDAP_UNAVAILABLE;
1300 rs->sr_text = "unable to retry";
1301 send_ldap_result( op, rs );
1305 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1311 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1313 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1314 struct berval binddn = slap_empty_bv;
1315 struct berval bindcred = slap_empty_bv;
1321 /* don't proxyAuthz if protocol is not LDAPv3 */
1322 switch ( li->li_version ) {
1327 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1336 if ( op->o_tag == LDAP_REQ_BIND ) {
1337 ndn = op->o_req_ndn;
1339 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1340 ndn = op->o_conn->c_ndn;
1347 * FIXME: we need to let clients use proxyAuthz
1348 * otherwise we cannot do symmetric pools of servers;
1349 * we have to live with the fact that a user can
1350 * authorize itself as any ID that is allowed
1351 * by the authzTo directive of the "proxyauthzdn".
1354 * NOTE: current Proxy Authorization specification
1355 * and implementation do not allow proxy authorization
1356 * control to be provided with Bind requests
1359 * if no bind took place yet, but the connection is bound
1360 * and the "proxyauthzdn" is set, then bind as
1361 * "proxyauthzdn" and explicitly add the proxyAuthz
1362 * control to every operation with the dn bound
1363 * to the connection as control value.
1366 /* bind as proxyauthzdn only if no idassert mode
1367 * is requested, or if the client's identity
1369 switch ( li->li_idassert_mode ) {
1370 case LDAP_BACK_IDASSERT_LEGACY:
1371 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1372 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1374 binddn = li->li_idassert_authcDN;
1375 bindcred = li->li_idassert_passwd;
1382 /* NOTE: rootdn can always idassert */
1383 if ( BER_BVISNULL( &ndn ) && li->li_idassert_authz == NULL ) {
1384 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1385 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1386 if ( sendok & LDAP_BACK_SENDERR ) {
1387 send_ldap_result( op, rs );
1389 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1392 rs->sr_err = LDAP_SUCCESS;
1393 binddn = slap_empty_bv;
1394 bindcred = slap_empty_bv;
1400 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1401 struct berval authcDN;
1403 if ( BER_BVISNULL( &ndn ) ) {
1404 authcDN = slap_empty_bv;
1409 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1410 &authcDN, &authcDN );
1411 if ( rs->sr_err != LDAP_SUCCESS ) {
1412 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1413 if ( sendok & LDAP_BACK_SENDERR ) {
1414 send_ldap_result( op, rs );
1416 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1419 rs->sr_err = LDAP_SUCCESS;
1420 binddn = slap_empty_bv;
1421 bindcred = slap_empty_bv;
1429 binddn = li->li_idassert_authcDN;
1430 bindcred = li->li_idassert_passwd;
1435 if ( dobind && li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1436 #ifdef HAVE_CYRUS_SASL
1437 void *defaults = NULL;
1438 struct berval authzID = BER_BVNULL;
1441 /* if SASL supports native authz, prepare for it */
1442 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1443 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1445 switch ( li->li_idassert_mode ) {
1446 case LDAP_BACK_IDASSERT_OTHERID:
1447 case LDAP_BACK_IDASSERT_OTHERDN:
1448 authzID = li->li_idassert_authzID;
1451 case LDAP_BACK_IDASSERT_ANONYMOUS:
1452 BER_BVSTR( &authzID, "dn:" );
1455 case LDAP_BACK_IDASSERT_SELF:
1456 if ( BER_BVISNULL( &ndn ) ) {
1457 /* connection is not authc'd, so don't idassert */
1458 BER_BVSTR( &authzID, "dn:" );
1461 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1462 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1463 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1464 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1465 ndn.bv_val, ndn.bv_len + 1 );
1474 if ( li->li_idassert_secprops != NULL ) {
1475 rs->sr_err = ldap_set_option( lc->lc_ld,
1476 LDAP_OPT_X_SASL_SECPROPS,
1477 (void *)li->li_idassert_secprops );
1479 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1480 rs->sr_err = LDAP_OTHER;
1481 if ( sendok & LDAP_BACK_SENDERR ) {
1482 send_ldap_result( op, rs );
1484 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1489 defaults = lutil_sasl_defaults( lc->lc_ld,
1490 li->li_idassert_sasl_mech.bv_val,
1491 li->li_idassert_sasl_realm.bv_val,
1492 li->li_idassert_authcID.bv_val,
1493 li->li_idassert_passwd.bv_val,
1496 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1497 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1498 LDAP_SASL_QUIET, lutil_sasl_interact,
1501 rs->sr_err = slap_map_api2result( rs );
1502 if ( rs->sr_err != LDAP_SUCCESS ) {
1503 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1504 if ( sendok & LDAP_BACK_SENDERR ) {
1505 send_ldap_result( op, rs );
1509 LDAP_BACK_CONN_ISBOUND_SET( lc );
1512 lutil_sasl_freedefs( defaults );
1514 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1518 #endif /* HAVE_CYRUS_SASL */
1521 switch ( li->li_idassert_authmethod ) {
1522 case LDAP_AUTH_NONE:
1526 case LDAP_AUTH_SIMPLE:
1527 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1528 binddn.bv_val, LDAP_SASL_SIMPLE,
1529 &bindcred, NULL, NULL, &msgid );
1530 rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
1535 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1536 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1537 if ( sendok & LDAP_BACK_SENDERR ) {
1538 send_ldap_result( op, rs );
1543 if ( rc == LDAP_SUCCESS ) {
1544 /* set rebind stuff in case of successful proxyAuthz bind,
1545 * so that referral chasing is attempted using the right
1547 LDAP_BACK_CONN_ISBOUND_SET( lc );
1548 ber_dupbv( &lc->lc_bound_ndn, &binddn );
1550 if ( LDAP_BACK_SAVECRED( li ) ) {
1551 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
1552 memset( lc->lc_cred.bv_val, 0,
1553 lc->lc_cred.bv_len );
1555 ber_bvreplace( &lc->lc_cred, &bindcred );
1556 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1560 return LDAP_BACK_CONN_ISBOUND( lc );
1564 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1565 * to existing server-side controls if required; if not,
1566 * the existing server-side controls are placed in *pctrls.
1567 * The caller, after using the controls in client API
1568 * operations, if ( *pctrls != op->o_ctrls ), should
1569 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1570 * The function returns success if the control could
1571 * be added if required, or if it did nothing; in the future,
1572 * it might return some error if it failed.
1574 * if no bind took place yet, but the connection is bound
1575 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1576 * and explicitly add proxyAuthz the control to every operation
1577 * with the dn bound to the connection as control value.
1579 * If no server-side controls are defined for the operation,
1580 * simply add the proxyAuthz control; otherwise, if the
1581 * proxyAuthz control is not already set, add it as
1584 * FIXME: is controls order significant for security?
1585 * ANSWER: controls ordering and interoperability
1586 * must be indicated by the specs of each control; if none
1587 * is specified, the order is irrelevant.
1590 ldap_back_proxy_authz_ctrl(
1594 LDAPControl ***pctrls )
1596 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
1597 LDAPControl **ctrls = NULL;
1600 struct berval assertedID,
1605 rs->sr_err = LDAP_SUCCESS;
1607 /* don't proxyAuthz if protocol is not LDAPv3 */
1608 switch ( li->li_version ) {
1613 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1622 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1623 * but if it is not set this test fails. We need a different
1624 * means to detect if idassert is enabled */
1625 if ( ( BER_BVISNULL( &li->li_idassert_authcID ) || BER_BVISEMPTY( &li->li_idassert_authcID ) )
1626 && ( BER_BVISNULL( &li->li_idassert_authcDN ) || BER_BVISEMPTY( &li->li_idassert_authcDN ) ) )
1631 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1635 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1636 ndn = op->o_conn->c_ndn;
1642 if ( li->li_idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1643 if ( op->o_proxy_authz ) {
1645 * FIXME: we do not want to perform proxyAuthz
1646 * on behalf of the client, because this would
1647 * be performed with "proxyauthzdn" privileges.
1649 * This might actually be too strict, since
1650 * the "proxyauthzdn" authzTo, and each entry's
1651 * authzFrom attributes may be crafted
1652 * to avoid unwanted proxyAuthz to take place.
1655 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1656 rs->sr_text = "proxyAuthz not allowed within namingContext";
1661 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1665 if ( BER_BVISNULL( &ndn ) ) {
1669 if ( BER_BVISNULL( &li->li_idassert_authcDN ) ) {
1673 } else if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1674 if ( ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1675 /* && ( !BER_BVISNULL( &ndn )
1676 || LDAP_BACK_CONN_ISBOUND( lc ) ) */ )
1678 /* already asserted in SASL via native authz */
1679 /* NOTE: the test on lc->lc_bound is used to trap
1680 * native authorization of anonymous users,
1681 * since in that case ndn is NULL */
1685 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1687 struct berval authcDN;
1689 if ( BER_BVISNULL( &ndn ) ) {
1690 authcDN = slap_empty_bv;
1694 rc = slap_sasl_matches( op, li->li_idassert_authz,
1695 &authcDN, & authcDN );
1696 if ( rc != LDAP_SUCCESS ) {
1697 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1699 /* ndn is not authorized
1700 * to use idassert */
1707 if ( op->o_proxy_authz ) {
1710 * 1) ignore the already set proxyAuthz control
1711 * 2) leave it in place, and don't set ours
1713 * 4) reject the operation
1715 * option (4) is very drastic
1716 * option (3) will make the remote server reject
1717 * the operation, thus being equivalent to (4)
1718 * option (2) will likely break the idassert
1719 * assumptions, so we cannot accept it;
1720 * option (1) means that we are contradicting
1721 * the client's reques.
1723 * I think (4) is the only correct choice.
1725 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1726 rs->sr_text = "proxyAuthz not allowed within namingContext";
1729 if ( op->o_is_auth_check ) {
1730 mode = LDAP_BACK_IDASSERT_NOASSERT;
1733 mode = li->li_idassert_mode;
1737 case LDAP_BACK_IDASSERT_SELF:
1738 if ( BER_BVISNULL( &ndn ) ) {
1744 case LDAP_BACK_IDASSERT_LEGACY:
1745 /* original behavior:
1746 * assert the client's identity */
1747 if ( BER_BVISNULL( &ndn ) ) {
1748 assertedID = slap_empty_bv;
1754 case LDAP_BACK_IDASSERT_ANONYMOUS:
1755 /* assert "anonymous" */
1756 assertedID = slap_empty_bv;
1759 case LDAP_BACK_IDASSERT_NOASSERT:
1760 /* don't assert; bind as proxyauthzdn */
1763 case LDAP_BACK_IDASSERT_OTHERID:
1764 case LDAP_BACK_IDASSERT_OTHERDN:
1765 /* assert idassert DN */
1766 assertedID = li->li_idassert_authzID;
1773 if ( BER_BVISNULL( &assertedID ) ) {
1774 assertedID = slap_empty_bv;
1777 /* don't idassert the bound DN (ITS#4497) */
1778 if ( dn_match( &assertedID, &lc->lc_bound_ndn ) ) {
1782 if ( op->o_ctrls ) {
1783 for ( i = 0; op->o_ctrls[ i ]; i++ )
1784 /* just count ctrls */ ;
1787 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
1789 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
1791 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1792 ctrls[ 0 ]->ldctl_iscritical = 1;
1794 switch ( li->li_idassert_mode ) {
1795 /* already in u:ID or dn:DN form */
1796 case LDAP_BACK_IDASSERT_OTHERID:
1797 case LDAP_BACK_IDASSERT_OTHERDN:
1798 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
1801 /* needs the dn: prefix */
1803 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1804 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
1806 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1807 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1808 assertedID.bv_val, assertedID.bv_len + 1 );
1812 /* Older versions of <draft-weltman-ldapv3-proxy> required
1813 * to encode the value of the authzID (and called it proxyDN);
1814 * this hack provides compatibility with those DSAs that
1815 * implement it this way */
1816 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
1817 struct berval authzID = ctrls[ 0 ]->ldctl_value;
1818 BerElementBuffer berbuf;
1819 BerElement *ber = (BerElement *)&berbuf;
1822 ber_init2( ber, 0, LBER_USE_DER );
1823 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1825 tag = ber_printf( ber, "O", &authzID );
1826 if ( tag == LBER_ERROR ) {
1827 rs->sr_err = LDAP_OTHER;
1831 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
1832 rs->sr_err = LDAP_OTHER;
1837 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
1838 ber_free_buf( ber );
1840 if ( rs->sr_err != LDAP_SUCCESS ) {
1841 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1846 } else if ( li->li_idassert_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
1847 struct berval authzID = ctrls[ 0 ]->ldctl_value,
1849 BerElementBuffer berbuf;
1850 BerElement *ber = (BerElement *)&berbuf;
1853 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
1854 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1855 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1857 rs->sr_err = LDAP_PROTOCOL_ERROR;
1862 tmp.bv_val += STRLENOF( "dn:" );
1863 tmp.bv_len -= STRLENOF( "dn:" );
1865 ber_init2( ber, 0, LBER_USE_DER );
1866 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1868 /* apparently, Mozilla API encodes this
1869 * as "SEQUENCE { LDAPDN }" */
1870 tag = ber_printf( ber, "{O}", &tmp );
1871 if ( tag == LBER_ERROR ) {
1872 rs->sr_err = LDAP_OTHER;
1876 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
1877 rs->sr_err = LDAP_OTHER;
1882 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
1883 ber_free_buf( ber );
1885 if ( rs->sr_err != LDAP_SUCCESS ) {
1886 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1891 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1894 if ( op->o_ctrls ) {
1895 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1896 ctrls[ i + 1 ] = op->o_ctrls[ i ];
1899 ctrls[ i + 1 ] = NULL;
1902 if ( ctrls == NULL ) {
1903 ctrls = op->o_ctrls;
1912 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1914 LDAPControl **ctrls = *pctrls;
1916 /* we assume that the first control is the proxyAuthz
1917 * added by back-ldap, so it's the only one we explicitly
1919 if ( ctrls && ctrls != op->o_ctrls ) {
1920 assert( ctrls[ 0 ] != NULL );
1922 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1923 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1926 op->o_tmpfree( ctrls, op->o_tmpmemctx );