s3:smb2_server: call change_to_root_user() or smbd_smb2_request_check_tcon()
[metze/samba/wip.git] / source3 / smbd / smb2_server.c
index b9fc3dc523ce57e1af0b101e47239a8f4a832416..90f476720311f7f3ff29efb58210cb13cd524e73 100644 (file)
 */
 
 #include "includes.h"
+#include "smbd/smbd.h"
 #include "smbd/globals.h"
 #include "../libcli/smb/smb_common.h"
 #include "../lib/tsocket/tsocket.h"
+#include "../lib/util/tevent_ntstatus.h"
+#include "smbprofile.h"
 
 #define OUTVEC_ALLOC_SIZE (SMB2_HDR_BODY + 9)
 
@@ -92,7 +95,7 @@ static NTSTATUS smbd_initialize_smb2(struct smbd_server_connection *sconn)
 
        TALLOC_FREE(sconn->smb1.fde);
 
-       sconn->smb2.event_ctx = smbd_event_context();
+       sconn->smb2.event_ctx = server_event_context();
 
        sconn->smb2.recv_queue = tevent_queue_create(sconn, "smb2 recv queue");
        if (sconn->smb2.recv_queue == NULL) {
@@ -110,6 +113,14 @@ static NTSTATUS smbd_initialize_smb2(struct smbd_server_connection *sconn)
        }
        sconn->smb2.sessions.limit = 0x0000FFFE;
        sconn->smb2.sessions.list = NULL;
