r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[ira/wip.git] / source3 / libsmb / ntlmssp.c
index 82eafc4cd53045848af14ed85d67eef51846ce3d..0c0744867dc37a2e0b3e32ffbb84ab972fe0a726 100644 (file)
@@ -5,10 +5,11 @@
 
    Copyright (C) Andrew Tridgell      2001
    Copyright (C) Andrew Bartlett 2001-2003
+   Copyright (C) Andrew Bartlett 2005 (Updated from gensec).
 
    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 2 of the License, or
+   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,
@@ -17,8 +18,7 @@
    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, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
@@ -71,6 +71,8 @@ void debug_ntlmssp_flags(uint32 neg_flags)
                DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_SIGN\n"));
        if (neg_flags & NTLMSSP_NEGOTIATE_SEAL) 
                DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_SEAL\n"));
+       if (neg_flags & NTLMSSP_NEGOTIATE_DATAGRAM_STYLE)
+               DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_DATAGRAM_STYLE\n"));
        if (neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) 
                DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_LM_KEY\n"));
        if (neg_flags & NTLMSSP_NEGOTIATE_NETWARE) 
@@ -85,6 +87,10 @@ void debug_ntlmssp_flags(uint32 neg_flags)
                DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_THIS_IS_LOCAL_CALL\n"));
        if (neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN) 
                DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_ALWAYS_SIGN\n"));
+       if (neg_flags & NTLMSSP_CHAL_ACCEPT_RESPONSE)
+               DEBUGADD(4, ("  NTLMSSP_CHAL_ACCEPT_RESPONSE\n"));
+       if (neg_flags & NTLMSSP_CHAL_NON_NT_SESSION_KEY)
+               DEBUGADD(4, ("  NTLMSSP_CHAL_NON_NT_SESSION_KEY\n"));
        if (neg_flags & NTLMSSP_NEGOTIATE_NTLM2) 
                DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_NTLM2\n"));
        if (neg_flags & NTLMSSP_CHAL_TARGET_INFO) 
@@ -93,6 +99,8 @@ void debug_ntlmssp_flags(uint32 neg_flags)
                DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_128\n"));
        if (neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) 
                DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_KEY_EXCH\n"));
+       if (neg_flags & NTLMSSP_NEGOTIATE_56)
+               DEBUGADD(4, ("  NTLMSSP_NEGOTIATE_56\n"));
 }
 
 /**
@@ -103,7 +111,7 @@ void debug_ntlmssp_flags(uint32 neg_flags)
 static const uint8 *get_challenge(const struct ntlmssp_state *ntlmssp_state)
 {
        static uchar chal[8];
-       generate_random_buffer(chal, sizeof(chal), False);
+       generate_random_buffer(chal, sizeof(chal));
 
        return chal;
 }
@@ -138,7 +146,7 @@ static NTSTATUS set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *ch
 
 NTSTATUS ntlmssp_set_username(NTLMSSP_STATE *ntlmssp_state, const char *user) 
 {
-       ntlmssp_state->user = talloc_strdup(ntlmssp_state->mem_ctx, user);
+       ntlmssp_state->user = talloc_strdup(ntlmssp_state->mem_ctx, user ? user : "" );
        if (!ntlmssp_state->user) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -146,18 +154,41 @@ NTSTATUS ntlmssp_set_username(NTLMSSP_STATE *ntlmssp_state, const char *user)
 }
 
 /** 
- * Set a password on an NTLMSSP context - ensures it is talloc()ed 
+ * Store NT and LM hashes on an NTLMSSP context - ensures they are talloc()ed 
+ *
+ */
+NTSTATUS ntlmssp_set_hashes(NTLMSSP_STATE *ntlmssp_state,
+               const unsigned char lm_hash[16],
+               const unsigned char nt_hash[16]) 
+{
+       ntlmssp_state->lm_hash = (unsigned char *)
+               TALLOC_MEMDUP(ntlmssp_state->mem_ctx, lm_hash, 16);
+       ntlmssp_state->nt_hash = (unsigned char *)
+               TALLOC_MEMDUP(ntlmssp_state->mem_ctx, nt_hash, 16);
+       if (!ntlmssp_state->lm_hash || !ntlmssp_state->nt_hash) {
+               TALLOC_FREE(ntlmssp_state->lm_hash);
+               TALLOC_FREE(ntlmssp_state->nt_hash);
+               return NT_STATUS_NO_MEMORY;
+       }
+       return NT_STATUS_OK;
+}
+
+/** 
+ * Converts a password to the hashes on an NTLMSSP context.
  *
  */
 NTSTATUS ntlmssp_set_password(NTLMSSP_STATE *ntlmssp_state, const char *password) 
 {
        if (!password) {
-               ntlmssp_state->password = NULL;
+               ntlmssp_state->lm_hash = NULL;
+               ntlmssp_state->nt_hash = NULL;
        } else {
-               ntlmssp_state->password = talloc_strdup(ntlmssp_state->mem_ctx, password);
-               if (!ntlmssp_state->password) {
-                       return NT_STATUS_NO_MEMORY;
-               }
+               unsigned char lm_hash[16];
+               unsigned char nt_hash[16];
+
+               E_deshash(password, lm_hash);
+               E_md4hash(password, nt_hash);
+               return ntlmssp_set_hashes(ntlmssp_state, lm_hash, nt_hash);
        }
        return NT_STATUS_OK;
 }
