smbXcli: call tevent_queue_stop() for the outgoing queue on disconnect
[gd/samba-autobuild/.git] / libcli / smb / smbXcli_base.c
index ddc1158a451bae942cfbc2a4d47f58a4df9aaabb..43fbbf4e4d20e78612c4bb3e9fe957d2d6205de2 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;
@@ -78,6 +84,7 @@ struct smbXcli_conn {
                        DATA_BLOB gss_blob;
                        uint8_t challenge[8];
                        const char *workgroup;
+                       const char *name;
                        int time_zone;
                        NTTIME system_time;
                } server;
@@ -94,6 +101,7 @@ struct smbXcli_conn {
        struct {
                struct {
                        uint16_t security_mode;
+                       struct GUID guid;
                } client;
 
                struct {
@@ -109,12 +117,31 @@ 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 signing_key;
+               DATA_BLOB session_key;
+               bool should_sign;
+               bool channel_setup;
        } smb2;
 };
 
 struct smbXcli_req_state {
        struct tevent_context *ev;
        struct smbXcli_conn *conn;
+       struct smbXcli_session *session; /* maybe NULL */
 
        uint8_t length_hdr[4];
 
@@ -142,6 +169,7 @@ struct smbXcli_req_state {
                struct iovec iov[1 + 3 + MAX_SMB_IOV];
                int iov_count;
 
+               bool one_way_seqnum;
                uint32_t seqnum;
                struct tevent_req **chained_requests;
 
@@ -162,6 +190,12 @@ struct smbXcli_req_state {
 
                /* always an array of 3 talloc elements */
                struct iovec *recv_iov;
+
+               uint16_t credit_charge;
+
+               bool signing_skipped;
+               bool notify_async;
+               bool got_async;
        } smb2;
 };
 
@@ -172,6 +206,11 @@ 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);
        }
@@ -183,7 +222,8 @@ struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
                                         int fd,
                                         const char *remote_name,
                                         enum smb_signing_setting signing_state,
-                                        uint32_t smb1_capabilities)
+                                        uint32_t smb1_capabilities,
+                                        struct GUID *client_guid)
 {
        struct smbXcli_conn *conn = NULL;
        void *ss = NULL;
@@ -196,12 +236,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;
@@ -269,11 +314,20 @@ struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
        if (conn->mandatory_signing) {
                conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
        }
+       if (client_guid) {
+               conn->smb2.client.guid = *client_guid;
+       }
+
+       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;
 }
@@ -284,7 +338,7 @@ bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
                return false;
        }
 
-       if (conn->fd == -1) {
+       if (conn->read_fd == -1) {
                return false;
        }
 
@@ -311,7 +365,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)
@@ -329,6 +383,75 @@ const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
        return conn->remote_name;
 }
 
+uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
+{
+       if (conn->protocol >= PROTOCOL_SMB2_02) {
+               /*
+                * TODO...
+                */
+               return 1;
+       }
+
+       return conn->smb1.server.max_mux;
+}
+
+NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
+{
+       if (conn->protocol >= PROTOCOL_SMB2_02) {
+               return conn->smb2.server.system_time;
+       }
+
+       return conn->smb1.server.system_time;
+}
+
+const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
+{
+       if (conn->protocol >= PROTOCOL_SMB2_02) {
+               return &conn->smb2.server.gss_blob;
+       }
+
+       return &conn->smb1.server.gss_blob;
+}
+
+const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
+{
+       if (conn->protocol >= PROTOCOL_SMB2_02) {
+               return &conn->smb2.server.guid;
+       }
+
+       return &conn->smb1.server.guid;
+}
+
+uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
+{
+       return conn->smb1.capabilities;
+}
+
+uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
+{
+       return conn->smb1.max_xmit;
+}
+
+uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
+{
+       return conn->smb1.server.session_key;
+}
+
+const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
+{
+       return conn->smb1.server.challenge;
+}
+
+uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
+{
+       return conn->smb1.server.security_mode;
+}
+
+int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
+{
+       return conn->smb1.server.time_zone;
+}
+
 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
                                   const DATA_BLOB user_session_key,
                                   const DATA_BLOB response)
@@ -381,38 +504,6 @@ static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
        return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
 }
 
