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
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "system/network.h"
25 #include "system/kerberos.h"
26 #include "system/time.h"
27 #include "auth/kerberos/kerberos.h"
31 #ifndef HAVE_KRB5_SET_REAL_TIME
33 * This function is not in the Heimdal mainline.
35 krb5_error_code krb5_set_real_time(krb5_context context, int32_t seconds, int32_t microseconds)
40 ret = krb5_us_timeofday(context, &sec, &usec);
44 context->kdc_sec_offset = seconds - sec;
45 context->kdc_usec_offset = microseconds - usec;
51 #if defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES) && !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
52 krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
54 return krb5_set_default_in_tkt_etypes(ctx, enc);
58 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
60 void setup_kaddr( krb5_address *pkaddr, struct sockaddr *paddr)
62 pkaddr->addr_type = KRB5_ADDRESS_INET;
63 pkaddr->address.length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
64 pkaddr->address.data = (char *)&(((struct sockaddr_in *)paddr)->sin_addr);
66 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
68 void setup_kaddr( krb5_address *pkaddr, struct sockaddr *paddr)
70 pkaddr->addrtype = ADDRTYPE_INET;
71 pkaddr->length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
72 pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in *)paddr)->sin_addr);
75 #error UNKNOWN_ADDRTYPE
78 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_USE_ENCTYPE) && defined(HAVE_KRB5_STRING_TO_KEY) && defined(HAVE_KRB5_ENCRYPT_BLOCK)
79 int create_kerberos_key_from_string(krb5_context context,
80 krb5_principal host_princ,
87 krb5_encrypt_block eblock;
89 ret = krb5_principal2salt(context, host_princ, &salt);
91 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
94 krb5_use_enctype(context, &eblock, enctype);
95 ret = krb5_string_to_key(context, &eblock, key, password, &salt);
99 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
100 int create_kerberos_key_from_string(krb5_context context,
101 krb5_principal host_princ,
104 krb5_enctype enctype)
109 ret = krb5_get_pw_salt(context, host_princ, &salt);
111 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret)));
114 return krb5_string_to_key_salt(context, enctype, password->data,
118 #error UNKNOWN_CREATE_KEY_FUNCTIONS
121 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
122 krb5_error_code get_kerberos_allowed_etypes(krb5_context context,
123 krb5_enctype **enctypes)
125 return krb5_get_permitted_enctypes(context, enctypes);
127 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
128 krb5_error_code get_kerberos_allowed_etypes(krb5_context context,
129 krb5_enctype **enctypes)
131 return krb5_get_default_in_tkt_etypes(context, enctypes);
134 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
137 void free_kerberos_etypes(krb5_context context,
138 krb5_enctype *enctypes)
140 #if defined(HAVE_KRB5_FREE_KTYPES)
141 krb5_free_ktypes(context, enctypes);
149 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
150 krb5_error_code krb5_auth_con_setuseruserkey(krb5_context context,
151 krb5_auth_context auth_context,
152 krb5_keyblock *keyblock)
154 return krb5_auth_con_setkey(context, auth_context, keyblock);
158 DATA_BLOB get_auth_data_from_tkt(TALLOC_CTX *mem_ctx,
161 DATA_BLOB auth_data = data_blob(NULL, 0);
162 #if defined(HAVE_KRB5_TKT_ENC_PART2)
163 if (tkt && tkt->enc_part2
164 && tkt->enc_part2->authorization_data
165 && tkt->enc_part2->authorization_data[0]
166 && tkt->enc_part2->authorization_data[0]->length)
167 auth_data = data_blob(tkt->enc_part2->authorization_data[0]->contents,
168 tkt->enc_part2->authorization_data[0]->length);
170 if (tkt && tkt->ticket.authorization_data && tkt->ticket.authorization_data->len)
171 auth_data = data_blob(tkt->ticket.authorization_data->val->ad_data.data,
172 tkt->ticket.authorization_data->val->ad_data.length);
177 krb5_const_principal get_principal_from_tkt(krb5_ticket *tkt)
179 #if defined(HAVE_KRB5_TKT_ENC_PART2)
180 return tkt->enc_part2->client;
186 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
187 void krb5_free_unparsed_name(krb5_context context, char *val)
193 void kerberos_free_data_contents(krb5_context context, krb5_data *pdata)
195 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
197 krb5_free_data_contents(context, pdata);
200 SAFE_FREE(pdata->data);
204 void kerberos_set_creds_enctype(krb5_creds *pcreds, int enctype)
206 #if defined(HAVE_KRB5_KEYBLOCK_IN_CREDS)
207 KRB5_KEY_TYPE((&pcreds->keyblock)) = enctype;
208 #elif defined(HAVE_KRB5_SESSION_IN_CREDS)
209 KRB5_KEY_TYPE((&pcreds->session)) = enctype;
211 #error UNKNOWN_KEYBLOCK_MEMBER_IN_KRB5_CREDS_STRUCT
215 BOOL kerberos_compatible_enctypes(krb5_context context,
216 krb5_enctype enctype1,
217 krb5_enctype enctype2)
219 #if defined(HAVE_KRB5_C_ENCTYPE_COMPARE)
220 krb5_boolean similar = 0;
222 krb5_c_enctype_compare(context, enctype1, enctype2, &similar);
223 return similar ? True : False;
224 #elif defined(HAVE_KRB5_ENCTYPES_COMPATIBLE_KEYS)
225 return krb5_enctypes_compatible_keys(context, enctype1, enctype2) ? True : False;
229 static BOOL ads_cleanup_expired_creds(krb5_context context,
233 krb5_error_code retval;
234 TALLOC_CTX *mem_ctx = talloc_init("ticket expied time");
239 DEBUG(3, ("Ticket in ccache[%s] expiration %s\n",
240 krb5_cc_default_name(context),
241 http_timestring(mem_ctx, credsp->times.endtime)));
243 talloc_free(mem_ctx);
245 /* we will probably need new tickets if the current ones
246 will expire within 10 seconds.
248 if (credsp->times.endtime >= (time(NULL) + 10))
251 /* heimdal won't remove creds from a file ccache, and
252 perhaps we shouldn't anyway, since internally we
253 use memory ccaches, and a FILE one probably means that
254 we're using creds obtained outside of our exectuable
256 if (strcasecmp_m(krb5_cc_get_type(context, ccache), "FILE") == 0) {
257 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a FILE ccache\n"));
261 retval = krb5_cc_remove_cred(context, ccache, 0, credsp);
263 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
264 error_message(retval)));
265 /* If we have an error in this, we want to display it,
266 but continue as though we deleted it */
272 we can't use krb5_mk_req because w2k wants the service to be in a particular format
274 krb5_error_code ads_krb5_mk_req(krb5_context context,
275 krb5_auth_context *auth_context,
276 const krb5_flags ap_req_options,
277 const char *principal,
281 krb5_error_code retval;
282 krb5_principal server;
286 BOOL creds_ready = False;
288 TALLOC_CTX *mem_ctx = NULL;
290 retval = krb5_parse_name(context, principal, &server);
292 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal));
296 /* obtain ticket & session key */
298 if ((retval = krb5_copy_principal(context, server, &creds.server))) {
299 DEBUG(1,("krb5_copy_principal failed (%s)\n",
300 error_message(retval)));
304 if ((retval = krb5_cc_get_principal(context, ccache, &creds.client))) {
305 /* This can commonly fail on smbd startup with no ticket in the cache.
306 * Report at higher level than 1. */
307 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
308 error_message(retval)));
312 while(!creds_ready) {
313 if ((retval = krb5_get_credentials(context, 0, ccache,
315 DEBUG(1,("ads_krb5_mk_req: krb5_get_credentials failed for %s (%s)\n",
316 principal, error_message(retval)));
320 /* cope with ticket being in the future due to clock skew */
321 if ((unsigned)credsp->times.starttime > time(NULL)) {
322 time_t t = time(NULL);
323 int time_offset =(unsigned)credsp->times.starttime-t;
324 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset));
325 krb5_set_real_time(context, t + time_offset + 1, 0);
328 if (!ads_cleanup_expired_creds(context, ccache, credsp))
332 mem_ctx = talloc_init("ticket expied time");
337 DEBUG(10,("Ticket (%s) in ccache (%s) is valid until: (%s - %d)\n",
338 principal, krb5_cc_default_name(context),
339 http_timestring(mem_ctx, (unsigned)credsp->times.endtime),
340 (unsigned)credsp->times.endtime));
343 retval = krb5_mk_req_extended(context, auth_context, ap_req_options,
344 &in_data, credsp, outbuf);
346 DEBUG(1,("ads_krb5_mk_req: krb5_mk_req_extended failed (%s)\n",
347 error_message(retval)));
350 krb5_free_creds(context, credsp);
353 krb5_free_cred_contents(context, &creds);
356 krb5_free_principal(context, server);
361 krb5_error_code smb_krb5_kt_free_entry(krb5_context context, krb5_keytab_entry *kt_entry)
363 #if defined(HAVE_KRB5_KT_FREE_ENTRY)
364 return krb5_kt_free_entry(context, kt_entry);
365 #elif defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
366 return krb5_free_keytab_entry_contents(context, kt_entry);
368 #error UNKNOWN_KT_FREE_FUNCTION
372 char *smb_get_krb5_error_message(krb5_context context, krb5_error_code code, TALLOC_CTX *mem_ctx)
376 #if defined(HAVE_KRB5_GET_ERROR_STRING) && defined(HAVE_KRB5_FREE_ERROR_STRING)
377 char *context_error = krb5_get_error_string(context);
379 ret = talloc_asprintf(mem_ctx, "%s: %s", error_message(code), context_error);
380 krb5_free_error_string(context, context_error);
384 ret = talloc_strdup(mem_ctx, error_message(code));