BUG 1080: fix declaration of SMB_BIG_UINT
[Samba.git] / source3 / auth / auth_ntlmssp.c
bloba5ce101e5e7360b46e458918f0e6dacd549ad2ff
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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "includes.h"
26 /**
27 * Return the challenge as determined by the authentication subsystem
28 * @return an 8 byte random challenge
31 static const uint8 *auth_ntlmssp_get_challenge(const struct ntlmssp_state *ntlmssp_state)
33 AUTH_NTLMSSP_STATE *auth_ntlmssp_state = ntlmssp_state->auth_context;
34 return auth_ntlmssp_state->auth_context->get_ntlm_challenge(auth_ntlmssp_state->auth_context);
37 /**
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 = ntlmssp_state->auth_context;
45 struct auth_context *auth_context = auth_ntlmssp_state->auth_context;
47 return auth_context->challenge_may_be_modified;
50 /**
51 * NTLM2 authentication modifies the effective challange,
52 * @param challenge The new challenge value
54 static NTSTATUS auth_ntlmssp_set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge)
56 AUTH_NTLMSSP_STATE *auth_ntlmssp_state = ntlmssp_state->auth_context;
57 struct auth_context *auth_context = auth_ntlmssp_state->auth_context;
59 SMB_ASSERT(challenge->length == 8);
61 auth_context->challenge = data_blob_talloc(auth_context->mem_ctx,
62 challenge->data, challenge->length);
64 auth_context->challenge_set_by = "NTLMSSP callback (NTLM2)";
66 DEBUG(5, ("auth_context challenge set by %s\n", auth_context->challenge_set_by));
67 DEBUG(5, ("challenge is: \n"));
68 dump_data(5, (const char *)auth_context->challenge.data, auth_context->challenge.length);
69 return NT_STATUS_OK;
72 /**
73 * Check the password on an NTLMSSP login.
75 * Return the session keys used on the connection.
78 static NTSTATUS auth_ntlmssp_check_password(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *nt_session_key, DATA_BLOB *lm_session_key)
80 AUTH_NTLMSSP_STATE *auth_ntlmssp_state = ntlmssp_state->auth_context;
81 uint32 auth_flags = AUTH_FLAG_NONE;
82 auth_usersupplied_info *user_info = NULL;
83 DATA_BLOB plaintext_password = data_blob(NULL, 0);
84 NTSTATUS nt_status;
86 if (auth_ntlmssp_state->ntlmssp_state->lm_resp.length) {
87 auth_flags |= AUTH_FLAG_LM_RESP;
90 if (auth_ntlmssp_state->ntlmssp_state->nt_resp.length == 24) {
91 auth_flags |= AUTH_FLAG_NTLM_RESP;
92 } else if (auth_ntlmssp_state->ntlmssp_state->nt_resp.length > 24) {
93 auth_flags |= AUTH_FLAG_NTLMv2_RESP;
96 /* the client has given us its machine name (which we otherwise would not get on port 445).
97 we need to possibly reload smb.conf if smb.conf includes depend on the machine name */
99 set_remote_machine_name(auth_ntlmssp_state->ntlmssp_state->workstation, True);
101 /* setup the string used by %U */
102 /* sub_set_smb_name checks for weird internally */
103 sub_set_smb_name(auth_ntlmssp_state->ntlmssp_state->user);
105 reload_services(True);
107 nt_status = make_user_info_map(&user_info,
108 auth_ntlmssp_state->ntlmssp_state->user,
109 auth_ntlmssp_state->ntlmssp_state->domain,
110 auth_ntlmssp_state->ntlmssp_state->workstation,
111 auth_ntlmssp_state->ntlmssp_state->lm_resp,
112 auth_ntlmssp_state->ntlmssp_state->nt_resp,
113 plaintext_password,
114 auth_flags, True);
116 if (!NT_STATUS_IS_OK(nt_status)) {
117 return nt_status;
120 nt_status = auth_ntlmssp_state->auth_context->check_ntlm_password(auth_ntlmssp_state->auth_context,
121 user_info, &auth_ntlmssp_state->server_info);
123 free_user_info(&user_info);
125 if (!NT_STATUS_IS_OK(nt_status)) {
126 return nt_status;
128 if (auth_ntlmssp_state->server_info->nt_session_key.length) {
129 DEBUG(10, ("Got NT session key of length %u\n", auth_ntlmssp_state->server_info->nt_session_key.length));
130 *nt_session_key = data_blob_talloc(auth_ntlmssp_state->mem_ctx,
131 auth_ntlmssp_state->server_info->nt_session_key.data,
132 auth_ntlmssp_state->server_info->nt_session_key.length);
134 if (auth_ntlmssp_state->server_info->lm_session_key.length) {
135 DEBUG(10, ("Got LM session key of length %u\n", auth_ntlmssp_state->server_info->lm_session_key.length));
136 *lm_session_key = data_blob_talloc(auth_ntlmssp_state->mem_ctx,
137 auth_ntlmssp_state->server_info->lm_session_key.data,
138 auth_ntlmssp_state->server_info->lm_session_key.length);
140 return nt_status;
143 NTSTATUS auth_ntlmssp_start(AUTH_NTLMSSP_STATE **auth_ntlmssp_state)
145 NTSTATUS nt_status;
146 TALLOC_CTX *mem_ctx;
148 mem_ctx = talloc_init("AUTH NTLMSSP context");
150 *auth_ntlmssp_state = talloc_zero(mem_ctx, sizeof(**auth_ntlmssp_state));
151 if (!*auth_ntlmssp_state) {
152 DEBUG(0,("auth_ntlmssp_start: talloc failed!\n"));
153 talloc_destroy(mem_ctx);
154 return NT_STATUS_NO_MEMORY;
157 ZERO_STRUCTP(*auth_ntlmssp_state);
159 (*auth_ntlmssp_state)->mem_ctx = mem_ctx;
161 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_server_start(&(*auth_ntlmssp_state)->ntlmssp_state))) {
162 return nt_status;
165 if (!NT_STATUS_IS_OK(nt_status = make_auth_context_subsystem(&(*auth_ntlmssp_state)->auth_context))) {
166 return nt_status;
169 (*auth_ntlmssp_state)->ntlmssp_state->auth_context = (*auth_ntlmssp_state);
170 (*auth_ntlmssp_state)->ntlmssp_state->get_challenge = auth_ntlmssp_get_challenge;
171 (*auth_ntlmssp_state)->ntlmssp_state->may_set_challenge = auth_ntlmssp_may_set_challenge;
172 (*auth_ntlmssp_state)->ntlmssp_state->set_challenge = auth_ntlmssp_set_challenge;
173 (*auth_ntlmssp_state)->ntlmssp_state->check_password = auth_ntlmssp_check_password;
174 (*auth_ntlmssp_state)->ntlmssp_state->server_role = lp_server_role();
176 return NT_STATUS_OK;
179 void auth_ntlmssp_end(AUTH_NTLMSSP_STATE **auth_ntlmssp_state)
181 TALLOC_CTX *mem_ctx = (*auth_ntlmssp_state)->mem_ctx;
183 if ((*auth_ntlmssp_state)->ntlmssp_state) {
184 ntlmssp_end(&(*auth_ntlmssp_state)->ntlmssp_state);
186 if ((*auth_ntlmssp_state)->auth_context) {
187 ((*auth_ntlmssp_state)->auth_context->free)(&(*auth_ntlmssp_state)->auth_context);
189 if ((*auth_ntlmssp_state)->server_info) {
190 free_server_info(&(*auth_ntlmssp_state)->server_info);
192 talloc_destroy(mem_ctx);
193 *auth_ntlmssp_state = NULL;
196 NTSTATUS auth_ntlmssp_update(AUTH_NTLMSSP_STATE *auth_ntlmssp_state,
197 const DATA_BLOB request, DATA_BLOB *reply)
199 return ntlmssp_update(auth_ntlmssp_state->ntlmssp_state, request, reply);