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