@@ -168,9 +199,7 @@ NTSTATUS ntlmssp_set_password(NTLMSSP_STATE *ntlmssp_state, const char *password
  */
 NTSTATUS ntlmssp_set_domain(NTLMSSP_STATE *ntlmssp_state, const char *domain) 
 {
-       /* Possibly make our NTLMv2 client more robust by always having 
-          an uppercase domain */
-       ntlmssp_state->domain = talloc_strdup_upper(ntlmssp_state->mem_ctx, domain);
+       ntlmssp_state->domain = talloc_strdup(ntlmssp_state->mem_ctx, domain ? domain : "" );
        if (!ntlmssp_state->domain) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -184,7 +213,7 @@ NTSTATUS ntlmssp_set_domain(NTLMSSP_STATE *ntlmssp_state, const char *domain)
 NTSTATUS ntlmssp_set_workstation(NTLMSSP_STATE *ntlmssp_state, const char *workstation) 
 {
        ntlmssp_state->workstation = talloc_strdup(ntlmssp_state->mem_ctx, workstation);
-       if (!ntlmssp_state->domain) {
+       if (!ntlmssp_state->workstation) {
                return NT_STATUS_NO_MEMORY;
        }
        return NT_STATUS_OK;
@@ -203,6 +232,50 @@ NTSTATUS ntlmssp_store_response(NTLMSSP_STATE *ntlmssp_state,
        return NT_STATUS_OK;
 }
 
+/**
+ * Request features for the NTLMSSP negotiation
+ *
+ * @param ntlmssp_state NTLMSSP state
+ * @param feature_list List of space seperated features requested from NTLMSSP.
+ */
+void ntlmssp_want_feature_list(NTLMSSP_STATE *ntlmssp_state, char *feature_list)
+{
+       /*
+        * We need to set this to allow a later SetPassword
+        * via the SAMR pipe to succeed. Strange.... We could
+        * also add  NTLMSSP_NEGOTIATE_SEAL here. JRA.
+        */
+       if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list, True)) {
+               ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
+       }
+       if (in_list("NTLMSSP_FEATURE_SIGN", feature_list, True)) {
+               ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
+       }
+       if(in_list("NTLMSSP_FEATURE_SEAL", feature_list, True)) {
+               ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
+       }
+}
+
+/**
+ * Request a feature for the NTLMSSP negotiation
+ *
+ * @param ntlmssp_state NTLMSSP state
+ * @param feature Bit flag specifying the requested feature
+ */
+void ntlmssp_want_feature(NTLMSSP_STATE *ntlmssp_state, uint32 feature)
+{
+       /* As per JRA's comment above */
+       if (feature & NTLMSSP_FEATURE_SESSION_KEY) {
+               ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
+       }
+       if (feature & NTLMSSP_FEATURE_SIGN) {
+               ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
+       }
+       if (feature & NTLMSSP_FEATURE_SEAL) {
+               ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
+       }
+}
 /**
  * Next state function for the NTLMSSP state machine
  * 
@@ -219,13 +292,19 @@ NTSTATUS ntlmssp_update(NTLMSSP_STATE *ntlmssp_state,
        uint32 ntlmssp_command;
        int i;
 
-       *out = data_blob(NULL, 0);
+       if (ntlmssp_state->expected_state == NTLMSSP_DONE) {
+               /* Called update after negotiations finished. */
+               DEBUG(1, ("Called NTLMSSP after state machine was 'done'\n"));
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       *out = data_blob_null;
 
        if (!in.length && ntlmssp_state->stored_response.length) {
                input = ntlmssp_state->stored_response;
                
                /* we only want to read the stored response once - overwrite it */
-               ntlmssp_state->stored_response = data_blob(NULL, 0);
+               ntlmssp_state->stored_response = data_blob_null;
        } else {
                input = in;
        }
@@ -245,7 +324,7 @@ NTSTATUS ntlmssp_update(NTLMSSP_STATE *ntlmssp_state,
                                 "NTLMSSP",
                                 &ntlmssp_command)) {
                        DEBUG(1, ("Failed to parse NTLMSSP packet, could not extract NTLMSSP command\n"));
-                       dump_data(2, (const char *)input.data, input.length);
+                       dump_data(2, input.data, input.length);
                        return NT_STATUS_INVALID_PARAMETER;
                }
        }
