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