s3-auth: Remember to always free the talloc_stackframe() in auth_samba4
[samba.git] / source3 / auth / auth_samba4.c
1 /*
2    Unix SMB/CIFS implementation.
3    Authenticate against Samba4's auth subsystem
4    Copyright (C) Volker Lendecke 2008
5    Copyright (C) Andrew Bartlett 2010
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "source3/include/auth.h"
23 #include "source4/auth/auth.h"
24 #include "auth/auth_sam_reply.h"
25 #include "param/param.h"
26 #include "source4/lib/events/events.h"
27 #include "source4/lib/messaging/messaging.h"
28 #include "auth/gensec/gensec.h"
29 #include "auth/credentials/credentials.h"
30
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_AUTH
33
34 static NTSTATUS make_auth4_context_s4(TALLOC_CTX *mem_ctx,
35                                       struct auth4_context **auth4_context);
36
37 static struct idr_context *task_id_tree;
38
39 static int free_task_id(struct server_id *server_id)
40 {
41         idr_remove(task_id_tree, server_id->task_id);
42         return 0;
43 }
44
45 /* Return a server_id with a unique task_id element.  Free the
46  * returned pointer to de-allocate the task_id via a talloc destructor
47  * (ie, use talloc_free()) */
48 static struct server_id *new_server_id_task(TALLOC_CTX *mem_ctx)
49 {
50         struct server_id *server_id;
51         int task_id;
52         if (!task_id_tree) {
53                 task_id_tree = idr_init(NULL);
54                 if (!task_id_tree) {
55                         return NULL;
56                 }
57         }
58
59         server_id = talloc(mem_ctx, struct server_id);
60
61         if (!server_id) {
62                 return NULL;
63         }
64         *server_id = procid_self();
65
66         /* 0 is the default server_id, so we need to start with 1 */
67         task_id = idr_get_new_above(task_id_tree, server_id, 1, INT32_MAX);
68
69         if (task_id == -1) {
70                 talloc_free(server_id);
71                 return NULL;
72         }
73
74         talloc_set_destructor(server_id, free_task_id);
75         server_id->task_id = task_id;
76         return server_id;
77 }
78
79 /*
80  * This module is not an ordinary authentication module.  It is really
81  * a way to redirect the whole authentication and authorization stack
82  * to use the source4 auth code, not a way to just handle NTLM
83  * authentication.
84  *
85  * See the comments above each function for how that hook changes the
86  * behaviour.
87  */
88
89 /* 
90  * This hook is currently used by winbindd only, as all other NTLM
91  * logins go via the hooks provided by make_auth4_context_s4() below.
92  *
93  * This is only left in case we find a way that it might become useful
94  * in future.  Importantly, this routine returns the information
95  * needed for a NETLOGON SamLogon, not what is needed to establish a
96  * session.
97  *
98  * We expect we may use this hook in the source3/ winbind when this
99  * services the AD DC.  It is tested via pdbtest.
100  */
101
102 static NTSTATUS check_samba4_security(const struct auth_context *auth_context,
103                                       void *my_private_data,
104                                       TALLOC_CTX *mem_ctx,
105                                       const struct auth_usersupplied_info *user_info,
106                                       struct auth_serversupplied_info **server_info)
107 {
108         TALLOC_CTX *frame = talloc_stackframe();
109         struct netr_SamInfo3 *info3 = NULL;
110         NTSTATUS nt_status;
111         struct auth_user_info_dc *user_info_dc;
112         struct auth4_context *auth4_context;
113
114         nt_status = make_auth4_context_s4(mem_ctx, &auth4_context);
115         if (!NT_STATUS_IS_OK(nt_status)) {
116                 TALLOC_FREE(frame);
117                 goto done;
118         }
119                 
120         nt_status = auth_context_set_challenge(auth4_context, auth_context->challenge.data, "auth_samba4");
121         if (!NT_STATUS_IS_OK(nt_status)) {
122                 TALLOC_FREE(auth4_context);
123                 TALLOC_FREE(frame);
124                 return nt_status;
125         }
126
127         nt_status = auth_check_password(auth4_context, auth4_context, user_info, &user_info_dc);
128         if (!NT_STATUS_IS_OK(nt_status)) {
129                 TALLOC_FREE(auth4_context);
130                 TALLOC_FREE(frame);
131                 return nt_status;
132         }
133         
134         nt_status = auth_convert_user_info_dc_saminfo3(mem_ctx,
135                                                        user_info_dc,
136                                                        &info3);
137         if (NT_STATUS_IS_OK(nt_status)) {
138                 /* We need the strings from the server_info to be valid as long as the info3 is around */
139                 talloc_steal(info3, user_info_dc);
140         }
141         talloc_free(auth4_context);
142
143         if (!NT_STATUS_IS_OK(nt_status)) {
144                 goto done;
145         }
146
147         nt_status = make_server_info_info3(mem_ctx, user_info->client.account_name,
148                                            user_info->mapped.domain_name, server_info,
149                                         info3);
150         if (!NT_STATUS_IS_OK(nt_status)) {
151                 DEBUG(10, ("make_server_info_info3 failed: %s\n",
152                            nt_errstr(nt_status)));
153                 TALLOC_FREE(frame);
154                 return nt_status;
155         }
156
157         nt_status = NT_STATUS_OK;
158
159  done:
160         TALLOC_FREE(frame);
161         return nt_status;
162 }
163
164 /*
165  * Hook to allow the source4 set of GENSEC modules to handle
166  * blob-based authentication mechanisms, without directly linking the
167  * mechanism code.
168  *
169  * This may eventually go away, when the GSSAPI acceptors are merged,
170  * when we will just rely on the make_auth4_context_s4 hook instead.
171  *
172  * Even for NTLMSSP, which has a common module, significant parts of
173  * the behaviour are overridden here, because it uses the source4 NTLM
174  * stack and the source4 mapping between the PAC/SamLogon response and
175  * the local token.
176  *
177  * It is important to override all this to ensure that the exact same
178  * token is generated and used in the SMB and LDAP servers, for NTLM
179  * and for Kerberos.
180  */
181 static NTSTATUS prepare_gensec(TALLOC_CTX *mem_ctx,
182                                struct gensec_security **gensec_context)
183 {
184         NTSTATUS status;
185         struct loadparm_context *lp_ctx;
186         struct tevent_context *event_ctx;
187         TALLOC_CTX *frame = talloc_stackframe();
188         struct gensec_security *gensec_ctx;
189         struct imessaging_context *msg_ctx;
190         struct cli_credentials *server_credentials;
191         struct server_id *server_id;
192
193         lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
194         if (lp_ctx == NULL) {
195                 DEBUG(1, ("loadparm_init_s3 failed\n"));
196                 TALLOC_FREE(frame);
197                 return NT_STATUS_INVALID_SERVER_STATE;
198         }
199         event_ctx = s4_event_context_init(frame);
200         if (event_ctx == NULL) {
201                 DEBUG(1, ("s4_event_context_init failed\n"));
202                 TALLOC_FREE(frame);
203                 return NT_STATUS_INVALID_SERVER_STATE;
204         }
205
206         server_id = new_server_id_task(frame);
207         if (server_id == NULL) {
208                 DEBUG(1, ("new_server_id_task failed\n"));
209                 TALLOC_FREE(frame);
210                 return NT_STATUS_INVALID_SERVER_STATE;
211         }
212
213         msg_ctx = imessaging_init(frame,
214                                   lp_ctx,
215                                   *server_id,
216                                   event_ctx, true);
217         if (msg_ctx == NULL) {
218                 DEBUG(1, ("imessaging_init failed\n"));
219                 TALLOC_FREE(frame);
220                 return NT_STATUS_INVALID_SERVER_STATE;
221         }
222
223         talloc_reparent(frame, msg_ctx, server_id);
224
225         server_credentials
226                 = cli_credentials_init(frame);
227         if (!server_credentials) {
228                 DEBUG(1, ("Failed to init server credentials"));
229                 TALLOC_FREE(frame);
230                 return NT_STATUS_INVALID_SERVER_STATE;
231         }
232
233         cli_credentials_set_conf(server_credentials, lp_ctx);
234         status = cli_credentials_set_machine_account(server_credentials, lp_ctx);
235         if (!NT_STATUS_IS_OK(status)) {
236                 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
237                 talloc_free(server_credentials);
238                 server_credentials = NULL;
239         }
240
241         status = samba_server_gensec_start(mem_ctx,
242                                            event_ctx, msg_ctx,
243                                            lp_ctx, server_credentials, "cifs",
244                                            &gensec_ctx);
245         if (!NT_STATUS_IS_OK(status)) {
246                 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
247                 TALLOC_FREE(frame);
248                 return status;
249         }
250
251         talloc_reparent(frame, gensec_ctx, msg_ctx);
252         talloc_reparent(frame, gensec_ctx, event_ctx);
253         talloc_reparent(frame, gensec_ctx, lp_ctx);
254         talloc_reparent(frame, gensec_ctx, server_credentials);
255
256         gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
257         gensec_want_feature(gensec_ctx, GENSEC_FEATURE_UNIX_TOKEN);
258
259         *gensec_context = gensec_ctx;
260         TALLOC_FREE(frame);
261         return status;
262 }
263
264 /*
265  * Hook to allow handling of NTLM authentication for AD operation
266  * without directly linking the s4 auth stack
267  *
268  * This ensures we use the source4 authentication stack, as well as
269  * the authorization stack to create the user's token.  This ensures
270  * consistency between NTLM logins and NTLMSSP logins, as NTLMSSP is
271  * handled by the hook above.
272  */
273 static NTSTATUS make_auth4_context_s4(TALLOC_CTX *mem_ctx,
274                                       struct auth4_context **auth4_context)
275 {
276         NTSTATUS status;
277         struct loadparm_context *lp_ctx;
278         struct tevent_context *event_ctx;
279         TALLOC_CTX *frame = talloc_stackframe();
280         struct imessaging_context *msg_ctx;
281         struct server_id *server_id;
282
283         lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
284         if (lp_ctx == NULL) {
285                 DEBUG(1, ("loadparm_init_s3 failed\n"));
286                 TALLOC_FREE(frame);
287                 return NT_STATUS_INVALID_SERVER_STATE;
288         }
289         event_ctx = s4_event_context_init(frame);
290         if (event_ctx == NULL) {
291                 DEBUG(1, ("s4_event_context_init failed\n"));
292                 TALLOC_FREE(frame);
293                 return NT_STATUS_INVALID_SERVER_STATE;
294         }
295
296         server_id = new_server_id_task(frame);
297         if (server_id == NULL) {
298                 DEBUG(1, ("new_server_id_task failed\n"));
299                 TALLOC_FREE(frame);
300                 return NT_STATUS_INVALID_SERVER_STATE;
301         }
302
303         msg_ctx = imessaging_init(frame,
304                                   lp_ctx,
305                                   *server_id,
306                                   event_ctx, true);
307         if (msg_ctx == NULL) {
308                 DEBUG(1, ("imessaging_init failed\n"));
309                 TALLOC_FREE(frame);
310                 return NT_STATUS_INVALID_SERVER_STATE;
311         }
312         talloc_reparent(frame, msg_ctx, server_id);
313
314         status = auth_context_create(mem_ctx,
315                                         event_ctx,
316                                         msg_ctx,
317                                         lp_ctx,
318                                         auth4_context);
319
320         if (!NT_STATUS_IS_OK(status)) {
321                 DEBUG(1, ("Failed to start auth server code: %s\n", nt_errstr(status)));
322                 TALLOC_FREE(frame);
323                 return status;
324         }
325
326         talloc_reparent(frame, *auth4_context, msg_ctx);
327         talloc_reparent(frame, *auth4_context, event_ctx);
328         talloc_reparent(frame, *auth4_context, lp_ctx);
329
330         TALLOC_FREE(frame);
331         return status;
332 }
333
334 /* module initialisation */
335 static NTSTATUS auth_init_samba4(struct auth_context *auth_context,
336                                     const char *param,
337                                     auth_methods **auth_method)
338 {
339         struct auth_methods *result;
340
341         gensec_init();
342
343         result = talloc_zero(auth_context, struct auth_methods);
344         if (result == NULL) {
345                 return NT_STATUS_NO_MEMORY;
346         }
347         result->name = "samba4";
348         result->auth = check_samba4_security;
349         result->prepare_gensec = prepare_gensec;
350         result->make_auth4_context = make_auth4_context_s4;
351
352         *auth_method = result;
353         return NT_STATUS_OK;
354 }
355
356 NTSTATUS auth_samba4_init(void)
357 {
358         smb_register_auth(AUTH_INTERFACE_VERSION, "samba4",
359                           auth_init_samba4);
360         return NT_STATUS_OK;
361 }