1 /*	$NetBSD: init.c,v 1.2 2021/08/14 16:14:59 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-2021 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.2 2021/08/14 16:14:59 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
asyncmeta_back_open(BackendInfo * bi)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
asyncmeta_back_initialize(BackendInfo * bi)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
asyncmeta_back_db_init(Backend * be,ConfigReply * cr)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
asyncmeta_target_finish(a_metainfo_t * mi,a_metatarget_t * mt,const char * log,char * msg,size_t msize)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
asyncmeta_back_db_open(Backend * be,ConfigReply * cr)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 	if ( mi->mi_ntargets == 0 ) {
249 		/* Dynamically added, nothing to check here until
250 		 * some targets get added
251 		 */
252 		if ( slapMode & SLAP_SERVER_RUNNING )
253 			return 0;
254 
255 		Debug( LDAP_DEBUG_ANY,
256 			"asyncmeta_back_db_open: no targets defined\n" );
257 		return 1;
258 	}
259 	mi->mi_num_conns = 0;
260 	for ( i = 0; i < mi->mi_ntargets; i++ ) {
261 		a_metatarget_t	*mt = mi->mi_targets[ i ];
262 		if ( asyncmeta_target_finish( mi, mt,
263 			"asyncmeta_back_db_open", msg, sizeof( msg )))
264 			return 1;
265 	}
266 	mi->mi_num_conns = (mi->mi_max_target_conns == 0) ? META_BACK_CFG_MAX_TARGET_CONNS : mi->mi_max_target_conns;
267 	assert(mi->mi_num_conns > 0);
268 	mi->mi_conns = ch_calloc( mi->mi_num_conns, sizeof( a_metaconn_t ));
269 	for (i = 0; i < mi->mi_num_conns; i++) {
270 		a_metaconn_t *mc = &mi->mi_conns[i];
271 		ldap_pvt_thread_mutex_init( &mc->mc_om_mutex);
272 		mc->mc_authz_target = META_BOUND_NONE;
273 		mc->mc_conns = ch_calloc( mi->mi_ntargets, sizeof( a_metasingleconn_t ));
274 		mc->mc_info = mi;
275 		LDAP_STAILQ_INIT( &mc->mc_om_list );
276 	}
277 	mi->mi_suffix = be->be_suffix[0];
278 	ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
279 	mi->mi_task = ldap_pvt_runqueue_insert( &slapd_rq, 1,
280 		asyncmeta_timeout_loop, mi, "asyncmeta_timeout_loop", mi->mi_suffix.bv_val );
281 	ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
282 	return 0;
283 }
284 
285 /*
286  * asyncmeta_back_conn_free()
287  *
288  * actually frees a connection; the reference count must be 0,
289  * and it must not (or no longer) be in the cache.
290  */
291 void
asyncmeta_back_conn_free(void * v_mc)292 asyncmeta_back_conn_free(
293 	void 		*v_mc )
294 {
295 	a_metaconn_t		*mc = v_mc;
296 
297 	assert( mc != NULL );
298 	ldap_pvt_thread_mutex_destroy( &mc->mc_om_mutex );
299 	free( mc );
300 }
301 
302 static void
asyncmeta_back_stop_miconns(a_metainfo_t * mi)303 asyncmeta_back_stop_miconns( a_metainfo_t *mi )
304 {
305 
306 	/*Todo do any other mc cleanup here if necessary*/
307 }
308 
309 static void
asyncmeta_back_clear_miconns(a_metainfo_t * mi)310 asyncmeta_back_clear_miconns( a_metainfo_t *mi )
311 {
312 	int i, j;
313 	a_metaconn_t *mc;
314 	for (i = 0; i < mi->mi_num_conns; i++) {
315 		mc = &mi->mi_conns[i];
316 		/* todo clear the message queue */
317 		for (j = 0; j < mi->mi_ntargets; j ++) {
318 			asyncmeta_clear_one_msc(NULL, mc, j, 1, __FUNCTION__);
319 		}
320 		free(mc->mc_conns);
321 		ldap_pvt_thread_mutex_destroy( &mc->mc_om_mutex );
322 	}
323 	free(mi->mi_conns);
324 }
325 
326 static void
asyncmeta_target_free(a_metatarget_t * mt)327 asyncmeta_target_free(
328 	a_metatarget_t	*mt )
329 {
330 	if ( mt->mt_uri ) {
331 		free( mt->mt_uri );
332 		ldap_pvt_thread_mutex_destroy( &mt->mt_uri_mutex );
333 	}
334 	if ( mt->mt_subtree ) {
335 		asyncmeta_subtree_destroy( mt->mt_subtree );
336 		mt->mt_subtree = NULL;
337 	}
338 	if ( mt->mt_filter ) {
339 		asyncmeta_filter_destroy( mt->mt_filter );
340 		mt->mt_filter = NULL;
341 	}
342 	if ( !BER_BVISNULL( &mt->mt_psuffix ) ) {
343 		free( mt->mt_psuffix.bv_val );
344 	}
345 	if ( !BER_BVISNULL( &mt->mt_nsuffix ) ) {
346 		free( mt->mt_nsuffix.bv_val );
347 	}
348 	if ( !BER_BVISNULL( &mt->mt_binddn ) ) {
349 		free( mt->mt_binddn.bv_val );
350 	}
351 	if ( !BER_BVISNULL( &mt->mt_bindpw ) ) {
352 		free( mt->mt_bindpw.bv_val );
353 	}
354 	if ( !BER_BVISNULL( &mt->mt_idassert_authcID ) ) {
355 		ch_free( mt->mt_idassert_authcID.bv_val );
356 	}
357 	if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) ) {
358 		ch_free( mt->mt_idassert_authcDN.bv_val );
359 	}
360 	if ( !BER_BVISNULL( &mt->mt_idassert_passwd ) ) {
361 		ch_free( mt->mt_idassert_passwd.bv_val );
362 	}
363 	if ( !BER_BVISNULL( &mt->mt_idassert_authzID ) ) {
364 		ch_free( mt->mt_idassert_authzID.bv_val );
365 	}
366 	if ( !BER_BVISNULL( &mt->mt_idassert_sasl_mech ) ) {
367 		ch_free( mt->mt_idassert_sasl_mech.bv_val );
368 	}
369 	if ( !BER_BVISNULL( &mt->mt_idassert_sasl_realm ) ) {
370 		ch_free( mt->mt_idassert_sasl_realm.bv_val );
371 	}
372 	if ( mt->mt_idassert_authz != NULL ) {
373 		ber_bvarray_free( mt->mt_idassert_authz );
374 	}
375 	if ( !BER_BVISNULL( &mt->mt_lsuffixm )) {
376 		ch_free( mt->mt_lsuffixm.bv_val );
377 	}
378 	if ( !BER_BVISNULL( &mt->mt_rsuffixm )) {
379 		ch_free( mt->mt_rsuffixm.bv_val );
380 	}
381 	free( mt );
382 }
383 
384 int
asyncmeta_back_db_close(Backend * be,ConfigReply * cr)385 asyncmeta_back_db_close(
386 	Backend		*be,
387 	ConfigReply	*cr )
388 {
389 	a_metainfo_t	*mi;
390 
391 	if ( be->be_private ) {
392 		mi = ( a_metainfo_t * )be->be_private;
393 		if ( mi->mi_task != NULL ) {
394 			ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
395 			if ( ldap_pvt_runqueue_isrunning( &slapd_rq, mi->mi_task )) {
396 				ldap_pvt_runqueue_stoptask( &slapd_rq,  mi->mi_task);
397 			}
398 			ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
399 			mi->mi_task = NULL;
400 		}
401 		ldap_pvt_thread_mutex_lock( &mi->mi_mc_mutex );
402 		asyncmeta_back_stop_miconns( mi );
403 		ldap_pvt_thread_mutex_unlock( &mi->mi_mc_mutex );
404 	}
405 	return 0;
406 }
407 
408 int
asyncmeta_back_db_destroy(Backend * be,ConfigReply * cr)409 asyncmeta_back_db_destroy(
410 	Backend		*be,
411 	ConfigReply	*cr )
412 {
413 	a_metainfo_t	*mi;
414 
415 	if ( be->be_private ) {
416 		int i;
417 
418 		mi = ( a_metainfo_t * )be->be_private;
419 		/*
420 		 * Destroy the per-target stuff (assuming there's at
421 		 * least one ...)
422 		 */
423 		if ( mi->mi_targets != NULL ) {
424 			for ( i = 0; i < mi->mi_ntargets; i++ ) {
425 				a_metatarget_t	*mt = mi->mi_targets[ i ];
426 
427 				if ( META_BACK_TGT_QUARANTINE( mt ) ) {
428 					if ( mt->mt_quarantine.ri_num != mi->mi_quarantine.ri_num )
429 					{
430 						mi->mi_ldap_extra->retry_info_destroy( &mt->mt_quarantine );
431 					}
432 
433 					ldap_pvt_thread_mutex_destroy( &mt->mt_quarantine_mutex );
434 				}
435 
436 				asyncmeta_target_free( mt );
437 			}
438 
439 			free( mi->mi_targets );
440 		}
441 
442 		ldap_pvt_thread_mutex_lock( &mi->mi_cache.mutex );
443 		if ( mi->mi_cache.tree ) {
444 			ldap_avl_free( mi->mi_cache.tree, asyncmeta_dncache_free );
445 		}
446 
447 		ldap_pvt_thread_mutex_unlock( &mi->mi_cache.mutex );
448 		ldap_pvt_thread_mutex_destroy( &mi->mi_cache.mutex );
449 
450 		if ( mi->mi_candidates != NULL ) {
451 			ber_memfree_x( mi->mi_candidates, NULL );
452 		}
453 
454 		if ( META_BACK_QUARANTINE( mi ) ) {
455 			mi->mi_ldap_extra->retry_info_destroy( &mi->mi_quarantine );
456 		}
457 
458 		ldap_pvt_thread_mutex_lock( &mi->mi_mc_mutex );
459 		asyncmeta_back_clear_miconns(mi);
460 		ldap_pvt_thread_mutex_unlock( &mi->mi_mc_mutex );
461 		ldap_pvt_thread_mutex_destroy( &mi->mi_mc_mutex );
462 
463 		free( be->be_private );
464 	}
465 	return 0;
466 }
467 
468 #if SLAPD_ASYNCMETA == SLAPD_MOD_DYNAMIC
469 
470 /* conditionally define the init_module() function */
471 SLAP_BACKEND_INIT_MODULE( asyncmeta )
472 
473 #endif /* SLAPD_ASYNCMETA == SLAPD_MOD_DYNAMIC */
474