]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/init.c
fix attr nvals & more improvements
[openldap] / servers / slapd / back-monitor / init.c
1 /* init.c - initialize monitor backend */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
8  * 
9  * This work has beed deveolped for the OpenLDAP Foundation 
10  * in the hope that it may be useful to the Open Source community, 
11  * but WITHOUT ANY WARRANTY.
12  * 
13  * Permission is granted to anyone to use this software for any purpose
14  * on any computer system, and to alter it and redistribute it, subject
15  * to the following restrictions:
16  * 
17  * 1. The author and SysNet s.n.c. are not responsible for the consequences
18  *    of use of this software, no matter how awful, even if they arise from
19  *    flaws in it.
20  * 
21  * 2. The origin of this software must not be misrepresented, either by
22  *    explicit claim or by omission.  Since few users ever read sources,
23  *    credits should appear in the documentation.
24  * 
25  * 3. Altered versions must be plainly marked as such, and must not be
26  *    misrepresented as being the original software.  Since few users
27  *    ever read sources, credits should appear in the documentation.
28  *    SysNet s.n.c. cannot be responsible for the consequences of the
29  *    alterations.
30  * 
31  * 4. This notice may not be removed or altered.
32  */
33
34 #include "portable.h"
35
36 #include <stdio.h>
37 #include <ac/string.h>
38
39 #include "slap.h"
40 #include "lber_pvt.h"
41 #include "back-monitor.h"
42
43 /*
44  * used by many functions to add description to entries
45  */
46 AttributeDescription *monitor_ad_desc = NULL;
47 slap_mr_normalize_func *monitor_ad_normalize = NULL;
48 BackendDB *be_monitor = NULL;
49
50 /*
51  * subsystem data
52  */
53 struct monitorsubsys monitor_subsys[] = {
54         { 
55                 SLAPD_MONITOR_LISTENER, SLAPD_MONITOR_LISTENER_NAME,    
56                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
57                 MONITOR_F_PERSISTENT_CH,
58                 monitor_subsys_listener_init,
59                 NULL,   /* update */
60                 NULL,   /* create */
61                 NULL    /* modify */
62         }, { 
63                 SLAPD_MONITOR_DATABASE, SLAPD_MONITOR_DATABASE_NAME,    
64                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
65                 MONITOR_F_PERSISTENT_CH,
66                 monitor_subsys_database_init,
67                 NULL,   /* update */
68                 NULL,   /* create */
69                 NULL    /* modify */
70         }, { 
71                 SLAPD_MONITOR_BACKEND, SLAPD_MONITOR_BACKEND_NAME, 
72                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
73                 MONITOR_F_PERSISTENT_CH,
74                 monitor_subsys_backend_init,
75                 NULL,   /* update */
76                 NULL,   /* create */
77                 NULL    /* modify */
78         }, { 
79                 SLAPD_MONITOR_THREAD, SLAPD_MONITOR_THREAD_NAME,        
80                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
81                 MONITOR_F_NONE,
82                 monitor_subsys_thread_init,
83                 monitor_subsys_thread_update,
84                 NULL,   /* create */
85                 NULL    /* modify */
86         }, { 
87                 SLAPD_MONITOR_SASL, SLAPD_MONITOR_SASL_NAME,    
88                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
89                 MONITOR_F_NONE,
90                 NULL,   /* init */
91                 NULL,   /* update */
92                 NULL,   /* create */
93                 NULL    /* modify */
94         }, { 
95                 SLAPD_MONITOR_TLS, SLAPD_MONITOR_TLS_NAME,
96                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
97                 MONITOR_F_NONE,
98                 NULL,   /* init */
99                 NULL,   /* update */
100                 NULL,   /* create */
101                 NULL    /* modify */
102         }, { 
103                 SLAPD_MONITOR_CONN, SLAPD_MONITOR_CONN_NAME,
104                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
105                 MONITOR_F_VOLATILE_CH,
106                 monitor_subsys_conn_init,
107                 monitor_subsys_conn_update,
108                 monitor_subsys_conn_create,
109                 NULL    /* modify */
110         }, { 
111                 SLAPD_MONITOR_READW, SLAPD_MONITOR_READW_NAME,
112                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
113                 MONITOR_F_NONE,
114                 NULL,   /* init */
115                 monitor_subsys_readw_update,
116                 NULL,   /* create */
117                 NULL    /* modify */
118         }, { 
119                 SLAPD_MONITOR_WRITEW, SLAPD_MONITOR_WRITEW_NAME,
120                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
121                 MONITOR_F_NONE,
122                 NULL,   /* init */
123                 monitor_subsys_writew_update,
124                 NULL,   /* create */
125                 NULL    /* modify */
126         }, { 
127                 SLAPD_MONITOR_LOG, SLAPD_MONITOR_LOG_NAME,
128                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
129                 MONITOR_F_NONE,
130                 monitor_subsys_log_init,
131                 NULL,   /* update */
132                 NULL,   /* create */
133                 monitor_subsys_log_modify
134         }, { 
135                 SLAPD_MONITOR_OPS, SLAPD_MONITOR_OPS_NAME,
136                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
137                 MONITOR_F_PERSISTENT_CH,
138                 monitor_subsys_ops_init,
139                 monitor_subsys_ops_update,
140                 NULL,   /* create */
141                 NULL,   /* modify */
142         }, { 
143                 SLAPD_MONITOR_SENT, SLAPD_MONITOR_SENT_NAME,
144                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
145                 MONITOR_F_PERSISTENT_CH,
146                 monitor_subsys_sent_init,
147                 monitor_subsys_sent_update,
148                 NULL,   /* create */
149                 NULL,   /* modify */
150         }, { 
151                 SLAPD_MONITOR_TIME, SLAPD_MONITOR_TIME_NAME,
152                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
153                 MONITOR_F_PERSISTENT_CH,
154                 monitor_subsys_time_init,
155                 monitor_subsys_time_update,
156                 NULL,   /* create */
157                 NULL,   /* modify */
158         }, { -1, NULL }
159 };
160
161 #ifdef SLAPD_MONITOR_DYNAMIC
162
163 int
164 back_monitor_LTX_init_module( int argc, char *argv[] )
165 {
166         BackendInfo bi;
167
168         memset( &bi, '\0', sizeof(bi) );
169         bi.bi_type = "monitor";
170         bi.bi_init = monitor_back_initialize;
171         backend_add( &bi );
172         return 0;
173 }
174
175 #endif /* SLAPD_MONITOR_DYNAMIC */
176
177 int
178 monitor_back_initialize(
179         BackendInfo     *bi
180 )
181 {
182         static char *controls[] = {
183                 LDAP_CONTROL_MANAGEDSAIT,
184                 LDAP_CONTROL_VALUESRETURNFILTER,
185                 NULL
186         };
187
188         bi->bi_controls = controls;
189
190         bi->bi_init = 0;
191         bi->bi_open = 0;
192         bi->bi_config = monitor_back_config;
193         bi->bi_close = 0;
194         bi->bi_destroy = 0;
195
196         bi->bi_db_init = monitor_back_db_init;
197         bi->bi_db_config = monitor_back_db_config;
198         bi->bi_db_open = monitor_back_db_open;
199         bi->bi_db_close = 0;
200         bi->bi_db_destroy = monitor_back_db_destroy;
201
202         bi->bi_op_bind = monitor_back_bind;
203         bi->bi_op_unbind = 0;
204         bi->bi_op_search = monitor_back_search;
205         bi->bi_op_compare = monitor_back_compare;
206         bi->bi_op_modify = monitor_back_modify;
207         bi->bi_op_modrdn = 0;
208         bi->bi_op_add = 0;
209         bi->bi_op_delete = 0;
210         bi->bi_op_abandon = 0;
211
212         bi->bi_extended = 0;
213
214         bi->bi_entry_release_rw = 0;
215         bi->bi_chk_referrals = 0;
216         bi->bi_operational = monitor_back_operational;
217
218         /*
219          * hooks for slap tools
220          */
221         bi->bi_tool_entry_open = 0;
222         bi->bi_tool_entry_close = 0;
223         bi->bi_tool_entry_first = 0;
224         bi->bi_tool_entry_next = 0;
225         bi->bi_tool_entry_get = 0;
226         bi->bi_tool_entry_put = 0;
227         bi->bi_tool_entry_reindex = 0;
228         bi->bi_tool_sync = 0;
229
230         bi->bi_connection_init = 0;
231         bi->bi_connection_destroy = 0;
232
233         return 0;
234 }
235
236 int
237 monitor_back_db_init(
238         BackendDB       *be
239 )
240 {
241         struct monitorinfo      *mi;
242         Entry                   *e, *e_tmp;
243         struct monitorentrypriv *mp;
244         int                     i, rc;
245         char                    buf[1024], *end_of_line;
246         struct berval           dn, ndn;
247         const char              *text;
248         struct berval           bv;
249
250         /*
251          * database monitor can be defined once only
252          */
253         if ( be_monitor ) {
254 #ifdef NEW_LOGGING
255                 LDAP_LOG( OPERATION, CRIT,
256                         "only one monitor backend is allowed\n" , 0, 0, 0);
257 #else
258                 Debug( LDAP_DEBUG_ANY,
259                         "only one monitor backend is allowed\n%s%s%s",
260                         "", "", "" );
261 #endif
262                 return( -1 );
263         }
264         be_monitor = be;
265
266         /* indicate system schema supported */
267         be->be_flags |= SLAP_BFLAG_MONITOR;
268
269         dn.bv_val = SLAPD_MONITOR_DN;
270         dn.bv_len = sizeof( SLAPD_MONITOR_DN ) - 1;
271
272         rc = dnNormalize2( NULL, &dn, &ndn );
273         if( rc != LDAP_SUCCESS ) {
274 #ifdef NEW_LOGGING
275                 LDAP_LOG( OPERATION, CRIT,
276                         "monitor DN \"" SLAPD_MONITOR_DN "\" backend is allowed\n" , 0, 0, 0 );
277 #else
278                 Debug( LDAP_DEBUG_ANY,
279                         "monitor DN \"" SLAPD_MONITOR_DN "\" backend is allowed\n",
280                         0, 0, 0 );
281 #endif
282                 return -1;
283         }
284
285         ber_dupbv( &bv, &dn );
286         ber_bvarray_add( &be->be_suffix, &bv );
287         ber_bvarray_add( &be->be_nsuffix, &ndn );
288
289         mi = ( struct monitorinfo * )ch_calloc( sizeof( struct monitorinfo ), 1 );
290         ldap_pvt_thread_mutex_init( &mi->mi_cache_mutex );
291
292         if ( slap_str2ad( "description", &monitor_ad_desc, &text ) ) {
293 #ifdef NEW_LOGGING
294                 LDAP_LOG( OPERATION, CRIT,
295                         "monitor_back_db_init: %s\n", text, 0, 0 );
296 #else
297                 Debug( LDAP_DEBUG_ANY,
298                         "monitor_subsys_backend_init: %s\n%s%s", 
299                         text, "", "" );
300 #endif
301                 return( -1 );
302         }
303
304         if ( monitor_ad_desc->ad_type->sat_equality ) {
305                 monitor_ad_normalize = monitor_ad_desc->ad_type->sat_equality->smr_normalize;
306         }
307
308         /*      
309          * Create all the subsystem specific entries
310          */
311         e_tmp = NULL;
312         for ( i = 0; monitor_subsys[ i ].mss_name != NULL; i++ ) {
313                 int             len = strlen( monitor_subsys[ i ].mss_name );
314                 struct berval   dn;
315                 int             rc;
316
317                 dn.bv_len = len + sizeof( "cn=" ) - 1;
318                 dn.bv_val = ch_calloc( sizeof( char ), dn.bv_len + 1 );
319                 strcpy( dn.bv_val, "cn=" );
320                 strcat( dn.bv_val, monitor_subsys[ i ].mss_name );
321                 rc = dnPretty2( NULL, &dn, &monitor_subsys[ i ].mss_rdn );
322                 free( dn.bv_val );
323                 if ( rc != LDAP_SUCCESS ) {
324 #ifdef NEW_LOGGING
325                         LDAP_LOG( OPERATION, CRIT,
326                                 "monitor RDN \"%s\" is invalid\n", 
327                                 dn.bv_val, 0, 0 );
328 #else
329                         Debug( LDAP_DEBUG_ANY,
330                                 "monitor RDN \"%s\" is invalid\n", 
331                                 dn.bv_val, 0, 0 );
332 #endif
333                         return( -1 );
334                 }
335
336                 dn.bv_len += sizeof( SLAPD_MONITOR_DN ); /* 1 for the , */
337                 dn.bv_val = ch_malloc( dn.bv_len + 1 );
338                 strcpy( dn.bv_val , monitor_subsys[ i ].mss_rdn.bv_val );
339                 strcat( dn.bv_val, "," SLAPD_MONITOR_DN );
340                 rc = dnPrettyNormal( NULL, &dn, &monitor_subsys[ i ].mss_dn,
341                         &monitor_subsys[ i ].mss_ndn );
342                 free( dn.bv_val );
343                 if ( rc != LDAP_SUCCESS ) {
344 #ifdef NEW_LOGGING
345                         LDAP_LOG( OPERATION, CRIT,
346                                 "monitor DN \"%s\" is invalid\n", 
347                                 dn.bv_val, 0, 0 );
348 #else
349                         Debug( LDAP_DEBUG_ANY,
350                                 "monitor DN \"%s\" is invalid\n", 
351                                 dn.bv_val, 0, 0 );
352 #endif
353                         return( -1 );
354                 }
355
356                 snprintf( buf, sizeof( buf ),
357                                 "dn: %s\n"
358                                 SLAPD_MONITOR_OBJECTCLASSES 
359                                 "cn: %s\n",
360                                 monitor_subsys[ i ].mss_dn.bv_val,
361                                 monitor_subsys[ i ].mss_name );
362                 
363                 e = str2entry( buf );
364                 
365                 if ( e == NULL) {
366 #ifdef NEW_LOGGING
367                         LDAP_LOG( OPERATION, CRIT,
368                                 "unable to create '%s' entry\n", 
369                                 monitor_subsys[ i ].mss_dn.bv_val, 0, 0 );
370 #else
371                         Debug( LDAP_DEBUG_ANY,
372                                 "unable to create '%s' entry\n", 
373                                 monitor_subsys[ i ].mss_dn.bv_val, 0, 0 );
374 #endif
375                         return( -1 );
376                 }
377
378                 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
379                 e->e_private = ( void * )mp;
380                 mp->mp_info = &monitor_subsys[ i ];
381                 mp->mp_children = NULL;
382                 mp->mp_next = e_tmp;
383                 mp->mp_flags = monitor_subsys[ i ].mss_flags;
384
385                 if ( monitor_cache_add( mi, e ) ) {
386 #ifdef NEW_LOGGING
387                         LDAP_LOG( OPERATION, CRIT,
388                                 "unable to add entry '%s' to cache\n",
389                                 monitor_subsys[ i ].mss_dn.bv_val, 0, 0 );
390 #else
391                         Debug( LDAP_DEBUG_ANY,
392                                 "unable to add entry '%s' to cache\n",
393                                 monitor_subsys[ i ].mss_dn.bv_val, 0, 0 );
394 #endif
395                         return -1;
396                 }
397
398                 e_tmp = e;
399         }
400
401         /*
402          * creates the "cn=Monitor" entry 
403          */
404         snprintf( buf, sizeof( buf ), 
405                         "dn: " SLAPD_MONITOR_DN "\n"
406                         "objectClass: top\n"
407                         "objectClass: monitor\n"
408                         "objectClass: extensibleObject\n"
409                         "structuralObjectClass: monitor\n"
410                         "cn: Monitor" );
411
412         e = str2entry( buf );
413         if ( e == NULL) {
414 #ifdef NEW_LOGGING
415                 LDAP_LOG( OPERATION, CRIT,
416                         "unable to create '%s' entry\n",
417                         SLAPD_MONITOR_DN, 0, 0 );
418 #else
419                 Debug( LDAP_DEBUG_ANY,
420                         "unable to create '%s' entry\n%s%s",
421                         SLAPD_MONITOR_DN, "", "" );
422 #endif
423                 return( -1 );
424         }
425         bv.bv_val = (char *) Versionstr;
426         end_of_line = strchr( Versionstr, '\n' );
427         if ( end_of_line ) {
428                 bv.bv_len = end_of_line - Versionstr;
429         } else {
430                 bv.bv_len = strlen( Versionstr );
431         }
432         if ( attr_merge_one( e, monitor_ad_desc, &bv, NULL ) ) {
433 #ifdef NEW_LOGGING
434                 LDAP_LOG( OPERATION, CRIT,
435                         "unable to add description to '%s' entry\n",
436                         SLAPD_MONITOR_DN, 0, 0 );
437 #else
438                 Debug( LDAP_DEBUG_ANY,
439                         "unable to add description to '%s' entry\n%s%s",
440                         SLAPD_MONITOR_DN, "", "" );
441 #endif
442                 return( -1 );
443         }
444
445         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
446         e->e_private = ( void * )mp;
447
448         mp->mp_info = NULL;
449         mp->mp_children = e_tmp;
450         mp->mp_next = NULL;
451
452         if ( monitor_cache_add( mi, e ) ) {
453 #ifdef NEW_LOGGING
454                 LDAP_LOG( OPERATION, CRIT,
455                         "unable to add entry '%s' to cache\n",
456                         SLAPD_MONITOR_DN, 0, 0 );
457 #else
458                 Debug( LDAP_DEBUG_ANY,
459                         "unable to add entry '%s' to cache\n%s%s",
460                         SLAPD_MONITOR_DN, "", "" );
461 #endif
462                 return -1;
463         }
464
465         be->be_private = mi;
466         
467         return 0;
468 }
469
470 int
471 monitor_back_db_open(
472         BackendDB       *be
473 )
474 {
475         struct monitorsubsys    *ms;
476
477         assert( be );
478
479         /*
480          * opens the monitor backend
481          */
482         for ( ms = monitor_subsys; ms->mss_name != NULL; ms++ ) {
483                 if ( ms->mss_init && ( *ms->mss_init )( be ) ) {
484                         return( -1 );
485                 }
486         }
487
488         return( 0 );
489 }
490
491 int
492 monitor_back_config(
493         BackendInfo     *bi,
494         const char      *fname,
495         int             lineno,
496         int             argc,
497         char            **argv
498 )
499 {
500         /*
501          * eventually, will hold backend specific configuration parameters
502          */
503         return 0;
504 }
505
506 int
507 monitor_back_db_config(
508         Backend     *be,
509         const char  *fname,
510         int         lineno,
511         int         argc,
512         char        **argv
513 )
514 {
515         /*
516          * eventually, will hold database specific configuration parameters
517          */
518 #ifdef NEW_LOGGING
519         LDAP_LOG( CONFIG, INFO,
520                 "line %d of file '%s' will be ignored\n", lineno, fname, 0 );
521 #else
522         Debug( LDAP_DEBUG_CONFIG, 
523                 "line %d of file '%s' will be ignored\n%s", lineno, fname, "" );
524 #endif
525         return( 0 );
526 }
527
528 int
529 monitor_back_db_destroy(
530         BackendDB       *be
531 )
532 {
533         /*
534          * FIXME: destroys all the data
535          */
536         return 0;
537 }
538