Convert mtime from a time_t to a struct timespec.
[Samba.git] / source4 / torture / smb2 / util.c
blob8feb96857d1b13f5c4ab3d818e5eb5d64e134137
1 /*
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/>.
22 #include "includes.h"
23 #include "libcli/security/security_descriptor.h"
24 #include "libcli/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26 #include "lib/cmdline/popt_common.h"
27 #include "system/time.h"
28 #include "librpc/gen_ndr/ndr_security.h"
29 #include "param/param.h"
30 #include "libcli/resolve/resolve.h"
32 #include "torture/torture.h"
36 write to a file on SMB2
38 NTSTATUS smb2_util_write(struct smb2_tree *tree,
39 struct smb2_handle handle,
40 const void *buf, off_t offset, size_t size)
42 struct smb2_write w;
44 ZERO_STRUCT(w);
45 w.in.file.handle = handle;
46 w.in.offset = offset;
47 w.in.data = data_blob_const(buf, size);
49 return smb2_write(tree, &w);
53 create a complex file/dir using the SMB2 protocol
55 static NTSTATUS smb2_create_complex(struct smb2_tree *tree, const char *fname,
56 struct smb2_handle *handle, bool dir)
58 TALLOC_CTX *tmp_ctx = talloc_new(tree);
59 char buf[7] = "abc";
60 struct smb2_create io;
61 union smb_setfileinfo setfile;
62 union smb_fileinfo fileinfo;
63 time_t t = (time(NULL) & ~1);
64 NTSTATUS status;
66 smb2_util_unlink(tree, fname);
67 ZERO_STRUCT(io);
68 io.in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED;
69 io.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
70 io.in.create_disposition = NTCREATEX_DISP_OVERWRITE_IF;
71 io.in.share_access =
72 NTCREATEX_SHARE_ACCESS_DELETE|
73 NTCREATEX_SHARE_ACCESS_READ|
74 NTCREATEX_SHARE_ACCESS_WRITE;
75 io.in.create_options = 0;
76 io.in.fname = fname;
77 if (dir) {
78 io.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
79 io.in.share_access &= ~NTCREATEX_SHARE_ACCESS_DELETE;
80 io.in.file_attributes = FILE_ATTRIBUTE_DIRECTORY;
81 io.in.create_disposition = NTCREATEX_DISP_CREATE;
84 /* it seems vista is now fussier about alignment? */
85 if (strchr(fname, ':') == NULL) {
86 /* setup some EAs */
87 io.in.eas.num_eas = 2;
88 io.in.eas.eas = talloc_array(tmp_ctx, struct ea_struct, 2);
89 io.in.eas.eas[0].flags = 0;
90 io.in.eas.eas[0].name.s = "EAONE";
91 io.in.eas.eas[0].value = data_blob_talloc(tmp_ctx, "VALUE1", 6);
92 io.in.eas.eas[1].flags = 0;
93 io.in.eas.eas[1].name.s = "SECONDEA";
94 io.in.eas.eas[1].value = data_blob_talloc(tmp_ctx, "ValueTwo", 8);
97 status = smb2_create(tree, tmp_ctx, &io);
98 talloc_free(tmp_ctx);
99 NT_STATUS_NOT_OK_RETURN(status);
101 *handle = io.out.file.handle;
103 if (!dir) {
104 status = smb2_util_write(tree, *handle, buf, 0, sizeof(buf));
105 NT_STATUS_NOT_OK_RETURN(status);
108 /* make sure all the timestamps aren't the same, and are also
109 in different DST zones*/
110 setfile.generic.level = RAW_SFILEINFO_BASIC_INFORMATION;
111 setfile.generic.in.file.handle = *handle;
113 unix_to_nt_time(&setfile.basic_info.in.create_time, t + 9*30*24*60*60);
114 unix_to_nt_time(&setfile.basic_info.in.access_time, t + 6*30*24*60*60);
115 unix_to_nt_time(&setfile.basic_info.in.write_time, t + 3*30*24*60*60);
116 unix_to_nt_time(&setfile.basic_info.in.change_time, t + 1*30*24*60*60);
117 setfile.basic_info.in.attrib = FILE_ATTRIBUTE_NORMAL;
119 status = smb2_setinfo_file(tree, &setfile);
120 if (!NT_STATUS_IS_OK(status)) {
121 printf("Failed to setup file times - %s\n", nt_errstr(status));
122 return status;
125 /* make sure all the timestamps aren't the same */
126 fileinfo.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
127 fileinfo.generic.in.file.handle = *handle;
129 status = smb2_getinfo_file(tree, tree, &fileinfo);
130 if (!NT_STATUS_IS_OK(status)) {
131 printf("Failed to query file times - %s\n", nt_errstr(status));
132 return status;
136 #define CHECK_TIME(field) do {\
137 if (setfile.basic_info.in.field != fileinfo.all_info2.out.field) { \
138 printf("(%s) " #field " not setup correctly: %s(%llu) => %s(%llu)\n", \
139 __location__, \
140 nt_time_string(tree, setfile.basic_info.in.field), \
141 (unsigned long long)setfile.basic_info.in.field, \
142 nt_time_string(tree, fileinfo.basic_info.out.field), \
143 (unsigned long long)fileinfo.basic_info.out.field); \
144 status = NT_STATUS_INVALID_PARAMETER; \
146 } while (0)
148 CHECK_TIME(create_time);
149 CHECK_TIME(access_time);
150 CHECK_TIME(write_time);
151 CHECK_TIME(change_time);
153 return status;
157 create a complex file using the SMB2 protocol
159 NTSTATUS smb2_create_complex_file(struct smb2_tree *tree, const char *fname,
160 struct smb2_handle *handle)
162 return smb2_create_complex(tree, fname, handle, false);
166 create a complex dir using the SMB2 protocol
168 NTSTATUS smb2_create_complex_dir(struct smb2_tree *tree, const char *fname,
169 struct smb2_handle *handle)
171 return smb2_create_complex(tree, fname, handle, true);
175 show lots of information about a file
177 void torture_smb2_all_info(struct smb2_tree *tree, struct smb2_handle handle)
179 NTSTATUS status;
180 TALLOC_CTX *tmp_ctx = talloc_new(tree);
181 union smb_fileinfo io;
183 io.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
184 io.generic.in.file.handle = handle;
186 status = smb2_getinfo_file(tree, tmp_ctx, &io);
187 if (!NT_STATUS_IS_OK(status)) {
188 DEBUG(0,("getinfo failed - %s\n", nt_errstr(status)));
189 talloc_free(tmp_ctx);
190 return;
193 d_printf("all_info for '%s'\n", io.all_info2.out.fname.s);
194 d_printf("\tcreate_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.create_time));
195 d_printf("\taccess_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.access_time));
196 d_printf("\twrite_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.write_time));
197 d_printf("\tchange_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.change_time));
198 d_printf("\tattrib: 0x%x\n", io.all_info2.out.attrib);
199 d_printf("\tunknown1: 0x%x\n", io.all_info2.out.unknown1);
200 d_printf("\talloc_size: %llu\n", (long long)io.all_info2.out.alloc_size);
201 d_printf("\tsize: %llu\n", (long long)io.all_info2.out.size);
202 d_printf("\tnlink: %u\n", io.all_info2.out.nlink);
203 d_printf("\tdelete_pending: %u\n", io.all_info2.out.delete_pending);
204 d_printf("\tdirectory: %u\n", io.all_info2.out.directory);
205 d_printf("\tfile_id: %llu\n", (long long)io.all_info2.out.file_id);
206 d_printf("\tea_size: %u\n", io.all_info2.out.ea_size);
207 d_printf("\taccess_mask: 0x%08x\n", io.all_info2.out.access_mask);
208 d_printf("\tposition: 0x%llx\n", (long long)io.all_info2.out.position);
209 d_printf("\tmode: 0x%llx\n", (long long)io.all_info2.out.mode);
211 /* short name, if any */
212 io.generic.level = RAW_FILEINFO_ALT_NAME_INFORMATION;
213 status = smb2_getinfo_file(tree, tmp_ctx, &io);
214 if (NT_STATUS_IS_OK(status)) {
215 d_printf("\tshort name: '%s'\n", io.alt_name_info.out.fname.s);
218 /* the EAs, if any */
219 io.generic.level = RAW_FILEINFO_SMB2_ALL_EAS;
220 status = smb2_getinfo_file(tree, tmp_ctx, &io);
221 if (NT_STATUS_IS_OK(status)) {
222 int i;
223 for (i=0;i<io.all_eas.out.num_eas;i++) {
224 d_printf("\tEA[%d] flags=%d len=%d '%s'\n", i,
225 io.all_eas.out.eas[i].flags,
226 (int)io.all_eas.out.eas[i].value.length,
227 io.all_eas.out.eas[i].name.s);
231 /* streams, if available */
232 io.generic.level = RAW_FILEINFO_STREAM_INFORMATION;
233 status = smb2_getinfo_file(tree, tmp_ctx, &io);
234 if (NT_STATUS_IS_OK(status)) {
235 int i;
236 for (i=0;i<io.stream_info.out.num_streams;i++) {
237 d_printf("\tstream %d:\n", i);
238 d_printf("\t\tsize %ld\n",
239 (long)io.stream_info.out.streams[i].size);
240 d_printf("\t\talloc size %ld\n",
241 (long)io.stream_info.out.streams[i].alloc_size);
242 d_printf("\t\tname %s\n", io.stream_info.out.streams[i].stream_name.s);
246 if (DEBUGLVL(1)) {
247 /* the security descriptor */
248 io.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
249 io.query_secdesc.in.secinfo_flags =
250 SECINFO_OWNER|SECINFO_GROUP|
251 SECINFO_DACL;
252 status = smb2_getinfo_file(tree, tmp_ctx, &io);
253 if (NT_STATUS_IS_OK(status)) {
254 NDR_PRINT_DEBUG(security_descriptor, io.query_secdesc.out.sd);
258 talloc_free(tmp_ctx);
263 open a smb2 connection
265 bool torture_smb2_connection(struct torture_context *tctx, struct smb2_tree **tree)
267 NTSTATUS status;
268 const char *host = torture_setting_string(tctx, "host", NULL);
269 const char *share = torture_setting_string(tctx, "share", NULL);
270 struct cli_credentials *credentials = cmdline_credentials;
271 struct smbcli_options options;
273 lpcfg_smbcli_options(tctx->lp_ctx, &options);
275 status = smb2_connect(tctx, host,
276 lpcfg_smb_ports(tctx->lp_ctx),
277 share,
278 lpcfg_resolve_context(tctx->lp_ctx),
279 credentials, tree,
280 tctx->ev, &options,
281 lpcfg_socket_options(tctx->lp_ctx),
282 lpcfg_gensec_settings(tctx, tctx->lp_ctx)
284 if (!NT_STATUS_IS_OK(status)) {
285 printf("Failed to connect to SMB2 share \\\\%s\\%s - %s\n",
286 host, share, nt_errstr(status));
287 return false;
289 return true;
294 create and return a handle to a test file
296 NTSTATUS torture_smb2_testfile(struct smb2_tree *tree, const char *fname,
297 struct smb2_handle *handle)
299 struct smb2_create io;
300 struct smb2_read r;
301 NTSTATUS status;
303 ZERO_STRUCT(io);
304 io.in.oplock_level = 0;
305 io.in.desired_access = SEC_RIGHTS_FILE_ALL;
306 io.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
307 io.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
308 io.in.share_access =
309 NTCREATEX_SHARE_ACCESS_DELETE|
310 NTCREATEX_SHARE_ACCESS_READ|
311 NTCREATEX_SHARE_ACCESS_WRITE;
312 io.in.create_options = 0;
313 io.in.fname = fname;
315 status = smb2_create(tree, tree, &io);
316 NT_STATUS_NOT_OK_RETURN(status);
318 *handle = io.out.file.handle;
320 ZERO_STRUCT(r);
321 r.in.file.handle = *handle;
322 r.in.length = 5;
323 r.in.offset = 0;
325 // What is the purpose of this? Server returns EOF.
326 smb2_read(tree, tree, &r);
328 return NT_STATUS_OK;
332 create and return a handle to a test directory
334 NTSTATUS torture_smb2_testdir(struct smb2_tree *tree, const char *fname,
335 struct smb2_handle *handle)
337 struct smb2_create io;
338 NTSTATUS status;
340 ZERO_STRUCT(io);
341 io.in.oplock_level = 0;
342 io.in.desired_access = SEC_RIGHTS_DIR_ALL;
343 io.in.file_attributes = FILE_ATTRIBUTE_DIRECTORY;
344 io.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
345 io.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE;
346 io.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
347 io.in.fname = fname;
349 status = smb2_create(tree, tree, &io);
350 NT_STATUS_NOT_OK_RETURN(status);
352 *handle = io.out.file.handle;
354 return NT_STATUS_OK;
359 create a complex file using SMB2, to make it easier to
360 find fields in SMB2 getinfo levels
362 NTSTATUS torture_setup_complex_file(struct smb2_tree *tree, const char *fname)
364 struct smb2_handle handle;
365 NTSTATUS status = smb2_create_complex_file(tree, fname, &handle);
366 NT_STATUS_NOT_OK_RETURN(status);
367 return smb2_util_close(tree, handle);
372 create a complex dir using SMB2, to make it easier to
373 find fields in SMB2 getinfo levels
375 NTSTATUS torture_setup_complex_dir(struct smb2_tree *tree, const char *fname)
377 struct smb2_handle handle;
378 NTSTATUS status = smb2_create_complex_dir(tree, fname, &handle);
379 NT_STATUS_NOT_OK_RETURN(status);
380 return smb2_util_close(tree, handle);
385 return a handle to the root of the share
387 NTSTATUS smb2_util_roothandle(struct smb2_tree *tree, struct smb2_handle *handle)
389 struct smb2_create io;
390 NTSTATUS status;
392 ZERO_STRUCT(io);
393 io.in.oplock_level = 0;
394 io.in.desired_access = SEC_STD_SYNCHRONIZE | SEC_DIR_READ_ATTRIBUTE | SEC_DIR_LIST;
395 io.in.file_attributes = 0;
396 io.in.create_disposition = NTCREATEX_DISP_OPEN;
397 io.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_DELETE;
398 io.in.create_options = NTCREATEX_OPTIONS_ASYNC_ALERT;
399 io.in.fname = NULL;
401 status = smb2_create(tree, tree, &io);
402 NT_STATUS_NOT_OK_RETURN(status);
404 *handle = io.out.file.handle;
406 return NT_STATUS_OK;
409 /* Comparable to torture_setup_dir, but for SMB2. */
410 bool smb2_util_setup_dir(struct torture_context *tctx, struct smb2_tree *tree,
411 const char *dname)
413 NTSTATUS status;
415 /* XXX: smb_raw_exit equivalent?
416 smb_raw_exit(cli->session); */
417 if (smb2_deltree(tree, dname) == -1) {
418 torture_result(tctx, TORTURE_ERROR, "Unable to deltree when setting up %s.\n", dname);
419 return false;
422 status = smb2_util_mkdir(tree, dname);
423 if (NT_STATUS_IS_ERR(status)) {
424 torture_result(tctx, TORTURE_ERROR, "Unable to mkdir when setting up %s - %s\n", dname,
425 nt_errstr(status));
426 return false;
429 return true;
432 #define CHECK_STATUS(status, correct) do { \
433 if (!NT_STATUS_EQUAL(status, correct)) { \
434 torture_result(tctx, TORTURE_FAIL, "(%s) Incorrect status %s - should be %s\n", \
435 __location__, nt_errstr(status), nt_errstr(correct)); \
436 ret = false; \
437 goto done; \
438 }} while (0)
441 * Helper function to verify a security descriptor, by querying
442 * and comparing against the passed in sd.
444 bool smb2_util_verify_sd(TALLOC_CTX *tctx, struct smb2_tree *tree,
445 struct smb2_handle handle, struct security_descriptor *sd)
447 NTSTATUS status;
448 bool ret = true;
449 union smb_fileinfo q = {};
451 q.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
452 q.query_secdesc.in.file.handle = handle;
453 q.query_secdesc.in.secinfo_flags =
454 SECINFO_OWNER |
455 SECINFO_GROUP |
456 SECINFO_DACL;
457 status = smb2_getinfo_file(tree, tctx, &q);
458 CHECK_STATUS(status, NT_STATUS_OK);
460 if (!security_acl_equal(
461 q.query_secdesc.out.sd->dacl, sd->dacl)) {
462 torture_warning(tctx, "%s: security descriptors don't match!\n",
463 __location__);
464 torture_warning(tctx, "got:\n");
465 NDR_PRINT_DEBUG(security_descriptor,
466 q.query_secdesc.out.sd);
467 torture_warning(tctx, "expected:\n");
468 NDR_PRINT_DEBUG(security_descriptor, sd);
469 ret = false;
472 done:
473 return ret;
477 * Helper function to verify attributes, by querying
478 * and comparing against the passed in attrib.
480 bool smb2_util_verify_attrib(TALLOC_CTX *tctx, struct smb2_tree *tree,
481 struct smb2_handle handle, uint32_t attrib)
483 NTSTATUS status;
484 bool ret = true;
485 union smb_fileinfo q = {};
487 q.standard.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
488 q.standard.in.file.handle = handle;
489 status = smb2_getinfo_file(tree, tctx, &q);
490 CHECK_STATUS(status, NT_STATUS_OK);
492 q.all_info2.out.attrib &= ~FILE_ATTRIBUTE_ARCHIVE;
494 if (q.all_info2.out.attrib != attrib) {
495 torture_warning(tctx, "%s: attributes don't match! "
496 "got %x, expected %x\n", __location__,
497 (uint32_t)q.standard.out.attrib,
498 (uint32_t)attrib);
499 ret = false;
502 done:
503 return ret;