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/>.
24 #include "system/filesys.h"
25 #include "krb5_samba.h"
26 #include "lib/util/asn1.h"
30 #endif /* HAVE_COM_ERR_H */
32 #ifndef KRB5_AUTHDATA_WIN2K_PAC
33 #define KRB5_AUTHDATA_WIN2K_PAC 128
36 #ifndef KRB5_AUTHDATA_IF_RELEVANT
37 #define KRB5_AUTHDATA_IF_RELEVANT 1
42 #define GSSAPI_CHECKSUM 0x8003 /* Checksum type value for Kerberos */
43 #define GSSAPI_BNDLENGTH 16 /* Bind Length (rfc-1964 pg.3) */
44 #define GSSAPI_CHECKSUM_SIZE (4+GSSAPI_BNDLENGTH+4) /* Length of bind length,
45 bind field, flags field. */
46 #define GSS_C_DELEG_FLAG 1
48 /* MIT krb5 1.7beta3 (in Ubuntu Karmic) is missing the prototype,
49 but still has the symbol */
50 #if !HAVE_DECL_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE
51 krb5_error_code
krb5_auth_con_set_req_cksumtype(
53 krb5_auth_context auth_context
,
54 krb5_cksumtype cksumtype
);
57 #if !defined(SMB_MALLOC)
59 #define SMB_MALLOC(s) malloc((s))
63 #define SMB_STRDUP(s) strdup(s)
66 #if !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
68 #if defined(HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES)
70 /* With MIT kerberos, we should use krb5_set_default_tgs_enctypes in preference
71 * to krb5_set_default_tgs_ktypes. See
72 * http://lists.samba.org/archive/samba-technical/2006-July/048271.html
74 * If the MIT libraries are not exporting internal symbols, we will end up in
75 * this branch, which is correct. Otherwise we will continue to use the
78 krb5_error_code
krb5_set_default_tgs_ktypes(krb5_context ctx
, const krb5_enctype
*enc
)
80 return krb5_set_default_tgs_enctypes(ctx
, enc
);
83 #elif defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES)
86 krb5_error_code
krb5_set_default_tgs_ktypes(krb5_context ctx
, const krb5_enctype
*enc
)
88 return krb5_set_default_in_tkt_etypes(ctx
, enc
);
91 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES */
93 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_KTYPES */
95 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
97 bool setup_kaddr( krb5_address
*pkaddr
, struct sockaddr_storage
*paddr
)
99 memset(pkaddr
, '\0', sizeof(krb5_address
));
100 #if defined(HAVE_IPV6) && defined(KRB5_ADDRESS_INET6)
101 if (paddr
->ss_family
== AF_INET6
) {
102 pkaddr
->addr_type
= KRB5_ADDRESS_INET6
;
103 pkaddr
->address
.length
= sizeof(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
104 pkaddr
->address
.data
= (char *)&(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
108 if (paddr
->ss_family
== AF_INET
) {
109 pkaddr
->addr_type
= KRB5_ADDRESS_INET
;
110 pkaddr
->address
.length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
111 pkaddr
->address
.data
= (char *)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
116 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
118 bool setup_kaddr( krb5_address
*pkaddr
, struct sockaddr_storage
*paddr
)
120 memset(pkaddr
, '\0', sizeof(krb5_address
));
121 #if defined(HAVE_IPV6) && defined(ADDRTYPE_INET6)
122 if (paddr
->ss_family
== AF_INET6
) {
123 pkaddr
->addrtype
= ADDRTYPE_INET6
;
124 pkaddr
->length
= sizeof(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
125 pkaddr
->contents
= (krb5_octet
*)&(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
129 if (paddr
->ss_family
== AF_INET
) {
130 pkaddr
->addrtype
= ADDRTYPE_INET
;
131 pkaddr
->length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
132 pkaddr
->contents
= (krb5_octet
*)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
138 #error UNKNOWN_ADDRTYPE
142 * @brief Create a keyblock based on input parameters
144 * @param context The krb5_context
145 * @param host_princ The krb5_principal to use
146 * @param salt The optional salt, if ommitted, salt is calculated with
147 * the provided principal.
148 * @param password The krb5_data containing the password
149 * @param enctype The krb5_enctype to use for the keyblock generation
150 * @param key The returned krb5_keyblock, caller needs to free with
151 * krb5_free_keyblock().
153 * @return krb5_error_code
155 int smb_krb5_create_key_from_string(krb5_context context
,
156 krb5_principal
*host_princ
,
159 krb5_enctype enctype
,
164 if (host_princ
== NULL
&& salt
== NULL
) {
168 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_C_STRING_TO_KEY)
173 ret
= krb5_principal2salt(context
, *host_princ
, &_salt
);
175 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret
)));
181 ret
= krb5_c_string_to_key(context
, enctype
, password
, &_salt
, key
);
183 SAFE_FREE(_salt
.data
);
186 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
191 ret
= krb5_get_pw_salt(context
, *host_princ
, &_salt
);
193 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret
)));
197 _salt
.saltvalue
= *salt
;
198 _salt
.salttype
= KRB5_PW_SALT
;
201 ret
= krb5_string_to_key_salt(context
, enctype
, (const char *)password
->data
, _salt
, key
);
203 krb5_free_salt(context
, _salt
);
207 #error UNKNOWN_CREATE_KEY_FUNCTIONS
213 * @brief Create a salt for a given principal
215 * @param context The initialized krb5_context
216 * @param host_princ The krb5_principal to create the salt for
217 * @param psalt A pointer to a krb5_data struct
219 * caller has to free the contents of psalt with kerberos_free_data_contents
220 * when function has succeeded
222 * @return krb5_error_code, returns 0 on success, error code otherwise
225 int smb_krb5_get_pw_salt(krb5_context context
,
226 krb5_principal host_princ
,
228 #if defined(HAVE_KRB5_GET_PW_SALT)
234 ret
= krb5_get_pw_salt(context
, host_princ
, &salt
);
239 psalt
->data
= salt
.saltvalue
.data
;
240 psalt
->length
= salt
.saltvalue
.length
;
244 #elif defined(HAVE_KRB5_PRINCIPAL2SALT)
247 return krb5_principal2salt(context
, host_princ
, psalt
);
250 #error UNKNOWN_SALT_FUNCTIONS
253 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
254 krb5_error_code
get_kerberos_allowed_etypes(krb5_context context
,
255 krb5_enctype
**enctypes
)
257 return krb5_get_permitted_enctypes(context
, enctypes
);
259 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
260 krb5_error_code
get_kerberos_allowed_etypes(krb5_context context
,
261 krb5_enctype
**enctypes
)
263 #ifdef HAVE_KRB5_PDU_NONE_DECL
264 return krb5_get_default_in_tkt_etypes(context
, KRB5_PDU_NONE
, enctypes
);
266 return krb5_get_default_in_tkt_etypes(context
, enctypes
);
270 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
273 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
274 krb5_error_code
krb5_auth_con_setuseruserkey(krb5_context context
,
275 krb5_auth_context auth_context
,
276 krb5_keyblock
*keyblock
)
278 return krb5_auth_con_setkey(context
, auth_context
, keyblock
);
282 bool unwrap_edata_ntstatus(TALLOC_CTX
*mem_ctx
,
284 DATA_BLOB
*edata_out
)
286 DATA_BLOB edata_contents
;
290 if (!edata
->length
) {
294 data
= asn1_init(mem_ctx
);
299 if (!asn1_load(data
, *edata
)) goto err
;
300 if (!asn1_start_tag(data
, ASN1_SEQUENCE(0))) goto err
;
301 if (!asn1_start_tag(data
, ASN1_CONTEXT(1))) goto err
;
302 if (!asn1_read_Integer(data
, &edata_type
)) goto err
;
304 if (edata_type
!= KRB5_PADATA_PW_SALT
) {
305 DEBUG(0,("edata is not of required type %d but of type %d\n",
306 KRB5_PADATA_PW_SALT
, edata_type
));
310 if (!asn1_start_tag(data
, ASN1_CONTEXT(2))) goto err
;
311 if (!asn1_read_OctetString(data
, talloc_tos(), &edata_contents
)) goto err
;
312 if (!asn1_end_tag(data
)) goto err
;
313 if (!asn1_end_tag(data
)) goto err
;
314 if (!asn1_end_tag(data
)) goto err
;
317 *edata_out
= data_blob_talloc(mem_ctx
, edata_contents
.data
, edata_contents
.length
);
319 data_blob_free(&edata_contents
);
330 static bool ads_cleanup_expired_creds(krb5_context context
,
334 krb5_error_code retval
;
335 const char *cc_type
= krb5_cc_get_type(context
, ccache
);
337 DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
338 cc_type
, krb5_cc_get_name(context
, ccache
),
339 http_timestring(talloc_tos(), credsp
->times
.endtime
)));
341 /* we will probably need new tickets if the current ones
342 will expire within 10 seconds.
344 if (credsp
->times
.endtime
>= (time(NULL
) + 10))
347 /* heimdal won't remove creds from a file ccache, and
348 perhaps we shouldn't anyway, since internally we
349 use memory ccaches, and a FILE one probably means that
350 we're using creds obtained outside of our exectuable
352 if (strequal(cc_type
, "FILE")) {
353 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type
));
357 retval
= krb5_cc_remove_cred(context
, ccache
, 0, credsp
);
359 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
360 error_message(retval
)));
361 /* If we have an error in this, we want to display it,
362 but continue as though we deleted it */
367 /* Allocate and setup the auth context into the state we need. */
369 static krb5_error_code
setup_auth_context(krb5_context context
,
370 krb5_auth_context
*auth_context
)
372 krb5_error_code retval
;
374 retval
= krb5_auth_con_init(context
, auth_context
);
376 DEBUG(1,("krb5_auth_con_init failed (%s)\n",
377 error_message(retval
)));
381 /* Ensure this is an addressless ticket. */
382 retval
= krb5_auth_con_setaddrs(context
, *auth_context
, NULL
, NULL
);
384 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n",
385 error_message(retval
)));
391 #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)
392 static krb5_error_code
create_gss_checksum(krb5_data
*in_data
, /* [inout] */
395 unsigned int orig_length
= in_data
->length
;
396 unsigned int base_cksum_size
= GSSAPI_CHECKSUM_SIZE
;
397 char *gss_cksum
= NULL
;
400 /* Extra length field for delgated ticket. */
401 base_cksum_size
+= 4;
404 if ((unsigned int)base_cksum_size
+ orig_length
<
405 (unsigned int)base_cksum_size
) {
409 gss_cksum
= (char *)SMB_MALLOC(base_cksum_size
+ orig_length
);
410 if (gss_cksum
== NULL
) {
414 memset(gss_cksum
, '\0', base_cksum_size
+ orig_length
);
415 SIVAL(gss_cksum
, 0, GSSAPI_BNDLENGTH
);
418 * GSS_C_NO_CHANNEL_BINDINGS means 16 zero bytes.
419 * This matches the behavior of heimdal and mit.
421 * And it is needed to work against some closed source
426 memset(&gss_cksum
[4], 0x00, GSSAPI_BNDLENGTH
);
428 SIVAL(gss_cksum
, 20, gss_flags
);
431 SSVAL(gss_cksum
, 24, 1); /* The Delegation Option identifier */
432 SSVAL(gss_cksum
, 26, orig_length
);
433 /* Copy the kerberos KRB_CRED data */
434 memcpy(gss_cksum
+ 28, in_data
->data
, orig_length
);
436 in_data
->data
= NULL
;
439 in_data
->data
= gss_cksum
;
440 in_data
->length
= base_cksum_size
+ orig_length
;
445 /**************************************************************
446 krb5_parse_name that takes a UNIX charset.
447 **************************************************************/
449 krb5_error_code
smb_krb5_parse_name(krb5_context context
,
450 const char *name
, /* in unix charset */
451 krb5_principal
*principal
)
455 size_t converted_size
;
456 TALLOC_CTX
*frame
= talloc_stackframe();
458 if (!push_utf8_talloc(frame
, &utf8_name
, name
, &converted_size
)) {
463 ret
= krb5_parse_name(context
, utf8_name
, principal
);
468 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
469 void krb5_free_unparsed_name(krb5_context context
, char *val
)
475 /**************************************************************
476 krb5_parse_name that returns a UNIX charset name. Must
477 be freed with talloc_free() call.
478 **************************************************************/
480 krb5_error_code
smb_krb5_unparse_name(TALLOC_CTX
*mem_ctx
,
481 krb5_context context
,
482 krb5_const_principal principal
,
487 size_t converted_size
;
490 ret
= krb5_unparse_name(context
, principal
, &utf8_name
);
495 if (!pull_utf8_talloc(mem_ctx
, unix_name
, utf8_name
, &converted_size
)) {
496 krb5_free_unparsed_name(context
, utf8_name
);
499 krb5_free_unparsed_name(context
, utf8_name
);
503 krb5_error_code
smb_krb5_parse_name_norealm(krb5_context context
,
505 krb5_principal
*principal
)
507 /* we are cheating here because parse_name will in fact set the realm.
508 * We don't care as the only caller of smb_krb5_parse_name_norealm
509 * ignores the realm anyway when calling
510 * smb_krb5_principal_compare_any_realm later - Guenther */
512 return smb_krb5_parse_name(context
, name
, principal
);
515 bool smb_krb5_principal_compare_any_realm(krb5_context context
,
516 krb5_const_principal princ1
,
517 krb5_const_principal princ2
)
519 return krb5_principal_compare_any_realm(context
, princ1
, princ2
);
523 we can't use krb5_mk_req because w2k wants the service to be in a particular format
525 static krb5_error_code
ads_krb5_mk_req(krb5_context context
,
526 krb5_auth_context
*auth_context
,
527 const krb5_flags ap_req_options
,
528 const char *principal
,
532 const char *impersonate_princ_s
)
534 krb5_error_code retval
;
535 krb5_principal server
;
536 krb5_principal impersonate_princ
= NULL
;
540 bool creds_ready
= false;
541 int i
= 0, maxtries
= 3;
543 ZERO_STRUCT(in_data
);
545 retval
= smb_krb5_parse_name(context
, principal
, &server
);
547 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal
));
551 if (impersonate_princ_s
) {
552 retval
= smb_krb5_parse_name(context
, impersonate_princ_s
,
555 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", impersonate_princ_s
));
560 /* obtain ticket & session key */
562 if ((retval
= krb5_copy_principal(context
, server
, &creds
.server
))) {
563 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n",
564 error_message(retval
)));
568 if ((retval
= krb5_cc_get_principal(context
, ccache
, &creds
.client
))) {
569 /* This can commonly fail on smbd startup with no ticket in the cache.
570 * Report at higher level than 1. */
571 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
572 error_message(retval
)));
576 while (!creds_ready
&& (i
< maxtries
)) {
578 if ((retval
= smb_krb5_get_credentials(context
, ccache
,
583 DEBUG(1,("ads_krb5_mk_req: smb_krb5_get_credentials failed for %s (%s)\n",
584 principal
, error_message(retval
)));
588 /* cope with ticket being in the future due to clock skew */
589 if ((unsigned)credsp
->times
.starttime
> time(NULL
)) {
590 time_t t
= time(NULL
);
591 int time_offset
=(int)((unsigned)credsp
->times
.starttime
-t
);
592 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset
));
593 krb5_set_real_time(context
, t
+ time_offset
+ 1, 0);
596 if (!ads_cleanup_expired_creds(context
, ccache
, credsp
)) {
603 DEBUG(10,("ads_krb5_mk_req: Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
604 principal
, krb5_cc_get_type(context
, ccache
), krb5_cc_get_name(context
, ccache
),
605 http_timestring(talloc_tos(), (unsigned)credsp
->times
.endtime
),
606 (unsigned)credsp
->times
.endtime
));
609 *expire_time
= (time_t)credsp
->times
.endtime
;
612 /* Allocate the auth_context. */
613 retval
= setup_auth_context(context
, auth_context
);
615 DEBUG(1,("setup_auth_context failed (%s)\n",
616 error_message(retval
)));
620 #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)
622 uint32_t gss_flags
= 0;
624 if( credsp
->ticket_flags
& TKT_FLG_OK_AS_DELEGATE
) {
625 /* Fetch a forwarded TGT from the KDC so that we can hand off a 2nd ticket
626 as part of the kerberos exchange. */
628 DEBUG( 3, ("ads_krb5_mk_req: server marked as OK to delegate to, building forwardable TGT\n") );
630 retval
= krb5_auth_con_setuseruserkey(context
,
634 DEBUG(1,("krb5_auth_con_setuseruserkey failed (%s)\n",
635 error_message(retval
)));
639 /* Must use a subkey for forwarded tickets. */
640 retval
= krb5_auth_con_setflags(context
,
642 KRB5_AUTH_CONTEXT_USE_SUBKEY
);
644 DEBUG(1,("krb5_auth_con_setflags failed (%s)\n",
645 error_message(retval
)));
649 retval
= krb5_fwd_tgt_creds(context
,/* Krb5 context [in] */
650 *auth_context
, /* Authentication context [in] */
651 discard_const_p(char, KRB5_TGS_NAME
), /* Ticket service name ("krbtgt") [in] */
652 credsp
->client
, /* Client principal for the tgt [in] */
653 credsp
->server
, /* Server principal for the tgt [in] */
654 ccache
, /* Credential cache to use for storage [in] */
655 1, /* Turn on for "Forwardable ticket" [in] */
656 &in_data
); /* Resulting response [out] */
659 DEBUG( 3, ("krb5_fwd_tgt_creds failed (%s)\n",
660 error_message( retval
) ) );
663 * This is not fatal. Delete the *auth_context and continue
664 * with krb5_mk_req_extended to get a non-forwardable ticket.
668 free( in_data
.data
);
672 krb5_auth_con_free(context
, *auth_context
);
673 *auth_context
= NULL
;
674 retval
= setup_auth_context(context
, auth_context
);
676 DEBUG(1,("setup_auth_context failed (%s)\n",
677 error_message(retval
)));
681 /* We got a delegated ticket. */
682 gss_flags
|= GSS_C_DELEG_FLAG
;
686 /* Frees and reallocates in_data into a GSS checksum blob. */
687 retval
= create_gss_checksum(&in_data
, gss_flags
);
692 /* We always want GSS-checksum types. */
693 retval
= krb5_auth_con_set_req_cksumtype(context
, *auth_context
, GSSAPI_CHECKSUM
);
695 DEBUG(1,("krb5_auth_con_set_req_cksumtype failed (%s)\n",
696 error_message(retval
)));
702 retval
= krb5_mk_req_extended(context
, auth_context
, ap_req_options
,
703 &in_data
, credsp
, outbuf
);
705 DEBUG(1,("ads_krb5_mk_req: krb5_mk_req_extended failed (%s)\n",
706 error_message(retval
)));
709 #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)
714 free( in_data
.data
);
718 krb5_free_creds(context
, credsp
);
721 krb5_free_cred_contents(context
, &creds
);
724 krb5_free_principal(context
, server
);
725 if (impersonate_princ
) {
726 krb5_free_principal(context
, impersonate_princ
);
732 void kerberos_free_data_contents(krb5_context context
, krb5_data
*pdata
)
734 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
736 krb5_free_data_contents(context
, pdata
);
738 #elif defined(HAVE_KRB5_DATA_FREE)
739 krb5_data_free(context
, pdata
);
741 SAFE_FREE(pdata
->data
);
746 * @brief copy a buffer into a krb5_data struct
748 * @param[in] p The krb5_data
749 * @param[in] data The data to copy
750 * @param[in] length The length of the data to copy
751 * @return krb5_error_code
753 * Caller has to free krb5_data with kerberos_free_data_contents().
756 krb5_error_code
krb5_copy_data_contents(krb5_data
*p
,
760 #if defined(HAVE_KRB5_DATA_COPY)
761 return krb5_data_copy(p
, data
, len
);
764 p
->data
= malloc(len
);
765 if (p
->data
== NULL
) {
768 memmove(p
->data
, data
, len
);
773 p
->magic
= KV5M_DATA
;
779 get a kerberos5 ticket for the given service
781 int cli_krb5_get_ticket(TALLOC_CTX
*mem_ctx
,
782 const char *principal
, time_t time_offset
,
783 DATA_BLOB
*ticket
, DATA_BLOB
*session_key_krb5
,
784 uint32_t extra_ap_opts
, const char *ccname
,
786 const char *impersonate_princ_s
)
789 krb5_error_code retval
;
791 krb5_context context
= NULL
;
792 krb5_ccache ccdef
= NULL
;
793 krb5_auth_context auth_context
= NULL
;
794 krb5_enctype enc_types
[] = {
795 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
796 ENCTYPE_AES256_CTS_HMAC_SHA1_96
,
798 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
799 ENCTYPE_AES128_CTS_HMAC_SHA1_96
,
801 ENCTYPE_ARCFOUR_HMAC
,
806 initialize_krb5_error_table();
807 retval
= krb5_init_context(&context
);
809 DEBUG(1, ("krb5_init_context failed (%s)\n",
810 error_message(retval
)));
814 if (time_offset
!= 0) {
815 krb5_set_real_time(context
, time(NULL
) + time_offset
, 0);
818 if ((retval
= krb5_cc_resolve(context
, ccname
?
819 ccname
: krb5_cc_default_name(context
), &ccdef
))) {
820 DEBUG(1, ("krb5_cc_default failed (%s)\n",
821 error_message(retval
)));
825 if ((retval
= krb5_set_default_tgs_ktypes(context
, enc_types
))) {
826 DEBUG(1, ("krb5_set_default_tgs_ktypes failed (%s)\n",
827 error_message(retval
)));
831 retval
= ads_krb5_mk_req(context
, &auth_context
,
832 AP_OPTS_USE_SUBKEY
| (krb5_flags
)extra_ap_opts
,
833 principal
, ccdef
, &packet
,
834 tgs_expire
, impersonate_princ_s
);
839 get_krb5_smb_session_key(mem_ctx
, context
, auth_context
,
840 session_key_krb5
, false);
842 *ticket
= data_blob_talloc(mem_ctx
, packet
.data
, packet
.length
);
844 kerberos_free_data_contents(context
, &packet
);
850 krb5_cc_close(context
, ccdef
);
852 krb5_auth_con_free(context
, auth_context
);
853 krb5_free_context(context
);
859 bool get_krb5_smb_session_key(TALLOC_CTX
*mem_ctx
,
860 krb5_context context
,
861 krb5_auth_context auth_context
,
862 DATA_BLOB
*session_key
, bool remote
)
864 krb5_keyblock
*skey
= NULL
;
865 krb5_error_code err
= 0;
869 err
= krb5_auth_con_getremotesubkey(context
,
870 auth_context
, &skey
);
872 err
= krb5_auth_con_getlocalsubkey(context
,
873 auth_context
, &skey
);
876 if (err
|| skey
== NULL
) {
877 DEBUG(10, ("KRB5 error getting session key %d\n", err
));
881 DEBUG(10, ("Got KRB5 session key of length %d\n",
882 (int)KRB5_KEY_LENGTH(skey
)));
884 *session_key
= data_blob_talloc(mem_ctx
,
886 KRB5_KEY_LENGTH(skey
));
887 dump_data_pw("KRB5 Session Key:\n",
889 session_key
->length
);
895 krb5_free_keyblock(context
, skey
);
902 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
903 const krb5_data
*krb5_princ_component(krb5_context context
, krb5_principal principal
, int i
);
905 const krb5_data
*krb5_princ_component(krb5_context context
, krb5_principal principal
, int i
)
907 static krb5_data kdata
;
909 kdata
.data
= discard_const_p(char, krb5_principal_get_comp_string(context
, principal
, i
));
910 kdata
.length
= strlen((const char *)kdata
.data
);
916 * @brief Get talloced string component of a principal
918 * @param[in] mem_ctx The TALLOC_CTX
919 * @param[in] context The krb5_context
920 * @param[in] principal The principal
921 * @param[in] component The component
922 * @return string component
924 * Caller must talloc_free if the return value is not NULL.
928 /* caller has to free returned string with free() */
929 char *smb_krb5_principal_get_comp_string(TALLOC_CTX
*mem_ctx
,
930 krb5_context context
,
931 krb5_const_principal principal
,
932 unsigned int component
)
934 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
935 return talloc_strdup(mem_ctx
, krb5_principal_get_comp_string(context
, principal
, component
));
939 if (component
>= krb5_princ_size(context
, principal
)) {
943 data
= krb5_princ_component(context
, principal
, component
);
948 return talloc_strndup(mem_ctx
, data
->data
, data
->length
);
954 krb5_error_code
smb_krb5_renew_ticket(const char *ccache_string
, /* FILE:/tmp/krb5cc_0 */
955 const char *client_string
, /* gd@BER.SUSE.DE */
956 const char *service_string
, /* krbtgt/BER.SUSE.DE@BER.SUSE.DE */
960 krb5_context context
= NULL
;
961 krb5_ccache ccache
= NULL
;
962 krb5_principal client
= NULL
;
963 krb5_creds creds
, creds_in
;
966 ZERO_STRUCT(creds_in
);
968 initialize_krb5_error_table();
969 ret
= krb5_init_context(&context
);
974 if (!ccache_string
) {
975 ccache_string
= krb5_cc_default_name(context
);
978 if (!ccache_string
) {
983 DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string
));
985 /* FIXME: we should not fall back to defaults */
986 ret
= krb5_cc_resolve(context
, discard_const_p(char, ccache_string
), &ccache
);
992 ret
= smb_krb5_parse_name(context
, client_string
, &client
);
997 ret
= krb5_cc_get_principal(context
, ccache
, &client
);
1003 ret
= krb5_get_renewed_creds(context
, &creds
, client
, ccache
, discard_const_p(char, service_string
));
1005 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret
)));
1009 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
1010 ret
= krb5_cc_initialize(context
, ccache
, client
);
1015 ret
= krb5_cc_store_cred(context
, ccache
, &creds
);
1018 *expire_time
= (time_t) creds
.times
.endtime
;
1022 krb5_free_cred_contents(context
, &creds_in
);
1023 krb5_free_cred_contents(context
, &creds
);
1026 krb5_free_principal(context
, client
);
1029 krb5_cc_close(context
, ccache
);
1032 krb5_free_context(context
);
1038 krb5_error_code
smb_krb5_free_addresses(krb5_context context
, smb_krb5_addresses
*addr
)
1040 krb5_error_code ret
= 0;
1044 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1045 krb5_free_addresses(context
, addr
->addrs
);
1046 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1047 ret
= krb5_free_addresses(context
, addr
->addrs
);
1048 SAFE_FREE(addr
->addrs
);
1055 #define MAX_NETBIOSNAME_LEN 16
1056 krb5_error_code
smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses
**kerb_addr
,
1057 const char *netbios_name
)
1059 krb5_error_code ret
= 0;
1060 char buf
[MAX_NETBIOSNAME_LEN
];
1062 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1063 krb5_address
**addrs
= NULL
;
1064 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1065 krb5_addresses
*addrs
= NULL
;
1068 *kerb_addr
= (smb_krb5_addresses
*)SMB_MALLOC(sizeof(smb_krb5_addresses
));
1069 if (*kerb_addr
== NULL
) {
1073 /* temporarily duplicate put_name() code here to avoid dependency
1074 * issues for a 5 lines function */
1075 len
= strlen(netbios_name
);
1076 memcpy(buf
, netbios_name
,
1077 (len
< MAX_NETBIOSNAME_LEN
) ? len
: MAX_NETBIOSNAME_LEN
- 1);
1078 if (len
< MAX_NETBIOSNAME_LEN
- 1) {
1079 memset(buf
+ len
, ' ', MAX_NETBIOSNAME_LEN
- 1 - len
);
1081 buf
[MAX_NETBIOSNAME_LEN
- 1] = 0x20;
1083 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1087 addrs
= (krb5_address
**)SMB_MALLOC(sizeof(krb5_address
*) * num_addr
);
1088 if (addrs
== NULL
) {
1089 SAFE_FREE(*kerb_addr
);
1093 memset(addrs
, 0, sizeof(krb5_address
*) * num_addr
);
1095 addrs
[0] = (krb5_address
*)SMB_MALLOC(sizeof(krb5_address
));
1096 if (addrs
[0] == NULL
) {
1098 SAFE_FREE(*kerb_addr
);
1102 addrs
[0]->magic
= KV5M_ADDRESS
;
1103 addrs
[0]->addrtype
= KRB5_ADDR_NETBIOS
;
1104 addrs
[0]->length
= MAX_NETBIOSNAME_LEN
;
1105 addrs
[0]->contents
= (unsigned char *)SMB_MALLOC(addrs
[0]->length
);
1106 if (addrs
[0]->contents
== NULL
) {
1107 SAFE_FREE(addrs
[0]);
1109 SAFE_FREE(*kerb_addr
);
1113 memcpy(addrs
[0]->contents
, buf
, addrs
[0]->length
);
1117 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1119 addrs
= (krb5_addresses
*)SMB_MALLOC(sizeof(krb5_addresses
));
1120 if (addrs
== NULL
) {
1121 SAFE_FREE(*kerb_addr
);
1125 memset(addrs
, 0, sizeof(krb5_addresses
));
1128 addrs
->val
= (krb5_address
*)SMB_MALLOC(sizeof(krb5_address
));
1129 if (addrs
->val
== NULL
) {
1131 SAFE_FREE(kerb_addr
);
1135 addrs
->val
[0].addr_type
= KRB5_ADDR_NETBIOS
;
1136 addrs
->val
[0].address
.length
= MAX_NETBIOSNAME_LEN
;
1137 addrs
->val
[0].address
.data
= (unsigned char *)SMB_MALLOC(addrs
->val
[0].address
.length
);
1138 if (addrs
->val
[0].address
.data
== NULL
) {
1139 SAFE_FREE(addrs
->val
);
1141 SAFE_FREE(*kerb_addr
);
1145 memcpy(addrs
->val
[0].address
.data
, buf
, addrs
->val
[0].address
.length
);
1148 #error UNKNOWN_KRB5_ADDRESS_FORMAT
1150 (*kerb_addr
)->addrs
= addrs
;
1155 void smb_krb5_free_error(krb5_context context
, krb5_error
*krberror
)
1157 #ifdef HAVE_KRB5_FREE_ERROR_CONTENTS /* Heimdal */
1158 krb5_free_error_contents(context
, krberror
);
1160 krb5_free_error(context
, krberror
);
1164 krb5_error_code
handle_krberror_packet(krb5_context context
,
1167 krb5_error_code ret
;
1168 bool got_error_code
= false;
1170 DEBUG(10,("handle_krberror_packet: got error packet\n"));
1172 #ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR /* Heimdal */
1174 krb5_error krberror
;
1176 if ((ret
= krb5_rd_error(context
, packet
, &krberror
))) {
1177 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1178 error_message(ret
)));
1182 if (krberror
.e_data
== NULL
|| krberror
.e_data
->data
== NULL
) {
1183 ret
= (krb5_error_code
) krberror
.error_code
;
1184 got_error_code
= true;
1187 smb_krb5_free_error(context
, &krberror
);
1191 krb5_error
*krberror
;
1193 if ((ret
= krb5_rd_error(context
, packet
, &krberror
))) {
1194 DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
1195 error_message(ret
)));
1199 if (krberror
->e_data
.data
== NULL
) {
1200 #if defined(ERROR_TABLE_BASE_krb5)
1201 ret
= ERROR_TABLE_BASE_krb5
+ (krb5_error_code
) krberror
->error
;
1203 ret
= (krb5_error_code
)krberror
->error
;
1205 got_error_code
= true;
1207 smb_krb5_free_error(context
, krberror
);
1210 if (got_error_code
) {
1211 DEBUG(5,("handle_krberror_packet: got KERBERR from kpasswd: %s (%d)\n",
1212 error_message(ret
), ret
));
1217 krb5_error_code
smb_krb5_get_init_creds_opt_alloc(krb5_context context
,
1218 krb5_get_init_creds_opt
**opt
)
1220 /* Heimdal or modern MIT version */
1221 return krb5_get_init_creds_opt_alloc(context
, opt
);
1224 void smb_krb5_get_init_creds_opt_free(krb5_context context
,
1225 krb5_get_init_creds_opt
*opt
)
1227 /* Modern MIT or Heimdal version */
1228 krb5_get_init_creds_opt_free(context
, opt
);
1231 krb5_enctype
smb_get_enctype_from_kt_entry(krb5_keytab_entry
*kt_entry
)
1233 return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry
));
1236 krb5_error_code
smb_krb5_kt_free_entry(krb5_context context
,
1237 krb5_keytab_entry
*kt_entry
)
1239 /* Try krb5_free_keytab_entry_contents first, since
1240 * MIT Kerberos >= 1.7 has both krb5_free_keytab_entry_contents and
1241 * krb5_kt_free_entry but only has a prototype for the first, while the
1242 * second is considered private.
1244 #if defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
1245 return krb5_free_keytab_entry_contents(context
, kt_entry
);
1246 #elif defined(HAVE_KRB5_KT_FREE_ENTRY)
1247 return krb5_kt_free_entry(context
, kt_entry
);
1249 #error UNKNOWN_KT_FREE_FUNCTION
1254 /* caller needs to free etype_s */
1255 krb5_error_code
smb_krb5_enctype_to_string(krb5_context context
,
1256 krb5_enctype enctype
,
1259 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
1260 return krb5_enctype_to_string(context
, enctype
, etype_s
); /* Heimdal */
1261 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
1263 krb5_error_code ret
= krb5_enctype_to_string(enctype
, buf
, 256); /* MIT */
1267 *etype_s
= SMB_STRDUP(buf
);
1273 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
1277 /**********************************************************************
1278 * Open a krb5 keytab with flags, handles readonly or readwrite access and
1279 * allows to process non-default keytab names.
1280 * @param context krb5_context
1281 * @param keytab_name_req string
1282 * @param write_access bool if writable keytab is required
1283 * @param krb5_keytab pointer to krb5_keytab (close with krb5_kt_close())
1284 * @return krb5_error_code
1285 **********************************************************************/
1287 /* This MAX_NAME_LEN is a constant defined in krb5.h */
1288 #ifndef MAX_KEYTAB_NAME_LEN
1289 #define MAX_KEYTAB_NAME_LEN 1100
1292 krb5_error_code
smb_krb5_open_keytab(krb5_context context
,
1293 const char *keytab_name_req
,
1295 krb5_keytab
*keytab
)
1297 krb5_error_code ret
= 0;
1298 TALLOC_CTX
*mem_ctx
;
1299 char keytab_string
[MAX_KEYTAB_NAME_LEN
];
1300 char *kt_str
= NULL
;
1301 bool found_valid_name
= false;
1302 const char *pragma
= "FILE";
1303 const char *tmp
= NULL
;
1305 if (!write_access
&& !keytab_name_req
) {
1306 /* caller just wants to read the default keytab readonly, so be it */
1307 return krb5_kt_default(context
, keytab
);
1310 mem_ctx
= talloc_init("smb_krb5_open_keytab");
1315 #ifdef HAVE_WRFILE_KEYTAB
1321 if (keytab_name_req
) {
1323 if (strlen(keytab_name_req
) > MAX_KEYTAB_NAME_LEN
) {
1324 ret
= KRB5_CONFIG_NOTENUFSPACE
;
1328 if ((strncmp(keytab_name_req
, "WRFILE:/", 8) == 0) ||
1329 (strncmp(keytab_name_req
, "FILE:/", 6) == 0)) {
1330 tmp
= keytab_name_req
;
1334 if (keytab_name_req
[0] != '/') {
1335 ret
= KRB5_KT_BADNAME
;
1339 tmp
= talloc_asprintf(mem_ctx
, "%s:%s", pragma
, keytab_name_req
);
1348 /* we need to handle more complex keytab_strings, like:
1349 * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
1351 ret
= krb5_kt_default_name(context
, &keytab_string
[0], MAX_KEYTAB_NAME_LEN
- 2);
1356 DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string
));
1358 tmp
= talloc_strdup(mem_ctx
, keytab_string
);
1364 if (strncmp(tmp
, "ANY:", 4) == 0) {
1368 memset(&keytab_string
, '\0', sizeof(keytab_string
));
1370 while (next_token_talloc(mem_ctx
, &tmp
, &kt_str
, ",")) {
1371 if (strncmp(kt_str
, "WRFILE:", 7) == 0) {
1372 found_valid_name
= true;
1377 if (strncmp(kt_str
, "FILE:", 5) == 0) {
1378 found_valid_name
= true;
1383 if (tmp
[0] == '/') {
1384 /* Treat as a FILE: keytab definition. */
1385 found_valid_name
= true;
1388 if (found_valid_name
) {
1389 if (tmp
[0] != '/') {
1390 ret
= KRB5_KT_BADNAME
;
1394 tmp
= talloc_asprintf(mem_ctx
, "%s:%s", pragma
, tmp
);
1403 if (!found_valid_name
) {
1404 ret
= KRB5_KT_UNKNOWN_TYPE
;
1409 DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp
));
1410 ret
= krb5_kt_resolve(context
, tmp
, keytab
);
1413 TALLOC_FREE(mem_ctx
);
1417 krb5_error_code
smb_krb5_keytab_name(TALLOC_CTX
*mem_ctx
,
1418 krb5_context context
,
1420 const char **keytab_name
)
1422 char keytab_string
[MAX_KEYTAB_NAME_LEN
];
1423 krb5_error_code ret
= 0;
1425 ret
= krb5_kt_get_name(context
, keytab
,
1426 keytab_string
, MAX_KEYTAB_NAME_LEN
- 2);
1431 *keytab_name
= talloc_strdup(mem_ctx
, keytab_string
);
1432 if (!*keytab_name
) {
1439 #if defined(HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE) && \
1440 defined(HAVE_KRB5_GET_CREDS_OPT_ALLOC) && \
1441 defined(HAVE_KRB5_GET_CREDS)
1442 static krb5_error_code
smb_krb5_get_credentials_for_user_opt(krb5_context context
,
1445 krb5_principal server
,
1446 krb5_principal impersonate_princ
,
1447 krb5_creds
**out_creds
)
1449 krb5_error_code ret
;
1450 krb5_get_creds_opt opt
;
1452 ret
= krb5_get_creds_opt_alloc(context
, &opt
);
1456 krb5_get_creds_opt_add_options(context
, opt
, KRB5_GC_FORWARDABLE
);
1458 if (impersonate_princ
) {
1459 ret
= krb5_get_creds_opt_set_impersonate(context
, opt
,
1466 ret
= krb5_get_creds(context
, opt
, ccache
, server
, out_creds
);
1473 krb5_get_creds_opt_free(context
, opt
);
1477 #endif /* HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE */
1479 #ifdef HAVE_KRB5_GET_CREDENTIALS_FOR_USER
1481 #if !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER
1482 krb5_error_code KRB5_CALLCONV
1483 krb5_get_credentials_for_user(krb5_context context
, krb5_flags options
,
1484 krb5_ccache ccache
, krb5_creds
*in_creds
,
1485 krb5_data
*subject_cert
,
1486 krb5_creds
**out_creds
);
1487 #endif /* !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER */
1489 static krb5_error_code
smb_krb5_get_credentials_for_user(krb5_context context
,
1492 krb5_principal server
,
1493 krb5_principal impersonate_princ
,
1494 krb5_creds
**out_creds
)
1496 krb5_error_code ret
;
1497 krb5_creds in_creds
;
1499 ZERO_STRUCT(in_creds
);
1501 if (impersonate_princ
) {
1503 in_creds
.server
= me
;
1504 in_creds
.client
= impersonate_princ
;
1506 ret
= krb5_get_credentials_for_user(context
,
1507 0, /* krb5_flags options */
1510 NULL
, /* krb5_data *subject_cert */
1513 in_creds
.client
= me
;
1514 in_creds
.server
= server
;
1516 ret
= krb5_get_credentials(context
, 0, ccache
,
1517 &in_creds
, out_creds
);
1522 #endif /* HAVE_KRB5_GET_CREDENTIALS_FOR_USER */
1525 * smb_krb5_get_credentials
1527 * @brief Get krb5 credentials for a server
1529 * @param[in] context An initialized krb5_context
1530 * @param[in] ccache An initialized krb5_ccache
1531 * @param[in] me The krb5_principal of the caller
1532 * @param[in] server The krb5_principal of the requested service
1533 * @param[in] impersonate_princ The krb5_principal of a user to impersonate as (optional)
1534 * @param[out] out_creds The returned krb5_creds structure
1535 * @return krb5_error_code
1538 krb5_error_code
smb_krb5_get_credentials(krb5_context context
,
1541 krb5_principal server
,
1542 krb5_principal impersonate_princ
,
1543 krb5_creds
**out_creds
)
1545 krb5_error_code ret
;
1546 krb5_creds
*creds
= NULL
;
1548 if (out_creds
!= NULL
) {
1552 if (impersonate_princ
) {
1553 #ifdef HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE /* Heimdal */
1554 ret
= smb_krb5_get_credentials_for_user_opt(context
, ccache
, me
, server
, impersonate_princ
, &creds
);
1555 #elif defined(HAVE_KRB5_GET_CREDENTIALS_FOR_USER) /* MIT */
1556 ret
= smb_krb5_get_credentials_for_user(context
, ccache
, me
, server
, impersonate_princ
, &creds
);
1561 krb5_creds in_creds
;
1563 ZERO_STRUCT(in_creds
);
1565 in_creds
.client
= me
;
1566 in_creds
.server
= server
;
1568 ret
= krb5_get_credentials(context
, 0, ccache
,
1581 krb5_free_creds(context
, creds
);
1587 krb5_error_code
smb_krb5_keyblock_init_contents(krb5_context context
,
1588 krb5_enctype enctype
,
1593 #if defined(HAVE_KRB5_KEYBLOCK_INIT)
1594 return krb5_keyblock_init(context
, enctype
, data
, length
, key
);
1596 memset(key
, 0, sizeof(krb5_keyblock
));
1597 KRB5_KEY_DATA(key
) = SMB_MALLOC(length
);
1598 if (NULL
== KRB5_KEY_DATA(key
)) {
1601 memcpy(KRB5_KEY_DATA(key
), data
, length
);
1602 KRB5_KEY_LENGTH(key
) = length
;
1603 KRB5_KEY_TYPE(key
) = enctype
;
1609 simulate a kinit, putting the tgt in the given credentials cache.
1610 Orignally by remus@snapserver.com
1612 This version is built to use a keyblock, rather than needing the
1615 The impersonate_principal is the principal if NULL, or the principal
1618 The target_service defaults to the krbtgt if NULL, but could be
1619 kpasswd/realm or the local service (if we are doing s4u2self)
1621 krb5_error_code
kerberos_kinit_keyblock_cc(krb5_context ctx
, krb5_ccache cc
,
1622 krb5_principal principal
,
1623 krb5_keyblock
*keyblock
,
1624 const char *target_service
,
1625 krb5_get_init_creds_opt
*krb_options
,
1626 time_t *expire_time
,
1629 krb5_error_code code
= 0;
1630 krb5_creds my_creds
;
1632 #if defined(HAVE_KRB5_GET_INIT_CREDS_KEYBLOCK)
1633 code
= krb5_get_init_creds_keyblock(ctx
, &my_creds
, principal
,
1634 keyblock
, 0, target_service
,
1636 #elif defined(HAVE_KRB5_GET_INIT_CREDS_KEYTAB)
1638 #define SMB_CREDS_KEYTAB "MEMORY:tmp_smb_creds_XXXXXX"
1639 char tmp_name
[sizeof(SMB_CREDS_KEYTAB
)];
1640 krb5_keytab_entry entry
;
1644 memset(&entry
, 0, sizeof(entry
));
1645 entry
.principal
= principal
;
1646 *(KRB5_KT_KEY(&entry
)) = *keyblock
;
1648 memcpy(tmp_name
, SMB_CREDS_KEYTAB
, sizeof(SMB_CREDS_KEYTAB
));
1649 mask
= umask(S_IRWXO
| S_IRWXG
);
1652 if (tmp_name
[0] == 0) {
1653 return KRB5_KT_BADNAME
;
1655 code
= krb5_kt_resolve(ctx
, tmp_name
, &keytab
);
1660 code
= krb5_kt_add_entry(ctx
, keytab
, &entry
);
1662 (void)krb5_kt_close(ctx
, keytab
);
1666 code
= krb5_get_init_creds_keytab(ctx
, &my_creds
, principal
,
1667 keytab
, 0, target_service
,
1669 (void)krb5_kt_close(ctx
, keytab
);
1672 #error krb5_get_init_creds_keyblock not available!
1678 code
= krb5_cc_initialize(ctx
, cc
, principal
);
1683 code
= krb5_cc_store_cred(ctx
, cc
, &my_creds
);
1689 *expire_time
= (time_t) my_creds
.times
.endtime
;
1693 *kdc_time
= (time_t) my_creds
.times
.starttime
;
1698 krb5_free_cred_contents(ctx
, &my_creds
);
1702 krb5_error_code
kerberos_kinit_password_cc(krb5_context ctx
, krb5_ccache cc
,
1703 krb5_principal principal
,
1704 const char *password
,
1705 const char *target_service
,
1706 krb5_get_init_creds_opt
*krb_options
,
1707 time_t *expire_time
,
1710 krb5_error_code code
= 0;
1711 krb5_creds my_creds
;
1713 code
= krb5_get_init_creds_password(ctx
, &my_creds
, principal
,
1714 password
, NULL
, NULL
, 0,
1715 target_service
, krb_options
);
1720 code
= krb5_cc_initialize(ctx
, cc
, principal
);
1725 code
= krb5_cc_store_cred(ctx
, cc
, &my_creds
);
1731 *expire_time
= (time_t) my_creds
.times
.endtime
;
1735 *kdc_time
= (time_t) my_creds
.times
.starttime
;
1740 krb5_free_cred_contents(ctx
, &my_creds
);
1744 #ifdef SAMBA4_USES_HEIMDAL
1746 simulate a kinit, putting the tgt in the given credentials cache.
1747 Orignally by remus@snapserver.com
1749 The impersonate_principal is the principal
1751 The self_service, should be the local service (for S4U2Self if
1752 impersonate_principal is given).
1754 The target_service defaults to the krbtgt if NULL, but could be
1755 kpasswd/realm or a remote service (for S4U2Proxy)
1758 krb5_error_code
kerberos_kinit_s4u2_cc(krb5_context ctx
,
1759 krb5_ccache store_cc
,
1760 krb5_principal init_principal
,
1761 const char *init_password
,
1762 krb5_principal impersonate_principal
,
1763 const char *self_service
,
1764 const char *target_service
,
1765 krb5_get_init_creds_opt
*krb_options
,
1766 time_t *expire_time
,
1769 krb5_error_code code
= 0;
1770 krb5_get_creds_opt options
;
1771 krb5_principal store_principal
;
1772 krb5_creds store_creds
;
1773 krb5_creds
*s4u2self_creds
;
1774 Ticket s4u2self_ticket
;
1775 size_t s4u2self_ticketlen
;
1776 krb5_creds
*s4u2proxy_creds
;
1777 krb5_principal self_princ
;
1779 krb5_principal target_princ
;
1781 const char *self_realm
;
1782 krb5_principal blacklist_principal
= NULL
;
1783 krb5_principal whitelist_principal
= NULL
;
1785 code
= krb5_get_init_creds_password(ctx
, &store_creds
,
1796 store_principal
= init_principal
;
1799 * We are trying S4U2Self now:
1801 * As we do not want to expose our TGT in the
1802 * krb5_ccache, which is also holds the impersonated creds.
1804 * Some low level krb5/gssapi function might use the TGT
1805 * identity and let the client act as our machine account.
1807 * We need to avoid that and use a temporary krb5_ccache
1808 * in order to pass our TGT to the krb5_get_creds() function.
1810 code
= krb5_cc_new_unique(ctx
, NULL
, NULL
, &tmp_cc
);
1812 krb5_free_cred_contents(ctx
, &store_creds
);
1816 code
= krb5_cc_initialize(ctx
, tmp_cc
, store_creds
.client
);
1818 krb5_cc_destroy(ctx
, tmp_cc
);
1819 krb5_free_cred_contents(ctx
, &store_creds
);
1823 code
= krb5_cc_store_cred(ctx
, tmp_cc
, &store_creds
);
1825 krb5_free_cred_contents(ctx
, &store_creds
);
1826 krb5_cc_destroy(ctx
, tmp_cc
);
1831 * we need to remember the client principal of our
1832 * TGT and make sure the KDC does not return this
1833 * in the impersonated tickets. This can happen
1834 * if the KDC does not support S4U2Self and S4U2Proxy.
1836 blacklist_principal
= store_creds
.client
;
1837 store_creds
.client
= NULL
;
1838 krb5_free_cred_contents(ctx
, &store_creds
);
1841 * Check if we also need S4U2Proxy or if S4U2Self is
1842 * enough in order to get a ticket for the target.
1844 if (target_service
== NULL
) {
1846 } else if (strcmp(target_service
, self_service
) == 0) {
1853 * For S4U2Self we need our own service principal,
1854 * which belongs to our own realm (available on
1855 * our client principal).
1857 self_realm
= krb5_principal_get_realm(ctx
, init_principal
);
1859 code
= krb5_parse_name(ctx
, self_service
, &self_princ
);
1861 krb5_free_principal(ctx
, blacklist_principal
);
1862 krb5_cc_destroy(ctx
, tmp_cc
);
1866 code
= krb5_principal_set_realm(ctx
, self_princ
, self_realm
);
1868 krb5_free_principal(ctx
, blacklist_principal
);
1869 krb5_free_principal(ctx
, self_princ
);
1870 krb5_cc_destroy(ctx
, tmp_cc
);
1874 code
= krb5_get_creds_opt_alloc(ctx
, &options
);
1876 krb5_free_principal(ctx
, blacklist_principal
);
1877 krb5_free_principal(ctx
, self_princ
);
1878 krb5_cc_destroy(ctx
, tmp_cc
);
1884 * If we want S4U2Proxy, we need the forwardable flag
1885 * on the S4U2Self ticket.
1887 krb5_get_creds_opt_set_options(ctx
, options
, KRB5_GC_FORWARDABLE
);
1890 code
= krb5_get_creds_opt_set_impersonate(ctx
, options
,
1891 impersonate_principal
);
1893 krb5_get_creds_opt_free(ctx
, options
);
1894 krb5_free_principal(ctx
, blacklist_principal
);
1895 krb5_free_principal(ctx
, self_princ
);
1896 krb5_cc_destroy(ctx
, tmp_cc
);
1900 code
= krb5_get_creds(ctx
, options
, tmp_cc
,
1901 self_princ
, &s4u2self_creds
);
1902 krb5_get_creds_opt_free(ctx
, options
);
1903 krb5_free_principal(ctx
, self_princ
);
1905 krb5_free_principal(ctx
, blacklist_principal
);
1906 krb5_cc_destroy(ctx
, tmp_cc
);
1911 krb5_cc_destroy(ctx
, tmp_cc
);
1914 * Now make sure we store the impersonated principal
1915 * and creds instead of the TGT related stuff
1916 * in the krb5_ccache of the caller.
1918 code
= krb5_copy_creds_contents(ctx
, s4u2self_creds
,
1920 krb5_free_creds(ctx
, s4u2self_creds
);
1926 * It's important to store the principal the KDC
1927 * returned, as otherwise the caller would not find
1928 * the S4U2Self ticket in the krb5_ccache lookup.
1930 store_principal
= store_creds
.client
;
1935 * We are trying S4U2Proxy:
1937 * We need the ticket from the S4U2Self step
1938 * and our TGT in order to get the delegated ticket.
1940 code
= decode_Ticket((const uint8_t *)s4u2self_creds
->ticket
.data
,
1941 s4u2self_creds
->ticket
.length
,
1943 &s4u2self_ticketlen
);
1945 krb5_free_creds(ctx
, s4u2self_creds
);
1946 krb5_free_principal(ctx
, blacklist_principal
);
1947 krb5_cc_destroy(ctx
, tmp_cc
);
1952 * we need to remember the client principal of the
1953 * S4U2Self stage and as it needs to match the one we
1954 * will get for the S4U2Proxy stage. We need this
1955 * in order to detect KDCs which does not support S4U2Proxy.
1957 whitelist_principal
= s4u2self_creds
->client
;
1958 s4u2self_creds
->client
= NULL
;
1959 krb5_free_creds(ctx
, s4u2self_creds
);
1962 * For S4U2Proxy we also got a target service principal,
1963 * which also belongs to our own realm (available on
1964 * our client principal).
1966 code
= krb5_parse_name(ctx
, target_service
, &target_princ
);
1968 free_Ticket(&s4u2self_ticket
);
1969 krb5_free_principal(ctx
, whitelist_principal
);
1970 krb5_free_principal(ctx
, blacklist_principal
);
1971 krb5_cc_destroy(ctx
, tmp_cc
);
1975 code
= krb5_principal_set_realm(ctx
, target_princ
, self_realm
);
1977 free_Ticket(&s4u2self_ticket
);
1978 krb5_free_principal(ctx
, target_princ
);
1979 krb5_free_principal(ctx
, whitelist_principal
);
1980 krb5_free_principal(ctx
, blacklist_principal
);
1981 krb5_cc_destroy(ctx
, tmp_cc
);
1985 code
= krb5_get_creds_opt_alloc(ctx
, &options
);
1987 free_Ticket(&s4u2self_ticket
);
1988 krb5_free_principal(ctx
, target_princ
);
1989 krb5_free_principal(ctx
, whitelist_principal
);
1990 krb5_free_principal(ctx
, blacklist_principal
);
1991 krb5_cc_destroy(ctx
, tmp_cc
);
1995 krb5_get_creds_opt_set_options(ctx
, options
, KRB5_GC_FORWARDABLE
);
1996 krb5_get_creds_opt_set_options(ctx
, options
, KRB5_GC_CONSTRAINED_DELEGATION
);
1998 code
= krb5_get_creds_opt_set_ticket(ctx
, options
, &s4u2self_ticket
);
1999 free_Ticket(&s4u2self_ticket
);
2001 krb5_get_creds_opt_free(ctx
, options
);
2002 krb5_free_principal(ctx
, target_princ
);
2003 krb5_free_principal(ctx
, whitelist_principal
);
2004 krb5_free_principal(ctx
, blacklist_principal
);
2005 krb5_cc_destroy(ctx
, tmp_cc
);
2009 code
= krb5_get_creds(ctx
, options
, tmp_cc
,
2010 target_princ
, &s4u2proxy_creds
);
2011 krb5_get_creds_opt_free(ctx
, options
);
2012 krb5_free_principal(ctx
, target_princ
);
2013 krb5_cc_destroy(ctx
, tmp_cc
);
2015 krb5_free_principal(ctx
, whitelist_principal
);
2016 krb5_free_principal(ctx
, blacklist_principal
);
2021 * Now make sure we store the impersonated principal
2022 * and creds instead of the TGT related stuff
2023 * in the krb5_ccache of the caller.
2025 code
= krb5_copy_creds_contents(ctx
, s4u2proxy_creds
,
2027 krb5_free_creds(ctx
, s4u2proxy_creds
);
2029 krb5_free_principal(ctx
, whitelist_principal
);
2030 krb5_free_principal(ctx
, blacklist_principal
);
2035 * It's important to store the principal the KDC
2036 * returned, as otherwise the caller would not find
2037 * the S4U2Self ticket in the krb5_ccache lookup.
2039 store_principal
= store_creds
.client
;
2042 if (blacklist_principal
&&
2043 krb5_principal_compare(ctx
, store_creds
.client
, blacklist_principal
)) {
2047 code
= krb5_unparse_name(ctx
, blacklist_principal
, &sp
);
2051 code
= krb5_unparse_name(ctx
, impersonate_principal
, &ip
);
2055 DEBUG(1, ("kerberos_kinit_password_cc: "
2056 "KDC returned self principal[%s] while impersonating [%s]\n",
2057 sp
?sp
:"<no memory>",
2058 ip
?ip
:"<no memory>"));
2063 krb5_free_principal(ctx
, whitelist_principal
);
2064 krb5_free_principal(ctx
, blacklist_principal
);
2065 krb5_free_cred_contents(ctx
, &store_creds
);
2066 return KRB5_FWD_BAD_PRINCIPAL
;
2068 if (blacklist_principal
) {
2069 krb5_free_principal(ctx
, blacklist_principal
);
2072 if (whitelist_principal
&&
2073 !krb5_principal_compare(ctx
, store_creds
.client
, whitelist_principal
)) {
2077 code
= krb5_unparse_name(ctx
, store_creds
.client
, &sp
);
2081 code
= krb5_unparse_name(ctx
, whitelist_principal
, &ep
);
2085 DEBUG(1, ("kerberos_kinit_password_cc: "
2086 "KDC returned wrong principal[%s] we expected [%s]\n",
2087 sp
?sp
:"<no memory>",
2088 ep
?ep
:"<no memory>"));
2093 krb5_free_principal(ctx
, whitelist_principal
);
2094 krb5_free_cred_contents(ctx
, &store_creds
);
2095 return KRB5_FWD_BAD_PRINCIPAL
;
2097 if (whitelist_principal
) {
2098 krb5_free_principal(ctx
, whitelist_principal
);
2101 code
= krb5_cc_initialize(ctx
, store_cc
, store_principal
);
2103 krb5_free_cred_contents(ctx
, &store_creds
);
2107 code
= krb5_cc_store_cred(ctx
, store_cc
, &store_creds
);
2109 krb5_free_cred_contents(ctx
, &store_creds
);
2114 *expire_time
= (time_t) store_creds
.times
.endtime
;
2118 *kdc_time
= (time_t) store_creds
.times
.starttime
;
2121 krb5_free_cred_contents(ctx
, &store_creds
);
2127 #if !defined(HAVE_KRB5_MAKE_PRINCIPAL) && defined(HAVE_KRB5_BUILD_PRINCIPAL_ALLOC_VA)
2128 krb5_error_code
smb_krb5_make_principal(krb5_context context
,
2129 krb5_principal
*principal
,
2130 const char *_realm
, ...)
2132 krb5_error_code code
;
2141 code
= krb5_get_default_realm(context
, &realm
);
2148 va_start(ap
, _realm
);
2149 code
= krb5_build_principal_alloc_va(context
, principal
,
2150 strlen(realm
), realm
,
2155 krb5_free_default_realm(context
, realm
);
2162 #if !defined(HAVE_KRB5_CC_GET_LIFETIME) && defined(HAVE_KRB5_CC_RETRIEVE_CRED)
2164 * @brief Get the lifetime of the initial ticket in the cache.
2166 * @param[in] context The kerberos context.
2168 * @param[in] id The credential cache to get the ticket lifetime.
2170 * @param[out] t A pointer to a time value to store the lifetime.
2172 * @return 0 on success, a krb5_error_code on error.
2174 krb5_error_code
smb_krb5_cc_get_lifetime(krb5_context context
,
2178 krb5_cc_cursor cursor
;
2179 krb5_error_code kerr
;
2185 kerr
= krb5_timeofday(context
, &now
);
2190 kerr
= krb5_cc_start_seq_get(context
, id
, &cursor
);
2195 while ((kerr
= krb5_cc_next_cred(context
, id
, &cursor
, &cred
)) == 0) {
2196 #ifndef HAVE_FLAGS_IN_KRB5_CREDS
2197 if (cred
.ticket_flags
& TKT_FLG_INITIAL
) {
2199 if (cred
.flags
.b
.initial
) {
2201 if (now
< cred
.times
.endtime
) {
2202 *t
= (time_t) (cred
.times
.endtime
- now
);
2204 krb5_free_cred_contents(context
, &cred
);
2207 krb5_free_cred_contents(context
, &cred
);
2210 krb5_cc_end_seq_get(context
, id
, &cursor
);
2214 #endif /* HAVE_KRB5_CC_GET_LIFETIME */
2216 #if !defined(HAVE_KRB5_FREE_CHECKSUM_CONTENTS) && defined(HAVE_FREE_CHECKSUM)
2217 void smb_krb5_free_checksum_contents(krb5_context ctx
, krb5_checksum
*cksum
)
2219 free_Checksum(cksum
);
2223 krb5_error_code
smb_krb5_make_pac_checksum(TALLOC_CTX
*mem_ctx
,
2224 DATA_BLOB
*pac_data
,
2225 krb5_context context
,
2226 const krb5_keyblock
*keyblock
,
2228 DATA_BLOB
*sig_blob
)
2230 krb5_error_code ret
;
2231 krb5_checksum cksum
;
2232 #if defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CREATE_CHECKSUM)
2236 ret
= krb5_crypto_init(context
,
2241 DEBUG(0,("krb5_crypto_init() failed: %s\n",
2242 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
2245 ret
= krb5_create_checksum(context
,
2247 KRB5_KU_OTHER_CKSUM
,
2253 DEBUG(2, ("PAC Verification failed: %s\n",
2254 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
2257 krb5_crypto_destroy(context
, crypto
);
2263 *sig_type
= cksum
.cksumtype
;
2264 *sig_blob
= data_blob_talloc(mem_ctx
,
2265 cksum
.checksum
.data
,
2266 cksum
.checksum
.length
);
2267 #elif defined(HAVE_KRB5_C_MAKE_CHECKSUM)
2270 input
.data
= (char *)pac_data
->data
;
2271 input
.length
= pac_data
->length
;
2273 ret
= krb5_c_make_checksum(context
,
2276 KRB5_KEYUSAGE_APP_DATA_CKSUM
,
2280 DEBUG(2, ("PAC Verification failed: %s\n",
2281 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
2285 *sig_type
= cksum
.checksum_type
;
2286 *sig_blob
= data_blob_talloc(mem_ctx
,
2291 #error krb5_create_checksum or krb5_c_make_checksum not available
2292 #endif /* HAVE_KRB5_C_MAKE_CHECKSUM */
2293 smb_krb5_free_checksum_contents(context
, &cksum
);
2300 * smb_krb5_principal_get_realm
2302 * @brief Get realm of a principal
2304 * @param[in] context The krb5_context
2305 * @param[in] principal The principal
2306 * @return pointer to the realm
2308 * Caller must free if the return value is not NULL.
2312 char *smb_krb5_principal_get_realm(krb5_context context
,
2313 krb5_const_principal principal
)
2315 #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
2316 return strdup(discard_const_p(char, krb5_principal_get_realm(context
, principal
)));
2317 #elif defined(krb5_princ_realm) /* MIT */
2319 realm
= krb5_princ_realm(context
, principal
);
2320 return strndup(realm
->data
, realm
->length
);
2322 #error UNKNOWN_GET_PRINC_REALM_FUNCTIONS
2327 * smb_krb5_principal_set_realm
2329 * @brief Get realm of a principal
2331 * @param[in] context The krb5_context
2332 * @param[in] principal The principal
2333 * @param[in] realm The realm
2334 * @return 0 on success, a krb5_error_code on error.
2338 krb5_error_code
smb_krb5_principal_set_realm(krb5_context context
,
2339 krb5_principal principal
,
2342 #ifdef HAVE_KRB5_PRINCIPAL_SET_REALM /* Heimdal */
2343 return krb5_principal_set_realm(context
, principal
, realm
);
2344 #elif defined(krb5_princ_realm) && defined(krb5_princ_set_realm) /* MIT */
2345 krb5_error_code ret
;
2347 krb5_data
*old_data
;
2349 old_data
= krb5_princ_realm(context
, principal
);
2351 ret
= krb5_copy_data_contents(&data
,
2358 /* free realm before setting */
2359 free(old_data
->data
);
2361 krb5_princ_set_realm(context
, principal
, &data
);
2365 #error UNKNOWN_PRINC_SET_REALM_FUNCTION
2370 /************************************************************************
2371 Routine to get the default realm from the kerberos credentials cache.
2372 Caller must free if the return value is not NULL.
2373 ************************************************************************/
2375 static char *smb_krb5_get_default_realm_from_ccache(TALLOC_CTX
*mem_ctx
)
2378 krb5_context ctx
= NULL
;
2379 krb5_ccache cc
= NULL
;
2380 krb5_principal princ
= NULL
;
2382 initialize_krb5_error_table();
2383 if (krb5_init_context(&ctx
)) {
2387 DEBUG(5,("kerberos_get_default_realm_from_ccache: "
2388 "Trying to read krb5 cache: %s\n",
2389 krb5_cc_default_name(ctx
)));
2390 if (krb5_cc_default(ctx
, &cc
)) {
2391 DEBUG(0,("kerberos_get_default_realm_from_ccache: "
2392 "failed to read default cache\n"));
2395 if (krb5_cc_get_principal(ctx
, cc
, &princ
)) {
2396 DEBUG(0,("kerberos_get_default_realm_from_ccache: "
2397 "failed to get default principal\n"));
2401 #if defined(HAVE_KRB5_PRINCIPAL_GET_REALM)
2402 realm
= talloc_strdup(mem_ctx
, krb5_principal_get_realm(ctx
, princ
));
2403 #elif defined(HAVE_KRB5_PRINC_REALM)
2405 krb5_data
*realm_data
= krb5_princ_realm(ctx
, princ
);
2406 realm
= talloc_strndup(mem_ctx
, realm_data
->data
, realm_data
->length
);
2414 krb5_free_principal(ctx
, princ
);
2417 krb5_cc_close(ctx
, cc
);
2419 krb5_free_context(ctx
);
2425 /************************************************************************
2426 Routine to get the realm from a given DNS name.
2427 ************************************************************************/
2429 static char *smb_krb5_get_realm_from_hostname(TALLOC_CTX
*mem_ctx
,
2430 const char *hostname
)
2432 #if defined(HAVE_KRB5_REALM_TYPE)
2434 krb5_realm
*realm_list
= NULL
;
2437 char **realm_list
= NULL
;
2440 krb5_error_code kerr
;
2441 krb5_context ctx
= NULL
;
2443 initialize_krb5_error_table();
2444 if (krb5_init_context(&ctx
)) {
2448 kerr
= krb5_get_host_realm(ctx
, hostname
, &realm_list
);
2450 DEBUG(3,("kerberos_get_realm_from_hostname %s: "
2452 hostname
? hostname
: "(NULL)",
2453 error_message(kerr
) ));
2457 if (realm_list
&& realm_list
[0]) {
2458 realm
= talloc_strdup(mem_ctx
, realm_list
[0]);
2465 krb5_free_host_realm(ctx
, realm_list
);
2468 krb5_free_context(ctx
);
2474 char *kerberos_get_principal_from_service_hostname(TALLOC_CTX
*mem_ctx
,
2475 const char *service
,
2476 const char *remote_name
,
2477 const char *default_realm
)
2482 host
= strchr_m(remote_name
, '.');
2485 realm
= smb_krb5_get_realm_from_hostname(talloc_tos(),
2488 /* NetBIOS name - use our realm. */
2489 realm
= smb_krb5_get_default_realm_from_ccache(talloc_tos());
2492 if (realm
== NULL
|| *realm
== '\0') {
2493 realm
= talloc_strdup(talloc_tos(), default_realm
);
2497 DEBUG(3,("kerberos_get_principal_from_service_hostname: "
2498 "cannot get realm from, "
2499 "desthost %s or default ccache. Using default "
2500 "smb.conf realm %s\n",
2505 principal
= talloc_asprintf(mem_ctx
,
2507 service
, remote_name
,
2513 char *smb_get_krb5_error_message(krb5_context context
,
2514 krb5_error_code code
,
2515 TALLOC_CTX
*mem_ctx
)
2519 #if defined(HAVE_KRB5_GET_ERROR_MESSAGE) && defined(HAVE_KRB5_FREE_ERROR_MESSAGE)
2520 const char *context_error
= krb5_get_error_message(context
, code
);
2521 if (context_error
) {
2522 ret
= talloc_asprintf(mem_ctx
, "%s: %s",
2523 error_message(code
), context_error
);
2524 krb5_free_error_message(context
, context_error
);
2528 ret
= talloc_strdup(mem_ctx
, error_message(code
));
2534 * @brief Return the kerberos library setting for "libdefaults:allow_weak_crypto"
2536 * @param context The krb5_context
2538 * @return krb5_boolean
2540 * Function returns true if weak crypto is allowd, false if not
2543 krb5_boolean
smb_krb5_get_allowed_weak_crypto(krb5_context context
)
2544 #if defined(HAVE_KRB5_CONFIG_GET_BOOL_DEFAULT)
2546 return krb5_config_get_bool_default(context
,
2550 "allow_weak_crypto",
2553 #elif defined(HAVE_PROFILE_H) && defined(HAVE_KRB5_GET_PROFILE)
2555 #include <profile.h>
2556 krb5_error_code ret
;
2557 krb5_boolean ret_default
= false;
2561 ret
= krb5_get_profile(context
,
2567 ret
= profile_get_boolean(profile
,
2569 "allow_weak_crypto",
2570 NULL
, /* subsubname */
2571 ret_default
, /* def_val */
2572 &ret_profile
/* *ret_default */);
2577 profile_release(profile
);
2582 #error UNKNOWN_KRB5_CONFIG_ROUTINES
2586 * @brief Return the type of a krb5_principal
2588 * @param context The krb5_context
2589 * @param principal The const krb5_principal
2591 * @return integer type of the principal
2593 int smb_krb5_principal_get_type(krb5_context context
,
2594 krb5_const_principal principal
)
2596 #ifdef HAVE_KRB5_PRINCIPAL_GET_TYPE /* Heimdal */
2597 return krb5_principal_get_type(context
, principal
);
2598 #elif defined(krb5_princ_type) /* MIT */
2599 return krb5_princ_type(context
, principal
);
2601 #error UNKNOWN_PRINC_GET_TYPE_FUNCTION
2606 * @brief Generate a krb5 warning, forwarding to com_err
2608 * @param context The krb5_context
2609 * @param fmt The message format
2610 * @param ... The message arguments
2614 #if !defined(HAVE_KRB5_WARNX)
2615 krb5_error_code
krb5_warnx(krb5_context context
, const char *fmt
, ...)
2619 va_start(args
, fmt
);
2620 com_err_va("kdb_samba", errno
, fmt
, args
);
2627 #else /* HAVE_KRB5 */
2628 /* this saves a few linking headaches */
2629 int cli_krb5_get_ticket(TALLOC_CTX
*mem_ctx
,
2630 const char *principal
, time_t time_offset
,
2631 DATA_BLOB
*ticket
, DATA_BLOB
*session_key_krb5
,
2632 uint32_t extra_ap_opts
,
2633 const char *ccname
, time_t *tgs_expire
,
2634 const char *impersonate_princ_s
)
2636 DEBUG(0,("NO KERBEROS SUPPORT\n"));
2640 #endif /* HAVE_KRB5 */