@@ -340,8 +419,8 @@ static void ntlmssp_handle_neg_flags(struct ntlmssp_state *ntlmssp_state,
                ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
        }
 
-       if (neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN) {
-               ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
+       if (!(neg_flags & NTLMSSP_NEGOTIATE_ALWAYS_SIGN)) {
+               ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
        }
 
        if (!(neg_flags & NTLMSSP_NEGOTIATE_NTLM2)) {
@@ -352,16 +431,73 @@ static void ntlmssp_handle_neg_flags(struct ntlmssp_state *ntlmssp_state,
                ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_128;
        }
 
+       if (!(neg_flags & NTLMSSP_NEGOTIATE_56)) {
+               ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_56;
+       }
+
        if (!(neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH)) {
                ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_KEY_EXCH;
        }
 
+       if (!(neg_flags & NTLMSSP_NEGOTIATE_SIGN)) {
+               ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SIGN;
+       }
+
+       if (!(neg_flags & NTLMSSP_NEGOTIATE_SEAL)) {
+               ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_SEAL;
+       }
+
+       /* Woop Woop - unknown flag for Windows compatibility...
+          What does this really do ? JRA. */
+       if (!(neg_flags & NTLMSSP_UNKNOWN_02000000)) {
+               ntlmssp_state->neg_flags &= ~NTLMSSP_UNKNOWN_02000000;
+       }
+
        if ((neg_flags & NTLMSSP_REQUEST_TARGET)) {
                ntlmssp_state->neg_flags |= NTLMSSP_REQUEST_TARGET;
        }
-       
 }
 
+/**
+ Weaken NTLMSSP keys to cope with down-level clients and servers.
+
+ We probably should have some parameters to control this, but as
+ it only occours for LM_KEY connections, and this is controlled
+ by the client lanman auth/lanman auth parameters, it isn't too bad.
+*/
+
+DATA_BLOB ntlmssp_weaken_keys(NTLMSSP_STATE *ntlmssp_state, TALLOC_CTX *mem_ctx)
+{
+       DATA_BLOB weakened_key = data_blob_talloc(mem_ctx,
+                                       ntlmssp_state->session_key.data,
+                                       ntlmssp_state->session_key.length);
+
+       /* Nothing to weaken.  We certainly don't want to 'extend' the length... */
+       if (weakened_key.length < 16) {
+               /* perhaps there was no key? */
+               return weakened_key;
+       }
+
+       /* Key weakening not performed on the master key for NTLM2
+          and does not occour for NTLM1.  Therefore we only need
+          to do this for the LM_KEY.
+       */
+
+       if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
+               /* LM key doesn't support 128 bit crypto, so this is
+                * the best we can do.  If you negotiate 128 bit, but
+                * not 56, you end up with 40 bit... */
+               if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_56) {
+                       weakened_key.data[7] = 0xa0;
+               } else { /* forty bits */
+                       weakened_key.data[5] = 0xe5;
+                       weakened_key.data[6] = 0x38;
+                       weakened_key.data[7] = 0xb0;
+               }
+               weakened_key.length = 8;
+       }
+       return weakened_key;
+}
 
 /**
  * Next state function for the Negotiate packet
@@ -379,7 +515,6 @@ static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
        fstring dnsname, dnsdomname;
        uint32 neg_flags = 0;
        uint32 ntlmssp_command, chal_flags;
-       char *cliname=NULL, *domname=NULL;
        const uint8 *cryptkey;
        const char *target_name;
 
@@ -389,20 +524,15 @@ static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
 #endif
 
        if (request.length) {
-               if (!msrpc_parse(&request, "CddAA",
-                                "NTLMSSP",
-                                &ntlmssp_command,
-                                &neg_flags,
-                                &cliname,
-                                &domname)) {
-                       DEBUG(1, ("ntlmssp_server_negotiate: failed to parse NTLMSSP:\n"));
-                       dump_data(2, (const char *)request.data, request.length);
+               if ((request.length < 16) || !msrpc_parse(&request, "Cdd",
+                                                       "NTLMSSP",
+                                                       &ntlmssp_command,
+                                                       &neg_flags)) {
+                       DEBUG(1, ("ntlmssp_server_negotiate: failed to parse NTLMSSP Negotiate of length %u\n",
+                               (unsigned int)request.length));
+                       dump_data(2, request.data, request.length);
                        return NT_STATUS_INVALID_PARAMETER;
                }
-               
-               SAFE_FREE(cliname);
-               SAFE_FREE(domname);
-               
                debug_ntlmssp_flags(neg_flags);
        }
        
@@ -444,13 +574,6 @@ static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
        /* This creates the 'blob' of names that appears at the end of the packet */
        if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) 
        {
-               const char *target_name_dns = "";
-               if (chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN) {
-                       target_name_dns = dnsdomname;
-               } else if (chal_flags |= NTLMSSP_TARGET_TYPE_SERVER) {
-                       target_name_dns = dnsname;
-               }
-
                msrpc_gen(&struct_blob, "aaaaa",
                          NTLMSSP_NAME_TYPE_DOMAIN, target_name,
                          NTLMSSP_NAME_TYPE_SERVER, ntlmssp_state->get_global_myname(),
@@ -458,7 +581,7 @@ static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
                          NTLMSSP_NAME_TYPE_SERVER_DNS, dnsname,
                          0, "");
        } else {
-               struct_blob = data_blob(NULL, 0);
+               struct_blob = data_blob_null;
        }
 
        {
@@ -499,12 +622,12 @@ static NTSTATUS ntlmssp_server_negotiate(struct ntlmssp_state *ntlmssp_state,
 static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
                                    const DATA_BLOB request, DATA_BLOB *reply) 
 {
-       DATA_BLOB encrypted_session_key = data_blob(NULL, 0);
-       DATA_BLOB nt_session_key = data_blob(NULL, 0);
-       DATA_BLOB lm_session_key = data_blob(NULL, 0);
-       DATA_BLOB session_key = data_blob(NULL, 0);
+       DATA_BLOB encrypted_session_key = data_blob_null;
+       DATA_BLOB user_session_key = data_blob_null;
+       DATA_BLOB lm_session_key = data_blob_null;
+       DATA_BLOB session_key = data_blob_null;
        uint32 ntlmssp_command, auth_flags;
-       NTSTATUS nt_status;
+       NTSTATUS nt_status = NT_STATUS_OK;
 
        /* used by NTLM2 */
        BOOL doing_ntlm2 = False;
@@ -518,7 +641,7 @@ static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
        char *workstation = NULL;
 
        /* parse the NTLMSSP packet */
-       *reply = data_blob(NULL, 0);
+       *reply = data_blob_null;
 
 #if 0
        file_save("ntlmssp_auth.dat", request.data, request.length);
@@ -548,8 +671,6 @@ static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
                         &workstation,
                         &encrypted_session_key,
                         &auth_flags)) {
-               DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP:\n"));
-               dump_data(2, (const char *)request.data, request.length);
                SAFE_FREE(domain);
                SAFE_FREE(user);
                SAFE_FREE(workstation);
@@ -572,8 +693,8 @@ static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
                                 &domain, 
                                 &user, 
                                 &workstation)) {
-                       DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP:\n"));
-                       dump_data(2, (const char *)request.data, request.length);
+                       DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP (tried both formats):\n"));
+                       dump_data(2, request.data, request.length);
                        SAFE_FREE(domain);
                        SAFE_FREE(user);
                        SAFE_FREE(workstation);
@@ -650,6 +771,9 @@ static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
                                data_blob_free(&encrypted_session_key);
                                return nt_status;
                        }
