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_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
118 const krb5_data *krb5_princ_component(krb5_context context,
119 krb5_principal principal, int i);
121 const krb5_data *krb5_princ_component(krb5_context context,
122 krb5_principal principal, int i)
124 static krb5_data kdata;
126 kdata.data = discard_const_p(char, krb5_principal_get_comp_string(context, principal, i));
127 kdata.length = strlen((const char *)kdata.data);
133 /**********************************************************
135 **********************************************************/
137 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
141 * @brief Stores the address of a 'struct sockaddr_storage' a krb5_address
143 * @param[in] paddr A pointer to a 'struct sockaddr_storage to extract the
146 * @param[out] pkaddr A Kerberos address to store tha address in.
148 * @return True on success, false if an error occurred.
150 bool smb_krb5_sockaddr_to_kaddr(struct sockaddr_storage *paddr,
151 krb5_address *pkaddr)
153 memset(pkaddr, '\0', sizeof(krb5_address));
155 if (paddr->ss_family == AF_INET6) {
156 pkaddr->addr_type = KRB5_ADDRESS_INET6;
157 pkaddr->address.length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr);
158 pkaddr->address.data = (char *)&(((struct sockaddr_in6 *)paddr)->sin6_addr);
162 if (paddr->ss_family == AF_INET) {
163 pkaddr->addr_type = KRB5_ADDRESS_INET;
164 pkaddr->address.length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
165 pkaddr->address.data = (char *)&(((struct sockaddr_in *)paddr)->sin_addr);
170 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
174 * @brief Stores the address of a 'struct sockaddr_storage' a krb5_address
176 * @param[in] paddr A pointer to a 'struct sockaddr_storage to extract the
179 * @param[in] pkaddr A Kerberos address to store tha address in.
181 * @return True on success, false if an error occurred.
183 bool smb_krb5_sockaddr_to_kaddr(struct sockaddr_storage *paddr,
184 krb5_address *pkaddr)
186 memset(pkaddr, '\0', sizeof(krb5_address));
188 if (paddr->ss_family == AF_INET6) {
189 pkaddr->addrtype = ADDRTYPE_INET6;
190 pkaddr->length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr);
191 pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in6 *)paddr)->sin6_addr);
195 if (paddr->ss_family == AF_INET) {
196 pkaddr->addrtype = ADDRTYPE_INET;
197 pkaddr->length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
198 pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in *)paddr)->sin_addr);
204 #error UNKNOWN_ADDRTYPE
207 krb5_error_code smb_krb5_mk_error(krb5_context context,
208 krb5_error_code error_code,
211 const krb5_principal client,
212 const krb5_principal server,
215 krb5_error_code code = EINVAL;
216 #ifdef SAMBA4_USES_HEIMDAL
217 code = krb5_mk_error(context,
223 NULL, /* client_time */
224 NULL, /* client_usec */
227 krb5_principal unspec_server = NULL;
233 code = krb5_us_timeofday(context,
240 errpkt.error = error_code;
242 errpkt.text.length = 0;
243 if (e_text != NULL) {
244 errpkt.text.length = strlen(e_text);
245 errpkt.text.data = discard_const_p(char, e_text);
248 errpkt.e_data.magic = KV5M_DATA;
249 errpkt.e_data.length = 0;
250 errpkt.e_data.data = NULL;
251 if (e_data != NULL) {
252 errpkt.e_data = *e_data;
255 errpkt.client = client;
257 if (server != NULL) {
258 errpkt.server = server;
260 code = smb_krb5_make_principal(context,
262 "<unspecified realm>",
267 errpkt.server = unspec_server;
270 code = krb5_mk_error(context,
273 krb5_free_principal(context, unspec_server);
279 * @brief Create a keyblock based on input parameters
281 * @param context The krb5_context
282 * @param host_princ The krb5_principal to use
283 * @param salt The optional salt, if omitted, salt is calculated with
284 * the provided principal.
285 * @param password The krb5_data containing the password
286 * @param enctype The krb5_enctype to use for the keyblock generation
287 * @param key The returned krb5_keyblock, caller needs to free with
288 * krb5_free_keyblock().
290 * @return krb5_error_code
292 int smb_krb5_create_key_from_string(krb5_context context,
293 krb5_const_principal host_princ,
296 krb5_enctype enctype,
301 if (host_princ == NULL && salt == NULL) {
305 if ((int)enctype == (int)ENCTYPE_ARCFOUR_HMAC) {
306 TALLOC_CTX *frame = talloc_stackframe();
307 uint8_t *utf16 = NULL;
308 size_t utf16_size = 0;
312 ok = convert_string_talloc(frame, CH_UNIX, CH_UTF16LE,
313 password->data, password->length,
314 (void **)&utf16, &utf16_size);
324 mdfour(nt_hash, utf16, utf16_size);
325 memset(utf16, 0, utf16_size);
326 ret = smb_krb5_keyblock_init_contents(context,
327 ENCTYPE_ARCFOUR_HMAC,
331 ZERO_STRUCT(nt_hash);
341 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_C_STRING_TO_KEY)
346 ret = krb5_principal2salt(context, host_princ, &_salt);
348 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
354 ret = krb5_c_string_to_key(context, enctype, password, &_salt, key);
356 SAFE_FREE(_salt.data);
359 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
364 ret = krb5_get_pw_salt(context, host_princ, &_salt);
366 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret)));
370 _salt.saltvalue = *salt;
371 _salt.salttype = KRB5_PW_SALT;
374 ret = krb5_string_to_key_salt(context, enctype, (const char *)password->data, _salt, key);
376 krb5_free_salt(context, _salt);
380 #error UNKNOWN_CREATE_KEY_FUNCTIONS
386 * @brief Create a salt for a given principal
388 * @param context The initialized krb5_context
389 * @param host_princ The krb5_principal to create the salt for
390 * @param psalt A pointer to a krb5_data struct
392 * caller has to free the contents of psalt with smb_krb5_free_data_contents
393 * when function has succeeded
395 * @return krb5_error_code, returns 0 on success, error code otherwise
398 int smb_krb5_get_pw_salt(krb5_context context,
399 krb5_const_principal host_princ,
401 #if defined(HAVE_KRB5_GET_PW_SALT)
407 ret = krb5_get_pw_salt(context, host_princ, &salt);
412 psalt->data = salt.saltvalue.data;
413 psalt->length = salt.saltvalue.length;
417 #elif defined(HAVE_KRB5_PRINCIPAL2SALT)
420 return krb5_principal2salt(context, host_princ, psalt);
423 #error UNKNOWN_SALT_FUNCTIONS
427 * @brief This constructs the salt principal used by active directory
429 * Most Kerberos encryption types require a salt in order to
430 * calculate the long term private key for user/computer object
431 * based on a password.
433 * The returned _salt_principal is a string in forms like this:
434 * - host/somehost.example.com@EXAMPLE.COM
435 * - SomeAccount@EXAMPLE.COM
436 * - SomePrincipal@EXAMPLE.COM
438 * This is not the form that's used as salt, it's just
439 * the human readable form. It needs to be converted by
440 * smb_krb5_salt_principal2data().
442 * @param[in] realm The realm the user/computer is added too.
444 * @param[in] sAMAccountName The sAMAccountName attribute of the object.
446 * @param[in] userPrincipalName The userPrincipalName attribute of the object
447 * or NULL is not available.
449 * @param[in] uac_flags UF_ACCOUNT_TYPE_MASKed userAccountControl field
451 * @param[in] mem_ctx The TALLOC_CTX to allocate _salt_principal.
453 * @param[out] _salt_principal The resulting principal as string.
455 * @retval 0 Success; otherwise - Kerberos error codes
457 * @see smb_krb5_salt_principal2data
459 int smb_krb5_salt_principal(krb5_context krb5_ctx,
461 const char *sAMAccountName,
462 const char *userPrincipalName,
464 krb5_principal *salt_princ)
466 TALLOC_CTX *frame = talloc_stackframe();
467 char *upper_realm = NULL;
468 const char *principal = NULL;
469 int principal_len = 0;
470 krb5_error_code krb5_ret;
474 if (sAMAccountName == NULL) {
484 if (uac_flags & ~UF_ACCOUNT_TYPE_MASK) {
486 * catch callers which still
492 if (uac_flags == 0) {
494 * catch callers which still
501 upper_realm = strupper_talloc(frame, realm);
502 if (upper_realm == NULL) {
507 /* Many, many thanks to lukeh@padl.com for this
508 * algorithm, described in his Nov 10 2004 mail to
509 * samba-technical@lists.samba.org */
512 * Determine a salting principal
514 if (uac_flags & UF_TRUST_ACCOUNT_MASK) {
515 int computer_len = 0;
517 computer_len = strlen(sAMAccountName);
518 if (sAMAccountName[computer_len-1] == '$') {
522 if (uac_flags & UF_INTERDOMAIN_TRUST_ACCOUNT) {
523 const char *krbtgt = "krbtgt";
524 krb5_ret = krb5_build_principal_ext(krb5_ctx,
538 const char *host = "host";
540 char *tmp_lower = NULL;
542 tmp = talloc_asprintf(frame, "%*.*s.%s",
552 tmp_lower = strlower_talloc(frame, tmp);
553 if (tmp_lower == NULL) {
558 krb5_ret = krb5_build_principal_ext(krb5_ctx,
573 } else if (userPrincipalName != NULL) {
575 * We parse the name not only to allow an easy
576 * replacement of the realm (no matter the realm in
577 * the UPN, the salt comes from the upper-case real
578 * realm, but also to correctly provide a salt when
579 * the UPN is host/foo.bar
581 * This can fail for a UPN of the form foo@bar@REALM
582 * (which is accepted by windows) however.
584 krb5_ret = krb5_parse_name(krb5_ctx,
594 * No matter what realm (including none) in the UPN,
595 * the realm is replaced with our upper-case realm
597 krb5_ret = smb_krb5_principal_set_realm(krb5_ctx,
601 krb5_free_principal(krb5_ctx, *salt_princ);
606 principal = sAMAccountName;
607 principal_len = strlen(principal);
609 krb5_ret = krb5_build_principal_ext(krb5_ctx,
627 * @brief This constructs the salt principal used by active directory
629 * Most Kerberos encryption types require a salt in order to
630 * calculate the long term private key for user/computer object
631 * based on a password.
633 * The returned _salt_principal is a string in forms like this:
634 * - host/somehost.example.com@EXAMPLE.COM
635 * - SomeAccount@EXAMPLE.COM
636 * - SomePrincipal@EXAMPLE.COM
638 * This is not the form that's used as salt, it's just
639 * the human readable form. It needs to be converted by
640 * smb_krb5_salt_principal2data().
642 * @param[in] realm The realm the user/computer is added too.
644 * @param[in] sAMAccountName The sAMAccountName attribute of the object.
646 * @param[in] userPrincipalName The userPrincipalName attribute of the object
647 * or NULL is not available.
649 * @param[in] uac_flags UF_ACCOUNT_TYPE_MASKed userAccountControl field
651 * @param[in] mem_ctx The TALLOC_CTX to allocate _salt_principal.
653 * @param[out] _salt_principal The resulting principal as string.
655 * @retval 0 Success; otherwise - Kerberos error codes
657 * @see smb_krb5_salt_principal2data
659 int smb_krb5_salt_principal_str(const char *realm,
660 const char *sAMAccountName,
661 const char *userPrincipalName,
664 char **_salt_principal_str)
666 krb5_principal salt_principal = NULL;
667 char *salt_principal_malloc;
668 krb5_context krb5_ctx;
669 krb5_error_code krb5_ret
670 = smb_krb5_init_context_common(&krb5_ctx);
672 DBG_ERR("kerberos init context failed (%s)\n",
673 error_message(krb5_ret));
677 krb5_ret = smb_krb5_salt_principal(krb5_ctx,
684 DBG_ERR("unable to create salt principal:%s\n",
685 error_message(krb5_ret));
689 krb5_ret = krb5_unparse_name(krb5_ctx, salt_principal,
690 &salt_principal_malloc);
692 krb5_free_principal(krb5_ctx, salt_principal);
693 DBG_ERR("kerberos unparse of salt principal failed (%s)\n",
694 error_message(krb5_ret));
697 krb5_free_principal(krb5_ctx, salt_principal);
699 = talloc_strdup(mem_ctx, salt_principal_malloc);
700 krb5_free_unparsed_name(krb5_ctx, salt_principal_malloc);
702 if (*_salt_principal_str == NULL) {
709 * @brief Converts the salt principal string into the salt data blob
711 * This function takes a salt_principal as string in forms like this:
712 * - host/somehost.example.com@EXAMPLE.COM
713 * - SomeAccount@EXAMPLE.COM
714 * - SomePrincipal@EXAMPLE.COM
716 * It generates values like:
717 * - EXAMPLE.COMhost/somehost.example.com
718 * - EXAMPLE.COMSomeAccount
719 * - EXAMPLE.COMSomePrincipal
721 * @param[in] realm The realm the user/computer is added too.
723 * @param[in] sAMAccountName The sAMAccountName attribute of the object.
725 * @param[in] userPrincipalName The userPrincipalName attribute of the object
726 * or NULL is not available.
728 * @param[in] is_computer The indication of the object includes
729 * objectClass=computer.
731 * @param[in] mem_ctx The TALLOC_CTX to allocate _salt_principal.
733 * @param[out] _salt_principal The resulting principal as string.
735 * @retval 0 Success; otherwise - Kerberos error codes
737 * @see smb_krb5_salt_principal
739 int smb_krb5_salt_principal2data(krb5_context context,
740 const char *salt_principal,
745 krb5_principal salt_princ = NULL;
750 ret = krb5_parse_name(context, salt_principal, &salt_princ);
755 ret = smb_krb5_get_pw_salt(context, salt_princ, &salt);
756 krb5_free_principal(context, salt_princ);
761 *_salt_data = talloc_strndup(mem_ctx,
764 smb_krb5_free_data_contents(context, &salt);
765 if (*_salt_data == NULL) {
772 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
774 * @brief Get a list of encryption types allowed for session keys
776 * @param[in] context The library context
778 * @param[in] enctypes An allocated, zero-terminated list of encryption types
780 * This function returns an allocated list of encryption types allowed for
783 * Use free() to free the enctypes when it is no longer needed.
785 * @retval 0 Success; otherwise - Kerberos error codes
787 krb5_error_code smb_krb5_get_allowed_etypes(krb5_context context,
788 krb5_enctype **enctypes)
790 return krb5_get_permitted_enctypes(context, enctypes);
792 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
793 krb5_error_code smb_krb5_get_allowed_etypes(krb5_context context,
794 krb5_enctype **enctypes)
796 #ifdef HAVE_KRB5_PDU_NONE_DECL
797 return krb5_get_default_in_tkt_etypes(context, KRB5_PDU_NONE, enctypes);
799 return krb5_get_default_in_tkt_etypes(context, enctypes);
803 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
808 * @brief Convert a string principal name to a Kerberos principal.
810 * @param[in] context The library context
812 * @param[in] name The principal as a unix charset string.
814 * @param[out] principal The newly allocated principal.
816 * Use krb5_free_principal() to free a principal when it is no longer needed.
818 * @return 0 on success, a Kerberos error code otherwise.
820 krb5_error_code smb_krb5_parse_name(krb5_context context,
822 krb5_principal *principal)
826 size_t converted_size;
827 TALLOC_CTX *frame = talloc_stackframe();
829 if (!push_utf8_talloc(frame, &utf8_name, name, &converted_size)) {
834 ret = krb5_parse_name(context, utf8_name, principal);
835 if (ret == KRB5_PARSE_MALFORMED) {
836 ret = krb5_parse_name_flags(context, utf8_name,
837 KRB5_PRINCIPAL_PARSE_ENTERPRISE,
845 * @brief Convert a Kerberos principal structure to a string representation.
847 * The resulting string representation will be a unix charset name and is
850 * @param[in] mem_ctx The talloc context to allocate memory on.
852 * @param[in] context The library context.
854 * @param[in] principal The principal.
856 * @param[out] unix_name A string representation of the princpial name as with
859 * Use talloc_free() to free the string representation if it is no longer
862 * @return 0 on success, a Kerberos error code otherwise.
864 krb5_error_code smb_krb5_unparse_name(TALLOC_CTX *mem_ctx,
865 krb5_context context,
866 krb5_const_principal principal,
871 size_t converted_size;
874 ret = krb5_unparse_name(context, principal, &utf8_name);
879 if (!pull_utf8_talloc(mem_ctx, unix_name, utf8_name, &converted_size)) {
880 krb5_free_unparsed_name(context, utf8_name);
883 krb5_free_unparsed_name(context, utf8_name);
888 * @brief Free the contents of a krb5_data structure and zero the data field.
890 * @param[in] context The krb5 context
892 * @param[in] pdata The data structure to free contents of
894 * This function frees the contents, not the structure itself.
896 void smb_krb5_free_data_contents(krb5_context context, krb5_data *pdata)
898 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
900 krb5_free_data_contents(context, pdata);
902 #elif defined(HAVE_KRB5_DATA_FREE)
903 krb5_data_free(context, pdata);
905 SAFE_FREE(pdata->data);
910 * @brief copy a buffer into a krb5_data struct
912 * @param[in] p The krb5_data
913 * @param[in] data The data to copy
914 * @param[in] length The length of the data to copy
915 * @return krb5_error_code
917 * Caller has to free krb5_data with smb_krb5_free_data_contents().
919 krb5_error_code smb_krb5_copy_data_contents(krb5_data *p,
923 #if defined(HAVE_KRB5_DATA_COPY)
924 return krb5_data_copy(p, data, len);
927 p->data = malloc(len);
928 if (p->data == NULL) {
931 memmove(p->data, data, len);
936 p->magic = KV5M_DATA;
941 bool smb_krb5_get_smb_session_key(TALLOC_CTX *mem_ctx,
942 krb5_context context,
943 krb5_auth_context auth_context,
944 DATA_BLOB *session_key,
947 krb5_keyblock *skey = NULL;
948 krb5_error_code err = 0;
952 #ifdef HAVE_KRB5_AUTH_CON_GETRECVSUBKEY
953 err = krb5_auth_con_getrecvsubkey(context,
956 #else /* HAVE_KRB5_AUTH_CON_GETRECVSUBKEY */
957 err = krb5_auth_con_getremotesubkey(context,
958 auth_context, &skey);
959 #endif /* HAVE_KRB5_AUTH_CON_GETRECVSUBKEY */
961 #ifdef HAVE_KRB5_AUTH_CON_GETSENDSUBKEY
962 err = krb5_auth_con_getsendsubkey(context,
965 #else /* HAVE_KRB5_AUTH_CON_GETSENDSUBKEY */
966 err = krb5_auth_con_getlocalsubkey(context,
967 auth_context, &skey);
968 #endif /* HAVE_KRB5_AUTH_CON_GETSENDSUBKEY */
971 if (err || skey == NULL) {
972 DEBUG(10, ("KRB5 error getting session key %d\n", err));
976 DEBUG(10, ("Got KRB5 session key of length %d\n",
977 (int)KRB5_KEY_LENGTH(skey)));
979 *session_key = data_blob_talloc(mem_ctx,
981 KRB5_KEY_LENGTH(skey));
982 dump_data_pw("KRB5 Session Key:\n",
984 session_key->length);
990 krb5_free_keyblock(context, skey);
998 * @brief Get talloced string component of a principal
1000 * @param[in] mem_ctx The TALLOC_CTX
1001 * @param[in] context The krb5_context
1002 * @param[in] principal The principal
1003 * @param[in] component The component
1004 * @return string component
1006 * Caller must talloc_free if the return value is not NULL.
1009 char *smb_krb5_principal_get_comp_string(TALLOC_CTX *mem_ctx,
1010 krb5_context context,
1011 krb5_const_principal principal,
1012 unsigned int component)
1014 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
1015 return talloc_strdup(mem_ctx, krb5_principal_get_comp_string(context, principal, component));
1019 if (component >= krb5_princ_size(context, principal)) {
1023 data = krb5_princ_component(context, principal, component);
1028 return talloc_strndup(mem_ctx, data->data, data->length);
1035 * @param[in] ccache_string A string pointing to the cache to renew the ticket
1036 * (e.g. FILE:/tmp/krb5cc_0) or NULL. If the principal
1037 * ccache has not been specified, the default ccache
1040 * @param[in] client_string The client principal string (e.g. user@SAMBA.SITE)
1041 * or NULL. If the principal string has not been
1042 * specified, the principal from the ccache will be
1045 * @param[in] service_string The service ticket string
1046 * (e.g. krbtgt/SAMBA.SITE@SAMBA.SITE) or NULL. If
1047 * the sevice ticket is specified, it is parsed (
1048 * with the realm part ignored) and used as the
1049 * server principal of the credential. Otherwise
1050 * the ticket-granting service is used.
1052 * @param[in] expire_time A pointer to store the credentials end time or
1055 * @return 0 on Succes, a Kerberos error code otherwise.
1057 krb5_error_code smb_krb5_renew_ticket(const char *ccache_string,
1058 const char *client_string,
1059 const char *service_string,
1060 time_t *expire_time)
1062 krb5_error_code ret;
1063 krb5_context context = NULL;
1064 krb5_ccache ccache = NULL;
1065 krb5_principal client = NULL;
1066 krb5_creds creds, creds_in;
1069 ZERO_STRUCT(creds_in);
1071 ret = smb_krb5_init_context_common(&context);
1073 DBG_ERR("kerberos init context failed (%s)\n",
1074 error_message(ret));
1078 if (!ccache_string) {
1079 ccache_string = krb5_cc_default_name(context);
1082 if (!ccache_string) {
1087 DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string));
1089 /* FIXME: we should not fall back to defaults */
1090 ret = krb5_cc_resolve(context, discard_const_p(char, ccache_string), &ccache);
1095 if (client_string) {
1096 ret = smb_krb5_parse_name(context, client_string, &client);
1101 ret = krb5_cc_get_principal(context, ccache, &client);
1107 ret = krb5_get_renewed_creds(context, &creds, client, ccache, discard_const_p(char, service_string));
1109 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
1113 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
1114 ret = krb5_cc_initialize(context, ccache, client);
1119 ret = krb5_cc_store_cred(context, ccache, &creds);
1122 *expire_time = (time_t) creds.times.endtime;
1126 krb5_free_cred_contents(context, &creds_in);
1127 krb5_free_cred_contents(context, &creds);
1130 krb5_free_principal(context, client);
1133 krb5_cc_close(context, ccache);
1136 krb5_free_context(context);
1143 * @brief Free the data stored in an smb_krb5_addresses structure.
1145 * @param[in] context The library context
1147 * @param[in] addr The address structure to free.
1149 * @return 0 on success, a Kerberos error code otherwise.
1151 krb5_error_code smb_krb5_free_addresses(krb5_context context,
1152 smb_krb5_addresses *addr)
1154 krb5_error_code ret = 0;
1158 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1159 krb5_free_addresses(context, addr->addrs);
1160 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1161 ret = krb5_free_addresses(context, addr->addrs);
1162 SAFE_FREE(addr->addrs);
1169 #define MAX_NETBIOSNAME_LEN 16
1172 * @brief Add a netbios name to the array of addresses
1174 * @param[in] kerb_addr A pointer to the smb_krb5_addresses to add the
1177 * @param[in] netbios_name The netbios name to add.
1179 * @return 0 on success, a Kerberos error code otherwise.
1181 krb5_error_code smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses **kerb_addr,
1182 const char *netbios_name)
1184 krb5_error_code ret = 0;
1185 char buf[MAX_NETBIOSNAME_LEN];
1187 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1188 krb5_address **addrs = NULL;
1189 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1190 krb5_addresses *addrs = NULL;
1193 *kerb_addr = (smb_krb5_addresses *)SMB_MALLOC(sizeof(smb_krb5_addresses));
1194 if (*kerb_addr == NULL) {
1198 /* temporarily duplicate put_name() code here to avoid dependency
1199 * issues for a 5 lines function */
1200 len = strlen(netbios_name);
1201 memcpy(buf, netbios_name,
1202 (len < MAX_NETBIOSNAME_LEN) ? len : MAX_NETBIOSNAME_LEN - 1);
1203 if (len < MAX_NETBIOSNAME_LEN - 1) {
1204 memset(buf + len, ' ', MAX_NETBIOSNAME_LEN - 1 - len);
1206 buf[MAX_NETBIOSNAME_LEN - 1] = 0x20;
1208 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
1212 addrs = (krb5_address **)SMB_MALLOC(sizeof(krb5_address *) * num_addr);
1213 if (addrs == NULL) {
1214 SAFE_FREE(*kerb_addr);
1218 memset(addrs, 0, sizeof(krb5_address *) * num_addr);
1220 addrs[0] = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
1221 if (addrs[0] == NULL) {
1223 SAFE_FREE(*kerb_addr);
1227 addrs[0]->magic = KV5M_ADDRESS;
1228 addrs[0]->addrtype = KRB5_ADDR_NETBIOS;
1229 addrs[0]->length = MAX_NETBIOSNAME_LEN;
1230 addrs[0]->contents = (unsigned char *)SMB_MALLOC(addrs[0]->length);
1231 if (addrs[0]->contents == NULL) {
1232 SAFE_FREE(addrs[0]);
1234 SAFE_FREE(*kerb_addr);
1238 memcpy(addrs[0]->contents, buf, addrs[0]->length);
1242 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
1244 addrs = (krb5_addresses *)SMB_MALLOC(sizeof(krb5_addresses));
1245 if (addrs == NULL) {
1246 SAFE_FREE(*kerb_addr);
1250 memset(addrs, 0, sizeof(krb5_addresses));
1253 addrs->val = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
1254 if (addrs->val == NULL) {
1256 SAFE_FREE(*kerb_addr);
1260 addrs->val[0].addr_type = KRB5_ADDR_NETBIOS;
1261 addrs->val[0].address.length = MAX_NETBIOSNAME_LEN;
1262 addrs->val[0].address.data = (unsigned char *)SMB_MALLOC(addrs->val[0].address.length);
1263 if (addrs->val[0].address.data == NULL) {
1264 SAFE_FREE(addrs->val);
1266 SAFE_FREE(*kerb_addr);
1270 memcpy(addrs->val[0].address.data, buf, addrs->val[0].address.length);
1273 #error UNKNOWN_KRB5_ADDRESS_FORMAT
1275 (*kerb_addr)->addrs = addrs;
1281 * @brief Get the enctype from a key table entry
1283 * @param[in] kt_entry Key table entry to get the enctype from.
1285 * @return The enctype from the entry.
1287 krb5_enctype smb_krb5_kt_get_enctype_from_entry(krb5_keytab_entry *kt_entry)
1289 return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry));
1293 * @brief Free the contents of a key table entry.
1295 * @param[in] context The library context.
1297 * @param[in] kt_entry The key table entry to free the contents of.
1299 * @return 0 on success, a Kerberos error code otherwise.
1301 * The pointer itself is not freed.
1303 krb5_error_code smb_krb5_kt_free_entry(krb5_context context,
1304 krb5_keytab_entry *kt_entry)
1306 /* Try krb5_free_keytab_entry_contents first, since
1307 * MIT Kerberos >= 1.7 has both krb5_free_keytab_entry_contents and
1308 * krb5_kt_free_entry but only has a prototype for the first, while the
1309 * second is considered private.
1311 #if defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
1312 return krb5_free_keytab_entry_contents(context, kt_entry);
1313 #elif defined(HAVE_KRB5_KT_FREE_ENTRY)
1314 return krb5_kt_free_entry(context, kt_entry);
1316 #error UNKNOWN_KT_FREE_FUNCTION
1322 * @brief Convert an encryption type to a string.
1324 * @param[in] context The library context.
1326 * @param[in] enctype The encryption type.
1328 * @param[in] etype_s A pointer to store the allocated encryption type as a
1331 * @return 0 on success, a Kerberos error code otherwise.
1333 * The caller needs to free the allocated string etype_s.
1335 krb5_error_code smb_krb5_enctype_to_string(krb5_context context,
1336 krb5_enctype enctype,
1339 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
1340 return krb5_enctype_to_string(context, enctype, etype_s); /* Heimdal */
1341 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
1343 krb5_error_code ret = krb5_enctype_to_string(enctype, buf, 256); /* MIT */
1347 *etype_s = SMB_STRDUP(buf);
1353 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
1357 /* This MAX_NAME_LEN is a constant defined in krb5.h */
1358 #ifndef MAX_KEYTAB_NAME_LEN
1359 #define MAX_KEYTAB_NAME_LEN 1100
1363 * @brief Open a key table readonly or with readwrite access.
1365 * Allows one to use a different keytab than the default one using a relative
1366 * path to the keytab.
1368 * @param[in] context The library context
1370 * @param[in] keytab_name_req The path to the key table.
1372 * @param[in] write_access Open with readwrite access.
1374 * @param[in] keytab A pointer o the opended key table.
1376 * The keytab pointer should be freed using krb5_kt_close().
1378 * @return 0 on success, a Kerberos error code otherwise.
1380 krb5_error_code smb_krb5_kt_open_relative(krb5_context context,
1381 const char *keytab_name_req,
1383 krb5_keytab *keytab)
1385 krb5_error_code ret = 0;
1386 TALLOC_CTX *mem_ctx;
1387 char keytab_string[MAX_KEYTAB_NAME_LEN];
1388 char *kt_str = NULL;
1389 bool found_valid_name = false;
1390 const char *pragma = "FILE";
1391 const char *tmp = NULL;
1393 if (!write_access && !keytab_name_req) {
1394 /* caller just wants to read the default keytab readonly, so be it */
1395 return krb5_kt_default(context, keytab);
1398 mem_ctx = talloc_init("smb_krb5_open_keytab");
1403 #ifdef HAVE_WRFILE_KEYTAB
1409 if (keytab_name_req) {
1411 if (strlen(keytab_name_req) > MAX_KEYTAB_NAME_LEN) {
1412 ret = KRB5_CONFIG_NOTENUFSPACE;
1416 if ((strncmp(keytab_name_req, "WRFILE:", 7) == 0) ||
1417 (strncmp(keytab_name_req, "FILE:", 5) == 0)) {
1418 tmp = keytab_name_req;
1422 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, keytab_name_req);
1431 /* we need to handle more complex keytab_strings, like:
1432 * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
1434 ret = krb5_kt_default_name(context, &keytab_string[0], MAX_KEYTAB_NAME_LEN - 2);
1439 DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string));
1441 tmp = talloc_strdup(mem_ctx, keytab_string);
1447 if (strncmp(tmp, "ANY:", 4) == 0) {
1451 memset(&keytab_string, '\0', sizeof(keytab_string));
1453 while (next_token_talloc(mem_ctx, &tmp, &kt_str, ",")) {
1454 if (strncmp(kt_str, "WRFILE:", 7) == 0) {
1455 found_valid_name = true;
1460 if (strncmp(kt_str, "FILE:", 5) == 0) {
1461 found_valid_name = true;
1466 if (tmp[0] == '/') {
1467 /* Treat as a FILE: keytab definition. */
1468 found_valid_name = true;
1471 if (found_valid_name) {
1472 if (tmp[0] != '/') {
1473 ret = KRB5_KT_BADNAME;
1477 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, tmp);
1486 if (!found_valid_name) {
1487 ret = KRB5_KT_UNKNOWN_TYPE;
1492 DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp));
1493 ret = krb5_kt_resolve(context, tmp, keytab);
1496 TALLOC_FREE(mem_ctx);
1501 * @brief Open a key table readonly or with readwrite access.
1503 * Allows one to use a different keytab than the default one. The path needs to be
1504 * an absolute path or an error will be returned.
1506 * @param[in] context The library context
1508 * @param[in] keytab_name_req The path to the key table.
1510 * @param[in] write_access Open with readwrite access.
1512 * @param[in] keytab A pointer o the opended key table.
1514 * The keytab pointer should be freed using krb5_kt_close().
1516 * @return 0 on success, a Kerberos error code otherwise.
1518 krb5_error_code smb_krb5_kt_open(krb5_context context,
1519 const char *keytab_name_req,
1521 krb5_keytab *keytab)
1525 if (keytab_name_req == NULL) {
1526 return KRB5_KT_BADNAME;
1529 if (keytab_name_req[0] == '/') {
1533 cmp = strncmp(keytab_name_req, "FILE:/", 6);
1538 cmp = strncmp(keytab_name_req, "WRFILE:/", 8);
1543 DBG_WARNING("ERROR: Invalid keytab name: %s\n", keytab_name_req);
1545 return KRB5_KT_BADNAME;
1548 return smb_krb5_kt_open_relative(context,
1555 * @brief Get a key table name.
1557 * @param[in] mem_ctx The talloc context to use for allocation.
1559 * @param[in] context The library context.
1561 * @param[in] keytab The key table to get the name from.
1563 * @param[in] keytab_name A talloc'ed string of the key table name.
1565 * The talloc'ed name string needs to be freed with talloc_free().
1567 * @return 0 on success, a Kerberos error code otherwise.
1569 krb5_error_code smb_krb5_kt_get_name(TALLOC_CTX *mem_ctx,
1570 krb5_context context,
1572 const char **keytab_name)
1574 char keytab_string[MAX_KEYTAB_NAME_LEN];
1575 krb5_error_code ret = 0;
1577 ret = krb5_kt_get_name(context, keytab,
1578 keytab_string, MAX_KEYTAB_NAME_LEN - 2);
1583 *keytab_name = talloc_strdup(mem_ctx, keytab_string);
1584 if (!*keytab_name) {
1592 * @brief Seek and delete old entries in a keytab based on the passed
1595 * @param[in] context The KRB5 context to use.
1597 * @param[in] keytab The keytab to operate on.
1599 * @param[in] kvno The kvnco to use.
1601 * @param[in] princ_s The principal as a string to search for.
1603 * @param[in] princ The principal as a krb5_principal to search for.
1605 * @param[in] flush Whether to flush the complete keytab.
1607 * @param[in] keep_old_entries Keep the entry with the previous kvno.
1609 * @retval 0 on Sucess
1611 * @return An appropriate KRB5 error code.
1613 krb5_error_code smb_krb5_kt_seek_and_delete_old_entries(krb5_context context,
1616 krb5_enctype enctype,
1617 const char *princ_s,
1618 krb5_principal princ,
1620 bool keep_old_entries)
1622 krb5_error_code ret;
1623 krb5_kt_cursor cursor;
1624 krb5_keytab_entry kt_entry;
1625 char *ktprinc = NULL;
1626 krb5_kvno old_kvno = kvno - 1;
1627 TALLOC_CTX *tmp_ctx;
1629 ZERO_STRUCT(cursor);
1630 ZERO_STRUCT(kt_entry);
1632 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
1633 if (ret == KRB5_KT_END || ret == ENOENT ) {
1638 tmp_ctx = talloc_new(NULL);
1639 if (tmp_ctx == NULL) {
1643 DEBUG(3, (__location__ ": Will try to delete old keytab entries\n"));
1644 while (!krb5_kt_next_entry(context, keytab, &kt_entry, &cursor)) {
1645 bool name_ok = false;
1646 krb5_enctype kt_entry_enctype =
1647 smb_krb5_kt_get_enctype_from_entry(&kt_entry);
1649 if (!flush && (princ_s != NULL)) {
1650 ret = smb_krb5_unparse_name(tmp_ctx, context,
1654 DEBUG(1, (__location__
1655 ": smb_krb5_unparse_name failed "
1656 "(%s)\n", error_message(ret)));
1660 #ifdef HAVE_KRB5_KT_COMPARE
1661 name_ok = krb5_kt_compare(context, &kt_entry,
1664 name_ok = (strcmp(ktprinc, princ_s) == 0);
1668 DEBUG(10, (__location__ ": ignoring keytab "
1669 "entry principal %s, kvno = %d\n",
1670 ktprinc, kt_entry.vno));
1673 * just free this entry and continue. */
1674 ret = smb_krb5_kt_free_entry(context,
1676 ZERO_STRUCT(kt_entry);
1678 DEBUG(1, (__location__
1679 ": smb_krb5_kt_free_entry "
1681 error_message(ret)));
1685 TALLOC_FREE(ktprinc);
1689 TALLOC_FREE(ktprinc);
1692 /*------------------------------------------------------------
1693 * Save the entries with kvno - 1. This is what microsoft does
1694 * to allow people with existing sessions that have kvno - 1
1695 * to still work. Otherwise, when the password for the machine
1696 * changes, all kerberizied sessions will 'break' until either
1697 * the client reboots or the client's session key expires and
1698 * they get a new session ticket with the new kvno.
1699 * Some keytab files only store the kvno in 8bits, limit
1700 * the compare accordingly.
1703 if (!flush && ((kt_entry.vno & 0xff) == (old_kvno & 0xff))) {
1704 DEBUG(5, (__location__ ": Saving previous (kvno %d) "
1705 "entry for principal: %s.\n",
1706 old_kvno, princ_s));
1710 if (keep_old_entries) {
1711 DEBUG(5, (__location__ ": Saving old (kvno %d) "
1712 "entry for principal: %s.\n",
1718 ((kt_entry.vno & 0xff) == (kvno & 0xff)) &&
1719 (kt_entry_enctype != enctype))
1721 DEBUG(5, (__location__ ": Saving entry with kvno [%d] "
1722 "enctype [%d] for principal: %s.\n",
1723 kvno, kt_entry_enctype, princ_s));
1727 DEBUG(5, (__location__ ": Found old entry for principal: %s "
1728 "(kvno %d) - trying to remove it.\n",
1729 princ_s, kt_entry.vno));
1731 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
1732 ZERO_STRUCT(cursor);
1734 DEBUG(1, (__location__ ": krb5_kt_end_seq_get() "
1735 "failed (%s)\n", error_message(ret)));
1738 ret = krb5_kt_remove_entry(context, keytab, &kt_entry);
1740 DEBUG(1, (__location__ ": krb5_kt_remove_entry() "
1741 "failed (%s)\n", error_message(ret)));
1745 DEBUG(5, (__location__ ": removed old entry for principal: "
1746 "%s (kvno %d).\n", princ_s, kt_entry.vno));
1748 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
1750 DEBUG(1, (__location__ ": krb5_kt_start_seq() failed "
1751 "(%s)\n", error_message(ret)));
1754 ret = smb_krb5_kt_free_entry(context, &kt_entry);
1755 ZERO_STRUCT(kt_entry);
1757 DEBUG(1, (__location__ ": krb5_kt_remove_entry() "
1758 "failed (%s)\n", error_message(ret)));
1764 talloc_free(tmp_ctx);
1765 if (!all_zero((uint8_t *)&kt_entry, sizeof(kt_entry))) {
1766 smb_krb5_kt_free_entry(context, &kt_entry);
1768 if (!all_zero((uint8_t *)&cursor, sizeof(cursor))) {
1769 krb5_kt_end_seq_get(context, keytab, &cursor);
1775 * @brief Add a keytab entry for the given principal
1777 * @param[in] context The krb5 context to use.
1779 * @param[in] keytab The keytab to add the entry to.
1781 * @param[in] kvno The kvno to use.
1783 * @param[in] princ_s The principal as a string.
1785 * @param[in] salt_principal The salt principal to salt the password with.
1786 * Only needed for keys which support salting.
1787 * If no salt is used set no_salt to false and
1790 * @param[in] enctype The encryption type of the keytab entry.
1792 * @param[in] password The password of the keytab entry.
1794 * @param[in] no_salt If the password should not be salted. Normally
1795 * this is only set to false for encryption types
1796 * which do not support salting like RC4.
1798 * @param[in] keep_old_entries Whether to keep or delete old keytab entries.
1800 * @retval 0 on Success
1802 * @return A corresponding KRB5 error code.
1804 * @see smb_krb5_kt_open()
1806 krb5_error_code smb_krb5_kt_add_entry(krb5_context context,
1809 const char *princ_s,
1810 const char *salt_principal,
1811 krb5_enctype enctype,
1812 krb5_data *password,
1814 bool keep_old_entries)
1816 krb5_error_code ret;
1817 krb5_keytab_entry kt_entry;
1818 krb5_principal princ = NULL;
1819 krb5_keyblock *keyp;
1821 ZERO_STRUCT(kt_entry);
1823 ret = smb_krb5_parse_name(context, princ_s, &princ);
1825 DEBUG(1, (__location__ ": smb_krb5_parse_name(%s) "
1826 "failed (%s)\n", princ_s, error_message(ret)));
1830 /* Seek and delete old keytab entries */
1831 ret = smb_krb5_kt_seek_and_delete_old_entries(context,
1843 /* If we get here, we have deleted all the old entries with kvno's
1844 * not equal to the current kvno-1. */
1846 keyp = KRB5_KT_KEY(&kt_entry);
1849 KRB5_KEY_DATA(keyp) = (KRB5_KEY_DATA_CAST *)SMB_MALLOC(password->length);
1850 if (KRB5_KEY_DATA(keyp) == NULL) {
1854 memcpy(KRB5_KEY_DATA(keyp), password->data, password->length);
1855 KRB5_KEY_LENGTH(keyp) = password->length;
1856 KRB5_KEY_TYPE(keyp) = enctype;
1858 krb5_principal salt_princ = NULL;
1860 /* Now add keytab entries for all encryption types */
1861 ret = smb_krb5_parse_name(context, salt_principal, &salt_princ);
1863 DBG_WARNING("krb5_parse_name(%s) failed (%s)\n",
1864 salt_principal, error_message(ret));
1868 ret = smb_krb5_create_key_from_string(context,
1874 krb5_free_principal(context, salt_princ);
1880 kt_entry.principal = princ;
1881 kt_entry.vno = kvno;
1883 DEBUG(3, (__location__ ": adding keytab entry for (%s) with "
1884 "encryption type (%d) and version (%d)\n",
1885 princ_s, enctype, kt_entry.vno));
1886 ret = krb5_kt_add_entry(context, keytab, &kt_entry);
1887 krb5_free_keyblock_contents(context, keyp);
1888 ZERO_STRUCT(kt_entry);
1890 DEBUG(1, (__location__ ": adding entry to keytab "
1891 "failed (%s)\n", error_message(ret)));
1897 krb5_free_principal(context, princ);
1903 #if defined(HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE) && \
1904 defined(HAVE_KRB5_GET_CREDS_OPT_ALLOC) && \
1905 defined(HAVE_KRB5_GET_CREDS)
1906 static krb5_error_code smb_krb5_get_credentials_for_user_opt(krb5_context context,
1909 krb5_principal server,
1910 krb5_principal impersonate_princ,
1911 krb5_creds **out_creds)
1913 krb5_error_code ret;
1914 krb5_get_creds_opt opt;
1916 ret = krb5_get_creds_opt_alloc(context, &opt);
1920 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_FORWARDABLE);
1922 if (impersonate_princ) {
1923 ret = krb5_get_creds_opt_set_impersonate(context, opt,
1930 ret = krb5_get_creds(context, opt, ccache, server, out_creds);
1937 krb5_get_creds_opt_free(context, opt);
1941 #endif /* HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE */
1943 #ifdef HAVE_KRB5_GET_CREDENTIALS_FOR_USER
1945 #if !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER
1946 krb5_error_code KRB5_CALLCONV
1947 krb5_get_credentials_for_user(krb5_context context, krb5_flags options,
1948 krb5_ccache ccache, krb5_creds *in_creds,
1949 krb5_data *subject_cert,
1950 krb5_creds **out_creds);
1951 #endif /* !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER */
1953 static krb5_error_code smb_krb5_get_credentials_for_user(krb5_context context,
1956 krb5_principal server,
1957 krb5_principal impersonate_princ,
1958 krb5_creds **out_creds)
1960 krb5_error_code ret;
1961 krb5_creds in_creds;
1963 ZERO_STRUCT(in_creds);
1965 if (impersonate_princ) {
1967 in_creds.server = me;
1968 in_creds.client = impersonate_princ;
1970 ret = krb5_get_credentials_for_user(context,
1971 0, /* krb5_flags options */
1974 NULL, /* krb5_data *subject_cert */
1977 in_creds.client = me;
1978 in_creds.server = server;
1980 ret = krb5_get_credentials(context, 0, ccache,
1981 &in_creds, out_creds);
1986 #endif /* HAVE_KRB5_GET_CREDENTIALS_FOR_USER */
1989 * smb_krb5_get_credentials
1991 * @brief Get krb5 credentials for a server
1993 * @param[in] context An initialized krb5_context
1994 * @param[in] ccache An initialized krb5_ccache
1995 * @param[in] me The krb5_principal of the caller
1996 * @param[in] server The krb5_principal of the requested service
1997 * @param[in] impersonate_princ The krb5_principal of a user to impersonate as (optional)
1998 * @param[out] out_creds The returned krb5_creds structure
1999 * @return krb5_error_code
2002 krb5_error_code smb_krb5_get_credentials(krb5_context context,
2005 krb5_principal server,
2006 krb5_principal impersonate_princ,
2007 krb5_creds **out_creds)
2009 krb5_error_code ret;
2010 krb5_creds *creds = NULL;
2012 if (out_creds != NULL) {
2016 if (impersonate_princ) {
2017 #ifdef HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE /* Heimdal */
2018 ret = smb_krb5_get_credentials_for_user_opt(context, ccache, me, server, impersonate_princ, &creds);
2019 #elif defined(HAVE_KRB5_GET_CREDENTIALS_FOR_USER) /* MIT */
2020 ret = smb_krb5_get_credentials_for_user(context, ccache, me, server, impersonate_princ, &creds);
2025 krb5_creds in_creds;
2027 ZERO_STRUCT(in_creds);
2029 in_creds.client = me;
2030 in_creds.server = server;
2032 ret = krb5_get_credentials(context, 0, ccache,
2045 krb5_free_creds(context, creds);
2052 * @brief Initialize a krb5_keyblock with the given data.
2054 * Initialized a new keyblock, allocates the contents fo the key and
2055 * copies the data into the keyblock.
2057 * @param[in] context The library context
2059 * @param[in] enctype The encryption type.
2061 * @param[in] data The date to initialize the keyblock with.
2063 * @param[in] length The length of the keyblock.
2065 * @param[in] key Newly allocated keyblock structure.
2067 * The key date must be freed using krb5_free_keyblock_contents() when it is
2070 * @return 0 on success, a Kerberos error code otherwise.
2072 krb5_error_code smb_krb5_keyblock_init_contents(krb5_context context,
2073 krb5_enctype enctype,
2078 #if defined(HAVE_KRB5_KEYBLOCK_INIT)
2079 return krb5_keyblock_init(context, enctype, data, length, key);
2081 memset(key, 0, sizeof(krb5_keyblock));
2082 KRB5_KEY_DATA(key) = SMB_MALLOC(length);
2083 if (NULL == KRB5_KEY_DATA(key)) {
2086 memcpy(KRB5_KEY_DATA(key), data, length);
2087 KRB5_KEY_LENGTH(key) = length;
2088 KRB5_KEY_TYPE(key) = enctype;
2094 * @brief Simulate a kinit by putting the tgt in the given credential cache.
2096 * This function uses a keyblock rather than needing the original password.
2098 * @param[in] ctx The library context
2100 * @param[in] cc The credential cache to put the tgt in.
2102 * @param[in] principal The client princial
2104 * @param[in] keyblock The keyblock to use.
2106 * @param[in] target_service The service name of the initial credentials (or NULL).
2108 * @param[in] krb_options Initial credential options.
2110 * @param[in] expire_time A pointer to store the experation time of the
2111 * credentials (or NULL).
2113 * @param[in] kdc_time A pointer to store the time when the ticket becomes
2116 * @return 0 on success, a Kerberos error code otherwise.
2118 krb5_error_code smb_krb5_kinit_keyblock_ccache(krb5_context ctx,
2120 krb5_principal principal,
2121 krb5_keyblock *keyblock,
2122 const char *target_service,
2123 krb5_get_init_creds_opt *krb_options,
2124 time_t *expire_time,
2127 krb5_error_code code = 0;
2128 krb5_creds my_creds;
2130 #if defined(HAVE_KRB5_GET_INIT_CREDS_KEYBLOCK)
2131 code = krb5_get_init_creds_keyblock(ctx, &my_creds, principal,
2132 keyblock, 0, target_service,
2134 #elif defined(HAVE_KRB5_GET_INIT_CREDS_KEYTAB)
2136 #define SMB_CREDS_KEYTAB "MEMORY:tmp_kinit_keyblock_ccache"
2137 char tmp_name[64] = {0};
2138 krb5_keytab_entry entry;
2142 memset(&entry, 0, sizeof(entry));
2143 entry.principal = principal;
2144 *(KRB5_KT_KEY(&entry)) = *keyblock;
2146 rc = snprintf(tmp_name, sizeof(tmp_name),
2151 return KRB5_KT_BADNAME;
2153 code = krb5_kt_resolve(ctx, tmp_name, &keytab);
2158 code = krb5_kt_add_entry(ctx, keytab, &entry);
2160 (void)krb5_kt_close(ctx, keytab);
2164 code = krb5_get_init_creds_keytab(ctx, &my_creds, principal,
2165 keytab, 0, target_service,
2167 (void)krb5_kt_close(ctx, keytab);
2170 #error krb5_get_init_creds_keyblock not available!
2176 #ifndef SAMBA4_USES_HEIMDAL /* MIT */
2178 * We need to store the principal as returned from the KDC to the
2179 * credentials cache. If we don't do that the KRB5 library is not
2180 * able to find the tickets it is looking for
2182 principal = my_creds.client;
2184 code = krb5_cc_initialize(ctx, cc, principal);
2189 code = krb5_cc_store_cred(ctx, cc, &my_creds);
2195 *expire_time = (time_t) my_creds.times.endtime;
2199 *kdc_time = (time_t) my_creds.times.starttime;
2204 krb5_free_cred_contents(ctx, &my_creds);
2209 * @brief Simulate a kinit by putting the tgt in the given credential cache.
2211 * @param[in] ctx The library context
2213 * @param[in] cc The credential cache to put the tgt in.
2215 * @param[in] principal The client princial
2217 * @param[in] password The password (or NULL).
2219 * @param[in] target_service The service name of the initial credentials (or NULL).
2221 * @param[in] krb_options Initial credential options.
2223 * @param[in] expire_time A pointer to store the experation time of the
2224 * credentials (or NULL).
2226 * @param[in] kdc_time A pointer to store the time when the ticket becomes
2229 * @return 0 on success, a Kerberos error code otherwise.
2231 krb5_error_code smb_krb5_kinit_password_ccache(krb5_context ctx,
2233 krb5_principal principal,
2234 const char *password,
2235 const char *target_service,
2236 krb5_get_init_creds_opt *krb_options,
2237 time_t *expire_time,
2240 krb5_error_code code = 0;
2241 krb5_creds my_creds;
2243 code = krb5_get_init_creds_password(ctx, &my_creds, principal,
2244 password, NULL, NULL, 0,
2245 target_service, krb_options);
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;
2256 code = krb5_cc_initialize(ctx, cc, principal);
2261 code = krb5_cc_store_cred(ctx, cc, &my_creds);
2267 *expire_time = (time_t) my_creds.times.endtime;
2271 *kdc_time = (time_t) my_creds.times.starttime;
2276 krb5_free_cred_contents(ctx, &my_creds);
2280 #ifdef SAMBA4_USES_HEIMDAL
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 store the tgt in.
2288 * @param[in] principal The initial client princial.
2290 * @param[in] password The password (or NULL).
2292 * @param[in] impersonate_principal The impersonatiion principal (or NULL).
2294 * @param[in] self_service The local service for S4U2Self if
2295 * impersonate_principal is specified).
2297 * @param[in] target_service The service name of the initial credentials
2298 * (kpasswd/REALM or a remote service). It defaults
2299 * to the krbtgt if NULL.
2301 * @param[in] krb_options Initial credential options.
2303 * @param[in] expire_time A pointer to store the experation time of the
2304 * credentials (or NULL).
2306 * @param[in] kdc_time A pointer to store the time when the ticket becomes
2309 * @return 0 on success, a Kerberos error code otherwise.
2311 krb5_error_code smb_krb5_kinit_s4u2_ccache(krb5_context ctx,
2312 krb5_ccache store_cc,
2313 krb5_principal init_principal,
2314 const char *init_password,
2315 krb5_principal impersonate_principal,
2316 const char *self_service,
2317 const char *target_service,
2318 krb5_get_init_creds_opt *krb_options,
2319 time_t *expire_time,
2322 krb5_error_code code = 0;
2323 krb5_get_creds_opt options;
2324 krb5_principal store_principal;
2325 krb5_creds store_creds;
2326 krb5_creds *s4u2self_creds;
2327 Ticket s4u2self_ticket;
2328 size_t s4u2self_ticketlen;
2329 krb5_creds *s4u2proxy_creds;
2330 krb5_principal self_princ;
2332 krb5_principal target_princ;
2334 const char *self_realm;
2335 const char *client_realm = NULL;
2336 krb5_principal blacklist_principal = NULL;
2337 krb5_principal whitelist_principal = NULL;
2339 code = krb5_get_init_creds_password(ctx, &store_creds,
2350 store_principal = init_principal;
2353 * We are trying S4U2Self now:
2355 * As we do not want to expose our TGT in the
2356 * krb5_ccache, which is also holds the impersonated creds.
2358 * Some low level krb5/gssapi function might use the TGT
2359 * identity and let the client act as our machine account.
2361 * We need to avoid that and use a temporary krb5_ccache
2362 * in order to pass our TGT to the krb5_get_creds() function.
2364 code = krb5_cc_new_unique(ctx, NULL, NULL, &tmp_cc);
2366 krb5_free_cred_contents(ctx, &store_creds);
2370 code = krb5_cc_initialize(ctx, tmp_cc, store_creds.client);
2372 krb5_cc_destroy(ctx, tmp_cc);
2373 krb5_free_cred_contents(ctx, &store_creds);
2377 code = krb5_cc_store_cred(ctx, tmp_cc, &store_creds);
2379 krb5_free_cred_contents(ctx, &store_creds);
2380 krb5_cc_destroy(ctx, tmp_cc);
2385 * we need to remember the client principal of our
2386 * TGT and make sure the KDC does not return this
2387 * in the impersonated tickets. This can happen
2388 * if the KDC does not support S4U2Self and S4U2Proxy.
2390 blacklist_principal = store_creds.client;
2391 store_creds.client = NULL;
2392 krb5_free_cred_contents(ctx, &store_creds);
2395 * Check if we also need S4U2Proxy or if S4U2Self is
2396 * enough in order to get a ticket for the target.
2398 if (target_service == NULL) {
2400 } else if (strcmp(target_service, self_service) == 0) {
2407 * For S4U2Self we need our own service principal,
2408 * which belongs to our own realm (available on
2409 * our client principal).
2411 self_realm = krb5_principal_get_realm(ctx, init_principal);
2413 code = krb5_parse_name(ctx, self_service, &self_princ);
2415 krb5_free_principal(ctx, blacklist_principal);
2416 krb5_cc_destroy(ctx, tmp_cc);
2420 code = krb5_principal_set_realm(ctx, self_princ, self_realm);
2422 krb5_free_principal(ctx, blacklist_principal);
2423 krb5_free_principal(ctx, self_princ);
2424 krb5_cc_destroy(ctx, tmp_cc);
2428 code = krb5_get_creds_opt_alloc(ctx, &options);
2430 krb5_free_principal(ctx, blacklist_principal);
2431 krb5_free_principal(ctx, self_princ);
2432 krb5_cc_destroy(ctx, tmp_cc);
2438 * If we want S4U2Proxy, we need the forwardable flag
2439 * on the S4U2Self ticket.
2441 krb5_get_creds_opt_set_options(ctx, options, KRB5_GC_FORWARDABLE);
2444 code = krb5_get_creds_opt_set_impersonate(ctx, options,
2445 impersonate_principal);
2447 krb5_get_creds_opt_free(ctx, options);
2448 krb5_free_principal(ctx, blacklist_principal);
2449 krb5_free_principal(ctx, self_princ);
2450 krb5_cc_destroy(ctx, tmp_cc);
2454 code = krb5_get_creds(ctx, options, tmp_cc,
2455 self_princ, &s4u2self_creds);
2456 krb5_get_creds_opt_free(ctx, options);
2457 krb5_free_principal(ctx, self_princ);
2459 krb5_free_principal(ctx, blacklist_principal);
2460 krb5_cc_destroy(ctx, tmp_cc);
2465 krb5_cc_destroy(ctx, tmp_cc);
2468 * Now make sure we store the impersonated principal
2469 * and creds instead of the TGT related stuff
2470 * in the krb5_ccache of the caller.
2472 code = krb5_copy_creds_contents(ctx, s4u2self_creds,
2474 krb5_free_creds(ctx, s4u2self_creds);
2480 * It's important to store the principal the KDC
2481 * returned, as otherwise the caller would not find
2482 * the S4U2Self ticket in the krb5_ccache lookup.
2484 store_principal = store_creds.client;
2489 * We are trying S4U2Proxy:
2491 * We need the ticket from the S4U2Self step
2492 * and our TGT in order to get the delegated ticket.
2494 code = decode_Ticket((const uint8_t *)s4u2self_creds->ticket.data,
2495 s4u2self_creds->ticket.length,
2497 &s4u2self_ticketlen);
2499 krb5_free_creds(ctx, s4u2self_creds);
2500 krb5_free_principal(ctx, blacklist_principal);
2501 krb5_cc_destroy(ctx, tmp_cc);
2506 * we need to remember the client principal of the
2507 * S4U2Self stage and as it needs to match the one we
2508 * will get for the S4U2Proxy stage. We need this
2509 * in order to detect KDCs which does not support S4U2Proxy.
2511 whitelist_principal = s4u2self_creds->client;
2512 s4u2self_creds->client = NULL;
2513 krb5_free_creds(ctx, s4u2self_creds);
2516 * For S4U2Proxy we also got a target service principal,
2517 * which also belongs to our own realm (available on
2518 * our client principal).
2520 code = krb5_parse_name(ctx, target_service, &target_princ);
2522 free_Ticket(&s4u2self_ticket);
2523 krb5_free_principal(ctx, whitelist_principal);
2524 krb5_free_principal(ctx, blacklist_principal);
2525 krb5_cc_destroy(ctx, tmp_cc);
2529 code = krb5_principal_set_realm(ctx, target_princ, self_realm);
2531 free_Ticket(&s4u2self_ticket);
2532 krb5_free_principal(ctx, target_princ);
2533 krb5_free_principal(ctx, whitelist_principal);
2534 krb5_free_principal(ctx, blacklist_principal);
2535 krb5_cc_destroy(ctx, tmp_cc);
2539 code = krb5_get_creds_opt_alloc(ctx, &options);
2541 free_Ticket(&s4u2self_ticket);
2542 krb5_free_principal(ctx, target_princ);
2543 krb5_free_principal(ctx, whitelist_principal);
2544 krb5_free_principal(ctx, blacklist_principal);
2545 krb5_cc_destroy(ctx, tmp_cc);
2549 krb5_get_creds_opt_set_options(ctx, options, KRB5_GC_FORWARDABLE);
2550 krb5_get_creds_opt_set_options(ctx, options, KRB5_GC_CONSTRAINED_DELEGATION);
2552 code = krb5_get_creds_opt_set_ticket(ctx, options, &s4u2self_ticket);
2553 free_Ticket(&s4u2self_ticket);
2555 krb5_get_creds_opt_free(ctx, options);
2556 krb5_free_principal(ctx, target_princ);
2557 krb5_free_principal(ctx, whitelist_principal);
2558 krb5_free_principal(ctx, blacklist_principal);
2559 krb5_cc_destroy(ctx, tmp_cc);
2563 code = krb5_get_creds(ctx, options, tmp_cc,
2564 target_princ, &s4u2proxy_creds);
2565 krb5_get_creds_opt_free(ctx, options);
2566 krb5_free_principal(ctx, target_princ);
2567 krb5_cc_destroy(ctx, tmp_cc);
2569 krb5_free_principal(ctx, whitelist_principal);
2570 krb5_free_principal(ctx, blacklist_principal);
2575 * Now make sure we store the impersonated principal
2576 * and creds instead of the TGT related stuff
2577 * in the krb5_ccache of the caller.
2579 code = krb5_copy_creds_contents(ctx, s4u2proxy_creds,
2581 krb5_free_creds(ctx, s4u2proxy_creds);
2583 krb5_free_principal(ctx, whitelist_principal);
2584 krb5_free_principal(ctx, blacklist_principal);
2589 * It's important to store the principal the KDC
2590 * returned, as otherwise the caller would not find
2591 * the S4U2Self ticket in the krb5_ccache lookup.
2593 store_principal = store_creds.client;
2596 if (blacklist_principal &&
2597 krb5_principal_compare(ctx, store_creds.client, blacklist_principal)) {
2601 code = krb5_unparse_name(ctx, blacklist_principal, &sp);
2605 code = krb5_unparse_name(ctx, impersonate_principal, &ip);
2609 DEBUG(1, ("smb_krb5_kinit_password_cache: "
2610 "KDC returned self principal[%s] while impersonating [%s]\n",
2611 sp?sp:"<no memory>",
2612 ip?ip:"<no memory>"));
2617 krb5_free_principal(ctx, whitelist_principal);
2618 krb5_free_principal(ctx, blacklist_principal);
2619 krb5_free_cred_contents(ctx, &store_creds);
2620 return KRB5_FWD_BAD_PRINCIPAL;
2622 if (blacklist_principal) {
2623 krb5_free_principal(ctx, blacklist_principal);
2626 if (whitelist_principal &&
2627 !krb5_principal_compare(ctx, store_creds.client, whitelist_principal)) {
2631 code = krb5_unparse_name(ctx, store_creds.client, &sp);
2635 code = krb5_unparse_name(ctx, whitelist_principal, &ep);
2639 DEBUG(1, ("smb_krb5_kinit_password_cache: "
2640 "KDC returned wrong principal[%s] we expected [%s]\n",
2641 sp?sp:"<no memory>",
2642 ep?ep:"<no memory>"));
2647 krb5_free_principal(ctx, whitelist_principal);
2648 krb5_free_cred_contents(ctx, &store_creds);
2649 return KRB5_FWD_BAD_PRINCIPAL;
2651 if (whitelist_principal) {
2652 krb5_free_principal(ctx, whitelist_principal);
2655 code = krb5_cc_initialize(ctx, store_cc, store_principal);
2657 krb5_free_cred_contents(ctx, &store_creds);
2661 code = krb5_cc_store_cred(ctx, store_cc, &store_creds);
2663 krb5_free_cred_contents(ctx, &store_creds);
2667 client_realm = krb5_principal_get_realm(ctx, store_creds.client);
2668 if (client_realm != NULL) {
2670 * Because the CANON flag doesn't have any impact
2671 * on the impersonate_principal => store_creds.client
2672 * realm mapping. We need to store the credentials twice,
2673 * once with the returned realm and once with the
2674 * realm of impersonate_principal.
2676 code = krb5_principal_set_realm(ctx, store_creds.server,
2679 krb5_free_cred_contents(ctx, &store_creds);
2683 code = krb5_cc_store_cred(ctx, store_cc, &store_creds);
2685 krb5_free_cred_contents(ctx, &store_creds);
2691 *expire_time = (time_t) store_creds.times.endtime;
2695 *kdc_time = (time_t) store_creds.times.starttime;
2698 krb5_free_cred_contents(ctx, &store_creds);
2704 #if !defined(HAVE_KRB5_MAKE_PRINCIPAL) && defined(HAVE_KRB5_BUILD_PRINCIPAL_ALLOC_VA)
2706 * @brief Create a principal name using a variable argument list.
2708 * @param[in] context The library context.
2710 * @param[inout] principal A pointer to the principal structure.
2712 * @param[in] _realm The realm to use. If NULL then the function will
2713 * get the default realm name.
2715 * @param[in] ... A list of 'char *' components, ending with NULL.
2717 * Use krb5_free_principal() to free the principal when it is no longer needed.
2719 * @return 0 on success, a Kerberos error code otherwise.
2721 krb5_error_code smb_krb5_make_principal(krb5_context context,
2722 krb5_principal *principal,
2723 const char *_realm, ...)
2725 krb5_error_code code;
2731 realm = discard_const_p(char, _realm);
2734 code = krb5_get_default_realm(context, &realm);
2741 va_start(ap, _realm);
2742 code = krb5_build_principal_alloc_va(context, principal,
2743 strlen(realm), realm,
2748 krb5_free_default_realm(context, realm);
2755 #if !defined(HAVE_KRB5_CC_GET_LIFETIME) && defined(HAVE_KRB5_CC_RETRIEVE_CRED)
2757 * @brief Get the lifetime of the initial ticket in the cache.
2759 * @param[in] context The kerberos context.
2761 * @param[in] id The credential cache to get the ticket lifetime.
2763 * @param[out] t A pointer to a time value to store the lifetime.
2765 * @return 0 on success, a krb5_error_code on error.
2767 krb5_error_code smb_krb5_cc_get_lifetime(krb5_context context,
2771 krb5_cc_cursor cursor;
2772 krb5_error_code kerr;
2778 kerr = krb5_timeofday(context, &now);
2783 kerr = krb5_cc_start_seq_get(context, id, &cursor);
2788 while ((kerr = krb5_cc_next_cred(context, id, &cursor, &cred)) == 0) {
2789 #ifndef HAVE_FLAGS_IN_KRB5_CREDS
2790 if (cred.ticket_flags & TKT_FLG_INITIAL) {
2792 if (cred.flags.b.initial) {
2794 if (now < cred.times.endtime) {
2795 *t = (time_t) (cred.times.endtime - now);
2797 krb5_free_cred_contents(context, &cred);
2800 krb5_free_cred_contents(context, &cred);
2803 krb5_cc_end_seq_get(context, id, &cursor);
2807 #endif /* HAVE_KRB5_CC_GET_LIFETIME */
2809 #if !defined(HAVE_KRB5_FREE_CHECKSUM_CONTENTS) && defined(HAVE_FREE_CHECKSUM)
2810 void smb_krb5_free_checksum_contents(krb5_context ctx, krb5_checksum *cksum)
2812 free_Checksum(cksum);
2817 * @brief Compute a checksum operating on a keyblock.
2819 * This function computes a checksum over a PAC using the keyblock for a keyed
2822 * @param[in] mem_ctx A talloc context to alocate the signature on.
2824 * @param[in] pac_data The PAC as input.
2826 * @param[in] context The library context.
2828 * @param[in] keyblock Encryption key for a keyed checksum.
2830 * @param[out] sig_type The checksum type
2832 * @param[out] sig_blob The talloc'ed checksum
2834 * The caller must free the sig_blob with talloc_free() when it is not needed
2837 * @return 0 on success, a Kerberos error code otherwise.
2839 krb5_error_code smb_krb5_make_pac_checksum(TALLOC_CTX *mem_ctx,
2840 DATA_BLOB *pac_data,
2841 krb5_context context,
2842 const krb5_keyblock *keyblock,
2844 DATA_BLOB *sig_blob)
2846 krb5_error_code ret;
2847 krb5_checksum cksum;
2848 #if defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CREATE_CHECKSUM)
2852 ret = krb5_crypto_init(context,
2857 DEBUG(0,("krb5_crypto_init() failed: %s\n",
2858 smb_get_krb5_error_message(context, ret, mem_ctx)));
2861 ret = krb5_create_checksum(context,
2863 KRB5_KU_OTHER_CKSUM,
2869 DEBUG(2, ("PAC Verification failed: %s\n",
2870 smb_get_krb5_error_message(context, ret, mem_ctx)));
2873 krb5_crypto_destroy(context, crypto);
2879 *sig_type = cksum.cksumtype;
2880 *sig_blob = data_blob_talloc(mem_ctx,
2881 cksum.checksum.data,
2882 cksum.checksum.length);
2883 #elif defined(HAVE_KRB5_C_MAKE_CHECKSUM)
2886 input.data = (char *)pac_data->data;
2887 input.length = pac_data->length;
2889 ret = krb5_c_make_checksum(context,
2892 KRB5_KEYUSAGE_APP_DATA_CKSUM,
2896 DEBUG(2, ("PAC Verification failed: %s\n",
2897 smb_get_krb5_error_message(context, ret, mem_ctx)));
2901 *sig_type = cksum.checksum_type;
2902 *sig_blob = data_blob_talloc(mem_ctx,
2907 #error krb5_create_checksum or krb5_c_make_checksum not available
2908 #endif /* HAVE_KRB5_C_MAKE_CHECKSUM */
2909 smb_krb5_free_checksum_contents(context, &cksum);
2916 * @brief Get realm of a principal
2918 * @param[in] mem_ctx The talloc ctx to put the result on
2920 * @param[in] context The library context
2922 * @param[in] principal The principal to get the realm from.
2924 * @return A talloced string with the realm or NULL if an error occurred.
2926 char *smb_krb5_principal_get_realm(TALLOC_CTX *mem_ctx,
2927 krb5_context context,
2928 krb5_const_principal principal)
2930 #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
2931 return talloc_strdup(mem_ctx,
2932 krb5_principal_get_realm(context, principal));
2933 #elif defined(krb5_princ_realm) /* MIT */
2934 const krb5_data *realm;
2935 realm = krb5_princ_realm(context, principal);
2936 return talloc_strndup(mem_ctx, realm->data, realm->length);
2938 #error UNKNOWN_GET_PRINC_REALM_FUNCTIONS
2943 * @brief Get realm of a principal
2945 * @param[in] context The library context
2947 * @param[in] principal The principal to set the realm
2949 * @param[in] realm The realm as a string to set.
2951 * @retur 0 on success, a Kerberos error code otherwise.
2953 krb5_error_code smb_krb5_principal_set_realm(krb5_context context,
2954 krb5_principal principal,
2957 #ifdef HAVE_KRB5_PRINCIPAL_SET_REALM /* Heimdal */
2958 return krb5_principal_set_realm(context, principal, realm);
2959 #elif defined(krb5_princ_realm) && defined(krb5_princ_set_realm) /* MIT */
2960 krb5_error_code ret;
2962 krb5_data *old_data;
2964 old_data = krb5_princ_realm(context, principal);
2966 ret = smb_krb5_copy_data_contents(&data,
2973 /* free realm before setting */
2974 free(old_data->data);
2976 krb5_princ_set_realm(context, principal, &data);
2980 #error UNKNOWN_PRINC_SET_REALM_FUNCTION
2986 * @brief Get the realm from the service hostname.
2988 * This function will look for a domain realm mapping in the [domain_realm]
2989 * section of the krb5.conf first and fallback to extract the realm from
2990 * the provided service hostname. As a last resort it will return the
2991 * provided client_realm.
2993 * @param[in] mem_ctx The talloc context
2995 * @param[in] hostname The service hostname
2997 * @param[in] client_realm If we can not find a mapping, fall back to
3000 * @return The realm to use for the service hostname, NULL if a fatal error
3003 char *smb_krb5_get_realm_from_hostname(TALLOC_CTX *mem_ctx,
3004 const char *hostname,
3005 const char *client_realm)
3007 #if defined(HAVE_KRB5_REALM_TYPE)
3009 krb5_realm *realm_list = NULL;
3012 char **realm_list = NULL;
3015 krb5_error_code kerr;
3016 krb5_context ctx = NULL;
3018 kerr = smb_krb5_init_context_common(&ctx);
3020 DBG_ERR("kerberos init context failed (%s)\n",
3021 error_message(kerr));
3025 kerr = krb5_get_host_realm(ctx, hostname, &realm_list);
3026 if (kerr == KRB5_ERR_HOST_REALM_UNKNOWN) {
3031 DEBUG(3,("kerberos_get_realm_from_hostname %s: "
3033 hostname ? hostname : "(NULL)",
3034 error_message(kerr) ));
3038 if (realm_list != NULL &&
3039 realm_list[0] != NULL &&
3040 realm_list[0][0] != '\0') {
3041 realm = talloc_strdup(mem_ctx, realm_list[0]);
3042 if (realm == NULL) {
3046 const char *p = NULL;
3049 * "dc6.samba2003.example.com"
3050 * returns a realm of "SAMBA2003.EXAMPLE.COM"
3052 * "dc6." returns realm as NULL
3054 p = strchr_m(hostname, '.');
3055 if (p != NULL && p[1] != '\0') {
3056 realm = talloc_strdup_upper(mem_ctx, p + 1);
3057 if (realm == NULL) {
3063 if (realm == NULL) {
3064 realm = talloc_strdup(mem_ctx, client_realm);
3071 krb5_free_host_realm(ctx, realm_list);
3074 krb5_free_context(ctx);
3081 * @brief Get an error string from a Kerberos error code.
3083 * @param[in] context The library context.
3085 * @param[in] code The Kerberos error code.
3087 * @param[in] mem_ctx The talloc context to allocate the error string on.
3089 * @return A talloc'ed error string or NULL if an error occurred.
3091 * The caller must free the returned error string with talloc_free() if not
3094 char *smb_get_krb5_error_message(krb5_context context,
3095 krb5_error_code code,
3096 TALLOC_CTX *mem_ctx)
3100 #if defined(HAVE_KRB5_GET_ERROR_MESSAGE) && defined(HAVE_KRB5_FREE_ERROR_MESSAGE)
3101 const char *context_error = krb5_get_error_message(context, code);
3102 if (context_error) {
3103 ret = talloc_asprintf(mem_ctx, "%s: %s",
3104 error_message(code), context_error);
3105 krb5_free_error_message(context, context_error);
3109 ret = talloc_strdup(mem_ctx, error_message(code));
3114 * @brief Return the type of a krb5_principal
3116 * @param[in] context The library context.
3118 * @param[in] principal The principal to get the type from.
3120 * @return The integer type of the principal.
3122 int smb_krb5_principal_get_type(krb5_context context,
3123 krb5_const_principal principal)
3125 #ifdef HAVE_KRB5_PRINCIPAL_GET_TYPE /* Heimdal */
3126 return krb5_principal_get_type(context, principal);
3127 #elif defined(krb5_princ_type) /* MIT */
3128 return krb5_princ_type(context, principal);
3130 #error UNKNOWN_PRINC_GET_TYPE_FUNCTION
3135 * @brief Set the type of a principal
3137 * @param[in] context The library context
3139 * @param[inout] principal The principal to set the type for.
3141 * @param[in] type The principal type to set.
3143 void smb_krb5_principal_set_type(krb5_context context,
3144 krb5_principal principal,
3147 #ifdef HAVE_KRB5_PRINCIPAL_SET_TYPE /* Heimdal */
3148 krb5_principal_set_type(context, principal, type);
3149 #elif defined(krb5_princ_type) /* MIT */
3150 krb5_princ_type(context, principal) = type;
3152 #error UNKNOWN_PRINC_SET_TYPE_FUNCTION
3156 #if !defined(HAVE_KRB5_WARNX)
3158 * @brief Log a Kerberos message
3160 * It sends the message to com_err.
3162 * @param[in] context The library context
3164 * @param[in] fmt The message format
3166 * @param[in] ... The message arguments
3168 * @return 0 on success.
3170 krb5_error_code krb5_warnx(krb5_context context, const char *fmt, ...)
3174 va_start(args, fmt);
3175 com_err_va("samba-kdc", errno, fmt, args);
3183 * @brief Copy a credential cache.
3185 * @param[in] context The library context.
3187 * @param[in] incc Credential cache to be copied.
3189 * @param[inout] outcc Copy of credential cache to be filled in.
3191 * @return 0 on success, a Kerberos error code otherwise.
3193 krb5_error_code smb_krb5_cc_copy_creds(krb5_context context,
3194 krb5_ccache incc, krb5_ccache outcc)
3196 #ifdef HAVE_KRB5_CC_COPY_CACHE /* Heimdal */
3197 return krb5_cc_copy_cache(context, incc, outcc);
3198 #elif defined(HAVE_KRB5_CC_COPY_CREDS)
3199 krb5_error_code ret;
3200 krb5_principal princ = NULL;
3202 ret = krb5_cc_get_principal(context, incc, &princ);
3206 ret = krb5_cc_initialize(context, outcc, princ);
3207 krb5_free_principal(context, princ);
3211 return krb5_cc_copy_creds(context, incc, outcc);
3213 #error UNKNOWN_KRB5_CC_COPY_CACHE_OR_CREDS_FUNCTION
3217 /**********************************************************
3219 **********************************************************/
3221 static bool ads_cleanup_expired_creds(krb5_context context,
3225 krb5_error_code retval;
3226 const char *cc_type = krb5_cc_get_type(context, ccache);
3228 DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
3229 cc_type, krb5_cc_get_name(context, ccache),
3230 http_timestring(talloc_tos(), credsp->times.endtime)));
3232 /* we will probably need new tickets if the current ones
3233 will expire within 10 seconds.
3235 if (credsp->times.endtime >= (time(NULL) + 10))
3238 /* heimdal won't remove creds from a file ccache, and
3239 perhaps we shouldn't anyway, since internally we
3240 use memory ccaches, and a FILE one probably means that
3241 we're using creds obtained outside of our exectuable
3243 if (strequal(cc_type, "FILE")) {
3244 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type));
3248 retval = krb5_cc_remove_cred(context, ccache, 0, credsp);
3250 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
3251 error_message(retval)));
3252 /* If we have an error in this, we want to display it,
3253 but continue as though we deleted it */
3258 /* Allocate and setup the auth context into the state we need. */
3260 static krb5_error_code ads_setup_auth_context(krb5_context context,
3261 krb5_auth_context *auth_context)
3263 krb5_error_code retval;
3265 retval = krb5_auth_con_init(context, auth_context );
3267 DEBUG(1,("krb5_auth_con_init failed (%s)\n",
3268 error_message(retval)));
3272 /* Ensure this is an addressless ticket. */
3273 retval = krb5_auth_con_setaddrs(context, *auth_context, NULL, NULL);
3275 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n",
3276 error_message(retval)));
3282 #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)
3283 static krb5_error_code ads_create_gss_checksum(krb5_data *in_data, /* [inout] */
3286 unsigned int orig_length = in_data->length;
3287 unsigned int base_cksum_size = GSSAPI_CHECKSUM_SIZE;
3288 char *gss_cksum = NULL;
3291 /* Extra length field for delgated ticket. */
3292 base_cksum_size += 4;
3295 if ((unsigned int)base_cksum_size + orig_length <
3296 (unsigned int)base_cksum_size) {
3300 gss_cksum = (char *)SMB_MALLOC(base_cksum_size + orig_length);
3301 if (gss_cksum == NULL) {
3305 memset(gss_cksum, '\0', base_cksum_size + orig_length);
3306 SIVAL(gss_cksum, 0, GSSAPI_BNDLENGTH);
3309 * GSS_C_NO_CHANNEL_BINDINGS means 16 zero bytes.
3310 * This matches the behavior of heimdal and mit.
3312 * And it is needed to work against some closed source
3317 memset(&gss_cksum[4], 0x00, GSSAPI_BNDLENGTH);
3319 SIVAL(gss_cksum, 20, gss_flags);
3321 if (orig_length && in_data->data != NULL) {
3322 SSVAL(gss_cksum, 24, 1); /* The Delegation Option identifier */
3323 SSVAL(gss_cksum, 26, orig_length);
3324 /* Copy the kerberos KRB_CRED data */
3325 memcpy(gss_cksum + 28, in_data->data, orig_length);
3326 free(in_data->data);
3327 in_data->data = NULL;
3328 in_data->length = 0;
3330 in_data->data = gss_cksum;
3331 in_data->length = base_cksum_size + orig_length;
3337 * We can't use krb5_mk_req because w2k wants the service to be in a particular
3340 static krb5_error_code ads_krb5_mk_req(krb5_context context,
3341 krb5_auth_context *auth_context,
3342 const krb5_flags ap_req_options,
3343 const char *principal,
3346 time_t *expire_time,
3347 const char *impersonate_princ_s)
3349 krb5_error_code retval;
3350 krb5_principal server;
3351 krb5_principal impersonate_princ = NULL;
3355 bool creds_ready = false;
3356 int i = 0, maxtries = 3;
3359 ZERO_STRUCT(in_data);
3361 retval = smb_krb5_parse_name(context, principal, &server);
3363 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal));
3367 if (impersonate_princ_s) {
3368 retval = smb_krb5_parse_name(context, impersonate_princ_s,
3369 &impersonate_princ);
3371 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", impersonate_princ_s));
3376 /* obtain ticket & session key */
3378 if ((retval = krb5_copy_principal(context, server, &creds.server))) {
3379 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n",
3380 error_message(retval)));
3384 retval = krb5_cc_get_principal(context, ccache, &creds.client);
3386 /* This can commonly fail on smbd startup with no ticket in the cache.
3387 * Report at higher level than 1. */
3388 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
3389 error_message(retval)));
3393 while (!creds_ready && (i < maxtries)) {
3395 retval = smb_krb5_get_credentials(context,
3402 DBG_WARNING("smb_krb5_get_credentials failed for %s "
3405 error_message(retval));
3409 /* cope with ticket being in the future due to clock skew */
3410 if ((unsigned)credsp->times.starttime > time(NULL)) {
3411 time_t t = time(NULL);
3412 int time_offset =(int)((unsigned)credsp->times.starttime-t);
3413 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset));
3414 krb5_set_real_time(context, t + time_offset + 1, 0);
3417 ok = ads_cleanup_expired_creds(context, ccache, credsp);
3425 DBG_DEBUG("Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
3427 krb5_cc_get_type(context, ccache),
3428 krb5_cc_get_name(context, ccache),
3429 http_timestring(talloc_tos(),
3430 (unsigned)credsp->times.endtime),
3431 (unsigned)credsp->times.endtime);
3434 *expire_time = (time_t)credsp->times.endtime;
3437 /* Allocate the auth_context. */
3438 retval = ads_setup_auth_context(context, auth_context);
3440 DBG_WARNING("ads_setup_auth_context failed (%s)\n",
3441 error_message(retval));
3445 #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)
3447 uint32_t gss_flags = 0;
3449 if (credsp->ticket_flags & TKT_FLG_OK_AS_DELEGATE) {
3451 * Fetch a forwarded TGT from the KDC so that we can
3452 * hand off a 2nd ticket as part of the kerberos
3456 DBG_INFO("Server marked as OK to delegate to, building "
3457 "forwardable TGT\n");
3459 retval = krb5_auth_con_setuseruserkey(context,
3461 &credsp->keyblock );
3463 DBG_WARNING("krb5_auth_con_setuseruserkey "
3465 error_message(retval));
3469 /* Must use a subkey for forwarded tickets. */
3470 retval = krb5_auth_con_setflags(context,
3472 KRB5_AUTH_CONTEXT_USE_SUBKEY);
3474 DBG_WARNING("krb5_auth_con_setflags failed (%s)\n",
3475 error_message(retval));
3479 retval = krb5_fwd_tgt_creds(context,/* Krb5 context [in] */
3480 *auth_context, /* Authentication context [in] */
3481 discard_const_p(char, KRB5_TGS_NAME), /* Ticket service name ("krbtgt") [in] */
3482 credsp->client, /* Client principal for the tgt [in] */
3483 credsp->server, /* Server principal for the tgt [in] */
3484 ccache, /* Credential cache to use for storage [in] */
3485 1, /* Turn on for "Forwardable ticket" [in] */
3486 &in_data ); /* Resulting response [out] */
3489 DBG_INFO("krb5_fwd_tgt_creds failed (%s)\n",
3490 error_message(retval));
3493 * This is not fatal. Delete the *auth_context and continue
3494 * with krb5_mk_req_extended to get a non-forwardable ticket.
3498 free( in_data.data );
3499 in_data.data = NULL;
3502 krb5_auth_con_free(context, *auth_context);
3503 *auth_context = NULL;
3504 retval = ads_setup_auth_context(context, auth_context);
3506 DBG_WARNING("ads_setup_auth_context failed (%s)\n",
3507 error_message(retval));
3511 /* We got a delegated ticket. */
3512 gss_flags |= GSS_C_DELEG_FLAG;
3516 /* Frees and reallocates in_data into a GSS checksum blob. */
3517 retval = ads_create_gss_checksum(&in_data, gss_flags);
3522 /* We always want GSS-checksum types. */
3523 retval = krb5_auth_con_set_req_cksumtype(context, *auth_context, GSSAPI_CHECKSUM );
3525 DEBUG(1,("krb5_auth_con_set_req_cksumtype failed (%s)\n",
3526 error_message(retval)));
3532 retval = krb5_mk_req_extended(context, auth_context, ap_req_options,
3533 &in_data, credsp, outbuf);
3535 DBG_WARNING("krb5_mk_req_extended failed (%s)\n",
3536 error_message(retval));
3539 #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)
3544 free( in_data.data );
3548 krb5_free_creds(context, credsp);
3551 krb5_free_cred_contents(context, &creds);
3554 krb5_free_principal(context, server);
3555 if (impersonate_princ) {
3556 krb5_free_principal(context, impersonate_princ);
3563 get a kerberos5 ticket for the given service
3565 int ads_krb5_cli_get_ticket(TALLOC_CTX *mem_ctx,
3566 const char *principal,
3569 DATA_BLOB *session_key_krb5,
3570 uint32_t extra_ap_opts, const char *ccname,
3572 const char *impersonate_princ_s)
3574 krb5_error_code retval;
3576 krb5_context context = NULL;
3577 krb5_ccache ccdef = NULL;
3578 krb5_auth_context auth_context = NULL;
3579 krb5_enctype enc_types[] = {
3580 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
3581 ENCTYPE_AES256_CTS_HMAC_SHA1_96,
3583 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
3584 ENCTYPE_AES128_CTS_HMAC_SHA1_96,
3586 ENCTYPE_ARCFOUR_HMAC,
3587 ENCTYPE_DES_CBC_MD5,
3588 ENCTYPE_DES_CBC_CRC,
3592 retval = smb_krb5_init_context_common(&context);
3594 DBG_ERR("kerberos init context failed (%s)\n",
3595 error_message(retval));
3599 if (time_offset != 0) {
3600 krb5_set_real_time(context, time(NULL) + time_offset, 0);
3603 retval = krb5_cc_resolve(context,
3604 ccname ? ccname : krb5_cc_default_name(context),
3607 DBG_WARNING("krb5_cc_default failed (%s)\n",
3608 error_message(retval));
3612 retval = krb5_set_default_tgs_ktypes(context, enc_types);
3614 DBG_WARNING("krb5_set_default_tgs_ktypes failed (%s)\n",
3615 error_message(retval));
3619 retval = ads_krb5_mk_req(context,
3621 AP_OPTS_USE_SUBKEY | (krb5_flags)extra_ap_opts,
3626 impersonate_princ_s);
3631 ok = smb_krb5_get_smb_session_key(mem_ctx,
3641 *ticket = data_blob_talloc(mem_ctx, packet.data, packet.length);
3643 smb_krb5_free_data_contents(context, &packet);
3649 krb5_cc_close(context, ccdef);
3652 krb5_auth_con_free(context, auth_context);
3654 krb5_free_context(context);
3660 #ifndef SAMBA4_USES_HEIMDAL /* MITKRB5 tracing callback */
3661 static void smb_krb5_trace_cb(krb5_context ctx,
3662 #ifdef HAVE_KRB5_TRACE_INFO
3663 const krb5_trace_info *info,
3664 #elif defined(HAVE_KRB5_TRACE_INFO_STRUCT)
3665 const struct krb5_trace_info *info,
3667 #error unknown krb5_trace_info
3672 DBGC_DEBUG(DBGC_KERBEROS, "%s", info->message);
3677 krb5_error_code smb_krb5_init_context_common(krb5_context *_krb5_context)
3679 krb5_error_code ret;
3680 krb5_context krb5_ctx;
3682 initialize_krb5_error_table();
3684 ret = krb5_init_context(&krb5_ctx);
3686 DBG_ERR("Krb5 context initialization failed (%s)\n",
3687 error_message(ret));
3691 /* The MIT Kerberos build relies on using the system krb5.conf file.
3692 * If you really want to use another file please set KRB5_CONFIG
3694 #ifndef SAMBA4_USES_HEIMDAL
3695 ret = krb5_set_trace_callback(krb5_ctx, smb_krb5_trace_cb, NULL);
3697 DBG_ERR("Failed to set MIT kerberos trace callback! (%s)\n",
3698 error_message(ret));
3702 #ifdef SAMBA4_USES_HEIMDAL
3703 /* Set options in kerberos */
3704 krb5_set_dns_canonicalize_hostname(krb5_ctx, false);
3707 *_krb5_context = krb5_ctx;
3711 #else /* HAVE_KRB5 */
3712 /* This saves a few linking headaches */
3713 int ads_krb5_cli_get_ticket(TALLOC_CTX *mem_ctx,
3714 const char *principal,
3717 DATA_BLOB *session_key_krb5,
3718 uint32_t extra_ap_opts, const char *ccname,
3720 const char *impersonate_princ_s)
3722 DEBUG(0,("NO KERBEROS SUPPORT\n"));
3726 #endif /* HAVE_KRB5 */