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