Fix smbclient/tarmode panic on connecting to Windows 2000 clients.
[Samba.git] / source3 / auth / auth_ntlmssp.c
blobae29c305703cc71bf1e6fb3bf21379b2855e0ac7
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
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/>.
23 #include "includes.h"
24 #include "auth.h"
25 #include "../libcli/auth/ntlmssp.h"
26 #include "ntlmssp_wrap.h"
27 #include "../librpc/gen_ndr/netlogon.h"
28 #include "smbd/smbd.h"
30 NTSTATUS auth_ntlmssp_steal_session_info(TALLOC_CTX *mem_ctx,
31 struct auth_ntlmssp_state *auth_ntlmssp_state,
32 struct auth_serversupplied_info **session_info)
34 /* Free the current server_info user_session_key and reset it from the
35 * current ntlmssp_state session_key */
36 data_blob_free(&auth_ntlmssp_state->server_info->user_session_key);
37 /* Set up the final session key for the connection */
38 auth_ntlmssp_state->server_info->user_session_key =
39 data_blob_talloc(
40 auth_ntlmssp_state->server_info,
41 auth_ntlmssp_state->ntlmssp_state->session_key.data,
42 auth_ntlmssp_state->ntlmssp_state->session_key.length);
43 if (auth_ntlmssp_state->ntlmssp_state->session_key.length &&
44 !auth_ntlmssp_state->server_info->user_session_key.data) {
45 *session_info = NULL;
46 return NT_STATUS_NO_MEMORY;
48 /* Steal session_info away from auth_ntlmssp_state */
49 *session_info = talloc_move(mem_ctx, &auth_ntlmssp_state->server_info);
50 return NT_STATUS_OK;
53 /**
54 * Return the challenge as determined by the authentication subsystem
55 * @return an 8 byte random challenge
58 static NTSTATUS auth_ntlmssp_get_challenge(const struct ntlmssp_state *ntlmssp_state,
59 uint8_t chal[8])
61 struct auth_ntlmssp_state *auth_ntlmssp_state =
62 (struct auth_ntlmssp_state *)ntlmssp_state->callback_private;
63 auth_ntlmssp_state->auth_context->get_ntlm_challenge(
64 auth_ntlmssp_state->auth_context, chal);
65 return NT_STATUS_OK;
68 /**
69 * Some authentication methods 'fix' the challenge, so we may not be able to set it
71 * @return If the effective challenge used by the auth subsystem may be modified
73 static bool auth_ntlmssp_may_set_challenge(const struct ntlmssp_state *ntlmssp_state)
75 struct auth_ntlmssp_state *auth_ntlmssp_state =
76 (struct auth_ntlmssp_state *)ntlmssp_state->callback_private;
77 struct auth_context *auth_context = auth_ntlmssp_state->auth_context;
79 return auth_context->challenge_may_be_modified;
82 /**
83 * NTLM2 authentication modifies the effective challenge,
84 * @param challenge The new challenge value
86 static NTSTATUS auth_ntlmssp_set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge)
88 struct auth_ntlmssp_state *auth_ntlmssp_state =
89 (struct auth_ntlmssp_state *)ntlmssp_state->callback_private;
90 struct auth_context *auth_context = auth_ntlmssp_state->auth_context;
92 SMB_ASSERT(challenge->length == 8);
94 auth_context->challenge = data_blob_talloc(auth_context,
95 challenge->data, challenge->length);
97 auth_context->challenge_set_by = "NTLMSSP callback (NTLM2)";
99 DEBUG(5, ("auth_context challenge set by %s\n", auth_context->challenge_set_by));
100 DEBUG(5, ("challenge is: \n"));
101 dump_data(5, auth_context->challenge.data, auth_context->challenge.length);
102 return NT_STATUS_OK;
106 * Check the password on an NTLMSSP login.
108 * Return the session keys used on the connection.
111 static NTSTATUS auth_ntlmssp_check_password(struct ntlmssp_state *ntlmssp_state, TALLOC_CTX *mem_ctx,
112 DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
114 struct auth_ntlmssp_state *auth_ntlmssp_state =
115 (struct auth_ntlmssp_state *)ntlmssp_state->callback_private;
116 struct auth_usersupplied_info *user_info = NULL;
117 NTSTATUS nt_status;
118 bool username_was_mapped;
120 /* the client has given us its machine name (which we otherwise would not get on port 445).
121 we need to possibly reload smb.conf if smb.conf includes depend on the machine name */
123 set_remote_machine_name(auth_ntlmssp_state->ntlmssp_state->client.netbios_name, True);
125 /* setup the string used by %U */
126 /* sub_set_smb_name checks for weird internally */
127 sub_set_smb_name(auth_ntlmssp_state->ntlmssp_state->user);
129 reload_services(smbd_messaging_context(), -1, True);
131 nt_status = make_user_info_map(&user_info,
132 auth_ntlmssp_state->ntlmssp_state->user,
133 auth_ntlmssp_state->ntlmssp_state->domain,
134 auth_ntlmssp_state->ntlmssp_state->client.netbios_name,
135 auth_ntlmssp_state->ntlmssp_state->lm_resp.data ? &auth_ntlmssp_state->ntlmssp_state->lm_resp : NULL,
136 auth_ntlmssp_state->ntlmssp_state->nt_resp.data ? &auth_ntlmssp_state->ntlmssp_state->nt_resp : NULL,
137 NULL, NULL, NULL,
138 AUTH_PASSWORD_RESPONSE);
140 if (!NT_STATUS_IS_OK(nt_status)) {
141 return nt_status;
144 user_info->logon_parameters = MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT;
146 nt_status = auth_ntlmssp_state->auth_context->check_ntlm_password(auth_ntlmssp_state->auth_context,
147 user_info, &auth_ntlmssp_state->server_info);
149 username_was_mapped = user_info->was_mapped;
151 free_user_info(&user_info);
153 if (!NT_STATUS_IS_OK(nt_status)) {
154 return nt_status;
157 auth_ntlmssp_state->server_info->nss_token |= username_was_mapped;
159 nt_status = create_local_token(auth_ntlmssp_state->server_info);
161 if (!NT_STATUS_IS_OK(nt_status)) {
162 DEBUG(10, ("create_local_token failed: %s\n",
163 nt_errstr(nt_status)));
164 return nt_status;
167 /* Clear out the session keys, and pass them to the caller.
168 * They will not be used in this form again - instead the
169 * NTLMSSP code will decide on the final correct session key,
170 * and put it back here at the end of
171 * auth_ntlmssp_steal_server_info */
172 if (auth_ntlmssp_state->server_info->user_session_key.length) {
173 DEBUG(10, ("Got NT session key of length %u\n",
174 (unsigned int)auth_ntlmssp_state->server_info->user_session_key.length));
175 *user_session_key = auth_ntlmssp_state->server_info->user_session_key;
176 talloc_steal(mem_ctx, auth_ntlmssp_state->server_info->user_session_key.data);
177 auth_ntlmssp_state->server_info->user_session_key = data_blob_null;
179 if (auth_ntlmssp_state->server_info->lm_session_key.length) {
180 DEBUG(10, ("Got LM session key of length %u\n",
181 (unsigned int)auth_ntlmssp_state->server_info->lm_session_key.length));
182 *lm_session_key = auth_ntlmssp_state->server_info->lm_session_key;
183 talloc_steal(mem_ctx, auth_ntlmssp_state->server_info->lm_session_key.data);
184 auth_ntlmssp_state->server_info->lm_session_key = data_blob_null;
186 return nt_status;
189 static int auth_ntlmssp_state_destructor(void *ptr);
191 NTSTATUS auth_ntlmssp_start(struct auth_ntlmssp_state **auth_ntlmssp_state)
193 NTSTATUS nt_status;
194 bool is_standalone;
195 const char *netbios_name;
196 const char *netbios_domain;
197 const char *dns_name;
198 char *dns_domain;
199 struct auth_ntlmssp_state *ans;
200 struct auth_context *auth_context;
202 if ((enum server_types)lp_server_role() == ROLE_STANDALONE) {
203 is_standalone = true;
204 } else {
205 is_standalone = false;
208 netbios_name = global_myname();
209 netbios_domain = lp_workgroup();
210 /* This should be a 'netbios domain -> DNS domain' mapping */
211 dns_domain = get_mydnsdomname(talloc_tos());
212 if (dns_domain) {
213 strlower_m(dns_domain);
215 dns_name = get_mydnsfullname();
217 ans = talloc_zero(NULL, struct auth_ntlmssp_state);
218 if (!ans) {
219 DEBUG(0,("auth_ntlmssp_start: talloc failed!\n"));
220 return NT_STATUS_NO_MEMORY;
223 nt_status = ntlmssp_server_start(ans,
224 is_standalone,
225 netbios_name,
226 netbios_domain,
227 dns_name,
228 dns_domain,
229 &ans->ntlmssp_state);
230 if (!NT_STATUS_IS_OK(nt_status)) {
231 return nt_status;
234 nt_status = make_auth_context_subsystem(talloc_tos(), &auth_context);
235 if (!NT_STATUS_IS_OK(nt_status)) {
236 return nt_status;
238 ans->auth_context = talloc_steal(ans, auth_context);
240 ans->ntlmssp_state->callback_private = ans;
241 ans->ntlmssp_state->get_challenge = auth_ntlmssp_get_challenge;
242 ans->ntlmssp_state->may_set_challenge = auth_ntlmssp_may_set_challenge;
243 ans->ntlmssp_state->set_challenge = auth_ntlmssp_set_challenge;
244 ans->ntlmssp_state->check_password = auth_ntlmssp_check_password;
246 talloc_set_destructor((TALLOC_CTX *)ans, auth_ntlmssp_state_destructor);
248 *auth_ntlmssp_state = ans;
249 return NT_STATUS_OK;
252 static int auth_ntlmssp_state_destructor(void *ptr)
254 struct auth_ntlmssp_state *ans;
256 ans = talloc_get_type(ptr, struct auth_ntlmssp_state);
258 TALLOC_FREE(ans->server_info);
259 TALLOC_FREE(ans->ntlmssp_state);
260 return 0;