debug: Add GPFS tracing backend
[Samba.git] / librpc / tools / ndrdump.c
blobf7aac4e11127a5a960cbdb1f6eb6a371ccffad8f
1 /*
2 Unix SMB/CIFS implementation.
3 SMB torture tester
4 Copyright (C) Andrew Tridgell 2003
5 Copyright (C) Jelmer Vernooij 2006
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.
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.
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/>.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "system/locale.h"
24 #include "librpc/ndr/libndr.h"
25 #include "librpc/ndr/ndr_table.h"
26 #include "librpc/gen_ndr/ndr_dcerpc.h"
27 #include "lib/cmdline/popt_common.h"
28 #include "param/param.h"
30 static const struct ndr_interface_call *find_function(
31 const struct ndr_interface_table *p,
32 const char *function)
34 int i;
35 if (isdigit(function[0])) {
36 i = strtol(function, NULL, 0);
37 return &p->calls[i];
39 for (i=0;i<p->num_calls;i++) {
40 if (strcmp(p->calls[i].name, function) == 0) {
41 break;
44 if (i == p->num_calls) {
45 printf("Function '%s' not found\n", function);
46 exit(1);
48 return &p->calls[i];
51 _NORETURN_ static void show_pipes(void)
53 const struct ndr_interface_list *l;
54 printf("\nYou must specify a pipe\n");
55 printf("known pipes are:\n");
56 for (l=ndr_table_list();l;l=l->next) {
57 if(l->table->helpstring) {
58 printf("\t%s - %s\n", l->table->name, l->table->helpstring);
59 } else {
60 printf("\t%s\n", l->table->name);
63 exit(1);
66 _NORETURN_ static void show_functions(const struct ndr_interface_table *p)
68 int i;
69 printf("\nYou must specify a function\n");
70 printf("known functions on '%s' are:\n", p->name);
71 for (i=0;i<p->num_calls;i++) {
72 printf("\t0x%02x (%2d) %s\n", i, i, p->calls[i].name);
74 exit(1);
77 static char *stdin_load(TALLOC_CTX *mem_ctx, size_t *size)
79 int num_read, total_len = 0;
80 char buf[255];
81 char *result = NULL;
83 while((num_read = read(STDIN_FILENO, buf, 255)) > 0) {
85 if (result) {
86 result = talloc_realloc(
87 mem_ctx, result, char, total_len + num_read);
88 } else {
89 result = talloc_array(mem_ctx, char, num_read);
92 memcpy(result + total_len, buf, num_read);
94 total_len += num_read;
97 if (size)
98 *size = total_len;
100 return result;
103 static const struct ndr_interface_table *load_iface_from_plugin(const char *plugin, const char *pipe_name)
105 const struct ndr_interface_table *p;
106 void *handle;
107 char *symbol;
109 handle = dlopen(plugin, RTLD_NOW);
110 if (handle == NULL) {
111 printf("%s: Unable to open: %s\n", plugin, dlerror());
112 return NULL;
115 symbol = talloc_asprintf(NULL, "ndr_table_%s", pipe_name);
116 p = (const struct ndr_interface_table *)dlsym(handle, symbol);
118 if (!p) {
119 printf("%s: Unable to find DCE/RPC interface table for '%s': %s\n", plugin, pipe_name, dlerror());
120 talloc_free(symbol);
121 dlclose(handle);
122 return NULL;
125 talloc_free(symbol);
127 return p;
130 static void ndrdump_data(uint8_t *d, uint32_t l, bool force)
132 dump_data_file(d, l, !force, stdout);
135 static NTSTATUS ndrdump_pull_and_print_pipes(const char *function,
136 struct ndr_pull *ndr_pull,
137 struct ndr_print *ndr_print,
138 const struct ndr_interface_call_pipes *pipes)
140 NTSTATUS status;
141 enum ndr_err_code ndr_err;
142 uint32_t i;
144 for (i=0; i < pipes->num_pipes; i++) {
145 uint64_t idx = 0;
146 while (true) {
147 void *saved_mem_ctx;
148 uint32_t *count;
149 void *c;
150 char *n;
152 c = talloc_zero_size(ndr_pull, pipes->pipes[i].chunk_struct_size);
153 talloc_set_name(c, "struct %s", pipes->pipes[i].name);
155 * Note: the first struct member is always
156 * 'uint32_t count;'
158 count = (uint32_t *)c;
160 n = talloc_asprintf(c, "%s: %s[%llu]",
161 function, pipes->pipes[i].name,
162 (unsigned long long)idx);
164 saved_mem_ctx = ndr_pull->current_mem_ctx;
165 ndr_pull->current_mem_ctx = c;
166 ndr_err = pipes->pipes[i].ndr_pull(ndr_pull, NDR_SCALARS, c);
167 ndr_pull->current_mem_ctx = saved_mem_ctx;
168 status = ndr_map_error2ntstatus(ndr_err);
170 printf("pull returned %s\n", nt_errstr(status));
171 if (!NT_STATUS_IS_OK(status)) {
172 talloc_free(c);
173 return status;
175 pipes->pipes[i].ndr_print(ndr_print, n, c);
176 talloc_free(c);
177 if (*count == 0) {
178 break;
180 idx++;
184 return NT_STATUS_OK;
187 int main(int argc, const char *argv[])
189 const struct ndr_interface_table *p = NULL;
190 const struct ndr_interface_call *f;
191 const char *pipe_name, *function, *inout, *filename;
192 uint8_t *data;
193 size_t size;
194 DATA_BLOB blob;
195 struct ndr_pull *ndr_pull;
196 struct ndr_print *ndr_print;
197 TALLOC_CTX *mem_ctx;
198 int flags;
199 poptContext pc;
200 NTSTATUS status;
201 enum ndr_err_code ndr_err;
202 void *st;
203 void *v_st;
204 const char *ctx_filename = NULL;
205 const char *plugin = NULL;
206 bool validate = false;
207 bool dumpdata = false;
208 bool assume_ndr64 = false;
209 int opt;
210 enum {OPT_CONTEXT_FILE=1000, OPT_VALIDATE, OPT_DUMP_DATA, OPT_LOAD_DSO, OPT_NDR64};
211 struct poptOption long_options[] = {
212 POPT_AUTOHELP
213 {"context-file", 'c', POPT_ARG_STRING, NULL, OPT_CONTEXT_FILE, "In-filename to parse first", "CTX-FILE" },
214 {"validate", 0, POPT_ARG_NONE, NULL, OPT_VALIDATE, "try to validate the data", NULL },
215 {"dump-data", 0, POPT_ARG_NONE, NULL, OPT_DUMP_DATA, "dump the hex data", NULL },
216 {"load-dso", 'l', POPT_ARG_STRING, NULL, OPT_LOAD_DSO, "load from shared object file", NULL },
217 {"ndr64", 0, POPT_ARG_NONE, NULL, OPT_NDR64, "Assume NDR64 data", NULL },
218 POPT_COMMON_SAMBA
219 POPT_COMMON_VERSION
220 { NULL }
222 const struct ndr_interface_call_pipes *in_pipes = NULL;
223 const struct ndr_interface_call_pipes *out_pipes = NULL;
224 uint32_t highest_ofs;
225 struct dcerpc_sec_verification_trailer *sec_vt = NULL;
227 ndr_table_init();
229 /* Initialise samba stuff */
230 load_case_tables();
232 setlinebuf(stdout);
234 setup_logging("ndrdump", DEBUG_STDOUT);
236 pc = poptGetContext("ndrdump", argc, argv, long_options, 0);
238 poptSetOtherOptionHelp(
239 pc, "<pipe|uuid> <function> <inout> [<filename>]");
241 while ((opt = poptGetNextOpt(pc)) != -1) {
242 switch (opt) {
243 case OPT_CONTEXT_FILE:
244 ctx_filename = poptGetOptArg(pc);
245 break;
246 case OPT_VALIDATE:
247 validate = true;
248 break;
249 case OPT_DUMP_DATA:
250 dumpdata = true;
251 break;
252 case OPT_LOAD_DSO:
253 plugin = poptGetOptArg(pc);
254 break;
255 case OPT_NDR64:
256 assume_ndr64 = true;
257 break;
261 pipe_name = poptGetArg(pc);
263 if (!pipe_name) {
264 poptPrintUsage(pc, stderr, 0);
265 show_pipes();
266 exit(1);
269 if (plugin != NULL) {
270 p = load_iface_from_plugin(plugin, pipe_name);
272 if (!p) {
273 p = ndr_table_by_name(pipe_name);
276 if (!p) {
277 struct GUID uuid;
279 status = GUID_from_string(pipe_name, &uuid);
281 if (NT_STATUS_IS_OK(status)) {
282 p = ndr_table_by_uuid(&uuid);
286 if (!p) {
287 printf("Unknown pipe or UUID '%s'\n", pipe_name);
288 exit(1);
291 function = poptGetArg(pc);
292 inout = poptGetArg(pc);
293 filename = poptGetArg(pc);
295 if (!function || !inout) {
296 poptPrintUsage(pc, stderr, 0);
297 show_functions(p);
298 exit(1);
301 f = find_function(p, function);
303 if (strcmp(inout, "in") == 0 ||
304 strcmp(inout, "request") == 0) {
305 flags = NDR_IN;
306 in_pipes = &f->in_pipes;
307 } else if (strcmp(inout, "out") == 0 ||
308 strcmp(inout, "response") == 0) {
309 flags = NDR_OUT;
310 out_pipes = &f->out_pipes;
311 } else {
312 printf("Bad inout value '%s'\n", inout);
313 exit(1);
316 mem_ctx = talloc_init("ndrdump");
318 st = talloc_zero_size(mem_ctx, f->struct_size);
319 if (!st) {
320 printf("Unable to allocate %d bytes\n", (int)f->struct_size);
321 exit(1);
324 v_st = talloc_zero_size(mem_ctx, f->struct_size);
325 if (!v_st) {
326 printf("Unable to allocate %d bytes\n", (int)f->struct_size);
327 exit(1);
330 if (ctx_filename) {
331 if (flags == NDR_IN) {
332 printf("Context file can only be used for \"out\" packages\n");
333 exit(1);
336 data = (uint8_t *)file_load(ctx_filename, &size, 0, mem_ctx);
337 if (!data) {
338 perror(ctx_filename);
339 exit(1);
342 blob.data = data;
343 blob.length = size;
345 ndr_pull = ndr_pull_init_blob(&blob, mem_ctx);
346 if (ndr_pull == NULL) {
347 perror("ndr_pull_init_blob");
348 exit(1);
350 ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
351 if (assume_ndr64) {
352 ndr_pull->flags |= LIBNDR_FLAG_NDR64;
355 ndr_err = f->ndr_pull(ndr_pull, NDR_IN, st);
357 if (ndr_pull->offset > ndr_pull->relative_highest_offset) {
358 highest_ofs = ndr_pull->offset;
359 } else {
360 highest_ofs = ndr_pull->relative_highest_offset;
363 if (highest_ofs != ndr_pull->data_size) {
364 printf("WARNING! %d unread bytes while parsing context file\n", ndr_pull->data_size - highest_ofs);
367 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
368 status = ndr_map_error2ntstatus(ndr_err);
369 printf("pull for context file returned %s\n", nt_errstr(status));
370 exit(1);
372 memcpy(v_st, st, f->struct_size);
375 if (filename)
376 data = (uint8_t *)file_load(filename, &size, 0, mem_ctx);
377 else
378 data = (uint8_t *)stdin_load(mem_ctx, &size);
380 if (!data) {
381 if (filename)
382 perror(filename);
383 else
384 perror("stdin");
385 exit(1);
388 blob.data = data;
389 blob.length = size;
391 ndr_pull = ndr_pull_init_blob(&blob, mem_ctx);
392 if (ndr_pull == NULL) {
393 perror("ndr_pull_init_blob");
394 exit(1);
396 ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
397 if (assume_ndr64) {
398 ndr_pull->flags |= LIBNDR_FLAG_NDR64;
401 ndr_print = talloc_zero(mem_ctx, struct ndr_print);
402 ndr_print->print = ndr_print_printf_helper;
403 ndr_print->depth = 1;
405 ndr_err = ndr_pop_dcerpc_sec_verification_trailer(ndr_pull, mem_ctx, &sec_vt);
406 status = ndr_map_error2ntstatus(ndr_err);
407 if (!NT_STATUS_IS_OK(status)) {
408 printf("ndr_pop_dcerpc_sec_verification_trailer returned %s\n",
409 nt_errstr(status));
412 if (sec_vt != NULL && sec_vt->count.count > 0) {
413 printf("SEC_VT: consumed %d bytes\n",
414 (int)(blob.length - ndr_pull->data_size));
415 if (dumpdata) {
416 ndrdump_data(blob.data + ndr_pull->data_size,
417 blob.length - ndr_pull->data_size,
418 dumpdata);
420 ndr_print_dcerpc_sec_verification_trailer(ndr_print, "SEC_VT", sec_vt);
422 TALLOC_FREE(sec_vt);
424 if (out_pipes) {
425 status = ndrdump_pull_and_print_pipes(function, ndr_pull, ndr_print, out_pipes);
426 if (!NT_STATUS_IS_OK(status)) {
427 printf("dump FAILED\n");
428 exit(1);
432 ndr_err = f->ndr_pull(ndr_pull, flags, st);
433 status = ndr_map_error2ntstatus(ndr_err);
435 printf("pull returned %s\n", nt_errstr(status));
437 if (ndr_pull->offset > ndr_pull->relative_highest_offset) {
438 highest_ofs = ndr_pull->offset;
439 } else {
440 highest_ofs = ndr_pull->relative_highest_offset;
443 if (highest_ofs != ndr_pull->data_size) {
444 printf("WARNING! %d unread bytes\n", ndr_pull->data_size - highest_ofs);
445 ndrdump_data(ndr_pull->data+highest_ofs,
446 ndr_pull->data_size - highest_ofs,
447 dumpdata);
450 if (dumpdata) {
451 printf("%d bytes consumed\n", ndr_pull->offset);
452 ndrdump_data(blob.data, blob.length, dumpdata);
455 f->ndr_print(ndr_print, function, flags, st);
457 if (!NT_STATUS_IS_OK(status)) {
458 printf("dump FAILED\n");
459 exit(1);
462 if (in_pipes) {
463 status = ndrdump_pull_and_print_pipes(function, ndr_pull, ndr_print, in_pipes);
464 if (!NT_STATUS_IS_OK(status)) {
465 printf("dump FAILED\n");
466 exit(1);
470 if (validate) {
471 DATA_BLOB v_blob;
472 struct ndr_push *ndr_v_push;
473 struct ndr_pull *ndr_v_pull;
474 struct ndr_print *ndr_v_print;
475 uint32_t i;
476 uint8_t byte_a, byte_b;
477 bool differ;
479 ndr_v_push = ndr_push_init_ctx(mem_ctx);
481 ndr_err = f->ndr_push(ndr_v_push, flags, st);
482 status = ndr_map_error2ntstatus(ndr_err);
483 printf("push returned %s\n", nt_errstr(status));
484 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
485 printf("validate push FAILED\n");
486 exit(1);
489 v_blob = ndr_push_blob(ndr_v_push);
491 if (dumpdata) {
492 printf("%ld bytes generated (validate)\n", (long)v_blob.length);
493 ndrdump_data(v_blob.data, v_blob.length, dumpdata);
496 ndr_v_pull = ndr_pull_init_blob(&v_blob, mem_ctx);
497 if (ndr_v_pull == NULL) {
498 perror("ndr_pull_init_blob");
499 exit(1);
501 ndr_v_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
503 ndr_err = f->ndr_pull(ndr_v_pull, flags, v_st);
504 status = ndr_map_error2ntstatus(ndr_err);
505 printf("pull returned %s\n", nt_errstr(status));
506 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
507 printf("validate pull FAILED\n");
508 exit(1);
512 if (ndr_v_pull->offset != ndr_v_pull->data_size) {
513 printf("WARNING! %d unread bytes in validation\n", ndr_v_pull->data_size - ndr_v_pull->offset);
514 ndrdump_data(ndr_v_pull->data+ndr_v_pull->offset,
515 ndr_v_pull->data_size - ndr_v_pull->offset,
516 dumpdata);
519 ndr_v_print = talloc_zero(mem_ctx, struct ndr_print);
520 ndr_v_print->print = ndr_print_debug_helper;
521 ndr_v_print->depth = 1;
522 f->ndr_print(ndr_v_print, function, flags, v_st);
524 if (blob.length != v_blob.length) {
525 printf("WARNING! orig bytes:%llu validated pushed bytes:%llu\n",
526 (unsigned long long)blob.length, (unsigned long long)v_blob.length);
529 if (ndr_pull->offset != ndr_v_pull->offset) {
530 printf("WARNING! orig pulled bytes:%llu validated pulled bytes:%llu\n",
531 (unsigned long long)ndr_pull->offset, (unsigned long long)ndr_v_pull->offset);
534 differ = false;
535 byte_a = 0x00;
536 byte_b = 0x00;
537 for (i=0; i < blob.length; i++) {
538 byte_a = blob.data[i];
540 if (i == v_blob.length) {
541 byte_b = 0x00;
542 differ = true;
543 break;
546 byte_b = v_blob.data[i];
548 if (byte_a != byte_b) {
549 differ = true;
550 break;
553 if (differ) {
554 printf("WARNING! orig and validated differ at byte 0x%02X (%u)\n", i, i);
555 printf("WARNING! orig byte[0x%02X] = 0x%02X validated byte[0x%02X] = 0x%02X\n",
556 i, byte_a, i, byte_b);
560 printf("dump OK\n");
562 talloc_free(mem_ctx);
564 poptFreeContext(pc);
566 return 0;