r8520: fixed a pile of warnings from the build farm gcc -Wall output on
[jelmer/samba4-debian.git] / source / 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 #include "lib/cmdline/popt_common.h"
23 #include "system/iconv.h"
24 #include "system/filesys.h"
25
26 static const struct dcerpc_interface_call *find_function(
27         const struct dcerpc_interface_table *p,
28         const char *function)
29 {
30         int i;
31         if (isdigit(function[0])) {
32                 i = strtol(function, NULL, 0);
33                 return &p->calls[i];
34         }
35         for (i=0;i<p->num_calls;i++) {
36                 if (strcmp(p->calls[i].name, function) == 0) {
37                         break;
38                 }
39         }
40         if (i == p->num_calls) {
41                 printf("Function '%s' not found\n", function);
42                 exit(1);
43         }
44         return &p->calls[i];
45 }
46
47
48 static void show_pipes(void)
49 {
50         const struct dcerpc_interface_list *l;
51         printf("\nYou must specify a pipe\n");
52         printf("known pipes are:\n");
53         for (l=librpc_dcerpc_pipes();l;l=l->next) {
54                 if(l->table->helpstring) {
55                         printf("\t%s - %s\n", l->table->name, l->table->helpstring);
56                 } else {
57                         printf("\t%s\n", l->table->name);
58                 }
59         }
60         exit(1);
61 }
62
63 static void show_functions(const struct dcerpc_interface_table *p)
64 {
65         int i;
66         printf("\nYou must specify a function\n");
67         printf("known functions on '%s' are:\n", p->name);
68         for (i=0;i<p->num_calls;i++) {
69                 printf("\t0x%02x (%2d) %s\n", i, i, p->calls[i].name);
70         }
71         exit(1);
72 }
73
74 static char *stdin_load(TALLOC_CTX *mem_ctx, size_t *size)
75 {
76         int num_read, total_len = 0;
77         char buf[255];
78         char *result = NULL;
79
80         while((num_read = read(STDIN_FILENO, buf, 255)) > 0) {
81
82                 if (result) {
83                         result = (char *) talloc_realloc(
84                                 mem_ctx, result, char *, total_len + num_read);
85                 } else {
86                         result = talloc_size(mem_ctx, num_read);
87                 }
88
89                 memcpy(result + total_len, buf, num_read);
90
91                 total_len += num_read;
92         }
93
94         if (size)
95                 *size = total_len;
96
97         return result;
98 }
99
100  int main(int argc, const char *argv[])
101 {
102         const struct dcerpc_interface_table *p;
103         const struct dcerpc_interface_call *f;
104         const char *pipe_name, *function, *inout, *filename;
105         uint8_t *data;
106         size_t size;
107         DATA_BLOB blob;
108         struct ndr_pull *ndr;
109         TALLOC_CTX *mem_ctx;
110         int flags;
111         poptContext pc;
112         NTSTATUS status;
113         void *st;
114         const char *ctx_filename = NULL;
115         int opt;
116         struct ndr_print *pr;
117         struct poptOption long_options[] = {
118                 {"context-file", 'c', POPT_ARG_STRING, &ctx_filename, 0, "In-filename to parse first", "CTX-FILE" },
119                 POPT_COMMON_SAMBA
120                 POPT_AUTOHELP
121                 POPT_TABLEEND
122         };
123
124         ndrdump_init_subsystems;
125
126         pc = poptGetContext("ndrdump", argc, argv, long_options, 0);
127         
128         poptSetOtherOptionHelp(
129                 pc, "<pipe|uuid> <function> <inout> [<filename>]");
130
131         while ((opt = poptGetNextOpt(pc)) != -1) {
132         }
133
134         pipe_name = poptGetArg(pc);
135
136         if (!pipe_name) {
137                 poptPrintUsage(pc, stderr, 0);
138                 show_pipes();
139                 exit(1);
140         }
141
142         p = idl_iface_by_name(pipe_name);
143
144         if (!p) {
145
146                 p = idl_iface_by_uuid(pipe_name);
147
148                 if (!p) {
149                         printf("Unknown pipe or UUID '%s'\n", pipe_name);
150                         exit(1);
151                 }
152         }
153
154         function = poptGetArg(pc);
155         inout = poptGetArg(pc);
156         filename = poptGetArg(pc);
157
158         if (!function || !inout) {
159                 poptPrintUsage(pc, stderr, 0);
160                 show_functions(p);
161                 exit(1);
162         }
163
164         if (strcmp(inout, "in") == 0 ||
165             strcmp(inout, "request") == 0) {
166                 flags = NDR_IN;
167         } else if (strcmp(inout, "out") == 0 ||
168                    strcmp(inout, "response") == 0) {
169                 flags = NDR_OUT;
170         } else {
171                 printf("Bad inout value '%s'\n", inout);
172                 exit(1);
173         }
174
175         f = find_function(p, function);
176
177         mem_ctx = talloc_init("ndrdump");
178
179         st = talloc_zero_size(mem_ctx, f->struct_size);
180         if (!st) {
181                 printf("Unable to allocate %d bytes\n", (int)f->struct_size);
182                 exit(1);
183         }
184
185         if (ctx_filename) {
186                 if (flags == NDR_IN) {
187                         printf("Context file can only be used for \"out\" packages\n");
188                         exit(1);
189                 }
190                         
191                 data = (uint8_t *)file_load(ctx_filename, &size, mem_ctx);
192                 if (!data) {
193                         perror(ctx_filename);
194                         exit(1);
195                 }
196
197                 blob.data = data;
198                 blob.length = size;
199
200                 ndr = ndr_pull_init_blob(&blob, mem_ctx);
201                 ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
202
203                 status = f->ndr_pull(ndr, NDR_IN, st);
204
205                 if (ndr->offset != ndr->data_size) {
206                         printf("WARNING! %d unread bytes while parsing context file\n", ndr->data_size - ndr->offset);
207                 }
208
209                 if (!NT_STATUS_IS_OK(status)) {
210                         printf("pull for context file returned %s\n", nt_errstr(status));
211                         exit(1);
212                 }
213         } 
214
215         if (filename)
216                 data = (uint8_t *)file_load(filename, &size, mem_ctx);
217         else
218                 data = (uint8_t *)stdin_load(mem_ctx, &size);
219
220         if (!data) {
221                 if (filename)
222                         perror(filename);
223                 else
224                         perror("stdin");
225                 exit(1);
226         }
227
228         blob.data = data;
229         blob.length = size;
230
231         ndr = ndr_pull_init_blob(&blob, mem_ctx);
232         ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
233
234         status = f->ndr_pull(ndr, flags, st);
235
236         printf("pull returned %s\n", nt_errstr(status));
237
238         if (ndr->offset != ndr->data_size) {
239                 printf("WARNING! %d unread bytes\n", ndr->data_size - ndr->offset);
240                 dump_data(0, ndr->data+ndr->offset, ndr->data_size - ndr->offset);
241         }
242
243         pr = talloc(NULL, struct ndr_print);
244         pr->print = ndr_print_debug_helper;
245         pr->depth = 1;
246         pr->flags = 0;
247         f->ndr_print(pr, function, flags, st);
248
249         if (!NT_STATUS_IS_OK(status) ||
250             ndr->offset != ndr->data_size) {
251                 printf("dump FAILED\n");
252                 exit(1);
253         }
254
255         printf("dump OK\n");
256
257         talloc_free(pr);
258         
259         poptFreeContext(pc);
260         
261         return 0;
262 }