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