2 Unix SMB/CIFS implementation.
3 Authenticate against Samba4's auth subsystem
4 Copyright (C) Volker Lendecke 2008
5 Copyright (C) Andrew Bartlett 2010
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.
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.
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/>.
22 #include "source3/include/auth.h"
23 #include "source3/include/messages.h"
24 #include "source4/auth/auth.h"
25 #include "auth/auth_sam_reply.h"
26 #include "param/param.h"
27 #include "source4/lib/events/events.h"
28 #include "source4/lib/messaging/messaging.h"
29 #include "auth/gensec/gensec.h"
30 #include "auth/credentials/credentials.h"
31 #include "lib/global_contexts.h"
32 #include "lib/util/idtree.h"
35 #define DBGC_CLASS DBGC_AUTH
37 static NTSTATUS make_auth4_context_s4(const struct auth_context *auth_context,
39 struct auth4_context **auth4_context);
41 static struct idr_context *task_id_tree;
43 static int free_task_id(struct server_id *server_id)
45 idr_remove(task_id_tree, server_id->task_id);
49 /* Return a server_id with a unique task_id element. Free the
50 * returned pointer to de-allocate the task_id via a talloc destructor
51 * (ie, use talloc_free()) */
52 static struct server_id *new_server_id_task(TALLOC_CTX *mem_ctx)
54 struct messaging_context *msg_ctx;
55 struct server_id *server_id;
58 task_id_tree = idr_init(NULL);
64 msg_ctx = global_messaging_context();
65 if (msg_ctx == NULL) {
69 server_id = talloc(mem_ctx, struct server_id);
74 *server_id = messaging_server_id(msg_ctx);
76 /* 0 is the default server_id, so we need to start with 1 */
77 task_id = idr_get_new_above(task_id_tree, server_id, 1, INT32_MAX);
80 talloc_free(server_id);
84 talloc_set_destructor(server_id, free_task_id);
85 server_id->task_id = task_id;
90 * This module is not an ordinary authentication module. It is really
91 * a way to redirect the whole authentication and authorization stack
92 * to use the source4 auth code, not a way to just handle NTLM
95 * See the comments above each function for how that hook changes the
100 * This hook is currently used by winbindd only, as all other NTLM
101 * logins go via the hooks provided by make_auth4_context_s4() below.
103 * This is only left in case we find a way that it might become useful
104 * in future. Importantly, this routine returns the information
105 * needed for a NETLOGON SamLogon, not what is needed to establish a
108 * We expect we may use this hook in the source3/ winbind when this
109 * services the AD DC. It is tested via pdbtest.
112 static NTSTATUS check_samba4_security(
113 const struct auth_context *auth_context,
114 void *my_private_data,
116 const struct auth_usersupplied_info *user_info,
117 struct auth_serversupplied_info **pserver_info)
119 TALLOC_CTX *frame = talloc_stackframe();
120 struct netr_SamInfo3 *info3 = NULL;
122 struct auth_user_info_dc *user_info_dc;
123 struct auth4_context *auth4_context;
124 uint8_t authoritative = 1;
125 struct auth_serversupplied_info *server_info = NULL;
127 nt_status = make_auth4_context_s4(auth_context, mem_ctx, &auth4_context);
128 if (!NT_STATUS_IS_OK(nt_status)) {
133 nt_status = auth_context_set_challenge(auth4_context, auth_context->challenge.data, "auth_samba4");
134 if (!NT_STATUS_IS_OK(nt_status)) {
135 TALLOC_FREE(auth4_context);
140 nt_status = auth_check_password(auth4_context, auth4_context, user_info,
141 &user_info_dc, &authoritative);
142 if (!NT_STATUS_IS_OK(nt_status)) {
143 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER) &&
146 nt_status = NT_STATUS_NOT_IMPLEMENTED;
148 TALLOC_FREE(auth4_context);
153 nt_status = auth_convert_user_info_dc_saminfo3(mem_ctx,
156 if (NT_STATUS_IS_OK(nt_status)) {
157 /* We need the strings from the server_info to be valid as long as the info3 is around */
158 talloc_steal(info3, user_info_dc);
160 talloc_free(auth4_context);
162 if (!NT_STATUS_IS_OK(nt_status)) {
166 if (user_info->flags & USER_INFO_INFO3_AND_NO_AUTHZ) {
167 server_info = make_server_info(mem_ctx);
168 if (server_info == NULL) {
169 nt_status = NT_STATUS_NO_MEMORY;
172 server_info->info3 = talloc_move(server_info, &info3);
174 nt_status = make_server_info_info3(
176 user_info->client.account_name,
177 user_info->mapped.domain_name,
180 if (!NT_STATUS_IS_OK(nt_status)) {
181 DEBUG(10, ("make_server_info_info3 failed: %s\n",
182 nt_errstr(nt_status)));
187 *pserver_info = server_info;
188 nt_status = NT_STATUS_OK;
196 * Hook to allow the source4 set of GENSEC modules to handle
197 * blob-based authentication mechanisms, without directly linking the
200 * This may eventually go away, when the GSSAPI acceptors are merged,
201 * when we will just rely on the make_auth4_context_s4 hook instead.
203 * Even for NTLMSSP, which has a common module, significant parts of
204 * the behaviour are overridden here, because it uses the source4 NTLM
205 * stack and the source4 mapping between the PAC/SamLogon response and
208 * It is important to override all this to ensure that the exact same
209 * token is generated and used in the SMB and LDAP servers, for NTLM
212 static NTSTATUS prepare_gensec(const struct auth_context *auth_context,
214 struct gensec_security **gensec_context)
217 struct loadparm_context *lp_ctx;
218 struct tevent_context *event_ctx;
219 TALLOC_CTX *frame = talloc_stackframe();
220 struct gensec_security *gensec_ctx;
221 struct imessaging_context *msg_ctx;
222 struct cli_credentials *server_credentials;
223 struct server_id *server_id;
225 lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
226 if (lp_ctx == NULL) {
227 DEBUG(1, ("loadparm_init_s3 failed\n"));
229 return NT_STATUS_INVALID_SERVER_STATE;
231 event_ctx = s4_event_context_init(frame);
232 if (event_ctx == NULL) {
233 DEBUG(1, ("s4_event_context_init failed\n"));
235 return NT_STATUS_INVALID_SERVER_STATE;
238 server_id = new_server_id_task(frame);
239 if (server_id == NULL) {
240 DEBUG(1, ("new_server_id_task failed\n"));
242 return NT_STATUS_INVALID_SERVER_STATE;
245 msg_ctx = imessaging_init_discard_incoming(frame,
249 if (msg_ctx == NULL) {
250 DEBUG(1, ("imessaging_init_discard_incoming failed\n"));
252 return NT_STATUS_INVALID_SERVER_STATE;
255 talloc_reparent(frame, msg_ctx, server_id);
258 = cli_credentials_init_server(frame, lp_ctx);
259 if (!server_credentials) {
260 DEBUG(1, ("Failed to init server credentials"));
262 return NT_STATUS_INVALID_SERVER_STATE;
265 status = samba_server_gensec_start(mem_ctx,
267 lp_ctx, server_credentials, "cifs",
269 if (!NT_STATUS_IS_OK(status)) {
270 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
275 talloc_reparent(frame, gensec_ctx, msg_ctx);
276 talloc_reparent(frame, gensec_ctx, event_ctx);
277 talloc_reparent(frame, gensec_ctx, lp_ctx);
278 talloc_reparent(frame, gensec_ctx, server_credentials);
280 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
281 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_UNIX_TOKEN);
283 *gensec_context = gensec_ctx;
289 * Hook to allow handling of NTLM authentication for AD operation
290 * without directly linking the s4 auth stack
292 * This ensures we use the source4 authentication stack, as well as
293 * the authorization stack to create the user's token. This ensures
294 * consistency between NTLM logins and NTLMSSP logins, as NTLMSSP is
295 * handled by the hook above.
297 static NTSTATUS make_auth4_context_s4(const struct auth_context *auth_context,
299 struct auth4_context **auth4_context)
302 struct loadparm_context *lp_ctx;
303 struct tevent_context *event_ctx;
304 TALLOC_CTX *frame = talloc_stackframe();
305 struct imessaging_context *msg_ctx;
306 struct server_id *server_id;
308 lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
309 if (lp_ctx == NULL) {
310 DEBUG(1, ("loadparm_init_s3 failed\n"));
312 return NT_STATUS_INVALID_SERVER_STATE;
314 event_ctx = s4_event_context_init(frame);
315 if (event_ctx == NULL) {
316 DEBUG(1, ("s4_event_context_init failed\n"));
318 return NT_STATUS_INVALID_SERVER_STATE;
321 server_id = new_server_id_task(frame);
322 if (server_id == NULL) {
323 DEBUG(1, ("new_server_id_task failed\n"));
325 return NT_STATUS_INVALID_SERVER_STATE;
328 msg_ctx = imessaging_init_discard_incoming(frame,
332 if (msg_ctx == NULL) {
333 DEBUG(1, ("imessaging_init_discard_incoming failed\n"));
335 return NT_STATUS_INVALID_SERVER_STATE;
337 talloc_reparent(frame, msg_ctx, server_id);
339 /* Allow forcing a specific auth4 module */
340 if (!auth_context->forced_samba4_methods) {
341 status = auth_context_create(mem_ctx,
347 const char * const *forced_auth_methods = (const char * const *)str_list_make(mem_ctx, auth_context->forced_samba4_methods, NULL);
348 status = auth_context_create_methods(mem_ctx, forced_auth_methods, event_ctx, msg_ctx, lp_ctx, NULL, auth4_context);
350 if (!NT_STATUS_IS_OK(status)) {
351 DEBUG(1, ("Failed to start auth server code: %s\n", nt_errstr(status)));
356 talloc_reparent(frame, *auth4_context, msg_ctx);
357 talloc_reparent(frame, *auth4_context, event_ctx);
358 talloc_reparent(frame, *auth4_context, lp_ctx);
364 /* module initialisation */
365 static NTSTATUS auth_init_samba4(struct auth_context *auth_context,
367 struct auth_methods **auth_method)
369 struct auth_methods *result;
373 result = talloc_zero(auth_context, struct auth_methods);
374 if (result == NULL) {
375 return NT_STATUS_NO_MEMORY;
377 result->name = "samba4";
378 result->auth = check_samba4_security;
379 result->prepare_gensec = prepare_gensec;
380 result->make_auth4_context = make_auth4_context_s4;
382 if (param && *param) {
383 auth_context->forced_samba4_methods = talloc_strdup(result, param);
384 if (!auth_context->forced_samba4_methods) {
385 return NT_STATUS_NO_MEMORY;
389 *auth_method = result;
393 NTSTATUS auth_samba4_init(TALLOC_CTX *mem_ctx);
394 NTSTATUS auth_samba4_init(TALLOC_CTX *mem_ctx)
396 smb_register_auth(AUTH_INTERFACE_VERSION, "samba4",