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