3b132ab8a4f655c22af1f50a24e842d880f12b27
[mat/samba.git] / source4 / torture / smb2 / scan.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB2 opcode scanner
5
6    Copyright (C) Andrew Tridgell 2005
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 "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"
29
30 #include "torture/smb2/proto.h"
31
32 #define FNAME "scan-getinfo.dat"
33 #define DNAME "scan-getinfo.dir"
34
35
36 /* 
37    scan for valid SMB2 getinfo levels
38 */
39 bool torture_smb2_getinfo_scan(struct torture_context *torture)
40 {
41         struct smb2_tree *tree;
42         NTSTATUS status;
43         struct smb2_getinfo io;
44         struct smb2_handle fhandle, dhandle;
45         int c, i;
46
47         if (!torture_smb2_connection(torture, &tree)) {
48                 return false;
49         }
50
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;
55         }
56         torture_setup_complex_file(tree, FNAME ":2ndstream");
57
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                 return false;
62         }
63         torture_setup_complex_file(tree, DNAME ":2ndstream");
64
65         torture_smb2_testfile(tree, FNAME, &fhandle);
66         torture_smb2_testdir(tree, DNAME, &dhandle);
67
68
69         ZERO_STRUCT(io);
70         io.in.output_buffer_length = 0xFFFF;
71
72         for (c=1;c<5;c++) {
73                 for (i=0;i<0x100;i++) {
74                         io.in.info_type = c;
75                         io.in.info_class = i;
76
77                         io.in.file.handle = fhandle;
78                         status = smb2_getinfo(tree, torture, &io);
79                         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS)) {
80                                 printf("file level 0x%02x:%02x %u is %ld bytes - %s\n", 
81                                        io.in.info_type, io.in.info_class, 
82                                        (unsigned)io.in.info_class, 
83                                        (long)io.out.blob.length, nt_errstr(status));
84                                 dump_data(1, io.out.blob.data, io.out.blob.length);
85                         }
86
87                         io.in.file.handle = dhandle;
88                         status = smb2_getinfo(tree, torture, &io);
89                         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS)) {
90                                 printf("dir  level 0x%02x:%02x %u is %ld bytes - %s\n", 
91                                        io.in.info_type, io.in.info_class,
92                                        (unsigned)io.in.info_class, 
93                                        (long)io.out.blob.length, nt_errstr(status));
94                                 dump_data(1, io.out.blob.data, io.out.blob.length);
95                         }
96                 }
97         }
98
99         return true;
100 }
101
102 /* 
103    scan for valid SMB2 setinfo levels
104 */
105 bool torture_smb2_setinfo_scan(struct torture_context *torture)
106 {
107         struct smb2_tree *tree;
108         NTSTATUS status;
109         struct smb2_setinfo io;
110         struct smb2_handle handle;
111         int c, i;
112
113         if (!torture_smb2_connection(torture, &tree)) {
114                 return false;
115         }
116
117         status = torture_setup_complex_file(tree, FNAME);
118         if (!NT_STATUS_IS_OK(status)) {
119                 printf("Failed to setup complex file '%s'\n", FNAME);
120                 return false;
121         }
122         torture_setup_complex_file(tree, FNAME ":2ndstream");
123
124         torture_smb2_testfile(tree, FNAME, &handle);
125
126         ZERO_STRUCT(io);
127         io.in.blob = data_blob_talloc_zero(torture, 1024);
128
129         for (c=1;c<5;c++) {
130                 for (i=0;i<0x100;i++) {
131                         io.in.level = (i<<8) | c;
132                         io.in.file.handle = handle;
133                         status = smb2_setinfo(tree, &io);
134                         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS)) {
135                                 printf("file level 0x%04x - %s\n", 
136                                        io.in.level, nt_errstr(status));
137                         }
138                 }
139         }
140
141         return true;
142 }
143
144
145 /* 
146    scan for valid SMB2 scan levels
147 */
148 bool torture_smb2_find_scan(struct torture_context *torture)
149 {
150         struct smb2_tree *tree;
151         NTSTATUS status;
152         struct smb2_find io;
153         struct smb2_handle handle;
154         int i;
155
156         if (!torture_smb2_connection(torture, &tree)) {
157                 return false;
158         }
159
160         status = smb2_util_roothandle(tree, &handle);
161         if (!NT_STATUS_IS_OK(status)) {
162                 printf("Failed to open roothandle - %s\n", nt_errstr(status));
163                 return false;
164         }
165
166         ZERO_STRUCT(io);
167         io.in.file.handle       = handle;
168         io.in.pattern           = "*";
169         io.in.continue_flags    = SMB2_CONTINUE_FLAG_RESTART;
170         io.in.max_response_size = 0x10000;
171
172         for (i=1;i<0x100;i++) {
173                 io.in.level = i;
174
175                 io.in.file.handle = handle;
176                 status = smb2_find(tree, torture, &io);
177                 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS) &&
178                     !NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER) &&
179                     !NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
180                         printf("find level 0x%04x is %ld bytes - %s\n", 
181                                io.in.level, (long)io.out.blob.length, nt_errstr(status));
182                         dump_data(1, io.out.blob.data, io.out.blob.length);
183                 }
184         }
185
186         return true;
187 }
188
189 /* 
190    scan for valid SMB2 opcodes
191 */
192 bool torture_smb2_scan(struct torture_context *torture)
193 {
194         TALLOC_CTX *mem_ctx = talloc_new(NULL);
195         struct smb2_tree *tree;
196         const char *host = torture_setting_string(torture, "host", NULL);
197         const char *share = torture_setting_string(torture, "share", NULL);
198         struct cli_credentials *credentials = cmdline_credentials;
199         NTSTATUS status;
200         int opcode;
201         struct smb2_request *req;
202         struct smbcli_options options;
203
204         lpcfg_smbcli_options(torture->lp_ctx, &options);
205
206         status = smb2_connect(mem_ctx, host, 
207                                                   lpcfg_smb_ports(torture->lp_ctx),
208                                                   share, 
209                                                   lpcfg_resolve_context(torture->lp_ctx),
210                                                   credentials, &tree, torture->ev, &options,
211                                                   lpcfg_socket_options(torture->lp_ctx),
212                                                   lpcfg_gensec_settings(torture, torture->lp_ctx));
213         if (!NT_STATUS_IS_OK(status)) {
214                 printf("Connection failed - %s\n", nt_errstr(status));
215                 return false;
216         }
217
218         tree->session->transport->options.request_timeout = 3;
219
220         for (opcode=0;opcode<1000;opcode++) {
221                 req = smb2_request_init_tree(tree, opcode, 2, false, 0);
222                 SSVAL(req->out.body, 0, 0);
223                 smb2_transport_send(req);
224                 if (!smb2_request_receive(req)) {
225                         talloc_free(tree);
226                         status = smb2_connect(mem_ctx, host, 
227                                                                   lpcfg_smb_ports(torture->lp_ctx),
228                                                                   share, 
229                                                                   lpcfg_resolve_context(torture->lp_ctx),
230                                                                   credentials, &tree, torture->ev, &options,
231                                                                   lpcfg_socket_options(torture->lp_ctx),
232                                                                   lpcfg_gensec_settings(mem_ctx, torture->lp_ctx));
233                         if (!NT_STATUS_IS_OK(status)) {
234                                 printf("Connection failed - %s\n", nt_errstr(status));
235                                 return false;
236                         }
237                         tree->session->transport->options.request_timeout = 3;
238                 } else {
239                         status = smb2_request_destroy(req);
240                         printf("active opcode %4d gave status %s\n", opcode, nt_errstr(status));
241                 }
242         }
243
244         talloc_free(mem_ctx);
245
246         return true;
247 }