root/source3/utils/net_eventlog.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. net_eventlog_dump
  2. net_eventlog_import
  3. net_eventlog_export
  4. net_eventlog

   1 /*
   2  * Samba Unix/Linux SMB client library
   3  * Distributed SMB/CIFS Server Management Utility
   4  * Local win32 eventlog interface
   5  *
   6  * Copyright (C) Guenther Deschner 2009
   7  *
   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.
  12  *
  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.
  17  *
  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/>.
  20  */
  21 
  22 #include "includes.h"
  23 #include "utils/net.h"
  24 
  25 /**
  26  * Dump an *evt win32 eventlog file
  27  *
  28  * @param argc  Standard main() style argc.
  29  * @param argv  Standard main() style argv. Initial components are already
  30  *              stripped.
  31  *
  32  * @return A shell status integer (0 for success).
  33  **/
  34 
  35 static int net_eventlog_dump(struct net_context *c, int argc,
     /* [<][>][^][v][top][bottom][index][help] */
  36                              const char **argv)
  37 {
  38         int ret = -1;
  39         TALLOC_CTX *ctx = talloc_stackframe();
  40         enum ndr_err_code ndr_err;
  41         DATA_BLOB blob;
  42         struct EVENTLOG_EVT_FILE evt;
  43         char *s;
  44 
  45         if (argc < 1 || c->display_usage) {
  46                 d_fprintf(stderr, "usage: net eventlog dump <file.evt>\n");
  47                 goto done;
  48         }
  49 
  50         blob.data = (uint8_t *)file_load(argv[0], &blob.length, 0, ctx);
  51         if (!blob.data) {
  52                 d_fprintf(stderr, "failed to load evt file: %s\n", argv[0]);
  53                 goto done;
  54         }
  55 
  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));
  60                 goto done;
  61         }
  62 
  63         s = NDR_PRINT_STRUCT_STRING(ctx, EVENTLOG_EVT_FILE, &evt);
  64         if (s) {
  65                 printf("%s\n", s);
  66         }
  67 
  68         ret = 0;
  69  done:
  70         TALLOC_FREE(ctx);
  71         return ret;
  72 }
  73 
  74 /**
  75  * Import an *evt win32 eventlog file to internal tdb representation
  76  *
  77  * @param argc  Standard main() style argc.
  78  * @param argv  Standard main() style argv. Initial components are already
  79  *              stripped.
  80  *
  81  * @return A shell status integer (0 for success).
  82  **/
  83 
  84 static int net_eventlog_import(struct net_context *c, int argc,
     /* [<][>][^][v][top][bottom][index][help] */
  85                                const char **argv)
  86 {
  87         int ret = -1;
  88         TALLOC_CTX *ctx = talloc_stackframe();
  89         NTSTATUS status;
  90         enum ndr_err_code ndr_err;
  91         DATA_BLOB blob;
  92         uint32_t num_records = 0;
  93         uint32_t i;
  94         ELOG_TDB *etdb = NULL;
  95 
  96         struct EVENTLOGHEADER evt_header;
  97         struct EVENTLOG_EVT_FILE evt;
  98 
  99         if (argc < 2 || c->display_usage) {
 100                 d_fprintf(stderr, "usage: net eventlog import <file> <eventlog>\n");
 101                 goto done;
 102         }
 103 
 104         blob.data = (uint8_t *)file_load(argv[0], &blob.length, 0, ctx);
 105         if (!blob.data) {
 106                 d_fprintf(stderr, "failed to load evt file: %s\n", argv[0]);
 107                 goto done;
 108         }
 109 
 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));
 115                 goto done;
 116         }
 117 
 118         if (evt_header.Flags & ELF_LOGFILE_HEADER_WRAP) {
 119                 d_fprintf(stderr, "input file is wrapped, cannot proceed\n");
 120                 goto done;
 121         }
 122 
 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));
 127                 goto done;
 128         }
 129 
 130         /* NDR_PRINT_DEBUG(EVENTLOG_EVT_FILE, &evt); */
 131 
 132         etdb = elog_open_tdb(argv[1], false, false);
 133         if (!etdb) {
 134                 d_fprintf(stderr, "can't open the eventlog TDB (%s)\n", argv[1]);
 135                 goto done;
 136         }
 137 
 138         num_records = evt.hdr.CurrentRecordNumber - evt.hdr.OldestRecordNumber;
 139 
 140         for (i=0; i<num_records; i++) {
 141                 uint32_t record_number;
 142                 struct eventlog_Record_tdb e;
 143 
 144                 status = evlog_evt_entry_to_tdb_entry(ctx, &evt.records[i], &e);
 145                 if (!NT_STATUS_IS_OK(status)) {
 146                         goto done;
 147                 }
 148 
 149                 status = evlog_push_record_tdb(ctx, ELOG_TDB_CTX(etdb),
 150                                                &e, &record_number);
 151                 if (!NT_STATUS_IS_OK(status)) {
 152                         d_fprintf(stderr, "can't write to the eventlog: %s\n",
 153                                 nt_errstr(status));
 154                         goto done;
 155                 }
 156         }
 157 
 158         printf("wrote %d entries to tdb\n", i);
 159 
 160         ret = 0;
 161  done:
 162 
 163         elog_close_tdb(etdb, false);
 164 
 165         TALLOC_FREE(ctx);
 166         return ret;
 167 }
 168 
 169 /**
 170  * Export internal eventlog tdb representation to an *evt win32 eventlog file
 171  *
 172  * @param argc  Standard main() style argc.
 173  * @param argv  Standard main() style argv. Initial components are already
 174  *              stripped.
 175  *
 176  * @return A shell status integer (0 for success).
 177  **/
 178 
 179 static int net_eventlog_export(struct net_context *c, int argc,
     /* [<][>][^][v][top][bottom][index][help] */
 180                                const char **argv)
 181 {
 182         int ret = -1;
 183         NTSTATUS status;
 184         TALLOC_CTX *ctx = talloc_stackframe();
 185         DATA_BLOB blob;
 186         uint32_t num_records = 0;
 187         ELOG_TDB *etdb = NULL;
 188 
 189         if (argc < 2 || c->display_usage) {
 190                 d_fprintf(stderr, "usage: net eventlog export <file> <eventlog>\n");
 191                 goto done;
 192         }
 193 
 194         etdb = elog_open_tdb(argv[1], false, true);
 195         if (!etdb) {
 196                 d_fprintf(stderr, "can't open the eventlog TDB (%s)\n", argv[1]);
 197                 goto done;
 198         }
 199 
 200         status = evlog_convert_tdb_to_evt(ctx, etdb, &blob, &num_records);
 201         if (!NT_STATUS_IS_OK(status)) {
 202                 goto done;
 203         }
 204 
 205         if (!file_save(argv[0], blob.data, blob.length)) {
 206                 d_fprintf(stderr, "failed to save evt file: %s\n", argv[0]);
 207                 goto done;
 208         }
 209 
 210         ret = 0;
 211  done:
 212 
 213         elog_close_tdb(etdb, false);
 214 
 215         TALLOC_FREE(ctx);
 216         return ret;
 217 }
 218 
 219 /**
 220  * 'net rpc eventlog' entrypoint.
 221  * @param argc  Standard main() style argc.
 222  * @param argv  Standard main() style argv. Initial components are already
 223  *              stripped.
 224  **/
 225 
 226 int net_eventlog(struct net_context *c, int argc, const char **argv)
     /* [<][>][^][v][top][bottom][index][help] */
 227 {
 228         int ret = -1;
 229 
 230         struct functable func[] = {
 231                 {
 232                         "dump",
 233                         net_eventlog_dump,
 234                         NET_TRANSPORT_LOCAL,
 235                         "Dump eventlog",
 236                         "net eventlog dump\n"
 237                         "    Dump win32 *.evt eventlog file"
 238                 },
 239                 {
 240                         "import",
 241                         net_eventlog_import,
 242                         NET_TRANSPORT_LOCAL,
 243                         "Import eventlog",
 244                         "net eventlog import\n"
 245                         "    Import win32 *.evt eventlog file"
 246                 },
 247                 {
 248                         "export",
 249                         net_eventlog_export,
 250                         NET_TRANSPORT_LOCAL,
 251                         "Export eventlog",
 252                         "net eventlog export\n"
 253                         "    Export win32 *.evt eventlog file"
 254                 },
 255 
 256 
 257         { NULL, NULL, 0, NULL, NULL }
 258         };
 259 
 260         ret = net_run_function(c, argc, argv, "net eventlog", func);
 261 
 262         return ret;
 263 }

/* [<][>][^][v][top][bottom][index][help] */