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