1 /* log.c - deal with log subsystem */
3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
9 * This work has beed deveolped for the OpenLDAP Foundation
10 * in the hope that it may be useful to the Open Source community,
11 * but WITHOUT ANY WARRANTY.
13 * Permission is granted to anyone to use this software for any purpose
14 * on any computer system, and to alter it and redistribute it, subject
15 * to the following restrictions:
17 * 1. The author and SysNet s.n.c. are not responsible for the consequences
18 * of use of this software, no matter how awful, even if they arise from
21 * 2. The origin of this software must not be misrepresented, either by
22 * explicit claim or by omission. Since few users ever read sources,
23 * credits should appear in the documentation.
25 * 3. Altered versions must be plainly marked as such, and must not be
26 * misrepresented as being the original software. Since few users
27 * ever read sources, credits should appear in the documentation.
28 * SysNet s.n.c. cannot be responsible for the consequences of the
31 * 4. This notice may not be removed or altered.
38 #include <ac/string.h>
44 #include "back-monitor.h"
49 ldap_pvt_thread_mutex_t monitor_log_mutex;
56 { LDAP_DEBUG_TRACE, BER_BVC("Trace"), { 0, NULL } },
57 { LDAP_DEBUG_PACKETS, BER_BVC("Packets"), { 0, NULL } },
58 { LDAP_DEBUG_ARGS, BER_BVC("Args"), { 0, NULL } },
59 { LDAP_DEBUG_CONNS, BER_BVC("Conns"), { 0, NULL } },
60 { LDAP_DEBUG_BER, BER_BVC("BER"), { 0, NULL } },
61 { LDAP_DEBUG_FILTER, BER_BVC("Filter"), { 0, NULL } },
62 { LDAP_DEBUG_CONFIG, BER_BVC("Config"), { 0, NULL } }, /* useless */
63 { LDAP_DEBUG_ACL, BER_BVC("ACL"), { 0, NULL } },
64 { LDAP_DEBUG_STATS, BER_BVC("Stats"), { 0, NULL } },
65 { LDAP_DEBUG_STATS2, BER_BVC("Stats2"), { 0, NULL } },
66 { LDAP_DEBUG_SHELL, BER_BVC("Shell"), { 0, NULL } },
67 { LDAP_DEBUG_PARSE, BER_BVC("Parse"), { 0, NULL } },
68 { LDAP_DEBUG_CACHE, BER_BVC("Cache"), { 0, NULL } },
69 { LDAP_DEBUG_INDEX, BER_BVC("Index"), { 0, NULL } },
70 { 0, { 0, NULL }, { 0, NULL } }
73 static int loglevel2int( struct berval *l );
74 static int int2loglevel( int n );
76 static int add_values( Entry *e, Modification *mod, int *newlevel );
77 static int delete_values( Entry *e, Modification *mod, int *newlevel );
78 static int replace_values( Entry *e, Modification *mod, int *newlevel );
81 * initializes log subentry
84 monitor_subsys_log_init(
88 struct monitorinfo *mi;
93 ldap_pvt_thread_mutex_init( &monitor_log_mutex );
95 mi = ( struct monitorinfo * )be->be_private;
97 if ( monitor_cache_get( mi, &monitor_subsys[SLAPD_MONITOR_LOG].mss_ndn,
100 LDAP_LOG( OPERATION, CRIT,
101 "monitor_subsys_log_init: "
102 "unable to get entry '%s'\n",
103 monitor_subsys[SLAPD_MONITOR_LOG].mss_ndn.bv_val, 0, 0 );
105 Debug( LDAP_DEBUG_ANY,
106 "monitor_subsys_log_init: "
107 "unable to get entry '%s'\n%s%s",
108 monitor_subsys[SLAPD_MONITOR_LOG].mss_ndn.bv_val,
116 /* initialize the debug level(s) */
117 for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
119 if ( monitor_ad_desc->ad_type->sat_equality->smr_normalize ) {
122 rc = (*monitor_ad_desc->ad_type->sat_equality->smr_normalize)(
124 monitor_ad_desc->ad_type->sat_syntax,
125 monitor_ad_desc->ad_type->sat_equality,
127 &int_2_level[ i ].n, NULL );
133 if ( int_2_level[ i ].i & ldap_syslog ) {
134 attr_merge_one( e, monitor_ad_desc,
136 &int_2_level[ i ].n );
140 monitor_cache_release( mi, e );
146 monitor_subsys_log_modify(
147 struct monitorinfo *mi,
149 Modifications *modlist
153 int newlevel = ldap_syslog;
154 Attribute *save_attrs;
157 ldap_pvt_thread_mutex_lock( &monitor_log_mutex );
159 save_attrs = e->e_attrs;
160 e->e_attrs = attrs_dup( e->e_attrs );
162 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
163 Modification *mod = &ml->sml_mod;
166 * accept all operational attributes
168 if ( is_at_operational( mod->sm_desc->ad_type ) ) {
169 ( void ) attr_delete( &e->e_attrs, mod->sm_desc );
170 rc = attr_merge( e, mod->sm_desc, mod->sm_bvalues,
179 * only the monitor description attribute can be modified
181 } else if ( mod->sm_desc != monitor_ad_desc ) {
182 rc = LDAP_UNWILLING_TO_PERFORM;
186 switch ( mod->sm_op ) {
188 rc = add_values( e, mod, &newlevel );
191 case LDAP_MOD_DELETE:
192 rc = delete_values( e, mod, &newlevel );
195 case LDAP_MOD_REPLACE:
196 rc = replace_values( e, mod, &newlevel );
204 if ( rc != LDAP_SUCCESS ) {
209 /* set the new debug level */
210 if ( rc == LDAP_SUCCESS ) {
212 static char textbuf[1024];
215 /* check for abandon */
216 if ( op->o_abandon ) {
223 /* check that the entry still obeys the schema */
224 rc = entry_schema_check( be_monitor, e, save_attrs,
225 &text, textbuf, sizeof( textbuf ) );
226 if ( rc != LDAP_SUCCESS ) {
231 * Do we need to protect this with a mutex?
233 ldap_syslog = newlevel;
235 #if 0 /* debug rather than log */
236 slap_debug = newlevel;
237 lutil_set_debug_level( "slapd", slap_debug );
238 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
239 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
240 ldif_debug = slap_debug;
245 if ( rc == LDAP_SUCCESS ) {
246 attrs_free( save_attrs );
249 attrs_free( e->e_attrs );
250 e->e_attrs = save_attrs;
253 ldap_pvt_thread_mutex_unlock( &monitor_log_mutex );
259 loglevel2int( struct berval *l )
263 for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
264 if ( l->bv_len != int_2_level[ i ].s.bv_len ) {
268 if ( strcasecmp( l->bv_val, int_2_level[ i ].s.bv_val ) == 0 ) {
269 return int_2_level[ i ].i;
277 int2loglevel( int n )
281 for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
282 if ( int_2_level[ i ].i == n ) {
291 check_constraints( Modification *mod, int *newlevel )
295 for ( i = 0; mod->sm_bvalues && mod->sm_bvalues[i].bv_val != NULL; i++ ) {
298 l = loglevel2int( &mod->sm_bvalues[i] );
300 return LDAP_CONSTRAINT_VIOLATION;
303 if ( ( l = int2loglevel( l ) ) == -1 ) {
307 assert( int_2_level[ l ].s.bv_len
308 == mod->sm_bvalues[i].bv_len );
310 AC_MEMCPY( mod->sm_bvalues[i].bv_val,
311 int_2_level[ l ].s.bv_val,
312 int_2_level[ l ].s.bv_len );
314 AC_MEMCPY( mod->sm_nvalues[i].bv_val,
315 int_2_level[ l ].n.bv_val,
316 int_2_level[ l ].n.bv_len );
325 add_values( Entry *e, Modification *mod, int *newlevel )
329 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
331 rc = check_constraints( mod, newlevel );
332 if ( rc != LDAP_SUCCESS ) {
336 a = attr_find( e->e_attrs, mod->sm_desc );
339 /* "description" SHOULD have appropriate rules ... */
340 if ( mr == NULL || !mr->smr_match ) {
341 return LDAP_INAPPROPRIATE_MATCHING;
344 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
347 const char *text = NULL;
348 struct berval asserted;
350 rc = asserted_value_validate_normalize(
351 mod->sm_desc, mr, SLAP_MR_EQUALITY,
352 &mod->sm_bvalues[i], &asserted, &text, NULL );
354 if ( rc != LDAP_SUCCESS ) {
358 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
360 int rc = value_match( &match, mod->sm_desc, mr,
362 &a->a_vals[j], &asserted, &text );
364 if ( rc == LDAP_SUCCESS && match == 0 ) {
365 free( asserted.bv_val );
366 return LDAP_TYPE_OR_VALUE_EXISTS;
370 free( asserted.bv_val );
375 rc = attr_merge( e, mod->sm_desc, mod->sm_bvalues, mod->sm_nvalues );
376 if ( rc != LDAP_SUCCESS ) {
377 /* this should return result of attr_mergeit */
385 delete_values( Entry *e, Modification *mod, int *newlevel )
387 int i, j, k, found, rc, nl = 0;
389 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
391 rc = check_constraints( mod, &nl );
392 if ( rc != LDAP_SUCCESS ) {
398 /* delete the entire attribute */
399 if ( mod->sm_bvalues == NULL ) {
400 int rc = attr_delete( &e->e_attrs, mod->sm_desc );
403 rc = LDAP_NO_SUCH_ATTRIBUTE;
411 if ( mr == NULL || !mr->smr_match ) {
412 /* disallow specific attributes from being deleted if
413 * no equality rule */
414 return LDAP_INAPPROPRIATE_MATCHING;
417 /* delete specific values - find the attribute first */
418 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
419 return( LDAP_NO_SUCH_ATTRIBUTE );
422 /* find each value to delete */
423 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
425 const char *text = NULL;
427 struct berval asserted;
429 rc = asserted_value_validate_normalize(
430 mod->sm_desc, mr, SLAP_MR_EQUALITY,
431 &mod->sm_bvalues[i], &asserted, &text, NULL );
433 if( rc != LDAP_SUCCESS ) return rc;
436 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
438 int rc = value_match( &match, mod->sm_desc, mr,
440 &a->a_vals[j], &asserted, &text );
442 if( rc == LDAP_SUCCESS && match != 0 ) {
446 /* found a matching value */
450 free( a->a_vals[j].bv_val );
451 for ( k = j + 1; a->a_vals[k].bv_val != NULL; k++ ) {
452 a->a_vals[k - 1] = a->a_vals[k];
454 a->a_vals[k - 1].bv_val = NULL;
459 free( asserted.bv_val );
461 /* looked through them all w/o finding it */
463 return LDAP_NO_SUCH_ATTRIBUTE;
467 /* if no values remain, delete the entire attribute */
468 if ( a->a_vals[0].bv_val == NULL ) {
469 /* should already be zero */
472 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
473 return LDAP_NO_SUCH_ATTRIBUTE;
481 replace_values( Entry *e, Modification *mod, int *newlevel )
486 rc = check_constraints( mod, newlevel );
487 if ( rc != LDAP_SUCCESS ) {
491 rc = attr_delete( &e->e_attrs, mod->sm_desc );
493 if ( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
497 if ( mod->sm_bvalues != NULL ) {
498 rc = attr_merge( e, mod->sm_desc, mod->sm_bvalues,
500 if ( rc != LDAP_SUCCESS ) {