libcli/smb: add smb2cli_tcon_is_encryption_on()
[mat/samba.git] / libcli / smb / smbXcli_base.c
index a9adcbb0b70015848c8e84792cbf94356faaefe1..9e5891afe62f8a253f0e53b714e73c1835867c89 100644 (file)
@@ -157,6 +157,13 @@ struct smbXcli_session {
        struct {
                DATA_BLOB signing_key;
        } smb2_channel;
+
+       /*
+        * this should be a short term hack
+        * until the upper layers have implemented
+        * re-authentication.
+        */
+       bool disconnect_expired;
 };
 
 struct smbXcli_tcon {
@@ -239,6 +246,11 @@ struct smbXcli_req_state {
                 */
                struct iovec *recv_iov;
 
+               /*
+                * the expected max for the response dyn_len
+                */
+               uint32_t max_dyn_len;
+
                uint16_t credit_charge;
 
                bool should_sign;
@@ -554,7 +566,7 @@ NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
                status = NT_STATUS_INVALID_PARAMETER_MIX;
                goto fail;
        }
-       ev = tevent_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                goto fail;
        }
@@ -583,6 +595,23 @@ uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
        return conn->smb1.max_xmit;
 }
 
+bool smb1cli_conn_req_possible(struct smbXcli_conn *conn)
+{
+       size_t pending;
+       uint16_t possible = conn->smb1.server.max_mux;
+
+       pending = tevent_queue_length(conn->outgoing);
+       if (pending >= possible) {
+               return false;
+       }
+       pending += talloc_array_length(conn->pending);
+       if (pending >= possible) {
+               return false;
+       }
+
+       return true;
+}
+
 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
 {
        return conn->smb1.server.session_key;
@@ -708,6 +737,14 @@ static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
        size_t num_pending = talloc_array_length(conn->pending);
        uint16_t result;
 
+       if (conn->protocol == PROTOCOL_NONE) {
+               /*
+                * This is what windows sends on the SMB1 Negprot request
+                * and some vendors reuse the SMB1 MID as SMB2 sequence number.
+                */
+               return 0;
+       }
+
        while (true) {
                size_t i;
 
@@ -1555,8 +1592,7 @@ static void smbXcli_conn_received(struct tevent_req *subreq)
        if (subreq != conn->read_smb_req) {
                DEBUG(1, ("Internal error: cli_smb_received called with "
                          "unexpected subreq\n"));
-               status = NT_STATUS_INTERNAL_ERROR;
-               smbXcli_conn_disconnect(conn, status);
+               smbXcli_conn_disconnect(conn, NT_STATUS_INTERNAL_ERROR);
                TALLOC_FREE(frame);
                return;
        }
@@ -1580,7 +1616,9 @@ static void smbXcli_conn_received(struct tevent_req *subreq)
                 * tevent_req_done().
                 */
                return;
-       } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
+       }
+
+       if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
                /*
                 * We got an error, so notify all pending requests
                 */
@@ -1611,7 +1649,7 @@ static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
        NTSTATUS status;
        size_t min_size = MIN_SMB_SIZE;
 
-       buflen = smb_len_nbt(buf);
+       buflen = smb_len_tcp(buf);
        taken = 0;
 
        hdr = buf + NBT_HDR_SIZE;
@@ -1838,7 +1876,7 @@ static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
        uint16_t mid;
        bool oplock_break;
        uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
-       size_t len = smb_len_nbt(inbuf);
+       size_t len = smb_len_tcp(inbuf);
        struct iovec *iov = NULL;
        int num_iov = 0;
        struct tevent_req **chain = NULL;
@@ -1970,6 +2008,17 @@ static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
        cmd = CVAL(inhdr, HDR_COM);
        status = smb1cli_pull_raw_error(inhdr);
 
+       if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
+           (state->session != NULL) && state->session->disconnect_expired)
+       {
+               /*
+                * this should be a short term hack
+                * until the upper layers have implemented
+                * re-authentication.
+                */
+               return status;
+       }
+
        if (state->smb1.chained_requests == NULL) {
                if (num_iov != 3) {
                        return NT_STATUS_INVALID_NETWORK_RESPONSE;
@@ -2412,6 +2461,28 @@ bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
                || (talloc_array_length(conn->pending) != 0));
 }
 
+bool smb2cli_conn_req_possible(struct smbXcli_conn *conn, uint32_t *max_dyn_len)
+{
+       uint16_t credits = 1;
+
+       if (conn->smb2.cur_credits == 0) {
+               if (max_dyn_len != NULL) {
+                       *max_dyn_len = 0;
+               }
+               return false;
+       }
+
+       if (conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
+               credits = conn->smb2.cur_credits;
+       }
+
+       if (max_dyn_len != NULL) {
+               *max_dyn_len = credits * 65536;
+       }
+
+       return true;
+}
+
 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
 {
        return conn->smb2.server.capabilities;
@@ -2471,7 +2542,7 @@ static bool smb2cli_req_cancel(struct tevent_req *req)
                                    0, /* timeout */
                                    tcon, session,
                                    fixed, fixed_len,
-                                   NULL, 0);
+                                   NULL, 0, 0);
        if (subreq == NULL) {
                return false;
        }
