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/>.
26 * Return the challenge as determined by the authentication subsystem
27 * @return an 8 byte random challenge
30 static const uint8 *auth_ntlmssp_get_challenge(const struct ntlmssp_state *ntlmssp_state)
32 AUTH_NTLMSSP_STATE *auth_ntlmssp_state =
33 (AUTH_NTLMSSP_STATE *)ntlmssp_state->auth_context;
34 return auth_ntlmssp_state->auth_context->get_ntlm_challenge(auth_ntlmssp_state->auth_context);
38 * Some authentication methods 'fix' the challenge, so we may not be able to set it
40 * @return If the effective challenge used by the auth subsystem may be modified
42 static bool auth_ntlmssp_may_set_challenge(const struct ntlmssp_state *ntlmssp_state)
44 AUTH_NTLMSSP_STATE *auth_ntlmssp_state =
45 (AUTH_NTLMSSP_STATE *)ntlmssp_state->auth_context;
46 struct auth_context *auth_context = auth_ntlmssp_state->auth_context;
48 return auth_context->challenge_may_be_modified;
52 * NTLM2 authentication modifies the effective challenge,
53 * @param challenge The new challenge value
55 static NTSTATUS auth_ntlmssp_set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge)
57 AUTH_NTLMSSP_STATE *auth_ntlmssp_state =
58 (AUTH_NTLMSSP_STATE *)ntlmssp_state->auth_context;
59 struct auth_context *auth_context = auth_ntlmssp_state->auth_context;
61 SMB_ASSERT(challenge->length == 8);
63 auth_context->challenge = data_blob_talloc(auth_context->mem_ctx,
64 challenge->data, challenge->length);
66 auth_context->challenge_set_by = "NTLMSSP callback (NTLM2)";
68 DEBUG(5, ("auth_context challenge set by %s\n", auth_context->challenge_set_by));
69 DEBUG(5, ("challenge is: \n"));
70 dump_data(5, auth_context->challenge.data, auth_context->challenge.length);
75 * Check the password on an NTLMSSP login.
77 * Return the session keys used on the connection.
80 static NTSTATUS auth_ntlmssp_check_password(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
82 AUTH_NTLMSSP_STATE *auth_ntlmssp_state =
83 (AUTH_NTLMSSP_STATE *)ntlmssp_state->auth_context;
84 auth_usersupplied_info *user_info = NULL;
86 bool username_was_mapped;
88 /* the client has given us its machine name (which we otherwise would not get on port 445).
89 we need to possibly reload smb.conf if smb.conf includes depend on the machine name */
91 set_remote_machine_name(auth_ntlmssp_state->ntlmssp_state->workstation, True);
93 /* setup the string used by %U */
94 /* sub_set_smb_name checks for weird internally */
95 sub_set_smb_name(auth_ntlmssp_state->ntlmssp_state->user);
97 reload_services(True);
99 nt_status = make_user_info_map(&user_info,
100 auth_ntlmssp_state->ntlmssp_state->user,
101 auth_ntlmssp_state->ntlmssp_state->domain,
102 auth_ntlmssp_state->ntlmssp_state->workstation,
103 auth_ntlmssp_state->ntlmssp_state->lm_resp.data ? &auth_ntlmssp_state->ntlmssp_state->lm_resp : NULL,
104 auth_ntlmssp_state->ntlmssp_state->nt_resp.data ? &auth_ntlmssp_state->ntlmssp_state->nt_resp : NULL,
108 user_info->logon_parameters = MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT;
110 if (!NT_STATUS_IS_OK(nt_status)) {
114 nt_status = auth_ntlmssp_state->auth_context->check_ntlm_password(auth_ntlmssp_state->auth_context,
115 user_info, &auth_ntlmssp_state->server_info);
117 username_was_mapped = user_info->was_mapped;
119 free_user_info(&user_info);
121 if (!NT_STATUS_IS_OK(nt_status)) {
125 auth_ntlmssp_state->server_info->was_mapped |= username_was_mapped;
127 nt_status = create_local_token(auth_ntlmssp_state->server_info);
129 if (!NT_STATUS_IS_OK(nt_status)) {
130 DEBUG(10, ("create_local_token failed\n"));
134 if (auth_ntlmssp_state->server_info->user_session_key.length) {
135 DEBUG(10, ("Got NT session key of length %u\n",
136 (unsigned int)auth_ntlmssp_state->server_info->user_session_key.length));
137 *user_session_key = data_blob_talloc(auth_ntlmssp_state->mem_ctx,
138 auth_ntlmssp_state->server_info->user_session_key.data,
139 auth_ntlmssp_state->server_info->user_session_key.length);
141 if (auth_ntlmssp_state->server_info->lm_session_key.length) {
142 DEBUG(10, ("Got LM session key of length %u\n",
143 (unsigned int)auth_ntlmssp_state->server_info->lm_session_key.length));
144 *lm_session_key = data_blob_talloc(auth_ntlmssp_state->mem_ctx,
145 auth_ntlmssp_state->server_info->lm_session_key.data,
146 auth_ntlmssp_state->server_info->lm_session_key.length);
151 NTSTATUS auth_ntlmssp_start(AUTH_NTLMSSP_STATE **auth_ntlmssp_state)
156 mem_ctx = talloc_init("AUTH NTLMSSP context");
158 *auth_ntlmssp_state = TALLOC_ZERO_P(mem_ctx, AUTH_NTLMSSP_STATE);
159 if (!*auth_ntlmssp_state) {
160 DEBUG(0,("auth_ntlmssp_start: talloc failed!\n"));
161 talloc_destroy(mem_ctx);
162 return NT_STATUS_NO_MEMORY;
165 ZERO_STRUCTP(*auth_ntlmssp_state);
167 (*auth_ntlmssp_state)->mem_ctx = mem_ctx;
169 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_server_start(&(*auth_ntlmssp_state)->ntlmssp_state))) {
173 if (!NT_STATUS_IS_OK(nt_status = make_auth_context_subsystem(&(*auth_ntlmssp_state)->auth_context))) {
177 (*auth_ntlmssp_state)->ntlmssp_state->auth_context = (*auth_ntlmssp_state);
178 (*auth_ntlmssp_state)->ntlmssp_state->get_challenge = auth_ntlmssp_get_challenge;
179 (*auth_ntlmssp_state)->ntlmssp_state->may_set_challenge = auth_ntlmssp_may_set_challenge;
180 (*auth_ntlmssp_state)->ntlmssp_state->set_challenge = auth_ntlmssp_set_challenge;
181 (*auth_ntlmssp_state)->ntlmssp_state->check_password = auth_ntlmssp_check_password;
182 (*auth_ntlmssp_state)->ntlmssp_state->server_role = (enum server_types)lp_server_role();
187 void auth_ntlmssp_end(AUTH_NTLMSSP_STATE **auth_ntlmssp_state)
191 if (*auth_ntlmssp_state == NULL) {
195 mem_ctx = (*auth_ntlmssp_state)->mem_ctx;
196 if ((*auth_ntlmssp_state)->ntlmssp_state) {
197 ntlmssp_end(&(*auth_ntlmssp_state)->ntlmssp_state);
199 if ((*auth_ntlmssp_state)->auth_context) {
200 ((*auth_ntlmssp_state)->auth_context->free)(&(*auth_ntlmssp_state)->auth_context);
202 if ((*auth_ntlmssp_state)->server_info) {
203 TALLOC_FREE((*auth_ntlmssp_state)->server_info);
205 talloc_destroy(mem_ctx);
206 *auth_ntlmssp_state = NULL;
209 NTSTATUS auth_ntlmssp_update(AUTH_NTLMSSP_STATE *auth_ntlmssp_state,
210 const DATA_BLOB request, DATA_BLOB *reply)
212 return ntlmssp_update(auth_ntlmssp_state->ntlmssp_state, request, reply);