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