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