]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/log.c
2aa3fc04cc57ad36471ced46d4424dc52014d701
[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         monitor_info_t  *mi;
77         Entry           *e;
78         int             i;
79         struct berval   desc[] = {
80                 BER_BVC("This entry allows to set the log level runtime."),
81                 BER_BVC("Set the attribute 'managedInfo' to the desired log levels."),
82                 BER_BVNULL
83         };
84
85         ldap_pvt_thread_mutex_init( &monitor_log_mutex );
86
87         mi = ( monitor_info_t * )be->be_private;
88
89         if ( monitor_cache_get( mi, &ms->mss_ndn, 
90                                 &e ) ) {
91                 Debug( LDAP_DEBUG_ANY,
92                         "monitor_subsys_log_init: "
93                         "unable to get entry \"%s\"\n",
94                         ms->mss_ndn.bv_val, 0, 0 );
95                 return( -1 );
96         }
97
98         /* initialize the debug level(s) */
99         for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
100                 if ( mi->mi_ad_managedInfo->ad_type->sat_equality->smr_normalize ) {
101                         int     rc;
102
103                         rc = (*mi->mi_ad_managedInfo->ad_type->sat_equality->smr_normalize)(
104                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
105                                         mi->mi_ad_managedInfo->ad_type->sat_syntax,
106                                         mi->mi_ad_managedInfo->ad_type->sat_equality,
107                                         &int_2_level[ i ].s,
108                                         &int_2_level[ i ].n, NULL );
109                         if ( rc ) {
110                                 return( -1 );
111                         }
112                 }
113
114                 if ( int_2_level[ i ].i & ldap_syslog ) {
115                         attr_merge_one( e, mi->mi_ad_managedInfo,
116                                         &int_2_level[ i ].s,
117                                         &int_2_level[ i ].n );
118                 }
119         }
120
121         attr_merge_normalize( e, mi->mi_ad_description, desc, NULL );
122
123         monitor_cache_release( mi, e );
124
125         return( 0 );
126 }
127
128 int 
129 monitor_subsys_log_modify( 
130         Operation               *op,
131         SlapReply               *rs,
132         Entry                   *e )
133 {
134         monitor_info_t  *mi = ( monitor_info_t * )op->o_bd->be_private;
135         int             rc = LDAP_OTHER;
136         int             newlevel = ldap_syslog;
137         Attribute       *save_attrs;
138         Modifications   *modlist = op->oq_modify.rs_modlist;
139         Modifications   *ml;
140
141         ldap_pvt_thread_mutex_lock( &monitor_log_mutex );
142
143         save_attrs = e->e_attrs;
144         e->e_attrs = attrs_dup( e->e_attrs );
145
146         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
147                 Modification    *mod = &ml->sml_mod;
148
149                 /*
150                  * accept all operational attributes;
151                  * this includes modifersName and modifyTimestamp
152                  * if lastmod is "on"
153                  */
154                 if ( is_at_operational( mod->sm_desc->ad_type ) ) {
155                         ( void ) attr_delete( &e->e_attrs, mod->sm_desc );
156                         rc = rs->sr_err = attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues );
157                         if ( rc != LDAP_SUCCESS ) {
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 = rs->sr_err = 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                         rs->sr_err = rc;
190                         break;
191                 }
192         }
193
194         /* set the new debug level */
195         if ( rc == LDAP_SUCCESS ) {
196                 const char      *text;
197                 static char     textbuf[ BACKMONITOR_BUFSIZE ];
198
199                 /* check for abandon */
200                 if ( op->o_abandon ) {
201                         rc = rs->sr_err = SLAPD_ABANDON;
202
203                         goto cleanup;
204                 }
205
206                 /* check that the entry still obeys the schema */
207                 rc = entry_schema_check( be_monitor, e, save_attrs, 
208                                 &text, textbuf, sizeof( textbuf ) );
209                 if ( rc != LDAP_SUCCESS ) {
210                         rs->sr_err = rc;
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         if ( rc == LDAP_SUCCESS ) {
240                 rc = SLAP_CB_CONTINUE;
241         }
242
243         return rc;
244 }
245
246 static int
247 loglevel2int( struct berval *l )
248 {
249         int             i;
250         
251         for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
252                 if ( l->bv_len != int_2_level[ i ].s.bv_len ) {
253                         continue;
254                 }
255
256                 if ( strcasecmp( l->bv_val, int_2_level[ i ].s.bv_val ) == 0 ) {
257                         return int_2_level[ i ].i;
258                 }
259         }
260
261         return 0;
262 }
263
264 static int
265 int2loglevel( int n )
266 {
267         int             i;
268         
269         for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
270                 if ( int_2_level[ i ].i == n ) {
271                         return i;
272                 }
273         }
274
275         return -1;
276 }
277
278 static int
279 check_constraints( Modification *mod, int *newlevel )
280 {
281         int             i;
282
283         for ( i = 0; mod->sm_values && !BER_BVISNULL( &mod->sm_values[ i ] ); i++ ) {
284                 int             l;
285                 
286                 l = loglevel2int( &mod->sm_values[ i ] );
287                 if ( !l ) {
288                         return LDAP_CONSTRAINT_VIOLATION;
289                 }
290
291                 if ( ( l = int2loglevel( l ) ) == -1 ) {
292                         return LDAP_OTHER;
293                 }
294
295                 assert( int_2_level[ l ].s.bv_len
296                                 == mod->sm_values[ i ].bv_len );
297                 
298                 AC_MEMCPY( mod->sm_values[ i ].bv_val,
299                                 int_2_level[ l ].s.bv_val,
300                                 int_2_level[ l ].s.bv_len );
301
302                 AC_MEMCPY( mod->sm_nvalues[ i ].bv_val,
303                                 int_2_level[ l ].n.bv_val,
304                                 int_2_level[ l ].n.bv_len );
305
306                 *newlevel |= l;
307         }
308
309         return LDAP_SUCCESS;
310 }       
311
312 static int 
313 add_values( Entry *e, Modification *mod, int *newlevel )
314 {
315         Attribute       *a;
316         int             i, rc;
317         MatchingRule    *mr = mod->sm_desc->ad_type->sat_equality;
318
319         rc = check_constraints( mod, newlevel );
320         if ( rc != LDAP_SUCCESS ) {
321                 return rc;
322         }
323
324         a = attr_find( e->e_attrs, mod->sm_desc );
325
326         if ( a != NULL ) {
327                 /* "description" SHOULD have appropriate rules ... */
328                 if ( mr == NULL || !mr->smr_match ) {
329                         return LDAP_INAPPROPRIATE_MATCHING;
330                 }
331
332                 for ( i = 0; !BER_BVISNULL( &mod->sm_values[ i ] ); i++ ) {
333                         int rc;
334                         int j;
335                         const char *text = NULL;
336                         struct berval asserted;
337
338                         rc = asserted_value_validate_normalize(
339                                 mod->sm_desc, mr, SLAP_MR_EQUALITY,
340                                 &mod->sm_values[ i ], &asserted, &text, NULL );
341
342                         if ( rc != LDAP_SUCCESS ) {
343                                 return rc;
344                         }
345
346                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ ) {
347                                 int match;
348                                 int rc = value_match( &match, mod->sm_desc, mr,
349                                         0, &a->a_vals[ j ], &asserted, &text );
350
351                                 if ( rc == LDAP_SUCCESS && match == 0 ) {
352                                         free( asserted.bv_val );
353                                         return LDAP_TYPE_OR_VALUE_EXISTS;
354                                 }
355                         }
356
357                         free( asserted.bv_val );
358                 }
359         }
360
361         /* no - add them */
362         rc = attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues );
363         if ( rc != LDAP_SUCCESS ) {
364                 /* this should return result of attr_mergeit */
365                 return rc;
366         }
367
368         return LDAP_SUCCESS;
369 }
370
371 static int
372 delete_values( Entry *e, Modification *mod, int *newlevel )
373 {
374         int             i, j, k, found, rc, nl = 0;
375         Attribute       *a;
376         MatchingRule    *mr = mod->sm_desc->ad_type->sat_equality;
377
378         rc = check_constraints( mod, &nl );
379         if ( rc != LDAP_SUCCESS ) {
380                 return rc;
381         }
382
383         *newlevel &= ~nl;
384
385         /* delete the entire attribute */
386         if ( mod->sm_values == NULL ) {
387                 int rc = attr_delete( &e->e_attrs, mod->sm_desc );
388
389                 if ( rc ) {
390                         rc = LDAP_NO_SUCH_ATTRIBUTE;
391                 } else {
392                         *newlevel = 0;
393                         rc = LDAP_SUCCESS;
394                 }
395                 return rc;
396         }
397
398         if ( mr == NULL || !mr->smr_match ) {
399                 /* disallow specific attributes from being deleted if
400                  * no equality rule */
401                 return LDAP_INAPPROPRIATE_MATCHING;
402         }
403
404         /* delete specific values - find the attribute first */
405         if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
406                 return( LDAP_NO_SUCH_ATTRIBUTE );
407         }
408
409         /* find each value to delete */
410         for ( i = 0; !BER_BVISNULL( &mod->sm_values[ i ] ); i++ ) {
411                 int rc;
412                 const char *text = NULL;
413
414                 struct berval asserted;
415
416                 rc = asserted_value_validate_normalize(
417                                 mod->sm_desc, mr, SLAP_MR_EQUALITY,
418                                 &mod->sm_values[ i ], &asserted, &text, NULL );
419
420                 if( rc != LDAP_SUCCESS ) return rc;
421
422                 found = 0;
423                 for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ ) {
424                         int match;
425                         int rc = value_match( &match, mod->sm_desc, mr,
426                                 0,
427                                 &a->a_vals[ j ], &asserted, &text );
428
429                         if( rc == LDAP_SUCCESS && match != 0 ) {
430                                 continue;
431                         }
432
433                         /* found a matching value */
434                         found = 1;
435
436                         /* delete it */
437                         free( a->a_vals[ j ].bv_val );
438                         for ( k = j + 1; !BER_BVISNULL( &a->a_vals[ k ] ); k++ ) {
439                                 a->a_vals[ k - 1 ] = a->a_vals[ k ];
440                         }
441                         BER_BVZERO( &a->a_vals[ k - 1 ] );
442
443                         break;
444                 }
445
446                 free( asserted.bv_val );
447
448                 /* looked through them all w/o finding it */
449                 if ( ! found ) {
450                         return LDAP_NO_SUCH_ATTRIBUTE;
451                 }
452         }
453
454         /* if no values remain, delete the entire attribute */
455         if ( BER_BVISNULL( &a->a_vals[ 0 ] ) ) {
456                 /* should already be zero */
457                 *newlevel = 0;
458                 
459                 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
460                         return LDAP_NO_SUCH_ATTRIBUTE;
461                 }
462         }
463
464         return LDAP_SUCCESS;
465 }
466
467 static int
468 replace_values( Entry *e, Modification *mod, int *newlevel )
469 {
470         int rc;
471
472         *newlevel = 0;
473         rc = check_constraints( mod, newlevel );
474         if ( rc != LDAP_SUCCESS ) {
475                 return rc;
476         }
477
478         rc = attr_delete( &e->e_attrs, mod->sm_desc );
479
480         if ( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
481                 return rc;
482         }
483
484         if ( mod->sm_values != NULL ) {
485                 rc = attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues );
486                 if ( rc != LDAP_SUCCESS ) {
487                         return rc;
488                 }
489         }
490
491         return LDAP_SUCCESS;
492 }
493