init.c 11.4 KB
Newer Older
1
2
3
4
/* init.c - initialization of a back-asyncmeta database */
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
5
 * Copyright 2016-2021 The OpenLDAP Foundation.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 * Portions Copyright 2016 Symas Corporation.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */

/* ACKNOWLEDGEMENTS:
 * This work was developed by Symas Corporation
 * based on back-meta module for inclusion in OpenLDAP Software.
 * This work was sponsored by Ericsson. */

#include "portable.h"

#include <stdio.h>

#include <ac/string.h>
#include <ac/socket.h>

#include "slap.h"
31
#include "slap-config.h"
32
33
34
#include "../back-ldap/back-ldap.h"
#include "back-asyncmeta.h"

35
36
int asyncmeta_debug;

37
38
39
40
41
42
43
44
45
46
47
48
49
50
int
asyncmeta_back_open(
	BackendInfo	*bi )
{
	/* FIXME: need to remove the pagedResults, and likely more... */
	bi->bi_controls = slap_known_controls;

	return 0;
}

int
asyncmeta_back_initialize(
	BackendInfo	*bi )
{
51
52
53
54
55
56
57
58
	int rc;
	struct berval debugbv = BER_BVC("asyncmeta");

	rc = slap_loglevel_get( &debugbv, &asyncmeta_debug );
	if ( rc ) {
		return rc;
	}

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
	bi->bi_flags =
#if 0
	/* this is not (yet) set essentially because back-meta does not
	 * directly support extended operations... */
#ifdef LDAP_DYNAMIC_OBJECTS
		/* this is set because all the support a proxy has to provide
		 * is the capability to forward the refresh exop, and to
		 * pass thru entries that contain the dynamicObject class
		 * and the entryTtl attribute */
		SLAP_BFLAG_DYNAMIC |
#endif /* LDAP_DYNAMIC_OBJECTS */
#endif

		/* back-meta recognizes RFC4525 increment;
		 * let the remote server complain, if needed (ITS#5912) */
		SLAP_BFLAG_INCREMENT;

	bi->bi_open = asyncmeta_back_open;
	bi->bi_config = 0;
	bi->bi_close = 0;
	bi->bi_destroy = 0;

	bi->bi_db_init = asyncmeta_back_db_init;
	bi->bi_db_config = config_generic_wrapper;
	bi->bi_db_open = asyncmeta_back_db_open;
	bi->bi_db_close = asyncmeta_back_db_close;
	bi->bi_db_destroy = asyncmeta_back_db_destroy;

	bi->bi_op_bind = asyncmeta_back_bind;
	bi->bi_op_unbind = 0;
	bi->bi_op_search = asyncmeta_back_search;
	bi->bi_op_compare = asyncmeta_back_compare;
	bi->bi_op_modify = asyncmeta_back_modify;
	bi->bi_op_modrdn = asyncmeta_back_modrdn;
	bi->bi_op_add = asyncmeta_back_add;
	bi->bi_op_delete = asyncmeta_back_delete;
	bi->bi_op_abandon = 0;

	bi->bi_extended = 0;

	bi->bi_chk_referrals = 0;

	bi->bi_connection_init = 0;
102
	bi->bi_connection_destroy =  0 /* asyncmeta_back_conn_destroy */;
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118

	return asyncmeta_back_init_cf( bi );
}

