--- /dev/null
+/*
+ Unix SMB/CIFS implementation.
+
+ test suite for SMB2 resilient opens
+
+ Copyright (C) Stefan Metzmacher 2008
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "includes.h"
+#include "libcli/smb2/smb2.h"
+#include "libcli/smb2/smb2_calls.h"
+#include "torture/torture.h"
+#include "torture/smb2/proto.h"
+
+#define CHECK_VAL(v, correct) do { \
+ if ((v) != (correct)) { \
+ torture_result(tctx, TORTURE_FAIL, "(%s): wrong value for %s got 0x%x - should be 0x%x\n", \
+ __location__, #v, (int)v, (int)correct); \
+ ret = false; \
+ }} while (0)
+
+#define CHECK_STATUS(status, correct) do { \
+ if (!NT_STATUS_EQUAL(status, correct)) { \
+ torture_result(tctx, TORTURE_FAIL, __location__": Incorrect status %s - should be %s", \
+ nt_errstr(status), nt_errstr(correct)); \
+ ret = false; \
+ goto done; \
+ }} while (0)
+
+#define CHECK_CREATED(__io, __created, __attribute) \
+ do { \
+ CHECK_VAL((__io)->out.create_action, NTCREATEX_ACTION_ ## __created); \
+ CHECK_VAL((__io)->out.alloc_size, 0); \
+ CHECK_VAL((__io)->out.size, 0); \
+ CHECK_VAL((__io)->out.file_attr, (__attribute)); \
+ CHECK_VAL((__io)->out.reserved2, 0); \
+ } while(0)
+
+
+static inline uint32_t map_lease(const char *ls)
+{
+ uint32_t val = 0;
+ int i;
+
+ for (i = 0; i < strlen(ls); i++) {
+ switch (ls[i]) {
+ case 'R':
+ val |= SMB2_LEASE_READ;
+ break;
+ case 'H':
+ val |= SMB2_LEASE_HANDLE;
+ break;
+ case 'W':
+ val |= SMB2_LEASE_WRITE;
+ break;
+ }
+ }
+
+ return val;
+}
+
+static inline uint32_t map_sharemode(const char *sharemode)
+{
+ uint32_t val = NTCREATEX_SHARE_ACCESS_NONE; /* 0 */
+ int i;
+
+ for (i = 0; i < strlen(sharemode); i++) {
+ switch(sharemode[i]) {
+ case 'R':
+ val |= NTCREATEX_SHARE_ACCESS_READ;
+ break;
+ case 'W':
+ val |= NTCREATEX_SHARE_ACCESS_WRITE;
+ break;
+ case 'D':
+ val |= NTCREATEX_SHARE_ACCESS_DELETE;
+ break;
+ }
+ }
+
+ return val;
+}
+
+/**
+ * basic durable_open test.
+ * durable state should only be granted when requested
+ * along with a batch oplock or a handle lease.
+ *
+ * This test tests durable open with all possible oplock types.
+ */
+
+bool test_resilient_open_open1(struct torture_context *tctx,
+ struct smb2_tree *tree)
+{
+ TALLOC_CTX *mem_ctx = talloc_new(tctx);
+ struct smb2_create cr;
+ struct smb2_ioctl io;
+ char fname[256];
+ bool ret = true;
+ int i;
+ NTSTATUS status;
+ struct smb2_handle _h;
+ struct smb2_handle *h = NULL;
+ uint8_t resiliency[8];
+ struct smb2_lease ls;
+
+ /* Choose a random name in case the state is left a little funky. */
+ snprintf(fname, 256, "resilient_open_open1_%s.dat",
+ generate_random_str(tctx, 8));
+
+ smb2_util_unlink(tree, fname);
+
+ ZERO_STRUCT(ls);
+ memset(&ls.lease_key, 0xef, sizeof(ls.lease_key));
+ ls.lease_state = SMB2_LEASE_READ|SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE;
+
+ ZERO_STRUCT(cr);
+ cr.in.security_flags = 0x00;
+ cr.in.impersonation_level = NTCREATEX_IMPERSONATION_IMPERSONATION;
+ cr.in.create_flags = 0x00000000;
+ cr.in.reserved = 0x00000000;
+ cr.in.desired_access = SEC_RIGHTS_FILE_ALL;
+ cr.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+ cr.in.create_disposition = NTCREATEX_DISP_CREATE;
+ cr.in.create_options = NTCREATEX_OPTIONS_SEQUENTIAL_ONLY |
+ NTCREATEX_OPTIONS_ASYNC_ALERT |
+ NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
+ 0x00200000 |
+ NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
+ cr.in.durable_open = true;
+ cr.in.fname = fname;
+ cr.in.share_access = map_sharemode("RWD");
+ cr.in.oplock_level = SMB2_OPLOCK_LEVEL_LEASE;
+ cr.in.lease_request = &ls;
+
+ status = smb2_create(tree, mem_ctx, &cr);
+ CHECK_STATUS(status, NT_STATUS_OK);
+ _h = cr.out.file.handle;
+ h = &_h;
+ CHECK_CREATED(&cr, CREATED, FILE_ATTRIBUTE_ARCHIVE);
+ CHECK_VAL(cr.out.durable_open, true);
+ CHECK_VAL(cr.out.oplock_level, SMB2_OPLOCK_LEVEL_LEASE);
+
+ SIVAL(resiliency, 0, 60*1000);
+ SIVAL(resiliency, 4, 0);
+
+ ZERO_STRUCT(io);
+ io.level = RAW_IOCTL_SMB2;
+ io.in.file.handle = *h;
+ io.in.function = FSCTL_LMR_REQ_RESILIENCY;
+ io.in.max_response_size = 0;
+ io.in.flags = SMB2_IOCTL_FLAG_IS_FSCTL;
+ io.in.in = data_blob_const(resiliency, ARRAY_SIZE(resiliency));
+ io.in.out = data_blob_const(resiliency, ARRAY_SIZE(resiliency));
+
+ status = smb2_ioctl(tree, mem_ctx, &io);
+ CHECK_STATUS(status, NT_STATUS_OK);
+
+done:
+ if (h != NULL) {
+ smb2_util_close(tree, *h);
+ }
+ talloc_free(mem_ctx);
+
+ return ret;
+}
+
+struct torture_suite *torture_smb2_resilient_open_init(void)
+{
+ struct torture_suite *suite =
+ torture_suite_create(talloc_autofree_context(), "resilient-open");
+
+ torture_suite_add_1smb2_test(suite, "open1", test_resilient_open_open1);
+
+ suite->description = talloc_strdup(suite, "SMB2-RESILIENT-OPEN tests");
+
+ return suite;
+}