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