2 Unix SMB/CIFS implementation.
3 simple kerberos5 routines for active directory
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Luke Howard 2002-2003
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
7 Copyright (C) Guenther Deschner 2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "system/network.h"
26 #include "system/kerberos.h"
27 #include "system/time.h"
28 #include "auth/kerberos/kerberos.h"
33 #ifndef HAVE_KRB5_SET_REAL_TIME
35 * This function is not in the Heimdal mainline.
37 krb5_error_code krb5_set_real_time(krb5_context context, int32_t seconds, int32_t microseconds)
42 ret = krb5_us_timeofday(context, &sec, &usec);
46 context->kdc_sec_offset = seconds - sec;
47 context->kdc_usec_offset = microseconds - usec;
53 #if defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES) && !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
54 krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
56 return krb5_set_default_in_tkt_etypes(ctx, enc);
60 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
62 void setup_kaddr( krb5_address *pkaddr, struct sockaddr *paddr)
64 pkaddr->addr_type = KRB5_ADDRESS_INET;
65 pkaddr->address.length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
66 pkaddr->address.data = (char *)&(((struct sockaddr_in *)paddr)->sin_addr);
68 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
70 void setup_kaddr( krb5_address *pkaddr, struct sockaddr *paddr)
72 pkaddr->addrtype = ADDRTYPE_INET;
73 pkaddr->length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
74 pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in *)paddr)->sin_addr);
77 #error UNKNOWN_ADDRTYPE
80 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_USE_ENCTYPE) && defined(HAVE_KRB5_STRING_TO_KEY) && defined(HAVE_KRB5_ENCRYPT_BLOCK)
81 int create_kerberos_key_from_string(krb5_context context,
82 krb5_principal host_princ,
89 krb5_encrypt_block eblock;
91 ret = krb5_principal2salt(context, host_princ, &salt);
93 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
96 krb5_use_enctype(context, &eblock, enctype);
97 ret = krb5_string_to_key(context, &eblock, key, password, &salt);
101 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
102 int create_kerberos_key_from_string(krb5_context context,
103 krb5_principal host_princ,
106 krb5_enctype enctype)
111 ret = krb5_get_pw_salt(context, host_princ, &salt);
113 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret)));
116 ret = krb5_string_to_key_salt(context, enctype, password->data,
118 krb5_free_salt(context, salt);
122 #error UNKNOWN_CREATE_KEY_FUNCTIONS
125 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
126 krb5_error_code get_kerberos_allowed_etypes(krb5_context context,
127 krb5_enctype **enctypes)
129 return krb5_get_permitted_enctypes(context, enctypes);
131 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
132 krb5_error_code get_kerberos_allowed_etypes(krb5_context context,
133 krb5_enctype **enctypes)
135 return krb5_get_default_in_tkt_etypes(context, enctypes);
138 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
141 void free_kerberos_etypes(krb5_context context,
142 krb5_enctype *enctypes)
144 #if defined(HAVE_KRB5_FREE_KTYPES)
145 krb5_free_ktypes(context, enctypes);
153 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
154 krb5_error_code krb5_auth_con_setuseruserkey(krb5_context context,
155 krb5_auth_context auth_context,
156 krb5_keyblock *keyblock)
158 return krb5_auth_con_setkey(context, auth_context, keyblock);
162 BOOL unwrap_pac(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, DATA_BLOB *unwrapped_pac_data)
164 DATA_BLOB pac_contents;
165 struct asn1_data data;
168 if (!auth_data->length) {
172 asn1_load(&data, *auth_data);
173 asn1_start_tag(&data, ASN1_SEQUENCE(0));
174 asn1_start_tag(&data, ASN1_SEQUENCE(0));
175 asn1_start_tag(&data, ASN1_CONTEXT(0));
176 asn1_read_Integer(&data, &data_type);
178 if (data_type != KRB5_AUTHDATA_WIN2K_PAC ) {
179 DEBUG(10,("authorization data is not a Windows PAC (type: %d)\n", data_type));
185 asn1_start_tag(&data, ASN1_CONTEXT(1));
186 asn1_read_OctetString(&data, &pac_contents);
192 *unwrapped_pac_data = data_blob_talloc(mem_ctx, pac_contents.data, pac_contents.length);
194 data_blob_free(&pac_contents);
199 BOOL get_auth_data_from_tkt(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, krb5_ticket *tkt)
201 DATA_BLOB auth_data_wrapped;
202 BOOL got_auth_data_pac = False;
205 #if defined(HAVE_KRB5_TKT_ENC_PART2)
206 if (tkt->enc_part2 && tkt->enc_part2->authorization_data &&
207 tkt->enc_part2->authorization_data[0] &&
208 tkt->enc_part2->authorization_data[0]->length)
210 for (i = 0; tkt->enc_part2->authorization_data[i] != NULL; i++) {
212 if (tkt->enc_part2->authorization_data[i]->ad_type !=
213 KRB5_AUTHDATA_IF_RELEVANT) {
214 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
215 tkt->enc_part2->authorization_data[i]->ad_type));
219 auth_data_wrapped = data_blob(tkt->enc_part2->authorization_data[i]->contents,
220 tkt->enc_part2->authorization_data[i]->length);
222 /* check if it is a PAC */
223 got_auth_data_pac = unwrap_pac(mem_ctx, &auth_data_wrapped, auth_data);
224 data_blob_free(&auth_data_wrapped);
226 if (!got_auth_data_pac) {
231 return got_auth_data_pac;
235 if (tkt->ticket.authorization_data &&
236 tkt->ticket.authorization_data->len)
238 for (i = 0; i < tkt->ticket.authorization_data->len; i++) {
240 if (tkt->ticket.authorization_data->val[i].ad_type !=
241 KRB5_AUTHDATA_IF_RELEVANT) {
242 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
243 tkt->ticket.authorization_data->val[i].ad_type));
247 auth_data_wrapped = data_blob(tkt->ticket.authorization_data->val[i].ad_data.data,
248 tkt->ticket.authorization_data->val[i].ad_data.length);
250 /* check if it is a PAC */
251 got_auth_data_pac = unwrap_pac(mem_ctx, &auth_data_wrapped, auth_data);
252 data_blob_free(&auth_data_wrapped);
254 if (!got_auth_data_pac) {
259 return got_auth_data_pac;
265 krb5_const_principal get_principal_from_tkt(krb5_ticket *tkt)
267 #if defined(HAVE_KRB5_TKT_ENC_PART2)
268 return tkt->enc_part2->client;
274 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
275 void krb5_free_unparsed_name(krb5_context context, char *val)
281 void kerberos_free_data_contents(krb5_context context, krb5_data *pdata)
283 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
285 krb5_free_data_contents(context, pdata);
288 SAFE_FREE(pdata->data);
292 void kerberos_set_creds_enctype(krb5_creds *pcreds, int enctype)
294 #if defined(HAVE_KRB5_KEYBLOCK_IN_CREDS)
295 KRB5_KEY_TYPE((&pcreds->keyblock)) = enctype;
296 #elif defined(HAVE_KRB5_SESSION_IN_CREDS)
297 KRB5_KEY_TYPE((&pcreds->session)) = enctype;
299 #error UNKNOWN_KEYBLOCK_MEMBER_IN_KRB5_CREDS_STRUCT
303 BOOL kerberos_compatible_enctypes(krb5_context context,
304 krb5_enctype enctype1,
305 krb5_enctype enctype2)
307 #if defined(HAVE_KRB5_C_ENCTYPE_COMPARE)
308 krb5_boolean similar = 0;
310 krb5_c_enctype_compare(context, enctype1, enctype2, &similar);
311 return similar ? True : False;
312 #elif defined(HAVE_KRB5_ENCTYPES_COMPATIBLE_KEYS)
313 return krb5_enctypes_compatible_keys(context, enctype1, enctype2) ? True : False;
317 static BOOL ads_cleanup_expired_creds(krb5_context context,
321 krb5_error_code retval;
322 TALLOC_CTX *mem_ctx = talloc_init("ticket expied time");
327 DEBUG(3, ("Ticket in ccache[%s] expiration %s\n",
328 krb5_cc_default_name(context),
329 http_timestring(mem_ctx, credsp->times.endtime)));
331 talloc_free(mem_ctx);
333 /* we will probably need new tickets if the current ones
334 will expire within 10 seconds.
336 if (credsp->times.endtime >= (time(NULL) + 10))
339 /* heimdal won't remove creds from a file ccache, and
340 perhaps we shouldn't anyway, since internally we
341 use memory ccaches, and a FILE one probably means that
342 we're using creds obtained outside of our exectuable
344 if (strcasecmp_m(krb5_cc_get_type(context, ccache), "FILE") == 0) {
345 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a FILE ccache\n"));
349 retval = krb5_cc_remove_cred(context, ccache, 0, credsp);
351 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
352 error_message(retval)));
353 /* If we have an error in this, we want to display it,
354 but continue as though we deleted it */
360 we can't use krb5_mk_req because w2k wants the service to be in a particular format
362 krb5_error_code ads_krb5_mk_req(krb5_context context,
363 krb5_auth_context *auth_context,
364 const krb5_flags ap_req_options,
365 const char *principal,
369 krb5_error_code retval;
370 krb5_principal server;
374 BOOL creds_ready = False;
376 TALLOC_CTX *mem_ctx = NULL;
378 retval = krb5_parse_name(context, principal, &server);
380 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal));
384 /* obtain ticket & session key */
386 if ((retval = krb5_copy_principal(context, server, &creds.server))) {
387 DEBUG(1,("krb5_copy_principal failed (%s)\n",
388 error_message(retval)));
392 if ((retval = krb5_cc_get_principal(context, ccache, &creds.client))) {
393 /* This can commonly fail on smbd startup with no ticket in the cache.
394 * Report at higher level than 1. */
395 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
396 error_message(retval)));
400 while(!creds_ready) {
401 if ((retval = krb5_get_credentials(context, 0, ccache,
403 DEBUG(1,("ads_krb5_mk_req: krb5_get_credentials failed for %s (%s)\n",
404 principal, error_message(retval)));
408 /* cope with ticket being in the future due to clock skew */
409 if ((unsigned)credsp->times.starttime > time(NULL)) {
410 time_t t = time(NULL);
411 int time_offset =(unsigned)credsp->times.starttime-t;
412 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset));
413 krb5_set_real_time(context, t + time_offset + 1, 0);
416 if (!ads_cleanup_expired_creds(context, ccache, credsp))
420 mem_ctx = talloc_init("ticket expied time");
425 DEBUG(10,("Ticket (%s) in ccache (%s) is valid until: (%s - %d)\n",
426 principal, krb5_cc_default_name(context),
427 http_timestring(mem_ctx, (unsigned)credsp->times.endtime),
428 (unsigned)credsp->times.endtime));
431 retval = krb5_mk_req_extended(context, auth_context, ap_req_options,
432 &in_data, credsp, outbuf);
434 DEBUG(1,("ads_krb5_mk_req: krb5_mk_req_extended failed (%s)\n",
435 error_message(retval)));
438 krb5_free_creds(context, credsp);
441 krb5_free_cred_contents(context, &creds);
444 krb5_free_principal(context, server);
449 krb5_error_code smb_krb5_kt_free_entry(krb5_context context, krb5_keytab_entry *kt_entry)
451 #if defined(HAVE_KRB5_KT_FREE_ENTRY)
452 return krb5_kt_free_entry(context, kt_entry);
453 #elif defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
454 return krb5_free_keytab_entry_contents(context, kt_entry);
456 #error UNKNOWN_KT_FREE_FUNCTION
460 char *smb_get_krb5_error_message(krb5_context context, krb5_error_code code, TALLOC_CTX *mem_ctx)
464 #if defined(HAVE_KRB5_GET_ERROR_STRING) && defined(HAVE_KRB5_FREE_ERROR_STRING)
465 char *context_error = krb5_get_error_string(context);
467 ret = talloc_asprintf(mem_ctx, "%s: %s", error_message(code), context_error);
468 krb5_free_error_string(context, context_error);
472 ret = talloc_strdup(mem_ctx, error_message(code));