libcli/smb: maintain smb2.should_sign on smbXcli_req_state
[kai/samba.git] / libcli / smb / smbXcli_base.c
index cb453856bd0807b04fb7556803fbc8f7fd031781..8a41824ef96d233aa91099304102e7ebb6830a24 100644 (file)
@@ -24,6 +24,7 @@
 #include "../lib/util/tevent_ntstatus.h"
 #include "../lib/util/tevent_unix.h"
 #include "lib/util/util_net.h"
+#include "lib/util/dlinklist.h"
 #include "../libcli/smb/smb_common.h"
 #include "../libcli/smb/smb_seal.h"
 #include "../libcli/smb/smb_signing.h"
 #include "smbXcli_base.h"
 #include "librpc/ndr/libndr.h"
 
+struct smbXcli_conn;
+struct smbXcli_req;
+struct smbXcli_session;
+
 struct smbXcli_conn {
-       int fd;
+       int read_fd;
+       int write_fd;
        struct sockaddr_storage local_ss;
        struct sockaddr_storage remote_ss;
        const char *remote_name;
@@ -90,10 +96,13 @@ struct smbXcli_conn {
 
                struct smb_signing_state *signing;
                struct smb_trans_enc_state *trans_enc;
+
+               struct tevent_req *read_braw_req;
        } smb1;
 
        struct {
                struct {
+                       uint32_t capabilities;
                        uint16_t security_mode;
                        struct GUID guid;
                } client;
@@ -111,12 +120,36 @@ struct smbXcli_conn {
                } server;
 
                uint64_t mid;
+               uint16_t cur_credits;
+               uint16_t max_credits;
+       } smb2;
+
+       struct smbXcli_session *sessions;
+};
+
+struct smbXcli_session {
+       struct smbXcli_session *prev, *next;
+       struct smbXcli_conn *conn;
+
+       struct {
+               uint64_t session_id;
+               uint16_t session_flags;
+               DATA_BLOB application_key;
+               DATA_BLOB signing_key;
+               bool should_sign;
+               bool should_encrypt;
+               DATA_BLOB encryption_key;
+               DATA_BLOB decryption_key;
+               uint64_t channel_nonce;
+               uint64_t channel_next;
+               DATA_BLOB channel_signing_key;
        } smb2;
 };
 
 struct smbXcli_req_state {
        struct tevent_context *ev;
        struct smbXcli_conn *conn;
+       struct smbXcli_session *session; /* maybe NULL */
 
        uint8_t length_hdr[4];
 
@@ -139,11 +172,12 @@ struct smbXcli_req_state {
                uint16_t *vwv;
                uint8_t bytecount_buf[2];
 
-#define MAX_SMB_IOV 5
+#define MAX_SMB_IOV 10
                /* length_hdr, hdr, words, byte_count, buffers */
                struct iovec iov[1 + 3 + MAX_SMB_IOV];
                int iov_count;
 
+               bool one_way_seqnum;
                uint32_t seqnum;
                struct tevent_req **chained_requests;
 
@@ -159,11 +193,24 @@ struct smbXcli_req_state {
                const uint8_t *dyn;
                uint32_t dyn_len;
 
-               uint8_t hdr[64];
+               uint8_t hdr[SMB2_HDR_BODY];
                uint8_t pad[7]; /* padding space for compounding */
 
-               /* always an array of 3 talloc elements */
+               /*
+                * always an array of 3 talloc elements
+                * (without a SMB2_TRANSFORM header!)
+                *
+                * HDR, BODY, DYN
+                */
                struct iovec *recv_iov;
+
+               uint16_t credit_charge;
+
+               bool should_sign;
+
+               bool signing_skipped;
+               bool notify_async;
+               bool got_async;
        } smb2;
 };
 
@@ -174,8 +221,13 @@ static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
         */
        smbXcli_conn_disconnect(conn, NT_STATUS_OK);
 
+       while (conn->sessions) {
+               conn->sessions->conn = NULL;
+               DLIST_REMOVE(conn->sessions, conn->sessions);
+       }
+
        if (conn->smb1.trans_enc) {
-               common_free_encryption_state(&conn->smb1.trans_enc);
+               TALLOC_FREE(conn->smb1.trans_enc);
        }
 
        return 0;
@@ -186,7 +238,8 @@ struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
                                         const char *remote_name,
                                         enum smb_signing_setting signing_state,
                                         uint32_t smb1_capabilities,
-                                        struct GUID *client_guid)
+                                        struct GUID *client_guid,
+                                        uint32_t smb2_capabilities)
 {
        struct smbXcli_conn *conn = NULL;
        void *ss = NULL;
@@ -199,12 +252,17 @@ struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
+       conn->read_fd = fd;
+       conn->write_fd = dup(fd);
+       if (conn->write_fd == -1) {
+               goto error;
+       }
+
        conn->remote_name = talloc_strdup(conn, remote_name);
        if (conn->remote_name == NULL) {
                goto error;
        }
 
-       conn->fd = fd;
 
        ss = (void *)&conn->local_ss;
        sa = (struct sockaddr *)ss;
@@ -275,11 +333,18 @@ struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
        if (client_guid) {
                conn->smb2.client.guid = *client_guid;
        }
+       conn->smb2.client.capabilities = smb2_capabilities;
+
+       conn->smb2.cur_credits = 1;
+       conn->smb2.max_credits = 0;
 
        talloc_set_destructor(conn, smbXcli_conn_destructor);
        return conn;
 
  error:
+       if (conn->write_fd != -1) {
+               close(conn->write_fd);
+       }
        TALLOC_FREE(conn);
        return NULL;
 }
@@ -290,7 +355,7 @@ bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
                return false;
        }
 
-       if (conn->fd == -1) {
+       if (conn->read_fd == -1) {
                return false;
        }
 
@@ -317,7 +382,7 @@ bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
 
 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
 {
-       set_socket_options(conn->fd, options);
+       set_socket_options(conn->read_fd, options);
 }
 
 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
@@ -374,6 +439,103 @@ const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
        return &conn->smb1.server.guid;
 }
 
