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/kerberos/kerberos_util.h"
34 #include "auth/kerberos/pac_utils.h"
36 _PUBLIC_ NTSTATUS
kerberos_pac_logon_info(TALLOC_CTX
*mem_ctx
,
39 const krb5_keyblock
*krbtgt_keyblock
,
40 const krb5_keyblock
*service_keyblock
,
41 krb5_const_principal client_principal
,
43 struct PAC_LOGON_INFO
**logon_info
)
46 struct PAC_DATA
*pac_data
;
48 nt_status
= kerberos_decode_pac(mem_ctx
,
56 if (!NT_STATUS_IS_OK(nt_status
)) {
61 for (i
=0; i
< pac_data
->num_buffers
; i
++) {
62 if (pac_data
->buffers
[i
].type
!= PAC_TYPE_LOGON_INFO
) {
65 *logon_info
= pac_data
->buffers
[i
].info
->logon_info
.info
;
68 return NT_STATUS_INVALID_PARAMETER
;
73 krb5_error_code
kerberos_encode_pac(TALLOC_CTX
*mem_ctx
,
74 struct PAC_DATA
*pac_data
,
76 const krb5_keyblock
*krbtgt_keyblock
,
77 const krb5_keyblock
*service_keyblock
,
82 enum ndr_err_code ndr_err
;
83 DATA_BLOB zero_blob
= data_blob(NULL
, 0);
84 DATA_BLOB tmp_blob
= data_blob(NULL
, 0);
85 struct PAC_SIGNATURE_DATA
*kdc_checksum
= NULL
;
86 struct PAC_SIGNATURE_DATA
*srv_checksum
= NULL
;
89 /* First, just get the keytypes filled in (and lengths right, eventually) */
90 for (i
=0; i
< pac_data
->num_buffers
; i
++) {
91 if (pac_data
->buffers
[i
].type
!= PAC_TYPE_KDC_CHECKSUM
) {
94 kdc_checksum
= &pac_data
->buffers
[i
].info
->kdc_cksum
,
95 ret
= smb_krb5_make_pac_checksum(mem_ctx
,
100 &kdc_checksum
->signature
);
102 DEBUG(2, ("making krbtgt PAC checksum failed: %s\n",
103 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
104 talloc_free(pac_data
);
109 for (i
=0; i
< pac_data
->num_buffers
; i
++) {
110 if (pac_data
->buffers
[i
].type
!= PAC_TYPE_SRV_CHECKSUM
) {
113 srv_checksum
= &pac_data
->buffers
[i
].info
->srv_cksum
;
114 ret
= smb_krb5_make_pac_checksum(mem_ctx
,
119 &srv_checksum
->signature
);
121 DEBUG(2, ("making service PAC checksum failed: %s\n",
122 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
123 talloc_free(pac_data
);
129 DEBUG(2, ("Invalid PAC constructed for signing, no KDC checksum present!"));
133 DEBUG(2, ("Invalid PAC constructed for signing, no SRV checksum present!"));
137 /* But wipe out the actual signatures */
138 memset(kdc_checksum
->signature
.data
, '\0', kdc_checksum
->signature
.length
);
139 memset(srv_checksum
->signature
.data
, '\0', srv_checksum
->signature
.length
);
141 ndr_err
= ndr_push_struct_blob(&tmp_blob
, mem_ctx
,
143 (ndr_push_flags_fn_t
)ndr_push_PAC_DATA
);
144 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
145 nt_status
= ndr_map_error2ntstatus(ndr_err
);
146 DEBUG(1, ("PAC (presig) push failed: %s\n", nt_errstr(nt_status
)));
147 talloc_free(pac_data
);
151 /* Then sign the result of the previous push, where the sig was zero'ed out */
152 ret
= smb_krb5_make_pac_checksum(mem_ctx
,
157 &srv_checksum
->signature
);
159 /* Then sign Server checksum */
160 ret
= smb_krb5_make_pac_checksum(mem_ctx
,
161 &srv_checksum
->signature
,
165 &kdc_checksum
->signature
);
167 DEBUG(2, ("making krbtgt PAC checksum failed: %s\n",
168 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
169 talloc_free(pac_data
);
173 /* And push it out again, this time to the world. This relies on determanistic pointer values */
174 ndr_err
= ndr_push_struct_blob(&tmp_blob
, mem_ctx
,
176 (ndr_push_flags_fn_t
)ndr_push_PAC_DATA
);
177 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
178 nt_status
= ndr_map_error2ntstatus(ndr_err
);
179 DEBUG(1, ("PAC (final) push failed: %s\n", nt_errstr(nt_status
)));
180 talloc_free(pac_data
);
190 krb5_error_code
kerberos_create_pac(TALLOC_CTX
*mem_ctx
,
191 struct auth_user_info_dc
*user_info_dc
,
192 krb5_context context
,
193 const krb5_keyblock
*krbtgt_keyblock
,
194 const krb5_keyblock
*service_keyblock
,
195 krb5_principal client_principal
,
201 struct PAC_DATA
*pac_data
= talloc(mem_ctx
, struct PAC_DATA
);
202 struct netr_SamInfo3
*sam3
;
203 union PAC_INFO
*u_LOGON_INFO
;
204 struct PAC_LOGON_INFO
*LOGON_INFO
;
205 union PAC_INFO
*u_LOGON_NAME
;
206 struct PAC_LOGON_NAME
*LOGON_NAME
;
207 union PAC_INFO
*u_KDC_CHECKSUM
;
208 union PAC_INFO
*u_SRV_CHECKSUM
;
213 PAC_BUF_LOGON_INFO
= 0,
214 PAC_BUF_LOGON_NAME
= 1,
215 PAC_BUF_SRV_CHECKSUM
= 2,
216 PAC_BUF_KDC_CHECKSUM
= 3,
217 PAC_BUF_NUM_BUFFERS
= 4
224 pac_data
->num_buffers
= PAC_BUF_NUM_BUFFERS
;
225 pac_data
->version
= 0;
227 pac_data
->buffers
= talloc_array(pac_data
,
229 pac_data
->num_buffers
);
230 if (!pac_data
->buffers
) {
231 talloc_free(pac_data
);
236 u_LOGON_INFO
= talloc_zero(pac_data
->buffers
, union PAC_INFO
);
238 talloc_free(pac_data
);
241 pac_data
->buffers
[PAC_BUF_LOGON_INFO
].type
= PAC_TYPE_LOGON_INFO
;
242 pac_data
->buffers
[PAC_BUF_LOGON_INFO
].info
= u_LOGON_INFO
;
245 u_LOGON_NAME
= talloc_zero(pac_data
->buffers
, union PAC_INFO
);
247 talloc_free(pac_data
);
250 pac_data
->buffers
[PAC_BUF_LOGON_NAME
].type
= PAC_TYPE_LOGON_NAME
;
251 pac_data
->buffers
[PAC_BUF_LOGON_NAME
].info
= u_LOGON_NAME
;
252 LOGON_NAME
= &u_LOGON_NAME
->logon_name
;
255 u_SRV_CHECKSUM
= talloc_zero(pac_data
->buffers
, union PAC_INFO
);
256 if (!u_SRV_CHECKSUM
) {
257 talloc_free(pac_data
);
260 pac_data
->buffers
[PAC_BUF_SRV_CHECKSUM
].type
= PAC_TYPE_SRV_CHECKSUM
;
261 pac_data
->buffers
[PAC_BUF_SRV_CHECKSUM
].info
= u_SRV_CHECKSUM
;
264 u_KDC_CHECKSUM
= talloc_zero(pac_data
->buffers
, union PAC_INFO
);
265 if (!u_KDC_CHECKSUM
) {
266 talloc_free(pac_data
);
269 pac_data
->buffers
[PAC_BUF_KDC_CHECKSUM
].type
= PAC_TYPE_KDC_CHECKSUM
;
270 pac_data
->buffers
[PAC_BUF_KDC_CHECKSUM
].info
= u_KDC_CHECKSUM
;
272 /* now the real work begins... */
274 LOGON_INFO
= talloc_zero(u_LOGON_INFO
, struct PAC_LOGON_INFO
);
276 talloc_free(pac_data
);
279 nt_status
= auth_convert_user_info_dc_saminfo3(LOGON_INFO
, user_info_dc
, &sam3
);
280 if (!NT_STATUS_IS_OK(nt_status
)) {
281 DEBUG(1, ("Getting Samba info failed: %s\n", nt_errstr(nt_status
)));
282 talloc_free(pac_data
);
286 u_LOGON_INFO
->logon_info
.info
= LOGON_INFO
;
287 LOGON_INFO
->info3
= *sam3
;
289 ret
= krb5_unparse_name_flags(context
, client_principal
,
290 KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &name
);
294 LOGON_NAME
->account_name
= talloc_strdup(LOGON_NAME
, name
);
297 this logon_time field is absolutely critical. This is what
298 caused all our PAC troubles :-)
300 unix_to_nt_time(&LOGON_NAME
->logon_time
, tgs_authtime
);
302 ret
= kerberos_encode_pac(mem_ctx
,
308 talloc_free(pac_data
);
312 krb5_error_code
kerberos_pac_to_user_info_dc(TALLOC_CTX
*mem_ctx
,
314 krb5_context context
,
315 struct auth_user_info_dc
**user_info_dc
,
316 struct PAC_SIGNATURE_DATA
*pac_srv_sig
,
317 struct PAC_SIGNATURE_DATA
*pac_kdc_sig
)
320 enum ndr_err_code ndr_err
;
323 DATA_BLOB pac_logon_info_in
, pac_srv_checksum_in
, pac_kdc_checksum_in
;
324 krb5_data k5pac_logon_info_in
, k5pac_srv_checksum_in
, k5pac_kdc_checksum_in
;
327 struct auth_user_info_dc
*user_info_dc_out
;
329 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
335 ret
= krb5_pac_get_buffer(context
, pac
, PAC_TYPE_LOGON_INFO
, &k5pac_logon_info_in
);
337 talloc_free(tmp_ctx
);
341 pac_logon_info_in
= data_blob_const(k5pac_logon_info_in
.data
, k5pac_logon_info_in
.length
);
343 ndr_err
= ndr_pull_union_blob(&pac_logon_info_in
, tmp_ctx
, &info
,
345 (ndr_pull_flags_fn_t
)ndr_pull_PAC_INFO
);
346 kerberos_free_data_contents(context
, &k5pac_logon_info_in
);
347 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
) || !info
.logon_info
.info
) {
348 nt_status
= ndr_map_error2ntstatus(ndr_err
);
349 DEBUG(0,("can't parse the PAC LOGON_INFO: %s\n", nt_errstr(nt_status
)));
350 talloc_free(tmp_ctx
);
354 /* Pull this right into the normal auth sysstem structures */
355 nt_status
= make_user_info_dc_pac(mem_ctx
,
356 info
.logon_info
.info
,
358 if (!NT_STATUS_IS_OK(nt_status
)) {
359 talloc_free(tmp_ctx
);
364 ret
= krb5_pac_get_buffer(context
, pac
, PAC_TYPE_SRV_CHECKSUM
, &k5pac_srv_checksum_in
);
366 talloc_free(tmp_ctx
);
370 pac_srv_checksum_in
= data_blob_const(k5pac_srv_checksum_in
.data
, k5pac_srv_checksum_in
.length
);
372 ndr_err
= ndr_pull_struct_blob(&pac_srv_checksum_in
, pac_srv_sig
,
374 (ndr_pull_flags_fn_t
)ndr_pull_PAC_SIGNATURE_DATA
);
375 kerberos_free_data_contents(context
, &k5pac_srv_checksum_in
);
376 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
377 nt_status
= ndr_map_error2ntstatus(ndr_err
);
378 DEBUG(0,("can't parse the KDC signature: %s\n",
379 nt_errstr(nt_status
)));
385 ret
= krb5_pac_get_buffer(context
, pac
, PAC_TYPE_KDC_CHECKSUM
, &k5pac_kdc_checksum_in
);
387 talloc_free(tmp_ctx
);
391 pac_kdc_checksum_in
= data_blob_const(k5pac_kdc_checksum_in
.data
, k5pac_kdc_checksum_in
.length
);
393 ndr_err
= ndr_pull_struct_blob(&pac_kdc_checksum_in
, pac_kdc_sig
,
395 (ndr_pull_flags_fn_t
)ndr_pull_PAC_SIGNATURE_DATA
);
396 kerberos_free_data_contents(context
, &k5pac_kdc_checksum_in
);
397 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
398 nt_status
= ndr_map_error2ntstatus(ndr_err
);
399 DEBUG(0,("can't parse the KDC signature: %s\n",
400 nt_errstr(nt_status
)));
404 *user_info_dc
= user_info_dc_out
;
410 NTSTATUS
kerberos_pac_blob_to_user_info_dc(TALLOC_CTX
*mem_ctx
,
412 krb5_context context
,
413 struct auth_user_info_dc
**user_info_dc
,
414 struct PAC_SIGNATURE_DATA
*pac_srv_sig
,
415 struct PAC_SIGNATURE_DATA
*pac_kdc_sig
)
419 ret
= krb5_pac_parse(context
,
420 pac_blob
.data
, pac_blob
.length
,
423 return map_nt_error_from_unix_common(ret
);
427 ret
= kerberos_pac_to_user_info_dc(mem_ctx
, pac
, context
, user_info_dc
, pac_srv_sig
, pac_kdc_sig
);
428 krb5_pac_free(context
, pac
);
430 return map_nt_error_from_unix_common(ret
);