s3-ntlmssp Remove references to auth_ntlmssp_context from the smb sealing code
[abartlet/samba.git/.git] / source3 / smbd / seal.c
index 6b3f30f00bc115d71e419e5c406e5c8d273f09bc..66b3c9f4a010a91f01b36b107f9f6b6f12f02e64 100644 (file)
@@ -2,17 +2,17 @@
    Unix SMB/CIFS implementation.
    SMB Transport encryption (sealing) code - server code.
    Copyright (C) Jeremy Allison 2007.
-   
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 #include "smbd/smbd.h"
 #include "smbd/globals.h"
 #include "../libcli/auth/spnego.h"
-#include "../libcli/auth/ntlmssp.h"
+#include "../auth/ntlmssp/ntlmssp.h"
 #include "ntlmssp_wrap.h"
 #include "smb_crypt.h"
 #include "../lib/util/asn1.h"
+#include "auth.h"
+#include "libsmb/libsmb.h"
+#include "../lib/tsocket/tsocket.h"
+#include "auth/gensec/gensec.h"
 
 /******************************************************************************
  Server side encryption.
 
 struct smb_srv_trans_enc_ctx {
        struct smb_trans_enc_state *es;
-       struct auth_ntlmssp_state *auth_ntlmssp_state; /* Must be kept in sync with pointer in ec->ntlmssp_state. */
+       struct gensec_security *gensec_security; /* Must be kept in sync with pointer in ec->ntlmssp_state. */
 };
 
 /******************************************************************************
  Return global enc context - this must change if we ever do multiple contexts.
 ******************************************************************************/
 
-uint16_t srv_enc_ctx(void)
+static uint16_t srv_enc_ctx(const struct smb_srv_trans_enc_ctx *ec)
 {
-       return srv_trans_enc_ctx->es->enc_ctx_num;
+       return ec->es->enc_ctx_num;
 }
 
 /******************************************************************************
  Is this an incoming encrypted packet ?
 ******************************************************************************/
 
-bool is_encrypted_packet(const uint8_t *inbuf)
+bool is_encrypted_packet(struct smbd_server_connection *sconn,
+                        const uint8_t *inbuf)
 {
        NTSTATUS status;
        uint16_t enc_num;
@@ -70,7 +75,7 @@ bool is_encrypted_packet(const uint8_t *inbuf)
        }
 
        /* Encrypted messages are 0xFF'E'<ctx> */
-       if (srv_trans_enc_ctx && enc_num == srv_enc_ctx()) {
+       if (srv_trans_enc_ctx && enc_num == srv_enc_ctx(srv_trans_enc_ctx)) {
                return true;
        }
        return false;
@@ -80,10 +85,22 @@ bool is_encrypted_packet(const uint8_t *inbuf)
  Create an auth_ntlmssp_state and ensure pointer copy is correct.
 ******************************************************************************/
 
-static NTSTATUS make_auth_ntlmssp(struct smb_srv_trans_enc_ctx *ec)
+static NTSTATUS make_auth_ntlmssp(const struct tsocket_address *remote_address,
+                                 struct smb_srv_trans_enc_ctx *ec)
 {
-       NTSTATUS status = auth_ntlmssp_start(&ec->auth_ntlmssp_state);
+       struct auth_ntlmssp_state *auth_ntlmssp_state;
+       NTSTATUS status = auth_ntlmssp_prepare(remote_address,
+                                              &auth_ntlmssp_state);
+       if (!NT_STATUS_IS_OK(status)) {
+               return nt_status_squash(status);
+       }
+
+       gensec_want_feature(auth_ntlmssp_state->gensec_security, GENSEC_FEATURE_SEAL);
+
+       status = auth_ntlmssp_start(auth_ntlmssp_state);
+
        if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(auth_ntlmssp_state);
                return nt_status_squash(status);
        }
 
@@ -91,7 +108,14 @@ static NTSTATUS make_auth_ntlmssp(struct smb_srv_trans_enc_ctx *ec)
         * We must remember to update the pointer copy for the common
         * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
         */
-       ec->es->s.ntlmssp_state = auth_ntlmssp_get_ntlmssp_state(ec->auth_ntlmssp_state);
+       ec->es->s.gensec_security = ec->gensec_security;
+
+       /* We do not need the auth_ntlmssp layer any more, which was
+        * allocated on NULL, so promote gensec_security to the NULL
+        * context */
+       ec->gensec_security = talloc_move(NULL, &auth_ntlmssp_state->gensec_security);
+       TALLOC_FREE(auth_ntlmssp_state);
+       
        return status;
 }
 
