s3:smbd: pass smbXsrv_session instead of user_struct to session_claim() and session_y...
[kai/samba.git] / source3 / smbd / smb2_sesssetup.c
index 07a168f8f6cc41cc2637fe8b4c5cb43037109645..29253d032171160cd2d9f2939aaa5e7c38cffd85 100644 (file)
@@ -190,6 +190,10 @@ static NTSTATUS smbd_smb2_auth_generic_return(struct smbXsrv_session *session,
                x->global->signing_required = true;
        }
 
+       if (lp_smb_encrypt(-1) == SMB_SIGNING_REQUIRED) {
+               x->global->encryption_required = true;
+       }
+
        if (security_session_user_level(session_info, NULL) < SECURITY_USER) {
                /* we map anonymous to guest internally */
                *out_session_flags |= SMB2_SESSION_FLAG_IS_GUEST;
@@ -199,6 +203,24 @@ static NTSTATUS smbd_smb2_auth_generic_return(struct smbXsrv_session *session,
                guest = true;
        }
 
+       if (guest && x->global->encryption_required) {
+               DEBUG(1,("reject guest session as encryption is required\n"));
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
+       if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
+               if (x->global->encryption_required) {
+                       DEBUG(1,("reject session with dialect[0x%04X] "
+                                "as encryption is required\n",
+                                conn->smb2.server.dialect));
+                       return NT_STATUS_ACCESS_DENIED;
+               }
+       }
+
+       if (x->global->encryption_required) {
+               *out_session_flags |= SMB2_SESSION_FLAG_ENCRYPT_DATA;
+       }
+
        ZERO_STRUCT(session_key);
        memcpy(session_key, session_info->session_key.data,
               MIN(session_info->session_key.length, sizeof(session_key)));
@@ -221,6 +243,45 @@ static NTSTATUS smbd_smb2_auth_generic_return(struct smbXsrv_session *session,
                                    x->global->signing_key.data);
        }
 
