r152: a quick airport commit ....
[samba.git] / source4 / utils / ndrdump.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB torture tester
4    Copyright (C) Andrew Tridgell 2003
5    
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.
10    
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.
15    
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.
19 */
20
21 #include "includes.h"
22
23 static const struct dcerpc_interface_table *find_pipe(const char *pipe_name)
24 {
25         int i;
26         for (i=0;dcerpc_pipes[i];i++) {
27                 if (strcmp(dcerpc_pipes[i]->name, pipe_name) == 0) {
28                         break;
29                 }
30         }
31         if (!dcerpc_pipes[i]) {
32                 printf("pipe '%s' not in table\n", pipe_name);
33                 exit(1);
34         }
35         return dcerpc_pipes[i];
36 }
37
38 static const struct dcerpc_interface_call *find_function(
39         const struct dcerpc_interface_table *p,
40         const char *function)
41 {
42         int i;
43         for (i=0;i<p->num_calls;i++) {
44                 if (strcmp(p->calls[i].name, function) == 0) {
45                         break;
46                 }
47         }
48         if (i == p->num_calls) {
49                 printf("Function '%s' not found\n", function);
50                 exit(1);
51         }
52         return &p->calls[i];
53 }
54
55 static void usage(void)
56 {
57         printf("Usage: ndrdump <pipe> <function> <inout> <filename>\n");
58 }
59
60
61 static void show_pipes(void)
62 {
63         int i;
64         usage();
65         printf("\nYou must specify a pipe\n");
66         printf("known pipes are:\n");
67         for (i=0;dcerpc_pipes[i];i++) {
68                 printf("\t%s\n", dcerpc_pipes[i]->name);
69         }
70         exit(1);
71 }
72
73 static void show_functions(const struct dcerpc_interface_table *p)
74 {
75         int i;
76         usage();
77         printf("\nYou must specify a function\n");
78         printf("known functions on '%s' are:\n", p->name);
79         for (i=0;i<p->num_calls;i++) {
80                 printf("\t0x%02x (%2d) %s\n", i, i, p->calls[i].name);
81         }
82         exit(1);
83 }
84
85 int main(int argc, char *argv[])
86 {
87         const struct dcerpc_interface_table *p;
88         const struct dcerpc_interface_call *f;
89         const char *pipe_name, *function, *inout, *filename;
90         char *data;
91         size_t size;
92         DATA_BLOB blob;
93         struct ndr_pull *ndr;
94         TALLOC_CTX *mem_ctx;
95         int flags;
96         NTSTATUS status;
97         void *st;
98         struct ndr_print pr;
99
100         DEBUGLEVEL = 10;
101
102         setup_logging("smbtorture", DEBUG_STDOUT);
103
104         if (argc < 2) {
105                 show_pipes();
106                 exit(1);
107         }
108
109         pipe_name = argv[1];
110
111         p = find_pipe(pipe_name);
112
113         if (argc < 5) {
114                 show_functions(p);
115                 exit(1);
116         }
117
118         function = argv[2];
119         inout = argv[3];
120         filename = argv[4];
121
122         if (strcmp(inout, "in") == 0 ||
123             strcmp(inout, "request") == 0) {
124                 flags = NDR_IN;
125         } else if (strcmp(inout, "out") == 0 ||
126                    strcmp(inout, "response") == 0) {
127                 flags = NDR_OUT;
128         } else {
129                 printf("Bad inout value '%s'\n", inout);
130                 exit(1);
131         }
132
133         f = find_function(p, function);
134
135         data = file_load(filename, &size);
136         if (!data) {
137                 perror(filename);
138                 exit(1);
139         }
140
141         blob.data = data;
142         blob.length = size;
143
144         mem_ctx = talloc_init("ndrdump");
145         
146         ndr = ndr_pull_init_blob(&blob, mem_ctx);
147
148         st = talloc_zero(mem_ctx, f->struct_size);
149         if (!st) {
150                 printf("Unable to allocate %d bytes\n", f->struct_size);
151                 exit(1);
152         }
153
154         if (flags == NDR_OUT) {
155                 ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
156         }
157
158         status = f->ndr_pull(ndr, flags, st);
159
160         printf("pull returned %s\n", nt_errstr(status));
161
162         if (ndr->offset != ndr->data_size) {
163                 printf("WARNING! %d unread bytes\n", ndr->data_size - ndr->offset);
164         }
165
166         pr.mem_ctx = mem_ctx;
167         pr.print = ndr_print_debug_helper;
168         pr.depth = 1;
169         f->ndr_print(&pr, function, flags, st);
170
171         if (!NT_STATUS_IS_OK(status) ||
172             ndr->offset != ndr->data_size) {
173                 printf("dump FAILED\n");
174                 exit(1);
175         }
176
177         printf("dump OK\n");
178         
179         return 0;
180 }