]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/init.c
Suck in latest changes from HEAD
[openldap] / servers / slapd / back-monitor / init.c
1 /* init.c - initialize monitor backend */
2 /*
3  * Copyright 1998-2002 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 "slap.h"
40 #include "back-monitor.h"
41
42 /*
43  * used by many functions to add description to entries
44  */
45 AttributeDescription *monitor_ad_desc = NULL;
46 BackendDB *be_monitor = NULL;
47
48 /*
49  * subsystem data
50  */
51 struct monitorsubsys monitor_subsys[] = {
52         { 
53                 SLAPD_MONITOR_LISTENER, SLAPD_MONITOR_LISTENER_NAME,    
54                 { 0L, NULL }, { 0L, NULL }, { 0L, NULL },
55                 MONITOR_F_PERSISTENT_CH,
56                 monitor_subsys_listener_init,
57                 NULL,   /* update */
58                 NULL,   /* create */
59                 NULL    /* modify */
60         }, { 
61                 SLAPD_MONITOR_DATABASE, SLAPD_MONITOR_DATABASE_NAME,    
62                 { 0L, NULL }, { 0L, NULL }, { 0L, NULL },
63                 MONITOR_F_PERSISTENT_CH,
64                 monitor_subsys_database_init,
65                 NULL,   /* update */
66                 NULL,   /* create */
67                 NULL    /* modify */
68         }, { 
69                 SLAPD_MONITOR_BACKEND, SLAPD_MONITOR_BACKEND_NAME, 
70                 { 0L, NULL }, { 0L, NULL }, { 0L, NULL },
71                 MONITOR_F_PERSISTENT_CH,
72                 monitor_subsys_backend_init,
73                 NULL,   /* update */
74                 NULL,   /* create */
75                 NULL    /* modify */
76         }, { 
77                 SLAPD_MONITOR_THREAD, SLAPD_MONITOR_THREAD_NAME,        
78                 { 0L, NULL }, { 0L, NULL }, { 0L, NULL },
79                 MONITOR_F_NONE,
80                 monitor_subsys_thread_init,
81                 monitor_subsys_thread_update,
82                 NULL,   /* create */
83                 NULL    /* modify */
84         }, { 
85                 SLAPD_MONITOR_SASL, SLAPD_MONITOR_SASL_NAME,    
86                 { 0L, NULL }, { 0L, NULL }, { 0L, NULL },
87                 MONITOR_F_NONE,
88                 NULL,   /* init */
89                 NULL,   /* update */
90                 NULL,   /* create */
91                 NULL    /* modify */
92         }, { 
93                 SLAPD_MONITOR_TLS, SLAPD_MONITOR_TLS_NAME,
94                 { 0L, NULL }, { 0L, NULL }, { 0L, NULL },
95                 MONITOR_F_NONE,
96                 NULL,   /* init */
97                 NULL,   /* update */
98                 NULL,   /* create */
99                 NULL    /* modify */
100         }, { 
101                 SLAPD_MONITOR_CONN, SLAPD_MONITOR_CONN_NAME,
102                 { 0L, NULL }, { 0L, NULL }, { 0L, NULL },
103                 MONITOR_F_VOLATILE_CH,
104                 monitor_subsys_conn_init,
105                 monitor_subsys_conn_update,
106                 monitor_subsys_conn_create,
107                 NULL    /* modify */
108         }, { 
109                 SLAPD_MONITOR_READW, SLAPD_MONITOR_READW_NAME,
110                 { 0L, NULL }, { 0L, NULL }, { 0L, NULL },
111                 MONITOR_F_NONE,
112                 NULL,   /* init */
113                 monitor_subsys_readw_update,
114                 NULL,   /* create */
115                 NULL    /* modify */
116         }, { 
117                 SLAPD_MONITOR_WRITEW, SLAPD_MONITOR_WRITEW_NAME,
118                 { 0L, NULL }, { 0L, NULL }, { 0L, NULL },
119                 MONITOR_F_NONE,
120                 NULL,   /* init */
121                 monitor_subsys_writew_update,
122                 NULL,   /* create */
123                 NULL    /* modify */
124         }, { 
125                 SLAPD_MONITOR_LOG, SLAPD_MONITOR_LOG_NAME,
126                 { 0L, NULL }, { 0L, NULL }, { 0L, NULL },
127                 MONITOR_F_NONE,
128                 monitor_subsys_log_init,
129                 NULL,   /* update */
130                 NULL,   /* create */
131                 monitor_subsys_log_modify
132         }, { 
133                 SLAPD_MONITOR_OPS, SLAPD_MONITOR_OPS_NAME,
134                 { 0L, NULL }, { 0L, NULL }, { 0L, NULL },
135                 MONITOR_F_PERSISTENT_CH,
136                 monitor_subsys_ops_init,
137                 monitor_subsys_ops_update,
138                 NULL,   /* create */
139                 NULL,   /* modify */
140         }, { 
141                 SLAPD_MONITOR_SENT, SLAPD_MONITOR_SENT_NAME,
142                 { 0L, NULL }, { 0L, NULL }, { 0L, NULL },
143                 MONITOR_F_PERSISTENT_CH,
144                 monitor_subsys_sent_init,
145                 monitor_subsys_sent_update,
146                 NULL,   /* create */
147                 NULL,   /* modify */
148         }, { 
149                 SLAPD_MONITOR_TIME, SLAPD_MONITOR_TIME_NAME,
150                 { 0L, NULL }, { 0L, NULL }, { 0L, NULL },
151                 MONITOR_F_PERSISTENT_CH,
152                 monitor_subsys_time_init,
153                 monitor_subsys_time_update,
154                 NULL,   /* create */
155                 NULL,   /* modify */
156         }, { -1, NULL }
157 };
158
159 int
160 monitor_back_initialize(
161         BackendInfo     *bi
162 )
163 {
164         static char *controls[] = {
165                 LDAP_CONTROL_MANAGEDSAIT,
166                 LDAP_CONTROL_VALUESRETURNFILTER,
167                 NULL
168         };
169
170         bi->bi_controls = controls;
171
172         bi->bi_init = 0;
173         bi->bi_open = monitor_back_open;
174         bi->bi_config = monitor_back_config;
175         bi->bi_close = 0;
176         bi->bi_destroy = 0;
177
178         bi->bi_db_init = monitor_back_db_init;
179         bi->bi_db_config = monitor_back_db_config;
180         bi->bi_db_open = 0;
181         bi->bi_db_close = 0;
182         bi->bi_db_destroy = monitor_back_db_destroy;
183
184         bi->bi_op_bind = monitor_back_bind;
185         bi->bi_op_unbind = 0;
186         bi->bi_op_search = monitor_back_search;
187         bi->bi_op_compare = monitor_back_compare;
188         bi->bi_op_modify = monitor_back_modify;
189         bi->bi_op_modrdn = 0;
190         bi->bi_op_add = 0;
191         bi->bi_op_delete = 0;
192         bi->bi_op_abandon = 0;
193
194         bi->bi_extended = 0;
195
196         bi->bi_entry_release_rw = 0;
197         bi->bi_acl_group = 0;
198         bi->bi_acl_attribute = 0;
199         bi->bi_chk_referrals = 0;
200         bi->bi_operational = monitor_back_operational;
201
202         /*
203          * hooks for slap tools
204          */
205         bi->bi_tool_entry_open = 0;
206         bi->bi_tool_entry_close = 0;
207         bi->bi_tool_entry_first = 0;
208         bi->bi_tool_entry_next = 0;
209         bi->bi_tool_entry_get = 0;
210         bi->bi_tool_entry_put = 0;
211         bi->bi_tool_entry_reindex = 0;
212         bi->bi_tool_sync = 0;
213
214         bi->bi_connection_init = 0;
215         bi->bi_connection_destroy = 0;
216
217         return 0;
218 }
219
220 int
221 monitor_back_db_init(
222         BackendDB       *be
223 )
224 {
225         struct monitorinfo      *mi;
226         Entry                   *e, *e_tmp;
227         struct monitorentrypriv *mp;
228         int                     i, rc;
229         char                    buf[1024], *end_of_line;
230         struct berval           dn, ndn;
231         const char              *text;
232         struct berval           bv[2];
233
234         /*
235          * database monitor can be defined once only
236          */
237         if ( be_monitor ) {
238 #ifdef NEW_LOGGING
239                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
240                         "only one monitor backend is allowed\n" ));
241 #else
242                 Debug( LDAP_DEBUG_ANY,
243                         "only one monitor backend is allowed\n%s%s%s",
244                         "", "", "" );
245 #endif
246                 return( -1 );
247         }
248         be_monitor = be;
249
250         /* indicate system schema supported */
251         be->be_flags |= SLAP_BFLAG_MONITOR;
252
253         dn.bv_val = SLAPD_MONITOR_DN;
254         dn.bv_len = sizeof( SLAPD_MONITOR_DN ) - 1;
255
256         rc = dnNormalize2( NULL, &dn, &ndn );
257         if( rc != LDAP_SUCCESS ) {
258 #ifdef NEW_LOGGING
259                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
260                         "monitor DN \"" SLAPD_MONITOR_DN "\" backend is allowed\n" ));
261 #else
262                 Debug( LDAP_DEBUG_ANY,
263                         "monitor DN \"" SLAPD_MONITOR_DN "\" backend is allowed\n",
264                         0, 0, 0 );
265 #endif
266                 return -1;
267         }
268
269         ber_dupbv( &bv[0], &dn );
270         ber_bvarray_add( &be->be_suffix, &bv[0] );
271         ber_bvarray_add( &be->be_nsuffix, &ndn );
272
273         mi = ( struct monitorinfo * )ch_calloc( sizeof( struct monitorinfo ), 1 );
274         ldap_pvt_thread_mutex_init( &mi->mi_cache_mutex );
275
276         if ( slap_str2ad( "description", &monitor_ad_desc, &text ) ) {
277 #ifdef NEW_LOGGING
278                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
279                         "monitor_back_db_init: %s\n", text ));
280 #else
281                 Debug( LDAP_DEBUG_ANY,
282                         "monitor_subsys_backend_init: %s\n%s%s", 
283                         text, "", "" );
284 #endif
285                 return( -1 );
286         }
287
288         /*      
289          * Create all the subsystem specific entries
290          */
291         e_tmp = NULL;
292         for ( i = 0; monitor_subsys[ i ].mss_name != NULL; i++ ) {
293                 int             len = strlen( monitor_subsys[ i ].mss_name );
294                 struct berval   dn;
295                 int             rc;
296
297                 dn.bv_len = len + sizeof( "cn=" ) - 1;
298                 dn.bv_val = ch_calloc( sizeof( char ), dn.bv_len + 1 );
299                 strcpy( dn.bv_val, "cn=" );
300                 strcat( dn.bv_val, monitor_subsys[ i ].mss_name );
301                 rc = dnPretty2( NULL, &dn, &monitor_subsys[ i ].mss_rdn );
302                 free( dn.bv_val );
303                 if ( rc != LDAP_SUCCESS ) {
304 #ifdef NEW_LOGGING
305                         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
306                                 "monitor RDN \"%s\" is invalid\n", 
307                                 dn.bv_val ));
308 #else
309                         Debug( LDAP_DEBUG_ANY,
310                                 "monitor RDN \"%s\" is invalid\n", 
311                                 dn.bv_val, 0, 0 );
312 #endif
313                         return( -1 );
314                 }
315
316                 dn.bv_len += sizeof( SLAPD_MONITOR_DN ); /* 1 for the , */
317                 dn.bv_val = ch_malloc( dn.bv_len + 1 );
318                 strcpy( dn.bv_val , monitor_subsys[ i ].mss_rdn.bv_val );
319                 strcat( dn.bv_val, "," SLAPD_MONITOR_DN );
320                 rc = dnPrettyNormal( NULL, &dn, &monitor_subsys[ i ].mss_dn,
321                         &monitor_subsys[ i ].mss_ndn );
322                 free( dn.bv_val );
323                 if ( rc != LDAP_SUCCESS ) {
324 #ifdef NEW_LOGGING
325                         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
326                                 "monitor DN \"%s\" is invalid\n", 
327                                 dn.bv_val ));
328 #else
329                         Debug( LDAP_DEBUG_ANY,
330                                 "monitor DN \"%s\" is invalid\n", 
331                                 dn.bv_val, 0, 0 );
332 #endif
333                         return( -1 );
334                 }
335
336                 snprintf( buf, sizeof( buf ),
337                                 "dn: %s\n"
338                                 SLAPD_MONITOR_OBJECTCLASSES 
339                                 "cn: %s\n",
340                                 monitor_subsys[ i ].mss_dn.bv_val,
341                                 monitor_subsys[ i ].mss_name );
342                 
343                 e = str2entry( buf );
344                 
345                 if ( e == NULL) {
346 #ifdef NEW_LOGGING
347                         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
348                                 "unable to create '%s' entry\n", 
349                                 monitor_subsys[ i ].mss_dn.bv_val ));
350 #else
351                         Debug( LDAP_DEBUG_ANY,
352                                 "unable to create '%s' entry\n", 
353                                 monitor_subsys[ i ].mss_dn.bv_val, 0, 0 );
354 #endif
355                         return( -1 );
356                 }
357
358                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
359                 e->e_private = ( void * )mp;
360                 mp->mp_info = &monitor_subsys[ i ];
361                 mp->mp_children = NULL;
362                 mp->mp_next = e_tmp;
363                 mp->mp_flags = monitor_subsys[ i ].mss_flags;
364
365                 if ( monitor_cache_add( mi, e ) ) {
366 #ifdef NEW_LOGGING
367                         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
368                                 "unable to add entry '%s' to cache\n",
369                                 monitor_subsys[ i ].mss_dn.bv_val ));
370 #else
371                         Debug( LDAP_DEBUG_ANY,
372                                 "unable to add entry '%s' to cache\n",
373                                 monitor_subsys[ i ].mss_dn.bv_val, 0, 0 );
374 #endif
375                         return -1;
376                 }
377
378                 e_tmp = e;
379         }
380
381         /*
382          * creates the "cn=Monitor" entry 
383          */
384         snprintf( buf, sizeof( buf ), 
385                         "dn: " SLAPD_MONITOR_DN "\n"
386                         "objectClass: top\n"
387                         "objectClass: monitor\n"
388                         "objectClass: extensibleObject\n"
389                         "structuralObjectClass: monitor\n"
390                         "cn: Monitor" );
391
392         e = str2entry( buf );
393         if ( e == NULL) {
394 #ifdef NEW_LOGGING
395                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
396                         "unable to create '%s' entry\n",
397                         SLAPD_MONITOR_DN ));
398 #else
399                 Debug( LDAP_DEBUG_ANY,
400                         "unable to create '%s' entry\n%s%s",
401                         SLAPD_MONITOR_DN, "", "" );
402 #endif
403                 return( -1 );
404         }
405         bv[1].bv_val = NULL;
406         bv[0].bv_val = (char *) Versionstr;
407         end_of_line = strchr( Versionstr, '\n' );
408         if ( end_of_line ) {
409                 bv[0].bv_len = end_of_line - Versionstr;
410         } else {
411                 bv[0].bv_len = strlen( Versionstr );
412         }
413         if ( attr_merge( e, monitor_ad_desc, bv ) ) {
414 #ifdef NEW_LOGGING
415                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
416                         "unable to add description to '%s' entry\n",
417                         SLAPD_MONITOR_DN ));
418 #else
419                 Debug( LDAP_DEBUG_ANY,
420                         "unable to add description to '%s' entry\n%s%s",
421                         SLAPD_MONITOR_DN, "", "" );
422 #endif
423                 return( -1 );
424         }
425
426         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
427         e->e_private = ( void * )mp;
428
429         mp->mp_info = NULL;
430         mp->mp_children = e_tmp;
431         mp->mp_next = NULL;
432
433         if ( monitor_cache_add( mi, e ) ) {
434 #ifdef NEW_LOGGING
435                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
436                         "unable to add entry '%s' to cache\n",
437                         SLAPD_MONITOR_DN ));
438 #else
439                 Debug( LDAP_DEBUG_ANY,
440                         "unable to add entry '%s' to cache\n%s%s",
441                         SLAPD_MONITOR_DN, "", "" );
442 #endif
443                 return -1;
444         }
445
446         be->be_private = mi;
447         
448         return 0;
449 }
450
451 int
452 monitor_back_open(
453         BackendInfo     *bi
454 )
455 {
456         BackendDB               *be;
457         struct monitorsubsys    *ms;
458         struct berval dn = { sizeof(SLAPD_MONITOR_DN)-1, SLAPD_MONITOR_DN };
459         struct berval ndn;
460         int rc;
461
462         /*
463          * adds the monitor backend
464          */
465         rc = dnNormalize2( NULL, &dn, &ndn );
466         if( rc != LDAP_SUCCESS ) {
467 #ifdef NEW_LOGGING
468                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
469                         "monitor DN \"" SLAPD_MONITOR_DN "\" is invalid\n" ));
470 #else
471                 Debug( LDAP_DEBUG_ANY,
472                         "monitor DN \"" SLAPD_MONITOR_DN "\" is invalid\n",
473                         0, 0, 0 );
474 #endif
475                 return( -1 );
476         }
477
478         be = select_backend( &ndn , 0, 0 );
479         free( ndn.bv_val );
480
481         if ( be == NULL ) {
482 #ifdef NEW_LOGGING
483                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
484                         "unable to get monitor backend\n" ));
485 #else
486                 Debug( LDAP_DEBUG_ANY,
487                         "unable to get monitor backend\n", 0, 0, 0 );
488 #endif
489                 return( -1 );
490         }
491
492         for ( ms = monitor_subsys; ms->mss_name != NULL; ms++ ) {
493                 if ( ms->mss_init && ( *ms->mss_init )( be ) ) {
494                         return( -1 );
495                 }
496         }
497
498         return( 0 );
499 }
500
501 int
502 monitor_back_config(
503         BackendInfo     *bi,
504         const char      *fname,
505         int             lineno,
506         int             argc,
507         char            **argv
508 )
509 {
510         /*
511          * eventually, will hold backend specific configuration parameters
512          */
513         return 0;
514 }
515
516 int
517 monitor_back_db_config(
518         Backend     *be,
519         const char  *fname,
520         int         lineno,
521         int         argc,
522         char        **argv
523 )
524 {
525 #ifdef NEW_LOGGING
526         LDAP_LOG(( "config", LDAP_LEVEL_INFO,
527                 "line %d of file '%s' will be ignored\n", lineno, fname ));
528 #else
529         Debug( LDAP_DEBUG_CONFIG, 
530                 "line %d of file '%s' will be ignored\n%s", lineno, fname, "" );
531 #endif
532         return( 0 );
533 }
534
535 int
536 monitor_back_db_destroy(
537         BackendDB       *be
538 )
539 {
540         /*
541          * FIXME: destroys all the data
542          */
543         return 0;
544 }
545