Move source4/lib/crypto to lib/crypto.
[ira/wip.git] / source4 / auth / ntlmssp / ntlmssp_server.c
index e29334e18376569c906956edd0ff99b0f39402a9..ad1ee8e871b6d045a69db776bbeed6d0d2c6cb46 100644 (file)
 #include "includes.h"
 #include "auth/ntlmssp/ntlmssp.h"
 #include "auth/ntlmssp/msrpc_parse.h"
-#include "lib/crypto/crypto.h"
-#include "pstring.h"
-#include "system/filesys.h"
+#include "../lib/crypto/crypto.h"
 #include "libcli/auth/libcli_auth.h"
 #include "auth/credentials/credentials.h"
 #include "auth/gensec/gensec.h"
 #include "auth/auth.h"
+#include "auth/ntlm/auth_proto.h"
+#include "param/param.h"
+#include "auth/session_proto.h"
 
 /** 
  * Set a username on an NTLMSSP context - ensures it is talloc()ed 
@@ -98,66 +99,13 @@ static const char *ntlmssp_target_name(struct gensec_ntlmssp_state *gensec_ntlms
                        return gensec_ntlmssp_state->server_name;
                } else {
                        *chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN;
-                       return gensec_ntlmssp_state->get_domain();
+                       return gensec_ntlmssp_state->domain;
                };
        } else {
                return "";
        }
 }
 
-/*
-  Andrew, please remove these totally bogus calls when you get time
-*/
-static BOOL get_myfullname(char *my_name)
-{
-       pstring hostname;
-
-       *hostname = 0;
-
-       /* get my host name */
-       if (gethostname(hostname, sizeof(hostname)) == -1) {
-               DEBUG(0,("gethostname failed\n"));
-               return False;
-       } 
-
-       /* Ensure null termination. */
-       hostname[sizeof(hostname)-1] = '\0';
-
-       if (my_name)
-               fstrcpy(my_name, hostname);
-       return True;
-}
-
-static BOOL get_mydomname(char *my_domname)
-{
-       pstring hostname;
-       char *p;
-
-       /* arrgh! relies on full name in system */
-
-       *hostname = 0;
-       /* get my host name */
-       if (gethostname(hostname, sizeof(hostname)) == -1) {
-               DEBUG(0,("gethostname failed\n"));
-               return False;
-       } 
-
-       /* Ensure null termination. */
-       hostname[sizeof(hostname)-1] = '\0';
-
-       p = strchr_m(hostname, '.');
-
-       if (!p)
-               return False;
-
-       p++;
-       
-       if (my_domname)
-               fstrcpy(my_domname, p);
-
-       return True;
-}
-
 
 
 /**
@@ -176,7 +124,6 @@ NTSTATUS ntlmssp_server_negotiate(struct gensec_security *gensec_security,
 {
        struct gensec_ntlmssp_state *gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
        DATA_BLOB struct_blob;
-       fstring dnsname, dnsdomname;
        uint32_t neg_flags = 0;
        uint32_t ntlmssp_command, chal_flags;
        const uint8_t *cryptkey;
@@ -188,7 +135,9 @@ NTSTATUS ntlmssp_server_negotiate(struct gensec_security *gensec_security,
 #endif
 
        if (in.length) {
-               if ((in.length < 16) || !msrpc_parse(out_mem_ctx, &in, "Cdd",
+               if ((in.length < 16) || !msrpc_parse(out_mem_ctx, 
+                                lp_iconv_convenience(gensec_security->lp_ctx),
+                                                        &in, "Cdd",
                                                         "NTLMSSP",
                                                         &ntlmssp_command,
                                                         &neg_flags)) {
@@ -205,6 +154,10 @@ NTSTATUS ntlmssp_server_negotiate(struct gensec_security *gensec_security,
 
        /* Ask our caller what challenge they would like in the packet */
        cryptkey = gensec_ntlmssp_state->get_challenge(gensec_ntlmssp_state);