+
+                       /* LM Key is incompatible. */
+                       ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
                }
        }
 
@@ -663,47 +787,56 @@ static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
        /* Finally, actually ask if the password is OK */
 
        if (!NT_STATUS_IS_OK(nt_status = ntlmssp_state->check_password(ntlmssp_state, 
-                                                                      &nt_session_key, &lm_session_key))) {
+                                                                      &user_session_key, &lm_session_key))) {
                data_blob_free(&encrypted_session_key);
                return nt_status;
        }
 
-       dump_data_pw("NT session key:\n", nt_session_key.data, nt_session_key.length);
+       dump_data_pw("NT session key:\n", user_session_key.data, user_session_key.length);
        dump_data_pw("LM first-8:\n", lm_session_key.data, lm_session_key.length);
 
        /* Handle the different session key derivation for NTLM2 */
        if (doing_ntlm2) {
-               if (nt_session_key.data && nt_session_key.length == 16) {
+               if (user_session_key.data && user_session_key.length == 16) {
                        session_key = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 16);
-                       hmac_md5(nt_session_key.data, session_nonce, 
+                       hmac_md5(user_session_key.data, session_nonce, 
                                 sizeof(session_nonce), session_key.data);
                        DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
                        dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
                        
                } else {
                        DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
-                       session_key = data_blob(NULL, 0);
+                       session_key = data_blob_null;
                }
        } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) {
                if (lm_session_key.data && lm_session_key.length >= 8) {
                        if (ntlmssp_state->lm_resp.data && ntlmssp_state->lm_resp.length == 24) {
                                session_key = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 16);
+                               if (session_key.data == NULL) {
+                                       return NT_STATUS_NO_MEMORY;
+                               }
                                SMBsesskeygen_lm_sess_key(lm_session_key.data, ntlmssp_state->lm_resp.data, 
                                                          session_key.data);
                                DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
-                               dump_data_pw("LM session key:\n", session_key.data, session_key.length);
                        } else {
-                               /* use the key unmodified - it's
-                                * probably a NULL key from the guest
-                                * login */
-                               session_key = lm_session_key;
+                               static const uint8 zeros[24] = { 0, };
+                               session_key = data_blob_talloc(
+                                       ntlmssp_state->mem_ctx, NULL, 16);
+                               if (session_key.data == NULL) {
+                                       return NT_STATUS_NO_MEMORY;
+                               }
+                               SMBsesskeygen_lm_sess_key(
+                                       lm_session_key.data, zeros,
+                                       session_key.data);
                        }
+                       dump_data_pw("LM session key:\n", session_key.data,
+                                    session_key.length);
                } else {
                        DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
-                       session_key = data_blob(NULL, 0);
+                       session_key = data_blob_null;
                }
