lib: Give lib/util/util_file.c its own header file
[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 "lib/util/util_file.h"
23 #include "system/filesys.h"
24 #include "system/locale.h"
25 #include "librpc/ndr/libndr.h"
26 #include "librpc/ndr/ndr_table.h"
27 #include "librpc/gen_ndr/ndr_dcerpc.h"
28 #include "lib/cmdline/cmdline.h"
29 #include "param/param.h"
30 #include "lib/util/base64.h"
31
32 static const struct ndr_interface_call *find_function(
33         const struct ndr_interface_table *p,
34         const char *function)
35 {
36         unsigned int i;
37         if (isdigit(function[0])) {
38                 char *eptr = NULL;
39                 i = strtoul(function, &eptr, 0);
40                 if (i >= p->num_calls
41                     || eptr == NULL
42                     || eptr[0] != '\0') {
43                         printf("Function number '%s' not found\n",
44                                function);
45                         exit(1);
46                 }
47                 return &p->calls[i];
48         }
49         for (i=0;i<p->num_calls;i++) {
50                 if (strcmp(p->calls[i].name, function) == 0) {
51                         break;
52                 }
53         }
54         if (i == p->num_calls) {
55                 printf("Function '%s' not found\n", function);
56                 exit(1);
57         }
58         return &p->calls[i];
59 }
60
61 /*
62  * Find a public structure on the pipe and return it as if it were
63  * a function (as the rest of ndrdump is based around functions)
64  */
65 static const struct ndr_interface_call *find_struct(
66         const struct ndr_interface_table *p,
67         const char *struct_name,
68         struct ndr_interface_call *out_buffer)
69 {
70         unsigned int i;
71         const struct ndr_interface_public_struct *public_struct = NULL;
72         if (isdigit(struct_name[0])) {
73                 char *eptr = NULL;
74                 i = strtoul(struct_name, &eptr, 0);
75                 if (i >= p->num_public_structs
76                     || eptr == NULL
77                     || eptr[0] != '\0') {
78                         printf("Public structure number '%s' not found\n",
79                                struct_name);
80                         exit(1);
81                 }
82                 public_struct = &p->public_structs[i];
83         } else {
84                 for (i=0;i<p->num_public_structs;i++) {
85                         if (strcmp(p->public_structs[i].name, struct_name) == 0) {
86                                 break;
87                         }
88                 }
89                 if (i == p->num_public_structs) {
90                         printf("Public structure '%s' not found\n", struct_name);
91                         exit(1);
92                 }
93                 public_struct = &p->public_structs[i];
94         }
95         *out_buffer = (struct ndr_interface_call) {
96                 .name = public_struct->name,
97                 .struct_size = public_struct->struct_size,
98                 .ndr_pull = public_struct->ndr_pull,
99                 .ndr_push = public_struct->ndr_push,
100                 .ndr_print = public_struct->ndr_print
101         };
102         return out_buffer;
103 }
104
105 _NORETURN_ static void show_pipes(void)
106 {
107         const struct ndr_interface_list *l;
108         printf("\nYou must specify a pipe\n");
109         printf("known pipes are:\n");
110         for (l=ndr_table_list();l;l=l->next) {
111                 if(l->table->helpstring) {
112                         printf("\t%s - %s\n", l->table->name, l->table->helpstring);
113                 } else {
114                         printf("\t%s\n", l->table->name);
115                 }
116         }
117         exit(1);
118 }
119
120 _NORETURN_ static void show_functions(const struct ndr_interface_table *p)
121 {
122         int i;
123         printf("\nYou must specify a function\n");
124         printf("known functions on '%s' are:\n", p->name);
125         for (i=0;i<p->num_calls;i++) {
126                 printf("\t0x%02x (%2d) %s\n", i, i, p->calls[i].name);
127         }
128         printf("known public structures on '%s' are:\n", p->name);
129         for (i=0;i<p->num_public_structs;i++) {
130                 printf("\t%s\n", p->public_structs[i].name);
131         }
132         exit(1);
133 }
134
135 static char *stdin_load(TALLOC_CTX *mem_ctx, size_t *size)
136 {
137         int num_read, total_len = 0;
138         char buf[255];
139         char *result = NULL;
140
141         while((num_read = read(STDIN_FILENO, buf, 255)) > 0) {
142
143                 if (result) {
144                         result = talloc_realloc(
145                                 mem_ctx, result, char, total_len + num_read);
146                 } else {
147                         result = talloc_array(mem_ctx, char, num_read);
148                 }
149
150                 memcpy(result + total_len, buf, num_read);
151
152                 total_len += num_read;
153         }
154
155         if (size)
156                 *size = total_len;
157
158         return result;
159 }
160
161 static const struct ndr_interface_table *load_iface_from_plugin(const char *plugin, const char *pipe_name)
162 {
163         const struct ndr_interface_table *p;
164         void *handle;
165         char *symbol;
166
167         handle = dlopen(plugin, RTLD_NOW);
168         if (handle == NULL) {
169                 printf("%s: Unable to open: %s\n", plugin, dlerror());
170                 return NULL;
171         }
172
173         symbol = talloc_asprintf(NULL, "ndr_table_%s", pipe_name);
174         p = (const struct ndr_interface_table *)dlsym(handle, symbol);
175
176         if (!p) {
177                 printf("%s: Unable to find DCE/RPC interface table for '%s': %s\n", plugin, pipe_name, dlerror());
178                 talloc_free(symbol);
179                 dlclose(handle);
180                 return NULL;
181         }
182
183         talloc_free(symbol);
184
185         return p;
186 }
187
188 static void ndrdump_data(uint8_t *d, uint32_t l, bool force)
189 {
190         dump_data_file(d, l, !force, stdout);
191 }
192
193 static void ndrdump_data_diff(const uint8_t *d1, size_t l1,
194                               const uint8_t *d2, size_t l2,
195                               bool force)
196 {
197         dump_data_file_diff(stdout, !force, d1, l1, d2, l2);
198 }
199
200 static NTSTATUS ndrdump_pull_and_print_pipes(const char *function,
201                                 struct ndr_pull *ndr_pull,
202                                 struct ndr_print *ndr_print,
203                                 const struct ndr_interface_call_pipes *pipes)
204 {
205         enum ndr_err_code ndr_err;
206         uint32_t i;
207
208         for (i=0; i < pipes->num_pipes; i++) {
209                 uint64_t idx = 0;
210                 while (true) {
211                         void *saved_mem_ctx;
212                         uint32_t *count;
213                         void *c;
214                         char *n;
215
216                         c = talloc_zero_size(ndr_pull, pipes->pipes[i].chunk_struct_size);
217                         talloc_set_name(c, "struct %s", pipes->pipes[i].name);
218                         /*
219                          * Note: the first struct member is always
220                          * 'uint32_t count;'
221                          */
222                         count = (uint32_t *)c;
223
224                         n = talloc_asprintf(c, "%s: %s[%"PRIu64"]",
225                                         function, pipes->pipes[i].name,
226                                         idx);
227
228                         saved_mem_ctx = ndr_pull->current_mem_ctx;
229                         ndr_pull->current_mem_ctx = c;
230                         ndr_err = pipes->pipes[i].ndr_pull(ndr_pull, NDR_SCALARS, c);
231                         ndr_pull->current_mem_ctx = saved_mem_ctx;
232
233                         printf("pull returned %s\n",
234                                ndr_map_error2string(ndr_err));
235                         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
236                                 talloc_free(c);
237                                 return ndr_map_error2ntstatus(ndr_err);
238                         }
239                         pipes->pipes[i].ndr_print(ndr_print, n, c);
240                         if (*count == 0) {
241                                 talloc_free(c);
242                                 break;
243                         }
244                         talloc_free(c);
245                         idx++;
246                 }
247         }
248
249         return NT_STATUS_OK;
250 }
251
252 static void ndr_print_dummy(struct ndr_print *ndr, const char *format, ...)
253 {
254         /* This is here so that you can turn ndr printing off for the purposes
255            of benchmarking ndr parsing. */
256 }
257
258  int main(int argc, const char *argv[])
259 {
260         const struct ndr_interface_table *p = NULL;
261         const struct ndr_interface_call *f;
262         struct ndr_interface_call f_buffer;
263         const char *pipe_name = NULL;
264         const char *filename = NULL;
265         /*
266          * The format type:
267          *   in:     a request
268          *   out:    a response
269          *   struct: a public structure
270          */
271         const char *type = NULL;
272         /*
273          * Format is either the name of the decoding function or the
274          * name of a public structure
275          */
276         const char *format = NULL;
277         const char *cmdline_input = NULL;
278         const uint8_t *data;
279         size_t size;
280         DATA_BLOB blob;
281         struct ndr_pull *ndr_pull;
282         struct ndr_print *ndr_print;
283         TALLOC_CTX *mem_ctx;
284         ndr_flags_type flags = 0;
285         poptContext pc;
286         NTSTATUS status;
287         enum ndr_err_code ndr_err;
288         void *st;
289         void *v_st;
290         const char *ctx_filename = NULL;
291         const char *plugin = NULL;
292         bool validate = false;
293         bool dumpdata = false;
294         bool assume_ndr64 = false;
295         bool quiet = false;
296         bool hex_input = false;
297         bool base64_input = false;
298         bool print_after_parse_failure = false;
299         int opt;
300         enum {
301                 OPT_CONTEXT_FILE=1000,
302                 OPT_VALIDATE,
303                 OPT_DUMP_DATA,
304                 OPT_LOAD_DSO,
305                 OPT_NDR64,
306                 OPT_QUIET,
307                 OPT_BASE64_INPUT,
308                 OPT_HEX_INPUT,
309                 OPT_CMDLINE_INPUT,
310                 OPT_PRINT_AFTER_PARSE_FAILURE,
311         };
312         struct poptOption long_options[] = {
313                 POPT_AUTOHELP
314                 {"context-file", 'c', POPT_ARG_STRING, NULL, OPT_CONTEXT_FILE, "In-filename to parse first", "CTX-FILE" },
315                 {"validate", 0, POPT_ARG_NONE, NULL, OPT_VALIDATE, "try to validate the data", NULL },
316                 {"dump-data", 0, POPT_ARG_NONE, NULL, OPT_DUMP_DATA, "dump the hex data", NULL },
317                 {"load-dso", 0, POPT_ARG_STRING, NULL, OPT_LOAD_DSO, "load from shared object file", NULL },
318                 {"ndr64", 0, POPT_ARG_NONE, NULL, OPT_NDR64, "Assume NDR64 data", NULL },
319                 {"quiet", 0, POPT_ARG_NONE, NULL, OPT_QUIET, "Don't actually dump anything", NULL },
320                 {"base64-input", 0, POPT_ARG_NONE, NULL, OPT_BASE64_INPUT, "Read the input file in as a base64 string", NULL },
321                 {"hex-input", 0, POPT_ARG_NONE, NULL, OPT_HEX_INPUT, "Read the input file in as a hex dump", NULL },
322                 {"input", 0, POPT_ARG_STRING, NULL, OPT_CMDLINE_INPUT, "Provide the input on the command line (use with --base64-input)", "INPUT" },
323                 {"print-after-parse-failure", 0, POPT_ARG_NONE, NULL, OPT_PRINT_AFTER_PARSE_FAILURE,
324                  "Try to print structures that fail to parse (used to develop parsers, segfaults are likely).", NULL },
325                 POPT_COMMON_SAMBA
326                 POPT_COMMON_VERSION
327                 POPT_TABLEEND
328         };
329         uint32_t highest_ofs;
330         struct dcerpc_sec_verification_trailer *sec_vt = NULL;
331         bool ok;
332
333         ndr_table_init();
334
335         /* Initialise samba stuff */
336         smb_init_locale();
337
338         setlinebuf(stdout);
339
340         mem_ctx = talloc_init("ndrdump.c/main");
341         if (mem_ctx == NULL) {
342                 exit(ENOMEM);
343         }
344
345         ok = samba_cmdline_init(mem_ctx,
346                                 SAMBA_CMDLINE_CONFIG_CLIENT,
347                                 false /* require_smbconf */);
348         if (!ok) {
349                 DBG_ERR("Failed to init cmdline parser!\n");
350                 TALLOC_FREE(mem_ctx);
351                 exit(1);
352         }
353
354         pc = samba_popt_get_context(getprogname(),
355                                     argc,
356                                     argv,
357                                     long_options,
358                                     0);
359         if (pc == NULL) {
360                 DBG_ERR("Failed to setup popt context!\n");
361                 TALLOC_FREE(mem_ctx);
362                 exit(1);
363         }
364
365         poptSetOtherOptionHelp(
366                 pc, "<pipe|uuid> <format> <in|out|struct> [<filename>]");
367
368         while ((opt = poptGetNextOpt(pc)) != -1) {
369                 switch (opt) {
370                 case OPT_CONTEXT_FILE:
371                         ctx_filename = poptGetOptArg(pc);
372                         break;
373                 case OPT_VALIDATE:
374                         validate = true;
375                         break;
376                 case OPT_DUMP_DATA:
377                         dumpdata = true;
378                         break;
379                 case OPT_LOAD_DSO:
380                         plugin = poptGetOptArg(pc);
381                         break;
382                 case OPT_NDR64:
383                         assume_ndr64 = true;
384                         break;
385                 case OPT_QUIET:
386                         quiet = true;
387                         break;
388                 case OPT_BASE64_INPUT:
389                         base64_input = true;
390                         break;
391                 case OPT_HEX_INPUT:
392                         hex_input = true;
393                         break;
394                 case OPT_CMDLINE_INPUT:
395                         cmdline_input = poptGetOptArg(pc);
396                         break;
397                 case OPT_PRINT_AFTER_PARSE_FAILURE:
398                         print_after_parse_failure = true;
399                         break;
400                 }
401         }
402
403         pipe_name = poptGetArg(pc);
404
405         if (!pipe_name) {
406                 poptPrintUsage(pc, stderr, 0);
407                 show_pipes();
408                 exit(1);
409         }
410
411         if (plugin != NULL) {
412                 p = load_iface_from_plugin(plugin, pipe_name);
413         }
414         if (!p) {
415                 p = ndr_table_by_name(pipe_name);
416         }
417
418         if (!p) {
419                 struct GUID uuid;
420
421                 status = GUID_from_string(pipe_name, &uuid);
422
423                 if (NT_STATUS_IS_OK(status)) {
424                         p = ndr_table_by_uuid(&uuid);
425                 }
426         }
427
428         if (!p) {
429                 printf("Unknown pipe or UUID '%s'\n", pipe_name);
430                 exit(1);
431         }
432
433         format = poptGetArg(pc);
434         type = poptGetArg(pc);
435         filename = poptGetArg(pc);
436
437         if (!format || !type) {
438                 poptPrintUsage(pc, stderr, 0);
439                 show_functions(p);
440                 exit(1);
441         }
442
443         if (strcmp(type, "struct") == 0) {
444                 flags = NDR_SCALARS|NDR_BUFFERS; /* neither NDR_IN nor NDR_OUT */
445                 f = find_struct(p, format, &f_buffer);
446         } else {
447                 f = find_function(p, format);
448                 if (strcmp(type, "in") == 0 ||
449                     strcmp(type, "request") == 0) {
450                         flags |= NDR_IN;
451                 } else if (strcmp(type, "out") == 0 ||
452                            strcmp(type, "response") == 0) {
453                         flags |= NDR_OUT;
454                 } else {
455                         printf("Bad type value '%s'\n", type);
456                         exit(1);
457                 }
458         }
459
460         st = talloc_zero_size(mem_ctx, f->struct_size);
461         if (!st) {
462                 printf("Unable to allocate %zu bytes for %s structure\n",
463                        f->struct_size,
464                        f->name);
465                 TALLOC_FREE(mem_ctx);
466                 exit(1);
467         }
468
469         v_st = talloc_zero_size(mem_ctx, f->struct_size);
470         if (!v_st) {
471                 printf("Unable to allocate %zu bytes for %s validation "
472                        "structure\n",
473                        f->struct_size,
474                        f->name);
475                 TALLOC_FREE(mem_ctx);
476                 exit(1);
477         }
478
479         if (ctx_filename) {
480                 if (flags & NDR_IN) {
481                         printf("Context file can only be used for \"out\" packages\n");
482                         TALLOC_FREE(mem_ctx);
483                         exit(1);
484                 }
485
486                 data = (uint8_t *)file_load(ctx_filename, &size, 0, mem_ctx);
487                 if (!data) {
488                         perror(ctx_filename);
489                         TALLOC_FREE(mem_ctx);
490                         exit(1);
491                 }
492
493                 blob = data_blob_const(data, size);
494
495                 ndr_pull = ndr_pull_init_blob(&blob, mem_ctx);
496                 if (ndr_pull == NULL) {
497                         perror("ndr_pull_init_blob");
498                         TALLOC_FREE(mem_ctx);
499                         exit(1);
500                 }
501                 ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
502                 if (assume_ndr64) {
503                         ndr_pull->flags |= LIBNDR_FLAG_NDR64;
504                 }
505
506                 ndr_err = f->ndr_pull(ndr_pull, NDR_IN, st);
507
508                 if (ndr_pull->offset > ndr_pull->relative_highest_offset) {
509                         highest_ofs = ndr_pull->offset;
510                 } else {
511                         highest_ofs = ndr_pull->relative_highest_offset;
512                 }
513
514                 if (highest_ofs != ndr_pull->data_size) {
515                         printf("WARNING! %"PRIu32" unread bytes while parsing context file\n", ndr_pull->data_size - highest_ofs);
516                 }
517
518                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
519                         printf("pull for context file returned %s\n",
520                                ndr_map_error2string(ndr_err));
521                         TALLOC_FREE(mem_ctx);
522                         exit(2);
523                 }
524                 memcpy(v_st, st, f->struct_size);
525         }
526
527         if (filename && cmdline_input) {
528                 printf("cannot combine --input with a filename\n");
529                 TALLOC_FREE(mem_ctx);
530                 exit(1);
531         } else if (cmdline_input) {
532                 data = (const uint8_t *)cmdline_input;
533                 size = strlen(cmdline_input);
534         } else if (filename) {
535                 data = (uint8_t *)file_load(filename, &size, 0, mem_ctx);
536         } else {
537                 data = (uint8_t *)stdin_load(mem_ctx, &size);
538         }
539
540         if (!data) {
541                 if (filename)
542                         perror(filename);
543                 else
544                         perror("stdin");
545                 exit(1);
546         }
547
548         if (hex_input && base64_input) {
549                 printf("cannot combine --hex-input with --base64-input\n");
550                 TALLOC_FREE(mem_ctx);
551                 exit(1);
552
553         } else if (hex_input && size >= 1 && data[0] != '[') {
554                 blob = strhex_to_data_blob(mem_ctx, (const char *)data);
555         } else if (hex_input) {
556                 blob = hexdump_to_data_blob(mem_ctx, (const char *)data, size);
557         } else if (base64_input) {
558                 /* Use talloc_strndup() to ensure null termination */
559                 blob = base64_decode_data_blob_talloc(
560                         mem_ctx,
561                         talloc_strndup(mem_ctx, (const char *)data, size));
562         } else {
563                 blob = data_blob_const(data, size);
564         }
565
566         if (data != NULL && blob.data == NULL) {
567                 printf("failed to decode input data\n");
568                 TALLOC_FREE(mem_ctx);
569                 exit(1);
570         }
571
572         ndr_pull = ndr_pull_init_blob(&blob, mem_ctx);
573         if (ndr_pull == NULL) {
574                 perror("ndr_pull_init_blob");
575                 TALLOC_FREE(mem_ctx);
576                 exit(1);
577         }
578         ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
579         if (assume_ndr64) {
580                 ndr_pull->flags |= LIBNDR_FLAG_NDR64;
581         }
582
583         ndr_print = talloc_zero(mem_ctx, struct ndr_print);
584         if (quiet) {
585                 ndr_print->print = ndr_print_dummy;
586         } else {
587                 ndr_print->print = ndr_print_printf_helper;
588         }
589         ndr_print->depth = 1;
590
591         ndr_err = ndr_pop_dcerpc_sec_verification_trailer(ndr_pull, mem_ctx, &sec_vt);
592         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
593                 printf("ndr_pop_dcerpc_sec_verification_trailer returned %s\n",
594                        ndr_map_error2string(ndr_err));
595         }
596
597         if (sec_vt != NULL && sec_vt->count.count > 0) {
598                 printf("SEC_VT: consumed %zu bytes\n",
599                        blob.length - ndr_pull->data_size);
600                 if (dumpdata) {
601                         ndrdump_data(blob.data + ndr_pull->data_size,
602                                      blob.length - ndr_pull->data_size,
603                                      dumpdata);
604                 }
605                 ndr_print_dcerpc_sec_verification_trailer(ndr_print, "SEC_VT", sec_vt);
606         }
607         TALLOC_FREE(sec_vt);
608
609         if (flags & NDR_OUT) {
610                 status = ndrdump_pull_and_print_pipes(format,
611                                                       ndr_pull,
612                                                       ndr_print,
613                                                       &f->out_pipes);
614                 if (!NT_STATUS_IS_OK(status)) {
615                         printf("pull and dump of OUT pipes FAILED: %s\n",
616                                nt_errstr(status));
617                         TALLOC_FREE(mem_ctx);
618                         exit(2);
619                 }
620         }
621
622         ndr_err = f->ndr_pull(ndr_pull, flags, st);
623         printf("pull returned %s\n",
624                ndr_map_error2string(ndr_err));
625
626         if (ndr_pull->offset > ndr_pull->relative_highest_offset) {
627                 highest_ofs = ndr_pull->offset;
628         } else {
629                 highest_ofs = ndr_pull->relative_highest_offset;
630         }
631
632         if (dumpdata) {
633                 printf("%"PRIu32" bytes consumed\n", highest_ofs);
634                 ndrdump_data(blob.data, blob.length, dumpdata);
635         }
636
637         if (!print_after_parse_failure && !NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
638                 TALLOC_FREE(mem_ctx);
639                 exit(2);
640         }
641
642         if (highest_ofs != ndr_pull->data_size) {
643                 printf("WARNING! %"PRIu32" unread bytes\n", ndr_pull->data_size - highest_ofs);
644                 ndrdump_data(ndr_pull->data+highest_ofs,
645                              ndr_pull->data_size - highest_ofs,
646                              dumpdata);
647         }
648
649         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
650                 printf("WARNING: pull of %s was incomplete, "
651                        "therefore the parse below may SEGFAULT\n",
652                         f->name);
653         }
654
655         f->ndr_print(ndr_print, f->name, flags, st);
656
657         if (flags & NDR_IN) {
658                 status = ndrdump_pull_and_print_pipes(format,
659                                                       ndr_pull,
660                                                       ndr_print,
661                                                       &f->in_pipes);
662                 if (!NT_STATUS_IS_OK(status)) {
663                         printf("pull and dump of IN pipes FAILED: %s\n",
664                                nt_errstr(status));
665                         exit(1);
666                 }
667         }
668
669         /* Do not proceed to validate if we got an error */
670         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
671                 printf("dump of failed-to-parse %s complete\n",
672                        f->name);
673                 TALLOC_FREE(mem_ctx);
674                 exit(2);
675         }
676
677         if (validate) {
678                 DATA_BLOB v_blob;
679                 struct ndr_push *ndr_v_push;
680                 struct ndr_pull *ndr_v_pull;
681                 struct ndr_print *ndr_v_print;
682                 uint32_t highest_v_ofs;
683                 uint32_t i;
684                 uint8_t byte_a, byte_b;
685                 bool differ;
686
687                 ndr_v_push = ndr_push_init_ctx(mem_ctx);
688                 if (ndr_v_push == NULL) {
689                         printf("No memory\n");
690                         exit(1);
691                 }
692
693                 if (assume_ndr64) {
694                         ndr_v_push->flags |= LIBNDR_FLAG_NDR64;
695                 }
696
697                 ndr_err = f->ndr_push(ndr_v_push, flags, st);
698                 printf("push returned %s\n",
699                        ndr_map_error2string(ndr_err));
700                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
701                         printf("validate push FAILED\n");
702                         TALLOC_FREE(mem_ctx);
703                         exit(1);
704                 }
705
706                 v_blob = ndr_push_blob(ndr_v_push);
707
708                 if (dumpdata) {
709                         printf("%zu bytes generated (validate)\n", v_blob.length);
710                         ndrdump_data(v_blob.data, v_blob.length, dumpdata);
711                 }
712
713                 ndr_v_pull = ndr_pull_init_blob(&v_blob, mem_ctx);
714                 if (ndr_v_pull == NULL) {
715                         perror("ndr_pull_init_blob");
716                         TALLOC_FREE(mem_ctx);
717                         exit(1);
718                 }
719                 ndr_v_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
720
721                 ndr_err = f->ndr_pull(ndr_v_pull, flags, v_st);
722                 printf("pull returned %s\n",
723                        ndr_map_error2string(ndr_err));
724                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
725                         printf("validate pull FAILED\n");
726                         TALLOC_FREE(mem_ctx);
727                         exit(1);
728                 }
729
730                 if (ndr_v_pull->offset > ndr_v_pull->relative_highest_offset) {
731                         highest_v_ofs = ndr_v_pull->offset;
732                 } else {
733                         highest_v_ofs = ndr_v_pull->relative_highest_offset;
734                 }
735
736                 if (highest_v_ofs != ndr_v_pull->data_size) {
737                         printf("WARNING! %"PRIu32" unread bytes in validation\n",
738                                ndr_v_pull->data_size - highest_v_ofs);
739                         ndrdump_data(ndr_v_pull->data + highest_v_ofs,
740                                      ndr_v_pull->data_size - highest_v_ofs,
741                                      dumpdata);
742                 }
743
744                 ndr_v_print = talloc_zero(mem_ctx, struct ndr_print);
745                 ndr_v_print->print = ndr_print_debug_helper;
746                 ndr_v_print->depth = 1;
747                 f->ndr_print(ndr_v_print,
748                              format,
749                              flags, v_st);
750
751                 if (blob.length != v_blob.length) {
752                         printf("WARNING! orig bytes:%zu validated pushed bytes:%zu\n",
753                                blob.length, v_blob.length);
754                 }
755
756                 if (highest_ofs != highest_v_ofs) {
757                         printf("WARNING! orig pulled bytes:%"PRIu32" validated pulled bytes:%"PRIu32"\n",
758                                highest_ofs, highest_v_ofs);
759                 }
760
761                 differ = false;
762                 byte_a = 0x00;
763                 byte_b = 0x00;
764                 for (i=0; i < blob.length; i++) {
765                         byte_a = blob.data[i];
766
767                         if (i == v_blob.length) {
768                                 byte_b = 0x00;
769                                 differ = true;
770                                 break;
771                         }
772
773                         byte_b = v_blob.data[i];
774
775                         if (byte_a != byte_b) {
776                                 differ = true;
777                                 break;
778                         }
779                 }
780                 if (differ) {
781                         printf("WARNING! orig and validated differ at byte 0x%02"PRIX32" (%"PRIu32")\n", i, i);
782                         printf("WARNING! orig byte[0x%02"PRIX32"] = 0x%02"PRIX8" validated byte[0x%02"PRIX32"] = 0x%02"PRIX8"\n",
783                                 i, byte_a, i, byte_b);
784                         ndrdump_data_diff(blob.data, blob.length,
785                                           v_blob.data, v_blob.length,
786                                           dumpdata);
787                 }
788         }
789
790         printf("dump OK\n");
791         TALLOC_FREE(mem_ctx);
792
793         poptFreeContext(pc);
794
795         return 0;
796 }