@@ -2520,7 +2591,8 @@ struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
                                      const uint8_t *fixed,
                                      uint16_t fixed_len,
                                      const uint8_t *dyn,
-                                     uint32_t dyn_len)
+                                     uint32_t dyn_len,
+                                     uint32_t max_dyn_len)
 {
        struct tevent_req *req;
        struct smbXcli_req_state *state;
@@ -2593,6 +2665,7 @@ struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
        state->smb2.fixed_len = fixed_len;
        state->smb2.dyn = dyn;
        state->smb2.dyn_len = dyn_len;
+       state->smb2.max_dyn_len = max_dyn_len;
 
        if (state->smb2.should_encrypt) {
                SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
@@ -2769,7 +2842,12 @@ NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
                }
 
                if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
-                       charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
+                       uint32_t max_dyn_len = 1;
+
+                       max_dyn_len = MAX(max_dyn_len, state->smb2.dyn_len);
+                       max_dyn_len = MAX(max_dyn_len, state->smb2.max_dyn_len);
+
+                       charge = (max_dyn_len - 1)/ 65536 + 1;
                } else {
                        charge = 1;
                }
@@ -2953,7 +3031,8 @@ struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
                                    const uint8_t *fixed,
                                    uint16_t fixed_len,
                                    const uint8_t *dyn,
-                                   uint32_t dyn_len)
+                                   uint32_t dyn_len,
+                                   uint32_t max_dyn_len)
 {
        struct tevent_req *req;
        NTSTATUS status;
@@ -2962,7 +3041,9 @@ struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
                                 additional_flags, clear_flags,
                                 timeout_msec,
                                 tcon, session,
-                                fixed, fixed_len, dyn, dyn_len);
+                                fixed, fixed_len,
+                                dyn, dyn_len,
+                                max_dyn_len);
        if (req == NULL) {
                return NULL;
        }
@@ -3041,6 +3122,7 @@ static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
                        struct smbXcli_session *s;
                        uint64_t uid;
                        struct iovec tf_iov[2];
+                       size_t enc_len;
                        NTSTATUS status;
 
                        if (len < SMB2_TF_HDR_SIZE) {
@@ -3053,9 +3135,16 @@ static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
                        taken += tf_len;
 
                        hdr = first_hdr + taken;
-                       len = IVAL(tf, SMB2_TF_MSG_SIZE);
+                       enc_len = IVAL(tf, SMB2_TF_MSG_SIZE);
                        uid = BVAL(tf, SMB2_TF_SESSION_ID);
 
+                       if (len < SMB2_TF_HDR_SIZE + enc_len) {
+                               DEBUG(10, ("%d bytes left, expected at least %d\n",
+                                          (int)len,
+                                          (int)(SMB2_TF_HDR_SIZE + enc_len)));
+                               goto inval;
+                       }
+
                        s = conn->sessions;
                        for (; s; s = s->next) {
                                if (s->smb2->session_id != uid) {
@@ -3073,7 +3162,7 @@ static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
                        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;
+                       tf_iov[1].iov_len = enc_len;
 
                        status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
                                                          conn->protocol,
@@ -3083,7 +3172,8 @@ static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
                                return status;
                        }
 
-                       verified_buflen = taken + len;
+                       verified_buflen = taken + enc_len;
+                       len = enc_len;
                }
 
                /*
@@ -3433,6 +3523,17 @@ static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
                        }
                }
 
+               if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
+                   (session != NULL) && session->disconnect_expired)
+               {
+                       /*
+                        * this should be a short term hack
+                        * until the upper layers have implemented
+                        * re-authentication.
+                        */
+                       return status;
+               }
+
                smbXcli_req_unset_pending(req);
 
                /*
@@ -3584,6 +3685,7 @@ static const struct {
        {PROTOCOL_SMB2_22,      SMB2_DIALECT_REVISION_222},
        {PROTOCOL_SMB2_24,      SMB2_DIALECT_REVISION_224},
        {PROTOCOL_SMB3_00,      SMB3_DIALECT_REVISION_300},
+       {PROTOCOL_SMB3_02,      SMB3_DIALECT_REVISION_302},
 };
 
 struct smbXcli_negprot_state {
@@ -4008,6 +4110,15 @@ static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
                if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
                        server_signing = "supported";
                        server_allowed = true;
+               } else if (conn->mandatory_signing) {
+                       /*
+                        * We have mandatory signing as client
+                        * lets assume the server will look at our
+                        * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
+                        * flag in the session setup
+                        */
+                       server_signing = "not announced";
+                       server_allowed = true;
                }
                if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
                        server_signing = "required";
