s4:torture/smb2: add smb2.getinfo.normalized test
[gd/samba-autobuild/.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 #include "librpc/gen_ndr/ndr_dcerpc.h"
27 #include "lib/cmdline/popt_common.h"
28 #include "param/param.h"
29
30 static const struct ndr_interface_call *find_function(
31         const struct ndr_interface_table *p,
32         const char *function)
33 {
34         int i;
35         if (isdigit(function[0])) {
36                 i = strtol(function, NULL, 0);
37                 return &p->calls[i];
38         }
39         for (i=0;i<p->num_calls;i++) {
40                 if (strcmp(p->calls[i].name, function) == 0) {
41                         break;
42                 }
43         }
44         if (i == p->num_calls) {
45                 printf("Function '%s' not found\n", function);
46                 exit(1);
47         }
48         return &p->calls[i];
49 }
50
51 _NORETURN_ static void show_pipes(void)
52 {
53         const struct ndr_interface_list *l;
54         printf("\nYou must specify a pipe\n");
55         printf("known pipes are:\n");
56         for (l=ndr_table_list();l;l=l->next) {
57                 if(l->table->helpstring) {
58                         printf("\t%s - %s\n", l->table->name, l->table->helpstring);
59                 } else {
60                         printf("\t%s\n", l->table->name);
61                 }
62         }
63         exit(1);
64 }
65
66 _NORETURN_ static void show_functions(const struct ndr_interface_table *p)
67 {
68         int i;
69         printf("\nYou must specify a function\n");
70         printf("known functions on '%s' are:\n", p->name);
71         for (i=0;i<p->num_calls;i++) {
72                 printf("\t0x%02x (%2d) %s\n", i, i, p->calls[i].name);
73         }
74         exit(1);
75 }
76
77 static char *stdin_load(TALLOC_CTX *mem_ctx, size_t *size)
78 {
79         int num_read, total_len = 0;
80         char buf[255];
81         char *result = NULL;
82
83         while((num_read = read(STDIN_FILENO, buf, 255)) > 0) {
84
85                 if (result) {
86                         result = talloc_realloc(
87                                 mem_ctx, result, char, total_len + num_read);
88                 } else {
89                         result = talloc_array(mem_ctx, char, num_read);
90                 }
91
92                 memcpy(result + total_len, buf, num_read);
93
94                 total_len += num_read;
95         }
96
97         if (size)
98                 *size = total_len;
99
100         return result;
101 }
102
103 static const struct ndr_interface_table *load_iface_from_plugin(const char *plugin, const char *pipe_name)
104 {
105         const struct ndr_interface_table *p;
106         void *handle;
107         char *symbol;
108
109         handle = dlopen(plugin, RTLD_NOW);
110         if (handle == NULL) {
111                 printf("%s: Unable to open: %s\n", plugin, dlerror());
112                 return NULL;
113         }
114
115         symbol = talloc_asprintf(NULL, "ndr_table_%s", pipe_name);
116         p = (const struct ndr_interface_table *)dlsym(handle, symbol);
117
118         if (!p) {
119                 printf("%s: Unable to find DCE/RPC interface table for '%s': %s\n", plugin, pipe_name, dlerror());
120                 talloc_free(symbol);
121                 dlclose(handle);
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         dump_data_file(d, l, !force, stdout);
133 }
134
135 static NTSTATUS ndrdump_pull_and_print_pipes(const char *function,
136                                 struct ndr_pull *ndr_pull,
137                                 struct ndr_print *ndr_print,
138                                 const struct ndr_interface_call_pipes *pipes)
139 {
140         NTSTATUS status;
141         enum ndr_err_code ndr_err;
142         uint32_t i;
143
144         for (i=0; i < pipes->num_pipes; i++) {
145                 uint64_t idx = 0;
146                 while (true) {
147                         void *saved_mem_ctx;
148                         uint32_t *count;
149                         void *c;
150                         char *n;
151
152                         c = talloc_zero_size(ndr_pull, pipes->pipes[i].chunk_struct_size);
153                         talloc_set_name(c, "struct %s", pipes->pipes[i].name);
154                         /*
155                          * Note: the first struct member is always
156                          * 'uint32_t count;'
157                          */
158                         count = (uint32_t *)c;
159
160                         n = talloc_asprintf(c, "%s: %s[%llu]",
161                                         function, pipes->pipes[i].name,
162                                         (unsigned long long)idx);
163
164                         saved_mem_ctx = ndr_pull->current_mem_ctx;
165                         ndr_pull->current_mem_ctx = c;
166                         ndr_err = pipes->pipes[i].ndr_pull(ndr_pull, NDR_SCALARS, c);
167                         ndr_pull->current_mem_ctx = saved_mem_ctx;
168                         status = ndr_map_error2ntstatus(ndr_err);
169
170                         printf("pull returned %s\n", nt_errstr(status));
171                         if (!NT_STATUS_IS_OK(status)) {
172                                 talloc_free(c);
173                                 return status;
174                         }
175                         pipes->pipes[i].ndr_print(ndr_print, n, c);
176                         talloc_free(c);
177                         if (*count == 0) {
178                                 break;
179                         }
180                         idx++;
181                 }
182         }
183
184         return NT_STATUS_OK;
185 }
186
187 static void ndr_print_dummy(struct ndr_print *ndr, const char *format, ...)
188 {
189         /* This is here so that you can turn ndr printing off for the purposes
190            of benchmarking ndr parsing. */
191 }
192
193  int main(int argc, const char *argv[])
194 {
195         const struct ndr_interface_table *p = NULL;
196         const struct ndr_interface_call *f;
197         const char *pipe_name, *function, *inout, *filename;
198         uint8_t *data;
199         size_t size;
200         DATA_BLOB blob;
201         struct ndr_pull *ndr_pull;
202         struct ndr_print *ndr_print;
203         TALLOC_CTX *mem_ctx;
204         int flags = 0;
205         poptContext pc;
206         NTSTATUS status;
207         enum ndr_err_code ndr_err;
208         void *st;
209         void *v_st;
210         const char *ctx_filename = NULL;
211         const char *plugin = NULL;
212         bool validate = false;
213         bool dumpdata = false;
214         bool assume_ndr64 = false;
215         bool quiet = false;
216         bool hex_input = false;
217         int opt;
218         enum {OPT_CONTEXT_FILE=1000, OPT_VALIDATE, OPT_DUMP_DATA, OPT_LOAD_DSO, OPT_NDR64, OPT_QUIET, OPT_HEX_INPUT};
219         struct poptOption long_options[] = {
220                 POPT_AUTOHELP
221                 {"context-file", 'c', POPT_ARG_STRING, NULL, OPT_CONTEXT_FILE, "In-filename to parse first", "CTX-FILE" },
222                 {"validate", 0, POPT_ARG_NONE, NULL, OPT_VALIDATE, "try to validate the data", NULL },  
223                 {"dump-data", 0, POPT_ARG_NONE, NULL, OPT_DUMP_DATA, "dump the hex data", NULL },       
224                 {"load-dso", 'l', POPT_ARG_STRING, NULL, OPT_LOAD_DSO, "load from shared object file", NULL },
225                 {"ndr64", 0, POPT_ARG_NONE, NULL, OPT_NDR64, "Assume NDR64 data", NULL },
226                 {"quiet", 0, POPT_ARG_NONE, NULL, OPT_QUIET, "Don't actually dump anything", NULL },
227                 {"hex-input", 0, POPT_ARG_NONE, NULL, OPT_HEX_INPUT, "Read the input file in as a hex dump", NULL },
228                 POPT_COMMON_SAMBA
229                 POPT_COMMON_VERSION
230                 { NULL }
231         };
232         uint32_t highest_ofs;
233         struct dcerpc_sec_verification_trailer *sec_vt = NULL;
234         
235         ndr_table_init();
236
237         /* Initialise samba stuff */
238         smb_init_locale();
239
240         setlinebuf(stdout);
241
242         setup_logging("ndrdump", DEBUG_STDOUT);
243
244         pc = poptGetContext("ndrdump", argc, argv, long_options, 0);
245         
246         poptSetOtherOptionHelp(
247                 pc, "<pipe|uuid> <function> <inout> [<filename>]");
248
249         while ((opt = poptGetNextOpt(pc)) != -1) {
250                 switch (opt) {
251                 case OPT_CONTEXT_FILE:
252                         ctx_filename = poptGetOptArg(pc);
253                         break;
254                 case OPT_VALIDATE:
255                         validate = true;
256                         break;
257                 case OPT_DUMP_DATA:
258                         dumpdata = true;
259                         break;
260                 case OPT_LOAD_DSO:
261                         plugin = poptGetOptArg(pc);
262                         break;
263                 case OPT_NDR64:
264                         assume_ndr64 = true;
265                         break;
266                 case OPT_QUIET:
267                         quiet = true;
268                         break;
269                 case OPT_HEX_INPUT:
270                         hex_input = true;
271                         break;
272                 }
273         }
274
275         pipe_name = poptGetArg(pc);
276
277         if (!pipe_name) {
278                 poptPrintUsage(pc, stderr, 0);
279                 show_pipes();
280                 exit(1);
281         }
282
283         if (plugin != NULL) {
284                 p = load_iface_from_plugin(plugin, pipe_name);
285         } 
286         if (!p) {
287                 p = ndr_table_by_name(pipe_name);
288         }
289
290         if (!p) {
291                 struct GUID uuid;
292
293                 status = GUID_from_string(pipe_name, &uuid);
294
295                 if (NT_STATUS_IS_OK(status)) {
296                         p = ndr_table_by_uuid(&uuid);
297                 }
298         }
299
300         if (!p) {
301                 printf("Unknown pipe or UUID '%s'\n", pipe_name);
302                 exit(1);
303         }
304
305         function = poptGetArg(pc);
306         inout = poptGetArg(pc);
307         filename = poptGetArg(pc);
308
309         if (!function || !inout) {
310                 poptPrintUsage(pc, stderr, 0);
311                 show_functions(p);
312                 exit(1);
313         }
314
315         f = find_function(p, function);
316
317         if (strcmp(inout, "in") == 0 ||
318             strcmp(inout, "request") == 0) {
319                 flags |= NDR_IN;
320         } else if (strcmp(inout, "out") == 0 ||
321                    strcmp(inout, "response") == 0) {
322                 flags |= NDR_OUT;
323         } else {
324                 printf("Bad inout value '%s'\n", inout);
325                 exit(1);
326         }
327
328         mem_ctx = talloc_init("ndrdump");
329
330         st = talloc_zero_size(mem_ctx, f->struct_size);
331         if (!st) {
332                 printf("Unable to allocate %d bytes\n", (int)f->struct_size);
333                 exit(1);
334         }
335
336         v_st = talloc_zero_size(mem_ctx, f->struct_size);
337         if (!v_st) {
338                 printf("Unable to allocate %d bytes\n", (int)f->struct_size);
339                 exit(1);
340         }
341
342         if (ctx_filename) {
343                 if (flags & NDR_IN) {
344                         printf("Context file can only be used for \"out\" packages\n");
345                         exit(1);
346                 }
347                         
348                 data = (uint8_t *)file_load(ctx_filename, &size, 0, mem_ctx);
349                 if (!data) {
350                         perror(ctx_filename);
351                         exit(1);
352                 }
353
354                 blob.data = data;
355                 blob.length = size;
356
357                 ndr_pull = ndr_pull_init_blob(&blob, mem_ctx);
358                 if (ndr_pull == NULL) {
359                         perror("ndr_pull_init_blob");
360                         exit(1);
361                 }
362                 ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
363                 if (assume_ndr64) {
364                         ndr_pull->flags |= LIBNDR_FLAG_NDR64;
365                 }
366
367                 ndr_err = f->ndr_pull(ndr_pull, NDR_IN, st);
368
369                 if (ndr_pull->offset > ndr_pull->relative_highest_offset) {
370                         highest_ofs = ndr_pull->offset;
371                 } else {
372                         highest_ofs = ndr_pull->relative_highest_offset;
373                 }
374
375                 if (highest_ofs != ndr_pull->data_size) {
376                         printf("WARNING! %d unread bytes while parsing context file\n", ndr_pull->data_size - highest_ofs);
377                 }
378
379                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
380                         status = ndr_map_error2ntstatus(ndr_err);
381                         printf("pull for context file returned %s\n", nt_errstr(status));
382                         exit(1);
383                 }
384                 memcpy(v_st, st, f->struct_size);
385         }
386
387         if (filename)
388                 data = (uint8_t *)file_load(filename, &size, 0, mem_ctx);
389         else
390                 data = (uint8_t *)stdin_load(mem_ctx, &size);
391
392         if (!data) {
393                 if (filename)
394                         perror(filename);
395                 else
396                         perror("stdin");
397                 exit(1);
398         }
399         
400         if (hex_input) {
401                 blob = hexdump_to_data_blob(mem_ctx, (char *)data, size);
402         } else {
403                 blob.data = data;
404                 blob.length = size;
405         }
406
407         ndr_pull = ndr_pull_init_blob(&blob, mem_ctx);
408         if (ndr_pull == NULL) {
409                 perror("ndr_pull_init_blob");
410                 exit(1);
411         }
412         ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
413         if (assume_ndr64) {
414                 ndr_pull->flags |= LIBNDR_FLAG_NDR64;
415         }
416
417         ndr_print = talloc_zero(mem_ctx, struct ndr_print);
418         if (quiet) {
419                 ndr_print->print = ndr_print_dummy;
420         } else {
421                 ndr_print->print = ndr_print_printf_helper;
422         }
423         ndr_print->depth = 1;
424
425         ndr_err = ndr_pop_dcerpc_sec_verification_trailer(ndr_pull, mem_ctx, &sec_vt);
426         status = ndr_map_error2ntstatus(ndr_err);
427         if (!NT_STATUS_IS_OK(status)) {
428                 printf("ndr_pop_dcerpc_sec_verification_trailer returned %s\n",
429                        nt_errstr(status));
430         }
431
432         if (sec_vt != NULL && sec_vt->count.count > 0) {
433                 printf("SEC_VT: consumed %d bytes\n",
434                        (int)(blob.length - ndr_pull->data_size));
435                 if (dumpdata) {
436                         ndrdump_data(blob.data + ndr_pull->data_size,
437                                      blob.length - ndr_pull->data_size,
438                                      dumpdata);
439                 }
440                 ndr_print_dcerpc_sec_verification_trailer(ndr_print, "SEC_VT", sec_vt);
441         }
442         TALLOC_FREE(sec_vt);
443
444         if (flags & NDR_OUT) {
445                 status = ndrdump_pull_and_print_pipes(function, ndr_pull, ndr_print, &f->out_pipes);
446                 if (!NT_STATUS_IS_OK(status)) {
447                         printf("dump FAILED\n");
448                         exit(1);
449                 }
450         }
451
452         ndr_err = f->ndr_pull(ndr_pull, flags, st);
453         status = ndr_map_error2ntstatus(ndr_err);
454
455         printf("pull returned %s\n", nt_errstr(status));
456
457         if (ndr_pull->offset > ndr_pull->relative_highest_offset) {
458                 highest_ofs = ndr_pull->offset;
459         } else {
460                 highest_ofs = ndr_pull->relative_highest_offset;
461         }
462
463         if (highest_ofs != ndr_pull->data_size) {
464                 printf("WARNING! %d unread bytes\n", ndr_pull->data_size - highest_ofs);
465                 ndrdump_data(ndr_pull->data+highest_ofs,
466                              ndr_pull->data_size - highest_ofs,
467                              dumpdata);
468         }
469
470         if (dumpdata) {
471                 printf("%d bytes consumed\n", highest_ofs);
472                 ndrdump_data(blob.data, blob.length, dumpdata);
473         }
474
475         f->ndr_print(ndr_print, function, flags, st);
476
477         if (!NT_STATUS_IS_OK(status)) {
478                 printf("dump FAILED\n");
479                 exit(1);
480         }
481
482         if (flags & NDR_IN) {
483                 status = ndrdump_pull_and_print_pipes(function, ndr_pull, ndr_print, &f->in_pipes);
484                 if (!NT_STATUS_IS_OK(status)) {
485                         printf("dump FAILED\n");
486                         exit(1);
487                 }
488         }
489
490         if (validate) {
491                 DATA_BLOB v_blob;
492                 struct ndr_push *ndr_v_push;
493                 struct ndr_pull *ndr_v_pull;
494                 struct ndr_print *ndr_v_print;
495                 uint32_t highest_v_ofs;
496                 uint32_t i;
497                 uint8_t byte_a, byte_b;
498                 bool differ;
499
500                 ndr_v_push = ndr_push_init_ctx(mem_ctx);
501                 if (ndr_v_push == NULL) {
502                         printf("No memory\n");
503                         exit(1);
504                 }
505
506                 if (assume_ndr64) {
507                         ndr_v_push->flags |= LIBNDR_FLAG_NDR64;
508                 }
509
510                 ndr_err = f->ndr_push(ndr_v_push, flags, st);
511                 status = ndr_map_error2ntstatus(ndr_err);
512                 printf("push returned %s\n", nt_errstr(status));
513                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
514                         printf("validate push FAILED\n");
515                         exit(1);
516                 }
517
518                 v_blob = ndr_push_blob(ndr_v_push);
519
520                 if (dumpdata) {
521                         printf("%ld bytes generated (validate)\n", (long)v_blob.length);
522                         ndrdump_data(v_blob.data, v_blob.length, dumpdata);
523                 }
524
525                 ndr_v_pull = ndr_pull_init_blob(&v_blob, mem_ctx);
526                 if (ndr_v_pull == NULL) {
527                         perror("ndr_pull_init_blob");
528                         exit(1);
529                 }
530                 ndr_v_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
531
532                 ndr_err = f->ndr_pull(ndr_v_pull, flags, v_st);
533                 status = ndr_map_error2ntstatus(ndr_err);
534                 printf("pull returned %s\n", nt_errstr(status));
535                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
536                         printf("validate pull FAILED\n");
537                         exit(1);
538                 }
539
540                 if (ndr_v_pull->offset > ndr_v_pull->relative_highest_offset) {
541                         highest_v_ofs = ndr_v_pull->offset;
542                 } else {
543                         highest_v_ofs = ndr_v_pull->relative_highest_offset;
544                 }
545
546                 if (highest_v_ofs != ndr_v_pull->data_size) {
547                         printf("WARNING! %d unread bytes in validation\n",
548                                ndr_v_pull->data_size - highest_v_ofs);
549                         ndrdump_data(ndr_v_pull->data + highest_v_ofs,
550                                      ndr_v_pull->data_size - highest_v_ofs,
551                                      dumpdata);
552                 }
553
554                 ndr_v_print = talloc_zero(mem_ctx, struct ndr_print);
555                 ndr_v_print->print = ndr_print_debug_helper;
556                 ndr_v_print->depth = 1;
557                 f->ndr_print(ndr_v_print, function, flags, v_st);
558
559                 if (blob.length != v_blob.length) {
560                         printf("WARNING! orig bytes:%llu validated pushed bytes:%llu\n", 
561                                (unsigned long long)blob.length, (unsigned long long)v_blob.length);
562                 }
563
564                 if (highest_ofs != highest_v_ofs) {
565                         printf("WARNING! orig pulled bytes:%llu validated pulled bytes:%llu\n", 
566                                (unsigned long long)highest_ofs, (unsigned long long)highest_v_ofs);
567                 }
568
569                 differ = false;
570                 byte_a = 0x00;
571                 byte_b = 0x00;
572                 for (i=0; i < blob.length; i++) {
573                         byte_a = blob.data[i];
574
575                         if (i == v_blob.length) {
576                                 byte_b = 0x00;
577                                 differ = true;
578                                 break;
579                         }
580
581                         byte_b = v_blob.data[i];
582
583                         if (byte_a != byte_b) {
584                                 differ = true;
585                                 break;
586                         }
587                 }
588                 if (differ) {
589                         printf("WARNING! orig and validated differ at byte 0x%02X (%u)\n", i, i);
590                         printf("WARNING! orig byte[0x%02X] = 0x%02X validated byte[0x%02X] = 0x%02X\n",
591                                 i, byte_a, i, byte_b);
592                 }
593         }
594
595         printf("dump OK\n");
596         talloc_free(mem_ctx);
597
598         poptFreeContext(pc);
599         
600         return 0;
601 }