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"
27 void auth_ntlmssp_want_sign(struct auth_ntlmssp_state *auth_ntlmssp_state)
32 void auth_ntlmssp_want_seal(struct auth_ntlmssp_state *auth_ntlmssp_state)
37 NTSTATUS auth_ntlmssp_steal_server_info(TALLOC_CTX *mem_ctx,
38 struct auth_ntlmssp_state *auth_ntlmssp_state,
39 struct auth_serversupplied_info **server_info)
41 /* Free the current server_info user_session_key and reset it from the
42 * current ntlmssp_state session_key */
43 data_blob_free(&auth_ntlmssp_state->server_info->user_session_key);
44 auth_ntlmssp_state->server_info->user_session_key =
46 auth_ntlmssp_state->server_info,
47 auth_ntlmssp_state->ntlmssp_state->session_key.data,
48 auth_ntlmssp_state->ntlmssp_state->session_key.length);
49 if (auth_ntlmssp_state->ntlmssp_state->session_key.length &&
50 !auth_ntlmssp_state->server_info->user_session_key.data) {
52 return NT_STATUS_NO_MEMORY;
54 /* Steal server_info away from auth_ntlmssp_state */
55 *server_info = talloc_move(mem_ctx, &auth_ntlmssp_state->server_info);
60 * Return the challenge as determined by the authentication subsystem
61 * @return an 8 byte random challenge
64 static NTSTATUS auth_ntlmssp_get_challenge(const struct ntlmssp_state *ntlmssp_state,
67 struct auth_ntlmssp_state *auth_ntlmssp_state =
68 (struct auth_ntlmssp_state *)ntlmssp_state->callback_private;
69 auth_ntlmssp_state->auth_context->get_ntlm_challenge(
70 auth_ntlmssp_state->auth_context, chal);
75 * Some authentication methods 'fix' the challenge, so we may not be able to set it
77 * @return If the effective challenge used by the auth subsystem may be modified
79 static bool auth_ntlmssp_may_set_challenge(const struct ntlmssp_state *ntlmssp_state)
81 struct auth_ntlmssp_state *auth_ntlmssp_state =
82 (struct auth_ntlmssp_state *)ntlmssp_state->callback_private;
83 struct auth_context *auth_context = auth_ntlmssp_state->auth_context;
85 return auth_context->challenge_may_be_modified;
89 * NTLM2 authentication modifies the effective challenge,
90 * @param challenge The new challenge value
92 static NTSTATUS auth_ntlmssp_set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge)
94 struct auth_ntlmssp_state *auth_ntlmssp_state =
95 (struct auth_ntlmssp_state *)ntlmssp_state->callback_private;
96 struct auth_context *auth_context = auth_ntlmssp_state->auth_context;
98 SMB_ASSERT(challenge->length == 8);
100 auth_context->challenge = data_blob_talloc(auth_context,
101 challenge->data, challenge->length);
103 auth_context->challenge_set_by = "NTLMSSP callback (NTLM2)";
105 DEBUG(5, ("auth_context challenge set by %s\n", auth_context->challenge_set_by));
106 DEBUG(5, ("challenge is: \n"));
107 dump_data(5, auth_context->challenge.data, auth_context->challenge.length);
112 * Check the password on an NTLMSSP login.
114 * Return the session keys used on the connection.
117 static NTSTATUS auth_ntlmssp_check_password(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
119 struct auth_ntlmssp_state *auth_ntlmssp_state =
120 (struct auth_ntlmssp_state *)ntlmssp_state->callback_private;
121 struct auth_usersupplied_info *user_info = NULL;
123 bool username_was_mapped;
125 /* the client has given us its machine name (which we otherwise would not get on port 445).
126 we need to possibly reload smb.conf if smb.conf includes depend on the machine name */
128 set_remote_machine_name(auth_ntlmssp_state->ntlmssp_state->client.netbios_name, True);
130 /* setup the string used by %U */
131 /* sub_set_smb_name checks for weird internally */
132 sub_set_smb_name(auth_ntlmssp_state->ntlmssp_state->user);
134 reload_services(True);
136 nt_status = make_user_info_map(&user_info,
137 auth_ntlmssp_state->ntlmssp_state->user,
138 auth_ntlmssp_state->ntlmssp_state->domain,
139 auth_ntlmssp_state->ntlmssp_state->client.netbios_name,
140 auth_ntlmssp_state->ntlmssp_state->lm_resp.data ? &auth_ntlmssp_state->ntlmssp_state->lm_resp : NULL,
141 auth_ntlmssp_state->ntlmssp_state->nt_resp.data ? &auth_ntlmssp_state->ntlmssp_state->nt_resp : NULL,
145 user_info->logon_parameters = MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT;
147 if (!NT_STATUS_IS_OK(nt_status)) {
151 nt_status = auth_ntlmssp_state->auth_context->check_ntlm_password(auth_ntlmssp_state->auth_context,
152 user_info, &auth_ntlmssp_state->server_info);
154 username_was_mapped = user_info->was_mapped;
156 free_user_info(&user_info);
158 if (!NT_STATUS_IS_OK(nt_status)) {
162 auth_ntlmssp_state->server_info->nss_token |= username_was_mapped;
164 nt_status = create_local_token(auth_ntlmssp_state->server_info);
166 if (!NT_STATUS_IS_OK(nt_status)) {
167 DEBUG(10, ("create_local_token failed: %s\n",
168 nt_errstr(nt_status)));
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 = data_blob_talloc(auth_ntlmssp_state,
176 auth_ntlmssp_state->server_info->user_session_key.data,
177 auth_ntlmssp_state->server_info->user_session_key.length);
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 = data_blob_talloc(auth_ntlmssp_state,
183 auth_ntlmssp_state->server_info->lm_session_key.data,
184 auth_ntlmssp_state->server_info->lm_session_key.length);
189 static int auth_ntlmssp_state_destructor(void *ptr);
191 NTSTATUS auth_ntlmssp_start(struct auth_ntlmssp_state **auth_ntlmssp_state)
195 const char *netbios_name;
196 const char *netbios_domain;
197 const char *dns_name;
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;
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());
213 strlower_m(dns_domain);
215 dns_name = get_mydnsfullname();
217 ans = talloc_zero(NULL, struct auth_ntlmssp_state);
219 DEBUG(0,("auth_ntlmssp_start: talloc failed!\n"));
220 return NT_STATUS_NO_MEMORY;
223 nt_status = ntlmssp_server_start(ans,
229 &ans->ntlmssp_state);
230 if (!NT_STATUS_IS_OK(nt_status)) {
234 nt_status = make_auth_context_subsystem(&auth_context);
235 if (!NT_STATUS_IS_OK(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;
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);