Home | History | Annotate | Line # | Download | only in back-asyncmeta
init.c revision 1.1.1.2
      1 /*	$NetBSD: init.c,v 1.1.1.2 2025/09/05 21:09:48 christos Exp $	*/
      2 
      3 /* init.c - initialization of a back-asyncmeta database */
      4 /* $OpenLDAP$ */
      5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      6  *
      7  * Copyright 2016-2024 The OpenLDAP Foundation.
      8  * Portions Copyright 2016 Symas Corporation.
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted only as authorized by the OpenLDAP
     13  * Public License.
     14  *
     15  * A copy of this license is available in the file LICENSE in the
     16  * top-level directory of the distribution or, alternatively, at
     17  * <http://www.OpenLDAP.org/license.html>.
     18  */
     19 
     20 /* ACKNOWLEDGEMENTS:
     21  * This work was developed by Symas Corporation
     22  * based on back-meta module for inclusion in OpenLDAP Software.
     23  * This work was sponsored by Ericsson. */
     24 
     25 #include <sys/cdefs.h>
     26 __RCSID("$NetBSD: init.c,v 1.1.1.2 2025/09/05 21:09:48 christos Exp $");
     27 
     28 #include "portable.h"
     29 
     30 #include <stdio.h>
     31 
     32 #include <ac/string.h>
     33 #include <ac/socket.h>
     34 
     35 #include "slap.h"
     36 #include "slap-config.h"
     37 #include "../back-ldap/back-ldap.h"
     38 #include "back-asyncmeta.h"
     39 
     40 int asyncmeta_debug;
     41 
     42 int
     43 asyncmeta_back_open(
     44 	BackendInfo	*bi )
     45 {
     46 	/* FIXME: need to remove the pagedResults, and likely more... */
     47 	bi->bi_controls = slap_known_controls;
     48 
     49 	return 0;
     50 }
     51 
     52 int
     53 asyncmeta_back_initialize(
     54 	BackendInfo	*bi )
     55 {
     56 	int rc;
     57 	struct berval debugbv = BER_BVC("asyncmeta");
     58 
     59 	rc = slap_loglevel_get( &debugbv, &asyncmeta_debug );
     60 	if ( rc ) {
     61 		return rc;
     62 	}
     63 
     64 	bi->bi_flags =
     65 #if 0
     66 	/* this is not (yet) set essentially because back-meta does not
     67 	 * directly support extended operations... */
     68 #ifdef LDAP_DYNAMIC_OBJECTS
     69 		/* this is set because all the support a proxy has to provide
     70 		 * is the capability to forward the refresh exop, and to
     71 		 * pass thru entries that contain the dynamicObject class
     72 		 * and the entryTtl attribute */
     73 		SLAP_BFLAG_DYNAMIC |
     74 #endif /* LDAP_DYNAMIC_OBJECTS */
     75 #endif
     76 
     77 		/* back-meta recognizes RFC4525 increment;
     78 		 * let the remote server complain, if needed (ITS#5912) */
     79 		SLAP_BFLAG_INCREMENT;
     80 
     81 	bi->bi_open = asyncmeta_back_open;
     82 	bi->bi_config = 0;
     83 	bi->bi_close = 0;
     84 	bi->bi_destroy = 0;
     85 
     86 	bi->bi_db_init = asyncmeta_back_db_init;
     87 	bi->bi_db_config = config_generic_wrapper;
     88 	bi->bi_db_open = asyncmeta_back_db_open;
     89 	bi->bi_db_close = asyncmeta_back_db_close;
     90 	bi->bi_db_destroy = asyncmeta_back_db_destroy;
     91 
     92 	bi->bi_op_bind = asyncmeta_back_bind;
     93 	bi->bi_op_unbind = 0;
     94 	bi->bi_op_search = asyncmeta_back_search;
     95 	bi->bi_op_compare = asyncmeta_back_compare;
     96 	bi->bi_op_modify = asyncmeta_back_modify;
     97 	bi->bi_op_modrdn = asyncmeta_back_modrdn;
     98 	bi->bi_op_add = asyncmeta_back_add;
     99 	bi->bi_op_delete = asyncmeta_back_delete;
    100 	bi->bi_op_abandon = 0;
    101 
    102 	bi->bi_extended = 0;
    103 
    104 	bi->bi_chk_referrals = 0;
    105 
    106 	bi->bi_connection_init = 0;
    107 	bi->bi_connection_destroy =  0 /* asyncmeta_back_conn_destroy */;
    108 
    109 	return asyncmeta_back_init_cf( bi );
    110 }
    111 
    112 int
    113 asyncmeta_back_db_init(
    114 	Backend		*be,
    115 	ConfigReply	*cr)
    116 {
    117 	a_metainfo_t	*mi;
    118 	int		i;
    119 	BackendInfo	*bi;
    120 
    121 	bi = backend_info( "ldap" );
    122 	if ( !bi || !bi->bi_extra ) {
    123 		Debug( LDAP_DEBUG_ANY,
    124 			"asyncmeta_back_db_init: needs back-ldap\n" );
    125 		return 1;
    126 	}
    127 
    128 	mi = ch_calloc( 1, sizeof( a_metainfo_t ) );
    129 	if ( mi == NULL ) {
    130 		return -1;
    131 	}
    132 
    133 	/* set default flags */
    134 	mi->mi_flags =
    135 		META_BACK_F_DEFER_ROOTDN_BIND
    136 		| META_BACK_F_PROXYAUTHZ_ALWAYS
    137 		| META_BACK_F_PROXYAUTHZ_ANON
    138 		| META_BACK_F_PROXYAUTHZ_NOANON;
    139 
    140 	/*
    141 	 * At present the default is no default target;
    142 	 * this may change
    143 	 */
    144 	mi->mi_defaulttarget = META_DEFAULT_TARGET_NONE;
    145 	mi->mi_bind_timeout.tv_sec = 0;
    146 	mi->mi_bind_timeout.tv_usec = META_BIND_TIMEOUT;
    147 
    148 	mi->mi_rebind_f = asyncmeta_back_default_rebind;
    149 	mi->mi_urllist_f = asyncmeta_back_default_urllist;
    150 
    151 	ldap_pvt_thread_mutex_init( &mi->mi_cache.mutex );
    152 
    153 	/* safe default */
    154 	mi->mi_nretries = META_RETRY_DEFAULT;
    155 	mi->mi_version = LDAP_VERSION3;
    156 
    157 	for ( i = 0; i < SLAP_OP_LAST; i++ ) {
    158 		mi->mi_timeout[ i ] = META_BACK_CFG_DEFAULT_OPS_TIMEOUT;
    159 	}
    160 
    161 	for ( i = LDAP_BACK_PCONN_FIRST; i < LDAP_BACK_PCONN_LAST; i++ ) {
    162 		mi->mi_conn_priv[ i ].mic_num = 0;
    163 		LDAP_TAILQ_INIT( &mi->mi_conn_priv[ i ].mic_priv );
    164 	}
    165 	mi->mi_conn_priv_max = LDAP_BACK_CONN_PRIV_DEFAULT;
    166 
    167 	mi->mi_ldap_extra = (ldap_extra_t *)bi->bi_extra;
    168 	ldap_pvt_thread_mutex_init( &mi->mi_mc_mutex);
    169 
    170 	be->be_private = mi;
    171 	be->be_cf_ocs = be->bd_info->bi_cf_ocs;
    172 
    173 	return 0;
    174 }
    175 
    176 int
    177 asyncmeta_target_finish(
    178 	a_metainfo_t *mi,
    179 	a_metatarget_t *mt,
    180 	const char *log,
    181 	char *msg,
    182 	size_t msize
    183 )
    184 {
    185 	slap_bindconf	sb = { BER_BVNULL };
    186 	int rc;
    187 
    188 	ber_str2bv( mt->mt_uri, 0, 0, &sb.sb_uri );
    189 	sb.sb_version = mt->mt_version;
    190 	sb.sb_method = LDAP_AUTH_SIMPLE;
    191 	BER_BVSTR( &sb.sb_binddn, "" );
    192 
    193 	if ( META_BACK_TGT_T_F_DISCOVER( mt ) ) {
    194 		rc = slap_discover_feature( &sb,
    195 				slap_schema.si_ad_supportedFeatures->ad_cname.bv_val,
    196 				LDAP_FEATURE_ABSOLUTE_FILTERS );
    197 		if ( rc == LDAP_COMPARE_TRUE ) {
    198 			mt->mt_flags |= LDAP_BACK_F_T_F;
    199 		}
    200 	}
    201 
    202 	if ( META_BACK_TGT_CANCEL_DISCOVER( mt ) ) {
    203 		rc = slap_discover_feature( &sb,
    204 				slap_schema.si_ad_supportedExtension->ad_cname.bv_val,
    205 				LDAP_EXOP_CANCEL );
    206 		if ( rc == LDAP_COMPARE_TRUE ) {
    207 			mt->mt_flags |= LDAP_BACK_F_CANCEL_EXOP;
    208 		}
    209 	}
    210 
    211 	if ( !( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE )
    212 		|| mt->mt_idassert_authz != NULL )
    213 	{
    214 		mi->mi_flags &= ~META_BACK_F_PROXYAUTHZ_ALWAYS;
    215 	}
    216 
    217 	if ( ( mt->mt_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL )
    218 		&& !( mt->mt_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) )
    219 	{
    220 		Debug(LDAP_DEBUG_ANY,
    221 		      "%s: inconsistent idassert configuration " "(likely authz=\"*\" used with \"non-prescriptive\" flag) (target %s)\n",
    222 		      log, mt->mt_uri );
    223 		return 1;
    224 	}
    225 
    226 	if ( !( mt->mt_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL ) )
    227 	{
    228 		mi->mi_flags &= ~META_BACK_F_PROXYAUTHZ_ANON;
    229 	}
    230 
    231 	if ( ( mt->mt_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) )
    232 	{
    233 		mi->mi_flags &= ~META_BACK_F_PROXYAUTHZ_NOANON;
    234 	}
    235 
    236 	return 0;
    237 }
    238 
    239 int
    240 asyncmeta_back_db_open(
    241 	Backend		*be,
    242 	ConfigReply	*cr )
    243 {
    244 	a_metainfo_t	*mi = (a_metainfo_t *)be->be_private;
    245 	char msg[SLAP_TEXT_BUFLEN];
    246 	int		i;
    247 
    248 	mi->mi_disabled = 0;
    249 	if ( mi->mi_ntargets == 0 ) {
    250 
    251 		Debug( LDAP_DEBUG_ANY,
    252 			"asyncmeta_back_db_open: no targets defined\n" );
    253 	}
    254 
    255 	for ( i = 0; i < mi->mi_ntargets; i++ ) {
    256 		a_metatarget_t	*mt = mi->mi_targets[ i ];
    257 		if ( asyncmeta_target_finish( mi, mt,
    258 					      "asyncmeta_back_db_open", msg, sizeof( msg ))) {
    259 			return 1;
    260 		}
    261 	}
    262 
    263 	if ( mi->mi_conns == NULL ) {
    264 		mi->mi_num_conns = (mi->mi_max_target_conns == 0) ? META_BACK_CFG_MAX_TARGET_CONNS : mi->mi_max_target_conns;
    265 		assert(mi->mi_num_conns > 0);
    266 		mi->mi_conns = ch_calloc( mi->mi_num_conns, sizeof( a_metaconn_t ));
    267 		for (i = 0; i < mi->mi_num_conns; i++) {
    268 			a_metaconn_t *mc = &mi->mi_conns[i];
    269 			ldap_pvt_thread_mutex_init( &mc->mc_om_mutex);
    270 			mc->mc_authz_target = META_BOUND_NONE;
    271 
    272 			if ( mi->mi_ntargets > 0 ) {
    273 				mc->mc_conns = ch_calloc( mi->mi_ntargets, sizeof( a_metasingleconn_t ));
    274 			} else {
    275 				mc->mc_conns = NULL;
    276 			}
    277 
    278 			mc->mc_info = mi;
    279 			LDAP_STAILQ_INIT( &mc->mc_om_list );
    280 		}
    281 
    282 
    283 		ber_dupbv ( &mi->mi_suffix, &be->be_suffix[0] );
    284 
    285 		if ( ( slapMode & SLAP_SERVER_MODE ) && mi->mi_ntargets > 0 ) {
    286 			ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
    287 			mi->mi_task = ldap_pvt_runqueue_insert( &slapd_rq, 1,
    288 													asyncmeta_timeout_loop, mi, "asyncmeta_timeout_loop", mi->mi_suffix.bv_val );
    289 			ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
    290 		}
    291 	}
    292 	return 0;
    293 }
    294 
    295 /*
    296  * asyncmeta_back_conn_free()
    297  *
    298  * actually frees a connection; the reference count must be 0,
    299  * and it must not (or no longer) be in the cache.
    300  */
    301 void
    302 asyncmeta_back_conn_free(
    303 	void 		*v_mc )
    304 {
    305 	a_metaconn_t		*mc = v_mc;
    306 
    307 	assert( mc != NULL );
    308 	ldap_pvt_thread_mutex_destroy( &mc->mc_om_mutex );
    309 	free( mc );
    310 }
    311 
    312 void
    313 asyncmeta_back_clear_miconns( a_metainfo_t *mi )
    314 {
    315 	int i, j;
    316 	a_metaconn_t *mc;
    317 	if ( mi->mi_conns != NULL ) {
    318 		for (i = 0; i < mi->mi_num_conns; i++) {
    319 			mc = &mi->mi_conns[i];
    320 			for (j = 0; j < mi->mi_ntargets; j ++) {
    321 				asyncmeta_clear_one_msc(NULL, mc, j, 1, __FUNCTION__);
    322 			}
    323 
    324 			if ( mc->mc_conns )
    325 				free( mc->mc_conns );
    326 			ldap_pvt_thread_mutex_destroy( &mc->mc_om_mutex );
    327 		}
    328 		free(mi->mi_conns);
    329 	}
    330 	mi->mi_conns = NULL;
    331 	mi->mi_num_conns = 0;
    332 }
    333 
    334 void
    335 asyncmeta_target_free(
    336 	a_metatarget_t	*mt )
    337 {
    338 	if ( mt->mt_uri ) {
    339 		free( mt->mt_uri );
    340 		ldap_pvt_thread_mutex_destroy( &mt->mt_uri_mutex );
    341 	}
    342 	if ( mt->mt_subtree ) {
    343 		asyncmeta_subtree_destroy( mt->mt_subtree );
    344 		mt->mt_subtree = NULL;
    345 	}
    346 	if ( mt->mt_filter ) {
    347 		asyncmeta_filter_destroy( mt->mt_filter );
    348 		mt->mt_filter = NULL;
    349 	}
    350 	if ( !BER_BVISNULL( &mt->mt_psuffix ) ) {
    351 		free( mt->mt_psuffix.bv_val );
    352 	}
    353 	if ( !BER_BVISNULL( &mt->mt_nsuffix ) ) {
    354 		free( mt->mt_nsuffix.bv_val );
    355 	}
    356 	if ( !BER_BVISNULL( &mt->mt_binddn ) ) {
    357 		free( mt->mt_binddn.bv_val );
    358 	}
    359 	if ( !BER_BVISNULL( &mt->mt_bindpw ) ) {
    360 		free( mt->mt_bindpw.bv_val );
    361 	}
    362 	if ( !BER_BVISNULL( &mt->mt_idassert_authcID ) ) {
    363 		ch_free( mt->mt_idassert_authcID.bv_val );
    364 	}
    365 	if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) ) {
    366 		ch_free( mt->mt_idassert_authcDN.bv_val );
    367 	}
    368 	if ( !BER_BVISNULL( &mt->mt_idassert_passwd ) ) {
    369 		ch_free( mt->mt_idassert_passwd.bv_val );
    370 	}
    371 	if ( !BER_BVISNULL( &mt->mt_idassert_authzID ) ) {
    372 		ch_free( mt->mt_idassert_authzID.bv_val );
    373 	}
    374 	if ( !BER_BVISNULL( &mt->mt_idassert_sasl_mech ) ) {
    375 		ch_free( mt->mt_idassert_sasl_mech.bv_val );
    376 	}
    377 	if ( !BER_BVISNULL( &mt->mt_idassert_sasl_realm ) ) {
    378 		ch_free( mt->mt_idassert_sasl_realm.bv_val );
    379 	}
    380 	if ( mt->mt_idassert_authz != NULL ) {
    381 		ber_bvarray_free( mt->mt_idassert_authz );
    382 	}
    383 	if ( !BER_BVISNULL( &mt->mt_lsuffixm )) {
    384 		ch_free( mt->mt_lsuffixm.bv_val );
    385 	}
    386 	if ( !BER_BVISNULL( &mt->mt_rsuffixm )) {
    387 		ch_free( mt->mt_rsuffixm.bv_val );
    388 	}
    389 	free( mt );
    390 }
    391 
    392 int
    393 asyncmeta_back_db_close(
    394 	Backend		*be,
    395 	ConfigReply	*cr )
    396 {
    397 	a_metainfo_t	*mi;
    398 
    399 	if ( be->be_private ) {
    400 		mi = ( a_metainfo_t * )be->be_private;
    401 		mi->mi_disabled = 1;
    402 		/* there are no pending ops so we can free up the connections and stop the timeout loop
    403 		 * else timeout_loop will clear up the ops and connections and not reschedule */
    404 		if ( asyncmeta_db_has_pending_ops( mi ) == 0 ) {
    405 				ldap_pvt_thread_mutex_lock( &mi->mi_mc_mutex );
    406 				asyncmeta_back_clear_miconns(mi);
    407 				ldap_pvt_thread_mutex_unlock( &mi->mi_mc_mutex );
    408 				if ( mi->mi_task != NULL ) {
    409 					ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
    410 					if ( ldap_pvt_runqueue_isrunning( &slapd_rq, mi->mi_task )) {
    411 						ldap_pvt_runqueue_stoptask( &slapd_rq,  mi->mi_task);
    412 					}
    413 					ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
    414 					mi->mi_task = NULL;
    415 				}
    416 		}
    417 	}
    418 	return 0;
    419 }
    420 
    421 int
    422 asyncmeta_back_db_destroy(
    423 	Backend		*be,
    424 	ConfigReply	*cr )
    425 {
    426 	a_metainfo_t	*mi;
    427 
    428 	if ( be->be_private ) {
    429 		int i;
    430 
    431 		mi = ( a_metainfo_t * )be->be_private;
    432 		/*
    433 		 * Destroy the per-target stuff (assuming there's at
    434 		 * least one ...)
    435 		 */
    436 		if ( mi->mi_targets != NULL ) {
    437 			for ( i = 0; i < mi->mi_ntargets; i++ ) {
    438 				a_metatarget_t	*mt = mi->mi_targets[ i ];
    439 
    440 				if ( META_BACK_TGT_QUARANTINE( mt ) ) {
    441 					if ( mt->mt_quarantine.ri_num != mi->mi_quarantine.ri_num )
    442 					{
    443 						mi->mi_ldap_extra->retry_info_destroy( &mt->mt_quarantine );
    444 					}
    445 
    446 					ldap_pvt_thread_mutex_destroy( &mt->mt_quarantine_mutex );
    447 				}
    448 
    449 				asyncmeta_target_free( mt );
    450 			}
    451 
    452 			free( mi->mi_targets );
    453 		}
    454 
    455 		ldap_pvt_thread_mutex_lock( &mi->mi_cache.mutex );
    456 		if ( mi->mi_cache.tree ) {
    457 			ldap_avl_free( mi->mi_cache.tree, asyncmeta_dncache_free );
    458 		}
    459 
    460 		ldap_pvt_thread_mutex_unlock( &mi->mi_cache.mutex );
    461 		ldap_pvt_thread_mutex_destroy( &mi->mi_cache.mutex );
    462 
    463 		if ( mi->mi_candidates != NULL ) {
    464 			ber_memfree_x( mi->mi_candidates, NULL );
    465 		}
    466 
    467 		if ( META_BACK_QUARANTINE( mi ) ) {
    468 			mi->mi_ldap_extra->retry_info_destroy( &mi->mi_quarantine );
    469 		}
    470 
    471 		ldap_pvt_thread_mutex_lock( &mi->mi_mc_mutex );
    472 		asyncmeta_back_clear_miconns(mi);
    473 		ldap_pvt_thread_mutex_unlock( &mi->mi_mc_mutex );
    474 		ldap_pvt_thread_mutex_destroy( &mi->mi_mc_mutex );
    475 
    476 		free( be->be_private );
    477 	}
    478 	return 0;
    479 }
    480 
    481 #if SLAPD_ASYNCMETA == SLAPD_MOD_DYNAMIC
    482 
    483 /* conditionally define the init_module() function */
    484 SLAP_BACKEND_INIT_MODULE( asyncmeta )
    485 
    486 #endif /* SLAPD_ASYNCMETA == SLAPD_MOD_DYNAMIC */
    487