1 /* init.c - initialize monitor backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2001-2021 The OpenLDAP Foundation.
6  * Portions Copyright 2001-2003 Pierangelo Masarati.
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 file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Pierangelo Masarati for inclusion
19  * in OpenLDAP Software.
20  */
21 
22 #include "portable.h"
23 
24 #include <stdio.h>
25 #include <ac/string.h>
26 
27 #include <lutil.h>
28 #include "slap.h"
29 #include "slap-config.h"
30 #include "lber_pvt.h"
31 #include "back-monitor.h"
32 
33 #include "slap-config.h"
34 
35 #undef INTEGRATE_CORE_SCHEMA
36 
37 /*
38  * used by many functions to add description to entries
39  *
40  * WARNING: be_monitor may change as new databases are added,
41  * so it should not be used outside monitor_back_db_init()
42  * until monitor_back_db_open is called.
43  */
44 BackendDB			*be_monitor;
45 
46 static struct monitor_subsys_t	**monitor_subsys;
47 static int			monitor_subsys_opened;
48 static monitor_info_t		monitor_info;
49 static const monitor_extra_t monitor_extra = {
50 	monitor_back_is_configured,
51 	monitor_back_get_subsys,
52 	monitor_back_get_subsys_by_dn,
53 
54 	monitor_back_register_subsys,
55 	monitor_back_register_backend,
56 	monitor_back_register_database,
57 	monitor_back_register_overlay_info,
58 	monitor_back_register_overlay,
59 	monitor_back_register_entry,
60 	monitor_back_register_entry_parent,
61 	monitor_back_register_entry_attrs,
62 	monitor_back_register_entry_callback,
63 
64 	monitor_back_unregister_entry,
65 	monitor_back_unregister_entry_parent,
66 	monitor_back_unregister_entry_attrs,
67 	monitor_back_unregister_entry_callback,
68 
69 	monitor_back_entry_stub,
70 	monitor_back_entrypriv_create,
71 	monitor_back_register_subsys_late,
72 	monitor_back_entry_get_unlocked
73 };
74 
75 
76 /*
77  * subsystem data
78  *
79  * the known subsystems are added to the subsystems
80  * array at backend initialization; other subsystems
81  * may be added by calling monitor_back_register_subsys()
82  * before the database is opened (e.g. by other backends
83  * or by overlays or modules).
84  */
85 static struct monitor_subsys_t known_monitor_subsys[] = {
86 	{
87 		SLAPD_MONITOR_BACKEND_NAME,
88 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
89 		{ BER_BVC( "This subsystem contains information about available backends." ),
90 			BER_BVNULL },
91 		MONITOR_F_PERSISTENT_CH,
92 		monitor_subsys_backend_init,
93 		NULL,	/* destroy */
94 		NULL,   /* update */
95 		NULL,   /* create */
96 		NULL	/* modify */
97        	}, {
98 		SLAPD_MONITOR_CONN_NAME,
99 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
100 		{ BER_BVC( "This subsystem contains information about connections." ),
101 			BER_BVNULL },
102 		MONITOR_F_VOLATILE_CH,
103 		monitor_subsys_conn_init,
104 		NULL,	/* destroy */
105 		NULL,   /* update */
106 		NULL,   /* create */
107 		NULL	/* modify */
108        	}, {
109 		SLAPD_MONITOR_DATABASE_NAME,
110 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
111 		{ BER_BVC( "This subsystem contains information about configured databases." ),
112 			BER_BVNULL },
113 		MONITOR_F_PERSISTENT_CH,
114 		monitor_subsys_database_init,
115 		NULL,	/* destroy */
116 		NULL,   /* update */
117 		NULL,   /* create */
118 		NULL	/* modify */
119        	}, {
120 		SLAPD_MONITOR_LISTENER_NAME,
121 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
122 		{ BER_BVC( "This subsystem contains information about active listeners." ),
123 			BER_BVNULL },
124 		MONITOR_F_PERSISTENT_CH,
125 		monitor_subsys_listener_init,
126 		NULL,	/* destroy */
127 		NULL,	/* update */
128 		NULL,	/* create */
129 		NULL	/* modify */
130        	}, {
131 		SLAPD_MONITOR_LOG_NAME,
132 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
133 		{ BER_BVC( "This subsystem contains information about logging." ),
134 		  	BER_BVC( "Set the attribute \"managedInfo\" to the desired log levels." ),
135 			BER_BVNULL },
136 		MONITOR_F_NONE,
137 		monitor_subsys_log_init,
138 		NULL,	/* destroy */
139 		NULL,	/* update */
140 		NULL,   /* create */
141 		NULL,	/* modify */
142        	}, {
143 		SLAPD_MONITOR_OPS_NAME,
144 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
145 		{ BER_BVC( "This subsystem contains information about performed operations." ),
146 			BER_BVNULL },
147 		MONITOR_F_PERSISTENT_CH,
148 		monitor_subsys_ops_init,
149 		NULL,	/* destroy */
150 		NULL,	/* update */
151 		NULL,   /* create */
152 		NULL,	/* modify */
153        	}, {
154 		SLAPD_MONITOR_OVERLAY_NAME,
155 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
156 		{ BER_BVC( "This subsystem contains information about available overlays." ),
157 			BER_BVNULL },
158 		MONITOR_F_PERSISTENT_CH,
159 		monitor_subsys_overlay_init,
160 		NULL,	/* destroy */
161 		NULL,	/* update */
162 		NULL,   /* create */
163 		NULL,	/* modify */
164 	}, {
165 		SLAPD_MONITOR_SASL_NAME,
166 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
167 		{ BER_BVC( "This subsystem contains information about SASL." ),
168 			BER_BVNULL },
169 		MONITOR_F_NONE,
170 		NULL,   /* init */
171 		NULL,	/* destroy */
172 		NULL,   /* update */
173 		NULL,   /* create */
174 		NULL	/* modify */
175        	}, {
176 		SLAPD_MONITOR_SENT_NAME,
177 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
178 		{ BER_BVC( "This subsystem contains statistics." ),
179 			BER_BVNULL },
180 		MONITOR_F_PERSISTENT_CH,
181 		monitor_subsys_sent_init,
182 		NULL,	/* destroy */
183 		NULL,   /* update */
184 		NULL,   /* create */
185 		NULL,	/* modify */
186        	}, {
187 		SLAPD_MONITOR_THREAD_NAME,
188 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
189 		{ BER_BVC( "This subsystem contains information about threads." ),
190 			BER_BVNULL },
191 		MONITOR_F_PERSISTENT_CH,
192 		monitor_subsys_thread_init,
193 		NULL,	/* destroy */
194 		NULL,   /* update */
195 		NULL,   /* create */
196 		NULL	/* modify */
197        	}, {
198 		SLAPD_MONITOR_TIME_NAME,
199 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
200 		{ BER_BVC( "This subsystem contains information about time." ),
201 			BER_BVNULL },
202 		MONITOR_F_PERSISTENT_CH,
203 		monitor_subsys_time_init,
204 		NULL,	/* destroy */
205 		NULL,   /* update */
206 		NULL,   /* create */
207 		NULL,	/* modify */
208        	}, {
209 		SLAPD_MONITOR_TLS_NAME,
210 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
211 		{ BER_BVC( "This subsystem contains information about TLS." ),
212 			BER_BVNULL },
213 		MONITOR_F_NONE,
214 		NULL,   /* init */
215 		NULL,	/* destroy */
216 		NULL,   /* update */
217 		NULL,   /* create */
218 		NULL	/* modify */
219        	}, {
220 		SLAPD_MONITOR_RWW_NAME,
221 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
222 		{ BER_BVC( "This subsystem contains information about read/write waiters." ),
223 			BER_BVNULL },
224 		MONITOR_F_PERSISTENT_CH,
225 		monitor_subsys_rww_init,
226 		NULL,	/* destroy */
227 		NULL,   /* update */
228 		NULL, 	/* create */
229 		NULL	/* modify */
230        	}, { NULL }
231 };
232 
233 int
monitor_subsys_is_opened(void)234 monitor_subsys_is_opened( void )
235 {
236 	return monitor_subsys_opened;
237 }
238 
239 int
monitor_back_register_subsys(monitor_subsys_t * ms)240 monitor_back_register_subsys(
241 	monitor_subsys_t	*ms )
242 {
243 	int	i = 0;
244 
245 	if ( monitor_subsys ) {
246 		for ( ; monitor_subsys[ i ] != NULL; i++ )
247 			/* just count'em */ ;
248 	}
249 
250 	monitor_subsys = ch_realloc( monitor_subsys,
251 			( 2 + i ) * sizeof( monitor_subsys_t * ) );
252 
253 	if ( monitor_subsys == NULL ) {
254 		return -1;
255 	}
256 
257 	monitor_subsys[ i ] = ms;
258 	monitor_subsys[ i + 1 ] = NULL;
259 
260 	/* if a subsystem is registered __AFTER__ subsystem
261 	 * initialization (depending on the sequence the databases
262 	 * are listed in slapd.conf), init it */
263 	if ( monitor_subsys_is_opened() ) {
264 
265 		/* FIXME: this should only be possible
266 		 * if be_monitor is already initialized */
267 		assert( be_monitor != NULL );
268 
269 		if ( ms->mss_open && ( *ms->mss_open )( be_monitor, ms ) ) {
270 			return -1;
271 		}
272 
273 		ms->mss_flags |= MONITOR_F_OPENED;
274 	}
275 
276 	return 0;
277 }
278 
279 enum {
280 	LIMBO_ENTRY,
281 	LIMBO_ENTRY_PARENT,
282 	LIMBO_ATTRS,
283 	LIMBO_CB,
284 	LIMBO_BACKEND,
285 	LIMBO_DATABASE,
286 	LIMBO_OVERLAY_INFO,
287 	LIMBO_OVERLAY,
288 	LIMBO_SUBSYS,
289 
290 	LIMBO_LAST
291 };
292 
293 typedef struct entry_limbo_t {
294 	int			el_type;
295 	BackendInfo		*el_bi;
296 	BackendDB		*el_be;
297 	slap_overinst		*el_on;
298 	Entry			*el_e;
299 	Attribute		*el_a;
300 	struct berval		*el_ndn;
301 	struct berval		el_nbase;
302 	int			el_scope;
303 	struct berval		el_filter;
304 	monitor_callback_t	*el_cb;
305 	monitor_subsys_t	*el_mss;
306 	unsigned long		el_flags;
307 	struct entry_limbo_t	*el_next;
308 } entry_limbo_t;
309 
310 int
monitor_back_is_configured(void)311 monitor_back_is_configured( void )
312 {
313 	return be_monitor != NULL;
314 }
315 
316 int
monitor_back_register_subsys_late(monitor_subsys_t * ms)317 monitor_back_register_subsys_late(
318 	monitor_subsys_t	*ms )
319 {
320 	entry_limbo_t	**elpp, el = { 0 };
321 	monitor_info_t 	*mi;
322 
323 	if ( be_monitor == NULL ) {
324 		Debug( LDAP_DEBUG_ANY,
325 			"monitor_back_register_subsys_late: "
326 			"monitor database not configured.\n" );
327 		return -1;
328 	}
329 
330 	/* everything is ready, can register already */
331 	if ( monitor_subsys_is_opened() ) {
332 		return monitor_back_register_subsys( ms );
333 	}
334 
335 	mi = ( monitor_info_t * )be_monitor->be_private;
336 
337 
338 	el.el_type = LIMBO_SUBSYS;
339 
340 	el.el_mss = ms;
341 
342 	for ( elpp = &mi->mi_entry_limbo;
343 			*elpp;
344 			elpp = &(*elpp)->el_next )
345 		/* go to last */;
346 
347 	*elpp = (entry_limbo_t *)ch_malloc( sizeof( entry_limbo_t ) );
348 
349 	el.el_next = NULL;
350 	**elpp = el;
351 
352 	return 0;
353 }
354 
355 int
monitor_back_register_backend(BackendInfo * bi)356 monitor_back_register_backend(
357 	BackendInfo		*bi )
358 {
359 	return -1;
360 }
361 
362 int
monitor_back_register_overlay_info(slap_overinst * on)363 monitor_back_register_overlay_info(
364 	slap_overinst		*on )
365 {
366 	return -1;
367 }
368 
369 int
monitor_back_register_backend_limbo(BackendInfo * bi)370 monitor_back_register_backend_limbo(
371 	BackendInfo		*bi )
372 {
373 	return -1;
374 }
375 
376 int
monitor_back_register_database_limbo(BackendDB * be,struct berval * ndn_out)377 monitor_back_register_database_limbo(
378 	BackendDB		*be,
379 	struct berval		*ndn_out )
380 {
381 	entry_limbo_t	**elpp, el = { 0 };
382 	monitor_info_t 	*mi;
383 
384 	if ( be_monitor == NULL ) {
385 		Debug( LDAP_DEBUG_ANY,
386 			"monitor_back_register_database_limbo: "
387 			"monitor database not configured.\n" );
388 		return -1;
389 	}
390 
391 	mi = ( monitor_info_t * )be_monitor->be_private;
392 
393 
394 	el.el_type = LIMBO_DATABASE;
395 
396 	el.el_be = be->bd_self;
397 	el.el_ndn = ndn_out;
398 
399 	for ( elpp = &mi->mi_entry_limbo;
400 			*elpp;
401 			elpp = &(*elpp)->el_next )
402 		/* go to last */;
403 
404 	*elpp = (entry_limbo_t *)ch_malloc( sizeof( entry_limbo_t ) );
405 
406 	el.el_next = NULL;
407 	**elpp = el;
408 
409 	return 0;
410 }
411 
412 int
monitor_back_register_overlay_info_limbo(slap_overinst * on)413 monitor_back_register_overlay_info_limbo(
414 	slap_overinst		*on )
415 {
416 	return -1;
417 }
418 
419 int
monitor_back_register_overlay_limbo(BackendDB * be,struct slap_overinst * on,struct berval * ndn_out)420 monitor_back_register_overlay_limbo(
421 	BackendDB		*be,
422 	struct slap_overinst	*on,
423 	struct berval		*ndn_out )
424 {
425 	entry_limbo_t	**elpp, el = { 0 };
426 	monitor_info_t 	*mi;
427 
428 	if ( be_monitor == NULL ) {
429 		Debug( LDAP_DEBUG_ANY,
430 			"monitor_back_register_overlay_limbo: "
431 			"monitor database not configured.\n" );
432 		return -1;
433 	}
434 
435 	mi = ( monitor_info_t * )be_monitor->be_private;
436 
437 
438 	el.el_type = LIMBO_OVERLAY;
439 
440 	el.el_be = be->bd_self;
441 	el.el_on = on;
442 	el.el_ndn = ndn_out;
443 
444 	for ( elpp = &mi->mi_entry_limbo;
445 			*elpp;
446 			elpp = &(*elpp)->el_next )
447 		/* go to last */;
448 
449 	*elpp = (entry_limbo_t *)ch_malloc( sizeof( entry_limbo_t ) );
450 
451 	el.el_next = NULL;
452 	**elpp = el;
453 
454 	return 0;
455 }
456 
457 int
monitor_back_register_entry(Entry * e,monitor_callback_t * cb,monitor_subsys_t * mss,unsigned long flags)458 monitor_back_register_entry(
459 	Entry			*e,
460 	monitor_callback_t	*cb,
461 	monitor_subsys_t	*mss,
462 	unsigned long		flags )
463 {
464 	monitor_info_t 	*mi;
465 	int rc = 0;
466 
467 	if ( be_monitor == NULL ) {
468 		Debug( LDAP_DEBUG_ANY,
469 			"monitor_back_register_entry(\"%s\"): "
470 			"monitor database not configured.\n",
471 			e->e_name.bv_val );
472 		return -1;
473 	}
474 
475 	mi = ( monitor_info_t * )be_monitor->be_private;
476 
477 	assert( mi != NULL );
478 	assert( e != NULL );
479 	assert( e->e_private == NULL );
480 
481 	if ( monitor_subsys_is_opened() ) {
482 		Entry		*e_parent = NULL,
483 				*e_new = NULL,
484 				**ep = NULL;
485 		struct berval	pdn = BER_BVNULL;
486 		monitor_entry_t *mp = NULL,
487 				*mp_parent = NULL;
488 
489 		if ( monitor_cache_get( mi, &e->e_nname, &e_parent ) == 0 ) {
490 			/* entry exists */
491 			Debug( LDAP_DEBUG_ANY,
492 				"monitor_back_register_entry(\"%s\"): "
493 				"entry exists\n",
494 				e->e_name.bv_val );
495 			monitor_cache_release( mi, e_parent );
496 			return -1;
497 		}
498 
499 		dnParent( &e->e_nname, &pdn );
500 		if ( monitor_cache_get( mi, &pdn, &e_parent ) != 0 ) {
501 			/* parent does not exist */
502 			Debug( LDAP_DEBUG_ANY,
503 				"monitor_back_register_entry(\"%s\"): "
504 				"parent \"%s\" not found\n",
505 				e->e_name.bv_val, pdn.bv_val );
506 			return -1;
507 		}
508 
509 		assert( e_parent->e_private != NULL );
510 		mp_parent = ( monitor_entry_t * )e_parent->e_private;
511 
512 		if ( mp_parent->mp_flags & MONITOR_F_VOLATILE ) {
513 			/* entry is volatile; cannot append children */
514 			Debug( LDAP_DEBUG_ANY,
515 				"monitor_back_register_entry(\"%s\"): "
516 				"parent \"%s\" is volatile\n",
517 				e->e_name.bv_val, e_parent->e_name.bv_val );
518 			rc = -1;
519 			goto done;
520 		}
521 
522 		mp = monitor_entrypriv_create();
523 		if ( mp == NULL ) {
524 			Debug( LDAP_DEBUG_ANY,
525 				"monitor_back_register_entry(\"%s\"): "
526 				"monitor_entrypriv_create() failed\n",
527 				e->e_name.bv_val );
528 			rc = -1;
529 			goto done;
530 		}
531 
532 		e_new = entry_dup( e );
533 		if ( e_new == NULL ) {
534 			Debug( LDAP_DEBUG_ANY,
535 				"monitor_back_register_entry(\"%s\"): "
536 				"entry_dup() failed\n",
537 				e->e_name.bv_val );
538 			rc = -1;
539 			goto done;
540 		}
541 
542 		e_new->e_private = ( void * )mp;
543 		if ( mss != NULL ) {
544 			mp->mp_info = mss;
545 			mp->mp_flags = flags;
546 
547 		} else {
548 			mp->mp_info = mp_parent->mp_info;
549 			mp->mp_flags = mp_parent->mp_flags | MONITOR_F_SUB;
550 		}
551 		mp->mp_cb = cb;
552 
553 		ep = &mp_parent->mp_children;
554 		for ( ; *ep; ) {
555 			mp_parent = ( monitor_entry_t * )(*ep)->e_private;
556 			ep = &mp_parent->mp_next;
557 		}
558 		*ep = e_new;
559 
560 		if ( monitor_cache_add( mi, e_new ) ) {
561 			Debug( LDAP_DEBUG_ANY,
562 				"monitor_back_register_entry(\"%s\"): "
563 				"unable to add entry\n",
564 				e->e_name.bv_val );
565 			rc = -1;
566 			goto done;
567 		}
568 
569 done:;
570 		if ( rc ) {
571 			if ( mp ) {
572 				ch_free( mp );
573 			}
574 			if ( e_new ) {
575 				e_new->e_private = NULL;
576 				entry_free( e_new );
577 			}
578 		}
579 
580 		if ( e_parent ) {
581 			monitor_cache_release( mi, e_parent );
582 		}
583 
584 	} else {
585 		entry_limbo_t	**elpp, el = { 0 };
586 
587 		el.el_type = LIMBO_ENTRY;
588 
589 		el.el_e = entry_dup( e );
590 		if ( el.el_e == NULL ) {
591 			Debug( LDAP_DEBUG_ANY,
592 				"monitor_back_register_entry(\"%s\"): "
593 				"entry_dup() failed\n",
594 				e->e_name.bv_val );
595 			return -1;
596 		}
597 
598 		el.el_cb = cb;
599 		el.el_mss = mss;
600 		el.el_flags = flags;
601 
602 		for ( elpp = &mi->mi_entry_limbo;
603 				*elpp;
604 				elpp = &(*elpp)->el_next )
605 			/* go to last */;
606 
607 		*elpp = (entry_limbo_t *)ch_malloc( sizeof( entry_limbo_t ) );
608 		if ( *elpp == NULL ) {
609 			el.el_e->e_private = NULL;
610 			entry_free( el.el_e );
611 			return -1;
612 		}
613 
614 		el.el_next = NULL;
615 		**elpp = el;
616 	}
617 
618 	return rc;
619 }
620 
621 int
monitor_back_register_entry_parent(Entry * e,monitor_callback_t * cb,monitor_subsys_t * mss,unsigned long flags,struct berval * nbase,int scope,struct berval * filter)622 monitor_back_register_entry_parent(
623 	Entry			*e,
624 	monitor_callback_t	*cb,
625 	monitor_subsys_t	*mss,
626 	unsigned long		flags,
627 	struct berval		*nbase,
628 	int			scope,
629 	struct berval		*filter )
630 {
631 	monitor_info_t 	*mi;
632 	struct berval	ndn = BER_BVNULL;
633 
634 	if ( be_monitor == NULL ) {
635 		Debug( LDAP_DEBUG_ANY,
636 			"monitor_back_register_entry_parent(base=\"%s\" scope=%s filter=\"%s\"): "
637 			"monitor database not configured.\n",
638 			BER_BVISNULL( nbase ) ? "" : nbase->bv_val,
639 			ldap_pvt_scope2str( scope ),
640 			BER_BVISNULL( filter ) ? "" : filter->bv_val );
641 		return -1;
642 	}
643 
644 	mi = ( monitor_info_t * )be_monitor->be_private;
645 
646 	assert( mi != NULL );
647 	assert( e != NULL );
648 	assert( e->e_private == NULL );
649 
650 	if ( BER_BVISNULL( filter ) ) {
651 		/* need a filter */
652 		Debug( LDAP_DEBUG_ANY,
653 			"monitor_back_register_entry_parent(\"\"): "
654 			"need a valid filter\n" );
655 		return -1;
656 	}
657 
658 	if ( monitor_subsys_is_opened() ) {
659 		Entry		*e_parent = NULL,
660 				*e_new = NULL,
661 				**ep = NULL;
662 		struct berval	e_name = BER_BVNULL,
663 				e_nname = BER_BVNULL;
664 		monitor_entry_t *mp = NULL,
665 				*mp_parent = NULL;
666 		int		rc = 0;
667 
668 		if ( monitor_search2ndn( nbase, scope, filter, &ndn ) ) {
669 			/* entry does not exist */
670 			Debug( LDAP_DEBUG_ANY,
671 				"monitor_back_register_entry_parent(\"\"): "
672 				"base=\"%s\" scope=%s filter=\"%s\": "
673 				"unable to find entry\n",
674 				nbase->bv_val ? nbase->bv_val : "\"\"",
675 				ldap_pvt_scope2str( scope ),
676 				filter->bv_val );
677 			return -1;
678 		}
679 
680 		if ( monitor_cache_get( mi, &ndn, &e_parent ) != 0 ) {
681 			/* entry does not exist */
682 			Debug( LDAP_DEBUG_ANY,
683 				"monitor_back_register_entry_parent(\"%s\"): "
684 				"parent entry does not exist\n",
685 				ndn.bv_val );
686 			rc = -1;
687 			goto done;
688 		}
689 
690 		assert( e_parent->e_private != NULL );
691 		mp_parent = ( monitor_entry_t * )e_parent->e_private;
692 
693 		if ( mp_parent->mp_flags & MONITOR_F_VOLATILE ) {
694 			/* entry is volatile; cannot append callback */
695 			Debug( LDAP_DEBUG_ANY,
696 				"monitor_back_register_entry_parent(\"%s\"): "
697 				"entry is volatile\n",
698 				e_parent->e_name.bv_val );
699 			rc = -1;
700 			goto done;
701 		}
702 
703 		build_new_dn( &e_name, &e_parent->e_name, &e->e_name, NULL );
704 		build_new_dn( &e_nname, &e_parent->e_nname, &e->e_nname, NULL );
705 
706 		if ( monitor_cache_get( mi, &e_nname, &e_new ) == 0 ) {
707 			/* entry already exists */
708 			Debug( LDAP_DEBUG_ANY,
709 				"monitor_back_register_entry_parent(\"%s\"): "
710 				"entry already exists\n",
711 				e_name.bv_val );
712 			monitor_cache_release( mi, e_new );
713 			e_new = NULL;
714 			rc = -1;
715 			goto done;
716 		}
717 
718 		mp = monitor_entrypriv_create();
719 		if ( mp == NULL ) {
720 			Debug( LDAP_DEBUG_ANY,
721 				"monitor_back_register_entry_parent(\"%s\"): "
722 				"monitor_entrypriv_create() failed\n",
723 				e->e_name.bv_val );
724 			rc = -1;
725 			goto done;
726 		}
727 
728 		e_new = entry_dup( e );
729 		if ( e_new == NULL ) {
730 			Debug( LDAP_DEBUG_ANY,
731 				"monitor_back_register_entry(\"%s\"): "
732 				"entry_dup() failed\n",
733 				e->e_name.bv_val );
734 			rc = -1;
735 			goto done;
736 		}
737 		ch_free( e_new->e_name.bv_val );
738 		ch_free( e_new->e_nname.bv_val );
739 		e_new->e_name = e_name;
740 		e_new->e_nname = e_nname;
741 
742 		e_new->e_private = ( void * )mp;
743 		if ( mss != NULL ) {
744 			mp->mp_info = mss;
745 			mp->mp_flags = flags;
746 
747 		} else {
748 			mp->mp_info = mp_parent->mp_info;
749 			mp->mp_flags = mp_parent->mp_flags | MONITOR_F_SUB;
750 		}
751 		mp->mp_cb = cb;
752 
753 		ep = &mp_parent->mp_children;
754 		for ( ; *ep; ) {
755 			mp_parent = ( monitor_entry_t * )(*ep)->e_private;
756 			ep = &mp_parent->mp_next;
757 		}
758 		*ep = e_new;
759 
760 		if ( monitor_cache_add( mi, e_new ) ) {
761 			Debug( LDAP_DEBUG_ANY,
762 				"monitor_back_register_entry(\"%s\"): "
763 				"unable to add entry\n",
764 				e->e_name.bv_val );
765 			rc = -1;
766 			goto done;
767 		}
768 
769 done:;
770 		if ( !BER_BVISNULL( &ndn ) ) {
771 			ch_free( ndn.bv_val );
772 		}
773 
774 		if ( rc ) {
775 			if ( mp ) {
776 				ch_free( mp );
777 			}
778 			if ( e_new ) {
779 				e_new->e_private = NULL;
780 				entry_free( e_new );
781 			}
782 		}
783 
784 		if ( e_parent ) {
785 			monitor_cache_release( mi, e_parent );
786 		}
787 
788 	} else {
789 		entry_limbo_t	**elpp = NULL, el = { 0 };
790 
791 		el.el_type = LIMBO_ENTRY_PARENT;
792 
793 		el.el_e = entry_dup( e );
794 		if ( el.el_e == NULL ) {
795 			Debug( LDAP_DEBUG_ANY,
796 				"monitor_back_register_entry(\"%s\"): "
797 				"entry_dup() failed\n",
798 				e->e_name.bv_val );
799 			goto done_limbo;
800 		}
801 
802 		if ( !BER_BVISNULL( nbase ) ) {
803 			ber_dupbv( &el.el_nbase, nbase );
804 		}
805 
806 		el.el_scope = scope;
807 		if ( !BER_BVISNULL( filter ) ) {
808 			ber_dupbv( &el.el_filter, filter  );
809 		}
810 
811 		el.el_cb = cb;
812 		el.el_mss = mss;
813 		el.el_flags = flags;
814 
815 		for ( elpp = &mi->mi_entry_limbo;
816 				*elpp;
817 				elpp = &(*elpp)->el_next )
818 			/* go to last */;
819 
820 		*elpp = (entry_limbo_t *)ch_malloc( sizeof( entry_limbo_t ) );
821 		if ( *elpp == NULL ) {
822 			goto done_limbo;
823 		}
824 
825 done_limbo:;
826 		if ( *elpp != NULL ) {
827 			el.el_next = NULL;
828 			**elpp = el;
829 
830 		} else {
831 			if ( !BER_BVISNULL( &el.el_filter ) ) {
832 				ch_free( el.el_filter.bv_val );
833 			}
834 			if ( !BER_BVISNULL( &el.el_nbase ) ) {
835 				ch_free( el.el_nbase.bv_val );
836 			}
837 			entry_free( el.el_e );
838 			return -1;
839 		}
840 	}
841 
842 	return 0;
843 }
844 
845 static int
monitor_search2ndn_cb(Operation * op,SlapReply * rs)846 monitor_search2ndn_cb( Operation *op, SlapReply *rs )
847 {
848 	if ( rs->sr_type == REP_SEARCH ) {
849 		struct berval	*ndn = op->o_callback->sc_private;
850 
851 		if ( !BER_BVISNULL( ndn ) ) {
852 			rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
853 			ch_free( ndn->bv_val );
854 			BER_BVZERO( ndn );
855 			return rs->sr_err;
856 		}
857 
858 		ber_dupbv( ndn, &rs->sr_entry->e_nname );
859 	}
860 
861 	return 0;
862 }
863 
864 int
monitor_search2ndn(struct berval * nbase,int scope,struct berval * filter,struct berval * ndn)865 monitor_search2ndn(
866 	struct berval	*nbase,
867 	int		scope,
868 	struct berval	*filter,
869 	struct berval	*ndn )
870 {
871 	Connection	conn = { 0 };
872 	OperationBuffer	opbuf;
873 	Operation	*op;
874 	void	*thrctx;
875 	SlapReply	rs = { REP_RESULT };
876 	slap_callback	cb = { NULL, monitor_search2ndn_cb, NULL, NULL };
877 	int		rc;
878 
879 	BER_BVZERO( ndn );
880 
881 	if ( be_monitor == NULL ) {
882 		return -1;
883 	}
884 
885 	thrctx = ldap_pvt_thread_pool_context();
886 	connection_fake_init2( &conn, &opbuf, thrctx, 0 );
887 	op = &opbuf.ob_op;
888 
889 	op->o_tag = LDAP_REQ_SEARCH;
890 
891 	/* use global malloc for now */
892 	if ( op->o_tmpmemctx ) {
893 		op->o_tmpmemctx = NULL;
894 	}
895 	op->o_tmpmfuncs = &ch_mfuncs;
896 
897 	op->o_bd = be_monitor;
898 	if ( nbase == NULL || BER_BVISNULL( nbase ) ) {
899 		ber_dupbv_x( &op->o_req_dn, &op->o_bd->be_suffix[ 0 ],
900 				op->o_tmpmemctx );
901 		ber_dupbv_x( &op->o_req_ndn, &op->o_bd->be_nsuffix[ 0 ],
902 				op->o_tmpmemctx );
903 
904 	} else {
905 		if ( dnPrettyNormal( NULL, nbase, &op->o_req_dn, &op->o_req_ndn,
906 					op->o_tmpmemctx ) ) {
907 			return -1;
908 		}
909 	}
910 
911 	op->o_callback = &cb;
912 	cb.sc_private = (void *)ndn;
913 
914 	op->ors_scope = scope;
915 	op->ors_filter = str2filter_x( op, filter->bv_val );
916 	if ( op->ors_filter == NULL ) {
917 		rc = LDAP_OTHER;
918 		goto cleanup;
919 	}
920 	ber_dupbv_x( &op->ors_filterstr, filter, op->o_tmpmemctx );
921 	op->ors_attrs = slap_anlist_no_attrs;
922 	op->ors_attrsonly = 0;
923 	op->ors_tlimit = SLAP_NO_LIMIT;
924 	op->ors_slimit = 1;
925 	op->ors_limit = NULL;
926 	op->ors_deref = LDAP_DEREF_NEVER;
927 
928 	op->o_nocaching = 1;
929 	op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
930 
931 	op->o_dn = be_monitor->be_rootdn;
932 	op->o_ndn = be_monitor->be_rootndn;
933 
934 	rc = op->o_bd->be_search( op, &rs );
935 
936 cleanup:;
937 	if ( op->ors_filter != NULL ) {
938 		filter_free_x( op, op->ors_filter, 1 );
939 	}
940 	if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
941 		op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
942 	}
943 	if ( !BER_BVISNULL( &op->o_req_dn ) ) {
944 		op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
945 	}
946 	if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
947 		op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
948 	}
949 
950 	if ( rc != 0 ) {
951 		return rc;
952 	}
953 
954 	switch ( rs.sr_err ) {
955 	case LDAP_SUCCESS:
956 		if ( BER_BVISNULL( ndn ) ) {
957 			rc = -1;
958 		}
959 		break;
960 
961 	case LDAP_SIZELIMIT_EXCEEDED:
962 	default:
963 		if ( !BER_BVISNULL( ndn ) ) {
964 			ber_memfree( ndn->bv_val );
965 			BER_BVZERO( ndn );
966 		}
967 		rc = -1;
968 		break;
969 	}
970 
971 	return rc;
972 }
973 
974 int
monitor_back_register_entry_attrs(struct berval * ndn_in,Attribute * a,monitor_callback_t * cb,struct berval * nbase,int scope,struct berval * filter)975 monitor_back_register_entry_attrs(
976 	struct berval		*ndn_in,
977 	Attribute		*a,
978 	monitor_callback_t	*cb,
979 	struct berval		*nbase,
980 	int			scope,
981 	struct berval		*filter )
982 {
983 	monitor_info_t 	*mi;
984 	struct berval	ndn = BER_BVNULL;
985 	char		*fname = ( a == NULL ? "callback" : "attrs" );
986 	struct berval	empty_bv = BER_BVC("");
987 
988 	if ( nbase == NULL ) nbase = &empty_bv;
989 	if ( filter == NULL ) filter = &empty_bv;
990 
991 	if ( be_monitor == NULL ) {
992 		Debug(LDAP_DEBUG_ANY,
993 		      "monitor_back_register_entry_%s(base=\"%s\" scope=%s filter=\"%s\"): " "monitor database not configured.\n\n",
994 		      fname, BER_BVISNULL(nbase) ? "" : nbase->bv_val,
995 		      ldap_pvt_scope2str(scope),
996 		      BER_BVISNULL(filter) ? "" : filter->bv_val );
997 
998 		return -1;
999 	}
1000 
1001 	mi = ( monitor_info_t * )be_monitor->be_private;
1002 
1003 	assert( mi != NULL );
1004 
1005 	if ( ndn_in != NULL ) {
1006 		ndn = *ndn_in;
1007 	}
1008 
1009 	if ( a == NULL && cb == NULL ) {
1010 		/* nothing to do */
1011 		return -1;
1012 	}
1013 
1014 	if ( ( ndn_in == NULL || BER_BVISNULL( &ndn ) )
1015 			&& BER_BVISNULL( filter ) )
1016 	{
1017 		/* need a filter */
1018 		Debug( LDAP_DEBUG_ANY,
1019 			"monitor_back_register_entry_%s(\"\"): "
1020 			"need a valid filter\n",
1021 			fname );
1022 		return -1;
1023 	}
1024 
1025 	if ( monitor_subsys_is_opened() ) {
1026 		Entry			*e = NULL;
1027 		Attribute		**atp = NULL;
1028 		monitor_entry_t 	*mp = NULL;
1029 		monitor_callback_t	**mcp = NULL;
1030 		int			rc = 0;
1031 		int			freeit = 0;
1032 
1033 		if ( BER_BVISNULL( &ndn ) ) {
1034 			if ( monitor_search2ndn( nbase, scope, filter, &ndn ) ) {
1035 				Debug(LDAP_DEBUG_ANY,
1036 				      "monitor_back_register_entry_%s(\"\"): " "base=\"%s\" scope=%s filter=\"%s\": " "unable to find entry\n\n",
1037 				      fname,
1038 				      nbase->bv_val ? nbase->bv_val : "\"\"",
1039 				      ldap_pvt_scope2str(scope),
1040 				      filter->bv_val );
1041 				return -1;
1042 			}
1043 
1044 			freeit = 1;
1045 		}
1046 
1047 		if ( monitor_cache_get( mi, &ndn, &e ) != 0 ) {
1048 			/* entry does not exist */
1049 			Debug( LDAP_DEBUG_ANY,
1050 				"monitor_back_register_entry_%s(\"%s\"): "
1051 				"entry does not exist\n",
1052 				fname, ndn.bv_val );
1053 			rc = -1;
1054 			goto done;
1055 		}
1056 
1057 		assert( e->e_private != NULL );
1058 		mp = ( monitor_entry_t * )e->e_private;
1059 
1060 		if ( mp->mp_flags & MONITOR_F_VOLATILE ) {
1061 			/* entry is volatile; cannot append callback */
1062 			Debug( LDAP_DEBUG_ANY,
1063 				"monitor_back_register_entry_%s(\"%s\"): "
1064 				"entry is volatile\n",
1065 				fname, e->e_name.bv_val );
1066 			rc = -1;
1067 			goto done;
1068 		}
1069 
1070 		if ( a ) {
1071 			for ( atp = &e->e_attrs; *atp; atp = &(*atp)->a_next )
1072 				/* just get to last */ ;
1073 
1074 			for ( ; a != NULL; a = a->a_next ) {
1075 				assert( a->a_desc != NULL );
1076 				assert( a->a_vals != NULL );
1077 
1078 				if ( attr_find( e->e_attrs, a->a_desc ) ) {
1079 					attr_merge( e, a->a_desc, a->a_vals,
1080 						a->a_nvals == a->a_vals ? NULL : a->a_nvals );
1081 
1082 				} else {
1083 					*atp = attr_dup( a );
1084 					if ( *atp == NULL ) {
1085 						Debug( LDAP_DEBUG_ANY,
1086 							"monitor_back_register_entry_%s(\"%s\"): "
1087 							"attr_dup() failed\n",
1088 							fname, e->e_name.bv_val );
1089 						rc = -1;
1090 						goto done;
1091 					}
1092 					atp = &(*atp)->a_next;
1093 				}
1094 			}
1095 		}
1096 
1097 		if ( cb ) {
1098 			for ( mcp = &mp->mp_cb; *mcp; mcp = &(*mcp)->mc_next )
1099 				/* go to tail */ ;
1100 
1101 			/* NOTE: we do not clear cb->mc_next, so this function
1102 			 * can be used to append a list of callbacks */
1103 			(*mcp) = cb;
1104 		}
1105 
1106 done:;
1107 		if ( rc ) {
1108 			if ( atp && *atp ) {
1109 				attrs_free( *atp );
1110 				*atp = NULL;
1111 			}
1112 		}
1113 
1114 		if ( freeit ) {
1115 			ber_memfree( ndn.bv_val );
1116 		}
1117 
1118 		if ( e ) {
1119 			monitor_cache_release( mi, e );
1120 		}
1121 
1122 	} else {
1123 		entry_limbo_t	**elpp, el = { 0 };
1124 
1125 		el.el_type = LIMBO_ATTRS;
1126 		el.el_ndn = ndn_in;
1127 		if ( !BER_BVISNULL( nbase ) ) {
1128 			ber_dupbv( &el.el_nbase, nbase);
1129 		}
1130 		el.el_scope = scope;
1131 		if ( !BER_BVISNULL( filter ) ) {
1132 			ber_dupbv( &el.el_filter, filter  );
1133 		}
1134 
1135 		el.el_a = attrs_dup( a );
1136 		el.el_cb = cb;
1137 
1138 		for ( elpp = &mi->mi_entry_limbo;
1139 				*elpp;
1140 				elpp = &(*elpp)->el_next )
1141 			/* go to last */;
1142 
1143 		*elpp = (entry_limbo_t *)ch_malloc( sizeof( entry_limbo_t ) );
1144 		if ( *elpp == NULL ) {
1145 			if ( !BER_BVISNULL( &el.el_filter ) ) {
1146 				ch_free( el.el_filter.bv_val );
1147 			}
1148 			if ( el.el_a != NULL ) {
1149 				attrs_free( el.el_a );
1150 			}
1151 			if ( !BER_BVISNULL( &el.el_nbase ) ) {
1152 				ch_free( &el.el_nbase.bv_val );
1153 			}
1154 			return -1;
1155 		}
1156 
1157 		el.el_next = NULL;
1158 		**elpp = el;
1159 	}
1160 
1161 	return 0;
1162 }
1163 
1164 int
monitor_back_register_entry_callback(struct berval * ndn,monitor_callback_t * cb,struct berval * nbase,int scope,struct berval * filter)1165 monitor_back_register_entry_callback(
1166 	struct berval		*ndn,
1167 	monitor_callback_t	*cb,
1168 	struct berval		*nbase,
1169 	int			scope,
1170 	struct berval		*filter )
1171 {
1172 	return monitor_back_register_entry_attrs( ndn, NULL, cb,
1173 			nbase, scope, filter );
1174 }
1175 
1176 /*
1177  * TODO: add corresponding calls to remove installed callbacks, entries
1178  * and so, in case the entity that installed them is removed (e.g. a
1179  * database, via back-config)
1180  */
1181 int
monitor_back_unregister_entry(struct berval * ndn)1182 monitor_back_unregister_entry(
1183 	struct berval	*ndn )
1184 {
1185 	monitor_info_t 	*mi;
1186 
1187 	if ( be_monitor == NULL ) {
1188 		Debug( LDAP_DEBUG_ANY,
1189 			"monitor_back_unregister_entry(\"%s\"): "
1190 			"monitor database not configured.\n",
1191 			ndn->bv_val );
1192 
1193 		return -1;
1194 	}
1195 
1196 	/* entry will be regularly freed, and resources released
1197 	 * according to callbacks */
1198 	if ( slapd_shutdown ) {
1199 		return 0;
1200 	}
1201 
1202 	mi = ( monitor_info_t * )be_monitor->be_private;
1203 
1204 	assert( mi != NULL );
1205 
1206 	if ( monitor_subsys_is_opened() ) {
1207 		Entry			*e = NULL;
1208 		monitor_entry_t 	*mp = NULL;
1209 		monitor_callback_t	*cb = NULL;
1210 
1211 		if ( monitor_cache_remove( mi, ndn, &e ) != 0 ) {
1212 			/* entry does not exist */
1213 			Debug( LDAP_DEBUG_ANY,
1214 				"monitor_back_unregister_entry(\"%s\"): "
1215 				"entry removal failed.\n",
1216 				ndn->bv_val );
1217 			return -1;
1218 		}
1219 
1220 		mp = (monitor_entry_t *)e->e_private;
1221 		assert( mp != NULL );
1222 
1223 		for ( cb = mp->mp_cb; cb != NULL; ) {
1224 			monitor_callback_t	*next = cb->mc_next;
1225 
1226 			if ( cb->mc_free ) {
1227 				(void)cb->mc_free( e, &cb->mc_private );
1228 			}
1229 			ch_free( cb );
1230 
1231 			cb = next;
1232 		}
1233 
1234 		ch_free( mp );
1235 		e->e_private = NULL;
1236 		entry_free( e );
1237 
1238 	} else {
1239 		entry_limbo_t	**elpp;
1240 
1241 		for ( elpp = &mi->mi_entry_limbo;
1242 			*elpp;
1243 			elpp = &(*elpp)->el_next )
1244 		{
1245 			entry_limbo_t	*elp = *elpp;
1246 
1247 			if ( elp->el_type == LIMBO_ENTRY
1248 				&& dn_match( ndn, &elp->el_e->e_nname ) )
1249 			{
1250 				monitor_callback_t	*cb, *next;
1251 
1252 				for ( cb = elp->el_cb; cb; cb = next ) {
1253 					/* FIXME: call callbacks? */
1254 					next = cb->mc_next;
1255 					if ( cb->mc_dispose ) {
1256 						cb->mc_dispose( &cb->mc_private );
1257 					}
1258 					ch_free( cb );
1259 				}
1260 				assert( elp->el_e != NULL );
1261 				elp->el_e->e_private = NULL;
1262 				entry_free( elp->el_e );
1263 				*elpp = elp->el_next;
1264 				ch_free( elp );
1265 				elpp = NULL;
1266 				break;
1267 			}
1268 		}
1269 
1270 		if ( elpp != NULL ) {
1271 			/* not found!  where did it go? */
1272 			return 1;
1273 		}
1274 	}
1275 
1276 	return 0;
1277 }
1278 
1279 int
monitor_back_unregister_entry_parent(struct berval * nrdn,monitor_callback_t * target_cb,struct berval * nbase,int scope,struct berval * filter)1280 monitor_back_unregister_entry_parent(
1281 	struct berval		*nrdn,
1282 	monitor_callback_t	*target_cb,
1283 	struct berval		*nbase,
1284 	int			scope,
1285 	struct berval		*filter )
1286 {
1287 	monitor_info_t 	*mi;
1288 	struct berval	ndn = BER_BVNULL;
1289 
1290 	if ( be_monitor == NULL ) {
1291 		Debug( LDAP_DEBUG_ANY,
1292 			"monitor_back_unregister_entry_parent(base=\"%s\" scope=%s filter=\"%s\"): "
1293 			"monitor database not configured.\n",
1294 			BER_BVISNULL( nbase ) ? "" : nbase->bv_val,
1295 			ldap_pvt_scope2str( scope ),
1296 			BER_BVISNULL( filter ) ? "" : filter->bv_val );
1297 
1298 		return -1;
1299 	}
1300 
1301 	/* entry will be regularly freed, and resources released
1302 	 * according to callbacks */
1303 	if ( slapd_shutdown ) {
1304 		return 0;
1305 	}
1306 
1307 	mi = ( monitor_info_t * )be_monitor->be_private;
1308 
1309 	assert( mi != NULL );
1310 
1311 	if ( ( nrdn == NULL || BER_BVISNULL( nrdn ) )
1312 			&& BER_BVISNULL( filter ) )
1313 	{
1314 		/* need a filter */
1315 		Debug( LDAP_DEBUG_ANY,
1316 			"monitor_back_unregister_entry_parent(\"\"): "
1317 			"need a valid filter\n" );
1318 		return -1;
1319 	}
1320 
1321 	if ( monitor_subsys_is_opened() ) {
1322 		Entry			*e = NULL;
1323 		monitor_entry_t 	*mp = NULL;
1324 
1325 		if ( monitor_search2ndn( nbase, scope, filter, &ndn ) ) {
1326 			/* entry does not exist */
1327 			Debug( LDAP_DEBUG_ANY,
1328 				"monitor_back_unregister_entry_parent(\"\"): "
1329 				"base=\"%s\" scope=%s filter=\"%s\": "
1330 				"unable to find entry\n",
1331 				nbase->bv_val ? nbase->bv_val : "\"\"",
1332 				ldap_pvt_scope2str( scope ),
1333 				filter->bv_val );
1334 			return -1;
1335 		}
1336 
1337 		if ( monitor_cache_remove( mi, &ndn, &e ) != 0 ) {
1338 			/* entry does not exist */
1339 			Debug( LDAP_DEBUG_ANY,
1340 				"monitor_back_unregister_entry(\"%s\"): "
1341 				"entry removal failed.\n",
1342 				ndn.bv_val );
1343 			ber_memfree( ndn.bv_val );
1344 			return -1;
1345 		}
1346 		ber_memfree( ndn.bv_val );
1347 
1348 		mp = (monitor_entry_t *)e->e_private;
1349 		assert( mp != NULL );
1350 
1351 		if ( target_cb != NULL ) {
1352 			monitor_callback_t	**cbp;
1353 
1354 			for ( cbp = &mp->mp_cb; *cbp != NULL; cbp = &(*cbp)->mc_next ) {
1355 				if ( *cbp == target_cb ) {
1356 					if ( (*cbp)->mc_free ) {
1357 						(void)(*cbp)->mc_free( e, &(*cbp)->mc_private );
1358 					}
1359 					*cbp = (*cbp)->mc_next;
1360 					ch_free( target_cb );
1361 					break;
1362 				}
1363 			}
1364 		}
1365 
1366 
1367 		ch_free( mp );
1368 		e->e_private = NULL;
1369 		entry_free( e );
1370 
1371 	} else {
1372 		entry_limbo_t	**elpp;
1373 
1374 		for ( elpp = &mi->mi_entry_limbo;
1375 			*elpp;
1376 			elpp = &(*elpp)->el_next )
1377 		{
1378 			entry_limbo_t	*elp = *elpp;
1379 
1380 			if ( elp->el_type == LIMBO_ENTRY_PARENT
1381 				&& dn_match( nrdn, &elp->el_e->e_nname )
1382 				&& dn_match( nbase, &elp->el_nbase )
1383 				&& scope == elp->el_scope
1384 				&& bvmatch( filter, &elp->el_filter ) )
1385 			{
1386 				monitor_callback_t	*cb, *next;
1387 
1388 				for ( cb = elp->el_cb; cb; cb = next ) {
1389 					/* FIXME: call callbacks? */
1390 					next = cb->mc_next;
1391 					if ( cb->mc_dispose ) {
1392 						cb->mc_dispose( &cb->mc_private );
1393 					}
1394 					ch_free( cb );
1395 				}
1396 				assert( elp->el_e != NULL );
1397 				elp->el_e->e_private = NULL;
1398 				entry_free( elp->el_e );
1399 				if ( !BER_BVISNULL( &elp->el_nbase ) ) {
1400 					ch_free( elp->el_nbase.bv_val );
1401 				}
1402 				if ( !BER_BVISNULL( &elp->el_filter ) ) {
1403 					ch_free( elp->el_filter.bv_val );
1404 				}
1405 				*elpp = elp->el_next;
1406 				ch_free( elp );
1407 				elpp = NULL;
1408 				break;
1409 			}
1410 		}
1411 
1412 		if ( elpp != NULL ) {
1413 			/* not found!  where did it go? */
1414 			return 1;
1415 		}
1416 	}
1417 
1418 	return 0;
1419 }
1420 
1421 int
monitor_back_unregister_entry_attrs(struct berval * ndn_in,Attribute * target_a,monitor_callback_t * target_cb,struct berval * nbase,int scope,struct berval * filter)1422 monitor_back_unregister_entry_attrs(
1423 	struct berval		*ndn_in,
1424 	Attribute		*target_a,
1425 	monitor_callback_t	*target_cb,
1426 	struct berval		*nbase,
1427 	int			scope,
1428 	struct berval		*filter )
1429 {
1430 	monitor_info_t 	*mi;
1431 	struct berval	ndn = BER_BVNULL;
1432 	char		*fname = ( target_a == NULL ? "callback" : "attrs" );
1433 
1434 	if ( be_monitor == NULL ) {
1435 		Debug(LDAP_DEBUG_ANY,
1436 		      "monitor_back_unregister_entry_%s(base=\"%s\" scope=%s filter=\"%s\"): " "monitor database not configured.\n\n",
1437 		      fname, BER_BVISNULL(nbase) ? "" : nbase->bv_val,
1438 		      ldap_pvt_scope2str(scope),
1439 		      BER_BVISNULL(filter) ? "" : filter->bv_val );
1440 
1441 		return -1;
1442 	}
1443 
1444 	/* entry will be regularly freed, and resources released
1445 	 * according to callbacks */
1446 	if ( slapd_shutdown ) {
1447 		return 0;
1448 	}
1449 
1450 	mi = ( monitor_info_t * )be_monitor->be_private;
1451 
1452 	assert( mi != NULL );
1453 
1454 	if ( ndn_in != NULL ) {
1455 		ndn = *ndn_in;
1456 	}
1457 
1458 	if ( target_a == NULL && target_cb == NULL ) {
1459 		/* nothing to do */
1460 		return -1;
1461 	}
1462 
1463 	if ( ( ndn_in == NULL || BER_BVISNULL( &ndn ) )
1464 			&& BER_BVISNULL( filter ) )
1465 	{
1466 		/* need a filter */
1467 		Debug( LDAP_DEBUG_ANY,
1468 			"monitor_back_unregister_entry_%s(\"\"): "
1469 			"need a valid filter\n",
1470 			fname );
1471 		return -1;
1472 	}
1473 
1474 	if ( monitor_subsys_is_opened() ) {
1475 		Entry			*e = NULL;
1476 		monitor_entry_t 	*mp = NULL;
1477 		int			freeit = 0;
1478 
1479 		if ( BER_BVISNULL( &ndn ) ) {
1480 			if ( monitor_search2ndn( nbase, scope, filter, &ndn ) ) {
1481 				Debug(LDAP_DEBUG_ANY,
1482 				      "monitor_back_unregister_entry_%s(\"\"): " "base=\"%s\" scope=%d filter=\"%s\": " "unable to find entry\n\n",
1483 				      fname,
1484 				      nbase->bv_val ? nbase->bv_val : "\"\"",
1485 				      scope, filter->bv_val );
1486 				return -1;
1487 			}
1488 
1489 			freeit = 1;
1490 		}
1491 
1492 		if ( monitor_cache_get( mi, &ndn, &e ) != 0 ) {
1493 			/* entry does not exist */
1494 			Debug( LDAP_DEBUG_ANY,
1495 				"monitor_back_unregister_entry(\"%s\"): "
1496 				"entry removal failed.\n",
1497 				ndn.bv_val );
1498 			return -1;
1499 		}
1500 
1501 		mp = (monitor_entry_t *)e->e_private;
1502 		assert( mp != NULL );
1503 
1504 		if ( target_cb != NULL ) {
1505 			monitor_callback_t	**cbp;
1506 
1507 			for ( cbp = &mp->mp_cb; *cbp != NULL; cbp = &(*cbp)->mc_next ) {
1508 				if ( *cbp == target_cb ) {
1509 					if ( (*cbp)->mc_free ) {
1510 						(void)(*cbp)->mc_free( e, &(*cbp)->mc_private );
1511 					}
1512 					*cbp = (*cbp)->mc_next;
1513 					ch_free( target_cb );
1514 					break;
1515 				}
1516 			}
1517 		}
1518 
1519 		if ( target_a != NULL ) {
1520 			Attribute	*a;
1521 
1522 			for ( a = target_a; a != NULL; a = a->a_next ) {
1523 				Modification	mod = { 0 };
1524 				const char	*text;
1525 				char		textbuf[ SLAP_TEXT_BUFLEN ];
1526 
1527 				mod.sm_op = LDAP_MOD_DELETE;
1528 				mod.sm_desc = a->a_desc;
1529 				mod.sm_values = a->a_vals;
1530 				mod.sm_nvalues = a->a_nvals;
1531 
1532 				(void)modify_delete_values( e, &mod, 1,
1533 					&text, textbuf, sizeof( textbuf ) );
1534 			}
1535 		}
1536 
1537 		if ( freeit ) {
1538 			ber_memfree( ndn.bv_val );
1539 		}
1540 
1541 		monitor_cache_release( mi, e );
1542 
1543 	} else {
1544 		entry_limbo_t	**elpp;
1545 
1546 		for ( elpp = &mi->mi_entry_limbo;
1547 			*elpp;
1548 			elpp = &(*elpp)->el_next )
1549 		{
1550 			entry_limbo_t	*elp = *elpp;
1551 
1552 			if ( elp->el_type == LIMBO_ATTRS
1553 				&& dn_match( nbase, &elp->el_nbase )
1554 				&& scope == elp->el_scope
1555 				&& bvmatch( filter, &elp->el_filter ) )
1556 			{
1557 				monitor_callback_t	*cb, *next;
1558 
1559 				for ( cb = elp->el_cb; cb; cb = next ) {
1560 					/* FIXME: call callbacks? */
1561 					next = cb->mc_next;
1562 					if ( cb->mc_dispose ) {
1563 						cb->mc_dispose( &cb->mc_private );
1564 					}
1565 					ch_free( cb );
1566 				}
1567 				assert( elp->el_e == NULL );
1568 				if ( elp->el_a != NULL ) {
1569 					attrs_free( elp->el_a );
1570 				}
1571 				if ( !BER_BVISNULL( &elp->el_nbase ) ) {
1572 					ch_free( elp->el_nbase.bv_val );
1573 				}
1574 				if ( !BER_BVISNULL( &elp->el_filter ) ) {
1575 					ch_free( elp->el_filter.bv_val );
1576 				}
1577 				*elpp = elp->el_next;
1578 				ch_free( elp );
1579 				elpp = NULL;
1580 				break;
1581 			}
1582 		}
1583 
1584 		if ( elpp != NULL ) {
1585 			/* not found!  where did it go? */
1586 			return 1;
1587 		}
1588 	}
1589 
1590 	return 0;
1591 }
1592 
1593 int
monitor_back_unregister_entry_callback(struct berval * ndn,monitor_callback_t * cb,struct berval * nbase,int scope,struct berval * filter)1594 monitor_back_unregister_entry_callback(
1595 	struct berval		*ndn,
1596 	monitor_callback_t	*cb,
1597 	struct berval		*nbase,
1598 	int			scope,
1599 	struct berval		*filter )
1600 {
1601 	/* TODO: lookup entry (by ndn, if not NULL, and/or by callback);
1602 	 * unregister the callback; if a is not null, unregister the
1603 	 * given attrs.  In any case, call cb->cb_free */
1604 	return monitor_back_unregister_entry_attrs( ndn,
1605 		NULL, cb, nbase, scope, filter );
1606 }
1607 
1608 monitor_subsys_t *
monitor_back_get_subsys(const char * name)1609 monitor_back_get_subsys( const char *name )
1610 {
1611 	if ( monitor_subsys != NULL ) {
1612 		int	i;
1613 
1614 		for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
1615 			if ( strcasecmp( monitor_subsys[ i ]->mss_name, name ) == 0 ) {
1616 				return monitor_subsys[ i ];
1617 			}
1618 		}
1619 	}
1620 
1621 	return NULL;
1622 }
1623 
1624 monitor_subsys_t *
monitor_back_get_subsys_by_dn(struct berval * ndn,int sub)1625 monitor_back_get_subsys_by_dn(
1626 	struct berval	*ndn,
1627 	int		sub )
1628 {
1629 	if ( monitor_subsys != NULL ) {
1630 		int	i;
1631 
1632 		if ( sub ) {
1633 			for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
1634 				if ( dnIsSuffix( ndn, &monitor_subsys[ i ]->mss_ndn ) ) {
1635 					return monitor_subsys[ i ];
1636 				}
1637 			}
1638 
1639 		} else {
1640 			for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
1641 				if ( dn_match( ndn, &monitor_subsys[ i ]->mss_ndn ) ) {
1642 					return monitor_subsys[ i ];
1643 				}
1644 			}
1645 		}
1646 	}
1647 
1648 	return NULL;
1649 }
1650 
1651 int
monitor_back_initialize(BackendInfo * bi)1652 monitor_back_initialize(
1653 	BackendInfo	*bi )
1654 {
1655 	static char		*controls[] = {
1656 		LDAP_CONTROL_MANAGEDSAIT,
1657 		NULL
1658 	};
1659 
1660 	static ConfigTable monitorcfg[] = {
1661 		{ NULL, NULL, 0, 0, 0, ARG_IGNORED,
1662 			NULL, NULL, NULL, NULL }
1663 	};
1664 
1665 	static ConfigOCs monitorocs[] = {
1666 		{ "( OLcfgDbOc:4.1 "
1667 			"NAME 'olcMonitorConfig' "
1668 			"DESC 'Monitor backend configuration' "
1669 			"SUP olcDatabaseConfig "
1670 			")",
1671 			 	Cft_Database, monitorcfg },
1672 		{ NULL, 0, NULL }
1673 	};
1674 
1675 	struct m_s {
1676 		char	*schema;
1677 		slap_mask_t flags;
1678 		int	offset;
1679 	} moc[] = {
1680 		{ "( 1.3.6.1.4.1.4203.666.3.16.1 "
1681 			"NAME 'monitor' "
1682 			"DESC 'OpenLDAP system monitoring' "
1683 			"SUP top STRUCTURAL "
1684 			"MUST cn "
1685 			"MAY ( "
1686 				"description "
1687 				"$ seeAlso "
1688 				"$ labeledURI "
1689 				"$ monitoredInfo "
1690 				"$ managedInfo "
1691 				"$ monitorOverlay "
1692 			") )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1693 			offsetof(monitor_info_t, mi_oc_monitor) },
1694 		{ "( 1.3.6.1.4.1.4203.666.3.16.2 "
1695 			"NAME 'monitorServer' "
1696 			"DESC 'Server monitoring root entry' "
1697 			"SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1698 			offsetof(monitor_info_t, mi_oc_monitorServer) },
1699 		{ "( 1.3.6.1.4.1.4203.666.3.16.3 "
1700 			"NAME 'monitorContainer' "
1701 			"DESC 'monitor container class' "
1702 			"SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1703 			offsetof(monitor_info_t, mi_oc_monitorContainer) },
1704 		{ "( 1.3.6.1.4.1.4203.666.3.16.4 "
1705 			"NAME 'monitorCounterObject' "
1706 			"DESC 'monitor counter class' "
1707 			"SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1708 			offsetof(monitor_info_t, mi_oc_monitorCounterObject) },
1709 		{ "( 1.3.6.1.4.1.4203.666.3.16.5 "
1710 			"NAME 'monitorOperation' "
1711 			"DESC 'monitor operation class' "
1712 			"SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1713 			offsetof(monitor_info_t, mi_oc_monitorOperation) },
1714 		{ "( 1.3.6.1.4.1.4203.666.3.16.6 "
1715 			"NAME 'monitorConnection' "
1716 			"DESC 'monitor connection class' "
1717 			"SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1718 			offsetof(monitor_info_t, mi_oc_monitorConnection) },
1719 		{ "( 1.3.6.1.4.1.4203.666.3.16.7 "
1720 			"NAME 'managedObject' "
1721 			"DESC 'monitor managed entity class' "
1722 			"SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1723 			offsetof(monitor_info_t, mi_oc_managedObject) },
1724 		{ "( 1.3.6.1.4.1.4203.666.3.16.8 "
1725 			"NAME 'monitoredObject' "
1726 			"DESC 'monitor monitored entity class' "
1727 			"SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1728 			offsetof(monitor_info_t, mi_oc_monitoredObject) },
1729 		{ NULL, 0, -1 }
1730 	}, mat[] = {
1731 		{ "( 1.3.6.1.4.1.4203.666.1.55.1 "
1732 			"NAME 'monitoredInfo' "
1733 			"DESC 'monitored info' "
1734 			/* "SUP name " */
1735 			"EQUALITY caseIgnoreMatch "
1736 			"SUBSTR caseIgnoreSubstringsMatch "
1737 			"SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{32768} "
1738 			"NO-USER-MODIFICATION "
1739 			"USAGE dSAOperation )", SLAP_AT_HIDE,
1740 			offsetof(monitor_info_t, mi_ad_monitoredInfo) },
1741 		{ "( 1.3.6.1.4.1.4203.666.1.55.2 "
1742 			"NAME 'managedInfo' "
1743 			"DESC 'monitor managed info' "
1744 			"SUP name )", SLAP_AT_HIDE,
1745 			offsetof(monitor_info_t, mi_ad_managedInfo) },
1746 		{ "( 1.3.6.1.4.1.4203.666.1.55.3 "
1747 			"NAME 'monitorCounter' "
1748 			"DESC 'monitor counter' "
1749 			"EQUALITY integerMatch "
1750 			"ORDERING integerOrderingMatch "
1751 			"SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
1752 			"NO-USER-MODIFICATION "
1753 			"USAGE dSAOperation )", SLAP_AT_HIDE,
1754 			offsetof(monitor_info_t, mi_ad_monitorCounter) },
1755 		{ "( 1.3.6.1.4.1.4203.666.1.55.4 "
1756 			"NAME 'monitorOpCompleted' "
1757 			"DESC 'monitor completed operations' "
1758 			"SUP monitorCounter "
1759 			"NO-USER-MODIFICATION "
1760 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1761 			offsetof(monitor_info_t, mi_ad_monitorOpCompleted) },
1762 		{ "( 1.3.6.1.4.1.4203.666.1.55.5 "
1763 			"NAME 'monitorOpInitiated' "
1764 			"DESC 'monitor initiated operations' "
1765 			"SUP monitorCounter "
1766 			"NO-USER-MODIFICATION "
1767 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1768 			offsetof(monitor_info_t, mi_ad_monitorOpInitiated) },
1769 		{ "( 1.3.6.1.4.1.4203.666.1.55.6 "
1770 			"NAME 'monitorConnectionNumber' "
1771 			"DESC 'monitor connection number' "
1772 			"SUP monitorCounter "
1773 			"NO-USER-MODIFICATION "
1774 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1775 			offsetof(monitor_info_t, mi_ad_monitorConnectionNumber) },
1776 		{ "( 1.3.6.1.4.1.4203.666.1.55.7 "
1777 			"NAME 'monitorConnectionAuthzDN' "
1778 			"DESC 'monitor connection authorization DN' "
1779 			/* "SUP distinguishedName " */
1780 			"EQUALITY distinguishedNameMatch "
1781 			"SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
1782 			"NO-USER-MODIFICATION "
1783 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1784 			offsetof(monitor_info_t, mi_ad_monitorConnectionAuthzDN) },
1785 		{ "( 1.3.6.1.4.1.4203.666.1.55.8 "
1786 			"NAME 'monitorConnectionLocalAddress' "
1787 			"DESC 'monitor connection local address' "
1788 			"SUP monitoredInfo "
1789 			"NO-USER-MODIFICATION "
1790 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1791 			offsetof(monitor_info_t, mi_ad_monitorConnectionLocalAddress) },
1792 		{ "( 1.3.6.1.4.1.4203.666.1.55.9 "
1793 			"NAME 'monitorConnectionPeerAddress' "
1794 			"DESC 'monitor connection peer address' "
1795 			"SUP monitoredInfo "
1796 			"NO-USER-MODIFICATION "
1797 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1798 			offsetof(monitor_info_t, mi_ad_monitorConnectionPeerAddress) },
1799 		{ "( 1.3.6.1.4.1.4203.666.1.55.10 "
1800 			"NAME 'monitorTimestamp' "
1801 			"DESC 'monitor timestamp' "
1802 			"EQUALITY generalizedTimeMatch "
1803 			"ORDERING generalizedTimeOrderingMatch "
1804 			"SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
1805 			"SINGLE-VALUE "
1806 			"NO-USER-MODIFICATION "
1807 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1808 			offsetof(monitor_info_t, mi_ad_monitorTimestamp) },
1809 		{ "( 1.3.6.1.4.1.4203.666.1.55.11 "
1810 			"NAME 'monitorOverlay' "
1811 			"DESC 'name of overlays defined for a given database' "
1812 			"SUP monitoredInfo "
1813 			"NO-USER-MODIFICATION "
1814 			"USAGE dSAOperation )", SLAP_AT_HIDE,
1815 			offsetof(monitor_info_t, mi_ad_monitorOverlay) },
1816 		{ "( 1.3.6.1.4.1.4203.666.1.55.12 "
1817 			"NAME 'readOnly' "
1818 			"DESC 'read/write status of a given database' "
1819 			"EQUALITY booleanMatch "
1820 			"SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
1821 			"SINGLE-VALUE "
1822 			"USAGE dSAOperation )", SLAP_AT_HIDE,
1823 			offsetof(monitor_info_t, mi_ad_readOnly) },
1824 		{ "( 1.3.6.1.4.1.4203.666.1.55.13 "
1825 			"NAME 'restrictedOperation' "
1826 			"DESC 'name of restricted operation for a given database' "
1827 			"SUP managedInfo )", SLAP_AT_HIDE,
1828 			offsetof(monitor_info_t, mi_ad_restrictedOperation ) },
1829 		{ "( 1.3.6.1.4.1.4203.666.1.55.14 "
1830 			"NAME 'monitorConnectionProtocol' "
1831 			"DESC 'monitor connection protocol' "
1832 			"SUP monitoredInfo "
1833 			"NO-USER-MODIFICATION "
1834 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1835 			offsetof(monitor_info_t, mi_ad_monitorConnectionProtocol) },
1836 		{ "( 1.3.6.1.4.1.4203.666.1.55.15 "
1837 			"NAME 'monitorConnectionOpsReceived' "
1838 			"DESC 'monitor number of operations received by the connection' "
1839 			"SUP monitorCounter "
1840 			"NO-USER-MODIFICATION "
1841 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1842 			offsetof(monitor_info_t, mi_ad_monitorConnectionOpsReceived) },
1843 		{ "( 1.3.6.1.4.1.4203.666.1.55.16 "
1844 			"NAME 'monitorConnectionOpsExecuting' "
1845 			"DESC 'monitor number of operations in execution within the connection' "
1846 			"SUP monitorCounter "
1847 			"NO-USER-MODIFICATION "
1848 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1849 			offsetof(monitor_info_t, mi_ad_monitorConnectionOpsExecuting) },
1850 		{ "( 1.3.6.1.4.1.4203.666.1.55.17 "
1851 			"NAME 'monitorConnectionOpsPending' "
1852 			"DESC 'monitor number of pending operations within the connection' "
1853 			"SUP monitorCounter "
1854 			"NO-USER-MODIFICATION "
1855 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1856 			offsetof(monitor_info_t, mi_ad_monitorConnectionOpsPending) },
1857 		{ "( 1.3.6.1.4.1.4203.666.1.55.18 "
1858 			"NAME 'monitorConnectionOpsCompleted' "
1859 			"DESC 'monitor number of operations completed within the connection' "
1860 			"SUP monitorCounter "
1861 			"NO-USER-MODIFICATION "
1862 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1863 			offsetof(monitor_info_t, mi_ad_monitorConnectionOpsCompleted) },
1864 		{ "( 1.3.6.1.4.1.4203.666.1.55.19 "
1865 			"NAME 'monitorConnectionGet' "
1866 			"DESC 'number of times connection_get() was called so far' "
1867 			"SUP monitorCounter "
1868 			"NO-USER-MODIFICATION "
1869 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1870 			offsetof(monitor_info_t, mi_ad_monitorConnectionGet) },
1871 		{ "( 1.3.6.1.4.1.4203.666.1.55.20 "
1872 			"NAME 'monitorConnectionRead' "
1873 			"DESC 'number of times connection_read() was called so far' "
1874 			"SUP monitorCounter "
1875 			"NO-USER-MODIFICATION "
1876 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1877 			offsetof(monitor_info_t, mi_ad_monitorConnectionRead) },
1878 		{ "( 1.3.6.1.4.1.4203.666.1.55.21 "
1879 			"NAME 'monitorConnectionWrite' "
1880 			"DESC 'number of times connection_write() was called so far' "
1881 			"SUP monitorCounter "
1882 			"NO-USER-MODIFICATION "
1883 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1884 			offsetof(monitor_info_t, mi_ad_monitorConnectionWrite) },
1885 		{ "( 1.3.6.1.4.1.4203.666.1.55.22 "
1886 			"NAME 'monitorConnectionMask' "
1887 			"DESC 'monitor connection mask' "
1888 			"SUP monitoredInfo "
1889 			"NO-USER-MODIFICATION "
1890 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1891 			offsetof(monitor_info_t, mi_ad_monitorConnectionMask) },
1892 		{ "( 1.3.6.1.4.1.4203.666.1.55.23 "
1893 			"NAME 'monitorConnectionListener' "
1894 			"DESC 'monitor connection listener' "
1895 			"SUP monitoredInfo "
1896 			"NO-USER-MODIFICATION "
1897 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1898 			offsetof(monitor_info_t, mi_ad_monitorConnectionListener) },
1899 		{ "( 1.3.6.1.4.1.4203.666.1.55.24 "
1900 			"NAME 'monitorConnectionPeerDomain' "
1901 			"DESC 'monitor connection peer domain' "
1902 			"SUP monitoredInfo "
1903 			"NO-USER-MODIFICATION "
1904 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1905 			offsetof(monitor_info_t, mi_ad_monitorConnectionPeerDomain) },
1906 		{ "( 1.3.6.1.4.1.4203.666.1.55.25 "
1907 			"NAME 'monitorConnectionStartTime' "
1908 			"DESC 'monitor connection start time' "
1909 			"SUP monitorTimestamp "
1910 			"SINGLE-VALUE "
1911 			"NO-USER-MODIFICATION "
1912 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1913 			offsetof(monitor_info_t, mi_ad_monitorConnectionStartTime) },
1914 		{ "( 1.3.6.1.4.1.4203.666.1.55.26 "
1915 			"NAME 'monitorConnectionActivityTime' "
1916 			"DESC 'monitor connection activity time' "
1917 			"SUP monitorTimestamp "
1918 			"SINGLE-VALUE "
1919 			"NO-USER-MODIFICATION "
1920 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1921 			offsetof(monitor_info_t, mi_ad_monitorConnectionActivityTime) },
1922 		{ "( 1.3.6.1.4.1.4203.666.1.55.27 "
1923 			"NAME 'monitorIsShadow' "
1924 			"DESC 'TRUE if the database is shadow' "
1925 			"EQUALITY booleanMatch "
1926 			"SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
1927 			"SINGLE-VALUE "
1928 			"USAGE dSAOperation )", SLAP_AT_HIDE,
1929 			offsetof(monitor_info_t, mi_ad_monitorIsShadow) },
1930 		{ "( 1.3.6.1.4.1.4203.666.1.55.28 "
1931 			"NAME 'monitorUpdateRef' "
1932 			"DESC 'update referral for shadow databases' "
1933 			"SUP monitoredInfo "
1934 			"SINGLE-VALUE "
1935 			"USAGE dSAOperation )", SLAP_AT_HIDE,
1936 			offsetof(monitor_info_t, mi_ad_monitorUpdateRef) },
1937 		{ "( 1.3.6.1.4.1.4203.666.1.55.29 "
1938 			"NAME 'monitorRuntimeConfig' "
1939 			"DESC 'TRUE if component allows runtime configuration' "
1940 			"EQUALITY booleanMatch "
1941 			"SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
1942 			"SINGLE-VALUE "
1943 			"USAGE dSAOperation )", SLAP_AT_HIDE,
1944 			offsetof(monitor_info_t, mi_ad_monitorRuntimeConfig) },
1945 		{ "( 1.3.6.1.4.1.4203.666.1.55.30 "
1946 			"NAME 'monitorSuperiorDN' "
1947 			"DESC 'monitor superior DN' "
1948 			/* "SUP distinguishedName " */
1949 			"EQUALITY distinguishedNameMatch "
1950 			"SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
1951 			"NO-USER-MODIFICATION "
1952 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1953 			offsetof(monitor_info_t, mi_ad_monitorSuperiorDN) },
1954 		{ "( 1.3.6.1.4.1.4203.666.1.55.31 "
1955 			"NAME 'monitorConnectionOpsAsync' "
1956 			"DESC 'monitor number of asynchronous operations in execution within the connection' "
1957 			"SUP monitorCounter "
1958 			"NO-USER-MODIFICATION "
1959 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1960 			offsetof(monitor_info_t, mi_ad_monitorConnectionOpsAsync) },
1961 		{ NULL, 0, -1 }
1962 	};
1963 
1964 	static struct {
1965 		char			*name;
1966 		char			*oid;
1967 	}		s_oid[] = {
1968 		{ "olmAttributes",			"1.3.6.1.4.1.4203.666.1.55" },
1969 		{ "olmSubSystemAttributes",		"olmAttributes:0" },
1970 		{ "olmGenericAttributes",		"olmSubSystemAttributes:0" },
1971 		{ "olmDatabaseAttributes",		"olmSubSystemAttributes:1" },
1972 		{ "olmOverlayAttributes",		"olmSubSystemAttributes:2" },
1973 		{ "olmModuleAttributes",		"olmSubSystemAttributes:3" },
1974 
1975 		/* for example, back-mdb specific attrs
1976 		 * are in "olmDatabaseAttributes:12"
1977 		 *
1978 		 * NOTE: developers, please record here OID assignments
1979 		 * for other modules */
1980 
1981 		{ "olmObjectClasses",			"1.3.6.1.4.1.4203.666.3.16" },
1982 		{ "olmSubSystemObjectClasses",		"olmObjectClasses:0" },
1983 		{ "olmGenericObjectClasses",		"olmSubSystemObjectClasses:0" },
1984 		{ "olmDatabaseObjectClasses",		"olmSubSystemObjectClasses:1" },
1985 		{ "olmOverlayObjectClasses",		"olmSubSystemObjectClasses:2" },
1986 		{ "olmModuleObjectClasses",			"olmSubSystemObjectClasses:3" },
1987 
1988 		/* for example, back-mdb specific objectClasses
1989 		 * are in "olmDatabaseObjectClasses:12"
1990 		 *
1991 		 * NOTE: developers, please record here OID assignments
1992 		 * for other modules */
1993 
1994 		{ NULL }
1995 	};
1996 
1997 	int			i, rc;
1998 	monitor_info_t		*mi = &monitor_info;
1999 	ConfigArgs c;
2000 	char	*argv[ 3 ];
2001 
2002 	argv[ 0 ] = "monitor";
2003 	c.argv = argv;
2004 	c.argc = 3;
2005 	c.fname = argv[0];
2006 
2007 	for ( i = 0; s_oid[ i ].name; i++ ) {
2008 		argv[ 1 ] = s_oid[ i ].name;
2009 		argv[ 2 ] = s_oid[ i ].oid;
2010 
2011 		if ( parse_oidm( &c, 0, NULL ) != 0 ) {
2012 			Debug( LDAP_DEBUG_ANY,
2013 				"monitor_back_initialize: unable to add "
2014 				"objectIdentifier \"%s=%s\"\n",
2015 				s_oid[ i ].name, s_oid[ i ].oid );
2016 			return 1;
2017 		}
2018 	}
2019 
2020 	/* schema integration */
2021 	for ( i = 0; mat[ i ].schema; i++ ) {
2022 		int			code;
2023 		AttributeDescription **ad =
2024 			((AttributeDescription **)&(((char *)mi)[ mat[ i ].offset ]));
2025 
2026 		*ad = NULL;
2027 		code = register_at( mat[ i ].schema, ad, 0 );
2028 
2029 		if ( code ) {
2030 			Debug( LDAP_DEBUG_ANY,
2031 				"monitor_back_db_init: register_at failed\n" );
2032 			return -1;
2033 		}
2034 		(*ad)->ad_type->sat_flags |= mat[ i ].flags;
2035 	}
2036 
2037 	for ( i = 0; moc[ i ].schema; i++ ) {
2038 		int			code;
2039 		ObjectClass		**Oc =
2040 			((ObjectClass **)&(((char *)mi)[ moc[ i ].offset ]));
2041 
2042 		code = register_oc( moc[ i ].schema, Oc, 0 );
2043 		if ( code ) {
2044 			Debug( LDAP_DEBUG_ANY,
2045 				"monitor_back_db_init: register_oc failed\n" );
2046 			return -1;
2047 		}
2048 		(*Oc)->soc_flags |= moc[ i ].flags;
2049 	}
2050 
2051 	bi->bi_controls = controls;
2052 
2053 	bi->bi_init = 0;
2054 	bi->bi_open = 0;
2055 	bi->bi_config = monitor_back_config;
2056 	bi->bi_close = 0;
2057 	bi->bi_destroy = 0;
2058 
2059 	bi->bi_db_init = monitor_back_db_init;
2060 #if 0
2061 	bi->bi_db_config = monitor_back_db_config;
2062 #endif
2063 	bi->bi_db_open = monitor_back_db_open;
2064 	bi->bi_db_close = 0;
2065 	bi->bi_db_destroy = monitor_back_db_destroy;
2066 
2067 	bi->bi_op_bind = monitor_back_bind;
2068 	bi->bi_op_unbind = 0;
2069 	bi->bi_op_search = monitor_back_search;
2070 	bi->bi_op_compare = monitor_back_compare;
2071 	bi->bi_op_modify = monitor_back_modify;
2072 	bi->bi_op_modrdn = 0;
2073 	bi->bi_op_add = 0;
2074 	bi->bi_op_delete = 0;
2075 	bi->bi_op_abandon = 0;
2076 
2077 	bi->bi_extended = 0;
2078 
2079 	bi->bi_entry_release_rw = monitor_back_release;
2080 	bi->bi_chk_referrals = 0;
2081 	bi->bi_operational = monitor_back_operational;
2082 
2083 	/*
2084 	 * hooks for slap tools
2085 	 */
2086 	bi->bi_tool_entry_open = 0;
2087 	bi->bi_tool_entry_close = 0;
2088 	bi->bi_tool_entry_first = 0;
2089 	bi->bi_tool_entry_first_x = 0;
2090 	bi->bi_tool_entry_next = 0;
2091 	bi->bi_tool_entry_get = 0;
2092 	bi->bi_tool_entry_put = 0;
2093 	bi->bi_tool_entry_reindex = 0;
2094 	bi->bi_tool_sync = 0;
2095 	bi->bi_tool_dn2id_get = 0;
2096 	bi->bi_tool_entry_modify = 0;
2097 
2098 	bi->bi_connection_init = 0;
2099 	bi->bi_connection_destroy = 0;
2100 
2101 	bi->bi_extra = (void *)&monitor_extra;
2102 
2103 	/*
2104 	 * configuration objectClasses (fake)
2105 	 */
2106 	bi->bi_cf_ocs = monitorocs;
2107 
2108 	rc = config_register_schema( monitorcfg, monitorocs );
2109 	if ( rc ) {
2110 		return rc;
2111 	}
2112 
2113 	return 0;
2114 }
2115 
2116 int
monitor_back_db_init(BackendDB * be,ConfigReply * c)2117 monitor_back_db_init(
2118 	BackendDB	*be,
2119 	ConfigReply	*c)
2120 {
2121 	int			rc;
2122 	struct berval		dn = BER_BVC( SLAPD_MONITOR_DN ),
2123 				pdn,
2124 				ndn;
2125 	BackendDB		*be2;
2126 
2127 	monitor_subsys_t	*ms;
2128 
2129 	/*
2130 	 * database monitor can be defined once only
2131 	 */
2132 	if ( be_monitor != NULL ) {
2133 		if (c) {
2134 			snprintf(c->msg, sizeof(c->msg),"only one monitor database allowed");
2135 		}
2136 		return( -1 );
2137 	}
2138 	be_monitor = be;
2139 
2140 	/*
2141 	 * register subsys
2142 	 */
2143 	for ( ms = known_monitor_subsys; ms->mss_name != NULL; ms++ ) {
2144 		if ( monitor_back_register_subsys( ms ) ) {
2145 			return -1;
2146 		}
2147 	}
2148 
2149 	/* indicate system schema supported */
2150 	SLAP_BFLAGS(be) |= SLAP_BFLAG_MONITOR;
2151 
2152 	rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn, NULL );
2153 	if( rc != LDAP_SUCCESS ) {
2154 		Debug( LDAP_DEBUG_ANY,
2155 			"unable to normalize/pretty monitor DN \"%s\" (%d)\n",
2156 			dn.bv_val, rc );
2157 		return -1;
2158 	}
2159 
2160 	ber_bvarray_add( &be->be_suffix, &pdn );
2161 	ber_bvarray_add( &be->be_nsuffix, &ndn );
2162 
2163 	/* NOTE: only one monitor database is allowed,
2164 	 * so we use static storage */
2165 	ldap_pvt_thread_mutex_init( &monitor_info.mi_cache_mutex );
2166 
2167 	be->be_private = &monitor_info;
2168 
2169 	be2 = select_backend( &ndn, 0 );
2170 	if ( be2 != be ) {
2171 		char	*type = be2->bd_info->bi_type;
2172 
2173 		if ( overlay_is_over( be2 ) ) {
2174 			slap_overinfo	*oi = (slap_overinfo *)be2->bd_info->bi_private;
2175 			type = oi->oi_orig->bi_type;
2176 		}
2177 
2178 		if (c) {
2179 			snprintf(c->msg, sizeof(c->msg),
2180 					"\"monitor\" database serving namingContext \"%s\" "
2181 					"is hidden by \"%s\" database serving namingContext \"%s\".\n",
2182 					pdn.bv_val, type, be2->be_nsuffix[ 0 ].bv_val );
2183 		}
2184 		return -1;
2185 	}
2186 
2187 	return 0;
2188 }
2189 
2190 static void
monitor_back_destroy_limbo_entry(entry_limbo_t * el,int dispose)2191 monitor_back_destroy_limbo_entry(
2192 	entry_limbo_t	*el,
2193 	int		dispose )
2194 {
2195 	if ( el->el_e ) {
2196 		entry_free( el->el_e );
2197 	}
2198 	if ( el->el_a ) {
2199 		attrs_free( el->el_a );
2200 	}
2201 	if ( !BER_BVISNULL( &el->el_nbase ) ) {
2202 		ber_memfree( el->el_nbase.bv_val );
2203 	}
2204 	if ( !BER_BVISNULL( &el->el_filter ) ) {
2205 		ber_memfree( el->el_filter.bv_val );
2206 	}
2207 
2208 	/* NOTE: callbacks are not copied; so only free them
2209 	 * if disposing of */
2210 	if ( el->el_cb && dispose != 0 ) {
2211 		monitor_callback_t *next;
2212 
2213 		for ( ; el->el_cb; el->el_cb = next ) {
2214 			next = el->el_cb->mc_next;
2215 			if ( el->el_cb->mc_dispose ) {
2216 				el->el_cb->mc_dispose( &el->el_cb->mc_private );
2217 			}
2218 			ch_free( el->el_cb );
2219 		}
2220 	}
2221 
2222 	ch_free( el );
2223 }
2224 
2225 int
monitor_back_db_open(BackendDB * be,ConfigReply * cr)2226 monitor_back_db_open(
2227 	BackendDB	*be,
2228 	ConfigReply	*cr)
2229 {
2230 	monitor_info_t 		*mi = (monitor_info_t *)be->be_private;
2231 	struct monitor_subsys_t	**ms;
2232 	Entry 			*e, **ep, *root;
2233 	monitor_entry_t		*mp;
2234 	int			i;
2235 	struct berval		bv, rdn = BER_BVC(SLAPD_MONITOR_DN);
2236 	struct tm		tms;
2237 	static char		tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
2238 	struct berval	desc[] = {
2239 		BER_BVC("This subtree contains monitoring/managing objects."),
2240 		BER_BVC("This object contains information about this server."),
2241 		BER_BVC("Most of the information is held in operational"
2242 		" attributes, which must be explicitly requested."),
2243 		BER_BVNULL };
2244 
2245 	int			retcode = 0;
2246 
2247 	assert( be_monitor != NULL );
2248 	if ( be != be_monitor ) {
2249 		be_monitor = be;
2250 	}
2251 
2252 	/*
2253 	 * Start
2254 	 */
2255 	ldap_pvt_gmtime( &starttime, &tms );
2256 	lutil_gentime( tmbuf, sizeof(tmbuf), &tms );
2257 
2258 	mi->mi_startTime.bv_val = tmbuf;
2259 	mi->mi_startTime.bv_len = strlen( tmbuf );
2260 
2261 	if ( BER_BVISEMPTY( &be->be_rootdn ) ) {
2262 		BER_BVSTR( &mi->mi_creatorsName, SLAPD_ANONYMOUS );
2263 		BER_BVSTR( &mi->mi_ncreatorsName, SLAPD_ANONYMOUS );
2264 	} else {
2265 		mi->mi_creatorsName = be->be_rootdn;
2266 		mi->mi_ncreatorsName = be->be_rootndn;
2267 	}
2268 
2269 	/*
2270 	 * creates the "cn=Monitor" entry
2271 	 */
2272 	e = monitor_entry_stub( NULL, NULL, &rdn, mi->mi_oc_monitorServer,
2273 		NULL, NULL );
2274 
2275 	if ( e == NULL) {
2276 		Debug( LDAP_DEBUG_ANY,
2277 			"unable to create \"%s\" entry\n",
2278 			SLAPD_MONITOR_DN );
2279 		return( -1 );
2280 	}
2281 
2282 	attr_merge_normalize( e, slap_schema.si_ad_description, desc, NULL );
2283 
2284 	bv.bv_val = strchr( (char *) Versionstr, '$' );
2285 	if ( bv.bv_val != NULL ) {
2286 		char	*end;
2287 
2288 		bv.bv_val++;
2289 		for ( ; bv.bv_val[ 0 ] == ' '; bv.bv_val++ )
2290 			;
2291 
2292 		end = strchr( bv.bv_val, '$' );
2293 		if ( end != NULL ) {
2294 			end--;
2295 
2296 			for ( ; end > bv.bv_val && end[ 0 ] == ' '; end-- )
2297 				;
2298 
2299 			end++;
2300 
2301 			bv.bv_len = end - bv.bv_val;
2302 
2303 		} else {
2304 			bv.bv_len = strlen( bv.bv_val );
2305 		}
2306 
2307 		if ( attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo,
2308 					&bv, NULL ) ) {
2309 			Debug( LDAP_DEBUG_ANY,
2310 				"unable to add monitoredInfo to \"%s\" entry\n",
2311 				SLAPD_MONITOR_DN );
2312 			return( -1 );
2313 		}
2314 	}
2315 
2316 	mp = monitor_entrypriv_create();
2317 	if ( mp == NULL ) {
2318 		return -1;
2319 	}
2320 	e->e_private = ( void * )mp;
2321 	ep = &mp->mp_children;
2322 
2323 	if ( monitor_cache_add( mi, e ) ) {
2324 		Debug( LDAP_DEBUG_ANY,
2325 			"unable to add entry \"%s\" to cache\n",
2326 			SLAPD_MONITOR_DN );
2327 		return -1;
2328 	}
2329 	root = e;
2330 
2331 	/*
2332 	 * Create all the subsystem specific entries
2333 	 */
2334 	for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
2335 		int 		len = strlen( monitor_subsys[ i ]->mss_name );
2336 		struct berval	dn;
2337 		int		rc;
2338 
2339 		dn.bv_len = len + sizeof( "cn=" ) - 1;
2340 		dn.bv_val = ch_calloc( sizeof( char ), dn.bv_len + 1 );
2341 		strcpy( dn.bv_val, "cn=" );
2342 		strcat( dn.bv_val, monitor_subsys[ i ]->mss_name );
2343 		rc = dnPretty( NULL, &dn, &monitor_subsys[ i ]->mss_rdn, NULL );
2344 		free( dn.bv_val );
2345 		if ( rc != LDAP_SUCCESS ) {
2346 			Debug( LDAP_DEBUG_ANY,
2347 				"monitor RDN \"%s\" is invalid\n",
2348 				dn.bv_val );
2349 			return( -1 );
2350 		}
2351 
2352 		e = monitor_entry_stub( &root->e_name, &root->e_nname,
2353 			&monitor_subsys[ i ]->mss_rdn, mi->mi_oc_monitorContainer,
2354 			NULL, NULL );
2355 
2356 		if ( e == NULL) {
2357 			Debug( LDAP_DEBUG_ANY,
2358 				"unable to create \"%s\" entry\n",
2359 				monitor_subsys[ i ]->mss_dn.bv_val );
2360 			return( -1 );
2361 		}
2362 		monitor_subsys[i]->mss_dn = e->e_name;
2363 		monitor_subsys[i]->mss_ndn = e->e_nname;
2364 
2365 		if ( !BER_BVISNULL( &monitor_subsys[ i ]->mss_desc[ 0 ] ) ) {
2366 			attr_merge_normalize( e, slap_schema.si_ad_description,
2367 					monitor_subsys[ i ]->mss_desc, NULL );
2368 		}
2369 
2370 		mp = monitor_entrypriv_create();
2371 		if ( mp == NULL ) {
2372 			return -1;
2373 		}
2374 		e->e_private = ( void * )mp;
2375 		mp->mp_info = monitor_subsys[ i ];
2376 		mp->mp_flags = monitor_subsys[ i ]->mss_flags;
2377 
2378 		if ( monitor_cache_add( mi, e ) ) {
2379 			Debug( LDAP_DEBUG_ANY,
2380 				"unable to add entry \"%s\" to cache\n",
2381 				monitor_subsys[ i ]->mss_dn.bv_val );
2382 			return -1;
2383 		}
2384 
2385 		*ep = e;
2386 		ep = &mp->mp_next;
2387 	}
2388 
2389 	assert( be != NULL );
2390 
2391 	be->be_private = mi;
2392 
2393 	/*
2394 	 * opens the monitor backend subsystems
2395 	 */
2396 	for ( ms = monitor_subsys; ms[ 0 ] != NULL; ms++ ) {
2397 		if ( ms[ 0 ]->mss_open && ms[ 0 ]->mss_open( be, ms[ 0 ] ) ) {
2398 			return( -1 );
2399 		}
2400 		ms[ 0 ]->mss_flags |= MONITOR_F_OPENED;
2401 	}
2402 
2403 	monitor_subsys_opened = 1;
2404 
2405 	if ( mi->mi_entry_limbo ) {
2406 		entry_limbo_t	*el = mi->mi_entry_limbo;
2407 
2408 		for ( ; el; ) {
2409 			entry_limbo_t	*tmp;
2410 			int		rc;
2411 
2412 			switch ( el->el_type ) {
2413 			case LIMBO_ENTRY:
2414 				rc = monitor_back_register_entry(
2415 						el->el_e,
2416 						el->el_cb,
2417 						el->el_mss,
2418 						el->el_flags );
2419 				break;
2420 
2421 			case LIMBO_ENTRY_PARENT:
2422 				rc = monitor_back_register_entry_parent(
2423 						el->el_e,
2424 						el->el_cb,
2425 						el->el_mss,
2426 						el->el_flags,
2427 						&el->el_nbase,
2428 						el->el_scope,
2429 						&el->el_filter );
2430 				break;
2431 
2432 
2433 			case LIMBO_ATTRS:
2434 				rc = monitor_back_register_entry_attrs(
2435 						el->el_ndn,
2436 						el->el_a,
2437 						el->el_cb,
2438 						&el->el_nbase,
2439 						el->el_scope,
2440 						&el->el_filter );
2441 				break;
2442 
2443 			case LIMBO_CB:
2444 				rc = monitor_back_register_entry_callback(
2445 						el->el_ndn,
2446 						el->el_cb,
2447 						&el->el_nbase,
2448 						el->el_scope,
2449 						&el->el_filter );
2450 				break;
2451 
2452 			case LIMBO_BACKEND:
2453 				rc = monitor_back_register_backend( el->el_bi );
2454 				break;
2455 
2456 			case LIMBO_DATABASE:
2457 				rc = monitor_back_register_database( el->el_be, el->el_ndn );
2458 				break;
2459 
2460 			case LIMBO_OVERLAY_INFO:
2461 				rc = monitor_back_register_overlay_info( el->el_on );
2462 				break;
2463 
2464 			case LIMBO_OVERLAY:
2465 				rc = monitor_back_register_overlay( el->el_be, el->el_on, el->el_ndn );
2466 				break;
2467 
2468 			case LIMBO_SUBSYS:
2469 				rc = monitor_back_register_subsys( el->el_mss );
2470 				break;
2471 
2472 			default:
2473 				assert( 0 );
2474 			}
2475 
2476 			tmp = el;
2477 			el = el->el_next;
2478 			monitor_back_destroy_limbo_entry( tmp, rc );
2479 
2480 			if ( rc != 0 ) {
2481 				/* try all, but report error at end */
2482 				retcode = 1;
2483 			}
2484 		}
2485 
2486 		mi->mi_entry_limbo = NULL;
2487 	}
2488 
2489 	return retcode;
2490 }
2491 
2492 int
monitor_back_config(BackendInfo * bi,const char * fname,int lineno,int argc,char ** argv)2493 monitor_back_config(
2494 	BackendInfo	*bi,
2495 	const char	*fname,
2496 	int		lineno,
2497 	int		argc,
2498 	char		**argv )
2499 {
2500 	/*
2501 	 * eventually, will hold backend specific configuration parameters
2502 	 */
2503 	return SLAP_CONF_UNKNOWN;
2504 }
2505 
2506 #if 0
2507 int
2508 monitor_back_db_config(
2509 	Backend     *be,
2510 	const char  *fname,
2511 	int         lineno,
2512 	int         argc,
2513 	char        **argv )
2514 {
2515 	monitor_info_t	*mi = ( monitor_info_t * )be->be_private;
2516 
2517 	/*
2518 	 * eventually, will hold database specific configuration parameters
2519 	 */
2520 	return SLAP_CONF_UNKNOWN;
2521 }
2522 #endif
2523 
2524 int
monitor_back_db_destroy(BackendDB * be,ConfigReply * cr)2525 monitor_back_db_destroy(
2526 	BackendDB	*be,
2527 	ConfigReply	*cr)
2528 {
2529 	monitor_info_t	*mi = ( monitor_info_t * )be->be_private;
2530 
2531 	if ( mi == NULL ) {
2532 		return -1;
2533 	}
2534 
2535 	/*
2536 	 * FIXME: destroys all the data
2537 	 */
2538 	/* NOTE: mi points to static storage; don't free it */
2539 
2540 	(void)monitor_cache_destroy( mi );
2541 
2542 	if ( monitor_subsys ) {
2543 		int	i;
2544 
2545 		for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
2546 			if ( monitor_subsys[ i ]->mss_destroy ) {
2547 				monitor_subsys[ i ]->mss_destroy( be, monitor_subsys[ i ] );
2548 			}
2549 
2550 			if ( !BER_BVISNULL( &monitor_subsys[ i ]->mss_rdn ) ) {
2551 				ch_free( monitor_subsys[ i ]->mss_rdn.bv_val );
2552 			}
2553 		}
2554 
2555 		ch_free( monitor_subsys );
2556 	}
2557 
2558 	if ( mi->mi_entry_limbo ) {
2559 		entry_limbo_t	*el = mi->mi_entry_limbo;
2560 
2561 		for ( ; el; ) {
2562 			entry_limbo_t *tmp = el;
2563 			el = el->el_next;
2564 			monitor_back_destroy_limbo_entry( tmp, 1 );
2565 		}
2566 	}
2567 
2568 	ldap_pvt_thread_mutex_destroy( &monitor_info.mi_cache_mutex );
2569 
2570 	be->be_private = NULL;
2571 
2572 	return 0;
2573 }
2574