Correct "succeded" typos.
[nivanova/samba-autobuild/.git] / source3 / smbd / smb2_create.c
index 0c27a010587749a9d30af295f98ca88b5485eaa7..cba0781430ecb20c7285e36fe36f2e8e5d89d93c 100644 (file)
@@ -25,6 +25,7 @@
 #include "smbd/globals.h"
 #include "../libcli/smb/smb_common.h"
 #include "../librpc/gen_ndr/ndr_security.h"
+#include "../librpc/gen_ndr/ndr_smb2_lease_struct.h"
 #include "../lib/util/tevent_ntstatus.h"
 #include "messages.h"
 
@@ -40,9 +41,7 @@ int map_smb2_oplock_levels_to_samba(uint8_t in_oplock_level)
        case SMB2_OPLOCK_LEVEL_BATCH:
                return BATCH_OPLOCK;
        case SMB2_OPLOCK_LEVEL_LEASE:
-               DEBUG(2,("map_smb2_oplock_levels_to_samba: "
-                       "LEASE_OPLOCK_REQUESTED\n"));
-               return NO_OPLOCK;
+               return LEASE_OPLOCK;
        default:
                DEBUG(2,("map_smb2_oplock_levels_to_samba: "
                        "unknown level %u\n",
@@ -59,6 +58,8 @@ static uint8_t map_samba_oplock_levels_to_smb2(int oplock_type)
                return SMB2_OPLOCK_LEVEL_EXCLUSIVE;
        } else if (oplock_type == LEVEL_II_OPLOCK) {
                return SMB2_OPLOCK_LEVEL_II;
+       } else if (oplock_type == LEASE_OPLOCK) {
+               return SMB2_OPLOCK_LEVEL_LEASE;
        } else {
                return SMB2_OPLOCK_LEVEL_NONE;
        }
@@ -241,13 +242,7 @@ NTSTATUS smbd_smb2_request_process_create(struct smbd_smb2_request *smb2req)
        }
        tevent_req_set_callback(tsubreq, smbd_smb2_request_create_done, smb2req);
 
-       /*
-        * For now we keep the logic that we do not send STATUS_PENDING
-        * for sharing violations, so we just wait 2 seconds.
-        *
-        * TODO: we need more tests for this.
-        */
-       return smbd_smb2_request_pending_queue(smb2req, tsubreq, 2000000);
+       return smbd_smb2_request_pending_queue(smb2req, tsubreq, 500);
 }
 
 static uint64_t get_mid_from_smb2req(struct smbd_smb2_request *smb2req)
