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