Merge tag 'samba-4.6.8' into v4-6-test
[Samba.git] / source4 / kdc / pac-glue.c
blob079030ee5e81fde3c985ea598dbaf40f56fddfb5
1 /*
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/>.
24 #include "includes.h"
25 #include "../libds/common/flags.h"
26 #include <ldb.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"
39 static
40 NTSTATUS samba_get_logon_info_pac_blob(TALLOC_CTX *mem_ctx,
41 const struct auth_user_info_dc *info,
42 DATA_BLOB *pac_data)
44 struct netr_SamInfo3 *info3;
45 union PAC_INFO pac_info;
46 enum ndr_err_code ndr_err;
47 NTSTATUS nt_status;
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)));
57 return 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,
68 PAC_TYPE_LOGON_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)));
74 return nt_status;
77 return NT_STATUS_OK;
80 static
81 NTSTATUS samba_get_upn_info_pac_blob(TALLOC_CTX *mem_ctx,
82 const struct auth_user_info_dc *info,
83 DATA_BLOB *upn_data)
85 union PAC_INFO pac_upn;
86 enum ndr_err_code ndr_err;
87 NTSTATUS nt_status;
89 ZERO_STRUCT(pac_upn);
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)));
110 return nt_status;
113 return NT_STATUS_OK;
116 static
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;
122 NTSTATUS nt_status;
123 struct samr_Password *lm_hash = NULL;
124 struct samr_Password *nt_hash = NULL;
125 struct PAC_CREDENTIAL_NTLM_SECPKG ntlm_secpkg = {
126 .version = 0,
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);
144 if (zero) {
145 lm_hash = NULL;
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);
159 if (zero) {
160 nt_hash = NULL;
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) {
172 return NT_STATUS_OK;
175 #ifdef DEBUG_PASSWORD
176 if (DEBUGLVL(11)) {
177 NDR_PRINT_DEBUG(PAC_CREDENTIAL_NTLM_SECPKG, &ntlm_secpkg);
179 #endif
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)));
188 return nt_status;
191 DEBUG(10, ("NTLM credential BLOB (len %zu) for user\n",
192 ntlm_blob.length));
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
204 if (DEBUGLVL(11)) {
205 NDR_PRINT_DEBUG(PAC_CREDENTIAL_DATA, &cred_data);
207 #endif
209 cred_ndr.ctr.data = &cred_data;
211 #ifdef DEBUG_PASSWORD
212 if (DEBUGLVL(11)) {
213 NDR_PRINT_DEBUG(PAC_CREDENTIAL_DATA_NDR, &cred_ndr);
215 #endif
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)));
224 return nt_status;
227 DEBUG(10, ("Created credential BLOB (len %zu) for user\n",
228 cred_blob->length));
229 dump_data_pw("PAC_CREDENTIAL_DATA_NDR",
230 cred_blob->data, cred_blob->length);
232 return NT_STATUS_OK;
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,
239 TALLOC_CTX *mem_ctx,
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, };
246 krb5_error_code ret;
247 const char *krb5err;
248 enum ndr_err_code ndr_err;
249 NTSTATUS nt_status;
251 *cred_info_blob = data_blob_null;
253 ret = krb5_crypto_init(context, pkreplykey, ETYPE_NULL,
254 &cred_crypto);
255 if (ret != 0) {
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);
259 return ret;
262 ret = krb5_crypto_getenctype(context, cred_crypto, &cred_enctype);
263 if (ret != 0) {
264 DEBUG(1, ("Failed getting crypto type for key\n"));
265 krb5_crypto_destroy(context, cred_crypto);
266 return ret;
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,
277 &cred_ndr_crypt);
278 krb5_crypto_destroy(context, cred_crypto);
279 if (ret != 0) {
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);
283 return ret;
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;
290 if (DEBUGLVL(10)) {
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);
309 return 0;
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,
315 TALLOC_CTX *mem_ctx,
316 DATA_BLOB *cred_info_blob)
318 krb5_key cred_key;
319 krb5_enctype cred_enctype;
320 struct PAC_CREDENTIAL_INFO pac_cred_info = { .version = 0, };
321 krb5_error_code code;
322 const char *krb5err;
323 enum ndr_err_code ndr_err;
324 NTSTATUS nt_status;
325 krb5_data cred_ndr_data;
326 krb5_enc_data cred_ndr_crypt;
327 size_t enc_len = 0;
329 *cred_info_blob = data_blob_null;
331 code = krb5_k_create_key(context,
332 pkreplykey,
333 &cred_key);
334 if (code != 0) {
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);
338 return code;
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,
355 cred_enctype,
356 cred_ndr_data.length,
357 &enc_len);
358 if (code != 0) {
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);
362 return code;
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");
368 return ENOMEM;
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,
375 cred_key,
376 KRB5_KU_OTHER_ENCRYPTED,
377 NULL,
378 &cred_ndr_data,
379 &cred_ndr_crypt);
380 krb5_k_free_key(context, cred_key);
381 if (code != 0) {
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);
385 return code;
388 if (DEBUGLVL(10)) {
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);
407 return 0;
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,
417 krb5_pac *pac)
419 krb5_data logon_data;
420 krb5_data cred_data;
421 krb5_data upn_data;
422 krb5_data deleg_data;
423 krb5_data null_data;
424 krb5_error_code ret;
426 ZERO_STRUCT(null_data);
428 /* The user account may be set not to want the PAC */
429 if (logon_blob == NULL) {
430 return 0;
433 ret = smb_krb5_copy_data_contents(&logon_data,
434 logon_blob->data,
435 logon_blob->length);
436 if (ret != 0) {
437 return ret;
440 ZERO_STRUCT(cred_data);
441 if (cred_blob != NULL) {
442 ret = smb_krb5_copy_data_contents(&cred_data,
443 cred_blob->data,
444 cred_blob->length);
445 if (ret != 0) {
446 smb_krb5_free_data_contents(context, &logon_data);
447 return ret;
451 ZERO_STRUCT(upn_data);
452 if (upn_blob != NULL) {
453 ret = smb_krb5_copy_data_contents(&upn_data,
454 upn_blob->data,
455 upn_blob->length);
456 if (ret != 0) {
457 smb_krb5_free_data_contents(context, &logon_data);
458 smb_krb5_free_data_contents(context, &cred_data);
459 return ret;
463 ZERO_STRUCT(deleg_data);
464 if (deleg_blob != NULL) {
465 ret = smb_krb5_copy_data_contents(&deleg_data,
466 deleg_blob->data,
467 deleg_blob->length);
468 if (ret != 0) {
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);
472 return ret;
476 ret = krb5_pac_init(context, pac);
477 if (ret != 0) {
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);
482 return ret;
485 ret = krb5_pac_add_buffer(context, *pac, PAC_TYPE_LOGON_INFO, &logon_data);
486 smb_krb5_free_data_contents(context, &logon_data);
487 if (ret != 0) {
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);
491 return ret;
494 if (cred_blob != NULL) {
495 ret = krb5_pac_add_buffer(context, *pac,
496 PAC_TYPE_CREDENTIAL_INFO,
497 &cred_data);
498 smb_krb5_free_data_contents(context, &cred_data);
499 if (ret != 0) {
500 smb_krb5_free_data_contents(context, &upn_data);
501 smb_krb5_free_data_contents(context, &deleg_data);
502 return ret;
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,
512 PAC_TYPE_LOGON_NAME,
513 &null_data);
514 if (ret != 0) {
515 smb_krb5_free_data_contents(context, &upn_data);
516 smb_krb5_free_data_contents(context, &deleg_data);
517 return ret;
520 if (upn_blob != NULL) {
521 ret = krb5_pac_add_buffer(context, *pac,
522 PAC_TYPE_UPN_DNS_INFO,
523 &upn_data);
524 smb_krb5_free_data_contents(context, &upn_data);
525 if (ret != 0) {
526 smb_krb5_free_data_contents(context, &deleg_data);
527 return ret;
531 if (deleg_blob != NULL) {
532 ret = krb5_pac_add_buffer(context, *pac,
533 PAC_TYPE_CONSTRAINED_DELEGATION,
534 &deleg_data);
535 smb_krb5_free_data_contents(context, &deleg_data);
536 if (ret != 0) {
537 return ret;
541 return ret;
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) {
552 return false;
555 return true;
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,
560 bool *is_in_db,
561 bool *is_untrusted)
563 NTSTATUS status;
564 int rodc_krbtgt_number, trust_direction;
565 uint32_t rid;
567 TALLOC_CTX *mem_ctx = talloc_new(NULL);
568 if (!mem_ctx) {
569 return ENOMEM;
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;
582 *is_in_db = false;
583 return 0;
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);
592 return EINVAL;
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;
600 *is_in_db = true;
601 talloc_free(mem_ctx);
602 return 0;
603 } else if (rodc_krbtgt_number != -1) {
604 *is_in_db = true;
605 *is_untrusted = true;
606 talloc_free(mem_ctx);
607 return 0;
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;
612 *is_in_db = true;
613 return 0;
614 } else if (rid == DOMAIN_RID_KRBTGT) {
615 /* krbtgt viewed from an RODC */
616 talloc_free(mem_ctx);
617 *is_untrusted = false;
618 *is_in_db = false;
619 return 0;
622 /* Another RODC */
623 talloc_free(mem_ctx);
624 *is_untrusted = true;
625 *is_in_db = false;
626 return 0;
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;
639 NTSTATUS nt_status;
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)) {
649 return NT_STATUS_OK;
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),
673 p->realm_dn,
674 p->msg,
675 data_blob(NULL, 0),
676 data_blob(NULL, 0),
677 &user_info_dc);
678 if (!NT_STATUS_IS_OK(nt_status)) {
679 DEBUG(0, ("Getting user info for PAC failed: %s\n",
680 nt_errstr(nt_status)));
681 return nt_status;
684 nt_status = samba_get_logon_info_pac_blob(logon_blob,
685 user_info_dc,
686 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)));
690 return nt_status;
693 if (cred_blob != NULL) {
694 nt_status = samba_get_cred_info_ndr_blob(cred_blob,
695 p->msg,
696 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)));
700 return nt_status;
704 nt_status = samba_get_upn_info_pac_blob(upn_blob,
705 user_info_dc,
706 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)));
710 return 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;
719 return NT_STATUS_OK;
722 NTSTATUS samba_kdc_get_pac_blob(TALLOC_CTX *mem_ctx,
723 struct samba_kdc_entry *p,
724 DATA_BLOB **_logon_info_blob)
726 NTSTATUS nt_status;
727 DATA_BLOB *upn_blob = NULL;
729 nt_status = samba_kdc_get_pac_blobs(mem_ctx, p,
730 _logon_info_blob,
731 NULL, /* cred_blob */
732 &upn_blob);
733 if (!NT_STATUS_IS_OK(nt_status)) {
734 return nt_status;
737 TALLOC_FREE(upn_blob);
738 return NT_STATUS_OK;
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;
748 krb5_error_code ret;
749 NTSTATUS nt_status;
751 ret = kerberos_pac_to_user_info_dc(mem_ctx, pac,
752 context, &user_info_dc, pac_srv_sig, pac_kdc_sig);
753 if (ret) {
754 return NT_STATUS_UNSUCCESSFUL;
757 nt_status = samba_get_logon_info_pac_blob(mem_ctx,
758 user_info_dc, pac_blob);
760 return nt_status;
763 NTSTATUS samba_kdc_update_delegation_info_blob(TALLOC_CTX *mem_ctx,
764 krb5_context context,
765 const krb5_pac pac,
766 const krb5_principal server_principal,
767 const krb5_principal proxy_principal,
768 DATA_BLOB *new_blob)
770 krb5_data old_data;
771 DATA_BLOB old_blob;
772 krb5_error_code ret;
773 NTSTATUS nt_status;
774 enum ndr_err_code ndr_err;
775 union PAC_INFO info;
776 struct PAC_CONSTRAINED_DELEGATION _d;
777 struct PAC_CONSTRAINED_DELEGATION *d = NULL;
778 char *server = NULL;
779 char *proxy = NULL;
780 uint32_t i;
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);
788 if (ret == ENOENT) {
789 ZERO_STRUCT(old_data);
790 } else if (ret) {
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;
798 ZERO_STRUCT(info);
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);
808 return nt_status;
810 } else {
811 ZERO_STRUCT(_d);
812 info.constrained_delegation.info = &_d;
814 smb_krb5_free_data_contents(context, &old_data);
816 ret = krb5_unparse_name(context, server_principal, &server);
817 if (ret) {
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);
824 if (ret) {
825 SAFE_FREE(server);
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);
841 SAFE_FREE(server);
842 SAFE_FREE(proxy);
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);
848 return nt_status;
851 talloc_free(tmp_ctx);
852 return NT_STATUS_OK;
855 /* function to map policy errors */
856 krb5_error_code samba_kdc_map_policy_err(NTSTATUS nt_status)
858 krb5_error_code ret;
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;
874 else
875 ret = KRB5KDC_ERR_POLICY;
877 return ret;
880 /* Given a kdc entry, consult the account_ok routine in auth/auth_sam.c
881 * for consistency */
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)
887 TALLOC_CTX *tmp_ctx;
888 NTSTATUS nt_status;
890 tmp_ctx = talloc_named(NULL, 0, "samba_kdc_check_client_access");
891 if (!tmp_ctx) {
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);
905 return nt_status;