+       if (!cryptkey) {
+               DEBUG(1, ("ntlmssp_server_negotiate: backend doesn't give a challenge\n"));
+               return NT_STATUS_INTERNAL_ERROR;
+       }
 
        /* Check if we may set the challenge */
        if (!gensec_ntlmssp_state->may_set_challenge(gensec_ntlmssp_state)) {
@@ -227,18 +180,24 @@ NTSTATUS ntlmssp_server_negotiate(struct gensec_security *gensec_security,
        gensec_ntlmssp_state->chal = data_blob_talloc(gensec_ntlmssp_state, cryptkey, 8);
        gensec_ntlmssp_state->internal_chal = data_blob_talloc(gensec_ntlmssp_state, cryptkey, 8);
 
-       /* This should be a 'netbios domain -> DNS domain' mapping */
-       dnsdomname[0] = '\0';
-       get_mydomname(dnsdomname);
-       strlower_m(dnsdomname);
-       
-       dnsname[0] = '\0';
-       get_myfullname(dnsname);
-       
        /* This creates the 'blob' of names that appears at the end of the packet */
-       if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) 
-       {
+       if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) {
+               char dnsdomname[MAXHOSTNAMELEN], dnsname[MAXHOSTNAMELEN];
                const char *target_name_dns = "";
+
+               /* Find out the DNS domain name */
+               dnsdomname[0] = '\0';
+               safe_strcpy(dnsdomname, lp_realm(gensec_security->lp_ctx), sizeof(dnsdomname) - 1);
+               strlower_m(dnsdomname);
+
+               /* Find out the DNS host name */
+               safe_strcpy(dnsname, gensec_ntlmssp_state->server_name, sizeof(dnsname) - 1);
+               if (dnsdomname[0] != '\0') {
+                       safe_strcat(dnsname, ".", sizeof(dnsname) - 1);
+                       safe_strcat(dnsname, dnsdomname, sizeof(dnsname) - 1);
+               }
+               strlower_m(dnsname);
+
                if (chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN) {
                        target_name_dns = dnsdomname;
                } else if (chal_flags |= NTLMSSP_TARGET_TYPE_SERVER) {
@@ -246,6 +205,7 @@ NTSTATUS ntlmssp_server_negotiate(struct gensec_security *gensec_security,
                }
 
                msrpc_gen(out_mem_ctx, 
+                         lp_iconv_convenience(gensec_security->lp_ctx),
                          &struct_blob, "aaaaa",
                          NTLMSSP_NAME_TYPE_DOMAIN, target_name,
                          NTLMSSP_NAME_TYPE_SERVER, gensec_ntlmssp_state->server_name,
@@ -257,7 +217,7 @@ NTSTATUS ntlmssp_server_negotiate(struct gensec_security *gensec_security,
        }
 
        {
-               /* Marshel the packet in the right format, be it unicode or ASCII */
+               /* Marshal the packet in the right format, be it unicode or ASCII */
                const char *gen_string;
                if (gensec_ntlmssp_state->unicode) {
                        gen_string = "CdUdbddB";
@@ -266,6 +226,7 @@ NTSTATUS ntlmssp_server_negotiate(struct gensec_security *gensec_security,
                }
                
                msrpc_gen(out_mem_ctx, 
+                         lp_iconv_convenience(gensec_security->lp_ctx),
                          out, gen_string,
                          "NTLMSSP", 
                          NTLMSSP_CHALLENGE,
@@ -323,6 +284,7 @@ static NTSTATUS ntlmssp_server_preauth(struct gensec_ntlmssp_state *gensec_ntlms
 
        /* now the NTLMSSP encoded auth hashes */
        if (!msrpc_parse(gensec_ntlmssp_state, 
+                        lp_iconv_convenience(gensec_ntlmssp_state->gensec_security->lp_ctx),
                         &request, parse_string,
                         "NTLMSSP", 
                         &ntlmssp_command, 
@@ -349,6 +311,7 @@ static NTSTATUS ntlmssp_server_preauth(struct gensec_ntlmssp_state *gensec_ntlms
 
                /* now the NTLMSSP encoded auth hashes */
                if (!msrpc_parse(gensec_ntlmssp_state, 
+                                lp_iconv_convenience(gensec_ntlmssp_state->gensec_security->lp_ctx),
                                 &request, parse_string,
                                 "NTLMSSP", 
                                 &ntlmssp_command, 
@@ -404,7 +367,7 @@ static NTSTATUS ntlmssp_server_preauth(struct gensec_ntlmssp_state *gensec_ntlms
                        SMB_ASSERT(gensec_ntlmssp_state->internal_chal.data 
                                   && gensec_ntlmssp_state->internal_chal.length == 8);
                        
-                       gensec_ntlmssp_state->doing_ntlm2 = True;
+                       gensec_ntlmssp_state->doing_ntlm2 = true;
 
                        memcpy(gensec_ntlmssp_state->crypt.ntlm2.session_nonce, gensec_ntlmssp_state->internal_chal.data, 8);
                        memcpy(&gensec_ntlmssp_state->crypt.ntlm2.session_nonce[8], gensec_ntlmssp_state->lm_resp.data, 8);
@@ -651,6 +614,8 @@ static const uint8_t *auth_ntlmssp_get_challenge(const struct gensec_ntlmssp_sta
 
        status = auth_get_challenge(gensec_ntlmssp_state->auth_context, &chal);
        if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("auth_ntlmssp_get_challenge: failed to get challenge: %s\n",
+                       nt_errstr(status)));
                return NULL;
        }
 
@@ -662,7 +627,7 @@ static const uint8_t *auth_ntlmssp_get_challenge(const struct gensec_ntlmssp_sta
  *
  * @return If the effective challenge used by the auth subsystem may be modified
  */
-static BOOL auth_ntlmssp_may_set_challenge(const struct gensec_ntlmssp_state *gensec_ntlmssp_state)
+static bool auth_ntlmssp_may_set_challenge(const struct gensec_ntlmssp_state *gensec_ntlmssp_state)
 {
        return auth_challenge_may_be_modified(gensec_ntlmssp_state->auth_context);
 }
@@ -706,7 +671,7 @@ static NTSTATUS auth_ntlmssp_check_password(struct gensec_ntlmssp_state *gensec_
 
        user_info->logon_parameters = MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT;
        user_info->flags = 0;
-       user_info->mapped_state = False;
+       user_info->mapped_state = false;
        user_info->client.account_name = gensec_ntlmssp_state->user;
        user_info->client.domain_name = gensec_ntlmssp_state->domain;
        user_info->workstation_name = gensec_ntlmssp_state->workstation;
@@ -762,7 +727,7 @@ NTSTATUS gensec_ntlmssp_session_info(struct gensec_security *gensec_security,
        NTSTATUS nt_status;
        struct gensec_ntlmssp_state *gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
 
-       nt_status = auth_generate_session_info(gensec_ntlmssp_state, gensec_ntlmssp_state->server_info, session_info);
+       nt_status = auth_generate_session_info(gensec_ntlmssp_state, gensec_security->event_ctx, gensec_security->lp_ctx, gensec_ntlmssp_state->server_info, session_info);
        NT_STATUS_NOT_OK_RETURN(nt_status);
 
        (*session_info)->session_key = data_blob_talloc(*session_info, 
@@ -789,16 +754,16 @@ NTSTATUS gensec_ntlmssp_server_start(struct gensec_security *gensec_security)
        gensec_ntlmssp_state->role = NTLMSSP_SERVER;
 
        gensec_ntlmssp_state->workstation = NULL;
-       gensec_ntlmssp_state->server_name = lp_netbios_name();
+       gensec_ntlmssp_state->server_name = lp_netbios_name(gensec_security->lp_ctx);
 
-       gensec_ntlmssp_state->get_domain = lp_workgroup;
+       gensec_ntlmssp_state->domain = lp_workgroup(gensec_security->lp_ctx);
 
        gensec_ntlmssp_state->expected_state = NTLMSSP_NEGOTIATE;
 
-       gensec_ntlmssp_state->allow_lm_key = (lp_lanman_auth() 
-                                         && lp_parm_bool(-1, "ntlmssp_server", "allow_lm_key", False));
+       gensec_ntlmssp_state->allow_lm_key = (lp_lanman_auth(gensec_security->lp_ctx
+                                         && lp_parm_bool(gensec_security->lp_ctx, NULL, "ntlmssp_server", "allow_lm_key", false));
 
-       gensec_ntlmssp_state->server_multiple_authentications = False;
+       gensec_ntlmssp_state->server_multiple_authentications = false;
        
        gensec_ntlmssp_state->neg_flags = 
                NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_UNKNOWN_02000000;
@@ -807,23 +772,23 @@ NTSTATUS gensec_ntlmssp_server_start(struct gensec_security *gensec_security)
        gensec_ntlmssp_state->nt_resp = data_blob(NULL, 0);
        gensec_ntlmssp_state->encrypted_session_key = data_blob(NULL, 0);
 
-       if (lp_parm_bool(-1, "ntlmssp_server", "128bit", True)) {
+       if (lp_parm_bool(gensec_security->lp_ctx, NULL, "ntlmssp_server", "128bit", true)) {
                gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_128;               
        }
 
-       if (lp_parm_bool(-1, "ntlmssp_server", "56bit", True)) {
+       if (lp_parm_bool(gensec_security->lp_ctx, NULL, "ntlmssp_server", "56bit", true)) {
                gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_56;                
        }
 
-       if (lp_parm_bool(-1, "ntlmssp_server", "keyexchange", True)) {
+       if (lp_parm_bool(gensec_security->lp_ctx, NULL, "ntlmssp_server", "keyexchange", true)) {
                gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_KEY_EXCH;          
        }
 
-       if (lp_parm_bool(-1, "ntlmssp_server", "alwayssign", True)) {
+       if (lp_parm_bool(gensec_security->lp_ctx, NULL, "ntlmssp_server", "alwayssign", true)) {
                gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;               
        }
 
-       if (lp_parm_bool(-1, "ntlmssp_server", "ntlm2", True)) {
+       if (lp_parm_bool(gensec_security->lp_ctx, NULL, "ntlmssp_server", "ntlm2", true)) {
                gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;             
        }
 
@@ -837,6 +802,7 @@ NTSTATUS gensec_ntlmssp_server_start(struct gensec_security *gensec_security)
        nt_status = auth_context_create(gensec_ntlmssp_state, 
                                        gensec_security->event_ctx,
                                        gensec_security->msg_ctx,
+                                       gensec_security->lp_ctx,
                                        &gensec_ntlmssp_state->auth_context);
        NT_STATUS_NOT_OK_RETURN(nt_status);
 
@@ -844,7 +810,7 @@ NTSTATUS gensec_ntlmssp_server_start(struct gensec_security *gensec_security)
        gensec_ntlmssp_state->may_set_challenge = auth_ntlmssp_may_set_challenge;
        gensec_ntlmssp_state->set_challenge = auth_ntlmssp_set_challenge;
        gensec_ntlmssp_state->check_password = auth_ntlmssp_check_password;
-       gensec_ntlmssp_state->server_role = lp_server_role();
+       gensec_ntlmssp_state->server_role = lp_server_role(gensec_security->lp_ctx);
 
        return NT_STATUS_OK;
 }