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