s4:torture: cleanup after smb2 getinfo scan
[Samba/gebeck_regimport.git] / source4 / torture / smb2 / scan.c
blobd0e95dbf8773bd0669fbf222379871a999857d3c
1 /*
2 Unix SMB/CIFS implementation.
4 SMB2 opcode scanner
6 Copyright (C) Andrew Tridgell 2005
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 "libcli/smb2/smb2.h"
24 #include "libcli/smb2/smb2_calls.h"
25 #include "lib/cmdline/popt_common.h"
26 #include "torture/torture.h"
27 #include "param/param.h"
28 #include "libcli/resolve/resolve.h"
30 #include "torture/smb2/proto.h"
32 #define FNAME "scan-getinfo.dat"
33 #define DNAME "scan-getinfo.dir"
36 /*
37 scan for valid SMB2 getinfo levels
39 bool torture_smb2_getinfo_scan(struct torture_context *torture)
41 struct smb2_tree *tree;
42 NTSTATUS status;
43 struct smb2_getinfo io;
44 struct smb2_handle fhandle, dhandle;
45 int c, i;
47 if (!torture_smb2_connection(torture, &tree)) {
48 return false;
51 status = torture_setup_complex_file(tree, FNAME);
52 if (!NT_STATUS_IS_OK(status)) {
53 printf("Failed to setup complex file '%s'\n", FNAME);
54 return false;
56 torture_setup_complex_file(tree, FNAME ":2ndstream");
58 status = torture_setup_complex_dir(tree, DNAME);
59 if (!NT_STATUS_IS_OK(status)) {
60 printf("Failed to setup complex dir '%s'\n", DNAME);
61 smb2_util_unlink(tree, FNAME);
62 return false;
64 torture_setup_complex_file(tree, DNAME ":2ndstream");
66 torture_smb2_testfile(tree, FNAME, &fhandle);
67 torture_smb2_testdir(tree, DNAME, &dhandle);
70 ZERO_STRUCT(io);
71 io.in.output_buffer_length = 0xFFFF;
73 for (c=1;c<5;c++) {
74 for (i=0;i<0x100;i++) {
75 io.in.info_type = c;
76 io.in.info_class = i;
78 io.in.file.handle = fhandle;
79 status = smb2_getinfo(tree, torture, &io);
80 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS)) {
81 printf("file level 0x%02x:%02x %u is %ld bytes - %s\n",
82 io.in.info_type, io.in.info_class,
83 (unsigned)io.in.info_class,
84 (long)io.out.blob.length, nt_errstr(status));
85 dump_data(1, io.out.blob.data, io.out.blob.length);
88 io.in.file.handle = dhandle;
89 status = smb2_getinfo(tree, torture, &io);
90 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS)) {
91 printf("dir level 0x%02x:%02x %u is %ld bytes - %s\n",
92 io.in.info_type, io.in.info_class,
93 (unsigned)io.in.info_class,
94 (long)io.out.blob.length, nt_errstr(status));
95 dump_data(1, io.out.blob.data, io.out.blob.length);
100 smb2_util_unlink(tree, FNAME);
101 smb2_util_rmdir(tree, DNAME);
102 return true;
106 scan for valid SMB2 setinfo levels
108 bool torture_smb2_setinfo_scan(struct torture_context *torture)
110 struct smb2_tree *tree;
111 NTSTATUS status;
112 struct smb2_setinfo io;
113 struct smb2_handle handle;
114 int c, i;
116 if (!torture_smb2_connection(torture, &tree)) {
117 return false;
120 status = torture_setup_complex_file(tree, FNAME);
121 if (!NT_STATUS_IS_OK(status)) {
122 printf("Failed to setup complex file '%s'\n", FNAME);
123 return false;
125 torture_setup_complex_file(tree, FNAME ":2ndstream");
127 torture_smb2_testfile(tree, FNAME, &handle);
129 ZERO_STRUCT(io);
130 io.in.blob = data_blob_talloc_zero(torture, 1024);
132 for (c=1;c<5;c++) {
133 for (i=0;i<0x100;i++) {
134 io.in.level = (i<<8) | c;
135 io.in.file.handle = handle;
136 status = smb2_setinfo(tree, &io);
137 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS)) {
138 printf("file level 0x%04x - %s\n",
139 io.in.level, nt_errstr(status));
144 return true;
149 scan for valid SMB2 scan levels
151 bool torture_smb2_find_scan(struct torture_context *torture)
153 struct smb2_tree *tree;
154 NTSTATUS status;
155 struct smb2_find io;
156 struct smb2_handle handle;
157 int i;
159 if (!torture_smb2_connection(torture, &tree)) {
160 return false;
163 status = smb2_util_roothandle(tree, &handle);
164 if (!NT_STATUS_IS_OK(status)) {
165 printf("Failed to open roothandle - %s\n", nt_errstr(status));
166 return false;
169 ZERO_STRUCT(io);
170 io.in.file.handle = handle;
171 io.in.pattern = "*";
172 io.in.continue_flags = SMB2_CONTINUE_FLAG_RESTART;
173 io.in.max_response_size = 0x10000;
175 for (i=1;i<0x100;i++) {
176 io.in.level = i;
178 io.in.file.handle = handle;
179 status = smb2_find(tree, torture, &io);
180 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS) &&
181 !NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER) &&
182 !NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
183 printf("find level 0x%04x is %ld bytes - %s\n",
184 io.in.level, (long)io.out.blob.length, nt_errstr(status));
185 dump_data(1, io.out.blob.data, io.out.blob.length);
189 return true;
193 scan for valid SMB2 opcodes
195 bool torture_smb2_scan(struct torture_context *torture)
197 TALLOC_CTX *mem_ctx = talloc_new(NULL);
198 struct smb2_tree *tree;
199 const char *host = torture_setting_string(torture, "host", NULL);
200 const char *share = torture_setting_string(torture, "share", NULL);
201 struct cli_credentials *credentials = cmdline_credentials;
202 NTSTATUS status;
203 int opcode;
204 struct smb2_request *req;
205 struct smbcli_options options;
207 lpcfg_smbcli_options(torture->lp_ctx, &options);
209 status = smb2_connect(mem_ctx, host,
210 lpcfg_smb_ports(torture->lp_ctx),
211 share,
212 lpcfg_resolve_context(torture->lp_ctx),
213 credentials, &tree, torture->ev, &options,
214 lpcfg_socket_options(torture->lp_ctx),
215 lpcfg_gensec_settings(torture, torture->lp_ctx));
216 if (!NT_STATUS_IS_OK(status)) {
217 printf("Connection failed - %s\n", nt_errstr(status));
218 return false;
221 tree->session->transport->options.request_timeout = 3;
223 for (opcode=0;opcode<1000;opcode++) {
224 req = smb2_request_init_tree(tree, opcode, 2, false, 0);
225 SSVAL(req->out.body, 0, 0);
226 smb2_transport_send(req);
227 if (!smb2_request_receive(req)) {
228 talloc_free(tree);
229 status = smb2_connect(mem_ctx, host,
230 lpcfg_smb_ports(torture->lp_ctx),
231 share,
232 lpcfg_resolve_context(torture->lp_ctx),
233 credentials, &tree, torture->ev, &options,
234 lpcfg_socket_options(torture->lp_ctx),
235 lpcfg_gensec_settings(mem_ctx, torture->lp_ctx));
236 if (!NT_STATUS_IS_OK(status)) {
237 printf("Connection failed - %s\n", nt_errstr(status));
238 return false;
240 tree->session->transport->options.request_timeout = 3;
241 } else {
242 status = smb2_request_destroy(req);
243 printf("active opcode %4d gave status %s\n", opcode, nt_errstr(status));
247 talloc_free(mem_ctx);
249 return true;