]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/init.c
5b46dca3026eaba9081698b3e2c7b28f2fe80890
[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  * database monitor can be defined once only
46  */
47 static int monitor_defined = 0;
48
49 /*
50  * used by many functions to add description to entries
51  */
52 AttributeDescription *monitor_ad_desc = NULL;
53
54 /*
55  * subsystem data
56  */
57 struct monitorsubsys monitor_subsys[] = {
58         { 
59                 SLAPD_MONITOR_LISTENER, SLAPD_MONITOR_LISTENER_NAME,    
60                 NULL, NULL, NULL,
61                 MONITOR_F_NONE,
62                 NULL,   /* init */
63                 NULL,   /* update */
64                 NULL,   /* create */
65                 NULL    /* modify */
66         }, { 
67                 SLAPD_MONITOR_DATABASE, SLAPD_MONITOR_DATABASE_NAME,    
68                 NULL, NULL, NULL,
69                 MONITOR_F_PERSISTENT_CH,
70                 monitor_subsys_database_init,
71                 NULL,   /* update */
72                 NULL,   /* create */
73                 NULL    /* modify */
74         }, { 
75                 SLAPD_MONITOR_BACKEND, SLAPD_MONITOR_BACKEND_NAME, 
76                 NULL, NULL, NULL,
77                 MONITOR_F_PERSISTENT_CH,
78                 monitor_subsys_backend_init,
79                 NULL,   /* update */
80                 NULL,   /* create */
81                 NULL    /* modify */
82         }, { 
83                 SLAPD_MONITOR_THREAD, SLAPD_MONITOR_THREAD_NAME,        
84                 NULL, NULL, NULL,
85                 MONITOR_F_NONE,
86                 monitor_subsys_thread_init,
87                 monitor_subsys_thread_update,
88                 NULL,   /* create */
89                 NULL    /* modify */
90         }, { 
91                 SLAPD_MONITOR_SASL, SLAPD_MONITOR_SASL_NAME,    
92                 NULL, NULL, NULL,
93                 MONITOR_F_NONE,
94                 NULL,   /* init */
95                 NULL,   /* update */
96                 NULL,   /* create */
97                 NULL    /* modify */
98         }, { 
99                 SLAPD_MONITOR_TLS, SLAPD_MONITOR_TLS_NAME,
100                 NULL, NULL, NULL,
101                 MONITOR_F_NONE,
102                 NULL,   /* init */
103                 NULL,   /* update */
104                 NULL,   /* create */
105                 NULL    /* modify */
106         }, { 
107                 SLAPD_MONITOR_CONN, SLAPD_MONITOR_CONN_NAME,
108                 NULL, NULL, NULL,
109                 MONITOR_F_VOLATILE_CH,
110                 monitor_subsys_conn_init,
111                 monitor_subsys_conn_update,
112                 monitor_subsys_conn_create,
113                 NULL    /* modify */
114         }, { 
115                 SLAPD_MONITOR_READW, SLAPD_MONITOR_READW_NAME,
116                 NULL, NULL, NULL,
117                 MONITOR_F_NONE,
118                 NULL,   /* init */
119                 monitor_subsys_readw_update,
120                 NULL,   /* create */
121                 NULL    /* modify */
122         }, { 
123                 SLAPD_MONITOR_WRITEW, SLAPD_MONITOR_WRITEW_NAME,
124                 NULL, NULL, NULL,
125                 MONITOR_F_NONE,
126                 NULL,   /* init */
127                 monitor_subsys_writew_update,
128                 NULL,   /* create */
129                 NULL    /* modify */
130         }, { 
131                 SLAPD_MONITOR_LOG, SLAPD_MONITOR_LOG_NAME,
132                 NULL, NULL, NULL,
133                 MONITOR_F_NONE,
134                 monitor_subsys_log_init,
135                 NULL,   /* update */
136                 NULL,   /* create */
137                 monitor_subsys_log_modify
138         }, { -1, NULL }
139 };
140
141 int
142 monitor_back_initialize(
143         BackendInfo     *bi
144 )
145 {
146         static char *controls[] = {
147                 LDAP_CONTROL_MANAGEDSAIT,
148                 NULL
149         };
150
151         bi->bi_controls = controls;
152
153         bi->bi_init = NULL;
154         bi->bi_open = monitor_back_open;
155         bi->bi_config = monitor_back_config;
156         bi->bi_close = NULL;
157         bi->bi_destroy = NULL;
158
159         bi->bi_db_init = monitor_back_db_init;
160         bi->bi_db_config = monitor_back_db_config;
161         bi->bi_db_open = NULL;
162         bi->bi_db_close = NULL;
163         bi->bi_db_destroy = monitor_back_db_destroy;
164
165         bi->bi_op_bind = monitor_back_bind;
166         bi->bi_op_unbind = NULL;
167         bi->bi_op_search = monitor_back_search;
168         bi->bi_op_compare = monitor_back_compare;
169         bi->bi_op_modify = monitor_back_modify;
170         bi->bi_op_modrdn = NULL;
171         bi->bi_op_add = NULL;
172         bi->bi_op_delete = NULL;
173         bi->bi_op_abandon = monitor_back_abandon;
174
175         bi->bi_extended = NULL;
176
177         bi->bi_entry_release_rw = NULL;
178         bi->bi_acl_group = NULL;
179         bi->bi_acl_attribute = NULL;
180         bi->bi_chk_referrals = NULL;
181
182         /*
183          * hooks for slap tools
184          */
185         bi->bi_tool_entry_open = NULL;
186         bi->bi_tool_entry_close = NULL;
187         bi->bi_tool_entry_first = NULL;
188         bi->bi_tool_entry_next = NULL;
189         bi->bi_tool_entry_get = NULL;
190         bi->bi_tool_entry_put = NULL;
191         bi->bi_tool_entry_reindex = NULL;
192         bi->bi_tool_sync = NULL;
193
194         bi->bi_connection_init = 0;
195         bi->bi_connection_destroy = 0;
196
197         return 0;
198 }
199
200 int
201 monitor_back_db_init(
202         BackendDB       *be
203 )
204 {
205         struct monitorinfo      *mi;
206         Entry                   *e, *e_tmp;
207         struct monitorentrypriv *mp;
208         int                     i;
209         char                    buf[1024], *ndn;
210         const char              *text;
211
212         if ( monitor_defined ) {
213 #ifdef NEW_LOGGING
214                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
215                         "only one monitor backend is allowed\n" ));
216 #else
217                 Debug( LDAP_DEBUG_ANY,
218                         "only one monitor backend is allowed\n%s%s%s",
219                         "", "", "" );
220 #endif
221                 return( -1 );
222         }
223         monitor_defined++;
224
225         ndn = ch_strdup( SLAPD_MONITOR_DN );
226         charray_add( &be->be_suffix, ndn );
227         dn_normalize( ndn );
228         ber_bvecadd( &be->be_nsuffix, ber_bvstr( ndn ) );
229         ch_free( 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         e_tmp = NULL;
247         for ( i = 0; monitor_subsys[ i ].mss_name != NULL; i++ ) {
248                 int len = strlen( monitor_subsys[ i ].mss_name );
249                 monitor_subsys[ i ].mss_rdn = ch_calloc( sizeof( char ), 
250                                 4 + len );
251                 strcpy( monitor_subsys[ i ].mss_rdn, "cn=" );
252                 strcat( monitor_subsys[ i ].mss_rdn, 
253                                 monitor_subsys[ i ].mss_name );
254
255                 monitor_subsys[ i ].mss_dn = ch_calloc( sizeof( char ), 
256                                 4 + len + sizeof( SLAPD_MONITOR_DN ) );
257                 strcpy( monitor_subsys[ i ].mss_dn, 
258                                 monitor_subsys[ i ].mss_rdn );
259                 strcat( monitor_subsys[ i ].mss_dn, "," );
260                 strcat( monitor_subsys[ i ].mss_dn, SLAPD_MONITOR_DN );
261
262                 monitor_subsys[ i ].mss_ndn 
263                         = ch_strdup( monitor_subsys[ i ].mss_dn );
264                 dn_normalize( monitor_subsys[ i ].mss_ndn );
265
266                 snprintf( buf, sizeof( buf ),
267                                 "dn: %s\n"
268                                 "objectClass: top\n"
269                                 "objectClass: LDAPsubEntry\n"
270 #ifdef SLAPD_MONITORSUBENTRY
271                                 "objectClass: monitorSubEntry\n"
272 #else /* !SLAPD_MONITORSUBENTRY */
273                                 "objectClass: extensibleObject\n"
274 #endif /* !SLAPD_MONITORSUBENTRY */
275                                 "cn: %s\n",
276                                 monitor_subsys[ i ].mss_dn,
277                                 monitor_subsys[ i ].mss_name );
278                 
279                 e = str2entry( buf );
280                 
281                 if ( e == NULL) {
282 #ifdef NEW_LOGGING
283                         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
284                                 "unable to create '%s' entry\n", 
285                                 monitor_subsys[ i ].mss_dn ));
286 #else
287                         Debug( LDAP_DEBUG_ANY,
288                                 "unable to create '%s' entry\n%s%s", 
289                                 monitor_subsys[ i ].mss_dn, "", "" );
290 #endif
291                         return( -1 );
292                 }
293
294                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
295                 e->e_private = ( void * )mp;
296                 mp->mp_info = &monitor_subsys[ i ];
297                 mp->mp_children = NULL;
298                 mp->mp_next = e_tmp;
299                 mp->mp_flags = monitor_subsys[ i ].mss_flags;
300
301                 if ( monitor_cache_add( mi, e ) ) {
302 #ifdef NEW_LOGGING
303                         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
304                                 "unable to add entry '%s' to cache\n",
305                                 monitor_subsys[ i ].mss_dn ));
306 #else
307                         Debug( LDAP_DEBUG_ANY,
308                                 "unable to add entry '%s' to cache\n%s%s",
309                                 monitor_subsys[ i ].mss_dn, "", "" );
310 #endif
311                         return -1;
312                 }
313
314                 e_tmp = e;
315         }
316
317         /*
318          * creates the "cn=Monitor" entry 
319          * and all the subsystem specific entries
320          */
321         snprintf( buf, sizeof( buf ), 
322                         "dn: %s\n"
323                         "objectClass: top\n"
324                         "objectClass: LDAPsubEntry\n"
325 #ifdef SLAPD_MONITORSUBENTRY
326                         "objectClass: monitorSubEntry\n"
327 #else /* !SLAPD_MONITORSUBENTRY */
328                         "objectClass: extensibleObject\n"
329 #endif /* !SLAPD_MONITORSUBENTRY */
330                         "cn: Monitor\n"
331                         "description: %s",
332                         SLAPD_MONITOR_DN,
333                         /* (char *) Versionstr */ "slapd 2.0"
334                         );
335         e = str2entry( buf );
336         if ( e == NULL) {
337 #ifdef NEW_LOGGING
338                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
339                         "unable to create '%s' entry\n",
340                         SLAPD_MONITOR_DN ));
341 #else
342                 Debug( LDAP_DEBUG_ANY,
343                         "unable to create '%s' entry\n%s%s",
344                         SLAPD_MONITOR_DN, "", "" );
345 #endif
346                 return( -1 );
347         }
348
349         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
350         e->e_private = ( void * )mp;
351
352         mp->mp_info = NULL;
353         mp->mp_children = e_tmp;
354         mp->mp_next = NULL;
355
356         if ( monitor_cache_add( mi, e ) ) {
357 #ifdef NEW_LOGGING
358                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
359                         "unable to add entry '%s' to cache\n",
360                         SLAPD_MONITOR_DN ));
361 #else
362                 Debug( LDAP_DEBUG_ANY,
363                         "unable to add entry '%s' to cache\n%s%s",
364                         SLAPD_MONITOR_DN, "", "" );
365 #endif
366                 return -1;
367         }
368
369         be->be_private = mi;
370         
371         return 0;
372 }
373
374 int
375 monitor_back_open(
376         BackendInfo     *bi
377 )
378 {
379         BackendDB               *be;
380         struct monitorsubsys    *ms;
381         char                    *ndn;
382
383         /*
384          * adds the monitor backend
385          */
386         ndn = ch_strdup( SLAPD_MONITOR_DN );
387         dn_normalize( ndn );
388         be = select_backend( ndn , 0 );
389         ch_free( ndn );
390         if ( be == NULL ) {
391 #ifdef NEW_LOGGING
392                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
393                         "unable to get monitor backend\n" ));
394 #else
395                 Debug( LDAP_DEBUG_ANY,
396                         "unable to get monitor backend\n%s%s%s", "", "", "" );
397 #endif
398                 return( -1 );
399         }
400
401         for ( ms = monitor_subsys; ms->mss_name != NULL; ms++ ) {
402                 if ( ms->mss_init && ( *ms->mss_init )( be ) ) {
403                         return( -1 );
404                 }
405         }
406
407         return( 0 );
408 }
409
410 int
411 monitor_back_config(
412         BackendInfo     *bi,
413         const char      *fname,
414         int             lineno,
415         int             argc,
416         char            **argv
417 )
418 {
419         /*
420          * eventually, will hold backend specific configuration parameters
421          */
422         return 0;
423 }
424
425 int
426 monitor_back_db_config(
427         Backend     *be,
428         const char  *fname,
429         int         lineno,
430         int         argc,
431         char        **argv
432 )
433 {
434 #ifdef NEW_LOGGING
435         LDAP_LOG(( "config", LDAP_DEBUG_NOTICE,
436                 "line %d of file '%s' will be ignored\n", lineno, fname ));
437 #else
438         Debug( LDAP_DEBUG_CONFIG, 
439                 "line %d of file '%s' will be ignored\n%s", lineno, fname, "" );
440 #endif
441         return( 0 );
442 }
443
444 int
445 monitor_back_db_destroy(
446         BackendDB       *be
447 )
448 {
449         /*
450          * FIXME: destroys all the data
451          */
452         return 0;
453 }
454