1 /* bind.c - ldap backend bind function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2010 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 #undef ldap_debug /* silence a warning in ldap-int.h */
37 #include "../../../libraries/libldap/ldap-int.h"
39 #include "lutil_ldap.h"
41 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ "2.16.840.1.113730.3.4.12"
43 #if LDAP_BACK_PRINT_CONNTREE > 0
49 { LDAP_BACK_FCONN_ISBOUND, 'B' },
50 { LDAP_BACK_FCONN_ISANON, 'A' },
51 { LDAP_BACK_FCONN_ISPRIV, 'P' },
52 { LDAP_BACK_FCONN_ISTLS, 'T' },
53 { LDAP_BACK_FCONN_BINDING, 'X' },
54 { LDAP_BACK_FCONN_TAINTED, 'E' },
55 { LDAP_BACK_FCONN_ABANDON, 'N' },
56 { LDAP_BACK_FCONN_ISIDASR, 'S' },
57 { LDAP_BACK_FCONN_CACHED, 'C' },
62 ldap_back_conn_print( ldapconn_t *lc, const char *avlstr )
64 char buf[ SLAP_TEXT_BUFLEN ];
65 char fbuf[ sizeof("BAPTIENSC") ];
68 ldap_back_conn2str( &lc->lc_base, buf, sizeof( buf ) );
69 for ( i = 0; flagsmap[ i ].c != '\0'; i++ ) {
70 if ( lc->lc_lcflags & flagsmap[i].f ) {
71 fbuf[i] = flagsmap[i].c;
79 fprintf( stderr, "lc=%p %s %s flags=0x%08x (%s)\n",
80 (void *)lc, buf, avlstr, lc->lc_lcflags, fbuf );
84 ldap_back_ravl_print( Avlnode *root, int depth )
93 ldap_back_ravl_print( root->avl_right, depth+1 );
95 for ( i = 0; i < depth; i++ ) {
96 fprintf( stderr, "-" );
100 ldap_back_conn_print( lc, avl_bf2str( root->avl_bf ) );
102 ldap_back_ravl_print( root->avl_left, depth + 1 );
105 static char* priv2str[] = {
116 ldap_back_print_conntree( ldapinfo_t *li, char *msg )
120 fprintf( stderr, "========> %s\n", msg );
122 for ( c = LDAP_BACK_PCONN_FIRST; c < LDAP_BACK_PCONN_LAST; c++ ) {
126 fprintf( stderr, " %s[%d]\n", priv2str[ c ], li->li_conn_priv[ c ].lic_num );
128 LDAP_TAILQ_FOREACH( lc, &li->li_conn_priv[ c ].lic_priv, lc_q )
130 fprintf( stderr, " [%d] ", i );
131 ldap_back_conn_print( lc, "" );
136 if ( li->li_conninfo.lai_tree == 0 ) {
137 fprintf( stderr, "\t(empty)\n" );
140 ldap_back_ravl_print( li->li_conninfo.lai_tree, 0 );
143 fprintf( stderr, "<======== %s\n", msg );
145 #endif /* LDAP_BACK_PRINT_CONNTREE */
148 ldap_back_freeconn( ldapinfo_t *li, ldapconn_t *lc, int dolock );
151 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
152 struct berval *binddn, struct berval *bindcred );
155 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
156 struct berval *binddn, struct berval *bindcred );
159 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs,
160 ldap_back_send_t sendok, struct berval *binddn, struct berval *bindcred );
163 ldap_back_prepare_conn( ldapconn_t *lc, Operation *op, SlapReply *rs,
164 ldap_back_send_t sendok );
167 ldap_back_conndnlc_cmp( const void *c1, const void *c2 );
170 ldap_back_conn_delete( ldapinfo_t *li, ldapconn_t *lc )
172 if ( LDAP_BACK_PCONN_ISPRIV( lc ) ) {
173 if ( LDAP_BACK_CONN_CACHED( lc ) ) {
174 assert( lc->lc_q.tqe_prev != NULL );
175 assert( li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num > 0 );
176 li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num--;
177 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv, lc, lc_q );
178 LDAP_TAILQ_ENTRY_INIT( lc, lc_q );
179 LDAP_BACK_CONN_CACHED_CLEAR( lc );
182 assert( LDAP_BACK_CONN_TAINTED( lc ) );
183 assert( lc->lc_q.tqe_prev == NULL );
187 ldapconn_t *tmplc = NULL;
189 if ( LDAP_BACK_CONN_CACHED( lc ) ) {
190 assert( !LDAP_BACK_CONN_TAINTED( lc ) );
191 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
192 ldap_back_conndnlc_cmp );
193 assert( tmplc == lc );
194 LDAP_BACK_CONN_CACHED_CLEAR( lc );
197 assert( LDAP_BACK_CONN_TAINTED( lc ) || tmplc == lc );
204 ldap_back_bind( Operation *op, SlapReply *rs )
206 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
209 LDAPControl **ctrls = NULL;
210 struct berval save_o_dn;
211 int save_o_do_not_cache,
214 ldap_back_send_t retrying = LDAP_BACK_RETRYING;
216 /* allow rootdn as a means to auth without the need to actually
217 * contact the proxied DSA */
218 switch ( be_rootdn_bind( op, rs ) ) {
219 case SLAP_CB_CONTINUE:
226 lc = ldap_back_getconn( op, rs, LDAP_BACK_BIND_SERR, NULL, NULL );
231 /* we can do (almost) whatever we want with this conn,
232 * because either it's temporary, or it's marked as binding */
233 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
234 ch_free( lc->lc_bound_ndn.bv_val );
235 BER_BVZERO( &lc->lc_bound_ndn );
237 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
238 memset( lc->lc_cred.bv_val, 0, lc->lc_cred.bv_len );
239 ch_free( lc->lc_cred.bv_val );
240 BER_BVZERO( &lc->lc_cred );
242 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
244 /* don't add proxyAuthz; set the bindDN */
245 save_o_dn = op->o_dn;
246 save_o_do_not_cache = op->o_do_not_cache;
247 op->o_dn = op->o_req_dn;
248 op->o_do_not_cache = 1;
251 rc = ldap_back_controls_add( op, rs, lc, &ctrls );
252 op->o_dn = save_o_dn;
253 op->o_do_not_cache = save_o_do_not_cache;
254 if ( rc != LDAP_SUCCESS ) {
255 send_ldap_result( op, rs );
256 ldap_back_release_conn( li, lc );
261 /* method is always LDAP_AUTH_SIMPLE if we got here */
262 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
264 &op->orb_cred, ctrls, NULL, &msgid );
265 /* FIXME: should we always retry, or only when piping the bind
266 * in the "override" connection pool? */
267 rc = ldap_back_op_result( lc, op, rs, msgid,
268 li->li_timeout[ SLAP_OP_BIND ],
269 LDAP_BACK_BIND_SERR | retrying );
270 if ( rc == LDAP_UNAVAILABLE && retrying ) {
271 retrying &= ~LDAP_BACK_RETRYING;
272 if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_BIND_SERR ) ) {
277 ldap_back_controls_free( op, rs, &ctrls );
279 if ( rc == LDAP_SUCCESS ) {
280 op->o_conn->c_authz_cookie = op->o_bd->be_private;
282 /* If defined, proxyAuthz will be used also when
283 * back-ldap is the authorizing backend; for this
284 * purpose, after a successful bind the connection
285 * is left for further binds, and further operations
286 * on this client connection will use a default
287 * connection with identity assertion */
288 /* NOTE: use with care */
289 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
290 ldap_back_release_conn( li, lc );
294 /* rebind is now done inside ldap_back_proxy_authz_bind()
295 * in case of success */
296 LDAP_BACK_CONN_ISBOUND_SET( lc );
297 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
299 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
300 memset( lc->lc_cred.bv_val, 0,
301 lc->lc_cred.bv_len );
304 if ( LDAP_BACK_SAVECRED( li ) ) {
305 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
306 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
309 lc->lc_cred.bv_len = 0;
313 /* must re-insert if local DN changed as result of bind */
314 if ( !LDAP_BACK_CONN_ISBOUND( lc )
315 || ( !dn_match( &op->o_req_ndn, &lc->lc_local_ndn )
316 && !LDAP_BACK_PCONN_ISPRIV( lc ) ) )
321 /* wait for all other ops to release the connection */
323 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
324 if ( lc->lc_refcnt > 1 ) {
325 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
326 ldap_pvt_thread_yield();
330 #if LDAP_BACK_PRINT_CONNTREE > 0
331 ldap_back_print_conntree( li, ">>> ldap_back_bind" );
332 #endif /* LDAP_BACK_PRINT_CONNTREE */
334 assert( lc->lc_refcnt == 1 );
335 ldap_back_conn_delete( li, lc );
337 /* delete all cached connections with the current connection */
338 if ( LDAP_BACK_SINGLECONN( li ) ) {
339 while ( ( tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc, ldap_back_conn_cmp ) ) != NULL )
341 assert( !LDAP_BACK_PCONN_ISPRIV( lc ) );
342 Debug( LDAP_DEBUG_TRACE,
343 "=>ldap_back_bind: destroying conn %lu (refcnt=%u)\n",
344 lc->lc_conn->c_connid, lc->lc_refcnt, 0 );
346 if ( tmplc->lc_refcnt != 0 ) {
348 LDAP_BACK_CONN_TAINTED_SET( tmplc );
349 LDAP_BACK_CONN_CACHED_CLEAR( tmplc );
353 * Needs a test because the handler may be corrupted,
354 * and calling ldap_unbind on a corrupted header results
355 * in a segmentation fault
357 ldap_back_conn_free( tmplc );
362 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
363 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
364 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
365 LDAP_BACK_PCONN_ROOTDN_SET( lc, op );
367 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
368 ldap_back_conndn_cmp, ldap_back_conndn_dup );
371 #if LDAP_BACK_PRINT_CONNTREE > 0
372 ldap_back_print_conntree( li, "<<< ldap_back_bind" );
373 #endif /* LDAP_BACK_PRINT_CONNTREE */
375 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
378 LDAP_BACK_CONN_CACHED_SET( lc );
382 /* duplicate; someone else successfully bound
383 * on the same connection with the same identity;
384 * we can do this because lc_refcnt == 1 */
385 ldap_back_conn_free( lc );
391 ldap_back_release_conn( li, lc );
398 * ldap_back_conndn_cmp
400 * compares two ldapconn_t based on the value of the conn pointer
401 * and of the local DN; used by avl stuff for insert, lookup
405 ldap_back_conndn_cmp( const void *c1, const void *c2 )
407 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
408 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
411 /* If local DNs don't match, it is definitely not a match */
412 /* For shared sessions, conn is NULL. Only explicitly
413 * bound sessions will have non-NULL conn.
415 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
417 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
424 * ldap_back_conndnlc_cmp
426 * compares two ldapconn_t based on the value of the conn pointer,
427 * the local DN and the lc pointer; used by avl stuff for insert, lookup
431 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
433 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
434 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
437 /* If local DNs don't match, it is definitely not a match */
438 /* For shared sessions, conn is NULL. Only explicitly
439 * bound sessions will have non-NULL conn.
441 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
443 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
445 rc = SLAP_PTRCMP( lc1, lc2 );
455 * compares two ldapconn_t based on the value of the conn pointer;
456 * used by avl stuff for delete of all conns with the same connid
459 ldap_back_conn_cmp( const void *c1, const void *c2 )
461 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
462 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
464 /* For shared sessions, conn is NULL. Only explicitly
465 * bound sessions will have non-NULL conn.
467 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
471 * ldap_back_conndn_dup
473 * returns -1 in case a duplicate ldapconn_t has been inserted;
477 ldap_back_conndn_dup( void *c1, void *c2 )
479 ldapconn_t *lc1 = (ldapconn_t *)c1;
480 ldapconn_t *lc2 = (ldapconn_t *)c2;
482 /* Cannot have more than one shared session with same DN */
483 if ( lc1->lc_conn == lc2->lc_conn &&
484 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
493 ldap_back_freeconn( ldapinfo_t *li, ldapconn_t *lc, int dolock )
496 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
499 #if LDAP_BACK_PRINT_CONNTREE > 0
500 ldap_back_print_conntree( li, ">>> ldap_back_freeconn" );
501 #endif /* LDAP_BACK_PRINT_CONNTREE */
503 (void)ldap_back_conn_delete( li, lc );
505 if ( lc->lc_refcnt == 0 ) {
506 ldap_back_conn_free( (void *)lc );
509 #if LDAP_BACK_PRINT_CONNTREE > 0
510 ldap_back_print_conntree( li, "<<< ldap_back_freeconn" );
511 #endif /* LDAP_BACK_PRINT_CONNTREE */
514 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
531 int rc = LDAP_SUCCESS;
533 /* start TLS ("tls-[try-]{start,propagate}" statements) */
534 if ( ( LDAP_BACK_USE_TLS_F( flags ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS_F( flags ) ) )
535 && !ldap_is_ldaps_url( url ) )
537 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
539 * use asynchronous StartTLS
540 * in case, chase referral (not implemented yet)
544 if ( protocol == 0 ) {
545 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
549 if ( protocol < LDAP_VERSION3 ) {
550 /* we should rather bail out... */
551 rc = LDAP_UNWILLING_TO_PERFORM;
552 *text = "invalid protocol version";
555 if ( rc == LDAP_SUCCESS ) {
556 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
559 if ( rc == LDAP_SUCCESS ) {
560 LDAPMessage *res = NULL;
563 LDAP_BACK_TV_SET( &tv );
566 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
568 rc = LDAP_UNAVAILABLE;
570 } else if ( rc == 0 ) {
571 if ( retries != LDAP_BACK_RETRY_NEVER ) {
572 ldap_pvt_thread_yield();
576 LDAP_BACK_TV_SET( &tv );
579 rc = LDAP_UNAVAILABLE;
581 } else if ( rc == LDAP_RES_EXTENDED ) {
582 struct berval *data = NULL;
584 rc = ldap_parse_extended_result( ld, res,
586 if ( rc == LDAP_SUCCESS ) {
588 rc = ldap_parse_result( ld, res, &rs.sr_err,
589 NULL, NULL, NULL, NULL, 1 );
590 if ( rc != LDAP_SUCCESS ) {
593 rc = slap_map_api2result( &rs );
596 /* FIXME: in case a referral
597 * is returned, should we try
598 * using it instead of the
600 if ( rc == LDAP_SUCCESS ) {
601 rc = ldap_install_tls( ld );
603 } else if ( rc == LDAP_REFERRAL ) {
604 rc = LDAP_UNWILLING_TO_PERFORM;
605 *text = "unwilling to chase referral returned by Start TLS exop";
609 if ( data->bv_val ) {
610 ber_memfree( data->bv_val );
624 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
626 * use synchronous StartTLS
628 rc = ldap_start_tls_s( ld, NULL, NULL );
629 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
631 /* if StartTLS is requested, only attempt it if the URL
632 * is not "ldaps://"; this may occur not only in case
633 * of misconfiguration, but also when used in the chain
634 * overlay, where the "uri" can be parsed out of a referral */
640 case LDAP_SERVER_DOWN:
644 if ( LDAP_BACK_TLS_CRITICAL_F( flags ) ) {
645 *text = "could not start TLS";
649 /* in case Start TLS is not critical */
661 #endif /* HAVE_TLS */
664 ldap_back_prepare_conn( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
666 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
670 int is_tls = op->o_conn->c_is_tls;
671 int flags = li->li_flags;
672 time_t lctime = (time_t)(-1);
674 #endif /* HAVE_TLS */
676 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
677 rs->sr_err = ldap_initialize( &ld, li->li_uri );
678 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
679 if ( rs->sr_err != LDAP_SUCCESS ) {
683 if ( li->li_urllist_f ) {
684 ldap_set_urllist_proc( ld, li->li_urllist_f, li->li_urllist_p );
687 /* Set LDAP version. This will always succeed: If the client
688 * bound with a particular version, then so can we.
690 if ( li->li_version != 0 ) {
691 version = li->li_version;
693 } else if ( op->o_protocol != 0 ) {
694 version = op->o_protocol;
697 /* assume it's an internal op; set to LDAPv3 */
698 version = LDAP_VERSION3;
700 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
702 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
703 ldap_set_option( ld, LDAP_OPT_REFERRALS,
704 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
706 if ( li->li_network_timeout > 0 ) {
709 tv.tv_sec = li->li_network_timeout;
711 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
715 if ( LDAP_BACK_CONN_ISPRIV( lc ) ) {
718 } else if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
719 sb = &li->li_idassert.si_bc;
725 if ( sb->sb_tls_do_init ) {
726 bindconf_tls_set( sb, ld );
727 } else if ( sb->sb_tls_ctx ) {
728 ldap_set_option( ld, LDAP_OPT_X_TLS_CTX, sb->sb_tls_ctx );
731 /* if required by the bindconf configuration, force TLS */
732 if ( ( sb == &li->li_acl || sb == &li->li_idassert.si_bc ) &&
735 flags |= LDAP_BACK_F_USE_TLS;
738 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
739 assert( li->li_uri_mutex_do_not_lock == 0 );
740 li->li_uri_mutex_do_not_lock = 1;
741 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
742 li->li_uri, flags, li->li_nretries, &rs->sr_text );
743 li->li_uri_mutex_do_not_lock = 0;
744 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
745 if ( rs->sr_err != LDAP_SUCCESS ) {
746 ldap_unbind_ext( ld, NULL, NULL );
747 rs->sr_text = "Start TLS failed";
750 } else if ( li->li_idle_timeout ) {
751 /* only touch when activity actually took place... */
754 #endif /* HAVE_TLS */
760 LDAP_BACK_CONN_ISTLS_SET( lc );
762 LDAP_BACK_CONN_ISTLS_CLEAR( lc );
764 if ( lctime != (time_t)(-1) ) {
765 lc->lc_time = lctime;
767 #endif /* HAVE_TLS */
770 if ( rs->sr_err != LDAP_SUCCESS ) {
771 rs->sr_err = slap_map_api2result( rs );
772 if ( sendok & LDAP_BACK_SENDERR ) {
773 if ( rs->sr_text == NULL ) {
774 rs->sr_text = "Proxy connection initialization failed";
776 send_ldap_result( op, rs );
780 if ( li->li_conn_ttl > 0 ) {
781 lc->lc_create_time = op->o_time;
792 ldap_back_send_t sendok,
793 struct berval *binddn,
794 struct berval *bindcred )
796 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
797 ldapconn_t *lc = NULL,
800 lookupconn = !( sendok & LDAP_BACK_BINDING );
802 /* if the server is quarantined, and
803 * - the current interval did not expire yet, or
804 * - no more retries should occur,
805 * don't return the connection */
806 if ( li->li_isquarantined ) {
807 slap_retry_info_t *ri = &li->li_quarantine;
810 if ( li->li_quarantine.ri_interval ) {
811 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
812 if ( li->li_isquarantined == LDAP_BACK_FQ_YES ) {
813 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
814 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
816 Debug( LDAP_DEBUG_ANY,
817 "%s: ldap_back_getconn quarantine "
818 "retry block #%d try #%d.\n",
819 op->o_log_prefix, ri->ri_idx, ri->ri_count );
820 li->li_isquarantined = LDAP_BACK_FQ_RETRYING;
823 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
827 rs->sr_err = LDAP_UNAVAILABLE;
828 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
829 rs->sr_text = "Target is quarantined";
830 send_ldap_result( op, rs );
836 /* Internal searches are privileged and shared. So is root. */
837 if ( op->o_do_not_cache || be_isroot( op ) ) {
838 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
839 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
840 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
843 struct berval tmpbinddn,
850 if ( binddn == NULL ) {
853 if ( bindcred == NULL ) {
854 bindcred = &tmpbindcred;
856 if ( op->o_tag == LDAP_REQ_BIND ) {
857 save_o_dn = op->o_dn;
858 save_o_ndn = op->o_ndn;
859 op->o_dn = op->o_req_dn;
860 op->o_ndn = op->o_req_ndn;
862 isproxyauthz = ldap_back_is_proxy_authz( op, rs, sendok, binddn, bindcred );
863 if ( op->o_tag == LDAP_REQ_BIND ) {
864 op->o_dn = save_o_dn;
865 op->o_ndn = save_o_ndn;
867 if ( isproxyauthz == -1 ) {
871 lc_curr.lc_local_ndn = op->o_ndn;
872 /* Explicit binds must not be shared;
873 * however, explicit binds are piped in a special connection
874 * when idassert is to occur with "override" set */
875 if ( op->o_tag == LDAP_REQ_BIND && !isproxyauthz ) {
876 lc_curr.lc_conn = op->o_conn;
879 if ( isproxyauthz && !( sendok & LDAP_BACK_BINDING ) ) {
880 lc_curr.lc_local_ndn = *binddn;
881 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
882 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
884 } else if ( isproxyauthz && ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) {
885 lc_curr.lc_local_ndn = slap_empty_bv;
886 LDAP_BACK_PCONN_BIND_SET( &lc_curr, op );
887 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
890 } else if ( SLAP_IS_AUTHZ_BACKEND( op ) ) {
891 lc_curr.lc_conn = op->o_conn;
894 LDAP_BACK_PCONN_ANON_SET( &lc_curr, op );
899 /* Explicit Bind requests always get their own conn */
902 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
903 if ( LDAP_BACK_PCONN_ISPRIV( &lc_curr ) ) {
904 /* lookup a conn that's not binding */
905 LDAP_TAILQ_FOREACH( lc,
906 &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv,
909 if ( !LDAP_BACK_CONN_BINDING( lc ) && lc->lc_refcnt == 0 ) {
915 if ( lc != LDAP_TAILQ_LAST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
918 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
920 LDAP_TAILQ_ENTRY_INIT( lc, lc_q );
921 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
925 } else if ( !LDAP_BACK_USE_TEMPORARIES( li )
926 && li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_num == li->li_conn_priv_max )
928 lc = LDAP_TAILQ_FIRST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv );
933 /* Searches for a ldapconn in the avl tree */
934 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
935 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
939 /* Don't reuse connections while they're still binding */
940 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
941 if ( !LDAP_BACK_USE_TEMPORARIES( li ) ) {
942 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
944 ldap_pvt_thread_yield();
951 if ( op->o_tag == LDAP_REQ_BIND ) {
952 /* right now, this is the only possible case */
953 assert( ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) );
954 LDAP_BACK_CONN_BINDING_SET( lc );
957 refcnt = ++lc->lc_refcnt;
960 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
963 /* Looks like we didn't get a bind. Open a new session... */
965 lc = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
966 lc->lc_flags = li->li_flags;
967 lc->lc_lcflags = lc_curr.lc_lcflags;
968 if ( ldap_back_prepare_conn( lc, op, rs, sendok ) != LDAP_SUCCESS ) {
973 if ( sendok & LDAP_BACK_BINDING ) {
974 LDAP_BACK_CONN_BINDING_SET( lc );
977 lc->lc_conn = lc_curr.lc_conn;
978 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
981 * the rationale is: connections as the rootdn are privileged,
982 * so acl_authcDN is to be used; however, in some cases
983 * one already configured identity assertion with a highly
984 * privileged idassert_authcDN, so if acl_authcDN is NULL
985 * and idassert_authcDN is not, use the second instead.
987 * might change in the future, because it's preferable
988 * to make clear what identity is being used, since
989 * the only drawback is that one risks to configure
990 * the same identity twice...
992 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
993 if ( BER_BVISNULL( &li->li_acl_authcDN ) && !BER_BVISNULL( &li->li_idassert_authcDN ) ) {
994 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
995 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
998 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
999 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
1001 LDAP_BACK_CONN_ISPRIV_SET( lc );
1003 } else if ( LDAP_BACK_CONN_ISIDASSERT( &lc_curr ) ) {
1004 if ( !LDAP_BACK_PCONN_ISBIND( &lc_curr ) ) {
1005 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
1006 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
1008 LDAP_BACK_CONN_ISIDASSERT_SET( lc );
1011 BER_BVZERO( &lc->lc_cred );
1012 BER_BVZERO( &lc->lc_bound_ndn );
1013 if ( !BER_BVISEMPTY( &op->o_ndn )
1014 && SLAP_IS_AUTHZ_BACKEND( op ) )
1016 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
1021 /* if start TLS failed but it was not mandatory,
1022 * check if the non-TLS connection was already
1023 * in cache; in case, destroy the newly created
1024 * connection and use the existing one */
1025 if ( LDAP_BACK_PCONN_ISTLS( lc )
1026 && !ldap_tls_inplace( lc->lc_ld ) )
1028 ldapconn_t *tmplc = NULL;
1029 int idx = LDAP_BACK_CONN2PRIV( &lc_curr ) - 1;
1031 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1032 LDAP_TAILQ_FOREACH( tmplc,
1033 &li->li_conn_priv[ idx ].lic_priv,
1036 if ( !LDAP_BACK_CONN_BINDING( tmplc ) ) {
1041 if ( tmplc != NULL ) {
1042 refcnt = ++tmplc->lc_refcnt;
1043 ldap_back_conn_free( lc );
1046 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1048 if ( tmplc != NULL ) {
1052 #endif /* HAVE_TLS */
1054 /* Inserts the newly created ldapconn in the avl tree */
1055 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1057 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1059 assert( lc->lc_refcnt == 1 );
1061 #if LDAP_BACK_PRINT_CONNTREE > 0
1062 ldap_back_print_conntree( li, ">>> ldap_back_getconn(insert)" );
1063 #endif /* LDAP_BACK_PRINT_CONNTREE */
1065 if ( LDAP_BACK_PCONN_ISPRIV( lc ) ) {
1066 if ( li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num < li->li_conn_priv_max ) {
1067 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv, lc, lc_q );
1068 li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num++;
1069 LDAP_BACK_CONN_CACHED_SET( lc );
1072 LDAP_BACK_CONN_TAINTED_SET( lc );
1077 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
1078 ldap_back_conndn_cmp, ldap_back_conndn_dup );
1079 LDAP_BACK_CONN_CACHED_SET( lc );
1082 #if LDAP_BACK_PRINT_CONNTREE > 0
1083 ldap_back_print_conntree( li, "<<< ldap_back_getconn(insert)" );
1084 #endif /* LDAP_BACK_PRINT_CONNTREE */
1086 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1088 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1089 char buf[ SLAP_TEXT_BUFLEN ];
1091 snprintf( buf, sizeof( buf ),
1092 "lc=%p inserted refcnt=%u rc=%d",
1093 (void *)lc, refcnt, rs->sr_err );
1095 Debug( LDAP_DEBUG_TRACE,
1096 "=>ldap_back_getconn: %s: %s\n",
1097 op->o_log_prefix, buf, 0 );
1100 if ( !LDAP_BACK_PCONN_ISPRIV( lc ) ) {
1101 /* Err could be -1 in case a duplicate ldapconn is inserted */
1102 switch ( rs->sr_err ) {
1107 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1108 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( li ) ) {
1109 /* duplicate: free and try to get the newly created one */
1110 ldap_back_conn_free( lc );
1115 /* taint connection, so that it'll be freed when released */
1116 LDAP_BACK_CONN_TAINTED_SET( lc );
1120 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1121 ldap_back_conn_free( lc );
1122 rs->sr_err = LDAP_OTHER;
1123 rs->sr_text = "Proxy bind collision";
1124 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1125 send_ldap_result( op, rs );
1134 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
1135 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
1139 /* let it be used, but taint/delete it so that
1140 * no-one else can look it up any further */
1141 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1143 #if LDAP_BACK_PRINT_CONNTREE > 0
1144 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1145 #endif /* LDAP_BACK_PRINT_CONNTREE */
1147 (void)ldap_back_conn_delete( li, lc );
1148 LDAP_BACK_CONN_TAINTED_SET( lc );
1150 #if LDAP_BACK_PRINT_CONNTREE > 0
1151 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1152 #endif /* LDAP_BACK_PRINT_CONNTREE */
1154 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1157 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1158 char buf[ SLAP_TEXT_BUFLEN ];
1160 snprintf( buf, sizeof( buf ),
1161 "conn %p fetched refcnt=%u%s",
1163 expiring ? " expiring" : "" );
1164 Debug( LDAP_DEBUG_TRACE,
1165 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
1171 #endif /* HAVE_TLS */
1177 ldap_back_release_conn_lock(
1183 ldapconn_t *lc = *lcp;
1186 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1188 assert( lc->lc_refcnt > 0 );
1189 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1191 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
1192 ldap_back_freeconn( li, lc, 0 );
1196 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1201 ldap_back_quarantine(
1205 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1207 slap_retry_info_t *ri = &li->li_quarantine;
1209 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
1211 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1212 time_t new_last = slap_get_time();
1214 switch ( li->li_isquarantined ) {
1215 case LDAP_BACK_FQ_NO:
1216 if ( ri->ri_last == new_last ) {
1220 Debug( LDAP_DEBUG_ANY,
1221 "%s: ldap_back_quarantine enter.\n",
1222 op->o_log_prefix, 0, 0 );
1228 case LDAP_BACK_FQ_RETRYING:
1229 Debug( LDAP_DEBUG_ANY,
1230 "%s: ldap_back_quarantine block #%d try #%d failed.\n",
1231 op->o_log_prefix, ri->ri_idx, ri->ri_count );
1234 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1235 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1246 li->li_isquarantined = LDAP_BACK_FQ_YES;
1247 ri->ri_last = new_last;
1249 } else if ( li->li_isquarantined != LDAP_BACK_FQ_NO ) {
1250 if ( ri->ri_last == slap_get_time() ) {
1254 Debug( LDAP_DEBUG_ANY,
1255 "%s: ldap_back_quarantine exit (%d) err=%d.\n",
1256 op->o_log_prefix, li->li_isquarantined, rs->sr_err );
1258 if ( li->li_quarantine_f ) {
1259 (void)li->li_quarantine_f( li, li->li_quarantine_p );
1264 li->li_isquarantined = LDAP_BACK_FQ_NO;
1268 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
1272 ldap_back_dobind_cb(
1277 ber_tag_t *tptr = op->o_callback->sc_private;
1279 rs->sr_tag = slap_req2res( op->o_tag );
1281 return SLAP_CB_CONTINUE;
1285 * ldap_back_dobind_int
1287 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1290 ldap_back_dobind_int(
1294 ldap_back_send_t sendok,
1298 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1301 struct berval binddn = slap_empty_bv,
1302 bindcred = slap_empty_bv;
1308 ber_tag_t o_tag = op->o_tag;
1309 slap_callback cb = {0};
1312 assert( lcp != NULL );
1313 assert( retries >= 0 );
1315 if ( sendok & LDAP_BACK_GETCONN ) {
1316 assert( *lcp == NULL );
1318 lc = ldap_back_getconn( op, rs, sendok, &binddn, &bindcred );
1328 assert( lc != NULL );
1332 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1335 if ( binding == 0 ) {
1336 /* check if already bound */
1337 rc = isbound = LDAP_BACK_CONN_ISBOUND( lc );
1340 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1345 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
1346 /* if someone else is about to bind it, give up and retry */
1348 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1350 ldap_pvt_thread_yield();
1354 /* otherwise this thread will bind it */
1355 LDAP_BACK_CONN_BINDING_SET( lc );
1361 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1365 * FIXME: we need to let clients use proxyAuthz
1366 * otherwise we cannot do symmetric pools of servers;
1367 * we have to live with the fact that a user can
1368 * authorize itself as any ID that is allowed
1369 * by the authzTo directive of the "proxyauthzdn".
1372 * NOTE: current Proxy Authorization specification
1373 * and implementation do not allow proxy authorization
1374 * control to be provided with Bind requests
1377 * if no bind took place yet, but the connection is bound
1378 * and the "idassert-authcDN" (or other ID) is set,
1379 * then bind as the asserting identity and explicitly
1380 * add the proxyAuthz control to every operation with the
1381 * dn bound to the connection as control value.
1382 * This is done also if this is the authorizing backend,
1383 * but the "override" flag is given to idassert.
1384 * It allows to use SASL bind and yet proxyAuthz users
1386 op->o_tag = LDAP_REQ_BIND;
1387 cb.sc_next = op->o_callback;
1388 cb.sc_private = &o_tag;
1389 cb.sc_response = ldap_back_dobind_cb;
1390 op->o_callback = &cb;
1392 if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
1393 if ( BER_BVISEMPTY( &binddn ) && BER_BVISEMPTY( &bindcred ) ) {
1394 /* if we got here, it shouldn't return result */
1395 rc = ldap_back_is_proxy_authz( op, rs,
1396 LDAP_BACK_DONTSEND, &binddn, &bindcred );
1399 rc = ldap_back_proxy_authz_bind( lc, op, rs, sendok, &binddn, &bindcred );
1403 #ifdef HAVE_CYRUS_SASL
1404 if ( LDAP_BACK_CONN_ISPRIV( lc )
1405 && li->li_acl_authmethod == LDAP_AUTH_SASL )
1407 void *defaults = NULL;
1409 if ( li->li_acl_secprops != NULL ) {
1410 rc = ldap_set_option( lc->lc_ld,
1411 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops );
1413 if ( rc != LDAP_OPT_SUCCESS ) {
1414 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
1415 "(SECPROPS,\"%s\") failed!\n",
1416 li->li_acl_secprops, 0, 0 );
1421 defaults = lutil_sasl_defaults( lc->lc_ld,
1422 li->li_acl_sasl_mech.bv_val,
1423 li->li_acl_sasl_realm.bv_val,
1424 li->li_acl_authcID.bv_val,
1425 li->li_acl_passwd.bv_val,
1427 if ( defaults == NULL ) {
1428 rs->sr_err = LDAP_OTHER;
1429 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1430 if ( sendok & LDAP_BACK_SENDERR ) {
1431 send_ldap_result( op, rs );
1436 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
1437 li->li_acl_authcDN.bv_val,
1438 li->li_acl_sasl_mech.bv_val, NULL, NULL,
1439 LDAP_SASL_QUIET, lutil_sasl_interact,
1442 lutil_sasl_freedefs( defaults );
1444 switch ( rs->sr_err ) {
1446 LDAP_BACK_CONN_ISBOUND_SET( lc );
1449 case LDAP_LOCAL_ERROR:
1450 /* list client API error codes that require
1451 * to taint the connection */
1452 /* FIXME: should actually retry? */
1453 LDAP_BACK_CONN_TAINTED_SET( lc );
1458 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1459 rs->sr_err = slap_map_api2result( rs );
1460 if ( sendok & LDAP_BACK_SENDERR ) {
1461 send_ldap_result( op, rs );
1466 if ( LDAP_BACK_QUARANTINE( li ) ) {
1467 ldap_back_quarantine( op, rs );
1472 #endif /* HAVE_CYRUS_SASL */
1475 if ( BER_BVISNULL( &lc->lc_cred ) ) {
1477 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) && !BER_BVISEMPTY( &lc->lc_bound_ndn ) ) {
1478 Debug( LDAP_DEBUG_ANY, "%s ldap_back_dobind_int: DN=\"%s\" without creds, binding anonymously",
1479 op->o_log_prefix, lc->lc_bound_ndn.bv_val, 0 );
1483 tmp_dn = lc->lc_bound_ndn.bv_val;
1485 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1487 LDAP_SASL_SIMPLE, &lc->lc_cred,
1488 NULL, NULL, &msgid );
1490 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
1491 if ( retries != LDAP_BACK_RETRY_NEVER ) {
1493 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1496 assert( lc->lc_refcnt > 0 );
1497 if ( lc->lc_refcnt == 1 ) {
1498 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1501 /* lc here must be the regular lc, reset and ready for init */
1502 rs->sr_err = ldap_back_prepare_conn( lc, op, rs, sendok );
1503 if ( rs->sr_err != LDAP_SUCCESS ) {
1504 sendok &= ~LDAP_BACK_SENDERR;
1510 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1513 if ( rs->sr_err == LDAP_SUCCESS ) {
1514 if ( retries > 0 ) {
1521 assert( lc->lc_refcnt == 1 );
1523 ldap_back_freeconn( li, lc, dolock );
1525 rs->sr_err = slap_map_api2result( rs );
1527 if ( LDAP_BACK_QUARANTINE( li ) ) {
1528 ldap_back_quarantine( op, rs );
1531 if ( rs->sr_err != LDAP_SUCCESS &&
1532 ( sendok & LDAP_BACK_SENDERR ) )
1534 if ( op->o_callback == &cb )
1535 op->o_callback = cb.sc_next;
1537 rs->sr_text = "Proxy can't contact remote server";
1538 send_ldap_result( op, rs );
1545 rc = ldap_back_op_result( lc, op, rs, msgid,
1546 -1, ( sendok | LDAP_BACK_BINDING ) );
1547 if ( rc == LDAP_SUCCESS ) {
1548 op->o_conn->c_authz_cookie = op->o_bd->be_private;
1549 LDAP_BACK_CONN_ISBOUND_SET( lc );
1553 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1554 rc = LDAP_BACK_CONN_ISBOUND( lc );
1556 ldap_back_release_conn_lock( li, lcp, dolock );
1558 } else if ( LDAP_BACK_SAVECRED( li ) ) {
1559 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1563 if ( op->o_callback == &cb )
1564 op->o_callback = cb.sc_next;
1573 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1576 ldap_back_dobind( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1578 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1580 return ldap_back_dobind_int( lcp, op, rs,
1581 ( sendok | LDAP_BACK_GETCONN ), li->li_nretries, 1 );
1585 * ldap_back_default_rebind
1587 * This is a callback used for chasing referrals using the same
1588 * credentials as the original user on this session.
1591 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1592 ber_int_t msgid, void *params )
1594 ldapconn_t *lc = (ldapconn_t *)params;
1597 /* ... otherwise we couldn't get here */
1598 assert( lc != NULL );
1600 if ( !ldap_tls_inplace( ld ) ) {
1601 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1603 const char *text = NULL;
1605 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1606 LDAP_BACK_RETRY_DEFAULT, &text );
1607 if ( rc != LDAP_SUCCESS ) {
1611 #endif /* HAVE_TLS */
1613 /* FIXME: add checks on the URL/identity? */
1615 return ldap_sasl_bind_s( ld,
1616 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1617 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1621 * ldap_back_default_urllist
1624 ldap_back_default_urllist(
1626 LDAPURLDesc **urllist,
1630 ldapinfo_t *li = (ldapinfo_t *)params;
1631 LDAPURLDesc **urltail;
1633 if ( urllist == url ) {
1634 return LDAP_SUCCESS;
1637 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1640 *urltail = *urllist;
1644 if ( !li->li_uri_mutex_do_not_lock ) {
1645 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1649 ch_free( li->li_uri );
1652 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1654 if ( !li->li_uri_mutex_do_not_lock ) {
1655 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1658 return LDAP_SUCCESS;
1667 ldap_back_send_t sendok )
1669 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1671 /* default behavior */
1672 if ( LDAP_BACK_ABANDON( li ) ) {
1673 return ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1676 if ( LDAP_BACK_IGNORE( li ) ) {
1677 return ldap_pvt_discard( lc->lc_ld, msgid );
1680 if ( LDAP_BACK_CANCEL( li ) ) {
1681 /* FIXME: asynchronous? */
1682 return ldap_cancel_s( lc->lc_ld, msgid, NULL, NULL );
1691 ldap_back_op_result(
1697 ldap_back_send_t sendok )
1699 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1704 LDAPControl **ctrls = NULL;
1707 rs->sr_matched = NULL;
1709 rs->sr_ctrls = NULL;
1711 /* if the error recorded in the reply corresponds
1712 * to a successful state, get the error from the
1713 * remote server response */
1714 if ( LDAP_ERR_OK( rs->sr_err ) ) {
1717 LDAPMessage *res = NULL;
1718 time_t stoptime = (time_t)(-1);
1719 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
1720 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
1721 const char *timeout_text = "Operation timed out";
1723 /* if timeout is not specified, compute and use
1724 * the one specific to the ongoing operation */
1725 if ( timeout == (time_t)(-1) ) {
1726 slap_op_t opidx = slap_req2op( op->o_tag );
1728 if ( opidx == SLAP_OP_SEARCH ) {
1729 if ( op->ors_tlimit <= 0 ) {
1733 timeout = op->ors_tlimit;
1734 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
1735 timeout_text = NULL;
1739 timeout = li->li_timeout[ opidx ];
1743 /* better than nothing :) */
1744 if ( timeout == 0 ) {
1745 if ( li->li_idle_timeout ) {
1746 timeout = li->li_idle_timeout;
1748 } else if ( li->li_conn_ttl ) {
1749 timeout = li->li_conn_ttl;
1754 stoptime = op->o_time + timeout;
1757 LDAP_BACK_TV_SET( &tv );
1760 /* if result parsing fails, note the failure reason */
1761 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1764 if ( timeout && slap_get_time() > stoptime ) {
1765 if ( sendok & LDAP_BACK_BINDING ) {
1766 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1769 /* let it be used, but taint/delete it so that
1770 * no-one else can look it up any further */
1771 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1773 #if LDAP_BACK_PRINT_CONNTREE > 0
1774 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1775 #endif /* LDAP_BACK_PRINT_CONNTREE */
1777 (void)ldap_back_conn_delete( li, lc );
1778 LDAP_BACK_CONN_TAINTED_SET( lc );
1780 #if LDAP_BACK_PRINT_CONNTREE > 0
1781 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1782 #endif /* LDAP_BACK_PRINT_CONNTREE */
1783 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1786 (void)ldap_back_cancel( lc, op, rs, msgid, sendok );
1788 rs->sr_err = timeout_err;
1789 rs->sr_text = timeout_text;
1794 LDAP_BACK_TV_SET( &tv );
1795 ldap_pvt_thread_yield();
1799 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1804 /* otherwise get the result; if it is not
1805 * LDAP_SUCCESS, record it in the reply
1806 * structure (this includes
1807 * LDAP_COMPARE_{TRUE|FALSE}) */
1809 /* only touch when activity actually took place... */
1810 if ( li->li_idle_timeout && lc ) {
1811 lc->lc_time = op->o_time;
1814 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1815 &match, &text, &refs, &ctrls, 1 );
1816 if ( rc == LDAP_SUCCESS ) {
1821 rs->sr_err = slap_map_api2result( rs );
1823 /* RFC 4511: referrals can only appear
1824 * if result code is LDAP_REFERRAL */
1826 && refs[ 0 ] != NULL
1827 && refs[ 0 ][ 0 ] != '\0' )
1829 if ( rs->sr_err != LDAP_REFERRAL ) {
1830 Debug( LDAP_DEBUG_ANY,
1831 "%s ldap_back_op_result: "
1832 "got referrals with err=%d\n",
1839 for ( i = 0; refs[ i ] != NULL; i++ )
1841 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1843 for ( i = 0; refs[ i ] != NULL; i++ ) {
1844 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1846 BER_BVZERO( &rs->sr_ref[ i ] );
1849 } else if ( rs->sr_err == LDAP_REFERRAL ) {
1850 Debug( LDAP_DEBUG_ANY,
1851 "%s ldap_back_op_result: "
1852 "got err=%d with null "
1853 "or empty referrals\n",
1857 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1860 if ( ctrls != NULL ) {
1861 rs->sr_ctrls = ctrls;
1866 /* if the error in the reply structure is not
1867 * LDAP_SUCCESS, try to map it from client
1868 * to server error */
1869 if ( !LDAP_ERR_OK( rs->sr_err ) ) {
1870 rs->sr_err = slap_map_api2result( rs );
1872 /* internal ops ( op->o_conn == NULL )
1873 * must not reply to client */
1874 if ( op->o_conn && !op->o_do_not_cache && match ) {
1876 /* record the (massaged) matched
1877 * DN into the reply structure */
1878 rs->sr_matched = match;
1882 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1883 if ( !( sendok & LDAP_BACK_RETRYING ) ) {
1884 if ( LDAP_BACK_QUARANTINE( li ) ) {
1885 ldap_back_quarantine( op, rs );
1887 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1888 if ( rs->sr_text == NULL ) rs->sr_text = "Proxy operation retry failed";
1889 send_ldap_result( op, rs );
1893 } else if ( op->o_conn &&
1894 ( ( ( sendok & LDAP_BACK_SENDOK ) && LDAP_ERR_OK( rs->sr_err ) )
1895 || ( ( sendok & LDAP_BACK_SENDERR ) && !LDAP_ERR_OK( rs->sr_err ) ) ) )
1897 send_ldap_result( op, rs );
1901 ldap_memfree( text );
1905 /* there can't be refs with a (successful) bind */
1907 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1912 ber_memvfree( (void **)refs );
1915 /* match should not be possible with a successful bind */
1917 if ( rs->sr_matched != match ) {
1918 free( (char *)rs->sr_matched );
1920 rs->sr_matched = NULL;
1921 ldap_memfree( match );
1924 if ( ctrls != NULL ) {
1925 if ( op->o_tag == LDAP_REQ_BIND && rs->sr_err == LDAP_SUCCESS ) {
1928 for ( i = 0; ctrls[i] != NULL; i++ );
1930 rs->sr_ctrls = op->o_tmpalloc( sizeof( LDAPControl * )*( i + 1 ),
1932 for ( i = 0; ctrls[ i ] != NULL; i++ ) {
1934 ber_len_t oidlen = strlen( ctrls[i]->ldctl_oid );
1935 ber_len_t size = sizeof( LDAPControl )
1937 + ctrls[i]->ldctl_value.bv_len + 1;
1939 rs->sr_ctrls[ i ] = op->o_tmpalloc( size, op->o_tmpmemctx );
1940 rs->sr_ctrls[ i ]->ldctl_oid = (char *)&rs->sr_ctrls[ i ][ 1 ];
1941 lutil_strcopy( rs->sr_ctrls[ i ]->ldctl_oid, ctrls[i]->ldctl_oid );
1942 rs->sr_ctrls[ i ]->ldctl_value.bv_val
1943 = (char *)&rs->sr_ctrls[ i ]->ldctl_oid[oidlen + 1];
1944 rs->sr_ctrls[ i ]->ldctl_value.bv_len
1945 = ctrls[i]->ldctl_value.bv_len;
1946 ptr = lutil_memcopy( rs->sr_ctrls[ i ]->ldctl_value.bv_val,
1947 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len );
1950 rs->sr_ctrls[ i ] = NULL;
1951 rs->sr_flags |= REP_CTRLS_MUSTBEFREED;
1954 assert( rs->sr_ctrls != NULL );
1955 rs->sr_ctrls = NULL;
1958 ldap_controls_free( ctrls );
1961 return( LDAP_ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1964 /* return true if bound, false if failed */
1966 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1968 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1971 assert( lcp != NULL );
1972 assert( *lcp != NULL );
1974 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1976 if ( (*lcp)->lc_refcnt == 1 ) {
1977 int binding = LDAP_BACK_CONN_BINDING( *lcp );
1979 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1980 Debug( LDAP_DEBUG_ANY,
1981 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1982 op->o_log_prefix, li->li_uri,
1983 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1984 "" : (*lcp)->lc_bound_ndn.bv_val );
1985 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1987 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1988 (*lcp)->lc_ld = NULL;
1989 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1991 /* lc here must be the regular lc, reset and ready for init */
1992 rc = ldap_back_prepare_conn( *lcp, op, rs, sendok );
1993 if ( rc != LDAP_SUCCESS ) {
1994 /* freeit, because lc_refcnt == 1 */
1995 (*lcp)->lc_refcnt = 0;
1996 (void)ldap_back_freeconn( li, *lcp, 0 );
2000 } else if ( ( sendok & LDAP_BACK_BINDING ) ) {
2002 LDAP_BACK_CONN_BINDING_SET( *lcp );
2007 rc = ldap_back_dobind_int( lcp, op, rs, sendok, 0, 0 );
2008 if ( rc == 0 && *lcp != NULL ) {
2009 /* freeit, because lc_refcnt == 1 */
2010 (*lcp)->lc_refcnt = 0;
2011 LDAP_BACK_CONN_TAINTED_SET( *lcp );
2012 (void)ldap_back_freeconn( li, *lcp, 0 );
2018 Debug( LDAP_DEBUG_TRACE,
2019 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
2020 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
2022 LDAP_BACK_CONN_TAINTED_SET( *lcp );
2023 ldap_back_release_conn_lock( li, lcp, 0 );
2024 assert( *lcp == NULL );
2026 if ( sendok & LDAP_BACK_SENDERR ) {
2027 rs->sr_err = LDAP_UNAVAILABLE;
2028 rs->sr_text = "Unable to retry";
2029 send_ldap_result( op, rs );
2033 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
2039 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
2040 struct berval *binddn, struct berval *bindcred )
2042 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2046 if ( op->o_conn == NULL || op->o_do_not_cache ) {
2050 /* don't proxyAuthz if protocol is not LDAPv3 */
2051 switch ( li->li_version ) {
2056 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2062 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2063 if ( sendok & LDAP_BACK_SENDERR ) {
2064 send_ldap_result( op, rs );
2071 *binddn = slap_empty_bv;
2072 *bindcred = slap_empty_bv;
2074 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2075 ndn = op->o_conn->c_ndn;
2081 switch ( li->li_idassert_mode ) {
2082 case LDAP_BACK_IDASSERT_LEGACY:
2083 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
2084 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
2086 *binddn = li->li_idassert_authcDN;
2087 *bindcred = li->li_idassert_passwd;
2094 /* NOTE: rootdn can always idassert */
2095 if ( BER_BVISNULL( &ndn )
2096 && li->li_idassert_authz == NULL
2097 && !( li->li_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL ) )
2099 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2100 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
2101 if ( sendok & LDAP_BACK_SENDERR ) {
2102 send_ldap_result( op, rs );
2107 rs->sr_err = LDAP_SUCCESS;
2108 *binddn = slap_empty_bv;
2109 *bindcred = slap_empty_bv;
2115 } else if ( !be_isroot( op ) ) {
2116 if ( li->li_idassert_passthru ) {
2117 struct berval authcDN;
2119 if ( BER_BVISNULL( &ndn ) ) {
2120 authcDN = slap_empty_bv;
2125 rs->sr_err = slap_sasl_matches( op, li->li_idassert_passthru,
2126 &authcDN, &authcDN );
2127 if ( rs->sr_err == LDAP_SUCCESS ) {
2133 if ( li->li_idassert_authz ) {
2134 struct berval authcDN;
2136 if ( BER_BVISNULL( &ndn ) ) {
2137 authcDN = slap_empty_bv;
2142 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
2143 &authcDN, &authcDN );
2144 if ( rs->sr_err != LDAP_SUCCESS ) {
2145 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2146 if ( sendok & LDAP_BACK_SENDERR ) {
2147 send_ldap_result( op, rs );
2152 rs->sr_err = LDAP_SUCCESS;
2153 *binddn = slap_empty_bv;
2154 *bindcred = slap_empty_bv;
2163 *binddn = li->li_idassert_authcDN;
2164 *bindcred = li->li_idassert_passwd;
2174 ldap_back_proxy_authz_bind(
2178 ldap_back_send_t sendok,
2179 struct berval *binddn,
2180 struct berval *bindcred )
2182 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2187 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2188 ndn = op->o_conn->c_ndn;
2194 if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
2195 #ifdef HAVE_CYRUS_SASL
2196 void *defaults = NULL;
2197 struct berval authzID = BER_BVNULL;
2200 /* if SASL supports native authz, prepare for it */
2201 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
2202 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2204 switch ( li->li_idassert_mode ) {
2205 case LDAP_BACK_IDASSERT_OTHERID:
2206 case LDAP_BACK_IDASSERT_OTHERDN:
2207 authzID = li->li_idassert_authzID;
2210 case LDAP_BACK_IDASSERT_ANONYMOUS:
2211 BER_BVSTR( &authzID, "dn:" );
2214 case LDAP_BACK_IDASSERT_SELF:
2215 if ( BER_BVISNULL( &ndn ) ) {
2216 /* connection is not authc'd, so don't idassert */
2217 BER_BVSTR( &authzID, "dn:" );
2220 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
2221 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
2222 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
2223 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
2224 ndn.bv_val, ndn.bv_len + 1 );
2233 if ( li->li_idassert_secprops != NULL ) {
2234 rs->sr_err = ldap_set_option( lc->lc_ld,
2235 LDAP_OPT_X_SASL_SECPROPS,
2236 (void *)li->li_idassert_secprops );
2238 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
2239 rs->sr_err = LDAP_OTHER;
2240 if ( sendok & LDAP_BACK_SENDERR ) {
2241 send_ldap_result( op, rs );
2243 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2248 defaults = lutil_sasl_defaults( lc->lc_ld,
2249 li->li_idassert_sasl_mech.bv_val,
2250 li->li_idassert_sasl_realm.bv_val,
2251 li->li_idassert_authcID.bv_val,
2252 li->li_idassert_passwd.bv_val,
2254 if ( defaults == NULL ) {
2255 rs->sr_err = LDAP_OTHER;
2256 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2257 if ( sendok & LDAP_BACK_SENDERR ) {
2258 send_ldap_result( op, rs );
2263 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn->bv_val,
2264 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
2265 LDAP_SASL_QUIET, lutil_sasl_interact,
2268 switch ( rs->sr_err ) {
2270 LDAP_BACK_CONN_ISBOUND_SET( lc );
2273 case LDAP_LOCAL_ERROR:
2274 /* list client API error codes that require
2275 * to taint the connection */
2276 /* FIXME: should actually retry? */
2277 LDAP_BACK_CONN_TAINTED_SET( lc );
2282 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2283 rs->sr_err = slap_map_api2result( rs );
2284 if ( sendok & LDAP_BACK_SENDERR ) {
2285 send_ldap_result( op, rs );
2290 lutil_sasl_freedefs( defaults );
2292 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
2296 #endif /* HAVE_CYRUS_SASL */
2299 switch ( li->li_idassert_authmethod ) {
2300 case LDAP_AUTH_NONE:
2301 /* FIXME: do we really need this? */
2302 BER_BVSTR( binddn, "" );
2303 BER_BVSTR( bindcred, "" );
2306 case LDAP_AUTH_SIMPLE:
2307 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
2308 binddn->bv_val, LDAP_SASL_SIMPLE,
2309 bindcred, NULL, NULL, &msgid );
2310 rc = ldap_back_op_result( lc, op, rs, msgid,
2311 -1, ( sendok | LDAP_BACK_BINDING ) );
2316 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2317 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
2318 if ( sendok & LDAP_BACK_SENDERR ) {
2319 send_ldap_result( op, rs );
2324 if ( rc == LDAP_SUCCESS ) {
2325 /* set rebind stuff in case of successful proxyAuthz bind,
2326 * so that referral chasing is attempted using the right
2328 LDAP_BACK_CONN_ISBOUND_SET( lc );
2329 op->o_conn->c_authz_cookie = op->o_bd->be_private;
2330 if ( !BER_BVISNULL( binddn ) ) {
2331 ber_bvreplace( &lc->lc_bound_ndn, binddn );
2334 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
2335 memset( lc->lc_cred.bv_val, 0,
2336 lc->lc_cred.bv_len );
2339 if ( LDAP_BACK_SAVECRED( li ) ) {
2340 if ( !BER_BVISNULL( bindcred ) ) {
2341 ber_bvreplace( &lc->lc_cred, bindcred );
2342 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
2346 lc->lc_cred.bv_len = 0;
2350 return LDAP_BACK_CONN_ISBOUND( lc );
2354 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
2355 * to existing server-side controls if required; if not,
2356 * the existing server-side controls are placed in *pctrls.
2357 * The caller, after using the controls in client API
2358 * operations, if ( *pctrls != op->o_ctrls ), should
2359 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
2360 * The function returns success if the control could
2361 * be added if required, or if it did nothing; in the future,
2362 * it might return some error if it failed.
2364 * if no bind took place yet, but the connection is bound
2365 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
2366 * and explicitly add proxyAuthz the control to every operation
2367 * with the dn bound to the connection as control value.
2369 * If no server-side controls are defined for the operation,
2370 * simply add the proxyAuthz control; otherwise, if the
2371 * proxyAuthz control is not already set, add it as
2374 * FIXME: is controls order significant for security?
2375 * ANSWER: controls ordering and interoperability
2376 * must be indicated by the specs of each control; if none
2377 * is specified, the order is irrelevant.
2380 ldap_back_proxy_authz_ctrl(
2383 struct berval *bound_ndn,
2385 slap_idassert_t *si,
2388 slap_idassert_mode_t mode;
2389 struct berval assertedID,
2393 rs->sr_err = SLAP_CB_CONTINUE;
2395 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
2396 * but if it is not set this test fails. We need a different
2397 * means to detect if idassert is enabled */
2398 if ( ( BER_BVISNULL( &si->si_bc.sb_authcId ) || BER_BVISEMPTY( &si->si_bc.sb_authcId ) )
2399 && ( BER_BVISNULL( &si->si_bc.sb_binddn ) || BER_BVISEMPTY( &si->si_bc.sb_binddn ) )
2400 && BER_BVISNULL( &si->si_bc.sb_saslmech ) )
2405 if ( !op->o_conn || op->o_do_not_cache || ( isroot = be_isroot( op ) ) ) {
2409 if ( op->o_tag == LDAP_REQ_BIND ) {
2410 ndn = op->o_req_ndn;
2412 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2413 ndn = op->o_conn->c_ndn;
2419 if ( si->si_mode == LDAP_BACK_IDASSERT_LEGACY ) {
2420 if ( op->o_proxy_authz ) {
2422 * FIXME: we do not want to perform proxyAuthz
2423 * on behalf of the client, because this would
2424 * be performed with "proxyauthzdn" privileges.
2426 * This might actually be too strict, since
2427 * the "proxyauthzdn" authzTo, and each entry's
2428 * authzFrom attributes may be crafted
2429 * to avoid unwanted proxyAuthz to take place.
2432 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2433 rs->sr_text = "proxyAuthz not allowed within namingContext";
2438 if ( !BER_BVISNULL( bound_ndn ) ) {
2442 if ( BER_BVISNULL( &ndn ) ) {
2446 if ( BER_BVISNULL( &si->si_bc.sb_binddn ) ) {
2450 } else if ( si->si_bc.sb_method == LDAP_AUTH_SASL ) {
2451 if ( ( si->si_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2453 /* already asserted in SASL via native authz */
2457 } else if ( si->si_authz && !isroot ) {
2459 struct berval authcDN;
2461 if ( BER_BVISNULL( &ndn ) ) {
2462 authcDN = slap_empty_bv;
2466 rc = slap_sasl_matches( op, si->si_authz,
2467 &authcDN, &authcDN );
2468 if ( rc != LDAP_SUCCESS ) {
2469 if ( si->si_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2470 /* ndn is not authorized
2471 * to use idassert */
2478 if ( op->o_proxy_authz ) {
2481 * 1) ignore the already set proxyAuthz control
2482 * 2) leave it in place, and don't set ours
2484 * 4) reject the operation
2486 * option (4) is very drastic
2487 * option (3) will make the remote server reject
2488 * the operation, thus being equivalent to (4)
2489 * option (2) will likely break the idassert
2490 * assumptions, so we cannot accept it;
2491 * option (1) means that we are contradicting
2492 * the client's reques.
2494 * I think (4) is the only correct choice.
2496 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2497 rs->sr_text = "proxyAuthz not allowed within namingContext";
2500 if ( op->o_is_auth_check ) {
2501 mode = LDAP_BACK_IDASSERT_NOASSERT;
2508 case LDAP_BACK_IDASSERT_LEGACY:
2509 /* original behavior:
2510 * assert the client's identity */
2511 case LDAP_BACK_IDASSERT_SELF:
2515 case LDAP_BACK_IDASSERT_ANONYMOUS:
2516 /* assert "anonymous" */
2517 assertedID = slap_empty_bv;
2520 case LDAP_BACK_IDASSERT_NOASSERT:
2521 /* don't assert; bind as proxyauthzdn */
2524 case LDAP_BACK_IDASSERT_OTHERID:
2525 case LDAP_BACK_IDASSERT_OTHERDN:
2526 /* assert idassert DN */
2527 assertedID = si->si_bc.sb_authzId;
2534 /* if we got here, "" is allowed to proxyAuthz */
2535 if ( BER_BVISNULL( &assertedID ) ) {
2536 assertedID = slap_empty_bv;
2539 /* don't idassert the bound DN (ITS#4497) */
2540 if ( dn_match( &assertedID, bound_ndn ) ) {
2544 ctrl->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
2545 ctrl->ldctl_iscritical = ( ( si->si_flags & LDAP_BACK_AUTH_PROXYAUTHZ_CRITICAL ) == LDAP_BACK_AUTH_PROXYAUTHZ_CRITICAL );
2547 switch ( si->si_mode ) {
2548 /* already in u:ID or dn:DN form */
2549 case LDAP_BACK_IDASSERT_OTHERID:
2550 case LDAP_BACK_IDASSERT_OTHERDN:
2551 ber_dupbv_x( &ctrl->ldctl_value, &assertedID, op->o_tmpmemctx );
2552 rs->sr_err = LDAP_SUCCESS;
2555 /* needs the dn: prefix */
2557 ctrl->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
2558 ctrl->ldctl_value.bv_val = op->o_tmpalloc( ctrl->ldctl_value.bv_len + 1,
2560 AC_MEMCPY( ctrl->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
2561 AC_MEMCPY( &ctrl->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
2562 assertedID.bv_val, assertedID.bv_len + 1 );
2563 rs->sr_err = LDAP_SUCCESS;
2567 /* Older versions of <draft-weltman-ldapv3-proxy> required
2568 * to encode the value of the authzID (and called it proxyDN);
2569 * this hack provides compatibility with those DSAs that
2570 * implement it this way */
2571 if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
2572 struct berval authzID = ctrl->ldctl_value;
2573 BerElementBuffer berbuf;
2574 BerElement *ber = (BerElement *)&berbuf;
2577 ber_init2( ber, 0, LBER_USE_DER );
2578 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2580 tag = ber_printf( ber, "O", &authzID );
2581 if ( tag == LBER_ERROR ) {
2582 rs->sr_err = LDAP_OTHER;
2586 if ( ber_flatten2( ber, &ctrl->ldctl_value, 1 ) == -1 ) {
2587 rs->sr_err = LDAP_OTHER;
2591 rs->sr_err = LDAP_SUCCESS;
2594 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2595 ber_free_buf( ber );
2597 if ( rs->sr_err != LDAP_SUCCESS ) {
2601 } else if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
2602 struct berval authzID = ctrl->ldctl_value,
2604 BerElementBuffer berbuf;
2605 BerElement *ber = (BerElement *)&berbuf;
2608 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
2609 rs->sr_err = LDAP_PROTOCOL_ERROR;
2614 tmp.bv_val += STRLENOF( "dn:" );
2615 tmp.bv_len -= STRLENOF( "dn:" );
2617 ber_init2( ber, 0, LBER_USE_DER );
2618 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2620 /* apparently, Mozilla API encodes this
2621 * as "SEQUENCE { LDAPDN }" */
2622 tag = ber_printf( ber, "{O}", &tmp );
2623 if ( tag == LBER_ERROR ) {
2624 rs->sr_err = LDAP_OTHER;
2628 if ( ber_flatten2( ber, &ctrl->ldctl_value, 1 ) == -1 ) {
2629 rs->sr_err = LDAP_OTHER;
2633 ctrl->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
2634 rs->sr_err = LDAP_SUCCESS;
2637 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2638 ber_free_buf( ber );
2640 if ( rs->sr_err != LDAP_SUCCESS ) {
2653 * if any needs to be added, it is prepended to existing ones,
2654 * in a newly allocated array. The companion function
2655 * ldap_back_controls_free() must be used to restore the original
2656 * status of op->o_ctrls.
2659 ldap_back_controls_add(
2663 LDAPControl ***pctrls )
2665 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2667 LDAPControl **ctrls = NULL;
2668 /* set to the maximum number of controls this backend can add */
2669 LDAPControl c[ 2 ] = { { 0 } };
2670 int n = 0, i, j1 = 0, j2 = 0;
2674 rs->sr_err = LDAP_SUCCESS;
2676 /* don't add controls if protocol is not LDAPv3 */
2677 switch ( li->li_version ) {
2682 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2691 /* put controls that go __before__ existing ones here */
2693 /* proxyAuthz for identity assertion */
2694 switch ( ldap_back_proxy_authz_ctrl( op, rs, &lc->lc_bound_ndn,
2695 li->li_version, &li->li_idassert, &c[ j1 ] ) )
2697 case SLAP_CB_CONTINUE:
2708 /* put controls that go __after__ existing ones here */
2710 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
2711 /* FIXME: according to <draft-wahl-ldap-session>,
2712 * the server should check if the control can be added
2713 * based on the identity of the client and so */
2715 /* session tracking */
2716 if ( LDAP_BACK_ST_REQUEST( li ) ) {
2717 switch ( slap_ctrl_session_tracking_request_add( op, rs, &c[ j1 + j2 ] ) ) {
2718 case SLAP_CB_CONTINUE:
2729 #endif /* SLAP_CONTROL_X_SESSION_TRACKING */
2731 if ( rs->sr_err == SLAP_CB_CONTINUE ) {
2732 rs->sr_err = LDAP_SUCCESS;
2735 /* if nothing to do, just bail out */
2736 if ( j1 == 0 && j2 == 0 ) {
2740 assert( j1 + j2 <= (int) (sizeof( c )/sizeof( c[0] )) );
2742 if ( op->o_ctrls ) {
2743 for ( n = 0; op->o_ctrls[ n ]; n++ )
2744 /* just count ctrls */ ;
2747 ctrls = op->o_tmpalloc( (n + j1 + j2 + 1) * sizeof( LDAPControl * ) + ( j1 + j2 ) * sizeof( LDAPControl ),
2750 ctrls[ 0 ] = (LDAPControl *)&ctrls[ n + j1 + j2 + 1 ];
2751 *ctrls[ 0 ] = c[ 0 ];
2752 for ( i = 1; i < j1; i++ ) {
2753 ctrls[ i ] = &ctrls[ 0 ][ i ];
2754 *ctrls[ i ] = c[ i ];
2759 if ( op->o_ctrls ) {
2760 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
2761 ctrls[ i + j1 ] = op->o_ctrls[ i ];
2767 ctrls[ n ] = (LDAPControl *)&ctrls[ n + j2 + 1 ] + j1;
2768 *ctrls[ n ] = c[ j1 ];
2769 for ( i = 1; i < j2; i++ ) {
2770 ctrls[ n + i ] = &ctrls[ n ][ i ];
2771 *ctrls[ n + i ] = c[ i ];
2775 ctrls[ n + j2 ] = NULL;
2778 if ( ctrls == NULL ) {
2779 ctrls = op->o_ctrls;
2788 ldap_back_controls_free( Operation *op, SlapReply *rs, LDAPControl ***pctrls )
2790 LDAPControl **ctrls = *pctrls;
2792 /* we assume that the controls added by the proxy come first,
2793 * so as soon as we find op->o_ctrls[ 0 ] we can stop */
2794 if ( ctrls && ctrls != op->o_ctrls ) {
2795 int i = 0, n = 0, n_added;
2796 LDAPControl *lower, *upper;
2798 assert( ctrls[ 0 ] != NULL );
2800 for ( n = 0; ctrls[ n ] != NULL; n++ )
2803 if ( op->o_ctrls ) {
2804 for ( i = 0; op->o_ctrls[ i ] != NULL; i++ )
2809 lower = (LDAPControl *)&ctrls[ n ];
2810 upper = &lower[ n_added ];
2812 for ( i = 0; ctrls[ i ] != NULL; i++ ) {
2813 if ( ctrls[ i ] < lower || ctrls[ i ] >= upper ) {
2814 /* original; don't touch */
2818 if ( !BER_BVISNULL( &ctrls[ i ]->ldctl_value ) ) {
2819 op->o_tmpfree( ctrls[ i ]->ldctl_value.bv_val, op->o_tmpmemctx );
2823 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2832 ldap_back_conn2str( const ldapconn_base_t *lc, char *buf, ber_len_t buflen )
2834 char tbuf[ SLAP_TEXT_BUFLEN ];
2835 char *ptr = buf, *end = buf + buflen;
2838 if ( ptr + sizeof("conn=") > end ) return -1;
2839 ptr = lutil_strcopy( ptr, "conn=" );
2841 len = ldap_back_connid2str( lc, ptr, (ber_len_t)(end - ptr) );
2843 if ( ptr >= end ) return -1;
2845 if ( !BER_BVISNULL( &lc->lcb_local_ndn ) ) {
2846 if ( ptr + sizeof(" DN=\"\"") + lc->lcb_local_ndn.bv_len > end ) return -1;
2847 ptr = lutil_strcopy( ptr, " DN=\"" );
2848 ptr = lutil_strncopy( ptr, lc->lcb_local_ndn.bv_val, lc->lcb_local_ndn.bv_len );
2852 if ( lc->lcb_create_time != 0 ) {
2853 len = snprintf( tbuf, sizeof(tbuf), "%ld", lc->lcb_create_time );
2854 if ( ptr + sizeof(" created=") + len >= end ) return -1;
2855 ptr = lutil_strcopy( ptr, " created=" );
2856 ptr = lutil_strcopy( ptr, tbuf );
2859 if ( lc->lcb_time != 0 ) {
2860 len = snprintf( tbuf, sizeof(tbuf), "%ld", lc->lcb_time );
2861 if ( ptr + sizeof(" modified=") + len >= end ) return -1;
2862 ptr = lutil_strcopy( ptr, " modified=" );
2863 ptr = lutil_strcopy( ptr, tbuf );
2866 len = snprintf( tbuf, sizeof(tbuf), "%u", lc->lcb_refcnt );
2867 if ( ptr + sizeof(" refcnt=") + len >= end ) return -1;
2868 ptr = lutil_strcopy( ptr, " refcnt=" );
2869 ptr = lutil_strcopy( ptr, tbuf );
2875 ldap_back_connid2str( const ldapconn_base_t *lc, char *buf, ber_len_t buflen )
2877 static struct berval conns[] = {
2879 BER_BVC("ROOTDN-TLS"),
2881 BER_BVC("ANON-TLS"),
2883 BER_BVC("BIND-TLS"),
2889 if ( LDAP_BACK_PCONN_ISPRIV( (const ldapconn_t *)lc ) ) {
2893 cid = (long)lc->lcb_conn;
2894 assert( cid >= LDAP_BACK_PCONN_FIRST && cid < LDAP_BACK_PCONN_LAST );
2898 if ( bv->bv_len >= buflen ) {
2899 return bv->bv_len + 1;
2903 lutil_strncopy( buf, bv->bv_val, bv->bv_len + 1 );
2906 len = snprintf( buf, buflen, "%lu", lc->lcb_conn->c_connid );