📄 srv_eventlog_lib.c
字号:
/* * Unix SMB/CIFS implementation. * Eventlog utility routines * Copyright (C) Marcin Krzysztof Porwit 2005, * Copyright (C) Brian Moran 2005. * Copyright (C) Gerald (Jerry) Carter 2005. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include "includes.h"/* maintain a list of open eventlog tdbs with reference counts */static ELOG_TDB *open_elog_list;/******************************************************************** Init an Eventlog TDB, and return it. If null, something bad happened.********************************************************************/TDB_CONTEXT *elog_init_tdb( char *tdbfilename ){ TDB_CONTEXT *tdb; DEBUG(10,("elog_init_tdb: Initializing eventlog tdb (%s)\n", tdbfilename)); tdb = tdb_open_log( tdbfilename, 0, TDB_DEFAULT, O_RDWR|O_CREAT|O_TRUNC, 0600 ); if ( !tdb ) { DEBUG( 0, ( "Can't open tdb for [%s]\n", tdbfilename ) ); return NULL; } /* initialize with defaults, copy real values in here from registry */ tdb_store_int32( tdb, EVT_OLDEST_ENTRY, 1 ); tdb_store_int32( tdb, EVT_NEXT_RECORD, 1 ); tdb_store_int32( tdb, EVT_MAXSIZE, 0x80000 ); tdb_store_int32( tdb, EVT_RETENTION, 0x93A80 ); tdb_store_int32( tdb, EVT_VERSION, EVENTLOG_DATABASE_VERSION_V1 ); return tdb;}/******************************************************************** make the tdb file name for an event log, given destination buffer and size. Caller must free memory.********************************************************************/char *elog_tdbname( const char *name ){ fstring path; char *tdb_fullpath; char *eventlogdir = lock_path( "eventlog" ); pstr_sprintf( path, "%s/%s.tdb", eventlogdir, name ); strlower_m( path ); tdb_fullpath = SMB_STRDUP( path ); return tdb_fullpath;}/******************************************************************** this function is used to count up the number of bytes in a particular TDB********************************************************************/struct trav_size_struct { int size; int rec_count;};static int eventlog_tdb_size_fn( TDB_CONTEXT * tdb, TDB_DATA key, TDB_DATA data, void *state ){ struct trav_size_struct *tsize = state; tsize->size += data.dsize; tsize->rec_count++; return 0;}/******************************************************************** returns the size of the eventlog, and if MaxSize is a non-null ptr, puts the MaxSize there. This is purely a way not to have yet another function that solely reads the maxsize of the eventlog. Yeah, that's it.********************************************************************/int elog_tdb_size( TDB_CONTEXT * tdb, int *MaxSize, int *Retention ){ struct trav_size_struct tsize; if ( !tdb ) return 0; ZERO_STRUCT( tsize ); tdb_traverse( tdb, eventlog_tdb_size_fn, &tsize ); if ( MaxSize != NULL ) { *MaxSize = tdb_fetch_int32( tdb, EVT_MAXSIZE ); } if ( Retention != NULL ) { *Retention = tdb_fetch_int32( tdb, EVT_RETENTION ); } DEBUG( 1, ( "eventlog size: [%d] for [%d] records\n", tsize.size, tsize.rec_count ) ); return tsize.size;}/******************************************************************** Discard early event logs until we have enough for 'needed' bytes... NO checking done beforehand to see that we actually need to do this, and it's going to pluck records one-by-one. So, it's best to determine that this needs to be done before doing it. Setting whack_by_date to True indicates that eventlogs falling outside of the retention range need to go... return True if we made enough room to accommodate needed bytes********************************************************************/BOOL make_way_for_eventlogs( TDB_CONTEXT * the_tdb, int32 needed, BOOL whack_by_date ){ int start_record, i, new_start; int end_record; int nbytes, reclen, len, Retention, MaxSize; int tresv1, trecnum, timegen, timewr; TDB_DATA key, ret; TALLOC_CTX *mem_ctx = NULL; time_t current_time, exp_time; /* discard some eventlogs */ /* read eventlogs from oldest_entry -- there can't be any discontinuity in recnos, although records not necessarily guaranteed to have successive times */ /* */ mem_ctx = talloc_init( "make_way_for_eventlogs" ); /* Homage to BPG */ if ( mem_ctx == NULL ) return False; /* can't allocate memory indicates bigger problems */ /* lock */ tdb_lock_bystring( the_tdb, EVT_NEXT_RECORD, 1 ); /* read */ end_record = tdb_fetch_int32( the_tdb, EVT_NEXT_RECORD ); start_record = tdb_fetch_int32( the_tdb, EVT_OLDEST_ENTRY ); Retention = tdb_fetch_int32( the_tdb, EVT_RETENTION ); MaxSize = tdb_fetch_int32( the_tdb, EVT_MAXSIZE ); time( ¤t_time ); /* calculate ... */ exp_time = current_time - Retention; /* discard older than exp_time */ /* todo - check for sanity in next_record */ nbytes = 0; DEBUG( 3, ( "MaxSize [%d] Retention [%d] Current Time [%d] exp_time [%d]\n", MaxSize, Retention, (uint32)current_time, (uint32)exp_time ) ); DEBUG( 3, ( "Start Record [%d] End Record [%d]\n", start_record, end_record ) ); for ( i = start_record; i < end_record; i++ ) { /* read a record, add the amt to nbytes */ key.dsize = sizeof( int32 ); key.dptr = ( char * ) ( int32 * ) & i; ret = tdb_fetch( the_tdb, key ); if ( ret.dsize == 0 ) { DEBUG( 8, ( "Can't find a record for the key, record [%d]\n", i ) ); tdb_unlock_bystring( the_tdb, EVT_NEXT_RECORD ); return False; } nbytes += ret.dsize; /* note this includes overhead */ len = tdb_unpack( ret.dptr, ret.dsize, "ddddd", &reclen, &tresv1, &trecnum, &timegen, &timewr ); DEBUG( 8, ( "read record %d, record size is [%d], total so far [%d]\n", i, reclen, nbytes ) ); SAFE_FREE( ret.dptr ); /* note that other servers may just stop writing records when the size limit is reached, and there are no records older than 'retention'. This doesn't like a very useful thing to do, so instead we whack (as in sleeps with the fishes) just enough records to fit the what we need. This behavior could be changed to 'match', if the need arises. */ if ( !whack_by_date && ( nbytes >= needed ) ) break; /* done */ if ( whack_by_date && ( timegen >= exp_time ) ) break; /* done */ } DEBUG( 3, ( "nbytes [%d] needed [%d] start_record is [%d], should be set to [%d]\n", nbytes, needed, start_record, i ) ); /* todo - remove eventlog entries here and set starting record to start_record... */ new_start = i; if ( start_record != new_start ) { for ( i = start_record; i < new_start; i++ ) { key.dsize = sizeof( int32 ); key.dptr = ( char * ) ( int32 * ) & i; tdb_delete( the_tdb, key ); } tdb_store_int32( the_tdb, EVT_OLDEST_ENTRY, new_start ); } tdb_unlock_bystring( the_tdb, EVT_NEXT_RECORD ); return True;}/******************************************************************** some hygiene for an eventlog - see how big it is, and then calculate how many bytes we need to remove ********************************************************************/BOOL prune_eventlog( TDB_CONTEXT * tdb ){ int MaxSize, Retention, CalcdSize; if ( !tdb ) { DEBUG( 4, ( "No eventlog tdb handle\n" ) ); return False; } CalcdSize = elog_tdb_size( tdb, &MaxSize, &Retention ); DEBUG( 3, ( "Calculated size [%d] MaxSize [%d]\n", CalcdSize, MaxSize ) ); if ( CalcdSize > MaxSize ) { return make_way_for_eventlogs( tdb, CalcdSize - MaxSize, False ); } return make_way_for_eventlogs( tdb, 0, True );}/****************************************************************************************************************************************/BOOL can_write_to_eventlog( TDB_CONTEXT * tdb, int32 needed ){ int calcd_size; int MaxSize, Retention; /* see if we can write to the eventlog -- do a policy enforcement */ if ( !tdb ) return False; /* tdb is null, so we can't write to it */ if ( needed < 0 ) return False; MaxSize = 0; Retention = 0; calcd_size = elog_tdb_size( tdb, &MaxSize, &Retention ); if ( calcd_size <= MaxSize ) return True; /* you betcha */ if ( calcd_size + needed < MaxSize ) return True; if ( Retention == 0xffffffff ) { return False; /* see msdn - we can't write no room, discard */ } /* note don't have to test, but always good to show intent, in case changes needed later */ if ( Retention == 0x00000000 ) { /* discard record(s) */ /* todo - decide when to remove a bunch vs. just what we need... */ return make_way_for_eventlogs( tdb, calcd_size - MaxSize, True ); } return make_way_for_eventlogs( tdb, calcd_size - MaxSize, False );}/**************************************************************************************************************************************/ELOG_TDB *elog_open_tdb( char *logname, BOOL force_clear ){ TDB_CONTEXT *tdb = NULL; uint32 vers_id; ELOG_TDB *ptr; char *tdbfilename; pstring tdbpath; ELOG_TDB *tdb_node = NULL; char *eventlogdir; /* first see if we have an open context */ for ( ptr=open_elog_list; ptr; ptr=ptr->next ) { if ( strequal( ptr->name, logname ) ) { ptr->ref_count++; /* trick to alow clearing of the eventlog tdb. The force_clear flag should imply that someone has done a force close. So make sure the tdb is NULL. If this is a normal open, then just return the existing reference */ if ( force_clear ) { SMB_ASSERT( ptr->tdb == NULL ); break; } else return ptr; } } /* make sure that the eventlog dir exists */ eventlogdir = lock_path( "eventlog" ); if ( !directory_exist( eventlogdir, NULL ) ) mkdir( eventlogdir, 0755 ); /* get the path on disk */ tdbfilename = elog_tdbname( logname ); pstrcpy( tdbpath, tdbfilename ); SAFE_FREE( tdbfilename ); DEBUG(7,("elog_open_tdb: Opening %s...(force_clear == %s)\n", tdbpath, force_clear?"True":"False" )); /* the tdb wasn't already open or this is a forced clear open */ if ( !force_clear ) { tdb = tdb_open_log( tdbpath, 0, TDB_DEFAULT, O_RDWR , 0 ); if ( tdb ) { vers_id = tdb_fetch_int32( tdb, EVT_VERSION ); if ( vers_id != EVENTLOG_DATABASE_VERSION_V1 ) { DEBUG(1,("elog_open_tdb: Invalid version [%d] on file [%s].\n", vers_id, tdbpath)); tdb_close( tdb ); tdb = elog_init_tdb( tdbpath ); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -