1 /* log.c - deal with log subsystem */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2003 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by Pierangelo Masarati for inclusion
18 * in OpenLDAP Software.
20 /* This is an altered version */
22 * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
24 * This work has beed deveolped for the OpenLDAP Foundation
25 * in the hope that it may be useful to the Open Source community,
26 * but WITHOUT ANY WARRANTY.
28 * Permission is granted to anyone to use this software for any purpose
29 * on any computer system, and to alter it and redistribute it, subject
30 * to the following restrictions:
32 * 1. The author and SysNet s.n.c. are not responsible for the consequences
33 * of use of this software, no matter how awful, even if they arise from
36 * 2. The origin of this software must not be misrepresented, either by
37 * explicit claim or by omission. Since few users ever read sources,
38 * credits should appear in the documentation.
40 * 3. Altered versions must be plainly marked as such, and must not be
41 * misrepresented as being the original software. Since few users
42 * ever read sources, credits should appear in the documentation.
43 * SysNet s.n.c. cannot be responsible for the consequences of the
46 * 4. This notice may not be removed or altered.
49 #ifndef _BACK_MONITOR_H_
50 #define _BACK_MONITOR_H_
53 #include <ldap_pvt_thread.h>
60 * The cache maps DNs to Entries.
62 * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
64 * This work has beed deveolped for the OpenLDAP Foundation
65 * in the hope that it may be useful to the Open Source community,
66 * but WITHOUT ANY WARRANTY.
68 * Permission is granted to anyone to use this software for any purpose
69 * on any computer system, and to alter it and redistribute it, subject
70 * to the following restrictions:
72 * 1. The author and SysNet s.n.c. are not responsible for the consequences
73 * of use of this software, no matter how awful, even if they arise from
76 * 2. The origin of this software must not be misrepresented, either by
77 * explicit claim or by omission. Since few users ever read sources,
78 * credits should appear in the documentation.
80 * 3. Altered versions must be plainly marked as such, and must not be
81 * misrepresented as being the original software. Since few users
82 * ever read sources, credits should appear in the documentation.
83 * SysNet s.n.c. cannot be responsible for the consequences of the
86 * 4. This notice may not be removed or altered.
93 #include <ac/string.h>
99 #include "back-monitor.h"
104 ldap_pvt_thread_mutex_t monitor_log_mutex;
111 { LDAP_DEBUG_TRACE, BER_BVC("Trace"), { 0, NULL } },
112 { LDAP_DEBUG_PACKETS, BER_BVC("Packets"), { 0, NULL } },
113 { LDAP_DEBUG_ARGS, BER_BVC("Args"), { 0, NULL } },
114 { LDAP_DEBUG_CONNS, BER_BVC("Conns"), { 0, NULL } },
115 { LDAP_DEBUG_BER, BER_BVC("BER"), { 0, NULL } },
116 { LDAP_DEBUG_FILTER, BER_BVC("Filter"), { 0, NULL } },
117 { LDAP_DEBUG_CONFIG, BER_BVC("Config"), { 0, NULL } }, /* useless */
118 { LDAP_DEBUG_ACL, BER_BVC("ACL"), { 0, NULL } },
119 { LDAP_DEBUG_STATS, BER_BVC("Stats"), { 0, NULL } },
120 { LDAP_DEBUG_STATS2, BER_BVC("Stats2"), { 0, NULL } },
121 { LDAP_DEBUG_SHELL, BER_BVC("Shell"), { 0, NULL } },
122 { LDAP_DEBUG_PARSE, BER_BVC("Parse"), { 0, NULL } },
123 { LDAP_DEBUG_CACHE, BER_BVC("Cache"), { 0, NULL } },
124 { LDAP_DEBUG_INDEX, BER_BVC("Index"), { 0, NULL } },
125 { 0, { 0, NULL }, { 0, NULL } }
128 static int loglevel2int( struct berval *l );
129 static int int2loglevel( int n );
131 static int add_values( Entry *e, Modification *mod, int *newlevel );
132 static int delete_values( Entry *e, Modification *mod, int *newlevel );
133 static int replace_values( Entry *e, Modification *mod, int *newlevel );
136 * initializes log subentry
139 monitor_subsys_log_init(
143 struct monitorinfo *mi;
146 struct berval desc[] = {
147 BER_BVC("This entry allows to set the log level runtime."),
148 BER_BVC("Set the attribute 'managedInfo' to the desired log levels."),
152 ldap_pvt_thread_mutex_init( &monitor_log_mutex );
154 mi = ( struct monitorinfo * )be->be_private;
156 if ( monitor_cache_get( mi, &monitor_subsys[SLAPD_MONITOR_LOG].mss_ndn,
159 LDAP_LOG( OPERATION, CRIT,
160 "monitor_subsys_log_init: "
161 "unable to get entry '%s'\n",
162 monitor_subsys[SLAPD_MONITOR_LOG].mss_ndn.bv_val, 0, 0 );
164 Debug( LDAP_DEBUG_ANY,
165 "monitor_subsys_log_init: "
166 "unable to get entry '%s'\n%s%s",
167 monitor_subsys[SLAPD_MONITOR_LOG].mss_ndn.bv_val,
173 /* initialize the debug level(s) */
174 for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
176 if ( mi->mi_ad_managedInfo->ad_type->sat_equality->smr_normalize ) {
179 rc = (*mi->mi_ad_managedInfo->ad_type->sat_equality->smr_normalize)(
180 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
181 mi->mi_ad_managedInfo->ad_type->sat_syntax,
182 mi->mi_ad_managedInfo->ad_type->sat_equality,
184 &int_2_level[ i ].n, NULL );
190 if ( int_2_level[ i ].i & ldap_syslog ) {
191 attr_merge_one( e, mi->mi_ad_managedInfo,
193 &int_2_level[ i ].n );
197 attr_merge( e, mi->mi_ad_description, desc, NULL );
199 monitor_cache_release( mi, e );
205 monitor_subsys_log_modify(
210 struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
212 int newlevel = ldap_syslog;
213 Attribute *save_attrs;
214 Modifications *modlist = op->oq_modify.rs_modlist;
217 ldap_pvt_thread_mutex_lock( &monitor_log_mutex );
219 save_attrs = e->e_attrs;
220 e->e_attrs = attrs_dup( e->e_attrs );
222 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
223 Modification *mod = &ml->sml_mod;
226 * accept all operational attributes
228 if ( is_at_operational( mod->sm_desc->ad_type ) ) {
229 ( void ) attr_delete( &e->e_attrs, mod->sm_desc );
230 rc = attr_merge( e, mod->sm_desc, mod->sm_bvalues,
239 * only the monitor description attribute can be modified
241 } else if ( mod->sm_desc != mi->mi_ad_managedInfo) {
242 rc = LDAP_UNWILLING_TO_PERFORM;
246 switch ( mod->sm_op ) {
248 rc = add_values( e, mod, &newlevel );
251 case LDAP_MOD_DELETE:
252 rc = delete_values( e, mod, &newlevel );
255 case LDAP_MOD_REPLACE:
256 rc = replace_values( e, mod, &newlevel );
264 if ( rc != LDAP_SUCCESS ) {
269 /* set the new debug level */
270 if ( rc == LDAP_SUCCESS ) {
272 static char textbuf[ BACKMONITOR_BUFSIZE ];
274 /* check for abandon */
275 if ( op->o_abandon ) {
281 /* check that the entry still obeys the schema */
282 rc = entry_schema_check( be_monitor, e, save_attrs,
283 &text, textbuf, sizeof( textbuf ) );
284 if ( rc != LDAP_SUCCESS ) {
289 * Do we need to protect this with a mutex?
291 ldap_syslog = newlevel;
293 #if 0 /* debug rather than log */
294 slap_debug = newlevel;
295 lutil_set_debug_level( "slapd", slap_debug );
296 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
297 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
298 ldif_debug = slap_debug;
303 if ( rc == LDAP_SUCCESS ) {
304 attrs_free( save_attrs );
307 attrs_free( e->e_attrs );
308 e->e_attrs = save_attrs;
311 ldap_pvt_thread_mutex_unlock( &monitor_log_mutex );
317 loglevel2int( struct berval *l )
321 for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
322 if ( l->bv_len != int_2_level[ i ].s.bv_len ) {
326 if ( strcasecmp( l->bv_val, int_2_level[ i ].s.bv_val ) == 0 ) {
327 return int_2_level[ i ].i;
335 int2loglevel( int n )
339 for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
340 if ( int_2_level[ i ].i == n ) {
349 check_constraints( Modification *mod, int *newlevel )
353 for ( i = 0; mod->sm_bvalues && mod->sm_bvalues[i].bv_val != NULL; i++ ) {
356 l = loglevel2int( &mod->sm_bvalues[i] );
358 return LDAP_CONSTRAINT_VIOLATION;
361 if ( ( l = int2loglevel( l ) ) == -1 ) {
365 assert( int_2_level[ l ].s.bv_len
366 == mod->sm_bvalues[i].bv_len );
368 AC_MEMCPY( mod->sm_bvalues[i].bv_val,
369 int_2_level[ l ].s.bv_val,
370 int_2_level[ l ].s.bv_len );
372 AC_MEMCPY( mod->sm_nvalues[i].bv_val,
373 int_2_level[ l ].n.bv_val,
374 int_2_level[ l ].n.bv_len );
383 add_values( Entry *e, Modification *mod, int *newlevel )
387 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
389 rc = check_constraints( mod, newlevel );
390 if ( rc != LDAP_SUCCESS ) {
394 a = attr_find( e->e_attrs, mod->sm_desc );
397 /* "description" SHOULD have appropriate rules ... */
398 if ( mr == NULL || !mr->smr_match ) {
399 return LDAP_INAPPROPRIATE_MATCHING;
402 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
405 const char *text = NULL;
406 struct berval asserted;
408 rc = asserted_value_validate_normalize(
409 mod->sm_desc, mr, SLAP_MR_EQUALITY,
410 &mod->sm_bvalues[i], &asserted, &text, NULL );
412 if ( rc != LDAP_SUCCESS ) {
416 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
418 int rc = value_match( &match, mod->sm_desc, mr,
420 &a->a_vals[j], &asserted, &text );
422 if ( rc == LDAP_SUCCESS && match == 0 ) {
423 free( asserted.bv_val );
424 return LDAP_TYPE_OR_VALUE_EXISTS;
428 free( asserted.bv_val );
433 rc = attr_merge( e, mod->sm_desc, mod->sm_bvalues, mod->sm_nvalues );
434 if ( rc != LDAP_SUCCESS ) {
435 /* this should return result of attr_mergeit */
443 delete_values( Entry *e, Modification *mod, int *newlevel )
445 int i, j, k, found, rc, nl = 0;
447 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
449 rc = check_constraints( mod, &nl );
450 if ( rc != LDAP_SUCCESS ) {
456 /* delete the entire attribute */
457 if ( mod->sm_bvalues == NULL ) {
458 int rc = attr_delete( &e->e_attrs, mod->sm_desc );
461 rc = LDAP_NO_SUCH_ATTRIBUTE;
469 if ( mr == NULL || !mr->smr_match ) {
470 /* disallow specific attributes from being deleted if
471 * no equality rule */
472 return LDAP_INAPPROPRIATE_MATCHING;
475 /* delete specific values - find the attribute first */
476 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
477 return( LDAP_NO_SUCH_ATTRIBUTE );
480 /* find each value to delete */
481 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
483 const char *text = NULL;
485 struct berval asserted;
487 rc = asserted_value_validate_normalize(
488 mod->sm_desc, mr, SLAP_MR_EQUALITY,
489 &mod->sm_bvalues[i], &asserted, &text, NULL );
491 if( rc != LDAP_SUCCESS ) return rc;
494 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
496 int rc = value_match( &match, mod->sm_desc, mr,
498 &a->a_vals[j], &asserted, &text );
500 if( rc == LDAP_SUCCESS && match != 0 ) {
504 /* found a matching value */
508 free( a->a_vals[j].bv_val );
509 for ( k = j + 1; a->a_vals[k].bv_val != NULL; k++ ) {
510 a->a_vals[k - 1] = a->a_vals[k];
512 a->a_vals[k - 1].bv_val = NULL;
517 free( asserted.bv_val );
519 /* looked through them all w/o finding it */
521 return LDAP_NO_SUCH_ATTRIBUTE;
525 /* if no values remain, delete the entire attribute */
526 if ( a->a_vals[0].bv_val == NULL ) {
527 /* should already be zero */
530 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
531 return LDAP_NO_SUCH_ATTRIBUTE;
539 replace_values( Entry *e, Modification *mod, int *newlevel )
544 rc = check_constraints( mod, newlevel );
545 if ( rc != LDAP_SUCCESS ) {
549 rc = attr_delete( &e->e_attrs, mod->sm_desc );
551 if ( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
555 if ( mod->sm_bvalues != NULL ) {
556 rc = attr_merge( e, mod->sm_desc, mod->sm_bvalues,
558 if ( rc != LDAP_SUCCESS ) {