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