s3-auth rename auth_ntlmssp_state -> auth_generic_state
[mat/samba.git] / source3 / smbd / seal.c
index ea017e08d8034a261b0a741d2b020dd054ef6dd9..ab87d76b26dfd69f1f887e4b263a256ccdc52593 100644 (file)
@@ -2,59 +2,61 @@
    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 "includes.h"
+#include "smbd/smbd.h"
+#include "smbd/globals.h"
+#include "../libcli/auth/spnego.h"
+#include "../auth/ntlmssp/ntlmssp.h"
+#include "ntlmssp_wrap.h"
+#include "../libcli/smb/smb_seal.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.
 ******************************************************************************/
 
-/******************************************************************************
- Global server state.
-******************************************************************************/
-
-struct smb_srv_trans_enc_ctx {
-       struct smb_trans_enc_state *es;
-       AUTH_NTLMSSP_STATE *auth_ntlmssp_state; /* Must be kept in sync with pointer in ec->ntlmssp_state. */
-};
-
-static struct smb_srv_trans_enc_ctx *partial_srv_trans_enc_ctx;
-static struct smb_srv_trans_enc_ctx *srv_trans_enc_ctx;
-
 /******************************************************************************
  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_trans_enc_state *es)
 {
-       return srv_trans_enc_ctx->es->enc_ctx_num;
+       return 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;
 
        /* Ignore non-session messages or non 0xFF'E' messages. */
-       if(CVAL(inbuf,0) || !(inbuf[4] == 0xFF && inbuf[5] == 'E')) {
+       if(CVAL(inbuf,0)
+          || (smb_len(inbuf) < 8)
+          || !(inbuf[4] == 0xFF && inbuf[5] == 'E')) {
                return false;
        }
 
@@ -64,7 +66,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;
@@ -74,37 +76,32 @@ 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_trans_enc_state *es)
 {
-       NTSTATUS status = auth_ntlmssp_start(&ec->auth_ntlmssp_state);
+       struct auth_generic_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);
        }
 
-       /*
-        * 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 = ec->auth_ntlmssp_state->ntlmssp_state;
-       return status;
-}
+       gensec_want_feature(auth_ntlmssp_state->gensec_security, GENSEC_FEATURE_SEAL);
 
-/******************************************************************************
- Destroy an auth_ntlmssp_state and ensure pointer copy is correct.
-******************************************************************************/
+       status = auth_ntlmssp_start(auth_ntlmssp_state);
 
-static void destroy_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.
-        */
-
-       if (ec->auth_ntlmssp_state) {
-               auth_ntlmssp_end(&ec->auth_ntlmssp_state);
-               /* The auth_ntlmssp_end killed this already. */
-               ec->es->s.ntlmssp_state = NULL;
+       if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(auth_ntlmssp_state);
+               return nt_status_squash(status);
        }
+
+       /* We do not need the auth_ntlmssp layer any more, which was
+        * allocated on NULL, so promote gensec_security to the NULL
+        * context */
+       es->s.gensec_security = talloc_move(NULL, &auth_ntlmssp_state->gensec_security);
+       TALLOC_FREE(auth_ntlmssp_state);
+
+       return status;
 }
 
 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
@@ -126,10 +123,9 @@ 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")};
 
