2 Unix SMB/CIFS implementation.
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/>.
22 #include "rpcclient.h"
24 static NTSTATUS get_eventlog_handle(struct rpc_pipe_client *cli,
27 struct policy_handle *handle)
30 struct eventlog_OpenUnknown0 unknown0;
31 struct lsa_String logname, servername;
33 unknown0.unknown0 = 0x005c;
34 unknown0.unknown1 = 0x0001;
36 init_lsa_String(&logname, log);
37 init_lsa_String(&servername, NULL);
39 status = rpccli_eventlog_OpenEventLogW(cli, mem_ctx,
43 0x00000001, /* major */
44 0x00000001, /* minor */
46 if (!NT_STATUS_IS_OK(status)) {
53 static NTSTATUS cmd_eventlog_readlog(struct rpc_pipe_client *cli,
58 NTSTATUS status = NT_STATUS_OK;
59 struct policy_handle handle;
61 uint32_t flags = EVENTLOG_BACKWARDS_READ |
62 EVENTLOG_SEQUENTIAL_READ;
64 uint32_t number_of_bytes = 0;
66 uint32_t sent_size = 0;
67 uint32_t real_size = 0;
69 if (argc < 2 || argc > 4) {
70 printf("Usage: %s logname [offset] [number_of_bytes]\n", argv[0]);
75 offset = atoi(argv[2]);
79 number_of_bytes = atoi(argv[3]);
80 data = talloc_array(mem_ctx, uint8_t, number_of_bytes);
86 status = get_eventlog_handle(cli, mem_ctx, argv[1], &handle);
87 if (!NT_STATUS_IS_OK(status)) {
93 enum ndr_err_code ndr_err;
95 struct EVENTLOGRECORD r;
99 status = rpccli_eventlog_ReadEventLogW(cli, mem_ctx,
107 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL) &&
109 number_of_bytes = real_size;
110 data = talloc_array(mem_ctx, uint8_t, real_size);
114 status = rpccli_eventlog_ReadEventLogW(cli, mem_ctx,
124 if (!NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE) &&
125 !NT_STATUS_IS_OK(status)) {
131 size = IVAL(data, pos);
135 blob = data_blob_const(data + pos, size);
136 /* dump_data(0, blob.data, blob.length); */
137 ndr_err = ndr_pull_struct_blob_all(&blob, mem_ctx, NULL, &r,
138 (ndr_pull_flags_fn_t)ndr_pull_EVENTLOGRECORD);
139 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
140 status = ndr_map_error2ntstatus(ndr_err);
144 NDR_PRINT_DEBUG(EVENTLOGRECORD, &r);
148 if (pos + 4 > sent_size) {
152 size = IVAL(data, pos);
157 } while (NT_STATUS_IS_OK(status));
160 rpccli_eventlog_CloseEventLog(cli, mem_ctx, &handle);
165 static NTSTATUS cmd_eventlog_numrecords(struct rpc_pipe_client *cli,
171 struct policy_handle handle;
175 printf("Usage: %s logname\n", argv[0]);
179 status = get_eventlog_handle(cli, mem_ctx, argv[1], &handle);
180 if (!NT_STATUS_IS_OK(status)) {
184 status = rpccli_eventlog_GetNumRecords(cli, mem_ctx,
187 if (!NT_STATUS_IS_OK(status)) {
191 printf("number of records: %d\n", number);
194 rpccli_eventlog_CloseEventLog(cli, mem_ctx, &handle);
199 static NTSTATUS cmd_eventlog_oldestrecord(struct rpc_pipe_client *cli,
205 struct policy_handle handle;
206 uint32_t oldest_entry = 0;
209 printf("Usage: %s logname\n", argv[0]);
213 status = get_eventlog_handle(cli, mem_ctx, argv[1], &handle);
214 if (!NT_STATUS_IS_OK(status)) {
218 status = rpccli_eventlog_GetOldestRecord(cli, mem_ctx,
221 if (!NT_STATUS_IS_OK(status)) {
225 printf("oldest entry: %d\n", oldest_entry);
228 rpccli_eventlog_CloseEventLog(cli, mem_ctx, &handle);
233 static NTSTATUS cmd_eventlog_reportevent(struct rpc_pipe_client *cli,
239 struct policy_handle handle;
241 uint16_t num_of_strings = 1;
242 uint32_t data_size = 0;
243 struct lsa_String servername;
244 struct lsa_String *strings;
245 uint8_t *data = NULL;
246 uint32_t record_number = 0;
247 time_t time_written = 0;
250 printf("Usage: %s logname\n", argv[0]);
254 status = get_eventlog_handle(cli, mem_ctx, argv[1], &handle);
255 if (!NT_STATUS_IS_OK(status)) {
259 strings = talloc_array(mem_ctx, struct lsa_String, num_of_strings);
261 return NT_STATUS_NO_MEMORY;
264 init_lsa_String(&strings[0], "test event written by rpcclient\n");
265 init_lsa_String(&servername, NULL);
267 status = rpccli_eventlog_ReportEventW(cli, mem_ctx,
270 EVENTLOG_INFORMATION_TYPE,
271 0, /* event_category */
283 if (!NT_STATUS_IS_OK(status)) {
287 printf("entry: %d written at %s\n", record_number,
288 http_timestring(talloc_tos(), time_written));
291 rpccli_eventlog_CloseEventLog(cli, mem_ctx, &handle);
296 static NTSTATUS cmd_eventlog_reporteventsource(struct rpc_pipe_client *cli,
302 struct policy_handle handle;
304 uint16_t num_of_strings = 1;
305 uint32_t data_size = 0;
306 struct lsa_String servername, sourcename;
307 struct lsa_String *strings;
308 uint8_t *data = NULL;
309 uint32_t record_number = 0;
310 time_t time_written = 0;
313 printf("Usage: %s logname\n", argv[0]);
317 status = get_eventlog_handle(cli, mem_ctx, argv[1], &handle);
318 if (!NT_STATUS_IS_OK(status)) {
322 strings = talloc_array(mem_ctx, struct lsa_String, num_of_strings);
324 return NT_STATUS_NO_MEMORY;
327 init_lsa_String(&strings[0], "test event written by rpcclient\n");
328 init_lsa_String(&servername, NULL);
329 init_lsa_String(&sourcename, "rpcclient");
331 status = rpccli_eventlog_ReportEventAndSourceW(cli, mem_ctx,
334 EVENTLOG_INFORMATION_TYPE,
335 0, /* event_category */
347 if (!NT_STATUS_IS_OK(status)) {
351 printf("entry: %d written at %s\n", record_number,
352 http_timestring(talloc_tos(), time_written));
355 rpccli_eventlog_CloseEventLog(cli, mem_ctx, &handle);
360 static NTSTATUS cmd_eventlog_registerevsource(struct rpc_pipe_client *cli,
366 struct policy_handle log_handle;
367 struct lsa_String module_name, reg_module_name;
368 struct eventlog_OpenUnknown0 unknown0;
370 unknown0.unknown0 = 0x005c;
371 unknown0.unknown1 = 0x0001;
374 printf("Usage: %s logname\n", argv[0]);
378 init_lsa_String(&module_name, "rpcclient");
379 init_lsa_String(®_module_name, NULL);
381 status = rpccli_eventlog_RegisterEventSourceW(cli, mem_ctx,
385 1, /* major_version */
386 1, /* minor_version */
388 if (!NT_STATUS_IS_OK(status)) {
393 rpccli_eventlog_DeregisterEventSource(cli, mem_ctx, &log_handle);
398 static NTSTATUS cmd_eventlog_backuplog(struct rpc_pipe_client *cli,
404 struct policy_handle handle;
405 struct lsa_String backup_filename;
409 printf("Usage: %s logname backupname\n", argv[0]);
413 status = get_eventlog_handle(cli, mem_ctx, argv[1], &handle);
414 if (!NT_STATUS_IS_OK(status)) {
418 tmp = talloc_asprintf(mem_ctx, "\\??\\%s", argv[2]);
420 status = NT_STATUS_NO_MEMORY;
424 init_lsa_String(&backup_filename, tmp);
426 status = rpccli_eventlog_BackupEventLogW(cli, mem_ctx,
431 rpccli_eventlog_CloseEventLog(cli, mem_ctx, &handle);
436 static NTSTATUS cmd_eventlog_loginfo(struct rpc_pipe_client *cli,
442 struct policy_handle handle;
443 uint8_t *buffer = NULL;
444 uint32_t buf_size = 0;
445 uint32_t bytes_needed = 0;
448 printf("Usage: %s logname\n", argv[0]);
452 status = get_eventlog_handle(cli, mem_ctx, argv[1], &handle);
453 if (!NT_STATUS_IS_OK(status)) {
457 status = rpccli_eventlog_GetLogIntormation(cli, mem_ctx,
463 if (!NT_STATUS_IS_OK(status) &&
464 !NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
468 buf_size = bytes_needed;
469 buffer = talloc_array(mem_ctx, uint8_t, bytes_needed);
471 status = NT_STATUS_NO_MEMORY;
475 status = rpccli_eventlog_GetLogIntormation(cli, mem_ctx,
481 if (!NT_STATUS_IS_OK(status)) {
486 rpccli_eventlog_CloseEventLog(cli, mem_ctx, &handle);
492 struct cmd_set eventlog_commands[] = {
494 { "eventlog_readlog", RPC_RTYPE_NTSTATUS, cmd_eventlog_readlog, NULL, &ndr_table_eventlog.syntax_id, NULL, "Read Eventlog", "" },
495 { "eventlog_numrecord", RPC_RTYPE_NTSTATUS, cmd_eventlog_numrecords, NULL, &ndr_table_eventlog.syntax_id, NULL, "Get number of records", "" },
496 { "eventlog_oldestrecord", RPC_RTYPE_NTSTATUS, cmd_eventlog_oldestrecord, NULL, &ndr_table_eventlog.syntax_id, NULL, "Get oldest record", "" },
497 { "eventlog_reportevent", RPC_RTYPE_NTSTATUS, cmd_eventlog_reportevent, NULL, &ndr_table_eventlog.syntax_id, NULL, "Report event", "" },
498 { "eventlog_reporteventsource", RPC_RTYPE_NTSTATUS, cmd_eventlog_reporteventsource, NULL, &ndr_table_eventlog.syntax_id, NULL, "Report event and source", "" },
499 { "eventlog_registerevsource", RPC_RTYPE_NTSTATUS, cmd_eventlog_registerevsource, NULL, &ndr_table_eventlog.syntax_id, NULL, "Register event source", "" },
500 { "eventlog_backuplog", RPC_RTYPE_NTSTATUS, cmd_eventlog_backuplog, NULL, &ndr_table_eventlog.syntax_id, NULL, "Backup Eventlog File", "" },
501 { "eventlog_loginfo", RPC_RTYPE_NTSTATUS, cmd_eventlog_loginfo, NULL, &ndr_table_eventlog.syntax_id, NULL, "Get Eventlog Information", "" },