]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/log.c
honor disclose
[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-2005 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                  * this includes modifersName and modifyTimestamp
153                  * if lastmod is "on"
154                  */
155                 if ( is_at_operational( mod->sm_desc->ad_type ) ) {
156                         ( void ) attr_delete( &e->e_attrs, mod->sm_desc );
157                         rc = attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues );
158                         if ( rc != 0 ) {
159                                 rc = LDAP_OTHER;
160                                 break;
161                         }
162                         continue;
163
164                 /*
165                  * only the monitor description attribute can be modified
166                  */
167                 } else if ( mod->sm_desc != mi->mi_ad_managedInfo) {
168                         rc = LDAP_UNWILLING_TO_PERFORM;
169                         break;
170                 }
171
172                 switch ( mod->sm_op ) {
173                 case LDAP_MOD_ADD:
174                         rc = add_values( e, mod, &newlevel );
175                         break;
176                         
177                 case LDAP_MOD_DELETE:
178                         rc = delete_values( e, mod, &newlevel );
179                         break;
180
181                 case LDAP_MOD_REPLACE:
182                         rc = replace_values( e, mod, &newlevel );
183                         break;
184
185                 default:
186                         rc = LDAP_OTHER;
187                         break;
188                 }
189
190                 if ( rc != LDAP_SUCCESS ) {
191                         break;
192                 }
193         }
194
195         /* set the new debug level */
196         if ( rc == LDAP_SUCCESS ) {
197                 const char      *text;
198                 static char     textbuf[ BACKMONITOR_BUFSIZE ];
199
200                 /* check for abandon */
201                 if ( op->o_abandon ) {
202                         rc = SLAPD_ABANDON;
203
204                         goto cleanup;
205                 }
206
207                 /* check that the entry still obeys the schema */
208                 rc = entry_schema_check( be_monitor, e, save_attrs, 
209                                 &text, textbuf, sizeof( textbuf ) );
210                 if ( rc != LDAP_SUCCESS ) {
211                         goto cleanup;
212                 }
213
214                 /*
215                  * Do we need to protect this with a mutex?
216                  */
217                 ldap_syslog = newlevel;
218
219 #if 0   /* debug rather than log */
220                 slap_debug = newlevel;
221                 lutil_set_debug_level( "slapd", slap_debug );
222                 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
223                 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
224                 ldif_debug = slap_debug;
225 #endif
226         }
227
228 cleanup:;
229         if ( rc == LDAP_SUCCESS ) {
230                 attrs_free( save_attrs );
231
232         } else {
233                 attrs_free( e->e_attrs );
234                 e->e_attrs = save_attrs;
235         }
236         
237         ldap_pvt_thread_mutex_unlock( &monitor_log_mutex );
238
239         return( rc );
240 }
241
242 static int
243 loglevel2int( struct berval *l )
244 {
245         int             i;
246         
247         for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
248                 if ( l->bv_len != int_2_level[ i ].s.bv_len ) {
249                         continue;
250                 }
251
252                 if ( strcasecmp( l->bv_val, int_2_level[ i ].s.bv_val ) == 0 ) {
253                         return int_2_level[ i ].i;
254                 }
255         }
256
257         return 0;
258 }
259
260 static int
261 int2loglevel( int n )
262 {
263         int             i;
264         
265         for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
266                 if ( int_2_level[ i ].i == n ) {
267                         return i;
268                 }
269         }
270
271         return -1;
272 }
273
274 static int
275 check_constraints( Modification *mod, int *newlevel )
276 {
277         int             i;
278
279         for ( i = 0; mod->sm_values && !BER_BVISNULL( &mod->sm_values[ i ] ); i++ ) {
280                 int             l;
281                 
282                 l = loglevel2int( &mod->sm_values[ i ] );
283                 if ( !l ) {
284                         return LDAP_CONSTRAINT_VIOLATION;
285                 }
286
287                 if ( ( l = int2loglevel( l ) ) == -1 ) {
288                         return LDAP_OTHER;
289                 }
290
291                 assert( int_2_level[ l ].s.bv_len
292                                 == mod->sm_values[ i ].bv_len );
293                 
294                 AC_MEMCPY( mod->sm_values[ i ].bv_val,
295                                 int_2_level[ l ].s.bv_val,
296                                 int_2_level[ l ].s.bv_len );
297
298                 AC_MEMCPY( mod->sm_nvalues[ i ].bv_val,
299                                 int_2_level[ l ].n.bv_val,
300                                 int_2_level[ l ].n.bv_len );
301
302                 *newlevel |= l;
303         }
304
305         return LDAP_SUCCESS;
306 }       
307
308 static int 
309 add_values( Entry *e, Modification *mod, int *newlevel )
310 {
311         Attribute       *a;
312         int             i, rc;
313         MatchingRule    *mr = mod->sm_desc->ad_type->sat_equality;
314
315         rc = check_constraints( mod, newlevel );
316         if ( rc != LDAP_SUCCESS ) {
317                 return rc;
318         }
319
320         a = attr_find( e->e_attrs, mod->sm_desc );
321
322         if ( a != NULL ) {
323                 /* "description" SHOULD have appropriate rules ... */
324                 if ( mr == NULL || !mr->smr_match ) {
325                         return LDAP_INAPPROPRIATE_MATCHING;
326                 }
327
328                 for ( i = 0; !BER_BVISNULL( &mod->sm_values[ i ] ); i++ ) {
329                         int rc;
330                         int j;
331                         const char *text = NULL;
332                         struct berval asserted;
333
334                         rc = asserted_value_validate_normalize(
335                                 mod->sm_desc, mr, SLAP_MR_EQUALITY,
336                                 &mod->sm_values[ i ], &asserted, &text, NULL );
337
338                         if ( rc != LDAP_SUCCESS ) {
339                                 return rc;
340                         }
341
342                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ ) {
343                                 int match;
344                                 int rc = value_match( &match, mod->sm_desc, mr,
345                                         0, &a->a_vals[ j ], &asserted, &text );
346
347                                 if ( rc == LDAP_SUCCESS && match == 0 ) {
348                                         free( asserted.bv_val );
349                                         return LDAP_TYPE_OR_VALUE_EXISTS;
350                                 }
351                         }
352
353                         free( asserted.bv_val );
354                 }
355         }
356
357         /* no - add them */
358         rc = attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues );
359         if ( rc != LDAP_SUCCESS ) {
360                 /* this should return result of attr_mergeit */
361                 return rc;
362         }
363
364         return LDAP_SUCCESS;
365 }
366
367 static int
368 delete_values( Entry *e, Modification *mod, int *newlevel )
369 {
370         int             i, j, k, found, rc, nl = 0;
371         Attribute       *a;
372         MatchingRule    *mr = mod->sm_desc->ad_type->sat_equality;
373
374         rc = check_constraints( mod, &nl );
375         if ( rc != LDAP_SUCCESS ) {
376                 return rc;
377         }
378
379         *newlevel &= ~nl;
380
381         /* delete the entire attribute */
382         if ( mod->sm_values == NULL ) {
383                 int rc = attr_delete( &e->e_attrs, mod->sm_desc );
384
385                 if ( rc ) {
386                         rc = LDAP_NO_SUCH_ATTRIBUTE;
387                 } else {
388                         *newlevel = 0;
389                         rc = LDAP_SUCCESS;
390                 }
391                 return rc;
392         }
393
394         if ( mr == NULL || !mr->smr_match ) {
395                 /* disallow specific attributes from being deleted if
396                  * no equality rule */
397                 return LDAP_INAPPROPRIATE_MATCHING;
398         }
399
400         /* delete specific values - find the attribute first */
401         if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
402                 return( LDAP_NO_SUCH_ATTRIBUTE );
403         }
404
405         /* find each value to delete */
406         for ( i = 0; !BER_BVISNULL( &mod->sm_values[ i ] ); i++ ) {
407                 int rc;
408                 const char *text = NULL;
409
410                 struct berval asserted;
411
412                 rc = asserted_value_validate_normalize(
413                                 mod->sm_desc, mr, SLAP_MR_EQUALITY,
414                                 &mod->sm_values[ i ], &asserted, &text, NULL );
415
416                 if( rc != LDAP_SUCCESS ) return rc;
417
418                 found = 0;
419                 for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ ) {
420                         int match;
421                         int rc = value_match( &match, mod->sm_desc, mr,
422                                 0,
423                                 &a->a_vals[ j ], &asserted, &text );
424
425                         if( rc == LDAP_SUCCESS && match != 0 ) {
426                                 continue;
427                         }
428
429                         /* found a matching value */
430                         found = 1;
431
432                         /* delete it */
433                         free( a->a_vals[ j ].bv_val );
434                         for ( k = j + 1; !BER_BVISNULL( &a->a_vals[ k ] ); k++ ) {
435                                 a->a_vals[ k - 1 ] = a->a_vals[ k ];
436                         }
437                         BER_BVZERO( &a->a_vals[ k - 1 ] );
438
439                         break;
440                 }
441
442                 free( asserted.bv_val );
443
444                 /* looked through them all w/o finding it */
445                 if ( ! found ) {
446                         return LDAP_NO_SUCH_ATTRIBUTE;
447                 }
448         }
449
450         /* if no values remain, delete the entire attribute */
451         if ( BER_BVISNULL( &a->a_vals[ 0 ] ) ) {
452                 /* should already be zero */
453                 *newlevel = 0;
454                 
455                 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
456                         return LDAP_NO_SUCH_ATTRIBUTE;
457                 }
458         }
459
460         return LDAP_SUCCESS;
461 }
462
463 static int
464 replace_values( Entry *e, Modification *mod, int *newlevel )
465 {
466         int rc;
467
468         *newlevel = 0;
469         rc = check_constraints( mod, newlevel );
470         if ( rc != LDAP_SUCCESS ) {
471                 return rc;
472         }
473
474         rc = attr_delete( &e->e_attrs, mod->sm_desc );
475
476         if ( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
477                 return rc;
478         }
479
480         if ( mod->sm_values != NULL ) {
481                 rc = attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues );
482                 if ( rc != LDAP_SUCCESS ) {
483                         return rc;
484                 }
485         }
486
487         return LDAP_SUCCESS;
488 }
489