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