@@ -297,7 +292,7 @@ static void smbd_smb2_request_create_done(struct tevent_req *tsubreq)
        if (!NT_STATUS_IS_OK(status)) {
                error = smbd_smb2_request_error(smb2req, status);
                if (!NT_STATUS_IS_OK(error)) {
-                       smbd_server_connection_terminate(smb2req->sconn,
+                       smbd_server_connection_terminate(smb2req->xconn,
                                                         nt_errstr(error));
                        return;
                }
@@ -308,7 +303,7 @@ static void smbd_smb2_request_create_done(struct tevent_req *tsubreq)
        if (!NT_STATUS_IS_OK(status)) {
                error = smbd_smb2_request_error(smb2req, status);
                if (!NT_STATUS_IS_OK(error)) {
-                       smbd_server_connection_terminate(smb2req->sconn,
+                       smbd_server_connection_terminate(smb2req->xconn,
                                                         nt_errstr(error));
                        return;
                }
@@ -323,7 +318,7 @@ static void smbd_smb2_request_create_done(struct tevent_req *tsubreq)
        if (outbody.data == NULL) {
                error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
                if (!NT_STATUS_IS_OK(error)) {
-                       smbd_server_connection_terminate(smb2req->sconn,
+                       smbd_server_connection_terminate(smb2req->xconn,
                                                         nt_errstr(error));
                        return;
                }
@@ -368,12 +363,70 @@ static void smbd_smb2_request_create_done(struct tevent_req *tsubreq)
 
        error = smbd_smb2_request_done(smb2req, outbody, &outdyn);
        if (!NT_STATUS_IS_OK(error)) {
-               smbd_server_connection_terminate(smb2req->sconn,
+               smbd_server_connection_terminate(smb2req->xconn,
                                                 nt_errstr(error));
                return;
        }
 }
 
+static bool smb2_lease_key_valid(const struct smb2_lease_key *key)
+{
+       return ((key->data[0] != 0) || (key->data[1] != 0));
+}
+
+static NTSTATUS smbd_smb2_create_durable_lease_check(struct smb_request *smb1req,
+       const char *requested_filename, const struct files_struct *fsp,
+       const struct smb2_lease *lease_ptr)
+{
+       struct smb_filename *smb_fname = NULL;
+       uint32_t ucf_flags;
+       NTSTATUS status;
+
+       if (lease_ptr == NULL) {
+               if (fsp->oplock_type != LEASE_OPLOCK) {
+                       return NT_STATUS_OK;
+               }
+               DEBUG(10, ("Reopened file has lease, but no lease "
+                          "requested\n"));
+               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+       }
+
+       if (fsp->oplock_type != LEASE_OPLOCK) {
+               DEBUG(10, ("Lease requested, but reopened file has no "
+                          "lease\n"));
+               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+       }
+
+       if (!smb2_lease_key_equal(&lease_ptr->lease_key,
+                                 &fsp->lease->lease.lease_key)) {
+               DEBUG(10, ("Different lease key requested than found "
+                          "in reopened file\n"));
+               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+       }
+
+       ucf_flags = filename_create_ucf_flags(smb1req, FILE_OPEN);
+       status = filename_convert(talloc_tos(), fsp->conn, false,
+                                 requested_filename, ucf_flags,
+                                 NULL, &smb_fname);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(10, ("filename_convert returned %s\n",
+                          nt_errstr(status)));
+               return status;
+       }
+
+       if (!strequal(fsp->fsp_name->base_name, smb_fname->base_name)) {
+               DEBUG(10, ("Lease requested for file %s, reopened file "
+                          "is named %s\n", smb_fname->base_name,
+                          fsp->fsp_name->base_name));
+               TALLOC_FREE(smb_fname);
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       TALLOC_FREE(smb_fname);
+
+       return NT_STATUS_OK;
+}
+
 struct smbd_smb2_create_state {
        struct smbd_smb2_request *smb2req;
        struct smb_request *smb1req;
@@ -394,7 +447,7 @@ struct smbd_smb2_create_state {
        uint32_t out_file_attributes;
        uint64_t out_file_id_persistent;
        uint64_t out_file_id_volatile;
-       struct smb2_create_blobs out_context_blobs;
+       struct smb2_create_blobs *out_context_blobs;
 };
 
 static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
@@ -416,15 +469,13 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
        struct smb_request *smb1req = NULL;
        files_struct *result = NULL;
        int info;
-       struct smb2_create_blobs out_context_blobs;
        int requested_oplock_level;
        struct smb2_create_blob *dhnc = NULL;
        struct smb2_create_blob *dh2c = NULL;
        struct smb2_create_blob *dhnq = NULL;
        struct smb2_create_blob *dh2q = NULL;
-       struct smbXsrv_open *op = NULL;
-
-       ZERO_STRUCT(out_context_blobs);
+       struct smb2_create_blob *rqls = NULL;
+       bool replay_operation = false;
 
        if(lp_fake_oplocks(SNUM(smb2req->tcon->compat))) {
                requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
@@ -456,10 +507,16 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                state = tevent_req_data(req,
                                struct smbd_smb2_create_state);
                smb1req = state->smb1req;
+               TALLOC_FREE(state->out_context_blobs);
                DEBUG(10,("smbd_smb2_create_send: reentrant for file %s\n",
                        in_name ));
        }
 
+       state->out_context_blobs = talloc_zero(state, struct smb2_create_blobs);
+       if (tevent_req_nomem(state->out_context_blobs, req)) {
+               return tevent_req_post(req, ev);
+       }
+
        dhnq = smb2_create_blob_find(&in_context_blobs,
                                     SMB2_CREATE_TAG_DHNQ);
        dhnc = smb2_create_blob_find(&in_context_blobs,
@@ -468,6 +525,10 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                                     SMB2_CREATE_TAG_DH2Q);
        dh2c = smb2_create_blob_find(&in_context_blobs,
                                     SMB2_CREATE_TAG_DH2C);
+       if (smb2req->xconn->smb2.server.capabilities & SMB2_CAP_LEASING) {
+               rqls = smb2_create_blob_find(&in_context_blobs,
+                                            SMB2_CREATE_TAG_RQLS);
+       }
 
        if ((dhnc && dh2c) || (dhnc && dh2q) || (dh2c && dhnq) ||
            (dh2q && dh2c))
@@ -495,10 +556,6 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                 * (Note that the cases of an additional dh2q or dh2c blob
                 *  which require a different error code, have been treated
                 *  above.)
-                *
-                * TODO:
-                * This is only true for the oplock case:
-                * For leases, lease request is required additionally.
                 */
 
                if (dhnq) {
@@ -507,6 +564,10 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                        num_blobs_allowed = 1;
                }
 
+               if (rqls != NULL) {
+                       num_blobs_allowed += 1;
+               }
+
                if (in_context_blobs.num_blobs != num_blobs_allowed) {
                        tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
                        return tevent_req_post(req, ev);
@@ -531,14 +592,14 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                 * (Note that the cases of an additional dhnq, dhnc or dh2q
                 *  blob which require a different error code, have been
                 *  treated above.)
-                *
-                * TODO:
-                * This is only true for the oplock case:
-                * For leases, lease request is required additionally!
                 */
 
                num_blobs_allowed = 1;
 
+               if (rqls != NULL) {
+                       num_blobs_allowed += 1;
+               }
+
                if (in_context_blobs.num_blobs != num_blobs_allowed) {
                        tevent_req_nterror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND);
                        return tevent_req_post(req, ev);
@@ -605,6 +666,14 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                uint32_t durable_timeout_msec = 0;
                bool do_durable_reconnect = false;
                uint64_t persistent_id = 0;
+               struct smb2_lease lease;
+               struct smb2_lease *lease_ptr = NULL;
+               ssize_t lease_len = -1;
+               bool need_replay_cache = false;
+               struct smbXsrv_open *op = NULL;
+#if 0
+               struct smb2_create_blob *svhdx = NULL;
+#endif
 
                exta = smb2_create_blob_find(&in_context_blobs,
                                             SMB2_CREATE_TAG_EXTA);
@@ -618,6 +687,15 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                                             SMB2_CREATE_TAG_TWRP);
                qfid = smb2_create_blob_find(&in_context_blobs,
                                             SMB2_CREATE_TAG_QFID);
+#if 0
+               if (smb2req->xconn->protocol >= PROTOCOL_SMB3_02) {
+                       /*
+                        * This was introduced with SMB3_02
+                        */
+                       svhdx = smb2_create_blob_find(&in_context_blobs,
+                                                     SVHDX_OPEN_DEVICE_CONTEXT);
+               }
+#endif
 
                fname = talloc_strdup(state, in_name);
                if (tevent_req_nomem(fname, req)) {
@@ -625,6 +703,12 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                }
 
                if (exta) {
+                       if (!lp_ea_support(SNUM(smb2req->tcon->compat))) {
+                               tevent_req_nterror(req,
+                                       NT_STATUS_EAS_NOT_SUPPORTED);
+                               return tevent_req_post(req, ev);
+                       }
+
                        ea_list = read_nttrans_ea_list(mem_ctx,
                                (const char *)exta->data.data, exta->data.length);
                        if (!ea_list) {
@@ -633,6 +717,12 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                                return tevent_req_post(req, ev);
                        }
 
+                       /*
+                        * NB. When SMB2+ unix extensions are added,
+                        * we need to relax this check in invalid
+                        * names - we used to not do this if
+                        * lp_posix_pathnames() was false.
+                        */
                        if (ea_list_has_invalid_name(ea_list)) {
                                tevent_req_nterror(req, STATUS_INVALID_EA_NAME);
                                return tevent_req_post(req, ev);
@@ -697,6 +787,8 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                        const uint8_t *p = dh2q->data.data;
                        uint32_t durable_v2_timeout = 0;
                        DATA_BLOB create_guid_blob;
+                       const uint8_t *hdr;
+                       uint32_t flags;
 
                        if (dh2q->data.length != 32) {
                                tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
@@ -722,6 +814,12 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                         */
                        update_open = true;
 
+                       /*
+                        * And we need to create a cache for replaying the
+                        * create.
+                        */
+                       need_replay_cache = true;
+
                        /*
                         * durable handle v2 request processed below
                         */
@@ -735,6 +833,38 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                                 */
                                durable_timeout_msec = (60*1000);
                        }
+
+                       /*
+                        * Check for replay operation.
+                        * Only consider it when we have dh2q.
+                        * If we do not have a replay operation, verify that
+                        * the create_guid is not cached for replay.
+                        */
+                       hdr = SMBD_SMB2_IN_HDR_PTR(smb2req);
+                       flags = IVAL(hdr, SMB2_HDR_FLAGS);
+                       replay_operation =
+                               flags & SMB2_HDR_FLAG_REPLAY_OPERATION;
+
+                       status = smb2srv_open_lookup_replay_cache(
+                                       smb2req->xconn, create_guid,
+                                       0 /* now */, &op);
+
+                       if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
+                               replay_operation = false;
+                       } else if (tevent_req_nterror(req, status)) {
+                               DBG_WARNING("smb2srv_open_lookup_replay_cache "
+                                           "failed: %s\n", nt_errstr(status));
+                               return tevent_req_post(req, ev);
+                       } else if (!replay_operation) {
+                               /*
+                                * If a create without replay operation flag
+                                * is sent but with a create_guid that is
+                                * currently in the replay cache -- fail.
+                                */
+                               status = NT_STATUS_DUPLICATE_OBJECTID;
+                               (void)tevent_req_nterror(req, status);
+                               return tevent_req_post(req, ev);
+                       }
                }
 
                if (dhnc) {
@@ -784,14 +914,14 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
 
                        TALLOC_FREE(fname);
                        fname = talloc_asprintf(state,
-                                       "@GMT-%04u.%02u.%02u-%02u.%02u.%02u\\%s",
+                                       "%s\\@GMT-%04u.%02u.%02u-%02u.%02u.%02u",
+                                       in_name,
                                        tm->tm_year + 1900,
                                        tm->tm_mon + 1,
                                        tm->tm_mday,
                                        tm->tm_hour,
                                        tm->tm_min,
-                                       tm->tm_sec,
-                                       in_name);
+                                       tm->tm_sec);
                        if (tevent_req_nomem(fname, req)) {
                                return tevent_req_post(req, ev);
                        }
@@ -804,6 +934,60 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                        }
                }
 
+               if (rqls) {
+                       lease_len = smb2_lease_pull(
+                               rqls->data.data, rqls->data.length, &lease);
+                       if (lease_len == -1) {
+                               tevent_req_nterror(
+                                       req, NT_STATUS_INVALID_PARAMETER);
+                               return tevent_req_post(req, ev);
+                       }
+                       lease_ptr = &lease;
+
+                       if (DEBUGLEVEL >= 10) {
+                               DEBUG(10, ("Got lease request size %d\n",
+                                          (int)lease_len));
+                               NDR_PRINT_DEBUG(smb2_lease, lease_ptr);
+                       }
+
+                       if (!smb2_lease_key_valid(&lease.lease_key)) {
+                               lease_ptr = NULL;
+                               requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
+                       }
+
+                       if ((smb2req->xconn->protocol < PROTOCOL_SMB3_00) &&
+                           (lease.lease_version != 1)) {
+                               DEBUG(10, ("v2 lease key only for SMB3\n"));
+                               lease_ptr = NULL;
+                       }
+
+                       /*
+                        * Replay with a lease is only allowed if the
+                        * established open carries a lease with the
+                        * same lease key.
+                        */
+                       if (replay_operation) {
+                               struct smb2_lease *op_ls =
+                                               &op->compat->lease->lease;
+                               int op_oplock = op->compat->oplock_type;
+
+                               if (map_samba_oplock_levels_to_smb2(op_oplock)
+                                   != SMB2_OPLOCK_LEVEL_LEASE)
+                               {
+                                       status = NT_STATUS_ACCESS_DENIED;
+                                       (void)tevent_req_nterror(req, status);
+                                       return tevent_req_post(req, ev);
+                               }
+                               if (!smb2_lease_key_equal(&lease.lease_key,
+                                                         &op_ls->lease_key))
+                               {
+                                       status = NT_STATUS_ACCESS_DENIED;
+                                       (void)tevent_req_nterror(req, status);
+                                       return tevent_req_post(req, ev);
+                               }
+                       }
+               }
+
                /* these are ignored for SMB2 */
                in_create_options &= ~(0x10);/* NTCREATEX_OPTIONS_SYNC_ALERT */
                in_create_options &= ~(0x20);/* NTCREATEX_OPTIONS_ASYNC_ALERT */
@@ -814,13 +998,20 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
 
                /*
                 * For the backend file open procedure, there are
-                * two possible modes: durable_reconnect or not.
+                * three possible modes: replay operation (in which case
+                * there is nothing else to do), durable_reconnect or
+                * new open.
                 */
-               if (do_durable_reconnect) {
+               if (replay_operation) {
+                       result = op->compat;
+                       result->op = op;
+                       update_open = false;
+                       info = op->create_action;
+               } else if (do_durable_reconnect) {
                        DATA_BLOB new_cookie = data_blob_null;
                        NTTIME now = timeval_to_nttime(&smb2req->request_time);
 
-                       status = smb2srv_open_recreate(smb2req->sconn->conn,
+                       status = smb2srv_open_recreate(smb2req->xconn,
                                                smb1req->conn->session_info,
                                                persistent_id, create_guid,
                                                now, &op);
@@ -834,7 +1025,7 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
 
                        DEBUG(10, ("smb2_create_send: %s to recreate the "
                                   "smb2srv_open struct for a durable handle.\n",
-                                  op->global->durable ? "succeded" : "failed"));
+                                  op->global->durable ? "succeeded" : "failed"));
 
                        if (!op->global->durable) {
                                talloc_free(op);
@@ -863,6 +1054,17 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                                return tevent_req_post(req, ev);
                        }
 
+                       DEBUG(10, ("result->oplock_type=%u, lease_ptr==%p\n",
+                                  (unsigned)result->oplock_type, lease_ptr));
+
+                       status = smbd_smb2_create_durable_lease_check(
+                               smb1req, fname, result, lease_ptr);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               close_file(smb1req, result, SHUTDOWN_CLOSE);
+                               tevent_req_nterror(req, status);
+                               return tevent_req_post(req, ev);
+                       }
+
                        data_blob_free(&op->global->backend_cookie);
                        op->global->backend_cookie = new_cookie;
 
@@ -877,6 +1079,15 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                        info = FILE_WAS_OPENED;
                } else {
                        struct smb_filename *smb_fname = NULL;
+                       uint32_t ucf_flags;
+
+                       if (requested_oplock_level == SMB2_OPLOCK_LEVEL_LEASE) {
+                               if (lease_ptr == NULL) {
+                                       requested_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
+                               }
+                       } else {
+                               lease_ptr = NULL;
+                       }
 
                        /*
                         * For a DFS path the function parse_dfs_path()
@@ -892,11 +1103,12 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                                }
                        }
 
+                       ucf_flags = filename_create_ucf_flags(smb1req, in_create_disposition);
                        status = filename_convert(req,
                                                  smb1req->conn,
                                                  smb1req->flags2 & FLAGS2_DFS_PATHNAMES,
                                                  fname,
-                                                 UCF_PREP_CREATEFILE,
+                                                 ucf_flags,
                                                  NULL, /* ppath_contains_wcards */
                                                  &smb_fname);
                        if (!NT_STATUS_IS_OK(status)) {
@@ -948,14 +1160,18 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                                                     in_create_options,
                                                     in_file_attributes,
                                                     map_smb2_oplock_levels_to_samba(requested_oplock_level),
+                                                    lease_ptr,
                                                     allocation_size,
                                                     0, /* private_flags */
                                                     sec_desc,
                                                     ea_list,
                                                     &result,
-                                                    &info);
+                                                    &info,
+                                                    &in_context_blobs,
+                                                    state->out_context_blobs);
                        if (!NT_STATUS_IS_OK(status)) {
-                               if (open_was_deferred(smb1req->sconn, smb1req->mid)) {
+                               if (open_was_deferred(smb1req->xconn, smb1req->mid)) {
+                                       SMBPROFILE_IOBYTES_ASYNC_SET_IDLE(smb2req->profile);
                                        return req;
                                }
                                tevent_req_nterror(req, status);
@@ -990,10 +1206,11 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                                SIVAL(p, 0, NT_STATUS_V(status));
                                SIVAL(p, 4, max_access_granted);
 
-                               status = smb2_create_blob_add(state,
-                                                       &out_context_blobs,
-                                                       SMB2_CREATE_TAG_MXAC,
-                                                       blob);
+                               status = smb2_create_blob_add(
+                                   state->out_context_blobs,
+                                   state->out_context_blobs,
+                                   SMB2_CREATE_TAG_MXAC,
+                                   blob);
                                if (!NT_STATUS_IS_OK(status)) {
                                        tevent_req_nterror(req, status);
                                        return tevent_req_post(req, ev);
@@ -1001,8 +1218,8 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                        }
                }
 
-               if (durable_requested &&
-                   BATCH_OPLOCK_TYPE(result->oplock_type))
+               if (!replay_operation && durable_requested &&
+                   (fsp_lease_type(result) & SMB2_LEASE_HANDLE))
                {
                        status = SMB_VFS_DURABLE_COOKIE(result,
                                                op,
@@ -1011,7 +1228,8 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                                op->global->backend_cookie = data_blob_null;
                        }
                }
-               if (op->global->backend_cookie.length > 0) {
+               if (!replay_operation && op->global->backend_cookie.length > 0)
+               {
                        update_open = true;
 
                        op->global->durable = true;
@@ -1020,6 +1238,9 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
 
                if (update_open) {
                        op->global->create_guid = _create_guid;
+                       if (need_replay_cache) {
+                               op->flags |= SMBXSRV_OPEN_NEED_REPLAY_CACHE;
+                       }
 
                        status = smbXsrv_open_update(op);
                        DEBUG(10, ("smb2_create_send: smbXsrv_open_update "
@@ -1035,8 +1256,8 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                        uint8_t p[8] = { 0, };
                        DATA_BLOB blob = data_blob_const(p, sizeof(p));
 
-                       status = smb2_create_blob_add(state,
-                                                     &out_context_blobs,
+                       status = smb2_create_blob_add(state->out_context_blobs,
+                                                     state->out_context_blobs,
                                                      SMB2_CREATE_TAG_DHNQ,
                                                      blob);
                        if (!NT_STATUS_IS_OK(status)) {
@@ -1045,7 +1266,18 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                        }
                }
 
-               if (dh2q && op->global->durable) {
+               if (dh2q && op->global->durable &&
+                   /*
+                    * For replay operations, we return the dh2q blob
+                    * in the case of oplocks not based on the state of
+                    * the open, but on whether it could have been granted
+                    * for the request data. In the case of leases instead,
+                    * the state of the open is used...
+                    */
+                   (!replay_operation ||
+                    in_oplock_level == SMB2_OPLOCK_LEVEL_BATCH ||
+                    in_oplock_level == SMB2_OPLOCK_LEVEL_LEASE))
+               {
                        uint8_t p[8] = { 0, };
                        DATA_BLOB blob = data_blob_const(p, sizeof(p));
                        uint32_t durable_v2_response_flags = 0;
@@ -1053,7 +1285,8 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                        SIVAL(p, 0, op->global->durable_timeout_msec);
                        SIVAL(p, 4, durable_v2_response_flags);
 
-                       status = smb2_create_blob_add(state, &out_context_blobs,
+                       status = smb2_create_blob_add(state->out_context_blobs,
+                                                     state->out_context_blobs,
                                                      SMB2_CREATE_TAG_DH2Q,
                                                      blob);
                        if (!NT_STATUS_IS_OK(status)) {
@@ -1077,7 +1310,8 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                        SBVAL(p, 0, file_index);
                        SIVAL(p, 8, result->fsp_name->st.st_ex_dev);/* FileIndexHigh */
 
-                       status = smb2_create_blob_add(state, &out_context_blobs,
+                       status = smb2_create_blob_add(state->out_context_blobs,
+                                                     state->out_context_blobs,
                                                      SMB2_CREATE_TAG_QFID,
                                                      blob);
                        if (!NT_STATUS_IS_OK(status)) {
@@ -1085,11 +1319,39 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
                                return tevent_req_post(req, ev);
                        }
                }
+
+               if ((rqls != NULL) && (result->oplock_type == LEASE_OPLOCK)) {
+                       uint8_t buf[52];
+
+                       lease = result->lease->lease;
+
+                       lease_len = sizeof(buf);
+                       if (lease.lease_version == 1) {
+                               lease_len = 32;
+                       }
+
+                       if (!smb2_lease_push(&lease, buf, lease_len)) {
+                               tevent_req_nterror(
+                                       req, NT_STATUS_INTERNAL_ERROR);
+                               return tevent_req_post(req, ev);
+                       }
+
+                       status = smb2_create_blob_add(
+                               state, state->out_context_blobs,
+                               SMB2_CREATE_TAG_RQLS,
+                               data_blob_const(buf, lease_len));
+                       if (!NT_STATUS_IS_OK(status)) {
+                               tevent_req_nterror(req, status);
+                               return tevent_req_post(req, ev);
+                       }
+               }
        }
 
        smb2req->compat_chain_fsp = smb1req->chain_fsp;
 
-       if(lp_fake_oplocks(SNUM(smb2req->tcon->compat))) {
+       if (replay_operation) {
+               state->out_oplock_level = in_oplock_level;
+       } else if (lp_fake_oplocks(SNUM(smb2req->tcon->compat))) {
                state->out_oplock_level = in_oplock_level;
        } else {
                state->out_oplock_level = map_samba_oplock_levels_to_smb2(result->oplock_type);
@@ -1101,6 +1363,7 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
        } else {
                state->out_create_action = info;
        }
+       result->op->create_action = state->out_create_action;
        state->out_file_attributes = dos_mode(result->conn,
                                           result->fsp_name);
 
@@ -1110,6 +1373,14 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
        state->out_last_write_ts = result->fsp_name->st.st_ex_mtime;
        state->out_change_ts = get_change_timespec(smb1req->conn,
                                        result, result->fsp_name);
+
+       if (lp_dos_filetime_resolution(SNUM(smb2req->tcon->compat))) {
+               dos_filetime_timespec(&state->out_creation_ts);
+               dos_filetime_timespec(&state->out_last_access_ts);
+               dos_filetime_timespec(&state->out_last_write_ts);
+               dos_filetime_timespec(&state->out_change_ts);
+       }
+
        state->out_allocation_size =
                        SMB_VFS_GET_ALLOC_SIZE(smb1req->conn, result,
                                               &(result->fsp_name->st));
@@ -1119,7 +1390,6 @@ static struct tevent_req *smbd_smb2_create_send(TALLOC_CTX *mem_ctx,
        }
        state->out_file_id_persistent = result->op->global->open_persistent_id;
        state->out_file_id_volatile = result->op->global->open_volatile_id;
-       state->out_context_blobs = out_context_blobs;
 
        DEBUG(10,("smbd_smb2_create_send: %s - %s\n",
                  fsp_str_dbg(result), fsp_fnum_dbg(result)));
@@ -1163,9 +1433,9 @@ static NTSTATUS smbd_smb2_create_recv(struct tevent_req *req,
        *out_file_attributes    = state->out_file_attributes;
        *out_file_id_persistent = state->out_file_id_persistent;
        *out_file_id_volatile   = state->out_file_id_volatile;
-       *out_context_blobs      = state->out_context_blobs;
+       *out_context_blobs      = *(state->out_context_blobs);
 
-       talloc_steal(mem_ctx, state->out_context_blobs.blobs);
+       talloc_steal(mem_ctx, state->out_context_blobs->blobs);
 
        tevent_req_received(req);
        return NT_STATUS_OK;
@@ -1211,9 +1481,8 @@ bool get_deferred_open_message_state_smb2(struct smbd_smb2_request *smb2req,
 *********************************************************/
 
 static struct smbd_smb2_request *find_open_smb2req(
-       struct smbd_server_connection *sconn, uint64_t mid)
+       struct smbXsrv_connection *xconn, uint64_t mid)
 {
-       struct smbXsrv_connection *xconn = sconn->conn;
        struct smbd_smb2_request *smb2req;
 
        for (smb2req = xconn->smb2.requests; smb2req; smb2req = smb2req->next) {
@@ -1234,12 +1503,12 @@ static struct smbd_smb2_request *find_open_smb2req(
        return NULL;
 }
 
-bool open_was_deferred_smb2(struct smbd_server_connection *sconn, uint64_t mid)
+bool open_was_deferred_smb2(struct smbXsrv_connection *xconn, uint64_t mid)
 {
        struct smbd_smb2_create_state *state = NULL;
        struct smbd_smb2_request *smb2req;
 
-       smb2req = find_open_smb2req(sconn, mid);
+       smb2req = find_open_smb2req(xconn, mid);
 
        if (!smb2req) {
                DEBUG(10,("open_was_deferred_smb2: mid %llu smb2req == NULL\n",
@@ -1297,11 +1566,11 @@ static void remove_deferred_open_message_smb2_internal(struct smbd_smb2_request
 }
 
 void remove_deferred_open_message_smb2(
-       struct smbd_server_connection *sconn, uint64_t mid)
+       struct smbXsrv_connection *xconn, uint64_t mid)
 {
        struct smbd_smb2_request *smb2req;
 
-       smb2req = find_open_smb2req(sconn, mid);
+       smb2req = find_open_smb2req(xconn, mid);
 
        if (!smb2req) {
                DEBUG(10,("remove_deferred_open_message_smb2: "
@@ -1318,7 +1587,6 @@ static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context *c
 {
        struct smbd_smb2_request *smb2req = talloc_get_type_abort(private_data,
                                        struct smbd_smb2_request);
-       struct smbd_server_connection *sconn = smb2req->sconn;
        uint64_t mid = get_mid_from_smb2req(smb2req);
        NTSTATUS status;
 
@@ -1328,18 +1596,19 @@ static void smbd_smb2_create_request_dispatch_immediate(struct tevent_context *c
 
        status = smbd_smb2_request_dispatch(smb2req);
        if (!NT_STATUS_IS_OK(status)) {
-               smbd_server_connection_terminate(sconn, nt_errstr(status));
+               smbd_server_connection_terminate(smb2req->xconn,
+                                                nt_errstr(status));
                return;
        }
 }
 
 bool schedule_deferred_open_message_smb2(
-       struct smbd_server_connection *sconn, uint64_t mid)
+       struct smbXsrv_connection *xconn, uint64_t mid)
 {
        struct smbd_smb2_create_state *state = NULL;
        struct smbd_smb2_request *smb2req;
 
-       smb2req = find_open_smb2req(sconn, mid);
+       smb2req = find_open_smb2req(xconn, mid);
 
        if (!smb2req) {
                DEBUG(10,("schedule_deferred_open_message_smb2: "
@@ -1375,7 +1644,7 @@ bool schedule_deferred_open_message_smb2(
 
        state->im = tevent_create_immediate(smb2req);
        if (!state->im) {
-               smbd_server_connection_terminate(smb2req->sconn,
+               smbd_server_connection_terminate(smb2req->xconn,
                        nt_errstr(NT_STATUS_NO_MEMORY));
                return false;
        }