Add smbta-util to manage the encryption key.
[Samba/nascimento.git] / source3 / rpcclient / cmd_eventlog.c
blob3acaf0ca83aa6aa8bccf96e11b0983756dcc9b73
1 /*
2 Unix SMB/CIFS implementation.
3 RPC pipe client
5 Copyright (C) Günther Deschner 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "rpcclient.h"
23 #include "../librpc/gen_ndr/cli_eventlog.h"
25 static NTSTATUS get_eventlog_handle(struct rpc_pipe_client *cli,
26 TALLOC_CTX *mem_ctx,
27 const char *log,
28 struct policy_handle *handle)
30 NTSTATUS status;
31 struct eventlog_OpenUnknown0 unknown0;
32 struct lsa_String logname, servername;
34 unknown0.unknown0 = 0x005c;
35 unknown0.unknown1 = 0x0001;
37 init_lsa_String(&logname, log);
38 init_lsa_String(&servername, NULL);
40 status = rpccli_eventlog_OpenEventLogW(cli, mem_ctx,
41 &unknown0,
42 &logname,
43 &servername,
44 0x00000001, /* major */
45 0x00000001, /* minor */
46 handle);
47 if (!NT_STATUS_IS_OK(status)) {
48 return status;
51 return NT_STATUS_OK;
54 static NTSTATUS cmd_eventlog_readlog(struct rpc_pipe_client *cli,
55 TALLOC_CTX *mem_ctx,
56 int argc,
57 const char **argv)
59 NTSTATUS status = NT_STATUS_OK;
60 struct policy_handle handle;
62 uint32_t flags = EVENTLOG_BACKWARDS_READ |
63 EVENTLOG_SEQUENTIAL_READ;
64 uint32_t offset = 0;
65 uint32_t number_of_bytes = 0;
66 uint8_t *data = NULL;
67 uint32_t sent_size = 0;
68 uint32_t real_size = 0;
70 if (argc < 2 || argc > 4) {
71 printf("Usage: %s logname [offset] [number_of_bytes]\n", argv[0]);
72 return NT_STATUS_OK;
75 if (argc >= 3) {
76 offset = atoi(argv[2]);
79 if (argc >= 4) {
80 number_of_bytes = atoi(argv[3]);
81 data = talloc_array(mem_ctx, uint8_t, number_of_bytes);
82 if (!data) {
83 goto done;
87 status = get_eventlog_handle(cli, mem_ctx, argv[1], &handle);
88 if (!NT_STATUS_IS_OK(status)) {
89 return status;
92 do {
94 enum ndr_err_code ndr_err;
95 DATA_BLOB blob;
96 struct EVENTLOGRECORD r;
97 uint32_t size = 0;
98 uint32_t pos = 0;
100 status = rpccli_eventlog_ReadEventLogW(cli, mem_ctx,
101 &handle,
102 flags,
103 offset,
104 number_of_bytes,
105 data,
106 &sent_size,
107 &real_size);
108 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL) &&
109 real_size > 0 ) {
110 number_of_bytes = real_size;
111 data = talloc_array(mem_ctx, uint8_t, real_size);
112 if (!data) {
113 goto done;
115 status = rpccli_eventlog_ReadEventLogW(cli, mem_ctx,
116 &handle,
117 flags,
118 offset,
119 number_of_bytes,
120 data,
121 &sent_size,
122 &real_size);
125 if (!NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE) &&
126 !NT_STATUS_IS_OK(status)) {
127 goto done;
130 number_of_bytes = 0;
132 size = IVAL(data, pos);
134 while (size > 0) {
136 blob = data_blob_const(data + pos, size);
137 /* dump_data(0, blob.data, blob.length); */
138 ndr_err = ndr_pull_struct_blob_all(&blob, mem_ctx, NULL, &r,
139 (ndr_pull_flags_fn_t)ndr_pull_EVENTLOGRECORD);
140 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
141 status = ndr_map_error2ntstatus(ndr_err);
142 goto done;
145 NDR_PRINT_DEBUG(EVENTLOGRECORD, &r);
147 pos += size;
149 if (pos + 4 > sent_size) {
150 break;
153 size = IVAL(data, pos);
156 offset++;
158 } while (NT_STATUS_IS_OK(status));
160 done:
161 rpccli_eventlog_CloseEventLog(cli, mem_ctx, &handle);
163 return status;
166 static NTSTATUS cmd_eventlog_numrecords(struct rpc_pipe_client *cli,
167 TALLOC_CTX *mem_ctx,
168 int argc,
169 const char **argv)
171 NTSTATUS status;
172 struct policy_handle handle;
173 uint32_t number = 0;
175 if (argc != 2) {
176 printf("Usage: %s logname\n", argv[0]);
177 return NT_STATUS_OK;
180 status = get_eventlog_handle(cli, mem_ctx, argv[1], &handle);
181 if (!NT_STATUS_IS_OK(status)) {
182 return status;
185 status = rpccli_eventlog_GetNumRecords(cli, mem_ctx,
186 &handle,
187 &number);
188 if (!NT_STATUS_IS_OK(status)) {
189 goto done;
192 printf("number of records: %d\n", number);
194 done:
195 rpccli_eventlog_CloseEventLog(cli, mem_ctx, &handle);
197 return status;
200 static NTSTATUS cmd_eventlog_oldestrecord(struct rpc_pipe_client *cli,
201 TALLOC_CTX *mem_ctx,
202 int argc,
203 const char **argv)
205 NTSTATUS status;
206 struct policy_handle handle;
207 uint32_t oldest_entry = 0;
209 if (argc != 2) {
210 printf("Usage: %s logname\n", argv[0]);
211 return NT_STATUS_OK;
214 status = get_eventlog_handle(cli, mem_ctx, argv[1], &handle);
215 if (!NT_STATUS_IS_OK(status)) {
216 return status;
219 status = rpccli_eventlog_GetOldestRecord(cli, mem_ctx,
220 &handle,
221 &oldest_entry);
222 if (!NT_STATUS_IS_OK(status)) {
223 goto done;
226 printf("oldest entry: %d\n", oldest_entry);
228 done:
229 rpccli_eventlog_CloseEventLog(cli, mem_ctx, &handle);
231 return status;
234 static NTSTATUS cmd_eventlog_reportevent(struct rpc_pipe_client *cli,
235 TALLOC_CTX *mem_ctx,
236 int argc,
237 const char **argv)
239 NTSTATUS status;
240 struct policy_handle handle;
242 uint16_t num_of_strings = 1;
243 uint32_t data_size = 0;
244 struct lsa_String servername;
245 struct lsa_String *strings;
246 uint8_t *data = NULL;
247 uint32_t record_number = 0;
248 time_t time_written = 0;
250 if (argc != 2) {
251 printf("Usage: %s logname\n", argv[0]);
252 return NT_STATUS_OK;
255 status = get_eventlog_handle(cli, mem_ctx, argv[1], &handle);
256 if (!NT_STATUS_IS_OK(status)) {
257 return status;
260 strings = talloc_array(mem_ctx, struct lsa_String, num_of_strings);
261 if (!strings) {
262 return NT_STATUS_NO_MEMORY;
265 init_lsa_String(&strings[0], "test event written by rpcclient\n");
266 init_lsa_String(&servername, NULL);
268 status = rpccli_eventlog_ReportEventW(cli, mem_ctx,
269 &handle,
270 time(NULL),
271 EVENTLOG_INFORMATION_TYPE,
272 0, /* event_category */
273 0, /* event_id */
274 num_of_strings,
275 data_size,
276 &servername,
277 NULL, /* user_sid */
278 &strings,
279 data,
280 0, /* flags */
281 &record_number,
282 &time_written);
284 if (!NT_STATUS_IS_OK(status)) {
285 goto done;
288 printf("entry: %d written at %s\n", record_number,
289 http_timestring(talloc_tos(), time_written));
291 done:
292 rpccli_eventlog_CloseEventLog(cli, mem_ctx, &handle);
294 return status;
297 static NTSTATUS cmd_eventlog_reporteventsource(struct rpc_pipe_client *cli,
298 TALLOC_CTX *mem_ctx,
299 int argc,
300 const char **argv)
302 NTSTATUS status;
303 struct policy_handle handle;
305 uint16_t num_of_strings = 1;
306 uint32_t data_size = 0;
307 struct lsa_String servername, sourcename;
308 struct lsa_String *strings;
309 uint8_t *data = NULL;
310 uint32_t record_number = 0;
311 time_t time_written = 0;
313 if (argc != 2) {
314 printf("Usage: %s logname\n", argv[0]);
315 return NT_STATUS_OK;
318 status = get_eventlog_handle(cli, mem_ctx, argv[1], &handle);
319 if (!NT_STATUS_IS_OK(status)) {
320 return status;
323 strings = talloc_array(mem_ctx, struct lsa_String, num_of_strings);
324 if (!strings) {
325 return NT_STATUS_NO_MEMORY;
328 init_lsa_String(&strings[0], "test event written by rpcclient\n");
329 init_lsa_String(&servername, NULL);
330 init_lsa_String(&sourcename, "rpcclient");
332 status = rpccli_eventlog_ReportEventAndSourceW(cli, mem_ctx,
333 &handle,
334 time(NULL),
335 EVENTLOG_INFORMATION_TYPE,
336 0, /* event_category */
337 0, /* event_id */
338 &sourcename,
339 num_of_strings,
340 data_size,
341 &servername,
342 NULL, /* user_sid */
343 &strings,
344 data,
345 0, /* flags */
346 &record_number,
347 &time_written);
348 if (!NT_STATUS_IS_OK(status)) {
349 goto done;
352 printf("entry: %d written at %s\n", record_number,
353 http_timestring(talloc_tos(), time_written));
355 done:
356 rpccli_eventlog_CloseEventLog(cli, mem_ctx, &handle);
358 return status;
361 static NTSTATUS cmd_eventlog_registerevsource(struct rpc_pipe_client *cli,
362 TALLOC_CTX *mem_ctx,
363 int argc,
364 const char **argv)
366 NTSTATUS status;
367 struct policy_handle log_handle;
368 struct lsa_String module_name, reg_module_name;
369 struct eventlog_OpenUnknown0 unknown0;
371 unknown0.unknown0 = 0x005c;
372 unknown0.unknown1 = 0x0001;
374 if (argc != 2) {
375 printf("Usage: %s logname\n", argv[0]);
376 return NT_STATUS_OK;
379 init_lsa_String(&module_name, "rpcclient");
380 init_lsa_String(&reg_module_name, NULL);
382 status = rpccli_eventlog_RegisterEventSourceW(cli, mem_ctx,
383 &unknown0,
384 &module_name,
385 &reg_module_name,
386 1, /* major_version */
387 1, /* minor_version */
388 &log_handle);
389 if (!NT_STATUS_IS_OK(status)) {
390 goto done;
393 done:
394 rpccli_eventlog_DeregisterEventSource(cli, mem_ctx, &log_handle);
396 return status;
399 static NTSTATUS cmd_eventlog_backuplog(struct rpc_pipe_client *cli,
400 TALLOC_CTX *mem_ctx,
401 int argc,
402 const char **argv)
404 NTSTATUS status;
405 struct policy_handle handle;
406 struct lsa_String backup_filename;
407 const char *tmp;
409 if (argc != 3) {
410 printf("Usage: %s logname backupname\n", argv[0]);
411 return NT_STATUS_OK;
414 status = get_eventlog_handle(cli, mem_ctx, argv[1], &handle);
415 if (!NT_STATUS_IS_OK(status)) {
416 return status;
419 tmp = talloc_asprintf(mem_ctx, "\\??\\%s", argv[2]);
420 if (!tmp) {
421 status = NT_STATUS_NO_MEMORY;
422 goto done;
425 init_lsa_String(&backup_filename, tmp);
427 status = rpccli_eventlog_BackupEventLogW(cli, mem_ctx,
428 &handle,
429 &backup_filename);
431 done:
432 rpccli_eventlog_CloseEventLog(cli, mem_ctx, &handle);
434 return status;
437 static NTSTATUS cmd_eventlog_loginfo(struct rpc_pipe_client *cli,
438 TALLOC_CTX *mem_ctx,
439 int argc,
440 const char **argv)
442 NTSTATUS status;
443 struct policy_handle handle;
444 uint8_t *buffer = NULL;
445 uint32_t buf_size = 0;
446 uint32_t bytes_needed = 0;
448 if (argc != 2) {
449 printf("Usage: %s logname\n", argv[0]);
450 return NT_STATUS_OK;
453 status = get_eventlog_handle(cli, mem_ctx, argv[1], &handle);
454 if (!NT_STATUS_IS_OK(status)) {
455 return status;
458 status = rpccli_eventlog_GetLogInformation(cli, mem_ctx,
459 &handle,
460 0, /* level */
461 buffer,
462 buf_size,
463 &bytes_needed);
464 if (!NT_STATUS_IS_OK(status) &&
465 !NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
466 goto done;
469 buf_size = bytes_needed;
470 buffer = talloc_array(mem_ctx, uint8_t, bytes_needed);
471 if (!buffer) {
472 status = NT_STATUS_NO_MEMORY;
473 goto done;
476 status = rpccli_eventlog_GetLogInformation(cli, mem_ctx,
477 &handle,
478 0, /* level */
479 buffer,
480 buf_size,
481 &bytes_needed);
482 if (!NT_STATUS_IS_OK(status)) {
483 goto done;
486 done:
487 rpccli_eventlog_CloseEventLog(cli, mem_ctx, &handle);
489 return status;
493 struct cmd_set eventlog_commands[] = {
494 { "EVENTLOG" },
495 { "eventlog_readlog", RPC_RTYPE_NTSTATUS, cmd_eventlog_readlog, NULL, &ndr_table_eventlog.syntax_id, NULL, "Read Eventlog", "" },
496 { "eventlog_numrecord", RPC_RTYPE_NTSTATUS, cmd_eventlog_numrecords, NULL, &ndr_table_eventlog.syntax_id, NULL, "Get number of records", "" },
497 { "eventlog_oldestrecord", RPC_RTYPE_NTSTATUS, cmd_eventlog_oldestrecord, NULL, &ndr_table_eventlog.syntax_id, NULL, "Get oldest record", "" },
498 { "eventlog_reportevent", RPC_RTYPE_NTSTATUS, cmd_eventlog_reportevent, NULL, &ndr_table_eventlog.syntax_id, NULL, "Report event", "" },
499 { "eventlog_reporteventsource", RPC_RTYPE_NTSTATUS, cmd_eventlog_reporteventsource, NULL, &ndr_table_eventlog.syntax_id, NULL, "Report event and source", "" },
500 { "eventlog_registerevsource", RPC_RTYPE_NTSTATUS, cmd_eventlog_registerevsource, NULL, &ndr_table_eventlog.syntax_id, NULL, "Register event source", "" },
501 { "eventlog_backuplog", RPC_RTYPE_NTSTATUS, cmd_eventlog_backuplog, NULL, &ndr_table_eventlog.syntax_id, NULL, "Backup Eventlog File", "" },
502 { "eventlog_loginfo", RPC_RTYPE_NTSTATUS, cmd_eventlog_loginfo, NULL, &ndr_table_eventlog.syntax_id, NULL, "Get Eventlog Information", "" },
503 { NULL }