]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/init.c
fix config message (ITS#4853)
[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-2007 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         void    *thrctx;
648         SlapReply       rs = { 0 };
649         slap_callback   cb = { NULL, monitor_filter2ndn_cb, NULL, NULL };
650         int             rc;
651
652         BER_BVZERO( ndn );
653
654         if ( be_monitor == NULL ) {
655                 return -1;
656         }
657
658         op = (Operation *) &opbuf;
659         thrctx = ldap_pvt_thread_pool_context();
660         connection_fake_init( &conn, op, thrctx );
661
662         op->o_tag = LDAP_REQ_SEARCH;
663
664         op->o_bd = be_monitor;
665         if ( base == NULL || BER_BVISNULL( base ) ) {
666                 ber_dupbv_x( &op->o_req_dn, &op->o_bd->be_suffix[ 0 ],
667                                 op->o_tmpmemctx );
668                 ber_dupbv_x( &op->o_req_ndn, &op->o_bd->be_nsuffix[ 0 ],
669                                 op->o_tmpmemctx );
670
671         } else {
672                 if ( dnPrettyNormal( NULL, base, &op->o_req_dn, &op->o_req_ndn,
673                                         op->o_tmpmemctx ) ) {
674                         return -1;
675                 }
676         }
677
678         op->o_callback = &cb;
679         cb.sc_private = (void *)ndn;
680
681         op->ors_scope = scope;
682         ber_dupbv_x( &op->ors_filterstr, filter, op->o_tmpmemctx );
683         op->ors_filter = str2filter_x( op, filter->bv_val );
684         op->ors_attrs = slap_anlist_no_attrs;
685         op->ors_attrsonly = 0;
686         op->ors_tlimit = SLAP_NO_LIMIT;
687         op->ors_slimit = 1;
688         op->ors_limit = NULL;
689         op->ors_deref = LDAP_DEREF_NEVER;
690
691         op->o_nocaching = 1;
692         op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
693
694         op->o_dn = be_monitor->be_rootdn;
695         op->o_ndn = be_monitor->be_rootndn;
696
697         rc = op->o_bd->be_search( op, &rs );
698
699         filter_free_x( op, op->ors_filter );
700         op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
701         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
702         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
703
704         if ( rc != 0 ) {
705                 return rc;
706         }
707
708         switch ( rs.sr_err ) {
709         case LDAP_SUCCESS:
710                 if ( BER_BVISNULL( ndn ) ) {
711                         rc = -1;
712                 }
713                 break;
714                         
715         case LDAP_SIZELIMIT_EXCEEDED:
716         default:
717                 if ( !BER_BVISNULL( ndn ) ) {
718                         ber_memfree( ndn->bv_val );
719                         BER_BVZERO( ndn );
720                 }
721                 rc = -1;
722                 break;
723         }
724
725         return rc;
726 }
727
728 int
729 monitor_back_register_entry_attrs(
730         struct berval           *ndn_in,
731         Attribute               *a,
732         monitor_callback_t      *cb,
733         struct berval           *base,
734         int                     scope,
735         struct berval           *filter )
736 {
737         monitor_info_t  *mi;
738         struct berval   ndn = BER_BVNULL;
739         char            *fname = ( a == NULL ? "callback" : "attrs" );
740
741         if ( be_monitor == NULL ) {
742                 char            buf[ SLAP_TEXT_BUFLEN ];
743
744                 snprintf( buf, sizeof( buf ),
745                         "monitor_back_register_entry_%s(base=\"%s\" scope=%s filter=\"%s\"): "
746                         "monitor database not configured.\n",
747                         fname,
748                         BER_BVISNULL( base ) ? "" : base->bv_val,
749                         scope == LDAP_SCOPE_BASE ? "base" : ( scope == LDAP_SCOPE_ONELEVEL ? "one" : "subtree" ),
750                         BER_BVISNULL( filter ) ? "" : filter->bv_val );
751                 Debug( LDAP_DEBUG_ANY, "%s\n", buf, 0, 0 );
752
753                 return -1;
754         }
755
756         mi = ( monitor_info_t * )be_monitor->be_private;
757
758         assert( mi != NULL );
759
760         if ( ndn_in != NULL ) {
761                 ndn = *ndn_in;
762         }
763
764         if ( a == NULL && cb == NULL ) {
765                 /* nothing to do */
766                 return -1;
767         }
768
769         if ( ( ndn_in == NULL || BER_BVISNULL( &ndn ) )
770                         && BER_BVISNULL( filter ) )
771         {
772                 /* need a filter */
773                 Debug( LDAP_DEBUG_ANY,
774                         "monitor_back_register_entry_%s(\"\"): "
775                         "need a valid filter\n",
776                         fname, 0, 0 );
777                 return -1;
778         }
779
780         if ( monitor_subsys_opened ) {
781                 Entry                   *e = NULL;
782                 Attribute               **atp = NULL;
783                 monitor_entry_t         *mp = NULL;
784                 monitor_callback_t      **mcp = NULL;
785                 int                     rc = 0;
786                 int                     freeit = 0;
787
788                 if ( BER_BVISNULL( &ndn ) ) {
789                         if ( monitor_filter2ndn( base, scope, filter, &ndn ) ) {
790                                 char            buf[ SLAP_TEXT_BUFLEN ];
791
792                                 snprintf( buf, sizeof( buf ),
793                                         "monitor_back_register_entry_%s(\"\"): "
794                                         "base=%s scope=%d filter=%s : "
795                                         "unable to find entry\n",
796                                         fname,
797                                         base->bv_val ? base->bv_val : "\"\"",
798                                         scope, filter->bv_val );
799
800                                 /* entry does not exist */
801                                 Debug( LDAP_DEBUG_ANY, "%s\n", buf, 0, 0 );
802                                 return -1;
803                         }
804
805                         freeit = 1;
806                 }
807
808                 if ( monitor_cache_get( mi, &ndn, &e ) != 0 ) {
809                         /* entry does not exist */
810                         Debug( LDAP_DEBUG_ANY,
811                                 "monitor_back_register_entry_%s(\"%s\"): "
812                                 "entry does not exist\n",
813                                 fname, ndn.bv_val, 0 );
814                         rc = -1;
815                         goto done;
816                 }
817
818                 assert( e->e_private != NULL );
819                 mp = ( monitor_entry_t * )e->e_private;
820
821                 if ( mp->mp_flags & MONITOR_F_VOLATILE ) {
822                         /* entry is volatile; cannot append callback */
823                         Debug( LDAP_DEBUG_ANY,
824                                 "monitor_back_register_entry_%s(\"%s\"): "
825                                 "entry is volatile\n",
826                                 fname, e->e_name.bv_val, 0 );
827                         rc = -1;
828                         goto done;
829                 }
830
831                 if ( a ) {
832                         for ( atp = &e->e_attrs; *atp; atp = &(*atp)->a_next )
833                                 /* just get to last */ ;
834
835                         *atp = attrs_dup( a );
836                         if ( *atp == NULL ) {
837                                 Debug( LDAP_DEBUG_ANY,
838                                         "monitor_back_register_entry_%s(\"%s\"): "
839                                         "attrs_dup() failed\n",
840                                         fname, e->e_name.bv_val, 0 );
841                                 rc = -1;
842                                 goto done;
843                         }
844                 }
845
846                 if ( cb ) {
847                         for ( mcp = &mp->mp_cb; *mcp; mcp = &(*mcp)->mc_next )
848                                 /* go to tail */ ;
849                 
850                         /* NOTE: we do not clear cb->mc_next, so this function
851                          * can be used to append a list of callbacks */
852                         (*mcp) = cb;
853                 }
854
855 done:;
856                 if ( rc ) {
857                         if ( *atp ) {
858                                 attrs_free( *atp );
859                                 *atp = NULL;
860                         }
861                 }
862
863                 if ( freeit ) {
864                         ber_memfree( ndn.bv_val );
865                 }
866
867                 if ( e ) {
868                         monitor_cache_release( mi, e );
869                 }
870
871         } else {
872                 entry_limbo_t   **elpp, el = { 0 };
873
874                 el.el_type = LIMBO_ATTRS;
875                 if ( !BER_BVISNULL( &ndn ) ) {
876                         ber_dupbv( &el.el_ndn, &ndn );
877                 }
878                 if ( !BER_BVISNULL( base ) ) {
879                         ber_dupbv( &el.el_base, base);
880                 }
881                 el.el_scope = scope;
882                 if ( !BER_BVISNULL( filter ) ) {
883                         ber_dupbv( &el.el_filter, filter );
884                 }
885
886                 el.el_a = attrs_dup( a );
887                 el.el_cb = cb;
888
889                 for ( elpp = (entry_limbo_t **)&mi->mi_entry_limbo;
890                                 *elpp;
891                                 elpp = &(*elpp)->el_next )
892                         /* go to last */;
893
894                 *elpp = (entry_limbo_t *)ch_malloc( sizeof( entry_limbo_t ) );
895                 if ( *elpp == NULL ) {
896                         el.el_e->e_private = NULL;
897                         entry_free( el.el_e );
898                         return -1;
899                 }
900
901                 el.el_next = NULL;
902                 **elpp = el;
903         }
904
905         return 0;
906 }
907
908 int
909 monitor_back_register_entry_callback(
910         struct berval           *ndn,
911         monitor_callback_t      *cb,
912         struct berval           *base,
913         int                     scope,
914         struct berval           *filter )
915 {
916         return monitor_back_register_entry_attrs( ndn, NULL, cb,
917                         base, scope, filter );
918 }
919
920 monitor_subsys_t *
921 monitor_back_get_subsys( const char *name )
922 {
923         if ( monitor_subsys != NULL ) {
924                 int     i;
925                 
926                 for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
927                         if ( strcasecmp( monitor_subsys[ i ]->mss_name, name ) == 0 ) {
928                                 return monitor_subsys[ i ];
929                         }
930                 }
931         }
932
933         return NULL;
934 }
935
936 monitor_subsys_t *
937 monitor_back_get_subsys_by_dn(
938         struct berval   *ndn,
939         int             sub )
940 {
941         if ( monitor_subsys != NULL ) {
942                 int     i;
943
944                 if ( sub ) {
945                         for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
946                                 if ( dnIsSuffix( ndn, &monitor_subsys[ i ]->mss_ndn ) ) {
947                                         return monitor_subsys[ i ];
948                                 }
949                         }
950
951                 } else {
952                         for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
953                                 if ( dn_match( ndn, &monitor_subsys[ i ]->mss_ndn ) ) {
954                                         return monitor_subsys[ i ];
955                                 }
956                         }
957                 }
958         }
959
960         return NULL;
961 }
962
963 int
964 monitor_back_initialize(
965         BackendInfo     *bi )
966 {
967         static char             *controls[] = {
968                 LDAP_CONTROL_MANAGEDSAIT,
969                 NULL
970         };
971
972         static ConfigTable monitorcfg[] = {
973                 { NULL, NULL, 0, 0, 0, ARG_IGNORED,
974                         NULL, NULL, NULL, NULL }
975         };
976
977         static ConfigOCs monitorocs[] = {
978                 { "( OLcfgDbOc:4.1 "
979                         "NAME 'olcMonitorConfig' "
980                         "DESC 'Monitor backend configuration' "
981                         "SUP olcDatabaseConfig "
982                         ")",
983                                 Cft_Database, monitorcfg },
984                 { NULL, 0, NULL }
985         };
986
987         struct m_s {
988                 char    *name;
989                 char    *schema;
990                 slap_mask_t flags;
991                 int     offset;
992         } moc[] = {
993                 { "monitor", "( 1.3.6.1.4.1.4203.666.3.16.1 "
994                         "NAME 'monitor' "
995                         "DESC 'OpenLDAP system monitoring' "
996                         "SUP top STRUCTURAL "
997                         "MUST cn "
998                         "MAY ( "
999                                 "description "
1000                                 "$ seeAlso "
1001                                 "$ labeledURI "
1002                                 "$ monitoredInfo "
1003                                 "$ managedInfo "
1004                                 "$ monitorOverlay "
1005                         ") )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1006                         offsetof(monitor_info_t, mi_oc_monitor) },
1007                 { "monitorServer", "( 1.3.6.1.4.1.4203.666.3.16.2 "
1008                         "NAME 'monitorServer' "
1009                         "DESC 'Server monitoring root entry' "
1010                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1011                         offsetof(monitor_info_t, mi_oc_monitorServer) },
1012                 { "monitorContainer", "( 1.3.6.1.4.1.4203.666.3.16.3 "
1013                         "NAME 'monitorContainer' "
1014                         "DESC 'monitor container class' "
1015                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1016                         offsetof(monitor_info_t, mi_oc_monitorContainer) },
1017                 { "monitorCounterObject", "( 1.3.6.1.4.1.4203.666.3.16.4 "
1018                         "NAME 'monitorCounterObject' "
1019                         "DESC 'monitor counter class' "
1020                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1021                         offsetof(monitor_info_t, mi_oc_monitorCounterObject) },
1022                 { "monitorOperation", "( 1.3.6.1.4.1.4203.666.3.16.5 "
1023                         "NAME 'monitorOperation' "
1024                         "DESC 'monitor operation class' "
1025                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1026                         offsetof(monitor_info_t, mi_oc_monitorOperation) },
1027                 { "monitorConnection", "( 1.3.6.1.4.1.4203.666.3.16.6 "
1028                         "NAME 'monitorConnection' "
1029                         "DESC 'monitor connection class' "
1030                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1031                         offsetof(monitor_info_t, mi_oc_monitorConnection) },
1032                 { "managedObject", "( 1.3.6.1.4.1.4203.666.3.16.7 "
1033                         "NAME 'managedObject' "
1034                         "DESC 'monitor managed entity class' "
1035                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1036                         offsetof(monitor_info_t, mi_oc_managedObject) },
1037                 { "monitoredObject", "( 1.3.6.1.4.1.4203.666.3.16.8 "
1038                         "NAME 'monitoredObject' "
1039                         "DESC 'monitor monitored entity class' "
1040                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1041                         offsetof(monitor_info_t, mi_oc_monitoredObject) },
1042                 { NULL, NULL, 0, -1 }
1043         }, mat[] = {
1044                 { "monitoredInfo", "( 1.3.6.1.4.1.4203.666.1.55.1 "
1045                         "NAME 'monitoredInfo' "
1046                         "DESC 'monitored info' "
1047                         /* "SUP name " */
1048                         "EQUALITY caseIgnoreMatch "
1049                         "SUBSTR caseIgnoreSubstringsMatch "
1050                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{32768} "
1051                         "NO-USER-MODIFICATION "
1052                         "USAGE directoryOperation )", SLAP_AT_HIDE,
1053                         offsetof(monitor_info_t, mi_ad_monitoredInfo) },
1054                 { "managedInfo", "( 1.3.6.1.4.1.4203.666.1.55.2 "
1055                         "NAME 'managedInfo' "
1056                         "DESC 'monitor managed info' "
1057                         "SUP name )", SLAP_AT_HIDE,
1058                         offsetof(monitor_info_t, mi_ad_managedInfo) },
1059                 { "monitorCounter", "( 1.3.6.1.4.1.4203.666.1.55.3 "
1060                         "NAME 'monitorCounter' "
1061                         "DESC 'monitor counter' "
1062                         "EQUALITY integerMatch "
1063                         "ORDERING integerOrderingMatch "
1064                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
1065                         "NO-USER-MODIFICATION "
1066                         "USAGE directoryOperation )", SLAP_AT_HIDE,
1067                         offsetof(monitor_info_t, mi_ad_monitorCounter) },
1068                 { "monitorOpCompleted", "( 1.3.6.1.4.1.4203.666.1.55.4 "
1069                         "NAME 'monitorOpCompleted' "
1070                         "DESC 'monitor completed operations' "
1071                         "SUP monitorCounter "
1072                         "NO-USER-MODIFICATION "
1073                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1074                         offsetof(monitor_info_t, mi_ad_monitorOpCompleted) },
1075                 { "monitorOpInitiated", "( 1.3.6.1.4.1.4203.666.1.55.5 "
1076                         "NAME 'monitorOpInitiated' "
1077                         "DESC 'monitor initiated operations' "
1078                         "SUP monitorCounter "
1079                         "NO-USER-MODIFICATION "
1080                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1081                         offsetof(monitor_info_t, mi_ad_monitorOpInitiated) },
1082                 { "monitorConnectionNumber", "( 1.3.6.1.4.1.4203.666.1.55.6 "
1083                         "NAME 'monitorConnectionNumber' "
1084                         "DESC 'monitor connection number' "
1085                         "SUP monitorCounter "
1086                         "NO-USER-MODIFICATION "
1087                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1088                         offsetof(monitor_info_t, mi_ad_monitorConnectionNumber) },
1089                 { "monitorConnectionAuthzDN", "( 1.3.6.1.4.1.4203.666.1.55.7 "
1090                         "NAME 'monitorConnectionAuthzDN' "
1091                         "DESC 'monitor connection authorization DN' "
1092                         /* "SUP distinguishedName " */
1093                         "EQUALITY distinguishedNameMatch "
1094                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
1095                         "NO-USER-MODIFICATION "
1096                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1097                         offsetof(monitor_info_t, mi_ad_monitorConnectionAuthzDN) },
1098                 { "monitorConnectionLocalAddress", "( 1.3.6.1.4.1.4203.666.1.55.8 "
1099                         "NAME 'monitorConnectionLocalAddress' "
1100                         "DESC 'monitor connection local address' "
1101                         "SUP monitoredInfo "
1102                         "NO-USER-MODIFICATION "
1103                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1104                         offsetof(monitor_info_t, mi_ad_monitorConnectionLocalAddress) },
1105                 { "monitorConnectionPeerAddress", "( 1.3.6.1.4.1.4203.666.1.55.9 "
1106                         "NAME 'monitorConnectionPeerAddress' "
1107                         "DESC 'monitor connection peer address' "
1108                         "SUP monitoredInfo "
1109                         "NO-USER-MODIFICATION "
1110                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1111                         offsetof(monitor_info_t, mi_ad_monitorConnectionPeerAddress) },
1112                 { "monitorTimestamp", "( 1.3.6.1.4.1.4203.666.1.55.10 "
1113                         "NAME 'monitorTimestamp' "
1114                         "DESC 'monitor timestamp' "
1115                         "EQUALITY generalizedTimeMatch "
1116                         "ORDERING generalizedTimeOrderingMatch "
1117                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
1118                         "SINGLE-VALUE "
1119                         "NO-USER-MODIFICATION "
1120                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1121                         offsetof(monitor_info_t, mi_ad_monitorTimestamp) },
1122                 { "monitorOverlay", "( 1.3.6.1.4.1.4203.666.1.55.11 "
1123                         "NAME 'monitorOverlay' "
1124                         "DESC 'name of overlays defined for a given database' "
1125                         "SUP monitoredInfo "
1126                         "NO-USER-MODIFICATION "
1127                         "USAGE directoryOperation )", SLAP_AT_HIDE,
1128                         offsetof(monitor_info_t, mi_ad_monitorOverlay) },
1129                 { "readOnly", "( 1.3.6.1.4.1.4203.666.1.55.12 "
1130                         "NAME 'readOnly' "
1131                         "DESC 'read/write status of a given database' "
1132                         "EQUALITY booleanMatch "
1133                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
1134                         "SINGLE-VALUE "
1135                         "USAGE directoryOperation )", SLAP_AT_HIDE,
1136                         offsetof(monitor_info_t, mi_ad_readOnly) },
1137                 { "restrictedOperation", "( 1.3.6.1.4.1.4203.666.1.55.13 "
1138                         "NAME 'restrictedOperation' "
1139                         "DESC 'name of restricted operation for a given database' "
1140                         "SUP managedInfo )", SLAP_AT_HIDE,
1141                         offsetof(monitor_info_t, mi_ad_restrictedOperation ) },
1142                 { "monitorConnectionProtocol", "( 1.3.6.1.4.1.4203.666.1.55.14 "
1143                         "NAME 'monitorConnectionProtocol' "
1144                         "DESC 'monitor connection protocol' "
1145                         "SUP monitoredInfo "
1146                         "NO-USER-MODIFICATION "
1147                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1148                         offsetof(monitor_info_t, mi_ad_monitorConnectionProtocol) },
1149                 { "monitorConnectionOpsReceived", "( 1.3.6.1.4.1.4203.666.1.55.15 "
1150                         "NAME 'monitorConnectionOpsReceived' "
1151                         "DESC 'monitor number of operations received by the connection' "
1152                         "SUP monitorCounter "
1153                         "NO-USER-MODIFICATION "
1154                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1155                         offsetof(monitor_info_t, mi_ad_monitorConnectionOpsReceived) },
1156                 { "monitorConnectionOpsExecuting", "( 1.3.6.1.4.1.4203.666.1.55.16 "
1157                         "NAME 'monitorConnectionOpsExecuting' "
1158                         "DESC 'monitor number of operations in execution within the connection' "
1159                         "SUP monitorCounter "
1160                         "NO-USER-MODIFICATION "
1161                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1162                         offsetof(monitor_info_t, mi_ad_monitorConnectionOpsExecuting) },
1163                 { "monitorConnectionOpsPending", "( 1.3.6.1.4.1.4203.666.1.55.17 "
1164                         "NAME 'monitorConnectionOpsPending' "
1165                         "DESC 'monitor number of pending operations within the connection' "
1166                         "SUP monitorCounter "
1167                         "NO-USER-MODIFICATION "
1168                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1169                         offsetof(monitor_info_t, mi_ad_monitorConnectionOpsPending) },
1170                 { "monitorConnectionOpsCompleted", "( 1.3.6.1.4.1.4203.666.1.55.18 "
1171                         "NAME 'monitorConnectionOpsCompleted' "
1172                         "DESC 'monitor number of operations completed within the connection' "
1173                         "SUP monitorCounter "
1174                         "NO-USER-MODIFICATION "
1175                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1176                         offsetof(monitor_info_t, mi_ad_monitorConnectionOpsCompleted) },
1177                 { "monitorConnectionGet", "( 1.3.6.1.4.1.4203.666.1.55.19 "
1178                         "NAME 'monitorConnectionGet' "
1179                         "DESC 'number of times connection_get() was called so far' "
1180                         "SUP monitorCounter "
1181                         "NO-USER-MODIFICATION "
1182                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1183                         offsetof(monitor_info_t, mi_ad_monitorConnectionGet) },
1184                 { "monitorConnectionRead", "( 1.3.6.1.4.1.4203.666.1.55.20 "
1185                         "NAME 'monitorConnectionRead' "
1186                         "DESC 'number of times connection_read() was called so far' "
1187                         "SUP monitorCounter "
1188                         "NO-USER-MODIFICATION "
1189                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1190                         offsetof(monitor_info_t, mi_ad_monitorConnectionRead) },
1191                 { "monitorConnectionWrite", "( 1.3.6.1.4.1.4203.666.1.55.21 "
1192                         "NAME 'monitorConnectionWrite' "
1193                         "DESC 'number of times connection_write() was called so far' "
1194                         "SUP monitorCounter "
1195                         "NO-USER-MODIFICATION "
1196                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1197                         offsetof(monitor_info_t, mi_ad_monitorConnectionWrite) },
1198                 { "monitorConnectionMask", "( 1.3.6.1.4.1.4203.666.1.55.22 "
1199                         "NAME 'monitorConnectionMask' "
1200                         "DESC 'monitor connection mask' "
1201                         "SUP monitoredInfo "
1202                         "NO-USER-MODIFICATION "
1203                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1204                         offsetof(monitor_info_t, mi_ad_monitorConnectionMask) },
1205                 { "monitorConnectionListener", "( 1.3.6.1.4.1.4203.666.1.55.23 "
1206                         "NAME 'monitorConnectionListener' "
1207                         "DESC 'monitor connection listener' "
1208                         "SUP monitoredInfo "
1209                         "NO-USER-MODIFICATION "
1210                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1211                         offsetof(monitor_info_t, mi_ad_monitorConnectionListener) },
1212                 { "monitorConnectionPeerDomain", "( 1.3.6.1.4.1.4203.666.1.55.24 "
1213                         "NAME 'monitorConnectionPeerDomain' "
1214                         "DESC 'monitor connection peer domain' "
1215                         "SUP monitoredInfo "
1216                         "NO-USER-MODIFICATION "
1217                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1218                         offsetof(monitor_info_t, mi_ad_monitorConnectionPeerDomain) },
1219                 { "monitorConnectionStartTime", "( 1.3.6.1.4.1.4203.666.1.55.25 "
1220                         "NAME 'monitorConnectionStartTime' "
1221                         "DESC 'monitor connection start time' "
1222                         "SUP monitorTimestamp "
1223                         "SINGLE-VALUE "
1224                         "NO-USER-MODIFICATION "
1225                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1226                         offsetof(monitor_info_t, mi_ad_monitorConnectionStartTime) },
1227                 { "monitorConnectionActivityTime", "( 1.3.6.1.4.1.4203.666.1.55.26 "
1228                         "NAME 'monitorConnectionActivityTime' "
1229                         "DESC 'monitor connection activity time' "
1230                         "SUP monitorTimestamp "
1231                         "SINGLE-VALUE "
1232                         "NO-USER-MODIFICATION "
1233                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1234                         offsetof(monitor_info_t, mi_ad_monitorConnectionActivityTime) },
1235                 { "monitorIsShadow", "( 1.3.6.1.4.1.4203.666.1.55.27 "
1236                         "NAME 'monitorIsShadow' "
1237                         "DESC 'TRUE if the database is shadow' "
1238                         "EQUALITY booleanMatch "
1239                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
1240                         "SINGLE-VALUE "
1241                         "USAGE directoryOperation )", SLAP_AT_HIDE,
1242                         offsetof(monitor_info_t, mi_ad_monitorIsShadow) },
1243                 { "monitorUpdateRef", "( 1.3.6.1.4.1.4203.666.1.55.28 "
1244                         "NAME 'monitorUpdateRef' "
1245                         "DESC 'update referral for shadow databases' "
1246                         "SUP monitoredInfo "
1247                         "SINGLE-VALUE "
1248                         "USAGE directoryOperation )", SLAP_AT_HIDE,
1249                         offsetof(monitor_info_t, mi_ad_monitorUpdateRef) },
1250                 { "monitorRuntimeConfig", "( 1.3.6.1.4.1.4203.666.1.55.29 "
1251                         "NAME 'monitorRuntimeConfig' "
1252                         "DESC 'TRUE if component allows runtime configuration' "
1253                         "EQUALITY booleanMatch "
1254                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
1255                         "SINGLE-VALUE "
1256                         "USAGE directoryOperation )", SLAP_AT_HIDE,
1257                         offsetof(monitor_info_t, mi_ad_monitorRuntimeConfig) },
1258                 { NULL, NULL, 0, -1 }
1259         };
1260
1261         int                     i, rc;
1262         const char              *text;
1263         monitor_info_t          *mi = &monitor_info;
1264
1265         /* schema integration */
1266         for ( i = 0; mat[ i ].name; i++ ) {
1267                 LDAPAttributeType       *at;
1268                 int                     code;
1269                 const char              *err;
1270                 AttributeDescription    **ad;
1271
1272                 at = ldap_str2attributetype( mat[ i ].schema, &code,
1273                         &err, LDAP_SCHEMA_ALLOW_ALL );
1274                 if ( !at ) {
1275                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
1276                                 "in AttributeType \"%s\" %s before %s\n",
1277                                 mat[ i ].name, ldap_scherr2str(code), err );
1278                         return -1;
1279                 }
1280
1281                 if ( at->at_oid == NULL ) {
1282                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
1283                                 "null OID for attributeType \"%s\"\n",
1284                                 mat[ i ].name, 0, 0 );
1285                         return -1;
1286                 }
1287
1288                 code = at_add(at, 0, NULL, &err);
1289                 if ( code ) {
1290                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
1291                                 "%s in attributeType \"%s\"\n",
1292                                 scherr2str(code), mat[ i ].name, 0 );
1293                         return -1;
1294                 }
1295                 ldap_memfree(at);
1296
1297                 ad = ((AttributeDescription **)&(((char *)mi)[ mat[ i ].offset ]));
1298                 ad[ 0 ] = NULL;
1299                 if ( slap_str2ad( mat[ i ].name, ad, &text ) ) {
1300                         Debug( LDAP_DEBUG_ANY,
1301                                 "monitor_back_db_init: %s\n", text, 0, 0 );
1302                         return -1;
1303                 }
1304
1305                 (*ad)->ad_type->sat_flags |= mat[ i ].flags;
1306         }
1307
1308         for ( i = 0; moc[ i ].name; i++ ) {
1309                 LDAPObjectClass         *oc;
1310                 int                     code;
1311                 const char              *err;
1312                 ObjectClass             *Oc;
1313
1314                 oc = ldap_str2objectclass(moc[ i ].schema, &code, &err,
1315                                 LDAP_SCHEMA_ALLOW_ALL );
1316                 if ( !oc ) {
1317                         Debug( LDAP_DEBUG_ANY,
1318                                 "unable to parse monitor objectclass \"%s\": "
1319                                 "%s before %s\n" , moc[ i ].name,
1320                                 ldap_scherr2str(code), err );
1321                         return -1;
1322                 }
1323
1324                 if ( oc->oc_oid == NULL ) {
1325                         Debug( LDAP_DEBUG_ANY,
1326                                 "objectclass \"%s\" has no OID\n" ,
1327                                 moc[ i ].name, 0, 0 );
1328                         return -1;
1329                 }
1330
1331                 code = oc_add(oc, 0, NULL, &err);
1332                 if ( code ) {
1333                         Debug( LDAP_DEBUG_ANY,
1334                                 "objectclass \"%s\": %s \"%s\"\n" ,
1335                                 moc[ i ].name, scherr2str(code), err );
1336                         return -1;
1337                 }
1338
1339                 ldap_memfree(oc);
1340
1341                 Oc = oc_find( moc[ i ].name );
1342                 if ( Oc == NULL ) {
1343                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
1344                                         "unable to find objectClass %s "
1345                                         "(just added)\n", moc[ i ].name, 0, 0 );
1346                         return -1;
1347                 }
1348
1349                 Oc->soc_flags |= moc[ i ].flags;
1350
1351                 ((ObjectClass **)&(((char *)mi)[ moc[ i ].offset ]))[ 0 ] = Oc;
1352         }
1353
1354         bi->bi_controls = controls;
1355
1356         bi->bi_init = 0;
1357         bi->bi_open = 0;
1358         bi->bi_config = monitor_back_config;
1359         bi->bi_close = 0;
1360         bi->bi_destroy = 0;
1361
1362         bi->bi_db_init = monitor_back_db_init;
1363 #if 0
1364         bi->bi_db_config = monitor_back_db_config;
1365 #endif
1366         bi->bi_db_open = monitor_back_db_open;
1367         bi->bi_db_close = 0;
1368         bi->bi_db_destroy = monitor_back_db_destroy;
1369
1370         bi->bi_op_bind = monitor_back_bind;
1371         bi->bi_op_unbind = 0;
1372         bi->bi_op_search = monitor_back_search;
1373         bi->bi_op_compare = monitor_back_compare;
1374         bi->bi_op_modify = monitor_back_modify;
1375         bi->bi_op_modrdn = 0;
1376         bi->bi_op_add = 0;
1377         bi->bi_op_delete = 0;
1378         bi->bi_op_abandon = 0;
1379
1380         bi->bi_extended = 0;
1381
1382         bi->bi_entry_release_rw = 0;
1383         bi->bi_chk_referrals = 0;
1384         bi->bi_operational = monitor_back_operational;
1385
1386         /*
1387          * hooks for slap tools
1388          */
1389         bi->bi_tool_entry_open = 0;
1390         bi->bi_tool_entry_close = 0;
1391         bi->bi_tool_entry_first = 0;
1392         bi->bi_tool_entry_next = 0;
1393         bi->bi_tool_entry_get = 0;
1394         bi->bi_tool_entry_put = 0;
1395         bi->bi_tool_entry_reindex = 0;
1396         bi->bi_tool_sync = 0;
1397         bi->bi_tool_dn2id_get = 0;
1398         bi->bi_tool_id2entry_get = 0;
1399         bi->bi_tool_entry_modify = 0;
1400
1401         bi->bi_connection_init = 0;
1402         bi->bi_connection_destroy = 0;
1403
1404         /*
1405          * configuration objectClasses (fake)
1406          */
1407         bi->bi_cf_ocs = monitorocs;
1408
1409         rc = config_register_schema( monitorcfg, monitorocs );
1410         if ( rc ) {
1411                 return rc;
1412         }
1413
1414         return 0;
1415 }
1416
1417 int
1418 monitor_back_db_init(
1419         BackendDB       *be )
1420 {
1421         int                     rc;
1422         struct berval           dn = BER_BVC( SLAPD_MONITOR_DN ),
1423                                 pdn,
1424                                 ndn;
1425         BackendDB               *be2;
1426
1427         monitor_subsys_t        *ms;
1428
1429         /*
1430          * register subsys
1431          */
1432         for ( ms = known_monitor_subsys; ms->mss_name != NULL; ms++ ) {
1433                 if ( monitor_back_register_subsys( ms ) ) {
1434                         return -1;
1435                 }
1436         }
1437
1438         /*
1439          * database monitor can be defined once only
1440          */
1441         if ( be_monitor != NULL ) {
1442                 Debug( LDAP_DEBUG_ANY,
1443                         "only one monitor database is allowed\n", 0, 0, 0 );
1444                 return( -1 );
1445         }
1446         be_monitor = be;
1447
1448         /* indicate system schema supported */
1449         SLAP_BFLAGS(be) |= SLAP_BFLAG_MONITOR;
1450
1451         rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn, NULL );
1452         if( rc != LDAP_SUCCESS ) {
1453                 Debug( LDAP_DEBUG_ANY,
1454                         "unable to normalize/pretty monitor DN \"%s\" (%d)\n",
1455                         dn.bv_val, rc, 0 );
1456                 return -1;
1457         }
1458
1459         ber_bvarray_add( &be->be_suffix, &pdn );
1460         ber_bvarray_add( &be->be_nsuffix, &ndn );
1461
1462         /* NOTE: only one monitor database is allowed,
1463          * so we use static storage */
1464         ldap_pvt_thread_mutex_init( &monitor_info.mi_cache_mutex );
1465
1466         be->be_private = &monitor_info;
1467
1468         be2 = select_backend( &ndn, 0, 0 );
1469         if ( be2 != be ) {
1470                 char    *type = be2->bd_info->bi_type;
1471
1472                 if ( overlay_is_over( be2 ) ) {
1473                         slap_overinfo   *oi = (slap_overinfo *)be2->bd_info->bi_private;
1474                         type = oi->oi_orig->bi_type;
1475                 }
1476
1477                 Debug( LDAP_DEBUG_ANY,
1478                         "\"monitor\" database serving namingContext \"%s\" "
1479                         "is hidden by \"%s\" database serving namingContext \"%s\".\n",
1480                         pdn.bv_val, type, be2->be_nsuffix[ 0 ].bv_val );
1481                 return -1;
1482         }
1483
1484         return 0;
1485 }
1486
1487 int
1488 monitor_back_db_open(
1489         BackendDB       *be )
1490 {
1491         monitor_info_t          *mi = (monitor_info_t *)be->be_private;
1492         struct monitor_subsys_t **ms;
1493         Entry                   *e, **ep;
1494         monitor_entry_t         *mp;
1495         int                     i;
1496         char                    buf[ BACKMONITOR_BUFSIZE ];
1497         struct berval           bv;
1498         struct tm               *tms;
1499 #ifdef HAVE_GMTIME_R
1500         struct tm               tm_buf;
1501 #endif
1502         static char             tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1503
1504         assert( be_monitor != NULL );
1505         if ( be != be_monitor ) {
1506                 be_monitor = be;
1507         }
1508
1509         /*
1510          * Start
1511          */
1512 #ifndef HAVE_GMTIME_R
1513         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1514 #endif
1515 #ifdef HACK_LOCAL_TIME
1516 # ifdef HAVE_LOCALTIME_R
1517         tms = localtime_r( &starttime, &tm_buf );
1518 # else
1519         tms = localtime( &starttime );
1520 # endif /* HAVE_LOCALTIME_R */
1521         lutil_localtime( tmbuf, sizeof(tmbuf), tms, -timezone );
1522 #else /* !HACK_LOCAL_TIME */
1523 # ifdef HAVE_GMTIME_R
1524         tms = gmtime_r( &starttime, &tm_buf );
1525 # else
1526         tms = gmtime( &starttime );
1527 # endif /* HAVE_GMTIME_R */
1528         lutil_gentime( tmbuf, sizeof(tmbuf), tms );
1529 #endif /* !HACK_LOCAL_TIME */
1530 #ifndef HAVE_GMTIME_R
1531         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1532 #endif
1533
1534         mi->mi_startTime.bv_val = tmbuf;
1535         mi->mi_startTime.bv_len = strlen( tmbuf );
1536
1537         if ( BER_BVISEMPTY( &be->be_rootdn ) ) {
1538                 BER_BVSTR( &mi->mi_creatorsName, SLAPD_ANONYMOUS );
1539         } else {
1540                 mi->mi_creatorsName = be->be_rootdn;
1541         }
1542
1543         /*
1544          * creates the "cn=Monitor" entry 
1545          */
1546         snprintf( buf, sizeof( buf ), 
1547                 "dn: %s\n"
1548                 "objectClass: %s\n"
1549                 "structuralObjectClass: %s\n"
1550                 "cn: Monitor\n"
1551                 "description: This subtree contains monitoring/managing objects.\n"
1552                 "description: This object contains information about this server.\n"
1553                 "description: Most of the information is held in operational"
1554                 " attributes, which must be explicitly requested.\n"
1555                 "creatorsName: %s\n"
1556                 "modifiersName: %s\n"
1557                 "createTimestamp: %s\n"
1558                 "modifyTimestamp: %s\n",
1559                 SLAPD_MONITOR_DN,
1560                 mi->mi_oc_monitorServer->soc_cname.bv_val,
1561                 mi->mi_oc_monitorServer->soc_cname.bv_val,
1562                 mi->mi_creatorsName.bv_val,
1563                 mi->mi_creatorsName.bv_val,
1564                 mi->mi_startTime.bv_val,
1565                 mi->mi_startTime.bv_val );
1566
1567         e = str2entry( buf );
1568         if ( e == NULL) {
1569                 Debug( LDAP_DEBUG_ANY,
1570                         "unable to create \"%s\" entry\n",
1571                         SLAPD_MONITOR_DN, 0, 0 );
1572                 return( -1 );
1573         }
1574
1575         bv.bv_val = strchr( (char *) Versionstr, '$' );
1576         if ( bv.bv_val != NULL ) {
1577                 char    *end;
1578
1579                 bv.bv_val++;
1580                 for ( ; bv.bv_val[ 0 ] == ' '; bv.bv_val++ )
1581                         ;
1582
1583                 end = strchr( bv.bv_val, '$' );
1584                 if ( end != NULL ) {
1585                         end--;
1586
1587                         for ( ; end > bv.bv_val && end[ 0 ] == ' '; end-- )
1588                                 ;
1589
1590                         end++;
1591
1592                         bv.bv_len = end - bv.bv_val;
1593
1594                 } else {
1595                         bv.bv_len = strlen( bv.bv_val );
1596                 }
1597
1598                 if ( attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo,
1599                                         &bv, NULL ) ) {
1600                         Debug( LDAP_DEBUG_ANY,
1601                                 "unable to add monitoredInfo to \"%s\" entry\n",
1602                                 SLAPD_MONITOR_DN, 0, 0 );
1603                         return( -1 );
1604                 }
1605         }
1606
1607         mp = monitor_entrypriv_create();
1608         if ( mp == NULL ) {
1609                 return -1;
1610         }
1611         e->e_private = ( void * )mp;
1612         ep = &mp->mp_children;
1613
1614         if ( monitor_cache_add( mi, e ) ) {
1615                 Debug( LDAP_DEBUG_ANY,
1616                         "unable to add entry \"%s\" to cache\n",
1617                         SLAPD_MONITOR_DN, 0, 0 );
1618                 return -1;
1619         }
1620
1621         /*      
1622          * Create all the subsystem specific entries
1623          */
1624         for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
1625                 int             len = strlen( monitor_subsys[ i ]->mss_name );
1626                 struct berval   dn;
1627                 int             rc;
1628
1629                 dn.bv_len = len + sizeof( "cn=" ) - 1;
1630                 dn.bv_val = ch_calloc( sizeof( char ), dn.bv_len + 1 );
1631                 strcpy( dn.bv_val, "cn=" );
1632                 strcat( dn.bv_val, monitor_subsys[ i ]->mss_name );
1633                 rc = dnPretty( NULL, &dn, &monitor_subsys[ i ]->mss_rdn, NULL );
1634                 free( dn.bv_val );
1635                 if ( rc != LDAP_SUCCESS ) {
1636                         Debug( LDAP_DEBUG_ANY,
1637                                 "monitor RDN \"%s\" is invalid\n", 
1638                                 dn.bv_val, 0, 0 );
1639                         return( -1 );
1640                 }
1641
1642                 dn.bv_len += sizeof( SLAPD_MONITOR_DN ); /* 1 for the , */
1643                 dn.bv_val = ch_malloc( dn.bv_len + 1 );
1644                 strcpy( dn.bv_val , monitor_subsys[ i ]->mss_rdn.bv_val );
1645                 strcat( dn.bv_val, "," SLAPD_MONITOR_DN );
1646                 rc = dnPrettyNormal( NULL, &dn, &monitor_subsys[ i ]->mss_dn,
1647                         &monitor_subsys[ i ]->mss_ndn, NULL );
1648                 free( dn.bv_val );
1649                 if ( rc != LDAP_SUCCESS ) {
1650                         Debug( LDAP_DEBUG_ANY,
1651                                 "monitor DN \"%s\" is invalid\n", 
1652                                 dn.bv_val, 0, 0 );
1653                         return( -1 );
1654                 }
1655
1656                 snprintf( buf, sizeof( buf ),
1657                                 "dn: %s\n"
1658                                 "objectClass: %s\n"
1659                                 "structuralObjectClass: %s\n"
1660                                 "cn: %s\n"
1661                                 "creatorsName: %s\n"
1662                                 "modifiersName: %s\n"
1663                                 "createTimestamp: %s\n"
1664                                 "modifyTimestamp: %s\n",
1665                                 monitor_subsys[ i ]->mss_dn.bv_val,
1666                                 mi->mi_oc_monitorContainer->soc_cname.bv_val,
1667                                 mi->mi_oc_monitorContainer->soc_cname.bv_val,
1668                                 monitor_subsys[ i ]->mss_name,
1669                                 mi->mi_creatorsName.bv_val,
1670                                 mi->mi_creatorsName.bv_val,
1671                                 mi->mi_startTime.bv_val,
1672                                 mi->mi_startTime.bv_val );
1673                 
1674                 e = str2entry( buf );
1675                 
1676                 if ( e == NULL) {
1677                         Debug( LDAP_DEBUG_ANY,
1678                                 "unable to create \"%s\" entry\n", 
1679                                 monitor_subsys[ i ]->mss_dn.bv_val, 0, 0 );
1680                         return( -1 );
1681                 }
1682
1683                 if ( !BER_BVISNULL( &monitor_subsys[ i ]->mss_desc[ 0 ] ) ) {
1684                         attr_merge_normalize( e, slap_schema.si_ad_description,
1685                                         monitor_subsys[ i ]->mss_desc, NULL );
1686                 }
1687
1688                 mp = monitor_entrypriv_create();
1689                 if ( mp == NULL ) {
1690                         return -1;
1691                 }
1692                 e->e_private = ( void * )mp;
1693                 mp->mp_info = monitor_subsys[ i ];
1694                 mp->mp_flags = monitor_subsys[ i ]->mss_flags;
1695
1696                 if ( monitor_cache_add( mi, e ) ) {
1697                         Debug( LDAP_DEBUG_ANY,
1698                                 "unable to add entry \"%s\" to cache\n",
1699                                 monitor_subsys[ i ]->mss_dn.bv_val, 0, 0 );
1700                         return -1;
1701                 }
1702
1703                 *ep = e;
1704                 ep = &mp->mp_next;
1705         }
1706
1707         assert( be != NULL );
1708
1709         be->be_private = mi;
1710         
1711         /*
1712          * opens the monitor backend subsystems
1713          */
1714         for ( ms = monitor_subsys; ms[ 0 ] != NULL; ms++ ) {
1715                 if ( ms[ 0 ]->mss_open && ( *ms[ 0 ]->mss_open )( be, ms[ 0 ] ) )
1716                 {
1717                         return( -1 );
1718                 }
1719                 ms[ 0 ]->mss_flags |= MONITOR_F_OPENED;
1720         }
1721
1722         monitor_subsys_opened = 1;
1723
1724         if ( mi->mi_entry_limbo ) {
1725                 entry_limbo_t   *el = (entry_limbo_t *)mi->mi_entry_limbo;
1726
1727                 for ( ; el; ) {
1728                         entry_limbo_t   *tmp;
1729
1730                         switch ( el->el_type ) {
1731                         case LIMBO_ENTRY:
1732                                 monitor_back_register_entry(
1733                                                 el->el_e,
1734                                                 el->el_cb );
1735                                 break;
1736
1737                         case LIMBO_ENTRY_PARENT:
1738                                 monitor_back_register_entry_parent(
1739                                                 el->el_e,
1740                                                 el->el_cb,
1741                                                 &el->el_base,
1742                                                 el->el_scope,
1743                                                 &el->el_filter );
1744                                 break;
1745                                 
1746
1747                         case LIMBO_ATTRS:
1748                                 monitor_back_register_entry_attrs(
1749                                                 &el->el_ndn,
1750                                                 el->el_a,
1751                                                 el->el_cb,
1752                                                 &el->el_base,
1753                                                 el->el_scope,
1754                                                 &el->el_filter );
1755                                 break;
1756
1757                         case LIMBO_CB:
1758                                 monitor_back_register_entry_callback(
1759                                                 &el->el_ndn,
1760                                                 el->el_cb,
1761                                                 &el->el_base,
1762                                                 el->el_scope,
1763                                                 &el->el_filter );
1764                                 break;
1765
1766                         default:
1767                                 assert( 0 );
1768                         }
1769
1770                         if ( el->el_e ) {
1771                                 entry_free( el->el_e );
1772                         }
1773                         if ( el->el_a ) {
1774                                 attrs_free( el->el_a );
1775                         }
1776                         if ( !BER_BVISNULL( &el->el_ndn ) ) {
1777                                 ber_memfree( el->el_ndn.bv_val );
1778                         }
1779                         if ( !BER_BVISNULL( &el->el_base ) ) {
1780                                 ber_memfree( el->el_base.bv_val );
1781                         }
1782                         if ( !BER_BVISNULL( &el->el_filter ) ) {
1783                                 ber_memfree( el->el_filter.bv_val );
1784                         }
1785
1786                         tmp = el;
1787                         el = el->el_next;
1788                         ch_free( tmp );
1789                 }
1790
1791                 mi->mi_entry_limbo = NULL;
1792         }
1793
1794         return( 0 );
1795 }
1796
1797 int
1798 monitor_back_config(
1799         BackendInfo     *bi,
1800         const char      *fname,
1801         int             lineno,
1802         int             argc,
1803         char            **argv )
1804 {
1805         /*
1806          * eventually, will hold backend specific configuration parameters
1807          */
1808         return SLAP_CONF_UNKNOWN;
1809 }
1810
1811 #if 0
1812 int
1813 monitor_back_db_config(
1814         Backend     *be,
1815         const char  *fname,
1816         int         lineno,
1817         int         argc,
1818         char        **argv )
1819 {
1820         monitor_info_t  *mi = ( monitor_info_t * )be->be_private;
1821
1822         /*
1823          * eventually, will hold database specific configuration parameters
1824          */
1825         return SLAP_CONF_UNKNOWN;
1826 }
1827 #endif
1828
1829 int
1830 monitor_back_db_destroy(
1831         BackendDB       *be )
1832 {
1833         monitor_info_t  *mi = ( monitor_info_t * )be->be_private;
1834
1835         if ( mi == NULL ) {
1836                 return -1;
1837         }
1838
1839         /*
1840          * FIXME: destroys all the data
1841          */
1842         /* NOTE: mi points to static storage; don't free it */
1843         
1844         (void)monitor_cache_destroy( mi );
1845
1846         if ( monitor_subsys ) {
1847                 int     i;
1848
1849                 for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
1850                         if ( monitor_subsys[ i ]->mss_destroy ) {
1851                                 monitor_subsys[ i ]->mss_destroy( be, monitor_subsys[ i ] );
1852                         }
1853
1854                         if ( !BER_BVISNULL( &monitor_subsys[ i ]->mss_rdn ) ) {
1855                                 ch_free( monitor_subsys[ i ]->mss_rdn.bv_val );
1856                         }
1857
1858                         if ( !BER_BVISNULL( &monitor_subsys[ i ]->mss_dn ) ) {
1859                                 ch_free( monitor_subsys[ i ]->mss_dn.bv_val );
1860                         }
1861
1862                         if ( !BER_BVISNULL( &monitor_subsys[ i ]->mss_ndn ) ) {
1863                                 ch_free( monitor_subsys[ i ]->mss_ndn.bv_val );
1864                         }
1865                 }
1866
1867                 ch_free( monitor_subsys );
1868         }
1869         
1870         ldap_pvt_thread_mutex_destroy( &monitor_info.mi_cache_mutex );
1871
1872         be->be_private = NULL;
1873
1874         return 0;
1875 }
1876
1877 #if SLAPD_MONITOR == SLAPD_MOD_DYNAMIC
1878
1879 /* conditionally define the init_module() function */
1880 SLAP_BACKEND_INIT_MODULE( monitor )
1881
1882 #endif /* SLAPD_MONITOR == SLAPD_MOD_DYNAMIC */
1883