]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/init.c
Fix prev commit
[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 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                                 "$ monitorOverlay "
327                         ") )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
328                         offsetof(struct monitorinfo, mi_oc_monitor) },
329                 { "monitorServer", "( 1.3.6.1.4.1.4203.666.3.7 "
330                         "NAME 'monitorServer' "
331                         "DESC 'Server monitoring root entry' "
332                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
333                         offsetof(struct monitorinfo, mi_oc_monitorServer) },
334                 { "monitorContainer", "( 1.3.6.1.4.1.4203.666.3.8 "
335                         "NAME 'monitorContainer' "
336                         "DESC 'monitor container class' "
337                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
338                         offsetof(struct monitorinfo, mi_oc_monitorContainer) },
339                 { "monitorCounterObject", "( 1.3.6.1.4.1.4203.666.3.9 "
340                         "NAME 'monitorCounterObject' "
341                         "DESC 'monitor counter class' "
342                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
343                         offsetof(struct monitorinfo, mi_oc_monitorCounterObject) },
344                 { "monitorOperation", "( 1.3.6.1.4.1.4203.666.3.10 "
345                         "NAME 'monitorOperation' "
346                         "DESC 'monitor operation class' "
347                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
348                         offsetof(struct monitorinfo, mi_oc_monitorOperation) },
349                 { "monitorConnection", "( 1.3.6.1.4.1.4203.666.3.11 "
350                         "NAME 'monitorConnection' "
351                         "DESC 'monitor connection class' "
352                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
353                         offsetof(struct monitorinfo, mi_oc_monitorConnection) },
354                 { "managedObject", "( 1.3.6.1.4.1.4203.666.3.12 "
355                         "NAME 'managedObject' "
356                         "DESC 'monitor managed entity class' "
357                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
358                         offsetof(struct monitorinfo, mi_oc_managedObject) },
359                 { "monitoredObject", "( 1.3.6.1.4.1.4203.666.3.13 "
360                         "NAME 'monitoredObject' "
361                         "DESC 'monitor monitored entity class' "
362                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
363                         offsetof(struct monitorinfo, mi_oc_monitoredObject) },
364                 { NULL, NULL, 0, -1 }
365         }, mat[] = {
366                 { "monitoredInfo", "( 1.3.6.1.4.1.4203.666.1.14 "
367                         "NAME 'monitoredInfo' "
368                         "DESC 'monitored info' "
369                         /* "SUP name " */
370                         "EQUALITY caseIgnoreMatch "
371                         "SUBSTR caseIgnoreSubstringsMatch "
372                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{32768} "
373                         "NO-USER-MODIFICATION "
374                         "USAGE directoryOperation )", SLAP_AT_HIDE,
375                         offsetof(struct monitorinfo, mi_ad_monitoredInfo) },
376                 { "managedInfo", "( 1.3.6.1.4.1.4203.666.1.15 "
377                         "NAME 'managedInfo' "
378                         "DESC 'monitor managed info' "
379                         "SUP name )", SLAP_AT_HIDE,
380                         offsetof(struct monitorinfo, mi_ad_managedInfo) },
381                 { "monitorCounter", "( 1.3.6.1.4.1.4203.666.1.16 "
382                         "NAME 'monitorCounter' "
383                         "DESC 'monitor counter' "
384                         "EQUALITY integerMatch "
385                         "ORDERING integerOrderingMatch "
386                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
387                         "NO-USER-MODIFICATION "
388                         "USAGE directoryOperation )", SLAP_AT_HIDE,
389                         offsetof(struct monitorinfo, mi_ad_monitorCounter) },
390                 { "monitorOpCompleted", "( 1.3.6.1.4.1.4203.666.1.17 "
391                         "NAME 'monitorOpCompleted' "
392                         "DESC 'monitor completed operations' "
393                         "SUP monitorCounter "
394                         "NO-USER-MODIFICATION "
395                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
396                         offsetof(struct monitorinfo, mi_ad_monitorOpCompleted) },
397                 { "monitorOpInitiated", "( 1.3.6.1.4.1.4203.666.1.18 "
398                         "NAME 'monitorOpInitiated' "
399                         "DESC 'monitor initiated operations' "
400                         "SUP monitorCounter "
401                         "NO-USER-MODIFICATION "
402                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
403                         offsetof(struct monitorinfo, mi_ad_monitorOpInitiated) },
404                 { "monitorConnectionNumber", "( 1.3.6.1.4.1.4203.666.1.19 "
405                         "NAME 'monitorConnectionNumber' "
406                         "DESC 'monitor connection number' "
407                         "SUP monitorCounter "
408                         "NO-USER-MODIFICATION "
409                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
410                         offsetof(struct monitorinfo, mi_ad_monitorConnectionNumber) },
411                 { "monitorConnectionAuthzDN", "( 1.3.6.1.4.1.4203.666.1.20 "
412                         "NAME 'monitorConnectionAuthzDN' "
413                         "DESC 'monitor connection authorization DN' "
414                         /* "SUP distinguishedName " */
415                         "EQUALITY distinguishedNameMatch "
416                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
417                         "NO-USER-MODIFICATION "
418                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
419                         offsetof(struct monitorinfo, mi_ad_monitorConnectionAuthzDN) },
420                 { "monitorConnectionLocalAddress", "( 1.3.6.1.4.1.4203.666.1.21 "
421                         "NAME 'monitorConnectionLocalAddress' "
422                         "DESC 'monitor connection local address' "
423                         "SUP monitoredInfo "
424                         "NO-USER-MODIFICATION "
425                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
426                         offsetof(struct monitorinfo, mi_ad_monitorConnectionLocalAddress) },
427                 { "monitorConnectionPeerAddress", "( 1.3.6.1.4.1.4203.666.1.22 "
428                         "NAME 'monitorConnectionPeerAddress' "
429                         "DESC 'monitor connection peer address' "
430                         "SUP monitoredInfo "
431                         "NO-USER-MODIFICATION "
432                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
433                         offsetof(struct monitorinfo, mi_ad_monitorConnectionPeerAddress) },
434                 { "monitorTimestamp", "( 1.3.6.1.4.1.4203.666.1.24 "
435                         "NAME 'monitorTimestamp' "
436                         "DESC 'monitor timestamp' "
437                         "EQUALITY generalizedTimeMatch "
438                         "ORDERING generalizedTimeOrderingMatch "
439                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
440                         "SINGLE-VALUE "
441                         "NO-USER-MODIFICATION "
442                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
443                         offsetof(struct monitorinfo, mi_ad_monitorTimestamp) },
444                 { "monitorOverlay", "( 1.3.6.1.4.1.4203.666.1.27 "
445                         "NAME 'monitorOverlay' "
446                         "DESC 'name of overlays defined for a give database' "
447                         "SUP monitoredInfo "
448                         "NO-USER-MODIFICATION "
449                         "USAGE directoryOperation )", SLAP_AT_HIDE,
450                         offsetof(struct monitorinfo, mi_ad_monitorOverlay) },
451 #ifdef INTEGRATE_CORE_SCHEMA
452                 { NULL, NULL, 0, -1 },  /* description */
453                 { NULL, NULL, 0, -1 },  /* seeAlso */
454                 { NULL, NULL, 0, -1 },  /* l */
455 #endif /* INTEGRATE_CORE_SCHEMA */
456                 { NULL, NULL, 0, -1 }
457         }, mat_core[] = {
458                 { "description", "( 2.5.4.13 "
459                         "NAME 'description' "
460                         "DESC 'RFC2256: descriptive information' "
461                         "EQUALITY caseIgnoreMatch "
462                         "SUBSTR caseIgnoreSubstringsMatch "
463                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{1024} )", 0,
464                         offsetof(struct monitorinfo, mi_ad_description) },
465                 { "seeAlso", "( 2.5.4.34 "
466                         "NAME 'seeAlso' "
467                         "DESC 'RFC2256: DN of related object' "
468                         "SUP distinguishedName )", 0,
469                         offsetof(struct monitorinfo, mi_ad_seeAlso) },
470                 { "l", "( 2.5.4.7 "
471                         "NAME ( 'l' 'localityName' ) "
472                         "DESC 'RFC2256: locality which this object resides in' "
473                         "SUP name )", 0,
474                         offsetof(struct monitorinfo, mi_ad_l) },
475                 { NULL, NULL, 0, -1 }
476         };
477         
478         struct tm               *tms;
479 #ifdef HAVE_GMTIME_R
480         struct tm               tm_buf;
481 #endif
482         static char             tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
483
484         /*
485          * Start
486          */
487 #ifndef HAVE_GMTIME_R
488         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
489 #endif
490 #ifdef HACK_LOCAL_TIME
491 # ifdef HAVE_LOCALTIME_R
492         tms = localtime_r( &starttime, &tm_buf );
493 # else
494         tms = localtime( &starttime );
495 # endif /* HAVE_LOCALTIME_R */
496         lutil_localtime( tmbuf, sizeof(tmbuf), tms, -timezone );
497 #else /* !HACK_LOCAL_TIME */
498 # ifdef HAVE_GMTIME_R
499         tms = gmtime_r( &starttime, &tm_buf );
500 # else
501         tms = gmtime( &starttime );
502 # endif /* HAVE_GMTIME_R */
503         lutil_gentime( tmbuf, sizeof(tmbuf), tms );
504 #endif /* !HACK_LOCAL_TIME */
505 #ifndef HAVE_GMTIME_R
506         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
507 #endif
508
509         mi->mi_startTime.bv_val = tmbuf;
510         mi->mi_startTime.bv_len = strlen( tmbuf );
511
512 #ifdef INTEGRATE_CORE_SCHEMA
513         /* prepare for schema integration */
514         for ( k = 0; mat[k].name != NULL; k++ );
515 #endif /* INTEGRATE_CORE_SCHEMA */
516
517         for ( i = 0; mat_core[i].name != NULL; i++ ) {
518                 AttributeDescription    **ad;
519                 const char              *text;
520
521                 ad = ((AttributeDescription **)&(((char *)mi)[mat_core[i].offset]));
522                 ad[0] = NULL;
523
524                 switch (slap_str2ad( mat_core[i].name, ad, &text ) ) {
525                 case LDAP_SUCCESS:
526                         break;
527
528 #ifdef INTEGRATE_CORE_SCHEMA
529                 case LDAP_UNDEFINED_TYPE:
530                         mat[k] = mat_core[i];
531                         k++;
532                         break;
533 #endif /* INTEGRATE_CORE_SCHEMA */
534
535                 default:
536 #ifdef NEW_LOGGING
537                         LDAP_LOG( OPERATION, CRIT,
538                                 "monitor_back_db_init: %s: %s\n",
539                                 mat_core[i].name, text, 0 );
540 #else
541                         Debug( LDAP_DEBUG_ANY,
542                                 "monitor_back_db_init: %s: %s\n",
543                                 mat_core[i].name, text, 0 );
544 #endif
545                         return( -1 );
546                 }
547         }
548
549         /* schema integration */
550         for ( i = 0; mat[i].name; i++ ) {
551                 LDAPAttributeType       *at;
552                 int                     code;
553                 const char              *err;
554                 AttributeDescription    **ad;
555
556                 at = ldap_str2attributetype( mat[i].schema, &code,
557                         &err, LDAP_SCHEMA_ALLOW_ALL );
558                 if ( !at ) {
559 #ifdef NEW_LOGGING
560                         LDAP_LOG( OPERATION, CRIT, "monitor_back_db_init: "
561                                 "in AttributeType '%s' %s before %s\n",
562                                 mat[i].name, ldap_scherr2str(code), err );
563 #else
564                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
565                                 "in AttributeType '%s' %s before %s\n",
566                                 mat[i].name, ldap_scherr2str(code), err );
567 #endif
568                         return -1;
569                 }
570
571                 if ( at->at_oid == NULL ) {
572 #ifdef NEW_LOGGING
573                         LDAP_LOG( OPERATION, CRIT, "monitor_back_db_init: "
574                                 "null OID for attributeType '%s'\n",
575                                 mat[i].name, 0, 0 );
576 #else
577                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
578                                 "null OID for attributeType '%s'\n",
579                                 mat[i].name, 0, 0 );
580 #endif
581                         return -1;
582                 }
583
584                 code = at_add(at, &err);
585                 if ( code ) {
586 #ifdef NEW_LOGGING
587                         LDAP_LOG( OPERATION, CRIT, "monitor_back_db_init: "
588                                 "%s in attributeType '%s'\n",
589                                 scherr2str(code), mat[i].name, 0 );
590 #else
591                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
592                                 "%s in attributeType '%s'\n",
593                                 scherr2str(code), mat[i].name, 0 );
594 #endif
595                         return -1;
596                 }
597                 ldap_memfree(at);
598
599                 ad = ((AttributeDescription **)&(((char *)mi)[mat[i].offset]));
600                 ad[0] = NULL;
601                 if ( slap_str2ad( mat[i].name, ad, &text ) ) {
602 #ifdef NEW_LOGGING
603                         LDAP_LOG( OPERATION, CRIT,
604                                 "monitor_back_db_init: %s\n", text, 0, 0 );
605 #else
606                         Debug( LDAP_DEBUG_ANY,
607                                 "monitor_back_db_init: %s\n", text, 0, 0 );
608 #endif
609                         return -1;
610                 }
611
612                 (*ad)->ad_type->sat_flags |= mat[i].flags;
613         }
614
615         for ( i = 0; moc[i].name; i++ ) {
616                 LDAPObjectClass         *oc;
617                 int                     code;
618                 const char              *err;
619                 ObjectClass             *Oc;
620
621                 oc = ldap_str2objectclass(moc[i].schema, &code, &err,
622                                 LDAP_SCHEMA_ALLOW_ALL );
623                 if ( !oc ) {
624 #ifdef NEW_LOGGING
625                         LDAP_LOG( OPERATION, CRIT,
626                                 "unable to parse monitor objectclass '%s': "
627                                 "%s before %s\n" , moc[i].name,
628                                 ldap_scherr2str(code), err );
629 #else
630                         Debug( LDAP_DEBUG_ANY,
631                                 "unable to parse monitor objectclass '%s': "
632                                 "%s before %s\n" , moc[i].name,
633                                 ldap_scherr2str(code), err );
634 #endif
635                         return -1;
636                 }
637
638                 if ( oc->oc_oid == NULL ) {
639 #ifdef NEW_LOGGING
640                         LDAP_LOG( OPERATION, CRIT,
641                                 "objectclass '%s' has no OID\n" ,
642                                 moc[i].name, 0, 0 );
643 #else
644                         Debug( LDAP_DEBUG_ANY,
645                                 "objectclass '%s' has no OID\n" ,
646                                 moc[i].name, 0, 0 );
647 #endif
648                         return -1;
649                 }
650
651                 code = oc_add(oc, 0, &err);
652                 if ( code ) {
653 #ifdef NEW_LOGGING
654                         LDAP_LOG( OPERATION, CRIT,
655                                 "objectclass '%s': %s \"%s\"\n" ,
656                                 moc[i].name, scherr2str(code), err );
657 #else
658                         Debug( LDAP_DEBUG_ANY,
659                                 "objectclass '%s': %s \"%s\"\n" ,
660                                 moc[i].name, scherr2str(code), err );
661 #endif
662                         return -1;
663                 }
664
665                 ldap_memfree(oc);
666
667                 Oc = oc_find( moc[i].name );
668                 if ( Oc == NULL ) {
669 #ifdef NEW_LOGGING
670                         LDAP_LOG( OPERATION, CRIT, "monitor_back_db_init: "
671                                         "unable to find objectClass %s "
672                                         "(just added)\n", moc[i].name, 0, 0 );
673 #else
674                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
675                                         "unable to find objectClass %s "
676                                         "(just added)\n", moc[i].name, 0, 0 );
677 #endif
678                         return -1;
679                 }
680
681                 Oc->soc_flags |= moc[i].flags;
682
683                 ((ObjectClass **)&(((char *)mi)[moc[i].offset]))[0] = Oc;
684         }
685
686         /*      
687          * Create all the subsystem specific entries
688          */
689         e_tmp = NULL;
690         for ( i = 0; monitor_subsys[ i ].mss_name != NULL; i++ ) {
691                 int             len = strlen( monitor_subsys[ i ].mss_name );
692                 struct berval   dn;
693                 int             rc;
694
695                 dn.bv_len = len + sizeof( "cn=" ) - 1;
696                 dn.bv_val = ch_calloc( sizeof( char ), dn.bv_len + 1 );
697                 strcpy( dn.bv_val, "cn=" );
698                 strcat( dn.bv_val, monitor_subsys[ i ].mss_name );
699                 rc = dnPretty( NULL, &dn, &monitor_subsys[ i ].mss_rdn, NULL );
700                 free( dn.bv_val );
701                 if ( rc != LDAP_SUCCESS ) {
702 #ifdef NEW_LOGGING
703                         LDAP_LOG( OPERATION, CRIT,
704                                 "monitor RDN \"%s\" is invalid\n", 
705                                 dn.bv_val, 0, 0 );
706 #else
707                         Debug( LDAP_DEBUG_ANY,
708                                 "monitor RDN \"%s\" is invalid\n", 
709                                 dn.bv_val, 0, 0 );
710 #endif
711                         return( -1 );
712                 }
713
714                 dn.bv_len += sizeof( SLAPD_MONITOR_DN ); /* 1 for the , */
715                 dn.bv_val = ch_malloc( dn.bv_len + 1 );
716                 strcpy( dn.bv_val , monitor_subsys[ i ].mss_rdn.bv_val );
717                 strcat( dn.bv_val, "," SLAPD_MONITOR_DN );
718                 rc = dnPrettyNormal( NULL, &dn, &monitor_subsys[ i ].mss_dn,
719                         &monitor_subsys[ i ].mss_ndn, NULL );
720                 free( dn.bv_val );
721                 if ( rc != LDAP_SUCCESS ) {
722 #ifdef NEW_LOGGING
723                         LDAP_LOG( OPERATION, CRIT,
724                                 "monitor DN \"%s\" is invalid\n", 
725                                 dn.bv_val, 0, 0 );
726 #else
727                         Debug( LDAP_DEBUG_ANY,
728                                 "monitor DN \"%s\" is invalid\n", 
729                                 dn.bv_val, 0, 0 );
730 #endif
731                         return( -1 );
732                 }
733
734                 snprintf( buf, sizeof( buf ),
735                                 "dn: %s\n"
736                                 "objectClass: %s\n"
737                                 "structuralObjectClass: %s\n"
738                                 "cn: %s\n"
739                                 "createTimestamp: %s\n"
740                                 "modifyTimestamp: %s\n",
741                                 monitor_subsys[ i ].mss_dn.bv_val,
742                                 mi->mi_oc_monitorContainer->soc_cname.bv_val,
743                                 mi->mi_oc_monitorContainer->soc_cname.bv_val,
744                                 monitor_subsys[ i ].mss_name,
745                                 mi->mi_startTime.bv_val,
746                                 mi->mi_startTime.bv_val );
747                 
748                 e = str2entry( buf );
749                 
750                 if ( e == NULL) {
751 #ifdef NEW_LOGGING
752                         LDAP_LOG( OPERATION, CRIT,
753                                 "unable to create '%s' entry\n", 
754                                 monitor_subsys[ i ].mss_dn.bv_val, 0, 0 );
755 #else
756                         Debug( LDAP_DEBUG_ANY,
757                                 "unable to create '%s' entry\n", 
758                                 monitor_subsys[ i ].mss_dn.bv_val, 0, 0 );
759 #endif
760                         return( -1 );
761                 }
762
763                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
764                 e->e_private = ( void * )mp;
765                 mp->mp_info = &monitor_subsys[ i ];
766                 mp->mp_children = NULL;
767                 mp->mp_next = e_tmp;
768                 mp->mp_flags = monitor_subsys[ i ].mss_flags;
769
770                 if ( monitor_cache_add( mi, e ) ) {
771 #ifdef NEW_LOGGING
772                         LDAP_LOG( OPERATION, CRIT,
773                                 "unable to add entry '%s' to cache\n",
774                                 monitor_subsys[ i ].mss_dn.bv_val, 0, 0 );
775 #else
776                         Debug( LDAP_DEBUG_ANY,
777                                 "unable to add entry '%s' to cache\n",
778                                 monitor_subsys[ i ].mss_dn.bv_val, 0, 0 );
779 #endif
780                         return -1;
781                 }
782
783                 e_tmp = e;
784         }
785
786         /*
787          * creates the "cn=Monitor" entry 
788          */
789         snprintf( buf, sizeof( buf ), 
790                 "dn: %s\n"
791                 "objectClass: %s\n"
792                 "structuralObjectClass: %s\n"
793                 "cn: Monitor\n"
794                 "%s: This subtree contains monitoring/managing objects.\n"
795                 "%s: This object contains information about this server.\n"
796 #if 0
797                 "%s: createTimestamp reflects the time this server instance was created.\n"
798                 "%s: modifyTimestamp reflects the time this server instance was last accessed.\n"
799 #endif
800                 "createTimestamp: %s\n"
801                 "modifyTimestamp: %s\n",
802                 SLAPD_MONITOR_DN,
803                 mi->mi_oc_monitorServer->soc_cname.bv_val,
804                 mi->mi_oc_monitorServer->soc_cname.bv_val,
805                 mi->mi_ad_description->ad_cname.bv_val,
806                 mi->mi_ad_description->ad_cname.bv_val,
807 #if 0
808                 mi->mi_ad_description->ad_cname.bv_val,
809                 mi->mi_ad_description->ad_cname.bv_val,
810 #endif
811                 mi->mi_startTime.bv_val,
812                 mi->mi_startTime.bv_val );
813
814         e = str2entry( buf );
815         if ( e == NULL) {
816 #ifdef NEW_LOGGING
817                 LDAP_LOG( OPERATION, CRIT,
818                         "unable to create '%s' entry\n",
819                         SLAPD_MONITOR_DN, 0, 0 );
820 #else
821                 Debug( LDAP_DEBUG_ANY,
822                         "unable to create '%s' entry\n",
823                         SLAPD_MONITOR_DN, 0, 0 );
824 #endif
825                 return( -1 );
826         }
827
828         bv.bv_val = (char *) Versionstr;
829         end_of_line = strchr( Versionstr, '\n' );
830         if ( end_of_line ) {
831                 bv.bv_len = end_of_line - Versionstr;
832         } else {
833                 bv.bv_len = strlen( Versionstr );
834         }
835
836         if ( attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo,
837                                 &bv, NULL ) ) {
838 #ifdef NEW_LOGGING
839                 LDAP_LOG( OPERATION, CRIT,
840                         "unable to add monitoredInfo to '%s' entry\n",
841                         SLAPD_MONITOR_DN, 0, 0 );
842 #else
843                 Debug( LDAP_DEBUG_ANY,
844                         "unable to add monitoredInfo to '%s' entry\n",
845                         SLAPD_MONITOR_DN, 0, 0 );
846 #endif
847                 return( -1 );
848         }
849
850         if ( mi->mi_l.bv_len ) {
851                 if ( attr_merge_normalize_one( e, mi->mi_ad_l, &mi->mi_l, NULL ) ) {
852 #ifdef NEW_LOGGING
853                         LDAP_LOG( OPERATION, CRIT,
854                                 "unable to add locality to '%s' entry\n",
855                                 SLAPD_MONITOR_DN, 0, 0 );
856 #else
857                         Debug( LDAP_DEBUG_ANY,
858                                 "unable to add locality to '%s' entry\n",
859                                 SLAPD_MONITOR_DN, 0, 0 );
860 #endif
861                         return( -1 );
862                 }
863         }
864
865         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
866         e->e_private = ( void * )mp;
867
868         mp->mp_info = NULL;
869         mp->mp_children = e_tmp;
870         mp->mp_next = NULL;
871
872         if ( monitor_cache_add( mi, e ) ) {
873 #ifdef NEW_LOGGING
874                 LDAP_LOG( OPERATION, CRIT,
875                         "unable to add entry '%s' to cache\n",
876                         SLAPD_MONITOR_DN, 0, 0 );
877 #else
878                 Debug( LDAP_DEBUG_ANY,
879                         "unable to add entry '%s' to cache\n",
880                         SLAPD_MONITOR_DN, 0, 0 );
881 #endif
882                 return -1;
883         }
884
885         be->be_private = mi;
886         
887         assert( be );
888
889         /*
890          * opens the monitor backend
891          */
892         for ( ms = monitor_subsys; ms->mss_name != NULL; ms++ ) {
893                 if ( ms->mss_init && ( *ms->mss_init )( be ) ) {
894                         return( -1 );
895                 }
896         }
897
898         return( 0 );
899 }
900
901 int
902 monitor_back_config(
903         BackendInfo     *bi,
904         const char      *fname,
905         int             lineno,
906         int             argc,
907         char            **argv
908 )
909 {
910         /*
911          * eventually, will hold backend specific configuration parameters
912          */
913         return SLAP_CONF_UNKNOWN;
914 }
915
916 int
917 monitor_back_db_config(
918         Backend     *be,
919         const char  *fname,
920         int         lineno,
921         int         argc,
922         char        **argv
923 )
924 {
925         struct monitorinfo *mi = (struct monitorinfo *)be->be_private;
926
927         /*
928          * eventually, will hold database specific configuration parameters
929          */
930         if ( strcasecmp( argv[ 0 ], "l" ) == 0 ) {
931                 if ( argc != 2 ) {
932                         return 1;
933                 }
934                 
935                 ber_str2bv( argv[ 1 ], 0, 1, &mi->mi_l );
936
937         } else {
938                 return SLAP_CONF_UNKNOWN;
939         }
940
941         return( 0 );
942 }
943
944 int
945 monitor_back_db_destroy(
946         BackendDB       *be
947 )
948 {
949         /*
950          * FIXME: destroys all the data
951          */
952         return 0;
953 }
954