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/>.
26 #include "lib/eventlog/eventlog.h"
28 #include "registry/reg_api.h"
29 #include "registry/reg_init_basic.h"
30 #include "registry/reg_util_token.h"
31 #include "registry/reg_backend_db.h"
32 #include "../libcli/registry/util_reg.h"
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 )
56 elogs
= lp_eventlog_list( );
57 printf( "Active eventlog names:\n" );
58 printf( "--------------------------------------\n" );
60 for ( i
= 0; elogs
[i
]; i
++ ) {
61 printf( "\t%s\n", elogs
[i
] );
65 printf( "\t<None specified>\n");
68 /*********************************************************************
69 for an eventlog, add in a source name. If the eventlog doesn't
70 exist (not in the list) do nothing. If a source for the log
71 already exists, change the information (remove, replace)
72 *********************************************************************/
73 static bool eventlog_add_source( const char *eventlog
, const char *sourcename
,
74 const char *messagefile
)
76 /* Find all of the eventlogs, add keys for each of them */
77 /* need to add to the value KEY_EVENTLOG/<eventlog>/Sources string (Creating if necessary)
78 need to add KEY of source to KEY_EVENTLOG/<eventlog>/<source> */
80 const char **elogs
= lp_eventlog_list( );
81 const char **wrklist
, **wp
;
82 char *evtlogpath
= NULL
;
87 TALLOC_CTX
*ctx
= talloc_stackframe();
89 struct registry_key
*key_hive
, *key_eventlog
, *key_source
;
90 struct security_token
*token
= NULL
;
91 const char *hive_name
, *relpath
;
92 enum winreg_CreateAction action
;
93 struct registry_value
*value
;
94 static const uint32_t ACCESS
= REG_KEY_READ
| REG_KEY_WRITE
;
98 d_printf("No Eventlogs configured\n");
102 for ( i
= 0; elogs
[i
]; i
++ ) {
103 if ( strequal( elogs
[i
], eventlog
) )
108 d_printf("Eventlog [%s] not found in list of valid event logs\n",
113 /* have to assume that the evenlog key itself exists at this point */
114 /* add in a key of [sourcename] under the eventlog key */
116 /* todo add to Sources */
118 evtlogpath
= talloc_asprintf(ctx
, "%s\\%s", KEY_EVENTLOG
, eventlog
);
120 d_printf("Out of memory\n");
124 relpath
= evtlogpath
+ sizeof(KEY_EVENTLOG
);
125 hive_name
= talloc_strndup(ctx
, evtlogpath
, relpath
- evtlogpath
);
127 d_printf("Out of memory\n");
132 werr
= ntstatus_to_werror(registry_create_admin_token(ctx
, &token
));
133 if (!W_ERROR_IS_OK(werr
)) {
134 d_printf("Failed to create admin token: %s\n", win_errstr(werr
));
138 werr
= reg_openhive(ctx
, hive_name
, ACCESS
, token
, &key_hive
);
139 if (!W_ERROR_IS_OK(werr
)) {
140 d_printf("Failed to open hive [%s]: %s\n", hive_name
, win_errstr(werr
));
144 werr
= reg_openkey(ctx
, key_hive
, relpath
, ACCESS
, &key_eventlog
);
145 if (!W_ERROR_IS_OK(werr
)) {
146 d_printf("Failed to open key [%s]: %s\n", evtlogpath
, win_errstr(werr
));
150 werr
= reg_queryvalue(ctx
, key_eventlog
, "Sources", &value
);
151 if (!W_ERROR_IS_OK(werr
)) {
152 d_printf("Failed to get value \"Sources\" for [%s]: %s\n", evtlogpath
, win_errstr(werr
));
155 /* perhaps this adding a new string to a multi_sz should be a fn? */
156 /* check to see if it's there already */
158 if ( value
->type
!= REG_MULTI_SZ
) {
159 d_printf("Wrong type for \"Sources\", should be REG_MULTI_SZ\n");
162 /* convert to a 'regulah' chars to do some comparisons */
166 dump_data(1, value
->data
.data
, value
->data
.length
);
168 if (!pull_reg_multi_sz(ctx
, &value
->data
, &wrklist
)) {
169 d_printf("Failed to pull REG_MULTI_SZ from \"Sources\"\n");
173 for (ii
=0; wrklist
[ii
]; ii
++) {
177 if (numsources
> 0) {
178 /* see if it's in there already */
182 if ( strequal( *wp
, sourcename
) ) {
183 d_printf("Source name [%s] already in list for [%s] \n",
184 sourcename
, eventlog
);
191 d_printf("Nothing in the sources list, this might be a problem\n");
195 /* make a new list with an additional entry; copy values, add another */
196 wp
= talloc_realloc(ctx
, wrklist
, const char *, numsources
+ 2 );
198 d_printf("Out of memory\n");
202 wp
[numsources
] = sourcename
;
203 wp
[numsources
+1] = NULL
;
204 if (!push_reg_multi_sz(ctx
, &value
->data
, wp
)) {
205 d_printf("Failed to push Sources\n");
208 dump_data( 1, value
->data
.data
, value
->data
.length
);
209 werr
= reg_setvalue(key_eventlog
, "Sources", value
);
210 if (!W_ERROR_IS_OK(werr
)) {
211 d_printf("Failed to set value Sources: %s\n", win_errstr(werr
));
215 d_printf("Source name [%s] found in existing list of sources\n",
219 werr
= reg_createkey(ctx
, key_eventlog
, sourcename
, ACCESS
, &key_source
, &action
);
220 if (!W_ERROR_IS_OK(werr
)) {
221 d_printf("Failed to create subkey \"%s\" of \"%s\": %s\n", sourcename
, evtlogpath
, win_errstr(werr
));
225 if (action
== REG_CREATED_NEW_KEY
) {
226 d_printf(" Source name [%s] for eventlog [%s] didn't exist, adding \n",
227 sourcename
, eventlog
);
230 /* at this point KEY_EVENTLOG/<eventlog>/<sourcename> key is in there. Now need to add EventMessageFile */
232 /* now add the values to the KEY_EVENTLOG/Application form key */
233 d_printf("Storing EventMessageFile [%s] to eventlog path of [%s]\n",
234 messagefile
, evtlogpath
);
236 if (!push_reg_sz(ctx
, &value
->data
, messagefile
)) {
237 d_printf("Failed to push \"EventMessageFile\"\n");
240 value
->type
= REG_SZ
;
242 werr
= reg_setvalue(key_source
, "EventMessageFile", value
);
243 if (!W_ERROR_IS_OK(werr
)) {
244 d_printf("Failed to set value \"EventMessageFile\": %s\n", win_errstr(werr
));
253 static int DoAddSourceCommand( int argc
, char **argv
, bool debugflag
, char *exename
)
258 printf( "need more arguments:\n" );
259 printf( "-o addsource EventlogName SourceName /path/to/EventMessageFile.dll\n" );
263 /* must open the registry before we access it */
264 werr
= registry_init_common();
265 if (!W_ERROR_IS_OK(werr
)) {
266 printf("Can't open the registry: %s.\n", win_errstr(werr
));
269 werr
= regdb_transaction_start();
270 if (!W_ERROR_IS_OK(werr
)) {
271 printf("Can't start transaction on registry: %s.\n", win_errstr(werr
));
275 if ( !eventlog_add_source( argv
[0], argv
[1], argv
[2] ) ) {
276 regdb_transaction_cancel();
279 werr
= regdb_transaction_commit();
280 if (!W_ERROR_IS_OK(werr
)) {
281 printf("Failed to commit transaction on registry: %s.\n", win_errstr(werr
));
287 static int DoWriteCommand( int argc
, char **argv
, bool debugflag
, char *exename
)
294 /* fixed constants are bad bad bad */
297 struct eventlog_Record_tdb ee
;
298 uint32_t record_number
= 0;
299 TALLOC_CTX
*mem_ctx
= talloc_tos();
303 printf( "Can't open STDIN\n" );
308 printf( "Starting write for eventlog [%s]\n", argv
[0] );
309 display_eventlog_names( );
314 if ( !( etdb
= elog_open_tdb( argfname
, False
, False
) ) ) {
315 printf( "can't open the eventlog TDB (%s)\n", argfname
);
319 ZERO_STRUCT( ee
); /* MUST initialize between records */
321 while ( !feof( f1
) ) {
322 if (fgets( linein
, sizeof( linein
) - 1, f1
) == NULL
) {
325 if ((strlen(linein
) > 0)
326 && (linein
[strlen(linein
)-1] == '\n')) {
327 linein
[strlen(linein
)-1] = 0;
331 printf( "Read line [%s]\n", linein
);
336 parse_logentry( mem_ctx
, ( char * ) &linein
, &ee
, &is_eor
);
337 /* should we do something with the return code? */
340 fixup_eventlog_record_tdb( &ee
);
343 printf( "record number [%d], tg [%d] , tw [%d]\n",
344 ee
.record_number
, (int)ee
.time_generated
, (int)ee
.time_written
);
346 if ( ee
.time_generated
!= 0 ) {
348 /* printf("Writing to the event log\n"); */
350 status
= evlog_push_record_tdb( mem_ctx
, ELOG_TDB_CTX(etdb
),
351 &ee
, &record_number
);
352 if ( !NT_STATUS_IS_OK(status
) ) {
353 printf( "Can't write to the event log: %s\n",
357 printf( "Wrote record %d\n",
362 printf( "<null record>\n" );
364 ZERO_STRUCT( ee
); /* MUST initialize between records */
368 elog_close_tdb( etdb
, False
);
373 static int DoDumpCommand(int argc
, char **argv
, bool debugflag
, char *exename
)
376 TALLOC_CTX
*mem_ctx
= talloc_tos();
384 count
= atoi(argv
[1]);
387 etdb
= elog_open_tdb(argv
[0], false, true);
389 printf("can't open the eventlog TDB (%s)\n", argv
[0]);
395 struct eventlog_Record_tdb
*r
;
398 r
= evlog_pull_record_tdb(mem_ctx
, etdb
->tdb
, count
);
403 printf("displaying record: %d\n", count
);
405 s
= NDR_PRINT_STRUCT_STRING(mem_ctx
, eventlog_Record_tdb
, r
);
413 elog_close_tdb(etdb
, false);
418 /* would be nice to use the popT stuff here, however doing so forces us to drag in a lot of other infrastructure */
420 int main( int argc
, char *argv
[] )
424 char *configfile
= NULL
;
425 TALLOC_CTX
*frame
= talloc_stackframe();
432 opt_debug
= 0; /* todo set this from getopts */
438 fstrcpy( opname
, "write" ); /* the default */
440 #if 0 /* TESTING CODE */
441 eventlog_add_source( "System", "TestSourceX", "SomeTestPathX" );
443 while ( ( opt
= getopt( argc
, argv
, "dho:s:" ) ) != EOF
) {
447 fstrcpy( opname
, optarg
);
452 display_eventlog_names( );
460 configfile
= talloc_strdup(frame
, optarg
);
470 printf( "\nNot enough arguments!\n" );
475 if ( configfile
== NULL
) {
476 lp_load_global(get_dyn_CONFIGFILE());
477 } else if (!lp_load_global(configfile
)) {
478 printf("Unable to parse configfile '%s'\n",configfile
);
482 /* note that the separate command types should call usage if they need to... */
484 if ( !strcasecmp_m( opname
, "addsource" ) ) {
485 rc
= DoAddSourceCommand( argc
, argv
, opt_debug
,
489 if ( !strcasecmp_m( opname
, "write" ) ) {
490 rc
= DoWriteCommand( argc
, argv
, opt_debug
, exename
);
493 if ( !strcasecmp_m( opname
, "dump" ) ) {
494 rc
= DoDumpCommand( argc
, argv
, opt_debug
, exename
);
497 printf( "unknown command [%s]\n", opname
);