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