1 /* bind.c - ldap backend bind function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2006 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 #ifndef PRINT_CONNTREE
39 #define PRINT_CONNTREE 0
40 #endif /* !PRINT_CONNTREE */
42 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ "2.16.840.1.113730.3.4.12"
44 #if PRINT_CONNTREE > 0
46 ravl_print( Avlnode *root, int depth )
55 ravl_print( root->avl_right, depth+1 );
57 for ( i = 0; i < depth; i++ ) {
58 fprintf( stderr, "-" );
62 fprintf( stderr, "lc=%p local=\"%s\" conn=%p %s refcnt=%d\n",
64 lc->lc_local_ndn.bv_val ? lc->lc_local_ndn.bv_val : "",
66 avl_bf2str( root->avl_bf ), lc->lc_refcnt );
68 ravl_print( root->avl_left, depth+1 );
72 myprint( Avlnode *root, char *msg )
74 fprintf( stderr, "========> %s\n", msg );
77 fprintf( stderr, "\tNULL\n" );
80 ravl_print( root, 0 );
83 fprintf( stderr, "<======== %s\n", msg );
85 #endif /* PRINT_CONNTREE */
88 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
91 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
94 ldap_back_conndnlc_cmp( const void *c1, const void *c2 );
97 ldap_back_bind( Operation *op, SlapReply *rs )
99 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
105 lc = ldap_back_getconn( op, rs, LDAP_BACK_BIND_SERR );
110 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
111 ch_free( lc->lc_bound_ndn.bv_val );
112 BER_BVZERO( &lc->lc_bound_ndn );
114 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
116 /* method is always LDAP_AUTH_SIMPLE if we got here */
117 rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
119 &op->orb_cred, op->o_ctrls, NULL, &msgid );
120 rc = ldap_back_op_result( lc, op, rs, msgid,
121 li->li_timeout[ SLAP_OP_BIND ],
122 LDAP_BACK_BIND_SERR );
123 if ( rc == LDAP_SUCCESS ) {
124 /* If defined, proxyAuthz will be used also when
125 * back-ldap is the authorizing backend; for this
126 * purpose, a successful bind is followed by a
127 * bind with the configured identity assertion */
128 /* NOTE: use with care */
129 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
130 ldap_back_proxy_authz_bind( lc, op, rs, LDAP_BACK_SENDERR );
131 if ( !LDAP_BACK_CONN_ISBOUND( lc ) ) {
137 /* rebind is now done inside ldap_back_proxy_authz_bind()
138 * in case of success */
139 LDAP_BACK_CONN_ISBOUND_SET( lc );
140 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
142 if ( LDAP_BACK_SAVECRED( li ) ) {
143 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
144 memset( lc->lc_cred.bv_val, 0,
145 lc->lc_cred.bv_len );
147 ber_bvreplace( &lc->lc_cred, &op->orb_cred );
148 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
153 assert( lc->lc_binding == 1 );
156 /* must re-insert if local DN changed as result of bind */
157 if ( !LDAP_BACK_CONN_ISBOUND( lc )
158 || ( LDAP_BACK_CONN_ISBOUND( lc )
159 && !dn_match( &op->o_req_ndn, &lc->lc_local_ndn ) ) )
164 /* wait for all other ops to release the connection */
166 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
167 if ( lc->lc_refcnt > 1 ) {
168 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
169 ldap_pvt_thread_yield();
173 assert( lc->lc_refcnt == 1 );
174 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
175 ldap_back_conndnlc_cmp );
176 assert( tmplc == NULL || lc == tmplc );
178 /* delete all cached connections with the current connection */
179 if ( LDAP_BACK_SINGLECONN( li ) ) {
180 while ( ( tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc, ldap_back_conn_cmp ) ) != NULL )
182 Debug( LDAP_DEBUG_TRACE,
183 "=>ldap_back_bind: destroying conn %ld (refcnt=%u)\n",
184 LDAP_BACK_PCONN_ID( lc->lc_conn ), lc->lc_refcnt, 0 );
186 if ( tmplc->lc_refcnt != 0 ) {
188 LDAP_BACK_CONN_TAINTED_SET( tmplc );
192 * Needs a test because the handler may be corrupted,
193 * and calling ldap_unbind on a corrupted header results
194 * in a segmentation fault
196 ldap_back_conn_free( tmplc );
201 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
202 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
203 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
204 lc->lc_conn = LDAP_BACK_PCONN_SET( op );
206 lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
207 ldap_back_conndn_cmp, ldap_back_conndn_dup );
210 #if PRINT_CONNTREE > 0
211 myprint( li->li_conninfo.lai_tree, "ldap_back_bind" );
212 #endif /* PRINT_CONNTREE */
214 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
220 /* duplicate; someone else successfully bound
221 * on the same connection with the same identity;
222 * we can do this because lc_refcnt == 1 */
223 ldap_back_conn_free( lc );
229 ldap_back_release_conn( op, rs, lc );
236 * ldap_back_conndn_cmp
238 * compares two ldapconn_t based on the value of the conn pointer
239 * and of the local DN; used by avl stuff for insert, lookup
243 ldap_back_conndn_cmp( const void *c1, const void *c2 )
245 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
246 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
249 /* If local DNs don't match, it is definitely not a match */
250 /* For shared sessions, conn is NULL. Only explicitly
251 * bound sessions will have non-NULL conn.
253 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
255 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
262 * ldap_back_conndnlc_cmp
264 * compares two ldapconn_t based on the value of the conn pointer,
265 * the local DN and the lc pointer; used by avl stuff for insert, lookup
269 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
271 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
272 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
275 /* If local DNs don't match, it is definitely not a match */
276 /* For shared sessions, conn is NULL. Only explicitly
277 * bound sessions will have non-NULL conn.
279 rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
281 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
283 rc = SLAP_PTRCMP( lc1, lc2 );
293 * compares two ldapconn_t based on the value of the conn pointer;
294 * used by avl stuff for delete of all conns with the same connid
297 ldap_back_conn_cmp( const void *c1, const void *c2 )
299 const ldapconn_t *lc1 = (const ldapconn_t *)c1;
300 const ldapconn_t *lc2 = (const ldapconn_t *)c2;
302 /* For shared sessions, conn is NULL. Only explicitly
303 * bound sessions will have non-NULL conn.
305 return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
309 * ldap_back_conndn_dup
311 * returns -1 in case a duplicate ldapconn_t has been inserted;
315 ldap_back_conndn_dup( void *c1, void *c2 )
317 ldapconn_t *lc1 = (ldapconn_t *)c1;
318 ldapconn_t *lc2 = (ldapconn_t *)c2;
320 /* Cannot have more than one shared session with same DN */
321 if ( lc1->lc_conn == lc2->lc_conn &&
322 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
331 ldap_back_freeconn( Operation *op, ldapconn_t *lc, int dolock )
333 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
337 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
340 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
341 ldap_back_conndnlc_cmp );
342 assert( LDAP_BACK_CONN_TAINTED( lc ) || tmplc == lc );
343 if ( lc->lc_refcnt == 0 ) {
344 ldap_back_conn_free( (void *)lc );
348 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
365 int rc = LDAP_SUCCESS;
368 /* this is ridiculous... */
369 dummy.li_flags = flags;
371 /* start TLS ("tls-[try-]{start,propagate}" statements) */
372 if ( ( LDAP_BACK_USE_TLS( &dummy ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS( &dummy ) ) )
373 && !ldap_is_ldaps_url( url ) )
375 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
377 * use asynchronous StartTLS
378 * in case, chase referral (not implemented yet)
382 if ( protocol == 0 ) {
383 ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
387 if ( protocol < LDAP_VERSION3 ) {
388 /* we should rather bail out... */
389 rc = LDAP_UNWILLING_TO_PERFORM;
390 *text = "invalid protocol version";
393 if ( rc == LDAP_SUCCESS ) {
394 rc = ldap_start_tls( ld, NULL, NULL, &msgid );
397 if ( rc == LDAP_SUCCESS ) {
398 LDAPMessage *res = NULL;
401 LDAP_BACK_TV_SET( &tv );
404 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
406 rc = LDAP_UNAVAILABLE;
408 } else if ( rc == 0 ) {
409 if ( retries != LDAP_BACK_RETRY_NEVER ) {
410 ldap_pvt_thread_yield();
414 LDAP_BACK_TV_SET( &tv );
417 rc = LDAP_UNAVAILABLE;
419 } else if ( rc == LDAP_RES_EXTENDED ) {
420 struct berval *data = NULL;
422 rc = ldap_parse_extended_result( ld, res,
424 if ( rc == LDAP_SUCCESS ) {
426 rc = ldap_parse_result( ld, res, &err,
427 NULL, NULL, NULL, NULL, 1 );
428 if ( rc == LDAP_SUCCESS ) {
433 /* FIXME: in case a referral
434 * is returned, should we try
435 * using it instead of the
437 if ( rc == LDAP_SUCCESS ) {
438 rc = ldap_install_tls( ld );
440 } else if ( rc == LDAP_REFERRAL ) {
441 rc = LDAP_UNWILLING_TO_PERFORM;
442 *text = "unwilling to chase referral returned by Start TLS exop";
446 if ( data->bv_val ) {
447 ber_memfree( data->bv_val );
461 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
463 * use synchronous StartTLS
465 rc = ldap_start_tls_s( ld, NULL, NULL );
466 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
468 /* if StartTLS is requested, only attempt it if the URL
469 * is not "ldaps://"; this may occur not only in case
470 * of misconfiguration, but also when used in the chain
471 * overlay, where the "uri" can be parsed out of a referral */
477 case LDAP_SERVER_DOWN:
481 if ( LDAP_BACK_TLS_CRITICAL( &dummy ) ) {
482 *text = "could not start TLS";
486 /* in case Start TLS is not critical */
498 #endif /* HAVE_TLS */
501 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
503 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
507 int is_tls = op->o_conn->c_is_tls;
508 #endif /* HAVE_TLS */
509 time_t lc_time = (time_t)(-1);
511 assert( lcp != NULL );
513 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
514 rs->sr_err = ldap_initialize( &ld, li->li_uri );
515 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
516 if ( rs->sr_err != LDAP_SUCCESS ) {
520 if ( li->li_urllist_f ) {
521 ldap_set_urllist_proc( ld, li->li_urllist_f, li->li_urllist_p );
524 /* Set LDAP version. This will always succeed: If the client
525 * bound with a particular version, then so can we.
527 if ( li->li_version != 0 ) {
528 version = li->li_version;
530 } else if ( op->o_protocol != 0 ) {
531 version = op->o_protocol;
534 /* assume it's an internal op; set to LDAPv3 */
535 version = LDAP_VERSION3;
537 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
539 /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
540 ldap_set_option( ld, LDAP_OPT_REFERRALS,
541 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
543 if ( li->li_network_timeout > 0 ) {
546 tv.tv_sec = li->li_network_timeout;
548 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
552 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
553 rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
554 li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
555 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
556 if ( rs->sr_err != LDAP_SUCCESS ) {
557 ldap_unbind_ext( ld, NULL, NULL );
560 } else if ( li->li_idle_timeout ) {
561 /* only touch when activity actually took place... */
562 lc_time = op->o_time;
564 #endif /* HAVE_TLS */
566 if ( *lcp == NULL ) {
567 *lcp = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
568 (*lcp)->lc_flags = li->li_flags;
571 (*lcp)->lc_refcnt = 1;
572 (*lcp)->lc_binding = 1;
575 LDAP_BACK_CONN_ISTLS_SET( *lcp );
577 LDAP_BACK_CONN_ISTLS_CLEAR( *lcp );
579 if ( lc_time != (time_t)(-1) ) {
580 (*lcp)->lc_time = lc_time;
582 #endif /* HAVE_TLS */
585 if ( rs->sr_err != LDAP_SUCCESS ) {
586 rs->sr_err = slap_map_api2result( rs );
587 if ( sendok & LDAP_BACK_SENDERR ) {
588 if ( rs->sr_text == NULL ) {
589 rs->sr_text = "ldap_initialize() failed";
591 send_ldap_result( op, rs );
596 if ( li->li_conn_ttl > 0 ) {
597 (*lcp)->lc_create_time = op->o_time;
605 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
607 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
608 ldapconn_t *lc = NULL,
610 int refcnt = 1, binding = 1;
612 /* if the server is quarantined, and
613 * - the current interval did not expire yet, or
614 * - no more retries should occur,
615 * don't return the connection */
616 if ( li->li_isquarantined ) {
617 slap_retry_info_t *ri = &li->li_quarantine;
620 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
621 if ( li->li_isquarantined == LDAP_BACK_FQ_YES ) {
622 dont_retry = ( ri->ri_num[ ri->ri_idx ] == SLAP_RETRYNUM_TAIL
623 || slap_get_time() < ri->ri_last + ri->ri_interval[ ri->ri_idx ] );
625 Debug( LDAP_DEBUG_ANY,
626 "%s: ldap_back_getconn quarantine "
627 "retry block #%d try #%d.\n",
628 op->o_log_prefix, ri->ri_idx, ri->ri_count );
629 li->li_isquarantined = LDAP_BACK_FQ_RETRYING;
632 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
635 rs->sr_err = LDAP_UNAVAILABLE;
636 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
637 send_ldap_result( op, rs );
643 /* Internal searches are privileged and shared. So is root. */
644 if ( op->o_do_not_cache || be_isroot( op ) ) {
645 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
646 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
647 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
650 lc_curr.lc_local_ndn = op->o_ndn;
651 /* Explicit binds must not be shared */
652 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
653 lc_curr.lc_conn = op->o_conn;
656 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
660 /* Explicit Bind requests always get their own conn */
661 if ( !( sendok & LDAP_BACK_BINDING ) ) {
662 /* Searches for a ldapconn in the avl tree */
664 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
666 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
667 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
669 /* Don't reuse connections while they're still binding */
670 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
671 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
672 ldap_pvt_thread_yield();
676 refcnt = ++lc->lc_refcnt;
677 binding = ++lc->lc_binding;
679 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
682 /* Looks like we didn't get a bind. Open a new session... */
684 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
687 if ( sendok & LDAP_BACK_BINDING ) {
688 LDAP_BACK_CONN_BINDING_SET( lc );
690 lc->lc_conn = lc_curr.lc_conn;
691 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
693 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
694 ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
695 ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
696 LDAP_BACK_CONN_ISPRIV_SET( lc );
699 BER_BVZERO( &lc->lc_cred );
700 BER_BVZERO( &lc->lc_bound_ndn );
701 if ( !BER_BVISEMPTY( &op->o_ndn )
702 && SLAP_IS_AUTHZ_BACKEND( op ) )
704 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
709 /* if start TLS failed but it was not mandatory,
710 * check if the non-TLS connection was already
711 * in cache; in case, destroy the newly created
712 * connection and use the existing one */
713 if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
714 && !ldap_tls_inplace( lc->lc_ld ) )
718 lc_curr.lc_conn = LDAP_BACK_PCONN;
719 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
720 tmplc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree,
721 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
722 if ( tmplc != NULL ) {
723 refcnt = ++tmplc->lc_refcnt;
724 binding = ++tmplc->lc_binding;
725 ldap_back_conn_free( lc );
728 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
730 if ( tmplc != NULL ) {
734 #endif /* HAVE_TLS */
736 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
738 /* Inserts the newly created ldapconn in the avl tree */
739 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
741 assert( lc->lc_refcnt == 1 );
742 assert( lc->lc_binding == 1 );
743 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
744 ldap_back_conndn_cmp, ldap_back_conndn_dup );
746 #if PRINT_CONNTREE > 0
747 myprint( li->li_conninfo.lai_tree, "ldap_back_getconn" );
748 #endif /* PRINT_CONNTREE */
750 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
752 Debug( LDAP_DEBUG_TRACE,
753 "=>ldap_back_getconn: conn %p inserted refcnt=%u binding=%u\n",
754 (void *)lc, refcnt, binding );
756 /* Err could be -1 in case a duplicate ldapconn is inserted */
757 switch ( rs->sr_err ) {
762 if ( !( sendok & LDAP_BACK_BINDING ) ) {
763 /* duplicate: free and try to get the newly created one */
766 /* taint connection, so that it'll be freed when released */
767 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
768 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
769 ldap_back_conndnlc_cmp );
770 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
771 LDAP_BACK_CONN_TAINTED_SET( lc );
775 ldap_back_conn_free( lc );
776 rs->sr_err = LDAP_OTHER;
777 rs->sr_text = "proxy bind collision";
778 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
779 send_ldap_result( op, rs );
788 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
789 || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
793 /* let it be used, but taint/delete it so that
794 * no-one else can look it up any further */
795 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
796 (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
797 ldap_back_conndnlc_cmp );
798 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
799 LDAP_BACK_CONN_TAINTED_SET( lc );
802 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
803 char buf[ SLAP_TEXT_BUFLEN ];
805 snprintf( buf, sizeof( buf ),
806 "conn %p fetched refcnt=%u binding=%u%s",
807 (void *)lc, refcnt, binding, expiring ? " expiring" : "" );
808 Debug( LDAP_DEBUG_TRACE,
809 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
815 #endif /* HAVE_TLS */
821 ldap_back_release_conn_lock(
827 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
830 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
832 assert( lc->lc_refcnt > 0 );
833 LDAP_BACK_CONN_BINDING_CLEAR( lc );
835 if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
836 ldap_back_freeconn( op, lc, 0 );
839 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
844 ldap_back_quarantine(
848 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
850 slap_retry_info_t *ri = &li->li_quarantine;
852 ldap_pvt_thread_mutex_lock( &li->li_quarantine_mutex );
854 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
855 time_t new_last = slap_get_time();
857 switch ( li->li_isquarantined ) {
858 case LDAP_BACK_FQ_NO:
859 if ( ri->ri_last == new_last ) {
863 Debug( LDAP_DEBUG_ANY,
864 "%s: ldap_back_quarantine enter.\n",
865 op->o_log_prefix, 0, 0 );
871 case LDAP_BACK_FQ_RETRYING:
872 Debug( LDAP_DEBUG_ANY,
873 "%s: ldap_back_quarantine block #%d try #%d failed.\n",
874 op->o_log_prefix, ri->ri_idx, ri->ri_count );
877 if ( ri->ri_num[ ri->ri_idx ] != SLAP_RETRYNUM_FOREVER
878 && ri->ri_count == ri->ri_num[ ri->ri_idx ] )
889 li->li_isquarantined = LDAP_BACK_FQ_YES;
890 ri->ri_last = new_last;
892 } else if ( li->li_isquarantined != LDAP_BACK_FQ_NO ) {
893 Debug( LDAP_DEBUG_ANY,
894 "%s: ldap_back_quarantine exit.\n",
895 op->o_log_prefix, ri->ri_idx, ri->ri_count );
897 if ( li->li_quarantine_f ) {
898 (void)li->li_quarantine_f( li, li->li_quarantine_p );
903 li->li_isquarantined = LDAP_BACK_FQ_NO;
907 ldap_pvt_thread_mutex_unlock( &li->li_quarantine_mutex );
913 * Note: as the check for the value of lc->lc_bound was already here, I removed
914 * it from all the callers, and I made the function return the flag, so
915 * it can be used to simplify the check.
917 * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
920 ldap_back_dobind_int(
924 ldap_back_send_t sendok,
928 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
935 assert( retries >= 0 );
939 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
942 if ( binding == 0 ) {
943 /* check if already bound */
944 rc = isbound = LDAP_BACK_CONN_ISBOUND( lc );
948 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
953 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
954 /* if someone else is about to bind it, give up and retry */
956 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
958 ldap_pvt_thread_yield();
962 /* otherwise this thread will bind it */
963 LDAP_BACK_CONN_BINDING_SET( lc );
968 /* wait for pending operations to finish */
969 /* FIXME: may become a bottleneck! */
970 if ( lc->lc_refcnt != lc->lc_binding ) {
972 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
974 ldap_pvt_thread_yield();
979 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
983 * FIXME: we need to let clients use proxyAuthz
984 * otherwise we cannot do symmetric pools of servers;
985 * we have to live with the fact that a user can
986 * authorize itself as any ID that is allowed
987 * by the authzTo directive of the "proxyauthzdn".
990 * NOTE: current Proxy Authorization specification
991 * and implementation do not allow proxy authorization
992 * control to be provided with Bind requests
995 * if no bind took place yet, but the connection is bound
996 * and the "idassert-authcDN" (or other ID) is set,
997 * then bind as the asserting identity and explicitly
998 * add the proxyAuthz control to every operation with the
999 * dn bound to the connection as control value.
1000 * This is done also if this is the authrizing backend,
1001 * but the "override" flag is given to idassert.
1002 * It allows to use SASL bind and yet proxyAuthz users
1004 if ( op->o_conn != NULL && !op->o_do_not_cache &&
1005 ( !LDAP_BACK_CONN_ISPRIV( lc ) || BER_BVISEMPTY( &lc->lc_bound_ndn )) &&
1006 ( !isbound || ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
1008 (void)ldap_back_proxy_authz_bind( lc, op, rs, sendok );
1012 #ifdef HAVE_CYRUS_SASL
1013 if ( LDAP_BACK_CONN_ISPRIV( lc )
1014 && li->li_acl_authmethod == LDAP_AUTH_SASL )
1016 void *defaults = NULL;
1018 if ( li->li_acl_secprops != NULL ) {
1019 rc = ldap_set_option( lc->lc_ld,
1020 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops );
1022 if ( rc != LDAP_OPT_SUCCESS ) {
1023 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
1024 "(SECPROPS,\"%s\") failed!\n",
1025 li->li_acl_secprops, 0, 0 );
1030 defaults = lutil_sasl_defaults( lc->lc_ld,
1031 li->li_acl_sasl_mech.bv_val,
1032 li->li_acl_sasl_realm.bv_val,
1033 li->li_acl_authcID.bv_val,
1034 li->li_acl_passwd.bv_val,
1037 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
1038 li->li_acl_authcDN.bv_val,
1039 li->li_acl_sasl_mech.bv_val, NULL, NULL,
1040 LDAP_SASL_QUIET, lutil_sasl_interact,
1043 lutil_sasl_freedefs( defaults );
1045 rs->sr_err = slap_map_api2result( rs );
1046 if ( rs->sr_err != LDAP_SUCCESS ) {
1047 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1048 send_ldap_result( op, rs );
1051 LDAP_BACK_CONN_ISBOUND_SET( lc );
1054 if ( LDAP_BACK_QUARANTINE( li ) ) {
1055 ldap_back_quarantine( op, rs );
1060 #endif /* HAVE_CYRUS_SASL */
1063 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1064 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1065 LDAP_SASL_SIMPLE, &lc->lc_cred,
1066 NULL, NULL, &msgid );
1068 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
1069 if ( retries != LDAP_BACK_RETRY_NEVER ) {
1071 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1074 assert( lc->lc_refcnt > 0 );
1075 if ( lc->lc_refcnt == 1 ) {
1076 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1079 /* lc here must be the regular lc, reset and ready for init */
1080 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
1081 if ( rs->sr_err != LDAP_SUCCESS ) {
1088 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1091 if ( rs->sr_err == LDAP_SUCCESS ) {
1092 if ( retries > 0 ) {
1100 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1104 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1108 /* FIXME: one binding-- too many? */
1110 ldap_back_freeconn( op, lc, dolock );
1111 rs->sr_err = slap_map_api2result( rs );
1113 if ( LDAP_BACK_QUARANTINE( li ) ) {
1114 ldap_back_quarantine( op, rs );
1120 rc = ldap_back_op_result( lc, op, rs, msgid,
1121 -1, (sendok|LDAP_BACK_BINDING) );
1122 if ( rc == LDAP_SUCCESS ) {
1123 LDAP_BACK_CONN_ISBOUND_SET( lc );
1128 LDAP_BACK_CONN_BINDING_CLEAR( lc );
1129 rc = LDAP_BACK_CONN_ISBOUND( lc );
1131 ldap_back_release_conn_lock( op, rs, lc, dolock );
1138 ldap_back_dobind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1140 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1142 return ldap_back_dobind_int( lc, op, rs, sendok, li->li_nretries, 1 );
1146 * ldap_back_default_rebind
1148 * This is a callback used for chasing referrals using the same
1149 * credentials as the original user on this session.
1152 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1153 ber_int_t msgid, void *params )
1155 ldapconn_t *lc = (ldapconn_t *)params;
1158 /* ... otherwise we couldn't get here */
1159 assert( lc != NULL );
1161 if ( !ldap_tls_inplace( ld ) ) {
1162 int is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1164 const char *text = NULL;
1166 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1167 LDAP_BACK_RETRY_DEFAULT, &text );
1168 if ( rc != LDAP_SUCCESS ) {
1172 #endif /* HAVE_TLS */
1174 /* FIXME: add checks on the URL/identity? */
1176 return ldap_sasl_bind_s( ld,
1177 BER_BVISNULL( &lc->lc_cred ) ? "" : lc->lc_bound_ndn.bv_val,
1178 LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1182 * ldap_back_default_urllist
1185 ldap_back_default_urllist(
1187 LDAPURLDesc **urllist,
1191 ldapinfo_t *li = (ldapinfo_t *)params;
1192 LDAPURLDesc **urltail;
1194 if ( urllist == url ) {
1195 return LDAP_SUCCESS;
1198 for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1201 *urltail = *urllist;
1205 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1207 ch_free( li->li_uri );
1210 ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1211 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1213 return LDAP_SUCCESS;
1222 ldap_back_send_t sendok )
1224 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1226 /* default behavior */
1227 if ( LDAP_BACK_ABANDON( li ) ) {
1228 return ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1231 if ( LDAP_BACK_IGNORE( li ) ) {
1232 return LDAP_SUCCESS;
1235 if ( LDAP_BACK_CANCEL( li ) ) {
1236 /* FIXME: asynchronous? */
1237 return ldap_cancel_s( lc->lc_ld, msgid, NULL, NULL );
1246 ldap_back_op_result(
1252 ldap_back_send_t sendok )
1254 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1259 LDAPControl **ctrls = NULL;
1261 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
1264 rs->sr_matched = NULL;
1266 rs->sr_ctrls = NULL;
1268 /* if the error recorded in the reply corresponds
1269 * to a successful state, get the error from the
1270 * remote server response */
1271 if ( ERR_OK( rs->sr_err ) ) {
1274 LDAPMessage *res = NULL;
1275 time_t stoptime = (time_t)(-1);
1276 int timeout_err = op->o_protocol >= LDAP_VERSION3 ?
1277 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
1278 const char *timeout_text = "Operation timed out";
1280 /* if timeout is not specified, compute and use
1281 * the one specific to the ongoing operation */
1282 if ( timeout == (time_t)(-1) ) {
1283 slap_op_t opidx = slap_req2op( op->o_tag );
1285 if ( opidx == SLAP_OP_SEARCH ) {
1286 if ( op->ors_tlimit <= 0 ) {
1290 timeout = op->ors_tlimit;
1291 timeout_err = LDAP_TIMELIMIT_EXCEEDED;
1292 timeout_text = NULL;
1296 timeout = li->li_timeout[ opidx ];
1300 /* better than nothing :) */
1301 if ( timeout == 0 ) {
1302 if ( li->li_idle_timeout ) {
1303 timeout = li->li_idle_timeout;
1305 } else if ( li->li_conn_ttl ) {
1306 timeout = li->li_conn_ttl;
1311 stoptime = op->o_time + timeout;
1314 LDAP_BACK_TV_SET( &tv );
1317 /* if result parsing fails, note the failure reason */
1318 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1321 if ( timeout && slap_get_time() > stoptime ) {
1322 if ( sendok & LDAP_BACK_BINDING ) {
1323 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
1325 LDAP_BACK_CONN_TAINTED_SET( lc );
1328 (void)ldap_back_cancel( lc, op, rs, msgid, sendok );
1330 rs->sr_err = timeout_err;
1331 rs->sr_text = timeout_text;
1336 LDAP_BACK_TV_SET( &tv );
1337 ldap_pvt_thread_yield();
1341 ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1346 /* otherwise get the result; if it is not
1347 * LDAP_SUCCESS, record it in the reply
1348 * structure (this includes
1349 * LDAP_COMPARE_{TRUE|FALSE}) */
1351 /* only touch when activity actually took place... */
1352 if ( li->li_idle_timeout && lc ) {
1353 lc->lc_time = op->o_time;
1356 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1357 &match, &text, &refs, &ctrls, 1 );
1359 if ( rc != LDAP_SUCCESS ) {
1362 if ( refs != NULL ) {
1365 for ( i = 0; refs[ i ] != NULL; i++ )
1367 rs->sr_ref = op->o_tmpalloc( sizeof( struct berval ) * ( i + 1 ),
1369 for ( i = 0; refs[ i ] != NULL; i++ ) {
1370 ber_str2bv( refs[ i ], 0, 0, &rs->sr_ref[ i ] );
1372 BER_BVZERO( &rs->sr_ref[ i ] );
1374 if ( ctrls != NULL ) {
1375 rs->sr_ctrls = ctrls;
1380 /* if the error in the reply structure is not
1381 * LDAP_SUCCESS, try to map it from client
1382 * to server error */
1383 if ( !ERR_OK( rs->sr_err ) ) {
1384 rs->sr_err = slap_map_api2result( rs );
1386 /* internal ops ( op->o_conn == NULL )
1387 * must not reply to client */
1388 if ( op->o_conn && !op->o_do_not_cache && match ) {
1390 /* record the (massaged) matched
1391 * DN into the reply structure */
1392 rs->sr_matched = match;
1396 if ( rs->sr_err == LDAP_UNAVAILABLE ) {
1397 if ( !( sendok & LDAP_BACK_RETRYING ) ) {
1398 if ( LDAP_BACK_QUARANTINE( li ) ) {
1399 ldap_back_quarantine( op, rs );
1401 if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
1402 send_ldap_result( op, rs );
1406 } else if ( op->o_conn &&
1407 ( ( ( sendok & LDAP_BACK_SENDOK ) && ERR_OK( rs->sr_err ) )
1408 || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1410 send_ldap_result( op, rs );
1414 if ( rs->sr_matched != match ) {
1415 free( (char *)rs->sr_matched );
1417 rs->sr_matched = NULL;
1418 ldap_memfree( match );
1422 ldap_memfree( text );
1427 assert( refs != NULL );
1428 ber_memvfree( (void **)refs );
1429 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
1434 assert( rs->sr_ctrls != NULL );
1435 ldap_controls_free( ctrls );
1436 rs->sr_ctrls = NULL;
1439 return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1442 /* return true if bound, false if failed */
1444 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1447 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1449 assert( lcp != NULL );
1450 assert( *lcp != NULL );
1452 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1454 if ( (*lcp)->lc_refcnt == 1 ) {
1455 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1456 Debug( LDAP_DEBUG_ANY,
1457 "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1458 op->o_log_prefix, li->li_uri,
1459 BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1460 "" : (*lcp)->lc_bound_ndn.bv_val );
1461 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1463 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1464 (*lcp)->lc_ld = NULL;
1465 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1467 /* lc here must be the regular lc, reset and ready for init */
1468 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
1469 if ( rc != LDAP_SUCCESS ) {
1470 /* freeit, because lc_refcnt == 1 */
1471 (*lcp)->lc_refcnt = 0;
1472 (void)ldap_back_freeconn( op, *lcp, 0 );
1477 rc = ldap_back_dobind_int( *lcp, op, rs, sendok, 0, 0 );
1478 if ( rc == 0 && *lcp != NULL ) {
1479 /* freeit, because lc_refcnt == 1 */
1480 (*lcp)->lc_refcnt = 0;
1481 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1482 (void)ldap_back_freeconn( op, *lcp, 0 );
1488 Debug( LDAP_DEBUG_TRACE,
1489 "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1490 (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1492 LDAP_BACK_CONN_TAINTED_SET( *lcp );
1493 ldap_back_release_conn_lock( op, rs, *lcp, 0 );
1497 rs->sr_err = LDAP_UNAVAILABLE;
1498 rs->sr_text = "unable to retry";
1499 send_ldap_result( op, rs );
1503 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1509 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1511 ldapinfo_t *li = (ldapinfo_t *)op->o_bd->be_private;
1512 struct berval binddn = slap_empty_bv;
1513 struct berval bindcred = slap_empty_bv;
1519 /* don't proxyAuthz if protocol is not LDAPv3 */
1520 switch ( li->li_version ) {
1525 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1531 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1532 if ( sendok & LDAP_BACK_SENDERR ) {
1533 send_ldap_result( op, rs );
1535 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1539 if ( op->o_tag == LDAP_REQ_BIND ) {
1540 ndn = op->o_req_ndn;
1542 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1543 ndn = op->o_conn->c_ndn;
1550 * FIXME: we need to let clients use proxyAuthz
1551 * otherwise we cannot do symmetric pools of servers;
1552 * we have to live with the fact that a user can
1553 * authorize itself as any ID that is allowed
1554 * by the authzTo directive of the "proxyauthzdn".
1557 * NOTE: current Proxy Authorization specification
1558 * and implementation do not allow proxy authorization
1559 * control to be provided with Bind requests
1562 * if no bind took place yet, but the connection is bound
1563 * and the "proxyauthzdn" is set, then bind as
1564 * "proxyauthzdn" and explicitly add the proxyAuthz
1565 * control to every operation with the dn bound
1566 * to the connection as control value.
1569 /* bind as proxyauthzdn only if no idassert mode
1570 * is requested, or if the client's identity
1572 switch ( li->li_idassert_mode ) {
1573 case LDAP_BACK_IDASSERT_LEGACY:
1574 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1575 if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1577 binddn = li->li_idassert_authcDN;
1578 bindcred = li->li_idassert_passwd;
1585 /* NOTE: rootdn can always idassert */
1586 if ( BER_BVISNULL( &ndn ) && li->li_idassert_authz == NULL ) {
1587 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1588 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1589 if ( sendok & LDAP_BACK_SENDERR ) {
1590 send_ldap_result( op, rs );
1592 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1595 rs->sr_err = LDAP_SUCCESS;
1596 binddn = slap_empty_bv;
1597 bindcred = slap_empty_bv;
1603 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1604 struct berval authcDN;
1606 if ( BER_BVISNULL( &ndn ) ) {
1607 authcDN = slap_empty_bv;
1612 rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1613 &authcDN, &authcDN );
1614 if ( rs->sr_err != LDAP_SUCCESS ) {
1615 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1616 if ( sendok & LDAP_BACK_SENDERR ) {
1617 send_ldap_result( op, rs );
1619 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1622 rs->sr_err = LDAP_SUCCESS;
1623 binddn = slap_empty_bv;
1624 bindcred = slap_empty_bv;
1632 binddn = li->li_idassert_authcDN;
1633 bindcred = li->li_idassert_passwd;
1638 if ( dobind && li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1639 #ifdef HAVE_CYRUS_SASL
1640 void *defaults = NULL;
1641 struct berval authzID = BER_BVNULL;
1644 /* if SASL supports native authz, prepare for it */
1645 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1646 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1648 switch ( li->li_idassert_mode ) {
1649 case LDAP_BACK_IDASSERT_OTHERID:
1650 case LDAP_BACK_IDASSERT_OTHERDN:
1651 authzID = li->li_idassert_authzID;
1654 case LDAP_BACK_IDASSERT_ANONYMOUS:
1655 BER_BVSTR( &authzID, "dn:" );
1658 case LDAP_BACK_IDASSERT_SELF:
1659 if ( BER_BVISNULL( &ndn ) ) {
1660 /* connection is not authc'd, so don't idassert */
1661 BER_BVSTR( &authzID, "dn:" );
1664 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1665 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1666 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1667 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1668 ndn.bv_val, ndn.bv_len + 1 );
1677 if ( li->li_idassert_secprops != NULL ) {
1678 rs->sr_err = ldap_set_option( lc->lc_ld,
1679 LDAP_OPT_X_SASL_SECPROPS,
1680 (void *)li->li_idassert_secprops );
1682 if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1683 rs->sr_err = LDAP_OTHER;
1684 if ( sendok & LDAP_BACK_SENDERR ) {
1685 send_ldap_result( op, rs );
1687 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1692 defaults = lutil_sasl_defaults( lc->lc_ld,
1693 li->li_idassert_sasl_mech.bv_val,
1694 li->li_idassert_sasl_realm.bv_val,
1695 li->li_idassert_authcID.bv_val,
1696 li->li_idassert_passwd.bv_val,
1699 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1700 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1701 LDAP_SASL_QUIET, lutil_sasl_interact,
1704 rs->sr_err = slap_map_api2result( rs );
1705 if ( rs->sr_err != LDAP_SUCCESS ) {
1706 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1707 if ( sendok & LDAP_BACK_SENDERR ) {
1708 send_ldap_result( op, rs );
1712 LDAP_BACK_CONN_ISBOUND_SET( lc );
1715 lutil_sasl_freedefs( defaults );
1717 slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1721 #endif /* HAVE_CYRUS_SASL */
1724 switch ( li->li_idassert_authmethod ) {
1725 case LDAP_AUTH_NONE:
1726 BER_BVSTR( &binddn, "" );
1727 BER_BVSTR( &bindcred, "" );
1730 case LDAP_AUTH_SIMPLE:
1731 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1732 binddn.bv_val, LDAP_SASL_SIMPLE,
1733 &bindcred, NULL, NULL, &msgid );
1734 rc = ldap_back_op_result( lc, op, rs, msgid,
1735 -1, (sendok|LDAP_BACK_BINDING) );
1740 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1741 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1742 if ( sendok & LDAP_BACK_SENDERR ) {
1743 send_ldap_result( op, rs );
1748 if ( rc == LDAP_SUCCESS ) {
1749 /* set rebind stuff in case of successful proxyAuthz bind,
1750 * so that referral chasing is attempted using the right
1752 LDAP_BACK_CONN_ISBOUND_SET( lc );
1753 ber_bvreplace( &lc->lc_bound_ndn, &binddn );
1755 if ( LDAP_BACK_SAVECRED( li ) ) {
1756 if ( !BER_BVISNULL( &lc->lc_cred ) ) {
1757 memset( lc->lc_cred.bv_val, 0,
1758 lc->lc_cred.bv_len );
1760 ber_bvreplace( &lc->lc_cred, &bindcred );
1761 ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
1765 return LDAP_BACK_CONN_ISBOUND( lc );
1769 * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1770 * to existing server-side controls if required; if not,
1771 * the existing server-side controls are placed in *pctrls.
1772 * The caller, after using the controls in client API
1773 * operations, if ( *pctrls != op->o_ctrls ), should
1774 * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1775 * The function returns success if the control could
1776 * be added if required, or if it did nothing; in the future,
1777 * it might return some error if it failed.
1779 * if no bind took place yet, but the connection is bound
1780 * and the "proxyauthzdn" is set, then bind as "proxyauthzdn"
1781 * and explicitly add proxyAuthz the control to every operation
1782 * with the dn bound to the connection as control value.
1784 * If no server-side controls are defined for the operation,
1785 * simply add the proxyAuthz control; otherwise, if the
1786 * proxyAuthz control is not already set, add it as
1789 * FIXME: is controls order significant for security?
1790 * ANSWER: controls ordering and interoperability
1791 * must be indicated by the specs of each control; if none
1792 * is specified, the order is irrelevant.
1795 ldap_back_proxy_authz_ctrl(
1796 struct berval *bound_ndn,
1798 slap_idassert_t *si,
1801 LDAPControl ***pctrls )
1803 LDAPControl **ctrls = NULL;
1805 slap_idassert_mode_t mode;
1806 struct berval assertedID,
1811 rs->sr_err = LDAP_SUCCESS;
1813 /* don't proxyAuthz if protocol is not LDAPv3 */
1814 switch ( version ) {
1819 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1828 /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1829 * but if it is not set this test fails. We need a different
1830 * means to detect if idassert is enabled */
1831 if ( ( BER_BVISNULL( &si->si_bc.sb_authcId ) || BER_BVISEMPTY( &si->si_bc.sb_authcId ) )
1832 && ( BER_BVISNULL( &si->si_bc.sb_binddn ) || BER_BVISEMPTY( &si->si_bc.sb_binddn ) ) )
1837 if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1841 if ( op->o_tag == LDAP_REQ_BIND ) {
1842 ndn = op->o_req_ndn;
1844 } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1845 ndn = op->o_conn->c_ndn;
1851 if ( si->si_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1852 if ( op->o_proxy_authz ) {
1854 * FIXME: we do not want to perform proxyAuthz
1855 * on behalf of the client, because this would
1856 * be performed with "proxyauthzdn" privileges.
1858 * This might actually be too strict, since
1859 * the "proxyauthzdn" authzTo, and each entry's
1860 * authzFrom attributes may be crafted
1861 * to avoid unwanted proxyAuthz to take place.
1864 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1865 rs->sr_text = "proxyAuthz not allowed within namingContext";
1870 if ( !BER_BVISNULL( bound_ndn ) ) {
1874 if ( BER_BVISNULL( &ndn ) ) {
1878 if ( BER_BVISNULL( &si->si_bc.sb_binddn ) ) {
1882 } else if ( si->si_bc.sb_method == LDAP_AUTH_SASL ) {
1883 if ( ( si->si_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1885 /* already asserted in SASL via native authz */
1889 } else if ( si->si_authz && !be_isroot( op ) ) {
1891 struct berval authcDN;
1893 if ( BER_BVISNULL( &ndn ) ) {
1894 authcDN = slap_empty_bv;
1898 rc = slap_sasl_matches( op, si->si_authz,
1899 &authcDN, & authcDN );
1900 if ( rc != LDAP_SUCCESS ) {
1901 if ( si->si_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1902 /* ndn is not authorized
1903 * to use idassert */
1910 if ( op->o_proxy_authz ) {
1913 * 1) ignore the already set proxyAuthz control
1914 * 2) leave it in place, and don't set ours
1916 * 4) reject the operation
1918 * option (4) is very drastic
1919 * option (3) will make the remote server reject
1920 * the operation, thus being equivalent to (4)
1921 * option (2) will likely break the idassert
1922 * assumptions, so we cannot accept it;
1923 * option (1) means that we are contradicting
1924 * the client's reques.
1926 * I think (4) is the only correct choice.
1928 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1929 rs->sr_text = "proxyAuthz not allowed within namingContext";
1932 if ( op->o_is_auth_check ) {
1933 mode = LDAP_BACK_IDASSERT_NOASSERT;
1940 case LDAP_BACK_IDASSERT_SELF:
1941 if ( BER_BVISNULL( &ndn ) ) {
1947 case LDAP_BACK_IDASSERT_LEGACY:
1948 /* original behavior:
1949 * assert the client's identity */
1950 if ( BER_BVISNULL( &ndn ) ) {
1951 assertedID = slap_empty_bv;
1957 case LDAP_BACK_IDASSERT_ANONYMOUS:
1958 /* assert "anonymous" */
1959 assertedID = slap_empty_bv;
1962 case LDAP_BACK_IDASSERT_NOASSERT:
1963 /* don't assert; bind as proxyauthzdn */
1966 case LDAP_BACK_IDASSERT_OTHERID:
1967 case LDAP_BACK_IDASSERT_OTHERDN:
1968 /* assert idassert DN */
1969 assertedID = si->si_bc.sb_authzId;
1976 if ( BER_BVISNULL( &assertedID ) ) {
1977 assertedID = slap_empty_bv;
1980 /* don't idassert the bound DN (ITS#4497) */
1981 if ( dn_match( &assertedID, bound_ndn ) ) {
1985 if ( op->o_ctrls ) {
1986 for ( i = 0; op->o_ctrls[ i ]; i++ )
1987 /* just count ctrls */ ;
1990 ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
1992 ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
1994 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1995 ctrls[ 0 ]->ldctl_iscritical = 1;
1997 switch ( si->si_mode ) {
1998 /* already in u:ID or dn:DN form */
1999 case LDAP_BACK_IDASSERT_OTHERID:
2000 case LDAP_BACK_IDASSERT_OTHERDN:
2001 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
2004 /* needs the dn: prefix */
2006 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
2007 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
2009 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
2010 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
2011 assertedID.bv_val, assertedID.bv_len + 1 );
2015 /* Older versions of <draft-weltman-ldapv3-proxy> required
2016 * to encode the value of the authzID (and called it proxyDN);
2017 * this hack provides compatibility with those DSAs that
2018 * implement it this way */
2019 if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
2020 struct berval authzID = ctrls[ 0 ]->ldctl_value;
2021 BerElementBuffer berbuf;
2022 BerElement *ber = (BerElement *)&berbuf;
2025 ber_init2( ber, 0, LBER_USE_DER );
2026 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2028 tag = ber_printf( ber, "O", &authzID );
2029 if ( tag == LBER_ERROR ) {
2030 rs->sr_err = LDAP_OTHER;
2034 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
2035 rs->sr_err = LDAP_OTHER;
2040 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2041 ber_free_buf( ber );
2043 if ( rs->sr_err != LDAP_SUCCESS ) {
2044 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2049 } else if ( si->si_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
2050 struct berval authzID = ctrls[ 0 ]->ldctl_value,
2052 BerElementBuffer berbuf;
2053 BerElement *ber = (BerElement *)&berbuf;
2056 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
2057 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
2058 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2060 rs->sr_err = LDAP_PROTOCOL_ERROR;
2065 tmp.bv_val += STRLENOF( "dn:" );
2066 tmp.bv_len -= STRLENOF( "dn:" );
2068 ber_init2( ber, 0, LBER_USE_DER );
2069 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2071 /* apparently, Mozilla API encodes this
2072 * as "SEQUENCE { LDAPDN }" */
2073 tag = ber_printf( ber, "{O}", &tmp );
2074 if ( tag == LBER_ERROR ) {
2075 rs->sr_err = LDAP_OTHER;
2079 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
2080 rs->sr_err = LDAP_OTHER;
2085 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
2086 ber_free_buf( ber );
2088 if ( rs->sr_err != LDAP_SUCCESS ) {
2089 op->o_tmpfree( ctrls, op->o_tmpmemctx );
2094 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
2097 if ( op->o_ctrls ) {
2098 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
2099 ctrls[ i + 1 ] = op->o_ctrls[ i ];
2102 ctrls[ i + 1 ] = NULL;
2105 if ( ctrls == NULL ) {
2106 ctrls = op->o_ctrls;
2115 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
2117 LDAPControl **ctrls = *pctrls;
2119 /* we assume that the first control is the proxyAuthz
2120 * added by back-ldap, so it's the only one we explicitly
2122 if ( ctrls && ctrls != op->o_ctrls ) {
2123 assert( ctrls[ 0 ] != NULL );
2125 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
2126 op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
2129 op->o_tmpfree( ctrls, op->o_tmpmemctx );