]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/init.c
rework subsystem initialization (can be added dynamically...)
[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-2004 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 BackendDB *be_monitor = NULL;
38
39 /*
40  * subsystem data
41  */
42 static struct monitorsubsys known_monitor_subsys[] = {
43         { 
44                 SLAPD_MONITOR_BACKEND_NAME, 
45                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
46                 MONITOR_F_PERSISTENT_CH,
47                 monitor_subsys_backend_init,
48                 NULL,   /* update */
49                 NULL,   /* create */
50                 NULL    /* modify */
51         }, { 
52                 SLAPD_MONITOR_CONN_NAME,
53                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
54                 MONITOR_F_VOLATILE_CH,
55                 monitor_subsys_conn_init,
56                 monitor_subsys_conn_update,
57                 monitor_subsys_conn_create,
58                 NULL    /* modify */
59         }, { 
60                 SLAPD_MONITOR_DATABASE_NAME,    
61                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
62                 MONITOR_F_PERSISTENT_CH,
63                 monitor_subsys_database_init,
64                 NULL,   /* update */
65                 NULL,   /* create */
66                 monitor_subsys_database_modify
67         }, { 
68                 SLAPD_MONITOR_LISTENER_NAME,    
69                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
70                 MONITOR_F_PERSISTENT_CH,
71                 monitor_subsys_listener_init,
72                 NULL,   /* update */
73                 NULL,   /* create */
74                 NULL    /* modify */
75         }, { 
76                 SLAPD_MONITOR_LOG_NAME,
77                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
78                 MONITOR_F_NONE,
79                 monitor_subsys_log_init,
80                 NULL,   /* update */
81                 NULL,   /* create */
82                 monitor_subsys_log_modify
83         }, { 
84                 SLAPD_MONITOR_OPS_NAME,
85                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
86                 MONITOR_F_PERSISTENT_CH,
87                 monitor_subsys_ops_init,
88                 monitor_subsys_ops_update,
89                 NULL,   /* create */
90                 NULL,   /* modify */
91         }, { 
92                 SLAPD_MONITOR_OVERLAY_NAME,
93                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
94                 MONITOR_F_PERSISTENT_CH,
95                 monitor_subsys_overlay_init,
96                 NULL,   /* update */
97                 NULL,   /* create */
98                 NULL,   /* modify */
99         }, { 
100                 SLAPD_MONITOR_SASL_NAME,        
101                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
102                 MONITOR_F_NONE,
103                 NULL,   /* init */
104                 NULL,   /* update */
105                 NULL,   /* create */
106                 NULL    /* modify */
107         }, { 
108                 SLAPD_MONITOR_SENT_NAME,
109                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
110                 MONITOR_F_PERSISTENT_CH,
111                 monitor_subsys_sent_init,
112                 monitor_subsys_sent_update,
113                 NULL,   /* create */
114                 NULL,   /* modify */
115         }, { 
116                 SLAPD_MONITOR_THREAD_NAME,      
117                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
118                 MONITOR_F_PERSISTENT_CH,
119                 monitor_subsys_thread_init,
120                 monitor_subsys_thread_update,
121                 NULL,   /* create */
122                 NULL    /* modify */
123         }, { 
124                 SLAPD_MONITOR_TIME_NAME,
125                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
126                 MONITOR_F_PERSISTENT_CH,
127                 monitor_subsys_time_init,
128                 monitor_subsys_time_update,
129                 NULL,   /* create */
130                 NULL,   /* modify */
131         }, { 
132                 SLAPD_MONITOR_TLS_NAME,
133                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
134                 MONITOR_F_NONE,
135                 NULL,   /* init */
136                 NULL,   /* update */
137                 NULL,   /* create */
138                 NULL    /* modify */
139         }, { 
140                 SLAPD_MONITOR_RWW_NAME,
141                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
142                 MONITOR_F_PERSISTENT_CH,
143                 monitor_subsys_rww_init,
144                 monitor_subsys_rww_update,
145                 NULL,   /* create */
146                 NULL    /* modify */
147         }, { NULL }
148 };
149
150 #if SLAPD_MONITOR == SLAPD_MOD_DYNAMIC
151
152 int
153 init_module( int argc, char *argv[] )
154 {
155         BackendInfo bi;
156
157         memset( &bi, '\0', sizeof(bi) );
158         bi.bi_type = "monitor";
159         bi.bi_init = monitor_back_initialize;
160         backend_add( &bi );
161         return 0;
162 }
163
164 #endif /* SLAPD_MONITOR */
165
166 static struct monitorsubsys **monitor_subsys = NULL;
167
168 int
169 monitor_back_register_subsys( monitorsubsys *ms )
170 {
171         int     i = 0;
172
173         if ( monitor_subsys ) {
174                 for ( ; monitor_subsys[ i ] != NULL; i++ )
175                         /* just count'em */ ;
176         }
177
178         monitor_subsys = ch_realloc( monitor_subsys,
179                         ( 2 + i ) * sizeof( monitorsubsys * ) );
180
181         if ( monitor_subsys == NULL ) {
182                 return -1;
183         }
184
185         monitor_subsys[ i ] = ms;
186         monitor_subsys[ i + 1 ] = NULL;
187
188         return 0;
189 }
190
191 monitorsubsys *
192 monitor_back_get_subsys( const char *name )
193 {
194         if ( monitor_subsys != NULL ) {
195                 int     i;
196                 
197                 for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
198                         if ( strcasecmp( monitor_subsys[ i ]->mss_name, name ) == 0 ) {
199                                 return monitor_subsys[ i ];
200                         }
201                 }
202         }
203
204         return NULL;
205 }
206
207 monitorsubsys *
208 monitor_back_get_subsys_by_dn( struct berval *ndn, int sub )
209 {
210         if ( monitor_subsys != NULL ) {
211                 int     i;
212
213                 if ( sub ) {
214                         for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
215                                 if ( dnIsSuffix( ndn, &monitor_subsys[ i ]->mss_ndn ) ) {
216                                         return monitor_subsys[ i ];
217                                 }
218                         }
219
220                 } else {
221                         for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
222                                 if ( dn_match( ndn, &monitor_subsys[ i ]->mss_ndn ) ) {
223                                         return monitor_subsys[ i ];
224                                 }
225                         }
226                 }
227         }
228
229         return NULL;
230 }
231
232 int
233 monitor_back_initialize(
234         BackendInfo     *bi
235 )
236 {
237         monitorsubsys   *ms;
238         static char     *controls[] = {
239                 LDAP_CONTROL_MANAGEDSAIT,
240                 LDAP_CONTROL_VALUESRETURNFILTER,
241                 NULL
242         };
243
244         bi->bi_controls = controls;
245
246         bi->bi_init = 0;
247         bi->bi_open = 0;
248         bi->bi_config = monitor_back_config;
249         bi->bi_close = 0;
250         bi->bi_destroy = 0;
251
252         bi->bi_db_init = monitor_back_db_init;
253         bi->bi_db_config = monitor_back_db_config;
254         bi->bi_db_open = monitor_back_db_open;
255         bi->bi_db_close = 0;
256         bi->bi_db_destroy = monitor_back_db_destroy;
257
258         bi->bi_op_bind = monitor_back_bind;
259         bi->bi_op_unbind = 0;
260         bi->bi_op_search = monitor_back_search;
261         bi->bi_op_compare = monitor_back_compare;
262         bi->bi_op_modify = monitor_back_modify;
263         bi->bi_op_modrdn = 0;
264         bi->bi_op_add = 0;
265         bi->bi_op_delete = 0;
266         bi->bi_op_abandon = 0;
267
268         bi->bi_extended = 0;
269
270         bi->bi_entry_release_rw = 0;
271         bi->bi_chk_referrals = 0;
272         bi->bi_operational = monitor_back_operational;
273
274         /*
275          * hooks for slap tools
276          */
277         bi->bi_tool_entry_open = 0;
278         bi->bi_tool_entry_close = 0;
279         bi->bi_tool_entry_first = 0;
280         bi->bi_tool_entry_next = 0;
281         bi->bi_tool_entry_get = 0;
282         bi->bi_tool_entry_put = 0;
283         bi->bi_tool_entry_reindex = 0;
284         bi->bi_tool_sync = 0;
285         bi->bi_tool_dn2id_get = 0;
286         bi->bi_tool_id2entry_get = 0;
287         bi->bi_tool_entry_modify = 0;
288
289         bi->bi_connection_init = 0;
290         bi->bi_connection_destroy = 0;
291
292         for ( ms = known_monitor_subsys; ms->mss_name != NULL; ms++ ) {
293                 if ( monitor_back_register_subsys( ms ) ) {
294                         return -1;
295                 }
296         }
297
298         return 0;
299 }
300
301 int
302 monitor_back_db_init(
303         BackendDB       *be
304 )
305 {
306         struct monitorinfo      *mi;
307         int                     i, rc;
308         struct berval           dn, ndn;
309         struct berval           bv;
310         const char              *text;
311
312         struct m_s {
313                 char    *name;
314                 char    *schema;
315                 slap_mask_t flags;
316                 int     offset;
317         } moc[] = {
318                 { "monitor", "( 1.3.6.1.4.1.4203.666.3.2 "
319                         "NAME 'monitor' "
320                         "DESC 'OpenLDAP system monitoring' "
321                         "SUP top STRUCTURAL "
322                         "MUST cn "
323                         "MAY ( "
324                                 "description "
325                                 "$ l "
326 #if 0   /* temporarily disabled */
327                                 "$ st "
328                                 "$ street "
329                                 "$ postalAddress "
330                                 "$ postalCode "
331 #endif
332                                 "$ seeAlso "
333                                 "$ labeledURI "
334                                 "$ monitoredInfo "
335                                 "$ managedInfo "
336                                 "$ monitorOverlay "
337                         ") )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
338                         offsetof(struct monitorinfo, mi_oc_monitor) },
339                 { "monitorServer", "( 1.3.6.1.4.1.4203.666.3.7 "
340                         "NAME 'monitorServer' "
341                         "DESC 'Server monitoring root entry' "
342                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
343                         offsetof(struct monitorinfo, mi_oc_monitorServer) },
344                 { "monitorContainer", "( 1.3.6.1.4.1.4203.666.3.8 "
345                         "NAME 'monitorContainer' "
346                         "DESC 'monitor container class' "
347                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
348                         offsetof(struct monitorinfo, mi_oc_monitorContainer) },
349                 { "monitorCounterObject", "( 1.3.6.1.4.1.4203.666.3.9 "
350                         "NAME 'monitorCounterObject' "
351                         "DESC 'monitor counter class' "
352                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
353                         offsetof(struct monitorinfo, mi_oc_monitorCounterObject) },
354                 { "monitorOperation", "( 1.3.6.1.4.1.4203.666.3.10 "
355                         "NAME 'monitorOperation' "
356                         "DESC 'monitor operation class' "
357                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
358                         offsetof(struct monitorinfo, mi_oc_monitorOperation) },
359                 { "monitorConnection", "( 1.3.6.1.4.1.4203.666.3.11 "
360                         "NAME 'monitorConnection' "
361                         "DESC 'monitor connection class' "
362                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
363                         offsetof(struct monitorinfo, mi_oc_monitorConnection) },
364                 { "managedObject", "( 1.3.6.1.4.1.4203.666.3.12 "
365                         "NAME 'managedObject' "
366                         "DESC 'monitor managed entity class' "
367                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
368                         offsetof(struct monitorinfo, mi_oc_managedObject) },
369                 { "monitoredObject", "( 1.3.6.1.4.1.4203.666.3.13 "
370                         "NAME 'monitoredObject' "
371                         "DESC 'monitor monitored entity class' "
372                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
373                         offsetof(struct monitorinfo, mi_oc_monitoredObject) },
374                 { NULL, NULL, 0, -1 }
375         }, mat[] = {
376                 { "monitoredInfo", "( 1.3.6.1.4.1.4203.666.1.14 "
377                         "NAME 'monitoredInfo' "
378                         "DESC 'monitored info' "
379                         /* "SUP name " */
380                         "EQUALITY caseIgnoreMatch "
381                         "SUBSTR caseIgnoreSubstringsMatch "
382                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{32768} "
383                         "NO-USER-MODIFICATION "
384                         "USAGE directoryOperation )", SLAP_AT_HIDE,
385                         offsetof(struct monitorinfo, mi_ad_monitoredInfo) },
386                 { "managedInfo", "( 1.3.6.1.4.1.4203.666.1.15 "
387                         "NAME 'managedInfo' "
388                         "DESC 'monitor managed info' "
389                         "SUP name )", SLAP_AT_HIDE,
390                         offsetof(struct monitorinfo, mi_ad_managedInfo) },
391                 { "monitorCounter", "( 1.3.6.1.4.1.4203.666.1.16 "
392                         "NAME 'monitorCounter' "
393                         "DESC 'monitor counter' "
394                         "EQUALITY integerMatch "
395                         "ORDERING integerOrderingMatch "
396                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
397                         "NO-USER-MODIFICATION "
398                         "USAGE directoryOperation )", SLAP_AT_HIDE,
399                         offsetof(struct monitorinfo, mi_ad_monitorCounter) },
400                 { "monitorOpCompleted", "( 1.3.6.1.4.1.4203.666.1.17 "
401                         "NAME 'monitorOpCompleted' "
402                         "DESC 'monitor completed operations' "
403                         "SUP monitorCounter "
404                         "NO-USER-MODIFICATION "
405                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
406                         offsetof(struct monitorinfo, mi_ad_monitorOpCompleted) },
407                 { "monitorOpInitiated", "( 1.3.6.1.4.1.4203.666.1.18 "
408                         "NAME 'monitorOpInitiated' "
409                         "DESC 'monitor initiated operations' "
410                         "SUP monitorCounter "
411                         "NO-USER-MODIFICATION "
412                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
413                         offsetof(struct monitorinfo, mi_ad_monitorOpInitiated) },
414                 { "monitorConnectionNumber", "( 1.3.6.1.4.1.4203.666.1.19 "
415                         "NAME 'monitorConnectionNumber' "
416                         "DESC 'monitor connection number' "
417                         "SUP monitorCounter "
418                         "NO-USER-MODIFICATION "
419                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
420                         offsetof(struct monitorinfo, mi_ad_monitorConnectionNumber) },
421                 { "monitorConnectionAuthzDN", "( 1.3.6.1.4.1.4203.666.1.20 "
422                         "NAME 'monitorConnectionAuthzDN' "
423                         "DESC 'monitor connection authorization DN' "
424                         /* "SUP distinguishedName " */
425                         "EQUALITY distinguishedNameMatch "
426                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
427                         "NO-USER-MODIFICATION "
428                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
429                         offsetof(struct monitorinfo, mi_ad_monitorConnectionAuthzDN) },
430                 { "monitorConnectionLocalAddress", "( 1.3.6.1.4.1.4203.666.1.21 "
431                         "NAME 'monitorConnectionLocalAddress' "
432                         "DESC 'monitor connection local address' "
433                         "SUP monitoredInfo "
434                         "NO-USER-MODIFICATION "
435                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
436                         offsetof(struct monitorinfo, mi_ad_monitorConnectionLocalAddress) },
437                 { "monitorConnectionPeerAddress", "( 1.3.6.1.4.1.4203.666.1.22 "
438                         "NAME 'monitorConnectionPeerAddress' "
439                         "DESC 'monitor connection peer address' "
440                         "SUP monitoredInfo "
441                         "NO-USER-MODIFICATION "
442                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
443                         offsetof(struct monitorinfo, mi_ad_monitorConnectionPeerAddress) },
444                 { "monitorTimestamp", "( 1.3.6.1.4.1.4203.666.1.24 "
445                         "NAME 'monitorTimestamp' "
446                         "DESC 'monitor timestamp' "
447                         "EQUALITY generalizedTimeMatch "
448                         "ORDERING generalizedTimeOrderingMatch "
449                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
450                         "SINGLE-VALUE "
451                         "NO-USER-MODIFICATION "
452                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
453                         offsetof(struct monitorinfo, mi_ad_monitorTimestamp) },
454                 { "monitorOverlay", "( 1.3.6.1.4.1.4203.666.1.27 "
455                         "NAME 'monitorOverlay' "
456                         "DESC 'name of overlays defined for a give database' "
457                         "SUP monitoredInfo "
458                         "NO-USER-MODIFICATION "
459                         "USAGE directoryOperation )", SLAP_AT_HIDE,
460                         offsetof(struct monitorinfo, mi_ad_monitorOverlay) },
461                 { "readOnly", "( 1.3.6.1.4.1.4203.666.1.31 "
462                         "NAME 'readOnly' "
463                         "DESC 'read/write status of a given database' "
464                         "EQUALITY booleanMatch "
465                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
466                         "SINGLE-VALUE "
467                         "USAGE directoryOperation )", SLAP_AT_HIDE,
468                         offsetof(struct monitorinfo, mi_ad_readOnly) },
469                 { "restrictedOperation", "( 1.3.6.1.4.1.4203.666.1.32 "
470                         "NAME 'restrictedOperation' "
471                         "DESC 'name of restricted operation for a given database' "
472                         "SUP managedInfo )", SLAP_AT_HIDE,
473                         offsetof(struct monitorinfo, mi_ad_restrictedOperation ) },
474 #ifdef INTEGRATE_CORE_SCHEMA
475                 { NULL, NULL, 0, -1 },  /* description */
476                 { NULL, NULL, 0, -1 },  /* seeAlso */
477                 { NULL, NULL, 0, -1 },  /* l */
478                 { NULL, NULL, 0, -1 },  /* labeledURI */
479 #endif /* INTEGRATE_CORE_SCHEMA */
480                 { NULL, NULL, 0, -1 }
481         }, mat_core[] = {
482                 { "description", "( 2.5.4.13 "
483                         "NAME 'description' "
484                         "DESC 'RFC2256: descriptive information' "
485                         "EQUALITY caseIgnoreMatch "
486                         "SUBSTR caseIgnoreSubstringsMatch "
487                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{1024} )", 0,
488                         offsetof(struct monitorinfo, mi_ad_description) },
489                 { "seeAlso", "( 2.5.4.34 "
490                         "NAME 'seeAlso' "
491                         "DESC 'RFC2256: DN of related object' "
492                         "SUP distinguishedName )", 0,
493                         offsetof(struct monitorinfo, mi_ad_seeAlso) },
494                 { "l", "( 2.5.4.7 "
495                         "NAME ( 'l' 'localityName' ) "
496                         "DESC 'RFC2256: locality which this object resides in' "
497                         "SUP name )", 0,
498                         offsetof(struct monitorinfo, mi_ad_l) },
499                 { "labeledURI", "( 1.3.6.1.4.1.250.1.57 "
500                         "NAME 'labeledURI' "
501                         "DESC 'RFC2079: Uniform Resource Identifier with optional label' "
502                         "EQUALITY caseExactMatch "
503                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )", 0,
504                         offsetof(struct monitorinfo, mi_ad_labeledURI) },
505                 { NULL, NULL, 0, -1 }
506         };
507         
508         /*
509          * database monitor can be defined once only
510          */
511         if ( be_monitor ) {
512                 Debug( LDAP_DEBUG_ANY,
513                         "only one monitor backend is allowed\n", 0, 0, 0 );
514                 return( -1 );
515         }
516         be_monitor = be;
517
518         /* indicate system schema supported */
519         SLAP_BFLAGS(be) |= SLAP_BFLAG_MONITOR;
520
521         dn.bv_val = SLAPD_MONITOR_DN;
522         dn.bv_len = sizeof( SLAPD_MONITOR_DN ) - 1;
523
524         rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL );
525         if( rc != LDAP_SUCCESS ) {
526                 Debug( LDAP_DEBUG_ANY,
527                         "unable to normalize monitor DN \"%s\"\n",
528                         SLAPD_MONITOR_DN, 0, 0 );
529                 return -1;
530         }
531
532         ber_dupbv( &bv, &dn );
533         ber_bvarray_add( &be->be_suffix, &bv );
534         ber_bvarray_add( &be->be_nsuffix, &ndn );
535
536         mi = ( struct monitorinfo * )ch_calloc( sizeof( struct monitorinfo ), 1 );
537         if ( mi == NULL ) {
538                 Debug( LDAP_DEBUG_ANY,
539                         "unable to initialize monitor backend\n", 0, 0, 0 );
540                 return -1;
541         }
542
543         memset( mi, 0, sizeof( struct monitorinfo ) );
544
545         ldap_pvt_thread_mutex_init( &mi->mi_cache_mutex );
546
547         be->be_private = mi;
548         
549 #ifdef INTEGRATE_CORE_SCHEMA
550         /* prepare for schema integration */
551         for ( k = 0; mat[k].name != NULL; k++ );
552 #endif /* INTEGRATE_CORE_SCHEMA */
553
554         for ( i = 0; mat_core[i].name != NULL; i++ ) {
555                 AttributeDescription    **ad;
556                 const char              *text;
557
558                 ad = ((AttributeDescription **)&(((char *)mi)[mat_core[i].offset]));
559                 ad[0] = NULL;
560
561                 switch (slap_str2ad( mat_core[i].name, ad, &text ) ) {
562                 case LDAP_SUCCESS:
563                         break;
564
565 #ifdef INTEGRATE_CORE_SCHEMA
566                 case LDAP_UNDEFINED_TYPE:
567                         mat[k] = mat_core[i];
568                         k++;
569                         break;
570 #endif /* INTEGRATE_CORE_SCHEMA */
571
572                 default:
573                         Debug( LDAP_DEBUG_ANY,
574                                 "monitor_back_db_init: %s: %s\n",
575                                 mat_core[i].name, text, 0 );
576                         return( -1 );
577                 }
578         }
579
580         /* schema integration */
581         for ( i = 0; mat[i].name; i++ ) {
582                 LDAPAttributeType       *at;
583                 int                     code;
584                 const char              *err;
585                 AttributeDescription    **ad;
586
587                 at = ldap_str2attributetype( mat[i].schema, &code,
588                         &err, LDAP_SCHEMA_ALLOW_ALL );
589                 if ( !at ) {
590                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
591                                 "in AttributeType \"%s\" %s before %s\n",
592                                 mat[i].name, ldap_scherr2str(code), err );
593                         return -1;
594                 }
595
596                 if ( at->at_oid == NULL ) {
597                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
598                                 "null OID for attributeType \"%s\"\n",
599                                 mat[i].name, 0, 0 );
600                         return -1;
601                 }
602
603                 code = at_add(at, &err);
604                 if ( code ) {
605                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
606                                 "%s in attributeType \"%s\"\n",
607                                 scherr2str(code), mat[i].name, 0 );
608                         return -1;
609                 }
610                 ldap_memfree(at);
611
612                 ad = ((AttributeDescription **)&(((char *)mi)[mat[i].offset]));
613                 ad[0] = NULL;
614                 if ( slap_str2ad( mat[i].name, ad, &text ) ) {
615                         Debug( LDAP_DEBUG_ANY,
616                                 "monitor_back_db_init: %s\n", text, 0, 0 );
617                         return -1;
618                 }
619
620                 (*ad)->ad_type->sat_flags |= mat[i].flags;
621         }
622
623         for ( i = 0; moc[i].name; i++ ) {
624                 LDAPObjectClass         *oc;
625                 int                     code;
626                 const char              *err;
627                 ObjectClass             *Oc;
628
629                 oc = ldap_str2objectclass(moc[i].schema, &code, &err,
630                                 LDAP_SCHEMA_ALLOW_ALL );
631                 if ( !oc ) {
632                         Debug( LDAP_DEBUG_ANY,
633                                 "unable to parse monitor objectclass \"%s\": "
634                                 "%s before %s\n" , moc[i].name,
635                                 ldap_scherr2str(code), err );
636                         return -1;
637                 }
638
639                 if ( oc->oc_oid == NULL ) {
640                         Debug( LDAP_DEBUG_ANY,
641                                 "objectclass \"%s\" has no OID\n" ,
642                                 moc[i].name, 0, 0 );
643                         return -1;
644                 }
645
646                 code = oc_add(oc, 0, &err);
647                 if ( code ) {
648                         Debug( LDAP_DEBUG_ANY,
649                                 "objectclass \"%s\": %s \"%s\"\n" ,
650                                 moc[i].name, scherr2str(code), err );
651                         return -1;
652                 }
653
654                 ldap_memfree(oc);
655
656                 Oc = oc_find( moc[i].name );
657                 if ( Oc == NULL ) {
658                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
659                                         "unable to find objectClass %s "
660                                         "(just added)\n", moc[i].name, 0, 0 );
661                         return -1;
662                 }
663
664                 Oc->soc_flags |= moc[i].flags;
665
666                 ((ObjectClass **)&(((char *)mi)[moc[i].offset]))[0] = Oc;
667         }
668
669         return 0;
670 }
671
672 int
673 monitor_back_db_open(
674         BackendDB       *be
675 )
676 {
677         struct monitorinfo      *mi = (struct monitorinfo *)be->be_private;
678         struct monitorsubsys    **ms;
679         Entry                   *e, **ep;
680         struct monitorentrypriv *mp;
681         int                     i;
682         char                    buf[ BACKMONITOR_BUFSIZE ], *end_of_line;
683         struct berval           bv;
684         struct tm               *tms;
685 #ifdef HAVE_GMTIME_R
686         struct tm               tm_buf;
687 #endif
688         static char             tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
689
690         /*
691          * Start
692          */
693 #ifndef HAVE_GMTIME_R
694         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
695 #endif
696 #ifdef HACK_LOCAL_TIME
697 # ifdef HAVE_LOCALTIME_R
698         tms = localtime_r( &starttime, &tm_buf );
699 # else
700         tms = localtime( &starttime );
701 # endif /* HAVE_LOCALTIME_R */
702         lutil_localtime( tmbuf, sizeof(tmbuf), tms, -timezone );
703 #else /* !HACK_LOCAL_TIME */
704 # ifdef HAVE_GMTIME_R
705         tms = gmtime_r( &starttime, &tm_buf );
706 # else
707         tms = gmtime( &starttime );
708 # endif /* HAVE_GMTIME_R */
709         lutil_gentime( tmbuf, sizeof(tmbuf), tms );
710 #endif /* !HACK_LOCAL_TIME */
711 #ifndef HAVE_GMTIME_R
712         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
713 #endif
714
715         mi->mi_startTime.bv_val = tmbuf;
716         mi->mi_startTime.bv_len = strlen( tmbuf );
717
718         if ( BER_BVISEMPTY( &be->be_rootdn ) ) {
719                 BER_BVSTR( &mi->mi_creatorsName, SLAPD_ANONYMOUS );
720         } else {
721                 mi->mi_creatorsName = be->be_rootdn;
722         }
723
724         /*
725          * creates the "cn=Monitor" entry 
726          */
727         snprintf( buf, sizeof( buf ), 
728                 "dn: %s\n"
729                 "objectClass: %s\n"
730                 "structuralObjectClass: %s\n"
731                 "cn: Monitor\n"
732                 "%s: This subtree contains monitoring/managing objects.\n"
733                 "%s: This object contains information about this server.\n"
734 #if 0
735                 "%s: createTimestamp reflects the time this server instance was created.\n"
736                 "%s: modifyTimestamp reflects the time this server instance was last accessed.\n"
737 #endif
738                 "creatorsName: %s\n"
739                 "modifiersName: %s\n"
740                 "createTimestamp: %s\n"
741                 "modifyTimestamp: %s\n",
742                 SLAPD_MONITOR_DN,
743                 mi->mi_oc_monitorServer->soc_cname.bv_val,
744                 mi->mi_oc_monitorServer->soc_cname.bv_val,
745                 mi->mi_ad_description->ad_cname.bv_val,
746                 mi->mi_ad_description->ad_cname.bv_val,
747 #if 0
748                 mi->mi_ad_description->ad_cname.bv_val,
749                 mi->mi_ad_description->ad_cname.bv_val,
750 #endif
751                 mi->mi_creatorsName.bv_val,
752                 mi->mi_creatorsName.bv_val,
753                 mi->mi_startTime.bv_val,
754                 mi->mi_startTime.bv_val );
755
756         e = str2entry( buf );
757         if ( e == NULL) {
758                 Debug( LDAP_DEBUG_ANY,
759                         "unable to create \"%s\" entry\n",
760                         SLAPD_MONITOR_DN, 0, 0 );
761                 return( -1 );
762         }
763
764         bv.bv_val = (char *) Versionstr;
765         end_of_line = strchr( Versionstr, '\n' );
766         if ( end_of_line ) {
767                 bv.bv_len = end_of_line - Versionstr;
768         } else {
769                 bv.bv_len = strlen( Versionstr );
770         }
771
772         if ( attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo,
773                                 &bv, NULL ) ) {
774                 Debug( LDAP_DEBUG_ANY,
775                         "unable to add monitoredInfo to \"%s\" entry\n",
776                         SLAPD_MONITOR_DN, 0, 0 );
777                 return( -1 );
778         }
779
780         if ( mi->mi_l.bv_len ) {
781                 if ( attr_merge_normalize_one( e, mi->mi_ad_l, &mi->mi_l, NULL ) ) {
782                         Debug( LDAP_DEBUG_ANY,
783                                 "unable to add locality to \"%s\" entry\n",
784                                 SLAPD_MONITOR_DN, 0, 0 );
785                         return( -1 );
786                 }
787         }
788
789         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
790         e->e_private = ( void * )mp;
791
792         mp->mp_info = NULL;
793         mp->mp_children = NULL;
794         mp->mp_next = NULL;
795
796         ep = &mp->mp_children;
797
798         if ( monitor_cache_add( mi, e ) ) {
799                 Debug( LDAP_DEBUG_ANY,
800                         "unable to add entry \"%s\" to cache\n",
801                         SLAPD_MONITOR_DN, 0, 0 );
802                 return -1;
803         }
804
805         /*      
806          * Create all the subsystem specific entries
807          */
808         for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
809                 int             len = strlen( monitor_subsys[ i ]->mss_name );
810                 struct berval   dn;
811                 int             rc;
812
813                 dn.bv_len = len + sizeof( "cn=" ) - 1;
814                 dn.bv_val = ch_calloc( sizeof( char ), dn.bv_len + 1 );
815                 strcpy( dn.bv_val, "cn=" );
816                 strcat( dn.bv_val, monitor_subsys[ i ]->mss_name );
817                 rc = dnPretty( NULL, &dn, &monitor_subsys[ i ]->mss_rdn, NULL );
818                 free( dn.bv_val );
819                 if ( rc != LDAP_SUCCESS ) {
820                         Debug( LDAP_DEBUG_ANY,
821                                 "monitor RDN \"%s\" is invalid\n", 
822                                 dn.bv_val, 0, 0 );
823                         return( -1 );
824                 }
825
826                 dn.bv_len += sizeof( SLAPD_MONITOR_DN ); /* 1 for the , */
827                 dn.bv_val = ch_malloc( dn.bv_len + 1 );
828                 strcpy( dn.bv_val , monitor_subsys[ i ]->mss_rdn.bv_val );
829                 strcat( dn.bv_val, "," SLAPD_MONITOR_DN );
830                 rc = dnPrettyNormal( NULL, &dn, &monitor_subsys[ i ]->mss_dn,
831                         &monitor_subsys[ i ]->mss_ndn, NULL );
832                 free( dn.bv_val );
833                 if ( rc != LDAP_SUCCESS ) {
834                         Debug( LDAP_DEBUG_ANY,
835                                 "monitor DN \"%s\" is invalid\n", 
836                                 dn.bv_val, 0, 0 );
837                         return( -1 );
838                 }
839
840                 snprintf( buf, sizeof( buf ),
841                                 "dn: %s\n"
842                                 "objectClass: %s\n"
843                                 "structuralObjectClass: %s\n"
844                                 "cn: %s\n"
845                                 "creatorsName: %s\n"
846                                 "modifiersName: %s\n"
847                                 "createTimestamp: %s\n"
848                                 "modifyTimestamp: %s\n",
849                                 monitor_subsys[ i ]->mss_dn.bv_val,
850                                 mi->mi_oc_monitorContainer->soc_cname.bv_val,
851                                 mi->mi_oc_monitorContainer->soc_cname.bv_val,
852                                 monitor_subsys[ i ]->mss_name,
853                                 mi->mi_creatorsName.bv_val,
854                                 mi->mi_creatorsName.bv_val,
855                                 mi->mi_startTime.bv_val,
856                                 mi->mi_startTime.bv_val );
857                 
858                 e = str2entry( buf );
859                 
860                 if ( e == NULL) {
861                         Debug( LDAP_DEBUG_ANY,
862                                 "unable to create \"%s\" entry\n", 
863                                 monitor_subsys[ i ]->mss_dn.bv_val, 0, 0 );
864                         return( -1 );
865                 }
866
867                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
868                 e->e_private = ( void * )mp;
869                 mp->mp_next = NULL;
870                 mp->mp_info = monitor_subsys[ i ];
871                 mp->mp_children = NULL;
872                 mp->mp_flags = monitor_subsys[ i ]->mss_flags;
873
874                 if ( monitor_cache_add( mi, e ) ) {
875                         Debug( LDAP_DEBUG_ANY,
876                                 "unable to add entry \"%s\" to cache\n",
877                                 monitor_subsys[ i ]->mss_dn.bv_val, 0, 0 );
878                         return -1;
879                 }
880
881                 *ep = e;
882                 ep = &mp->mp_next;
883         }
884
885         assert( be );
886
887         be->be_private = mi;
888         
889         /*
890          * opens the monitor backend subsystems
891          */
892         for ( ms = monitor_subsys; ms[ 0 ] != NULL; ms++ ) {
893                 if ( ms[ 0 ]->mss_open && ( *ms[ 0 ]->mss_open )( be, ms[ 0 ] ) )
894                 {
895                         return( -1 );
896                 }
897         }
898
899         return( 0 );
900 }
901
902 int
903 monitor_back_config(
904         BackendInfo     *bi,
905         const char      *fname,
906         int             lineno,
907         int             argc,
908         char            **argv
909 )
910 {
911         /*
912          * eventually, will hold backend specific configuration parameters
913          */
914         return SLAP_CONF_UNKNOWN;
915 }
916
917 int
918 monitor_back_db_config(
919         Backend     *be,
920         const char  *fname,
921         int         lineno,
922         int         argc,
923         char        **argv
924 )
925 {
926         struct monitorinfo *mi = (struct monitorinfo *)be->be_private;
927
928         /*
929          * eventually, will hold database specific configuration parameters
930          */
931         if ( strcasecmp( argv[ 0 ], "l" ) == 0 ) {
932                 if ( argc != 2 ) {
933                         return 1;
934                 }
935                 
936                 ber_str2bv( argv[ 1 ], 0, 1, &mi->mi_l );
937
938         } else {
939                 return SLAP_CONF_UNKNOWN;
940         }
941
942         return( 0 );
943 }
944
945 int
946 monitor_back_db_destroy(
947         BackendDB       *be
948 )
949 {
950         /*
951          * FIXME: destroys all the data
952          */
953         return 0;
954 }
955