-/**
- * Figure out if there is an andx command behind the current one
- * @param[in] buf      The smb buffer to look at
- * @param[in] ofs      The offset to the wct field that is followed by the cmd
- * @retval Is there a command following?
- */
-
-static bool smb1cli_have_andx_command(const uint8_t *buf,
-                                     uint16_t ofs,
-                                     uint8_t cmd)
-{
-       uint8_t wct;
-       size_t buflen = talloc_get_size(buf);
-
-       if (!smb1cli_is_andx_req(cmd)) {
-               return false;
-       }
-
-       if ((ofs == buflen-1) || (ofs == buflen)) {
-               return false;
-       }
-
-       wct = CVAL(buf, ofs);
-       if (wct < 2) {
-               /*
-                * Not enough space for the command and a following pointer
-                */
-               return false;
-       }
-       return (CVAL(buf, ofs+1) != 0xff);
-}
-
 /**
  * Is the SMB command able to hold an AND_X successor
  * @param[in] cmd      The SMB command in question
@@ -538,6 +629,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)
@@ -565,6 +680,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)) {
                /*
@@ -615,7 +731,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;
        }
@@ -625,10 +743,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);
+       }
+       if (conn->write_fd != -1) {
+               close(conn->write_fd);
        }
-       conn->fd = -1;
+       conn->read_fd = -1;
+       conn->write_fd = -1;
 
        /*
         * Cancel all pending requests. We do not do a for-loop walking
@@ -638,29 +762,64 @@ void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
        while (talloc_array_length(conn->pending) > 0) {
                struct tevent_req *req;
                struct smbXcli_req_state *state;
+               struct tevent_req **chain;
+               size_t num_chained;
+               size_t i;
 
                req = conn->pending[0];
                state = tevent_req_data(req, struct smbXcli_req_state);
 
-               /*
-                * We're dead. No point waiting for trans2
-                * replies.
-                */
-               state->smb1.mid = 0;
+               if (state->smb1.chained_requests == NULL) {
+                       /*
+                        * We're dead. No point waiting for trans2
+                        * replies.
+                        */
+                       state->smb1.mid = 0;
 
-               smbXcli_req_unset_pending(req);
+                       smbXcli_req_unset_pending(req);
+
+                       if (NT_STATUS_IS_OK(status)) {
+                               /* do not notify the callers */
+                               continue;
+                       }
 
-               if (NT_STATUS_IS_OK(status)) {
-                       /* do not notify the callers */
+                       /*
+                        * we need to defer the callback, because we may notify
+                        * more then one caller.
+                        */
+                       tevent_req_defer_callback(req, state->ev);
+                       tevent_req_nterror(req, status);
                        continue;
                }
 
-               /*
-                * we need to defer the callback, because we may notify more
-                * then one caller.
-                */
-               tevent_req_defer_callback(req, state->ev);
-               tevent_req_nterror(req, status);
+               chain = talloc_move(conn, &state->smb1.chained_requests);
+               num_chained = talloc_array_length(chain);
+
+               for (i=0; i<num_chained; i++) {
+                       req = chain[i];
+                       state = tevent_req_data(req, struct smbXcli_req_state);
+
+                       /*
+                        * We're dead. No point waiting for trans2
+                        * replies.
+                        */
+                       state->smb1.mid = 0;
+
+                       smbXcli_req_unset_pending(req);
+
+                       if (NT_STATUS_IS_OK(status)) {
+                               /* do not notify the callers */
+                               continue;
+                       }
+
+                       /*
+                        * we need to defer the callback, because we may notify
+                        * more than one caller.
+                        */
+                       tevent_req_defer_callback(req, state->ev);
+                       tevent_req_nterror(req, status);
+               }
+               TALLOC_FREE(chain);
        }
 }
 
@@ -785,6 +944,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,
@@ -849,7 +1064,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;
 
