r15834: fixed a memory leak in the session code
[Samba/aatanasov.git] / source4 / utils / ndrdump.c
blob86a32e9d5b11600d30f194b76fc62b0710ac511f
1 /*
2 Unix SMB/CIFS implementation.
3 SMB torture tester
4 Copyright (C) Andrew Tridgell 2003
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
22 #include "lib/cmdline/popt_common.h"
23 #include "system/filesys.h"
24 #include "system/locale.h"
25 #include "librpc/rpc/dcerpc.h"
26 #include "librpc/rpc/dcerpc_table.h"
28 static const struct dcerpc_interface_call *find_function(
29 const struct dcerpc_interface_table *p,
30 const char *function)
32 int i;
33 if (isdigit(function[0])) {
34 i = strtol(function, NULL, 0);
35 return &p->calls[i];
37 for (i=0;i<p->num_calls;i++) {
38 if (strcmp(p->calls[i].name, function) == 0) {
39 break;
42 if (i == p->num_calls) {
43 printf("Function '%s' not found\n", function);
44 exit(1);
46 return &p->calls[i];
50 static void show_pipes(void)
52 const struct dcerpc_interface_list *l;
53 printf("\nYou must specify a pipe\n");
54 printf("known pipes are:\n");
55 for (l=librpc_dcerpc_pipes();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 static void show_functions(const struct dcerpc_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 = (char *) talloc_realloc(
86 mem_ctx, result, char *, total_len + num_read);
87 } else {
88 result = talloc_size(mem_ctx, 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 const struct dcerpc_interface_table *load_iface_from_plugin(const char *plugin, const char *pipe_name)
104 const struct dcerpc_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, "dcerpc_table_%s", pipe_name);
115 p = 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 return NULL;
123 talloc_free(symbol);
125 return p;
128 int main(int argc, const char *argv[])
130 const struct dcerpc_interface_table *p = NULL;
131 const struct dcerpc_interface_call *f;
132 const char *pipe_name, *function, *inout, *filename;
133 uint8_t *data;
134 size_t size;
135 DATA_BLOB blob;
136 struct ndr_pull *ndr_pull;
137 struct ndr_print *ndr_print;
138 TALLOC_CTX *mem_ctx;
139 int flags;
140 poptContext pc;
141 NTSTATUS status;
142 void *st;
143 void *v_st;
144 const char *ctx_filename = NULL;
145 const char *plugin = NULL;
146 BOOL validate = False;
147 BOOL dumpdata = False;
148 int opt;
149 struct poptOption long_options[] = {
150 {"context-file", 'c', POPT_ARG_STRING, &ctx_filename, 0, "In-filename to parse first", "CTX-FILE" },
151 {"validate", 0, POPT_ARG_NONE, &validate, 0, "try to validate the data", NULL },
152 {"dump-data", 0, POPT_ARG_NONE, &dumpdata, 0, "dump the hex data", NULL },
153 {"load-dso", 'l', POPT_ARG_STRING, &plugin, 0, "load from shared object file", NULL },
154 POPT_COMMON_SAMBA
155 POPT_AUTOHELP
156 POPT_TABLEEND
159 dcerpc_table_init();
161 pc = poptGetContext("ndrdump", argc, argv, long_options, 0);
163 poptSetOtherOptionHelp(
164 pc, "<pipe|uuid> <function> <inout> [<filename>]");
166 while ((opt = poptGetNextOpt(pc)) != -1) {
169 pipe_name = poptGetArg(pc);
171 if (!pipe_name) {
172 poptPrintUsage(pc, stderr, 0);
173 show_pipes();
174 exit(1);
177 if (plugin != NULL) {
178 p = load_iface_from_plugin(plugin, pipe_name);
181 if (!p) {
182 p = idl_iface_by_name(pipe_name);
185 if (!p) {
186 struct GUID uuid;
188 status = GUID_from_string(pipe_name, &uuid);
190 if (NT_STATUS_IS_OK(status)) {
191 p = idl_iface_by_uuid(&uuid);
195 if (!p) {
196 printf("Unknown pipe or UUID '%s'\n", pipe_name);
197 exit(1);
200 function = poptGetArg(pc);
201 inout = poptGetArg(pc);
202 filename = poptGetArg(pc);
204 if (!function || !inout) {
205 poptPrintUsage(pc, stderr, 0);
206 show_functions(p);
207 exit(1);
210 if (strcmp(inout, "in") == 0 ||
211 strcmp(inout, "request") == 0) {
212 flags = NDR_IN;
213 } else if (strcmp(inout, "out") == 0 ||
214 strcmp(inout, "response") == 0) {
215 flags = NDR_OUT;
216 } else {
217 printf("Bad inout value '%s'\n", inout);
218 exit(1);
221 f = find_function(p, function);
223 mem_ctx = talloc_init("ndrdump");
225 st = talloc_zero_size(mem_ctx, f->struct_size);
226 if (!st) {
227 printf("Unable to allocate %d bytes\n", (int)f->struct_size);
228 exit(1);
231 v_st = talloc_zero_size(mem_ctx, f->struct_size);
232 if (!v_st) {
233 printf("Unable to allocate %d bytes\n", (int)f->struct_size);
234 exit(1);
237 if (ctx_filename) {
238 if (flags == NDR_IN) {
239 printf("Context file can only be used for \"out\" packages\n");
240 exit(1);
243 data = (uint8_t *)file_load(ctx_filename, &size, mem_ctx);
244 if (!data) {
245 perror(ctx_filename);
246 exit(1);
249 blob.data = data;
250 blob.length = size;
252 ndr_pull = ndr_pull_init_blob(&blob, mem_ctx);
253 ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
255 status = f->ndr_pull(ndr_pull, NDR_IN, st);
257 if (ndr_pull->offset != ndr_pull->data_size) {
258 printf("WARNING! %d unread bytes while parsing context file\n", ndr_pull->data_size - ndr_pull->offset);
261 if (!NT_STATUS_IS_OK(status)) {
262 printf("pull for context file returned %s\n", nt_errstr(status));
263 exit(1);
265 memcpy(v_st, st, f->struct_size);
268 if (filename)
269 data = (uint8_t *)file_load(filename, &size, mem_ctx);
270 else
271 data = (uint8_t *)stdin_load(mem_ctx, &size);
273 if (!data) {
274 if (filename)
275 perror(filename);
276 else
277 perror("stdin");
278 exit(1);
281 blob.data = data;
282 blob.length = size;
284 ndr_pull = ndr_pull_init_blob(&blob, mem_ctx);
285 ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
287 status = f->ndr_pull(ndr_pull, flags, st);
289 printf("pull returned %s\n", nt_errstr(status));
291 if (ndr_pull->offset != ndr_pull->data_size) {
292 printf("WARNING! %d unread bytes\n", ndr_pull->data_size - ndr_pull->offset);
293 dump_data(0, ndr_pull->data+ndr_pull->offset, ndr_pull->data_size - ndr_pull->offset);
296 if (dumpdata) {
297 printf("%d bytes consumed\n", ndr_pull->offset);
298 dump_data(0, blob.data, blob.length);
301 ndr_print = talloc_zero(mem_ctx, struct ndr_print);
302 ndr_print->print = ndr_print_debug_helper;
303 ndr_print->depth = 1;
304 f->ndr_print(ndr_print, function, flags, st);
306 if (!NT_STATUS_IS_OK(status)) {
307 printf("dump FAILED\n");
308 exit(1);
311 if (validate) {
312 DATA_BLOB v_blob;
313 struct ndr_push *ndr_v_push;
314 struct ndr_pull *ndr_v_pull;
315 struct ndr_print *ndr_v_print;
317 ndr_v_push = ndr_push_init_ctx(mem_ctx);
319 status = f->ndr_push(ndr_v_push, flags, st);
320 if (!NT_STATUS_IS_OK(status)) {
321 printf("validate push FAILED\n");
322 exit(1);
325 v_blob = ndr_push_blob(ndr_v_push);
327 if (dumpdata) {
328 printf("%ld bytes generated (validate)\n", (long)v_blob.length);
329 dump_data(0, v_blob.data, v_blob.length);
332 ndr_v_pull = ndr_pull_init_blob(&v_blob, mem_ctx);
333 ndr_v_pull->flags |= LIBNDR_FLAG_REF_ALLOC;
335 status = f->ndr_pull(ndr_v_pull, flags, v_st);
336 if (!NT_STATUS_IS_OK(status)) {
337 printf("validate pull FAILED\n");
338 exit(1);
341 printf("pull returned %s\n", nt_errstr(status));
343 if (ndr_v_pull->offset != ndr_v_pull->data_size) {
344 printf("WARNING! %d unread bytes in validation\n", ndr_v_pull->data_size - ndr_v_pull->offset);
345 dump_data(0, ndr_v_pull->data+ndr_v_pull->offset, ndr_v_pull->data_size - ndr_v_pull->offset);
348 ndr_v_print = talloc_zero(mem_ctx, struct ndr_print);
349 ndr_v_print->print = ndr_print_debug_helper;
350 ndr_v_print->depth = 1;
351 f->ndr_print(ndr_v_print, function, flags, v_st);
353 if (blob.length != v_blob.length) {
354 printf("WARNING! orig bytes:%ld validated pushed bytes:%ld\n", (long)blob.length, (long)v_blob.length);
357 if (ndr_pull->offset != ndr_v_pull->offset) {
358 printf("WARNING! orig pulled bytes:%d validated pulled bytes:%d\n", ndr_pull->offset, ndr_v_pull->offset);
362 printf("dump OK\n");
364 talloc_free(mem_ctx);
366 poptFreeContext(pc);
368 return 0;