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 const 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 *pac_data = data_blob_null;
53 nt_status = auth_convert_user_info_dc_saminfo3(mem_ctx, info, &info3);
54 if (!NT_STATUS_IS_OK(nt_status)) {
55 DEBUG(1, ("Getting Samba info failed: %s\n",
56 nt_errstr(nt_status)));
60 pac_info.logon_info.info = talloc_zero(mem_ctx, struct PAC_LOGON_INFO);
61 if (!pac_info.logon_info.info) {
62 return NT_STATUS_NO_MEMORY;
65 pac_info.logon_info.info->info3 = *info3;
67 ndr_err = ndr_push_union_blob(pac_data, mem_ctx, &pac_info,
69 (ndr_push_flags_fn_t)ndr_push_PAC_INFO);
70 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
71 nt_status = ndr_map_error2ntstatus(ndr_err);
72 DEBUG(1, ("PAC_LOGON_INFO (presig) push failed: %s\n",
73 nt_errstr(nt_status)));
81 NTSTATUS samba_get_upn_info_pac_blob(TALLOC_CTX *mem_ctx,
82 const struct auth_user_info_dc *info,
85 union PAC_INFO pac_upn;
86 enum ndr_err_code ndr_err;
91 *upn_data = data_blob_null;
93 pac_upn.upn_dns_info.upn_name = info->info->user_principal_name;
94 pac_upn.upn_dns_info.dns_domain_name = strupper_talloc(mem_ctx,
95 info->info->dns_domain_name);
96 if (pac_upn.upn_dns_info.dns_domain_name == NULL) {
97 return NT_STATUS_NO_MEMORY;
99 if (info->info->user_principal_constructed) {
100 pac_upn.upn_dns_info.flags |= PAC_UPN_DNS_FLAG_CONSTRUCTED;
103 ndr_err = ndr_push_union_blob(upn_data, mem_ctx, &pac_upn,
104 PAC_TYPE_UPN_DNS_INFO,
105 (ndr_push_flags_fn_t)ndr_push_PAC_INFO);
106 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
107 nt_status = ndr_map_error2ntstatus(ndr_err);
108 DEBUG(1, ("PAC UPN_DNS_INFO (presig) push failed: %s\n",
109 nt_errstr(nt_status)));
117 NTSTATUS samba_get_cred_info_ndr_blob(TALLOC_CTX *mem_ctx,
118 const struct ldb_message *msg,
119 DATA_BLOB *cred_blob)
121 enum ndr_err_code ndr_err;
123 struct samr_Password *lm_hash = NULL;
124 struct samr_Password *nt_hash = NULL;
125 struct PAC_CREDENTIAL_NTLM_SECPKG ntlm_secpkg = {
128 DATA_BLOB ntlm_blob = data_blob_null;
129 struct PAC_CREDENTIAL_SUPPLEMENTAL_SECPKG secpkgs[1] = {{
130 .credential_size = 0,
132 struct PAC_CREDENTIAL_DATA cred_data = {
133 .credential_count = 0,
135 struct PAC_CREDENTIAL_DATA_NDR cred_ndr;
137 ZERO_STRUCT(cred_ndr);
139 *cred_blob = data_blob_null;
141 lm_hash = samdb_result_hash(mem_ctx, msg, "dBCSPwd");
142 if (lm_hash != NULL) {
143 bool zero = all_zero(lm_hash->hash, 16);
148 if (lm_hash != NULL) {
149 DEBUG(5, ("Passing LM password hash through credentials set\n"));
150 ntlm_secpkg.flags |= PAC_CREDENTIAL_NTLM_HAS_LM_HASH;
151 ntlm_secpkg.lm_password = *lm_hash;
152 ZERO_STRUCTP(lm_hash);
153 TALLOC_FREE(lm_hash);
156 nt_hash = samdb_result_hash(mem_ctx, msg, "unicodePwd");
157 if (nt_hash != NULL) {
158 bool zero = all_zero(nt_hash->hash, 16);
163 if (nt_hash != NULL) {
164 DEBUG(5, ("Passing LM password hash through credentials set\n"));
165 ntlm_secpkg.flags |= PAC_CREDENTIAL_NTLM_HAS_NT_HASH;
166 ntlm_secpkg.nt_password = *nt_hash;
167 ZERO_STRUCTP(nt_hash);
168 TALLOC_FREE(nt_hash);
171 if (ntlm_secpkg.flags == 0) {
175 #ifdef DEBUG_PASSWORD
177 NDR_PRINT_DEBUG(PAC_CREDENTIAL_NTLM_SECPKG, &ntlm_secpkg);
181 ndr_err = ndr_push_struct_blob(&ntlm_blob, mem_ctx, &ntlm_secpkg,
182 (ndr_push_flags_fn_t)ndr_push_PAC_CREDENTIAL_NTLM_SECPKG);
183 ZERO_STRUCT(ntlm_secpkg);
184 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
185 nt_status = ndr_map_error2ntstatus(ndr_err);
186 DEBUG(1, ("PAC_CREDENTIAL_NTLM_SECPKG (presig) push failed: %s\n",
187 nt_errstr(nt_status)));
191 DEBUG(10, ("NTLM credential BLOB (len %zu) for user\n",
193 dump_data_pw("PAC_CREDENTIAL_NTLM_SECPKG",
194 ntlm_blob.data, ntlm_blob.length);
196 secpkgs[0].package_name.string = discard_const_p(char, "NTLM");
197 secpkgs[0].credential_size = ntlm_blob.length;
198 secpkgs[0].credential = ntlm_blob.data;
200 cred_data.credential_count = ARRAY_SIZE(secpkgs);
201 cred_data.credentials = secpkgs;
203 #ifdef DEBUG_PASSWORD
205 NDR_PRINT_DEBUG(PAC_CREDENTIAL_DATA, &cred_data);
209 cred_ndr.ctr.data = &cred_data;
211 #ifdef DEBUG_PASSWORD
213 NDR_PRINT_DEBUG(PAC_CREDENTIAL_DATA_NDR, &cred_ndr);
217 ndr_err = ndr_push_struct_blob(cred_blob, mem_ctx, &cred_ndr,
218 (ndr_push_flags_fn_t)ndr_push_PAC_CREDENTIAL_DATA_NDR);
219 data_blob_clear(&ntlm_blob);
220 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
221 nt_status = ndr_map_error2ntstatus(ndr_err);
222 DEBUG(1, ("PAC_CREDENTIAL_DATA_NDR (presig) push failed: %s\n",
223 nt_errstr(nt_status)));
227 DEBUG(10, ("Created credential BLOB (len %zu) for user\n",
229 dump_data_pw("PAC_CREDENTIAL_DATA_NDR",
230 cred_blob->data, cred_blob->length);
235 #ifdef SAMBA4_USES_HEIMDAL
236 krb5_error_code samba_kdc_encrypt_pac_credentials(krb5_context context,
237 const krb5_keyblock *pkreplykey,
238 const DATA_BLOB *cred_ndr_blob,
240 DATA_BLOB *cred_info_blob)
242 krb5_crypto cred_crypto;
243 krb5_enctype cred_enctype;
244 krb5_data cred_ndr_crypt;
245 struct PAC_CREDENTIAL_INFO pac_cred_info = { .version = 0, };
248 enum ndr_err_code ndr_err;
251 *cred_info_blob = data_blob_null;
253 ret = krb5_crypto_init(context, pkreplykey, ETYPE_NULL,
256 krb5err = krb5_get_error_message(context, ret);
257 DEBUG(1, ("Failed initializing cred data crypto: %s\n", krb5err));
258 krb5_free_error_message(context, krb5err);
262 ret = krb5_crypto_getenctype(context, cred_crypto, &cred_enctype);
264 DEBUG(1, ("Failed getting crypto type for key\n"));
265 krb5_crypto_destroy(context, cred_crypto);
269 DEBUG(10, ("Plain cred_ndr_blob (len %zu)\n",
270 cred_ndr_blob->length));
271 dump_data_pw("PAC_CREDENTIAL_DATA_NDR",
272 cred_ndr_blob->data, cred_ndr_blob->length);
274 ret = krb5_encrypt(context, cred_crypto,
275 KRB5_KU_OTHER_ENCRYPTED,
276 cred_ndr_blob->data, cred_ndr_blob->length,
278 krb5_crypto_destroy(context, cred_crypto);
280 krb5err = krb5_get_error_message(context, ret);
281 DEBUG(1, ("Failed crypt of cred data: %s\n", krb5err));
282 krb5_free_error_message(context, krb5err);
286 pac_cred_info.encryption_type = cred_enctype;
287 pac_cred_info.encrypted_data.length = cred_ndr_crypt.length;
288 pac_cred_info.encrypted_data.data = (uint8_t *)cred_ndr_crypt.data;
291 NDR_PRINT_DEBUG(PAC_CREDENTIAL_INFO, &pac_cred_info);
294 ndr_err = ndr_push_struct_blob(cred_info_blob, mem_ctx, &pac_cred_info,
295 (ndr_push_flags_fn_t)ndr_push_PAC_CREDENTIAL_INFO);
296 krb5_data_free(&cred_ndr_crypt);
297 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
298 nt_status = ndr_map_error2ntstatus(ndr_err);
299 DEBUG(1, ("PAC_CREDENTIAL_INFO (presig) push failed: %s\n",
300 nt_errstr(nt_status)));
301 return KRB5KDC_ERR_SVC_UNAVAILABLE;
304 DEBUG(10, ("Encrypted credential BLOB (len %zu) with alg %d\n",
305 cred_info_blob->length, (int)pac_cred_info.encryption_type));
306 dump_data_pw("PAC_CREDENTIAL_INFO",
307 cred_info_blob->data, cred_info_blob->length);
311 #else /* SAMBA4_USES_HEIMDAL */
312 krb5_error_code samba_kdc_encrypt_pac_credentials(krb5_context context,
313 const krb5_keyblock *pkreplykey,
314 const DATA_BLOB *cred_ndr_blob,
316 DATA_BLOB *cred_info_blob)
319 krb5_enctype cred_enctype;
320 struct PAC_CREDENTIAL_INFO pac_cred_info = { .version = 0, };
321 krb5_error_code code;
323 enum ndr_err_code ndr_err;
325 krb5_data cred_ndr_data;
326 krb5_enc_data cred_ndr_crypt;
329 *cred_info_blob = data_blob_null;
331 code = krb5_k_create_key(context,
335 krb5err = krb5_get_error_message(context, code);
336 DEBUG(1, ("Failed initializing cred data crypto: %s\n", krb5err));
337 krb5_free_error_message(context, krb5err);
341 cred_enctype = krb5_k_key_enctype(context, cred_key);
343 DEBUG(10, ("Plain cred_ndr_blob (len %zu)\n",
344 cred_ndr_blob->length));
345 dump_data_pw("PAC_CREDENTIAL_DATA_NDR",
346 cred_ndr_blob->data, cred_ndr_blob->length);
348 pac_cred_info.encryption_type = cred_enctype;
350 cred_ndr_data.magic = 0;
351 cred_ndr_data.data = (char *)cred_ndr_blob->data;
352 cred_ndr_data.length = cred_ndr_blob->length;
354 code = krb5_c_encrypt_length(context,
356 cred_ndr_data.length,
359 krb5err = krb5_get_error_message(context, code);
360 DEBUG(1, ("Failed initializing cred data crypto: %s\n", krb5err));
361 krb5_free_error_message(context, krb5err);
365 pac_cred_info.encrypted_data = data_blob_talloc_zero(mem_ctx, enc_len);
366 if (pac_cred_info.encrypted_data.data == NULL) {
367 DBG_ERR("Out of memory\n");
371 cred_ndr_crypt.ciphertext.length = enc_len;
372 cred_ndr_crypt.ciphertext.data = (char *)pac_cred_info.encrypted_data.data;
374 code = krb5_k_encrypt(context,
376 KRB5_KU_OTHER_ENCRYPTED,
380 krb5_k_free_key(context, cred_key);
382 krb5err = krb5_get_error_message(context, code);
383 DEBUG(1, ("Failed crypt of cred data: %s\n", krb5err));
384 krb5_free_error_message(context, krb5err);
389 NDR_PRINT_DEBUG(PAC_CREDENTIAL_INFO, &pac_cred_info);
392 ndr_err = ndr_push_struct_blob(cred_info_blob, mem_ctx, &pac_cred_info,
393 (ndr_push_flags_fn_t)ndr_push_PAC_CREDENTIAL_INFO);
394 TALLOC_FREE(pac_cred_info.encrypted_data.data);
395 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
396 nt_status = ndr_map_error2ntstatus(ndr_err);
397 DEBUG(1, ("PAC_CREDENTIAL_INFO (presig) push failed: %s\n",
398 nt_errstr(nt_status)));
399 return KRB5KDC_ERR_SVC_UNAVAILABLE;
402 DEBUG(10, ("Encrypted credential BLOB (len %zu) with alg %d\n",
403 cred_info_blob->length, (int)pac_cred_info.encryption_type));
404 dump_data_pw("PAC_CREDENTIAL_INFO",
405 cred_info_blob->data, cred_info_blob->length);
409 #endif /* SAMBA4_USES_HEIMDAL */
412 krb5_error_code samba_make_krb5_pac(krb5_context context,
413 const DATA_BLOB *logon_blob,
414 const DATA_BLOB *cred_blob,
415 const DATA_BLOB *upn_blob,
416 const DATA_BLOB *deleg_blob,
419 krb5_data logon_data;
422 krb5_data deleg_data;
426 ZERO_STRUCT(null_data);
428 /* The user account may be set not to want the PAC */
429 if (logon_blob == NULL) {
433 ret = smb_krb5_copy_data_contents(&logon_data,
440 ZERO_STRUCT(cred_data);
441 if (cred_blob != NULL) {
442 ret = smb_krb5_copy_data_contents(&cred_data,
446 smb_krb5_free_data_contents(context, &logon_data);
451 ZERO_STRUCT(upn_data);
452 if (upn_blob != NULL) {
453 ret = smb_krb5_copy_data_contents(&upn_data,
457 smb_krb5_free_data_contents(context, &logon_data);
458 smb_krb5_free_data_contents(context, &cred_data);
463 ZERO_STRUCT(deleg_data);
464 if (deleg_blob != NULL) {
465 ret = smb_krb5_copy_data_contents(&deleg_data,
469 smb_krb5_free_data_contents(context, &logon_data);
470 smb_krb5_free_data_contents(context, &cred_data);
471 smb_krb5_free_data_contents(context, &upn_data);
476 ret = krb5_pac_init(context, pac);
478 smb_krb5_free_data_contents(context, &logon_data);
479 smb_krb5_free_data_contents(context, &cred_data);
480 smb_krb5_free_data_contents(context, &upn_data);
481 smb_krb5_free_data_contents(context, &deleg_data);
485 ret = krb5_pac_add_buffer(context, *pac, PAC_TYPE_LOGON_INFO, &logon_data);
486 smb_krb5_free_data_contents(context, &logon_data);
488 smb_krb5_free_data_contents(context, &upn_data);
489 smb_krb5_free_data_contents(context, &cred_data);
490 smb_krb5_free_data_contents(context, &deleg_data);
494 if (cred_blob != NULL) {
495 ret = krb5_pac_add_buffer(context, *pac,
496 PAC_TYPE_CREDENTIAL_INFO,
498 smb_krb5_free_data_contents(context, &cred_data);
500 smb_krb5_free_data_contents(context, &upn_data);
501 smb_krb5_free_data_contents(context, &deleg_data);
507 * null_data will be filled by the generic KDC code in the caller
508 * here we just add it in order to have it before
509 * PAC_TYPE_UPN_DNS_INFO
511 ret = krb5_pac_add_buffer(context, *pac,
515 smb_krb5_free_data_contents(context, &upn_data);
516 smb_krb5_free_data_contents(context, &deleg_data);
520 if (upn_blob != NULL) {
521 ret = krb5_pac_add_buffer(context, *pac,
522 PAC_TYPE_UPN_DNS_INFO,
524 smb_krb5_free_data_contents(context, &upn_data);
526 smb_krb5_free_data_contents(context, &deleg_data);
531 if (deleg_blob != NULL) {
532 ret = krb5_pac_add_buffer(context, *pac,
533 PAC_TYPE_CONSTRAINED_DELEGATION,
535 smb_krb5_free_data_contents(context, &deleg_data);
544 bool samba_princ_needs_pac(struct samba_kdc_entry *skdc_entry)
547 uint32_t userAccountControl;
549 /* The service account may be set not to want the PAC */
550 userAccountControl = ldb_msg_find_attr_as_uint(skdc_entry->msg, "userAccountControl", 0);
551 if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
558 /* Was the krbtgt in this DB (ie, should we check the incoming signature) and was it an RODC */
559 int samba_krbtgt_is_in_db(struct samba_kdc_entry *p,
564 int rodc_krbtgt_number, trust_direction;
567 TALLOC_CTX *mem_ctx = talloc_new(NULL);
572 trust_direction = ldb_msg_find_attr_as_int(p->msg, "trustDirection", 0);
574 if (trust_direction != 0) {
575 /* Domain trust - we cannot check the sig, but we trust it for a correct PAC
577 This is exactly where we should flag for SID
578 validation when we do inter-foreest trusts
580 talloc_free(mem_ctx);
581 *is_untrusted = false;
586 /* The lack of password controls etc applies to krbtgt by
587 * virtue of being that particular RID */
588 status = dom_sid_split_rid(NULL, samdb_result_dom_sid(mem_ctx, p->msg, "objectSid"), NULL, &rid);
590 if (!NT_STATUS_IS_OK(status)) {
591 talloc_free(mem_ctx);
595 rodc_krbtgt_number = ldb_msg_find_attr_as_int(p->msg, "msDS-SecondaryKrbTgtNumber", -1);
597 if (p->kdc_db_ctx->my_krbtgt_number == 0) {
598 if (rid == DOMAIN_RID_KRBTGT) {
599 *is_untrusted = false;
601 talloc_free(mem_ctx);
603 } else if (rodc_krbtgt_number != -1) {
605 *is_untrusted = true;
606 talloc_free(mem_ctx);
609 } else if ((rid != DOMAIN_RID_KRBTGT) && (rodc_krbtgt_number == p->kdc_db_ctx->my_krbtgt_number)) {
610 talloc_free(mem_ctx);
611 *is_untrusted = false;
614 } else if (rid == DOMAIN_RID_KRBTGT) {
615 /* krbtgt viewed from an RODC */
616 talloc_free(mem_ctx);
617 *is_untrusted = false;
623 talloc_free(mem_ctx);
624 *is_untrusted = true;
629 NTSTATUS samba_kdc_get_pac_blobs(TALLOC_CTX *mem_ctx,
630 struct samba_kdc_entry *p,
631 DATA_BLOB **_logon_info_blob,
632 DATA_BLOB **_cred_ndr_blob,
633 DATA_BLOB **_upn_info_blob)
635 struct auth_user_info_dc *user_info_dc;
636 DATA_BLOB *logon_blob = NULL;
637 DATA_BLOB *cred_blob = NULL;
638 DATA_BLOB *upn_blob = NULL;
641 *_logon_info_blob = NULL;
642 if (_cred_ndr_blob != NULL) {
643 *_cred_ndr_blob = NULL;
645 *_upn_info_blob = NULL;
647 /* The user account may be set not to want the PAC */
648 if ( ! samba_princ_needs_pac(p)) {
652 logon_blob = talloc_zero(mem_ctx, DATA_BLOB);
653 if (logon_blob == NULL) {
654 return NT_STATUS_NO_MEMORY;
657 if (_cred_ndr_blob != NULL) {
658 cred_blob = talloc_zero(mem_ctx, DATA_BLOB);
659 if (cred_blob == NULL) {
660 return NT_STATUS_NO_MEMORY;
664 upn_blob = talloc_zero(mem_ctx, DATA_BLOB);
665 if (upn_blob == NULL) {
666 return NT_STATUS_NO_MEMORY;
669 nt_status = authsam_make_user_info_dc(mem_ctx, p->kdc_db_ctx->samdb,
670 lpcfg_netbios_name(p->kdc_db_ctx->lp_ctx),
671 lpcfg_sam_name(p->kdc_db_ctx->lp_ctx),
672 lpcfg_sam_dnsname(p->kdc_db_ctx->lp_ctx),
678 if (!NT_STATUS_IS_OK(nt_status)) {
679 DEBUG(0, ("Getting user info for PAC failed: %s\n",
680 nt_errstr(nt_status)));
684 nt_status = samba_get_logon_info_pac_blob(logon_blob,
687 if (!NT_STATUS_IS_OK(nt_status)) {
688 DEBUG(0, ("Building PAC LOGON INFO failed: %s\n",
689 nt_errstr(nt_status)));
693 if (cred_blob != NULL) {
694 nt_status = samba_get_cred_info_ndr_blob(cred_blob,
697 if (!NT_STATUS_IS_OK(nt_status)) {
698 DEBUG(0, ("Building PAC CRED INFO failed: %s\n",
699 nt_errstr(nt_status)));
704 nt_status = samba_get_upn_info_pac_blob(upn_blob,
707 if (!NT_STATUS_IS_OK(nt_status)) {
708 DEBUG(0, ("Building PAC UPN INFO failed: %s\n",
709 nt_errstr(nt_status)));
713 TALLOC_FREE(user_info_dc);
714 *_logon_info_blob = logon_blob;
715 if (_cred_ndr_blob != NULL) {
716 *_cred_ndr_blob = cred_blob;
718 *_upn_info_blob = upn_blob;
722 NTSTATUS samba_kdc_get_pac_blob(TALLOC_CTX *mem_ctx,
723 struct samba_kdc_entry *p,
724 DATA_BLOB **_logon_info_blob)
727 DATA_BLOB *upn_blob = NULL;
729 nt_status = samba_kdc_get_pac_blobs(mem_ctx, p,
731 NULL, /* cred_blob */
733 if (!NT_STATUS_IS_OK(nt_status)) {
737 TALLOC_FREE(upn_blob);
741 NTSTATUS samba_kdc_update_pac_blob(TALLOC_CTX *mem_ctx,
742 krb5_context context,
743 const krb5_pac pac, DATA_BLOB *pac_blob,
744 struct PAC_SIGNATURE_DATA *pac_srv_sig,
745 struct PAC_SIGNATURE_DATA *pac_kdc_sig)
747 struct auth_user_info_dc *user_info_dc;
751 ret = kerberos_pac_to_user_info_dc(mem_ctx, pac,
752 context, &user_info_dc, pac_srv_sig, pac_kdc_sig);
754 return NT_STATUS_UNSUCCESSFUL;
757 nt_status = samba_get_logon_info_pac_blob(mem_ctx,
758 user_info_dc, pac_blob);
763 NTSTATUS samba_kdc_update_delegation_info_blob(TALLOC_CTX *mem_ctx,
764 krb5_context context,
766 const krb5_principal server_principal,
767 const krb5_principal proxy_principal,
774 enum ndr_err_code ndr_err;
776 struct PAC_CONSTRAINED_DELEGATION _d;
777 struct PAC_CONSTRAINED_DELEGATION *d = NULL;
781 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
783 if (tmp_ctx == NULL) {
784 return NT_STATUS_NO_MEMORY;
787 ret = krb5_pac_get_buffer(context, pac, PAC_TYPE_CONSTRAINED_DELEGATION, &old_data);
789 ZERO_STRUCT(old_data);
791 talloc_free(tmp_ctx);
792 return NT_STATUS_UNSUCCESSFUL;
795 old_blob.length = old_data.length;
796 old_blob.data = (uint8_t *)old_data.data;
799 if (old_blob.length > 0) {
800 ndr_err = ndr_pull_union_blob(&old_blob, mem_ctx,
801 &info, PAC_TYPE_CONSTRAINED_DELEGATION,
802 (ndr_pull_flags_fn_t)ndr_pull_PAC_INFO);
803 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
804 smb_krb5_free_data_contents(context, &old_data);
805 nt_status = ndr_map_error2ntstatus(ndr_err);
806 DEBUG(0,("can't parse the PAC LOGON_INFO: %s\n", nt_errstr(nt_status)));
807 talloc_free(tmp_ctx);
812 info.constrained_delegation.info = &_d;
814 smb_krb5_free_data_contents(context, &old_data);
816 ret = krb5_unparse_name(context, server_principal, &server);
818 talloc_free(tmp_ctx);
819 return NT_STATUS_INTERNAL_ERROR;
822 ret = krb5_unparse_name_flags(context, proxy_principal,
823 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &proxy);
826 talloc_free(tmp_ctx);
827 return NT_STATUS_INTERNAL_ERROR;
830 d = info.constrained_delegation.info;
831 i = d->num_transited_services;
832 d->proxy_target.string = server;
833 d->transited_services = talloc_realloc(mem_ctx, d->transited_services,
834 struct lsa_String, i + 1);
835 d->transited_services[i].string = proxy;
836 d->num_transited_services = i + 1;
838 ndr_err = ndr_push_union_blob(new_blob, mem_ctx,
839 &info, PAC_TYPE_CONSTRAINED_DELEGATION,
840 (ndr_push_flags_fn_t)ndr_push_PAC_INFO);
843 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
844 smb_krb5_free_data_contents(context, &old_data);
845 nt_status = ndr_map_error2ntstatus(ndr_err);
846 DEBUG(0,("can't parse the PAC LOGON_INFO: %s\n", nt_errstr(nt_status)));
847 talloc_free(tmp_ctx);
851 talloc_free(tmp_ctx);
855 /* function to map policy errors */
856 krb5_error_code samba_kdc_map_policy_err(NTSTATUS nt_status)
860 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_MUST_CHANGE))
861 ret = KRB5KDC_ERR_KEY_EXP;
862 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_EXPIRED))
863 ret = KRB5KDC_ERR_KEY_EXP;
864 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_EXPIRED))
865 ret = KRB5KDC_ERR_CLIENT_REVOKED;
866 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_DISABLED))
867 ret = KRB5KDC_ERR_CLIENT_REVOKED;
868 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_LOGON_HOURS))
869 ret = KRB5KDC_ERR_CLIENT_REVOKED;
870 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_LOCKED_OUT))
871 ret = KRB5KDC_ERR_CLIENT_REVOKED;
872 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_WORKSTATION))
873 ret = KRB5KDC_ERR_POLICY;
875 ret = KRB5KDC_ERR_POLICY;
880 /* Given a kdc entry, consult the account_ok routine in auth/auth_sam.c
882 NTSTATUS samba_kdc_check_client_access(struct samba_kdc_entry *kdc_entry,
883 const char *client_name,
884 const char *workstation,
885 bool password_change)
890 tmp_ctx = talloc_named(NULL, 0, "samba_kdc_check_client_access");
892 return NT_STATUS_NO_MEMORY;
895 /* we allow all kinds of trusts here */
896 nt_status = authsam_account_ok(tmp_ctx,
897 kdc_entry->kdc_db_ctx->samdb,
898 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
899 MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
900 kdc_entry->realm_dn, kdc_entry->msg,
901 workstation, client_name,
902 true, password_change);
904 talloc_free(tmp_ctx);