Revert "smbd: add smbd_impersonate_debug_create() helper"
[gd/samba-autobuild/.git] / source3 / smbd / smb2_tcon.c
index 96a5c12b2aef51379d511a65306b2854d7db0c91..3a4a15d3059ecc3229d4163ba1499ebdb659792e 100644 (file)
@@ -27,6 +27,9 @@
 #include "lib/param/loadparm.h"
 #include "../lib/util/tevent_ntstatus.h"
 
+#undef DBGC_CLASS
+#define DBGC_CLASS DBGC_SMB2
+
 static struct tevent_req *smbd_smb2_tree_connect_send(TALLOC_CTX *mem_ctx,
                                        struct tevent_context *ev,
                                        struct smbd_smb2_request *smb2req,
@@ -36,14 +39,14 @@ static NTSTATUS smbd_smb2_tree_connect_recv(struct tevent_req *req,
                                            uint32_t *out_share_flags,
                                            uint32_t *out_capabilities,
                                            uint32_t *out_maximal_access,
-                                           uint32_t *out_tree_id);
+                                           uint32_t *out_tree_id,
+                                           bool *disconnect);
 
 static void smbd_smb2_request_tcon_done(struct tevent_req *subreq);
 
 NTSTATUS smbd_smb2_request_process_tcon(struct smbd_smb2_request *req)
 {
        const uint8_t *inbody;
-       int i = req->current_idx;
        uint16_t in_path_offset;
        uint16_t in_path_length;
        DATA_BLOB in_path_buffer;
@@ -57,20 +60,20 @@ NTSTATUS smbd_smb2_request_process_tcon(struct smbd_smb2_request *req)
        if (!NT_STATUS_IS_OK(status)) {
                return smbd_smb2_request_error(req, status);
        }
-       inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
+       inbody = SMBD_SMB2_IN_BODY_PTR(req);
 
        in_path_offset = SVAL(inbody, 0x04);
        in_path_length = SVAL(inbody, 0x06);
 
-       if (in_path_offset != (SMB2_HDR_BODY + req->in.vector[i+1].iov_len)) {
+       if (in_path_offset != (SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(req))) {
                return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
        }
 
-       if (in_path_length > req->in.vector[i+2].iov_len) {
+       if (in_path_length > SMBD_SMB2_IN_DYN_LEN(req)) {
                return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
        }
 
-       in_path_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
+       in_path_buffer.data = SMBD_SMB2_IN_DYN_PTR(req);
        in_path_buffer.length = in_path_length;
 
        ok = convert_string_talloc(req, CH_UTF16, CH_UNIX,
@@ -91,7 +94,7 @@ NTSTATUS smbd_smb2_request_process_tcon(struct smbd_smb2_request *req)
        }
 
        subreq = smbd_smb2_tree_connect_send(req,
-                                            req->sconn->ev_ctx,
+                                            req->ev_ctx,
                                             req,
                                             in_path_string);
        if (subreq == NULL) {
@@ -107,7 +110,6 @@ static void smbd_smb2_request_tcon_done(struct tevent_req *subreq)
        struct smbd_smb2_request *req =
                tevent_req_callback_data(subreq,
                struct smbd_smb2_request);
-       int i = req->current_idx;
        uint8_t *outhdr;
        DATA_BLOB outbody;
        uint8_t out_share_type = 0;
@@ -115,6 +117,7 @@ static void smbd_smb2_request_tcon_done(struct tevent_req *subreq)
        uint32_t out_capabilities = 0;
        uint32_t out_maximal_access = 0;
        uint32_t out_tree_id = 0;
+       bool disconnect = false;
        NTSTATUS status;
        NTSTATUS error;
 
@@ -123,25 +126,31 @@ static void smbd_smb2_request_tcon_done(struct tevent_req *subreq)
                                             &out_share_flags,
                                             &out_capabilities,
                                             &out_maximal_access,
-                                            &out_tree_id);
+                                            &out_tree_id,
+                                            &disconnect);
        TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
+               if (disconnect) {
+                       smbd_server_connection_terminate(req->xconn,
+                                                        nt_errstr(status));
+                       return;
+               }
                error = smbd_smb2_request_error(req, status);
                if (!NT_STATUS_IS_OK(error)) {
-                       smbd_server_connection_terminate(req->sconn,
+                       smbd_server_connection_terminate(req->xconn,
                                                         nt_errstr(error));
                        return;
                }
                return;
        }
 
-       outhdr = (uint8_t *)req->out.vector[i].iov_base;
+       outhdr = SMBD_SMB2_OUT_HDR_PTR(req);
 
-       outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
+       outbody = smbd_smb2_generate_outbody(req, 0x10);
        if (outbody.data == NULL) {
                error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
                if (!NT_STATUS_IS_OK(error)) {
-                       smbd_server_connection_terminate(req->sconn,
+                       smbd_server_connection_terminate(req->xconn,
                                                         nt_errstr(error));
                        return;
                }
@@ -163,7 +172,7 @@ static void smbd_smb2_request_tcon_done(struct tevent_req *subreq)
 
        error = smbd_smb2_request_done(req, outbody, NULL);
        if (!NT_STATUS_IS_OK(error)) {
-               smbd_server_connection_terminate(req->sconn,
+               smbd_server_connection_terminate(req->xconn,
                                                 nt_errstr(error));
                return;
        }
@@ -175,8 +184,10 @@ static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
                                       uint32_t *out_share_flags,
                                       uint32_t *out_capabilities,
                                       uint32_t *out_maximal_access,
-                                      uint32_t *out_tree_id)
+                                      uint32_t *out_tree_id,
+                                      bool *disconnect)
 {
+       struct smbXsrv_connection *conn = req->xconn;
        const char *share = in_path;
        char *service = NULL;
        int snum = -1;
@@ -185,6 +196,12 @@ static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
        connection_struct *compat_conn = NULL;
        struct user_struct *compat_vuser = req->session->compat;
        NTSTATUS status;
+       bool encryption_desired = req->session->global->encryption_flags & SMBXSRV_ENCRYPTION_DESIRED;
+       bool encryption_required = req->session->global->encryption_flags & SMBXSRV_ENCRYPTION_REQUIRED;
+       bool guest_session = false;
+       bool require_signed_tcon = false;
+
+       *disconnect = false;
 
        if (strncmp(share, "\\\\", 2) == 0) {
                const char *p = strchr(share+2, '\\');
@@ -196,12 +213,34 @@ static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
        DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
                  in_path, share));
 
+       if (security_session_user_level(compat_vuser->session_info, NULL) < SECURITY_USER) {
+               guest_session = true;
+       }
+
+       if (conn->protocol >= PROTOCOL_SMB3_11 && !guest_session) {
+               require_signed_tcon = true;
+       }
+
+       if (require_signed_tcon && !req->do_encryption && !req->do_signing) {
+               DEBUG(1, ("smbd_smb2_tree_connect: reject request to share "
+                         "[%s] as '%s\\%s' without encryption or signing. "
+                         "Disconnecting.\n",
+                         share,
+                         req->session->global->auth_session_info->info->domain_name,
+                         req->session->global->auth_session_info->info->account_name));
+               *disconnect = true;
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
        service = talloc_strdup(talloc_tos(), share);
        if(!service) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       strlower_m(service);
+       if (!strlower_m(service)) {
+               DEBUG(2, ("strlower_m %s failed\n", service));
+               return NT_STATUS_INVALID_PARAMETER;
+       }
 
        /* TODO: do more things... */
        if (strequal(service,HOMES_NAME)) {
@@ -231,12 +270,45 @@ static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
                return NT_STATUS_BAD_NETWORK_NAME;
        }
 
+       /* Handle non-DFS clients attempting connections to msdfs proxy */
+       if (lp_host_msdfs()) {
+               char *proxy = lp_msdfs_proxy(talloc_tos(), snum);
+
+               if ((proxy != NULL) && (*proxy != '\0')) {
+                       DBG_NOTICE("refusing connection to dfs proxy share "
+                                  "'%s' (pointing to %s)\n",
+                                  service,
+                                  proxy);
+                       TALLOC_FREE(proxy);
+                       return NT_STATUS_BAD_NETWORK_NAME;
+               }
+               TALLOC_FREE(proxy);
+       }
+
+       if ((lp_smb_encrypt(snum) >= SMB_SIGNING_DESIRED) &&
+           (conn->smb2.server.cipher != 0))
+       {
+               encryption_desired = true;
+       }
+
        if (lp_smb_encrypt(snum) == SMB_SIGNING_REQUIRED) {
-               status = NT_STATUS_ACCESS_DENIED;
-               DEBUG(3,("smbd_smb2_tree_connect: "
-                        "service %s needs encryption - %s\n",
-                        service, nt_errstr(status)));
-               return status;
+               encryption_desired = true;
+               encryption_required = true;
+       }
+
+       if (guest_session && encryption_required) {
+               DEBUG(1,("reject guest as encryption is required for service %s\n",
+                        service));
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
+       if (conn->smb2.server.cipher == 0) {
+               if (encryption_required) {
+                       DEBUG(1,("reject tcon with dialect[0x%04X] "
+                                "as encryption is required for service %s\n",
+                                conn->smb2.server.dialect, service));
+                       return NT_STATUS_ACCESS_DENIED;
+               }
        }
 
        /* create a new tcon as child of the session */
@@ -245,7 +317,14 @@ static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
                return status;
        }
 
-       compat_conn = make_connection_smb2(req->sconn,
+       if (encryption_desired) {
+               tcon->global->encryption_flags |= SMBXSRV_ENCRYPTION_DESIRED;
+       }
+       if (encryption_required) {
+               tcon->global->encryption_flags |= SMBXSRV_ENCRYPTION_REQUIRED;
+       }
+
+       compat_conn = make_connection_smb2(req,
                                        tcon, snum,
                                        req->session->compat,
                                        "???",
@@ -262,6 +341,8 @@ static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
                TALLOC_FREE(tcon);
                return NT_STATUS_NO_MEMORY;
        }
+       tcon->global->session_global_id =
+               req->session->global->session_global_id;
 
        tcon->compat = talloc_move(tcon, &compat_conn);
 
@@ -306,14 +387,20 @@ static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
                break;
        }
 
-       if (lp_hideunreadable(SNUM(tcon->compat)) ||
-           lp_hideunwriteable_files(SNUM(tcon->compat))) {
+       if (lp_hide_unreadable(SNUM(tcon->compat)) ||
+           lp_hide_unwriteable_files(SNUM(tcon->compat))) {
                *out_share_flags |= SMB2_SHAREFLAG_ACCESS_BASED_DIRECTORY_ENUM;
        }
 
+       if (encryption_desired) {
+               *out_share_flags |= SMB2_SHAREFLAG_ENCRYPT_DATA;
+       }
+
        *out_maximal_access = tcon->compat->share_access;
 
        *out_tree_id = tcon->global->tcon_wire_id;
+       req->last_tid = tcon->global->tcon_wire_id;
+
        return NT_STATUS_OK;
 }
 
@@ -324,6 +411,7 @@ struct smbd_smb2_tree_connect_state {
        uint32_t out_capabilities;
        uint32_t out_maximal_access;
        uint32_t out_tree_id;
+       bool disconnect;
 };
 
 static struct tevent_req *smbd_smb2_tree_connect_send(TALLOC_CTX *mem_ctx,
@@ -348,7 +436,8 @@ static struct tevent_req *smbd_smb2_tree_connect_send(TALLOC_CTX *mem_ctx,
                                        &state->out_share_flags,
                                        &state->out_capabilities,
                                        &state->out_maximal_access,
-                                       &state->out_tree_id);
+                                       &state->out_tree_id,
+                                       &state->disconnect);
        if (tevent_req_nterror(req, status)) {
                return tevent_req_post(req, ev);
        }
@@ -362,7 +451,8 @@ static NTSTATUS smbd_smb2_tree_connect_recv(struct tevent_req *req,
                                            uint32_t *out_share_flags,
                                            uint32_t *out_capabilities,
                                            uint32_t *out_maximal_access,
-                                           uint32_t *out_tree_id)
+                                           uint32_t *out_tree_id,
+                                           bool *disconnect)
 {
        struct smbd_smb2_tree_connect_state *state =
                tevent_req_data(req,
@@ -379,45 +469,198 @@ static NTSTATUS smbd_smb2_tree_connect_recv(struct tevent_req *req,
        *out_capabilities = state->out_capabilities;
        *out_maximal_access = state->out_maximal_access;
        *out_tree_id = state->out_tree_id;
+       *disconnect = state->disconnect;
 
        tevent_req_received(req);
        return NT_STATUS_OK;
 }
 
+static struct tevent_req *smbd_smb2_tdis_send(TALLOC_CTX *mem_ctx,
+                                       struct tevent_context *ev,
+                                       struct smbd_smb2_request *smb2req);
+static NTSTATUS smbd_smb2_tdis_recv(struct tevent_req *req);
+static void smbd_smb2_request_tdis_done(struct tevent_req *subreq);
+
 NTSTATUS smbd_smb2_request_process_tdis(struct smbd_smb2_request *req)
 {
        NTSTATUS status;
-       DATA_BLOB outbody;
+       struct tevent_req *subreq = NULL;
 
        status = smbd_smb2_request_verify_sizes(req, 0x04);
        if (!NT_STATUS_IS_OK(status)) {
                return smbd_smb2_request_error(req, status);
        }
 
+       subreq = smbd_smb2_tdis_send(req, req->ev_ctx, req);
+       if (subreq == NULL) {
+               return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
+       }
+       tevent_req_set_callback(subreq, smbd_smb2_request_tdis_done, req);
+
        /*
-        * TODO: cancel all outstanding requests on the tcon
+        * Wait a long time before going async on this to allow
+        * requests we're waiting on to finish. Set timeout to 10 secs.
         */
-       status = smbXsrv_tcon_disconnect(req->tcon, req->tcon->compat->vuid);
+       return smbd_smb2_request_pending_queue(req, subreq, 10000000);
+}
+
+static void smbd_smb2_request_tdis_done(struct tevent_req *subreq)
+{
+       struct smbd_smb2_request *smb2req =
+               tevent_req_callback_data(subreq,
+               struct smbd_smb2_request);
+       DATA_BLOB outbody;
+       NTSTATUS status;
+       NTSTATUS error;
+
+       status = smbd_smb2_tdis_recv(subreq);
+       TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("smbd_smb2_request_process_tdis: "
-                         "smbXsrv_tcon_disconnect() failed: %s\n",
-                         nt_errstr(status)));
-               /*
-                * If we hit this case, there is something completely
-                * wrong, so we better disconnect the transport connection.
-                */
-               return status;
+               error = smbd_smb2_request_error(smb2req, status);
+               if (!NT_STATUS_IS_OK(error)) {
+                       smbd_server_connection_terminate(smb2req->xconn,
+                                                       nt_errstr(error));
+                       return;
+               }
+               return;
        }
 
-       TALLOC_FREE(req->tcon);
-
-       outbody = data_blob_talloc(req->out.vector, NULL, 0x04);
+       outbody = smbd_smb2_generate_outbody(smb2req, 0x04);
        if (outbody.data == NULL) {
-               return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
+               error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
+               if (!NT_STATUS_IS_OK(error)) {
+                       smbd_server_connection_terminate(smb2req->xconn,
+                                                       nt_errstr(error));
+                       return;
+               }
+               return;
        }
 
        SSVAL(outbody.data, 0x00, 0x04);        /* struct size */
        SSVAL(outbody.data, 0x02, 0);           /* reserved */
 
-       return smbd_smb2_request_done(req, outbody, NULL);
+       error = smbd_smb2_request_done(smb2req, outbody, NULL);
+       if (!NT_STATUS_IS_OK(error)) {
+               smbd_server_connection_terminate(smb2req->xconn,
+                                               nt_errstr(error));
+               return;
+       }
+}
+
+struct smbd_smb2_tdis_state {
+       struct smbd_smb2_request *smb2req;
+       struct tevent_queue *wait_queue;
+};
+
+static void smbd_smb2_tdis_wait_done(struct tevent_req *subreq);
+
+static struct tevent_req *smbd_smb2_tdis_send(TALLOC_CTX *mem_ctx,
+                                       struct tevent_context *ev,
+                                       struct smbd_smb2_request *smb2req)
+{
+       struct tevent_req *req;
+       struct smbd_smb2_tdis_state *state;
+       struct tevent_req *subreq;
+       struct smbXsrv_connection *xconn = NULL;
+
+       req = tevent_req_create(mem_ctx, &state,
+                       struct smbd_smb2_tdis_state);
+       if (req == NULL) {
+               return NULL;
+       }
+       state->smb2req = smb2req;
+
+       state->wait_queue = tevent_queue_create(state, "tdis_wait_queue");
+       if (tevent_req_nomem(state->wait_queue, req)) {
+               return tevent_req_post(req, ev);
+       }
+
+       /*
+        * Make sure that no new request will be able to use this tcon.
+        */
+       smb2req->tcon->status = NT_STATUS_NETWORK_NAME_DELETED;
+
+       xconn = smb2req->xconn->client->connections;
+       for (; xconn != NULL; xconn = xconn->next) {
+               struct smbd_smb2_request *preq;
+
+               for (preq = xconn->smb2.requests; preq != NULL; preq = preq->next) {
+                       if (preq == smb2req) {
+                               /* Can't cancel current request. */
+                               continue;
+                       }
+                       if (preq->tcon != smb2req->tcon) {
+                               /* Request on different tcon. */
+                               continue;
+                       }
+
+                       /*
+                        * Never cancel anything in a compound
+                        * request. Way too hard to deal with
+                        * the result.
+                        */
+                       if (!preq->compound_related && preq->subreq != NULL) {
+                               tevent_req_cancel(preq->subreq);
+                       }
+
+                       /*
+                        * Now wait until the request is finished.
+                        *
+                        * We don't set a callback, as we just want to block the
+                        * wait queue and the talloc_free() of the request will
+                        * remove the item from the wait queue.
+                        */
+                       subreq = tevent_queue_wait_send(preq, ev, state->wait_queue);
+                       if (tevent_req_nomem(subreq, req)) {
+                               return tevent_req_post(req, ev);
+                       }
+               }
+       }
+
+       /*
+        * Now we add our own waiter to the end of the queue,
+        * this way we get notified when all pending requests are finished
+        * and send to the socket.
+        */
+       subreq = tevent_queue_wait_send(state, ev, state->wait_queue);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, smbd_smb2_tdis_wait_done, req);
+
+       return req;
+}
+
+static void smbd_smb2_tdis_wait_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct smbd_smb2_tdis_state *state = tevent_req_data(
+               req, struct smbd_smb2_tdis_state);
+       NTSTATUS status;
+
+       tevent_queue_wait_recv(subreq);
+       TALLOC_FREE(subreq);
+
+       /*
+        * As we've been awoken, we may have changed
+        * uid in the meantime. Ensure we're still
+        * root (SMB2_OP_TDIS has .as_root = true).
+        */
+       change_to_root_user();
+
+       status = smbXsrv_tcon_disconnect(state->smb2req->tcon,
+                                        state->smb2req->tcon->compat->vuid);
+       if (tevent_req_nterror(req, status)) {
+               return;
+       }
+
+       /* We did tear down the tcon. */
+       TALLOC_FREE(state->smb2req->tcon);
+       tevent_req_done(req);
+}
+
+static NTSTATUS smbd_smb2_tdis_recv(struct tevent_req *req)
+{
+       return tevent_req_simple_recv_ntstatus(req);
 }