s4:torture/smb2: let torture_smb2_con_sopt() use smb2_connect()
[Samba.git] / source4 / torture / smb2 / util.c
blob01b1b2f0793aa447781c3ed04e157fdfed06375b
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 "../libcli/smb/smbXcli_base.h"
27 #include "lib/cmdline/cmdline.h"
28 #include "system/time.h"
29 #include "librpc/gen_ndr/ndr_security.h"
30 #include "param/param.h"
31 #include "libcli/resolve/resolve.h"
32 #include "lib/util/tevent_ntstatus.h"
34 #include "torture/torture.h"
35 #include "torture/smb2/proto.h"
36 #include "source4/torture/util.h"
37 #include "libcli/security/dom_sid.h"
38 #include "librpc/gen_ndr/lsa.h"
39 #include "libcli/util/clilsa.h"
43 write to a file on SMB2
45 NTSTATUS smb2_util_write(struct smb2_tree *tree,
46 struct smb2_handle handle,
47 const void *buf, off_t offset, size_t size)
49 struct smb2_write w;
51 ZERO_STRUCT(w);
52 w.in.file.handle = handle;
53 w.in.offset = offset;
54 w.in.data = data_blob_const(buf, size);
56 return smb2_write(tree, &w);
60 create a complex file/dir using the SMB2 protocol
62 static NTSTATUS smb2_create_complex(struct torture_context *tctx,
63 struct smb2_tree *tree,
64 const char *fname,
65 struct smb2_handle *handle,
66 bool dir)
68 TALLOC_CTX *tmp_ctx = talloc_new(tree);
69 char buf[7] = "abc";
70 struct smb2_create io;
71 union smb_setfileinfo setfile;
72 union smb_fileinfo fileinfo;
73 time_t t = (time(NULL) & ~1);
74 NTSTATUS status;
76 smb2_util_unlink(tree, fname);
77 ZERO_STRUCT(io);
78 io.in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED;
79 io.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
80 io.in.create_disposition = NTCREATEX_DISP_OVERWRITE_IF;
81 io.in.share_access =
82 NTCREATEX_SHARE_ACCESS_DELETE|
83 NTCREATEX_SHARE_ACCESS_READ|
84 NTCREATEX_SHARE_ACCESS_WRITE;
85 io.in.create_options = 0;
86 io.in.fname = fname;
87 if (dir) {
88 io.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
89 io.in.share_access &= ~NTCREATEX_SHARE_ACCESS_DELETE;
90 io.in.file_attributes = FILE_ATTRIBUTE_DIRECTORY;
91 io.in.create_disposition = NTCREATEX_DISP_CREATE;
94 /* it seems vista is now fussier about alignment? */
95 if (strchr(fname, ':') == NULL) {
96 /* setup some EAs */
97 io.in.eas.num_eas = 2;
98 io.in.eas.eas = talloc_array(tmp_ctx, struct ea_struct, 2);
99 io.in.eas.eas[0].flags = 0;
100 io.in.eas.eas[0].name.s = "EAONE";
101 io.in.eas.eas[0].value = data_blob_talloc(tmp_ctx, "VALUE1", 6);
102 io.in.eas.eas[1].flags = 0;
103 io.in.eas.eas[1].name.s = "SECONDEA";
104 io.in.eas.eas[1].value = data_blob_talloc(tmp_ctx, "ValueTwo", 8);
107 status = smb2_create(tree, tmp_ctx, &io);
108 if (NT_STATUS_EQUAL(status, NT_STATUS_EAS_NOT_SUPPORTED)) {
109 torture_comment(
110 tctx, "EAs not supported, creating: %s\n", fname);
111 io.in.eas.num_eas = 0;
112 status = smb2_create(tree, tmp_ctx, &io);
115 talloc_free(tmp_ctx);
116 NT_STATUS_NOT_OK_RETURN(status);
118 *handle = io.out.file.handle;
120 if (!dir) {
121 status = smb2_util_write(tree, *handle, buf, 0, sizeof(buf));
122 NT_STATUS_NOT_OK_RETURN(status);
125 /* make sure all the timestamps aren't the same, and are also
126 in different DST zones*/
127 setfile.generic.level = RAW_SFILEINFO_BASIC_INFORMATION;
128 setfile.generic.in.file.handle = *handle;
130 unix_to_nt_time(&setfile.basic_info.in.create_time, t + 9*30*24*60*60);
131 unix_to_nt_time(&setfile.basic_info.in.access_time, t + 6*30*24*60*60);
132 unix_to_nt_time(&setfile.basic_info.in.write_time, t + 3*30*24*60*60);
133 unix_to_nt_time(&setfile.basic_info.in.change_time, t + 1*30*24*60*60);
134 setfile.basic_info.in.attrib = FILE_ATTRIBUTE_NORMAL;
136 status = smb2_setinfo_file(tree, &setfile);
137 if (!NT_STATUS_IS_OK(status)) {
138 torture_comment(tctx, "Failed to setup file times - %s\n", nt_errstr(status));
139 return status;
142 /* make sure all the timestamps aren't the same */
143 fileinfo.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
144 fileinfo.generic.in.file.handle = *handle;
146 status = smb2_getinfo_file(tree, tree, &fileinfo);
147 if (!NT_STATUS_IS_OK(status)) {
148 torture_comment(tctx, "Failed to query file times - %s\n", nt_errstr(status));
149 return status;
153 #define CHECK_TIME(field) do {\
154 if (setfile.basic_info.in.field != fileinfo.all_info2.out.field) { \
155 torture_comment(tctx, "(%s) " #field " not setup correctly: %s(%llu) => %s(%llu)\n", \
156 __location__, \
157 nt_time_string(tree, setfile.basic_info.in.field), \
158 (unsigned long long)setfile.basic_info.in.field, \
159 nt_time_string(tree, fileinfo.basic_info.out.field), \
160 (unsigned long long)fileinfo.basic_info.out.field); \
161 status = NT_STATUS_INVALID_PARAMETER; \
163 } while (0)
165 CHECK_TIME(create_time);
166 CHECK_TIME(access_time);
167 CHECK_TIME(write_time);
168 CHECK_TIME(change_time);
170 return status;
174 create a complex file using the SMB2 protocol
176 NTSTATUS smb2_create_complex_file(struct torture_context *tctx,
177 struct smb2_tree *tree, const char *fname,
178 struct smb2_handle *handle)
180 return smb2_create_complex(tctx, tree, fname, handle, false);
184 create a complex dir using the SMB2 protocol
186 NTSTATUS smb2_create_complex_dir(struct torture_context *tctx,
187 struct smb2_tree *tree, const char *fname,
188 struct smb2_handle *handle)
190 return smb2_create_complex(tctx, tree, fname, handle, true);
194 show lots of information about a file
196 void torture_smb2_all_info(struct torture_context *tctx,
197 struct smb2_tree *tree, struct smb2_handle handle)
199 NTSTATUS status;
200 TALLOC_CTX *tmp_ctx = talloc_new(tree);
201 union smb_fileinfo io;
203 io.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
204 io.generic.in.file.handle = handle;
206 status = smb2_getinfo_file(tree, tmp_ctx, &io);
207 if (!NT_STATUS_IS_OK(status)) {
208 DEBUG(0,("getinfo failed - %s\n", nt_errstr(status)));
209 talloc_free(tmp_ctx);
210 return;
213 torture_comment(tctx, "all_info for '%s'\n", io.all_info2.out.fname.s);
214 torture_comment(tctx, "\tcreate_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.create_time));
215 torture_comment(tctx, "\taccess_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.access_time));
216 torture_comment(tctx, "\twrite_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.write_time));
217 torture_comment(tctx, "\tchange_time: %s\n", nt_time_string(tmp_ctx, io.all_info2.out.change_time));
218 torture_comment(tctx, "\tattrib: 0x%x\n", io.all_info2.out.attrib);
219 torture_comment(tctx, "\tunknown1: 0x%x\n", io.all_info2.out.unknown1);
220 torture_comment(tctx, "\talloc_size: %llu\n", (long long)io.all_info2.out.alloc_size);
221 torture_comment(tctx, "\tsize: %llu\n", (long long)io.all_info2.out.size);
222 torture_comment(tctx, "\tnlink: %u\n", io.all_info2.out.nlink);
223 torture_comment(tctx, "\tdelete_pending: %u\n", io.all_info2.out.delete_pending);
224 torture_comment(tctx, "\tdirectory: %u\n", io.all_info2.out.directory);
225 torture_comment(tctx, "\tfile_id: %llu\n", (long long)io.all_info2.out.file_id);
226 torture_comment(tctx, "\tea_size: %u\n", io.all_info2.out.ea_size);
227 torture_comment(tctx, "\taccess_mask: 0x%08x\n", io.all_info2.out.access_mask);
228 torture_comment(tctx, "\tposition: 0x%llx\n", (long long)io.all_info2.out.position);
229 torture_comment(tctx, "\tmode: 0x%llx\n", (long long)io.all_info2.out.mode);
231 /* short name, if any */
232 io.generic.level = RAW_FILEINFO_ALT_NAME_INFORMATION;
233 status = smb2_getinfo_file(tree, tmp_ctx, &io);
234 if (NT_STATUS_IS_OK(status)) {
235 torture_comment(tctx, "\tshort name: '%s'\n", io.alt_name_info.out.fname.s);
238 /* the EAs, if any */
239 io.generic.level = RAW_FILEINFO_SMB2_ALL_EAS;
240 status = smb2_getinfo_file(tree, tmp_ctx, &io);
241 if (NT_STATUS_IS_OK(status)) {
242 int i;
243 for (i=0;i<io.all_eas.out.num_eas;i++) {
244 torture_comment(tctx, "\tEA[%d] flags=%d len=%d '%s'\n", i,
245 io.all_eas.out.eas[i].flags,
246 (int)io.all_eas.out.eas[i].value.length,
247 io.all_eas.out.eas[i].name.s);
251 /* streams, if available */
252 io.generic.level = RAW_FILEINFO_STREAM_INFORMATION;
253 status = smb2_getinfo_file(tree, tmp_ctx, &io);
254 if (NT_STATUS_IS_OK(status)) {
255 int i;
256 for (i=0;i<io.stream_info.out.num_streams;i++) {
257 torture_comment(tctx, "\tstream %d:\n", i);
258 torture_comment(tctx, "\t\tsize %ld\n",
259 (long)io.stream_info.out.streams[i].size);
260 torture_comment(tctx, "\t\talloc size %ld\n",
261 (long)io.stream_info.out.streams[i].alloc_size);
262 torture_comment(tctx, "\t\tname %s\n", io.stream_info.out.streams[i].stream_name.s);
266 if (DEBUGLVL(1)) {
267 /* the security descriptor */
268 io.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
269 io.query_secdesc.in.secinfo_flags =
270 SECINFO_OWNER|SECINFO_GROUP|
271 SECINFO_DACL;
272 status = smb2_getinfo_file(tree, tmp_ctx, &io);
273 if (NT_STATUS_IS_OK(status)) {
274 NDR_PRINT_DEBUG(security_descriptor, io.query_secdesc.out.sd);
278 talloc_free(tmp_ctx);
282 get granted access of a file handle
284 NTSTATUS torture_smb2_get_allinfo_access(struct smb2_tree *tree,
285 struct smb2_handle handle,
286 uint32_t *granted_access)
288 NTSTATUS status;
289 TALLOC_CTX *tmp_ctx = talloc_new(tree);
290 union smb_fileinfo io;
292 io.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
293 io.generic.in.file.handle = handle;
295 status = smb2_getinfo_file(tree, tmp_ctx, &io);
296 if (!NT_STATUS_IS_OK(status)) {
297 DEBUG(0, ("getinfo failed - %s\n", nt_errstr(status)));
298 goto out;
301 *granted_access = io.all_info2.out.access_mask;
303 out:
304 talloc_free(tmp_ctx);
305 return status;
309 * open a smb2 tree connect
311 bool torture_smb2_tree_connect(struct torture_context *tctx,
312 struct smb2_session *session,
313 TALLOC_CTX *mem_ctx,
314 struct smb2_tree **_tree)
316 NTSTATUS status;
317 const char *host = torture_setting_string(tctx, "host", NULL);
318 const char *share = torture_setting_string(tctx, "share", NULL);
319 const char *unc;
320 struct smb2_tree *tree;
321 struct tevent_req *subreq;
322 uint32_t timeout_msec;
324 unc = talloc_asprintf(tctx, "\\\\%s\\%s", host, share);
325 torture_assert(tctx, unc != NULL, "talloc_asprintf");
327 tree = smb2_tree_init(session, mem_ctx, false);
328 torture_assert(tctx, tree != NULL, "smb2_tree_init");
330 timeout_msec = session->transport->options.request_timeout * 1000;
332 subreq = smb2cli_tcon_send(tree, tctx->ev,
333 session->transport->conn,
334 timeout_msec,
335 session->smbXcli,
336 tree->smbXcli,
337 0, /* flags */
338 unc);
339 torture_assert(tctx, subreq != NULL, "smb2cli_tcon_send");
341 torture_assert(tctx,
342 tevent_req_poll_ntstatus(subreq, tctx->ev, &status),
343 "tevent_req_poll_ntstatus");
345 status = smb2cli_tcon_recv(subreq);
346 TALLOC_FREE(subreq);
347 torture_assert_ntstatus_ok(tctx, status, "smb2cli_tcon_recv");
349 *_tree = tree;
351 return true;
355 * do a smb2 session setup (without a tree connect)
357 bool torture_smb2_session_setup(struct torture_context *tctx,
358 struct smb2_transport *transport,
359 uint64_t previous_session_id,
360 TALLOC_CTX *mem_ctx,
361 struct smb2_session **_session)
363 NTSTATUS status;
364 struct smb2_session *session;
366 session = smb2_session_init(transport,
367 lpcfg_gensec_settings(tctx, tctx->lp_ctx),
368 mem_ctx);
370 if (session == NULL) {
371 return false;
374 status = smb2_session_setup_spnego(session,
375 samba_cmdline_get_creds(),
376 previous_session_id);
377 if (!NT_STATUS_IS_OK(status)) {
378 torture_comment(tctx, "session setup failed: %s\n", nt_errstr(status));
379 talloc_free(session);
380 return false;
383 *_session = session;
385 return true;
389 open a smb2 connection
391 bool torture_smb2_connection_ext(struct torture_context *tctx,
392 uint64_t previous_session_id,
393 const struct smbcli_options *options,
394 struct smb2_tree **tree)
396 NTSTATUS status;
397 const char *host = torture_setting_string(tctx, "host", NULL);
398 const char *share = torture_setting_string(tctx, "share", NULL);
399 const char *p = torture_setting_string(tctx, "unclist", NULL);
400 TALLOC_CTX *mem_ctx = NULL;
401 bool ok;
403 if (p != NULL) {
404 char *host2 = NULL;
405 char *share2 = NULL;
407 mem_ctx = talloc_new(tctx);
408 if (mem_ctx == NULL) {
409 return false;
412 ok = torture_get_conn_index(tctx->conn_index++, mem_ctx, tctx,
413 &host2, &share2);
414 if (!ok) {
415 TALLOC_FREE(mem_ctx);
416 return false;
419 host = host2;
420 share = share2;
423 status = smb2_connect_ext(tctx,
424 host,
425 lpcfg_smb_ports(tctx->lp_ctx),
426 share,
427 lpcfg_resolve_context(tctx->lp_ctx),
428 samba_cmdline_get_creds(),
429 previous_session_id,
430 tree,
431 tctx->ev,
432 options,
433 lpcfg_socket_options(tctx->lp_ctx),
434 lpcfg_gensec_settings(tctx, tctx->lp_ctx)
436 if (!NT_STATUS_IS_OK(status)) {
437 torture_comment(tctx, "Failed to connect to SMB2 share \\\\%s\\%s - %s\n",
438 host, share, nt_errstr(status));
439 TALLOC_FREE(mem_ctx);
440 return false;
443 TALLOC_FREE(mem_ctx);
444 return true;
447 bool torture_smb2_connection(struct torture_context *tctx, struct smb2_tree **tree)
449 bool ret;
450 struct smbcli_options options;
452 lpcfg_smbcli_options(tctx->lp_ctx, &options);
454 ret = torture_smb2_connection_ext(tctx, 0, &options, tree);
456 return ret;
460 * SMB2 connect with share from soption
462 bool torture_smb2_con_sopt(struct torture_context *tctx,
463 const char *soption,
464 struct smb2_tree **tree)
466 struct smbcli_options options;
467 NTSTATUS status;
468 const char *host = torture_setting_string(tctx, "host", NULL);
469 const char *share = torture_setting_string(tctx, soption, NULL);
471 lpcfg_smbcli_options(tctx->lp_ctx, &options);
473 if (share == NULL) {
474 torture_comment(tctx, "No share for option %s\n", soption);
475 return false;
478 status = smb2_connect(tctx,
479 host,
480 lpcfg_smb_ports(tctx->lp_ctx),
481 share,
482 lpcfg_resolve_context(tctx->lp_ctx),
483 samba_cmdline_get_creds(),
484 tree,
485 tctx->ev,
486 &options,
487 lpcfg_socket_options(tctx->lp_ctx),
488 lpcfg_gensec_settings(tctx, tctx->lp_ctx)
490 if (!NT_STATUS_IS_OK(status)) {
491 torture_comment(tctx, "Failed to connect to SMB2 share \\\\%s\\%s - %s\n",
492 host, share, nt_errstr(status));
493 return false;
495 return true;
499 create and return a handle to a test file
500 with a specific access mask
502 NTSTATUS torture_smb2_testfile_access(struct smb2_tree *tree, const char *fname,
503 struct smb2_handle *handle,
504 uint32_t desired_access)
506 struct smb2_create io;
507 NTSTATUS status;
509 ZERO_STRUCT(io);
510 io.in.oplock_level = 0;
511 io.in.desired_access = desired_access;
512 io.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
513 io.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
514 io.in.share_access =
515 NTCREATEX_SHARE_ACCESS_DELETE|
516 NTCREATEX_SHARE_ACCESS_READ|
517 NTCREATEX_SHARE_ACCESS_WRITE;
518 io.in.create_options = 0;
519 io.in.fname = fname;
521 status = smb2_create(tree, tree, &io);
522 NT_STATUS_NOT_OK_RETURN(status);
524 *handle = io.out.file.handle;
526 return NT_STATUS_OK;
530 create and return a handle to a test file
532 NTSTATUS torture_smb2_testfile(struct smb2_tree *tree, const char *fname,
533 struct smb2_handle *handle)
535 return torture_smb2_testfile_access(tree, fname, handle,
536 SEC_RIGHTS_FILE_ALL);
540 create and return a handle to a test file
541 with a specific access mask
543 NTSTATUS torture_smb2_open(struct smb2_tree *tree,
544 const char *fname,
545 uint32_t desired_access,
546 struct smb2_handle *handle)
548 struct smb2_create io;
549 NTSTATUS status;
551 io = (struct smb2_create) {
552 .in.fname = fname,
553 .in.desired_access = desired_access,
554 .in.file_attributes = FILE_ATTRIBUTE_NORMAL,
555 .in.create_disposition = NTCREATEX_DISP_OPEN,
556 .in.share_access = NTCREATEX_SHARE_ACCESS_MASK,
559 status = smb2_create(tree, tree, &io);
560 if (!NT_STATUS_IS_OK(status)) {
561 return status;
564 *handle = io.out.file.handle;
566 return NT_STATUS_OK;
570 create and return a handle to a test directory
571 with specific desired access
573 NTSTATUS torture_smb2_testdir_access(struct smb2_tree *tree, const char *fname,
574 struct smb2_handle *handle,
575 uint32_t desired_access)
577 struct smb2_create io;
578 NTSTATUS status;
580 ZERO_STRUCT(io);
581 io.in.oplock_level = 0;
582 io.in.desired_access = desired_access;
583 io.in.file_attributes = FILE_ATTRIBUTE_DIRECTORY;
584 io.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
585 io.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE;
586 io.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
587 io.in.fname = fname;
589 status = smb2_create(tree, tree, &io);
590 NT_STATUS_NOT_OK_RETURN(status);
592 *handle = io.out.file.handle;
594 return NT_STATUS_OK;
598 create and return a handle to a test directory
600 NTSTATUS torture_smb2_testdir(struct smb2_tree *tree, const char *fname,
601 struct smb2_handle *handle)
603 return torture_smb2_testdir_access(tree, fname, handle,
604 SEC_RIGHTS_DIR_ALL);
608 create a simple file using the SMB2 protocol
610 NTSTATUS smb2_create_simple_file(struct torture_context *tctx,
611 struct smb2_tree *tree, const char *fname,
612 struct smb2_handle *handle)
614 char buf[7] = "abc";
615 NTSTATUS status;
617 smb2_util_unlink(tree, fname);
618 status = torture_smb2_testfile_access(tree,
619 fname, handle,
620 SEC_FLAG_MAXIMUM_ALLOWED);
621 NT_STATUS_NOT_OK_RETURN(status);
623 status = smb2_util_write(tree, *handle, buf, 0, sizeof(buf));
624 NT_STATUS_NOT_OK_RETURN(status);
626 return NT_STATUS_OK;
630 create a simple file using SMB2.
632 NTSTATUS torture_setup_simple_file(struct torture_context *tctx,
633 struct smb2_tree *tree, const char *fname)
635 struct smb2_handle handle;
636 NTSTATUS status = smb2_create_simple_file(tctx, tree, fname, &handle);
637 NT_STATUS_NOT_OK_RETURN(status);
638 return smb2_util_close(tree, handle);
642 create a complex file using SMB2, to make it easier to
643 find fields in SMB2 getinfo levels
645 NTSTATUS torture_setup_complex_file(struct torture_context *tctx,
646 struct smb2_tree *tree, const char *fname)
648 struct smb2_handle handle;
649 NTSTATUS status = smb2_create_complex_file(tctx, tree, fname, &handle);
650 NT_STATUS_NOT_OK_RETURN(status);
651 return smb2_util_close(tree, handle);
656 create a complex dir using SMB2, to make it easier to
657 find fields in SMB2 getinfo levels
659 NTSTATUS torture_setup_complex_dir(struct torture_context *tctx,
660 struct smb2_tree *tree, const char *fname)
662 struct smb2_handle handle;
663 NTSTATUS status = smb2_create_complex_dir(tctx, tree, fname, &handle);
664 NT_STATUS_NOT_OK_RETURN(status);
665 return smb2_util_close(tree, handle);
670 return a handle to the root of the share
672 NTSTATUS smb2_util_roothandle(struct smb2_tree *tree, struct smb2_handle *handle)
674 struct smb2_create io;
675 NTSTATUS status;
677 ZERO_STRUCT(io);
678 io.in.oplock_level = 0;
679 io.in.desired_access = SEC_STD_SYNCHRONIZE | SEC_DIR_READ_ATTRIBUTE | SEC_DIR_LIST;
680 io.in.file_attributes = 0;
681 io.in.create_disposition = NTCREATEX_DISP_OPEN;
682 io.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_DELETE;
683 io.in.create_options = NTCREATEX_OPTIONS_ASYNC_ALERT;
684 io.in.fname = "";
686 status = smb2_create(tree, tree, &io);
687 NT_STATUS_NOT_OK_RETURN(status);
689 *handle = io.out.file.handle;
691 return NT_STATUS_OK;
694 /* Comparable to torture_setup_dir, but for SMB2. */
695 bool smb2_util_setup_dir(struct torture_context *tctx, struct smb2_tree *tree,
696 const char *dname)
698 NTSTATUS status;
700 /* XXX: smb_raw_exit equivalent?
701 smb_raw_exit(cli->session); */
702 if (smb2_deltree(tree, dname) == -1) {
703 torture_result(tctx, TORTURE_ERROR, "Unable to deltree when setting up %s.\n", dname);
704 return false;
707 status = smb2_util_mkdir(tree, dname);
708 if (NT_STATUS_IS_ERR(status)) {
709 torture_result(tctx, TORTURE_ERROR, "Unable to mkdir when setting up %s - %s\n", dname,
710 nt_errstr(status));
711 return false;
714 return true;
717 #define CHECK_STATUS(status, correct) do { \
718 if (!NT_STATUS_EQUAL(status, correct)) { \
719 torture_result(tctx, TORTURE_FAIL, "(%s) Incorrect status %s - should be %s\n", \
720 __location__, nt_errstr(status), nt_errstr(correct)); \
721 ret = false; \
722 goto done; \
723 }} while (0)
726 * Helper function to verify a security descriptor, by querying
727 * and comparing against the passed in sd.
729 bool smb2_util_verify_sd(TALLOC_CTX *tctx, struct smb2_tree *tree,
730 struct smb2_handle handle, struct security_descriptor *sd)
732 NTSTATUS status;
733 bool ret = true;
734 union smb_fileinfo q = {};
736 q.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
737 q.query_secdesc.in.file.handle = handle;
738 q.query_secdesc.in.secinfo_flags =
739 SECINFO_OWNER |
740 SECINFO_GROUP |
741 SECINFO_DACL;
742 status = smb2_getinfo_file(tree, tctx, &q);
743 CHECK_STATUS(status, NT_STATUS_OK);
745 if (!security_acl_equal(
746 q.query_secdesc.out.sd->dacl, sd->dacl)) {
747 torture_warning(tctx, "%s: security descriptors don't match!\n",
748 __location__);
749 torture_warning(tctx, "got:\n");
750 NDR_PRINT_DEBUG(security_descriptor,
751 q.query_secdesc.out.sd);
752 torture_warning(tctx, "expected:\n");
753 NDR_PRINT_DEBUG(security_descriptor, sd);
754 ret = false;
757 done:
758 return ret;
762 * Helper function to verify attributes, by querying
763 * and comparing against the passed in attrib.
765 bool smb2_util_verify_attrib(TALLOC_CTX *tctx, struct smb2_tree *tree,
766 struct smb2_handle handle, uint32_t attrib)
768 NTSTATUS status;
769 bool ret = true;
770 union smb_fileinfo q = {};
772 q.standard.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
773 q.standard.in.file.handle = handle;
774 status = smb2_getinfo_file(tree, tctx, &q);
775 CHECK_STATUS(status, NT_STATUS_OK);
777 q.all_info2.out.attrib &= ~(FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_NONINDEXED);
779 if (q.all_info2.out.attrib != attrib) {
780 torture_warning(tctx, "%s: attributes don't match! "
781 "got %x, expected %x\n", __location__,
782 (uint32_t)q.standard.out.attrib,
783 (uint32_t)attrib);
784 ret = false;
787 done:
788 return ret;
792 uint32_t smb2_util_lease_state(const char *ls)
794 uint32_t val = 0;
795 int i;
797 for (i = 0; i < strlen(ls); i++) {
798 switch (ls[i]) {
799 case 'R':
800 val |= SMB2_LEASE_READ;
801 break;
802 case 'H':
803 val |= SMB2_LEASE_HANDLE;
804 break;
805 case 'W':
806 val |= SMB2_LEASE_WRITE;
807 break;
811 return val;
814 char *smb2_util_lease_state_string(TALLOC_CTX *mem_ctx, uint32_t ls)
816 return talloc_asprintf(mem_ctx, "0x%0x (%s%s%s)",
817 (unsigned)ls,
818 ls & SMB2_LEASE_READ ? "R": "",
819 ls & SMB2_LEASE_HANDLE ? "H": "",
820 ls & SMB2_LEASE_WRITE ? "W": "");
823 uint32_t smb2_util_share_access(const char *sharemode)
825 uint32_t val = NTCREATEX_SHARE_ACCESS_NONE; /* 0 */
826 int i;
828 for (i = 0; i < strlen(sharemode); i++) {
829 switch(sharemode[i]) {
830 case 'R':
831 val |= NTCREATEX_SHARE_ACCESS_READ;
832 break;
833 case 'W':
834 val |= NTCREATEX_SHARE_ACCESS_WRITE;
835 break;
836 case 'D':
837 val |= NTCREATEX_SHARE_ACCESS_DELETE;
838 break;
842 return val;
845 uint8_t smb2_util_oplock_level(const char *op)
847 uint8_t val = SMB2_OPLOCK_LEVEL_NONE;
848 int i;
850 for (i = 0; i < strlen(op); i++) {
851 switch (op[i]) {
852 case 's':
853 return SMB2_OPLOCK_LEVEL_II;
854 case 'x':
855 return SMB2_OPLOCK_LEVEL_EXCLUSIVE;
856 case 'b':
857 return SMB2_OPLOCK_LEVEL_BATCH;
858 default:
859 continue;
863 return val;
867 * Helper functions to fill a smb2_create struct for several
868 * open scenarios.
870 void smb2_generic_create_share(struct smb2_create *io, struct smb2_lease *ls,
871 bool dir, const char *name, uint32_t disposition,
872 uint32_t share_access,
873 uint8_t oplock, uint64_t leasekey,
874 uint32_t leasestate)
876 ZERO_STRUCT(*io);
877 io->in.security_flags = 0x00;
878 io->in.oplock_level = oplock;
879 io->in.impersonation_level = NTCREATEX_IMPERSONATION_IMPERSONATION;
880 io->in.create_flags = 0x00000000;
881 io->in.reserved = 0x00000000;
882 io->in.desired_access = SEC_RIGHTS_FILE_ALL;
883 io->in.file_attributes = FILE_ATTRIBUTE_NORMAL;
884 io->in.share_access = share_access;
885 io->in.create_disposition = disposition;
886 io->in.create_options = NTCREATEX_OPTIONS_SEQUENTIAL_ONLY |
887 NTCREATEX_OPTIONS_ASYNC_ALERT |
888 NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
889 0x00200000;
890 io->in.fname = name;
892 if (dir) {
893 io->in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
894 io->in.file_attributes = FILE_ATTRIBUTE_DIRECTORY;
895 io->in.create_disposition = NTCREATEX_DISP_CREATE;
898 if (ls) {
899 ZERO_STRUCTPN(ls);
900 ls->lease_key.data[0] = leasekey;
901 ls->lease_key.data[1] = ~leasekey;
902 ls->lease_state = leasestate;
903 io->in.lease_request = ls;
907 void smb2_generic_create(struct smb2_create *io, struct smb2_lease *ls,
908 bool dir, const char *name, uint32_t disposition,
909 uint8_t oplock, uint64_t leasekey,
910 uint32_t leasestate)
912 smb2_generic_create_share(io, ls, dir, name, disposition,
913 smb2_util_share_access("RWD"),
914 oplock,
915 leasekey, leasestate);
918 void smb2_lease_create_share(struct smb2_create *io, struct smb2_lease *ls,
919 bool dir, const char *name, uint32_t share_access,
920 uint64_t leasekey, uint32_t leasestate)
922 smb2_generic_create_share(io, ls, dir, name, NTCREATEX_DISP_OPEN_IF,
923 share_access, SMB2_OPLOCK_LEVEL_LEASE,
924 leasekey, leasestate);
927 void smb2_lease_create(struct smb2_create *io, struct smb2_lease *ls,
928 bool dir, const char *name, uint64_t leasekey,
929 uint32_t leasestate)
931 smb2_lease_create_share(io, ls, dir, name,
932 smb2_util_share_access("RWD"),
933 leasekey, leasestate);
936 void smb2_lease_v2_create_share(struct smb2_create *io,
937 struct smb2_lease *ls,
938 bool dir,
939 const char *name,
940 uint32_t share_access,
941 uint64_t leasekey,
942 const uint64_t *parentleasekey,
943 uint32_t leasestate,
944 uint16_t lease_epoch)
946 smb2_generic_create_share(io, NULL, dir, name, NTCREATEX_DISP_OPEN_IF,
947 share_access, SMB2_OPLOCK_LEVEL_LEASE, 0, 0);
949 if (ls) {
950 ZERO_STRUCT(*ls);
951 ls->lease_key.data[0] = leasekey;
952 ls->lease_key.data[1] = ~leasekey;
953 ls->lease_state = leasestate;
954 if (parentleasekey != NULL) {
955 ls->lease_flags |= SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET;
956 ls->parent_lease_key.data[0] = *parentleasekey;
957 ls->parent_lease_key.data[1] = ~(*parentleasekey);
959 ls->lease_epoch = lease_epoch;
960 io->in.lease_request_v2 = ls;
964 void smb2_lease_v2_create(struct smb2_create *io,
965 struct smb2_lease *ls,
966 bool dir,
967 const char *name,
968 uint64_t leasekey,
969 const uint64_t *parentleasekey,
970 uint32_t leasestate,
971 uint16_t lease_epoch)
973 smb2_lease_v2_create_share(io, ls, dir, name,
974 smb2_util_share_access("RWD"),
975 leasekey, parentleasekey,
976 leasestate, lease_epoch);
980 void smb2_oplock_create_share(struct smb2_create *io, const char *name,
981 uint32_t share_access, uint8_t oplock)
983 smb2_generic_create_share(io, NULL, false, name, NTCREATEX_DISP_OPEN_IF,
984 share_access, oplock, 0, 0);
986 void smb2_oplock_create(struct smb2_create *io, const char *name, uint8_t oplock)
988 smb2_oplock_create_share(io, name, smb2_util_share_access("RWD"),
989 oplock);
993 a wrapper around smblsa_sid_check_privilege, that tries to take
994 account of the fact that the lsa privileges calls don't expand
995 group memberships, using an explicit check for administrator. There
996 must be a better way ...
998 NTSTATUS torture_smb2_check_privilege(struct smb2_tree *tree,
999 const char *sid_str,
1000 const char *privilege)
1002 struct dom_sid *sid = NULL;
1003 TALLOC_CTX *tmp_ctx = NULL;
1004 uint32_t rid;
1005 NTSTATUS status;
1007 tmp_ctx = talloc_new(tree);
1008 if (tmp_ctx == NULL) {
1009 return NT_STATUS_NO_MEMORY;
1012 sid = dom_sid_parse_talloc(tmp_ctx, sid_str);
1013 if (sid == NULL) {
1014 talloc_free(tmp_ctx);
1015 return NT_STATUS_INVALID_SID;
1018 status = dom_sid_split_rid(tmp_ctx, sid, NULL, &rid);
1019 if (!NT_STATUS_IS_OK(status)) {
1020 TALLOC_FREE(tmp_ctx);
1021 return status;
1024 if (rid == DOMAIN_RID_ADMINISTRATOR) {
1025 /* assume the administrator has them all */
1026 TALLOC_FREE(tmp_ctx);
1027 return NT_STATUS_OK;
1030 talloc_free(tmp_ctx);
1032 return smb2lsa_sid_check_privilege(tree, sid_str, privilege);