s3:registry: extract the reg_backend_db prototypes into their own header.
[Samba/ekacnet.git] / source3 / utils / eventlogadm.c
blob7dbcf7d3713097b1118170c6dfe0b134d49b4b7f
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"
26 #include "lib/eventlog/eventlog.h"
27 #include "registry.h"
28 #include "registry/reg_backend_db.h"
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_UTIL_EVENTLOG
34 extern int optind;
35 extern char *optarg;
37 int opt_debug = 0;
39 static void usage( char *s )
41 printf( "\nUsage: %s [OPTION]\n\n", s );
42 printf( " -o write <Eventlog Name> \t\t\t\t\tWrites records to eventlog from STDIN\n" );
43 printf( " -o addsource <EventlogName> <sourcename> <msgfileDLLname> \tAdds the specified source & DLL eventlog registry entry\n" );
44 printf( " -o dump <Eventlog Name> <starting_record>\t\t\t\t\tDump stored eventlog entries on STDOUT\n" );
45 printf( "\nMiscellaneous options:\n" );
46 printf( " -s <filename>\t\t\t\t\t\t\tUse configuration file <filename>.\n");
47 printf( " -d\t\t\t\t\t\t\t\tturn debug on\n" );
48 printf( " -h\t\t\t\t\t\t\t\tdisplay help\n\n" );
51 static void display_eventlog_names( void )
53 const char **elogs;
54 int i;
56 elogs = lp_eventlog_list( );
57 printf( "Active eventlog names:\n" );
58 printf( "--------------------------------------\n" );
59 if ( elogs ) {
60 for ( i = 0; elogs[i]; i++ ) {
61 printf( "\t%s\n", elogs[i] );
64 else
65 printf( "\t<None specified>\n");
68 static int DoAddSourceCommand( int argc, char **argv, bool debugflag, char *exename )
71 if ( argc < 3 ) {
72 printf( "need more arguments:\n" );
73 printf( "-o addsource EventlogName SourceName /path/to/EventMessageFile.dll\n" );
74 return -1;
76 /* must open the registry before we access it */
77 if (!W_ERROR_IS_OK(regdb_init())) {
78 printf( "Can't open the registry.\n" );
79 return -1;
82 if ( !eventlog_add_source( argv[0], argv[1], argv[2] ) )
83 return -2;
84 return 0;
87 static int DoWriteCommand( int argc, char **argv, bool debugflag, char *exename )
89 FILE *f1;
90 char *argfname;
91 ELOG_TDB *etdb;
92 NTSTATUS status;
94 /* fixed constants are bad bad bad */
95 char linein[1024];
96 bool is_eor;
97 struct eventlog_Record_tdb ee;
98 uint32_t record_number = 0;
99 TALLOC_CTX *mem_ctx = talloc_tos();
101 f1 = stdin;
102 if ( !f1 ) {
103 printf( "Can't open STDIN\n" );
104 return -1;
107 if ( debugflag ) {
108 printf( "Starting write for eventlog [%s]\n", argv[0] );
109 display_eventlog_names( );
112 argfname = argv[0];
114 if ( !( etdb = elog_open_tdb( argfname, False, False ) ) ) {
115 printf( "can't open the eventlog TDB (%s)\n", argfname );
116 return -1;
119 ZERO_STRUCT( ee ); /* MUST initialize between records */
121 while ( !feof( f1 ) ) {
122 if (fgets( linein, sizeof( linein ) - 1, f1 ) == NULL) {
123 break;
125 if ((strlen(linein) > 0)
126 && (linein[strlen(linein)-1] == '\n')) {
127 linein[strlen(linein)-1] = 0;
130 if ( debugflag )
131 printf( "Read line [%s]\n", linein );
133 is_eor = False;
136 parse_logentry( mem_ctx, ( char * ) &linein, &ee, &is_eor );
137 /* should we do something with the return code? */
139 if ( is_eor ) {
140 fixup_eventlog_record_tdb( &ee );
142 if ( opt_debug )
143 printf( "record number [%d], tg [%d] , tw [%d]\n",
144 ee.record_number, (int)ee.time_generated, (int)ee.time_written );
146 if ( ee.time_generated != 0 ) {
148 /* printf("Writing to the event log\n"); */
150 status = evlog_push_record_tdb( mem_ctx, ELOG_TDB_CTX(etdb),
151 &ee, &record_number );
152 if ( !NT_STATUS_IS_OK(status) ) {
153 printf( "Can't write to the event log: %s\n",
154 nt_errstr(status) );
155 } else {
156 if ( opt_debug )
157 printf( "Wrote record %d\n",
158 record_number );
160 } else {
161 if ( opt_debug )
162 printf( "<null record>\n" );
164 ZERO_STRUCT( ee ); /* MUST initialize between records */
168 elog_close_tdb( etdb , False );
170 return 0;
173 static int DoDumpCommand(int argc, char **argv, bool debugflag, char *exename)
175 ELOG_TDB *etdb;
176 TALLOC_CTX *mem_ctx = talloc_tos();
177 const char *tdb_filename;
178 uint32_t count = 1;
180 if (argc > 2) {
181 return -1;
184 tdb_filename = argv[0];
186 if (argc > 1) {
187 count = atoi(argv[1]);
190 etdb = elog_open_tdb(argv[0], false, true);
191 if (!etdb) {
192 printf("can't open the eventlog TDB (%s)\n", argv[0]);
193 return -1;
196 while (1) {
198 struct eventlog_Record_tdb *r;
199 char *s;
201 r = evlog_pull_record_tdb(mem_ctx, etdb->tdb, count);
202 if (!r) {
203 break;
206 printf("displaying record: %d\n", count);
208 s = NDR_PRINT_STRUCT_STRING(mem_ctx, eventlog_Record_tdb, r);
209 if (s) {
210 printf("%s\n", s);
211 talloc_free(s);
213 count++;
216 elog_close_tdb(etdb, false);
218 return 0;
221 /* would be nice to use the popT stuff here, however doing so forces us to drag in a lot of other infrastructure */
223 int main( int argc, char *argv[] )
225 int opt, rc;
226 char *exename;
227 char *configfile = NULL;
228 TALLOC_CTX *frame = talloc_stackframe();
231 fstring opname;
233 load_case_tables();
235 opt_debug = 0; /* todo set this from getopts */
237 exename = argv[0];
239 /* default */
241 fstrcpy( opname, "write" ); /* the default */
243 #if 0 /* TESTING CODE */
244 eventlog_add_source( "System", "TestSourceX", "SomeTestPathX" );
245 #endif
246 while ( ( opt = getopt( argc, argv, "dho:s:" ) ) != EOF ) {
247 switch ( opt ) {
249 case 'o':
250 fstrcpy( opname, optarg );
251 break;
253 case 'h':
254 usage( exename );
255 display_eventlog_names( );
256 exit( 0 );
257 break;
259 case 'd':
260 opt_debug = 1;
261 break;
262 case 's':
263 configfile = talloc_strdup(frame, optarg);
264 break;
269 argc -= optind;
270 argv += optind;
272 if ( argc < 1 ) {
273 printf( "\nNot enough arguments!\n" );
274 usage( exename );
275 exit( 1 );
278 if ( configfile == NULL ) {
279 lp_load(get_dyn_CONFIGFILE(), True, False, False, True);
280 } else if (!lp_load(configfile, True, False, False, True)) {
281 printf("Unable to parse configfile '%s'\n",configfile);
282 exit( 1 );
285 /* note that the separate command types should call usage if they need to... */
286 while ( 1 ) {
287 if ( !StrCaseCmp( opname, "addsource" ) ) {
288 rc = DoAddSourceCommand( argc, argv, opt_debug,
289 exename );
290 break;
292 if ( !StrCaseCmp( opname, "write" ) ) {
293 rc = DoWriteCommand( argc, argv, opt_debug, exename );
294 break;
296 if ( !StrCaseCmp( opname, "dump" ) ) {
297 rc = DoDumpCommand( argc, argv, opt_debug, exename );
298 break;
300 printf( "unknown command [%s]\n", opname );
301 usage( exename );
302 exit( 1 );
303 break;
305 TALLOC_FREE(frame);
306 return rc;