]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/init.c
de541088e71b35652d5260240d145a0d7a3b1912
[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
41 #include "slap.h"
42 #include "back-monitor.h"
43
44 /*
45  * used by many functions to add description to entries
46  */
47 AttributeDescription *monitor_ad_desc = NULL;
48
49 /*
50  * subsystem data
51  */
52 struct monitorsubsys monitor_subsys[] = {
53         { 
54                 SLAPD_MONITOR_LISTENER, SLAPD_MONITOR_LISTENER_NAME,    
55                 NULL, NULL, NULL,
56                 MONITOR_F_NONE,
57                 NULL,   /* init */
58                 NULL,   /* update */
59                 NULL,   /* create */
60                 NULL    /* modify */
61         }, { 
62                 SLAPD_MONITOR_DATABASE, SLAPD_MONITOR_DATABASE_NAME,    
63                 NULL, NULL, NULL,
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                 NULL, NULL, NULL,
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                 NULL, NULL, NULL,
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                 NULL, NULL, NULL,
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                 NULL, NULL, NULL,
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                 NULL, NULL, NULL,
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                 NULL, NULL, NULL,
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                 NULL, NULL, NULL,
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                 NULL, NULL, NULL,
128                 MONITOR_F_NONE,
129                 monitor_subsys_log_init,
130                 NULL,   /* update */
131                 NULL,   /* create */
132                 monitor_subsys_log_modify
133         }, { -1, NULL }
134 };
135
136 int
137 monitor_back_initialize(
138         BackendInfo     *bi
139 )
140 {
141         static char *controls[] = {
142                 LDAP_CONTROL_MANAGEDSAIT,
143                 NULL
144         };
145
146         bi->bi_controls = controls;
147
148         bi->bi_init = NULL;
149         bi->bi_open = monitor_back_open;
150         bi->bi_config = monitor_back_config;
151         bi->bi_close = NULL;
152         bi->bi_destroy = NULL;
153
154         bi->bi_db_init = monitor_back_db_init;
155         bi->bi_db_config = monitor_back_db_config;
156         bi->bi_db_open = NULL;
157         bi->bi_db_close = NULL;
158         bi->bi_db_destroy = monitor_back_db_destroy;
159
160         bi->bi_op_bind = monitor_back_bind;
161         bi->bi_op_unbind = NULL;
162         bi->bi_op_search = monitor_back_search;
163         bi->bi_op_compare = monitor_back_compare;
164         bi->bi_op_modify = monitor_back_modify;
165         bi->bi_op_modrdn = NULL;
166         bi->bi_op_add = NULL;
167         bi->bi_op_delete = NULL;
168         bi->bi_op_abandon = monitor_back_abandon;
169
170         bi->bi_extended = NULL;
171
172         bi->bi_entry_release_rw = NULL;
173         bi->bi_acl_group = NULL;
174         bi->bi_acl_attribute = NULL;
175         bi->bi_chk_referrals = NULL;
176
177         /*
178          * hooks for slap tools
179          */
180         bi->bi_tool_entry_open = NULL;
181         bi->bi_tool_entry_close = NULL;
182         bi->bi_tool_entry_first = NULL;
183         bi->bi_tool_entry_next = NULL;
184         bi->bi_tool_entry_get = NULL;
185         bi->bi_tool_entry_put = NULL;
186         bi->bi_tool_entry_reindex = NULL;
187         bi->bi_tool_sync = NULL;
188
189         bi->bi_connection_init = 0;
190         bi->bi_connection_destroy = 0;
191
192         return 0;
193 }
194
195 int
196 monitor_back_db_init(
197         BackendDB       *be
198 )
199 {
200         struct monitorinfo      *mi;
201         Entry                   *e, *e_tmp;
202         struct monitorentrypriv *mp;
203         int                     i;
204         char                    buf[1024], *ndn, *end_of_line;
205         const char              *text;
206         struct berval           val, *bv[2] = { &val, NULL };
207
208         /*
209          * database monitor can be defined once only
210          */
211         static int              monitor_defined = 0;
212
213         if ( monitor_defined ) {
214 #ifdef NEW_LOGGING
215                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
216                         "only one monitor backend is allowed\n" ));
217 #else
218                 Debug( LDAP_DEBUG_ANY,
219                         "only one monitor backend is allowed\n%s%s%s",
220                         "", "", "" );
221 #endif
222                 return( -1 );
223         }
224         monitor_defined++;
225
226         ndn = ch_strdup( SLAPD_MONITOR_DN );
227         charray_add( &be->be_suffix, ndn );
228         dn_normalize( ndn );
229         ber_bvecadd( &be->be_nsuffix, ber_bvstr( ndn ) );
230
231         mi = ( struct monitorinfo * )ch_calloc( sizeof( struct monitorinfo ), 1 );
232         ldap_pvt_thread_mutex_init( &mi->mi_cache_mutex );
233
234         if ( slap_str2ad( "description", &monitor_ad_desc, &text ) ) {
235 #ifdef NEW_LOGGING
236                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
237                         "monitor_back_db_init: %s\n", text ));
238 #else
239                 Debug( LDAP_DEBUG_ANY,
240                         "monitor_subsys_backend_init: %s\n%s%s", 
241                         text, "", "" );
242 #endif
243                 return( -1 );
244         }
245
246         /*      
247          * Create all the subsystem specific entries
248          */
249         e_tmp = NULL;
250         for ( i = 0; monitor_subsys[ i ].mss_name != NULL; i++ ) {
251                 int len = strlen( monitor_subsys[ i ].mss_name );
252
253                 monitor_subsys[ i ].mss_rdn = ch_calloc( sizeof( char ), 
254                                 4 + len );
255                 strcpy( monitor_subsys[ i ].mss_rdn, "cn=" );
256                 strcat( monitor_subsys[ i ].mss_rdn, 
257                                 monitor_subsys[ i ].mss_name );
258
259                 monitor_subsys[ i ].mss_dn = ch_calloc( sizeof( char ), 
260                                 4 + len + sizeof( SLAPD_MONITOR_DN ) );
261                 strcpy( monitor_subsys[ i ].mss_dn, 
262                                 monitor_subsys[ i ].mss_rdn );
263                 strcat( monitor_subsys[ i ].mss_dn, "," );
264                 strcat( monitor_subsys[ i ].mss_dn, SLAPD_MONITOR_DN );
265
266                 monitor_subsys[ i ].mss_ndn 
267                         = ch_strdup( monitor_subsys[ i ].mss_dn );
268                 dn_normalize( monitor_subsys[ i ].mss_ndn );
269
270                 snprintf( buf, sizeof( buf ),
271                                 "dn: %s\n"
272                                 "objectClass: top\n"
273                                 "objectClass: LDAPsubEntry\n"
274 #ifdef SLAPD_MONITORSUBENTRY
275                                 "objectClass: monitorSubEntry\n"
276 #else /* !SLAPD_MONITORSUBENTRY */
277                                 "objectClass: extensibleObject\n"
278 #endif /* !SLAPD_MONITORSUBENTRY */
279                                 "cn: %s\n",
280                                 monitor_subsys[ i ].mss_dn,
281                                 monitor_subsys[ i ].mss_name );
282                 
283                 e = str2entry( buf );
284                 
285                 if ( e == NULL) {
286 #ifdef NEW_LOGGING
287                         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
288                                 "unable to create '%s' entry\n", 
289                                 monitor_subsys[ i ].mss_dn ));
290 #else
291                         Debug( LDAP_DEBUG_ANY,
292                                 "unable to create '%s' entry\n%s%s", 
293                                 monitor_subsys[ i ].mss_dn, "", "" );
294 #endif
295                         return( -1 );
296                 }
297
298                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
299                 e->e_private = ( void * )mp;
300                 mp->mp_info = &monitor_subsys[ i ];
301                 mp->mp_children = NULL;
302                 mp->mp_next = e_tmp;
303                 mp->mp_flags = monitor_subsys[ i ].mss_flags;
304
305                 if ( monitor_cache_add( mi, e ) ) {
306 #ifdef NEW_LOGGING
307                         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
308                                 "unable to add entry '%s' to cache\n",
309                                 monitor_subsys[ i ].mss_dn ));
310 #else
311                         Debug( LDAP_DEBUG_ANY,
312                                 "unable to add entry '%s' to cache\n%s%s",
313                                 monitor_subsys[ i ].mss_dn, "", "" );
314 #endif
315                         return -1;
316                 }
317
318                 e_tmp = e;
319         }
320
321         /*
322          * creates the "cn=Monitor" entry 
323          */
324         snprintf( buf, sizeof( buf ), 
325                         "dn: %s\n"
326                         "objectClass: top\n"
327                         "objectClass: LDAPsubEntry\n"
328 #ifdef SLAPD_MONITORSUBENTRY
329                         "objectClass: monitorSubEntry\n"
330 #else /* !SLAPD_MONITORSUBENTRY */
331                         "objectClass: extensibleObject\n"
332 #endif /* !SLAPD_MONITORSUBENTRY */
333                         "cn: Monitor",
334                         SLAPD_MONITOR_DN
335                         );
336         e = str2entry( buf );
337         if ( e == NULL) {
338 #ifdef NEW_LOGGING
339                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
340                         "unable to create '%s' entry\n",
341                         SLAPD_MONITOR_DN ));
342 #else
343                 Debug( LDAP_DEBUG_ANY,
344                         "unable to create '%s' entry\n%s%s",
345                         SLAPD_MONITOR_DN, "", "" );
346 #endif
347                 return( -1 );
348         }
349         val.bv_val = (char *) Versionstr;
350         end_of_line = strchr( Versionstr, '\n' );
351         if ( end_of_line ) {
352                 val.bv_len = end_of_line - Versionstr;
353         } else {
354                 val.bv_len = strlen( Versionstr );
355         }
356         if ( attr_merge( e, monitor_ad_desc, bv ) ) {
357 #ifdef NEW_LOGGING
358                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
359                         "unable to add description to '%s' entry\n",
360                         SLAPD_MONITOR_DN ));
361 #else
362                 Debug( LDAP_DEBUG_ANY,
363                         "unable to add description to '%s' entry\n%s%s",
364                         SLAPD_MONITOR_DN, "", "" );
365 #endif
366                 return( -1 );
367         }
368
369         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
370         e->e_private = ( void * )mp;
371
372         mp->mp_info = NULL;
373         mp->mp_children = e_tmp;
374         mp->mp_next = NULL;
375
376         if ( monitor_cache_add( mi, e ) ) {
377 #ifdef NEW_LOGGING
378                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
379                         "unable to add entry '%s' to cache\n",
380                         SLAPD_MONITOR_DN ));
381 #else
382                 Debug( LDAP_DEBUG_ANY,
383                         "unable to add entry '%s' to cache\n%s%s",
384                         SLAPD_MONITOR_DN, "", "" );
385 #endif
386                 return -1;
387         }
388
389         be->be_private = mi;
390         
391         return 0;
392 }
393
394 int
395 monitor_back_open(
396         BackendInfo     *bi
397 )
398 {
399         BackendDB               *be;
400         struct monitorsubsys    *ms;
401         char                    *ndn;
402
403         /*
404          * adds the monitor backend
405          */
406         ndn = ch_strdup( SLAPD_MONITOR_DN );
407         dn_normalize( ndn );
408         be = select_backend( ndn , 0, 0 );
409         ch_free( ndn );
410         if ( be == NULL ) {
411 #ifdef NEW_LOGGING
412                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
413                         "unable to get monitor backend\n" ));
414 #else
415                 Debug( LDAP_DEBUG_ANY,
416                         "unable to get monitor backend\n%s%s%s", "", "", "" );
417 #endif
418                 return( -1 );
419         }
420
421         for ( ms = monitor_subsys; ms->mss_name != NULL; ms++ ) {
422                 if ( ms->mss_init && ( *ms->mss_init )( be ) ) {
423                         return( -1 );
424                 }
425         }
426
427         return( 0 );
428 }
429
430 int
431 monitor_back_config(
432         BackendInfo     *bi,
433         const char      *fname,
434         int             lineno,
435         int             argc,
436         char            **argv
437 )
438 {
439         /*
440          * eventually, will hold backend specific configuration parameters
441          */
442         return 0;
443 }
444
445 int
446 monitor_back_db_config(
447         Backend     *be,
448         const char  *fname,
449         int         lineno,
450         int         argc,
451         char        **argv
452 )
453 {
454 #ifdef NEW_LOGGING
455         LDAP_LOG(( "config", LDAP_DEBUG_NOTICE,
456                 "line %d of file '%s' will be ignored\n", lineno, fname ));
457 #else
458         Debug( LDAP_DEBUG_CONFIG, 
459                 "line %d of file '%s' will be ignored\n%s", lineno, fname, "" );
460 #endif
461         return( 0 );
462 }
463
464 int
465 monitor_back_db_destroy(
466         BackendDB       *be
467 )
468 {
469         /*
470          * FIXME: destroys all the data
471          */
472         return 0;
473 }
474