9ccc1e285910139f891574a06d16c2d7906179b6
[abartlet/samba.git/.git] / source4 / utils / ndrdump.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB torture tester
4    Copyright (C) Andrew Tridgell 2003
5    
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.
10    
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.
15    
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.
19 */
20
21 #include "includes.h"
22 #include "lib/cmdline/popt_common.h"
23 #include "system/iconv.h"
24
25 static const struct dcerpc_interface_table *find_pipe(const char *pipe_name)
26 {
27         int i;
28         for (i=0;dcerpc_pipes[i];i++) {
29                 if (strcmp(dcerpc_pipes[i]->name, pipe_name) == 0) {
30                         break;
31                 }
32         }
33         if (!dcerpc_pipes[i]) {
34                 printf("pipe '%s' not in table\n", pipe_name);
35                 exit(1);
36         }
37         return dcerpc_pipes[i];
38 }
39
40 static const struct dcerpc_interface_call *find_function(
41         const struct dcerpc_interface_table *p,
42         const char *function)
43 {
44         int i;
45         if (isdigit(function[0])) {
46                 i = strtol(function, NULL, 0);
47                 return &p->calls[i];
48         }
49         for (i=0;i<p->num_calls;i++) {
50                 if (strcmp(p->calls[i].name, function) == 0) {
51                         break;
52                 }
53         }
54         if (i == p->num_calls) {
55                 printf("Function '%s' not found\n", function);
56                 exit(1);
57         }
58         return &p->calls[i];
59 }
60
61
62 static void show_pipes(void)
63 {
64         int i;
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);
70                 } else {
71                         printf("\t%s\n", dcerpc_pipes[i]->name);
72                 }
73         }
74         exit(1);
75 }
76
77 static void show_functions(const struct dcerpc_interface_table *p)
78 {
79         int i;
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);
84         }
85         exit(1);
86 }
87
88  int main(int argc, const char *argv[])
89 {
90         const struct dcerpc_interface_table *p;
91         const struct dcerpc_interface_call *f;
92         const char *pipe_name, *function, *inout, *filename;
93         char *data;
94         size_t size;
95         DATA_BLOB blob;
96         struct ndr_pull *ndr;
97         TALLOC_CTX *mem_ctx;
98         int flags;
99         poptContext pc;
100         NTSTATUS status;
101         void *st;
102         const char *ctx_filename = NULL;
103         int opt;
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" },
107                 POPT_AUTOHELP
108                 POPT_TABLEEND
109         };
110
111         DEBUGLEVEL = 10;
112
113         setup_logging("ndrdump", DEBUG_STDOUT);
114
115         pc = poptGetContext("ndrdump", argc, argv, long_options, 0);
116         
117         poptSetOtherOptionHelp(pc, "<pipe> <function> <inout> <filename>");
118
119         while ((opt = poptGetNextOpt(pc)) != -1) {
120         }
121
122         pipe_name = poptGetArg(pc);
123
124         if (!pipe_name) {
125                 poptPrintUsage(pc, stderr, 0);
126                 show_pipes();
127                 exit(1);
128         }
129
130         p = find_pipe(pipe_name);
131
132         function = poptGetArg(pc);
133         inout = poptGetArg(pc);
134         filename = poptGetArg(pc);
135
136         if (!function || !inout || !filename) {
137                 poptPrintUsage(pc, stderr, 0);
138                 show_functions(p);
139                 exit(1);
140         }
141
142         if (strcmp(inout, "in") == 0 ||
143             strcmp(inout, "request") == 0) {
144                 flags = NDR_IN;
145         } else if (strcmp(inout, "out") == 0 ||
146                    strcmp(inout, "response") == 0) {
147                 flags = NDR_OUT;
148         } else {
149                 printf("Bad inout value '%s'\n", inout);
150                 exit(1);
151         }
152
153         f = find_function(p, function);
154
155         mem_ctx = talloc_init("ndrdump");
156
157         st = talloc_zero(mem_ctx, f->struct_size);
158         if (!st) {
159                 printf("Unable to allocate %d bytes\n", f->struct_size);
160                 exit(1);
161         }
162
163         if (ctx_filename) {
164                 if (flags == NDR_IN) {
165                         printf("Context file can only be used for \"out\" packages\n");
166                         exit(1);
167                 }
168                         
169                 data = file_load(ctx_filename, &size);
170                 if (!data) {
171                         perror(ctx_filename);
172                         exit(1);
173                 }
174
175                 blob.data = data;
176                 blob.length = size;
177
178                 ndr = ndr_pull_init_blob(&blob, mem_ctx);
179
180                 status = f->ndr_pull(ndr, NDR_IN, st);
181
182                 if (ndr->offset != ndr->data_size) {
183                         printf("WARNING! %d unread bytes while parsing context file\n", ndr->data_size - ndr->offset);
184                 }
185
186                 if (!NT_STATUS_IS_OK(status)) {
187                         printf("pull for context file returned %s\n", nt_errstr(status));
188                         exit(1);
189                 }
190         } 
191
192         data = file_load(filename, &size);
193         if (!data) {
194                 perror(filename);
195                 exit(1);
196         }
197
198         blob.data = data;
199         blob.length = size;
200
201         ndr = ndr_pull_init_blob(&blob, mem_ctx);
202
203         if (flags == NDR_OUT) {
204                 ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
205         }
206
207         status = f->ndr_pull(ndr, flags, st);
208
209         printf("pull returned %s\n", nt_errstr(status));
210
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);
214         }
215
216         pr = talloc_p(NULL, struct ndr_print);
217         pr->print = ndr_print_debug_helper;
218         pr->depth = 1;
219         f->ndr_print(pr, function, flags, st);
220
221         if (!NT_STATUS_IS_OK(status) ||
222             ndr->offset != ndr->data_size) {
223                 printf("dump FAILED\n");
224                 exit(1);
225         }
226
227         printf("dump OK\n");
228
229         talloc_free(pr);
230         
231         poptFreeContext(pc);
232         
233         return 0;
234 }