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