]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/init.c
coverity scan, e is non-NULL
[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                 monitor_cache_release( mi, e );
1500
1501         } else {
1502                 entry_limbo_t   **elpp;
1503
1504                 for ( elpp = (entry_limbo_t **)&mi->mi_entry_limbo;
1505                         *elpp;
1506                         elpp = &(*elpp)->el_next )
1507                 {
1508                         entry_limbo_t   *elp = *elpp;
1509
1510                         if ( elp->el_type == LIMBO_ATTRS
1511                                 && dn_match( nbase, &elp->el_nbase )
1512                                 && scope == elp->el_scope
1513                                 && bvmatch( filter, &elp->el_filter ) )
1514                         {
1515                                 monitor_callback_t      *cb, *next;
1516
1517                                 for ( cb = elp->el_cb; cb; cb = next ) {
1518                                         /* FIXME: call callbacks? */
1519                                         next = cb->mc_next;
1520                                         ch_free( cb );
1521                                 }
1522                                 assert( elp->el_e == NULL );
1523                                 if ( elp->el_a != NULL ) {
1524                                         attrs_free( elp->el_a );
1525                                 }
1526                                 if ( !BER_BVISNULL( &elp->el_nbase ) ) {
1527                                         ch_free( elp->el_nbase.bv_val );
1528                                 }
1529                                 if ( !BER_BVISNULL( &elp->el_filter ) ) {
1530                                         ch_free( elp->el_filter.bv_val );
1531                                 }
1532                                 *elpp = elp->el_next;
1533                                 ch_free( elp );
1534                                 elpp = NULL;
1535                                 break;
1536                         }
1537                 }
1538
1539                 if ( elpp != NULL ) {
1540                         /* not found!  where did it go? */
1541                         return 1;
1542                 }
1543         }
1544
1545         return 0;
1546 }
1547
1548 int
1549 monitor_back_unregister_entry_callback(
1550         struct berval           *ndn,
1551         monitor_callback_t      *cb,
1552         struct berval           *nbase,
1553         int                     scope,
1554         struct berval           *filter )
1555 {
1556         /* TODO: lookup entry (by ndn, if not NULL, and/or by callback);
1557          * unregister the callback; if a is not null, unregister the
1558          * given attrs.  In any case, call cb->cb_free */
1559         return monitor_back_unregister_entry_attrs( ndn,
1560                 NULL, cb, nbase, scope, filter );
1561 }
1562
1563 monitor_subsys_t *
1564 monitor_back_get_subsys( const char *name )
1565 {
1566         if ( monitor_subsys != NULL ) {
1567                 int     i;
1568                 
1569                 for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
1570                         if ( strcasecmp( monitor_subsys[ i ]->mss_name, name ) == 0 ) {
1571                                 return monitor_subsys[ i ];
1572                         }
1573                 }
1574         }
1575
1576         return NULL;
1577 }
1578
1579 monitor_subsys_t *
1580 monitor_back_get_subsys_by_dn(
1581         struct berval   *ndn,
1582         int             sub )
1583 {
1584         if ( monitor_subsys != NULL ) {
1585                 int     i;
1586
1587                 if ( sub ) {
1588                         for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
1589                                 if ( dnIsSuffix( ndn, &monitor_subsys[ i ]->mss_ndn ) ) {
1590                                         return monitor_subsys[ i ];
1591                                 }
1592                         }
1593
1594                 } else {
1595                         for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
1596                                 if ( dn_match( ndn, &monitor_subsys[ i ]->mss_ndn ) ) {
1597                                         return monitor_subsys[ i ];
1598                                 }
1599                         }
1600                 }
1601         }
1602
1603         return NULL;
1604 }
1605
1606 int
1607 monitor_back_initialize(
1608         BackendInfo     *bi )
1609 {
1610         static char             *controls[] = {
1611                 LDAP_CONTROL_MANAGEDSAIT,
1612                 NULL
1613         };
1614
1615         static ConfigTable monitorcfg[] = {
1616                 { NULL, NULL, 0, 0, 0, ARG_IGNORED,
1617                         NULL, NULL, NULL, NULL }
1618         };
1619
1620         static ConfigOCs monitorocs[] = {
1621                 { "( OLcfgDbOc:4.1 "
1622                         "NAME 'olcMonitorConfig' "
1623                         "DESC 'Monitor backend configuration' "
1624                         "SUP olcDatabaseConfig "
1625                         ")",
1626                                 Cft_Database, monitorcfg },
1627                 { NULL, 0, NULL }
1628         };
1629
1630         struct m_s {
1631                 char    *schema;
1632                 slap_mask_t flags;
1633                 int     offset;
1634         } moc[] = {
1635                 { "( 1.3.6.1.4.1.4203.666.3.16.1 "
1636                         "NAME 'monitor' "
1637                         "DESC 'OpenLDAP system monitoring' "
1638                         "SUP top STRUCTURAL "
1639                         "MUST cn "
1640                         "MAY ( "
1641                                 "description "
1642                                 "$ seeAlso "
1643                                 "$ labeledURI "
1644                                 "$ monitoredInfo "
1645                                 "$ managedInfo "
1646                                 "$ monitorOverlay "
1647                         ") )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1648                         offsetof(monitor_info_t, mi_oc_monitor) },
1649                 { "( 1.3.6.1.4.1.4203.666.3.16.2 "
1650                         "NAME 'monitorServer' "
1651                         "DESC 'Server monitoring root entry' "
1652                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1653                         offsetof(monitor_info_t, mi_oc_monitorServer) },
1654                 { "( 1.3.6.1.4.1.4203.666.3.16.3 "
1655                         "NAME 'monitorContainer' "
1656                         "DESC 'monitor container class' "
1657                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1658                         offsetof(monitor_info_t, mi_oc_monitorContainer) },
1659                 { "( 1.3.6.1.4.1.4203.666.3.16.4 "
1660                         "NAME 'monitorCounterObject' "
1661                         "DESC 'monitor counter class' "
1662                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1663                         offsetof(monitor_info_t, mi_oc_monitorCounterObject) },
1664                 { "( 1.3.6.1.4.1.4203.666.3.16.5 "
1665                         "NAME 'monitorOperation' "
1666                         "DESC 'monitor operation class' "
1667                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1668                         offsetof(monitor_info_t, mi_oc_monitorOperation) },
1669                 { "( 1.3.6.1.4.1.4203.666.3.16.6 "
1670                         "NAME 'monitorConnection' "
1671                         "DESC 'monitor connection class' "
1672                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1673                         offsetof(monitor_info_t, mi_oc_monitorConnection) },
1674                 { "( 1.3.6.1.4.1.4203.666.3.16.7 "
1675                         "NAME 'managedObject' "
1676                         "DESC 'monitor managed entity class' "
1677                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1678                         offsetof(monitor_info_t, mi_oc_managedObject) },
1679                 { "( 1.3.6.1.4.1.4203.666.3.16.8 "
1680                         "NAME 'monitoredObject' "
1681                         "DESC 'monitor monitored entity class' "
1682                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
1683                         offsetof(monitor_info_t, mi_oc_monitoredObject) },
1684                 { NULL, 0, -1 }
1685         }, mat[] = {
1686                 { "( 1.3.6.1.4.1.4203.666.1.55.1 "
1687                         "NAME 'monitoredInfo' "
1688                         "DESC 'monitored info' "
1689                         /* "SUP name " */
1690                         "EQUALITY caseIgnoreMatch "
1691                         "SUBSTR caseIgnoreSubstringsMatch "
1692                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{32768} "
1693                         "NO-USER-MODIFICATION "
1694                         "USAGE dSAOperation )", SLAP_AT_HIDE,
1695                         offsetof(monitor_info_t, mi_ad_monitoredInfo) },
1696                 { "( 1.3.6.1.4.1.4203.666.1.55.2 "
1697                         "NAME 'managedInfo' "
1698                         "DESC 'monitor managed info' "
1699                         "SUP name )", SLAP_AT_HIDE,
1700                         offsetof(monitor_info_t, mi_ad_managedInfo) },
1701                 { "( 1.3.6.1.4.1.4203.666.1.55.3 "
1702                         "NAME 'monitorCounter' "
1703                         "DESC 'monitor counter' "
1704                         "EQUALITY integerMatch "
1705                         "ORDERING integerOrderingMatch "
1706                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
1707                         "NO-USER-MODIFICATION "
1708                         "USAGE dSAOperation )", SLAP_AT_HIDE,
1709                         offsetof(monitor_info_t, mi_ad_monitorCounter) },
1710                 { "( 1.3.6.1.4.1.4203.666.1.55.4 "
1711                         "NAME 'monitorOpCompleted' "
1712                         "DESC 'monitor completed operations' "
1713                         "SUP monitorCounter "
1714                         "NO-USER-MODIFICATION "
1715                         "USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1716                         offsetof(monitor_info_t, mi_ad_monitorOpCompleted) },
1717                 { "( 1.3.6.1.4.1.4203.666.1.55.5 "
1718                         "NAME 'monitorOpInitiated' "
1719                         "DESC 'monitor initiated operations' "
1720                         "SUP monitorCounter "
1721                         "NO-USER-MODIFICATION "
1722                         "USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1723                         offsetof(monitor_info_t, mi_ad_monitorOpInitiated) },
1724                 { "( 1.3.6.1.4.1.4203.666.1.55.6 "
1725                         "NAME 'monitorConnectionNumber' "
1726                         "DESC 'monitor connection number' "
1727                         "SUP monitorCounter "
1728                         "NO-USER-MODIFICATION "
1729                         "USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1730                         offsetof(monitor_info_t, mi_ad_monitorConnectionNumber) },
1731                 { "( 1.3.6.1.4.1.4203.666.1.55.7 "
1732                         "NAME 'monitorConnectionAuthzDN' "
1733                         "DESC 'monitor connection authorization DN' "
1734                         /* "SUP distinguishedName " */
1735                         "EQUALITY distinguishedNameMatch "
1736                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
1737                         "NO-USER-MODIFICATION "
1738                         "USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1739                         offsetof(monitor_info_t, mi_ad_monitorConnectionAuthzDN) },
1740                 { "( 1.3.6.1.4.1.4203.666.1.55.8 "
1741                         "NAME 'monitorConnectionLocalAddress' "
1742                         "DESC 'monitor connection local address' "
1743                         "SUP monitoredInfo "
1744                         "NO-USER-MODIFICATION "
1745                         "USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1746                         offsetof(monitor_info_t, mi_ad_monitorConnectionLocalAddress) },
1747                 { "( 1.3.6.1.4.1.4203.666.1.55.9 "
1748                         "NAME 'monitorConnectionPeerAddress' "
1749                         "DESC 'monitor connection peer address' "
1750                         "SUP monitoredInfo "
1751                         "NO-USER-MODIFICATION "
1752                         "USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1753                         offsetof(monitor_info_t, mi_ad_monitorConnectionPeerAddress) },
1754                 { "( 1.3.6.1.4.1.4203.666.1.55.10 "
1755                         "NAME 'monitorTimestamp' "
1756                         "DESC 'monitor timestamp' "
1757                         "EQUALITY generalizedTimeMatch "
1758                         "ORDERING generalizedTimeOrderingMatch "
1759                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
1760                         "SINGLE-VALUE "
1761                         "NO-USER-MODIFICATION "
1762                         "USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1763                         offsetof(monitor_info_t, mi_ad_monitorTimestamp) },
1764                 { "( 1.3.6.1.4.1.4203.666.1.55.11 "
1765                         "NAME 'monitorOverlay' "
1766                         "DESC 'name of overlays defined for a given database' "
1767                         "SUP monitoredInfo "
1768                         "NO-USER-MODIFICATION "
1769                         "USAGE dSAOperation )", SLAP_AT_HIDE,
1770                         offsetof(monitor_info_t, mi_ad_monitorOverlay) },
1771                 { "( 1.3.6.1.4.1.4203.666.1.55.12 "
1772                         "NAME 'readOnly' "
1773                         "DESC 'read/write status of a given database' "
1774                         "EQUALITY booleanMatch "
1775                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
1776                         "SINGLE-VALUE "
1777                         "USAGE dSAOperation )", SLAP_AT_HIDE,
1778                         offsetof(monitor_info_t, mi_ad_readOnly) },
1779                 { "( 1.3.6.1.4.1.4203.666.1.55.13 "
1780                         "NAME 'restrictedOperation' "
1781                         "DESC 'name of restricted operation for a given database' "
1782                         "SUP managedInfo )", SLAP_AT_HIDE,
1783                         offsetof(monitor_info_t, mi_ad_restrictedOperation ) },
1784                 { "( 1.3.6.1.4.1.4203.666.1.55.14 "
1785                         "NAME 'monitorConnectionProtocol' "
1786                         "DESC 'monitor connection protocol' "
1787                         "SUP monitoredInfo "
1788                         "NO-USER-MODIFICATION "
1789                         "USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1790                         offsetof(monitor_info_t, mi_ad_monitorConnectionProtocol) },
1791                 { "( 1.3.6.1.4.1.4203.666.1.55.15 "
1792                         "NAME 'monitorConnectionOpsReceived' "
1793                         "DESC 'monitor number of operations received by the connection' "
1794                         "SUP monitorCounter "
1795                         "NO-USER-MODIFICATION "
1796                         "USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1797                         offsetof(monitor_info_t, mi_ad_monitorConnectionOpsReceived) },
1798                 { "( 1.3.6.1.4.1.4203.666.1.55.16 "
1799                         "NAME 'monitorConnectionOpsExecuting' "
1800                         "DESC 'monitor number of operations in execution within the connection' "
1801                         "SUP monitorCounter "
1802                         "NO-USER-MODIFICATION "
1803                         "USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1804                         offsetof(monitor_info_t, mi_ad_monitorConnectionOpsExecuting) },
1805                 { "( 1.3.6.1.4.1.4203.666.1.55.17 "
1806                         "NAME 'monitorConnectionOpsPending' "
1807                         "DESC 'monitor number of pending operations within the connection' "
1808                         "SUP monitorCounter "
1809                         "NO-USER-MODIFICATION "
1810                         "USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1811                         offsetof(monitor_info_t, mi_ad_monitorConnectionOpsPending) },
1812                 { "( 1.3.6.1.4.1.4203.666.1.55.18 "
1813                         "NAME 'monitorConnectionOpsCompleted' "
1814                         "DESC 'monitor number of operations completed within the connection' "
1815                         "SUP monitorCounter "
1816                         "NO-USER-MODIFICATION "
1817                         "USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1818                         offsetof(monitor_info_t, mi_ad_monitorConnectionOpsCompleted) },
1819                 { "( 1.3.6.1.4.1.4203.666.1.55.19 "
1820                         "NAME 'monitorConnectionGet' "
1821                         "DESC 'number of times connection_get() was called so far' "
1822                         "SUP monitorCounter "
1823                         "NO-USER-MODIFICATION "
1824                         "USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1825                         offsetof(monitor_info_t, mi_ad_monitorConnectionGet) },
1826                 { "( 1.3.6.1.4.1.4203.666.1.55.20 "
1827                         "NAME 'monitorConnectionRead' "
1828                         "DESC 'number of times connection_read() was called so far' "
1829                         "SUP monitorCounter "
1830                         "NO-USER-MODIFICATION "
1831                         "USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1832                         offsetof(monitor_info_t, mi_ad_monitorConnectionRead) },
1833                 { "( 1.3.6.1.4.1.4203.666.1.55.21 "
1834                         "NAME 'monitorConnectionWrite' "
1835                         "DESC 'number of times connection_write() was called so far' "
1836                         "SUP monitorCounter "
1837                         "NO-USER-MODIFICATION "
1838                         "USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1839                         offsetof(monitor_info_t, mi_ad_monitorConnectionWrite) },
1840                 { "( 1.3.6.1.4.1.4203.666.1.55.22 "
1841                         "NAME 'monitorConnectionMask' "
1842                         "DESC 'monitor connection mask' "
1843                         "SUP monitoredInfo "
1844                         "NO-USER-MODIFICATION "
1845                         "USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1846                         offsetof(monitor_info_t, mi_ad_monitorConnectionMask) },
1847                 { "( 1.3.6.1.4.1.4203.666.1.55.23 "
1848                         "NAME 'monitorConnectionListener' "
1849                         "DESC 'monitor connection listener' "
1850                         "SUP monitoredInfo "
1851                         "NO-USER-MODIFICATION "
1852                         "USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1853                         offsetof(monitor_info_t, mi_ad_monitorConnectionListener) },
1854                 { "( 1.3.6.1.4.1.4203.666.1.55.24 "
1855                         "NAME 'monitorConnectionPeerDomain' "
1856                         "DESC 'monitor connection peer domain' "
1857                         "SUP monitoredInfo "
1858                         "NO-USER-MODIFICATION "
1859                         "USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1860                         offsetof(monitor_info_t, mi_ad_monitorConnectionPeerDomain) },
1861                 { "( 1.3.6.1.4.1.4203.666.1.55.25 "
1862                         "NAME 'monitorConnectionStartTime' "
1863                         "DESC 'monitor connection start time' "
1864                         "SUP monitorTimestamp "
1865                         "SINGLE-VALUE "
1866                         "NO-USER-MODIFICATION "
1867                         "USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1868                         offsetof(monitor_info_t, mi_ad_monitorConnectionStartTime) },
1869                 { "( 1.3.6.1.4.1.4203.666.1.55.26 "
1870                         "NAME 'monitorConnectionActivityTime' "
1871                         "DESC 'monitor connection activity time' "
1872                         "SUP monitorTimestamp "
1873                         "SINGLE-VALUE "
1874                         "NO-USER-MODIFICATION "
1875                         "USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1876                         offsetof(monitor_info_t, mi_ad_monitorConnectionActivityTime) },
1877                 { "( 1.3.6.1.4.1.4203.666.1.55.27 "
1878                         "NAME 'monitorIsShadow' "
1879                         "DESC 'TRUE if the database is shadow' "
1880                         "EQUALITY booleanMatch "
1881                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
1882                         "SINGLE-VALUE "
1883                         "USAGE dSAOperation )", SLAP_AT_HIDE,
1884                         offsetof(monitor_info_t, mi_ad_monitorIsShadow) },
1885                 { "( 1.3.6.1.4.1.4203.666.1.55.28 "
1886                         "NAME 'monitorUpdateRef' "
1887                         "DESC 'update referral for shadow databases' "
1888                         "SUP monitoredInfo "
1889                         "SINGLE-VALUE "
1890                         "USAGE dSAOperation )", SLAP_AT_HIDE,
1891                         offsetof(monitor_info_t, mi_ad_monitorUpdateRef) },
1892                 { "( 1.3.6.1.4.1.4203.666.1.55.29 "
1893                         "NAME 'monitorRuntimeConfig' "
1894                         "DESC 'TRUE if component allows runtime configuration' "
1895                         "EQUALITY booleanMatch "
1896                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
1897                         "SINGLE-VALUE "
1898                         "USAGE dSAOperation )", SLAP_AT_HIDE,
1899                         offsetof(monitor_info_t, mi_ad_monitorRuntimeConfig) },
1900                 { NULL, 0, -1 }
1901         };
1902
1903         static struct {
1904                 char                    *name;
1905                 char                    *oid;
1906         }               s_oid[] = {
1907                 { "olmAttributes",                      "1.3.6.1.4.1.4203.666.1.55" },
1908                 { "olmSubSystemAttributes",             "olmAttributes:0" },
1909                 { "olmGenericAttributes",               "olmSubSystemAttributes:0" },
1910                 { "olmDatabaseAttributes",              "olmSubSystemAttributes:1" },
1911
1912                 /* for example, back-bdb specific attrs
1913                  * are in "olmDatabaseAttributes:1"
1914                  *
1915                  * NOTE: developers, please record here OID assignments
1916                  * for other modules */
1917
1918                 { "olmObjectClasses",                   "1.3.6.1.4.1.4203.666.3.16" },
1919                 { "olmSubSystemObjectClasses",          "olmObjectClasses:0" },
1920                 { "olmGenericObjectClasses",            "olmSubSystemObjectClasses:0" },
1921                 { "olmDatabaseObjectClasses",           "olmSubSystemObjectClasses:1" },
1922
1923                 /* for example, back-bdb specific objectClasses
1924                  * are in "olmDatabaseObjectClasses:1"
1925                  *
1926                  * NOTE: developers, please record here OID assignments
1927                  * for other modules */
1928
1929                 { NULL }
1930         };
1931
1932         int                     i, rc;
1933         monitor_info_t          *mi = &monitor_info;
1934         ConfigArgs c;
1935         char    *argv[ 3 ];
1936
1937         argv[ 0 ] = "monitor";
1938         c.argv = argv;
1939         c.argc = 3;
1940         c.fname = argv[0];
1941
1942         for ( i = 0; s_oid[ i ].name; i++ ) {
1943                 argv[ 1 ] = s_oid[ i ].name;
1944                 argv[ 2 ] = s_oid[ i ].oid;
1945
1946                 if ( parse_oidm( &c, 0, NULL ) != 0 ) {
1947                         Debug( LDAP_DEBUG_ANY,
1948                                 "monitor_back_initialize: unable to add "
1949                                 "objectIdentifier \"%s=%s\"\n",
1950                                 s_oid[ i ].name, s_oid[ i ].oid, 0 );
1951                         return 1;
1952                 }
1953         }
1954
1955         /* schema integration */
1956         for ( i = 0; mat[ i ].schema; i++ ) {
1957                 int                     code;
1958                 AttributeDescription **ad =
1959                         ((AttributeDescription **)&(((char *)mi)[ mat[ i ].offset ]));
1960
1961                 *ad = NULL;
1962                 code = register_at( mat[ i ].schema, ad, 0 );
1963
1964                 if ( code ) {
1965                         Debug( LDAP_DEBUG_ANY,
1966                                 "monitor_back_db_init: register_at failed\n", 0, 0, 0 );
1967                         return -1;
1968                 }
1969                 (*ad)->ad_type->sat_flags |= mat[ i ].flags;
1970         }
1971
1972         for ( i = 0; moc[ i ].schema; i++ ) {
1973                 int                     code;
1974                 ObjectClass             **Oc =
1975                         ((ObjectClass **)&(((char *)mi)[ moc[ i ].offset ]));
1976
1977                 code = register_oc( moc[ i ].schema, Oc, 0 );
1978                 if ( code ) {
1979                         Debug( LDAP_DEBUG_ANY,
1980                                 "monitor_back_db_init: register_oc failed\n", 0, 0, 0 );
1981                         return -1;
1982                 }
1983                 (*Oc)->soc_flags |= moc[ i ].flags;
1984         }
1985
1986         bi->bi_controls = controls;
1987
1988         bi->bi_init = 0;
1989         bi->bi_open = 0;
1990         bi->bi_config = monitor_back_config;
1991         bi->bi_close = 0;
1992         bi->bi_destroy = 0;
1993
1994         bi->bi_db_init = monitor_back_db_init;
1995 #if 0
1996         bi->bi_db_config = monitor_back_db_config;
1997 #endif
1998         bi->bi_db_open = monitor_back_db_open;
1999         bi->bi_db_close = 0;
2000         bi->bi_db_destroy = monitor_back_db_destroy;
2001
2002         bi->bi_op_bind = monitor_back_bind;
2003         bi->bi_op_unbind = 0;
2004         bi->bi_op_search = monitor_back_search;
2005         bi->bi_op_compare = monitor_back_compare;
2006         bi->bi_op_modify = monitor_back_modify;
2007         bi->bi_op_modrdn = 0;
2008         bi->bi_op_add = 0;
2009         bi->bi_op_delete = 0;
2010         bi->bi_op_abandon = 0;
2011
2012         bi->bi_extended = 0;
2013
2014         bi->bi_entry_release_rw = 0;
2015         bi->bi_chk_referrals = 0;
2016         bi->bi_operational = monitor_back_operational;
2017
2018         /*
2019          * hooks for slap tools
2020          */
2021         bi->bi_tool_entry_open = 0;
2022         bi->bi_tool_entry_close = 0;
2023         bi->bi_tool_entry_first = 0;
2024         bi->bi_tool_entry_next = 0;
2025         bi->bi_tool_entry_get = 0;
2026         bi->bi_tool_entry_put = 0;
2027         bi->bi_tool_entry_reindex = 0;
2028         bi->bi_tool_sync = 0;
2029         bi->bi_tool_dn2id_get = 0;
2030         bi->bi_tool_id2entry_get = 0;
2031         bi->bi_tool_entry_modify = 0;
2032
2033         bi->bi_connection_init = 0;
2034         bi->bi_connection_destroy = 0;
2035
2036         bi->bi_extra = (void *)&monitor_extra;
2037
2038         /*
2039          * configuration objectClasses (fake)
2040          */
2041         bi->bi_cf_ocs = monitorocs;
2042
2043         rc = config_register_schema( monitorcfg, monitorocs );
2044         if ( rc ) {
2045                 return rc;
2046         }
2047
2048         return 0;
2049 }
2050
2051 int
2052 monitor_back_db_init(
2053         BackendDB       *be )
2054 {
2055         int                     rc;
2056         struct berval           dn = BER_BVC( SLAPD_MONITOR_DN ),
2057                                 pdn,
2058                                 ndn;
2059         BackendDB               *be2;
2060
2061         monitor_subsys_t        *ms;
2062
2063         /*
2064          * database monitor can be defined once only
2065          */
2066         if ( be_monitor != NULL ) {
2067                 Debug( LDAP_DEBUG_ANY,
2068                         "only one monitor database is allowed\n", 0, 0, 0 );
2069                 return( -1 );
2070         }
2071         be_monitor = be;
2072
2073         /*
2074          * register subsys
2075          */
2076         for ( ms = known_monitor_subsys; ms->mss_name != NULL; ms++ ) {
2077                 if ( monitor_back_register_subsys( ms ) ) {
2078                         return -1;
2079                 }
2080         }
2081
2082         /* indicate system schema supported */
2083         SLAP_BFLAGS(be) |= SLAP_BFLAG_MONITOR;
2084
2085         rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn, NULL );
2086         if( rc != LDAP_SUCCESS ) {
2087                 Debug( LDAP_DEBUG_ANY,
2088                         "unable to normalize/pretty monitor DN \"%s\" (%d)\n",
2089                         dn.bv_val, rc, 0 );
2090                 return -1;
2091         }
2092
2093         ber_bvarray_add( &be->be_suffix, &pdn );
2094         ber_bvarray_add( &be->be_nsuffix, &ndn );
2095
2096         /* NOTE: only one monitor database is allowed,
2097          * so we use static storage */
2098         ldap_pvt_thread_mutex_init( &monitor_info.mi_cache_mutex );
2099
2100         be->be_private = &monitor_info;
2101
2102         be2 = select_backend( &ndn, 0, 0 );
2103         if ( be2 != be ) {
2104                 char    *type = be2->bd_info->bi_type;
2105
2106                 if ( overlay_is_over( be2 ) ) {
2107                         slap_overinfo   *oi = (slap_overinfo *)be2->bd_info->bi_private;
2108                         type = oi->oi_orig->bi_type;
2109                 }
2110
2111                 Debug( LDAP_DEBUG_ANY,
2112                         "\"monitor\" database serving namingContext \"%s\" "
2113                         "is hidden by \"%s\" database serving namingContext \"%s\".\n",
2114                         pdn.bv_val, type, be2->be_nsuffix[ 0 ].bv_val );
2115                 return -1;
2116         }
2117
2118         return 0;
2119 }
2120
2121 int
2122 monitor_back_db_open(
2123         BackendDB       *be )
2124 {
2125         monitor_info_t          *mi = (monitor_info_t *)be->be_private;
2126         struct monitor_subsys_t **ms;
2127         Entry                   *e, **ep, *root;
2128         monitor_entry_t         *mp;
2129         int                     i;
2130         struct berval           bv, rdn = BER_BVC(SLAPD_MONITOR_DN);
2131         struct tm               *tms;
2132 #ifdef HAVE_GMTIME_R
2133         struct tm               tm_buf;
2134 #endif
2135         static char             tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
2136         struct berval   desc[] = {
2137                 BER_BVC("This subtree contains monitoring/managing objects."),
2138                 BER_BVC("This object contains information about this server."),
2139                 BER_BVC("Most of the information is held in operational"
2140                 " attributes, which must be explicitly requested."),
2141                 BER_BVNULL };
2142
2143         assert( be_monitor != NULL );
2144         if ( be != be_monitor ) {
2145                 be_monitor = be;
2146         }
2147
2148         /*
2149          * Start
2150          */
2151 #ifndef HAVE_GMTIME_R
2152         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
2153 #endif
2154 #ifdef HACK_LOCAL_TIME
2155 # ifdef HAVE_LOCALTIME_R
2156         tms = localtime_r( &starttime, &tm_buf );
2157 # else
2158         tms = localtime( &starttime );
2159 # endif /* HAVE_LOCALTIME_R */
2160         lutil_localtime( tmbuf, sizeof(tmbuf), tms, -timezone );
2161 #else /* !HACK_LOCAL_TIME */
2162 # ifdef HAVE_GMTIME_R
2163         tms = gmtime_r( &starttime, &tm_buf );
2164 # else
2165         tms = gmtime( &starttime );
2166 # endif /* HAVE_GMTIME_R */
2167         lutil_gentime( tmbuf, sizeof(tmbuf), tms );
2168 #endif /* !HACK_LOCAL_TIME */
2169 #ifndef HAVE_GMTIME_R
2170         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
2171 #endif
2172
2173         mi->mi_startTime.bv_val = tmbuf;
2174         mi->mi_startTime.bv_len = strlen( tmbuf );
2175
2176         if ( BER_BVISEMPTY( &be->be_rootdn ) ) {
2177                 BER_BVSTR( &mi->mi_creatorsName, SLAPD_ANONYMOUS );
2178                 BER_BVSTR( &mi->mi_ncreatorsName, SLAPD_ANONYMOUS );
2179         } else {
2180                 mi->mi_creatorsName = be->be_rootdn;
2181                 mi->mi_ncreatorsName = be->be_rootndn;
2182         }
2183
2184         /*
2185          * creates the "cn=Monitor" entry 
2186          */
2187         e = monitor_entry_stub( NULL, NULL, &rdn, mi->mi_oc_monitorServer, mi,
2188                 NULL, NULL );
2189
2190         if ( e == NULL) {
2191                 Debug( LDAP_DEBUG_ANY,
2192                         "unable to create \"%s\" entry\n",
2193                         SLAPD_MONITOR_DN, 0, 0 );
2194                 return( -1 );
2195         }
2196
2197         attr_merge_normalize( e, slap_schema.si_ad_description, desc, NULL );
2198
2199         bv.bv_val = strchr( (char *) Versionstr, '$' );
2200         if ( bv.bv_val != NULL ) {
2201                 char    *end;
2202
2203                 bv.bv_val++;
2204                 for ( ; bv.bv_val[ 0 ] == ' '; bv.bv_val++ )
2205                         ;
2206
2207                 end = strchr( bv.bv_val, '$' );
2208                 if ( end != NULL ) {
2209                         end--;
2210
2211                         for ( ; end > bv.bv_val && end[ 0 ] == ' '; end-- )
2212                                 ;
2213
2214                         end++;
2215
2216                         bv.bv_len = end - bv.bv_val;
2217
2218                 } else {
2219                         bv.bv_len = strlen( bv.bv_val );
2220                 }
2221
2222                 if ( attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo,
2223                                         &bv, NULL ) ) {
2224                         Debug( LDAP_DEBUG_ANY,
2225                                 "unable to add monitoredInfo to \"%s\" entry\n",
2226                                 SLAPD_MONITOR_DN, 0, 0 );
2227                         return( -1 );
2228                 }
2229         }
2230
2231         mp = monitor_entrypriv_create();
2232         if ( mp == NULL ) {
2233                 return -1;
2234         }
2235         e->e_private = ( void * )mp;
2236         ep = &mp->mp_children;
2237
2238         if ( monitor_cache_add( mi, e ) ) {
2239                 Debug( LDAP_DEBUG_ANY,
2240                         "unable to add entry \"%s\" to cache\n",
2241                         SLAPD_MONITOR_DN, 0, 0 );
2242                 return -1;
2243         }
2244         root = e;
2245
2246         /*      
2247          * Create all the subsystem specific entries
2248          */
2249         for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
2250                 int             len = strlen( monitor_subsys[ i ]->mss_name );
2251                 struct berval   dn;
2252                 int             rc;
2253
2254                 dn.bv_len = len + sizeof( "cn=" ) - 1;
2255                 dn.bv_val = ch_calloc( sizeof( char ), dn.bv_len + 1 );
2256                 strcpy( dn.bv_val, "cn=" );
2257                 strcat( dn.bv_val, monitor_subsys[ i ]->mss_name );
2258                 rc = dnPretty( NULL, &dn, &monitor_subsys[ i ]->mss_rdn, NULL );
2259                 free( dn.bv_val );
2260                 if ( rc != LDAP_SUCCESS ) {
2261                         Debug( LDAP_DEBUG_ANY,
2262                                 "monitor RDN \"%s\" is invalid\n", 
2263                                 dn.bv_val, 0, 0 );
2264                         return( -1 );
2265                 }
2266
2267                 e = monitor_entry_stub( &root->e_name, &root->e_nname,
2268                         &monitor_subsys[ i ]->mss_rdn, mi->mi_oc_monitorContainer, mi,
2269                         NULL, NULL );
2270
2271                 if ( e == NULL) {
2272                         Debug( LDAP_DEBUG_ANY,
2273                                 "unable to create \"%s\" entry\n", 
2274                                 monitor_subsys[ i ]->mss_dn.bv_val, 0, 0 );
2275                         return( -1 );
2276                 }
2277                 monitor_subsys[i]->mss_dn = e->e_name;
2278                 monitor_subsys[i]->mss_ndn = e->e_nname;
2279
2280                 if ( !BER_BVISNULL( &monitor_subsys[ i ]->mss_desc[ 0 ] ) ) {
2281                         attr_merge_normalize( e, slap_schema.si_ad_description,
2282                                         monitor_subsys[ i ]->mss_desc, NULL );
2283                 }
2284
2285                 mp = monitor_entrypriv_create();
2286                 if ( mp == NULL ) {
2287                         return -1;
2288                 }
2289                 e->e_private = ( void * )mp;
2290                 mp->mp_info = monitor_subsys[ i ];
2291                 mp->mp_flags = monitor_subsys[ i ]->mss_flags;
2292
2293                 if ( monitor_cache_add( mi, e ) ) {
2294                         Debug( LDAP_DEBUG_ANY,
2295                                 "unable to add entry \"%s\" to cache\n",
2296                                 monitor_subsys[ i ]->mss_dn.bv_val, 0, 0 );
2297                         return -1;
2298                 }
2299
2300                 *ep = e;
2301                 ep = &mp->mp_next;
2302         }
2303
2304         assert( be != NULL );
2305
2306         be->be_private = mi;
2307         
2308         /*
2309          * opens the monitor backend subsystems
2310          */
2311         for ( ms = monitor_subsys; ms[ 0 ] != NULL; ms++ ) {
2312                 if ( ms[ 0 ]->mss_open && ( *ms[ 0 ]->mss_open )( be, ms[ 0 ] ) )
2313                 {
2314                         return( -1 );
2315                 }
2316                 ms[ 0 ]->mss_flags |= MONITOR_F_OPENED;
2317         }
2318
2319         monitor_subsys_opened = 1;
2320
2321         if ( mi->mi_entry_limbo ) {
2322                 entry_limbo_t   *el = (entry_limbo_t *)mi->mi_entry_limbo;
2323
2324                 for ( ; el; ) {
2325                         entry_limbo_t   *tmp;
2326                         int             rc;
2327
2328                         switch ( el->el_type ) {
2329                         case LIMBO_ENTRY:
2330                                 rc = monitor_back_register_entry(
2331                                                 el->el_e,
2332                                                 el->el_cb,
2333                                                 el->el_mss,
2334                                                 el->el_flags );
2335                                 break;
2336
2337                         case LIMBO_ENTRY_PARENT:
2338                                 rc = monitor_back_register_entry_parent(
2339                                                 el->el_e,
2340                                                 el->el_cb,
2341                                                 el->el_mss,
2342                                                 el->el_flags,
2343                                                 &el->el_nbase,
2344                                                 el->el_scope,
2345                                                 &el->el_filter );
2346                                 break;
2347                                 
2348
2349                         case LIMBO_ATTRS:
2350                                 rc = monitor_back_register_entry_attrs(
2351                                                 &el->el_ndn,
2352                                                 el->el_a,
2353                                                 el->el_cb,
2354                                                 &el->el_nbase,
2355                                                 el->el_scope,
2356                                                 &el->el_filter );
2357                                 break;
2358
2359                         case LIMBO_CB:
2360                                 rc = monitor_back_register_entry_callback(
2361                                                 &el->el_ndn,
2362                                                 el->el_cb,
2363                                                 &el->el_nbase,
2364                                                 el->el_scope,
2365                                                 &el->el_filter );
2366                                 break;
2367
2368                         case LIMBO_BACKEND:
2369                                 rc = monitor_back_register_backend( el->el_bi );
2370                                 break;
2371
2372                         case LIMBO_DATABASE:
2373                                 rc = monitor_back_register_database( el->el_be );
2374                                 break;
2375
2376                         case LIMBO_OVERLAY_INFO:
2377                                 rc = monitor_back_register_overlay_info( el->el_on );
2378                                 break;
2379
2380                         case LIMBO_OVERLAY:
2381                                 rc = monitor_back_register_overlay( el->el_be );
2382                                 break;
2383
2384                         default:
2385                                 assert( 0 );
2386                         }
2387
2388                         if ( el->el_e ) {
2389                                 entry_free( el->el_e );
2390                         }
2391                         if ( el->el_a ) {
2392                                 attrs_free( el->el_a );
2393                         }
2394                         if ( !BER_BVISNULL( &el->el_ndn ) ) {
2395                                 ber_memfree( el->el_ndn.bv_val );
2396                         }
2397                         if ( !BER_BVISNULL( &el->el_nbase ) ) {
2398                                 ber_memfree( el->el_nbase.bv_val );
2399                         }
2400                         if ( !BER_BVISNULL( &el->el_filter ) ) {
2401                                 ber_memfree( el->el_filter.bv_val );
2402                         }
2403                         if ( el->el_cb && rc != 0 ) {
2404                                 if ( el->el_cb->mc_dispose ) {
2405                                         el->el_cb->mc_dispose( &el->el_cb->mc_private );
2406                                 }
2407                                 ch_free( el->el_cb );
2408                         }
2409
2410                         tmp = el;
2411                         el = el->el_next;
2412                         ch_free( tmp );
2413                 }
2414
2415                 mi->mi_entry_limbo = NULL;
2416         }
2417
2418         return( 0 );
2419 }
2420
2421 int
2422 monitor_back_config(
2423         BackendInfo     *bi,
2424         const char      *fname,
2425         int             lineno,
2426         int             argc,
2427         char            **argv )
2428 {
2429         /*
2430          * eventually, will hold backend specific configuration parameters
2431          */
2432         return SLAP_CONF_UNKNOWN;
2433 }
2434
2435 #if 0
2436 int
2437 monitor_back_db_config(
2438         Backend     *be,
2439         const char  *fname,
2440         int         lineno,
2441         int         argc,
2442         char        **argv )
2443 {
2444         monitor_info_t  *mi = ( monitor_info_t * )be->be_private;
2445
2446         /*
2447          * eventually, will hold database specific configuration parameters
2448          */
2449         return SLAP_CONF_UNKNOWN;
2450 }
2451 #endif
2452
2453 int
2454 monitor_back_db_destroy(
2455         BackendDB       *be )
2456 {
2457         monitor_info_t  *mi = ( monitor_info_t * )be->be_private;
2458
2459         if ( mi == NULL ) {
2460                 return -1;
2461         }
2462
2463         /*
2464          * FIXME: destroys all the data
2465          */
2466         /* NOTE: mi points to static storage; don't free it */
2467         
2468         (void)monitor_cache_destroy( mi );
2469
2470         if ( monitor_subsys ) {
2471                 int     i;
2472
2473                 for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
2474                         if ( monitor_subsys[ i ]->mss_destroy ) {
2475                                 monitor_subsys[ i ]->mss_destroy( be, monitor_subsys[ i ] );
2476                         }
2477
2478                         if ( !BER_BVISNULL( &monitor_subsys[ i ]->mss_rdn ) ) {
2479                                 ch_free( monitor_subsys[ i ]->mss_rdn.bv_val );
2480                         }
2481                 }
2482
2483                 ch_free( monitor_subsys );
2484         }
2485         
2486         ldap_pvt_thread_mutex_destroy( &monitor_info.mi_cache_mutex );
2487
2488         be->be_private = NULL;
2489
2490         return 0;
2491 }
2492
2493 #if SLAPD_MONITOR == SLAPD_MOD_DYNAMIC
2494
2495 /* conditionally define the init_module() function */
2496 SLAP_BACKEND_INIT_MODULE( monitor )
2497
2498 #endif /* SLAPD_MONITOR == SLAPD_MOD_DYNAMIC */
2499