-       } else if (nt_session_key.data) {
-               session_key = nt_session_key;
+       } else if (user_session_key.data) {
+               session_key = user_session_key;
                DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
                dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
        } else if (lm_session_key.data) {
@@ -712,7 +845,7 @@ static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
                dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
        } else {
                DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
-               session_key = data_blob(NULL, 0);
+               session_key = data_blob_null;
        }
 
        /* With KEY_EXCH, the client supplies the proposed session key, 
@@ -721,11 +854,11 @@ static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
                if (!encrypted_session_key.data || encrypted_session_key.length != 16) {
                        data_blob_free(&encrypted_session_key);
                        DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n", 
-                                 encrypted_session_key.length));
+                                 (unsigned int)encrypted_session_key.length));
                        return NT_STATUS_INVALID_PARAMETER;
                } else if (!session_key.data || session_key.length != 16) {
                        DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n", 
-                                 session_key.length));
+                                 (unsigned int)session_key.length));
                        ntlmssp_state->session_key = session_key;
                } else {
                        dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
@@ -743,15 +876,15 @@ static NTSTATUS ntlmssp_server_auth(struct ntlmssp_state *ntlmssp_state,
        }
 
        if (!NT_STATUS_IS_OK(nt_status)) {
-               ntlmssp_state->session_key = data_blob(NULL, 0);
+               ntlmssp_state->session_key = data_blob_null;
        } else if (ntlmssp_state->session_key.length) {
                nt_status = ntlmssp_sign_init(ntlmssp_state);
        }
 
        data_blob_free(&encrypted_session_key);
        
-       /* allow arbitarily many authentications */
-       ntlmssp_state->expected_state = NTLMSSP_AUTH;
+       /* Only one authentication allowed per server state. */
+       ntlmssp_state->expected_state = NTLMSSP_DONE;
 
        return nt_status;
 }
