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/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26 #include "lib/cmdline/popt_common.h"
27 #include "lib/events/events.h"
28 #include "system/time.h"
32 close a handle with SMB2
34 NTSTATUS smb2_util_close(struct smb2_tree *tree, struct smb2_handle h)
41 return smb2_close(tree, &c);
45 unlink a file with SMB2
47 NTSTATUS smb2_util_unlink(struct smb2_tree *tree, const char *fname)
49 struct smb2_create io;
53 io.in.access_mask = SEC_RIGHTS_FILE_ALL;
54 io.in.file_attr = FILE_ATTRIBUTE_NORMAL;
55 io.in.open_disposition = NTCREATEX_DISP_OPEN;
57 NTCREATEX_SHARE_ACCESS_DELETE|
58 NTCREATEX_SHARE_ACCESS_READ|
59 NTCREATEX_SHARE_ACCESS_WRITE;
60 io.in.create_options = NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
63 status = smb2_create(tree, tree, &io);
64 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
67 NT_STATUS_NOT_OK_RETURN(status);
69 return smb2_util_close(tree, io.out.handle);
73 write to a file on SMB2
75 NTSTATUS smb2_util_write(struct smb2_tree *tree,
76 struct smb2_handle handle,
77 const void *buf, off_t offset, size_t size)
84 w.in.data = data_blob_const(buf, size);
86 return smb2_write(tree, &w);
90 create a complex file/dir using the SMB2 protocol
92 static NTSTATUS smb2_create_complex(struct smb2_tree *tree, const char *fname,
93 struct smb2_handle *handle, BOOL dir)
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);
103 smb2_util_unlink(tree, fname);
105 io.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
106 io.in.file_attr = FILE_ATTRIBUTE_NORMAL;
107 io.in.open_disposition = NTCREATEX_DISP_OVERWRITE_IF;
109 NTCREATEX_SHARE_ACCESS_DELETE|
110 NTCREATEX_SHARE_ACCESS_READ|
111 NTCREATEX_SHARE_ACCESS_WRITE;
112 io.in.create_options = 0;
115 io.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
116 io.in.share_access &= ~NTCREATEX_SHARE_ACCESS_DELETE;
117 io.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
118 io.in.open_disposition = NTCREATEX_DISP_CREATE;
121 if (strchr(fname, ':') == NULL) {
123 io.in.eas.num_eas = 2;
124 io.in.eas.eas = talloc_array(tmp_ctx, struct ea_struct, 2);
125 io.in.eas.eas[0].flags = 0;
126 io.in.eas.eas[0].name.s = "EAONE";
127 io.in.eas.eas[0].value = data_blob_talloc(tmp_ctx, "VALUE1", 6);
128 io.in.eas.eas[1].flags = 0;
129 io.in.eas.eas[1].name.s = "SECONDEA";
130 io.in.eas.eas[1].value = data_blob_talloc(tmp_ctx, "ValueTwo", 8);
133 status = smb2_create(tree, tmp_ctx, &io);
134 talloc_free(tmp_ctx);
135 NT_STATUS_NOT_OK_RETURN(status);
137 *handle = io.out.handle;
140 status = smb2_util_write(tree, *handle, buf, 0, sizeof(buf));
141 NT_STATUS_NOT_OK_RETURN(status);
144 /* make sure all the timestamps aren't the same, and are also
145 in different DST zones*/
146 setfile.generic.level = RAW_SFILEINFO_BASIC_INFORMATION;
147 setfile.generic.file.handle = *handle;
149 setfile.basic_info.in.create_time = t + 9*30*24*60*60;
150 setfile.basic_info.in.access_time = t + 6*30*24*60*60;
151 setfile.basic_info.in.write_time = t + 3*30*24*60*60;
152 setfile.basic_info.in.change_time = t + 1*30*24*60*60;
153 setfile.basic_info.in.attrib = FILE_ATTRIBUTE_NORMAL;
155 status = smb2_setinfo_file(tree, &setfile);
156 if (!NT_STATUS_IS_OK(status)) {
157 printf("Failed to setup file times - %s\n", nt_errstr(status));
160 /* make sure all the timestamps aren't the same */
161 fileinfo.generic.level = RAW_FILEINFO_BASIC_INFORMATION;
162 fileinfo.generic.in.handle = *handle;
164 status = smb2_getinfo_file(tree, tree, &fileinfo);
165 if (!NT_STATUS_IS_OK(status)) {
166 printf("Failed to query file times - %s\n", nt_errstr(status));
169 if (setfile.basic_info.in.create_time != fileinfo.basic_info.out.create_time) {
170 printf("create_time not setup correctly\n");
172 if (setfile.basic_info.in.access_time != fileinfo.basic_info.out.access_time) {
173 printf("access_time not setup correctly\n");
175 if (setfile.basic_info.in.write_time != fileinfo.basic_info.out.write_time) {
176 printf("write_time not setup correctly\n");
183 create a complex file using the SMB2 protocol
185 NTSTATUS smb2_create_complex_file(struct smb2_tree *tree, const char *fname,
186 struct smb2_handle *handle)
188 return smb2_create_complex(tree, fname, handle, False);
192 create a complex dir using the SMB2 protocol
194 NTSTATUS smb2_create_complex_dir(struct smb2_tree *tree, const char *fname,
195 struct smb2_handle *handle)
197 return smb2_create_complex(tree, fname, handle, True);
201 show lots of information about a file
203 void torture_smb2_all_info(struct smb2_tree *tree, struct smb2_handle handle)
206 TALLOC_CTX *tmp_ctx = talloc_new(tree);
207 union smb_fileinfo io;
209 io.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
210 io.generic.in.handle = handle;
212 status = smb2_getinfo_file(tree, tmp_ctx, &io);
213 if (!NT_STATUS_IS_OK(status)) {
214 DEBUG(0,("getinfo failed - %s\n", nt_errstr(status)));
215 talloc_free(tmp_ctx);
219 d_printf("all_info for '%s'\n", io.all_info2.out.fname.s);
220 d_printf("\tcreate_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.create_time));
221 d_printf("\taccess_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.access_time));
222 d_printf("\twrite_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.write_time));
223 d_printf("\tchange_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.change_time));
224 d_printf("\tattrib: 0x%x\n", io.all_info2.out.attrib);
225 d_printf("\tunknown1: 0x%x\n", io.all_info2.out.unknown1);
226 d_printf("\talloc_size: %llu\n", (long long)io.all_info2.out.alloc_size);
227 d_printf("\tsize: %llu\n", (long long)io.all_info2.out.size);
228 d_printf("\tnlink: %u\n", io.all_info2.out.nlink);
229 d_printf("\tdelete_pending: %u\n", io.all_info2.out.delete_pending);
230 d_printf("\tdirectory: %u\n", io.all_info2.out.directory);
231 d_printf("\tfile_id: %llu\n", (long long)io.all_info2.out.file_id);
232 d_printf("\tea_size: %u\n", io.all_info2.out.ea_size);
233 d_printf("\taccess_mask: 0x%08x\n", io.all_info2.out.access_mask);
234 d_printf("\tposition: 0x%llx\n", (long long)io.all_info2.out.position);
235 d_printf("\tmode: 0x%llx\n", (long long)io.all_info2.out.mode);
237 /* short name, if any */
238 io.generic.level = RAW_FILEINFO_ALT_NAME_INFORMATION;
239 status = smb2_getinfo_file(tree, tmp_ctx, &io);
240 if (NT_STATUS_IS_OK(status)) {
241 d_printf("\tshort name: '%s'\n", io.alt_name_info.out.fname.s);
244 /* the EAs, if any */
245 io.generic.level = RAW_FILEINFO_SMB2_ALL_EAS;
246 status = smb2_getinfo_file(tree, tmp_ctx, &io);
247 if (NT_STATUS_IS_OK(status)) {
249 for (i=0;i<io.all_eas.out.num_eas;i++) {
250 d_printf("\tEA[%d] flags=%d len=%d '%s'\n", i,
251 io.all_eas.out.eas[i].flags,
252 (int)io.all_eas.out.eas[i].value.length,
253 io.all_eas.out.eas[i].name.s);
257 /* streams, if available */
258 io.generic.level = RAW_FILEINFO_STREAM_INFORMATION;
259 status = smb2_getinfo_file(tree, tmp_ctx, &io);
260 if (NT_STATUS_IS_OK(status)) {
262 for (i=0;i<io.stream_info.out.num_streams;i++) {
263 d_printf("\tstream %d:\n", i);
264 d_printf("\t\tsize %ld\n",
265 (long)io.stream_info.out.streams[i].size);
266 d_printf("\t\talloc size %ld\n",
267 (long)io.stream_info.out.streams[i].alloc_size);
268 d_printf("\t\tname %s\n", io.stream_info.out.streams[i].stream_name.s);
273 /* the security descriptor */
274 io.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
275 io.query_secdesc.secinfo_flags =
276 SECINFO_OWNER|SECINFO_GROUP|
278 status = smb2_getinfo_file(tree, tmp_ctx, &io);
279 if (NT_STATUS_IS_OK(status)) {
280 NDR_PRINT_DEBUG(security_descriptor, io.query_secdesc.out.sd);
284 talloc_free(tmp_ctx);
289 open a smb2 connection
291 BOOL torture_smb2_connection(TALLOC_CTX *mem_ctx, struct smb2_tree **tree)
294 const char *host = lp_parm_string(-1, "torture", "host");
295 const char *share = lp_parm_string(-1, "torture", "share");
296 struct cli_credentials *credentials = cmdline_credentials;
298 status = smb2_connect(mem_ctx, host, share, credentials, tree,
299 event_context_find(mem_ctx));
300 if (!NT_STATUS_IS_OK(status)) {
301 printf("Failed to connect to SMB2 share \\\\%s\\%s - %s\n",
302 host, share, nt_errstr(status));
310 create and return a handle to a test file
312 NTSTATUS torture_smb2_testfile(struct smb2_tree *tree, const char *fname,
313 struct smb2_handle *handle)
315 struct smb2_create io;
320 io.in.oplock_flags = 0;
321 io.in.access_mask = SEC_RIGHTS_FILE_ALL;
322 io.in.file_attr = FILE_ATTRIBUTE_NORMAL;
323 io.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
325 NTCREATEX_SHARE_ACCESS_DELETE|
326 NTCREATEX_SHARE_ACCESS_READ|
327 NTCREATEX_SHARE_ACCESS_WRITE;
328 io.in.create_options = 0;
331 status = smb2_create(tree, tree, &io);
332 NT_STATUS_NOT_OK_RETURN(status);
334 *handle = io.out.handle;
339 r.in.handle = *handle;
341 smb2_read(tree, tree, &r);
347 create and return a handle to a test directory
349 NTSTATUS torture_smb2_testdir(struct smb2_tree *tree, const char *fname,
350 struct smb2_handle *handle)
352 struct smb2_create io;
356 io.in.oplock_flags = 0;
357 io.in.access_mask = SEC_RIGHTS_DIR_ALL;
358 io.in.file_attr = FILE_ATTRIBUTE_DIRECTORY;
359 io.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
360 io.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE;
361 io.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
364 status = smb2_create(tree, tree, &io);
365 NT_STATUS_NOT_OK_RETURN(status);
367 *handle = io.out.handle;
374 create a complex file using the old SMB protocol, to make it easier to
375 find fields in SMB2 getinfo levels
377 NTSTATUS torture_setup_complex_file(struct smb2_tree *tree, const char *fname)
379 struct smb2_handle handle;
380 NTSTATUS status = smb2_create_complex_file(tree, fname, &handle);
381 NT_STATUS_NOT_OK_RETURN(status);
382 return smb2_util_close(tree, handle);
387 create a complex dir using the old SMB protocol, to make it easier to
388 find fields in SMB2 getinfo levels
390 NTSTATUS torture_setup_complex_dir(struct smb2_tree *tree, const char *fname)
392 struct smb2_handle handle;
393 NTSTATUS status = smb2_create_complex_dir(tree, fname, &handle);
394 NT_STATUS_NOT_OK_RETURN(status);
395 return smb2_util_close(tree, handle);
400 return a handle to the root of the share
402 NTSTATUS smb2_util_roothandle(struct smb2_tree *tree, struct smb2_handle *handle)
404 struct smb2_create io;
408 io.in.oplock_flags = 0;
409 io.in.access_mask = SEC_STD_SYNCHRONIZE | SEC_DIR_READ_ATTRIBUTE | SEC_DIR_LIST;
411 io.in.open_disposition = NTCREATEX_DISP_OPEN;
412 io.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_DELETE;
413 io.in.create_options = NTCREATEX_OPTIONS_ASYNC_ALERT;
416 status = smb2_create(tree, tree, &io);
417 NT_STATUS_NOT_OK_RETURN(status);
419 *handle = io.out.handle;