49d0a5bcb806454911f8f767b6e7eaa95c64e38d
[samba.git] / source / torture / rpc / autoidl.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    auto-idl scanner
5
6    Copyright (C) Andrew Tridgell 2003
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "librpc/gen_ndr/ndr_drsuapi.h"
25
26
27 #if 1
28 /*
29   get a DRSUAPI policy handle
30 */
31 static BOOL get_policy_handle(struct dcerpc_pipe *p,
32                               TALLOC_CTX *mem_ctx, 
33                               struct policy_handle *handle)
34 {
35         NTSTATUS status;
36         struct drsuapi_DsBind r;
37
38         ZERO_STRUCT(r);
39         r.out.bind_handle = handle;
40
41         status = dcerpc_drsuapi_DsBind(p, mem_ctx, &r);
42         if (!NT_STATUS_IS_OK(status)) {
43                 printf("drsuapi_DsBind failed - %s\n", nt_errstr(status));
44                 return False;
45         }
46
47         return True;
48 }
49 #else
50 /*
51   get a SAMR handle
52 */
53 static BOOL get_policy_handle(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
54                               struct policy_handle *handle)
55 {
56         NTSTATUS status;
57         struct samr_Connect r;
58
59         r.in.system_name = 0;
60         r.in.access_mask = SEC_RIGHTS_MAXIMUM_ALLOWED;
61         r.out.connect_handle = handle;
62
63         status = dcerpc_samr_Connect(p, mem_ctx, &r);
64         if (!NT_STATUS_IS_OK(status)) {
65                 printf("samr_Connect failed - %s\n", nt_errstr(status));
66                 return False;
67         }
68
69         return True;
70 }
71 #endif
72
73 static void fill_blob_handle(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
74                              struct policy_handle *handle)
75 {
76         DATA_BLOB b2;
77
78         if (blob->length < 20) {
79                 return;
80         }
81
82         ndr_push_struct_blob(&b2, mem_ctx, handle, (ndr_push_flags_fn_t)ndr_push_policy_handle);
83
84         memcpy(blob->data, b2.data, 20);
85 }
86
87 static void reopen(struct dcerpc_pipe **p, const struct dcerpc_interface_table *iface)
88 {
89         NTSTATUS status;
90
91         if (*p) {
92                 dcerpc_pipe_close(*p);
93         }
94
95         status = torture_rpc_connection(p, iface->endpoints->names[0], iface->uuid, iface->if_version);
96         if (!NT_STATUS_IS_OK(status)) {
97                 printf("Failed to reopen '%s' - %s\n", iface->name, nt_errstr(status));
98                 exit(1);
99         }
100 }
101
102 static void print_depth(int depth)
103 {
104         int i;
105         for (i=0;i<depth;i++) {
106                 printf("    ");
107         }
108 }
109
110 static void test_ptr_scan(TALLOC_CTX *mem_ctx, const struct dcerpc_interface_table *iface, 
111                           int opnum, DATA_BLOB *base_in, int min_ofs, int max_ofs, int depth);
112
113 static void try_expand(TALLOC_CTX *mem_ctx, const struct dcerpc_interface_table *iface, 
114                        int opnum, DATA_BLOB *base_in, int insert_ofs, int depth)
115 {
116         DATA_BLOB stub_in, stub_out;
117         int n;
118         NTSTATUS status;
119         struct dcerpc_pipe *p = NULL;
120
121         reopen(&p, iface);
122
123         /* work out how much to expand to get a non fault */
124         for (n=0;n<2000;n++) {
125                 stub_in = data_blob(NULL, base_in->length + n);
126                 data_blob_clear(&stub_in);
127                 memcpy(stub_in.data, base_in->data, insert_ofs);
128                 memcpy(stub_in.data+insert_ofs+n, base_in->data+insert_ofs, base_in->length-insert_ofs);
129
130                 status = dcerpc_request(p, NULL, opnum, mem_ctx, &stub_in, &stub_out);
131
132                 if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
133                         print_depth(depth);
134                         printf("expand by %d gives %s\n", n, nt_errstr(status));
135                         if (n >= 4) {
136                                 test_ptr_scan(mem_ctx, iface, opnum, &stub_in, 
137                                               insert_ofs, insert_ofs+n, depth+1);
138                         }
139                         return;
140                 } else {
141 #if 0
142                         print_depth(depth);
143                         printf("expand by %d gives fault %s\n", n, dcerpc_errstr(mem_ctx, p->last_fault_code));
144 #endif
145                 }
146                 if (p->last_fault_code == 5) {
147                         reopen(&p, iface);
148                 }
149         }
150
151         dcerpc_pipe_close(p);   
152 }
153
154
155 static void test_ptr_scan(TALLOC_CTX *mem_ctx, const struct dcerpc_interface_table *iface, 
156                           int opnum, DATA_BLOB *base_in, int min_ofs, int max_ofs, int depth)
157 {
158         DATA_BLOB stub_in, stub_out;
159         int ofs;
160         NTSTATUS status;
161         struct dcerpc_pipe *p = NULL;
162
163         reopen(&p, iface);
164
165         stub_in = data_blob(NULL, base_in->length);
166         memcpy(stub_in.data, base_in->data, base_in->length);
167
168         /* work out which elements are pointers */
169         for (ofs=min_ofs;ofs<=max_ofs-4;ofs+=4) {
170                 SIVAL(stub_in.data, ofs, 1);
171                 status = dcerpc_request(p, NULL, opnum, mem_ctx, &stub_in, &stub_out);
172
173                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
174                         print_depth(depth);
175                         printf("possible ptr at ofs %d - fault %s\n", 
176                                ofs-min_ofs, dcerpc_errstr(mem_ctx, p->last_fault_code));
177                         if (p->last_fault_code == 5) {
178                                 reopen(&p, iface);
179                         }
180                         if (depth == 0) {
181                                 try_expand(mem_ctx, iface, opnum, &stub_in, ofs+4, depth+1);
182                         } else {
183                                 try_expand(mem_ctx, iface, opnum, &stub_in, max_ofs, depth+1);
184                         }
185                         SIVAL(stub_in.data, ofs, 0);
186                         continue;
187                 }
188                 SIVAL(stub_in.data, ofs, 0);
189         }
190
191         dcerpc_pipe_close(p);   
192 }
193         
194
195 static void test_scan_call(TALLOC_CTX *mem_ctx, const struct dcerpc_interface_table *iface, int opnum)
196 {
197         DATA_BLOB stub_in, stub_out;
198         int i;
199         NTSTATUS status;
200         struct dcerpc_pipe *p = NULL;
201         struct policy_handle handle;
202
203         reopen(&p, iface);
204
205         get_policy_handle(p, mem_ctx, &handle);
206
207         /* work out the minimum amount of input data */
208         for (i=0;i<2000;i++) {
209                 stub_in = data_blob(NULL, i);
210                 data_blob_clear(&stub_in);
211
212
213                 status = dcerpc_request(p, NULL, opnum, mem_ctx, &stub_in, &stub_out);
214
215                 if (NT_STATUS_IS_OK(status)) {
216                         printf("opnum %d   min_input %d - output %d\n", 
217                                opnum, stub_in.length, stub_out.length);
218                         dump_data(0, stub_out.data, stub_out.length);
219                         dcerpc_pipe_close(p);
220                         test_ptr_scan(mem_ctx, iface, opnum, &stub_in, 0, stub_in.length, 0);
221                         return;
222                 }
223
224                 fill_blob_handle(&stub_in, mem_ctx, &handle);
225
226                 status = dcerpc_request(p, NULL, opnum, mem_ctx, &stub_in, &stub_out);
227
228                 if (NT_STATUS_IS_OK(status)) {
229                         printf("opnum %d   min_input %d - output %d (with handle)\n", 
230                                opnum, stub_in.length, stub_out.length);
231                         dump_data(0, stub_out.data, stub_out.length);
232                         dcerpc_pipe_close(p);
233                         test_ptr_scan(mem_ctx, iface, opnum, &stub_in, 0, stub_in.length, 0);
234                         return;
235                 }
236
237                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
238                         printf("opnum %d  size %d fault %s\n", opnum, i, dcerpc_errstr(mem_ctx, p->last_fault_code));
239                         if (p->last_fault_code == 5) {
240                                 reopen(&p, iface);
241                         }
242                         continue;
243                 }
244
245                 printf("opnum %d  size %d error %s\n", opnum, i, nt_errstr(status));
246         }
247
248         printf("opnum %d minimum not found!?\n", opnum);
249         dcerpc_pipe_close(p);
250 }
251
252
253 static void test_auto_scan(TALLOC_CTX *mem_ctx, const struct dcerpc_interface_table *iface)
254 {
255         test_scan_call(mem_ctx, iface, 2);
256 }
257
258 BOOL torture_rpc_autoidl(void)
259 {
260         TALLOC_CTX *mem_ctx;
261         const struct dcerpc_interface_table *iface;
262                 
263         iface = idl_iface_by_name("drsuapi");
264         if (!iface) {
265                 printf("Unknown interface!\n");
266                 return False;
267         }
268
269         mem_ctx = talloc_init("torture_rpc_autoidl");
270
271         printf("\nProbing pipe '%s'\n", iface->name);
272
273         test_auto_scan(mem_ctx, iface);
274
275         return True;
276 }