s3-auth: Use the gensec-supplied DNS domain name and hostname.
[kai/samba.git] / source3 / auth / auth_ntlmssp.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    handle NLTMSSP, server side
5
6    Copyright (C) Andrew Tridgell      2001
7    Copyright (C) Andrew Bartlett 2001-2005,2011
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 "auth.h"
26 #include "../auth/ntlmssp/ntlmssp.h"
27 #include "../auth/ntlmssp/ntlmssp_private.h"
28 #include "../librpc/gen_ndr/netlogon.h"
29 #include "../librpc/gen_ndr/dcerpc.h"
30 #include "../lib/tsocket/tsocket.h"
31 #include "auth/gensec/gensec.h"
32 #include "librpc/rpc/dcerpc.h"
33 #include "lib/param/param.h"
34
35 NTSTATUS auth3_generate_session_info(TALLOC_CTX *mem_ctx,
36                                      struct auth4_context *auth_context,
37                                      void *server_returned_info,
38                                      const char *original_user_name,
39                                      uint32_t session_info_flags,
40                                      struct auth_session_info **session_info)
41 {
42         struct auth_serversupplied_info *server_info = talloc_get_type_abort(server_returned_info,
43                                                                              struct auth_serversupplied_info);
44         NTSTATUS nt_status;
45
46         nt_status = create_local_token(mem_ctx,
47                                        server_info,
48                                        NULL,
49                                        original_user_name,
50                                        session_info);
51         if (!NT_STATUS_IS_OK(nt_status)) {
52                 DEBUG(10, ("create_local_token failed: %s\n",
53                            nt_errstr(nt_status)));
54                 return nt_status;
55         }
56
57         return NT_STATUS_OK;
58 }
59
60 /**
61  * Return the challenge as determined by the authentication subsystem 
62  * @return an 8 byte random challenge
63  */
64
65 NTSTATUS auth3_get_challenge(struct auth4_context *auth4_context,
66                                            uint8_t chal[8])
67 {
68         struct auth_context *auth_context = talloc_get_type_abort(auth4_context->private_data,
69                                                                   struct auth_context);
70         auth_context->get_ntlm_challenge(auth_context, chal);
71         return NT_STATUS_OK;
72 }
73
74 /**
75  * Some authentication methods 'fix' the challenge, so we may not be able to set it
76  *
77  * @return If the effective challenge used by the auth subsystem may be modified
78  */
79 bool auth3_may_set_challenge(struct auth4_context *auth4_context)
80 {
81         struct auth_context *auth_context = talloc_get_type_abort(auth4_context->private_data,
82                                                                   struct auth_context);
83         return auth_context->challenge_may_be_modified;
84 }
85
86 /**
87  * NTLM2 authentication modifies the effective challenge, 
88  * @param challenge The new challenge value
89  */
90 NTSTATUS auth3_set_challenge(struct auth4_context *auth4_context, const uint8_t *chal,
91                              const char *challenge_set_by)
92 {
93         struct auth_context *auth_context = talloc_get_type_abort(auth4_context->private_data,
94                                                                   struct auth_context);
95
96         auth_context->challenge = data_blob_talloc(auth_context,
97                                                    chal, 8);
98         NT_STATUS_HAVE_NO_MEMORY(auth_context->challenge.data);
99
100         auth_context->challenge_set_by = talloc_strdup(auth_context, challenge_set_by);
101         NT_STATUS_HAVE_NO_MEMORY(auth_context->challenge_set_by);
102
103         DEBUG(5, ("auth_context challenge set by %s\n", auth_context->challenge_set_by));
104         DEBUG(5, ("challenge is: \n"));
105         dump_data(5, auth_context->challenge.data, auth_context->challenge.length);
106         return NT_STATUS_OK;
107 }
108
109 /**
110  * Check the password on an NTLMSSP login.  
111  *
112  * Return the session keys used on the connection.
113  */
114
115 NTSTATUS auth3_check_password(struct auth4_context *auth4_context,
116                               TALLOC_CTX *mem_ctx,
117                               const struct auth_usersupplied_info *user_info,
118                               void **server_returned_info,
119                               DATA_BLOB *session_key, DATA_BLOB *lm_session_key)
120 {
121         struct auth_context *auth_context = talloc_get_type_abort(auth4_context->private_data,
122                                                                   struct auth_context);
123         struct auth_usersupplied_info *mapped_user_info = NULL;
124         struct auth_serversupplied_info *server_info;
125         NTSTATUS nt_status;
126         bool username_was_mapped;
127
128         /* The client has given us its machine name (which we only get over NBT transport).
129            We need to possibly reload smb.conf if smb.conf includes depend on the machine name. */
130
131         set_remote_machine_name(user_info->workstation_name, True);
132
133         /* setup the string used by %U */
134         /* sub_set_smb_name checks for weird internally */
135         sub_set_smb_name(user_info->client.account_name);
136
137         lp_load(get_dyn_CONFIGFILE(), false, false, true, true);
138
139         nt_status = make_user_info_map(&mapped_user_info,
140                                        user_info->client.account_name,
141                                        user_info->client.domain_name,
142                                        user_info->workstation_name,
143                                        user_info->remote_host,
144                                        user_info->password.response.lanman.data ? &user_info->password.response.lanman : NULL,
145                                        user_info->password.response.nt.data ? &user_info->password.response.nt : NULL,
146                                        NULL, NULL, NULL,
147                                        AUTH_PASSWORD_RESPONSE);
148
149         if (!NT_STATUS_IS_OK(nt_status)) {
150                 return nt_status;
151         }
152
153         mapped_user_info->logon_parameters = user_info->logon_parameters;
154
155         mapped_user_info->flags = user_info->flags;
156
157         nt_status = auth_context->check_ntlm_password(auth_context,
158                                                       mapped_user_info, &server_info);
159
160         if (!NT_STATUS_IS_OK(nt_status)) {
161                 DEBUG(5,("Checking NTLMSSP password for %s\\%s failed: %s\n",
162                          user_info->client.domain_name,
163                          user_info->client.account_name,
164                          nt_errstr(nt_status)));
165         }
166
167         username_was_mapped = mapped_user_info->was_mapped;
168
169         free_user_info(&mapped_user_info);
170
171         if (!NT_STATUS_IS_OK(nt_status)) {
172                 nt_status = do_map_to_guest_server_info(nt_status,
173                                                         &server_info,
174                                                         user_info->client.account_name,
175                                                         user_info->client.domain_name);
176                 *server_returned_info = talloc_steal(mem_ctx, server_info);
177                 return nt_status;
178         }
179
180         server_info->nss_token |= username_was_mapped;
181
182         /* Clear out the session keys, and pass them to the caller.
183          * They will not be used in this form again - instead the
184          * NTLMSSP code will decide on the final correct session key,
185          * and supply it to create_local_token() */
186         if (session_key) {
187                 DEBUG(10, ("Got NT session key of length %u\n",
188                         (unsigned int)server_info->session_key.length));
189                 *session_key = server_info->session_key;
190                 talloc_steal(mem_ctx, server_info->session_key.data);
191                 server_info->session_key = data_blob_null;
192         }
193         if (lm_session_key) {
194                 DEBUG(10, ("Got LM session key of length %u\n",
195                         (unsigned int)server_info->lm_session_key.length));
196                 *lm_session_key = server_info->lm_session_key;
197                 talloc_steal(mem_ctx, server_info->lm_session_key.data);
198                 server_info->lm_session_key = data_blob_null;
199         }
200
201         *server_returned_info = talloc_steal(mem_ctx, server_info);
202         return nt_status;
203 }
204
205 static NTSTATUS gensec_ntlmssp3_server_start(struct gensec_security *gensec_security)
206 {
207         NTSTATUS nt_status;
208         struct gensec_ntlmssp_context *gensec_ntlmssp;
209         struct ntlmssp_state *ntlmssp_state;
210         const char *netbios_name;
211         const char *netbios_domain;
212         const char *dns_name;
213         const char *dns_domain;
214
215         nt_status = gensec_ntlmssp_start(gensec_security);
216         NT_STATUS_NOT_OK_RETURN(nt_status);
217
218         gensec_ntlmssp =
219                 talloc_get_type_abort(gensec_security->private_data,
220                                       struct gensec_ntlmssp_context);
221
222         ntlmssp_state = talloc_zero(gensec_ntlmssp, struct ntlmssp_state);
223         if (!ntlmssp_state) {
224                 return NT_STATUS_NO_MEMORY;
225         }
226         gensec_ntlmssp->ntlmssp_state = ntlmssp_state;
227
228         ntlmssp_state->callback_private = gensec_ntlmssp;
229
230         ntlmssp_state->role = NTLMSSP_SERVER;
231
232         ntlmssp_state->expected_state = NTLMSSP_NEGOTIATE;
233
234         if (lpcfg_lanman_auth(gensec_security->settings->lp_ctx) &&
235             gensec_setting_bool(gensec_security->settings,
236                                 "ntlmssp_server", "allow_lm_key", false))
237         {
238                 ntlmssp_state->allow_lm_key = true;
239         }
240
241         ntlmssp_state->neg_flags =
242                 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_VERSION;
243
244         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "128bit", true)) {
245                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_128;
246         }
247
248         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "56bit", true)) {
249                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_56;
250         }
251
252         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "keyexchange", true)) {
253                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_KEY_EXCH;
254         }
255
256         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "alwayssign", true)) {
257                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
258         }
259
260         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "ntlm2", true)) {
261                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
262         }
263
264         if (gensec_security->want_features & GENSEC_FEATURE_SESSION_KEY) {
265                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
266         }
267         if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
268                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
269         }
270         if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
271                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
272                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
273         }
274
275         ntlmssp_state->get_challenge = auth_ntlmssp_get_challenge;
276         ntlmssp_state->may_set_challenge = auth_ntlmssp_may_set_challenge;
277         ntlmssp_state->set_challenge = auth_ntlmssp_set_challenge;
278         ntlmssp_state->check_password = auth_ntlmssp_check_password;
279
280         if (lpcfg_server_role(gensec_security->settings->lp_ctx) == ROLE_STANDALONE) {
281                 ntlmssp_state->server.is_standalone = true;
282         } else {
283                 ntlmssp_state->server.is_standalone = false;
284         }
285
286         netbios_name = lpcfg_netbios_name(gensec_security->settings->lp_ctx);
287         netbios_domain = lpcfg_workgroup(gensec_security->settings->lp_ctx);
288
289         if (gensec_security->settings->server_dns_name) {
290                 dns_name = gensec_security->settings->server_dns_name;
291         } else {
292                 const char *dnsdomain = lpcfg_dnsdomain(gensec_security->settings->lp_ctx);
293                 char *lower_netbiosname;
294
295                 lower_netbiosname = strlower_talloc(ntlmssp_state, netbios_name);
296                 NT_STATUS_HAVE_NO_MEMORY(lower_netbiosname);
297
298                 /* Find out the DNS host name */
299                 if (dnsdomain && dnsdomain[0] != '\0') {
300                         dns_name = talloc_asprintf(ntlmssp_state, "%s.%s",
301                                                    lower_netbiosname,
302                                                    dnsdomain);
303                         talloc_free(lower_netbiosname);
304                         NT_STATUS_HAVE_NO_MEMORY(dns_name);
305                 } else {
306                         dns_name = lower_netbiosname;
307                 }
308         }
309
310         if (gensec_security->settings->server_dns_domain) {
311                 dns_domain = gensec_security->settings->server_dns_domain;
312         } else {
313                 dns_domain = lpcfg_dnsdomain(gensec_security->settings->lp_ctx);
314         }
315
316         ntlmssp_state->server.netbios_name = talloc_strdup(ntlmssp_state, netbios_name);
317         NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.netbios_name);
318
319         ntlmssp_state->server.netbios_domain = talloc_strdup(ntlmssp_state, netbios_domain);
320         NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.netbios_domain);
321
322         ntlmssp_state->server.dns_name = talloc_strdup(ntlmssp_state, dns_name);
323         NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.dns_name);
324
325         ntlmssp_state->server.dns_domain = talloc_strdup(ntlmssp_state, dns_domain);
326         NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.dns_domain);
327
328         return NT_STATUS_OK;
329 }
330
331 static const char *gensec_ntlmssp3_server_oids[] = {
332         GENSEC_OID_NTLMSSP,
333         NULL
334 };
335
336 const struct gensec_security_ops gensec_ntlmssp3_server_ops = {
337         .name           = "ntlmssp3_server",
338         .sasl_name      = GENSEC_SASL_NAME_NTLMSSP, /* "NTLM" */
339         .auth_type      = DCERPC_AUTH_TYPE_NTLMSSP,
340         .oid            = gensec_ntlmssp3_server_oids,
341         .server_start   = gensec_ntlmssp3_server_start,
342         .magic          = gensec_ntlmssp_magic,
343         .update         = gensec_ntlmssp_update,
344         .sig_size       = gensec_ntlmssp_sig_size,
345         .sign_packet    = gensec_ntlmssp_sign_packet,
346         .check_packet   = gensec_ntlmssp_check_packet,
347         .seal_packet    = gensec_ntlmssp_seal_packet,
348         .unseal_packet  = gensec_ntlmssp_unseal_packet,
349         .wrap           = gensec_ntlmssp_wrap,
350         .unwrap         = gensec_ntlmssp_unwrap,
351         .session_key    = gensec_ntlmssp_session_key,
352         .session_info   = gensec_ntlmssp_session_info,
353         .have_feature   = gensec_ntlmssp_have_feature,
354         .enabled        = true,
355         .priority       = GENSEC_NTLMSSP
356 };
357