1 /* bind.c - ldap backend bind function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2012 The OpenLDAP Foundation.
6 * Portions Copyright 2000-2003 Pierangelo Masarati.
7 * Portions Copyright 1999-2003 Howard Chu.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
19 * This work was initially developed by Howard Chu for inclusion
20 * in OpenLDAP Software and subsequently enhanced by Pierangelo
29 #include <ac/socket.h>
30 #include <ac/string.h>
34 #include "back-ldap.h"
36 #include "lutil_ldap.h"
38 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ "2.16.840.1.113730.3.4.12"
41 #define SLAP_AUTH_DN 1
44 #if LDAP_BACK_PRINT_CONNTREE > 0
50 { LDAP_BACK_FCONN_ISBOUND, 'B' },
51 { LDAP_BACK_FCONN_ISANON, 'A' },
52 { LDAP_BACK_FCONN_ISPRIV, 'P' },
53 { LDAP_BACK_FCONN_ISTLS, 'T' },
54 { LDAP_BACK_FCONN_BINDING, 'X' },
55 { LDAP_BACK_FCONN_TAINTED, 'E' },
56 { LDAP_BACK_FCONN_ABANDON, 'N' },
57 { LDAP_BACK_FCONN_ISIDASR, 'S' },
58 { LDAP_BACK_FCONN_CACHED, 'C' },
63 ldap_back_conn_print( ldapconn_t *lc, const char *avlstr )
65 char buf[ SLAP_TEXT_BUFLEN ];
66 char fbuf[ sizeof("BAPTIENSC") ];
69 ldap_back_conn2str( &lc->lc_base, buf, sizeof( buf ) );
70 for ( i = 0; flagsmap[ i ].c != '\0'; i++ ) {
71 if ( lc->lc_lcflags & flagsmap[i].f ) {
72 fbuf[i] = flagsmap[i].c;
80 fprintf( stderr, "lc=%p %s %s flags=0x%08x (%s)\n",
81 (void *)lc, buf, avlstr, lc->lc_lcflags, fbuf );
85 ldap_back_ravl_print( Avlnode *root, int depth )
94 ldap_back_ravl_print( root->avl_right, depth+1 );
96 for ( i = 0; i < depth; i++ ) {
97 fprintf( stderr, "-" );
101 ldap_back_conn_print( lc, avl_bf2str( root->avl_bf ) );
103 ldap_back_ravl_print( root->avl_left, depth + 1 );
106 static char* priv2str[] = {
117 ldap_back_print_conntree( ldapinfo_t *li, char *msg )
121 fprintf( stderr, "========> %s\n", msg );
123 for ( c = LDAP_BACK_PCONN_FIRST; c < LDAP_BACK_PCONN_LAST; c++ ) {
127 fprintf( stderr, " %s[%d]\n", priv2str[ c ], li->li_conn_priv[ c ].lic_num );
129 LDAP_TAILQ_FOREACH( lc, &li->li_conn_priv[ c ].lic_priv, lc_q )
131 fprintf( stderr, " [%d] ", i );
132 ldap_back_conn_print( lc, "" );
137 if ( li->li_conninfo.lai_tree == 0 ) {
138 fprintf( stderr, "\t(empty)\n" );
141 ldap_back_ravl_print( li->li_conninfo.lai_tree, 0 );
144 fprintf( stderr, "<======== %s\n", msg );
146 #endif /* LDAP_BACK_PRINT_CONNTREE */
149 ldap_back_freeconn( ldapinfo_t *li, ldapconn_t *lc, int dolock );
152 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
153 struct berval *binddn, struct berval *bindcred );
156 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
157 struct berval *binddn, struct berval *bindcred );
160 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs,
161 ldap_back_send_t sendok, struct berval *binddn, struct berval *bindcred );
164 ldap_back_prepare_conn( ldapconn_t *lc, Operation *op, SlapReply *rs,
165 ldap_back_send_t sendok );
168 ldap_back_conndnlc_cmp( const void *c1, const void *c2 );
171 ldap_back_conn_delete( ldapinfo_t *li, ldapconn_t *lc )
173 if ( LDAP_BACK_PCONN_ISPRIV( lc ) ) {
174 if ( LDAP_BACK_CONN_CACHED( lc ) ) {
175 assert( lc->lc_q.tqe_prev != NULL );
176 assert( li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num > 0 );
177 li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num--;
178 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv, lc, lc_q );
179 LDAP_TAILQ_ENTRY_INIT( lc, lc_q );
180 LDAP_BACK_CONN_CACHED_CLEAR( lc );
183 assert( LDAP_BACK_CONN_TAINTED( lc ) );
184 assert( lc->lc_q.tqe_prev == NULL );
188 ldapconn_t *tmplc = NULL;
190 if ( LDAP_BACK_CONN_CACHED( lc ) ) {
191 assert( !LDAP_BACK_CONN_TAINTED( lc ) );
192 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
193 ldap_back_conndnlc_cmp );
194 assert( tmplc == lc );
195 LDAP_BACK_CONN_CACHED_CLEAR( lc );
198 assert( LDAP_BACK_CONN_TAINTED( lc ) || tmplc == lc );
205 ldap_back_bind( Operation *op, SlapReply *rs )
207 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
210 LDAPControl **ctrls = NULL;
211 struct berval save_o_dn;
212 int save_o_do_not_cache,
215 ldap_back_send_t retrying = LDAP_BACK_RETRYING;
217 /* allow rootdn as a means to auth without the need to actually
218 * contact the proxied DSA */
219 switch ( be_rootdn_bind( op, rs ) ) {
220 case SLAP_CB_CONTINUE:
227 lc = ldap_back_getconn( op, rs, LDAP_BACK_BIND_SERR, NULL, NULL );
232 /* we can do (almost) whatever we want with this conn,
233 * because either it's temporary, or it's marked as binding */
234 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
235 ch_free( lc->lc_bound_ndn.bv_val );
236 BER_BVZERO( &lc->lc_bound_ndn );
238 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
239 memset( lc->lc_cred.bv_val, 0, lc->lc_cred.bv_len );
240 ch_free( lc->lc_cred.bv_val );
241 BER_BVZERO( &lc->lc_cred );
243 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
245 /* don't add proxyAuthz; set the bindDN */
246 save_o_dn = op->o_dn;
247 save_o_do_not_cache = op->o_do_not_cache;
248 op->o_dn = op->o_req_dn;
249 op->o_do_not_cache = 1;
252 rc = ldap_back_controls_add( op, rs, lc, &ctrls );
253 op->o_dn = save_o_dn;
254 op->o_do_not_cache = save_o_do_not_cache;
255 if ( rc != LDAP_SUCCESS ) {
256 send_ldap_result( op, rs );
257 ldap_back_release_conn( li, lc );
262 /* method is always LDAP_AUTH_SIMPLE if we got here */
263 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
265 &op->orb_cred, ctrls, NULL, &msgid );
266 /* FIXME: should we always retry, or only when piping the bind
267 * in the "override" connection pool? */
268 rc = ldap_back_op_result( lc, op, rs, msgid,
269 li->li_timeout[ SLAP_OP_BIND ],
270 LDAP_BACK_BIND_SERR | retrying );
271 if ( rc == LDAP_UNAVAILABLE && retrying ) {
272 retrying &= ~LDAP_BACK_RETRYING;
273 if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_BIND_SERR ) ) {
278 ldap_pvt_thread_mutex_lock( &li->li_counter_mutex );
279 ldap_pvt_mp_add( li->li_ops_completed[ SLAP_OP_BIND ], 1 );
280 ldap_pvt_thread_mutex_unlock( &li->li_counter_mutex );
282 ldap_back_controls_free( op, rs, &ctrls );
284 if ( rc == LDAP_SUCCESS ) {
285 op->o_conn->c_authz_cookie = op->o_bd->be_private;
287 /* If defined, proxyAuthz will be used also when
288 * back-ldap is the authorizing backend; for this
289 * purpose, after a successful bind the connection
290 * is left for further binds, and further operations
291 * on this client connection will use a default
292 * connection with identity assertion */
293 /* NOTE: use with care */
294 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
295 ldap_back_release_conn( li, lc );
299 /* rebind is now done inside ldap_back_proxy_authz_bind()
300 * in case of success */
301 LDAP_BACK_CONN_ISBOUND_SET( lc );
302 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
304 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
305 memset( lc->lc_cred.bv_val, 0,
306 lc->lc_cred.bv_len );
309 if ( LDAP_BACK_SAVECRED( li ) ) {
310 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
311 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
314 lc->lc_cred.bv_len = 0;
318 /* must re-insert if local DN changed as result of bind */
319 if ( !LDAP_BACK_CONN_ISBOUND( lc )
320 || ( !dn_match( &op->o_req_ndn, &lc->lc_local_ndn )
321 && !LDAP_BACK_PCONN_ISPRIV( lc ) ) )
326 /* wait for all other ops to release the connection */
328 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
329 if ( lc->lc_refcnt > 1 ) {
330 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
331 ldap_pvt_thread_yield();
335 #if LDAP_BACK_PRINT_CONNTREE > 0
336 ldap_back_print_conntree( li, ">>> ldap_back_bind" );
337 #endif /* LDAP_BACK_PRINT_CONNTREE */
339 assert( lc->lc_refcnt == 1 );
340 ldap_back_conn_delete( li, lc );
342 /* delete all cached connections with the current connection */
343 if ( LDAP_BACK_SINGLECONN( li ) ) {
344 while ( ( tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc, ldap_back_conn_cmp ) ) != NULL )
346 assert( !LDAP_BACK_PCONN_ISPRIV( lc ) );
347 Debug( LDAP_DEBUG_TRACE,
348 "=>ldap_back_bind: destroying conn %lu (refcnt=%u)\n",
349 lc->lc_conn->c_connid, lc->lc_refcnt, 0 );
351 if ( tmplc->lc_refcnt != 0 ) {
353 LDAP_BACK_CONN_TAINTED_SET( tmplc );
354 LDAP_BACK_CONN_CACHED_CLEAR( tmplc );
358 * Needs a test because the handler may be corrupted,
359 * and calling ldap_unbind on a corrupted header results
360 * in a segmentation fault
362 ldap_back_conn_free( tmplc );
367 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
368 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
369 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
370 LDAP_BACK_PCONN_ROOTDN_SET( lc, op );
372 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
373 ldap_back_conndn_cmp, ldap_back_conndn_dup );
376 #if LDAP_BACK_PRINT_CONNTREE > 0
377 ldap_back_print_conntree( li, "<<< ldap_back_bind" );
378 #endif /* LDAP_BACK_PRINT_CONNTREE */
380 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
383 LDAP_BACK_CONN_CACHED_SET( lc );
387 /* duplicate; someone else successfully bound
388 * on the same connection with the same identity;
389 * we can do this because lc_refcnt == 1 */
390 ldap_back_conn_free( lc );
396 ldap_back_release_conn( li, lc );
403 * ldap_back_conndn_cmp
405 * compares two ldapconn_t based on the value of the conn pointer
406 * and of the local DN; used by avl stuff for insert, lookup
410 ldap_back_conndn_cmp( const void *c1, const void *c2 )
412 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
413 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
416 /* If local DNs don't match, it is definitely not a match */
417 /* For shared sessions, conn is NULL. Only explicitly
418 * bound sessions will have non-NULL conn.
420 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
422 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
429 * ldap_back_conndnlc_cmp
431 * compares two ldapconn_t based on the value of the conn pointer,
432 * the local DN and the lc pointer; used by avl stuff for insert, lookup
436 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
438 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
439 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
442 /* If local DNs don't match, it is definitely not a match */
443 /* For shared sessions, conn is NULL. Only explicitly
444 * bound sessions will have non-NULL conn.
446 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
448 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
450 rc = SLAP_PTRCMP( lc1, lc2 );
460 * compares two ldapconn_t based on the value of the conn pointer;
461 * used by avl stuff for delete of all conns with the same connid
464 ldap_back_conn_cmp( const void *c1, const void *c2 )
466 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
467 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
469 /* For shared sessions, conn is NULL. Only explicitly
470 * bound sessions will have non-NULL conn.
472 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
476 * ldap_back_conndn_dup
478 * returns -1 in case a duplicate ldapconn_t has been inserted;
482 ldap_back_conndn_dup( void *c1, void *c2 )
484 ldapconn_t *lc1 = (ldapconn_t *)c1;
485 ldapconn_t *lc2 = (ldapconn_t *)c2;
487 /* Cannot have more than one shared session with same DN */
488 if ( lc1->lc_conn == lc2->lc_conn &&
489 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
498 ldap_back_freeconn( ldapinfo_t *li, ldapconn_t *lc, int dolock )
501 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
504 #if LDAP_BACK_PRINT_CONNTREE > 0
505 ldap_back_print_conntree( li, ">>> ldap_back_freeconn" );
506 #endif /* LDAP_BACK_PRINT_CONNTREE */
508 (void)ldap_back_conn_delete( li, lc );
510 if ( lc->lc_refcnt == 0 ) {
511 ldap_back_conn_free( (void *)lc );
514 #if LDAP_BACK_PRINT_CONNTREE > 0
515 ldap_back_print_conntree( li, "<<< ldap_back_freeconn" );
516 #endif /* LDAP_BACK_PRINT_CONNTREE */
519 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
536 int rc = LDAP_SUCCESS;
538 /* start TLS ("tls-[try-]{start,propagate}" statements) */
539 if ( ( LDAP_BACK_USE_TLS_F( flags ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS_F( flags ) ) )
540 && !ldap_is_ldaps_url( url ) )
542 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
544 * use asynchronous StartTLS
545 * in case, chase referral (not implemented yet)
549 if ( protocol == 0 ) {
550 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
554 if ( protocol < LDAP_VERSION3 ) {
555 /* we should rather bail out... */
556 rc = LDAP_UNWILLING_TO_PERFORM;
557 *text = "invalid protocol version";
560 if ( rc == LDAP_SUCCESS ) {
561 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
564 if ( rc == LDAP_SUCCESS ) {
565 LDAPMessage *res = NULL;
568 LDAP_BACK_TV_SET( &tv );
571 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
573 rc = LDAP_UNAVAILABLE;
575 } else if ( rc == 0 ) {
576 if ( retries != LDAP_BACK_RETRY_NEVER ) {
577 ldap_pvt_thread_yield();
581 LDAP_BACK_TV_SET( &tv );
584 rc = LDAP_UNAVAILABLE;
586 } else if ( rc == LDAP_RES_EXTENDED ) {
587 struct berval *data = NULL;
589 rc = ldap_parse_extended_result( ld, res,
591 if ( rc == LDAP_SUCCESS ) {
593 rc = ldap_parse_result( ld, res, &rs.sr_err,
594 NULL, NULL, NULL, NULL, 1 );
595 if ( rc != LDAP_SUCCESS ) {
598 rc = slap_map_api2result( &rs );
601 /* FIXME: in case a referral
602 * is returned, should we try
603 * using it instead of the
605 if ( rc == LDAP_SUCCESS ) {
606 rc = ldap_install_tls( ld );
608 } else if ( rc == LDAP_REFERRAL ) {
609 rc = LDAP_UNWILLING_TO_PERFORM;
610 *text = "unwilling to chase referral returned by Start TLS exop";
614 if ( data->bv_val ) {
615 ber_memfree( data->bv_val );
629 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
631 * use synchronous StartTLS
633 rc = ldap_start_tls_s( ld, NULL, NULL );
634 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
636 /* if StartTLS is requested, only attempt it if the URL
637 * is not "ldaps://"; this may occur not only in case
638 * of misconfiguration, but also when used in the chain
639 * overlay, where the "uri" can be parsed out of a referral */
645 case LDAP_SERVER_DOWN:
649 if ( LDAP_BACK_TLS_CRITICAL_F( flags ) ) {
650 *text = "could not start TLS";
654 /* in case Start TLS is not critical */
666 #endif /* HAVE_TLS */
669 ldap_back_prepare_conn( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
671 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
675 int is_tls = op->o_conn->c_is_tls;
676 int flags = li->li_flags;
677 time_t lctime = (time_t)(-1);
679 #endif /* HAVE_TLS */
681 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
682 rs->sr_err = ldap_initialize( &ld, li->li_uri );
683 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
684 if ( rs->sr_err != LDAP_SUCCESS ) {
688 if ( li->li_urllist_f ) {
689 ldap_set_urllist_proc( ld, li->li_urllist_f, li->li_urllist_p );
692 /* Set LDAP version. This will always succeed: If the client
693 * bound with a particular version, then so can we.
695 if ( li->li_version != 0 ) {
696 version = li->li_version;
698 } else if ( op->o_protocol != 0 ) {
699 version = op->o_protocol;
702 /* assume it's an internal op; set to LDAPv3 */
703 version = LDAP_VERSION3;
705 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
707 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
708 ldap_set_option( ld, LDAP_OPT_REFERRALS,
709 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
711 if ( li->li_network_timeout > 0 ) {
714 tv.tv_sec = li->li_network_timeout;
716 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
720 if ( LDAP_BACK_CONN_ISPRIV( lc ) ) {
721 /* See "rationale" comment in ldap_back_getconn() */
722 if ( li->li_acl_authmethod == LDAP_AUTH_NONE &&
723 li->li_idassert_authmethod != LDAP_AUTH_NONE )
724 sb = &li->li_idassert.si_bc;
728 } else if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
729 sb = &li->li_idassert.si_bc;
735 if ( sb->sb_tls_do_init ) {
736 bindconf_tls_set( sb, ld );
737 } else if ( sb->sb_tls_ctx ) {
738 ldap_set_option( ld, LDAP_OPT_X_TLS_CTX, sb->sb_tls_ctx );
741 /* if required by the bindconf configuration, force TLS */
742 if ( ( sb == &li->li_acl || sb == &li->li_idassert.si_bc ) &&
745 flags |= LDAP_BACK_F_USE_TLS;
748 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
749 assert( li->li_uri_mutex_do_not_lock == 0 );
750 li->li_uri_mutex_do_not_lock = 1;
751 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
752 li->li_uri, flags, li->li_nretries, &rs->sr_text );
753 li->li_uri_mutex_do_not_lock = 0;
754 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
755 if ( rs->sr_err != LDAP_SUCCESS ) {
756 ldap_unbind_ext( ld, NULL, NULL );
757 rs->sr_text = "Start TLS failed";
760 } else if ( li->li_idle_timeout ) {
761 /* only touch when activity actually took place... */
764 #endif /* HAVE_TLS */
770 LDAP_BACK_CONN_ISTLS_SET( lc );
772 LDAP_BACK_CONN_ISTLS_CLEAR( lc );
774 if ( lctime != (time_t)(-1) ) {
775 lc->lc_time = lctime;
777 #endif /* HAVE_TLS */
780 if ( rs->sr_err != LDAP_SUCCESS ) {
781 rs->sr_err = slap_map_api2result( rs );
782 if ( sendok & LDAP_BACK_SENDERR ) {
783 if ( rs->sr_text == NULL ) {
784 rs->sr_text = "Proxy connection initialization failed";
786 send_ldap_result( op, rs );
790 if ( li->li_conn_ttl > 0 ) {
791 lc->lc_create_time = op->o_time;
802 ldap_back_send_t sendok,
803 struct berval *binddn,
804 struct berval *bindcred )
806 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
807 ldapconn_t *lc = NULL,
810 lookupconn = !( sendok & LDAP_BACK_BINDING );
812 /* if the server is quarantined, and
813 * - the current interval did not expire yet, or
814 * - no more retries should occur,
815 * don't return the connection */
816 if ( li->li_isquarantined ) {
817 slap_retry_info_t *ri = &li->li_quarantine;
820 if ( li->li_quarantine.ri_interval ) {
821 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
822 if ( li->li_isquarantined == LDAP_BACK_FQ_YES ) {
823 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
824 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
826 Debug( LDAP_DEBUG_ANY,
827 "%s: ldap_back_getconn quarantine "
828 "retry block #%d try #%d.\n",
829 op->o_log_prefix, ri->ri_idx, ri->ri_count );
830 li->li_isquarantined = LDAP_BACK_FQ_RETRYING;
833 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
837 rs->sr_err = LDAP_UNAVAILABLE;
838 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
839 rs->sr_text = "Target is quarantined";
840 send_ldap_result( op, rs );
846 /* Internal searches are privileged and shared. So is root. */
847 if ( op->o_do_not_cache || be_isroot( op ) ) {
848 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
849 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
850 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
853 struct berval tmpbinddn,
860 if ( binddn == NULL ) {
863 if ( bindcred == NULL ) {
864 bindcred = &tmpbindcred;
866 if ( op->o_tag == LDAP_REQ_BIND ) {
867 save_o_dn = op->o_dn;
868 save_o_ndn = op->o_ndn;
869 op->o_dn = op->o_req_dn;
870 op->o_ndn = op->o_req_ndn;
872 isproxyauthz = ldap_back_is_proxy_authz( op, rs, sendok, binddn, bindcred );
873 if ( op->o_tag == LDAP_REQ_BIND ) {
874 op->o_dn = save_o_dn;
875 op->o_ndn = save_o_ndn;
877 if ( isproxyauthz == -1 ) {
881 lc_curr.lc_local_ndn = op->o_ndn;
882 /* Explicit binds must not be shared;
883 * however, explicit binds are piped in a special connection
884 * when idassert is to occur with "override" set */
885 if ( op->o_tag == LDAP_REQ_BIND && !isproxyauthz ) {
886 lc_curr.lc_conn = op->o_conn;
889 if ( isproxyauthz && !( sendok & LDAP_BACK_BINDING ) ) {
890 lc_curr.lc_local_ndn = *binddn;
891 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
892 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
894 } else if ( isproxyauthz && ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) {
895 lc_curr.lc_local_ndn = slap_empty_bv;
896 LDAP_BACK_PCONN_BIND_SET( &lc_curr, op );
897 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
900 } else if ( SLAP_IS_AUTHZ_BACKEND( op ) ) {
901 lc_curr.lc_conn = op->o_conn;
904 LDAP_BACK_PCONN_ANON_SET( &lc_curr, op );
909 /* Explicit Bind requests always get their own conn */
912 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
913 if ( LDAP_BACK_PCONN_ISPRIV( &lc_curr ) ) {
914 /* lookup a conn that's not binding */
915 LDAP_TAILQ_FOREACH( lc,
916 &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv,
919 if ( !LDAP_BACK_CONN_BINDING( lc ) && lc->lc_refcnt == 0 ) {
925 if ( lc != LDAP_TAILQ_LAST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
928 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
930 LDAP_TAILQ_ENTRY_INIT( lc, lc_q );
931 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
935 } else if ( !LDAP_BACK_USE_TEMPORARIES( li )
936 && li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_num == li->li_conn_priv_max )
938 lc = LDAP_TAILQ_FIRST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv );
943 /* Searches for a ldapconn in the avl tree */
944 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
945 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
949 /* Don't reuse connections while they're still binding */
950 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
951 if ( !LDAP_BACK_USE_TEMPORARIES( li ) ) {
952 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
954 ldap_pvt_thread_yield();
961 if ( op->o_tag == LDAP_REQ_BIND ) {
962 /* right now, this is the only possible case */
963 assert( ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) );
964 LDAP_BACK_CONN_BINDING_SET( lc );
967 refcnt = ++lc->lc_refcnt;
970 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
973 /* Looks like we didn't get a bind. Open a new session... */
975 lc = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
976 lc->lc_flags = li->li_flags;
977 lc->lc_lcflags = lc_curr.lc_lcflags;
978 if ( ldap_back_prepare_conn( lc, op, rs, sendok ) != LDAP_SUCCESS ) {
983 if ( sendok & LDAP_BACK_BINDING ) {
984 LDAP_BACK_CONN_BINDING_SET( lc );
987 lc->lc_conn = lc_curr.lc_conn;
988 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
991 * the rationale is: connections as the rootdn are privileged,
992 * so li_acl is to be used; however, in some cases
993 * one already configured identity assertion with a highly
994 * privileged idassert_authcDN, so if li_acl is not configured
995 * and idassert is, use idassert instead.
997 * might change in the future, because it's preferable
998 * to make clear what identity is being used, since
999 * the only drawback is that one risks to configure
1000 * the same identity twice...
1002 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
1003 if ( li->li_acl_authmethod == LDAP_AUTH_NONE &&
1004 li->li_idassert_authmethod != LDAP_AUTH_NONE ) {
1005 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
1006 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
1009 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
1010 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
1012 LDAP_BACK_CONN_ISPRIV_SET( lc );
1014 } else if ( LDAP_BACK_CONN_ISIDASSERT( &lc_curr ) ) {
1015 if ( !LDAP_BACK_PCONN_ISBIND( &lc_curr ) ) {
1016 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
1017 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
1019 LDAP_BACK_CONN_ISIDASSERT_SET( lc );
1022 BER_BVZERO( &lc->lc_cred );
1023 BER_BVZERO( &lc->lc_bound_ndn );
1024 if ( !BER_BVISEMPTY( &op->o_ndn )
1025 && SLAP_IS_AUTHZ_BACKEND( op ) )
1027 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
1032 /* if start TLS failed but it was not mandatory,
1033 * check if the non-TLS connection was already
1034 * in cache; in case, destroy the newly created
1035 * connection and use the existing one */
1036 if ( LDAP_BACK_PCONN_ISTLS( lc )
1037 && !ldap_tls_inplace( lc->lc_ld ) )
1039 ldapconn_t *tmplc = NULL;
1040 int idx = LDAP_BACK_CONN2PRIV( &lc_curr ) - 1;
1042 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1043 LDAP_TAILQ_FOREACH( tmplc,
1044 &li->li_conn_priv[ idx ].lic_priv,
1047 if ( !LDAP_BACK_CONN_BINDING( tmplc ) ) {
1052 if ( tmplc != NULL ) {
1053 refcnt = ++tmplc->lc_refcnt;
1054 ldap_back_conn_free( lc );
1057 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1059 if ( tmplc != NULL ) {
1063 #endif /* HAVE_TLS */
1065 /* Inserts the newly created ldapconn in the avl tree */
1066 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1068 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1069 lc->lc_connid = li->li_conn_nextid++;
1071 assert( lc->lc_refcnt == 1 );
1073 #if LDAP_BACK_PRINT_CONNTREE > 0
1074 ldap_back_print_conntree( li, ">>> ldap_back_getconn(insert)" );
1075 #endif /* LDAP_BACK_PRINT_CONNTREE */
1077 if ( LDAP_BACK_PCONN_ISPRIV( lc ) ) {
1078 if ( li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num < li->li_conn_priv_max ) {
1079 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv, lc, lc_q );
1080 li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num++;
1081 LDAP_BACK_CONN_CACHED_SET( lc );
1084 LDAP_BACK_CONN_TAINTED_SET( lc );
1089 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
1090 ldap_back_conndn_cmp, ldap_back_conndn_dup );
1091 LDAP_BACK_CONN_CACHED_SET( lc );
1094 #if LDAP_BACK_PRINT_CONNTREE > 0
1095 ldap_back_print_conntree( li, "<<< ldap_back_getconn(insert)" );
1096 #endif /* LDAP_BACK_PRINT_CONNTREE */
1098 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1100 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1101 char buf[ SLAP_TEXT_BUFLEN ];
1103 snprintf( buf, sizeof( buf ),
1104 "lc=%p inserted refcnt=%u rc=%d",
1105 (void *)lc, refcnt, rs->sr_err );
1107 Debug( LDAP_DEBUG_TRACE,
1108 "=>ldap_back_getconn: %s: %s\n",
1109 op->o_log_prefix, buf, 0 );
1112 if ( !LDAP_BACK_PCONN_ISPRIV( lc ) ) {
1113 /* Err could be -1 in case a duplicate ldapconn is inserted */
1114 switch ( rs->sr_err ) {
1119 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1120 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( li ) ) {
1121 /* duplicate: free and try to get the newly created one */
1122 ldap_back_conn_free( lc );
1127 /* taint connection, so that it'll be freed when released */
1128 LDAP_BACK_CONN_TAINTED_SET( lc );
1132 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1133 ldap_back_conn_free( lc );
1134 rs->sr_err = LDAP_OTHER;
1135 rs->sr_text = "Proxy bind collision";
1136 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1137 send_ldap_result( op, rs );
1146 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
1147 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
1151 /* let it be used, but taint/delete it so that
1152 * no-one else can look it up any further */
1153 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1155 #if LDAP_BACK_PRINT_CONNTREE > 0
1156 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1157 #endif /* LDAP_BACK_PRINT_CONNTREE */
1159 (void)ldap_back_conn_delete( li, lc );
1160 LDAP_BACK_CONN_TAINTED_SET( lc );
1162 #if LDAP_BACK_PRINT_CONNTREE > 0
1163 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1164 #endif /* LDAP_BACK_PRINT_CONNTREE */
1166 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1169 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1170 char buf[ SLAP_TEXT_BUFLEN ];
1172 snprintf( buf, sizeof( buf ),
1173 "conn %p fetched refcnt=%u%s",
1175 expiring ? " expiring" : "" );
1176 Debug( LDAP_DEBUG_TRACE,
1177 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
1183 #endif /* HAVE_TLS */
1189 ldap_back_release_conn_lock(
1195 ldapconn_t *lc = *lcp;
1198 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1200 assert( lc->lc_refcnt > 0 );
1201 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1203 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
1204 ldap_back_freeconn( li, lc, 0 );
1208 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1213 ldap_back_quarantine(
1217 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1219 slap_retry_info_t *ri = &li->li_quarantine;
1221 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
1223 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1224 time_t new_last = slap_get_time();
1226 switch ( li->li_isquarantined ) {
1227 case LDAP_BACK_FQ_NO:
1228 if ( ri->ri_last == new_last ) {
1232 Debug( LDAP_DEBUG_ANY,
1233 "%s: ldap_back_quarantine enter.\n",
1234 op->o_log_prefix, 0, 0 );
1240 case LDAP_BACK_FQ_RETRYING:
1241 Debug( LDAP_DEBUG_ANY,
1242 "%s: ldap_back_quarantine block #%d try #%d failed.\n",
1243 op->o_log_prefix, ri->ri_idx, ri->ri_count );
1246 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1247 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1258 li->li_isquarantined = LDAP_BACK_FQ_YES;
1259 ri->ri_last = new_last;
1261 } else if ( li->li_isquarantined != LDAP_BACK_FQ_NO ) {
1262 if ( ri->ri_last == slap_get_time() ) {
1266 Debug( LDAP_DEBUG_ANY,
1267 "%s: ldap_back_quarantine exit (%d) err=%d.\n",
1268 op->o_log_prefix, li->li_isquarantined, rs->sr_err );
1270 if ( li->li_quarantine_f ) {
1271 (void)li->li_quarantine_f( li, li->li_quarantine_p );
1276 li->li_isquarantined = LDAP_BACK_FQ_NO;
1280 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
1284 ldap_back_dobind_cb(
1289 ber_tag_t *tptr = op->o_callback->sc_private;
1291 rs->sr_tag = slap_req2res( op->o_tag );
1293 return SLAP_CB_CONTINUE;
1297 * ldap_back_dobind_int
1299 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1302 ldap_back_dobind_int(
1306 ldap_back_send_t sendok,
1310 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1313 struct berval binddn = slap_empty_bv,
1314 bindcred = slap_empty_bv;
1320 ber_tag_t o_tag = op->o_tag;
1321 slap_callback cb = {0};
1324 assert( lcp != NULL );
1325 assert( retries >= 0 );
1327 if ( sendok & LDAP_BACK_GETCONN ) {
1328 assert( *lcp == NULL );
1330 lc = ldap_back_getconn( op, rs, sendok, &binddn, &bindcred );
1340 assert( lc != NULL );
1344 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1347 if ( binding == 0 ) {
1348 /* check if already bound */
1349 rc = isbound = LDAP_BACK_CONN_ISBOUND( lc );
1352 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1357 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
1358 /* if someone else is about to bind it, give up and retry */
1360 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1362 ldap_pvt_thread_yield();
1366 /* otherwise this thread will bind it */
1367 LDAP_BACK_CONN_BINDING_SET( lc );
1373 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1377 * FIXME: we need to let clients use proxyAuthz
1378 * otherwise we cannot do symmetric pools of servers;
1379 * we have to live with the fact that a user can
1380 * authorize itself as any ID that is allowed
1381 * by the authzTo directive of the "proxyauthzdn".
1384 * NOTE: current Proxy Authorization specification
1385 * and implementation do not allow proxy authorization
1386 * control to be provided with Bind requests
1389 * if no bind took place yet, but the connection is bound
1390 * and the "idassert-authcDN" (or other ID) is set,
1391 * then bind as the asserting identity and explicitly
1392 * add the proxyAuthz control to every operation with the
1393 * dn bound to the connection as control value.
1394 * This is done also if this is the authorizing backend,
1395 * but the "override" flag is given to idassert.
1396 * It allows to use SASL bind and yet proxyAuthz users
1398 op->o_tag = LDAP_REQ_BIND;
1399 cb.sc_next = op->o_callback;
1400 cb.sc_private = &o_tag;
1401 cb.sc_response = ldap_back_dobind_cb;
1402 op->o_callback = &cb;
1404 if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
1405 if ( BER_BVISEMPTY( &binddn ) && BER_BVISEMPTY( &bindcred ) ) {
1406 /* if we got here, it shouldn't return result */
1407 rc = ldap_back_is_proxy_authz( op, rs,
1408 LDAP_BACK_DONTSEND, &binddn, &bindcred );
1410 Debug( LDAP_DEBUG_ANY, "Error: ldap_back_is_proxy_authz "
1411 "returned %d, misconfigured URI?\n", rc, 0, 0 );
1412 rs->sr_err = LDAP_OTHER;
1413 rs->sr_text = "misconfigured URI?";
1414 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1415 if ( sendok & LDAP_BACK_SENDERR ) {
1416 send_ldap_result( op, rs );
1421 rc = ldap_back_proxy_authz_bind( lc, op, rs, sendok, &binddn, &bindcred );
1425 #ifdef HAVE_CYRUS_SASL
1426 if ( LDAP_BACK_CONN_ISPRIV( lc )) {
1428 if ( li->li_acl_authmethod != LDAP_AUTH_NONE )
1431 sb = &li->li_idassert.si_bc;
1433 if ( sb->sb_method == LDAP_AUTH_SASL ) {
1434 void *defaults = NULL;
1436 if ( sb->sb_secprops != NULL ) {
1437 rc = ldap_set_option( lc->lc_ld,
1438 LDAP_OPT_X_SASL_SECPROPS, sb->sb_secprops );
1440 if ( rc != LDAP_OPT_SUCCESS ) {
1441 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
1442 "(SECPROPS,\"%s\") failed!\n",
1443 sb->sb_secprops, 0, 0 );
1448 defaults = lutil_sasl_defaults( lc->lc_ld,
1449 sb->sb_saslmech.bv_val,
1450 sb->sb_realm.bv_val,
1451 sb->sb_authcId.bv_val,
1454 if ( defaults == NULL ) {
1455 rs->sr_err = LDAP_OTHER;
1456 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1457 if ( sendok & LDAP_BACK_SENDERR ) {
1458 send_ldap_result( op, rs );
1463 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
1464 sb->sb_binddn.bv_val,
1465 sb->sb_saslmech.bv_val, NULL, NULL,
1466 LDAP_SASL_QUIET, lutil_sasl_interact,
1469 ldap_pvt_thread_mutex_lock( &li->li_counter_mutex );
1470 ldap_pvt_mp_add( li->li_ops_completed[ SLAP_OP_BIND ], 1 );
1471 ldap_pvt_thread_mutex_unlock( &li->li_counter_mutex );
1473 lutil_sasl_freedefs( defaults );
1475 switch ( rs->sr_err ) {
1477 LDAP_BACK_CONN_ISBOUND_SET( lc );
1480 case LDAP_LOCAL_ERROR:
1481 /* list client API error codes that require
1482 * to taint the connection */
1483 /* FIXME: should actually retry? */
1484 LDAP_BACK_CONN_TAINTED_SET( lc );
1489 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1490 rs->sr_err = slap_map_api2result( rs );
1491 if ( sendok & LDAP_BACK_SENDERR ) {
1492 send_ldap_result( op, rs );
1497 if ( LDAP_BACK_QUARANTINE( li ) ) {
1498 ldap_back_quarantine( op, rs );
1504 #endif /* HAVE_CYRUS_SASL */
1507 if ( BER_BVISNULL( &lc->lc_cred ) ) {
1509 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) && !BER_BVISEMPTY( &lc->lc_bound_ndn ) ) {
1510 Debug( LDAP_DEBUG_ANY, "%s ldap_back_dobind_int: DN=\"%s\" without creds, binding anonymously",
1511 op->o_log_prefix, lc->lc_bound_ndn.bv_val, 0 );
1515 tmp_dn = lc->lc_bound_ndn.bv_val;
1517 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1519 LDAP_SASL_SIMPLE, &lc->lc_cred,
1520 NULL, NULL, &msgid );
1522 ldap_pvt_thread_mutex_lock( &li->li_counter_mutex );
1523 ldap_pvt_mp_add( li->li_ops_completed[ SLAP_OP_BIND ], 1 );
1524 ldap_pvt_thread_mutex_unlock( &li->li_counter_mutex );
1526 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
1527 if ( retries != LDAP_BACK_RETRY_NEVER ) {
1529 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1532 assert( lc->lc_refcnt > 0 );
1533 if ( lc->lc_refcnt == 1 ) {
1534 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1537 /* lc here must be the regular lc, reset and ready for init */
1538 rs->sr_err = ldap_back_prepare_conn( lc, op, rs, sendok );
1539 if ( rs->sr_err != LDAP_SUCCESS ) {
1540 sendok &= ~LDAP_BACK_SENDERR;
1546 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1549 if ( rs->sr_err == LDAP_SUCCESS ) {
1550 if ( retries > 0 ) {
1557 assert( lc->lc_refcnt == 1 );
1559 ldap_back_freeconn( li, lc, dolock );
1561 rs->sr_err = slap_map_api2result( rs );
1563 if ( LDAP_BACK_QUARANTINE( li ) ) {
1564 ldap_back_quarantine( op, rs );
1567 if ( rs->sr_err != LDAP_SUCCESS &&
1568 ( sendok & LDAP_BACK_SENDERR ) )
1570 if ( op->o_callback == &cb )
1571 op->o_callback = cb.sc_next;
1573 rs->sr_text = "Proxy can't contact remote server";
1574 send_ldap_result( op, rs );
1581 rc = ldap_back_op_result( lc, op, rs, msgid,
1582 -1, ( sendok | LDAP_BACK_BINDING ) );
1583 if ( rc == LDAP_SUCCESS ) {
1584 LDAP_BACK_CONN_ISBOUND_SET( lc );
1588 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1589 rc = LDAP_BACK_CONN_ISBOUND( lc );
1591 ldap_back_release_conn_lock( li, lcp, dolock );
1593 } else if ( LDAP_BACK_SAVECRED( li ) ) {
1594 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1598 if ( op->o_callback == &cb )
1599 op->o_callback = cb.sc_next;
1608 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1611 ldap_back_dobind( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1613 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1615 return ldap_back_dobind_int( lcp, op, rs,
1616 ( sendok | LDAP_BACK_GETCONN ), li->li_nretries, 1 );
1620 * ldap_back_default_rebind
1622 * This is a callback used for chasing referrals using the same
1623 * credentials as the original user on this session.
1626 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1627 ber_int_t msgid, void *params )
1629 ldapconn_t *lc = (ldapconn_t *)params;
1632 /* ... otherwise we couldn't get here */
1633 assert( lc != NULL );
1635 if ( !ldap_tls_inplace( ld ) ) {
1636 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1638 const char *text = NULL;
1640 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1641 LDAP_BACK_RETRY_DEFAULT, &text );
1642 if ( rc != LDAP_SUCCESS ) {
1646 #endif /* HAVE_TLS */
1648 /* FIXME: add checks on the URL/identity? */
1649 /* TODO: would like to count this bind operation for monitoring
1650 * too, but where do we get the ldapinfo_t? */
1652 return ldap_sasl_bind_s( ld,
1653 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1654 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1658 * ldap_back_default_urllist
1661 ldap_back_default_urllist(
1663 LDAPURLDesc **urllist,
1667 ldapinfo_t *li = (ldapinfo_t *)params;
1668 LDAPURLDesc **urltail;
1670 if ( urllist == url ) {
1671 return LDAP_SUCCESS;
1674 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1677 *urltail = *urllist;
1681 if ( !li->li_uri_mutex_do_not_lock ) {
1682 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1686 ch_free( li->li_uri );
1689 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1691 if ( !li->li_uri_mutex_do_not_lock ) {
1692 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1695 return LDAP_SUCCESS;
1704 ldap_back_send_t sendok )
1706 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1708 /* default behavior */
1709 if ( LDAP_BACK_ABANDON( li ) ) {
1710 return ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1713 if ( LDAP_BACK_IGNORE( li ) ) {
1714 return ldap_pvt_discard( lc->lc_ld, msgid );
1717 if ( LDAP_BACK_CANCEL( li ) ) {
1718 /* FIXME: asynchronous? */
1719 return ldap_cancel_s( lc->lc_ld, msgid, NULL, NULL );
1728 ldap_back_op_result(
1734 ldap_back_send_t sendok )
1736 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1741 LDAPControl **ctrls = NULL;
1744 rs->sr_matched = NULL;
1746 rs->sr_ctrls = NULL;
1748 /* if the error recorded in the reply corresponds
1749 * to a successful state, get the error from the
1750 * remote server response */
1751 if ( LDAP_ERR_OK( rs->sr_err ) ) {
1754 LDAPMessage *res = NULL;
1755 time_t stoptime = (time_t)(-1);
1756 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
1757 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
1758 const char *timeout_text = "Operation timed out";
1760 /* if timeout is not specified, compute and use
1761 * the one specific to the ongoing operation */
1762 if ( timeout == (time_t)(-1) ) {
1763 slap_op_t opidx = slap_req2op( op->o_tag );
1765 if ( opidx == SLAP_OP_SEARCH ) {
1766 if ( op->ors_tlimit <= 0 ) {
1770 timeout = op->ors_tlimit;
1771 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
1772 timeout_text = NULL;
1776 timeout = li->li_timeout[ opidx ];
1780 /* better than nothing :) */
1781 if ( timeout == 0 ) {
1782 if ( li->li_idle_timeout ) {
1783 timeout = li->li_idle_timeout;
1785 } else if ( li->li_conn_ttl ) {
1786 timeout = li->li_conn_ttl;
1791 stoptime = op->o_time + timeout;
1794 LDAP_BACK_TV_SET( &tv );
1797 /* if result parsing fails, note the failure reason */
1798 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1801 if ( timeout && slap_get_time() > stoptime ) {
1802 if ( sendok & LDAP_BACK_BINDING ) {
1803 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1806 /* let it be used, but taint/delete it so that
1807 * no-one else can look it up any further */
1808 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1810 #if LDAP_BACK_PRINT_CONNTREE > 0
1811 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1812 #endif /* LDAP_BACK_PRINT_CONNTREE */
1814 (void)ldap_back_conn_delete( li, lc );
1815 LDAP_BACK_CONN_TAINTED_SET( lc );
1817 #if LDAP_BACK_PRINT_CONNTREE > 0
1818 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1819 #endif /* LDAP_BACK_PRINT_CONNTREE */
1820 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1823 (void)ldap_back_cancel( lc, op, rs, msgid, sendok );
1825 rs->sr_err = timeout_err;
1826 rs->sr_text = timeout_text;
1831 LDAP_BACK_TV_SET( &tv );
1832 ldap_pvt_thread_yield();
1836 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1841 /* otherwise get the result; if it is not
1842 * LDAP_SUCCESS, record it in the reply
1843 * structure (this includes
1844 * LDAP_COMPARE_{TRUE|FALSE}) */
1846 /* only touch when activity actually took place... */
1847 if ( li->li_idle_timeout && lc ) {
1848 lc->lc_time = op->o_time;
1851 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1852 &match, &text, &refs, &ctrls, 1 );
1853 if ( rc == LDAP_SUCCESS ) {
1858 rs->sr_err = slap_map_api2result( rs );
1860 /* RFC 4511: referrals can only appear
1861 * if result code is LDAP_REFERRAL */
1863 && refs[ 0 ] != NULL
1864 && refs[ 0 ][ 0 ] != '\0' )
1866 if ( rs->sr_err != LDAP_REFERRAL ) {
1867 Debug( LDAP_DEBUG_ANY,
1868 "%s ldap_back_op_result: "
1869 "got referrals with err=%d\n",
1876 for ( i = 0; refs[ i ] != NULL; i++ )
1878 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1880 for ( i = 0; refs[ i ] != NULL; i++ ) {
1881 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1883 BER_BVZERO( &rs->sr_ref[ i ] );
1886 } else if ( rs->sr_err == LDAP_REFERRAL ) {
1887 Debug( LDAP_DEBUG_ANY,
1888 "%s ldap_back_op_result: "
1889 "got err=%d with null "
1890 "or empty referrals\n",
1894 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1897 if ( ctrls != NULL ) {
1898 rs->sr_ctrls = ctrls;
1903 /* if the error in the reply structure is not
1904 * LDAP_SUCCESS, try to map it from client
1905 * to server error */
1906 if ( !LDAP_ERR_OK( rs->sr_err ) ) {
1907 rs->sr_err = slap_map_api2result( rs );
1909 /* internal ops ( op->o_conn == NULL )
1910 * must not reply to client */
1911 if ( op->o_conn && !op->o_do_not_cache && match ) {
1913 /* record the (massaged) matched
1914 * DN into the reply structure */
1915 rs->sr_matched = match;
1919 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1920 if ( !( sendok & LDAP_BACK_RETRYING ) ) {
1921 if ( LDAP_BACK_QUARANTINE( li ) ) {
1922 ldap_back_quarantine( op, rs );
1924 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1925 if ( rs->sr_text == NULL ) rs->sr_text = "Proxy operation retry failed";
1926 send_ldap_result( op, rs );
1930 } else if ( op->o_conn &&
1931 ( ( ( sendok & LDAP_BACK_SENDOK ) && LDAP_ERR_OK( rs->sr_err ) )
1932 || ( ( sendok & LDAP_BACK_SENDERR ) && !LDAP_ERR_OK( rs->sr_err ) ) ) )
1934 send_ldap_result( op, rs );
1938 ldap_memfree( text );
1942 /* there can't be refs with a (successful) bind */
1944 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1949 ber_memvfree( (void **)refs );
1952 /* match should not be possible with a successful bind */
1954 if ( rs->sr_matched != match ) {
1955 free( (char *)rs->sr_matched );
1957 rs->sr_matched = NULL;
1958 ldap_memfree( match );
1961 if ( ctrls != NULL ) {
1962 if ( op->o_tag == LDAP_REQ_BIND && rs->sr_err == LDAP_SUCCESS ) {
1965 for ( i = 0; ctrls[i] != NULL; i++ );
1967 rs->sr_ctrls = op->o_tmpalloc( sizeof( LDAPControl * )*( i + 1 ),
1969 for ( i = 0; ctrls[ i ] != NULL; i++ ) {
1971 ber_len_t oidlen = strlen( ctrls[i]->ldctl_oid );
1972 ber_len_t size = sizeof( LDAPControl )
1974 + ctrls[i]->ldctl_value.bv_len + 1;
1976 rs->sr_ctrls[ i ] = op->o_tmpalloc( size, op->o_tmpmemctx );
1977 rs->sr_ctrls[ i ]->ldctl_oid = (char *)&rs->sr_ctrls[ i ][ 1 ];
1978 lutil_strcopy( rs->sr_ctrls[ i ]->ldctl_oid, ctrls[i]->ldctl_oid );
1979 rs->sr_ctrls[ i ]->ldctl_value.bv_val
1980 = (char *)&rs->sr_ctrls[ i ]->ldctl_oid[oidlen + 1];
1981 rs->sr_ctrls[ i ]->ldctl_value.bv_len
1982 = ctrls[i]->ldctl_value.bv_len;
1983 ptr = lutil_memcopy( rs->sr_ctrls[ i ]->ldctl_value.bv_val,
1984 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len );
1987 rs->sr_ctrls[ i ] = NULL;
1988 rs->sr_flags |= REP_CTRLS_MUSTBEFREED;
1991 assert( rs->sr_ctrls != NULL );
1992 rs->sr_ctrls = NULL;
1995 ldap_controls_free( ctrls );
1998 return( LDAP_ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
2001 /* return true if bound, false if failed */
2003 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
2005 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2008 assert( lcp != NULL );
2009 assert( *lcp != NULL );
2011 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
2013 if ( (*lcp)->lc_refcnt == 1 ) {
2014 int binding = LDAP_BACK_CONN_BINDING( *lcp );
2016 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
2017 Debug( LDAP_DEBUG_ANY,
2018 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
2019 op->o_log_prefix, li->li_uri,
2020 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
2021 "" : (*lcp)->lc_bound_ndn.bv_val );
2022 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
2024 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
2025 (*lcp)->lc_ld = NULL;
2026 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
2028 /* lc here must be the regular lc, reset and ready for init */
2029 rc = ldap_back_prepare_conn( *lcp, op, rs, sendok );
2030 if ( rc != LDAP_SUCCESS ) {
2031 /* freeit, because lc_refcnt == 1 */
2032 (*lcp)->lc_refcnt = 0;
2033 (void)ldap_back_freeconn( li, *lcp, 0 );
2037 } else if ( ( sendok & LDAP_BACK_BINDING ) ) {
2039 LDAP_BACK_CONN_BINDING_SET( *lcp );
2044 rc = ldap_back_dobind_int( lcp, op, rs, sendok, 0, 0 );
2045 if ( rc == 0 && *lcp != NULL ) {
2046 /* freeit, because lc_refcnt == 1 */
2047 (*lcp)->lc_refcnt = 0;
2048 LDAP_BACK_CONN_TAINTED_SET( *lcp );
2049 (void)ldap_back_freeconn( li, *lcp, 0 );
2055 Debug( LDAP_DEBUG_TRACE,
2056 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
2057 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
2059 LDAP_BACK_CONN_TAINTED_SET( *lcp );
2060 ldap_back_release_conn_lock( li, lcp, 0 );
2061 assert( *lcp == NULL );
2063 if ( sendok & LDAP_BACK_SENDERR ) {
2064 rs->sr_err = LDAP_UNAVAILABLE;
2065 rs->sr_text = "Unable to retry";
2066 send_ldap_result( op, rs );
2070 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
2076 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
2077 struct berval *binddn, struct berval *bindcred )
2079 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2083 if ( op->o_conn == NULL || op->o_do_not_cache ) {
2087 /* don't proxyAuthz if protocol is not LDAPv3 */
2088 switch ( li->li_version ) {
2093 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2099 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2100 if ( sendok & LDAP_BACK_SENDERR ) {
2101 send_ldap_result( op, rs );
2108 *binddn = slap_empty_bv;
2109 *bindcred = slap_empty_bv;
2111 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2112 ndn = op->o_conn->c_ndn;
2118 if ( !( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE )) {
2119 if ( op->o_tag == LDAP_REQ_BIND ) {
2120 if ( !BER_BVISEMPTY( &ndn )) {
2124 } else if ( SLAP_IS_AUTHZ_BACKEND( op )) {
2130 switch ( li->li_idassert_mode ) {
2131 case LDAP_BACK_IDASSERT_LEGACY:
2132 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
2133 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
2135 *binddn = li->li_idassert_authcDN;
2136 *bindcred = li->li_idassert_passwd;
2143 /* NOTE: rootdn can always idassert */
2144 if ( BER_BVISNULL( &ndn )
2145 && li->li_idassert_authz == NULL
2146 && !( li->li_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL ) )
2148 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2149 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
2150 if ( sendok & LDAP_BACK_SENDERR ) {
2151 send_ldap_result( op, rs );
2156 rs->sr_err = LDAP_SUCCESS;
2157 *binddn = slap_empty_bv;
2158 *bindcred = slap_empty_bv;
2164 } else if ( !be_isroot( op ) ) {
2165 if ( li->li_idassert_passthru ) {
2166 struct berval authcDN;
2168 if ( BER_BVISNULL( &ndn ) ) {
2169 authcDN = slap_empty_bv;
2174 rs->sr_err = slap_sasl_matches( op, li->li_idassert_passthru,
2175 &authcDN, &authcDN );
2176 if ( rs->sr_err == LDAP_SUCCESS ) {
2182 if ( li->li_idassert_authz ) {
2183 struct berval authcDN;
2185 if ( BER_BVISNULL( &ndn ) ) {
2186 authcDN = slap_empty_bv;
2191 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
2192 &authcDN, &authcDN );
2193 if ( rs->sr_err != LDAP_SUCCESS ) {
2194 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2195 if ( sendok & LDAP_BACK_SENDERR ) {
2196 send_ldap_result( op, rs );
2201 rs->sr_err = LDAP_SUCCESS;
2202 *binddn = slap_empty_bv;
2203 *bindcred = slap_empty_bv;
2212 *binddn = li->li_idassert_authcDN;
2213 *bindcred = li->li_idassert_passwd;
2223 ldap_back_proxy_authz_bind(
2227 ldap_back_send_t sendok,
2228 struct berval *binddn,
2229 struct berval *bindcred )
2231 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2236 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2237 ndn = op->o_conn->c_ndn;
2243 if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
2244 #ifdef HAVE_CYRUS_SASL
2245 void *defaults = NULL;
2246 struct berval authzID = BER_BVNULL;
2248 LDAPControl **ctrlsp = NULL;
2249 LDAPMessage *result = NULL;
2250 const char *rmech = NULL;
2251 const char *save_text = rs->sr_text;
2254 LDAPControl ctrl, *ctrls[2];
2256 #endif /* SLAP_AUTH_DN */
2258 /* if SASL supports native authz, prepare for it */
2259 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
2260 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2262 switch ( li->li_idassert_mode ) {
2263 case LDAP_BACK_IDASSERT_OTHERID:
2264 case LDAP_BACK_IDASSERT_OTHERDN:
2265 authzID = li->li_idassert_authzID;
2268 case LDAP_BACK_IDASSERT_ANONYMOUS:
2269 BER_BVSTR( &authzID, "dn:" );
2272 case LDAP_BACK_IDASSERT_SELF:
2273 if ( BER_BVISNULL( &ndn ) ) {
2274 /* connection is not authc'd, so don't idassert */
2275 BER_BVSTR( &authzID, "dn:" );
2278 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
2279 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
2280 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
2281 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
2282 ndn.bv_val, ndn.bv_len + 1 );
2291 if ( li->li_idassert_secprops != NULL ) {
2292 rs->sr_err = ldap_set_option( lc->lc_ld,
2293 LDAP_OPT_X_SASL_SECPROPS,
2294 (void *)li->li_idassert_secprops );
2296 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
2297 rs->sr_err = LDAP_OTHER;
2298 if ( sendok & LDAP_BACK_SENDERR ) {
2299 send_ldap_result( op, rs );
2301 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2306 defaults = lutil_sasl_defaults( lc->lc_ld,
2307 li->li_idassert_sasl_mech.bv_val,
2308 li->li_idassert_sasl_realm.bv_val,
2309 li->li_idassert_authcID.bv_val,
2310 li->li_idassert_passwd.bv_val,
2312 if ( defaults == NULL ) {
2313 rs->sr_err = LDAP_OTHER;
2314 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2315 if ( sendok & LDAP_BACK_SENDERR ) {
2316 send_ldap_result( op, rs );
2322 if ( li->li_idassert_flags & LDAP_BACK_AUTH_DN_AUTHZID ) {
2323 assert( BER_BVISNULL( binddn ) );
2325 ctrl.ldctl_oid = LDAP_CONTROL_AUTHZID_REQUEST;
2326 ctrl.ldctl_iscritical = 0;
2327 BER_BVZERO( &ctrl.ldctl_value );
2332 #endif /* SLAP_AUTH_DN */
2335 rs->sr_err = ldap_sasl_interactive_bind( lc->lc_ld, binddn->bv_val,
2336 li->li_idassert_sasl_mech.bv_val,
2337 ctrlsp, NULL, LDAP_SASL_QUIET, lutil_sasl_interact, defaults,
2338 result, &rmech, &msgid );
2340 if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS )
2343 ldap_msgfree( result );
2345 if ( ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 || !result ) {
2346 ldap_get_option( lc->lc_ld, LDAP_OPT_RESULT_CODE, (void*)&rs->sr_err );
2347 ldap_get_option( lc->lc_ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&rs->sr_text );
2350 } while ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
2352 ldap_pvt_thread_mutex_lock( &li->li_counter_mutex );
2353 ldap_pvt_mp_add( li->li_ops_completed[ SLAP_OP_BIND ], 1 );
2354 ldap_pvt_thread_mutex_unlock( &li->li_counter_mutex );
2356 switch ( rs->sr_err ) {
2359 /* FIXME: right now, the only reason to check
2360 * response controls is RFC 3829 authzid */
2361 if ( li->li_idassert_flags & LDAP_BACK_AUTH_DN_AUTHZID ) {
2363 rc = ldap_parse_result( lc->lc_ld, result, NULL, NULL, NULL, NULL,
2365 if ( rc == LDAP_SUCCESS && ctrlsp ) {
2368 ctrl = ldap_control_find( LDAP_CONTROL_AUTHZID_RESPONSE,
2371 Debug( LDAP_DEBUG_TRACE, "%s: ldap_back_proxy_authz_bind: authzID=\"%s\" (authzid)\n",
2372 op->o_log_prefix, ctrl->ldctl_value.bv_val, 0 );
2373 if ( ctrl->ldctl_value.bv_len > STRLENOF("dn:") &&
2374 strncasecmp( ctrl->ldctl_value.bv_val, "dn:", STRLENOF("dn:") ) == 0 )
2377 bv.bv_val = &ctrl->ldctl_value.bv_val[STRLENOF("dn:")];
2378 bv.bv_len = ctrl->ldctl_value.bv_len - STRLENOF("dn:");
2379 ber_bvreplace( &lc->lc_bound_ndn, &bv );
2384 ldap_controls_free( ctrlsp );
2386 } else if ( li->li_idassert_flags & LDAP_BACK_AUTH_DN_WHOAMI ) {
2387 struct berval *val = NULL;
2388 rc = ldap_whoami_s( lc->lc_ld, &val, NULL, NULL );
2389 if ( rc == LDAP_SUCCESS && val != NULL ) {
2390 Debug( LDAP_DEBUG_TRACE, "%s: ldap_back_proxy_authz_bind: authzID=\"%s\" (whoami)\n",
2391 op->o_log_prefix, val->bv_val, 0 );
2392 if ( val->bv_len > STRLENOF("dn:") &&
2393 strncasecmp( val->bv_val, "dn:", STRLENOF("dn:") ) == 0 )
2396 bv.bv_val = &val->bv_val[STRLENOF("dn:")];
2397 bv.bv_len = val->bv_len - STRLENOF("dn:");
2398 ber_bvreplace( &lc->lc_bound_ndn, &bv );
2404 if ( ( li->li_idassert_flags & LDAP_BACK_AUTH_DN_MASK ) &&
2405 BER_BVISNULL( &lc->lc_bound_ndn ) )
2407 /* all in all, we only need it to be non-null */
2408 /* FIXME: should this be configurable? */
2409 static struct berval bv = BER_BVC("cn=authzdn");
2410 ber_bvreplace( &lc->lc_bound_ndn, &bv );
2412 #endif /* SLAP_AUTH_DN */
2413 LDAP_BACK_CONN_ISBOUND_SET( lc );
2416 case LDAP_LOCAL_ERROR:
2417 /* list client API error codes that require
2418 * to taint the connection */
2419 /* FIXME: should actually retry? */
2420 LDAP_BACK_CONN_TAINTED_SET( lc );
2425 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2426 rs->sr_err = slap_map_api2result( rs );
2427 if ( sendok & LDAP_BACK_SENDERR ) {
2428 send_ldap_result( op, rs );
2433 if ( save_text != rs->sr_text ) {
2434 ldap_memfree( (char *)rs->sr_text );
2435 rs->sr_text = save_text;
2438 ldap_msgfree( result );
2440 lutil_sasl_freedefs( defaults );
2442 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
2446 #endif /* HAVE_CYRUS_SASL */
2449 switch ( li->li_idassert_authmethod ) {
2450 case LDAP_AUTH_NONE:
2451 /* FIXME: do we really need this? */
2452 BER_BVSTR( binddn, "" );
2453 BER_BVSTR( bindcred, "" );
2456 case LDAP_AUTH_SIMPLE:
2457 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
2458 binddn->bv_val, LDAP_SASL_SIMPLE,
2459 bindcred, NULL, NULL, &msgid );
2460 rc = ldap_back_op_result( lc, op, rs, msgid,
2461 -1, ( sendok | LDAP_BACK_BINDING ) );
2463 ldap_pvt_thread_mutex_lock( &li->li_counter_mutex );
2464 ldap_pvt_mp_add( li->li_ops_completed[ SLAP_OP_BIND ], 1 );
2465 ldap_pvt_thread_mutex_unlock( &li->li_counter_mutex );
2470 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2471 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
2472 if ( sendok & LDAP_BACK_SENDERR ) {
2473 send_ldap_result( op, rs );
2478 if ( rc == LDAP_SUCCESS ) {
2479 /* set rebind stuff in case of successful proxyAuthz bind,
2480 * so that referral chasing is attempted using the right
2482 LDAP_BACK_CONN_ISBOUND_SET( lc );
2483 if ( !BER_BVISNULL( binddn ) ) {
2484 ber_bvreplace( &lc->lc_bound_ndn, binddn );
2487 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
2488 memset( lc->lc_cred.bv_val, 0,
2489 lc->lc_cred.bv_len );
2492 if ( LDAP_BACK_SAVECRED( li ) ) {
2493 if ( !BER_BVISNULL( bindcred ) ) {
2494 ber_bvreplace( &lc->lc_cred, bindcred );
2495 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
2499 lc->lc_cred.bv_len = 0;
2504 return LDAP_BACK_CONN_ISBOUND( lc );
2508 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
2509 * to existing server-side controls if required; if not,
2510 * the existing server-side controls are placed in *pctrls.
2511 * The caller, after using the controls in client API
2512 * operations, if ( *pctrls != op->o_ctrls ), should
2513 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
2514 * The function returns success if the control could
2515 * be added if required, or if it did nothing; in the future,
2516 * it might return some error if it failed.
2518 * if no bind took place yet, but the connection is bound
2519 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
2520 * and explicitly add proxyAuthz the control to every operation
2521 * with the dn bound to the connection as control value.
2523 * If no server-side controls are defined for the operation,
2524 * simply add the proxyAuthz control; otherwise, if the
2525 * proxyAuthz control is not already set, add it as
2528 * FIXME: is controls order significant for security?
2529 * ANSWER: controls ordering and interoperability
2530 * must be indicated by the specs of each control; if none
2531 * is specified, the order is irrelevant.
2534 ldap_back_proxy_authz_ctrl(
2537 struct berval *bound_ndn,
2539 slap_idassert_t *si,
2542 slap_idassert_mode_t mode;
2543 struct berval assertedID,
2547 rs->sr_err = SLAP_CB_CONTINUE;
2549 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
2550 * but if it is not set this test fails. We need a different
2551 * means to detect if idassert is enabled */
2552 if ( ( BER_BVISNULL( &si->si_bc.sb_authcId ) || BER_BVISEMPTY( &si->si_bc.sb_authcId ) )
2553 && ( BER_BVISNULL( &si->si_bc.sb_binddn ) || BER_BVISEMPTY( &si->si_bc.sb_binddn ) )
2554 && BER_BVISNULL( &si->si_bc.sb_saslmech ) )
2559 if ( !op->o_conn || op->o_do_not_cache || ( isroot = be_isroot( op ) ) ) {
2563 if ( op->o_tag == LDAP_REQ_BIND ) {
2564 ndn = op->o_req_ndn;
2566 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2567 ndn = op->o_conn->c_ndn;
2573 if ( si->si_mode == LDAP_BACK_IDASSERT_LEGACY ) {
2574 if ( op->o_proxy_authz ) {
2576 * FIXME: we do not want to perform proxyAuthz
2577 * on behalf of the client, because this would
2578 * be performed with "proxyauthzdn" privileges.
2580 * This might actually be too strict, since
2581 * the "proxyauthzdn" authzTo, and each entry's
2582 * authzFrom attributes may be crafted
2583 * to avoid unwanted proxyAuthz to take place.
2586 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2587 rs->sr_text = "proxyAuthz not allowed within namingContext";
2592 if ( !BER_BVISNULL( bound_ndn ) ) {
2596 if ( BER_BVISNULL( &ndn ) ) {
2600 if ( BER_BVISNULL( &si->si_bc.sb_binddn ) ) {
2604 } else if ( si->si_bc.sb_method == LDAP_AUTH_SASL ) {
2605 if ( ( si->si_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2607 /* already asserted in SASL via native authz */
2611 } else if ( si->si_authz && !isroot ) {
2613 struct berval authcDN;
2615 if ( BER_BVISNULL( &ndn ) ) {
2616 authcDN = slap_empty_bv;
2620 rc = slap_sasl_matches( op, si->si_authz,
2621 &authcDN, &authcDN );
2622 if ( rc != LDAP_SUCCESS ) {
2623 if ( si->si_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2624 /* ndn is not authorized
2625 * to use idassert */
2632 if ( op->o_proxy_authz ) {
2635 * 1) ignore the already set proxyAuthz control
2636 * 2) leave it in place, and don't set ours
2638 * 4) reject the operation
2640 * option (4) is very drastic
2641 * option (3) will make the remote server reject
2642 * the operation, thus being equivalent to (4)
2643 * option (2) will likely break the idassert
2644 * assumptions, so we cannot accept it;
2645 * option (1) means that we are contradicting
2646 * the client's reques.
2648 * I think (4) is the only correct choice.
2650 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2651 rs->sr_text = "proxyAuthz not allowed within namingContext";
2654 if ( op->o_is_auth_check ) {
2655 mode = LDAP_BACK_IDASSERT_NOASSERT;
2662 case LDAP_BACK_IDASSERT_LEGACY:
2663 /* original behavior:
2664 * assert the client's identity */
2665 case LDAP_BACK_IDASSERT_SELF:
2669 case LDAP_BACK_IDASSERT_ANONYMOUS:
2670 /* assert "anonymous" */
2671 assertedID = slap_empty_bv;
2674 case LDAP_BACK_IDASSERT_NOASSERT:
2675 /* don't assert; bind as proxyauthzdn */
2678 case LDAP_BACK_IDASSERT_OTHERID:
2679 case LDAP_BACK_IDASSERT_OTHERDN:
2680 /* assert idassert DN */
2681 assertedID = si->si_bc.sb_authzId;
2688 /* if we got here, "" is allowed to proxyAuthz */
2689 if ( BER_BVISNULL( &assertedID ) ) {
2690 assertedID = slap_empty_bv;
2693 /* don't idassert the bound DN (ITS#4497) */
2694 if ( dn_match( &assertedID, bound_ndn ) ) {
2698 ctrl->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
2699 ctrl->ldctl_iscritical = ( ( si->si_flags & LDAP_BACK_AUTH_PROXYAUTHZ_CRITICAL ) == LDAP_BACK_AUTH_PROXYAUTHZ_CRITICAL );
2701 switch ( si->si_mode ) {
2702 /* already in u:ID or dn:DN form */
2703 case LDAP_BACK_IDASSERT_OTHERID:
2704 case LDAP_BACK_IDASSERT_OTHERDN:
2705 ber_dupbv_x( &ctrl->ldctl_value, &assertedID, op->o_tmpmemctx );
2706 rs->sr_err = LDAP_SUCCESS;
2709 /* needs the dn: prefix */
2711 ctrl->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
2712 ctrl->ldctl_value.bv_val = op->o_tmpalloc( ctrl->ldctl_value.bv_len + 1,
2714 AC_MEMCPY( ctrl->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
2715 AC_MEMCPY( &ctrl->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
2716 assertedID.bv_val, assertedID.bv_len + 1 );
2717 rs->sr_err = LDAP_SUCCESS;
2721 /* Older versions of <draft-weltman-ldapv3-proxy> required
2722 * to encode the value of the authzID (and called it proxyDN);
2723 * this hack provides compatibility with those DSAs that
2724 * implement it this way */
2725 if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
2726 struct berval authzID = ctrl->ldctl_value;
2727 BerElementBuffer berbuf;
2728 BerElement *ber = (BerElement *)&berbuf;
2731 ber_init2( ber, 0, LBER_USE_DER );
2732 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2734 tag = ber_printf( ber, "O", &authzID );
2735 if ( tag == LBER_ERROR ) {
2736 rs->sr_err = LDAP_OTHER;
2740 if ( ber_flatten2( ber, &ctrl->ldctl_value, 1 ) == -1 ) {
2741 rs->sr_err = LDAP_OTHER;
2745 rs->sr_err = LDAP_SUCCESS;
2748 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2749 ber_free_buf( ber );
2751 if ( rs->sr_err != LDAP_SUCCESS ) {
2755 } else if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
2756 struct berval authzID = ctrl->ldctl_value,
2758 BerElementBuffer berbuf;
2759 BerElement *ber = (BerElement *)&berbuf;
2762 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
2763 rs->sr_err = LDAP_PROTOCOL_ERROR;
2768 tmp.bv_val += STRLENOF( "dn:" );
2769 tmp.bv_len -= STRLENOF( "dn:" );
2771 ber_init2( ber, 0, LBER_USE_DER );
2772 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2774 /* apparently, Mozilla API encodes this
2775 * as "SEQUENCE { LDAPDN }" */
2776 tag = ber_printf( ber, "{O}", &tmp );
2777 if ( tag == LBER_ERROR ) {
2778 rs->sr_err = LDAP_OTHER;
2782 if ( ber_flatten2( ber, &ctrl->ldctl_value, 1 ) == -1 ) {
2783 rs->sr_err = LDAP_OTHER;
2787 ctrl->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
2788 rs->sr_err = LDAP_SUCCESS;
2791 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2792 ber_free_buf( ber );
2794 if ( rs->sr_err != LDAP_SUCCESS ) {
2807 * if any needs to be added, it is prepended to existing ones,
2808 * in a newly allocated array. The companion function
2809 * ldap_back_controls_free() must be used to restore the original
2810 * status of op->o_ctrls.
2813 ldap_back_controls_add(
2817 LDAPControl ***pctrls )
2819 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2821 LDAPControl **ctrls = NULL;
2822 /* set to the maximum number of controls this backend can add */
2823 LDAPControl c[ 2 ] = { { 0 } };
2824 int n = 0, i, j1 = 0, j2 = 0;
2828 rs->sr_err = LDAP_SUCCESS;
2830 /* don't add controls if protocol is not LDAPv3 */
2831 switch ( li->li_version ) {
2836 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2845 /* put controls that go __before__ existing ones here */
2847 /* proxyAuthz for identity assertion */
2848 switch ( ldap_back_proxy_authz_ctrl( op, rs, &lc->lc_bound_ndn,
2849 li->li_version, &li->li_idassert, &c[ j1 ] ) )
2851 case SLAP_CB_CONTINUE:
2862 /* put controls that go __after__ existing ones here */
2864 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
2865 /* FIXME: according to <draft-wahl-ldap-session>,
2866 * the server should check if the control can be added
2867 * based on the identity of the client and so */
2869 /* session tracking */
2870 if ( LDAP_BACK_ST_REQUEST( li ) ) {
2871 switch ( slap_ctrl_session_tracking_request_add( op, rs, &c[ j1 + j2 ] ) ) {
2872 case SLAP_CB_CONTINUE:
2883 #endif /* SLAP_CONTROL_X_SESSION_TRACKING */
2885 if ( rs->sr_err == SLAP_CB_CONTINUE ) {
2886 rs->sr_err = LDAP_SUCCESS;
2889 /* if nothing to do, just bail out */
2890 if ( j1 == 0 && j2 == 0 ) {
2894 assert( j1 + j2 <= (int) (sizeof( c )/sizeof( c[0] )) );
2896 if ( op->o_ctrls ) {
2897 for ( n = 0; op->o_ctrls[ n ]; n++ )
2898 /* just count ctrls */ ;
2901 ctrls = op->o_tmpalloc( (n + j1 + j2 + 1) * sizeof( LDAPControl * ) + ( j1 + j2 ) * sizeof( LDAPControl ),
2904 ctrls[ 0 ] = (LDAPControl *)&ctrls[ n + j1 + j2 + 1 ];
2905 *ctrls[ 0 ] = c[ 0 ];
2906 for ( i = 1; i < j1; i++ ) {
2907 ctrls[ i ] = &ctrls[ 0 ][ i ];
2908 *ctrls[ i ] = c[ i ];
2913 if ( op->o_ctrls ) {
2914 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
2915 ctrls[ i + j1 ] = op->o_ctrls[ i ];
2921 ctrls[ n ] = (LDAPControl *)&ctrls[ n + j2 + 1 ] + j1;
2922 *ctrls[ n ] = c[ j1 ];
2923 for ( i = 1; i < j2; i++ ) {
2924 ctrls[ n + i ] = &ctrls[ n ][ i ];
2925 *ctrls[ n + i ] = c[ i ];
2929 ctrls[ n + j2 ] = NULL;
2932 if ( ctrls == NULL ) {
2933 ctrls = op->o_ctrls;
2942 ldap_back_controls_free( Operation *op, SlapReply *rs, LDAPControl ***pctrls )
2944 LDAPControl **ctrls = *pctrls;
2946 /* we assume that the controls added by the proxy come first,
2947 * so as soon as we find op->o_ctrls[ 0 ] we can stop */
2948 if ( ctrls && ctrls != op->o_ctrls ) {
2949 int i = 0, n = 0, n_added;
2950 LDAPControl *lower, *upper;
2952 assert( ctrls[ 0 ] != NULL );
2954 for ( n = 0; ctrls[ n ] != NULL; n++ )
2957 if ( op->o_ctrls ) {
2958 for ( i = 0; op->o_ctrls[ i ] != NULL; i++ )
2963 lower = (LDAPControl *)&ctrls[ n ];
2964 upper = &lower[ n_added ];
2966 for ( i = 0; ctrls[ i ] != NULL; i++ ) {
2967 if ( ctrls[ i ] < lower || ctrls[ i ] >= upper ) {
2968 /* original; don't touch */
2972 if ( !BER_BVISNULL( &ctrls[ i ]->ldctl_value ) ) {
2973 op->o_tmpfree( ctrls[ i ]->ldctl_value.bv_val, op->o_tmpmemctx );
2977 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2986 ldap_back_conn2str( const ldapconn_base_t *lc, char *buf, ber_len_t buflen )
2988 char tbuf[ SLAP_TEXT_BUFLEN ];
2989 char *ptr = buf, *end = buf + buflen;
2992 if ( ptr + sizeof("conn=") > end ) return -1;
2993 ptr = lutil_strcopy( ptr, "conn=" );
2995 len = ldap_back_connid2str( lc, ptr, (ber_len_t)(end - ptr) );
2997 if ( ptr >= end ) return -1;
2999 if ( !BER_BVISNULL( &lc->lcb_local_ndn ) ) {
3000 if ( ptr + sizeof(" DN=\"\"") + lc->lcb_local_ndn.bv_len > end ) return -1;
3001 ptr = lutil_strcopy( ptr, " DN=\"" );
3002 ptr = lutil_strncopy( ptr, lc->lcb_local_ndn.bv_val, lc->lcb_local_ndn.bv_len );
3006 if ( lc->lcb_create_time != 0 ) {
3007 len = snprintf( tbuf, sizeof(tbuf), "%ld", lc->lcb_create_time );
3008 if ( ptr + sizeof(" created=") + len >= end ) return -1;
3009 ptr = lutil_strcopy( ptr, " created=" );
3010 ptr = lutil_strcopy( ptr, tbuf );
3013 if ( lc->lcb_time != 0 ) {
3014 len = snprintf( tbuf, sizeof(tbuf), "%ld", lc->lcb_time );
3015 if ( ptr + sizeof(" modified=") + len >= end ) return -1;
3016 ptr = lutil_strcopy( ptr, " modified=" );
3017 ptr = lutil_strcopy( ptr, tbuf );
3020 len = snprintf( tbuf, sizeof(tbuf), "%u", lc->lcb_refcnt );
3021 if ( ptr + sizeof(" refcnt=") + len >= end ) return -1;
3022 ptr = lutil_strcopy( ptr, " refcnt=" );
3023 ptr = lutil_strcopy( ptr, tbuf );
3029 ldap_back_connid2str( const ldapconn_base_t *lc, char *buf, ber_len_t buflen )
3031 static struct berval conns[] = {
3033 BER_BVC("ROOTDN-TLS"),
3035 BER_BVC("ANON-TLS"),
3037 BER_BVC("BIND-TLS"),
3043 if ( LDAP_BACK_PCONN_ISPRIV( (const ldapconn_t *)lc ) ) {
3047 cid = (long)lc->lcb_conn;
3048 assert( cid >= LDAP_BACK_PCONN_FIRST && cid < LDAP_BACK_PCONN_LAST );
3052 if ( bv->bv_len >= buflen ) {
3053 return bv->bv_len + 1;
3057 lutil_strncopy( buf, bv->bv_val, bv->bv_len + 1 );
3060 len = snprintf( buf, buflen, "%lu", lc->lcb_conn->c_connid );