r14542: Remove librpc, libndr and libnbt from includes.h
[ira/wip.git] / source / 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 #include "system/filesys.h"
25 #include "librpc/rpc/dcerpc.h"
26 #include "librpc/rpc/dcerpc_table.h"
27
28 static const struct dcerpc_interface_call *find_function(
29         const struct dcerpc_interface_table *p,
30         const char *function)
31 {
32         int i;
33         if (isdigit(function[0])) {
34                 i = strtol(function, NULL, 0);
35                 return &p->calls[i];
36         }
37         for (i=0;i<p->num_calls;i++) {
38                 if (strcmp(p->calls[i].name, function) == 0) {
39                         break;
40                 }
41         }
42         if (i == p->num_calls) {
43                 printf("Function '%s' not found\n", function);
44                 exit(1);
45         }
46         return &p->calls[i];
47 }
48
49
50 static void show_pipes(void)
51 {
52         const struct dcerpc_interface_list *l;
53         printf("\nYou must specify a pipe\n");
54         printf("known pipes are:\n");
55         for (l=librpc_dcerpc_pipes();l;l=l->next) {
56                 if(l->table->helpstring) {
57                         printf("\t%s - %s\n", l->table->name, l->table->helpstring);
58                 } else {
59                         printf("\t%s\n", l->table->name);
60                 }
61         }
62         exit(1);
63 }
64
65 static void show_functions(const struct dcerpc_interface_table *p)
66 {
67         int i;
68         printf("\nYou must specify a function\n");
69         printf("known functions on '%s' are:\n", p->name);
70         for (i=0;i<p->num_calls;i++) {
71                 printf("\t0x%02x (%2d) %s\n", i, i, p->calls[i].name);
72         }
73         exit(1);
74 }
75
76 static char *stdin_load(TALLOC_CTX *mem_ctx, size_t *size)
77 {
78         int num_read, total_len = 0;
79         char buf[255];
80         char *result = NULL;
81
82         while((num_read = read(STDIN_FILENO, buf, 255)) > 0) {
83
84                 if (result) {
85                         result = (char *) talloc_realloc(
86                                 mem_ctx, result, char *, total_len + num_read);
87                 } else {
88                         result = talloc_size(mem_ctx, num_read);
89                 }
90
91                 memcpy(result + total_len, buf, num_read);
92
93                 total_len += num_read;
94         }
95
96         if (size)
97                 *size = total_len;
98
99         return result;
100 }
101
102  int main(int argc, const char *argv[])
103 {
104         const struct dcerpc_interface_table *p;
105         const struct dcerpc_interface_call *f;
106         const char *pipe_name, *function, *inout, *filename;
107         uint8_t *data;
108         size_t size;
109         DATA_BLOB blob;
110         struct ndr_pull *ndr_pull;
111         struct ndr_print *ndr_print;
112         TALLOC_CTX *mem_ctx;
113         int flags;
114         poptContext pc;
115         NTSTATUS status;
116         void *st;
117         void *v_st;
118         const char *ctx_filename = NULL;
119         BOOL validate = False;
120         BOOL dumpdata = False;
121         int opt;
122         struct poptOption long_options[] = {
123                 {"context-file", 'c', POPT_ARG_STRING, &ctx_filename, 0, "In-filename to parse first", "CTX-FILE" },
124                 {"validate", 0, POPT_ARG_NONE, &validate, 0, "try to validate the data", NULL },        
125                 {"dump-data", 0, POPT_ARG_NONE, &dumpdata, 0, "dump the hex data", NULL },      
126                 POPT_COMMON_SAMBA
127                 POPT_AUTOHELP
128                 POPT_TABLEEND
129         };
130
131         dcerpc_table_init();
132
133         pc = poptGetContext("ndrdump", argc, argv, long_options, 0);
134         
135         poptSetOtherOptionHelp(
136                 pc, "<pipe|uuid> <function> <inout> [<filename>]");
137
138         while ((opt = poptGetNextOpt(pc)) != -1) {
139         }
140
141         pipe_name = poptGetArg(pc);
142
143         if (!pipe_name) {
144                 poptPrintUsage(pc, stderr, 0);
145                 show_pipes();
146                 exit(1);
147         }
148
149         p = idl_iface_by_name(pipe_name);
150
151         if (!p) {
152                 struct GUID uuid;
153
154                 status = GUID_from_string(pipe_name, &uuid);
155
156                 if (NT_STATUS_IS_OK(status)) {
157                         p = idl_iface_by_uuid(&uuid);
158                 }
159
160                 if (!p) {
161                         printf("Unknown pipe or UUID '%s'\n", pipe_name);
162                         exit(1);
163                 }
164         }
165
166         function = poptGetArg(pc);
167         inout = poptGetArg(pc);
168         filename = poptGetArg(pc);
169
170         if (!function || !inout) {
171                 poptPrintUsage(pc, stderr, 0);
172                 show_functions(p);
173                 exit(1);
174         }
175
176         if (strcmp(inout, "in") == 0 ||
177             strcmp(inout, "request") == 0) {
178                 flags = NDR_IN;
179         } else if (strcmp(inout, "out") == 0 ||
180                    strcmp(inout, "response") == 0) {
181                 flags = NDR_OUT;
182         } else {
183                 printf("Bad inout value '%s'\n", inout);
184                 exit(1);
185         }
186
187         f = find_function(p, function);
188
189         mem_ctx = talloc_init("ndrdump");
190
191         st = talloc_zero_size(mem_ctx, f->struct_size);
192         if (!st) {
193                 printf("Unable to allocate %d bytes\n", (int)f->struct_size);
194                 exit(1);
195         }
196
197         v_st = talloc_zero_size(mem_ctx, f->struct_size);
198         if (!v_st) {
199                 printf("Unable to allocate %d bytes\n", (int)f->struct_size);
200                 exit(1);
201         }
202
203         if (ctx_filename) {
204                 if (flags == NDR_IN) {
205                         printf("Context file can only be used for \"out\" packages\n");
206                         exit(1);
207                 }
208                         
209                 data = (uint8_t *)file_load(ctx_filename, &size, mem_ctx);
210                 if (!data) {
211                         perror(ctx_filename);
212                         exit(1);
213                 }
214
215                 blob.data = data;
216                 blob.length = size;
217
218                 ndr_pull = ndr_pull_init_blob(&blob, mem_ctx);
219                 ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
220
221                 status = f->ndr_pull(ndr_pull, NDR_IN, st);
222
223                 if (ndr_pull->offset != ndr_pull->data_size) {
224                         printf("WARNING! %d unread bytes while parsing context file\n", ndr_pull->data_size - ndr_pull->offset);
225                 }
226
227                 if (!NT_STATUS_IS_OK(status)) {
228                         printf("pull for context file returned %s\n", nt_errstr(status));
229                         exit(1);
230                 }
231                 memcpy(v_st, st, f->struct_size);
232         } 
233
234         if (filename)
235                 data = (uint8_t *)file_load(filename, &size, mem_ctx);
236         else
237                 data = (uint8_t *)stdin_load(mem_ctx, &size);
238
239         if (!data) {
240                 if (filename)
241                         perror(filename);
242                 else
243                         perror("stdin");
244                 exit(1);
245         }
246
247         blob.data = data;
248         blob.length = size;
249
250         ndr_pull = ndr_pull_init_blob(&blob, mem_ctx);
251         ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
252
253         status = f->ndr_pull(ndr_pull, flags, st);
254
255         printf("pull returned %s\n", nt_errstr(status));
256
257         if (ndr_pull->offset != ndr_pull->data_size) {
258                 printf("WARNING! %d unread bytes\n", ndr_pull->data_size - ndr_pull->offset);
259                 dump_data(0, ndr_pull->data+ndr_pull->offset, ndr_pull->data_size - ndr_pull->offset);
260         }
261
262         if (dumpdata) {
263                 printf("%d bytes consumed\n", ndr_pull->offset);
264                 dump_data(0, blob.data, blob.length);
265         }
266
267         ndr_print = talloc_zero(mem_ctx, struct ndr_print);
268         ndr_print->print = ndr_print_debug_helper;
269         ndr_print->depth = 1;
270         f->ndr_print(ndr_print, function, flags, st);
271
272         if (!NT_STATUS_IS_OK(status)) {
273                 printf("dump FAILED\n");
274                 exit(1);
275         }
276
277         if (validate) {
278                 DATA_BLOB v_blob;
279                 struct ndr_push *ndr_v_push;
280                 struct ndr_pull *ndr_v_pull;
281                 struct ndr_print *ndr_v_print;
282
283                 ndr_v_push = ndr_push_init_ctx(mem_ctx);
284                 
285                 status = f->ndr_push(ndr_v_push, flags, st);
286                 if (!NT_STATUS_IS_OK(status)) {
287                         printf("validate push FAILED\n");
288                         exit(1);
289                 }
290
291                 v_blob = ndr_push_blob(ndr_v_push);
292
293                 if (dumpdata) {
294                         printf("%ld bytes generated (validate)\n", (long)v_blob.length);
295                         dump_data(0, v_blob.data, v_blob.length);
296                 }
297
298                 ndr_v_pull = ndr_pull_init_blob(&v_blob, mem_ctx);
299                 ndr_v_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
300
301                 status = f->ndr_pull(ndr_v_pull, flags, v_st);
302                 if (!NT_STATUS_IS_OK(status)) {
303                         printf("validate pull FAILED\n");
304                         exit(1);
305                 }
306
307                 printf("pull returned %s\n", nt_errstr(status));
308
309                 if (ndr_v_pull->offset != ndr_v_pull->data_size) {
310                         printf("WARNING! %d unread bytes in validation\n", ndr_v_pull->data_size - ndr_v_pull->offset);
311                         dump_data(0, ndr_v_pull->data+ndr_v_pull->offset, ndr_v_pull->data_size - ndr_v_pull->offset);
312                 }
313
314                 ndr_v_print = talloc_zero(mem_ctx, struct ndr_print);
315                 ndr_v_print->print = ndr_print_debug_helper;
316                 ndr_v_print->depth = 1;
317                 f->ndr_print(ndr_v_print, function, flags, v_st);
318
319                 if (blob.length != v_blob.length) {
320                         printf("WARNING! orig bytes:%ld validated pushed bytes:%ld\n", (long)blob.length, (long)v_blob.length);
321                 }
322
323                 if (ndr_pull->offset != ndr_v_pull->offset) {
324                         printf("WARNING! orig pulled bytes:%d validated pulled bytes:%d\n", ndr_pull->offset, ndr_v_pull->offset);
325                 }
326         }
327
328         printf("dump OK\n");
329
330         talloc_free(mem_ctx);
331
332         poptFreeContext(pc);
333         
334         return 0;
335 }