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/crypto/md4.h"
27 #include "../libds/common/flags.h"
31 #endif /* HAVE_COM_ERR_H */
33 #ifndef KRB5_AUTHDATA_WIN2K_PAC
34 #define KRB5_AUTHDATA_WIN2K_PAC 128
37 #ifndef KRB5_AUTHDATA_IF_RELEVANT
38 #define KRB5_AUTHDATA_IF_RELEVANT 1
43 #define GSSAPI_CHECKSUM 0x8003 /* Checksum type value for Kerberos */
44 #define GSSAPI_BNDLENGTH 16 /* Bind Length (rfc-1964 pg.3) */
45 #define GSSAPI_CHECKSUM_SIZE (4+GSSAPI_BNDLENGTH+4) /* Length of bind length,
46 bind field, flags field. */
47 #define GSS_C_DELEG_FLAG 1
49 /* MIT krb5 1.7beta3 (in Ubuntu Karmic) is missing the prototype,
50 but still has the symbol */
51 #if !HAVE_DECL_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE
52 krb5_error_code
krb5_auth_con_set_req_cksumtype(
54 krb5_auth_context auth_context
,
55 krb5_cksumtype cksumtype
);
58 #if !defined(SMB_MALLOC)
60 #define SMB_MALLOC(s) malloc((s))
64 #define SMB_STRDUP(s) strdup(s)
67 /**********************************************************
69 **********************************************************/
71 #if !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
73 #if defined(HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES)
75 /* With MIT kerberos, we should use krb5_set_default_tgs_enctypes in preference
76 * to krb5_set_default_tgs_ktypes. See
77 * http://lists.samba.org/archive/samba-technical/2006-July/048271.html
79 * If the MIT libraries are not exporting internal symbols, we will end up in
80 * this branch, which is correct. Otherwise we will continue to use the
83 krb5_error_code
krb5_set_default_tgs_ktypes(krb5_context ctx
, const krb5_enctype
*enc
)
85 return krb5_set_default_tgs_enctypes(ctx
, enc
);
88 #elif defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES)
91 krb5_error_code
krb5_set_default_tgs_ktypes(krb5_context ctx
, const krb5_enctype
*enc
)
93 return krb5_set_default_in_tkt_etypes(ctx
, enc
);
96 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES */
98 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_KTYPES */
101 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
102 krb5_error_code
krb5_auth_con_setuseruserkey(krb5_context context
,
103 krb5_auth_context auth_context
,
104 krb5_keyblock
*keyblock
)
106 return krb5_auth_con_setkey(context
, auth_context
, keyblock
);
110 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
111 void krb5_free_unparsed_name(krb5_context context
, char *val
)
117 #if !defined(HAVE_KRB5_FREE_ENCTYPES)
118 void krb5_free_enctypes(krb5_context context
, krb5_enctype
*val
) {
123 #if !defined(HAVE_KRB5_FREE_STRING)
124 void krb5_free_string(krb5_context context
, char *val
) {
129 krb5_error_code
smb_krb5_princ_component(krb5_context context
,
130 krb5_const_principal principal
,
133 krb5_error_code
smb_krb5_princ_component(krb5_context context
,
134 krb5_const_principal principal
,
138 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
139 const char *component
= NULL
;
145 component
= krb5_principal_get_comp_string(context
, principal
, i
);
146 if (component
== NULL
) {
150 *data
= smb_krb5_make_data(discard_const_p(char, component
), strlen(component
));
154 const krb5_data
*kdata
= NULL
;
160 kdata
= krb5_princ_component(context
, principal
, i
);
171 /**********************************************************
173 **********************************************************/
176 * @brief Stores the address of a 'struct sockaddr_storage' into a krb5_address
178 * @param[in] paddr A pointer to a 'struct sockaddr_storage to extract the
181 * @param[out] pkaddr A Kerberos address to store the address in.
183 * @return True on success, false if an error occurred.
185 bool smb_krb5_sockaddr_to_kaddr(struct sockaddr_storage
*paddr
,
186 krb5_address
*pkaddr
)
188 memset(pkaddr
, '\0', sizeof(krb5_address
));
189 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
192 if (paddr
->ss_family
== AF_INET6
) {
193 pkaddr
->addr_type
= KRB5_ADDRESS_INET6
;
194 pkaddr
->address
.length
= sizeof(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
195 pkaddr
->address
.data
= (char *)&(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
199 if (paddr
->ss_family
== AF_INET
) {
200 pkaddr
->addr_type
= KRB5_ADDRESS_INET
;
201 pkaddr
->address
.length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
202 pkaddr
->address
.data
= (char *)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
205 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
208 if (paddr
->ss_family
== AF_INET6
) {
209 pkaddr
->addrtype
= ADDRTYPE_INET6
;
210 pkaddr
->length
= sizeof(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
211 pkaddr
->contents
= (krb5_octet
*)&(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
215 if (paddr
->ss_family
== AF_INET
) {
216 pkaddr
->addrtype
= ADDRTYPE_INET
;
217 pkaddr
->length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
218 pkaddr
->contents
= (krb5_octet
*)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
222 #error UNKNOWN_ADDRTYPE
227 krb5_error_code
smb_krb5_mk_error(krb5_context context
,
228 krb5_error_code error_code
,
231 const krb5_principal client
,
232 const krb5_principal server
,
235 krb5_error_code code
= EINVAL
;
236 #ifdef SAMBA4_USES_HEIMDAL
237 code
= krb5_mk_error(context
,
243 NULL
, /* client_time */
244 NULL
, /* client_usec */
247 krb5_principal unspec_server
= NULL
;
253 code
= krb5_us_timeofday(context
,
260 errpkt
.error
= error_code
- ERROR_TABLE_BASE_krb5
;
262 errpkt
.text
.length
= 0;
263 if (e_text
!= NULL
) {
264 errpkt
.text
= smb_krb5_make_data(discard_const_p(char, e_text
), strlen(e_text
));
267 errpkt
.e_data
= smb_krb5_make_data(NULL
, 0);
268 if (e_data
!= NULL
) {
269 errpkt
.e_data
= *e_data
;
272 errpkt
.client
= client
;
274 if (server
!= NULL
) {
275 errpkt
.server
= server
;
277 code
= smb_krb5_make_principal(context
,
279 "<unspecified realm>",
284 errpkt
.server
= unspec_server
;
287 code
= krb5_mk_error(context
,
290 krb5_free_principal(context
, unspec_server
);
296 * @brief Create a keyblock based on input parameters
298 * @param context The krb5_context
299 * @param host_princ The krb5_principal to use
300 * @param salt The optional salt, if omitted, salt is calculated with
301 * the provided principal.
302 * @param password The krb5_data containing the password
303 * @param enctype The krb5_enctype to use for the keyblock generation
304 * @param key The returned krb5_keyblock, caller needs to free with
305 * krb5_free_keyblock().
307 * @return krb5_error_code
309 int smb_krb5_create_key_from_string(krb5_context context
,
310 krb5_const_principal host_princ
,
311 const krb5_data
*salt
,
312 const krb5_data
*password
,
313 krb5_enctype enctype
,
318 if (host_princ
== NULL
&& salt
== NULL
) {
322 if ((int)enctype
== (int)ENCTYPE_ARCFOUR_HMAC
) {
323 TALLOC_CTX
*frame
= talloc_stackframe();
324 uint8_t *utf16
= NULL
;
325 size_t utf16_size
= 0;
329 ok
= convert_string_talloc(frame
, CH_UNIX
, CH_UTF16LE
,
330 password
->data
, password
->length
,
331 &utf16
, &utf16_size
);
341 mdfour(nt_hash
, utf16
, utf16_size
);
342 BURN_PTR_SIZE(utf16
, utf16_size
);
343 ret
= smb_krb5_keyblock_init_contents(context
,
344 ENCTYPE_ARCFOUR_HMAC
,
348 ZERO_STRUCT(nt_hash
);
358 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_C_STRING_TO_KEY)
363 ret
= krb5_principal2salt(context
, host_princ
, &_salt
);
365 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret
)));
371 ret
= krb5_c_string_to_key(context
, enctype
, password
, &_salt
, key
);
373 SAFE_FREE(_salt
.data
);
376 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
381 ret
= krb5_get_pw_salt(context
, host_princ
, &_salt
);
383 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret
)));
387 _salt
.saltvalue
= *salt
;
388 _salt
.salttype
= KRB5_PW_SALT
;
391 ret
= krb5_string_to_key_salt(context
, enctype
, (const char *)password
->data
, _salt
, key
);
393 krb5_free_salt(context
, _salt
);
397 #error UNKNOWN_CREATE_KEY_FUNCTIONS
403 * @brief Create a salt for a given principal
405 * @param context The initialized krb5_context
406 * @param host_princ The krb5_principal to create the salt for
407 * @param psalt A pointer to a krb5_data struct
409 * caller has to free the contents of psalt with smb_krb5_free_data_contents
410 * when function has succeeded
412 * @return krb5_error_code, returns 0 on success, error code otherwise
415 int smb_krb5_get_pw_salt(krb5_context context
,
416 krb5_const_principal host_princ
,
418 #if defined(HAVE_KRB5_GET_PW_SALT)
424 ret
= krb5_get_pw_salt(context
, host_princ
, &salt
);
429 *psalt
= salt
.saltvalue
;
433 #elif defined(HAVE_KRB5_PRINCIPAL2SALT)
436 return krb5_principal2salt(context
, host_princ
, psalt
);
439 #error UNKNOWN_SALT_FUNCTIONS
443 * @brief This constructs the salt principal used by active directory
445 * Most Kerberos encryption types require a salt in order to
446 * calculate the long term private key for user/computer object
447 * based on a password.
449 * The returned _salt_principal is a string in forms like this:
450 * - host/somehost.example.com@EXAMPLE.COM
451 * - SomeAccount@EXAMPLE.COM
452 * - SomePrincipal@EXAMPLE.COM
454 * This is not the form that's used as salt, it's just
455 * the human readable form. It needs to be converted by
456 * smb_krb5_salt_principal2data().
458 * @param[in] realm The realm the user/computer is added too.
460 * @param[in] sAMAccountName The sAMAccountName attribute of the object.
462 * @param[in] userPrincipalName The userPrincipalName attribute of the object
463 * or NULL if not available.
465 * @param[in] uac_flags UF_ACCOUNT_TYPE_MASKed userAccountControl field
467 * @param[in] mem_ctx The TALLOC_CTX to allocate _salt_principal.
469 * @param[out] _salt_principal The resulting principal as string.
471 * @retval 0 Success; otherwise - Kerberos error codes
473 * @see smb_krb5_salt_principal2data
475 int smb_krb5_salt_principal(krb5_context krb5_ctx
,
477 const char *sAMAccountName
,
478 const char *userPrincipalName
,
480 krb5_principal
*salt_princ
)
482 TALLOC_CTX
*frame
= talloc_stackframe();
483 char *upper_realm
= NULL
;
484 const char *principal
= NULL
;
485 int principal_len
= 0;
486 krb5_error_code krb5_ret
;
490 if (sAMAccountName
== NULL
) {
500 if (uac_flags
& ~UF_ACCOUNT_TYPE_MASK
) {
502 * catch callers which still
508 if (uac_flags
== 0) {
510 * catch callers which still
517 upper_realm
= strupper_talloc(frame
, realm
);
518 if (upper_realm
== NULL
) {
523 /* Many, many thanks to lukeh@padl.com for this
524 * algorithm, described in his Nov 10 2004 mail to
525 * samba-technical@lists.samba.org */
528 * Determine a salting principal
530 if (uac_flags
& UF_TRUST_ACCOUNT_MASK
) {
531 int computer_len
= 0;
533 computer_len
= strlen(sAMAccountName
);
534 if (sAMAccountName
[computer_len
-1] == '$') {
538 if (uac_flags
& UF_INTERDOMAIN_TRUST_ACCOUNT
) {
539 const char *krbtgt
= "krbtgt";
540 krb5_ret
= krb5_build_principal_ext(krb5_ctx
,
554 const char *host
= "host";
556 char *tmp_lower
= NULL
;
558 tmp
= talloc_asprintf(frame
, "%*.*s.%s",
568 tmp_lower
= strlower_talloc(frame
, tmp
);
569 if (tmp_lower
== NULL
) {
574 krb5_ret
= krb5_build_principal_ext(krb5_ctx
,
589 } else if (userPrincipalName
!= NULL
) {
591 * We parse the name not only to allow an easy
592 * replacement of the realm (no matter the realm in
593 * the UPN, the salt comes from the upper-case real
594 * realm, but also to correctly provide a salt when
595 * the UPN is host/foo.bar
597 * This can fail for a UPN of the form foo@bar@REALM
598 * (which is accepted by windows) however.
600 krb5_ret
= krb5_parse_name(krb5_ctx
,
610 * No matter what realm (including none) in the UPN,
611 * the realm is replaced with our upper-case realm
613 krb5_ret
= smb_krb5_principal_set_realm(krb5_ctx
,
617 krb5_free_principal(krb5_ctx
, *salt_princ
);
622 principal
= sAMAccountName
;
623 principal_len
= strlen(principal
);
625 krb5_ret
= krb5_build_principal_ext(krb5_ctx
,
643 * @brief This constructs the salt principal used by active directory
645 * Most Kerberos encryption types require a salt in order to
646 * calculate the long term private key for user/computer object
647 * based on a password.
649 * The returned _salt_principal is a string in forms like this:
650 * - host/somehost.example.com@EXAMPLE.COM
651 * - SomeAccount@EXAMPLE.COM
652 * - SomePrincipal@EXAMPLE.COM
654 * This is not the form that's used as salt, it's just
655 * the human readable form. It needs to be converted by
656 * smb_krb5_salt_principal2data().
658 * @param[in] realm The realm the user/computer is added too.
660 * @param[in] sAMAccountName The sAMAccountName attribute of the object.
662 * @param[in] userPrincipalName The userPrincipalName attribute of the object
663 * or NULL if not available.
665 * @param[in] uac_flags UF_ACCOUNT_TYPE_MASKed userAccountControl field
667 * @param[in] mem_ctx The TALLOC_CTX to allocate _salt_principal.
669 * @param[out] _salt_principal The resulting principal as string.
671 * @retval 0 Success; otherwise - Kerberos error codes
673 * @see smb_krb5_salt_principal2data
675 int smb_krb5_salt_principal_str(const char *realm
,
676 const char *sAMAccountName
,
677 const char *userPrincipalName
,
680 char **_salt_principal_str
)
682 krb5_principal salt_principal
= NULL
;
683 char *salt_principal_malloc
;
684 krb5_context krb5_ctx
;
685 krb5_error_code krb5_ret
686 = smb_krb5_init_context_common(&krb5_ctx
);
688 DBG_ERR("kerberos init context failed (%s)\n",
689 error_message(krb5_ret
));
693 krb5_ret
= smb_krb5_salt_principal(krb5_ctx
,
700 DBG_ERR("unable to create salt principal:%s\n",
701 error_message(krb5_ret
));
705 krb5_ret
= krb5_unparse_name(krb5_ctx
, salt_principal
,
706 &salt_principal_malloc
);
708 krb5_free_principal(krb5_ctx
, salt_principal
);
709 DBG_ERR("kerberos unparse of salt principal failed (%s)\n",
710 error_message(krb5_ret
));
713 krb5_free_principal(krb5_ctx
, salt_principal
);
715 = talloc_strdup(mem_ctx
, salt_principal_malloc
);
716 krb5_free_unparsed_name(krb5_ctx
, salt_principal_malloc
);
718 if (*_salt_principal_str
== NULL
) {
725 * @brief Converts the salt principal string into the salt data blob
727 * This function takes a salt_principal as string in forms like this:
728 * - host/somehost.example.com@EXAMPLE.COM
729 * - SomeAccount@EXAMPLE.COM
730 * - SomePrincipal@EXAMPLE.COM
732 * It generates values like:
733 * - EXAMPLE.COMhost/somehost.example.com
734 * - EXAMPLE.COMSomeAccount
735 * - EXAMPLE.COMSomePrincipal
737 * @param[in] realm The realm the user/computer is added too.
739 * @param[in] sAMAccountName The sAMAccountName attribute of the object.
741 * @param[in] userPrincipalName The userPrincipalName attribute of the object
742 * or NULL if not available.
744 * @param[in] is_computer The indication of the object includes
745 * objectClass=computer.
747 * @param[in] mem_ctx The TALLOC_CTX to allocate _salt_principal.
749 * @param[out] _salt_principal The resulting principal as string.
751 * @retval 0 Success; otherwise - Kerberos error codes
753 * @see smb_krb5_salt_principal
755 int smb_krb5_salt_principal2data(krb5_context context
,
756 const char *salt_principal
,
761 krb5_principal salt_princ
= NULL
;
766 ret
= krb5_parse_name(context
, salt_principal
, &salt_princ
);
771 ret
= smb_krb5_get_pw_salt(context
, salt_princ
, &salt
);
772 krb5_free_principal(context
, salt_princ
);
777 *_salt_data
= talloc_strndup(mem_ctx
,
780 smb_krb5_free_data_contents(context
, &salt
);
781 if (*_salt_data
== NULL
) {
788 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
790 * @brief Get a list of encryption types allowed for session keys
792 * @param[in] context The library context
794 * @param[in] enctypes An allocated, zero-terminated list of encryption types
796 * This function returns an allocated list of encryption types allowed for
799 * Use krb5_free_enctypes() to free the enctypes when it is no longer needed.
801 * @retval 0 Success; otherwise - Kerberos error codes
803 krb5_error_code
smb_krb5_get_allowed_etypes(krb5_context context
,
804 krb5_enctype
**enctypes
)
806 return krb5_get_permitted_enctypes(context
, enctypes
);
808 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
809 krb5_error_code
smb_krb5_get_allowed_etypes(krb5_context context
,
810 krb5_enctype
**enctypes
)
812 #ifdef HAVE_KRB5_PDU_NONE_DECL
813 return krb5_get_default_in_tkt_etypes(context
, KRB5_PDU_NONE
, enctypes
);
815 return krb5_get_default_in_tkt_etypes(context
, enctypes
);
819 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
824 * @brief Convert a string principal name to a Kerberos principal.
826 * @param[in] context The library context
828 * @param[in] name The principal as a unix charset string.
830 * @param[out] principal The newly allocated principal.
832 * Use krb5_free_principal() to free a principal when it is no longer needed.
834 * @return 0 on success, a Kerberos error code otherwise.
836 krb5_error_code
smb_krb5_parse_name(krb5_context context
,
838 krb5_principal
*principal
)
842 size_t converted_size
;
843 TALLOC_CTX
*frame
= talloc_stackframe();
845 if (!push_utf8_talloc(frame
, &utf8_name
, name
, &converted_size
)) {
850 ret
= krb5_parse_name(context
, utf8_name
, principal
);
851 if (ret
== KRB5_PARSE_MALFORMED
) {
852 ret
= krb5_parse_name_flags(context
, utf8_name
,
853 KRB5_PRINCIPAL_PARSE_ENTERPRISE
,
861 * @brief Convert a Kerberos principal structure to a string representation.
863 * The resulting string representation will be a unix charset name and is
866 * @param[in] mem_ctx The talloc context to allocate memory on.
868 * @param[in] context The library context.
870 * @param[in] principal The principal.
872 * @param[out] unix_name A string representation of the principal name as with
875 * Use talloc_free() to free the string representation if it is no longer
878 * @return 0 on success, a Kerberos error code otherwise.
880 krb5_error_code
smb_krb5_unparse_name(TALLOC_CTX
*mem_ctx
,
881 krb5_context context
,
882 krb5_const_principal principal
,
887 size_t converted_size
;
890 ret
= krb5_unparse_name(context
, principal
, &utf8_name
);
895 if (!pull_utf8_talloc(mem_ctx
, unix_name
, utf8_name
, &converted_size
)) {
896 krb5_free_unparsed_name(context
, utf8_name
);
899 krb5_free_unparsed_name(context
, utf8_name
);
904 * @brief Free the contents of a krb5_data structure and zero the data field.
906 * @param[in] context The krb5 context
908 * @param[in] pdata The data structure to free contents of
910 * This function frees the contents, not the structure itself.
912 void smb_krb5_free_data_contents(krb5_context context
, krb5_data
*pdata
)
914 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
916 krb5_free_data_contents(context
, pdata
);
918 #elif defined(HAVE_KRB5_DATA_FREE)
919 krb5_data_free(context
, pdata
);
921 SAFE_FREE(pdata
->data
);
926 * @brief copy a buffer into a krb5_data struct
928 * @param[in] p The krb5_data
929 * @param[in] data The data to copy
930 * @param[in] length The length of the data to copy
931 * @return krb5_error_code
933 * Caller has to free krb5_data with smb_krb5_free_data_contents().
935 krb5_error_code
smb_krb5_copy_data_contents(krb5_data
*p
,
939 #if defined(HAVE_KRB5_DATA_COPY)
940 return krb5_data_copy(p
, data
, len
);
943 p
->data
= malloc(len
);
944 if (p
->data
== NULL
) {
947 memmove(p
->data
, data
, len
);
952 p
->magic
= KV5M_DATA
;
958 * @brief put a buffer reference into a krb5_data struct
960 * @param[in] data The data to reference
961 * @param[in] length The length of the data to reference
964 * Caller should not free krb5_data.
966 krb5_data
smb_krb5_make_data(void *data
,
971 #ifdef SAMBA4_USES_HEIMDAL
972 d
.data
= (uint8_t *)data
;
982 krb5_data
smb_krb5_data_from_blob(DATA_BLOB blob
)
984 return smb_krb5_make_data(blob
.data
, blob
.length
);
987 bool smb_krb5_get_smb_session_key(TALLOC_CTX
*mem_ctx
,
988 krb5_context context
,
989 krb5_auth_context auth_context
,
990 DATA_BLOB
*session_key
,
993 krb5_keyblock
*skey
= NULL
;
994 krb5_error_code err
= 0;
998 #ifdef HAVE_KRB5_AUTH_CON_GETRECVSUBKEY
999 err
= krb5_auth_con_getrecvsubkey(context
,
1002 #else /* HAVE_KRB5_AUTH_CON_GETRECVSUBKEY */
1003 err
= krb5_auth_con_getremotesubkey(context
,
1004 auth_context
, &skey
);
1005 #endif /* HAVE_KRB5_AUTH_CON_GETRECVSUBKEY */
1007 #ifdef HAVE_KRB5_AUTH_CON_GETSENDSUBKEY
1008 err
= krb5_auth_con_getsendsubkey(context
,
1011 #else /* HAVE_KRB5_AUTH_CON_GETSENDSUBKEY */
1012 err
= krb5_auth_con_getlocalsubkey(context
,
1013 auth_context
, &skey
);
1014 #endif /* HAVE_KRB5_AUTH_CON_GETSENDSUBKEY */
1017 if (err
|| skey
== NULL
) {
1018 DEBUG(10, ("KRB5 error getting session key %d\n", err
));
1022 DEBUG(10, ("Got KRB5 session key of length %d\n",
1023 (int)KRB5_KEY_LENGTH(skey
)));
1025 *session_key
= data_blob_talloc(mem_ctx
,
1026 KRB5_KEY_DATA(skey
),
1027 KRB5_KEY_LENGTH(skey
));
1028 dump_data_pw("KRB5 Session Key:\n",
1030 session_key
->length
);
1036 krb5_free_keyblock(context
, skey
);
1044 * @brief Get talloced string component of a principal
1046 * @param[in] mem_ctx The TALLOC_CTX
1047 * @param[in] context The krb5_context
1048 * @param[in] principal The principal
1049 * @param[in] component The component
1050 * @return string component
1052 * Caller must talloc_free if the return value is not NULL.
1055 char *smb_krb5_principal_get_comp_string(TALLOC_CTX
*mem_ctx
,
1056 krb5_context context
,
1057 krb5_const_principal principal
,
1058 unsigned int component
)
1060 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
1061 const char *str
= NULL
;
1063 str
= krb5_principal_get_comp_string(context
, principal
, component
);
1068 return talloc_strdup(mem_ctx
, str
);
1072 if (component
>= krb5_princ_size(context
, principal
)) {
1076 data
= krb5_princ_component(context
, principal
, component
);
1081 return talloc_strndup(mem_ctx
, data
->data
, data
->length
);
1088 * @param[in] ccache_string A string pointing to the cache to renew the ticket
1089 * (e.g. FILE:/tmp/krb5cc_0) or NULL. If the principal
1090 * ccache has not been specified, the default ccache
1093 * @param[in] client_string The client principal string (e.g. user@SAMBA.SITE)
1094 * or NULL. If the principal string has not been
1095 * specified, the principal from the ccache will be
1098 * @param[in] service_string The service ticket string
1099 * (e.g. krbtgt/SAMBA.SITE@SAMBA.SITE) or NULL. If
1100 * the service ticket is specified, it is parsed
1101 * (with the realm part ignored) and used as the
1102 * server principal of the credential. Otherwise
1103 * the ticket-granting service is used.
1105 * @param[in] expire_time A pointer to store the credentials end time or
1108 * @return 0 on Success, a Kerberos error code otherwise.
1110 krb5_error_code
smb_krb5_renew_ticket(const char *ccache_string
,
1111 const char *client_string
,
1112 const char *service_string
,
1113 time_t *expire_time
)
1115 krb5_error_code ret
;
1116 krb5_context context
= NULL
;
1117 krb5_ccache ccache
= NULL
;
1118 krb5_principal client
= NULL
;
1119 krb5_creds creds
, creds_in
;
1122 ZERO_STRUCT(creds_in
);
1124 ret
= smb_krb5_init_context_common(&context
);
1126 DBG_ERR("kerberos init context failed (%s)\n",
1127 error_message(ret
));
1131 if (!ccache_string
) {
1132 ccache_string
= krb5_cc_default_name(context
);
1135 if (!ccache_string
) {
1140 DBG_DEBUG("Using %s as ccache for client '%s' and service '%s'\n",
1141 ccache_string
, client_string
, service_string
);
1143 /* FIXME: we should not fall back to defaults */
1144 ret
= krb5_cc_resolve(context
, discard_const_p(char, ccache_string
), &ccache
);
1149 if (client_string
) {
1150 ret
= smb_krb5_parse_name(context
, client_string
, &client
);
1155 ret
= krb5_cc_get_principal(context
, ccache
, &client
);
1161 ret
= krb5_get_renewed_creds(context
, &creds
, client
, ccache
, discard_const_p(char, service_string
));
1163 DBG_DEBUG("krb5_get_renewed_creds using ccache '%s' "
1164 "for client '%s' and service '%s' failed: %s\n",
1165 ccache_string
, client_string
, service_string
,
1166 error_message(ret
));
1170 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
1171 ret
= krb5_cc_initialize(context
, ccache
, client
);
1176 ret
= krb5_cc_store_cred(context
, ccache
, &creds
);
1179 *expire_time
= (time_t) creds
.times
.endtime
;
1183 krb5_free_cred_contents(context
, &creds_in
);
1184 krb5_free_cred_contents(context
, &creds
);
1187 krb5_free_principal(context
, client
);
1190 krb5_cc_close(context
, ccache
);
1193 krb5_free_context(context
);
1200 * @brief Free the data stored in an smb_krb5_addresses structure.
1202 * @param[in] context The library context
1204 * @param[in] addr The address structure to free.
1206 * @return 0 on success, a Kerberos error code otherwise.
1208 krb5_error_code
smb_krb5_free_addresses(krb5_context context
,
1209 smb_krb5_addresses
*addr
)
1211 krb5_error_code ret
= 0;
1215 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1216 krb5_free_addresses(context
, addr
->addrs
);
1217 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1218 ret
= krb5_free_addresses(context
, addr
->addrs
);
1219 SAFE_FREE(addr
->addrs
);
1226 #define MAX_NETBIOSNAME_LEN 16
1229 * @brief Add a netbios name to the array of addresses
1231 * @param[in] kerb_addr A pointer to the smb_krb5_addresses to add the
1234 * @param[in] netbios_name The netbios name to add.
1236 * @return 0 on success, a Kerberos error code otherwise.
1238 krb5_error_code
smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses
**kerb_addr
,
1239 const char *netbios_name
)
1241 krb5_error_code ret
= 0;
1242 char buf
[MAX_NETBIOSNAME_LEN
];
1244 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1245 krb5_address
**addrs
= NULL
;
1246 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1247 krb5_addresses
*addrs
= NULL
;
1250 *kerb_addr
= (smb_krb5_addresses
*)SMB_MALLOC(sizeof(smb_krb5_addresses
));
1251 if (*kerb_addr
== NULL
) {
1255 /* temporarily duplicate put_name() code here to avoid dependency
1256 * issues for a 5 lines function */
1257 len
= strlen(netbios_name
);
1258 memcpy(buf
, netbios_name
,
1259 (len
< MAX_NETBIOSNAME_LEN
) ? len
: MAX_NETBIOSNAME_LEN
- 1);
1260 if (len
< MAX_NETBIOSNAME_LEN
- 1) {
1261 memset(buf
+ len
, ' ', MAX_NETBIOSNAME_LEN
- 1 - len
);
1263 buf
[MAX_NETBIOSNAME_LEN
- 1] = 0x20;
1265 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1269 addrs
= (krb5_address
**)SMB_MALLOC(sizeof(krb5_address
*) * num_addr
);
1270 if (addrs
== NULL
) {
1271 SAFE_FREE(*kerb_addr
);
1275 memset(addrs
, 0, sizeof(krb5_address
*) * num_addr
);
1277 addrs
[0] = (krb5_address
*)SMB_MALLOC(sizeof(krb5_address
));
1278 if (addrs
[0] == NULL
) {
1280 SAFE_FREE(*kerb_addr
);
1284 addrs
[0]->magic
= KV5M_ADDRESS
;
1285 addrs
[0]->addrtype
= KRB5_ADDR_NETBIOS
;
1286 addrs
[0]->length
= MAX_NETBIOSNAME_LEN
;
1287 addrs
[0]->contents
= (unsigned char *)SMB_MALLOC(addrs
[0]->length
);
1288 if (addrs
[0]->contents
== NULL
) {
1289 SAFE_FREE(addrs
[0]);
1291 SAFE_FREE(*kerb_addr
);
1295 memcpy(addrs
[0]->contents
, buf
, addrs
[0]->length
);
1299 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1301 addrs
= (krb5_addresses
*)SMB_MALLOC(sizeof(krb5_addresses
));
1302 if (addrs
== NULL
) {
1303 SAFE_FREE(*kerb_addr
);
1307 memset(addrs
, 0, sizeof(krb5_addresses
));
1310 addrs
->val
= (krb5_address
*)SMB_MALLOC(sizeof(krb5_address
));
1311 if (addrs
->val
== NULL
) {
1313 SAFE_FREE(*kerb_addr
);
1317 addrs
->val
[0].addr_type
= KRB5_ADDR_NETBIOS
;
1318 addrs
->val
[0].address
.length
= MAX_NETBIOSNAME_LEN
;
1319 addrs
->val
[0].address
.data
= (unsigned char *)SMB_MALLOC(addrs
->val
[0].address
.length
);
1320 if (addrs
->val
[0].address
.data
== NULL
) {
1321 SAFE_FREE(addrs
->val
);
1323 SAFE_FREE(*kerb_addr
);
1327 memcpy(addrs
->val
[0].address
.data
, buf
, addrs
->val
[0].address
.length
);
1330 #error UNKNOWN_KRB5_ADDRESS_FORMAT
1332 (*kerb_addr
)->addrs
= addrs
;
1338 * @brief Get the enctype from a key table entry
1340 * @param[in] kt_entry Key table entry to get the enctype from.
1342 * @return The enctype from the entry.
1344 krb5_enctype
smb_krb5_kt_get_enctype_from_entry(krb5_keytab_entry
*kt_entry
)
1346 return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry
));
1350 * @brief Free the contents of a key table entry.
1352 * @param[in] context The library context.
1354 * @param[in] kt_entry The key table entry to free the contents of.
1356 * @return 0 on success, a Kerberos error code otherwise.
1358 * The pointer itself is not freed.
1360 krb5_error_code
smb_krb5_kt_free_entry(krb5_context context
,
1361 krb5_keytab_entry
*kt_entry
)
1363 /* Try krb5_free_keytab_entry_contents first, since
1364 * MIT Kerberos >= 1.7 has both krb5_free_keytab_entry_contents and
1365 * krb5_kt_free_entry but only has a prototype for the first, while the
1366 * second is considered private.
1368 #if defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
1369 return krb5_free_keytab_entry_contents(context
, kt_entry
);
1370 #elif defined(HAVE_KRB5_KT_FREE_ENTRY)
1371 return krb5_kt_free_entry(context
, kt_entry
);
1373 #error UNKNOWN_KT_FREE_FUNCTION
1379 * @brief Convert an encryption type to a string.
1381 * @param[in] context The library context.
1383 * @param[in] enctype The encryption type.
1385 * @param[in] etype_s A pointer to store the allocated encryption type as a
1388 * @return 0 on success, a Kerberos error code otherwise.
1390 * The caller needs to free the allocated string etype_s.
1392 krb5_error_code
smb_krb5_enctype_to_string(krb5_context context
,
1393 krb5_enctype enctype
,
1396 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
1397 return krb5_enctype_to_string(context
, enctype
, etype_s
); /* Heimdal */
1398 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
1400 krb5_error_code ret
= krb5_enctype_to_string(enctype
, buf
, 256); /* MIT */
1404 *etype_s
= SMB_STRDUP(buf
);
1410 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
1414 /* This MAX_NAME_LEN is a constant defined in krb5.h */
1415 #ifndef MAX_KEYTAB_NAME_LEN
1416 #define MAX_KEYTAB_NAME_LEN 1100
1420 * @brief Open a key table readonly or with readwrite access.
1422 * Allows one to use a different keytab than the default one using a relative
1423 * path to the keytab.
1425 * @param[in] context The library context
1427 * @param[in] keytab_name_req The path to the key table.
1429 * @param[in] write_access Open with readwrite access.
1431 * @param[in] keytab A pointer to the opened key table.
1433 * The keytab pointer should be freed using krb5_kt_close().
1435 * @return 0 on success, a Kerberos error code otherwise.
1437 krb5_error_code
smb_krb5_kt_open_relative(krb5_context context
,
1438 const char *keytab_name_req
,
1440 krb5_keytab
*keytab
)
1442 krb5_error_code ret
= 0;
1443 TALLOC_CTX
*mem_ctx
;
1444 char keytab_string
[MAX_KEYTAB_NAME_LEN
];
1445 char *kt_str
= NULL
;
1446 bool found_valid_name
= false;
1447 const char *pragma
= "FILE";
1448 const char *tmp
= NULL
;
1450 if (!write_access
&& !keytab_name_req
) {
1451 /* caller just wants to read the default keytab readonly, so be it */
1452 return krb5_kt_default(context
, keytab
);
1455 mem_ctx
= talloc_init("smb_krb5_kt_open_relative");
1460 #ifdef HAVE_WRFILE_KEYTAB
1466 if (keytab_name_req
) {
1468 if (strlen(keytab_name_req
) > MAX_KEYTAB_NAME_LEN
) {
1469 ret
= KRB5_CONFIG_NOTENUFSPACE
;
1473 if ((strncmp(keytab_name_req
, "WRFILE:", 7) == 0) ||
1474 (strncmp(keytab_name_req
, "FILE:", 5) == 0)) {
1475 tmp
= keytab_name_req
;
1479 tmp
= talloc_asprintf(mem_ctx
, "%s:%s", pragma
, keytab_name_req
);
1488 /* we need to handle more complex keytab_strings, like:
1489 * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
1491 ret
= krb5_kt_default_name(context
, &keytab_string
[0], MAX_KEYTAB_NAME_LEN
- 2);
1496 DBG_DEBUG("krb5_kt_default_name returned %s\n", keytab_string
);
1498 tmp
= talloc_strdup(mem_ctx
, keytab_string
);
1504 if (strncmp(tmp
, "ANY:", 4) == 0) {
1508 memset(&keytab_string
, '\0', sizeof(keytab_string
));
1510 while (next_token_talloc(mem_ctx
, &tmp
, &kt_str
, ",")) {
1511 if (strncmp(kt_str
, "WRFILE:", 7) == 0) {
1512 found_valid_name
= true;
1517 if (strncmp(kt_str
, "FILE:", 5) == 0) {
1518 found_valid_name
= true;
1523 if (tmp
[0] == '/') {
1524 /* Treat as a FILE: keytab definition. */
1525 found_valid_name
= true;
1528 if (found_valid_name
) {
1529 if (tmp
[0] != '/') {
1530 ret
= KRB5_KT_BADNAME
;
1534 tmp
= talloc_asprintf(mem_ctx
, "%s:%s", pragma
, tmp
);
1543 if (!found_valid_name
) {
1544 ret
= KRB5_KT_UNKNOWN_TYPE
;
1549 DBG_DEBUG("resolving: %s\n", tmp
);
1550 ret
= krb5_kt_resolve(context
, tmp
, keytab
);
1553 TALLOC_FREE(mem_ctx
);
1558 * @brief Open a key table readonly or with readwrite access.
1560 * Allows one to use a different keytab than the default one. The path needs to be
1561 * an absolute path or an error will be returned.
1563 * @param[in] context The library context
1565 * @param[in] keytab_name_req The path to the key table.
1567 * @param[in] write_access Open with readwrite access.
1569 * @param[in] keytab A pointer to the opened key table.
1571 * The keytab pointer should be freed using krb5_kt_close().
1573 * @return 0 on success, a Kerberos error code otherwise.
1575 krb5_error_code
smb_krb5_kt_open(krb5_context context
,
1576 const char *keytab_name_req
,
1578 krb5_keytab
*keytab
)
1582 if (keytab_name_req
== NULL
) {
1583 return KRB5_KT_BADNAME
;
1586 if (keytab_name_req
[0] == '/') {
1590 cmp
= strncmp(keytab_name_req
, "FILE:/", 6);
1595 cmp
= strncmp(keytab_name_req
, "WRFILE:/", 8);
1600 DBG_WARNING("ERROR: Invalid keytab name: %s\n", keytab_name_req
);
1602 return KRB5_KT_BADNAME
;
1605 return smb_krb5_kt_open_relative(context
,
1612 * @brief Get a key table name.
1614 * @param[in] mem_ctx The talloc context to use for allocation.
1616 * @param[in] context The library context.
1618 * @param[in] keytab The key table to get the name from.
1620 * @param[in] keytab_name A talloc'ed string of the key table name.
1622 * The talloc'ed name string needs to be freed with talloc_free().
1624 * @return 0 on success, a Kerberos error code otherwise.
1626 krb5_error_code
smb_krb5_kt_get_name(TALLOC_CTX
*mem_ctx
,
1627 krb5_context context
,
1629 const char **keytab_name
)
1631 char keytab_string
[MAX_KEYTAB_NAME_LEN
];
1632 krb5_error_code ret
= 0;
1634 ret
= krb5_kt_get_name(context
, keytab
,
1635 keytab_string
, MAX_KEYTAB_NAME_LEN
- 2);
1640 *keytab_name
= talloc_strdup(mem_ctx
, keytab_string
);
1641 if (!*keytab_name
) {
1649 * @brief Seek and delete old entries in a keytab based on the passed
1652 * @param[in] context The KRB5 context to use.
1654 * @param[in] keytab The keytab to operate on.
1656 * @param[in] keep_old_kvno Keep the entries with the previous kvno.
1658 * @param[in] kvno The kvno to use.
1660 * @param[in] enctype_only Only evaluate the enctype argument if true
1662 * @param[in] enctype Only search for entries with the specified enctype
1664 * @param[in] princ_s The principal as a string to search for.
1666 * @param[in] princ The principal as a krb5_principal to search for.
1668 * @param[in] flush Whether to flush the complete keytab.
1670 * @retval 0 on Success
1672 * @return An appropriate KRB5 error code.
1674 krb5_error_code
smb_krb5_kt_seek_and_delete_old_entries(krb5_context context
,
1679 krb5_enctype enctype
,
1680 const char *princ_s
,
1681 krb5_principal princ
,
1684 krb5_error_code ret
;
1685 krb5_kt_cursor cursor
;
1686 krb5_keytab_entry kt_entry
;
1687 char *ktprinc
= NULL
;
1688 krb5_kvno old_kvno
= kvno
- 1;
1689 TALLOC_CTX
*tmp_ctx
;
1692 SMB_ASSERT(!keep_old_kvno
);
1693 SMB_ASSERT(!enctype_only
);
1694 SMB_ASSERT(princ_s
== NULL
);
1695 SMB_ASSERT(princ
== NULL
);
1697 SMB_ASSERT(princ_s
!= NULL
);
1698 SMB_ASSERT(princ
!= NULL
);
1701 ZERO_STRUCT(cursor
);
1702 ZERO_STRUCT(kt_entry
);
1705 * Start with talloc_new() and only then call krb5_kt_start_seq_get().
1706 * If any of them fails, the cleanup code is simpler.
1708 tmp_ctx
= talloc_new(NULL
);
1709 if (tmp_ctx
== NULL
) {
1713 ret
= krb5_kt_start_seq_get(context
, keytab
, &cursor
);
1714 if (ret
== KRB5_KT_END
|| ret
== ENOENT
) {
1716 talloc_free(tmp_ctx
);
1720 DEBUG(3, (__location__
": Will try to delete old keytab entries\n"));
1721 while (!krb5_kt_next_entry(context
, keytab
, &kt_entry
, &cursor
)) {
1722 bool name_ok
= false;
1723 krb5_enctype kt_entry_enctype
=
1724 smb_krb5_kt_get_enctype_from_entry(&kt_entry
);
1726 if (princ_s
!= NULL
) {
1727 ret
= smb_krb5_unparse_name(tmp_ctx
, context
,
1731 DEBUG(1, (__location__
1732 ": smb_krb5_unparse_name failed "
1733 "(%s)\n", error_message(ret
)));
1737 #ifdef HAVE_KRB5_KT_COMPARE
1738 name_ok
= krb5_kt_compare(context
, &kt_entry
,
1741 name_ok
= (strcmp(ktprinc
, princ_s
) == 0);
1745 DEBUG(10, (__location__
": ignoring keytab "
1746 "entry principal %s, kvno = %d\n",
1747 ktprinc
, kt_entry
.vno
));
1750 * just free this entry and continue. */
1751 ret
= smb_krb5_kt_free_entry(context
,
1753 ZERO_STRUCT(kt_entry
);
1755 DEBUG(1, (__location__
1756 ": smb_krb5_kt_free_entry "
1758 error_message(ret
)));
1762 TALLOC_FREE(ktprinc
);
1766 TALLOC_FREE(ktprinc
);
1769 /*------------------------------------------------------------
1770 * Save the entries with kvno - 1. This is what microsoft does
1771 * to allow people with existing sessions that have kvno - 1
1772 * to still work. Otherwise, when the password for the machine
1773 * changes, all kerberized sessions will 'break' until either
1774 * the client reboots or the client's session key expires and
1775 * they get a new session ticket with the new kvno.
1776 * Some keytab files only store the kvno in 8bits, limit
1777 * the compare accordingly.
1780 if (keep_old_kvno
&& ((kt_entry
.vno
& 0xff) == (old_kvno
& 0xff))) {
1781 DEBUG(5, (__location__
": Saving previous (kvno %d) "
1782 "entry for principal: %s.\n",
1784 princ_s
!= NULL
? princ_s
: "UNKNOWN"));
1789 ((kt_entry
.vno
& 0xff) == (kvno
& 0xff)) &&
1790 (kt_entry_enctype
!= enctype
))
1792 DEBUG(5, (__location__
": Saving entry with kvno [%d] "
1793 "enctype [%d] for principal: %s.\n",
1794 kvno
, kt_entry_enctype
,
1795 princ_s
!= NULL
? princ_s
: "UNKNOWN"));
1799 DEBUG(5, (__location__
": Found old entry for principal: %s "
1800 "(kvno %d) - trying to remove it.\n",
1801 princ_s
!= NULL
? princ_s
: "UNKNOWN",
1804 ret
= krb5_kt_end_seq_get(context
, keytab
, &cursor
);
1805 ZERO_STRUCT(cursor
);
1807 DEBUG(1, (__location__
": krb5_kt_end_seq_get() "
1808 "failed (%s)\n", error_message(ret
)));
1811 ret
= krb5_kt_remove_entry(context
, keytab
, &kt_entry
);
1813 DEBUG(1, (__location__
": krb5_kt_remove_entry() "
1814 "failed (%s)\n", error_message(ret
)));
1818 DEBUG(5, (__location__
": removed old entry for principal: "
1820 princ_s
!= NULL
? princ_s
: "UNKNOWN",
1823 ret
= krb5_kt_start_seq_get(context
, keytab
, &cursor
);
1825 DEBUG(1, (__location__
": krb5_kt_start_seq() failed "
1826 "(%s)\n", error_message(ret
)));
1829 ret
= smb_krb5_kt_free_entry(context
, &kt_entry
);
1830 ZERO_STRUCT(kt_entry
);
1832 DEBUG(1, (__location__
": krb5_kt_remove_entry() "
1833 "failed (%s)\n", error_message(ret
)));
1839 talloc_free(tmp_ctx
);
1840 if (!all_zero((uint8_t *)&kt_entry
, sizeof(kt_entry
))) {
1841 smb_krb5_kt_free_entry(context
, &kt_entry
);
1843 if (!all_zero((uint8_t *)&cursor
, sizeof(cursor
))) {
1844 krb5_kt_end_seq_get(context
, keytab
, &cursor
);
1850 * @brief Add a keytab entry for the given principal
1852 * @param[in] context The krb5 context to use.
1854 * @param[in] keytab The keytab to add the entry to.
1856 * @param[in] kvno The kvno to use.
1858 * @param[in] princ_s The principal as a string.
1860 * @param[in] salt_principal The salt principal to salt the password with.
1861 * Only needed for keys which support salting.
1862 * If no salt is used set no_salt to false and
1865 * @param[in] enctype The encryption type of the keytab entry.
1867 * @param[in] password The password of the keytab entry.
1869 * @param[in] no_salt If the password should not be salted. Normally
1870 * this is only set to false for encryption types
1871 * which do not support salting like RC4.
1873 * @retval 0 on Success
1875 * @return A corresponding KRB5 error code.
1877 * @see smb_krb5_kt_open()
1879 krb5_error_code
smb_krb5_kt_add_entry(krb5_context context
,
1882 const char *princ_s
,
1883 const char *salt_principal
,
1884 krb5_enctype enctype
,
1885 krb5_data
*password
,
1888 krb5_error_code ret
;
1889 krb5_keytab_entry kt_entry
;
1890 krb5_principal princ
= NULL
;
1891 krb5_keyblock
*keyp
;
1893 ZERO_STRUCT(kt_entry
);
1895 ret
= smb_krb5_parse_name(context
, princ_s
, &princ
);
1897 DEBUG(1, (__location__
": smb_krb5_parse_name(%s) "
1898 "failed (%s)\n", princ_s
, error_message(ret
)));
1902 /* Seek and delete old keytab entries */
1903 ret
= smb_krb5_kt_seek_and_delete_old_entries(context
,
1905 true, /* keep_old_kvno */
1907 true, /* enctype_only */
1916 /* If we get here, we have deleted all the old entries with kvno's
1917 * not equal to the current kvno-1. */
1919 keyp
= KRB5_KT_KEY(&kt_entry
);
1922 KRB5_KEY_DATA(keyp
) = (KRB5_KEY_DATA_CAST
*)SMB_MALLOC(password
->length
);
1923 if (KRB5_KEY_DATA(keyp
) == NULL
) {
1927 memcpy(KRB5_KEY_DATA(keyp
), password
->data
, password
->length
);
1928 KRB5_KEY_LENGTH(keyp
) = password
->length
;
1929 KRB5_KEY_TYPE(keyp
) = enctype
;
1931 krb5_principal salt_princ
= NULL
;
1933 /* Now add keytab entries for all encryption types */
1934 ret
= smb_krb5_parse_name(context
, salt_principal
, &salt_princ
);
1936 DBG_WARNING("krb5_parse_name(%s) failed (%s)\n",
1937 salt_principal
, error_message(ret
));
1941 ret
= smb_krb5_create_key_from_string(context
,
1947 krb5_free_principal(context
, salt_princ
);
1953 kt_entry
.principal
= princ
;
1954 kt_entry
.vno
= kvno
;
1956 DEBUG(3, (__location__
": adding keytab entry for (%s) with "
1957 "encryption type (%d) and version (%d)\n",
1958 princ_s
, enctype
, kt_entry
.vno
));
1959 ret
= krb5_kt_add_entry(context
, keytab
, &kt_entry
);
1960 krb5_free_keyblock_contents(context
, keyp
);
1961 ZERO_STRUCT(kt_entry
);
1963 DEBUG(1, (__location__
": adding entry to keytab "
1964 "failed (%s)\n", error_message(ret
)));
1970 krb5_free_principal(context
, princ
);
1976 #if defined(HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE) && \
1977 defined(HAVE_KRB5_GET_CREDS_OPT_ALLOC) && \
1978 defined(HAVE_KRB5_GET_CREDS)
1979 static krb5_error_code
smb_krb5_get_credentials_for_user_opt(krb5_context context
,
1982 krb5_principal server
,
1983 krb5_principal impersonate_princ
,
1984 krb5_creds
**out_creds
)
1986 krb5_error_code ret
;
1987 krb5_get_creds_opt opt
;
1989 ret
= krb5_get_creds_opt_alloc(context
, &opt
);
1993 krb5_get_creds_opt_add_options(context
, opt
, KRB5_GC_FORWARDABLE
);
1995 if (impersonate_princ
) {
1996 ret
= krb5_get_creds_opt_set_impersonate(context
, opt
,
2003 ret
= krb5_get_creds(context
, opt
, ccache
, server
, out_creds
);
2010 krb5_get_creds_opt_free(context
, opt
);
2014 #endif /* HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE */
2016 #ifdef HAVE_KRB5_GET_CREDENTIALS_FOR_USER
2018 #if !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER
2019 krb5_error_code KRB5_CALLCONV
2020 krb5_get_credentials_for_user(krb5_context context
, krb5_flags options
,
2021 krb5_ccache ccache
, krb5_creds
*in_creds
,
2022 krb5_data
*subject_cert
,
2023 krb5_creds
**out_creds
);
2024 #endif /* !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER */
2026 static krb5_error_code
smb_krb5_get_credentials_for_user(krb5_context context
,
2029 krb5_principal server
,
2030 krb5_principal impersonate_princ
,
2031 krb5_creds
**out_creds
)
2033 krb5_error_code ret
;
2034 krb5_creds in_creds
;
2036 ZERO_STRUCT(in_creds
);
2038 if (impersonate_princ
) {
2040 in_creds
.server
= me
;
2041 in_creds
.client
= impersonate_princ
;
2043 ret
= krb5_get_credentials_for_user(context
,
2044 0, /* krb5_flags options */
2047 NULL
, /* krb5_data *subject_cert */
2050 in_creds
.client
= me
;
2051 in_creds
.server
= server
;
2053 ret
= krb5_get_credentials(context
, 0, ccache
,
2054 &in_creds
, out_creds
);
2059 #endif /* HAVE_KRB5_GET_CREDENTIALS_FOR_USER */
2062 * smb_krb5_get_credentials
2064 * @brief Get krb5 credentials for a server
2066 * @param[in] context An initialized krb5_context
2067 * @param[in] ccache An initialized krb5_ccache
2068 * @param[in] me The krb5_principal of the caller
2069 * @param[in] server The krb5_principal of the requested service
2070 * @param[in] impersonate_princ The krb5_principal of a user to impersonate as (optional)
2071 * @param[out] out_creds The returned krb5_creds structure
2072 * @return krb5_error_code
2075 krb5_error_code
smb_krb5_get_credentials(krb5_context context
,
2078 krb5_principal server
,
2079 krb5_principal impersonate_princ
,
2080 krb5_creds
**out_creds
)
2082 krb5_error_code ret
;
2083 krb5_creds
*creds
= NULL
;
2085 if (out_creds
!= NULL
) {
2089 if (impersonate_princ
) {
2090 #ifdef HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE /* Heimdal */
2091 ret
= smb_krb5_get_credentials_for_user_opt(context
, ccache
, me
, server
, impersonate_princ
, &creds
);
2092 #elif defined(HAVE_KRB5_GET_CREDENTIALS_FOR_USER) /* MIT */
2093 ret
= smb_krb5_get_credentials_for_user(context
, ccache
, me
, server
, impersonate_princ
, &creds
);
2098 krb5_creds in_creds
;
2100 ZERO_STRUCT(in_creds
);
2102 in_creds
.client
= me
;
2103 in_creds
.server
= server
;
2105 ret
= krb5_get_credentials(context
, 0, ccache
,
2118 krb5_free_creds(context
, creds
);
2125 * @brief Initialize a krb5_keyblock with the given data.
2127 * Initializes a new keyblock, allocates the contents for the key and
2128 * copies the data into the keyblock.
2130 * @param[in] context The library context
2132 * @param[in] enctype The encryption type.
2134 * @param[in] data The date to initialize the keyblock with.
2136 * @param[in] length The length of the keyblock.
2138 * @param[in] key Newly allocated keyblock structure.
2140 * The key date must be freed using krb5_free_keyblock_contents() when it is
2143 * @return 0 on success, a Kerberos error code otherwise.
2145 krb5_error_code
smb_krb5_keyblock_init_contents(krb5_context context
,
2146 krb5_enctype enctype
,
2151 #if defined(HAVE_KRB5_KEYBLOCK_INIT)
2152 return krb5_keyblock_init(context
, enctype
, data
, length
, key
);
2154 memset(key
, 0, sizeof(krb5_keyblock
));
2155 KRB5_KEY_DATA(key
) = SMB_MALLOC(length
);
2156 if (NULL
== KRB5_KEY_DATA(key
)) {
2159 memcpy(KRB5_KEY_DATA(key
), data
, length
);
2160 KRB5_KEY_LENGTH(key
) = length
;
2161 KRB5_KEY_TYPE(key
) = enctype
;
2167 * @brief Simulate a kinit by putting the tgt in the given credential cache.
2169 * This function uses a keyblock rather than needing the original password.
2171 * @param[in] ctx The library context
2173 * @param[in] cc The credential cache to put the tgt in.
2175 * @param[in] principal The client princial
2177 * @param[in] keyblock The keyblock to use.
2179 * @param[in] target_service The service name of the initial credentials (or NULL).
2181 * @param[in] krb_options Initial credential options.
2183 * @param[in] expire_time A pointer to store the expiration time of the
2184 * credentials (or NULL).
2186 * @param[in] kdc_time A pointer to store the time when the ticket becomes
2189 * @return 0 on success, a Kerberos error code otherwise.
2191 krb5_error_code
smb_krb5_kinit_keyblock_ccache(krb5_context ctx
,
2193 krb5_principal principal
,
2194 krb5_keyblock
*keyblock
,
2195 const char *target_service
,
2196 krb5_get_init_creds_opt
*krb_options
,
2197 time_t *expire_time
,
2200 krb5_error_code code
= 0;
2201 krb5_creds my_creds
;
2203 #if defined(HAVE_KRB5_GET_INIT_CREDS_KEYBLOCK)
2204 code
= krb5_get_init_creds_keyblock(ctx
, &my_creds
, principal
,
2205 keyblock
, 0, target_service
,
2207 #elif defined(HAVE_KRB5_GET_INIT_CREDS_KEYTAB)
2209 #define SMB_CREDS_KEYTAB "MEMORY:tmp_kinit_keyblock_ccache"
2210 char tmp_name
[64] = {0};
2211 krb5_keytab_entry entry
;
2215 memset(&entry
, 0, sizeof(entry
));
2216 entry
.principal
= principal
;
2217 *(KRB5_KT_KEY(&entry
)) = *keyblock
;
2219 rc
= snprintf(tmp_name
, sizeof(tmp_name
),
2224 return KRB5_KT_BADNAME
;
2226 code
= krb5_kt_resolve(ctx
, tmp_name
, &keytab
);
2231 code
= krb5_kt_add_entry(ctx
, keytab
, &entry
);
2233 (void)krb5_kt_close(ctx
, keytab
);
2237 code
= krb5_get_init_creds_keytab(ctx
, &my_creds
, principal
,
2238 keytab
, 0, target_service
,
2240 (void)krb5_kt_close(ctx
, keytab
);
2243 #error krb5_get_init_creds_keyblock not available!
2249 #ifndef SAMBA4_USES_HEIMDAL /* MIT */
2251 * We need to store the principal as returned from the KDC to the
2252 * credentials cache. If we don't do that the KRB5 library is not
2253 * able to find the tickets it is looking for
2255 principal
= my_creds
.client
;
2257 code
= krb5_cc_initialize(ctx
, cc
, principal
);
2262 code
= krb5_cc_store_cred(ctx
, cc
, &my_creds
);
2268 *expire_time
= (time_t) my_creds
.times
.endtime
;
2272 *kdc_time
= (time_t) my_creds
.times
.starttime
;
2277 krb5_free_cred_contents(ctx
, &my_creds
);
2282 * @brief Simulate a kinit by putting the tgt in the given credential cache.
2284 * @param[in] ctx The library context
2286 * @param[in] cc The credential cache to put the tgt in.
2288 * @param[in] principal The client princial
2290 * @param[in] password The password (or NULL).
2292 * @param[in] target_service The service name of the initial credentials (or NULL).
2294 * @param[in] krb_options Initial credential options.
2296 * @param[in] expire_time A pointer to store the expiration time of the
2297 * credentials (or NULL).
2299 * @param[in] kdc_time A pointer to store the time when the ticket becomes
2302 * @return 0 on success, a Kerberos error code otherwise.
2304 krb5_error_code
smb_krb5_kinit_password_ccache(krb5_context ctx
,
2306 krb5_principal principal
,
2307 const char *password
,
2308 const char *target_service
,
2309 krb5_get_init_creds_opt
*krb_options
,
2310 time_t *expire_time
,
2313 krb5_error_code code
= 0;
2314 krb5_creds my_creds
;
2316 code
= krb5_get_init_creds_password(ctx
, &my_creds
, principal
,
2317 password
, NULL
, NULL
, 0,
2318 target_service
, krb_options
);
2324 * We need to store the principal as returned from the KDC to the
2325 * credentials cache. If we don't do that the KRB5 library is not
2326 * able to find the tickets it is looking for
2328 principal
= my_creds
.client
;
2329 code
= krb5_cc_initialize(ctx
, cc
, principal
);
2334 code
= krb5_cc_store_cred(ctx
, cc
, &my_creds
);
2340 *expire_time
= (time_t) my_creds
.times
.endtime
;
2344 *kdc_time
= (time_t) my_creds
.times
.starttime
;
2349 krb5_free_cred_contents(ctx
, &my_creds
);
2353 #ifdef SAMBA4_USES_HEIMDAL
2355 * @brief Simulate a kinit by putting the tgt in the given credential cache.
2357 * @param[in] ctx The library context
2359 * @param[in] cc The credential cache to store the tgt in.
2361 * @param[in] principal The initial client princial.
2363 * @param[in] password The password (or NULL).
2365 * @param[in] impersonate_principal The impersonation principal (or NULL).
2367 * @param[in] self_service The local service for S4U2Self if
2368 * impersonate_principal is specified).
2370 * @param[in] target_service The service name of the initial credentials
2371 * (kpasswd/REALM or a remote service). It defaults
2372 * to the krbtgt if NULL.
2374 * @param[in] krb_options Initial credential options.
2376 * @param[in] expire_time A pointer to store the expiration time of the
2377 * credentials (or NULL).
2379 * @param[in] kdc_time A pointer to store the time when the ticket becomes
2382 * @return 0 on success, a Kerberos error code otherwise.
2384 krb5_error_code
smb_krb5_kinit_s4u2_ccache(krb5_context ctx
,
2385 krb5_ccache store_cc
,
2386 krb5_principal init_principal
,
2387 const char *init_password
,
2388 krb5_principal impersonate_principal
,
2389 const char *self_service
,
2390 const char *target_service
,
2391 krb5_get_init_creds_opt
*krb_options
,
2392 time_t *expire_time
,
2395 krb5_error_code code
= 0;
2396 krb5_get_creds_opt options
;
2397 krb5_principal store_principal
;
2398 krb5_creds store_creds
;
2399 krb5_creds
*s4u2self_creds
;
2400 Ticket s4u2self_ticket
;
2401 size_t s4u2self_ticketlen
;
2402 krb5_creds
*s4u2proxy_creds
;
2403 krb5_principal self_princ
;
2405 krb5_principal target_princ
;
2407 const char *self_realm
;
2408 const char *client_realm
= NULL
;
2409 krb5_principal blacklist_principal
= NULL
;
2410 krb5_principal whitelist_principal
= NULL
;
2412 code
= krb5_get_init_creds_password(ctx
, &store_creds
,
2423 store_principal
= init_principal
;
2426 * We are trying S4U2Self now:
2428 * As we do not want to expose our TGT in the
2429 * krb5_ccache, which is also holds the impersonated creds.
2431 * Some low level krb5/gssapi function might use the TGT
2432 * identity and let the client act as our machine account.
2434 * We need to avoid that and use a temporary krb5_ccache
2435 * in order to pass our TGT to the krb5_get_creds() function.
2437 code
= krb5_cc_new_unique(ctx
, NULL
, NULL
, &tmp_cc
);
2439 krb5_free_cred_contents(ctx
, &store_creds
);
2443 code
= krb5_cc_initialize(ctx
, tmp_cc
, store_creds
.client
);
2445 krb5_cc_destroy(ctx
, tmp_cc
);
2446 krb5_free_cred_contents(ctx
, &store_creds
);
2450 code
= krb5_cc_store_cred(ctx
, tmp_cc
, &store_creds
);
2452 krb5_free_cred_contents(ctx
, &store_creds
);
2453 krb5_cc_destroy(ctx
, tmp_cc
);
2458 * we need to remember the client principal of our
2459 * TGT and make sure the KDC does not return this
2460 * in the impersonated tickets. This can happen
2461 * if the KDC does not support S4U2Self and S4U2Proxy.
2463 blacklist_principal
= store_creds
.client
;
2464 store_creds
.client
= NULL
;
2465 krb5_free_cred_contents(ctx
, &store_creds
);
2468 * Check if we also need S4U2Proxy or if S4U2Self is
2469 * enough in order to get a ticket for the target.
2471 if (target_service
== NULL
) {
2473 } else if (strcmp(target_service
, self_service
) == 0) {
2480 * For S4U2Self we need our own service principal,
2481 * which belongs to our own realm (available on
2482 * our client principal).
2484 self_realm
= krb5_principal_get_realm(ctx
, init_principal
);
2486 code
= krb5_parse_name(ctx
, self_service
, &self_princ
);
2488 krb5_free_principal(ctx
, blacklist_principal
);
2489 krb5_cc_destroy(ctx
, tmp_cc
);
2493 code
= krb5_principal_set_realm(ctx
, self_princ
, self_realm
);
2495 krb5_free_principal(ctx
, blacklist_principal
);
2496 krb5_free_principal(ctx
, self_princ
);
2497 krb5_cc_destroy(ctx
, tmp_cc
);
2501 code
= krb5_get_creds_opt_alloc(ctx
, &options
);
2503 krb5_free_principal(ctx
, blacklist_principal
);
2504 krb5_free_principal(ctx
, self_princ
);
2505 krb5_cc_destroy(ctx
, tmp_cc
);
2511 * If we want S4U2Proxy, we need the forwardable flag
2512 * on the S4U2Self ticket.
2514 krb5_get_creds_opt_set_options(ctx
, options
, KRB5_GC_FORWARDABLE
);
2517 code
= krb5_get_creds_opt_set_impersonate(ctx
, options
,
2518 impersonate_principal
);
2520 krb5_get_creds_opt_free(ctx
, options
);
2521 krb5_free_principal(ctx
, blacklist_principal
);
2522 krb5_free_principal(ctx
, self_princ
);
2523 krb5_cc_destroy(ctx
, tmp_cc
);
2527 code
= krb5_get_creds(ctx
, options
, tmp_cc
,
2528 self_princ
, &s4u2self_creds
);
2529 krb5_get_creds_opt_free(ctx
, options
);
2530 krb5_free_principal(ctx
, self_princ
);
2532 krb5_free_principal(ctx
, blacklist_principal
);
2533 krb5_cc_destroy(ctx
, tmp_cc
);
2538 krb5_cc_destroy(ctx
, tmp_cc
);
2541 * Now make sure we store the impersonated principal
2542 * and creds instead of the TGT related stuff
2543 * in the krb5_ccache of the caller.
2545 code
= krb5_copy_creds_contents(ctx
, s4u2self_creds
,
2547 krb5_free_creds(ctx
, s4u2self_creds
);
2553 * It's important to store the principal the KDC
2554 * returned, as otherwise the caller would not find
2555 * the S4U2Self ticket in the krb5_ccache lookup.
2557 store_principal
= store_creds
.client
;
2562 * We are trying S4U2Proxy:
2564 * We need the ticket from the S4U2Self step
2565 * and our TGT in order to get the delegated ticket.
2567 code
= decode_Ticket((const uint8_t *)s4u2self_creds
->ticket
.data
,
2568 s4u2self_creds
->ticket
.length
,
2570 &s4u2self_ticketlen
);
2572 krb5_free_creds(ctx
, s4u2self_creds
);
2573 krb5_free_principal(ctx
, blacklist_principal
);
2574 krb5_cc_destroy(ctx
, tmp_cc
);
2579 * we need to remember the client principal of the
2580 * S4U2Self stage and as it needs to match the one we
2581 * will get for the S4U2Proxy stage. We need this
2582 * in order to detect KDCs which does not support S4U2Proxy.
2584 whitelist_principal
= s4u2self_creds
->client
;
2585 s4u2self_creds
->client
= NULL
;
2586 krb5_free_creds(ctx
, s4u2self_creds
);
2589 * For S4U2Proxy we also got a target service principal,
2590 * which also belongs to our own realm (available on
2591 * our client principal).
2593 code
= krb5_parse_name(ctx
, target_service
, &target_princ
);
2595 free_Ticket(&s4u2self_ticket
);
2596 krb5_free_principal(ctx
, whitelist_principal
);
2597 krb5_free_principal(ctx
, blacklist_principal
);
2598 krb5_cc_destroy(ctx
, tmp_cc
);
2602 code
= krb5_principal_set_realm(ctx
, target_princ
, self_realm
);
2604 free_Ticket(&s4u2self_ticket
);
2605 krb5_free_principal(ctx
, target_princ
);
2606 krb5_free_principal(ctx
, whitelist_principal
);
2607 krb5_free_principal(ctx
, blacklist_principal
);
2608 krb5_cc_destroy(ctx
, tmp_cc
);
2612 code
= krb5_get_creds_opt_alloc(ctx
, &options
);
2614 free_Ticket(&s4u2self_ticket
);
2615 krb5_free_principal(ctx
, target_princ
);
2616 krb5_free_principal(ctx
, whitelist_principal
);
2617 krb5_free_principal(ctx
, blacklist_principal
);
2618 krb5_cc_destroy(ctx
, tmp_cc
);
2622 krb5_get_creds_opt_set_options(ctx
, options
, KRB5_GC_FORWARDABLE
);
2623 krb5_get_creds_opt_set_options(ctx
, options
, KRB5_GC_CONSTRAINED_DELEGATION
);
2625 code
= krb5_get_creds_opt_set_ticket(ctx
, options
, &s4u2self_ticket
);
2626 free_Ticket(&s4u2self_ticket
);
2628 krb5_get_creds_opt_free(ctx
, options
);
2629 krb5_free_principal(ctx
, target_princ
);
2630 krb5_free_principal(ctx
, whitelist_principal
);
2631 krb5_free_principal(ctx
, blacklist_principal
);
2632 krb5_cc_destroy(ctx
, tmp_cc
);
2636 code
= krb5_get_creds(ctx
, options
, tmp_cc
,
2637 target_princ
, &s4u2proxy_creds
);
2638 krb5_get_creds_opt_free(ctx
, options
);
2639 krb5_free_principal(ctx
, target_princ
);
2640 krb5_cc_destroy(ctx
, tmp_cc
);
2642 krb5_free_principal(ctx
, whitelist_principal
);
2643 krb5_free_principal(ctx
, blacklist_principal
);
2648 * Now make sure we store the impersonated principal
2649 * and creds instead of the TGT related stuff
2650 * in the krb5_ccache of the caller.
2652 code
= krb5_copy_creds_contents(ctx
, s4u2proxy_creds
,
2654 krb5_free_creds(ctx
, s4u2proxy_creds
);
2656 krb5_free_principal(ctx
, whitelist_principal
);
2657 krb5_free_principal(ctx
, blacklist_principal
);
2662 * It's important to store the principal the KDC
2663 * returned, as otherwise the caller would not find
2664 * the S4U2Self ticket in the krb5_ccache lookup.
2666 store_principal
= store_creds
.client
;
2669 if (blacklist_principal
&&
2670 krb5_principal_compare(ctx
, store_creds
.client
, blacklist_principal
)) {
2674 code
= krb5_unparse_name(ctx
, blacklist_principal
, &sp
);
2678 code
= krb5_unparse_name(ctx
, impersonate_principal
, &ip
);
2682 DBG_WARNING("KDC returned self principal[%s] while impersonating [%s]\n",
2683 sp
?sp
:"<no memory>",
2684 ip
?ip
:"<no memory>");
2689 krb5_free_principal(ctx
, whitelist_principal
);
2690 krb5_free_principal(ctx
, blacklist_principal
);
2691 krb5_free_cred_contents(ctx
, &store_creds
);
2692 return KRB5_FWD_BAD_PRINCIPAL
;
2694 if (blacklist_principal
) {
2695 krb5_free_principal(ctx
, blacklist_principal
);
2698 if (whitelist_principal
&&
2699 !krb5_principal_compare(ctx
, store_creds
.client
, whitelist_principal
)) {
2703 code
= krb5_unparse_name(ctx
, store_creds
.client
, &sp
);
2707 code
= krb5_unparse_name(ctx
, whitelist_principal
, &ep
);
2711 DBG_WARNING("KDC returned wrong principal[%s] we expected [%s]\n",
2712 sp
?sp
:"<no memory>",
2713 ep
?ep
:"<no memory>");
2718 krb5_free_principal(ctx
, whitelist_principal
);
2719 krb5_free_cred_contents(ctx
, &store_creds
);
2720 return KRB5_FWD_BAD_PRINCIPAL
;
2722 if (whitelist_principal
) {
2723 krb5_free_principal(ctx
, whitelist_principal
);
2726 code
= krb5_cc_initialize(ctx
, store_cc
, store_principal
);
2728 krb5_free_cred_contents(ctx
, &store_creds
);
2732 code
= krb5_cc_store_cred(ctx
, store_cc
, &store_creds
);
2734 krb5_free_cred_contents(ctx
, &store_creds
);
2738 client_realm
= krb5_principal_get_realm(ctx
, store_creds
.client
);
2739 if (client_realm
!= NULL
) {
2741 * Because the CANON flag doesn't have any impact
2742 * on the impersonate_principal => store_creds.client
2743 * realm mapping. We need to store the credentials twice,
2744 * once with the returned realm and once with the
2745 * realm of impersonate_principal.
2747 code
= krb5_principal_set_realm(ctx
, store_creds
.server
,
2750 krb5_free_cred_contents(ctx
, &store_creds
);
2754 code
= krb5_cc_store_cred(ctx
, store_cc
, &store_creds
);
2756 krb5_free_cred_contents(ctx
, &store_creds
);
2762 *expire_time
= (time_t) store_creds
.times
.endtime
;
2766 *kdc_time
= (time_t) store_creds
.times
.starttime
;
2769 krb5_free_cred_contents(ctx
, &store_creds
);
2776 static bool princ_compare_no_dollar(krb5_context ctx
,
2780 krb5_principal mod
= NULL
;
2783 if (a
->length
== 1 && b
->length
== 1 &&
2784 a
->data
[0].length
!= 0 && b
->data
[0].length
!= 0 &&
2785 a
->data
[0].data
[a
->data
[0].length
- 1] !=
2786 b
->data
[0].data
[b
->data
[0].length
- 1]) {
2787 if (a
->data
[0].data
[a
->data
[0].length
- 1] == '$') {
2789 mod
->data
[0].length
--;
2790 } else if (b
->data
[0].data
[b
->data
[0].length
- 1] == '$') {
2792 mod
->data
[0].length
--;
2796 cmp
= krb5_principal_compare_flags(ctx
,
2799 KRB5_PRINCIPAL_COMPARE_CASEFOLD
);
2801 mod
->data
[0].length
++;
2807 krb5_error_code
smb_krb5_kinit_s4u2_ccache(krb5_context ctx
,
2808 krb5_ccache store_cc
,
2809 krb5_principal init_principal
,
2810 const char *init_password
,
2811 krb5_principal impersonate_principal
,
2812 const char *self_service
,
2813 const char *target_service
,
2814 krb5_get_init_creds_opt
*krb_options
,
2815 time_t *expire_time
,
2818 krb5_error_code code
;
2819 krb5_principal self_princ
= NULL
;
2820 krb5_principal target_princ
= NULL
;
2821 krb5_creds
*store_creds
= NULL
;
2822 krb5_creds
*s4u2self_creds
= NULL
;
2823 krb5_creds
*s4u2proxy_creds
= NULL
;
2824 krb5_creds init_creds
= {0};
2825 krb5_creds mcreds
= {0};
2826 krb5_flags options
= KRB5_GC_NO_STORE
;
2828 bool s4u2proxy
= false;
2831 code
= krb5_cc_new_unique(ctx
, "MEMORY", NULL
, &tmp_cc
);
2836 code
= krb5_get_init_creds_password(ctx
,
2849 code
= krb5_cc_initialize(ctx
, tmp_cc
, init_creds
.client
);
2854 code
= krb5_cc_store_cred(ctx
, tmp_cc
, &init_creds
);
2860 * Check if we also need S4U2Proxy or if S4U2Self is
2861 * enough in order to get a ticket for the target.
2863 if (target_service
== NULL
) {
2865 } else if (strcmp(target_service
, self_service
) == 0) {
2871 code
= krb5_parse_name(ctx
, self_service
, &self_princ
);
2877 * MIT lacks aliases support in S4U, for S4U2Self we require the tgt
2878 * client and the request server to be the same principal name.
2880 ok
= princ_compare_no_dollar(ctx
, init_creds
.client
, self_princ
);
2882 code
= KRB5KDC_ERR_PADATA_TYPE_NOSUPP
;
2886 mcreds
.client
= impersonate_principal
;
2887 mcreds
.server
= init_creds
.client
;
2889 code
= krb5_get_credentials_for_user(ctx
, options
, tmp_cc
, &mcreds
,
2890 NULL
, &s4u2self_creds
);
2896 code
= krb5_parse_name(ctx
, target_service
, &target_princ
);
2901 mcreds
.client
= init_creds
.client
;
2902 mcreds
.server
= target_princ
;
2903 mcreds
.second_ticket
= s4u2self_creds
->ticket
;
2905 code
= krb5_get_credentials(ctx
, options
|
2906 KRB5_GC_CONSTRAINED_DELEGATION
,
2907 tmp_cc
, &mcreds
, &s4u2proxy_creds
);
2912 /* Check KDC support of S4U2Proxy extension */
2913 if (!krb5_principal_compare(ctx
, s4u2self_creds
->client
,
2914 s4u2proxy_creds
->client
)) {
2915 code
= KRB5KDC_ERR_PADATA_TYPE_NOSUPP
;
2919 store_creds
= s4u2proxy_creds
;
2921 store_creds
= s4u2self_creds
;;
2923 /* We need to save the ticket with the requested server name
2924 * or the caller won't be able to find it in cache. */
2925 if (!krb5_principal_compare(ctx
, self_princ
,
2926 store_creds
->server
)) {
2927 krb5_free_principal(ctx
, store_creds
->server
);
2928 store_creds
->server
= NULL
;
2929 code
= krb5_copy_principal(ctx
, self_princ
,
2930 &store_creds
->server
);
2937 code
= krb5_cc_initialize(ctx
, store_cc
, store_creds
->client
);
2942 code
= krb5_cc_store_cred(ctx
, store_cc
, store_creds
);
2948 *expire_time
= (time_t) store_creds
->times
.endtime
;
2952 *kdc_time
= (time_t) store_creds
->times
.starttime
;
2956 krb5_cc_destroy(ctx
, tmp_cc
);
2957 krb5_free_cred_contents(ctx
, &init_creds
);
2958 krb5_free_creds(ctx
, s4u2self_creds
);
2959 krb5_free_creds(ctx
, s4u2proxy_creds
);
2960 krb5_free_principal(ctx
, self_princ
);
2961 krb5_free_principal(ctx
, target_princ
);
2967 #if !defined(HAVE_KRB5_MAKE_PRINCIPAL) && defined(HAVE_KRB5_BUILD_PRINCIPAL_ALLOC_VA)
2969 * @brief Create a principal name using a variable argument list.
2971 * @param[in] context The library context.
2973 * @param[inout] principal A pointer to the principal structure.
2975 * @param[in] _realm The realm to use. If NULL then the function will
2976 * get the default realm name.
2978 * @param[in] ... A list of 'char *' components, ending with NULL.
2980 * Use krb5_free_principal() to free the principal when it is no longer needed.
2982 * @return 0 on success, a Kerberos error code otherwise.
2984 krb5_error_code
smb_krb5_make_principal(krb5_context context
,
2985 krb5_principal
*principal
,
2986 const char *_realm
, ...)
2988 krb5_error_code code
;
2994 realm
= discard_const_p(char, _realm
);
2997 code
= krb5_get_default_realm(context
, &realm
);
3004 va_start(ap
, _realm
);
3005 code
= krb5_build_principal_alloc_va(context
, principal
,
3006 strlen(realm
), realm
,
3011 krb5_free_default_realm(context
, realm
);
3018 #if !defined(HAVE_KRB5_CC_GET_LIFETIME) && defined(HAVE_KRB5_CC_RETRIEVE_CRED)
3020 * @brief Get the lifetime of the initial ticket in the cache.
3022 * @param[in] context The kerberos context.
3024 * @param[in] id The credential cache to get the ticket lifetime.
3026 * @param[out] t A pointer to a time value to store the lifetime.
3028 * @return 0 on success, a krb5_error_code on error.
3030 krb5_error_code
smb_krb5_cc_get_lifetime(krb5_context context
,
3034 krb5_cc_cursor cursor
;
3035 krb5_error_code kerr
;
3041 kerr
= krb5_timeofday(context
, &now
);
3046 kerr
= krb5_cc_start_seq_get(context
, id
, &cursor
);
3051 while ((kerr
= krb5_cc_next_cred(context
, id
, &cursor
, &cred
)) == 0) {
3052 #ifndef HAVE_FLAGS_IN_KRB5_CREDS
3053 if (cred
.ticket_flags
& TKT_FLG_INITIAL
) {
3055 if (cred
.flags
.b
.initial
) {
3057 if (now
< cred
.times
.endtime
) {
3058 *t
= (time_t) (cred
.times
.endtime
- now
);
3060 krb5_free_cred_contents(context
, &cred
);
3063 krb5_free_cred_contents(context
, &cred
);
3066 krb5_cc_end_seq_get(context
, id
, &cursor
);
3070 #endif /* HAVE_KRB5_CC_GET_LIFETIME */
3072 #if !defined(HAVE_KRB5_FREE_CHECKSUM_CONTENTS) && defined(HAVE_FREE_CHECKSUM)
3073 void smb_krb5_free_checksum_contents(krb5_context ctx
, krb5_checksum
*cksum
)
3075 free_Checksum(cksum
);
3080 * @brief Compute a checksum operating on a keyblock.
3082 * This function computes a checksum over a PAC using the keyblock for a keyed
3085 * @param[in] mem_ctx A talloc context to allocate the signature on.
3087 * @param[in] pac_data The PAC as input.
3089 * @param[in] context The library context.
3091 * @param[in] keyblock Encryption key for a keyed checksum.
3093 * @param[out] sig_type The checksum type
3095 * @param[out] sig_blob The talloc'ed checksum
3097 * The caller must free the sig_blob with talloc_free() when it is not needed
3100 * @return 0 on success, a Kerberos error code otherwise.
3102 krb5_error_code
smb_krb5_make_pac_checksum(TALLOC_CTX
*mem_ctx
,
3103 DATA_BLOB
*pac_data
,
3104 krb5_context context
,
3105 const krb5_keyblock
*keyblock
,
3107 DATA_BLOB
*sig_blob
)
3109 krb5_error_code ret
;
3110 krb5_checksum cksum
;
3111 #if defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CREATE_CHECKSUM)
3115 ret
= krb5_crypto_init(context
,
3120 DEBUG(0,("krb5_crypto_init() failed: %s\n",
3121 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
3124 ret
= krb5_create_checksum(context
,
3126 KRB5_KU_OTHER_CKSUM
,
3132 DEBUG(2, ("PAC Verification failed: %s\n",
3133 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
3136 krb5_crypto_destroy(context
, crypto
);
3142 *sig_type
= cksum
.cksumtype
;
3143 *sig_blob
= data_blob_talloc(mem_ctx
,
3144 cksum
.checksum
.data
,
3145 cksum
.checksum
.length
);
3146 #elif defined(HAVE_KRB5_C_MAKE_CHECKSUM)
3149 input
.data
= (char *)pac_data
->data
;
3150 input
.length
= pac_data
->length
;
3152 ret
= krb5_c_make_checksum(context
,
3155 KRB5_KEYUSAGE_APP_DATA_CKSUM
,
3159 DEBUG(2, ("PAC Verification failed: %s\n",
3160 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
3164 *sig_type
= cksum
.checksum_type
;
3165 *sig_blob
= data_blob_talloc(mem_ctx
,
3170 #error krb5_create_checksum or krb5_c_make_checksum not available
3171 #endif /* HAVE_KRB5_C_MAKE_CHECKSUM */
3172 smb_krb5_free_checksum_contents(context
, &cksum
);
3179 * @brief Get realm of a principal
3181 * @param[in] mem_ctx The talloc ctx to put the result on
3183 * @param[in] context The library context
3185 * @param[in] principal The principal to get the realm from.
3187 * @return A talloced string with the realm or NULL if an error occurred.
3189 char *smb_krb5_principal_get_realm(TALLOC_CTX
*mem_ctx
,
3190 krb5_context context
,
3191 krb5_const_principal principal
)
3193 #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
3194 const char *realm
= NULL
;
3196 realm
= krb5_principal_get_realm(context
, principal
);
3197 if (realm
== NULL
) {
3201 return talloc_strdup(mem_ctx
, realm
);
3202 #elif defined(krb5_princ_realm) /* MIT */
3203 const krb5_data
*realm
= NULL
;
3205 realm
= krb5_princ_realm(context
, principal
);
3206 if (realm
== NULL
) {
3210 return talloc_strndup(mem_ctx
, realm
->data
, realm
->length
);
3212 #error UNKNOWN_GET_PRINC_REALM_FUNCTIONS
3217 * @brief Get realm of a principal
3219 * @param[in] context The library context
3221 * @param[in] principal The principal to set the realm
3223 * @param[in] realm The realm as a string to set.
3225 * @return 0 on success, a Kerberos error code otherwise.
3227 krb5_error_code
smb_krb5_principal_set_realm(krb5_context context
,
3228 krb5_principal principal
,
3231 #ifdef HAVE_KRB5_PRINCIPAL_SET_REALM /* Heimdal */
3232 return krb5_principal_set_realm(context
, principal
, realm
);
3233 #elif defined(krb5_princ_realm) && defined(krb5_princ_set_realm) /* MIT */
3234 krb5_error_code ret
;
3236 krb5_data
*old_data
;
3238 old_data
= krb5_princ_realm(context
, principal
);
3240 ret
= smb_krb5_copy_data_contents(&data
,
3247 /* free realm before setting */
3248 free(old_data
->data
);
3250 krb5_princ_set_realm(context
, principal
, &data
);
3254 #error UNKNOWN_PRINC_SET_REALM_FUNCTION
3260 * @brief Get the realm from the service hostname.
3262 * This function will look for a domain realm mapping in the [domain_realm]
3263 * section of the krb5.conf first and fallback to extract the realm from
3264 * the provided service hostname. As a last resort it will return the
3265 * provided client_realm.
3267 * @param[in] mem_ctx The talloc context
3269 * @param[in] hostname The service hostname
3271 * @param[in] client_realm If we can not find a mapping, fall back to
3274 * @return The realm to use for the service hostname, NULL if a fatal error
3277 char *smb_krb5_get_realm_from_hostname(TALLOC_CTX
*mem_ctx
,
3278 const char *hostname
,
3279 const char *client_realm
)
3281 #if defined(HAVE_KRB5_REALM_TYPE)
3283 krb5_realm
*realm_list
= NULL
;
3286 char **realm_list
= NULL
;
3289 krb5_error_code kerr
;
3290 krb5_context ctx
= NULL
;
3292 kerr
= smb_krb5_init_context_common(&ctx
);
3294 DBG_ERR("kerberos init context failed (%s)\n",
3295 error_message(kerr
));
3299 kerr
= krb5_get_host_realm(ctx
, hostname
, &realm_list
);
3300 if (kerr
== KRB5_ERR_HOST_REALM_UNKNOWN
) {
3305 DEBUG(3,("kerberos_get_realm_from_hostname %s: "
3307 hostname
? hostname
: "(NULL)",
3308 error_message(kerr
) ));
3312 if (realm_list
!= NULL
&&
3313 realm_list
[0] != NULL
&&
3314 realm_list
[0][0] != '\0') {
3315 realm
= talloc_strdup(mem_ctx
, realm_list
[0]);
3316 if (realm
== NULL
) {
3320 const char *p
= NULL
;
3323 * "dc6.samba2003.example.com"
3324 * returns a realm of "SAMBA2003.EXAMPLE.COM"
3326 * "dc6." returns realm as NULL
3328 p
= strchr_m(hostname
, '.');
3329 if (p
!= NULL
&& p
[1] != '\0') {
3330 realm
= talloc_strdup_upper(mem_ctx
, p
+ 1);
3331 if (realm
== NULL
) {
3337 if (realm
== NULL
) {
3338 realm
= talloc_strdup(mem_ctx
, client_realm
);
3345 krb5_free_host_realm(ctx
, realm_list
);
3348 krb5_free_context(ctx
);
3355 * @brief Get an error string from a Kerberos error code.
3357 * @param[in] context The library context.
3359 * @param[in] code The Kerberos error code.
3361 * @param[in] mem_ctx The talloc context to allocate the error string on.
3363 * @return A talloc'ed error string or NULL if an error occurred.
3365 * The caller must free the returned error string with talloc_free() if not
3368 char *smb_get_krb5_error_message(krb5_context context
,
3369 krb5_error_code code
,
3370 TALLOC_CTX
*mem_ctx
)
3374 #if defined(HAVE_KRB5_GET_ERROR_MESSAGE) && defined(HAVE_KRB5_FREE_ERROR_MESSAGE)
3375 const char *context_error
= krb5_get_error_message(context
, code
);
3376 if (context_error
) {
3377 ret
= talloc_asprintf(mem_ctx
, "%s: %s",
3378 error_message(code
), context_error
);
3379 krb5_free_error_message(context
, context_error
);
3383 ret
= talloc_strdup(mem_ctx
, error_message(code
));
3388 * @brief Return the type of a krb5_principal
3390 * @param[in] context The library context.
3392 * @param[in] principal The principal to get the type from.
3394 * @return The integer type of the principal.
3396 int smb_krb5_principal_get_type(krb5_context context
,
3397 krb5_const_principal principal
)
3399 #ifdef HAVE_KRB5_PRINCIPAL_GET_TYPE /* Heimdal */
3400 return krb5_principal_get_type(context
, principal
);
3401 #elif defined(krb5_princ_type) /* MIT */
3402 return krb5_princ_type(context
, principal
);
3404 #error UNKNOWN_PRINC_GET_TYPE_FUNCTION
3409 * @brief Set the type of a principal
3411 * @param[in] context The library context
3413 * @param[inout] principal The principal to set the type for.
3415 * @param[in] type The principal type to set.
3417 void smb_krb5_principal_set_type(krb5_context context
,
3418 krb5_principal principal
,
3421 #ifdef HAVE_KRB5_PRINCIPAL_SET_TYPE /* Heimdal */
3422 krb5_principal_set_type(context
, principal
, type
);
3423 #elif defined(krb5_princ_type) /* MIT */
3424 krb5_princ_type(context
, principal
) = type
;
3426 #error UNKNOWN_PRINC_SET_TYPE_FUNCTION
3431 * @brief Check if a principal is a TGS
3433 * @param[in] context The library context
3435 * @param[inout] principal The principal to check.
3437 * @returns 1 if equal, 0 if not and -1 on error.
3439 int smb_krb5_principal_is_tgs(krb5_context context
,
3440 krb5_const_principal principal
)
3445 p
= smb_krb5_principal_get_comp_string(NULL
, context
, principal
, 0);
3450 eq
= krb5_princ_size(context
, principal
) == 2 &&
3451 (strcmp(p
, KRB5_TGS_NAME
) == 0);
3458 #if !defined(HAVE_KRB5_WARNX)
3460 * @brief Log a Kerberos message
3462 * It sends the message to com_err.
3464 * @param[in] context The library context
3466 * @param[in] fmt The message format
3468 * @param[in] ... The message arguments
3470 * @return 0 on success.
3472 krb5_error_code
krb5_warnx(krb5_context context
, const char *fmt
, ...)
3476 va_start(args
, fmt
);
3477 com_err_va("samba-kdc", errno
, fmt
, args
);
3485 * @brief Copy a credential cache.
3487 * @param[in] context The library context.
3489 * @param[in] incc Credential cache to be copied.
3491 * @param[inout] outcc Copy of credential cache to be filled in.
3493 * @return 0 on success, a Kerberos error code otherwise.
3495 krb5_error_code
smb_krb5_cc_copy_creds(krb5_context context
,
3496 krb5_ccache incc
, krb5_ccache outcc
)
3498 #ifdef HAVE_KRB5_CC_COPY_CACHE /* Heimdal */
3499 return krb5_cc_copy_cache(context
, incc
, outcc
);
3500 #elif defined(HAVE_KRB5_CC_COPY_CREDS)
3501 krb5_error_code ret
;
3502 krb5_principal princ
= NULL
;
3504 ret
= krb5_cc_get_principal(context
, incc
, &princ
);
3508 ret
= krb5_cc_initialize(context
, outcc
, princ
);
3509 krb5_free_principal(context
, princ
);
3513 return krb5_cc_copy_creds(context
, incc
, outcc
);
3515 #error UNKNOWN_KRB5_CC_COPY_CACHE_OR_CREDS_FUNCTION
3519 /**********************************************************
3521 **********************************************************/
3523 static bool ads_cleanup_expired_creds(krb5_context context
,
3527 krb5_error_code retval
;
3528 const char *cc_type
= krb5_cc_get_type(context
, ccache
);
3530 DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
3531 cc_type
, krb5_cc_get_name(context
, ccache
),
3532 http_timestring(talloc_tos(), credsp
->times
.endtime
)));
3534 /* we will probably need new tickets if the current ones
3535 will expire within 10 seconds.
3537 if (credsp
->times
.endtime
>= (time(NULL
) + 10))
3540 /* heimdal won't remove creds from a file ccache, and
3541 perhaps we shouldn't anyway, since internally we
3542 use memory ccaches, and a FILE one probably means that
3543 we're using creds obtained outside of our executable
3545 if (strequal(cc_type
, "FILE")) {
3546 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type
));
3550 retval
= krb5_cc_remove_cred(context
, ccache
, 0, credsp
);
3552 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
3553 error_message(retval
)));
3554 /* If we have an error in this, we want to display it,
3555 but continue as though we deleted it */
3560 /* Allocate and setup the auth context into the state we need. */
3562 static krb5_error_code
ads_setup_auth_context(krb5_context context
,
3563 krb5_auth_context
*auth_context
)
3565 krb5_error_code retval
;
3567 retval
= krb5_auth_con_init(context
, auth_context
);
3569 DEBUG(1,("krb5_auth_con_init failed (%s)\n",
3570 error_message(retval
)));
3574 /* Ensure this is an addressless ticket. */
3575 retval
= krb5_auth_con_setaddrs(context
, *auth_context
, NULL
, NULL
);
3577 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n",
3578 error_message(retval
)));
3584 #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)
3585 static krb5_error_code
ads_create_gss_checksum(krb5_data
*in_data
, /* [inout] */
3588 unsigned int orig_length
= in_data
->length
;
3589 unsigned int base_cksum_size
= GSSAPI_CHECKSUM_SIZE
;
3590 char *gss_cksum
= NULL
;
3593 /* Extra length field for delegated ticket. */
3594 base_cksum_size
+= 4;
3597 if ((unsigned int)base_cksum_size
+ orig_length
<
3598 (unsigned int)base_cksum_size
) {
3602 gss_cksum
= (char *)SMB_MALLOC(base_cksum_size
+ orig_length
);
3603 if (gss_cksum
== NULL
) {
3607 memset(gss_cksum
, '\0', base_cksum_size
+ orig_length
);
3608 SIVAL(gss_cksum
, 0, GSSAPI_BNDLENGTH
);
3611 * GSS_C_NO_CHANNEL_BINDINGS means 16 zero bytes.
3612 * This matches the behavior of heimdal and mit.
3614 * And it is needed to work against some closed source
3619 memset(&gss_cksum
[4], 0x00, GSSAPI_BNDLENGTH
);
3621 SIVAL(gss_cksum
, 20, gss_flags
);
3623 if (orig_length
&& in_data
->data
!= NULL
) {
3624 SSVAL(gss_cksum
, 24, 1); /* The Delegation Option identifier */
3625 SSVAL(gss_cksum
, 26, orig_length
);
3626 /* Copy the kerberos KRB_CRED data */
3627 memcpy(gss_cksum
+ 28, in_data
->data
, orig_length
);
3628 free(in_data
->data
);
3629 in_data
->data
= NULL
;
3630 in_data
->length
= 0;
3632 in_data
->data
= gss_cksum
;
3633 in_data
->length
= base_cksum_size
+ orig_length
;
3639 * We can't use krb5_mk_req because w2k wants the service to be in a particular
3642 static krb5_error_code
ads_krb5_mk_req(krb5_context context
,
3643 krb5_auth_context
*auth_context
,
3644 const krb5_flags ap_req_options
,
3645 const char *principal
,
3648 time_t *expire_time
,
3649 const char *impersonate_princ_s
)
3651 krb5_error_code retval
;
3652 krb5_principal server
;
3653 krb5_principal impersonate_princ
= NULL
;
3657 bool creds_ready
= false;
3658 int i
= 0, maxtries
= 3;
3661 ZERO_STRUCT(in_data
);
3663 retval
= smb_krb5_parse_name(context
, principal
, &server
);
3665 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal
));
3669 if (impersonate_princ_s
) {
3670 retval
= smb_krb5_parse_name(context
, impersonate_princ_s
,
3671 &impersonate_princ
);
3673 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", impersonate_princ_s
));
3678 /* obtain ticket & session key */
3680 if ((retval
= krb5_copy_principal(context
, server
, &creds
.server
))) {
3681 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n",
3682 error_message(retval
)));
3686 retval
= krb5_cc_get_principal(context
, ccache
, &creds
.client
);
3688 /* This can commonly fail on smbd startup with no ticket in the cache.
3689 * Report at higher level than 1. */
3690 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
3691 error_message(retval
)));
3695 while (!creds_ready
&& (i
< maxtries
)) {
3697 retval
= smb_krb5_get_credentials(context
,
3704 DBG_WARNING("smb_krb5_get_credentials failed for %s "
3707 error_message(retval
));
3711 /* cope with ticket being in the future due to clock skew */
3712 if ((unsigned)credsp
->times
.starttime
> time(NULL
)) {
3713 time_t t
= time(NULL
);
3714 int time_offset
=(int)((unsigned)credsp
->times
.starttime
-t
);
3715 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset
));
3716 krb5_set_real_time(context
, t
+ time_offset
+ 1, 0);
3719 ok
= ads_cleanup_expired_creds(context
, ccache
, credsp
);
3727 DBG_DEBUG("Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
3729 krb5_cc_get_type(context
, ccache
),
3730 krb5_cc_get_name(context
, ccache
),
3731 http_timestring(talloc_tos(),
3732 (unsigned)credsp
->times
.endtime
),
3733 (unsigned)credsp
->times
.endtime
);
3736 *expire_time
= (time_t)credsp
->times
.endtime
;
3739 /* Allocate the auth_context. */
3740 retval
= ads_setup_auth_context(context
, auth_context
);
3742 DBG_WARNING("ads_setup_auth_context failed (%s)\n",
3743 error_message(retval
));
3747 #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)
3749 uint32_t gss_flags
= 0;
3751 if (credsp
->ticket_flags
& TKT_FLG_OK_AS_DELEGATE
) {
3753 * Fetch a forwarded TGT from the KDC so that we can
3754 * hand off a 2nd ticket as part of the kerberos
3758 DBG_INFO("Server marked as OK to delegate to, building "
3759 "forwardable TGT\n");
3761 retval
= krb5_auth_con_setuseruserkey(context
,
3763 &credsp
->keyblock
);
3765 DBG_WARNING("krb5_auth_con_setuseruserkey "
3767 error_message(retval
));
3771 /* Must use a subkey for forwarded tickets. */
3772 retval
= krb5_auth_con_setflags(context
,
3774 KRB5_AUTH_CONTEXT_USE_SUBKEY
);
3776 DBG_WARNING("krb5_auth_con_setflags failed (%s)\n",
3777 error_message(retval
));
3781 retval
= krb5_fwd_tgt_creds(context
,/* Krb5 context [in] */
3782 *auth_context
, /* Authentication context [in] */
3783 discard_const_p(char, KRB5_TGS_NAME
), /* Ticket service name ("krbtgt") [in] */
3784 credsp
->client
, /* Client principal for the tgt [in] */
3785 credsp
->server
, /* Server principal for the tgt [in] */
3786 ccache
, /* Credential cache to use for storage [in] */
3787 1, /* Turn on for "Forwardable ticket" [in] */
3788 &in_data
); /* Resulting response [out] */
3791 DBG_INFO("krb5_fwd_tgt_creds failed (%s)\n",
3792 error_message(retval
));
3795 * This is not fatal. Delete the *auth_context and continue
3796 * with krb5_mk_req_extended to get a non-forwardable ticket.
3800 free( in_data
.data
);
3801 in_data
.data
= NULL
;
3804 krb5_auth_con_free(context
, *auth_context
);
3805 *auth_context
= NULL
;
3806 retval
= ads_setup_auth_context(context
, auth_context
);
3808 DBG_WARNING("ads_setup_auth_context failed (%s)\n",
3809 error_message(retval
));
3813 /* We got a delegated ticket. */
3814 gss_flags
|= GSS_C_DELEG_FLAG
;
3818 /* Frees and reallocates in_data into a GSS checksum blob. */
3819 retval
= ads_create_gss_checksum(&in_data
, gss_flags
);
3824 /* We always want GSS-checksum types. */
3825 retval
= krb5_auth_con_set_req_cksumtype(context
, *auth_context
, GSSAPI_CHECKSUM
);
3827 DEBUG(1,("krb5_auth_con_set_req_cksumtype failed (%s)\n",
3828 error_message(retval
)));
3834 retval
= krb5_mk_req_extended(context
, auth_context
, ap_req_options
,
3835 &in_data
, credsp
, outbuf
);
3837 DBG_WARNING("krb5_mk_req_extended failed (%s)\n",
3838 error_message(retval
));
3841 #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)
3846 free( in_data
.data
);
3850 krb5_free_creds(context
, credsp
);
3853 krb5_free_cred_contents(context
, &creds
);
3856 krb5_free_principal(context
, server
);
3857 if (impersonate_princ
) {
3858 krb5_free_principal(context
, impersonate_princ
);
3865 get a kerberos5 ticket for the given service
3867 int ads_krb5_cli_get_ticket(TALLOC_CTX
*mem_ctx
,
3868 const char *principal
,
3871 DATA_BLOB
*session_key_krb5
,
3872 uint32_t extra_ap_opts
, const char *ccname
,
3874 const char *impersonate_princ_s
)
3876 krb5_error_code retval
;
3878 krb5_context context
= NULL
;
3879 krb5_ccache ccdef
= NULL
;
3880 krb5_auth_context auth_context
= NULL
;
3881 krb5_enctype enc_types
[] = {
3882 ENCTYPE_AES256_CTS_HMAC_SHA1_96
,
3883 ENCTYPE_AES128_CTS_HMAC_SHA1_96
,
3884 ENCTYPE_ARCFOUR_HMAC
,
3888 DBG_DEBUG("Getting ticket for service [%s] using creds from [%s] "
3889 "and impersonating [%s]\n",
3890 principal
, ccname
, impersonate_princ_s
);
3892 retval
= smb_krb5_init_context_common(&context
);
3894 DBG_ERR("kerberos init context failed (%s)\n",
3895 error_message(retval
));
3899 if (time_offset
!= 0) {
3900 krb5_set_real_time(context
, time(NULL
) + time_offset
, 0);
3903 retval
= krb5_cc_resolve(context
,
3904 ccname
? ccname
: krb5_cc_default_name(context
),
3907 DBG_WARNING("krb5_cc_default failed (%s)\n",
3908 error_message(retval
));
3912 retval
= krb5_set_default_tgs_ktypes(context
, enc_types
);
3914 DBG_WARNING("krb5_set_default_tgs_ktypes failed (%s)\n",
3915 error_message(retval
));
3919 retval
= ads_krb5_mk_req(context
,
3921 AP_OPTS_USE_SUBKEY
| (krb5_flags
)extra_ap_opts
,
3926 impersonate_princ_s
);
3931 ok
= smb_krb5_get_smb_session_key(mem_ctx
,
3941 *ticket
= data_blob_talloc(mem_ctx
, packet
.data
, packet
.length
);
3943 smb_krb5_free_data_contents(context
, &packet
);
3949 krb5_cc_close(context
, ccdef
);
3952 krb5_auth_con_free(context
, auth_context
);
3954 krb5_free_context(context
);
3960 #ifndef SAMBA4_USES_HEIMDAL /* MITKRB5 tracing callback */
3961 static void smb_krb5_trace_cb(krb5_context ctx
,
3962 #ifdef HAVE_KRB5_TRACE_INFO
3963 const krb5_trace_info
*info
,
3964 #elif defined(HAVE_KRB5_TRACE_INFO_STRUCT)
3965 const struct krb5_trace_info
*info
,
3967 #error unknown krb5_trace_info
3972 DBGC_DEBUG(DBGC_KERBEROS
, "%s", info
->message
);
3977 krb5_error_code
smb_krb5_init_context_common(krb5_context
*_krb5_context
)
3979 krb5_error_code ret
;
3980 krb5_context krb5_ctx
;
3982 initialize_krb5_error_table();
3984 ret
= krb5_init_context(&krb5_ctx
);
3986 DBG_ERR("Krb5 context initialization failed (%s)\n",
3987 error_message(ret
));
3991 /* The MIT Kerberos build relies on using the system krb5.conf file.
3992 * If you really want to use another file please set KRB5_CONFIG
3994 #ifndef SAMBA4_USES_HEIMDAL
3995 ret
= krb5_set_trace_callback(krb5_ctx
, smb_krb5_trace_cb
, NULL
);
3997 DBG_ERR("Failed to set MIT kerberos trace callback! (%s)\n",
3998 error_message(ret
));
4002 #ifdef SAMBA4_USES_HEIMDAL
4003 /* Set options in kerberos */
4004 krb5_set_dns_canonicalize_hostname(krb5_ctx
, false);
4007 *_krb5_context
= krb5_ctx
;
4011 #else /* HAVE_KRB5 */
4012 /* This saves a few linking headaches */
4013 int ads_krb5_cli_get_ticket(TALLOC_CTX
*mem_ctx
,
4014 const char *principal
,
4017 DATA_BLOB
*session_key_krb5
,
4018 uint32_t extra_ap_opts
, const char *ccname
,
4020 const char *impersonate_princ_s
)
4022 DEBUG(0,("NO KERBEROS SUPPORT\n"));
4026 #endif /* HAVE_KRB5 */