-       asprintf(&host_princ_s, "%s@%s", service, name);
-       if (host_princ_s == NULL) {
+       if (asprintf(&host_princ_s, "%s@%s", service, name) == -1) {
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -183,13 +179,13 @@ static NTSTATUS get_srv_gss_creds(const char *service,
  host/server@realm.
 ******************************************************************************/
 
-static NTSTATUS make_auth_gss(struct smb_srv_trans_enc_ctx *ec)
+static NTSTATUS make_auth_gss(struct smb_trans_enc_state *es)
 {
        NTSTATUS status;
        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);
@@ -200,17 +196,17 @@ static NTSTATUS make_auth_gss(struct smb_srv_trans_enc_ctx *ec)
                }
        }
 
-       ec->es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
-       if (!ec->es->s.gss_state) {
+       es->s.gss_state = SMB_MALLOC_P(struct smb_tran_enc_state_gss);
+       if (!es->s.gss_state) {
                OM_uint32 min;
                gss_release_cred(&min, &srv_cred);
                return NT_STATUS_NO_MEMORY;
        }
-       ZERO_STRUCTP(ec->es->s.gss_state);
-       ec->es->s.gss_state->creds = srv_cred;
+       ZERO_STRUCTP(es->s.gss_state);
+       es->s.gss_state->creds = srv_cred;
 
        /* No context yet. */
-       ec->es->s.gss_state->gss_ctx = GSS_C_NO_CONTEXT;
+       es->s.gss_state->gss_ctx = GSS_C_NO_CONTEXT;
 
        return NT_STATUS_OK;
 }
@@ -220,59 +216,46 @@ static NTSTATUS make_auth_gss(struct smb_srv_trans_enc_ctx *ec)
  Shutdown a server encryption context.
 ******************************************************************************/
 
-static void srv_free_encryption_context(struct smb_srv_trans_enc_ctx **pp_ec)
+static void srv_free_encryption_context(struct smb_trans_enc_state **pp_es)
 {
-       struct smb_srv_trans_enc_ctx *ec = *pp_ec;
+       struct smb_trans_enc_state *es = *pp_es;
 
-       if (!ec) {
+       if (!es) {
                return;
        }
 
-       if (ec->es) {
-               switch (ec->es->smb_enc_type) {
-                       case SMB_TRANS_ENC_NTLM:
-                               destroy_auth_ntlmssp(ec);
-                               break;
-#if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
-                       case SMB_TRANS_ENC_GSS:
-                               break;
-#endif
-               }
-               common_free_encryption_state(&ec->es);
-       }
+       common_free_encryption_state(&es);
 
-       SAFE_FREE(ec);
-       *pp_ec = NULL;
+       SAFE_FREE(es);
+       *pp_es = NULL;
 }
 
 /******************************************************************************
  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_trans_enc_state **pp_es)
 {
-       struct smb_srv_trans_enc_ctx *ec;
+       struct smb_trans_enc_state *es;
 
-       *pp_ec = NULL;
+       *pp_es = NULL;
 
-       ec = SMB_MALLOC_P(struct smb_srv_trans_enc_ctx);
-       if (!ec) {
-               return NT_STATUS_NO_MEMORY;
-       }
        ZERO_STRUCTP(partial_srv_trans_enc_ctx);
-       ec->es = SMB_MALLOC_P(struct smb_trans_enc_state);
-       if (!ec->es) {
-               SAFE_FREE(ec);
+       es = SMB_MALLOC_P(struct smb_trans_enc_state);
+       if (!es) {
                return NT_STATUS_NO_MEMORY;
        }
-       ZERO_STRUCTP(ec->es);
-       ec->es->smb_enc_type = smb_enc_type;
+       ZERO_STRUCTP(es);
+       es->smb_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,
+                                                                   es);
                                if (!NT_STATUS_IS_OK(status)) {
-                                       srv_free_encryption_context(&ec);
+                                       srv_free_encryption_context(&es);
                                        return status;
                                }
                        }
@@ -282,19 +265,19 @@ static NTSTATUS make_srv_encryption_context(enum smb_trans_enc_type smb_enc_type
                case SMB_TRANS_ENC_GSS:
                        /* Acquire our credentials by calling gss_acquire_cred here. */
                        {
-                               NTSTATUS status = make_auth_gss(ec);
+                               NTSTATUS status = make_auth_gss(es);
                                if (!NT_STATUS_IS_OK(status)) {
-                                       srv_free_encryption_context(&ec);
+                                       srv_free_encryption_context(&es);
                                        return status;
                                }
                        }
                        break;
 #endif
                default:
-                       srv_free_encryption_context(&ec);
+                       srv_free_encryption_context(&es);
                        return NT_STATUS_INVALID_PARAMETER;
        }
-       *pp_ec = ec;
+       *pp_es = es;
        return NT_STATUS_OK;
 }
 
