693613f5ec76919ebc629af4fa01a26d2571490c
[ira/wip.git] / source4 / auth / ntlmssp / ntlmssp_server.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    handle NLTMSSP, client server side parsing
5
6    Copyright (C) Andrew Tridgell      2001
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2005
8    Copyright (C) Stefan Metzmacher 2005
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "system/network.h"
26 #include "lib/tsocket/tsocket.h"
27 #include "auth/ntlmssp/ntlmssp.h"
28 #include "../librpc/gen_ndr/ndr_ntlmssp.h"
29 #include "auth/ntlmssp/ntlmssp_ndr.h"
30 #include "auth/ntlmssp/ntlmssp_private.h"
31 #include "../libcli/auth/libcli_auth.h"
32 #include "../lib/crypto/crypto.h"
33 #include "auth/gensec/gensec.h"
34 #include "auth/gensec/gensec_proto.h"
35 #include "auth/auth.h"
36 #include "param/param.h"
37 #include "source4/auth/ntlmssp/proto.h"
38
39 /**
40  * Next state function for the Negotiate packet (GENSEC wrapper)
41  *
42  * @param gensec_security GENSEC state
43  * @param out_mem_ctx Memory context for *out
44  * @param in The request, as a DATA_BLOB.  reply.data must be NULL
45  * @param out The reply, as an allocated DATA_BLOB, caller to free.
46  * @return Errors or MORE_PROCESSING_REQUIRED if (normal) a reply is required.
47  */
48
49 NTSTATUS gensec_ntlmssp_server_negotiate(struct gensec_security *gensec_security,
50                                          TALLOC_CTX *out_mem_ctx,
51                                          const DATA_BLOB request, DATA_BLOB *reply)
52 {
53         struct gensec_ntlmssp_context *gensec_ntlmssp =
54                 talloc_get_type_abort(gensec_security->private_data,
55                                       struct gensec_ntlmssp_context);
56         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
57         return ntlmssp_server_negotiate(ntlmssp_state, out_mem_ctx, request, reply);
58 }
59
60 /**
61  * Next state function for the Authenticate packet (GENSEC wrapper)
62  *
63  * @param gensec_security GENSEC state
64  * @param out_mem_ctx Memory context for *out
65  * @param in The request, as a DATA_BLOB.  reply.data must be NULL
66  * @param out The reply, as an allocated DATA_BLOB, caller to free.
67  * @return Errors or NT_STATUS_OK if authentication sucessful
68  */
69
70 NTSTATUS gensec_ntlmssp_server_auth(struct gensec_security *gensec_security,
71                                     TALLOC_CTX *out_mem_ctx,
72                                     const DATA_BLOB in, DATA_BLOB *out)
73 {
74         struct gensec_ntlmssp_context *gensec_ntlmssp =
75                 talloc_get_type_abort(gensec_security->private_data,
76                                       struct gensec_ntlmssp_context);
77         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
78         return ntlmssp_server_auth(ntlmssp_state, out_mem_ctx, in, out);
79 }
80
81 /**
82  * Return the challenge as determined by the authentication subsystem 
83  * @return an 8 byte random challenge
84  */
85
86 static NTSTATUS auth_ntlmssp_get_challenge(const struct ntlmssp_state *ntlmssp_state,
87                                            uint8_t chal[8])
88 {
89         struct gensec_ntlmssp_context *gensec_ntlmssp =
90                 talloc_get_type_abort(ntlmssp_state->callback_private,
91                                       struct gensec_ntlmssp_context);
92         struct auth4_context *auth_context = gensec_ntlmssp->gensec_security->auth_context;
93         NTSTATUS status = NT_STATUS_NOT_IMPLEMENTED;
94
95         if (auth_context->get_challenge) {
96                 status = auth_context->get_challenge(auth_context, chal);
97                 if (!NT_STATUS_IS_OK(status)) {
98                         DEBUG(1, ("auth_ntlmssp_get_challenge: failed to get challenge: %s\n",
99                                   nt_errstr(status)));
100                         return status;
101                 }
102         }
103
104         return status;
105 }
106
107 /**
108  * Some authentication methods 'fix' the challenge, so we may not be able to set it
109  *
110  * @return If the effective challenge used by the auth subsystem may be modified
111  */
112 static bool auth_ntlmssp_may_set_challenge(const struct ntlmssp_state *ntlmssp_state)
113 {
114         struct gensec_ntlmssp_context *gensec_ntlmssp =
115                 talloc_get_type_abort(ntlmssp_state->callback_private,
116                                       struct gensec_ntlmssp_context);
117         struct auth4_context *auth_context = gensec_ntlmssp->gensec_security->auth_context;
118
119         if (auth_context->challenge_may_be_modified) {
120                 return auth_context->challenge_may_be_modified(auth_context);
121         }
122         return false;
123 }
124
125 /**
126  * NTLM2 authentication modifies the effective challenge, 
127  * @param challenge The new challenge value
128  */
129 static NTSTATUS auth_ntlmssp_set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge)
130 {
131         struct gensec_ntlmssp_context *gensec_ntlmssp =
132                 talloc_get_type_abort(ntlmssp_state->callback_private,
133                                       struct gensec_ntlmssp_context);
134         struct auth4_context *auth_context = gensec_ntlmssp->gensec_security->auth_context;
135         NTSTATUS nt_status = NT_STATUS_NOT_IMPLEMENTED;
136         const uint8_t *chal;
137
138         if (challenge->length != 8) {
139                 return NT_STATUS_INVALID_PARAMETER;
140         }
141
142         chal = challenge->data;
143
144         if (auth_context->set_challenge) {
145                 nt_status = auth_context->set_challenge(auth_context,
146                                                         chal,
147                                                         "NTLMSSP callback (NTLM2)");
148         }
149         return nt_status;
150 }
151
152 /**
153  * Check the password on an NTLMSSP login.  
154  *
155  * Return the session keys used on the connection.
156  */
157
158 static NTSTATUS auth_ntlmssp_check_password(struct ntlmssp_state *ntlmssp_state,
159                                             TALLOC_CTX *mem_ctx,
160                                             DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
161 {
162         struct gensec_ntlmssp_context *gensec_ntlmssp =
163                 talloc_get_type_abort(ntlmssp_state->callback_private,
164                                       struct gensec_ntlmssp_context);
165         struct auth4_context *auth_context = gensec_ntlmssp->gensec_security->auth_context;
166         NTSTATUS nt_status = NT_STATUS_NOT_IMPLEMENTED;
167         struct auth_usersupplied_info *user_info;
168
169         user_info = talloc_zero(ntlmssp_state, struct auth_usersupplied_info);
170         if (!user_info) {
171                 return NT_STATUS_NO_MEMORY;
172         }
173
174         user_info->logon_parameters = MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT;
175         user_info->flags = 0;
176         user_info->mapped_state = false;
177         user_info->client.account_name = ntlmssp_state->user;
178         user_info->client.domain_name = ntlmssp_state->domain;
179         user_info->workstation_name = ntlmssp_state->client.netbios_name;
180         user_info->remote_host = gensec_get_remote_address(gensec_ntlmssp->gensec_security);
181
182         user_info->password_state = AUTH_PASSWORD_RESPONSE;
183         user_info->password.response.lanman = ntlmssp_state->lm_resp;
184         user_info->password.response.lanman.data = talloc_steal(user_info, ntlmssp_state->lm_resp.data);
185         user_info->password.response.nt = ntlmssp_state->nt_resp;
186         user_info->password.response.nt.data = talloc_steal(user_info, ntlmssp_state->nt_resp.data);
187
188         if (auth_context->check_password) {
189                 nt_status = auth_context->check_password(auth_context,
190                                                          gensec_ntlmssp,
191                                                          user_info,
192                                                          &gensec_ntlmssp->server_returned_info,
193                                                          user_session_key, lm_session_key);
194         }
195         talloc_free(user_info);
196         NT_STATUS_NOT_OK_RETURN(nt_status);
197
198         talloc_steal(mem_ctx, user_session_key->data);
199         talloc_steal(mem_ctx, lm_session_key->data);
200         
201         return nt_status;
202 }
203
204 /** 
205  * Return the credentials of a logged on user, including session keys
206  * etc.
207  *
208  * Only valid after a successful authentication
209  *
210  * May only be called once per authentication.
211  *
212  */
213
214 NTSTATUS gensec_ntlmssp_session_info(struct gensec_security *gensec_security,
215                                      TALLOC_CTX *mem_ctx,
216                                      struct auth_session_info **session_info) 
217 {
218         NTSTATUS nt_status;
219         struct gensec_ntlmssp_context *gensec_ntlmssp =
220                 talloc_get_type_abort(gensec_security->private_data,
221                                       struct gensec_ntlmssp_context);
222         uint32_t session_info_flags = 0;
223
224         if (gensec_security->want_features & GENSEC_FEATURE_UNIX_TOKEN) {
225                 session_info_flags |= AUTH_SESSION_INFO_UNIX_TOKEN;
226         }
227
228         session_info_flags |= AUTH_SESSION_INFO_DEFAULT_GROUPS;
229
230         if (gensec_security->auth_context && gensec_security->auth_context->generate_session_info) {
231                 nt_status = gensec_security->auth_context->generate_session_info(mem_ctx, gensec_security->auth_context,
232                                                                                  gensec_ntlmssp->server_returned_info,
233                                                                                  gensec_ntlmssp->ntlmssp_state->user,
234                                                                                  session_info_flags,
235                                                                                  session_info);
236         } else {
237                 DEBUG(0, ("Cannot generate a session_info without the auth_context\n"));
238                 return NT_STATUS_INTERNAL_ERROR;
239         }
240         
241         NT_STATUS_NOT_OK_RETURN(nt_status);
242
243         return gensec_ntlmssp_session_key(gensec_security, *session_info,
244                                           &(*session_info)->session_key);
245 }
246
247 /**
248  * Start NTLMSSP on the server side 
249  *
250  */
251 NTSTATUS gensec_ntlmssp_server_start(struct gensec_security *gensec_security)
252 {
253         NTSTATUS nt_status;
254         struct ntlmssp_state *ntlmssp_state;
255         struct gensec_ntlmssp_context *gensec_ntlmssp;
256
257         nt_status = gensec_ntlmssp_start(gensec_security);
258         NT_STATUS_NOT_OK_RETURN(nt_status);
259
260         gensec_ntlmssp =
261                 talloc_get_type_abort(gensec_security->private_data,
262                                       struct gensec_ntlmssp_context);
263
264         ntlmssp_state = talloc_zero(gensec_ntlmssp,
265                                     struct ntlmssp_state);
266         if (!ntlmssp_state) {
267                 return NT_STATUS_NO_MEMORY;
268         }
269
270         ntlmssp_state->callback_private = gensec_ntlmssp;
271
272         gensec_ntlmssp->ntlmssp_state = ntlmssp_state;
273
274         ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
275
276         ntlmssp_state->role = NTLMSSP_SERVER;
277
278         ntlmssp_state->expected_state = NTLMSSP_NEGOTIATE;
279
280         ntlmssp_state->allow_lm_key = (lpcfg_lanman_auth(gensec_security->settings->lp_ctx)
281                                           && gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "allow_lm_key", false));
282
283         ntlmssp_state->neg_flags =
284                 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_VERSION;
285
286         ntlmssp_state->lm_resp = data_blob(NULL, 0);
287         ntlmssp_state->nt_resp = data_blob(NULL, 0);
288
289         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "128bit", true)) {
290                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_128;
291         }
292
293         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "56bit", true)) {
294                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_56;
295         }
296
297         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "keyexchange", true)) {
298                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_KEY_EXCH;
299         }
300
301         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "alwayssign", true)) {
302                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
303         }
304
305         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "ntlm2", true)) {
306                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
307         }
308
309         if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
310                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
311         }
312         if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
313                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
314                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
315         }
316
317         ntlmssp_state->get_challenge = auth_ntlmssp_get_challenge;
318         ntlmssp_state->may_set_challenge = auth_ntlmssp_may_set_challenge;
319         ntlmssp_state->set_challenge = auth_ntlmssp_set_challenge;
320         ntlmssp_state->check_password = auth_ntlmssp_check_password;
321         if (lpcfg_server_role(gensec_security->settings->lp_ctx) == ROLE_STANDALONE) {
322                 ntlmssp_state->server.is_standalone = true;
323         } else {
324                 ntlmssp_state->server.is_standalone = false;
325         }
326
327         ntlmssp_state->server.netbios_name = lpcfg_netbios_name(gensec_security->settings->lp_ctx);
328
329         ntlmssp_state->server.netbios_domain = lpcfg_workgroup(gensec_security->settings->lp_ctx);
330
331         {
332                 const char *dnsdomain = lpcfg_dnsdomain(gensec_security->settings->lp_ctx);
333                 char *dnsname, *lower_netbiosname;
334                 lower_netbiosname = strlower_talloc(ntlmssp_state, ntlmssp_state->server.netbios_name);
335
336                 /* Find out the DNS host name */
337                 if (dnsdomain && dnsdomain[0] != '\0') {
338                         dnsname = talloc_asprintf(ntlmssp_state, "%s.%s",
339                                                   lower_netbiosname,
340                                                   dnsdomain);
341                         talloc_free(lower_netbiosname);
342                         ntlmssp_state->server.dns_name = dnsname;
343                 } else {
344                         ntlmssp_state->server.dns_name = lower_netbiosname;
345                 }
346
347                 NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.dns_name);
348
349                 ntlmssp_state->server.dns_domain
350                         = talloc_strdup(ntlmssp_state,
351                                         lpcfg_dnsdomain(gensec_security->settings->lp_ctx));
352                 NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.dns_domain);
353         }
354
355         return NT_STATUS_OK;
356 }
357