int
asyncmeta_back_db_init(
	Backend		*be,
	ConfigReply	*cr)
{
	a_metainfo_t	*mi;
	int		i;
	BackendInfo	*bi;

	bi = backend_info( "ldap" );
	if ( !bi || !bi->bi_extra ) {
		Debug( LDAP_DEBUG_ANY,
119
			"asyncmeta_back_db_init: needs back-ldap\n" );
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
		return 1;
	}

	mi = ch_calloc( 1, sizeof( a_metainfo_t ) );
	if ( mi == NULL ) {
		return -1;
	}

	/* set default flags */
	mi->mi_flags =
		META_BACK_F_DEFER_ROOTDN_BIND
		| META_BACK_F_PROXYAUTHZ_ALWAYS
		| META_BACK_F_PROXYAUTHZ_ANON
		| META_BACK_F_PROXYAUTHZ_NOANON;

	/*
	 * At present the default is no default target;
	 * this may change
	 */
	mi->mi_defaulttarget = META_DEFAULT_TARGET_NONE;
	mi->mi_bind_timeout.tv_sec = 0;
	mi->mi_bind_timeout.tv_usec = META_BIND_TIMEOUT;

	mi->mi_rebind_f = asyncmeta_back_default_rebind;
	mi->mi_urllist_f = asyncmeta_back_default_urllist;

	ldap_pvt_thread_mutex_init( &mi->mi_cache.mutex );

	/* safe default */
	mi->mi_nretries = META_RETRY_DEFAULT;
	mi->mi_version = LDAP_VERSION3;

	for ( i = LDAP_BACK_PCONN_FIRST; i < LDAP_BACK_PCONN_LAST; i++ ) {
		mi->mi_conn_priv[ i ].mic_num = 0;
		LDAP_TAILQ_INIT( &mi->mi_conn_priv[ i ].mic_priv );
	}
	mi->mi_conn_priv_max = LDAP_BACK_CONN_PRIV_DEFAULT;

	mi->mi_ldap_extra = (ldap_extra_t *)bi->bi_extra;
	ldap_pvt_thread_mutex_init( &mi->mi_mc_mutex);

	be->be_private = mi;
	be->be_cf_ocs = be->bd_info->bi_cf_ocs;

	return 0;
}

int
asyncmeta_target_finish(
	a_metainfo_t *mi,
	a_metatarget_t *mt,
	const char *log,
	char *msg,
	size_t msize
)
{
	slap_bindconf	sb = { BER_BVNULL };
	int rc;

	ber_str2bv( mt->mt_uri, 0, 0, &sb.sb_uri );
	sb.sb_version = mt->mt_version;
	sb.sb_method = LDAP_AUTH_SIMPLE;
	BER_BVSTR( &sb.sb_binddn, "" );

	if ( META_BACK_TGT_T_F_DISCOVER( mt ) ) {
		rc = slap_discover_feature( &sb,
				slap_schema.si_ad_supportedFeatures->ad_cname.bv_val,
				LDAP_FEATURE_ABSOLUTE_FILTERS );
		if ( rc == LDAP_COMPARE_TRUE ) {
			mt->mt_flags |= LDAP_BACK_F_T_F;
		}
	}

	if ( META_BACK_TGT_CANCEL_DISCOVER( mt ) ) {
		rc = slap_discover_feature( &sb,
				slap_schema.si_ad_supportedExtension->ad_cname.bv_val,
				LDAP_EXOP_CANCEL );
		if ( rc == LDAP_COMPARE_TRUE ) {
			mt->mt_flags |= LDAP_BACK_F_CANCEL_EXOP;
		}
	}

	if ( !( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE )
		|| mt->mt_idassert_authz != NULL )
	{
		mi->mi_flags &= ~META_BACK_F_PROXYAUTHZ_ALWAYS;
	}

	if ( ( mt->mt_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL )
		&& !( mt->mt_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) )
	{
211
212
213
		Debug(LDAP_DEBUG_ANY,
		      "%s: inconsistent idassert configuration " "(likely authz=\"*\" used with \"non-prescriptive\" flag) (target %s)\n",
		      log, mt->mt_uri );
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
		return 1;
	}

	if ( !( mt->mt_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL ) )
	{
		mi->mi_flags &= ~META_BACK_F_PROXYAUTHZ_ANON;
	}

	if ( ( mt->mt_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) )
	{
		mi->mi_flags &= ~META_BACK_F_PROXYAUTHZ_NOANON;
	}

	return 0;
}

