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_call *find_function(
26 const struct dcerpc_interface_table *p,
30 if (isdigit(function[0])) {
31 i = strtol(function, NULL, 0);
34 for (i=0;i<p->num_calls;i++) {
35 if (strcmp(p->calls[i].name, function) == 0) {
39 if (i == p->num_calls) {
40 printf("Function '%s' not found\n", function);
47 static void show_pipes(void)
49 const struct dcerpc_interface_list *l;
50 printf("\nYou must specify a pipe\n");
51 printf("known pipes are:\n");
52 for (l=librpc_dcerpc_pipes();l;l=l->next) {
53 if(l->table->helpstring) {
54 printf("\t%s - %s\n", l->table->name, l->table->helpstring);
56 printf("\t%s\n", l->table->name);
62 static void show_functions(const struct dcerpc_interface_table *p)
65 printf("\nYou must specify a function\n");
66 printf("known functions on '%s' are:\n", p->name);
67 for (i=0;i<p->num_calls;i++) {
68 printf("\t0x%02x (%2d) %s\n", i, i, p->calls[i].name);
73 int main(int argc, const char *argv[])
75 const struct dcerpc_interface_table *p;
76 const struct dcerpc_interface_call *f;
77 const char *pipe_name, *function, *inout, *filename;
87 const char *ctx_filename = NULL;
90 struct poptOption long_options[] = {
91 {"context-file", 'c', POPT_ARG_STRING, &ctx_filename, 0, "In-filename to parse first", "CTX-FILE" },
98 setup_logging("ndrdump", DEBUG_STDOUT);
100 ndrdump_init_subsystems;
102 pc = poptGetContext("ndrdump", argc, argv, long_options, 0);
104 poptSetOtherOptionHelp(pc, "<pipe> <function> <inout> <filename>");
106 while ((opt = poptGetNextOpt(pc)) != -1) {
109 pipe_name = poptGetArg(pc);
112 poptPrintUsage(pc, stderr, 0);
117 p = idl_iface_by_name(pipe_name);
120 printf("Unknown pipe '%s'\n", pipe_name);
124 function = poptGetArg(pc);
125 inout = poptGetArg(pc);
126 filename = poptGetArg(pc);
128 if (!function || !inout || !filename) {
129 poptPrintUsage(pc, stderr, 0);
134 if (strcmp(inout, "in") == 0 ||
135 strcmp(inout, "request") == 0) {
137 } else if (strcmp(inout, "out") == 0 ||
138 strcmp(inout, "response") == 0) {
141 printf("Bad inout value '%s'\n", inout);
145 f = find_function(p, function);
147 mem_ctx = talloc_init("ndrdump");
149 st = talloc_zero_size(mem_ctx, f->struct_size);
151 printf("Unable to allocate %d bytes\n", f->struct_size);
156 if (flags == NDR_IN) {
157 printf("Context file can only be used for \"out\" packages\n");
161 data = (uint8_t *)file_load(ctx_filename, &size);
163 perror(ctx_filename);
170 ndr = ndr_pull_init_blob(&blob, mem_ctx);
172 status = f->ndr_pull(ndr, NDR_IN, st);
174 if (ndr->offset != ndr->data_size) {
175 printf("WARNING! %d unread bytes while parsing context file\n", ndr->data_size - ndr->offset);
178 if (!NT_STATUS_IS_OK(status)) {
179 printf("pull for context file returned %s\n", nt_errstr(status));
184 data = (uint8_t *)file_load(filename, &size);
193 ndr = ndr_pull_init_blob(&blob, mem_ctx);
195 if (flags == NDR_OUT) {
196 ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
199 status = f->ndr_pull(ndr, flags, st);
201 printf("pull returned %s\n", nt_errstr(status));
203 if (ndr->offset != ndr->data_size) {
204 printf("WARNING! %d unread bytes\n", ndr->data_size - ndr->offset);
205 dump_data(0, ndr->data+ndr->offset, ndr->data_size - ndr->offset);
208 pr = talloc_p(NULL, struct ndr_print);
209 pr->print = ndr_print_debug_helper;
211 f->ndr_print(pr, function, flags, st);
213 if (!NT_STATUS_IS_OK(status) ||
214 ndr->offset != ndr->data_size) {
215 printf("dump FAILED\n");