/* write to a file on SMB2 */ NTSTATUS smb2_util_write(struct smb2_tree *tree, struct smb2_handle handle, const void *buf, off_t offset, size_t size) { struct smb2_write w; ZERO_STRUCT(w); w.in.file.handle = handle; w.in.offset = offset; w.in.data = data_blob_const(buf, size); return smb2_write(tree, &w); }
/* test writing */ static NTSTATUS torture_smb2_write(struct smb2_tree *tree, struct smb2_handle handle) { struct smb2_write w; struct smb2_read r; struct smb2_flush f; NTSTATUS status; DATA_BLOB data; int i; if (lp_parm_bool(-1, "torture", "dangerous", False)) { data = data_blob_talloc(tree, NULL, 160000); } else if (lp_parm_bool(-1, "torture", "samba4", False)) { data = data_blob_talloc(tree, NULL, UINT16_MAX); } else { data = data_blob_talloc(tree, NULL, 120000); } for (i=0;i<data.length;i++) { data.data[i] = i; } ZERO_STRUCT(w); w.in.file.handle = handle; w.in.offset = 0; w.in.data = data; status = smb2_write(tree, &w); if (!NT_STATUS_IS_OK(status)) { printf("write failed - %s\n", nt_errstr(status)); return status; } torture_smb2_all_info(tree, handle); status = smb2_write(tree, &w); if (!NT_STATUS_IS_OK(status)) { printf("write failed - %s\n", nt_errstr(status)); return status; } torture_smb2_all_info(tree, handle); ZERO_STRUCT(f); f.in.file.handle = handle; status = smb2_flush(tree, &f); if (!NT_STATUS_IS_OK(status)) { printf("flush failed - %s\n", nt_errstr(status)); return status; } ZERO_STRUCT(r); r.in.file.handle = handle; r.in.length = data.length; r.in.offset = 0; status = smb2_read(tree, tree, &r); if (!NT_STATUS_IS_OK(status)) { printf("read failed - %s\n", nt_errstr(status)); return status; } if (data.length != r.out.data.length || memcmp(data.data, r.out.data.data, data.length) != 0) { printf("read data mismatch\n"); return NT_STATUS_NET_WRITE_FAULT; } return status; }
static bool test_lease_multibreak(struct torture_context *tctx, struct smb2_tree *tree) { TALLOC_CTX *mem_ctx = talloc_new(tctx); struct smb2_create io; struct smb2_lease ls; struct smb2_handle h, h2, h3; struct smb2_write w; NTSTATUS status; const char *fname = "lease.dat"; bool ret = true; uint32_t caps; caps = smb2cli_conn_server_capabilities(tree->session->transport->conn); if (!(caps & SMB2_CAP_LEASING)) { torture_skip(tctx, "leases are not supported"); } tree->session->transport->lease.handler = torture_lease_handler; tree->session->transport->lease.private_data = tree; tree->session->transport->oplock.handler = torture_oplock_handler; tree->session->transport->oplock.private_data = tree; smb2_util_unlink(tree, fname); ZERO_STRUCT(break_info); /* Grab lease, upgrade to RHW .. */ smb2_lease_create(&io, &ls, false, fname, LEASE1, smb2_util_lease_state("RH")); status = smb2_create(tree, mem_ctx, &io); CHECK_STATUS(status, NT_STATUS_OK); h = io.out.file.handle; CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE); CHECK_LEASE(&io, "RH", true, LEASE1); smb2_lease_create(&io, &ls, false, fname, LEASE1, smb2_util_lease_state("RHW")); status = smb2_create(tree, mem_ctx, &io); CHECK_STATUS(status, NT_STATUS_OK); h2 = io.out.file.handle; CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE); CHECK_LEASE(&io, "RHW", true, LEASE1); /* Contend with LEASE2. */ smb2_lease_create(&io, &ls, false, fname, LEASE2, smb2_util_lease_state("RHW")); status = smb2_create(tree, mem_ctx, &io); CHECK_STATUS(status, NT_STATUS_OK); h3 = io.out.file.handle; CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE); CHECK_LEASE(&io, "RH", true, LEASE2); /* Verify that we were only sent one break. */ CHECK_BREAK_INFO("RHW", "RH", LEASE1); /* Drop LEASE1 / LEASE2 */ status = smb2_util_close(tree, h); CHECK_STATUS(status, NT_STATUS_OK); status = smb2_util_close(tree, h2); CHECK_STATUS(status, NT_STATUS_OK); status = smb2_util_close(tree, h3); CHECK_STATUS(status, NT_STATUS_OK); ZERO_STRUCT(break_info); /* Grab an R lease. */ smb2_lease_create(&io, &ls, false, fname, LEASE1, smb2_util_lease_state("R")); status = smb2_create(tree, mem_ctx, &io); CHECK_STATUS(status, NT_STATUS_OK); h = io.out.file.handle; CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE); CHECK_LEASE(&io, "R", true, LEASE1); /* Grab a level-II oplock. */ smb2_oplock_create(&io, fname, smb2_util_oplock_level("s")); status = smb2_create(tree, mem_ctx, &io); CHECK_STATUS(status, NT_STATUS_OK); h2 = io.out.file.handle; CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_ARCHIVE); CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("s")); break_info.held_oplock_level = io.out.oplock_level; /* Verify no breaks. */ CHECK_VAL(break_info.count, 0); CHECK_VAL(break_info.failures, 0); /* Open for truncate, force a break. */ smb2_generic_create(&io, NULL, false, fname, NTCREATEX_DISP_OVERWRITE_IF, smb2_util_oplock_level(""), 0, 0); status = smb2_create(tree, mem_ctx, &io); CHECK_STATUS(status, NT_STATUS_OK); h3 = io.out.file.handle; CHECK_CREATED(&io, TRUNCATED, FILE_ATTRIBUTE_ARCHIVE); CHECK_VAL(io.out.oplock_level, smb2_util_oplock_level("")); break_info.held_oplock_level = io.out.oplock_level; /* Sleep, use a write to clear the recv queue. */ smb_msleep(250); ZERO_STRUCT(w); w.in.file.handle = h3; w.in.offset = 0; w.in.data = data_blob_talloc(mem_ctx, NULL, 4096); memset(w.in.data.data, 'o', w.in.data.length); status = smb2_write(tree, &w); CHECK_STATUS(status, NT_STATUS_OK); /* Verify one oplock break, one lease break. */ CHECK_VAL(break_info.oplock_count, 1); CHECK_VAL(break_info.oplock_failures, 0); CHECK_VAL(break_info.oplock_level, smb2_util_oplock_level("")); CHECK_BREAK_INFO("R", "", LEASE1); done: smb2_util_close(tree, h); smb2_util_close(tree, h2); smb2_util_close(tree, h3); smb2_util_unlink(tree, fname); talloc_free(mem_ctx); return ret; }
/* test writing */ static NTSTATUS torture_smb2_write(TALLOC_CTX *mem_ctx, struct smb2_tree *tree, struct smb2_handle handle) { struct smb2_write w; struct smb2_read r; NTSTATUS status; int i, len; int max = 80000000; int min = 1; while (max > min) { TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx); len = 1+(min+max)/2; ZERO_STRUCT(w); w.in.file.handle = handle; w.in.offset = 0; w.in.data = data_blob_talloc(tmp_ctx, NULL, len); for (i=0;i<len;i++) { w.in.data.data[i] = i % 256; } printf("trying to write %d bytes (min=%d max=%d)\n", len, min, max); status = smb2_write(tree, &w); if (!NT_STATUS_IS_OK(status)) { printf("write failed - %s\n", nt_errstr(status)); max = len-1; status = smb2_util_close(tree, handle); if (!NT_STATUS_IS_OK(status)) { /* vista bug */ printf("coping with server disconnect\n"); talloc_free(tree); if (!torture_smb2_connection(mem_ctx, &tree)) { printf("failed to reconnect\n"); return NT_STATUS_NET_WRITE_FAULT; } } handle = torture_smb2_create(tree, FNAME); continue; } else { min = len; } ZERO_STRUCT(r); r.in.file.handle = handle; r.in.length = len; r.in.offset = 0; printf("reading %d bytes\n", len); status = smb2_read(tree, tmp_ctx, &r); if (!NT_STATUS_IS_OK(status)) { printf("read failed - %s\n", nt_errstr(status)); } else if (w.in.data.length != r.out.data.length || memcmp(w.in.data.data, r.out.data.data, len) != 0) { printf("read data mismatch\n"); } talloc_free(tmp_ctx); } printf("converged: len=%d\n", max); smb2_util_close(tree, handle); smb2_util_unlink(tree, FNAME); return NT_STATUS_OK; }
static bool test_lease_v2_request(struct torture_context *tctx, struct smb2_tree *tree) { TALLOC_CTX *mem_ctx = talloc_new(tctx); struct smb2_create io; struct smb2_lease ls; struct smb2_handle h1, h2, h3, h4, h5; struct smb2_write w; NTSTATUS status; const char *fname = "lease.dat"; const char *dname = "lease.dir"; const char *dnamefname = "lease.dir\\lease.dat"; const char *dnamefname2 = "lease.dir\\lease2.dat"; bool ret = true; smb2_util_unlink(tree, fname); smb2_deltree(tree, dname); tree->session->transport->lease.handler = torture_lease_handler; tree->session->transport->lease.private_data = tree; tree->session->transport->oplock.handler = torture_oplock_handler; tree->session->transport->oplock.private_data = tree; ZERO_STRUCT(break_info); ZERO_STRUCT(io); smb2_lease_v2_create_share(&io, &ls, false, fname, smb2_util_share_access("RWD"), LEASE1, NULL, smb2_util_lease_state("RHW"), 0); status = smb2_create(tree, mem_ctx, &io); CHECK_STATUS(status, NT_STATUS_OK); h1 = io.out.file.handle; CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE); CHECK_LEASE_V2(&io, "RHW", true, LEASE1, 0); ZERO_STRUCT(io); smb2_lease_v2_create_share(&io, &ls, true, dname, smb2_util_share_access("RWD"), LEASE2, NULL, smb2_util_lease_state("RHW"), 0); status = smb2_create(tree, mem_ctx, &io); CHECK_STATUS(status, NT_STATUS_OK); h2 = io.out.file.handle; CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_DIRECTORY); CHECK_LEASE_V2(&io, "RH", true, LEASE2, 0); ZERO_STRUCT(io); smb2_lease_v2_create_share(&io, &ls, false, dnamefname, smb2_util_share_access("RWD"), LEASE3, &LEASE2, smb2_util_lease_state("RHW"), 0); status = smb2_create(tree, mem_ctx, &io); CHECK_STATUS(status, NT_STATUS_OK); h3 = io.out.file.handle; CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE); CHECK_LEASE_V2(&io, "RHW", true, LEASE3, SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET); torture_wait_for_lease_break(tctx); CHECK_VAL(break_info.count, 0); CHECK_VAL(break_info.failures, 0); ZERO_STRUCT(io); smb2_lease_v2_create_share(&io, &ls, false, dnamefname2, smb2_util_share_access("RWD"), LEASE4, NULL, smb2_util_lease_state("RHW"), 0); status = smb2_create(tree, mem_ctx, &io); CHECK_STATUS(status, NT_STATUS_OK); h4 = io.out.file.handle; CHECK_CREATED(&io, CREATED, FILE_ATTRIBUTE_ARCHIVE); CHECK_LEASE_V2(&io, "RHW", true, LEASE4, 0); torture_wait_for_lease_break(tctx); torture_wait_for_lease_break(tctx); CHECK_BREAK_INFO("RH", "", LEASE2); torture_wait_for_lease_break(tctx); ZERO_STRUCT(break_info); ZERO_STRUCT(io); smb2_lease_v2_create_share(&io, &ls, true, dname, smb2_util_share_access("RWD"), LEASE2, NULL, smb2_util_lease_state("RHW"), 0); io.in.create_disposition = NTCREATEX_DISP_OPEN; status = smb2_create(tree, mem_ctx, &io); CHECK_STATUS(status, NT_STATUS_OK); h5 = io.out.file.handle; CHECK_CREATED(&io, EXISTED, FILE_ATTRIBUTE_DIRECTORY); CHECK_LEASE_V2(&io, "RH", true, LEASE2, 0); smb2_util_close(tree, h5); ZERO_STRUCT(w); w.in.file.handle = h4; w.in.offset = 0; w.in.data = data_blob_talloc(mem_ctx, NULL, 4096); memset(w.in.data.data, 'o', w.in.data.length); status = smb2_write(tree, &w); CHECK_STATUS(status, NT_STATUS_OK); smb_msleep(2000); torture_wait_for_lease_break(tctx); CHECK_VAL(break_info.count, 0); CHECK_VAL(break_info.failures, 0); smb2_util_close(tree, h4); torture_wait_for_lease_break(tctx); torture_wait_for_lease_break(tctx); CHECK_BREAK_INFO("RH", "", LEASE2); torture_wait_for_lease_break(tctx); done: smb2_util_close(tree, h1); smb2_util_close(tree, h2); smb2_util_close(tree, h3); smb2_util_close(tree, h4); smb2_util_close(tree, h5); smb2_util_unlink(tree, fname); smb2_deltree(tree, dname); talloc_free(mem_ctx); return ret; }