s3: Explicitly handle inbuf in cli_message_start_done
[Samba/gebeck_regimport.git] / source3 / utils / eventlogadm.c
blobd94f25b42dfbe30ade9f318fa979bca05de4285c
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( " -s <filename>\t\t\t\t\t\t\tUse configuration file <filename>.\n");
44 printf( " -d\t\t\t\t\t\t\t\tturn debug on\n" );
45 printf( " -h\t\t\t\t\t\t\t\tdisplay help\n\n" );
48 static void display_eventlog_names( void )
50 const char **elogs;
51 int i;
53 elogs = lp_eventlog_list( );
54 printf( "Active eventlog names:\n" );
55 printf( "--------------------------------------\n" );
56 if ( elogs ) {
57 for ( i = 0; elogs[i]; i++ ) {
58 printf( "\t%s\n", elogs[i] );
61 else
62 printf( "\t<None specified>\n");
65 static int DoAddSourceCommand( int argc, char **argv, bool debugflag, char *exename )
68 if ( argc < 3 ) {
69 printf( "need more arguments:\n" );
70 printf( "-o addsource EventlogName SourceName /path/to/EventMessageFile.dll\n" );
71 return -1;
73 /* must open the registry before we access it */
74 if (!W_ERROR_IS_OK(regdb_init())) {
75 printf( "Can't open the registry.\n" );
76 return -1;
79 if ( !eventlog_add_source( argv[0], argv[1], argv[2] ) )
80 return -2;
81 return 0;
84 static int DoWriteCommand( int argc, char **argv, bool debugflag, char *exename )
86 FILE *f1;
87 char *argfname;
88 ELOG_TDB *etdb;
89 NTSTATUS status;
91 /* fixed constants are bad bad bad */
92 char linein[1024];
93 bool is_eor;
94 struct eventlog_Record_tdb ee;
95 uint32_t record_number = 0;
96 TALLOC_CTX *mem_ctx = talloc_tos();
98 f1 = stdin;
99 if ( !f1 ) {
100 printf( "Can't open STDIN\n" );
101 return -1;
104 if ( debugflag ) {
105 printf( "Starting write for eventlog [%s]\n", argv[0] );
106 display_eventlog_names( );
109 argfname = argv[0];
111 if ( !( etdb = elog_open_tdb( argfname, False, False ) ) ) {
112 printf( "can't open the eventlog TDB (%s)\n", argfname );
113 return -1;
116 ZERO_STRUCT( ee ); /* MUST initialize between records */
118 while ( !feof( f1 ) ) {
119 if (fgets( linein, sizeof( linein ) - 1, f1 ) == NULL) {
120 break;
122 if ((strlen(linein) > 0)
123 && (linein[strlen(linein)-1] == '\n')) {
124 linein[strlen(linein)-1] = 0;
127 if ( debugflag )
128 printf( "Read line [%s]\n", linein );
130 is_eor = False;
133 parse_logentry( mem_ctx, ( char * ) &linein, &ee, &is_eor );
134 /* should we do something with the return code? */
136 if ( is_eor ) {
137 fixup_eventlog_record_tdb( &ee );
139 if ( opt_debug )
140 printf( "record number [%d], tg [%d] , tw [%d]\n",
141 ee.record_number, (int)ee.time_generated, (int)ee.time_written );
143 if ( ee.time_generated != 0 ) {
145 /* printf("Writing to the event log\n"); */
147 status = evlog_push_record_tdb( mem_ctx, ELOG_TDB_CTX(etdb),
148 &ee, &record_number );
149 if ( !NT_STATUS_IS_OK(status) ) {
150 printf( "Can't write to the event log: %s\n",
151 nt_errstr(status) );
152 } else {
153 if ( opt_debug )
154 printf( "Wrote record %d\n",
155 record_number );
157 } else {
158 if ( opt_debug )
159 printf( "<null record>\n" );
161 ZERO_STRUCT( ee ); /* MUST initialize between records */
165 elog_close_tdb( etdb , False );
167 return 0;
170 static int DoDumpCommand(int argc, char **argv, bool debugflag, char *exename)
172 ELOG_TDB *etdb;
173 TALLOC_CTX *mem_ctx = talloc_tos();
174 const char *tdb_filename;
175 uint32_t count = 1;
177 if (argc > 2) {
178 return -1;
181 tdb_filename = argv[0];
183 if (argc > 1) {
184 count = atoi(argv[1]);
187 etdb = elog_open_tdb(argv[0], false, true);
188 if (!etdb) {
189 printf("can't open the eventlog TDB (%s)\n", argv[0]);
190 return -1;
193 while (1) {
195 struct eventlog_Record_tdb *r;
196 char *s;
198 r = evlog_pull_record_tdb(mem_ctx, etdb->tdb, count);
199 if (!r) {
200 break;
203 printf("displaying record: %d\n", count);
205 s = NDR_PRINT_STRUCT_STRING(mem_ctx, eventlog_Record_tdb, r);
206 if (s) {
207 printf("%s\n", s);
208 talloc_free(s);
210 count++;
213 elog_close_tdb(etdb, false);
215 return 0;
218 /* would be nice to use the popT stuff here, however doing so forces us to drag in a lot of other infrastructure */
220 int main( int argc, char *argv[] )
222 int opt, rc;
223 char *exename;
224 char *configfile = NULL;
225 TALLOC_CTX *frame = talloc_stackframe();
228 fstring opname;
230 load_case_tables();
232 opt_debug = 0; /* todo set this from getopts */
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:s:" ) ) != 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;
259 case 's':
260 configfile = talloc_strdup(frame, optarg);
261 break;
266 argc -= optind;
267 argv += optind;
269 if ( argc < 1 ) {
270 printf( "\nNot enough arguments!\n" );
271 usage( exename );
272 exit( 1 );
275 if ( configfile == NULL ) {
276 lp_load(get_dyn_CONFIGFILE(), True, False, False, True);
277 } else if (!lp_load(configfile, True, False, False, True)) {
278 printf("Unable to parse configfile '%s'\n",configfile);
279 exit( 1 );
282 /* note that the separate command types should call usage if they need to... */
283 while ( 1 ) {
284 if ( !StrCaseCmp( opname, "addsource" ) ) {
285 rc = DoAddSourceCommand( argc, argv, opt_debug,
286 exename );
287 break;
289 if ( !StrCaseCmp( opname, "write" ) ) {
290 rc = DoWriteCommand( argc, argv, opt_debug, exename );
291 break;
293 if ( !StrCaseCmp( opname, "dump" ) ) {
294 rc = DoDumpCommand( argc, argv, opt_debug, exename );
295 break;
297 printf( "unknown command [%s]\n", opname );
298 usage( exename );
299 exit( 1 );
300 break;
302 TALLOC_FREE(frame);
303 return rc;