@@ -768,7 +901,7 @@ NTSTATUS ntlmssp_server_start(NTLMSSP_STATE **ntlmssp_state)
 
        mem_ctx = talloc_init("NTLMSSP context");
        
-       *ntlmssp_state = talloc_zero(mem_ctx, sizeof(**ntlmssp_state));
+       *ntlmssp_state = TALLOC_ZERO_P(mem_ctx, NTLMSSP_STATE);
        if (!*ntlmssp_state) {
                DEBUG(0,("ntlmssp_server_start: talloc failed!\n"));
                talloc_destroy(mem_ctx);
@@ -792,10 +925,14 @@ NTSTATUS ntlmssp_server_start(NTLMSSP_STATE **ntlmssp_state)
 
        (*ntlmssp_state)->neg_flags = 
                NTLMSSP_NEGOTIATE_128 |
+               NTLMSSP_NEGOTIATE_56 |
+               NTLMSSP_UNKNOWN_02000000 |
+               NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
                NTLMSSP_NEGOTIATE_NTLM |
                NTLMSSP_NEGOTIATE_NTLM2 |
                NTLMSSP_NEGOTIATE_KEY_EXCH |
-               NTLMSSP_NEGOTIATE_SIGN;
+               NTLMSSP_NEGOTIATE_SIGN |
+               NTLMSSP_NEGOTIATE_SEAL;
 
        return NT_STATUS_OK;
 }
@@ -854,15 +991,15 @@ static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
        uint32 chal_flags, ntlmssp_command, unkn1, unkn2;
        DATA_BLOB server_domain_blob;
        DATA_BLOB challenge_blob;
-       DATA_BLOB struct_blob = data_blob(NULL, 0);
+       DATA_BLOB struct_blob = data_blob_null;
        char *server_domain;
        const char *chal_parse_string;
        const char *auth_gen_string;
-       DATA_BLOB lm_response = data_blob(NULL, 0);
-       DATA_BLOB nt_response = data_blob(NULL, 0);
-       DATA_BLOB session_key = data_blob(NULL, 0);
-       DATA_BLOB encrypted_session_key = data_blob(NULL, 0);
-       NTSTATUS nt_status;
+       DATA_BLOB lm_response = data_blob_null;
+       DATA_BLOB nt_response = data_blob_null;
+       DATA_BLOB session_key = data_blob_null;
+       DATA_BLOB encrypted_session_key = data_blob_null;
+       NTSTATUS nt_status = NT_STATUS_OK;
 
        if (!msrpc_parse(&reply, "CdBd",
                         "NTLMSSP",
@@ -870,7 +1007,7 @@ static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
                         &server_domain_blob,
                         &chal_flags)) {
                DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#1)\n"));
-               dump_data(2, (const char *)reply.data, reply.length);
+               dump_data(2, reply.data, reply.length);
 
                return NT_STATUS_INVALID_PARAMETER;
        }