+       if (conn->protocol >= PROTOCOL_SMB2_24) {
+               const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
+               const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
+
+               x->global->decryption_key = data_blob_talloc(x->global,
+                                                            session_key,
+                                                            sizeof(session_key));
+               if (x->global->decryption_key.data == NULL) {
+                       ZERO_STRUCT(session_key);
+                       return NT_STATUS_NO_MEMORY;
+               }
+
+               smb2_key_derivation(session_key, sizeof(session_key),
+                                   label.data, label.length,
+                                   context.data, context.length,
+                                   x->global->decryption_key.data);
+       }
+
+       if (conn->protocol >= PROTOCOL_SMB2_24) {
+               const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
+               const DATA_BLOB context = data_blob_string_const_null("ServerOut");
+
+               x->global->encryption_key = data_blob_talloc(x->global,
+                                                            session_key,
+                                                            sizeof(session_key));
+               if (x->global->encryption_key.data == NULL) {
+                       ZERO_STRUCT(session_key);
+                       return NT_STATUS_NO_MEMORY;
+               }
+
+               smb2_key_derivation(session_key, sizeof(session_key),
+                                   label.data, label.length,
+                                   context.data, context.length,
+                                   x->global->encryption_key.data);
+
+               generate_random_buffer((uint8_t *)&x->nonce_high, sizeof(x->nonce_high));
+               x->nonce_low = 1;
+       }
+
        x->global->application_key = data_blob_dup_talloc(x->global,
                                                x->global->signing_key);
        if (x->global->application_key.data == NULL) {
@@ -269,7 +330,7 @@ static NTSTATUS smbd_smb2_auth_generic_return(struct smbXsrv_session *session,
                        register_homes_share(session_info->unix_info->unix_name);
        }
 
-       if (!session_claim(smb2req->sconn, session->compat)) {
+       if (!session_claim(smb2req->sconn, session)) {
                DEBUG(1, ("smb2: Failed to claim session "
                        "for vuid=%llu\n",
                        (unsigned long long)session->compat->vuid));
@@ -378,8 +439,22 @@ struct smbd_smb2_session_setup_state {
        uint16_t out_session_flags;
        DATA_BLOB out_security_buffer;
        uint64_t out_session_id;
+       /* The following pointer is owned by state->session. */
+       struct smbd_smb2_session_setup_state **pp_self_ref;
 };
 
+static int pp_self_ref_destructor(struct smbd_smb2_session_setup_state **pp_state)
+{
+       (*pp_state)->session = NULL;
+       /*
+        * To make things clearer, ensure the pp_self_ref
+        * pointer is nulled out. We're never going to
+        * access this again.
+        */
+       (*pp_state)->pp_self_ref = NULL;
+       return 0;
+}
+
 static int smbd_smb2_session_setup_state_destructor(struct smbd_smb2_session_setup_state *state)
 {
        /*
@@ -393,6 +468,24 @@ static int smbd_smb2_session_setup_state_destructor(struct smbd_smb2_session_set
 static void smbd_smb2_session_setup_gensec_done(struct tevent_req *subreq);
 static void smbd_smb2_session_setup_previous_done(struct tevent_req *subreq);
 
+/************************************************************************
+ We have to tag the state->session pointer with memory talloc'ed
+ on it to ensure it gets NULL'ed out if the underlying struct smbXsrv_session
+ is deleted by shutdown whilst this request is in flight.
+************************************************************************/
+
+static NTSTATUS tag_state_session_ptr(struct smbd_smb2_session_setup_state *state)
+{
+       state->pp_self_ref = talloc_zero(state->session,
+                       struct smbd_smb2_session_setup_state *);
+       if (state->pp_self_ref == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       *state->pp_self_ref = state;
+       talloc_set_destructor(state->pp_self_ref, pp_self_ref_destructor);
+       return NT_STATUS_OK;
+}
+
 static struct tevent_req *smbd_smb2_session_setup_send(TALLOC_CTX *mem_ctx,
                                        struct tevent_context *ev,
                                        struct smbd_smb2_request *smb2req,
@@ -461,6 +554,11 @@ static struct tevent_req *smbd_smb2_session_setup_send(TALLOC_CTX *mem_ctx,
                }
        }
 
+       status = tag_state_session_ptr(state);
+       if (tevent_req_nterror(req, status)) {
+               return tevent_req_post(req, ev);
+       }
+
        if (state->session->gensec == NULL) {
                status = auth_generic_prepare(state->session,
                                              state->session->connection->remote_address,
@@ -516,7 +614,7 @@ static void smbd_smb2_session_setup_gensec_done(struct tevent_req *subreq)
        if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
                state->out_session_id = state->session->global->session_wire_id;
                /* we want to keep the session */
-               state->session = NULL;
+               TALLOC_FREE(state->pp_self_ref);
                tevent_req_nterror(req, status);
                return;
        }
@@ -556,7 +654,7 @@ static void smbd_smb2_session_setup_gensec_done(struct tevent_req *subreq)
                        return;
                }
                /* we want to keep the session */
-               state->session = NULL;
+               TALLOC_FREE(state->pp_self_ref);
                tevent_req_done(req);
                return;
        }
@@ -572,7 +670,7 @@ static void smbd_smb2_session_setup_gensec_done(struct tevent_req *subreq)
        }
 
        /* we want to keep the session */
-       state->session = NULL;
+       TALLOC_FREE(state->pp_self_ref);
        tevent_req_done(req);
        return;
 }
@@ -603,7 +701,7 @@ static void smbd_smb2_session_setup_previous_done(struct tevent_req *subreq)
                        return;
                }
                /* we want to keep the session */
-               state->session = NULL;
+               TALLOC_FREE(state->pp_self_ref);
                tevent_req_done(req);
                return;
        }
@@ -619,7 +717,7 @@ static void smbd_smb2_session_setup_previous_done(struct tevent_req *subreq)
        }
 
        /* we want to keep the session */
-       state->session = NULL;
+       TALLOC_FREE(state->pp_self_ref);
        tevent_req_done(req);
        return;
 }