2 Unix SMB/Netbios implementation.
4 handle NLTMSSP, server side
6 Copyright (C) Andrew Tridgell 2001
7 Copyright (C) Andrew Bartlett 2001-2003
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "../libcli/auth/ntlmssp.h"
25 #include "ntlmssp_wrap.h"
26 #include "../librpc/gen_ndr/netlogon.h"
28 NTSTATUS auth_ntlmssp_steal_session_info(TALLOC_CTX *mem_ctx,
29 struct auth_ntlmssp_state *auth_ntlmssp_state,
30 struct auth_serversupplied_info **session_info)
32 NTSTATUS nt_status = create_local_token(mem_ctx,
33 auth_ntlmssp_state->server_info,
34 &auth_ntlmssp_state->ntlmssp_state->session_key,
37 if (!NT_STATUS_IS_OK(nt_status)) {
38 DEBUG(10, ("create_local_token failed: %s\n",
39 nt_errstr(nt_status)));
45 * Return the challenge as determined by the authentication subsystem
46 * @return an 8 byte random challenge
49 static NTSTATUS auth_ntlmssp_get_challenge(const struct ntlmssp_state *ntlmssp_state,
52 struct auth_ntlmssp_state *auth_ntlmssp_state =
53 (struct auth_ntlmssp_state *)ntlmssp_state->callback_private;
54 auth_ntlmssp_state->auth_context->get_ntlm_challenge(
55 auth_ntlmssp_state->auth_context, chal);
60 * Some authentication methods 'fix' the challenge, so we may not be able to set it
62 * @return If the effective challenge used by the auth subsystem may be modified
64 static bool auth_ntlmssp_may_set_challenge(const struct ntlmssp_state *ntlmssp_state)
66 struct auth_ntlmssp_state *auth_ntlmssp_state =
67 (struct auth_ntlmssp_state *)ntlmssp_state->callback_private;
68 struct auth_context *auth_context = auth_ntlmssp_state->auth_context;
70 return auth_context->challenge_may_be_modified;
74 * NTLM2 authentication modifies the effective challenge,
75 * @param challenge The new challenge value
77 static NTSTATUS auth_ntlmssp_set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge)
79 struct auth_ntlmssp_state *auth_ntlmssp_state =
80 (struct auth_ntlmssp_state *)ntlmssp_state->callback_private;
81 struct auth_context *auth_context = auth_ntlmssp_state->auth_context;
83 SMB_ASSERT(challenge->length == 8);
85 auth_context->challenge = data_blob_talloc(auth_context,
86 challenge->data, challenge->length);
88 auth_context->challenge_set_by = "NTLMSSP callback (NTLM2)";
90 DEBUG(5, ("auth_context challenge set by %s\n", auth_context->challenge_set_by));
91 DEBUG(5, ("challenge is: \n"));
92 dump_data(5, auth_context->challenge.data, auth_context->challenge.length);
97 * Check the password on an NTLMSSP login.
99 * Return the session keys used on the connection.
102 static NTSTATUS auth_ntlmssp_check_password(struct ntlmssp_state *ntlmssp_state, TALLOC_CTX *mem_ctx,
103 DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
105 struct auth_ntlmssp_state *auth_ntlmssp_state =
106 (struct auth_ntlmssp_state *)ntlmssp_state->callback_private;
107 struct auth_usersupplied_info *user_info = NULL;
109 bool username_was_mapped;
111 /* the client has given us its machine name (which we otherwise would not get on port 445).
112 we need to possibly reload smb.conf if smb.conf includes depend on the machine name */
114 set_remote_machine_name(auth_ntlmssp_state->ntlmssp_state->client.netbios_name, True);
116 /* setup the string used by %U */
117 /* sub_set_smb_name checks for weird internally */
118 sub_set_smb_name(auth_ntlmssp_state->ntlmssp_state->user);
120 reload_services(smbd_messaging_context(), -1, True);
122 nt_status = make_user_info_map(&user_info,
123 auth_ntlmssp_state->ntlmssp_state->user,
124 auth_ntlmssp_state->ntlmssp_state->domain,
125 auth_ntlmssp_state->ntlmssp_state->client.netbios_name,
126 auth_ntlmssp_state->ntlmssp_state->lm_resp.data ? &auth_ntlmssp_state->ntlmssp_state->lm_resp : NULL,
127 auth_ntlmssp_state->ntlmssp_state->nt_resp.data ? &auth_ntlmssp_state->ntlmssp_state->nt_resp : NULL,
129 AUTH_PASSWORD_RESPONSE);
131 if (!NT_STATUS_IS_OK(nt_status)) {
135 user_info->logon_parameters = MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT;
137 nt_status = auth_ntlmssp_state->auth_context->check_ntlm_password(auth_ntlmssp_state->auth_context,
138 user_info, &auth_ntlmssp_state->server_info);
140 username_was_mapped = user_info->was_mapped;
142 free_user_info(&user_info);
144 if (!NT_STATUS_IS_OK(nt_status)) {
148 auth_ntlmssp_state->server_info->nss_token |= username_was_mapped;
150 /* Clear out the session keys, and pass them to the caller.
151 * They will not be used in this form again - instead the
152 * NTLMSSP code will decide on the final correct session key,
153 * and put it back here at the end of
154 * auth_ntlmssp_steal_server_info */
155 if (auth_ntlmssp_state->server_info->user_session_key.length) {
156 DEBUG(10, ("Got NT session key of length %u\n",
157 (unsigned int)auth_ntlmssp_state->server_info->user_session_key.length));
158 *user_session_key = auth_ntlmssp_state->server_info->user_session_key;
159 talloc_steal(mem_ctx, auth_ntlmssp_state->server_info->user_session_key.data);
160 auth_ntlmssp_state->server_info->user_session_key = data_blob_null;
162 if (auth_ntlmssp_state->server_info->lm_session_key.length) {
163 DEBUG(10, ("Got LM session key of length %u\n",
164 (unsigned int)auth_ntlmssp_state->server_info->lm_session_key.length));
165 *lm_session_key = auth_ntlmssp_state->server_info->lm_session_key;
166 talloc_steal(mem_ctx, auth_ntlmssp_state->server_info->lm_session_key.data);
167 auth_ntlmssp_state->server_info->lm_session_key = data_blob_null;
172 static int auth_ntlmssp_state_destructor(void *ptr);
174 NTSTATUS auth_ntlmssp_start(struct auth_ntlmssp_state **auth_ntlmssp_state)
178 const char *netbios_name;
179 const char *netbios_domain;
180 const char *dns_name;
182 struct auth_ntlmssp_state *ans;
183 struct auth_context *auth_context;
185 if ((enum server_types)lp_server_role() == ROLE_STANDALONE) {
186 is_standalone = true;
188 is_standalone = false;
191 netbios_name = global_myname();
192 netbios_domain = lp_workgroup();
193 /* This should be a 'netbios domain -> DNS domain' mapping */
194 dns_domain = get_mydnsdomname(talloc_tos());
196 strlower_m(dns_domain);
198 dns_name = get_mydnsfullname();
200 ans = talloc_zero(NULL, struct auth_ntlmssp_state);
202 DEBUG(0,("auth_ntlmssp_start: talloc failed!\n"));
203 return NT_STATUS_NO_MEMORY;
206 nt_status = ntlmssp_server_start(ans,
212 &ans->ntlmssp_state);
213 if (!NT_STATUS_IS_OK(nt_status)) {
217 nt_status = make_auth_context_subsystem(talloc_tos(), &auth_context);
218 if (!NT_STATUS_IS_OK(nt_status)) {
221 ans->auth_context = talloc_steal(ans, auth_context);
223 ans->ntlmssp_state->callback_private = ans;
224 ans->ntlmssp_state->get_challenge = auth_ntlmssp_get_challenge;
225 ans->ntlmssp_state->may_set_challenge = auth_ntlmssp_may_set_challenge;
226 ans->ntlmssp_state->set_challenge = auth_ntlmssp_set_challenge;
227 ans->ntlmssp_state->check_password = auth_ntlmssp_check_password;
229 talloc_set_destructor((TALLOC_CTX *)ans, auth_ntlmssp_state_destructor);
231 *auth_ntlmssp_state = ans;
235 static int auth_ntlmssp_state_destructor(void *ptr)
237 struct auth_ntlmssp_state *ans;
239 ans = talloc_get_type(ptr, struct auth_ntlmssp_state);
241 TALLOC_FREE(ans->server_info);
242 TALLOC_FREE(ans->ntlmssp_state);