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