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