2 Unix SMB/CIFS implementation.
3 kerberos utility library
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Remus Koos 2001
6 Copyright (C) Luke Howard 2003
7 Copyright (C) Guenther Deschner 2003, 2005
8 Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2003
9 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
10 Copyright (C) Jeremy Allison 2007
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 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/>.
30 #if !defined(HAVE_KRB5_PRINC_COMPONENT)
31 const krb5_data
*krb5_princ_component(krb5_context
, krb5_principal
, int );
34 /**********************************************************************************
35 Try to verify a ticket using the system keytab... the system keytab has kvno -1 entries, so
36 it's more like what microsoft does... see comment in utils/net_ads.c in the
37 ads_keytab_add_entry function for details.
38 ***********************************************************************************/
40 static bool ads_keytab_verify_ticket(krb5_context context
,
41 krb5_auth_context auth_context
,
42 const DATA_BLOB
*ticket
,
44 krb5_keyblock
**keyblock
,
45 krb5_error_code
*perr
)
47 krb5_error_code ret
= 0;
49 krb5_keytab keytab
= NULL
;
50 krb5_kt_cursor kt_cursor
;
51 krb5_keytab_entry kt_entry
;
52 char *valid_princ_formats
[7] = { NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
53 char *entry_princ_s
= NULL
;
54 fstring my_name
, my_fqdn
;
56 int number_matched_principals
= 0;
63 /* Generate the list of principal names which we expect
64 * clients might want to use for authenticating to the file
65 * service. We allow name$,{host,cifs}/{name,fqdn,name.REALM}. */
67 fstrcpy(my_name
, global_myname());
70 name_to_fqdn(my_fqdn
, global_myname());
72 if (asprintf(&valid_princ_formats
[0], "%s$@%s", my_name
, lp_realm()) == -1) {
75 if (asprintf(&valid_princ_formats
[1], "host/%s@%s", my_name
, lp_realm()) == -1) {
78 if (asprintf(&valid_princ_formats
[2], "host/%s@%s", my_fqdn
, lp_realm()) == -1) {
81 if (asprintf(&valid_princ_formats
[3], "host/%s.%s@%s", my_name
, lp_realm(), lp_realm()) == -1) {
84 if (asprintf(&valid_princ_formats
[4], "cifs/%s@%s", my_name
, lp_realm()) == -1) {
87 if (asprintf(&valid_princ_formats
[5], "cifs/%s@%s", my_fqdn
, lp_realm()) == -1) {
90 if (asprintf(&valid_princ_formats
[6], "cifs/%s.%s@%s", my_name
, lp_realm(), lp_realm()) == -1) {
94 ZERO_STRUCT(kt_entry
);
95 ZERO_STRUCT(kt_cursor
);
97 ret
= smb_krb5_open_keytab(context
, NULL
, False
, &keytab
);
99 DEBUG(1, ("ads_keytab_verify_ticket: smb_krb5_open_keytab failed (%s)\n", error_message(ret
)));
103 /* Iterate through the keytab. For each key, if the principal
104 * name case-insensitively matches one of the allowed formats,
105 * try verifying the ticket using that principal. */
107 ret
= krb5_kt_start_seq_get(context
, keytab
, &kt_cursor
);
109 DEBUG(1, ("ads_keytab_verify_ticket: krb5_kt_start_seq_get failed (%s)\n", error_message(ret
)));
113 while (!auth_ok
&& (krb5_kt_next_entry(context
, keytab
, &kt_entry
, &kt_cursor
) == 0)) {
114 ret
= smb_krb5_unparse_name(context
, kt_entry
.principal
, &entry_princ_s
);
116 DEBUG(1, ("ads_keytab_verify_ticket: smb_krb5_unparse_name failed (%s)\n",
117 error_message(ret
)));
121 for (i
= 0; i
< ARRAY_SIZE(valid_princ_formats
); i
++) {
123 if (!strequal(entry_princ_s
, valid_princ_formats
[i
])) {
127 number_matched_principals
++;
128 packet
.length
= ticket
->length
;
129 packet
.data
= (char *)ticket
->data
;
132 ret
= krb5_rd_req_return_keyblock_from_keytab(context
, &auth_context
, &packet
,
133 kt_entry
.principal
, keytab
,
134 NULL
, pp_tkt
, keyblock
);
137 DEBUG(10,("ads_keytab_verify_ticket: "
138 "krb5_rd_req_return_keyblock_from_keytab(%s) failed: %s\n",
139 entry_princ_s
, error_message(ret
)));
141 /* workaround for MIT:
142 * as krb5_ktfile_get_entry will explicitly
143 * close the krb5_keytab as soon as krb5_rd_req
144 * has successfully decrypted the ticket but the
145 * ticket is not valid yet (due to clockskew)
146 * there is no point in querying more keytab
147 * entries - Guenther */
149 if (ret
== KRB5KRB_AP_ERR_TKT_NYV
||
150 ret
== KRB5KRB_AP_ERR_TKT_EXPIRED
||
151 ret
== KRB5KRB_AP_ERR_SKEW
) {
155 DEBUG(3,("ads_keytab_verify_ticket: "
156 "krb5_rd_req_return_keyblock_from_keytab succeeded for principal %s\n",
163 /* Free the name we parsed. */
164 SAFE_FREE(entry_princ_s
);
166 /* Free the entry we just read. */
167 smb_krb5_kt_free_entry(context
, &kt_entry
);
168 ZERO_STRUCT(kt_entry
);
170 krb5_kt_end_seq_get(context
, keytab
, &kt_cursor
);
172 ZERO_STRUCT(kt_cursor
);
176 for (i
= 0; i
< ARRAY_SIZE(valid_princ_formats
); i
++) {
177 SAFE_FREE(valid_princ_formats
[i
]);
181 if (!number_matched_principals
) {
182 DEBUG(3, ("ads_keytab_verify_ticket: no keytab principals matched expected file service name.\n"));
184 DEBUG(3, ("ads_keytab_verify_ticket: krb5_rd_req failed for all %d matched keytab principals\n",
185 number_matched_principals
));
189 SAFE_FREE(entry_princ_s
);
192 krb5_keytab_entry zero_kt_entry
;
193 ZERO_STRUCT(zero_kt_entry
);
194 if (memcmp(&zero_kt_entry
, &kt_entry
, sizeof(krb5_keytab_entry
))) {
195 smb_krb5_kt_free_entry(context
, &kt_entry
);
200 krb5_kt_cursor zero_csr
;
201 ZERO_STRUCT(zero_csr
);
202 if ((memcmp(&kt_cursor
, &zero_csr
, sizeof(krb5_kt_cursor
)) != 0) && keytab
) {
203 krb5_kt_end_seq_get(context
, keytab
, &kt_cursor
);
208 krb5_kt_close(context
, keytab
);
214 /**********************************************************************************
215 Try to verify a ticket using the secrets.tdb.
216 ***********************************************************************************/
218 static krb5_error_code
ads_secrets_verify_ticket(krb5_context context
,
219 krb5_auth_context auth_context
,
220 krb5_principal host_princ
,
221 const DATA_BLOB
*ticket
,
222 krb5_ticket
**pp_tkt
,
223 krb5_keyblock
**keyblock
,
224 krb5_error_code
*perr
)
226 krb5_error_code ret
= 0;
227 bool auth_ok
= False
;
228 char *password_s
= NULL
;
230 krb5_enctype enctypes
[] = {
231 #if defined(ENCTYPE_ARCFOUR_HMAC)
232 ENCTYPE_ARCFOUR_HMAC
,
246 if (!secrets_init()) {
247 DEBUG(1,("ads_secrets_verify_ticket: secrets_init failed\n"));
248 *perr
= KRB5_CONFIG_CANTOPEN
;
252 password_s
= secrets_fetch_machine_password(lp_workgroup(), NULL
, NULL
);
254 DEBUG(1,("ads_secrets_verify_ticket: failed to fetch machine password\n"));
255 *perr
= KRB5_LIBOS_CANTREADPWD
;
259 password
.data
= password_s
;
260 password
.length
= strlen(password_s
);
262 /* CIFS doesn't use addresses in tickets. This would break NAT. JRA */
264 packet
.length
= ticket
->length
;
265 packet
.data
= (char *)ticket
->data
;
267 /* We need to setup a auth context with each possible encoding type in turn. */
268 for (i
=0;enctypes
[i
];i
++) {
269 krb5_keyblock
*key
= NULL
;
271 if (!(key
= SMB_MALLOC_P(krb5_keyblock
))) {
276 if (create_kerberos_key_from_string(context
, host_princ
, &password
, key
, enctypes
[i
], false)) {
281 krb5_auth_con_setuseruserkey(context
, auth_context
, key
);
283 if (!(ret
= krb5_rd_req(context
, &auth_context
, &packet
,
285 NULL
, NULL
, pp_tkt
))) {
286 DEBUG(10,("ads_secrets_verify_ticket: enc type [%u] decrypted message !\n",
287 (unsigned int)enctypes
[i
] ));
289 krb5_copy_keyblock(context
, key
, keyblock
);
290 krb5_free_keyblock(context
, key
);
294 DEBUG((ret
!= KRB5_BAD_ENCTYPE
) ? 3 : 10,
295 ("ads_secrets_verify_ticket: enc type [%u] failed to decrypt with error %s\n",
296 (unsigned int)enctypes
[i
], error_message(ret
)));
298 /* successfully decrypted but ticket is just not valid at the moment */
299 if (ret
== KRB5KRB_AP_ERR_TKT_NYV
||
300 ret
== KRB5KRB_AP_ERR_TKT_EXPIRED
||
301 ret
== KRB5KRB_AP_ERR_SKEW
) {
302 krb5_free_keyblock(context
, key
);
306 krb5_free_keyblock(context
, key
);
311 SAFE_FREE(password_s
);
316 /**********************************************************************************
317 Verify an incoming ticket and parse out the principal name and
318 authorization_data if available.
319 ***********************************************************************************/
321 NTSTATUS
ads_verify_ticket(TALLOC_CTX
*mem_ctx
,
324 const DATA_BLOB
*ticket
,
326 struct PAC_DATA
**pac_data
,
328 DATA_BLOB
*session_key
,
329 bool use_replay_cache
)
331 NTSTATUS sret
= NT_STATUS_LOGON_FAILURE
;
334 krb5_context context
= NULL
;
335 krb5_auth_context auth_context
= NULL
;
337 krb5_ticket
*tkt
= NULL
;
338 krb5_rcache rcache
= NULL
;
339 krb5_keyblock
*keyblock
= NULL
;
341 krb5_error_code ret
= 0;
343 krb5_principal host_princ
= NULL
;
344 krb5_const_principal client_principal
= NULL
;
345 char *host_princ_s
= NULL
;
346 bool auth_ok
= False
;
347 bool got_auth_data
= False
;
348 struct named_mutex
*mutex
= NULL
;
351 ZERO_STRUCT(auth_data
);
355 *ap_rep
= data_blob_null
;
356 *session_key
= data_blob_null
;
358 initialize_krb5_error_table();
359 ret
= krb5_init_context(&context
);
361 DEBUG(1,("ads_verify_ticket: krb5_init_context failed (%s)\n", error_message(ret
)));
362 return NT_STATUS_LOGON_FAILURE
;
365 if (time_offset
!= 0) {
366 krb5_set_real_time(context
, time(NULL
) + time_offset
, 0);
369 ret
= krb5_set_default_realm(context
, realm
);
371 DEBUG(1,("ads_verify_ticket: krb5_set_default_realm failed (%s)\n", error_message(ret
)));
375 /* This whole process is far more complex than I would
376 like. We have to go through all this to allow us to store
377 the secret internally, instead of using /etc/krb5.keytab */
379 ret
= krb5_auth_con_init(context
, &auth_context
);
381 DEBUG(1,("ads_verify_ticket: krb5_auth_con_init failed (%s)\n", error_message(ret
)));
385 krb5_auth_con_getflags( context
, auth_context
, &flags
);
386 if ( !use_replay_cache
) {
387 /* Disable default use of a replay cache */
388 flags
&= ~KRB5_AUTH_CONTEXT_DO_TIME
;
389 krb5_auth_con_setflags( context
, auth_context
, flags
);
392 if (asprintf(&host_princ_s
, "%s$", global_myname()) == -1) {
396 strlower_m(host_princ_s
);
397 ret
= smb_krb5_parse_name(context
, host_princ_s
, &host_princ
);
399 DEBUG(1,("ads_verify_ticket: smb_krb5_parse_name(%s) failed (%s)\n",
400 host_princ_s
, error_message(ret
)));
405 if ( use_replay_cache
) {
407 /* Lock a mutex surrounding the replay as there is no
408 locking in the MIT krb5 code surrounding the replay
411 mutex
= grab_named_mutex(talloc_tos(), "replay cache mutex",
414 DEBUG(1,("ads_verify_ticket: unable to protect "
415 "replay cache with mutex.\n"));
420 /* JRA. We must set the rcache here. This will prevent
423 ret
= krb5_get_server_rcache(context
,
424 krb5_princ_component(context
, host_princ
, 0),
427 DEBUG(1,("ads_verify_ticket: krb5_get_server_rcache "
428 "failed (%s)\n", error_message(ret
)));
432 ret
= krb5_auth_con_setrcache(context
, auth_context
, rcache
);
434 DEBUG(1,("ads_verify_ticket: krb5_auth_con_setrcache "
435 "failed (%s)\n", error_message(ret
)));
440 /* Try secrets.tdb first and fallback to the krb5.keytab if
443 auth_ok
= ads_secrets_verify_ticket(context
, auth_context
, host_princ
,
444 ticket
, &tkt
, &keyblock
, &ret
);
447 (ret
== KRB5KRB_AP_ERR_TKT_NYV
||
448 ret
== KRB5KRB_AP_ERR_TKT_EXPIRED
||
449 ret
== KRB5KRB_AP_ERR_SKEW
)) {
453 if (!auth_ok
&& lp_use_kerberos_keytab()) {
454 auth_ok
= ads_keytab_verify_ticket(context
, auth_context
,
455 ticket
, &tkt
, &keyblock
, &ret
);
458 if ( use_replay_cache
) {
461 /* Heimdal leaks here, if we fix the leak, MIT crashes */
463 krb5_rc_close(context
, rcache
);
470 DEBUG(3,("ads_verify_ticket: krb5_rd_req with auth failed (%s)\n",
471 error_message(ret
)));
472 /* Try map the error return in case it's something like
473 * a clock skew error.
475 sret
= krb5_to_nt_status(ret
);
476 if (NT_STATUS_IS_OK(sret
) || NT_STATUS_EQUAL(sret
,NT_STATUS_UNSUCCESSFUL
)) {
477 sret
= NT_STATUS_LOGON_FAILURE
;
479 DEBUG(10,("ads_verify_ticket: returning error %s\n",
484 authtime
= get_authtime_from_tkt(tkt
);
485 client_principal
= get_principal_from_tkt(tkt
);
487 ret
= krb5_mk_rep(context
, auth_context
, &packet
);
489 DEBUG(3,("ads_verify_ticket: Failed to generate mutual authentication reply (%s)\n",
490 error_message(ret
)));
494 *ap_rep
= data_blob(packet
.data
, packet
.length
);
496 kerberos_free_data_contents(context
, &packet
);
500 get_krb5_smb_session_key(context
, auth_context
, session_key
, True
);
501 dump_data_pw("SMB session key (from ticket)\n", session_key
->data
, session_key
->length
);
504 file_save("/tmp/ticket.dat", ticket
->data
, ticket
->length
);
507 /* continue when no PAC is retrieved or we couldn't decode the PAC
508 (like accounts that have the UF_NO_AUTH_DATA_REQUIRED flag set, or
509 Kerberos tickets encrypted using a DES key) - Guenther */
511 got_auth_data
= get_auth_data_from_tkt(mem_ctx
, &auth_data
, tkt
);
512 if (!got_auth_data
) {
513 DEBUG(3,("ads_verify_ticket: did not retrieve auth data. continuing without PAC\n"));
517 pac_ret
= decode_pac_data(mem_ctx
, &auth_data
, context
, keyblock
, client_principal
, authtime
, pac_data
);
518 if (!NT_STATUS_IS_OK(pac_ret
)) {
519 DEBUG(3,("ads_verify_ticket: failed to decode PAC_DATA: %s\n", nt_errstr(pac_ret
)));
522 data_blob_free(&auth_data
);
526 #if defined(HAVE_KRB5_TKT_ENC_PART2)
528 if (tkt
->enc_part2
) {
529 file_save("/tmp/authdata.dat",
530 tkt
->enc_part2
->authorization_data
[0]->contents
,
531 tkt
->enc_part2
->authorization_data
[0]->length
);
535 if (tkt
->ticket
.authorization_data
) {
536 file_save("/tmp/authdata.dat",
537 tkt
->ticket
.authorization_data
->val
->ad_data
.data
,
538 tkt
->ticket
.authorization_data
->val
->ad_data
.length
);
543 if ((ret
= smb_krb5_unparse_name(context
, client_principal
, principal
))) {
544 DEBUG(3,("ads_verify_ticket: smb_krb5_unparse_name failed (%s)\n",
545 error_message(ret
)));
546 sret
= NT_STATUS_LOGON_FAILURE
;
556 if (!NT_STATUS_IS_OK(sret
)) {
557 data_blob_free(&auth_data
);
560 if (!NT_STATUS_IS_OK(sret
)) {
561 data_blob_free(ap_rep
);
565 krb5_free_principal(context
, host_princ
);
569 krb5_free_keyblock(context
, keyblock
);
573 krb5_free_ticket(context
, tkt
);
576 SAFE_FREE(host_princ_s
);
579 krb5_auth_con_free(context
, auth_context
);
583 krb5_free_context(context
);
589 #endif /* HAVE_KRB5 */