s3-auth: Move auth_ntlmssp wrappers in their own file
[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 "../libcli/auth/ntlmssp.h"
25 #include "ntlmssp_wrap.h"
26
27 void auth_ntlmssp_want_sign(struct auth_ntlmssp_state *auth_ntlmssp_state)
28 {
29
30 }
31
32 void auth_ntlmssp_want_seal(struct auth_ntlmssp_state *auth_ntlmssp_state)
33 {
34
35 }
36
37 NTSTATUS auth_ntlmssp_steal_server_info(TALLOC_CTX *mem_ctx,
38                                 struct auth_ntlmssp_state *auth_ntlmssp_state,
39                                 struct auth_serversupplied_info **server_info)
40 {
41         /* Free the current server_info user_session_key and reset it from the
42          * current ntlmssp_state session_key */
43         data_blob_free(&auth_ntlmssp_state->server_info->user_session_key);
44         auth_ntlmssp_state->server_info->user_session_key =
45                 data_blob_talloc(
46                         auth_ntlmssp_state->server_info,
47                         auth_ntlmssp_state->ntlmssp_state->session_key.data,
48                         auth_ntlmssp_state->ntlmssp_state->session_key.length);
49         if (auth_ntlmssp_state->ntlmssp_state->session_key.length &&
50             !auth_ntlmssp_state->server_info->user_session_key.data) {
51                 *server_info = NULL;
52                 return NT_STATUS_NO_MEMORY;
53         }
54         /* Steal server_info away from auth_ntlmssp_state */
55         *server_info = talloc_move(mem_ctx, &auth_ntlmssp_state->server_info);
56         return NT_STATUS_OK;
57 }
58
59 /**
60  * Return the challenge as determined by the authentication subsystem 
61  * @return an 8 byte random challenge
62  */
63
64 static NTSTATUS auth_ntlmssp_get_challenge(const struct ntlmssp_state *ntlmssp_state,
65                                            uint8_t chal[8])
66 {
67         struct auth_ntlmssp_state *auth_ntlmssp_state =
68                 (struct auth_ntlmssp_state *)ntlmssp_state->callback_private;
69         auth_ntlmssp_state->auth_context->get_ntlm_challenge(
70                 auth_ntlmssp_state->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 static bool auth_ntlmssp_may_set_challenge(const struct ntlmssp_state *ntlmssp_state)
80 {
81         struct auth_ntlmssp_state *auth_ntlmssp_state =
82                 (struct auth_ntlmssp_state *)ntlmssp_state->callback_private;
83         struct auth_context *auth_context = auth_ntlmssp_state->auth_context;
84
85         return auth_context->challenge_may_be_modified;
86 }
87
88 /**
89  * NTLM2 authentication modifies the effective challenge, 
90  * @param challenge The new challenge value
91  */
92 static NTSTATUS auth_ntlmssp_set_challenge(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *challenge)
93 {
94         struct auth_ntlmssp_state *auth_ntlmssp_state =
95                 (struct auth_ntlmssp_state *)ntlmssp_state->callback_private;
96         struct auth_context *auth_context = auth_ntlmssp_state->auth_context;
97
98         SMB_ASSERT(challenge->length == 8);
99
100         auth_context->challenge = data_blob_talloc(auth_context,
101                                                    challenge->data, challenge->length);
102
103         auth_context->challenge_set_by = "NTLMSSP callback (NTLM2)";
104
105         DEBUG(5, ("auth_context challenge set by %s\n", auth_context->challenge_set_by));
106         DEBUG(5, ("challenge is: \n"));
107         dump_data(5, auth_context->challenge.data, auth_context->challenge.length);
108         return NT_STATUS_OK;
109 }
110
111 /**
112  * Check the password on an NTLMSSP login.  
113  *
114  * Return the session keys used on the connection.
115  */
116
117 static NTSTATUS auth_ntlmssp_check_password(struct ntlmssp_state *ntlmssp_state, DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key) 
118 {
119         struct auth_ntlmssp_state *auth_ntlmssp_state =
120                 (struct auth_ntlmssp_state *)ntlmssp_state->callback_private;
121         struct auth_usersupplied_info *user_info = NULL;
122         NTSTATUS nt_status;
123         bool username_was_mapped;
124
125         /* the client has given us its machine name (which we otherwise would not get on port 445).
126            we need to possibly reload smb.conf if smb.conf includes depend on the machine name */
127
128         set_remote_machine_name(auth_ntlmssp_state->ntlmssp_state->client.netbios_name, True);
129
130         /* setup the string used by %U */
131         /* sub_set_smb_name checks for weird internally */
132         sub_set_smb_name(auth_ntlmssp_state->ntlmssp_state->user);
133
134         reload_services(True);
135
136         nt_status = make_user_info_map(&user_info, 
137                                        auth_ntlmssp_state->ntlmssp_state->user, 
138                                        auth_ntlmssp_state->ntlmssp_state->domain, 
139                                        auth_ntlmssp_state->ntlmssp_state->client.netbios_name,
140                                        auth_ntlmssp_state->ntlmssp_state->lm_resp.data ? &auth_ntlmssp_state->ntlmssp_state->lm_resp : NULL, 
141                                        auth_ntlmssp_state->ntlmssp_state->nt_resp.data ? &auth_ntlmssp_state->ntlmssp_state->nt_resp : NULL, 
142                                        NULL, NULL, NULL,
143                                        True);
144
145         user_info->logon_parameters = MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT;
146
147         if (!NT_STATUS_IS_OK(nt_status)) {
148                 return nt_status;
149         }
150
151         nt_status = auth_ntlmssp_state->auth_context->check_ntlm_password(auth_ntlmssp_state->auth_context, 
152                                                                           user_info, &auth_ntlmssp_state->server_info); 
153
154         username_was_mapped = user_info->was_mapped;
155
156         free_user_info(&user_info);
157
158         if (!NT_STATUS_IS_OK(nt_status)) {
159                 return nt_status;
160         }
161
162         auth_ntlmssp_state->server_info->nss_token |= username_was_mapped;
163
164         nt_status = create_local_token(auth_ntlmssp_state->server_info);
165
166         if (!NT_STATUS_IS_OK(nt_status)) {
167                 DEBUG(10, ("create_local_token failed: %s\n",
168                         nt_errstr(nt_status)));
169                 return nt_status;
170         }
171
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 = data_blob_talloc(auth_ntlmssp_state,
176                                                    auth_ntlmssp_state->server_info->user_session_key.data,
177                                                    auth_ntlmssp_state->server_info->user_session_key.length);
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 = data_blob_talloc(auth_ntlmssp_state,
183                                                    auth_ntlmssp_state->server_info->lm_session_key.data,
184                                                    auth_ntlmssp_state->server_info->lm_session_key.length);
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(&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 }