s3-libsmb: Don't duplicate kerberos service tickets.
[Samba.git] / source3 / libsmb / clikrb5.c
blob6a11def19c02c254cd5d1347dfbf95a6137ea309
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-2009
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 #include "includes.h"
24 #include "smb_krb5.h"
25 #include "../librpc/gen_ndr/krb5pac.h"
26 #include "../lib/util/asn1.h"
27 #include "libsmb/nmblib.h"
29 #ifndef KRB5_AUTHDATA_WIN2K_PAC
30 #define KRB5_AUTHDATA_WIN2K_PAC 128
31 #endif
33 #ifndef KRB5_AUTHDATA_IF_RELEVANT
34 #define KRB5_AUTHDATA_IF_RELEVANT 1
35 #endif
37 #ifdef HAVE_KRB5
39 #define GSSAPI_CHECKSUM 0x8003 /* Checksum type value for Kerberos */
40 #define GSSAPI_BNDLENGTH 16 /* Bind Length (rfc-1964 pg.3) */
41 #define GSSAPI_CHECKSUM_SIZE (4+GSSAPI_BNDLENGTH+4) /* Length of bind length,
42 bind field, flags field. */
44 /* MIT krb5 1.7beta3 (in Ubuntu Karmic) is missing the prototype,
45 but still has the symbol */
46 #if !HAVE_DECL_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE
47 krb5_error_code krb5_auth_con_set_req_cksumtype(
48 krb5_context context,
49 krb5_auth_context auth_context,
50 krb5_cksumtype cksumtype);
51 #endif
53 #ifndef HAVE_KRB5_SET_REAL_TIME
55 * This function is not in the Heimdal mainline.
57 krb5_error_code krb5_set_real_time(krb5_context context, int32_t seconds, int32_t microseconds)
59 krb5_error_code ret;
60 int32_t sec, usec;
62 ret = krb5_us_timeofday(context, &sec, &usec);
63 if (ret)
64 return ret;
66 context->kdc_sec_offset = seconds - sec;
67 context->kdc_usec_offset = microseconds - usec;
69 return 0;
71 #endif
73 #if !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
75 #if defined(HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES)
77 /* With MIT kerberos, we should use krb5_set_default_tgs_enctypes in preference
78 * to krb5_set_default_tgs_ktypes. See
79 * http://lists.samba.org/archive/samba-technical/2006-July/048271.html
81 * If the MIT libraries are not exporting internal symbols, we will end up in
82 * this branch, which is correct. Otherwise we will continue to use the
83 * internal symbol
85 krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
87 return krb5_set_default_tgs_enctypes(ctx, enc);
90 #elif defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES)
92 /* Heimdal */
93 krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
95 return krb5_set_default_in_tkt_etypes(ctx, enc);
98 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES */
100 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_KTYPES */
102 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
103 /* HEIMDAL */
104 bool setup_kaddr( krb5_address *pkaddr, struct sockaddr_storage *paddr)
106 memset(pkaddr, '\0', sizeof(krb5_address));
107 #if defined(HAVE_IPV6) && defined(KRB5_ADDRESS_INET6)
108 if (paddr->ss_family == AF_INET6) {
109 pkaddr->addr_type = KRB5_ADDRESS_INET6;
110 pkaddr->address.length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr);
111 pkaddr->address.data = (char *)&(((struct sockaddr_in6 *)paddr)->sin6_addr);
112 return true;
114 #endif
115 if (paddr->ss_family == AF_INET) {
116 pkaddr->addr_type = KRB5_ADDRESS_INET;
117 pkaddr->address.length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
118 pkaddr->address.data = (char *)&(((struct sockaddr_in *)paddr)->sin_addr);
119 return true;
121 return false;
123 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
124 /* MIT */
125 bool setup_kaddr( krb5_address *pkaddr, struct sockaddr_storage *paddr)
127 memset(pkaddr, '\0', sizeof(krb5_address));
128 #if defined(HAVE_IPV6) && defined(ADDRTYPE_INET6)
129 if (paddr->ss_family == AF_INET6) {
130 pkaddr->addrtype = ADDRTYPE_INET6;
131 pkaddr->length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr);
132 pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in6 *)paddr)->sin6_addr);
133 return true;
135 #endif
136 if (paddr->ss_family == AF_INET) {
137 pkaddr->addrtype = ADDRTYPE_INET;
138 pkaddr->length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
139 pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in *)paddr)->sin_addr);
140 return true;
142 return false;
144 #else
145 #error UNKNOWN_ADDRTYPE
146 #endif
148 int create_kerberos_key_from_string(krb5_context context,
149 krb5_principal host_princ,
150 krb5_data *password,
151 krb5_keyblock *key,
152 krb5_enctype enctype,
153 bool no_salt)
155 krb5_principal salt_princ = NULL;
156 int ret;
158 * Check if we've determined that the KDC is salting keys for this
159 * principal/enctype in a non-obvious way. If it is, try to match
160 * its behavior.
162 if (no_salt) {
163 KRB5_KEY_DATA(key) = (KRB5_KEY_DATA_CAST *)SMB_MALLOC(password->length);
164 if (!KRB5_KEY_DATA(key)) {
165 return ENOMEM;
167 memcpy(KRB5_KEY_DATA(key), password->data, password->length);
168 KRB5_KEY_LENGTH(key) = password->length;
169 KRB5_KEY_TYPE(key) = enctype;
170 return 0;
172 salt_princ = kerberos_fetch_salt_princ_for_host_princ(context, host_princ, enctype);
173 ret = create_kerberos_key_from_string_direct(context, salt_princ ? salt_princ : host_princ, password, key, enctype);
174 if (salt_princ) {
175 krb5_free_principal(context, salt_princ);
177 return ret;
180 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
181 krb5_error_code get_kerberos_allowed_etypes(krb5_context context,
182 krb5_enctype **enctypes)
184 return krb5_get_permitted_enctypes(context, enctypes);
186 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
187 krb5_error_code get_kerberos_allowed_etypes(krb5_context context,
188 krb5_enctype **enctypes)
190 #ifdef HAVE_KRB5_PDU_NONE_DECL
191 return krb5_get_default_in_tkt_etypes(context, KRB5_PDU_NONE, enctypes);
192 #else
193 return krb5_get_default_in_tkt_etypes(context, enctypes);
194 #endif
196 #else
197 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
198 #endif
200 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
201 krb5_error_code krb5_auth_con_setuseruserkey(krb5_context context,
202 krb5_auth_context auth_context,
203 krb5_keyblock *keyblock)
205 return krb5_auth_con_setkey(context, auth_context, keyblock);
207 #endif
209 bool unwrap_edata_ntstatus(TALLOC_CTX *mem_ctx,
210 DATA_BLOB *edata,
211 DATA_BLOB *edata_out)
213 DATA_BLOB edata_contents;
214 ASN1_DATA *data;
215 int edata_type;
217 if (!edata->length) {
218 return False;
221 data = asn1_init(mem_ctx);
222 if (data == NULL) {
223 return false;
226 asn1_load(data, *edata);
227 asn1_start_tag(data, ASN1_SEQUENCE(0));
228 asn1_start_tag(data, ASN1_CONTEXT(1));
229 asn1_read_Integer(data, &edata_type);
231 if (edata_type != KRB5_PADATA_PW_SALT) {
232 DEBUG(0,("edata is not of required type %d but of type %d\n",
233 KRB5_PADATA_PW_SALT, edata_type));
234 asn1_free(data);
235 return False;
238 asn1_start_tag(data, ASN1_CONTEXT(2));
239 asn1_read_OctetString(data, talloc_tos(), &edata_contents);
240 asn1_end_tag(data);
241 asn1_end_tag(data);
242 asn1_end_tag(data);
243 asn1_free(data);
245 *edata_out = data_blob_talloc(mem_ctx, edata_contents.data, edata_contents.length);
247 data_blob_free(&edata_contents);
249 return True;
253 bool unwrap_pac(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, DATA_BLOB *unwrapped_pac_data)
255 DATA_BLOB pac_contents;
256 ASN1_DATA *data;
257 int data_type;
259 if (!auth_data->length) {
260 return False;
263 data = asn1_init(mem_ctx);
264 if (data == NULL) {
265 return false;
268 asn1_load(data, *auth_data);
269 asn1_start_tag(data, ASN1_SEQUENCE(0));
270 asn1_start_tag(data, ASN1_SEQUENCE(0));
271 asn1_start_tag(data, ASN1_CONTEXT(0));
272 asn1_read_Integer(data, &data_type);
274 if (data_type != KRB5_AUTHDATA_WIN2K_PAC ) {
275 DEBUG(10,("authorization data is not a Windows PAC (type: %d)\n", data_type));
276 asn1_free(data);
277 return False;
280 asn1_end_tag(data);
281 asn1_start_tag(data, ASN1_CONTEXT(1));
282 asn1_read_OctetString(data, talloc_tos(), &pac_contents);
283 asn1_end_tag(data);
284 asn1_end_tag(data);
285 asn1_end_tag(data);
286 asn1_free(data);
288 *unwrapped_pac_data = data_blob_talloc(mem_ctx, pac_contents.data, pac_contents.length);
290 data_blob_free(&pac_contents);
292 return True;
295 bool get_auth_data_from_tkt(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, krb5_ticket *tkt)
297 DATA_BLOB auth_data_wrapped;
298 bool got_auth_data_pac = False;
299 int i;
301 #if defined(HAVE_KRB5_TKT_ENC_PART2)
302 if (tkt->enc_part2 && tkt->enc_part2->authorization_data &&
303 tkt->enc_part2->authorization_data[0] &&
304 tkt->enc_part2->authorization_data[0]->length)
306 for (i = 0; tkt->enc_part2->authorization_data[i] != NULL; i++) {
308 if (tkt->enc_part2->authorization_data[i]->ad_type !=
309 KRB5_AUTHDATA_IF_RELEVANT) {
310 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
311 tkt->enc_part2->authorization_data[i]->ad_type));
312 continue;
315 auth_data_wrapped = data_blob(tkt->enc_part2->authorization_data[i]->contents,
316 tkt->enc_part2->authorization_data[i]->length);
318 /* check if it is a PAC */
319 got_auth_data_pac = unwrap_pac(mem_ctx, &auth_data_wrapped, auth_data);
320 data_blob_free(&auth_data_wrapped);
322 if (got_auth_data_pac) {
323 return true;
327 return got_auth_data_pac;
330 #else
331 if (tkt->ticket.authorization_data &&
332 tkt->ticket.authorization_data->len)
334 for (i = 0; i < tkt->ticket.authorization_data->len; i++) {
336 if (tkt->ticket.authorization_data->val[i].ad_type !=
337 KRB5_AUTHDATA_IF_RELEVANT) {
338 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
339 tkt->ticket.authorization_data->val[i].ad_type));
340 continue;
343 auth_data_wrapped = data_blob(tkt->ticket.authorization_data->val[i].ad_data.data,
344 tkt->ticket.authorization_data->val[i].ad_data.length);
346 /* check if it is a PAC */
347 got_auth_data_pac = unwrap_pac(mem_ctx, &auth_data_wrapped, auth_data);
348 data_blob_free(&auth_data_wrapped);
350 if (got_auth_data_pac) {
351 return true;
355 return got_auth_data_pac;
357 #endif
358 return False;
361 krb5_const_principal get_principal_from_tkt(krb5_ticket *tkt)
363 #if defined(HAVE_KRB5_TKT_ENC_PART2)
364 return tkt->enc_part2->client;
365 #else
366 return tkt->client;
367 #endif
370 #if !defined(HAVE_KRB5_LOCATE_KDC)
372 /* krb5_locate_kdc is an internal MIT symbol. MIT are not yet willing to commit
373 * to a public interface for this functionality, so we have to be able to live
374 * without it if the MIT libraries are hiding their internal symbols.
377 #if defined(KRB5_KRBHST_INIT)
378 /* Heimdal */
379 krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm, struct sockaddr **addr_pp, int *naddrs, int get_masters)
381 krb5_krbhst_handle hnd;
382 krb5_krbhst_info *hinfo;
383 krb5_error_code rc;
384 int num_kdcs, i;
385 struct sockaddr *sa;
386 struct addrinfo *ai;
388 *addr_pp = NULL;
389 *naddrs = 0;
391 rc = krb5_krbhst_init(ctx, realm->data, KRB5_KRBHST_KDC, &hnd);
392 if (rc) {
393 DEBUG(0, ("smb_krb5_locate_kdc: krb5_krbhst_init failed (%s)\n", error_message(rc)));
394 return rc;
397 for ( num_kdcs = 0; (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); num_kdcs++)
400 krb5_krbhst_reset(ctx, hnd);
402 if (!num_kdcs) {
403 DEBUG(0, ("smb_krb5_locate_kdc: zero kdcs found !\n"));
404 krb5_krbhst_free(ctx, hnd);
405 return -1;
408 sa = SMB_MALLOC_ARRAY( struct sockaddr, num_kdcs );
409 if (!sa) {
410 DEBUG(0, ("smb_krb5_locate_kdc: malloc failed\n"));
411 krb5_krbhst_free(ctx, hnd);
412 naddrs = 0;
413 return -1;
416 memset(sa, '\0', sizeof(struct sockaddr) * num_kdcs );
418 for (i = 0; i < num_kdcs && (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); i++) {
420 #if defined(HAVE_KRB5_KRBHST_GET_ADDRINFO)
421 rc = krb5_krbhst_get_addrinfo(ctx, hinfo, &ai);
422 if (rc) {
423 DEBUG(0,("krb5_krbhst_get_addrinfo failed: %s\n", error_message(rc)));
424 continue;
426 #endif
427 if (hinfo->ai && hinfo->ai->ai_family == AF_INET)
428 memcpy(&sa[i], hinfo->ai->ai_addr, sizeof(struct sockaddr));
431 krb5_krbhst_free(ctx, hnd);
433 *naddrs = num_kdcs;
434 *addr_pp = sa;
435 return 0;
438 #else /* ! defined(KRB5_KRBHST_INIT) */
440 krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm,
441 struct sockaddr **addr_pp, int *naddrs, int get_masters)
443 DEBUG(0, ("unable to explicitly locate the KDC on this platform\n"));
444 return KRB5_KDC_UNREACH;
447 #endif /* KRB5_KRBHST_INIT */
449 #else /* ! HAVE_KRB5_LOCATE_KDC */
451 krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm,
452 struct sockaddr **addr_pp, int *naddrs, int get_masters)
454 return krb5_locate_kdc(ctx, realm, addr_pp, naddrs, get_masters);
457 #endif /* HAVE_KRB5_LOCATE_KDC */
459 void kerberos_set_creds_enctype(krb5_creds *pcreds, int enctype)
461 #if defined(HAVE_KRB5_KEYBLOCK_IN_CREDS)
462 KRB5_KEY_TYPE((&pcreds->keyblock)) = enctype;
463 #elif defined(HAVE_KRB5_SESSION_IN_CREDS)
464 KRB5_KEY_TYPE((&pcreds->session)) = enctype;
465 #else
466 #error UNKNOWN_KEYBLOCK_MEMBER_IN_KRB5_CREDS_STRUCT
467 #endif
470 bool kerberos_compatible_enctypes(krb5_context context,
471 krb5_enctype enctype1,
472 krb5_enctype enctype2)
474 #if defined(HAVE_KRB5_C_ENCTYPE_COMPARE)
475 krb5_boolean similar = 0;
477 krb5_c_enctype_compare(context, enctype1, enctype2, &similar);
478 return similar ? True : False;
479 #elif defined(HAVE_KRB5_ENCTYPES_COMPATIBLE_KEYS)
480 return krb5_enctypes_compatible_keys(context, enctype1, enctype2) ? True : False;
481 #endif
484 static bool ads_cleanup_expired_creds(krb5_context context,
485 krb5_ccache ccache,
486 krb5_creds *credsp)
488 krb5_error_code retval;
489 const char *cc_type = krb5_cc_get_type(context, ccache);
491 DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
492 cc_type, krb5_cc_get_name(context, ccache),
493 http_timestring(talloc_tos(), credsp->times.endtime)));
495 /* we will probably need new tickets if the current ones
496 will expire within 10 seconds.
498 if (credsp->times.endtime >= (time(NULL) + 10))
499 return False;
501 /* heimdal won't remove creds from a file ccache, and
502 perhaps we shouldn't anyway, since internally we
503 use memory ccaches, and a FILE one probably means that
504 we're using creds obtained outside of our exectuable
506 if (strequal(cc_type, "FILE")) {
507 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type));
508 return False;
511 retval = krb5_cc_remove_cred(context, ccache, 0, credsp);
512 if (retval) {
513 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
514 error_message(retval)));
515 /* If we have an error in this, we want to display it,
516 but continue as though we deleted it */
518 return True;
521 /* Allocate and setup the auth context into the state we need. */
523 static krb5_error_code setup_auth_context(krb5_context context,
524 krb5_auth_context *auth_context)
526 krb5_error_code retval;
528 retval = krb5_auth_con_init(context, auth_context );
529 if (retval) {
530 DEBUG(1,("krb5_auth_con_init failed (%s)\n",
531 error_message(retval)));
532 return retval;
535 /* Ensure this is an addressless ticket. */
536 retval = krb5_auth_con_setaddrs(context, *auth_context, NULL, NULL);
537 if (retval) {
538 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n",
539 error_message(retval)));
542 return retval;
545 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_FWD_TGT_CREDS) && defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE)
546 static krb5_error_code create_gss_checksum(krb5_data *in_data, /* [inout] */
547 uint32_t gss_flags)
549 unsigned int orig_length = in_data->length;
550 unsigned int base_cksum_size = GSSAPI_CHECKSUM_SIZE;
551 char *gss_cksum = NULL;
553 if (orig_length) {
554 /* Extra length field for delgated ticket. */
555 base_cksum_size += 4;
558 if ((unsigned int)base_cksum_size + orig_length <
559 (unsigned int)base_cksum_size) {
560 return EINVAL;
563 gss_cksum = (char *)SMB_MALLOC(base_cksum_size + orig_length);
564 if (gss_cksum == NULL) {
565 return ENOMEM;
568 memset(gss_cksum, '\0', base_cksum_size + orig_length);
569 SIVAL(gss_cksum, 0, GSSAPI_BNDLENGTH);
572 * GSS_C_NO_CHANNEL_BINDINGS means 16 zero bytes.
573 * This matches the behavior of heimdal and mit.
575 * And it is needed to work against some closed source
576 * SMB servers.
578 * See bug #7883
580 memset(&gss_cksum[4], 0x00, GSSAPI_BNDLENGTH);
582 SIVAL(gss_cksum, 20, gss_flags);
584 if (orig_length) {
585 SSVAL(gss_cksum, 24, 1); /* The Delegation Option identifier */
586 SSVAL(gss_cksum, 26, orig_length);
587 /* Copy the kerberos KRB_CRED data */
588 memcpy(gss_cksum + 28, in_data->data, orig_length);
589 free(in_data->data);
590 in_data->data = NULL;
591 in_data->length = 0;
593 in_data->data = gss_cksum;
594 in_data->length = base_cksum_size + orig_length;
595 return 0;
597 #endif
600 we can't use krb5_mk_req because w2k wants the service to be in a particular format
602 static krb5_error_code ads_krb5_mk_req(krb5_context context,
603 krb5_auth_context *auth_context,
604 const krb5_flags ap_req_options,
605 const char *principal,
606 krb5_ccache ccache,
607 krb5_data *outbuf,
608 time_t *expire_time,
609 const char *impersonate_princ_s)
611 krb5_error_code retval;
612 krb5_principal server;
613 krb5_principal impersonate_princ = NULL;
614 krb5_creds * credsp;
615 krb5_creds creds;
616 krb5_data in_data;
617 bool creds_ready = False;
618 int i = 0, maxtries = 3;
620 ZERO_STRUCT(in_data);
622 retval = smb_krb5_parse_name(context, principal, &server);
623 if (retval) {
624 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal));
625 return retval;
628 if (impersonate_princ_s) {
629 retval = smb_krb5_parse_name(context, impersonate_princ_s,
630 &impersonate_princ);
631 if (retval) {
632 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", impersonate_princ_s));
633 goto cleanup_princ;
637 /* obtain ticket & session key */
638 ZERO_STRUCT(creds);
639 if ((retval = krb5_copy_principal(context, server, &creds.server))) {
640 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n",
641 error_message(retval)));
642 goto cleanup_princ;
645 if ((retval = krb5_cc_get_principal(context, ccache, &creds.client))) {
646 /* This can commonly fail on smbd startup with no ticket in the cache.
647 * Report at higher level than 1. */
648 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
649 error_message(retval)));
650 goto cleanup_creds;
653 while (!creds_ready && (i < maxtries)) {
655 if ((retval = smb_krb5_get_credentials(context, ccache,
656 creds.client,
657 creds.server,
658 impersonate_princ,
659 &credsp))) {
660 DEBUG(1,("ads_krb5_mk_req: smb_krb5_get_credentials failed for %s (%s)\n",
661 principal, error_message(retval)));
662 goto cleanup_creds;
665 /* cope with ticket being in the future due to clock skew */
666 if ((unsigned)credsp->times.starttime > time(NULL)) {
667 time_t t = time(NULL);
668 int time_offset =(int)((unsigned)credsp->times.starttime-t);
669 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset));
670 krb5_set_real_time(context, t + time_offset + 1, 0);
673 if (!ads_cleanup_expired_creds(context, ccache, credsp)) {
674 creds_ready = True;
677 i++;
680 DEBUG(10,("ads_krb5_mk_req: Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
681 principal, krb5_cc_get_type(context, ccache), krb5_cc_get_name(context, ccache),
682 http_timestring(talloc_tos(), (unsigned)credsp->times.endtime),
683 (unsigned)credsp->times.endtime));
685 if (expire_time) {
686 *expire_time = (time_t)credsp->times.endtime;
689 /* Allocate the auth_context. */
690 retval = setup_auth_context(context, auth_context);
691 if (retval) {
692 DEBUG(1,("setup_auth_context failed (%s)\n",
693 error_message(retval)));
694 goto cleanup_creds;
697 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_FWD_TGT_CREDS) && defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE)
699 uint32_t gss_flags = 0;
701 if( credsp->ticket_flags & TKT_FLG_OK_AS_DELEGATE ) {
702 /* Fetch a forwarded TGT from the KDC so that we can hand off a 2nd ticket
703 as part of the kerberos exchange. */
705 DEBUG( 3, ("ads_krb5_mk_req: server marked as OK to delegate to, building forwardable TGT\n") );
707 retval = krb5_auth_con_setuseruserkey(context,
708 *auth_context,
709 &credsp->keyblock );
710 if (retval) {
711 DEBUG(1,("krb5_auth_con_setuseruserkey failed (%s)\n",
712 error_message(retval)));
713 goto cleanup_creds;
716 /* Must use a subkey for forwarded tickets. */
717 retval = krb5_auth_con_setflags(context,
718 *auth_context,
719 KRB5_AUTH_CONTEXT_USE_SUBKEY);
720 if (retval) {
721 DEBUG(1,("krb5_auth_con_setflags failed (%s)\n",
722 error_message(retval)));
723 goto cleanup_creds;
726 retval = krb5_fwd_tgt_creds(context,/* Krb5 context [in] */
727 *auth_context, /* Authentication context [in] */
728 discard_const_p(char, KRB5_TGS_NAME), /* Ticket service name ("krbtgt") [in] */
729 credsp->client, /* Client principal for the tgt [in] */
730 credsp->server, /* Server principal for the tgt [in] */
731 ccache, /* Credential cache to use for storage [in] */
732 1, /* Turn on for "Forwardable ticket" [in] */
733 &in_data ); /* Resulting response [out] */
735 if (retval) {
736 DEBUG( 3, ("krb5_fwd_tgt_creds failed (%s)\n",
737 error_message( retval ) ) );
740 * This is not fatal. Delete the *auth_context and continue
741 * with krb5_mk_req_extended to get a non-forwardable ticket.
744 if (in_data.data) {
745 free( in_data.data );
746 in_data.data = NULL;
747 in_data.length = 0;
749 krb5_auth_con_free(context, *auth_context);
750 *auth_context = NULL;
751 retval = setup_auth_context(context, auth_context);
752 if (retval) {
753 DEBUG(1,("setup_auth_context failed (%s)\n",
754 error_message(retval)));
755 goto cleanup_creds;
757 } else {
758 /* We got a delegated ticket. */
759 gss_flags |= GSS_C_DELEG_FLAG;
763 /* Frees and reallocates in_data into a GSS checksum blob. */
764 retval = create_gss_checksum(&in_data, gss_flags);
765 if (retval) {
766 goto cleanup_data;
769 /* We always want GSS-checksum types. */
770 retval = krb5_auth_con_set_req_cksumtype(context, *auth_context, GSSAPI_CHECKSUM );
771 if (retval) {
772 DEBUG(1,("krb5_auth_con_set_req_cksumtype failed (%s)\n",
773 error_message(retval)));
774 goto cleanup_data;
777 #endif
779 retval = krb5_mk_req_extended(context, auth_context, ap_req_options,
780 &in_data, credsp, outbuf);
781 if (retval) {
782 DEBUG(1,("ads_krb5_mk_req: krb5_mk_req_extended failed (%s)\n",
783 error_message(retval)));
786 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_FWD_TGT_CREDS) && defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE)
787 cleanup_data:
788 #endif
790 if (in_data.data) {
791 free( in_data.data );
792 in_data.length = 0;
795 krb5_free_creds(context, credsp);
797 cleanup_creds:
798 krb5_free_cred_contents(context, &creds);
800 cleanup_princ:
801 krb5_free_principal(context, server);
802 if (impersonate_princ) {
803 krb5_free_principal(context, impersonate_princ);
806 return retval;
810 get a kerberos5 ticket for the given service
812 int cli_krb5_get_ticket(TALLOC_CTX *mem_ctx,
813 const char *principal, time_t time_offset,
814 DATA_BLOB *ticket, DATA_BLOB *session_key_krb5,
815 uint32_t extra_ap_opts, const char *ccname,
816 time_t *tgs_expire,
817 const char *impersonate_princ_s)
820 krb5_error_code retval;
821 krb5_data packet;
822 krb5_context context = NULL;
823 krb5_ccache ccdef = NULL;
824 krb5_auth_context auth_context = NULL;
825 krb5_enctype enc_types[] = {
826 ENCTYPE_ARCFOUR_HMAC,
827 ENCTYPE_DES_CBC_MD5,
828 ENCTYPE_DES_CBC_CRC,
829 ENCTYPE_NULL};
831 initialize_krb5_error_table();
832 retval = krb5_init_context(&context);
833 if (retval) {
834 DEBUG(1, ("krb5_init_context failed (%s)\n",
835 error_message(retval)));
836 goto failed;
839 if (time_offset != 0) {
840 krb5_set_real_time(context, time(NULL) + time_offset, 0);
843 if ((retval = krb5_cc_resolve(context, ccname ?
844 ccname : krb5_cc_default_name(context), &ccdef))) {
845 DEBUG(1, ("krb5_cc_default failed (%s)\n",
846 error_message(retval)));
847 goto failed;
850 if ((retval = krb5_set_default_tgs_ktypes(context, enc_types))) {
851 DEBUG(1, ("krb5_set_default_tgs_ktypes failed (%s)\n",
852 error_message(retval)));
853 goto failed;
856 retval = ads_krb5_mk_req(context, &auth_context,
857 AP_OPTS_USE_SUBKEY | (krb5_flags)extra_ap_opts,
858 principal, ccdef, &packet,
859 tgs_expire, impersonate_princ_s);
860 if (retval) {
861 goto failed;
864 get_krb5_smb_session_key(mem_ctx, context, auth_context,
865 session_key_krb5, false);
867 *ticket = data_blob_talloc(mem_ctx, packet.data, packet.length);
869 kerberos_free_data_contents(context, &packet);
871 failed:
873 if (context) {
874 if (ccdef)
875 krb5_cc_close(context, ccdef);
876 if (auth_context)
877 krb5_auth_con_free(context, auth_context);
878 krb5_free_context(context);
881 return retval;
884 bool get_krb5_smb_session_key(TALLOC_CTX *mem_ctx,
885 krb5_context context,
886 krb5_auth_context auth_context,
887 DATA_BLOB *session_key, bool remote)
889 krb5_keyblock *skey = NULL;
890 krb5_error_code err = 0;
891 bool ret = false;
893 if (remote) {
894 err = krb5_auth_con_getremotesubkey(context,
895 auth_context, &skey);
896 } else {
897 err = krb5_auth_con_getlocalsubkey(context,
898 auth_context, &skey);
901 if (err || skey == NULL) {
902 DEBUG(10, ("KRB5 error getting session key %d\n", err));
903 goto done;
906 DEBUG(10, ("Got KRB5 session key of length %d\n",
907 (int)KRB5_KEY_LENGTH(skey)));
909 *session_key = data_blob_talloc(mem_ctx,
910 KRB5_KEY_DATA(skey),
911 KRB5_KEY_LENGTH(skey));
912 dump_data_pw("KRB5 Session Key:\n",
913 session_key->data,
914 session_key->length);
916 ret = true;
918 done:
919 if (skey) {
920 krb5_free_keyblock(context, skey);
923 return ret;
927 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
928 const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i );
930 const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i )
932 static krb5_data kdata;
934 kdata.data = discard_const_p(char, krb5_principal_get_comp_string(context, principal, i));
935 kdata.length = strlen((const char *)kdata.data);
936 return &kdata;
938 #endif
940 time_t get_authtime_from_tkt(krb5_ticket *tkt)
942 #if defined(HAVE_KRB5_TKT_ENC_PART2)
943 return tkt->enc_part2->times.authtime;
944 #else
945 return tkt->ticket.authtime;
946 #endif
949 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
950 static int get_kvno_from_ap_req(krb5_ap_req *ap_req)
952 #ifdef HAVE_TICKET_POINTER_IN_KRB5_AP_REQ /* MIT */
953 if (ap_req->ticket->enc_part.kvno)
954 return ap_req->ticket->enc_part.kvno;
955 #else /* Heimdal */
956 if (ap_req->ticket.enc_part.kvno)
957 return *ap_req->ticket.enc_part.kvno;
958 #endif
959 return 0;
962 static krb5_enctype get_enctype_from_ap_req(krb5_ap_req *ap_req)
964 #ifdef HAVE_ETYPE_IN_ENCRYPTEDDATA /* Heimdal */
965 return ap_req->ticket.enc_part.etype;
966 #else /* MIT */
967 return ap_req->ticket->enc_part.enctype;
968 #endif
970 #endif /* HAVE_KRB5_DECODE_AP_REQ */
972 static krb5_error_code
973 get_key_from_keytab(krb5_context context,
974 krb5_const_principal server,
975 krb5_enctype enctype,
976 krb5_kvno kvno,
977 krb5_keyblock **out_key)
979 krb5_keytab_entry entry;
980 krb5_error_code ret;
981 krb5_keytab keytab;
982 char *name = NULL;
983 krb5_keyblock *keyp;
985 /* We have to open a new keytab handle here, as MIT does
986 an implicit open/getnext/close on krb5_kt_get_entry. We
987 may be in the middle of a keytab enumeration when this is
988 called. JRA. */
990 ret = smb_krb5_open_keytab(context, NULL, False, &keytab);
991 if (ret) {
992 DEBUG(1,("get_key_from_keytab: smb_krb5_open_keytab failed (%s)\n", error_message(ret)));
993 return ret;
996 if ( DEBUGLEVEL >= 10 ) {
997 if (smb_krb5_unparse_name(talloc_tos(), context, server, &name) == 0) {
998 DEBUG(10,("get_key_from_keytab: will look for kvno %d, enctype %d and name: %s\n",
999 kvno, enctype, name));
1000 TALLOC_FREE(name);
1004 ret = krb5_kt_get_entry(context,
1005 keytab,
1006 server,
1007 kvno,
1008 enctype,
1009 &entry);
1011 if (ret) {
1012 DEBUG(0,("get_key_from_keytab: failed to retrieve key: %s\n", error_message(ret)));
1013 goto out;
1016 keyp = KRB5_KT_KEY(&entry);
1018 ret = krb5_copy_keyblock(context, keyp, out_key);
1019 if (ret) {
1020 DEBUG(0,("get_key_from_keytab: failed to copy key: %s\n", error_message(ret)));
1021 goto out;
1024 smb_krb5_kt_free_entry(context, &entry);
1026 out:
1027 krb5_kt_close(context, keytab);
1028 return ret;
1031 /* Prototypes */
1033 krb5_error_code smb_krb5_get_keyinfo_from_ap_req(krb5_context context,
1034 const krb5_data *inbuf,
1035 krb5_kvno *kvno,
1036 krb5_enctype *enctype)
1038 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
1040 krb5_error_code ret;
1041 krb5_ap_req ap_req;
1043 ret = krb5_decode_ap_req(context, inbuf, &ap_req);
1044 if (ret)
1045 return ret;
1047 *kvno = get_kvno_from_ap_req(&ap_req);
1048 *enctype = get_enctype_from_ap_req(&ap_req);
1050 free_AP_REQ(&ap_req);
1051 return 0;
1053 #endif
1055 /* Possibly not an appropriate error code. */
1056 return KRB5KDC_ERR_BADOPTION;
1059 krb5_error_code krb5_rd_req_return_keyblock_from_keytab(krb5_context context,
1060 krb5_auth_context *auth_context,
1061 const krb5_data *inbuf,
1062 krb5_const_principal server,
1063 krb5_keytab keytab,
1064 krb5_flags *ap_req_options,
1065 krb5_ticket **ticket,
1066 krb5_keyblock **keyblock)
1068 krb5_error_code ret;
1069 krb5_kvno kvno;
1070 krb5_enctype enctype;
1071 krb5_keyblock *local_keyblock;
1073 ret = krb5_rd_req(context,
1074 auth_context,
1075 inbuf,
1076 server,
1077 keytab,
1078 ap_req_options,
1079 ticket);
1080 if (ret) {
1081 return ret;
1084 #ifdef KRB5_TICKET_HAS_KEYINFO
1085 enctype = (*ticket)->enc_part.enctype;
1086 kvno = (*ticket)->enc_part.kvno;
1087 #else
1088 ret = smb_krb5_get_keyinfo_from_ap_req(context, inbuf, &kvno, &enctype);
1089 if (ret) {
1090 return ret;
1092 #endif
1094 ret = get_key_from_keytab(context,
1095 server,
1096 enctype,
1097 kvno,
1098 &local_keyblock);
1099 if (ret) {
1100 DEBUG(0,("krb5_rd_req_return_keyblock_from_keytab: failed to call get_key_from_keytab\n"));
1101 goto out;
1104 out:
1105 if (ret && local_keyblock != NULL) {
1106 krb5_free_keyblock(context, local_keyblock);
1107 } else {
1108 *keyblock = local_keyblock;
1111 return ret;
1114 krb5_error_code smb_krb5_renew_ticket(const char *ccache_string, /* FILE:/tmp/krb5cc_0 */
1115 const char *client_string, /* gd@BER.SUSE.DE */
1116 const char *service_string, /* krbtgt/BER.SUSE.DE@BER.SUSE.DE */
1117 time_t *expire_time)
1119 krb5_error_code ret;
1120 krb5_context context = NULL;
1121 krb5_ccache ccache = NULL;
1122 krb5_principal client = NULL;
1123 krb5_creds creds, creds_in, *creds_out = NULL;
1125 ZERO_STRUCT(creds);
1126 ZERO_STRUCT(creds_in);
1128 initialize_krb5_error_table();
1129 ret = krb5_init_context(&context);
1130 if (ret) {
1131 goto done;
1134 if (!ccache_string) {
1135 ccache_string = krb5_cc_default_name(context);
1138 if (!ccache_string) {
1139 ret = EINVAL;
1140 goto done;
1143 DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string));
1145 /* FIXME: we should not fall back to defaults */
1146 ret = krb5_cc_resolve(context, discard_const_p(char, ccache_string), &ccache);
1147 if (ret) {
1148 goto done;
1151 if (client_string) {
1152 ret = smb_krb5_parse_name(context, client_string, &client);
1153 if (ret) {
1154 goto done;
1156 } else {
1157 ret = krb5_cc_get_principal(context, ccache, &client);
1158 if (ret) {
1159 goto done;
1163 #ifdef HAVE_KRB5_GET_RENEWED_CREDS /* MIT */
1165 ret = krb5_get_renewed_creds(context, &creds, client, ccache, discard_const_p(char, service_string));
1166 if (ret) {
1167 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
1168 goto done;
1171 #elif defined(HAVE_KRB5_GET_KDC_CRED) /* Heimdal */
1173 krb5_kdc_flags flags;
1174 krb5_realm *client_realm = NULL;
1176 ret = krb5_copy_principal(context, client, &creds_in.client);
1177 if (ret) {
1178 goto done;
1181 if (service_string) {
1182 ret = smb_krb5_parse_name(context, service_string, &creds_in.server);
1183 if (ret) {
1184 goto done;
1186 } else {
1187 /* build tgt service by default */
1188 client_realm = krb5_princ_realm(context, creds_in.client);
1189 if (!client_realm) {
1190 ret = ENOMEM;
1191 goto done;
1193 ret = krb5_make_principal(context, &creds_in.server, *client_realm, KRB5_TGS_NAME, *client_realm, NULL);
1194 if (ret) {
1195 goto done;
1199 flags.i = 0;
1200 flags.b.renewable = flags.b.renew = True;
1202 ret = krb5_get_kdc_cred(context, ccache, flags, NULL, NULL, &creds_in, &creds_out);
1203 if (ret) {
1204 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
1205 goto done;
1208 creds = *creds_out;
1210 #else
1211 #error NO_SUITABLE_KRB5_TICKET_RENEW_FUNCTION_AVAILABLE
1212 #endif
1214 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
1215 ret = krb5_cc_initialize(context, ccache, client);
1216 if (ret) {
1217 goto done;
1220 ret = krb5_cc_store_cred(context, ccache, &creds);
1222 if (expire_time) {
1223 *expire_time = (time_t) creds.times.endtime;
1226 done:
1227 krb5_free_cred_contents(context, &creds_in);
1229 if (creds_out) {
1230 krb5_free_creds(context, creds_out);
1231 } else {
1232 krb5_free_cred_contents(context, &creds);
1235 if (client) {
1236 krb5_free_principal(context, client);
1238 if (ccache) {
1239 krb5_cc_close(context, ccache);
1241 if (context) {
1242 krb5_free_context(context);
1245 return ret;
1248 krb5_error_code smb_krb5_free_addresses(krb5_context context, smb_krb5_addresses *addr)
1250 krb5_error_code ret = 0;
1251 if (addr == NULL) {
1252 return ret;
1254 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1255 krb5_free_addresses(context, addr->addrs);
1256 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1257 ret = krb5_free_addresses(context, addr->addrs);
1258 SAFE_FREE(addr->addrs);
1259 #endif
1260 SAFE_FREE(addr);
1261 addr = NULL;
1262 return ret;
1265 krb5_error_code smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses **kerb_addr)
1267 krb5_error_code ret = 0;
1268 nstring buf;
1269 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1270 krb5_address **addrs = NULL;
1271 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1272 krb5_addresses *addrs = NULL;
1273 #endif
1275 *kerb_addr = (smb_krb5_addresses *)SMB_MALLOC(sizeof(smb_krb5_addresses));
1276 if (*kerb_addr == NULL) {
1277 return ENOMEM;
1280 put_name(buf, lp_netbios_name(), ' ', 0x20);
1282 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1284 int num_addr = 2;
1286 addrs = (krb5_address **)SMB_MALLOC(sizeof(krb5_address *) * num_addr);
1287 if (addrs == NULL) {
1288 SAFE_FREE(*kerb_addr);
1289 return ENOMEM;
1292 memset(addrs, 0, sizeof(krb5_address *) * num_addr);
1294 addrs[0] = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
1295 if (addrs[0] == NULL) {
1296 SAFE_FREE(addrs);
1297 SAFE_FREE(*kerb_addr);
1298 return ENOMEM;
1301 addrs[0]->magic = KV5M_ADDRESS;
1302 addrs[0]->addrtype = KRB5_ADDR_NETBIOS;
1303 addrs[0]->length = MAX_NETBIOSNAME_LEN;
1304 addrs[0]->contents = (unsigned char *)SMB_MALLOC(addrs[0]->length);
1305 if (addrs[0]->contents == NULL) {
1306 SAFE_FREE(addrs[0]);
1307 SAFE_FREE(addrs);
1308 SAFE_FREE(*kerb_addr);
1309 return ENOMEM;
1312 memcpy(addrs[0]->contents, buf, addrs[0]->length);
1314 addrs[1] = NULL;
1316 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1318 addrs = (krb5_addresses *)SMB_MALLOC(sizeof(krb5_addresses));
1319 if (addrs == NULL) {
1320 SAFE_FREE(*kerb_addr);
1321 return ENOMEM;
1324 memset(addrs, 0, sizeof(krb5_addresses));
1326 addrs->len = 1;
1327 addrs->val = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
1328 if (addrs->val == NULL) {
1329 SAFE_FREE(addrs);
1330 SAFE_FREE(kerb_addr);
1331 return ENOMEM;
1334 addrs->val[0].addr_type = KRB5_ADDR_NETBIOS;
1335 addrs->val[0].address.length = MAX_NETBIOSNAME_LEN;
1336 addrs->val[0].address.data = (unsigned char *)SMB_MALLOC(addrs->val[0].address.length);
1337 if (addrs->val[0].address.data == NULL) {
1338 SAFE_FREE(addrs->val);
1339 SAFE_FREE(addrs);
1340 SAFE_FREE(*kerb_addr);
1341 return ENOMEM;
1344 memcpy(addrs->val[0].address.data, buf, addrs->val[0].address.length);
1346 #else
1347 #error UNKNOWN_KRB5_ADDRESS_FORMAT
1348 #endif
1349 (*kerb_addr)->addrs = addrs;
1351 return ret;
1354 void smb_krb5_free_error(krb5_context context, krb5_error *krberror)
1356 #ifdef HAVE_KRB5_FREE_ERROR_CONTENTS /* Heimdal */
1357 krb5_free_error_contents(context, krberror);
1358 #else /* MIT */
1359 krb5_free_error(context, krberror);
1360 #endif
1363 krb5_error_code handle_krberror_packet(krb5_context context,
1364 krb5_data *packet)
1366 krb5_error_code ret;
1367 bool got_error_code = False;
1369 DEBUG(10,("handle_krberror_packet: got error packet\n"));
1371 #ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR /* Heimdal */
1373 krb5_error krberror;
1375 if ((ret = krb5_rd_error(context, packet, &krberror))) {
1376 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1377 error_message(ret)));
1378 return ret;
1381 if (krberror.e_data == NULL || krberror.e_data->data == NULL) {
1382 ret = (krb5_error_code) krberror.error_code;
1383 got_error_code = True;
1386 smb_krb5_free_error(context, &krberror);
1388 #else /* MIT */
1390 krb5_error *krberror;
1392 if ((ret = krb5_rd_error(context, packet, &krberror))) {
1393 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1394 error_message(ret)));
1395 return ret;
1398 if (krberror->e_data.data == NULL) {
1399 #if defined(ERROR_TABLE_BASE_krb5)
1400 ret = ERROR_TABLE_BASE_krb5 + (krb5_error_code) krberror->error;
1401 #else
1402 ret = (krb5_error_code)krberror->error;
1403 #endif
1404 got_error_code = True;
1406 smb_krb5_free_error(context, krberror);
1408 #endif
1409 if (got_error_code) {
1410 DEBUG(5,("handle_krberror_packet: got KERBERR from kpasswd: %s (%d)\n",
1411 error_message(ret), ret));
1413 return ret;
1416 krb5_error_code smb_krb5_get_init_creds_opt_alloc(krb5_context context,
1417 krb5_get_init_creds_opt **opt)
1419 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
1420 /* Heimdal or modern MIT version */
1421 return krb5_get_init_creds_opt_alloc(context, opt);
1422 #else
1423 /* Historical MIT version */
1424 krb5_get_init_creds_opt *my_opt;
1426 *opt = NULL;
1428 if ((my_opt = SMB_MALLOC_P(krb5_get_init_creds_opt)) == NULL) {
1429 return ENOMEM;
1432 krb5_get_init_creds_opt_init(my_opt);
1434 *opt = my_opt;
1435 return 0;
1436 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC */
1439 void smb_krb5_get_init_creds_opt_free(krb5_context context,
1440 krb5_get_init_creds_opt *opt)
1442 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_FREE
1444 #ifdef KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT
1445 /* Modern MIT or Heimdal version */
1446 krb5_get_init_creds_opt_free(context, opt);
1447 #else
1448 /* Heimdal version */
1449 krb5_get_init_creds_opt_free(opt);
1450 #endif /* KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT */
1452 #else /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1453 /* Historical MIT version */
1454 SAFE_FREE(opt);
1455 opt = NULL;
1456 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1459 krb5_enctype smb_get_enctype_from_kt_entry(krb5_keytab_entry *kt_entry)
1461 return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry));
1465 /* caller needs to free etype_s */
1466 krb5_error_code smb_krb5_enctype_to_string(krb5_context context,
1467 krb5_enctype enctype,
1468 char **etype_s)
1470 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
1471 return krb5_enctype_to_string(context, enctype, etype_s); /* Heimdal */
1472 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
1473 char buf[256];
1474 krb5_error_code ret = krb5_enctype_to_string(enctype, buf, 256); /* MIT */
1475 if (ret) {
1476 return ret;
1478 *etype_s = SMB_STRDUP(buf);
1479 if (!*etype_s) {
1480 return ENOMEM;
1482 return ret;
1483 #else
1484 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
1485 #endif
1488 krb5_error_code smb_krb5_mk_error(krb5_context context,
1489 krb5_error_code error_code,
1490 const krb5_principal server,
1491 krb5_data *reply)
1493 #ifdef HAVE_SHORT_KRB5_MK_ERROR_INTERFACE /* MIT */
1495 * The MIT interface is *terrible*.
1496 * We have to construct this ourselves...
1498 krb5_error e;
1500 memset(&e, 0, sizeof(e));
1501 krb5_us_timeofday(context, &e.stime, &e.susec);
1502 e.server = server;
1503 #if defined(krb5_err_base)
1504 e.error = error_code - krb5_err_base;
1505 #elif defined(ERROR_TABLE_BASE_krb5)
1506 e.error = error_code - ERROR_TABLE_BASE_krb5;
1507 #else
1508 e.error = error_code; /* Almost certainly wrong, but what can we do... ? */
1509 #endif
1511 return krb5_mk_error(context, &e, reply);
1512 #else /* Heimdal. */
1513 return krb5_mk_error(context,
1514 error_code,
1515 NULL,
1516 NULL, /* e_data */
1517 NULL,
1518 server,
1519 NULL,
1520 NULL,
1521 reply);
1522 #endif
1525 /**********************************************************************
1526 * Open a krb5 keytab with flags, handles readonly or readwrite access and
1527 * allows to process non-default keytab names.
1528 * @param context krb5_context
1529 * @param keytab_name_req string
1530 * @param write_access bool if writable keytab is required
1531 * @param krb5_keytab pointer to krb5_keytab (close with krb5_kt_close())
1532 * @return krb5_error_code
1533 **********************************************************************/
1535 /* This MAX_NAME_LEN is a constant defined in krb5.h */
1536 #ifndef MAX_KEYTAB_NAME_LEN
1537 #define MAX_KEYTAB_NAME_LEN 1100
1538 #endif
1540 krb5_error_code smb_krb5_open_keytab(krb5_context context,
1541 const char *keytab_name_req,
1542 bool write_access,
1543 krb5_keytab *keytab)
1545 krb5_error_code ret = 0;
1546 TALLOC_CTX *mem_ctx;
1547 char keytab_string[MAX_KEYTAB_NAME_LEN];
1548 char *kt_str = NULL;
1549 bool found_valid_name = False;
1550 const char *pragma = "FILE";
1551 const char *tmp = NULL;
1553 if (!write_access && !keytab_name_req) {
1554 /* caller just wants to read the default keytab readonly, so be it */
1555 return krb5_kt_default(context, keytab);
1558 mem_ctx = talloc_init("smb_krb5_open_keytab");
1559 if (!mem_ctx) {
1560 return ENOMEM;
1563 #ifdef HAVE_WRFILE_KEYTAB
1564 if (write_access) {
1565 pragma = "WRFILE";
1567 #endif
1569 if (keytab_name_req) {
1571 if (strlen(keytab_name_req) > MAX_KEYTAB_NAME_LEN) {
1572 ret = KRB5_CONFIG_NOTENUFSPACE;
1573 goto out;
1576 if ((strncmp(keytab_name_req, "WRFILE:/", 8) == 0) ||
1577 (strncmp(keytab_name_req, "FILE:/", 6) == 0)) {
1578 tmp = keytab_name_req;
1579 goto resolve;
1582 if (keytab_name_req[0] != '/') {
1583 ret = KRB5_KT_BADNAME;
1584 goto out;
1587 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, keytab_name_req);
1588 if (!tmp) {
1589 ret = ENOMEM;
1590 goto out;
1593 goto resolve;
1596 /* we need to handle more complex keytab_strings, like:
1597 * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
1599 ret = krb5_kt_default_name(context, &keytab_string[0], MAX_KEYTAB_NAME_LEN - 2);
1600 if (ret) {
1601 goto out;
1604 DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string));
1606 tmp = talloc_strdup(mem_ctx, keytab_string);
1607 if (!tmp) {
1608 ret = ENOMEM;
1609 goto out;
1612 if (strncmp(tmp, "ANY:", 4) == 0) {
1613 tmp += 4;
1616 memset(&keytab_string, '\0', sizeof(keytab_string));
1618 while (next_token_talloc(mem_ctx, &tmp, &kt_str, ",")) {
1619 if (strncmp(kt_str, "WRFILE:", 7) == 0) {
1620 found_valid_name = True;
1621 tmp = kt_str;
1622 tmp += 7;
1625 if (strncmp(kt_str, "FILE:", 5) == 0) {
1626 found_valid_name = True;
1627 tmp = kt_str;
1628 tmp += 5;
1631 if (tmp[0] == '/') {
1632 /* Treat as a FILE: keytab definition. */
1633 found_valid_name = true;
1636 if (found_valid_name) {
1637 if (tmp[0] != '/') {
1638 ret = KRB5_KT_BADNAME;
1639 goto out;
1642 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, tmp);
1643 if (!tmp) {
1644 ret = ENOMEM;
1645 goto out;
1647 break;
1651 if (!found_valid_name) {
1652 ret = KRB5_KT_UNKNOWN_TYPE;
1653 goto out;
1656 resolve:
1657 DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp));
1658 ret = krb5_kt_resolve(context, tmp, keytab);
1660 out:
1661 TALLOC_FREE(mem_ctx);
1662 return ret;
1665 krb5_error_code smb_krb5_keytab_name(TALLOC_CTX *mem_ctx,
1666 krb5_context context,
1667 krb5_keytab keytab,
1668 const char **keytab_name)
1670 char keytab_string[MAX_KEYTAB_NAME_LEN];
1671 krb5_error_code ret = 0;
1673 ret = krb5_kt_get_name(context, keytab,
1674 keytab_string, MAX_KEYTAB_NAME_LEN - 2);
1675 if (ret) {
1676 return ret;
1679 *keytab_name = talloc_strdup(mem_ctx, keytab_string);
1680 if (!*keytab_name) {
1681 return ENOMEM;
1684 return ret;
1687 #if defined(HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE) && \
1688 defined(HAVE_KRB5_GET_CREDS_OPT_ALLOC) && \
1689 defined(HAVE_KRB5_GET_CREDS)
1690 static krb5_error_code smb_krb5_get_credentials_for_user_opt(krb5_context context,
1691 krb5_ccache ccache,
1692 krb5_principal me,
1693 krb5_principal server,
1694 krb5_principal impersonate_princ,
1695 krb5_creds **out_creds)
1697 krb5_error_code ret;
1698 krb5_get_creds_opt opt;
1700 ret = krb5_get_creds_opt_alloc(context, &opt);
1701 if (ret) {
1702 goto done;
1704 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_FORWARDABLE);
1706 if (impersonate_princ) {
1707 ret = krb5_get_creds_opt_set_impersonate(context, opt,
1708 impersonate_princ);
1709 if (ret) {
1710 goto done;
1714 ret = krb5_get_creds(context, opt, ccache, server, out_creds);
1715 if (ret) {
1716 goto done;
1719 done:
1720 if (opt) {
1721 krb5_get_creds_opt_free(context, opt);
1723 return ret;
1725 #endif /* HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE */
1727 #ifdef HAVE_KRB5_GET_CREDENTIALS_FOR_USER
1728 static krb5_error_code smb_krb5_get_credentials_for_user(krb5_context context,
1729 krb5_ccache ccache,
1730 krb5_principal me,
1731 krb5_principal server,
1732 krb5_principal impersonate_princ,
1733 krb5_creds **out_creds)
1735 krb5_error_code ret;
1736 krb5_creds in_creds;
1738 #if !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER
1739 krb5_error_code KRB5_CALLCONV
1740 krb5_get_credentials_for_user(krb5_context context, krb5_flags options,
1741 krb5_ccache ccache, krb5_creds *in_creds,
1742 krb5_data *subject_cert,
1743 krb5_creds **out_creds);
1744 #endif /* !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER */
1746 ZERO_STRUCT(in_creds);
1748 if (impersonate_princ) {
1750 in_creds.server = me;
1751 in_creds.client = impersonate_princ;
1753 ret = krb5_get_credentials_for_user(context,
1754 0, /* krb5_flags options */
1755 ccache,
1756 &in_creds,
1757 NULL, /* krb5_data *subject_cert */
1758 out_creds);
1759 } else {
1760 in_creds.client = me;
1761 in_creds.server = server;
1763 ret = krb5_get_credentials(context, 0, ccache,
1764 &in_creds, out_creds);
1767 return ret;
1769 #endif /* HAVE_KRB5_GET_CREDENTIALS_FOR_USER */
1772 * smb_krb5_get_credentials
1774 * @brief Get krb5 credentials for a server
1776 * @param[in] context An initialized krb5_context
1777 * @param[in] ccache An initialized krb5_ccache
1778 * @param[in] me The krb5_principal of the caller
1779 * @param[in] server The krb5_principal of the requested service
1780 * @param[in] impersonate_princ The krb5_principal of a user to impersonate as (optional)
1781 * @param[out] out_creds The returned krb5_creds structure
1782 * @return krb5_error_code
1785 krb5_error_code smb_krb5_get_credentials(krb5_context context,
1786 krb5_ccache ccache,
1787 krb5_principal me,
1788 krb5_principal server,
1789 krb5_principal impersonate_princ,
1790 krb5_creds **out_creds)
1792 krb5_error_code ret;
1793 krb5_creds *creds = NULL;
1795 *out_creds = NULL;
1797 if (impersonate_princ) {
1798 #ifdef HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE /* Heimdal */
1799 ret = smb_krb5_get_credentials_for_user_opt(context, ccache, me, server, impersonate_princ, &creds);
1800 #elif defined(HAVE_KRB5_GET_CREDENTIALS_FOR_USER) /* MIT */
1801 ret = smb_krb5_get_credentials_for_user(context, ccache, me, server, impersonate_princ, &creds);
1802 #else
1803 ret = ENOTSUP;
1804 #endif
1805 } else {
1806 krb5_creds in_creds;
1808 ZERO_STRUCT(in_creds);
1810 in_creds.client = me;
1811 in_creds.server = server;
1813 ret = krb5_get_credentials(context, 0, ccache,
1814 &in_creds, &creds);
1816 if (ret) {
1817 goto done;
1820 if (out_creds) {
1821 *out_creds = creds;
1824 done:
1825 if (creds && ret) {
1826 krb5_free_creds(context, creds);
1829 return ret;
1833 * smb_krb5_get_creds
1835 * @brief Get krb5 credentials for a server
1837 * @param[in] server_s The string name of the service
1838 * @param[in] time_offset The offset to the KDCs time in seconds (optional)
1839 * @param[in] cc The krb5 credential cache string name (optional)
1840 * @param[in] impersonate_princ_s The string principal name to impersonate (optional)
1841 * @param[out] creds_p The returned krb5_creds structure
1842 * @return krb5_error_code
1845 krb5_error_code smb_krb5_get_creds(const char *server_s,
1846 time_t time_offset,
1847 const char *cc,
1848 const char *impersonate_princ_s,
1849 krb5_creds **creds_p)
1851 krb5_error_code ret;
1852 krb5_context context = NULL;
1853 krb5_principal me = NULL;
1854 krb5_principal server = NULL;
1855 krb5_principal impersonate_princ = NULL;
1856 krb5_creds *creds = NULL;
1857 krb5_ccache ccache = NULL;
1859 *creds_p = NULL;
1861 initialize_krb5_error_table();
1862 ret = krb5_init_context(&context);
1863 if (ret) {
1864 goto done;
1867 if (time_offset != 0) {
1868 krb5_set_real_time(context, time(NULL) + time_offset, 0);
1871 ret = krb5_cc_resolve(context, cc ? cc :
1872 krb5_cc_default_name(context), &ccache);
1873 if (ret) {
1874 goto done;
1877 ret = krb5_cc_get_principal(context, ccache, &me);
1878 if (ret) {
1879 goto done;
1882 ret = smb_krb5_parse_name(context, server_s, &server);
1883 if (ret) {
1884 goto done;
1887 if (impersonate_princ_s) {
1888 ret = smb_krb5_parse_name(context, impersonate_princ_s,
1889 &impersonate_princ);
1890 if (ret) {
1891 goto done;
1895 ret = smb_krb5_get_credentials(context, ccache,
1896 me, server, impersonate_princ,
1897 &creds);
1898 if (ret) {
1899 goto done;
1902 ret = krb5_cc_store_cred(context, ccache, creds);
1903 if (ret) {
1904 goto done;
1907 if (creds_p) {
1908 *creds_p = creds;
1911 DEBUG(1,("smb_krb5_get_creds: got ticket for %s\n",
1912 server_s));
1914 if (impersonate_princ_s) {
1915 char *client = NULL;
1917 ret = smb_krb5_unparse_name(talloc_tos(), context, creds->client, &client);
1918 if (ret) {
1919 goto done;
1921 DEBUGADD(1,("smb_krb5_get_creds: using S4U2SELF impersonation as %s\n",
1922 client));
1923 TALLOC_FREE(client);
1926 done:
1927 if (!context) {
1928 return ret;
1931 if (creds && ret) {
1932 krb5_free_creds(context, creds);
1934 if (server) {
1935 krb5_free_principal(context, server);
1937 if (me) {
1938 krb5_free_principal(context, me);
1940 if (impersonate_princ) {
1941 krb5_free_principal(context, impersonate_princ);
1943 if (ccache) {
1944 krb5_cc_close(context, ccache);
1946 krb5_free_context(context);
1948 return ret;
1952 * smb_krb5_principal_get_realm
1954 * @brief Get realm of a principal
1956 * @param[in] context The krb5_context
1957 * @param[in] principal The principal
1958 * @return pointer to the realm
1962 char *smb_krb5_principal_get_realm(krb5_context context,
1963 krb5_principal principal)
1965 #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
1966 return discard_const_p(char, krb5_principal_get_realm(context, principal));
1967 #elif defined(krb5_princ_realm) /* MIT */
1968 krb5_data *realm;
1969 realm = krb5_princ_realm(context, principal);
1970 return discard_const_p(char, realm->data);
1971 #else
1972 return NULL;
1973 #endif
1976 #else /* HAVE_KRB5 */
1977 /* this saves a few linking headaches */
1978 int cli_krb5_get_ticket(TALLOC_CTX *mem_ctx,
1979 const char *principal, time_t time_offset,
1980 DATA_BLOB *ticket, DATA_BLOB *session_key_krb5,
1981 uint32_t extra_ap_opts,
1982 const char *ccname, time_t *tgs_expire,
1983 const char *impersonate_princ_s)
1985 DEBUG(0,("NO KERBEROS SUPPORT\n"));
1986 return 1;
1989 bool unwrap_pac(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, DATA_BLOB *unwrapped_pac_data)
1991 DEBUG(0,("NO KERBEROS SUPPORT\n"));
1992 return false;
1995 #endif /* HAVE_KRB5 */