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