s3:util:net_registry: registry_value_cmp() uses NUMERIC_CMP()
[samba.git] / source3 / utils / eventlogadm.c
1
2 /*
3  * Samba Unix/Linux SMB client utility 
4  * Write Eventlog records to a tdb, perform other eventlog related functions
5  *
6  *
7  * Copyright (C) Brian Moran                2005.
8  * Copyright (C) Guenther Deschner          2009.
9  *
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.
14  *
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.
19  *
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/>.
22  */
23
24
25 #include "includes.h"
26 #include "lib/eventlog/eventlog.h"
27 #include "registry.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"
33 #include "cmdline_contexts.h"
34 #include "lib/util/string_wrappers.h"
35
36 extern int optind;
37 extern char *optarg;
38
39 int opt_debug = 0;
40
41 static void usage( char *s )
42 {
43         printf( "\nUsage: %s [OPTION]\n\n", s );
44         printf( " -o write <Eventlog Name> \t\t\t\t\tWrites records to eventlog from STDIN\n" );
45         printf( " -o addsource <EventlogName> <sourcename> <msgfileDLLname> \tAdds the specified source & DLL eventlog registry entry\n" );
46         printf( " -o dump <Eventlog Name> <starting_record>\t\t\t\t\tDump stored eventlog entries on STDOUT\n" );
47         printf( "\nMiscellaneous options:\n" );
48         printf( " -s <filename>\t\t\t\t\t\t\tUse configuration file <filename>.\n");
49         printf( " -d\t\t\t\t\t\t\t\tturn debug on\n" );
50         printf( " -h\t\t\t\t\t\t\t\tdisplay help\n\n" );
51 }
52
53 static void display_eventlog_names( void )
54 {
55         const char **elogs;
56         int i;
57
58         elogs = lp_eventlog_list(  );
59         printf( "Active eventlog names:\n" );
60         printf( "--------------------------------------\n" );
61         if ( elogs ) {
62                 for ( i = 0; elogs[i]; i++ ) {
63                         printf( "\t%s\n", elogs[i] );
64                 }
65         } 
66         else
67                 printf( "\t<None specified>\n");
68 }
69
70 /*********************************************************************
71  for an eventlog, add in a source name. If the eventlog doesn't
72  exist (not in the list) do nothing.   If a source for the log
73  already exists, change the information (remove, replace)
74 *********************************************************************/
75 static bool eventlog_add_source( const char *eventlog, const char *sourcename,
76                                  const char *messagefile )
77 {
78         /* Find all of the eventlogs, add keys for each of them */
79         /* need to add to the value KEY_EVENTLOG/<eventlog>/Sources string (Creating if necessary)
80            need to add KEY of source to KEY_EVENTLOG/<eventlog>/<source> */
81
82         const char **elogs = lp_eventlog_list(  );
83         const char **wrklist, **wp;
84         char *evtlogpath = NULL;
85         int ii = 0;
86         bool already_in;
87         int i;
88         int numsources = 0;
89         TALLOC_CTX *ctx = talloc_stackframe();
90         WERROR werr;
91         struct registry_key *key_hive, *key_eventlog, *key_source;
92         struct security_token *token = NULL;
93         const char *hive_name, *relpath;
94         enum winreg_CreateAction action;
95         struct registry_value *value;
96         static const uint32_t ACCESS = REG_KEY_READ | REG_KEY_WRITE;
97         bool ret = false;
98
99         if (!elogs) {
100                 d_printf("No Eventlogs configured\n");
101                 goto done;
102         }
103
104         for ( i = 0; elogs[i]; i++ ) {
105                 if ( strequal( elogs[i], eventlog ) )
106                         break;
107         }
108
109         if ( !elogs[i] ) {
110                 d_printf("Eventlog [%s] not found in list of valid event logs\n",
111                          eventlog);
112                 goto done;
113         }
114
115         /* have to assume that the evenlog key itself exists at this point */
116         /* add in a key of [sourcename] under the eventlog key */
117
118         /* todo add to Sources */
119
120         evtlogpath = talloc_asprintf(ctx, "%s\\%s", KEY_EVENTLOG, eventlog);
121         if (!evtlogpath) {
122                 d_printf("Out of memory\n");
123                 goto done;
124         }
125
126         relpath = evtlogpath + sizeof(KEY_EVENTLOG);
127         hive_name = talloc_strndup(ctx, evtlogpath, relpath - evtlogpath);
128         if (!hive_name) {
129                 d_printf("Out of memory\n");
130                 goto done;
131         }
132         relpath++;
133
134         werr = ntstatus_to_werror(registry_create_admin_token(ctx, &token));
135         if (!W_ERROR_IS_OK(werr)) {
136                 d_printf("Failed to create admin token: %s\n", win_errstr(werr));
137                 goto done;
138         }
139
140         werr = reg_openhive(ctx, hive_name, ACCESS, token, &key_hive);
141         if (!W_ERROR_IS_OK(werr)) {
142                 d_printf("Failed to open hive [%s]: %s\n", hive_name, win_errstr(werr));
143                 goto done;
144         }
145
146         werr = reg_openkey(ctx, key_hive, relpath, ACCESS, &key_eventlog);
147         if (!W_ERROR_IS_OK(werr)) {
148                 d_printf("Failed to open key [%s]: %s\n", evtlogpath, win_errstr(werr));
149                 goto done;
150         }
151
152         werr = reg_queryvalue(ctx, key_eventlog, "Sources", &value);
153         if (!W_ERROR_IS_OK(werr)) {
154                 d_printf("Failed to get value \"Sources\" for [%s]: %s\n", evtlogpath, win_errstr(werr));
155                 goto done;
156         }
157         /* perhaps this adding a new string to a multi_sz should be a fn? */
158         /* check to see if it's there already */
159
160         if ( value->type != REG_MULTI_SZ ) {
161                 d_printf("Wrong type for \"Sources\", should be REG_MULTI_SZ\n");
162                 goto done;
163         }
164         /* convert to a 'regulah' chars to do some comparisons */
165
166         already_in = false;
167         wrklist = NULL;
168         dump_data(1, value->data.data, value->data.length);
169
170         if (!pull_reg_multi_sz(ctx, &value->data, &wrklist)) {
171                 d_printf("Failed to pull REG_MULTI_SZ from \"Sources\"\n");
172                 goto done;
173         }
174
175         for (ii=0; wrklist[ii]; ii++) {
176                 numsources++;
177         }
178
179         if (numsources > 0) {
180                 /* see if it's in there already */
181                 wp = wrklist;
182
183                 while (wp && *wp ) {
184                         if ( strequal( *wp, sourcename ) ) {
185                                 d_printf("Source name [%s] already in list for [%s] \n",
186                                          sourcename, eventlog);
187                                 already_in = true;
188                                 break;
189                         }
190                         wp++;
191                 }
192         } else {
193                 d_printf("Nothing in the sources list, this might be a problem\n");
194         }
195
196         if ( !already_in ) {
197                 /* make a new list with an additional entry; copy values, add another */
198                 wp = talloc_realloc(ctx, wrklist, const char *, numsources + 2 );
199                 if ( !wp ) {
200                         d_printf("Out of memory\n");
201                         goto done;
202                 }
203
204                 wp[numsources] = sourcename;
205                 wp[numsources+1] = NULL;
206                 if (!push_reg_multi_sz(ctx, &value->data, wp)) {
207                         d_printf("Failed to push Sources\n");
208                         goto done;
209                 }
210                 dump_data( 1, value->data.data, value->data.length);
211                 werr = reg_setvalue(key_eventlog,  "Sources", value);
212                 if (!W_ERROR_IS_OK(werr)) {
213                         d_printf("Failed to set value Sources:  %s\n", win_errstr(werr));
214                         goto done;
215                 }
216         } else {
217                 d_printf("Source name [%s] found in existing list of sources\n",
218                          sourcename);
219         }
220
221         werr = reg_createkey(ctx, key_eventlog, sourcename, ACCESS, &key_source, &action);
222         if (!W_ERROR_IS_OK(werr)) {
223                 d_printf("Failed to create subkey \"%s\" of \"%s\": %s\n", sourcename, evtlogpath, win_errstr(werr));
224                 goto done;
225         }
226
227         if (action == REG_CREATED_NEW_KEY) {
228                 d_printf(" Source name [%s] for eventlog [%s] didn't exist, adding \n",
229                          sourcename, eventlog);
230         }
231
232         /* at this point KEY_EVENTLOG/<eventlog>/<sourcename> key is in there. Now need to add EventMessageFile */
233
234         /* now add the values to the KEY_EVENTLOG/Application form key */
235         d_printf("Storing EventMessageFile [%s] to eventlog path of [%s]\n",
236                  messagefile, evtlogpath);
237
238         if (!push_reg_sz(ctx, &value->data, messagefile)) {
239                 d_printf("Failed to push \"EventMessageFile\"\n");
240                 goto done;
241         }
242         value->type = REG_SZ;
243
244         werr = reg_setvalue(key_source, "EventMessageFile", value);
245         if (!W_ERROR_IS_OK(werr)) {
246                 d_printf("Failed to set value \"EventMessageFile\":  %s\n", win_errstr(werr));
247                 return false;
248         }
249         ret = true;
250 done:
251         talloc_free(ctx);
252         return ret;
253 }
254
255 static int DoAddSourceCommand( int argc, char **argv, bool debugflag, char *exename )
256 {
257         WERROR werr;
258
259         if ( argc < 3 ) {
260                 printf( "need more arguments:\n" );
261                 printf( "-o addsource EventlogName SourceName /path/to/EventMessageFile.dll\n" );
262                 return -1;
263         }
264
265         /* must open the registry before we access it */
266         werr = registry_init_common();
267         if (!W_ERROR_IS_OK(werr)) {
268                 printf("Can't open the registry: %s.\n", win_errstr(werr));
269                 return -1;
270         }
271         werr = regdb_transaction_start();
272         if (!W_ERROR_IS_OK(werr)) {
273                 printf("Can't start transaction on registry: %s.\n", win_errstr(werr));
274                 return -1;
275         }
276
277         if ( !eventlog_add_source( argv[0], argv[1], argv[2] ) ) {
278                 regdb_transaction_cancel();
279                 return -2;
280         }
281         werr = regdb_transaction_commit();
282         if (!W_ERROR_IS_OK(werr)) {
283                 printf("Failed to commit transaction on registry: %s.\n", win_errstr(werr));
284                 return -1;
285         }
286         return 0;
287 }
288
289 static int DoWriteCommand( int argc, char **argv, bool debugflag, char *exename )
290 {
291         FILE *f1;
292         char *argfname;
293         ELOG_TDB *etdb;
294         NTSTATUS status;
295
296         /* fixed constants are bad bad bad  */
297         char linein[1024];
298         bool is_eor;
299         struct eventlog_Record_tdb ee;
300         uint32_t record_number = 0;
301         TALLOC_CTX *mem_ctx = talloc_tos();
302
303         f1 = stdin;
304         if ( !f1 ) {
305                 printf( "Can't open STDIN\n" );
306                 return -1;
307         }
308
309         if ( debugflag ) {
310                 printf( "Starting write for eventlog [%s]\n", argv[0] );
311                 display_eventlog_names(  );
312         }
313
314         argfname = argv[0];
315
316         if ( !( etdb = elog_open_tdb( argfname, False, False ) ) ) {
317                 printf( "can't open the eventlog TDB (%s)\n", argfname );
318                 return -1;
319         }
320
321         ZERO_STRUCT( ee );      /* MUST initialize between records */
322
323         while ( !feof( f1 ) ) {
324                 if (fgets( linein, sizeof( linein ) - 1, f1 ) == NULL) {
325                         break;
326                 }
327                 if ((strlen(linein) > 0)
328                     && (linein[strlen(linein)-1] == '\n')) {
329                         linein[strlen(linein)-1] = 0;
330                 }
331
332                 if ( debugflag )
333                         printf( "Read line [%s]\n", linein );
334
335                 is_eor = False;
336
337
338                 parse_logentry( mem_ctx, ( char * ) &linein, &ee, &is_eor );
339                 /* should we do something with the return code? */
340
341                 if ( is_eor ) {
342                         fixup_eventlog_record_tdb( &ee );
343
344                         if ( opt_debug )
345                                 printf( "record number [%d], tg [%d] , tw [%d]\n",
346                                         ee.record_number, (int)ee.time_generated, (int)ee.time_written );
347
348                         if ( ee.time_generated != 0 ) {
349
350                                 /* printf("Writing to the event log\n"); */
351
352                                 status = evlog_push_record_tdb( mem_ctx, ELOG_TDB_CTX(etdb),
353                                                                 &ee, &record_number );
354                                 if ( !NT_STATUS_IS_OK(status) ) {
355                                         printf( "Can't write to the event log: %s\n",
356                                                 nt_errstr(status) );
357                                 } else {
358                                         if ( opt_debug )
359                                                 printf( "Wrote record %d\n",
360                                                         record_number );
361                                 }
362                         } else {
363                                 if ( opt_debug )
364                                         printf( "<null record>\n" );
365                         }
366                         ZERO_STRUCT( ee );      /* MUST initialize between records */
367                 }
368         }
369
370         elog_close_tdb( etdb , False );
371
372         return 0;
373 }
374
375 static int DoDumpCommand(int argc, char **argv, bool debugflag, char *exename)
376 {
377         ELOG_TDB *etdb;
378         TALLOC_CTX *mem_ctx = talloc_tos();
379         uint32_t count = 1;
380
381         if (argc > 2) {
382                 return -1;
383         }
384
385         if (argc > 1) {
386                 count = atoi(argv[1]);
387         }
388
389         etdb = elog_open_tdb(argv[0], false, true);
390         if (!etdb) {
391                 printf("can't open the eventlog TDB (%s)\n", argv[0]);
392                 return -1;
393         }
394
395         while (1) {
396
397                 struct eventlog_Record_tdb *r;
398                 char *s;
399
400                 r = evlog_pull_record_tdb(mem_ctx, etdb->tdb, count);
401                 if (!r) {
402                         break;
403                 }
404
405                 printf("displaying record: %d\n", count);
406
407                 s = NDR_PRINT_STRUCT_STRING(mem_ctx, eventlog_Record_tdb, r);
408                 if (s) {
409                         printf("%s\n", s);
410                         talloc_free(s);
411                 }
412                 count++;
413         }
414
415         elog_close_tdb(etdb, false);
416
417         return 0;
418 }
419
420 /* would be nice to use the popT stuff here, however doing so forces us to drag in a lot of other infrastructure */
421
422 int main( int argc, char *argv[] )
423 {
424         int opt, rc;
425         char *exename;
426         char *configfile = NULL;
427         TALLOC_CTX *frame = talloc_stackframe();
428
429
430         fstring opname;
431
432         smb_init_locale();
433
434         opt_debug = 0;          /* todo set this from getopts */
435
436         exename = argv[0];
437
438         /* default */
439
440         fstrcpy( opname, "write" );     /* the default */
441
442 #if 0                           /* TESTING CODE */
443         eventlog_add_source( "System", "TestSourceX", "SomeTestPathX" );
444 #endif
445         while ( ( opt = getopt( argc, argv, "dho:s:" ) ) != EOF ) {
446                 switch ( opt ) {
447
448                 case 'o':
449                         fstrcpy( opname, optarg );
450                         break;
451
452                 case 'h':
453                         usage( exename );
454                         display_eventlog_names(  );
455                         exit( 0 );
456                         break;
457
458                 case 'd':
459                         opt_debug = 1;
460                         break;
461                 case 's':
462                         configfile = talloc_strdup(frame, optarg);
463                         break;
464
465                 }
466         }
467
468         argc -= optind;
469         argv += optind;
470
471         if ( argc < 1 ) {
472                 printf( "\nNot enough arguments!\n" );
473                 usage( exename );
474                 exit( 1 );
475         }
476
477         if ( configfile == NULL ) {
478                 lp_load_global(get_dyn_CONFIGFILE());
479         } else if (!lp_load_global(configfile)) {
480                 printf("Unable to parse configfile '%s'\n",configfile);
481                 exit( 1 );
482         }
483
484         /*  note that the separate command types should call usage if they need to... */
485         while ( 1 ) {
486                 if ( !strcasecmp_m( opname, "addsource" ) ) {
487                         rc = DoAddSourceCommand( argc, argv, opt_debug,
488                                                  exename );
489                         break;
490                 }
491                 if ( !strcasecmp_m( opname, "write" ) ) {
492                         rc = DoWriteCommand( argc, argv, opt_debug, exename );
493                         break;
494                 }
495                 if ( !strcasecmp_m( opname, "dump" ) ) {
496                         rc = DoDumpCommand( argc, argv, opt_debug, exename );
497                         break;
498                 }
499                 printf( "unknown command [%s]\n", opname );
500                 usage( exename );
501                 exit( 1 );
502                 break;
503         }
504         TALLOC_FREE(frame);
505         return rc;
506 }