]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/init.c
0b9e500ca79d6c8ead5627ab343ab416890d9fae
[openldap] / servers / slapd / back-monitor / init.c
1 /* init.c - initialize monitor backend */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright 2001 The OpenLDAP Foundation, All Rights Reserved.
8  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
9  * 
10  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
11  * 
12  * This work has beed deveolped for the OpenLDAP Foundation 
13  * in the hope that it may be useful to the Open Source community, 
14  * but WITHOUT ANY WARRANTY.
15  * 
16  * Permission is granted to anyone to use this software for any purpose
17  * on any computer system, and to alter it and redistribute it, subject
18  * to the following restrictions:
19  * 
20  * 1. The author and SysNet s.n.c. are not responsible for the consequences
21  *    of use of this software, no matter how awful, even if they arise from
22  *    flaws in it.
23  * 
24  * 2. The origin of this software must not be misrepresented, either by
25  *    explicit claim or by omission.  Since few users ever read sources,
26  *    credits should appear in the documentation.
27  * 
28  * 3. Altered versions must be plainly marked as such, and must not be
29  *    misrepresented as being the original software.  Since few users
30  *    ever read sources, credits should appear in the documentation.
31  *    SysNet s.n.c. cannot be responsible for the consequences of the
32  *    alterations.
33  * 
34  * 4. This notice may not be removed or altered.
35  */
36
37 #include "portable.h"
38
39 #include <stdio.h>
40 #include <ac/string.h>
41
42 #include "slap.h"
43 #include "back-monitor.h"
44
45 /*
46  * used by many functions to add description to entries
47  */
48 AttributeDescription *monitor_ad_desc = NULL;
49
50 /*
51  * subsystem data
52  */
53 struct monitorsubsys monitor_subsys[] = {
54         { 
55                 SLAPD_MONITOR_LISTENER, SLAPD_MONITOR_LISTENER_NAME,    
56                 NULL, NULL, NULL,
57                 MONITOR_F_NONE,
58                 NULL,   /* init */
59                 NULL,   /* update */
60                 NULL,   /* create */
61                 NULL    /* modify */
62         }, { 
63                 SLAPD_MONITOR_DATABASE, SLAPD_MONITOR_DATABASE_NAME,    
64                 NULL, NULL, NULL,
65                 MONITOR_F_PERSISTENT_CH,
66                 monitor_subsys_database_init,
67                 NULL,   /* update */
68                 NULL,   /* create */
69                 NULL    /* modify */
70         }, { 
71                 SLAPD_MONITOR_BACKEND, SLAPD_MONITOR_BACKEND_NAME, 
72                 NULL, NULL, NULL,
73                 MONITOR_F_PERSISTENT_CH,
74                 monitor_subsys_backend_init,
75                 NULL,   /* update */
76                 NULL,   /* create */
77                 NULL    /* modify */
78         }, { 
79                 SLAPD_MONITOR_THREAD, SLAPD_MONITOR_THREAD_NAME,        
80                 NULL, NULL, NULL,
81                 MONITOR_F_NONE,
82                 monitor_subsys_thread_init,
83                 monitor_subsys_thread_update,
84                 NULL,   /* create */
85                 NULL    /* modify */
86         }, { 
87                 SLAPD_MONITOR_SASL, SLAPD_MONITOR_SASL_NAME,    
88                 NULL, NULL, NULL,
89                 MONITOR_F_NONE,
90                 NULL,   /* init */
91                 NULL,   /* update */
92                 NULL,   /* create */
93                 NULL    /* modify */
94         }, { 
95                 SLAPD_MONITOR_TLS, SLAPD_MONITOR_TLS_NAME,
96                 NULL, NULL, NULL,
97                 MONITOR_F_NONE,
98                 NULL,   /* init */
99                 NULL,   /* update */
100                 NULL,   /* create */
101                 NULL    /* modify */
102         }, { 
103                 SLAPD_MONITOR_CONN, SLAPD_MONITOR_CONN_NAME,
104                 NULL, NULL, NULL,
105                 MONITOR_F_VOLATILE_CH,
106                 monitor_subsys_conn_init,
107                 monitor_subsys_conn_update,
108                 monitor_subsys_conn_create,
109                 NULL    /* modify */
110         }, { 
111                 SLAPD_MONITOR_READW, SLAPD_MONITOR_READW_NAME,
112                 NULL, NULL, NULL,
113                 MONITOR_F_NONE,
114                 NULL,   /* init */
115                 monitor_subsys_readw_update,
116                 NULL,   /* create */
117                 NULL    /* modify */
118         }, { 
119                 SLAPD_MONITOR_WRITEW, SLAPD_MONITOR_WRITEW_NAME,
120                 NULL, NULL, NULL,
121                 MONITOR_F_NONE,
122                 NULL,   /* init */
123                 monitor_subsys_writew_update,
124                 NULL,   /* create */
125                 NULL    /* modify */
126         }, { 
127                 SLAPD_MONITOR_LOG, SLAPD_MONITOR_LOG_NAME,
128                 NULL, NULL, NULL,
129                 MONITOR_F_NONE,
130                 monitor_subsys_log_init,
131                 NULL,   /* update */
132                 NULL,   /* create */
133                 monitor_subsys_log_modify
134         }, { 
135                 SLAPD_MONITOR_OPS, SLAPD_MONITOR_OPS_NAME,
136                 NULL, NULL, NULL,
137                 MONITOR_F_NONE,
138                 monitor_subsys_ops_init,
139                 monitor_subsys_ops_update,
140                 NULL,   /* create */
141                 NULL,   /* modify */
142         }, { 
143                 SLAPD_MONITOR_SENT, SLAPD_MONITOR_SENT_NAME,
144                 NULL, NULL, NULL,
145                 MONITOR_F_NONE,
146                 monitor_subsys_sent_init,
147                 monitor_subsys_sent_update,
148                 NULL,   /* create */
149                 NULL,   /* modify */
150         }, { -1, NULL }
151 };
152
153 int
154 monitor_back_initialize(
155         BackendInfo     *bi
156 )
157 {
158         static char *controls[] = {
159                 LDAP_CONTROL_MANAGEDSAIT,
160                 NULL
161         };
162
163         bi->bi_controls = controls;
164
165         bi->bi_init = 0;
166         bi->bi_open = monitor_back_open;
167         bi->bi_config = monitor_back_config;
168         bi->bi_close = 0;
169         bi->bi_destroy = 0;
170
171         bi->bi_db_init = monitor_back_db_init;
172         bi->bi_db_config = monitor_back_db_config;
173         bi->bi_db_open = 0;
174         bi->bi_db_close = 0;
175         bi->bi_db_destroy = monitor_back_db_destroy;
176
177         bi->bi_op_bind = monitor_back_bind;
178         bi->bi_op_unbind = 0;
179         bi->bi_op_search = monitor_back_search;
180         bi->bi_op_compare = monitor_back_compare;
181         bi->bi_op_modify = monitor_back_modify;
182         bi->bi_op_modrdn = 0;
183         bi->bi_op_add = 0;
184         bi->bi_op_delete = 0;
185         bi->bi_op_abandon = monitor_back_abandon;
186
187         bi->bi_extended = 0;
188
189         bi->bi_entry_release_rw = 0;
190         bi->bi_acl_group = 0;
191         bi->bi_acl_attribute = 0;
192         bi->bi_chk_referrals = 0;
193         bi->bi_operational = monitor_back_operational;
194
195         /*
196          * hooks for slap tools
197          */
198         bi->bi_tool_entry_open = 0;
199         bi->bi_tool_entry_close = 0;
200         bi->bi_tool_entry_first = 0;
201         bi->bi_tool_entry_next = 0;
202         bi->bi_tool_entry_get = 0;
203         bi->bi_tool_entry_put = 0;
204         bi->bi_tool_entry_reindex = 0;
205         bi->bi_tool_sync = 0;
206
207         bi->bi_connection_init = 0;
208         bi->bi_connection_destroy = 0;
209
210         return 0;
211 }
212
213 int
214 monitor_back_db_init(
215         BackendDB       *be
216 )
217 {
218         struct monitorinfo      *mi;
219         Entry                   *e, *e_tmp;
220         struct monitorentrypriv *mp;
221         int                     i, rc;
222         char                    buf[1024], *end_of_line;
223         struct berval           dn, *ndn;
224         const char              *text;
225         struct berval           val, *bv[2] = { &val, NULL };
226
227         /*
228          * database monitor can be defined once only
229          */
230         static int              monitor_defined = 0;
231
232         if ( monitor_defined ) {
233 #ifdef NEW_LOGGING
234                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
235                         "only one monitor backend is allowed\n" ));
236 #else
237                 Debug( LDAP_DEBUG_ANY,
238                         "only one monitor backend is allowed\n%s%s%s",
239                         "", "", "" );
240 #endif
241                 return( -1 );
242         }
243         monitor_defined++;
244
245         ndn = NULL;
246         dn.bv_val = SLAPD_MONITOR_DN;
247         dn.bv_len = sizeof( SLAPD_MONITOR_DN ) - 1;
248
249         rc = dnNormalize( NULL, &dn, &ndn );
250         if( rc != LDAP_SUCCESS ) {
251 #ifdef NEW_LOGGING
252                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
253                         "monitor DN \"" SLAPD_MONITOR_DN "\" backend is allowed\n" ));
254 #else
255                 Debug( LDAP_DEBUG_ANY,
256                         "monitor DN \"" SLAPD_MONITOR_DN "\" backend is allowed\n",
257                         0, 0, 0 );
258 #endif
259                 return -1;
260         }
261
262         ber_bvecadd( &be->be_suffix, ber_bvdup( &dn ) );
263         ber_bvecadd( &be->be_nsuffix, ndn );
264
265         mi = ( struct monitorinfo * )ch_calloc( sizeof( struct monitorinfo ), 1 );
266         ldap_pvt_thread_mutex_init( &mi->mi_cache_mutex );
267
268         if ( slap_str2ad( "description", &monitor_ad_desc, &text ) ) {
269 #ifdef NEW_LOGGING
270                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
271                         "monitor_back_db_init: %s\n", text ));
272 #else
273                 Debug( LDAP_DEBUG_ANY,
274                         "monitor_subsys_backend_init: %s\n%s%s", 
275                         text, "", "" );
276 #endif
277                 return( -1 );
278         }
279
280         /*      
281          * Create all the subsystem specific entries
282          */
283         e_tmp = NULL;
284         for ( i = 0; monitor_subsys[ i ].mss_name != NULL; i++ ) {
285                 int             len = strlen( monitor_subsys[ i ].mss_name );
286                 struct berval   dn, *pdn;
287                 int             rc;
288
289                 dn.bv_len = len + sizeof( "cn=" ) - 1;
290                 dn.bv_val = ch_calloc( sizeof( char ), dn.bv_len + 1 );
291                 strcpy( dn.bv_val, "cn=" );
292                 strcat( dn.bv_val, monitor_subsys[ i ].mss_name );
293                 pdn = NULL;
294                 rc = dnPretty( NULL, &dn, &pdn );
295                 if ( rc != LDAP_SUCCESS ) {
296 #ifdef NEW_LOGGING
297                         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
298                                 "monitor RDN \"%s\" is invalid\n", 
299                                 dn.bv_val ));
300 #else
301                         Debug( LDAP_DEBUG_ANY,
302                                 "monitor RDN \"%s\" is invalid\n", 
303                                 dn.bv_val, 0, 0 );
304 #endif
305                         free( dn.bv_val );
306                         return( -1 );
307                 }
308                 free( dn.bv_val );
309                 monitor_subsys[ i ].mss_rdn = pdn;
310
311                 dn.bv_len += sizeof( SLAPD_MONITOR_DN ); /* 1 for the , */
312                 dn.bv_val = ch_calloc( sizeof( char ), dn.bv_len + 1 );
313                 strcpy( dn.bv_val , monitor_subsys[ i ].mss_rdn->bv_val );
314                 strcat( dn.bv_val, "," SLAPD_MONITOR_DN );
315                 pdn = NULL;
316                 rc = dnPretty( NULL, &dn, &pdn );
317                 if ( rc != LDAP_SUCCESS ) {
318 #ifdef NEW_LOGGING
319                         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
320                                 "monitor DN \"%s\" is invalid\n", 
321                                 dn.bv_val ));
322 #else
323                         Debug( LDAP_DEBUG_ANY,
324                                 "monitor DN \"%s\" is invalid\n", 
325                                 dn.bv_val, 0, 0 );
326 #endif
327                         free( dn.bv_val );
328                         return( -1 );
329                 }
330                 monitor_subsys[ i ].mss_dn = pdn;
331
332                 pdn = NULL;
333                 dnNormalize( NULL, &dn, &pdn );
334                 if ( rc != LDAP_SUCCESS ) {
335 #ifdef NEW_LOGGING
336                         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
337                                 "monitor DN \"%s\" is invalid\n", 
338                                 dn.bv_val ));
339 #else
340                         Debug( LDAP_DEBUG_ANY,
341                                 "monitor DN \"%s\" is invalid\n", 
342                                 dn.bv_val, 0, 0 );
343 #endif
344                         free( dn.bv_val );
345                         return( -1 );
346                 }
347                 free( dn.bv_val );
348                 monitor_subsys[ i ].mss_ndn = pdn;
349
350                 snprintf( buf, sizeof( buf ),
351                                 "dn: %s\n"
352                                 "objectClass: top\n"
353                                 "objectClass: LDAPsubEntry\n"
354 #ifdef SLAPD_MONITORSUBENTRY
355                                 "objectClass: monitorSubEntry\n"
356 #else /* !SLAPD_MONITORSUBENTRY */
357                                 "objectClass: extensibleObject\n"
358 #endif /* !SLAPD_MONITORSUBENTRY */
359                                 "cn: %s\n",
360                                 monitor_subsys[ i ].mss_dn->bv_val,
361                                 monitor_subsys[ i ].mss_name );
362                 
363                 e = str2entry( buf );
364                 
365                 if ( e == NULL) {
366 #ifdef NEW_LOGGING
367                         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
368                                 "unable to create '%s' entry\n", 
369                                 monitor_subsys[ i ].mss_dn->bv_val ));
370 #else
371                         Debug( LDAP_DEBUG_ANY,
372                                 "unable to create '%s' entry\n", 
373                                 monitor_subsys[ i ].mss_dn->bv_val, 0, 0 );
374 #endif
375                         return( -1 );
376                 }
377
378                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
379                 e->e_private = ( void * )mp;
380                 mp->mp_info = &monitor_subsys[ i ];
381                 mp->mp_children = NULL;
382                 mp->mp_next = e_tmp;
383                 mp->mp_flags = monitor_subsys[ i ].mss_flags;
384
385                 if ( monitor_cache_add( mi, e ) ) {
386 #ifdef NEW_LOGGING
387                         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
388                                 "unable to add entry '%s' to cache\n",
389                                 monitor_subsys[ i ].mss_dn->bv_val ));
390 #else
391                         Debug( LDAP_DEBUG_ANY,
392                                 "unable to add entry '%s' to cache\n",
393                                 monitor_subsys[ i ].mss_dn->bv_val, 0, 0 );
394 #endif
395                         return -1;
396                 }
397
398                 e_tmp = e;
399         }
400
401         /*
402          * creates the "cn=Monitor" entry 
403          */
404         snprintf( buf, sizeof( buf ), 
405                         "dn: " SLAPD_MONITOR_DN "\n"
406                         "objectClass: top\n"
407                         "objectClass: LDAPsubEntry\n"
408 #ifdef SLAPD_MONITORSUBENTRY
409                         "objectClass: monitorSubEntry\n"
410 #else /* !SLAPD_MONITORSUBENTRY */
411                         "objectClass: extensibleObject\n"
412 #endif /* !SLAPD_MONITORSUBENTRY */
413                         "cn: Monitor" );
414
415         e = str2entry( buf );
416         if ( e == NULL) {
417 #ifdef NEW_LOGGING
418                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
419                         "unable to create '%s' entry\n",
420                         SLAPD_MONITOR_DN ));
421 #else
422                 Debug( LDAP_DEBUG_ANY,
423                         "unable to create '%s' entry\n%s%s",
424                         SLAPD_MONITOR_DN, "", "" );
425 #endif
426                 return( -1 );
427         }
428         val.bv_val = (char *) Versionstr;
429         end_of_line = strchr( Versionstr, '\n' );
430         if ( end_of_line ) {
431                 val.bv_len = end_of_line - Versionstr;
432         } else {
433                 val.bv_len = strlen( Versionstr );
434         }
435         if ( attr_merge( e, monitor_ad_desc, bv ) ) {
436 #ifdef NEW_LOGGING
437                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
438                         "unable to add description to '%s' entry\n",
439                         SLAPD_MONITOR_DN ));
440 #else
441                 Debug( LDAP_DEBUG_ANY,
442                         "unable to add description to '%s' entry\n%s%s",
443                         SLAPD_MONITOR_DN, "", "" );
444 #endif
445                 return( -1 );
446         }
447
448         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
449         e->e_private = ( void * )mp;
450
451         mp->mp_info = NULL;
452         mp->mp_children = e_tmp;
453         mp->mp_next = NULL;
454
455         if ( monitor_cache_add( mi, e ) ) {
456 #ifdef NEW_LOGGING
457                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
458                         "unable to add entry '%s' to cache\n",
459                         SLAPD_MONITOR_DN ));
460 #else
461                 Debug( LDAP_DEBUG_ANY,
462                         "unable to add entry '%s' to cache\n%s%s",
463                         SLAPD_MONITOR_DN, "", "" );
464 #endif
465                 return -1;
466         }
467
468         be->be_private = mi;
469         
470         return 0;
471 }
472
473 int
474 monitor_back_open(
475         BackendInfo     *bi
476 )
477 {
478         BackendDB               *be;
479         struct monitorsubsys    *ms;
480         struct berval dn = { sizeof(SLAPD_MONITOR_DN)-1, SLAPD_MONITOR_DN };
481         struct berval *ndn = NULL;
482         int rc;
483
484         /*
485          * adds the monitor backend
486          */
487         rc = dnNormalize( NULL, &dn, &ndn );
488         if( rc != LDAP_SUCCESS ) {
489 #ifdef NEW_LOGGING
490                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
491                         "monitor DN \"" SLAPD_MONITOR_DN "\" is invalid\n" ));
492 #else
493                 Debug( LDAP_DEBUG_ANY,
494                         "monitor DN \"" SLAPD_MONITOR_DN "\" is invalid\n",
495                         0, 0, 0 );
496 #endif
497                 return( -1 );
498         }
499
500         be = select_backend( ndn , 0, 0 );
501         ber_bvfree( ndn );
502
503         if ( be == NULL ) {
504 #ifdef NEW_LOGGING
505                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
506                         "unable to get monitor backend\n" ));
507 #else
508                 Debug( LDAP_DEBUG_ANY,
509                         "unable to get monitor backend\n", 0, 0, 0 );
510 #endif
511                 return( -1 );
512         }
513
514         for ( ms = monitor_subsys; ms->mss_name != NULL; ms++ ) {
515                 if ( ms->mss_init && ( *ms->mss_init )( be ) ) {
516                         return( -1 );
517                 }
518         }
519
520         return( 0 );
521 }
522
523 int
524 monitor_back_config(
525         BackendInfo     *bi,
526         const char      *fname,
527         int             lineno,
528         int             argc,
529         char            **argv
530 )
531 {
532         /*
533          * eventually, will hold backend specific configuration parameters
534          */
535         return 0;
536 }
537
538 int
539 monitor_back_db_config(
540         Backend     *be,
541         const char  *fname,
542         int         lineno,
543         int         argc,
544         char        **argv
545 )
546 {
547 #ifdef NEW_LOGGING
548         LDAP_LOG(( "config", LDAP_DEBUG_NOTICE,
549                 "line %d of file '%s' will be ignored\n", lineno, fname ));
550 #else
551         Debug( LDAP_DEBUG_CONFIG, 
552                 "line %d of file '%s' will be ignored\n%s", lineno, fname, "" );
553 #endif
554         return( 0 );
555 }
556
557 int
558 monitor_back_db_destroy(
559         BackendDB       *be
560 )
561 {
562         /*
563          * FIXME: destroys all the data
564          */
565         return 0;
566 }
567