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
));
72 * @brief Decode a blob containing a NDR envoded PAC structure
74 * @param mem_ctx - The memory context
75 * @param pac_data_blob - The data blob containing the NDR encoded data
76 * @param context - The Kerberos Context
77 * @param service_keyblock - The Service Key used to verify the checksum
78 * @param client_principal - The client principal
79 * @param tgs_authtime - The ticket timestamp
80 * @param pac_data_out - [out] The decoded PAC
82 * @return - A NTSTATUS error code
84 NTSTATUS
decode_pac_data(TALLOC_CTX
*mem_ctx
,
85 DATA_BLOB
*pac_data_blob
,
87 krb5_keyblock
*service_keyblock
,
88 krb5_const_principal client_principal
,
90 struct PAC_DATA
**pac_data_out
)
93 enum ndr_err_code ndr_err
;
95 DATA_BLOB modified_pac_blob
;
97 NTTIME tgs_authtime_nttime
;
98 krb5_principal client_principal_pac
= NULL
;
101 struct PAC_SIGNATURE_DATA
*srv_sig_ptr
= NULL
;
102 struct PAC_SIGNATURE_DATA
*kdc_sig_ptr
= NULL
;
103 struct PAC_SIGNATURE_DATA
*srv_sig_wipe
= NULL
;
104 struct PAC_SIGNATURE_DATA
*kdc_sig_wipe
= NULL
;
105 struct PAC_LOGON_NAME
*logon_name
= NULL
;
106 struct PAC_LOGON_INFO
*logon_info
= NULL
;
107 struct PAC_DATA
*pac_data
= NULL
;
108 struct PAC_DATA_RAW
*pac_data_raw
= NULL
;
110 DATA_BLOB
*srv_sig_blob
= NULL
;
111 DATA_BLOB
*kdc_sig_blob
= NULL
;
115 *pac_data_out
= NULL
;
117 pac_data
= TALLOC_ZERO_P(mem_ctx
, struct PAC_DATA
);
118 pac_data_raw
= TALLOC_ZERO_P(mem_ctx
, struct PAC_DATA_RAW
);
119 kdc_sig_wipe
= TALLOC_ZERO_P(mem_ctx
, struct PAC_SIGNATURE_DATA
);
120 srv_sig_wipe
= TALLOC_ZERO_P(mem_ctx
, struct PAC_SIGNATURE_DATA
);
121 if (!pac_data_raw
|| !pac_data
|| !kdc_sig_wipe
|| !srv_sig_wipe
) {
122 return NT_STATUS_NO_MEMORY
;
125 ndr_err
= ndr_pull_struct_blob(pac_data_blob
, pac_data
, pac_data
,
126 (ndr_pull_flags_fn_t
)ndr_pull_PAC_DATA
);
127 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
128 status
= ndr_map_error2ntstatus(ndr_err
);
129 DEBUG(0,("can't parse the PAC: %s\n",
134 if (pac_data
->num_buffers
< 4) {
135 /* we need logon_ingo, service_key and kdc_key */
136 DEBUG(0,("less than 4 PAC buffers\n"));
137 return NT_STATUS_INVALID_PARAMETER
;
140 ndr_err
= ndr_pull_struct_blob(
141 pac_data_blob
, pac_data_raw
, pac_data_raw
,
142 (ndr_pull_flags_fn_t
)ndr_pull_PAC_DATA_RAW
);
143 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
144 status
= ndr_map_error2ntstatus(ndr_err
);
145 DEBUG(0,("can't parse the PAC: %s\n",
150 if (pac_data_raw
->num_buffers
< 4) {
151 /* we need logon_ingo, service_key and kdc_key */
152 DEBUG(0,("less than 4 PAC buffers\n"));
153 return NT_STATUS_INVALID_PARAMETER
;
156 if (pac_data
->num_buffers
!= pac_data_raw
->num_buffers
) {
157 /* we need logon_ingo, service_key and kdc_key */
158 DEBUG(0, ("misparse! PAC_DATA has %d buffers while "
159 "PAC_DATA_RAW has %d\n", pac_data
->num_buffers
,
160 pac_data_raw
->num_buffers
));
161 return NT_STATUS_INVALID_PARAMETER
;
164 for (i
=0; i
< pac_data
->num_buffers
; i
++) {
165 struct PAC_BUFFER
*data_buf
= &pac_data
->buffers
[i
];
166 struct PAC_BUFFER_RAW
*raw_buf
= &pac_data_raw
->buffers
[i
];
168 if (data_buf
->type
!= raw_buf
->type
) {
169 DEBUG(0, ("misparse! PAC_DATA buffer %d has type "
170 "%d while PAC_DATA_RAW has %d\n", i
,
171 data_buf
->type
, raw_buf
->type
));
172 return NT_STATUS_INVALID_PARAMETER
;
174 switch (data_buf
->type
) {
175 case PAC_TYPE_LOGON_INFO
:
176 if (!data_buf
->info
) {
179 logon_info
= data_buf
->info
->logon_info
.info
;
181 case PAC_TYPE_SRV_CHECKSUM
:
182 if (!data_buf
->info
) {
185 srv_sig_ptr
= &data_buf
->info
->srv_cksum
;
186 srv_sig_blob
= &raw_buf
->info
->remaining
;
188 case PAC_TYPE_KDC_CHECKSUM
:
189 if (!data_buf
->info
) {
192 kdc_sig_ptr
= &data_buf
->info
->kdc_cksum
;
193 kdc_sig_blob
= &raw_buf
->info
->remaining
;
195 case PAC_TYPE_LOGON_NAME
:
196 logon_name
= &data_buf
->info
->logon_name
;
204 DEBUG(0,("PAC no logon_info\n"));
205 return NT_STATUS_INVALID_PARAMETER
;
209 DEBUG(0,("PAC no logon_name\n"));
210 return NT_STATUS_INVALID_PARAMETER
;
213 if (!srv_sig_ptr
|| !srv_sig_blob
) {
214 DEBUG(0,("PAC no srv_key\n"));
215 return NT_STATUS_INVALID_PARAMETER
;
218 if (!kdc_sig_ptr
|| !kdc_sig_blob
) {
219 DEBUG(0,("PAC no kdc_key\n"));
220 return NT_STATUS_INVALID_PARAMETER
;
223 /* Find and zero out the signatures,
224 * as required by the signing algorithm */
226 /* We find the data blobs above,
227 * now we parse them to get at the exact portion we should zero */
228 ndr_err
= ndr_pull_struct_blob(
229 kdc_sig_blob
, kdc_sig_wipe
, kdc_sig_wipe
,
230 (ndr_pull_flags_fn_t
)ndr_pull_PAC_SIGNATURE_DATA
);
231 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
232 status
= ndr_map_error2ntstatus(ndr_err
);
233 DEBUG(0,("can't parse the KDC signature: %s\n",
238 ndr_err
= ndr_pull_struct_blob(
239 srv_sig_blob
, srv_sig_wipe
, srv_sig_wipe
,
240 (ndr_pull_flags_fn_t
)ndr_pull_PAC_SIGNATURE_DATA
);
241 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
242 status
= ndr_map_error2ntstatus(ndr_err
);
243 DEBUG(0,("can't parse the SRV signature: %s\n",
248 /* Now zero the decoded structure */
249 memset(kdc_sig_wipe
->signature
.data
,
250 '\0', kdc_sig_wipe
->signature
.length
);
251 memset(srv_sig_wipe
->signature
.data
,
252 '\0', srv_sig_wipe
->signature
.length
);
254 /* and reencode, back into the same place it came from */
255 ndr_err
= ndr_push_struct_blob(
256 kdc_sig_blob
, pac_data_raw
, kdc_sig_wipe
,
257 (ndr_push_flags_fn_t
)ndr_push_PAC_SIGNATURE_DATA
);
258 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
259 status
= ndr_map_error2ntstatus(ndr_err
);
260 DEBUG(0,("can't repack the KDC signature: %s\n",
264 ndr_err
= ndr_push_struct_blob(
265 srv_sig_blob
, pac_data_raw
, srv_sig_wipe
,
266 (ndr_push_flags_fn_t
)ndr_push_PAC_SIGNATURE_DATA
);
267 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
268 status
= ndr_map_error2ntstatus(ndr_err
);
269 DEBUG(0,("can't repack the SRV signature: %s\n",
274 /* push out the whole structure, but now with zero'ed signatures */
275 ndr_err
= ndr_push_struct_blob(
276 &modified_pac_blob
, pac_data_raw
, pac_data_raw
,
277 (ndr_push_flags_fn_t
)ndr_push_PAC_DATA_RAW
);
278 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
279 status
= ndr_map_error2ntstatus(ndr_err
);
280 DEBUG(0,("can't repack the RAW PAC: %s\n",
285 /* verify by service_key */
286 ret
= check_pac_checksum(mem_ctx
,
287 modified_pac_blob
, srv_sig_ptr
,
291 DEBUG(1, ("PAC Decode: Failed to verify the service "
292 "signature: %s\n", error_message(ret
)));
293 return NT_STATUS_ACCESS_DENIED
;
296 /* Convert to NT time, so as not to loose accuracy in comparison */
297 unix_to_nt_time(&tgs_authtime_nttime
, tgs_authtime
);
299 if (tgs_authtime_nttime
!= logon_name
->logon_time
) {
300 DEBUG(2, ("PAC Decode: "
301 "Logon time mismatch between ticket and PAC!\n"));
302 DEBUG(2, ("PAC Decode: PAC: %s\n",
303 nt_time_string(mem_ctx
, logon_name
->logon_time
)));
304 DEBUG(2, ("PAC Decode: Ticket: %s\n",
305 nt_time_string(mem_ctx
, tgs_authtime_nttime
)));
306 return NT_STATUS_ACCESS_DENIED
;
309 ret
= smb_krb5_parse_name_norealm(context
,
310 logon_name
->account_name
,
311 &client_principal_pac
);
313 DEBUG(2, ("Could not parse name from PAC: [%s]:%s\n",
314 logon_name
->account_name
, error_message(ret
)));
315 return NT_STATUS_INVALID_PARAMETER
;
318 bool_ret
= smb_krb5_principal_compare_any_realm(context
,
320 client_principal_pac
);
322 krb5_free_principal(context
, client_principal_pac
);
325 DEBUG(2, ("Name in PAC [%s] does not match principal name "
326 "in ticket\n", logon_name
->account_name
));
327 return NT_STATUS_ACCESS_DENIED
;
330 DEBUG(3,("Found account name from PAC: %s [%s]\n",
331 logon_info
->info3
.base
.account_name
.string
,
332 logon_info
->info3
.base
.full_name
.string
));
334 DEBUG(10,("Successfully validated Kerberos PAC\n"));
336 if (DEBUGLEVEL
>= 10) {
338 s
= NDR_PRINT_STRUCT_STRING(mem_ctx
, PAC_DATA
, pac_data
);
340 DEBUGADD(10,("%s\n", s
));
344 *pac_data_out
= pac_data
;
349 /****************************************************************
350 Given a username, password and other details, return the
351 PAC_LOGON_INFO (the structure containing the important user
352 information such as groups).
353 ****************************************************************/
355 NTSTATUS
kerberos_return_pac(TALLOC_CTX
*mem_ctx
,
360 time_t *renew_till_time
,
361 const char *cache_name
,
363 bool add_netbios_addr
,
364 time_t renewable_time
,
365 const char *impersonate_princ_s
,
366 struct PAC_LOGON_INFO
**logon_info
)
369 NTSTATUS status
= NT_STATUS_INVALID_PARAMETER
;
370 DATA_BLOB tkt
, ap_rep
, sesskey1
, sesskey2
;
371 char *client_princ_out
= NULL
;
372 const char *auth_princ
= NULL
;
373 const char *local_service
= NULL
;
374 const char *cc
= "MEMORY:kerberos_return_pac";
378 ZERO_STRUCT(sesskey1
);
379 ZERO_STRUCT(sesskey2
);
381 if (!name
|| !pass
) {
382 return NT_STATUS_INVALID_PARAMETER
;
389 if (!strchr_m(name
, '@')) {
390 auth_princ
= talloc_asprintf(mem_ctx
, "%s@%s", name
,
395 NT_STATUS_HAVE_NO_MEMORY(auth_princ
);
397 local_service
= talloc_asprintf(mem_ctx
, "%s$@%s",
398 global_myname(), lp_realm());
399 NT_STATUS_HAVE_NO_MEMORY(local_service
);
401 ret
= kerberos_kinit_password_ext(auth_princ
,
412 DEBUG(1,("kinit failed for '%s' with: %s (%d)\n",
413 auth_princ
, error_message(ret
), ret
));
414 /* status already set */
418 DEBUG(10,("got TGT for %s in %s\n", auth_princ
, cc
));
420 DEBUGADD(10,("\tvalid until: %s (%d)\n",
421 http_timestring(talloc_tos(), *expire_time
),
424 if (renew_till_time
) {
425 DEBUGADD(10,("\trenewable till: %s (%d)\n",
426 http_timestring(talloc_tos(), *renew_till_time
),
427 (int)*renew_till_time
));
430 /* we cannot continue with krb5 when UF_DONT_REQUIRE_PREAUTH is set,
431 * in that case fallback to NTLM - gd */
433 if (expire_time
&& renew_till_time
&&
434 (*expire_time
== 0) && (*renew_till_time
== 0)) {
435 return NT_STATUS_INVALID_LOGON_TYPE
;
438 ret
= cli_krb5_get_ticket(mem_ctx
,
446 impersonate_princ_s
);
448 DEBUG(1,("failed to get ticket for %s: %s\n",
449 local_service
, error_message(ret
)));
450 if (impersonate_princ_s
) {
451 DEBUGADD(1,("tried S4U2SELF impersonation as: %s\n",
452 impersonate_princ_s
));
454 status
= krb5_to_nt_status(ret
);
457 status
= ads_verify_ticket(mem_ctx
,
466 if (!NT_STATUS_IS_OK(status
)) {
467 DEBUG(1,("ads_verify_ticket failed: %s\n",
473 DEBUG(1,("no PAC\n"));
474 status
= NT_STATUS_INVALID_PARAMETER
;
479 if (cc
!= cache_name
) {
483 data_blob_free(&tkt
);
484 data_blob_free(&ap_rep
);
485 data_blob_free(&sesskey1
);
486 data_blob_free(&sesskey2
);
488 TALLOC_FREE(client_princ_out
);