@@ -911,7 +1048,7 @@ static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
                         &unkn1, &unkn2,
                         &struct_blob)) {
                DEBUG(1, ("Failed to parse the NTLMSSP Challenge: (#2)\n"));
-               dump_data(2, (const char *)reply.data, reply.length);
+               dump_data(2, reply.data, reply.length);
                return NT_STATUS_INVALID_PARAMETER;
        }
 
@@ -924,8 +1061,8 @@ static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       if (!ntlmssp_state->password) {
-               static const uchar zeros[16];
+       if (!ntlmssp_state->nt_hash || !ntlmssp_state->lm_hash) {
+               static const uchar zeros[16] = { 0, };
                /* do nothing - blobs are zero length */
 
                /* session key is all zeros */
@@ -944,9 +1081,9 @@ static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
                /* TODO: if the remote server is standalone, then we should replace 'domain'
                   with the server name as supplied above */
                
-               if (!SMBNTLMv2encrypt(ntlmssp_state->user, 
+               if (!SMBNTLMv2encrypt_hash(ntlmssp_state->user, 
                                      ntlmssp_state->domain, 
-                                     ntlmssp_state->password, &challenge_blob, 
+                                     ntlmssp_state->nt_hash, &challenge_blob, 
                                      &struct_blob, 
                                      &lm_response, &nt_response, &session_key)) {
                        data_blob_free(&challenge_blob);
@@ -955,14 +1092,12 @@ static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
                }
        } else if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
                struct MD5Context md5_session_nonce_ctx;
-               uchar nt_hash[16];
                uchar session_nonce[16];
                uchar session_nonce_hash[16];
-               uchar nt_session_key[16];
-               E_md4hash(ntlmssp_state->password, nt_hash);
+               uchar user_session_key[16];
                
                lm_response = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 24);
-               generate_random_buffer(lm_response.data, 8, False);
+               generate_random_buffer(lm_response.data, 8);
                memset(lm_response.data+8, 0, 16);
 
                memcpy(session_nonce, challenge_blob.data, 8);
@@ -975,45 +1110,38 @@ static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
 
                DEBUG(5, ("NTLMSSP challenge set by NTLM2\n"));
                DEBUG(5, ("challenge is: \n"));
-               dump_data(5, (const char *)session_nonce_hash, 8);
+               dump_data(5, session_nonce_hash, 8);
                
                nt_response = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 24);
-               SMBNTencrypt(ntlmssp_state->password,
+               SMBNTencrypt_hash(ntlmssp_state->nt_hash,
                             session_nonce_hash,
                             nt_response.data);
 
                session_key = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 16);
 
-               SMBsesskeygen_ntv1(nt_hash, NULL, nt_session_key);
-               hmac_md5(nt_session_key, session_nonce, sizeof(session_nonce), session_key.data);
+               SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, NULL, user_session_key);
+               hmac_md5(user_session_key, session_nonce, sizeof(session_nonce), session_key.data);
                dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
        } else {
-               
-               
-               uchar lm_hash[16];
-               uchar nt_hash[16];
-               E_deshash(ntlmssp_state->password, lm_hash);
-               E_md4hash(ntlmssp_state->password, nt_hash);
-               
                /* lanman auth is insecure, it may be disabled */
                if (lp_client_lanman_auth()) {
                        lm_response = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 24);
-                       SMBencrypt(ntlmssp_state->password,challenge_blob.data,
+                       SMBencrypt_hash(ntlmssp_state->lm_hash,challenge_blob.data,
                                   lm_response.data);
                }
                
                nt_response = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 24);
-               SMBNTencrypt(ntlmssp_state->password,challenge_blob.data,
+               SMBNTencrypt_hash(ntlmssp_state->nt_hash,challenge_blob.data,
                             nt_response.data);
                
                session_key = data_blob_talloc(ntlmssp_state->mem_ctx, NULL, 16);
                if ((ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) 
                    && lp_client_lanman_auth()) {
-                       SMBsesskeygen_lmv1(lm_hash, lm_response.data, 
-                                          session_key.data);
+                       SMBsesskeygen_lm_sess_key(ntlmssp_state->lm_hash, lm_response.data,
+                                       session_key.data);
                        dump_data_pw("LM session key\n", session_key.data, session_key.length);
                } else {
-                       SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
+                       SMBsesskeygen_ntv1(ntlmssp_state->nt_hash, NULL, session_key.data);
                        dump_data_pw("NT session key:\n", session_key.data, session_key.length);
                }
        }
