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