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
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 2 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, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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
, krb5_auth_context auth_context
,
41 const DATA_BLOB
*ticket
, krb5_data
*p_packet
, krb5_ticket
**pp_tkt
,
42 krb5_keyblock
**keyblock
)
44 krb5_error_code ret
= 0;
46 krb5_keytab keytab
= NULL
;
47 krb5_kt_cursor kt_cursor
;
48 krb5_keytab_entry kt_entry
;
49 char *valid_princ_formats
[7] = { NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
50 char *entry_princ_s
= NULL
;
51 fstring my_name
, my_fqdn
;
53 int number_matched_principals
= 0;
55 /* Generate the list of principal names which we expect
56 * clients might want to use for authenticating to the file
57 * service. We allow name$,{host,cifs}/{name,fqdn,name.REALM}. */
59 fstrcpy(my_name
, global_myname());
62 name_to_fqdn(my_fqdn
, global_myname());
64 asprintf(&valid_princ_formats
[0], "%s$@%s", my_name
, lp_realm());
65 asprintf(&valid_princ_formats
[1], "host/%s@%s", my_name
, lp_realm());
66 asprintf(&valid_princ_formats
[2], "host/%s@%s", my_fqdn
, lp_realm());
67 asprintf(&valid_princ_formats
[3], "host/%s.%s@%s", my_name
, lp_realm(), lp_realm());
68 asprintf(&valid_princ_formats
[4], "cifs/%s@%s", my_name
, lp_realm());
69 asprintf(&valid_princ_formats
[5], "cifs/%s@%s", my_fqdn
, lp_realm());
70 asprintf(&valid_princ_formats
[6], "cifs/%s.%s@%s", my_name
, lp_realm(), lp_realm());
72 ZERO_STRUCT(kt_entry
);
73 ZERO_STRUCT(kt_cursor
);
75 ret
= krb5_kt_default(context
, &keytab
);
77 DEBUG(1, ("ads_keytab_verify_ticket: krb5_kt_default failed (%s)\n", error_message(ret
)));
81 /* Iterate through the keytab. For each key, if the principal
82 * name case-insensitively matches one of the allowed formats,
83 * try verifying the ticket using that principal. */
85 ret
= krb5_kt_start_seq_get(context
, keytab
, &kt_cursor
);
87 DEBUG(1, ("ads_keytab_verify_ticket: krb5_kt_start_seq_get failed (%s)\n", error_message(ret
)));
91 if (ret
!= KRB5_KT_END
&& ret
!= ENOENT
) {
92 while (!auth_ok
&& (krb5_kt_next_entry(context
, keytab
, &kt_entry
, &kt_cursor
) == 0)) {
93 ret
= krb5_unparse_name(context
, kt_entry
.principal
, &entry_princ_s
);
95 DEBUG(1, ("ads_keytab_verify_ticket: krb5_unparse_name failed (%s)\n", error_message(ret
)));
99 for (i
= 0; i
< sizeof(valid_princ_formats
) / sizeof(valid_princ_formats
[0]); i
++) {
100 if (strequal(entry_princ_s
, valid_princ_formats
[i
])) {
101 number_matched_principals
++;
102 p_packet
->length
= ticket
->length
;
103 p_packet
->data
= (krb5_pointer
)ticket
->data
;
106 ret
= krb5_rd_req_return_keyblock_from_keytab(context
, &auth_context
, p_packet
,
107 kt_entry
.principal
, keytab
,
108 NULL
, pp_tkt
, keyblock
);
111 DEBUG(10,("ads_keytab_verify_ticket: "
112 "krb5_rd_req_return_keyblock_from_keytab(%s) failed: %s\n",
113 entry_princ_s
, error_message(ret
)));
115 /* workaround for MIT:
116 * as krb5_ktfile_get_entry will
117 * explicitly close the
118 * krb5_keytab as soon as
119 * krb5_rd_req has sucessfully
120 * decrypted the ticket but the
121 * ticket is not valid yet (due
122 * to clockskew) there is no
123 * point in querying more
124 * keytab entries - Guenther */
126 if (ret
== KRB5KRB_AP_ERR_TKT_NYV
||
127 ret
== KRB5KRB_AP_ERR_TKT_EXPIRED
) {
131 DEBUG(3,("ads_keytab_verify_ticket: "
132 "krb5_rd_req_return_keyblock_from_keytab succeeded for principal %s\n",
140 /* Free the name we parsed. */
141 krb5_free_unparsed_name(context
, entry_princ_s
);
142 entry_princ_s
= NULL
;
144 /* Free the entry we just read. */
145 smb_krb5_kt_free_entry(context
, &kt_entry
);
146 ZERO_STRUCT(kt_entry
);
148 krb5_kt_end_seq_get(context
, keytab
, &kt_cursor
);
151 ZERO_STRUCT(kt_cursor
);
155 for (i
= 0; i
< sizeof(valid_princ_formats
) / sizeof(valid_princ_formats
[0]); i
++) {
156 SAFE_FREE(valid_princ_formats
[i
]);
160 if (!number_matched_principals
) {
161 DEBUG(3, ("ads_keytab_verify_ticket: no keytab principals matched expected file service name.\n"));
163 DEBUG(3, ("ads_keytab_verify_ticket: krb5_rd_req failed for all %d matched keytab principals\n",
164 number_matched_principals
));
169 krb5_free_unparsed_name(context
, entry_princ_s
);
173 krb5_keytab_entry zero_kt_entry
;
174 ZERO_STRUCT(zero_kt_entry
);
175 if (memcmp(&zero_kt_entry
, &kt_entry
, sizeof(krb5_keytab_entry
))) {
176 smb_krb5_kt_free_entry(context
, &kt_entry
);
181 krb5_kt_cursor zero_csr
;
182 ZERO_STRUCT(zero_csr
);
183 if ((memcmp(&kt_cursor
, &zero_csr
, sizeof(krb5_kt_cursor
)) != 0) && keytab
) {
184 krb5_kt_end_seq_get(context
, keytab
, &kt_cursor
);
189 krb5_kt_close(context
, keytab
);
194 /**********************************************************************************
195 Try to verify a ticket using the secrets.tdb.
196 ***********************************************************************************/
198 static BOOL
ads_secrets_verify_ticket(krb5_context context
, krb5_auth_context auth_context
,
199 krb5_principal host_princ
,
200 const DATA_BLOB
*ticket
, krb5_data
*p_packet
, krb5_ticket
**pp_tkt
,
201 krb5_keyblock
**keyblock
)
203 krb5_error_code ret
= 0;
204 BOOL auth_ok
= False
;
205 char *password_s
= NULL
;
207 krb5_enctype
*enctypes
= NULL
;
210 ZERO_STRUCTP(keyblock
);
212 if (!secrets_init()) {
213 DEBUG(1,("ads_secrets_verify_ticket: secrets_init failed\n"));
217 password_s
= secrets_fetch_machine_password(lp_workgroup(), NULL
, NULL
);
219 DEBUG(1,("ads_secrets_verify_ticket: failed to fetch machine password\n"));
223 password
.data
= password_s
;
224 password
.length
= strlen(password_s
);
226 /* CIFS doesn't use addresses in tickets. This would break NAT. JRA */
228 if ((ret
= get_kerberos_allowed_etypes(context
, &enctypes
))) {
229 DEBUG(1,("ads_secrets_verify_ticket: krb5_get_permitted_enctypes failed (%s)\n",
230 error_message(ret
)));
234 p_packet
->length
= ticket
->length
;
235 p_packet
->data
= (krb5_pointer
)ticket
->data
;
237 /* We need to setup a auth context with each possible encoding type in turn. */
238 for (i
=0;enctypes
[i
];i
++) {
239 krb5_keyblock
*key
= NULL
;
241 if (!(key
= SMB_MALLOC_P(krb5_keyblock
))) {
245 if (create_kerberos_key_from_string(context
, host_princ
, &password
, key
, enctypes
[i
])) {
250 krb5_auth_con_setuseruserkey(context
, auth_context
, key
);
252 if (!(ret
= krb5_rd_req(context
, &auth_context
, p_packet
,
254 NULL
, NULL
, pp_tkt
))) {
255 DEBUG(10,("ads_secrets_verify_ticket: enc type [%u] decrypted message !\n",
256 (unsigned int)enctypes
[i
] ));
258 krb5_copy_keyblock(context
, key
, keyblock
);
259 krb5_free_keyblock(context
, key
);
263 DEBUG((ret
!= KRB5_BAD_ENCTYPE
) ? 3 : 10,
264 ("ads_secrets_verify_ticket: enc type [%u] failed to decrypt with error %s\n",
265 (unsigned int)enctypes
[i
], error_message(ret
)));
267 /* successfully decrypted but ticket is just not valid at the moment */
268 if (ret
== KRB5KRB_AP_ERR_TKT_NYV
||
269 ret
== KRB5KRB_AP_ERR_TKT_EXPIRED
) {
273 krb5_free_keyblock(context
, key
);
279 free_kerberos_etypes(context
, enctypes
);
280 SAFE_FREE(password_s
);
285 /**********************************************************************************
286 Verify an incoming ticket and parse out the principal name and
287 authorization_data if available.
288 ***********************************************************************************/
290 NTSTATUS
ads_verify_ticket(TALLOC_CTX
*mem_ctx
,
291 const char *realm
, const DATA_BLOB
*ticket
,
292 char **principal
, PAC_DATA
**pac_data
,
294 DATA_BLOB
*session_key
)
296 NTSTATUS sret
= NT_STATUS_LOGON_FAILURE
;
299 krb5_context context
= NULL
;
300 krb5_auth_context auth_context
= NULL
;
302 krb5_ticket
*tkt
= NULL
;
303 krb5_rcache rcache
= NULL
;
304 krb5_keyblock
*keyblock
= NULL
;
308 krb5_principal host_princ
= NULL
;
309 krb5_const_principal client_principal
= NULL
;
310 char *host_princ_s
= NULL
;
311 BOOL got_replay_mutex
= False
;
313 BOOL auth_ok
= False
;
314 BOOL got_auth_data
= False
;
317 ZERO_STRUCT(auth_data
);
318 ZERO_STRUCTP(ap_rep
);
319 ZERO_STRUCTP(session_key
);
321 initialize_krb5_error_table();
322 ret
= krb5_init_context(&context
);
324 DEBUG(1,("ads_verify_ticket: krb5_init_context failed (%s)\n", error_message(ret
)));
325 return NT_STATUS_LOGON_FAILURE
;
328 ret
= krb5_set_default_realm(context
, realm
);
330 DEBUG(1,("ads_verify_ticket: krb5_set_default_realm failed (%s)\n", error_message(ret
)));
334 /* This whole process is far more complex than I would
335 like. We have to go through all this to allow us to store
336 the secret internally, instead of using /etc/krb5.keytab */
338 ret
= krb5_auth_con_init(context
, &auth_context
);
340 DEBUG(1,("ads_verify_ticket: krb5_auth_con_init failed (%s)\n", error_message(ret
)));
344 asprintf(&host_princ_s
, "%s$", global_myname());
345 strlower_m(host_princ_s
);
346 ret
= krb5_parse_name(context
, host_princ_s
, &host_princ
);
348 DEBUG(1,("ads_verify_ticket: krb5_parse_name(%s) failed (%s)\n",
349 host_princ_s
, error_message(ret
)));
354 /* Lock a mutex surrounding the replay as there is no locking in the MIT krb5
355 * code surrounding the replay cache... */
357 if (!grab_server_mutex("replay cache mutex")) {
358 DEBUG(1,("ads_verify_ticket: unable to protect replay cache with mutex.\n"));
362 got_replay_mutex
= True
;
365 * JRA. We must set the rcache here. This will prevent replay attacks.
368 ret
= krb5_get_server_rcache(context
, krb5_princ_component(context
, host_princ
, 0), &rcache
);
370 DEBUG(1,("ads_verify_ticket: krb5_get_server_rcache failed (%s)\n", error_message(ret
)));
374 ret
= krb5_auth_con_setrcache(context
, auth_context
, rcache
);
376 DEBUG(1,("ads_verify_ticket: krb5_auth_con_setrcache failed (%s)\n", error_message(ret
)));
380 if (lp_use_kerberos_keytab()) {
381 auth_ok
= ads_keytab_verify_ticket(context
, auth_context
, ticket
, &packet
, &tkt
, &keyblock
);
384 auth_ok
= ads_secrets_verify_ticket(context
, auth_context
, host_princ
,
385 ticket
, &packet
, &tkt
, &keyblock
);
388 release_server_mutex();
389 got_replay_mutex
= False
;
392 /* Heimdal leaks here, if we fix the leak, MIT crashes */
394 krb5_rc_close(context
, rcache
);
399 DEBUG(3,("ads_verify_ticket: krb5_rd_req with auth failed (%s)\n",
400 error_message(ret
)));
404 authtime
= get_authtime_from_tkt(tkt
);
405 client_principal
= get_principal_from_tkt(tkt
);
407 ret
= krb5_mk_rep(context
, auth_context
, &packet
);
409 DEBUG(3,("ads_verify_ticket: Failed to generate mutual authentication reply (%s)\n",
410 error_message(ret
)));
414 *ap_rep
= data_blob(packet
.data
, packet
.length
);
415 SAFE_FREE(packet
.data
);
418 get_krb5_smb_session_key(context
, auth_context
, session_key
, True
);
419 dump_data_pw("SMB session key (from ticket)\n", session_key
->data
, session_key
->length
);
422 file_save("/tmp/ticket.dat", ticket
->data
, ticket
->length
);
425 /* continue when no PAC is retrieved or we couldn't decode the PAC
426 (like accounts that have the UF_NO_AUTH_DATA_REQUIRED flag set, or
427 Kerberos tickets encrypted using a DES key) - Guenther */
429 got_auth_data
= get_auth_data_from_tkt(mem_ctx
, &auth_data
, tkt
);
430 if (!got_auth_data
) {
431 DEBUG(3,("ads_verify_ticket: did not retrieve auth data. continuing without PAC\n"));
434 if (got_auth_data
&& pac_data
!= NULL
) {
436 pac_ret
= decode_pac_data(mem_ctx
, &auth_data
, context
, keyblock
, client_principal
, authtime
, pac_data
);
437 if (!NT_STATUS_IS_OK(pac_ret
)) {
438 DEBUG(3,("ads_verify_ticket: failed to decode PAC_DATA: %s\n", nt_errstr(pac_ret
)));
441 data_blob_free(&auth_data
);
445 #if defined(HAVE_KRB5_TKT_ENC_PART2)
447 if (tkt
->enc_part2
) {
448 file_save("/tmp/authdata.dat",
449 tkt
->enc_part2
->authorization_data
[0]->contents
,
450 tkt
->enc_part2
->authorization_data
[0]->length
);
454 if (tkt
->ticket
.authorization_data
) {
455 file_save("/tmp/authdata.dat",
456 tkt
->ticket
.authorization_data
->val
->ad_data
.data
,
457 tkt
->ticket
.authorization_data
->val
->ad_data
.length
);
462 if ((ret
= krb5_unparse_name(context
, client_principal
, principal
))) {
463 DEBUG(3,("ads_verify_ticket: krb5_unparse_name failed (%s)\n",
464 error_message(ret
)));
465 sret
= NT_STATUS_LOGON_FAILURE
;
473 if (got_replay_mutex
) {
474 release_server_mutex();
477 if (!NT_STATUS_IS_OK(sret
)) {
478 data_blob_free(&auth_data
);
481 if (!NT_STATUS_IS_OK(sret
)) {
482 data_blob_free(ap_rep
);
486 krb5_free_principal(context
, host_princ
);
490 krb5_free_keyblock(context
, keyblock
);
494 krb5_free_ticket(context
, tkt
);
497 SAFE_FREE(host_princ_s
);
500 krb5_auth_con_free(context
, auth_context
);
504 krb5_free_context(context
);
510 #endif /* HAVE_KRB5 */