Debian Codegraph @

servers/slapd/back-asyncmeta/init.c

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