@@ -302,7 +285,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,
@@ -313,7 +296,7 @@ void srv_free_enc_buffer(char *buf)
        }
 
        if (srv_trans_enc_ctx) {
-               common_free_enc_buffer(srv_trans_enc_ctx->es, buf);
+               common_free_enc_buffer(srv_trans_enc_ctx, buf);
        }
 }
 
@@ -321,7 +304,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)) {
@@ -329,7 +312,7 @@ NTSTATUS srv_decrypt_buffer(char *buf)
        }
 
        if (srv_trans_enc_ctx) {
-               return common_decrypt_buffer(srv_trans_enc_ctx->es, buf);
+               return common_decrypt_buffer(srv_trans_enc_ctx, buf);
        }
 
        return NT_STATUS_OK;
@@ -339,7 +322,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;
 
@@ -349,7 +333,7 @@ NTSTATUS srv_encrypt_buffer(char *buf, char **buf_out)
        }
 
        if (srv_trans_enc_ctx) {
-               return common_encrypt_buffer(srv_trans_enc_ctx->es, buf, buf_out);
+               return common_encrypt_buffer(srv_trans_enc_ctx, buf, buf_out);
        }
        /* Not encrypting. */
        return NT_STATUS_OK;
@@ -361,7 +345,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;
@@ -373,13 +360,15 @@ 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;
                }
        }
 
-       gss_state = partial_srv_trans_enc_ctx->es->s.gss_state;
+       gss_state = partial_srv_trans_enc_ctx->s.gss_state;
 
        in_buf.value = secblob.data;
        in_buf.length = secblob.length;
@@ -422,13 +411,18 @@ static NTSTATUS srv_enc_spnego_gss_negotiate(unsigned char **ppdata, size_t *p_d
        gss_release_buffer(&min, &out_buf);
 
        /* Wrap in SPNEGO. */
-       response = spnego_gen_auth_response(&auth_reply, status, OID_KERBEROS5);
+       response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, OID_KERBEROS5);
        data_blob_free(&auth_reply);
 
        SAFE_FREE(*ppdata);
-       *ppdata = response.data;
+       *ppdata = (unsigned char *)memdup(response.data, response.length);
+       if ((*ppdata) == NULL && response.length > 0) {
+               status = NT_STATUS_NO_MEMORY;
+       }
        *p_data_size = response.length;
 
+       data_blob_free(&response);
+
        return status;
 }
 #endif
@@ -438,24 +432,32 @@ 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->s.gensec_security,
+                              talloc_tos(), NULL,
+                              secblob, &chal);
 
        /* status here should be NT_STATUS_MORE_PROCESSING_REQUIRED
         * for success ... */
 
        if (spnego_wrap) {
-               response = spnego_gen_auth_response(&chal, status, OID_NTLMSSP);
+               response = spnego_gen_auth_response(talloc_tos(), &chal, status, OID_NTLMSSP);
                data_blob_free(&chal);
        } else {
                /* Return the raw blob. */
@@ -463,8 +465,13 @@ static NTSTATUS srv_enc_ntlm_negotiate(unsigned char **ppdata, size_t *p_data_si
        }
 
        SAFE_FREE(*ppdata);
-       *ppdata = response.data;
+       *ppdata = (unsigned char *)memdup(response.data, response.length);
+       if ((*ppdata) == NULL && response.length > 0) {
+               status = NT_STATUS_NO_MEMORY;
+       }
        *p_data_size = response.length;
+       data_blob_free(&response);
+
        return status;
 }
 
