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