+struct smbXcli_conn_samba_suicide_state {
+       struct smbXcli_conn *conn;
+       struct iovec iov;
+       uint8_t buf[9];
+};
+
+static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
+
+struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
+                                                  struct tevent_context *ev,
+                                                  struct smbXcli_conn *conn,
+                                                  uint8_t exitcode)
+{
+       struct tevent_req *req, *subreq;
+       struct smbXcli_conn_samba_suicide_state *state;
+
+       req = tevent_req_create(mem_ctx, &state,
+                               struct smbXcli_conn_samba_suicide_state);
+       if (req == NULL) {
+               return NULL;
+       }
+       state->conn = conn;
+       SIVAL(state->buf, 4, 0x74697865);
+       SCVAL(state->buf, 8, exitcode);
+       _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
+
+       state->iov.iov_base = state->buf;
+       state->iov.iov_len = sizeof(state->buf);
+
+       subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
+                            false, &state->iov, 1);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
+       return req;
+}
+
+static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
+               req, struct smbXcli_conn_samba_suicide_state);
+       ssize_t nwritten;
+       int err;
+
+       nwritten = writev_recv(subreq, &err);
+       TALLOC_FREE(subreq);
+       if (nwritten == -1) {
+               NTSTATUS status = map_nt_error_from_unix_common(err);
+               smbXcli_conn_disconnect(state->conn, status);
+               return;
+       }
+       tevent_req_done(req);
+}
+
+NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
+{
+       return tevent_req_simple_recv_ntstatus(req);
+}
+
+NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
+                                   uint8_t exitcode)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct tevent_context *ev;
+       struct tevent_req *req;
+       NTSTATUS status = NT_STATUS_NO_MEMORY;
+       bool ok;
+
+       if (smbXcli_conn_has_async_calls(conn)) {
+               /*
+                * Can't use sync call while an async call is in flight
+                */
+               status = NT_STATUS_INVALID_PARAMETER_MIX;
+               goto fail;
+       }
+       ev = tevent_context_init(frame);
+       if (ev == NULL) {
+               goto fail;
+       }
+       req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
+       if (req == NULL) {
+               goto fail;
+       }
+       ok = tevent_req_poll(req, ev);
+       if (!ok) {
+               status = map_nt_error_from_unix_common(errno);
+               goto fail;
+       }
+       status = smbXcli_conn_samba_suicide_recv(req);
+ fail:
+       TALLOC_FREE(frame);
+       return status;
+}
+
 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
 {
        return conn->smb1.capabilities;
@@ -399,6 +561,26 @@ uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
        return conn->smb1.server.security_mode;
 }
 
+bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
+{
+       return conn->smb1.server.readbraw;
+}
+
+bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
+{
+       return conn->smb1.server.writebraw;
+}
+
+bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
+{
+       return conn->smb1.server.lockread;
+}
+
+bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
+{
+       return conn->smb1.server.writeunlock;
+}
+
 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
 {
        return conn->smb1.server.time_zone;
@@ -429,7 +611,7 @@ void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
 {
        /* Replace the old state, if any. */
        if (conn->smb1.trans_enc) {
-               common_free_encryption_state(&conn->smb1.trans_enc);
+               TALLOC_FREE(conn->smb1.trans_enc);
        }
        conn->smb1.trans_enc = es;
 }
@@ -581,6 +763,30 @@ static int smbXcli_req_destructor(struct tevent_req *req)
        return 0;
 }
 
+static bool smb1cli_req_cancel(struct tevent_req *req);
+static bool smb2cli_req_cancel(struct tevent_req *req);
+
+static bool smbXcli_req_cancel(struct tevent_req *req)
+{
+       struct smbXcli_req_state *state =
+               tevent_req_data(req,
+               struct smbXcli_req_state);
+
+       if (!smbXcli_conn_is_connected(state->conn)) {
+               return false;
+       }
+
+       if (state->conn->protocol == PROTOCOL_NONE) {
+               return false;
+       }
+
+       if (state->conn->protocol >= PROTOCOL_SMB2_02) {
+               return smb2cli_req_cancel(req);
+       }
+
+       return smb1cli_req_cancel(req);
+}
+
 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
 
 bool smbXcli_req_set_pending(struct tevent_req *req)
@@ -608,6 +814,7 @@ bool smbXcli_req_set_pending(struct tevent_req *req)
        pending[num_pending] = req;
        conn->pending = pending;
        talloc_set_destructor(req, smbXcli_req_destructor);
