CVE-2020-25719 kdc: Avoid races and multiple DB lookups in s4u2self check
[Samba.git] / source3 / auth / auth_generic.c
blobfc7a7549e8e5c6a6eb3b35f7ec62ad135c74d5e2
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
4 handle GENSEC authentication, server side
6 Copyright (C) Andrew Tridgell 2001
7 Copyright (C) Andrew Bartlett 2001-2003,2011
8 Copyright (C) Simo Sorce 2010.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include <tevent.h>
26 #include "../lib/util/tevent_ntstatus.h"
27 #include "auth.h"
28 #include "../lib/tsocket/tsocket.h"
29 #include "auth/gensec/gensec.h"
30 #include "lib/param/param.h"
31 #ifdef HAVE_KRB5
32 #include "auth/kerberos/pac_utils.h"
33 #include "nsswitch/libwbclient/wbclient.h"
34 #endif
35 #include "librpc/crypto/gse.h"
36 #include "auth/credentials/credentials.h"
37 #include "lib/param/loadparm.h"
38 #include "librpc/gen_ndr/dcerpc.h"
40 static NTSTATUS auth3_generate_session_info_pac(struct auth4_context *auth_ctx,
41 TALLOC_CTX *mem_ctx,
42 struct smb_krb5_context *smb_krb5_context,
43 DATA_BLOB *pac_blob,
44 const char *princ_name,
45 const struct tsocket_address *remote_address,
46 uint32_t session_info_flags,
47 struct auth_session_info **session_info)
49 enum server_role server_role = lp_server_role();
50 TALLOC_CTX *tmp_ctx;
51 bool is_mapped;
52 bool is_guest;
53 char *ntuser;
54 char *ntdomain;
55 char *username;
56 const char *rhost;
57 struct passwd *pw;
58 NTSTATUS status;
60 tmp_ctx = talloc_new(mem_ctx);
61 if (!tmp_ctx) {
62 return NT_STATUS_NO_MEMORY;
65 if (tsocket_address_is_inet(remote_address, "ip")) {
66 rhost = tsocket_address_inet_addr_string(
67 remote_address, tmp_ctx);
68 if (rhost == NULL) {
69 status = NT_STATUS_NO_MEMORY;
70 goto done;
72 } else {
73 rhost = "127.0.0.1";
76 if (server_role != ROLE_STANDALONE) {
77 struct wbcAuthUserParams params = { 0 };
78 struct wbcAuthUserInfo *info = NULL;
79 struct wbcAuthErrorInfo *err = NULL;
80 struct auth_serversupplied_info *server_info = NULL;
81 char *original_user_name = NULL;
82 char *p = NULL;
83 wbcErr wbc_err;
85 if (pac_blob == NULL) {
87 * This should already be catched at the main
88 * gensec layer, but better check twice
90 status = NT_STATUS_INTERNAL_ERROR;
91 goto done;
95 * Let winbind decode the PAC.
96 * This will also store the user
97 * data in the netsamlogon cache.
99 * This used to be a cache prime
100 * optimization, but now we delegate
101 * all logic to winbindd, as we require
102 * winbindd as domain member anyway.
104 params.level = WBC_AUTH_USER_LEVEL_PAC;
105 params.password.pac.data = pac_blob->data;
106 params.password.pac.length = pac_blob->length;
108 /* we are contacting the privileged pipe */
109 become_root();
110 wbc_err = wbcAuthenticateUserEx(&params, &info, &err);
111 unbecome_root();
114 * As this is merely a cache prime
115 * WBC_ERR_WINBIND_NOT_AVAILABLE
116 * is not a fatal error, treat it
117 * as success.
120 switch (wbc_err) {
121 case WBC_ERR_SUCCESS:
122 break;
123 case WBC_ERR_WINBIND_NOT_AVAILABLE:
124 status = NT_STATUS_NO_LOGON_SERVERS;
125 DBG_ERR("winbindd not running - "
126 "but required as domain member: %s\n",
127 nt_errstr(status));
128 goto done;
129 case WBC_ERR_AUTH_ERROR:
130 status = NT_STATUS(err->nt_status);
131 wbcFreeMemory(err);
132 goto done;
133 case WBC_ERR_NO_MEMORY:
134 status = NT_STATUS_NO_MEMORY;
135 goto done;
136 default:
137 status = NT_STATUS_LOGON_FAILURE;
138 goto done;
141 status = make_server_info_wbcAuthUserInfo(tmp_ctx,
142 info->account_name,
143 info->domain_name,
144 info, &server_info);
145 if (!NT_STATUS_IS_OK(status)) {
146 DEBUG(10, ("make_server_info_wbcAuthUserInfo failed: %s\n",
147 nt_errstr(status)));
148 goto done;
151 /* We skip doing this step if the caller asked us not to */
152 if (!(server_info->guest)) {
153 const char *unix_username = server_info->unix_name;
155 /* We might not be root if we are an RPC call */
156 become_root();
157 status = smb_pam_accountcheck(unix_username, rhost);
158 unbecome_root();
160 if (!NT_STATUS_IS_OK(status)) {
161 DEBUG(3, ("check_ntlm_password: PAM Account for user [%s] "
162 "FAILED with error %s\n",
163 unix_username, nt_errstr(status)));
164 goto done;
167 DEBUG(5, ("check_ntlm_password: PAM Account for user [%s] "
168 "succeeded\n", unix_username));
171 DEBUG(3, ("Kerberos ticket principal name is [%s]\n", princ_name));
173 p = strchr_m(princ_name, '@');
174 if (!p) {
175 DEBUG(3, ("[%s] Doesn't look like a valid principal\n",
176 princ_name));
177 status = NT_STATUS_LOGON_FAILURE;
178 goto done;
181 original_user_name = talloc_strndup(tmp_ctx, princ_name, p - princ_name);
182 if (original_user_name == NULL) {
183 status = NT_STATUS_NO_MEMORY;
184 goto done;
187 status = create_local_token(mem_ctx,
188 server_info,
189 NULL,
190 original_user_name,
191 session_info);
192 if (!NT_STATUS_IS_OK(status)) {
193 DEBUG(10, ("create_local_token failed: %s\n",
194 nt_errstr(status)));
195 goto done;
198 goto session_info_ready;
201 /* This is the standalone legacy code path */
203 if (pac_blob != NULL) {
205 * In standalone mode we don't expect a PAC!
206 * we only support MIT realms
208 status = NT_STATUS_BAD_TOKEN_TYPE;
209 DBG_WARNING("Unexpected PAC for [%s] in standalone mode - %s\n",
210 princ_name, nt_errstr(status));
211 if (!NT_STATUS_IS_OK(status)) {
212 goto done;
216 status = get_user_from_kerberos_info(tmp_ctx, rhost,
217 princ_name,
218 &is_mapped, &is_guest,
219 &ntuser, &ntdomain,
220 &username, &pw);
221 if (!NT_STATUS_IS_OK(status)) {
222 DBG_NOTICE("Failed to map kerberos principal to system user "
223 "(%s)\n", nt_errstr(status));
224 status = NT_STATUS_ACCESS_DENIED;
225 goto done;
228 status = make_session_info_krb5(mem_ctx,
229 ntuser, ntdomain, username, pw,
230 is_guest, is_mapped,
231 session_info);
232 if (!NT_STATUS_IS_OK(status)) {
233 DEBUG(1, ("Failed to map kerberos pac to server info (%s)\n",
234 nt_errstr(status)));
235 status = nt_status_squash(status);
236 goto done;
239 session_info_ready:
241 /* setup the string used by %U */
242 set_current_user_info((*session_info)->unix_info->sanitized_username,
243 (*session_info)->unix_info->unix_name,
244 (*session_info)->info->domain_name);
246 /* reload services so that the new %U is taken into account */
247 lp_load_with_shares(get_dyn_CONFIGFILE());
249 DEBUG(5, (__location__ "OK: user: %s domain: %s client: %s\n",
250 (*session_info)->info->account_name,
251 (*session_info)->info->domain_name,
252 rhost));
254 status = NT_STATUS_OK;
256 done:
257 TALLOC_FREE(tmp_ctx);
258 return status;
261 static struct auth4_context *make_auth4_context_s3(TALLOC_CTX *mem_ctx, struct auth_context *auth_context)
263 struct auth4_context *auth4_context = talloc_zero(mem_ctx, struct auth4_context);
264 if (auth4_context == NULL) {
265 DEBUG(10, ("failed to allocate auth4_context failed\n"));
266 return NULL;
268 auth4_context->generate_session_info_pac = auth3_generate_session_info_pac;
269 auth4_context->generate_session_info = auth3_generate_session_info;
270 auth4_context->get_ntlm_challenge = auth3_get_challenge;
271 auth4_context->set_ntlm_challenge = auth3_set_challenge;
272 auth4_context->check_ntlm_password_send = auth3_check_password_send;
273 auth4_context->check_ntlm_password_recv = auth3_check_password_recv;
274 auth4_context->private_data = talloc_steal(auth4_context, auth_context);
275 return auth4_context;
278 NTSTATUS make_auth4_context(TALLOC_CTX *mem_ctx, struct auth4_context **auth4_context_out)
280 struct auth_context *auth_context;
281 NTSTATUS nt_status;
283 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
284 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
286 nt_status = make_auth3_context_for_ntlm(tmp_ctx, &auth_context);
287 if (!NT_STATUS_IS_OK(nt_status)) {
288 TALLOC_FREE(tmp_ctx);
289 return nt_status;
292 if (auth_context->make_auth4_context) {
293 nt_status = auth_context->make_auth4_context(auth_context, mem_ctx, auth4_context_out);
294 TALLOC_FREE(tmp_ctx);
295 return nt_status;
297 } else {
298 struct auth4_context *auth4_context = make_auth4_context_s3(tmp_ctx, auth_context);
299 if (auth4_context == NULL) {
300 TALLOC_FREE(tmp_ctx);
301 return NT_STATUS_NO_MEMORY;
303 *auth4_context_out = talloc_steal(mem_ctx, auth4_context);
304 TALLOC_FREE(tmp_ctx);
305 return NT_STATUS_OK;
309 NTSTATUS auth_generic_prepare(TALLOC_CTX *mem_ctx,
310 const struct tsocket_address *remote_address,
311 const struct tsocket_address *local_address,
312 const char *service_description,
313 struct gensec_security **gensec_security_out)
315 struct gensec_security *gensec_security;
316 struct auth_context *auth_context = NULL;
317 NTSTATUS nt_status;
319 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
320 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
322 nt_status = make_auth3_context_for_ntlm(tmp_ctx, &auth_context);
323 if (!NT_STATUS_IS_OK(nt_status)) {
324 goto done;
327 if (auth_context->prepare_gensec) {
328 nt_status = auth_context->prepare_gensec(auth_context, tmp_ctx,
329 &gensec_security);
330 if (!NT_STATUS_IS_OK(nt_status)) {
331 goto done;
333 } else {
334 const struct gensec_security_ops **backends = NULL;
335 struct gensec_settings *gensec_settings;
336 struct loadparm_context *lp_ctx;
337 size_t idx = 0;
338 struct cli_credentials *server_credentials;
339 const char *dns_name;
340 const char *dns_domain;
341 bool ok;
342 struct auth4_context *auth4_context = make_auth4_context_s3(tmp_ctx, auth_context);
343 if (auth4_context == NULL) {
344 goto nomem;
347 lp_ctx = loadparm_init_s3(tmp_ctx, loadparm_s3_helpers());
348 if (lp_ctx == NULL) {
349 DEBUG(10, ("loadparm_init_s3 failed\n"));
350 nt_status = NT_STATUS_INVALID_SERVER_STATE;
351 goto done;
354 gensec_settings = lpcfg_gensec_settings(tmp_ctx, lp_ctx);
355 if (lp_ctx == NULL) {
356 DEBUG(10, ("lpcfg_gensec_settings failed\n"));
357 goto nomem;
361 * This should be a 'netbios domain -> DNS domain'
362 * mapping, and can currently validly return NULL on
363 * poorly configured systems.
365 * This is used for the NTLMSSP server
368 dns_name = get_mydnsfullname();
369 if (dns_name == NULL) {
370 dns_name = "";
373 dns_domain = get_mydnsdomname(tmp_ctx);
374 if (dns_domain == NULL) {
375 dns_domain = "";
378 gensec_settings->server_dns_name = strlower_talloc(gensec_settings, dns_name);
379 if (gensec_settings->server_dns_name == NULL) {
380 goto nomem;
383 gensec_settings->server_dns_domain = strlower_talloc(gensec_settings, dns_domain);
384 if (gensec_settings->server_dns_domain == NULL) {
385 goto nomem;
388 backends = talloc_zero_array(gensec_settings,
389 const struct gensec_security_ops *, 6);
390 if (backends == NULL) {
391 goto nomem;
393 gensec_settings->backends = backends;
395 gensec_init();
397 /* These need to be in priority order, krb5 before NTLMSSP */
398 #if defined(HAVE_KRB5)
399 backends[idx++] = &gensec_gse_krb5_security_ops;
400 #endif
402 backends[idx++] = gensec_security_by_oid(NULL, GENSEC_OID_NTLMSSP);
404 backends[idx++] = gensec_security_by_oid(NULL, GENSEC_OID_SPNEGO);
406 backends[idx++] = gensec_security_by_auth_type(NULL, DCERPC_AUTH_TYPE_SCHANNEL);
408 backends[idx++] = gensec_security_by_auth_type(NULL, DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM);
411 * This is anonymous for now, because we just use it
412 * to set the kerberos state at the moment
414 server_credentials = cli_credentials_init_anon(tmp_ctx);
415 if (!server_credentials) {
416 DEBUG(0, ("auth_generic_prepare: Failed to init server credentials\n"));
417 goto nomem;
420 ok = cli_credentials_set_conf(server_credentials, lp_ctx);
421 if (!ok) {
422 DBG_ERR("Failed to set server credentials defaults "
423 "from smb.conf.\n");
424 goto nomem;
427 if (lp_security() == SEC_ADS || USE_KERBEROS_KEYTAB) {
428 cli_credentials_set_kerberos_state(server_credentials,
429 CRED_USE_KERBEROS_DESIRED,
430 CRED_SPECIFIED);
431 } else {
432 cli_credentials_set_kerberos_state(server_credentials,
433 CRED_USE_KERBEROS_DISABLED,
434 CRED_SPECIFIED);
437 nt_status = gensec_server_start(tmp_ctx, gensec_settings,
438 auth4_context, &gensec_security);
440 if (!NT_STATUS_IS_OK(nt_status)) {
441 goto done;
444 nt_status = gensec_set_credentials(
445 gensec_security, server_credentials);
446 if (!NT_STATUS_IS_OK(nt_status)) {
447 goto done;
451 nt_status = gensec_set_remote_address(gensec_security,
452 remote_address);
453 if (!NT_STATUS_IS_OK(nt_status)) {
454 goto done;
457 nt_status = gensec_set_local_address(gensec_security,
458 local_address);
459 if (!NT_STATUS_IS_OK(nt_status)) {
460 goto done;
463 nt_status = gensec_set_target_service_description(gensec_security,
464 service_description);
465 if (!NT_STATUS_IS_OK(nt_status)) {
466 goto done;
469 *gensec_security_out = talloc_move(mem_ctx, &gensec_security);
470 nt_status = NT_STATUS_OK;
471 goto done;
472 nomem:
473 nt_status = NT_STATUS_NO_MEMORY;
474 done:
475 TALLOC_FREE(tmp_ctx);
476 return nt_status;
480 * Check a username and password, and return the final session_info.
481 * We also log the authorization of the session here, just as
482 * gensec_session_info() does.
484 NTSTATUS auth_check_password_session_info(struct auth4_context *auth_context,
485 TALLOC_CTX *mem_ctx,
486 struct auth_usersupplied_info *user_info,
487 struct auth_session_info **session_info)
489 NTSTATUS nt_status;
490 void *server_info;
491 uint8_t authoritative = 1;
492 struct tevent_context *ev = NULL;
493 struct tevent_req *subreq = NULL;
494 bool ok;
496 ev = samba_tevent_context_init(talloc_tos());
497 if (ev == NULL) {
498 return NT_STATUS_NO_MEMORY;
501 subreq = auth_context->check_ntlm_password_send(ev, ev,
502 auth_context,
503 user_info);
504 if (subreq == NULL) {
505 TALLOC_FREE(ev);
506 return NT_STATUS_NO_MEMORY;
508 ok = tevent_req_poll_ntstatus(subreq, ev, &nt_status);
509 if (!ok) {
510 TALLOC_FREE(ev);
511 return nt_status;
513 nt_status = auth_context->check_ntlm_password_recv(subreq,
514 talloc_tos(),
515 &authoritative,
516 &server_info,
517 NULL, NULL);
518 TALLOC_FREE(ev);
519 if (!NT_STATUS_IS_OK(nt_status)) {
520 return nt_status;
523 nt_status = auth_context->generate_session_info(auth_context,
524 mem_ctx,
525 server_info,
526 user_info->client.account_name,
527 AUTH_SESSION_INFO_UNIX_TOKEN |
528 AUTH_SESSION_INFO_DEFAULT_GROUPS |
529 AUTH_SESSION_INFO_NTLM,
530 session_info);
531 TALLOC_FREE(server_info);
533 if (!NT_STATUS_IS_OK(nt_status)) {
534 return nt_status;
538 * This is rather redundant (the authentication has just been
539 * logged, with much the same details), but because we want to
540 * log all authorizations consistently (be they NLTM, NTLMSSP
541 * or krb5) we log this info again as an authorization.
543 log_successful_authz_event(auth_context->msg_ctx,
544 auth_context->lp_ctx,
545 user_info->remote_host,
546 user_info->local_host,
547 user_info->service_description,
548 user_info->auth_description,
549 AUTHZ_TRANSPORT_PROTECTION_SMB,
550 *session_info);
552 return nt_status;