Build error exception lost (without wafcache)
[Samba/ita.git] / source3 / libsmb / clikrb5.c
blob854d32476f172a5a27eaca8f87666b9844828003
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"
27 #ifndef KRB5_AUTHDATA_WIN2K_PAC
28 #define KRB5_AUTHDATA_WIN2K_PAC 128
29 #endif
31 #ifndef KRB5_AUTHDATA_IF_RELEVANT
32 #define KRB5_AUTHDATA_IF_RELEVANT 1
33 #endif
35 #ifdef HAVE_KRB5
37 #define GSSAPI_CHECKSUM 0x8003 /* Checksum type value for Kerberos */
38 #define GSSAPI_BNDLENGTH 16 /* Bind Length (rfc-1964 pg.3) */
39 #define GSSAPI_CHECKSUM_SIZE (4+GSSAPI_BNDLENGTH+4) /* Length of bind length,
40 bind field, flags field. */
42 /* MIT krb5 1.7beta3 (in Ubuntu Karmic) is missing the prototype,
43 but still has the symbol */
44 #if !HAVE_DECL_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE
45 krb5_error_code krb5_auth_con_set_req_cksumtype(
46 krb5_context context,
47 krb5_auth_context auth_context,
48 krb5_cksumtype cksumtype);
49 #endif
51 /**************************************************************
52 Wrappers around kerberos string functions that convert from
53 utf8 -> unix charset and vica versa.
54 **************************************************************/
56 /**************************************************************
57 krb5_parse_name that takes a UNIX charset.
58 **************************************************************/
60 krb5_error_code smb_krb5_parse_name(krb5_context context,
61 const char *name, /* in unix charset */
62 krb5_principal *principal)
64 krb5_error_code ret;
65 char *utf8_name;
66 size_t converted_size;
68 if (!push_utf8_talloc(talloc_tos(), &utf8_name, name, &converted_size)) {
69 return ENOMEM;
72 ret = krb5_parse_name(context, utf8_name, principal);
73 TALLOC_FREE(utf8_name);
74 return ret;
77 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
78 /**************************************************************
79 krb5_parse_name_norealm that takes a UNIX charset.
80 **************************************************************/
82 static krb5_error_code smb_krb5_parse_name_norealm_conv(krb5_context context,
83 const char *name, /* in unix charset */
84 krb5_principal *principal)
86 krb5_error_code ret;
87 char *utf8_name;
88 size_t converted_size;
90 *principal = NULL;
91 if (!push_utf8_talloc(talloc_tos(), &utf8_name, name, &converted_size)) {
92 return ENOMEM;
95 ret = krb5_parse_name_norealm(context, utf8_name, principal);
96 TALLOC_FREE(utf8_name);
97 return ret;
99 #endif
101 /**************************************************************
102 krb5_parse_name that returns a UNIX charset name. Must
103 be freed with talloc_free() call.
104 **************************************************************/
106 krb5_error_code smb_krb5_unparse_name(TALLOC_CTX *mem_ctx,
107 krb5_context context,
108 krb5_const_principal principal,
109 char **unix_name)
111 krb5_error_code ret;
112 char *utf8_name;
113 size_t converted_size;
115 *unix_name = NULL;
116 ret = krb5_unparse_name(context, principal, &utf8_name);
117 if (ret) {
118 return ret;
121 if (!pull_utf8_talloc(mem_ctx, unix_name, utf8_name, &converted_size)) {
122 krb5_free_unparsed_name(context, utf8_name);
123 return ENOMEM;
125 krb5_free_unparsed_name(context, utf8_name);
126 return 0;
129 #ifndef HAVE_KRB5_SET_REAL_TIME
131 * This function is not in the Heimdal mainline.
133 krb5_error_code krb5_set_real_time(krb5_context context, int32_t seconds, int32_t microseconds)
135 krb5_error_code ret;
136 int32_t sec, usec;
138 ret = krb5_us_timeofday(context, &sec, &usec);
139 if (ret)
140 return ret;
142 context->kdc_sec_offset = seconds - sec;
143 context->kdc_usec_offset = microseconds - usec;
145 return 0;
147 #endif
149 #if !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
151 #if defined(HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES)
153 /* With MIT kerberos, we should use krb5_set_default_tgs_enctypes in preference
154 * to krb5_set_default_tgs_ktypes. See
155 * http://lists.samba.org/archive/samba-technical/2006-July/048271.html
157 * If the MIT libraries are not exporting internal symbols, we will end up in
158 * this branch, which is correct. Otherwise we will continue to use the
159 * internal symbol
161 krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
163 return krb5_set_default_tgs_enctypes(ctx, enc);
166 #elif defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES)
168 /* Heimdal */
169 krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
171 return krb5_set_default_in_tkt_etypes(ctx, enc);
174 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES */
176 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_KTYPES */
178 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
179 /* HEIMDAL */
180 bool setup_kaddr( krb5_address *pkaddr, struct sockaddr_storage *paddr)
182 memset(pkaddr, '\0', sizeof(krb5_address));
183 #if defined(HAVE_IPV6) && defined(KRB5_ADDRESS_INET6)
184 if (paddr->ss_family == AF_INET6) {
185 pkaddr->addr_type = KRB5_ADDRESS_INET6;
186 pkaddr->address.length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr);
187 pkaddr->address.data = (char *)&(((struct sockaddr_in6 *)paddr)->sin6_addr);
188 return true;
190 #endif
191 if (paddr->ss_family == AF_INET) {
192 pkaddr->addr_type = KRB5_ADDRESS_INET;
193 pkaddr->address.length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
194 pkaddr->address.data = (char *)&(((struct sockaddr_in *)paddr)->sin_addr);
195 return true;
197 return false;
199 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
200 /* MIT */
201 bool setup_kaddr( krb5_address *pkaddr, struct sockaddr_storage *paddr)
203 memset(pkaddr, '\0', sizeof(krb5_address));
204 #if defined(HAVE_IPV6) && defined(ADDRTYPE_INET6)
205 if (paddr->ss_family == AF_INET6) {
206 pkaddr->addrtype = ADDRTYPE_INET6;
207 pkaddr->length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr);
208 pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in6 *)paddr)->sin6_addr);
209 return true;
211 #endif
212 if (paddr->ss_family == AF_INET) {
213 pkaddr->addrtype = ADDRTYPE_INET;
214 pkaddr->length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
215 pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in *)paddr)->sin_addr);
216 return true;
218 return false;
220 #else
221 #error UNKNOWN_ADDRTYPE
222 #endif
224 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_USE_ENCTYPE) && defined(HAVE_KRB5_STRING_TO_KEY) && defined(HAVE_KRB5_ENCRYPT_BLOCK)
225 static int create_kerberos_key_from_string_direct(krb5_context context,
226 krb5_principal host_princ,
227 krb5_data *password,
228 krb5_keyblock *key,
229 krb5_enctype enctype)
231 int ret = 0;
232 krb5_data salt;
233 krb5_encrypt_block eblock;
235 ret = krb5_principal2salt(context, host_princ, &salt);
236 if (ret) {
237 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
238 return ret;
240 krb5_use_enctype(context, &eblock, enctype);
241 ret = krb5_string_to_key(context, &eblock, key, password, &salt);
242 SAFE_FREE(salt.data);
244 return ret;
246 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
247 static int create_kerberos_key_from_string_direct(krb5_context context,
248 krb5_principal host_princ,
249 krb5_data *password,
250 krb5_keyblock *key,
251 krb5_enctype enctype)
253 int ret;
254 krb5_salt salt;
256 ret = krb5_get_pw_salt(context, host_princ, &salt);
257 if (ret) {
258 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret)));
259 return ret;
262 ret = krb5_string_to_key_salt(context, enctype, (const char *)password->data, salt, key);
263 krb5_free_salt(context, salt);
265 return ret;
267 #else
268 #error UNKNOWN_CREATE_KEY_FUNCTIONS
269 #endif
271 int create_kerberos_key_from_string(krb5_context context,
272 krb5_principal host_princ,
273 krb5_data *password,
274 krb5_keyblock *key,
275 krb5_enctype enctype,
276 bool no_salt)
278 krb5_principal salt_princ = NULL;
279 int ret;
281 * Check if we've determined that the KDC is salting keys for this
282 * principal/enctype in a non-obvious way. If it is, try to match
283 * its behavior.
285 if (no_salt) {
286 KRB5_KEY_DATA(key) = (KRB5_KEY_DATA_CAST *)SMB_MALLOC(password->length);
287 if (!KRB5_KEY_DATA(key)) {
288 return ENOMEM;
290 memcpy(KRB5_KEY_DATA(key), password->data, password->length);
291 KRB5_KEY_LENGTH(key) = password->length;
292 KRB5_KEY_TYPE(key) = enctype;
293 return 0;
295 salt_princ = kerberos_fetch_salt_princ_for_host_princ(context, host_princ, enctype);
296 ret = create_kerberos_key_from_string_direct(context, salt_princ ? salt_princ : host_princ, password, key, enctype);
297 if (salt_princ) {
298 krb5_free_principal(context, salt_princ);
300 return ret;
303 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
304 krb5_error_code get_kerberos_allowed_etypes(krb5_context context,
305 krb5_enctype **enctypes)
307 return krb5_get_permitted_enctypes(context, enctypes);
309 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
310 krb5_error_code get_kerberos_allowed_etypes(krb5_context context,
311 krb5_enctype **enctypes)
313 return krb5_get_default_in_tkt_etypes(context, enctypes);
315 #else
316 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
317 #endif
319 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
320 krb5_error_code krb5_auth_con_setuseruserkey(krb5_context context,
321 krb5_auth_context auth_context,
322 krb5_keyblock *keyblock)
324 return krb5_auth_con_setkey(context, auth_context, keyblock);
326 #endif
328 bool unwrap_edata_ntstatus(TALLOC_CTX *mem_ctx,
329 DATA_BLOB *edata,
330 DATA_BLOB *edata_out)
332 DATA_BLOB edata_contents;
333 ASN1_DATA *data;
334 int edata_type;
336 if (!edata->length) {
337 return False;
340 data = asn1_init(mem_ctx);
341 if (data == NULL) {
342 return false;
345 asn1_load(data, *edata);
346 asn1_start_tag(data, ASN1_SEQUENCE(0));
347 asn1_start_tag(data, ASN1_CONTEXT(1));
348 asn1_read_Integer(data, &edata_type);
350 if (edata_type != KRB5_PADATA_PW_SALT) {
351 DEBUG(0,("edata is not of required type %d but of type %d\n",
352 KRB5_PADATA_PW_SALT, edata_type));
353 asn1_free(data);
354 return False;
357 asn1_start_tag(data, ASN1_CONTEXT(2));
358 asn1_read_OctetString(data, talloc_autofree_context(), &edata_contents);
359 asn1_end_tag(data);
360 asn1_end_tag(data);
361 asn1_end_tag(data);
362 asn1_free(data);
364 *edata_out = data_blob_talloc(mem_ctx, edata_contents.data, edata_contents.length);
366 data_blob_free(&edata_contents);
368 return True;
372 bool unwrap_pac(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, DATA_BLOB *unwrapped_pac_data)
374 DATA_BLOB pac_contents;
375 ASN1_DATA *data;
376 int data_type;
378 if (!auth_data->length) {
379 return False;
382 data = asn1_init(mem_ctx);
383 if (data == NULL) {
384 return false;
387 asn1_load(data, *auth_data);
388 asn1_start_tag(data, ASN1_SEQUENCE(0));
389 asn1_start_tag(data, ASN1_SEQUENCE(0));
390 asn1_start_tag(data, ASN1_CONTEXT(0));
391 asn1_read_Integer(data, &data_type);
393 if (data_type != KRB5_AUTHDATA_WIN2K_PAC ) {
394 DEBUG(10,("authorization data is not a Windows PAC (type: %d)\n", data_type));
395 asn1_free(data);
396 return False;
399 asn1_end_tag(data);
400 asn1_start_tag(data, ASN1_CONTEXT(1));
401 asn1_read_OctetString(data, talloc_autofree_context(), &pac_contents);
402 asn1_end_tag(data);
403 asn1_end_tag(data);
404 asn1_end_tag(data);
405 asn1_free(data);
407 *unwrapped_pac_data = data_blob_talloc(mem_ctx, pac_contents.data, pac_contents.length);
409 data_blob_free(&pac_contents);
411 return True;
414 bool get_auth_data_from_tkt(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, krb5_ticket *tkt)
416 DATA_BLOB auth_data_wrapped;
417 bool got_auth_data_pac = False;
418 int i;
420 #if defined(HAVE_KRB5_TKT_ENC_PART2)
421 if (tkt->enc_part2 && tkt->enc_part2->authorization_data &&
422 tkt->enc_part2->authorization_data[0] &&
423 tkt->enc_part2->authorization_data[0]->length)
425 for (i = 0; tkt->enc_part2->authorization_data[i] != NULL; i++) {
427 if (tkt->enc_part2->authorization_data[i]->ad_type !=
428 KRB5_AUTHDATA_IF_RELEVANT) {
429 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
430 tkt->enc_part2->authorization_data[i]->ad_type));
431 continue;
434 auth_data_wrapped = data_blob(tkt->enc_part2->authorization_data[i]->contents,
435 tkt->enc_part2->authorization_data[i]->length);
437 /* check if it is a PAC */
438 got_auth_data_pac = unwrap_pac(mem_ctx, &auth_data_wrapped, auth_data);
439 data_blob_free(&auth_data_wrapped);
441 if (got_auth_data_pac) {
442 return true;
446 return got_auth_data_pac;
449 #else
450 if (tkt->ticket.authorization_data &&
451 tkt->ticket.authorization_data->len)
453 for (i = 0; i < tkt->ticket.authorization_data->len; i++) {
455 if (tkt->ticket.authorization_data->val[i].ad_type !=
456 KRB5_AUTHDATA_IF_RELEVANT) {
457 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
458 tkt->ticket.authorization_data->val[i].ad_type));
459 continue;
462 auth_data_wrapped = data_blob(tkt->ticket.authorization_data->val[i].ad_data.data,
463 tkt->ticket.authorization_data->val[i].ad_data.length);
465 /* check if it is a PAC */
466 got_auth_data_pac = unwrap_pac(mem_ctx, &auth_data_wrapped, auth_data);
467 data_blob_free(&auth_data_wrapped);
469 if (got_auth_data_pac) {
470 return true;
474 return got_auth_data_pac;
476 #endif
477 return False;
480 krb5_const_principal get_principal_from_tkt(krb5_ticket *tkt)
482 #if defined(HAVE_KRB5_TKT_ENC_PART2)
483 return tkt->enc_part2->client;
484 #else
485 return tkt->client;
486 #endif
489 #if !defined(HAVE_KRB5_LOCATE_KDC)
491 /* krb5_locate_kdc is an internal MIT symbol. MIT are not yet willing to commit
492 * to a public interface for this functionality, so we have to be able to live
493 * without it if the MIT libraries are hiding their internal symbols.
496 #if defined(KRB5_KRBHST_INIT)
497 /* Heimdal */
498 krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm, struct sockaddr **addr_pp, int *naddrs, int get_masters)
500 krb5_krbhst_handle hnd;
501 krb5_krbhst_info *hinfo;
502 krb5_error_code rc;
503 int num_kdcs, i;
504 struct sockaddr *sa;
505 struct addrinfo *ai;
507 *addr_pp = NULL;
508 *naddrs = 0;
510 rc = krb5_krbhst_init(ctx, realm->data, KRB5_KRBHST_KDC, &hnd);
511 if (rc) {
512 DEBUG(0, ("smb_krb5_locate_kdc: krb5_krbhst_init failed (%s)\n", error_message(rc)));
513 return rc;
516 for ( num_kdcs = 0; (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); num_kdcs++)
519 krb5_krbhst_reset(ctx, hnd);
521 if (!num_kdcs) {
522 DEBUG(0, ("smb_krb5_locate_kdc: zero kdcs found !\n"));
523 krb5_krbhst_free(ctx, hnd);
524 return -1;
527 sa = SMB_MALLOC_ARRAY( struct sockaddr, num_kdcs );
528 if (!sa) {
529 DEBUG(0, ("smb_krb5_locate_kdc: malloc failed\n"));
530 krb5_krbhst_free(ctx, hnd);
531 naddrs = 0;
532 return -1;
535 memset(sa, '\0', sizeof(struct sockaddr) * num_kdcs );
537 for (i = 0; i < num_kdcs && (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); i++) {
539 #if defined(HAVE_KRB5_KRBHST_GET_ADDRINFO)
540 rc = krb5_krbhst_get_addrinfo(ctx, hinfo, &ai);
541 if (rc) {
542 DEBUG(0,("krb5_krbhst_get_addrinfo failed: %s\n", error_message(rc)));
543 continue;
545 #endif
546 if (hinfo->ai && hinfo->ai->ai_family == AF_INET)
547 memcpy(&sa[i], hinfo->ai->ai_addr, sizeof(struct sockaddr));
550 krb5_krbhst_free(ctx, hnd);
552 *naddrs = num_kdcs;
553 *addr_pp = sa;
554 return 0;
557 #else /* ! defined(KRB5_KRBHST_INIT) */
559 krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm,
560 struct sockaddr **addr_pp, int *naddrs, int get_masters)
562 DEBUG(0, ("unable to explicitly locate the KDC on this platform\n"));
563 return KRB5_KDC_UNREACH;
566 #endif /* KRB5_KRBHST_INIT */
568 #else /* ! HAVE_KRB5_LOCATE_KDC */
570 krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm,
571 struct sockaddr **addr_pp, int *naddrs, int get_masters)
573 return krb5_locate_kdc(ctx, realm, addr_pp, naddrs, get_masters);
576 #endif /* HAVE_KRB5_LOCATE_KDC */
578 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
579 void krb5_free_unparsed_name(krb5_context context, char *val)
581 SAFE_FREE(val);
583 #endif
585 void kerberos_free_data_contents(krb5_context context, krb5_data *pdata)
587 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
588 if (pdata->data) {
589 krb5_free_data_contents(context, pdata);
591 #else
592 SAFE_FREE(pdata->data);
593 #endif
596 void kerberos_set_creds_enctype(krb5_creds *pcreds, int enctype)
598 #if defined(HAVE_KRB5_KEYBLOCK_IN_CREDS)
599 KRB5_KEY_TYPE((&pcreds->keyblock)) = enctype;
600 #elif defined(HAVE_KRB5_SESSION_IN_CREDS)
601 KRB5_KEY_TYPE((&pcreds->session)) = enctype;
602 #else
603 #error UNKNOWN_KEYBLOCK_MEMBER_IN_KRB5_CREDS_STRUCT
604 #endif
607 bool kerberos_compatible_enctypes(krb5_context context,
608 krb5_enctype enctype1,
609 krb5_enctype enctype2)
611 #if defined(HAVE_KRB5_C_ENCTYPE_COMPARE)
612 krb5_boolean similar = 0;
614 krb5_c_enctype_compare(context, enctype1, enctype2, &similar);
615 return similar ? True : False;
616 #elif defined(HAVE_KRB5_ENCTYPES_COMPATIBLE_KEYS)
617 return krb5_enctypes_compatible_keys(context, enctype1, enctype2) ? True : False;
618 #endif
621 static bool ads_cleanup_expired_creds(krb5_context context,
622 krb5_ccache ccache,
623 krb5_creds *credsp)
625 krb5_error_code retval;
626 const char *cc_type = krb5_cc_get_type(context, ccache);
628 DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
629 cc_type, krb5_cc_get_name(context, ccache),
630 http_timestring(talloc_tos(), credsp->times.endtime)));
632 /* we will probably need new tickets if the current ones
633 will expire within 10 seconds.
635 if (credsp->times.endtime >= (time(NULL) + 10))
636 return False;
638 /* heimdal won't remove creds from a file ccache, and
639 perhaps we shouldn't anyway, since internally we
640 use memory ccaches, and a FILE one probably means that
641 we're using creds obtained outside of our exectuable
643 if (strequal(cc_type, "FILE")) {
644 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type));
645 return False;
648 retval = krb5_cc_remove_cred(context, ccache, 0, credsp);
649 if (retval) {
650 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
651 error_message(retval)));
652 /* If we have an error in this, we want to display it,
653 but continue as though we deleted it */
655 return True;
658 /* Allocate and setup the auth context into the state we need. */
660 static krb5_error_code setup_auth_context(krb5_context context,
661 krb5_auth_context *auth_context)
663 krb5_error_code retval;
665 retval = krb5_auth_con_init(context, auth_context );
666 if (retval) {
667 DEBUG(1,("krb5_auth_con_init failed (%s)\n",
668 error_message(retval)));
669 return retval;
672 /* Ensure this is an addressless ticket. */
673 retval = krb5_auth_con_setaddrs(context, *auth_context, NULL, NULL);
674 if (retval) {
675 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n",
676 error_message(retval)));
679 return retval;
682 static krb5_error_code create_gss_checksum(krb5_data *in_data, /* [inout] */
683 uint32_t gss_flags)
685 unsigned int orig_length = in_data->length;
686 unsigned int base_cksum_size = GSSAPI_CHECKSUM_SIZE;
687 char *gss_cksum = NULL;
689 if (orig_length) {
690 /* Extra length field for delgated ticket. */
691 base_cksum_size += 4;
694 if ((unsigned int)base_cksum_size + orig_length <
695 (unsigned int)base_cksum_size) {
696 return EINVAL;
699 gss_cksum = (char *)SMB_MALLOC(base_cksum_size + orig_length);
700 if (gss_cksum == NULL) {
701 return ENOMEM;
704 memset(gss_cksum, '\0', base_cksum_size + orig_length);
705 SIVAL(gss_cksum, 0, GSSAPI_BNDLENGTH);
707 /* Precalculated MD5sum of NULL channel bindings (20 bytes) */
708 /* Channel bindings are: (all ints encoded as little endian)
710 [4 bytes] initiator_addrtype (255 for null bindings)
711 [4 bytes] initiator_address length
712 [n bytes] .. initiator_address data - not present
713 in null bindings.
714 [4 bytes] acceptor_addrtype (255 for null bindings)
715 [4 bytes] acceptor_address length
716 [n bytes] .. acceptor_address data - not present
717 in null bindings.
718 [4 bytes] application_data length
719 [n bytes] .. application_ data - not present
720 in null bindings.
721 MD5 of this is ""\x14\x8f\x0c\xf7\xb1u\xdey*J\x9a%\xdfV\xc5\x18"
724 memcpy(&gss_cksum[4],
725 "\x14\x8f\x0c\xf7\xb1u\xdey*J\x9a%\xdfV\xc5\x18",
726 GSSAPI_BNDLENGTH);
728 SIVAL(gss_cksum, 20, gss_flags);
730 if (orig_length) {
731 SSVAL(gss_cksum, 24, 1); /* The Delegation Option identifier */
732 SSVAL(gss_cksum, 26, orig_length);
733 /* Copy the kerberos KRB_CRED data */
734 memcpy(gss_cksum + 28, in_data->data, orig_length);
735 free(in_data->data);
736 in_data->data = NULL;
737 in_data->length = 0;
739 in_data->data = gss_cksum;
740 in_data->length = base_cksum_size + orig_length;
741 return 0;
745 we can't use krb5_mk_req because w2k wants the service to be in a particular format
747 static krb5_error_code ads_krb5_mk_req(krb5_context context,
748 krb5_auth_context *auth_context,
749 const krb5_flags ap_req_options,
750 const char *principal,
751 krb5_ccache ccache,
752 krb5_data *outbuf,
753 time_t *expire_time,
754 const char *impersonate_princ_s)
756 krb5_error_code retval;
757 krb5_principal server;
758 krb5_principal impersonate_princ = NULL;
759 krb5_creds * credsp;
760 krb5_creds creds;
761 krb5_data in_data;
762 bool creds_ready = False;
763 int i = 0, maxtries = 3;
764 uint32_t gss_flags = 0;
766 ZERO_STRUCT(in_data);
768 retval = smb_krb5_parse_name(context, principal, &server);
769 if (retval) {
770 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal));
771 return retval;
774 if (impersonate_princ_s) {
775 retval = smb_krb5_parse_name(context, impersonate_princ_s,
776 &impersonate_princ);
777 if (retval) {
778 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", impersonate_princ_s));
779 goto cleanup_princ;
783 /* obtain ticket & session key */
784 ZERO_STRUCT(creds);
785 if ((retval = krb5_copy_principal(context, server, &creds.server))) {
786 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n",
787 error_message(retval)));
788 goto cleanup_princ;
791 if ((retval = krb5_cc_get_principal(context, ccache, &creds.client))) {
792 /* This can commonly fail on smbd startup with no ticket in the cache.
793 * Report at higher level than 1. */
794 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
795 error_message(retval)));
796 goto cleanup_creds;
799 while (!creds_ready && (i < maxtries)) {
801 if ((retval = smb_krb5_get_credentials(context, ccache,
802 creds.client,
803 creds.server,
804 impersonate_princ,
805 &credsp))) {
806 DEBUG(1,("ads_krb5_mk_req: smb_krb5_get_credentials failed for %s (%s)\n",
807 principal, error_message(retval)));
808 goto cleanup_creds;
811 /* cope with ticket being in the future due to clock skew */
812 if ((unsigned)credsp->times.starttime > time(NULL)) {
813 time_t t = time(NULL);
814 int time_offset =(int)((unsigned)credsp->times.starttime-t);
815 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset));
816 krb5_set_real_time(context, t + time_offset + 1, 0);
819 if (!ads_cleanup_expired_creds(context, ccache, credsp)) {
820 creds_ready = True;
823 i++;
826 DEBUG(10,("ads_krb5_mk_req: Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
827 principal, krb5_cc_get_type(context, ccache), krb5_cc_get_name(context, ccache),
828 http_timestring(talloc_tos(), (unsigned)credsp->times.endtime),
829 (unsigned)credsp->times.endtime));
831 if (expire_time) {
832 *expire_time = (time_t)credsp->times.endtime;
835 /* Allocate the auth_context. */
836 retval = setup_auth_context(context, auth_context);
837 if (retval) {
838 DEBUG(1,("setup_auth_context failed (%s)\n",
839 error_message(retval)));
840 goto cleanup_creds;
843 #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)
844 if( credsp->ticket_flags & TKT_FLG_OK_AS_DELEGATE ) {
845 /* Fetch a forwarded TGT from the KDC so that we can hand off a 2nd ticket
846 as part of the kerberos exchange. */
848 DEBUG( 3, ("ads_krb5_mk_req: server marked as OK to delegate to, building forwardable TGT\n") );
850 retval = krb5_auth_con_setuseruserkey(context,
851 *auth_context,
852 &credsp->keyblock );
853 if (retval) {
854 DEBUG(1,("krb5_auth_con_setuseruserkey failed (%s)\n",
855 error_message(retval)));
856 goto cleanup_creds;
859 /* Must use a subkey for forwarded tickets. */
860 retval = krb5_auth_con_setflags(context,
861 *auth_context,
862 KRB5_AUTH_CONTEXT_USE_SUBKEY);
863 if (retval) {
864 DEBUG(1,("krb5_auth_con_setflags failed (%s)\n",
865 error_message(retval)));
866 goto cleanup_creds;
869 retval = krb5_fwd_tgt_creds(context,/* Krb5 context [in] */
870 *auth_context, /* Authentication context [in] */
871 CONST_DISCARD(char *, KRB5_TGS_NAME), /* Ticket service name ("krbtgt") [in] */
872 credsp->client, /* Client principal for the tgt [in] */
873 credsp->server, /* Server principal for the tgt [in] */
874 ccache, /* Credential cache to use for storage [in] */
875 1, /* Turn on for "Forwardable ticket" [in] */
876 &in_data ); /* Resulting response [out] */
878 if (retval) {
879 DEBUG( 3, ("krb5_fwd_tgt_creds failed (%s)\n",
880 error_message( retval ) ) );
883 * This is not fatal. Delete the *auth_context and continue
884 * with krb5_mk_req_extended to get a non-forwardable ticket.
887 if (in_data.data) {
888 free( in_data.data );
889 in_data.data = NULL;
890 in_data.length = 0;
892 krb5_auth_con_free(context, *auth_context);
893 *auth_context = NULL;
894 retval = setup_auth_context(context, auth_context);
895 if (retval) {
896 DEBUG(1,("setup_auth_context failed (%s)\n",
897 error_message(retval)));
898 goto cleanup_creds;
900 } else {
901 /* We got a delegated ticket. */
902 gss_flags |= GSS_C_DELEG_FLAG;
905 #endif
907 /* Frees and reallocates in_data into a GSS checksum blob. */
908 retval = create_gss_checksum(&in_data, gss_flags);
909 if (retval) {
910 goto cleanup_data;
913 #if defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE)
914 /* We always want GSS-checksum types. */
915 retval = krb5_auth_con_set_req_cksumtype(context, *auth_context, GSSAPI_CHECKSUM );
916 if (retval) {
917 DEBUG(1,("krb5_auth_con_set_req_cksumtype failed (%s)\n",
918 error_message(retval)));
919 goto cleanup_data;
921 #endif
923 retval = krb5_mk_req_extended(context, auth_context, ap_req_options,
924 &in_data, credsp, outbuf);
925 if (retval) {
926 DEBUG(1,("ads_krb5_mk_req: krb5_mk_req_extended failed (%s)\n",
927 error_message(retval)));
930 cleanup_data:
931 if (in_data.data) {
932 free( in_data.data );
933 in_data.length = 0;
936 krb5_free_creds(context, credsp);
938 cleanup_creds:
939 krb5_free_cred_contents(context, &creds);
941 cleanup_princ:
942 krb5_free_principal(context, server);
943 if (impersonate_princ) {
944 krb5_free_principal(context, impersonate_princ);
947 return retval;
951 get a kerberos5 ticket for the given service
953 int cli_krb5_get_ticket(TALLOC_CTX *mem_ctx,
954 const char *principal, time_t time_offset,
955 DATA_BLOB *ticket, DATA_BLOB *session_key_krb5,
956 uint32_t extra_ap_opts, const char *ccname,
957 time_t *tgs_expire,
958 const char *impersonate_princ_s)
961 krb5_error_code retval;
962 krb5_data packet;
963 krb5_context context = NULL;
964 krb5_ccache ccdef = NULL;
965 krb5_auth_context auth_context = NULL;
966 krb5_enctype enc_types[] = {
967 ENCTYPE_ARCFOUR_HMAC,
968 ENCTYPE_DES_CBC_MD5,
969 ENCTYPE_DES_CBC_CRC,
970 ENCTYPE_NULL};
972 initialize_krb5_error_table();
973 retval = krb5_init_context(&context);
974 if (retval) {
975 DEBUG(1, ("krb5_init_context failed (%s)\n",
976 error_message(retval)));
977 goto failed;
980 if (time_offset != 0) {
981 krb5_set_real_time(context, time(NULL) + time_offset, 0);
984 if ((retval = krb5_cc_resolve(context, ccname ?
985 ccname : krb5_cc_default_name(context), &ccdef))) {
986 DEBUG(1, ("krb5_cc_default failed (%s)\n",
987 error_message(retval)));
988 goto failed;
991 if ((retval = krb5_set_default_tgs_ktypes(context, enc_types))) {
992 DEBUG(1, ("krb5_set_default_tgs_ktypes failed (%s)\n",
993 error_message(retval)));
994 goto failed;
997 retval = ads_krb5_mk_req(context, &auth_context,
998 AP_OPTS_USE_SUBKEY | (krb5_flags)extra_ap_opts,
999 principal, ccdef, &packet,
1000 tgs_expire, impersonate_princ_s);
1001 if (retval) {
1002 goto failed;
1005 get_krb5_smb_session_key(mem_ctx, context, auth_context,
1006 session_key_krb5, false);
1008 *ticket = data_blob_talloc(mem_ctx, packet.data, packet.length);
1010 kerberos_free_data_contents(context, &packet);
1012 failed:
1014 if (context) {
1015 if (ccdef)
1016 krb5_cc_close(context, ccdef);
1017 if (auth_context)
1018 krb5_auth_con_free(context, auth_context);
1019 krb5_free_context(context);
1022 return retval;
1025 bool get_krb5_smb_session_key(TALLOC_CTX *mem_ctx,
1026 krb5_context context,
1027 krb5_auth_context auth_context,
1028 DATA_BLOB *session_key, bool remote)
1030 krb5_keyblock *skey = NULL;
1031 krb5_error_code err = 0;
1032 bool ret = false;
1034 if (remote) {
1035 err = krb5_auth_con_getremotesubkey(context,
1036 auth_context, &skey);
1037 } else {
1038 err = krb5_auth_con_getlocalsubkey(context,
1039 auth_context, &skey);
1042 if (err || skey == NULL) {
1043 DEBUG(10, ("KRB5 error getting session key %d\n", err));
1044 goto done;
1047 DEBUG(10, ("Got KRB5 session key of length %d\n",
1048 (int)KRB5_KEY_LENGTH(skey)));
1050 *session_key = data_blob_talloc(mem_ctx,
1051 KRB5_KEY_DATA(skey),
1052 KRB5_KEY_LENGTH(skey));
1053 dump_data_pw("KRB5 Session Key:\n",
1054 session_key->data,
1055 session_key->length);
1057 ret = true;
1059 done:
1060 if (skey) {
1061 krb5_free_keyblock(context, skey);
1064 return ret;
1068 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
1069 const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i );
1071 const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i )
1073 static krb5_data kdata;
1075 kdata.data = (char *)krb5_principal_get_comp_string(context, principal, i);
1076 kdata.length = strlen((const char *)kdata.data);
1077 return &kdata;
1079 #endif
1081 krb5_error_code smb_krb5_kt_free_entry(krb5_context context, krb5_keytab_entry *kt_entry)
1083 /* Try krb5_free_keytab_entry_contents first, since
1084 * MIT Kerberos >= 1.7 has both krb5_free_keytab_entry_contents and
1085 * krb5_kt_free_entry but only has a prototype for the first, while the
1086 * second is considered private.
1088 #if defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
1089 return krb5_free_keytab_entry_contents(context, kt_entry);
1090 #elif defined(HAVE_KRB5_KT_FREE_ENTRY)
1091 return krb5_kt_free_entry(context, kt_entry);
1092 #else
1093 #error UNKNOWN_KT_FREE_FUNCTION
1094 #endif
1097 void smb_krb5_checksum_from_pac_sig(krb5_checksum *cksum,
1098 struct PAC_SIGNATURE_DATA *sig)
1100 #ifdef HAVE_CHECKSUM_IN_KRB5_CHECKSUM
1101 cksum->cksumtype = (krb5_cksumtype)sig->type;
1102 cksum->checksum.length = sig->signature.length;
1103 cksum->checksum.data = sig->signature.data;
1104 #else
1105 cksum->checksum_type = (krb5_cksumtype)sig->type;
1106 cksum->length = sig->signature.length;
1107 cksum->contents = sig->signature.data;
1108 #endif
1111 krb5_error_code smb_krb5_verify_checksum(krb5_context context,
1112 const krb5_keyblock *keyblock,
1113 krb5_keyusage usage,
1114 krb5_checksum *cksum,
1115 uint8 *data,
1116 size_t length)
1118 krb5_error_code ret;
1120 /* verify the checksum */
1122 /* welcome to the wonderful world of samba's kerberos abstraction layer:
1124 * function heimdal 0.6.1rc3 heimdal 0.7 MIT krb 1.4.2
1125 * -----------------------------------------------------------------------------
1126 * krb5_c_verify_checksum - works works
1127 * krb5_verify_checksum works (6 args) works (6 args) broken (7 args)
1130 #if defined(HAVE_KRB5_C_VERIFY_CHECKSUM)
1132 krb5_boolean checksum_valid = False;
1133 krb5_data input;
1135 input.data = (char *)data;
1136 input.length = length;
1138 ret = krb5_c_verify_checksum(context,
1139 keyblock,
1140 usage,
1141 &input,
1142 cksum,
1143 &checksum_valid);
1144 if (ret) {
1145 DEBUG(3,("smb_krb5_verify_checksum: krb5_c_verify_checksum() failed: %s\n",
1146 error_message(ret)));
1147 return ret;
1150 if (!checksum_valid)
1151 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1154 #elif KRB5_VERIFY_CHECKSUM_ARGS == 6 && defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CRYPTO) && defined(HAVE_KRB5_CRYPTO_DESTROY)
1156 /* Warning: MIT's krb5_verify_checksum cannot be used as it will use a key
1157 * without enctype and it ignores any key_usage types - Guenther */
1161 krb5_crypto crypto;
1162 ret = krb5_crypto_init(context,
1163 keyblock,
1165 &crypto);
1166 if (ret) {
1167 DEBUG(0,("smb_krb5_verify_checksum: krb5_crypto_init() failed: %s\n",
1168 error_message(ret)));
1169 return ret;
1172 ret = krb5_verify_checksum(context,
1173 crypto,
1174 usage,
1175 data,
1176 length,
1177 cksum);
1179 krb5_crypto_destroy(context, crypto);
1182 #else
1183 #error UNKNOWN_KRB5_VERIFY_CHECKSUM_FUNCTION
1184 #endif
1186 return ret;
1189 time_t get_authtime_from_tkt(krb5_ticket *tkt)
1191 #if defined(HAVE_KRB5_TKT_ENC_PART2)
1192 return tkt->enc_part2->times.authtime;
1193 #else
1194 return tkt->ticket.authtime;
1195 #endif
1198 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
1199 static int get_kvno_from_ap_req(krb5_ap_req *ap_req)
1201 #ifdef HAVE_TICKET_POINTER_IN_KRB5_AP_REQ /* MIT */
1202 if (ap_req->ticket->enc_part.kvno)
1203 return ap_req->ticket->enc_part.kvno;
1204 #else /* Heimdal */
1205 if (ap_req->ticket.enc_part.kvno)
1206 return *ap_req->ticket.enc_part.kvno;
1207 #endif
1208 return 0;
1211 static krb5_enctype get_enctype_from_ap_req(krb5_ap_req *ap_req)
1213 #ifdef HAVE_ETYPE_IN_ENCRYPTEDDATA /* Heimdal */
1214 return ap_req->ticket.enc_part.etype;
1215 #else /* MIT */
1216 return ap_req->ticket->enc_part.enctype;
1217 #endif
1219 #endif /* HAVE_KRB5_DECODE_AP_REQ */
1221 static krb5_error_code
1222 get_key_from_keytab(krb5_context context,
1223 krb5_const_principal server,
1224 krb5_enctype enctype,
1225 krb5_kvno kvno,
1226 krb5_keyblock **out_key)
1228 krb5_keytab_entry entry;
1229 krb5_error_code ret;
1230 krb5_keytab keytab;
1231 char *name = NULL;
1232 krb5_keyblock *keyp;
1234 /* We have to open a new keytab handle here, as MIT does
1235 an implicit open/getnext/close on krb5_kt_get_entry. We
1236 may be in the middle of a keytab enumeration when this is
1237 called. JRA. */
1239 ret = smb_krb5_open_keytab(context, NULL, False, &keytab);
1240 if (ret) {
1241 DEBUG(1,("get_key_from_keytab: smb_krb5_open_keytab failed (%s)\n", error_message(ret)));
1242 return ret;
1245 if ( DEBUGLEVEL >= 10 ) {
1246 if (smb_krb5_unparse_name(talloc_tos(), context, server, &name) == 0) {
1247 DEBUG(10,("get_key_from_keytab: will look for kvno %d, enctype %d and name: %s\n",
1248 kvno, enctype, name));
1249 TALLOC_FREE(name);
1253 ret = krb5_kt_get_entry(context,
1254 keytab,
1255 server,
1256 kvno,
1257 enctype,
1258 &entry);
1260 if (ret) {
1261 DEBUG(0,("get_key_from_keytab: failed to retrieve key: %s\n", error_message(ret)));
1262 goto out;
1265 keyp = KRB5_KT_KEY(&entry);
1267 ret = krb5_copy_keyblock(context, keyp, out_key);
1268 if (ret) {
1269 DEBUG(0,("get_key_from_keytab: failed to copy key: %s\n", error_message(ret)));
1270 goto out;
1273 smb_krb5_kt_free_entry(context, &entry);
1275 out:
1276 krb5_kt_close(context, keytab);
1277 return ret;
1280 /* Prototypes */
1282 krb5_error_code smb_krb5_get_keyinfo_from_ap_req(krb5_context context,
1283 const krb5_data *inbuf,
1284 krb5_kvno *kvno,
1285 krb5_enctype *enctype)
1287 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
1289 krb5_error_code ret;
1290 krb5_ap_req ap_req;
1292 ret = krb5_decode_ap_req(context, inbuf, &ap_req);
1293 if (ret)
1294 return ret;
1296 *kvno = get_kvno_from_ap_req(&ap_req);
1297 *enctype = get_enctype_from_ap_req(&ap_req);
1299 free_AP_REQ(&ap_req);
1300 return 0;
1302 #endif
1304 /* Possibly not an appropriate error code. */
1305 return KRB5KDC_ERR_BADOPTION;
1308 krb5_error_code krb5_rd_req_return_keyblock_from_keytab(krb5_context context,
1309 krb5_auth_context *auth_context,
1310 const krb5_data *inbuf,
1311 krb5_const_principal server,
1312 krb5_keytab keytab,
1313 krb5_flags *ap_req_options,
1314 krb5_ticket **ticket,
1315 krb5_keyblock **keyblock)
1317 krb5_error_code ret;
1318 krb5_kvno kvno;
1319 krb5_enctype enctype;
1320 krb5_keyblock *local_keyblock;
1322 ret = krb5_rd_req(context,
1323 auth_context,
1324 inbuf,
1325 server,
1326 keytab,
1327 ap_req_options,
1328 ticket);
1329 if (ret) {
1330 return ret;
1333 #ifdef KRB5_TICKET_HAS_KEYINFO
1334 enctype = (*ticket)->enc_part.enctype;
1335 kvno = (*ticket)->enc_part.kvno;
1336 #else
1337 ret = smb_krb5_get_keyinfo_from_ap_req(context, inbuf, &kvno, &enctype);
1338 if (ret) {
1339 return ret;
1341 #endif
1343 ret = get_key_from_keytab(context,
1344 server,
1345 enctype,
1346 kvno,
1347 &local_keyblock);
1348 if (ret) {
1349 DEBUG(0,("krb5_rd_req_return_keyblock_from_keytab: failed to call get_key_from_keytab\n"));
1350 goto out;
1353 out:
1354 if (ret && local_keyblock != NULL) {
1355 krb5_free_keyblock(context, local_keyblock);
1356 } else {
1357 *keyblock = local_keyblock;
1360 return ret;
1363 krb5_error_code smb_krb5_parse_name_norealm(krb5_context context,
1364 const char *name,
1365 krb5_principal *principal)
1367 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
1368 return smb_krb5_parse_name_norealm_conv(context, name, principal);
1369 #endif
1371 /* we are cheating here because parse_name will in fact set the realm.
1372 * We don't care as the only caller of smb_krb5_parse_name_norealm
1373 * ignores the realm anyway when calling
1374 * smb_krb5_principal_compare_any_realm later - Guenther */
1376 return smb_krb5_parse_name(context, name, principal);
1379 bool smb_krb5_principal_compare_any_realm(krb5_context context,
1380 krb5_const_principal princ1,
1381 krb5_const_principal princ2)
1383 #ifdef HAVE_KRB5_PRINCIPAL_COMPARE_ANY_REALM
1385 return krb5_principal_compare_any_realm(context, princ1, princ2);
1387 /* krb5_princ_size is a macro in MIT */
1388 #elif defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
1390 int i, len1, len2;
1391 const krb5_data *p1, *p2;
1393 len1 = krb5_princ_size(context, princ1);
1394 len2 = krb5_princ_size(context, princ2);
1396 if (len1 != len2)
1397 return False;
1399 for (i = 0; i < len1; i++) {
1401 p1 = krb5_princ_component(context, CONST_DISCARD(krb5_principal, princ1), i);
1402 p2 = krb5_princ_component(context, CONST_DISCARD(krb5_principal, princ2), i);
1404 if (p1->length != p2->length || memcmp(p1->data, p2->data, p1->length))
1405 return False;
1408 return True;
1409 #else
1410 #error NO_SUITABLE_PRINCIPAL_COMPARE_FUNCTION
1411 #endif
1414 krb5_error_code smb_krb5_renew_ticket(const char *ccache_string, /* FILE:/tmp/krb5cc_0 */
1415 const char *client_string, /* gd@BER.SUSE.DE */
1416 const char *service_string, /* krbtgt/BER.SUSE.DE@BER.SUSE.DE */
1417 time_t *expire_time)
1419 krb5_error_code ret;
1420 krb5_context context = NULL;
1421 krb5_ccache ccache = NULL;
1422 krb5_principal client = NULL;
1423 krb5_creds creds, creds_in, *creds_out = NULL;
1425 ZERO_STRUCT(creds);
1426 ZERO_STRUCT(creds_in);
1428 initialize_krb5_error_table();
1429 ret = krb5_init_context(&context);
1430 if (ret) {
1431 goto done;
1434 if (!ccache_string) {
1435 ccache_string = krb5_cc_default_name(context);
1438 if (!ccache_string) {
1439 ret = EINVAL;
1440 goto done;
1443 DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string));
1445 /* FIXME: we should not fall back to defaults */
1446 ret = krb5_cc_resolve(context, CONST_DISCARD(char *, ccache_string), &ccache);
1447 if (ret) {
1448 goto done;
1451 if (client_string) {
1452 ret = smb_krb5_parse_name(context, client_string, &client);
1453 if (ret) {
1454 goto done;
1456 } else {
1457 ret = krb5_cc_get_principal(context, ccache, &client);
1458 if (ret) {
1459 goto done;
1463 #ifdef HAVE_KRB5_GET_RENEWED_CREDS /* MIT */
1465 ret = krb5_get_renewed_creds(context, &creds, client, ccache, CONST_DISCARD(char *, service_string));
1466 if (ret) {
1467 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
1468 goto done;
1471 #elif defined(HAVE_KRB5_GET_KDC_CRED) /* Heimdal */
1473 krb5_kdc_flags flags;
1474 krb5_realm *client_realm = NULL;
1476 ret = krb5_copy_principal(context, client, &creds_in.client);
1477 if (ret) {
1478 goto done;
1481 if (service_string) {
1482 ret = smb_krb5_parse_name(context, service_string, &creds_in.server);
1483 if (ret) {
1484 goto done;
1486 } else {
1487 /* build tgt service by default */
1488 client_realm = krb5_princ_realm(context, creds_in.client);
1489 if (!client_realm) {
1490 ret = ENOMEM;
1491 goto done;
1493 ret = krb5_make_principal(context, &creds_in.server, *client_realm, KRB5_TGS_NAME, *client_realm, NULL);
1494 if (ret) {
1495 goto done;
1499 flags.i = 0;
1500 flags.b.renewable = flags.b.renew = True;
1502 ret = krb5_get_kdc_cred(context, ccache, flags, NULL, NULL, &creds_in, &creds_out);
1503 if (ret) {
1504 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
1505 goto done;
1508 creds = *creds_out;
1510 #else
1511 #error NO_SUITABLE_KRB5_TICKET_RENEW_FUNCTION_AVAILABLE
1512 #endif
1514 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
1515 ret = krb5_cc_initialize(context, ccache, client);
1516 if (ret) {
1517 goto done;
1520 ret = krb5_cc_store_cred(context, ccache, &creds);
1522 if (expire_time) {
1523 *expire_time = (time_t) creds.times.endtime;
1526 done:
1527 krb5_free_cred_contents(context, &creds_in);
1529 if (creds_out) {
1530 krb5_free_creds(context, creds_out);
1531 } else {
1532 krb5_free_cred_contents(context, &creds);
1535 if (client) {
1536 krb5_free_principal(context, client);
1538 if (ccache) {
1539 krb5_cc_close(context, ccache);
1541 if (context) {
1542 krb5_free_context(context);
1545 return ret;
1548 krb5_error_code smb_krb5_free_addresses(krb5_context context, smb_krb5_addresses *addr)
1550 krb5_error_code ret = 0;
1551 if (addr == NULL) {
1552 return ret;
1554 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1555 krb5_free_addresses(context, addr->addrs);
1556 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1557 ret = krb5_free_addresses(context, addr->addrs);
1558 SAFE_FREE(addr->addrs);
1559 #endif
1560 SAFE_FREE(addr);
1561 addr = NULL;
1562 return ret;
1565 krb5_error_code smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses **kerb_addr)
1567 krb5_error_code ret = 0;
1568 nstring buf;
1569 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1570 krb5_address **addrs = NULL;
1571 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1572 krb5_addresses *addrs = NULL;
1573 #endif
1575 *kerb_addr = (smb_krb5_addresses *)SMB_MALLOC(sizeof(smb_krb5_addresses));
1576 if (*kerb_addr == NULL) {
1577 return ENOMEM;
1580 put_name(buf, global_myname(), ' ', 0x20);
1582 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1584 int num_addr = 2;
1586 addrs = (krb5_address **)SMB_MALLOC(sizeof(krb5_address *) * num_addr);
1587 if (addrs == NULL) {
1588 SAFE_FREE(*kerb_addr);
1589 return ENOMEM;
1592 memset(addrs, 0, sizeof(krb5_address *) * num_addr);
1594 addrs[0] = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
1595 if (addrs[0] == NULL) {
1596 SAFE_FREE(addrs);
1597 SAFE_FREE(*kerb_addr);
1598 return ENOMEM;
1601 addrs[0]->magic = KV5M_ADDRESS;
1602 addrs[0]->addrtype = KRB5_ADDR_NETBIOS;
1603 addrs[0]->length = MAX_NETBIOSNAME_LEN;
1604 addrs[0]->contents = (unsigned char *)SMB_MALLOC(addrs[0]->length);
1605 if (addrs[0]->contents == NULL) {
1606 SAFE_FREE(addrs[0]);
1607 SAFE_FREE(addrs);
1608 SAFE_FREE(*kerb_addr);
1609 return ENOMEM;
1612 memcpy(addrs[0]->contents, buf, addrs[0]->length);
1614 addrs[1] = NULL;
1616 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1618 addrs = (krb5_addresses *)SMB_MALLOC(sizeof(krb5_addresses));
1619 if (addrs == NULL) {
1620 SAFE_FREE(*kerb_addr);
1621 return ENOMEM;
1624 memset(addrs, 0, sizeof(krb5_addresses));
1626 addrs->len = 1;
1627 addrs->val = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
1628 if (addrs->val == NULL) {
1629 SAFE_FREE(addrs);
1630 SAFE_FREE(kerb_addr);
1631 return ENOMEM;
1634 addrs->val[0].addr_type = KRB5_ADDR_NETBIOS;
1635 addrs->val[0].address.length = MAX_NETBIOSNAME_LEN;
1636 addrs->val[0].address.data = (unsigned char *)SMB_MALLOC(addrs->val[0].address.length);
1637 if (addrs->val[0].address.data == NULL) {
1638 SAFE_FREE(addrs->val);
1639 SAFE_FREE(addrs);
1640 SAFE_FREE(*kerb_addr);
1641 return ENOMEM;
1644 memcpy(addrs->val[0].address.data, buf, addrs->val[0].address.length);
1646 #else
1647 #error UNKNOWN_KRB5_ADDRESS_FORMAT
1648 #endif
1649 (*kerb_addr)->addrs = addrs;
1651 return ret;
1654 void smb_krb5_free_error(krb5_context context, krb5_error *krberror)
1656 #ifdef HAVE_KRB5_FREE_ERROR_CONTENTS /* Heimdal */
1657 krb5_free_error_contents(context, krberror);
1658 #else /* MIT */
1659 krb5_free_error(context, krberror);
1660 #endif
1663 krb5_error_code handle_krberror_packet(krb5_context context,
1664 krb5_data *packet)
1666 krb5_error_code ret;
1667 bool got_error_code = False;
1669 DEBUG(10,("handle_krberror_packet: got error packet\n"));
1671 #ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR /* Heimdal */
1673 krb5_error krberror;
1675 if ((ret = krb5_rd_error(context, packet, &krberror))) {
1676 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1677 error_message(ret)));
1678 return ret;
1681 if (krberror.e_data == NULL || krberror.e_data->data == NULL) {
1682 ret = (krb5_error_code) krberror.error_code;
1683 got_error_code = True;
1686 smb_krb5_free_error(context, &krberror);
1688 #else /* MIT */
1690 krb5_error *krberror;
1692 if ((ret = krb5_rd_error(context, packet, &krberror))) {
1693 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1694 error_message(ret)));
1695 return ret;
1698 if (krberror->e_data.data == NULL) {
1699 #if defined(ERROR_TABLE_BASE_krb5)
1700 ret = ERROR_TABLE_BASE_krb5 + (krb5_error_code) krberror->error;
1701 #else
1702 ret = (krb5_error_code)krberror->error;
1703 #endif
1704 got_error_code = True;
1706 smb_krb5_free_error(context, krberror);
1708 #endif
1709 if (got_error_code) {
1710 DEBUG(5,("handle_krberror_packet: got KERBERR from kpasswd: %s (%d)\n",
1711 error_message(ret), ret));
1713 return ret;
1716 krb5_error_code smb_krb5_get_init_creds_opt_alloc(krb5_context context,
1717 krb5_get_init_creds_opt **opt)
1719 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
1720 /* Heimdal or modern MIT version */
1721 return krb5_get_init_creds_opt_alloc(context, opt);
1722 #else
1723 /* Historical MIT version */
1724 krb5_get_init_creds_opt *my_opt;
1726 *opt = NULL;
1728 if ((my_opt = SMB_MALLOC_P(krb5_get_init_creds_opt)) == NULL) {
1729 return ENOMEM;
1732 krb5_get_init_creds_opt_init(my_opt);
1734 *opt = my_opt;
1735 return 0;
1736 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC */
1739 void smb_krb5_get_init_creds_opt_free(krb5_context context,
1740 krb5_get_init_creds_opt *opt)
1742 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_FREE
1744 #ifdef KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT
1745 /* Modern MIT or Heimdal version */
1746 krb5_get_init_creds_opt_free(context, opt);
1747 #else
1748 /* Heimdal version */
1749 krb5_get_init_creds_opt_free(opt);
1750 #endif /* KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT */
1752 #else /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1753 /* Historical MIT version */
1754 SAFE_FREE(opt);
1755 opt = NULL;
1756 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1759 krb5_enctype smb_get_enctype_from_kt_entry(krb5_keytab_entry *kt_entry)
1761 return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry));
1765 /* caller needs to free etype_s */
1766 krb5_error_code smb_krb5_enctype_to_string(krb5_context context,
1767 krb5_enctype enctype,
1768 char **etype_s)
1770 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
1771 return krb5_enctype_to_string(context, enctype, etype_s); /* Heimdal */
1772 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
1773 char buf[256];
1774 krb5_error_code ret = krb5_enctype_to_string(enctype, buf, 256); /* MIT */
1775 if (ret) {
1776 return ret;
1778 *etype_s = SMB_STRDUP(buf);
1779 if (!*etype_s) {
1780 return ENOMEM;
1782 return ret;
1783 #else
1784 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
1785 #endif
1788 krb5_error_code smb_krb5_mk_error(krb5_context context,
1789 krb5_error_code error_code,
1790 const krb5_principal server,
1791 krb5_data *reply)
1793 #ifdef HAVE_SHORT_KRB5_MK_ERROR_INTERFACE /* MIT */
1795 * The MIT interface is *terrible*.
1796 * We have to construct this ourselves...
1798 krb5_error e;
1800 memset(&e, 0, sizeof(e));
1801 krb5_us_timeofday(context, &e.stime, &e.susec);
1802 e.server = server;
1803 #if defined(krb5_err_base)
1804 e.error = error_code - krb5_err_base;
1805 #elif defined(ERROR_TABLE_BASE_krb5)
1806 e.error = error_code - ERROR_TABLE_BASE_krb5;
1807 #else
1808 e.error = error_code; /* Almost certainly wrong, but what can we do... ? */
1809 #endif
1811 return krb5_mk_error(context, &e, reply);
1812 #else /* Heimdal. */
1813 return krb5_mk_error(context,
1814 error_code,
1815 NULL,
1816 NULL, /* e_data */
1817 NULL,
1818 server,
1819 NULL,
1820 NULL,
1821 reply);
1822 #endif
1825 /**********************************************************************
1826 * Open a krb5 keytab with flags, handles readonly or readwrite access and
1827 * allows to process non-default keytab names.
1828 * @param context krb5_context
1829 * @param keytab_name_req string
1830 * @param write_access bool if writable keytab is required
1831 * @param krb5_keytab pointer to krb5_keytab (close with krb5_kt_close())
1832 * @return krb5_error_code
1833 **********************************************************************/
1835 /* This MAX_NAME_LEN is a constant defined in krb5.h */
1836 #ifndef MAX_KEYTAB_NAME_LEN
1837 #define MAX_KEYTAB_NAME_LEN 1100
1838 #endif
1840 krb5_error_code smb_krb5_open_keytab(krb5_context context,
1841 const char *keytab_name_req,
1842 bool write_access,
1843 krb5_keytab *keytab)
1845 krb5_error_code ret = 0;
1846 TALLOC_CTX *mem_ctx;
1847 char keytab_string[MAX_KEYTAB_NAME_LEN];
1848 char *kt_str = NULL;
1849 bool found_valid_name = False;
1850 const char *pragma = "FILE";
1851 const char *tmp = NULL;
1853 if (!write_access && !keytab_name_req) {
1854 /* caller just wants to read the default keytab readonly, so be it */
1855 return krb5_kt_default(context, keytab);
1858 mem_ctx = talloc_init("smb_krb5_open_keytab");
1859 if (!mem_ctx) {
1860 return ENOMEM;
1863 #ifdef HAVE_WRFILE_KEYTAB
1864 if (write_access) {
1865 pragma = "WRFILE";
1867 #endif
1869 if (keytab_name_req) {
1871 if (strlen(keytab_name_req) > MAX_KEYTAB_NAME_LEN) {
1872 ret = KRB5_CONFIG_NOTENUFSPACE;
1873 goto out;
1876 if ((strncmp(keytab_name_req, "WRFILE:/", 8) == 0) ||
1877 (strncmp(keytab_name_req, "FILE:/", 6) == 0)) {
1878 tmp = keytab_name_req;
1879 goto resolve;
1882 if (keytab_name_req[0] != '/') {
1883 ret = KRB5_KT_BADNAME;
1884 goto out;
1887 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, keytab_name_req);
1888 if (!tmp) {
1889 ret = ENOMEM;
1890 goto out;
1893 goto resolve;
1896 /* we need to handle more complex keytab_strings, like:
1897 * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
1899 ret = krb5_kt_default_name(context, &keytab_string[0], MAX_KEYTAB_NAME_LEN - 2);
1900 if (ret) {
1901 goto out;
1904 DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string));
1906 tmp = talloc_strdup(mem_ctx, keytab_string);
1907 if (!tmp) {
1908 ret = ENOMEM;
1909 goto out;
1912 if (strncmp(tmp, "ANY:", 4) == 0) {
1913 tmp += 4;
1916 memset(&keytab_string, '\0', sizeof(keytab_string));
1918 while (next_token_talloc(mem_ctx, &tmp, &kt_str, ",")) {
1919 if (strncmp(kt_str, "WRFILE:", 7) == 0) {
1920 found_valid_name = True;
1921 tmp = kt_str;
1922 tmp += 7;
1925 if (strncmp(kt_str, "FILE:", 5) == 0) {
1926 found_valid_name = True;
1927 tmp = kt_str;
1928 tmp += 5;
1931 if (tmp[0] == '/') {
1932 /* Treat as a FILE: keytab definition. */
1933 found_valid_name = true;
1936 if (found_valid_name) {
1937 if (tmp[0] != '/') {
1938 ret = KRB5_KT_BADNAME;
1939 goto out;
1942 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, tmp);
1943 if (!tmp) {
1944 ret = ENOMEM;
1945 goto out;
1947 break;
1951 if (!found_valid_name) {
1952 ret = KRB5_KT_UNKNOWN_TYPE;
1953 goto out;
1956 resolve:
1957 DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp));
1958 ret = krb5_kt_resolve(context, tmp, keytab);
1960 out:
1961 TALLOC_FREE(mem_ctx);
1962 return ret;
1965 krb5_error_code smb_krb5_keytab_name(TALLOC_CTX *mem_ctx,
1966 krb5_context context,
1967 krb5_keytab keytab,
1968 const char **keytab_name)
1970 char keytab_string[MAX_KEYTAB_NAME_LEN];
1971 krb5_error_code ret = 0;
1973 ret = krb5_kt_get_name(context, keytab,
1974 keytab_string, MAX_KEYTAB_NAME_LEN - 2);
1975 if (ret) {
1976 return ret;
1979 *keytab_name = talloc_strdup(mem_ctx, keytab_string);
1980 if (!*keytab_name) {
1981 return ENOMEM;
1984 return ret;
1987 #if defined(HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE) && \
1988 defined(HAVE_KRB5_GET_CREDS_OPT_ALLOC) && \
1989 defined(HAVE_KRB5_GET_CREDS)
1990 static krb5_error_code smb_krb5_get_credentials_for_user_opt(krb5_context context,
1991 krb5_ccache ccache,
1992 krb5_principal me,
1993 krb5_principal server,
1994 krb5_principal impersonate_princ,
1995 krb5_creds **out_creds)
1997 krb5_error_code ret;
1998 krb5_get_creds_opt opt;
2000 ret = krb5_get_creds_opt_alloc(context, &opt);
2001 if (ret) {
2002 goto done;
2004 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_FORWARDABLE);
2006 if (impersonate_princ) {
2007 ret = krb5_get_creds_opt_set_impersonate(context, opt,
2008 impersonate_princ);
2009 if (ret) {
2010 goto done;
2014 ret = krb5_get_creds(context, opt, ccache, server, out_creds);
2015 if (ret) {
2016 goto done;
2019 done:
2020 if (opt) {
2021 krb5_get_creds_opt_free(context, opt);
2023 return ret;
2025 #endif /* HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE */
2027 #ifdef HAVE_KRB5_GET_CREDENTIALS_FOR_USER
2028 static krb5_error_code smb_krb5_get_credentials_for_user(krb5_context context,
2029 krb5_ccache ccache,
2030 krb5_principal me,
2031 krb5_principal server,
2032 krb5_principal impersonate_princ,
2033 krb5_creds **out_creds)
2035 krb5_error_code ret;
2036 krb5_creds in_creds;
2038 #if !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER
2039 krb5_error_code KRB5_CALLCONV
2040 krb5_get_credentials_for_user(krb5_context context, krb5_flags options,
2041 krb5_ccache ccache, krb5_creds *in_creds,
2042 krb5_data *subject_cert,
2043 krb5_creds **out_creds);
2044 #endif /* !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER */
2046 ZERO_STRUCT(in_creds);
2048 if (impersonate_princ) {
2050 in_creds.server = me;
2051 in_creds.client = impersonate_princ;
2053 ret = krb5_get_credentials_for_user(context,
2054 0, /* krb5_flags options */
2055 ccache,
2056 &in_creds,
2057 NULL, /* krb5_data *subject_cert */
2058 out_creds);
2059 } else {
2060 in_creds.client = me;
2061 in_creds.server = server;
2063 ret = krb5_get_credentials(context, 0, ccache,
2064 &in_creds, out_creds);
2067 return ret;
2069 #endif /* HAVE_KRB5_GET_CREDENTIALS_FOR_USER */
2072 * smb_krb5_get_credentials
2074 * @brief Get krb5 credentials for a server
2076 * @param[in] context An initialized krb5_context
2077 * @param[in] ccache An initialized krb5_ccache
2078 * @param[in] me The krb5_principal of the caller
2079 * @param[in] server The krb5_principal of the requested service
2080 * @param[in] impersonate_princ The krb5_principal of a user to impersonate as (optional)
2081 * @param[out] out_creds The returned krb5_creds structure
2082 * @return krb5_error_code
2085 krb5_error_code smb_krb5_get_credentials(krb5_context context,
2086 krb5_ccache ccache,
2087 krb5_principal me,
2088 krb5_principal server,
2089 krb5_principal impersonate_princ,
2090 krb5_creds **out_creds)
2092 krb5_error_code ret;
2093 krb5_creds *creds = NULL;
2095 *out_creds = NULL;
2097 if (impersonate_princ) {
2098 #ifdef HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE /* Heimdal */
2099 ret = smb_krb5_get_credentials_for_user_opt(context, ccache, me, server, impersonate_princ, &creds);
2100 #elif defined(HAVE_KRB5_GET_CREDENTIALS_FOR_USER) /* MIT */
2101 ret = smb_krb5_get_credentials_for_user(context, ccache, me, server, impersonate_princ, &creds);
2102 #else
2103 ret = ENOTSUP;
2104 #endif
2105 } else {
2106 krb5_creds in_creds;
2108 ZERO_STRUCT(in_creds);
2110 in_creds.client = me;
2111 in_creds.server = server;
2113 ret = krb5_get_credentials(context, 0, ccache,
2114 &in_creds, &creds);
2116 if (ret) {
2117 goto done;
2120 ret = krb5_cc_store_cred(context, ccache, creds);
2121 if (ret) {
2122 goto done;
2125 if (out_creds) {
2126 *out_creds = creds;
2129 done:
2130 if (creds && ret) {
2131 krb5_free_creds(context, creds);
2134 return ret;
2138 * smb_krb5_get_creds
2140 * @brief Get krb5 credentials for a server
2142 * @param[in] server_s The string name of the service
2143 * @param[in] time_offset The offset to the KDCs time in seconds (optional)
2144 * @param[in] cc The krb5 credential cache string name (optional)
2145 * @param[in] impersonate_princ_s The string principal name to impersonate (optional)
2146 * @param[out] creds_p The returned krb5_creds structure
2147 * @return krb5_error_code
2150 krb5_error_code smb_krb5_get_creds(const char *server_s,
2151 time_t time_offset,
2152 const char *cc,
2153 const char *impersonate_princ_s,
2154 krb5_creds **creds_p)
2156 krb5_error_code ret;
2157 krb5_context context = NULL;
2158 krb5_principal me = NULL;
2159 krb5_principal server = NULL;
2160 krb5_principal impersonate_princ = NULL;
2161 krb5_creds *creds = NULL;
2162 krb5_ccache ccache = NULL;
2164 *creds_p = NULL;
2166 initialize_krb5_error_table();
2167 ret = krb5_init_context(&context);
2168 if (ret) {
2169 goto done;
2172 if (time_offset != 0) {
2173 krb5_set_real_time(context, time(NULL) + time_offset, 0);
2176 ret = krb5_cc_resolve(context, cc ? cc :
2177 krb5_cc_default_name(context), &ccache);
2178 if (ret) {
2179 goto done;
2182 ret = krb5_cc_get_principal(context, ccache, &me);
2183 if (ret) {
2184 goto done;
2187 ret = smb_krb5_parse_name(context, server_s, &server);
2188 if (ret) {
2189 goto done;
2192 if (impersonate_princ_s) {
2193 ret = smb_krb5_parse_name(context, impersonate_princ_s,
2194 &impersonate_princ);
2195 if (ret) {
2196 goto done;
2200 ret = smb_krb5_get_credentials(context, ccache,
2201 me, server, impersonate_princ,
2202 &creds);
2203 if (ret) {
2204 goto done;
2207 ret = krb5_cc_store_cred(context, ccache, creds);
2208 if (ret) {
2209 goto done;
2212 if (creds_p) {
2213 *creds_p = creds;
2216 DEBUG(1,("smb_krb5_get_creds: got ticket for %s\n",
2217 server_s));
2219 if (impersonate_princ_s) {
2220 char *client = NULL;
2222 ret = smb_krb5_unparse_name(talloc_tos(), context, creds->client, &client);
2223 if (ret) {
2224 goto done;
2226 DEBUGADD(1,("smb_krb5_get_creds: using S4U2SELF impersonation as %s\n",
2227 client));
2228 TALLOC_FREE(client);
2231 done:
2232 if (!context) {
2233 return ret;
2236 if (creds && ret) {
2237 krb5_free_creds(context, creds);
2239 if (server) {
2240 krb5_free_principal(context, server);
2242 if (me) {
2243 krb5_free_principal(context, me);
2245 if (impersonate_princ) {
2246 krb5_free_principal(context, impersonate_princ);
2248 if (ccache) {
2249 krb5_cc_close(context, ccache);
2251 krb5_free_context(context);
2253 return ret;
2257 * smb_krb5_principal_get_realm
2259 * @brief Get realm of a principal
2261 * @param[in] context The krb5_context
2262 * @param[in] principal The principal
2263 * @return pointer to the realm
2267 char *smb_krb5_principal_get_realm(krb5_context context,
2268 krb5_principal principal)
2270 #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
2271 return krb5_principal_get_realm(context, principal);
2272 #elif defined(krb5_princ_realm) /* MIT */
2273 krb5_data *realm;
2274 realm = krb5_princ_realm(context, principal);
2275 return (char *)realm->data;
2276 #else
2277 return NULL;
2278 #endif
2281 #else /* HAVE_KRB5 */
2282 /* this saves a few linking headaches */
2283 int cli_krb5_get_ticket(TALLOC_CTX *mem_ctx,
2284 const char *principal, time_t time_offset,
2285 DATA_BLOB *ticket, DATA_BLOB *session_key_krb5,
2286 uint32_t extra_ap_opts,
2287 const char *ccname, time_t *tgs_expire,
2288 const char *impersonate_princ_s)
2290 DEBUG(0,("NO KERBEROS SUPPORT\n"));
2291 return 1;
2294 bool unwrap_pac(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, DATA_BLOB *unwrapped_pac_data)
2296 DEBUG(0,("NO KERBEROS SUPPORT\n"));
2297 return false;
2300 #endif /* HAVE_KRB5 */