2 Unix SMB/CIFS implementation.
3 kerberos authorization data (PAC) utility library
4 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
6 Copyright (C) Andrew Tridgell 2001
7 Copyright (C) Luke Howard 2002-2003
8 Copyright (C) Stefan Metzmacher 2004-2005
9 Copyright (C) Guenther Deschner 2005,2007,2008
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.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "librpc/gen_ndr/ndr_krb5pac.h"
28 #include "libads/kerberos_proto.h"
32 /****************************************************************
33 ****************************************************************/
35 static krb5_error_code
check_pac_checksum(TALLOC_CTX
*mem_ctx
,
37 struct PAC_SIGNATURE_DATA
*sig
,
39 krb5_keyblock
*keyblock
)
43 krb5_keyusage usage
= 0;
45 smb_krb5_checksum_from_pac_sig(&cksum
, sig
);
47 #ifdef HAVE_KRB5_KU_OTHER_CKSUM /* Heimdal */
48 usage
= KRB5_KU_OTHER_CKSUM
;
49 #elif defined(HAVE_KRB5_KEYUSAGE_APP_DATA_CKSUM) /* MIT */
50 usage
= KRB5_KEYUSAGE_APP_DATA_CKSUM
;
52 #error UNKNOWN_KRB5_KEYUSAGE
55 ret
= smb_krb5_verify_checksum(context
,
63 DEBUG(2,("check_pac_checksum: PAC Verification failed: %s (%d)\n",
64 error_message(ret
), ret
));
71 /****************************************************************
72 ****************************************************************/
74 NTSTATUS
decode_pac_data(TALLOC_CTX
*mem_ctx
,
75 DATA_BLOB
*pac_data_blob
,
77 krb5_keyblock
*service_keyblock
,
78 krb5_const_principal client_principal
,
80 struct PAC_DATA
**pac_data_out
)
83 enum ndr_err_code ndr_err
;
85 DATA_BLOB modified_pac_blob
;
87 NTTIME tgs_authtime_nttime
;
88 krb5_principal client_principal_pac
= NULL
;
91 struct PAC_SIGNATURE_DATA
*srv_sig_ptr
= NULL
;
92 struct PAC_SIGNATURE_DATA
*kdc_sig_ptr
= NULL
;
93 struct PAC_SIGNATURE_DATA
*srv_sig_wipe
= NULL
;
94 struct PAC_SIGNATURE_DATA
*kdc_sig_wipe
= NULL
;
95 struct PAC_LOGON_NAME
*logon_name
= NULL
;
96 struct PAC_LOGON_INFO
*logon_info
= NULL
;
97 struct PAC_DATA
*pac_data
= NULL
;
98 struct PAC_DATA_RAW
*pac_data_raw
= NULL
;
100 DATA_BLOB
*srv_sig_blob
= NULL
;
101 DATA_BLOB
*kdc_sig_blob
= NULL
;
105 *pac_data_out
= NULL
;
107 pac_data
= TALLOC_ZERO_P(mem_ctx
, struct PAC_DATA
);
108 pac_data_raw
= TALLOC_ZERO_P(mem_ctx
, struct PAC_DATA_RAW
);
109 kdc_sig_wipe
= TALLOC_ZERO_P(mem_ctx
, struct PAC_SIGNATURE_DATA
);
110 srv_sig_wipe
= TALLOC_ZERO_P(mem_ctx
, struct PAC_SIGNATURE_DATA
);
111 if (!pac_data_raw
|| !pac_data
|| !kdc_sig_wipe
|| !srv_sig_wipe
) {
112 return NT_STATUS_NO_MEMORY
;
115 ndr_err
= ndr_pull_struct_blob(pac_data_blob
, pac_data
, pac_data
,
116 (ndr_pull_flags_fn_t
)ndr_pull_PAC_DATA
);
117 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
118 status
= ndr_map_error2ntstatus(ndr_err
);
119 DEBUG(0,("can't parse the PAC: %s\n",
124 if (pac_data
->num_buffers
< 4) {
125 /* we need logon_ingo, service_key and kdc_key */
126 DEBUG(0,("less than 4 PAC buffers\n"));
127 return NT_STATUS_INVALID_PARAMETER
;
130 ndr_err
= ndr_pull_struct_blob(pac_data_blob
, pac_data_raw
, pac_data_raw
,
131 (ndr_pull_flags_fn_t
)ndr_pull_PAC_DATA_RAW
);
132 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
133 status
= ndr_map_error2ntstatus(ndr_err
);
134 DEBUG(0,("can't parse the PAC: %s\n",
139 if (pac_data_raw
->num_buffers
< 4) {
140 /* we need logon_ingo, service_key and kdc_key */
141 DEBUG(0,("less than 4 PAC buffers\n"));
142 return NT_STATUS_INVALID_PARAMETER
;
145 if (pac_data
->num_buffers
!= pac_data_raw
->num_buffers
) {
146 /* we need logon_ingo, service_key and kdc_key */
147 DEBUG(0,("misparse! PAC_DATA has %d buffers while PAC_DATA_RAW has %d\n",
148 pac_data
->num_buffers
, pac_data_raw
->num_buffers
));
149 return NT_STATUS_INVALID_PARAMETER
;
152 for (i
=0; i
< pac_data
->num_buffers
; i
++) {
153 if (pac_data
->buffers
[i
].type
!= pac_data_raw
->buffers
[i
].type
) {
154 DEBUG(0,("misparse! PAC_DATA buffer %d has type %d while PAC_DATA_RAW has %d\n",
155 i
, pac_data
->buffers
[i
].type
, pac_data
->buffers
[i
].type
));
156 return NT_STATUS_INVALID_PARAMETER
;
158 switch (pac_data
->buffers
[i
].type
) {
159 case PAC_TYPE_LOGON_INFO
:
160 if (!pac_data
->buffers
[i
].info
) {
163 logon_info
= pac_data
->buffers
[i
].info
->logon_info
.info
;
165 case PAC_TYPE_SRV_CHECKSUM
:
166 if (!pac_data
->buffers
[i
].info
) {
169 srv_sig_ptr
= &pac_data
->buffers
[i
].info
->srv_cksum
;
170 srv_sig_blob
= &pac_data_raw
->buffers
[i
].info
->remaining
;
172 case PAC_TYPE_KDC_CHECKSUM
:
173 if (!pac_data
->buffers
[i
].info
) {
176 kdc_sig_ptr
= &pac_data
->buffers
[i
].info
->kdc_cksum
;
177 kdc_sig_blob
= &pac_data_raw
->buffers
[i
].info
->remaining
;
179 case PAC_TYPE_LOGON_NAME
:
180 logon_name
= &pac_data
->buffers
[i
].info
->logon_name
;
188 DEBUG(0,("PAC no logon_info\n"));
189 return NT_STATUS_INVALID_PARAMETER
;
193 DEBUG(0,("PAC no logon_name\n"));
194 return NT_STATUS_INVALID_PARAMETER
;
197 if (!srv_sig_ptr
|| !srv_sig_blob
) {
198 DEBUG(0,("PAC no srv_key\n"));
199 return NT_STATUS_INVALID_PARAMETER
;
202 if (!kdc_sig_ptr
|| !kdc_sig_blob
) {
203 DEBUG(0,("PAC no kdc_key\n"));
204 return NT_STATUS_INVALID_PARAMETER
;
207 /* Find and zero out the signatures, as required by the signing algorithm */
209 /* We find the data blobs above, now we parse them to get at the exact portion we should zero */
210 ndr_err
= ndr_pull_struct_blob(kdc_sig_blob
, kdc_sig_wipe
, kdc_sig_wipe
,
211 (ndr_pull_flags_fn_t
)ndr_pull_PAC_SIGNATURE_DATA
);
212 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
213 status
= ndr_map_error2ntstatus(ndr_err
);
214 DEBUG(0,("can't parse the KDC signature: %s\n",
219 ndr_err
= ndr_pull_struct_blob(srv_sig_blob
, srv_sig_wipe
, srv_sig_wipe
,
220 (ndr_pull_flags_fn_t
)ndr_pull_PAC_SIGNATURE_DATA
);
221 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
222 status
= ndr_map_error2ntstatus(ndr_err
);
223 DEBUG(0,("can't parse the SRV signature: %s\n",
228 /* Now zero the decoded structure */
229 memset(kdc_sig_wipe
->signature
.data
, '\0', kdc_sig_wipe
->signature
.length
);
230 memset(srv_sig_wipe
->signature
.data
, '\0', srv_sig_wipe
->signature
.length
);
232 /* and reencode, back into the same place it came from */
233 ndr_err
= ndr_push_struct_blob(kdc_sig_blob
, pac_data_raw
, kdc_sig_wipe
,
234 (ndr_push_flags_fn_t
)ndr_push_PAC_SIGNATURE_DATA
);
235 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
236 status
= ndr_map_error2ntstatus(ndr_err
);
237 DEBUG(0,("can't repack the KDC signature: %s\n",
241 ndr_err
= ndr_push_struct_blob(srv_sig_blob
, pac_data_raw
, srv_sig_wipe
,
242 (ndr_push_flags_fn_t
)ndr_push_PAC_SIGNATURE_DATA
);
243 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
244 status
= ndr_map_error2ntstatus(ndr_err
);
245 DEBUG(0,("can't repack the SRV signature: %s\n",
250 /* push out the whole structure, but now with zero'ed signatures */
251 ndr_err
= ndr_push_struct_blob(&modified_pac_blob
, pac_data_raw
,
253 (ndr_push_flags_fn_t
)ndr_push_PAC_DATA_RAW
);
254 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
255 status
= ndr_map_error2ntstatus(ndr_err
);
256 DEBUG(0,("can't repack the RAW PAC: %s\n",
261 /* verify by service_key */
262 ret
= check_pac_checksum(mem_ctx
,
263 modified_pac_blob
, srv_sig_ptr
,
267 DEBUG(1, ("PAC Decode: Failed to verify the service signature: %s\n",
268 error_message(ret
)));
269 return NT_STATUS_ACCESS_DENIED
;
272 /* Convert to NT time, so as not to loose accuracy in comparison */
273 unix_to_nt_time(&tgs_authtime_nttime
, tgs_authtime
);
275 if (tgs_authtime_nttime
!= logon_name
->logon_time
) {
276 DEBUG(2, ("PAC Decode: Logon time mismatch between ticket and PAC!\n"));
277 DEBUG(2, ("PAC Decode: PAC: %s\n", nt_time_string(mem_ctx
, logon_name
->logon_time
)));
278 DEBUG(2, ("PAC Decode: Ticket: %s\n", nt_time_string(mem_ctx
, tgs_authtime_nttime
)));
279 return NT_STATUS_ACCESS_DENIED
;
282 ret
= smb_krb5_parse_name_norealm(context
, logon_name
->account_name
,
283 &client_principal_pac
);
285 DEBUG(2, ("Could not parse name from incoming PAC: [%s]: %s\n",
286 logon_name
->account_name
,
287 error_message(ret
)));
288 return NT_STATUS_INVALID_PARAMETER
;
291 bool_ret
= smb_krb5_principal_compare_any_realm(
292 context
, client_principal
, client_principal_pac
);
294 krb5_free_principal(context
, client_principal_pac
);
297 DEBUG(2, ("Name in PAC [%s] does not match principal name in ticket\n",
298 logon_name
->account_name
));
299 return NT_STATUS_ACCESS_DENIED
;
302 DEBUG(3,("Found account name from PAC: %s [%s]\n",
303 logon_info
->info3
.base
.account_name
.string
,
304 logon_info
->info3
.base
.full_name
.string
));
306 DEBUG(10,("Successfully validated Kerberos PAC\n"));
308 if (DEBUGLEVEL
>= 10) {
310 s
= NDR_PRINT_STRUCT_STRING(mem_ctx
, PAC_DATA
, pac_data
);
312 DEBUGADD(10,("%s\n", s
));
316 *pac_data_out
= pac_data
;
321 /****************************************************************
322 Given a username, password and other details, return the
323 PAC_LOGON_INFO (the structure containing the important user
324 information such as groups).
325 ****************************************************************/
327 NTSTATUS
kerberos_return_pac(TALLOC_CTX
*mem_ctx
,
332 time_t *renew_till_time
,
333 const char *cache_name
,
335 bool add_netbios_addr
,
336 time_t renewable_time
,
337 const char *impersonate_princ_s
,
338 struct PAC_LOGON_INFO
**logon_info
)
341 NTSTATUS status
= NT_STATUS_INVALID_PARAMETER
;
342 DATA_BLOB tkt
, ap_rep
, sesskey1
, sesskey2
;
343 char *client_princ_out
= NULL
;
344 const char *auth_princ
= NULL
;
345 const char *local_service
= NULL
;
346 const char *cc
= "MEMORY:kerberos_return_pac";
350 ZERO_STRUCT(sesskey1
);
351 ZERO_STRUCT(sesskey2
);
353 if (!name
|| !pass
) {
354 return NT_STATUS_INVALID_PARAMETER
;
361 if (!strchr_m(name
, '@')) {
362 auth_princ
= talloc_asprintf(mem_ctx
, "%s@%s", name
,
367 NT_STATUS_HAVE_NO_MEMORY(auth_princ
);
369 local_service
= talloc_asprintf(mem_ctx
, "%s$@%s",
370 global_myname(), lp_realm());
371 NT_STATUS_HAVE_NO_MEMORY(local_service
);
373 ret
= kerberos_kinit_password_ext(auth_princ
,
384 DEBUG(1,("kinit failed for '%s' with: %s (%d)\n",
385 auth_princ
, error_message(ret
), ret
));
386 /* status already set */
390 DEBUG(10,("got TGT for %s in %s\n", auth_princ
, cc
));
392 DEBUGADD(10,("\tvalid until: %s (%d)\n",
393 http_timestring(talloc_tos(), *expire_time
),
396 if (renew_till_time
) {
397 DEBUGADD(10,("\trenewable till: %s (%d)\n",
398 http_timestring(talloc_tos(), *renew_till_time
),
399 (int)*renew_till_time
));
402 /* we cannot continue with krb5 when UF_DONT_REQUIRE_PREAUTH is set,
403 * in that case fallback to NTLM - gd */
405 if (expire_time
&& renew_till_time
&&
406 (*expire_time
== 0) && (*renew_till_time
== 0)) {
407 return NT_STATUS_INVALID_LOGON_TYPE
;
410 ret
= cli_krb5_get_ticket(mem_ctx
,
418 impersonate_princ_s
);
420 DEBUG(1,("failed to get ticket for %s: %s\n",
421 local_service
, error_message(ret
)));
422 if (impersonate_princ_s
) {
423 DEBUGADD(1,("tried S4U2SELF impersonation as: %s\n",
424 impersonate_princ_s
));
426 status
= krb5_to_nt_status(ret
);
429 status
= ads_verify_ticket(mem_ctx
,
438 if (!NT_STATUS_IS_OK(status
)) {
439 DEBUG(1,("ads_verify_ticket failed: %s\n",
445 DEBUG(1,("no PAC\n"));
446 status
= NT_STATUS_INVALID_PARAMETER
;
451 if (cc
!= cache_name
) {
455 data_blob_free(&tkt
);
456 data_blob_free(&ap_rep
);
457 data_blob_free(&sesskey1
);
458 data_blob_free(&sesskey2
);
460 TALLOC_FREE(client_princ_out
);