s3:rpc_server: explicitly use allow_warnings=True for RPC_SPOOLSS
[Samba/wip.git] / lib / krb5_wrap / krb5_samba.c
blobb218437da0ef2ec5ab3554b9907b0e1873d3f41b
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
1338 #if !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER
1339 krb5_error_code KRB5_CALLCONV
1340 krb5_get_credentials_for_user(krb5_context context, krb5_flags options,
1341 krb5_ccache ccache, krb5_creds *in_creds,
1342 krb5_data *subject_cert,
1343 krb5_creds **out_creds);
1344 #endif /* !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER */
1346 static krb5_error_code smb_krb5_get_credentials_for_user(krb5_context context,
1347 krb5_ccache ccache,
1348 krb5_principal me,
1349 krb5_principal server,
1350 krb5_principal impersonate_princ,
1351 krb5_creds **out_creds)
1353 krb5_error_code ret;
1354 krb5_creds in_creds;
1356 ZERO_STRUCT(in_creds);
1358 if (impersonate_princ) {
1360 in_creds.server = me;
1361 in_creds.client = impersonate_princ;
1363 ret = krb5_get_credentials_for_user(context,
1364 0, /* krb5_flags options */
1365 ccache,
1366 &in_creds,
1367 NULL, /* krb5_data *subject_cert */
1368 out_creds);
1369 } else {
1370 in_creds.client = me;
1371 in_creds.server = server;
1373 ret = krb5_get_credentials(context, 0, ccache,
1374 &in_creds, out_creds);
1377 return ret;
1379 #endif /* HAVE_KRB5_GET_CREDENTIALS_FOR_USER */
1382 * smb_krb5_get_credentials
1384 * @brief Get krb5 credentials for a server
1386 * @param[in] context An initialized krb5_context
1387 * @param[in] ccache An initialized krb5_ccache
1388 * @param[in] me The krb5_principal of the caller
1389 * @param[in] server The krb5_principal of the requested service
1390 * @param[in] impersonate_princ The krb5_principal of a user to impersonate as (optional)
1391 * @param[out] out_creds The returned krb5_creds structure
1392 * @return krb5_error_code
1395 krb5_error_code smb_krb5_get_credentials(krb5_context context,
1396 krb5_ccache ccache,
1397 krb5_principal me,
1398 krb5_principal server,
1399 krb5_principal impersonate_princ,
1400 krb5_creds **out_creds)
1402 krb5_error_code ret;
1403 krb5_creds *creds = NULL;
1405 if (out_creds != NULL) {
1406 *out_creds = NULL;
1409 if (impersonate_princ) {
1410 #ifdef HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE /* Heimdal */
1411 ret = smb_krb5_get_credentials_for_user_opt(context, ccache, me, server, impersonate_princ, &creds);
1412 #elif defined(HAVE_KRB5_GET_CREDENTIALS_FOR_USER) /* MIT */
1413 ret = smb_krb5_get_credentials_for_user(context, ccache, me, server, impersonate_princ, &creds);
1414 #else
1415 ret = ENOTSUP;
1416 #endif
1417 } else {
1418 krb5_creds in_creds;
1420 ZERO_STRUCT(in_creds);
1422 in_creds.client = me;
1423 in_creds.server = server;
1425 ret = krb5_get_credentials(context, 0, ccache,
1426 &in_creds, &creds);
1428 if (ret) {
1429 goto done;
1432 if (out_creds) {
1433 *out_creds = creds;
1436 done:
1437 if (creds && ret) {
1438 krb5_free_creds(context, creds);
1441 return ret;
1444 krb5_error_code smb_krb5_keyblock_init_contents(krb5_context context,
1445 krb5_enctype enctype,
1446 const void *data,
1447 size_t length,
1448 krb5_keyblock *key)
1450 #if defined(HAVE_KRB5_KEYBLOCK_INIT)
1451 return krb5_keyblock_init(context, enctype, data, length, key);
1452 #else
1453 memset(key, 0, sizeof(krb5_keyblock));
1454 KRB5_KEY_DATA(key) = SMB_MALLOC(length);
1455 if (NULL == KRB5_KEY_DATA(key)) {
1456 return ENOMEM;
1458 memcpy(KRB5_KEY_DATA(key), data, length);
1459 KRB5_KEY_LENGTH(key) = length;
1460 KRB5_KEY_TYPE(key) = enctype;
1461 return 0;
1462 #endif
1466 simulate a kinit, putting the tgt in the given credentials cache.
1467 Orignally by remus@snapserver.com
1469 This version is built to use a keyblock, rather than needing the
1470 original password.
1472 The impersonate_principal is the principal if NULL, or the principal
1473 to impersonate
1475 The target_service defaults to the krbtgt if NULL, but could be
1476 kpasswd/realm or the local service (if we are doing s4u2self)
1478 krb5_error_code kerberos_kinit_keyblock_cc(krb5_context ctx, krb5_ccache cc,
1479 krb5_principal principal,
1480 krb5_keyblock *keyblock,
1481 const char *target_service,
1482 krb5_get_init_creds_opt *krb_options,
1483 time_t *expire_time,
1484 time_t *kdc_time)
1486 krb5_error_code code = 0;
1487 krb5_creds my_creds;
1489 #if defined(HAVE_KRB5_GET_INIT_CREDS_KEYBLOCK)
1490 code = krb5_get_init_creds_keyblock(ctx, &my_creds, principal,
1491 keyblock, 0, target_service,
1492 krb_options);
1493 #elif defined(HAVE_KRB5_GET_INIT_CREDS_KEYTAB)
1495 #define SMB_CREDS_KEYTAB "MEMORY:tmp_smb_creds_XXXXXX"
1496 char tmp_name[sizeof(SMB_CREDS_KEYTAB)];
1497 krb5_keytab_entry entry;
1498 krb5_keytab keytab;
1499 mode_t mask;
1501 memset(&entry, 0, sizeof(entry));
1502 entry.principal = principal;
1503 *(KRB5_KT_KEY(&entry)) = *keyblock;
1505 memcpy(tmp_name, SMB_CREDS_KEYTAB, sizeof(SMB_CREDS_KEYTAB));
1506 mask = umask(S_IRWXO | S_IRWXG);
1507 mktemp(tmp_name);
1508 umask(mask);
1509 if (tmp_name[0] == 0) {
1510 return KRB5_KT_BADNAME;
1512 code = krb5_kt_resolve(ctx, tmp_name, &keytab);
1513 if (code) {
1514 return code;
1517 code = krb5_kt_add_entry(ctx, keytab, &entry);
1518 if (code) {
1519 (void)krb5_kt_close(ctx, keytab);
1520 goto done;
1523 code = krb5_get_init_creds_keytab(ctx, &my_creds, principal,
1524 keytab, 0, target_service,
1525 krb_options);
1526 (void)krb5_kt_close(ctx, keytab);
1528 #else
1529 #error krb5_get_init_creds_keyblock not available!
1530 #endif
1531 if (code) {
1532 return code;
1535 code = krb5_cc_initialize(ctx, cc, principal);
1536 if (code) {
1537 goto done;
1540 code = krb5_cc_store_cred(ctx, cc, &my_creds);
1541 if (code) {
1542 goto done;
1545 if (expire_time) {
1546 *expire_time = (time_t) my_creds.times.endtime;
1549 if (kdc_time) {
1550 *kdc_time = (time_t) my_creds.times.starttime;
1553 code = 0;
1554 done:
1555 krb5_free_cred_contents(ctx, &my_creds);
1556 return code;
1559 krb5_error_code kerberos_kinit_password_cc(krb5_context ctx, krb5_ccache cc,
1560 krb5_principal principal,
1561 const char *password,
1562 const char *target_service,
1563 krb5_get_init_creds_opt *krb_options,
1564 time_t *expire_time,
1565 time_t *kdc_time)
1567 krb5_error_code code = 0;
1568 krb5_creds my_creds;
1570 code = krb5_get_init_creds_password(ctx, &my_creds, principal,
1571 password, NULL, NULL, 0,
1572 target_service, krb_options);
1573 if (code) {
1574 return code;
1577 code = krb5_cc_initialize(ctx, cc, principal);
1578 if (code) {
1579 goto done;
1582 code = krb5_cc_store_cred(ctx, cc, &my_creds);
1583 if (code) {
1584 goto done;
1587 if (expire_time) {
1588 *expire_time = (time_t) my_creds.times.endtime;
1591 if (kdc_time) {
1592 *kdc_time = (time_t) my_creds.times.starttime;
1595 code = 0;
1596 done:
1597 krb5_free_cred_contents(ctx, &my_creds);
1598 return code;
1601 #ifdef SAMBA4_USES_HEIMDAL
1603 simulate a kinit, putting the tgt in the given credentials cache.
1604 Orignally by remus@snapserver.com
1606 The impersonate_principal is the principal
1608 The self_service, should be the local service (for S4U2Self if
1609 impersonate_principal is given).
1611 The target_service defaults to the krbtgt if NULL, but could be
1612 kpasswd/realm or a remote service (for S4U2Proxy)
1615 krb5_error_code kerberos_kinit_s4u2_cc(krb5_context ctx,
1616 krb5_ccache store_cc,
1617 krb5_principal init_principal,
1618 const char *init_password,
1619 krb5_principal impersonate_principal,
1620 const char *self_service,
1621 const char *target_service,
1622 krb5_get_init_creds_opt *krb_options,
1623 time_t *expire_time,
1624 time_t *kdc_time)
1626 krb5_error_code code = 0;
1627 krb5_get_creds_opt options;
1628 krb5_principal store_principal;
1629 krb5_creds store_creds;
1630 krb5_creds *s4u2self_creds;
1631 Ticket s4u2self_ticket;
1632 size_t s4u2self_ticketlen;
1633 krb5_creds *s4u2proxy_creds;
1634 krb5_principal self_princ;
1635 bool s4u2proxy;
1636 krb5_principal target_princ;
1637 krb5_ccache tmp_cc;
1638 const char *self_realm;
1639 krb5_principal blacklist_principal = NULL;
1640 krb5_principal whitelist_principal = NULL;
1642 code = krb5_get_init_creds_password(ctx, &store_creds,
1643 init_principal,
1644 init_password,
1645 NULL, NULL,
1647 NULL,
1648 krb_options);
1649 if (code != 0) {
1650 return code;
1653 store_principal = init_principal;
1656 * We are trying S4U2Self now:
1658 * As we do not want to expose our TGT in the
1659 * krb5_ccache, which is also holds the impersonated creds.
1661 * Some low level krb5/gssapi function might use the TGT
1662 * identity and let the client act as our machine account.
1664 * We need to avoid that and use a temporary krb5_ccache
1665 * in order to pass our TGT to the krb5_get_creds() function.
1667 code = krb5_cc_new_unique(ctx, NULL, NULL, &tmp_cc);
1668 if (code != 0) {
1669 krb5_free_cred_contents(ctx, &store_creds);
1670 return code;
1673 code = krb5_cc_initialize(ctx, tmp_cc, store_creds.client);
1674 if (code != 0) {
1675 krb5_cc_destroy(ctx, tmp_cc);
1676 krb5_free_cred_contents(ctx, &store_creds);
1677 return code;
1680 code = krb5_cc_store_cred(ctx, tmp_cc, &store_creds);
1681 if (code != 0) {
1682 krb5_free_cred_contents(ctx, &store_creds);
1683 krb5_cc_destroy(ctx, tmp_cc);
1684 return code;
1688 * we need to remember the client principal of our
1689 * TGT and make sure the KDC does not return this
1690 * in the impersonated tickets. This can happen
1691 * if the KDC does not support S4U2Self and S4U2Proxy.
1693 blacklist_principal = store_creds.client;
1694 store_creds.client = NULL;
1695 krb5_free_cred_contents(ctx, &store_creds);
1698 * Check if we also need S4U2Proxy or if S4U2Self is
1699 * enough in order to get a ticket for the target.
1701 if (target_service == NULL) {
1702 s4u2proxy = false;
1703 } else if (strcmp(target_service, self_service) == 0) {
1704 s4u2proxy = false;
1705 } else {
1706 s4u2proxy = true;
1710 * For S4U2Self we need our own service principal,
1711 * which belongs to our own realm (available on
1712 * our client principal).
1714 self_realm = krb5_principal_get_realm(ctx, init_principal);
1716 code = krb5_parse_name(ctx, self_service, &self_princ);
1717 if (code != 0) {
1718 krb5_free_principal(ctx, blacklist_principal);
1719 krb5_cc_destroy(ctx, tmp_cc);
1720 return code;
1723 code = krb5_principal_set_realm(ctx, self_princ, self_realm);
1724 if (code != 0) {
1725 krb5_free_principal(ctx, blacklist_principal);
1726 krb5_free_principal(ctx, self_princ);
1727 krb5_cc_destroy(ctx, tmp_cc);
1728 return code;
1731 code = krb5_get_creds_opt_alloc(ctx, &options);
1732 if (code != 0) {
1733 krb5_free_principal(ctx, blacklist_principal);
1734 krb5_free_principal(ctx, self_princ);
1735 krb5_cc_destroy(ctx, tmp_cc);
1736 return code;
1739 if (s4u2proxy) {
1741 * If we want S4U2Proxy, we need the forwardable flag
1742 * on the S4U2Self ticket.
1744 krb5_get_creds_opt_set_options(ctx, options, KRB5_GC_FORWARDABLE);
1747 code = krb5_get_creds_opt_set_impersonate(ctx, options,
1748 impersonate_principal);
1749 if (code != 0) {
1750 krb5_get_creds_opt_free(ctx, options);
1751 krb5_free_principal(ctx, blacklist_principal);
1752 krb5_free_principal(ctx, self_princ);
1753 krb5_cc_destroy(ctx, tmp_cc);
1754 return code;
1757 code = krb5_get_creds(ctx, options, tmp_cc,
1758 self_princ, &s4u2self_creds);
1759 krb5_get_creds_opt_free(ctx, options);
1760 krb5_free_principal(ctx, self_princ);
1761 if (code != 0) {
1762 krb5_free_principal(ctx, blacklist_principal);
1763 krb5_cc_destroy(ctx, tmp_cc);
1764 return code;
1767 if (!s4u2proxy) {
1768 krb5_cc_destroy(ctx, tmp_cc);
1771 * Now make sure we store the impersonated principal
1772 * and creds instead of the TGT related stuff
1773 * in the krb5_ccache of the caller.
1775 code = krb5_copy_creds_contents(ctx, s4u2self_creds,
1776 &store_creds);
1777 krb5_free_creds(ctx, s4u2self_creds);
1778 if (code != 0) {
1779 return code;
1783 * It's important to store the principal the KDC
1784 * returned, as otherwise the caller would not find
1785 * the S4U2Self ticket in the krb5_ccache lookup.
1787 store_principal = store_creds.client;
1788 goto store;
1792 * We are trying S4U2Proxy:
1794 * We need the ticket from the S4U2Self step
1795 * and our TGT in order to get the delegated ticket.
1797 code = decode_Ticket((const uint8_t *)s4u2self_creds->ticket.data,
1798 s4u2self_creds->ticket.length,
1799 &s4u2self_ticket,
1800 &s4u2self_ticketlen);
1801 if (code != 0) {
1802 krb5_free_creds(ctx, s4u2self_creds);
1803 krb5_free_principal(ctx, blacklist_principal);
1804 krb5_cc_destroy(ctx, tmp_cc);
1805 return code;
1809 * we need to remember the client principal of the
1810 * S4U2Self stage and as it needs to match the one we
1811 * will get for the S4U2Proxy stage. We need this
1812 * in order to detect KDCs which does not support S4U2Proxy.
1814 whitelist_principal = s4u2self_creds->client;
1815 s4u2self_creds->client = NULL;
1816 krb5_free_creds(ctx, s4u2self_creds);
1819 * For S4U2Proxy we also got a target service principal,
1820 * which also belongs to our own realm (available on
1821 * our client principal).
1823 code = krb5_parse_name(ctx, target_service, &target_princ);
1824 if (code != 0) {
1825 free_Ticket(&s4u2self_ticket);
1826 krb5_free_principal(ctx, whitelist_principal);
1827 krb5_free_principal(ctx, blacklist_principal);
1828 krb5_cc_destroy(ctx, tmp_cc);
1829 return code;
1832 code = krb5_principal_set_realm(ctx, target_princ, self_realm);
1833 if (code != 0) {
1834 free_Ticket(&s4u2self_ticket);
1835 krb5_free_principal(ctx, target_princ);
1836 krb5_free_principal(ctx, whitelist_principal);
1837 krb5_free_principal(ctx, blacklist_principal);
1838 krb5_cc_destroy(ctx, tmp_cc);
1839 return code;
1842 code = krb5_get_creds_opt_alloc(ctx, &options);
1843 if (code != 0) {
1844 free_Ticket(&s4u2self_ticket);
1845 krb5_free_principal(ctx, target_princ);
1846 krb5_free_principal(ctx, whitelist_principal);
1847 krb5_free_principal(ctx, blacklist_principal);
1848 krb5_cc_destroy(ctx, tmp_cc);
1849 return code;
1852 krb5_get_creds_opt_set_options(ctx, options, KRB5_GC_FORWARDABLE);
1853 krb5_get_creds_opt_set_options(ctx, options, KRB5_GC_CONSTRAINED_DELEGATION);
1855 code = krb5_get_creds_opt_set_ticket(ctx, options, &s4u2self_ticket);
1856 free_Ticket(&s4u2self_ticket);
1857 if (code != 0) {
1858 krb5_get_creds_opt_free(ctx, options);
1859 krb5_free_principal(ctx, target_princ);
1860 krb5_free_principal(ctx, whitelist_principal);
1861 krb5_free_principal(ctx, blacklist_principal);
1862 krb5_cc_destroy(ctx, tmp_cc);
1863 return code;
1866 code = krb5_get_creds(ctx, options, tmp_cc,
1867 target_princ, &s4u2proxy_creds);
1868 krb5_get_creds_opt_free(ctx, options);
1869 krb5_free_principal(ctx, target_princ);
1870 krb5_cc_destroy(ctx, tmp_cc);
1871 if (code != 0) {
1872 krb5_free_principal(ctx, whitelist_principal);
1873 krb5_free_principal(ctx, blacklist_principal);
1874 return code;
1878 * Now make sure we store the impersonated principal
1879 * and creds instead of the TGT related stuff
1880 * in the krb5_ccache of the caller.
1882 code = krb5_copy_creds_contents(ctx, s4u2proxy_creds,
1883 &store_creds);
1884 krb5_free_creds(ctx, s4u2proxy_creds);
1885 if (code != 0) {
1886 krb5_free_principal(ctx, whitelist_principal);
1887 krb5_free_principal(ctx, blacklist_principal);
1888 return code;
1892 * It's important to store the principal the KDC
1893 * returned, as otherwise the caller would not find
1894 * the S4U2Self ticket in the krb5_ccache lookup.
1896 store_principal = store_creds.client;
1898 store:
1899 if (blacklist_principal &&
1900 krb5_principal_compare(ctx, store_creds.client, blacklist_principal)) {
1901 char *sp = NULL;
1902 char *ip = NULL;
1904 code = krb5_unparse_name(ctx, blacklist_principal, &sp);
1905 if (code != 0) {
1906 sp = NULL;
1908 code = krb5_unparse_name(ctx, impersonate_principal, &ip);
1909 if (code != 0) {
1910 ip = NULL;
1912 DEBUG(1, ("kerberos_kinit_password_cc: "
1913 "KDC returned self principal[%s] while impersonating [%s]\n",
1914 sp?sp:"<no memory>",
1915 ip?ip:"<no memory>"));
1917 SAFE_FREE(sp);
1918 SAFE_FREE(ip);
1920 krb5_free_principal(ctx, whitelist_principal);
1921 krb5_free_principal(ctx, blacklist_principal);
1922 krb5_free_cred_contents(ctx, &store_creds);
1923 return KRB5_FWD_BAD_PRINCIPAL;
1925 if (blacklist_principal) {
1926 krb5_free_principal(ctx, blacklist_principal);
1929 if (whitelist_principal &&
1930 !krb5_principal_compare(ctx, store_creds.client, whitelist_principal)) {
1931 char *sp = NULL;
1932 char *ep = NULL;
1934 code = krb5_unparse_name(ctx, store_creds.client, &sp);
1935 if (code != 0) {
1936 sp = NULL;
1938 code = krb5_unparse_name(ctx, whitelist_principal, &ep);
1939 if (code != 0) {
1940 ep = NULL;
1942 DEBUG(1, ("kerberos_kinit_password_cc: "
1943 "KDC returned wrong principal[%s] we expected [%s]\n",
1944 sp?sp:"<no memory>",
1945 ep?ep:"<no memory>"));
1947 SAFE_FREE(sp);
1948 SAFE_FREE(ep);
1950 krb5_free_principal(ctx, whitelist_principal);
1951 krb5_free_cred_contents(ctx, &store_creds);
1952 return KRB5_FWD_BAD_PRINCIPAL;
1954 if (whitelist_principal) {
1955 krb5_free_principal(ctx, whitelist_principal);
1958 code = krb5_cc_initialize(ctx, store_cc, store_principal);
1959 if (code != 0) {
1960 krb5_free_cred_contents(ctx, &store_creds);
1961 return code;
1964 code = krb5_cc_store_cred(ctx, store_cc, &store_creds);
1965 if (code != 0) {
1966 krb5_free_cred_contents(ctx, &store_creds);
1967 return code;
1970 if (expire_time) {
1971 *expire_time = (time_t) store_creds.times.endtime;
1974 if (kdc_time) {
1975 *kdc_time = (time_t) store_creds.times.starttime;
1978 krb5_free_cred_contents(ctx, &store_creds);
1980 return 0;
1982 #endif
1984 #if !defined(HAVE_KRB5_MAKE_PRINCIPAL) && defined(HAVE_KRB5_BUILD_PRINCIPAL_ALLOC_VA)
1985 krb5_error_code smb_krb5_make_principal(krb5_context context,
1986 krb5_principal *principal,
1987 const char *_realm, ...)
1989 krb5_error_code code;
1990 bool free_realm;
1991 char *realm;
1992 va_list ap;
1994 if (_realm) {
1995 realm = _realm;
1996 free_realm = false;
1997 } else {
1998 code = krb5_get_default_realm(context, &realm);
1999 if (code) {
2000 return code;
2002 free_realm = true;
2005 va_start(ap, _realm);
2006 code = krb5_build_principal_alloc_va(context, principal,
2007 strlen(realm), realm,
2008 ap);
2009 va_end(ap);
2011 if (free_realm) {
2012 krb5_free_default_realm(context, realm);
2015 return code;
2017 #endif
2019 #if !defined(HAVE_KRB5_CC_GET_LIFETIME) && defined(HAVE_KRB5_CC_RETRIEVE_CRED)
2021 * @brief Get the lifetime of the initial ticket in the cache.
2023 * @param[in] context The kerberos context.
2025 * @param[in] id The credential cache to get the ticket lifetime.
2027 * @param[out] t A pointer to a time value to store the lifetime.
2029 * @return 0 on success, a krb5_error_code on error.
2031 krb5_error_code smb_krb5_cc_get_lifetime(krb5_context context,
2032 krb5_ccache id,
2033 time_t *t)
2035 krb5_cc_cursor cursor;
2036 krb5_error_code kerr;
2037 krb5_creds cred;
2038 krb5_timestamp now;
2040 *t = 0;
2042 kerr = krb5_timeofday(context, &now);
2043 if (kerr) {
2044 return kerr;
2047 kerr = krb5_cc_start_seq_get(context, id, &cursor);
2048 if (kerr) {
2049 return kerr;
2052 while ((kerr = krb5_cc_next_cred(context, id, &cursor, &cred)) == 0) {
2053 #ifndef HAVE_FLAGS_IN_KRB5_CREDS
2054 if (cred.ticket_flags & TKT_FLG_INITIAL) {
2055 #else
2056 if (cred.flags.b.initial) {
2057 #endif
2058 if (now < cred.times.endtime) {
2059 *t = (time_t) (cred.times.endtime - now);
2061 krb5_free_cred_contents(context, &cred);
2062 break;
2064 krb5_free_cred_contents(context, &cred);
2067 krb5_cc_end_seq_get(context, id, &cursor);
2069 return kerr;
2071 #endif /* HAVE_KRB5_CC_GET_LIFETIME */
2073 #if !defined(HAVE_KRB5_FREE_CHECKSUM_CONTENTS) && defined(HAVE_FREE_CHECKSUM)
2074 void smb_krb5_free_checksum_contents(krb5_context ctx, krb5_checksum *cksum)
2076 free_Checksum(cksum);
2078 #endif
2080 krb5_error_code smb_krb5_make_pac_checksum(TALLOC_CTX *mem_ctx,
2081 DATA_BLOB *pac_data,
2082 krb5_context context,
2083 const krb5_keyblock *keyblock,
2084 uint32_t *sig_type,
2085 DATA_BLOB *sig_blob)
2087 krb5_error_code ret;
2088 krb5_checksum cksum;
2089 #if defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CREATE_CHECKSUM)
2090 krb5_crypto crypto;
2093 ret = krb5_crypto_init(context,
2094 keyblock,
2096 &crypto);
2097 if (ret) {
2098 DEBUG(0,("krb5_crypto_init() failed: %s\n",
2099 smb_get_krb5_error_message(context, ret, mem_ctx)));
2100 return ret;
2102 ret = krb5_create_checksum(context,
2103 crypto,
2104 KRB5_KU_OTHER_CKSUM,
2106 pac_data->data,
2107 pac_data->length,
2108 &cksum);
2109 if (ret) {
2110 DEBUG(2, ("PAC Verification failed: %s\n",
2111 smb_get_krb5_error_message(context, ret, mem_ctx)));
2114 krb5_crypto_destroy(context, crypto);
2116 if (ret) {
2117 return ret;
2120 *sig_type = cksum.cksumtype;
2121 *sig_blob = data_blob_talloc(mem_ctx,
2122 cksum.checksum.data,
2123 cksum.checksum.length);
2124 #elif defined(HAVE_KRB5_C_MAKE_CHECKSUM)
2125 krb5_data input;
2127 input.data = (char *)pac_data->data;
2128 input.length = pac_data->length;
2130 ret = krb5_c_make_checksum(context,
2132 keyblock,
2133 KRB5_KEYUSAGE_APP_DATA_CKSUM,
2134 &input,
2135 &cksum);
2136 if (ret) {
2137 DEBUG(2, ("PAC Verification failed: %s\n",
2138 smb_get_krb5_error_message(context, ret, mem_ctx)));
2139 return ret;
2142 *sig_type = cksum.checksum_type;
2143 *sig_blob = data_blob_talloc(mem_ctx,
2144 cksum.contents,
2145 cksum.length);
2147 #else
2148 #error krb5_create_checksum or krb5_c_make_checksum not available
2149 #endif /* HAVE_KRB5_C_MAKE_CHECKSUM */
2150 smb_krb5_free_checksum_contents(context, &cksum);
2152 return 0;
2157 * smb_krb5_principal_get_realm
2159 * @brief Get realm of a principal
2161 * @param[in] context The krb5_context
2162 * @param[in] principal The principal
2163 * @return pointer to the realm
2167 char *smb_krb5_principal_get_realm(krb5_context context,
2168 krb5_principal principal)
2170 #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
2171 return discard_const_p(char, krb5_principal_get_realm(context, principal));
2172 #elif defined(krb5_princ_realm) /* MIT */
2173 krb5_data *realm;
2174 realm = krb5_princ_realm(context, principal);
2175 return discard_const_p(char, realm->data);
2176 #else
2177 return NULL;
2178 #endif
2181 /************************************************************************
2182 Routine to get the default realm from the kerberos credentials cache.
2183 Caller must free if the return value is not NULL.
2184 ************************************************************************/
2186 static char *smb_krb5_get_default_realm_from_ccache(TALLOC_CTX *mem_ctx)
2188 char *realm = NULL;
2189 krb5_context ctx = NULL;
2190 krb5_ccache cc = NULL;
2191 krb5_principal princ = NULL;
2193 initialize_krb5_error_table();
2194 if (krb5_init_context(&ctx)) {
2195 return NULL;
2198 DEBUG(5,("kerberos_get_default_realm_from_ccache: "
2199 "Trying to read krb5 cache: %s\n",
2200 krb5_cc_default_name(ctx)));
2201 if (krb5_cc_default(ctx, &cc)) {
2202 DEBUG(0,("kerberos_get_default_realm_from_ccache: "
2203 "failed to read default cache\n"));
2204 goto out;
2206 if (krb5_cc_get_principal(ctx, cc, &princ)) {
2207 DEBUG(0,("kerberos_get_default_realm_from_ccache: "
2208 "failed to get default principal\n"));
2209 goto out;
2212 #if defined(HAVE_KRB5_PRINCIPAL_GET_REALM)
2213 realm = talloc_strdup(mem_ctx, krb5_principal_get_realm(ctx, princ));
2214 #elif defined(HAVE_KRB5_PRINC_REALM)
2216 krb5_data *realm_data = krb5_princ_realm(ctx, princ);
2217 realm = talloc_strndup(mem_ctx, realm_data->data, realm_data->length);
2219 #endif
2221 out:
2223 if (ctx) {
2224 if (princ) {
2225 krb5_free_principal(ctx, princ);
2227 if (cc) {
2228 krb5_cc_close(ctx, cc);
2230 krb5_free_context(ctx);
2233 return realm;
2236 /************************************************************************
2237 Routine to get the realm from a given DNS name.
2238 ************************************************************************/
2240 static char *smb_krb5_get_realm_from_hostname(TALLOC_CTX *mem_ctx,
2241 const char *hostname)
2243 #if defined(HAVE_KRB5_REALM_TYPE)
2244 /* Heimdal. */
2245 krb5_realm *realm_list = NULL;
2246 #else
2247 /* MIT */
2248 char **realm_list = NULL;
2249 #endif
2250 char *realm = NULL;
2251 krb5_error_code kerr;
2252 krb5_context ctx = NULL;
2254 initialize_krb5_error_table();
2255 if (krb5_init_context(&ctx)) {
2256 return NULL;
2259 kerr = krb5_get_host_realm(ctx, hostname, &realm_list);
2260 if (kerr != 0) {
2261 DEBUG(3,("kerberos_get_realm_from_hostname %s: "
2262 "failed %s\n",
2263 hostname ? hostname : "(NULL)",
2264 error_message(kerr) ));
2265 goto out;
2268 if (realm_list && realm_list[0]) {
2269 realm = talloc_strdup(mem_ctx, realm_list[0]);
2272 out:
2274 if (ctx) {
2275 if (realm_list) {
2276 krb5_free_host_realm(ctx, realm_list);
2277 realm_list = NULL;
2279 krb5_free_context(ctx);
2280 ctx = NULL;
2282 return realm;
2285 char *kerberos_get_principal_from_service_hostname(TALLOC_CTX *mem_ctx,
2286 const char *service,
2287 const char *remote_name,
2288 const char *default_realm)
2290 char *realm = NULL;
2291 char *host = NULL;
2292 char *principal;
2293 host = strchr_m(remote_name, '.');
2294 if (host) {
2295 /* DNS name. */
2296 realm = smb_krb5_get_realm_from_hostname(talloc_tos(),
2297 remote_name);
2298 } else {
2299 /* NetBIOS name - use our realm. */
2300 realm = smb_krb5_get_default_realm_from_ccache(talloc_tos());
2303 if (realm == NULL || *realm == '\0') {
2304 realm = talloc_strdup(talloc_tos(), default_realm);
2305 if (!realm) {
2306 return NULL;
2308 DEBUG(3,("kerberos_get_principal_from_service_hostname: "
2309 "cannot get realm from, "
2310 "desthost %s or default ccache. Using default "
2311 "smb.conf realm %s\n",
2312 remote_name,
2313 realm));
2316 principal = talloc_asprintf(mem_ctx,
2317 "%s/%s@%s",
2318 service, remote_name,
2319 realm);
2320 TALLOC_FREE(realm);
2321 return principal;
2324 char *smb_get_krb5_error_message(krb5_context context,
2325 krb5_error_code code,
2326 TALLOC_CTX *mem_ctx)
2328 char *ret;
2330 #if defined(HAVE_KRB5_GET_ERROR_MESSAGE) && defined(HAVE_KRB5_FREE_ERROR_MESSAGE)
2331 const char *context_error = krb5_get_error_message(context, code);
2332 if (context_error) {
2333 ret = talloc_asprintf(mem_ctx, "%s: %s",
2334 error_message(code), context_error);
2335 krb5_free_error_message(context, context_error);
2336 return ret;
2338 #endif
2339 ret = talloc_strdup(mem_ctx, error_message(code));
2340 return ret;
2343 #else /* HAVE_KRB5 */
2344 /* this saves a few linking headaches */
2345 int cli_krb5_get_ticket(TALLOC_CTX *mem_ctx,
2346 const char *principal, time_t time_offset,
2347 DATA_BLOB *ticket, DATA_BLOB *session_key_krb5,
2348 uint32_t extra_ap_opts,
2349 const char *ccname, time_t *tgs_expire,
2350 const char *impersonate_princ_s)
2352 DEBUG(0,("NO KERBEROS SUPPORT\n"));
2353 return 1;
2356 #endif /* HAVE_KRB5 */