2 Unix SMB/Netbios implementation.
4 handle NLTMSSP, server side
6 Copyright (C) Andrew Tridgell 2001
7 Copyright (C) Andrew Bartlett 2001-2003,2011
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/>.
25 #include "../auth/ntlmssp/ntlmssp.h"
26 #include "../librpc/gen_ndr/netlogon.h"
27 #include "../librpc/gen_ndr/dcerpc.h"
28 #include "../lib/tsocket/tsocket.h"
29 #include "auth/gensec/gensec.h"
30 #include "librpc/rpc/dcerpc.h"
31 #include "lib/param/param.h"
33 static NTSTATUS
gensec_ntlmssp3_server_session_info(struct gensec_security
*gensec_security
,
35 struct auth_session_info
**session_info
)
37 struct gensec_ntlmssp_context
*gensec_ntlmssp
=
38 talloc_get_type_abort(gensec_security
->private_data
,
39 struct gensec_ntlmssp_context
);
40 struct auth_serversupplied_info
*server_info
= talloc_get_type_abort(gensec_ntlmssp
->server_returned_info
,
41 struct auth_serversupplied_info
);
44 nt_status
= create_local_token(mem_ctx
,
46 &gensec_ntlmssp
->ntlmssp_state
->session_key
,
47 gensec_ntlmssp
->ntlmssp_state
->user
,
49 if (!NT_STATUS_IS_OK(nt_status
)) {
50 DEBUG(10, ("create_local_token failed: %s\n",
51 nt_errstr(nt_status
)));
58 static NTSTATUS
gensec_ntlmssp3_server_update(struct gensec_security
*gensec_security
,
59 TALLOC_CTX
*out_mem_ctx
,
60 struct tevent_context
*ev
,
61 const DATA_BLOB request
,
65 struct gensec_ntlmssp_context
*gensec_ntlmssp
=
66 talloc_get_type_abort(gensec_security
->private_data
,
67 struct gensec_ntlmssp_context
);
69 status
= ntlmssp_update(gensec_ntlmssp
->ntlmssp_state
, request
, reply
);
70 if (NT_STATUS_IS_OK(status
) ||
71 NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
72 talloc_steal(out_mem_ctx
, reply
->data
);
79 * Return the challenge as determined by the authentication subsystem
80 * @return an 8 byte random challenge
83 static NTSTATUS
auth_ntlmssp_get_challenge(const struct ntlmssp_state
*ntlmssp_state
,
86 struct gensec_ntlmssp_context
*gensec_ntlmssp
=
87 (struct gensec_ntlmssp_context
*)ntlmssp_state
->callback_private
;
88 gensec_ntlmssp
->auth_context
->get_ntlm_challenge(
89 gensec_ntlmssp
->auth_context
, chal
);
94 * Some authentication methods 'fix' the challenge, so we may not be able to set it
96 * @return If the effective challenge used by the auth subsystem may be modified
98 static bool auth_ntlmssp_may_set_challenge(const struct ntlmssp_state
*ntlmssp_state
)
100 struct gensec_ntlmssp_context
*gensec_ntlmssp
=
101 (struct gensec_ntlmssp_context
*)ntlmssp_state
->callback_private
;
102 struct auth_context
*auth_context
= gensec_ntlmssp
->auth_context
;
104 return auth_context
->challenge_may_be_modified
;
108 * NTLM2 authentication modifies the effective challenge,
109 * @param challenge The new challenge value
111 static NTSTATUS
auth_ntlmssp_set_challenge(struct ntlmssp_state
*ntlmssp_state
, DATA_BLOB
*challenge
)
113 struct gensec_ntlmssp_context
*gensec_ntlmssp
=
114 (struct gensec_ntlmssp_context
*)ntlmssp_state
->callback_private
;
115 struct auth_context
*auth_context
= gensec_ntlmssp
->auth_context
;
117 SMB_ASSERT(challenge
->length
== 8);
119 auth_context
->challenge
= data_blob_talloc(auth_context
,
120 challenge
->data
, challenge
->length
);
122 auth_context
->challenge_set_by
= "NTLMSSP callback (NTLM2)";
124 DEBUG(5, ("auth_context challenge set by %s\n", auth_context
->challenge_set_by
));
125 DEBUG(5, ("challenge is: \n"));
126 dump_data(5, auth_context
->challenge
.data
, auth_context
->challenge
.length
);
131 * Check the password on an NTLMSSP login.
133 * Return the session keys used on the connection.
136 static NTSTATUS
auth_ntlmssp_check_password(struct ntlmssp_state
*ntlmssp_state
, TALLOC_CTX
*mem_ctx
,
137 DATA_BLOB
*session_key
, DATA_BLOB
*lm_session_key
)
139 struct gensec_ntlmssp_context
*gensec_ntlmssp
=
140 (struct gensec_ntlmssp_context
*)ntlmssp_state
->callback_private
;
141 struct auth_usersupplied_info
*user_info
= NULL
;
142 struct auth_serversupplied_info
*server_info
;
144 bool username_was_mapped
;
146 /* The client has given us its machine name (which we only get over NBT transport).
147 We need to possibly reload smb.conf if smb.conf includes depend on the machine name. */
149 set_remote_machine_name(gensec_ntlmssp
->ntlmssp_state
->client
.netbios_name
, True
);
151 /* setup the string used by %U */
152 /* sub_set_smb_name checks for weird internally */
153 sub_set_smb_name(gensec_ntlmssp
->ntlmssp_state
->user
);
155 lp_load(get_dyn_CONFIGFILE(), false, false, true, true);
157 nt_status
= make_user_info_map(&user_info
,
158 gensec_ntlmssp
->ntlmssp_state
->user
,
159 gensec_ntlmssp
->ntlmssp_state
->domain
,
160 gensec_ntlmssp
->ntlmssp_state
->client
.netbios_name
,
161 gensec_get_remote_address(gensec_ntlmssp
->gensec_security
),
162 gensec_ntlmssp
->ntlmssp_state
->lm_resp
.data
? &gensec_ntlmssp
->ntlmssp_state
->lm_resp
: NULL
,
163 gensec_ntlmssp
->ntlmssp_state
->nt_resp
.data
? &gensec_ntlmssp
->ntlmssp_state
->nt_resp
: NULL
,
165 AUTH_PASSWORD_RESPONSE
);
167 if (!NT_STATUS_IS_OK(nt_status
)) {
171 user_info
->logon_parameters
= MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT
| MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
;
173 nt_status
= gensec_ntlmssp
->auth_context
->check_ntlm_password(gensec_ntlmssp
->auth_context
,
174 user_info
, &server_info
);
176 username_was_mapped
= user_info
->was_mapped
;
178 free_user_info(&user_info
);
180 if (!NT_STATUS_IS_OK(nt_status
)) {
181 nt_status
= do_map_to_guest_server_info(nt_status
,
183 gensec_ntlmssp
->ntlmssp_state
->user
,
184 gensec_ntlmssp
->ntlmssp_state
->domain
);
185 gensec_ntlmssp
->server_returned_info
= server_info
;
189 server_info
->nss_token
|= username_was_mapped
;
191 /* Clear out the session keys, and pass them to the caller.
192 * They will not be used in this form again - instead the
193 * NTLMSSP code will decide on the final correct session key,
194 * and supply it to create_local_token() */
195 if (server_info
->session_key
.length
) {
196 DEBUG(10, ("Got NT session key of length %u\n",
197 (unsigned int)server_info
->session_key
.length
));
198 *session_key
= server_info
->session_key
;
199 talloc_steal(mem_ctx
, server_info
->session_key
.data
);
200 server_info
->session_key
= data_blob_null
;
202 if (server_info
->lm_session_key
.length
) {
203 DEBUG(10, ("Got LM session key of length %u\n",
204 (unsigned int)server_info
->lm_session_key
.length
));
205 *lm_session_key
= server_info
->lm_session_key
;
206 talloc_steal(mem_ctx
, server_info
->lm_session_key
.data
);
207 server_info
->lm_session_key
= data_blob_null
;
209 gensec_ntlmssp
->server_returned_info
= server_info
;
213 static NTSTATUS
gensec_ntlmssp3_server_start(struct gensec_security
*gensec_security
)
217 const char *netbios_name
;
218 const char *netbios_domain
;
219 const char *dns_name
;
221 struct gensec_ntlmssp_context
*gensec_ntlmssp
;
223 if ((enum server_role
)lp_server_role() == ROLE_STANDALONE
) {
224 is_standalone
= true;
226 is_standalone
= false;
229 netbios_name
= lp_netbios_name();
230 netbios_domain
= lp_workgroup();
231 /* This should be a 'netbios domain -> DNS domain' mapping */
232 dns_domain
= get_mydnsdomname(talloc_tos());
234 strlower_m(dns_domain
);
236 dns_name
= get_mydnsfullname();
238 nt_status
= gensec_ntlmssp_start(gensec_security
);
239 NT_STATUS_NOT_OK_RETURN(nt_status
);
242 talloc_get_type_abort(gensec_security
->private_data
,
243 struct gensec_ntlmssp_context
);
245 nt_status
= make_auth_context_subsystem(gensec_ntlmssp
, &gensec_ntlmssp
->auth_context
);
246 if (!NT_STATUS_IS_OK(nt_status
)) {
250 nt_status
= ntlmssp_server_start(gensec_ntlmssp
,
256 &gensec_ntlmssp
->ntlmssp_state
);
257 if (!NT_STATUS_IS_OK(nt_status
)) {
261 gensec_ntlmssp
->ntlmssp_state
->callback_private
= gensec_ntlmssp
;
263 gensec_ntlmssp
->ntlmssp_state
->get_challenge
= auth_ntlmssp_get_challenge
;
264 gensec_ntlmssp
->ntlmssp_state
->may_set_challenge
= auth_ntlmssp_may_set_challenge
;
265 gensec_ntlmssp
->ntlmssp_state
->set_challenge
= auth_ntlmssp_set_challenge
;
266 gensec_ntlmssp
->ntlmssp_state
->check_password
= auth_ntlmssp_check_password
;
268 if (gensec_ntlmssp
->gensec_security
->want_features
& GENSEC_FEATURE_SESSION_KEY
) {
269 gensec_ntlmssp
->ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_SIGN
;
271 if (gensec_ntlmssp
->gensec_security
->want_features
& GENSEC_FEATURE_SIGN
) {
272 gensec_ntlmssp
->ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_SIGN
;
274 if (gensec_ntlmssp
->gensec_security
->want_features
& GENSEC_FEATURE_SEAL
) {
275 gensec_ntlmssp
->ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_SIGN
;
276 gensec_ntlmssp
->ntlmssp_state
->neg_flags
|= NTLMSSP_NEGOTIATE_SEAL
;
282 static const char *gensec_ntlmssp3_server_oids
[] = {
287 const struct gensec_security_ops gensec_ntlmssp3_server_ops
= {
288 .name
= "ntlmssp3_server",
289 .sasl_name
= GENSEC_SASL_NAME_NTLMSSP
, /* "NTLM" */
290 .auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
,
291 .oid
= gensec_ntlmssp3_server_oids
,
292 .server_start
= gensec_ntlmssp3_server_start
,
293 .magic
= gensec_ntlmssp_magic
,
294 .update
= gensec_ntlmssp3_server_update
,
295 .sig_size
= gensec_ntlmssp_sig_size
,
296 .sign_packet
= gensec_ntlmssp_sign_packet
,
297 .check_packet
= gensec_ntlmssp_check_packet
,
298 .seal_packet
= gensec_ntlmssp_seal_packet
,
299 .unseal_packet
= gensec_ntlmssp_unseal_packet
,
300 .wrap
= gensec_ntlmssp_wrap
,
301 .unwrap
= gensec_ntlmssp_unwrap
,
302 .session_key
= gensec_ntlmssp_session_key
,
303 .session_info
= gensec_ntlmssp3_server_session_info
,
304 .have_feature
= gensec_ntlmssp_have_feature
,
306 .priority
= GENSEC_NTLMSSP