]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/init.c
db4713afb1a2cc6e47e0534dbea0920f86114f95
[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         bi->bi_operational = monitor_back_operational;
177
178         /*
179          * hooks for slap tools
180          */
181         bi->bi_tool_entry_open = NULL;
182         bi->bi_tool_entry_close = NULL;
183         bi->bi_tool_entry_first = NULL;
184         bi->bi_tool_entry_next = NULL;
185         bi->bi_tool_entry_get = NULL;
186         bi->bi_tool_entry_put = NULL;
187         bi->bi_tool_entry_reindex = NULL;
188         bi->bi_tool_sync = NULL;
189
190         bi->bi_connection_init = 0;
191         bi->bi_connection_destroy = 0;
192
193         return 0;
194 }
195
196 int
197 monitor_back_db_init(
198         BackendDB       *be
199 )
200 {
201         struct monitorinfo      *mi;
202         Entry                   *e, *e_tmp;
203         struct monitorentrypriv *mp;
204         int                     i;
205         char                    buf[1024], *ndn, *end_of_line;
206         const char              *text;
207         struct berval           val, *bv[2] = { &val, NULL };
208
209         /*
210          * database monitor can be defined once only
211          */
212         static int              monitor_defined = 0;
213
214         if ( monitor_defined ) {
215 #ifdef NEW_LOGGING
216                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
217                         "only one monitor backend is allowed\n" ));
218 #else
219                 Debug( LDAP_DEBUG_ANY,
220                         "only one monitor backend is allowed\n%s%s%s",
221                         "", "", "" );
222 #endif
223                 return( -1 );
224         }
225         monitor_defined++;
226
227         ndn = ch_strdup( SLAPD_MONITOR_DN );
228         charray_add( &be->be_suffix, ndn );
229         dn_normalize( ndn );
230         ber_bvecadd( &be->be_nsuffix, ber_bvstr( ndn ) );
231
232         mi = ( struct monitorinfo * )ch_calloc( sizeof( struct monitorinfo ), 1 );
233         ldap_pvt_thread_mutex_init( &mi->mi_cache_mutex );
234
235         if ( slap_str2ad( "description", &monitor_ad_desc, &text ) ) {
236 #ifdef NEW_LOGGING
237                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
238                         "monitor_back_db_init: %s\n", text ));
239 #else
240                 Debug( LDAP_DEBUG_ANY,
241                         "monitor_subsys_backend_init: %s\n%s%s", 
242                         text, "", "" );
243 #endif
244                 return( -1 );
245         }
246
247         /*      
248          * Create all the subsystem specific entries
249          */
250         e_tmp = NULL;
251         for ( i = 0; monitor_subsys[ i ].mss_name != NULL; i++ ) {
252                 int len = strlen( monitor_subsys[ i ].mss_name );
253
254                 monitor_subsys[ i ].mss_rdn = ch_calloc( sizeof( char ), 
255                                 4 + len );
256                 strcpy( monitor_subsys[ i ].mss_rdn, "cn=" );
257                 strcat( monitor_subsys[ i ].mss_rdn, 
258                                 monitor_subsys[ i ].mss_name );
259
260                 monitor_subsys[ i ].mss_dn = ch_calloc( sizeof( char ), 
261                                 4 + len + sizeof( SLAPD_MONITOR_DN ) );
262                 strcpy( monitor_subsys[ i ].mss_dn, 
263                                 monitor_subsys[ i ].mss_rdn );
264                 strcat( monitor_subsys[ i ].mss_dn, "," );
265                 strcat( monitor_subsys[ i ].mss_dn, SLAPD_MONITOR_DN );
266
267                 monitor_subsys[ i ].mss_ndn 
268                         = ch_strdup( monitor_subsys[ i ].mss_dn );
269                 dn_normalize( monitor_subsys[ i ].mss_ndn );
270
271                 snprintf( buf, sizeof( buf ),
272                                 "dn: %s\n"
273                                 "objectClass: top\n"
274                                 "objectClass: LDAPsubEntry\n"
275 #ifdef SLAPD_MONITORSUBENTRY
276                                 "objectClass: monitorSubEntry\n"
277 #else /* !SLAPD_MONITORSUBENTRY */
278                                 "objectClass: extensibleObject\n"
279 #endif /* !SLAPD_MONITORSUBENTRY */
280                                 "cn: %s\n",
281                                 monitor_subsys[ i ].mss_dn,
282                                 monitor_subsys[ i ].mss_name );
283                 
284                 e = str2entry( buf );
285                 
286                 if ( e == NULL) {
287 #ifdef NEW_LOGGING
288                         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
289                                 "unable to create '%s' entry\n", 
290                                 monitor_subsys[ i ].mss_dn ));
291 #else
292                         Debug( LDAP_DEBUG_ANY,
293                                 "unable to create '%s' entry\n%s%s", 
294                                 monitor_subsys[ i ].mss_dn, "", "" );
295 #endif
296                         return( -1 );
297                 }
298
299                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
300                 e->e_private = ( void * )mp;
301                 mp->mp_info = &monitor_subsys[ i ];
302                 mp->mp_children = NULL;
303                 mp->mp_next = e_tmp;
304                 mp->mp_flags = monitor_subsys[ i ].mss_flags;
305
306                 if ( monitor_cache_add( mi, e ) ) {
307 #ifdef NEW_LOGGING
308                         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
309                                 "unable to add entry '%s' to cache\n",
310                                 monitor_subsys[ i ].mss_dn ));
311 #else
312                         Debug( LDAP_DEBUG_ANY,
313                                 "unable to add entry '%s' to cache\n%s%s",
314                                 monitor_subsys[ i ].mss_dn, "", "" );
315 #endif
316                         return -1;
317                 }
318
319                 e_tmp = e;
320         }
321
322         /*
323          * creates the "cn=Monitor" entry 
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: Monitor",
335                         SLAPD_MONITOR_DN
336                         );
337         e = str2entry( buf );
338         if ( e == NULL) {
339 #ifdef NEW_LOGGING
340                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
341                         "unable to create '%s' entry\n",
342                         SLAPD_MONITOR_DN ));
343 #else
344                 Debug( LDAP_DEBUG_ANY,
345                         "unable to create '%s' entry\n%s%s",
346                         SLAPD_MONITOR_DN, "", "" );
347 #endif
348                 return( -1 );
349         }
350         val.bv_val = (char *) Versionstr;
351         end_of_line = strchr( Versionstr, '\n' );
352         if ( end_of_line ) {
353                 val.bv_len = end_of_line - Versionstr;
354         } else {
355                 val.bv_len = strlen( Versionstr );
356         }
357         if ( attr_merge( e, monitor_ad_desc, bv ) ) {
358 #ifdef NEW_LOGGING
359                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
360                         "unable to add description to '%s' entry\n",
361                         SLAPD_MONITOR_DN ));
362 #else
363                 Debug( LDAP_DEBUG_ANY,
364                         "unable to add description to '%s' entry\n%s%s",
365                         SLAPD_MONITOR_DN, "", "" );
366 #endif
367                 return( -1 );
368         }
369
370         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
371         e->e_private = ( void * )mp;
372
373         mp->mp_info = NULL;
374         mp->mp_children = e_tmp;
375         mp->mp_next = NULL;
376
377         if ( monitor_cache_add( mi, e ) ) {
378 #ifdef NEW_LOGGING
379                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
380                         "unable to add entry '%s' to cache\n",
381                         SLAPD_MONITOR_DN ));
382 #else
383                 Debug( LDAP_DEBUG_ANY,
384                         "unable to add entry '%s' to cache\n%s%s",
385                         SLAPD_MONITOR_DN, "", "" );
386 #endif
387                 return -1;
388         }
389
390         be->be_private = mi;
391         
392         return 0;
393 }
394
395 int
396 monitor_back_open(
397         BackendInfo     *bi
398 )
399 {
400         BackendDB               *be;
401         struct monitorsubsys    *ms;
402         char                    *ndn;
403
404         /*
405          * adds the monitor backend
406          */
407         ndn = ch_strdup( SLAPD_MONITOR_DN );
408         dn_normalize( ndn );
409         be = select_backend( ndn , 0, 0 );
410         ch_free( ndn );
411         if ( be == NULL ) {
412 #ifdef NEW_LOGGING
413                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
414                         "unable to get monitor backend\n" ));
415 #else
416                 Debug( LDAP_DEBUG_ANY,
417                         "unable to get monitor backend\n%s%s%s", "", "", "" );
418 #endif
419                 return( -1 );
420         }
421
422         for ( ms = monitor_subsys; ms->mss_name != NULL; ms++ ) {
423                 if ( ms->mss_init && ( *ms->mss_init )( be ) ) {
424                         return( -1 );
425                 }
426         }
427
428         return( 0 );
429 }
430
431 int
432 monitor_back_config(
433         BackendInfo     *bi,
434         const char      *fname,
435         int             lineno,
436         int             argc,
437         char            **argv
438 )
439 {
440         /*
441          * eventually, will hold backend specific configuration parameters
442          */
443         return 0;
444 }
445
446 int
447 monitor_back_db_config(
448         Backend     *be,
449         const char  *fname,
450         int         lineno,
451         int         argc,
452         char        **argv
453 )
454 {
455 #ifdef NEW_LOGGING
456         LDAP_LOG(( "config", LDAP_DEBUG_NOTICE,
457                 "line %d of file '%s' will be ignored\n", lineno, fname ));
458 #else
459         Debug( LDAP_DEBUG_CONFIG, 
460                 "line %d of file '%s' will be ignored\n%s", lineno, fname, "" );
461 #endif
462         return( 0 );
463 }
464
465 int
466 monitor_back_db_destroy(
467         BackendDB       *be
468 )
469 {
470         /*
471          * FIXME: destroys all the data
472          */
473         return 0;
474 }
475