2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2003
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "lib/cmdline/popt_common.h"
23 #include "system/iconv.h"
25 static const struct dcerpc_interface_table *find_pipe(const char *pipe_name)
28 for (i=0;dcerpc_pipes[i];i++) {
29 if (strcmp(dcerpc_pipes[i]->name, pipe_name) == 0) {
33 if (!dcerpc_pipes[i]) {
34 printf("pipe '%s' not in table\n", pipe_name);
37 return dcerpc_pipes[i];
40 static const struct dcerpc_interface_call *find_function(
41 const struct dcerpc_interface_table *p,
45 if (isdigit(function[0])) {
46 i = strtol(function, NULL, 0);
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 static void show_pipes(void)
65 printf("\nYou must specify a pipe\n");
66 printf("known pipes are:\n");
67 for (i=0;dcerpc_pipes[i];i++) {
68 if(dcerpc_pipes[i]->helpstring) {
69 printf("\t%s - %s\n", dcerpc_pipes[i]->name, dcerpc_pipes[i]->helpstring);
71 printf("\t%s\n", dcerpc_pipes[i]->name);
77 static void show_functions(const struct dcerpc_interface_table *p)
80 printf("\nYou must specify a function\n");
81 printf("known functions on '%s' are:\n", p->name);
82 for (i=0;i<p->num_calls;i++) {
83 printf("\t0x%02x (%2d) %s\n", i, i, p->calls[i].name);
88 int main(int argc, const char *argv[])
90 const struct dcerpc_interface_table *p;
91 const struct dcerpc_interface_call *f;
92 const char *pipe_name, *function, *inout, *filename;
102 const char *ctx_filename = NULL;
104 struct ndr_print *pr;
105 struct poptOption long_options[] = {
106 {"context-file", 'c', POPT_ARG_STRING, &ctx_filename, 0, "In-filename to parse first", "CTX-FILE" },
113 setup_logging("ndrdump", DEBUG_STDOUT);
115 pc = poptGetContext("ndrdump", argc, argv, long_options, 0);
117 poptSetOtherOptionHelp(pc, "<pipe> <function> <inout> <filename>");
119 while ((opt = poptGetNextOpt(pc)) != -1) {
122 pipe_name = poptGetArg(pc);
125 poptPrintUsage(pc, stderr, 0);
130 p = find_pipe(pipe_name);
132 function = poptGetArg(pc);
133 inout = poptGetArg(pc);
134 filename = poptGetArg(pc);
136 if (!function || !inout || !filename) {
137 poptPrintUsage(pc, stderr, 0);
142 if (strcmp(inout, "in") == 0 ||
143 strcmp(inout, "request") == 0) {
145 } else if (strcmp(inout, "out") == 0 ||
146 strcmp(inout, "response") == 0) {
149 printf("Bad inout value '%s'\n", inout);
153 f = find_function(p, function);
155 mem_ctx = talloc_init("ndrdump");
157 st = talloc_zero(mem_ctx, f->struct_size);
159 printf("Unable to allocate %d bytes\n", f->struct_size);
164 if (flags == NDR_IN) {
165 printf("Context file can only be used for \"out\" packages\n");
169 data = file_load(ctx_filename, &size);
171 perror(ctx_filename);
178 ndr = ndr_pull_init_blob(&blob, mem_ctx);
180 status = f->ndr_pull(ndr, NDR_IN, st);
182 if (ndr->offset != ndr->data_size) {
183 printf("WARNING! %d unread bytes while parsing context file\n", ndr->data_size - ndr->offset);
186 if (!NT_STATUS_IS_OK(status)) {
187 printf("pull for context file returned %s\n", nt_errstr(status));
192 data = file_load(filename, &size);
201 ndr = ndr_pull_init_blob(&blob, mem_ctx);
203 if (flags == NDR_OUT) {
204 ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
207 status = f->ndr_pull(ndr, flags, st);
209 printf("pull returned %s\n", nt_errstr(status));
211 if (ndr->offset != ndr->data_size) {
212 printf("WARNING! %d unread bytes\n", ndr->data_size - ndr->offset);
213 dump_data(0, ndr->data+ndr->offset, ndr->data_size - ndr->offset);
216 pr = talloc_p(NULL, struct ndr_print);
217 pr->print = ndr_print_debug_helper;
219 f->ndr_print(pr, function, flags, st);
221 if (!NT_STATUS_IS_OK(status) ||
222 ndr->offset != ndr->data_size) {
223 printf("dump FAILED\n");