s4:tortore/rpc/spoolss: some compilers don't like .foo.bar = 5
[Samba/gebeck_regimport.git] / source4 / torture / rpc / autoidl.c
blobee76ddd05b8d7bbf13d6bfc54a53182a14c8da91
1 /*
2 Unix SMB/CIFS implementation.
4 auto-idl scanner
6 Copyright (C) Andrew Tridgell 2003
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.
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.
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/>.
22 #include "includes.h"
23 #include "librpc/gen_ndr/ndr_drsuapi_c.h"
24 #include "librpc/ndr/ndr_table.h"
25 #include "torture/rpc/rpc.h"
28 #if 1
30 get a DRSUAPI policy handle
32 static bool get_policy_handle(struct dcerpc_binding_handle *b,
33 TALLOC_CTX *mem_ctx,
34 struct policy_handle *handle)
36 NTSTATUS status;
37 struct drsuapi_DsBind r;
39 ZERO_STRUCT(r);
40 r.out.bind_handle = handle;
42 status = dcerpc_drsuapi_DsBind_r(b, mem_ctx, &r);
43 if (!NT_STATUS_IS_OK(status)) {
44 printf("drsuapi_DsBind failed - %s\n", nt_errstr(status));
45 return false;
48 return true;
50 #else
52 get a SAMR handle
54 static bool get_policy_handle(struct dcerpc_binding_handle *b,
55 TALLOC_CTX *mem_ctx,
56 struct policy_handle *handle)
58 NTSTATUS status;
59 struct samr_Connect r;
61 r.in.system_name = 0;
62 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
63 r.out.connect_handle = handle;
65 status = dcerpc_samr_Connect_r(b, mem_ctx, &r);
66 if (!NT_STATUS_IS_OK(status)) {
67 printf("samr_Connect failed - %s\n", nt_errstr(status));
68 return false;
71 return true;
73 #endif
75 static void fill_blob_handle(DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
76 struct policy_handle *handle)
78 DATA_BLOB b2;
80 if (blob->length < 20) {
81 return;
84 ndr_push_struct_blob(&b2, mem_ctx, NULL, handle, (ndr_push_flags_fn_t)ndr_push_policy_handle);
86 memcpy(blob->data, b2.data, 20);
89 static void reopen(struct torture_context *tctx,
90 struct dcerpc_pipe **p,
91 const struct ndr_interface_table *iface)
93 NTSTATUS status;
95 talloc_free(*p);
97 status = torture_rpc_connection(tctx, p, iface);
98 if (!NT_STATUS_IS_OK(status)) {
99 printf("Failed to reopen '%s' - %s\n", iface->name, nt_errstr(status));
100 exit(1);
104 static void print_depth(int depth)
106 int i;
107 for (i=0;i<depth;i++) {
108 printf(" ");
112 static void test_ptr_scan(struct torture_context *tctx, const struct ndr_interface_table *iface,
113 int opnum, DATA_BLOB *base_in, int min_ofs, int max_ofs, int depth);
115 static void try_expand(struct torture_context *tctx, const struct ndr_interface_table *iface,
116 int opnum, DATA_BLOB *base_in, int insert_ofs, int depth)
118 DATA_BLOB stub_in, stub_out;
119 int n;
120 NTSTATUS status;
121 struct dcerpc_pipe *p = NULL;
123 reopen(tctx, &p, iface);
125 /* work out how much to expand to get a non fault */
126 for (n=0;n<2000;n++) {
127 stub_in = data_blob(NULL, base_in->length + n);
128 data_blob_clear(&stub_in);
129 memcpy(stub_in.data, base_in->data, insert_ofs);
130 memcpy(stub_in.data+insert_ofs+n, base_in->data+insert_ofs, base_in->length-insert_ofs);
132 status = dcerpc_request(p, NULL, opnum, tctx, &stub_in, &stub_out);
134 if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
135 print_depth(depth);
136 printf("expand by %d gives %s\n", n, nt_errstr(status));
137 if (n >= 4) {
138 test_ptr_scan(tctx, iface, opnum, &stub_in,
139 insert_ofs, insert_ofs+n, depth+1);
141 return;
142 } else {
143 #if 0
144 print_depth(depth);
145 printf("expand by %d gives fault %s\n", n, dcerpc_errstr(tctx, p->last_fault_code));
146 #endif
148 if (p->last_fault_code == 5) {
149 reopen(tctx, &p, iface);
153 talloc_free(p);
157 static void test_ptr_scan(struct torture_context *tctx, const struct ndr_interface_table *iface,
158 int opnum, DATA_BLOB *base_in, int min_ofs, int max_ofs, int depth)
160 DATA_BLOB stub_in, stub_out;
161 int ofs;
162 NTSTATUS status;
163 struct dcerpc_pipe *p = NULL;
165 reopen(tctx, &p, iface);
167 stub_in = data_blob(NULL, base_in->length);
168 memcpy(stub_in.data, base_in->data, base_in->length);
170 /* work out which elements are pointers */
171 for (ofs=min_ofs;ofs<=max_ofs-4;ofs+=4) {
172 SIVAL(stub_in.data, ofs, 1);
173 status = dcerpc_request(p, NULL, opnum, tctx, &stub_in, &stub_out);
175 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
176 print_depth(depth);
177 printf("possible ptr at ofs %d - fault %s\n",
178 ofs-min_ofs, dcerpc_errstr(tctx, p->last_fault_code));
179 if (p->last_fault_code == 5) {
180 reopen(tctx, &p, iface);
182 if (depth == 0) {
183 try_expand(tctx, iface, opnum, &stub_in, ofs+4, depth+1);
184 } else {
185 try_expand(tctx, iface, opnum, &stub_in, max_ofs, depth+1);
187 SIVAL(stub_in.data, ofs, 0);
188 continue;
190 SIVAL(stub_in.data, ofs, 0);
193 talloc_free(p);
197 static void test_scan_call(struct torture_context *tctx, const struct ndr_interface_table *iface, int opnum)
199 DATA_BLOB stub_in, stub_out;
200 int i;
201 NTSTATUS status;
202 struct dcerpc_pipe *p = NULL;
203 struct policy_handle handle;
205 reopen(tctx, &p, iface);
207 get_policy_handle(p->binding_handle, tctx, &handle);
209 /* work out the minimum amount of input data */
210 for (i=0;i<2000;i++) {
211 stub_in = data_blob(NULL, i);
212 data_blob_clear(&stub_in);
215 status = dcerpc_request(p, NULL, opnum, tctx, &stub_in, &stub_out);
217 if (NT_STATUS_IS_OK(status)) {
218 printf("opnum %d min_input %d - output %d\n",
219 opnum, (int)stub_in.length, (int)stub_out.length);
220 dump_data(0, stub_out.data, stub_out.length);
221 talloc_free(p);
222 test_ptr_scan(tctx, iface, opnum, &stub_in, 0, stub_in.length, 0);
223 return;
226 fill_blob_handle(&stub_in, tctx, &handle);
228 status = dcerpc_request(p, NULL, opnum, tctx, &stub_in, &stub_out);
230 if (NT_STATUS_IS_OK(status)) {
231 printf("opnum %d min_input %d - output %d (with handle)\n",
232 opnum, (int)stub_in.length, (int)stub_out.length);
233 dump_data(0, stub_out.data, stub_out.length);
234 talloc_free(p);
235 test_ptr_scan(tctx, iface, opnum, &stub_in, 0, stub_in.length, 0);
236 return;
239 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
240 printf("opnum %d size %d fault %s\n", opnum, i, dcerpc_errstr(tctx, p->last_fault_code));
241 if (p->last_fault_code == 5) {
242 reopen(tctx, &p, iface);
244 continue;
247 printf("opnum %d size %d error %s\n", opnum, i, nt_errstr(status));
250 printf("opnum %d minimum not found!?\n", opnum);
251 talloc_free(p);
255 static void test_auto_scan(struct torture_context *tctx, const struct ndr_interface_table *iface)
257 test_scan_call(tctx, iface, 2);
260 bool torture_rpc_autoidl(struct torture_context *torture)
262 const struct ndr_interface_table *iface;
264 iface = ndr_table_by_name("drsuapi");
265 if (!iface) {
266 printf("Unknown interface!\n");
267 return false;
270 printf("\nProbing pipe '%s'\n", iface->name);
272 test_auto_scan(torture, iface);
274 return true;