2 Unix SMB/CIFS implementation.
4 PAC Glue between Samba and the KDC
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2009
7 Copyright (C) Simo Sorce <idra@samba.org> 2010
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 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/>.
25 #include "../libds/common/flags.h"
27 #include "auth/auth.h"
28 #include "auth/auth_sam_reply.h"
29 #include "system/kerberos.h"
30 #include "auth/kerberos/kerberos.h"
31 #include "kdc/samba_kdc.h"
32 #include "kdc/pac-glue.h"
33 #include "param/param.h"
34 #include "librpc/gen_ndr/ndr_krb5pac.h"
35 #include "libcli/security/security.h"
36 #include "dsdb/samdb/samdb.h"
37 #include "auth/kerberos/pac_utils.h"
40 NTSTATUS samba_get_logon_info_pac_blob(TALLOC_CTX *mem_ctx,
41 struct auth_user_info_dc *info,
44 struct netr_SamInfo3 *info3;
45 union PAC_INFO pac_info;
46 enum ndr_err_code ndr_err;
49 ZERO_STRUCT(pac_info);
51 nt_status = auth_convert_user_info_dc_saminfo3(mem_ctx, info, &info3);
52 if (!NT_STATUS_IS_OK(nt_status)) {
53 DEBUG(1, ("Getting Samba info failed: %s\n",
54 nt_errstr(nt_status)));
58 pac_info.logon_info.info = talloc_zero(mem_ctx, struct PAC_LOGON_INFO);
59 if (!pac_info.logon_info.info) {
60 return NT_STATUS_NO_MEMORY;
63 pac_info.logon_info.info->info3 = *info3;
65 ndr_err = ndr_push_union_blob(pac_data, mem_ctx, &pac_info,
67 (ndr_push_flags_fn_t)ndr_push_PAC_INFO);
68 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
69 nt_status = ndr_map_error2ntstatus(ndr_err);
70 DEBUG(1, ("PAC (presig) push failed: %s\n",
71 nt_errstr(nt_status)));
78 krb5_error_code samba_make_krb5_pac(krb5_context context,
80 DATA_BLOB *deleg_blob,
87 /* The user account may be set not to want the PAC */
92 ret = krb5_copy_data_contents(&pac_data,
99 ZERO_STRUCT(deleg_data);
101 ret = krb5_copy_data_contents(&deleg_data,
105 kerberos_free_data_contents(context, &pac_data);
110 ret = krb5_pac_init(context, pac);
112 kerberos_free_data_contents(context, &pac_data);
113 kerberos_free_data_contents(context, &deleg_data);
117 ret = krb5_pac_add_buffer(context, *pac, PAC_TYPE_LOGON_INFO, &pac_data);
118 kerberos_free_data_contents(context, &pac_data);
120 kerberos_free_data_contents(context, &deleg_data);
125 ret = krb5_pac_add_buffer(context, *pac,
126 PAC_TYPE_CONSTRAINED_DELEGATION,
128 kerberos_free_data_contents(context, &deleg_data);
137 bool samba_princ_needs_pac(struct samba_kdc_entry *skdc_entry)
140 uint32_t userAccountControl;
142 /* The service account may be set not to want the PAC */
143 userAccountControl = ldb_msg_find_attr_as_uint(skdc_entry->msg, "userAccountControl", 0);
144 if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
151 /* Was the krbtgt in this DB (ie, should we check the incoming signature) and was it an RODC */
152 int samba_krbtgt_is_in_db(struct samba_kdc_entry *p,
157 int rodc_krbtgt_number, trust_direction;
160 TALLOC_CTX *mem_ctx = talloc_new(NULL);
165 trust_direction = ldb_msg_find_attr_as_int(p->msg, "trustDirection", 0);
167 if (trust_direction != 0) {
168 /* Domain trust - we cannot check the sig, but we trust it for a correct PAC
170 This is exactly where we should flag for SID
171 validation when we do inter-foreest trusts
173 talloc_free(mem_ctx);
174 *is_untrusted = false;
179 /* The lack of password controls etc applies to krbtgt by
180 * virtue of being that particular RID */
181 status = dom_sid_split_rid(NULL, samdb_result_dom_sid(mem_ctx, p->msg, "objectSid"), NULL, &rid);
183 if (!NT_STATUS_IS_OK(status)) {
184 talloc_free(mem_ctx);
188 rodc_krbtgt_number = ldb_msg_find_attr_as_int(p->msg, "msDS-SecondaryKrbTgtNumber", -1);
190 if (p->kdc_db_ctx->my_krbtgt_number == 0) {
191 if (rid == DOMAIN_RID_KRBTGT) {
192 *is_untrusted = false;
194 talloc_free(mem_ctx);
196 } else if (rodc_krbtgt_number != -1) {
198 *is_untrusted = true;
199 talloc_free(mem_ctx);
202 } else if ((rid != DOMAIN_RID_KRBTGT) && (rodc_krbtgt_number == p->kdc_db_ctx->my_krbtgt_number)) {
203 talloc_free(mem_ctx);
204 *is_untrusted = false;
207 } else if (rid == DOMAIN_RID_KRBTGT) {
208 /* krbtgt viewed from an RODC */
209 talloc_free(mem_ctx);
210 *is_untrusted = false;
216 talloc_free(mem_ctx);
217 *is_untrusted = true;
222 NTSTATUS samba_kdc_get_pac_blob(TALLOC_CTX *mem_ctx,
223 struct samba_kdc_entry *p,
224 DATA_BLOB **_pac_blob)
226 struct auth_user_info_dc *user_info_dc;
230 /* The user account may be set not to want the PAC */
231 if ( ! samba_princ_needs_pac(p)) {
236 pac_blob = talloc_zero(mem_ctx, DATA_BLOB);
238 return NT_STATUS_NO_MEMORY;
241 nt_status = authsam_make_user_info_dc(mem_ctx, p->kdc_db_ctx->samdb,
242 lpcfg_netbios_name(p->kdc_db_ctx->lp_ctx),
243 lpcfg_sam_name(p->kdc_db_ctx->lp_ctx),
249 if (!NT_STATUS_IS_OK(nt_status)) {
250 DEBUG(0, ("Getting user info for PAC failed: %s\n",
251 nt_errstr(nt_status)));
255 nt_status = samba_get_logon_info_pac_blob(mem_ctx, user_info_dc, pac_blob);
256 if (!NT_STATUS_IS_OK(nt_status)) {
257 DEBUG(0, ("Building PAC failed: %s\n",
258 nt_errstr(nt_status)));
262 *_pac_blob = pac_blob;
266 NTSTATUS samba_kdc_update_pac_blob(TALLOC_CTX *mem_ctx,
267 krb5_context context,
268 const krb5_pac pac, DATA_BLOB *pac_blob,
269 struct PAC_SIGNATURE_DATA *pac_srv_sig,
270 struct PAC_SIGNATURE_DATA *pac_kdc_sig)
272 struct auth_user_info_dc *user_info_dc;
276 ret = kerberos_pac_to_user_info_dc(mem_ctx, pac,
277 context, &user_info_dc, pac_srv_sig, pac_kdc_sig);
279 return NT_STATUS_UNSUCCESSFUL;
282 nt_status = samba_get_logon_info_pac_blob(mem_ctx,
283 user_info_dc, pac_blob);
288 NTSTATUS samba_kdc_update_delegation_info_blob(TALLOC_CTX *mem_ctx,
289 krb5_context context,
291 const krb5_principal server_principal,
292 const krb5_principal proxy_principal,
299 enum ndr_err_code ndr_err;
301 struct PAC_CONSTRAINED_DELEGATION _d;
302 struct PAC_CONSTRAINED_DELEGATION *d = NULL;
306 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
308 if (tmp_ctx == NULL) {
309 return NT_STATUS_NO_MEMORY;
312 ret = krb5_pac_get_buffer(context, pac, PAC_TYPE_CONSTRAINED_DELEGATION, &old_data);
314 ZERO_STRUCT(old_data);
316 talloc_free(tmp_ctx);
317 return NT_STATUS_UNSUCCESSFUL;
320 old_blob.length = old_data.length;
321 old_blob.data = (uint8_t *)old_data.data;
324 if (old_blob.length > 0) {
325 ndr_err = ndr_pull_union_blob(&old_blob, mem_ctx,
326 &info, PAC_TYPE_CONSTRAINED_DELEGATION,
327 (ndr_pull_flags_fn_t)ndr_pull_PAC_INFO);
328 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
329 kerberos_free_data_contents(context, &old_data);
330 nt_status = ndr_map_error2ntstatus(ndr_err);
331 DEBUG(0,("can't parse the PAC LOGON_INFO: %s\n", nt_errstr(nt_status)));
332 talloc_free(tmp_ctx);
337 info.constrained_delegation.info = &_d;
339 kerberos_free_data_contents(context, &old_data);
341 ret = krb5_unparse_name(context, server_principal, &server);
343 talloc_free(tmp_ctx);
344 return NT_STATUS_INTERNAL_ERROR;
347 ret = krb5_unparse_name_flags(context, proxy_principal,
348 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &proxy);
351 talloc_free(tmp_ctx);
352 return NT_STATUS_INTERNAL_ERROR;
355 d = info.constrained_delegation.info;
356 i = d->num_transited_services;
357 d->proxy_target.string = server;
358 d->transited_services = talloc_realloc(mem_ctx, d->transited_services,
359 struct lsa_String, i + 1);
360 d->transited_services[i].string = proxy;
361 d->num_transited_services = i + 1;
363 ndr_err = ndr_push_union_blob(new_blob, mem_ctx,
364 &info, PAC_TYPE_CONSTRAINED_DELEGATION,
365 (ndr_push_flags_fn_t)ndr_push_PAC_INFO);
368 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
369 kerberos_free_data_contents(context, &old_data);
370 nt_status = ndr_map_error2ntstatus(ndr_err);
371 DEBUG(0,("can't parse the PAC LOGON_INFO: %s\n", nt_errstr(nt_status)));
372 talloc_free(tmp_ctx);
376 talloc_free(tmp_ctx);
380 /* function to map policy errors */
381 krb5_error_code samba_kdc_map_policy_err(NTSTATUS nt_status)
385 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_MUST_CHANGE))
386 ret = KRB5KDC_ERR_KEY_EXP;
387 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_EXPIRED))
388 ret = KRB5KDC_ERR_KEY_EXP;
389 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_EXPIRED))
390 ret = KRB5KDC_ERR_CLIENT_REVOKED;
391 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_DISABLED))
392 ret = KRB5KDC_ERR_CLIENT_REVOKED;
393 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_LOGON_HOURS))
394 ret = KRB5KDC_ERR_CLIENT_REVOKED;
395 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_LOCKED_OUT))
396 ret = KRB5KDC_ERR_CLIENT_REVOKED;
397 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_WORKSTATION))
398 ret = KRB5KDC_ERR_POLICY;
400 ret = KRB5KDC_ERR_POLICY;
405 /* Given a kdc entry, consult the account_ok routine in auth/auth_sam.c
407 NTSTATUS samba_kdc_check_client_access(struct samba_kdc_entry *kdc_entry,
408 const char *client_name,
409 const char *workstation,
410 bool password_change)
415 tmp_ctx = talloc_named(NULL, 0, "samba_kdc_check_client_access");
417 return NT_STATUS_NO_MEMORY;
420 /* we allow all kinds of trusts here */
421 nt_status = authsam_account_ok(tmp_ctx,
422 kdc_entry->kdc_db_ctx->samdb,
423 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
424 MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
425 kdc_entry->realm_dn, kdc_entry->msg,
426 workstation, client_name,
427 true, password_change);
429 talloc_free(tmp_ctx);