1 /* bind.c - ldap backend bind function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2009 The OpenLDAP Foundation.
6 * Portions Copyright 2000-2003 Pierangelo Masarati.
7 * Portions Copyright 1999-2003 Howard Chu.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
19 * This work was initially developed by Howard Chu for inclusion
20 * in OpenLDAP Software and subsequently enhanced by Pierangelo
29 #include <ac/socket.h>
30 #include <ac/string.h>
34 #include "back-ldap.h"
35 #undef ldap_debug /* silence a warning in ldap-int.h */
36 #include "../../../libraries/libldap/ldap-int.h"
38 #include "lutil_ldap.h"
40 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ "2.16.840.1.113730.3.4.12"
42 #if LDAP_BACK_PRINT_CONNTREE > 0
48 { LDAP_BACK_FCONN_ISBOUND, 'B' },
49 { LDAP_BACK_FCONN_ISANON, 'A' },
50 { LDAP_BACK_FCONN_ISPRIV, 'P' },
51 { LDAP_BACK_FCONN_ISTLS, 'T' },
52 { LDAP_BACK_FCONN_BINDING, 'X' },
53 { LDAP_BACK_FCONN_TAINTED, 'E' },
54 { LDAP_BACK_FCONN_ABANDON, 'N' },
55 { LDAP_BACK_FCONN_ISIDASR, 'S' },
56 { LDAP_BACK_FCONN_CACHED, 'C' },
61 ldap_back_conn_print( ldapconn_t *lc, const char *avlstr )
63 char buf[ SLAP_TEXT_BUFLEN ];
64 char fbuf[ sizeof("BAPTIENSC") ];
67 ldap_back_conn2str( &lc->lc_base, buf, sizeof( buf ) );
68 for ( i = 0; flagsmap[ i ].c != '\0'; i++ ) {
69 if ( lc->lc_lcflags & flagsmap[i].f ) {
70 fbuf[i] = flagsmap[i].c;
78 fprintf( stderr, "lc=%p %s %s flags=0x%08x (%s)\n",
79 (void *)lc, buf, avlstr, lc->lc_lcflags, fbuf );
83 ldap_back_ravl_print( Avlnode *root, int depth )
92 ldap_back_ravl_print( root->avl_right, depth+1 );
94 for ( i = 0; i < depth; i++ ) {
95 fprintf( stderr, "-" );
99 ldap_back_conn_print( lc, avl_bf2str( root->avl_bf ) );
101 ldap_back_ravl_print( root->avl_left, depth + 1 );
104 static char* priv2str[] = {
115 ldap_back_print_conntree( ldapinfo_t *li, char *msg )
119 fprintf( stderr, "========> %s\n", msg );
121 for ( c = LDAP_BACK_PCONN_FIRST; c < LDAP_BACK_PCONN_LAST; c++ ) {
125 fprintf( stderr, " %s[%d]\n", priv2str[ c ], li->li_conn_priv[ c ].lic_num );
127 LDAP_TAILQ_FOREACH( lc, &li->li_conn_priv[ c ].lic_priv, lc_q )
129 fprintf( stderr, " [%d] ", i );
130 ldap_back_conn_print( lc, "" );
135 if ( li->li_conninfo.lai_tree == 0 ) {
136 fprintf( stderr, "\t(empty)\n" );
139 ldap_back_ravl_print( li->li_conninfo.lai_tree, 0 );
142 fprintf( stderr, "<======== %s\n", msg );
144 #endif /* LDAP_BACK_PRINT_CONNTREE */
147 ldap_back_freeconn( ldapinfo_t *li, ldapconn_t *lc, int dolock );
150 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
151 struct berval *binddn, struct berval *bindcred );
154 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
155 struct berval *binddn, struct berval *bindcred );
158 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs,
159 ldap_back_send_t sendok, struct berval *binddn, struct berval *bindcred );
162 ldap_back_prepare_conn( ldapconn_t *lc, Operation *op, SlapReply *rs,
163 ldap_back_send_t sendok );
166 ldap_back_conndnlc_cmp( const void *c1, const void *c2 );
169 ldap_back_conn_delete( ldapinfo_t *li, ldapconn_t *lc )
171 if ( LDAP_BACK_PCONN_ISPRIV( lc ) ) {
172 if ( LDAP_BACK_CONN_CACHED( lc ) ) {
173 assert( lc->lc_q.tqe_prev != NULL );
174 assert( li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num > 0 );
175 li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num--;
176 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv, lc, lc_q );
177 LDAP_TAILQ_ENTRY_INIT( lc, lc_q );
178 LDAP_BACK_CONN_CACHED_CLEAR( lc );
181 assert( LDAP_BACK_CONN_TAINTED( lc ) );
182 assert( lc->lc_q.tqe_prev == NULL );
186 ldapconn_t *tmplc = NULL;
188 if ( LDAP_BACK_CONN_CACHED( lc ) ) {
189 assert( !LDAP_BACK_CONN_TAINTED( lc ) );
190 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
191 ldap_back_conndnlc_cmp );
192 assert( tmplc == lc );
193 LDAP_BACK_CONN_CACHED_CLEAR( lc );
196 assert( LDAP_BACK_CONN_TAINTED( lc ) || tmplc == lc );
203 ldap_back_bind( Operation *op, SlapReply *rs )
205 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
208 LDAPControl **ctrls = NULL;
209 struct berval save_o_dn;
210 int save_o_do_not_cache,
213 ldap_back_send_t retrying = LDAP_BACK_RETRYING;
215 /* allow rootdn as a means to auth without the need to actually
216 * contact the proxied DSA */
217 switch ( be_rootdn_bind( op, rs ) ) {
218 case SLAP_CB_CONTINUE:
225 lc = ldap_back_getconn( op, rs, LDAP_BACK_BIND_SERR, NULL, NULL );
230 /* we can do (almost) whatever we want with this conn,
231 * because either it's temporary, or it's marked as binding */
232 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
233 ch_free( lc->lc_bound_ndn.bv_val );
234 BER_BVZERO( &lc->lc_bound_ndn );
236 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
237 memset( lc->lc_cred.bv_val, 0, lc->lc_cred.bv_len );
238 ch_free( lc->lc_cred.bv_val );
239 BER_BVZERO( &lc->lc_cred );
241 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
243 /* don't add proxyAuthz; set the bindDN */
244 save_o_dn = op->o_dn;
245 save_o_do_not_cache = op->o_do_not_cache;
246 op->o_dn = op->o_req_dn;
247 op->o_do_not_cache = 1;
250 rc = ldap_back_controls_add( op, rs, lc, &ctrls );
251 op->o_dn = save_o_dn;
252 op->o_do_not_cache = save_o_do_not_cache;
253 if ( rc != LDAP_SUCCESS ) {
254 send_ldap_result( op, rs );
255 ldap_back_release_conn( li, lc );
260 /* method is always LDAP_AUTH_SIMPLE if we got here */
261 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
263 &op->orb_cred, ctrls, NULL, &msgid );
264 /* FIXME: should we always retry, or only when piping the bind
265 * in the "override" connection pool? */
266 rc = ldap_back_op_result( lc, op, rs, msgid,
267 li->li_timeout[ SLAP_OP_BIND ],
268 LDAP_BACK_BIND_SERR | retrying );
269 if ( rc == LDAP_UNAVAILABLE && retrying ) {
270 retrying &= ~LDAP_BACK_RETRYING;
271 if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_BIND_SERR ) ) {
276 ldap_back_controls_free( op, rs, &ctrls );
278 if ( rc == LDAP_SUCCESS ) {
279 /* If defined, proxyAuthz will be used also when
280 * back-ldap is the authorizing backend; for this
281 * purpose, after a successful bind the connection
282 * is left for further binds, and further operations
283 * on this client connection will use a default
284 * connection with identity assertion */
285 /* NOTE: use with care */
286 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
287 ldap_back_release_conn( li, lc );
291 /* rebind is now done inside ldap_back_proxy_authz_bind()
292 * in case of success */
293 LDAP_BACK_CONN_ISBOUND_SET( lc );
294 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
296 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
297 memset( lc->lc_cred.bv_val, 0,
298 lc->lc_cred.bv_len );
301 if ( LDAP_BACK_SAVECRED( li ) ) {
302 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
303 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
306 lc->lc_cred.bv_len = 0;
310 /* must re-insert if local DN changed as result of bind */
311 if ( !LDAP_BACK_CONN_ISBOUND( lc )
312 || ( !dn_match( &op->o_req_ndn, &lc->lc_local_ndn )
313 && !LDAP_BACK_PCONN_ISPRIV( lc ) ) )
318 /* wait for all other ops to release the connection */
320 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
321 if ( lc->lc_refcnt > 1 ) {
322 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
323 ldap_pvt_thread_yield();
327 #if LDAP_BACK_PRINT_CONNTREE > 0
328 ldap_back_print_conntree( li, ">>> ldap_back_bind" );
329 #endif /* LDAP_BACK_PRINT_CONNTREE */
331 assert( lc->lc_refcnt == 1 );
332 ldap_back_conn_delete( li, lc );
334 /* delete all cached connections with the current connection */
335 if ( LDAP_BACK_SINGLECONN( li ) ) {
336 while ( ( tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc, ldap_back_conn_cmp ) ) != NULL )
338 assert( !LDAP_BACK_PCONN_ISPRIV( lc ) );
339 Debug( LDAP_DEBUG_TRACE,
340 "=>ldap_back_bind: destroying conn %lu (refcnt=%u)\n",
341 lc->lc_conn->c_connid, lc->lc_refcnt, 0 );
343 if ( tmplc->lc_refcnt != 0 ) {
345 LDAP_BACK_CONN_TAINTED_SET( tmplc );
346 LDAP_BACK_CONN_CACHED_CLEAR( tmplc );
350 * Needs a test because the handler may be corrupted,
351 * and calling ldap_unbind on a corrupted header results
352 * in a segmentation fault
354 ldap_back_conn_free( tmplc );
359 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
360 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
361 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
362 LDAP_BACK_PCONN_ROOTDN_SET( lc, op );
364 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
365 ldap_back_conndn_cmp, ldap_back_conndn_dup );
368 #if LDAP_BACK_PRINT_CONNTREE > 0
369 ldap_back_print_conntree( li, "<<< ldap_back_bind" );
370 #endif /* LDAP_BACK_PRINT_CONNTREE */
372 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
375 LDAP_BACK_CONN_CACHED_SET( lc );
379 /* duplicate; someone else successfully bound
380 * on the same connection with the same identity;
381 * we can do this because lc_refcnt == 1 */
382 ldap_back_conn_free( lc );
388 ldap_back_release_conn( li, lc );
395 * ldap_back_conndn_cmp
397 * compares two ldapconn_t based on the value of the conn pointer
398 * and of the local DN; used by avl stuff for insert, lookup
402 ldap_back_conndn_cmp( const void *c1, const void *c2 )
404 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
405 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
408 /* If local DNs don't match, it is definitely not a match */
409 /* For shared sessions, conn is NULL. Only explicitly
410 * bound sessions will have non-NULL conn.
412 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
414 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
421 * ldap_back_conndnlc_cmp
423 * compares two ldapconn_t based on the value of the conn pointer,
424 * the local DN and the lc pointer; used by avl stuff for insert, lookup
428 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
430 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
431 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
434 /* If local DNs don't match, it is definitely not a match */
435 /* For shared sessions, conn is NULL. Only explicitly
436 * bound sessions will have non-NULL conn.
438 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
440 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
442 rc = SLAP_PTRCMP( lc1, lc2 );
452 * compares two ldapconn_t based on the value of the conn pointer;
453 * used by avl stuff for delete of all conns with the same connid
456 ldap_back_conn_cmp( const void *c1, const void *c2 )
458 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
459 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
461 /* For shared sessions, conn is NULL. Only explicitly
462 * bound sessions will have non-NULL conn.
464 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
468 * ldap_back_conndn_dup
470 * returns -1 in case a duplicate ldapconn_t has been inserted;
474 ldap_back_conndn_dup( void *c1, void *c2 )
476 ldapconn_t *lc1 = (ldapconn_t *)c1;
477 ldapconn_t *lc2 = (ldapconn_t *)c2;
479 /* Cannot have more than one shared session with same DN */
480 if ( lc1->lc_conn == lc2->lc_conn &&
481 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
490 ldap_back_freeconn( ldapinfo_t *li, ldapconn_t *lc, int dolock )
493 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
496 #if LDAP_BACK_PRINT_CONNTREE > 0
497 ldap_back_print_conntree( li, ">>> ldap_back_freeconn" );
498 #endif /* LDAP_BACK_PRINT_CONNTREE */
500 (void)ldap_back_conn_delete( li, lc );
502 if ( lc->lc_refcnt == 0 ) {
503 ldap_back_conn_free( (void *)lc );
506 #if LDAP_BACK_PRINT_CONNTREE > 0
507 ldap_back_print_conntree( li, "<<< ldap_back_freeconn" );
508 #endif /* LDAP_BACK_PRINT_CONNTREE */
511 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
528 int rc = LDAP_SUCCESS;
530 /* start TLS ("tls-[try-]{start,propagate}" statements) */
531 if ( ( LDAP_BACK_USE_TLS_F( flags ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS_F( flags ) ) )
532 && !ldap_is_ldaps_url( url ) )
534 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
536 * use asynchronous StartTLS
537 * in case, chase referral (not implemented yet)
541 if ( protocol == 0 ) {
542 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
546 if ( protocol < LDAP_VERSION3 ) {
547 /* we should rather bail out... */
548 rc = LDAP_UNWILLING_TO_PERFORM;
549 *text = "invalid protocol version";
552 if ( rc == LDAP_SUCCESS ) {
553 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
556 if ( rc == LDAP_SUCCESS ) {
557 LDAPMessage *res = NULL;
560 LDAP_BACK_TV_SET( &tv );
563 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
565 rc = LDAP_UNAVAILABLE;
567 } else if ( rc == 0 ) {
568 if ( retries != LDAP_BACK_RETRY_NEVER ) {
569 ldap_pvt_thread_yield();
573 LDAP_BACK_TV_SET( &tv );
576 rc = LDAP_UNAVAILABLE;
578 } else if ( rc == LDAP_RES_EXTENDED ) {
579 struct berval *data = NULL;
581 rc = ldap_parse_extended_result( ld, res,
583 if ( rc == LDAP_SUCCESS ) {
585 rc = ldap_parse_result( ld, res, &err,
586 NULL, NULL, NULL, NULL, 1 );
587 if ( rc == LDAP_SUCCESS ) {
592 /* FIXME: in case a referral
593 * is returned, should we try
594 * using it instead of the
596 if ( rc == LDAP_SUCCESS ) {
597 rc = ldap_install_tls( ld );
599 } else if ( rc == LDAP_REFERRAL ) {
600 rc = LDAP_UNWILLING_TO_PERFORM;
601 *text = "unwilling to chase referral returned by Start TLS exop";
605 if ( data->bv_val ) {
606 ber_memfree( data->bv_val );
620 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
622 * use synchronous StartTLS
624 rc = ldap_start_tls_s( ld, NULL, NULL );
625 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
627 /* if StartTLS is requested, only attempt it if the URL
628 * is not "ldaps://"; this may occur not only in case
629 * of misconfiguration, but also when used in the chain
630 * overlay, where the "uri" can be parsed out of a referral */
636 case LDAP_SERVER_DOWN:
640 if ( LDAP_BACK_TLS_CRITICAL_F( flags ) ) {
641 *text = "could not start TLS";
645 /* in case Start TLS is not critical */
657 #endif /* HAVE_TLS */
660 ldap_back_prepare_conn( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
662 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
666 int is_tls = op->o_conn->c_is_tls;
667 time_t lctime = (time_t)(-1);
669 #endif /* HAVE_TLS */
671 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
672 rs->sr_err = ldap_initialize( &ld, li->li_uri );
673 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
674 if ( rs->sr_err != LDAP_SUCCESS ) {
678 if ( li->li_urllist_f ) {
679 ldap_set_urllist_proc( ld, li->li_urllist_f, li->li_urllist_p );
682 /* Set LDAP version. This will always succeed: If the client
683 * bound with a particular version, then so can we.
685 if ( li->li_version != 0 ) {
686 version = li->li_version;
688 } else if ( op->o_protocol != 0 ) {
689 version = op->o_protocol;
692 /* assume it's an internal op; set to LDAPv3 */
693 version = LDAP_VERSION3;
695 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
697 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
698 ldap_set_option( ld, LDAP_OPT_REFERRALS,
699 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
701 if ( li->li_network_timeout > 0 ) {
704 tv.tv_sec = li->li_network_timeout;
706 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
710 if ( LDAP_BACK_CONN_ISPRIV( lc ) ) {
713 } else if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
714 sb = &li->li_idassert.si_bc;
720 if ( sb->sb_tls_do_init ) {
721 bindconf_tls_set( sb, ld );
722 } else if ( sb->sb_tls_ctx ) {
723 ldap_set_option( ld, LDAP_OPT_X_TLS_CTX, sb->sb_tls_ctx );
726 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
727 assert( li->li_uri_mutex_do_not_lock == 0 );
728 li->li_uri_mutex_do_not_lock = 1;
729 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
730 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
731 li->li_uri_mutex_do_not_lock = 0;
732 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
733 if ( rs->sr_err != LDAP_SUCCESS ) {
734 ldap_unbind_ext( ld, NULL, NULL );
735 rs->sr_text = "Start TLS failed";
738 } else if ( li->li_idle_timeout ) {
739 /* only touch when activity actually took place... */
742 #endif /* HAVE_TLS */
748 LDAP_BACK_CONN_ISTLS_SET( lc );
750 LDAP_BACK_CONN_ISTLS_CLEAR( lc );
752 if ( lctime != (time_t)(-1) ) {
753 lc->lc_time = lctime;
755 #endif /* HAVE_TLS */
758 if ( rs->sr_err != LDAP_SUCCESS ) {
759 rs->sr_err = slap_map_api2result( rs );
760 if ( sendok & LDAP_BACK_SENDERR ) {
761 if ( rs->sr_text == NULL ) {
762 rs->sr_text = "Proxy connection initialization failed";
764 send_ldap_result( op, rs );
768 if ( li->li_conn_ttl > 0 ) {
769 lc->lc_create_time = op->o_time;
780 ldap_back_send_t sendok,
781 struct berval *binddn,
782 struct berval *bindcred )
784 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
785 ldapconn_t *lc = NULL,
788 lookupconn = !( sendok & LDAP_BACK_BINDING );
790 /* if the server is quarantined, and
791 * - the current interval did not expire yet, or
792 * - no more retries should occur,
793 * don't return the connection */
794 if ( li->li_isquarantined ) {
795 slap_retry_info_t *ri = &li->li_quarantine;
798 if ( li->li_quarantine.ri_interval ) {
799 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
800 if ( li->li_isquarantined == LDAP_BACK_FQ_YES ) {
801 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
802 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
804 Debug( LDAP_DEBUG_ANY,
805 "%s: ldap_back_getconn quarantine "
806 "retry block #%d try #%d.\n",
807 op->o_log_prefix, ri->ri_idx, ri->ri_count );
808 li->li_isquarantined = LDAP_BACK_FQ_RETRYING;
811 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
815 rs->sr_err = LDAP_UNAVAILABLE;
816 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
817 rs->sr_text = "Target is quarantined";
818 send_ldap_result( op, rs );
824 /* Internal searches are privileged and shared. So is root. */
825 if ( op->o_do_not_cache || be_isroot( op ) ) {
826 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
827 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
828 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
831 struct berval tmpbinddn,
838 if ( binddn == NULL ) {
841 if ( bindcred == NULL ) {
842 bindcred = &tmpbindcred;
844 if ( op->o_tag == LDAP_REQ_BIND ) {
845 save_o_dn = op->o_dn;
846 save_o_ndn = op->o_ndn;
847 op->o_dn = op->o_req_dn;
848 op->o_ndn = op->o_req_ndn;
850 isproxyauthz = ldap_back_is_proxy_authz( op, rs, sendok, binddn, bindcred );
851 if ( op->o_tag == LDAP_REQ_BIND ) {
852 op->o_dn = save_o_dn;
853 op->o_ndn = save_o_ndn;
855 if ( isproxyauthz == -1 ) {
859 lc_curr.lc_local_ndn = op->o_ndn;
860 /* Explicit binds must not be shared;
861 * however, explicit binds are piped in a special connection
862 * when idassert is to occur with "override" set */
863 if ( op->o_tag == LDAP_REQ_BIND && !isproxyauthz ) {
864 lc_curr.lc_conn = op->o_conn;
867 if ( isproxyauthz && !( sendok & LDAP_BACK_BINDING ) ) {
868 lc_curr.lc_local_ndn = *binddn;
869 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
870 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
872 } else if ( isproxyauthz && ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) {
873 lc_curr.lc_local_ndn = slap_empty_bv;
874 LDAP_BACK_PCONN_BIND_SET( &lc_curr, op );
875 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
878 } else if ( SLAP_IS_AUTHZ_BACKEND( op ) ) {
879 lc_curr.lc_conn = op->o_conn;
882 LDAP_BACK_PCONN_ANON_SET( &lc_curr, op );
887 /* Explicit Bind requests always get their own conn */
890 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
891 if ( LDAP_BACK_PCONN_ISPRIV( &lc_curr ) ) {
892 /* lookup a conn that's not binding */
893 LDAP_TAILQ_FOREACH( lc,
894 &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv,
897 if ( !LDAP_BACK_CONN_BINDING( lc ) && lc->lc_refcnt == 0 ) {
903 if ( lc != LDAP_TAILQ_LAST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
906 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
908 LDAP_TAILQ_ENTRY_INIT( lc, lc_q );
909 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
913 } else if ( !LDAP_BACK_USE_TEMPORARIES( li )
914 && li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_num == li->li_conn_priv_max )
916 lc = LDAP_TAILQ_FIRST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv );
921 /* Searches for a ldapconn in the avl tree */
922 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
923 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
927 /* Don't reuse connections while they're still binding */
928 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
929 if ( !LDAP_BACK_USE_TEMPORARIES( li ) ) {
930 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
932 ldap_pvt_thread_yield();
939 if ( op->o_tag == LDAP_REQ_BIND ) {
940 /* right now, this is the only possible case */
941 assert( ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) );
942 LDAP_BACK_CONN_BINDING_SET( lc );
945 refcnt = ++lc->lc_refcnt;
948 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
951 /* Looks like we didn't get a bind. Open a new session... */
953 lc = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
954 lc->lc_flags = li->li_flags;
955 lc->lc_lcflags = lc_curr.lc_lcflags;
956 if ( ldap_back_prepare_conn( lc, op, rs, sendok ) != LDAP_SUCCESS ) {
961 if ( sendok & LDAP_BACK_BINDING ) {
962 LDAP_BACK_CONN_BINDING_SET( lc );
965 lc->lc_conn = lc_curr.lc_conn;
966 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
969 * the rationale is: connections as the rootdn are privileged,
970 * so acl_authcDN is to be used; however, in some cases
971 * one already configured identity assertion with a highly
972 * privileged idassert_authcDN, so if acl_authcDN is NULL
973 * and idassert_authcDN is not, use the second instead.
975 * might change in the future, because it's preferable
976 * to make clear what identity is being used, since
977 * the only drawback is that one risks to configure
978 * the same identity twice...
980 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
981 if ( BER_BVISNULL( &li->li_acl_authcDN ) && !BER_BVISNULL( &li->li_idassert_authcDN ) ) {
982 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
983 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
986 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
987 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
989 LDAP_BACK_CONN_ISPRIV_SET( lc );
991 } else if ( LDAP_BACK_CONN_ISIDASSERT( &lc_curr ) ) {
992 if ( !LDAP_BACK_PCONN_ISBIND( &lc_curr ) ) {
993 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
994 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
996 LDAP_BACK_CONN_ISIDASSERT_SET( lc );
999 BER_BVZERO( &lc->lc_cred );
1000 BER_BVZERO( &lc->lc_bound_ndn );
1001 if ( !BER_BVISEMPTY( &op->o_ndn )
1002 && SLAP_IS_AUTHZ_BACKEND( op ) )
1004 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
1009 /* if start TLS failed but it was not mandatory,
1010 * check if the non-TLS connection was already
1011 * in cache; in case, destroy the newly created
1012 * connection and use the existing one */
1013 if ( LDAP_BACK_PCONN_ISTLS( lc )
1014 && !ldap_tls_inplace( lc->lc_ld ) )
1016 ldapconn_t *tmplc = NULL;
1017 int idx = LDAP_BACK_CONN2PRIV( &lc_curr ) - 1;
1019 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1020 LDAP_TAILQ_FOREACH( tmplc,
1021 &li->li_conn_priv[ idx ].lic_priv,
1024 if ( !LDAP_BACK_CONN_BINDING( tmplc ) ) {
1029 if ( tmplc != NULL ) {
1030 refcnt = ++tmplc->lc_refcnt;
1031 ldap_back_conn_free( lc );
1034 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1036 if ( tmplc != NULL ) {
1040 #endif /* HAVE_TLS */
1042 /* Inserts the newly created ldapconn in the avl tree */
1043 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1045 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1047 assert( lc->lc_refcnt == 1 );
1049 #if LDAP_BACK_PRINT_CONNTREE > 0
1050 ldap_back_print_conntree( li, ">>> ldap_back_getconn(insert)" );
1051 #endif /* LDAP_BACK_PRINT_CONNTREE */
1053 if ( LDAP_BACK_PCONN_ISPRIV( lc ) ) {
1054 if ( li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num < li->li_conn_priv_max ) {
1055 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv, lc, lc_q );
1056 li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num++;
1057 LDAP_BACK_CONN_CACHED_SET( lc );
1060 LDAP_BACK_CONN_TAINTED_SET( lc );
1065 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
1066 ldap_back_conndn_cmp, ldap_back_conndn_dup );
1067 LDAP_BACK_CONN_CACHED_SET( lc );
1070 #if LDAP_BACK_PRINT_CONNTREE > 0
1071 ldap_back_print_conntree( li, "<<< ldap_back_getconn(insert)" );
1072 #endif /* LDAP_BACK_PRINT_CONNTREE */
1074 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1076 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1077 char buf[ SLAP_TEXT_BUFLEN ];
1079 snprintf( buf, sizeof( buf ),
1080 "lc=%p inserted refcnt=%u rc=%d",
1081 (void *)lc, refcnt, rs->sr_err );
1083 Debug( LDAP_DEBUG_TRACE,
1084 "=>ldap_back_getconn: %s: %s\n",
1085 op->o_log_prefix, buf, 0 );
1088 if ( !LDAP_BACK_PCONN_ISPRIV( lc ) ) {
1089 /* Err could be -1 in case a duplicate ldapconn is inserted */
1090 switch ( rs->sr_err ) {
1095 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1096 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( li ) ) {
1097 /* duplicate: free and try to get the newly created one */
1098 ldap_back_conn_free( lc );
1103 /* taint connection, so that it'll be freed when released */
1104 LDAP_BACK_CONN_TAINTED_SET( lc );
1108 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1109 ldap_back_conn_free( lc );
1110 rs->sr_err = LDAP_OTHER;
1111 rs->sr_text = "Proxy bind collision";
1112 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1113 send_ldap_result( op, rs );
1122 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
1123 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
1127 /* let it be used, but taint/delete it so that
1128 * no-one else can look it up any further */
1129 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1131 #if LDAP_BACK_PRINT_CONNTREE > 0
1132 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1133 #endif /* LDAP_BACK_PRINT_CONNTREE */
1135 (void)ldap_back_conn_delete( li, lc );
1136 LDAP_BACK_CONN_TAINTED_SET( lc );
1138 #if LDAP_BACK_PRINT_CONNTREE > 0
1139 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1140 #endif /* LDAP_BACK_PRINT_CONNTREE */
1142 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1145 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1146 char buf[ SLAP_TEXT_BUFLEN ];
1148 snprintf( buf, sizeof( buf ),
1149 "conn %p fetched refcnt=%u%s",
1151 expiring ? " expiring" : "" );
1152 Debug( LDAP_DEBUG_TRACE,
1153 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
1159 #endif /* HAVE_TLS */
1165 ldap_back_release_conn_lock(
1171 ldapconn_t *lc = *lcp;
1174 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1176 assert( lc->lc_refcnt > 0 );
1177 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1179 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
1180 ldap_back_freeconn( li, lc, 0 );
1184 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1189 ldap_back_quarantine(
1193 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1195 slap_retry_info_t *ri = &li->li_quarantine;
1197 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
1199 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1200 time_t new_last = slap_get_time();
1202 switch ( li->li_isquarantined ) {
1203 case LDAP_BACK_FQ_NO:
1204 if ( ri->ri_last == new_last ) {
1208 Debug( LDAP_DEBUG_ANY,
1209 "%s: ldap_back_quarantine enter.\n",
1210 op->o_log_prefix, 0, 0 );
1216 case LDAP_BACK_FQ_RETRYING:
1217 Debug( LDAP_DEBUG_ANY,
1218 "%s: ldap_back_quarantine block #%d try #%d failed.\n",
1219 op->o_log_prefix, ri->ri_idx, ri->ri_count );
1222 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1223 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1234 li->li_isquarantined = LDAP_BACK_FQ_YES;
1235 ri->ri_last = new_last;
1237 } else if ( li->li_isquarantined != LDAP_BACK_FQ_NO ) {
1238 if ( ri->ri_last == slap_get_time() ) {
1242 Debug( LDAP_DEBUG_ANY,
1243 "%s: ldap_back_quarantine exit (%d) err=%d.\n",
1244 op->o_log_prefix, li->li_isquarantined, rs->sr_err );
1246 if ( li->li_quarantine_f ) {
1247 (void)li->li_quarantine_f( li, li->li_quarantine_p );
1252 li->li_isquarantined = LDAP_BACK_FQ_NO;
1256 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
1260 ldap_back_dobind_cb(
1265 ber_tag_t *tptr = op->o_callback->sc_private;
1267 rs->sr_tag = slap_req2res( op->o_tag );
1269 return SLAP_CB_CONTINUE;
1273 * ldap_back_dobind_int
1275 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1278 ldap_back_dobind_int(
1282 ldap_back_send_t sendok,
1286 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1289 struct berval binddn = slap_empty_bv,
1290 bindcred = slap_empty_bv;
1296 ber_tag_t o_tag = op->o_tag;
1297 slap_callback cb = {0};
1299 assert( lcp != NULL );
1300 assert( retries >= 0 );
1302 if ( sendok & LDAP_BACK_GETCONN ) {
1303 assert( *lcp == NULL );
1305 lc = ldap_back_getconn( op, rs, sendok, &binddn, &bindcred );
1315 assert( lc != NULL );
1319 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1322 if ( binding == 0 ) {
1323 /* check if already bound */
1324 rc = isbound = LDAP_BACK_CONN_ISBOUND( lc );
1327 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1332 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
1333 /* if someone else is about to bind it, give up and retry */
1335 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1337 ldap_pvt_thread_yield();
1341 /* otherwise this thread will bind it */
1342 LDAP_BACK_CONN_BINDING_SET( lc );
1348 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1352 * FIXME: we need to let clients use proxyAuthz
1353 * otherwise we cannot do symmetric pools of servers;
1354 * we have to live with the fact that a user can
1355 * authorize itself as any ID that is allowed
1356 * by the authzTo directive of the "proxyauthzdn".
1359 * NOTE: current Proxy Authorization specification
1360 * and implementation do not allow proxy authorization
1361 * control to be provided with Bind requests
1364 * if no bind took place yet, but the connection is bound
1365 * and the "idassert-authcDN" (or other ID) is set,
1366 * then bind as the asserting identity and explicitly
1367 * add the proxyAuthz control to every operation with the
1368 * dn bound to the connection as control value.
1369 * This is done also if this is the authorizing backend,
1370 * but the "override" flag is given to idassert.
1371 * It allows to use SASL bind and yet proxyAuthz users
1373 op->o_tag = LDAP_REQ_BIND;
1374 cb.sc_next = op->o_callback;
1375 cb.sc_private = &o_tag;
1376 cb.sc_response = ldap_back_dobind_cb;
1377 op->o_callback = &cb;
1379 if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
1380 if ( BER_BVISEMPTY( &binddn ) && BER_BVISEMPTY( &bindcred ) ) {
1381 /* if we got here, it shouldn't return result */
1382 rc = ldap_back_is_proxy_authz( op, rs,
1383 LDAP_BACK_DONTSEND, &binddn, &bindcred );
1386 rc = ldap_back_proxy_authz_bind( lc, op, rs, sendok, &binddn, &bindcred );
1390 #ifdef HAVE_CYRUS_SASL
1391 if ( LDAP_BACK_CONN_ISPRIV( lc )
1392 && li->li_acl_authmethod == LDAP_AUTH_SASL )
1394 void *defaults = NULL;
1396 if ( li->li_acl_secprops != NULL ) {
1397 rc = ldap_set_option( lc->lc_ld,
1398 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops );
1400 if ( rc != LDAP_OPT_SUCCESS ) {
1401 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
1402 "(SECPROPS,\"%s\") failed!\n",
1403 li->li_acl_secprops, 0, 0 );
1408 defaults = lutil_sasl_defaults( lc->lc_ld,
1409 li->li_acl_sasl_mech.bv_val,
1410 li->li_acl_sasl_realm.bv_val,
1411 li->li_acl_authcID.bv_val,
1412 li->li_acl_passwd.bv_val,
1414 if ( defaults == NULL ) {
1415 rs->sr_err = LDAP_OTHER;
1416 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1417 if ( sendok & LDAP_BACK_SENDERR ) {
1418 send_ldap_result( op, rs );
1423 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
1424 li->li_acl_authcDN.bv_val,
1425 li->li_acl_sasl_mech.bv_val, NULL, NULL,
1426 LDAP_SASL_QUIET, lutil_sasl_interact,
1429 lutil_sasl_freedefs( defaults );
1431 switch ( rs->sr_err ) {
1433 LDAP_BACK_CONN_ISBOUND_SET( lc );
1436 case LDAP_LOCAL_ERROR:
1437 /* list client API error codes that require
1438 * to taint the connection */
1439 /* FIXME: should actually retry? */
1440 LDAP_BACK_CONN_TAINTED_SET( lc );
1445 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1446 rs->sr_err = slap_map_api2result( rs );
1447 if ( sendok & LDAP_BACK_SENDERR ) {
1448 send_ldap_result( op, rs );
1453 if ( LDAP_BACK_QUARANTINE( li ) ) {
1454 ldap_back_quarantine( op, rs );
1459 #endif /* HAVE_CYRUS_SASL */
1462 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1463 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1464 LDAP_SASL_SIMPLE, &lc->lc_cred,
1465 NULL, NULL, &msgid );
1467 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
1468 if ( retries != LDAP_BACK_RETRY_NEVER ) {
1470 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1473 assert( lc->lc_refcnt > 0 );
1474 if ( lc->lc_refcnt == 1 ) {
1475 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1478 /* lc here must be the regular lc, reset and ready for init */
1479 rs->sr_err = ldap_back_prepare_conn( lc, op, rs, sendok );
1480 if ( rs->sr_err != LDAP_SUCCESS ) {
1481 sendok &= ~LDAP_BACK_SENDERR;
1487 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1490 if ( rs->sr_err == LDAP_SUCCESS ) {
1491 if ( retries > 0 ) {
1498 assert( lc->lc_refcnt == 1 );
1500 ldap_back_freeconn( li, lc, dolock );
1502 rs->sr_err = slap_map_api2result( rs );
1504 if ( LDAP_BACK_QUARANTINE( li ) ) {
1505 ldap_back_quarantine( op, rs );
1508 if ( rs->sr_err != LDAP_SUCCESS &&
1509 ( sendok & LDAP_BACK_SENDERR ) )
1511 if ( op->o_callback == &cb )
1512 op->o_callback = cb.sc_next;
1514 rs->sr_text = "Proxy can't contact remote server";
1515 send_ldap_result( op, rs );
1522 rc = ldap_back_op_result( lc, op, rs, msgid,
1523 -1, ( sendok | LDAP_BACK_BINDING ) );
1524 if ( rc == LDAP_SUCCESS ) {
1525 LDAP_BACK_CONN_ISBOUND_SET( lc );
1529 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1530 rc = LDAP_BACK_CONN_ISBOUND( lc );
1532 ldap_back_release_conn_lock( li, lcp, dolock );
1534 } else if ( LDAP_BACK_SAVECRED( li ) ) {
1535 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1539 if ( op->o_callback == &cb )
1540 op->o_callback = cb.sc_next;
1549 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1552 ldap_back_dobind( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1554 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1556 return ldap_back_dobind_int( lcp, op, rs,
1557 ( sendok | LDAP_BACK_GETCONN ), li->li_nretries, 1 );
1561 * ldap_back_default_rebind
1563 * This is a callback used for chasing referrals using the same
1564 * credentials as the original user on this session.
1567 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1568 ber_int_t msgid, void *params )
1570 ldapconn_t *lc = (ldapconn_t *)params;
1573 /* ... otherwise we couldn't get here */
1574 assert( lc != NULL );
1576 if ( !ldap_tls_inplace( ld ) ) {
1577 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1579 const char *text = NULL;
1581 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1582 LDAP_BACK_RETRY_DEFAULT, &text );
1583 if ( rc != LDAP_SUCCESS ) {
1587 #endif /* HAVE_TLS */
1589 /* FIXME: add checks on the URL/identity? */
1591 return ldap_sasl_bind_s( ld,
1592 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1593 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1597 * ldap_back_default_urllist
1600 ldap_back_default_urllist(
1602 LDAPURLDesc **urllist,
1606 ldapinfo_t *li = (ldapinfo_t *)params;
1607 LDAPURLDesc **urltail;
1609 if ( urllist == url ) {
1610 return LDAP_SUCCESS;
1613 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1616 *urltail = *urllist;
1620 if ( !li->li_uri_mutex_do_not_lock ) {
1621 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1625 ch_free( li->li_uri );
1628 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1630 if ( !li->li_uri_mutex_do_not_lock ) {
1631 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1634 return LDAP_SUCCESS;
1643 ldap_back_send_t sendok )
1645 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1647 /* default behavior */
1648 if ( LDAP_BACK_ABANDON( li ) ) {
1649 return ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1652 if ( LDAP_BACK_IGNORE( li ) ) {
1653 return ldap_pvt_discard( lc->lc_ld, msgid );
1656 if ( LDAP_BACK_CANCEL( li ) ) {
1657 /* FIXME: asynchronous? */
1658 return ldap_cancel_s( lc->lc_ld, msgid, NULL, NULL );
1667 ldap_back_op_result(
1673 ldap_back_send_t sendok )
1675 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1680 LDAPControl **ctrls = NULL;
1683 rs->sr_matched = NULL;
1685 rs->sr_ctrls = NULL;
1687 /* if the error recorded in the reply corresponds
1688 * to a successful state, get the error from the
1689 * remote server response */
1690 if ( LDAP_ERR_OK( rs->sr_err ) ) {
1693 LDAPMessage *res = NULL;
1694 time_t stoptime = (time_t)(-1);
1695 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
1696 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
1697 const char *timeout_text = "Operation timed out";
1699 /* if timeout is not specified, compute and use
1700 * the one specific to the ongoing operation */
1701 if ( timeout == (time_t)(-1) ) {
1702 slap_op_t opidx = slap_req2op( op->o_tag );
1704 if ( opidx == SLAP_OP_SEARCH ) {
1705 if ( op->ors_tlimit <= 0 ) {
1709 timeout = op->ors_tlimit;
1710 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
1711 timeout_text = NULL;
1715 timeout = li->li_timeout[ opidx ];
1719 /* better than nothing :) */
1720 if ( timeout == 0 ) {
1721 if ( li->li_idle_timeout ) {
1722 timeout = li->li_idle_timeout;
1724 } else if ( li->li_conn_ttl ) {
1725 timeout = li->li_conn_ttl;
1730 stoptime = op->o_time + timeout;
1733 LDAP_BACK_TV_SET( &tv );
1736 /* if result parsing fails, note the failure reason */
1737 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1740 if ( timeout && slap_get_time() > stoptime ) {
1741 if ( sendok & LDAP_BACK_BINDING ) {
1742 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1745 /* let it be used, but taint/delete it so that
1746 * no-one else can look it up any further */
1747 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1749 #if LDAP_BACK_PRINT_CONNTREE > 0
1750 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1751 #endif /* LDAP_BACK_PRINT_CONNTREE */
1753 (void)ldap_back_conn_delete( li, lc );
1754 LDAP_BACK_CONN_TAINTED_SET( lc );
1756 #if LDAP_BACK_PRINT_CONNTREE > 0
1757 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1758 #endif /* LDAP_BACK_PRINT_CONNTREE */
1759 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1762 (void)ldap_back_cancel( lc, op, rs, msgid, sendok );
1764 rs->sr_err = timeout_err;
1765 rs->sr_text = timeout_text;
1770 LDAP_BACK_TV_SET( &tv );
1771 ldap_pvt_thread_yield();
1775 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1780 /* otherwise get the result; if it is not
1781 * LDAP_SUCCESS, record it in the reply
1782 * structure (this includes
1783 * LDAP_COMPARE_{TRUE|FALSE}) */
1785 /* only touch when activity actually took place... */
1786 if ( li->li_idle_timeout && lc ) {
1787 lc->lc_time = op->o_time;
1790 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1791 &match, &text, &refs, &ctrls, 1 );
1793 if ( rc != LDAP_SUCCESS ) {
1797 /* RFC 4511: referrals can only appear
1798 * if result code is LDAP_REFERRAL */
1800 && refs[ 0 ] != NULL
1801 && refs[ 0 ][ 0 ] != '\0' )
1803 if ( rs->sr_err != LDAP_REFERRAL ) {
1804 Debug( LDAP_DEBUG_ANY,
1805 "%s ldap_back_op_result: "
1806 "got referrals with err=%d\n",
1813 for ( i = 0; refs[ i ] != NULL; i++ )
1815 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1817 for ( i = 0; refs[ i ] != NULL; i++ ) {
1818 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1820 BER_BVZERO( &rs->sr_ref[ i ] );
1823 } else if ( rs->sr_err == LDAP_REFERRAL ) {
1824 Debug( LDAP_DEBUG_ANY,
1825 "%s ldap_back_op_result: "
1826 "got err=%d with null "
1827 "or empty referrals\n",
1831 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1834 if ( ctrls != NULL ) {
1835 rs->sr_ctrls = ctrls;
1840 /* if the error in the reply structure is not
1841 * LDAP_SUCCESS, try to map it from client
1842 * to server error */
1843 if ( !LDAP_ERR_OK( rs->sr_err ) ) {
1844 rs->sr_err = slap_map_api2result( rs );
1846 /* internal ops ( op->o_conn == NULL )
1847 * must not reply to client */
1848 if ( op->o_conn && !op->o_do_not_cache && match ) {
1850 /* record the (massaged) matched
1851 * DN into the reply structure */
1852 rs->sr_matched = match;
1856 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1857 if ( !( sendok & LDAP_BACK_RETRYING ) ) {
1858 if ( LDAP_BACK_QUARANTINE( li ) ) {
1859 ldap_back_quarantine( op, rs );
1861 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1862 if ( rs->sr_text == NULL ) rs->sr_text = "Proxy operation retry failed";
1863 send_ldap_result( op, rs );
1867 } else if ( op->o_conn &&
1868 ( ( ( sendok & LDAP_BACK_SENDOK ) && LDAP_ERR_OK( rs->sr_err ) )
1869 || ( ( sendok & LDAP_BACK_SENDERR ) && !LDAP_ERR_OK( rs->sr_err ) ) ) )
1871 send_ldap_result( op, rs );
1875 if ( rs->sr_matched != match ) {
1876 free( (char *)rs->sr_matched );
1878 rs->sr_matched = NULL;
1879 ldap_memfree( match );
1883 ldap_memfree( text );
1888 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1893 ber_memvfree( (void **)refs );
1897 assert( rs->sr_ctrls != NULL );
1898 ldap_controls_free( ctrls );
1899 rs->sr_ctrls = NULL;
1902 return( LDAP_ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1905 /* return true if bound, false if failed */
1907 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1909 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1912 assert( lcp != NULL );
1913 assert( *lcp != NULL );
1915 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1917 if ( (*lcp)->lc_refcnt == 1 ) {
1918 int binding = LDAP_BACK_CONN_BINDING( *lcp );
1920 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1921 Debug( LDAP_DEBUG_ANY,
1922 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1923 op->o_log_prefix, li->li_uri,
1924 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1925 "" : (*lcp)->lc_bound_ndn.bv_val );
1926 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1928 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1929 (*lcp)->lc_ld = NULL;
1930 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1932 /* lc here must be the regular lc, reset and ready for init */
1933 rc = ldap_back_prepare_conn( *lcp, op, rs, sendok );
1934 if ( rc != LDAP_SUCCESS ) {
1935 /* freeit, because lc_refcnt == 1 */
1936 (*lcp)->lc_refcnt = 0;
1937 (void)ldap_back_freeconn( li, *lcp, 0 );
1941 } else if ( ( sendok & LDAP_BACK_BINDING ) ) {
1943 LDAP_BACK_CONN_BINDING_SET( *lcp );
1948 rc = ldap_back_dobind_int( lcp, op, rs, sendok, 0, 0 );
1949 if ( rc == 0 && *lcp != NULL ) {
1950 /* freeit, because lc_refcnt == 1 */
1951 (*lcp)->lc_refcnt = 0;
1952 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1953 (void)ldap_back_freeconn( li, *lcp, 0 );
1959 Debug( LDAP_DEBUG_TRACE,
1960 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1961 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1963 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1964 ldap_back_release_conn_lock( li, lcp, 0 );
1965 assert( *lcp == NULL );
1967 if ( sendok & LDAP_BACK_SENDERR ) {
1968 rs->sr_err = LDAP_UNAVAILABLE;
1969 rs->sr_text = "Unable to retry";
1970 send_ldap_result( op, rs );
1974 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1980 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
1981 struct berval *binddn, struct berval *bindcred )
1983 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1987 if ( op->o_conn == NULL || op->o_do_not_cache ) {
1991 /* don't proxyAuthz if protocol is not LDAPv3 */
1992 switch ( li->li_version ) {
1997 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2003 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2004 if ( sendok & LDAP_BACK_SENDERR ) {
2005 send_ldap_result( op, rs );
2012 *binddn = slap_empty_bv;
2013 *bindcred = slap_empty_bv;
2015 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2016 ndn = op->o_conn->c_ndn;
2022 switch ( li->li_idassert_mode ) {
2023 case LDAP_BACK_IDASSERT_LEGACY:
2024 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
2025 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
2027 *binddn = li->li_idassert_authcDN;
2028 *bindcred = li->li_idassert_passwd;
2035 /* NOTE: rootdn can always idassert */
2036 if ( BER_BVISNULL( &ndn )
2037 && li->li_idassert_authz == NULL
2038 && !( li->li_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL ) )
2040 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2041 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
2042 if ( sendok & LDAP_BACK_SENDERR ) {
2043 send_ldap_result( op, rs );
2048 rs->sr_err = LDAP_SUCCESS;
2049 *binddn = slap_empty_bv;
2050 *bindcred = slap_empty_bv;
2056 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
2057 struct berval authcDN;
2059 if ( BER_BVISNULL( &ndn ) ) {
2060 authcDN = slap_empty_bv;
2065 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
2066 &authcDN, &authcDN );
2067 if ( rs->sr_err != LDAP_SUCCESS ) {
2068 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2069 if ( sendok & LDAP_BACK_SENDERR ) {
2070 send_ldap_result( op, rs );
2075 rs->sr_err = LDAP_SUCCESS;
2076 *binddn = slap_empty_bv;
2077 *bindcred = slap_empty_bv;
2085 *binddn = li->li_idassert_authcDN;
2086 *bindcred = li->li_idassert_passwd;
2096 ldap_back_proxy_authz_bind(
2100 ldap_back_send_t sendok,
2101 struct berval *binddn,
2102 struct berval *bindcred )
2104 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2109 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2110 ndn = op->o_conn->c_ndn;
2116 if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
2117 #ifdef HAVE_CYRUS_SASL
2118 void *defaults = NULL;
2119 struct berval authzID = BER_BVNULL;
2122 /* if SASL supports native authz, prepare for it */
2123 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
2124 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2126 switch ( li->li_idassert_mode ) {
2127 case LDAP_BACK_IDASSERT_OTHERID:
2128 case LDAP_BACK_IDASSERT_OTHERDN:
2129 authzID = li->li_idassert_authzID;
2132 case LDAP_BACK_IDASSERT_ANONYMOUS:
2133 BER_BVSTR( &authzID, "dn:" );
2136 case LDAP_BACK_IDASSERT_SELF:
2137 if ( BER_BVISNULL( &ndn ) ) {
2138 /* connection is not authc'd, so don't idassert */
2139 BER_BVSTR( &authzID, "dn:" );
2142 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
2143 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
2144 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
2145 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
2146 ndn.bv_val, ndn.bv_len + 1 );
2155 if ( li->li_idassert_secprops != NULL ) {
2156 rs->sr_err = ldap_set_option( lc->lc_ld,
2157 LDAP_OPT_X_SASL_SECPROPS,
2158 (void *)li->li_idassert_secprops );
2160 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
2161 rs->sr_err = LDAP_OTHER;
2162 if ( sendok & LDAP_BACK_SENDERR ) {
2163 send_ldap_result( op, rs );
2165 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2170 defaults = lutil_sasl_defaults( lc->lc_ld,
2171 li->li_idassert_sasl_mech.bv_val,
2172 li->li_idassert_sasl_realm.bv_val,
2173 li->li_idassert_authcID.bv_val,
2174 li->li_idassert_passwd.bv_val,
2176 if ( defaults == NULL ) {
2177 rs->sr_err = LDAP_OTHER;
2178 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2179 if ( sendok & LDAP_BACK_SENDERR ) {
2180 send_ldap_result( op, rs );
2185 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn->bv_val,
2186 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
2187 LDAP_SASL_QUIET, lutil_sasl_interact,
2190 switch ( rs->sr_err ) {
2192 LDAP_BACK_CONN_ISBOUND_SET( lc );
2195 case LDAP_LOCAL_ERROR:
2196 /* list client API error codes that require
2197 * to taint the connection */
2198 /* FIXME: should actually retry? */
2199 LDAP_BACK_CONN_TAINTED_SET( lc );
2204 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2205 rs->sr_err = slap_map_api2result( rs );
2206 if ( sendok & LDAP_BACK_SENDERR ) {
2207 send_ldap_result( op, rs );
2212 lutil_sasl_freedefs( defaults );
2214 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
2218 #endif /* HAVE_CYRUS_SASL */
2221 switch ( li->li_idassert_authmethod ) {
2222 case LDAP_AUTH_NONE:
2223 /* FIXME: do we really need this? */
2224 BER_BVSTR( binddn, "" );
2225 BER_BVSTR( bindcred, "" );
2228 case LDAP_AUTH_SIMPLE:
2229 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
2230 binddn->bv_val, LDAP_SASL_SIMPLE,
2231 bindcred, NULL, NULL, &msgid );
2232 rc = ldap_back_op_result( lc, op, rs, msgid,
2233 -1, ( sendok | LDAP_BACK_BINDING ) );
2238 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2239 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
2240 if ( sendok & LDAP_BACK_SENDERR ) {
2241 send_ldap_result( op, rs );
2246 if ( rc == LDAP_SUCCESS ) {
2247 /* set rebind stuff in case of successful proxyAuthz bind,
2248 * so that referral chasing is attempted using the right
2250 LDAP_BACK_CONN_ISBOUND_SET( lc );
2251 if ( !BER_BVISNULL( binddn ) ) {
2252 ber_bvreplace( &lc->lc_bound_ndn, binddn );
2255 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
2256 memset( lc->lc_cred.bv_val, 0,
2257 lc->lc_cred.bv_len );
2260 if ( LDAP_BACK_SAVECRED( li ) ) {
2261 if ( !BER_BVISNULL( bindcred ) ) {
2262 ber_bvreplace( &lc->lc_cred, bindcred );
2263 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
2267 lc->lc_cred.bv_len = 0;
2271 return LDAP_BACK_CONN_ISBOUND( lc );
2275 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
2276 * to existing server-side controls if required; if not,
2277 * the existing server-side controls are placed in *pctrls.
2278 * The caller, after using the controls in client API
2279 * operations, if ( *pctrls != op->o_ctrls ), should
2280 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
2281 * The function returns success if the control could
2282 * be added if required, or if it did nothing; in the future,
2283 * it might return some error if it failed.
2285 * if no bind took place yet, but the connection is bound
2286 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
2287 * and explicitly add proxyAuthz the control to every operation
2288 * with the dn bound to the connection as control value.
2290 * If no server-side controls are defined for the operation,
2291 * simply add the proxyAuthz control; otherwise, if the
2292 * proxyAuthz control is not already set, add it as
2295 * FIXME: is controls order significant for security?
2296 * ANSWER: controls ordering and interoperability
2297 * must be indicated by the specs of each control; if none
2298 * is specified, the order is irrelevant.
2301 ldap_back_proxy_authz_ctrl(
2304 struct berval *bound_ndn,
2306 slap_idassert_t *si,
2309 slap_idassert_mode_t mode;
2310 struct berval assertedID,
2314 rs->sr_err = SLAP_CB_CONTINUE;
2316 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
2317 * but if it is not set this test fails. We need a different
2318 * means to detect if idassert is enabled */
2319 if ( ( BER_BVISNULL( &si->si_bc.sb_authcId ) || BER_BVISEMPTY( &si->si_bc.sb_authcId ) )
2320 && ( BER_BVISNULL( &si->si_bc.sb_binddn ) || BER_BVISEMPTY( &si->si_bc.sb_binddn ) )
2321 && BER_BVISNULL( &si->si_bc.sb_saslmech ) )
2326 if ( !op->o_conn || op->o_do_not_cache || ( isroot = be_isroot( op ) ) ) {
2330 if ( op->o_tag == LDAP_REQ_BIND ) {
2331 ndn = op->o_req_ndn;
2333 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2334 ndn = op->o_conn->c_ndn;
2340 if ( si->si_mode == LDAP_BACK_IDASSERT_LEGACY ) {
2341 if ( op->o_proxy_authz ) {
2343 * FIXME: we do not want to perform proxyAuthz
2344 * on behalf of the client, because this would
2345 * be performed with "proxyauthzdn" privileges.
2347 * This might actually be too strict, since
2348 * the "proxyauthzdn" authzTo, and each entry's
2349 * authzFrom attributes may be crafted
2350 * to avoid unwanted proxyAuthz to take place.
2353 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2354 rs->sr_text = "proxyAuthz not allowed within namingContext";
2359 if ( !BER_BVISNULL( bound_ndn ) ) {
2363 if ( BER_BVISNULL( &ndn ) ) {
2367 if ( BER_BVISNULL( &si->si_bc.sb_binddn ) ) {
2371 } else if ( si->si_bc.sb_method == LDAP_AUTH_SASL ) {
2372 if ( ( si->si_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2374 /* already asserted in SASL via native authz */
2378 } else if ( si->si_authz && !isroot ) {
2380 struct berval authcDN;
2382 if ( BER_BVISNULL( &ndn ) ) {
2383 authcDN = slap_empty_bv;
2387 rc = slap_sasl_matches( op, si->si_authz,
2388 &authcDN, &authcDN );
2389 if ( rc != LDAP_SUCCESS ) {
2390 if ( si->si_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2391 /* ndn is not authorized
2392 * to use idassert */
2399 if ( op->o_proxy_authz ) {
2402 * 1) ignore the already set proxyAuthz control
2403 * 2) leave it in place, and don't set ours
2405 * 4) reject the operation
2407 * option (4) is very drastic
2408 * option (3) will make the remote server reject
2409 * the operation, thus being equivalent to (4)
2410 * option (2) will likely break the idassert
2411 * assumptions, so we cannot accept it;
2412 * option (1) means that we are contradicting
2413 * the client's reques.
2415 * I think (4) is the only correct choice.
2417 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2418 rs->sr_text = "proxyAuthz not allowed within namingContext";
2421 if ( op->o_is_auth_check ) {
2422 mode = LDAP_BACK_IDASSERT_NOASSERT;
2429 case LDAP_BACK_IDASSERT_LEGACY:
2430 /* original behavior:
2431 * assert the client's identity */
2432 case LDAP_BACK_IDASSERT_SELF:
2436 case LDAP_BACK_IDASSERT_ANONYMOUS:
2437 /* assert "anonymous" */
2438 assertedID = slap_empty_bv;
2441 case LDAP_BACK_IDASSERT_NOASSERT:
2442 /* don't assert; bind as proxyauthzdn */
2445 case LDAP_BACK_IDASSERT_OTHERID:
2446 case LDAP_BACK_IDASSERT_OTHERDN:
2447 /* assert idassert DN */
2448 assertedID = si->si_bc.sb_authzId;
2455 /* if we got here, "" is allowed to proxyAuthz */
2456 if ( BER_BVISNULL( &assertedID ) ) {
2457 assertedID = slap_empty_bv;
2460 /* don't idassert the bound DN (ITS#4497) */
2461 if ( dn_match( &assertedID, bound_ndn ) ) {
2465 ctrl->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
2467 switch ( si->si_mode ) {
2468 /* already in u:ID or dn:DN form */
2469 case LDAP_BACK_IDASSERT_OTHERID:
2470 case LDAP_BACK_IDASSERT_OTHERDN:
2471 ber_dupbv_x( &ctrl->ldctl_value, &assertedID, op->o_tmpmemctx );
2472 rs->sr_err = LDAP_SUCCESS;
2475 /* needs the dn: prefix */
2477 ctrl->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
2478 ctrl->ldctl_value.bv_val = op->o_tmpalloc( ctrl->ldctl_value.bv_len + 1,
2480 AC_MEMCPY( ctrl->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
2481 AC_MEMCPY( &ctrl->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
2482 assertedID.bv_val, assertedID.bv_len + 1 );
2483 rs->sr_err = LDAP_SUCCESS;
2487 /* Older versions of <draft-weltman-ldapv3-proxy> required
2488 * to encode the value of the authzID (and called it proxyDN);
2489 * this hack provides compatibility with those DSAs that
2490 * implement it this way */
2491 if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
2492 struct berval authzID = ctrl->ldctl_value;
2493 BerElementBuffer berbuf;
2494 BerElement *ber = (BerElement *)&berbuf;
2497 ber_init2( ber, 0, LBER_USE_DER );
2498 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2500 tag = ber_printf( ber, "O", &authzID );
2501 if ( tag == LBER_ERROR ) {
2502 rs->sr_err = LDAP_OTHER;
2506 if ( ber_flatten2( ber, &ctrl->ldctl_value, 1 ) == -1 ) {
2507 rs->sr_err = LDAP_OTHER;
2511 rs->sr_err = LDAP_SUCCESS;
2514 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2515 ber_free_buf( ber );
2517 if ( rs->sr_err != LDAP_SUCCESS ) {
2521 } else if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
2522 struct berval authzID = ctrl->ldctl_value,
2524 BerElementBuffer berbuf;
2525 BerElement *ber = (BerElement *)&berbuf;
2528 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
2529 rs->sr_err = LDAP_PROTOCOL_ERROR;
2534 tmp.bv_val += STRLENOF( "dn:" );
2535 tmp.bv_len -= STRLENOF( "dn:" );
2537 ber_init2( ber, 0, LBER_USE_DER );
2538 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2540 /* apparently, Mozilla API encodes this
2541 * as "SEQUENCE { LDAPDN }" */
2542 tag = ber_printf( ber, "{O}", &tmp );
2543 if ( tag == LBER_ERROR ) {
2544 rs->sr_err = LDAP_OTHER;
2548 if ( ber_flatten2( ber, &ctrl->ldctl_value, 1 ) == -1 ) {
2549 rs->sr_err = LDAP_OTHER;
2553 ctrl->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
2554 rs->sr_err = LDAP_SUCCESS;
2557 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2558 ber_free_buf( ber );
2560 if ( rs->sr_err != LDAP_SUCCESS ) {
2573 * if any needs to be added, it is prepended to existing ones,
2574 * in a newly allocated array. The companion function
2575 * ldap_back_controls_free() must be used to restore the original
2576 * status of op->o_ctrls.
2579 ldap_back_controls_add(
2583 LDAPControl ***pctrls )
2585 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2587 LDAPControl **ctrls = NULL;
2588 /* set to the maximum number of controls this backend can add */
2589 LDAPControl c[ 2 ] = { { 0 } };
2590 int n = 0, i, j1 = 0, j2 = 0;
2594 rs->sr_err = LDAP_SUCCESS;
2596 /* don't add controls if protocol is not LDAPv3 */
2597 switch ( li->li_version ) {
2602 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2611 /* put controls that go __before__ existing ones here */
2613 /* proxyAuthz for identity assertion */
2614 switch ( ldap_back_proxy_authz_ctrl( op, rs, &lc->lc_bound_ndn,
2615 li->li_version, &li->li_idassert, &c[ j1 ] ) )
2617 case SLAP_CB_CONTINUE:
2628 /* put controls that go __after__ existing ones here */
2630 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
2631 /* FIXME: according to <draft-wahl-ldap-session>,
2632 * the server should check if the control can be added
2633 * based on the identity of the client and so */
2635 /* session tracking */
2636 if ( LDAP_BACK_ST_REQUEST( li ) ) {
2637 switch ( slap_ctrl_session_tracking_request_add( op, rs, &c[ j1 + j2 ] ) ) {
2638 case SLAP_CB_CONTINUE:
2649 #endif /* SLAP_CONTROL_X_SESSION_TRACKING */
2651 if ( rs->sr_err == SLAP_CB_CONTINUE ) {
2652 rs->sr_err = LDAP_SUCCESS;
2655 /* if nothing to do, just bail out */
2656 if ( j1 == 0 && j2 == 0 ) {
2660 assert( j1 + j2 <= (int) (sizeof( c )/sizeof( c[0] )) );
2662 if ( op->o_ctrls ) {
2663 for ( n = 0; op->o_ctrls[ n ]; n++ )
2664 /* just count ctrls */ ;
2667 ctrls = op->o_tmpalloc( (n + j1 + j2 + 1) * sizeof( LDAPControl * ) + ( j1 + j2 ) * sizeof( LDAPControl ),
2670 ctrls[ 0 ] = (LDAPControl *)&ctrls[ n + j1 + j2 + 1 ];
2671 *ctrls[ 0 ] = c[ 0 ];
2672 for ( i = 1; i < j1; i++ ) {
2673 ctrls[ i ] = &ctrls[ 0 ][ i ];
2674 *ctrls[ i ] = c[ i ];
2679 if ( op->o_ctrls ) {
2680 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
2681 ctrls[ i + j1 ] = op->o_ctrls[ i ];
2687 ctrls[ n ] = (LDAPControl *)&ctrls[ n + j2 + 1 ] + j1;
2688 *ctrls[ n ] = c[ j1 ];
2689 for ( i = 1; i < j2; i++ ) {
2690 ctrls[ n + i ] = &ctrls[ n ][ i ];
2691 *ctrls[ n + i ] = c[ i ];
2695 ctrls[ n + j2 ] = NULL;
2698 if ( ctrls == NULL ) {
2699 ctrls = op->o_ctrls;
2708 ldap_back_controls_free( Operation *op, SlapReply *rs, LDAPControl ***pctrls )
2710 LDAPControl **ctrls = *pctrls;
2712 /* we assume that the controls added by the proxy come first,
2713 * so as soon as we find op->o_ctrls[ 0 ] we can stop */
2714 if ( ctrls && ctrls != op->o_ctrls ) {
2715 int i = 0, n = 0, n_added;
2716 LDAPControl *lower, *upper;
2718 assert( ctrls[ 0 ] != NULL );
2720 for ( n = 0; ctrls[ n ] != NULL; n++ )
2723 if ( op->o_ctrls ) {
2724 for ( i = 0; op->o_ctrls[ i ] != NULL; i++ )
2729 lower = (LDAPControl *)&ctrls[ n ];
2730 upper = &lower[ n_added ];
2732 for ( i = 0; ctrls[ i ] != NULL; i++ ) {
2733 if ( ctrls[ i ] < lower || ctrls[ i ] >= upper ) {
2734 /* original; don't touch */
2738 if ( !BER_BVISNULL( &ctrls[ i ]->ldctl_value ) ) {
2739 op->o_tmpfree( ctrls[ i ]->ldctl_value.bv_val, op->o_tmpmemctx );
2743 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2752 ldap_back_conn2str( const ldapconn_base_t *lc, char *buf, ber_len_t buflen )
2754 char tbuf[ SLAP_TEXT_BUFLEN ];
2755 char *ptr = buf, *end = buf + buflen;
2758 if ( ptr + sizeof("conn=") > end ) return -1;
2759 ptr = lutil_strcopy( ptr, "conn=" );
2761 len = ldap_back_connid2str( lc, ptr, (ber_len_t)(end - ptr) );
2763 if ( ptr >= end ) return -1;
2765 if ( !BER_BVISNULL( &lc->lcb_local_ndn ) ) {
2766 if ( ptr + sizeof(" DN=\"\"") + lc->lcb_local_ndn.bv_len > end ) return -1;
2767 ptr = lutil_strcopy( ptr, " DN=\"" );
2768 ptr = lutil_strncopy( ptr, lc->lcb_local_ndn.bv_val, lc->lcb_local_ndn.bv_len );
2772 if ( lc->lcb_create_time != 0 ) {
2773 len = snprintf( tbuf, sizeof(tbuf), "%ld", lc->lcb_create_time );
2774 if ( ptr + sizeof(" created=") + len >= end ) return -1;
2775 ptr = lutil_strcopy( ptr, " created=" );
2776 ptr = lutil_strcopy( ptr, tbuf );
2779 if ( lc->lcb_time != 0 ) {
2780 len = snprintf( tbuf, sizeof(tbuf), "%ld", lc->lcb_time );
2781 if ( ptr + sizeof(" modified=") + len >= end ) return -1;
2782 ptr = lutil_strcopy( ptr, " modified=" );
2783 ptr = lutil_strcopy( ptr, tbuf );
2786 len = snprintf( tbuf, sizeof(tbuf), "%u", lc->lcb_refcnt );
2787 if ( ptr + sizeof(" refcnt=") + len >= end ) return -1;
2788 ptr = lutil_strcopy( ptr, " refcnt=" );
2789 ptr = lutil_strcopy( ptr, tbuf );
2795 ldap_back_connid2str( const ldapconn_base_t *lc, char *buf, ber_len_t buflen )
2797 static struct berval conns[] = {
2799 BER_BVC("ROOTDN-TLS"),
2801 BER_BVC("ANON-TLS"),
2803 BER_BVC("BIND-TLS"),
2809 if ( LDAP_BACK_PCONN_ISPRIV( (const ldapconn_t *)lc ) ) {
2813 cid = (long)lc->lcb_conn;
2814 assert( cid >= LDAP_BACK_PCONN_FIRST && cid < LDAP_BACK_PCONN_LAST );
2818 if ( bv->bv_len >= buflen ) {
2819 return bv->bv_len + 1;
2823 lutil_strncopy( buf, bv->bv_val, bv->bv_len + 1 );
2826 len = snprintf( buf, buflen, "%lu", lc->lcb_conn->c_connid );