1 /* bind.c - ldap backend bind function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2009 The OpenLDAP Foundation.
6 * Portions Copyright 2000-2003 Pierangelo Masarati.
7 * Portions Copyright 1999-2003 Howard Chu.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
19 * This work was initially developed by Howard Chu for inclusion
20 * in OpenLDAP Software and subsequently enhanced by Pierangelo
29 #include <ac/socket.h>
30 #include <ac/string.h>
34 #include "back-ldap.h"
36 #undef ldap_debug /* silence a warning in ldap-int.h */
37 #include "../../../libraries/libldap/ldap-int.h"
39 #include "lutil_ldap.h"
41 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ "2.16.840.1.113730.3.4.12"
43 #if LDAP_BACK_PRINT_CONNTREE > 0
49 { LDAP_BACK_FCONN_ISBOUND, 'B' },
50 { LDAP_BACK_FCONN_ISANON, 'A' },
51 { LDAP_BACK_FCONN_ISPRIV, 'P' },
52 { LDAP_BACK_FCONN_ISTLS, 'T' },
53 { LDAP_BACK_FCONN_BINDING, 'X' },
54 { LDAP_BACK_FCONN_TAINTED, 'E' },
55 { LDAP_BACK_FCONN_ABANDON, 'N' },
56 { LDAP_BACK_FCONN_ISIDASR, 'S' },
57 { LDAP_BACK_FCONN_CACHED, 'C' },
62 ldap_back_conn_print( ldapconn_t *lc, const char *avlstr )
64 char buf[ SLAP_TEXT_BUFLEN ];
65 char fbuf[ sizeof("BAPTIENSC") ];
68 ldap_back_conn2str( &lc->lc_base, buf, sizeof( buf ) );
69 for ( i = 0; flagsmap[ i ].c != '\0'; i++ ) {
70 if ( lc->lc_lcflags & flagsmap[i].f ) {
71 fbuf[i] = flagsmap[i].c;
79 fprintf( stderr, "lc=%p %s %s flags=0x%08x (%s)\n",
80 (void *)lc, buf, avlstr, lc->lc_lcflags, fbuf );
84 ldap_back_ravl_print( Avlnode *root, int depth )
93 ldap_back_ravl_print( root->avl_right, depth+1 );
95 for ( i = 0; i < depth; i++ ) {
96 fprintf( stderr, "-" );
100 ldap_back_conn_print( lc, avl_bf2str( root->avl_bf ) );
102 ldap_back_ravl_print( root->avl_left, depth + 1 );
105 static char* priv2str[] = {
116 ldap_back_print_conntree( ldapinfo_t *li, char *msg )
120 fprintf( stderr, "========> %s\n", msg );
122 for ( c = LDAP_BACK_PCONN_FIRST; c < LDAP_BACK_PCONN_LAST; c++ ) {
126 fprintf( stderr, " %s[%d]\n", priv2str[ c ], li->li_conn_priv[ c ].lic_num );
128 LDAP_TAILQ_FOREACH( lc, &li->li_conn_priv[ c ].lic_priv, lc_q )
130 fprintf( stderr, " [%d] ", i );
131 ldap_back_conn_print( lc, "" );
136 if ( li->li_conninfo.lai_tree == 0 ) {
137 fprintf( stderr, "\t(empty)\n" );
140 ldap_back_ravl_print( li->li_conninfo.lai_tree, 0 );
143 fprintf( stderr, "<======== %s\n", msg );
145 #endif /* LDAP_BACK_PRINT_CONNTREE */
148 ldap_back_freeconn( ldapinfo_t *li, ldapconn_t *lc, int dolock );
151 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
152 struct berval *binddn, struct berval *bindcred );
155 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
156 struct berval *binddn, struct berval *bindcred );
159 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs,
160 ldap_back_send_t sendok, struct berval *binddn, struct berval *bindcred );
163 ldap_back_prepare_conn( ldapconn_t *lc, Operation *op, SlapReply *rs,
164 ldap_back_send_t sendok );
167 ldap_back_conndnlc_cmp( const void *c1, const void *c2 );
170 ldap_back_conn_delete( ldapinfo_t *li, ldapconn_t *lc )
172 if ( LDAP_BACK_PCONN_ISPRIV( lc ) ) {
173 if ( LDAP_BACK_CONN_CACHED( lc ) ) {
174 assert( lc->lc_q.tqe_prev != NULL );
175 assert( li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num > 0 );
176 li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num--;
177 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv, lc, lc_q );
178 LDAP_TAILQ_ENTRY_INIT( lc, lc_q );
179 LDAP_BACK_CONN_CACHED_CLEAR( lc );
182 assert( LDAP_BACK_CONN_TAINTED( lc ) );
183 assert( lc->lc_q.tqe_prev == NULL );
187 ldapconn_t *tmplc = NULL;
189 if ( LDAP_BACK_CONN_CACHED( lc ) ) {
190 assert( !LDAP_BACK_CONN_TAINTED( lc ) );
191 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
192 ldap_back_conndnlc_cmp );
193 assert( tmplc == lc );
194 LDAP_BACK_CONN_CACHED_CLEAR( lc );
197 assert( LDAP_BACK_CONN_TAINTED( lc ) || tmplc == lc );
204 ldap_back_bind( Operation *op, SlapReply *rs )
206 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
209 LDAPControl **ctrls = NULL;
210 struct berval save_o_dn;
211 int save_o_do_not_cache,
214 ldap_back_send_t retrying = LDAP_BACK_RETRYING;
216 /* allow rootdn as a means to auth without the need to actually
217 * contact the proxied DSA */
218 switch ( be_rootdn_bind( op, rs ) ) {
219 case SLAP_CB_CONTINUE:
226 lc = ldap_back_getconn( op, rs, LDAP_BACK_BIND_SERR, NULL, NULL );
231 /* we can do (almost) whatever we want with this conn,
232 * because either it's temporary, or it's marked as binding */
233 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
234 ch_free( lc->lc_bound_ndn.bv_val );
235 BER_BVZERO( &lc->lc_bound_ndn );
237 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
238 memset( lc->lc_cred.bv_val, 0, lc->lc_cred.bv_len );
239 ch_free( lc->lc_cred.bv_val );
240 BER_BVZERO( &lc->lc_cred );
242 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
244 /* don't add proxyAuthz; set the bindDN */
245 save_o_dn = op->o_dn;
246 save_o_do_not_cache = op->o_do_not_cache;
247 op->o_dn = op->o_req_dn;
248 op->o_do_not_cache = 1;
251 rc = ldap_back_controls_add( op, rs, lc, &ctrls );
252 op->o_dn = save_o_dn;
253 op->o_do_not_cache = save_o_do_not_cache;
254 if ( rc != LDAP_SUCCESS ) {
255 send_ldap_result( op, rs );
256 ldap_back_release_conn( li, lc );
261 /* method is always LDAP_AUTH_SIMPLE if we got here */
262 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
264 &op->orb_cred, ctrls, NULL, &msgid );
265 /* FIXME: should we always retry, or only when piping the bind
266 * in the "override" connection pool? */
267 rc = ldap_back_op_result( lc, op, rs, msgid,
268 li->li_timeout[ SLAP_OP_BIND ],
269 LDAP_BACK_BIND_SERR | retrying );
270 if ( rc == LDAP_UNAVAILABLE && retrying ) {
271 retrying &= ~LDAP_BACK_RETRYING;
272 if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_BIND_SERR ) ) {
277 ldap_back_controls_free( op, rs, &ctrls );
279 if ( rc == LDAP_SUCCESS ) {
280 op->o_conn->c_authz_cookie = op->o_bd->be_private;
282 /* If defined, proxyAuthz will be used also when
283 * back-ldap is the authorizing backend; for this
284 * purpose, after a successful bind the connection
285 * is left for further binds, and further operations
286 * on this client connection will use a default
287 * connection with identity assertion */
288 /* NOTE: use with care */
289 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
290 ldap_back_release_conn( li, lc );
294 /* rebind is now done inside ldap_back_proxy_authz_bind()
295 * in case of success */
296 LDAP_BACK_CONN_ISBOUND_SET( lc );
297 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
299 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
300 memset( lc->lc_cred.bv_val, 0,
301 lc->lc_cred.bv_len );
304 if ( LDAP_BACK_SAVECRED( li ) ) {
305 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
306 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
309 lc->lc_cred.bv_len = 0;
313 /* must re-insert if local DN changed as result of bind */
314 if ( !LDAP_BACK_CONN_ISBOUND( lc )
315 || ( !dn_match( &op->o_req_ndn, &lc->lc_local_ndn )
316 && !LDAP_BACK_PCONN_ISPRIV( lc ) ) )
321 /* wait for all other ops to release the connection */
323 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
324 if ( lc->lc_refcnt > 1 ) {
325 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
326 ldap_pvt_thread_yield();
330 #if LDAP_BACK_PRINT_CONNTREE > 0
331 ldap_back_print_conntree( li, ">>> ldap_back_bind" );
332 #endif /* LDAP_BACK_PRINT_CONNTREE */
334 assert( lc->lc_refcnt == 1 );
335 ldap_back_conn_delete( li, lc );
337 /* delete all cached connections with the current connection */
338 if ( LDAP_BACK_SINGLECONN( li ) ) {
339 while ( ( tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc, ldap_back_conn_cmp ) ) != NULL )
341 assert( !LDAP_BACK_PCONN_ISPRIV( lc ) );
342 Debug( LDAP_DEBUG_TRACE,
343 "=>ldap_back_bind: destroying conn %lu (refcnt=%u)\n",
344 lc->lc_conn->c_connid, lc->lc_refcnt, 0 );
346 if ( tmplc->lc_refcnt != 0 ) {
348 LDAP_BACK_CONN_TAINTED_SET( tmplc );
349 LDAP_BACK_CONN_CACHED_CLEAR( tmplc );
353 * Needs a test because the handler may be corrupted,
354 * and calling ldap_unbind on a corrupted header results
355 * in a segmentation fault
357 ldap_back_conn_free( tmplc );
362 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
363 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
364 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
365 LDAP_BACK_PCONN_ROOTDN_SET( lc, op );
367 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
368 ldap_back_conndn_cmp, ldap_back_conndn_dup );
371 #if LDAP_BACK_PRINT_CONNTREE > 0
372 ldap_back_print_conntree( li, "<<< ldap_back_bind" );
373 #endif /* LDAP_BACK_PRINT_CONNTREE */
375 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
378 LDAP_BACK_CONN_CACHED_SET( lc );
382 /* duplicate; someone else successfully bound
383 * on the same connection with the same identity;
384 * we can do this because lc_refcnt == 1 */
385 ldap_back_conn_free( lc );
391 ldap_back_release_conn( li, lc );
398 * ldap_back_conndn_cmp
400 * compares two ldapconn_t based on the value of the conn pointer
401 * and of the local DN; used by avl stuff for insert, lookup
405 ldap_back_conndn_cmp( const void *c1, const void *c2 )
407 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
408 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
411 /* If local DNs don't match, it is definitely not a match */
412 /* For shared sessions, conn is NULL. Only explicitly
413 * bound sessions will have non-NULL conn.
415 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
417 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
424 * ldap_back_conndnlc_cmp
426 * compares two ldapconn_t based on the value of the conn pointer,
427 * the local DN and the lc pointer; used by avl stuff for insert, lookup
431 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
433 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
434 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
437 /* If local DNs don't match, it is definitely not a match */
438 /* For shared sessions, conn is NULL. Only explicitly
439 * bound sessions will have non-NULL conn.
441 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
443 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
445 rc = SLAP_PTRCMP( lc1, lc2 );
455 * compares two ldapconn_t based on the value of the conn pointer;
456 * used by avl stuff for delete of all conns with the same connid
459 ldap_back_conn_cmp( const void *c1, const void *c2 )
461 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
462 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
464 /* For shared sessions, conn is NULL. Only explicitly
465 * bound sessions will have non-NULL conn.
467 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
471 * ldap_back_conndn_dup
473 * returns -1 in case a duplicate ldapconn_t has been inserted;
477 ldap_back_conndn_dup( void *c1, void *c2 )
479 ldapconn_t *lc1 = (ldapconn_t *)c1;
480 ldapconn_t *lc2 = (ldapconn_t *)c2;
482 /* Cannot have more than one shared session with same DN */
483 if ( lc1->lc_conn == lc2->lc_conn &&
484 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
493 ldap_back_freeconn( ldapinfo_t *li, ldapconn_t *lc, int dolock )
496 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
499 #if LDAP_BACK_PRINT_CONNTREE > 0
500 ldap_back_print_conntree( li, ">>> ldap_back_freeconn" );
501 #endif /* LDAP_BACK_PRINT_CONNTREE */
503 (void)ldap_back_conn_delete( li, lc );
505 if ( lc->lc_refcnt == 0 ) {
506 ldap_back_conn_free( (void *)lc );
509 #if LDAP_BACK_PRINT_CONNTREE > 0
510 ldap_back_print_conntree( li, "<<< ldap_back_freeconn" );
511 #endif /* LDAP_BACK_PRINT_CONNTREE */
514 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
531 int rc = LDAP_SUCCESS;
533 /* start TLS ("tls-[try-]{start,propagate}" statements) */
534 if ( ( LDAP_BACK_USE_TLS_F( flags ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS_F( flags ) ) )
535 && !ldap_is_ldaps_url( url ) )
537 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
539 * use asynchronous StartTLS
540 * in case, chase referral (not implemented yet)
544 if ( protocol == 0 ) {
545 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
549 if ( protocol < LDAP_VERSION3 ) {
550 /* we should rather bail out... */
551 rc = LDAP_UNWILLING_TO_PERFORM;
552 *text = "invalid protocol version";
555 if ( rc == LDAP_SUCCESS ) {
556 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
559 if ( rc == LDAP_SUCCESS ) {
560 LDAPMessage *res = NULL;
563 LDAP_BACK_TV_SET( &tv );
566 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
568 rc = LDAP_UNAVAILABLE;
570 } else if ( rc == 0 ) {
571 if ( retries != LDAP_BACK_RETRY_NEVER ) {
572 ldap_pvt_thread_yield();
576 LDAP_BACK_TV_SET( &tv );
579 rc = LDAP_UNAVAILABLE;
581 } else if ( rc == LDAP_RES_EXTENDED ) {
582 struct berval *data = NULL;
584 rc = ldap_parse_extended_result( ld, res,
586 if ( rc == LDAP_SUCCESS ) {
588 rc = ldap_parse_result( ld, res, &err,
589 NULL, NULL, NULL, NULL, 1 );
590 if ( rc == LDAP_SUCCESS ) {
595 /* FIXME: in case a referral
596 * is returned, should we try
597 * using it instead of the
599 if ( rc == LDAP_SUCCESS ) {
600 rc = ldap_install_tls( ld );
602 } else if ( rc == LDAP_REFERRAL ) {
603 rc = LDAP_UNWILLING_TO_PERFORM;
604 *text = "unwilling to chase referral returned by Start TLS exop";
608 if ( data->bv_val ) {
609 ber_memfree( data->bv_val );
623 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
625 * use synchronous StartTLS
627 rc = ldap_start_tls_s( ld, NULL, NULL );
628 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
630 /* if StartTLS is requested, only attempt it if the URL
631 * is not "ldaps://"; this may occur not only in case
632 * of misconfiguration, but also when used in the chain
633 * overlay, where the "uri" can be parsed out of a referral */
639 case LDAP_SERVER_DOWN:
643 if ( LDAP_BACK_TLS_CRITICAL_F( flags ) ) {
644 *text = "could not start TLS";
648 /* in case Start TLS is not critical */
660 #endif /* HAVE_TLS */
663 ldap_back_prepare_conn( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
665 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
669 int is_tls = op->o_conn->c_is_tls;
670 time_t lctime = (time_t)(-1);
672 #endif /* HAVE_TLS */
674 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
675 rs->sr_err = ldap_initialize( &ld, li->li_uri );
676 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
677 if ( rs->sr_err != LDAP_SUCCESS ) {
681 if ( li->li_urllist_f ) {
682 ldap_set_urllist_proc( ld, li->li_urllist_f, li->li_urllist_p );
685 /* Set LDAP version. This will always succeed: If the client
686 * bound with a particular version, then so can we.
688 if ( li->li_version != 0 ) {
689 version = li->li_version;
691 } else if ( op->o_protocol != 0 ) {
692 version = op->o_protocol;
695 /* assume it's an internal op; set to LDAPv3 */
696 version = LDAP_VERSION3;
698 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
700 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
701 ldap_set_option( ld, LDAP_OPT_REFERRALS,
702 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
704 if ( li->li_network_timeout > 0 ) {
707 tv.tv_sec = li->li_network_timeout;
709 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
713 if ( LDAP_BACK_CONN_ISPRIV( lc ) ) {
716 } else if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
717 sb = &li->li_idassert.si_bc;
723 if ( sb->sb_tls_do_init ) {
724 bindconf_tls_set( sb, ld );
725 } else if ( sb->sb_tls_ctx ) {
726 ldap_set_option( ld, LDAP_OPT_X_TLS_CTX, sb->sb_tls_ctx );
729 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
730 assert( li->li_uri_mutex_do_not_lock == 0 );
731 li->li_uri_mutex_do_not_lock = 1;
732 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
733 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
734 li->li_uri_mutex_do_not_lock = 0;
735 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
736 if ( rs->sr_err != LDAP_SUCCESS ) {
737 ldap_unbind_ext( ld, NULL, NULL );
738 rs->sr_text = "Start TLS failed";
741 } else if ( li->li_idle_timeout ) {
742 /* only touch when activity actually took place... */
745 #endif /* HAVE_TLS */
751 LDAP_BACK_CONN_ISTLS_SET( lc );
753 LDAP_BACK_CONN_ISTLS_CLEAR( lc );
755 if ( lctime != (time_t)(-1) ) {
756 lc->lc_time = lctime;
758 #endif /* HAVE_TLS */
761 if ( rs->sr_err != LDAP_SUCCESS ) {
762 rs->sr_err = slap_map_api2result( rs );
763 if ( sendok & LDAP_BACK_SENDERR ) {
764 if ( rs->sr_text == NULL ) {
765 rs->sr_text = "Proxy connection initialization failed";
767 send_ldap_result( op, rs );
771 if ( li->li_conn_ttl > 0 ) {
772 lc->lc_create_time = op->o_time;
783 ldap_back_send_t sendok,
784 struct berval *binddn,
785 struct berval *bindcred )
787 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
788 ldapconn_t *lc = NULL,
791 lookupconn = !( sendok & LDAP_BACK_BINDING );
793 /* if the server is quarantined, and
794 * - the current interval did not expire yet, or
795 * - no more retries should occur,
796 * don't return the connection */
797 if ( li->li_isquarantined ) {
798 slap_retry_info_t *ri = &li->li_quarantine;
801 if ( li->li_quarantine.ri_interval ) {
802 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
803 if ( li->li_isquarantined == LDAP_BACK_FQ_YES ) {
804 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
805 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
807 Debug( LDAP_DEBUG_ANY,
808 "%s: ldap_back_getconn quarantine "
809 "retry block #%d try #%d.\n",
810 op->o_log_prefix, ri->ri_idx, ri->ri_count );
811 li->li_isquarantined = LDAP_BACK_FQ_RETRYING;
814 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
818 rs->sr_err = LDAP_UNAVAILABLE;
819 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
820 rs->sr_text = "Target is quarantined";
821 send_ldap_result( op, rs );
827 /* Internal searches are privileged and shared. So is root. */
828 if ( op->o_do_not_cache || be_isroot( op ) ) {
829 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
830 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
831 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
834 struct berval tmpbinddn,
841 if ( binddn == NULL ) {
844 if ( bindcred == NULL ) {
845 bindcred = &tmpbindcred;
847 if ( op->o_tag == LDAP_REQ_BIND ) {
848 save_o_dn = op->o_dn;
849 save_o_ndn = op->o_ndn;
850 op->o_dn = op->o_req_dn;
851 op->o_ndn = op->o_req_ndn;
853 isproxyauthz = ldap_back_is_proxy_authz( op, rs, sendok, binddn, bindcred );
854 if ( op->o_tag == LDAP_REQ_BIND ) {
855 op->o_dn = save_o_dn;
856 op->o_ndn = save_o_ndn;
858 if ( isproxyauthz == -1 ) {
862 lc_curr.lc_local_ndn = op->o_ndn;
863 /* Explicit binds must not be shared;
864 * however, explicit binds are piped in a special connection
865 * when idassert is to occur with "override" set */
866 if ( op->o_tag == LDAP_REQ_BIND && !isproxyauthz ) {
867 lc_curr.lc_conn = op->o_conn;
870 if ( isproxyauthz && !( sendok & LDAP_BACK_BINDING ) ) {
871 lc_curr.lc_local_ndn = *binddn;
872 LDAP_BACK_PCONN_ROOTDN_SET( &lc_curr, op );
873 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
875 } else if ( isproxyauthz && ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) {
876 lc_curr.lc_local_ndn = slap_empty_bv;
877 LDAP_BACK_PCONN_BIND_SET( &lc_curr, op );
878 LDAP_BACK_CONN_ISIDASSERT_SET( &lc_curr );
881 } else if ( SLAP_IS_AUTHZ_BACKEND( op ) ) {
882 lc_curr.lc_conn = op->o_conn;
885 LDAP_BACK_PCONN_ANON_SET( &lc_curr, op );
890 /* Explicit Bind requests always get their own conn */
893 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
894 if ( LDAP_BACK_PCONN_ISPRIV( &lc_curr ) ) {
895 /* lookup a conn that's not binding */
896 LDAP_TAILQ_FOREACH( lc,
897 &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv,
900 if ( !LDAP_BACK_CONN_BINDING( lc ) && lc->lc_refcnt == 0 ) {
906 if ( lc != LDAP_TAILQ_LAST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
909 LDAP_TAILQ_REMOVE( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
911 LDAP_TAILQ_ENTRY_INIT( lc, lc_q );
912 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv,
916 } else if ( !LDAP_BACK_USE_TEMPORARIES( li )
917 && li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_num == li->li_conn_priv_max )
919 lc = LDAP_TAILQ_FIRST( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( &lc_curr ) ].lic_priv );
924 /* Searches for a ldapconn in the avl tree */
925 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
926 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
930 /* Don't reuse connections while they're still binding */
931 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
932 if ( !LDAP_BACK_USE_TEMPORARIES( li ) ) {
933 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
935 ldap_pvt_thread_yield();
942 if ( op->o_tag == LDAP_REQ_BIND ) {
943 /* right now, this is the only possible case */
944 assert( ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) );
945 LDAP_BACK_CONN_BINDING_SET( lc );
948 refcnt = ++lc->lc_refcnt;
951 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
954 /* Looks like we didn't get a bind. Open a new session... */
956 lc = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
957 lc->lc_flags = li->li_flags;
958 lc->lc_lcflags = lc_curr.lc_lcflags;
959 if ( ldap_back_prepare_conn( lc, op, rs, sendok ) != LDAP_SUCCESS ) {
964 if ( sendok & LDAP_BACK_BINDING ) {
965 LDAP_BACK_CONN_BINDING_SET( lc );
968 lc->lc_conn = lc_curr.lc_conn;
969 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
972 * the rationale is: connections as the rootdn are privileged,
973 * so acl_authcDN is to be used; however, in some cases
974 * one already configured identity assertion with a highly
975 * privileged idassert_authcDN, so if acl_authcDN is NULL
976 * and idassert_authcDN is not, use the second instead.
978 * might change in the future, because it's preferable
979 * to make clear what identity is being used, since
980 * the only drawback is that one risks to configure
981 * the same identity twice...
983 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
984 if ( BER_BVISNULL( &li->li_acl_authcDN ) && !BER_BVISNULL( &li->li_idassert_authcDN ) ) {
985 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
986 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
989 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
990 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
992 LDAP_BACK_CONN_ISPRIV_SET( lc );
994 } else if ( LDAP_BACK_CONN_ISIDASSERT( &lc_curr ) ) {
995 if ( !LDAP_BACK_PCONN_ISBIND( &lc_curr ) ) {
996 ber_dupbv( &lc->lc_bound_ndn, &li->li_idassert_authcDN );
997 ber_dupbv( &lc->lc_cred, &li->li_idassert_passwd );
999 LDAP_BACK_CONN_ISIDASSERT_SET( lc );
1002 BER_BVZERO( &lc->lc_cred );
1003 BER_BVZERO( &lc->lc_bound_ndn );
1004 if ( !BER_BVISEMPTY( &op->o_ndn )
1005 && SLAP_IS_AUTHZ_BACKEND( op ) )
1007 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
1012 /* if start TLS failed but it was not mandatory,
1013 * check if the non-TLS connection was already
1014 * in cache; in case, destroy the newly created
1015 * connection and use the existing one */
1016 if ( LDAP_BACK_PCONN_ISTLS( lc )
1017 && !ldap_tls_inplace( lc->lc_ld ) )
1019 ldapconn_t *tmplc = NULL;
1020 int idx = LDAP_BACK_CONN2PRIV( &lc_curr ) - 1;
1022 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1023 LDAP_TAILQ_FOREACH( tmplc,
1024 &li->li_conn_priv[ idx ].lic_priv,
1027 if ( !LDAP_BACK_CONN_BINDING( tmplc ) ) {
1032 if ( tmplc != NULL ) {
1033 refcnt = ++tmplc->lc_refcnt;
1034 ldap_back_conn_free( lc );
1037 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1039 if ( tmplc != NULL ) {
1043 #endif /* HAVE_TLS */
1045 /* Inserts the newly created ldapconn in the avl tree */
1046 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1048 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1050 assert( lc->lc_refcnt == 1 );
1052 #if LDAP_BACK_PRINT_CONNTREE > 0
1053 ldap_back_print_conntree( li, ">>> ldap_back_getconn(insert)" );
1054 #endif /* LDAP_BACK_PRINT_CONNTREE */
1056 if ( LDAP_BACK_PCONN_ISPRIV( lc ) ) {
1057 if ( li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num < li->li_conn_priv_max ) {
1058 LDAP_TAILQ_INSERT_TAIL( &li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_priv, lc, lc_q );
1059 li->li_conn_priv[ LDAP_BACK_CONN2PRIV( lc ) ].lic_num++;
1060 LDAP_BACK_CONN_CACHED_SET( lc );
1063 LDAP_BACK_CONN_TAINTED_SET( lc );
1068 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
1069 ldap_back_conndn_cmp, ldap_back_conndn_dup );
1070 LDAP_BACK_CONN_CACHED_SET( lc );
1073 #if LDAP_BACK_PRINT_CONNTREE > 0
1074 ldap_back_print_conntree( li, "<<< ldap_back_getconn(insert)" );
1075 #endif /* LDAP_BACK_PRINT_CONNTREE */
1077 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1079 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1080 char buf[ SLAP_TEXT_BUFLEN ];
1082 snprintf( buf, sizeof( buf ),
1083 "lc=%p inserted refcnt=%u rc=%d",
1084 (void *)lc, refcnt, rs->sr_err );
1086 Debug( LDAP_DEBUG_TRACE,
1087 "=>ldap_back_getconn: %s: %s\n",
1088 op->o_log_prefix, buf, 0 );
1091 if ( !LDAP_BACK_PCONN_ISPRIV( lc ) ) {
1092 /* Err could be -1 in case a duplicate ldapconn is inserted */
1093 switch ( rs->sr_err ) {
1098 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1099 if ( !( sendok & LDAP_BACK_BINDING ) && !LDAP_BACK_USE_TEMPORARIES( li ) ) {
1100 /* duplicate: free and try to get the newly created one */
1101 ldap_back_conn_free( lc );
1106 /* taint connection, so that it'll be freed when released */
1107 LDAP_BACK_CONN_TAINTED_SET( lc );
1111 LDAP_BACK_CONN_CACHED_CLEAR( lc );
1112 ldap_back_conn_free( lc );
1113 rs->sr_err = LDAP_OTHER;
1114 rs->sr_text = "Proxy bind collision";
1115 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1116 send_ldap_result( op, rs );
1125 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
1126 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
1130 /* let it be used, but taint/delete it so that
1131 * no-one else can look it up any further */
1132 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1134 #if LDAP_BACK_PRINT_CONNTREE > 0
1135 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1136 #endif /* LDAP_BACK_PRINT_CONNTREE */
1138 (void)ldap_back_conn_delete( li, lc );
1139 LDAP_BACK_CONN_TAINTED_SET( lc );
1141 #if LDAP_BACK_PRINT_CONNTREE > 0
1142 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1143 #endif /* LDAP_BACK_PRINT_CONNTREE */
1145 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1148 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
1149 char buf[ SLAP_TEXT_BUFLEN ];
1151 snprintf( buf, sizeof( buf ),
1152 "conn %p fetched refcnt=%u%s",
1154 expiring ? " expiring" : "" );
1155 Debug( LDAP_DEBUG_TRACE,
1156 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
1162 #endif /* HAVE_TLS */
1168 ldap_back_release_conn_lock(
1174 ldapconn_t *lc = *lcp;
1177 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1179 assert( lc->lc_refcnt > 0 );
1180 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1182 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
1183 ldap_back_freeconn( li, lc, 0 );
1187 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1192 ldap_back_quarantine(
1196 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1198 slap_retry_info_t *ri = &li->li_quarantine;
1200 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
1202 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1203 time_t new_last = slap_get_time();
1205 switch ( li->li_isquarantined ) {
1206 case LDAP_BACK_FQ_NO:
1207 if ( ri->ri_last == new_last ) {
1211 Debug( LDAP_DEBUG_ANY,
1212 "%s: ldap_back_quarantine enter.\n",
1213 op->o_log_prefix, 0, 0 );
1219 case LDAP_BACK_FQ_RETRYING:
1220 Debug( LDAP_DEBUG_ANY,
1221 "%s: ldap_back_quarantine block #%d try #%d failed.\n",
1222 op->o_log_prefix, ri->ri_idx, ri->ri_count );
1225 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
1226 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
1237 li->li_isquarantined = LDAP_BACK_FQ_YES;
1238 ri->ri_last = new_last;
1240 } else if ( li->li_isquarantined != LDAP_BACK_FQ_NO ) {
1241 if ( ri->ri_last == slap_get_time() ) {
1245 Debug( LDAP_DEBUG_ANY,
1246 "%s: ldap_back_quarantine exit (%d) err=%d.\n",
1247 op->o_log_prefix, li->li_isquarantined, rs->sr_err );
1249 if ( li->li_quarantine_f ) {
1250 (void)li->li_quarantine_f( li, li->li_quarantine_p );
1255 li->li_isquarantined = LDAP_BACK_FQ_NO;
1259 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
1263 ldap_back_dobind_cb(
1268 ber_tag_t *tptr = op->o_callback->sc_private;
1270 rs->sr_tag = slap_req2res( op->o_tag );
1272 return SLAP_CB_CONTINUE;
1276 * ldap_back_dobind_int
1278 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1281 ldap_back_dobind_int(
1285 ldap_back_send_t sendok,
1289 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1292 struct berval binddn = slap_empty_bv,
1293 bindcred = slap_empty_bv;
1299 ber_tag_t o_tag = op->o_tag;
1300 slap_callback cb = {0};
1302 assert( lcp != NULL );
1303 assert( retries >= 0 );
1305 if ( sendok & LDAP_BACK_GETCONN ) {
1306 assert( *lcp == NULL );
1308 lc = ldap_back_getconn( op, rs, sendok, &binddn, &bindcred );
1318 assert( lc != NULL );
1322 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1325 if ( binding == 0 ) {
1326 /* check if already bound */
1327 rc = isbound = LDAP_BACK_CONN_ISBOUND( lc );
1330 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1335 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
1336 /* if someone else is about to bind it, give up and retry */
1338 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1340 ldap_pvt_thread_yield();
1344 /* otherwise this thread will bind it */
1345 LDAP_BACK_CONN_BINDING_SET( lc );
1351 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1355 * FIXME: we need to let clients use proxyAuthz
1356 * otherwise we cannot do symmetric pools of servers;
1357 * we have to live with the fact that a user can
1358 * authorize itself as any ID that is allowed
1359 * by the authzTo directive of the "proxyauthzdn".
1362 * NOTE: current Proxy Authorization specification
1363 * and implementation do not allow proxy authorization
1364 * control to be provided with Bind requests
1367 * if no bind took place yet, but the connection is bound
1368 * and the "idassert-authcDN" (or other ID) is set,
1369 * then bind as the asserting identity and explicitly
1370 * add the proxyAuthz control to every operation with the
1371 * dn bound to the connection as control value.
1372 * This is done also if this is the authorizing backend,
1373 * but the "override" flag is given to idassert.
1374 * It allows to use SASL bind and yet proxyAuthz users
1376 op->o_tag = LDAP_REQ_BIND;
1377 cb.sc_next = op->o_callback;
1378 cb.sc_private = &o_tag;
1379 cb.sc_response = ldap_back_dobind_cb;
1380 op->o_callback = &cb;
1382 if ( LDAP_BACK_CONN_ISIDASSERT( lc ) ) {
1383 if ( BER_BVISEMPTY( &binddn ) && BER_BVISEMPTY( &bindcred ) ) {
1384 /* if we got here, it shouldn't return result */
1385 rc = ldap_back_is_proxy_authz( op, rs,
1386 LDAP_BACK_DONTSEND, &binddn, &bindcred );
1389 rc = ldap_back_proxy_authz_bind( lc, op, rs, sendok, &binddn, &bindcred );
1393 #ifdef HAVE_CYRUS_SASL
1394 if ( LDAP_BACK_CONN_ISPRIV( lc )
1395 && li->li_acl_authmethod == LDAP_AUTH_SASL )
1397 void *defaults = NULL;
1399 if ( li->li_acl_secprops != NULL ) {
1400 rc = ldap_set_option( lc->lc_ld,
1401 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops );
1403 if ( rc != LDAP_OPT_SUCCESS ) {
1404 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
1405 "(SECPROPS,\"%s\") failed!\n",
1406 li->li_acl_secprops, 0, 0 );
1411 defaults = lutil_sasl_defaults( lc->lc_ld,
1412 li->li_acl_sasl_mech.bv_val,
1413 li->li_acl_sasl_realm.bv_val,
1414 li->li_acl_authcID.bv_val,
1415 li->li_acl_passwd.bv_val,
1417 if ( defaults == NULL ) {
1418 rs->sr_err = LDAP_OTHER;
1419 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1420 if ( sendok & LDAP_BACK_SENDERR ) {
1421 send_ldap_result( op, rs );
1426 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
1427 li->li_acl_authcDN.bv_val,
1428 li->li_acl_sasl_mech.bv_val, NULL, NULL,
1429 LDAP_SASL_QUIET, lutil_sasl_interact,
1432 lutil_sasl_freedefs( defaults );
1434 switch ( rs->sr_err ) {
1436 LDAP_BACK_CONN_ISBOUND_SET( lc );
1439 case LDAP_LOCAL_ERROR:
1440 /* list client API error codes that require
1441 * to taint the connection */
1442 /* FIXME: should actually retry? */
1443 LDAP_BACK_CONN_TAINTED_SET( lc );
1448 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1449 rs->sr_err = slap_map_api2result( rs );
1450 if ( sendok & LDAP_BACK_SENDERR ) {
1451 send_ldap_result( op, rs );
1456 if ( LDAP_BACK_QUARANTINE( li ) ) {
1457 ldap_back_quarantine( op, rs );
1462 #endif /* HAVE_CYRUS_SASL */
1465 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1466 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1467 LDAP_SASL_SIMPLE, &lc->lc_cred,
1468 NULL, NULL, &msgid );
1470 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
1471 if ( retries != LDAP_BACK_RETRY_NEVER ) {
1473 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1476 assert( lc->lc_refcnt > 0 );
1477 if ( lc->lc_refcnt == 1 ) {
1478 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1481 /* lc here must be the regular lc, reset and ready for init */
1482 rs->sr_err = ldap_back_prepare_conn( lc, op, rs, sendok );
1483 if ( rs->sr_err != LDAP_SUCCESS ) {
1484 sendok &= ~LDAP_BACK_SENDERR;
1490 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1493 if ( rs->sr_err == LDAP_SUCCESS ) {
1494 if ( retries > 0 ) {
1501 assert( lc->lc_refcnt == 1 );
1503 ldap_back_freeconn( li, lc, dolock );
1505 rs->sr_err = slap_map_api2result( rs );
1507 if ( LDAP_BACK_QUARANTINE( li ) ) {
1508 ldap_back_quarantine( op, rs );
1511 if ( rs->sr_err != LDAP_SUCCESS &&
1512 ( sendok & LDAP_BACK_SENDERR ) )
1514 if ( op->o_callback == &cb )
1515 op->o_callback = cb.sc_next;
1517 rs->sr_text = "Proxy can't contact remote server";
1518 send_ldap_result( op, rs );
1525 rc = ldap_back_op_result( lc, op, rs, msgid,
1526 -1, ( sendok | LDAP_BACK_BINDING ) );
1527 if ( rc == LDAP_SUCCESS ) {
1528 op->o_conn->c_authz_cookie = op->o_bd->be_private;
1529 LDAP_BACK_CONN_ISBOUND_SET( lc );
1533 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1534 rc = LDAP_BACK_CONN_ISBOUND( lc );
1536 ldap_back_release_conn_lock( li, lcp, dolock );
1538 } else if ( LDAP_BACK_SAVECRED( li ) ) {
1539 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1543 if ( op->o_callback == &cb )
1544 op->o_callback = cb.sc_next;
1553 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
1556 ldap_back_dobind( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1558 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1560 return ldap_back_dobind_int( lcp, op, rs,
1561 ( sendok | LDAP_BACK_GETCONN ), li->li_nretries, 1 );
1565 * ldap_back_default_rebind
1567 * This is a callback used for chasing referrals using the same
1568 * credentials as the original user on this session.
1571 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1572 ber_int_t msgid, void *params )
1574 ldapconn_t *lc = (ldapconn_t *)params;
1577 /* ... otherwise we couldn't get here */
1578 assert( lc != NULL );
1580 if ( !ldap_tls_inplace( ld ) ) {
1581 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1583 const char *text = NULL;
1585 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1586 LDAP_BACK_RETRY_DEFAULT, &text );
1587 if ( rc != LDAP_SUCCESS ) {
1591 #endif /* HAVE_TLS */
1593 /* FIXME: add checks on the URL/identity? */
1595 return ldap_sasl_bind_s( ld,
1596 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1597 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1601 * ldap_back_default_urllist
1604 ldap_back_default_urllist(
1606 LDAPURLDesc **urllist,
1610 ldapinfo_t *li = (ldapinfo_t *)params;
1611 LDAPURLDesc **urltail;
1613 if ( urllist == url ) {
1614 return LDAP_SUCCESS;
1617 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1620 *urltail = *urllist;
1624 if ( !li->li_uri_mutex_do_not_lock ) {
1625 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1629 ch_free( li->li_uri );
1632 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1634 if ( !li->li_uri_mutex_do_not_lock ) {
1635 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1638 return LDAP_SUCCESS;
1647 ldap_back_send_t sendok )
1649 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1651 /* default behavior */
1652 if ( LDAP_BACK_ABANDON( li ) ) {
1653 return ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1656 if ( LDAP_BACK_IGNORE( li ) ) {
1657 return ldap_pvt_discard( lc->lc_ld, msgid );
1660 if ( LDAP_BACK_CANCEL( li ) ) {
1661 /* FIXME: asynchronous? */
1662 return ldap_cancel_s( lc->lc_ld, msgid, NULL, NULL );
1671 ldap_back_op_result(
1677 ldap_back_send_t sendok )
1679 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1684 LDAPControl **ctrls = NULL;
1687 rs->sr_matched = NULL;
1689 rs->sr_ctrls = NULL;
1691 /* if the error recorded in the reply corresponds
1692 * to a successful state, get the error from the
1693 * remote server response */
1694 if ( LDAP_ERR_OK( rs->sr_err ) ) {
1697 LDAPMessage *res = NULL;
1698 time_t stoptime = (time_t)(-1);
1699 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
1700 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
1701 const char *timeout_text = "Operation timed out";
1703 /* if timeout is not specified, compute and use
1704 * the one specific to the ongoing operation */
1705 if ( timeout == (time_t)(-1) ) {
1706 slap_op_t opidx = slap_req2op( op->o_tag );
1708 if ( opidx == SLAP_OP_SEARCH ) {
1709 if ( op->ors_tlimit <= 0 ) {
1713 timeout = op->ors_tlimit;
1714 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
1715 timeout_text = NULL;
1719 timeout = li->li_timeout[ opidx ];
1723 /* better than nothing :) */
1724 if ( timeout == 0 ) {
1725 if ( li->li_idle_timeout ) {
1726 timeout = li->li_idle_timeout;
1728 } else if ( li->li_conn_ttl ) {
1729 timeout = li->li_conn_ttl;
1734 stoptime = op->o_time + timeout;
1737 LDAP_BACK_TV_SET( &tv );
1740 /* if result parsing fails, note the failure reason */
1741 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1744 if ( timeout && slap_get_time() > stoptime ) {
1745 if ( sendok & LDAP_BACK_BINDING ) {
1746 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1749 /* let it be used, but taint/delete it so that
1750 * no-one else can look it up any further */
1751 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1753 #if LDAP_BACK_PRINT_CONNTREE > 0
1754 ldap_back_print_conntree( li, ">>> ldap_back_getconn(timeout)" );
1755 #endif /* LDAP_BACK_PRINT_CONNTREE */
1757 (void)ldap_back_conn_delete( li, lc );
1758 LDAP_BACK_CONN_TAINTED_SET( lc );
1760 #if LDAP_BACK_PRINT_CONNTREE > 0
1761 ldap_back_print_conntree( li, "<<< ldap_back_getconn(timeout)" );
1762 #endif /* LDAP_BACK_PRINT_CONNTREE */
1763 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1766 (void)ldap_back_cancel( lc, op, rs, msgid, sendok );
1768 rs->sr_err = timeout_err;
1769 rs->sr_text = timeout_text;
1774 LDAP_BACK_TV_SET( &tv );
1775 ldap_pvt_thread_yield();
1779 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1784 /* otherwise get the result; if it is not
1785 * LDAP_SUCCESS, record it in the reply
1786 * structure (this includes
1787 * LDAP_COMPARE_{TRUE|FALSE}) */
1789 /* only touch when activity actually took place... */
1790 if ( li->li_idle_timeout && lc ) {
1791 lc->lc_time = op->o_time;
1794 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1795 &match, &text, &refs, &ctrls, 1 );
1797 if ( rc != LDAP_SUCCESS ) {
1801 /* RFC 4511: referrals can only appear
1802 * if result code is LDAP_REFERRAL */
1804 && refs[ 0 ] != NULL
1805 && refs[ 0 ][ 0 ] != '\0' )
1807 if ( rs->sr_err != LDAP_REFERRAL ) {
1808 Debug( LDAP_DEBUG_ANY,
1809 "%s ldap_back_op_result: "
1810 "got referrals with err=%d\n",
1817 for ( i = 0; refs[ i ] != NULL; i++ )
1819 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1821 for ( i = 0; refs[ i ] != NULL; i++ ) {
1822 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1824 BER_BVZERO( &rs->sr_ref[ i ] );
1827 } else if ( rs->sr_err == LDAP_REFERRAL ) {
1828 Debug( LDAP_DEBUG_ANY,
1829 "%s ldap_back_op_result: "
1830 "got err=%d with null "
1831 "or empty referrals\n",
1835 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1838 if ( ctrls != NULL ) {
1839 rs->sr_ctrls = ctrls;
1844 /* if the error in the reply structure is not
1845 * LDAP_SUCCESS, try to map it from client
1846 * to server error */
1847 if ( !LDAP_ERR_OK( rs->sr_err ) ) {
1848 rs->sr_err = slap_map_api2result( rs );
1850 /* internal ops ( op->o_conn == NULL )
1851 * must not reply to client */
1852 if ( op->o_conn && !op->o_do_not_cache && match ) {
1854 /* record the (massaged) matched
1855 * DN into the reply structure */
1856 rs->sr_matched = match;
1860 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1861 if ( !( sendok & LDAP_BACK_RETRYING ) ) {
1862 if ( LDAP_BACK_QUARANTINE( li ) ) {
1863 ldap_back_quarantine( op, rs );
1865 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1866 if ( rs->sr_text == NULL ) rs->sr_text = "Proxy operation retry failed";
1867 send_ldap_result( op, rs );
1871 } else if ( op->o_conn &&
1872 ( ( ( sendok & LDAP_BACK_SENDOK ) && LDAP_ERR_OK( rs->sr_err ) )
1873 || ( ( sendok & LDAP_BACK_SENDERR ) && !LDAP_ERR_OK( rs->sr_err ) ) ) )
1875 send_ldap_result( op, rs );
1879 if ( rs->sr_matched != match ) {
1880 free( (char *)rs->sr_matched );
1882 rs->sr_matched = NULL;
1883 ldap_memfree( match );
1887 ldap_memfree( text );
1892 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1897 ber_memvfree( (void **)refs );
1901 assert( rs->sr_ctrls != NULL );
1902 ldap_controls_free( ctrls );
1903 rs->sr_ctrls = NULL;
1906 return( LDAP_ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1909 /* return true if bound, false if failed */
1911 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1913 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1916 assert( lcp != NULL );
1917 assert( *lcp != NULL );
1919 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1921 if ( (*lcp)->lc_refcnt == 1 ) {
1922 int binding = LDAP_BACK_CONN_BINDING( *lcp );
1924 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1925 Debug( LDAP_DEBUG_ANY,
1926 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1927 op->o_log_prefix, li->li_uri,
1928 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1929 "" : (*lcp)->lc_bound_ndn.bv_val );
1930 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1932 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1933 (*lcp)->lc_ld = NULL;
1934 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1936 /* lc here must be the regular lc, reset and ready for init */
1937 rc = ldap_back_prepare_conn( *lcp, op, rs, sendok );
1938 if ( rc != LDAP_SUCCESS ) {
1939 /* freeit, because lc_refcnt == 1 */
1940 (*lcp)->lc_refcnt = 0;
1941 (void)ldap_back_freeconn( li, *lcp, 0 );
1945 } else if ( ( sendok & LDAP_BACK_BINDING ) ) {
1947 LDAP_BACK_CONN_BINDING_SET( *lcp );
1952 rc = ldap_back_dobind_int( lcp, op, rs, sendok, 0, 0 );
1953 if ( rc == 0 && *lcp != NULL ) {
1954 /* freeit, because lc_refcnt == 1 */
1955 (*lcp)->lc_refcnt = 0;
1956 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1957 (void)ldap_back_freeconn( li, *lcp, 0 );
1963 Debug( LDAP_DEBUG_TRACE,
1964 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1965 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1967 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1968 ldap_back_release_conn_lock( li, lcp, 0 );
1969 assert( *lcp == NULL );
1971 if ( sendok & LDAP_BACK_SENDERR ) {
1972 rs->sr_err = LDAP_UNAVAILABLE;
1973 rs->sr_text = "Unable to retry";
1974 send_ldap_result( op, rs );
1978 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1984 ldap_back_is_proxy_authz( Operation *op, SlapReply *rs, ldap_back_send_t sendok,
1985 struct berval *binddn, struct berval *bindcred )
1987 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1991 if ( op->o_conn == NULL || op->o_do_not_cache ) {
1995 /* don't proxyAuthz if protocol is not LDAPv3 */
1996 switch ( li->li_version ) {
2001 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2007 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2008 if ( sendok & LDAP_BACK_SENDERR ) {
2009 send_ldap_result( op, rs );
2016 *binddn = slap_empty_bv;
2017 *bindcred = slap_empty_bv;
2019 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2020 ndn = op->o_conn->c_ndn;
2026 switch ( li->li_idassert_mode ) {
2027 case LDAP_BACK_IDASSERT_LEGACY:
2028 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
2029 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
2031 *binddn = li->li_idassert_authcDN;
2032 *bindcred = li->li_idassert_passwd;
2039 /* NOTE: rootdn can always idassert */
2040 if ( BER_BVISNULL( &ndn )
2041 && li->li_idassert_authz == NULL
2042 && !( li->li_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL ) )
2044 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2045 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
2046 if ( sendok & LDAP_BACK_SENDERR ) {
2047 send_ldap_result( op, rs );
2052 rs->sr_err = LDAP_SUCCESS;
2053 *binddn = slap_empty_bv;
2054 *bindcred = slap_empty_bv;
2060 } else if ( !be_isroot( op ) ) {
2061 if ( li->li_idassert_passthru ) {
2062 struct berval authcDN;
2064 if ( BER_BVISNULL( &ndn ) ) {
2065 authcDN = slap_empty_bv;
2070 rs->sr_err = slap_sasl_matches( op, li->li_idassert_passthru,
2071 &authcDN, &authcDN );
2072 if ( rs->sr_err == LDAP_SUCCESS ) {
2078 if ( li->li_idassert_authz ) {
2079 struct berval authcDN;
2081 if ( BER_BVISNULL( &ndn ) ) {
2082 authcDN = slap_empty_bv;
2087 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
2088 &authcDN, &authcDN );
2089 if ( rs->sr_err != LDAP_SUCCESS ) {
2090 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2091 if ( sendok & LDAP_BACK_SENDERR ) {
2092 send_ldap_result( op, rs );
2097 rs->sr_err = LDAP_SUCCESS;
2098 *binddn = slap_empty_bv;
2099 *bindcred = slap_empty_bv;
2108 *binddn = li->li_idassert_authcDN;
2109 *bindcred = li->li_idassert_passwd;
2119 ldap_back_proxy_authz_bind(
2123 ldap_back_send_t sendok,
2124 struct berval *binddn,
2125 struct berval *bindcred )
2127 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2132 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2133 ndn = op->o_conn->c_ndn;
2139 if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
2140 #ifdef HAVE_CYRUS_SASL
2141 void *defaults = NULL;
2142 struct berval authzID = BER_BVNULL;
2145 /* if SASL supports native authz, prepare for it */
2146 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
2147 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2149 switch ( li->li_idassert_mode ) {
2150 case LDAP_BACK_IDASSERT_OTHERID:
2151 case LDAP_BACK_IDASSERT_OTHERDN:
2152 authzID = li->li_idassert_authzID;
2155 case LDAP_BACK_IDASSERT_ANONYMOUS:
2156 BER_BVSTR( &authzID, "dn:" );
2159 case LDAP_BACK_IDASSERT_SELF:
2160 if ( BER_BVISNULL( &ndn ) ) {
2161 /* connection is not authc'd, so don't idassert */
2162 BER_BVSTR( &authzID, "dn:" );
2165 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
2166 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
2167 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
2168 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
2169 ndn.bv_val, ndn.bv_len + 1 );
2178 if ( li->li_idassert_secprops != NULL ) {
2179 rs->sr_err = ldap_set_option( lc->lc_ld,
2180 LDAP_OPT_X_SASL_SECPROPS,
2181 (void *)li->li_idassert_secprops );
2183 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
2184 rs->sr_err = LDAP_OTHER;
2185 if ( sendok & LDAP_BACK_SENDERR ) {
2186 send_ldap_result( op, rs );
2188 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2193 defaults = lutil_sasl_defaults( lc->lc_ld,
2194 li->li_idassert_sasl_mech.bv_val,
2195 li->li_idassert_sasl_realm.bv_val,
2196 li->li_idassert_authcID.bv_val,
2197 li->li_idassert_passwd.bv_val,
2199 if ( defaults == NULL ) {
2200 rs->sr_err = LDAP_OTHER;
2201 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2202 if ( sendok & LDAP_BACK_SENDERR ) {
2203 send_ldap_result( op, rs );
2208 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn->bv_val,
2209 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
2210 LDAP_SASL_QUIET, lutil_sasl_interact,
2213 switch ( rs->sr_err ) {
2215 LDAP_BACK_CONN_ISBOUND_SET( lc );
2218 case LDAP_LOCAL_ERROR:
2219 /* list client API error codes that require
2220 * to taint the connection */
2221 /* FIXME: should actually retry? */
2222 LDAP_BACK_CONN_TAINTED_SET( lc );
2227 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2228 rs->sr_err = slap_map_api2result( rs );
2229 if ( sendok & LDAP_BACK_SENDERR ) {
2230 send_ldap_result( op, rs );
2235 lutil_sasl_freedefs( defaults );
2237 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
2241 #endif /* HAVE_CYRUS_SASL */
2244 switch ( li->li_idassert_authmethod ) {
2245 case LDAP_AUTH_NONE:
2246 /* FIXME: do we really need this? */
2247 BER_BVSTR( binddn, "" );
2248 BER_BVSTR( bindcred, "" );
2251 case LDAP_AUTH_SIMPLE:
2252 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
2253 binddn->bv_val, LDAP_SASL_SIMPLE,
2254 bindcred, NULL, NULL, &msgid );
2255 rc = ldap_back_op_result( lc, op, rs, msgid,
2256 -1, ( sendok | LDAP_BACK_BINDING ) );
2261 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
2262 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
2263 if ( sendok & LDAP_BACK_SENDERR ) {
2264 send_ldap_result( op, rs );
2269 if ( rc == LDAP_SUCCESS ) {
2270 /* set rebind stuff in case of successful proxyAuthz bind,
2271 * so that referral chasing is attempted using the right
2273 LDAP_BACK_CONN_ISBOUND_SET( lc );
2274 op->o_conn->c_authz_cookie = op->o_bd->be_private;
2275 if ( !BER_BVISNULL( binddn ) ) {
2276 ber_bvreplace( &lc->lc_bound_ndn, binddn );
2279 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
2280 memset( lc->lc_cred.bv_val, 0,
2281 lc->lc_cred.bv_len );
2284 if ( LDAP_BACK_SAVECRED( li ) ) {
2285 if ( !BER_BVISNULL( bindcred ) ) {
2286 ber_bvreplace( &lc->lc_cred, bindcred );
2287 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
2291 lc->lc_cred.bv_len = 0;
2295 return LDAP_BACK_CONN_ISBOUND( lc );
2299 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
2300 * to existing server-side controls if required; if not,
2301 * the existing server-side controls are placed in *pctrls.
2302 * The caller, after using the controls in client API
2303 * operations, if ( *pctrls != op->o_ctrls ), should
2304 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
2305 * The function returns success if the control could
2306 * be added if required, or if it did nothing; in the future,
2307 * it might return some error if it failed.
2309 * if no bind took place yet, but the connection is bound
2310 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
2311 * and explicitly add proxyAuthz the control to every operation
2312 * with the dn bound to the connection as control value.
2314 * If no server-side controls are defined for the operation,
2315 * simply add the proxyAuthz control; otherwise, if the
2316 * proxyAuthz control is not already set, add it as
2319 * FIXME: is controls order significant for security?
2320 * ANSWER: controls ordering and interoperability
2321 * must be indicated by the specs of each control; if none
2322 * is specified, the order is irrelevant.
2325 ldap_back_proxy_authz_ctrl(
2328 struct berval *bound_ndn,
2330 slap_idassert_t *si,
2333 slap_idassert_mode_t mode;
2334 struct berval assertedID,
2338 rs->sr_err = SLAP_CB_CONTINUE;
2340 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
2341 * but if it is not set this test fails. We need a different
2342 * means to detect if idassert is enabled */
2343 if ( ( BER_BVISNULL( &si->si_bc.sb_authcId ) || BER_BVISEMPTY( &si->si_bc.sb_authcId ) )
2344 && ( BER_BVISNULL( &si->si_bc.sb_binddn ) || BER_BVISEMPTY( &si->si_bc.sb_binddn ) )
2345 && BER_BVISNULL( &si->si_bc.sb_saslmech ) )
2350 if ( !op->o_conn || op->o_do_not_cache || ( isroot = be_isroot( op ) ) ) {
2354 if ( op->o_tag == LDAP_REQ_BIND ) {
2355 ndn = op->o_req_ndn;
2357 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
2358 ndn = op->o_conn->c_ndn;
2364 if ( si->si_mode == LDAP_BACK_IDASSERT_LEGACY ) {
2365 if ( op->o_proxy_authz ) {
2367 * FIXME: we do not want to perform proxyAuthz
2368 * on behalf of the client, because this would
2369 * be performed with "proxyauthzdn" privileges.
2371 * This might actually be too strict, since
2372 * the "proxyauthzdn" authzTo, and each entry's
2373 * authzFrom attributes may be crafted
2374 * to avoid unwanted proxyAuthz to take place.
2377 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2378 rs->sr_text = "proxyAuthz not allowed within namingContext";
2383 if ( !BER_BVISNULL( bound_ndn ) ) {
2387 if ( BER_BVISNULL( &ndn ) ) {
2391 if ( BER_BVISNULL( &si->si_bc.sb_binddn ) ) {
2395 } else if ( si->si_bc.sb_method == LDAP_AUTH_SASL ) {
2396 if ( ( si->si_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
2398 /* already asserted in SASL via native authz */
2402 } else if ( si->si_authz && !isroot ) {
2404 struct berval authcDN;
2406 if ( BER_BVISNULL( &ndn ) ) {
2407 authcDN = slap_empty_bv;
2411 rc = slap_sasl_matches( op, si->si_authz,
2412 &authcDN, &authcDN );
2413 if ( rc != LDAP_SUCCESS ) {
2414 if ( si->si_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
2415 /* ndn is not authorized
2416 * to use idassert */
2423 if ( op->o_proxy_authz ) {
2426 * 1) ignore the already set proxyAuthz control
2427 * 2) leave it in place, and don't set ours
2429 * 4) reject the operation
2431 * option (4) is very drastic
2432 * option (3) will make the remote server reject
2433 * the operation, thus being equivalent to (4)
2434 * option (2) will likely break the idassert
2435 * assumptions, so we cannot accept it;
2436 * option (1) means that we are contradicting
2437 * the client's reques.
2439 * I think (4) is the only correct choice.
2441 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2442 rs->sr_text = "proxyAuthz not allowed within namingContext";
2445 if ( op->o_is_auth_check ) {
2446 mode = LDAP_BACK_IDASSERT_NOASSERT;
2453 case LDAP_BACK_IDASSERT_LEGACY:
2454 /* original behavior:
2455 * assert the client's identity */
2456 case LDAP_BACK_IDASSERT_SELF:
2460 case LDAP_BACK_IDASSERT_ANONYMOUS:
2461 /* assert "anonymous" */
2462 assertedID = slap_empty_bv;
2465 case LDAP_BACK_IDASSERT_NOASSERT:
2466 /* don't assert; bind as proxyauthzdn */
2469 case LDAP_BACK_IDASSERT_OTHERID:
2470 case LDAP_BACK_IDASSERT_OTHERDN:
2471 /* assert idassert DN */
2472 assertedID = si->si_bc.sb_authzId;
2479 /* if we got here, "" is allowed to proxyAuthz */
2480 if ( BER_BVISNULL( &assertedID ) ) {
2481 assertedID = slap_empty_bv;
2484 /* don't idassert the bound DN (ITS#4497) */
2485 if ( dn_match( &assertedID, bound_ndn ) ) {
2489 ctrl->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
2491 switch ( si->si_mode ) {
2492 /* already in u:ID or dn:DN form */
2493 case LDAP_BACK_IDASSERT_OTHERID:
2494 case LDAP_BACK_IDASSERT_OTHERDN:
2495 ber_dupbv_x( &ctrl->ldctl_value, &assertedID, op->o_tmpmemctx );
2496 rs->sr_err = LDAP_SUCCESS;
2499 /* needs the dn: prefix */
2501 ctrl->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
2502 ctrl->ldctl_value.bv_val = op->o_tmpalloc( ctrl->ldctl_value.bv_len + 1,
2504 AC_MEMCPY( ctrl->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
2505 AC_MEMCPY( &ctrl->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
2506 assertedID.bv_val, assertedID.bv_len + 1 );
2507 rs->sr_err = LDAP_SUCCESS;
2511 /* Older versions of <draft-weltman-ldapv3-proxy> required
2512 * to encode the value of the authzID (and called it proxyDN);
2513 * this hack provides compatibility with those DSAs that
2514 * implement it this way */
2515 if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
2516 struct berval authzID = ctrl->ldctl_value;
2517 BerElementBuffer berbuf;
2518 BerElement *ber = (BerElement *)&berbuf;
2521 ber_init2( ber, 0, LBER_USE_DER );
2522 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2524 tag = ber_printf( ber, "O", &authzID );
2525 if ( tag == LBER_ERROR ) {
2526 rs->sr_err = LDAP_OTHER;
2530 if ( ber_flatten2( ber, &ctrl->ldctl_value, 1 ) == -1 ) {
2531 rs->sr_err = LDAP_OTHER;
2535 rs->sr_err = LDAP_SUCCESS;
2538 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2539 ber_free_buf( ber );
2541 if ( rs->sr_err != LDAP_SUCCESS ) {
2545 } else if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
2546 struct berval authzID = ctrl->ldctl_value,
2548 BerElementBuffer berbuf;
2549 BerElement *ber = (BerElement *)&berbuf;
2552 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
2553 rs->sr_err = LDAP_PROTOCOL_ERROR;
2558 tmp.bv_val += STRLENOF( "dn:" );
2559 tmp.bv_len -= STRLENOF( "dn:" );
2561 ber_init2( ber, 0, LBER_USE_DER );
2562 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2564 /* apparently, Mozilla API encodes this
2565 * as "SEQUENCE { LDAPDN }" */
2566 tag = ber_printf( ber, "{O}", &tmp );
2567 if ( tag == LBER_ERROR ) {
2568 rs->sr_err = LDAP_OTHER;
2572 if ( ber_flatten2( ber, &ctrl->ldctl_value, 1 ) == -1 ) {
2573 rs->sr_err = LDAP_OTHER;
2577 ctrl->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
2578 rs->sr_err = LDAP_SUCCESS;
2581 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2582 ber_free_buf( ber );
2584 if ( rs->sr_err != LDAP_SUCCESS ) {
2597 * if any needs to be added, it is prepended to existing ones,
2598 * in a newly allocated array. The companion function
2599 * ldap_back_controls_free() must be used to restore the original
2600 * status of op->o_ctrls.
2603 ldap_back_controls_add(
2607 LDAPControl ***pctrls )
2609 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
2611 LDAPControl **ctrls = NULL;
2612 /* set to the maximum number of controls this backend can add */
2613 LDAPControl c[ 2 ] = { { 0 } };
2614 int n = 0, i, j1 = 0, j2 = 0;
2618 rs->sr_err = LDAP_SUCCESS;
2620 /* don't add controls if protocol is not LDAPv3 */
2621 switch ( li->li_version ) {
2626 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
2635 /* put controls that go __before__ existing ones here */
2637 /* proxyAuthz for identity assertion */
2638 switch ( ldap_back_proxy_authz_ctrl( op, rs, &lc->lc_bound_ndn,
2639 li->li_version, &li->li_idassert, &c[ j1 ] ) )
2641 case SLAP_CB_CONTINUE:
2652 /* put controls that go __after__ existing ones here */
2654 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
2655 /* FIXME: according to <draft-wahl-ldap-session>,
2656 * the server should check if the control can be added
2657 * based on the identity of the client and so */
2659 /* session tracking */
2660 if ( LDAP_BACK_ST_REQUEST( li ) ) {
2661 switch ( slap_ctrl_session_tracking_request_add( op, rs, &c[ j1 + j2 ] ) ) {
2662 case SLAP_CB_CONTINUE:
2673 #endif /* SLAP_CONTROL_X_SESSION_TRACKING */
2675 if ( rs->sr_err == SLAP_CB_CONTINUE ) {
2676 rs->sr_err = LDAP_SUCCESS;
2679 /* if nothing to do, just bail out */
2680 if ( j1 == 0 && j2 == 0 ) {
2684 assert( j1 + j2 <= (int) (sizeof( c )/sizeof( c[0] )) );
2686 if ( op->o_ctrls ) {
2687 for ( n = 0; op->o_ctrls[ n ]; n++ )
2688 /* just count ctrls */ ;
2691 ctrls = op->o_tmpalloc( (n + j1 + j2 + 1) * sizeof( LDAPControl * ) + ( j1 + j2 ) * sizeof( LDAPControl ),
2694 ctrls[ 0 ] = (LDAPControl *)&ctrls[ n + j1 + j2 + 1 ];
2695 *ctrls[ 0 ] = c[ 0 ];
2696 for ( i = 1; i < j1; i++ ) {
2697 ctrls[ i ] = &ctrls[ 0 ][ i ];
2698 *ctrls[ i ] = c[ i ];
2703 if ( op->o_ctrls ) {
2704 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
2705 ctrls[ i + j1 ] = op->o_ctrls[ i ];
2711 ctrls[ n ] = (LDAPControl *)&ctrls[ n + j2 + 1 ] + j1;
2712 *ctrls[ n ] = c[ j1 ];
2713 for ( i = 1; i < j2; i++ ) {
2714 ctrls[ n + i ] = &ctrls[ n ][ i ];
2715 *ctrls[ n + i ] = c[ i ];
2719 ctrls[ n + j2 ] = NULL;
2722 if ( ctrls == NULL ) {
2723 ctrls = op->o_ctrls;
2732 ldap_back_controls_free( Operation *op, SlapReply *rs, LDAPControl ***pctrls )
2734 LDAPControl **ctrls = *pctrls;
2736 /* we assume that the controls added by the proxy come first,
2737 * so as soon as we find op->o_ctrls[ 0 ] we can stop */
2738 if ( ctrls && ctrls != op->o_ctrls ) {
2739 int i = 0, n = 0, n_added;
2740 LDAPControl *lower, *upper;
2742 assert( ctrls[ 0 ] != NULL );
2744 for ( n = 0; ctrls[ n ] != NULL; n++ )
2747 if ( op->o_ctrls ) {
2748 for ( i = 0; op->o_ctrls[ i ] != NULL; i++ )
2753 lower = (LDAPControl *)&ctrls[ n ];
2754 upper = &lower[ n_added ];
2756 for ( i = 0; ctrls[ i ] != NULL; i++ ) {
2757 if ( ctrls[ i ] < lower || ctrls[ i ] >= upper ) {
2758 /* original; don't touch */
2762 if ( !BER_BVISNULL( &ctrls[ i ]->ldctl_value ) ) {
2763 op->o_tmpfree( ctrls[ i ]->ldctl_value.bv_val, op->o_tmpmemctx );
2767 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2776 ldap_back_conn2str( const ldapconn_base_t *lc, char *buf, ber_len_t buflen )
2778 char tbuf[ SLAP_TEXT_BUFLEN ];
2779 char *ptr = buf, *end = buf + buflen;
2782 if ( ptr + sizeof("conn=") > end ) return -1;
2783 ptr = lutil_strcopy( ptr, "conn=" );
2785 len = ldap_back_connid2str( lc, ptr, (ber_len_t)(end - ptr) );
2787 if ( ptr >= end ) return -1;
2789 if ( !BER_BVISNULL( &lc->lcb_local_ndn ) ) {
2790 if ( ptr + sizeof(" DN=\"\"") + lc->lcb_local_ndn.bv_len > end ) return -1;
2791 ptr = lutil_strcopy( ptr, " DN=\"" );
2792 ptr = lutil_strncopy( ptr, lc->lcb_local_ndn.bv_val, lc->lcb_local_ndn.bv_len );
2796 if ( lc->lcb_create_time != 0 ) {
2797 len = snprintf( tbuf, sizeof(tbuf), "%ld", lc->lcb_create_time );
2798 if ( ptr + sizeof(" created=") + len >= end ) return -1;
2799 ptr = lutil_strcopy( ptr, " created=" );
2800 ptr = lutil_strcopy( ptr, tbuf );
2803 if ( lc->lcb_time != 0 ) {
2804 len = snprintf( tbuf, sizeof(tbuf), "%ld", lc->lcb_time );
2805 if ( ptr + sizeof(" modified=") + len >= end ) return -1;
2806 ptr = lutil_strcopy( ptr, " modified=" );
2807 ptr = lutil_strcopy( ptr, tbuf );
2810 len = snprintf( tbuf, sizeof(tbuf), "%u", lc->lcb_refcnt );
2811 if ( ptr + sizeof(" refcnt=") + len >= end ) return -1;
2812 ptr = lutil_strcopy( ptr, " refcnt=" );
2813 ptr = lutil_strcopy( ptr, tbuf );
2819 ldap_back_connid2str( const ldapconn_base_t *lc, char *buf, ber_len_t buflen )
2821 static struct berval conns[] = {
2823 BER_BVC("ROOTDN-TLS"),
2825 BER_BVC("ANON-TLS"),
2827 BER_BVC("BIND-TLS"),
2833 if ( LDAP_BACK_PCONN_ISPRIV( (const ldapconn_t *)lc ) ) {
2837 cid = (long)lc->lcb_conn;
2838 assert( cid >= LDAP_BACK_PCONN_FIRST && cid < LDAP_BACK_PCONN_LAST );
2842 if ( bv->bv_len >= buflen ) {
2843 return bv->bv_len + 1;
2847 lutil_strncopy( buf, bv->bv_val, bv->bv_len + 1 );
2850 len = snprintf( buf, buflen, "%lu", lc->lcb_conn->c_connid );