Fix bug 7005 - mangle method = hash truncates files with dot '. ' character
[Samba.git] / source / utils / eventlogadm.c
blob5fed4d1a399867c7a8bf40e54d61ac1558218847
2 /*
3 * Samba Unix/Linux SMB client utility
4 * Write Eventlog records to a tdb, perform other eventlog related functions
7 * Copyright (C) Brian Moran 2005.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_UTIL_EVENTLOG
30 extern int optind;
31 extern char *optarg;
33 int opt_debug = 0;
35 static void usage( char *s )
37 printf( "\nUsage: %s [OPTION]\n\n", s );
38 printf( " -o write <Eventlog Name> \t\t\t\t\tWrites records to eventlog from STDIN\n" );
39 printf( " -o addsource <EventlogName> <sourcename> <msgfileDLLname> \tAdds the specified source & DLL eventlog registry entry\n" );
40 printf( "\nMiscellaneous options:\n" );
41 printf( " -d\t\t\t\t\t\t\t\tturn debug on\n" );
42 printf( " -h\t\t\t\t\t\t\t\tdisplay help\n\n" );
45 static void display_eventlog_names( void )
47 const char **elogs;
48 int i;
50 elogs = lp_eventlog_list( );
51 printf( "Active eventlog names (from smb.conf):\n" );
52 printf( "--------------------------------------\n" );
53 if ( elogs ) {
54 for ( i = 0; elogs[i]; i++ ) {
55 printf( "\t%s\n", elogs[i] );
58 else
59 printf( "\t<None specified>\n");
62 static int DoAddSourceCommand( int argc, char **argv, bool debugflag, char *exename )
65 if ( argc < 3 ) {
66 printf( "need more arguments:\n" );
67 printf( "-o addsource EventlogName SourceName /path/to/EventMessageFile.dll\n" );
68 return -1;
70 /* must open the registry before we access it */
71 if (!W_ERROR_IS_OK(regdb_init())) {
72 printf( "Can't open the registry.\n" );
73 return -1;
76 if ( !eventlog_add_source( argv[0], argv[1], argv[2] ) )
77 return -2;
78 return 0;
81 static int DoWriteCommand( int argc, char **argv, bool debugflag, char *exename )
83 FILE *f1;
84 char *argfname;
85 ELOG_TDB *etdb;
87 /* fixed constants are bad bad bad */
88 char linein[1024];
89 bool is_eor;
90 Eventlog_entry ee;
91 int rcnum;
93 f1 = stdin;
94 if ( !f1 ) {
95 printf( "Can't open STDIN\n" );
96 return -1;
99 if ( debugflag ) {
100 printf( "Starting write for eventlog [%s]\n", argv[0] );
101 display_eventlog_names( );
104 argfname = argv[0];
106 if ( !( etdb = elog_open_tdb( argfname, False ) ) ) {
107 printf( "can't open the eventlog TDB (%s)\n", argfname );
108 return -1;
111 ZERO_STRUCT( ee ); /* MUST initialize between records */
113 while ( !feof( f1 ) ) {
114 if (fgets( linein, sizeof( linein ) - 1, f1 ) == NULL) {
115 break;
117 linein[strlen( linein ) - 1] = 0; /* whack the line delimiter */
119 if ( debugflag )
120 printf( "Read line [%s]\n", linein );
122 is_eor = False;
125 parse_logentry( ( char * ) &linein, &ee, &is_eor );
126 /* should we do something with the return code? */
128 if ( is_eor ) {
129 fixup_eventlog_entry( &ee );
131 if ( opt_debug )
132 printf( "record number [%d], tg [%d] , tw [%d]\n", ee.record.record_number, ee.record.time_generated, ee.record.time_written );
134 if ( ee.record.time_generated != 0 ) {
136 /* printf("Writing to the event log\n"); */
138 rcnum = write_eventlog_tdb( ELOG_TDB_CTX(etdb), &ee );
139 if ( !rcnum ) {
140 printf( "Can't write to the event log\n" );
141 } else {
142 if ( opt_debug )
143 printf( "Wrote record %d\n",
144 rcnum );
146 } else {
147 if ( opt_debug )
148 printf( "<null record>\n" );
150 ZERO_STRUCT( ee ); /* MUST initialize between records */
154 elog_close_tdb( etdb , False );
156 return 0;
159 /* would be nice to use the popT stuff here, however doing so forces us to drag in a lot of other infrastructure */
161 int main( int argc, char *argv[] )
163 int opt, rc;
164 char *exename;
165 TALLOC_CTX *frame = talloc_stackframe();
168 fstring opname;
170 load_case_tables();
172 opt_debug = 0; /* todo set this from getopts */
174 lp_load(get_dyn_CONFIGFILE(), True, False, False, True);
176 exename = argv[0];
178 /* default */
180 fstrcpy( opname, "write" ); /* the default */
182 #if 0 /* TESTING CODE */
183 eventlog_add_source( "System", "TestSourceX", "SomeTestPathX" );
184 #endif
185 while ( ( opt = getopt( argc, argv, "dho:" ) ) != EOF ) {
186 switch ( opt ) {
188 case 'o':
189 fstrcpy( opname, optarg );
190 break;
192 case 'h':
193 usage( exename );
194 display_eventlog_names( );
195 exit( 0 );
196 break;
198 case 'd':
199 opt_debug = 1;
200 break;
204 argc -= optind;
205 argv += optind;
207 if ( argc < 1 ) {
208 printf( "\nNot enough arguments!\n" );
209 usage( exename );
210 exit( 1 );
213 /* note that the separate command types should call usage if they need to... */
214 while ( 1 ) {
215 if ( !StrCaseCmp( opname, "addsource" ) ) {
216 rc = DoAddSourceCommand( argc, argv, opt_debug,
217 exename );
218 break;
220 if ( !StrCaseCmp( opname, "write" ) ) {
221 rc = DoWriteCommand( argc, argv, opt_debug, exename );
222 break;
224 printf( "unknown command [%s]\n", opname );
225 usage( exename );
226 exit( 1 );
227 break;
229 TALLOC_FREE(frame);
230 return rc;