libcli/smb: add FLAG_CASELESS_PATHNAMES based on FILE_CASE_SENSITIVE_SEARCH to smb1...
[mat/samba.git] / libcli / smb / smbXcli_base.c
index 5a5828acc76c02c35df21a26fc425751b95da538..082b62604b1f49bf1a4e5dff89ab75503000feb0 100644 (file)
@@ -167,6 +167,9 @@ struct smbXcli_session {
 };
 
 struct smbXcli_tcon {
+       bool is_smb1;
+       uint32_t fs_attributes;
+
        struct {
                uint16_t tcon_id;
                uint16_t optional_support;
@@ -246,6 +249,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;
@@ -590,6 +598,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;
@@ -715,6 +740,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;
 
@@ -1239,6 +1272,19 @@ struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
 
        if (tcon) {
                tid = tcon->smb1.tcon_id;
+
+               if (tcon->fs_attributes & FILE_CASE_SENSITIVE_SEARCH) {
+                       clear_flags |= FLAG_CASELESS_PATHNAMES;
+               } else {
+                       /* Default setting, case insensitive. */
+                       additional_flags |= FLAG_CASELESS_PATHNAMES;
+               }
+
+               if (smbXcli_conn_dfs_supported(conn) &&
+                   smbXcli_tcon_is_dfs_share(tcon))
+               {
+                       additional_flags2 |= FLAGS2_DFS_PATHNAMES;
+               }
        }
 
        state->smb1.recv_cmd = 0xFF;
@@ -1562,8 +1608,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;
        }
@@ -1587,7 +1632,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
                 */
@@ -2430,6 +2477,37 @@ bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
                || (talloc_array_length(conn->pending) != 0));
 }
 
+bool smbXcli_conn_dfs_supported(struct smbXcli_conn *conn)
+{
+       if (conn->protocol >= PROTOCOL_SMB2_02) {
+               return (smb2cli_conn_server_capabilities(conn) & SMB2_CAP_DFS);
+       }
+
+       return (smb1cli_conn_capabilities(conn) & CAP_DFS);
+}
+
+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;
@@ -2489,7 +2567,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;
        }
@@ -2538,7 +2616,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;
@@ -2611,6 +2690,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);
@@ -2787,7 +2867,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;
                }
@@ -2971,7 +3056,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;
@@ -2980,7 +3066,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;
        }
@@ -3622,6 +3710,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 {
@@ -4244,7 +4333,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)
@@ -4404,9 +4494,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);
@@ -4672,12 +4767,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) {
@@ -4769,11 +4870,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;
@@ -4888,6 +5008,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;
@@ -4900,6 +5041,38 @@ struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
        return tcon;
 }
 
+void smbXcli_tcon_set_fs_attributes(struct smbXcli_tcon *tcon,
+                                   uint32_t fs_attributes)
+{
+       tcon->fs_attributes = fs_attributes;
+}
+
+uint32_t smbXcli_tcon_get_fs_attributes(struct smbXcli_tcon *tcon)
+{
+       return tcon->fs_attributes;
+}
+
+bool smbXcli_tcon_is_dfs_share(struct smbXcli_tcon *tcon)
+{
+       if (tcon == NULL) {
+               return false;
+       }
+
+       if (tcon->is_smb1) {
+               if (tcon->smb1.optional_support & SMB_SHARE_IN_DFS) {
+                       return true;
+               }
+
+               return false;
+       }
+
+       if (tcon->smb2.capabilities & SMB2_SHARE_CAP_DFS) {
+               return true;
+       }
+
+       return false;
+}
+
 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
 {
        return tcon->smb1.tcon_id;
@@ -4907,6 +5080,7 @@ uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
 
 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
 {
+       tcon->is_smb1 = true;
        tcon->smb1.tcon_id = tcon_id;
 }
 
@@ -4918,6 +5092,8 @@ bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
                             const char *service,
                             const char *fs_type)
 {
+       tcon->is_smb1 = true;
+       tcon->fs_attributes = 0;
        tcon->smb1.tcon_id = tcon_id;
        tcon->smb1.optional_support = optional_support;
        tcon->smb1.maximal_access = maximal_access;
@@ -4956,6 +5132,8 @@ void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
                             uint32_t capabilities,
                             uint32_t maximal_access)
 {
+       tcon->is_smb1 = false;
+       tcon->fs_attributes = 0;
        tcon->smb2.tcon_id = tcon_id;
        tcon->smb2.type = type;
        tcon->smb2.flags = flags;
@@ -4974,3 +5152,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;
+}