cc9d8a1641d343f044380d2319f94ed73ecdb1aa
[jelmer/samba4-debian.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 "torture/torture.h"
25 #include "librpc/gen_ndr/ndr_drsuapi.h"
26 #include "librpc/gen_ndr/ndr_misc.h"
27 #include "librpc/rpc/dcerpc_table.h"
28 #include "torture/rpc/rpc.h"
29
30
31 #if 1
32 /*
33   get a DRSUAPI policy handle
34 */
35 static BOOL get_policy_handle(struct dcerpc_pipe *p,
36                               TALLOC_CTX *mem_ctx, 
37                               struct policy_handle *handle)
38 {
39         NTSTATUS status;
40         struct drsuapi_DsBind r;
41
42         ZERO_STRUCT(r);
43         r.out.bind_handle = handle;
44
45         status = dcerpc_drsuapi_DsBind(p, mem_ctx, &r);
46         if (!NT_STATUS_IS_OK(status)) {
47                 printf("drsuapi_DsBind failed - %s\n", nt_errstr(status));
48                 return False;
49         }
50
51         return True;
52 }
53 #else
54 /*
55   get a SAMR handle
56 */
57 static BOOL get_policy_handle(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
58                               struct policy_handle *handle)
59 {
60         NTSTATUS status;
61         struct samr_Connect r;
62
63         r.in.system_name = 0;
64         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
65         r.out.connect_handle = handle;
66
67         status = dcerpc_samr_Connect(p, mem_ctx, &r);
68         if (!NT_STATUS_IS_OK(status)) {
69                 printf("samr_Connect failed - %s\n", nt_errstr(status));
70                 return False;
71         }
72
73         return True;
74 }
75 #endif
76
77 static void fill_blob_handle(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
78                              struct policy_handle *handle)
79 {
80         DATA_BLOB b2;
81
82         if (blob->length < 20) {
83                 return;
84         }
85
86         ndr_push_struct_blob(&b2, mem_ctx, handle, (ndr_push_flags_fn_t)ndr_push_policy_handle);
87
88         memcpy(blob->data, b2.data, 20);
89 }
90
91 static void reopen(TALLOC_CTX *mem_ctx, 
92                    struct dcerpc_pipe **p, 
93                    const struct dcerpc_interface_table *iface)
94 {
95         NTSTATUS status;
96
97         talloc_free(*p);
98
99         status = torture_rpc_connection(mem_ctx, 
100                                         p, iface);
101         if (!NT_STATUS_IS_OK(status)) {
102                 printf("Failed to reopen '%s' - %s\n", iface->name, nt_errstr(status));
103                 exit(1);
104         }
105 }
106
107 static void print_depth(int depth)
108 {
109         int i;
110         for (i=0;i<depth;i++) {
111                 printf("    ");
112         }
113 }
114
115 static void test_ptr_scan(TALLOC_CTX *mem_ctx, const struct dcerpc_interface_table *iface, 
116                           int opnum, DATA_BLOB *base_in, int min_ofs, int max_ofs, int depth);
117
118 static void try_expand(TALLOC_CTX *mem_ctx, const struct dcerpc_interface_table *iface, 
119                        int opnum, DATA_BLOB *base_in, int insert_ofs, int depth)
120 {
121         DATA_BLOB stub_in, stub_out;
122         int n;
123         NTSTATUS status;
124         struct dcerpc_pipe *p = NULL;
125
126         reopen(mem_ctx, &p, iface);
127
128         /* work out how much to expand to get a non fault */
129         for (n=0;n<2000;n++) {
130                 stub_in = data_blob(NULL, base_in->length + n);
131                 data_blob_clear(&stub_in);
132                 memcpy(stub_in.data, base_in->data, insert_ofs);
133                 memcpy(stub_in.data+insert_ofs+n, base_in->data+insert_ofs, base_in->length-insert_ofs);
134
135                 status = dcerpc_request(p, NULL, opnum, False, mem_ctx, &stub_in, &stub_out);
136
137                 if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
138                         print_depth(depth);
139                         printf("expand by %d gives %s\n", n, nt_errstr(status));
140                         if (n >= 4) {
141                                 test_ptr_scan(mem_ctx, iface, opnum, &stub_in, 
142                                               insert_ofs, insert_ofs+n, depth+1);
143                         }
144                         return;
145                 } else {
146 #if 0
147                         print_depth(depth);
148                         printf("expand by %d gives fault %s\n", n, dcerpc_errstr(mem_ctx, p->last_fault_code));
149 #endif
150                 }
151                 if (p->last_fault_code == 5) {
152                         reopen(mem_ctx, &p, iface);
153                 }
154         }
155
156         talloc_free(p); 
157 }
158
159
160 static void test_ptr_scan(TALLOC_CTX *mem_ctx, const struct dcerpc_interface_table *iface, 
161                           int opnum, DATA_BLOB *base_in, int min_ofs, int max_ofs, int depth)
162 {
163         DATA_BLOB stub_in, stub_out;
164         int ofs;
165         NTSTATUS status;
166         struct dcerpc_pipe *p = NULL;
167
168         reopen(mem_ctx, &p, iface);
169
170         stub_in = data_blob(NULL, base_in->length);
171         memcpy(stub_in.data, base_in->data, base_in->length);
172
173         /* work out which elements are pointers */
174         for (ofs=min_ofs;ofs<=max_ofs-4;ofs+=4) {
175                 SIVAL(stub_in.data, ofs, 1);
176                 status = dcerpc_request(p, NULL, opnum, False, mem_ctx, &stub_in, &stub_out);
177
178                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
179                         print_depth(depth);
180                         printf("possible ptr at ofs %d - fault %s\n", 
181                                ofs-min_ofs, dcerpc_errstr(mem_ctx, p->last_fault_code));
182                         if (p->last_fault_code == 5) {
183                                 reopen(mem_ctx, &p, iface);
184                         }
185                         if (depth == 0) {
186                                 try_expand(mem_ctx, iface, opnum, &stub_in, ofs+4, depth+1);
187                         } else {
188                                 try_expand(mem_ctx, iface, opnum, &stub_in, max_ofs, depth+1);
189                         }
190                         SIVAL(stub_in.data, ofs, 0);
191                         continue;
192                 }
193                 SIVAL(stub_in.data, ofs, 0);
194         }
195
196         talloc_free(p); 
197 }
198         
199
200 static void test_scan_call(TALLOC_CTX *mem_ctx, const struct dcerpc_interface_table *iface, int opnum)
201 {
202         DATA_BLOB stub_in, stub_out;
203         int i;
204         NTSTATUS status;
205         struct dcerpc_pipe *p = NULL;
206         struct policy_handle handle;
207
208         reopen(mem_ctx, &p, iface);
209
210         get_policy_handle(p, mem_ctx, &handle);
211
212         /* work out the minimum amount of input data */
213         for (i=0;i<2000;i++) {
214                 stub_in = data_blob(NULL, i);
215                 data_blob_clear(&stub_in);
216
217
218                 status = dcerpc_request(p, NULL, opnum, False, mem_ctx, &stub_in, &stub_out);
219
220                 if (NT_STATUS_IS_OK(status)) {
221                         printf("opnum %d   min_input %d - output %d\n", 
222                                opnum, (int)stub_in.length, (int)stub_out.length);
223                         dump_data(0, stub_out.data, stub_out.length);
224                         talloc_free(p);
225                         test_ptr_scan(mem_ctx, iface, opnum, &stub_in, 0, stub_in.length, 0);
226                         return;
227                 }
228
229                 fill_blob_handle(&stub_in, mem_ctx, &handle);
230
231                 status = dcerpc_request(p, NULL, opnum, False, mem_ctx, &stub_in, &stub_out);
232
233                 if (NT_STATUS_IS_OK(status)) {
234                         printf("opnum %d   min_input %d - output %d (with handle)\n", 
235                                opnum, (int)stub_in.length, (int)stub_out.length);
236                         dump_data(0, stub_out.data, stub_out.length);
237                         talloc_free(p);
238                         test_ptr_scan(mem_ctx, iface, opnum, &stub_in, 0, stub_in.length, 0);
239                         return;
240                 }
241
242                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
243                         printf("opnum %d  size %d fault %s\n", opnum, i, dcerpc_errstr(mem_ctx, p->last_fault_code));
244                         if (p->last_fault_code == 5) {
245                                 reopen(mem_ctx, &p, iface);
246                         }
247                         continue;
248                 }
249
250                 printf("opnum %d  size %d error %s\n", opnum, i, nt_errstr(status));
251         }
252
253         printf("opnum %d minimum not found!?\n", opnum);
254         talloc_free(p);
255 }
256
257
258 static void test_auto_scan(TALLOC_CTX *mem_ctx, const struct dcerpc_interface_table *iface)
259 {
260         test_scan_call(mem_ctx, iface, 2);
261 }
262
263 BOOL torture_rpc_autoidl(void)
264 {
265         TALLOC_CTX *mem_ctx;
266         const struct dcerpc_interface_table *iface;
267                 
268         iface = idl_iface_by_name("drsuapi");
269         if (!iface) {
270                 printf("Unknown interface!\n");
271                 return False;
272         }
273
274         mem_ctx = talloc_init("torture_rpc_autoidl");
275
276         printf("\nProbing pipe '%s'\n", iface->name);
277
278         test_auto_scan(mem_ctx, iface);
279
280         talloc_free(mem_ctx);
281         return True;
282 }