LCOV - code coverage report
Current view: top level - librpc/tools - ndrdump.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 225 392 57.4 %
Date: 2021-09-23 10:06:22 Functions: 4 9 44.4 %

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

Generated by: LCOV version 1.13