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"
31 /****************************************************************
32 ****************************************************************/
34 static krb5_error_code
check_pac_checksum(TALLOC_CTX
*mem_ctx
,
36 struct PAC_SIGNATURE_DATA
*sig
,
38 krb5_keyblock
*keyblock
)
42 krb5_keyusage usage
= 0;
44 smb_krb5_checksum_from_pac_sig(&cksum
, sig
);
46 #ifdef HAVE_KRB5_KU_OTHER_CKSUM /* Heimdal */
47 usage
= KRB5_KU_OTHER_CKSUM
;
48 #elif defined(HAVE_KRB5_KEYUSAGE_APP_DATA_CKSUM) /* MIT */
49 usage
= KRB5_KEYUSAGE_APP_DATA_CKSUM
;
51 #error UNKNOWN_KRB5_KEYUSAGE
54 ret
= smb_krb5_verify_checksum(context
,
62 DEBUG(2,("check_pac_checksum: PAC Verification failed: %s (%d)\n",
63 error_message(ret
), ret
));
70 /****************************************************************
71 ****************************************************************/
73 NTSTATUS
decode_pac_data(TALLOC_CTX
*mem_ctx
,
74 DATA_BLOB
*pac_data_blob
,
76 krb5_keyblock
*service_keyblock
,
77 krb5_const_principal client_principal
,
79 struct PAC_DATA
**pac_data_out
)
82 enum ndr_err_code ndr_err
;
84 DATA_BLOB modified_pac_blob
;
86 NTTIME tgs_authtime_nttime
;
87 krb5_principal client_principal_pac
= NULL
;
90 struct PAC_SIGNATURE_DATA
*srv_sig_ptr
= NULL
;
91 struct PAC_SIGNATURE_DATA
*kdc_sig_ptr
= NULL
;
92 struct PAC_SIGNATURE_DATA
*srv_sig_wipe
= NULL
;
93 struct PAC_SIGNATURE_DATA
*kdc_sig_wipe
= NULL
;
94 struct PAC_LOGON_NAME
*logon_name
= NULL
;
95 struct PAC_LOGON_INFO
*logon_info
= NULL
;
96 struct PAC_DATA
*pac_data
= NULL
;
97 struct PAC_DATA_RAW
*pac_data_raw
= NULL
;
99 DATA_BLOB
*srv_sig_blob
= NULL
;
100 DATA_BLOB
*kdc_sig_blob
= NULL
;
104 *pac_data_out
= NULL
;
106 pac_data
= TALLOC_ZERO_P(mem_ctx
, struct PAC_DATA
);
107 pac_data_raw
= TALLOC_ZERO_P(mem_ctx
, struct PAC_DATA_RAW
);
108 kdc_sig_wipe
= TALLOC_ZERO_P(mem_ctx
, struct PAC_SIGNATURE_DATA
);
109 srv_sig_wipe
= TALLOC_ZERO_P(mem_ctx
, struct PAC_SIGNATURE_DATA
);
110 if (!pac_data_raw
|| !pac_data
|| !kdc_sig_wipe
|| !srv_sig_wipe
) {
111 return NT_STATUS_NO_MEMORY
;
114 ndr_err
= ndr_pull_struct_blob(pac_data_blob
, pac_data
, pac_data
,
115 (ndr_pull_flags_fn_t
)ndr_pull_PAC_DATA
);
116 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
117 status
= ndr_map_error2ntstatus(ndr_err
);
118 DEBUG(0,("can't parse the PAC: %s\n",
123 if (pac_data
->num_buffers
< 4) {
124 /* we need logon_ingo, service_key and kdc_key */
125 DEBUG(0,("less than 4 PAC buffers\n"));
126 return NT_STATUS_INVALID_PARAMETER
;
129 ndr_err
= ndr_pull_struct_blob(pac_data_blob
, pac_data_raw
, pac_data_raw
,
130 (ndr_pull_flags_fn_t
)ndr_pull_PAC_DATA_RAW
);
131 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
132 status
= ndr_map_error2ntstatus(ndr_err
);
133 DEBUG(0,("can't parse the PAC: %s\n",
138 if (pac_data_raw
->num_buffers
< 4) {
139 /* we need logon_ingo, service_key and kdc_key */
140 DEBUG(0,("less than 4 PAC buffers\n"));
141 return NT_STATUS_INVALID_PARAMETER
;
144 if (pac_data
->num_buffers
!= pac_data_raw
->num_buffers
) {
145 /* we need logon_ingo, service_key and kdc_key */
146 DEBUG(0,("misparse! PAC_DATA has %d buffers while PAC_DATA_RAW has %d\n",
147 pac_data
->num_buffers
, pac_data_raw
->num_buffers
));
148 return NT_STATUS_INVALID_PARAMETER
;
151 for (i
=0; i
< pac_data
->num_buffers
; i
++) {
152 if (pac_data
->buffers
[i
].type
!= pac_data_raw
->buffers
[i
].type
) {
153 DEBUG(0,("misparse! PAC_DATA buffer %d has type %d while PAC_DATA_RAW has %d\n",
154 i
, pac_data
->buffers
[i
].type
, pac_data
->buffers
[i
].type
));
155 return NT_STATUS_INVALID_PARAMETER
;
157 switch (pac_data
->buffers
[i
].type
) {
158 case PAC_TYPE_LOGON_INFO
:
159 if (!pac_data
->buffers
[i
].info
) {
162 logon_info
= pac_data
->buffers
[i
].info
->logon_info
.info
;
164 case PAC_TYPE_SRV_CHECKSUM
:
165 if (!pac_data
->buffers
[i
].info
) {
168 srv_sig_ptr
= &pac_data
->buffers
[i
].info
->srv_cksum
;
169 srv_sig_blob
= &pac_data_raw
->buffers
[i
].info
->remaining
;
171 case PAC_TYPE_KDC_CHECKSUM
:
172 if (!pac_data
->buffers
[i
].info
) {
175 kdc_sig_ptr
= &pac_data
->buffers
[i
].info
->kdc_cksum
;
176 kdc_sig_blob
= &pac_data_raw
->buffers
[i
].info
->remaining
;
178 case PAC_TYPE_LOGON_NAME
:
179 logon_name
= &pac_data
->buffers
[i
].info
->logon_name
;
187 DEBUG(0,("PAC no logon_info\n"));
188 return NT_STATUS_INVALID_PARAMETER
;
192 DEBUG(0,("PAC no logon_name\n"));
193 return NT_STATUS_INVALID_PARAMETER
;
196 if (!srv_sig_ptr
|| !srv_sig_blob
) {
197 DEBUG(0,("PAC no srv_key\n"));
198 return NT_STATUS_INVALID_PARAMETER
;
201 if (!kdc_sig_ptr
|| !kdc_sig_blob
) {
202 DEBUG(0,("PAC no kdc_key\n"));
203 return NT_STATUS_INVALID_PARAMETER
;
206 /* Find and zero out the signatures, as required by the signing algorithm */
208 /* We find the data blobs above, now we parse them to get at the exact portion we should zero */
209 ndr_err
= ndr_pull_struct_blob(kdc_sig_blob
, kdc_sig_wipe
, kdc_sig_wipe
,
210 (ndr_pull_flags_fn_t
)ndr_pull_PAC_SIGNATURE_DATA
);
211 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
212 status
= ndr_map_error2ntstatus(ndr_err
);
213 DEBUG(0,("can't parse the KDC signature: %s\n",
218 ndr_err
= ndr_pull_struct_blob(srv_sig_blob
, srv_sig_wipe
, srv_sig_wipe
,
219 (ndr_pull_flags_fn_t
)ndr_pull_PAC_SIGNATURE_DATA
);
220 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
221 status
= ndr_map_error2ntstatus(ndr_err
);
222 DEBUG(0,("can't parse the SRV signature: %s\n",
227 /* Now zero the decoded structure */
228 memset(kdc_sig_wipe
->signature
.data
, '\0', kdc_sig_wipe
->signature
.length
);
229 memset(srv_sig_wipe
->signature
.data
, '\0', srv_sig_wipe
->signature
.length
);
231 /* and reencode, back into the same place it came from */
232 ndr_err
= ndr_push_struct_blob(kdc_sig_blob
, pac_data_raw
, kdc_sig_wipe
,
233 (ndr_push_flags_fn_t
)ndr_push_PAC_SIGNATURE_DATA
);
234 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
235 status
= ndr_map_error2ntstatus(ndr_err
);
236 DEBUG(0,("can't repack the KDC signature: %s\n",
240 ndr_err
= ndr_push_struct_blob(srv_sig_blob
, pac_data_raw
, srv_sig_wipe
,
241 (ndr_push_flags_fn_t
)ndr_push_PAC_SIGNATURE_DATA
);
242 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
243 status
= ndr_map_error2ntstatus(ndr_err
);
244 DEBUG(0,("can't repack the SRV signature: %s\n",
249 /* push out the whole structure, but now with zero'ed signatures */
250 ndr_err
= ndr_push_struct_blob(&modified_pac_blob
, pac_data_raw
,
252 (ndr_push_flags_fn_t
)ndr_push_PAC_DATA_RAW
);
253 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
254 status
= ndr_map_error2ntstatus(ndr_err
);
255 DEBUG(0,("can't repack the RAW PAC: %s\n",
260 /* verify by service_key */
261 ret
= check_pac_checksum(mem_ctx
,
262 modified_pac_blob
, srv_sig_ptr
,
266 DEBUG(1, ("PAC Decode: Failed to verify the service signature: %s\n",
267 error_message(ret
)));
268 return NT_STATUS_ACCESS_DENIED
;
271 /* Convert to NT time, so as not to loose accuracy in comparison */
272 unix_to_nt_time(&tgs_authtime_nttime
, tgs_authtime
);
274 if (tgs_authtime_nttime
!= logon_name
->logon_time
) {
275 DEBUG(2, ("PAC Decode: Logon time mismatch between ticket and PAC!\n"));
276 DEBUG(2, ("PAC Decode: PAC: %s\n", nt_time_string(mem_ctx
, logon_name
->logon_time
)));
277 DEBUG(2, ("PAC Decode: Ticket: %s\n", nt_time_string(mem_ctx
, tgs_authtime_nttime
)));
278 return NT_STATUS_ACCESS_DENIED
;
281 ret
= smb_krb5_parse_name_norealm(context
, logon_name
->account_name
,
282 &client_principal_pac
);
284 DEBUG(2, ("Could not parse name from incoming PAC: [%s]: %s\n",
285 logon_name
->account_name
,
286 error_message(ret
)));
287 return NT_STATUS_INVALID_PARAMETER
;
290 bool_ret
= smb_krb5_principal_compare_any_realm(
291 context
, client_principal
, client_principal_pac
);
293 krb5_free_principal(context
, client_principal_pac
);
296 DEBUG(2, ("Name in PAC [%s] does not match principal name in ticket\n",
297 logon_name
->account_name
));
298 return NT_STATUS_ACCESS_DENIED
;
301 DEBUG(3,("Found account name from PAC: %s [%s]\n",
302 logon_info
->info3
.base
.account_name
.string
,
303 logon_info
->info3
.base
.full_name
.string
));
305 DEBUG(10,("Successfully validated Kerberos PAC\n"));
307 if (DEBUGLEVEL
>= 10) {
309 s
= NDR_PRINT_STRUCT_STRING(mem_ctx
, PAC_DATA
, pac_data
);
311 DEBUGADD(10,("%s\n", s
));
315 *pac_data_out
= pac_data
;
320 /****************************************************************
321 Given a username, password and other details, return the
322 PAC_LOGON_INFO (the structure containing the important user
323 information such as groups).
324 ****************************************************************/
326 NTSTATUS
kerberos_return_pac(TALLOC_CTX
*mem_ctx
,
331 time_t *renew_till_time
,
332 const char *cache_name
,
334 bool add_netbios_addr
,
335 time_t renewable_time
,
336 const char *impersonate_princ_s
,
337 struct PAC_LOGON_INFO
**logon_info
)
340 NTSTATUS status
= NT_STATUS_INVALID_PARAMETER
;
341 DATA_BLOB tkt
, ap_rep
, sesskey1
, sesskey2
;
342 char *client_princ_out
= NULL
;
343 const char *auth_princ
= NULL
;
344 const char *local_service
= NULL
;
345 const char *cc
= "MEMORY:kerberos_return_pac";
349 ZERO_STRUCT(sesskey1
);
350 ZERO_STRUCT(sesskey2
);
352 if (!name
|| !pass
) {
353 return NT_STATUS_INVALID_PARAMETER
;
360 if (!strchr_m(name
, '@')) {
361 auth_princ
= talloc_asprintf(mem_ctx
, "%s@%s", name
,
366 NT_STATUS_HAVE_NO_MEMORY(auth_princ
);
368 local_service
= talloc_asprintf(mem_ctx
, "%s$@%s",
369 global_myname(), lp_realm());
370 NT_STATUS_HAVE_NO_MEMORY(local_service
);
372 ret
= kerberos_kinit_password_ext(auth_princ
,
383 DEBUG(1,("kinit failed for '%s' with: %s (%d)\n",
384 auth_princ
, error_message(ret
), ret
));
385 /* status already set */
389 DEBUG(10,("got TGT for %s in %s\n", auth_princ
, cc
));
391 DEBUGADD(10,("\tvalid until: %s (%d)\n",
392 http_timestring(talloc_tos(), *expire_time
),
395 if (renew_till_time
) {
396 DEBUGADD(10,("\trenewable till: %s (%d)\n",
397 http_timestring(talloc_tos(), *renew_till_time
),
398 (int)*renew_till_time
));
401 /* we cannot continue with krb5 when UF_DONT_REQUIRE_PREAUTH is set,
402 * in that case fallback to NTLM - gd */
404 if (expire_time
&& renew_till_time
&&
405 (*expire_time
== 0) && (*renew_till_time
== 0)) {
406 return NT_STATUS_INVALID_LOGON_TYPE
;
409 ret
= cli_krb5_get_ticket(mem_ctx
,
417 impersonate_princ_s
);
419 DEBUG(1,("failed to get ticket for %s: %s\n",
420 local_service
, error_message(ret
)));
421 if (impersonate_princ_s
) {
422 DEBUGADD(1,("tried S4U2SELF impersonation as: %s\n",
423 impersonate_princ_s
));
425 status
= krb5_to_nt_status(ret
);
428 status
= ads_verify_ticket(mem_ctx
,
437 if (!NT_STATUS_IS_OK(status
)) {
438 DEBUG(1,("ads_verify_ticket failed: %s\n",
444 DEBUG(1,("no PAC\n"));
445 status
= NT_STATUS_INVALID_PARAMETER
;
450 if (cc
!= cache_name
) {
454 data_blob_free(&tkt
);
455 data_blob_free(&ap_rep
);
456 data_blob_free(&sesskey1
);
457 data_blob_free(&sesskey2
);
459 TALLOC_FREE(client_princ_out
);