s3-krb5 Fix Kerberos on FreeBSD with Samba4 DCs
[Samba.git] / source3 / libsmb / clikrb5.c
blobff93ddbe95161768f4399aea6bc007861577336a
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 "authdata.h"
27 #ifdef HAVE_KRB5
29 #define GSSAPI_CHECKSUM 0x8003 /* Checksum type value for Kerberos */
30 #define GSSAPI_BNDLENGTH 16 /* Bind Length (rfc-1964 pg.3) */
31 #define GSSAPI_CHECKSUM_SIZE (4+GSSAPI_BNDLENGTH+4) /* Length of bind length,
32 bind field, flags field. */
34 /* MIT krb5 1.7beta3 (in Ubuntu Karmic) is missing the prototype,
35 but still has the symbol */
36 #if !HAVE_DECL_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE
37 krb5_error_code krb5_auth_con_set_req_cksumtype(
38 krb5_context context,
39 krb5_auth_context auth_context,
40 krb5_cksumtype cksumtype);
41 #endif
43 /**************************************************************
44 Wrappers around kerberos string functions that convert from
45 utf8 -> unix charset and vica versa.
46 **************************************************************/
48 /**************************************************************
49 krb5_parse_name that takes a UNIX charset.
50 **************************************************************/
52 krb5_error_code smb_krb5_parse_name(krb5_context context,
53 const char *name, /* in unix charset */
54 krb5_principal *principal)
56 krb5_error_code ret;
57 char *utf8_name;
58 size_t converted_size;
60 if (!push_utf8_talloc(talloc_tos(), &utf8_name, name, &converted_size)) {
61 return ENOMEM;
64 ret = krb5_parse_name(context, utf8_name, principal);
65 TALLOC_FREE(utf8_name);
66 return ret;
69 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
70 /**************************************************************
71 krb5_parse_name_norealm that takes a UNIX charset.
72 **************************************************************/
74 static krb5_error_code smb_krb5_parse_name_norealm_conv(krb5_context context,
75 const char *name, /* in unix charset */
76 krb5_principal *principal)
78 krb5_error_code ret;
79 char *utf8_name;
80 size_t converted_size;
82 *principal = NULL;
83 if (!push_utf8_talloc(talloc_tos(), &utf8_name, name, &converted_size)) {
84 return ENOMEM;
87 ret = krb5_parse_name_norealm(context, utf8_name, principal);
88 TALLOC_FREE(utf8_name);
89 return ret;
91 #endif
93 /**************************************************************
94 krb5_parse_name that returns a UNIX charset name. Must
95 be freed with talloc_free() call.
96 **************************************************************/
98 krb5_error_code smb_krb5_unparse_name(TALLOC_CTX *mem_ctx,
99 krb5_context context,
100 krb5_const_principal principal,
101 char **unix_name)
103 krb5_error_code ret;
104 char *utf8_name;
105 size_t converted_size;
107 *unix_name = NULL;
108 ret = krb5_unparse_name(context, principal, &utf8_name);
109 if (ret) {
110 return ret;
113 if (!pull_utf8_talloc(mem_ctx, unix_name, utf8_name, &converted_size)) {
114 krb5_free_unparsed_name(context, utf8_name);
115 return ENOMEM;
117 krb5_free_unparsed_name(context, utf8_name);
118 return 0;
121 #ifndef HAVE_KRB5_SET_REAL_TIME
123 * This function is not in the Heimdal mainline.
125 krb5_error_code krb5_set_real_time(krb5_context context, int32_t seconds, int32_t microseconds)
127 krb5_error_code ret;
128 int32_t sec, usec;
130 ret = krb5_us_timeofday(context, &sec, &usec);
131 if (ret)
132 return ret;
134 context->kdc_sec_offset = seconds - sec;
135 context->kdc_usec_offset = microseconds - usec;
137 return 0;
139 #endif
141 #if !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
143 #if defined(HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES)
145 /* With MIT kerberos, we should use krb5_set_default_tgs_enctypes in preference
146 * to krb5_set_default_tgs_ktypes. See
147 * http://lists.samba.org/archive/samba-technical/2006-July/048271.html
149 * If the MIT libraries are not exporting internal symbols, we will end up in
150 * this branch, which is correct. Otherwise we will continue to use the
151 * internal symbol
153 krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
155 return krb5_set_default_tgs_enctypes(ctx, enc);
158 #elif defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES)
160 /* Heimdal */
161 krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
163 return krb5_set_default_in_tkt_etypes(ctx, enc);
166 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES */
168 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_KTYPES */
170 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
171 /* HEIMDAL */
172 bool setup_kaddr( krb5_address *pkaddr, struct sockaddr_storage *paddr)
174 memset(pkaddr, '\0', sizeof(krb5_address));
175 #if defined(HAVE_IPV6) && defined(KRB5_ADDRESS_INET6)
176 if (paddr->ss_family == AF_INET6) {
177 pkaddr->addr_type = KRB5_ADDRESS_INET6;
178 pkaddr->address.length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr);
179 pkaddr->address.data = (char *)&(((struct sockaddr_in6 *)paddr)->sin6_addr);
180 return true;
182 #endif
183 if (paddr->ss_family == AF_INET) {
184 pkaddr->addr_type = KRB5_ADDRESS_INET;
185 pkaddr->address.length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
186 pkaddr->address.data = (char *)&(((struct sockaddr_in *)paddr)->sin_addr);
187 return true;
189 return false;
191 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
192 /* MIT */
193 bool setup_kaddr( krb5_address *pkaddr, struct sockaddr_storage *paddr)
195 memset(pkaddr, '\0', sizeof(krb5_address));
196 #if defined(HAVE_IPV6) && defined(ADDRTYPE_INET6)
197 if (paddr->ss_family == AF_INET6) {
198 pkaddr->addrtype = ADDRTYPE_INET6;
199 pkaddr->length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr);
200 pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in6 *)paddr)->sin6_addr);
201 return true;
203 #endif
204 if (paddr->ss_family == AF_INET) {
205 pkaddr->addrtype = ADDRTYPE_INET;
206 pkaddr->length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
207 pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in *)paddr)->sin_addr);
208 return true;
210 return false;
212 #else
213 #error UNKNOWN_ADDRTYPE
214 #endif
216 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_USE_ENCTYPE) && defined(HAVE_KRB5_STRING_TO_KEY) && defined(HAVE_KRB5_ENCRYPT_BLOCK)
217 static int create_kerberos_key_from_string_direct(krb5_context context,
218 krb5_principal host_princ,
219 krb5_data *password,
220 krb5_keyblock *key,
221 krb5_enctype enctype)
223 int ret = 0;
224 krb5_data salt;
225 krb5_encrypt_block eblock;
227 ret = krb5_principal2salt(context, host_princ, &salt);
228 if (ret) {
229 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
230 return ret;
232 krb5_use_enctype(context, &eblock, enctype);
233 ret = krb5_string_to_key(context, &eblock, key, password, &salt);
234 SAFE_FREE(salt.data);
236 return ret;
238 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
239 static int create_kerberos_key_from_string_direct(krb5_context context,
240 krb5_principal host_princ,
241 krb5_data *password,
242 krb5_keyblock *key,
243 krb5_enctype enctype)
245 int ret;
246 krb5_salt salt;
248 ret = krb5_get_pw_salt(context, host_princ, &salt);
249 if (ret) {
250 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret)));
251 return ret;
254 ret = krb5_string_to_key_salt(context, enctype, (const char *)password->data, salt, key);
255 krb5_free_salt(context, salt);
257 return ret;
259 #else
260 #error UNKNOWN_CREATE_KEY_FUNCTIONS
261 #endif
263 int create_kerberos_key_from_string(krb5_context context,
264 krb5_principal host_princ,
265 krb5_data *password,
266 krb5_keyblock *key,
267 krb5_enctype enctype,
268 bool no_salt)
270 krb5_principal salt_princ = NULL;
271 int ret;
273 * Check if we've determined that the KDC is salting keys for this
274 * principal/enctype in a non-obvious way. If it is, try to match
275 * its behavior.
277 if (no_salt) {
278 KRB5_KEY_DATA(key) = (KRB5_KEY_DATA_CAST *)SMB_MALLOC(password->length);
279 if (!KRB5_KEY_DATA(key)) {
280 return ENOMEM;
282 memcpy(KRB5_KEY_DATA(key), password->data, password->length);
283 KRB5_KEY_LENGTH(key) = password->length;
284 KRB5_KEY_TYPE(key) = enctype;
285 return 0;
287 salt_princ = kerberos_fetch_salt_princ_for_host_princ(context, host_princ, enctype);
288 ret = create_kerberos_key_from_string_direct(context, salt_princ ? salt_princ : host_princ, password, key, enctype);
289 if (salt_princ) {
290 krb5_free_principal(context, salt_princ);
292 return ret;
295 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
296 krb5_error_code get_kerberos_allowed_etypes(krb5_context context,
297 krb5_enctype **enctypes)
299 return krb5_get_permitted_enctypes(context, enctypes);
301 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
302 krb5_error_code get_kerberos_allowed_etypes(krb5_context context,
303 krb5_enctype **enctypes)
305 return krb5_get_default_in_tkt_etypes(context, enctypes);
307 #else
308 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
309 #endif
311 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
312 krb5_error_code krb5_auth_con_setuseruserkey(krb5_context context,
313 krb5_auth_context auth_context,
314 krb5_keyblock *keyblock)
316 return krb5_auth_con_setkey(context, auth_context, keyblock);
318 #endif
320 bool unwrap_edata_ntstatus(TALLOC_CTX *mem_ctx,
321 DATA_BLOB *edata,
322 DATA_BLOB *edata_out)
324 DATA_BLOB edata_contents;
325 ASN1_DATA *data;
326 int edata_type;
328 if (!edata->length) {
329 return False;
332 data = asn1_init(mem_ctx);
333 if (data == NULL) {
334 return false;
337 asn1_load(data, *edata);
338 asn1_start_tag(data, ASN1_SEQUENCE(0));
339 asn1_start_tag(data, ASN1_CONTEXT(1));
340 asn1_read_Integer(data, &edata_type);
342 if (edata_type != KRB5_PADATA_PW_SALT) {
343 DEBUG(0,("edata is not of required type %d but of type %d\n",
344 KRB5_PADATA_PW_SALT, edata_type));
345 asn1_free(data);
346 return False;
349 asn1_start_tag(data, ASN1_CONTEXT(2));
350 asn1_read_OctetString(data, talloc_autofree_context(), &edata_contents);
351 asn1_end_tag(data);
352 asn1_end_tag(data);
353 asn1_end_tag(data);
354 asn1_free(data);
356 *edata_out = data_blob_talloc(mem_ctx, edata_contents.data, edata_contents.length);
358 data_blob_free(&edata_contents);
360 return True;
364 bool unwrap_pac(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, DATA_BLOB *unwrapped_pac_data)
366 DATA_BLOB pac_contents;
367 ASN1_DATA *data;
368 int data_type;
370 if (!auth_data->length) {
371 return False;
374 data = asn1_init(mem_ctx);
375 if (data == NULL) {
376 return false;
379 asn1_load(data, *auth_data);
380 asn1_start_tag(data, ASN1_SEQUENCE(0));
381 asn1_start_tag(data, ASN1_SEQUENCE(0));
382 asn1_start_tag(data, ASN1_CONTEXT(0));
383 asn1_read_Integer(data, &data_type);
385 if (data_type != KRB5_AUTHDATA_WIN2K_PAC ) {
386 DEBUG(10,("authorization data is not a Windows PAC (type: %d)\n", data_type));
387 asn1_free(data);
388 return False;
391 asn1_end_tag(data);
392 asn1_start_tag(data, ASN1_CONTEXT(1));
393 asn1_read_OctetString(data, talloc_autofree_context(), &pac_contents);
394 asn1_end_tag(data);
395 asn1_end_tag(data);
396 asn1_end_tag(data);
397 asn1_free(data);
399 *unwrapped_pac_data = data_blob_talloc(mem_ctx, pac_contents.data, pac_contents.length);
401 data_blob_free(&pac_contents);
403 return True;
406 bool get_auth_data_from_tkt(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, krb5_ticket *tkt)
408 DATA_BLOB auth_data_wrapped;
409 bool got_auth_data_pac = False;
410 int i;
412 #if defined(HAVE_KRB5_TKT_ENC_PART2)
413 if (tkt->enc_part2 && tkt->enc_part2->authorization_data &&
414 tkt->enc_part2->authorization_data[0] &&
415 tkt->enc_part2->authorization_data[0]->length)
417 for (i = 0; tkt->enc_part2->authorization_data[i] != NULL; i++) {
419 if (tkt->enc_part2->authorization_data[i]->ad_type !=
420 KRB5_AUTHDATA_IF_RELEVANT) {
421 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
422 tkt->enc_part2->authorization_data[i]->ad_type));
423 continue;
426 auth_data_wrapped = data_blob(tkt->enc_part2->authorization_data[i]->contents,
427 tkt->enc_part2->authorization_data[i]->length);
429 /* check if it is a PAC */
430 got_auth_data_pac = unwrap_pac(mem_ctx, &auth_data_wrapped, auth_data);
431 data_blob_free(&auth_data_wrapped);
433 if (got_auth_data_pac) {
434 return true;
438 return got_auth_data_pac;
441 #else
442 if (tkt->ticket.authorization_data &&
443 tkt->ticket.authorization_data->len)
445 for (i = 0; i < tkt->ticket.authorization_data->len; i++) {
447 if (tkt->ticket.authorization_data->val[i].ad_type !=
448 KRB5_AUTHDATA_IF_RELEVANT) {
449 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
450 tkt->ticket.authorization_data->val[i].ad_type));
451 continue;
454 auth_data_wrapped = data_blob(tkt->ticket.authorization_data->val[i].ad_data.data,
455 tkt->ticket.authorization_data->val[i].ad_data.length);
457 /* check if it is a PAC */
458 got_auth_data_pac = unwrap_pac(mem_ctx, &auth_data_wrapped, auth_data);
459 data_blob_free(&auth_data_wrapped);
461 if (got_auth_data_pac) {
462 return true;
466 return got_auth_data_pac;
468 #endif
469 return False;
472 krb5_const_principal get_principal_from_tkt(krb5_ticket *tkt)
474 #if defined(HAVE_KRB5_TKT_ENC_PART2)
475 return tkt->enc_part2->client;
476 #else
477 return tkt->client;
478 #endif
481 #if !defined(HAVE_KRB5_LOCATE_KDC)
483 /* krb5_locate_kdc is an internal MIT symbol. MIT are not yet willing to commit
484 * to a public interface for this functionality, so we have to be able to live
485 * without it if the MIT libraries are hiding their internal symbols.
488 #if defined(KRB5_KRBHST_INIT)
489 /* Heimdal */
490 krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm, struct sockaddr **addr_pp, int *naddrs, int get_masters)
492 krb5_krbhst_handle hnd;
493 krb5_krbhst_info *hinfo;
494 krb5_error_code rc;
495 int num_kdcs, i;
496 struct sockaddr *sa;
497 struct addrinfo *ai;
499 *addr_pp = NULL;
500 *naddrs = 0;
502 rc = krb5_krbhst_init(ctx, realm->data, KRB5_KRBHST_KDC, &hnd);
503 if (rc) {
504 DEBUG(0, ("smb_krb5_locate_kdc: krb5_krbhst_init failed (%s)\n", error_message(rc)));
505 return rc;
508 for ( num_kdcs = 0; (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); num_kdcs++)
511 krb5_krbhst_reset(ctx, hnd);
513 if (!num_kdcs) {
514 DEBUG(0, ("smb_krb5_locate_kdc: zero kdcs found !\n"));
515 krb5_krbhst_free(ctx, hnd);
516 return -1;
519 sa = SMB_MALLOC_ARRAY( struct sockaddr, num_kdcs );
520 if (!sa) {
521 DEBUG(0, ("smb_krb5_locate_kdc: malloc failed\n"));
522 krb5_krbhst_free(ctx, hnd);
523 naddrs = 0;
524 return -1;
527 memset(sa, '\0', sizeof(struct sockaddr) * num_kdcs );
529 for (i = 0; i < num_kdcs && (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); i++) {
531 #if defined(HAVE_KRB5_KRBHST_GET_ADDRINFO)
532 rc = krb5_krbhst_get_addrinfo(ctx, hinfo, &ai);
533 if (rc) {
534 DEBUG(0,("krb5_krbhst_get_addrinfo failed: %s\n", error_message(rc)));
535 continue;
537 #endif
538 if (hinfo->ai && hinfo->ai->ai_family == AF_INET)
539 memcpy(&sa[i], hinfo->ai->ai_addr, sizeof(struct sockaddr));
542 krb5_krbhst_free(ctx, hnd);
544 *naddrs = num_kdcs;
545 *addr_pp = sa;
546 return 0;
549 #else /* ! defined(KRB5_KRBHST_INIT) */
551 krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm,
552 struct sockaddr **addr_pp, int *naddrs, int get_masters)
554 DEBUG(0, ("unable to explicitly locate the KDC on this platform\n"));
555 return KRB5_KDC_UNREACH;
558 #endif /* KRB5_KRBHST_INIT */
560 #else /* ! HAVE_KRB5_LOCATE_KDC */
562 krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm,
563 struct sockaddr **addr_pp, int *naddrs, int get_masters)
565 return krb5_locate_kdc(ctx, realm, addr_pp, naddrs, get_masters);
568 #endif /* HAVE_KRB5_LOCATE_KDC */
570 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
571 void krb5_free_unparsed_name(krb5_context context, char *val)
573 SAFE_FREE(val);
575 #endif
577 void kerberos_free_data_contents(krb5_context context, krb5_data *pdata)
579 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
580 if (pdata->data) {
581 krb5_free_data_contents(context, pdata);
583 #else
584 SAFE_FREE(pdata->data);
585 #endif
588 void kerberos_set_creds_enctype(krb5_creds *pcreds, int enctype)
590 #if defined(HAVE_KRB5_KEYBLOCK_IN_CREDS)
591 KRB5_KEY_TYPE((&pcreds->keyblock)) = enctype;
592 #elif defined(HAVE_KRB5_SESSION_IN_CREDS)
593 KRB5_KEY_TYPE((&pcreds->session)) = enctype;
594 #else
595 #error UNKNOWN_KEYBLOCK_MEMBER_IN_KRB5_CREDS_STRUCT
596 #endif
599 bool kerberos_compatible_enctypes(krb5_context context,
600 krb5_enctype enctype1,
601 krb5_enctype enctype2)
603 #if defined(HAVE_KRB5_C_ENCTYPE_COMPARE)
604 krb5_boolean similar = 0;
606 krb5_c_enctype_compare(context, enctype1, enctype2, &similar);
607 return similar ? True : False;
608 #elif defined(HAVE_KRB5_ENCTYPES_COMPATIBLE_KEYS)
609 return krb5_enctypes_compatible_keys(context, enctype1, enctype2) ? True : False;
610 #endif
613 static bool ads_cleanup_expired_creds(krb5_context context,
614 krb5_ccache ccache,
615 krb5_creds *credsp)
617 krb5_error_code retval;
618 const char *cc_type = krb5_cc_get_type(context, ccache);
620 DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
621 cc_type, krb5_cc_get_name(context, ccache),
622 http_timestring(talloc_tos(), credsp->times.endtime)));
624 /* we will probably need new tickets if the current ones
625 will expire within 10 seconds.
627 if (credsp->times.endtime >= (time(NULL) + 10))
628 return False;
630 /* heimdal won't remove creds from a file ccache, and
631 perhaps we shouldn't anyway, since internally we
632 use memory ccaches, and a FILE one probably means that
633 we're using creds obtained outside of our exectuable
635 if (strequal(cc_type, "FILE")) {
636 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type));
637 return False;
640 retval = krb5_cc_remove_cred(context, ccache, 0, credsp);
641 if (retval) {
642 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
643 error_message(retval)));
644 /* If we have an error in this, we want to display it,
645 but continue as though we deleted it */
647 return True;
650 /* Allocate and setup the auth context into the state we need. */
652 static krb5_error_code setup_auth_context(krb5_context context,
653 krb5_auth_context *auth_context)
655 krb5_error_code retval;
657 retval = krb5_auth_con_init(context, auth_context );
658 if (retval) {
659 DEBUG(1,("krb5_auth_con_init failed (%s)\n",
660 error_message(retval)));
661 return retval;
664 /* Ensure this is an addressless ticket. */
665 retval = krb5_auth_con_setaddrs(context, *auth_context, NULL, NULL);
666 if (retval) {
667 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n",
668 error_message(retval)));
671 return retval;
674 static krb5_error_code create_gss_checksum(krb5_data *in_data, /* [inout] */
675 uint32_t gss_flags)
677 unsigned int orig_length = in_data->length;
678 unsigned int base_cksum_size = GSSAPI_CHECKSUM_SIZE;
679 char *gss_cksum = NULL;
681 if (orig_length) {
682 /* Extra length field for delgated ticket. */
683 base_cksum_size += 4;
686 if ((unsigned int)base_cksum_size + orig_length <
687 (unsigned int)base_cksum_size) {
688 return EINVAL;
691 gss_cksum = (char *)SMB_MALLOC(base_cksum_size + orig_length);
692 if (gss_cksum == NULL) {
693 return ENOMEM;
696 memset(gss_cksum, '\0', base_cksum_size + orig_length);
697 SIVAL(gss_cksum, 0, GSSAPI_BNDLENGTH);
699 /* Precalculated MD5sum of NULL channel bindings (20 bytes) */
700 /* Channel bindings are: (all ints encoded as little endian)
702 [4 bytes] initiator_addrtype (255 for null bindings)
703 [4 bytes] initiator_address length
704 [n bytes] .. initiator_address data - not present
705 in null bindings.
706 [4 bytes] acceptor_addrtype (255 for null bindings)
707 [4 bytes] acceptor_address length
708 [n bytes] .. acceptor_address data - not present
709 in null bindings.
710 [4 bytes] application_data length
711 [n bytes] .. application_ data - not present
712 in null bindings.
713 MD5 of this is ""\x14\x8f\x0c\xf7\xb1u\xdey*J\x9a%\xdfV\xc5\x18"
716 memcpy(&gss_cksum[4],
717 "\x14\x8f\x0c\xf7\xb1u\xdey*J\x9a%\xdfV\xc5\x18",
718 GSSAPI_BNDLENGTH);
720 SIVAL(gss_cksum, 20, gss_flags);
722 if (orig_length) {
723 SSVAL(gss_cksum, 24, 1); /* The Delegation Option identifier */
724 SSVAL(gss_cksum, 26, orig_length);
725 /* Copy the kerberos KRB_CRED data */
726 memcpy(gss_cksum + 28, in_data->data, orig_length);
727 free(in_data->data);
728 in_data->data = NULL;
729 in_data->length = 0;
731 in_data->data = gss_cksum;
732 in_data->length = base_cksum_size + orig_length;
733 return 0;
737 we can't use krb5_mk_req because w2k wants the service to be in a particular format
739 static krb5_error_code ads_krb5_mk_req(krb5_context context,
740 krb5_auth_context *auth_context,
741 const krb5_flags ap_req_options,
742 const char *principal,
743 krb5_ccache ccache,
744 krb5_data *outbuf,
745 time_t *expire_time,
746 const char *impersonate_princ_s)
748 krb5_error_code retval;
749 krb5_principal server;
750 krb5_principal impersonate_princ = NULL;
751 krb5_creds * credsp;
752 krb5_creds creds;
753 krb5_data in_data;
754 bool creds_ready = False;
755 int i = 0, maxtries = 3;
756 uint32_t gss_flags = 0;
758 ZERO_STRUCT(in_data);
760 retval = smb_krb5_parse_name(context, principal, &server);
761 if (retval) {
762 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal));
763 return retval;
766 if (impersonate_princ_s) {
767 retval = smb_krb5_parse_name(context, impersonate_princ_s,
768 &impersonate_princ);
769 if (retval) {
770 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", impersonate_princ_s));
771 goto cleanup_princ;
775 /* obtain ticket & session key */
776 ZERO_STRUCT(creds);
777 if ((retval = krb5_copy_principal(context, server, &creds.server))) {
778 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n",
779 error_message(retval)));
780 goto cleanup_princ;
783 if ((retval = krb5_cc_get_principal(context, ccache, &creds.client))) {
784 /* This can commonly fail on smbd startup with no ticket in the cache.
785 * Report at higher level than 1. */
786 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
787 error_message(retval)));
788 goto cleanup_creds;
791 while (!creds_ready && (i < maxtries)) {
793 if ((retval = smb_krb5_get_credentials(context, ccache,
794 creds.client,
795 creds.server,
796 impersonate_princ,
797 &credsp))) {
798 DEBUG(1,("ads_krb5_mk_req: smb_krb5_get_credentials failed for %s (%s)\n",
799 principal, error_message(retval)));
800 goto cleanup_creds;
803 /* cope with ticket being in the future due to clock skew */
804 if ((unsigned)credsp->times.starttime > time(NULL)) {
805 time_t t = time(NULL);
806 int time_offset =(int)((unsigned)credsp->times.starttime-t);
807 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset));
808 krb5_set_real_time(context, t + time_offset + 1, 0);
811 if (!ads_cleanup_expired_creds(context, ccache, credsp)) {
812 creds_ready = True;
815 i++;
818 DEBUG(10,("ads_krb5_mk_req: Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
819 principal, krb5_cc_get_type(context, ccache), krb5_cc_get_name(context, ccache),
820 http_timestring(talloc_tos(), (unsigned)credsp->times.endtime),
821 (unsigned)credsp->times.endtime));
823 if (expire_time) {
824 *expire_time = (time_t)credsp->times.endtime;
827 /* Allocate the auth_context. */
828 retval = setup_auth_context(context, auth_context);
829 if (retval) {
830 DEBUG(1,("setup_auth_context failed (%s)\n",
831 error_message(retval)));
832 goto cleanup_creds;
835 #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)
836 if( credsp->ticket_flags & TKT_FLG_OK_AS_DELEGATE ) {
837 /* Fetch a forwarded TGT from the KDC so that we can hand off a 2nd ticket
838 as part of the kerberos exchange. */
840 DEBUG( 3, ("ads_krb5_mk_req: server marked as OK to delegate to, building forwardable TGT\n") );
842 retval = krb5_auth_con_setuseruserkey(context,
843 *auth_context,
844 &credsp->keyblock );
845 if (retval) {
846 DEBUG(1,("krb5_auth_con_setuseruserkey failed (%s)\n",
847 error_message(retval)));
848 goto cleanup_creds;
851 /* Must use a subkey for forwarded tickets. */
852 retval = krb5_auth_con_setflags(context,
853 *auth_context,
854 KRB5_AUTH_CONTEXT_USE_SUBKEY);
855 if (retval) {
856 DEBUG(1,("krb5_auth_con_setflags failed (%s)\n",
857 error_message(retval)));
858 goto cleanup_creds;
861 retval = krb5_fwd_tgt_creds(context,/* Krb5 context [in] */
862 *auth_context, /* Authentication context [in] */
863 CONST_DISCARD(char *, KRB5_TGS_NAME), /* Ticket service name ("krbtgt") [in] */
864 credsp->client, /* Client principal for the tgt [in] */
865 credsp->server, /* Server principal for the tgt [in] */
866 ccache, /* Credential cache to use for storage [in] */
867 1, /* Turn on for "Forwardable ticket" [in] */
868 &in_data ); /* Resulting response [out] */
870 if (retval) {
871 DEBUG( 3, ("krb5_fwd_tgt_creds failed (%s)\n",
872 error_message( retval ) ) );
875 * This is not fatal. Delete the *auth_context and continue
876 * with krb5_mk_req_extended to get a non-forwardable ticket.
879 if (in_data.data) {
880 free( in_data.data );
881 in_data.data = NULL;
882 in_data.length = 0;
884 krb5_auth_con_free(context, *auth_context);
885 *auth_context = NULL;
886 retval = setup_auth_context(context, auth_context);
887 if (retval) {
888 DEBUG(1,("setup_auth_context failed (%s)\n",
889 error_message(retval)));
890 goto cleanup_creds;
892 } else {
893 /* We got a delegated ticket. */
894 gss_flags |= GSS_C_DELEG_FLAG;
898 /* Frees and reallocates in_data into a GSS checksum blob. */
899 retval = create_gss_checksum(&in_data, gss_flags);
900 if (retval) {
901 goto cleanup_data;
904 /* We always want GSS-checksum types. */
905 retval = krb5_auth_con_set_req_cksumtype(context, *auth_context, GSSAPI_CHECKSUM );
906 if (retval) {
907 DEBUG(1,("krb5_auth_con_set_req_cksumtype failed (%s)\n",
908 error_message(retval)));
909 goto cleanup_data;
911 #endif
913 retval = krb5_mk_req_extended(context, auth_context, ap_req_options,
914 &in_data, credsp, outbuf);
915 if (retval) {
916 DEBUG(1,("ads_krb5_mk_req: krb5_mk_req_extended failed (%s)\n",
917 error_message(retval)));
920 cleanup_data:
921 if (in_data.data) {
922 free( in_data.data );
923 in_data.length = 0;
926 krb5_free_creds(context, credsp);
928 cleanup_creds:
929 krb5_free_cred_contents(context, &creds);
931 cleanup_princ:
932 krb5_free_principal(context, server);
933 if (impersonate_princ) {
934 krb5_free_principal(context, impersonate_princ);
937 return retval;
941 get a kerberos5 ticket for the given service
943 int cli_krb5_get_ticket(const char *principal, time_t time_offset,
944 DATA_BLOB *ticket, DATA_BLOB *session_key_krb5,
945 uint32 extra_ap_opts, const char *ccname,
946 time_t *tgs_expire,
947 const char *impersonate_princ_s)
950 krb5_error_code retval;
951 krb5_data packet;
952 krb5_context context = NULL;
953 krb5_ccache ccdef = NULL;
954 krb5_auth_context auth_context = NULL;
955 krb5_enctype enc_types[] = {
956 #ifdef ENCTYPE_ARCFOUR_HMAC
957 ENCTYPE_ARCFOUR_HMAC,
958 #endif
959 ENCTYPE_DES_CBC_MD5,
960 ENCTYPE_DES_CBC_CRC,
961 ENCTYPE_NULL};
963 initialize_krb5_error_table();
964 retval = krb5_init_context(&context);
965 if (retval) {
966 DEBUG(1,("cli_krb5_get_ticket: krb5_init_context failed (%s)\n",
967 error_message(retval)));
968 goto failed;
971 if (time_offset != 0) {
972 krb5_set_real_time(context, time(NULL) + time_offset, 0);
975 if ((retval = krb5_cc_resolve(context, ccname ?
976 ccname : krb5_cc_default_name(context), &ccdef))) {
977 DEBUG(1,("cli_krb5_get_ticket: krb5_cc_default failed (%s)\n",
978 error_message(retval)));
979 goto failed;
982 if ((retval = krb5_set_default_tgs_ktypes(context, enc_types))) {
983 DEBUG(1,("cli_krb5_get_ticket: krb5_set_default_tgs_ktypes failed (%s)\n",
984 error_message(retval)));
985 goto failed;
988 if ((retval = ads_krb5_mk_req(context,
989 &auth_context,
990 AP_OPTS_USE_SUBKEY | (krb5_flags)extra_ap_opts,
991 principal,
992 ccdef, &packet,
993 tgs_expire,
994 impersonate_princ_s))) {
995 goto failed;
998 get_krb5_smb_session_key(context, auth_context, session_key_krb5, False);
1000 *ticket = data_blob(packet.data, packet.length);
1002 kerberos_free_data_contents(context, &packet);
1004 failed:
1006 if ( context ) {
1007 if (ccdef)
1008 krb5_cc_close(context, ccdef);
1009 if (auth_context)
1010 krb5_auth_con_free(context, auth_context);
1011 krb5_free_context(context);
1014 return retval;
1017 bool get_krb5_smb_session_key(krb5_context context, krb5_auth_context auth_context, DATA_BLOB *session_key, bool remote)
1019 krb5_keyblock *skey = NULL;
1020 krb5_error_code err = 0;
1021 bool ret = false;
1023 if (remote) {
1024 err = krb5_auth_con_getremotesubkey(context, auth_context, &skey);
1025 } else {
1026 err = krb5_auth_con_getlocalsubkey(context, auth_context, &skey);
1029 if (err || skey == NULL) {
1030 DEBUG(10, ("KRB5 error getting session key %d\n", err));
1031 goto done;
1034 DEBUG(10, ("Got KRB5 session key of length %d\n", (int)KRB5_KEY_LENGTH(skey)));
1035 *session_key = data_blob(KRB5_KEY_DATA(skey), KRB5_KEY_LENGTH(skey));
1036 dump_data_pw("KRB5 Session Key:\n", session_key->data, session_key->length);
1038 ret = true;
1040 done:
1041 if (skey) {
1042 krb5_free_keyblock(context, skey);
1045 return ret;
1049 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
1050 const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i );
1052 const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i )
1054 static krb5_data kdata;
1056 kdata.data = (char *)krb5_principal_get_comp_string(context, principal, i);
1057 kdata.length = strlen((const char *)kdata.data);
1058 return &kdata;
1060 #endif
1062 krb5_error_code smb_krb5_kt_free_entry(krb5_context context, krb5_keytab_entry *kt_entry)
1064 /* Try krb5_free_keytab_entry_contents first, since
1065 * MIT Kerberos >= 1.7 has both krb5_free_keytab_entry_contents and
1066 * krb5_kt_free_entry but only has a prototype for the first, while the
1067 * second is considered private.
1069 #if defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
1070 return krb5_free_keytab_entry_contents(context, kt_entry);
1071 #elif defined(HAVE_KRB5_KT_FREE_ENTRY)
1072 return krb5_kt_free_entry(context, kt_entry);
1073 #else
1074 #error UNKNOWN_KT_FREE_FUNCTION
1075 #endif
1078 void smb_krb5_checksum_from_pac_sig(krb5_checksum *cksum,
1079 struct PAC_SIGNATURE_DATA *sig)
1081 #ifdef HAVE_CHECKSUM_IN_KRB5_CHECKSUM
1082 cksum->cksumtype = (krb5_cksumtype)sig->type;
1083 cksum->checksum.length = sig->signature.length;
1084 cksum->checksum.data = sig->signature.data;
1085 #else
1086 cksum->checksum_type = (krb5_cksumtype)sig->type;
1087 cksum->length = sig->signature.length;
1088 cksum->contents = sig->signature.data;
1089 #endif
1092 krb5_error_code smb_krb5_verify_checksum(krb5_context context,
1093 const krb5_keyblock *keyblock,
1094 krb5_keyusage usage,
1095 krb5_checksum *cksum,
1096 uint8 *data,
1097 size_t length)
1099 krb5_error_code ret;
1101 /* verify the checksum */
1103 /* welcome to the wonderful world of samba's kerberos abstraction layer:
1105 * function heimdal 0.6.1rc3 heimdal 0.7 MIT krb 1.4.2
1106 * -----------------------------------------------------------------------------
1107 * krb5_c_verify_checksum - works works
1108 * krb5_verify_checksum works (6 args) works (6 args) broken (7 args)
1111 #if defined(HAVE_KRB5_C_VERIFY_CHECKSUM)
1113 krb5_boolean checksum_valid = False;
1114 krb5_data input;
1116 input.data = (char *)data;
1117 input.length = length;
1119 ret = krb5_c_verify_checksum(context,
1120 keyblock,
1121 usage,
1122 &input,
1123 cksum,
1124 &checksum_valid);
1125 if (ret) {
1126 DEBUG(3,("smb_krb5_verify_checksum: krb5_c_verify_checksum() failed: %s\n",
1127 error_message(ret)));
1128 return ret;
1131 if (!checksum_valid)
1132 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1135 #elif KRB5_VERIFY_CHECKSUM_ARGS == 6 && defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CRYPTO) && defined(HAVE_KRB5_CRYPTO_DESTROY)
1137 /* Warning: MIT's krb5_verify_checksum cannot be used as it will use a key
1138 * without enctype and it ignores any key_usage types - Guenther */
1142 krb5_crypto crypto;
1143 ret = krb5_crypto_init(context,
1144 keyblock,
1146 &crypto);
1147 if (ret) {
1148 DEBUG(0,("smb_krb5_verify_checksum: krb5_crypto_init() failed: %s\n",
1149 error_message(ret)));
1150 return ret;
1153 ret = krb5_verify_checksum(context,
1154 crypto,
1155 usage,
1156 data,
1157 length,
1158 cksum);
1160 krb5_crypto_destroy(context, crypto);
1163 #else
1164 #error UNKNOWN_KRB5_VERIFY_CHECKSUM_FUNCTION
1165 #endif
1167 return ret;
1170 time_t get_authtime_from_tkt(krb5_ticket *tkt)
1172 #if defined(HAVE_KRB5_TKT_ENC_PART2)
1173 return tkt->enc_part2->times.authtime;
1174 #else
1175 return tkt->ticket.authtime;
1176 #endif
1179 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
1180 static int get_kvno_from_ap_req(krb5_ap_req *ap_req)
1182 #ifdef HAVE_TICKET_POINTER_IN_KRB5_AP_REQ /* MIT */
1183 if (ap_req->ticket->enc_part.kvno)
1184 return ap_req->ticket->enc_part.kvno;
1185 #else /* Heimdal */
1186 if (ap_req->ticket.enc_part.kvno)
1187 return *ap_req->ticket.enc_part.kvno;
1188 #endif
1189 return 0;
1192 static krb5_enctype get_enctype_from_ap_req(krb5_ap_req *ap_req)
1194 #ifdef HAVE_ETYPE_IN_ENCRYPTEDDATA /* Heimdal */
1195 return ap_req->ticket.enc_part.etype;
1196 #else /* MIT */
1197 return ap_req->ticket->enc_part.enctype;
1198 #endif
1200 #endif /* HAVE_KRB5_DECODE_AP_REQ */
1202 static krb5_error_code
1203 get_key_from_keytab(krb5_context context,
1204 krb5_const_principal server,
1205 krb5_enctype enctype,
1206 krb5_kvno kvno,
1207 krb5_keyblock **out_key)
1209 krb5_keytab_entry entry;
1210 krb5_error_code ret;
1211 krb5_keytab keytab;
1212 char *name = NULL;
1213 krb5_keyblock *keyp;
1215 /* We have to open a new keytab handle here, as MIT does
1216 an implicit open/getnext/close on krb5_kt_get_entry. We
1217 may be in the middle of a keytab enumeration when this is
1218 called. JRA. */
1220 ret = smb_krb5_open_keytab(context, NULL, False, &keytab);
1221 if (ret) {
1222 DEBUG(1,("get_key_from_keytab: smb_krb5_open_keytab failed (%s)\n", error_message(ret)));
1223 return ret;
1226 if ( DEBUGLEVEL >= 10 ) {
1227 if (smb_krb5_unparse_name(talloc_tos(), context, server, &name) == 0) {
1228 DEBUG(10,("get_key_from_keytab: will look for kvno %d, enctype %d and name: %s\n",
1229 kvno, enctype, name));
1230 TALLOC_FREE(name);
1234 ret = krb5_kt_get_entry(context,
1235 keytab,
1236 server,
1237 kvno,
1238 enctype,
1239 &entry);
1241 if (ret) {
1242 DEBUG(0,("get_key_from_keytab: failed to retrieve key: %s\n", error_message(ret)));
1243 goto out;
1246 keyp = KRB5_KT_KEY(&entry);
1248 ret = krb5_copy_keyblock(context, keyp, out_key);
1249 if (ret) {
1250 DEBUG(0,("get_key_from_keytab: failed to copy key: %s\n", error_message(ret)));
1251 goto out;
1254 smb_krb5_kt_free_entry(context, &entry);
1256 out:
1257 krb5_kt_close(context, keytab);
1258 return ret;
1261 /* Prototypes */
1263 krb5_error_code smb_krb5_get_keyinfo_from_ap_req(krb5_context context,
1264 const krb5_data *inbuf,
1265 krb5_kvno *kvno,
1266 krb5_enctype *enctype)
1268 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
1270 krb5_error_code ret;
1271 krb5_ap_req ap_req;
1273 ret = krb5_decode_ap_req(context, inbuf, &ap_req);
1274 if (ret)
1275 return ret;
1277 *kvno = get_kvno_from_ap_req(&ap_req);
1278 *enctype = get_enctype_from_ap_req(&ap_req);
1280 free_AP_REQ(&ap_req);
1281 return 0;
1283 #endif
1285 /* Possibly not an appropriate error code. */
1286 return KRB5KDC_ERR_BADOPTION;
1289 krb5_error_code krb5_rd_req_return_keyblock_from_keytab(krb5_context context,
1290 krb5_auth_context *auth_context,
1291 const krb5_data *inbuf,
1292 krb5_const_principal server,
1293 krb5_keytab keytab,
1294 krb5_flags *ap_req_options,
1295 krb5_ticket **ticket,
1296 krb5_keyblock **keyblock)
1298 krb5_error_code ret;
1299 krb5_kvno kvno;
1300 krb5_enctype enctype;
1301 krb5_keyblock *local_keyblock;
1303 ret = krb5_rd_req(context,
1304 auth_context,
1305 inbuf,
1306 server,
1307 keytab,
1308 ap_req_options,
1309 ticket);
1310 if (ret) {
1311 return ret;
1314 #ifdef KRB5_TICKET_HAS_KEYINFO
1315 enctype = (*ticket)->enc_part.enctype;
1316 kvno = (*ticket)->enc_part.kvno;
1317 #else
1318 ret = smb_krb5_get_keyinfo_from_ap_req(context, inbuf, &kvno, &enctype);
1319 if (ret) {
1320 return ret;
1322 #endif
1324 ret = get_key_from_keytab(context,
1325 server,
1326 enctype,
1327 kvno,
1328 &local_keyblock);
1329 if (ret) {
1330 DEBUG(0,("krb5_rd_req_return_keyblock_from_keytab: failed to call get_key_from_keytab\n"));
1331 goto out;
1334 out:
1335 if (ret && local_keyblock != NULL) {
1336 krb5_free_keyblock(context, local_keyblock);
1337 } else {
1338 *keyblock = local_keyblock;
1341 return ret;
1344 krb5_error_code smb_krb5_parse_name_norealm(krb5_context context,
1345 const char *name,
1346 krb5_principal *principal)
1348 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
1349 return smb_krb5_parse_name_norealm_conv(context, name, principal);
1350 #endif
1352 /* we are cheating here because parse_name will in fact set the realm.
1353 * We don't care as the only caller of smb_krb5_parse_name_norealm
1354 * ignores the realm anyway when calling
1355 * smb_krb5_principal_compare_any_realm later - Guenther */
1357 return smb_krb5_parse_name(context, name, principal);
1360 bool smb_krb5_principal_compare_any_realm(krb5_context context,
1361 krb5_const_principal princ1,
1362 krb5_const_principal princ2)
1364 #ifdef HAVE_KRB5_PRINCIPAL_COMPARE_ANY_REALM
1366 return krb5_principal_compare_any_realm(context, princ1, princ2);
1368 /* krb5_princ_size is a macro in MIT */
1369 #elif defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
1371 int i, len1, len2;
1372 const krb5_data *p1, *p2;
1374 len1 = krb5_princ_size(context, princ1);
1375 len2 = krb5_princ_size(context, princ2);
1377 if (len1 != len2)
1378 return False;
1380 for (i = 0; i < len1; i++) {
1382 p1 = krb5_princ_component(context, CONST_DISCARD(krb5_principal, princ1), i);
1383 p2 = krb5_princ_component(context, CONST_DISCARD(krb5_principal, princ2), i);
1385 if (p1->length != p2->length || memcmp(p1->data, p2->data, p1->length))
1386 return False;
1389 return True;
1390 #else
1391 #error NO_SUITABLE_PRINCIPAL_COMPARE_FUNCTION
1392 #endif
1395 krb5_error_code smb_krb5_renew_ticket(const char *ccache_string, /* FILE:/tmp/krb5cc_0 */
1396 const char *client_string, /* gd@BER.SUSE.DE */
1397 const char *service_string, /* krbtgt/BER.SUSE.DE@BER.SUSE.DE */
1398 time_t *expire_time)
1400 krb5_error_code ret;
1401 krb5_context context = NULL;
1402 krb5_ccache ccache = NULL;
1403 krb5_principal client = NULL;
1404 krb5_creds creds, creds_in, *creds_out = NULL;
1406 ZERO_STRUCT(creds);
1407 ZERO_STRUCT(creds_in);
1409 initialize_krb5_error_table();
1410 ret = krb5_init_context(&context);
1411 if (ret) {
1412 goto done;
1415 if (!ccache_string) {
1416 ccache_string = krb5_cc_default_name(context);
1419 if (!ccache_string) {
1420 ret = EINVAL;
1421 goto done;
1424 DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string));
1426 /* FIXME: we should not fall back to defaults */
1427 ret = krb5_cc_resolve(context, CONST_DISCARD(char *, ccache_string), &ccache);
1428 if (ret) {
1429 goto done;
1432 if (client_string) {
1433 ret = smb_krb5_parse_name(context, client_string, &client);
1434 if (ret) {
1435 goto done;
1437 } else {
1438 ret = krb5_cc_get_principal(context, ccache, &client);
1439 if (ret) {
1440 goto done;
1444 #ifdef HAVE_KRB5_GET_RENEWED_CREDS /* MIT */
1446 ret = krb5_get_renewed_creds(context, &creds, client, ccache, CONST_DISCARD(char *, service_string));
1447 if (ret) {
1448 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
1449 goto done;
1452 #elif defined(HAVE_KRB5_GET_KDC_CRED) /* Heimdal */
1454 krb5_kdc_flags flags;
1455 krb5_realm *client_realm = NULL;
1457 ret = krb5_copy_principal(context, client, &creds_in.client);
1458 if (ret) {
1459 goto done;
1462 if (service_string) {
1463 ret = smb_krb5_parse_name(context, service_string, &creds_in.server);
1464 if (ret) {
1465 goto done;
1467 } else {
1468 /* build tgt service by default */
1469 client_realm = krb5_princ_realm(context, creds_in.client);
1470 if (!client_realm) {
1471 ret = ENOMEM;
1472 goto done;
1474 ret = krb5_make_principal(context, &creds_in.server, *client_realm, KRB5_TGS_NAME, *client_realm, NULL);
1475 if (ret) {
1476 goto done;
1480 flags.i = 0;
1481 flags.b.renewable = flags.b.renew = True;
1483 ret = krb5_get_kdc_cred(context, ccache, flags, NULL, NULL, &creds_in, &creds_out);
1484 if (ret) {
1485 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
1486 goto done;
1489 creds = *creds_out;
1491 #else
1492 #error NO_SUITABLE_KRB5_TICKET_RENEW_FUNCTION_AVAILABLE
1493 #endif
1495 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
1496 ret = krb5_cc_initialize(context, ccache, client);
1497 if (ret) {
1498 goto done;
1501 ret = krb5_cc_store_cred(context, ccache, &creds);
1503 if (expire_time) {
1504 *expire_time = (time_t) creds.times.endtime;
1507 done:
1508 krb5_free_cred_contents(context, &creds_in);
1510 if (creds_out) {
1511 krb5_free_creds(context, creds_out);
1512 } else {
1513 krb5_free_cred_contents(context, &creds);
1516 if (client) {
1517 krb5_free_principal(context, client);
1519 if (ccache) {
1520 krb5_cc_close(context, ccache);
1522 if (context) {
1523 krb5_free_context(context);
1526 return ret;
1529 krb5_error_code smb_krb5_free_addresses(krb5_context context, smb_krb5_addresses *addr)
1531 krb5_error_code ret = 0;
1532 if (addr == NULL) {
1533 return ret;
1535 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1536 krb5_free_addresses(context, addr->addrs);
1537 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1538 ret = krb5_free_addresses(context, addr->addrs);
1539 SAFE_FREE(addr->addrs);
1540 #endif
1541 SAFE_FREE(addr);
1542 addr = NULL;
1543 return ret;
1546 krb5_error_code smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses **kerb_addr)
1548 krb5_error_code ret = 0;
1549 nstring buf;
1550 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1551 krb5_address **addrs = NULL;
1552 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1553 krb5_addresses *addrs = NULL;
1554 #endif
1556 *kerb_addr = (smb_krb5_addresses *)SMB_MALLOC(sizeof(smb_krb5_addresses));
1557 if (*kerb_addr == NULL) {
1558 return ENOMEM;
1561 put_name(buf, global_myname(), ' ', 0x20);
1563 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1565 int num_addr = 2;
1567 addrs = (krb5_address **)SMB_MALLOC(sizeof(krb5_address *) * num_addr);
1568 if (addrs == NULL) {
1569 SAFE_FREE(*kerb_addr);
1570 return ENOMEM;
1573 memset(addrs, 0, sizeof(krb5_address *) * num_addr);
1575 addrs[0] = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
1576 if (addrs[0] == NULL) {
1577 SAFE_FREE(addrs);
1578 SAFE_FREE(*kerb_addr);
1579 return ENOMEM;
1582 addrs[0]->magic = KV5M_ADDRESS;
1583 addrs[0]->addrtype = KRB5_ADDR_NETBIOS;
1584 addrs[0]->length = MAX_NETBIOSNAME_LEN;
1585 addrs[0]->contents = (unsigned char *)SMB_MALLOC(addrs[0]->length);
1586 if (addrs[0]->contents == NULL) {
1587 SAFE_FREE(addrs[0]);
1588 SAFE_FREE(addrs);
1589 SAFE_FREE(*kerb_addr);
1590 return ENOMEM;
1593 memcpy(addrs[0]->contents, buf, addrs[0]->length);
1595 addrs[1] = NULL;
1597 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1599 addrs = (krb5_addresses *)SMB_MALLOC(sizeof(krb5_addresses));
1600 if (addrs == NULL) {
1601 SAFE_FREE(*kerb_addr);
1602 return ENOMEM;
1605 memset(addrs, 0, sizeof(krb5_addresses));
1607 addrs->len = 1;
1608 addrs->val = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
1609 if (addrs->val == NULL) {
1610 SAFE_FREE(addrs);
1611 SAFE_FREE(kerb_addr);
1612 return ENOMEM;
1615 addrs->val[0].addr_type = KRB5_ADDR_NETBIOS;
1616 addrs->val[0].address.length = MAX_NETBIOSNAME_LEN;
1617 addrs->val[0].address.data = (unsigned char *)SMB_MALLOC(addrs->val[0].address.length);
1618 if (addrs->val[0].address.data == NULL) {
1619 SAFE_FREE(addrs->val);
1620 SAFE_FREE(addrs);
1621 SAFE_FREE(*kerb_addr);
1622 return ENOMEM;
1625 memcpy(addrs->val[0].address.data, buf, addrs->val[0].address.length);
1627 #else
1628 #error UNKNOWN_KRB5_ADDRESS_FORMAT
1629 #endif
1630 (*kerb_addr)->addrs = addrs;
1632 return ret;
1635 void smb_krb5_free_error(krb5_context context, krb5_error *krberror)
1637 #ifdef HAVE_KRB5_FREE_ERROR_CONTENTS /* Heimdal */
1638 krb5_free_error_contents(context, krberror);
1639 #else /* MIT */
1640 krb5_free_error(context, krberror);
1641 #endif
1644 krb5_error_code handle_krberror_packet(krb5_context context,
1645 krb5_data *packet)
1647 krb5_error_code ret;
1648 bool got_error_code = False;
1650 DEBUG(10,("handle_krberror_packet: got error packet\n"));
1652 #ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR /* Heimdal */
1654 krb5_error krberror;
1656 if ((ret = krb5_rd_error(context, packet, &krberror))) {
1657 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1658 error_message(ret)));
1659 return ret;
1662 if (krberror.e_data == NULL || krberror.e_data->data == NULL) {
1663 ret = (krb5_error_code) krberror.error_code;
1664 got_error_code = True;
1667 smb_krb5_free_error(context, &krberror);
1669 #else /* MIT */
1671 krb5_error *krberror;
1673 if ((ret = krb5_rd_error(context, packet, &krberror))) {
1674 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1675 error_message(ret)));
1676 return ret;
1679 if (krberror->e_data.data == NULL) {
1680 #if defined(ERROR_TABLE_BASE_krb5)
1681 ret = ERROR_TABLE_BASE_krb5 + (krb5_error_code) krberror->error;
1682 #else
1683 ret = (krb5_error_code)krberror->error;
1684 #endif
1685 got_error_code = True;
1687 smb_krb5_free_error(context, krberror);
1689 #endif
1690 if (got_error_code) {
1691 DEBUG(5,("handle_krberror_packet: got KERBERR from kpasswd: %s (%d)\n",
1692 error_message(ret), ret));
1694 return ret;
1697 krb5_error_code smb_krb5_get_init_creds_opt_alloc(krb5_context context,
1698 krb5_get_init_creds_opt **opt)
1700 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
1701 /* Heimdal or modern MIT version */
1702 return krb5_get_init_creds_opt_alloc(context, opt);
1703 #else
1704 /* Historical MIT version */
1705 krb5_get_init_creds_opt *my_opt;
1707 *opt = NULL;
1709 if ((my_opt = SMB_MALLOC_P(krb5_get_init_creds_opt)) == NULL) {
1710 return ENOMEM;
1713 krb5_get_init_creds_opt_init(my_opt);
1715 *opt = my_opt;
1716 return 0;
1717 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC */
1720 void smb_krb5_get_init_creds_opt_free(krb5_context context,
1721 krb5_get_init_creds_opt *opt)
1723 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_FREE
1725 #ifdef KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT
1726 /* Modern MIT or Heimdal version */
1727 krb5_get_init_creds_opt_free(context, opt);
1728 #else
1729 /* Heimdal version */
1730 krb5_get_init_creds_opt_free(opt);
1731 #endif /* KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT */
1733 #else /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1734 /* Historical MIT version */
1735 SAFE_FREE(opt);
1736 opt = NULL;
1737 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1740 krb5_enctype smb_get_enctype_from_kt_entry(krb5_keytab_entry *kt_entry)
1742 return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry));
1746 /* caller needs to free etype_s */
1747 krb5_error_code smb_krb5_enctype_to_string(krb5_context context,
1748 krb5_enctype enctype,
1749 char **etype_s)
1751 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
1752 return krb5_enctype_to_string(context, enctype, etype_s); /* Heimdal */
1753 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
1754 char buf[256];
1755 krb5_error_code ret = krb5_enctype_to_string(enctype, buf, 256); /* MIT */
1756 if (ret) {
1757 return ret;
1759 *etype_s = SMB_STRDUP(buf);
1760 if (!*etype_s) {
1761 return ENOMEM;
1763 return ret;
1764 #else
1765 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
1766 #endif
1769 krb5_error_code smb_krb5_mk_error(krb5_context context,
1770 krb5_error_code error_code,
1771 const krb5_principal server,
1772 krb5_data *reply)
1774 #ifdef HAVE_SHORT_KRB5_MK_ERROR_INTERFACE /* MIT */
1776 * The MIT interface is *terrible*.
1777 * We have to construct this ourselves...
1779 krb5_error e;
1781 memset(&e, 0, sizeof(e));
1782 krb5_us_timeofday(context, &e.stime, &e.susec);
1783 e.server = server;
1784 #if defined(krb5_err_base)
1785 e.error = error_code - krb5_err_base;
1786 #elif defined(ERROR_TABLE_BASE_krb5)
1787 e.error = error_code - ERROR_TABLE_BASE_krb5;
1788 #else
1789 e.error = error_code; /* Almost certainly wrong, but what can we do... ? */
1790 #endif
1792 return krb5_mk_error(context, &e, reply);
1793 #else /* Heimdal. */
1794 return krb5_mk_error(context,
1795 error_code,
1796 NULL,
1797 NULL, /* e_data */
1798 NULL,
1799 server,
1800 NULL,
1801 NULL,
1802 reply);
1803 #endif
1806 /**********************************************************************
1807 * Open a krb5 keytab with flags, handles readonly or readwrite access and
1808 * allows to process non-default keytab names.
1809 * @param context krb5_context
1810 * @param keytab_name_req string
1811 * @param write_access bool if writable keytab is required
1812 * @param krb5_keytab pointer to krb5_keytab (close with krb5_kt_close())
1813 * @return krb5_error_code
1814 **********************************************************************/
1816 /* This MAX_NAME_LEN is a constant defined in krb5.h */
1817 #ifndef MAX_KEYTAB_NAME_LEN
1818 #define MAX_KEYTAB_NAME_LEN 1100
1819 #endif
1821 krb5_error_code smb_krb5_open_keytab(krb5_context context,
1822 const char *keytab_name_req,
1823 bool write_access,
1824 krb5_keytab *keytab)
1826 krb5_error_code ret = 0;
1827 TALLOC_CTX *mem_ctx;
1828 char keytab_string[MAX_KEYTAB_NAME_LEN];
1829 char *kt_str = NULL;
1830 bool found_valid_name = False;
1831 const char *pragma = "FILE";
1832 const char *tmp = NULL;
1834 if (!write_access && !keytab_name_req) {
1835 /* caller just wants to read the default keytab readonly, so be it */
1836 return krb5_kt_default(context, keytab);
1839 mem_ctx = talloc_init("smb_krb5_open_keytab");
1840 if (!mem_ctx) {
1841 return ENOMEM;
1844 #ifdef HAVE_WRFILE_KEYTAB
1845 if (write_access) {
1846 pragma = "WRFILE";
1848 #endif
1850 if (keytab_name_req) {
1852 if (strlen(keytab_name_req) > MAX_KEYTAB_NAME_LEN) {
1853 ret = KRB5_CONFIG_NOTENUFSPACE;
1854 goto out;
1857 if ((strncmp(keytab_name_req, "WRFILE:/", 8) == 0) ||
1858 (strncmp(keytab_name_req, "FILE:/", 6) == 0)) {
1859 tmp = keytab_name_req;
1860 goto resolve;
1863 if (keytab_name_req[0] != '/') {
1864 ret = KRB5_KT_BADNAME;
1865 goto out;
1868 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, keytab_name_req);
1869 if (!tmp) {
1870 ret = ENOMEM;
1871 goto out;
1874 goto resolve;
1877 /* we need to handle more complex keytab_strings, like:
1878 * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
1880 ret = krb5_kt_default_name(context, &keytab_string[0], MAX_KEYTAB_NAME_LEN - 2);
1881 if (ret) {
1882 goto out;
1885 DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string));
1887 tmp = talloc_strdup(mem_ctx, keytab_string);
1888 if (!tmp) {
1889 ret = ENOMEM;
1890 goto out;
1893 if (strncmp(tmp, "ANY:", 4) == 0) {
1894 tmp += 4;
1897 memset(&keytab_string, '\0', sizeof(keytab_string));
1899 while (next_token_talloc(mem_ctx, &tmp, &kt_str, ",")) {
1900 if (strncmp(kt_str, "WRFILE:", 7) == 0) {
1901 found_valid_name = True;
1902 tmp = kt_str;
1903 tmp += 7;
1906 if (strncmp(kt_str, "FILE:", 5) == 0) {
1907 found_valid_name = True;
1908 tmp = kt_str;
1909 tmp += 5;
1912 if (tmp[0] == '/') {
1913 /* Treat as a FILE: keytab definition. */
1914 found_valid_name = true;
1917 if (found_valid_name) {
1918 if (tmp[0] != '/') {
1919 ret = KRB5_KT_BADNAME;
1920 goto out;
1923 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, tmp);
1924 if (!tmp) {
1925 ret = ENOMEM;
1926 goto out;
1928 break;
1932 if (!found_valid_name) {
1933 ret = KRB5_KT_UNKNOWN_TYPE;
1934 goto out;
1937 resolve:
1938 DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp));
1939 ret = krb5_kt_resolve(context, tmp, keytab);
1941 out:
1942 TALLOC_FREE(mem_ctx);
1943 return ret;
1946 krb5_error_code smb_krb5_keytab_name(TALLOC_CTX *mem_ctx,
1947 krb5_context context,
1948 krb5_keytab keytab,
1949 const char **keytab_name)
1951 char keytab_string[MAX_KEYTAB_NAME_LEN];
1952 krb5_error_code ret = 0;
1954 ret = krb5_kt_get_name(context, keytab,
1955 keytab_string, MAX_KEYTAB_NAME_LEN - 2);
1956 if (ret) {
1957 return ret;
1960 *keytab_name = talloc_strdup(mem_ctx, keytab_string);
1961 if (!*keytab_name) {
1962 return ENOMEM;
1965 return ret;
1968 #if defined(HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE) && \
1969 defined(HAVE_KRB5_GET_CREDS_OPT_ALLOC) && \
1970 defined(HAVE_KRB5_GET_CREDS)
1971 static krb5_error_code smb_krb5_get_credentials_for_user_opt(krb5_context context,
1972 krb5_ccache ccache,
1973 krb5_principal me,
1974 krb5_principal server,
1975 krb5_principal impersonate_princ,
1976 krb5_creds **out_creds)
1978 krb5_error_code ret;
1979 krb5_get_creds_opt opt;
1981 ret = krb5_get_creds_opt_alloc(context, &opt);
1982 if (ret) {
1983 goto done;
1985 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_FORWARDABLE);
1987 if (impersonate_princ) {
1988 ret = krb5_get_creds_opt_set_impersonate(context, opt,
1989 impersonate_princ);
1990 if (ret) {
1991 goto done;
1995 ret = krb5_get_creds(context, opt, ccache, server, out_creds);
1996 if (ret) {
1997 goto done;
2000 done:
2001 if (opt) {
2002 krb5_get_creds_opt_free(context, opt);
2004 return ret;
2006 #endif /* HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE */
2008 #ifdef HAVE_KRB5_GET_CREDENTIALS_FOR_USER
2009 static krb5_error_code smb_krb5_get_credentials_for_user(krb5_context context,
2010 krb5_ccache ccache,
2011 krb5_principal me,
2012 krb5_principal server,
2013 krb5_principal impersonate_princ,
2014 krb5_creds **out_creds)
2016 krb5_error_code ret;
2017 krb5_creds in_creds;
2019 #if !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER
2020 krb5_error_code KRB5_CALLCONV
2021 krb5_get_credentials_for_user(krb5_context context, krb5_flags options,
2022 krb5_ccache ccache, krb5_creds *in_creds,
2023 krb5_data *subject_cert,
2024 krb5_creds **out_creds);
2025 #endif /* !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER */
2027 ZERO_STRUCT(in_creds);
2029 if (impersonate_princ) {
2031 in_creds.server = me;
2032 in_creds.client = impersonate_princ;
2034 ret = krb5_get_credentials_for_user(context,
2035 0, /* krb5_flags options */
2036 ccache,
2037 &in_creds,
2038 NULL, /* krb5_data *subject_cert */
2039 out_creds);
2040 } else {
2041 in_creds.client = me;
2042 in_creds.server = server;
2044 ret = krb5_get_credentials(context, 0, ccache,
2045 &in_creds, out_creds);
2048 return ret;
2050 #endif /* HAVE_KRB5_GET_CREDENTIALS_FOR_USER */
2053 * smb_krb5_get_credentials
2055 * @brief Get krb5 credentials for a server
2057 * @param[in] context An initialized krb5_context
2058 * @param[in] ccache An initialized krb5_ccache
2059 * @param[in] me The krb5_principal of the caller
2060 * @param[in] server The krb5_principal of the requested service
2061 * @param[in] impersonate_princ The krb5_principal of a user to impersonate as (optional)
2062 * @param[out] out_creds The returned krb5_creds structure
2063 * @return krb5_error_code
2066 krb5_error_code smb_krb5_get_credentials(krb5_context context,
2067 krb5_ccache ccache,
2068 krb5_principal me,
2069 krb5_principal server,
2070 krb5_principal impersonate_princ,
2071 krb5_creds **out_creds)
2073 krb5_error_code ret;
2074 krb5_creds *creds = NULL;
2076 *out_creds = NULL;
2078 if (impersonate_princ) {
2079 #ifdef HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE /* Heimdal */
2080 ret = smb_krb5_get_credentials_for_user_opt(context, ccache, me, server, impersonate_princ, &creds);
2081 #elif defined(HAVE_KRB5_GET_CREDENTIALS_FOR_USER) /* MIT */
2082 ret = smb_krb5_get_credentials_for_user(context, ccache, me, server, impersonate_princ, &creds);
2083 #else
2084 ret = ENOTSUP;
2085 #endif
2086 } else {
2087 krb5_creds in_creds;
2089 ZERO_STRUCT(in_creds);
2091 in_creds.client = me;
2092 in_creds.server = server;
2094 ret = krb5_get_credentials(context, 0, ccache,
2095 &in_creds, &creds);
2097 if (ret) {
2098 goto done;
2101 ret = krb5_cc_store_cred(context, ccache, creds);
2102 if (ret) {
2103 goto done;
2106 if (out_creds) {
2107 *out_creds = creds;
2110 done:
2111 if (creds && ret) {
2112 krb5_free_creds(context, creds);
2115 return ret;
2119 * smb_krb5_get_creds
2121 * @brief Get krb5 credentials for a server
2123 * @param[in] server_s The string name of the service
2124 * @param[in] time_offset The offset to the KDCs time in seconds (optional)
2125 * @param[in] cc The krb5 credential cache string name (optional)
2126 * @param[in] impersonate_princ_s The string principal name to impersonate (optional)
2127 * @param[out] creds_p The returned krb5_creds structure
2128 * @return krb5_error_code
2131 krb5_error_code smb_krb5_get_creds(const char *server_s,
2132 time_t time_offset,
2133 const char *cc,
2134 const char *impersonate_princ_s,
2135 krb5_creds **creds_p)
2137 krb5_error_code ret;
2138 krb5_context context = NULL;
2139 krb5_principal me = NULL;
2140 krb5_principal server = NULL;
2141 krb5_principal impersonate_princ = NULL;
2142 krb5_creds *creds = NULL;
2143 krb5_ccache ccache = NULL;
2145 *creds_p = NULL;
2147 initialize_krb5_error_table();
2148 ret = krb5_init_context(&context);
2149 if (ret) {
2150 goto done;
2153 if (time_offset != 0) {
2154 krb5_set_real_time(context, time(NULL) + time_offset, 0);
2157 ret = krb5_cc_resolve(context, cc ? cc :
2158 krb5_cc_default_name(context), &ccache);
2159 if (ret) {
2160 goto done;
2163 ret = krb5_cc_get_principal(context, ccache, &me);
2164 if (ret) {
2165 goto done;
2168 ret = smb_krb5_parse_name(context, server_s, &server);
2169 if (ret) {
2170 goto done;
2173 if (impersonate_princ_s) {
2174 ret = smb_krb5_parse_name(context, impersonate_princ_s,
2175 &impersonate_princ);
2176 if (ret) {
2177 goto done;
2181 ret = smb_krb5_get_credentials(context, ccache,
2182 me, server, impersonate_princ,
2183 &creds);
2184 if (ret) {
2185 goto done;
2188 ret = krb5_cc_store_cred(context, ccache, creds);
2189 if (ret) {
2190 goto done;
2193 if (creds_p) {
2194 *creds_p = creds;
2197 DEBUG(1,("smb_krb5_get_creds: got ticket for %s\n",
2198 server_s));
2200 if (impersonate_princ_s) {
2201 char *client = NULL;
2203 ret = smb_krb5_unparse_name(talloc_tos(), context, creds->client, &client);
2204 if (ret) {
2205 goto done;
2207 DEBUGADD(1,("smb_krb5_get_creds: using S4U2SELF impersonation as %s\n",
2208 client));
2209 TALLOC_FREE(client);
2212 done:
2213 if (!context) {
2214 return ret;
2217 if (creds && ret) {
2218 krb5_free_creds(context, creds);
2220 if (server) {
2221 krb5_free_principal(context, server);
2223 if (me) {
2224 krb5_free_principal(context, me);
2226 if (impersonate_princ) {
2227 krb5_free_principal(context, impersonate_princ);
2229 if (ccache) {
2230 krb5_cc_close(context, ccache);
2232 krb5_free_context(context);
2234 return ret;
2238 * smb_krb5_principal_get_realm
2240 * @brief Get realm of a principal
2242 * @param[in] context The krb5_context
2243 * @param[in] principal The principal
2244 * @return pointer to the realm
2248 char *smb_krb5_principal_get_realm(krb5_context context,
2249 krb5_principal principal)
2251 #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
2252 return krb5_principal_get_realm(context, principal);
2253 #elif defined(krb5_princ_realm) /* MIT */
2254 krb5_data *realm;
2255 realm = krb5_princ_realm(context, principal);
2256 return (char *)realm->data;
2257 #else
2258 return NULL;
2259 #endif
2262 #else /* HAVE_KRB5 */
2263 /* this saves a few linking headaches */
2264 int cli_krb5_get_ticket(const char *principal, time_t time_offset,
2265 DATA_BLOB *ticket, DATA_BLOB *session_key_krb5, uint32 extra_ap_opts,
2266 const char *ccname, time_t *tgs_expire,
2267 const char *impersonate_princ_s)
2269 DEBUG(0,("NO KERBEROS SUPPORT\n"));
2270 return 1;
2273 #endif