2 Unix SMB/CIFS implementation.
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/>.
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
,
34 if (isdigit(function
[0])) {
35 i
= strtol(function
, NULL
, 0);
38 for (i
=0;i
<p
->num_calls
;i
++) {
39 if (strcmp(p
->calls
[i
].name
, function
) == 0) {
43 if (i
== p
->num_calls
) {
44 printf("Function '%s' not found\n", function
);
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
);
59 printf("\t%s\n", l
->table
->name
);
65 _NORETURN_
static void show_functions(const struct ndr_interface_table
*p
)
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
);
76 static char *stdin_load(TALLOC_CTX
*mem_ctx
, size_t *size
)
78 int num_read
, total_len
= 0;
82 while((num_read
= read(STDIN_FILENO
, buf
, 255)) > 0) {
85 result
= talloc_realloc(
86 mem_ctx
, result
, char, total_len
+ num_read
);
88 result
= talloc_array(mem_ctx
, char, num_read
);
91 memcpy(result
+ total_len
, buf
, num_read
);
93 total_len
+= num_read
;
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
;
108 handle
= dlopen(plugin
, RTLD_NOW
);
109 if (handle
== NULL
) {
110 printf("%s: Unable to open: %s\n", plugin
, dlerror());
114 symbol
= talloc_asprintf(NULL
, "ndr_table_%s", pipe_name
);
115 p
= (const struct ndr_interface_table
*)dlsym(handle
, symbol
);
118 printf("%s: Unable to find DCE/RPC interface table for '%s': %s\n", plugin
, pipe_name
, dlerror());
128 static void printf_cb(const char *buf
, void *private_data
)
133 static void ndrdump_data(uint8_t *d
, uint32_t l
, bool force
)
135 dump_data_cb(d
, l
, !force
, printf_cb
, NULL
);
138 static NTSTATUS
ndrdump_pull_and_print_pipes(const char *function
,
139 struct ndr_pull
*ndr_pull
,
140 struct ndr_print
*ndr_print
,
141 const struct ndr_interface_call_pipes
*pipes
)
144 enum ndr_err_code ndr_err
;
147 for (i
=0; i
< pipes
->num_pipes
; i
++) {
154 c
= talloc_zero_size(ndr_pull
, pipes
->pipes
[i
].chunk_struct_size
);
155 talloc_set_name(c
, "struct %s", pipes
->pipes
[i
].name
);
157 * Note: the first struct member is always
160 count
= (uint32_t *)c
;
162 n
= talloc_asprintf(c
, "%s: %s[%llu]",
163 function
, pipes
->pipes
[i
].name
,
164 (unsigned long long)idx
);
166 ndr_err
= pipes
->pipes
[i
].ndr_pull(ndr_pull
, NDR_SCALARS
, c
);
167 status
= ndr_map_error2ntstatus(ndr_err
);
169 printf("pull returned %s\n", nt_errstr(status
));
170 if (!NT_STATUS_IS_OK(status
)) {
173 pipes
->pipes
[i
].ndr_print(ndr_print
, n
, c
);
185 int main(int argc
, const char *argv
[])
187 const struct ndr_interface_table
*p
= NULL
;
188 const struct ndr_interface_call
*f
;
189 const char *pipe_name
, *function
, *inout
, *filename
;
193 struct ndr_pull
*ndr_pull
;
194 struct ndr_print
*ndr_print
;
199 enum ndr_err_code ndr_err
;
202 const char *ctx_filename
= NULL
;
203 const char *plugin
= NULL
;
204 bool validate
= false;
205 bool dumpdata
= false;
206 bool assume_ndr64
= false;
208 enum {OPT_CONTEXT_FILE
=1000, OPT_VALIDATE
, OPT_DUMP_DATA
, OPT_LOAD_DSO
, OPT_NDR64
};
209 struct poptOption long_options
[] = {
211 {"context-file", 'c', POPT_ARG_STRING
, NULL
, OPT_CONTEXT_FILE
, "In-filename to parse first", "CTX-FILE" },
212 {"validate", 0, POPT_ARG_NONE
, NULL
, OPT_VALIDATE
, "try to validate the data", NULL
},
213 {"dump-data", 0, POPT_ARG_NONE
, NULL
, OPT_DUMP_DATA
, "dump the hex data", NULL
},
214 {"load-dso", 'l', POPT_ARG_STRING
, NULL
, OPT_LOAD_DSO
, "load from shared object file", NULL
},
215 {"ndr64", 0, POPT_ARG_NONE
, NULL
, OPT_NDR64
, "Assume NDR64 data", NULL
},
220 const struct ndr_interface_call_pipes
*in_pipes
= NULL
;
221 const struct ndr_interface_call_pipes
*out_pipes
= NULL
;
225 /* Initialise samba stuff */
230 setup_logging("ndrdump", DEBUG_STDOUT
);
232 pc
= poptGetContext("ndrdump", argc
, argv
, long_options
, 0);
234 poptSetOtherOptionHelp(
235 pc
, "<pipe|uuid> <function> <inout> [<filename>]");
237 while ((opt
= poptGetNextOpt(pc
)) != -1) {
239 case OPT_CONTEXT_FILE
:
240 ctx_filename
= poptGetOptArg(pc
);
249 plugin
= poptGetOptArg(pc
);
257 pipe_name
= poptGetArg(pc
);
260 poptPrintUsage(pc
, stderr
, 0);
265 if (plugin
!= NULL
) {
266 p
= load_iface_from_plugin(plugin
, pipe_name
);
269 p
= ndr_table_by_name(pipe_name
);
275 status
= GUID_from_string(pipe_name
, &uuid
);
277 if (NT_STATUS_IS_OK(status
)) {
278 p
= ndr_table_by_uuid(&uuid
);
283 printf("Unknown pipe or UUID '%s'\n", pipe_name
);
287 function
= poptGetArg(pc
);
288 inout
= poptGetArg(pc
);
289 filename
= poptGetArg(pc
);
291 if (!function
|| !inout
) {
292 poptPrintUsage(pc
, stderr
, 0);
297 f
= find_function(p
, function
);
299 if (strcmp(inout
, "in") == 0 ||
300 strcmp(inout
, "request") == 0) {
302 in_pipes
= &f
->in_pipes
;
303 } else if (strcmp(inout
, "out") == 0 ||
304 strcmp(inout
, "response") == 0) {
306 out_pipes
= &f
->out_pipes
;
308 printf("Bad inout value '%s'\n", inout
);
312 mem_ctx
= talloc_init("ndrdump");
314 st
= talloc_zero_size(mem_ctx
, f
->struct_size
);
316 printf("Unable to allocate %d bytes\n", (int)f
->struct_size
);
320 v_st
= talloc_zero_size(mem_ctx
, f
->struct_size
);
322 printf("Unable to allocate %d bytes\n", (int)f
->struct_size
);
327 if (flags
== NDR_IN
) {
328 printf("Context file can only be used for \"out\" packages\n");
332 data
= (uint8_t *)file_load(ctx_filename
, &size
, 0, mem_ctx
);
334 perror(ctx_filename
);
341 ndr_pull
= ndr_pull_init_blob(&blob
, mem_ctx
);
342 ndr_pull
->flags
|= LIBNDR_FLAG_REF_ALLOC
;
344 ndr_pull
->flags
|= LIBNDR_FLAG_NDR64
;
347 ndr_err
= f
->ndr_pull(ndr_pull
, NDR_IN
, st
);
349 if (ndr_pull
->offset
!= ndr_pull
->data_size
) {
350 printf("WARNING! %d unread bytes while parsing context file\n", ndr_pull
->data_size
- ndr_pull
->offset
);
353 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
354 status
= ndr_map_error2ntstatus(ndr_err
);
355 printf("pull for context file returned %s\n", nt_errstr(status
));
358 memcpy(v_st
, st
, f
->struct_size
);
362 data
= (uint8_t *)file_load(filename
, &size
, 0, mem_ctx
);
364 data
= (uint8_t *)stdin_load(mem_ctx
, &size
);
377 ndr_pull
= ndr_pull_init_blob(&blob
, mem_ctx
);
378 ndr_pull
->flags
|= LIBNDR_FLAG_REF_ALLOC
;
380 ndr_pull
->flags
|= LIBNDR_FLAG_NDR64
;
383 ndr_print
= talloc_zero(mem_ctx
, struct ndr_print
);
384 ndr_print
->print
= ndr_print_printf_helper
;
385 ndr_print
->depth
= 1;
388 status
= ndrdump_pull_and_print_pipes(function
, ndr_pull
, ndr_print
, out_pipes
);
389 if (!NT_STATUS_IS_OK(status
)) {
390 printf("dump FAILED\n");
395 ndr_err
= f
->ndr_pull(ndr_pull
, flags
, st
);
396 status
= ndr_map_error2ntstatus(ndr_err
);
398 printf("pull returned %s\n", nt_errstr(status
));
400 if (ndr_pull
->offset
!= ndr_pull
->data_size
) {
401 printf("WARNING! %d unread bytes\n", ndr_pull
->data_size
- ndr_pull
->offset
);
402 ndrdump_data(ndr_pull
->data
+ndr_pull
->offset
,
403 ndr_pull
->data_size
- ndr_pull
->offset
,
408 printf("%d bytes consumed\n", ndr_pull
->offset
);
409 ndrdump_data(blob
.data
, blob
.length
, dumpdata
);
412 f
->ndr_print(ndr_print
, function
, flags
, st
);
414 if (!NT_STATUS_IS_OK(status
)) {
415 printf("dump FAILED\n");
420 status
= ndrdump_pull_and_print_pipes(function
, ndr_pull
, ndr_print
, in_pipes
);
421 if (!NT_STATUS_IS_OK(status
)) {
422 printf("dump FAILED\n");
429 struct ndr_push
*ndr_v_push
;
430 struct ndr_pull
*ndr_v_pull
;
431 struct ndr_print
*ndr_v_print
;
433 uint8_t byte_a
, byte_b
;
436 ndr_v_push
= ndr_push_init_ctx(mem_ctx
);
438 ndr_err
= f
->ndr_push(ndr_v_push
, flags
, st
);
439 status
= ndr_map_error2ntstatus(ndr_err
);
440 printf("push returned %s\n", nt_errstr(status
));
441 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
442 printf("validate push FAILED\n");
446 v_blob
= ndr_push_blob(ndr_v_push
);
449 printf("%ld bytes generated (validate)\n", (long)v_blob
.length
);
450 ndrdump_data(v_blob
.data
, v_blob
.length
, dumpdata
);
453 ndr_v_pull
= ndr_pull_init_blob(&v_blob
, mem_ctx
);
454 ndr_v_pull
->flags
|= LIBNDR_FLAG_REF_ALLOC
;
456 ndr_err
= f
->ndr_pull(ndr_v_pull
, flags
, v_st
);
457 status
= ndr_map_error2ntstatus(ndr_err
);
458 printf("pull returned %s\n", nt_errstr(status
));
459 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
460 printf("validate pull FAILED\n");
465 if (ndr_v_pull
->offset
!= ndr_v_pull
->data_size
) {
466 printf("WARNING! %d unread bytes in validation\n", ndr_v_pull
->data_size
- ndr_v_pull
->offset
);
467 ndrdump_data(ndr_v_pull
->data
+ndr_v_pull
->offset
,
468 ndr_v_pull
->data_size
- ndr_v_pull
->offset
,
472 ndr_v_print
= talloc_zero(mem_ctx
, struct ndr_print
);
473 ndr_v_print
->print
= ndr_print_debug_helper
;
474 ndr_v_print
->depth
= 1;
475 f
->ndr_print(ndr_v_print
, function
, flags
, v_st
);
477 if (blob
.length
!= v_blob
.length
) {
478 printf("WARNING! orig bytes:%llu validated pushed bytes:%llu\n",
479 (unsigned long long)blob
.length
, (unsigned long long)v_blob
.length
);
482 if (ndr_pull
->offset
!= ndr_v_pull
->offset
) {
483 printf("WARNING! orig pulled bytes:%llu validated pulled bytes:%llu\n",
484 (unsigned long long)ndr_pull
->offset
, (unsigned long long)ndr_v_pull
->offset
);
490 for (i
=0; i
< blob
.length
; i
++) {
491 byte_a
= blob
.data
[i
];
493 if (i
== v_blob
.length
) {
499 byte_b
= v_blob
.data
[i
];
501 if (byte_a
!= byte_b
) {
507 printf("WARNING! orig and validated differ at byte 0x%02X (%u)\n", i
, i
);
508 printf("WARNING! orig byte[0x%02X] = 0x%02X validated byte[0x%02X] = 0x%02X\n",
509 i
, byte_a
, i
, byte_b
);
515 talloc_free(mem_ctx
);