]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/init.c
Initial round of changes for 2.3 beta
[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                 { NULL, NULL, 0, -1 }
1162         };
1163         
1164         bi->bi_controls = controls;
1165
1166         bi->bi_init = 0;
1167         bi->bi_open = 0;
1168         bi->bi_config = monitor_back_config;
1169         bi->bi_close = 0;
1170         bi->bi_destroy = 0;
1171
1172         bi->bi_db_init = monitor_back_db_init;
1173         bi->bi_db_config = monitor_back_db_config;
1174         bi->bi_db_open = monitor_back_db_open;
1175         bi->bi_db_close = 0;
1176         bi->bi_db_destroy = monitor_back_db_destroy;
1177
1178         bi->bi_op_bind = monitor_back_bind;
1179         bi->bi_op_unbind = 0;
1180         bi->bi_op_search = monitor_back_search;
1181         bi->bi_op_compare = monitor_back_compare;
1182         bi->bi_op_modify = monitor_back_modify;
1183         bi->bi_op_modrdn = 0;
1184         bi->bi_op_add = 0;
1185         bi->bi_op_delete = 0;
1186         bi->bi_op_abandon = 0;
1187
1188         bi->bi_extended = 0;
1189
1190         bi->bi_entry_release_rw = 0;
1191         bi->bi_chk_referrals = 0;
1192         bi->bi_operational = monitor_back_operational;
1193
1194         /*
1195          * hooks for slap tools
1196          */
1197         bi->bi_tool_entry_open = 0;
1198         bi->bi_tool_entry_close = 0;
1199         bi->bi_tool_entry_first = 0;
1200         bi->bi_tool_entry_next = 0;
1201         bi->bi_tool_entry_get = 0;
1202         bi->bi_tool_entry_put = 0;
1203         bi->bi_tool_entry_reindex = 0;
1204         bi->bi_tool_sync = 0;
1205         bi->bi_tool_dn2id_get = 0;
1206         bi->bi_tool_id2entry_get = 0;
1207         bi->bi_tool_entry_modify = 0;
1208
1209         bi->bi_connection_init = 0;
1210         bi->bi_connection_destroy = 0;
1211
1212         for ( ms = known_monitor_subsys; ms->mss_name != NULL; ms++ ) {
1213                 if ( monitor_back_register_subsys( ms ) ) {
1214                         return -1;
1215                 }
1216         }
1217
1218         /* schema integration */
1219         for ( i = 0; mat[ i ].name; i++ ) {
1220                 LDAPAttributeType       *at;
1221                 int                     code;
1222                 const char              *err;
1223                 AttributeDescription    **ad;
1224
1225                 at = ldap_str2attributetype( mat[ i ].schema, &code,
1226                         &err, LDAP_SCHEMA_ALLOW_ALL );
1227                 if ( !at ) {
1228                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
1229                                 "in AttributeType \"%s\" %s before %s\n",
1230                                 mat[ i ].name, ldap_scherr2str(code), err );
1231                         return -1;
1232                 }
1233
1234                 if ( at->at_oid == NULL ) {
1235                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
1236                                 "null OID for attributeType \"%s\"\n",
1237                                 mat[ i ].name, 0, 0 );
1238                         return -1;
1239                 }
1240
1241                 code = at_add(at, 0, NULL, &err);
1242                 if ( code ) {
1243                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
1244                                 "%s in attributeType \"%s\"\n",
1245                                 scherr2str(code), mat[ i ].name, 0 );
1246                         return -1;
1247                 }
1248                 ldap_memfree(at);
1249
1250                 ad = ((AttributeDescription **)&(((char *)mi)[ mat[ i ].offset ]));
1251                 ad[ 0 ] = NULL;
1252                 if ( slap_str2ad( mat[ i ].name, ad, &text ) ) {
1253                         Debug( LDAP_DEBUG_ANY,
1254                                 "monitor_back_db_init: %s\n", text, 0, 0 );
1255                         return -1;
1256                 }
1257
1258                 (*ad)->ad_type->sat_flags |= mat[ i ].flags;
1259         }
1260
1261         for ( i = 0; moc[ i ].name; i++ ) {
1262                 LDAPObjectClass         *oc;
1263                 int                     code;
1264                 const char              *err;
1265                 ObjectClass             *Oc;
1266
1267                 oc = ldap_str2objectclass(moc[ i ].schema, &code, &err,
1268                                 LDAP_SCHEMA_ALLOW_ALL );
1269                 if ( !oc ) {
1270                         Debug( LDAP_DEBUG_ANY,
1271                                 "unable to parse monitor objectclass \"%s\": "
1272                                 "%s before %s\n" , moc[ i ].name,
1273                                 ldap_scherr2str(code), err );
1274                         return -1;
1275                 }
1276
1277                 if ( oc->oc_oid == NULL ) {
1278                         Debug( LDAP_DEBUG_ANY,
1279                                 "objectclass \"%s\" has no OID\n" ,
1280                                 moc[ i ].name, 0, 0 );
1281                         return -1;
1282                 }
1283
1284                 code = oc_add(oc, 0, NULL, &err);
1285                 if ( code ) {
1286                         Debug( LDAP_DEBUG_ANY,
1287                                 "objectclass \"%s\": %s \"%s\"\n" ,
1288                                 moc[ i ].name, scherr2str(code), err );
1289                         return -1;
1290                 }
1291
1292                 ldap_memfree(oc);
1293
1294                 Oc = oc_find( moc[ i ].name );
1295                 if ( Oc == NULL ) {
1296                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
1297                                         "unable to find objectClass %s "
1298                                         "(just added)\n", moc[ i ].name, 0, 0 );
1299                         return -1;
1300                 }
1301
1302                 Oc->soc_flags |= moc[ i ].flags;
1303
1304                 ((ObjectClass **)&(((char *)mi)[ moc[ i ].offset ]))[ 0 ] = Oc;
1305         }
1306
1307         return 0;
1308 }
1309
1310 int
1311 monitor_back_db_init(
1312         BackendDB       *be )
1313 {
1314         monitor_info_t  *mi;
1315         int             rc;
1316         struct berval   dn, ndn;
1317         struct berval   bv;
1318
1319         /*
1320          * database monitor can be defined once only
1321          */
1322         if ( be_monitor != NULL ) {
1323                 Debug( LDAP_DEBUG_ANY,
1324                         "only one monitor database is allowed\n", 0, 0, 0 );
1325                 return( -1 );
1326         }
1327         be_monitor = be;
1328
1329         /* indicate system schema supported */
1330         SLAP_BFLAGS(be) |= SLAP_BFLAG_MONITOR;
1331
1332         dn.bv_val = SLAPD_MONITOR_DN;
1333         dn.bv_len = sizeof( SLAPD_MONITOR_DN ) - 1;
1334
1335         rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL );
1336         if( rc != LDAP_SUCCESS ) {
1337                 Debug( LDAP_DEBUG_ANY,
1338                         "unable to normalize monitor DN \"%s\"\n",
1339                         SLAPD_MONITOR_DN, 0, 0 );
1340                 return -1;
1341         }
1342
1343         ber_dupbv( &bv, &dn );
1344         ber_bvarray_add( &be->be_suffix, &bv );
1345         ber_bvarray_add( &be->be_nsuffix, &ndn );
1346
1347         /* NOTE: only one monitor database is allowed,
1348          * so we use static storage */
1349         mi = &monitor_info;
1350
1351         ldap_pvt_thread_mutex_init( &mi->mi_cache_mutex );
1352
1353         be->be_private = mi;
1354
1355         return 0;
1356 }
1357
1358 int
1359 monitor_back_db_open(
1360         BackendDB       *be )
1361 {
1362         monitor_info_t          *mi = (monitor_info_t *)be->be_private;
1363         struct monitor_subsys_t **ms;
1364         Entry                   *e, **ep;
1365         monitor_entry_t         *mp;
1366         int                     i;
1367         char                    buf[ BACKMONITOR_BUFSIZE ],
1368                                 *end_of_line;
1369         struct berval           bv;
1370         struct tm               *tms;
1371 #ifdef HAVE_GMTIME_R
1372         struct tm               tm_buf;
1373 #endif
1374         static char             tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1375
1376         assert( be_monitor );
1377         if ( be != be_monitor ) {
1378                 be_monitor = be;
1379         }
1380
1381         /*
1382          * Start
1383          */
1384 #ifndef HAVE_GMTIME_R
1385         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1386 #endif
1387 #ifdef HACK_LOCAL_TIME
1388 # ifdef HAVE_LOCALTIME_R
1389         tms = localtime_r( &starttime, &tm_buf );
1390 # else
1391         tms = localtime( &starttime );
1392 # endif /* HAVE_LOCALTIME_R */
1393         lutil_localtime( tmbuf, sizeof(tmbuf), tms, -timezone );
1394 #else /* !HACK_LOCAL_TIME */
1395 # ifdef HAVE_GMTIME_R
1396         tms = gmtime_r( &starttime, &tm_buf );
1397 # else
1398         tms = gmtime( &starttime );
1399 # endif /* HAVE_GMTIME_R */
1400         lutil_gentime( tmbuf, sizeof(tmbuf), tms );
1401 #endif /* !HACK_LOCAL_TIME */
1402 #ifndef HAVE_GMTIME_R
1403         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1404 #endif
1405
1406         mi->mi_startTime.bv_val = tmbuf;
1407         mi->mi_startTime.bv_len = strlen( tmbuf );
1408
1409         if ( BER_BVISEMPTY( &be->be_rootdn ) ) {
1410                 BER_BVSTR( &mi->mi_creatorsName, SLAPD_ANONYMOUS );
1411         } else {
1412                 mi->mi_creatorsName = be->be_rootdn;
1413         }
1414
1415         /*
1416          * creates the "cn=Monitor" entry 
1417          */
1418         snprintf( buf, sizeof( buf ), 
1419                 "dn: %s\n"
1420                 "objectClass: %s\n"
1421                 "structuralObjectClass: %s\n"
1422                 "cn: Monitor\n"
1423                 "description: This subtree contains monitoring/managing objects.\n"
1424                 "description: This object contains information about this server.\n"
1425                 "description: Most of the information is contained in operational\n"
1426                 "description: attributeTypes, which must be explicitly requested.\n"
1427                 "creatorsName: %s\n"
1428                 "modifiersName: %s\n"
1429                 "createTimestamp: %s\n"
1430                 "modifyTimestamp: %s\n",
1431                 SLAPD_MONITOR_DN,
1432                 mi->mi_oc_monitorServer->soc_cname.bv_val,
1433                 mi->mi_oc_monitorServer->soc_cname.bv_val,
1434                 mi->mi_creatorsName.bv_val,
1435                 mi->mi_creatorsName.bv_val,
1436                 mi->mi_startTime.bv_val,
1437                 mi->mi_startTime.bv_val );
1438
1439         e = str2entry( buf );
1440         if ( e == NULL) {
1441                 Debug( LDAP_DEBUG_ANY,
1442                         "unable to create \"%s\" entry\n",
1443                         SLAPD_MONITOR_DN, 0, 0 );
1444                 return( -1 );
1445         }
1446
1447         bv.bv_val = (char *) Versionstr;
1448         end_of_line = strchr( Versionstr, '\n' );
1449         if ( end_of_line ) {
1450                 bv.bv_len = end_of_line - Versionstr;
1451         } else {
1452                 bv.bv_len = strlen( Versionstr );
1453         }
1454
1455         if ( attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo,
1456                                 &bv, NULL ) ) {
1457                 Debug( LDAP_DEBUG_ANY,
1458                         "unable to add monitoredInfo to \"%s\" entry\n",
1459                         SLAPD_MONITOR_DN, 0, 0 );
1460                 return( -1 );
1461         }
1462
1463         mp = monitor_entrypriv_create();
1464         if ( mp == NULL ) {
1465                 return -1;
1466         }
1467         e->e_private = ( void * )mp;
1468         ep = &mp->mp_children;
1469
1470         if ( monitor_cache_add( mi, e ) ) {
1471                 Debug( LDAP_DEBUG_ANY,
1472                         "unable to add entry \"%s\" to cache\n",
1473                         SLAPD_MONITOR_DN, 0, 0 );
1474                 return -1;
1475         }
1476
1477         /*      
1478          * Create all the subsystem specific entries
1479          */
1480         for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
1481                 int             len = strlen( monitor_subsys[ i ]->mss_name );
1482                 struct berval   dn;
1483                 int             rc;
1484
1485                 dn.bv_len = len + sizeof( "cn=" ) - 1;
1486                 dn.bv_val = ch_calloc( sizeof( char ), dn.bv_len + 1 );
1487                 strcpy( dn.bv_val, "cn=" );
1488                 strcat( dn.bv_val, monitor_subsys[ i ]->mss_name );
1489                 rc = dnPretty( NULL, &dn, &monitor_subsys[ i ]->mss_rdn, NULL );
1490                 free( dn.bv_val );
1491                 if ( rc != LDAP_SUCCESS ) {
1492                         Debug( LDAP_DEBUG_ANY,
1493                                 "monitor RDN \"%s\" is invalid\n", 
1494                                 dn.bv_val, 0, 0 );
1495                         return( -1 );
1496                 }
1497
1498                 dn.bv_len += sizeof( SLAPD_MONITOR_DN ); /* 1 for the , */
1499                 dn.bv_val = ch_malloc( dn.bv_len + 1 );
1500                 strcpy( dn.bv_val , monitor_subsys[ i ]->mss_rdn.bv_val );
1501                 strcat( dn.bv_val, "," SLAPD_MONITOR_DN );
1502                 rc = dnPrettyNormal( NULL, &dn, &monitor_subsys[ i ]->mss_dn,
1503                         &monitor_subsys[ i ]->mss_ndn, NULL );
1504                 free( dn.bv_val );
1505                 if ( rc != LDAP_SUCCESS ) {
1506                         Debug( LDAP_DEBUG_ANY,
1507                                 "monitor DN \"%s\" is invalid\n", 
1508                                 dn.bv_val, 0, 0 );
1509                         return( -1 );
1510                 }
1511
1512                 snprintf( buf, sizeof( buf ),
1513                                 "dn: %s\n"
1514                                 "objectClass: %s\n"
1515                                 "structuralObjectClass: %s\n"
1516                                 "cn: %s\n"
1517                                 "creatorsName: %s\n"
1518                                 "modifiersName: %s\n"
1519                                 "createTimestamp: %s\n"
1520                                 "modifyTimestamp: %s\n",
1521                                 monitor_subsys[ i ]->mss_dn.bv_val,
1522                                 mi->mi_oc_monitorContainer->soc_cname.bv_val,
1523                                 mi->mi_oc_monitorContainer->soc_cname.bv_val,
1524                                 monitor_subsys[ i ]->mss_name,
1525                                 mi->mi_creatorsName.bv_val,
1526                                 mi->mi_creatorsName.bv_val,
1527                                 mi->mi_startTime.bv_val,
1528                                 mi->mi_startTime.bv_val );
1529                 
1530                 e = str2entry( buf );
1531                 
1532                 if ( e == NULL) {
1533                         Debug( LDAP_DEBUG_ANY,
1534                                 "unable to create \"%s\" entry\n", 
1535                                 monitor_subsys[ i ]->mss_dn.bv_val, 0, 0 );
1536                         return( -1 );
1537                 }
1538
1539                 if ( !BER_BVISNULL( &monitor_subsys[ i ]->mss_desc[ 0 ] ) ) {
1540                         attr_merge_normalize( e, slap_schema.si_ad_description,
1541                                         monitor_subsys[ i ]->mss_desc, NULL );
1542                 }
1543
1544                 mp = monitor_entrypriv_create();
1545                 if ( mp == NULL ) {
1546                         return -1;
1547                 }
1548                 e->e_private = ( void * )mp;
1549                 mp->mp_info = monitor_subsys[ i ];
1550                 mp->mp_flags = monitor_subsys[ i ]->mss_flags;
1551
1552                 if ( monitor_cache_add( mi, e ) ) {
1553                         Debug( LDAP_DEBUG_ANY,
1554                                 "unable to add entry \"%s\" to cache\n",
1555                                 monitor_subsys[ i ]->mss_dn.bv_val, 0, 0 );
1556                         return -1;
1557                 }
1558
1559                 *ep = e;
1560                 ep = &mp->mp_next;
1561         }
1562
1563         assert( be );
1564
1565         be->be_private = mi;
1566         
1567         /*
1568          * opens the monitor backend subsystems
1569          */
1570         for ( ms = monitor_subsys; ms[ 0 ] != NULL; ms++ ) {
1571                 if ( ms[ 0 ]->mss_open && ( *ms[ 0 ]->mss_open )( be, ms[ 0 ] ) )
1572                 {
1573                         return( -1 );
1574                 }
1575                 ms[ 0 ]->mss_flags |= MONITOR_F_OPENED;
1576         }
1577
1578         monitor_subsys_opened = 1;
1579
1580         if ( mi->mi_entry_limbo ) {
1581                 entry_limbo_t   *el = (entry_limbo_t *)mi->mi_entry_limbo;
1582
1583                 for ( ; el; ) {
1584                         entry_limbo_t   *tmp;
1585
1586                         switch ( el->el_type ) {
1587                         case LIMBO_ENTRY:
1588                                 monitor_back_register_entry(
1589                                                 el->el_e,
1590                                                 el->el_cb );
1591                                 break;
1592
1593                         case LIMBO_ENTRY_PARENT:
1594                                 monitor_back_register_entry_parent(
1595                                                 el->el_e,
1596                                                 el->el_cb,
1597                                                 &el->el_base,
1598                                                 el->el_scope,
1599                                                 &el->el_filter );
1600                                 break;
1601                                 
1602
1603                         case LIMBO_ATTRS:
1604                                 monitor_back_register_entry_attrs(
1605                                                 &el->el_ndn,
1606                                                 el->el_a,
1607                                                 el->el_cb,
1608                                                 &el->el_base,
1609                                                 el->el_scope,
1610                                                 &el->el_filter );
1611                                 break;
1612
1613                         case LIMBO_CB:
1614                                 monitor_back_register_entry_callback(
1615                                                 &el->el_ndn,
1616                                                 el->el_cb,
1617                                                 &el->el_base,
1618                                                 el->el_scope,
1619                                                 &el->el_filter );
1620                                 break;
1621
1622                         default:
1623                                 assert( 0 );
1624                         }
1625
1626                         if ( el->el_e ) {
1627                                 entry_free( el->el_e );
1628                         }
1629                         if ( el->el_a ) {
1630                                 attrs_free( el->el_a );
1631                         }
1632                         if ( !BER_BVISNULL( &el->el_ndn ) ) {
1633                                 ber_memfree( el->el_ndn.bv_val );
1634                         }
1635                         if ( !BER_BVISNULL( &el->el_base ) ) {
1636                                 ber_memfree( el->el_base.bv_val );
1637                         }
1638                         if ( !BER_BVISNULL( &el->el_filter ) ) {
1639                                 ber_memfree( el->el_filter.bv_val );
1640                         }
1641
1642                         tmp = el;
1643                         el = el->el_next;
1644                         ch_free( tmp );
1645                 }
1646
1647                 mi->mi_entry_limbo = NULL;
1648         }
1649
1650         return( 0 );
1651 }
1652
1653 int
1654 monitor_back_config(
1655         BackendInfo     *bi,
1656         const char      *fname,
1657         int             lineno,
1658         int             argc,
1659         char            **argv )
1660 {
1661         /*
1662          * eventually, will hold backend specific configuration parameters
1663          */
1664         return SLAP_CONF_UNKNOWN;
1665 }
1666
1667 int
1668 monitor_back_db_config(
1669         Backend     *be,
1670         const char  *fname,
1671         int         lineno,
1672         int         argc,
1673         char        **argv )
1674 {
1675         monitor_info_t  *mi = ( monitor_info_t * )be->be_private;
1676
1677         /*
1678          * eventually, will hold database specific configuration parameters
1679          */
1680         return SLAP_CONF_UNKNOWN;
1681 }
1682
1683 int
1684 monitor_back_db_destroy(
1685         BackendDB       *be )
1686 {
1687         monitor_info_t  *mi = ( monitor_info_t * )be->be_private;
1688
1689         /*
1690          * FIXME: destroys all the data
1691          */
1692         /* NOTE: mi points to static storage; don't free it */
1693         
1694         return 0;
1695 }
1696
1697 #if SLAPD_MONITOR == SLAPD_MOD_DYNAMIC
1698
1699 /* conditionally define the init_module() function */
1700 SLAP_BACKEND_INIT_MODULE( monitor )
1701
1702 #endif /* SLAPD_MONITOR == SLAPD_MOD_DYNAMIC */
1703