int
asyncmeta_back_db_open(
	Backend		*be,
	ConfigReply	*cr )
{
	a_metainfo_t	*mi = (a_metainfo_t *)be->be_private;
	char msg[SLAP_TEXT_BUFLEN];
237
	int		i;
238
239
240
241
242
243
244
245
246

	if ( mi->mi_ntargets == 0 ) {
		/* Dynamically added, nothing to check here until
		 * some targets get added
		 */
		if ( slapMode & SLAP_SERVER_RUNNING )
			return 0;

		Debug( LDAP_DEBUG_ANY,
247
			"asyncmeta_back_db_open: no targets defined\n" );
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
		return 1;
	}
	mi->mi_num_conns = 0;
	for ( i = 0; i < mi->mi_ntargets; i++ ) {
		a_metatarget_t	*mt = mi->mi_targets[ i ];
		if ( asyncmeta_target_finish( mi, mt,
			"asyncmeta_back_db_open", msg, sizeof( msg )))
			return 1;
	}
	mi->mi_num_conns = (mi->mi_max_target_conns == 0) ? META_BACK_CFG_MAX_TARGET_CONNS : mi->mi_max_target_conns;
	assert(mi->mi_num_conns > 0);
	mi->mi_conns = ch_calloc( mi->mi_num_conns, sizeof( a_metaconn_t ));
	for (i = 0; i < mi->mi_num_conns; i++) {
		a_metaconn_t *mc = &mi->mi_conns[i];
		ldap_pvt_thread_mutex_init( &mc->mc_om_mutex);
		mc->mc_authz_target = META_BOUND_NONE;
		mc->mc_conns = ch_calloc( mi->mi_ntargets, sizeof( a_metasingleconn_t ));
		mc->mc_info = mi;
266
		LDAP_STAILQ_INIT( &mc->mc_om_list );
267
	}
268
	mi->mi_suffix = be->be_suffix[0];
269
	ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
Howard Chu's avatar
Howard Chu committed
270
	mi->mi_task = ldap_pvt_runqueue_insert( &slapd_rq, 1,
271
		asyncmeta_timeout_loop, mi, "asyncmeta_timeout_loop", mi->mi_suffix.bv_val );
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
	ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
	return 0;
}

/*
 * asyncmeta_back_conn_free()
 *
 * actually frees a connection; the reference count must be 0,
 * and it must not (or no longer) be in the cache.
 */
void
asyncmeta_back_conn_free(
	void 		*v_mc )
{
	a_metaconn_t		*mc = v_mc;

	assert( mc != NULL );
	ldap_pvt_thread_mutex_destroy( &mc->mc_om_mutex );
	free( mc );
}

static void
asyncmeta_back_stop_miconns( a_metainfo_t *mi )
{

	/*Todo do any other mc cleanup here if necessary*/
}

static void
asyncmeta_back_clear_miconns( a_metainfo_t *mi )
{
	int i, j;
	a_metaconn_t *mc;
	for (i = 0; i < mi->mi_num_conns; i++) {
		mc = &mi->mi_conns[i];
		/* todo clear the message queue */
		for (j = 0; j < mi->mi_ntargets; j ++) {
309
			asyncmeta_clear_one_msc(NULL, mc, j, 1, __FUNCTION__);
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
		}
		free(mc->mc_conns);
		ldap_pvt_thread_mutex_destroy( &mc->mc_om_mutex );
	}
	free(mi->mi_conns);
}

static void
asyncmeta_target_free(
	a_metatarget_t	*mt )
{
	if ( mt->mt_uri ) {
		free( mt->mt_uri );
		ldap_pvt_thread_mutex_destroy( &mt->mt_uri_mutex );
	}
	if ( mt->mt_subtree ) {
		asyncmeta_subtree_destroy( mt->mt_subtree );
		mt->mt_subtree = NULL;
	}
	if ( mt->mt_filter ) {
		asyncmeta_filter_destroy( mt->mt_filter );
		mt->mt_filter = NULL;
	}
	if ( !BER_BVISNULL( &mt->mt_psuffix ) ) {
		free( mt->mt_psuffix.bv_val );
	}
	if ( !BER_BVISNULL( &mt->mt_nsuffix ) ) {
		free( mt->mt_nsuffix.bv_val );
	}
	if ( !BER_BVISNULL( &mt->mt_binddn ) ) {
		free( mt->mt_binddn.bv_val );
	}
	if ( !BER_BVISNULL( &mt->mt_bindpw ) ) {
		free( mt->mt_bindpw.bv_val );
	}
	if ( !BER_BVISNULL( &mt->mt_idassert_authcID ) ) {
		ch_free( mt->mt_idassert_authcID.bv_val );
	}
	if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) ) {
		ch_free( mt->mt_idassert_authcDN.bv_val );
	}
	if ( !BER_BVISNULL( &mt->mt_idassert_passwd ) ) {
		ch_free( mt->mt_idassert_passwd.bv_val );
	}
	if ( !BER_BVISNULL( &mt->mt_idassert_authzID ) ) {
		ch_free( mt->mt_idassert_authzID.bv_val );
	}
	if ( !BER_BVISNULL( &mt->mt_idassert_sasl_mech ) ) {
		ch_free( mt->mt_idassert_sasl_mech.bv_val );
	}
	if ( !BER_BVISNULL( &mt->mt_idassert_sasl_realm ) ) {
		ch_free( mt->mt_idassert_sasl_realm.bv_val );
	}
	if ( mt->mt_idassert_authz != NULL ) {
		ber_bvarray_free( mt->mt_idassert_authz );
	}
