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