Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 : SMB torture tester
4 : Copyright (C) Andrew Tridgell 2003
5 : Copyright (C) Jelmer Vernooij 2006
6 :
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.
11 :
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.
16 :
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/>.
19 : */
20 :
21 : #include "includes.h"
22 : #include "lib/util/util_file.h"
23 : #include "system/filesys.h"
24 : #include "system/locale.h"
25 : #include "librpc/ndr/libndr.h"
26 : #include "librpc/ndr/ndr_table.h"
27 : #include "librpc/gen_ndr/ndr_dcerpc.h"
28 : #include "lib/cmdline/cmdline.h"
29 : #include "param/param.h"
30 : #include "lib/util/base64.h"
31 :
32 11 : static const struct ndr_interface_call *find_function(
33 : const struct ndr_interface_table *p,
34 : const char *function)
35 : {
36 11 : unsigned int i;
37 11 : if (isdigit(function[0])) {
38 3 : char *eptr = NULL;
39 3 : i = strtoul(function, &eptr, 0);
40 3 : if (i >= p->num_calls
41 3 : || eptr == NULL
42 3 : || eptr[0] != '\0') {
43 0 : printf("Function number '%s' not found\n",
44 : function);
45 0 : exit(1);
46 : }
47 3 : return &p->calls[i];
48 : }
49 247 : for (i=0;i<p->num_calls;i++) {
50 247 : if (strcmp(p->calls[i].name, function) == 0) {
51 : break;
52 : }
53 : }
54 8 : if (i == p->num_calls) {
55 0 : printf("Function '%s' not found\n", function);
56 0 : exit(1);
57 : }
58 8 : return &p->calls[i];
59 : }
60 :
61 : /*
62 : * Find a public structure on the pipe and return it as if it were
63 : * a function (as the rest of ndrdump is based around functions)
64 : */
65 24 : static const struct ndr_interface_call *find_struct(
66 : const struct ndr_interface_table *p,
67 : const char *struct_name,
68 : struct ndr_interface_call *out_buffer)
69 : {
70 24 : unsigned int i;
71 24 : const struct ndr_interface_public_struct *public_struct = NULL;
72 24 : if (isdigit(struct_name[0])) {
73 1 : char *eptr = NULL;
74 1 : i = strtoul(struct_name, &eptr, 0);
75 1 : if (i >= p->num_public_structs
76 1 : || eptr == NULL
77 1 : || eptr[0] != '\0') {
78 0 : printf("Public structure number '%s' not found\n",
79 : struct_name);
80 0 : exit(1);
81 : }
82 1 : public_struct = &p->public_structs[i];
83 : } else {
84 151 : for (i=0;i<p->num_public_structs;i++) {
85 150 : if (strcmp(p->public_structs[i].name, struct_name) == 0) {
86 : break;
87 : }
88 : }
89 23 : if (i == p->num_public_structs) {
90 1 : printf("Public structure '%s' not found\n", struct_name);
91 1 : exit(1);
92 : }
93 22 : public_struct = &p->public_structs[i];
94 : }
95 23 : *out_buffer = (struct ndr_interface_call) {
96 23 : .name = public_struct->name,
97 23 : .struct_size = public_struct->struct_size,
98 23 : .ndr_pull = public_struct->ndr_pull,
99 23 : .ndr_push = public_struct->ndr_push,
100 23 : .ndr_print = public_struct->ndr_print
101 : };
102 23 : return out_buffer;
103 : }
104 :
105 0 : _NORETURN_ static void show_pipes(void)
106 : {
107 0 : const struct ndr_interface_list *l;
108 0 : printf("\nYou must specify a pipe\n");
109 0 : printf("known pipes are:\n");
110 0 : for (l=ndr_table_list();l;l=l->next) {
111 0 : if(l->table->helpstring) {
112 0 : printf("\t%s - %s\n", l->table->name, l->table->helpstring);
113 : } else {
114 0 : printf("\t%s\n", l->table->name);
115 : }
116 : }
117 0 : exit(1);
118 : }
119 :
120 0 : _NORETURN_ static void show_functions(const struct ndr_interface_table *p)
121 : {
122 0 : int i;
123 0 : printf("\nYou must specify a function\n");
124 0 : printf("known functions on '%s' are:\n", p->name);
125 0 : for (i=0;i<p->num_calls;i++) {
126 0 : printf("\t0x%02x (%2d) %s\n", i, i, p->calls[i].name);
127 : }
128 0 : printf("known public structures on '%s' are:\n", p->name);
129 0 : for (i=0;i<p->num_public_structs;i++) {
130 0 : printf("\t%s\n", p->public_structs[i].name);
131 : }
132 0 : exit(1);
133 : }
134 :
135 0 : static char *stdin_load(TALLOC_CTX *mem_ctx, size_t *size)
136 : {
137 0 : int num_read, total_len = 0;
138 0 : char buf[255];
139 0 : char *result = NULL;
140 :
141 0 : while((num_read = read(STDIN_FILENO, buf, 255)) > 0) {
142 :
143 0 : if (result) {
144 0 : result = talloc_realloc(
145 : mem_ctx, result, char, total_len + num_read);
146 : } else {
147 0 : result = talloc_array(mem_ctx, char, num_read);
148 : }
149 :
150 0 : memcpy(result + total_len, buf, num_read);
151 :
152 0 : total_len += num_read;
153 : }
154 :
155 0 : if (size)
156 0 : *size = total_len;
157 :
158 0 : return result;
159 : }
160 :
161 0 : static const struct ndr_interface_table *load_iface_from_plugin(const char *plugin, const char *pipe_name)
162 : {
163 0 : const struct ndr_interface_table *p;
164 0 : void *handle;
165 0 : char *symbol;
166 :
167 0 : handle = dlopen(plugin, RTLD_NOW);
168 0 : if (handle == NULL) {
169 0 : printf("%s: Unable to open: %s\n", plugin, dlerror());
170 0 : return NULL;
171 : }
172 :
173 0 : symbol = talloc_asprintf(NULL, "ndr_table_%s", pipe_name);
174 0 : p = (const struct ndr_interface_table *)dlsym(handle, symbol);
175 :
176 0 : if (!p) {
177 0 : printf("%s: Unable to find DCE/RPC interface table for '%s': %s\n", plugin, pipe_name, dlerror());
178 0 : talloc_free(symbol);
179 0 : dlclose(handle);
180 0 : return NULL;
181 : }
182 :
183 0 : talloc_free(symbol);
184 :
185 0 : return p;
186 : }
187 :
188 14 : static void ndrdump_data(uint8_t *d, uint32_t l, bool force)
189 : {
190 14 : dump_data_file(d, l, !force, stdout);
191 14 : }
192 :
193 8 : static void ndrdump_data_diff(const uint8_t *d1, size_t l1,
194 : const uint8_t *d2, size_t l2,
195 : bool force)
196 : {
197 8 : dump_data_file_diff(stdout, !force, d1, l1, d2, l2);
198 8 : }
199 :
200 11 : static NTSTATUS ndrdump_pull_and_print_pipes(const char *function,
201 : struct ndr_pull *ndr_pull,
202 : struct ndr_print *ndr_print,
203 : const struct ndr_interface_call_pipes *pipes)
204 : {
205 11 : enum ndr_err_code ndr_err;
206 11 : uint32_t i;
207 :
208 11 : for (i=0; i < pipes->num_pipes; i++) {
209 : uint64_t idx = 0;
210 0 : while (true) {
211 0 : void *saved_mem_ctx;
212 0 : uint32_t *count;
213 0 : void *c;
214 0 : char *n;
215 :
216 0 : c = talloc_zero_size(ndr_pull, pipes->pipes[i].chunk_struct_size);
217 0 : talloc_set_name(c, "struct %s", pipes->pipes[i].name);
218 : /*
219 : * Note: the first struct member is always
220 : * 'uint32_t count;'
221 : */
222 0 : count = (uint32_t *)c;
223 :
224 0 : n = talloc_asprintf(c, "%s: %s[%"PRIu64"]",
225 0 : function, pipes->pipes[i].name,
226 : idx);
227 :
228 0 : saved_mem_ctx = ndr_pull->current_mem_ctx;
229 0 : ndr_pull->current_mem_ctx = c;
230 0 : ndr_err = pipes->pipes[i].ndr_pull(ndr_pull, NDR_SCALARS, c);
231 0 : ndr_pull->current_mem_ctx = saved_mem_ctx;
232 :
233 0 : printf("pull returned %s\n",
234 : ndr_map_error2string(ndr_err));
235 0 : if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
236 0 : talloc_free(c);
237 0 : return ndr_map_error2ntstatus(ndr_err);
238 : }
239 0 : pipes->pipes[i].ndr_print(ndr_print, n, c);
240 0 : if (*count == 0) {
241 0 : talloc_free(c);
242 0 : break;
243 : }
244 0 : talloc_free(c);
245 0 : idx++;
246 : }
247 : }
248 :
249 11 : return NT_STATUS_OK;
250 : }
251 :
252 0 : static void ndr_print_dummy(struct ndr_print *ndr, const char *format, ...)
253 : {
254 : /* This is here so that you can turn ndr printing off for the purposes
255 : of benchmarking ndr parsing. */
256 0 : }
257 :
258 35 : int main(int argc, const char *argv[])
259 : {
260 35 : const struct ndr_interface_table *p = NULL;
261 35 : const struct ndr_interface_call *f;
262 35 : struct ndr_interface_call f_buffer;
263 35 : const char *pipe_name = NULL;
264 35 : const char *filename = NULL;
265 : /*
266 : * The format type:
267 : * in: a request
268 : * out: a response
269 : * struct: a public structure
270 : */
271 35 : const char *type = NULL;
272 : /*
273 : * Format is either the name of the decoding function or the
274 : * name of a public structure
275 : */
276 35 : const char *format = NULL;
277 35 : const char *cmdline_input = NULL;
278 35 : const uint8_t *data;
279 35 : size_t size;
280 35 : DATA_BLOB blob;
281 35 : struct ndr_pull *ndr_pull;
282 35 : struct ndr_print *ndr_print;
283 35 : TALLOC_CTX *mem_ctx;
284 35 : ndr_flags_type flags = 0;
285 35 : poptContext pc;
286 35 : NTSTATUS status;
287 35 : enum ndr_err_code ndr_err;
288 35 : void *st;
289 35 : void *v_st;
290 35 : const char *ctx_filename = NULL;
291 35 : const char *plugin = NULL;
292 35 : bool validate = false;
293 35 : bool dumpdata = false;
294 35 : bool assume_ndr64 = false;
295 35 : bool quiet = false;
296 35 : bool hex_input = false;
297 35 : bool base64_input = false;
298 35 : bool print_after_parse_failure = false;
299 35 : int opt;
300 35 : enum {
301 : OPT_CONTEXT_FILE=1000,
302 : OPT_VALIDATE,
303 : OPT_DUMP_DATA,
304 : OPT_LOAD_DSO,
305 : OPT_NDR64,
306 : OPT_QUIET,
307 : OPT_BASE64_INPUT,
308 : OPT_HEX_INPUT,
309 : OPT_CMDLINE_INPUT,
310 : OPT_PRINT_AFTER_PARSE_FAILURE,
311 : };
312 105 : struct poptOption long_options[] = {
313 : POPT_AUTOHELP
314 : {"context-file", 'c', POPT_ARG_STRING, NULL, OPT_CONTEXT_FILE, "In-filename to parse first", "CTX-FILE" },
315 : {"validate", 0, POPT_ARG_NONE, NULL, OPT_VALIDATE, "try to validate the data", NULL },
316 : {"dump-data", 0, POPT_ARG_NONE, NULL, OPT_DUMP_DATA, "dump the hex data", NULL },
317 : {"load-dso", 0, POPT_ARG_STRING, NULL, OPT_LOAD_DSO, "load from shared object file", NULL },
318 : {"ndr64", 0, POPT_ARG_NONE, NULL, OPT_NDR64, "Assume NDR64 data", NULL },
319 : {"quiet", 0, POPT_ARG_NONE, NULL, OPT_QUIET, "Don't actually dump anything", NULL },
320 : {"base64-input", 0, POPT_ARG_NONE, NULL, OPT_BASE64_INPUT, "Read the input file in as a base64 string", NULL },
321 : {"hex-input", 0, POPT_ARG_NONE, NULL, OPT_HEX_INPUT, "Read the input file in as a hex dump", NULL },
322 : {"input", 0, POPT_ARG_STRING, NULL, OPT_CMDLINE_INPUT, "Provide the input on the command line (use with --base64-input)", "INPUT" },
323 : {"print-after-parse-failure", 0, POPT_ARG_NONE, NULL, OPT_PRINT_AFTER_PARSE_FAILURE,
324 : "Try to print structures that fail to parse (used to develop parsers, segfaults are likely).", NULL },
325 35 : POPT_COMMON_SAMBA
326 35 : POPT_COMMON_VERSION
327 : POPT_TABLEEND
328 : };
329 35 : uint32_t highest_ofs;
330 35 : struct dcerpc_sec_verification_trailer *sec_vt = NULL;
331 35 : bool ok;
332 :
333 35 : ndr_table_init();
334 :
335 : /* Initialise samba stuff */
336 35 : smb_init_locale();
337 :
338 35 : setlinebuf(stdout);
339 :
340 35 : mem_ctx = talloc_init("ndrdump.c/main");
341 35 : if (mem_ctx == NULL) {
342 0 : exit(ENOMEM);
343 : }
344 :
345 35 : ok = samba_cmdline_init(mem_ctx,
346 : SAMBA_CMDLINE_CONFIG_CLIENT,
347 : false /* require_smbconf */);
348 35 : if (!ok) {
349 0 : DBG_ERR("Failed to init cmdline parser!\n");
350 0 : TALLOC_FREE(mem_ctx);
351 0 : exit(1);
352 : }
353 :
354 35 : pc = samba_popt_get_context(getprogname(),
355 : argc,
356 : argv,
357 : long_options,
358 : 0);
359 35 : if (pc == NULL) {
360 0 : DBG_ERR("Failed to setup popt context!\n");
361 0 : TALLOC_FREE(mem_ctx);
362 0 : exit(1);
363 : }
364 :
365 35 : poptSetOtherOptionHelp(
366 : pc, "<pipe|uuid> <format> <in|out|struct> [<filename>]");
367 :
368 90 : while ((opt = poptGetNextOpt(pc)) != -1) {
369 55 : switch (opt) {
370 1 : case OPT_CONTEXT_FILE:
371 1 : ctx_filename = poptGetOptArg(pc);
372 1 : break;
373 13 : case OPT_VALIDATE:
374 13 : validate = true;
375 13 : break;
376 2 : case OPT_DUMP_DATA:
377 2 : dumpdata = true;
378 2 : break;
379 0 : case OPT_LOAD_DSO:
380 0 : plugin = poptGetOptArg(pc);
381 0 : break;
382 0 : case OPT_NDR64:
383 0 : assume_ndr64 = true;
384 0 : break;
385 0 : case OPT_QUIET:
386 0 : quiet = true;
387 0 : break;
388 15 : case OPT_BASE64_INPUT:
389 15 : base64_input = true;
390 15 : break;
391 7 : case OPT_HEX_INPUT:
392 7 : hex_input = true;
393 7 : break;
394 16 : case OPT_CMDLINE_INPUT:
395 16 : cmdline_input = poptGetOptArg(pc);
396 16 : break;
397 1 : case OPT_PRINT_AFTER_PARSE_FAILURE:
398 1 : print_after_parse_failure = true;
399 1 : break;
400 : }
401 : }
402 :
403 35 : pipe_name = poptGetArg(pc);
404 :
405 35 : if (!pipe_name) {
406 0 : poptPrintUsage(pc, stderr, 0);
407 0 : show_pipes();
408 : exit(1);
409 : }
410 :
411 35 : if (plugin != NULL) {
412 0 : p = load_iface_from_plugin(plugin, pipe_name);
413 : }
414 0 : if (!p) {
415 35 : p = ndr_table_by_name(pipe_name);
416 : }
417 :
418 35 : if (!p) {
419 0 : struct GUID uuid;
420 :
421 0 : status = GUID_from_string(pipe_name, &uuid);
422 :
423 0 : if (NT_STATUS_IS_OK(status)) {
424 0 : p = ndr_table_by_uuid(&uuid);
425 : }
426 : }
427 :
428 35 : if (!p) {
429 0 : printf("Unknown pipe or UUID '%s'\n", pipe_name);
430 0 : exit(1);
431 : }
432 :
433 35 : format = poptGetArg(pc);
434 35 : type = poptGetArg(pc);
435 35 : filename = poptGetArg(pc);
436 :
437 35 : if (!format || !type) {
438 0 : poptPrintUsage(pc, stderr, 0);
439 0 : show_functions(p);
440 : exit(1);
441 : }
442 :
443 35 : if (strcmp(type, "struct") == 0) {
444 24 : flags = NDR_SCALARS|NDR_BUFFERS; /* neither NDR_IN nor NDR_OUT */
445 24 : f = find_struct(p, format, &f_buffer);
446 : } else {
447 11 : f = find_function(p, format);
448 11 : if (strcmp(type, "in") == 0 ||
449 7 : strcmp(type, "request") == 0) {
450 : flags |= NDR_IN;
451 7 : } else if (strcmp(type, "out") == 0 ||
452 0 : strcmp(type, "response") == 0) {
453 : flags |= NDR_OUT;
454 : } else {
455 0 : printf("Bad type value '%s'\n", type);
456 0 : exit(1);
457 : }
458 : }
459 :
460 34 : st = talloc_zero_size(mem_ctx, f->struct_size);
461 34 : if (!st) {
462 0 : printf("Unable to allocate %zu bytes for %s structure\n",
463 0 : f->struct_size,
464 0 : f->name);
465 0 : TALLOC_FREE(mem_ctx);
466 0 : exit(1);
467 : }
468 :
469 34 : v_st = talloc_zero_size(mem_ctx, f->struct_size);
470 34 : if (!v_st) {
471 0 : printf("Unable to allocate %zu bytes for %s validation "
472 : "structure\n",
473 0 : f->struct_size,
474 0 : f->name);
475 0 : TALLOC_FREE(mem_ctx);
476 0 : exit(1);
477 : }
478 :
479 34 : if (ctx_filename) {
480 1 : if (flags & NDR_IN) {
481 0 : printf("Context file can only be used for \"out\" packages\n");
482 0 : TALLOC_FREE(mem_ctx);
483 0 : exit(1);
484 : }
485 :
486 1 : data = (uint8_t *)file_load(ctx_filename, &size, 0, mem_ctx);
487 1 : if (!data) {
488 0 : perror(ctx_filename);
489 0 : TALLOC_FREE(mem_ctx);
490 0 : exit(1);
491 : }
492 :
493 1 : blob = data_blob_const(data, size);
494 :
495 1 : ndr_pull = ndr_pull_init_blob(&blob, mem_ctx);
496 1 : if (ndr_pull == NULL) {
497 0 : perror("ndr_pull_init_blob");
498 0 : TALLOC_FREE(mem_ctx);
499 0 : exit(1);
500 : }
501 1 : ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
502 1 : if (assume_ndr64) {
503 0 : ndr_pull->flags |= LIBNDR_FLAG_NDR64;
504 : }
505 :
506 1 : ndr_err = f->ndr_pull(ndr_pull, NDR_IN, st);
507 :
508 1 : if (ndr_pull->offset > ndr_pull->relative_highest_offset) {
509 : highest_ofs = ndr_pull->offset;
510 : } else {
511 : highest_ofs = ndr_pull->relative_highest_offset;
512 : }
513 :
514 1 : if (highest_ofs != ndr_pull->data_size) {
515 1 : printf("WARNING! %"PRIu32" unread bytes while parsing context file\n", ndr_pull->data_size - highest_ofs);
516 : }
517 :
518 1 : if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
519 0 : printf("pull for context file returned %s\n",
520 : ndr_map_error2string(ndr_err));
521 0 : TALLOC_FREE(mem_ctx);
522 0 : exit(2);
523 : }
524 1 : memcpy(v_st, st, f->struct_size);
525 : }
526 :
527 34 : if (filename && cmdline_input) {
528 0 : printf("cannot combine --input with a filename\n");
529 0 : TALLOC_FREE(mem_ctx);
530 0 : exit(1);
531 34 : } else if (cmdline_input) {
532 15 : data = (const uint8_t *)cmdline_input;
533 15 : size = strlen(cmdline_input);
534 19 : } else if (filename) {
535 19 : data = (uint8_t *)file_load(filename, &size, 0, mem_ctx);
536 : } else {
537 0 : data = (uint8_t *)stdin_load(mem_ctx, &size);
538 : }
539 :
540 34 : if (!data) {
541 0 : if (filename)
542 0 : perror(filename);
543 : else
544 0 : perror("stdin");
545 0 : exit(1);
546 : }
547 :
548 34 : if (hex_input && base64_input) {
549 0 : printf("cannot combine --hex-input with --base64-input\n");
550 0 : TALLOC_FREE(mem_ctx);
551 0 : exit(1);
552 :
553 34 : } else if (hex_input && size >= 1 && data[0] != '[') {
554 3 : blob = strhex_to_data_blob(mem_ctx, (const char *)data);
555 31 : } else if (hex_input) {
556 4 : blob = hexdump_to_data_blob(mem_ctx, (const char *)data, size);
557 27 : } else if (base64_input) {
558 : /* Use talloc_strndup() to ensure null termination */
559 15 : blob = base64_decode_data_blob_talloc(
560 : mem_ctx,
561 15 : talloc_strndup(mem_ctx, (const char *)data, size));
562 : } else {
563 12 : blob = data_blob_const(data, size);
564 : }
565 :
566 34 : if (data != NULL && blob.data == NULL) {
567 0 : printf("failed to decode input data\n");
568 0 : TALLOC_FREE(mem_ctx);
569 0 : exit(1);
570 : }
571 :
572 34 : ndr_pull = ndr_pull_init_blob(&blob, mem_ctx);
573 34 : if (ndr_pull == NULL) {
574 0 : perror("ndr_pull_init_blob");
575 0 : TALLOC_FREE(mem_ctx);
576 0 : exit(1);
577 : }
578 34 : ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
579 34 : if (assume_ndr64) {
580 0 : ndr_pull->flags |= LIBNDR_FLAG_NDR64;
581 : }
582 :
583 34 : ndr_print = talloc_zero(mem_ctx, struct ndr_print);
584 34 : if (quiet) {
585 0 : ndr_print->print = ndr_print_dummy;
586 : } else {
587 34 : ndr_print->print = ndr_print_printf_helper;
588 : }
589 34 : ndr_print->depth = 1;
590 :
591 34 : ndr_err = ndr_pop_dcerpc_sec_verification_trailer(ndr_pull, mem_ctx, &sec_vt);
592 34 : if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
593 0 : printf("ndr_pop_dcerpc_sec_verification_trailer returned %s\n",
594 : ndr_map_error2string(ndr_err));
595 : }
596 :
597 34 : if (sec_vt != NULL && sec_vt->count.count > 0) {
598 0 : printf("SEC_VT: consumed %zu bytes\n",
599 0 : blob.length - ndr_pull->data_size);
600 0 : if (dumpdata) {
601 0 : ndrdump_data(blob.data + ndr_pull->data_size,
602 0 : blob.length - ndr_pull->data_size,
603 : dumpdata);
604 : }
605 0 : ndr_print_dcerpc_sec_verification_trailer(ndr_print, "SEC_VT", sec_vt);
606 : }
607 34 : TALLOC_FREE(sec_vt);
608 :
609 34 : if (flags & NDR_OUT) {
610 7 : status = ndrdump_pull_and_print_pipes(format,
611 : ndr_pull,
612 : ndr_print,
613 : &f->out_pipes);
614 7 : if (!NT_STATUS_IS_OK(status)) {
615 0 : printf("pull and dump of OUT pipes FAILED: %s\n",
616 : nt_errstr(status));
617 0 : TALLOC_FREE(mem_ctx);
618 0 : exit(2);
619 : }
620 : }
621 :
622 34 : ndr_err = f->ndr_pull(ndr_pull, flags, st);
623 34 : printf("pull returned %s\n",
624 : ndr_map_error2string(ndr_err));
625 :
626 34 : if (ndr_pull->offset > ndr_pull->relative_highest_offset) {
627 : highest_ofs = ndr_pull->offset;
628 : } else {
629 : highest_ofs = ndr_pull->relative_highest_offset;
630 : }
631 :
632 34 : if (dumpdata) {
633 2 : printf("%"PRIu32" bytes consumed\n", highest_ofs);
634 2 : ndrdump_data(blob.data, blob.length, dumpdata);
635 : }
636 :
637 34 : if (!print_after_parse_failure && !NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
638 5 : TALLOC_FREE(mem_ctx);
639 5 : exit(2);
640 : }
641 :
642 29 : if (highest_ofs != ndr_pull->data_size) {
643 12 : printf("WARNING! %"PRIu32" unread bytes\n", ndr_pull->data_size - highest_ofs);
644 41 : ndrdump_data(ndr_pull->data+highest_ofs,
645 12 : ndr_pull->data_size - highest_ofs,
646 : dumpdata);
647 : }
648 :
649 29 : if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
650 30 : printf("WARNING: pull of %s was incomplete, "
651 : "therefore the parse below may SEGFAULT\n",
652 1 : f->name);
653 : }
654 :
655 29 : f->ndr_print(ndr_print, f->name, flags, st);
656 :
657 29 : if (flags & NDR_IN) {
658 4 : status = ndrdump_pull_and_print_pipes(format,
659 : ndr_pull,
660 : ndr_print,
661 : &f->in_pipes);
662 4 : if (!NT_STATUS_IS_OK(status)) {
663 0 : printf("pull and dump of IN pipes FAILED: %s\n",
664 : nt_errstr(status));
665 0 : exit(1);
666 : }
667 : }
668 :
669 : /* Do not proceed to validate if we got an error */
670 29 : if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
671 2 : printf("dump of failed-to-parse %s complete\n",
672 1 : f->name);
673 1 : TALLOC_FREE(mem_ctx);
674 1 : exit(2);
675 : }
676 :
677 28 : if (validate) {
678 13 : DATA_BLOB v_blob;
679 13 : struct ndr_push *ndr_v_push;
680 13 : struct ndr_pull *ndr_v_pull;
681 13 : struct ndr_print *ndr_v_print;
682 13 : uint32_t highest_v_ofs;
683 13 : uint32_t i;
684 13 : uint8_t byte_a, byte_b;
685 13 : bool differ;
686 :
687 13 : ndr_v_push = ndr_push_init_ctx(mem_ctx);
688 13 : if (ndr_v_push == NULL) {
689 0 : printf("No memory\n");
690 0 : exit(1);
691 : }
692 :
693 13 : if (assume_ndr64) {
694 0 : ndr_v_push->flags |= LIBNDR_FLAG_NDR64;
695 : }
696 :
697 13 : ndr_err = f->ndr_push(ndr_v_push, flags, st);
698 13 : printf("push returned %s\n",
699 : ndr_map_error2string(ndr_err));
700 13 : if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
701 0 : printf("validate push FAILED\n");
702 0 : TALLOC_FREE(mem_ctx);
703 0 : exit(1);
704 : }
705 :
706 13 : v_blob = ndr_push_blob(ndr_v_push);
707 :
708 13 : if (dumpdata) {
709 0 : printf("%zu bytes generated (validate)\n", v_blob.length);
710 0 : ndrdump_data(v_blob.data, v_blob.length, dumpdata);
711 : }
712 :
713 13 : ndr_v_pull = ndr_pull_init_blob(&v_blob, mem_ctx);
714 13 : if (ndr_v_pull == NULL) {
715 0 : perror("ndr_pull_init_blob");
716 0 : TALLOC_FREE(mem_ctx);
717 0 : exit(1);
718 : }
719 13 : ndr_v_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
720 :
721 13 : ndr_err = f->ndr_pull(ndr_v_pull, flags, v_st);
722 13 : printf("pull returned %s\n",
723 : ndr_map_error2string(ndr_err));
724 13 : if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
725 0 : printf("validate pull FAILED\n");
726 0 : TALLOC_FREE(mem_ctx);
727 0 : exit(1);
728 : }
729 :
730 13 : if (ndr_v_pull->offset > ndr_v_pull->relative_highest_offset) {
731 : highest_v_ofs = ndr_v_pull->offset;
732 : } else {
733 : highest_v_ofs = ndr_v_pull->relative_highest_offset;
734 : }
735 :
736 13 : if (highest_v_ofs != ndr_v_pull->data_size) {
737 0 : printf("WARNING! %"PRIu32" unread bytes in validation\n",
738 : ndr_v_pull->data_size - highest_v_ofs);
739 13 : ndrdump_data(ndr_v_pull->data + highest_v_ofs,
740 0 : ndr_v_pull->data_size - highest_v_ofs,
741 : dumpdata);
742 : }
743 :
744 13 : ndr_v_print = talloc_zero(mem_ctx, struct ndr_print);
745 13 : ndr_v_print->print = ndr_print_debug_helper;
746 13 : ndr_v_print->depth = 1;
747 13 : f->ndr_print(ndr_v_print,
748 : format,
749 : flags, v_st);
750 :
751 13 : if (blob.length != v_blob.length) {
752 7 : printf("WARNING! orig bytes:%zu validated pushed bytes:%zu\n",
753 : blob.length, v_blob.length);
754 : }
755 :
756 13 : if (highest_ofs != highest_v_ofs) {
757 3 : printf("WARNING! orig pulled bytes:%"PRIu32" validated pulled bytes:%"PRIu32"\n",
758 : highest_ofs, highest_v_ofs);
759 : }
760 :
761 13 : differ = false;
762 13 : byte_a = 0x00;
763 13 : byte_b = 0x00;
764 18853 : for (i=0; i < blob.length; i++) {
765 18848 : byte_a = blob.data[i];
766 :
767 18848 : if (i == v_blob.length) {
768 : byte_b = 0x00;
769 : differ = true;
770 : break;
771 : }
772 :
773 18847 : byte_b = v_blob.data[i];
774 :
775 18847 : if (byte_a != byte_b) {
776 : differ = true;
777 : break;
778 : }
779 : }
780 13 : if (differ) {
781 8 : printf("WARNING! orig and validated differ at byte 0x%02"PRIX32" (%"PRIu32")\n", i, i);
782 8 : printf("WARNING! orig byte[0x%02"PRIX32"] = 0x%02"PRIX8" validated byte[0x%02"PRIX32"] = 0x%02"PRIX8"\n",
783 : i, byte_a, i, byte_b);
784 13 : ndrdump_data_diff(blob.data, blob.length,
785 8 : v_blob.data, v_blob.length,
786 : dumpdata);
787 : }
788 : }
789 :
790 28 : printf("dump OK\n");
791 28 : TALLOC_FREE(mem_ctx);
792 :
793 28 : poptFreeContext(pc);
794 :
795 28 : return 0;
796 : }
|