@@ -883,8 +1098,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) &&
@@ -899,8 +1117,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;
 
        /*
@@ -924,16 +1144,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;
 }
 
@@ -984,7 +1207,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;
@@ -1024,8 +1248,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;
        }
@@ -1703,9 +1929,6 @@ NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
        struct smbXcli_req_state *first_state =
                tevent_req_data(reqs[0],
                struct smbXcli_req_state);
-       struct smbXcli_req_state *last_state =
-               tevent_req_data(reqs[num_reqs-1],
-               struct smbXcli_req_state);
        struct smbXcli_req_state *state;
        size_t wct_offset;
        size_t chain_padding = 0;
@@ -1751,13 +1974,13 @@ NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
                iovlen += state->smb1.iov_count - 2;
        }
 
-       iov = talloc_zero_array(last_state, struct iovec, iovlen);
+       iov = talloc_zero_array(first_state, struct iovec, iovlen);
        if (iov == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
 
        first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
-               last_state, reqs, sizeof(*reqs) * num_reqs);
+               first_state, reqs, sizeof(*reqs) * num_reqs);
        if (first_state->smb1.chained_requests == NULL) {
                TALLOC_FREE(iov);
                return NT_STATUS_NO_MEMORY;
@@ -1842,19 +2065,13 @@ NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
                return NT_STATUS_INVALID_PARAMETER_MIX;
        }
 
-       status = smb1cli_req_writev_submit(reqs[0], last_state, iov, iovlen);
+       status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
        if (!NT_STATUS_IS_OK(status)) {
                TALLOC_FREE(iov);
                TALLOC_FREE(first_state->smb1.chained_requests);
                return status;
        }
 
-       for (i=0; i < (num_reqs - 1); i++) {
-               state = tevent_req_data(reqs[i], struct smbXcli_req_state);
-
-               state->smb1.seqnum = last_state->smb1.seqnum;
-       }
-
        return NT_STATUS_OK;
 }
 
@@ -1864,6 +2081,99 @@ bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
                || (talloc_array_length(conn->pending) != 0));
 }
 
+uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
+{
+       return conn->smb2.server.capabilities;
+}
+
+uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
+{
+       return conn->smb2.server.security_mode;
+}
+
+uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
+{
+       return conn->smb2.server.max_trans_size;
+}
+
+uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
+{
+       return conn->smb2.server.max_read_size;
+}
+
+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,
@@ -1873,7 +2183,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,
@@ -1882,6 +2192,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);
@@ -1891,6 +2202,11 @@ 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.recv_iov = talloc_zero_array(state, struct iovec, 3);
        if (state->smb2.recv_iov == NULL) {
@@ -1908,10 +2224,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);
@@ -1944,6 +2257,15 @@ struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
        return req;
 }
 
+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_writev_done(struct tevent_req *subreq);
 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
                                               TALLOC_CTX *tmp_mem,
@@ -1972,9 +2294,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;
+               bool should_sign = false;
 
                if (!tevent_req_is_in_progress(reqs[i])) {
                        return NT_STATUS_INTERNAL_ERROR;
@@ -1991,15 +2319,50 @@ NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
                        return NT_STATUS_REVISION_MISMATCH;
                }
 
-               if (state->conn->smb2.mid == UINT64_MAX) {
+               opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
+               if (opcode == SMB2_OP_CANCEL) {
+                       goto skip_credits;
+               }
+
+               avail = UINT64_MAX - state->conn->smb2.mid;
+               if (avail < 1) {
                        return NT_STATUS_CONNECTION_ABORTED;
                }
 
+               if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
+                       charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
+               } else {
+                       charge = 1;
+               }
+
+               charge = MAX(state->smb2.credit_charge, charge);
+
+               avail = MIN(avail, state->conn->smb2.cur_credits);
+               if (avail < charge) {
+                       return NT_STATUS_INTERNAL_ERROR;
+               }
+
+               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;
+               }
+
                mid = state->conn->smb2.mid;
-               state->conn->smb2.mid += 1;
+               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:
+               hdr_iov = num_iov;
                iov[num_iov].iov_base = state->smb2.hdr;
                iov[num_iov].iov_len  = sizeof(state->smb2.hdr);
                num_iov += 1;
@@ -2030,16 +2393,29 @@ NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
                }
                nbt_len += reqlen;
 
+               if (state->session) {
+                       should_sign = state->session->smb2.should_sign;
+                       if (state->session->smb2.channel_setup) {
+                               should_sign = true;
+                       }
+               }
+
+               if (should_sign) {
+                       NTSTATUS status;
+
+                       status = smb2_signing_sign_pdu(state->session->smb2.signing_key,
+                                                      &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;
@@ -2050,7 +2426,7 @@ 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;
        }
@@ -2058,6 +2434,15 @@ NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
        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,
@@ -2067,7 +2452,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,
@@ -2079,7 +2464,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;
@@ -2244,6 +2629,7 @@ 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;
 
        status = smb2cli_inbuf_parse_compound(inbuf, tmp_mem,
                                              &iov, &num_iov);
@@ -2259,6 +2645,19 @@ static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
                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;
+               bool should_sign = false;
+
+               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) {
@@ -2266,10 +2665,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;
@@ -2278,18 +2680,145 @@ 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;
                }
 
-               smbXcli_req_unset_pending(req);
+               session = state->session;
+               if (req_flags & SMB2_HDR_FLAG_CHAINED) {
+                       session = last_session;
+               }
+               last_session = session;
 
-               /*
+               if (session) {
+                       should_sign = session->smb2.should_sign;
+                       if (session->smb2.channel_setup) {
+                               should_sign = true;
+                       }
+               }
+
+               if (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.signing_key;
+               }
+
+               if ((opcode == SMB2_OP_SESSSETUP) &&
+                    NT_STATUS_IS_OK(status)) {
+                       /*
+                        * the caller has to check the signing
+                        * as only the caller knows the correct
+                        * session 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.
+                        */
+                       if (signing_key) {
+                               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, cur, 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
                 */
@@ -2343,6 +2872,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)) {
@@ -2387,6 +2920,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;
        }
@@ -2397,3 +2939,1024 @@ NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
 
        return status;
 }
