]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/init.c
Don't split description across multiple values.
[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-2005 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 "lber_pvt.h"
30 #include "back-monitor.h"
31
32 #undef INTEGRATE_CORE_SCHEMA
33
34 /*
35  * used by many functions to add description to entries
36  *
37  * WARNING: be_monitor may change as new databases are added,
38  * so it should not be used outside monitor_back_db_init()
39  * until monitor_back_db_open is called.
40  */
41 BackendDB                       *be_monitor;
42
43 static struct monitor_subsys_t  **monitor_subsys;
44 static int                      monitor_subsys_opened;
45 static monitor_info_t           monitor_info;
46
47 /*
48  * subsystem data
49  *
50  * the known subsystems are added to the subsystems
51  * array at backend initialization; other subsystems
52  * may be added by calling monitor_back_register_subsys()
53  * before the database is opened (e.g. by other backends
54  * or by overlays or modules).
55  */
56 static struct monitor_subsys_t known_monitor_subsys[] = {
57         { 
58                 SLAPD_MONITOR_BACKEND_NAME, 
59                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
60                 { BER_BVC( "This subsystem contains information about available backends." ),
61                         BER_BVNULL },
62                 MONITOR_F_PERSISTENT_CH,
63                 monitor_subsys_backend_init,
64                 NULL,   /* update */
65                 NULL,   /* create */
66                 NULL    /* modify */
67         }, { 
68                 SLAPD_MONITOR_CONN_NAME,
69                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
70                 { BER_BVC( "This subsystem contains information about connections." ),
71                         BER_BVNULL },
72                 MONITOR_F_VOLATILE_CH,
73                 monitor_subsys_conn_init,
74                 monitor_subsys_conn_update,
75                 monitor_subsys_conn_create,
76                 NULL    /* modify */
77         }, { 
78                 SLAPD_MONITOR_DATABASE_NAME,    
79                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
80                 { BER_BVC( "This subsystem contains information about configured databases." ),
81                         BER_BVNULL },
82                 MONITOR_F_PERSISTENT_CH,
83                 monitor_subsys_database_init,
84                 NULL,   /* update */
85                 NULL,   /* create */
86                 monitor_subsys_database_modify
87         }, { 
88                 SLAPD_MONITOR_LISTENER_NAME,    
89                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
90                 { BER_BVC( "This subsystem contains information about active listeners." ),
91                         BER_BVNULL },
92                 MONITOR_F_PERSISTENT_CH,
93                 monitor_subsys_listener_init,
94                 NULL,   /* update */
95                 NULL,   /* create */
96                 NULL    /* modify */
97         }, { 
98                 SLAPD_MONITOR_LOG_NAME,
99                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
100                 { BER_BVC( "This subsystem contains information about logging." ),
101                         BER_BVC( "Set the attribute \"managedInfo\" to the desired log levels." ),
102                         BER_BVNULL },
103                 MONITOR_F_NONE,
104                 monitor_subsys_log_init,
105                 NULL,   /* update */
106                 NULL,   /* create */
107                 monitor_subsys_log_modify
108         }, { 
109                 SLAPD_MONITOR_OPS_NAME,
110                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
111                 { BER_BVC( "This subsystem contains information about performed operations." ),
112                         BER_BVNULL },
113                 MONITOR_F_PERSISTENT_CH,
114                 monitor_subsys_ops_init,
115                 monitor_subsys_ops_update,
116                 NULL,   /* create */
117                 NULL,   /* modify */
118         }, { 
119                 SLAPD_MONITOR_OVERLAY_NAME,
120                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
121                 { BER_BVC( "This subsystem contains information about available overlays." ),
122                         BER_BVNULL },
123                 MONITOR_F_PERSISTENT_CH,
124                 monitor_subsys_overlay_init,
125                 NULL,   /* update */
126                 NULL,   /* create */
127                 NULL,   /* modify */
128         }, { 
129                 SLAPD_MONITOR_SASL_NAME,        
130                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
131                 { BER_BVC( "This subsystem contains information about SASL." ),
132                         BER_BVNULL },
133                 MONITOR_F_NONE,
134                 NULL,   /* init */
135                 NULL,   /* update */
136                 NULL,   /* create */
137                 NULL    /* modify */
138         }, { 
139                 SLAPD_MONITOR_SENT_NAME,
140                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
141                 { BER_BVC( "This subsystem contains statistics." ),
142                         BER_BVNULL },
143                 MONITOR_F_PERSISTENT_CH,
144                 monitor_subsys_sent_init,
145                 monitor_subsys_sent_update,
146                 NULL,   /* create */
147                 NULL,   /* modify */
148         }, { 
149                 SLAPD_MONITOR_THREAD_NAME,      
150                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
151                 { BER_BVC( "This subsystem contains information about threads." ),
152                         BER_BVNULL },
153                 MONITOR_F_PERSISTENT_CH,
154                 monitor_subsys_thread_init,
155                 monitor_subsys_thread_update,
156                 NULL,   /* create */
157                 NULL    /* modify */
158         }, { 
159                 SLAPD_MONITOR_TIME_NAME,
160                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
161                 { BER_BVC( "This subsystem contains information about time." ),
162                         BER_BVNULL },
163                 MONITOR_F_PERSISTENT_CH,
164                 monitor_subsys_time_init,
165                 monitor_subsys_time_update,
166                 NULL,   /* create */
167                 NULL,   /* modify */
168         }, { 
169                 SLAPD_MONITOR_TLS_NAME,
170                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
171                 { BER_BVC( "This subsystem contains information about TLS." ),
172                         BER_BVNULL },
173                 MONITOR_F_NONE,
174                 NULL,   /* init */
175                 NULL,   /* update */
176                 NULL,   /* create */
177                 NULL    /* modify */
178         }, { 
179                 SLAPD_MONITOR_RWW_NAME,
180                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
181                 { BER_BVC( "This subsystem contains information about read/write waiters." ),
182                         BER_BVNULL },
183                 MONITOR_F_PERSISTENT_CH,
184                 monitor_subsys_rww_init,
185                 monitor_subsys_rww_update,
186                 NULL,   /* create */
187                 NULL    /* modify */
188         }, { NULL }
189 };
190
191 int
192 monitor_back_register_subsys(
193         monitor_subsys_t        *ms )
194 {
195         int     i = 0;
196
197         if ( monitor_subsys ) {
198                 for ( ; monitor_subsys[ i ] != NULL; i++ )
199                         /* just count'em */ ;
200         }
201
202         monitor_subsys = ch_realloc( monitor_subsys,
203                         ( 2 + i ) * sizeof( monitor_subsys_t * ) );
204
205         if ( monitor_subsys == NULL ) {
206                 return -1;
207         }
208
209         monitor_subsys[ i ] = ms;
210         monitor_subsys[ i + 1 ] = NULL;
211
212         /* if a subsystem is registered __AFTER__ subsystem 
213          * initialization (depending on the sequence the databases
214          * are listed in slapd.conf), init it */
215         if ( monitor_subsys_opened ) {
216
217                 /* FIXME: this should only be possible
218                  * if be_monitor is already initialized */
219                 assert( be_monitor );
220
221                 if ( ms->mss_open && ( *ms->mss_open )( be_monitor, ms ) ) {
222                         return -1;
223                 }
224
225                 ms->mss_flags |= MONITOR_F_OPENED;
226         }
227
228         return 0;
229 }
230
231 enum {
232         LIMBO_ENTRY,
233         LIMBO_ENTRY_PARENT,
234         LIMBO_ATTRS,
235         LIMBO_CB
236 };
237
238 typedef struct entry_limbo_t {
239         int                     el_type;
240         Entry                   *el_e;
241         Attribute               *el_a;
242         struct berval           el_ndn;
243         struct berval           el_base;
244         int                     el_scope;
245         struct berval           el_filter;
246         monitor_callback_t      *el_cb;
247         struct entry_limbo_t    *el_next;
248 } entry_limbo_t;
249
250 int
251 monitor_back_register_entry(
252         Entry                   *e,
253         monitor_callback_t      *cb )
254 {
255         monitor_info_t  *mi = ( monitor_info_t * )be_monitor->be_private;
256
257         assert( mi != NULL );
258         assert( e != NULL );
259         assert( e->e_private == NULL );
260         
261         if ( monitor_subsys_opened ) {
262                 Entry           *e_parent = NULL,
263                                 *e_new = NULL,
264                                 **ep = NULL;
265                 struct berval   pdn = BER_BVNULL;
266                 monitor_entry_t *mp = NULL,
267                                 *mp_parent = NULL;
268                 int             rc = 0;
269
270                 if ( monitor_cache_get( mi, &e->e_nname, &e_parent ) == 0 ) {
271                         /* entry exists */
272                         Debug( LDAP_DEBUG_ANY,
273                                 "monitor_back_register_entry(\"%s\"): "
274                                 "entry exists\n",
275                                 e->e_name.bv_val, 0, 0 );
276                         monitor_cache_release( mi, e_parent );
277                         return -1;
278                 }
279
280                 dnParent( &e->e_nname, &pdn );
281                 if ( monitor_cache_get( mi, &pdn, &e_parent ) != 0 ) {
282                         /* parent does not exist */
283                         Debug( LDAP_DEBUG_ANY,
284                                 "monitor_back_register_entry(\"%s\"): "
285                                 "parent \"%s\" not found\n",
286                                 e->e_name.bv_val, pdn.bv_val, 0 );
287                         return -1;
288                 }
289
290                 assert( e_parent->e_private != NULL );
291                 mp_parent = ( monitor_entry_t * )e_parent->e_private;
292
293                 if ( mp_parent->mp_flags & MONITOR_F_VOLATILE ) {
294                         /* entry is volatile; cannot append children */
295                         Debug( LDAP_DEBUG_ANY,
296                                 "monitor_back_register_entry(\"%s\"): "
297                                 "parent \"%s\" is volatile\n",
298                                 e->e_name.bv_val, e_parent->e_name.bv_val, 0 );
299                         rc = -1;
300                         goto done;
301                 }
302
303                 mp = monitor_entrypriv_create();
304                 if ( mp == NULL ) {
305                         Debug( LDAP_DEBUG_ANY,
306                                 "monitor_back_register_entry(\"%s\"): "
307                                 "monitor_entrypriv_create() failed\n",
308                                 e->e_name.bv_val, 0, 0 );
309                         rc = -1;
310                         goto done;
311                 }
312
313                 e_new = entry_dup( e );
314                 if ( e_new == NULL ) {
315                         Debug( LDAP_DEBUG_ANY,
316                                 "monitor_back_register_entry(\"%s\"): "
317                                 "entry_dup() failed\n",
318                                 e->e_name.bv_val, 0, 0 );
319                         rc = -1;
320                         goto done;
321                 }
322                 
323                 e_new->e_private = ( void * )mp;
324                 mp->mp_info = mp_parent->mp_info;
325                 mp->mp_flags = mp_parent->mp_flags | MONITOR_F_SUB;
326                 mp->mp_cb = cb;
327
328                 ep = &mp_parent->mp_children;
329                 for ( ; *ep; ) {
330                         mp_parent = ( monitor_entry_t * )(*ep)->e_private;
331                         ep = &mp_parent->mp_next;
332                 }
333                 *ep = e_new;
334
335                 if ( monitor_cache_add( mi, e_new ) ) {
336                         Debug( LDAP_DEBUG_ANY,
337                                 "monitor_back_register_entry(\"%s\"): "
338                                 "unable to add entry\n",
339                                 e->e_name.bv_val, 0, 0 );
340                         rc = -1;
341                         goto done;
342                 }
343
344 done:;
345                 if ( rc ) {
346                         if ( mp ) {
347                                 ch_free( mp );
348                         }
349                         if ( e_new ) {
350                                 e_new->e_private = NULL;
351                                 entry_free( e_new );
352                         }
353                 }
354
355                 if ( e_parent ) {
356                         monitor_cache_release( mi, e_parent );
357                 }
358
359         } else {
360                 entry_limbo_t   **elpp, el = { 0 };
361
362                 el.el_type = LIMBO_ENTRY;
363
364                 el.el_e = entry_dup( e );
365                 if ( el.el_e == NULL ) {
366                         Debug( LDAP_DEBUG_ANY,
367                                 "monitor_back_register_entry(\"%s\"): "
368                                 "entry_dup() failed\n",
369                                 e->e_name.bv_val, 0, 0 );
370                         return -1;
371                 }
372                 
373                 el.el_cb = cb;
374
375                 for ( elpp = (entry_limbo_t **)&mi->mi_entry_limbo;
376                                 *elpp;
377                                 elpp = &(*elpp)->el_next )
378                         /* go to last */;
379
380                 *elpp = (entry_limbo_t *)ch_malloc( sizeof( entry_limbo_t ) );
381                 if ( *elpp == NULL ) {
382                         el.el_e->e_private = NULL;
383                         entry_free( el.el_e );
384                         return -1;
385                 }
386
387                 el.el_next = NULL;
388                 **elpp = el;
389         }
390
391         return 0;
392 }
393
394 int
395 monitor_back_register_entry_parent(
396         Entry                   *e,
397         monitor_callback_t      *cb,
398         struct berval           *base,
399         int                     scope,
400         struct berval           *filter )
401 {
402         monitor_info_t  *mi = ( monitor_info_t * )be_monitor->be_private;
403         struct berval   ndn = BER_BVNULL;
404
405         assert( mi != NULL );
406         assert( e != NULL );
407         assert( e->e_private == NULL );
408
409         if ( BER_BVISNULL( filter ) ) {
410                 /* need a filter */
411                 Debug( LDAP_DEBUG_ANY,
412                         "monitor_back_register_entry_parent(\"\"): "
413                         "need a valid filter\n",
414                         0, 0, 0 );
415                 return -1;
416         }
417
418         if ( monitor_subsys_opened ) {
419                 Entry           *e_parent = NULL,
420                                 *e_new = NULL,
421                                 **ep = NULL;
422                 struct berval   e_name = BER_BVNULL,
423                                 e_nname = BER_BVNULL;
424                 monitor_entry_t *mp = NULL,
425                                 *mp_parent = NULL;
426                 int             rc = 0;
427
428                 if ( monitor_filter2ndn( base, scope, filter, &ndn ) ) {
429                         /* entry does not exist */
430                         Debug( LDAP_DEBUG_ANY,
431                                 "monitor_back_register_entry_parent(\"\"): "
432                                 "base=%s scope=%d filter=%s : "
433                                 "unable to find entry\n",
434                                 base->bv_val ? base->bv_val : "\"\"",
435                                 scope, filter->bv_val );
436                         return -1;
437                 }
438
439                 if ( monitor_cache_get( mi, &ndn, &e_parent ) != 0 ) {
440                         /* entry does not exist */
441                         Debug( LDAP_DEBUG_ANY,
442                                 "monitor_back_register_entry_parent(\"%s\"): "
443                                 "parent entry does not exist\n",
444                                 ndn.bv_val, 0, 0 );
445                         rc = -1;
446                         goto done;
447                 }
448
449                 assert( e_parent->e_private != NULL );
450                 mp_parent = ( monitor_entry_t * )e_parent->e_private;
451
452                 if ( mp_parent->mp_flags & MONITOR_F_VOLATILE ) {
453                         /* entry is volatile; cannot append callback */
454                         Debug( LDAP_DEBUG_ANY,
455                                 "monitor_back_register_entry_parent(\"%s\"): "
456                                 "entry is volatile\n",
457                                 e_parent->e_name.bv_val, 0, 0 );
458                         rc = -1;
459                         goto done;
460                 }
461
462                 build_new_dn( &e_name, &e_parent->e_name, &e->e_name, NULL );
463                 build_new_dn( &e_nname, &e_parent->e_nname, &e->e_nname, NULL );
464
465                 if ( monitor_cache_get( mi, &e_nname, &e_new ) == 0 ) {
466                         /* entry already exists */
467                         Debug( LDAP_DEBUG_ANY,
468                                 "monitor_back_register_entry_parent(\"%s\"): "
469                                 "entry already exists\n",
470                                 e_name.bv_val, 0, 0 );
471                         monitor_cache_release( mi, e_new );
472                         rc = -1;
473                         goto done;
474                 }
475
476                 mp = monitor_entrypriv_create();
477                 if ( mp == NULL ) {
478                         Debug( LDAP_DEBUG_ANY,
479                                 "monitor_back_register_entry_parent(\"%s\"): "
480                                 "monitor_entrypriv_create() failed\n",
481                                 e->e_name.bv_val, 0, 0 );
482                         rc = -1;
483                         goto done;
484                 }
485
486                 e_new = entry_dup( e );
487                 if ( e_new == NULL ) {
488                         Debug( LDAP_DEBUG_ANY,
489                                 "monitor_back_register_entry(\"%s\"): "
490                                 "entry_dup() failed\n",
491                                 e->e_name.bv_val, 0, 0 );
492                         rc = -1;
493                         goto done;
494                 }
495                 ch_free( e_new->e_name.bv_val );
496                 ch_free( e_new->e_nname.bv_val );
497                 e_new->e_name = e_name;
498                 e_new->e_nname = e_nname;
499                 
500                 e_new->e_private = ( void * )mp;
501                 mp->mp_info = mp_parent->mp_info;
502                 mp->mp_flags = mp_parent->mp_flags | MONITOR_F_SUB;
503                 mp->mp_cb = cb;
504
505                 ep = &mp_parent->mp_children;
506                 for ( ; *ep; ) {
507                         mp_parent = ( monitor_entry_t * )(*ep)->e_private;
508                         ep = &mp_parent->mp_next;
509                 }
510                 *ep = e_new;
511
512                 if ( monitor_cache_add( mi, e_new ) ) {
513                         Debug( LDAP_DEBUG_ANY,
514                                 "monitor_back_register_entry(\"%s\"): "
515                                 "unable to add entry\n",
516                                 e->e_name.bv_val, 0, 0 );
517                         rc = -1;
518                         goto done;
519                 }
520
521 done:;
522                 if ( !BER_BVISNULL( &ndn ) ) {
523                         ch_free( ndn.bv_val );
524                 }
525
526                 if ( rc ) {
527                         if ( mp ) {
528                                 ch_free( mp );
529                         }
530                         if ( e_new ) {
531                                 e_new->e_private = NULL;
532                                 entry_free( e_new );
533                         }
534                 }
535
536                 if ( e_parent ) {
537                         monitor_cache_release( mi, e_parent );
538                 }
539
540         } else {
541                 entry_limbo_t   **elpp, el = { 0 };
542
543                 el.el_type = LIMBO_ENTRY_PARENT;
544
545                 el.el_e = entry_dup( e );
546                 if ( el.el_e == NULL ) {
547                         Debug( LDAP_DEBUG_ANY,
548                                 "monitor_back_register_entry(\"%s\"): "
549                                 "entry_dup() failed\n",
550                                 e->e_name.bv_val, 0, 0 );
551                         return -1;
552                 }
553                 
554                 if ( !BER_BVISNULL( base ) ) {
555                         ber_dupbv( &el.el_base, base );
556                 }
557                 el.el_scope = scope;
558                 if ( !BER_BVISNULL( filter ) ) {
559                         ber_dupbv( &el.el_filter, filter );
560                 }
561
562                 el.el_cb = cb;
563
564                 for ( elpp = (entry_limbo_t **)&mi->mi_entry_limbo;
565                                 *elpp;
566                                 elpp = &(*elpp)->el_next )
567                         /* go to last */;
568
569                 *elpp = (entry_limbo_t *)ch_malloc( sizeof( entry_limbo_t ) );
570                 if ( *elpp == NULL ) {
571                         el.el_e->e_private = NULL;
572                         entry_free( el.el_e );
573                         return -1;
574                 }
575
576                 el.el_next = NULL;
577                 **elpp = el;
578         }
579
580         return 0;
581 }
582
583 static int
584 monitor_filter2ndn_cb( Operation *op, SlapReply *rs )
585 {
586         if ( rs->sr_type == REP_SEARCH ) {
587                 struct berval   *ndn = op->o_callback->sc_private;
588                 
589                 ber_dupbv( ndn, &rs->sr_entry->e_nname );
590         }
591
592         return 0;
593 }
594
595 int
596 monitor_filter2ndn(
597         struct berval   *base,
598         int             scope,
599         struct berval   *filter,
600         struct berval   *ndn )
601 {
602         Connection      conn = { 0 };
603         char            opbuf[OPERATION_BUFFER_SIZE];
604         Operation       *op;
605         SlapReply       rs = { 0 };
606         slap_callback   cb = { NULL, monitor_filter2ndn_cb, NULL, NULL };
607         int             rc;
608
609         BER_BVZERO( ndn );
610
611         if ( be_monitor == NULL ) {
612                 return -1;
613         }
614
615         op = (Operation *)opbuf;
616         connection_fake_init( &conn, op, &conn );
617
618         op->o_tag = LDAP_REQ_SEARCH;
619
620         /* use global malloc for now */
621         op->o_tmpmemctx = NULL;
622         op->o_tmpmfuncs = &ch_mfuncs;
623
624         op->o_bd = be_monitor;
625         if ( base == NULL || BER_BVISNULL( base ) ) {
626                 ber_dupbv_x( &op->o_req_dn, &op->o_bd->be_suffix[ 0 ],
627                                 op->o_tmpmemctx );
628                 ber_dupbv_x( &op->o_req_ndn, &op->o_bd->be_nsuffix[ 0 ],
629                                 op->o_tmpmemctx );
630
631         } else {
632                 if ( dnPrettyNormal( NULL, base, &op->o_req_dn, &op->o_req_ndn,
633                                         op->o_tmpmemctx ) ) {
634                         /* error */
635                 }
636         }
637
638         op->o_callback = &cb;
639         cb.sc_private = (void *)ndn;
640
641         op->ors_scope = scope;
642         ber_dupbv_x( &op->ors_filterstr, filter, op->o_tmpmemctx );
643         op->ors_filter = str2filter_x( op, filter->bv_val );
644         op->ors_attrs = slap_anlist_no_attrs;
645         op->ors_attrsonly = 0;
646         op->ors_tlimit = SLAP_NO_LIMIT;
647         op->ors_slimit = 1;
648         op->ors_limit = NULL;
649         op->ors_deref = LDAP_DEREF_NEVER;
650
651         op->o_nocaching = 1;
652         op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
653
654         op->o_dn = be_monitor->be_rootdn;
655         op->o_ndn = be_monitor->be_rootndn;
656
657         rc = op->o_bd->be_search( op, &rs );
658
659         filter_free_x( op, op->ors_filter );
660         op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
661         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
662         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
663
664         if ( rc != 0 ) {
665                 return rc;
666         }
667
668         switch ( rs.sr_err ) {
669         case LDAP_SUCCESS:
670                 if ( BER_BVISNULL( ndn ) ) {
671                         rc = -1;
672                 }
673                 break;
674                         
675         case LDAP_SIZELIMIT_EXCEEDED:
676         default:
677                 if ( !BER_BVISNULL( ndn ) ) {
678                         ber_memfree( ndn->bv_val );
679                         BER_BVZERO( ndn );
680                 }
681                 rc = -1;
682                 break;
683         }
684
685         return rc;
686 }
687
688 int
689 monitor_back_register_entry_attrs(
690         struct berval           *ndn_in,
691         Attribute               *a,
692         monitor_callback_t      *cb,
693         struct berval           *base,
694         int                     scope,
695         struct berval           *filter )
696 {
697         monitor_info_t  *mi = ( monitor_info_t * )be_monitor->be_private;
698         struct berval   ndn = BER_BVNULL;
699
700         assert( mi != NULL );
701
702         if ( ndn_in != NULL ) {
703                 ndn = *ndn_in;
704         }
705
706         if ( a == NULL && cb == NULL ) {
707                 /* nothing to do */
708                 return -1;
709         }
710
711         if ( ( ndn_in == NULL || BER_BVISNULL( &ndn ) )
712                         && BER_BVISNULL( filter ) )
713         {
714                 /* need a filter */
715                 Debug( LDAP_DEBUG_ANY,
716                         "monitor_back_register_entry_*(\"\"): "
717                         "need a valid filter\n",
718                         0, 0, 0 );
719                 return -1;
720         }
721
722         if ( monitor_subsys_opened ) {
723                 Entry                   *e = NULL;
724                 Attribute               **atp = NULL;
725                 monitor_entry_t         *mp = NULL;
726                 monitor_callback_t      **mcp = NULL;
727                 int                     rc = 0;
728                 int                     freeit = 0;
729
730                 if ( BER_BVISNULL( &ndn ) ) {
731                         if ( monitor_filter2ndn( base, scope, filter, &ndn ) ) {
732                                 /* entry does not exist */
733                                 Debug( LDAP_DEBUG_ANY,
734                                         "monitor_back_register_entry_*(\"\"): "
735                                         "base=%s scope=%d filter=%s : "
736                                         "unable to find entry\n",
737                                         base->bv_val ? base->bv_val : "\"\"",
738                                         scope, filter->bv_val );
739                                 return -1;
740                         }
741
742                         freeit = 1;
743                 }
744
745                 if ( monitor_cache_get( mi, &ndn, &e ) != 0 ) {
746                         /* entry does not exist */
747                         Debug( LDAP_DEBUG_ANY,
748                                 "monitor_back_register_entry_*(\"%s\"): "
749                                 "entry does not exist\n",
750                                 ndn.bv_val, 0, 0 );
751                         rc = -1;
752                         goto done;
753                 }
754
755                 assert( e->e_private != NULL );
756                 mp = ( monitor_entry_t * )e->e_private;
757
758                 if ( mp->mp_flags & MONITOR_F_VOLATILE ) {
759                         /* entry is volatile; cannot append callback */
760                         Debug( LDAP_DEBUG_ANY,
761                                 "monitor_back_register_entry_*(\"%s\"): "
762                                 "entry is volatile\n",
763                                 e->e_name.bv_val, 0, 0 );
764                         rc = -1;
765                         goto done;
766                 }
767
768                 if ( a ) {
769                         for ( atp = &e->e_attrs; *atp; atp = &(*atp)->a_next )
770                                 /* just get to last */ ;
771
772                         *atp = attrs_dup( a );
773                         if ( *atp == NULL ) {
774                                 Debug( LDAP_DEBUG_ANY,
775                                         "monitor_back_register_entry_*(\"%s\"): "
776                                         "attrs_dup() failed\n",
777                                         e->e_name.bv_val, 0, 0 );
778                                 rc = -1;
779                                 goto done;
780                         }
781                 }
782
783                 if ( cb ) {
784                         for ( mcp = &mp->mp_cb; *mcp; mcp = &(*mcp)->mc_next )
785                                 /* go to tail */ ;
786                 
787                         /* NOTE: we do not clear cb->mc_next, so this function
788                          * can be used to append a list of callbacks */
789                         (*mcp) = cb;
790                 }
791
792 done:;
793                 if ( rc ) {
794                         if ( *atp ) {
795                                 attrs_free( *atp );
796                                 *atp = NULL;
797                         }
798                 }
799
800                 if ( freeit ) {
801                         ber_memfree( ndn.bv_val );
802                 }
803
804                 if ( e ) {
805                         monitor_cache_release( mi, e );
806                 }
807
808         } else {
809                 entry_limbo_t   **elpp, el = { 0 };
810
811                 el.el_type = LIMBO_ATTRS;
812                 if ( !BER_BVISNULL( &ndn ) ) {
813                         ber_dupbv( &el.el_ndn, &ndn );
814                 }
815                 if ( !BER_BVISNULL( base ) ) {
816                         ber_dupbv( &el.el_base, base);
817                 }
818                 el.el_scope = scope;
819                 if ( !BER_BVISNULL( filter ) ) {
820                         ber_dupbv( &el.el_filter, filter );
821                 }
822
823                 el.el_a = attrs_dup( a );
824                 el.el_cb = cb;
825
826                 for ( elpp = (entry_limbo_t **)&mi->mi_entry_limbo;
827                                 *elpp;
828                                 elpp = &(*elpp)->el_next )
829                         /* go to last */;
830
831                 *elpp = (entry_limbo_t *)ch_malloc( sizeof( entry_limbo_t ) );
832                 if ( *elpp == NULL ) {
833                         el.el_e->e_private = NULL;
834                         entry_free( el.el_e );
835                         return -1;
836                 }
837
838                 el.el_next = NULL;
839                 **elpp = el;
840         }
841
842         return 0;
843 }
844
845 int
846 monitor_back_register_entry_callback(
847         struct berval           *ndn,
848         monitor_callback_t      *cb,
849         struct berval           *base,
850         int                     scope,
851         struct berval           *filter )
852 {
853         return monitor_back_register_entry_attrs( ndn, NULL, cb,
854                         base, scope, filter );
855 }
856
857 monitor_subsys_t *
858 monitor_back_get_subsys( const char *name )
859 {
860         if ( monitor_subsys != NULL ) {
861                 int     i;
862                 
863                 for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
864                         if ( strcasecmp( monitor_subsys[ i ]->mss_name, name ) == 0 ) {
865                                 return monitor_subsys[ i ];
866                         }
867                 }
868         }
869
870         return NULL;
871 }
872
873 monitor_subsys_t *
874 monitor_back_get_subsys_by_dn(
875         struct berval   *ndn,
876         int             sub )
877 {
878         if ( monitor_subsys != NULL ) {
879                 int     i;
880
881                 if ( sub ) {
882                         for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
883                                 if ( dnIsSuffix( ndn, &monitor_subsys[ i ]->mss_ndn ) ) {
884                                         return monitor_subsys[ i ];
885                                 }
886                         }
887
888                 } else {
889                         for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
890                                 if ( dn_match( ndn, &monitor_subsys[ i ]->mss_ndn ) ) {
891                                         return monitor_subsys[ i ];
892                                 }
893                         }
894                 }
895         }
896
897         return NULL;
898 }
899
900 int
901 monitor_back_initialize(
902         BackendInfo     *bi )
903 {
904         monitor_subsys_t        *ms;
905         static char             *controls[] = {
906                 LDAP_CONTROL_MANAGEDSAIT,
907                 NULL
908         };
909         monitor_info_t          *mi = &monitor_info;
910         int                     i;
911         const char              *text;
912
913         struct m_s {
914                 char    *name;
915                 char    *schema;
916                 slap_mask_t flags;
917                 int     offset;
918         } moc[] = {
919                 { "monitor", "( 1.3.6.1.4.1.4203.666.3.2 "
920                         "NAME 'monitor' "
921                         "DESC 'OpenLDAP system monitoring' "
922                         "SUP top STRUCTURAL "
923                         "MUST cn "
924                         "MAY ( "
925                                 "description "
926                                 "$ seeAlso "
927                                 "$ labeledURI "
928                                 "$ monitoredInfo "
929                                 "$ managedInfo "
930                                 "$ monitorOverlay "
931                         ") )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
932                         offsetof(monitor_info_t, mi_oc_monitor) },
933                 { "monitorServer", "( 1.3.6.1.4.1.4203.666.3.7 "
934                         "NAME 'monitorServer' "
935                         "DESC 'Server monitoring root entry' "
936                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
937                         offsetof(monitor_info_t, mi_oc_monitorServer) },
938                 { "monitorContainer", "( 1.3.6.1.4.1.4203.666.3.8 "
939                         "NAME 'monitorContainer' "
940                         "DESC 'monitor container class' "
941                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
942                         offsetof(monitor_info_t, mi_oc_monitorContainer) },
943                 { "monitorCounterObject", "( 1.3.6.1.4.1.4203.666.3.9 "
944                         "NAME 'monitorCounterObject' "
945                         "DESC 'monitor counter class' "
946                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
947                         offsetof(monitor_info_t, mi_oc_monitorCounterObject) },
948                 { "monitorOperation", "( 1.3.6.1.4.1.4203.666.3.10 "
949                         "NAME 'monitorOperation' "
950                         "DESC 'monitor operation class' "
951                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
952                         offsetof(monitor_info_t, mi_oc_monitorOperation) },
953                 { "monitorConnection", "( 1.3.6.1.4.1.4203.666.3.11 "
954                         "NAME 'monitorConnection' "
955                         "DESC 'monitor connection class' "
956                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
957                         offsetof(monitor_info_t, mi_oc_monitorConnection) },
958                 { "managedObject", "( 1.3.6.1.4.1.4203.666.3.12 "
959                         "NAME 'managedObject' "
960                         "DESC 'monitor managed entity class' "
961                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
962                         offsetof(monitor_info_t, mi_oc_managedObject) },
963                 { "monitoredObject", "( 1.3.6.1.4.1.4203.666.3.14 "
964                         "NAME 'monitoredObject' "
965                         "DESC 'monitor monitored entity class' "
966                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
967                         offsetof(monitor_info_t, mi_oc_monitoredObject) },
968                 { NULL, NULL, 0, -1 }
969         }, mat[] = {
970                 { "monitoredInfo", "( 1.3.6.1.4.1.4203.666.1.14 "
971                         "NAME 'monitoredInfo' "
972                         "DESC 'monitored info' "
973                         /* "SUP name " */
974                         "EQUALITY caseIgnoreMatch "
975                         "SUBSTR caseIgnoreSubstringsMatch "
976                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{32768} "
977                         "NO-USER-MODIFICATION "
978                         "USAGE directoryOperation )", SLAP_AT_HIDE,
979                         offsetof(monitor_info_t, mi_ad_monitoredInfo) },
980                 { "managedInfo", "( 1.3.6.1.4.1.4203.666.1.15 "
981                         "NAME 'managedInfo' "
982                         "DESC 'monitor managed info' "
983                         "SUP name )", SLAP_AT_HIDE,
984                         offsetof(monitor_info_t, mi_ad_managedInfo) },
985                 { "monitorCounter", "( 1.3.6.1.4.1.4203.666.1.16 "
986                         "NAME 'monitorCounter' "
987                         "DESC 'monitor counter' "
988                         "EQUALITY integerMatch "
989                         "ORDERING integerOrderingMatch "
990                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
991                         "NO-USER-MODIFICATION "
992                         "USAGE directoryOperation )", SLAP_AT_HIDE,
993                         offsetof(monitor_info_t, mi_ad_monitorCounter) },
994                 { "monitorOpCompleted", "( 1.3.6.1.4.1.4203.666.1.17 "
995                         "NAME 'monitorOpCompleted' "
996                         "DESC 'monitor completed operations' "
997                         "SUP monitorCounter "
998                         "NO-USER-MODIFICATION "
999                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1000                         offsetof(monitor_info_t, mi_ad_monitorOpCompleted) },
1001                 { "monitorOpInitiated", "( 1.3.6.1.4.1.4203.666.1.18 "
1002                         "NAME 'monitorOpInitiated' "
1003                         "DESC 'monitor initiated operations' "
1004                         "SUP monitorCounter "
1005                         "NO-USER-MODIFICATION "
1006                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1007                         offsetof(monitor_info_t, mi_ad_monitorOpInitiated) },
1008                 { "monitorConnectionNumber", "( 1.3.6.1.4.1.4203.666.1.19 "
1009                         "NAME 'monitorConnectionNumber' "
1010                         "DESC 'monitor connection number' "
1011                         "SUP monitorCounter "
1012                         "NO-USER-MODIFICATION "
1013                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1014                         offsetof(monitor_info_t, mi_ad_monitorConnectionNumber) },
1015                 { "monitorConnectionAuthzDN", "( 1.3.6.1.4.1.4203.666.1.20 "
1016                         "NAME 'monitorConnectionAuthzDN' "
1017                         "DESC 'monitor connection authorization DN' "
1018                         /* "SUP distinguishedName " */
1019                         "EQUALITY distinguishedNameMatch "
1020                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
1021                         "NO-USER-MODIFICATION "
1022                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1023                         offsetof(monitor_info_t, mi_ad_monitorConnectionAuthzDN) },
1024                 { "monitorConnectionLocalAddress", "( 1.3.6.1.4.1.4203.666.1.21 "
1025                         "NAME 'monitorConnectionLocalAddress' "
1026                         "DESC 'monitor connection local address' "
1027                         "SUP monitoredInfo "
1028                         "NO-USER-MODIFICATION "
1029                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1030                         offsetof(monitor_info_t, mi_ad_monitorConnectionLocalAddress) },
1031                 { "monitorConnectionPeerAddress", "( 1.3.6.1.4.1.4203.666.1.22 "
1032                         "NAME 'monitorConnectionPeerAddress' "
1033                         "DESC 'monitor connection peer address' "
1034                         "SUP monitoredInfo "
1035                         "NO-USER-MODIFICATION "
1036                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1037                         offsetof(monitor_info_t, mi_ad_monitorConnectionPeerAddress) },
1038                 { "monitorTimestamp", "( 1.3.6.1.4.1.4203.666.1.24 "
1039                         "NAME 'monitorTimestamp' "
1040                         "DESC 'monitor timestamp' "
1041                         "EQUALITY generalizedTimeMatch "
1042                         "ORDERING generalizedTimeOrderingMatch "
1043                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
1044                         "SINGLE-VALUE "
1045                         "NO-USER-MODIFICATION "
1046                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1047                         offsetof(monitor_info_t, mi_ad_monitorTimestamp) },
1048                 { "monitorOverlay", "( 1.3.6.1.4.1.4203.666.1.27 "
1049                         "NAME 'monitorOverlay' "
1050                         "DESC 'name of overlays defined for a given database' "
1051                         "SUP monitoredInfo "
1052                         "NO-USER-MODIFICATION "
1053                         "USAGE directoryOperation )", SLAP_AT_HIDE,
1054                         offsetof(monitor_info_t, mi_ad_monitorOverlay) },
1055                 { "readOnly", "( 1.3.6.1.4.1.4203.666.1.31 "
1056                         "NAME 'readOnly' "
1057                         "DESC 'read/write status of a given database' "
1058                         "EQUALITY booleanMatch "
1059                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
1060                         "SINGLE-VALUE "
1061                         "USAGE directoryOperation )", SLAP_AT_HIDE,
1062                         offsetof(monitor_info_t, mi_ad_readOnly) },
1063                 { "restrictedOperation", "( 1.3.6.1.4.1.4203.666.1.32 "
1064                         "NAME 'restrictedOperation' "
1065                         "DESC 'name of restricted operation for a given database' "
1066                         "SUP managedInfo )", SLAP_AT_HIDE,
1067                         offsetof(monitor_info_t, mi_ad_restrictedOperation ) },
1068                 { "monitorConnectionProtocol", "( 1.3.6.1.4.1.4203.666.1.39 "
1069                         "NAME 'monitorConnectionProtocol' "
1070                         "DESC 'monitor connection protocol' "
1071                         "SUP monitoredInfo "
1072                         "NO-USER-MODIFICATION "
1073                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1074                         offsetof(monitor_info_t, mi_ad_monitorConnectionProtocol) },
1075                 { "monitorConnectionOpsReceived", "( 1.3.6.1.4.1.4203.666.1.40 "
1076                         "NAME 'monitorConnectionOpsReceived' "
1077                         "DESC 'monitor number of operations received by the connection' "
1078                         "SUP monitorCounter "
1079                         "NO-USER-MODIFICATION "
1080                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1081                         offsetof(monitor_info_t, mi_ad_monitorConnectionOpsReceived) },
1082                 { "monitorConnectionOpsExecuting", "( 1.3.6.1.4.1.4203.666.1.41 "
1083                         "NAME 'monitorConnectionOpsExecuting' "
1084                         "DESC 'monitor number of operations in execution within the connection' "
1085                         "SUP monitorCounter "
1086                         "NO-USER-MODIFICATION "
1087                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1088                         offsetof(monitor_info_t, mi_ad_monitorConnectionOpsExecuting) },
1089                 { "monitorConnectionOpsPending", "( 1.3.6.1.4.1.4203.666.1.42 "
1090                         "NAME 'monitorConnectionOpsPending' "
1091                         "DESC 'monitor number of pending operations within the connection' "
1092                         "SUP monitorCounter "
1093                         "NO-USER-MODIFICATION "
1094                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1095                         offsetof(monitor_info_t, mi_ad_monitorConnectionOpsPending) },
1096                 { "monitorConnectionOpsCompleted", "( 1.3.6.1.4.1.4203.666.1.43 "
1097                         "NAME 'monitorConnectionOpsCompleted' "
1098                         "DESC 'monitor number of operations completed within the connection' "
1099                         "SUP monitorCounter "
1100                         "NO-USER-MODIFICATION "
1101                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1102                         offsetof(monitor_info_t, mi_ad_monitorConnectionOpsCompleted) },
1103                 { "monitorConnectionGet", "( 1.3.6.1.4.1.4203.666.1.44 "
1104                         "NAME 'monitorConnectionGet' "
1105                         "DESC 'number of times connection_get() was called so far' "
1106                         "SUP monitorCounter "
1107                         "NO-USER-MODIFICATION "
1108                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1109                         offsetof(monitor_info_t, mi_ad_monitorConnectionGet) },
1110                 { "monitorConnectionRead", "( 1.3.6.1.4.1.4203.666.1.45 "
1111                         "NAME 'monitorConnectionRead' "
1112                         "DESC 'number of times connection_read() was called so far' "
1113                         "SUP monitorCounter "
1114                         "NO-USER-MODIFICATION "
1115                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1116                         offsetof(monitor_info_t, mi_ad_monitorConnectionRead) },
1117                 { "monitorConnectionWrite", "( 1.3.6.1.4.1.4203.666.1.46 "
1118                         "NAME 'monitorConnectionWrite' "
1119                         "DESC 'number of times connection_write() was called so far' "
1120                         "SUP monitorCounter "
1121                         "NO-USER-MODIFICATION "
1122                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1123                         offsetof(monitor_info_t, mi_ad_monitorConnectionWrite) },
1124                 { "monitorConnectionMask", "( 1.3.6.1.4.1.4203.666.1.47 "
1125                         "NAME 'monitorConnectionMask' "
1126                         "DESC 'monitor connection mask' "
1127                         "SUP monitoredInfo "
1128                         "NO-USER-MODIFICATION "
1129                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1130                         offsetof(monitor_info_t, mi_ad_monitorConnectionMask) },
1131                 { "monitorConnectionListener", "( 1.3.6.1.4.1.4203.666.1.48 "
1132                         "NAME 'monitorConnectionListener' "
1133                         "DESC 'monitor connection listener' "
1134                         "SUP monitoredInfo "
1135                         "NO-USER-MODIFICATION "
1136                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1137                         offsetof(monitor_info_t, mi_ad_monitorConnectionListener) },
1138                 { "monitorConnectionPeerDomain", "( 1.3.6.1.4.1.4203.666.1.49 "
1139                         "NAME 'monitorConnectionPeerDomain' "
1140                         "DESC 'monitor connection peer domain' "
1141                         "SUP monitoredInfo "
1142                         "NO-USER-MODIFICATION "
1143                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1144                         offsetof(monitor_info_t, mi_ad_monitorConnectionPeerDomain) },
1145                 { "monitorConnectionStartTime", "( 1.3.6.1.4.1.4203.666.1.50 "
1146                         "NAME 'monitorConnectionStartTime' "
1147                         "DESC 'monitor connection start time' "
1148                         "SUP monitorTimestamp "
1149                         "SINGLE-VALUE "
1150                         "NO-USER-MODIFICATION "
1151                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1152                         offsetof(monitor_info_t, mi_ad_monitorConnectionStartTime) },
1153                 { "monitorConnectionActivityTime", "( 1.3.6.1.4.1.4203.666.1.51 "
1154                         "NAME 'monitorConnectionActivityTime' "
1155                         "DESC 'monitor connection activity time' "
1156                         "SUP monitorTimestamp "
1157                         "SINGLE-VALUE "
1158                         "NO-USER-MODIFICATION "
1159                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1160                         offsetof(monitor_info_t, mi_ad_monitorConnectionActivityTime) },
1161                 { "monitorIsShadow", "( 1.3.6.1.4.1.4203.666.1.52 "
1162                         "NAME 'monitorIsShadow' "
1163                         "DESC 'TRUE if the database is shadow' "
1164                         "EQUALITY booleanMatch "
1165                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
1166                         "SINGLE-VALUE "
1167                         "USAGE directoryOperation )", SLAP_AT_HIDE,
1168                         offsetof(monitor_info_t, mi_ad_monitorIsShadow) },
1169                 { NULL, NULL, 0, -1 }
1170         };
1171         
1172         bi->bi_controls = controls;
1173
1174         bi->bi_init = 0;
1175         bi->bi_open = 0;
1176         bi->bi_config = monitor_back_config;
1177         bi->bi_close = 0;
1178         bi->bi_destroy = 0;
1179
1180         bi->bi_db_init = monitor_back_db_init;
1181         bi->bi_db_config = monitor_back_db_config;
1182         bi->bi_db_open = monitor_back_db_open;
1183         bi->bi_db_close = 0;
1184         bi->bi_db_destroy = monitor_back_db_destroy;
1185
1186         bi->bi_op_bind = monitor_back_bind;
1187         bi->bi_op_unbind = 0;
1188         bi->bi_op_search = monitor_back_search;
1189         bi->bi_op_compare = monitor_back_compare;
1190         bi->bi_op_modify = monitor_back_modify;
1191         bi->bi_op_modrdn = 0;
1192         bi->bi_op_add = 0;
1193         bi->bi_op_delete = 0;
1194         bi->bi_op_abandon = 0;
1195
1196         bi->bi_extended = 0;
1197
1198         bi->bi_entry_release_rw = 0;
1199         bi->bi_chk_referrals = 0;
1200         bi->bi_operational = monitor_back_operational;
1201
1202         /*
1203          * hooks for slap tools
1204          */
1205         bi->bi_tool_entry_open = 0;
1206         bi->bi_tool_entry_close = 0;
1207         bi->bi_tool_entry_first = 0;
1208         bi->bi_tool_entry_next = 0;
1209         bi->bi_tool_entry_get = 0;
1210         bi->bi_tool_entry_put = 0;
1211         bi->bi_tool_entry_reindex = 0;
1212         bi->bi_tool_sync = 0;
1213         bi->bi_tool_dn2id_get = 0;
1214         bi->bi_tool_id2entry_get = 0;
1215         bi->bi_tool_entry_modify = 0;
1216
1217         bi->bi_connection_init = 0;
1218         bi->bi_connection_destroy = 0;
1219
1220         for ( ms = known_monitor_subsys; ms->mss_name != NULL; ms++ ) {
1221                 if ( monitor_back_register_subsys( ms ) ) {
1222                         return -1;
1223                 }
1224         }
1225
1226         /* schema integration */
1227         for ( i = 0; mat[ i ].name; i++ ) {
1228                 LDAPAttributeType       *at;
1229                 int                     code;
1230                 const char              *err;
1231                 AttributeDescription    **ad;
1232
1233                 at = ldap_str2attributetype( mat[ i ].schema, &code,
1234                         &err, LDAP_SCHEMA_ALLOW_ALL );
1235                 if ( !at ) {
1236                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
1237                                 "in AttributeType \"%s\" %s before %s\n",
1238                                 mat[ i ].name, ldap_scherr2str(code), err );
1239                         return -1;
1240                 }
1241
1242                 if ( at->at_oid == NULL ) {
1243                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
1244                                 "null OID for attributeType \"%s\"\n",
1245                                 mat[ i ].name, 0, 0 );
1246                         return -1;
1247                 }
1248
1249                 code = at_add(at, 0, NULL, &err);
1250                 if ( code ) {
1251                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
1252                                 "%s in attributeType \"%s\"\n",
1253                                 scherr2str(code), mat[ i ].name, 0 );
1254                         return -1;
1255                 }
1256                 ldap_memfree(at);
1257
1258                 ad = ((AttributeDescription **)&(((char *)mi)[ mat[ i ].offset ]));
1259                 ad[ 0 ] = NULL;
1260                 if ( slap_str2ad( mat[ i ].name, ad, &text ) ) {
1261                         Debug( LDAP_DEBUG_ANY,
1262                                 "monitor_back_db_init: %s\n", text, 0, 0 );
1263                         return -1;
1264                 }
1265
1266                 (*ad)->ad_type->sat_flags |= mat[ i ].flags;
1267         }
1268
1269         for ( i = 0; moc[ i ].name; i++ ) {
1270                 LDAPObjectClass         *oc;
1271                 int                     code;
1272                 const char              *err;
1273                 ObjectClass             *Oc;
1274
1275                 oc = ldap_str2objectclass(moc[ i ].schema, &code, &err,
1276                                 LDAP_SCHEMA_ALLOW_ALL );
1277                 if ( !oc ) {
1278                         Debug( LDAP_DEBUG_ANY,
1279                                 "unable to parse monitor objectclass \"%s\": "
1280                                 "%s before %s\n" , moc[ i ].name,
1281                                 ldap_scherr2str(code), err );
1282                         return -1;
1283                 }
1284
1285                 if ( oc->oc_oid == NULL ) {
1286                         Debug( LDAP_DEBUG_ANY,
1287                                 "objectclass \"%s\" has no OID\n" ,
1288                                 moc[ i ].name, 0, 0 );
1289                         return -1;
1290                 }
1291
1292                 code = oc_add(oc, 0, NULL, &err);
1293                 if ( code ) {
1294                         Debug( LDAP_DEBUG_ANY,
1295                                 "objectclass \"%s\": %s \"%s\"\n" ,
1296                                 moc[ i ].name, scherr2str(code), err );
1297                         return -1;
1298                 }
1299
1300                 ldap_memfree(oc);
1301
1302                 Oc = oc_find( moc[ i ].name );
1303                 if ( Oc == NULL ) {
1304                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
1305                                         "unable to find objectClass %s "
1306                                         "(just added)\n", moc[ i ].name, 0, 0 );
1307                         return -1;
1308                 }
1309
1310                 Oc->soc_flags |= moc[ i ].flags;
1311
1312                 ((ObjectClass **)&(((char *)mi)[ moc[ i ].offset ]))[ 0 ] = Oc;
1313         }
1314
1315         return 0;
1316 }
1317
1318 int
1319 monitor_back_db_init(
1320         BackendDB       *be )
1321 {
1322         monitor_info_t  *mi;
1323         int             rc;
1324         struct berval   dn, ndn;
1325         struct berval   bv;
1326
1327         /*
1328          * database monitor can be defined once only
1329          */
1330         if ( be_monitor != NULL ) {
1331                 Debug( LDAP_DEBUG_ANY,
1332                         "only one monitor database is allowed\n", 0, 0, 0 );
1333                 return( -1 );
1334         }
1335         be_monitor = be;
1336
1337         /* indicate system schema supported */
1338         SLAP_BFLAGS(be) |= SLAP_BFLAG_MONITOR;
1339
1340         dn.bv_val = SLAPD_MONITOR_DN;
1341         dn.bv_len = sizeof( SLAPD_MONITOR_DN ) - 1;
1342
1343         rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL );
1344         if( rc != LDAP_SUCCESS ) {
1345                 Debug( LDAP_DEBUG_ANY,
1346                         "unable to normalize monitor DN \"%s\"\n",
1347                         SLAPD_MONITOR_DN, 0, 0 );
1348                 return -1;
1349         }
1350
1351         ber_dupbv( &bv, &dn );
1352         ber_bvarray_add( &be->be_suffix, &bv );
1353         ber_bvarray_add( &be->be_nsuffix, &ndn );
1354
1355         /* NOTE: only one monitor database is allowed,
1356          * so we use static storage */
1357         mi = &monitor_info;
1358
1359         ldap_pvt_thread_mutex_init( &mi->mi_cache_mutex );
1360
1361         be->be_private = mi;
1362
1363         return 0;
1364 }
1365
1366 int
1367 monitor_back_db_open(
1368         BackendDB       *be )
1369 {
1370         monitor_info_t          *mi = (monitor_info_t *)be->be_private;
1371         struct monitor_subsys_t **ms;
1372         Entry                   *e, **ep;
1373         monitor_entry_t         *mp;
1374         int                     i;
1375         char                    buf[ BACKMONITOR_BUFSIZE ];
1376         struct berval           bv;
1377         struct tm               *tms;
1378 #ifdef HAVE_GMTIME_R
1379         struct tm               tm_buf;
1380 #endif
1381         static char             tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1382
1383         assert( be_monitor );
1384         if ( be != be_monitor ) {
1385                 be_monitor = be;
1386         }
1387
1388         /*
1389          * Start
1390          */
1391 #ifndef HAVE_GMTIME_R
1392         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1393 #endif
1394 #ifdef HACK_LOCAL_TIME
1395 # ifdef HAVE_LOCALTIME_R
1396         tms = localtime_r( &starttime, &tm_buf );
1397 # else
1398         tms = localtime( &starttime );
1399 # endif /* HAVE_LOCALTIME_R */
1400         lutil_localtime( tmbuf, sizeof(tmbuf), tms, -timezone );
1401 #else /* !HACK_LOCAL_TIME */
1402 # ifdef HAVE_GMTIME_R
1403         tms = gmtime_r( &starttime, &tm_buf );
1404 # else
1405         tms = gmtime( &starttime );
1406 # endif /* HAVE_GMTIME_R */
1407         lutil_gentime( tmbuf, sizeof(tmbuf), tms );
1408 #endif /* !HACK_LOCAL_TIME */
1409 #ifndef HAVE_GMTIME_R
1410         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1411 #endif
1412
1413         mi->mi_startTime.bv_val = tmbuf;
1414         mi->mi_startTime.bv_len = strlen( tmbuf );
1415
1416         if ( BER_BVISEMPTY( &be->be_rootdn ) ) {
1417                 BER_BVSTR( &mi->mi_creatorsName, SLAPD_ANONYMOUS );
1418         } else {
1419                 mi->mi_creatorsName = be->be_rootdn;
1420         }
1421
1422         /*
1423          * creates the "cn=Monitor" entry 
1424          */
1425         snprintf( buf, sizeof( buf ), 
1426                 "dn: %s\n"
1427                 "objectClass: %s\n"
1428                 "structuralObjectClass: %s\n"
1429                 "cn: Monitor\n"
1430                 "description: This subtree contains monitoring/managing objects.\n"
1431                 "description: This object contains information about this server.\n"
1432                 "description: Most of the information is held in operational"
1433                 " attributeTypes, which must be explicitly requested.\n"
1434                 "creatorsName: %s\n"
1435                 "modifiersName: %s\n"
1436                 "createTimestamp: %s\n"
1437                 "modifyTimestamp: %s\n",
1438                 SLAPD_MONITOR_DN,
1439                 mi->mi_oc_monitorServer->soc_cname.bv_val,
1440                 mi->mi_oc_monitorServer->soc_cname.bv_val,
1441                 mi->mi_creatorsName.bv_val,
1442                 mi->mi_creatorsName.bv_val,
1443                 mi->mi_startTime.bv_val,
1444                 mi->mi_startTime.bv_val );
1445
1446         e = str2entry( buf );
1447         if ( e == NULL) {
1448                 Debug( LDAP_DEBUG_ANY,
1449                         "unable to create \"%s\" entry\n",
1450                         SLAPD_MONITOR_DN, 0, 0 );
1451                 return( -1 );
1452         }
1453
1454         bv.bv_val = strchr( (char *) Versionstr, '$' );
1455         if ( bv.bv_val != NULL ) {
1456                 char    *end;
1457
1458                 bv.bv_val++;
1459                 for ( ; bv.bv_val[ 0 ] == ' '; bv.bv_val++ )
1460                         ;
1461
1462                 end = strchr( bv.bv_val, '$' );
1463                 if ( end != NULL ) {
1464                         end--;
1465
1466                         for ( ; end > bv.bv_val && end[ 0 ] == ' '; end-- )
1467                                 ;
1468
1469                         end++;
1470
1471                         bv.bv_len = end - bv.bv_val;
1472
1473                 } else {
1474                         bv.bv_len = strlen( bv.bv_val );
1475                 }
1476
1477                 if ( attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo,
1478                                         &bv, NULL ) ) {
1479                         Debug( LDAP_DEBUG_ANY,
1480                                 "unable to add monitoredInfo to \"%s\" entry\n",
1481                                 SLAPD_MONITOR_DN, 0, 0 );
1482                         return( -1 );
1483                 }
1484         }
1485
1486         mp = monitor_entrypriv_create();
1487         if ( mp == NULL ) {
1488                 return -1;
1489         }
1490         e->e_private = ( void * )mp;
1491         ep = &mp->mp_children;
1492
1493         if ( monitor_cache_add( mi, e ) ) {
1494                 Debug( LDAP_DEBUG_ANY,
1495                         "unable to add entry \"%s\" to cache\n",
1496                         SLAPD_MONITOR_DN, 0, 0 );
1497                 return -1;
1498         }
1499
1500         /*      
1501          * Create all the subsystem specific entries
1502          */
1503         for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
1504                 int             len = strlen( monitor_subsys[ i ]->mss_name );
1505                 struct berval   dn;
1506                 int             rc;
1507
1508                 dn.bv_len = len + sizeof( "cn=" ) - 1;
1509                 dn.bv_val = ch_calloc( sizeof( char ), dn.bv_len + 1 );
1510                 strcpy( dn.bv_val, "cn=" );
1511                 strcat( dn.bv_val, monitor_subsys[ i ]->mss_name );
1512                 rc = dnPretty( NULL, &dn, &monitor_subsys[ i ]->mss_rdn, NULL );
1513                 free( dn.bv_val );
1514                 if ( rc != LDAP_SUCCESS ) {
1515                         Debug( LDAP_DEBUG_ANY,
1516                                 "monitor RDN \"%s\" is invalid\n", 
1517                                 dn.bv_val, 0, 0 );
1518                         return( -1 );
1519                 }
1520
1521                 dn.bv_len += sizeof( SLAPD_MONITOR_DN ); /* 1 for the , */
1522                 dn.bv_val = ch_malloc( dn.bv_len + 1 );
1523                 strcpy( dn.bv_val , monitor_subsys[ i ]->mss_rdn.bv_val );
1524                 strcat( dn.bv_val, "," SLAPD_MONITOR_DN );
1525                 rc = dnPrettyNormal( NULL, &dn, &monitor_subsys[ i ]->mss_dn,
1526                         &monitor_subsys[ i ]->mss_ndn, NULL );
1527                 free( dn.bv_val );
1528                 if ( rc != LDAP_SUCCESS ) {
1529                         Debug( LDAP_DEBUG_ANY,
1530                                 "monitor DN \"%s\" is invalid\n", 
1531                                 dn.bv_val, 0, 0 );
1532                         return( -1 );
1533                 }
1534
1535                 snprintf( buf, sizeof( buf ),
1536                                 "dn: %s\n"
1537                                 "objectClass: %s\n"
1538                                 "structuralObjectClass: %s\n"
1539                                 "cn: %s\n"
1540                                 "creatorsName: %s\n"
1541                                 "modifiersName: %s\n"
1542                                 "createTimestamp: %s\n"
1543                                 "modifyTimestamp: %s\n",
1544                                 monitor_subsys[ i ]->mss_dn.bv_val,
1545                                 mi->mi_oc_monitorContainer->soc_cname.bv_val,
1546                                 mi->mi_oc_monitorContainer->soc_cname.bv_val,
1547                                 monitor_subsys[ i ]->mss_name,
1548                                 mi->mi_creatorsName.bv_val,
1549                                 mi->mi_creatorsName.bv_val,
1550                                 mi->mi_startTime.bv_val,
1551                                 mi->mi_startTime.bv_val );
1552                 
1553                 e = str2entry( buf );
1554                 
1555                 if ( e == NULL) {
1556                         Debug( LDAP_DEBUG_ANY,
1557                                 "unable to create \"%s\" entry\n", 
1558                                 monitor_subsys[ i ]->mss_dn.bv_val, 0, 0 );
1559                         return( -1 );
1560                 }
1561
1562                 if ( !BER_BVISNULL( &monitor_subsys[ i ]->mss_desc[ 0 ] ) ) {
1563                         attr_merge_normalize( e, slap_schema.si_ad_description,
1564                                         monitor_subsys[ i ]->mss_desc, NULL );
1565                 }
1566
1567                 mp = monitor_entrypriv_create();
1568                 if ( mp == NULL ) {
1569                         return -1;
1570                 }
1571                 e->e_private = ( void * )mp;
1572                 mp->mp_info = monitor_subsys[ i ];
1573                 mp->mp_flags = monitor_subsys[ i ]->mss_flags;
1574
1575                 if ( monitor_cache_add( mi, e ) ) {
1576                         Debug( LDAP_DEBUG_ANY,
1577                                 "unable to add entry \"%s\" to cache\n",
1578                                 monitor_subsys[ i ]->mss_dn.bv_val, 0, 0 );
1579                         return -1;
1580                 }
1581
1582                 *ep = e;
1583                 ep = &mp->mp_next;
1584         }
1585
1586         assert( be );
1587
1588         be->be_private = mi;
1589         
1590         /*
1591          * opens the monitor backend subsystems
1592          */
1593         for ( ms = monitor_subsys; ms[ 0 ] != NULL; ms++ ) {
1594                 if ( ms[ 0 ]->mss_open && ( *ms[ 0 ]->mss_open )( be, ms[ 0 ] ) )
1595                 {
1596                         return( -1 );
1597                 }
1598                 ms[ 0 ]->mss_flags |= MONITOR_F_OPENED;
1599         }
1600
1601         monitor_subsys_opened = 1;
1602
1603         if ( mi->mi_entry_limbo ) {
1604                 entry_limbo_t   *el = (entry_limbo_t *)mi->mi_entry_limbo;
1605
1606                 for ( ; el; ) {
1607                         entry_limbo_t   *tmp;
1608
1609                         switch ( el->el_type ) {
1610                         case LIMBO_ENTRY:
1611                                 monitor_back_register_entry(
1612                                                 el->el_e,
1613                                                 el->el_cb );
1614                                 break;
1615
1616                         case LIMBO_ENTRY_PARENT:
1617                                 monitor_back_register_entry_parent(
1618                                                 el->el_e,
1619                                                 el->el_cb,
1620                                                 &el->el_base,
1621                                                 el->el_scope,
1622                                                 &el->el_filter );
1623                                 break;
1624                                 
1625
1626                         case LIMBO_ATTRS:
1627                                 monitor_back_register_entry_attrs(
1628                                                 &el->el_ndn,
1629                                                 el->el_a,
1630                                                 el->el_cb,
1631                                                 &el->el_base,
1632                                                 el->el_scope,
1633                                                 &el->el_filter );
1634                                 break;
1635
1636                         case LIMBO_CB:
1637                                 monitor_back_register_entry_callback(
1638                                                 &el->el_ndn,
1639                                                 el->el_cb,
1640                                                 &el->el_base,
1641                                                 el->el_scope,
1642                                                 &el->el_filter );
1643                                 break;
1644
1645                         default:
1646                                 assert( 0 );
1647                         }
1648
1649                         if ( el->el_e ) {
1650                                 entry_free( el->el_e );
1651                         }
1652                         if ( el->el_a ) {
1653                                 attrs_free( el->el_a );
1654                         }
1655                         if ( !BER_BVISNULL( &el->el_ndn ) ) {
1656                                 ber_memfree( el->el_ndn.bv_val );
1657                         }
1658                         if ( !BER_BVISNULL( &el->el_base ) ) {
1659                                 ber_memfree( el->el_base.bv_val );
1660                         }
1661                         if ( !BER_BVISNULL( &el->el_filter ) ) {
1662                                 ber_memfree( el->el_filter.bv_val );
1663                         }
1664
1665                         tmp = el;
1666                         el = el->el_next;
1667                         ch_free( tmp );
1668                 }
1669
1670                 mi->mi_entry_limbo = NULL;
1671         }
1672
1673         return( 0 );
1674 }
1675
1676 int
1677 monitor_back_config(
1678         BackendInfo     *bi,
1679         const char      *fname,
1680         int             lineno,
1681         int             argc,
1682         char            **argv )
1683 {
1684         /*
1685          * eventually, will hold backend specific configuration parameters
1686          */
1687         return SLAP_CONF_UNKNOWN;
1688 }
1689
1690 int
1691 monitor_back_db_config(
1692         Backend     *be,
1693         const char  *fname,
1694         int         lineno,
1695         int         argc,
1696         char        **argv )
1697 {
1698         monitor_info_t  *mi = ( monitor_info_t * )be->be_private;
1699
1700         /*
1701          * eventually, will hold database specific configuration parameters
1702          */
1703         return SLAP_CONF_UNKNOWN;
1704 }
1705
1706 int
1707 monitor_back_db_destroy(
1708         BackendDB       *be )
1709 {
1710         monitor_info_t  *mi = ( monitor_info_t * )be->be_private;
1711
1712         /*
1713          * FIXME: destroys all the data
1714          */
1715         /* NOTE: mi points to static storage; don't free it */
1716         
1717         return 0;
1718 }
1719
1720 #if SLAPD_MONITOR == SLAPD_MOD_DYNAMIC
1721
1722 /* conditionally define the init_module() function */
1723 SLAP_BACKEND_INIT_MODULE( monitor )
1724
1725 #endif /* SLAPD_MONITOR == SLAPD_MOD_DYNAMIC */
1726