f3e4d5cd36a40acf2719b4be26cfefcf939dd265
[gd/samba-autobuild/.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 "lib/events/events.h"
27 #include "torture/torture.h"
28
29 #include "torture/smb2/proto.h"
30
31 #define FNAME "scan-getinfo.dat"
32 #define DNAME "scan-getinfo.dir"
33
34
35 /* 
36    scan for valid SMB2 getinfo levels
37 */
38 bool torture_smb2_getinfo_scan(struct torture_context *torture)
39 {
40         TALLOC_CTX *mem_ctx = talloc_new(NULL);
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(mem_ctx, &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.max_response_size = 0xFFFF;
71
72         for (c=1;c<5;c++) {
73                 for (i=0;i<0x100;i++) {
74                         io.in.level = (i<<8) | c;
75
76                         io.in.file.handle = fhandle;
77                         status = smb2_getinfo(tree, mem_ctx, &io);
78                         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS) &&
79                             !NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER) &&
80                             !NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
81                                 printf("file level 0x%04x is %ld bytes - %s\n", 
82                                        io.in.level, (long)io.out.blob.length, nt_errstr(status));
83                                 dump_data(1, io.out.blob.data, io.out.blob.length);
84                         }
85
86                         io.in.file.handle = dhandle;
87                         status = smb2_getinfo(tree, mem_ctx, &io);
88                         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS) &&
89                             !NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER) &&
90                             !NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
91                                 printf("dir  level 0x%04x is %ld bytes - %s\n", 
92                                        io.in.level, (long)io.out.blob.length, nt_errstr(status));
93                                 dump_data(1, io.out.blob.data, io.out.blob.length);
94                         }
95                 }
96         }
97
98         talloc_free(mem_ctx);
99
100         return true;
101 }
102
103 /* 
104    scan for valid SMB2 setinfo levels
105 */
106 bool torture_smb2_setinfo_scan(struct torture_context *torture)
107 {
108         TALLOC_CTX *mem_ctx = talloc_new(NULL);
109         struct smb2_tree *tree;
110         NTSTATUS status;
111         struct smb2_setinfo io;
112         struct smb2_handle handle;
113         int c, i;
114
115         if (!torture_smb2_connection(mem_ctx, &tree)) {
116                 return false;
117         }
118
119         status = torture_setup_complex_file(tree, FNAME);
120         if (!NT_STATUS_IS_OK(status)) {
121                 printf("Failed to setup complex file '%s'\n", FNAME);
122                 return false;
123         }
124         torture_setup_complex_file(tree, FNAME ":2ndstream");
125
126         torture_smb2_testfile(tree, FNAME, &handle);
127
128         ZERO_STRUCT(io);
129         io.in.blob = data_blob_talloc_zero(mem_ctx, 1024);
130
131         for (c=1;c<5;c++) {
132                 for (i=0;i<0x100;i++) {
133                         io.in.level = (i<<8) | c;
134                         io.in.file.handle = handle;
135                         status = smb2_setinfo(tree, &io);
136                         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS) &&
137                             !NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
138                                 printf("file level 0x%04x - %s\n", 
139                                        io.in.level, nt_errstr(status));
140                         }
141                 }
142         }
143
144         talloc_free(mem_ctx);
145
146         return true;
147 }
148
149
150 /* 
151    scan for valid SMB2 scan levels
152 */
153 bool torture_smb2_find_scan(struct torture_context *torture)
154 {
155         TALLOC_CTX *mem_ctx = talloc_new(NULL);
156         struct smb2_tree *tree;
157         NTSTATUS status;
158         struct smb2_find io;
159         struct smb2_handle handle;
160         int i;
161
162         if (!torture_smb2_connection(mem_ctx, &tree)) {
163                 return false;
164         }
165
166         status = smb2_util_roothandle(tree, &handle);
167         if (!NT_STATUS_IS_OK(status)) {
168                 printf("Failed to open roothandle - %s\n", nt_errstr(status));
169                 return false;
170         }
171
172         ZERO_STRUCT(io);
173         io.in.file.handle       = handle;
174         io.in.pattern           = "*";
175         io.in.continue_flags    = SMB2_CONTINUE_FLAG_RESTART;
176         io.in.max_response_size = 0x10000;
177
178         for (i=1;i<0x100;i++) {
179                 io.in.level = i;
180
181                 io.in.file.handle = handle;
182                 status = smb2_find(tree, mem_ctx, &io);
183                 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS) &&
184                     !NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER) &&
185                     !NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
186                         printf("find level 0x%04x is %ld bytes - %s\n", 
187                                io.in.level, (long)io.out.blob.length, nt_errstr(status));
188                         dump_data(1, io.out.blob.data, io.out.blob.length);
189                 }
190         }
191
192         talloc_free(mem_ctx);
193
194         return true;
195 }
196
197 /* 
198    scan for valid SMB2 opcodes
199 */
200 bool torture_smb2_scan(struct torture_context *torture)
201 {
202         TALLOC_CTX *mem_ctx = talloc_new(NULL);
203         struct smb2_tree *tree;
204         const char *host = torture_setting_string(torture, "host", NULL);
205         const char *share = torture_setting_string(torture, "share", NULL);
206         struct cli_credentials *credentials = cmdline_credentials;
207         NTSTATUS status;
208         int opcode;
209         struct smb2_request *req;
210
211         status = smb2_connect(mem_ctx, host, share, credentials, &tree, 
212                               event_context_find(mem_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.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, share, credentials, &tree, 
227                                               event_context_find(mem_ctx));
228                         if (!NT_STATUS_IS_OK(status)) {
229                                 printf("Connection failed - %s\n", nt_errstr(status));
230                                 return false;
231                         }
232                         tree->session->transport->options.timeout = 3;
233                 } else {
234                         status = smb2_request_destroy(req);
235                         printf("active opcode %4d gave status %s\n", opcode, nt_errstr(status));
236                 }
237         }
238
239         talloc_free(mem_ctx);
240
241         return true;
242 }