r1481: add idl file and torture test dummies
[kai/samba.git] / source4 / torture / rpc / drsuapi.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    DRSUapi tests
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Stefan (metze) Metzmacher 2004
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26
27 static void reopen(struct dcerpc_pipe **p, const struct dcerpc_interface_table *iface)
28 {
29         NTSTATUS status;
30
31         if (*p) {
32                 dcerpc_pipe_close(*p);
33         }
34
35         status = torture_rpc_connection(p, iface->endpoints->names[0], iface->uuid, iface->if_version);
36         if (!NT_STATUS_IS_OK(status)) {
37                 printf("Failed to reopen '%s' - %s\n", iface->name, nt_errstr(status));
38                 exit(1);
39         }
40 }
41
42 static void print_depth(int depth)
43 {
44         int i;
45         for (i=0;i<depth;i++) {
46                 printf("    ");
47         }
48 }
49
50 static void test_ptr_scan(TALLOC_CTX *mem_ctx, const struct dcerpc_interface_table *iface, 
51                           int opnum, DATA_BLOB *base_in, int min_ofs, int max_ofs, int depth);
52
53 static void try_expand(TALLOC_CTX *mem_ctx, const struct dcerpc_interface_table *iface, 
54                        int opnum, DATA_BLOB *base_in, int insert_ofs, int depth)
55 {
56         DATA_BLOB stub_in, stub_out;
57         int n;
58         NTSTATUS status;
59         struct dcerpc_pipe *p = NULL;
60
61         reopen(&p, iface);
62
63         /* work out how much to expand to get a non fault */
64         for (n=0;n<2000;n++) {
65                 stub_in = data_blob(NULL, base_in->length + n);
66                 data_blob_clear(&stub_in);
67                 memcpy(stub_in.data, base_in->data, insert_ofs);
68                 memcpy(stub_in.data+insert_ofs+n, base_in->data+insert_ofs, base_in->length-insert_ofs);
69
70                 status = dcerpc_request(p, opnum, mem_ctx, &stub_in, &stub_out);
71
72                 if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
73                         print_depth(depth);
74                         printf("expand by %d gives %s\n", n, nt_errstr(status));
75                         if (n >= 4) {
76                                 test_ptr_scan(mem_ctx, iface, opnum, &stub_in, 
77                                               insert_ofs, insert_ofs+n, depth+1);
78                         }
79                         return;
80                 } else {
81 #if 0
82                         print_depth(depth);
83                         printf("expand by %d gives fault 0x%x\n", n, p->last_fault_code);
84 #endif
85                 }
86                 if (p->last_fault_code == 5) {
87                         reopen(&p, iface);
88                 }
89         }
90
91         dcerpc_pipe_close(p);   
92 }
93
94
95 static void test_ptr_scan(TALLOC_CTX *mem_ctx, const struct dcerpc_interface_table *iface, 
96                           int opnum, DATA_BLOB *base_in, int min_ofs, int max_ofs, int depth)
97 {
98         DATA_BLOB stub_in, stub_out;
99         int ofs;
100         NTSTATUS status;
101         struct dcerpc_pipe *p = NULL;
102
103         reopen(&p, iface);
104
105         stub_in = data_blob(NULL, base_in->length);
106         memcpy(stub_in.data, base_in->data, base_in->length);
107
108         /* work out which elements are pointers */
109         for (ofs=min_ofs;ofs<=max_ofs-4;ofs+=4) {
110                 SIVAL(stub_in.data, ofs, 1);
111                 status = dcerpc_request(p, opnum, mem_ctx, &stub_in, &stub_out);
112
113                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
114                         print_depth(depth);
115                         printf("possible ptr at ofs %d - fault 0x%08x\n", 
116                                ofs-min_ofs, p->last_fault_code);
117                         if (p->last_fault_code == 5) {
118                                 reopen(&p, iface);
119                         }
120                         if (depth == 0) {
121                                 try_expand(mem_ctx, iface, opnum, &stub_in, ofs+4, depth+1);
122                         } else {
123                                 try_expand(mem_ctx, iface, opnum, &stub_in, max_ofs, depth+1);
124                         }
125                         SIVAL(stub_in.data, ofs, 0);
126                         continue;
127                 }
128                 SIVAL(stub_in.data, ofs, 0);
129         }
130
131         dcerpc_pipe_close(p);   
132 }
133         
134
135 static void test_scan_call(TALLOC_CTX *mem_ctx, const struct dcerpc_interface_table *iface, int opnum)
136 {
137         DATA_BLOB stub_in, stub_out;
138         int i;
139         NTSTATUS status;
140         struct dcerpc_pipe *p = NULL;
141
142         reopen(&p, iface);
143
144         /* work out the minimum amount of input data */
145         for (i=0;i<2000;i++) {
146                 stub_in = data_blob(NULL, i);
147                 data_blob_clear(&stub_in);
148
149
150                 status = dcerpc_request(p, opnum, mem_ctx, &stub_in, &stub_out);
151
152                 if (NT_STATUS_IS_OK(status)) {
153                         printf("opnum %d   min_input %d - output %d\n", 
154                                opnum, stub_in.length, stub_out.length);
155                         dump_data(0, stub_out.data, stub_out.length);
156                         dcerpc_pipe_close(p);
157                         test_ptr_scan(mem_ctx, iface, opnum, &stub_in, 0, stub_in.length, 0);
158                         return;
159                 }
160
161                 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
162                         printf("opnum %d  size %d fault 0x%08x\n", opnum, i, p->last_fault_code);
163                         if (p->last_fault_code == 5) {
164                                 reopen(&p, iface);
165                         }
166                         continue;
167                 }
168
169                 printf("opnum %d  size %d error %s\n", opnum, i, nt_errstr(status));
170         }
171
172         printf("opnum %d minimum not found!?\n", opnum);
173         dcerpc_pipe_close(p);
174 }
175
176
177 static BOOL test_scan(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
178 {
179         test_scan_call(mem_ctx, &dcerpc_table_drsuapi, 0x0);
180         return True;
181 }
182
183 static BOOL test_DRSBind(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
184 {
185         NTSTATUS status;
186         struct DRSUAPI_BIND r;
187         BOOL ret = True;
188
189         ZERO_STRUCT(r.in.blob);
190
191         status = dcerpc_DRSUAPI_BIND(p, mem_ctx, &r);
192         if (!NT_STATUS_IS_OK(status)) {
193                 printf("DRSUAPI_BIND level failed - %s\n", nt_errstr(status));
194                 ret = False;
195         }
196
197         return ret;
198 }
199
200 BOOL torture_rpc_drsuapi(int dummy)
201 {
202         NTSTATUS status;
203         struct dcerpc_pipe *p;
204         TALLOC_CTX *mem_ctx;
205         BOOL ret = True;
206
207         mem_ctx = talloc_init("torture_rpc_srvsvc");
208
209         status = torture_rpc_connection(&p,
210                                         DCERPC_DRSUAPI_NAME,
211                                         DCERPC_DRSUAPI_UUID,
212                                         DCERPC_DRSUAPI_VERSION);
213         if (!NT_STATUS_IS_OK(status)) {
214                 return False;
215         }
216
217         if (!test_DRSBind(p, mem_ctx)) {
218                 ret = False;
219         }
220
221         if (!test_scan(p, mem_ctx)) {
222                 ret = False;
223         }
224
225         talloc_destroy(mem_ctx);
226
227         torture_rpc_close(p);
228
229         return ret;
230 }