s3-auth: use auth.h where needed.
[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-2003
8
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.
13    
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.
18    
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/>.
21 */
22
23 #include "includes.h"
24 #include "auth.h"
25 #include "../libcli/auth/ntlmssp.h"
26 #include "ntlmssp_wrap.h"
27 #include "../librpc/gen_ndr/netlogon.h"
28 #include "smbd/smbd.h"
29
30 NTSTATUS auth_ntlmssp_steal_session_info(TALLOC_CTX *mem_ctx,
31                                         struct auth_ntlmssp_state *auth_ntlmssp_state,
32                                         struct auth_serversupplied_info **session_info)
33 {
34         /* Free the current server_info user_session_key and reset it from the
35          * current ntlmssp_state session_key */
36         data_blob_free(&auth_ntlmssp_state->server_info->user_session_key);
37         /* Set up the final session key for the connection */
38         auth_ntlmssp_state->server_info->user_session_key =
39                 data_blob_talloc(
40                         auth_ntlmssp_state->server_info,
41                         auth_ntlmssp_state->ntlmssp_state->session_key.data,
42                         auth_ntlmssp_state->ntlmssp_state->session_key.length);
43         if (auth_ntlmssp_state->ntlmssp_state->session_key.length &&
44             !auth_ntlmssp_state->server_info->user_session_key.data) {
45                 *session_info = NULL;
46                 return NT_STATUS_NO_MEMORY;
47         }
48         /* Steal session_info away from auth_ntlmssp_state */
49         *session_info = talloc_move(mem_ctx, &auth_ntlmssp_state->server_info);
50         return NT_STATUS_OK;
51 }
52
53 /**
54  * Return the challenge as determined by the authentication subsystem 
55  * @return an 8 byte random challenge
56  */
57
58 static NTSTATUS auth_ntlmssp_get_challenge(const struct ntlmssp_state *ntlmssp_state,
59                                            uint8_t chal[8])
60 {
61         struct auth_ntlmssp_state *auth_ntlmssp_state =
62                 (struct auth_ntlmssp_state *)ntlmssp_state->callback_private;
63         auth_ntlmssp_state->auth_context->get_ntlm_challenge(
64                 auth_ntlmssp_state->auth_context, chal);
65         return NT_STATUS_OK;
66 }
67
68 /**
69  * Some authentication methods 'fix' the challenge, so we may not be able to set it
70  *
71  * @return If the effective challenge used by the auth subsystem may be modified
72  */
73 static bool auth_ntlmssp_may_set_challenge(const struct ntlmssp_state *ntlmssp_state)
74 {
75         struct auth_ntlmssp_state *auth_ntlmssp_state =
76                 (struct auth_ntlmssp_state *)ntlmssp_state->callback_private;
77         struct auth_context *auth_context = auth_ntlmssp_state->auth_context;
78
79         return auth_context->challenge_may_be_modified;
80 }
81
82 /**
83  * NTLM2 authentication modifies the effective challenge, 
84  * @param challenge The new challenge value
85  */
86 static NTSTATUS auth_ntlmssp_set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge)
87 {
88         struct auth_ntlmssp_state *auth_ntlmssp_state =
89                 (struct auth_ntlmssp_state *)ntlmssp_state->callback_private;
90         struct auth_context *auth_context = auth_ntlmssp_state->auth_context;
91
92         SMB_ASSERT(challenge->length == 8);
93
94         auth_context->challenge = data_blob_talloc(auth_context,
95                                                    challenge->data, challenge->length);
96
97         auth_context->challenge_set_by = "NTLMSSP callback (NTLM2)";
98
99         DEBUG(5, ("auth_context challenge set by %s\n", auth_context->challenge_set_by));
100         DEBUG(5, ("challenge is: \n"));
101         dump_data(5, auth_context->challenge.data, auth_context->challenge.length);
102         return NT_STATUS_OK;
103 }
104
105 /**
106  * Check the password on an NTLMSSP login.  
107  *
108  * Return the session keys used on the connection.
109  */
110
111 static NTSTATUS auth_ntlmssp_check_password(struct ntlmssp_state *ntlmssp_state, TALLOC_CTX *mem_ctx,
112                                             DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key)
113 {
114         struct auth_ntlmssp_state *auth_ntlmssp_state =
115                 (struct auth_ntlmssp_state *)ntlmssp_state->callback_private;
116         struct auth_usersupplied_info *user_info = NULL;
117         NTSTATUS nt_status;
118         bool username_was_mapped;
119
120         /* the client has given us its machine name (which we otherwise would not get on port 445).
121            we need to possibly reload smb.conf if smb.conf includes depend on the machine name */
122
123         set_remote_machine_name(auth_ntlmssp_state->ntlmssp_state->client.netbios_name, True);
124
125         /* setup the string used by %U */
126         /* sub_set_smb_name checks for weird internally */
127         sub_set_smb_name(auth_ntlmssp_state->ntlmssp_state->user);
128
129         reload_services(smbd_messaging_context(), -1, True);
130
131         nt_status = make_user_info_map(&user_info, 
132                                        auth_ntlmssp_state->ntlmssp_state->user, 
133                                        auth_ntlmssp_state->ntlmssp_state->domain, 
134                                        auth_ntlmssp_state->ntlmssp_state->client.netbios_name,
135                                        auth_ntlmssp_state->ntlmssp_state->lm_resp.data ? &auth_ntlmssp_state->ntlmssp_state->lm_resp : NULL, 
136                                        auth_ntlmssp_state->ntlmssp_state->nt_resp.data ? &auth_ntlmssp_state->ntlmssp_state->nt_resp : NULL, 
137                                        NULL, NULL, NULL,
138                                        AUTH_PASSWORD_RESPONSE);
139
140         if (!NT_STATUS_IS_OK(nt_status)) {
141                 return nt_status;
142         }
143
144         user_info->logon_parameters = MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT;
145
146         nt_status = auth_ntlmssp_state->auth_context->check_ntlm_password(auth_ntlmssp_state->auth_context, 
147                                                                           user_info, &auth_ntlmssp_state->server_info); 
148
149         username_was_mapped = user_info->was_mapped;
150
151         free_user_info(&user_info);
152
153         if (!NT_STATUS_IS_OK(nt_status)) {
154                 return nt_status;
155         }
156
157         auth_ntlmssp_state->server_info->nss_token |= username_was_mapped;
158
159         nt_status = create_local_token(auth_ntlmssp_state->server_info);
160
161         if (!NT_STATUS_IS_OK(nt_status)) {
162                 DEBUG(10, ("create_local_token failed: %s\n",
163                         nt_errstr(nt_status)));
164                 return nt_status;
165         }
166
167         /* Clear out the session keys, and pass them to the caller.
168          * They will not be used in this form again - instead the
169          * NTLMSSP code will decide on the final correct session key,
170          * and put it back here at the end of
171          * auth_ntlmssp_steal_server_info */
172         if (auth_ntlmssp_state->server_info->user_session_key.length) {
173                 DEBUG(10, ("Got NT session key of length %u\n",
174                         (unsigned int)auth_ntlmssp_state->server_info->user_session_key.length));
175                 *user_session_key = auth_ntlmssp_state->server_info->user_session_key;
176                 talloc_steal(mem_ctx, auth_ntlmssp_state->server_info->user_session_key.data);
177                 auth_ntlmssp_state->server_info->user_session_key = data_blob_null;
178         }
179         if (auth_ntlmssp_state->server_info->lm_session_key.length) {
180                 DEBUG(10, ("Got LM session key of length %u\n",
181                         (unsigned int)auth_ntlmssp_state->server_info->lm_session_key.length));
182                 *lm_session_key = auth_ntlmssp_state->server_info->lm_session_key;
183                 talloc_steal(mem_ctx, auth_ntlmssp_state->server_info->lm_session_key.data);
184                 auth_ntlmssp_state->server_info->lm_session_key = data_blob_null;
185         }
186         return nt_status;
187 }
188
189 static int auth_ntlmssp_state_destructor(void *ptr);
190
191 NTSTATUS auth_ntlmssp_start(struct auth_ntlmssp_state **auth_ntlmssp_state)
192 {
193         NTSTATUS nt_status;
194         bool is_standalone;
195         const char *netbios_name;
196         const char *netbios_domain;
197         const char *dns_name;
198         char *dns_domain;
199         struct auth_ntlmssp_state *ans;
200         struct auth_context *auth_context;
201
202         if ((enum server_types)lp_server_role() == ROLE_STANDALONE) {
203                 is_standalone = true;
204         } else {
205                 is_standalone = false;
206         }
207
208         netbios_name = global_myname();
209         netbios_domain = lp_workgroup();
210         /* This should be a 'netbios domain -> DNS domain' mapping */
211         dns_domain = get_mydnsdomname(talloc_tos());
212         if (dns_domain) {
213                 strlower_m(dns_domain);
214         }
215         dns_name = get_mydnsfullname();
216
217         ans = talloc_zero(NULL, struct auth_ntlmssp_state);
218         if (!ans) {
219                 DEBUG(0,("auth_ntlmssp_start: talloc failed!\n"));
220                 return NT_STATUS_NO_MEMORY;
221         }
222
223         nt_status = ntlmssp_server_start(ans,
224                                          is_standalone,
225                                          netbios_name,
226                                          netbios_domain,
227                                          dns_name,
228                                          dns_domain,
229                                          &ans->ntlmssp_state);
230         if (!NT_STATUS_IS_OK(nt_status)) {
231                 return nt_status;
232         }
233
234         nt_status = make_auth_context_subsystem(talloc_tos(), &auth_context);
235         if (!NT_STATUS_IS_OK(nt_status)) {
236                 return nt_status;
237         }
238         ans->auth_context = talloc_steal(ans, auth_context);
239
240         ans->ntlmssp_state->callback_private = ans;
241         ans->ntlmssp_state->get_challenge = auth_ntlmssp_get_challenge;
242         ans->ntlmssp_state->may_set_challenge = auth_ntlmssp_may_set_challenge;
243         ans->ntlmssp_state->set_challenge = auth_ntlmssp_set_challenge;
244         ans->ntlmssp_state->check_password = auth_ntlmssp_check_password;
245
246         talloc_set_destructor((TALLOC_CTX *)ans, auth_ntlmssp_state_destructor);
247
248         *auth_ntlmssp_state = ans;
249         return NT_STATUS_OK;
250 }
251
252 static int auth_ntlmssp_state_destructor(void *ptr)
253 {
254         struct auth_ntlmssp_state *ans;
255
256         ans = talloc_get_type(ptr, struct auth_ntlmssp_state);
257
258         TALLOC_FREE(ans->server_info);
259         TALLOC_FREE(ans->ntlmssp_state);
260         return 0;
261 }