]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/log.c
unifdef -UNEW_LOGGING
[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 )
75 {
76         struct monitorinfo      *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 = ( struct monitorinfo * )be->be_private;
88
89         if ( monitor_cache_get( mi, &monitor_subsys[SLAPD_MONITOR_LOG].mss_ndn, 
90                                 &e ) ) {
91                 Debug( LDAP_DEBUG_ANY,
92                         "monitor_subsys_log_init: "
93                         "unable to get entry '%s'\n%s%s",
94                         monitor_subsys[SLAPD_MONITOR_LOG].mss_ndn.bv_val, 
95                         "", "" );
96                 return( -1 );
97         }
98
99         /* initialize the debug level(s) */
100         for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
101
102                 if ( mi->mi_ad_managedInfo->ad_type->sat_equality->smr_normalize ) {
103                         int     rc;
104
105                         rc = (*mi->mi_ad_managedInfo->ad_type->sat_equality->smr_normalize)(
106                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
107                                         mi->mi_ad_managedInfo->ad_type->sat_syntax,
108                                         mi->mi_ad_managedInfo->ad_type->sat_equality,
109                                         &int_2_level[ i ].s,
110                                         &int_2_level[ i ].n, NULL );
111                         if ( rc ) {
112                                 return( -1 );
113                         }
114                 }
115
116                 if ( int_2_level[ i ].i & ldap_syslog ) {
117                         attr_merge_one( e, mi->mi_ad_managedInfo,
118                                         &int_2_level[ i ].s,
119                                         &int_2_level[ i ].n );
120                 }
121         }
122
123         attr_merge( e, mi->mi_ad_description, desc, NULL );
124
125         monitor_cache_release( mi, e );
126
127         return( 0 );
128 }
129
130 int 
131 monitor_subsys_log_modify( 
132         Operation               *op,
133         Entry                   *e
134 )
135 {
136         struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
137         int             rc = LDAP_OTHER;
138         int             newlevel = ldap_syslog;
139         Attribute       *save_attrs;
140         Modifications   *modlist = op->oq_modify.rs_modlist;
141         Modifications   *ml;
142
143         ldap_pvt_thread_mutex_lock( &monitor_log_mutex );
144
145         save_attrs = e->e_attrs;
146         e->e_attrs = attrs_dup( e->e_attrs );
147
148         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
149                 Modification    *mod = &ml->sml_mod;
150
151                 /*
152                  * accept all operational attributes
153                  */
154                 if ( is_at_operational( mod->sm_desc->ad_type ) ) {
155                         ( void ) attr_delete( &e->e_attrs, mod->sm_desc );
156                         rc = attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues );
157                         if ( rc != 0 ) {
158                                 rc = LDAP_OTHER;
159                                 break;
160                         }
161                         continue;
162
163                 /*
164                  * only the monitor description attribute can be modified
165                  */
166                 } else if ( mod->sm_desc != mi->mi_ad_managedInfo) {
167                         rc = LDAP_UNWILLING_TO_PERFORM;
168                         break;
169                 }
170
171                 switch ( mod->sm_op ) {
172                 case LDAP_MOD_ADD:
173                         rc = add_values( e, mod, &newlevel );
174                         break;
175                         
176                 case LDAP_MOD_DELETE:
177                         rc = delete_values( e, mod, &newlevel );
178                         break;
179
180                 case LDAP_MOD_REPLACE:
181                         rc = replace_values( e, mod, &newlevel );
182                         break;
183
184                 default:
185                         rc = LDAP_OTHER;
186                         break;
187                 }
188
189                 if ( rc != LDAP_SUCCESS ) {
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 = 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                         goto cleanup;
211                 }
212
213                 /*
214                  * Do we need to protect this with a mutex?
215                  */
216                 ldap_syslog = newlevel;
217
218 #if 0   /* debug rather than log */
219                 slap_debug = newlevel;
220                 lutil_set_debug_level( "slapd", slap_debug );
221                 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
222                 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
223                 ldif_debug = slap_debug;
224 #endif
225         }
226
227 cleanup:;
228         if ( rc == LDAP_SUCCESS ) {
229                 attrs_free( save_attrs );
230
231         } else {
232                 attrs_free( e->e_attrs );
233                 e->e_attrs = save_attrs;
234         }
235         
236         ldap_pvt_thread_mutex_unlock( &monitor_log_mutex );
237
238         return( rc );
239 }
240
241 static int
242 loglevel2int( struct berval *l )
243 {
244         int             i;
245         
246         for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
247                 if ( l->bv_len != int_2_level[ i ].s.bv_len ) {
248                         continue;
249                 }
250
251                 if ( strcasecmp( l->bv_val, int_2_level[ i ].s.bv_val ) == 0 ) {
252                         return int_2_level[ i ].i;
253                 }
254         }
255
256         return 0;
257 }
258
259 static int
260 int2loglevel( int n )
261 {
262         int             i;
263         
264         for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
265                 if ( int_2_level[ i ].i == n ) {
266                         return i;
267                 }
268         }
269
270         return -1;
271 }
272
273 static int
274 check_constraints( Modification *mod, int *newlevel )
275 {
276         int             i;
277
278         for ( i = 0; mod->sm_values && mod->sm_values[i].bv_val != NULL; i++ ) {
279                 int             l;
280                 
281                 l = loglevel2int( &mod->sm_values[i] );
282                 if ( !l ) {
283                         return LDAP_CONSTRAINT_VIOLATION;
284                 }
285
286                 if ( ( l = int2loglevel( l ) ) == -1 ) {
287                         return LDAP_OTHER;
288                 }
289
290                 assert( int_2_level[ l ].s.bv_len
291                                 == mod->sm_values[i].bv_len );
292                 
293                 AC_MEMCPY( mod->sm_values[i].bv_val,
294                                 int_2_level[ l ].s.bv_val,
295                                 int_2_level[ l ].s.bv_len );
296
297                 AC_MEMCPY( mod->sm_nvalues[i].bv_val,
298                                 int_2_level[ l ].n.bv_val,
299                                 int_2_level[ l ].n.bv_len );
300
301                 *newlevel |= l;
302         }
303
304         return LDAP_SUCCESS;
305 }       
306
307 static int 
308 add_values( Entry *e, Modification *mod, int *newlevel )
309 {
310         Attribute       *a;
311         int             i, rc;
312         MatchingRule    *mr = mod->sm_desc->ad_type->sat_equality;
313
314         rc = check_constraints( mod, newlevel );
315         if ( rc != LDAP_SUCCESS ) {
316                 return rc;
317         }
318
319         a = attr_find( e->e_attrs, mod->sm_desc );
320
321         if ( a != NULL ) {
322                 /* "description" SHOULD have appropriate rules ... */
323                 if ( mr == NULL || !mr->smr_match ) {
324                         return LDAP_INAPPROPRIATE_MATCHING;
325                 }
326
327                 for ( i = 0; mod->sm_values[i].bv_val != NULL; i++ ) {
328                         int rc;
329                         int j;
330                         const char *text = NULL;
331                         struct berval asserted;
332
333                         rc = asserted_value_validate_normalize(
334                                 mod->sm_desc, mr, SLAP_MR_EQUALITY,
335                                 &mod->sm_values[i], &asserted, &text, NULL );
336
337                         if ( rc != LDAP_SUCCESS ) {
338                                 return rc;
339                         }
340
341                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
342                                 int match;
343                                 int rc = value_match( &match, mod->sm_desc, mr,
344                                         0, &a->a_vals[j], &asserted, &text );
345
346                                 if ( rc == LDAP_SUCCESS && match == 0 ) {
347                                         free( asserted.bv_val );
348                                         return LDAP_TYPE_OR_VALUE_EXISTS;
349                                 }
350                         }
351
352                         free( asserted.bv_val );
353                 }
354         }
355
356         /* no - add them */
357         rc = attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues );
358         if ( rc != LDAP_SUCCESS ) {
359                 /* this should return result of attr_mergeit */
360                 return rc;
361         }
362
363         return LDAP_SUCCESS;
364 }
365
366 static int
367 delete_values( Entry *e, Modification *mod, int *newlevel )
368 {
369         int             i, j, k, found, rc, nl = 0;
370         Attribute       *a;
371         MatchingRule    *mr = mod->sm_desc->ad_type->sat_equality;
372
373         rc = check_constraints( mod, &nl );
374         if ( rc != LDAP_SUCCESS ) {
375                 return rc;
376         }
377
378         *newlevel &= ~nl;
379
380         /* delete the entire attribute */
381         if ( mod->sm_values == NULL ) {
382                 int rc = attr_delete( &e->e_attrs, mod->sm_desc );
383
384                 if ( rc ) {
385                         rc = LDAP_NO_SUCH_ATTRIBUTE;
386                 } else {
387                         *newlevel = 0;
388                         rc = LDAP_SUCCESS;
389                 }
390                 return rc;
391         }
392
393         if ( mr == NULL || !mr->smr_match ) {
394                 /* disallow specific attributes from being deleted if
395                  * no equality rule */
396                 return LDAP_INAPPROPRIATE_MATCHING;
397         }
398
399         /* delete specific values - find the attribute first */
400         if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
401                 return( LDAP_NO_SUCH_ATTRIBUTE );
402         }
403
404         /* find each value to delete */
405         for ( i = 0; mod->sm_values[i].bv_val != NULL; i++ ) {
406                 int rc;
407                 const char *text = NULL;
408
409                 struct berval asserted;
410
411                 rc = asserted_value_validate_normalize(
412                                 mod->sm_desc, mr, SLAP_MR_EQUALITY,
413                                 &mod->sm_values[i], &asserted, &text, NULL );
414
415                 if( rc != LDAP_SUCCESS ) return rc;
416
417                 found = 0;
418                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
419                         int match;
420                         int rc = value_match( &match, mod->sm_desc, mr,
421                                 0,
422                                 &a->a_vals[j], &asserted, &text );
423
424                         if( rc == LDAP_SUCCESS && match != 0 ) {
425                                 continue;
426                         }
427
428                         /* found a matching value */
429                         found = 1;
430
431                         /* delete it */
432                         free( a->a_vals[j].bv_val );
433                         for ( k = j + 1; a->a_vals[k].bv_val != NULL; k++ ) {
434                                 a->a_vals[k - 1] = a->a_vals[k];
435                         }
436                         a->a_vals[k - 1].bv_val = NULL;
437
438                         break;
439                 }
440
441                 free( asserted.bv_val );
442
443                 /* looked through them all w/o finding it */
444                 if ( ! found ) {
445                         return LDAP_NO_SUCH_ATTRIBUTE;
446                 }
447         }
448
449         /* if no values remain, delete the entire attribute */
450         if ( a->a_vals[0].bv_val == NULL ) {
451                 /* should already be zero */
452                 *newlevel = 0;
453                 
454                 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
455                         return LDAP_NO_SUCH_ATTRIBUTE;
456                 }
457         }
458
459         return LDAP_SUCCESS;
460 }
461
462 static int
463 replace_values( Entry *e, Modification *mod, int *newlevel )
464 {
465         int rc;
466
467         *newlevel = 0;
468         rc = check_constraints( mod, newlevel );
469         if ( rc != LDAP_SUCCESS ) {
470                 return rc;
471         }
472
473         rc = attr_delete( &e->e_attrs, mod->sm_desc );
474
475         if ( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
476                 return rc;
477         }
478
479         if ( mod->sm_values != NULL ) {
480                 rc = attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues );
481                 if ( rc != LDAP_SUCCESS ) {
482                         return rc;
483                 }
484         }
485
486         return LDAP_SUCCESS;
487 }
488