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