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