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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/smb2/smb2.h"
26 #include "libcli/smb2/smb2_calls.h"
27 #include "lib/cmdline/popt_common.h"
28 #include "lib/events/events.h"
29 #include "system/time.h"
33 close a handle with SMB2
35 NTSTATUS smb2_util_close(struct smb2_tree *tree, struct smb2_handle h)
42 return smb2_close(tree, &c);
46 unlink a file with SMB2
48 NTSTATUS smb2_util_unlink(struct smb2_tree *tree, const char *fname)
50 struct smb2_create io;
54 io.in.access_mask = SEC_RIGHTS_FILE_ALL;
55 io.in.file_attr = FILE_ATTRIBUTE_NORMAL;
56 io.in.open_disposition = NTCREATEX_DISP_OPEN;
58 NTCREATEX_SHARE_ACCESS_DELETE|
59 NTCREATEX_SHARE_ACCESS_READ|
60 NTCREATEX_SHARE_ACCESS_WRITE;
61 io.in.create_options = NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
64 status = smb2_create(tree, tree, &io);
65 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
68 NT_STATUS_NOT_OK_RETURN(status);
70 return smb2_util_close(tree, io.out.handle);
74 write to a file on SMB2
76 NTSTATUS smb2_util_write(struct smb2_tree *tree,
77 struct smb2_handle handle,
78 const void *buf, off_t offset, size_t size)
85 w.in.data = data_blob_const(buf, size);
87 return smb2_write(tree, &w);
91 create a complex file using the SMB2 protocol
93 NTSTATUS smb2_create_complex_file(struct smb2_tree *tree, const char *fname, struct smb2_handle *handle)
95 TALLOC_CTX *tmp_ctx = talloc_new(tree);
97 struct smb2_create io;
98 union smb_setfileinfo setfile;
99 union smb_fileinfo fileinfo;
100 time_t t = (time(NULL) & ~1);
104 io.in.access_mask = SEC_RIGHTS_FILE_ALL;
105 io.in.file_attr = FILE_ATTRIBUTE_NORMAL;
106 io.in.open_disposition = NTCREATEX_DISP_OVERWRITE_IF;
108 NTCREATEX_SHARE_ACCESS_DELETE|
109 NTCREATEX_SHARE_ACCESS_READ|
110 NTCREATEX_SHARE_ACCESS_WRITE;
111 io.in.create_options = 0;
114 io.in.sd = security_descriptor_create(tmp_ctx,
116 SID_NT_AUTHENTICATED_USERS,
117 SEC_ACE_TYPE_ACCESS_ALLOWED,
118 SEC_RIGHTS_FILE_ALL | SEC_STD_ALL,
121 SEC_ACE_TYPE_ACCESS_ALLOWED,
122 SEC_RIGHTS_FILE_READ | SEC_STD_ALL,
126 if (strchr(fname, ':') == NULL) {
128 io.in.eas.num_eas = 2;
129 io.in.eas.eas = talloc_array(tmp_ctx, struct ea_struct, 2);
130 io.in.eas.eas[0].flags = 0;
131 io.in.eas.eas[0].name.s = "EAONE";
132 io.in.eas.eas[0].value = data_blob_talloc(tmp_ctx, "VALUE1", 6);
133 io.in.eas.eas[1].flags = 0;
134 io.in.eas.eas[1].name.s = "SECONDEA";
135 io.in.eas.eas[1].value = data_blob_talloc(tmp_ctx, "ValueTwo", 8);
138 status = smb2_create(tree, tmp_ctx, &io);
139 talloc_free(tmp_ctx);
140 NT_STATUS_NOT_OK_RETURN(status);
142 *handle = io.out.handle;
144 status = smb2_util_write(tree, *handle, buf, 0, sizeof(buf));
145 NT_STATUS_NOT_OK_RETURN(status);
147 /* make sure all the timestamps aren't the same, and are also
148 in different DST zones*/
149 setfile.generic.level = RAW_SFILEINFO_BASIC_INFORMATION;
150 setfile.generic.file.handle = *handle;
152 setfile.basic_info.in.create_time = t + 9*30*24*60*60;
153 setfile.basic_info.in.access_time = t + 6*30*24*60*60;
154 setfile.basic_info.in.write_time = t + 3*30*24*60*60;
155 setfile.basic_info.in.change_time = t + 1*30*24*60*60;
156 setfile.basic_info.in.attrib = FILE_ATTRIBUTE_NORMAL;
158 status = smb2_setinfo_file(tree, &setfile);
159 if (!NT_STATUS_IS_OK(status)) {
160 printf("Failed to setup file times - %s\n", nt_errstr(status));
163 /* make sure all the timestamps aren't the same */
164 fileinfo.generic.level = RAW_FILEINFO_BASIC_INFORMATION;
165 fileinfo.generic.in.handle = *handle;
167 status = smb2_getinfo_file(tree, tree, &fileinfo);
168 if (!NT_STATUS_IS_OK(status)) {
169 printf("Failed to query file times - %s\n", nt_errstr(status));
172 if (setfile.basic_info.in.create_time != fileinfo.basic_info.out.create_time) {
173 printf("create_time not setup correctly\n");
175 if (setfile.basic_info.in.access_time != fileinfo.basic_info.out.access_time) {
176 printf("access_time not setup correctly\n");
178 if (setfile.basic_info.in.write_time != fileinfo.basic_info.out.write_time) {
179 printf("write_time not setup correctly\n");
186 show lots of information about a file
188 void torture_smb2_all_info(struct smb2_tree *tree, struct smb2_handle handle)
191 TALLOC_CTX *tmp_ctx = talloc_new(tree);
192 union smb_fileinfo io;
194 io.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
195 io.generic.in.handle = handle;
197 status = smb2_getinfo_file(tree, tmp_ctx, &io);
198 if (!NT_STATUS_IS_OK(status)) {
199 DEBUG(0,("getinfo failed - %s\n", nt_errstr(status)));
200 talloc_free(tmp_ctx);
204 d_printf("\tcreate_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.create_time));
205 d_printf("\taccess_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.access_time));
206 d_printf("\twrite_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.write_time));
207 d_printf("\tchange_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.change_time));
208 d_printf("\tattrib: 0x%x\n", io.all_info2.out.attrib);
209 d_printf("\tunknown1: 0x%x\n", io.all_info2.out.unknown1);
210 d_printf("\talloc_size: %llu\n", (uint64_t)io.all_info2.out.alloc_size);
211 d_printf("\tsize: %llu\n", (uint64_t)io.all_info2.out.size);
212 d_printf("\tnlink: %u\n", io.all_info2.out.nlink);
213 d_printf("\tdelete_pending: %u\n", io.all_info2.out.delete_pending);
214 d_printf("\tdirectory: %u\n", io.all_info2.out.directory);
215 d_printf("\tfile_id: %llu\n", io.all_info2.out.file_id);
216 d_printf("\tea_size: %u\n", io.all_info2.out.ea_size);
217 d_printf("\taccess_mask: 0x%08x\n", io.all_info2.out.access_mask);
218 d_printf("\tunknown2: 0x%llx\n", io.all_info2.out.unknown2);
219 d_printf("\tunknown3: 0x%llx\n", io.all_info2.out.unknown3);
220 d_printf("\tfname: '%s'\n", io.all_info2.out.fname.s);
222 /* short name, if any */
223 io.generic.level = RAW_FILEINFO_ALT_NAME_INFORMATION;
224 status = smb2_getinfo_file(tree, tmp_ctx, &io);
225 if (NT_STATUS_IS_OK(status)) {
226 d_printf("\tshort name: '%s'\n", io.alt_name_info.out.fname.s);
229 /* the EAs, if any */
230 io.generic.level = RAW_FILEINFO_SMB2_ALL_EAS;
231 status = smb2_getinfo_file(tree, tmp_ctx, &io);
232 if (NT_STATUS_IS_OK(status)) {
234 for (i=0;i<io.all_eas.out.num_eas;i++) {
235 d_printf("\tEA[%d] flags=%d len=%d '%s'\n", i,
236 io.all_eas.out.eas[i].flags,
237 (int)io.all_eas.out.eas[i].value.length,
238 io.all_eas.out.eas[i].name.s);
242 /* streams, if available */
243 io.generic.level = RAW_FILEINFO_STREAM_INFORMATION;
244 status = smb2_getinfo_file(tree, tmp_ctx, &io);
245 if (NT_STATUS_IS_OK(status)) {
247 for (i=0;i<io.stream_info.out.num_streams;i++) {
248 d_printf("\tstream %d:\n", i);
249 d_printf("\t\tsize %ld\n",
250 (long)io.stream_info.out.streams[i].size);
251 d_printf("\t\talloc size %ld\n",
252 (long)io.stream_info.out.streams[i].alloc_size);
253 d_printf("\t\tname %s\n", io.stream_info.out.streams[i].stream_name.s);
257 talloc_free(tmp_ctx);
262 open a smb2 connection
264 BOOL torture_smb2_connection(TALLOC_CTX *mem_ctx, struct smb2_tree **tree)
267 const char *host = lp_parm_string(-1, "torture", "host");
268 const char *share = lp_parm_string(-1, "torture", "share");
269 struct cli_credentials *credentials = cmdline_credentials;
271 status = smb2_connect(mem_ctx, host, share, credentials, tree,
272 event_context_find(mem_ctx));
273 if (!NT_STATUS_IS_OK(status)) {
274 printf("Failed to connect to SMB2 share \\\\%s\\%s - %s\n",
275 host, share, nt_errstr(status));
283 create and return a handle to a test file
285 NTSTATUS torture_smb2_testfile(struct smb2_tree *tree, const char *fname,
286 struct smb2_handle *handle)
288 struct smb2_create io;
293 io.in.oplock_flags = 0;
294 io.in.access_mask = SEC_RIGHTS_FILE_ALL;
295 io.in.file_attr = FILE_ATTRIBUTE_NORMAL;
296 io.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
298 NTCREATEX_SHARE_ACCESS_DELETE|
299 NTCREATEX_SHARE_ACCESS_READ|
300 NTCREATEX_SHARE_ACCESS_WRITE;
301 io.in.create_options = 0;
304 status = smb2_create(tree, tree, &io);
305 NT_STATUS_NOT_OK_RETURN(status);
307 *handle = io.out.handle;
312 r.in.handle = *handle;
314 smb2_read(tree, tree, &r);
320 create and return a handle to a test directory
322 NTSTATUS torture_smb2_testdir(struct smb2_tree *tree, const char *fname,
323 struct smb2_handle *handle)
325 struct smb2_create io;
329 io.in.oplock_flags = 0;
330 io.in.access_mask = SEC_RIGHTS_DIR_ALL;
331 io.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
332 io.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
333 io.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE;
334 io.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
337 status = smb2_create(tree, tree, &io);
338 NT_STATUS_NOT_OK_RETURN(status);
340 *handle = io.out.handle;
347 create a complex file using the old SMB protocol, to make it easier to
348 find fields in SMB2 getinfo levels
350 BOOL torture_setup_complex_file(const char *fname)
352 struct smbcli_state *cli;
355 if (!torture_open_connection(&cli)) {
359 fnum = create_complex_file(cli, cli, fname);
362 torture_all_info(cli->tree, fname);
370 create a complex directory using the old SMB protocol, to make it easier to
371 find fields in SMB2 getinfo levels
373 BOOL torture_setup_complex_dir(const char *dname)
375 struct smbcli_state *cli;
378 if (!torture_open_connection(&cli)) {
382 fnum = create_complex_dir(cli, cli, dname);
385 torture_all_info(cli->tree, dname);