+       tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
 
        if (!smbXcli_conn_receive_next(conn)) {
                /*
@@ -658,7 +865,9 @@ static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
         * We're the first ones, add the read_smb request that waits for the
         * answer from the server
         */
-       conn->read_smb_req = read_smb_send(conn->pending, state->ev, conn->fd);
+       conn->read_smb_req = read_smb_send(conn->pending,
+                                          state->ev,
+                                          conn->read_fd);
        if (conn->read_smb_req == NULL) {
                return false;
        }
@@ -668,10 +877,16 @@ static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
 
 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
 {
-       if (conn->fd != -1) {
-               close(conn->fd);
+       tevent_queue_stop(conn->outgoing);
+
+       if (conn->read_fd != -1) {
+               close(conn->read_fd);
        }
-       conn->fd = -1;
+       if (conn->write_fd != -1) {
+               close(conn->write_fd);
+       }
+       conn->read_fd = -1;
+       conn->write_fd = -1;
 
        /*
         * Cancel all pending requests. We do not do a for-loop walking
@@ -733,7 +948,7 @@ void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
 
                        /*
                         * we need to defer the callback, because we may notify
-                        * more then one caller.
+                        * more than one caller.
                         */
                        tevent_req_defer_callback(req, state->ev);
                        tevent_req_nterror(req, status);
@@ -863,6 +1078,62 @@ static void smb1cli_req_flags(enum protocol_types protocol,
        *_flags2 = flags2;
 }
 
+static void smb1cli_req_cancel_done(struct tevent_req *subreq);
+
+static bool smb1cli_req_cancel(struct tevent_req *req)
+{
+       struct smbXcli_req_state *state =
+               tevent_req_data(req,
+               struct smbXcli_req_state);
+       uint8_t flags;
+       uint16_t flags2;
+       uint32_t pid;
+       uint16_t tid;
+       uint16_t uid;
+       uint16_t mid;
+       struct tevent_req *subreq;
+       NTSTATUS status;
+
+       flags = CVAL(state->smb1.hdr, HDR_FLG);
+       flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
+       pid  = SVAL(state->smb1.hdr, HDR_PID);
+       pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
+       tid = SVAL(state->smb1.hdr, HDR_TID);
+       uid = SVAL(state->smb1.hdr, HDR_UID);
+       mid = SVAL(state->smb1.hdr, HDR_MID);
+
+       subreq = smb1cli_req_create(state, state->ev,
+                                   state->conn,
+                                   SMBntcancel,
+                                   flags, 0,
+                                   flags2, 0,
+                                   0, /* timeout */
+                                   pid, tid, uid,
+                                   0, NULL, /* vwv */
+                                   0, NULL); /* bytes */
+       if (subreq == NULL) {
+               return false;
+       }
+       smb1cli_req_set_mid(subreq, mid);
+
+       status = smb1cli_req_chain_submit(&subreq, 1);
+       if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(subreq);
+               return false;
+       }
+       smb1cli_req_set_mid(subreq, 0);
+
+       tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
+
+       return true;
+}
+
+static void smb1cli_req_cancel_done(struct tevent_req *subreq)
+{
+       /* we do not care about the result */
+       TALLOC_FREE(subreq);
+}
+
 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
                                      struct tevent_context *ev,
                                      struct smbXcli_conn *conn,
@@ -927,7 +1198,7 @@ struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
        SSVAL(state->smb1.hdr, HDR_PID,     pid);
        SSVAL(state->smb1.hdr, HDR_UID,     uid);
        SSVAL(state->smb1.hdr, HDR_MID,     0); /* this comes later */
-       SSVAL(state->smb1.hdr, HDR_WCT,     wct);
+       SCVAL(state->smb1.hdr, HDR_WCT,     wct);
 
        state->smb1.vwv = vwv;
 
@@ -961,8 +1232,11 @@ struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
        case SMBtranss:
        case SMBtranss2:
        case SMBnttranss:
+               state->one_way = true;
+               break;
        case SMBntcancel:
                state->one_way = true;
+               state->smb1.one_way_seqnum = true;
                break;
        case SMBlockingX:
                if ((wct == 8) &&
@@ -977,8 +1251,10 @@ struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
 
 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
                                   struct iovec *iov, int iov_count,
-                                  uint32_t *seqnum)
+                                  uint32_t *seqnum,
+                                  bool one_way_seqnum)
 {
+       TALLOC_CTX *frame = NULL;
        uint8_t *buf;
 
        /*
@@ -1002,16 +1278,19 @@ static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
                return NT_STATUS_INVALID_PARAMETER_MIX;
        }
 
-       buf = smbXcli_iov_concat(talloc_tos(), iov, iov_count);
+       frame = talloc_stackframe();
+
+       buf = smbXcli_iov_concat(frame, iov, iov_count);
        if (buf == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       *seqnum = smb_signing_next_seqnum(conn->smb1.signing, false);
+       *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
+                                         one_way_seqnum);
        smb_signing_sign_pdu(conn->smb1.signing, buf, *seqnum);
        memcpy(iov[1].iov_base, buf+4, iov[1].iov_len);
 
-       TALLOC_FREE(buf);
+       TALLOC_FREE(frame);
        return NT_STATUS_OK;
 }
 
@@ -1026,6 +1305,7 @@ static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
 {
        struct tevent_req *subreq;
        NTSTATUS status;
+       uint8_t cmd;
        uint16_t mid;
 
        if (!smbXcli_conn_is_connected(state->conn)) {
@@ -1052,6 +1332,14 @@ static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
                return NT_STATUS_INVALID_PARAMETER_MIX;
        }
 
+       cmd = CVAL(iov[1].iov_base, HDR_COM);
+       if (cmd == SMBreadBraw) {
+               if (smbXcli_conn_has_async_calls(state->conn)) {
+                       return NT_STATUS_INVALID_PARAMETER_MIX;
+               }
+               state->conn->smb1.read_braw_req = req;
+       }
+
        if (state->smb1.mid != 0) {
                mid = state->smb1.mid;
        } else {
@@ -1062,7 +1350,8 @@ static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
        _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
 
        status = smb1cli_conn_signv(state->conn, iov, iov_count,
-                                   &state->smb1.seqnum);
+                                   &state->smb1.seqnum,
+                                   state->smb1.one_way_seqnum);
 
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -1102,8 +1391,10 @@ static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
                state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
        }
 
+       tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
+
        subreq = writev_send(state, state->ev, state->conn->outgoing,
-                            state->conn->fd, false, iov, iov_count);
+                            state->conn->write_fd, false, iov, iov_count);
        if (subreq == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -1433,6 +1724,40 @@ static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
        size_t num_chained = 0;
        size_t num_responses = 0;
 
+       if (conn->smb1.read_braw_req != NULL) {
+               req = conn->smb1.read_braw_req;
+               conn->smb1.read_braw_req = NULL;
+               state = tevent_req_data(req, struct smbXcli_req_state);
+
+               smbXcli_req_unset_pending(req);
+
+               if (state->smb1.recv_iov == NULL) {
+                       /*
+                        * For requests with more than
+                        * one response, we have to readd the
+                        * recv_iov array.
+                        */
+                       state->smb1.recv_iov = talloc_zero_array(state,
+                                                                struct iovec,
+                                                                3);
+                       if (tevent_req_nomem(state->smb1.recv_iov, req)) {
+                               return NT_STATUS_OK;
+                       }
+               }
+
+               state->smb1.recv_iov[0].iov_base = (void *)(inbuf + NBT_HDR_SIZE);
+               state->smb1.recv_iov[0].iov_len = smb_len_nbt(inbuf);
+               ZERO_STRUCT(state->smb1.recv_iov[1]);
+               ZERO_STRUCT(state->smb1.recv_iov[2]);
+
+               state->smb1.recv_cmd = SMBreadBraw;
+               state->smb1.recv_status = NT_STATUS_OK;
+               state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
+
+               tevent_req_done(req);
+               return NT_STATUS_OK;
+       }
+
        if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
            && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
                DEBUG(10, ("Got non-SMB PDU\n"));
@@ -1529,6 +1854,20 @@ static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
 
                smbXcli_req_unset_pending(req);
 
+               if (state->smb1.recv_iov == NULL) {
+                       /*
+                        * For requests with more than
+                        * one response, we have to readd the
+                        * recv_iov array.
+                        */
+                       state->smb1.recv_iov = talloc_zero_array(state,
+                                                                struct iovec,
+                                                                3);
+                       if (tevent_req_nomem(state->smb1.recv_iov, req)) {
+                               return NT_STATUS_OK;
+                       }
+               }
+
                state->smb1.recv_cmd = cmd;
                state->smb1.recv_status = status;
                state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
@@ -1577,6 +1916,20 @@ static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
                        continue;
                }
 
+               if (state->smb1.recv_iov == NULL) {
+                       /*
+                        * For requests with more than
+                        * one response, we have to readd the
+                        * recv_iov array.
+                        */
+                       state->smb1.recv_iov = talloc_zero_array(state,
+                                                                struct iovec,
+                                                                3);
+                       if (tevent_req_nomem(state->smb1.recv_iov, req)) {
+                               continue;
+                       }
+               }
+
                state->smb1.recv_cmd = cmd;
 
                if (i == (num_responses - 1)) {
@@ -1671,13 +2024,16 @@ NTSTATUS smb1cli_req_recv(struct tevent_req *req,
 
        if (state->inbuf != NULL) {
                recv_iov = state->smb1.recv_iov;
-               hdr = (uint8_t *)recv_iov[0].iov_base;
-               wct = recv_iov[1].iov_len/2;
-               vwv = (uint16_t *)recv_iov[1].iov_base;
-               vwv_offset = PTR_DIFF(vwv, hdr);
-               num_bytes = recv_iov[2].iov_len;
-               bytes = (uint8_t *)recv_iov[2].iov_base;
-               bytes_offset = PTR_DIFF(bytes, hdr);
+               state->smb1.recv_iov = NULL;
+               if (state->smb1.recv_cmd != SMBreadBraw) {
+                       hdr = (uint8_t *)recv_iov[0].iov_base;
+                       wct = recv_iov[1].iov_len/2;
+                       vwv = (uint16_t *)recv_iov[1].iov_base;
+                       vwv_offset = PTR_DIFF(vwv, hdr);
+                       num_bytes = recv_iov[2].iov_len;
+                       bytes = (uint8_t *)recv_iov[2].iov_base;
+                       bytes_offset = PTR_DIFF(bytes, hdr);
+               }
        }
 
        if (tevent_req_is_nterror(req, &status)) {
@@ -1958,6 +2314,74 @@ uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
        return conn->smb2.server.max_write_size;
 }
 
+void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
+                                 uint16_t max_credits)
+{
+       conn->smb2.max_credits = max_credits;
+}
+
+static void smb2cli_req_cancel_done(struct tevent_req *subreq);
+
+static bool smb2cli_req_cancel(struct tevent_req *req)
+{
+       struct smbXcli_req_state *state =
+               tevent_req_data(req,
+               struct smbXcli_req_state);
+       uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
+       uint32_t pid = IVAL(state->smb2.hdr, SMB2_HDR_PID);
+       uint32_t tid = IVAL(state->smb2.hdr, SMB2_HDR_TID);
+       uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
+       uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
+       struct smbXcli_session *session = state->session;
+       uint8_t *fixed = state->smb2.pad;
+       uint16_t fixed_len = 4;
+       struct tevent_req *subreq;
+       struct smbXcli_req_state *substate;
+       NTSTATUS status;
+
+       SSVAL(fixed, 0, 0x04);
+       SSVAL(fixed, 2, 0);
+
+       subreq = smb2cli_req_create(state, state->ev,
+                                   state->conn,
+                                   SMB2_OP_CANCEL,
+                                   flags, 0,
+                                   0, /* timeout */
+                                   pid, tid, session,
+                                   fixed, fixed_len,
+                                   NULL, 0);
+       if (subreq == NULL) {
+               return false;
+       }
+       substate = tevent_req_data(subreq, struct smbXcli_req_state);
+
+       if (flags & SMB2_HDR_FLAG_ASYNC) {
+               mid = 0;
+       }
+
+       SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
+       SIVAL(substate->smb2.hdr, SMB2_HDR_PID, pid);
+       SIVAL(substate->smb2.hdr, SMB2_HDR_TID, tid);
+       SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
+       SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
+
+       status = smb2cli_req_compound_submit(&subreq, 1);
+       if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(subreq);
+               return false;
+       }
+
+       tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
+
+       return true;
+}
+
+static void smb2cli_req_cancel_done(struct tevent_req *subreq)
+{
+       /* we do not care about the result */
+       TALLOC_FREE(subreq);
+}
+
 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
                                      struct tevent_context *ev,
                                      struct smbXcli_conn *conn,
