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