2 Unix SMB/CIFS implementation.
4 Create and parse the krb5 PAC
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005,2008
7 Copyright (C) Andrew Tridgell 2001
8 Copyright (C) Luke Howard 2002-2003
9 Copyright (C) Stefan Metzmacher 2004-2005
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "system/kerberos.h"
28 #include "auth/auth.h"
29 #include "auth/kerberos/kerberos.h"
30 #include "librpc/gen_ndr/ndr_krb5pac.h"
32 #include "auth/auth_sam_reply.h"
33 #include "auth/credentials/credentials.h"
34 #include "auth/kerberos/kerberos_util.h"
35 #include "auth/kerberos/pac_utils.h"
37 krb5_error_code
kerberos_encode_pac(TALLOC_CTX
*mem_ctx
,
38 struct PAC_DATA
*pac_data
,
40 const krb5_keyblock
*krbtgt_keyblock
,
41 const krb5_keyblock
*service_keyblock
,
46 enum ndr_err_code ndr_err
;
47 DATA_BLOB zero_blob
= data_blob(NULL
, 0);
48 DATA_BLOB tmp_blob
= data_blob(NULL
, 0);
49 struct PAC_SIGNATURE_DATA
*kdc_checksum
= NULL
;
50 struct PAC_SIGNATURE_DATA
*srv_checksum
= NULL
;
53 /* First, just get the keytypes filled in (and lengths right, eventually) */
54 for (i
=0; i
< pac_data
->num_buffers
; i
++) {
55 if (pac_data
->buffers
[i
].type
!= PAC_TYPE_KDC_CHECKSUM
) {
58 kdc_checksum
= &pac_data
->buffers
[i
].info
->kdc_cksum
,
59 ret
= smb_krb5_make_pac_checksum(mem_ctx
,
64 &kdc_checksum
->signature
);
66 DEBUG(2, ("making krbtgt PAC checksum failed: %s\n",
67 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
68 talloc_free(pac_data
);
73 for (i
=0; i
< pac_data
->num_buffers
; i
++) {
74 if (pac_data
->buffers
[i
].type
!= PAC_TYPE_SRV_CHECKSUM
) {
77 srv_checksum
= &pac_data
->buffers
[i
].info
->srv_cksum
;
78 ret
= smb_krb5_make_pac_checksum(mem_ctx
,
83 &srv_checksum
->signature
);
85 DEBUG(2, ("making service PAC checksum failed: %s\n",
86 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
87 talloc_free(pac_data
);
93 DEBUG(2, ("Invalid PAC constructed for signing, no KDC checksum present!\n"));
97 DEBUG(2, ("Invalid PAC constructed for signing, no SRV checksum present!\n"));
101 /* But wipe out the actual signatures */
102 memset(kdc_checksum
->signature
.data
, '\0', kdc_checksum
->signature
.length
);
103 memset(srv_checksum
->signature
.data
, '\0', srv_checksum
->signature
.length
);
105 ndr_err
= ndr_push_struct_blob(&tmp_blob
, mem_ctx
,
107 (ndr_push_flags_fn_t
)ndr_push_PAC_DATA
);
108 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
109 nt_status
= ndr_map_error2ntstatus(ndr_err
);
110 DEBUG(1, ("PAC (presig) push failed: %s\n", nt_errstr(nt_status
)));
111 talloc_free(pac_data
);
115 /* Then sign the result of the previous push, where the sig was zero'ed out */
116 ret
= smb_krb5_make_pac_checksum(mem_ctx
,
121 &srv_checksum
->signature
);
124 DBG_WARNING("making krbtgt PAC srv_checksum failed: %s\n",
125 smb_get_krb5_error_message(context
, ret
, mem_ctx
));
126 talloc_free(pac_data
);
130 /* Then sign Server checksum */
131 ret
= smb_krb5_make_pac_checksum(mem_ctx
,
132 &srv_checksum
->signature
,
136 &kdc_checksum
->signature
);
138 DBG_WARNING("making krbtgt PAC kdc_checksum failed: %s\n",
139 smb_get_krb5_error_message(context
, ret
, mem_ctx
));
140 talloc_free(pac_data
);
144 /* And push it out again, this time to the world. This relies on deterministic pointer values */
145 ndr_err
= ndr_push_struct_blob(&tmp_blob
, mem_ctx
,
147 (ndr_push_flags_fn_t
)ndr_push_PAC_DATA
);
148 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
149 nt_status
= ndr_map_error2ntstatus(ndr_err
);
150 DEBUG(1, ("PAC (final) push failed: %s\n", nt_errstr(nt_status
)));
151 talloc_free(pac_data
);
161 krb5_error_code
kerberos_create_pac(TALLOC_CTX
*mem_ctx
,
162 struct auth_user_info_dc
*user_info_dc
,
163 krb5_context context
,
164 const krb5_keyblock
*krbtgt_keyblock
,
165 const krb5_keyblock
*service_keyblock
,
166 krb5_principal client_principal
,
172 struct PAC_DATA
*pac_data
= talloc(mem_ctx
, struct PAC_DATA
);
173 struct netr_SamInfo3
*sam3
;
174 union PAC_INFO
*u_LOGON_INFO
;
175 struct PAC_LOGON_INFO
*LOGON_INFO
;
176 union PAC_INFO
*u_LOGON_NAME
;
177 struct PAC_LOGON_NAME
*LOGON_NAME
;
178 union PAC_INFO
*u_KDC_CHECKSUM
;
179 union PAC_INFO
*u_SRV_CHECKSUM
;
184 PAC_BUF_LOGON_INFO
= 0,
185 PAC_BUF_LOGON_NAME
= 1,
186 PAC_BUF_SRV_CHECKSUM
= 2,
187 PAC_BUF_KDC_CHECKSUM
= 3,
188 PAC_BUF_NUM_BUFFERS
= 4
195 pac_data
->num_buffers
= PAC_BUF_NUM_BUFFERS
;
196 pac_data
->version
= 0;
198 pac_data
->buffers
= talloc_array(pac_data
,
200 pac_data
->num_buffers
);
201 if (!pac_data
->buffers
) {
202 talloc_free(pac_data
);
207 u_LOGON_INFO
= talloc_zero(pac_data
->buffers
, union PAC_INFO
);
209 talloc_free(pac_data
);
212 pac_data
->buffers
[PAC_BUF_LOGON_INFO
].type
= PAC_TYPE_LOGON_INFO
;
213 pac_data
->buffers
[PAC_BUF_LOGON_INFO
].info
= u_LOGON_INFO
;
216 u_LOGON_NAME
= talloc_zero(pac_data
->buffers
, union PAC_INFO
);
218 talloc_free(pac_data
);
221 pac_data
->buffers
[PAC_BUF_LOGON_NAME
].type
= PAC_TYPE_LOGON_NAME
;
222 pac_data
->buffers
[PAC_BUF_LOGON_NAME
].info
= u_LOGON_NAME
;
223 LOGON_NAME
= &u_LOGON_NAME
->logon_name
;
226 u_SRV_CHECKSUM
= talloc_zero(pac_data
->buffers
, union PAC_INFO
);
227 if (!u_SRV_CHECKSUM
) {
228 talloc_free(pac_data
);
231 pac_data
->buffers
[PAC_BUF_SRV_CHECKSUM
].type
= PAC_TYPE_SRV_CHECKSUM
;
232 pac_data
->buffers
[PAC_BUF_SRV_CHECKSUM
].info
= u_SRV_CHECKSUM
;
235 u_KDC_CHECKSUM
= talloc_zero(pac_data
->buffers
, union PAC_INFO
);
236 if (!u_KDC_CHECKSUM
) {
237 talloc_free(pac_data
);
240 pac_data
->buffers
[PAC_BUF_KDC_CHECKSUM
].type
= PAC_TYPE_KDC_CHECKSUM
;
241 pac_data
->buffers
[PAC_BUF_KDC_CHECKSUM
].info
= u_KDC_CHECKSUM
;
243 /* now the real work begins... */
245 LOGON_INFO
= talloc_zero(u_LOGON_INFO
, struct PAC_LOGON_INFO
);
247 talloc_free(pac_data
);
250 nt_status
= auth_convert_user_info_dc_saminfo3(LOGON_INFO
, user_info_dc
,
251 AUTH_INCLUDE_RESOURCE_GROUPS
,
253 if (!NT_STATUS_IS_OK(nt_status
)) {
254 DEBUG(1, ("Getting Samba info failed: %s\n", nt_errstr(nt_status
)));
255 talloc_free(pac_data
);
259 u_LOGON_INFO
->logon_info
.info
= LOGON_INFO
;
260 LOGON_INFO
->info3
= *sam3
;
262 ret
= krb5_unparse_name_flags(context
, client_principal
,
263 KRB5_PRINCIPAL_UNPARSE_NO_REALM
|
264 KRB5_PRINCIPAL_UNPARSE_DISPLAY
,
269 LOGON_NAME
->account_name
= talloc_strdup(LOGON_NAME
, name
);
272 this logon_time field is absolutely critical. This is what
273 caused all our PAC troubles :-)
275 unix_to_nt_time(&LOGON_NAME
->logon_time
, tgs_authtime
);
277 ret
= kerberos_encode_pac(mem_ctx
,
283 talloc_free(pac_data
);
287 static krb5_error_code
kerberos_pac_buffer_present(krb5_context context
,
288 const krb5_const_pac pac
,
291 #ifdef SAMBA4_USES_HEIMDAL
292 return krb5_pac_get_buffer(context
, pac
, type
, NULL
);
298 * MIT won't let us pass NULL for the data parameter, so we are forced
299 * to allocate a new buffer and then immediately free it.
301 ret
= krb5_pac_get_buffer(context
, pac
, type
, &data
);
303 krb5_free_data_contents(context
, &data
);
306 #endif /* SAMBA4_USES_HEIMDAL */
309 krb5_error_code
kerberos_pac_to_user_info_dc(TALLOC_CTX
*mem_ctx
,
311 krb5_context context
,
312 struct auth_user_info_dc
**user_info_dc
,
313 const enum auth_group_inclusion group_inclusion
,
314 struct PAC_SIGNATURE_DATA
*pac_srv_sig
,
315 struct PAC_SIGNATURE_DATA
*pac_kdc_sig
,
316 struct PAC_DOMAIN_GROUP_MEMBERSHIP
**resource_groups
)
319 enum ndr_err_code ndr_err
;
322 DATA_BLOB pac_logon_info_in
, pac_srv_checksum_in
, pac_kdc_checksum_in
;
323 krb5_data k5pac_logon_info_in
, k5pac_srv_checksum_in
, k5pac_kdc_checksum_in
;
324 DATA_BLOB pac_upn_dns_info_in
;
325 krb5_data k5pac_upn_dns_info_in
;
328 union PAC_INFO _upn_dns_info
;
329 struct PAC_UPN_DNS_INFO
*upn_dns_info
= NULL
;
330 struct auth_user_info_dc
*user_info_dc_out
;
331 const struct PAC_DOMAIN_GROUP_MEMBERSHIP
*resource_groups_in
= NULL
;
332 struct PAC_DOMAIN_GROUP_MEMBERSHIP
*resource_groups_out
= NULL
;
334 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
341 talloc_free(tmp_ctx
);
345 ret
= krb5_pac_get_buffer(context
, pac
, PAC_TYPE_LOGON_INFO
, &k5pac_logon_info_in
);
347 talloc_free(tmp_ctx
);
351 pac_logon_info_in
= data_blob_const(k5pac_logon_info_in
.data
, k5pac_logon_info_in
.length
);
353 ndr_err
= ndr_pull_union_blob(&pac_logon_info_in
, tmp_ctx
, &info
,
355 (ndr_pull_flags_fn_t
)ndr_pull_PAC_INFO
);
356 smb_krb5_free_data_contents(context
, &k5pac_logon_info_in
);
357 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
358 nt_status
= ndr_map_error2ntstatus(ndr_err
);
359 DEBUG(0,("can't parse the PAC LOGON_INFO: %s\n", nt_errstr(nt_status
)));
360 talloc_free(tmp_ctx
);
363 if (info
.logon_info
.info
== NULL
) {
364 DEBUG(0,("can't parse the PAC LOGON_INFO: missing info pointer\n"));
365 talloc_free(tmp_ctx
);
369 ret
= krb5_pac_get_buffer(context
, pac
, PAC_TYPE_UPN_DNS_INFO
,
370 &k5pac_upn_dns_info_in
);
372 ZERO_STRUCT(k5pac_upn_dns_info_in
);
376 talloc_free(tmp_ctx
);
380 pac_upn_dns_info_in
= data_blob_const(k5pac_upn_dns_info_in
.data
,
381 k5pac_upn_dns_info_in
.length
);
383 if (pac_upn_dns_info_in
.length
!= 0) {
384 ndr_err
= ndr_pull_union_blob(&pac_upn_dns_info_in
, tmp_ctx
,
386 PAC_TYPE_UPN_DNS_INFO
,
387 (ndr_pull_flags_fn_t
)ndr_pull_PAC_INFO
);
388 smb_krb5_free_data_contents(context
, &k5pac_upn_dns_info_in
);
389 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
390 nt_status
= ndr_map_error2ntstatus(ndr_err
);
391 DEBUG(0,("can't parse the PAC UPN_DNS_INFO: %s\n",
392 nt_errstr(nt_status
)));
393 talloc_free(tmp_ctx
);
396 upn_dns_info
= &_upn_dns_info
.upn_dns_info
;
399 /* Pull this right into the normal auth system structures */
400 nt_status
= make_user_info_dc_pac(tmp_ctx
,
401 info
.logon_info
.info
,
405 if (!NT_STATUS_IS_OK(nt_status
)) {
406 DBG_ERR("make_user_info_dc_pac() failed - %s\n",
407 nt_errstr(nt_status
));
408 NDR_PRINT_DEBUG(PAC_LOGON_INFO
, info
.logon_info
.info
);
409 if (upn_dns_info
!= NULL
) {
410 NDR_PRINT_DEBUG(PAC_UPN_DNS_INFO
, upn_dns_info
);
412 talloc_free(tmp_ctx
);
417 ret
= krb5_pac_get_buffer(context
, pac
, PAC_TYPE_SRV_CHECKSUM
, &k5pac_srv_checksum_in
);
419 talloc_free(tmp_ctx
);
423 pac_srv_checksum_in
= data_blob_const(k5pac_srv_checksum_in
.data
, k5pac_srv_checksum_in
.length
);
425 ndr_err
= ndr_pull_struct_blob(&pac_srv_checksum_in
, pac_srv_sig
,
427 (ndr_pull_flags_fn_t
)ndr_pull_PAC_SIGNATURE_DATA
);
428 smb_krb5_free_data_contents(context
, &k5pac_srv_checksum_in
);
429 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
430 nt_status
= ndr_map_error2ntstatus(ndr_err
);
431 DEBUG(0,("can't parse the server signature: %s\n",
432 nt_errstr(nt_status
)));
438 ret
= krb5_pac_get_buffer(context
, pac
, PAC_TYPE_KDC_CHECKSUM
, &k5pac_kdc_checksum_in
);
440 talloc_free(tmp_ctx
);
444 pac_kdc_checksum_in
= data_blob_const(k5pac_kdc_checksum_in
.data
, k5pac_kdc_checksum_in
.length
);
446 ndr_err
= ndr_pull_struct_blob(&pac_kdc_checksum_in
, pac_kdc_sig
,
448 (ndr_pull_flags_fn_t
)ndr_pull_PAC_SIGNATURE_DATA
);
449 smb_krb5_free_data_contents(context
, &k5pac_kdc_checksum_in
);
450 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
451 nt_status
= ndr_map_error2ntstatus(ndr_err
);
452 DEBUG(0,("can't parse the KDC signature: %s\n",
453 nt_errstr(nt_status
)));
459 * Based on the presence of a REQUESTER_SID PAC buffer, ascertain
460 * whether the ticket is a TGT. This helps the KDC and kpasswd service
461 * ensure they do not accept tickets meant for the other.
463 * This heuristic will fail for older Samba versions and Windows prior
464 * to Nov. 2021 updates, which lack support for the REQUESTER_SID PAC
467 ret
= kerberos_pac_buffer_present(context
, pac
, PAC_TYPE_REQUESTER_SID
);
469 /* This probably isn't a TGT. */
470 user_info_dc_out
->ticket_type
= TICKET_TYPE_NON_TGT
;
471 } else if (ret
!= 0) {
472 talloc_free(tmp_ctx
);
475 /* This probably is a TGT. */
476 user_info_dc_out
->ticket_type
= TICKET_TYPE_TGT
;
480 * If we have resource groups and the caller wants them returned, we
483 resource_groups_in
= &info
.logon_info
.info
->resource_groups
;
484 if (resource_groups
!= NULL
&& resource_groups_in
->groups
.count
!= 0) {
485 resource_groups_out
= talloc(tmp_ctx
, struct PAC_DOMAIN_GROUP_MEMBERSHIP
);
486 if (resource_groups_out
== NULL
) {
487 talloc_free(tmp_ctx
);
491 *resource_groups_out
= (struct PAC_DOMAIN_GROUP_MEMBERSHIP
) {
492 .domain_sid
= talloc_steal(resource_groups_out
, resource_groups_in
->domain_sid
),
494 .count
= resource_groups_in
->groups
.count
,
495 .rids
= talloc_steal(resource_groups_out
, resource_groups_in
->groups
.rids
),
500 *user_info_dc
= talloc_steal(mem_ctx
, user_info_dc_out
);
501 if (resource_groups_out
!= NULL
) {
502 *resource_groups
= talloc_steal(mem_ctx
, resource_groups_out
);
509 NTSTATUS
kerberos_pac_blob_to_user_info_dc(TALLOC_CTX
*mem_ctx
,
511 krb5_context context
,
512 struct auth_user_info_dc
**user_info_dc
,
513 struct PAC_SIGNATURE_DATA
*pac_srv_sig
,
514 struct PAC_SIGNATURE_DATA
*pac_kdc_sig
)
518 ret
= krb5_pac_parse(context
,
519 pac_blob
.data
, pac_blob
.length
,
522 return map_nt_error_from_unix_common(ret
);
526 ret
= kerberos_pac_to_user_info_dc(mem_ctx
,
530 AUTH_INCLUDE_RESOURCE_GROUPS
,
534 krb5_pac_free(context
, pac
);
536 return map_nt_error_from_unix_common(ret
);