s4/asn1: Added torture suite for ASN1
[Samba/ekacnet.git] / source3 / libsmb / clikrb5.c
blob152c23bd150c39f679ef563c8a2fecde0b3ab2ae
1 /*
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-2007
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 3 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, see <http://www.gnu.org/licenses/>.
23 #define KRB5_PRIVATE 1 /* this file uses PRIVATE interfaces! */
24 #define KRB5_DEPRECATED 1 /* this file uses DEPRECATED interfaces! */
26 #include "includes.h"
28 #ifdef HAVE_KRB5
30 #define GSSAPI_CHECKSUM 0x8003 /* Checksum type value for Kerberos */
31 #define GSSAPI_BNDLENGTH 16 /* Bind Length (rfc-1964 pg.3) */
32 #define GSSAPI_CHECKSUM_SIZE (12+GSSAPI_BNDLENGTH)
34 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_FWD_TGT_CREDS) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY)
35 static krb5_error_code ads_krb5_get_fwd_ticket( krb5_context context,
36 krb5_auth_context *auth_context,
37 krb5_creds *credsp,
38 krb5_ccache ccache,
39 krb5_data *authenticator);
40 #endif
42 /**************************************************************
43 Wrappers around kerberos string functions that convert from
44 utf8 -> unix charset and vica versa.
45 **************************************************************/
47 /**************************************************************
48 krb5_parse_name that takes a UNIX charset.
49 **************************************************************/
51 krb5_error_code smb_krb5_parse_name(krb5_context context,
52 const char *name, /* in unix charset */
53 krb5_principal *principal)
55 krb5_error_code ret;
56 char *utf8_name;
57 size_t converted_size;
59 if (!push_utf8_talloc(talloc_tos(), &utf8_name, name, &converted_size)) {
60 return ENOMEM;
63 ret = krb5_parse_name(context, utf8_name, principal);
64 TALLOC_FREE(utf8_name);
65 return ret;
68 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
69 /**************************************************************
70 krb5_parse_name_norealm that takes a UNIX charset.
71 **************************************************************/
73 static krb5_error_code smb_krb5_parse_name_norealm_conv(krb5_context context,
74 const char *name, /* in unix charset */
75 krb5_principal *principal)
77 krb5_error_code ret;
78 char *utf8_name;
79 size_t converted_size;
81 *principal = NULL;
82 if (!push_utf8_talloc(talloc_tos(), &utf8_name, name, &converted_size)) {
83 return ENOMEM;
86 ret = krb5_parse_name_norealm(context, utf8_name, principal);
87 TALLOC_FREE(utf8_name);
88 return ret;
90 #endif
92 /**************************************************************
93 krb5_parse_name that returns a UNIX charset name. Must
94 be freed with talloc_free() call.
95 **************************************************************/
97 krb5_error_code smb_krb5_unparse_name(TALLOC_CTX *mem_ctx,
98 krb5_context context,
99 krb5_const_principal principal,
100 char **unix_name)
102 krb5_error_code ret;
103 char *utf8_name;
104 size_t converted_size;
106 *unix_name = NULL;
107 ret = krb5_unparse_name(context, principal, &utf8_name);
108 if (ret) {
109 return ret;
112 if (!pull_utf8_talloc(mem_ctx, unix_name, utf8_name, &converted_size)) {
113 krb5_free_unparsed_name(context, utf8_name);
114 return ENOMEM;
116 krb5_free_unparsed_name(context, utf8_name);
117 return 0;
120 #ifndef HAVE_KRB5_SET_REAL_TIME
122 * This function is not in the Heimdal mainline.
124 krb5_error_code krb5_set_real_time(krb5_context context, int32_t seconds, int32_t microseconds)
126 krb5_error_code ret;
127 int32_t sec, usec;
129 ret = krb5_us_timeofday(context, &sec, &usec);
130 if (ret)
131 return ret;
133 context->kdc_sec_offset = seconds - sec;
134 context->kdc_usec_offset = microseconds - usec;
136 return 0;
138 #endif
140 #if !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
142 #if defined(HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES)
144 /* With MIT kerberos, we should use krb5_set_default_tgs_enctypes in preference
145 * to krb5_set_default_tgs_ktypes. See
146 * http://lists.samba.org/archive/samba-technical/2006-July/048271.html
148 * If the MIT libraries are not exporting internal symbols, we will end up in
149 * this branch, which is correct. Otherwise we will continue to use the
150 * internal symbol
152 krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
154 return krb5_set_default_tgs_enctypes(ctx, enc);
157 #elif defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES)
159 /* Heimdal */
160 krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
162 return krb5_set_default_in_tkt_etypes(ctx, enc);
165 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES */
167 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_KTYPES */
169 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
170 /* HEIMDAL */
171 bool setup_kaddr( krb5_address *pkaddr, struct sockaddr_storage *paddr)
173 memset(pkaddr, '\0', sizeof(krb5_address));
174 #if defined(HAVE_IPV6) && defined(KRB5_ADDRESS_INET6)
175 if (paddr->ss_family == AF_INET6) {
176 pkaddr->addr_type = KRB5_ADDRESS_INET6;
177 pkaddr->address.length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr);
178 pkaddr->address.data = (char *)&(((struct sockaddr_in6 *)paddr)->sin6_addr);
179 return true;
181 #endif
182 if (paddr->ss_family == AF_INET) {
183 pkaddr->addr_type = KRB5_ADDRESS_INET;
184 pkaddr->address.length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
185 pkaddr->address.data = (char *)&(((struct sockaddr_in *)paddr)->sin_addr);
186 return true;
188 return false;
190 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
191 /* MIT */
192 bool setup_kaddr( krb5_address *pkaddr, struct sockaddr_storage *paddr)
194 memset(pkaddr, '\0', sizeof(krb5_address));
195 #if defined(HAVE_IPV6) && defined(ADDRTYPE_INET6)
196 if (paddr->ss_family == AF_INET6) {
197 pkaddr->addrtype = ADDRTYPE_INET6;
198 pkaddr->length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr);
199 pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in6 *)paddr)->sin6_addr);
200 return true;
202 #endif
203 if (paddr->ss_family == AF_INET) {
204 pkaddr->addrtype = ADDRTYPE_INET;
205 pkaddr->length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
206 pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in *)paddr)->sin_addr);
207 return true;
209 return false;
211 #else
212 #error UNKNOWN_ADDRTYPE
213 #endif
215 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_USE_ENCTYPE) && defined(HAVE_KRB5_STRING_TO_KEY) && defined(HAVE_KRB5_ENCRYPT_BLOCK)
216 static int create_kerberos_key_from_string_direct(krb5_context context,
217 krb5_principal host_princ,
218 krb5_data *password,
219 krb5_keyblock *key,
220 krb5_enctype enctype)
222 int ret = 0;
223 krb5_data salt;
224 krb5_encrypt_block eblock;
226 ret = krb5_principal2salt(context, host_princ, &salt);
227 if (ret) {
228 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
229 return ret;
231 krb5_use_enctype(context, &eblock, enctype);
232 ret = krb5_string_to_key(context, &eblock, key, password, &salt);
233 SAFE_FREE(salt.data);
235 return ret;
237 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
238 static int create_kerberos_key_from_string_direct(krb5_context context,
239 krb5_principal host_princ,
240 krb5_data *password,
241 krb5_keyblock *key,
242 krb5_enctype enctype)
244 int ret;
245 krb5_salt salt;
247 ret = krb5_get_pw_salt(context, host_princ, &salt);
248 if (ret) {
249 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret)));
250 return ret;
253 ret = krb5_string_to_key_salt(context, enctype, (const char *)password->data, salt, key);
254 krb5_free_salt(context, salt);
256 return ret;
258 #else
259 #error UNKNOWN_CREATE_KEY_FUNCTIONS
260 #endif
262 int create_kerberos_key_from_string(krb5_context context,
263 krb5_principal host_princ,
264 krb5_data *password,
265 krb5_keyblock *key,
266 krb5_enctype enctype,
267 bool no_salt)
269 krb5_principal salt_princ = NULL;
270 int ret;
272 * Check if we've determined that the KDC is salting keys for this
273 * principal/enctype in a non-obvious way. If it is, try to match
274 * its behavior.
276 if (no_salt) {
277 KRB5_KEY_DATA(key) = (KRB5_KEY_DATA_CAST *)SMB_MALLOC(password->length);
278 if (!KRB5_KEY_DATA(key)) {
279 return ENOMEM;
281 memcpy(KRB5_KEY_DATA(key), password->data, password->length);
282 KRB5_KEY_LENGTH(key) = password->length;
283 KRB5_KEY_TYPE(key) = enctype;
284 return 0;
286 salt_princ = kerberos_fetch_salt_princ_for_host_princ(context, host_princ, enctype);
287 ret = create_kerberos_key_from_string_direct(context, salt_princ ? salt_princ : host_princ, password, key, enctype);
288 if (salt_princ) {
289 krb5_free_principal(context, salt_princ);
291 return ret;
294 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
295 krb5_error_code get_kerberos_allowed_etypes(krb5_context context,
296 krb5_enctype **enctypes)
298 return krb5_get_permitted_enctypes(context, enctypes);
300 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
301 krb5_error_code get_kerberos_allowed_etypes(krb5_context context,
302 krb5_enctype **enctypes)
304 return krb5_get_default_in_tkt_etypes(context, enctypes);
306 #else
307 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
308 #endif
310 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
311 krb5_error_code krb5_auth_con_setuseruserkey(krb5_context context,
312 krb5_auth_context auth_context,
313 krb5_keyblock *keyblock)
315 return krb5_auth_con_setkey(context, auth_context, keyblock);
317 #endif
319 bool unwrap_edata_ntstatus(TALLOC_CTX *mem_ctx,
320 DATA_BLOB *edata,
321 DATA_BLOB *edata_out)
323 DATA_BLOB edata_contents;
324 ASN1_DATA *data;
325 int edata_type;
327 if (!edata->length) {
328 return False;
331 data = asn1_init(mem_ctx);
332 if (data == NULL) {
333 return false;
336 asn1_load(data, *edata);
337 asn1_start_tag(data, ASN1_SEQUENCE(0));
338 asn1_start_tag(data, ASN1_CONTEXT(1));
339 asn1_read_Integer(data, &edata_type);
341 if (edata_type != KRB5_PADATA_PW_SALT) {
342 DEBUG(0,("edata is not of required type %d but of type %d\n",
343 KRB5_PADATA_PW_SALT, edata_type));
344 asn1_free(data);
345 return False;
348 asn1_start_tag(data, ASN1_CONTEXT(2));
349 asn1_read_OctetString(data, talloc_autofree_context(), &edata_contents);
350 asn1_end_tag(data);
351 asn1_end_tag(data);
352 asn1_end_tag(data);
353 asn1_free(data);
355 *edata_out = data_blob_talloc(mem_ctx, edata_contents.data, edata_contents.length);
357 data_blob_free(&edata_contents);
359 return True;
363 bool unwrap_pac(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, DATA_BLOB *unwrapped_pac_data)
365 DATA_BLOB pac_contents;
366 ASN1_DATA *data;
367 int data_type;
369 if (!auth_data->length) {
370 return False;
373 data = asn1_init(mem_ctx);
374 if (data == NULL) {
375 return false;
378 asn1_load(data, *auth_data);
379 asn1_start_tag(data, ASN1_SEQUENCE(0));
380 asn1_start_tag(data, ASN1_SEQUENCE(0));
381 asn1_start_tag(data, ASN1_CONTEXT(0));
382 asn1_read_Integer(data, &data_type);
384 if (data_type != KRB5_AUTHDATA_WIN2K_PAC ) {
385 DEBUG(10,("authorization data is not a Windows PAC (type: %d)\n", data_type));
386 asn1_free(data);
387 return False;
390 asn1_end_tag(data);
391 asn1_start_tag(data, ASN1_CONTEXT(1));
392 asn1_read_OctetString(data, talloc_autofree_context(), &pac_contents);
393 asn1_end_tag(data);
394 asn1_end_tag(data);
395 asn1_end_tag(data);
396 asn1_free(data);
398 *unwrapped_pac_data = data_blob_talloc(mem_ctx, pac_contents.data, pac_contents.length);
400 data_blob_free(&pac_contents);
402 return True;
405 bool get_auth_data_from_tkt(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, krb5_ticket *tkt)
407 DATA_BLOB auth_data_wrapped;
408 bool got_auth_data_pac = False;
409 int i;
411 #if defined(HAVE_KRB5_TKT_ENC_PART2)
412 if (tkt->enc_part2 && tkt->enc_part2->authorization_data &&
413 tkt->enc_part2->authorization_data[0] &&
414 tkt->enc_part2->authorization_data[0]->length)
416 for (i = 0; tkt->enc_part2->authorization_data[i] != NULL; i++) {
418 if (tkt->enc_part2->authorization_data[i]->ad_type !=
419 KRB5_AUTHDATA_IF_RELEVANT) {
420 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
421 tkt->enc_part2->authorization_data[i]->ad_type));
422 continue;
425 auth_data_wrapped = data_blob(tkt->enc_part2->authorization_data[i]->contents,
426 tkt->enc_part2->authorization_data[i]->length);
428 /* check if it is a PAC */
429 got_auth_data_pac = unwrap_pac(mem_ctx, &auth_data_wrapped, auth_data);
430 data_blob_free(&auth_data_wrapped);
432 if (got_auth_data_pac) {
433 return true;
437 return got_auth_data_pac;
440 #else
441 if (tkt->ticket.authorization_data &&
442 tkt->ticket.authorization_data->len)
444 for (i = 0; i < tkt->ticket.authorization_data->len; i++) {
446 if (tkt->ticket.authorization_data->val[i].ad_type !=
447 KRB5_AUTHDATA_IF_RELEVANT) {
448 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
449 tkt->ticket.authorization_data->val[i].ad_type));
450 continue;
453 auth_data_wrapped = data_blob(tkt->ticket.authorization_data->val[i].ad_data.data,
454 tkt->ticket.authorization_data->val[i].ad_data.length);
456 /* check if it is a PAC */
457 got_auth_data_pac = unwrap_pac(mem_ctx, &auth_data_wrapped, auth_data);
458 data_blob_free(&auth_data_wrapped);
460 if (got_auth_data_pac) {
461 return true;
465 return got_auth_data_pac;
467 #endif
468 return False;
471 krb5_const_principal get_principal_from_tkt(krb5_ticket *tkt)
473 #if defined(HAVE_KRB5_TKT_ENC_PART2)
474 return tkt->enc_part2->client;
475 #else
476 return tkt->client;
477 #endif
480 #if !defined(HAVE_KRB5_LOCATE_KDC)
482 /* krb5_locate_kdc is an internal MIT symbol. MIT are not yet willing to commit
483 * to a public interface for this functionality, so we have to be able to live
484 * without it if the MIT libraries are hiding their internal symbols.
487 #if defined(KRB5_KRBHST_INIT)
488 /* Heimdal */
489 krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm, struct sockaddr **addr_pp, int *naddrs, int get_masters)
491 krb5_krbhst_handle hnd;
492 krb5_krbhst_info *hinfo;
493 krb5_error_code rc;
494 int num_kdcs, i;
495 struct sockaddr *sa;
496 struct addrinfo *ai;
498 *addr_pp = NULL;
499 *naddrs = 0;
501 rc = krb5_krbhst_init(ctx, realm->data, KRB5_KRBHST_KDC, &hnd);
502 if (rc) {
503 DEBUG(0, ("smb_krb5_locate_kdc: krb5_krbhst_init failed (%s)\n", error_message(rc)));
504 return rc;
507 for ( num_kdcs = 0; (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); num_kdcs++)
510 krb5_krbhst_reset(ctx, hnd);
512 if (!num_kdcs) {
513 DEBUG(0, ("smb_krb5_locate_kdc: zero kdcs found !\n"));
514 krb5_krbhst_free(ctx, hnd);
515 return -1;
518 sa = SMB_MALLOC_ARRAY( struct sockaddr, num_kdcs );
519 if (!sa) {
520 DEBUG(0, ("smb_krb5_locate_kdc: malloc failed\n"));
521 krb5_krbhst_free(ctx, hnd);
522 naddrs = 0;
523 return -1;
526 memset(sa, '\0', sizeof(struct sockaddr) * num_kdcs );
528 for (i = 0; i < num_kdcs && (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); i++) {
530 #if defined(HAVE_KRB5_KRBHST_GET_ADDRINFO)
531 rc = krb5_krbhst_get_addrinfo(ctx, hinfo, &ai);
532 if (rc) {
533 DEBUG(0,("krb5_krbhst_get_addrinfo failed: %s\n", error_message(rc)));
534 continue;
536 #endif
537 if (hinfo->ai && hinfo->ai->ai_family == AF_INET)
538 memcpy(&sa[i], hinfo->ai->ai_addr, sizeof(struct sockaddr));
541 krb5_krbhst_free(ctx, hnd);
543 *naddrs = num_kdcs;
544 *addr_pp = sa;
545 return 0;
548 #else /* ! defined(KRB5_KRBHST_INIT) */
550 krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm,
551 struct sockaddr **addr_pp, int *naddrs, int get_masters)
553 DEBUG(0, ("unable to explicitly locate the KDC on this platform\n"));
554 return KRB5_KDC_UNREACH;
557 #endif /* KRB5_KRBHST_INIT */
559 #else /* ! HAVE_KRB5_LOCATE_KDC */
561 krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm,
562 struct sockaddr **addr_pp, int *naddrs, int get_masters)
564 return krb5_locate_kdc(ctx, realm, addr_pp, naddrs, get_masters);
567 #endif /* HAVE_KRB5_LOCATE_KDC */
569 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
570 void krb5_free_unparsed_name(krb5_context context, char *val)
572 SAFE_FREE(val);
574 #endif
576 void kerberos_free_data_contents(krb5_context context, krb5_data *pdata)
578 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
579 if (pdata->data) {
580 krb5_free_data_contents(context, pdata);
582 #else
583 SAFE_FREE(pdata->data);
584 #endif
587 void kerberos_set_creds_enctype(krb5_creds *pcreds, int enctype)
589 #if defined(HAVE_KRB5_KEYBLOCK_IN_CREDS)
590 KRB5_KEY_TYPE((&pcreds->keyblock)) = enctype;
591 #elif defined(HAVE_KRB5_SESSION_IN_CREDS)
592 KRB5_KEY_TYPE((&pcreds->session)) = enctype;
593 #else
594 #error UNKNOWN_KEYBLOCK_MEMBER_IN_KRB5_CREDS_STRUCT
595 #endif
598 bool kerberos_compatible_enctypes(krb5_context context,
599 krb5_enctype enctype1,
600 krb5_enctype enctype2)
602 #if defined(HAVE_KRB5_C_ENCTYPE_COMPARE)
603 krb5_boolean similar = 0;
605 krb5_c_enctype_compare(context, enctype1, enctype2, &similar);
606 return similar ? True : False;
607 #elif defined(HAVE_KRB5_ENCTYPES_COMPATIBLE_KEYS)
608 return krb5_enctypes_compatible_keys(context, enctype1, enctype2) ? True : False;
609 #endif
612 static bool ads_cleanup_expired_creds(krb5_context context,
613 krb5_ccache ccache,
614 krb5_creds *credsp)
616 krb5_error_code retval;
617 const char *cc_type = krb5_cc_get_type(context, ccache);
619 DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
620 cc_type, krb5_cc_get_name(context, ccache),
621 http_timestring(talloc_tos(), credsp->times.endtime)));
623 /* we will probably need new tickets if the current ones
624 will expire within 10 seconds.
626 if (credsp->times.endtime >= (time(NULL) + 10))
627 return False;
629 /* heimdal won't remove creds from a file ccache, and
630 perhaps we shouldn't anyway, since internally we
631 use memory ccaches, and a FILE one probably means that
632 we're using creds obtained outside of our exectuable
634 if (strequal(cc_type, "FILE")) {
635 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type));
636 return False;
639 retval = krb5_cc_remove_cred(context, ccache, 0, credsp);
640 if (retval) {
641 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
642 error_message(retval)));
643 /* If we have an error in this, we want to display it,
644 but continue as though we deleted it */
646 return True;
650 we can't use krb5_mk_req because w2k wants the service to be in a particular format
652 static krb5_error_code ads_krb5_mk_req(krb5_context context,
653 krb5_auth_context *auth_context,
654 const krb5_flags ap_req_options,
655 const char *principal,
656 krb5_ccache ccache,
657 krb5_data *outbuf,
658 time_t *expire_time)
660 krb5_error_code retval;
661 krb5_principal server;
662 krb5_creds * credsp;
663 krb5_creds creds;
664 krb5_data in_data;
665 bool creds_ready = False;
666 int i = 0, maxtries = 3;
668 ZERO_STRUCT(in_data);
670 retval = smb_krb5_parse_name(context, principal, &server);
671 if (retval) {
672 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal));
673 return retval;
676 /* obtain ticket & session key */
677 ZERO_STRUCT(creds);
678 if ((retval = krb5_copy_principal(context, server, &creds.server))) {
679 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n",
680 error_message(retval)));
681 goto cleanup_princ;
684 if ((retval = krb5_cc_get_principal(context, ccache, &creds.client))) {
685 /* This can commonly fail on smbd startup with no ticket in the cache.
686 * Report at higher level than 1. */
687 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
688 error_message(retval)));
689 goto cleanup_creds;
692 while (!creds_ready && (i < maxtries)) {
694 if ((retval = krb5_get_credentials(context, 0, ccache,
695 &creds, &credsp))) {
696 DEBUG(1,("ads_krb5_mk_req: krb5_get_credentials failed for %s (%s)\n",
697 principal, error_message(retval)));
698 goto cleanup_creds;
701 /* cope with ticket being in the future due to clock skew */
702 if ((unsigned)credsp->times.starttime > time(NULL)) {
703 time_t t = time(NULL);
704 int time_offset =(int)((unsigned)credsp->times.starttime-t);
705 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset));
706 krb5_set_real_time(context, t + time_offset + 1, 0);
709 if (!ads_cleanup_expired_creds(context, ccache, credsp)) {
710 creds_ready = True;
713 i++;
716 DEBUG(10,("ads_krb5_mk_req: Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
717 principal, krb5_cc_get_type(context, ccache), krb5_cc_get_name(context, ccache),
718 http_timestring(talloc_tos(), (unsigned)credsp->times.endtime),
719 (unsigned)credsp->times.endtime));
721 if (expire_time) {
722 *expire_time = (time_t)credsp->times.endtime;
725 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_FWD_TGT_CREDS) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY)
726 if( credsp->ticket_flags & TKT_FLG_OK_AS_DELEGATE ) {
727 /* Fetch a forwarded TGT from the KDC so that we can hand off a 2nd ticket
728 as part of the kerberos exchange. */
730 DEBUG( 3, ("ads_krb5_mk_req: server marked as OK to delegate to, building forwardable TGT\n") );
732 if( *auth_context == NULL ) {
733 /* Allocate if it has not yet been allocated. */
734 retval = krb5_auth_con_init( context, auth_context );
735 if (retval) {
736 DEBUG(1,("ads_krb5_mk_req: krb5_auth_con_init failed (%s)\n",
737 error_message(retval)));
738 goto cleanup_creds;
742 retval = krb5_auth_con_setuseruserkey( context, *auth_context, &credsp->keyblock );
743 if (retval) {
744 DEBUG(1,("ads_krb5_mk_req: krb5_auth_con_setuseruserkey failed (%s)\n",
745 error_message(retval)));
746 goto cleanup_creds;
749 /* Must use a subkey for forwarded tickets. */
750 retval = krb5_auth_con_setflags( context, *auth_context, KRB5_AUTH_CONTEXT_USE_SUBKEY);
751 if (retval) {
752 DEBUG(1,("ads_krb5_mk_req: krb5_auth_con_setflags failed (%s)\n",
753 error_message(retval)));
754 goto cleanup_creds;
757 retval = ads_krb5_get_fwd_ticket( context,
758 auth_context,
759 credsp,
760 ccache,
761 &in_data );
762 if (retval) {
763 DEBUG( 3, ("ads_krb5_get_fwd_ticket failed (%s)\n",
764 error_message( retval ) ) );
767 * This is not fatal. Delete the *auth_context and continue
768 * with krb5_mk_req_extended to get a non-forwardable ticket.
771 if (in_data.data) {
772 free( in_data.data );
773 in_data.data = NULL;
774 in_data.length = 0;
776 krb5_auth_con_free(context, *auth_context);
777 *auth_context = NULL;
780 #endif
782 retval = krb5_mk_req_extended(context, auth_context, ap_req_options,
783 &in_data, credsp, outbuf);
784 if (retval) {
785 DEBUG(1,("ads_krb5_mk_req: krb5_mk_req_extended failed (%s)\n",
786 error_message(retval)));
789 if (in_data.data) {
790 free( in_data.data );
791 in_data.length = 0;
794 krb5_free_creds(context, credsp);
796 cleanup_creds:
797 krb5_free_cred_contents(context, &creds);
799 cleanup_princ:
800 krb5_free_principal(context, server);
802 return retval;
806 get a kerberos5 ticket for the given service
808 int cli_krb5_get_ticket(const char *principal, time_t time_offset,
809 DATA_BLOB *ticket, DATA_BLOB *session_key_krb5,
810 uint32 extra_ap_opts, const char *ccname,
811 time_t *tgs_expire)
814 krb5_error_code retval;
815 krb5_data packet;
816 krb5_context context = NULL;
817 krb5_ccache ccdef = NULL;
818 krb5_auth_context auth_context = NULL;
819 krb5_enctype enc_types[] = {
820 #ifdef ENCTYPE_ARCFOUR_HMAC
821 ENCTYPE_ARCFOUR_HMAC,
822 #endif
823 ENCTYPE_DES_CBC_MD5,
824 ENCTYPE_DES_CBC_CRC,
825 ENCTYPE_NULL};
827 initialize_krb5_error_table();
828 retval = krb5_init_context(&context);
829 if (retval) {
830 DEBUG(1,("cli_krb5_get_ticket: krb5_init_context failed (%s)\n",
831 error_message(retval)));
832 goto failed;
835 if (time_offset != 0) {
836 krb5_set_real_time(context, time(NULL) + time_offset, 0);
839 if ((retval = krb5_cc_resolve(context, ccname ?
840 ccname : krb5_cc_default_name(context), &ccdef))) {
841 DEBUG(1,("cli_krb5_get_ticket: krb5_cc_default failed (%s)\n",
842 error_message(retval)));
843 goto failed;
846 if ((retval = krb5_set_default_tgs_ktypes(context, enc_types))) {
847 DEBUG(1,("cli_krb5_get_ticket: krb5_set_default_tgs_ktypes failed (%s)\n",
848 error_message(retval)));
849 goto failed;
852 if ((retval = ads_krb5_mk_req(context,
853 &auth_context,
854 AP_OPTS_USE_SUBKEY | (krb5_flags)extra_ap_opts,
855 principal,
856 ccdef, &packet,
857 tgs_expire))) {
858 goto failed;
861 get_krb5_smb_session_key(context, auth_context, session_key_krb5, False);
863 *ticket = data_blob(packet.data, packet.length);
865 kerberos_free_data_contents(context, &packet);
867 failed:
869 if ( context ) {
870 if (ccdef)
871 krb5_cc_close(context, ccdef);
872 if (auth_context)
873 krb5_auth_con_free(context, auth_context);
874 krb5_free_context(context);
877 return retval;
880 bool get_krb5_smb_session_key(krb5_context context, krb5_auth_context auth_context, DATA_BLOB *session_key, bool remote)
882 krb5_keyblock *skey = NULL;
883 krb5_error_code err = 0;
884 bool ret = false;
886 if (remote) {
887 err = krb5_auth_con_getremotesubkey(context, auth_context, &skey);
888 } else {
889 err = krb5_auth_con_getlocalsubkey(context, auth_context, &skey);
892 if (err || skey == NULL) {
893 DEBUG(10, ("KRB5 error getting session key %d\n", err));
894 goto done;
897 DEBUG(10, ("Got KRB5 session key of length %d\n", (int)KRB5_KEY_LENGTH(skey)));
898 *session_key = data_blob(KRB5_KEY_DATA(skey), KRB5_KEY_LENGTH(skey));
899 dump_data_pw("KRB5 Session Key:\n", session_key->data, session_key->length);
901 ret = true;
903 done:
904 if (skey) {
905 krb5_free_keyblock(context, skey);
908 return ret;
912 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
913 const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i );
915 const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i )
917 static krb5_data kdata;
919 kdata.data = (char *)krb5_principal_get_comp_string(context, principal, i);
920 kdata.length = strlen((const char *)kdata.data);
921 return &kdata;
923 #endif
925 krb5_error_code smb_krb5_kt_free_entry(krb5_context context, krb5_keytab_entry *kt_entry)
927 /* Try krb5_free_keytab_entry_contents first, since
928 * MIT Kerberos >= 1.7 has both krb5_free_keytab_entry_contents and
929 * krb5_kt_free_entry but only has a prototype for the first, while the
930 * second is considered private.
932 #if defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
933 return krb5_free_keytab_entry_contents(context, kt_entry);
934 #elif defined(HAVE_KRB5_KT_FREE_ENTRY)
935 return krb5_kt_free_entry(context, kt_entry);
936 #else
937 #error UNKNOWN_KT_FREE_FUNCTION
938 #endif
941 void smb_krb5_checksum_from_pac_sig(krb5_checksum *cksum,
942 struct PAC_SIGNATURE_DATA *sig)
944 #ifdef HAVE_CHECKSUM_IN_KRB5_CHECKSUM
945 cksum->cksumtype = (krb5_cksumtype)sig->type;
946 cksum->checksum.length = sig->signature.length;
947 cksum->checksum.data = sig->signature.data;
948 #else
949 cksum->checksum_type = (krb5_cksumtype)sig->type;
950 cksum->length = sig->signature.length;
951 cksum->contents = sig->signature.data;
952 #endif
955 krb5_error_code smb_krb5_verify_checksum(krb5_context context,
956 const krb5_keyblock *keyblock,
957 krb5_keyusage usage,
958 krb5_checksum *cksum,
959 uint8 *data,
960 size_t length)
962 krb5_error_code ret;
964 /* verify the checksum */
966 /* welcome to the wonderful world of samba's kerberos abstraction layer:
968 * function heimdal 0.6.1rc3 heimdal 0.7 MIT krb 1.4.2
969 * -----------------------------------------------------------------------------
970 * krb5_c_verify_checksum - works works
971 * krb5_verify_checksum works (6 args) works (6 args) broken (7 args)
974 #if defined(HAVE_KRB5_C_VERIFY_CHECKSUM)
976 krb5_boolean checksum_valid = False;
977 krb5_data input;
979 input.data = (char *)data;
980 input.length = length;
982 ret = krb5_c_verify_checksum(context,
983 keyblock,
984 usage,
985 &input,
986 cksum,
987 &checksum_valid);
988 if (ret) {
989 DEBUG(3,("smb_krb5_verify_checksum: krb5_c_verify_checksum() failed: %s\n",
990 error_message(ret)));
991 return ret;
994 if (!checksum_valid)
995 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
998 #elif KRB5_VERIFY_CHECKSUM_ARGS == 6 && defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CRYPTO) && defined(HAVE_KRB5_CRYPTO_DESTROY)
1000 /* Warning: MIT's krb5_verify_checksum cannot be used as it will use a key
1001 * without enctype and it ignores any key_usage types - Guenther */
1005 krb5_crypto crypto;
1006 ret = krb5_crypto_init(context,
1007 keyblock,
1009 &crypto);
1010 if (ret) {
1011 DEBUG(0,("smb_krb5_verify_checksum: krb5_crypto_init() failed: %s\n",
1012 error_message(ret)));
1013 return ret;
1016 ret = krb5_verify_checksum(context,
1017 crypto,
1018 usage,
1019 data,
1020 length,
1021 cksum);
1023 krb5_crypto_destroy(context, crypto);
1026 #else
1027 #error UNKNOWN_KRB5_VERIFY_CHECKSUM_FUNCTION
1028 #endif
1030 return ret;
1033 time_t get_authtime_from_tkt(krb5_ticket *tkt)
1035 #if defined(HAVE_KRB5_TKT_ENC_PART2)
1036 return tkt->enc_part2->times.authtime;
1037 #else
1038 return tkt->ticket.authtime;
1039 #endif
1042 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
1043 static int get_kvno_from_ap_req(krb5_ap_req *ap_req)
1045 #ifdef HAVE_TICKET_POINTER_IN_KRB5_AP_REQ /* MIT */
1046 if (ap_req->ticket->enc_part.kvno)
1047 return ap_req->ticket->enc_part.kvno;
1048 #else /* Heimdal */
1049 if (ap_req->ticket.enc_part.kvno)
1050 return *ap_req->ticket.enc_part.kvno;
1051 #endif
1052 return 0;
1055 static krb5_enctype get_enctype_from_ap_req(krb5_ap_req *ap_req)
1057 #ifdef HAVE_ETYPE_IN_ENCRYPTEDDATA /* Heimdal */
1058 return ap_req->ticket.enc_part.etype;
1059 #else /* MIT */
1060 return ap_req->ticket->enc_part.enctype;
1061 #endif
1063 #endif /* HAVE_KRB5_DECODE_AP_REQ */
1065 static krb5_error_code
1066 get_key_from_keytab(krb5_context context,
1067 krb5_const_principal server,
1068 krb5_enctype enctype,
1069 krb5_kvno kvno,
1070 krb5_keyblock **out_key)
1072 krb5_keytab_entry entry;
1073 krb5_error_code ret;
1074 krb5_keytab keytab;
1075 char *name = NULL;
1076 krb5_keyblock *keyp;
1078 /* We have to open a new keytab handle here, as MIT does
1079 an implicit open/getnext/close on krb5_kt_get_entry. We
1080 may be in the middle of a keytab enumeration when this is
1081 called. JRA. */
1083 ret = smb_krb5_open_keytab(context, NULL, False, &keytab);
1084 if (ret) {
1085 DEBUG(1,("get_key_from_keytab: smb_krb5_open_keytab failed (%s)\n", error_message(ret)));
1086 return ret;
1089 if ( DEBUGLEVEL >= 10 ) {
1090 if (smb_krb5_unparse_name(talloc_tos(), context, server, &name) == 0) {
1091 DEBUG(10,("get_key_from_keytab: will look for kvno %d, enctype %d and name: %s\n",
1092 kvno, enctype, name));
1093 TALLOC_FREE(name);
1097 ret = krb5_kt_get_entry(context,
1098 keytab,
1099 server,
1100 kvno,
1101 enctype,
1102 &entry);
1104 if (ret) {
1105 DEBUG(0,("get_key_from_keytab: failed to retrieve key: %s\n", error_message(ret)));
1106 goto out;
1109 keyp = KRB5_KT_KEY(&entry);
1111 ret = krb5_copy_keyblock(context, keyp, out_key);
1112 if (ret) {
1113 DEBUG(0,("get_key_from_keytab: failed to copy key: %s\n", error_message(ret)));
1114 goto out;
1117 smb_krb5_kt_free_entry(context, &entry);
1119 out:
1120 krb5_kt_close(context, keytab);
1121 return ret;
1124 /* Prototypes */
1126 krb5_error_code smb_krb5_get_keyinfo_from_ap_req(krb5_context context,
1127 const krb5_data *inbuf,
1128 krb5_kvno *kvno,
1129 krb5_enctype *enctype)
1131 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
1133 krb5_error_code ret;
1134 krb5_ap_req ap_req;
1136 ret = krb5_decode_ap_req(context, inbuf, &ap_req);
1137 if (ret)
1138 return ret;
1140 *kvno = get_kvno_from_ap_req(&ap_req);
1141 *enctype = get_enctype_from_ap_req(&ap_req);
1143 free_AP_REQ(&ap_req);
1144 return 0;
1146 #endif
1148 /* Possibly not an appropriate error code. */
1149 return KRB5KDC_ERR_BADOPTION;
1152 krb5_error_code krb5_rd_req_return_keyblock_from_keytab(krb5_context context,
1153 krb5_auth_context *auth_context,
1154 const krb5_data *inbuf,
1155 krb5_const_principal server,
1156 krb5_keytab keytab,
1157 krb5_flags *ap_req_options,
1158 krb5_ticket **ticket,
1159 krb5_keyblock **keyblock)
1161 krb5_error_code ret;
1162 krb5_kvno kvno;
1163 krb5_enctype enctype;
1164 krb5_keyblock *local_keyblock;
1166 ret = krb5_rd_req(context,
1167 auth_context,
1168 inbuf,
1169 server,
1170 keytab,
1171 ap_req_options,
1172 ticket);
1173 if (ret) {
1174 return ret;
1177 #ifdef KRB5_TICKET_HAS_KEYINFO
1178 enctype = (*ticket)->enc_part.enctype;
1179 kvno = (*ticket)->enc_part.kvno;
1180 #else
1181 ret = smb_krb5_get_keyinfo_from_ap_req(context, inbuf, &kvno, &enctype);
1182 if (ret) {
1183 return ret;
1185 #endif
1187 ret = get_key_from_keytab(context,
1188 server,
1189 enctype,
1190 kvno,
1191 &local_keyblock);
1192 if (ret) {
1193 DEBUG(0,("krb5_rd_req_return_keyblock_from_keytab: failed to call get_key_from_keytab\n"));
1194 goto out;
1197 out:
1198 if (ret && local_keyblock != NULL) {
1199 krb5_free_keyblock(context, local_keyblock);
1200 } else {
1201 *keyblock = local_keyblock;
1204 return ret;
1207 krb5_error_code smb_krb5_parse_name_norealm(krb5_context context,
1208 const char *name,
1209 krb5_principal *principal)
1211 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
1212 return smb_krb5_parse_name_norealm_conv(context, name, principal);
1213 #endif
1215 /* we are cheating here because parse_name will in fact set the realm.
1216 * We don't care as the only caller of smb_krb5_parse_name_norealm
1217 * ignores the realm anyway when calling
1218 * smb_krb5_principal_compare_any_realm later - Guenther */
1220 return smb_krb5_parse_name(context, name, principal);
1223 bool smb_krb5_principal_compare_any_realm(krb5_context context,
1224 krb5_const_principal princ1,
1225 krb5_const_principal princ2)
1227 #ifdef HAVE_KRB5_PRINCIPAL_COMPARE_ANY_REALM
1229 return krb5_principal_compare_any_realm(context, princ1, princ2);
1231 /* krb5_princ_size is a macro in MIT */
1232 #elif defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
1234 int i, len1, len2;
1235 const krb5_data *p1, *p2;
1237 len1 = krb5_princ_size(context, princ1);
1238 len2 = krb5_princ_size(context, princ2);
1240 if (len1 != len2)
1241 return False;
1243 for (i = 0; i < len1; i++) {
1245 p1 = krb5_princ_component(context, CONST_DISCARD(krb5_principal, princ1), i);
1246 p2 = krb5_princ_component(context, CONST_DISCARD(krb5_principal, princ2), i);
1248 if (p1->length != p2->length || memcmp(p1->data, p2->data, p1->length))
1249 return False;
1252 return True;
1253 #else
1254 #error NO_SUITABLE_PRINCIPAL_COMPARE_FUNCTION
1255 #endif
1258 krb5_error_code smb_krb5_renew_ticket(const char *ccache_string, /* FILE:/tmp/krb5cc_0 */
1259 const char *client_string, /* gd@BER.SUSE.DE */
1260 const char *service_string, /* krbtgt/BER.SUSE.DE@BER.SUSE.DE */
1261 time_t *expire_time)
1263 krb5_error_code ret;
1264 krb5_context context = NULL;
1265 krb5_ccache ccache = NULL;
1266 krb5_principal client = NULL;
1267 krb5_creds creds, creds_in, *creds_out = NULL;
1269 ZERO_STRUCT(creds);
1270 ZERO_STRUCT(creds_in);
1272 initialize_krb5_error_table();
1273 ret = krb5_init_context(&context);
1274 if (ret) {
1275 goto done;
1278 if (!ccache_string) {
1279 ccache_string = krb5_cc_default_name(context);
1282 if (!ccache_string) {
1283 ret = EINVAL;
1284 goto done;
1287 DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string));
1289 /* FIXME: we should not fall back to defaults */
1290 ret = krb5_cc_resolve(context, CONST_DISCARD(char *, ccache_string), &ccache);
1291 if (ret) {
1292 goto done;
1295 if (client_string) {
1296 ret = smb_krb5_parse_name(context, client_string, &client);
1297 if (ret) {
1298 goto done;
1300 } else {
1301 ret = krb5_cc_get_principal(context, ccache, &client);
1302 if (ret) {
1303 goto done;
1307 #ifdef HAVE_KRB5_GET_RENEWED_CREDS /* MIT */
1309 ret = krb5_get_renewed_creds(context, &creds, client, ccache, CONST_DISCARD(char *, service_string));
1310 if (ret) {
1311 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
1312 goto done;
1315 #elif defined(HAVE_KRB5_GET_KDC_CRED) /* Heimdal */
1317 krb5_kdc_flags flags;
1318 krb5_realm *client_realm = NULL;
1320 ret = krb5_copy_principal(context, client, &creds_in.client);
1321 if (ret) {
1322 goto done;
1325 if (service_string) {
1326 ret = smb_krb5_parse_name(context, service_string, &creds_in.server);
1327 if (ret) {
1328 goto done;
1330 } else {
1331 /* build tgt service by default */
1332 client_realm = krb5_princ_realm(context, creds_in.client);
1333 if (!client_realm) {
1334 ret = ENOMEM;
1335 goto done;
1337 ret = krb5_make_principal(context, &creds_in.server, *client_realm, KRB5_TGS_NAME, *client_realm, NULL);
1338 if (ret) {
1339 goto done;
1343 flags.i = 0;
1344 flags.b.renewable = flags.b.renew = True;
1346 ret = krb5_get_kdc_cred(context, ccache, flags, NULL, NULL, &creds_in, &creds_out);
1347 if (ret) {
1348 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
1349 goto done;
1352 creds = *creds_out;
1354 #else
1355 #error NO_SUITABLE_KRB5_TICKET_RENEW_FUNCTION_AVAILABLE
1356 #endif
1358 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
1359 ret = krb5_cc_initialize(context, ccache, client);
1360 if (ret) {
1361 goto done;
1364 ret = krb5_cc_store_cred(context, ccache, &creds);
1366 if (expire_time) {
1367 *expire_time = (time_t) creds.times.endtime;
1370 done:
1371 krb5_free_cred_contents(context, &creds_in);
1373 if (creds_out) {
1374 krb5_free_creds(context, creds_out);
1375 } else {
1376 krb5_free_cred_contents(context, &creds);
1379 if (client) {
1380 krb5_free_principal(context, client);
1382 if (ccache) {
1383 krb5_cc_close(context, ccache);
1385 if (context) {
1386 krb5_free_context(context);
1389 return ret;
1392 krb5_error_code smb_krb5_free_addresses(krb5_context context, smb_krb5_addresses *addr)
1394 krb5_error_code ret = 0;
1395 if (addr == NULL) {
1396 return ret;
1398 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1399 krb5_free_addresses(context, addr->addrs);
1400 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1401 ret = krb5_free_addresses(context, addr->addrs);
1402 SAFE_FREE(addr->addrs);
1403 #endif
1404 SAFE_FREE(addr);
1405 addr = NULL;
1406 return ret;
1409 krb5_error_code smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses **kerb_addr)
1411 krb5_error_code ret = 0;
1412 nstring buf;
1413 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1414 krb5_address **addrs = NULL;
1415 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1416 krb5_addresses *addrs = NULL;
1417 #endif
1419 *kerb_addr = (smb_krb5_addresses *)SMB_MALLOC(sizeof(smb_krb5_addresses));
1420 if (*kerb_addr == NULL) {
1421 return ENOMEM;
1424 put_name(buf, global_myname(), ' ', 0x20);
1426 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1428 int num_addr = 2;
1430 addrs = (krb5_address **)SMB_MALLOC(sizeof(krb5_address *) * num_addr);
1431 if (addrs == NULL) {
1432 SAFE_FREE(*kerb_addr);
1433 return ENOMEM;
1436 memset(addrs, 0, sizeof(krb5_address *) * num_addr);
1438 addrs[0] = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
1439 if (addrs[0] == NULL) {
1440 SAFE_FREE(addrs);
1441 SAFE_FREE(*kerb_addr);
1442 return ENOMEM;
1445 addrs[0]->magic = KV5M_ADDRESS;
1446 addrs[0]->addrtype = KRB5_ADDR_NETBIOS;
1447 addrs[0]->length = MAX_NETBIOSNAME_LEN;
1448 addrs[0]->contents = (unsigned char *)SMB_MALLOC(addrs[0]->length);
1449 if (addrs[0]->contents == NULL) {
1450 SAFE_FREE(addrs[0]);
1451 SAFE_FREE(addrs);
1452 SAFE_FREE(*kerb_addr);
1453 return ENOMEM;
1456 memcpy(addrs[0]->contents, buf, addrs[0]->length);
1458 addrs[1] = NULL;
1460 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1462 addrs = (krb5_addresses *)SMB_MALLOC(sizeof(krb5_addresses));
1463 if (addrs == NULL) {
1464 SAFE_FREE(*kerb_addr);
1465 return ENOMEM;
1468 memset(addrs, 0, sizeof(krb5_addresses));
1470 addrs->len = 1;
1471 addrs->val = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
1472 if (addrs->val == NULL) {
1473 SAFE_FREE(addrs);
1474 SAFE_FREE(kerb_addr);
1475 return ENOMEM;
1478 addrs->val[0].addr_type = KRB5_ADDR_NETBIOS;
1479 addrs->val[0].address.length = MAX_NETBIOSNAME_LEN;
1480 addrs->val[0].address.data = (unsigned char *)SMB_MALLOC(addrs->val[0].address.length);
1481 if (addrs->val[0].address.data == NULL) {
1482 SAFE_FREE(addrs->val);
1483 SAFE_FREE(addrs);
1484 SAFE_FREE(*kerb_addr);
1485 return ENOMEM;
1488 memcpy(addrs->val[0].address.data, buf, addrs->val[0].address.length);
1490 #else
1491 #error UNKNOWN_KRB5_ADDRESS_FORMAT
1492 #endif
1493 (*kerb_addr)->addrs = addrs;
1495 return ret;
1498 void smb_krb5_free_error(krb5_context context, krb5_error *krberror)
1500 #ifdef HAVE_KRB5_FREE_ERROR_CONTENTS /* Heimdal */
1501 krb5_free_error_contents(context, krberror);
1502 #else /* MIT */
1503 krb5_free_error(context, krberror);
1504 #endif
1507 krb5_error_code handle_krberror_packet(krb5_context context,
1508 krb5_data *packet)
1510 krb5_error_code ret;
1511 bool got_error_code = False;
1513 DEBUG(10,("handle_krberror_packet: got error packet\n"));
1515 #ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR /* Heimdal */
1517 krb5_error krberror;
1519 if ((ret = krb5_rd_error(context, packet, &krberror))) {
1520 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1521 error_message(ret)));
1522 return ret;
1525 if (krberror.e_data == NULL || krberror.e_data->data == NULL) {
1526 ret = (krb5_error_code) krberror.error_code;
1527 got_error_code = True;
1530 smb_krb5_free_error(context, &krberror);
1532 #else /* MIT */
1534 krb5_error *krberror;
1536 if ((ret = krb5_rd_error(context, packet, &krberror))) {
1537 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1538 error_message(ret)));
1539 return ret;
1542 if (krberror->e_data.data == NULL) {
1543 ret = ERROR_TABLE_BASE_krb5 + (krb5_error_code) krberror->error;
1544 got_error_code = True;
1546 smb_krb5_free_error(context, krberror);
1548 #endif
1549 if (got_error_code) {
1550 DEBUG(5,("handle_krberror_packet: got KERBERR from kpasswd: %s (%d)\n",
1551 error_message(ret), ret));
1553 return ret;
1556 krb5_error_code smb_krb5_get_init_creds_opt_alloc(krb5_context context,
1557 krb5_get_init_creds_opt **opt)
1559 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
1560 /* Heimdal or modern MIT version */
1561 return krb5_get_init_creds_opt_alloc(context, opt);
1562 #else
1563 /* Historical MIT version */
1564 krb5_get_init_creds_opt *my_opt;
1566 *opt = NULL;
1568 if ((my_opt = SMB_MALLOC_P(krb5_get_init_creds_opt)) == NULL) {
1569 return ENOMEM;
1572 krb5_get_init_creds_opt_init(my_opt);
1574 *opt = my_opt;
1575 return 0;
1576 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC */
1579 void smb_krb5_get_init_creds_opt_free(krb5_context context,
1580 krb5_get_init_creds_opt *opt)
1582 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_FREE
1584 #ifdef KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT
1585 /* Modern MIT or Heimdal version */
1586 krb5_get_init_creds_opt_free(context, opt);
1587 #else
1588 /* Heimdal version */
1589 krb5_get_init_creds_opt_free(opt);
1590 #endif /* KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT */
1592 #else /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1593 /* Historical MIT version */
1594 SAFE_FREE(opt);
1595 opt = NULL;
1596 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1599 krb5_enctype smb_get_enctype_from_kt_entry(krb5_keytab_entry *kt_entry)
1601 return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry));
1605 /* caller needs to free etype_s */
1606 krb5_error_code smb_krb5_enctype_to_string(krb5_context context,
1607 krb5_enctype enctype,
1608 char **etype_s)
1610 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
1611 return krb5_enctype_to_string(context, enctype, etype_s); /* Heimdal */
1612 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
1613 char buf[256];
1614 krb5_error_code ret = krb5_enctype_to_string(enctype, buf, 256); /* MIT */
1615 if (ret) {
1616 return ret;
1618 *etype_s = SMB_STRDUP(buf);
1619 if (!*etype_s) {
1620 return ENOMEM;
1622 return ret;
1623 #else
1624 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
1625 #endif
1628 krb5_error_code smb_krb5_mk_error(krb5_context context,
1629 krb5_error_code error_code,
1630 const krb5_principal server,
1631 krb5_data *reply)
1633 #ifdef HAVE_SHORT_KRB5_MK_ERROR_INTERFACE /* MIT */
1635 * The MIT interface is *terrible*.
1636 * We have to construct this ourselves...
1638 krb5_error e;
1640 memset(&e, 0, sizeof(e));
1641 krb5_us_timeofday(context, &e.stime, &e.susec);
1642 e.server = server;
1643 #if defined(krb5_err_base)
1644 e.error = error_code - krb5_err_base;
1645 #elif defined(ERROR_TABLE_BASE_krb5)
1646 e.error = error_code - ERROR_TABLE_BASE_krb5;
1647 #else
1648 e.error = error_code; /* Almost certainly wrong, but what can we do... ? */
1649 #endif
1651 return krb5_mk_error(context, &e, reply);
1652 #else /* Heimdal. */
1653 return krb5_mk_error(context,
1654 error_code,
1655 NULL,
1656 NULL, /* e_data */
1657 NULL,
1658 server,
1659 NULL,
1660 NULL,
1661 reply);
1662 #endif
1665 /**********************************************************************
1666 * Open a krb5 keytab with flags, handles readonly or readwrite access and
1667 * allows to process non-default keytab names.
1668 * @param context krb5_context
1669 * @param keytab_name_req string
1670 * @param write_access bool if writable keytab is required
1671 * @param krb5_keytab pointer to krb5_keytab (close with krb5_kt_close())
1672 * @return krb5_error_code
1673 **********************************************************************/
1675 /* This MAX_NAME_LEN is a constant defined in krb5.h */
1676 #ifndef MAX_KEYTAB_NAME_LEN
1677 #define MAX_KEYTAB_NAME_LEN 1100
1678 #endif
1680 krb5_error_code smb_krb5_open_keytab(krb5_context context,
1681 const char *keytab_name_req,
1682 bool write_access,
1683 krb5_keytab *keytab)
1685 krb5_error_code ret = 0;
1686 TALLOC_CTX *mem_ctx;
1687 char keytab_string[MAX_KEYTAB_NAME_LEN];
1688 char *kt_str = NULL;
1689 bool found_valid_name = False;
1690 const char *pragma = "FILE";
1691 const char *tmp = NULL;
1693 if (!write_access && !keytab_name_req) {
1694 /* caller just wants to read the default keytab readonly, so be it */
1695 return krb5_kt_default(context, keytab);
1698 mem_ctx = talloc_init("smb_krb5_open_keytab");
1699 if (!mem_ctx) {
1700 return ENOMEM;
1703 #ifdef HAVE_WRFILE_KEYTAB
1704 if (write_access) {
1705 pragma = "WRFILE";
1707 #endif
1709 if (keytab_name_req) {
1711 if (strlen(keytab_name_req) > MAX_KEYTAB_NAME_LEN) {
1712 ret = KRB5_CONFIG_NOTENUFSPACE;
1713 goto out;
1716 if ((strncmp(keytab_name_req, "WRFILE:/", 8) == 0) ||
1717 (strncmp(keytab_name_req, "FILE:/", 6) == 0)) {
1718 tmp = keytab_name_req;
1719 goto resolve;
1722 if (keytab_name_req[0] != '/') {
1723 ret = KRB5_KT_BADNAME;
1724 goto out;
1727 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, keytab_name_req);
1728 if (!tmp) {
1729 ret = ENOMEM;
1730 goto out;
1733 goto resolve;
1736 /* we need to handle more complex keytab_strings, like:
1737 * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
1739 ret = krb5_kt_default_name(context, &keytab_string[0], MAX_KEYTAB_NAME_LEN - 2);
1740 if (ret) {
1741 goto out;
1744 DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string));
1746 tmp = talloc_strdup(mem_ctx, keytab_string);
1747 if (!tmp) {
1748 ret = ENOMEM;
1749 goto out;
1752 if (strncmp(tmp, "ANY:", 4) == 0) {
1753 tmp += 4;
1756 memset(&keytab_string, '\0', sizeof(keytab_string));
1758 while (next_token_talloc(mem_ctx, &tmp, &kt_str, ",")) {
1759 if (strncmp(kt_str, "WRFILE:", 7) == 0) {
1760 found_valid_name = True;
1761 tmp = kt_str;
1762 tmp += 7;
1765 if (strncmp(kt_str, "FILE:", 5) == 0) {
1766 found_valid_name = True;
1767 tmp = kt_str;
1768 tmp += 5;
1771 if (tmp[0] == '/') {
1772 /* Treat as a FILE: keytab definition. */
1773 found_valid_name = true;
1776 if (found_valid_name) {
1777 if (tmp[0] != '/') {
1778 ret = KRB5_KT_BADNAME;
1779 goto out;
1782 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, tmp);
1783 if (!tmp) {
1784 ret = ENOMEM;
1785 goto out;
1787 break;
1791 if (!found_valid_name) {
1792 ret = KRB5_KT_UNKNOWN_TYPE;
1793 goto out;
1796 resolve:
1797 DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp));
1798 ret = krb5_kt_resolve(context, tmp, keytab);
1800 out:
1801 TALLOC_FREE(mem_ctx);
1802 return ret;
1805 krb5_error_code smb_krb5_keytab_name(TALLOC_CTX *mem_ctx,
1806 krb5_context context,
1807 krb5_keytab keytab,
1808 const char **keytab_name)
1810 char keytab_string[MAX_KEYTAB_NAME_LEN];
1811 krb5_error_code ret = 0;
1813 ret = krb5_kt_get_name(context, keytab,
1814 keytab_string, MAX_KEYTAB_NAME_LEN - 2);
1815 if (ret) {
1816 return ret;
1819 *keytab_name = talloc_strdup(mem_ctx, keytab_string);
1820 if (!*keytab_name) {
1821 return ENOMEM;
1824 return ret;
1827 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_FWD_TGT_CREDS) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY)
1828 /**************************************************************
1829 Routine: ads_krb5_get_fwd_ticket
1830 Description:
1831 When a service ticket is flagged as trusted
1832 for delegation we should provide a forwardable
1833 ticket so that the remote host can act on our
1834 behalf. This is done by taking the 2nd forwardable
1835 TGT and storing it in the GSS-API authenticator
1836 "checksum". This routine will populate
1837 the krb5_data authenticator with this TGT.
1838 Parameters:
1839 krb5_context context: The kerberos context for this authentication.
1840 krb5_auth_context: The authentication context.
1841 krb5_creds *credsp: The ticket credentials (AS-REP).
1842 krb5_ccache ccache: The credentials cache.
1843 krb5_data &authenticator: The checksum field that will store the TGT, and
1844 authenticator.data must be freed by the caller.
1846 Returns:
1847 krb5_error_code: 0 if no errors, otherwise set.
1848 **************************************************************/
1850 static krb5_error_code ads_krb5_get_fwd_ticket( krb5_context context,
1851 krb5_auth_context *auth_context,
1852 krb5_creds *credsp,
1853 krb5_ccache ccache,
1854 krb5_data *authenticator)
1856 krb5_data fwdData;
1857 krb5_error_code retval = 0;
1858 char *pChksum = NULL;
1859 char *p = NULL;
1861 ZERO_STRUCT(fwdData);
1862 ZERO_STRUCTP(authenticator);
1864 retval = krb5_fwd_tgt_creds(context,/* Krb5 context [in] */
1865 *auth_context, /* Authentication context [in] */
1866 CONST_DISCARD(char *, KRB5_TGS_NAME), /* Ticket service name ("krbtgt") [in] */
1867 credsp->client, /* Client principal for the tgt [in] */
1868 credsp->server, /* Server principal for the tgt [in] */
1869 ccache, /* Credential cache to use for storage [in] */
1870 1, /* Turn on for "Forwardable ticket" [in] */
1871 &fwdData ); /* Resulting response [out] */
1874 if (retval) {
1875 DEBUG(1,("ads_krb5_get_fwd_ticket: krb5_fwd_tgt_creds failed (%s)\n",
1876 error_message(retval)));
1877 goto out;
1880 if ((unsigned int)GSSAPI_CHECKSUM_SIZE + (unsigned int)fwdData.length <
1881 (unsigned int)GSSAPI_CHECKSUM_SIZE) {
1882 retval = EINVAL;
1883 goto out;
1886 /* We're going to allocate a gssChecksum structure with a little
1887 extra data the length of the kerberos credentials length
1888 (APPLICATION 22) so that we can pack it on the end of the structure.
1891 pChksum = (char *)SMB_MALLOC(GSSAPI_CHECKSUM_SIZE + fwdData.length );
1892 if (!pChksum) {
1893 retval = ENOMEM;
1894 goto out;
1897 p = pChksum;
1899 SIVAL(p, 0, GSSAPI_BNDLENGTH);
1900 p += 4;
1902 /* Zero out the bindings fields */
1903 memset(p, '\0', GSSAPI_BNDLENGTH );
1904 p += GSSAPI_BNDLENGTH;
1906 SIVAL(p, 0, GSS_C_DELEG_FLAG );
1907 p += 4;
1908 SSVAL(p, 0, 1 );
1909 p += 2;
1910 SSVAL(p, 0, fwdData.length );
1911 p += 2;
1913 /* Migrate the kerberos KRB_CRED data to the checksum delegation */
1914 memcpy(p, fwdData.data, fwdData.length );
1915 p += fwdData.length;
1917 /* We need to do this in order to allow our GSS-API */
1918 retval = krb5_auth_con_set_req_cksumtype( context, *auth_context, GSSAPI_CHECKSUM );
1919 if (retval) {
1920 goto out;
1923 /* We now have a service ticket, now turn it into an AP-REQ. */
1924 authenticator->length = fwdData.length + GSSAPI_CHECKSUM_SIZE;
1926 /* Caller should call free() when they're done with this. */
1927 authenticator->data = (char *)pChksum;
1929 out:
1931 /* Remove that input data, we never needed it anyway. */
1932 if (fwdData.length > 0) {
1933 krb5_free_data_contents( context, &fwdData );
1936 return retval;
1938 #endif
1940 #else /* HAVE_KRB5 */
1941 /* this saves a few linking headaches */
1942 int cli_krb5_get_ticket(const char *principal, time_t time_offset,
1943 DATA_BLOB *ticket, DATA_BLOB *session_key_krb5, uint32 extra_ap_opts,
1944 const char *ccname, time_t *tgs_expire)
1946 DEBUG(0,("NO KERBEROS SUPPORT\n"));
1947 return 1;
1950 #endif