dns_server: Do not look for a wildcard for @
[Samba.git] / source3 / auth / auth_generic.c
blob6dedeedd30229661ce2c8188d816784be45b2ff6
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 "auth.h"
26 #include "../lib/tsocket/tsocket.h"
27 #include "auth/gensec/gensec.h"
28 #include "lib/param/param.h"
29 #ifdef HAVE_KRB5
30 #include "auth/kerberos/pac_utils.h"
31 #include "nsswitch/libwbclient/wbclient.h"
32 #endif
33 #include "librpc/crypto/gse.h"
34 #include "auth/credentials/credentials.h"
35 #include "lib/param/loadparm.h"
36 #include "librpc/gen_ndr/dcerpc.h"
38 static NTSTATUS auth3_generate_session_info_pac(struct auth4_context *auth_ctx,
39 TALLOC_CTX *mem_ctx,
40 struct smb_krb5_context *smb_krb5_context,
41 DATA_BLOB *pac_blob,
42 const char *princ_name,
43 const struct tsocket_address *remote_address,
44 uint32_t session_info_flags,
45 struct auth_session_info **session_info)
47 TALLOC_CTX *tmp_ctx;
48 struct PAC_LOGON_INFO *logon_info = NULL;
49 struct netr_SamInfo3 *info3_copy = NULL;
50 bool is_mapped;
51 bool is_guest;
52 char *ntuser;
53 char *ntdomain;
54 char *username;
55 char *rhost;
56 struct passwd *pw;
57 NTSTATUS status;
58 int rc;
60 tmp_ctx = talloc_new(mem_ctx);
61 if (!tmp_ctx) {
62 return NT_STATUS_NO_MEMORY;
65 if (pac_blob) {
66 #ifdef HAVE_KRB5
67 struct wbcAuthUserParams params = {};
68 struct wbcAuthUserInfo *info = NULL;
69 struct wbcAuthErrorInfo *err = NULL;
70 wbcErr wbc_err;
73 * Let winbind decode the PAC.
74 * This will also store the user
75 * data in the netsamlogon cache.
77 * We need to do this *before* we
78 * call get_user_from_kerberos_info()
79 * as that does a user lookup that
80 * expects info in the netsamlogon cache.
82 * See BUG: https://bugzilla.samba.org/show_bug.cgi?id=11259
84 params.level = WBC_AUTH_USER_LEVEL_PAC;
85 params.password.pac.data = pac_blob->data;
86 params.password.pac.length = pac_blob->length;
88 become_root();
89 wbc_err = wbcAuthenticateUserEx(&params, &info, &err);
90 unbecome_root();
93 * As this is merely a cache prime
94 * WBC_ERR_WINBIND_NOT_AVAILABLE
95 * is not a fatal error, treat it
96 * as success.
99 switch (wbc_err) {
100 case WBC_ERR_WINBIND_NOT_AVAILABLE:
101 case WBC_ERR_SUCCESS:
102 break;
103 case WBC_ERR_AUTH_ERROR:
104 status = NT_STATUS(err->nt_status);
105 wbcFreeMemory(err);
106 goto done;
107 default:
108 status = NT_STATUS_LOGON_FAILURE;
109 goto done;
112 status = kerberos_pac_logon_info(tmp_ctx, *pac_blob, NULL, NULL,
113 NULL, NULL, 0, &logon_info);
114 #else
115 status = NT_STATUS_ACCESS_DENIED;
116 #endif
117 if (!NT_STATUS_IS_OK(status)) {
118 goto done;
122 rc = get_remote_hostname(remote_address,
123 &rhost,
124 tmp_ctx);
125 if (rc < 0) {
126 status = NT_STATUS_NO_MEMORY;
127 goto done;
129 if (strequal(rhost, "UNKNOWN")) {
130 rhost = tsocket_address_inet_addr_string(remote_address,
131 tmp_ctx);
132 if (rhost == NULL) {
133 status = NT_STATUS_NO_MEMORY;
134 goto done;
138 status = get_user_from_kerberos_info(tmp_ctx, rhost,
139 princ_name, logon_info,
140 &is_mapped, &is_guest,
141 &ntuser, &ntdomain,
142 &username, &pw);
143 if (!NT_STATUS_IS_OK(status)) {
144 DBG_NOTICE("Failed to map kerberos principal to system user "
145 "(%s)\n", nt_errstr(status));
146 status = NT_STATUS_ACCESS_DENIED;
147 goto done;
150 /* Get the info3 from the PAC data if we have it */
151 if (logon_info) {
152 status = create_info3_from_pac_logon_info(tmp_ctx,
153 logon_info,
154 &info3_copy);
155 if (!NT_STATUS_IS_OK(status)) {
156 goto done;
160 /* setup the string used by %U */
161 sub_set_smb_name(username);
163 /* reload services so that the new %U is taken into account */
164 lp_load_with_shares(get_dyn_CONFIGFILE());
166 status = make_session_info_krb5(mem_ctx,
167 ntuser, ntdomain, username, pw,
168 info3_copy, is_guest, is_mapped, NULL /* No session key for now, caller will sort it out */,
169 session_info);
170 if (!NT_STATUS_IS_OK(status)) {
171 DEBUG(1, ("Failed to map kerberos pac to server info (%s)\n",
172 nt_errstr(status)));
173 status = NT_STATUS_ACCESS_DENIED;
174 goto done;
177 DEBUG(5, (__location__ "OK: user: %s domain: %s client: %s\n",
178 ntuser, ntdomain, rhost));
180 status = NT_STATUS_OK;
182 done:
183 TALLOC_FREE(tmp_ctx);
184 return status;
187 static struct auth4_context *make_auth4_context_s3(TALLOC_CTX *mem_ctx, struct auth_context *auth_context)
189 struct auth4_context *auth4_context = talloc_zero(mem_ctx, struct auth4_context);
190 if (auth4_context == NULL) {
191 DEBUG(10, ("failed to allocate auth4_context failed\n"));
192 return NULL;
194 auth4_context->generate_session_info_pac = auth3_generate_session_info_pac;
195 auth4_context->generate_session_info = auth3_generate_session_info;
196 auth4_context->get_ntlm_challenge = auth3_get_challenge;
197 auth4_context->set_ntlm_challenge = auth3_set_challenge;
198 auth4_context->check_ntlm_password = auth3_check_password;
199 auth4_context->private_data = talloc_steal(auth4_context, auth_context);
200 return auth4_context;
203 NTSTATUS make_auth4_context(TALLOC_CTX *mem_ctx, struct auth4_context **auth4_context_out)
205 struct auth_context *auth_context;
206 NTSTATUS nt_status;
208 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
209 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
211 nt_status = make_auth3_context_for_ntlm(tmp_ctx, &auth_context);
212 if (!NT_STATUS_IS_OK(nt_status)) {
213 TALLOC_FREE(tmp_ctx);
214 return nt_status;
217 if (auth_context->make_auth4_context) {
218 nt_status = auth_context->make_auth4_context(auth_context, mem_ctx, auth4_context_out);
219 TALLOC_FREE(tmp_ctx);
220 return nt_status;
222 } else {
223 struct auth4_context *auth4_context = make_auth4_context_s3(tmp_ctx, auth_context);
224 if (auth4_context == NULL) {
225 TALLOC_FREE(tmp_ctx);
226 return NT_STATUS_NO_MEMORY;
228 *auth4_context_out = talloc_steal(mem_ctx, auth4_context);
229 TALLOC_FREE(tmp_ctx);
230 return NT_STATUS_OK;
234 NTSTATUS auth_generic_prepare(TALLOC_CTX *mem_ctx,
235 const struct tsocket_address *remote_address,
236 const struct tsocket_address *local_address,
237 const char *service_description,
238 struct gensec_security **gensec_security_out)
240 struct gensec_security *gensec_security;
241 struct auth_context *auth_context;
242 NTSTATUS nt_status;
244 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
245 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
247 nt_status = make_auth3_context_for_ntlm(tmp_ctx, &auth_context);
248 if (!NT_STATUS_IS_OK(nt_status)) {
249 TALLOC_FREE(tmp_ctx);
250 return nt_status;
253 if (auth_context->prepare_gensec) {
254 nt_status = auth_context->prepare_gensec(auth_context, tmp_ctx,
255 &gensec_security);
256 if (!NT_STATUS_IS_OK(nt_status)) {
257 TALLOC_FREE(tmp_ctx);
258 return nt_status;
260 } else {
261 const struct gensec_security_ops **backends = NULL;
262 struct gensec_settings *gensec_settings;
263 struct loadparm_context *lp_ctx;
264 size_t idx = 0;
265 struct cli_credentials *server_credentials;
266 const char *dns_name;
267 const char *dns_domain;
268 struct auth4_context *auth4_context = make_auth4_context_s3(tmp_ctx, auth_context);
269 if (auth4_context == NULL) {
270 TALLOC_FREE(tmp_ctx);
271 return NT_STATUS_NO_MEMORY;
274 lp_ctx = loadparm_init_s3(tmp_ctx, loadparm_s3_helpers());
275 if (lp_ctx == NULL) {
276 DEBUG(10, ("loadparm_init_s3 failed\n"));
277 TALLOC_FREE(tmp_ctx);
278 return NT_STATUS_INVALID_SERVER_STATE;
281 gensec_settings = lpcfg_gensec_settings(tmp_ctx, lp_ctx);
282 if (lp_ctx == NULL) {
283 DEBUG(10, ("lpcfg_gensec_settings failed\n"));
284 TALLOC_FREE(tmp_ctx);
285 return NT_STATUS_NO_MEMORY;
289 * This should be a 'netbios domain -> DNS domain'
290 * mapping, and can currently validly return NULL on
291 * poorly configured systems.
293 * This is used for the NTLMSSP server
296 dns_name = get_mydnsfullname();
297 if (dns_name == NULL) {
298 dns_name = "";
301 dns_domain = get_mydnsdomname(tmp_ctx);
302 if (dns_domain == NULL) {
303 dns_domain = "";
306 gensec_settings->server_dns_name = strlower_talloc(gensec_settings, dns_name);
307 if (gensec_settings->server_dns_name == NULL) {
308 TALLOC_FREE(tmp_ctx);
309 return NT_STATUS_NO_MEMORY;
312 gensec_settings->server_dns_domain = strlower_talloc(gensec_settings, dns_domain);
313 if (gensec_settings->server_dns_domain == NULL) {
314 TALLOC_FREE(tmp_ctx);
315 return NT_STATUS_NO_MEMORY;
318 backends = talloc_zero_array(gensec_settings,
319 const struct gensec_security_ops *, 6);
320 if (backends == NULL) {
321 TALLOC_FREE(tmp_ctx);
322 return NT_STATUS_NO_MEMORY;
324 gensec_settings->backends = backends;
326 gensec_init();
328 /* These need to be in priority order, krb5 before NTLMSSP */
329 #if defined(HAVE_KRB5)
330 backends[idx++] = &gensec_gse_krb5_security_ops;
331 #endif
333 backends[idx++] = gensec_security_by_oid(NULL, GENSEC_OID_NTLMSSP);
335 backends[idx++] = gensec_security_by_oid(NULL, GENSEC_OID_SPNEGO);
337 backends[idx++] = gensec_security_by_auth_type(NULL, DCERPC_AUTH_TYPE_SCHANNEL);
339 backends[idx++] = gensec_security_by_auth_type(NULL, DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM);
342 * This is anonymous for now, because we just use it
343 * to set the kerberos state at the moment
345 server_credentials = cli_credentials_init_anon(tmp_ctx);
346 if (!server_credentials) {
347 DEBUG(0, ("auth_generic_prepare: Failed to init server credentials\n"));
348 return NT_STATUS_NO_MEMORY;
351 cli_credentials_set_conf(server_credentials, lp_ctx);
353 if (lp_security() == SEC_ADS || USE_KERBEROS_KEYTAB) {
354 cli_credentials_set_kerberos_state(server_credentials, CRED_AUTO_USE_KERBEROS);
355 } else {
356 cli_credentials_set_kerberos_state(server_credentials, CRED_DONT_USE_KERBEROS);
359 nt_status = gensec_server_start(tmp_ctx, gensec_settings,
360 auth4_context, &gensec_security);
362 if (!NT_STATUS_IS_OK(nt_status)) {
363 TALLOC_FREE(tmp_ctx);
364 return nt_status;
367 gensec_set_credentials(gensec_security, server_credentials);
369 talloc_unlink(tmp_ctx, lp_ctx);
370 talloc_unlink(tmp_ctx, server_credentials);
371 talloc_unlink(tmp_ctx, gensec_settings);
372 talloc_unlink(tmp_ctx, auth4_context);
375 nt_status = gensec_set_remote_address(gensec_security,
376 remote_address);
377 if (!NT_STATUS_IS_OK(nt_status)) {
378 TALLOC_FREE(tmp_ctx);
379 return nt_status;
382 nt_status = gensec_set_local_address(gensec_security,
383 local_address);
384 if (!NT_STATUS_IS_OK(nt_status)) {
385 TALLOC_FREE(tmp_ctx);
386 return nt_status;
389 nt_status = gensec_set_target_service_description(gensec_security,
390 service_description);
392 if (!NT_STATUS_IS_OK(nt_status)) {
393 TALLOC_FREE(tmp_ctx);
394 return nt_status;
397 *gensec_security_out = talloc_steal(mem_ctx, gensec_security);
398 TALLOC_FREE(tmp_ctx);
399 return NT_STATUS_OK;
403 * Check a username and password, and return the final session_info.
404 * We also log the authorization of the session here, just as
405 * gensec_session_info() does.
407 NTSTATUS auth_check_password_session_info(struct auth4_context *auth_context,
408 TALLOC_CTX *mem_ctx,
409 struct auth_usersupplied_info *user_info,
410 struct auth_session_info **session_info)
412 NTSTATUS nt_status;
413 void *server_info;
414 uint8_t authoritative = 0;
416 nt_status = auth_context->check_ntlm_password(auth_context,
417 talloc_tos(),
418 user_info,
419 &authoritative,
420 &server_info, NULL, NULL);
422 if (!NT_STATUS_IS_OK(nt_status)) {
423 return nt_status;
426 nt_status = auth_context->generate_session_info(auth_context,
427 mem_ctx,
428 server_info,
429 user_info->client.account_name,
430 AUTH_SESSION_INFO_UNIX_TOKEN |
431 AUTH_SESSION_INFO_DEFAULT_GROUPS |
432 AUTH_SESSION_INFO_NTLM,
433 session_info);
434 TALLOC_FREE(server_info);
436 if (!NT_STATUS_IS_OK(nt_status)) {
437 return nt_status;
441 * This is rather redundant (the authentication has just been
442 * logged, with much the same details), but because we want to
443 * log all authorizations consistently (be they NLTM, NTLMSSP
444 * or krb5) we log this info again as an authorization.
446 log_successful_authz_event(auth_context->msg_ctx,
447 auth_context->lp_ctx,
448 user_info->remote_host,
449 user_info->local_host,
450 user_info->service_description,
451 user_info->auth_description,
452 AUTHZ_TRANSPORT_PROTECTION_SMB,
453 *session_info);
455 return nt_status;