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