xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/back-meta/init.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: init.c,v 1.1.1.5 2014/05/28 09:58:50 tron Exp $	*/
2 
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1999-2014 The OpenLDAP Foundation.
7  * Portions Copyright 2001-2003 Pierangelo Masarati.
8  * Portions Copyright 1999-2003 Howard Chu.
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 #include "portable.h"
21 
22 #include <stdio.h>
23 
24 #include <ac/string.h>
25 #include <ac/socket.h>
26 
27 #include "slap.h"
28 #include "config.h"
29 #include "../back-ldap/back-ldap.h"
30 #include "back-meta.h"
31 
32 int
33 meta_back_open(
34 	BackendInfo	*bi )
35 {
36 	/* FIXME: need to remove the pagedResults, and likely more... */
37 	bi->bi_controls = slap_known_controls;
38 
39 	return 0;
40 }
41 
42 int
43 meta_back_initialize(
44 	BackendInfo	*bi )
45 {
46 	bi->bi_flags =
47 #if 0
48 	/* this is not (yet) set essentially because back-meta does not
49 	 * directly support extended operations... */
50 #ifdef LDAP_DYNAMIC_OBJECTS
51 		/* this is set because all the support a proxy has to provide
52 		 * is the capability to forward the refresh exop, and to
53 		 * pass thru entries that contain the dynamicObject class
54 		 * and the entryTtl attribute */
55 		SLAP_BFLAG_DYNAMIC |
56 #endif /* LDAP_DYNAMIC_OBJECTS */
57 #endif
58 
59 		/* back-meta recognizes RFC4525 increment;
60 		 * let the remote server complain, if needed (ITS#5912) */
61 		SLAP_BFLAG_INCREMENT;
62 
63 	bi->bi_open = meta_back_open;
64 	bi->bi_config = 0;
65 	bi->bi_close = 0;
66 	bi->bi_destroy = 0;
67 
68 	bi->bi_db_init = meta_back_db_init;
69 	bi->bi_db_config = config_generic_wrapper;
70 	bi->bi_db_open = meta_back_db_open;
71 	bi->bi_db_close = 0;
72 	bi->bi_db_destroy = meta_back_db_destroy;
73 
74 	bi->bi_op_bind = meta_back_bind;
75 	bi->bi_op_unbind = 0;
76 	bi->bi_op_search = meta_back_search;
77 	bi->bi_op_compare = meta_back_compare;
78 	bi->bi_op_modify = meta_back_modify;
79 	bi->bi_op_modrdn = meta_back_modrdn;
80 	bi->bi_op_add = meta_back_add;
81 	bi->bi_op_delete = meta_back_delete;
82 	bi->bi_op_abandon = 0;
83 
84 	bi->bi_extended = 0;
85 
86 	bi->bi_chk_referrals = 0;
87 
88 	bi->bi_connection_init = 0;
89 	bi->bi_connection_destroy = meta_back_conn_destroy;
90 
91 	return meta_back_init_cf( bi );
92 }
93 
94 int
95 meta_back_db_init(
96 	Backend		*be,
97 	ConfigReply	*cr)
98 {
99 	metainfo_t	*mi;
100 	int		i;
101 	BackendInfo	*bi;
102 
103 	bi = backend_info( "ldap" );
104 	if ( !bi || !bi->bi_extra ) {
105 		Debug( LDAP_DEBUG_ANY,
106 			"meta_back_db_init: needs back-ldap\n",
107 			0, 0, 0 );
108 		return 1;
109 	}
110 
111 	mi = ch_calloc( 1, sizeof( metainfo_t ) );
112 	if ( mi == NULL ) {
113  		return -1;
114  	}
115 
116 	/* set default flags */
117 	mi->mi_flags =
118 		META_BACK_F_DEFER_ROOTDN_BIND
119 		| META_BACK_F_PROXYAUTHZ_ALWAYS
120 		| META_BACK_F_PROXYAUTHZ_ANON
121 		| META_BACK_F_PROXYAUTHZ_NOANON;
122 
123 	/*
124 	 * At present the default is no default target;
125 	 * this may change
126 	 */
127 	mi->mi_defaulttarget = META_DEFAULT_TARGET_NONE;
128 	mi->mi_bind_timeout.tv_sec = 0;
129 	mi->mi_bind_timeout.tv_usec = META_BIND_TIMEOUT;
130 
131 	mi->mi_rebind_f = meta_back_default_rebind;
132 	mi->mi_urllist_f = meta_back_default_urllist;
133 
134 	ldap_pvt_thread_mutex_init( &mi->mi_conninfo.lai_mutex );
135 	ldap_pvt_thread_mutex_init( &mi->mi_cache.mutex );
136 
137 	/* safe default */
138 	mi->mi_nretries = META_RETRY_DEFAULT;
139 	mi->mi_version = LDAP_VERSION3;
140 
141 	for ( i = LDAP_BACK_PCONN_FIRST; i < LDAP_BACK_PCONN_LAST; i++ ) {
142 		mi->mi_conn_priv[ i ].mic_num = 0;
143 		LDAP_TAILQ_INIT( &mi->mi_conn_priv[ i ].mic_priv );
144 	}
145 	mi->mi_conn_priv_max = LDAP_BACK_CONN_PRIV_DEFAULT;
146 
147 	mi->mi_ldap_extra = (ldap_extra_t *)bi->bi_extra;
148 
149 	be->be_private = mi;
150 	be->be_cf_ocs = be->bd_info->bi_cf_ocs;
151 
152 	return 0;
153 }
154 
155 int
156 meta_target_finish(
157 	metainfo_t *mi,
158 	metatarget_t *mt,
159 	const char *log,
160 	char *msg,
161 	size_t msize
162 )
163 {
164 	slap_bindconf	sb = { BER_BVNULL };
165 	struct berval mapped;
166 	int rc;
167 
168 	ber_str2bv( mt->mt_uri, 0, 0, &sb.sb_uri );
169 	sb.sb_version = mt->mt_version;
170 	sb.sb_method = LDAP_AUTH_SIMPLE;
171 	BER_BVSTR( &sb.sb_binddn, "" );
172 
173 	if ( META_BACK_TGT_T_F_DISCOVER( mt ) ) {
174 		rc = slap_discover_feature( &sb,
175 				slap_schema.si_ad_supportedFeatures->ad_cname.bv_val,
176 				LDAP_FEATURE_ABSOLUTE_FILTERS );
177 		if ( rc == LDAP_COMPARE_TRUE ) {
178 			mt->mt_flags |= LDAP_BACK_F_T_F;
179 		}
180 	}
181 
182 	if ( META_BACK_TGT_CANCEL_DISCOVER( mt ) ) {
183 		rc = slap_discover_feature( &sb,
184 				slap_schema.si_ad_supportedExtension->ad_cname.bv_val,
185 				LDAP_EXOP_CANCEL );
186 		if ( rc == LDAP_COMPARE_TRUE ) {
187 			mt->mt_flags |= LDAP_BACK_F_CANCEL_EXOP;
188 		}
189 	}
190 
191 	if ( !( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE )
192 		|| mt->mt_idassert_authz != NULL )
193 	{
194 		mi->mi_flags &= ~META_BACK_F_PROXYAUTHZ_ALWAYS;
195 	}
196 
197 	if ( ( mt->mt_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL )
198 		&& !( mt->mt_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) )
199 	{
200 		snprintf( msg, msize,
201 			"%s: inconsistent idassert configuration "
202 			"(likely authz=\"*\" used with \"non-prescriptive\" flag)",
203 			log );
204 		Debug( LDAP_DEBUG_ANY, "%s (target %s)\n",
205 			msg, mt->mt_uri, 0 );
206 		return 1;
207 	}
208 
209 	if ( !( mt->mt_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL ) )
210 	{
211 		mi->mi_flags &= ~META_BACK_F_PROXYAUTHZ_ANON;
212 	}
213 
214 	if ( ( mt->mt_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) )
215 	{
216 		mi->mi_flags &= ~META_BACK_F_PROXYAUTHZ_NOANON;
217 	}
218 
219 	BER_BVZERO( &mapped );
220 	ldap_back_map( &mt->mt_rwmap.rwm_at,
221 		&slap_schema.si_ad_entryDN->ad_cname, &mapped,
222 		BACKLDAP_REMAP );
223 	if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
224 		mt->mt_rep_flags |= REP_NO_ENTRYDN;
225 	}
226 
227 	BER_BVZERO( &mapped );
228 	ldap_back_map( &mt->mt_rwmap.rwm_at,
229 		&slap_schema.si_ad_subschemaSubentry->ad_cname, &mapped,
230 		BACKLDAP_REMAP );
231 	if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
232 		mt->mt_rep_flags |= REP_NO_SUBSCHEMA;
233 	}
234 
235 	return 0;
236 }
237 
238 int
239 meta_back_db_open(
240 	Backend		*be,
241 	ConfigReply	*cr )
242 {
243 	metainfo_t	*mi = (metainfo_t *)be->be_private;
244 	char msg[SLAP_TEXT_BUFLEN];
245 
246 	int		i, rc;
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 			"meta_back_db_open: no targets defined\n",
257 			0, 0, 0 );
258 		return 1;
259 	}
260 
261 	for ( i = 0; i < mi->mi_ntargets; i++ ) {
262 		metatarget_t	*mt = mi->mi_targets[ i ];
263 
264 		if ( meta_target_finish( mi, mt,
265 			"meta_back_db_open", msg, sizeof( msg )))
266 			return 1;
267 	}
268 
269 	return 0;
270 }
271 
272 /*
273  * meta_back_conn_free()
274  *
275  * actually frees a connection; the reference count must be 0,
276  * and it must not (or no longer) be in the cache.
277  */
278 void
279 meta_back_conn_free(
280 	void 		*v_mc )
281 {
282 	metaconn_t		*mc = v_mc;
283 	int			ntargets;
284 
285 	assert( mc != NULL );
286 	assert( mc->mc_refcnt == 0 );
287 
288 	/* at least one must be present... */
289 	ntargets = mc->mc_info->mi_ntargets;
290 	assert( ntargets > 0 );
291 
292 	for ( ; ntargets--; ) {
293 		(void)meta_clear_one_candidate( NULL, mc, ntargets );
294 	}
295 
296 	if ( !BER_BVISNULL( &mc->mc_local_ndn ) ) {
297 		free( mc->mc_local_ndn.bv_val );
298 	}
299 
300 	free( mc );
301 }
302 
303 static void
304 mapping_free(
305 	void		*v_mapping )
306 {
307 	struct ldapmapping *mapping = v_mapping;
308 	ch_free( mapping->src.bv_val );
309 	ch_free( mapping->dst.bv_val );
310 	ch_free( mapping );
311 }
312 
313 static void
314 mapping_dst_free(
315 	void		*v_mapping )
316 {
317 	struct ldapmapping *mapping = v_mapping;
318 
319 	if ( BER_BVISEMPTY( &mapping->dst ) ) {
320 		mapping_free( &mapping[ -1 ] );
321 	}
322 }
323 
324 void
325 meta_back_map_free( struct ldapmap *lm )
326 {
327 	avl_free( lm->remap, mapping_dst_free );
328 	avl_free( lm->map, mapping_free );
329 	lm->remap = NULL;
330 	lm->map = NULL;
331 }
332 
333 static void
334 target_free(
335 	metatarget_t	*mt )
336 {
337 	if ( mt->mt_uri ) {
338 		free( mt->mt_uri );
339 		ldap_pvt_thread_mutex_destroy( &mt->mt_uri_mutex );
340 	}
341 	if ( mt->mt_subtree ) {
342 		meta_subtree_destroy( mt->mt_subtree );
343 		mt->mt_subtree = NULL;
344 	}
345 	if ( mt->mt_filter ) {
346 		meta_filter_destroy( mt->mt_filter );
347 		mt->mt_filter = NULL;
348 	}
349 	if ( !BER_BVISNULL( &mt->mt_psuffix ) ) {
350 		free( mt->mt_psuffix.bv_val );
351 	}
352 	if ( !BER_BVISNULL( &mt->mt_nsuffix ) ) {
353 		free( mt->mt_nsuffix.bv_val );
354 	}
355 	if ( !BER_BVISNULL( &mt->mt_binddn ) ) {
356 		free( mt->mt_binddn.bv_val );
357 	}
358 	if ( !BER_BVISNULL( &mt->mt_bindpw ) ) {
359 		free( mt->mt_bindpw.bv_val );
360 	}
361 	if ( !BER_BVISNULL( &mt->mt_idassert_authcID ) ) {
362 		ch_free( mt->mt_idassert_authcID.bv_val );
363 	}
364 	if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) ) {
365 		ch_free( mt->mt_idassert_authcDN.bv_val );
366 	}
367 	if ( !BER_BVISNULL( &mt->mt_idassert_passwd ) ) {
368 		ch_free( mt->mt_idassert_passwd.bv_val );
369 	}
370 	if ( !BER_BVISNULL( &mt->mt_idassert_authzID ) ) {
371 		ch_free( mt->mt_idassert_authzID.bv_val );
372 	}
373 	if ( !BER_BVISNULL( &mt->mt_idassert_sasl_mech ) ) {
374 		ch_free( mt->mt_idassert_sasl_mech.bv_val );
375 	}
376 	if ( !BER_BVISNULL( &mt->mt_idassert_sasl_realm ) ) {
377 		ch_free( mt->mt_idassert_sasl_realm.bv_val );
378 	}
379 	if ( mt->mt_idassert_authz != NULL ) {
380 		ber_bvarray_free( mt->mt_idassert_authz );
381 	}
382 	if ( mt->mt_rwmap.rwm_rw ) {
383 		rewrite_info_delete( &mt->mt_rwmap.rwm_rw );
384 		if ( mt->mt_rwmap.rwm_bva_rewrite )
385 			ber_bvarray_free( mt->mt_rwmap.rwm_bva_rewrite );
386 	}
387 	meta_back_map_free( &mt->mt_rwmap.rwm_oc );
388 	meta_back_map_free( &mt->mt_rwmap.rwm_at );
389 	ber_bvarray_free( mt->mt_rwmap.rwm_bva_map );
390 
391 	free( mt );
392 }
393 
394 int
395 meta_back_db_destroy(
396 	Backend		*be,
397 	ConfigReply	*cr )
398 {
399 	metainfo_t	*mi;
400 
401 	if ( be->be_private ) {
402 		int i;
403 
404 		mi = ( metainfo_t * )be->be_private;
405 
406 		/*
407 		 * Destroy the connection tree
408 		 */
409 		ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
410 
411 		if ( mi->mi_conninfo.lai_tree ) {
412 			avl_free( mi->mi_conninfo.lai_tree, meta_back_conn_free );
413 		}
414 		for ( i = LDAP_BACK_PCONN_FIRST; i < LDAP_BACK_PCONN_LAST; i++ ) {
415 			while ( !LDAP_TAILQ_EMPTY( &mi->mi_conn_priv[ i ].mic_priv ) ) {
416 				metaconn_t	*mc = LDAP_TAILQ_FIRST( &mi->mi_conn_priv[ i ].mic_priv );
417 
418 				LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ i ].mic_priv, mc, mc_q );
419 				meta_back_conn_free( mc );
420 			}
421 		}
422 
423 		/*
424 		 * Destroy the per-target stuff (assuming there's at
425 		 * least one ...)
426 		 */
427 		if ( mi->mi_targets != NULL ) {
428 			for ( i = 0; i < mi->mi_ntargets; i++ ) {
429 				metatarget_t	*mt = mi->mi_targets[ i ];
430 
431 				if ( META_BACK_TGT_QUARANTINE( mt ) ) {
432 					if ( mt->mt_quarantine.ri_num != mi->mi_quarantine.ri_num )
433 					{
434 						mi->mi_ldap_extra->retry_info_destroy( &mt->mt_quarantine );
435 					}
436 
437 					ldap_pvt_thread_mutex_destroy( &mt->mt_quarantine_mutex );
438 				}
439 
440 				target_free( mt );
441 			}
442 
443 			free( mi->mi_targets );
444 		}
445 
446 		ldap_pvt_thread_mutex_lock( &mi->mi_cache.mutex );
447 		if ( mi->mi_cache.tree ) {
448 			avl_free( mi->mi_cache.tree, meta_dncache_free );
449 		}
450 
451 		ldap_pvt_thread_mutex_unlock( &mi->mi_cache.mutex );
452 		ldap_pvt_thread_mutex_destroy( &mi->mi_cache.mutex );
453 
454 		ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
455 		ldap_pvt_thread_mutex_destroy( &mi->mi_conninfo.lai_mutex );
456 
457 		if ( mi->mi_candidates != NULL ) {
458 			ber_memfree_x( mi->mi_candidates, NULL );
459 		}
460 
461 		if ( META_BACK_QUARANTINE( mi ) ) {
462 			mi->mi_ldap_extra->retry_info_destroy( &mi->mi_quarantine );
463 		}
464 	}
465 
466 	free( be->be_private );
467 	return 0;
468 }
469 
470 #if SLAPD_META == SLAPD_MOD_DYNAMIC
471 
472 /* conditionally define the init_module() function */
473 SLAP_BACKEND_INIT_MODULE( meta )
474 
475 #endif /* SLAPD_META == SLAPD_MOD_DYNAMIC */
476 
477 
478