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