s3: Fix bug #9085.
[Samba.git] / source3 / utils / eventlogadm.c
blob415330f68c898839bff377a4ce304898fcb5051b
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.
8 * Copyright (C) Guenther Deschner 2009.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_UTIL_EVENTLOG
31 extern int optind;
32 extern char *optarg;
34 int opt_debug = 0;
36 static void usage( char *s )
38 printf( "\nUsage: %s [OPTION]\n\n", s );
39 printf( " -o write <Eventlog Name> \t\t\t\t\tWrites records to eventlog from STDIN\n" );
40 printf( " -o addsource <EventlogName> <sourcename> <msgfileDLLname> \tAdds the specified source & DLL eventlog registry entry\n" );
41 printf( " -o dump <Eventlog Name> <starting_record>\t\t\t\t\tDump stored eventlog entries on STDOUT\n" );
42 printf( "\nMiscellaneous options:\n" );
43 printf( " -d\t\t\t\t\t\t\t\tturn debug on\n" );
44 printf( " -h\t\t\t\t\t\t\t\tdisplay help\n\n" );
47 static void display_eventlog_names( void )
49 const char **elogs;
50 int i;
52 elogs = lp_eventlog_list( );
53 printf( "Active eventlog names (from smb.conf):\n" );
54 printf( "--------------------------------------\n" );
55 if ( elogs ) {
56 for ( i = 0; elogs[i]; i++ ) {
57 printf( "\t%s\n", elogs[i] );
60 else
61 printf( "\t<None specified>\n");
64 static int DoAddSourceCommand( int argc, char **argv, bool debugflag, char *exename )
67 if ( argc < 3 ) {
68 printf( "need more arguments:\n" );
69 printf( "-o addsource EventlogName SourceName /path/to/EventMessageFile.dll\n" );
70 return -1;
72 /* must open the registry before we access it */
73 if (!W_ERROR_IS_OK(regdb_init())) {
74 printf( "Can't open the registry.\n" );
75 return -1;
78 if ( !eventlog_add_source( argv[0], argv[1], argv[2] ) )
79 return -2;
80 return 0;
83 static int DoWriteCommand( int argc, char **argv, bool debugflag, char *exename )
85 FILE *f1;
86 char *argfname;
87 ELOG_TDB *etdb;
88 NTSTATUS status;
90 /* fixed constants are bad bad bad */
91 char linein[1024];
92 bool is_eor;
93 struct eventlog_Record_tdb ee;
94 uint32_t record_number = 0;
95 TALLOC_CTX *mem_ctx = talloc_tos();
97 f1 = stdin;
98 if ( !f1 ) {
99 printf( "Can't open STDIN\n" );
100 return -1;
103 if ( debugflag ) {
104 printf( "Starting write for eventlog [%s]\n", argv[0] );
105 display_eventlog_names( );
108 argfname = argv[0];
110 if ( !( etdb = elog_open_tdb( argfname, False, False ) ) ) {
111 printf( "can't open the eventlog TDB (%s)\n", argfname );
112 return -1;
115 ZERO_STRUCT( ee ); /* MUST initialize between records */
117 while ( !feof( f1 ) ) {
118 if (fgets( linein, sizeof( linein ) - 1, f1 ) == NULL) {
119 break;
121 if ((strlen(linein) > 0)
122 && (linein[strlen(linein)-1] == '\n')) {
123 linein[strlen(linein)-1] = 0;
126 if ( debugflag )
127 printf( "Read line [%s]\n", linein );
129 is_eor = False;
132 parse_logentry( mem_ctx, ( char * ) &linein, &ee, &is_eor );
133 /* should we do something with the return code? */
135 if ( is_eor ) {
136 fixup_eventlog_record_tdb( &ee );
138 if ( opt_debug )
139 printf( "record number [%d], tg [%d] , tw [%d]\n",
140 ee.record_number, (int)ee.time_generated, (int)ee.time_written );
142 if ( ee.time_generated != 0 ) {
144 /* printf("Writing to the event log\n"); */
146 status = evlog_push_record_tdb( mem_ctx, ELOG_TDB_CTX(etdb),
147 &ee, &record_number );
148 if ( !NT_STATUS_IS_OK(status) ) {
149 printf( "Can't write to the event log: %s\n",
150 nt_errstr(status) );
151 } else {
152 if ( opt_debug )
153 printf( "Wrote record %d\n",
154 record_number );
156 } else {
157 if ( opt_debug )
158 printf( "<null record>\n" );
160 ZERO_STRUCT( ee ); /* MUST initialize between records */
164 elog_close_tdb( etdb , False );
166 return 0;
169 static int DoDumpCommand(int argc, char **argv, bool debugflag, char *exename)
171 ELOG_TDB *etdb;
172 TALLOC_CTX *mem_ctx = talloc_tos();
173 const char *tdb_filename;
174 uint32_t count = 1;
176 if (argc > 2) {
177 return -1;
180 tdb_filename = argv[0];
182 if (argc > 1) {
183 count = atoi(argv[1]);
186 etdb = elog_open_tdb(argv[0], false, true);
187 if (!etdb) {
188 printf("can't open the eventlog TDB (%s)\n", argv[0]);
189 return -1;
192 while (1) {
194 struct eventlog_Record_tdb *r;
195 char *s;
197 r = evlog_pull_record_tdb(mem_ctx, etdb->tdb, count);
198 if (!r) {
199 break;
202 printf("displaying record: %d\n", count);
204 s = NDR_PRINT_STRUCT_STRING(mem_ctx, eventlog_Record_tdb, r);
205 if (s) {
206 printf("%s\n", s);
207 talloc_free(s);
209 count++;
212 elog_close_tdb(etdb, false);
214 return 0;
217 /* would be nice to use the popT stuff here, however doing so forces us to drag in a lot of other infrastructure */
219 int main( int argc, char *argv[] )
221 int opt, rc;
222 char *exename;
223 TALLOC_CTX *frame = talloc_stackframe();
226 fstring opname;
228 load_case_tables();
230 opt_debug = 0; /* todo set this from getopts */
232 lp_load(get_dyn_CONFIGFILE(), True, False, False, True);
234 exename = argv[0];
236 /* default */
238 fstrcpy( opname, "write" ); /* the default */
240 #if 0 /* TESTING CODE */
241 eventlog_add_source( "System", "TestSourceX", "SomeTestPathX" );
242 #endif
243 while ( ( opt = getopt( argc, argv, "dho:" ) ) != EOF ) {
244 switch ( opt ) {
246 case 'o':
247 fstrcpy( opname, optarg );
248 break;
250 case 'h':
251 usage( exename );
252 display_eventlog_names( );
253 exit( 0 );
254 break;
256 case 'd':
257 opt_debug = 1;
258 break;
262 argc -= optind;
263 argv += optind;
265 if ( argc < 1 ) {
266 printf( "\nNot enough arguments!\n" );
267 usage( exename );
268 exit( 1 );
271 /* note that the separate command types should call usage if they need to... */
272 while ( 1 ) {
273 if ( !StrCaseCmp( opname, "addsource" ) ) {
274 rc = DoAddSourceCommand( argc, argv, opt_debug,
275 exename );
276 break;
278 if ( !StrCaseCmp( opname, "write" ) ) {
279 rc = DoWriteCommand( argc, argv, opt_debug, exename );
280 break;
282 if ( !StrCaseCmp( opname, "dump" ) ) {
283 rc = DoDumpCommand( argc, argv, opt_debug, exename );
284 break;
286 printf( "unknown command [%s]\n", opname );
287 usage( exename );
288 exit( 1 );
289 break;
291 TALLOC_FREE(frame);
292 return rc;