s4:rootdse LDB module - remove unused variable
[Samba/gebeck_regimport.git] / source3 / torture / scanner.c
blobd7f4d2eabfba29a21ba7d3d042ed322987736da7
1 /*
2 Unix SMB/CIFS implementation.
3 SMB torture tester - scanning functions
4 Copyright (C) Andrew Tridgell 2001
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "torture/proto.h"
23 #define VERBOSE 0
24 #define OP_MIN 0
25 #define OP_MAX 20
27 #define DATA_SIZE 1024
28 #define PARAM_SIZE 1024
30 /****************************************************************************
31 look for a partial hit
32 ****************************************************************************/
33 static void trans2_check_hit(const char *format, int op, int level, NTSTATUS status)
35 if (NT_STATUS_V(status) == NT_STATUS_V(NT_STATUS_INVALID_LEVEL) ||
36 NT_STATUS_V(status) == NT_STATUS_V(NT_STATUS_NOT_IMPLEMENTED) ||
37 NT_STATUS_V(status) == NT_STATUS_V(NT_STATUS_NOT_SUPPORTED) ||
38 NT_STATUS_V(status) == NT_STATUS_V(NT_STATUS_UNSUCCESSFUL) ||
39 NT_STATUS_V(status) == NT_STATUS_V(NT_STATUS_INVALID_INFO_CLASS)) {
40 return;
42 #if VERBOSE
43 printf("possible %s hit op=%3d level=%5d status=%s\n",
44 format, op, level, nt_errstr(status));
45 #endif
48 /****************************************************************************
49 check for existance of a trans2 call
50 ****************************************************************************/
51 static NTSTATUS try_trans2(struct cli_state *cli,
52 int op,
53 char *param, char *data,
54 int param_len, int data_len,
55 unsigned int *rparam_len, unsigned int *rdata_len)
57 uint16 setup = op;
58 char *rparam=NULL, *rdata=NULL;
60 if (!cli_send_trans(cli, SMBtrans2,
61 NULL, /* name */
62 -1, 0, /* fid, flags */
63 &setup, 1, 0, /* setup, length, max */
64 param, param_len, 2, /* param, length, max */
65 data, data_len, cli->max_xmit /* data, length, max */
66 )) {
67 return cli_nt_error(cli);
70 cli_receive_trans(cli, SMBtrans2,
71 &rparam, rparam_len,
72 &rdata, rdata_len);
74 SAFE_FREE(rdata);
75 SAFE_FREE(rparam);
77 return cli_nt_error(cli);
81 static NTSTATUS try_trans2_len(struct cli_state *cli,
82 const char *format,
83 int op, int level,
84 char *param, char *data,
85 int param_len, int *data_len,
86 unsigned int *rparam_len, unsigned int *rdata_len)
88 NTSTATUS ret=NT_STATUS_OK;
90 ret = try_trans2(cli, op, param, data, param_len,
91 DATA_SIZE, rparam_len, rdata_len);
92 #if VERBOSE
93 printf("op=%d level=%d ret=%s\n", op, level, nt_errstr(ret));
94 #endif
95 if (!NT_STATUS_IS_OK(ret)) return ret;
97 *data_len = 0;
98 while (*data_len < DATA_SIZE) {
99 ret = try_trans2(cli, op, param, data, param_len,
100 *data_len, rparam_len, rdata_len);
101 if (NT_STATUS_IS_OK(ret)) break;
102 *data_len += 2;
104 if (NT_STATUS_IS_OK(ret)) {
105 printf("found %s level=%d data_len=%d rparam_len=%d rdata_len=%d\n",
106 format, level, *data_len, *rparam_len, *rdata_len);
107 } else {
108 trans2_check_hit(format, op, level, ret);
110 return ret;
113 /****************************************************************************
114 check for existance of a trans2 call
115 ****************************************************************************/
116 static bool scan_trans2(struct cli_state *cli, int op, int level,
117 int fnum, int dnum, const char *fname)
119 int data_len = 0;
120 int param_len = 0;
121 unsigned int rparam_len, rdata_len;
122 char param[PARAM_SIZE], data[DATA_SIZE];
123 NTSTATUS status;
125 memset(data, 0, sizeof(data));
126 data_len = 4;
128 /* try with a info level only */
129 param_len = 2;
130 SSVAL(param, 0, level);
131 status = try_trans2_len(cli, "void", op, level, param, data, param_len, &data_len,
132 &rparam_len, &rdata_len);
133 if (NT_STATUS_IS_OK(status)) return True;
135 /* try with a file descriptor */
136 param_len = 6;
137 SSVAL(param, 0, fnum);
138 SSVAL(param, 2, level);
139 SSVAL(param, 4, 0);
140 status = try_trans2_len(cli, "fnum", op, level, param, data, param_len, &data_len,
141 &rparam_len, &rdata_len);
142 if (NT_STATUS_IS_OK(status)) return True;
145 /* try with a notify style */
146 param_len = 6;
147 SSVAL(param, 0, dnum);
148 SSVAL(param, 2, dnum);
149 SSVAL(param, 4, level);
150 status = try_trans2_len(cli, "notify", op, level, param, data, param_len, &data_len,
151 &rparam_len, &rdata_len);
152 if (NT_STATUS_IS_OK(status)) return True;
154 /* try with a file name */
155 param_len = 6;
156 SSVAL(param, 0, level);
157 SSVAL(param, 2, 0);
158 SSVAL(param, 4, 0);
159 param_len += clistr_push(cli, &param[6], fname, -1, STR_TERMINATE);
161 status = try_trans2_len(cli, "fname", op, level, param, data, param_len, &data_len,
162 &rparam_len, &rdata_len);
163 if (NT_STATUS_IS_OK(status)) return True;
165 /* try with a new file name */
166 param_len = 6;
167 SSVAL(param, 0, level);
168 SSVAL(param, 2, 0);
169 SSVAL(param, 4, 0);
170 param_len += clistr_push(cli, &param[6], "\\newfile.dat", -1, STR_TERMINATE);
172 status = try_trans2_len(cli, "newfile", op, level, param, data, param_len, &data_len,
173 &rparam_len, &rdata_len);
174 cli_unlink(cli, "\\newfile.dat", aSYSTEM | aHIDDEN);
175 cli_rmdir(cli, "\\newfile.dat");
176 if (NT_STATUS_IS_OK(status)) return True;
178 /* try dfs style */
179 cli_mkdir(cli, "\\testdir");
180 param_len = 2;
181 SSVAL(param, 0, level);
182 param_len += clistr_push(cli, &param[2], "\\testdir", -1, STR_TERMINATE);
184 status = try_trans2_len(cli, "dfs", op, level, param, data, param_len, &data_len,
185 &rparam_len, &rdata_len);
186 cli_rmdir(cli, "\\testdir");
187 if (NT_STATUS_IS_OK(status)) return True;
189 return False;
193 bool torture_trans2_scan(int dummy)
195 static struct cli_state *cli;
196 int op, level;
197 const char *fname = "\\scanner.dat";
198 uint16_t fnum, dnum;
200 printf("starting trans2 scan test\n");
202 if (!torture_open_connection(&cli, 0)) {
203 return False;
206 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR | O_CREAT | O_TRUNC,
207 DENY_NONE, &fnum))) {
208 printf("open of %s failed\n", fname);
209 return false;
211 if (!NT_STATUS_IS_OK(cli_open(cli, "\\", O_RDONLY, DENY_NONE, &dnum))) {
212 printf("open of \\ failed\n");
213 return false;
216 for (op=OP_MIN; op<=OP_MAX; op++) {
217 printf("Scanning op=%d\n", op);
218 for (level = 0; level <= 50; level++) {
219 scan_trans2(cli, op, level, fnum, dnum, fname);
222 for (level = 0x100; level <= 0x130; level++) {
223 scan_trans2(cli, op, level, fnum, dnum, fname);
226 for (level = 1000; level < 1050; level++) {
227 scan_trans2(cli, op, level, fnum, dnum, fname);
231 torture_close_connection(cli);
233 printf("trans2 scan finished\n");
234 return True;
240 /****************************************************************************
241 look for a partial hit
242 ****************************************************************************/
243 static void nttrans_check_hit(const char *format, int op, int level, NTSTATUS status)
245 if (NT_STATUS_V(status) == NT_STATUS_V(NT_STATUS_INVALID_LEVEL) ||
246 NT_STATUS_V(status) == NT_STATUS_V(NT_STATUS_NOT_IMPLEMENTED) ||
247 NT_STATUS_V(status) == NT_STATUS_V(NT_STATUS_NOT_SUPPORTED) ||
248 NT_STATUS_V(status) == NT_STATUS_V(NT_STATUS_UNSUCCESSFUL) ||
249 NT_STATUS_V(status) == NT_STATUS_V(NT_STATUS_INVALID_INFO_CLASS)) {
250 return;
252 #if VERBOSE
253 printf("possible %s hit op=%3d level=%5d status=%s\n",
254 format, op, level, nt_errstr(status));
255 #endif
258 /****************************************************************************
259 check for existance of a nttrans call
260 ****************************************************************************/
261 static NTSTATUS try_nttrans(struct cli_state *cli,
262 int op,
263 char *param, char *data,
264 int param_len, int data_len,
265 unsigned int *rparam_len, unsigned int *rdata_len)
267 char *rparam=NULL, *rdata=NULL;
269 if (!cli_send_nt_trans(cli, op,
271 NULL, 0, 0,
272 param, param_len, 2, /* param, length, max */
273 data, data_len, cli->max_xmit /* data, length, max */
274 )) {
275 return cli_nt_error(cli);
278 cli_receive_nt_trans(cli,
279 &rparam, rparam_len,
280 &rdata, rdata_len);
282 SAFE_FREE(rdata);
283 SAFE_FREE(rparam);
285 return cli_nt_error(cli);
289 static NTSTATUS try_nttrans_len(struct cli_state *cli,
290 const char *format,
291 int op, int level,
292 char *param, char *data,
293 int param_len, int *data_len,
294 unsigned int *rparam_len, unsigned int *rdata_len)
296 NTSTATUS ret=NT_STATUS_OK;
298 ret = try_nttrans(cli, op, param, data, param_len,
299 DATA_SIZE, rparam_len, rdata_len);
300 #if VERBOSE
301 printf("op=%d level=%d ret=%s\n", op, level, nt_errstr(ret));
302 #endif
303 if (!NT_STATUS_IS_OK(ret)) return ret;
305 *data_len = 0;
306 while (*data_len < DATA_SIZE) {
307 ret = try_nttrans(cli, op, param, data, param_len,
308 *data_len, rparam_len, rdata_len);
309 if (NT_STATUS_IS_OK(ret)) break;
310 *data_len += 2;
312 if (NT_STATUS_IS_OK(ret)) {
313 printf("found %s level=%d data_len=%d rparam_len=%d rdata_len=%d\n",
314 format, level, *data_len, *rparam_len, *rdata_len);
315 } else {
316 nttrans_check_hit(format, op, level, ret);
318 return ret;
321 /****************************************************************************
322 check for existance of a nttrans call
323 ****************************************************************************/
324 static bool scan_nttrans(struct cli_state *cli, int op, int level,
325 int fnum, int dnum, const char *fname)
327 int data_len = 0;
328 int param_len = 0;
329 unsigned int rparam_len, rdata_len;
330 char param[PARAM_SIZE], data[DATA_SIZE];
331 NTSTATUS status;
333 memset(data, 0, sizeof(data));
334 data_len = 4;
336 /* try with a info level only */
337 param_len = 2;
338 SSVAL(param, 0, level);
339 status = try_nttrans_len(cli, "void", op, level, param, data, param_len, &data_len,
340 &rparam_len, &rdata_len);
341 if (NT_STATUS_IS_OK(status)) return True;
343 /* try with a file descriptor */
344 param_len = 6;
345 SSVAL(param, 0, fnum);
346 SSVAL(param, 2, level);
347 SSVAL(param, 4, 0);
348 status = try_nttrans_len(cli, "fnum", op, level, param, data, param_len, &data_len,
349 &rparam_len, &rdata_len);
350 if (NT_STATUS_IS_OK(status)) return True;
353 /* try with a notify style */
354 param_len = 6;
355 SSVAL(param, 0, dnum);
356 SSVAL(param, 2, dnum);
357 SSVAL(param, 4, level);
358 status = try_nttrans_len(cli, "notify", op, level, param, data, param_len, &data_len,
359 &rparam_len, &rdata_len);
360 if (NT_STATUS_IS_OK(status)) return True;
362 /* try with a file name */
363 param_len = 6;
364 SSVAL(param, 0, level);
365 SSVAL(param, 2, 0);
366 SSVAL(param, 4, 0);
367 param_len += clistr_push(cli, &param[6], fname, -1, STR_TERMINATE);
369 status = try_nttrans_len(cli, "fname", op, level, param, data, param_len, &data_len,
370 &rparam_len, &rdata_len);
371 if (NT_STATUS_IS_OK(status)) return True;
373 /* try with a new file name */
374 param_len = 6;
375 SSVAL(param, 0, level);
376 SSVAL(param, 2, 0);
377 SSVAL(param, 4, 0);
378 param_len += clistr_push(cli, &param[6], "\\newfile.dat", -1, STR_TERMINATE);
380 status = try_nttrans_len(cli, "newfile", op, level, param, data, param_len, &data_len,
381 &rparam_len, &rdata_len);
382 cli_unlink(cli, "\\newfile.dat", aSYSTEM | aHIDDEN);
383 cli_rmdir(cli, "\\newfile.dat");
384 if (NT_STATUS_IS_OK(status)) return True;
386 /* try dfs style */
387 cli_mkdir(cli, "\\testdir");
388 param_len = 2;
389 SSVAL(param, 0, level);
390 param_len += clistr_push(cli, &param[2], "\\testdir", -1, STR_TERMINATE);
392 status = try_nttrans_len(cli, "dfs", op, level, param, data, param_len, &data_len,
393 &rparam_len, &rdata_len);
394 cli_rmdir(cli, "\\testdir");
395 if (NT_STATUS_IS_OK(status)) return True;
397 return False;
401 bool torture_nttrans_scan(int dummy)
403 static struct cli_state *cli;
404 int op, level;
405 const char *fname = "\\scanner.dat";
406 uint16_t fnum, dnum;
408 printf("starting nttrans scan test\n");
410 if (!torture_open_connection(&cli, 0)) {
411 return False;
414 cli_open(cli, fname, O_RDWR | O_CREAT | O_TRUNC,
415 DENY_NONE, &fnum);
416 cli_open(cli, "\\", O_RDONLY, DENY_NONE, &dnum);
418 for (op=OP_MIN; op<=OP_MAX; op++) {
419 printf("Scanning op=%d\n", op);
420 for (level = 0; level <= 50; level++) {
421 scan_nttrans(cli, op, level, fnum, dnum, fname);
424 for (level = 0x100; level <= 0x130; level++) {
425 scan_nttrans(cli, op, level, fnum, dnum, fname);
428 for (level = 1000; level < 1050; level++) {
429 scan_nttrans(cli, op, level, fnum, dnum, fname);
433 torture_close_connection(cli);
435 printf("nttrans scan finished\n");
436 return True;