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