s3: Fix Coverity ID 2325, RESOURCE_LEAK
[Samba.git] / source3 / libsmb / clikrb5.c
blob5e1f70492112e872534036bc72457befb368b164
1 /*
2 Unix SMB/CIFS implementation.
3 simple kerberos5 routines for active directory
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Luke Howard 2002-2003
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
7 Copyright (C) Guenther Deschner 2005-2009
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "smb_krb5.h"
25 #include "../librpc/gen_ndr/krb5pac.h"
26 #include "../lib/util/asn1.h"
27 #include "libsmb/nmblib.h"
29 #ifndef KRB5_AUTHDATA_WIN2K_PAC
30 #define KRB5_AUTHDATA_WIN2K_PAC 128
31 #endif
33 #ifndef KRB5_AUTHDATA_IF_RELEVANT
34 #define KRB5_AUTHDATA_IF_RELEVANT 1
35 #endif
37 #ifdef HAVE_KRB5
39 #define GSSAPI_CHECKSUM 0x8003 /* Checksum type value for Kerberos */
40 #define GSSAPI_BNDLENGTH 16 /* Bind Length (rfc-1964 pg.3) */
41 #define GSSAPI_CHECKSUM_SIZE (4+GSSAPI_BNDLENGTH+4) /* Length of bind length,
42 bind field, flags field. */
44 /* MIT krb5 1.7beta3 (in Ubuntu Karmic) is missing the prototype,
45 but still has the symbol */
46 #if !HAVE_DECL_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE
47 krb5_error_code krb5_auth_con_set_req_cksumtype(
48 krb5_context context,
49 krb5_auth_context auth_context,
50 krb5_cksumtype cksumtype);
51 #endif
53 /**************************************************************
54 Wrappers around kerberos string functions that convert from
55 utf8 -> unix charset and vica versa.
56 **************************************************************/
58 /**************************************************************
59 krb5_parse_name that takes a UNIX charset.
60 **************************************************************/
62 krb5_error_code smb_krb5_parse_name(krb5_context context,
63 const char *name, /* in unix charset */
64 krb5_principal *principal)
66 krb5_error_code ret;
67 char *utf8_name;
68 size_t converted_size;
70 if (!push_utf8_talloc(talloc_tos(), &utf8_name, name, &converted_size)) {
71 return ENOMEM;
74 ret = krb5_parse_name(context, utf8_name, principal);
75 TALLOC_FREE(utf8_name);
76 return ret;
79 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
80 /**************************************************************
81 krb5_parse_name_norealm that takes a UNIX charset.
82 **************************************************************/
84 static krb5_error_code smb_krb5_parse_name_norealm_conv(krb5_context context,
85 const char *name, /* in unix charset */
86 krb5_principal *principal)
88 krb5_error_code ret;
89 char *utf8_name;
90 size_t converted_size;
92 *principal = NULL;
93 if (!push_utf8_talloc(talloc_tos(), &utf8_name, name, &converted_size)) {
94 return ENOMEM;
97 ret = krb5_parse_name_norealm(context, utf8_name, principal);
98 TALLOC_FREE(utf8_name);
99 return ret;
101 #endif
103 /**************************************************************
104 krb5_parse_name that returns a UNIX charset name. Must
105 be freed with talloc_free() call.
106 **************************************************************/
108 krb5_error_code smb_krb5_unparse_name(TALLOC_CTX *mem_ctx,
109 krb5_context context,
110 krb5_const_principal principal,
111 char **unix_name)
113 krb5_error_code ret;
114 char *utf8_name;
115 size_t converted_size;
117 *unix_name = NULL;
118 ret = krb5_unparse_name(context, principal, &utf8_name);
119 if (ret) {
120 return ret;
123 if (!pull_utf8_talloc(mem_ctx, unix_name, utf8_name, &converted_size)) {
124 krb5_free_unparsed_name(context, utf8_name);
125 return ENOMEM;
127 krb5_free_unparsed_name(context, utf8_name);
128 return 0;
131 #ifndef HAVE_KRB5_SET_REAL_TIME
133 * This function is not in the Heimdal mainline.
135 krb5_error_code krb5_set_real_time(krb5_context context, int32_t seconds, int32_t microseconds)
137 krb5_error_code ret;
138 int32_t sec, usec;
140 ret = krb5_us_timeofday(context, &sec, &usec);
141 if (ret)
142 return ret;
144 context->kdc_sec_offset = seconds - sec;
145 context->kdc_usec_offset = microseconds - usec;
147 return 0;
149 #endif
151 #if !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
153 #if defined(HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES)
155 /* With MIT kerberos, we should use krb5_set_default_tgs_enctypes in preference
156 * to krb5_set_default_tgs_ktypes. See
157 * http://lists.samba.org/archive/samba-technical/2006-July/048271.html
159 * If the MIT libraries are not exporting internal symbols, we will end up in
160 * this branch, which is correct. Otherwise we will continue to use the
161 * internal symbol
163 krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
165 return krb5_set_default_tgs_enctypes(ctx, enc);
168 #elif defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES)
170 /* Heimdal */
171 krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
173 return krb5_set_default_in_tkt_etypes(ctx, enc);
176 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES */
178 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_KTYPES */
180 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
181 /* HEIMDAL */
182 bool setup_kaddr( krb5_address *pkaddr, struct sockaddr_storage *paddr)
184 memset(pkaddr, '\0', sizeof(krb5_address));
185 #if defined(HAVE_IPV6) && defined(KRB5_ADDRESS_INET6)
186 if (paddr->ss_family == AF_INET6) {
187 pkaddr->addr_type = KRB5_ADDRESS_INET6;
188 pkaddr->address.length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr);
189 pkaddr->address.data = (char *)&(((struct sockaddr_in6 *)paddr)->sin6_addr);
190 return true;
192 #endif
193 if (paddr->ss_family == AF_INET) {
194 pkaddr->addr_type = KRB5_ADDRESS_INET;
195 pkaddr->address.length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
196 pkaddr->address.data = (char *)&(((struct sockaddr_in *)paddr)->sin_addr);
197 return true;
199 return false;
201 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
202 /* MIT */
203 bool setup_kaddr( krb5_address *pkaddr, struct sockaddr_storage *paddr)
205 memset(pkaddr, '\0', sizeof(krb5_address));
206 #if defined(HAVE_IPV6) && defined(ADDRTYPE_INET6)
207 if (paddr->ss_family == AF_INET6) {
208 pkaddr->addrtype = ADDRTYPE_INET6;
209 pkaddr->length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr);
210 pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in6 *)paddr)->sin6_addr);
211 return true;
213 #endif
214 if (paddr->ss_family == AF_INET) {
215 pkaddr->addrtype = ADDRTYPE_INET;
216 pkaddr->length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
217 pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in *)paddr)->sin_addr);
218 return true;
220 return false;
222 #else
223 #error UNKNOWN_ADDRTYPE
224 #endif
226 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_USE_ENCTYPE) && defined(HAVE_KRB5_STRING_TO_KEY) && defined(HAVE_KRB5_ENCRYPT_BLOCK)
227 static int create_kerberos_key_from_string_direct(krb5_context context,
228 krb5_principal host_princ,
229 krb5_data *password,
230 krb5_keyblock *key,
231 krb5_enctype enctype)
233 int ret = 0;
234 krb5_data salt;
235 krb5_encrypt_block eblock;
237 ret = krb5_principal2salt(context, host_princ, &salt);
238 if (ret) {
239 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
240 return ret;
242 krb5_use_enctype(context, &eblock, enctype);
243 ret = krb5_string_to_key(context, &eblock, key, password, &salt);
244 SAFE_FREE(salt.data);
246 return ret;
248 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
249 static int create_kerberos_key_from_string_direct(krb5_context context,
250 krb5_principal host_princ,
251 krb5_data *password,
252 krb5_keyblock *key,
253 krb5_enctype enctype)
255 int ret;
256 krb5_salt salt;
258 ret = krb5_get_pw_salt(context, host_princ, &salt);
259 if (ret) {
260 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret)));
261 return ret;
264 ret = krb5_string_to_key_salt(context, enctype, (const char *)password->data, salt, key);
265 krb5_free_salt(context, salt);
267 return ret;
269 #else
270 #error UNKNOWN_CREATE_KEY_FUNCTIONS
271 #endif
273 int create_kerberos_key_from_string(krb5_context context,
274 krb5_principal host_princ,
275 krb5_data *password,
276 krb5_keyblock *key,
277 krb5_enctype enctype,
278 bool no_salt)
280 krb5_principal salt_princ = NULL;
281 int ret;
283 * Check if we've determined that the KDC is salting keys for this
284 * principal/enctype in a non-obvious way. If it is, try to match
285 * its behavior.
287 if (no_salt) {
288 KRB5_KEY_DATA(key) = (KRB5_KEY_DATA_CAST *)SMB_MALLOC(password->length);
289 if (!KRB5_KEY_DATA(key)) {
290 return ENOMEM;
292 memcpy(KRB5_KEY_DATA(key), password->data, password->length);
293 KRB5_KEY_LENGTH(key) = password->length;
294 KRB5_KEY_TYPE(key) = enctype;
295 return 0;
297 salt_princ = kerberos_fetch_salt_princ_for_host_princ(context, host_princ, enctype);
298 ret = create_kerberos_key_from_string_direct(context, salt_princ ? salt_princ : host_princ, password, key, enctype);
299 if (salt_princ) {
300 krb5_free_principal(context, salt_princ);
302 return ret;
305 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
306 krb5_error_code get_kerberos_allowed_etypes(krb5_context context,
307 krb5_enctype **enctypes)
309 return krb5_get_permitted_enctypes(context, enctypes);
311 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
312 krb5_error_code get_kerberos_allowed_etypes(krb5_context context,
313 krb5_enctype **enctypes)
315 return krb5_get_default_in_tkt_etypes(context, enctypes);
317 #else
318 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
319 #endif
321 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
322 krb5_error_code krb5_auth_con_setuseruserkey(krb5_context context,
323 krb5_auth_context auth_context,
324 krb5_keyblock *keyblock)
326 return krb5_auth_con_setkey(context, auth_context, keyblock);
328 #endif
330 bool unwrap_edata_ntstatus(TALLOC_CTX *mem_ctx,
331 DATA_BLOB *edata,
332 DATA_BLOB *edata_out)
334 DATA_BLOB edata_contents;
335 ASN1_DATA *data;
336 int edata_type;
338 if (!edata->length) {
339 return False;
342 data = asn1_init(mem_ctx);
343 if (data == NULL) {
344 return false;
347 asn1_load(data, *edata);
348 asn1_start_tag(data, ASN1_SEQUENCE(0));
349 asn1_start_tag(data, ASN1_CONTEXT(1));
350 asn1_read_Integer(data, &edata_type);
352 if (edata_type != KRB5_PADATA_PW_SALT) {
353 DEBUG(0,("edata is not of required type %d but of type %d\n",
354 KRB5_PADATA_PW_SALT, edata_type));
355 asn1_free(data);
356 return False;
359 asn1_start_tag(data, ASN1_CONTEXT(2));
360 asn1_read_OctetString(data, talloc_tos(), &edata_contents);
361 asn1_end_tag(data);
362 asn1_end_tag(data);
363 asn1_end_tag(data);
364 asn1_free(data);
366 *edata_out = data_blob_talloc(mem_ctx, edata_contents.data, edata_contents.length);
368 data_blob_free(&edata_contents);
370 return True;
374 bool unwrap_pac(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, DATA_BLOB *unwrapped_pac_data)
376 DATA_BLOB pac_contents;
377 ASN1_DATA *data;
378 int data_type;
380 if (!auth_data->length) {
381 return False;
384 data = asn1_init(mem_ctx);
385 if (data == NULL) {
386 return false;
389 asn1_load(data, *auth_data);
390 asn1_start_tag(data, ASN1_SEQUENCE(0));
391 asn1_start_tag(data, ASN1_SEQUENCE(0));
392 asn1_start_tag(data, ASN1_CONTEXT(0));
393 asn1_read_Integer(data, &data_type);
395 if (data_type != KRB5_AUTHDATA_WIN2K_PAC ) {
396 DEBUG(10,("authorization data is not a Windows PAC (type: %d)\n", data_type));
397 asn1_free(data);
398 return False;
401 asn1_end_tag(data);
402 asn1_start_tag(data, ASN1_CONTEXT(1));
403 asn1_read_OctetString(data, talloc_tos(), &pac_contents);
404 asn1_end_tag(data);
405 asn1_end_tag(data);
406 asn1_end_tag(data);
407 asn1_free(data);
409 *unwrapped_pac_data = data_blob_talloc(mem_ctx, pac_contents.data, pac_contents.length);
411 data_blob_free(&pac_contents);
413 return True;
416 bool get_auth_data_from_tkt(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, krb5_ticket *tkt)
418 DATA_BLOB auth_data_wrapped;
419 bool got_auth_data_pac = False;
420 int i;
422 #if defined(HAVE_KRB5_TKT_ENC_PART2)
423 if (tkt->enc_part2 && tkt->enc_part2->authorization_data &&
424 tkt->enc_part2->authorization_data[0] &&
425 tkt->enc_part2->authorization_data[0]->length)
427 for (i = 0; tkt->enc_part2->authorization_data[i] != NULL; i++) {
429 if (tkt->enc_part2->authorization_data[i]->ad_type !=
430 KRB5_AUTHDATA_IF_RELEVANT) {
431 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
432 tkt->enc_part2->authorization_data[i]->ad_type));
433 continue;
436 auth_data_wrapped = data_blob(tkt->enc_part2->authorization_data[i]->contents,
437 tkt->enc_part2->authorization_data[i]->length);
439 /* check if it is a PAC */
440 got_auth_data_pac = unwrap_pac(mem_ctx, &auth_data_wrapped, auth_data);
441 data_blob_free(&auth_data_wrapped);
443 if (got_auth_data_pac) {
444 return true;
448 return got_auth_data_pac;
451 #else
452 if (tkt->ticket.authorization_data &&
453 tkt->ticket.authorization_data->len)
455 for (i = 0; i < tkt->ticket.authorization_data->len; i++) {
457 if (tkt->ticket.authorization_data->val[i].ad_type !=
458 KRB5_AUTHDATA_IF_RELEVANT) {
459 DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n",
460 tkt->ticket.authorization_data->val[i].ad_type));
461 continue;
464 auth_data_wrapped = data_blob(tkt->ticket.authorization_data->val[i].ad_data.data,
465 tkt->ticket.authorization_data->val[i].ad_data.length);
467 /* check if it is a PAC */
468 got_auth_data_pac = unwrap_pac(mem_ctx, &auth_data_wrapped, auth_data);
469 data_blob_free(&auth_data_wrapped);
471 if (got_auth_data_pac) {
472 return true;
476 return got_auth_data_pac;
478 #endif
479 return False;
482 krb5_const_principal get_principal_from_tkt(krb5_ticket *tkt)
484 #if defined(HAVE_KRB5_TKT_ENC_PART2)
485 return tkt->enc_part2->client;
486 #else
487 return tkt->client;
488 #endif
491 #if !defined(HAVE_KRB5_LOCATE_KDC)
493 /* krb5_locate_kdc is an internal MIT symbol. MIT are not yet willing to commit
494 * to a public interface for this functionality, so we have to be able to live
495 * without it if the MIT libraries are hiding their internal symbols.
498 #if defined(KRB5_KRBHST_INIT)
499 /* Heimdal */
500 krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm, struct sockaddr **addr_pp, int *naddrs, int get_masters)
502 krb5_krbhst_handle hnd;
503 krb5_krbhst_info *hinfo;
504 krb5_error_code rc;
505 int num_kdcs, i;
506 struct sockaddr *sa;
507 struct addrinfo *ai;
509 *addr_pp = NULL;
510 *naddrs = 0;
512 rc = krb5_krbhst_init(ctx, realm->data, KRB5_KRBHST_KDC, &hnd);
513 if (rc) {
514 DEBUG(0, ("smb_krb5_locate_kdc: krb5_krbhst_init failed (%s)\n", error_message(rc)));
515 return rc;
518 for ( num_kdcs = 0; (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); num_kdcs++)
521 krb5_krbhst_reset(ctx, hnd);
523 if (!num_kdcs) {
524 DEBUG(0, ("smb_krb5_locate_kdc: zero kdcs found !\n"));
525 krb5_krbhst_free(ctx, hnd);
526 return -1;
529 sa = SMB_MALLOC_ARRAY( struct sockaddr, num_kdcs );
530 if (!sa) {
531 DEBUG(0, ("smb_krb5_locate_kdc: malloc failed\n"));
532 krb5_krbhst_free(ctx, hnd);
533 naddrs = 0;
534 return -1;
537 memset(sa, '\0', sizeof(struct sockaddr) * num_kdcs );
539 for (i = 0; i < num_kdcs && (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); i++) {
541 #if defined(HAVE_KRB5_KRBHST_GET_ADDRINFO)
542 rc = krb5_krbhst_get_addrinfo(ctx, hinfo, &ai);
543 if (rc) {
544 DEBUG(0,("krb5_krbhst_get_addrinfo failed: %s\n", error_message(rc)));
545 continue;
547 #endif
548 if (hinfo->ai && hinfo->ai->ai_family == AF_INET)
549 memcpy(&sa[i], hinfo->ai->ai_addr, sizeof(struct sockaddr));
552 krb5_krbhst_free(ctx, hnd);
554 *naddrs = num_kdcs;
555 *addr_pp = sa;
556 return 0;
559 #else /* ! defined(KRB5_KRBHST_INIT) */
561 krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm,
562 struct sockaddr **addr_pp, int *naddrs, int get_masters)
564 DEBUG(0, ("unable to explicitly locate the KDC on this platform\n"));
565 return KRB5_KDC_UNREACH;
568 #endif /* KRB5_KRBHST_INIT */
570 #else /* ! HAVE_KRB5_LOCATE_KDC */
572 krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm,
573 struct sockaddr **addr_pp, int *naddrs, int get_masters)
575 return krb5_locate_kdc(ctx, realm, addr_pp, naddrs, get_masters);
578 #endif /* HAVE_KRB5_LOCATE_KDC */
580 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
581 void krb5_free_unparsed_name(krb5_context context, char *val)
583 SAFE_FREE(val);
585 #endif
587 void kerberos_free_data_contents(krb5_context context, krb5_data *pdata)
589 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
590 if (pdata->data) {
591 krb5_free_data_contents(context, pdata);
593 #else
594 SAFE_FREE(pdata->data);
595 #endif
598 void kerberos_set_creds_enctype(krb5_creds *pcreds, int enctype)
600 #if defined(HAVE_KRB5_KEYBLOCK_IN_CREDS)
601 KRB5_KEY_TYPE((&pcreds->keyblock)) = enctype;
602 #elif defined(HAVE_KRB5_SESSION_IN_CREDS)
603 KRB5_KEY_TYPE((&pcreds->session)) = enctype;
604 #else
605 #error UNKNOWN_KEYBLOCK_MEMBER_IN_KRB5_CREDS_STRUCT
606 #endif
609 bool kerberos_compatible_enctypes(krb5_context context,
610 krb5_enctype enctype1,
611 krb5_enctype enctype2)
613 #if defined(HAVE_KRB5_C_ENCTYPE_COMPARE)
614 krb5_boolean similar = 0;
616 krb5_c_enctype_compare(context, enctype1, enctype2, &similar);
617 return similar ? True : False;
618 #elif defined(HAVE_KRB5_ENCTYPES_COMPATIBLE_KEYS)
619 return krb5_enctypes_compatible_keys(context, enctype1, enctype2) ? True : False;
620 #endif
623 static bool ads_cleanup_expired_creds(krb5_context context,
624 krb5_ccache ccache,
625 krb5_creds *credsp)
627 krb5_error_code retval;
628 const char *cc_type = krb5_cc_get_type(context, ccache);
630 DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
631 cc_type, krb5_cc_get_name(context, ccache),
632 http_timestring(talloc_tos(), credsp->times.endtime)));
634 /* we will probably need new tickets if the current ones
635 will expire within 10 seconds.
637 if (credsp->times.endtime >= (time(NULL) + 10))
638 return False;
640 /* heimdal won't remove creds from a file ccache, and
641 perhaps we shouldn't anyway, since internally we
642 use memory ccaches, and a FILE one probably means that
643 we're using creds obtained outside of our exectuable
645 if (strequal(cc_type, "FILE")) {
646 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type));
647 return False;
650 retval = krb5_cc_remove_cred(context, ccache, 0, credsp);
651 if (retval) {
652 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
653 error_message(retval)));
654 /* If we have an error in this, we want to display it,
655 but continue as though we deleted it */
657 return True;
660 /* Allocate and setup the auth context into the state we need. */
662 static krb5_error_code setup_auth_context(krb5_context context,
663 krb5_auth_context *auth_context)
665 krb5_error_code retval;
667 retval = krb5_auth_con_init(context, auth_context );
668 if (retval) {
669 DEBUG(1,("krb5_auth_con_init failed (%s)\n",
670 error_message(retval)));
671 return retval;
674 /* Ensure this is an addressless ticket. */
675 retval = krb5_auth_con_setaddrs(context, *auth_context, NULL, NULL);
676 if (retval) {
677 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n",
678 error_message(retval)));
681 return retval;
684 static krb5_error_code create_gss_checksum(krb5_data *in_data, /* [inout] */
685 uint32_t gss_flags)
687 unsigned int orig_length = in_data->length;
688 unsigned int base_cksum_size = GSSAPI_CHECKSUM_SIZE;
689 char *gss_cksum = NULL;
691 if (orig_length) {
692 /* Extra length field for delgated ticket. */
693 base_cksum_size += 4;
696 if ((unsigned int)base_cksum_size + orig_length <
697 (unsigned int)base_cksum_size) {
698 return EINVAL;
701 gss_cksum = (char *)SMB_MALLOC(base_cksum_size + orig_length);
702 if (gss_cksum == NULL) {
703 return ENOMEM;
706 memset(gss_cksum, '\0', base_cksum_size + orig_length);
707 SIVAL(gss_cksum, 0, GSSAPI_BNDLENGTH);
710 * GSS_C_NO_CHANNEL_BINDINGS means 16 zero bytes.
711 * This matches the behavior of heimdal and mit.
713 * And it is needed to work against some closed source
714 * SMB servers.
716 * See bug #7883
718 memset(&gss_cksum[4], 0x00, 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(TALLOC_CTX *mem_ctx,
944 const char *principal, time_t time_offset,
945 DATA_BLOB *ticket, DATA_BLOB *session_key_krb5,
946 uint32_t extra_ap_opts, const char *ccname,
947 time_t *tgs_expire,
948 const char *impersonate_princ_s)
951 krb5_error_code retval;
952 krb5_data packet;
953 krb5_context context = NULL;
954 krb5_ccache ccdef = NULL;
955 krb5_auth_context auth_context = NULL;
956 krb5_enctype enc_types[] = {
957 ENCTYPE_ARCFOUR_HMAC,
958 ENCTYPE_DES_CBC_MD5,
959 ENCTYPE_DES_CBC_CRC,
960 ENCTYPE_NULL};
962 initialize_krb5_error_table();
963 retval = krb5_init_context(&context);
964 if (retval) {
965 DEBUG(1, ("krb5_init_context failed (%s)\n",
966 error_message(retval)));
967 goto failed;
970 if (time_offset != 0) {
971 krb5_set_real_time(context, time(NULL) + time_offset, 0);
974 if ((retval = krb5_cc_resolve(context, ccname ?
975 ccname : krb5_cc_default_name(context), &ccdef))) {
976 DEBUG(1, ("krb5_cc_default failed (%s)\n",
977 error_message(retval)));
978 goto failed;
981 if ((retval = krb5_set_default_tgs_ktypes(context, enc_types))) {
982 DEBUG(1, ("krb5_set_default_tgs_ktypes failed (%s)\n",
983 error_message(retval)));
984 goto failed;
987 retval = ads_krb5_mk_req(context, &auth_context,
988 AP_OPTS_USE_SUBKEY | (krb5_flags)extra_ap_opts,
989 principal, ccdef, &packet,
990 tgs_expire, impersonate_princ_s);
991 if (retval) {
992 goto failed;
995 get_krb5_smb_session_key(mem_ctx, context, auth_context,
996 session_key_krb5, false);
998 *ticket = data_blob_talloc(mem_ctx, packet.data, packet.length);
1000 kerberos_free_data_contents(context, &packet);
1002 failed:
1004 if (context) {
1005 if (ccdef)
1006 krb5_cc_close(context, ccdef);
1007 if (auth_context)
1008 krb5_auth_con_free(context, auth_context);
1009 krb5_free_context(context);
1012 return retval;
1015 bool get_krb5_smb_session_key(TALLOC_CTX *mem_ctx,
1016 krb5_context context,
1017 krb5_auth_context auth_context,
1018 DATA_BLOB *session_key, bool remote)
1020 krb5_keyblock *skey = NULL;
1021 krb5_error_code err = 0;
1022 bool ret = false;
1024 if (remote) {
1025 err = krb5_auth_con_getremotesubkey(context,
1026 auth_context, &skey);
1027 } else {
1028 err = krb5_auth_con_getlocalsubkey(context,
1029 auth_context, &skey);
1032 if (err || skey == NULL) {
1033 DEBUG(10, ("KRB5 error getting session key %d\n", err));
1034 goto done;
1037 DEBUG(10, ("Got KRB5 session key of length %d\n",
1038 (int)KRB5_KEY_LENGTH(skey)));
1040 *session_key = data_blob_talloc(mem_ctx,
1041 KRB5_KEY_DATA(skey),
1042 KRB5_KEY_LENGTH(skey));
1043 dump_data_pw("KRB5 Session Key:\n",
1044 session_key->data,
1045 session_key->length);
1047 ret = true;
1049 done:
1050 if (skey) {
1051 krb5_free_keyblock(context, skey);
1054 return ret;
1058 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
1059 const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i );
1061 const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i )
1063 static krb5_data kdata;
1065 kdata.data = (char *)krb5_principal_get_comp_string(context, principal, i);
1066 kdata.length = strlen((const char *)kdata.data);
1067 return &kdata;
1069 #endif
1071 krb5_error_code smb_krb5_kt_free_entry(krb5_context context, krb5_keytab_entry *kt_entry)
1073 /* Try krb5_free_keytab_entry_contents first, since
1074 * MIT Kerberos >= 1.7 has both krb5_free_keytab_entry_contents and
1075 * krb5_kt_free_entry but only has a prototype for the first, while the
1076 * second is considered private.
1078 #if defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
1079 return krb5_free_keytab_entry_contents(context, kt_entry);
1080 #elif defined(HAVE_KRB5_KT_FREE_ENTRY)
1081 return krb5_kt_free_entry(context, kt_entry);
1082 #else
1083 #error UNKNOWN_KT_FREE_FUNCTION
1084 #endif
1087 void smb_krb5_checksum_from_pac_sig(krb5_checksum *cksum,
1088 struct PAC_SIGNATURE_DATA *sig)
1090 #ifdef HAVE_CHECKSUM_IN_KRB5_CHECKSUM
1091 cksum->cksumtype = (krb5_cksumtype)sig->type;
1092 cksum->checksum.length = sig->signature.length;
1093 cksum->checksum.data = sig->signature.data;
1094 #else
1095 cksum->checksum_type = (krb5_cksumtype)sig->type;
1096 cksum->length = sig->signature.length;
1097 cksum->contents = sig->signature.data;
1098 #endif
1101 krb5_error_code smb_krb5_verify_checksum(krb5_context context,
1102 const krb5_keyblock *keyblock,
1103 krb5_keyusage usage,
1104 krb5_checksum *cksum,
1105 uint8 *data,
1106 size_t length)
1108 krb5_error_code ret;
1110 /* verify the checksum */
1112 /* welcome to the wonderful world of samba's kerberos abstraction layer:
1114 * function heimdal 0.6.1rc3 heimdal 0.7 MIT krb 1.4.2
1115 * -----------------------------------------------------------------------------
1116 * krb5_c_verify_checksum - works works
1117 * krb5_verify_checksum works (6 args) works (6 args) broken (7 args)
1120 #if defined(HAVE_KRB5_C_VERIFY_CHECKSUM)
1122 krb5_boolean checksum_valid = False;
1123 krb5_data input;
1125 input.data = (char *)data;
1126 input.length = length;
1128 ret = krb5_c_verify_checksum(context,
1129 keyblock,
1130 usage,
1131 &input,
1132 cksum,
1133 &checksum_valid);
1134 if (ret) {
1135 DEBUG(3,("smb_krb5_verify_checksum: krb5_c_verify_checksum() failed: %s\n",
1136 error_message(ret)));
1137 return ret;
1140 if (!checksum_valid)
1141 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1144 #elif KRB5_VERIFY_CHECKSUM_ARGS == 6 && defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CRYPTO) && defined(HAVE_KRB5_CRYPTO_DESTROY)
1146 /* Warning: MIT's krb5_verify_checksum cannot be used as it will use a key
1147 * without enctype and it ignores any key_usage types - Guenther */
1151 krb5_crypto crypto;
1152 ret = krb5_crypto_init(context,
1153 keyblock,
1155 &crypto);
1156 if (ret) {
1157 DEBUG(0,("smb_krb5_verify_checksum: krb5_crypto_init() failed: %s\n",
1158 error_message(ret)));
1159 return ret;
1162 ret = krb5_verify_checksum(context,
1163 crypto,
1164 usage,
1165 data,
1166 length,
1167 cksum);
1169 krb5_crypto_destroy(context, crypto);
1172 #else
1173 #error UNKNOWN_KRB5_VERIFY_CHECKSUM_FUNCTION
1174 #endif
1176 return ret;
1179 time_t get_authtime_from_tkt(krb5_ticket *tkt)
1181 #if defined(HAVE_KRB5_TKT_ENC_PART2)
1182 return tkt->enc_part2->times.authtime;
1183 #else
1184 return tkt->ticket.authtime;
1185 #endif
1188 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
1189 static int get_kvno_from_ap_req(krb5_ap_req *ap_req)
1191 #ifdef HAVE_TICKET_POINTER_IN_KRB5_AP_REQ /* MIT */
1192 if (ap_req->ticket->enc_part.kvno)
1193 return ap_req->ticket->enc_part.kvno;
1194 #else /* Heimdal */
1195 if (ap_req->ticket.enc_part.kvno)
1196 return *ap_req->ticket.enc_part.kvno;
1197 #endif
1198 return 0;
1201 static krb5_enctype get_enctype_from_ap_req(krb5_ap_req *ap_req)
1203 #ifdef HAVE_ETYPE_IN_ENCRYPTEDDATA /* Heimdal */
1204 return ap_req->ticket.enc_part.etype;
1205 #else /* MIT */
1206 return ap_req->ticket->enc_part.enctype;
1207 #endif
1209 #endif /* HAVE_KRB5_DECODE_AP_REQ */
1211 static krb5_error_code
1212 get_key_from_keytab(krb5_context context,
1213 krb5_const_principal server,
1214 krb5_enctype enctype,
1215 krb5_kvno kvno,
1216 krb5_keyblock **out_key)
1218 krb5_keytab_entry entry;
1219 krb5_error_code ret;
1220 krb5_keytab keytab;
1221 char *name = NULL;
1222 krb5_keyblock *keyp;
1224 /* We have to open a new keytab handle here, as MIT does
1225 an implicit open/getnext/close on krb5_kt_get_entry. We
1226 may be in the middle of a keytab enumeration when this is
1227 called. JRA. */
1229 ret = smb_krb5_open_keytab(context, NULL, False, &keytab);
1230 if (ret) {
1231 DEBUG(1,("get_key_from_keytab: smb_krb5_open_keytab failed (%s)\n", error_message(ret)));
1232 return ret;
1235 if ( DEBUGLEVEL >= 10 ) {
1236 if (smb_krb5_unparse_name(talloc_tos(), context, server, &name) == 0) {
1237 DEBUG(10,("get_key_from_keytab: will look for kvno %d, enctype %d and name: %s\n",
1238 kvno, enctype, name));
1239 TALLOC_FREE(name);
1243 ret = krb5_kt_get_entry(context,
1244 keytab,
1245 server,
1246 kvno,
1247 enctype,
1248 &entry);
1250 if (ret) {
1251 DEBUG(0,("get_key_from_keytab: failed to retrieve key: %s\n", error_message(ret)));
1252 goto out;
1255 keyp = KRB5_KT_KEY(&entry);
1257 ret = krb5_copy_keyblock(context, keyp, out_key);
1258 if (ret) {
1259 DEBUG(0,("get_key_from_keytab: failed to copy key: %s\n", error_message(ret)));
1260 goto out;
1263 smb_krb5_kt_free_entry(context, &entry);
1265 out:
1266 krb5_kt_close(context, keytab);
1267 return ret;
1270 /* Prototypes */
1272 krb5_error_code smb_krb5_get_keyinfo_from_ap_req(krb5_context context,
1273 const krb5_data *inbuf,
1274 krb5_kvno *kvno,
1275 krb5_enctype *enctype)
1277 #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
1279 krb5_error_code ret;
1280 krb5_ap_req ap_req;
1282 ret = krb5_decode_ap_req(context, inbuf, &ap_req);
1283 if (ret)
1284 return ret;
1286 *kvno = get_kvno_from_ap_req(&ap_req);
1287 *enctype = get_enctype_from_ap_req(&ap_req);
1289 free_AP_REQ(&ap_req);
1290 return 0;
1292 #endif
1294 /* Possibly not an appropriate error code. */
1295 return KRB5KDC_ERR_BADOPTION;
1298 krb5_error_code krb5_rd_req_return_keyblock_from_keytab(krb5_context context,
1299 krb5_auth_context *auth_context,
1300 const krb5_data *inbuf,
1301 krb5_const_principal server,
1302 krb5_keytab keytab,
1303 krb5_flags *ap_req_options,
1304 krb5_ticket **ticket,
1305 krb5_keyblock **keyblock)
1307 krb5_error_code ret;
1308 krb5_kvno kvno;
1309 krb5_enctype enctype;
1310 krb5_keyblock *local_keyblock;
1312 ret = krb5_rd_req(context,
1313 auth_context,
1314 inbuf,
1315 server,
1316 keytab,
1317 ap_req_options,
1318 ticket);
1319 if (ret) {
1320 return ret;
1323 #ifdef KRB5_TICKET_HAS_KEYINFO
1324 enctype = (*ticket)->enc_part.enctype;
1325 kvno = (*ticket)->enc_part.kvno;
1326 #else
1327 ret = smb_krb5_get_keyinfo_from_ap_req(context, inbuf, &kvno, &enctype);
1328 if (ret) {
1329 return ret;
1331 #endif
1333 ret = get_key_from_keytab(context,
1334 server,
1335 enctype,
1336 kvno,
1337 &local_keyblock);
1338 if (ret) {
1339 DEBUG(0,("krb5_rd_req_return_keyblock_from_keytab: failed to call get_key_from_keytab\n"));
1340 goto out;
1343 out:
1344 if (ret && local_keyblock != NULL) {
1345 krb5_free_keyblock(context, local_keyblock);
1346 } else {
1347 *keyblock = local_keyblock;
1350 return ret;
1353 krb5_error_code smb_krb5_parse_name_norealm(krb5_context context,
1354 const char *name,
1355 krb5_principal *principal)
1357 #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
1358 return smb_krb5_parse_name_norealm_conv(context, name, principal);
1359 #endif
1361 /* we are cheating here because parse_name will in fact set the realm.
1362 * We don't care as the only caller of smb_krb5_parse_name_norealm
1363 * ignores the realm anyway when calling
1364 * smb_krb5_principal_compare_any_realm later - Guenther */
1366 return smb_krb5_parse_name(context, name, principal);
1369 bool smb_krb5_principal_compare_any_realm(krb5_context context,
1370 krb5_const_principal princ1,
1371 krb5_const_principal princ2)
1373 #ifdef HAVE_KRB5_PRINCIPAL_COMPARE_ANY_REALM
1375 return krb5_principal_compare_any_realm(context, princ1, princ2);
1377 /* krb5_princ_size is a macro in MIT */
1378 #elif defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
1380 int i, len1, len2;
1381 const krb5_data *p1, *p2;
1383 len1 = krb5_princ_size(context, princ1);
1384 len2 = krb5_princ_size(context, princ2);
1386 if (len1 != len2)
1387 return False;
1389 for (i = 0; i < len1; i++) {
1391 p1 = krb5_princ_component(context, CONST_DISCARD(krb5_principal, princ1), i);
1392 p2 = krb5_princ_component(context, CONST_DISCARD(krb5_principal, princ2), i);
1394 if (p1->length != p2->length || memcmp(p1->data, p2->data, p1->length))
1395 return False;
1398 return True;
1399 #else
1400 #error NO_SUITABLE_PRINCIPAL_COMPARE_FUNCTION
1401 #endif
1404 krb5_error_code smb_krb5_renew_ticket(const char *ccache_string, /* FILE:/tmp/krb5cc_0 */
1405 const char *client_string, /* gd@BER.SUSE.DE */
1406 const char *service_string, /* krbtgt/BER.SUSE.DE@BER.SUSE.DE */
1407 time_t *expire_time)
1409 krb5_error_code ret;
1410 krb5_context context = NULL;
1411 krb5_ccache ccache = NULL;
1412 krb5_principal client = NULL;
1413 krb5_creds creds, creds_in, *creds_out = NULL;
1415 ZERO_STRUCT(creds);
1416 ZERO_STRUCT(creds_in);
1418 initialize_krb5_error_table();
1419 ret = krb5_init_context(&context);
1420 if (ret) {
1421 goto done;
1424 if (!ccache_string) {
1425 ccache_string = krb5_cc_default_name(context);
1428 if (!ccache_string) {
1429 ret = EINVAL;
1430 goto done;
1433 DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string));
1435 /* FIXME: we should not fall back to defaults */
1436 ret = krb5_cc_resolve(context, CONST_DISCARD(char *, ccache_string), &ccache);
1437 if (ret) {
1438 goto done;
1441 if (client_string) {
1442 ret = smb_krb5_parse_name(context, client_string, &client);
1443 if (ret) {
1444 goto done;
1446 } else {
1447 ret = krb5_cc_get_principal(context, ccache, &client);
1448 if (ret) {
1449 goto done;
1453 #ifdef HAVE_KRB5_GET_RENEWED_CREDS /* MIT */
1455 ret = krb5_get_renewed_creds(context, &creds, client, ccache, CONST_DISCARD(char *, service_string));
1456 if (ret) {
1457 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
1458 goto done;
1461 #elif defined(HAVE_KRB5_GET_KDC_CRED) /* Heimdal */
1463 krb5_kdc_flags flags;
1464 krb5_realm *client_realm = NULL;
1466 ret = krb5_copy_principal(context, client, &creds_in.client);
1467 if (ret) {
1468 goto done;
1471 if (service_string) {
1472 ret = smb_krb5_parse_name(context, service_string, &creds_in.server);
1473 if (ret) {
1474 goto done;
1476 } else {
1477 /* build tgt service by default */
1478 client_realm = krb5_princ_realm(context, creds_in.client);
1479 if (!client_realm) {
1480 ret = ENOMEM;
1481 goto done;
1483 ret = krb5_make_principal(context, &creds_in.server, *client_realm, KRB5_TGS_NAME, *client_realm, NULL);
1484 if (ret) {
1485 goto done;
1489 flags.i = 0;
1490 flags.b.renewable = flags.b.renew = True;
1492 ret = krb5_get_kdc_cred(context, ccache, flags, NULL, NULL, &creds_in, &creds_out);
1493 if (ret) {
1494 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
1495 goto done;
1498 creds = *creds_out;
1500 #else
1501 #error NO_SUITABLE_KRB5_TICKET_RENEW_FUNCTION_AVAILABLE
1502 #endif
1504 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
1505 ret = krb5_cc_initialize(context, ccache, client);
1506 if (ret) {
1507 goto done;
1510 ret = krb5_cc_store_cred(context, ccache, &creds);
1512 if (expire_time) {
1513 *expire_time = (time_t) creds.times.endtime;
1516 done:
1517 krb5_free_cred_contents(context, &creds_in);
1519 if (creds_out) {
1520 krb5_free_creds(context, creds_out);
1521 } else {
1522 krb5_free_cred_contents(context, &creds);
1525 if (client) {
1526 krb5_free_principal(context, client);
1528 if (ccache) {
1529 krb5_cc_close(context, ccache);
1531 if (context) {
1532 krb5_free_context(context);
1535 return ret;
1538 krb5_error_code smb_krb5_free_addresses(krb5_context context, smb_krb5_addresses *addr)
1540 krb5_error_code ret = 0;
1541 if (addr == NULL) {
1542 return ret;
1544 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1545 krb5_free_addresses(context, addr->addrs);
1546 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1547 ret = krb5_free_addresses(context, addr->addrs);
1548 SAFE_FREE(addr->addrs);
1549 #endif
1550 SAFE_FREE(addr);
1551 addr = NULL;
1552 return ret;
1555 krb5_error_code smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses **kerb_addr)
1557 krb5_error_code ret = 0;
1558 nstring buf;
1559 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1560 krb5_address **addrs = NULL;
1561 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1562 krb5_addresses *addrs = NULL;
1563 #endif
1565 *kerb_addr = (smb_krb5_addresses *)SMB_MALLOC(sizeof(smb_krb5_addresses));
1566 if (*kerb_addr == NULL) {
1567 return ENOMEM;
1570 put_name(buf, global_myname(), ' ', 0x20);
1572 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1574 int num_addr = 2;
1576 addrs = (krb5_address **)SMB_MALLOC(sizeof(krb5_address *) * num_addr);
1577 if (addrs == NULL) {
1578 SAFE_FREE(*kerb_addr);
1579 return ENOMEM;
1582 memset(addrs, 0, sizeof(krb5_address *) * num_addr);
1584 addrs[0] = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
1585 if (addrs[0] == NULL) {
1586 SAFE_FREE(addrs);
1587 SAFE_FREE(*kerb_addr);
1588 return ENOMEM;
1591 addrs[0]->magic = KV5M_ADDRESS;
1592 addrs[0]->addrtype = KRB5_ADDR_NETBIOS;
1593 addrs[0]->length = MAX_NETBIOSNAME_LEN;
1594 addrs[0]->contents = (unsigned char *)SMB_MALLOC(addrs[0]->length);
1595 if (addrs[0]->contents == NULL) {
1596 SAFE_FREE(addrs[0]);
1597 SAFE_FREE(addrs);
1598 SAFE_FREE(*kerb_addr);
1599 return ENOMEM;
1602 memcpy(addrs[0]->contents, buf, addrs[0]->length);
1604 addrs[1] = NULL;
1606 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1608 addrs = (krb5_addresses *)SMB_MALLOC(sizeof(krb5_addresses));
1609 if (addrs == NULL) {
1610 SAFE_FREE(*kerb_addr);
1611 return ENOMEM;
1614 memset(addrs, 0, sizeof(krb5_addresses));
1616 addrs->len = 1;
1617 addrs->val = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
1618 if (addrs->val == NULL) {
1619 SAFE_FREE(addrs);
1620 SAFE_FREE(kerb_addr);
1621 return ENOMEM;
1624 addrs->val[0].addr_type = KRB5_ADDR_NETBIOS;
1625 addrs->val[0].address.length = MAX_NETBIOSNAME_LEN;
1626 addrs->val[0].address.data = (unsigned char *)SMB_MALLOC(addrs->val[0].address.length);
1627 if (addrs->val[0].address.data == NULL) {
1628 SAFE_FREE(addrs->val);
1629 SAFE_FREE(addrs);
1630 SAFE_FREE(*kerb_addr);
1631 return ENOMEM;
1634 memcpy(addrs->val[0].address.data, buf, addrs->val[0].address.length);
1636 #else
1637 #error UNKNOWN_KRB5_ADDRESS_FORMAT
1638 #endif
1639 (*kerb_addr)->addrs = addrs;
1641 return ret;
1644 void smb_krb5_free_error(krb5_context context, krb5_error *krberror)
1646 #ifdef HAVE_KRB5_FREE_ERROR_CONTENTS /* Heimdal */
1647 krb5_free_error_contents(context, krberror);
1648 #else /* MIT */
1649 krb5_free_error(context, krberror);
1650 #endif
1653 krb5_error_code handle_krberror_packet(krb5_context context,
1654 krb5_data *packet)
1656 krb5_error_code ret;
1657 bool got_error_code = False;
1659 DEBUG(10,("handle_krberror_packet: got error packet\n"));
1661 #ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR /* Heimdal */
1663 krb5_error krberror;
1665 if ((ret = krb5_rd_error(context, packet, &krberror))) {
1666 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1667 error_message(ret)));
1668 return ret;
1671 if (krberror.e_data == NULL || krberror.e_data->data == NULL) {
1672 ret = (krb5_error_code) krberror.error_code;
1673 got_error_code = True;
1676 smb_krb5_free_error(context, &krberror);
1678 #else /* MIT */
1680 krb5_error *krberror;
1682 if ((ret = krb5_rd_error(context, packet, &krberror))) {
1683 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1684 error_message(ret)));
1685 return ret;
1688 if (krberror->e_data.data == NULL) {
1689 #if defined(ERROR_TABLE_BASE_krb5)
1690 ret = ERROR_TABLE_BASE_krb5 + (krb5_error_code) krberror->error;
1691 #else
1692 ret = (krb5_error_code)krberror->error;
1693 #endif
1694 got_error_code = True;
1696 smb_krb5_free_error(context, krberror);
1698 #endif
1699 if (got_error_code) {
1700 DEBUG(5,("handle_krberror_packet: got KERBERR from kpasswd: %s (%d)\n",
1701 error_message(ret), ret));
1703 return ret;
1706 krb5_error_code smb_krb5_get_init_creds_opt_alloc(krb5_context context,
1707 krb5_get_init_creds_opt **opt)
1709 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
1710 /* Heimdal or modern MIT version */
1711 return krb5_get_init_creds_opt_alloc(context, opt);
1712 #else
1713 /* Historical MIT version */
1714 krb5_get_init_creds_opt *my_opt;
1716 *opt = NULL;
1718 if ((my_opt = SMB_MALLOC_P(krb5_get_init_creds_opt)) == NULL) {
1719 return ENOMEM;
1722 krb5_get_init_creds_opt_init(my_opt);
1724 *opt = my_opt;
1725 return 0;
1726 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC */
1729 void smb_krb5_get_init_creds_opt_free(krb5_context context,
1730 krb5_get_init_creds_opt *opt)
1732 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_FREE
1734 #ifdef KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT
1735 /* Modern MIT or Heimdal version */
1736 krb5_get_init_creds_opt_free(context, opt);
1737 #else
1738 /* Heimdal version */
1739 krb5_get_init_creds_opt_free(opt);
1740 #endif /* KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT */
1742 #else /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1743 /* Historical MIT version */
1744 SAFE_FREE(opt);
1745 opt = NULL;
1746 #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
1749 krb5_enctype smb_get_enctype_from_kt_entry(krb5_keytab_entry *kt_entry)
1751 return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry));
1755 /* caller needs to free etype_s */
1756 krb5_error_code smb_krb5_enctype_to_string(krb5_context context,
1757 krb5_enctype enctype,
1758 char **etype_s)
1760 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
1761 return krb5_enctype_to_string(context, enctype, etype_s); /* Heimdal */
1762 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
1763 char buf[256];
1764 krb5_error_code ret = krb5_enctype_to_string(enctype, buf, 256); /* MIT */
1765 if (ret) {
1766 return ret;
1768 *etype_s = SMB_STRDUP(buf);
1769 if (!*etype_s) {
1770 return ENOMEM;
1772 return ret;
1773 #else
1774 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
1775 #endif
1778 krb5_error_code smb_krb5_mk_error(krb5_context context,
1779 krb5_error_code error_code,
1780 const krb5_principal server,
1781 krb5_data *reply)
1783 #ifdef HAVE_SHORT_KRB5_MK_ERROR_INTERFACE /* MIT */
1785 * The MIT interface is *terrible*.
1786 * We have to construct this ourselves...
1788 krb5_error e;
1790 memset(&e, 0, sizeof(e));
1791 krb5_us_timeofday(context, &e.stime, &e.susec);
1792 e.server = server;
1793 #if defined(krb5_err_base)
1794 e.error = error_code - krb5_err_base;
1795 #elif defined(ERROR_TABLE_BASE_krb5)
1796 e.error = error_code - ERROR_TABLE_BASE_krb5;
1797 #else
1798 e.error = error_code; /* Almost certainly wrong, but what can we do... ? */
1799 #endif
1801 return krb5_mk_error(context, &e, reply);
1802 #else /* Heimdal. */
1803 return krb5_mk_error(context,
1804 error_code,
1805 NULL,
1806 NULL, /* e_data */
1807 NULL,
1808 server,
1809 NULL,
1810 NULL,
1811 reply);
1812 #endif
1815 /**********************************************************************
1816 * Open a krb5 keytab with flags, handles readonly or readwrite access and
1817 * allows to process non-default keytab names.
1818 * @param context krb5_context
1819 * @param keytab_name_req string
1820 * @param write_access bool if writable keytab is required
1821 * @param krb5_keytab pointer to krb5_keytab (close with krb5_kt_close())
1822 * @return krb5_error_code
1823 **********************************************************************/
1825 /* This MAX_NAME_LEN is a constant defined in krb5.h */
1826 #ifndef MAX_KEYTAB_NAME_LEN
1827 #define MAX_KEYTAB_NAME_LEN 1100
1828 #endif
1830 krb5_error_code smb_krb5_open_keytab(krb5_context context,
1831 const char *keytab_name_req,
1832 bool write_access,
1833 krb5_keytab *keytab)
1835 krb5_error_code ret = 0;
1836 TALLOC_CTX *mem_ctx;
1837 char keytab_string[MAX_KEYTAB_NAME_LEN];
1838 char *kt_str = NULL;
1839 bool found_valid_name = False;
1840 const char *pragma = "FILE";
1841 const char *tmp = NULL;
1843 if (!write_access && !keytab_name_req) {
1844 /* caller just wants to read the default keytab readonly, so be it */
1845 return krb5_kt_default(context, keytab);
1848 mem_ctx = talloc_init("smb_krb5_open_keytab");
1849 if (!mem_ctx) {
1850 return ENOMEM;
1853 #ifdef HAVE_WRFILE_KEYTAB
1854 if (write_access) {
1855 pragma = "WRFILE";
1857 #endif
1859 if (keytab_name_req) {
1861 if (strlen(keytab_name_req) > MAX_KEYTAB_NAME_LEN) {
1862 ret = KRB5_CONFIG_NOTENUFSPACE;
1863 goto out;
1866 if ((strncmp(keytab_name_req, "WRFILE:/", 8) == 0) ||
1867 (strncmp(keytab_name_req, "FILE:/", 6) == 0)) {
1868 tmp = keytab_name_req;
1869 goto resolve;
1872 if (keytab_name_req[0] != '/') {
1873 ret = KRB5_KT_BADNAME;
1874 goto out;
1877 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, keytab_name_req);
1878 if (!tmp) {
1879 ret = ENOMEM;
1880 goto out;
1883 goto resolve;
1886 /* we need to handle more complex keytab_strings, like:
1887 * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
1889 ret = krb5_kt_default_name(context, &keytab_string[0], MAX_KEYTAB_NAME_LEN - 2);
1890 if (ret) {
1891 goto out;
1894 DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string));
1896 tmp = talloc_strdup(mem_ctx, keytab_string);
1897 if (!tmp) {
1898 ret = ENOMEM;
1899 goto out;
1902 if (strncmp(tmp, "ANY:", 4) == 0) {
1903 tmp += 4;
1906 memset(&keytab_string, '\0', sizeof(keytab_string));
1908 while (next_token_talloc(mem_ctx, &tmp, &kt_str, ",")) {
1909 if (strncmp(kt_str, "WRFILE:", 7) == 0) {
1910 found_valid_name = True;
1911 tmp = kt_str;
1912 tmp += 7;
1915 if (strncmp(kt_str, "FILE:", 5) == 0) {
1916 found_valid_name = True;
1917 tmp = kt_str;
1918 tmp += 5;
1921 if (tmp[0] == '/') {
1922 /* Treat as a FILE: keytab definition. */
1923 found_valid_name = true;
1926 if (found_valid_name) {
1927 if (tmp[0] != '/') {
1928 ret = KRB5_KT_BADNAME;
1929 goto out;
1932 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, tmp);
1933 if (!tmp) {
1934 ret = ENOMEM;
1935 goto out;
1937 break;
1941 if (!found_valid_name) {
1942 ret = KRB5_KT_UNKNOWN_TYPE;
1943 goto out;
1946 resolve:
1947 DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp));
1948 ret = krb5_kt_resolve(context, tmp, keytab);
1950 out:
1951 TALLOC_FREE(mem_ctx);
1952 return ret;
1955 krb5_error_code smb_krb5_keytab_name(TALLOC_CTX *mem_ctx,
1956 krb5_context context,
1957 krb5_keytab keytab,
1958 const char **keytab_name)
1960 char keytab_string[MAX_KEYTAB_NAME_LEN];
1961 krb5_error_code ret = 0;
1963 ret = krb5_kt_get_name(context, keytab,
1964 keytab_string, MAX_KEYTAB_NAME_LEN - 2);
1965 if (ret) {
1966 return ret;
1969 *keytab_name = talloc_strdup(mem_ctx, keytab_string);
1970 if (!*keytab_name) {
1971 return ENOMEM;
1974 return ret;
1977 #if defined(HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE) && \
1978 defined(HAVE_KRB5_GET_CREDS_OPT_ALLOC) && \
1979 defined(HAVE_KRB5_GET_CREDS)
1980 static krb5_error_code smb_krb5_get_credentials_for_user_opt(krb5_context context,
1981 krb5_ccache ccache,
1982 krb5_principal me,
1983 krb5_principal server,
1984 krb5_principal impersonate_princ,
1985 krb5_creds **out_creds)
1987 krb5_error_code ret;
1988 krb5_get_creds_opt opt;
1990 ret = krb5_get_creds_opt_alloc(context, &opt);
1991 if (ret) {
1992 goto done;
1994 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_FORWARDABLE);
1996 if (impersonate_princ) {
1997 ret = krb5_get_creds_opt_set_impersonate(context, opt,
1998 impersonate_princ);
1999 if (ret) {
2000 goto done;
2004 ret = krb5_get_creds(context, opt, ccache, server, out_creds);
2005 if (ret) {
2006 goto done;
2009 done:
2010 if (opt) {
2011 krb5_get_creds_opt_free(context, opt);
2013 return ret;
2015 #endif /* HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE */
2017 #ifdef HAVE_KRB5_GET_CREDENTIALS_FOR_USER
2018 static krb5_error_code smb_krb5_get_credentials_for_user(krb5_context context,
2019 krb5_ccache ccache,
2020 krb5_principal me,
2021 krb5_principal server,
2022 krb5_principal impersonate_princ,
2023 krb5_creds **out_creds)
2025 krb5_error_code ret;
2026 krb5_creds in_creds;
2028 #if !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER
2029 krb5_error_code KRB5_CALLCONV
2030 krb5_get_credentials_for_user(krb5_context context, krb5_flags options,
2031 krb5_ccache ccache, krb5_creds *in_creds,
2032 krb5_data *subject_cert,
2033 krb5_creds **out_creds);
2034 #endif /* !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER */
2036 ZERO_STRUCT(in_creds);
2038 if (impersonate_princ) {
2040 in_creds.server = me;
2041 in_creds.client = impersonate_princ;
2043 ret = krb5_get_credentials_for_user(context,
2044 0, /* krb5_flags options */
2045 ccache,
2046 &in_creds,
2047 NULL, /* krb5_data *subject_cert */
2048 out_creds);
2049 } else {
2050 in_creds.client = me;
2051 in_creds.server = server;
2053 ret = krb5_get_credentials(context, 0, ccache,
2054 &in_creds, out_creds);
2057 return ret;
2059 #endif /* HAVE_KRB5_GET_CREDENTIALS_FOR_USER */
2062 * smb_krb5_get_credentials
2064 * @brief Get krb5 credentials for a server
2066 * @param[in] context An initialized krb5_context
2067 * @param[in] ccache An initialized krb5_ccache
2068 * @param[in] me The krb5_principal of the caller
2069 * @param[in] server The krb5_principal of the requested service
2070 * @param[in] impersonate_princ The krb5_principal of a user to impersonate as (optional)
2071 * @param[out] out_creds The returned krb5_creds structure
2072 * @return krb5_error_code
2075 krb5_error_code smb_krb5_get_credentials(krb5_context context,
2076 krb5_ccache ccache,
2077 krb5_principal me,
2078 krb5_principal server,
2079 krb5_principal impersonate_princ,
2080 krb5_creds **out_creds)
2082 krb5_error_code ret;
2083 krb5_creds *creds = NULL;
2085 *out_creds = NULL;
2087 if (impersonate_princ) {
2088 #ifdef HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE /* Heimdal */
2089 ret = smb_krb5_get_credentials_for_user_opt(context, ccache, me, server, impersonate_princ, &creds);
2090 #elif defined(HAVE_KRB5_GET_CREDENTIALS_FOR_USER) /* MIT */
2091 ret = smb_krb5_get_credentials_for_user(context, ccache, me, server, impersonate_princ, &creds);
2092 #else
2093 ret = ENOTSUP;
2094 #endif
2095 } else {
2096 krb5_creds in_creds;
2098 ZERO_STRUCT(in_creds);
2100 in_creds.client = me;
2101 in_creds.server = server;
2103 ret = krb5_get_credentials(context, 0, ccache,
2104 &in_creds, &creds);
2106 if (ret) {
2107 goto done;
2110 ret = krb5_cc_store_cred(context, ccache, creds);
2111 if (ret) {
2112 goto done;
2115 if (out_creds) {
2116 *out_creds = creds;
2119 done:
2120 if (creds && ret) {
2121 krb5_free_creds(context, creds);
2124 return ret;
2128 * smb_krb5_get_creds
2130 * @brief Get krb5 credentials for a server
2132 * @param[in] server_s The string name of the service
2133 * @param[in] time_offset The offset to the KDCs time in seconds (optional)
2134 * @param[in] cc The krb5 credential cache string name (optional)
2135 * @param[in] impersonate_princ_s The string principal name to impersonate (optional)
2136 * @param[out] creds_p The returned krb5_creds structure
2137 * @return krb5_error_code
2140 krb5_error_code smb_krb5_get_creds(const char *server_s,
2141 time_t time_offset,
2142 const char *cc,
2143 const char *impersonate_princ_s,
2144 krb5_creds **creds_p)
2146 krb5_error_code ret;
2147 krb5_context context = NULL;
2148 krb5_principal me = NULL;
2149 krb5_principal server = NULL;
2150 krb5_principal impersonate_princ = NULL;
2151 krb5_creds *creds = NULL;
2152 krb5_ccache ccache = NULL;
2154 *creds_p = NULL;
2156 initialize_krb5_error_table();
2157 ret = krb5_init_context(&context);
2158 if (ret) {
2159 goto done;
2162 if (time_offset != 0) {
2163 krb5_set_real_time(context, time(NULL) + time_offset, 0);
2166 ret = krb5_cc_resolve(context, cc ? cc :
2167 krb5_cc_default_name(context), &ccache);
2168 if (ret) {
2169 goto done;
2172 ret = krb5_cc_get_principal(context, ccache, &me);
2173 if (ret) {
2174 goto done;
2177 ret = smb_krb5_parse_name(context, server_s, &server);
2178 if (ret) {
2179 goto done;
2182 if (impersonate_princ_s) {
2183 ret = smb_krb5_parse_name(context, impersonate_princ_s,
2184 &impersonate_princ);
2185 if (ret) {
2186 goto done;
2190 ret = smb_krb5_get_credentials(context, ccache,
2191 me, server, impersonate_princ,
2192 &creds);
2193 if (ret) {
2194 goto done;
2197 ret = krb5_cc_store_cred(context, ccache, creds);
2198 if (ret) {
2199 goto done;
2202 if (creds_p) {
2203 *creds_p = creds;
2206 DEBUG(1,("smb_krb5_get_creds: got ticket for %s\n",
2207 server_s));
2209 if (impersonate_princ_s) {
2210 char *client = NULL;
2212 ret = smb_krb5_unparse_name(talloc_tos(), context, creds->client, &client);
2213 if (ret) {
2214 goto done;
2216 DEBUGADD(1,("smb_krb5_get_creds: using S4U2SELF impersonation as %s\n",
2217 client));
2218 TALLOC_FREE(client);
2221 done:
2222 if (!context) {
2223 return ret;
2226 if (creds && ret) {
2227 krb5_free_creds(context, creds);
2229 if (server) {
2230 krb5_free_principal(context, server);
2232 if (me) {
2233 krb5_free_principal(context, me);
2235 if (impersonate_princ) {
2236 krb5_free_principal(context, impersonate_princ);
2238 if (ccache) {
2239 krb5_cc_close(context, ccache);
2241 krb5_free_context(context);
2243 return ret;
2247 * smb_krb5_principal_get_realm
2249 * @brief Get realm of a principal
2251 * @param[in] context The krb5_context
2252 * @param[in] principal The principal
2253 * @return pointer to the realm
2257 char *smb_krb5_principal_get_realm(krb5_context context,
2258 krb5_principal principal)
2260 #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
2261 return krb5_principal_get_realm(context, principal);
2262 #elif defined(krb5_princ_realm) /* MIT */
2263 krb5_data *realm;
2264 realm = krb5_princ_realm(context, principal);
2265 return (char *)realm->data;
2266 #else
2267 return NULL;
2268 #endif
2271 #else /* HAVE_KRB5 */
2272 /* this saves a few linking headaches */
2273 int cli_krb5_get_ticket(TALLOC_CTX *mem_ctx,
2274 const char *principal, time_t time_offset,
2275 DATA_BLOB *ticket, DATA_BLOB *session_key_krb5,
2276 uint32_t extra_ap_opts,
2277 const char *ccname, time_t *tgs_expire,
2278 const char *impersonate_princ_s)
2280 DEBUG(0,("NO KERBEROS SUPPORT\n"));
2281 return 1;
2284 bool unwrap_pac(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, DATA_BLOB *unwrapped_pac_data)
2286 DEBUG(0,("NO KERBEROS SUPPORT\n"));
2287 return false;
2290 #endif /* HAVE_KRB5 */