2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2003
5 Copyright (C) Jelmer Vernooij 2006
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 "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"
32 static const struct ndr_interface_call *find_function(
33 const struct ndr_interface_table *p,
37 if (isdigit(function[0])) {
39 i = strtoul(function, &eptr, 0);
43 printf("Function number '%s' not found\n",
49 for (i=0;i<p->num_calls;i++) {
50 if (strcmp(p->calls[i].name, function) == 0) {
54 if (i == p->num_calls) {
55 printf("Function '%s' not found\n", function);
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)
65 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)
71 const struct ndr_interface_public_struct *public_struct = NULL;
72 if (isdigit(struct_name[0])) {
74 i = strtoul(struct_name, &eptr, 0);
75 if (i >= p->num_public_structs
78 printf("Public structure number '%s' not found\n",
82 public_struct = &p->public_structs[i];
84 for (i=0;i<p->num_public_structs;i++) {
85 if (strcmp(p->public_structs[i].name, struct_name) == 0) {
89 if (i == p->num_public_structs) {
90 printf("Public structure '%s' not found\n", struct_name);
93 public_struct = &p->public_structs[i];
95 *out_buffer = (struct ndr_interface_call) {
96 .name = public_struct->name,
97 .struct_size = public_struct->struct_size,
98 .ndr_pull = public_struct->ndr_pull,
99 .ndr_push = public_struct->ndr_push,
100 .ndr_print = public_struct->ndr_print
105 _NORETURN_ static void show_pipes(void)
107 const struct ndr_interface_list *l;
108 printf("\nYou must specify a pipe\n");
109 printf("known pipes are:\n");
110 for (l=ndr_table_list();l;l=l->next) {
111 if(l->table->helpstring) {
112 printf("\t%s - %s\n", l->table->name, l->table->helpstring);
114 printf("\t%s\n", l->table->name);
120 _NORETURN_ static void show_functions(const struct ndr_interface_table *p)
123 printf("\nYou must specify a function\n");
124 printf("known functions on '%s' are:\n", p->name);
125 for (i=0;i<p->num_calls;i++) {
126 printf("\t0x%02x (%2d) %s\n", i, i, p->calls[i].name);
128 printf("known public structures on '%s' are:\n", p->name);
129 for (i=0;i<p->num_public_structs;i++) {
130 printf("\t%s\n", p->public_structs[i].name);
135 static char *stdin_load(TALLOC_CTX *mem_ctx, size_t *size)
137 int num_read, total_len = 0;
141 while((num_read = read(STDIN_FILENO, buf, 255)) > 0) {
144 result = talloc_realloc(
145 mem_ctx, result, char, total_len + num_read);
147 result = talloc_array(mem_ctx, char, num_read);
150 memcpy(result + total_len, buf, num_read);
152 total_len += num_read;
161 static const struct ndr_interface_table *load_iface_from_plugin(const char *plugin, const char *pipe_name)
163 const struct ndr_interface_table *p;
167 handle = dlopen(plugin, RTLD_NOW);
168 if (handle == NULL) {
169 printf("%s: Unable to open: %s\n", plugin, dlerror());
173 symbol = talloc_asprintf(NULL, "ndr_table_%s", pipe_name);
174 p = (const struct ndr_interface_table *)dlsym(handle, symbol);
177 printf("%s: Unable to find DCE/RPC interface table for '%s': %s\n", plugin, pipe_name, dlerror());
188 static void ndrdump_data(uint8_t *d, uint32_t l, bool force)
190 dump_data_file(d, l, !force, stdout);
193 static void ndrdump_data_diff(const uint8_t *d1, size_t l1,
194 const uint8_t *d2, size_t l2,
197 dump_data_file_diff(stdout, !force, d1, l1, d2, l2);
200 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)
205 enum ndr_err_code ndr_err;
208 for (i=0; i < pipes->num_pipes; i++) {
216 c = talloc_zero_size(ndr_pull, pipes->pipes[i].chunk_struct_size);
217 talloc_set_name(c, "struct %s", pipes->pipes[i].name);
219 * Note: the first struct member is always
222 count = (uint32_t *)c;
224 n = talloc_asprintf(c, "%s: %s[%"PRIu64"]",
225 function, pipes->pipes[i].name,
228 saved_mem_ctx = ndr_pull->current_mem_ctx;
229 ndr_pull->current_mem_ctx = c;
230 ndr_err = pipes->pipes[i].ndr_pull(ndr_pull, NDR_SCALARS, c);
231 ndr_pull->current_mem_ctx = saved_mem_ctx;
233 printf("pull returned %s\n",
234 ndr_map_error2string(ndr_err));
235 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
237 return ndr_map_error2ntstatus(ndr_err);
239 pipes->pipes[i].ndr_print(ndr_print, n, c);
252 static void ndr_print_dummy(struct ndr_print *ndr, const char *format, ...)
254 /* This is here so that you can turn ndr printing off for the purposes
255 of benchmarking ndr parsing. */
258 int main(int argc, const char *argv[])
260 const struct ndr_interface_table *p = NULL;
261 const struct ndr_interface_call *f;
262 struct ndr_interface_call f_buffer;
263 const char *pipe_name = NULL;
264 const char *filename = NULL;
269 * struct: a public structure
271 const char *type = NULL;
273 * Format is either the name of the decoding function or the
274 * name of a public structure
276 const char *format = NULL;
277 const char *cmdline_input = NULL;
281 struct ndr_pull *ndr_pull;
282 struct ndr_print *ndr_print;
284 ndr_flags_type flags = 0;
287 enum ndr_err_code ndr_err;
290 const char *ctx_filename = NULL;
291 const char *plugin = NULL;
292 bool validate = false;
293 bool dumpdata = false;
294 bool assume_ndr64 = false;
296 bool hex_input = false;
297 bool base64_input = false;
298 bool print_after_parse_failure = false;
301 OPT_CONTEXT_FILE=1000,
310 OPT_PRINT_AFTER_PARSE_FAILURE,
312 struct poptOption long_options[] = {
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 },
329 uint32_t highest_ofs;
330 struct dcerpc_sec_verification_trailer *sec_vt = NULL;
335 /* Initialise samba stuff */
340 mem_ctx = talloc_init("ndrdump.c/main");
341 if (mem_ctx == NULL) {
345 ok = samba_cmdline_init(mem_ctx,
346 SAMBA_CMDLINE_CONFIG_CLIENT,
347 false /* require_smbconf */);
349 DBG_ERR("Failed to init cmdline parser!\n");
350 TALLOC_FREE(mem_ctx);
354 pc = samba_popt_get_context(getprogname(),
360 DBG_ERR("Failed to setup popt context!\n");
361 TALLOC_FREE(mem_ctx);
365 poptSetOtherOptionHelp(
366 pc, "<pipe|uuid> <format> <in|out|struct> [<filename>]");
368 while ((opt = poptGetNextOpt(pc)) != -1) {
370 case OPT_CONTEXT_FILE:
371 ctx_filename = poptGetOptArg(pc);
380 plugin = poptGetOptArg(pc);
388 case OPT_BASE64_INPUT:
394 case OPT_CMDLINE_INPUT:
395 cmdline_input = poptGetOptArg(pc);
397 case OPT_PRINT_AFTER_PARSE_FAILURE:
398 print_after_parse_failure = true;
403 pipe_name = poptGetArg(pc);
406 poptPrintUsage(pc, stderr, 0);
411 if (plugin != NULL) {
412 p = load_iface_from_plugin(plugin, pipe_name);
415 p = ndr_table_by_name(pipe_name);
421 status = GUID_from_string(pipe_name, &uuid);
423 if (NT_STATUS_IS_OK(status)) {
424 p = ndr_table_by_uuid(&uuid);
429 printf("Unknown pipe or UUID '%s'\n", pipe_name);
433 format = poptGetArg(pc);
434 type = poptGetArg(pc);
435 filename = poptGetArg(pc);
437 if (!format || !type) {
438 poptPrintUsage(pc, stderr, 0);
443 if (strcmp(type, "struct") == 0) {
444 flags = NDR_SCALARS|NDR_BUFFERS; /* neither NDR_IN nor NDR_OUT */
445 f = find_struct(p, format, &f_buffer);
447 f = find_function(p, format);
448 if (strcmp(type, "in") == 0 ||
449 strcmp(type, "request") == 0) {
451 } else if (strcmp(type, "out") == 0 ||
452 strcmp(type, "response") == 0) {
455 printf("Bad type value '%s'\n", type);
460 st = talloc_zero_size(mem_ctx, f->struct_size);
462 printf("Unable to allocate %zu bytes for %s structure\n",
465 TALLOC_FREE(mem_ctx);
469 v_st = talloc_zero_size(mem_ctx, f->struct_size);
471 printf("Unable to allocate %zu bytes for %s validation "
475 TALLOC_FREE(mem_ctx);
480 if (flags & NDR_IN) {
481 printf("Context file can only be used for \"out\" packages\n");
482 TALLOC_FREE(mem_ctx);
486 data = (uint8_t *)file_load(ctx_filename, &size, 0, mem_ctx);
488 perror(ctx_filename);
489 TALLOC_FREE(mem_ctx);
493 blob = data_blob_const(data, size);
495 ndr_pull = ndr_pull_init_blob(&blob, mem_ctx);
496 if (ndr_pull == NULL) {
497 perror("ndr_pull_init_blob");
498 TALLOC_FREE(mem_ctx);
501 ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
503 ndr_pull->flags |= LIBNDR_FLAG_NDR64;
506 ndr_err = f->ndr_pull(ndr_pull, NDR_IN, st);
508 if (ndr_pull->offset > ndr_pull->relative_highest_offset) {
509 highest_ofs = ndr_pull->offset;
511 highest_ofs = ndr_pull->relative_highest_offset;
514 if (highest_ofs != ndr_pull->data_size) {
515 printf("WARNING! %"PRIu32" unread bytes while parsing context file\n", ndr_pull->data_size - highest_ofs);
518 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
519 printf("pull for context file returned %s\n",
520 ndr_map_error2string(ndr_err));
521 TALLOC_FREE(mem_ctx);
524 memcpy(v_st, st, f->struct_size);
527 if (filename && cmdline_input) {
528 printf("cannot combine --input with a filename\n");
529 TALLOC_FREE(mem_ctx);
531 } else if (cmdline_input) {
532 data = (const uint8_t *)cmdline_input;
533 size = strlen(cmdline_input);
534 } else if (filename) {
535 data = (uint8_t *)file_load(filename, &size, 0, mem_ctx);
537 data = (uint8_t *)stdin_load(mem_ctx, &size);
548 if (hex_input && base64_input) {
549 printf("cannot combine --hex-input with --base64-input\n");
550 TALLOC_FREE(mem_ctx);
553 } else if (hex_input && size >= 1 && data[0] != '[') {
554 blob = strhex_to_data_blob(mem_ctx, (const char *)data);
555 } else if (hex_input) {
556 blob = hexdump_to_data_blob(mem_ctx, (const char *)data, size);
557 } else if (base64_input) {
558 /* Use talloc_strndup() to ensure null termination */
559 blob = base64_decode_data_blob_talloc(
561 talloc_strndup(mem_ctx, (const char *)data, size));
563 blob = data_blob_const(data, size);
566 if (data != NULL && blob.data == NULL) {
567 printf("failed to decode input data\n");
568 TALLOC_FREE(mem_ctx);
572 ndr_pull = ndr_pull_init_blob(&blob, mem_ctx);
573 if (ndr_pull == NULL) {
574 perror("ndr_pull_init_blob");
575 TALLOC_FREE(mem_ctx);
578 ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
580 ndr_pull->flags |= LIBNDR_FLAG_NDR64;
583 ndr_print = talloc_zero(mem_ctx, struct ndr_print);
585 ndr_print->print = ndr_print_dummy;
587 ndr_print->print = ndr_print_printf_helper;
589 ndr_print->depth = 1;
591 ndr_err = ndr_pop_dcerpc_sec_verification_trailer(ndr_pull, mem_ctx, &sec_vt);
592 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
593 printf("ndr_pop_dcerpc_sec_verification_trailer returned %s\n",
594 ndr_map_error2string(ndr_err));
597 if (sec_vt != NULL && sec_vt->count.count > 0) {
598 printf("SEC_VT: consumed %zu bytes\n",
599 blob.length - ndr_pull->data_size);
601 ndrdump_data(blob.data + ndr_pull->data_size,
602 blob.length - ndr_pull->data_size,
605 ndr_print_dcerpc_sec_verification_trailer(ndr_print, "SEC_VT", sec_vt);
609 if (flags & NDR_OUT) {
610 status = ndrdump_pull_and_print_pipes(format,
614 if (!NT_STATUS_IS_OK(status)) {
615 printf("pull and dump of OUT pipes FAILED: %s\n",
617 TALLOC_FREE(mem_ctx);
622 ndr_err = f->ndr_pull(ndr_pull, flags, st);
623 printf("pull returned %s\n",
624 ndr_map_error2string(ndr_err));
626 if (ndr_pull->offset > ndr_pull->relative_highest_offset) {
627 highest_ofs = ndr_pull->offset;
629 highest_ofs = ndr_pull->relative_highest_offset;
633 printf("%"PRIu32" bytes consumed\n", highest_ofs);
634 ndrdump_data(blob.data, blob.length, dumpdata);
637 if (!print_after_parse_failure && !NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
638 TALLOC_FREE(mem_ctx);
642 if (highest_ofs != ndr_pull->data_size) {
643 printf("WARNING! %"PRIu32" unread bytes\n", ndr_pull->data_size - highest_ofs);
644 ndrdump_data(ndr_pull->data+highest_ofs,
645 ndr_pull->data_size - highest_ofs,
649 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
650 printf("WARNING: pull of %s was incomplete, "
651 "therefore the parse below may SEGFAULT\n",
655 f->ndr_print(ndr_print, f->name, flags, st);
657 if (flags & NDR_IN) {
658 status = ndrdump_pull_and_print_pipes(format,
662 if (!NT_STATUS_IS_OK(status)) {
663 printf("pull and dump of IN pipes FAILED: %s\n",
669 /* Do not proceed to validate if we got an error */
670 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
671 printf("dump of failed-to-parse %s complete\n",
673 TALLOC_FREE(mem_ctx);
679 struct ndr_push *ndr_v_push;
680 struct ndr_pull *ndr_v_pull;
681 struct ndr_print *ndr_v_print;
682 uint32_t highest_v_ofs;
684 uint8_t byte_a, byte_b;
687 ndr_v_push = ndr_push_init_ctx(mem_ctx);
688 if (ndr_v_push == NULL) {
689 printf("No memory\n");
694 ndr_v_push->flags |= LIBNDR_FLAG_NDR64;
697 ndr_err = f->ndr_push(ndr_v_push, flags, st);
698 printf("push returned %s\n",
699 ndr_map_error2string(ndr_err));
700 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
701 printf("validate push FAILED\n");
702 TALLOC_FREE(mem_ctx);
706 v_blob = ndr_push_blob(ndr_v_push);
709 printf("%zu bytes generated (validate)\n", v_blob.length);
710 ndrdump_data(v_blob.data, v_blob.length, dumpdata);
713 ndr_v_pull = ndr_pull_init_blob(&v_blob, mem_ctx);
714 if (ndr_v_pull == NULL) {
715 perror("ndr_pull_init_blob");
716 TALLOC_FREE(mem_ctx);
719 ndr_v_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
721 ndr_err = f->ndr_pull(ndr_v_pull, flags, v_st);
722 printf("pull returned %s\n",
723 ndr_map_error2string(ndr_err));
724 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
725 printf("validate pull FAILED\n");
726 TALLOC_FREE(mem_ctx);
730 if (ndr_v_pull->offset > ndr_v_pull->relative_highest_offset) {
731 highest_v_ofs = ndr_v_pull->offset;
733 highest_v_ofs = ndr_v_pull->relative_highest_offset;
736 if (highest_v_ofs != ndr_v_pull->data_size) {
737 printf("WARNING! %"PRIu32" unread bytes in validation\n",
738 ndr_v_pull->data_size - highest_v_ofs);
739 ndrdump_data(ndr_v_pull->data + highest_v_ofs,
740 ndr_v_pull->data_size - highest_v_ofs,
744 ndr_v_print = talloc_zero(mem_ctx, struct ndr_print);
745 ndr_v_print->print = ndr_print_debug_helper;
746 ndr_v_print->depth = 1;
747 f->ndr_print(ndr_v_print,
751 if (blob.length != v_blob.length) {
752 printf("WARNING! orig bytes:%zu validated pushed bytes:%zu\n",
753 blob.length, v_blob.length);
756 if (highest_ofs != highest_v_ofs) {
757 printf("WARNING! orig pulled bytes:%"PRIu32" validated pulled bytes:%"PRIu32"\n",
758 highest_ofs, highest_v_ofs);
764 for (i=0; i < blob.length; i++) {
765 byte_a = blob.data[i];
767 if (i == v_blob.length) {
773 byte_b = v_blob.data[i];
775 if (byte_a != byte_b) {
781 printf("WARNING! orig and validated differ at byte 0x%02"PRIX32" (%"PRIu32")\n", i, i);
782 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 ndrdump_data_diff(blob.data, blob.length,
785 v_blob.data, v_blob.length,
791 TALLOC_FREE(mem_ctx);