s3-printing: remove duplicate cups response processing code
[Samba.git] / librpc / tools / ndrdump.c
blob284dbcedf9c0dcea3f94d26bdd25691401a697d8
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 #if (_SAMBA_BUILD_ >= 4)
27 #include "lib/cmdline/popt_common.h"
28 #include "param/param.h"
29 #endif
31 static const struct ndr_interface_call *find_function(
32 const struct ndr_interface_table *p,
33 const char *function)
35 int i;
36 if (isdigit(function[0])) {
37 i = strtol(function, NULL, 0);
38 return &p->calls[i];
40 for (i=0;i<p->num_calls;i++) {
41 if (strcmp(p->calls[i].name, function) == 0) {
42 break;
45 if (i == p->num_calls) {
46 printf("Function '%s' not found\n", function);
47 exit(1);
49 return &p->calls[i];
52 _NORETURN_ static void show_pipes(void)
54 const struct ndr_interface_list *l;
55 printf("\nYou must specify a pipe\n");
56 printf("known pipes are:\n");
57 for (l=ndr_table_list();l;l=l->next) {
58 if(l->table->helpstring) {
59 printf("\t%s - %s\n", l->table->name, l->table->helpstring);
60 } else {
61 printf("\t%s\n", l->table->name);
64 exit(1);
67 _NORETURN_ static void show_functions(const struct ndr_interface_table *p)
69 int i;
70 printf("\nYou must specify a function\n");
71 printf("known functions on '%s' are:\n", p->name);
72 for (i=0;i<p->num_calls;i++) {
73 printf("\t0x%02x (%2d) %s\n", i, i, p->calls[i].name);
75 exit(1);
78 static char *stdin_load(TALLOC_CTX *mem_ctx, size_t *size)
80 int num_read, total_len = 0;
81 char buf[255];
82 char *result = NULL;
84 while((num_read = read(STDIN_FILENO, buf, 255)) > 0) {
86 if (result) {
87 result = talloc_realloc(
88 mem_ctx, result, char, total_len + num_read);
89 } else {
90 result = talloc_array(mem_ctx, char, num_read);
93 memcpy(result + total_len, buf, num_read);
95 total_len += num_read;
98 if (size)
99 *size = total_len;
101 return result;
104 static const struct ndr_interface_table *load_iface_from_plugin(const char *plugin, const char *pipe_name)
106 const struct ndr_interface_table *p;
107 void *handle;
108 char *symbol;
110 handle = dlopen(plugin, RTLD_NOW);
111 if (handle == NULL) {
112 printf("%s: Unable to open: %s\n", plugin, dlerror());
113 return NULL;
116 symbol = talloc_asprintf(NULL, "ndr_table_%s", pipe_name);
117 p = (const struct ndr_interface_table *)dlsym(handle, symbol);
119 if (!p) {
120 printf("%s: Unable to find DCE/RPC interface table for '%s': %s\n", plugin, pipe_name, dlerror());
121 talloc_free(symbol);
122 return NULL;
125 talloc_free(symbol);
127 return p;
130 static void printf_cb(const char *buf, void *private_data)
132 printf("%s", buf);
135 static void ndrdump_data(uint8_t *d, uint32_t l, bool force)
137 dump_data_cb(d, l, !force, printf_cb, NULL);
140 static NTSTATUS ndrdump_pull_and_print_pipes(const char *function,
141 struct ndr_pull *ndr_pull,
142 struct ndr_print *ndr_print,
143 const struct ndr_interface_call_pipes *pipes)
145 NTSTATUS status;
146 enum ndr_err_code ndr_err;
147 uint32_t i;
149 for (i=0; i < pipes->num_pipes; i++) {
150 uint64_t idx = 0;
151 while (true) {
152 uint32_t *count;
153 void *c;
154 char *n;
156 c = talloc_zero_size(ndr_pull, pipes->pipes[i].chunk_struct_size);
157 talloc_set_name(c, "struct %s", pipes->pipes[i].name);
159 * Note: the first struct member is always
160 * 'uint32_t count;'
162 count = (uint32_t *)c;
164 n = talloc_asprintf(c, "%s: %s[%llu]",
165 function, pipes->pipes[i].name,
166 (unsigned long long)idx);
168 ndr_err = pipes->pipes[i].ndr_pull(ndr_pull, NDR_SCALARS, c);
169 status = ndr_map_error2ntstatus(ndr_err);
171 printf("pull returned %s\n", nt_errstr(status));
172 if (!NT_STATUS_IS_OK(status)) {
173 return status;
175 pipes->pipes[i].ndr_print(ndr_print, n, 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 struct ndr_interface_call_pipes *in_pipes = NULL;
223 struct ndr_interface_call_pipes *out_pipes = NULL;
225 ndr_table_init();
227 /* Initialise samba stuff */
228 load_case_tables();
230 setlinebuf(stdout);
232 setup_logging("ndrdump", DEBUG_STDOUT);
234 pc = poptGetContext("ndrdump", argc, argv, long_options, 0);
236 poptSetOtherOptionHelp(
237 pc, "<pipe|uuid> <function> <inout> [<filename>]");
239 while ((opt = poptGetNextOpt(pc)) != -1) {
240 switch (opt) {
241 case OPT_CONTEXT_FILE:
242 ctx_filename = poptGetOptArg(pc);
243 break;
244 case OPT_VALIDATE:
245 validate = true;
246 break;
247 case OPT_DUMP_DATA:
248 dumpdata = true;
249 break;
250 case OPT_LOAD_DSO:
251 plugin = poptGetOptArg(pc);
252 break;
253 case OPT_NDR64:
254 assume_ndr64 = true;
255 break;
259 pipe_name = poptGetArg(pc);
261 if (!pipe_name) {
262 poptPrintUsage(pc, stderr, 0);
263 show_pipes();
264 exit(1);
267 if (plugin != NULL) {
268 p = load_iface_from_plugin(plugin, pipe_name);
270 if (!p) {
271 p = ndr_table_by_name(pipe_name);
274 if (!p) {
275 struct GUID uuid;
277 status = GUID_from_string(pipe_name, &uuid);
279 if (NT_STATUS_IS_OK(status)) {
280 p = ndr_table_by_uuid(&uuid);
284 if (!p) {
285 printf("Unknown pipe or UUID '%s'\n", pipe_name);
286 exit(1);
289 function = poptGetArg(pc);
290 inout = poptGetArg(pc);
291 filename = poptGetArg(pc);
293 if (!function || !inout) {
294 poptPrintUsage(pc, stderr, 0);
295 show_functions(p);
296 exit(1);
299 f = find_function(p, function);
301 if (strcmp(inout, "in") == 0 ||
302 strcmp(inout, "request") == 0) {
303 flags = NDR_IN;
304 in_pipes = &f->in_pipes;
305 } else if (strcmp(inout, "out") == 0 ||
306 strcmp(inout, "response") == 0) {
307 flags = NDR_OUT;
308 out_pipes = &f->out_pipes;
309 } else {
310 printf("Bad inout value '%s'\n", inout);
311 exit(1);
314 mem_ctx = talloc_init("ndrdump");
316 st = talloc_zero_size(mem_ctx, f->struct_size);
317 if (!st) {
318 printf("Unable to allocate %d bytes\n", (int)f->struct_size);
319 exit(1);
322 v_st = talloc_zero_size(mem_ctx, f->struct_size);
323 if (!v_st) {
324 printf("Unable to allocate %d bytes\n", (int)f->struct_size);
325 exit(1);
328 if (ctx_filename) {
329 if (flags == NDR_IN) {
330 printf("Context file can only be used for \"out\" packages\n");
331 exit(1);
334 data = (uint8_t *)file_load(ctx_filename, &size, 0, mem_ctx);
335 if (!data) {
336 perror(ctx_filename);
337 exit(1);
340 blob.data = data;
341 blob.length = size;
343 ndr_pull = ndr_pull_init_blob(&blob, mem_ctx);
344 ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
345 if (assume_ndr64) {
346 ndr_pull->flags |= LIBNDR_FLAG_NDR64;
349 ndr_err = f->ndr_pull(ndr_pull, NDR_IN, st);
351 if (ndr_pull->offset != ndr_pull->data_size) {
352 printf("WARNING! %d unread bytes while parsing context file\n", ndr_pull->data_size - ndr_pull->offset);
355 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
356 status = ndr_map_error2ntstatus(ndr_err);
357 printf("pull for context file returned %s\n", nt_errstr(status));
358 exit(1);
360 memcpy(v_st, st, f->struct_size);
363 if (filename)
364 data = (uint8_t *)file_load(filename, &size, 0, mem_ctx);
365 else
366 data = (uint8_t *)stdin_load(mem_ctx, &size);
368 if (!data) {
369 if (filename)
370 perror(filename);
371 else
372 perror("stdin");
373 exit(1);
376 blob.data = data;
377 blob.length = size;
379 ndr_pull = ndr_pull_init_blob(&blob, mem_ctx);
380 ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
381 if (assume_ndr64) {
382 ndr_pull->flags |= LIBNDR_FLAG_NDR64;
385 ndr_print = talloc_zero(mem_ctx, struct ndr_print);
386 ndr_print->print = ndr_print_printf_helper;
387 ndr_print->depth = 1;
389 if (out_pipes) {
390 status = ndrdump_pull_and_print_pipes(function, ndr_pull, ndr_print, out_pipes);
391 if (!NT_STATUS_IS_OK(status)) {
392 printf("dump FAILED\n");
393 exit(1);
397 ndr_err = f->ndr_pull(ndr_pull, flags, st);
398 status = ndr_map_error2ntstatus(ndr_err);
400 printf("pull returned %s\n", nt_errstr(status));
402 if (ndr_pull->offset != ndr_pull->data_size) {
403 printf("WARNING! %d unread bytes\n", ndr_pull->data_size - ndr_pull->offset);
404 ndrdump_data(ndr_pull->data+ndr_pull->offset,
405 ndr_pull->data_size - ndr_pull->offset,
406 dumpdata);
409 if (dumpdata) {
410 printf("%d bytes consumed\n", ndr_pull->offset);
411 ndrdump_data(blob.data, blob.length, dumpdata);
414 f->ndr_print(ndr_print, function, flags, st);
416 if (!NT_STATUS_IS_OK(status)) {
417 printf("dump FAILED\n");
418 exit(1);
421 if (in_pipes) {
422 status = ndrdump_pull_and_print_pipes(function, ndr_pull, ndr_print, in_pipes);
423 if (!NT_STATUS_IS_OK(status)) {
424 printf("dump FAILED\n");
425 exit(1);
429 if (validate) {
430 DATA_BLOB v_blob;
431 struct ndr_push *ndr_v_push;
432 struct ndr_pull *ndr_v_pull;
433 struct ndr_print *ndr_v_print;
434 uint32_t i;
435 uint8_t byte_a, byte_b;
436 bool differ;
438 ndr_v_push = ndr_push_init_ctx(mem_ctx);
440 ndr_err = f->ndr_push(ndr_v_push, flags, st);
441 status = ndr_map_error2ntstatus(ndr_err);
442 printf("push returned %s\n", nt_errstr(status));
443 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
444 printf("validate push FAILED\n");
445 exit(1);
448 v_blob = ndr_push_blob(ndr_v_push);
450 if (dumpdata) {
451 printf("%ld bytes generated (validate)\n", (long)v_blob.length);
452 ndrdump_data(v_blob.data, v_blob.length, dumpdata);
455 ndr_v_pull = ndr_pull_init_blob(&v_blob, mem_ctx);
456 ndr_v_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
458 ndr_err = f->ndr_pull(ndr_v_pull, flags, v_st);
459 status = ndr_map_error2ntstatus(ndr_err);
460 printf("pull returned %s\n", nt_errstr(status));
461 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
462 printf("validate pull FAILED\n");
463 exit(1);
467 if (ndr_v_pull->offset != ndr_v_pull->data_size) {
468 printf("WARNING! %d unread bytes in validation\n", ndr_v_pull->data_size - ndr_v_pull->offset);
469 ndrdump_data(ndr_v_pull->data+ndr_v_pull->offset,
470 ndr_v_pull->data_size - ndr_v_pull->offset,
471 dumpdata);
474 ndr_v_print = talloc_zero(mem_ctx, struct ndr_print);
475 ndr_v_print->print = ndr_print_debug_helper;
476 ndr_v_print->depth = 1;
477 f->ndr_print(ndr_v_print, function, flags, v_st);
479 if (blob.length != v_blob.length) {
480 printf("WARNING! orig bytes:%llu validated pushed bytes:%llu\n",
481 (unsigned long long)blob.length, (unsigned long long)v_blob.length);
484 if (ndr_pull->offset != ndr_v_pull->offset) {
485 printf("WARNING! orig pulled bytes:%llu validated pulled bytes:%llu\n",
486 (unsigned long long)ndr_pull->offset, (unsigned long long)ndr_v_pull->offset);
489 differ = false;
490 byte_a = 0x00;
491 byte_b = 0x00;
492 for (i=0; i < blob.length; i++) {
493 byte_a = blob.data[i];
495 if (i == v_blob.length) {
496 byte_b = 0x00;
497 differ = true;
498 break;
501 byte_b = v_blob.data[i];
503 if (byte_a != byte_b) {
504 differ = true;
505 break;
508 if (differ) {
509 printf("WARNING! orig and validated differ at byte 0x%02X (%u)\n", i, i);
510 printf("WARNING! orig byte[0x%02X] = 0x%02X validated byte[0x%02X] = 0x%02X\n",
511 i, byte_a, i, byte_b);
515 printf("dump OK\n");
517 talloc_free(mem_ctx);
519 poptFreeContext(pc);
521 return 0;