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