]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/init.c
Sync with HEAD
[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-2005 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 #undef INTEGRATE_CORE_SCHEMA
34
35 /*
36  * used by many functions to add description to entries
37  *
38  * WARNING: be_monitor may change as new databases are added,
39  * so it should not be used outside monitor_back_db_init()
40  * until monitor_back_db_open is called.
41  */
42 BackendDB                       *be_monitor;
43
44 static struct monitor_subsys_t  **monitor_subsys;
45 static int                      monitor_subsys_opened;
46 static monitor_info_t           monitor_info;
47
48 /*
49  * subsystem data
50  *
51  * the known subsystems are added to the subsystems
52  * array at backend initialization; other subsystems
53  * may be added by calling monitor_back_register_subsys()
54  * before the database is opened (e.g. by other backends
55  * or by overlays or modules).
56  */
57 static struct monitor_subsys_t known_monitor_subsys[] = {
58         { 
59                 SLAPD_MONITOR_BACKEND_NAME, 
60                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
61                 { BER_BVC( "This subsystem contains information about available backends." ),
62                         BER_BVNULL },
63                 MONITOR_F_PERSISTENT_CH,
64                 monitor_subsys_backend_init,
65                 NULL,   /* destroy */
66                 NULL,   /* update */
67                 NULL,   /* create */
68                 NULL    /* modify */
69         }, { 
70                 SLAPD_MONITOR_CONN_NAME,
71                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
72                 { BER_BVC( "This subsystem contains information about connections." ),
73                         BER_BVNULL },
74                 MONITOR_F_VOLATILE_CH,
75                 monitor_subsys_conn_init,
76                 NULL,   /* destroy */
77                 NULL,   /* update */
78                 NULL,   /* create */
79                 NULL    /* modify */
80         }, { 
81                 SLAPD_MONITOR_DATABASE_NAME,    
82                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
83                 { BER_BVC( "This subsystem contains information about configured databases." ),
84                         BER_BVNULL },
85                 MONITOR_F_PERSISTENT_CH,
86                 monitor_subsys_database_init,
87                 NULL,   /* destroy */
88                 NULL,   /* update */
89                 NULL,   /* create */
90                 NULL    /* modify */
91         }, { 
92                 SLAPD_MONITOR_LISTENER_NAME,    
93                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
94                 { BER_BVC( "This subsystem contains information about active listeners." ),
95                         BER_BVNULL },
96                 MONITOR_F_PERSISTENT_CH,
97                 monitor_subsys_listener_init,
98                 NULL,   /* destroy */
99                 NULL,   /* update */
100                 NULL,   /* create */
101                 NULL    /* modify */
102         }, { 
103                 SLAPD_MONITOR_LOG_NAME,
104                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
105                 { BER_BVC( "This subsystem contains information about logging." ),
106                         BER_BVC( "Set the attribute \"managedInfo\" to the desired log levels." ),
107                         BER_BVNULL },
108                 MONITOR_F_NONE,
109                 monitor_subsys_log_init,
110                 NULL,   /* destroy */
111                 NULL,   /* update */
112                 NULL,   /* create */
113                 NULL,   /* modify */
114         }, { 
115                 SLAPD_MONITOR_OPS_NAME,
116                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
117                 { BER_BVC( "This subsystem contains information about performed operations." ),
118                         BER_BVNULL },
119                 MONITOR_F_PERSISTENT_CH,
120                 monitor_subsys_ops_init,
121                 NULL,   /* destroy */
122                 NULL,   /* update */
123                 NULL,   /* create */
124                 NULL,   /* modify */
125         }, { 
126                 SLAPD_MONITOR_OVERLAY_NAME,
127                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
128                 { BER_BVC( "This subsystem contains information about available overlays." ),
129                         BER_BVNULL },
130                 MONITOR_F_PERSISTENT_CH,
131                 monitor_subsys_overlay_init,
132                 NULL,   /* destroy */
133                 NULL,   /* update */
134                 NULL,   /* create */
135                 NULL,   /* modify */
136         }, { 
137                 SLAPD_MONITOR_SASL_NAME,        
138                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
139                 { BER_BVC( "This subsystem contains information about SASL." ),
140                         BER_BVNULL },
141                 MONITOR_F_NONE,
142                 NULL,   /* init */
143                 NULL,   /* destroy */
144                 NULL,   /* update */
145                 NULL,   /* create */
146                 NULL    /* modify */
147         }, { 
148                 SLAPD_MONITOR_SENT_NAME,
149                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
150                 { BER_BVC( "This subsystem contains statistics." ),
151                         BER_BVNULL },
152                 MONITOR_F_PERSISTENT_CH,
153                 monitor_subsys_sent_init,
154                 NULL,   /* destroy */
155                 NULL,   /* update */
156                 NULL,   /* create */
157                 NULL,   /* modify */
158         }, { 
159                 SLAPD_MONITOR_THREAD_NAME,      
160                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
161                 { BER_BVC( "This subsystem contains information about threads." ),
162                         BER_BVNULL },
163                 MONITOR_F_PERSISTENT_CH,
164                 monitor_subsys_thread_init,
165                 NULL,   /* destroy */
166                 NULL,   /* update */
167                 NULL,   /* create */
168                 NULL    /* modify */
169         }, { 
170                 SLAPD_MONITOR_TIME_NAME,
171                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
172                 { BER_BVC( "This subsystem contains information about time." ),
173                         BER_BVNULL },
174                 MONITOR_F_PERSISTENT_CH,
175                 monitor_subsys_time_init,
176                 NULL,   /* destroy */
177                 NULL,   /* update */
178                 NULL,   /* create */
179                 NULL,   /* modify */
180         }, { 
181                 SLAPD_MONITOR_TLS_NAME,
182                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
183                 { BER_BVC( "This subsystem contains information about TLS." ),
184                         BER_BVNULL },
185                 MONITOR_F_NONE,
186                 NULL,   /* init */
187                 NULL,   /* destroy */
188                 NULL,   /* update */
189                 NULL,   /* create */
190                 NULL    /* modify */
191         }, { 
192                 SLAPD_MONITOR_RWW_NAME,
193                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
194                 { BER_BVC( "This subsystem contains information about read/write waiters." ),
195                         BER_BVNULL },
196                 MONITOR_F_PERSISTENT_CH,
197                 monitor_subsys_rww_init,
198                 NULL,   /* destroy */
199                 NULL,   /* update */
200                 NULL,   /* create */
201                 NULL    /* modify */
202         }, { NULL }
203 };
204
205 int
206 monitor_back_register_subsys(
207         monitor_subsys_t        *ms )
208 {
209         int     i = 0;
210
211         if ( monitor_subsys ) {
212                 for ( ; monitor_subsys[ i ] != NULL; i++ )
213                         /* just count'em */ ;
214         }
215
216         monitor_subsys = ch_realloc( monitor_subsys,
217                         ( 2 + i ) * sizeof( monitor_subsys_t * ) );
218
219         if ( monitor_subsys == NULL ) {
220                 return -1;
221         }
222
223         monitor_subsys[ i ] = ms;
224         monitor_subsys[ i + 1 ] = NULL;
225
226         /* if a subsystem is registered __AFTER__ subsystem 
227          * initialization (depending on the sequence the databases
228          * are listed in slapd.conf), init it */
229         if ( monitor_subsys_opened ) {
230
231                 /* FIXME: this should only be possible
232                  * if be_monitor is already initialized */
233                 assert( be_monitor != NULL );
234
235                 if ( ms->mss_open && ( *ms->mss_open )( be_monitor, ms ) ) {
236                         return -1;
237                 }
238
239                 ms->mss_flags |= MONITOR_F_OPENED;
240         }
241
242         return 0;
243 }
244
245 enum {
246         LIMBO_ENTRY,
247         LIMBO_ENTRY_PARENT,
248         LIMBO_ATTRS,
249         LIMBO_CB
250 };
251
252 typedef struct entry_limbo_t {
253         int                     el_type;
254         Entry                   *el_e;
255         Attribute               *el_a;
256         struct berval           el_ndn;
257         struct berval           el_base;
258         int                     el_scope;
259         struct berval           el_filter;
260         monitor_callback_t      *el_cb;
261         struct entry_limbo_t    *el_next;
262 } entry_limbo_t;
263
264 int
265 monitor_back_is_configured( void )
266 {
267         return be_monitor != NULL;
268 }
269
270 int
271 monitor_back_register_entry(
272         Entry                   *e,
273         monitor_callback_t      *cb )
274 {
275         monitor_info_t  *mi;
276
277         if ( be_monitor == NULL ) {
278                 Debug( LDAP_DEBUG_ANY,
279                         "monitor_back_register_entry(\"%s\"): "
280                         "monitor database not configured.\n",
281                         e->e_name.bv_val, 0, 0 );
282                 return -1;
283         }
284
285         mi = ( monitor_info_t * )be_monitor->be_private;
286
287         assert( mi != NULL );
288         assert( e != NULL );
289         assert( e->e_private == NULL );
290         
291         if ( monitor_subsys_opened ) {
292                 Entry           *e_parent = NULL,
293                                 *e_new = NULL,
294                                 **ep = NULL;
295                 struct berval   pdn = BER_BVNULL;
296                 monitor_entry_t *mp = NULL,
297                                 *mp_parent = NULL;
298                 int             rc = 0;
299
300                 if ( monitor_cache_get( mi, &e->e_nname, &e_parent ) == 0 ) {
301                         /* entry exists */
302                         Debug( LDAP_DEBUG_ANY,
303                                 "monitor_back_register_entry(\"%s\"): "
304                                 "entry exists\n",
305                                 e->e_name.bv_val, 0, 0 );
306                         monitor_cache_release( mi, e_parent );
307                         return -1;
308                 }
309
310                 dnParent( &e->e_nname, &pdn );
311                 if ( monitor_cache_get( mi, &pdn, &e_parent ) != 0 ) {
312                         /* parent does not exist */
313                         Debug( LDAP_DEBUG_ANY,
314                                 "monitor_back_register_entry(\"%s\"): "
315                                 "parent \"%s\" not found\n",
316                                 e->e_name.bv_val, pdn.bv_val, 0 );
317                         return -1;
318                 }
319
320                 assert( e_parent->e_private != NULL );
321                 mp_parent = ( monitor_entry_t * )e_parent->e_private;
322
323                 if ( mp_parent->mp_flags & MONITOR_F_VOLATILE ) {
324                         /* entry is volatile; cannot append children */
325                         Debug( LDAP_DEBUG_ANY,
326                                 "monitor_back_register_entry(\"%s\"): "
327                                 "parent \"%s\" is volatile\n",
328                                 e->e_name.bv_val, e_parent->e_name.bv_val, 0 );
329                         rc = -1;
330                         goto done;
331                 }
332
333                 mp = monitor_entrypriv_create();
334                 if ( mp == NULL ) {
335                         Debug( LDAP_DEBUG_ANY,
336                                 "monitor_back_register_entry(\"%s\"): "
337                                 "monitor_entrypriv_create() failed\n",
338                                 e->e_name.bv_val, 0, 0 );
339                         rc = -1;
340                         goto done;
341                 }
342
343                 e_new = entry_dup( e );
344                 if ( e_new == NULL ) {
345                         Debug( LDAP_DEBUG_ANY,
346                                 "monitor_back_register_entry(\"%s\"): "
347                                 "entry_dup() failed\n",
348                                 e->e_name.bv_val, 0, 0 );
349                         rc = -1;
350                         goto done;
351                 }
352                 
353                 e_new->e_private = ( void * )mp;
354                 mp->mp_info = mp_parent->mp_info;
355                 mp->mp_flags = mp_parent->mp_flags | MONITOR_F_SUB;
356                 mp->mp_cb = cb;
357
358                 ep = &mp_parent->mp_children;
359                 for ( ; *ep; ) {
360                         mp_parent = ( monitor_entry_t * )(*ep)->e_private;
361                         ep = &mp_parent->mp_next;
362                 }
363                 *ep = e_new;
364
365                 if ( monitor_cache_add( mi, e_new ) ) {
366                         Debug( LDAP_DEBUG_ANY,
367                                 "monitor_back_register_entry(\"%s\"): "
368                                 "unable to add entry\n",
369                                 e->e_name.bv_val, 0, 0 );
370                         rc = -1;
371                         goto done;
372                 }
373
374 done:;
375                 if ( rc ) {
376                         if ( mp ) {
377                                 ch_free( mp );
378                         }
379                         if ( e_new ) {
380                                 e_new->e_private = NULL;
381                                 entry_free( e_new );
382                         }
383                 }
384
385                 if ( e_parent ) {
386                         monitor_cache_release( mi, e_parent );
387                 }
388
389         } else {
390                 entry_limbo_t   **elpp, el = { 0 };
391
392                 el.el_type = LIMBO_ENTRY;
393
394                 el.el_e = entry_dup( e );
395                 if ( el.el_e == NULL ) {
396                         Debug( LDAP_DEBUG_ANY,
397                                 "monitor_back_register_entry(\"%s\"): "
398                                 "entry_dup() failed\n",
399                                 e->e_name.bv_val, 0, 0 );
400                         return -1;
401                 }
402                 
403                 el.el_cb = cb;
404
405                 for ( elpp = (entry_limbo_t **)&mi->mi_entry_limbo;
406                                 *elpp;
407                                 elpp = &(*elpp)->el_next )
408                         /* go to last */;
409
410                 *elpp = (entry_limbo_t *)ch_malloc( sizeof( entry_limbo_t ) );
411                 if ( *elpp == NULL ) {
412                         el.el_e->e_private = NULL;
413                         entry_free( el.el_e );
414                         return -1;
415                 }
416
417                 el.el_next = NULL;
418                 **elpp = el;
419         }
420
421         return 0;
422 }
423
424 int
425 monitor_back_register_entry_parent(
426         Entry                   *e,
427         monitor_callback_t      *cb,
428         struct berval           *base,
429         int                     scope,
430         struct berval           *filter )
431 {
432         monitor_info_t  *mi;
433         struct berval   ndn = BER_BVNULL;
434
435         if ( be_monitor == NULL ) {
436                 Debug( LDAP_DEBUG_ANY,
437                         "monitor_back_register_entry_parent(base=\"%s\" scope=%s filter=\"%s\"): "
438                         "monitor database not configured.\n",
439                         BER_BVISNULL( base ) ? "" : base->bv_val,
440                         scope == LDAP_SCOPE_BASE ? "base" : ( scope == LDAP_SCOPE_ONELEVEL ? "one" : "subtree" ),
441                         BER_BVISNULL( filter ) ? "" : filter->bv_val );
442                 return -1;
443         }
444
445         mi = ( monitor_info_t * )be_monitor->be_private;
446
447         assert( mi != NULL );
448         assert( e != NULL );
449         assert( e->e_private == NULL );
450
451         if ( BER_BVISNULL( filter ) ) {
452                 /* need a filter */
453                 Debug( LDAP_DEBUG_ANY,
454                         "monitor_back_register_entry_parent(\"\"): "
455                         "need a valid filter\n",
456                         0, 0, 0 );
457                 return -1;
458         }
459
460         if ( monitor_subsys_opened ) {
461                 Entry           *e_parent = NULL,
462                                 *e_new = NULL,
463                                 **ep = NULL;
464                 struct berval   e_name = BER_BVNULL,
465                                 e_nname = BER_BVNULL;
466                 monitor_entry_t *mp = NULL,
467                                 *mp_parent = NULL;
468                 int             rc = 0;
469
470                 if ( monitor_filter2ndn( base, scope, filter, &ndn ) ) {
471                         /* entry does not exist */
472                         Debug( LDAP_DEBUG_ANY,
473                                 "monitor_back_register_entry_parent(\"\"): "
474                                 "base=%s scope=%d filter=%s : "
475                                 "unable to find entry\n",
476                                 base->bv_val ? base->bv_val : "\"\"",
477                                 scope, filter->bv_val );
478                         return -1;
479                 }
480
481                 if ( monitor_cache_get( mi, &ndn, &e_parent ) != 0 ) {
482                         /* entry does not exist */
483                         Debug( LDAP_DEBUG_ANY,
484                                 "monitor_back_register_entry_parent(\"%s\"): "
485                                 "parent entry does not exist\n",
486                                 ndn.bv_val, 0, 0 );
487                         rc = -1;
488                         goto done;
489                 }
490
491                 assert( e_parent->e_private != NULL );
492                 mp_parent = ( monitor_entry_t * )e_parent->e_private;
493
494                 if ( mp_parent->mp_flags & MONITOR_F_VOLATILE ) {
495                         /* entry is volatile; cannot append callback */
496                         Debug( LDAP_DEBUG_ANY,
497                                 "monitor_back_register_entry_parent(\"%s\"): "
498                                 "entry is volatile\n",
499                                 e_parent->e_name.bv_val, 0, 0 );
500                         rc = -1;
501                         goto done;
502                 }
503
504                 build_new_dn( &e_name, &e_parent->e_name, &e->e_name, NULL );
505                 build_new_dn( &e_nname, &e_parent->e_nname, &e->e_nname, NULL );
506
507                 if ( monitor_cache_get( mi, &e_nname, &e_new ) == 0 ) {
508                         /* entry already exists */
509                         Debug( LDAP_DEBUG_ANY,
510                                 "monitor_back_register_entry_parent(\"%s\"): "
511                                 "entry already exists\n",
512                                 e_name.bv_val, 0, 0 );
513                         monitor_cache_release( mi, e_new );
514                         rc = -1;
515                         goto done;
516                 }
517
518                 mp = monitor_entrypriv_create();
519                 if ( mp == NULL ) {
520                         Debug( LDAP_DEBUG_ANY,
521                                 "monitor_back_register_entry_parent(\"%s\"): "
522                                 "monitor_entrypriv_create() failed\n",
523                                 e->e_name.bv_val, 0, 0 );
524                         rc = -1;
525                         goto done;
526                 }
527
528                 e_new = entry_dup( e );
529                 if ( e_new == NULL ) {
530                         Debug( LDAP_DEBUG_ANY,
531                                 "monitor_back_register_entry(\"%s\"): "
532                                 "entry_dup() failed\n",
533                                 e->e_name.bv_val, 0, 0 );
534                         rc = -1;
535                         goto done;
536                 }
537                 ch_free( e_new->e_name.bv_val );
538                 ch_free( e_new->e_nname.bv_val );
539                 e_new->e_name = e_name;
540                 e_new->e_nname = e_nname;
541                 
542                 e_new->e_private = ( void * )mp;
543                 mp->mp_info = mp_parent->mp_info;
544                 mp->mp_flags = mp_parent->mp_flags | MONITOR_F_SUB;
545                 mp->mp_cb = cb;
546
547                 ep = &mp_parent->mp_children;
548                 for ( ; *ep; ) {
549                         mp_parent = ( monitor_entry_t * )(*ep)->e_private;
550                         ep = &mp_parent->mp_next;
551                 }
552                 *ep = e_new;
553
554                 if ( monitor_cache_add( mi, e_new ) ) {
555                         Debug( LDAP_DEBUG_ANY,
556                                 "monitor_back_register_entry(\"%s\"): "
557                                 "unable to add entry\n",
558                                 e->e_name.bv_val, 0, 0 );
559                         rc = -1;
560                         goto done;
561                 }
562
563 done:;
564                 if ( !BER_BVISNULL( &ndn ) ) {
565                         ch_free( ndn.bv_val );
566                 }
567
568                 if ( rc ) {
569                         if ( mp ) {
570                                 ch_free( mp );
571                         }
572                         if ( e_new ) {
573                                 e_new->e_private = NULL;
574                                 entry_free( e_new );
575                         }
576                 }
577
578                 if ( e_parent ) {
579                         monitor_cache_release( mi, e_parent );
580                 }
581
582         } else {
583                 entry_limbo_t   **elpp, el = { 0 };
584
585                 el.el_type = LIMBO_ENTRY_PARENT;
586
587                 el.el_e = entry_dup( e );
588                 if ( el.el_e == NULL ) {
589                         Debug( LDAP_DEBUG_ANY,
590                                 "monitor_back_register_entry(\"%s\"): "
591                                 "entry_dup() failed\n",
592                                 e->e_name.bv_val, 0, 0 );
593                         return -1;
594                 }
595                 
596                 if ( !BER_BVISNULL( base ) ) {
597                         ber_dupbv( &el.el_base, base );
598                 }
599                 el.el_scope = scope;
600                 if ( !BER_BVISNULL( filter ) ) {
601                         ber_dupbv( &el.el_filter, filter );
602                 }
603
604                 el.el_cb = cb;
605
606                 for ( elpp = (entry_limbo_t **)&mi->mi_entry_limbo;
607                                 *elpp;
608                                 elpp = &(*elpp)->el_next )
609                         /* go to last */;
610
611                 *elpp = (entry_limbo_t *)ch_malloc( sizeof( entry_limbo_t ) );
612                 if ( *elpp == NULL ) {
613                         el.el_e->e_private = NULL;
614                         entry_free( el.el_e );
615                         return -1;
616                 }
617
618                 el.el_next = NULL;
619                 **elpp = el;
620         }
621
622         return 0;
623 }
624
625 static int
626 monitor_filter2ndn_cb( Operation *op, SlapReply *rs )
627 {
628         if ( rs->sr_type == REP_SEARCH ) {
629                 struct berval   *ndn = op->o_callback->sc_private;
630                 
631                 ber_dupbv( ndn, &rs->sr_entry->e_nname );
632         }
633
634         return 0;
635 }
636
637 int
638 monitor_filter2ndn(
639         struct berval   *base,
640         int             scope,
641         struct berval   *filter,
642         struct berval   *ndn )
643 {
644         Connection      conn = { 0 };
645         OperationBuffer opbuf;
646         Operation       *op;
647         SlapReply       rs = { 0 };
648         slap_callback   cb = { NULL, monitor_filter2ndn_cb, NULL, NULL };
649         int             rc;
650
651         BER_BVZERO( ndn );
652
653         if ( be_monitor == NULL ) {
654                 return -1;
655         }
656
657         op = (Operation *) &opbuf;
658         connection_fake_init( &conn, op, &conn );
659
660         op->o_tag = LDAP_REQ_SEARCH;
661
662         /* use global malloc for now */
663         if ( op->o_tmpmemctx ) {
664                 /* FIXME: connection_fake_init() calls slap_sl_mem_create, so we destroy it for now */
665                 slap_sl_mem_destroy( NULL, op->o_tmpmemctx );
666                 op->o_tmpmemctx = NULL;
667         }
668         op->o_tmpmfuncs = &ch_mfuncs;
669
670         op->o_bd = be_monitor;
671         if ( base == NULL || BER_BVISNULL( base ) ) {
672                 ber_dupbv_x( &op->o_req_dn, &op->o_bd->be_suffix[ 0 ],
673                                 op->o_tmpmemctx );
674                 ber_dupbv_x( &op->o_req_ndn, &op->o_bd->be_nsuffix[ 0 ],
675                                 op->o_tmpmemctx );
676
677         } else {
678                 if ( dnPrettyNormal( NULL, base, &op->o_req_dn, &op->o_req_ndn,
679                                         op->o_tmpmemctx ) ) {
680                         return -1;
681                 }
682         }
683
684         op->o_callback = &cb;
685         cb.sc_private = (void *)ndn;
686
687         op->ors_scope = scope;
688         ber_dupbv_x( &op->ors_filterstr, filter, op->o_tmpmemctx );
689         op->ors_filter = str2filter_x( op, filter->bv_val );
690         op->ors_attrs = slap_anlist_no_attrs;
691         op->ors_attrsonly = 0;
692         op->ors_tlimit = SLAP_NO_LIMIT;
693         op->ors_slimit = 1;
694         op->ors_limit = NULL;
695         op->ors_deref = LDAP_DEREF_NEVER;
696
697         op->o_nocaching = 1;
698         op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
699
700         op->o_dn = be_monitor->be_rootdn;
701         op->o_ndn = be_monitor->be_rootndn;
702
703         rc = op->o_bd->be_search( op, &rs );
704
705         filter_free_x( op, op->ors_filter );
706         op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
707         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
708         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
709
710         if ( rc != 0 ) {
711                 return rc;
712         }
713
714         switch ( rs.sr_err ) {
715         case LDAP_SUCCESS:
716                 if ( BER_BVISNULL( ndn ) ) {
717                         rc = -1;
718                 }
719                 break;
720                         
721         case LDAP_SIZELIMIT_EXCEEDED:
722         default:
723                 if ( !BER_BVISNULL( ndn ) ) {
724                         ber_memfree( ndn->bv_val );
725                         BER_BVZERO( ndn );
726                 }
727                 rc = -1;
728                 break;
729         }
730
731         return rc;
732 }
733
734 int
735 monitor_back_register_entry_attrs(
736         struct berval           *ndn_in,
737         Attribute               *a,
738         monitor_callback_t      *cb,
739         struct berval           *base,
740         int                     scope,
741         struct berval           *filter )
742 {
743         monitor_info_t  *mi;
744         struct berval   ndn = BER_BVNULL;
745         char            *fname = ( a == NULL ? "callback" : "attrs" );
746
747         if ( be_monitor == NULL ) {
748                 char            buf[ SLAP_TEXT_BUFLEN ];
749
750                 snprintf( buf, sizeof( buf ),
751                         "monitor_back_register_entry_%s(base=\"%s\" scope=%s filter=\"%s\"): "
752                         "monitor database not configured.\n",
753                         fname,
754                         BER_BVISNULL( base ) ? "" : base->bv_val,
755                         scope == LDAP_SCOPE_BASE ? "base" : ( scope == LDAP_SCOPE_ONELEVEL ? "one" : "subtree" ),
756                         BER_BVISNULL( filter ) ? "" : filter->bv_val );
757                 Debug( LDAP_DEBUG_ANY, "%s\n", buf, 0, 0 );
758
759                 return -1;
760         }
761
762         mi = ( monitor_info_t * )be_monitor->be_private;
763
764         assert( mi != NULL );
765
766         if ( ndn_in != NULL ) {
767                 ndn = *ndn_in;
768         }
769
770         if ( a == NULL && cb == NULL ) {
771                 /* nothing to do */
772                 return -1;
773         }
774
775         if ( ( ndn_in == NULL || BER_BVISNULL( &ndn ) )
776                         && BER_BVISNULL( filter ) )
777         {
778                 /* need a filter */
779                 Debug( LDAP_DEBUG_ANY,
780                         "monitor_back_register_entry_%s(\"\"): "
781                         "need a valid filter\n",
782                         fname, 0, 0 );
783                 return -1;
784         }
785
786         if ( monitor_subsys_opened ) {
787                 Entry                   *e = NULL;
788                 Attribute               **atp = NULL;
789                 monitor_entry_t         *mp = NULL;
790                 monitor_callback_t      **mcp = NULL;
791                 int                     rc = 0;
792                 int                     freeit = 0;
793
794                 if ( BER_BVISNULL( &ndn ) ) {
795                         if ( monitor_filter2ndn( base, scope, filter, &ndn ) ) {
796                                 char            buf[ SLAP_TEXT_BUFLEN ];
797
798                                 snprintf( buf, sizeof( buf ),
799                                         "monitor_back_register_entry_%s(\"\"): "
800                                         "base=%s scope=%d filter=%s : "
801                                         "unable to find entry\n",
802                                         fname,
803                                         base->bv_val ? base->bv_val : "\"\"",
804                                         scope, filter->bv_val );
805
806                                 /* entry does not exist */
807                                 Debug( LDAP_DEBUG_ANY, "%s\n", buf, 0, 0 );
808                                 return -1;
809                         }
810
811                         freeit = 1;
812                 }
813
814                 if ( monitor_cache_get( mi, &ndn, &e ) != 0 ) {
815                         /* entry does not exist */
816                         Debug( LDAP_DEBUG_ANY,
817                                 "monitor_back_register_entry_%s(\"%s\"): "
818                                 "entry does not exist\n",
819                                 fname, ndn.bv_val, 0 );
820                         rc = -1;
821                         goto done;
822                 }
823
824                 assert( e->e_private != NULL );
825                 mp = ( monitor_entry_t * )e->e_private;
826
827                 if ( mp->mp_flags & MONITOR_F_VOLATILE ) {
828                         /* entry is volatile; cannot append callback */
829                         Debug( LDAP_DEBUG_ANY,
830                                 "monitor_back_register_entry_%s(\"%s\"): "
831                                 "entry is volatile\n",
832                                 fname, e->e_name.bv_val, 0 );
833                         rc = -1;
834                         goto done;
835                 }
836
837                 if ( a ) {
838                         for ( atp = &e->e_attrs; *atp; atp = &(*atp)->a_next )
839                                 /* just get to last */ ;
840
841                         *atp = attrs_dup( a );
842                         if ( *atp == NULL ) {
843                                 Debug( LDAP_DEBUG_ANY,
844                                         "monitor_back_register_entry_%s(\"%s\"): "
845                                         "attrs_dup() failed\n",
846                                         fname, e->e_name.bv_val, 0 );
847                                 rc = -1;
848                                 goto done;
849                         }
850                 }
851
852                 if ( cb ) {
853                         for ( mcp = &mp->mp_cb; *mcp; mcp = &(*mcp)->mc_next )
854                                 /* go to tail */ ;
855                 
856                         /* NOTE: we do not clear cb->mc_next, so this function
857                          * can be used to append a list of callbacks */
858                         (*mcp) = cb;
859                 }
860
861 done:;
862                 if ( rc ) {
863                         if ( *atp ) {
864                                 attrs_free( *atp );
865                                 *atp = NULL;
866                         }
867                 }
868
869                 if ( freeit ) {
870                         ber_memfree( ndn.bv_val );
871                 }
872
873                 if ( e ) {
874                         monitor_cache_release( mi, e );
875                 }
876
877         } else {
878                 entry_limbo_t   **elpp, el = { 0 };
879
880                 el.el_type = LIMBO_ATTRS;
881                 if ( !BER_BVISNULL( &ndn ) ) {
882                         ber_dupbv( &el.el_ndn, &ndn );
883                 }
884                 if ( !BER_BVISNULL( base ) ) {
885                         ber_dupbv( &el.el_base, base);
886                 }
887                 el.el_scope = scope;
888                 if ( !BER_BVISNULL( filter ) ) {
889                         ber_dupbv( &el.el_filter, filter );
890                 }
891
892                 el.el_a = attrs_dup( a );
893                 el.el_cb = cb;
894
895                 for ( elpp = (entry_limbo_t **)&mi->mi_entry_limbo;
896                                 *elpp;
897                                 elpp = &(*elpp)->el_next )
898                         /* go to last */;
899
900                 *elpp = (entry_limbo_t *)ch_malloc( sizeof( entry_limbo_t ) );
901                 if ( *elpp == NULL ) {
902                         el.el_e->e_private = NULL;
903                         entry_free( el.el_e );
904                         return -1;
905                 }
906
907                 el.el_next = NULL;
908                 **elpp = el;
909         }
910
911         return 0;
912 }
913
914 int
915 monitor_back_register_entry_callback(
916         struct berval           *ndn,
917         monitor_callback_t      *cb,
918         struct berval           *base,
919         int                     scope,
920         struct berval           *filter )
921 {
922         return monitor_back_register_entry_attrs( ndn, NULL, cb,
923                         base, scope, filter );
924 }
925
926 monitor_subsys_t *
927 monitor_back_get_subsys( const char *name )
928 {
929         if ( monitor_subsys != NULL ) {
930                 int     i;
931                 
932                 for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
933                         if ( strcasecmp( monitor_subsys[ i ]->mss_name, name ) == 0 ) {
934                                 return monitor_subsys[ i ];
935                         }
936                 }
937         }
938
939         return NULL;
940 }
941
942 monitor_subsys_t *
943 monitor_back_get_subsys_by_dn(
944         struct berval   *ndn,
945         int             sub )
946 {
947         if ( monitor_subsys != NULL ) {
948                 int     i;
949
950                 if ( sub ) {
951                         for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
952                                 if ( dnIsSuffix( ndn, &monitor_subsys[ i ]->mss_ndn ) ) {
953                                         return monitor_subsys[ i ];
954                                 }
955                         }
956
957                 } else {
958                         for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
959                                 if ( dn_match( ndn, &monitor_subsys[ i ]->mss_ndn ) ) {
960                                         return monitor_subsys[ i ];
961                                 }
962                         }
963                 }
964         }
965
966         return NULL;
967 }
968
969 int
970 monitor_back_initialize(
971         BackendInfo     *bi )
972 {
973         static char             *controls[] = {
974                 LDAP_CONTROL_MANAGEDSAIT,
975                 NULL
976         };
977
978         static ConfigTable monitorcfg[] = {
979                 { NULL, NULL, 0, 0, 0, ARG_IGNORED,
980                         NULL, NULL, NULL, NULL }
981         };
982
983         static ConfigOCs monitorocs[] = {
984                 { "( OLcfgDbOc:4.1 "
985                         "NAME 'olcMonitorConfig' "
986                         "DESC 'Monitor backend configuration' "
987                         "SUP olcDatabaseConfig "
988                         ")",
989                                 Cft_Database, monitorcfg },
990                 { NULL, 0, NULL }
991         };
992
993         int             rc;
994
995         bi->bi_controls = controls;
996
997         bi->bi_init = 0;
998         bi->bi_open = 0;
999         bi->bi_config = monitor_back_config;
1000         bi->bi_close = 0;
1001         bi->bi_destroy = 0;
1002
1003         bi->bi_db_init = monitor_back_db_init;
1004         bi->bi_db_config = monitor_back_db_config;
1005         bi->bi_db_open = monitor_back_db_open;
1006         bi->bi_db_close = 0;
1007         bi->bi_db_destroy = monitor_back_db_destroy;
1008
1009         bi->bi_op_bind = monitor_back_bind;
1010         bi->bi_op_unbind = 0;
1011         bi->bi_op_search = monitor_back_search;
1012         bi->bi_op_compare = monitor_back_compare;
1013         bi->bi_op_modify = monitor_back_modify;
1014         bi->bi_op_modrdn = 0;
1015         bi->bi_op_add = 0;
1016         bi->bi_op_delete = 0;
1017         bi->bi_op_abandon = 0;
1018
1019         bi->bi_extended = 0;
1020
1021         bi->bi_entry_release_rw = 0;
1022         bi->bi_chk_referrals = 0;
1023         bi->bi_operational = monitor_back_operational;
1024
1025         /*
1026          * hooks for slap tools
1027          */
1028         bi->bi_tool_entry_open = 0;
1029         bi->bi_tool_entry_close = 0;
1030         bi->bi_tool_entry_first = 0;
1031         bi->bi_tool_entry_next = 0;
1032         bi->bi_tool_entry_get = 0;
1033         bi->bi_tool_entry_put = 0;
1034         bi->bi_tool_entry_reindex = 0;
1035         bi->bi_tool_sync = 0;
1036         bi->bi_tool_dn2id_get = 0;
1037         bi->bi_tool_id2entry_get = 0;
1038         bi->bi_tool_entry_modify = 0;
1039
1040         bi->bi_connection_init = 0;
1041         bi->bi_connection_destroy = 0;
1042
1043         /*
1044          * configuration objectClasses (fake)
1045          */
1046         bi->bi_cf_ocs = monitorocs;
1047
1048         rc = config_register_schema( monitorcfg, monitorocs );
1049         if ( rc ) {
1050                 return rc;
1051         }
1052
1053         return 0;
1054 }
1055
1056 int
1057 monitor_back_db_init(
1058         BackendDB       *be )
1059 {
1060         int                     rc;
1061         struct berval           dn = BER_BVC( SLAPD_MONITOR_DN ),
1062                                 pdn,
1063                                 ndn;
1064         BackendDB               *be2;
1065
1066         monitor_subsys_t        *ms;
1067         monitor_info_t          *mi = &monitor_info;
1068         int                     i;
1069         const char              *text;
1070
1071         struct m_s {
1072                 char    *name;
1073                 char    *schema;
1074                 slap_mask_t flags;
1075                 int     offset;
1076         } moc[] = {
1077                 { "monitor", "( 1.3.6.1.4.1.4203.666.3.16.1 "
1078                         "NAME 'monitor' "
1079                         "DESC 'OpenLDAP system monitoring' "
1080                         "SUP top STRUCTURAL "
1081                         "MUST cn "
1082                         "MAY ( "
1083                                 "description "
1084                                 "$ seeAlso "
1085                                 "$ labeledURI "
1086                                 "$ monitoredInfo "
1087                                 "$ managedInfo "
1088                                 "$ monitorOverlay "
1089                         ") )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1090                         offsetof(monitor_info_t, mi_oc_monitor) },
1091                 { "monitorServer", "( 1.3.6.1.4.1.4203.666.3.16.2 "
1092                         "NAME 'monitorServer' "
1093                         "DESC 'Server monitoring root entry' "
1094                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1095                         offsetof(monitor_info_t, mi_oc_monitorServer) },
1096                 { "monitorContainer", "( 1.3.6.1.4.1.4203.666.3.16.3 "
1097                         "NAME 'monitorContainer' "
1098                         "DESC 'monitor container class' "
1099                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1100                         offsetof(monitor_info_t, mi_oc_monitorContainer) },
1101                 { "monitorCounterObject", "( 1.3.6.1.4.1.4203.666.3.16.4 "
1102                         "NAME 'monitorCounterObject' "
1103                         "DESC 'monitor counter class' "
1104                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1105                         offsetof(monitor_info_t, mi_oc_monitorCounterObject) },
1106                 { "monitorOperation", "( 1.3.6.1.4.1.4203.666.3.16.5 "
1107                         "NAME 'monitorOperation' "
1108                         "DESC 'monitor operation class' "
1109                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1110                         offsetof(monitor_info_t, mi_oc_monitorOperation) },
1111                 { "monitorConnection", "( 1.3.6.1.4.1.4203.666.3.16.6 "
1112                         "NAME 'monitorConnection' "
1113                         "DESC 'monitor connection class' "
1114                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1115                         offsetof(monitor_info_t, mi_oc_monitorConnection) },
1116                 { "managedObject", "( 1.3.6.1.4.1.4203.666.3.16.7 "
1117                         "NAME 'managedObject' "
1118                         "DESC 'monitor managed entity class' "
1119                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1120                         offsetof(monitor_info_t, mi_oc_managedObject) },
1121                 { "monitoredObject", "( 1.3.6.1.4.1.4203.666.3.16.8 "
1122                         "NAME 'monitoredObject' "
1123                         "DESC 'monitor monitored entity class' "
1124                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1125                         offsetof(monitor_info_t, mi_oc_monitoredObject) },
1126                 { NULL, NULL, 0, -1 }
1127         }, mat[] = {
1128                 { "monitoredInfo", "( 1.3.6.1.4.1.4203.666.1.55.1 "
1129                         "NAME 'monitoredInfo' "
1130                         "DESC 'monitored info' "
1131                         /* "SUP name " */
1132                         "EQUALITY caseIgnoreMatch "
1133                         "SUBSTR caseIgnoreSubstringsMatch "
1134                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{32768} "
1135                         "NO-USER-MODIFICATION "
1136                         "USAGE directoryOperation )", SLAP_AT_HIDE,
1137                         offsetof(monitor_info_t, mi_ad_monitoredInfo) },
1138                 { "managedInfo", "( 1.3.6.1.4.1.4203.666.1.55.2 "
1139                         "NAME 'managedInfo' "
1140                         "DESC 'monitor managed info' "
1141                         "SUP name )", SLAP_AT_HIDE,
1142                         offsetof(monitor_info_t, mi_ad_managedInfo) },
1143                 { "monitorCounter", "( 1.3.6.1.4.1.4203.666.1.55.3 "
1144                         "NAME 'monitorCounter' "
1145                         "DESC 'monitor counter' "
1146                         "EQUALITY integerMatch "
1147                         "ORDERING integerOrderingMatch "
1148                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
1149                         "NO-USER-MODIFICATION "
1150                         "USAGE directoryOperation )", SLAP_AT_HIDE,
1151                         offsetof(monitor_info_t, mi_ad_monitorCounter) },
1152                 { "monitorOpCompleted", "( 1.3.6.1.4.1.4203.666.1.55.4 "
1153                         "NAME 'monitorOpCompleted' "
1154                         "DESC 'monitor completed operations' "
1155                         "SUP monitorCounter "
1156                         "NO-USER-MODIFICATION "
1157                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1158                         offsetof(monitor_info_t, mi_ad_monitorOpCompleted) },
1159                 { "monitorOpInitiated", "( 1.3.6.1.4.1.4203.666.1.55.5 "
1160                         "NAME 'monitorOpInitiated' "
1161                         "DESC 'monitor initiated operations' "
1162                         "SUP monitorCounter "
1163                         "NO-USER-MODIFICATION "
1164                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1165                         offsetof(monitor_info_t, mi_ad_monitorOpInitiated) },
1166                 { "monitorConnectionNumber", "( 1.3.6.1.4.1.4203.666.1.55.6 "
1167                         "NAME 'monitorConnectionNumber' "
1168                         "DESC 'monitor connection number' "
1169                         "SUP monitorCounter "
1170                         "NO-USER-MODIFICATION "
1171                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1172                         offsetof(monitor_info_t, mi_ad_monitorConnectionNumber) },
1173                 { "monitorConnectionAuthzDN", "( 1.3.6.1.4.1.4203.666.1.55.7 "
1174                         "NAME 'monitorConnectionAuthzDN' "
1175                         "DESC 'monitor connection authorization DN' "
1176                         /* "SUP distinguishedName " */
1177                         "EQUALITY distinguishedNameMatch "
1178                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
1179                         "NO-USER-MODIFICATION "
1180                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1181                         offsetof(monitor_info_t, mi_ad_monitorConnectionAuthzDN) },
1182                 { "monitorConnectionLocalAddress", "( 1.3.6.1.4.1.4203.666.1.55.8 "
1183                         "NAME 'monitorConnectionLocalAddress' "
1184                         "DESC 'monitor connection local address' "
1185                         "SUP monitoredInfo "
1186                         "NO-USER-MODIFICATION "
1187                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1188                         offsetof(monitor_info_t, mi_ad_monitorConnectionLocalAddress) },
1189                 { "monitorConnectionPeerAddress", "( 1.3.6.1.4.1.4203.666.1.55.9 "
1190                         "NAME 'monitorConnectionPeerAddress' "
1191                         "DESC 'monitor connection peer address' "
1192                         "SUP monitoredInfo "
1193                         "NO-USER-MODIFICATION "
1194                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1195                         offsetof(monitor_info_t, mi_ad_monitorConnectionPeerAddress) },
1196                 { "monitorTimestamp", "( 1.3.6.1.4.1.4203.666.1.55.10 "
1197                         "NAME 'monitorTimestamp' "
1198                         "DESC 'monitor timestamp' "
1199                         "EQUALITY generalizedTimeMatch "
1200                         "ORDERING generalizedTimeOrderingMatch "
1201                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
1202                         "SINGLE-VALUE "
1203                         "NO-USER-MODIFICATION "
1204                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1205                         offsetof(monitor_info_t, mi_ad_monitorTimestamp) },
1206                 { "monitorOverlay", "( 1.3.6.1.4.1.4203.666.1.55.11 "
1207                         "NAME 'monitorOverlay' "
1208                         "DESC 'name of overlays defined for a given database' "
1209                         "SUP monitoredInfo "
1210                         "NO-USER-MODIFICATION "
1211                         "USAGE directoryOperation )", SLAP_AT_HIDE,
1212                         offsetof(monitor_info_t, mi_ad_monitorOverlay) },
1213                 { "readOnly", "( 1.3.6.1.4.1.4203.666.1.55.12 "
1214                         "NAME 'readOnly' "
1215                         "DESC 'read/write status of a given database' "
1216                         "EQUALITY booleanMatch "
1217                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
1218                         "SINGLE-VALUE "
1219                         "USAGE directoryOperation )", SLAP_AT_HIDE,
1220                         offsetof(monitor_info_t, mi_ad_readOnly) },
1221                 { "restrictedOperation", "( 1.3.6.1.4.1.4203.666.1.55.13 "
1222                         "NAME 'restrictedOperation' "
1223                         "DESC 'name of restricted operation for a given database' "
1224                         "SUP managedInfo )", SLAP_AT_HIDE,
1225                         offsetof(monitor_info_t, mi_ad_restrictedOperation ) },
1226                 { "monitorConnectionProtocol", "( 1.3.6.1.4.1.4203.666.1.55.14 "
1227                         "NAME 'monitorConnectionProtocol' "
1228                         "DESC 'monitor connection protocol' "
1229                         "SUP monitoredInfo "
1230                         "NO-USER-MODIFICATION "
1231                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1232                         offsetof(monitor_info_t, mi_ad_monitorConnectionProtocol) },
1233                 { "monitorConnectionOpsReceived", "( 1.3.6.1.4.1.4203.666.1.55.15 "
1234                         "NAME 'monitorConnectionOpsReceived' "
1235                         "DESC 'monitor number of operations received by the connection' "
1236                         "SUP monitorCounter "
1237                         "NO-USER-MODIFICATION "
1238                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1239                         offsetof(monitor_info_t, mi_ad_monitorConnectionOpsReceived) },
1240                 { "monitorConnectionOpsExecuting", "( 1.3.6.1.4.1.4203.666.1.55.16 "
1241                         "NAME 'monitorConnectionOpsExecuting' "
1242                         "DESC 'monitor number of operations in execution within the connection' "
1243                         "SUP monitorCounter "
1244                         "NO-USER-MODIFICATION "
1245                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1246                         offsetof(monitor_info_t, mi_ad_monitorConnectionOpsExecuting) },
1247                 { "monitorConnectionOpsPending", "( 1.3.6.1.4.1.4203.666.1.55.17 "
1248                         "NAME 'monitorConnectionOpsPending' "
1249                         "DESC 'monitor number of pending operations within the connection' "
1250                         "SUP monitorCounter "
1251                         "NO-USER-MODIFICATION "
1252                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1253                         offsetof(monitor_info_t, mi_ad_monitorConnectionOpsPending) },
1254                 { "monitorConnectionOpsCompleted", "( 1.3.6.1.4.1.4203.666.1.55.18 "
1255                         "NAME 'monitorConnectionOpsCompleted' "
1256                         "DESC 'monitor number of operations completed within the connection' "
1257                         "SUP monitorCounter "
1258                         "NO-USER-MODIFICATION "
1259                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1260                         offsetof(monitor_info_t, mi_ad_monitorConnectionOpsCompleted) },
1261                 { "monitorConnectionGet", "( 1.3.6.1.4.1.4203.666.1.55.19 "
1262                         "NAME 'monitorConnectionGet' "
1263                         "DESC 'number of times connection_get() was called so far' "
1264                         "SUP monitorCounter "
1265                         "NO-USER-MODIFICATION "
1266                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1267                         offsetof(monitor_info_t, mi_ad_monitorConnectionGet) },
1268                 { "monitorConnectionRead", "( 1.3.6.1.4.1.4203.666.1.55.20 "
1269                         "NAME 'monitorConnectionRead' "
1270                         "DESC 'number of times connection_read() was called so far' "
1271                         "SUP monitorCounter "
1272                         "NO-USER-MODIFICATION "
1273                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1274                         offsetof(monitor_info_t, mi_ad_monitorConnectionRead) },
1275                 { "monitorConnectionWrite", "( 1.3.6.1.4.1.4203.666.1.55.21 "
1276                         "NAME 'monitorConnectionWrite' "
1277                         "DESC 'number of times connection_write() was called so far' "
1278                         "SUP monitorCounter "
1279                         "NO-USER-MODIFICATION "
1280                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1281                         offsetof(monitor_info_t, mi_ad_monitorConnectionWrite) },
1282                 { "monitorConnectionMask", "( 1.3.6.1.4.1.4203.666.1.55.22 "
1283                         "NAME 'monitorConnectionMask' "
1284                         "DESC 'monitor connection mask' "
1285                         "SUP monitoredInfo "
1286                         "NO-USER-MODIFICATION "
1287                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1288                         offsetof(monitor_info_t, mi_ad_monitorConnectionMask) },
1289                 { "monitorConnectionListener", "( 1.3.6.1.4.1.4203.666.1.55.23 "
1290                         "NAME 'monitorConnectionListener' "
1291                         "DESC 'monitor connection listener' "
1292                         "SUP monitoredInfo "
1293                         "NO-USER-MODIFICATION "
1294                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1295                         offsetof(monitor_info_t, mi_ad_monitorConnectionListener) },
1296                 { "monitorConnectionPeerDomain", "( 1.3.6.1.4.1.4203.666.1.55.24 "
1297                         "NAME 'monitorConnectionPeerDomain' "
1298                         "DESC 'monitor connection peer domain' "
1299                         "SUP monitoredInfo "
1300                         "NO-USER-MODIFICATION "
1301                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1302                         offsetof(monitor_info_t, mi_ad_monitorConnectionPeerDomain) },
1303                 { "monitorConnectionStartTime", "( 1.3.6.1.4.1.4203.666.1.55.25 "
1304                         "NAME 'monitorConnectionStartTime' "
1305                         "DESC 'monitor connection start time' "
1306                         "SUP monitorTimestamp "
1307                         "SINGLE-VALUE "
1308                         "NO-USER-MODIFICATION "
1309                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1310                         offsetof(monitor_info_t, mi_ad_monitorConnectionStartTime) },
1311                 { "monitorConnectionActivityTime", "( 1.3.6.1.4.1.4203.666.1.55.26 "
1312                         "NAME 'monitorConnectionActivityTime' "
1313                         "DESC 'monitor connection activity time' "
1314                         "SUP monitorTimestamp "
1315                         "SINGLE-VALUE "
1316                         "NO-USER-MODIFICATION "
1317                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1318                         offsetof(monitor_info_t, mi_ad_monitorConnectionActivityTime) },
1319                 { "monitorIsShadow", "( 1.3.6.1.4.1.4203.666.1.55.27 "
1320                         "NAME 'monitorIsShadow' "
1321                         "DESC 'TRUE if the database is shadow' "
1322                         "EQUALITY booleanMatch "
1323                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
1324                         "SINGLE-VALUE "
1325                         "USAGE directoryOperation )", SLAP_AT_HIDE,
1326                         offsetof(monitor_info_t, mi_ad_monitorIsShadow) },
1327                 { "monitorUpdateRef", "( 1.3.6.1.4.1.4203.666.1.55.28 "
1328                         "NAME 'monitorUpdateRef' "
1329                         "DESC 'update referral for shadow databases' "
1330                         "SUP monitoredInfo "
1331                         "SINGLE-VALUE "
1332                         "USAGE directoryOperation )", SLAP_AT_HIDE,
1333                         offsetof(monitor_info_t, mi_ad_monitorUpdateRef) },
1334                 { "monitorRuntimeConfig", "( 1.3.6.1.4.1.4203.666.1.55.29 "
1335                         "NAME 'monitorRuntimeConfig' "
1336                         "DESC 'TRUE if component allows runtime configuration' "
1337                         "EQUALITY booleanMatch "
1338                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
1339                         "SINGLE-VALUE "
1340                         "USAGE directoryOperation )", SLAP_AT_HIDE,
1341                         offsetof(monitor_info_t, mi_ad_monitorRuntimeConfig) },
1342                 { NULL, NULL, 0, -1 }
1343         };
1344
1345         /*
1346          * register subsys
1347          */
1348         for ( ms = known_monitor_subsys; ms->mss_name != NULL; ms++ ) {
1349                 if ( monitor_back_register_subsys( ms ) ) {
1350                         return -1;
1351                 }
1352         }
1353
1354         /* schema integration */
1355         for ( i = 0; mat[ i ].name; i++ ) {
1356                 LDAPAttributeType       *at;
1357                 int                     code;
1358                 const char              *err;
1359                 AttributeDescription    **ad;
1360
1361                 at = ldap_str2attributetype( mat[ i ].schema, &code,
1362                         &err, LDAP_SCHEMA_ALLOW_ALL );
1363                 if ( !at ) {
1364                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
1365                                 "in AttributeType \"%s\" %s before %s\n",
1366                                 mat[ i ].name, ldap_scherr2str(code), err );
1367                         return -1;
1368                 }
1369
1370                 if ( at->at_oid == NULL ) {
1371                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
1372                                 "null OID for attributeType \"%s\"\n",
1373                                 mat[ i ].name, 0, 0 );
1374                         return -1;
1375                 }
1376
1377                 code = at_add(at, 0, NULL, &err);
1378                 if ( code ) {
1379                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
1380                                 "%s in attributeType \"%s\"\n",
1381                                 scherr2str(code), mat[ i ].name, 0 );
1382                         return -1;
1383                 }
1384                 ldap_memfree(at);
1385
1386                 ad = ((AttributeDescription **)&(((char *)mi)[ mat[ i ].offset ]));
1387                 ad[ 0 ] = NULL;
1388                 if ( slap_str2ad( mat[ i ].name, ad, &text ) ) {
1389                         Debug( LDAP_DEBUG_ANY,
1390                                 "monitor_back_db_init: %s\n", text, 0, 0 );
1391                         return -1;
1392                 }
1393
1394                 (*ad)->ad_type->sat_flags |= mat[ i ].flags;
1395         }
1396
1397         for ( i = 0; moc[ i ].name; i++ ) {
1398                 LDAPObjectClass         *oc;
1399                 int                     code;
1400                 const char              *err;
1401                 ObjectClass             *Oc;
1402
1403                 oc = ldap_str2objectclass(moc[ i ].schema, &code, &err,
1404                                 LDAP_SCHEMA_ALLOW_ALL );
1405                 if ( !oc ) {
1406                         Debug( LDAP_DEBUG_ANY,
1407                                 "unable to parse monitor objectclass \"%s\": "
1408                                 "%s before %s\n" , moc[ i ].name,
1409                                 ldap_scherr2str(code), err );
1410                         return -1;
1411                 }
1412
1413                 if ( oc->oc_oid == NULL ) {
1414                         Debug( LDAP_DEBUG_ANY,
1415                                 "objectclass \"%s\" has no OID\n" ,
1416                                 moc[ i ].name, 0, 0 );
1417                         return -1;
1418                 }
1419
1420                 code = oc_add(oc, 0, NULL, &err);
1421                 if ( code ) {
1422                         Debug( LDAP_DEBUG_ANY,
1423                                 "objectclass \"%s\": %s \"%s\"\n" ,
1424                                 moc[ i ].name, scherr2str(code), err );
1425                         return -1;
1426                 }
1427
1428                 ldap_memfree(oc);
1429
1430                 Oc = oc_find( moc[ i ].name );
1431                 if ( Oc == NULL ) {
1432                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
1433                                         "unable to find objectClass %s "
1434                                         "(just added)\n", moc[ i ].name, 0, 0 );
1435                         return -1;
1436                 }
1437
1438                 Oc->soc_flags |= moc[ i ].flags;
1439
1440                 ((ObjectClass **)&(((char *)mi)[ moc[ i ].offset ]))[ 0 ] = Oc;
1441         }
1442
1443         /*
1444          * database monitor can be defined once only
1445          */
1446         if ( be_monitor != NULL ) {
1447                 Debug( LDAP_DEBUG_ANY,
1448                         "only one monitor database is allowed\n", 0, 0, 0 );
1449                 return( -1 );
1450         }
1451         be_monitor = be;
1452
1453         /* indicate system schema supported */
1454         SLAP_BFLAGS(be) |= SLAP_BFLAG_MONITOR;
1455
1456         rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn, NULL );
1457         if( rc != LDAP_SUCCESS ) {
1458                 Debug( LDAP_DEBUG_ANY,
1459                         "unable to normalize/pretty monitor DN \"%s\" (%d)\n",
1460                         dn.bv_val, rc, 0 );
1461                 return -1;
1462         }
1463
1464         ber_bvarray_add( &be->be_suffix, &pdn );
1465         ber_bvarray_add( &be->be_nsuffix, &ndn );
1466
1467         /* NOTE: only one monitor database is allowed,
1468          * so we use static storage */
1469         ldap_pvt_thread_mutex_init( &monitor_info.mi_cache_mutex );
1470
1471         be->be_private = &monitor_info;
1472
1473         be2 = select_backend( &ndn, 0, 0 );
1474         if ( be2 != be ) {
1475                 char    *type = be2->bd_info->bi_type;
1476
1477                 if ( overlay_is_over( be2 ) ) {
1478                         slap_overinfo   *oi = (slap_overinfo *)be2->bd_info->bi_private;
1479                         type = oi->oi_orig->bi_type;
1480                 }
1481
1482                 Debug( LDAP_DEBUG_ANY,
1483                         "\"monitor\" database serving namingContext \"%s\" "
1484                         "is hidden by \"%s\" database serving namingContext \"%s\".\n",
1485                         pdn.bv_val, type, be2->be_nsuffix[ 0 ].bv_val );
1486                 return -1;
1487         }
1488
1489         return 0;
1490 }
1491
1492 int
1493 monitor_back_db_open(
1494         BackendDB       *be )
1495 {
1496         monitor_info_t          *mi = (monitor_info_t *)be->be_private;
1497         struct monitor_subsys_t **ms;
1498         Entry                   *e, **ep;
1499         monitor_entry_t         *mp;
1500         int                     i;
1501         char                    buf[ BACKMONITOR_BUFSIZE ];
1502         struct berval           bv;
1503         struct tm               *tms;
1504 #ifdef HAVE_GMTIME_R
1505         struct tm               tm_buf;
1506 #endif
1507         static char             tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1508
1509         assert( be_monitor != NULL );
1510         if ( be != be_monitor ) {
1511                 be_monitor = be;
1512         }
1513
1514         /*
1515          * Start
1516          */
1517 #ifndef HAVE_GMTIME_R
1518         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1519 #endif
1520 #ifdef HACK_LOCAL_TIME
1521 # ifdef HAVE_LOCALTIME_R
1522         tms = localtime_r( &starttime, &tm_buf );
1523 # else
1524         tms = localtime( &starttime );
1525 # endif /* HAVE_LOCALTIME_R */
1526         lutil_localtime( tmbuf, sizeof(tmbuf), tms, -timezone );
1527 #else /* !HACK_LOCAL_TIME */
1528 # ifdef HAVE_GMTIME_R
1529         tms = gmtime_r( &starttime, &tm_buf );
1530 # else
1531         tms = gmtime( &starttime );
1532 # endif /* HAVE_GMTIME_R */
1533         lutil_gentime( tmbuf, sizeof(tmbuf), tms );
1534 #endif /* !HACK_LOCAL_TIME */
1535 #ifndef HAVE_GMTIME_R
1536         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1537 #endif
1538
1539         mi->mi_startTime.bv_val = tmbuf;
1540         mi->mi_startTime.bv_len = strlen( tmbuf );
1541
1542         if ( BER_BVISEMPTY( &be->be_rootdn ) ) {
1543                 BER_BVSTR( &mi->mi_creatorsName, SLAPD_ANONYMOUS );
1544         } else {
1545                 mi->mi_creatorsName = be->be_rootdn;
1546         }
1547
1548         /*
1549          * creates the "cn=Monitor" entry 
1550          */
1551         snprintf( buf, sizeof( buf ), 
1552                 "dn: %s\n"
1553                 "objectClass: %s\n"
1554                 "structuralObjectClass: %s\n"
1555                 "cn: Monitor\n"
1556                 "description: This subtree contains monitoring/managing objects.\n"
1557                 "description: This object contains information about this server.\n"
1558                 "description: Most of the information is held in operational"
1559                 " attributeTypes, which must be explicitly requested.\n"
1560                 "creatorsName: %s\n"
1561                 "modifiersName: %s\n"
1562                 "createTimestamp: %s\n"
1563                 "modifyTimestamp: %s\n",
1564                 SLAPD_MONITOR_DN,
1565                 mi->mi_oc_monitorServer->soc_cname.bv_val,
1566                 mi->mi_oc_monitorServer->soc_cname.bv_val,
1567                 mi->mi_creatorsName.bv_val,
1568                 mi->mi_creatorsName.bv_val,
1569                 mi->mi_startTime.bv_val,
1570                 mi->mi_startTime.bv_val );
1571
1572         e = str2entry( buf );
1573         if ( e == NULL) {
1574                 Debug( LDAP_DEBUG_ANY,
1575                         "unable to create \"%s\" entry\n",
1576                         SLAPD_MONITOR_DN, 0, 0 );
1577                 return( -1 );
1578         }
1579
1580         bv.bv_val = strchr( (char *) Versionstr, '$' );
1581         if ( bv.bv_val != NULL ) {
1582                 char    *end;
1583
1584                 bv.bv_val++;
1585                 for ( ; bv.bv_val[ 0 ] == ' '; bv.bv_val++ )
1586                         ;
1587
1588                 end = strchr( bv.bv_val, '$' );
1589                 if ( end != NULL ) {
1590                         end--;
1591
1592                         for ( ; end > bv.bv_val && end[ 0 ] == ' '; end-- )
1593                                 ;
1594
1595                         end++;
1596
1597                         bv.bv_len = end - bv.bv_val;
1598
1599                 } else {
1600                         bv.bv_len = strlen( bv.bv_val );
1601                 }
1602
1603                 if ( attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo,
1604                                         &bv, NULL ) ) {
1605                         Debug( LDAP_DEBUG_ANY,
1606                                 "unable to add monitoredInfo to \"%s\" entry\n",
1607                                 SLAPD_MONITOR_DN, 0, 0 );
1608                         return( -1 );
1609                 }
1610         }
1611
1612         mp = monitor_entrypriv_create();
1613         if ( mp == NULL ) {
1614                 return -1;
1615         }
1616         e->e_private = ( void * )mp;
1617         ep = &mp->mp_children;
1618
1619         if ( monitor_cache_add( mi, e ) ) {
1620                 Debug( LDAP_DEBUG_ANY,
1621                         "unable to add entry \"%s\" to cache\n",
1622                         SLAPD_MONITOR_DN, 0, 0 );
1623                 return -1;
1624         }
1625
1626         /*      
1627          * Create all the subsystem specific entries
1628          */
1629         for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
1630                 int             len = strlen( monitor_subsys[ i ]->mss_name );
1631                 struct berval   dn;
1632                 int             rc;
1633
1634                 dn.bv_len = len + sizeof( "cn=" ) - 1;
1635                 dn.bv_val = ch_calloc( sizeof( char ), dn.bv_len + 1 );
1636                 strcpy( dn.bv_val, "cn=" );
1637                 strcat( dn.bv_val, monitor_subsys[ i ]->mss_name );
1638                 rc = dnPretty( NULL, &dn, &monitor_subsys[ i ]->mss_rdn, NULL );
1639                 free( dn.bv_val );
1640                 if ( rc != LDAP_SUCCESS ) {
1641                         Debug( LDAP_DEBUG_ANY,
1642                                 "monitor RDN \"%s\" is invalid\n", 
1643                                 dn.bv_val, 0, 0 );
1644                         return( -1 );
1645                 }
1646
1647                 dn.bv_len += sizeof( SLAPD_MONITOR_DN ); /* 1 for the , */
1648                 dn.bv_val = ch_malloc( dn.bv_len + 1 );
1649                 strcpy( dn.bv_val , monitor_subsys[ i ]->mss_rdn.bv_val );
1650                 strcat( dn.bv_val, "," SLAPD_MONITOR_DN );
1651                 rc = dnPrettyNormal( NULL, &dn, &monitor_subsys[ i ]->mss_dn,
1652                         &monitor_subsys[ i ]->mss_ndn, NULL );
1653                 free( dn.bv_val );
1654                 if ( rc != LDAP_SUCCESS ) {
1655                         Debug( LDAP_DEBUG_ANY,
1656                                 "monitor DN \"%s\" is invalid\n", 
1657                                 dn.bv_val, 0, 0 );
1658                         return( -1 );
1659                 }
1660
1661                 snprintf( buf, sizeof( buf ),
1662                                 "dn: %s\n"
1663                                 "objectClass: %s\n"
1664                                 "structuralObjectClass: %s\n"
1665                                 "cn: %s\n"
1666                                 "creatorsName: %s\n"
1667                                 "modifiersName: %s\n"
1668                                 "createTimestamp: %s\n"
1669                                 "modifyTimestamp: %s\n",
1670                                 monitor_subsys[ i ]->mss_dn.bv_val,
1671                                 mi->mi_oc_monitorContainer->soc_cname.bv_val,
1672                                 mi->mi_oc_monitorContainer->soc_cname.bv_val,
1673                                 monitor_subsys[ i ]->mss_name,
1674                                 mi->mi_creatorsName.bv_val,
1675                                 mi->mi_creatorsName.bv_val,
1676                                 mi->mi_startTime.bv_val,
1677                                 mi->mi_startTime.bv_val );
1678                 
1679                 e = str2entry( buf );
1680                 
1681                 if ( e == NULL) {
1682                         Debug( LDAP_DEBUG_ANY,
1683                                 "unable to create \"%s\" entry\n", 
1684                                 monitor_subsys[ i ]->mss_dn.bv_val, 0, 0 );
1685                         return( -1 );
1686                 }
1687
1688                 if ( !BER_BVISNULL( &monitor_subsys[ i ]->mss_desc[ 0 ] ) ) {
1689                         attr_merge_normalize( e, slap_schema.si_ad_description,
1690                                         monitor_subsys[ i ]->mss_desc, NULL );
1691                 }
1692
1693                 mp = monitor_entrypriv_create();
1694                 if ( mp == NULL ) {
1695                         return -1;
1696                 }
1697                 e->e_private = ( void * )mp;
1698                 mp->mp_info = monitor_subsys[ i ];
1699                 mp->mp_flags = monitor_subsys[ i ]->mss_flags;
1700
1701                 if ( monitor_cache_add( mi, e ) ) {
1702                         Debug( LDAP_DEBUG_ANY,
1703                                 "unable to add entry \"%s\" to cache\n",
1704                                 monitor_subsys[ i ]->mss_dn.bv_val, 0, 0 );
1705                         return -1;
1706                 }
1707
1708                 *ep = e;
1709                 ep = &mp->mp_next;
1710         }
1711
1712         assert( be != NULL );
1713
1714         be->be_private = mi;
1715         
1716         /*
1717          * opens the monitor backend subsystems
1718          */
1719         for ( ms = monitor_subsys; ms[ 0 ] != NULL; ms++ ) {
1720                 if ( ms[ 0 ]->mss_open && ( *ms[ 0 ]->mss_open )( be, ms[ 0 ] ) )
1721                 {
1722                         return( -1 );
1723                 }
1724                 ms[ 0 ]->mss_flags |= MONITOR_F_OPENED;
1725         }
1726
1727         monitor_subsys_opened = 1;
1728
1729         if ( mi->mi_entry_limbo ) {
1730                 entry_limbo_t   *el = (entry_limbo_t *)mi->mi_entry_limbo;
1731
1732                 for ( ; el; ) {
1733                         entry_limbo_t   *tmp;
1734
1735                         switch ( el->el_type ) {
1736                         case LIMBO_ENTRY:
1737                                 monitor_back_register_entry(
1738                                                 el->el_e,
1739                                                 el->el_cb );
1740                                 break;
1741
1742                         case LIMBO_ENTRY_PARENT:
1743                                 monitor_back_register_entry_parent(
1744                                                 el->el_e,
1745                                                 el->el_cb,
1746                                                 &el->el_base,
1747                                                 el->el_scope,
1748                                                 &el->el_filter );
1749                                 break;
1750                                 
1751
1752                         case LIMBO_ATTRS:
1753                                 monitor_back_register_entry_attrs(
1754                                                 &el->el_ndn,
1755                                                 el->el_a,
1756                                                 el->el_cb,
1757                                                 &el->el_base,
1758                                                 el->el_scope,
1759                                                 &el->el_filter );
1760                                 break;
1761
1762                         case LIMBO_CB:
1763                                 monitor_back_register_entry_callback(
1764                                                 &el->el_ndn,
1765                                                 el->el_cb,
1766                                                 &el->el_base,
1767                                                 el->el_scope,
1768                                                 &el->el_filter );
1769                                 break;
1770
1771                         default:
1772                                 assert( 0 );
1773                         }
1774
1775                         if ( el->el_e ) {
1776                                 entry_free( el->el_e );
1777                         }
1778                         if ( el->el_a ) {
1779                                 attrs_free( el->el_a );
1780                         }
1781                         if ( !BER_BVISNULL( &el->el_ndn ) ) {
1782                                 ber_memfree( el->el_ndn.bv_val );
1783                         }
1784                         if ( !BER_BVISNULL( &el->el_base ) ) {
1785                                 ber_memfree( el->el_base.bv_val );
1786                         }
1787                         if ( !BER_BVISNULL( &el->el_filter ) ) {
1788                                 ber_memfree( el->el_filter.bv_val );
1789                         }
1790
1791                         tmp = el;
1792                         el = el->el_next;
1793                         ch_free( tmp );
1794                 }
1795
1796                 mi->mi_entry_limbo = NULL;
1797         }
1798
1799         return( 0 );
1800 }
1801
1802 int
1803 monitor_back_config(
1804         BackendInfo     *bi,
1805         const char      *fname,
1806         int             lineno,
1807         int             argc,
1808         char            **argv )
1809 {
1810         /*
1811          * eventually, will hold backend specific configuration parameters
1812          */
1813         return SLAP_CONF_UNKNOWN;
1814 }
1815
1816 int
1817 monitor_back_db_config(
1818         Backend     *be,
1819         const char  *fname,
1820         int         lineno,
1821         int         argc,
1822         char        **argv )
1823 {
1824 #if 0
1825         monitor_info_t  *mi = ( monitor_info_t * )be->be_private;
1826 #endif
1827
1828         /*
1829          * eventually, will hold database specific configuration parameters
1830          */
1831         return SLAP_CONF_UNKNOWN;
1832 }
1833
1834 int
1835 monitor_back_db_destroy(
1836         BackendDB       *be )
1837 {
1838         monitor_info_t  *mi = ( monitor_info_t * )be->be_private;
1839
1840         if ( mi == NULL ) {
1841                 return -1;
1842         }
1843
1844         /*
1845          * FIXME: destroys all the data
1846          */
1847         /* NOTE: mi points to static storage; don't free it */
1848         
1849         (void)monitor_cache_destroy( mi );
1850
1851         if ( monitor_subsys ) {
1852                 int     i;
1853
1854                 for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
1855                         if ( monitor_subsys[ i ]->mss_destroy ) {
1856                                 monitor_subsys[ i ]->mss_destroy( be, monitor_subsys[ i ] );
1857                         }
1858
1859                         if ( !BER_BVISNULL( &monitor_subsys[ i ]->mss_rdn ) ) {
1860                                 ch_free( monitor_subsys[ i ]->mss_rdn.bv_val );
1861                         }
1862
1863                         if ( !BER_BVISNULL( &monitor_subsys[ i ]->mss_dn ) ) {
1864                                 ch_free( monitor_subsys[ i ]->mss_dn.bv_val );
1865                         }
1866
1867                         if ( !BER_BVISNULL( &monitor_subsys[ i ]->mss_ndn ) ) {
1868                                 ch_free( monitor_subsys[ i ]->mss_ndn.bv_val );
1869                         }
1870                 }
1871
1872                 ch_free( monitor_subsys );
1873         }
1874         
1875         ldap_pvt_thread_mutex_destroy( &monitor_info.mi_cache_mutex );
1876
1877         be->be_private = NULL;
1878
1879         return 0;
1880 }
1881
1882 #if SLAPD_MONITOR == SLAPD_MOD_DYNAMIC
1883
1884 /* conditionally define the init_module() function */
1885 SLAP_BACKEND_INIT_MODULE( monitor )
1886
1887 #endif /* SLAPD_MONITOR == SLAPD_MOD_DYNAMIC */
1888