366
367
368
369
370
	if ( !BER_BVISNULL( &mt->mt_lsuffixm )) {
		ch_free( mt->mt_lsuffixm.bv_val );
	}
	if ( !BER_BVISNULL( &mt->mt_rsuffixm )) {
		ch_free( mt->mt_rsuffixm.bv_val );
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
	}
	free( mt );
}

int
asyncmeta_back_db_close(
	Backend		*be,
	ConfigReply	*cr )
{
	a_metainfo_t	*mi;

	if ( be->be_private ) {
		mi = ( a_metainfo_t * )be->be_private;
		if ( mi->mi_task != NULL ) {
			ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
			if ( ldap_pvt_runqueue_isrunning( &slapd_rq, mi->mi_task )) {
				ldap_pvt_runqueue_stoptask( &slapd_rq,  mi->mi_task);
			}
			ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
			mi->mi_task = NULL;
		}
		ldap_pvt_thread_mutex_lock( &mi->mi_mc_mutex );
		asyncmeta_back_stop_miconns( mi );
		ldap_pvt_thread_mutex_unlock( &mi->mi_mc_mutex );
	}
396
	return 0;
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
}

int
asyncmeta_back_db_destroy(
	Backend		*be,
	ConfigReply	*cr )
{
	a_metainfo_t	*mi;

	if ( be->be_private ) {
		int i;

		mi = ( a_metainfo_t * )be->be_private;
		/*
		 * Destroy the per-target stuff (assuming there's at
		 * least one ...)
		 */
		if ( mi->mi_targets != NULL ) {
			for ( i = 0; i < mi->mi_ntargets; i++ ) {
				a_metatarget_t	*mt = mi->mi_targets[ i ];

				if ( META_BACK_TGT_QUARANTINE( mt ) ) {
					if ( mt->mt_quarantine.ri_num != mi->mi_quarantine.ri_num )
					{
						mi->mi_ldap_extra->retry_info_destroy( &mt->mt_quarantine );
					}

					ldap_pvt_thread_mutex_destroy( &mt->mt_quarantine_mutex );
				}

				asyncmeta_target_free( mt );
			}

			free( mi->mi_targets );
		}

		ldap_pvt_thread_mutex_lock( &mi->mi_cache.mutex );
		if ( mi->mi_cache.tree ) {
			avl_free( mi->mi_cache.tree, asyncmeta_dncache_free );
		}

		ldap_pvt_thread_mutex_unlock( &mi->mi_cache.mutex );
		ldap_pvt_thread_mutex_destroy( &mi->mi_cache.mutex );

		if ( mi->mi_candidates != NULL ) {
			ber_memfree_x( mi->mi_candidates, NULL );
		}

		if ( META_BACK_QUARANTINE( mi ) ) {
			mi->mi_ldap_extra->retry_info_destroy( &mi->mi_quarantine );
		}

449
450
451
452
453
454
455
		ldap_pvt_thread_mutex_lock( &mi->mi_mc_mutex );
		asyncmeta_back_clear_miconns(mi);
		ldap_pvt_thread_mutex_unlock( &mi->mi_mc_mutex );
		ldap_pvt_thread_mutex_destroy( &mi->mi_mc_mutex );

		free( be->be_private );
	}
456
457
458
459
460
461
462
463
464
	return 0;
}

#if SLAPD_ASYNCMETA == SLAPD_MOD_DYNAMIC

/* conditionally define the init_module() function */
SLAP_BACKEND_INIT_MODULE( asyncmeta )

#endif /* SLAPD_ASYNCMETA == SLAPD_MOD_DYNAMIC */