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