b9d4b72222f38a260482670b1cbefc945062e290
[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 static NTSTATUS gensec_ntlmssp3_server_update(struct gensec_security *gensec_security,
61                                               TALLOC_CTX *out_mem_ctx,
62                                               struct tevent_context *ev,
63                                               const DATA_BLOB request,
64                                               DATA_BLOB *reply)
65 {
66         NTSTATUS status;
67         struct gensec_ntlmssp_context *gensec_ntlmssp =
68                 talloc_get_type_abort(gensec_security->private_data,
69                                       struct gensec_ntlmssp_context);
70
71         status = ntlmssp_update(gensec_ntlmssp->ntlmssp_state, request, reply);
72         if (NT_STATUS_IS_OK(status) ||
73             NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
74                 talloc_steal(out_mem_ctx, reply->data);
75         }
76
77         return status;
78 }
79
80 /**
81  * Return the challenge as determined by the authentication subsystem 
82  * @return an 8 byte random challenge
83  */
84
85 NTSTATUS auth3_get_challenge(struct auth4_context *auth4_context,
86                                            uint8_t chal[8])
87 {
88         struct auth_context *auth_context = talloc_get_type_abort(auth4_context->private_data,
89                                                                   struct auth_context);
90         auth_context->get_ntlm_challenge(auth_context, chal);
91         return NT_STATUS_OK;
92 }
93
94 /**
95  * Some authentication methods 'fix' the challenge, so we may not be able to set it
96  *
97  * @return If the effective challenge used by the auth subsystem may be modified
98  */
99 bool auth3_may_set_challenge(struct auth4_context *auth4_context)
100 {
101         struct auth_context *auth_context = talloc_get_type_abort(auth4_context->private_data,
102                                                                   struct auth_context);
103         return auth_context->challenge_may_be_modified;
104 }
105
106 /**
107  * NTLM2 authentication modifies the effective challenge, 
108  * @param challenge The new challenge value
109  */
110 NTSTATUS auth3_set_challenge(struct auth4_context *auth4_context, const uint8_t *chal,
111                              const char *challenge_set_by)
112 {
113         struct auth_context *auth_context = talloc_get_type_abort(auth4_context->private_data,
114                                                                   struct auth_context);
115
116         auth_context->challenge = data_blob_talloc(auth_context,
117                                                    chal, 8);
118         NT_STATUS_HAVE_NO_MEMORY(auth_context->challenge.data);
119
120         auth_context->challenge_set_by = talloc_strdup(auth_context, challenge_set_by);
121         NT_STATUS_HAVE_NO_MEMORY(auth_context->challenge_set_by);
122
123         DEBUG(5, ("auth_context challenge set by %s\n", auth_context->challenge_set_by));
124         DEBUG(5, ("challenge is: \n"));
125         dump_data(5, auth_context->challenge.data, auth_context->challenge.length);
126         return NT_STATUS_OK;
127 }
128
129 /**
130  * Check the password on an NTLMSSP login.  
131  *
132  * Return the session keys used on the connection.
133  */
134
135 NTSTATUS auth3_check_password(struct auth4_context *auth4_context,
136                               TALLOC_CTX *mem_ctx,
137                               const struct auth_usersupplied_info *user_info,
138                               void **server_returned_info,
139                               DATA_BLOB *session_key, DATA_BLOB *lm_session_key)
140 {
141         struct auth_context *auth_context = talloc_get_type_abort(auth4_context->private_data,
142                                                                   struct auth_context);
143         struct auth_usersupplied_info *mapped_user_info = NULL;
144         struct auth_serversupplied_info *server_info;
145         NTSTATUS nt_status;
146         bool username_was_mapped;
147
148         /* The client has given us its machine name (which we only get over NBT transport).
149            We need to possibly reload smb.conf if smb.conf includes depend on the machine name. */
150
151         set_remote_machine_name(user_info->workstation_name, True);
152
153         /* setup the string used by %U */
154         /* sub_set_smb_name checks for weird internally */
155         sub_set_smb_name(user_info->client.account_name);
156
157         lp_load(get_dyn_CONFIGFILE(), false, false, true, true);
158
159         nt_status = make_user_info_map(&mapped_user_info,
160                                        user_info->client.account_name,
161                                        user_info->client.domain_name,
162                                        user_info->workstation_name,
163                                        user_info->remote_host,
164                                        user_info->password.response.lanman.data ? &user_info->password.response.lanman : NULL,
165                                        user_info->password.response.nt.data ? &user_info->password.response.nt : NULL,
166                                        NULL, NULL, NULL,
167                                        AUTH_PASSWORD_RESPONSE);
168
169         if (!NT_STATUS_IS_OK(nt_status)) {
170                 return nt_status;
171         }
172
173         mapped_user_info->logon_parameters = user_info->logon_parameters;
174
175         mapped_user_info->flags = user_info->flags;
176
177         nt_status = auth_context->check_ntlm_password(auth_context,
178                                                       mapped_user_info, &server_info);
179
180         if (!NT_STATUS_IS_OK(nt_status)) {
181                 DEBUG(5,("Checking NTLMSSP password for %s\\%s failed: %s\n",
182                          user_info->client.domain_name,
183                          user_info->client.account_name,
184                          nt_errstr(nt_status)));
185         }
186
187         username_was_mapped = mapped_user_info->was_mapped;
188
189         free_user_info(&mapped_user_info);
190
191         if (!NT_STATUS_IS_OK(nt_status)) {
192                 nt_status = do_map_to_guest_server_info(nt_status,
193                                                         &server_info,
194                                                         user_info->client.account_name,
195                                                         user_info->client.domain_name);
196                 *server_returned_info = talloc_steal(mem_ctx, server_info);
197                 return nt_status;
198         }
199
200         server_info->nss_token |= username_was_mapped;
201
202         /* Clear out the session keys, and pass them to the caller.
203          * They will not be used in this form again - instead the
204          * NTLMSSP code will decide on the final correct session key,
205          * and supply it to create_local_token() */
206         if (session_key) {
207                 DEBUG(10, ("Got NT session key of length %u\n",
208                         (unsigned int)server_info->session_key.length));
209                 *session_key = server_info->session_key;
210                 talloc_steal(mem_ctx, server_info->session_key.data);
211                 server_info->session_key = data_blob_null;
212         }
213         if (lm_session_key) {
214                 DEBUG(10, ("Got LM session key of length %u\n",
215                         (unsigned int)server_info->lm_session_key.length));
216                 *lm_session_key = server_info->lm_session_key;
217                 talloc_steal(mem_ctx, server_info->lm_session_key.data);
218                 server_info->lm_session_key = data_blob_null;
219         }
220
221         *server_returned_info = talloc_steal(mem_ctx, server_info);
222         return nt_status;
223 }
224
225 static NTSTATUS gensec_ntlmssp3_server_start(struct gensec_security *gensec_security)
226 {
227         NTSTATUS nt_status;
228         bool is_standalone;
229         const char *netbios_name;
230         const char *netbios_domain;
231         const char *dns_name;
232         char *dns_domain;
233         struct gensec_ntlmssp_context *gensec_ntlmssp;
234
235         if ((enum server_role)lp_server_role() == ROLE_STANDALONE) {
236                 is_standalone = true;
237         } else {
238                 is_standalone = false;
239         }
240
241         netbios_name = lp_netbios_name();
242         netbios_domain = lp_workgroup();
243         /* This should be a 'netbios domain -> DNS domain' mapping */
244         dns_domain = get_mydnsdomname(talloc_tos());
245         if (dns_domain) {
246                 strlower_m(dns_domain);
247         }
248         dns_name = get_mydnsfullname();
249
250         nt_status = gensec_ntlmssp_start(gensec_security);
251         NT_STATUS_NOT_OK_RETURN(nt_status);
252
253         gensec_ntlmssp =
254                 talloc_get_type_abort(gensec_security->private_data,
255                                       struct gensec_ntlmssp_context);
256
257         nt_status = ntlmssp_server_start(gensec_ntlmssp,
258                                          is_standalone,
259                                          netbios_name,
260                                          netbios_domain,
261                                          dns_name,
262                                          dns_domain,
263                                          &gensec_ntlmssp->ntlmssp_state);
264         if (!NT_STATUS_IS_OK(nt_status)) {
265                 return nt_status;
266         }
267
268         gensec_ntlmssp->ntlmssp_state->callback_private = gensec_ntlmssp;
269
270         gensec_ntlmssp->ntlmssp_state->get_challenge = auth_ntlmssp_get_challenge;
271         gensec_ntlmssp->ntlmssp_state->may_set_challenge = auth_ntlmssp_may_set_challenge;
272         gensec_ntlmssp->ntlmssp_state->set_challenge = auth_ntlmssp_set_challenge;
273         gensec_ntlmssp->ntlmssp_state->check_password = auth_ntlmssp_check_password;
274
275         if (gensec_ntlmssp->gensec_security->want_features & GENSEC_FEATURE_SESSION_KEY) {
276                 gensec_ntlmssp->ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
277         }
278         if (gensec_ntlmssp->gensec_security->want_features & GENSEC_FEATURE_SIGN) {
279                 gensec_ntlmssp->ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
280         }
281         if (gensec_ntlmssp->gensec_security->want_features & GENSEC_FEATURE_SEAL) {
282                 gensec_ntlmssp->ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
283                 gensec_ntlmssp->ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
284         }
285
286         return NT_STATUS_OK;
287 }
288
289 static const char *gensec_ntlmssp3_server_oids[] = {
290         GENSEC_OID_NTLMSSP,
291         NULL
292 };
293
294 const struct gensec_security_ops gensec_ntlmssp3_server_ops = {
295         .name           = "ntlmssp3_server",
296         .sasl_name      = GENSEC_SASL_NAME_NTLMSSP, /* "NTLM" */
297         .auth_type      = DCERPC_AUTH_TYPE_NTLMSSP,
298         .oid            = gensec_ntlmssp3_server_oids,
299         .server_start   = gensec_ntlmssp3_server_start,
300         .magic          = gensec_ntlmssp_magic,
301         .update         = gensec_ntlmssp3_server_update,
302         .sig_size       = gensec_ntlmssp_sig_size,
303         .sign_packet    = gensec_ntlmssp_sign_packet,
304         .check_packet   = gensec_ntlmssp_check_packet,
305         .seal_packet    = gensec_ntlmssp_seal_packet,
306         .unseal_packet  = gensec_ntlmssp_unseal_packet,
307         .wrap           = gensec_ntlmssp_wrap,
308         .unwrap         = gensec_ntlmssp_unwrap,
309         .session_key    = gensec_ntlmssp_session_key,
310         .session_info   = gensec_ntlmssp_session_info,
311         .have_feature   = gensec_ntlmssp_have_feature,
312         .enabled        = true,
313         .priority       = GENSEC_NTLMSSP
314 };
315