s3:smbd: add expected_seq_low arg to smbd_initialize_smb2
[kai/samba-autobuild/.git] / source3 / smbd / smb2_server.c
index d9e6a7644d5984335570b4d04ba971e5b7383876..2c1546c5077c0d2a8c76c62d09565cb7d6f4789d 100644 (file)
@@ -28,7 +28,9 @@
 #include "smbprofile.h"
 #include "../lib/util/bitmap.h"
 #include "../librpc/gen_ndr/krb5pac.h"
+#include "lib/util/iov_buf.h"
 #include "auth.h"
+#include "lib/crypto/sha512.h"
 
 static void smbd_smb2_connection_handler(struct tevent_context *ev,
                                         struct tevent_fd *fde,
@@ -114,7 +116,7 @@ static const struct smbd_smb2_dispatch_table {
                _OP(SMB2_OP_KEEPALIVE),
                .as_root = true,
        },{
-               _OP(SMB2_OP_FIND),
+               _OP(SMB2_OP_QUERY_DIRECTORY),
                .need_session = true,
                .need_tcon = true,
                .fileid_ofs = 0x08,
@@ -198,11 +200,12 @@ bool smbd_is_smb2_header(const uint8_t *inbuf, size_t size)
        return true;
 }
 
-static NTSTATUS smbd_initialize_smb2(struct smbXsrv_connection *xconn)
+static NTSTATUS smbd_initialize_smb2(struct smbXsrv_connection *xconn,
+                                    uint64_t expected_seq_low)
 {
        TALLOC_FREE(xconn->transport.fde);
 
-       xconn->smb2.credits.seq_low = 0;
+       xconn->smb2.credits.seq_low = expected_seq_low;
        xconn->smb2.credits.seq_range = 1;
        xconn->smb2.credits.granted = 1;
        xconn->smb2.credits.max = lp_smb2_max_credits();
@@ -236,16 +239,22 @@ static NTSTATUS smbd_initialize_smb2(struct smbXsrv_connection *xconn)
        buf[3] = (len)&0xFF; \
 } while (0)
 
-static void smb2_setup_nbt_length(struct iovec *vector, int count)
+static bool smb2_setup_nbt_length(struct iovec *vector, int count)
 {
-       size_t len = 0;
-       int i;
+       ssize_t len;
 
-       for (i=1; i < count; i++) {
-               len += vector[i].iov_len;
+       if (count == 0) {
+               return false;
+       }
+
+       len = iov_buflen(vector+1, count-1);
+
+       if ((len == -1) || (len > 0xFFFFFF)) {
+               return false;
        }
 
        _smb2_setlen(vector[0].iov_base, len);
+       return true;
 }
 
 static int smbd_smb2_request_destructor(struct smbd_smb2_request *req)
@@ -356,7 +365,7 @@ static NTSTATUS smbd_smb2_inbuf_parse_compound(struct smbXsrv_connection *xconn,
                                goto inval;
                        }
 
-                       if (!(xconn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
+                       if (xconn->smb2.server.cipher == 0) {
                                DEBUG(10, ("Got SMB2_TRANSFORM header, "
                                           "but not negotiated "
                                           "client[0x%08X] server[0x%08X]\n",
@@ -385,7 +394,8 @@ static NTSTATUS smbd_smb2_inbuf_parse_compound(struct smbXsrv_connection *xconn,
                                goto inval;
                        }
 
-                       status = smb2srv_session_lookup(xconn, uid, now, &s);
+                       status = smb2srv_session_lookup_conn(xconn, uid, now,
+                                                            &s);
                        if (s == NULL) {
                                DEBUG(1, ("invalid session[%llu] in "
                                          "SMB2_TRANSFORM header\n",
@@ -400,7 +410,7 @@ static NTSTATUS smbd_smb2_inbuf_parse_compound(struct smbXsrv_connection *xconn,
                        tf_iov[1].iov_len = enc_len;
 
                        status = smb2_signing_decrypt_pdu(s->global->decryption_key,
-                                                         xconn->protocol,
+                                                         xconn->smb2.server.cipher,
                                                          tf_iov, 2);
                        if (!NT_STATUS_IS_OK(status)) {
                                TALLOC_FREE(iov_alloc);
@@ -509,7 +519,7 @@ static NTSTATUS smbd_smb2_request_create(struct smbXsrv_connection *xconn,
                                         const uint8_t *_inpdu, size_t size,
                                         struct smbd_smb2_request **_req)
 {
-       struct smbd_server_connection *sconn = xconn->sconn;
+       struct smbd_server_connection *sconn = xconn->client->sconn;
        struct smbd_smb2_request *req;
        uint32_t protocol_version;
        uint8_t *inpdu = NULL;
@@ -802,6 +812,7 @@ static void smb2_set_operation_credit(struct smbXsrv_connection *xconn,
 
        cmd = SVAL(inhdr, SMB2_HDR_OPCODE);
        credits_requested = SVAL(inhdr, SMB2_HDR_CREDIT);
+       credits_requested = MAX(credits_requested, 1);
        out_flags = IVAL(outhdr, SMB2_HDR_FLAGS);
        out_status = NT_STATUS(IVAL(outhdr, SMB2_HDR_STATUS));
 
@@ -820,7 +831,9 @@ static void smb2_set_operation_credit(struct smbXsrv_connection *xconn,
                 * credits on the final response.
                 */
                credits_granted = 0;
-       } else if (credits_requested > 0) {
+       } else {
+               uint16_t additional_possible =
+                       xconn->smb2.credits.max - credit_charge;
                uint16_t additional_max = 0;
                uint16_t additional_credits = credits_requested - 1;
 
@@ -846,14 +859,10 @@ static void smb2_set_operation_credit(struct smbXsrv_connection *xconn,
                        break;
                }
 
+               additional_max = MIN(additional_max, additional_possible);
                additional_credits = MIN(additional_credits, additional_max);
 
                credits_granted = credit_charge + additional_credits;
-       } else if (xconn->smb2.credits.granted == 0) {
-               /*
-                * Make sure the client has always at least one credit
-                */
-               credits_granted = 1;
        }
 
        /*
@@ -944,6 +953,7 @@ static NTSTATUS smbd_smb2_request_setup_out(struct smbd_smb2_request *req)
        struct iovec *vector;
        int count;
        int idx;
+       bool ok;
 
        count = req->in.vector_count;
        if (count <= ARRAY_SIZE(req->out._vector)) {
@@ -1035,7 +1045,10 @@ static NTSTATUS smbd_smb2_request_setup_out(struct smbd_smb2_request *req)
        req->out.vector_count = count;
 
        /* setup the length of the NBT packet */
-       smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
+       ok = smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
+       if (!ok) {
+               return NT_STATUS_INVALID_PARAMETER_MIX;
+       }
 
        DLIST_ADD_END(xconn->smb2.requests, req, struct smbd_smb2_request *);
 
@@ -1156,6 +1169,7 @@ static struct smbd_smb2_request *dup_smb2_req(const struct smbd_smb2_request *re
        struct iovec *outvec = NULL;
        int count = req->out.vector_count;
        int i;
+       bool ok;
 
        newreq = smbd_smb2_request_allocate(req->xconn);
        if (!newreq) {
@@ -1195,8 +1209,12 @@ static struct smbd_smb2_request *dup_smb2_req(const struct smbd_smb2_request *re
                return NULL;
        }
 
-       smb2_setup_nbt_length(newreq->out.vector,
-               newreq->out.vector_count);
+       ok = smb2_setup_nbt_length(newreq->out.vector,
+                                  newreq->out.vector_count);
+       if (!ok) {
+               TALLOC_FREE(newreq);
+               return NULL;
+       }
 
        return newreq;
 }
@@ -1210,6 +1228,7 @@ static NTSTATUS smb2_send_async_interim_response(const struct smbd_smb2_request
        uint8_t *outhdr = NULL;
        struct smbd_smb2_request *nreq = NULL;
        NTSTATUS status;
+       bool ok;
 
        /* Create a new smb2 request we'll use
           for the interim return. */
@@ -1222,8 +1241,11 @@ static NTSTATUS smb2_send_async_interim_response(const struct smbd_smb2_request
           ones we'll be using for the async reply. */
        nreq->out.vector_count -= SMBD_SMB2_NUM_IOV_PER_REQ;
 
-       smb2_setup_nbt_length(nreq->out.vector,
-               nreq->out.vector_count);
+       ok = smb2_setup_nbt_length(nreq->out.vector,
+                                  nreq->out.vector_count);
+       if (!ok) {
+               return NT_STATUS_INVALID_PARAMETER_MIX;
+       }
 
        /* Step back to the previous reply. */
        nreq->current_idx -= SMBD_SMB2_NUM_IOV_PER_REQ;
@@ -1250,7 +1272,7 @@ static NTSTATUS smb2_send_async_interim_response(const struct smbd_smb2_request
         */
        if (firsttf->iov_len == SMB2_TF_HDR_SIZE) {
                status = smb2_signing_encrypt_pdu(req->first_key,
-                                       xconn->protocol,
+                                       xconn->smb2.server.cipher,
                                        firsttf,
                                        nreq->out.vector_count - first_idx);
                if (!NT_STATUS_IS_OK(status)) {
@@ -1328,12 +1350,11 @@ NTSTATUS smbd_smb2_request_pending_queue(struct smbd_smb2_request *req,
 
        if (req->in.vector_count > req->current_idx + SMBD_SMB2_NUM_IOV_PER_REQ) {
                /*
-                * We're trying to go async in a compound
-                * request chain.
-                * This is only allowed for opens that
-                * cause an oplock break, otherwise it
-                * is not allowed. See [MS-SMB2].pdf
-                * note <194> on Section 3.3.5.2.7.
+                * We're trying to go async in a compound request
+                * chain. This is only allowed for opens that cause an
+                * oplock break or for the last operation in the
+                * chain, otherwise it is not allowed. See
+                * [MS-SMB2].pdf note <206> on Section 3.3.5.2.7.
                 */
                const uint8_t *inhdr = SMBD_SMB2_IN_HDR_PTR(req);
 
@@ -1420,6 +1441,60 @@ NTSTATUS smbd_smb2_request_pending_queue(struct smbd_smb2_request *req,
        return NT_STATUS_OK;
 }
 
+static DATA_BLOB smbd_smb2_signing_key(struct smbXsrv_session *session,
+                                      struct smbXsrv_connection *xconn)
+{
+       struct smbXsrv_channel_global0 *c = NULL;
+       NTSTATUS status;
+       DATA_BLOB key = data_blob_null;
+
+       status = smbXsrv_session_find_channel(session, xconn, &c);
+       if (NT_STATUS_IS_OK(status)) {
+               key = c->signing_key;
+       }
+
+       if (key.length == 0) {
+               key = session->global->signing_key;
+       }
+
+       return key;
+}
+
+static NTSTATUS smb2_get_new_nonce(struct smbXsrv_session *session,
+                                  uint64_t *new_nonce_high,
+                                  uint64_t *new_nonce_low)
+{
+       uint64_t nonce_high;
+       uint64_t nonce_low;
+
+       session->nonce_low += 1;
+       if (session->nonce_low == 0) {
+               session->nonce_low += 1;
+               session->nonce_high += 1;
+       }
+
+       /*
+        * CCM and GCM algorithms must never have their
+        * nonce wrap, or the security of the whole
+        * communication and the keys is destroyed.
+        * We must drop the connection once we have
+        * transfered too much data.
+        *
+        * NOTE: We assume nonces greater than 8 bytes.
+        */
+       if (session->nonce_high >= session->nonce_high_max) {
+               return NT_STATUS_ENCRYPTION_FAILED;
+       }
+
+       nonce_high = session->nonce_high_random;
+       nonce_high += session->nonce_high;
+       nonce_low = session->nonce_low;
+
+       *new_nonce_high = nonce_high;
+       *new_nonce_low = nonce_low;
+       return NT_STATUS_OK;
+}
+
 static void smbd_smb2_request_pending_timer(struct tevent_context *ev,
                                            struct tevent_timer *te,
                                            struct timeval current_time,
@@ -1444,6 +1519,7 @@ static void smbd_smb2_request_pending_timer(struct tevent_context *ev,
        uint64_t nonce_low = 0;
        uint64_t async_id = 0;
        NTSTATUS status;
+       bool ok;
 
        TALLOC_FREE(req->async_te);
 
@@ -1485,15 +1561,13 @@ static void smbd_smb2_request_pending_timer(struct tevent_context *ev,
        dyn = body + 8;
 
        if (req->do_encryption) {
-               struct smbXsrv_session *x = req->session;
-
-               nonce_high = x->nonce_high;
-               nonce_low = x->nonce_low;
-
-               x->nonce_low += 1;
-               if (x->nonce_low == 0) {
-                       x->nonce_low += 1;
-                       x->nonce_high += 1;
+               status = smb2_get_new_nonce(req->session,
+                                           &nonce_high,
+                                           &nonce_low);
+               if (!NT_STATUS_IS_OK(status)) {
+                       smbd_server_connection_terminate(xconn,
+                                                        nt_errstr(status));
+                       return;
                }
        }
 
@@ -1545,7 +1619,13 @@ static void smbd_smb2_request_pending_timer(struct tevent_context *ev,
        state->vector[1+SMBD_SMB2_DYN_IOV_OFS].iov_base  = dyn;
        state->vector[1+SMBD_SMB2_DYN_IOV_OFS].iov_len   = 1;
 
-       smb2_setup_nbt_length(state->vector, 1 + SMBD_SMB2_NUM_IOV_PER_REQ);
+       ok = smb2_setup_nbt_length(state->vector,
+                                  1 + SMBD_SMB2_NUM_IOV_PER_REQ);
+       if (!ok) {
+               smbd_server_connection_terminate(
+                       xconn, nt_errstr(NT_STATUS_INTERNAL_ERROR));
+               return;
+       }
 
        /* Ensure we correctly go through crediting. Grant
           the credits now, and zero credits on the final
@@ -1572,7 +1652,7 @@ static void smbd_smb2_request_pending_timer(struct tevent_context *ev,
                DATA_BLOB encryption_key = x->global->encryption_key;
 
                status = smb2_signing_encrypt_pdu(encryption_key,
-                                       xconn->protocol,
+                                       xconn->smb2.server.cipher,
                                        &state->vector[1+SMBD_SMB2_TF_IOV_OFS],
                                        SMBD_SMB2_NUM_IOV_PER_REQ);
                if (!NT_STATUS_IS_OK(status)) {
@@ -1582,7 +1662,7 @@ static void smbd_smb2_request_pending_timer(struct tevent_context *ev,
                }
        } else if (req->do_signing) {
                struct smbXsrv_session *x = req->session;
-               DATA_BLOB signing_key = x->global->channels[0].signing_key;
+               DATA_BLOB signing_key = smbd_smb2_signing_key(x, xconn);
 
                status = smb2_signing_sign_pdu(signing_key,
                                        xconn->protocol,
@@ -1754,10 +1834,26 @@ static NTSTATUS smbd_smb2_request_check_session(struct smbd_smb2_request *req)
 
        req->last_session_id = 0;
 
-       /* lookup an existing session */
-       status = smb2srv_session_lookup(req->xconn,
-                                       in_session_id, now,
-                                       &session);
+       /* look an existing session up */
+       switch (in_opcode) {
+       case SMB2_OP_SESSSETUP:
+               /*
+                * For a session bind request, we don't have the
+                * channel set up at this point yet, so we defer
+                * the verification that the connection belongs
+                * to the session to the session setup code, which
+                * can look at the session binding flags.
+                */
+               status = smb2srv_session_lookup_client(req->xconn->client,
+                                                      in_session_id, now,
+                                                      &session);
+               break;
+       default:
+               status = smb2srv_session_lookup_conn(req->xconn,
+                                                    in_session_id, now,
+                                                    &session);
+               break;
+       }
        if (session) {
                req->session = session;
                req->last_session_id = in_session_id;
@@ -1797,8 +1893,8 @@ static NTSTATUS smbd_smb2_request_check_session(struct smbd_smb2_request *req)
                return NT_STATUS_INVALID_HANDLE;
        }
 
-       if (in_session_id != req->xconn->last_session_id) {
-               req->xconn->last_session_id = in_session_id;
+       if (in_session_id != req->xconn->client->last_session_id) {
+               req->xconn->client->last_session_id = in_session_id;
                set_current_user_info(session_info->unix_info->sanitized_username,
                                      session_info->unix_info->unix_name,
                                      session_info->info->domain_name);
@@ -1922,10 +2018,13 @@ NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
        NTSTATUS return_value;
        struct smbXsrv_session *x = NULL;
        bool signing_required = false;
+       bool encryption_desired = false;
        bool encryption_required = false;
 
        inhdr = SMBD_SMB2_IN_HDR_PTR(req);
 
+       DO_PROFILE_INC(request);
+
        /* TODO: verify more things */
 
        flags = IVAL(inhdr, SMB2_HDR_FLAGS);
@@ -1967,16 +2066,13 @@ NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
        x = req->session;
        if (x != NULL) {
                signing_required = x->global->signing_required;
+               encryption_desired = x->encryption_desired;
                encryption_required = x->global->encryption_required;
-
-               if (opcode == SMB2_OP_SESSSETUP &&
-                   x->global->channels[0].signing_key.length) {
-                       signing_required = true;
-               }
        }
 
        req->do_signing = false;
        req->do_encryption = false;
+       req->was_encrypted = false;
        if (intf_v->iov_len == SMB2_TF_HDR_SIZE) {
                const uint8_t *intf = SMBD_SMB2_IN_TF_PTR(req);
                uint64_t tf_session_id = BVAL(intf, SMB2_TF_SESSION_ID);
@@ -1998,10 +2094,10 @@ NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
                                        NT_STATUS_ACCESS_DENIED);
                }
 
-               req->do_encryption = true;
+               req->was_encrypted = true;
        }
 
-       if (encryption_required && !req->do_encryption) {
+       if (encryption_required && !req->was_encrypted) {
                return smbd_smb2_request_error(req,
                                NT_STATUS_ACCESS_DENIED);
        }
@@ -2014,6 +2110,14 @@ NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
        allowed_flags = SMB2_HDR_FLAG_CHAINED |
                        SMB2_HDR_FLAG_SIGNED |
                        SMB2_HDR_FLAG_DFS;
+       if (xconn->protocol >= PROTOCOL_SMB3_11) {
+               allowed_flags |= SMB2_HDR_FLAG_PRIORITY_MASK;
+       }
+       if (opcode == SMB2_OP_NEGPROT) {
+               if (lp_server_max_protocol() >= PROTOCOL_SMB3_11) {
+                       allowed_flags |= SMB2_HDR_FLAG_PRIORITY_MASK;
+               }
+       }
        if (opcode == SMB2_OP_CANCEL) {
                allowed_flags |= SMB2_HDR_FLAG_ASYNC;
        }
@@ -2033,10 +2137,10 @@ NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
                req->compat_chain_fsp = NULL;
        }
 
-       if (req->do_encryption) {
+       if (req->was_encrypted) {
                signing_required = false;
        } else if (signing_required || (flags & SMB2_HDR_FLAG_SIGNED)) {
-               DATA_BLOB signing_key;
+               DATA_BLOB signing_key = data_blob_null;
 
                if (x == NULL) {
                        /*
@@ -2058,7 +2162,7 @@ NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
                        return smbd_smb2_request_error(req, status);
                }
 
-               signing_key = x->global->channels[0].signing_key;
+               signing_key = smbd_smb2_signing_key(x, xconn);
 
                /*
                 * If we have a signing key, we should
@@ -2119,15 +2223,22 @@ NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
                if (!NT_STATUS_IS_OK(status)) {
                        return smbd_smb2_request_error(req, status);
                }
+               if (req->tcon->encryption_desired) {
+                       encryption_desired = true;
+               }
                if (req->tcon->global->encryption_required) {
                        encryption_required = true;
                }
-               if (encryption_required && !req->do_encryption) {
+               if (encryption_required && !req->was_encrypted) {
                        return smbd_smb2_request_error(req,
                                NT_STATUS_ACCESS_DENIED);
                }
        }
 
+       if (req->was_encrypted || encryption_desired) {
+               req->do_encryption = true;
+       }
+
        if (call->fileid_ofs != 0) {
                size_t needed = call->fileid_ofs + 16;
                const uint8_t *body = SMBD_SMB2_IN_BODY_PTR(req);
@@ -2172,157 +2283,127 @@ NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
                SMB_ASSERT(call->need_tcon);
        }
 
+#define _INBYTES(_r) \
+       iov_buflen(SMBD_SMB2_IN_HDR_IOV(_r), SMBD_SMB2_NUM_IOV_PER_REQ-1)
+
        switch (opcode) {
        case SMB2_OP_NEGPROT:
-               {
-                       START_PROFILE(smb2_negprot);
-                       return_value = smbd_smb2_request_process_negprot(req);
-                       END_PROFILE(smb2_negprot);
-               }
+               SMBPROFILE_IOBYTES_ASYNC_START(smb2_negprot, profile_p,
+                                              req->profile, _INBYTES(req));
+               return_value = smbd_smb2_request_process_negprot(req);
                break;
 
        case SMB2_OP_SESSSETUP:
-               {
-                       START_PROFILE(smb2_sesssetup);
-                       return_value = smbd_smb2_request_process_sesssetup(req);
-                       END_PROFILE(smb2_sesssetup);
-               }
+               SMBPROFILE_IOBYTES_ASYNC_START(smb2_sesssetup, profile_p,
+                                              req->profile, _INBYTES(req));
+               return_value = smbd_smb2_request_process_sesssetup(req);
                break;
 
        case SMB2_OP_LOGOFF:
-               {
-                       START_PROFILE(smb2_logoff);
-                       return_value = smbd_smb2_request_process_logoff(req);
-                       END_PROFILE(smb2_logoff);
-               }
+               SMBPROFILE_IOBYTES_ASYNC_START(smb2_logoff, profile_p,
+                                              req->profile, _INBYTES(req));
+               return_value = smbd_smb2_request_process_logoff(req);
                break;
 
        case SMB2_OP_TCON:
-               {
-                       START_PROFILE(smb2_tcon);
-                       return_value = smbd_smb2_request_process_tcon(req);
-                       END_PROFILE(smb2_tcon);
-               }
+               SMBPROFILE_IOBYTES_ASYNC_START(smb2_tcon, profile_p,
+                                              req->profile, _INBYTES(req));
+               return_value = smbd_smb2_request_process_tcon(req);
                break;
 
        case SMB2_OP_TDIS:
-               {
-                       START_PROFILE(smb2_tdis);
-                       return_value = smbd_smb2_request_process_tdis(req);
-                       END_PROFILE(smb2_tdis);
-               }
+               SMBPROFILE_IOBYTES_ASYNC_START(smb2_tdis, profile_p,
+                                              req->profile, _INBYTES(req));
+               return_value = smbd_smb2_request_process_tdis(req);
                break;
 
        case SMB2_OP_CREATE:
-               {
-                       START_PROFILE(smb2_create);
-                       return_value = smbd_smb2_request_process_create(req);
-                       END_PROFILE(smb2_create);
+               if (req->subreq == NULL) {
+                       SMBPROFILE_IOBYTES_ASYNC_START(smb2_create, profile_p,
+                                                      req->profile, _INBYTES(req));
+               } else {
+                       SMBPROFILE_IOBYTES_ASYNC_SET_BUSY(req->profile);
                }
+               return_value = smbd_smb2_request_process_create(req);
                break;
 
        case SMB2_OP_CLOSE:
-               {
-                       START_PROFILE(smb2_close);
-                       return_value = smbd_smb2_request_process_close(req);
-                       END_PROFILE(smb2_close);
-               }
+               SMBPROFILE_IOBYTES_ASYNC_START(smb2_close, profile_p,
+                                              req->profile, _INBYTES(req));
+               return_value = smbd_smb2_request_process_close(req);
                break;
 
        case SMB2_OP_FLUSH:
-               {
-                       START_PROFILE(smb2_flush);
-                       return_value = smbd_smb2_request_process_flush(req);
-                       END_PROFILE(smb2_flush);
-               }
+               SMBPROFILE_IOBYTES_ASYNC_START(smb2_flush, profile_p,
+                                              req->profile, _INBYTES(req));
+               return_value = smbd_smb2_request_process_flush(req);
                break;
 
        case SMB2_OP_READ:
-               {
-                       START_PROFILE(smb2_read);
-                       return_value = smbd_smb2_request_process_read(req);
-                       END_PROFILE(smb2_read);
-               }
+               SMBPROFILE_IOBYTES_ASYNC_START(smb2_read, profile_p,
+                                              req->profile, _INBYTES(req));
+               return_value = smbd_smb2_request_process_read(req);
                break;
 
        case SMB2_OP_WRITE:
-               {
-                       START_PROFILE(smb2_write);
-                       return_value = smbd_smb2_request_process_write(req);
-                       END_PROFILE(smb2_write);
-               }
+               SMBPROFILE_IOBYTES_ASYNC_START(smb2_write, profile_p,
+                                              req->profile, _INBYTES(req));
+               return_value = smbd_smb2_request_process_write(req);
                break;
 
        case SMB2_OP_LOCK:
-               {
-                       START_PROFILE(smb2_lock);
-                       return_value = smbd_smb2_request_process_lock(req);
-                       END_PROFILE(smb2_lock);
-               }
+               SMBPROFILE_IOBYTES_ASYNC_START(smb2_lock, profile_p,
+                                              req->profile, _INBYTES(req));
+               return_value = smbd_smb2_request_process_lock(req);
                break;
 
        case SMB2_OP_IOCTL:
-               {
-                       START_PROFILE(smb2_ioctl);
-                       return_value = smbd_smb2_request_process_ioctl(req);
-                       END_PROFILE(smb2_ioctl);
-               }
+               SMBPROFILE_IOBYTES_ASYNC_START(smb2_ioctl, profile_p,
+                                              req->profile, _INBYTES(req));
+               return_value = smbd_smb2_request_process_ioctl(req);
                break;
 
        case SMB2_OP_CANCEL:
-               {
-                       START_PROFILE(smb2_cancel);
-                       return_value = smbd_smb2_request_process_cancel(req);
-                       END_PROFILE(smb2_cancel);
-               }
+               SMBPROFILE_IOBYTES_ASYNC_START(smb2_cancel, profile_p,
+                                              req->profile, _INBYTES(req));
+               return_value = smbd_smb2_request_process_cancel(req);
+               SMBPROFILE_IOBYTES_ASYNC_END(req->profile, 0);
                break;
 
        case SMB2_OP_KEEPALIVE:
-               {
-                       START_PROFILE(smb2_keepalive);
-                       return_value = smbd_smb2_request_process_keepalive(req);
-                       END_PROFILE(smb2_keepalive);
-               }
+               SMBPROFILE_IOBYTES_ASYNC_START(smb2_keepalive, profile_p,
+                                              req->profile, _INBYTES(req));
+               return_value = smbd_smb2_request_process_keepalive(req);
                break;
 
-       case SMB2_OP_FIND:
-               {
-                       START_PROFILE(smb2_find);
-                       return_value = smbd_smb2_request_process_find(req);
-                       END_PROFILE(smb2_find);
-               }
+       case SMB2_OP_QUERY_DIRECTORY:
+               SMBPROFILE_IOBYTES_ASYNC_START(smb2_find, profile_p,
+                                              req->profile, _INBYTES(req));
+               return_value = smbd_smb2_request_process_query_directory(req);
                break;
 
        case SMB2_OP_NOTIFY:
-               {
-                       START_PROFILE(smb2_notify);
-                       return_value = smbd_smb2_request_process_notify(req);
-                       END_PROFILE(smb2_notify);
-               }
+               SMBPROFILE_IOBYTES_ASYNC_START(smb2_notify, profile_p,
+                                              req->profile, _INBYTES(req));
+               return_value = smbd_smb2_request_process_notify(req);
                break;
 
        case SMB2_OP_GETINFO:
-               {
-                       START_PROFILE(smb2_getinfo);
-                       return_value = smbd_smb2_request_process_getinfo(req);
-                       END_PROFILE(smb2_getinfo);
-               }
+               SMBPROFILE_IOBYTES_ASYNC_START(smb2_getinfo, profile_p,
+                                              req->profile, _INBYTES(req));
+               return_value = smbd_smb2_request_process_getinfo(req);
                break;
 
        case SMB2_OP_SETINFO:
-               {
-                       START_PROFILE(smb2_setinfo);
-                       return_value = smbd_smb2_request_process_setinfo(req);
-                       END_PROFILE(smb2_setinfo);
-               }
+               SMBPROFILE_IOBYTES_ASYNC_START(smb2_setinfo, profile_p,
+                                              req->profile, _INBYTES(req));
+               return_value = smbd_smb2_request_process_setinfo(req);
                break;
 
        case SMB2_OP_BREAK:
-               {
-                       START_PROFILE(smb2_break);
-                       return_value = smbd_smb2_request_process_break(req);
-                       END_PROFILE(smb2_break);
-               }
+               SMBPROFILE_IOBYTES_ASYNC_START(smb2_break, profile_p,
+                                              req->profile, _INBYTES(req));
+               return_value = smbd_smb2_request_process_break(req);
                break;
 
        default:
@@ -2340,6 +2421,7 @@ static NTSTATUS smbd_smb2_request_reply(struct smbd_smb2_request *req)
        struct iovec *outhdr = SMBD_SMB2_OUT_HDR_IOV(req);
        struct iovec *outdyn = SMBD_SMB2_OUT_DYN_IOV(req);
        NTSTATUS status;
+       bool ok;
 
        req->subreq = NULL;
        TALLOC_FREE(req->async_te);
@@ -2353,17 +2435,14 @@ static NTSTATUS smbd_smb2_request_reply(struct smbd_smb2_request *req)
                DATA_BLOB encryption_key = req->session->global->encryption_key;
                uint8_t *tf;
                uint64_t session_id = req->session->global->session_wire_id;
-               struct smbXsrv_session *x = req->session;
                uint64_t nonce_high;
                uint64_t nonce_low;
 
-               nonce_high = x->nonce_high;
-               nonce_low = x->nonce_low;
-
-               x->nonce_low += 1;
-               if (x->nonce_low == 0) {
-                       x->nonce_low += 1;
-                       x->nonce_high += 1;
+               status = smb2_get_new_nonce(req->session,
+                                           &nonce_high,
+                                           &nonce_low);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
                }
 
                /*
@@ -2421,6 +2500,9 @@ static NTSTATUS smbd_smb2_request_reply(struct smbd_smb2_request *req)
                data_blob_clear_free(&req->last_key);
        }
 
+       SMBPROFILE_IOBYTES_ASYNC_END(req->profile,
+               iov_buflen(outhdr, SMBD_SMB2_NUM_IOV_PER_REQ-1));
+
        req->current_idx += SMBD_SMB2_NUM_IOV_PER_REQ;
 
        if (req->current_idx < req->out.vector_count) {
@@ -2439,7 +2521,7 @@ static NTSTATUS smbd_smb2_request_reply(struct smbd_smb2_request *req)
 
                if (req->do_signing && firsttf->iov_len == 0) {
                        struct smbXsrv_session *x = req->session;
-                       DATA_BLOB signing_key = x->global->channels[0].signing_key;
+                       DATA_BLOB signing_key = smbd_smb2_signing_key(x, xconn);
 
                        /*
                         * we need to remember the signing key
@@ -2464,7 +2546,10 @@ static NTSTATUS smbd_smb2_request_reply(struct smbd_smb2_request *req)
                req->compound_related = false;
        }
 
-       smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
+       ok = smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
+       if (!ok) {
+               return NT_STATUS_INVALID_PARAMETER_MIX;
+       }
 
        /* Set credit for these operations (zero credits if this
           is a final reply for an async operation). */
@@ -2475,7 +2560,7 @@ static NTSTATUS smbd_smb2_request_reply(struct smbd_smb2_request *req)
         */
        if (firsttf->iov_len == SMB2_TF_HDR_SIZE) {
                status = smb2_signing_encrypt_pdu(req->first_key,
-                                       xconn->protocol,
+                                       xconn->smb2.server.cipher,
                                        firsttf,
                                        req->out.vector_count - first_idx);
                if (!NT_STATUS_IS_OK(status)) {
@@ -2483,7 +2568,7 @@ static NTSTATUS smbd_smb2_request_reply(struct smbd_smb2_request *req)
                }
        } else if (req->do_signing) {
                struct smbXsrv_session *x = req->session;
-               DATA_BLOB signing_key = x->global->channels[0].signing_key;
+               DATA_BLOB signing_key = smbd_smb2_signing_key(x, xconn);
 
                status = smb2_signing_sign_pdu(signing_key,
                                               xconn->protocol,
@@ -2497,6 +2582,33 @@ static NTSTATUS smbd_smb2_request_reply(struct smbd_smb2_request *req)
                data_blob_clear_free(&req->first_key);
        }
 
+       if (req->preauth != NULL) {
+               struct hc_sha512state sctx;
+               int i;
+
+               samba_SHA512_Init(&sctx);
+               samba_SHA512_Update(&sctx, req->preauth->sha512_value,
+                                   sizeof(req->preauth->sha512_value));
+               for (i = 1; i < req->in.vector_count; i++) {
+                       samba_SHA512_Update(&sctx,
+                                           req->in.vector[i].iov_base,
+                                           req->in.vector[i].iov_len);
+               }
+               samba_SHA512_Final(req->preauth->sha512_value, &sctx);
+
+               samba_SHA512_Init(&sctx);
+               samba_SHA512_Update(&sctx, req->preauth->sha512_value,
+                                   sizeof(req->preauth->sha512_value));
+               for (i = 1; i < req->out.vector_count; i++) {
+                       samba_SHA512_Update(&sctx,
+                                           req->out.vector[i].iov_base,
+                                           req->out.vector[i].iov_len);
+               }
+               samba_SHA512_Final(req->preauth->sha512_value, &sctx);
+
+               req->preauth = NULL;
+       }
+
        /* I am a sick, sick man... :-). Sendfile hack ... JRA. */
        if (req->out.vector_count < (2*SMBD_SMB2_NUM_IOV_PER_REQ) &&
            outdyn->iov_base == NULL && outdyn->iov_len != 0) {
@@ -2600,8 +2712,13 @@ NTSTATUS smbd_smb2_request_done_ex(struct smbd_smb2_request *req,
                outdyn_v->iov_len = 0;
        }
 
-       /* see if we need to recalculate the offset to the next response */
-       if (next_command_ofs > 0) {
+       /*
+        * See if we need to recalculate the offset to the next response
+        *
+        * Note that all responses may require padding (including the very last
+        * one).
+        */
+       if (req->out.vector_count >= (2 * SMBD_SMB2_NUM_IOV_PER_REQ)) {
                next_command_ofs  = SMB2_HDR_BODY;
                next_command_ofs += SMBD_SMB2_OUT_BODY_LEN(req);
                next_command_ofs += SMBD_SMB2_OUT_DYN_LEN(req);
@@ -2655,8 +2772,11 @@ NTSTATUS smbd_smb2_request_done_ex(struct smbd_smb2_request *req,
                next_command_ofs += pad_size;
        }
 
-       SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, next_command_ofs);
-
+       if ((req->current_idx + SMBD_SMB2_NUM_IOV_PER_REQ) >= req->out.vector_count) {
+               SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, 0);
+       } else {
+               SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, next_command_ofs);
+       }
        return smbd_smb2_request_reply(req);
 }
 
@@ -2671,9 +2791,9 @@ NTSTATUS smbd_smb2_request_error_ex(struct smbd_smb2_request *req,
        uint8_t *outhdr = SMBD_SMB2_OUT_HDR_PTR(req);
        size_t unread_bytes = smbd_smb2_unread_bytes(req);
 
-       DEBUG(10,("smbd_smb2_request_error_ex: idx[%d] status[%s] |%s| at %s\n",
-                 req->current_idx, nt_errstr(status), info ? " +info" : "",
-                 location));
+       DBG_NOTICE("smbd_smb2_request_error_ex: idx[%d] status[%s] |%s| "
+                  "at %s\n", req->current_idx, nt_errstr(status),
+                  info ? " +info" : "", location);
 
        if (unread_bytes) {
                /* Recvfile error. Drain incoming socket. */
@@ -2734,48 +2854,51 @@ struct smbd_smb2_send_break_state {
        uint8_t body[1];
 };
 
-static NTSTATUS smbd_smb2_send_break(struct smbd_server_connection *sconn,
+static NTSTATUS smbd_smb2_send_break(struct smbXsrv_connection *xconn,
                                     struct smbXsrv_session *session,
                                     struct smbXsrv_tcon *tcon,
                                     const uint8_t *body,
                                     size_t body_len)
 {
        struct smbd_smb2_send_break_state *state;
-       struct smbXsrv_connection *xconn = sconn->conn;
-       bool do_encryption = session->global->encryption_required;
+       bool do_encryption = false;
+       uint64_t session_wire_id = 0;
        uint64_t nonce_high = 0;
        uint64_t nonce_low = 0;
        NTSTATUS status;
        size_t statelen;
+       bool ok;
 
-       if (tcon->global->encryption_required) {
-               do_encryption = true;
+       if (session != NULL) {
+               session_wire_id = session->global->session_wire_id;
+               do_encryption = session->encryption_desired;
+               if (tcon->encryption_desired) {
+                       do_encryption = true;
+               }
        }
 
        statelen = offsetof(struct smbd_smb2_send_break_state, body) +
                body_len;
 
-       state = talloc_zero_size(sconn, statelen);
+       state = talloc_zero_size(xconn, statelen);
        if (state == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
        talloc_set_name_const(state, "struct smbd_smb2_send_break_state");
 
        if (do_encryption) {
-               nonce_high = session->nonce_high;
-               nonce_low = session->nonce_low;
-
-               session->nonce_low += 1;
-               if (session->nonce_low == 0) {
-                       session->nonce_low += 1;
-                       session->nonce_high += 1;
+               status = smb2_get_new_nonce(session,
+                                           &nonce_high,
+                                           &nonce_low);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
                }
        }
 
        SIVAL(state->tf, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
        SBVAL(state->tf, SMB2_TF_NONCE+0, nonce_low);
        SBVAL(state->tf, SMB2_TF_NONCE+8, nonce_high);
-       SBVAL(state->tf, SMB2_TF_SESSION_ID, session->global->session_wire_id);
+       SBVAL(state->tf, SMB2_TF_SESSION_ID, session_wire_id);
 
        SIVAL(state->hdr, 0,                            SMB2_MAGIC);
        SSVAL(state->hdr, SMB2_HDR_LENGTH,              SMB2_HDR_BODY);
@@ -2824,13 +2947,17 @@ static NTSTATUS smbd_smb2_send_break(struct smbd_server_connection *sconn,
         * state->vector[1+SMBD_SMB2_DYN_IOV_OFS] is NULL by talloc_zero above
         */
 
-       smb2_setup_nbt_length(state->vector, 1 + SMBD_SMB2_NUM_IOV_PER_REQ);
+       ok = smb2_setup_nbt_length(state->vector,
+                                  1 + SMBD_SMB2_NUM_IOV_PER_REQ);
+       if (!ok) {
+               return NT_STATUS_INVALID_PARAMETER_MIX;
+       }
 
        if (do_encryption) {
                DATA_BLOB encryption_key = session->global->encryption_key;
 
                status = smb2_signing_encrypt_pdu(encryption_key,
-                                       xconn->protocol,
+                                       xconn->smb2.server.cipher,
                                        &state->vector[1+SMBD_SMB2_TF_IOV_OFS],
                                        SMBD_SMB2_NUM_IOV_PER_REQ);
                if (!NT_STATUS_IS_OK(status)) {
@@ -2852,7 +2979,7 @@ static NTSTATUS smbd_smb2_send_break(struct smbd_server_connection *sconn,
        return NT_STATUS_OK;
 }
 
-NTSTATUS smbd_smb2_send_oplock_break(struct smbd_server_connection *sconn,
+NTSTATUS smbd_smb2_send_oplock_break(struct smbXsrv_connection *xconn,
                                     struct smbXsrv_session *session,
                                     struct smbXsrv_tcon *tcon,
                                     struct smbXsrv_open *op,
@@ -2867,7 +2994,30 @@ NTSTATUS smbd_smb2_send_oplock_break(struct smbd_server_connection *sconn,
        SBVAL(body, 0x08, op->global->open_persistent_id);
        SBVAL(body, 0x10, op->global->open_volatile_id);
 
-       return smbd_smb2_send_break(sconn, session, tcon, body, sizeof(body));
+       return smbd_smb2_send_break(xconn, session, tcon, body, sizeof(body));
+}
+
+NTSTATUS smbd_smb2_send_lease_break(struct smbXsrv_connection *xconn,
+                                   uint16_t new_epoch,
+                                   uint32_t lease_flags,
+                                   struct smb2_lease_key *lease_key,
+                                   uint32_t current_lease_state,
+                                   uint32_t new_lease_state)
+{
+       uint8_t body[0x2c];
+
+       SSVAL(body, 0x00, sizeof(body));
+       SSVAL(body, 0x02, new_epoch);
+       SIVAL(body, 0x04, lease_flags);
+       SBVAL(body, 0x08, lease_key->data[0]);
+       SBVAL(body, 0x10, lease_key->data[1]);
+       SIVAL(body, 0x18, current_lease_state);
+       SIVAL(body, 0x1c, new_lease_state);
+       SIVAL(body, 0x20, 0);           /* BreakReason, MUST be 0 */
+       SIVAL(body, 0x24, 0);           /* AccessMaskHint, MUST be 0 */
+       SIVAL(body, 0x28, 0);           /* ShareMaskHint, MUST be 0 */
+
+       return smbd_smb2_send_break(xconn, NULL, NULL, body, sizeof(body));
 }
 
 static bool is_smb2_recvfile_write(struct smbd_smb2_request_read_state *state)
@@ -2953,7 +3103,7 @@ static bool is_smb2_recvfile_write(struct smbd_smb2_request_read_state *state)
 
 static NTSTATUS smbd_smb2_request_next_incoming(struct smbXsrv_connection *xconn)
 {
-       struct smbd_server_connection *sconn = xconn->sconn;
+       struct smbd_server_connection *sconn = xconn->client->sconn;
        struct smbd_smb2_request_read_state *state = &xconn->smb2.request_read_state;
        size_t max_send_queue_len;
        size_t cur_send_queue_len;
@@ -3003,14 +3153,14 @@ static NTSTATUS smbd_smb2_request_next_incoming(struct smbXsrv_connection *xconn
 void smbd_smb2_first_negprot(struct smbXsrv_connection *xconn,
                             const uint8_t *inpdu, size_t size)
 {
-       struct smbd_server_connection *sconn = xconn->sconn;
+       struct smbd_server_connection *sconn = xconn->client->sconn;
        NTSTATUS status;
        struct smbd_smb2_request *req = NULL;
 
        DEBUG(10,("smbd_smb2_first_negprot: packet length %u\n",
                 (unsigned int)size));
 
-       status = smbd_initialize_smb2(xconn);
+       status = smbd_initialize_smb2(xconn, 0);
        if (!NT_STATUS_IS_OK(status)) {
                smbd_server_connection_terminate(xconn, nt_errstr(status));
                return;
@@ -3034,6 +3184,15 @@ void smbd_smb2_first_negprot(struct smbXsrv_connection *xconn,
                return;
        }
 
+#ifdef WITH_PROFILE
+       /*
+        * this was already counted at the SMB1 layer =>
+        * smbd_smb2_request_dispatch() should not count it twice.
+        */
+       if (profile_p->values.request_stats.count > 0) {
+               profile_p->values.request_stats.count--;
+       }
+#endif
        status = smbd_smb2_request_dispatch(req);
        if (!NT_STATUS_IS_OK(status)) {
                smbd_server_connection_terminate(xconn, nt_errstr(status));
@@ -3113,8 +3272,10 @@ static NTSTATUS smbd_smb2_flush_send_queue(struct smbXsrv_connection *xconn)
 
        while (xconn->smb2.send_queue != NULL) {
                struct smbd_smb2_send_queue *e = xconn->smb2.send_queue;
+               bool ok;
 
                if (e->sendfile_header != NULL) {
+                       NTSTATUS status = NT_STATUS_INTERNAL_ERROR;
                        size_t size = 0;
                        size_t i = 0;
                        uint8_t *buf;
@@ -3142,6 +3303,7 @@ static NTSTATUS smbd_smb2_flush_send_queue(struct smbXsrv_connection *xconn)
 
                        e->sendfile_header->data = buf;
                        e->sendfile_header->length = size;
+                       e->sendfile_status = &status;
                        e->count = 0;
 
                        xconn->smb2.send_queue_len--;
@@ -3151,6 +3313,10 @@ static NTSTATUS smbd_smb2_flush_send_queue(struct smbXsrv_connection *xconn)
                         * the destructor.
                         */
                        talloc_free(e->mem_ctx);
+
+                       if (!NT_STATUS_IS_OK(status)) {
+                               return status;
+                       }
                        continue;
                }
 
@@ -3168,31 +3334,10 @@ static NTSTATUS smbd_smb2_flush_send_queue(struct smbXsrv_connection *xconn)
                if (err != 0) {
                        return map_nt_error_from_unix_common(err);
                }
-               while (ret > 0) {
-                       if (ret < e->vector[0].iov_len) {
-                               uint8_t *base;
-                               base = (uint8_t *)e->vector[0].iov_base;
-                               base += ret;
-                               e->vector[0].iov_base = (void *)base;
-                               e->vector[0].iov_len -= ret;
-                               break;
-                       }
-                       ret -= e->vector[0].iov_len;
-                       e->vector += 1;
-                       e->count -= 1;
-               }
 
-               /*
-                * there're maybe some empty vectors at the end
-                * which we need to skip, otherwise we would get
-                * ret == 0 from the readv() call and return EPIPE
-                */
-               while (e->count > 0) {
-                       if (e->vector[0].iov_len > 0) {
-                               break;
-                       }
-                       e->vector += 1;
-                       e->count -= 1;
+               ok = iov_advance(&e->vector, &e->count, ret);
+               if (!ok) {
+                       return NT_STATUS_INTERNAL_ERROR;
                }
 
                if (e->count > 0) {
@@ -3212,7 +3357,7 @@ static NTSTATUS smbd_smb2_flush_send_queue(struct smbXsrv_connection *xconn)
 static NTSTATUS smbd_smb2_io_handler(struct smbXsrv_connection *xconn,
                                     uint16_t fde_flags)
 {
-       struct smbd_server_connection *sconn = xconn->sconn;
+       struct smbd_server_connection *sconn = xconn->client->sconn;
        struct smbd_smb2_request_read_state *state = &xconn->smb2.request_read_state;
        struct smbd_smb2_request *req = NULL;
        size_t min_recvfile_size = UINT32_MAX;