@@ -4197,7 +4308,8 @@ static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_sta
                                state->timeout_msec,
                                NULL, NULL, /* tcon, session */
                                state->smb2.fixed, sizeof(state->smb2.fixed),
-                               state->smb2.dyn, dialect_count*2);
+                               state->smb2.dyn, dialect_count*2,
+                               UINT16_MAX); /* max_dyn_len */
 }
 
 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
@@ -4357,9 +4469,14 @@ static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
 
                /*
                 * we got an SMB2 answer, which consumed sequence number 0
-                * so we need to use 1 as the next one
+                * so we need to use 1 as the next one.
+                *
+                * we also need to set the current credits to 0
+                * as we consumed the initial one. The SMB2 answer
+                * hopefully grant us a new credit.
                 */
                conn->smb2.mid = 1;
+               conn->smb2.cur_credits = 0;
                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);
@@ -4392,7 +4509,7 @@ NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
                status = NT_STATUS_INVALID_PARAMETER_MIX;
                goto fail;
        }
-       ev = tevent_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                goto fail;
        }
@@ -4444,6 +4561,33 @@ struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
        return session;
 }
 
+struct smbXcli_session *smbXcli_session_copy(TALLOC_CTX *mem_ctx,
+                                               struct smbXcli_session *src)
+{
+       struct smbXcli_session *session;
+
+       session = talloc_zero(mem_ctx, struct smbXcli_session);
+       if (session == NULL) {
+               return NULL;
+       }
+       session->smb2 = talloc_zero(session, struct smb2cli_session);
+       if (session->smb2 == NULL) {
+               talloc_free(session);
+               return NULL;
+       }
+
+       session->conn = src->conn;
+       *session->smb2 = *src->smb2;
+       session->smb2_channel = src->smb2_channel;
+       session->disconnect_expired = src->disconnect_expired;
+
+       DLIST_ADD_END(src->conn->sessions, session, struct smbXcli_session *);
+       talloc_set_destructor(session, smbXcli_session_destructor);
+
+       return session;
+}
+
+
 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
                                         TALLOC_CTX *mem_ctx,
                                         DATA_BLOB *key)
@@ -4474,6 +4618,11 @@ NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
        return NT_STATUS_OK;
 }
 
+void smbXcli_session_set_disconnect_expired(struct smbXcli_session *session)
+{
+       session->disconnect_expired = true;
+}
+
 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
 {
        return session->smb1.session_id;
@@ -4593,12 +4742,18 @@ NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
        struct smbXcli_conn *conn = session->conn;
        uint16_t no_sign_flags;
        uint8_t session_key[16];
+       bool check_signature = true;
+       uint32_t hdr_flags;
        NTSTATUS status;
 
        if (conn == NULL) {
                return NT_STATUS_INVALID_PARAMETER_MIX;
        }
 
+       if (recv_iov[0].iov_len != SMB2_HDR_BODY) {
+               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) {
@@ -4690,11 +4845,30 @@ NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
                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;
+       check_signature = conn->mandatory_signing;
+
+       hdr_flags = IVAL(recv_iov[0].iov_base, SMB2_HDR_FLAGS);
+       if (hdr_flags & SMB2_HDR_FLAG_SIGNED) {
+               /*
+                * Sadly some vendors don't sign the
+                * final SMB2 session setup response
+                *
+                * At least Windows and Samba are always doing this
+                * if there's a session key available.
+                *
+                * We only check the signature if it's mandatory
+                * or SMB2_HDR_FLAG_SIGNED is provided.
+                */
+               check_signature = true;
+       }
+
+       if (check_signature) {
+               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;
@@ -4809,6 +4983,27 @@ NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
        return NT_STATUS_OK;
 }
 
+NTSTATUS smb2cli_session_encryption_on(struct smbXcli_session *session)
+{
+       if (session->smb2->should_encrypt) {
+               return NT_STATUS_OK;
+       }
+
+       if (session->conn->protocol < PROTOCOL_SMB2_24) {
+               return NT_STATUS_NOT_SUPPORTED;
+       }
+
+       if (!(session->conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
+               return NT_STATUS_NOT_SUPPORTED;
+       }
+
+       if (session->smb2->signing_key.data == NULL) {
+               return NT_STATUS_NOT_SUPPORTED;
+       }
+       session->smb2->should_encrypt = true;
+       return NT_STATUS_OK;
+}
+
 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
 {
        struct smbXcli_tcon *tcon;
@@ -4895,3 +5090,8 @@ void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
                tcon->smb2.should_encrypt = true;
        }
 }
+
+bool smb2cli_tcon_is_encryption_on(struct smbXcli_tcon *tcon)
+{
+       return tcon->smb2.should_encrypt;
+}