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