1 /* bind.c - ldap backend bind function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2011 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_back_controls_free( op, rs, &ctrls );
280 if ( rc == LDAP_SUCCESS ) {
281 op->o_conn->c_authz_cookie = op->o_bd->be_private;
283 /* If defined, proxyAuthz will be used also when
284 * back-ldap is the authorizing backend; for this
285 * purpose, after a successful bind the connection
286 * is left for further binds, and further operations
287 * on this client connection will use a default
288 * connection with identity assertion */
289 /* NOTE: use with care */
290 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
291 ldap_back_release_conn( li, lc );
295 /* rebind is now done inside ldap_back_proxy_authz_bind()
296 * in case of success */
297 LDAP_BACK_CONN_ISBOUND_SET( lc );
298 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
300 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
301 memset( lc->lc_cred.bv_val, 0,
302 lc->lc_cred.bv_len );
305 if ( LDAP_BACK_SAVECRED( li ) ) {
306 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
307 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
310 lc->lc_cred.bv_len = 0;
314 /* must re-insert if local DN changed as result of bind */
315 if ( !LDAP_BACK_CONN_ISBOUND( lc )
316 || ( !dn_match( &op->o_req_ndn, &lc->lc_local_ndn )
317 && !LDAP_BACK_PCONN_ISPRIV( lc ) ) )
322 /* wait for all other ops to release the connection */
324 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
325 if ( lc->lc_refcnt > 1 ) {
326 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
327 ldap_pvt_thread_yield();
331 #if LDAP_BACK_PRINT_CONNTREE > 0
332 ldap_back_print_conntree( li, ">>> ldap_back_bind" );
333 #endif /* LDAP_BACK_PRINT_CONNTREE */
335 assert( lc->lc_refcnt == 1 );
336 ldap_back_conn_delete( li, lc );
338 /* delete all cached connections with the current connection */
339 if ( LDAP_BACK_SINGLECONN( li ) ) {
340 while ( ( tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc, ldap_back_conn_cmp ) ) != NULL )
342 assert( !LDAP_BACK_PCONN_ISPRIV( lc ) );
343 Debug( LDAP_DEBUG_TRACE,
344 "=>ldap_back_bind: destroying conn %lu (refcnt=%u)\n",
345 lc->lc_conn->c_connid, lc->lc_refcnt, 0 );
347 if ( tmplc->lc_refcnt != 0 ) {
349 LDAP_BACK_CONN_TAINTED_SET( tmplc );
350 LDAP_BACK_CONN_CACHED_CLEAR( tmplc );
354 * Needs a test because the handler may be corrupted,
355 * and calling ldap_unbind on a corrupted header results
356 * in a segmentation fault
358 ldap_back_conn_free( tmplc );
363 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
364 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
365 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
366 LDAP_BACK_PCONN_ROOTDN_SET( lc, op );
368 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
369 ldap_back_conndn_cmp, ldap_back_conndn_dup );
372 #if LDAP_BACK_PRINT_CONNTREE > 0
373 ldap_back_print_conntree( li, "<<< ldap_back_bind" );
374 #endif /* LDAP_BACK_PRINT_CONNTREE */
376 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
379 LDAP_BACK_CONN_CACHED_SET( lc );
383 /* duplicate; someone else successfully bound
384 * on the same connection with the same identity;
385 * we can do this because lc_refcnt == 1 */
386 ldap_back_conn_free( lc );
392 ldap_back_release_conn( li, lc );
399 * ldap_back_conndn_cmp
401 * compares two ldapconn_t based on the value of the conn pointer
402 * and of the local DN; used by avl stuff for insert, lookup
406 ldap_back_conndn_cmp( const void *c1, const void *c2 )
408 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
409 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
412 /* If local DNs don't match, it is definitely not a match */
413 /* For shared sessions, conn is NULL. Only explicitly
414 * bound sessions will have non-NULL conn.
416 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
418 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
425 * ldap_back_conndnlc_cmp
427 * compares two ldapconn_t based on the value of the conn pointer,
428 * the local DN and the lc pointer; used by avl stuff for insert, lookup
432 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
434 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
435 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
438 /* If local DNs don't match, it is definitely not a match */
439 /* For shared sessions, conn is NULL. Only explicitly
440 * bound sessions will have non-NULL conn.
442 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
444 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
446 rc = SLAP_PTRCMP( lc1, lc2 );
456 * compares two ldapconn_t based on the value of the conn pointer;
457 * used by avl stuff for delete of all conns with the same connid
460 ldap_back_conn_cmp( const void *c1, const void *c2 )
462 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
463 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
465 /* For shared sessions, conn is NULL. Only explicitly
466 * bound sessions will have non-NULL conn.
468 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
472 * ldap_back_conndn_dup
474 * returns -1 in case a duplicate ldapconn_t has been inserted;
478 ldap_back_conndn_dup( void *c1, void *c2 )
480 ldapconn_t *lc1 = (ldapconn_t *)c1;
481 ldapconn_t *lc2 = (ldapconn_t *)c2;
483 /* Cannot have more than one shared session with same DN */
484 if ( lc1->lc_conn == lc2->lc_conn &&
485 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
494 ldap_back_freeconn( ldapinfo_t *li, ldapconn_t *lc, int dolock )
497 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
500 #if LDAP_BACK_PRINT_CONNTREE > 0
501 ldap_back_print_conntree( li, ">>> ldap_back_freeconn" );
502 #endif /* LDAP_BACK_PRINT_CONNTREE */
504 (void)ldap_back_conn_delete( li, lc );
506 if ( lc->lc_refcnt == 0 ) {
507 ldap_back_conn_free( (void *)lc );
510 #if LDAP_BACK_PRINT_CONNTREE > 0
511 ldap_back_print_conntree( li, "<<< ldap_back_freeconn" );
512 #endif /* LDAP_BACK_PRINT_CONNTREE */
515 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
532 int rc = LDAP_SUCCESS;
534 /* start TLS ("tls-[try-]{start,propagate}" statements) */
535 if ( ( LDAP_BACK_USE_TLS_F( flags ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS_F( flags ) ) )
536 && !ldap_is_ldaps_url( url ) )
538 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
540 * use asynchronous StartTLS
541 * in case, chase referral (not implemented yet)
545 if ( protocol == 0 ) {
546 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
550 if ( protocol < LDAP_VERSION3 ) {
551 /* we should rather bail out... */
552 rc = LDAP_UNWILLING_TO_PERFORM;
553 *text = "invalid protocol version";
556 if ( rc == LDAP_SUCCESS ) {
557 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
560 if ( rc == LDAP_SUCCESS ) {
561 LDAPMessage *res = NULL;
564 LDAP_BACK_TV_SET( &tv );
567 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
569 rc = LDAP_UNAVAILABLE;
571 } else if ( rc == 0 ) {
572 if ( retries != LDAP_BACK_RETRY_NEVER ) {
573 ldap_pvt_thread_yield();
577 LDAP_BACK_TV_SET( &tv );
580 rc = LDAP_UNAVAILABLE;
582 } else if ( rc == LDAP_RES_EXTENDED ) {
583 struct berval *data = NULL;
585 rc = ldap_parse_extended_result( ld, res,
587 if ( rc == LDAP_SUCCESS ) {
589 rc = ldap_parse_result( ld, res, &rs.sr_err,
590 NULL, NULL, NULL, NULL, 1 );
591 if ( rc != LDAP_SUCCESS ) {
594 rc = slap_map_api2result( &rs );
597 /* FIXME: in case a referral
598 * is returned, should we try
599 * using it instead of the
601 if ( rc == LDAP_SUCCESS ) {
602 rc = ldap_install_tls( ld );
604 } else if ( rc == LDAP_REFERRAL ) {
605 rc = LDAP_UNWILLING_TO_PERFORM;
606 *text = "unwilling to chase referral returned by Start TLS exop";
610 if ( data->bv_val ) {
611 ber_memfree( data->bv_val );
625 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
627 * use synchronous StartTLS
629 rc = ldap_start_tls_s( ld, NULL, NULL );
630 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
632 /* if StartTLS is requested, only attempt it if the URL
633 * is not "ldaps://"; this may occur not only in case
634 * of misconfiguration, but also when used in the chain
635 * overlay, where the "uri" can be parsed out of a referral */
641 case LDAP_SERVER_DOWN:
645 if ( LDAP_BACK_TLS_CRITICAL_F( flags ) ) {
646 *text = "could not start TLS";
650 /* in case Start TLS is not critical */
662 #endif /* HAVE_TLS */
665 ldap_back_prepare_conn( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
667 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
671 int is_tls = op->o_conn->c_is_tls;
672 int flags = li->li_flags;
673 time_t lctime = (time_t)(-1);
675 #endif /* HAVE_TLS */
677 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
678 rs->sr_err = ldap_initialize( &ld, li->li_uri );
679 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
680 if ( rs->sr_err != LDAP_SUCCESS ) {
684 if ( li->li_urllist_f ) {
685 ldap_set_urllist_proc( ld, li->li_urllist_f, li->li_urllist_p );
688 /* Set LDAP version. This will always succeed: If the client
689 * bound with a particular version, then so can we.
691 if ( li->li_version != 0 ) {
692 version = li->li_version;
694 } else if ( op->o_protocol != 0 ) {
695 version = op->o_protocol;
698 /* assume it's an internal op; set to LDAPv3 */
699 version = LDAP_VERSION3;
701 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
703 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
704 ldap_set_option( ld, LDAP_OPT_REFERRALS,
705 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
707 if ( li->li_network_timeout > 0 ) {
710 tv.tv_sec = li->li_network_timeout;
712 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
716 if ( LDAP_BACK_CONN_ISPRIV( lc ) ) {
717 /* See "rationale" comment in ldap_back_getconn() */
718 if ( li->li_acl_authmethod == LDAP_AUTH_NONE &&
719 li->li_idassert_authmethod != LDAP_AUTH_NONE )
720 sb = &li->li_idassert.si_bc;
724 } else if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
725 sb = &li->li_idassert.si_bc;
731 if ( sb->sb_tls_do_init ) {
732 bindconf_tls_set( sb, ld );
733 } else if ( sb->sb_tls_ctx ) {
734 ldap_set_option( ld, LDAP_OPT_X_TLS_CTX, sb->sb_tls_ctx );
737 /* if required by the bindconf configuration, force TLS */
738 if ( ( sb == &li->li_acl || sb == &li->li_idassert.si_bc ) &&
741 flags |= LDAP_BACK_F_USE_TLS;
744 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
745 assert( li->li_uri_mutex_do_not_lock == 0 );
746 li->li_uri_mutex_do_not_lock = 1;
747 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
748 li->li_uri, flags, li->li_nretries, &rs->sr_text );
749 li->li_uri_mutex_do_not_lock = 0;
750 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
751 if ( rs->sr_err != LDAP_SUCCESS ) {
752 ldap_unbind_ext( ld, NULL, NULL );
753 rs->sr_text = "Start TLS failed";
756 } else if ( li->li_idle_timeout ) {
757 /* only touch when activity actually took place... */
760 #endif /* HAVE_TLS */
766 LDAP_BACK_CONN_ISTLS_SET( lc );
768 LDAP_BACK_CONN_ISTLS_CLEAR( lc );
770 if ( lctime != (time_t)(-1) ) {
771 lc->lc_time = lctime;
773 #endif /* HAVE_TLS */
776 if ( rs->sr_err != LDAP_SUCCESS ) {
777 rs->sr_err = slap_map_api2result( rs );
778 if ( sendok & LDAP_BACK_SENDERR ) {
779 if ( rs->sr_text == NULL ) {
780 rs->sr_text = "Proxy connection initialization failed";
782 send_ldap_result( op, rs );
786 if ( li->li_conn_ttl > 0 ) {
787 lc->lc_create_time = op->o_time;
798 ldap_back_send_t sendok,
799 struct berval *binddn,
800 struct berval *bindcred )
802 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
803 ldapconn_t *lc = NULL,
806 lookupconn = !( sendok & LDAP_BACK_BINDING );
808 /* if the server is quarantined, and
809 * - the current interval did not expire yet, or
810 * - no more retries should occur,
811 * don't return the connection */
812 if ( li->li_isquarantined ) {
813 slap_retry_info_t *ri = &li->li_quarantine;
816 if ( li->li_quarantine.ri_interval ) {
817 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
818 if ( li->li_isquarantined == LDAP_BACK_FQ_YES ) {
819 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
820 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
822 Debug( LDAP_DEBUG_ANY,
823 "%s: ldap_back_getconn quarantine "
824 "retry block #%d try #%d.\n",
825 op->o_log_prefix, ri->ri_idx, ri->ri_count );
826 li->li_isquarantined = LDAP_BACK_FQ_RETRYING;
829 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
833 rs->sr_err = LDAP_UNAVAILABLE;
834 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
835 rs->sr_text = "Target is quarantined";
836 send_ldap_result( op, rs );
842 /* Internal searches are privileged and shared. So is root. */
843 if ( op->o_do_not_cache || be_isroot( op ) ) {
844 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
845 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
846 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
849 struct berval tmpbinddn,
856 if ( binddn == NULL ) {
859 if ( bindcred == NULL ) {
860 bindcred = &tmpbindcred;
862 if ( op->o_tag == LDAP_REQ_BIND ) {
863 save_o_dn = op->o_dn;
864 save_o_ndn = op->o_ndn;
865 op->o_dn = op->o_req_dn;
866 op->o_ndn = op->o_req_ndn;
868 isproxyauthz = ldap_back_is_proxy_authz( op, rs, sendok, binddn, bindcred );
869 if ( op->o_tag == LDAP_REQ_BIND ) {
870 op->o_dn = save_o_dn;
871 op->o_ndn = save_o_ndn;
873 if ( isproxyauthz == -1 ) {
877 lc_curr.lc_local_ndn = op->o_ndn;
878 /* Explicit binds must not be shared;
879 * however, explicit binds are piped in a special connection
880 * when idassert is to occur with "override" set */
881 if ( op->o_tag == LDAP_REQ_BIND && !isproxyauthz ) {
882 lc_curr.lc_conn = op->o_conn;
885 if ( isproxyauthz && !( sendok & LDAP_BACK_BINDING ) ) {
886 lc_curr.lc_local_ndn = *binddn;
887 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
888 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
890 } else if ( isproxyauthz && ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) {
891 lc_curr.lc_local_ndn = slap_empty_bv;
892 LDAP_BACK_PCONN_BIND_SET( &lc_curr, op );
893 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
896 } else if ( SLAP_IS_AUTHZ_BACKEND( op ) ) {
897 lc_curr.lc_conn = op->o_conn;
900 LDAP_BACK_PCONN_ANON_SET( &lc_curr, op );
905 /* Explicit Bind requests always get their own conn */
908 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
909 if ( LDAP_BACK_PCONN_ISPRIV( &lc_curr ) ) {
910 /* lookup a conn that's not binding */
911 LDAP_TAILQ_FOREACH( lc,
912 &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv,
915 if ( !LDAP_BACK_CONN_BINDING( lc ) && lc->lc_refcnt == 0 ) {
921 if ( lc != LDAP_TAILQ_LAST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
924 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
926 LDAP_TAILQ_ENTRY_INIT( lc, lc_q );
927 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
931 } else if ( !LDAP_BACK_USE_TEMPORARIES( li )
932 && li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_num == li->li_conn_priv_max )
934 lc = LDAP_TAILQ_FIRST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv );
939 /* Searches for a ldapconn in the avl tree */
940 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
941 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
945 /* Don't reuse connections while they're still binding */
946 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
947 if ( !LDAP_BACK_USE_TEMPORARIES( li ) ) {
948 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
950 ldap_pvt_thread_yield();
957 if ( op->o_tag == LDAP_REQ_BIND ) {
958 /* right now, this is the only possible case */
959 assert( ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) );
960 LDAP_BACK_CONN_BINDING_SET( lc );
963 refcnt = ++lc->lc_refcnt;
966 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
969 /* Looks like we didn't get a bind. Open a new session... */
971 lc = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
972 lc->lc_flags = li->li_flags;
973 lc->lc_lcflags = lc_curr.lc_lcflags;
974 if ( ldap_back_prepare_conn( lc, op, rs, sendok ) != LDAP_SUCCESS ) {
979 if ( sendok & LDAP_BACK_BINDING ) {
980 LDAP_BACK_CONN_BINDING_SET( lc );
983 lc->lc_conn = lc_curr.lc_conn;
984 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
987 * the rationale is: connections as the rootdn are privileged,
988 * so li_acl is to be used; however, in some cases
989 * one already configured identity assertion with a highly
990 * privileged idassert_authcDN, so if li_acl is not configured
991 * and idassert is, use idassert instead.
993 * might change in the future, because it's preferable
994 * to make clear what identity is being used, since
995 * the only drawback is that one risks to configure
996 * the same identity twice...
998 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
999 if ( li->li_acl_authmethod == LDAP_AUTH_NONE &&
1000 li->li_idassert_authmethod != LDAP_AUTH_NONE ) {
1001 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
1002 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
1005 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
1006 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
1008 LDAP_BACK_CONN_ISPRIV_SET( lc );
1010 } else if ( LDAP_BACK_CONN_ISIDASSERT( &lc_curr ) ) {
1011 if ( !LDAP_BACK_PCONN_ISBIND( &lc_curr ) ) {
1012 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
1013 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
1015 LDAP_BACK_CONN_ISIDASSERT_SET( lc );
1018 BER_BVZERO( &lc->lc_cred );
1019 BER_BVZERO( &lc->lc_bound_ndn );
1020 if ( !BER_BVISEMPTY( &op->o_ndn )
1021 && SLAP_IS_AUTHZ_BACKEND( op ) )
1023 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
1028 /* if start TLS failed but it was not mandatory,
1029 * check if the non-TLS connection was already
1030 * in cache; in case, destroy the newly created
1031 * connection and use the existing one */
1032 if ( LDAP_BACK_PCONN_ISTLS( lc )
1033 && !ldap_tls_inplace( lc->lc_ld ) )
1035 ldapconn_t *tmplc = NULL;
1036 int idx = LDAP_BACK_CONN2PRIV( &lc_curr ) - 1;
1038 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1039 LDAP_TAILQ_FOREACH( tmplc,
1040 &li->li_conn_priv[ idx ].lic_priv,
1043 if ( !LDAP_BACK_CONN_BINDING( tmplc ) ) {
1048 if ( tmplc != NULL ) {
1049 refcnt = ++tmplc->lc_refcnt;
1050 ldap_back_conn_free( lc );
1053 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1055 if ( tmplc != NULL ) {
1059 #endif /* HAVE_TLS */
1061 /* Inserts the newly created ldapconn in the avl tree */
1062 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1064 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1066 assert( lc->lc_refcnt == 1 );
1068 #if LDAP_BACK_PRINT_CONNTREE > 0
1069 ldap_back_print_conntree( li, ">>> ldap_back_getconn(insert)" );
1070 #endif /* LDAP_BACK_PRINT_CONNTREE */
1072 if ( LDAP_BACK_PCONN_ISPRIV( lc ) ) {
1073 if ( li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num < li->li_conn_priv_max ) {
1074 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv, lc, lc_q );
1075 li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num++;
1076 LDAP_BACK_CONN_CACHED_SET( lc );
1079 LDAP_BACK_CONN_TAINTED_SET( lc );
1084 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
1085 ldap_back_conndn_cmp, ldap_back_conndn_dup );
1086 LDAP_BACK_CONN_CACHED_SET( lc );
1089 #if LDAP_BACK_PRINT_CONNTREE > 0
1090 ldap_back_print_conntree( li, "<<< ldap_back_getconn(insert)" );
1091 #endif /* LDAP_BACK_PRINT_CONNTREE */
1093 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1095 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1096 char buf[ SLAP_TEXT_BUFLEN ];
1098 snprintf( buf, sizeof( buf ),
1099 "lc=%p inserted refcnt=%u rc=%d",
1100 (void *)lc, refcnt, rs->sr_err );
1102 Debug( LDAP_DEBUG_TRACE,
1103 "=>ldap_back_getconn: %s: %s\n",
1104 op->o_log_prefix, buf, 0 );
1107 if ( !LDAP_BACK_PCONN_ISPRIV( lc ) ) {
1108 /* Err could be -1 in case a duplicate ldapconn is inserted */
1109 switch ( rs->sr_err ) {
1114 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1115 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( li ) ) {
1116 /* duplicate: free and try to get the newly created one */
1117 ldap_back_conn_free( lc );
1122 /* taint connection, so that it'll be freed when released */
1123 LDAP_BACK_CONN_TAINTED_SET( lc );
1127 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1128 ldap_back_conn_free( lc );
1129 rs->sr_err = LDAP_OTHER;
1130 rs->sr_text = "Proxy bind collision";
1131 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1132 send_ldap_result( op, rs );
1141 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
1142 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
1146 /* let it be used, but taint/delete it so that
1147 * no-one else can look it up any further */
1148 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1150 #if LDAP_BACK_PRINT_CONNTREE > 0
1151 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1152 #endif /* LDAP_BACK_PRINT_CONNTREE */
1154 (void)ldap_back_conn_delete( li, lc );
1155 LDAP_BACK_CONN_TAINTED_SET( lc );
1157 #if LDAP_BACK_PRINT_CONNTREE > 0
1158 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1159 #endif /* LDAP_BACK_PRINT_CONNTREE */
1161 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1164 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1165 char buf[ SLAP_TEXT_BUFLEN ];
1167 snprintf( buf, sizeof( buf ),
1168 "conn %p fetched refcnt=%u%s",
1170 expiring ? " expiring" : "" );
1171 Debug( LDAP_DEBUG_TRACE,
1172 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
1178 #endif /* HAVE_TLS */
1184 ldap_back_release_conn_lock(
1190 ldapconn_t *lc = *lcp;
1193 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1195 assert( lc->lc_refcnt > 0 );
1196 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1198 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
1199 ldap_back_freeconn( li, lc, 0 );
1203 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1208 ldap_back_quarantine(
1212 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1214 slap_retry_info_t *ri = &li->li_quarantine;
1216 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
1218 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1219 time_t new_last = slap_get_time();
1221 switch ( li->li_isquarantined ) {
1222 case LDAP_BACK_FQ_NO:
1223 if ( ri->ri_last == new_last ) {
1227 Debug( LDAP_DEBUG_ANY,
1228 "%s: ldap_back_quarantine enter.\n",
1229 op->o_log_prefix, 0, 0 );
1235 case LDAP_BACK_FQ_RETRYING:
1236 Debug( LDAP_DEBUG_ANY,
1237 "%s: ldap_back_quarantine block #%d try #%d failed.\n",
1238 op->o_log_prefix, ri->ri_idx, ri->ri_count );
1241 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1242 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1253 li->li_isquarantined = LDAP_BACK_FQ_YES;
1254 ri->ri_last = new_last;
1256 } else if ( li->li_isquarantined != LDAP_BACK_FQ_NO ) {
1257 if ( ri->ri_last == slap_get_time() ) {
1261 Debug( LDAP_DEBUG_ANY,
1262 "%s: ldap_back_quarantine exit (%d) err=%d.\n",
1263 op->o_log_prefix, li->li_isquarantined, rs->sr_err );
1265 if ( li->li_quarantine_f ) {
1266 (void)li->li_quarantine_f( li, li->li_quarantine_p );
1271 li->li_isquarantined = LDAP_BACK_FQ_NO;
1275 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
1279 ldap_back_dobind_cb(
1284 ber_tag_t *tptr = op->o_callback->sc_private;
1286 rs->sr_tag = slap_req2res( op->o_tag );
1288 return SLAP_CB_CONTINUE;
1292 * ldap_back_dobind_int
1294 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1297 ldap_back_dobind_int(
1301 ldap_back_send_t sendok,
1305 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1308 struct berval binddn = slap_empty_bv,
1309 bindcred = slap_empty_bv;
1315 ber_tag_t o_tag = op->o_tag;
1316 slap_callback cb = {0};
1319 assert( lcp != NULL );
1320 assert( retries >= 0 );
1322 if ( sendok & LDAP_BACK_GETCONN ) {
1323 assert( *lcp == NULL );
1325 lc = ldap_back_getconn( op, rs, sendok, &binddn, &bindcred );
1335 assert( lc != NULL );
1339 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1342 if ( binding == 0 ) {
1343 /* check if already bound */
1344 rc = isbound = LDAP_BACK_CONN_ISBOUND( lc );
1347 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1352 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
1353 /* if someone else is about to bind it, give up and retry */
1355 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1357 ldap_pvt_thread_yield();
1361 /* otherwise this thread will bind it */
1362 LDAP_BACK_CONN_BINDING_SET( lc );
1368 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1372 * FIXME: we need to let clients use proxyAuthz
1373 * otherwise we cannot do symmetric pools of servers;
1374 * we have to live with the fact that a user can
1375 * authorize itself as any ID that is allowed
1376 * by the authzTo directive of the "proxyauthzdn".
1379 * NOTE: current Proxy Authorization specification
1380 * and implementation do not allow proxy authorization
1381 * control to be provided with Bind requests
1384 * if no bind took place yet, but the connection is bound
1385 * and the "idassert-authcDN" (or other ID) is set,
1386 * then bind as the asserting identity and explicitly
1387 * add the proxyAuthz control to every operation with the
1388 * dn bound to the connection as control value.
1389 * This is done also if this is the authorizing backend,
1390 * but the "override" flag is given to idassert.
1391 * It allows to use SASL bind and yet proxyAuthz users
1393 op->o_tag = LDAP_REQ_BIND;
1394 cb.sc_next = op->o_callback;
1395 cb.sc_private = &o_tag;
1396 cb.sc_response = ldap_back_dobind_cb;
1397 op->o_callback = &cb;
1399 if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
1400 if ( BER_BVISEMPTY( &binddn ) && BER_BVISEMPTY( &bindcred ) ) {
1401 /* if we got here, it shouldn't return result */
1402 rc = ldap_back_is_proxy_authz( op, rs,
1403 LDAP_BACK_DONTSEND, &binddn, &bindcred );
1406 rc = ldap_back_proxy_authz_bind( lc, op, rs, sendok, &binddn, &bindcred );
1410 #ifdef HAVE_CYRUS_SASL
1411 if ( LDAP_BACK_CONN_ISPRIV( lc )) {
1413 if ( li->li_acl_authmethod != LDAP_AUTH_NONE )
1416 sb = &li->li_idassert.si_bc;
1418 if ( sb->sb_method == LDAP_AUTH_SASL ) {
1419 void *defaults = NULL;
1421 if ( sb->sb_secprops != NULL ) {
1422 rc = ldap_set_option( lc->lc_ld,
1423 LDAP_OPT_X_SASL_SECPROPS, sb->sb_secprops );
1425 if ( rc != LDAP_OPT_SUCCESS ) {
1426 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
1427 "(SECPROPS,\"%s\") failed!\n",
1428 sb->sb_secprops, 0, 0 );
1433 defaults = lutil_sasl_defaults( lc->lc_ld,
1434 sb->sb_saslmech.bv_val,
1435 sb->sb_realm.bv_val,
1436 sb->sb_authcId.bv_val,
1439 if ( defaults == NULL ) {
1440 rs->sr_err = LDAP_OTHER;
1441 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1442 if ( sendok & LDAP_BACK_SENDERR ) {
1443 send_ldap_result( op, rs );
1448 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
1449 sb->sb_binddn.bv_val,
1450 sb->sb_saslmech.bv_val, NULL, NULL,
1451 LDAP_SASL_QUIET, lutil_sasl_interact,
1454 lutil_sasl_freedefs( defaults );
1456 switch ( rs->sr_err ) {
1458 LDAP_BACK_CONN_ISBOUND_SET( lc );
1461 case LDAP_LOCAL_ERROR:
1462 /* list client API error codes that require
1463 * to taint the connection */
1464 /* FIXME: should actually retry? */
1465 LDAP_BACK_CONN_TAINTED_SET( lc );
1470 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1471 rs->sr_err = slap_map_api2result( rs );
1472 if ( sendok & LDAP_BACK_SENDERR ) {
1473 send_ldap_result( op, rs );
1478 if ( LDAP_BACK_QUARANTINE( li ) ) {
1479 ldap_back_quarantine( op, rs );
1485 #endif /* HAVE_CYRUS_SASL */
1488 if ( BER_BVISNULL( &lc->lc_cred ) ) {
1490 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) && !BER_BVISEMPTY( &lc->lc_bound_ndn ) ) {
1491 Debug( LDAP_DEBUG_ANY, "%s ldap_back_dobind_int: DN=\"%s\" without creds, binding anonymously",
1492 op->o_log_prefix, lc->lc_bound_ndn.bv_val, 0 );
1496 tmp_dn = lc->lc_bound_ndn.bv_val;
1498 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1500 LDAP_SASL_SIMPLE, &lc->lc_cred,
1501 NULL, NULL, &msgid );
1503 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
1504 if ( retries != LDAP_BACK_RETRY_NEVER ) {
1506 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1509 assert( lc->lc_refcnt > 0 );
1510 if ( lc->lc_refcnt == 1 ) {
1511 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1514 /* lc here must be the regular lc, reset and ready for init */
1515 rs->sr_err = ldap_back_prepare_conn( lc, op, rs, sendok );
1516 if ( rs->sr_err != LDAP_SUCCESS ) {
1517 sendok &= ~LDAP_BACK_SENDERR;
1523 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1526 if ( rs->sr_err == LDAP_SUCCESS ) {
1527 if ( retries > 0 ) {
1534 assert( lc->lc_refcnt == 1 );
1536 ldap_back_freeconn( li, lc, dolock );
1538 rs->sr_err = slap_map_api2result( rs );
1540 if ( LDAP_BACK_QUARANTINE( li ) ) {
1541 ldap_back_quarantine( op, rs );
1544 if ( rs->sr_err != LDAP_SUCCESS &&
1545 ( sendok & LDAP_BACK_SENDERR ) )
1547 if ( op->o_callback == &cb )
1548 op->o_callback = cb.sc_next;
1550 rs->sr_text = "Proxy can't contact remote server";
1551 send_ldap_result( op, rs );
1558 rc = ldap_back_op_result( lc, op, rs, msgid,
1559 -1, ( sendok | LDAP_BACK_BINDING ) );
1560 if ( rc == LDAP_SUCCESS ) {
1561 op->o_conn->c_authz_cookie = op->o_bd->be_private;
1562 LDAP_BACK_CONN_ISBOUND_SET( lc );
1566 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1567 rc = LDAP_BACK_CONN_ISBOUND( lc );
1569 ldap_back_release_conn_lock( li, lcp, dolock );
1571 } else if ( LDAP_BACK_SAVECRED( li ) ) {
1572 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1576 if ( op->o_callback == &cb )
1577 op->o_callback = cb.sc_next;
1586 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1589 ldap_back_dobind( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1591 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1593 return ldap_back_dobind_int( lcp, op, rs,
1594 ( sendok | LDAP_BACK_GETCONN ), li->li_nretries, 1 );
1598 * ldap_back_default_rebind
1600 * This is a callback used for chasing referrals using the same
1601 * credentials as the original user on this session.
1604 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1605 ber_int_t msgid, void *params )
1607 ldapconn_t *lc = (ldapconn_t *)params;
1610 /* ... otherwise we couldn't get here */
1611 assert( lc != NULL );
1613 if ( !ldap_tls_inplace( ld ) ) {
1614 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1616 const char *text = NULL;
1618 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1619 LDAP_BACK_RETRY_DEFAULT, &text );
1620 if ( rc != LDAP_SUCCESS ) {
1624 #endif /* HAVE_TLS */
1626 /* FIXME: add checks on the URL/identity? */
1628 return ldap_sasl_bind_s( ld,
1629 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1630 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1634 * ldap_back_default_urllist
1637 ldap_back_default_urllist(
1639 LDAPURLDesc **urllist,
1643 ldapinfo_t *li = (ldapinfo_t *)params;
1644 LDAPURLDesc **urltail;
1646 if ( urllist == url ) {
1647 return LDAP_SUCCESS;
1650 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1653 *urltail = *urllist;
1657 if ( !li->li_uri_mutex_do_not_lock ) {
1658 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1662 ch_free( li->li_uri );
1665 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1667 if ( !li->li_uri_mutex_do_not_lock ) {
1668 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1671 return LDAP_SUCCESS;
1680 ldap_back_send_t sendok )
1682 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1684 /* default behavior */
1685 if ( LDAP_BACK_ABANDON( li ) ) {
1686 return ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1689 if ( LDAP_BACK_IGNORE( li ) ) {
1690 return ldap_pvt_discard( lc->lc_ld, msgid );
1693 if ( LDAP_BACK_CANCEL( li ) ) {
1694 /* FIXME: asynchronous? */
1695 return ldap_cancel_s( lc->lc_ld, msgid, NULL, NULL );
1704 ldap_back_op_result(
1710 ldap_back_send_t sendok )
1712 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1717 LDAPControl **ctrls = NULL;
1720 rs->sr_matched = NULL;
1722 rs->sr_ctrls = NULL;
1724 /* if the error recorded in the reply corresponds
1725 * to a successful state, get the error from the
1726 * remote server response */
1727 if ( LDAP_ERR_OK( rs->sr_err ) ) {
1730 LDAPMessage *res = NULL;
1731 time_t stoptime = (time_t)(-1);
1732 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
1733 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
1734 const char *timeout_text = "Operation timed out";
1736 /* if timeout is not specified, compute and use
1737 * the one specific to the ongoing operation */
1738 if ( timeout == (time_t)(-1) ) {
1739 slap_op_t opidx = slap_req2op( op->o_tag );
1741 if ( opidx == SLAP_OP_SEARCH ) {
1742 if ( op->ors_tlimit <= 0 ) {
1746 timeout = op->ors_tlimit;
1747 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
1748 timeout_text = NULL;
1752 timeout = li->li_timeout[ opidx ];
1756 /* better than nothing :) */
1757 if ( timeout == 0 ) {
1758 if ( li->li_idle_timeout ) {
1759 timeout = li->li_idle_timeout;
1761 } else if ( li->li_conn_ttl ) {
1762 timeout = li->li_conn_ttl;
1767 stoptime = op->o_time + timeout;
1770 LDAP_BACK_TV_SET( &tv );
1773 /* if result parsing fails, note the failure reason */
1774 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1777 if ( timeout && slap_get_time() > stoptime ) {
1778 if ( sendok & LDAP_BACK_BINDING ) {
1779 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1782 /* let it be used, but taint/delete it so that
1783 * no-one else can look it up any further */
1784 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1786 #if LDAP_BACK_PRINT_CONNTREE > 0
1787 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1788 #endif /* LDAP_BACK_PRINT_CONNTREE */
1790 (void)ldap_back_conn_delete( li, lc );
1791 LDAP_BACK_CONN_TAINTED_SET( lc );
1793 #if LDAP_BACK_PRINT_CONNTREE > 0
1794 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1795 #endif /* LDAP_BACK_PRINT_CONNTREE */
1796 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1799 (void)ldap_back_cancel( lc, op, rs, msgid, sendok );
1801 rs->sr_err = timeout_err;
1802 rs->sr_text = timeout_text;
1807 LDAP_BACK_TV_SET( &tv );
1808 ldap_pvt_thread_yield();
1812 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1817 /* otherwise get the result; if it is not
1818 * LDAP_SUCCESS, record it in the reply
1819 * structure (this includes
1820 * LDAP_COMPARE_{TRUE|FALSE}) */
1822 /* only touch when activity actually took place... */
1823 if ( li->li_idle_timeout && lc ) {
1824 lc->lc_time = op->o_time;
1827 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1828 &match, &text, &refs, &ctrls, 1 );
1829 if ( rc == LDAP_SUCCESS ) {
1834 rs->sr_err = slap_map_api2result( rs );
1836 /* RFC 4511: referrals can only appear
1837 * if result code is LDAP_REFERRAL */
1839 && refs[ 0 ] != NULL
1840 && refs[ 0 ][ 0 ] != '\0' )
1842 if ( rs->sr_err != LDAP_REFERRAL ) {
1843 Debug( LDAP_DEBUG_ANY,
1844 "%s ldap_back_op_result: "
1845 "got referrals with err=%d\n",
1852 for ( i = 0; refs[ i ] != NULL; i++ )
1854 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1856 for ( i = 0; refs[ i ] != NULL; i++ ) {
1857 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1859 BER_BVZERO( &rs->sr_ref[ i ] );
1862 } else if ( rs->sr_err == LDAP_REFERRAL ) {
1863 Debug( LDAP_DEBUG_ANY,
1864 "%s ldap_back_op_result: "
1865 "got err=%d with null "
1866 "or empty referrals\n",
1870 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1873 if ( ctrls != NULL ) {
1874 rs->sr_ctrls = ctrls;
1879 /* if the error in the reply structure is not
1880 * LDAP_SUCCESS, try to map it from client
1881 * to server error */
1882 if ( !LDAP_ERR_OK( rs->sr_err ) ) {
1883 rs->sr_err = slap_map_api2result( rs );
1885 /* internal ops ( op->o_conn == NULL )
1886 * must not reply to client */
1887 if ( op->o_conn && !op->o_do_not_cache && match ) {
1889 /* record the (massaged) matched
1890 * DN into the reply structure */
1891 rs->sr_matched = match;
1895 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1896 if ( !( sendok & LDAP_BACK_RETRYING ) ) {
1897 if ( LDAP_BACK_QUARANTINE( li ) ) {
1898 ldap_back_quarantine( op, rs );
1900 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1901 if ( rs->sr_text == NULL ) rs->sr_text = "Proxy operation retry failed";
1902 send_ldap_result( op, rs );
1906 } else if ( op->o_conn &&
1907 ( ( ( sendok & LDAP_BACK_SENDOK ) && LDAP_ERR_OK( rs->sr_err ) )
1908 || ( ( sendok & LDAP_BACK_SENDERR ) && !LDAP_ERR_OK( rs->sr_err ) ) ) )
1910 send_ldap_result( op, rs );
1914 ldap_memfree( text );
1918 /* there can't be refs with a (successful) bind */
1920 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1925 ber_memvfree( (void **)refs );
1928 /* match should not be possible with a successful bind */
1930 if ( rs->sr_matched != match ) {
1931 free( (char *)rs->sr_matched );
1933 rs->sr_matched = NULL;
1934 ldap_memfree( match );
1937 if ( ctrls != NULL ) {
1938 if ( op->o_tag == LDAP_REQ_BIND && rs->sr_err == LDAP_SUCCESS ) {
1941 for ( i = 0; ctrls[i] != NULL; i++ );
1943 rs->sr_ctrls = op->o_tmpalloc( sizeof( LDAPControl * )*( i + 1 ),
1945 for ( i = 0; ctrls[ i ] != NULL; i++ ) {
1947 ber_len_t oidlen = strlen( ctrls[i]->ldctl_oid );
1948 ber_len_t size = sizeof( LDAPControl )
1950 + ctrls[i]->ldctl_value.bv_len + 1;
1952 rs->sr_ctrls[ i ] = op->o_tmpalloc( size, op->o_tmpmemctx );
1953 rs->sr_ctrls[ i ]->ldctl_oid = (char *)&rs->sr_ctrls[ i ][ 1 ];
1954 lutil_strcopy( rs->sr_ctrls[ i ]->ldctl_oid, ctrls[i]->ldctl_oid );
1955 rs->sr_ctrls[ i ]->ldctl_value.bv_val
1956 = (char *)&rs->sr_ctrls[ i ]->ldctl_oid[oidlen + 1];
1957 rs->sr_ctrls[ i ]->ldctl_value.bv_len
1958 = ctrls[i]->ldctl_value.bv_len;
1959 ptr = lutil_memcopy( rs->sr_ctrls[ i ]->ldctl_value.bv_val,
1960 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len );
1963 rs->sr_ctrls[ i ] = NULL;
1964 rs->sr_flags |= REP_CTRLS_MUSTBEFREED;
1967 assert( rs->sr_ctrls != NULL );
1968 rs->sr_ctrls = NULL;
1971 ldap_controls_free( ctrls );
1974 return( LDAP_ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1977 /* return true if bound, false if failed */
1979 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1981 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1984 assert( lcp != NULL );
1985 assert( *lcp != NULL );
1987 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1989 if ( (*lcp)->lc_refcnt == 1 ) {
1990 int binding = LDAP_BACK_CONN_BINDING( *lcp );
1992 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1993 Debug( LDAP_DEBUG_ANY,
1994 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1995 op->o_log_prefix, li->li_uri,
1996 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1997 "" : (*lcp)->lc_bound_ndn.bv_val );
1998 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
2000 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
2001 (*lcp)->lc_ld = NULL;
2002 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
2004 /* lc here must be the regular lc, reset and ready for init */
2005 rc = ldap_back_prepare_conn( *lcp, op, rs, sendok );
2006 if ( rc != LDAP_SUCCESS ) {
2007 /* freeit, because lc_refcnt == 1 */
2008 (*lcp)->lc_refcnt = 0;
2009 (void)ldap_back_freeconn( li, *lcp, 0 );
2013 } else if ( ( sendok & LDAP_BACK_BINDING ) ) {
2015 LDAP_BACK_CONN_BINDING_SET( *lcp );
2020 rc = ldap_back_dobind_int( lcp, op, rs, sendok, 0, 0 );
2021 if ( rc == 0 && *lcp != NULL ) {
2022 /* freeit, because lc_refcnt == 1 */
2023 (*lcp)->lc_refcnt = 0;
2024 LDAP_BACK_CONN_TAINTED_SET( *lcp );
2025 (void)ldap_back_freeconn( li, *lcp, 0 );
2031 Debug( LDAP_DEBUG_TRACE,
2032 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
2033 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
2035 LDAP_BACK_CONN_TAINTED_SET( *lcp );
2036 ldap_back_release_conn_lock( li, lcp, 0 );
2037 assert( *lcp == NULL );
2039 if ( sendok & LDAP_BACK_SENDERR ) {
2040 rs->sr_err = LDAP_UNAVAILABLE;
2041 rs->sr_text = "Unable to retry";
2042 send_ldap_result( op, rs );
2046 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
2052 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
2053 struct berval *binddn, struct berval *bindcred )
2055 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2059 if ( op->o_conn == NULL || op->o_do_not_cache ) {
2063 /* don't proxyAuthz if protocol is not LDAPv3 */
2064 switch ( li->li_version ) {
2069 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2075 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2076 if ( sendok & LDAP_BACK_SENDERR ) {
2077 send_ldap_result( op, rs );
2084 *binddn = slap_empty_bv;
2085 *bindcred = slap_empty_bv;
2087 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2088 ndn = op->o_conn->c_ndn;
2094 switch ( li->li_idassert_mode ) {
2095 case LDAP_BACK_IDASSERT_LEGACY:
2096 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
2097 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
2099 *binddn = li->li_idassert_authcDN;
2100 *bindcred = li->li_idassert_passwd;
2107 /* NOTE: rootdn can always idassert */
2108 if ( BER_BVISNULL( &ndn )
2109 && li->li_idassert_authz == NULL
2110 && !( li->li_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL ) )
2112 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2113 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
2114 if ( sendok & LDAP_BACK_SENDERR ) {
2115 send_ldap_result( op, rs );
2120 rs->sr_err = LDAP_SUCCESS;
2121 *binddn = slap_empty_bv;
2122 *bindcred = slap_empty_bv;
2128 } else if ( !be_isroot( op ) ) {
2129 if ( li->li_idassert_passthru ) {
2130 struct berval authcDN;
2132 if ( BER_BVISNULL( &ndn ) ) {
2133 authcDN = slap_empty_bv;
2138 rs->sr_err = slap_sasl_matches( op, li->li_idassert_passthru,
2139 &authcDN, &authcDN );
2140 if ( rs->sr_err == LDAP_SUCCESS ) {
2146 if ( li->li_idassert_authz ) {
2147 struct berval authcDN;
2149 if ( BER_BVISNULL( &ndn ) ) {
2150 authcDN = slap_empty_bv;
2155 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
2156 &authcDN, &authcDN );
2157 if ( rs->sr_err != LDAP_SUCCESS ) {
2158 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2159 if ( sendok & LDAP_BACK_SENDERR ) {
2160 send_ldap_result( op, rs );
2165 rs->sr_err = LDAP_SUCCESS;
2166 *binddn = slap_empty_bv;
2167 *bindcred = slap_empty_bv;
2176 *binddn = li->li_idassert_authcDN;
2177 *bindcred = li->li_idassert_passwd;
2187 ldap_back_proxy_authz_bind(
2191 ldap_back_send_t sendok,
2192 struct berval *binddn,
2193 struct berval *bindcred )
2195 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2200 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2201 ndn = op->o_conn->c_ndn;
2207 if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
2208 #ifdef HAVE_CYRUS_SASL
2209 void *defaults = NULL;
2210 struct berval authzID = BER_BVNULL;
2212 LDAPControl **ctrlsp = NULL;
2213 LDAPMessage *result = NULL;
2214 const char *rmech = NULL;
2215 const char *save_text = rs->sr_text;
2218 LDAPControl ctrl, *ctrls[2];
2220 #endif /* SLAP_AUTH_DN */
2222 /* if SASL supports native authz, prepare for it */
2223 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
2224 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2226 switch ( li->li_idassert_mode ) {
2227 case LDAP_BACK_IDASSERT_OTHERID:
2228 case LDAP_BACK_IDASSERT_OTHERDN:
2229 authzID = li->li_idassert_authzID;
2232 case LDAP_BACK_IDASSERT_ANONYMOUS:
2233 BER_BVSTR( &authzID, "dn:" );
2236 case LDAP_BACK_IDASSERT_SELF:
2237 if ( BER_BVISNULL( &ndn ) ) {
2238 /* connection is not authc'd, so don't idassert */
2239 BER_BVSTR( &authzID, "dn:" );
2242 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
2243 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
2244 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
2245 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
2246 ndn.bv_val, ndn.bv_len + 1 );
2255 if ( li->li_idassert_secprops != NULL ) {
2256 rs->sr_err = ldap_set_option( lc->lc_ld,
2257 LDAP_OPT_X_SASL_SECPROPS,
2258 (void *)li->li_idassert_secprops );
2260 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
2261 rs->sr_err = LDAP_OTHER;
2262 if ( sendok & LDAP_BACK_SENDERR ) {
2263 send_ldap_result( op, rs );
2265 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2270 defaults = lutil_sasl_defaults( lc->lc_ld,
2271 li->li_idassert_sasl_mech.bv_val,
2272 li->li_idassert_sasl_realm.bv_val,
2273 li->li_idassert_authcID.bv_val,
2274 li->li_idassert_passwd.bv_val,
2276 if ( defaults == NULL ) {
2277 rs->sr_err = LDAP_OTHER;
2278 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2279 if ( sendok & LDAP_BACK_SENDERR ) {
2280 send_ldap_result( op, rs );
2286 if ( li->li_idassert_flags & LDAP_BACK_AUTH_DN_AUTHZID ) {
2287 assert( BER_BVISNULL( binddn ) );
2289 ctrl.ldctl_oid = LDAP_CONTROL_AUTHZID_REQUEST;
2290 ctrl.ldctl_iscritical = 0;
2291 BER_BVZERO( &ctrl.ldctl_value );
2296 #endif /* SLAP_AUTH_DN */
2299 rs->sr_err = ldap_sasl_interactive_bind( lc->lc_ld, binddn->bv_val,
2300 li->li_idassert_sasl_mech.bv_val,
2301 ctrlsp, NULL, LDAP_SASL_QUIET, lutil_sasl_interact, defaults,
2302 result, &rmech, &msgid );
2304 if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS )
2307 ldap_msgfree( result );
2309 if ( ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 || !result ) {
2310 ldap_get_option( lc->lc_ld, LDAP_OPT_RESULT_CODE, (void*)&rs->sr_err );
2311 ldap_get_option( lc->lc_ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&rs->sr_text );
2314 } while ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
2316 switch ( rs->sr_err ) {
2319 /* FIXME: right now, the only reason to check
2320 * response controls is RFC 3829 authzid */
2321 if ( li->li_idassert_flags & LDAP_BACK_AUTH_DN_AUTHZID ) {
2323 rc = ldap_parse_result( lc->lc_ld, result, NULL, NULL, NULL, NULL,
2325 if ( rc == LDAP_SUCCESS && ctrlsp ) {
2328 ctrl = ldap_control_find( LDAP_CONTROL_AUTHZID_RESPONSE,
2331 Debug( LDAP_DEBUG_TRACE, "%s: ldap_back_proxy_authz_bind: authzID=\"%s\" (authzid)\n",
2332 op->o_log_prefix, ctrl->ldctl_value.bv_val, 0 );
2333 if ( ctrl->ldctl_value.bv_len > STRLENOF("dn:") &&
2334 strncasecmp( ctrl->ldctl_value.bv_val, "dn:", STRLENOF("dn:") ) == 0 )
2337 bv.bv_val = &ctrl->ldctl_value.bv_val[STRLENOF("dn:")];
2338 bv.bv_len = ctrl->ldctl_value.bv_len - STRLENOF("dn:");
2339 ber_bvreplace( &lc->lc_bound_ndn, &bv );
2344 ldap_controls_free( ctrlsp );
2346 } else if ( li->li_idassert_flags & LDAP_BACK_AUTH_DN_WHOAMI ) {
2347 struct berval *val = NULL;
2348 rc = ldap_whoami_s( lc->lc_ld, &val, NULL, NULL );
2349 if ( rc == LDAP_SUCCESS && val != NULL ) {
2350 Debug( LDAP_DEBUG_TRACE, "%s: ldap_back_proxy_authz_bind: authzID=\"%s\" (whoami)\n",
2351 op->o_log_prefix, val->bv_val, 0 );
2352 if ( val->bv_len > STRLENOF("dn:") &&
2353 strncasecmp( val->bv_val, "dn:", STRLENOF("dn:") ) == 0 )
2356 bv.bv_val = &val->bv_val[STRLENOF("dn:")];
2357 bv.bv_len = val->bv_len - STRLENOF("dn:");
2358 ber_bvreplace( &lc->lc_bound_ndn, &bv );
2364 if ( ( li->li_idassert_flags & LDAP_BACK_AUTH_DN_MASK ) &&
2365 BER_BVISNULL( &lc->lc_bound_ndn ) )
2367 /* all in all, we only need it to be non-null */
2368 /* FIXME: should this be configurable? */
2369 static struct berval bv = BER_BVC("cn=authzdn");
2370 ber_bvreplace( &lc->lc_bound_ndn, &bv );
2372 #endif /* SLAP_AUTH_DN */
2373 op->o_conn->c_authz_cookie = op->o_bd->be_private;
2374 LDAP_BACK_CONN_ISBOUND_SET( lc );
2377 case LDAP_LOCAL_ERROR:
2378 /* list client API error codes that require
2379 * to taint the connection */
2380 /* FIXME: should actually retry? */
2381 LDAP_BACK_CONN_TAINTED_SET( lc );
2386 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2387 rs->sr_err = slap_map_api2result( rs );
2388 if ( sendok & LDAP_BACK_SENDERR ) {
2389 send_ldap_result( op, rs );
2394 if ( save_text != rs->sr_text ) {
2395 ldap_memfree( (char *)rs->sr_text );
2396 rs->sr_text = save_text;
2399 ldap_msgfree( result );
2401 lutil_sasl_freedefs( defaults );
2403 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
2407 #endif /* HAVE_CYRUS_SASL */
2410 switch ( li->li_idassert_authmethod ) {
2411 case LDAP_AUTH_NONE:
2412 /* FIXME: do we really need this? */
2413 BER_BVSTR( binddn, "" );
2414 BER_BVSTR( bindcred, "" );
2417 case LDAP_AUTH_SIMPLE:
2418 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
2419 binddn->bv_val, LDAP_SASL_SIMPLE,
2420 bindcred, NULL, NULL, &msgid );
2421 rc = ldap_back_op_result( lc, op, rs, msgid,
2422 -1, ( sendok | LDAP_BACK_BINDING ) );
2427 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2428 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
2429 if ( sendok & LDAP_BACK_SENDERR ) {
2430 send_ldap_result( op, rs );
2435 if ( rc == LDAP_SUCCESS ) {
2436 /* set rebind stuff in case of successful proxyAuthz bind,
2437 * so that referral chasing is attempted using the right
2439 LDAP_BACK_CONN_ISBOUND_SET( lc );
2440 op->o_conn->c_authz_cookie = op->o_bd->be_private;
2441 if ( !BER_BVISNULL( binddn ) ) {
2442 ber_bvreplace( &lc->lc_bound_ndn, binddn );
2445 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
2446 memset( lc->lc_cred.bv_val, 0,
2447 lc->lc_cred.bv_len );
2450 if ( LDAP_BACK_SAVECRED( li ) ) {
2451 if ( !BER_BVISNULL( bindcred ) ) {
2452 ber_bvreplace( &lc->lc_cred, bindcred );
2453 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
2457 lc->lc_cred.bv_len = 0;
2462 return LDAP_BACK_CONN_ISBOUND( lc );
2466 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
2467 * to existing server-side controls if required; if not,
2468 * the existing server-side controls are placed in *pctrls.
2469 * The caller, after using the controls in client API
2470 * operations, if ( *pctrls != op->o_ctrls ), should
2471 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
2472 * The function returns success if the control could
2473 * be added if required, or if it did nothing; in the future,
2474 * it might return some error if it failed.
2476 * if no bind took place yet, but the connection is bound
2477 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
2478 * and explicitly add proxyAuthz the control to every operation
2479 * with the dn bound to the connection as control value.
2481 * If no server-side controls are defined for the operation,
2482 * simply add the proxyAuthz control; otherwise, if the
2483 * proxyAuthz control is not already set, add it as
2486 * FIXME: is controls order significant for security?
2487 * ANSWER: controls ordering and interoperability
2488 * must be indicated by the specs of each control; if none
2489 * is specified, the order is irrelevant.
2492 ldap_back_proxy_authz_ctrl(
2495 struct berval *bound_ndn,
2497 slap_idassert_t *si,
2500 slap_idassert_mode_t mode;
2501 struct berval assertedID,
2505 rs->sr_err = SLAP_CB_CONTINUE;
2507 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
2508 * but if it is not set this test fails. We need a different
2509 * means to detect if idassert is enabled */
2510 if ( ( BER_BVISNULL( &si->si_bc.sb_authcId ) || BER_BVISEMPTY( &si->si_bc.sb_authcId ) )
2511 && ( BER_BVISNULL( &si->si_bc.sb_binddn ) || BER_BVISEMPTY( &si->si_bc.sb_binddn ) )
2512 && BER_BVISNULL( &si->si_bc.sb_saslmech ) )
2517 if ( !op->o_conn || op->o_do_not_cache || ( isroot = be_isroot( op ) ) ) {
2521 if ( op->o_tag == LDAP_REQ_BIND ) {
2522 ndn = op->o_req_ndn;
2524 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2525 ndn = op->o_conn->c_ndn;
2531 if ( si->si_mode == LDAP_BACK_IDASSERT_LEGACY ) {
2532 if ( op->o_proxy_authz ) {
2534 * FIXME: we do not want to perform proxyAuthz
2535 * on behalf of the client, because this would
2536 * be performed with "proxyauthzdn" privileges.
2538 * This might actually be too strict, since
2539 * the "proxyauthzdn" authzTo, and each entry's
2540 * authzFrom attributes may be crafted
2541 * to avoid unwanted proxyAuthz to take place.
2544 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2545 rs->sr_text = "proxyAuthz not allowed within namingContext";
2550 if ( !BER_BVISNULL( bound_ndn ) ) {
2554 if ( BER_BVISNULL( &ndn ) ) {
2558 if ( BER_BVISNULL( &si->si_bc.sb_binddn ) ) {
2562 } else if ( si->si_bc.sb_method == LDAP_AUTH_SASL ) {
2563 if ( ( si->si_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2565 /* already asserted in SASL via native authz */
2569 } else if ( si->si_authz && !isroot ) {
2571 struct berval authcDN;
2573 if ( BER_BVISNULL( &ndn ) ) {
2574 authcDN = slap_empty_bv;
2578 rc = slap_sasl_matches( op, si->si_authz,
2579 &authcDN, &authcDN );
2580 if ( rc != LDAP_SUCCESS ) {
2581 if ( si->si_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2582 /* ndn is not authorized
2583 * to use idassert */
2590 if ( op->o_proxy_authz ) {
2593 * 1) ignore the already set proxyAuthz control
2594 * 2) leave it in place, and don't set ours
2596 * 4) reject the operation
2598 * option (4) is very drastic
2599 * option (3) will make the remote server reject
2600 * the operation, thus being equivalent to (4)
2601 * option (2) will likely break the idassert
2602 * assumptions, so we cannot accept it;
2603 * option (1) means that we are contradicting
2604 * the client's reques.
2606 * I think (4) is the only correct choice.
2608 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2609 rs->sr_text = "proxyAuthz not allowed within namingContext";
2612 if ( op->o_is_auth_check ) {
2613 mode = LDAP_BACK_IDASSERT_NOASSERT;
2620 case LDAP_BACK_IDASSERT_LEGACY:
2621 /* original behavior:
2622 * assert the client's identity */
2623 case LDAP_BACK_IDASSERT_SELF:
2627 case LDAP_BACK_IDASSERT_ANONYMOUS:
2628 /* assert "anonymous" */
2629 assertedID = slap_empty_bv;
2632 case LDAP_BACK_IDASSERT_NOASSERT:
2633 /* don't assert; bind as proxyauthzdn */
2636 case LDAP_BACK_IDASSERT_OTHERID:
2637 case LDAP_BACK_IDASSERT_OTHERDN:
2638 /* assert idassert DN */
2639 assertedID = si->si_bc.sb_authzId;
2646 /* if we got here, "" is allowed to proxyAuthz */
2647 if ( BER_BVISNULL( &assertedID ) ) {
2648 assertedID = slap_empty_bv;
2651 /* don't idassert the bound DN (ITS#4497) */
2652 if ( dn_match( &assertedID, bound_ndn ) ) {
2656 ctrl->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
2657 ctrl->ldctl_iscritical = ( ( si->si_flags & LDAP_BACK_AUTH_PROXYAUTHZ_CRITICAL ) == LDAP_BACK_AUTH_PROXYAUTHZ_CRITICAL );
2659 switch ( si->si_mode ) {
2660 /* already in u:ID or dn:DN form */
2661 case LDAP_BACK_IDASSERT_OTHERID:
2662 case LDAP_BACK_IDASSERT_OTHERDN:
2663 ber_dupbv_x( &ctrl->ldctl_value, &assertedID, op->o_tmpmemctx );
2664 rs->sr_err = LDAP_SUCCESS;
2667 /* needs the dn: prefix */
2669 ctrl->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
2670 ctrl->ldctl_value.bv_val = op->o_tmpalloc( ctrl->ldctl_value.bv_len + 1,
2672 AC_MEMCPY( ctrl->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
2673 AC_MEMCPY( &ctrl->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
2674 assertedID.bv_val, assertedID.bv_len + 1 );
2675 rs->sr_err = LDAP_SUCCESS;
2679 /* Older versions of <draft-weltman-ldapv3-proxy> required
2680 * to encode the value of the authzID (and called it proxyDN);
2681 * this hack provides compatibility with those DSAs that
2682 * implement it this way */
2683 if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
2684 struct berval authzID = ctrl->ldctl_value;
2685 BerElementBuffer berbuf;
2686 BerElement *ber = (BerElement *)&berbuf;
2689 ber_init2( ber, 0, LBER_USE_DER );
2690 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2692 tag = ber_printf( ber, "O", &authzID );
2693 if ( tag == LBER_ERROR ) {
2694 rs->sr_err = LDAP_OTHER;
2698 if ( ber_flatten2( ber, &ctrl->ldctl_value, 1 ) == -1 ) {
2699 rs->sr_err = LDAP_OTHER;
2703 rs->sr_err = LDAP_SUCCESS;
2706 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2707 ber_free_buf( ber );
2709 if ( rs->sr_err != LDAP_SUCCESS ) {
2713 } else if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
2714 struct berval authzID = ctrl->ldctl_value,
2716 BerElementBuffer berbuf;
2717 BerElement *ber = (BerElement *)&berbuf;
2720 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
2721 rs->sr_err = LDAP_PROTOCOL_ERROR;
2726 tmp.bv_val += STRLENOF( "dn:" );
2727 tmp.bv_len -= STRLENOF( "dn:" );
2729 ber_init2( ber, 0, LBER_USE_DER );
2730 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2732 /* apparently, Mozilla API encodes this
2733 * as "SEQUENCE { LDAPDN }" */
2734 tag = ber_printf( ber, "{O}", &tmp );
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 ctrl->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
2746 rs->sr_err = LDAP_SUCCESS;
2749 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2750 ber_free_buf( ber );
2752 if ( rs->sr_err != LDAP_SUCCESS ) {
2765 * if any needs to be added, it is prepended to existing ones,
2766 * in a newly allocated array. The companion function
2767 * ldap_back_controls_free() must be used to restore the original
2768 * status of op->o_ctrls.
2771 ldap_back_controls_add(
2775 LDAPControl ***pctrls )
2777 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2779 LDAPControl **ctrls = NULL;
2780 /* set to the maximum number of controls this backend can add */
2781 LDAPControl c[ 2 ] = { { 0 } };
2782 int n = 0, i, j1 = 0, j2 = 0;
2786 rs->sr_err = LDAP_SUCCESS;
2788 /* don't add controls if protocol is not LDAPv3 */
2789 switch ( li->li_version ) {
2794 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2803 /* put controls that go __before__ existing ones here */
2805 /* proxyAuthz for identity assertion */
2806 switch ( ldap_back_proxy_authz_ctrl( op, rs, &lc->lc_bound_ndn,
2807 li->li_version, &li->li_idassert, &c[ j1 ] ) )
2809 case SLAP_CB_CONTINUE:
2820 /* put controls that go __after__ existing ones here */
2822 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
2823 /* FIXME: according to <draft-wahl-ldap-session>,
2824 * the server should check if the control can be added
2825 * based on the identity of the client and so */
2827 /* session tracking */
2828 if ( LDAP_BACK_ST_REQUEST( li ) ) {
2829 switch ( slap_ctrl_session_tracking_request_add( op, rs, &c[ j1 + j2 ] ) ) {
2830 case SLAP_CB_CONTINUE:
2841 #endif /* SLAP_CONTROL_X_SESSION_TRACKING */
2843 if ( rs->sr_err == SLAP_CB_CONTINUE ) {
2844 rs->sr_err = LDAP_SUCCESS;
2847 /* if nothing to do, just bail out */
2848 if ( j1 == 0 && j2 == 0 ) {
2852 assert( j1 + j2 <= (int) (sizeof( c )/sizeof( c[0] )) );
2854 if ( op->o_ctrls ) {
2855 for ( n = 0; op->o_ctrls[ n ]; n++ )
2856 /* just count ctrls */ ;
2859 ctrls = op->o_tmpalloc( (n + j1 + j2 + 1) * sizeof( LDAPControl * ) + ( j1 + j2 ) * sizeof( LDAPControl ),
2862 ctrls[ 0 ] = (LDAPControl *)&ctrls[ n + j1 + j2 + 1 ];
2863 *ctrls[ 0 ] = c[ 0 ];
2864 for ( i = 1; i < j1; i++ ) {
2865 ctrls[ i ] = &ctrls[ 0 ][ i ];
2866 *ctrls[ i ] = c[ i ];
2871 if ( op->o_ctrls ) {
2872 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
2873 ctrls[ i + j1 ] = op->o_ctrls[ i ];
2879 ctrls[ n ] = (LDAPControl *)&ctrls[ n + j2 + 1 ] + j1;
2880 *ctrls[ n ] = c[ j1 ];
2881 for ( i = 1; i < j2; i++ ) {
2882 ctrls[ n + i ] = &ctrls[ n ][ i ];
2883 *ctrls[ n + i ] = c[ i ];
2887 ctrls[ n + j2 ] = NULL;
2890 if ( ctrls == NULL ) {
2891 ctrls = op->o_ctrls;
2900 ldap_back_controls_free( Operation *op, SlapReply *rs, LDAPControl ***pctrls )
2902 LDAPControl **ctrls = *pctrls;
2904 /* we assume that the controls added by the proxy come first,
2905 * so as soon as we find op->o_ctrls[ 0 ] we can stop */
2906 if ( ctrls && ctrls != op->o_ctrls ) {
2907 int i = 0, n = 0, n_added;
2908 LDAPControl *lower, *upper;
2910 assert( ctrls[ 0 ] != NULL );
2912 for ( n = 0; ctrls[ n ] != NULL; n++ )
2915 if ( op->o_ctrls ) {
2916 for ( i = 0; op->o_ctrls[ i ] != NULL; i++ )
2921 lower = (LDAPControl *)&ctrls[ n ];
2922 upper = &lower[ n_added ];
2924 for ( i = 0; ctrls[ i ] != NULL; i++ ) {
2925 if ( ctrls[ i ] < lower || ctrls[ i ] >= upper ) {
2926 /* original; don't touch */
2930 if ( !BER_BVISNULL( &ctrls[ i ]->ldctl_value ) ) {
2931 op->o_tmpfree( ctrls[ i ]->ldctl_value.bv_val, op->o_tmpmemctx );
2935 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2944 ldap_back_conn2str( const ldapconn_base_t *lc, char *buf, ber_len_t buflen )
2946 char tbuf[ SLAP_TEXT_BUFLEN ];
2947 char *ptr = buf, *end = buf + buflen;
2950 if ( ptr + sizeof("conn=") > end ) return -1;
2951 ptr = lutil_strcopy( ptr, "conn=" );
2953 len = ldap_back_connid2str( lc, ptr, (ber_len_t)(end - ptr) );
2955 if ( ptr >= end ) return -1;
2957 if ( !BER_BVISNULL( &lc->lcb_local_ndn ) ) {
2958 if ( ptr + sizeof(" DN=\"\"") + lc->lcb_local_ndn.bv_len > end ) return -1;
2959 ptr = lutil_strcopy( ptr, " DN=\"" );
2960 ptr = lutil_strncopy( ptr, lc->lcb_local_ndn.bv_val, lc->lcb_local_ndn.bv_len );
2964 if ( lc->lcb_create_time != 0 ) {
2965 len = snprintf( tbuf, sizeof(tbuf), "%ld", lc->lcb_create_time );
2966 if ( ptr + sizeof(" created=") + len >= end ) return -1;
2967 ptr = lutil_strcopy( ptr, " created=" );
2968 ptr = lutil_strcopy( ptr, tbuf );
2971 if ( lc->lcb_time != 0 ) {
2972 len = snprintf( tbuf, sizeof(tbuf), "%ld", lc->lcb_time );
2973 if ( ptr + sizeof(" modified=") + len >= end ) return -1;
2974 ptr = lutil_strcopy( ptr, " modified=" );
2975 ptr = lutil_strcopy( ptr, tbuf );
2978 len = snprintf( tbuf, sizeof(tbuf), "%u", lc->lcb_refcnt );
2979 if ( ptr + sizeof(" refcnt=") + len >= end ) return -1;
2980 ptr = lutil_strcopy( ptr, " refcnt=" );
2981 ptr = lutil_strcopy( ptr, tbuf );
2987 ldap_back_connid2str( const ldapconn_base_t *lc, char *buf, ber_len_t buflen )
2989 static struct berval conns[] = {
2991 BER_BVC("ROOTDN-TLS"),
2993 BER_BVC("ANON-TLS"),
2995 BER_BVC("BIND-TLS"),
3001 if ( LDAP_BACK_PCONN_ISPRIV( (const ldapconn_t *)lc ) ) {
3005 cid = (long)lc->lcb_conn;
3006 assert( cid >= LDAP_BACK_PCONN_FIRST && cid < LDAP_BACK_PCONN_LAST );
3010 if ( bv->bv_len >= buflen ) {
3011 return bv->bv_len + 1;
3015 lutil_strncopy( buf, bv->bv_val, bv->bv_len + 1 );
3018 len = snprintf( buf, buflen, "%lu", lc->lcb_conn->c_connid );