1 /* log.c - deal with log subsystem */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2001-2005 The OpenLDAP Foundation.
6 * Portions Copyright 2001-2003 Pierangelo Masarati.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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>.
18 * This work was initially developed by Pierangelo Masarati for inclusion
19 * in OpenLDAP Software.
26 #include <ac/string.h>
32 #include "back-monitor.h"
37 ldap_pvt_thread_mutex_t monitor_log_mutex;
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 }
61 static int loglevel2int( struct berval *l );
62 static int int2loglevel( int n );
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 );
69 * initializes log subentry
72 monitor_subsys_log_init(
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."),
86 ldap_pvt_thread_mutex_init( &monitor_log_mutex );
88 mi = ( monitor_info_t * )be->be_private;
90 if ( monitor_cache_get( mi, &ms->mss_ndn,
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 );
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 ) {
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,
109 &int_2_level[ i ].n, NULL );
115 if ( int_2_level[ i ].i & ldap_syslog ) {
116 attr_merge_one( e, mi->mi_ad_managedInfo,
118 &int_2_level[ i ].n );
122 attr_merge( e, mi->mi_ad_description, desc, NULL );
124 monitor_cache_release( mi, e );
130 monitor_subsys_log_modify(
135 monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private;
137 int newlevel = ldap_syslog;
138 Attribute *save_attrs;
139 Modifications *modlist = op->oq_modify.rs_modlist;
142 ldap_pvt_thread_mutex_lock( &monitor_log_mutex );
144 save_attrs = e->e_attrs;
145 e->e_attrs = attrs_dup( e->e_attrs );
147 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
148 Modification *mod = &ml->sml_mod;
151 * accept all operational attributes;
152 * this includes modifersName and modifyTimestamp
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 );
165 * only the monitor description attribute can be modified
167 } else if ( mod->sm_desc != mi->mi_ad_managedInfo) {
168 rc = LDAP_UNWILLING_TO_PERFORM;
172 switch ( mod->sm_op ) {
174 rc = add_values( e, mod, &newlevel );
177 case LDAP_MOD_DELETE:
178 rc = delete_values( e, mod, &newlevel );
181 case LDAP_MOD_REPLACE:
182 rc = replace_values( e, mod, &newlevel );
190 if ( rc != LDAP_SUCCESS ) {
195 /* set the new debug level */
196 if ( rc == LDAP_SUCCESS ) {
198 static char textbuf[ BACKMONITOR_BUFSIZE ];
200 /* check for abandon */
201 if ( op->o_abandon ) {
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 ) {
215 * Do we need to protect this with a mutex?
217 ldap_syslog = newlevel;
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;
229 if ( rc == LDAP_SUCCESS ) {
230 attrs_free( save_attrs );
233 attrs_free( e->e_attrs );
234 e->e_attrs = save_attrs;
237 ldap_pvt_thread_mutex_unlock( &monitor_log_mutex );
243 loglevel2int( struct berval *l )
247 for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
248 if ( l->bv_len != int_2_level[ i ].s.bv_len ) {
252 if ( strcasecmp( l->bv_val, int_2_level[ i ].s.bv_val ) == 0 ) {
253 return int_2_level[ i ].i;
261 int2loglevel( int n )
265 for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
266 if ( int_2_level[ i ].i == n ) {
275 check_constraints( Modification *mod, int *newlevel )
279 for ( i = 0; mod->sm_values && !BER_BVISNULL( &mod->sm_values[ i ] ); i++ ) {
282 l = loglevel2int( &mod->sm_values[ i ] );
284 return LDAP_CONSTRAINT_VIOLATION;
287 if ( ( l = int2loglevel( l ) ) == -1 ) {
291 assert( int_2_level[ l ].s.bv_len
292 == mod->sm_values[ i ].bv_len );
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 );
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 );
309 add_values( Entry *e, Modification *mod, int *newlevel )
313 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
315 rc = check_constraints( mod, newlevel );
316 if ( rc != LDAP_SUCCESS ) {
320 a = attr_find( e->e_attrs, mod->sm_desc );
323 /* "description" SHOULD have appropriate rules ... */
324 if ( mr == NULL || !mr->smr_match ) {
325 return LDAP_INAPPROPRIATE_MATCHING;
328 for ( i = 0; !BER_BVISNULL( &mod->sm_values[ i ] ); i++ ) {
331 const char *text = NULL;
332 struct berval asserted;
334 rc = asserted_value_validate_normalize(
335 mod->sm_desc, mr, SLAP_MR_EQUALITY,
336 &mod->sm_values[ i ], &asserted, &text, NULL );
338 if ( rc != LDAP_SUCCESS ) {
342 for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ ) {
344 int rc = value_match( &match, mod->sm_desc, mr,
345 0, &a->a_vals[ j ], &asserted, &text );
347 if ( rc == LDAP_SUCCESS && match == 0 ) {
348 free( asserted.bv_val );
349 return LDAP_TYPE_OR_VALUE_EXISTS;
353 free( asserted.bv_val );
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 */
368 delete_values( Entry *e, Modification *mod, int *newlevel )
370 int i, j, k, found, rc, nl = 0;
372 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
374 rc = check_constraints( mod, &nl );
375 if ( rc != LDAP_SUCCESS ) {
381 /* delete the entire attribute */
382 if ( mod->sm_values == NULL ) {
383 int rc = attr_delete( &e->e_attrs, mod->sm_desc );
386 rc = LDAP_NO_SUCH_ATTRIBUTE;
394 if ( mr == NULL || !mr->smr_match ) {
395 /* disallow specific attributes from being deleted if
396 * no equality rule */
397 return LDAP_INAPPROPRIATE_MATCHING;
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 );
405 /* find each value to delete */
406 for ( i = 0; !BER_BVISNULL( &mod->sm_values[ i ] ); i++ ) {
408 const char *text = NULL;
410 struct berval asserted;
412 rc = asserted_value_validate_normalize(
413 mod->sm_desc, mr, SLAP_MR_EQUALITY,
414 &mod->sm_values[ i ], &asserted, &text, NULL );
416 if( rc != LDAP_SUCCESS ) return rc;
419 for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ ) {
421 int rc = value_match( &match, mod->sm_desc, mr,
423 &a->a_vals[ j ], &asserted, &text );
425 if( rc == LDAP_SUCCESS && match != 0 ) {
429 /* found a matching value */
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 ];
437 BER_BVZERO( &a->a_vals[ k - 1 ] );
442 free( asserted.bv_val );
444 /* looked through them all w/o finding it */
446 return LDAP_NO_SUCH_ATTRIBUTE;
450 /* if no values remain, delete the entire attribute */
451 if ( BER_BVISNULL( &a->a_vals[ 0 ] ) ) {
452 /* should already be zero */
455 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
456 return LDAP_NO_SUCH_ATTRIBUTE;
464 replace_values( Entry *e, Modification *mod, int *newlevel )
469 rc = check_constraints( mod, newlevel );
470 if ( rc != LDAP_SUCCESS ) {
474 rc = attr_delete( &e->e_attrs, mod->sm_desc );
476 if ( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
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 ) {