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