@@ -1967,7 +2391,7 @@ struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
                                      uint32_t timeout_msec,
                                      uint32_t pid,
                                      uint32_t tid,
-                                     uint64_t uid,
+                                     struct smbXcli_session *session,
                                      const uint8_t *fixed,
                                      uint16_t fixed_len,
                                      const uint8_t *dyn,
@@ -1976,6 +2400,7 @@ struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
        struct tevent_req *req;
        struct smbXcli_req_state *state;
        uint32_t flags = 0;
+       uint64_t uid = 0;
 
        req = tevent_req_create(mem_ctx, &state,
                                struct smbXcli_req_state);
@@ -1985,6 +2410,18 @@ struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
 
        state->ev = ev;
        state->conn = conn;
+       state->session = session;
+
+       if (session) {
+               uid = session->smb2.session_id;
+
+               state->smb2.should_sign = session->smb2.should_sign;
+
+               if (cmd == SMB2_OP_SESSSETUP &&
+                   session->smb2.signing_key.length != 0) {
+                       state->smb2.should_sign = true;
+               }
+       }
 
        state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
        if (state->smb2.recv_iov == NULL) {
@@ -2002,10 +2439,7 @@ struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
 
        SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID,    SMB2_MAGIC);
        SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH,         SMB2_HDR_BODY);
-       SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE,  1);
-       SIVAL(state->smb2.hdr, SMB2_HDR_STATUS,         NT_STATUS_V(NT_STATUS_OK));
        SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE,         cmd);
-       SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT,         31);
        SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS,          flags);
        SIVAL(state->smb2.hdr, SMB2_HDR_PID,            pid);
        SIVAL(state->smb2.hdr, SMB2_HDR_TID,            tid);
@@ -2038,7 +2472,16 @@ struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
        return req;
 }
 
-static void smb2cli_writev_done(struct tevent_req *subreq);
+void smb2cli_req_set_notify_async(struct tevent_req *req)
+{
+       struct smbXcli_req_state *state =
+               tevent_req_data(req,
+               struct smbXcli_req_state);
+
+       state->smb2.notify_async = true;
+}
+
+static void smb2cli_req_writev_done(struct tevent_req *subreq);
 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
                                               TALLOC_CTX *tmp_mem,
                                               uint8_t *inbuf);
@@ -2066,9 +2509,15 @@ NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
        nbt_len = 0;
 
        for (i=0; i<num_reqs; i++) {
+               int hdr_iov;
                size_t reqlen;
                bool ret;
+               uint16_t opcode;
+               uint64_t avail;
+               uint16_t charge;
+               uint16_t credits;
                uint64_t mid;
+               const DATA_BLOB *signing_key = NULL;
 
                if (!tevent_req_is_in_progress(reqs[i])) {
                        return NT_STATUS_INTERNAL_ERROR;
@@ -2085,30 +2534,91 @@ NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
                        return NT_STATUS_REVISION_MISMATCH;
                }
 
-               if (state->conn->smb2.mid == UINT64_MAX) {
-                       return NT_STATUS_CONNECTION_ABORTED;
+               opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
+               if (opcode == SMB2_OP_CANCEL) {
+                       goto skip_credits;
                }
 
-               mid = state->conn->smb2.mid;
-               state->conn->smb2.mid += 1;
+               avail = UINT64_MAX - state->conn->smb2.mid;
+               if (avail < 1) {
+                       return NT_STATUS_CONNECTION_ABORTED;
+               }
 
-               SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
+               if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
+                       charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
+               } else {
+                       charge = 1;
+               }
 
-               iov[num_iov].iov_base = state->smb2.hdr;
-               iov[num_iov].iov_len  = sizeof(state->smb2.hdr);
-               num_iov += 1;
+               charge = MAX(state->smb2.credit_charge, charge);
 
-               iov[num_iov].iov_base = discard_const(state->smb2.fixed);
-               iov[num_iov].iov_len  = state->smb2.fixed_len;
-               num_iov += 1;
+               avail = MIN(avail, state->conn->smb2.cur_credits);
+               if (avail < charge) {
+                       return NT_STATUS_INTERNAL_ERROR;
+               }
 
-               if (state->smb2.dyn != NULL) {
-                       iov[num_iov].iov_base = discard_const(state->smb2.dyn);
-                       iov[num_iov].iov_len  = state->smb2.dyn_len;
-                       num_iov += 1;
+               credits = 0;
+               if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
+                       credits = state->conn->smb2.max_credits -
+                                 state->conn->smb2.cur_credits;
+               }
+               if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
+                       credits += 1;
                }
 
-               reqlen  = sizeof(state->smb2.hdr);
+               mid = state->conn->smb2.mid;
+               state->conn->smb2.mid += charge;
+               state->conn->smb2.cur_credits -= charge;
+
+               if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
+                       SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
+               }
+               SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
+               SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
+
+skip_credits:
+               if (state->session) {
+                       /*
+                        * We prefer the channel signing key if it is
+                        * already there.
+                        */
+                       if (state->smb2.should_sign) {
+                               signing_key = &state->session->smb2.channel_signing_key;
+                       }
+
+                       /*
+                        * If it is a channel binding, we already have the main
+                        * signing key and try that one.
+                        */
+                       if (signing_key && signing_key->length == 0) {
+                               signing_key = &state->session->smb2.signing_key;
+                       }
+
+                       /*
+                        * If we do not have any session key yet, we skip the
+                        * signing of SMB2_OP_SESSSETUP requests.
+                        */
+                       if (signing_key && signing_key->length == 0) {
+                               signing_key = NULL;
+                       }
+               }
+
+               hdr_iov = num_iov;
+               iov[num_iov].iov_base = state->smb2.hdr;
+               iov[num_iov].iov_len  = sizeof(state->smb2.hdr);
+               num_iov += 1;
+
+               iov[num_iov].iov_base = discard_const(state->smb2.fixed);
+               iov[num_iov].iov_len  = state->smb2.fixed_len;
+               num_iov += 1;
+
+               if (state->smb2.dyn != NULL) {
+                       iov[num_iov].iov_base = discard_const(state->smb2.dyn);
+                       iov[num_iov].iov_len  = state->smb2.dyn_len;
+                       num_iov += 1;
+               }
+
+               reqlen  = sizeof(state->smb2.hdr);
                reqlen += state->smb2.fixed_len;
                reqlen += state->smb2.dyn_len;
 
