2 Unix SMB/CIFS implementation.
4 helper functions for SMB2 test suite
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/>.
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 "system/time.h"
28 #include "librpc/gen_ndr/ndr_security.h"
29 #include "param/param.h"
31 #include "torture/torture.h"
32 #include "torture/smb2/proto.h"
36 close a handle with SMB2
38 NTSTATUS smb2_util_close(struct smb2_tree *tree, struct smb2_handle h)
45 return smb2_close(tree, &c);
49 unlink a file with SMB2
51 NTSTATUS smb2_util_unlink(struct smb2_tree *tree, const char *fname)
53 struct smb2_create io;
57 io.in.access_mask = SEC_RIGHTS_FILE_ALL;
58 io.in.file_attr = FILE_ATTRIBUTE_NORMAL;
59 io.in.open_disposition = NTCREATEX_DISP_OPEN;
61 NTCREATEX_SHARE_ACCESS_DELETE|
62 NTCREATEX_SHARE_ACCESS_READ|
63 NTCREATEX_SHARE_ACCESS_WRITE;
64 io.in.create_options = NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
67 status = smb2_create(tree, tree, &io);
68 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
71 NT_STATUS_NOT_OK_RETURN(status);
73 return smb2_util_close(tree, io.out.file.handle);
77 write to a file on SMB2
79 NTSTATUS smb2_util_write(struct smb2_tree *tree,
80 struct smb2_handle handle,
81 const void *buf, off_t offset, size_t size)
86 w.in.file.handle = handle;
88 w.in.data = data_blob_const(buf, size);
90 return smb2_write(tree, &w);
94 create a complex file/dir using the SMB2 protocol
96 static NTSTATUS smb2_create_complex(struct smb2_tree *tree, const char *fname,
97 struct smb2_handle *handle, bool dir)
99 TALLOC_CTX *tmp_ctx = talloc_new(tree);
101 struct smb2_create io;
102 union smb_setfileinfo setfile;
103 union smb_fileinfo fileinfo;
104 time_t t = (time(NULL) & ~1);
107 smb2_util_unlink(tree, fname);
109 io.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
110 io.in.file_attr = FILE_ATTRIBUTE_NORMAL;
111 io.in.open_disposition = NTCREATEX_DISP_OVERWRITE_IF;
113 NTCREATEX_SHARE_ACCESS_DELETE|
114 NTCREATEX_SHARE_ACCESS_READ|
115 NTCREATEX_SHARE_ACCESS_WRITE;
116 io.in.create_options = 0;
119 io.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
120 io.in.share_access &= ~NTCREATEX_SHARE_ACCESS_DELETE;
121 io.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
122 io.in.open_disposition = NTCREATEX_DISP_CREATE;
125 if (strchr(fname, ':') == NULL) {
127 io.in.eas.num_eas = 2;
128 io.in.eas.eas = talloc_array(tmp_ctx, struct ea_struct, 2);
129 io.in.eas.eas[0].flags = 0;
130 io.in.eas.eas[0].name.s = "EAONE";
131 io.in.eas.eas[0].value = data_blob_talloc(tmp_ctx, "VALUE1", 6);
132 io.in.eas.eas[1].flags = 0;
133 io.in.eas.eas[1].name.s = "SECONDEA";
134 io.in.eas.eas[1].value = data_blob_talloc(tmp_ctx, "ValueTwo", 8);
137 status = smb2_create(tree, tmp_ctx, &io);
138 talloc_free(tmp_ctx);
139 NT_STATUS_NOT_OK_RETURN(status);
141 *handle = io.out.file.handle;
144 status = smb2_util_write(tree, *handle, buf, 0, sizeof(buf));
145 NT_STATUS_NOT_OK_RETURN(status);
148 /* make sure all the timestamps aren't the same, and are also
149 in different DST zones*/
150 setfile.generic.level = RAW_SFILEINFO_BASIC_INFORMATION;
151 setfile.generic.in.file.handle = *handle;
153 unix_to_nt_time(&setfile.basic_info.in.create_time, t + 9*30*24*60*60);
154 unix_to_nt_time(&setfile.basic_info.in.access_time, t + 6*30*24*60*60);
155 unix_to_nt_time(&setfile.basic_info.in.write_time, t + 3*30*24*60*60);
156 unix_to_nt_time(&setfile.basic_info.in.change_time, t + 1*30*24*60*60);
157 setfile.basic_info.in.attrib = FILE_ATTRIBUTE_NORMAL;
159 status = smb2_setinfo_file(tree, &setfile);
160 if (!NT_STATUS_IS_OK(status)) {
161 printf("Failed to setup file times - %s\n", nt_errstr(status));
165 /* make sure all the timestamps aren't the same */
166 fileinfo.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
167 fileinfo.generic.in.file.handle = *handle;
169 status = smb2_getinfo_file(tree, tree, &fileinfo);
170 if (!NT_STATUS_IS_OK(status)) {
171 printf("Failed to query file times - %s\n", nt_errstr(status));
176 #define CHECK_TIME(field) do {\
177 if (setfile.basic_info.in.field != fileinfo.all_info2.out.field) { \
178 printf("(%s) " #field " not setup correctly: %s(%llu) => %s(%llu)\n", \
180 nt_time_string(tree, setfile.basic_info.in.field), \
181 (unsigned long long)setfile.basic_info.in.field, \
182 nt_time_string(tree, fileinfo.basic_info.out.field), \
183 (unsigned long long)fileinfo.basic_info.out.field); \
184 status = NT_STATUS_INVALID_PARAMETER; \
188 CHECK_TIME(create_time);
189 CHECK_TIME(access_time);
190 CHECK_TIME(write_time);
191 CHECK_TIME(change_time);
197 create a complex file using the SMB2 protocol
199 NTSTATUS smb2_create_complex_file(struct smb2_tree *tree, const char *fname,
200 struct smb2_handle *handle)
202 return smb2_create_complex(tree, fname, handle, false);
206 create a complex dir using the SMB2 protocol
208 NTSTATUS smb2_create_complex_dir(struct smb2_tree *tree, const char *fname,
209 struct smb2_handle *handle)
211 return smb2_create_complex(tree, fname, handle, true);
215 show lots of information about a file
217 void torture_smb2_all_info(struct smb2_tree *tree, struct smb2_handle handle)
220 TALLOC_CTX *tmp_ctx = talloc_new(tree);
221 union smb_fileinfo io;
223 io.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
224 io.generic.in.file.handle = handle;
226 status = smb2_getinfo_file(tree, tmp_ctx, &io);
227 if (!NT_STATUS_IS_OK(status)) {
228 DEBUG(0,("getinfo failed - %s\n", nt_errstr(status)));
229 talloc_free(tmp_ctx);
233 d_printf("all_info for '%s'\n", io.all_info2.out.fname.s);
234 d_printf("\tcreate_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.create_time));
235 d_printf("\taccess_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.access_time));
236 d_printf("\twrite_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.write_time));
237 d_printf("\tchange_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.change_time));
238 d_printf("\tattrib: 0x%x\n", io.all_info2.out.attrib);
239 d_printf("\tunknown1: 0x%x\n", io.all_info2.out.unknown1);
240 d_printf("\talloc_size: %llu\n", (long long)io.all_info2.out.alloc_size);
241 d_printf("\tsize: %llu\n", (long long)io.all_info2.out.size);
242 d_printf("\tnlink: %u\n", io.all_info2.out.nlink);
243 d_printf("\tdelete_pending: %u\n", io.all_info2.out.delete_pending);
244 d_printf("\tdirectory: %u\n", io.all_info2.out.directory);
245 d_printf("\tfile_id: %llu\n", (long long)io.all_info2.out.file_id);
246 d_printf("\tea_size: %u\n", io.all_info2.out.ea_size);
247 d_printf("\taccess_mask: 0x%08x\n", io.all_info2.out.access_mask);
248 d_printf("\tposition: 0x%llx\n", (long long)io.all_info2.out.position);
249 d_printf("\tmode: 0x%llx\n", (long long)io.all_info2.out.mode);
251 /* short name, if any */
252 io.generic.level = RAW_FILEINFO_ALT_NAME_INFORMATION;
253 status = smb2_getinfo_file(tree, tmp_ctx, &io);
254 if (NT_STATUS_IS_OK(status)) {
255 d_printf("\tshort name: '%s'\n", io.alt_name_info.out.fname.s);
258 /* the EAs, if any */
259 io.generic.level = RAW_FILEINFO_SMB2_ALL_EAS;
260 status = smb2_getinfo_file(tree, tmp_ctx, &io);
261 if (NT_STATUS_IS_OK(status)) {
263 for (i=0;i<io.all_eas.out.num_eas;i++) {
264 d_printf("\tEA[%d] flags=%d len=%d '%s'\n", i,
265 io.all_eas.out.eas[i].flags,
266 (int)io.all_eas.out.eas[i].value.length,
267 io.all_eas.out.eas[i].name.s);
271 /* streams, if available */
272 io.generic.level = RAW_FILEINFO_STREAM_INFORMATION;
273 status = smb2_getinfo_file(tree, tmp_ctx, &io);
274 if (NT_STATUS_IS_OK(status)) {
276 for (i=0;i<io.stream_info.out.num_streams;i++) {
277 d_printf("\tstream %d:\n", i);
278 d_printf("\t\tsize %ld\n",
279 (long)io.stream_info.out.streams[i].size);
280 d_printf("\t\talloc size %ld\n",
281 (long)io.stream_info.out.streams[i].alloc_size);
282 d_printf("\t\tname %s\n", io.stream_info.out.streams[i].stream_name.s);
287 /* the security descriptor */
288 io.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
289 io.query_secdesc.in.secinfo_flags =
290 SECINFO_OWNER|SECINFO_GROUP|
292 status = smb2_getinfo_file(tree, tmp_ctx, &io);
293 if (NT_STATUS_IS_OK(status)) {
294 NDR_PRINT_DEBUG(security_descriptor, io.query_secdesc.out.sd);
298 talloc_free(tmp_ctx);
303 open a smb2 connection
305 bool torture_smb2_connection(TALLOC_CTX *mem_ctx, struct smb2_tree **tree)
308 const char *host = lp_parm_string(global_loadparm, NULL, "torture", "host");
309 const char *share = lp_parm_string(global_loadparm, NULL, "torture", "share");
310 struct cli_credentials *credentials = cmdline_credentials;
312 status = smb2_connect(mem_ctx, host, share, credentials, tree,
313 event_context_find(mem_ctx));
314 if (!NT_STATUS_IS_OK(status)) {
315 printf("Failed to connect to SMB2 share \\\\%s\\%s - %s\n",
316 host, share, nt_errstr(status));
324 create and return a handle to a test file
326 NTSTATUS torture_smb2_testfile(struct smb2_tree *tree, const char *fname,
327 struct smb2_handle *handle)
329 struct smb2_create io;
334 io.in.oplock_flags = 0;
335 io.in.access_mask = SEC_RIGHTS_FILE_ALL;
336 io.in.file_attr = FILE_ATTRIBUTE_NORMAL;
337 io.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
339 NTCREATEX_SHARE_ACCESS_DELETE|
340 NTCREATEX_SHARE_ACCESS_READ|
341 NTCREATEX_SHARE_ACCESS_WRITE;
342 io.in.create_options = 0;
345 status = smb2_create(tree, tree, &io);
346 NT_STATUS_NOT_OK_RETURN(status);
348 *handle = io.out.file.handle;
351 r.in.file.handle = *handle;
355 smb2_read(tree, tree, &r);
361 create and return a handle to a test directory
363 NTSTATUS torture_smb2_testdir(struct smb2_tree *tree, const char *fname,
364 struct smb2_handle *handle)
366 struct smb2_create io;
370 io.in.oplock_flags = 0;
371 io.in.access_mask = SEC_RIGHTS_DIR_ALL;
372 io.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
373 io.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
374 io.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE;
375 io.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
378 status = smb2_create(tree, tree, &io);
379 NT_STATUS_NOT_OK_RETURN(status);
381 *handle = io.out.file.handle;
388 create a complex file using the old SMB protocol, to make it easier to
389 find fields in SMB2 getinfo levels
391 NTSTATUS torture_setup_complex_file(struct smb2_tree *tree, const char *fname)
393 struct smb2_handle handle;
394 NTSTATUS status = smb2_create_complex_file(tree, fname, &handle);
395 NT_STATUS_NOT_OK_RETURN(status);
396 return smb2_util_close(tree, handle);
401 create a complex dir using the old SMB protocol, to make it easier to
402 find fields in SMB2 getinfo levels
404 NTSTATUS torture_setup_complex_dir(struct smb2_tree *tree, const char *fname)
406 struct smb2_handle handle;
407 NTSTATUS status = smb2_create_complex_dir(tree, fname, &handle);
408 NT_STATUS_NOT_OK_RETURN(status);
409 return smb2_util_close(tree, handle);
414 return a handle to the root of the share
416 NTSTATUS smb2_util_roothandle(struct smb2_tree *tree, struct smb2_handle *handle)
418 struct smb2_create io;
422 io.in.oplock_flags = 0;
423 io.in.access_mask = SEC_STD_SYNCHRONIZE | SEC_DIR_READ_ATTRIBUTE | SEC_DIR_LIST;
425 io.in.open_disposition = NTCREATEX_DISP_OPEN;
426 io.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_DELETE;
427 io.in.create_options = NTCREATEX_OPTIONS_ASYNC_ALERT;
430 status = smb2_create(tree, tree, &io);
431 NT_STATUS_NOT_OK_RETURN(status);
433 *handle = io.out.file.handle;