@@ -483,11 +490,11 @@ static NTSTATUS srv_enc_spnego_negotiate(connection_struct *conn,
        NTSTATUS status;
        DATA_BLOB blob = data_blob_null;
        DATA_BLOB secblob = data_blob_null;
-       bool got_kerberos_mechanism = false;
+       char *kerb_mech = NULL;
 
        blob = data_blob_const(*ppdata, *p_data_size);
 
-       status = parse_spnego_mechanisms(blob, &secblob, &got_kerberos_mechanism);
+       status = parse_spnego_mechanisms(talloc_tos(), blob, &secblob, &kerb_mech);
        if (!NT_STATUS_IS_OK(status)) {
                return nt_status_squash(status);
        }
@@ -496,13 +503,25 @@ static NTSTATUS srv_enc_spnego_negotiate(connection_struct *conn,
 
        srv_free_encryption_context(&partial_srv_trans_enc_ctx);
 
+       if (kerb_mech) {
+               TALLOC_FREE(kerb_mech);
+
 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
-       if (got_kerberos_mechanism && lp_use_kerberos_keytab() ) {
-               status = srv_enc_spnego_gss_negotiate(ppdata, p_data_size, secblob);
-       } else 
+               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
-       {
-               status = srv_enc_ntlm_negotiate(ppdata, p_data_size, secblob, true);
+       } else {
+               status = srv_enc_ntlm_negotiate(conn->sconn->remote_address,
+                                               ppdata,
+                                               p_data_size,
+                                               secblob,
+                                               true);
        }
 
        data_blob_free(&secblob);
@@ -517,7 +536,7 @@ static NTSTATUS srv_enc_spnego_negotiate(connection_struct *conn,
                if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
                        return NT_STATUS_NO_MEMORY;
                }
-               SSVAL(*pparam,0,partial_srv_trans_enc_ctx->es->enc_ctx_num);
+               SSVAL(*pparam,0,partial_srv_trans_enc_ctx->enc_ctx_num);
                *p_param_size = 2;
        }
 
@@ -540,25 +559,34 @@ static NTSTATUS srv_enc_spnego_ntlm_auth(connection_struct *conn,
        DATA_BLOB auth = data_blob_null;
        DATA_BLOB auth_reply = data_blob_null;
        DATA_BLOB response = data_blob_null;
-       struct smb_srv_trans_enc_ctx *ec = partial_srv_trans_enc_ctx;
+       struct smb_trans_enc_state *es = partial_srv_trans_enc_ctx;
 
        /* 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 (!es || es->s.gensec_security == NULL || es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
                srv_free_encryption_context(&partial_srv_trans_enc_ctx);
                return NT_STATUS_INVALID_PARAMETER;
        }
 
        blob = data_blob_const(*ppdata, *p_data_size);
-       if (!spnego_parse_auth(blob, &auth)) {
+       if (!spnego_parse_auth(talloc_tos(), blob, &auth)) {
                srv_free_encryption_context(&partial_srv_trans_enc_ctx);
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       status = auth_ntlmssp_update(ec->auth_ntlmssp_state, auth, &auth_reply);
+       status = gensec_update(es->s.gensec_security, talloc_tos(), NULL, auth, &auth_reply);
        data_blob_free(&auth);
 
-       response = spnego_gen_auth_response(&auth_reply, status, OID_NTLMSSP);
+       /* From RFC4178.
+        *
+        *    supportedMech
+        *
+        *          This field SHALL only be present in the first reply from the
+        *                target.
+        * So set mechOID to NULL here.
+        */
+
+       response = spnego_gen_auth_response(talloc_tos(), &auth_reply, status, NULL);
        data_blob_free(&auth_reply);
 
        if (NT_STATUS_IS_OK(status)) {
@@ -566,13 +594,16 @@ static NTSTATUS srv_enc_spnego_ntlm_auth(connection_struct *conn,
                if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
                        return NT_STATUS_NO_MEMORY;
                }
-               SSVAL(*pparam,0,ec->es->enc_ctx_num);
+               SSVAL(*pparam,0,es->enc_ctx_num);
                *p_param_size = 2;
        }
 
        SAFE_FREE(*ppdata);
-       *ppdata = response.data;
+       *ppdata = (unsigned char *)memdup(response.data, response.length);
+       if ((*ppdata) == NULL && response.length > 0)
+               return NT_STATUS_NO_MEMORY;
        *p_data_size = response.length;
+       data_blob_free(&response);
        return status;
 }
 
@@ -590,11 +621,15 @@ static NTSTATUS srv_enc_raw_ntlm_auth(connection_struct *conn,
        NTSTATUS status;
        DATA_BLOB blob = data_blob_const(*ppdata, *p_data_size);
        DATA_BLOB response = data_blob_null;
-       struct smb_srv_trans_enc_ctx *ec;
+       struct smb_trans_enc_state *es;
 
        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);
@@ -602,28 +637,33 @@ static NTSTATUS srv_enc_raw_ntlm_auth(connection_struct *conn,
                return status;
        }
 
-       ec = partial_srv_trans_enc_ctx;
-       if (!ec || !ec->es || ec->auth_ntlmssp_state == NULL || ec->es->smb_enc_type != SMB_TRANS_ENC_NTLM) {
+       es = partial_srv_trans_enc_ctx;
+       if (!es || es->s.gensec_security == NULL || 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->s.gensec_security,
+                              talloc_tos(), NULL,
+                              blob, &response);
 
        if (NT_STATUS_IS_OK(status)) {
                /* Return the context we're using for this encryption state. */
                if (!(*pparam = SMB_MALLOC_ARRAY(unsigned char, 2))) {
                        return NT_STATUS_NO_MEMORY;
                }
-               SSVAL(*pparam,0,ec->es->enc_ctx_num);
+               SSVAL(*pparam, 0, es->enc_ctx_num);
                *p_param_size = 2;
        }
 
        /* Return the raw blob. */
        SAFE_FREE(*ppdata);
-       *ppdata = response.data;
+       *ppdata = (unsigned char *)memdup(response.data, response.length);
+       if ((*ppdata) == NULL && response.length > 0)
+               return NT_STATUS_NO_MEMORY;
        *p_data_size = response.length;
+       data_blob_free(&response);
        return status;
 }
 
@@ -674,15 +714,18 @@ NTSTATUS srv_request_encryption_setup(connection_struct *conn,
  Negotiation was successful - turn on server-side encryption.
 ******************************************************************************/
 
-static NTSTATUS check_enc_good(struct smb_srv_trans_enc_ctx *ec)
+static NTSTATUS check_enc_good(struct smb_trans_enc_state *es)
 {
-       if (!ec || !ec->es) {
+       if (!es) {
                return NT_STATUS_LOGON_FAILURE;
        }
 
-       if (ec->es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
-               if ((ec->es->s.ntlmssp_state->neg_flags & (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL)) !=
-                               (NTLMSSP_NEGOTIATE_SIGN|NTLMSSP_NEGOTIATE_SEAL)) {
+       if (es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
+               if (!gensec_have_feature(es->s.gensec_security, GENSEC_FEATURE_SIGN)) {
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
+
+               if (!gensec_have_feature(es->s.gensec_security, GENSEC_FEATURE_SEAL)) {
                        return NT_STATUS_INVALID_PARAMETER;
                }
        }
@@ -709,7 +752,7 @@ NTSTATUS srv_encryption_start(connection_struct *conn)
 
        /* Steal the partial pointer. Deliberate shallow copy. */
        srv_trans_enc_ctx = partial_srv_trans_enc_ctx;
-       srv_trans_enc_ctx->es->enc_on = true;
+       srv_trans_enc_ctx->enc_on = true;
 
        partial_srv_trans_enc_ctx = NULL;
 
@@ -721,7 +764,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);