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