examples: Move example smb.conf over to "server role"
[Samba/gebeck_regimport.git] / librpc / tools / ndrdump.c
blob692e6559480cafecaf1ba3105e71725c641394a8
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 "lib/cmdline/popt_common.h"
27 #include "param/param.h"
29 static const struct ndr_interface_call *find_function(
30 const struct ndr_interface_table *p,
31 const char *function)
33 int i;
34 if (isdigit(function[0])) {
35 i = strtol(function, NULL, 0);
36 return &p->calls[i];
38 for (i=0;i<p->num_calls;i++) {
39 if (strcmp(p->calls[i].name, function) == 0) {
40 break;
43 if (i == p->num_calls) {
44 printf("Function '%s' not found\n", function);
45 exit(1);
47 return &p->calls[i];
50 _NORETURN_ static void show_pipes(void)
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);
62 exit(1);
65 _NORETURN_ static void show_functions(const struct ndr_interface_table *p)
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);
73 exit(1);
76 static char *stdin_load(TALLOC_CTX *mem_ctx, size_t *size)
78 int num_read, total_len = 0;
79 char buf[255];
80 char *result = NULL;
82 while((num_read = read(STDIN_FILENO, buf, 255)) > 0) {
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);
91 memcpy(result + total_len, buf, num_read);
93 total_len += num_read;
96 if (size)
97 *size = total_len;
99 return result;
102 static const struct ndr_interface_table *load_iface_from_plugin(const char *plugin, const char *pipe_name)
104 const struct ndr_interface_table *p;
105 void *handle;
106 char *symbol;
108 handle = dlopen(plugin, RTLD_NOW);
109 if (handle == NULL) {
110 printf("%s: Unable to open: %s\n", plugin, dlerror());
111 return NULL;
114 symbol = talloc_asprintf(NULL, "ndr_table_%s", pipe_name);
115 p = (const struct ndr_interface_table *)dlsym(handle, symbol);
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 dlclose(handle);
121 return NULL;
124 talloc_free(symbol);
126 return p;
129 static void ndrdump_data(uint8_t *d, uint32_t l, bool force)
131 dump_data_file(d, l, !force, stdout);
134 static NTSTATUS ndrdump_pull_and_print_pipes(const char *function,
135 struct ndr_pull *ndr_pull,
136 struct ndr_print *ndr_print,
137 const struct ndr_interface_call_pipes *pipes)
139 NTSTATUS status;
140 enum ndr_err_code ndr_err;
141 uint32_t i;
143 for (i=0; i < pipes->num_pipes; i++) {
144 uint64_t idx = 0;
145 while (true) {
146 uint32_t *count;
147 void *c;
148 char *n;
150 c = talloc_zero_size(ndr_pull, pipes->pipes[i].chunk_struct_size);
151 talloc_set_name(c, "struct %s", pipes->pipes[i].name);
153 * Note: the first struct member is always
154 * 'uint32_t count;'
156 count = (uint32_t *)c;
158 n = talloc_asprintf(c, "%s: %s[%llu]",
159 function, pipes->pipes[i].name,
160 (unsigned long long)idx);
162 ndr_err = pipes->pipes[i].ndr_pull(ndr_pull, NDR_SCALARS, c);
163 status = ndr_map_error2ntstatus(ndr_err);
165 printf("pull returned %s\n", nt_errstr(status));
166 if (!NT_STATUS_IS_OK(status)) {
167 return status;
169 pipes->pipes[i].ndr_print(ndr_print, n, c);
171 if (*count == 0) {
172 break;
174 idx++;
178 return NT_STATUS_OK;
181 int main(int argc, const char *argv[])
183 const struct ndr_interface_table *p = NULL;
184 const struct ndr_interface_call *f;
185 const char *pipe_name, *function, *inout, *filename;
186 uint8_t *data;
187 size_t size;
188 DATA_BLOB blob;
189 struct ndr_pull *ndr_pull;
190 struct ndr_print *ndr_print;
191 TALLOC_CTX *mem_ctx;
192 int flags;
193 poptContext pc;
194 NTSTATUS status;
195 enum ndr_err_code ndr_err;
196 void *st;
197 void *v_st;
198 const char *ctx_filename = NULL;
199 const char *plugin = NULL;
200 bool validate = false;
201 bool dumpdata = false;
202 bool assume_ndr64 = false;
203 int opt;
204 enum {OPT_CONTEXT_FILE=1000, OPT_VALIDATE, OPT_DUMP_DATA, OPT_LOAD_DSO, OPT_NDR64};
205 struct poptOption long_options[] = {
206 POPT_AUTOHELP
207 {"context-file", 'c', POPT_ARG_STRING, NULL, OPT_CONTEXT_FILE, "In-filename to parse first", "CTX-FILE" },
208 {"validate", 0, POPT_ARG_NONE, NULL, OPT_VALIDATE, "try to validate the data", NULL },
209 {"dump-data", 0, POPT_ARG_NONE, NULL, OPT_DUMP_DATA, "dump the hex data", NULL },
210 {"load-dso", 'l', POPT_ARG_STRING, NULL, OPT_LOAD_DSO, "load from shared object file", NULL },
211 {"ndr64", 0, POPT_ARG_NONE, NULL, OPT_NDR64, "Assume NDR64 data", NULL },
212 POPT_COMMON_SAMBA
213 POPT_COMMON_VERSION
214 { NULL }
216 const struct ndr_interface_call_pipes *in_pipes = NULL;
217 const struct ndr_interface_call_pipes *out_pipes = NULL;
218 uint32_t highest_ofs;
220 ndr_table_init();
222 /* Initialise samba stuff */
223 load_case_tables();
225 setlinebuf(stdout);
227 setup_logging("ndrdump", DEBUG_STDOUT);
229 pc = poptGetContext("ndrdump", argc, argv, long_options, 0);
231 poptSetOtherOptionHelp(
232 pc, "<pipe|uuid> <function> <inout> [<filename>]");
234 while ((opt = poptGetNextOpt(pc)) != -1) {
235 switch (opt) {
236 case OPT_CONTEXT_FILE:
237 ctx_filename = poptGetOptArg(pc);
238 break;
239 case OPT_VALIDATE:
240 validate = true;
241 break;
242 case OPT_DUMP_DATA:
243 dumpdata = true;
244 break;
245 case OPT_LOAD_DSO:
246 plugin = poptGetOptArg(pc);
247 break;
248 case OPT_NDR64:
249 assume_ndr64 = true;
250 break;
254 pipe_name = poptGetArg(pc);
256 if (!pipe_name) {
257 poptPrintUsage(pc, stderr, 0);
258 show_pipes();
259 exit(1);
262 if (plugin != NULL) {
263 p = load_iface_from_plugin(plugin, pipe_name);
265 if (!p) {
266 p = ndr_table_by_name(pipe_name);
269 if (!p) {
270 struct GUID uuid;
272 status = GUID_from_string(pipe_name, &uuid);
274 if (NT_STATUS_IS_OK(status)) {
275 p = ndr_table_by_uuid(&uuid);
279 if (!p) {
280 printf("Unknown pipe or UUID '%s'\n", pipe_name);
281 exit(1);
284 function = poptGetArg(pc);
285 inout = poptGetArg(pc);
286 filename = poptGetArg(pc);
288 if (!function || !inout) {
289 poptPrintUsage(pc, stderr, 0);
290 show_functions(p);
291 exit(1);
294 f = find_function(p, function);
296 if (strcmp(inout, "in") == 0 ||
297 strcmp(inout, "request") == 0) {
298 flags = NDR_IN;
299 in_pipes = &f->in_pipes;
300 } else if (strcmp(inout, "out") == 0 ||
301 strcmp(inout, "response") == 0) {
302 flags = NDR_OUT;
303 out_pipes = &f->out_pipes;
304 } else {
305 printf("Bad inout value '%s'\n", inout);
306 exit(1);
309 mem_ctx = talloc_init("ndrdump");
311 st = talloc_zero_size(mem_ctx, f->struct_size);
312 if (!st) {
313 printf("Unable to allocate %d bytes\n", (int)f->struct_size);
314 exit(1);
317 v_st = talloc_zero_size(mem_ctx, f->struct_size);
318 if (!v_st) {
319 printf("Unable to allocate %d bytes\n", (int)f->struct_size);
320 exit(1);
323 if (ctx_filename) {
324 if (flags == NDR_IN) {
325 printf("Context file can only be used for \"out\" packages\n");
326 exit(1);
329 data = (uint8_t *)file_load(ctx_filename, &size, 0, mem_ctx);
330 if (!data) {
331 perror(ctx_filename);
332 exit(1);
335 blob.data = data;
336 blob.length = size;
338 ndr_pull = ndr_pull_init_blob(&blob, mem_ctx);
339 if (ndr_pull == NULL) {
340 perror("ndr_pull_init_blob");
341 exit(1);
343 ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
344 if (assume_ndr64) {
345 ndr_pull->flags |= LIBNDR_FLAG_NDR64;
348 ndr_err = f->ndr_pull(ndr_pull, NDR_IN, st);
350 if (ndr_pull->offset > ndr_pull->relative_highest_offset) {
351 highest_ofs = ndr_pull->offset;
352 } else {
353 highest_ofs = ndr_pull->relative_highest_offset;
356 if (highest_ofs != ndr_pull->data_size) {
357 printf("WARNING! %d unread bytes while parsing context file\n", ndr_pull->data_size - highest_ofs);
360 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
361 status = ndr_map_error2ntstatus(ndr_err);
362 printf("pull for context file returned %s\n", nt_errstr(status));
363 exit(1);
365 memcpy(v_st, st, f->struct_size);
368 if (filename)
369 data = (uint8_t *)file_load(filename, &size, 0, mem_ctx);
370 else
371 data = (uint8_t *)stdin_load(mem_ctx, &size);
373 if (!data) {
374 if (filename)
375 perror(filename);
376 else
377 perror("stdin");
378 exit(1);
381 blob.data = data;
382 blob.length = size;
384 ndr_pull = ndr_pull_init_blob(&blob, mem_ctx);
385 if (ndr_pull == NULL) {
386 perror("ndr_pull_init_blob");
387 exit(1);
389 ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
390 if (assume_ndr64) {
391 ndr_pull->flags |= LIBNDR_FLAG_NDR64;
394 ndr_print = talloc_zero(mem_ctx, struct ndr_print);
395 ndr_print->print = ndr_print_printf_helper;
396 ndr_print->depth = 1;
398 if (out_pipes) {
399 status = ndrdump_pull_and_print_pipes(function, ndr_pull, ndr_print, out_pipes);
400 if (!NT_STATUS_IS_OK(status)) {
401 printf("dump FAILED\n");
402 exit(1);
406 ndr_err = f->ndr_pull(ndr_pull, flags, st);
407 status = ndr_map_error2ntstatus(ndr_err);
409 printf("pull returned %s\n", nt_errstr(status));
411 if (ndr_pull->offset > ndr_pull->relative_highest_offset) {
412 highest_ofs = ndr_pull->offset;
413 } else {
414 highest_ofs = ndr_pull->relative_highest_offset;
417 if (highest_ofs != ndr_pull->data_size) {
418 printf("WARNING! %d unread bytes\n", ndr_pull->data_size - highest_ofs);
419 ndrdump_data(ndr_pull->data+highest_ofs,
420 ndr_pull->data_size - highest_ofs,
421 dumpdata);
424 if (dumpdata) {
425 printf("%d bytes consumed\n", ndr_pull->offset);
426 ndrdump_data(blob.data, blob.length, dumpdata);
429 f->ndr_print(ndr_print, function, flags, st);
431 if (!NT_STATUS_IS_OK(status)) {
432 printf("dump FAILED\n");
433 exit(1);
436 if (in_pipes) {
437 status = ndrdump_pull_and_print_pipes(function, ndr_pull, ndr_print, in_pipes);
438 if (!NT_STATUS_IS_OK(status)) {
439 printf("dump FAILED\n");
440 exit(1);
444 if (validate) {
445 DATA_BLOB v_blob;
446 struct ndr_push *ndr_v_push;
447 struct ndr_pull *ndr_v_pull;
448 struct ndr_print *ndr_v_print;
449 uint32_t i;
450 uint8_t byte_a, byte_b;
451 bool differ;
453 ndr_v_push = ndr_push_init_ctx(mem_ctx);
455 ndr_err = f->ndr_push(ndr_v_push, flags, st);
456 status = ndr_map_error2ntstatus(ndr_err);
457 printf("push returned %s\n", nt_errstr(status));
458 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
459 printf("validate push FAILED\n");
460 exit(1);
463 v_blob = ndr_push_blob(ndr_v_push);
465 if (dumpdata) {
466 printf("%ld bytes generated (validate)\n", (long)v_blob.length);
467 ndrdump_data(v_blob.data, v_blob.length, dumpdata);
470 ndr_v_pull = ndr_pull_init_blob(&v_blob, mem_ctx);
471 if (ndr_v_pull == NULL) {
472 perror("ndr_pull_init_blob");
473 exit(1);
475 ndr_v_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
477 ndr_err = f->ndr_pull(ndr_v_pull, flags, v_st);
478 status = ndr_map_error2ntstatus(ndr_err);
479 printf("pull returned %s\n", nt_errstr(status));
480 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
481 printf("validate pull FAILED\n");
482 exit(1);
486 if (ndr_v_pull->offset != ndr_v_pull->data_size) {
487 printf("WARNING! %d unread bytes in validation\n", ndr_v_pull->data_size - ndr_v_pull->offset);
488 ndrdump_data(ndr_v_pull->data+ndr_v_pull->offset,
489 ndr_v_pull->data_size - ndr_v_pull->offset,
490 dumpdata);
493 ndr_v_print = talloc_zero(mem_ctx, struct ndr_print);
494 ndr_v_print->print = ndr_print_debug_helper;
495 ndr_v_print->depth = 1;
496 f->ndr_print(ndr_v_print, function, flags, v_st);
498 if (blob.length != v_blob.length) {
499 printf("WARNING! orig bytes:%llu validated pushed bytes:%llu\n",
500 (unsigned long long)blob.length, (unsigned long long)v_blob.length);
503 if (ndr_pull->offset != ndr_v_pull->offset) {
504 printf("WARNING! orig pulled bytes:%llu validated pulled bytes:%llu\n",
505 (unsigned long long)ndr_pull->offset, (unsigned long long)ndr_v_pull->offset);
508 differ = false;
509 byte_a = 0x00;
510 byte_b = 0x00;
511 for (i=0; i < blob.length; i++) {
512 byte_a = blob.data[i];
514 if (i == v_blob.length) {
515 byte_b = 0x00;
516 differ = true;
517 break;
520 byte_b = v_blob.data[i];
522 if (byte_a != byte_b) {
523 differ = true;
524 break;
527 if (differ) {
528 printf("WARNING! orig and validated differ at byte 0x%02X (%u)\n", i, i);
529 printf("WARNING! orig byte[0x%02X] = 0x%02X validated byte[0x%02X] = 0x%02X\n",
530 i, byte_a, i, byte_b);
534 printf("dump OK\n");
536 talloc_free(mem_ctx);
538 poptFreeContext(pc);
540 return 0;