@@ -106,10 +130,10 @@ static void destroy_auth_ntlmssp(struct smb_srv_trans_enc_ctx *ec)
         * functions after any auth_ntlmssp_start/auth_ntlmssp_end.
         */
 
-       if (ec->auth_ntlmssp_state) {
-               TALLOC_FREE(ec->auth_ntlmssp_state);
+       if (ec->gensec_security) {
+               TALLOC_FREE(ec->gensec_security);
                /* The auth_ntlmssp_end killed this already. */
-               ec->es->s.ntlmssp_state = NULL;
+               ec->es->s.gensec_security = NULL;
        }
 }
 
@@ -132,7 +156,7 @@ static NTSTATUS get_srv_gss_creds(const char *service,
        NTSTATUS status = NT_STATUS_OK;
 
        gss_OID_desc nt_hostbased_service =
-       {10, CONST_DISCARD(char *,"\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
+       {10, discard_const_p(char, "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04")};
 
        if (asprintf(&host_princ_s, "%s@%s", service, name) == -1) {
                return NT_STATUS_NO_MEMORY;
@@ -194,7 +218,7 @@ static NTSTATUS make_auth_gss(struct smb_srv_trans_enc_ctx *ec)
        gss_cred_id_t srv_cred;
        fstring fqdn;
 
-       name_to_fqdn(fqdn, global_myname());
+       name_to_fqdn(fqdn, lp_netbios_name());
        strlower_m(fqdn);
 
        status = get_srv_gss_creds("cifs", fqdn, GSS_C_ACCEPT, &srv_cred);
@@ -254,7 +278,9 @@ static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx **pp_ec)
  Create a server encryption context.
 ******************************************************************************/
 
-static NTSTATUS make_srv_encryption_context(enum smb_trans_enc_type smb_enc_type, struct smb_srv_trans_enc_ctx **pp_ec)
+static NTSTATUS make_srv_encryption_context(const struct tsocket_address *remote_address,
+                                           enum smb_trans_enc_type smb_enc_type,
+                                           struct smb_srv_trans_enc_ctx **pp_ec)
 {
        struct smb_srv_trans_enc_ctx *ec;
 
@@ -275,7 +301,8 @@ static NTSTATUS make_srv_encryption_context(enum smb_trans_enc_type smb_enc_type
        switch (smb_enc_type) {
                case SMB_TRANS_ENC_NTLM:
                        {
-                               NTSTATUS status = make_auth_ntlmssp(ec);
+                               NTSTATUS status = make_auth_ntlmssp(remote_address,
+                                                                   ec);
                                if (!NT_STATUS_IS_OK(status)) {
                                        srv_free_encryption_context(&ec);
                                        return status;
@@ -307,7 +334,7 @@ static NTSTATUS make_srv_encryption_context(enum smb_trans_enc_type smb_enc_type
  Free an encryption-allocated buffer.
 ******************************************************************************/
 
-void srv_free_enc_buffer(char *buf)
+void srv_free_enc_buffer(struct smbd_server_connection *sconn, char *buf)
 {
        /* We know this is an smb buffer, and we
         * didn't malloc, only copy, for a keepalive,
@@ -326,7 +353,7 @@ void srv_free_enc_buffer(char *buf)
  Decrypt an incoming buffer.
 ******************************************************************************/
 
-NTSTATUS srv_decrypt_buffer(char *buf)
+NTSTATUS srv_decrypt_buffer(struct smbd_server_connection *sconn, char *buf)
 {
        /* Ignore non-session messages. */
        if(CVAL(buf,0)) {
@@ -344,7 +371,8 @@ NTSTATUS srv_decrypt_buffer(char *buf)
  Encrypt an outgoing buffer. Return the encrypted pointer in buf_out.
 ******************************************************************************/
 
-NTSTATUS srv_encrypt_buffer(char *buf, char **buf_out)
+NTSTATUS srv_encrypt_buffer(struct smbd_server_connection *sconn, char *buf,
+                           char **buf_out)
 {
        *buf_out = buf;
 
@@ -366,7 +394,10 @@ NTSTATUS srv_encrypt_buffer(char *buf, char **buf_out)
 ******************************************************************************/
 
 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
-static NTSTATUS srv_enc_spnego_gss_negotiate(unsigned char **ppdata, size_t *p_data_size, DATA_BLOB secblob)
+static NTSTATUS srv_enc_spnego_gss_negotiate(const struct tsocket_address *remote_address,
+                                            unsigned char **ppdata,
+                                            size_t *p_data_size,
+                                            DATA_BLOB secblob)
 {
        OM_uint32 ret;
        OM_uint32 min;
@@ -378,7 +409,9 @@ static NTSTATUS srv_enc_spnego_gss_negotiate(unsigned char **ppdata, size_t *p_d
        NTSTATUS status;
 
        if (!partial_srv_trans_enc_ctx) {
-               status = make_srv_encryption_context(SMB_TRANS_ENC_GSS, &partial_srv_trans_enc_ctx);
+               status = make_srv_encryption_context(remote_address,
+                                                    SMB_TRANS_ENC_GSS,
+                                                    &partial_srv_trans_enc_ctx);
                if (!NT_STATUS_IS_OK(status)) {
                        return status;
                }
@@ -448,18 +481,26 @@ static NTSTATUS srv_enc_spnego_gss_negotiate(unsigned char **ppdata, size_t *p_d
  Until success we do everything on the partial enc ctx.
 ******************************************************************************/
 
-static NTSTATUS srv_enc_ntlm_negotiate(unsigned char **ppdata, size_t *p_data_size, DATA_BLOB secblob, bool spnego_wrap)
+static NTSTATUS srv_enc_ntlm_negotiate(const struct tsocket_address *remote_address,
+                                      unsigned char **ppdata,
+                                      size_t *p_data_size,
+                                      DATA_BLOB secblob,
+                                      bool spnego_wrap)
 {
        NTSTATUS status;
        DATA_BLOB chal = data_blob_null;
        DATA_BLOB response = data_blob_null;
 
-       status = make_srv_encryption_context(SMB_TRANS_ENC_NTLM, &partial_srv_trans_enc_ctx);
+       status = make_srv_encryption_context(remote_address,
+                                            SMB_TRANS_ENC_NTLM,
+                                            &partial_srv_trans_enc_ctx);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
-       status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, secblob, &chal);
+       status = gensec_update(partial_srv_trans_enc_ctx->gensec_security,
+                              partial_srv_trans_enc_ctx->gensec_security, NULL, 
+                              secblob, &chal);
 
        /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
         * for success ... */
@@ -515,14 +556,21 @@ static NTSTATUS srv_enc_spnego_negotiate(connection_struct *conn,
                TALLOC_FREE(kerb_mech);
 
 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
-               status = srv_enc_spnego_gss_negotiate(ppdata, p_data_size, secblob);
+               status = srv_enc_spnego_gss_negotiate(conn->sconn->remote_address,
+                                                     ppdata,
+                                                     p_data_size,
+                                                     secblob);
 #else
                /* Currently we don't SPNEGO negotiate
                 * back to NTLMSSP as we do in sessionsetupX. We should... */
                return NT_STATUS_LOGON_FAILURE;
 #endif
        } else {
-               status = srv_enc_ntlm_negotiate(ppdata, p_data_size, secblob, true);
+               status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
+                                               ppdata,
+                                               p_data_size,
+                                               secblob,
+                                               true);
        }
 
        data_blob_free(&secblob);
@@ -564,7 +612,7 @@ static NTSTATUS srv_enc_spnego_ntlm_auth(connection_struct *conn,
 
        /* We must have a partial context here. */
 
-       if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
+       if (!ec || !ec->es || ec->gensec_security == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
                srv_free_encryption_context(&partial_srv_trans_enc_ctx);
                return NT_STATUS_INVALID_PARAMETER;
        }
@@ -575,7 +623,7 @@ static NTSTATUS srv_enc_spnego_ntlm_auth(connection_struct *conn,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       status = auth_ntlmssp_update(ec->auth_ntlmssp_state, auth, &auth_reply);
+       status = gensec_update(ec->gensec_security, talloc_tos(), NULL, auth, &auth_reply);
        data_blob_free(&auth);
 
        /* From RFC4178.
@@ -626,7 +674,11 @@ static NTSTATUS srv_enc_raw_ntlm_auth(connection_struct *conn,
 
        if (!partial_srv_trans_enc_ctx) {
                /* This is the initial step. */
-               status = srv_enc_ntlm_negotiate(ppdata, p_data_size, blob, false);
+               status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
+                                               ppdata,
+                                               p_data_size,
+                                               blob,
+                                               false);
                if (!NT_STATUS_EQUAL(status,NT_STATUS_MORE_PROCESSING_REQUIRED) && !NT_STATUS_IS_OK(status)) {
                        srv_free_encryption_context(&partial_srv_trans_enc_ctx);
                        return nt_status_squash(status);
@@ -635,13 +687,15 @@ static NTSTATUS srv_enc_raw_ntlm_auth(connection_struct *conn,
        }
 
        ec = partial_srv_trans_enc_ctx;
-       if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
+       if (!ec || !ec->es || ec->gensec_security == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
                srv_free_encryption_context(&partial_srv_trans_enc_ctx);
                return NT_STATUS_INVALID_PARAMETER;
        }
 
        /* Second step. */
-       status = auth_ntlmssp_update(partial_srv_trans_enc_ctx->auth_ntlmssp_state, blob, &response);
+       status = gensec_update(partial_srv_trans_enc_ctx->gensec_security,
+                              talloc_tos(), NULL, 
+                              blob, &response);
 
        if (NT_STATUS_IS_OK(status)) {
                /* Return the context we're using for this encryption state. */
@@ -716,11 +770,11 @@ static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
        }
 
        if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
-               if (!auth_ntlmssp_negotiated_sign((ec->auth_ntlmssp_state))) {
+               if (!gensec_have_feature(ec->gensec_security, GENSEC_FEATURE_SIGN)) {
                        return NT_STATUS_INVALID_PARAMETER;
                }
 
-               if (!auth_ntlmssp_negotiated_seal((ec->auth_ntlmssp_state))) {
+               if (!gensec_have_feature(ec->gensec_security, GENSEC_FEATURE_SEAL)) {
                        return NT_STATUS_INVALID_PARAMETER;
                }
        }
@@ -759,7 +813,7 @@ NTSTATUS srv_encryption_start(connection_struct *conn)
  Shutdown all server contexts.
 ******************************************************************************/
 
-void server_encryption_shutdown(void)
+void server_encryption_shutdown(struct smbd_server_connection *sconn)
 {
        srv_free_encryption_context(&partial_srv_trans_enc_ctx);
        srv_free_encryption_context(&srv_trans_enc_ctx);