1 /* log.c - deal with log subsystem */
3 * Copyright 1998-2002 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>
43 #include "back-monitor.h"
48 ldap_pvt_thread_mutex_t monitor_log_mutex;
54 { LDAP_DEBUG_TRACE, "Trace" },
55 { LDAP_DEBUG_PACKETS, "Packets" },
56 { LDAP_DEBUG_ARGS, "Args" },
57 { LDAP_DEBUG_CONNS, "Conns" },
58 { LDAP_DEBUG_BER, "BER" },
59 { LDAP_DEBUG_FILTER, "Filter" },
60 { LDAP_DEBUG_CONFIG, "Config" }, /* useless */
61 { LDAP_DEBUG_ACL, "ACL" },
62 { LDAP_DEBUG_STATS, "Stats" },
63 { LDAP_DEBUG_STATS2, "Stats2" },
64 { LDAP_DEBUG_SHELL, "Shell" },
65 { LDAP_DEBUG_PARSE, "Parse" },
66 { LDAP_DEBUG_CACHE, "Cache" },
67 { LDAP_DEBUG_INDEX, "Index" },
71 static int loglevel2int( const char *str );
72 static const char * int2loglevel( int n );
74 static int add_values( Entry *e, Modification *mod, int *newlevel );
75 static int delete_values( Entry *e, Modification *mod, int *newlevel );
76 static int replace_values( Entry *e, Modification *mod, int *newlevel );
79 * initializes log subentry
82 monitor_subsys_log_init(
86 struct monitorinfo *mi;
91 ldap_pvt_thread_mutex_init( &monitor_log_mutex );
93 mi = ( struct monitorinfo * )be->be_private;
95 if ( monitor_cache_get( mi, &monitor_subsys[SLAPD_MONITOR_LOG].mss_ndn,
98 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
99 "monitor_subsys_log_init: "
100 "unable to get entry '%s'\n",
101 monitor_subsys[SLAPD_MONITOR_LOG].mss_ndn.bv_val ));
103 Debug( LDAP_DEBUG_ANY,
104 "monitor_subsys_log_init: "
105 "unable to get entry '%s'\n%s%s",
106 monitor_subsys[SLAPD_MONITOR_LOG].mss_ndn.bv_val,
114 /* initialize the debug level */
115 for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
116 if ( int_2_level[ i ].i & ldap_syslog ) {
117 bv[0].bv_val = ( char * )int_2_level[ i ].s;
118 bv[0].bv_len = strlen( bv[0].bv_val );
120 attr_merge( e, monitor_ad_desc, bv );
124 monitor_cache_release( mi, e );
130 monitor_subsys_log_modify(
131 struct monitorinfo *mi,
133 Modifications *modlist
137 int newlevel = ldap_syslog;
138 Attribute *save_attrs;
141 ldap_pvt_thread_mutex_lock( &monitor_log_mutex );
143 save_attrs = e->e_attrs;
144 e->e_attrs = attrs_dup( e->e_attrs );
146 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
147 Modification *mod = &ml->sml_mod;
150 * accept all operational attributes
152 if ( is_at_operational( mod->sm_desc->ad_type ) ) {
153 ( void ) attr_delete( &e->e_attrs, mod->sm_desc );
154 rc = attr_merge( e, mod->sm_desc, mod->sm_bvalues );
162 * only the monitor description attribute can be modified
164 } else if ( mod->sm_desc != monitor_ad_desc ) {
165 rc = LDAP_UNWILLING_TO_PERFORM;
169 switch ( mod->sm_op ) {
171 rc = add_values( e, mod, &newlevel );
174 case LDAP_MOD_DELETE:
175 rc = delete_values( e, mod, &newlevel );
178 case LDAP_MOD_REPLACE:
179 rc = replace_values( e, mod, &newlevel );
183 rc = LDAP_OPERATIONS_ERROR;
187 if ( rc != LDAP_SUCCESS ) {
192 /* set the new debug level */
193 if ( rc == LDAP_SUCCESS ) {
195 static char textbuf[1024];
198 /* check for abandon */
199 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
200 if ( op->o_abandon ) {
201 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
208 /* check that the entry still obeys the schema */
209 rc = entry_schema_check( be_monitor, e, save_attrs,
210 &text, textbuf, sizeof( textbuf ) );
211 if ( rc != LDAP_SUCCESS ) {
215 ldap_syslog = newlevel;
217 #if 0 /* debug rather than log */
218 slap_debug = newlevel;
219 lutil_set_debug_level( "slapd", slap_debug );
220 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
221 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
222 ldif_debug = slap_debug;
227 if ( rc == LDAP_SUCCESS ) {
228 attrs_free( save_attrs );
231 attrs_free( e->e_attrs );
232 e->e_attrs = save_attrs;
235 ldap_pvt_thread_mutex_unlock( &monitor_log_mutex );
241 loglevel2int( const char *str )
245 for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
246 if ( strcasecmp( str, int_2_level[ i ].s ) == 0 ) {
247 return int_2_level[ i ].i;
255 int2loglevel( int n )
259 for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
260 if ( int_2_level[ i ].i == n ) {
261 return int_2_level[ i ].s;
269 check_constraints( Modification *mod, int *newlevel )
273 for ( i = 0; mod->sm_bvalues && mod->sm_bvalues[i].bv_val != NULL; i++ ) {
278 l = loglevel2int( mod->sm_bvalues[i].bv_val );
280 return LDAP_CONSTRAINT_VIOLATION;
283 s = int2loglevel( l );
285 assert( len == mod->sm_bvalues[i].bv_len );
287 AC_MEMCPY( mod->sm_bvalues[i].bv_val, s, len );
296 add_values( Entry *e, Modification *mod, int *newlevel )
300 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
302 rc = check_constraints( mod, newlevel );
303 if ( rc != LDAP_SUCCESS ) {
307 a = attr_find( e->e_attrs, mod->sm_desc );
311 /* "description" SHOULD have appropriate rules ... */
312 if ( mr == NULL || !mr->smr_match ) {
313 return LDAP_INAPPROPRIATE_MATCHING;
316 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
319 const char *text = NULL;
320 struct berval asserted;
322 rc = value_normalize( mod->sm_desc,
328 if ( rc != LDAP_SUCCESS ) {
332 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
334 int rc = value_match( &match, mod->sm_desc, mr,
335 SLAP_MR_VALUE_SYNTAX_MATCH,
336 &a->a_vals[j], &asserted, &text );
338 if ( rc == LDAP_SUCCESS && match == 0 ) {
339 free( asserted.bv_val );
340 return LDAP_TYPE_OR_VALUE_EXISTS;
344 free( asserted.bv_val );
349 if ( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
350 /* this should return result of attr_merge */
358 delete_values( Entry *e, Modification *mod, int *newlevel )
360 int i, j, k, found, rc, nl = 0;
362 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
364 rc = check_constraints( mod, &nl );
365 if ( rc != LDAP_SUCCESS ) {
371 /* delete the entire attribute */
372 if ( mod->sm_bvalues == NULL ) {
373 int rc = attr_delete( &e->e_attrs, mod->sm_desc );
376 rc = LDAP_NO_SUCH_ATTRIBUTE;
384 if ( mr == NULL || !mr->smr_match ) {
385 /* disallow specific attributes from being deleted if
386 * no equality rule */
387 return LDAP_INAPPROPRIATE_MATCHING;
390 /* delete specific values - find the attribute first */
391 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
392 return( LDAP_NO_SUCH_ATTRIBUTE );
395 /* find each value to delete */
396 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
398 const char *text = NULL;
400 struct berval asserted;
402 rc = value_normalize( mod->sm_desc,
408 if( rc != LDAP_SUCCESS ) return rc;
411 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
413 int rc = value_match( &match, mod->sm_desc, mr,
414 SLAP_MR_VALUE_SYNTAX_MATCH,
415 &a->a_vals[j], &asserted, &text );
417 if( rc == LDAP_SUCCESS && match != 0 ) {
421 /* found a matching value */
425 free( a->a_vals[j].bv_val );
426 for ( k = j + 1; a->a_vals[k].bv_val != NULL; k++ ) {
427 a->a_vals[k - 1] = a->a_vals[k];
429 a->a_vals[k - 1].bv_val = NULL;
434 free( asserted.bv_val );
436 /* looked through them all w/o finding it */
438 return LDAP_NO_SUCH_ATTRIBUTE;
442 /* if no values remain, delete the entire attribute */
443 if ( a->a_vals[0].bv_val == NULL ) {
444 /* should already be zero */
447 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
448 return LDAP_NO_SUCH_ATTRIBUTE;
456 replace_values( Entry *e, Modification *mod, int *newlevel )
461 rc = check_constraints( mod, newlevel );
462 if ( rc != LDAP_SUCCESS ) {
466 rc = attr_delete( &e->e_attrs, mod->sm_desc );
468 if ( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
472 if ( mod->sm_bvalues != NULL &&
473 attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {