Use tables in Samba 3 ndrdump.
[samba.git] / librpc / tools / ndrdump.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB torture tester
4    Copyright (C) Andrew Tridgell 2003
5    Copyright (C) Jelmer Vernooij 2006
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "system/locale.h"
24 #include "librpc/ndr/libndr.h"
25 #include "librpc/ndr/ndr_table.h"
26 #if (_SAMBA_BUILD_ >= 4)
27 #include "lib/cmdline/popt_common.h"
28 #include "param/param.h"
29 #endif
30
31 static const struct ndr_interface_call *find_function(
32         const struct ndr_interface_table *p,
33         const char *function)
34 {
35         int i;
36         if (isdigit(function[0])) {
37                 i = strtol(function, NULL, 0);
38                 return &p->calls[i];
39         }
40         for (i=0;i<p->num_calls;i++) {
41                 if (strcmp(p->calls[i].name, function) == 0) {
42                         break;
43                 }
44         }
45         if (i == p->num_calls) {
46                 printf("Function '%s' not found\n", function);
47                 exit(1);
48         }
49         return &p->calls[i];
50 }
51
52 _NORETURN_ static void show_pipes(void)
53 {
54         const struct ndr_interface_list *l;
55         printf("\nYou must specify a pipe\n");
56         printf("known pipes are:\n");
57         for (l=ndr_table_list();l;l=l->next) {
58                 if(l->table->helpstring) {
59                         printf("\t%s - %s\n", l->table->name, l->table->helpstring);
60                 } else {
61                         printf("\t%s\n", l->table->name);
62                 }
63         }
64         exit(1);
65 }
66
67 _NORETURN_ static void show_functions(const struct ndr_interface_table *p)
68 {
69         int i;
70         printf("\nYou must specify a function\n");
71         printf("known functions on '%s' are:\n", p->name);
72         for (i=0;i<p->num_calls;i++) {
73                 printf("\t0x%02x (%2d) %s\n", i, i, p->calls[i].name);
74         }
75         exit(1);
76 }
77
78 static char *stdin_load(TALLOC_CTX *mem_ctx, size_t *size)
79 {
80         int num_read, total_len = 0;
81         char buf[255];
82         char *result = NULL;
83
84         while((num_read = read(STDIN_FILENO, buf, 255)) > 0) {
85
86                 if (result) {
87                         result = talloc_realloc(
88                                 mem_ctx, result, char, total_len + num_read);
89                 } else {
90                         result = talloc_array(mem_ctx, char, num_read);
91                 }
92
93                 memcpy(result + total_len, buf, num_read);
94
95                 total_len += num_read;
96         }
97
98         if (size)
99                 *size = total_len;
100
101         return result;
102 }
103
104 static const struct ndr_interface_table *load_iface_from_plugin(const char *plugin, const char *pipe_name)
105 {
106         const struct ndr_interface_table *p;
107         void *handle;
108         char *symbol;
109
110         handle = dlopen(plugin, RTLD_NOW);
111         if (handle == NULL) {
112                 printf("%s: Unable to open: %s\n", plugin, dlerror());
113                 return NULL;
114         }
115
116         symbol = talloc_asprintf(NULL, "ndr_table_%s", pipe_name);
117         p = (const struct ndr_interface_table *)dlsym(handle, symbol);
118
119         if (!p) {
120                 printf("%s: Unable to find DCE/RPC interface table for '%s': %s\n", plugin, pipe_name, dlerror());
121                 talloc_free(symbol);
122                 return NULL;
123         }
124
125         talloc_free(symbol);
126         
127         return p;
128 }
129
130 static void ndrdump_data(uint8_t *d, uint32_t l, bool force)
131 {
132         if (force) {
133                 dump_data(0, d, l);
134         } else {
135                 dump_data_skip_zeros(0, d, l);
136         }
137 }
138
139  int main(int argc, const char *argv[])
140 {
141         const struct ndr_interface_table *p = NULL;
142         const struct ndr_interface_call *f;
143         const char *pipe_name, *function, *inout, *filename;
144         uint8_t *data;
145         size_t size;
146         DATA_BLOB blob;
147         struct ndr_pull *ndr_pull;
148         struct ndr_print *ndr_print;
149         TALLOC_CTX *mem_ctx;
150         int flags;
151         poptContext pc;
152         NTSTATUS status;
153         enum ndr_err_code ndr_err;
154         void *st;
155         void *v_st;
156         const char *ctx_filename = NULL;
157         const char *plugin = NULL;
158         bool validate = false;
159         bool dumpdata = false;
160         int opt;
161         enum {OPT_CONTEXT_FILE=1000, OPT_VALIDATE, OPT_DUMP_DATA, OPT_LOAD_DSO};
162         struct poptOption long_options[] = {
163                 POPT_AUTOHELP
164                 {"context-file", 'c', POPT_ARG_STRING, NULL, OPT_CONTEXT_FILE, "In-filename to parse first", "CTX-FILE" },
165                 {"validate", 0, POPT_ARG_NONE, NULL, OPT_VALIDATE, "try to validate the data", NULL },  
166                 {"dump-data", 0, POPT_ARG_NONE, NULL, OPT_DUMP_DATA, "dump the hex data", NULL },       
167                 {"load-dso", 'l', POPT_ARG_STRING, NULL, OPT_LOAD_DSO, "load from shared object file", NULL },
168                 POPT_COMMON_SAMBA
169                 POPT_COMMON_VERSION
170                 { NULL }
171         };
172
173         ndr_table_init();
174 #if (_SAMBA_BUILD_ >= 3)
175         /* Initialise samba stuff */
176         load_case_tables();
177
178         setlinebuf(stdout);
179
180         dbf = x_stderr;
181
182         setup_logging(argv[0],True);
183 #endif
184
185         pc = poptGetContext("ndrdump", argc, argv, long_options, 0);
186         
187         poptSetOtherOptionHelp(
188                 pc, "<pipe|uuid> <function> <inout> [<filename>]");
189
190         while ((opt = poptGetNextOpt(pc)) != -1) {
191                 switch (opt) {
192                 case OPT_CONTEXT_FILE:
193                         ctx_filename = poptGetOptArg(pc);
194                         break;
195                 case OPT_VALIDATE:
196                         validate = true;
197                         break;
198                 case OPT_DUMP_DATA:
199                         dumpdata = true;
200                         break;
201                 case OPT_LOAD_DSO:
202                         plugin = poptGetOptArg(pc);
203                         break;
204                 }
205         }
206
207         pipe_name = poptGetArg(pc);
208
209         if (!pipe_name) {
210                 poptPrintUsage(pc, stderr, 0);
211                 show_pipes();
212                 exit(1);
213         }
214
215         if (plugin != NULL) {
216                 p = load_iface_from_plugin(plugin, pipe_name);
217         } 
218         if (!p) {
219                 p = ndr_table_by_name(pipe_name);
220         }
221
222         if (!p) {
223                 struct GUID uuid;
224
225                 status = GUID_from_string(pipe_name, &uuid);
226
227                 if (NT_STATUS_IS_OK(status)) {
228                         p = ndr_table_by_uuid(&uuid);
229                 }
230         }
231
232         if (!p) {
233                 printf("Unknown pipe or UUID '%s'\n", pipe_name);
234                 exit(1);
235         }
236
237         function = poptGetArg(pc);
238         inout = poptGetArg(pc);
239         filename = poptGetArg(pc);
240
241         if (!function || !inout) {
242                 poptPrintUsage(pc, stderr, 0);
243                 show_functions(p);
244                 exit(1);
245         }
246
247         if (strcmp(inout, "in") == 0 ||
248             strcmp(inout, "request") == 0) {
249                 flags = NDR_IN;
250         } else if (strcmp(inout, "out") == 0 ||
251                    strcmp(inout, "response") == 0) {
252                 flags = NDR_OUT;
253         } else {
254                 printf("Bad inout value '%s'\n", inout);
255                 exit(1);
256         }
257
258         f = find_function(p, function);
259
260         mem_ctx = talloc_init("ndrdump");
261
262         st = talloc_zero_size(mem_ctx, f->struct_size);
263         if (!st) {
264                 printf("Unable to allocate %d bytes\n", (int)f->struct_size);
265                 exit(1);
266         }
267
268         v_st = talloc_zero_size(mem_ctx, f->struct_size);
269         if (!v_st) {
270                 printf("Unable to allocate %d bytes\n", (int)f->struct_size);
271                 exit(1);
272         }
273
274         if (ctx_filename) {
275                 if (flags == NDR_IN) {
276                         printf("Context file can only be used for \"out\" packages\n");
277                         exit(1);
278                 }
279                         
280                 data = (uint8_t *)file_load(ctx_filename, &size, 0, mem_ctx);
281                 if (!data) {
282                         perror(ctx_filename);
283                         exit(1);
284                 }
285
286                 blob.data = data;
287                 blob.length = size;
288
289                 ndr_pull = ndr_pull_init_blob(&blob, mem_ctx, lp_iconv_convenience(cmdline_lp_ctx));
290                 ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
291
292                 ndr_err = f->ndr_pull(ndr_pull, NDR_IN, st);
293
294                 if (ndr_pull->offset != ndr_pull->data_size) {
295                         printf("WARNING! %d unread bytes while parsing context file\n", ndr_pull->data_size - ndr_pull->offset);
296                 }
297
298                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
299                         status = ndr_map_error2ntstatus(ndr_err);
300                         printf("pull for context file returned %s\n", nt_errstr(status));
301                         exit(1);
302                 }
303                 memcpy(v_st, st, f->struct_size);
304         } 
305
306         if (filename)
307                 data = (uint8_t *)file_load(filename, &size, 0, mem_ctx);
308         else
309                 data = (uint8_t *)stdin_load(mem_ctx, &size);
310
311         if (!data) {
312                 if (filename)
313                         perror(filename);
314                 else
315                         perror("stdin");
316                 exit(1);
317         }
318
319         blob.data = data;
320         blob.length = size;
321
322         ndr_pull = ndr_pull_init_blob(&blob, mem_ctx, lp_iconv_convenience(cmdline_lp_ctx));
323         ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
324
325         ndr_err = f->ndr_pull(ndr_pull, flags, st);
326         status = ndr_map_error2ntstatus(ndr_err);
327
328         printf("pull returned %s\n", nt_errstr(status));
329
330         if (ndr_pull->offset != ndr_pull->data_size) {
331                 printf("WARNING! %d unread bytes\n", ndr_pull->data_size - ndr_pull->offset);
332                 ndrdump_data(ndr_pull->data+ndr_pull->offset,
333                              ndr_pull->data_size - ndr_pull->offset,
334                              dumpdata);
335         }
336
337         if (dumpdata) {
338                 printf("%d bytes consumed\n", ndr_pull->offset);
339                 ndrdump_data(blob.data, blob.length, dumpdata);
340         }
341
342         ndr_print = talloc_zero(mem_ctx, struct ndr_print);
343         ndr_print->print = ndr_print_debug_helper;
344         ndr_print->depth = 1;
345         f->ndr_print(ndr_print, function, flags, st);
346
347         if (!NT_STATUS_IS_OK(status)) {
348                 printf("dump FAILED\n");
349                 exit(1);
350         }
351
352         if (validate) {
353                 DATA_BLOB v_blob;
354                 struct ndr_push *ndr_v_push;
355                 struct ndr_pull *ndr_v_pull;
356                 struct ndr_print *ndr_v_print;
357                 uint32_t i;
358                 uint8_t byte_a, byte_b;
359                 bool differ;
360
361                 ndr_v_push = ndr_push_init_ctx(mem_ctx, lp_iconv_convenience(cmdline_lp_ctx));
362                 
363                 ndr_err = f->ndr_push(ndr_v_push, flags, st);
364                 status = ndr_map_error2ntstatus(ndr_err);
365                 printf("push returned %s\n", nt_errstr(status));
366                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
367                         printf("validate push FAILED\n");
368                         exit(1);
369                 }
370
371                 v_blob = ndr_push_blob(ndr_v_push);
372
373                 if (dumpdata) {
374                         printf("%ld bytes generated (validate)\n", (long)v_blob.length);
375                         ndrdump_data(v_blob.data, v_blob.length, dumpdata);
376                 }
377
378                 ndr_v_pull = ndr_pull_init_blob(&v_blob, mem_ctx, lp_iconv_convenience(cmdline_lp_ctx));
379                 ndr_v_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
380
381                 ndr_err = f->ndr_pull(ndr_v_pull, flags, v_st);
382                 status = ndr_map_error2ntstatus(ndr_err);
383                 printf("pull returned %s\n", nt_errstr(status));
384                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
385                         printf("validate pull FAILED\n");
386                         exit(1);
387                 }
388
389
390                 if (ndr_v_pull->offset != ndr_v_pull->data_size) {
391                         printf("WARNING! %d unread bytes in validation\n", ndr_v_pull->data_size - ndr_v_pull->offset);
392                         ndrdump_data(ndr_v_pull->data+ndr_v_pull->offset,
393                                      ndr_v_pull->data_size - ndr_v_pull->offset,
394                                      dumpdata);
395                 }
396
397                 ndr_v_print = talloc_zero(mem_ctx, struct ndr_print);
398                 ndr_v_print->print = ndr_print_debug_helper;
399                 ndr_v_print->depth = 1;
400                 f->ndr_print(ndr_v_print, function, flags, v_st);
401
402                 if (blob.length != v_blob.length) {
403                         printf("WARNING! orig bytes:%llu validated pushed bytes:%llu\n", 
404                                (unsigned long long)blob.length, (unsigned long long)v_blob.length);
405                 }
406
407                 if (ndr_pull->offset != ndr_v_pull->offset) {
408                         printf("WARNING! orig pulled bytes:%llu validated pulled bytes:%llu\n", 
409                                (unsigned long long)ndr_pull->offset, (unsigned long long)ndr_v_pull->offset);
410                 }
411
412                 differ = false;
413                 byte_a = 0x00;
414                 byte_b = 0x00;
415                 for (i=0; i < blob.length; i++) {
416                         byte_a = blob.data[i];
417
418                         if (i == v_blob.length) {
419                                 byte_b = 0x00;
420                                 differ = true;
421                                 break;
422                         }
423
424                         byte_b = v_blob.data[i];
425
426                         if (byte_a != byte_b) {
427                                 differ = true;
428                                 break;
429                         }
430                 }
431                 if (differ) {
432                         printf("WARNING! orig and validated differ at byte 0x%02X (%u)\n", i, i);
433                         printf("WARNING! orig byte[0x%02X] = 0x%02X validated byte[0x%02X] = 0x%02X\n",
434                                 i, byte_a, i, byte_b);
435                 }
436         }
437
438         printf("dump OK\n");
439
440         talloc_free(mem_ctx);
441
442         poptFreeContext(pc);
443         
444         return 0;
445 }