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"
24 #include "system/filesys.h"
26 static const struct dcerpc_interface_call *find_function(
27 const struct dcerpc_interface_table *p,
31 if (isdigit(function[0])) {
32 i = strtol(function, NULL, 0);
35 for (i=0;i<p->num_calls;i++) {
36 if (strcmp(p->calls[i].name, function) == 0) {
40 if (i == p->num_calls) {
41 printf("Function '%s' not found\n", function);
48 static void show_pipes(void)
50 const struct dcerpc_interface_list *l;
51 printf("\nYou must specify a pipe\n");
52 printf("known pipes are:\n");
53 for (l=librpc_dcerpc_pipes();l;l=l->next) {
54 if(l->table->helpstring) {
55 printf("\t%s - %s\n", l->table->name, l->table->helpstring);
57 printf("\t%s\n", l->table->name);
63 static void show_functions(const struct dcerpc_interface_table *p)
66 printf("\nYou must specify a function\n");
67 printf("known functions on '%s' are:\n", p->name);
68 for (i=0;i<p->num_calls;i++) {
69 printf("\t0x%02x (%2d) %s\n", i, i, p->calls[i].name);
74 static char *stdin_load(TALLOC_CTX *mem_ctx, size_t *size)
76 int num_read, total_len = 0;
80 while((num_read = read(STDIN_FILENO, buf, 255)) > 0) {
83 result = (char *) talloc_realloc(
84 mem_ctx, result, char *, total_len + num_read);
86 result = talloc_size(mem_ctx, num_read);
89 memcpy(result + total_len, buf, num_read);
91 total_len += num_read;
100 int main(int argc, const char *argv[])
102 const struct dcerpc_interface_table *p;
103 const struct dcerpc_interface_call *f;
104 const char *pipe_name, *function, *inout, *filename;
108 struct ndr_pull *ndr;
114 const char *ctx_filename = NULL;
116 struct ndr_print *pr;
117 struct poptOption long_options[] = {
118 {"context-file", 'c', POPT_ARG_STRING, &ctx_filename, 0, "In-filename to parse first", "CTX-FILE" },
124 ndrdump_init_subsystems;
126 pc = poptGetContext("ndrdump", argc, argv, long_options, 0);
128 poptSetOtherOptionHelp(
129 pc, "<pipe|uuid> <function> <inout> [<filename>]");
131 while ((opt = poptGetNextOpt(pc)) != -1) {
134 pipe_name = poptGetArg(pc);
137 poptPrintUsage(pc, stderr, 0);
142 p = idl_iface_by_name(pipe_name);
146 p = idl_iface_by_uuid(pipe_name);
149 printf("Unknown pipe or UUID '%s'\n", pipe_name);
154 function = poptGetArg(pc);
155 inout = poptGetArg(pc);
156 filename = poptGetArg(pc);
158 if (!function || !inout) {
159 poptPrintUsage(pc, stderr, 0);
164 if (strcmp(inout, "in") == 0 ||
165 strcmp(inout, "request") == 0) {
167 } else if (strcmp(inout, "out") == 0 ||
168 strcmp(inout, "response") == 0) {
171 printf("Bad inout value '%s'\n", inout);
175 f = find_function(p, function);
177 mem_ctx = talloc_init("ndrdump");
179 st = talloc_zero_size(mem_ctx, f->struct_size);
181 printf("Unable to allocate %d bytes\n", (int)f->struct_size);
186 if (flags == NDR_IN) {
187 printf("Context file can only be used for \"out\" packages\n");
191 data = (uint8_t *)file_load(ctx_filename, &size, mem_ctx);
193 perror(ctx_filename);
200 ndr = ndr_pull_init_blob(&blob, mem_ctx);
201 ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
203 status = f->ndr_pull(ndr, NDR_IN, st);
205 if (ndr->offset != ndr->data_size) {
206 printf("WARNING! %d unread bytes while parsing context file\n", ndr->data_size - ndr->offset);
209 if (!NT_STATUS_IS_OK(status)) {
210 printf("pull for context file returned %s\n", nt_errstr(status));
216 data = (uint8_t *)file_load(filename, &size, mem_ctx);
218 data = (uint8_t *)stdin_load(mem_ctx, &size);
231 ndr = ndr_pull_init_blob(&blob, mem_ctx);
232 ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
234 status = f->ndr_pull(ndr, flags, st);
236 printf("pull returned %s\n", nt_errstr(status));
238 if (ndr->offset != ndr->data_size) {
239 printf("WARNING! %d unread bytes\n", ndr->data_size - ndr->offset);
240 dump_data(0, ndr->data+ndr->offset, ndr->data_size - ndr->offset);
243 pr = talloc_zero(NULL, struct ndr_print);
244 pr->print = ndr_print_debug_helper;
246 f->ndr_print(pr, function, flags, st);
248 if (!NT_STATUS_IS_OK(status) ||
249 ndr->offset != ndr->data_size) {
250 printf("dump FAILED\n");