2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2005 The OpenLDAP Foundation.
5 * Portions Copyright 2001-2003 Pierangelo Masarati.
6 * Portions Copyright 1999-2003 Howard Chu.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by the Howard Chu for inclusion
19 * in OpenLDAP Software and subsequently enhanced by Pierangelo
27 #include <ac/socket.h>
28 #include <ac/string.h>
33 #include "../back-ldap/back-ldap.h"
34 #include "back-meta.h"
37 * Set PRINT_CONNTREE larger than 0 to dump the connection tree (debug only)
39 #define PRINT_CONNTREE 0
44 * compares two struct metaconn based on the value of the conn pointer;
52 metaconn_t *mc1 = ( metaconn_t * )c1;
53 metaconn_t *mc2 = ( metaconn_t * )c2;
55 return SLAP_PTRCMP( mc1->mc_conn, mc2->mc_conn );
61 * returns -1 in case a duplicate struct metaconn has been inserted;
69 metaconn_t *mc1 = ( metaconn_t * )c1;
70 metaconn_t *mc2 = ( metaconn_t * )c2;
72 return( ( mc1->mc_conn == mc2->mc_conn ) ? -1 : 0 );
76 * Debug stuff (got it from libavl)
78 #if PRINT_CONNTREE > 0
80 ravl_print( Avlnode *root, int depth )
88 ravl_print( root->avl_right, depth + 1 );
90 for ( i = 0; i < depth; i++ ) {
94 printf( "c(%d) %d\n", ( ( metaconn_t * )root->avl_data )->mc_conn->c_connid, root->avl_bf );
96 ravl_print( root->avl_left, depth + 1 );
100 myprint( Avlnode *root )
102 printf( "********\n" );
105 printf( "\tNULL\n" );
107 ravl_print( root, 0 );
110 printf( "********\n" );
112 #endif /* PRINT_CONNTREE */
120 * Allocates a connection structure, making room for all the referenced targets
126 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
128 int ntargets = mi->mi_ntargets;
130 assert( ntargets > 0 );
132 /* malloc once only; leave an extra one for one-past-end */
133 mc = ( metaconn_t * )ch_malloc( sizeof( metaconn_t )
134 + sizeof( metasingleconn_t ) * ( ntargets + 1 ) );
139 mc->mc_conns = ( metasingleconn_t * )&mc[ 1 ];
141 /* FIXME: needed by META_LAST() */
142 mc->mc_conns[ ntargets ].msc_candidate = META_LAST_CONN;
144 for ( ; ntargets-- > 0; ) {
145 mc->mc_conns[ ntargets ].msc_ld = NULL;
146 BER_BVZERO( &mc->mc_conns[ ntargets ].msc_bound_ndn );
147 BER_BVZERO( &mc->mc_conns[ ntargets ].msc_cred );
148 mc->mc_conns[ ntargets ].msc_bound = META_UNBOUND;
149 mc->mc_conns[ ntargets ].msc_info = mi;
152 mc->mc_auth_target = META_BOUND_NONE;
153 ldap_pvt_thread_mutex_init( &mc->mc_mutex );
159 * meta_back_conn_free
171 ldap_pvt_thread_mutex_destroy( &mc->mc_mutex );
177 * meta_back_init_one_conn
179 * Initializes one connection
182 meta_back_init_one_conn(
186 metasingleconn_t *msc,
187 ldap_back_send_t sendok )
189 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
196 if ( msc->msc_ld != NULL ) {
197 rs->sr_err = LDAP_SUCCESS;
202 * Attempts to initialize the connection to the target ds
204 rs->sr_err = ldap_initialize( &msc->msc_ld, mt->mt_uri );
205 if ( rs->sr_err != LDAP_SUCCESS ) {
210 * Set LDAP version. This will always succeed: If the client
211 * bound with a particular version, then so can we.
213 vers = op->o_conn->c_protocol;
214 ldap_set_option( msc->msc_ld, LDAP_OPT_PROTOCOL_VERSION, &vers );
216 /* automatically chase referrals ("chase-referrals"/"dont-chase-referrals" statement) */
217 if ( LDAP_BACK_CHASE_REFERRALS( mi ) ) {
218 ldap_set_option( msc->msc_ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
222 /* start TLS ("start-tls"/"try-start-tls" statements) */
223 if ( ( LDAP_BACK_USE_TLS( mi ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( mi ) ) )
224 && !ldap_is_ldaps_url( mt->mt_uri ) )
226 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
228 * use asynchronous StartTLS
229 * in case, chase referral (not implemented yet)
233 rs->sr_err = ldap_start_tls( msc->msc_ld, NULL, NULL, &msgid );
234 if ( rs->sr_err == LDAP_SUCCESS ) {
235 LDAPMessage *res = NULL;
236 int rc, nretries = mt->mt_nretries;
237 struct timeval tv = { 0, 0 };
240 rc = ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
242 rs->sr_err = LDAP_OTHER;
244 } else if ( rc == 0 ) {
245 if ( nretries != 0 ) {
246 if ( nretries > 0 ) {
253 rs->sr_err = LDAP_OTHER;
255 } else if ( rc == LDAP_RES_EXTENDED ) {
256 struct berval *data = NULL;
258 rs->sr_err = ldap_parse_extended_result( msc->msc_ld, res,
260 if ( rs->sr_err == LDAP_SUCCESS ) {
261 rs->sr_err = ldap_result2error( msc->msc_ld, res, 1 );
264 /* FIXME: in case a referral
265 * is returned, should we try
266 * using it instead of the
268 if ( rs->sr_err == LDAP_SUCCESS ) {
269 ldap_install_tls( msc->msc_ld );
271 } else if ( rs->sr_err == LDAP_REFERRAL ) {
272 rs->sr_err = LDAP_OTHER;
273 rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
277 if ( data->bv_val ) {
278 ber_memfree( data->bv_val );
285 rs->sr_err = LDAP_OTHER;
292 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
294 * use synchronous StartTLS
296 rs->sr_err = ldap_start_tls_s( msc->msc_ld, NULL, NULL );
297 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
299 /* if StartTLS is requested, only attempt it if the URL
300 * is not "ldaps://"; this may occur not only in case
301 * of misconfiguration, but also when used in the chain
302 * overlay, where the "uri" can be parsed out of a referral */
303 if ( rs->sr_err == LDAP_SERVER_DOWN
304 || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( mi ) ) )
306 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
310 #endif /* HAVE_TLS */
313 * Set the network timeout if set
315 if ( mi->mi_network_timeout != 0 ) {
316 struct timeval network_timeout;
318 network_timeout.tv_usec = 0;
319 network_timeout.tv_sec = mi->mi_network_timeout;
321 ldap_set_option( msc->msc_ld, LDAP_OPT_NETWORK_TIMEOUT,
322 (void *)&network_timeout );
326 * Sets a cookie for the rewrite session
328 ( void )rewrite_session_init( mt->mt_rwmap.rwm_rw, op->o_conn );
331 * If the connection DN is not null, an attempt to rewrite it is made
333 if ( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
335 dc.conn = op->o_conn;
340 * Rewrite the bind dn if needed
342 if ( ldap_back_dn_massage( &dc, &op->o_conn->c_dn,
343 &msc->msc_bound_ndn ) )
348 /* copy the DN idf needed */
349 if ( msc->msc_bound_ndn.bv_val == op->o_conn->c_dn.bv_val ) {
350 ber_dupbv( &msc->msc_bound_ndn, &op->o_conn->c_dn );
353 assert( !BER_BVISNULL( &msc->msc_bound_ndn ) );
356 ber_str2bv( "", 0, 1, &msc->msc_bound_ndn );
359 msc->msc_bound = META_UNBOUND;
362 if ( rs->sr_err != LDAP_SUCCESS ) {
363 rs->sr_err = slap_map_api2result( rs );
364 if ( sendok & LDAP_BACK_SENDERR ) {
365 send_ldap_result( op, rs );
376 * Retries one connection
384 ldap_back_send_t sendok )
386 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
387 metatarget_t *mt = &mi->mi_targets[ candidate ];
389 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
391 ldap_pvt_thread_mutex_lock( &mc->mc_mutex );
393 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
397 /* mc here must be the regular mc, reset and ready for init */
398 rc = meta_back_init_one_conn( op, rs, mt, msc, sendok );
400 if ( rc == LDAP_SUCCESS ) {
401 rc = meta_back_single_dobind( op, rs, mc, candidate,
402 sendok, mt->mt_nretries );
405 ldap_pvt_thread_mutex_unlock( &mc->mc_mutex );
407 return rc == LDAP_SUCCESS ? 1 : 0;
411 * callback for unique candidate selection
414 meta_back_conn_cb( Operation *op, SlapReply *rs )
416 assert( op->o_tag == LDAP_REQ_SEARCH );
418 switch ( rs->sr_type ) {
420 ((int *)op->o_callback->sc_private)[0] = (int)op->o_private;
436 meta_back_get_candidate(
441 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
445 * tries to get a unique candidate
446 * (takes care of default target)
448 candidate = meta_back_select_unique_candidate( mi, ndn );
451 * if any is found, inits the connection
453 if ( candidate == META_TARGET_NONE ) {
454 rs->sr_err = LDAP_NO_SUCH_OBJECT;
455 rs->sr_text = "no suitable candidate target found";
457 } else if ( candidate == META_TARGET_MULTIPLE ) {
460 SlapReply rs2 = { 0 };
461 slap_callback cb2 = { 0 };
464 /* try to get a unique match for the request ndn
465 * among the multiple candidates available */
466 op2.o_tag = LDAP_REQ_SEARCH;
468 op2.o_req_ndn = *ndn;
469 op2.ors_scope = LDAP_SCOPE_BASE;
470 op2.ors_deref = LDAP_DEREF_NEVER;
471 op2.ors_attrs = slap_anlist_no_attrs;
472 op2.ors_attrsonly = 0;
473 op2.ors_limit = NULL;
475 op2.ors_tlimit = SLAP_NO_LIMIT;
477 f.f_choice = LDAP_FILTER_PRESENT;
478 f.f_desc = slap_schema.si_ad_objectClass;
480 BER_BVSTR( &op2.ors_filterstr, "(objectClass=*)" );
482 op2.o_callback = &cb2;
483 cb2.sc_response = meta_back_conn_cb;
484 cb2.sc_private = (void *)&candidate;
486 rc = op->o_bd->be_search( &op2, &rs2 );
488 switch ( rs2.sr_err ) {
491 rs->sr_err = rs2.sr_err;
494 case LDAP_SIZELIMIT_EXCEEDED:
495 /* if multiple candidates can serve the operation,
496 * and a default target is defined, and it is
497 * a candidate, try using it (FIXME: YMMV) */
498 if ( mi->mi_defaulttarget != META_DEFAULT_TARGET_NONE
499 && meta_back_is_candidate( &mi->mi_targets[ mi->mi_defaulttarget ].mt_nsuffix,
500 ndn, op->o_tag == LDAP_REQ_SEARCH ? op->ors_scope : LDAP_SCOPE_BASE ) )
502 candidate = mi->mi_defaulttarget;
503 rs->sr_err = LDAP_SUCCESS;
507 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
508 rs->sr_text = "cannot select unique candidate target";
518 meta_back_candidate_keyfree(
522 ber_memfree_x( data, NULL );
526 meta_back_candidates_get( Operation *op )
528 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
531 if ( op->o_threadctx ) {
532 ldap_pvt_thread_pool_getkey( op->o_threadctx,
533 meta_back_candidate_keyfree, &data, NULL );
535 data = (void *)mi->mi_candidates;
538 if ( data == NULL ) {
539 data = ber_memalloc( sizeof( SlapReply ) * mi->mi_ntargets );
540 if ( op->o_threadctx ) {
541 ldap_pvt_thread_pool_setkey( op->o_threadctx,
542 meta_back_candidate_keyfree, data,
543 meta_back_candidate_keyfree );
546 mi->mi_candidates = (SlapReply *)data;
550 return (SlapReply *)data;
556 * Prepares the connection structure
560 * - determine what DN is being requested:
562 * op requires candidate checks
564 * add unique parent of o_req_ndn
565 * bind unique^*[/all] o_req_ndn [no check]
566 * compare unique^+ o_req_ndn
567 * delete unique o_req_ndn
568 * modify unique o_req_ndn
569 * search any o_req_ndn
570 * modrdn unique[, unique] o_req_ndn[, orr_nnewSup]
572 * - for ops that require the candidate to be unique, in case of multiple
573 * occurrences an internal search with sizeLimit=1 is performed
574 * if a unique candidate can actually be determined. If none is found,
575 * the operation aborts; if multiple are found, the default target
576 * is used if defined and candidate; otherwise the operation aborts.
578 * *^note: actually, the bind operation is handled much like a search;
579 * i.e. the bind is broadcast to all candidate targets.
581 * +^note: actually, the compare operation is handled much like a search;
582 * i.e. the compare is broadcast to all candidate targets, while checking
583 * that exactly none (noSuchObject) or one (TRUE/FALSE/UNDEFINED) is
591 ldap_back_send_t sendok )
593 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
594 metaconn_t *mc, mc_curr;
595 int cached = META_TARGET_NONE,
596 i = META_TARGET_NONE,
600 meta_op_type op_type = META_OP_REQUIRE_SINGLE;
603 struct berval ndn = op->o_req_ndn,
606 SlapReply *candidates = meta_back_candidates_get( op );
608 /* Searches for a metaconn in the avl tree */
609 mc_curr.mc_conn = op->o_conn;
610 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
611 mc = (metaconn_t *)avl_find( mi->mi_conntree,
612 (caddr_t)&mc_curr, meta_back_conn_cmp );
613 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
615 switch ( op->o_tag ) {
617 /* if we go to selection, the entry must not exist,
618 * and we must be able to resolve the parent */
620 dnParent( &ndn, &pndn );
623 case LDAP_REQ_MODRDN:
624 /* if nnewSuperior is not NULL, it must resolve
625 * to the same candidate as the req_ndn */
626 if ( op->orr_nnewSup ) {
632 /* if bound as rootdn, the backend must bind to all targets
633 * with the administrative identity */
634 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
635 op_type = META_OP_REQUIRE_ALL;
639 case LDAP_REQ_DELETE:
640 case LDAP_REQ_MODIFY:
641 /* just a unique candidate */
644 case LDAP_REQ_COMPARE:
645 case LDAP_REQ_SEARCH:
646 /* allow multiple candidates for the searchBase */
647 op_type = META_OP_ALLOW_MULTIPLE;
651 /* right now, just break (exop?) */
656 * require all connections ...
658 if ( op_type == META_OP_REQUIRE_ALL ) {
660 /* Looks like we didn't get a bind. Open a new session... */
662 mc = metaconn_alloc( op );
663 mc->mc_conn = op->o_conn;
667 for ( i = 0; i < mi->mi_ntargets; i++ ) {
670 * The target is activated; if needed, it is
673 int lerr = meta_back_init_one_conn( op, rs, &mi->mi_targets[ i ],
674 &mc->mc_conns[ i ], sendok );
675 if ( lerr == LDAP_SUCCESS ) {
676 candidates[ i ].sr_tag = META_CANDIDATE;
681 * FIXME: in case one target cannot
682 * be init'd, should the other ones
685 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
694 * looks in cache, if any
696 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
697 cached = i = meta_dncache_get_target( &mi->mi_cache, &op->o_req_ndn );
700 if ( op_type == META_OP_REQUIRE_SINGLE ) {
703 for ( j = 0; j < mi->mi_ntargets; j++ ) {
704 candidates[ j ].sr_tag = META_NOT_CANDIDATE;
708 * tries to get a unique candidate
709 * (takes care of default target)
711 if ( i == META_TARGET_NONE ) {
712 i = meta_back_get_candidate( op, rs, &ndn );
714 if ( rs->sr_err == LDAP_NO_SUCH_OBJECT && parent ) {
715 i = meta_back_get_candidate( op, rs, &pndn );
718 if ( rs->sr_err != LDAP_SUCCESS ) {
719 if ( sendok & LDAP_BACK_SENDERR ) {
720 send_ldap_result( op, rs );
727 if ( newparent && meta_back_get_candidate( op, rs, op->orr_nnewSup ) != i )
729 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
730 rs->sr_text = "cross-target rename not supported";
731 if ( sendok & LDAP_BACK_SENDERR ) {
732 send_ldap_result( op, rs );
738 Debug( LDAP_DEBUG_CACHE,
739 "==>meta_back_getconn: got target %d for ndn=\"%s\" from cache\n",
740 i, op->o_req_ndn.bv_val, 0 );
742 /* Retries searching for a metaconn in the avl tree */
743 mc_curr.mc_conn = op->o_conn;
744 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
745 mc = (metaconn_t *)avl_find( mi->mi_conntree,
746 (caddr_t)&mc_curr, meta_back_conn_cmp );
747 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
749 /* Looks like we didn't get a bind. Open a new session... */
751 mc = metaconn_alloc( op );
752 mc->mc_conn = op->o_conn;
757 * Clear all other candidates
759 ( void )meta_clear_unused_candidates( op, i );
762 * The target is activated; if needed, it is
763 * also init'd. In case of error, meta_back_init_one_conn
764 * sends the appropriate result.
766 err = meta_back_init_one_conn( op, rs, &mi->mi_targets[ i ],
767 &mc->mc_conns[ i ], sendok );
768 if ( err == LDAP_SUCCESS ) {
769 candidates[ i ].sr_tag = META_CANDIDATE;
774 * FIXME: in case one target cannot
775 * be init'd, should the other ones
778 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
780 ( void )meta_clear_one_candidate( &mc->mc_conns[ i ] );
781 meta_back_conn_free( mc );
791 * if no unique candidate ...
797 /* Looks like we didn't get a bind. Open a new session... */
799 mc = metaconn_alloc( op );
800 mc->mc_conn = op->o_conn;
804 for ( i = 0; i < mi->mi_ntargets; i++ ) {
806 || meta_back_is_candidate( &mi->mi_targets[ i ].mt_nsuffix,
807 &op->o_req_ndn, LDAP_SCOPE_SUBTREE ) )
811 * The target is activated; if needed, it is
814 int lerr = meta_back_init_one_conn( op, rs,
815 &mi->mi_targets[ i ],
816 &mc->mc_conns[ i ], sendok );
817 if ( lerr == LDAP_SUCCESS ) {
818 candidates[ i ].sr_tag = META_CANDIDATE;
824 * FIXME: in case one target cannot
825 * be init'd, should the other ones
829 ( void )meta_clear_one_candidate( &mc->mc_conns[ i ] );
831 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
834 Debug( LDAP_DEBUG_ANY, "%s: meta_back_init_one_conn(%d) failed: %d\n",
835 op->o_log_prefix, i, lerr );
842 ( void )meta_clear_one_candidate( &mc->mc_conns[ i ] );
844 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
848 if ( ncandidates == 0 ) {
850 meta_back_conn_free( mc );
853 rs->sr_err = LDAP_NO_SUCH_OBJECT;
854 rs->sr_text = "Unable to select valid candidates";
856 if ( sendok & LDAP_BACK_SENDERR ) {
857 send_ldap_result( op, rs );
866 /* clear out meta_back_init_one_conn non-fatal errors */
867 rs->sr_err = LDAP_SUCCESS;
873 * Inserts the newly created metaconn in the avl tree
875 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
876 err = avl_insert( &mi->mi_conntree, ( caddr_t )mc,
877 meta_back_conn_cmp, meta_back_conn_dup );
879 #if PRINT_CONNTREE > 0
880 myprint( mi->mi_conntree );
881 #endif /* PRINT_CONNTREE */
883 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
886 * Err could be -1 in case a duplicate metaconn is inserted
889 Debug( LDAP_DEBUG_TRACE,
890 "%s meta_back_getconn: conn %ld inserted\n",
891 op->o_log_prefix, mc->mc_conn->c_connid, 0 );
894 Debug( LDAP_DEBUG_TRACE,
895 "%s meta_back_getconn: conn %ld insert failed\n",
896 op->o_log_prefix, mc->mc_conn->c_connid, 0 );
898 rs->sr_err = LDAP_OTHER;
899 rs->sr_text = "Internal server error";
900 meta_back_conn_free( mc );
901 if ( sendok & LDAP_BACK_SENDERR ) {
902 send_ldap_result( op, rs );
909 Debug( LDAP_DEBUG_TRACE,
910 "%s meta_back_getconn: conn %ld fetched\n",
911 op->o_log_prefix, mc->mc_conn->c_connid, 0 );