krb5wrap: Move mask to the right position.
[Samba.git] / lib / krb5_wrap / krb5_samba.c
blob024e1883b0bfd6ff14b1e7ba6c03e7bd017f43ff
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 "system/filesys.h"
25 #include "krb5_samba.h"
26 #include "lib/util/asn1.h"
28 #ifndef KRB5_AUTHDATA_WIN2K_PAC
29 #define KRB5_AUTHDATA_WIN2K_PAC 128
30 #endif
32 #ifndef KRB5_AUTHDATA_IF_RELEVANT
33 #define KRB5_AUTHDATA_IF_RELEVANT 1
34 #endif
36 #ifdef HAVE_KRB5
38 #define GSSAPI_CHECKSUM 0x8003 /* Checksum type value for Kerberos */
39 #define GSSAPI_BNDLENGTH 16 /* Bind Length (rfc-1964 pg.3) */
40 #define GSSAPI_CHECKSUM_SIZE (4+GSSAPI_BNDLENGTH+4) /* Length of bind length,
41 bind field, flags field. */
42 #define GSS_C_DELEG_FLAG 1
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 #if !defined(SMB_MALLOC)
54 #undef malloc
55 #define SMB_MALLOC(s) malloc((s))
56 #endif
58 #ifndef SMB_STRDUP
59 #define SMB_STRDUP(s) strdup(s)
60 #endif
62 #if !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
64 #if defined(HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES)
66 /* With MIT kerberos, we should use krb5_set_default_tgs_enctypes in preference
67 * to krb5_set_default_tgs_ktypes. See
68 * http://lists.samba.org/archive/samba-technical/2006-July/048271.html
70 * If the MIT libraries are not exporting internal symbols, we will end up in
71 * this branch, which is correct. Otherwise we will continue to use the
72 * internal symbol
74 krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
76 return krb5_set_default_tgs_enctypes(ctx, enc);
79 #elif defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES)
81 /* Heimdal */
82 krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
84 return krb5_set_default_in_tkt_etypes(ctx, enc);
87 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES */
89 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_KTYPES */
91 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
92 /* HEIMDAL */
93 bool setup_kaddr( krb5_address *pkaddr, struct sockaddr_storage *paddr)
95 memset(pkaddr, '\0', sizeof(krb5_address));
96 #if defined(HAVE_IPV6) && defined(KRB5_ADDRESS_INET6)
97 if (paddr->ss_family == AF_INET6) {
98 pkaddr->addr_type = KRB5_ADDRESS_INET6;
99 pkaddr->address.length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr);
100 pkaddr->address.data = (char *)&(((struct sockaddr_in6 *)paddr)->sin6_addr);
101 return true;
103 #endif
104 if (paddr->ss_family == AF_INET) {
105 pkaddr->addr_type = KRB5_ADDRESS_INET;
106 pkaddr->address.length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
107 pkaddr->address.data = (char *)&(((struct sockaddr_in *)paddr)->sin_addr);
108 return true;
110 return false;
112 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
113 /* MIT */
114 bool setup_kaddr( krb5_address *pkaddr, struct sockaddr_storage *paddr)
116 memset(pkaddr, '\0', sizeof(krb5_address));
117 #if defined(HAVE_IPV6) && defined(ADDRTYPE_INET6)
118 if (paddr->ss_family == AF_INET6) {
119 pkaddr->addrtype = ADDRTYPE_INET6;
120 pkaddr->length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr);
121 pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in6 *)paddr)->sin6_addr);
122 return true;
124 #endif
125 if (paddr->ss_family == AF_INET) {
126 pkaddr->addrtype = ADDRTYPE_INET;
127 pkaddr->length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
128 pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in *)paddr)->sin_addr);
129 return true;
131 return false;
133 #else
134 #error UNKNOWN_ADDRTYPE
135 #endif
137 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_C_STRING_TO_KEY)
138 /* MIT */
139 int create_kerberos_key_from_string_direct(krb5_context context,
140 krb5_principal host_princ,
141 krb5_data *password,
142 krb5_keyblock *key,
143 krb5_enctype enctype)
145 int ret = 0;
146 krb5_data salt;
148 ret = krb5_principal2salt(context, host_princ, &salt);
149 if (ret) {
150 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
151 return ret;
153 ret = krb5_c_string_to_key(context, enctype, password, &salt, key);
154 SAFE_FREE(salt.data);
156 return ret;
158 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
159 /* Heimdal */
160 int create_kerberos_key_from_string_direct(krb5_context context,
161 krb5_principal host_princ,
162 krb5_data *password,
163 krb5_keyblock *key,
164 krb5_enctype enctype)
166 int ret;
167 krb5_salt salt;
169 ret = krb5_get_pw_salt(context, host_princ, &salt);
170 if (ret) {
171 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret)));
172 return ret;
175 ret = krb5_string_to_key_salt(context, enctype, (const char *)password->data, salt, key);
176 krb5_free_salt(context, salt);
178 return ret;
180 #else
181 #error UNKNOWN_CREATE_KEY_FUNCTIONS
182 #endif
184 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
185 krb5_error_code get_kerberos_allowed_etypes(krb5_context context,
186 krb5_enctype **enctypes)
188 return krb5_get_permitted_enctypes(context, enctypes);
190 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
191 krb5_error_code get_kerberos_allowed_etypes(krb5_context context,
192 krb5_enctype **enctypes)
194 #ifdef HAVE_KRB5_PDU_NONE_DECL
195 return krb5_get_default_in_tkt_etypes(context, KRB5_PDU_NONE, enctypes);
196 #else
197 return krb5_get_default_in_tkt_etypes(context, enctypes);
198 #endif
200 #else
201 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
202 #endif
204 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
205 krb5_error_code krb5_auth_con_setuseruserkey(krb5_context context,
206 krb5_auth_context auth_context,
207 krb5_keyblock *keyblock)
209 return krb5_auth_con_setkey(context, auth_context, keyblock);
211 #endif
213 bool unwrap_edata_ntstatus(TALLOC_CTX *mem_ctx,
214 DATA_BLOB *edata,
215 DATA_BLOB *edata_out)
217 DATA_BLOB edata_contents;
218 ASN1_DATA *data;
219 int edata_type;
221 if (!edata->length) {
222 return false;
225 data = asn1_init(mem_ctx);
226 if (data == NULL) {
227 return false;
230 asn1_load(data, *edata);
231 asn1_start_tag(data, ASN1_SEQUENCE(0));
232 asn1_start_tag(data, ASN1_CONTEXT(1));
233 asn1_read_Integer(data, &edata_type);
235 if (edata_type != KRB5_PADATA_PW_SALT) {
236 DEBUG(0,("edata is not of required type %d but of type %d\n",
237 KRB5_PADATA_PW_SALT, edata_type));
238 asn1_free(data);
239 return false;
242 asn1_start_tag(data, ASN1_CONTEXT(2));
243 asn1_read_OctetString(data, talloc_tos(), &edata_contents);
244 asn1_end_tag(data);
245 asn1_end_tag(data);
246 asn1_end_tag(data);
247 asn1_free(data);
249 *edata_out = data_blob_talloc(mem_ctx, edata_contents.data, edata_contents.length);
251 data_blob_free(&edata_contents);
253 return true;
257 static bool ads_cleanup_expired_creds(krb5_context context,
258 krb5_ccache ccache,
259 krb5_creds *credsp)
261 krb5_error_code retval;
262 const char *cc_type = krb5_cc_get_type(context, ccache);
264 DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
265 cc_type, krb5_cc_get_name(context, ccache),
266 http_timestring(talloc_tos(), credsp->times.endtime)));
268 /* we will probably need new tickets if the current ones
269 will expire within 10 seconds.
271 if (credsp->times.endtime >= (time(NULL) + 10))
272 return false;
274 /* heimdal won't remove creds from a file ccache, and
275 perhaps we shouldn't anyway, since internally we
276 use memory ccaches, and a FILE one probably means that
277 we're using creds obtained outside of our exectuable
279 if (strequal(cc_type, "FILE")) {
280 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type));
281 return false;
284 retval = krb5_cc_remove_cred(context, ccache, 0, credsp);
285 if (retval) {
286 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
287 error_message(retval)));
288 /* If we have an error in this, we want to display it,
289 but continue as though we deleted it */
291 return true;
294 /* Allocate and setup the auth context into the state we need. */
296 static krb5_error_code setup_auth_context(krb5_context context,
297 krb5_auth_context *auth_context)
299 krb5_error_code retval;
301 retval = krb5_auth_con_init(context, auth_context );
302 if (retval) {
303 DEBUG(1,("krb5_auth_con_init failed (%s)\n",
304 error_message(retval)));
305 return retval;
308 /* Ensure this is an addressless ticket. */
309 retval = krb5_auth_con_setaddrs(context, *auth_context, NULL, NULL);
310 if (retval) {
311 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n",
312 error_message(retval)));
315 return retval;
318 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE)
319 static krb5_error_code create_gss_checksum(krb5_data *in_data, /* [inout] */
320 uint32_t gss_flags)
322 unsigned int orig_length = in_data->length;
323 unsigned int base_cksum_size = GSSAPI_CHECKSUM_SIZE;
324 char *gss_cksum = NULL;
326 if (orig_length) {
327 /* Extra length field for delgated ticket. */
328 base_cksum_size += 4;
331 if ((unsigned int)base_cksum_size + orig_length <
332 (unsigned int)base_cksum_size) {
333 return EINVAL;
336 gss_cksum = (char *)SMB_MALLOC(base_cksum_size + orig_length);
337 if (gss_cksum == NULL) {
338 return ENOMEM;
341 memset(gss_cksum, '\0', base_cksum_size + orig_length);
342 SIVAL(gss_cksum, 0, GSSAPI_BNDLENGTH);
345 * GSS_C_NO_CHANNEL_BINDINGS means 16 zero bytes.
346 * This matches the behavior of heimdal and mit.
348 * And it is needed to work against some closed source
349 * SMB servers.
351 * See bug #7883
353 memset(&gss_cksum[4], 0x00, GSSAPI_BNDLENGTH);
355 SIVAL(gss_cksum, 20, gss_flags);
357 if (orig_length) {
358 SSVAL(gss_cksum, 24, 1); /* The Delegation Option identifier */
359 SSVAL(gss_cksum, 26, orig_length);
360 /* Copy the kerberos KRB_CRED data */
361 memcpy(gss_cksum + 28, in_data->data, orig_length);
362 free(in_data->data);
363 in_data->data = NULL;
364 in_data->length = 0;
366 in_data->data = gss_cksum;
367 in_data->length = base_cksum_size + orig_length;
368 return 0;
370 #endif
372 /**************************************************************
373 krb5_parse_name that takes a UNIX charset.
374 **************************************************************/
376 krb5_error_code smb_krb5_parse_name(krb5_context context,
377 const char *name, /* in unix charset */
378 krb5_principal *principal)
380 krb5_error_code ret;
381 char *utf8_name;
382 size_t converted_size;
383 TALLOC_CTX *frame = talloc_stackframe();
385 if (!push_utf8_talloc(frame, &utf8_name, name, &converted_size)) {
386 talloc_free(frame);
387 return ENOMEM;
390 ret = krb5_parse_name(context, utf8_name, principal);
391 TALLOC_FREE(frame);
392 return ret;
395 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
396 void krb5_free_unparsed_name(krb5_context context, char *val)
398 SAFE_FREE(val);
400 #endif
402 /**************************************************************
403 krb5_parse_name that returns a UNIX charset name. Must
404 be freed with talloc_free() call.
405 **************************************************************/
407 krb5_error_code smb_krb5_unparse_name(TALLOC_CTX *mem_ctx,
408 krb5_context context,
409 krb5_const_principal principal,
410 char **unix_name)
412 krb5_error_code ret;
413 char *utf8_name;
414 size_t converted_size;
416 *unix_name = NULL;
417 ret = krb5_unparse_name(context, principal, &utf8_name);
418 if (ret) {
419 return ret;
422 if (!pull_utf8_talloc(mem_ctx, unix_name, utf8_name, &converted_size)) {
423 krb5_free_unparsed_name(context, utf8_name);
424 return ENOMEM;
426 krb5_free_unparsed_name(context, utf8_name);
427 return 0;
430 krb5_error_code smb_krb5_parse_name_norealm(krb5_context context,
431 const char *name,
432 krb5_principal *principal)
434 /* we are cheating here because parse_name will in fact set the realm.
435 * We don't care as the only caller of smb_krb5_parse_name_norealm
436 * ignores the realm anyway when calling
437 * smb_krb5_principal_compare_any_realm later - Guenther */
439 return smb_krb5_parse_name(context, name, principal);
442 bool smb_krb5_principal_compare_any_realm(krb5_context context,
443 krb5_const_principal princ1,
444 krb5_const_principal princ2)
446 return krb5_principal_compare_any_realm(context, princ1, princ2);
450 we can't use krb5_mk_req because w2k wants the service to be in a particular format
452 static krb5_error_code ads_krb5_mk_req(krb5_context context,
453 krb5_auth_context *auth_context,
454 const krb5_flags ap_req_options,
455 const char *principal,
456 krb5_ccache ccache,
457 krb5_data *outbuf,
458 time_t *expire_time,
459 const char *impersonate_princ_s)
461 krb5_error_code retval;
462 krb5_principal server;
463 krb5_principal impersonate_princ = NULL;
464 krb5_creds * credsp;
465 krb5_creds creds;
466 krb5_data in_data;
467 bool creds_ready = false;
468 int i = 0, maxtries = 3;
470 ZERO_STRUCT(in_data);
472 retval = smb_krb5_parse_name(context, principal, &server);
473 if (retval) {
474 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal));
475 return retval;
478 if (impersonate_princ_s) {
479 retval = smb_krb5_parse_name(context, impersonate_princ_s,
480 &impersonate_princ);
481 if (retval) {
482 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", impersonate_princ_s));
483 goto cleanup_princ;
487 /* obtain ticket & session key */
488 ZERO_STRUCT(creds);
489 if ((retval = krb5_copy_principal(context, server, &creds.server))) {
490 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n",
491 error_message(retval)));
492 goto cleanup_princ;
495 if ((retval = krb5_cc_get_principal(context, ccache, &creds.client))) {
496 /* This can commonly fail on smbd startup with no ticket in the cache.
497 * Report at higher level than 1. */
498 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
499 error_message(retval)));
500 goto cleanup_creds;
503 while (!creds_ready && (i < maxtries)) {
505 if ((retval = smb_krb5_get_credentials(context, ccache,
506 creds.client,
507 creds.server,
508 impersonate_princ,
509 &credsp))) {
510 DEBUG(1,("ads_krb5_mk_req: smb_krb5_get_credentials failed for %s (%s)\n",
511 principal, error_message(retval)));
512 goto cleanup_creds;
515 /* cope with ticket being in the future due to clock skew */
516 if ((unsigned)credsp->times.starttime > time(NULL)) {
517 time_t t = time(NULL);
518 int time_offset =(int)((unsigned)credsp->times.starttime-t);
519 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset));
520 krb5_set_real_time(context, t + time_offset + 1, 0);
523 if (!ads_cleanup_expired_creds(context, ccache, credsp)) {
524 creds_ready = true;
527 i++;
530 DEBUG(10,("ads_krb5_mk_req: Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
531 principal, krb5_cc_get_type(context, ccache), krb5_cc_get_name(context, ccache),
532 http_timestring(talloc_tos(), (unsigned)credsp->times.endtime),
533 (unsigned)credsp->times.endtime));
535 if (expire_time) {
536 *expire_time = (time_t)credsp->times.endtime;
539 /* Allocate the auth_context. */
540 retval = setup_auth_context(context, auth_context);
541 if (retval) {
542 DEBUG(1,("setup_auth_context failed (%s)\n",
543 error_message(retval)));
544 goto cleanup_creds;
547 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE)
549 uint32_t gss_flags = 0;
551 if( credsp->ticket_flags & TKT_FLG_OK_AS_DELEGATE ) {
552 /* Fetch a forwarded TGT from the KDC so that we can hand off a 2nd ticket
553 as part of the kerberos exchange. */
555 DEBUG( 3, ("ads_krb5_mk_req: server marked as OK to delegate to, building forwardable TGT\n") );
557 retval = krb5_auth_con_setuseruserkey(context,
558 *auth_context,
559 &credsp->keyblock );
560 if (retval) {
561 DEBUG(1,("krb5_auth_con_setuseruserkey failed (%s)\n",
562 error_message(retval)));
563 goto cleanup_creds;
566 /* Must use a subkey for forwarded tickets. */
567 retval = krb5_auth_con_setflags(context,
568 *auth_context,
569 KRB5_AUTH_CONTEXT_USE_SUBKEY);
570 if (retval) {
571 DEBUG(1,("krb5_auth_con_setflags failed (%s)\n",
572 error_message(retval)));
573 goto cleanup_creds;
576 retval = krb5_fwd_tgt_creds(context,/* Krb5 context [in] */
577 *auth_context, /* Authentication context [in] */
578 discard_const_p(char, KRB5_TGS_NAME), /* Ticket service name ("krbtgt") [in] */
579 credsp->client, /* Client principal for the tgt [in] */
580 credsp->server, /* Server principal for the tgt [in] */
581 ccache, /* Credential cache to use for storage [in] */
582 1, /* Turn on for "Forwardable ticket" [in] */
583 &in_data ); /* Resulting response [out] */
585 if (retval) {
586 DEBUG( 3, ("krb5_fwd_tgt_creds failed (%s)\n",
587 error_message( retval ) ) );
590 * This is not fatal. Delete the *auth_context and continue
591 * with krb5_mk_req_extended to get a non-forwardable ticket.
594 if (in_data.data) {
595 free( in_data.data );
596 in_data.data = NULL;
597 in_data.length = 0;
599 krb5_auth_con_free(context, *auth_context);
600 *auth_context = NULL;
601 retval = setup_auth_context(context, auth_context);
602 if (retval) {
603 DEBUG(1,("setup_auth_context failed (%s)\n",
604 error_message(retval)));
605 goto cleanup_creds;
607 } else {
608 /* We got a delegated ticket. */
609 gss_flags |= GSS_C_DELEG_FLAG;
613 /* Frees and reallocates in_data into a GSS checksum blob. */
614 retval = create_gss_checksum(&in_data, gss_flags);
615 if (retval) {
616 goto cleanup_data;
619 /* We always want GSS-checksum types. */
620 retval = krb5_auth_con_set_req_cksumtype(context, *auth_context, GSSAPI_CHECKSUM );
621 if (retval) {
622 DEBUG(1,("krb5_auth_con_set_req_cksumtype failed (%s)\n",
623 error_message(retval)));
624 goto cleanup_data;
627 #endif
629 retval = krb5_mk_req_extended(context, auth_context, ap_req_options,
630 &in_data, credsp, outbuf);
631 if (retval) {
632 DEBUG(1,("ads_krb5_mk_req: krb5_mk_req_extended failed (%s)\n",
633 error_message(retval)));
636 #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE)
637 cleanup_data:
638 #endif
640 if (in_data.data) {
641 free( in_data.data );
642 in_data.length = 0;
645 krb5_free_creds(context, credsp);
647 cleanup_creds:
648 krb5_free_cred_contents(context, &creds);
650 cleanup_princ:
651 krb5_free_principal(context, server);
652 if (impersonate_princ) {
653 krb5_free_principal(context, impersonate_princ);
656 return retval;
659 void kerberos_free_data_contents(krb5_context context, krb5_data *pdata)
661 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
662 if (pdata->data) {
663 krb5_free_data_contents(context, pdata);
665 #elif defined(HAVE_KRB5_DATA_FREE)
666 krb5_data_free(context, pdata);
667 #else
668 SAFE_FREE(pdata->data);
669 #endif
673 get a kerberos5 ticket for the given service
675 int cli_krb5_get_ticket(TALLOC_CTX *mem_ctx,
676 const char *principal, time_t time_offset,
677 DATA_BLOB *ticket, DATA_BLOB *session_key_krb5,
678 uint32_t extra_ap_opts, const char *ccname,
679 time_t *tgs_expire,
680 const char *impersonate_princ_s)
683 krb5_error_code retval;
684 krb5_data packet;
685 krb5_context context = NULL;
686 krb5_ccache ccdef = NULL;
687 krb5_auth_context auth_context = NULL;
688 krb5_enctype enc_types[] = {
689 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
690 ENCTYPE_AES256_CTS_HMAC_SHA1_96,
691 #endif
692 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
693 ENCTYPE_AES128_CTS_HMAC_SHA1_96,
694 #endif
695 ENCTYPE_ARCFOUR_HMAC,
696 ENCTYPE_DES_CBC_MD5,
697 ENCTYPE_DES_CBC_CRC,
698 ENCTYPE_NULL};
700 initialize_krb5_error_table();
701 retval = krb5_init_context(&context);
702 if (retval) {
703 DEBUG(1, ("krb5_init_context failed (%s)\n",
704 error_message(retval)));
705 goto failed;
708 if (time_offset != 0) {
709 krb5_set_real_time(context, time(NULL) + time_offset, 0);
712 if ((retval = krb5_cc_resolve(context, ccname ?
713 ccname : krb5_cc_default_name(context), &ccdef))) {
714 DEBUG(1, ("krb5_cc_default failed (%s)\n",
715 error_message(retval)));
716 goto failed;
719 if ((retval = krb5_set_default_tgs_ktypes(context, enc_types))) {
720 DEBUG(1, ("krb5_set_default_tgs_ktypes failed (%s)\n",
721 error_message(retval)));
722 goto failed;
725 retval = ads_krb5_mk_req(context, &auth_context,
726 AP_OPTS_USE_SUBKEY | (krb5_flags)extra_ap_opts,
727 principal, ccdef, &packet,
728 tgs_expire, impersonate_princ_s);
729 if (retval) {
730 goto failed;
733 get_krb5_smb_session_key(mem_ctx, context, auth_context,
734 session_key_krb5, false);
736 *ticket = data_blob_talloc(mem_ctx, packet.data, packet.length);
738 kerberos_free_data_contents(context, &packet);
740 failed:
742 if (context) {
743 if (ccdef)
744 krb5_cc_close(context, ccdef);
745 if (auth_context)
746 krb5_auth_con_free(context, auth_context);
747 krb5_free_context(context);
750 return retval;
753 bool get_krb5_smb_session_key(TALLOC_CTX *mem_ctx,
754 krb5_context context,
755 krb5_auth_context auth_context,
756 DATA_BLOB *session_key, bool remote)
758 krb5_keyblock *skey = NULL;
759 krb5_error_code err = 0;
760 bool ret = false;
762 if (remote) {
763 err = krb5_auth_con_getremotesubkey(context,
764 auth_context, &skey);
765 } else {
766 err = krb5_auth_con_getlocalsubkey(context,
767 auth_context, &skey);
770 if (err || skey == NULL) {
771 DEBUG(10, ("KRB5 error getting session key %d\n", err));
772 goto done;
775 DEBUG(10, ("Got KRB5 session key of length %d\n",
776 (int)KRB5_KEY_LENGTH(skey)));
778 *session_key = data_blob_talloc(mem_ctx,
779 KRB5_KEY_DATA(skey),
780 KRB5_KEY_LENGTH(skey));
781 dump_data_pw("KRB5 Session Key:\n",
782 session_key->data,
783 session_key->length);
785 ret = true;
787 done:
788 if (skey) {
789 krb5_free_keyblock(context, skey);
792 return ret;
796 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
797 const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i );
799 const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i )
801 static krb5_data kdata;
803 kdata.data = discard_const_p(char, krb5_principal_get_comp_string(context, principal, i));
804 kdata.length = strlen((const char *)kdata.data);
805 return &kdata;
807 #endif
809 /* Prototypes */
811 krb5_error_code smb_krb5_renew_ticket(const char *ccache_string, /* FILE:/tmp/krb5cc_0 */
812 const char *client_string, /* gd@BER.SUSE.DE */
813 const char *service_string, /* krbtgt/BER.SUSE.DE@BER.SUSE.DE */
814 time_t *expire_time)
816 krb5_error_code ret;
817 krb5_context context = NULL;
818 krb5_ccache ccache = NULL;
819 krb5_principal client = NULL;
820 krb5_creds creds, creds_in;
822 ZERO_STRUCT(creds);
823 ZERO_STRUCT(creds_in);
825 initialize_krb5_error_table();
826 ret = krb5_init_context(&context);
827 if (ret) {
828 goto done;
831 if (!ccache_string) {
832 ccache_string = krb5_cc_default_name(context);
835 if (!ccache_string) {
836 ret = EINVAL;
837 goto done;
840 DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string));
842 /* FIXME: we should not fall back to defaults */
843 ret = krb5_cc_resolve(context, discard_const_p(char, ccache_string), &ccache);
844 if (ret) {
845 goto done;
848 if (client_string) {
849 ret = smb_krb5_parse_name(context, client_string, &client);
850 if (ret) {
851 goto done;
853 } else {
854 ret = krb5_cc_get_principal(context, ccache, &client);
855 if (ret) {
856 goto done;
860 ret = krb5_get_renewed_creds(context, &creds, client, ccache, discard_const_p(char, service_string));
861 if (ret) {
862 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
863 goto done;
866 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
867 ret = krb5_cc_initialize(context, ccache, client);
868 if (ret) {
869 goto done;
872 ret = krb5_cc_store_cred(context, ccache, &creds);
874 if (expire_time) {
875 *expire_time = (time_t) creds.times.endtime;
878 done:
879 krb5_free_cred_contents(context, &creds_in);
880 krb5_free_cred_contents(context, &creds);
882 if (client) {
883 krb5_free_principal(context, client);
885 if (ccache) {
886 krb5_cc_close(context, ccache);
888 if (context) {
889 krb5_free_context(context);
892 return ret;
895 krb5_error_code smb_krb5_free_addresses(krb5_context context, smb_krb5_addresses *addr)
897 krb5_error_code ret = 0;
898 if (addr == NULL) {
899 return ret;
901 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
902 krb5_free_addresses(context, addr->addrs);
903 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
904 ret = krb5_free_addresses(context, addr->addrs);
905 SAFE_FREE(addr->addrs);
906 #endif
907 SAFE_FREE(addr);
908 addr = NULL;
909 return ret;
912 #define MAX_NETBIOSNAME_LEN 16
913 krb5_error_code smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses **kerb_addr,
914 const char *netbios_name)
916 krb5_error_code ret = 0;
917 char buf[MAX_NETBIOSNAME_LEN];
918 int len;
919 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
920 krb5_address **addrs = NULL;
921 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
922 krb5_addresses *addrs = NULL;
923 #endif
925 *kerb_addr = (smb_krb5_addresses *)SMB_MALLOC(sizeof(smb_krb5_addresses));
926 if (*kerb_addr == NULL) {
927 return ENOMEM;
930 /* temporarily duplicate put_name() code here to avoid dependency
931 * issues for a 5 lines function */
932 len = strlen(netbios_name);
933 memcpy(buf, netbios_name,
934 (len < MAX_NETBIOSNAME_LEN) ? len : MAX_NETBIOSNAME_LEN - 1);
935 if (len < MAX_NETBIOSNAME_LEN - 1) {
936 memset(buf + len, ' ', MAX_NETBIOSNAME_LEN - 1 - len);
938 buf[MAX_NETBIOSNAME_LEN - 1] = 0x20;
940 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
942 int num_addr = 2;
944 addrs = (krb5_address **)SMB_MALLOC(sizeof(krb5_address *) * num_addr);
945 if (addrs == NULL) {
946 SAFE_FREE(*kerb_addr);
947 return ENOMEM;
950 memset(addrs, 0, sizeof(krb5_address *) * num_addr);
952 addrs[0] = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
953 if (addrs[0] == NULL) {
954 SAFE_FREE(addrs);
955 SAFE_FREE(*kerb_addr);
956 return ENOMEM;
959 addrs[0]->magic = KV5M_ADDRESS;
960 addrs[0]->addrtype = KRB5_ADDR_NETBIOS;
961 addrs[0]->length = MAX_NETBIOSNAME_LEN;
962 addrs[0]->contents = (unsigned char *)SMB_MALLOC(addrs[0]->length);
963 if (addrs[0]->contents == NULL) {
964 SAFE_FREE(addrs[0]);
965 SAFE_FREE(addrs);
966 SAFE_FREE(*kerb_addr);
967 return ENOMEM;
970 memcpy(addrs[0]->contents, buf, addrs[0]->length);
972 addrs[1] = NULL;
974 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
976 addrs = (krb5_addresses *)SMB_MALLOC(sizeof(krb5_addresses));
977 if (addrs == NULL) {
978 SAFE_FREE(*kerb_addr);
979 return ENOMEM;
982 memset(addrs, 0, sizeof(krb5_addresses));
984 addrs->len = 1;
985 addrs->val = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
986 if (addrs->val == NULL) {
987 SAFE_FREE(addrs);
988 SAFE_FREE(kerb_addr);
989 return ENOMEM;
992 addrs->val[0].addr_type = KRB5_ADDR_NETBIOS;
993 addrs->val[0].address.length = MAX_NETBIOSNAME_LEN;
994 addrs->val[0].address.data = (unsigned char *)SMB_MALLOC(addrs->val[0].address.length);
995 if (addrs->val[0].address.data == NULL) {
996 SAFE_FREE(addrs->val);
997 SAFE_FREE(addrs);
998 SAFE_FREE(*kerb_addr);
999 return ENOMEM;
1002 memcpy(addrs->val[0].address.data, buf, addrs->val[0].address.length);
1004 #else
1005 #error UNKNOWN_KRB5_ADDRESS_FORMAT
1006 #endif
1007 (*kerb_addr)->addrs = addrs;
1009 return ret;
1012 void smb_krb5_free_error(krb5_context context, krb5_error *krberror)
1014 #ifdef HAVE_KRB5_FREE_ERROR_CONTENTS /* Heimdal */
1015 krb5_free_error_contents(context, krberror);
1016 #else /* MIT */
1017 krb5_free_error(context, krberror);
1018 #endif
1021 krb5_error_code handle_krberror_packet(krb5_context context,
1022 krb5_data *packet)
1024 krb5_error_code ret;
1025 bool got_error_code = false;
1027 DEBUG(10,("handle_krberror_packet: got error packet\n"));
1029 #ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR /* Heimdal */
1031 krb5_error krberror;
1033 if ((ret = krb5_rd_error(context, packet, &krberror))) {
1034 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1035 error_message(ret)));
1036 return ret;
1039 if (krberror.e_data == NULL || krberror.e_data->data == NULL) {
1040 ret = (krb5_error_code) krberror.error_code;
1041 got_error_code = true;
1044 smb_krb5_free_error(context, &krberror);
1046 #else /* MIT */
1048 krb5_error *krberror;
1050 if ((ret = krb5_rd_error(context, packet, &krberror))) {
1051 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1052 error_message(ret)));
1053 return ret;
1056 if (krberror->e_data.data == NULL) {
1057 #if defined(ERROR_TABLE_BASE_krb5)
1058 ret = ERROR_TABLE_BASE_krb5 + (krb5_error_code) krberror->error;
1059 #else
1060 ret = (krb5_error_code)krberror->error;
1061 #endif
1062 got_error_code = true;
1064 smb_krb5_free_error(context, krberror);
1066 #endif
1067 if (got_error_code) {
1068 DEBUG(5,("handle_krberror_packet: got KERBERR from kpasswd: %s (%d)\n",
1069 error_message(ret), ret));
1071 return ret;
1074 krb5_error_code smb_krb5_get_init_creds_opt_alloc(krb5_context context,
1075 krb5_get_init_creds_opt **opt)
1077 /* Heimdal or modern MIT version */
1078 return krb5_get_init_creds_opt_alloc(context, opt);
1081 void smb_krb5_get_init_creds_opt_free(krb5_context context,
1082 krb5_get_init_creds_opt *opt)
1084 /* Modern MIT or Heimdal version */
1085 krb5_get_init_creds_opt_free(context, opt);
1088 krb5_enctype smb_get_enctype_from_kt_entry(krb5_keytab_entry *kt_entry)
1090 return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry));
1093 krb5_error_code smb_krb5_kt_free_entry(krb5_context context,
1094 krb5_keytab_entry *kt_entry)
1096 /* Try krb5_free_keytab_entry_contents first, since
1097 * MIT Kerberos >= 1.7 has both krb5_free_keytab_entry_contents and
1098 * krb5_kt_free_entry but only has a prototype for the first, while the
1099 * second is considered private.
1101 #if defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
1102 return krb5_free_keytab_entry_contents(context, kt_entry);
1103 #elif defined(HAVE_KRB5_KT_FREE_ENTRY)
1104 return krb5_kt_free_entry(context, kt_entry);
1105 #else
1106 #error UNKNOWN_KT_FREE_FUNCTION
1107 #endif
1111 /* caller needs to free etype_s */
1112 krb5_error_code smb_krb5_enctype_to_string(krb5_context context,
1113 krb5_enctype enctype,
1114 char **etype_s)
1116 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
1117 return krb5_enctype_to_string(context, enctype, etype_s); /* Heimdal */
1118 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
1119 char buf[256];
1120 krb5_error_code ret = krb5_enctype_to_string(enctype, buf, 256); /* MIT */
1121 if (ret) {
1122 return ret;
1124 *etype_s = SMB_STRDUP(buf);
1125 if (!*etype_s) {
1126 return ENOMEM;
1128 return ret;
1129 #else
1130 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
1131 #endif
1134 /**********************************************************************
1135 * Open a krb5 keytab with flags, handles readonly or readwrite access and
1136 * allows to process non-default keytab names.
1137 * @param context krb5_context
1138 * @param keytab_name_req string
1139 * @param write_access bool if writable keytab is required
1140 * @param krb5_keytab pointer to krb5_keytab (close with krb5_kt_close())
1141 * @return krb5_error_code
1142 **********************************************************************/
1144 /* This MAX_NAME_LEN is a constant defined in krb5.h */
1145 #ifndef MAX_KEYTAB_NAME_LEN
1146 #define MAX_KEYTAB_NAME_LEN 1100
1147 #endif
1149 krb5_error_code smb_krb5_open_keytab(krb5_context context,
1150 const char *keytab_name_req,
1151 bool write_access,
1152 krb5_keytab *keytab)
1154 krb5_error_code ret = 0;
1155 TALLOC_CTX *mem_ctx;
1156 char keytab_string[MAX_KEYTAB_NAME_LEN];
1157 char *kt_str = NULL;
1158 bool found_valid_name = false;
1159 const char *pragma = "FILE";
1160 const char *tmp = NULL;
1162 if (!write_access && !keytab_name_req) {
1163 /* caller just wants to read the default keytab readonly, so be it */
1164 return krb5_kt_default(context, keytab);
1167 mem_ctx = talloc_init("smb_krb5_open_keytab");
1168 if (!mem_ctx) {
1169 return ENOMEM;
1172 #ifdef HAVE_WRFILE_KEYTAB
1173 if (write_access) {
1174 pragma = "WRFILE";
1176 #endif
1178 if (keytab_name_req) {
1180 if (strlen(keytab_name_req) > MAX_KEYTAB_NAME_LEN) {
1181 ret = KRB5_CONFIG_NOTENUFSPACE;
1182 goto out;
1185 if ((strncmp(keytab_name_req, "WRFILE:/", 8) == 0) ||
1186 (strncmp(keytab_name_req, "FILE:/", 6) == 0)) {
1187 tmp = keytab_name_req;
1188 goto resolve;
1191 if (keytab_name_req[0] != '/') {
1192 ret = KRB5_KT_BADNAME;
1193 goto out;
1196 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, keytab_name_req);
1197 if (!tmp) {
1198 ret = ENOMEM;
1199 goto out;
1202 goto resolve;
1205 /* we need to handle more complex keytab_strings, like:
1206 * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
1208 ret = krb5_kt_default_name(context, &keytab_string[0], MAX_KEYTAB_NAME_LEN - 2);
1209 if (ret) {
1210 goto out;
1213 DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string));
1215 tmp = talloc_strdup(mem_ctx, keytab_string);
1216 if (!tmp) {
1217 ret = ENOMEM;
1218 goto out;
1221 if (strncmp(tmp, "ANY:", 4) == 0) {
1222 tmp += 4;
1225 memset(&keytab_string, '\0', sizeof(keytab_string));
1227 while (next_token_talloc(mem_ctx, &tmp, &kt_str, ",")) {
1228 if (strncmp(kt_str, "WRFILE:", 7) == 0) {
1229 found_valid_name = true;
1230 tmp = kt_str;
1231 tmp += 7;
1234 if (strncmp(kt_str, "FILE:", 5) == 0) {
1235 found_valid_name = true;
1236 tmp = kt_str;
1237 tmp += 5;
1240 if (tmp[0] == '/') {
1241 /* Treat as a FILE: keytab definition. */
1242 found_valid_name = true;
1245 if (found_valid_name) {
1246 if (tmp[0] != '/') {
1247 ret = KRB5_KT_BADNAME;
1248 goto out;
1251 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, tmp);
1252 if (!tmp) {
1253 ret = ENOMEM;
1254 goto out;
1256 break;
1260 if (!found_valid_name) {
1261 ret = KRB5_KT_UNKNOWN_TYPE;
1262 goto out;
1265 resolve:
1266 DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp));
1267 ret = krb5_kt_resolve(context, tmp, keytab);
1269 out:
1270 TALLOC_FREE(mem_ctx);
1271 return ret;
1274 krb5_error_code smb_krb5_keytab_name(TALLOC_CTX *mem_ctx,
1275 krb5_context context,
1276 krb5_keytab keytab,
1277 const char **keytab_name)
1279 char keytab_string[MAX_KEYTAB_NAME_LEN];
1280 krb5_error_code ret = 0;
1282 ret = krb5_kt_get_name(context, keytab,
1283 keytab_string, MAX_KEYTAB_NAME_LEN - 2);
1284 if (ret) {
1285 return ret;
1288 *keytab_name = talloc_strdup(mem_ctx, keytab_string);
1289 if (!*keytab_name) {
1290 return ENOMEM;
1293 return ret;
1296 #if defined(HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE) && \
1297 defined(HAVE_KRB5_GET_CREDS_OPT_ALLOC) && \
1298 defined(HAVE_KRB5_GET_CREDS)
1299 static krb5_error_code smb_krb5_get_credentials_for_user_opt(krb5_context context,
1300 krb5_ccache ccache,
1301 krb5_principal me,
1302 krb5_principal server,
1303 krb5_principal impersonate_princ,
1304 krb5_creds **out_creds)
1306 krb5_error_code ret;
1307 krb5_get_creds_opt opt;
1309 ret = krb5_get_creds_opt_alloc(context, &opt);
1310 if (ret) {
1311 goto done;
1313 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_FORWARDABLE);
1315 if (impersonate_princ) {
1316 ret = krb5_get_creds_opt_set_impersonate(context, opt,
1317 impersonate_princ);
1318 if (ret) {
1319 goto done;
1323 ret = krb5_get_creds(context, opt, ccache, server, out_creds);
1324 if (ret) {
1325 goto done;
1328 done:
1329 if (opt) {
1330 krb5_get_creds_opt_free(context, opt);
1332 return ret;
1334 #endif /* HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE */
1336 #ifdef HAVE_KRB5_GET_CREDENTIALS_FOR_USER
1337 static krb5_error_code smb_krb5_get_credentials_for_user(krb5_context context,
1338 krb5_ccache ccache,
1339 krb5_principal me,
1340 krb5_principal server,
1341 krb5_principal impersonate_princ,
1342 krb5_creds **out_creds)
1344 krb5_error_code ret;
1345 krb5_creds in_creds;
1347 #if !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER
1348 krb5_error_code KRB5_CALLCONV
1349 krb5_get_credentials_for_user(krb5_context context, krb5_flags options,
1350 krb5_ccache ccache, krb5_creds *in_creds,
1351 krb5_data *subject_cert,
1352 krb5_creds **out_creds);
1353 #endif /* !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER */
1355 ZERO_STRUCT(in_creds);
1357 if (impersonate_princ) {
1359 in_creds.server = me;
1360 in_creds.client = impersonate_princ;
1362 ret = krb5_get_credentials_for_user(context,
1363 0, /* krb5_flags options */
1364 ccache,
1365 &in_creds,
1366 NULL, /* krb5_data *subject_cert */
1367 out_creds);
1368 } else {
1369 in_creds.client = me;
1370 in_creds.server = server;
1372 ret = krb5_get_credentials(context, 0, ccache,
1373 &in_creds, out_creds);
1376 return ret;
1378 #endif /* HAVE_KRB5_GET_CREDENTIALS_FOR_USER */
1381 * smb_krb5_get_credentials
1383 * @brief Get krb5 credentials for a server
1385 * @param[in] context An initialized krb5_context
1386 * @param[in] ccache An initialized krb5_ccache
1387 * @param[in] me The krb5_principal of the caller
1388 * @param[in] server The krb5_principal of the requested service
1389 * @param[in] impersonate_princ The krb5_principal of a user to impersonate as (optional)
1390 * @param[out] out_creds The returned krb5_creds structure
1391 * @return krb5_error_code
1394 krb5_error_code smb_krb5_get_credentials(krb5_context context,
1395 krb5_ccache ccache,
1396 krb5_principal me,
1397 krb5_principal server,
1398 krb5_principal impersonate_princ,
1399 krb5_creds **out_creds)
1401 krb5_error_code ret;
1402 krb5_creds *creds = NULL;
1404 if (out_creds != NULL) {
1405 *out_creds = NULL;
1408 if (impersonate_princ) {
1409 #ifdef HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE /* Heimdal */
1410 ret = smb_krb5_get_credentials_for_user_opt(context, ccache, me, server, impersonate_princ, &creds);
1411 #elif defined(HAVE_KRB5_GET_CREDENTIALS_FOR_USER) /* MIT */
1412 ret = smb_krb5_get_credentials_for_user(context, ccache, me, server, impersonate_princ, &creds);
1413 #else
1414 ret = ENOTSUP;
1415 #endif
1416 } else {
1417 krb5_creds in_creds;
1419 ZERO_STRUCT(in_creds);
1421 in_creds.client = me;
1422 in_creds.server = server;
1424 ret = krb5_get_credentials(context, 0, ccache,
1425 &in_creds, &creds);
1427 if (ret) {
1428 goto done;
1431 if (out_creds) {
1432 *out_creds = creds;
1435 done:
1436 if (creds && ret) {
1437 krb5_free_creds(context, creds);
1440 return ret;
1443 krb5_error_code smb_krb5_keyblock_init_contents(krb5_context context,
1444 krb5_enctype enctype,
1445 const void *data,
1446 size_t length,
1447 krb5_keyblock *key)
1449 #if defined(HAVE_KRB5_KEYBLOCK_INIT)
1450 return krb5_keyblock_init(context, enctype, data, length, key);
1451 #else
1452 memset(key, 0, sizeof(krb5_keyblock));
1453 KRB5_KEY_DATA(key) = SMB_MALLOC(length);
1454 if (NULL == KRB5_KEY_DATA(key)) {
1455 return ENOMEM;
1457 memcpy(KRB5_KEY_DATA(key), data, length);
1458 KRB5_KEY_LENGTH(key) = length;
1459 KRB5_KEY_TYPE(key) = enctype;
1460 return 0;
1461 #endif
1465 simulate a kinit, putting the tgt in the given credentials cache.
1466 Orignally by remus@snapserver.com
1468 This version is built to use a keyblock, rather than needing the
1469 original password.
1471 The impersonate_principal is the principal if NULL, or the principal
1472 to impersonate
1474 The target_service defaults to the krbtgt if NULL, but could be
1475 kpasswd/realm or the local service (if we are doing s4u2self)
1477 krb5_error_code kerberos_kinit_keyblock_cc(krb5_context ctx, krb5_ccache cc,
1478 krb5_principal principal,
1479 krb5_keyblock *keyblock,
1480 const char *target_service,
1481 krb5_get_init_creds_opt *krb_options,
1482 time_t *expire_time,
1483 time_t *kdc_time)
1485 krb5_error_code code = 0;
1486 krb5_creds my_creds;
1488 #if defined(HAVE_KRB5_GET_INIT_CREDS_KEYBLOCK)
1489 code = krb5_get_init_creds_keyblock(ctx, &my_creds, principal,
1490 keyblock, 0, target_service,
1491 krb_options);
1492 #elif defined(HAVE_KRB5_GET_INIT_CREDS_KEYTAB)
1494 #define SMB_CREDS_KEYTAB "MEMORY:tmp_smb_creds_XXXXXX"
1495 char tmp_name[sizeof(SMB_CREDS_KEYTAB)];
1496 krb5_keytab_entry entry;
1497 krb5_keytab keytab;
1498 mode_t mask;
1500 memset(&entry, 0, sizeof(entry));
1501 entry.principal = principal;
1502 *(KRB5_KT_KEY(&entry)) = *keyblock;
1504 memcpy(tmp_name, SMB_CREDS_KEYTAB, sizeof(SMB_CREDS_KEYTAB));
1505 mask = umask(S_IRWXO | S_IRWXG);
1506 mktemp(tmp_name);
1507 umask(mask);
1508 if (tmp_name[0] == 0) {
1509 return KRB5_KT_BADNAME;
1511 code = krb5_kt_resolve(ctx, tmp_name, &keytab);
1512 if (code) {
1513 return code;
1516 code = krb5_kt_add_entry(ctx, keytab, &entry);
1517 if (code) {
1518 (void)krb5_kt_close(ctx, keytab);
1519 goto done;
1522 code = krb5_get_init_creds_keytab(ctx, &my_creds, principal,
1523 keytab, 0, target_service,
1524 krb_options);
1525 (void)krb5_kt_close(ctx, keytab);
1527 #else
1528 #error krb5_get_init_creds_keyblock not available!
1529 #endif
1530 if (code) {
1531 return code;
1534 code = krb5_cc_initialize(ctx, cc, principal);
1535 if (code) {
1536 goto done;
1539 code = krb5_cc_store_cred(ctx, cc, &my_creds);
1540 if (code) {
1541 goto done;
1544 if (expire_time) {
1545 *expire_time = (time_t) my_creds.times.endtime;
1548 if (kdc_time) {
1549 *kdc_time = (time_t) my_creds.times.starttime;
1552 code = 0;
1553 done:
1554 krb5_free_cred_contents(ctx, &my_creds);
1555 return code;
1558 krb5_error_code kerberos_kinit_password_cc(krb5_context ctx, krb5_ccache cc,
1559 krb5_principal principal,
1560 const char *password,
1561 const char *target_service,
1562 krb5_get_init_creds_opt *krb_options,
1563 time_t *expire_time,
1564 time_t *kdc_time)
1566 krb5_error_code code = 0;
1567 krb5_creds my_creds;
1569 code = krb5_get_init_creds_password(ctx, &my_creds, principal,
1570 password, NULL, NULL, 0,
1571 target_service, krb_options);
1572 if (code) {
1573 return code;
1576 code = krb5_cc_initialize(ctx, cc, principal);
1577 if (code) {
1578 goto done;
1581 code = krb5_cc_store_cred(ctx, cc, &my_creds);
1582 if (code) {
1583 goto done;
1586 if (expire_time) {
1587 *expire_time = (time_t) my_creds.times.endtime;
1590 if (kdc_time) {
1591 *kdc_time = (time_t) my_creds.times.starttime;
1594 code = 0;
1595 done:
1596 krb5_free_cred_contents(ctx, &my_creds);
1597 return code;
1600 #ifdef SAMBA4_USES_HEIMDAL
1602 simulate a kinit, putting the tgt in the given credentials cache.
1603 Orignally by remus@snapserver.com
1605 The impersonate_principal is the principal
1607 The self_service, should be the local service (for S4U2Self if
1608 impersonate_principal is given).
1610 The target_service defaults to the krbtgt if NULL, but could be
1611 kpasswd/realm or a remote service (for S4U2Proxy)
1614 krb5_error_code kerberos_kinit_s4u2_cc(krb5_context ctx,
1615 krb5_ccache store_cc,
1616 krb5_principal init_principal,
1617 const char *init_password,
1618 krb5_principal impersonate_principal,
1619 const char *self_service,
1620 const char *target_service,
1621 krb5_get_init_creds_opt *krb_options,
1622 time_t *expire_time,
1623 time_t *kdc_time)
1625 krb5_error_code code = 0;
1626 krb5_get_creds_opt options;
1627 krb5_principal store_principal;
1628 krb5_creds store_creds;
1629 krb5_creds *s4u2self_creds;
1630 Ticket s4u2self_ticket;
1631 size_t s4u2self_ticketlen;
1632 krb5_creds *s4u2proxy_creds;
1633 krb5_principal self_princ;
1634 bool s4u2proxy;
1635 krb5_principal target_princ;
1636 krb5_ccache tmp_cc;
1637 const char *self_realm;
1638 krb5_principal blacklist_principal = NULL;
1639 krb5_principal whitelist_principal = NULL;
1641 code = krb5_get_init_creds_password(ctx, &store_creds,
1642 init_principal,
1643 init_password,
1644 NULL, NULL,
1646 NULL,
1647 krb_options);
1648 if (code != 0) {
1649 return code;
1652 store_principal = init_principal;
1655 * We are trying S4U2Self now:
1657 * As we do not want to expose our TGT in the
1658 * krb5_ccache, which is also holds the impersonated creds.
1660 * Some low level krb5/gssapi function might use the TGT
1661 * identity and let the client act as our machine account.
1663 * We need to avoid that and use a temporary krb5_ccache
1664 * in order to pass our TGT to the krb5_get_creds() function.
1666 code = krb5_cc_new_unique(ctx, NULL, NULL, &tmp_cc);
1667 if (code != 0) {
1668 krb5_free_cred_contents(ctx, &store_creds);
1669 return code;
1672 code = krb5_cc_initialize(ctx, tmp_cc, store_creds.client);
1673 if (code != 0) {
1674 krb5_cc_destroy(ctx, tmp_cc);
1675 krb5_free_cred_contents(ctx, &store_creds);
1676 return code;
1679 code = krb5_cc_store_cred(ctx, tmp_cc, &store_creds);
1680 if (code != 0) {
1681 krb5_free_cred_contents(ctx, &store_creds);
1682 krb5_cc_destroy(ctx, tmp_cc);
1683 return code;
1687 * we need to remember the client principal of our
1688 * TGT and make sure the KDC does not return this
1689 * in the impersonated tickets. This can happen
1690 * if the KDC does not support S4U2Self and S4U2Proxy.
1692 blacklist_principal = store_creds.client;
1693 store_creds.client = NULL;
1694 krb5_free_cred_contents(ctx, &store_creds);
1697 * Check if we also need S4U2Proxy or if S4U2Self is
1698 * enough in order to get a ticket for the target.
1700 if (target_service == NULL) {
1701 s4u2proxy = false;
1702 } else if (strcmp(target_service, self_service) == 0) {
1703 s4u2proxy = false;
1704 } else {
1705 s4u2proxy = true;
1709 * For S4U2Self we need our own service principal,
1710 * which belongs to our own realm (available on
1711 * our client principal).
1713 self_realm = krb5_principal_get_realm(ctx, init_principal);
1715 code = krb5_parse_name(ctx, self_service, &self_princ);
1716 if (code != 0) {
1717 krb5_free_principal(ctx, blacklist_principal);
1718 krb5_cc_destroy(ctx, tmp_cc);
1719 return code;
1722 code = krb5_principal_set_realm(ctx, self_princ, self_realm);
1723 if (code != 0) {
1724 krb5_free_principal(ctx, blacklist_principal);
1725 krb5_free_principal(ctx, self_princ);
1726 krb5_cc_destroy(ctx, tmp_cc);
1727 return code;
1730 code = krb5_get_creds_opt_alloc(ctx, &options);
1731 if (code != 0) {
1732 krb5_free_principal(ctx, blacklist_principal);
1733 krb5_free_principal(ctx, self_princ);
1734 krb5_cc_destroy(ctx, tmp_cc);
1735 return code;
1738 if (s4u2proxy) {
1740 * If we want S4U2Proxy, we need the forwardable flag
1741 * on the S4U2Self ticket.
1743 krb5_get_creds_opt_set_options(ctx, options, KRB5_GC_FORWARDABLE);
1746 code = krb5_get_creds_opt_set_impersonate(ctx, options,
1747 impersonate_principal);
1748 if (code != 0) {
1749 krb5_get_creds_opt_free(ctx, options);
1750 krb5_free_principal(ctx, blacklist_principal);
1751 krb5_free_principal(ctx, self_princ);
1752 krb5_cc_destroy(ctx, tmp_cc);
1753 return code;
1756 code = krb5_get_creds(ctx, options, tmp_cc,
1757 self_princ, &s4u2self_creds);
1758 krb5_get_creds_opt_free(ctx, options);
1759 krb5_free_principal(ctx, self_princ);
1760 if (code != 0) {
1761 krb5_free_principal(ctx, blacklist_principal);
1762 krb5_cc_destroy(ctx, tmp_cc);
1763 return code;
1766 if (!s4u2proxy) {
1767 krb5_cc_destroy(ctx, tmp_cc);
1770 * Now make sure we store the impersonated principal
1771 * and creds instead of the TGT related stuff
1772 * in the krb5_ccache of the caller.
1774 code = krb5_copy_creds_contents(ctx, s4u2self_creds,
1775 &store_creds);
1776 krb5_free_creds(ctx, s4u2self_creds);
1777 if (code != 0) {
1778 return code;
1782 * It's important to store the principal the KDC
1783 * returned, as otherwise the caller would not find
1784 * the S4U2Self ticket in the krb5_ccache lookup.
1786 store_principal = store_creds.client;
1787 goto store;
1791 * We are trying S4U2Proxy:
1793 * We need the ticket from the S4U2Self step
1794 * and our TGT in order to get the delegated ticket.
1796 code = decode_Ticket((const uint8_t *)s4u2self_creds->ticket.data,
1797 s4u2self_creds->ticket.length,
1798 &s4u2self_ticket,
1799 &s4u2self_ticketlen);
1800 if (code != 0) {
1801 krb5_free_creds(ctx, s4u2self_creds);
1802 krb5_free_principal(ctx, blacklist_principal);
1803 krb5_cc_destroy(ctx, tmp_cc);
1804 return code;
1808 * we need to remember the client principal of the
1809 * S4U2Self stage and as it needs to match the one we
1810 * will get for the S4U2Proxy stage. We need this
1811 * in order to detect KDCs which does not support S4U2Proxy.
1813 whitelist_principal = s4u2self_creds->client;
1814 s4u2self_creds->client = NULL;
1815 krb5_free_creds(ctx, s4u2self_creds);
1818 * For S4U2Proxy we also got a target service principal,
1819 * which also belongs to our own realm (available on
1820 * our client principal).
1822 code = krb5_parse_name(ctx, target_service, &target_princ);
1823 if (code != 0) {
1824 free_Ticket(&s4u2self_ticket);
1825 krb5_free_principal(ctx, whitelist_principal);
1826 krb5_free_principal(ctx, blacklist_principal);
1827 krb5_cc_destroy(ctx, tmp_cc);
1828 return code;
1831 code = krb5_principal_set_realm(ctx, target_princ, self_realm);
1832 if (code != 0) {
1833 free_Ticket(&s4u2self_ticket);
1834 krb5_free_principal(ctx, target_princ);
1835 krb5_free_principal(ctx, whitelist_principal);
1836 krb5_free_principal(ctx, blacklist_principal);
1837 krb5_cc_destroy(ctx, tmp_cc);
1838 return code;
1841 code = krb5_get_creds_opt_alloc(ctx, &options);
1842 if (code != 0) {
1843 free_Ticket(&s4u2self_ticket);
1844 krb5_free_principal(ctx, target_princ);
1845 krb5_free_principal(ctx, whitelist_principal);
1846 krb5_free_principal(ctx, blacklist_principal);
1847 krb5_cc_destroy(ctx, tmp_cc);
1848 return code;
1851 krb5_get_creds_opt_set_options(ctx, options, KRB5_GC_FORWARDABLE);
1852 krb5_get_creds_opt_set_options(ctx, options, KRB5_GC_CONSTRAINED_DELEGATION);
1854 code = krb5_get_creds_opt_set_ticket(ctx, options, &s4u2self_ticket);
1855 free_Ticket(&s4u2self_ticket);
1856 if (code != 0) {
1857 krb5_get_creds_opt_free(ctx, options);
1858 krb5_free_principal(ctx, target_princ);
1859 krb5_free_principal(ctx, whitelist_principal);
1860 krb5_free_principal(ctx, blacklist_principal);
1861 krb5_cc_destroy(ctx, tmp_cc);
1862 return code;
1865 code = krb5_get_creds(ctx, options, tmp_cc,
1866 target_princ, &s4u2proxy_creds);
1867 krb5_get_creds_opt_free(ctx, options);
1868 krb5_free_principal(ctx, target_princ);
1869 krb5_cc_destroy(ctx, tmp_cc);
1870 if (code != 0) {
1871 krb5_free_principal(ctx, whitelist_principal);
1872 krb5_free_principal(ctx, blacklist_principal);
1873 return code;
1877 * Now make sure we store the impersonated principal
1878 * and creds instead of the TGT related stuff
1879 * in the krb5_ccache of the caller.
1881 code = krb5_copy_creds_contents(ctx, s4u2proxy_creds,
1882 &store_creds);
1883 krb5_free_creds(ctx, s4u2proxy_creds);
1884 if (code != 0) {
1885 krb5_free_principal(ctx, whitelist_principal);
1886 krb5_free_principal(ctx, blacklist_principal);
1887 return code;
1891 * It's important to store the principal the KDC
1892 * returned, as otherwise the caller would not find
1893 * the S4U2Self ticket in the krb5_ccache lookup.
1895 store_principal = store_creds.client;
1897 store:
1898 if (blacklist_principal &&
1899 krb5_principal_compare(ctx, store_creds.client, blacklist_principal)) {
1900 char *sp = NULL;
1901 char *ip = NULL;
1903 code = krb5_unparse_name(ctx, blacklist_principal, &sp);
1904 if (code != 0) {
1905 sp = NULL;
1907 code = krb5_unparse_name(ctx, impersonate_principal, &ip);
1908 if (code != 0) {
1909 ip = NULL;
1911 DEBUG(1, ("kerberos_kinit_password_cc: "
1912 "KDC returned self principal[%s] while impersonating [%s]\n",
1913 sp?sp:"<no memory>",
1914 ip?ip:"<no memory>"));
1916 SAFE_FREE(sp);
1917 SAFE_FREE(ip);
1919 krb5_free_principal(ctx, whitelist_principal);
1920 krb5_free_principal(ctx, blacklist_principal);
1921 krb5_free_cred_contents(ctx, &store_creds);
1922 return KRB5_FWD_BAD_PRINCIPAL;
1924 if (blacklist_principal) {
1925 krb5_free_principal(ctx, blacklist_principal);
1928 if (whitelist_principal &&
1929 !krb5_principal_compare(ctx, store_creds.client, whitelist_principal)) {
1930 char *sp = NULL;
1931 char *ep = NULL;
1933 code = krb5_unparse_name(ctx, store_creds.client, &sp);
1934 if (code != 0) {
1935 sp = NULL;
1937 code = krb5_unparse_name(ctx, whitelist_principal, &ep);
1938 if (code != 0) {
1939 ep = NULL;
1941 DEBUG(1, ("kerberos_kinit_password_cc: "
1942 "KDC returned wrong principal[%s] we expected [%s]\n",
1943 sp?sp:"<no memory>",
1944 ep?ep:"<no memory>"));
1946 SAFE_FREE(sp);
1947 SAFE_FREE(ep);
1949 krb5_free_principal(ctx, whitelist_principal);
1950 krb5_free_cred_contents(ctx, &store_creds);
1951 return KRB5_FWD_BAD_PRINCIPAL;
1953 if (whitelist_principal) {
1954 krb5_free_principal(ctx, whitelist_principal);
1957 code = krb5_cc_initialize(ctx, store_cc, store_principal);
1958 if (code != 0) {
1959 krb5_free_cred_contents(ctx, &store_creds);
1960 return code;
1963 code = krb5_cc_store_cred(ctx, store_cc, &store_creds);
1964 if (code != 0) {
1965 krb5_free_cred_contents(ctx, &store_creds);
1966 return code;
1969 if (expire_time) {
1970 *expire_time = (time_t) store_creds.times.endtime;
1973 if (kdc_time) {
1974 *kdc_time = (time_t) store_creds.times.starttime;
1977 krb5_free_cred_contents(ctx, &store_creds);
1979 return 0;
1981 #endif
1983 #if !defined(HAVE_KRB5_MAKE_PRINCIPAL) && defined(HAVE_KRB5_BUILD_PRINCIPAL_ALLOC_VA)
1984 krb5_error_code smb_krb5_make_principal(krb5_context context,
1985 krb5_principal *principal,
1986 const char *_realm, ...)
1988 krb5_error_code code;
1989 bool free_realm;
1990 char *realm;
1991 va_list ap;
1993 if (_realm) {
1994 realm = _realm;
1995 free_realm = false;
1996 } else {
1997 code = krb5_get_default_realm(context, &realm);
1998 if (code) {
1999 return code;
2001 free_realm = true;
2004 va_start(ap, _realm);
2005 code = krb5_build_principal_alloc_va(context, principal,
2006 strlen(realm), realm,
2007 ap);
2008 va_end(ap);
2010 if (free_realm) {
2011 krb5_free_default_realm(context, realm);
2014 return code;
2016 #endif
2018 #if !defined(HAVE_KRB5_CC_GET_LIFETIME) && defined(HAVE_KRB5_CC_RETRIEVE_CRED)
2020 * @brief Get the lifetime of the initial ticket in the cache.
2022 * @param[in] context The kerberos context.
2024 * @param[in] id The credential cache to get the ticket lifetime.
2026 * @param[out] t A pointer to a time value to store the lifetime.
2028 * @return 0 on success, a krb5_error_code on error.
2030 krb5_error_code smb_krb5_cc_get_lifetime(krb5_context context,
2031 krb5_ccache id,
2032 time_t *t)
2034 krb5_cc_cursor cursor;
2035 krb5_error_code kerr;
2036 krb5_creds cred;
2037 krb5_timestamp now;
2039 *t = 0;
2041 kerr = krb5_timeofday(context, &now);
2042 if (kerr) {
2043 return kerr;
2046 kerr = krb5_cc_start_seq_get(context, id, &cursor);
2047 if (kerr) {
2048 return kerr;
2051 while ((kerr = krb5_cc_next_cred(context, id, &cursor, &cred)) == 0) {
2052 #ifndef HAVE_FLAGS_IN_KRB5_CREDS
2053 if (cred.ticket_flags & TKT_FLG_INITIAL) {
2054 #else
2055 if (cred.flags.b.initial) {
2056 #endif
2057 if (now < cred.times.endtime) {
2058 *t = (time_t) (cred.times.endtime - now);
2060 krb5_free_cred_contents(context, &cred);
2061 break;
2063 krb5_free_cred_contents(context, &cred);
2066 krb5_cc_end_seq_get(context, id, &cursor);
2068 return kerr;
2070 #endif /* HAVE_KRB5_CC_GET_LIFETIME */
2072 #if !defined(HAVE_KRB5_FREE_CHECKSUM_CONTENTS) && defined(HAVE_FREE_CHECKSUM)
2073 void smb_krb5_free_checksum_contents(krb5_context ctx, krb5_checksum *cksum)
2075 free_Checksum(cksum);
2077 #endif
2079 krb5_error_code smb_krb5_make_pac_checksum(TALLOC_CTX *mem_ctx,
2080 DATA_BLOB *pac_data,
2081 krb5_context context,
2082 const krb5_keyblock *keyblock,
2083 uint32_t *sig_type,
2084 DATA_BLOB *sig_blob)
2086 krb5_error_code ret;
2087 krb5_checksum cksum;
2088 #if defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CREATE_CHECKSUM)
2089 krb5_crypto crypto;
2092 ret = krb5_crypto_init(context,
2093 keyblock,
2095 &crypto);
2096 if (ret) {
2097 DEBUG(0,("krb5_crypto_init() failed: %s\n",
2098 smb_get_krb5_error_message(context, ret, mem_ctx)));
2099 return ret;
2101 ret = krb5_create_checksum(context,
2102 crypto,
2103 KRB5_KU_OTHER_CKSUM,
2105 pac_data->data,
2106 pac_data->length,
2107 &cksum);
2108 if (ret) {
2109 DEBUG(2, ("PAC Verification failed: %s\n",
2110 smb_get_krb5_error_message(context, ret, mem_ctx)));
2113 krb5_crypto_destroy(context, crypto);
2115 if (ret) {
2116 return ret;
2119 *sig_type = cksum.cksumtype;
2120 *sig_blob = data_blob_talloc(mem_ctx,
2121 cksum.checksum.data,
2122 cksum.checksum.length);
2123 #elif defined(HAVE_KRB5_C_MAKE_CHECKSUM)
2124 krb5_data input;
2126 input.data = (char *)pac_data->data;
2127 input.length = pac_data->length;
2129 ret = krb5_c_make_checksum(context,
2131 keyblock,
2132 KRB5_KEYUSAGE_APP_DATA_CKSUM,
2133 &input,
2134 &cksum);
2135 if (ret) {
2136 DEBUG(2, ("PAC Verification failed: %s\n",
2137 smb_get_krb5_error_message(context, ret, mem_ctx)));
2138 return ret;
2141 *sig_type = cksum.checksum_type;
2142 *sig_blob = data_blob_talloc(mem_ctx,
2143 cksum.contents,
2144 cksum.length);
2146 #else
2147 #error krb5_create_checksum or krb5_c_make_checksum not available
2148 #endif /* HAVE_KRB5_C_MAKE_CHECKSUM */
2149 smb_krb5_free_checksum_contents(context, &cksum);
2151 return 0;
2156 * smb_krb5_principal_get_realm
2158 * @brief Get realm of a principal
2160 * @param[in] context The krb5_context
2161 * @param[in] principal The principal
2162 * @return pointer to the realm
2166 char *smb_krb5_principal_get_realm(krb5_context context,
2167 krb5_principal principal)
2169 #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
2170 return discard_const_p(char, krb5_principal_get_realm(context, principal));
2171 #elif defined(krb5_princ_realm) /* MIT */
2172 krb5_data *realm;
2173 realm = krb5_princ_realm(context, principal);
2174 return discard_const_p(char, realm->data);
2175 #else
2176 return NULL;
2177 #endif
2180 /************************************************************************
2181 Routine to get the default realm from the kerberos credentials cache.
2182 Caller must free if the return value is not NULL.
2183 ************************************************************************/
2185 static char *smb_krb5_get_default_realm_from_ccache(TALLOC_CTX *mem_ctx)
2187 char *realm = NULL;
2188 krb5_context ctx = NULL;
2189 krb5_ccache cc = NULL;
2190 krb5_principal princ = NULL;
2192 initialize_krb5_error_table();
2193 if (krb5_init_context(&ctx)) {
2194 return NULL;
2197 DEBUG(5,("kerberos_get_default_realm_from_ccache: "
2198 "Trying to read krb5 cache: %s\n",
2199 krb5_cc_default_name(ctx)));
2200 if (krb5_cc_default(ctx, &cc)) {
2201 DEBUG(0,("kerberos_get_default_realm_from_ccache: "
2202 "failed to read default cache\n"));
2203 goto out;
2205 if (krb5_cc_get_principal(ctx, cc, &princ)) {
2206 DEBUG(0,("kerberos_get_default_realm_from_ccache: "
2207 "failed to get default principal\n"));
2208 goto out;
2211 #if defined(HAVE_KRB5_PRINCIPAL_GET_REALM)
2212 realm = talloc_strdup(mem_ctx, krb5_principal_get_realm(ctx, princ));
2213 #elif defined(HAVE_KRB5_PRINC_REALM)
2215 krb5_data *realm_data = krb5_princ_realm(ctx, princ);
2216 realm = talloc_strndup(mem_ctx, realm_data->data, realm_data->length);
2218 #endif
2220 out:
2222 if (ctx) {
2223 if (princ) {
2224 krb5_free_principal(ctx, princ);
2226 if (cc) {
2227 krb5_cc_close(ctx, cc);
2229 krb5_free_context(ctx);
2232 return realm;
2235 /************************************************************************
2236 Routine to get the realm from a given DNS name.
2237 ************************************************************************/
2239 static char *smb_krb5_get_realm_from_hostname(TALLOC_CTX *mem_ctx,
2240 const char *hostname)
2242 #if defined(HAVE_KRB5_REALM_TYPE)
2243 /* Heimdal. */
2244 krb5_realm *realm_list = NULL;
2245 #else
2246 /* MIT */
2247 char **realm_list = NULL;
2248 #endif
2249 char *realm = NULL;
2250 krb5_error_code kerr;
2251 krb5_context ctx = NULL;
2253 initialize_krb5_error_table();
2254 if (krb5_init_context(&ctx)) {
2255 return NULL;
2258 kerr = krb5_get_host_realm(ctx, hostname, &realm_list);
2259 if (kerr != 0) {
2260 DEBUG(3,("kerberos_get_realm_from_hostname %s: "
2261 "failed %s\n",
2262 hostname ? hostname : "(NULL)",
2263 error_message(kerr) ));
2264 goto out;
2267 if (realm_list && realm_list[0]) {
2268 realm = talloc_strdup(mem_ctx, realm_list[0]);
2271 out:
2273 if (ctx) {
2274 if (realm_list) {
2275 krb5_free_host_realm(ctx, realm_list);
2276 realm_list = NULL;
2278 krb5_free_context(ctx);
2279 ctx = NULL;
2281 return realm;
2284 char *kerberos_get_principal_from_service_hostname(TALLOC_CTX *mem_ctx,
2285 const char *service,
2286 const char *remote_name,
2287 const char *default_realm)
2289 char *realm = NULL;
2290 char *host = NULL;
2291 char *principal;
2292 host = strchr_m(remote_name, '.');
2293 if (host) {
2294 /* DNS name. */
2295 realm = smb_krb5_get_realm_from_hostname(talloc_tos(),
2296 remote_name);
2297 } else {
2298 /* NetBIOS name - use our realm. */
2299 realm = smb_krb5_get_default_realm_from_ccache(talloc_tos());
2302 if (realm == NULL || *realm == '\0') {
2303 realm = talloc_strdup(talloc_tos(), default_realm);
2304 if (!realm) {
2305 return NULL;
2307 DEBUG(3,("kerberos_get_principal_from_service_hostname: "
2308 "cannot get realm from, "
2309 "desthost %s or default ccache. Using default "
2310 "smb.conf realm %s\n",
2311 remote_name,
2312 realm));
2315 principal = talloc_asprintf(mem_ctx,
2316 "%s/%s@%s",
2317 service, remote_name,
2318 realm);
2319 TALLOC_FREE(realm);
2320 return principal;
2323 char *smb_get_krb5_error_message(krb5_context context,
2324 krb5_error_code code,
2325 TALLOC_CTX *mem_ctx)
2327 char *ret;
2329 #if defined(HAVE_KRB5_GET_ERROR_MESSAGE) && defined(HAVE_KRB5_FREE_ERROR_MESSAGE)
2330 const char *context_error = krb5_get_error_message(context, code);
2331 if (context_error) {
2332 ret = talloc_asprintf(mem_ctx, "%s: %s",
2333 error_message(code), context_error);
2334 krb5_free_error_message(context, context_error);
2335 return ret;
2337 #endif
2338 ret = talloc_strdup(mem_ctx, error_message(code));
2339 return ret;
2342 #else /* HAVE_KRB5 */
2343 /* this saves a few linking headaches */
2344 int cli_krb5_get_ticket(TALLOC_CTX *mem_ctx,
2345 const char *principal, time_t time_offset,
2346 DATA_BLOB *ticket, DATA_BLOB *session_key_krb5,
2347 uint32_t extra_ap_opts,
2348 const char *ccname, time_t *tgs_expire,
2349 const char *impersonate_princ_s)
2351 DEBUG(0,("NO KERBEROS SUPPORT\n"));
2352 return 1;
2355 #endif /* HAVE_KRB5 */