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 "system/filesys.h"
23 #include "system/locale.h"
24 #include "librpc/ndr/libndr.h"
25 #include "librpc/ndr/ndr_table.h"
26 #include "librpc/gen_ndr/ndr_dcerpc.h"
27 #include "lib/cmdline/popt_common.h"
28 #include "param/param.h"
30 static const struct ndr_interface_call *find_function(
31 const struct ndr_interface_table *p,
35 if (isdigit(function[0])) {
36 i = strtol(function, NULL, 0);
39 for (i=0;i<p->num_calls;i++) {
40 if (strcmp(p->calls[i].name, function) == 0) {
44 if (i == p->num_calls) {
45 printf("Function '%s' not found\n", function);
51 _NORETURN_ static void show_pipes(void)
53 const struct ndr_interface_list *l;
54 printf("\nYou must specify a pipe\n");
55 printf("known pipes are:\n");
56 for (l=ndr_table_list();l;l=l->next) {
57 if(l->table->helpstring) {
58 printf("\t%s - %s\n", l->table->name, l->table->helpstring);
60 printf("\t%s\n", l->table->name);
66 _NORETURN_ static void show_functions(const struct ndr_interface_table *p)
69 printf("\nYou must specify a function\n");
70 printf("known functions on '%s' are:\n", p->name);
71 for (i=0;i<p->num_calls;i++) {
72 printf("\t0x%02x (%2d) %s\n", i, i, p->calls[i].name);
77 static char *stdin_load(TALLOC_CTX *mem_ctx, size_t *size)
79 int num_read, total_len = 0;
83 while((num_read = read(STDIN_FILENO, buf, 255)) > 0) {
86 result = talloc_realloc(
87 mem_ctx, result, char, total_len + num_read);
89 result = talloc_array(mem_ctx, char, num_read);
92 memcpy(result + total_len, buf, num_read);
94 total_len += num_read;
103 static const struct ndr_interface_table *load_iface_from_plugin(const char *plugin, const char *pipe_name)
105 const struct ndr_interface_table *p;
109 handle = dlopen(plugin, RTLD_NOW);
110 if (handle == NULL) {
111 printf("%s: Unable to open: %s\n", plugin, dlerror());
115 symbol = talloc_asprintf(NULL, "ndr_table_%s", pipe_name);
116 p = (const struct ndr_interface_table *)dlsym(handle, symbol);
119 printf("%s: Unable to find DCE/RPC interface table for '%s': %s\n", plugin, pipe_name, dlerror());
130 static void ndrdump_data(uint8_t *d, uint32_t l, bool force)
132 dump_data_file(d, l, !force, stdout);
135 static NTSTATUS ndrdump_pull_and_print_pipes(const char *function,
136 struct ndr_pull *ndr_pull,
137 struct ndr_print *ndr_print,
138 const struct ndr_interface_call_pipes *pipes)
141 enum ndr_err_code ndr_err;
144 for (i=0; i < pipes->num_pipes; i++) {
152 c = talloc_zero_size(ndr_pull, pipes->pipes[i].chunk_struct_size);
153 talloc_set_name(c, "struct %s", pipes->pipes[i].name);
155 * Note: the first struct member is always
158 count = (uint32_t *)c;
160 n = talloc_asprintf(c, "%s: %s[%llu]",
161 function, pipes->pipes[i].name,
162 (unsigned long long)idx);
164 saved_mem_ctx = ndr_pull->current_mem_ctx;
165 ndr_pull->current_mem_ctx = c;
166 ndr_err = pipes->pipes[i].ndr_pull(ndr_pull, NDR_SCALARS, c);
167 ndr_pull->current_mem_ctx = saved_mem_ctx;
168 status = ndr_map_error2ntstatus(ndr_err);
170 printf("pull returned %s\n", nt_errstr(status));
171 if (!NT_STATUS_IS_OK(status)) {
175 pipes->pipes[i].ndr_print(ndr_print, n, c);
187 static void ndr_print_dummy(struct ndr_print *ndr, const char *format, ...)
189 /* This is here so that you can turn ndr printing off for the purposes
190 of benchmarking ndr parsing. */
193 int main(int argc, const char *argv[])
195 const struct ndr_interface_table *p = NULL;
196 const struct ndr_interface_call *f;
197 const char *pipe_name, *function, *inout, *filename;
201 struct ndr_pull *ndr_pull;
202 struct ndr_print *ndr_print;
207 enum ndr_err_code ndr_err;
210 const char *ctx_filename = NULL;
211 const char *plugin = NULL;
212 bool validate = false;
213 bool dumpdata = false;
214 bool assume_ndr64 = false;
217 enum {OPT_CONTEXT_FILE=1000, OPT_VALIDATE, OPT_DUMP_DATA, OPT_LOAD_DSO, OPT_NDR64, OPT_QUIET};
218 struct poptOption long_options[] = {
220 {"context-file", 'c', POPT_ARG_STRING, NULL, OPT_CONTEXT_FILE, "In-filename to parse first", "CTX-FILE" },
221 {"validate", 0, POPT_ARG_NONE, NULL, OPT_VALIDATE, "try to validate the data", NULL },
222 {"dump-data", 0, POPT_ARG_NONE, NULL, OPT_DUMP_DATA, "dump the hex data", NULL },
223 {"load-dso", 'l', POPT_ARG_STRING, NULL, OPT_LOAD_DSO, "load from shared object file", NULL },
224 {"ndr64", 0, POPT_ARG_NONE, NULL, OPT_NDR64, "Assume NDR64 data", NULL },
225 {"quiet", 0, POPT_ARG_NONE, NULL, OPT_QUIET, "Don't actually dump anything", NULL },
230 const struct ndr_interface_call_pipes *in_pipes = NULL;
231 const struct ndr_interface_call_pipes *out_pipes = NULL;
232 uint32_t highest_ofs;
233 struct dcerpc_sec_verification_trailer *sec_vt = NULL;
237 /* Initialise samba stuff */
242 setup_logging("ndrdump", DEBUG_STDOUT);
244 pc = poptGetContext("ndrdump", argc, argv, long_options, 0);
246 poptSetOtherOptionHelp(
247 pc, "<pipe|uuid> <function> <inout> [<filename>]");
249 while ((opt = poptGetNextOpt(pc)) != -1) {
251 case OPT_CONTEXT_FILE:
252 ctx_filename = poptGetOptArg(pc);
261 plugin = poptGetOptArg(pc);
272 pipe_name = poptGetArg(pc);
275 poptPrintUsage(pc, stderr, 0);
280 if (plugin != NULL) {
281 p = load_iface_from_plugin(plugin, pipe_name);
284 p = ndr_table_by_name(pipe_name);
290 status = GUID_from_string(pipe_name, &uuid);
292 if (NT_STATUS_IS_OK(status)) {
293 p = ndr_table_by_uuid(&uuid);
298 printf("Unknown pipe or UUID '%s'\n", pipe_name);
302 function = poptGetArg(pc);
303 inout = poptGetArg(pc);
304 filename = poptGetArg(pc);
306 if (!function || !inout) {
307 poptPrintUsage(pc, stderr, 0);
312 f = find_function(p, function);
314 if (strcmp(inout, "in") == 0 ||
315 strcmp(inout, "request") == 0) {
317 in_pipes = &f->in_pipes;
318 } else if (strcmp(inout, "out") == 0 ||
319 strcmp(inout, "response") == 0) {
321 out_pipes = &f->out_pipes;
323 printf("Bad inout value '%s'\n", inout);
327 mem_ctx = talloc_init("ndrdump");
329 st = talloc_zero_size(mem_ctx, f->struct_size);
331 printf("Unable to allocate %d bytes\n", (int)f->struct_size);
335 v_st = talloc_zero_size(mem_ctx, f->struct_size);
337 printf("Unable to allocate %d bytes\n", (int)f->struct_size);
342 if (flags == NDR_IN) {
343 printf("Context file can only be used for \"out\" packages\n");
347 data = (uint8_t *)file_load(ctx_filename, &size, 0, mem_ctx);
349 perror(ctx_filename);
356 ndr_pull = ndr_pull_init_blob(&blob, mem_ctx);
357 if (ndr_pull == NULL) {
358 perror("ndr_pull_init_blob");
361 ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
363 ndr_pull->flags |= LIBNDR_FLAG_NDR64;
366 ndr_err = f->ndr_pull(ndr_pull, NDR_IN, st);
368 if (ndr_pull->offset > ndr_pull->relative_highest_offset) {
369 highest_ofs = ndr_pull->offset;
371 highest_ofs = ndr_pull->relative_highest_offset;
374 if (highest_ofs != ndr_pull->data_size) {
375 printf("WARNING! %d unread bytes while parsing context file\n", ndr_pull->data_size - highest_ofs);
378 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
379 status = ndr_map_error2ntstatus(ndr_err);
380 printf("pull for context file returned %s\n", nt_errstr(status));
383 memcpy(v_st, st, f->struct_size);
387 data = (uint8_t *)file_load(filename, &size, 0, mem_ctx);
389 data = (uint8_t *)stdin_load(mem_ctx, &size);
402 ndr_pull = ndr_pull_init_blob(&blob, mem_ctx);
403 if (ndr_pull == NULL) {
404 perror("ndr_pull_init_blob");
407 ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
409 ndr_pull->flags |= LIBNDR_FLAG_NDR64;
412 ndr_print = talloc_zero(mem_ctx, struct ndr_print);
414 ndr_print->print = ndr_print_dummy;
416 ndr_print->print = ndr_print_printf_helper;
418 ndr_print->depth = 1;
420 ndr_err = ndr_pop_dcerpc_sec_verification_trailer(ndr_pull, mem_ctx, &sec_vt);
421 status = ndr_map_error2ntstatus(ndr_err);
422 if (!NT_STATUS_IS_OK(status)) {
423 printf("ndr_pop_dcerpc_sec_verification_trailer returned %s\n",
427 if (sec_vt != NULL && sec_vt->count.count > 0) {
428 printf("SEC_VT: consumed %d bytes\n",
429 (int)(blob.length - ndr_pull->data_size));
431 ndrdump_data(blob.data + ndr_pull->data_size,
432 blob.length - ndr_pull->data_size,
435 ndr_print_dcerpc_sec_verification_trailer(ndr_print, "SEC_VT", sec_vt);
440 status = ndrdump_pull_and_print_pipes(function, ndr_pull, ndr_print, out_pipes);
441 if (!NT_STATUS_IS_OK(status)) {
442 printf("dump FAILED\n");
447 ndr_err = f->ndr_pull(ndr_pull, flags, st);
448 status = ndr_map_error2ntstatus(ndr_err);
450 printf("pull returned %s\n", nt_errstr(status));
452 if (ndr_pull->offset > ndr_pull->relative_highest_offset) {
453 highest_ofs = ndr_pull->offset;
455 highest_ofs = ndr_pull->relative_highest_offset;
458 if (highest_ofs != ndr_pull->data_size) {
459 printf("WARNING! %d unread bytes\n", ndr_pull->data_size - highest_ofs);
460 ndrdump_data(ndr_pull->data+highest_ofs,
461 ndr_pull->data_size - highest_ofs,
466 printf("%d bytes consumed\n", ndr_pull->offset);
467 ndrdump_data(blob.data, blob.length, dumpdata);
470 f->ndr_print(ndr_print, function, flags, st);
472 if (!NT_STATUS_IS_OK(status)) {
473 printf("dump FAILED\n");
478 status = ndrdump_pull_and_print_pipes(function, ndr_pull, ndr_print, in_pipes);
479 if (!NT_STATUS_IS_OK(status)) {
480 printf("dump FAILED\n");
487 struct ndr_push *ndr_v_push;
488 struct ndr_pull *ndr_v_pull;
489 struct ndr_print *ndr_v_print;
491 uint8_t byte_a, byte_b;
494 ndr_v_push = ndr_push_init_ctx(mem_ctx);
496 ndr_err = f->ndr_push(ndr_v_push, flags, st);
497 status = ndr_map_error2ntstatus(ndr_err);
498 printf("push returned %s\n", nt_errstr(status));
499 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
500 printf("validate push FAILED\n");
504 v_blob = ndr_push_blob(ndr_v_push);
507 printf("%ld bytes generated (validate)\n", (long)v_blob.length);
508 ndrdump_data(v_blob.data, v_blob.length, dumpdata);
511 ndr_v_pull = ndr_pull_init_blob(&v_blob, mem_ctx);
512 if (ndr_v_pull == NULL) {
513 perror("ndr_pull_init_blob");
516 ndr_v_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
518 ndr_err = f->ndr_pull(ndr_v_pull, flags, v_st);
519 status = ndr_map_error2ntstatus(ndr_err);
520 printf("pull returned %s\n", nt_errstr(status));
521 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
522 printf("validate pull FAILED\n");
527 if (ndr_v_pull->offset != ndr_v_pull->data_size) {
528 printf("WARNING! %d unread bytes in validation\n", ndr_v_pull->data_size - ndr_v_pull->offset);
529 ndrdump_data(ndr_v_pull->data+ndr_v_pull->offset,
530 ndr_v_pull->data_size - ndr_v_pull->offset,
534 ndr_v_print = talloc_zero(mem_ctx, struct ndr_print);
535 ndr_v_print->print = ndr_print_debug_helper;
536 ndr_v_print->depth = 1;
537 f->ndr_print(ndr_v_print, function, flags, v_st);
539 if (blob.length != v_blob.length) {
540 printf("WARNING! orig bytes:%llu validated pushed bytes:%llu\n",
541 (unsigned long long)blob.length, (unsigned long long)v_blob.length);
544 if (ndr_pull->offset != ndr_v_pull->offset) {
545 printf("WARNING! orig pulled bytes:%llu validated pulled bytes:%llu\n",
546 (unsigned long long)ndr_pull->offset, (unsigned long long)ndr_v_pull->offset);
552 for (i=0; i < blob.length; i++) {
553 byte_a = blob.data[i];
555 if (i == v_blob.length) {
561 byte_b = v_blob.data[i];
563 if (byte_a != byte_b) {
569 printf("WARNING! orig and validated differ at byte 0x%02X (%u)\n", i, i);
570 printf("WARNING! orig byte[0x%02X] = 0x%02X validated byte[0x%02X] = 0x%02X\n",
571 i, byte_a, i, byte_b);
577 talloc_free(mem_ctx);