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