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