@@ -1022,16 +1150,19 @@ static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
        /* Key exchange encryptes a new client-generated session key with
           the password-derived key */
        if (ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
+               /* Make up a new session key */
                uint8 client_session_key[16];
-               
-               generate_random_buffer(client_session_key, sizeof(client_session_key), False);  
+               generate_random_buffer(client_session_key, sizeof(client_session_key));
+
+               /* Encrypt the new session key with the old one */
                encrypted_session_key = data_blob(client_session_key, sizeof(client_session_key));
                dump_data_pw("KEY_EXCH session key:\n", encrypted_session_key.data, encrypted_session_key.length);
-
                SamOEMhash(encrypted_session_key.data, session_key.data, encrypted_session_key.length);
+               dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
+
+               /* Mark the new session key as the 'real' session key */
                data_blob_free(&session_key);
                session_key = data_blob_talloc(ntlmssp_state->mem_ctx, client_session_key, sizeof(client_session_key));
-               dump_data_pw("KEY_EXCH session key (enc):\n", encrypted_session_key.data, encrypted_session_key.length);
        }
 
        /* this generates the actual auth packet */
@@ -1053,19 +1184,19 @@ static NTSTATUS ntlmssp_client_challenge(struct ntlmssp_state *ntlmssp_state,
 
        data_blob_free(&ntlmssp_state->chal);
 
+       ntlmssp_state->session_key = session_key;
+
        ntlmssp_state->chal = challenge_blob;
        ntlmssp_state->lm_resp = lm_response;
        ntlmssp_state->nt_resp = nt_response;
-       ntlmssp_state->session_key = session_key;
 
-       ntlmssp_state->expected_state = NTLMSSP_UNKNOWN;
+       ntlmssp_state->expected_state = NTLMSSP_DONE;
 
        if (!NT_STATUS_IS_OK(nt_status = ntlmssp_sign_init(ntlmssp_state))) {
                DEBUG(1, ("Could not setup NTLMSSP signing/sealing system (error was: %s)\n", nt_errstr(nt_status)));
-               return nt_status;
        }
 
-       return NT_STATUS_MORE_PROCESSING_REQUIRED;
+       return nt_status;
 }
 
 NTSTATUS ntlmssp_client_start(NTLMSSP_STATE **ntlmssp_state)
@@ -1074,7 +1205,7 @@ NTSTATUS ntlmssp_client_start(NTLMSSP_STATE **ntlmssp_state)
 
        mem_ctx = talloc_init("NTLMSSP Client context");
        
-       *ntlmssp_state = talloc_zero(mem_ctx, sizeof(**ntlmssp_state));
+       *ntlmssp_state = TALLOC_ZERO_P(mem_ctx, NTLMSSP_STATE);
        if (!*ntlmssp_state) {
                DEBUG(0,("ntlmssp_client_start: talloc failed!\n"));
                talloc_destroy(mem_ctx);
@@ -1098,17 +1229,11 @@ NTSTATUS ntlmssp_client_start(NTLMSSP_STATE **ntlmssp_state)
 
        (*ntlmssp_state)->neg_flags = 
                NTLMSSP_NEGOTIATE_128 |
+               NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
                NTLMSSP_NEGOTIATE_NTLM |
                NTLMSSP_NEGOTIATE_NTLM2 |
                NTLMSSP_NEGOTIATE_KEY_EXCH |
-               /*
-                * We need to set this to allow a later SetPassword
-                * via the SAMR pipe to succeed. Strange.... We could
-                * also add  NTLMSSP_NEGOTIATE_SEAL here. JRA.
-                * */
-               NTLMSSP_NEGOTIATE_SIGN |
                NTLMSSP_REQUEST_TARGET;
 
        return NT_STATUS_OK;
 }
-