@@ -2124,16 +2634,23 @@ NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
                }
                nbt_len += reqlen;
 
+               if (signing_key) {
+                       NTSTATUS status;
+
+                       status = smb2_signing_sign_pdu(*signing_key,
+                                                      state->session->conn->protocol,
+                                                      &iov[hdr_iov], num_iov - hdr_iov);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               return status;
+                       }
+               }
+
                ret = smbXcli_req_set_pending(reqs[i]);
                if (!ret) {
                        return NT_STATUS_NO_MEMORY;
                }
        }
 
-       /*
-        * TODO: Do signing here
-        */
-
        state = tevent_req_data(reqs[0], struct smbXcli_req_state);
        _smb_setlen_tcp(state->length_hdr, nbt_len);
        iov[0].iov_base = state->length_hdr;
@@ -2144,14 +2661,23 @@ NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
        }
 
        subreq = writev_send(state, state->ev, state->conn->outgoing,
-                            state->conn->fd, false, iov, num_iov);
+                            state->conn->write_fd, false, iov, num_iov);
        if (subreq == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
-       tevent_req_set_callback(subreq, smb2cli_writev_done, reqs[0]);
+       tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
        return NT_STATUS_OK;
 }
 
+void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
+{
+       struct smbXcli_req_state *state =
+               tevent_req_data(req,
+               struct smbXcli_req_state);
+
+       state->smb2.credit_charge = charge;
+}
+
 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
                                    struct tevent_context *ev,
                                    struct smbXcli_conn *conn,
@@ -2161,7 +2687,7 @@ struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
                                    uint32_t timeout_msec,
                                    uint32_t pid,
                                    uint32_t tid,
-                                   uint64_t uid,
+                                   struct smbXcli_session *session,
                                    const uint8_t *fixed,
                                    uint16_t fixed_len,
                                    const uint8_t *dyn,
@@ -2173,7 +2699,7 @@ struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
        req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
                                 additional_flags, clear_flags,
                                 timeout_msec,
-                                pid, tid, uid,
+                                pid, tid, session,
                                 fixed, fixed_len, dyn, dyn_len);
        if (req == NULL) {
                return NULL;
@@ -2188,7 +2714,7 @@ struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
        return req;
 }
 
-static void smb2cli_writev_done(struct tevent_req *subreq)
+static void smb2cli_req_writev_done(struct tevent_req *subreq)
 {
        struct tevent_req *req =
                tevent_req_callback_data(subreq,
@@ -2209,27 +2735,25 @@ static void smb2cli_writev_done(struct tevent_req *subreq)
        }
 }
 
-static NTSTATUS smb2cli_inbuf_parse_compound(uint8_t *buf, TALLOC_CTX *mem_ctx,
+static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
+                                            uint8_t *buf,
+                                            size_t buflen,
+                                            TALLOC_CTX *mem_ctx,
                                             struct iovec **piov, int *pnum_iov)
 {
        struct iovec *iov;
-       int num_iov;
-       size_t buflen;
-       size_t taken;
-       uint8_t *first_hdr;
-
-       num_iov = 0;
+       int num_iov = 0;
+       size_t taken = 0;
+       uint8_t *first_hdr = buf;
 
        iov = talloc_array(mem_ctx, struct iovec, num_iov);
        if (iov == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       buflen = smb_len_tcp(buf);
-       taken = 0;
-       first_hdr = buf + NBT_HDR_SIZE;
-
        while (taken < buflen) {
+               uint8_t *tf = NULL;
+               size_t tf_len = 0;
                size_t len = buflen - taken;
                uint8_t *hdr = first_hdr + taken;
                struct iovec *cur;
@@ -2238,6 +2762,58 @@ static NTSTATUS smb2cli_inbuf_parse_compound(uint8_t *buf, TALLOC_CTX *mem_ctx,
                uint16_t body_size;
                struct iovec *iov_tmp;
 
+               if (len < 4) {
+                       DEBUG(10, ("%d bytes left, expected at least %d\n",
+                                  (int)len, 4));
+                       goto inval;
+               }
+               if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
+                       struct smbXcli_session *s;
+                       uint64_t uid;
+                       struct iovec tf_iov[2];
+                       NTSTATUS status;
+
+                       if (len < SMB2_TF_HDR_SIZE) {
+                               DEBUG(10, ("%d bytes left, expected at least %d\n",
+                                          (int)len, SMB2_TF_HDR_SIZE));
+                               goto inval;
+                       }
+                       tf = hdr;
+                       tf_len = SMB2_TF_HDR_SIZE;
+                       taken += tf_len;
+
+                       hdr = first_hdr + taken;
+                       len = IVAL(tf, SMB2_TF_MSG_SIZE);
+                       uid = BVAL(tf, SMB2_TF_SESSION_ID);
+
+                       s = conn->sessions;
+                       for (; s; s = s->next) {
+                               if (s->smb2.session_id != uid) {
+                                       continue;
+                               }
+                               break;
+                       }
+
+                       if (s == NULL) {
+                               DEBUG(10, ("unknown session_id %llu\n",
+                                          (unsigned long long)uid));
+                               goto inval;
+                       }
+
+                       tf_iov[0].iov_base = (void *)tf;
+                       tf_iov[0].iov_len = tf_len;
+                       tf_iov[1].iov_base = (void *)hdr;
+                       tf_iov[1].iov_len = len;
+
+                       status = smb2_signing_decrypt_pdu(s->smb2.decryption_key,
+                                                         conn->protocol,
+                                                         tf_iov, 2);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               TALLOC_FREE(iov);
+                               return status;
+                       }
+               }
+
                /*
                 * We need the header plus the body length field
                 */
@@ -2269,6 +2845,9 @@ static NTSTATUS smb2cli_inbuf_parse_compound(uint8_t *buf, TALLOC_CTX *mem_ctx,
                        if (next_command_ofs > full_size) {
                                goto inval;
                        }
+                       if (tf && next_command_ofs < len) {
+                               goto inval;
+                       }
                        full_size = next_command_ofs;
                }
                if (body_size < 2) {
@@ -2281,21 +2860,23 @@ static NTSTATUS smb2cli_inbuf_parse_compound(uint8_t *buf, TALLOC_CTX *mem_ctx,
                }
 
                iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
-                                        num_iov + 3);
+                                        num_iov + 4);
                if (iov_tmp == NULL) {
                        TALLOC_FREE(iov);
                        return NT_STATUS_NO_MEMORY;
                }
                iov = iov_tmp;
                cur = &iov[num_iov];
-               num_iov += 3;
+               num_iov += 4;
 
-               cur[0].iov_base = hdr;
-               cur[0].iov_len  = SMB2_HDR_BODY;
-               cur[1].iov_base = hdr + SMB2_HDR_BODY;
-               cur[1].iov_len  = body_size;
-               cur[2].iov_base = hdr + SMB2_HDR_BODY + body_size;
-               cur[2].iov_len  = full_size - (SMB2_HDR_BODY + body_size);
+               cur[0].iov_base = tf;
+               cur[0].iov_len  = tf_len;
+               cur[1].iov_base = hdr;
+               cur[1].iov_len  = SMB2_HDR_BODY;
+               cur[2].iov_base = hdr + SMB2_HDR_BODY;
+               cur[2].iov_len  = body_size;
+               cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
+               cur[3].iov_len  = full_size - (SMB2_HDR_BODY + body_size);
 
                taken += full_size;
        }
