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