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