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