2 * Samba Unix/Linux SMB client library
3 * Distributed SMB/CIFS Server Management Utility
4 * Local win32 eventlog interface
6 * Copyright (C) Guenther Deschner 2009
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "utils/net.h"
26 * Dump an *evt win32 eventlog file
28 * @param argc Standard main() style argc.
29 * @param argv Standard main() style argv. Initial components are already
32 * @return A shell status integer (0 for success).
35 static int net_eventlog_dump(struct net_context
*c
, int argc
,
39 TALLOC_CTX
*ctx
= talloc_stackframe();
40 enum ndr_err_code ndr_err
;
42 struct EVENTLOG_EVT_FILE evt
;
45 if (argc
< 1 || c
->display_usage
) {
46 d_fprintf(stderr
, "usage: net eventlog dump <file.evt>\n");
50 blob
.data
= (uint8_t *)file_load(argv
[0], &blob
.length
, 0, ctx
);
52 d_fprintf(stderr
, "failed to load evt file: %s\n", argv
[0]);
56 ndr_err
= ndr_pull_struct_blob(&blob
, ctx
, NULL
, &evt
,
57 (ndr_pull_flags_fn_t
)ndr_pull_EVENTLOG_EVT_FILE
);
58 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
59 d_fprintf(stderr
, "evt pull failed: %s\n", ndr_errstr(ndr_err
));
63 s
= NDR_PRINT_STRUCT_STRING(ctx
, EVENTLOG_EVT_FILE
, &evt
);
75 * Import an *evt win32 eventlog file to internal tdb representation
77 * @param argc Standard main() style argc.
78 * @param argv Standard main() style argv. Initial components are already
81 * @return A shell status integer (0 for success).
84 static int net_eventlog_import(struct net_context
*c
, int argc
,
88 TALLOC_CTX
*ctx
= talloc_stackframe();
90 enum ndr_err_code ndr_err
;
92 uint32_t num_records
= 0;
94 ELOG_TDB
*etdb
= NULL
;
96 struct EVENTLOGHEADER evt_header
;
97 struct EVENTLOG_EVT_FILE evt
;
99 if (argc
< 2 || c
->display_usage
) {
100 d_fprintf(stderr
, "usage: net eventlog import <file> <eventlog>\n");
104 blob
.data
= (uint8_t *)file_load(argv
[0], &blob
.length
, 0, ctx
);
106 d_fprintf(stderr
, "failed to load evt file: %s\n", argv
[0]);
110 /* dump_data(0, blob.data, blob.length); */
111 ndr_err
= ndr_pull_struct_blob(&blob
, ctx
, NULL
, &evt_header
,
112 (ndr_pull_flags_fn_t
)ndr_pull_EVENTLOGHEADER
);
113 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
114 d_fprintf(stderr
, "evt header pull failed: %s\n", ndr_errstr(ndr_err
));
118 if (evt_header
.Flags
& ELF_LOGFILE_HEADER_WRAP
) {
119 d_fprintf(stderr
, "input file is wrapped, cannot proceed\n");
123 ndr_err
= ndr_pull_struct_blob(&blob
, ctx
, NULL
, &evt
,
124 (ndr_pull_flags_fn_t
)ndr_pull_EVENTLOG_EVT_FILE
);
125 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
126 d_fprintf(stderr
, "evt pull failed: %s\n", ndr_errstr(ndr_err
));
130 /* NDR_PRINT_DEBUG(EVENTLOG_EVT_FILE, &evt); */
132 etdb
= elog_open_tdb(argv
[1], false, false);
134 d_fprintf(stderr
, "can't open the eventlog TDB (%s)\n", argv
[1]);
138 num_records
= evt
.hdr
.CurrentRecordNumber
- evt
.hdr
.OldestRecordNumber
;
140 for (i
=0; i
<num_records
; i
++) {
141 uint32_t record_number
;
142 struct eventlog_Record_tdb e
;
144 status
= evlog_evt_entry_to_tdb_entry(ctx
, &evt
.records
[i
], &e
);
145 if (!NT_STATUS_IS_OK(status
)) {
149 status
= evlog_push_record_tdb(ctx
, ELOG_TDB_CTX(etdb
),
151 if (!NT_STATUS_IS_OK(status
)) {
152 d_fprintf(stderr
, "can't write to the eventlog: %s\n",
158 printf("wrote %d entries to tdb\n", i
);
163 elog_close_tdb(etdb
, false);
170 * Export internal eventlog tdb representation to an *evt win32 eventlog file
172 * @param argc Standard main() style argc.
173 * @param argv Standard main() style argv. Initial components are already
176 * @return A shell status integer (0 for success).
179 static int net_eventlog_export(struct net_context
*c
, int argc
,
184 TALLOC_CTX
*ctx
= talloc_stackframe();
185 enum ndr_err_code ndr_err
;
187 uint32_t num_records
= 0;
188 struct EVENTLOG_EVT_FILE evt
;
189 ELOG_TDB
*etdb
= NULL
;
191 size_t endoffset
= 0;
193 if (argc
< 2 || c
->display_usage
) {
194 d_fprintf(stderr
, "usage: net eventlog export <file> <eventlog>\n");
198 etdb
= elog_open_tdb(argv
[1], false, true);
200 d_fprintf(stderr
, "can't open the eventlog TDB (%s)\n", argv
[1]);
208 struct eventlog_Record_tdb
*r
;
209 struct EVENTLOGRECORD e
;
211 r
= evlog_pull_record_tdb(ctx
, etdb
->tdb
, count
);
216 status
= evlog_tdb_entry_to_evt_entry(ctx
, r
, &e
);
217 if (!NT_STATUS_IS_OK(status
)) {
221 endoffset
+= ndr_size_EVENTLOGRECORD(&e
, NULL
, 0);
223 ADD_TO_ARRAY(ctx
, struct EVENTLOGRECORD
, e
, &evt
.records
, &num_records
);
227 evt
.hdr
.StartOffset
= 0x30;
228 evt
.hdr
.EndOffset
= evt
.hdr
.StartOffset
+ endoffset
;
229 evt
.hdr
.CurrentRecordNumber
= count
;
230 evt
.hdr
.OldestRecordNumber
= 1;
231 evt
.hdr
.MaxSize
= tdb_fetch_int32(etdb
->tdb
, EVT_MAXSIZE
);
233 evt
.hdr
.Retention
= tdb_fetch_int32(etdb
->tdb
, EVT_RETENTION
);
235 if (DEBUGLEVEL
>= 10) {
236 NDR_PRINT_DEBUG(EVENTLOGHEADER
, &evt
.hdr
);
239 evt
.eof
.BeginRecord
= 0x30;
240 evt
.eof
.EndRecord
= evt
.hdr
.StartOffset
+ endoffset
;
241 evt
.eof
.CurrentRecordNumber
= evt
.hdr
.CurrentRecordNumber
;
242 evt
.eof
.OldestRecordNumber
= evt
.hdr
.OldestRecordNumber
;
244 if (DEBUGLEVEL
>= 10) {
245 NDR_PRINT_DEBUG(EVENTLOGEOF
, &evt
.eof
);
248 ndr_err
= ndr_push_struct_blob(&blob
, ctx
, NULL
, &evt
,
249 (ndr_push_flags_fn_t
)ndr_push_EVENTLOG_EVT_FILE
);
250 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
251 d_fprintf(stderr
, "evt push failed: %s\n", ndr_errstr(ndr_err
));
255 if (!file_save(argv
[0], blob
.data
, blob
.length
)) {
256 d_fprintf(stderr
, "failed to save evt file: %s\n", argv
[0]);
263 elog_close_tdb(etdb
, false);
270 * 'net rpc eventlog' entrypoint.
271 * @param argc Standard main() style argc.
272 * @param argv Standard main() style argv. Initial components are already
276 int net_eventlog(struct net_context
*c
, int argc
, const char **argv
)
280 struct functable func
[] = {
286 "net eventlog dump\n"
287 " Dump win32 *.evt eventlog file"
294 "net eventlog import\n"
295 " Import win32 *.evt eventlog file"
302 "net eventlog export\n"
303 " Export win32 *.evt eventlog file"
307 { NULL
, NULL
, 0, NULL
, NULL
}
310 ret
= net_run_function(c
, argc
, argv
, "net eventlog", func
);