+       sconn->smb2.seqnum_low = 0;
+       sconn->smb2.credits_granted = 0;
+       sconn->smb2.max_credits = lp_smb2_max_credits();
+       sconn->smb2.credits_bitmap = bitmap_talloc(sconn,
+                       DEFAULT_SMB2_MAX_CREDIT_BITMAP_FACTOR*sconn->smb2.max_credits);
+       if (sconn->smb2.credits_bitmap == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
 
        ret = tstream_bsd_existing_socket(sconn, sconn->sock,
                                          &sconn->smb2.stream);
@@ -258,15 +269,15 @@ static NTSTATUS smbd_smb2_request_create(struct smbd_server_connection *sconn,
        memcpy(req->in.nbt_hdr, inbuf, 4);
 
        ofs = 0;
-       req->in.vector[0].iov_base      = (void *)req->in.nbt_hdr;
+       req->in.vector[0].iov_base      = discard_const_p(void, req->in.nbt_hdr);
        req->in.vector[0].iov_len       = 4;
        ofs += req->in.vector[0].iov_len;
 
-       req->in.vector[1].iov_base      = (void *)(inbuf + ofs);
+       req->in.vector[1].iov_base      = discard_const_p(void, (inbuf + ofs));
        req->in.vector[1].iov_len       = SMB2_HDR_BODY;
        ofs += req->in.vector[1].iov_len;
 
-       req->in.vector[2].iov_base      = (void *)(inbuf + ofs);
+       req->in.vector[2].iov_base      = discard_const_p(void, (inbuf + ofs));
        req->in.vector[2].iov_len       = SVAL(inbuf, ofs) & 0xFFFE;
        ofs += req->in.vector[2].iov_len;
 
@@ -274,7 +285,7 @@ static NTSTATUS smbd_smb2_request_create(struct smbd_server_connection *sconn,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       req->in.vector[3].iov_base      = (void *)(inbuf + ofs);
+       req->in.vector[3].iov_base      = discard_const_p(void, (inbuf + ofs));
        req->in.vector[3].iov_len       = size - ofs;
        ofs += req->in.vector[3].iov_len;
 
@@ -284,14 +295,70 @@ static NTSTATUS smbd_smb2_request_create(struct smbd_server_connection *sconn,
        return NT_STATUS_OK;
 }
 
-static NTSTATUS smbd_smb2_request_validate(struct smbd_smb2_request *req,
-                               uint16_t *p_creds_requested)
+static bool smb2_validate_message_id(struct smbd_server_connection *sconn,
+                               const uint8_t *inhdr)
+{
+       uint64_t message_id = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
+       struct bitmap *credits_bm = sconn->smb2.credits_bitmap;
+       uint16_t opcode = IVAL(inhdr, SMB2_HDR_OPCODE);
+       unsigned int bitmap_offset;
+
+       if (opcode == SMB2_OP_CANCEL) {
+               /* SMB2_CANCEL requests by definition resend messageids. */
+               return true;
+       }
+
+       if (message_id < sconn->smb2.seqnum_low ||
+                       message_id > (sconn->smb2.seqnum_low +
+                       (sconn->smb2.max_credits * DEFAULT_SMB2_MAX_CREDIT_BITMAP_FACTOR))) {
+               DEBUG(0,("smb2_validate_message_id: bad message_id "
+                       "%llu (low = %llu, max = %lu)\n",
+                       (unsigned long long)message_id,
+                       (unsigned long long)sconn->smb2.seqnum_low,
+                       (unsigned long)sconn->smb2.max_credits ));
+               return false;
+       }
+
+       /* client just used a credit. */
+       SMB_ASSERT(sconn->smb2.credits_granted > 0);
+       sconn->smb2.credits_granted -= 1;
+
+       /* Mark the message_id as seen in the bitmap. */
+       bitmap_offset = (unsigned int)(message_id %
+                       (uint64_t)(sconn->smb2.max_credits * DEFAULT_SMB2_MAX_CREDIT_BITMAP_FACTOR));
+       if (bitmap_query(credits_bm, bitmap_offset)) {
+               DEBUG(0,("smb2_validate_message_id: duplicate message_id "
+                       "%llu (bm offset %u)\n",
+                       (unsigned long long)message_id,
+                       bitmap_offset));
+               return false;
+       }
+       bitmap_set(credits_bm, bitmap_offset);
+
+       if (message_id == sconn->smb2.seqnum_low + 1) {
+               /* Move the window forward by all the message_id's
+                  already seen. */
+               while (bitmap_query(credits_bm, bitmap_offset)) {
+                       DEBUG(10,("smb2_validate_message_id: clearing "
+                               "id %llu (position %u) from bitmap\n",
+                               (unsigned long long)(sconn->smb2.seqnum_low + 1),
+                               bitmap_offset ));
+                       bitmap_clear(credits_bm, bitmap_offset);
+                       sconn->smb2.seqnum_low += 1;
+                       bitmap_offset = (bitmap_offset + 1) %
+                               (sconn->smb2.max_credits * DEFAULT_SMB2_MAX_CREDIT_BITMAP_FACTOR);
+               }
+       }
+
+       return true;
+}
+
+static NTSTATUS smbd_smb2_request_validate(struct smbd_smb2_request *req)
 {
        int count;
        int idx;
        bool compound_related = false;
 
-       *p_creds_requested = 0;
        count = req->in.vector_count;
 
        if (count < 4) {
@@ -300,7 +367,6 @@ static NTSTATUS smbd_smb2_request_validate(struct smbd_smb2_request *req,
        }
 
        for (idx=1; idx < count; idx += 3) {
-               uint16_t creds_requested = 0;
                const uint8_t *inhdr = NULL;
                uint32_t flags;
 
@@ -314,16 +380,13 @@ static NTSTATUS smbd_smb2_request_validate(struct smbd_smb2_request *req,
 
                inhdr = (const uint8_t *)req->in.vector[idx].iov_base;
 
-               /* setup the SMB2 header */
+               /* Check the SMB2 header */
                if (IVAL(inhdr, SMB2_HDR_PROTOCOL_ID) != SMB2_MAGIC) {
                        return NT_STATUS_INVALID_PARAMETER;
                }
 
-               creds_requested = SVAL(inhdr, SMB2_HDR_CREDIT);
-               if (*p_creds_requested + creds_requested < creds_requested) {
-                       *p_creds_requested = 65535;
-               } else {
-                       *p_creds_requested += creds_requested;
+               if (!smb2_validate_message_id(req->sconn, inhdr)) {
+                       return NT_STATUS_INVALID_PARAMETER;
                }
 
                flags = IVAL(inhdr, SMB2_HDR_FLAGS);
@@ -374,7 +437,56 @@ static NTSTATUS smbd_smb2_request_validate(struct smbd_smb2_request *req,
        return NT_STATUS_OK;
 }
 
-static NTSTATUS smbd_smb2_request_setup_out(struct smbd_smb2_request *req, uint16_t creds)
+static void smb2_set_operation_credit(struct smbd_server_connection *sconn,
+                       const struct iovec *in_vector,
+                       struct iovec *out_vector)
+{
+       uint8_t *outhdr = (uint8_t *)out_vector->iov_base;
+       uint16_t credits_requested = 0;
+       uint16_t credits_granted = 0;
+
+       if (in_vector != NULL) {
+               const uint8_t *inhdr = (const uint8_t *)in_vector->iov_base;
+               credits_requested = SVAL(inhdr, SMB2_HDR_CREDIT);
+       }
+
+       SMB_ASSERT(sconn->smb2.max_credits >= sconn->smb2.credits_granted);
+
+       /* Remember what we gave out. */
+       credits_granted = MIN(credits_requested, (sconn->smb2.max_credits -
+               sconn->smb2.credits_granted));
+
+       if (credits_granted == 0 && sconn->smb2.credits_granted == 0) {
+               /* First negprot packet, or ensure the client credits can
+                  never drop to zero. */
+               credits_granted = 1;
+       }
+
+       SSVAL(outhdr, SMB2_HDR_CREDIT, credits_granted);
+       sconn->smb2.credits_granted += credits_granted;
+
+       DEBUG(10,("smb2_set_operation_credit: requested %u, "
+               "granted %u, total granted %u\n",
+               (unsigned int)credits_requested,
+               (unsigned int)credits_granted,
+               (unsigned int)sconn->smb2.credits_granted ));
+}
+
+static void smb2_calculate_credits(const struct smbd_smb2_request *inreq,
+                               struct smbd_smb2_request *outreq)
+{
+       int count, idx;
+
+       count = outreq->out.vector_count;
+
+       for (idx=1; idx < count; idx += 3) {
+               smb2_set_operation_credit(outreq->sconn,
+                       &inreq->in.vector[idx],
+                       &outreq->out.vector[idx]);
+       }
+}
+
+static NTSTATUS smbd_smb2_request_setup_out(struct smbd_smb2_request *req)
 {
        struct iovec *vector;
        int count;
@@ -432,11 +544,6 @@ static NTSTATUS smbd_smb2_request_setup_out(struct smbd_smb2_request *req, uint1
                      NT_STATUS_V(NT_STATUS_INTERNAL_ERROR));
                SSVAL(outhdr, SMB2_HDR_OPCODE,
                      SVAL(inhdr, SMB2_HDR_OPCODE));
-               SSVAL(outhdr, SMB2_HDR_CREDIT,          creds);
-
-               /* Remember what we gave out. */
-               req->sconn->smb2.credits_granted += creds;
-
                SIVAL(outhdr, SMB2_HDR_FLAGS,
                      IVAL(inhdr, SMB2_HDR_FLAGS) | SMB2_HDR_FLAG_REDIRECT);
                SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND,    next_command_ofs);
@@ -501,7 +608,7 @@ static bool dup_smb2_vec3(TALLOC_CTX *ctx,
                        srcvec[1].iov_base ==
                                ((uint8_t *)srcvec[0].iov_base) +
                                        SMB2_HDR_BODY) {
-               outvec[1].iov_base = ((uint8_t *)outvec[1].iov_base) +
+               outvec[1].iov_base = ((uint8_t *)outvec[0].iov_base) +
                                        SMB2_HDR_BODY;
                outvec[1].iov_len = 8;
        } else {
@@ -558,10 +665,18 @@ static struct smbd_smb2_request *dup_smb2_req(const struct smbd_smb2_request *re
        }
 
        newreq->sconn = req->sconn;
+       newreq->session = req->session;
        newreq->do_signing = req->do_signing;
        newreq->current_idx = req->current_idx;
        newreq->async = false;
        newreq->cancelled = false;
+       /* Note we are leaving:
+               ->tcon
+               ->smb1req
+               ->compat_chain_fsp
+          uninitialized as NULL here as
+          they're not used in the interim
+          response code. JRA. */
 
        outvec = talloc_zero_array(newreq, struct iovec, count);
        if (!outvec) {
@@ -623,6 +738,9 @@ static NTSTATUS smb2_send_async_interim_response(const struct smbd_smb2_request
        /* And end the chain. */
        SIVAL(outhdr, SMB2_HDR_NEXT_COMMAND, 0);
 
+       /* Calculate outgoing credits */
+       smb2_calculate_credits(req, nreq);
+
        /* Re-sign if needed. */
        if (nreq->do_signing) {
                NTSTATUS status;
@@ -779,9 +897,6 @@ NTSTATUS smbd_smb2_request_pending_queue(struct smbd_smb2_request *req,
        SSVAL(hdr, SMB2_HDR_EPOCH, 0);
        SIVAL(hdr, SMB2_HDR_STATUS, NT_STATUS_V(STATUS_PENDING));
        SSVAL(hdr, SMB2_HDR_OPCODE, SVAL(reqhdr, SMB2_HDR_OPCODE));
-       SSVAL(hdr, SMB2_HDR_CREDIT, 5);
-
-       req->sconn->smb2.credits_granted += 5;
 
        SIVAL(hdr, SMB2_HDR_FLAGS, flags | SMB2_HDR_FLAG_ASYNC);
        SIVAL(hdr, SMB2_HDR_NEXT_COMMAND, 0);
@@ -799,9 +914,16 @@ NTSTATUS smbd_smb2_request_pending_queue(struct smbd_smb2_request *req,
        /* Match W2K8R2... */
        SCVAL(body, 0x08, 0x21);
 
+       /* Ensure we correctly go through crediting. Grant
+          the credits now, and zero credits on the final
+          response. */
+       smb2_set_operation_credit(req->sconn,
+                       &req->in.vector[i],
+                       &state->vector[1]);
+
        if (req->do_signing) {
                status = smb2_signing_sign_pdu(req->session->session_key,
-                                       state->vector, 3);
+                                       &state->vector[1], 2);
                if (!NT_STATUS_IS_OK(status)) {
                        return status;
                }
@@ -876,8 +998,6 @@ NTSTATUS smbd_smb2_request_pending_queue(struct smbd_smb2_request *req,
        reqhdr = (uint8_t *)req->out.vector[1].iov_base;
        SIVAL(reqhdr, SMB2_HDR_FLAGS, flags | SMB2_HDR_FLAG_ASYNC);
        SBVAL(reqhdr, SMB2_HDR_PID, async_id);
-       /* Only return credits on the interim response. */
-       SSVAL(reqhdr, SMB2_HDR_CREDIT, 0);
 
        {
                const uint8_t *inhdr =
@@ -1019,6 +1139,9 @@ NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
 
        switch (opcode) {
        case SMB2_OP_NEGPROT:
+               /* This call needs to be run as root */
+               change_to_root_user();
+
                {
                        START_PROFILE(smb2_negprot);
                        return_value = smbd_smb2_request_process_negprot(req);
@@ -1027,6 +1150,9 @@ NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
                break;
 
        case SMB2_OP_SESSSETUP:
+               /* This call needs to be run as root */
+               change_to_root_user();
+
                {
                        START_PROFILE(smb2_sesssetup);
                        return_value = smbd_smb2_request_process_sesssetup(req);
@@ -1040,6 +1166,9 @@ NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
                        break;
                }
 
+               /* This call needs to be run as root */
+               change_to_root_user();
+
                {
                        START_PROFILE(smb2_logoff);
                        return_value = smbd_smb2_request_process_logoff(req);
@@ -1052,11 +1181,9 @@ NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
                        return_value = smbd_smb2_request_error(req, session_status);
                        break;
                }
-               status = smbd_smb2_request_check_session(req);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return_value = smbd_smb2_request_error(req, status);
-                       break;
-               }
+
+               /* This call needs to be run as root */
+               change_to_root_user();
 
                {
                        START_PROFILE(smb2_tcon);
@@ -1075,6 +1202,9 @@ NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
                        return_value = smbd_smb2_request_error(req, status);
                        break;
                }
+               /* This call needs to be run as root */
+               change_to_root_user();
+
 
                {
                        START_PROFILE(smb2_tdis);
@@ -1218,6 +1348,9 @@ NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
                break;
 
        case SMB2_OP_CANCEL:
+               /* This call needs to be run as root */
+               change_to_root_user();
+
                {
                        START_PROFILE(smb2_cancel);
                        return_value = smbd_smb2_request_process_cancel(req);
@@ -1226,9 +1359,14 @@ NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
                break;
 
        case SMB2_OP_KEEPALIVE:
-               {START_PROFILE(smb2_keepalive);
-               return_value = smbd_smb2_request_process_keepalive(req);
-               END_PROFILE(smb2_keepalive);}
+               /* This call needs to be run as root */
+               change_to_root_user();
+
+               {
+                       START_PROFILE(smb2_keepalive);
+                       return_value = smbd_smb2_request_process_keepalive(req);
+                       END_PROFILE(smb2_keepalive);
+               }
                break;
 
        case SMB2_OP_FIND:
@@ -1331,21 +1469,10 @@ NTSTATUS smbd_smb2_request_dispatch(struct smbd_smb2_request *req)
 static NTSTATUS smbd_smb2_request_reply(struct smbd_smb2_request *req)
 {
        struct tevent_req *subreq;
+       int i = req->current_idx;
 
        req->subreq = NULL;
 
-       smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
-
-       if (req->do_signing) {
-               int i = req->current_idx;
-               NTSTATUS status;
-               status = smb2_signing_sign_pdu(req->session->session_key,
-                                              &req->out.vector[i], 3);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return status;
-               }
-       }
-
        req->current_idx += 3;
 
        if (req->current_idx < req->out.vector_count) {
@@ -1368,11 +1495,37 @@ static NTSTATUS smbd_smb2_request_reply(struct smbd_smb2_request *req)
                return NT_STATUS_OK;
        }
 
+       smb2_setup_nbt_length(req->out.vector, req->out.vector_count);
+
+       /* Set credit for this operation (zero credits if this
+          is a final reply for an async operation). */
+       smb2_set_operation_credit(req->sconn,
+                       req->async ? NULL : &req->in.vector[i],
+                       &req->out.vector[i]);
+
+       if (req->do_signing) {
+               NTSTATUS status;
+               status = smb2_signing_sign_pdu(req->session->session_key,
+                                              &req->out.vector[i], 3);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+       }
+
        if (DEBUGLEVEL >= 10) {
                dbgtext("smbd_smb2_request_reply: sending...\n");
                print_req_vectors(req);
        }
 
+       /* I am a sick, sick man... :-). Sendfile hack ... JRA. */
+       if (req->out.vector_count == 4 &&
+                       req->out.vector[3].iov_base == NULL &&
+                       req->out.vector[3].iov_len != 0) {
+               /* Dynamic part is NULL. Chop it off,
+                  We're going to send it via sendfile. */
+               req->out.vector_count -= 1;
+       }
+
        subreq = tstream_writev_queue_send(req,
                                           req->sconn->smb2.event_ctx,
                                           req->sconn->smb2.stream,
@@ -1891,9 +2044,11 @@ static int smbd_smb2_request_next_vector(struct tstream_context *stream,
                                invalid = true;
                        }
 
-                       if ((body_size % 2) != 0) {
-                               body_size -= 1;
-                       }
+                       /*
+                        * Mask out the lowest bit, the "dynamic" part
+                        * of body_size.
+                        */
+                       body_size &= ~1;
 
                        if (body_size > (full_size - SMB2_HDR_BODY)) {
                                /*
@@ -2042,17 +2197,9 @@ void smbd_smb2_first_negprot(struct smbd_server_connection *sconn,
                             const uint8_t *inbuf, size_t size)
 {
        NTSTATUS status;
-       struct smbd_smb2_request *req;
+       struct smbd_smb2_request *req = NULL;
        struct tevent_req *subreq;
 
-       if (lp_security() == SEC_SHARE) {
-               DEBUG(2,("WARNING!!: \"security = share\" is deprecated for "
-                       "SMB2 servers. Mapping to \"security = user\" and "
-                       "\"map to guest = Bad User\"\n" ));
-               lp_do_parameter(-1, "security", "user");
-               lp_do_parameter(-1, "map to guest", "Bad User");
-       }
-
        DEBUG(10,("smbd_smb2_first_negprot: packet length %u\n",
                 (unsigned int)size));
 
@@ -2068,7 +2215,7 @@ void smbd_smb2_first_negprot(struct smbd_server_connection *sconn,
                return;
        }
 
-       status = smbd_smb2_request_setup_out(req, 1);
+       status = smbd_smb2_request_setup_out(req);
        if (!NT_STATUS_IS_OK(status)) {
                smbd_server_connection_terminate(sconn, nt_errstr(status));
                return;
@@ -2091,7 +2238,6 @@ void smbd_smb2_first_negprot(struct smbd_server_connection *sconn,
 
 static void smbd_smb2_request_incoming(struct tevent_req *subreq)
 {
-       uint16_t creds_requested = 0;
        struct smbd_server_connection *sconn = tevent_req_callback_data(subreq,
                                               struct smbd_server_connection);
        NTSTATUS status;
@@ -2118,13 +2264,13 @@ static void smbd_smb2_request_incoming(struct tevent_req *subreq)
        DEBUG(10,("smbd_smb2_request_incoming: idx[%d] of %d vectors\n",
                 req->current_idx, req->in.vector_count));
 
-       status = smbd_smb2_request_validate(req, &creds_requested);
+       status = smbd_smb2_request_validate(req);
        if (!NT_STATUS_IS_OK(status)) {
                smbd_server_connection_terminate(sconn, nt_errstr(status));
                return;
        }
 
-       status = smbd_smb2_request_setup_out(req, 5);
+       status = smbd_smb2_request_setup_out(req);
        if (!NT_STATUS_IS_OK(status)) {
                smbd_server_connection_terminate(sconn, nt_errstr(status));
                return;