]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/init.c
listener monitor
[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_NONE,
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_NONE,
144                 monitor_subsys_sent_init,
145                 monitor_subsys_sent_update,
146                 NULL,   /* create */
147                 NULL,   /* modify */
148         }, { -1, NULL }
149 };
150
151 int
152 monitor_back_initialize(
153         BackendInfo     *bi
154 )
155 {
156         static char *controls[] = {
157                 LDAP_CONTROL_MANAGEDSAIT,
158                 NULL
159         };
160
161         bi->bi_controls = controls;
162
163         bi->bi_init = 0;
164         bi->bi_open = monitor_back_open;
165         bi->bi_config = monitor_back_config;
166         bi->bi_close = 0;
167         bi->bi_destroy = 0;
168
169         bi->bi_db_init = monitor_back_db_init;
170         bi->bi_db_config = monitor_back_db_config;
171         bi->bi_db_open = 0;
172         bi->bi_db_close = 0;
173         bi->bi_db_destroy = monitor_back_db_destroy;
174
175         bi->bi_op_bind = monitor_back_bind;
176         bi->bi_op_unbind = 0;
177         bi->bi_op_search = monitor_back_search;
178         bi->bi_op_compare = monitor_back_compare;
179         bi->bi_op_modify = monitor_back_modify;
180         bi->bi_op_modrdn = 0;
181         bi->bi_op_add = 0;
182         bi->bi_op_delete = 0;
183         bi->bi_op_abandon = 0;
184
185         bi->bi_extended = 0;
186
187         bi->bi_entry_release_rw = 0;
188         bi->bi_acl_group = 0;
189         bi->bi_acl_attribute = 0;
190         bi->bi_chk_referrals = 0;
191         bi->bi_operational = monitor_back_operational;
192
193         /*
194          * hooks for slap tools
195          */
196         bi->bi_tool_entry_open = 0;
197         bi->bi_tool_entry_close = 0;
198         bi->bi_tool_entry_first = 0;
199         bi->bi_tool_entry_next = 0;
200         bi->bi_tool_entry_get = 0;
201         bi->bi_tool_entry_put = 0;
202         bi->bi_tool_entry_reindex = 0;
203         bi->bi_tool_sync = 0;
204
205         bi->bi_connection_init = 0;
206         bi->bi_connection_destroy = 0;
207
208         return 0;
209 }
210
211 int
212 monitor_back_db_init(
213         BackendDB       *be
214 )
215 {
216         struct monitorinfo      *mi;
217         Entry                   *e, *e_tmp;
218         struct monitorentrypriv *mp;
219         int                     i, rc;
220         char                    buf[1024], *end_of_line;
221         struct berval           dn, *ndn;
222         const char              *text;
223         struct berval           bv[2];
224
225         /*
226          * database monitor can be defined once only
227          */
228         if ( be_monitor ) {
229 #ifdef NEW_LOGGING
230                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
231                         "only one monitor backend is allowed\n" ));
232 #else
233                 Debug( LDAP_DEBUG_ANY,
234                         "only one monitor backend is allowed\n%s%s%s",
235                         "", "", "" );
236 #endif
237                 return( -1 );
238         }
239         be_monitor = be;
240
241         ndn = NULL;
242         dn.bv_val = SLAPD_MONITOR_DN;
243         dn.bv_len = sizeof( SLAPD_MONITOR_DN ) - 1;
244
245         rc = dnNormalize( NULL, &dn, &ndn );
246         if( rc != LDAP_SUCCESS ) {
247 #ifdef NEW_LOGGING
248                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
249                         "monitor DN \"" SLAPD_MONITOR_DN "\" backend is allowed\n" ));
250 #else
251                 Debug( LDAP_DEBUG_ANY,
252                         "monitor DN \"" SLAPD_MONITOR_DN "\" backend is allowed\n",
253                         0, 0, 0 );
254 #endif
255                 return -1;
256         }
257
258         ber_bvecadd( &be->be_suffix, ber_dupbv( NULL, &dn ) );
259         ber_bvecadd( &be->be_nsuffix, ndn );
260
261         mi = ( struct monitorinfo * )ch_calloc( sizeof( struct monitorinfo ), 1 );
262         ldap_pvt_thread_mutex_init( &mi->mi_cache_mutex );
263
264         if ( slap_str2ad( "description", &monitor_ad_desc, &text ) ) {
265 #ifdef NEW_LOGGING
266                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
267                         "monitor_back_db_init: %s\n", text ));
268 #else
269                 Debug( LDAP_DEBUG_ANY,
270                         "monitor_subsys_backend_init: %s\n%s%s", 
271                         text, "", "" );
272 #endif
273                 return( -1 );
274         }
275
276         /*      
277          * Create all the subsystem specific entries
278          */
279         e_tmp = NULL;
280         for ( i = 0; monitor_subsys[ i ].mss_name != NULL; i++ ) {
281                 int             len = strlen( monitor_subsys[ i ].mss_name );
282                 struct berval   dn;
283                 int             rc;
284
285                 dn.bv_len = len + sizeof( "cn=" ) - 1;
286                 dn.bv_val = ch_calloc( sizeof( char ), dn.bv_len + 1 );
287                 strcpy( dn.bv_val, "cn=" );
288                 strcat( dn.bv_val, monitor_subsys[ i ].mss_name );
289                 rc = dnPretty2( NULL, &dn, &monitor_subsys[ i ].mss_rdn );
290                 free( dn.bv_val );
291                 if ( rc != LDAP_SUCCESS ) {
292 #ifdef NEW_LOGGING
293                         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
294                                 "monitor RDN \"%s\" is invalid\n", 
295                                 dn.bv_val ));
296 #else
297                         Debug( LDAP_DEBUG_ANY,
298                                 "monitor RDN \"%s\" is invalid\n", 
299                                 dn.bv_val, 0, 0 );
300 #endif
301                         return( -1 );
302                 }
303
304                 dn.bv_len += sizeof( SLAPD_MONITOR_DN ); /* 1 for the , */
305                 dn.bv_val = ch_malloc( dn.bv_len + 1 );
306                 strcpy( dn.bv_val , monitor_subsys[ i ].mss_rdn.bv_val );
307                 strcat( dn.bv_val, "," SLAPD_MONITOR_DN );
308                 rc = dnPrettyNormal( NULL, &dn, &monitor_subsys[ i ].mss_dn,
309                         &monitor_subsys[ i ].mss_ndn );
310                 free( dn.bv_val );
311                 if ( rc != LDAP_SUCCESS ) {
312 #ifdef NEW_LOGGING
313                         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
314                                 "monitor DN \"%s\" is invalid\n", 
315                                 dn.bv_val ));
316 #else
317                         Debug( LDAP_DEBUG_ANY,
318                                 "monitor DN \"%s\" is invalid\n", 
319                                 dn.bv_val, 0, 0 );
320 #endif
321                         return( -1 );
322                 }
323
324                 snprintf( buf, sizeof( buf ),
325                                 "dn: %s\n"
326                                 SLAPD_MONITOR_OBJECTCLASSES 
327                                 "cn: %s\n",
328                                 monitor_subsys[ i ].mss_dn.bv_val,
329                                 monitor_subsys[ i ].mss_name );
330                 
331                 e = str2entry( buf );
332                 
333                 if ( e == NULL) {
334 #ifdef NEW_LOGGING
335                         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
336                                 "unable to create '%s' entry\n", 
337                                 monitor_subsys[ i ].mss_dn.bv_val ));
338 #else
339                         Debug( LDAP_DEBUG_ANY,
340                                 "unable to create '%s' entry\n", 
341                                 monitor_subsys[ i ].mss_dn.bv_val, 0, 0 );
342 #endif
343                         return( -1 );
344                 }
345
346                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
347                 e->e_private = ( void * )mp;
348                 mp->mp_info = &monitor_subsys[ i ];
349                 mp->mp_children = NULL;
350                 mp->mp_next = e_tmp;
351                 mp->mp_flags = monitor_subsys[ i ].mss_flags;
352
353                 if ( monitor_cache_add( mi, e ) ) {
354 #ifdef NEW_LOGGING
355                         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
356                                 "unable to add entry '%s' to cache\n",
357                                 monitor_subsys[ i ].mss_dn.bv_val ));
358 #else
359                         Debug( LDAP_DEBUG_ANY,
360                                 "unable to add entry '%s' to cache\n",
361                                 monitor_subsys[ i ].mss_dn.bv_val, 0, 0 );
362 #endif
363                         return -1;
364                 }
365
366                 e_tmp = e;
367         }
368
369         /*
370          * creates the "cn=Monitor" entry 
371          */
372         snprintf( buf, sizeof( buf ), 
373                         "dn: " SLAPD_MONITOR_DN "\n"
374                         "objectClass: top\n"
375                         "objectClass: monitor\n"
376                         "objectClass: extensibleObject\n"
377                         "structuralObjectClass: monitor\n"
378                         "cn: Monitor" );
379
380         e = str2entry( buf );
381         if ( e == NULL) {
382 #ifdef NEW_LOGGING
383                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
384                         "unable to create '%s' entry\n",
385                         SLAPD_MONITOR_DN ));
386 #else
387                 Debug( LDAP_DEBUG_ANY,
388                         "unable to create '%s' entry\n%s%s",
389                         SLAPD_MONITOR_DN, "", "" );
390 #endif
391                 return( -1 );
392         }
393         bv[1].bv_val = NULL;
394         bv[0].bv_val = (char *) Versionstr;
395         end_of_line = strchr( Versionstr, '\n' );
396         if ( end_of_line ) {
397                 bv[0].bv_len = end_of_line - Versionstr;
398         } else {
399                 bv[0].bv_len = strlen( Versionstr );
400         }
401         if ( attr_merge( e, monitor_ad_desc, bv ) ) {
402 #ifdef NEW_LOGGING
403                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
404                         "unable to add description to '%s' entry\n",
405                         SLAPD_MONITOR_DN ));
406 #else
407                 Debug( LDAP_DEBUG_ANY,
408                         "unable to add description to '%s' entry\n%s%s",
409                         SLAPD_MONITOR_DN, "", "" );
410 #endif
411                 return( -1 );
412         }
413
414         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
415         e->e_private = ( void * )mp;
416
417         mp->mp_info = NULL;
418         mp->mp_children = e_tmp;
419         mp->mp_next = NULL;
420
421         if ( monitor_cache_add( mi, e ) ) {
422 #ifdef NEW_LOGGING
423                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
424                         "unable to add entry '%s' to cache\n",
425                         SLAPD_MONITOR_DN ));
426 #else
427                 Debug( LDAP_DEBUG_ANY,
428                         "unable to add entry '%s' to cache\n%s%s",
429                         SLAPD_MONITOR_DN, "", "" );
430 #endif
431                 return -1;
432         }
433
434         be->be_private = mi;
435         
436         return 0;
437 }
438
439 int
440 monitor_back_open(
441         BackendInfo     *bi
442 )
443 {
444         BackendDB               *be;
445         struct monitorsubsys    *ms;
446         struct berval dn = { sizeof(SLAPD_MONITOR_DN)-1, SLAPD_MONITOR_DN };
447         struct berval ndn;
448         int rc;
449
450         /*
451          * adds the monitor backend
452          */
453         rc = dnNormalize2( NULL, &dn, &ndn );
454         if( rc != LDAP_SUCCESS ) {
455 #ifdef NEW_LOGGING
456                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
457                         "monitor DN \"" SLAPD_MONITOR_DN "\" is invalid\n" ));
458 #else
459                 Debug( LDAP_DEBUG_ANY,
460                         "monitor DN \"" SLAPD_MONITOR_DN "\" is invalid\n",
461                         0, 0, 0 );
462 #endif
463                 return( -1 );
464         }
465
466         be = select_backend( &ndn , 0, 0 );
467         free( ndn.bv_val );
468
469         if ( be == NULL ) {
470 #ifdef NEW_LOGGING
471                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
472                         "unable to get monitor backend\n" ));
473 #else
474                 Debug( LDAP_DEBUG_ANY,
475                         "unable to get monitor backend\n", 0, 0, 0 );
476 #endif
477                 return( -1 );
478         }
479
480         for ( ms = monitor_subsys; ms->mss_name != NULL; ms++ ) {
481                 if ( ms->mss_init && ( *ms->mss_init )( be ) ) {
482                         return( -1 );
483                 }
484         }
485
486         return( 0 );
487 }
488
489 int
490 monitor_back_config(
491         BackendInfo     *bi,
492         const char      *fname,
493         int             lineno,
494         int             argc,
495         char            **argv
496 )
497 {
498         /*
499          * eventually, will hold backend specific configuration parameters
500          */
501         return 0;
502 }
503
504 int
505 monitor_back_db_config(
506         Backend     *be,
507         const char  *fname,
508         int         lineno,
509         int         argc,
510         char        **argv
511 )
512 {
513 #ifdef NEW_LOGGING
514         LDAP_LOG(( "config", LDAP_DEBUG_NOTICE,
515                 "line %d of file '%s' will be ignored\n", lineno, fname ));
516 #else
517         Debug( LDAP_DEBUG_CONFIG, 
518                 "line %d of file '%s' will be ignored\n%s", lineno, fname, "" );
519 #endif
520         return( 0 );
521 }
522
523 int
524 monitor_back_db_destroy(
525         BackendDB       *be
526 )
527 {
528         /*
529          * FIXME: destroys all the data
530          */
531         return 0;
532 }
533