+
+static const struct {
+       enum protocol_types proto;
+       const char *smb1_name;
+} smb1cli_prots[] = {
+       {PROTOCOL_CORE,         "PC NETWORK PROGRAM 1.0"},
+       {PROTOCOL_COREPLUS,     "MICROSOFT NETWORKS 1.03"},
+       {PROTOCOL_LANMAN1,      "MICROSOFT NETWORKS 3.0"},
+       {PROTOCOL_LANMAN1,      "LANMAN1.0"},
+       {PROTOCOL_LANMAN2,      "LM1.2X002"},
+       {PROTOCOL_LANMAN2,      "DOS LANMAN2.1"},
+       {PROTOCOL_LANMAN2,      "LANMAN2.1"},
+       {PROTOCOL_LANMAN2,      "Samba"},
+       {PROTOCOL_NT1,          "NT LANMAN 1.0"},
+       {PROTOCOL_NT1,          "NT LM 0.12"},
+       {PROTOCOL_SMB2_02,      "SMB 2.002"},
+       {PROTOCOL_SMB2_10,      "SMB 2.???"},
+};
+
+static const struct {
+       enum protocol_types proto;
+       uint16_t smb2_dialect;
+} smb2cli_prots[] = {
+       {PROTOCOL_SMB2_02,      SMB2_DIALECT_REVISION_202},
+       {PROTOCOL_SMB2_10,      SMB2_DIALECT_REVISION_210},
+       {PROTOCOL_SMB2_22,      SMB2_DIALECT_REVISION_222},
+};
+
+struct smbXcli_negprot_state {
+       struct smbXcli_conn *conn;
+       struct tevent_context *ev;
+       uint32_t timeout_msec;
+       enum protocol_types min_protocol;
+       enum protocol_types max_protocol;
+
+       struct {
+               uint8_t fixed[36];
+               uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
+       } smb2;
+};
+
+static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
+static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
+static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
+static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
+static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
+static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
+                                                 TALLOC_CTX *frame,
+                                                 uint8_t *inbuf);
+
+struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
+                                       struct tevent_context *ev,
+                                       struct smbXcli_conn *conn,
+                                       uint32_t timeout_msec,
+                                       enum protocol_types min_protocol,
+                                       enum protocol_types max_protocol)
+{
+       struct tevent_req *req, *subreq;
+       struct smbXcli_negprot_state *state;
+
+       req = tevent_req_create(mem_ctx, &state,
+                               struct smbXcli_negprot_state);
+       if (req == NULL) {
+               return NULL;
+       }
+       state->conn = conn;
+       state->ev = ev;
+       state->timeout_msec = timeout_msec;
+       state->min_protocol = min_protocol;
+       state->max_protocol = max_protocol;
+
+       if (min_protocol == PROTOCOL_NONE) {
+               tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
+               return tevent_req_post(req, ev);
+       }
+
+       if (max_protocol == PROTOCOL_NONE) {
+               tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
+               return tevent_req_post(req, ev);
+       }
+
+       if (min_protocol > max_protocol) {
+               tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
+               return tevent_req_post(req, ev);
+       }
+
+       if ((min_protocol < PROTOCOL_SMB2_02) &&
+           (max_protocol < PROTOCOL_SMB2_02)) {
+               /*
+                * SMB1 only...
+                */
+               conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
+
+               subreq = smbXcli_negprot_smb1_subreq(state);
+               if (tevent_req_nomem(subreq, req)) {
+                       return tevent_req_post(req, ev);
+               }
+               tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
+               return req;
+       }
+
+       if ((min_protocol >= PROTOCOL_SMB2_02) &&
+           (max_protocol >= PROTOCOL_SMB2_02)) {
+               /*
+                * SMB2 only...
+                */
+               conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
+
+               subreq = smbXcli_negprot_smb2_subreq(state);
+               if (tevent_req_nomem(subreq, req)) {
+                       return tevent_req_post(req, ev);
+               }
+               tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
+               return req;
+       }
+
+       /*
+        * We send an SMB1 negprot with the SMB2 dialects
+        * and expect a SMB1 or a SMB2 response.
+        *
+        * smbXcli_negprot_dispatch_incoming() will fix the
+        * callback to match protocol of the response.
+        */
+       conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
+
+       subreq = smbXcli_negprot_smb1_subreq(state);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
+       return req;
+}
+
+static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req =
+               tevent_req_callback_data(subreq,
+               struct tevent_req);
+       NTSTATUS status;
+
+       /*
+        * we just want the low level error
+        */
+       status = tevent_req_simple_recv_ntstatus(subreq);
+       TALLOC_FREE(subreq);
+       if (tevent_req_nterror(req, status)) {
+               return;
+       }
+
+       /* this should never happen */
+       tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
+}
+
+static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
+{
+       size_t i;
+       DATA_BLOB bytes = data_blob_null;
+       uint8_t flags;
+       uint16_t flags2;
+
+       /* setup the protocol strings */
+       for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
+               uint8_t c = 2;
+               bool ok;
+
+               if (smb1cli_prots[i].proto < state->min_protocol) {
+                       continue;
+               }
+
+               if (smb1cli_prots[i].proto > state->max_protocol) {
+                       continue;
+               }
+
+               ok = data_blob_append(state, &bytes, &c, sizeof(c));
+               if (!ok) {
+                       return NULL;
+               }
+
+               /*
+                * We now it is already ascii and
+                * we want NULL termination.
+                */
+               ok = data_blob_append(state, &bytes,
+                                     smb1cli_prots[i].smb1_name,
+                                     strlen(smb1cli_prots[i].smb1_name)+1);
+               if (!ok) {
+                       return NULL;
+               }
+       }
+
+       smb1cli_req_flags(state->max_protocol,
+                         state->conn->smb1.client.capabilities,
+                         SMBnegprot,
+                         0, 0, &flags,
+                         0, 0, &flags2);
+
+       return smb1cli_req_send(state, state->ev, state->conn,
+                               SMBnegprot,
+                               flags, ~flags,
+                               flags2, ~flags2,
+                               state->timeout_msec,
+                               0xFFFE, 0, 0, /* pid, tid, uid */
+                               0, NULL, /* wct, vwv */
+                               bytes.length, bytes.data);
+}
+
+static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req =
+               tevent_req_callback_data(subreq,
+               struct tevent_req);
+       struct smbXcli_negprot_state *state =
+               tevent_req_data(req,
+               struct smbXcli_negprot_state);
+       struct smbXcli_conn *conn = state->conn;
+       struct iovec *recv_iov = NULL;
+       uint8_t *inhdr;
+       uint8_t wct;
+       uint16_t *vwv;
+       uint32_t num_bytes;
+       uint8_t *bytes;
+       NTSTATUS status;
+       uint16_t protnum;
+       size_t i;
+       size_t num_prots = 0;
+       uint8_t flags;
+       uint32_t client_capabilities = conn->smb1.client.capabilities;
+       uint32_t both_capabilities;
+       uint32_t server_capabilities = 0;
+       uint32_t capabilities;
+       uint32_t client_max_xmit = conn->smb1.client.max_xmit;
+       uint32_t server_max_xmit = 0;
+       uint32_t max_xmit;
+       uint32_t server_max_mux = 0;
+       uint16_t server_security_mode = 0;
+       uint32_t server_session_key = 0;
+       bool server_readbraw = false;
+       bool server_writebraw = false;
+       bool server_lockread = false;
+       bool server_writeunlock = false;
+       struct GUID server_guid = GUID_zero();
+       DATA_BLOB server_gss_blob = data_blob_null;
+       uint8_t server_challenge[8];
+       char *server_workgroup = NULL;
+       char *server_name = NULL;
+       int server_time_zone = 0;
+       NTTIME server_system_time = 0;
+       static const struct smb1cli_req_expected_response expected[] = {
+       {
+               .status = NT_STATUS_OK,
+               .wct = 0x11, /* NT1 */
+       },
+       {
+               .status = NT_STATUS_OK,
+               .wct = 0x0D, /* LM */
+       },
+       {
+               .status = NT_STATUS_OK,
+               .wct = 0x01, /* CORE */
+       }
+       };
+
+       ZERO_STRUCT(server_challenge);
+
+       status = smb1cli_req_recv(subreq, state,
+                                 &recv_iov,
+                                 &inhdr,
+                                 &wct,
+                                 &vwv,
+                                 NULL, /* pvwv_offset */
+                                 &num_bytes,
+                                 &bytes,
+                                 NULL, /* pbytes_offset */
+                                 NULL, /* pinbuf */
+                                 expected, ARRAY_SIZE(expected));
+       TALLOC_FREE(subreq);
+       if (tevent_req_nterror(req, status)) {
+               return;
+       }
+
+       flags = CVAL(inhdr, HDR_FLG);
+
+       protnum = SVAL(vwv, 0);
+
+       for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
+               if (smb1cli_prots[i].proto < state->min_protocol) {
+                       continue;
+               }
+
+               if (smb1cli_prots[i].proto > state->max_protocol) {
+                       continue;
+               }
+
+               if (protnum != num_prots) {
+                       num_prots++;
+                       continue;
+               }
+
+               conn->protocol = smb1cli_prots[i].proto;
+               break;
+       }
+
+       if (conn->protocol == PROTOCOL_NONE) {
+               tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+               return;
+       }
+
+       if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
+               DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
+                        "and the selected protocol level doesn't support it.\n"));
+               tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
+               return;
+       }
+
+       if (flags & FLAG_SUPPORT_LOCKREAD) {
+               server_lockread = true;
+               server_writeunlock = true;
+       }
+
+       if (conn->protocol >= PROTOCOL_NT1) {
+               const char *client_signing = NULL;
+               bool server_mandatory = false;
+               bool server_allowed = false;
+               const char *server_signing = NULL;
+               bool ok;
+               uint8_t key_len;
+
+               if (wct != 0x11) {
+                       tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+                       return;
+               }
+
+               /* NT protocol */
+               server_security_mode = CVAL(vwv + 1, 0);
+               server_max_mux = SVAL(vwv + 1, 1);
+               server_max_xmit = IVAL(vwv + 3, 1);
+               server_session_key = IVAL(vwv + 7, 1);
+               server_time_zone = SVALS(vwv + 15, 1);
+               server_time_zone *= 60;
+               /* this time arrives in real GMT */
+               server_system_time = BVAL(vwv + 11, 1);
+               server_capabilities = IVAL(vwv + 9, 1);
+
+               key_len = CVAL(vwv + 16, 1);
+
+               if (server_capabilities & CAP_RAW_MODE) {
+                       server_readbraw = true;
+                       server_writebraw = true;
+               }
+               if (server_capabilities & CAP_LOCK_AND_READ) {
+                       server_lockread = true;
+               }
+
+               if (server_capabilities & CAP_EXTENDED_SECURITY) {
+                       DATA_BLOB blob1, blob2;
+
+                       if (num_bytes < 16) {
+                               tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+                               return;
+                       }
+
+                       blob1 = data_blob_const(bytes, 16);
+                       status = GUID_from_data_blob(&blob1, &server_guid);
+                       if (tevent_req_nterror(req, status)) {
+                               return;
+                       }
+
+                       blob1 = data_blob_const(bytes+16, num_bytes-16);
+                       blob2 = data_blob_dup_talloc(state, blob1);
+                       if (blob1.length > 0 &&
+                           tevent_req_nomem(blob2.data, req)) {
+                               return;
+                       }
+                       server_gss_blob = blob2;
+               } else {
+                       DATA_BLOB blob1, blob2;
+
+                       if (num_bytes < key_len) {
+                               tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+                               return;
+                       }
+
+                       if (key_len != 0 && key_len != 8) {
+                               tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+                               return;
+                       }
+
+                       if (key_len == 8) {
+                               memcpy(server_challenge, bytes, 8);
+                       }
+
+                       blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
+                       blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
+                       if (blob1.length > 0) {
+                               size_t len;
+
+                               len = utf16_len_n(blob1.data,
+                                                 blob1.length);
+                               blob1.length = len;
+
+                               ok = convert_string_talloc(state,
+                                                          CH_UTF16LE,
+                                                          CH_UNIX,
+                                                          blob1.data,
+                                                          blob1.length,
+                                                          &server_workgroup,
+                                                          &len);
+                               if (!ok) {
+                                       status = map_nt_error_from_unix_common(errno);
+                                       tevent_req_nterror(req, status);
+                                       return;
+                               }
+                       }
+
+                       blob2.data += blob1.length;
+                       blob2.length -= blob1.length;
+                       if (blob2.length > 0) {
+                               size_t len;
+
+                               len = utf16_len_n(blob1.data,
+                                                 blob1.length);
+                               blob1.length = len;
+
+                               ok = convert_string_talloc(state,
+                                                          CH_UTF16LE,
+                                                          CH_UNIX,
+                                                          blob2.data,
+                                                          blob2.length,
+                                                          &server_name,
+                                                          &len);
+                               if (!ok) {
+                                       status = map_nt_error_from_unix_common(errno);
+                                       tevent_req_nterror(req, status);
+                                       return;
+                               }
+                       }
+               }
+
+               client_signing = "disabled";
+               if (conn->allow_signing) {
+                       client_signing = "allowed";
+               }
+               if (conn->mandatory_signing) {
+                       client_signing = "required";
+               }
+
+               server_signing = "not supported";
+               if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
+                       server_signing = "supported";
+                       server_allowed = true;
+               }
+               if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
+                       server_signing = "required";
+                       server_mandatory = true;
+               }
+
+               ok = smb_signing_set_negotiated(conn->smb1.signing,
+                                               server_allowed,
+                                               server_mandatory);
+               if (!ok) {
+                       DEBUG(1,("cli_negprot: SMB signing is required, "
+                                "but client[%s] and server[%s] mismatch\n",
+                                client_signing, server_signing));
+                       tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
+                       return;
+               }
+
+       } else if (conn->protocol >= PROTOCOL_LANMAN1) {
+               DATA_BLOB blob1;
+               uint8_t key_len;
+               time_t t;
+
+               if (wct != 0x0D) {
+                       tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+                       return;
+               }
+
+               server_security_mode = SVAL(vwv + 1, 0);
+               server_max_xmit = SVAL(vwv + 2, 0);
+               server_max_mux = SVAL(vwv + 3, 0);
+               server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
+               server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
+               server_session_key = IVAL(vwv + 6, 0);
+               server_time_zone = SVALS(vwv + 10, 0);
+               server_time_zone *= 60;
+               /* this time is converted to GMT by make_unix_date */
+               t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
+               unix_to_nt_time(&server_system_time, t);
+               key_len = SVAL(vwv + 11, 0);
+
+               if (num_bytes < key_len) {
+                       tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+                       return;
+               }
+
+               if (key_len != 0 && key_len != 8) {
+                       tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+                       return;
+               }
+
+               if (key_len == 8) {
+                       memcpy(server_challenge, bytes, 8);
+               }
+
+               blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
+               if (blob1.length > 0) {
+                       size_t len;
+                       bool ok;
+
+                       len = utf16_len_n(blob1.data,
+                                         blob1.length);
+                       blob1.length = len;
+
+                       ok = convert_string_talloc(state,
+                                                  CH_DOS,
+                                                  CH_UNIX,
+                                                  blob1.data,
+                                                  blob1.length,
+                                                  &server_workgroup,
+                                                  &len);
+                       if (!ok) {
+                               status = map_nt_error_from_unix_common(errno);
+                               tevent_req_nterror(req, status);
+                               return;
+                       }
+               }
+
+       } else {
+               /* the old core protocol */
+               server_time_zone = get_time_zone(time(NULL));
+               server_max_xmit = 1024;
+               server_max_mux = 1;
+       }
+
+       if (server_max_xmit < 1024) {
+               tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+               return;
+       }
+
+       if (server_max_mux < 1) {
+               tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+               return;
+       }
+
+       /*
+        * Now calculate the negotiated capabilities
+        * based on the mask for:
+        * - client only flags
+        * - flags used in both directions
+        * - server only flags
+        */
+       both_capabilities = client_capabilities & server_capabilities;
+       capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
+       capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
+       capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
+
+       max_xmit = MIN(client_max_xmit, server_max_xmit);
+
+       conn->smb1.server.capabilities = server_capabilities;
+       conn->smb1.capabilities = capabilities;
+
+       conn->smb1.server.max_xmit = server_max_xmit;
+       conn->smb1.max_xmit = max_xmit;
+
+       conn->smb1.server.max_mux = server_max_mux;
+
+       conn->smb1.server.security_mode = server_security_mode;
+
+       conn->smb1.server.readbraw = server_readbraw;
+       conn->smb1.server.writebraw = server_writebraw;
+       conn->smb1.server.lockread = server_lockread;
+       conn->smb1.server.writeunlock = server_writeunlock;
+
+       conn->smb1.server.session_key = server_session_key;
+
+       talloc_steal(conn, server_gss_blob.data);
+       conn->smb1.server.gss_blob = server_gss_blob;
+       conn->smb1.server.guid = server_guid;
+       memcpy(conn->smb1.server.challenge, server_challenge, 8);
+       conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
+       conn->smb1.server.name = talloc_move(conn, &server_name);
+
+       conn->smb1.server.time_zone = server_time_zone;
+       conn->smb1.server.system_time = server_system_time;
+
+       tevent_req_done(req);
+}
+
+static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
+{
+       size_t i;
+       uint8_t *buf;
+       uint16_t dialect_count = 0;
+
+       buf = state->smb2.dyn;
+       for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
+               if (smb2cli_prots[i].proto < state->min_protocol) {
+                       continue;
+               }
+
+               if (smb2cli_prots[i].proto > state->max_protocol) {
+                       continue;
+               }
+
+               SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
+               dialect_count++;
+       }
+
+       buf = state->smb2.fixed;
+       SSVAL(buf, 0, 36);
+       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_10) {
+               NTSTATUS status;
+               DATA_BLOB blob;
+
+               status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
+                                         state, &blob);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return NULL;
+               }
+               memcpy(buf+12, blob.data, 16); /* ClientGuid */
+       } else {
+               memset(buf+12, 0, 16);  /* ClientGuid */
+       }
+       SBVAL(buf, 28, 0);      /* ClientStartTime */
+
+       return smb2cli_req_send(state, state->ev,
+                               state->conn, SMB2_OP_NEGPROT,
+                               0, 0, /* flags */
+                               state->timeout_msec,
+                               0xFEFF, 0, NULL, /* pid, tid, session */
+                               state->smb2.fixed, sizeof(state->smb2.fixed),
+                               state->smb2.dyn, dialect_count*2);
+}
+
+static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req =
+               tevent_req_callback_data(subreq,
+               struct tevent_req);
+       struct smbXcli_negprot_state *state =
+               tevent_req_data(req,
+               struct smbXcli_negprot_state);
+       struct smbXcli_conn *conn = state->conn;
+       size_t security_offset, security_length;
+       DATA_BLOB blob;
+       NTSTATUS status;
+       struct iovec *iov;
+       uint8_t *body;
+       size_t i;
+       uint16_t dialect_revision;
+       static const struct smb2cli_req_expected_response expected[] = {
+       {
+               .status = NT_STATUS_OK,
+               .body_size = 0x41
+       }
+       };
+
+       status = smb2cli_req_recv(subreq, state, &iov,
+                                 expected, ARRAY_SIZE(expected));
+       TALLOC_FREE(subreq);
+       if (tevent_req_nterror(req, status)) {
+               return;
+       }
+
+       body = (uint8_t *)iov[1].iov_base;
+
+       dialect_revision = SVAL(body, 4);
+
+       for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
+               if (smb2cli_prots[i].proto < state->min_protocol) {
+                       continue;
+               }
+
+               if (smb2cli_prots[i].proto > state->max_protocol) {
+                       continue;
+               }
+
+               if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
+                       continue;
+               }
+
+               conn->protocol = smb2cli_prots[i].proto;
+               break;
+       }
+
+       if (conn->protocol == PROTOCOL_NONE) {
+               if (state->min_protocol >= PROTOCOL_SMB2_02) {
+                       tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+                       return;
+               }
+
+               if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
+                       tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+                       return;
+               }
+
+               /* make sure we do not loop forever */
+               state->min_protocol = PROTOCOL_SMB2_02;
+
+               /*
+                * send a SMB2 negprot, in order to negotiate
+                * the SMB2 dialect. This needs to use the
+                * message id 1.
+                */
+               state->conn->smb2.mid = 1;
+               subreq = smbXcli_negprot_smb2_subreq(state);
+               if (tevent_req_nomem(subreq, req)) {
+                       return;
+               }
+               tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
+               return;
+       }
+
+       conn->smb2.server.security_mode = SVAL(body, 2);
+
+       blob = data_blob_const(body + 8, 16);
+       status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
+       if (tevent_req_nterror(req, status)) {
+               return;
+       }
+
+       conn->smb2.server.capabilities  = IVAL(body, 24);
+       conn->smb2.server.max_trans_size= IVAL(body, 28);
+       conn->smb2.server.max_read_size = IVAL(body, 32);
+       conn->smb2.server.max_write_size= IVAL(body, 36);
+       conn->smb2.server.system_time   = BVAL(body, 40);
+       conn->smb2.server.start_time    = BVAL(body, 48);
+
+       security_offset = SVAL(body, 56);
+       security_length = SVAL(body, 58);
+
+       if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
+               tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+               return;
+       }
+
+       if (security_length > iov[2].iov_len) {
+               tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+               return;
+       }
+
+       conn->smb2.server.gss_blob = data_blob_talloc(conn,
+                                               iov[2].iov_base,
+                                               security_length);
+       if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
+               return;
+       }
+
+       tevent_req_done(req);
+}
+
+static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
+                                                 TALLOC_CTX *tmp_mem,
+                                                 uint8_t *inbuf)
+{
+       size_t num_pending = talloc_array_length(conn->pending);
+       struct tevent_req *subreq;
+       struct smbXcli_req_state *substate;
+       struct tevent_req *req;
+       uint32_t protocol_magic = IVAL(inbuf, 4);
+
+       if (num_pending != 1) {
+               return NT_STATUS_INTERNAL_ERROR;
+       }
+
+       subreq = conn->pending[0];
+       substate = tevent_req_data(subreq, struct smbXcli_req_state);
+       req = tevent_req_callback_data(subreq, struct tevent_req);
+
+       switch (protocol_magic) {
+       case SMB_MAGIC:
+               tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
+               conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
+               return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
+
+       case SMB2_MAGIC:
+               if (substate->smb2.recv_iov == NULL) {
+                       /*
+                        * For the SMB1 negprot we have move it.
+                        */
+                       substate->smb2.recv_iov = substate->smb1.recv_iov;
+                       substate->smb1.recv_iov = NULL;
+               }
+
+               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);
+       }
+
+       DEBUG(10, ("Got non-SMB PDU\n"));
+       return NT_STATUS_INVALID_NETWORK_RESPONSE;
+}
+
+NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
+{
+       return tevent_req_simple_recv_ntstatus(req);
+}
+
+NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
+                        uint32_t timeout_msec,
+                        enum protocol_types min_protocol,
+                        enum protocol_types max_protocol)
+{
+       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_negprot_send(frame, ev, conn, timeout_msec,
+                                  min_protocol, max_protocol);
+       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_negprot_recv(req);
+ fail:
+       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;
+}
+
+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_update_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;
+       DATA_BLOB signing_key;
+       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) {
+               signing_key = session->smb2.signing_key;
+       } else {
+               signing_key = session_key;
+       }
+       if (session->smb2.channel_setup) {
+               signing_key = session_key;
+       }
+
+       status = smb2_signing_check_pdu(signing_key, recv_iov, 3);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       if (!session->smb2.channel_setup) {
+               session->smb2.session_key = data_blob_dup_talloc(session,
+                                                                session_key);
+               if (session->smb2.session_key.data == NULL) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+       }
+
+       if (session->smb2.channel_setup) {
+               data_blob_free(&session->smb2.signing_key);
+               session->smb2.channel_setup = false;
+       }
+
+       if (session->smb2.signing_key.length > 0) {
+               return NT_STATUS_OK;
+       }
+
+       session->smb2.signing_key = data_blob_dup_talloc(session, signing_key);
+       if (session->smb2.signing_key.data == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       session->smb2.should_sign = 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;
+       }
+
+       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;
+       uint16_t no_sign_flags;
+
+       no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
+
+       if (session1->smb2.session_flags & no_sign_flags) {
+               return NT_STATUS_INVALID_PARAMETER_MIX;
+       }
+
+       if (session1->smb2.session_key.length == 0) {
+               return NT_STATUS_INVALID_PARAMETER_MIX;
+       }
+
+       if (session1->smb2.signing_key.length == 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.session_key = data_blob_dup_talloc(session2,
+                                               session1->smb2.session_key);
+       if (session2->smb2.session_key.data == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       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.should_sign = session1->smb2.should_sign;
+       session2->smb2.channel_setup = true;
+
+       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;
+}