@@ -2338,21 +2919,38 @@ static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
        int i, num_iov;
        NTSTATUS status;
        bool defer = true;
+       struct smbXcli_session *last_session = NULL;
+       size_t inbuf_len = smb_len_tcp(inbuf);
 
-       status = smb2cli_inbuf_parse_compound(inbuf, tmp_mem,
+       status = smb2cli_inbuf_parse_compound(conn,
+                                             inbuf + NBT_HDR_SIZE,
+                                             inbuf_len,
+                                             tmp_mem,
                                              &iov, &num_iov);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
-       for (i=0; i<num_iov; i+=3) {
+       for (i=0; i<num_iov; i+=4) {
                uint8_t *inbuf_ref = NULL;
                struct iovec *cur = &iov[i];
-               uint8_t *inhdr = (uint8_t *)cur[0].iov_base;
+               uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
                uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
                uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
                uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
                uint16_t req_opcode;
+               uint32_t req_flags;
+               uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
+               uint32_t new_credits;
+               struct smbXcli_session *session = NULL;
+               const DATA_BLOB *signing_key = NULL;
+
+               new_credits = conn->smb2.cur_credits;
+               new_credits += credits;
+               if (new_credits > UINT16_MAX) {
+                       return NT_STATUS_INVALID_NETWORK_RESPONSE;
+               }
+               conn->smb2.cur_credits += credits;
 
                req = smb2cli_conn_find_pending(conn, mid);
                if (req == NULL) {
@@ -2360,10 +2958,13 @@ static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
                }
                state = tevent_req_data(req, struct smbXcli_req_state);
 
+               state->smb2.got_async = false;
+
                req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
                if (opcode != req_opcode) {
                        return NT_STATUS_INVALID_NETWORK_RESPONSE;
                }
+               req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
 
                if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
                        return NT_STATUS_INVALID_NETWORK_RESPONSE;
@@ -2372,22 +2973,164 @@ static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
                status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
                if ((flags & SMB2_HDR_FLAG_ASYNC) &&
                    NT_STATUS_EQUAL(status, STATUS_PENDING)) {
-                       uint32_t req_flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
                        uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
 
+                       /*
+                        * async interim responses are not signed,
+                        * even if the SMB2_HDR_FLAG_SIGNED flag
+                        * is set.
+                        */
                        req_flags |= SMB2_HDR_FLAG_ASYNC;
                        SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
                        SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
+
+                       if (state->smb2.notify_async) {
+                               state->smb2.got_async = true;
+                               tevent_req_defer_callback(req, state->ev);
+                               tevent_req_notify_callback(req);
+                       }
                        continue;
                }
 
+               session = state->session;
+               if (req_flags & SMB2_HDR_FLAG_CHAINED) {
+                       session = last_session;
+               }
+               last_session = session;
+
+               if (state->smb2.should_sign) {
+                       if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
+                               return NT_STATUS_ACCESS_DENIED;
+                       }
+               }
+
+               if (flags & SMB2_HDR_FLAG_SIGNED) {
+                       uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
+
+                       if (session == NULL) {
+                               struct smbXcli_session *s;
+
+                               s = state->conn->sessions;
+                               for (; s; s = s->next) {
+                                       if (s->smb2.session_id != uid) {
+                                               continue;
+                                       }
+
+                                       session = s;
+                                       break;
+                               }
+                       }
+
+                       if (session == NULL) {
+                               return NT_STATUS_INVALID_NETWORK_RESPONSE;
+                       }
+
+                       last_session = session;
+                       signing_key = &session->smb2.channel_signing_key;
+               }
+
+               if (opcode == SMB2_OP_SESSSETUP) {
+                       /*
+                        * We prefer the channel signing key, if it is
+                        * already there.
+                        *
+                        * If we do not have a channel signing key yet,
+                        * we try the main signing key, if it is not
+                        * the final response.
+                        */
+                       if (signing_key && signing_key->length == 0 &&
+                           !NT_STATUS_IS_OK(status)) {
+                               signing_key = &session->smb2.signing_key;
+                       }
+
+                       if (signing_key && signing_key->length == 0) {
+                               /*
+                                * If we do not have a session key to
+                                * verify the signature, we defer the
+                                * signing check to the caller.
+                                *
+                                * The caller gets NT_STATUS_OK, it
+                                * has to call
+                                * smb2cli_session_set_session_key()
+                                * or
+                                * smb2cli_session_set_channel_key()
+                                * which will check the signature
+                                * with the channel signing key.
+                                */
+                               signing_key = NULL;
+                       }
+               }
+
+               if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
+                       /*
+                        * if the server returns NT_STATUS_USER_SESSION_DELETED
+                        * the response is not signed and we should
+                        * propagate the NT_STATUS_USER_SESSION_DELETED
+                        * status to the caller.
+                        */
+                       signing_key = NULL;
+               }
+
+               if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
+                   NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
+                   NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
+                       /*
+                        * if the server returns
+                        * NT_STATUS_NETWORK_NAME_DELETED
+                        * NT_STATUS_FILE_CLOSED
+                        * NT_STATUS_INVALID_PARAMETER
+                        * the response might not be signed
+                        * as this happens before the signing checks.
+                        *
+                        * If server echos the signature (or all zeros)
+                        * we should report the status from the server
+                        * to the caller.
+                        */
+                       if (signing_key) {
+                               int cmp;
+
+                               cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
+                                            state->smb2.hdr+SMB2_HDR_SIGNATURE,
+                                            16);
+                               if (cmp == 0) {
+                                       state->smb2.signing_skipped = true;
+                                       signing_key = NULL;
+                               }
+                       }
+                       if (signing_key) {
+                               int cmp;
+                               static const uint8_t zeros[16];
+
+                               cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
+                                            zeros,
+                                            16);
+                               if (cmp == 0) {
+                                       state->smb2.signing_skipped = true;
+                                       signing_key = NULL;
+                               }
+                       }
+               }
+
+               if (signing_key) {
+                       status = smb2_signing_check_pdu(*signing_key,
+                                                       state->conn->protocol,
+                                                       &cur[1], 3);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               /*
+                                * If the signing check fails, we disconnect
+                                * the connection.
+                                */
+                               return status;
+                       }
+               }
+
                smbXcli_req_unset_pending(req);
 
                /*
                 * There might be more than one response
                 * we need to defer the notifications
                 */
