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