]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/log.c
4d3df1b06163c1c076303da6d275e0a4f27bb87d
[openldap] / servers / slapd / back-monitor / log.c
1 /* log.c - deal with log subsystem */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2001-2004 The OpenLDAP Foundation.
6  * Portions Copyright 2001-2003 Pierangelo Masarati.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Pierangelo Masarati for inclusion
19  * in OpenLDAP Software.
20  */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25
26 #include <ac/string.h>
27
28 #include "slap.h"
29 #include <lber_pvt.h>
30 #include "lutil.h"
31 #include "ldif.h"
32 #include "back-monitor.h"
33
34 /*
35  * log mutex
36  */
37 ldap_pvt_thread_mutex_t         monitor_log_mutex;
38
39 static struct {
40         int i;
41         struct berval s;
42         struct berval n;
43 } int_2_level[] = {
44         { LDAP_DEBUG_TRACE,     BER_BVC("Trace"),       BER_BVNULL },
45         { LDAP_DEBUG_PACKETS,   BER_BVC("Packets"),     BER_BVNULL },
46         { LDAP_DEBUG_ARGS,      BER_BVC("Args"),        BER_BVNULL },
47         { LDAP_DEBUG_CONNS,     BER_BVC("Conns"),       BER_BVNULL },
48         { LDAP_DEBUG_BER,       BER_BVC("BER"), BER_BVNULL },
49         { LDAP_DEBUG_FILTER,    BER_BVC("Filter"),      BER_BVNULL },
50         { LDAP_DEBUG_CONFIG,    BER_BVC("Config"),      BER_BVNULL },   /* useless */
51         { LDAP_DEBUG_ACL,       BER_BVC("ACL"), BER_BVNULL },
52         { LDAP_DEBUG_STATS,     BER_BVC("Stats"),       BER_BVNULL },
53         { LDAP_DEBUG_STATS2,    BER_BVC("Stats2"),      BER_BVNULL },
54         { LDAP_DEBUG_SHELL,     BER_BVC("Shell"),       BER_BVNULL },
55         { LDAP_DEBUG_PARSE,     BER_BVC("Parse"),       BER_BVNULL },
56         { LDAP_DEBUG_CACHE,     BER_BVC("Cache"),       BER_BVNULL },
57         { LDAP_DEBUG_INDEX,     BER_BVC("Index"),       BER_BVNULL },
58         { 0,                    BER_BVNULL,     BER_BVNULL }
59 };
60
61 static int loglevel2int( struct berval *l );
62 static int int2loglevel( int n );
63
64 static int add_values( Entry *e, Modification *mod, int *newlevel );
65 static int delete_values( Entry *e, Modification *mod, int *newlevel );
66 static int replace_values( Entry *e, Modification *mod, int *newlevel );
67
68 /*
69  * initializes log subentry
70  */
71 int
72 monitor_subsys_log_init(
73         BackendDB               *be,
74         monitor_subsys_t        *ms
75 )
76 {
77         monitor_info_t  *mi;
78         Entry           *e;
79         int             i;
80         struct berval   desc[] = {
81                 BER_BVC("This entry allows to set the log level runtime."),
82                 BER_BVC("Set the attribute 'managedInfo' to the desired log levels."),
83                 BER_BVNULL
84         };
85
86         ldap_pvt_thread_mutex_init( &monitor_log_mutex );
87
88         mi = ( monitor_info_t * )be->be_private;
89
90         if ( monitor_cache_get( mi, &ms->mss_ndn, 
91                                 &e ) ) {
92                 Debug( LDAP_DEBUG_ANY,
93                         "monitor_subsys_log_init: "
94                         "unable to get entry \"%s\"\n",
95                         ms->mss_ndn.bv_val, 0, 0 );
96                 return( -1 );
97         }
98
99         /* initialize the debug level(s) */
100         for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
101                 if ( mi->mi_ad_managedInfo->ad_type->sat_equality->smr_normalize ) {
102                         int     rc;
103
104                         rc = (*mi->mi_ad_managedInfo->ad_type->sat_equality->smr_normalize)(
105                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
106                                         mi->mi_ad_managedInfo->ad_type->sat_syntax,
107                                         mi->mi_ad_managedInfo->ad_type->sat_equality,
108                                         &int_2_level[ i ].s,
109                                         &int_2_level[ i ].n, NULL );
110                         if ( rc ) {
111                                 return( -1 );
112                         }
113                 }
114
115                 if ( int_2_level[ i ].i & ldap_syslog ) {
116                         attr_merge_one( e, mi->mi_ad_managedInfo,
117                                         &int_2_level[ i ].s,
118                                         &int_2_level[ i ].n );
119                 }
120         }
121
122         attr_merge( e, mi->mi_ad_description, desc, NULL );
123
124         monitor_cache_release( mi, e );
125
126         return( 0 );
127 }
128
129 int 
130 monitor_subsys_log_modify( 
131         Operation               *op,
132         Entry                   *e
133 )
134 {
135         monitor_info_t  *mi = ( monitor_info_t * )op->o_bd->be_private;
136         int             rc = LDAP_OTHER;
137         int             newlevel = ldap_syslog;
138         Attribute       *save_attrs;
139         Modifications   *modlist = op->oq_modify.rs_modlist;
140         Modifications   *ml;
141
142         ldap_pvt_thread_mutex_lock( &monitor_log_mutex );
143
144         save_attrs = e->e_attrs;
145         e->e_attrs = attrs_dup( e->e_attrs );
146
147         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
148                 Modification    *mod = &ml->sml_mod;
149
150                 /*
151                  * accept all operational attributes
152                  */
153                 if ( is_at_operational( mod->sm_desc->ad_type ) ) {
154                         ( void ) attr_delete( &e->e_attrs, mod->sm_desc );
155                         rc = attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues );
156                         if ( rc != 0 ) {
157                                 rc = LDAP_OTHER;
158                                 break;
159                         }
160                         continue;
161
162                 /*
163                  * only the monitor description attribute can be modified
164                  */
165                 } else if ( mod->sm_desc != mi->mi_ad_managedInfo) {
166                         rc = LDAP_UNWILLING_TO_PERFORM;
167                         break;
168                 }
169
170                 switch ( mod->sm_op ) {
171                 case LDAP_MOD_ADD:
172                         rc = add_values( e, mod, &newlevel );
173                         break;
174                         
175                 case LDAP_MOD_DELETE:
176                         rc = delete_values( e, mod, &newlevel );
177                         break;
178
179                 case LDAP_MOD_REPLACE:
180                         rc = replace_values( e, mod, &newlevel );
181                         break;
182
183                 default:
184                         rc = LDAP_OTHER;
185                         break;
186                 }
187
188                 if ( rc != LDAP_SUCCESS ) {
189                         break;
190                 }
191         }
192
193         /* set the new debug level */
194         if ( rc == LDAP_SUCCESS ) {
195                 const char      *text;
196                 static char     textbuf[ BACKMONITOR_BUFSIZE ];
197
198                 /* check for abandon */
199                 if ( op->o_abandon ) {
200                         rc = SLAPD_ABANDON;
201
202                         goto cleanup;
203                 }
204
205                 /* check that the entry still obeys the schema */
206                 rc = entry_schema_check( be_monitor, e, save_attrs, 
207                                 &text, textbuf, sizeof( textbuf ) );
208                 if ( rc != LDAP_SUCCESS ) {
209                         goto cleanup;
210                 }
211
212                 /*
213                  * Do we need to protect this with a mutex?
214                  */
215                 ldap_syslog = newlevel;
216
217 #if 0   /* debug rather than log */
218                 slap_debug = newlevel;
219                 lutil_set_debug_level( "slapd", slap_debug );
220                 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
221                 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
222                 ldif_debug = slap_debug;
223 #endif
224         }
225
226 cleanup:;
227         if ( rc == LDAP_SUCCESS ) {
228                 attrs_free( save_attrs );
229
230         } else {
231                 attrs_free( e->e_attrs );
232                 e->e_attrs = save_attrs;
233         }
234         
235         ldap_pvt_thread_mutex_unlock( &monitor_log_mutex );
236
237         return( rc );
238 }
239
240 static int
241 loglevel2int( struct berval *l )
242 {
243         int             i;
244         
245         for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
246                 if ( l->bv_len != int_2_level[ i ].s.bv_len ) {
247                         continue;
248                 }
249
250                 if ( strcasecmp( l->bv_val, int_2_level[ i ].s.bv_val ) == 0 ) {
251                         return int_2_level[ i ].i;
252                 }
253         }
254
255         return 0;
256 }
257
258 static int
259 int2loglevel( int n )
260 {
261         int             i;
262         
263         for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
264                 if ( int_2_level[ i ].i == n ) {
265                         return i;
266                 }
267         }
268
269         return -1;
270 }
271
272 static int
273 check_constraints( Modification *mod, int *newlevel )
274 {
275         int             i;
276
277         for ( i = 0; mod->sm_values && !BER_BVISNULL( &mod->sm_values[ i ] ); i++ ) {
278                 int             l;
279                 
280                 l = loglevel2int( &mod->sm_values[ i ] );
281                 if ( !l ) {
282                         return LDAP_CONSTRAINT_VIOLATION;
283                 }
284
285                 if ( ( l = int2loglevel( l ) ) == -1 ) {
286                         return LDAP_OTHER;
287                 }
288
289                 assert( int_2_level[ l ].s.bv_len
290                                 == mod->sm_values[ i ].bv_len );
291                 
292                 AC_MEMCPY( mod->sm_values[ i ].bv_val,
293                                 int_2_level[ l ].s.bv_val,
294                                 int_2_level[ l ].s.bv_len );
295
296                 AC_MEMCPY( mod->sm_nvalues[ i ].bv_val,
297                                 int_2_level[ l ].n.bv_val,
298                                 int_2_level[ l ].n.bv_len );
299
300                 *newlevel |= l;
301         }
302
303         return LDAP_SUCCESS;
304 }       
305
306 static int 
307 add_values( Entry *e, Modification *mod, int *newlevel )
308 {
309         Attribute       *a;
310         int             i, rc;
311         MatchingRule    *mr = mod->sm_desc->ad_type->sat_equality;
312
313         rc = check_constraints( mod, newlevel );
314         if ( rc != LDAP_SUCCESS ) {
315                 return rc;
316         }
317
318         a = attr_find( e->e_attrs, mod->sm_desc );
319
320         if ( a != NULL ) {
321                 /* "description" SHOULD have appropriate rules ... */
322                 if ( mr == NULL || !mr->smr_match ) {
323                         return LDAP_INAPPROPRIATE_MATCHING;
324                 }
325
326                 for ( i = 0; !BER_BVISNULL( &mod->sm_values[ i ] ); i++ ) {
327                         int rc;
328                         int j;
329                         const char *text = NULL;
330                         struct berval asserted;
331
332                         rc = asserted_value_validate_normalize(
333                                 mod->sm_desc, mr, SLAP_MR_EQUALITY,
334                                 &mod->sm_values[ i ], &asserted, &text, NULL );
335
336                         if ( rc != LDAP_SUCCESS ) {
337                                 return rc;
338                         }
339
340                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ ) {
341                                 int match;
342                                 int rc = value_match( &match, mod->sm_desc, mr,
343                                         0, &a->a_vals[ j ], &asserted, &text );
344
345                                 if ( rc == LDAP_SUCCESS && match == 0 ) {
346                                         free( asserted.bv_val );
347                                         return LDAP_TYPE_OR_VALUE_EXISTS;
348                                 }
349                         }
350
351                         free( asserted.bv_val );
352                 }
353         }
354
355         /* no - add them */
356         rc = attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues );
357         if ( rc != LDAP_SUCCESS ) {
358                 /* this should return result of attr_mergeit */
359                 return rc;
360         }
361
362         return LDAP_SUCCESS;
363 }
364
365 static int
366 delete_values( Entry *e, Modification *mod, int *newlevel )
367 {
368         int             i, j, k, found, rc, nl = 0;
369         Attribute       *a;
370         MatchingRule    *mr = mod->sm_desc->ad_type->sat_equality;
371
372         rc = check_constraints( mod, &nl );
373         if ( rc != LDAP_SUCCESS ) {
374                 return rc;
375         }
376
377         *newlevel &= ~nl;
378
379         /* delete the entire attribute */
380         if ( mod->sm_values == NULL ) {
381                 int rc = attr_delete( &e->e_attrs, mod->sm_desc );
382
383                 if ( rc ) {
384                         rc = LDAP_NO_SUCH_ATTRIBUTE;
385                 } else {
386                         *newlevel = 0;
387                         rc = LDAP_SUCCESS;
388                 }
389                 return rc;
390         }
391
392         if ( mr == NULL || !mr->smr_match ) {
393                 /* disallow specific attributes from being deleted if
394                  * no equality rule */
395                 return LDAP_INAPPROPRIATE_MATCHING;
396         }
397
398         /* delete specific values - find the attribute first */
399         if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
400                 return( LDAP_NO_SUCH_ATTRIBUTE );
401         }
402
403         /* find each value to delete */
404         for ( i = 0; !BER_BVISNULL( &mod->sm_values[ i ] ); i++ ) {
405                 int rc;
406                 const char *text = NULL;
407
408                 struct berval asserted;
409
410                 rc = asserted_value_validate_normalize(
411                                 mod->sm_desc, mr, SLAP_MR_EQUALITY,
412                                 &mod->sm_values[ i ], &asserted, &text, NULL );
413
414                 if( rc != LDAP_SUCCESS ) return rc;
415
416                 found = 0;
417                 for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ ) {
418                         int match;
419                         int rc = value_match( &match, mod->sm_desc, mr,
420                                 0,
421                                 &a->a_vals[ j ], &asserted, &text );
422
423                         if( rc == LDAP_SUCCESS && match != 0 ) {
424                                 continue;
425                         }
426
427                         /* found a matching value */
428                         found = 1;
429
430                         /* delete it */
431                         free( a->a_vals[ j ].bv_val );
432                         for ( k = j + 1; !BER_BVISNULL( &a->a_vals[ k ] ); k++ ) {
433                                 a->a_vals[ k - 1 ] = a->a_vals[ k ];
434                         }
435                         BER_BVZERO( &a->a_vals[ k - 1 ] );
436
437                         break;
438                 }
439
440                 free( asserted.bv_val );
441
442                 /* looked through them all w/o finding it */
443                 if ( ! found ) {
444                         return LDAP_NO_SUCH_ATTRIBUTE;
445                 }
446         }
447
448         /* if no values remain, delete the entire attribute */
449         if ( BER_BVISNULL( &a->a_vals[ 0 ] ) ) {
450                 /* should already be zero */
451                 *newlevel = 0;
452                 
453                 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
454                         return LDAP_NO_SUCH_ATTRIBUTE;
455                 }
456         }
457
458         return LDAP_SUCCESS;
459 }
460
461 static int
462 replace_values( Entry *e, Modification *mod, int *newlevel )
463 {
464         int rc;
465
466         *newlevel = 0;
467         rc = check_constraints( mod, newlevel );
468         if ( rc != LDAP_SUCCESS ) {
469                 return rc;
470         }
471
472         rc = attr_delete( &e->e_attrs, mod->sm_desc );
473
474         if ( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
475                 return rc;
476         }
477
478         if ( mod->sm_values != NULL ) {
479                 rc = attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues );
480                 if ( rc != LDAP_SUCCESS ) {
481                         return rc;
482                 }
483         }
484
485         return LDAP_SUCCESS;
486 }
487