-               if ((num_iov == 4) && (talloc_array_length(conn->pending) == 0)) {
+               if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
                        defer = false;
                }
 
@@ -2405,9 +3148,9 @@ static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
                }
 
                /* copy the related buffers */
-               state->smb2.recv_iov[0] = cur[0];
-               state->smb2.recv_iov[1] = cur[1];
-               state->smb2.recv_iov[2] = cur[2];
+               state->smb2.recv_iov[0] = cur[1];
+               state->smb2.recv_iov[1] = cur[2];
+               state->smb2.recv_iov[2] = cur[3];
 
                tevent_req_done(req);
        }
@@ -2437,6 +3180,10 @@ NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
                *piov = NULL;
        }
 
+       if (state->smb2.got_async) {
+               return STATUS_PENDING;
+       }
+
        if (tevent_req_is_nterror(req, &status)) {
                for (i=0; i < num_expected; i++) {
                        if (NT_STATUS_EQUAL(status, expected[i].status)) {
@@ -2481,6 +3228,15 @@ NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
                return status;
        }
 
+       if (state->smb2.signing_skipped) {
+               if (num_expected > 0) {
+                       return NT_STATUS_ACCESS_DENIED;
+               }
+               if (!NT_STATUS_IS_ERR(status)) {
+                       return NT_STATUS_ACCESS_DENIED;
+               }
+       }
+
        if (!found_size) {
                return NT_STATUS_INVALID_NETWORK_RESPONSE;
        }
@@ -2516,6 +3272,9 @@ static const struct {
 } smb2cli_prots[] = {
        {PROTOCOL_SMB2_02,      SMB2_DIALECT_REVISION_202},
        {PROTOCOL_SMB2_10,      SMB2_DIALECT_REVISION_210},
+       {PROTOCOL_SMB2_22,      SMB2_DIALECT_REVISION_222},
+       {PROTOCOL_SMB2_24,      SMB2_DIALECT_REVISION_224},
+       {PROTOCOL_SMB3_00,      SMB3_DIALECT_REVISION_300},
 };
 
 struct smbXcli_negprot_state {
@@ -3103,7 +3862,11 @@ static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_sta
        SSVAL(buf, 2, dialect_count);
        SSVAL(buf, 4, state->conn->smb2.client.security_mode);
        SSVAL(buf, 6, 0);       /* Reserved */
-       SSVAL(buf, 8, 0);       /* Capabilities */
+       if (state->max_protocol >= PROTOCOL_SMB2_22) {
+               SIVAL(buf, 8, state->conn->smb2.client.capabilities);
+       } else {
+               SIVAL(buf, 8, 0);       /* Capabilities */
+       }
        if (state->max_protocol >= PROTOCOL_SMB2_10) {
                NTSTATUS status;
                DATA_BLOB blob;
@@ -3123,7 +3886,7 @@ static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_sta
                                state->conn, SMB2_OP_NEGPROT,
                                0, 0, /* flags */
                                state->timeout_msec,
-                               0xFEFF, 0, 0, /* pid, tid, uid */
+                               0xFEFF, 0, NULL, /* pid, tid, session */
                                state->smb2.fixed, sizeof(state->smb2.fixed),
                                state->smb2.dyn, dialect_count*2);
 }
@@ -3195,10 +3958,8 @@ static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
 
                /*
                 * send a SMB2 negprot, in order to negotiate
-                * the SMB2 dialect. This needs to use the
-                * message id 1.
+                * the SMB2 dialect.
                 */
-               state->conn->smb2.mid = 1;
                subreq = smbXcli_negprot_smb2_subreq(state);
                if (tevent_req_nomem(subreq, req)) {
                        return;
@@ -3253,17 +4014,22 @@ static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
        struct tevent_req *subreq;
        struct smbXcli_req_state *substate;
        struct tevent_req *req;
-       struct smbXcli_negprot_state *state;
-       uint32_t protocol_magic = IVAL(inbuf, 4);
+       uint32_t protocol_magic;
+       size_t inbuf_len = smb_len_nbt(inbuf);
 
        if (num_pending != 1) {
                return NT_STATUS_INTERNAL_ERROR;
        }
 
+       if (inbuf_len < 4) {
+               return NT_STATUS_INVALID_NETWORK_RESPONSE;
+       }
+
        subreq = conn->pending[0];
        substate = tevent_req_data(subreq, struct smbXcli_req_state);
        req = tevent_req_callback_data(subreq, struct tevent_req);
-       state = tevent_req_data(req, struct smbXcli_negprot_state);
+
+       protocol_magic = IVAL(inbuf, 4);
 
        switch (protocol_magic) {
        case SMB_MAGIC:
@@ -3280,6 +4046,11 @@ static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
                        substate->smb1.recv_iov = NULL;
                }
 
+               /*
+                * we got an SMB2 answer, which consumed sequence number 0
+                * so we need to use 1 as the next one
+                */
+               conn->smb2.mid = 1;
                tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
                conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
                return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
@@ -3331,3 +4102,326 @@ NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
        TALLOC_FREE(frame);
        return status;
 }
+
+static int smbXcli_session_destructor(struct smbXcli_session *session)
+{
+       if (session->conn == NULL) {
+               return 0;
+       }
+
+       DLIST_REMOVE(session->conn->sessions, session);
+       return 0;
+}
+
+struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
+                                              struct smbXcli_conn *conn)
+{
+       struct smbXcli_session *session;
+
+       session = talloc_zero(mem_ctx, struct smbXcli_session);
+       if (session == NULL) {
+               return NULL;
+       }
+       talloc_set_destructor(session, smbXcli_session_destructor);
+
+       DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
+       session->conn = conn;
+
+       return session;
+}
+
+uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
+{
+       struct smbXcli_conn *conn = session->conn;
+       uint8_t security_mode = 0;
+
+       if (conn == NULL) {
+               return security_mode;
+       }
+
+       security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
+       if (conn->mandatory_signing) {
+               security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
+       }
+
+       return security_mode;
+}
+
+uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
+{
+       return session->smb2.session_id;
+}
+
+uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
+{
+       return session->smb2.session_flags;
+}
+
+NTSTATUS smb2cli_session_application_key(struct smbXcli_session *session,
+                                        TALLOC_CTX *mem_ctx,
+                                        DATA_BLOB *key)
+{
+       *key = data_blob_null;
+
+       if (session->smb2.application_key.length == 0) {
+               return NT_STATUS_NO_USER_SESSION_KEY;
+       }
+
+       *key = data_blob_dup_talloc(mem_ctx, session->smb2.application_key);
+       if (key->data == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       return NT_STATUS_OK;
+}
+
+void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
+                                     uint64_t session_id,
+                                     uint16_t session_flags)
+{
+       session->smb2.session_id = session_id;
+       session->smb2.session_flags = session_flags;
+}
+
+NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
+                                        const DATA_BLOB _session_key,
+                                        const struct iovec *recv_iov)
+{
+       struct smbXcli_conn *conn = session->conn;
+       uint16_t no_sign_flags;
+       uint8_t session_key[16];
+       NTSTATUS status;
+
+       if (conn == NULL) {
+               return NT_STATUS_INVALID_PARAMETER_MIX;
+       }
+
+       no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
+
+       if (session->smb2.session_flags & no_sign_flags) {
+               session->smb2.should_sign = false;
+               return NT_STATUS_OK;
+       }
+
+       if (session->smb2.signing_key.length != 0) {
+               return NT_STATUS_INVALID_PARAMETER_MIX;
+       }
+
+       ZERO_STRUCT(session_key);
+       memcpy(session_key, _session_key.data,
+              MIN(_session_key.length, sizeof(session_key)));
+
+       session->smb2.signing_key = data_blob_talloc(session,
+                                                    session_key,
+                                                    sizeof(session_key));
+       if (session->smb2.signing_key.data == NULL) {
+               ZERO_STRUCT(session_key);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       if (conn->protocol >= PROTOCOL_SMB2_24) {
+               const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
+               const DATA_BLOB context = data_blob_string_const_null("SmbSign");
+
+               smb2_key_derivation(session_key, sizeof(session_key),
+                                   label.data, label.length,
+                                   context.data, context.length,
+                                   session->smb2.signing_key.data);
+       }
+
+       session->smb2.encryption_key = data_blob_dup_talloc(session,
+                                               session->smb2.signing_key);
+       if (session->smb2.encryption_key.data == NULL) {
+               ZERO_STRUCT(session_key);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       if (conn->protocol >= PROTOCOL_SMB2_24) {
+               const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
+               const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
+
+               smb2_key_derivation(session_key, sizeof(session_key),
+                                   label.data, label.length,
+                                   context.data, context.length,
+                                   session->smb2.encryption_key.data);
+       }
+
+       session->smb2.decryption_key = data_blob_dup_talloc(session,
+                                               session->smb2.signing_key);
+       if (session->smb2.decryption_key.data == NULL) {
+               ZERO_STRUCT(session_key);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       if (conn->protocol >= PROTOCOL_SMB2_24) {
+               const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
+               const DATA_BLOB context = data_blob_string_const_null("ServerOut");
+
+               smb2_key_derivation(session_key, sizeof(session_key),
+                                   label.data, label.length,
+                                   context.data, context.length,
+                                   session->smb2.decryption_key.data);
+       }
+
+       session->smb2.application_key = data_blob_dup_talloc(session,
+                                               session->smb2.signing_key);
+       if (session->smb2.application_key.data == NULL) {
+               ZERO_STRUCT(session_key);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       if (conn->protocol >= PROTOCOL_SMB2_24) {
+               const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
+               const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
+
+               smb2_key_derivation(session_key, sizeof(session_key),
+                                   label.data, label.length,
+                                   context.data, context.length,
+                                   session->smb2.application_key.data);
+       }
+       ZERO_STRUCT(session_key);
+
+       session->smb2.channel_signing_key = data_blob_dup_talloc(session,
+                                               session->smb2.signing_key);
+       if (session->smb2.channel_signing_key.data == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       status = smb2_signing_check_pdu(session->smb2.channel_signing_key,
+                                       session->conn->protocol,
+                                       recv_iov, 3);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       session->smb2.should_sign = false;
+       session->smb2.should_encrypt = false;
+
+       if (conn->desire_signing) {
+               session->smb2.should_sign = true;
+       }
+
+       if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
+               session->smb2.should_sign = true;
+       }
+
+       generate_random_buffer((uint8_t *)&session->smb2.channel_nonce,
+                              sizeof(session->smb2.channel_nonce));
+       session->smb2.channel_next = 1;
+
+       return NT_STATUS_OK;
+}
+
+NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
+                                       struct smbXcli_session *session1,
+                                       struct smbXcli_conn *conn,
+                                       struct smbXcli_session **_session2)
+{
+       struct smbXcli_session *session2;
+
+       if (session1->smb2.signing_key.length == 0) {
+               return NT_STATUS_INVALID_PARAMETER_MIX;
+       }
+
+       if (session1->smb2.channel_next == 0) {
+               return NT_STATUS_INVALID_PARAMETER_MIX;
+       }
+
+       if (conn == NULL) {
+               return NT_STATUS_INVALID_PARAMETER_MIX;
+       }
+
+       session2 = talloc_zero(mem_ctx, struct smbXcli_session);
+       if (session2 == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       session2->smb2.session_id = session1->smb2.session_id;
+       session2->smb2.session_flags = session1->smb2.session_flags;
+
+       session2->smb2.signing_key = data_blob_dup_talloc(session2,
+                                               session1->smb2.signing_key);
+       if (session2->smb2.signing_key.data == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       session2->smb2.application_key = data_blob_dup_talloc(session2,
+                                               session1->smb2.application_key);
+       if (session2->smb2.application_key.data == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       session2->smb2.should_sign = session1->smb2.should_sign;
+       session2->smb2.should_encrypt = session1->smb2.should_encrypt;
+
+       session2->smb2.channel_nonce = session1->smb2.channel_nonce;
+       session2->smb2.channel_nonce += session1->smb2.channel_next;
+       session1->smb2.channel_next++;
+
+       session2->smb2.encryption_key = data_blob_dup_talloc(session2,
+                                               session1->smb2.encryption_key);
+       if (session2->smb2.encryption_key.data == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       session2->smb2.decryption_key = data_blob_dup_talloc(session2,
+                                               session1->smb2.decryption_key);
+       if (session2->smb2.decryption_key.data == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       talloc_set_destructor(session2, smbXcli_session_destructor);
+       DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
+       session2->conn = conn;
+
+       *_session2 = session2;
+       return NT_STATUS_OK;
+}
+
+NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
+                                        const DATA_BLOB _channel_key,
+                                        const struct iovec *recv_iov)
+{
+       struct smbXcli_conn *conn = session->conn;
+       uint8_t channel_key[16];
+       NTSTATUS status;
+
+       if (conn == NULL) {
+               return NT_STATUS_INVALID_PARAMETER_MIX;
+       }
+
+       if (session->smb2.channel_signing_key.length != 0) {
+               return NT_STATUS_INVALID_PARAMETER_MIX;
+       }
+
+       ZERO_STRUCT(channel_key);
+       memcpy(channel_key, _channel_key.data,
+              MIN(_channel_key.length, sizeof(channel_key)));
+
+       session->smb2.channel_signing_key = data_blob_talloc(session,
+                                               channel_key,
+                                               sizeof(channel_key));
+       if (session->smb2.channel_signing_key.data == NULL) {
+               ZERO_STRUCT(channel_key);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       if (conn->protocol >= PROTOCOL_SMB2_24) {
+               const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
+               const DATA_BLOB context = data_blob_string_const_null("SmbSign");
+
+               smb2_key_derivation(channel_key, sizeof(channel_key),
+                                   label.data, label.length,
+                                   context.data, context.length,
+                                   session->smb2.channel_signing_key.data);
+       }
+       ZERO_STRUCT(channel_key);
+
+       status = smb2_signing_check_pdu(session->smb2.channel_signing_key,
+                                       session->conn->protocol,
+                                       recv_iov, 3);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       return NT_STATUS_OK;
+}