r12608: Remove some unused #include lines.
[bbaumbach/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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26 #include "lib/cmdline/popt_common.h"
27 #include "lib/events/events.h"
28
29
30 #define FNAME "scan-getinfo.dat"
31 #define DNAME "scan-getinfo.dir"
32
33
34 /* 
35    scan for valid SMB2 getinfo levels
36 */
37 BOOL torture_smb2_getinfo_scan(void)
38 {
39         TALLOC_CTX *mem_ctx = talloc_new(NULL);
40         struct smb2_tree *tree;
41         NTSTATUS status;
42         struct smb2_getinfo io;
43         struct smb2_handle fhandle, dhandle;
44         int c, i;
45
46         if (!torture_smb2_connection(mem_ctx, &tree)) {
47                 return False;
48         }
49
50         status = torture_setup_complex_file(tree, FNAME);
51         if (!NT_STATUS_IS_OK(status)) {
52                 printf("Failed to setup complex file '%s'\n", FNAME);
53                 return False;
54         }
55         torture_setup_complex_file(tree, FNAME ":2ndstream");
56
57         status = torture_setup_complex_dir(tree, DNAME);
58         if (!NT_STATUS_IS_OK(status)) {
59                 printf("Failed to setup complex dir  '%s'\n", DNAME);
60                 return False;
61         }
62         torture_setup_complex_file(tree, DNAME ":2ndstream");
63
64         torture_smb2_testfile(tree, FNAME, &fhandle);
65         torture_smb2_testdir(tree, DNAME, &dhandle);
66
67
68         ZERO_STRUCT(io);
69         io.in.max_response_size = 0xFFFF;
70
71         for (c=1;c<5;c++) {
72                 for (i=0;i<0x100;i++) {
73                         io.in.level = (i<<8) | c;
74
75                         io.in.handle = fhandle;
76                         status = smb2_getinfo(tree, mem_ctx, &io);
77                         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS) &&
78                             !NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER) &&
79                             !NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
80                                 printf("file level 0x%04x is %ld bytes - %s\n", 
81                                        io.in.level, (long)io.out.blob.length, nt_errstr(status));
82                                 dump_data(1, io.out.blob.data, io.out.blob.length);
83                         }
84
85                         io.in.handle = dhandle;
86                         status = smb2_getinfo(tree, mem_ctx, &io);
87                         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS) &&
88                             !NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER) &&
89                             !NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
90                                 printf("dir  level 0x%04x is %ld bytes - %s\n", 
91                                        io.in.level, (long)io.out.blob.length, nt_errstr(status));
92                                 dump_data(1, io.out.blob.data, io.out.blob.length);
93                         }
94                 }
95         }
96
97         talloc_free(mem_ctx);
98
99         return True;
100 }
101
102 /* 
103    scan for valid SMB2 setinfo levels
104 */
105 BOOL torture_smb2_setinfo_scan(void)
106 {
107         TALLOC_CTX *mem_ctx = talloc_new(NULL);
108         struct smb2_tree *tree;
109         NTSTATUS status;
110         struct smb2_setinfo io;
111         struct smb2_handle handle;
112         int c, i;
113
114         if (!torture_smb2_connection(mem_ctx, &tree)) {
115                 return False;
116         }
117
118         status = torture_setup_complex_file(tree, FNAME);
119         if (!NT_STATUS_IS_OK(status)) {
120                 printf("Failed to setup complex file '%s'\n", FNAME);
121                 return False;
122         }
123         torture_setup_complex_file(tree, FNAME ":2ndstream");
124
125         torture_smb2_testfile(tree, FNAME, &handle);
126
127         ZERO_STRUCT(io);
128         io.in.blob = data_blob_talloc(mem_ctx, NULL, 1024);
129
130         for (c=1;c<5;c++) {
131                 for (i=0;i<0x100;i++) {
132                         io.in.level = (i<<8) | c;
133                         io.in.handle = handle;
134                         status = smb2_setinfo(tree, &io);
135                         if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS) &&
136                             !NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
137                                 printf("file level 0x%04x - %s\n", 
138                                        io.in.level, nt_errstr(status));
139                         }
140                 }
141         }
142
143         talloc_free(mem_ctx);
144
145         return True;
146 }
147
148
149 /* 
150    scan for valid SMB2 scan levels
151 */
152 BOOL torture_smb2_find_scan(void)
153 {
154         TALLOC_CTX *mem_ctx = talloc_new(NULL);
155         struct smb2_tree *tree;
156         NTSTATUS status;
157         struct smb2_find io;
158         struct smb2_handle handle;
159         int i;
160
161         if (!torture_smb2_connection(mem_ctx, &tree)) {
162                 return False;
163         }
164
165         status = smb2_util_roothandle(tree, &handle);
166         if (!NT_STATUS_IS_OK(status)) {
167                 printf("Failed to open roothandle - %s\n", nt_errstr(status));
168                 return False;
169         }
170
171         ZERO_STRUCT(io);
172         io.in.pattern = "*";
173         io.in.continue_flags = SMB2_CONTINUE_FLAG_RESTART;
174         io.in.max_response_size = 0x10000;
175         io.in.handle = handle;
176
177         for (i=1;i<0x100;i++) {
178                 io.in.level = i;
179
180                 io.in.handle = handle;
181                 status = smb2_find(tree, mem_ctx, &io);
182                 if (!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_INFO_CLASS) &&
183                     !NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER) &&
184                     !NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
185                         printf("find level 0x%04x is %ld bytes - %s\n", 
186                                io.in.level, (long)io.out.blob.length, nt_errstr(status));
187                         dump_data(1, io.out.blob.data, io.out.blob.length);
188                 }
189         }
190
191         talloc_free(mem_ctx);
192
193         return True;
194 }
195
196 /* 
197    scan for valid SMB2 opcodes
198 */
199 BOOL torture_smb2_scan(void)
200 {
201         TALLOC_CTX *mem_ctx = talloc_new(NULL);
202         struct smb2_tree *tree;
203         const char *host = lp_parm_string(-1, "torture", "host");
204         const char *share = lp_parm_string(-1, "torture", "share");
205         struct cli_credentials *credentials = cmdline_credentials;
206         NTSTATUS status;
207         int opcode;
208         struct smb2_request *req;
209
210         status = smb2_connect(mem_ctx, host, share, credentials, &tree, 
211                               event_context_find(mem_ctx));
212         if (!NT_STATUS_IS_OK(status)) {
213                 printf("Connection failed - %s\n", nt_errstr(status));
214                 return False;
215         }
216
217         tree->session->transport->options.timeout = 3;
218
219         for (opcode=0;opcode<1000;opcode++) {
220                 req = smb2_request_init_tree(tree, opcode, 2, 0);
221                 SSVAL(req->out.body, 0, 0);
222                 smb2_transport_send(req);
223                 if (!smb2_request_receive(req)) {
224                         talloc_free(tree);
225                         status = smb2_connect(mem_ctx, host, share, credentials, &tree, 
226                                               event_context_find(mem_ctx));
227                         if (!NT_STATUS_IS_OK(status)) {
228                                 printf("Connection failed - %s\n", nt_errstr(status));
229                                 return False;
230                         }
231                         tree->session->transport->options.timeout = 3;
232                 } else {
233                         status = smb2_request_destroy(req);
234                         printf("active opcode %4d gave status %s\n", opcode, nt_errstr(status));
235                 }
236         }
237
238         talloc_free(mem_ctx);
239
240         return True;
241 }