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