tdb/tools: add -l option to tdbtool
[Samba.git] / source3 / auth / auth_samba4.c
blobd9d71512a2baa493fb191244d6afa28a2d3537a7
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
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/>.
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"
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_AUTH
34 static NTSTATUS make_auth4_context_s4(const struct auth_context *auth_context,
35 TALLOC_CTX *mem_ctx,
36 struct auth4_context **auth4_context);
38 static struct idr_context *task_id_tree;
40 static int free_task_id(struct server_id *server_id)
42 idr_remove(task_id_tree, server_id->task_id);
43 return 0;
46 /* Return a server_id with a unique task_id element. Free the
47 * returned pointer to de-allocate the task_id via a talloc destructor
48 * (ie, use talloc_free()) */
49 static struct server_id *new_server_id_task(TALLOC_CTX *mem_ctx)
51 struct server_id *server_id;
52 int task_id;
53 if (!task_id_tree) {
54 task_id_tree = idr_init(NULL);
55 if (!task_id_tree) {
56 return NULL;
60 server_id = talloc(mem_ctx, struct server_id);
62 if (!server_id) {
63 return NULL;
65 *server_id = procid_self();
67 /* 0 is the default server_id, so we need to start with 1 */
68 task_id = idr_get_new_above(task_id_tree, server_id, 1, INT32_MAX);
70 if (task_id == -1) {
71 talloc_free(server_id);
72 return NULL;
75 talloc_set_destructor(server_id, free_task_id);
76 server_id->task_id = task_id;
77 return server_id;
81 * This module is not an ordinary authentication module. It is really
82 * a way to redirect the whole authentication and authorization stack
83 * to use the source4 auth code, not a way to just handle NTLM
84 * authentication.
86 * See the comments above each function for how that hook changes the
87 * behaviour.
90 /*
91 * This hook is currently used by winbindd only, as all other NTLM
92 * logins go via the hooks provided by make_auth4_context_s4() below.
94 * This is only left in case we find a way that it might become useful
95 * in future. Importantly, this routine returns the information
96 * needed for a NETLOGON SamLogon, not what is needed to establish a
97 * session.
99 * We expect we may use this hook in the source3/ winbind when this
100 * services the AD DC. It is tested via pdbtest.
103 static NTSTATUS check_samba4_security(const struct auth_context *auth_context,
104 void *my_private_data,
105 TALLOC_CTX *mem_ctx,
106 const struct auth_usersupplied_info *user_info,
107 struct auth_serversupplied_info **server_info)
109 TALLOC_CTX *frame = talloc_stackframe();
110 struct netr_SamInfo3 *info3 = NULL;
111 NTSTATUS nt_status;
112 struct auth_user_info_dc *user_info_dc;
113 struct auth4_context *auth4_context;
115 nt_status = make_auth4_context_s4(auth_context, mem_ctx, &auth4_context);
116 if (!NT_STATUS_IS_OK(nt_status)) {
117 TALLOC_FREE(frame);
118 goto done;
121 nt_status = auth_context_set_challenge(auth4_context, auth_context->challenge.data, "auth_samba4");
122 if (!NT_STATUS_IS_OK(nt_status)) {
123 TALLOC_FREE(auth4_context);
124 TALLOC_FREE(frame);
125 return nt_status;
128 nt_status = auth_check_password(auth4_context, auth4_context, user_info, &user_info_dc);
129 if (!NT_STATUS_IS_OK(nt_status)) {
130 TALLOC_FREE(auth4_context);
131 TALLOC_FREE(frame);
132 return nt_status;
135 nt_status = auth_convert_user_info_dc_saminfo3(mem_ctx,
136 user_info_dc,
137 &info3);
138 if (NT_STATUS_IS_OK(nt_status)) {
139 /* We need the strings from the server_info to be valid as long as the info3 is around */
140 talloc_steal(info3, user_info_dc);
142 talloc_free(auth4_context);
144 if (!NT_STATUS_IS_OK(nt_status)) {
145 goto done;
148 nt_status = make_server_info_info3(mem_ctx, user_info->client.account_name,
149 user_info->mapped.domain_name, server_info,
150 info3);
151 if (!NT_STATUS_IS_OK(nt_status)) {
152 DEBUG(10, ("make_server_info_info3 failed: %s\n",
153 nt_errstr(nt_status)));
154 TALLOC_FREE(frame);
155 return nt_status;
158 nt_status = NT_STATUS_OK;
160 done:
161 TALLOC_FREE(frame);
162 return nt_status;
166 * Hook to allow the source4 set of GENSEC modules to handle
167 * blob-based authentication mechanisms, without directly linking the
168 * mechanism code.
170 * This may eventually go away, when the GSSAPI acceptors are merged,
171 * when we will just rely on the make_auth4_context_s4 hook instead.
173 * Even for NTLMSSP, which has a common module, significant parts of
174 * the behaviour are overridden here, because it uses the source4 NTLM
175 * stack and the source4 mapping between the PAC/SamLogon response and
176 * the local token.
178 * It is important to override all this to ensure that the exact same
179 * token is generated and used in the SMB and LDAP servers, for NTLM
180 * and for Kerberos.
182 static NTSTATUS prepare_gensec(struct auth_context *auth_context,
183 TALLOC_CTX *mem_ctx,
184 struct gensec_security **gensec_context)
186 NTSTATUS status;
187 struct loadparm_context *lp_ctx;
188 struct tevent_context *event_ctx;
189 TALLOC_CTX *frame = talloc_stackframe();
190 struct gensec_security *gensec_ctx;
191 struct imessaging_context *msg_ctx;
192 struct cli_credentials *server_credentials;
193 struct server_id *server_id;
195 lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
196 if (lp_ctx == NULL) {
197 DEBUG(1, ("loadparm_init_s3 failed\n"));
198 TALLOC_FREE(frame);
199 return NT_STATUS_INVALID_SERVER_STATE;
201 event_ctx = s4_event_context_init(frame);
202 if (event_ctx == NULL) {
203 DEBUG(1, ("s4_event_context_init failed\n"));
204 TALLOC_FREE(frame);
205 return NT_STATUS_INVALID_SERVER_STATE;
208 server_id = new_server_id_task(frame);
209 if (server_id == NULL) {
210 DEBUG(1, ("new_server_id_task failed\n"));
211 TALLOC_FREE(frame);
212 return NT_STATUS_INVALID_SERVER_STATE;
215 msg_ctx = imessaging_init(frame,
216 lp_ctx,
217 *server_id,
218 event_ctx, true);
219 if (msg_ctx == NULL) {
220 DEBUG(1, ("imessaging_init failed\n"));
221 TALLOC_FREE(frame);
222 return NT_STATUS_INVALID_SERVER_STATE;
225 talloc_reparent(frame, msg_ctx, server_id);
227 server_credentials
228 = cli_credentials_init(frame);
229 if (!server_credentials) {
230 DEBUG(1, ("Failed to init server credentials"));
231 TALLOC_FREE(frame);
232 return NT_STATUS_INVALID_SERVER_STATE;
235 cli_credentials_set_conf(server_credentials, lp_ctx);
236 status = cli_credentials_set_machine_account(server_credentials, lp_ctx);
237 if (!NT_STATUS_IS_OK(status)) {
238 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
239 talloc_free(server_credentials);
240 server_credentials = NULL;
243 status = samba_server_gensec_start(mem_ctx,
244 event_ctx, msg_ctx,
245 lp_ctx, server_credentials, "cifs",
246 &gensec_ctx);
247 if (!NT_STATUS_IS_OK(status)) {
248 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
249 TALLOC_FREE(frame);
250 return status;
253 talloc_reparent(frame, gensec_ctx, msg_ctx);
254 talloc_reparent(frame, gensec_ctx, event_ctx);
255 talloc_reparent(frame, gensec_ctx, lp_ctx);
256 talloc_reparent(frame, gensec_ctx, server_credentials);
258 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
259 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_UNIX_TOKEN);
261 *gensec_context = gensec_ctx;
262 TALLOC_FREE(frame);
263 return status;
267 * Hook to allow handling of NTLM authentication for AD operation
268 * without directly linking the s4 auth stack
270 * This ensures we use the source4 authentication stack, as well as
271 * the authorization stack to create the user's token. This ensures
272 * consistency between NTLM logins and NTLMSSP logins, as NTLMSSP is
273 * handled by the hook above.
275 static NTSTATUS make_auth4_context_s4(const struct auth_context *auth_context,
276 TALLOC_CTX *mem_ctx,
277 struct auth4_context **auth4_context)
279 NTSTATUS status;
280 struct loadparm_context *lp_ctx;
281 struct tevent_context *event_ctx;
282 TALLOC_CTX *frame = talloc_stackframe();
283 struct imessaging_context *msg_ctx;
284 struct server_id *server_id;
286 lp_ctx = loadparm_init_s3(frame, loadparm_s3_helpers());
287 if (lp_ctx == NULL) {
288 DEBUG(1, ("loadparm_init_s3 failed\n"));
289 TALLOC_FREE(frame);
290 return NT_STATUS_INVALID_SERVER_STATE;
292 event_ctx = s4_event_context_init(frame);
293 if (event_ctx == NULL) {
294 DEBUG(1, ("s4_event_context_init failed\n"));
295 TALLOC_FREE(frame);
296 return NT_STATUS_INVALID_SERVER_STATE;
299 server_id = new_server_id_task(frame);
300 if (server_id == NULL) {
301 DEBUG(1, ("new_server_id_task failed\n"));
302 TALLOC_FREE(frame);
303 return NT_STATUS_INVALID_SERVER_STATE;
306 msg_ctx = imessaging_init(frame,
307 lp_ctx,
308 *server_id,
309 event_ctx, true);
310 if (msg_ctx == NULL) {
311 DEBUG(1, ("imessaging_init failed\n"));
312 TALLOC_FREE(frame);
313 return NT_STATUS_INVALID_SERVER_STATE;
315 talloc_reparent(frame, msg_ctx, server_id);
317 /* Allow forcing a specific auth4 module */
318 if (!auth_context->forced_samba4_methods) {
319 status = auth_context_create(mem_ctx,
320 event_ctx,
321 msg_ctx,
322 lp_ctx,
323 auth4_context);
324 } else {
325 const char * const *forced_auth_methods = (const char * const *)str_list_make(mem_ctx, auth_context->forced_samba4_methods, NULL);
326 status = auth_context_create_methods(mem_ctx, forced_auth_methods, event_ctx, msg_ctx, lp_ctx, NULL, auth4_context);
328 if (!NT_STATUS_IS_OK(status)) {
329 DEBUG(1, ("Failed to start auth server code: %s\n", nt_errstr(status)));
330 TALLOC_FREE(frame);
331 return status;
334 talloc_reparent(frame, *auth4_context, msg_ctx);
335 talloc_reparent(frame, *auth4_context, event_ctx);
336 talloc_reparent(frame, *auth4_context, lp_ctx);
338 TALLOC_FREE(frame);
339 return status;
342 /* module initialisation */
343 static NTSTATUS auth_init_samba4(struct auth_context *auth_context,
344 const char *param,
345 auth_methods **auth_method)
347 struct auth_methods *result;
349 gensec_init();
351 result = talloc_zero(auth_context, struct auth_methods);
352 if (result == NULL) {
353 return NT_STATUS_NO_MEMORY;
355 result->name = "samba4";
356 result->auth = check_samba4_security;
357 result->prepare_gensec = prepare_gensec;
358 result->make_auth4_context = make_auth4_context_s4;
360 if (param && *param) {
361 auth_context->forced_samba4_methods = talloc_strdup(result, param);
362 if (!auth_context->forced_samba4_methods) {
363 return NT_STATUS_NO_MEMORY;
367 *auth_method = result;
368 return NT_STATUS_OK;
371 NTSTATUS auth_samba4_init(void);
372 NTSTATUS auth_samba4_init(void)
374 smb_register_auth(AUTH_INTERFACE_VERSION, "samba4",
375 auth_init_samba4);
376 return NT_STATUS_OK;