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/crypto.h"
30 #endif /* HAVE_COM_ERR_H */
32 #ifndef KRB5_AUTHDATA_WIN2K_PAC
33 #define KRB5_AUTHDATA_WIN2K_PAC 128
36 #ifndef KRB5_AUTHDATA_IF_RELEVANT
37 #define KRB5_AUTHDATA_IF_RELEVANT 1
42 #define GSSAPI_CHECKSUM 0x8003 /* Checksum type value for Kerberos */
43 #define GSSAPI_BNDLENGTH 16 /* Bind Length (rfc-1964 pg.3) */
44 #define GSSAPI_CHECKSUM_SIZE (4+GSSAPI_BNDLENGTH+4) /* Length of bind length,
45 bind field, flags field. */
46 #define GSS_C_DELEG_FLAG 1
48 /* MIT krb5 1.7beta3 (in Ubuntu Karmic) is missing the prototype,
49 but still has the symbol */
50 #if !HAVE_DECL_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE
51 krb5_error_code
krb5_auth_con_set_req_cksumtype(
53 krb5_auth_context auth_context
,
54 krb5_cksumtype cksumtype
);
57 #if !defined(SMB_MALLOC)
59 #define SMB_MALLOC(s) malloc((s))
63 #define SMB_STRDUP(s) strdup(s)
66 /**********************************************************
68 **********************************************************/
70 #if !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
72 #if defined(HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES)
74 /* With MIT kerberos, we should use krb5_set_default_tgs_enctypes in preference
75 * to krb5_set_default_tgs_ktypes. See
76 * http://lists.samba.org/archive/samba-technical/2006-July/048271.html
78 * If the MIT libraries are not exporting internal symbols, we will end up in
79 * this branch, which is correct. Otherwise we will continue to use the
82 krb5_error_code
krb5_set_default_tgs_ktypes(krb5_context ctx
, const krb5_enctype
*enc
)
84 return krb5_set_default_tgs_enctypes(ctx
, enc
);
87 #elif defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES)
90 krb5_error_code
krb5_set_default_tgs_ktypes(krb5_context ctx
, const krb5_enctype
*enc
)
92 return krb5_set_default_in_tkt_etypes(ctx
, enc
);
95 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES */
97 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_KTYPES */
100 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
101 krb5_error_code
krb5_auth_con_setuseruserkey(krb5_context context
,
102 krb5_auth_context auth_context
,
103 krb5_keyblock
*keyblock
)
105 return krb5_auth_con_setkey(context
, auth_context
, keyblock
);
109 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
110 void krb5_free_unparsed_name(krb5_context context
, char *val
)
116 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
117 const krb5_data
*krb5_princ_component(krb5_context context
,
118 krb5_principal principal
, int i
);
120 const krb5_data
*krb5_princ_component(krb5_context context
,
121 krb5_principal principal
, int i
)
123 static krb5_data kdata
;
125 kdata
.data
= discard_const_p(char, krb5_principal_get_comp_string(context
, principal
, i
));
126 kdata
.length
= strlen((const char *)kdata
.data
);
132 /**********************************************************
134 **********************************************************/
136 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
140 * @brief Stores the address of a 'struct sockaddr_storage' a krb5_address
142 * @param[in] paddr A pointer to a 'struct sockaddr_storage to extract the
145 * @param[out] pkaddr A Kerberos address to store tha address in.
147 * @return True on success, false if an error occurred.
149 bool smb_krb5_sockaddr_to_kaddr(struct sockaddr_storage
*paddr
,
150 krb5_address
*pkaddr
)
152 memset(pkaddr
, '\0', sizeof(krb5_address
));
153 #if defined(HAVE_IPV6) && defined(KRB5_ADDRESS_INET6)
154 if (paddr
->ss_family
== AF_INET6
) {
155 pkaddr
->addr_type
= KRB5_ADDRESS_INET6
;
156 pkaddr
->address
.length
= sizeof(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
157 pkaddr
->address
.data
= (char *)&(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
161 if (paddr
->ss_family
== AF_INET
) {
162 pkaddr
->addr_type
= KRB5_ADDRESS_INET
;
163 pkaddr
->address
.length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
164 pkaddr
->address
.data
= (char *)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
169 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
173 * @brief Stores the address of a 'struct sockaddr_storage' a krb5_address
175 * @param[in] paddr A pointer to a 'struct sockaddr_storage to extract the
178 * @param[in] pkaddr A Kerberos address to store tha address in.
180 * @return True on success, false if an error occurred.
182 bool smb_krb5_sockaddr_to_kaddr(struct sockaddr_storage
*paddr
,
183 krb5_address
*pkaddr
)
185 memset(pkaddr
, '\0', sizeof(krb5_address
));
186 #if defined(HAVE_IPV6) && defined(ADDRTYPE_INET6)
187 if (paddr
->ss_family
== AF_INET6
) {
188 pkaddr
->addrtype
= ADDRTYPE_INET6
;
189 pkaddr
->length
= sizeof(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
190 pkaddr
->contents
= (krb5_octet
*)&(((struct sockaddr_in6
*)paddr
)->sin6_addr
);
194 if (paddr
->ss_family
== AF_INET
) {
195 pkaddr
->addrtype
= ADDRTYPE_INET
;
196 pkaddr
->length
= sizeof(((struct sockaddr_in
*)paddr
)->sin_addr
);
197 pkaddr
->contents
= (krb5_octet
*)&(((struct sockaddr_in
*)paddr
)->sin_addr
);
203 #error UNKNOWN_ADDRTYPE
206 krb5_error_code
smb_krb5_mk_error(krb5_context context
,
207 krb5_error_code error_code
,
210 const krb5_principal client
,
211 const krb5_principal server
,
214 krb5_error_code code
= EINVAL
;
215 #ifdef SAMBA4_USES_HEIMDAL
216 code
= krb5_mk_error(context
,
222 NULL
, /* client_time */
223 NULL
, /* client_usec */
226 krb5_principal unspec_server
= NULL
;
232 code
= krb5_us_timeofday(context
,
239 errpkt
.error
= error_code
;
241 errpkt
.text
.length
= 0;
242 if (e_text
!= NULL
) {
243 errpkt
.text
.length
= strlen(e_text
);
244 errpkt
.text
.data
= discard_const_p(char, e_text
);
247 errpkt
.e_data
.magic
= KV5M_DATA
;
248 errpkt
.e_data
.length
= 0;
249 errpkt
.e_data
.data
= NULL
;
250 if (e_data
!= NULL
) {
251 errpkt
.e_data
= *e_data
;
254 errpkt
.client
= client
;
256 if (server
!= NULL
) {
257 errpkt
.server
= server
;
259 code
= smb_krb5_make_principal(context
,
261 "<unspecified realm>",
266 errpkt
.server
= unspec_server
;
269 code
= krb5_mk_error(context
,
272 krb5_free_principal(context
, unspec_server
);
278 * @brief Create a keyblock based on input parameters
280 * @param context The krb5_context
281 * @param host_princ The krb5_principal to use
282 * @param salt The optional salt, if omitted, salt is calculated with
283 * the provided principal.
284 * @param password The krb5_data containing the password
285 * @param enctype The krb5_enctype to use for the keyblock generation
286 * @param key The returned krb5_keyblock, caller needs to free with
287 * krb5_free_keyblock().
289 * @return krb5_error_code
291 int smb_krb5_create_key_from_string(krb5_context context
,
292 krb5_const_principal host_princ
,
295 krb5_enctype enctype
,
300 if (host_princ
== NULL
&& salt
== NULL
) {
304 if ((int)enctype
== (int)ENCTYPE_ARCFOUR_HMAC
) {
305 TALLOC_CTX
*frame
= talloc_stackframe();
306 uint8_t *utf16
= NULL
;
307 size_t utf16_size
= 0;
311 ok
= convert_string_talloc(frame
, CH_UNIX
, CH_UTF16LE
,
312 password
->data
, password
->length
,
313 (void **)&utf16
, &utf16_size
);
323 mdfour(nt_hash
, utf16
, utf16_size
);
324 memset(utf16
, 0, utf16_size
);
325 ret
= smb_krb5_keyblock_init_contents(context
,
326 ENCTYPE_ARCFOUR_HMAC
,
330 ZERO_STRUCT(nt_hash
);
340 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_C_STRING_TO_KEY)
345 ret
= krb5_principal2salt(context
, host_princ
, &_salt
);
347 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret
)));
353 ret
= krb5_c_string_to_key(context
, enctype
, password
, &_salt
, key
);
355 SAFE_FREE(_salt
.data
);
358 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
363 ret
= krb5_get_pw_salt(context
, host_princ
, &_salt
);
365 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret
)));
369 _salt
.saltvalue
= *salt
;
370 _salt
.salttype
= KRB5_PW_SALT
;
373 ret
= krb5_string_to_key_salt(context
, enctype
, (const char *)password
->data
, _salt
, key
);
375 krb5_free_salt(context
, _salt
);
379 #error UNKNOWN_CREATE_KEY_FUNCTIONS
385 * @brief Create a salt for a given principal
387 * @param context The initialized krb5_context
388 * @param host_princ The krb5_principal to create the salt for
389 * @param psalt A pointer to a krb5_data struct
391 * caller has to free the contents of psalt with smb_krb5_free_data_contents
392 * when function has succeeded
394 * @return krb5_error_code, returns 0 on success, error code otherwise
397 int smb_krb5_get_pw_salt(krb5_context context
,
398 krb5_const_principal host_princ
,
400 #if defined(HAVE_KRB5_GET_PW_SALT)
406 ret
= krb5_get_pw_salt(context
, host_princ
, &salt
);
411 psalt
->data
= salt
.saltvalue
.data
;
412 psalt
->length
= salt
.saltvalue
.length
;
416 #elif defined(HAVE_KRB5_PRINCIPAL2SALT)
419 return krb5_principal2salt(context
, host_princ
, psalt
);
422 #error UNKNOWN_SALT_FUNCTIONS
425 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
427 * @brief Get a list of encryption types allowed for session keys
429 * @param[in] context The library context
431 * @param[in] enctypes An allocated, zero-terminated list of encryption types
433 * This function returns an allocated list of encryption types allowed for
436 * Use free() to free the enctypes when it is no longer needed.
438 * @retval 0 Success; otherwise - Kerberos error codes
440 krb5_error_code
smb_krb5_get_allowed_etypes(krb5_context context
,
441 krb5_enctype
**enctypes
)
443 return krb5_get_permitted_enctypes(context
, enctypes
);
445 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
446 krb5_error_code
smb_krb5_get_allowed_etypes(krb5_context context
,
447 krb5_enctype
**enctypes
)
449 #ifdef HAVE_KRB5_PDU_NONE_DECL
450 return krb5_get_default_in_tkt_etypes(context
, KRB5_PDU_NONE
, enctypes
);
452 return krb5_get_default_in_tkt_etypes(context
, enctypes
);
456 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
461 * @brief Convert a string principal name to a Kerberos principal.
463 * @param[in] context The library context
465 * @param[in] name The principal as a unix charset string.
467 * @param[out] principal The newly allocated principal.
469 * Use krb5_free_principal() to free a principal when it is no longer needed.
471 * @return 0 on success, a Kerberos error code otherwise.
473 krb5_error_code
smb_krb5_parse_name(krb5_context context
,
475 krb5_principal
*principal
)
479 size_t converted_size
;
480 TALLOC_CTX
*frame
= talloc_stackframe();
482 if (!push_utf8_talloc(frame
, &utf8_name
, name
, &converted_size
)) {
487 ret
= krb5_parse_name(context
, utf8_name
, principal
);
493 * @brief Convert a Kerberos principal structure to a string representation.
495 * The resulting string representation will be a unix charset name and is
498 * @param[in] mem_ctx The talloc context to allocate memory on.
500 * @param[in] context The library context.
502 * @param[in] principal The principal.
504 * @param[out] unix_name A string representation of the princpial name as with
507 * Use talloc_free() to free the string representation if it is no longer
510 * @return 0 on success, a Kerberos error code otherwise.
512 krb5_error_code
smb_krb5_unparse_name(TALLOC_CTX
*mem_ctx
,
513 krb5_context context
,
514 krb5_const_principal principal
,
519 size_t converted_size
;
522 ret
= krb5_unparse_name(context
, principal
, &utf8_name
);
527 if (!pull_utf8_talloc(mem_ctx
, unix_name
, utf8_name
, &converted_size
)) {
528 krb5_free_unparsed_name(context
, utf8_name
);
531 krb5_free_unparsed_name(context
, utf8_name
);
536 * @brief Free the contents of a krb5_data structure and zero the data field.
538 * @param[in] context The krb5 context
540 * @param[in] pdata The data structure to free contents of
542 * This function frees the contents, not the structure itself.
544 void smb_krb5_free_data_contents(krb5_context context
, krb5_data
*pdata
)
546 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
548 krb5_free_data_contents(context
, pdata
);
550 #elif defined(HAVE_KRB5_DATA_FREE)
551 krb5_data_free(context
, pdata
);
553 SAFE_FREE(pdata
->data
);
558 * @brief copy a buffer into a krb5_data struct
560 * @param[in] p The krb5_data
561 * @param[in] data The data to copy
562 * @param[in] length The length of the data to copy
563 * @return krb5_error_code
565 * Caller has to free krb5_data with smb_krb5_free_data_contents().
567 krb5_error_code
smb_krb5_copy_data_contents(krb5_data
*p
,
571 #if defined(HAVE_KRB5_DATA_COPY)
572 return krb5_data_copy(p
, data
, len
);
575 p
->data
= malloc(len
);
576 if (p
->data
== NULL
) {
579 memmove(p
->data
, data
, len
);
584 p
->magic
= KV5M_DATA
;
589 bool smb_krb5_get_smb_session_key(TALLOC_CTX
*mem_ctx
,
590 krb5_context context
,
591 krb5_auth_context auth_context
,
592 DATA_BLOB
*session_key
,
595 krb5_keyblock
*skey
= NULL
;
596 krb5_error_code err
= 0;
600 #ifdef HAVE_KRB5_AUTH_CON_GETRECVSUBKEY
601 err
= krb5_auth_con_getrecvsubkey(context
,
604 #else /* HAVE_KRB5_AUTH_CON_GETRECVSUBKEY */
605 err
= krb5_auth_con_getremotesubkey(context
,
606 auth_context
, &skey
);
607 #endif /* HAVE_KRB5_AUTH_CON_GETRECVSUBKEY */
609 #ifdef HAVE_KRB5_AUTH_CON_GETSENDSUBKEY
610 err
= krb5_auth_con_getsendsubkey(context
,
613 #else /* HAVE_KRB5_AUTH_CON_GETSENDSUBKEY */
614 err
= krb5_auth_con_getlocalsubkey(context
,
615 auth_context
, &skey
);
616 #endif /* HAVE_KRB5_AUTH_CON_GETSENDSUBKEY */
619 if (err
|| skey
== NULL
) {
620 DEBUG(10, ("KRB5 error getting session key %d\n", err
));
624 DEBUG(10, ("Got KRB5 session key of length %d\n",
625 (int)KRB5_KEY_LENGTH(skey
)));
627 *session_key
= data_blob_talloc(mem_ctx
,
629 KRB5_KEY_LENGTH(skey
));
630 dump_data_pw("KRB5 Session Key:\n",
632 session_key
->length
);
638 krb5_free_keyblock(context
, skey
);
646 * @brief Get talloced string component of a principal
648 * @param[in] mem_ctx The TALLOC_CTX
649 * @param[in] context The krb5_context
650 * @param[in] principal The principal
651 * @param[in] component The component
652 * @return string component
654 * Caller must talloc_free if the return value is not NULL.
657 char *smb_krb5_principal_get_comp_string(TALLOC_CTX
*mem_ctx
,
658 krb5_context context
,
659 krb5_const_principal principal
,
660 unsigned int component
)
662 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
663 return talloc_strdup(mem_ctx
, krb5_principal_get_comp_string(context
, principal
, component
));
667 if (component
>= krb5_princ_size(context
, principal
)) {
671 data
= krb5_princ_component(context
, principal
, component
);
676 return talloc_strndup(mem_ctx
, data
->data
, data
->length
);
683 * @param[in] ccache_string A string pointing to the cache to renew the ticket
684 * (e.g. FILE:/tmp/krb5cc_0) or NULL. If the principal
685 * ccache has not been specified, the default ccache
688 * @param[in] client_string The client principal string (e.g. user@SAMBA.SITE)
689 * or NULL. If the principal string has not been
690 * specified, the principal from the ccache will be
693 * @param[in] service_string The service ticket string
694 * (e.g. krbtgt/SAMBA.SITE@SAMBA.SITE) or NULL. If
695 * the sevice ticket is specified, it is parsed (
696 * with the realm part ignored) and used as the
697 * server principal of the credential. Otherwise
698 * the ticket-granting service is used.
700 * @param[in] expire_time A pointer to store the credentials end time or
703 * @return 0 on Succes, a Kerberos error code otherwise.
705 krb5_error_code
smb_krb5_renew_ticket(const char *ccache_string
,
706 const char *client_string
,
707 const char *service_string
,
711 krb5_context context
= NULL
;
712 krb5_ccache ccache
= NULL
;
713 krb5_principal client
= NULL
;
714 krb5_creds creds
, creds_in
;
717 ZERO_STRUCT(creds_in
);
719 initialize_krb5_error_table();
720 ret
= krb5_init_context(&context
);
725 if (!ccache_string
) {
726 ccache_string
= krb5_cc_default_name(context
);
729 if (!ccache_string
) {
734 DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string
));
736 /* FIXME: we should not fall back to defaults */
737 ret
= krb5_cc_resolve(context
, discard_const_p(char, ccache_string
), &ccache
);
743 ret
= smb_krb5_parse_name(context
, client_string
, &client
);
748 ret
= krb5_cc_get_principal(context
, ccache
, &client
);
754 ret
= krb5_get_renewed_creds(context
, &creds
, client
, ccache
, discard_const_p(char, service_string
));
756 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret
)));
760 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
761 ret
= krb5_cc_initialize(context
, ccache
, client
);
766 ret
= krb5_cc_store_cred(context
, ccache
, &creds
);
769 *expire_time
= (time_t) creds
.times
.endtime
;
773 krb5_free_cred_contents(context
, &creds_in
);
774 krb5_free_cred_contents(context
, &creds
);
777 krb5_free_principal(context
, client
);
780 krb5_cc_close(context
, ccache
);
783 krb5_free_context(context
);
790 * @brief Free the data stored in an smb_krb5_addresses structure.
792 * @param[in] context The library context
794 * @param[in] addr The address structure to free.
796 * @return 0 on success, a Kerberos error code otherwise.
798 krb5_error_code
smb_krb5_free_addresses(krb5_context context
,
799 smb_krb5_addresses
*addr
)
801 krb5_error_code ret
= 0;
805 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
806 krb5_free_addresses(context
, addr
->addrs
);
807 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
808 ret
= krb5_free_addresses(context
, addr
->addrs
);
809 SAFE_FREE(addr
->addrs
);
816 #define MAX_NETBIOSNAME_LEN 16
819 * @brief Add a netbios name to the array of addresses
821 * @param[in] kerb_addr A pointer to the smb_krb5_addresses to add the
824 * @param[in] netbios_name The netbios name to add.
826 * @return 0 on success, a Kerberos error code otherwise.
828 krb5_error_code
smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses
**kerb_addr
,
829 const char *netbios_name
)
831 krb5_error_code ret
= 0;
832 char buf
[MAX_NETBIOSNAME_LEN
];
834 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
835 krb5_address
**addrs
= NULL
;
836 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
837 krb5_addresses
*addrs
= NULL
;
840 *kerb_addr
= (smb_krb5_addresses
*)SMB_MALLOC(sizeof(smb_krb5_addresses
));
841 if (*kerb_addr
== NULL
) {
845 /* temporarily duplicate put_name() code here to avoid dependency
846 * issues for a 5 lines function */
847 len
= strlen(netbios_name
);
848 memcpy(buf
, netbios_name
,
849 (len
< MAX_NETBIOSNAME_LEN
) ? len
: MAX_NETBIOSNAME_LEN
- 1);
850 if (len
< MAX_NETBIOSNAME_LEN
- 1) {
851 memset(buf
+ len
, ' ', MAX_NETBIOSNAME_LEN
- 1 - len
);
853 buf
[MAX_NETBIOSNAME_LEN
- 1] = 0x20;
855 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
859 addrs
= (krb5_address
**)SMB_MALLOC(sizeof(krb5_address
*) * num_addr
);
861 SAFE_FREE(*kerb_addr
);
865 memset(addrs
, 0, sizeof(krb5_address
*) * num_addr
);
867 addrs
[0] = (krb5_address
*)SMB_MALLOC(sizeof(krb5_address
));
868 if (addrs
[0] == NULL
) {
870 SAFE_FREE(*kerb_addr
);
874 addrs
[0]->magic
= KV5M_ADDRESS
;
875 addrs
[0]->addrtype
= KRB5_ADDR_NETBIOS
;
876 addrs
[0]->length
= MAX_NETBIOSNAME_LEN
;
877 addrs
[0]->contents
= (unsigned char *)SMB_MALLOC(addrs
[0]->length
);
878 if (addrs
[0]->contents
== NULL
) {
881 SAFE_FREE(*kerb_addr
);
885 memcpy(addrs
[0]->contents
, buf
, addrs
[0]->length
);
889 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
891 addrs
= (krb5_addresses
*)SMB_MALLOC(sizeof(krb5_addresses
));
893 SAFE_FREE(*kerb_addr
);
897 memset(addrs
, 0, sizeof(krb5_addresses
));
900 addrs
->val
= (krb5_address
*)SMB_MALLOC(sizeof(krb5_address
));
901 if (addrs
->val
== NULL
) {
903 SAFE_FREE(kerb_addr
);
907 addrs
->val
[0].addr_type
= KRB5_ADDR_NETBIOS
;
908 addrs
->val
[0].address
.length
= MAX_NETBIOSNAME_LEN
;
909 addrs
->val
[0].address
.data
= (unsigned char *)SMB_MALLOC(addrs
->val
[0].address
.length
);
910 if (addrs
->val
[0].address
.data
== NULL
) {
911 SAFE_FREE(addrs
->val
);
913 SAFE_FREE(*kerb_addr
);
917 memcpy(addrs
->val
[0].address
.data
, buf
, addrs
->val
[0].address
.length
);
920 #error UNKNOWN_KRB5_ADDRESS_FORMAT
922 (*kerb_addr
)->addrs
= addrs
;
928 * @brief Get the enctype from a key table entry
930 * @param[in] kt_entry Key table entry to get the enctype from.
932 * @return The enctype from the entry.
934 krb5_enctype
smb_krb5_kt_get_enctype_from_entry(krb5_keytab_entry
*kt_entry
)
936 return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry
));
940 * @brief Free the contents of a key table entry.
942 * @param[in] context The library context.
944 * @param[in] kt_entry The key table entry to free the contents of.
946 * @return 0 on success, a Kerberos error code otherwise.
948 * The pointer itself is not freed.
950 krb5_error_code
smb_krb5_kt_free_entry(krb5_context context
,
951 krb5_keytab_entry
*kt_entry
)
953 /* Try krb5_free_keytab_entry_contents first, since
954 * MIT Kerberos >= 1.7 has both krb5_free_keytab_entry_contents and
955 * krb5_kt_free_entry but only has a prototype for the first, while the
956 * second is considered private.
958 #if defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
959 return krb5_free_keytab_entry_contents(context
, kt_entry
);
960 #elif defined(HAVE_KRB5_KT_FREE_ENTRY)
961 return krb5_kt_free_entry(context
, kt_entry
);
963 #error UNKNOWN_KT_FREE_FUNCTION
969 * @brief Convert an encryption type to a string.
971 * @param[in] context The library context.
973 * @param[in] enctype The encryption type.
975 * @param[in] etype_s A pointer to store the allocated encryption type as a
978 * @return 0 on success, a Kerberos error code otherwise.
980 * The caller needs to free the allocated string etype_s.
982 krb5_error_code
smb_krb5_enctype_to_string(krb5_context context
,
983 krb5_enctype enctype
,
986 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
987 return krb5_enctype_to_string(context
, enctype
, etype_s
); /* Heimdal */
988 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
990 krb5_error_code ret
= krb5_enctype_to_string(enctype
, buf
, 256); /* MIT */
994 *etype_s
= SMB_STRDUP(buf
);
1000 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
1004 /* This MAX_NAME_LEN is a constant defined in krb5.h */
1005 #ifndef MAX_KEYTAB_NAME_LEN
1006 #define MAX_KEYTAB_NAME_LEN 1100
1010 * @brief Open a key table readonly or with readwrite access.
1012 * Allows to use a different keytab than the default one using a relative
1013 * path to the keytab.
1015 * @param[in] context The library context
1017 * @param[in] keytab_name_req The path to the key table.
1019 * @param[in] write_access Open with readwrite access.
1021 * @param[in] keytab A pointer o the opended key table.
1023 * The keytab pointer should be freed using krb5_kt_close().
1025 * @return 0 on success, a Kerberos error code otherwise.
1027 krb5_error_code
smb_krb5_kt_open_relative(krb5_context context
,
1028 const char *keytab_name_req
,
1030 krb5_keytab
*keytab
)
1032 krb5_error_code ret
= 0;
1033 TALLOC_CTX
*mem_ctx
;
1034 char keytab_string
[MAX_KEYTAB_NAME_LEN
];
1035 char *kt_str
= NULL
;
1036 bool found_valid_name
= false;
1037 const char *pragma
= "FILE";
1038 const char *tmp
= NULL
;
1040 if (!write_access
&& !keytab_name_req
) {
1041 /* caller just wants to read the default keytab readonly, so be it */
1042 return krb5_kt_default(context
, keytab
);
1045 mem_ctx
= talloc_init("smb_krb5_open_keytab");
1050 #ifdef HAVE_WRFILE_KEYTAB
1056 if (keytab_name_req
) {
1058 if (strlen(keytab_name_req
) > MAX_KEYTAB_NAME_LEN
) {
1059 ret
= KRB5_CONFIG_NOTENUFSPACE
;
1063 if ((strncmp(keytab_name_req
, "WRFILE:", 7) == 0) ||
1064 (strncmp(keytab_name_req
, "FILE:", 5) == 0)) {
1065 tmp
= keytab_name_req
;
1069 tmp
= talloc_asprintf(mem_ctx
, "%s:%s", pragma
, keytab_name_req
);
1078 /* we need to handle more complex keytab_strings, like:
1079 * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
1081 ret
= krb5_kt_default_name(context
, &keytab_string
[0], MAX_KEYTAB_NAME_LEN
- 2);
1086 DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string
));
1088 tmp
= talloc_strdup(mem_ctx
, keytab_string
);
1094 if (strncmp(tmp
, "ANY:", 4) == 0) {
1098 memset(&keytab_string
, '\0', sizeof(keytab_string
));
1100 while (next_token_talloc(mem_ctx
, &tmp
, &kt_str
, ",")) {
1101 if (strncmp(kt_str
, "WRFILE:", 7) == 0) {
1102 found_valid_name
= true;
1107 if (strncmp(kt_str
, "FILE:", 5) == 0) {
1108 found_valid_name
= true;
1113 if (tmp
[0] == '/') {
1114 /* Treat as a FILE: keytab definition. */
1115 found_valid_name
= true;
1118 if (found_valid_name
) {
1119 if (tmp
[0] != '/') {
1120 ret
= KRB5_KT_BADNAME
;
1124 tmp
= talloc_asprintf(mem_ctx
, "%s:%s", pragma
, tmp
);
1133 if (!found_valid_name
) {
1134 ret
= KRB5_KT_UNKNOWN_TYPE
;
1139 DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp
));
1140 ret
= krb5_kt_resolve(context
, tmp
, keytab
);
1143 TALLOC_FREE(mem_ctx
);
1148 * @brief Open a key table readonly or with readwrite access.
1150 * Allows to use a different keytab than the default one. The path needs to be
1151 * an absolute path or an error will be returned.
1153 * @param[in] context The library context
1155 * @param[in] keytab_name_req The path to the key table.
1157 * @param[in] write_access Open with readwrite access.
1159 * @param[in] keytab A pointer o the opended key table.
1161 * The keytab pointer should be freed using krb5_kt_close().
1163 * @return 0 on success, a Kerberos error code otherwise.
1165 krb5_error_code
smb_krb5_kt_open(krb5_context context
,
1166 const char *keytab_name_req
,
1168 krb5_keytab
*keytab
)
1172 if (keytab_name_req
== NULL
) {
1173 return KRB5_KT_BADNAME
;
1176 if (keytab_name_req
[0] == '/') {
1180 cmp
= strncmp(keytab_name_req
, "FILE:/", 6);
1185 cmp
= strncmp(keytab_name_req
, "WRFILE:/", 8);
1190 DBG_WARNING("ERROR: Invalid keytab name: %s\n", keytab_name_req
);
1192 return KRB5_KT_BADNAME
;
1195 return smb_krb5_kt_open_relative(context
,
1202 * @brief Get a key table name.
1204 * @param[in] mem_ctx The talloc context to use for allocation.
1206 * @param[in] context The library context.
1208 * @param[in] keytab The key table to get the name from.
1210 * @param[in] keytab_name A talloc'ed string of the key table name.
1212 * The talloc'ed name string needs to be freed with talloc_free().
1214 * @return 0 on success, a Kerberos error code otherwise.
1216 krb5_error_code
smb_krb5_kt_get_name(TALLOC_CTX
*mem_ctx
,
1217 krb5_context context
,
1219 const char **keytab_name
)
1221 char keytab_string
[MAX_KEYTAB_NAME_LEN
];
1222 krb5_error_code ret
= 0;
1224 ret
= krb5_kt_get_name(context
, keytab
,
1225 keytab_string
, MAX_KEYTAB_NAME_LEN
- 2);
1230 *keytab_name
= talloc_strdup(mem_ctx
, keytab_string
);
1231 if (!*keytab_name
) {
1239 * @brief Seek and delete old entries in a keytab based on the passed
1242 * @param[in] context The KRB5 context to use.
1244 * @param[in] keytab The keytab to operate on.
1246 * @param[in] kvno The kvnco to use.
1248 * @param[in] princ_s The principal as a string to search for.
1250 * @param[in] princ The principal as a krb5_principal to search for.
1252 * @param[in] flush Weather to flush the complete keytab.
1254 * @param[in] keep_old_entries Keep the entry with the previous kvno.
1256 * @retval 0 on Sucess
1258 * @return An appropriate KRB5 error code.
1260 krb5_error_code
smb_krb5_kt_seek_and_delete_old_entries(krb5_context context
,
1263 krb5_enctype enctype
,
1264 const char *princ_s
,
1265 krb5_principal princ
,
1267 bool keep_old_entries
)
1269 krb5_error_code ret
;
1270 krb5_kt_cursor cursor
;
1271 krb5_keytab_entry kt_entry
;
1272 char *ktprinc
= NULL
;
1273 krb5_kvno old_kvno
= kvno
- 1;
1274 TALLOC_CTX
*tmp_ctx
;
1276 ZERO_STRUCT(cursor
);
1277 ZERO_STRUCT(kt_entry
);
1279 ret
= krb5_kt_start_seq_get(context
, keytab
, &cursor
);
1280 if (ret
== KRB5_KT_END
|| ret
== ENOENT
) {
1285 tmp_ctx
= talloc_new(NULL
);
1286 if (tmp_ctx
== NULL
) {
1290 DEBUG(3, (__location__
": Will try to delete old keytab entries\n"));
1291 while (!krb5_kt_next_entry(context
, keytab
, &kt_entry
, &cursor
)) {
1292 bool name_ok
= false;
1293 krb5_enctype kt_entry_enctype
=
1294 smb_krb5_kt_get_enctype_from_entry(&kt_entry
);
1296 if (!flush
&& (princ_s
!= NULL
)) {
1297 ret
= smb_krb5_unparse_name(tmp_ctx
, context
,
1301 DEBUG(1, (__location__
1302 ": smb_krb5_unparse_name failed "
1303 "(%s)\n", error_message(ret
)));
1307 #ifdef HAVE_KRB5_KT_COMPARE
1308 name_ok
= krb5_kt_compare(context
, &kt_entry
,
1311 name_ok
= (strcmp(ktprinc
, princ_s
) == 0);
1315 DEBUG(10, (__location__
": ignoring keytab "
1316 "entry principal %s, kvno = %d\n",
1317 ktprinc
, kt_entry
.vno
));
1320 * just free this entry and continue. */
1321 ret
= smb_krb5_kt_free_entry(context
,
1323 ZERO_STRUCT(kt_entry
);
1325 DEBUG(1, (__location__
1326 ": smb_krb5_kt_free_entry "
1328 error_message(ret
)));
1332 TALLOC_FREE(ktprinc
);
1336 TALLOC_FREE(ktprinc
);
1339 /*------------------------------------------------------------
1340 * Save the entries with kvno - 1. This is what microsoft does
1341 * to allow people with existing sessions that have kvno - 1
1342 * to still work. Otherwise, when the password for the machine
1343 * changes, all kerberizied sessions will 'break' until either
1344 * the client reboots or the client's session key expires and
1345 * they get a new session ticket with the new kvno.
1346 * Some keytab files only store the kvno in 8bits, limit
1347 * the compare accordingly.
1350 if (!flush
&& ((kt_entry
.vno
& 0xff) == (old_kvno
& 0xff))) {
1351 DEBUG(5, (__location__
": Saving previous (kvno %d) "
1352 "entry for principal: %s.\n",
1353 old_kvno
, princ_s
));
1357 if (keep_old_entries
) {
1358 DEBUG(5, (__location__
": Saving old (kvno %d) "
1359 "entry for principal: %s.\n",
1365 (kt_entry
.vno
== kvno
) &&
1366 (kt_entry_enctype
!= enctype
))
1368 DEBUG(5, (__location__
": Saving entry with kvno [%d] "
1369 "enctype [%d] for principal: %s.\n",
1370 kvno
, kt_entry_enctype
, princ_s
));
1374 DEBUG(5, (__location__
": Found old entry for principal: %s "
1375 "(kvno %d) - trying to remove it.\n",
1376 princ_s
, kt_entry
.vno
));
1378 ret
= krb5_kt_end_seq_get(context
, keytab
, &cursor
);
1379 ZERO_STRUCT(cursor
);
1381 DEBUG(1, (__location__
": krb5_kt_end_seq_get() "
1382 "failed (%s)\n", error_message(ret
)));
1385 ret
= krb5_kt_remove_entry(context
, keytab
, &kt_entry
);
1387 DEBUG(1, (__location__
": krb5_kt_remove_entry() "
1388 "failed (%s)\n", error_message(ret
)));
1392 DEBUG(5, (__location__
": removed old entry for principal: "
1393 "%s (kvno %d).\n", princ_s
, kt_entry
.vno
));
1395 ret
= krb5_kt_start_seq_get(context
, keytab
, &cursor
);
1397 DEBUG(1, (__location__
": krb5_kt_start_seq() failed "
1398 "(%s)\n", error_message(ret
)));
1401 ret
= smb_krb5_kt_free_entry(context
, &kt_entry
);
1402 ZERO_STRUCT(kt_entry
);
1404 DEBUG(1, (__location__
": krb5_kt_remove_entry() "
1405 "failed (%s)\n", error_message(ret
)));
1411 talloc_free(tmp_ctx
);
1412 if (!all_zero((uint8_t *)&kt_entry
, sizeof(kt_entry
))) {
1413 smb_krb5_kt_free_entry(context
, &kt_entry
);
1415 if (!all_zero((uint8_t *)&cursor
, sizeof(cursor
))) {
1416 krb5_kt_end_seq_get(context
, keytab
, &cursor
);
1422 * @brief Add a keytab entry for the given principal
1424 * @param[in] context The krb5 context to use.
1426 * @param[in] keytab The keytab to add the entry to.
1428 * @param[in] kvno The kvno to use.
1430 * @param[in] princ_s The principal as a string.
1432 * @param[in] salt_principal The salt principal to salt the password with.
1433 * Only needed for keys which support salting.
1434 * If no salt is used set no_salt to false and
1437 * @param[in] enctype The encryption type of the keytab entry.
1439 * @param[in] password The password of the keytab entry.
1441 * @param[in] no_salt If the password should not be salted. Normally
1442 * this is only set to false for encryption types
1443 * which do not support salting like RC4.
1445 * @param[in] keep_old_entries Wether to keep or delte old keytab entries.
1447 * @retval 0 on Success
1449 * @return A corresponding KRB5 error code.
1451 * @see smb_krb5_kt_open()
1453 krb5_error_code
smb_krb5_kt_add_entry(krb5_context context
,
1456 const char *princ_s
,
1457 const char *salt_principal
,
1458 krb5_enctype enctype
,
1459 krb5_data
*password
,
1461 bool keep_old_entries
)
1463 krb5_error_code ret
;
1464 krb5_keytab_entry kt_entry
;
1465 krb5_principal princ
= NULL
;
1466 krb5_keyblock
*keyp
;
1468 ZERO_STRUCT(kt_entry
);
1470 ret
= smb_krb5_parse_name(context
, princ_s
, &princ
);
1472 DEBUG(1, (__location__
": smb_krb5_parse_name(%s) "
1473 "failed (%s)\n", princ_s
, error_message(ret
)));
1477 /* Seek and delete old keytab entries */
1478 ret
= smb_krb5_kt_seek_and_delete_old_entries(context
,
1490 /* If we get here, we have deleted all the old entries with kvno's
1491 * not equal to the current kvno-1. */
1493 keyp
= KRB5_KT_KEY(&kt_entry
);
1496 KRB5_KEY_DATA(keyp
) = (KRB5_KEY_DATA_CAST
*)SMB_MALLOC(password
->length
);
1497 if (KRB5_KEY_DATA(keyp
) == NULL
) {
1501 memcpy(KRB5_KEY_DATA(keyp
), password
->data
, password
->length
);
1502 KRB5_KEY_LENGTH(keyp
) = password
->length
;
1503 KRB5_KEY_TYPE(keyp
) = enctype
;
1505 krb5_principal salt_princ
= NULL
;
1507 /* Now add keytab entries for all encryption types */
1508 ret
= smb_krb5_parse_name(context
, salt_principal
, &salt_princ
);
1510 DBG_WARNING("krb5_parse_name(%s) failed (%s)\n",
1511 salt_principal
, error_message(ret
));
1515 ret
= smb_krb5_create_key_from_string(context
,
1521 krb5_free_principal(context
, salt_princ
);
1527 kt_entry
.principal
= princ
;
1528 kt_entry
.vno
= kvno
;
1530 DEBUG(3, (__location__
": adding keytab entry for (%s) with "
1531 "encryption type (%d) and version (%d)\n",
1532 princ_s
, enctype
, kt_entry
.vno
));
1533 ret
= krb5_kt_add_entry(context
, keytab
, &kt_entry
);
1534 krb5_free_keyblock_contents(context
, keyp
);
1535 ZERO_STRUCT(kt_entry
);
1537 DEBUG(1, (__location__
": adding entry to keytab "
1538 "failed (%s)\n", error_message(ret
)));
1544 krb5_free_principal(context
, princ
);
1550 #if defined(HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE) && \
1551 defined(HAVE_KRB5_GET_CREDS_OPT_ALLOC) && \
1552 defined(HAVE_KRB5_GET_CREDS)
1553 static krb5_error_code
smb_krb5_get_credentials_for_user_opt(krb5_context context
,
1556 krb5_principal server
,
1557 krb5_principal impersonate_princ
,
1558 krb5_creds
**out_creds
)
1560 krb5_error_code ret
;
1561 krb5_get_creds_opt opt
;
1563 ret
= krb5_get_creds_opt_alloc(context
, &opt
);
1567 krb5_get_creds_opt_add_options(context
, opt
, KRB5_GC_FORWARDABLE
);
1569 if (impersonate_princ
) {
1570 ret
= krb5_get_creds_opt_set_impersonate(context
, opt
,
1577 ret
= krb5_get_creds(context
, opt
, ccache
, server
, out_creds
);
1584 krb5_get_creds_opt_free(context
, opt
);
1588 #endif /* HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE */
1590 #ifdef HAVE_KRB5_GET_CREDENTIALS_FOR_USER
1592 #if !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER
1593 krb5_error_code KRB5_CALLCONV
1594 krb5_get_credentials_for_user(krb5_context context
, krb5_flags options
,
1595 krb5_ccache ccache
, krb5_creds
*in_creds
,
1596 krb5_data
*subject_cert
,
1597 krb5_creds
**out_creds
);
1598 #endif /* !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER */
1600 static krb5_error_code
smb_krb5_get_credentials_for_user(krb5_context context
,
1603 krb5_principal server
,
1604 krb5_principal impersonate_princ
,
1605 krb5_creds
**out_creds
)
1607 krb5_error_code ret
;
1608 krb5_creds in_creds
;
1610 ZERO_STRUCT(in_creds
);
1612 if (impersonate_princ
) {
1614 in_creds
.server
= me
;
1615 in_creds
.client
= impersonate_princ
;
1617 ret
= krb5_get_credentials_for_user(context
,
1618 0, /* krb5_flags options */
1621 NULL
, /* krb5_data *subject_cert */
1624 in_creds
.client
= me
;
1625 in_creds
.server
= server
;
1627 ret
= krb5_get_credentials(context
, 0, ccache
,
1628 &in_creds
, out_creds
);
1633 #endif /* HAVE_KRB5_GET_CREDENTIALS_FOR_USER */
1636 * smb_krb5_get_credentials
1638 * @brief Get krb5 credentials for a server
1640 * @param[in] context An initialized krb5_context
1641 * @param[in] ccache An initialized krb5_ccache
1642 * @param[in] me The krb5_principal of the caller
1643 * @param[in] server The krb5_principal of the requested service
1644 * @param[in] impersonate_princ The krb5_principal of a user to impersonate as (optional)
1645 * @param[out] out_creds The returned krb5_creds structure
1646 * @return krb5_error_code
1649 krb5_error_code
smb_krb5_get_credentials(krb5_context context
,
1652 krb5_principal server
,
1653 krb5_principal impersonate_princ
,
1654 krb5_creds
**out_creds
)
1656 krb5_error_code ret
;
1657 krb5_creds
*creds
= NULL
;
1659 if (out_creds
!= NULL
) {
1663 if (impersonate_princ
) {
1664 #ifdef HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE /* Heimdal */
1665 ret
= smb_krb5_get_credentials_for_user_opt(context
, ccache
, me
, server
, impersonate_princ
, &creds
);
1666 #elif defined(HAVE_KRB5_GET_CREDENTIALS_FOR_USER) /* MIT */
1667 ret
= smb_krb5_get_credentials_for_user(context
, ccache
, me
, server
, impersonate_princ
, &creds
);
1672 krb5_creds in_creds
;
1674 ZERO_STRUCT(in_creds
);
1676 in_creds
.client
= me
;
1677 in_creds
.server
= server
;
1679 ret
= krb5_get_credentials(context
, 0, ccache
,
1692 krb5_free_creds(context
, creds
);
1699 * @brief Initialize a krb5_keyblock with the given data.
1701 * Initialized a new keyblock, allocates the contents fo the key and
1702 * copies the data into the keyblock.
1704 * @param[in] context The library context
1706 * @param[in] enctype The encryption type.
1708 * @param[in] data The date to initialize the keyblock with.
1710 * @param[in] length The length of the keyblock.
1712 * @param[in] key Newly allocated keyblock structure.
1714 * The key date must be freed using krb5_free_keyblock_contents() when it is
1717 * @return 0 on success, a Kerberos error code otherwise.
1719 krb5_error_code
smb_krb5_keyblock_init_contents(krb5_context context
,
1720 krb5_enctype enctype
,
1725 #if defined(HAVE_KRB5_KEYBLOCK_INIT)
1726 return krb5_keyblock_init(context
, enctype
, data
, length
, key
);
1728 memset(key
, 0, sizeof(krb5_keyblock
));
1729 KRB5_KEY_DATA(key
) = SMB_MALLOC(length
);
1730 if (NULL
== KRB5_KEY_DATA(key
)) {
1733 memcpy(KRB5_KEY_DATA(key
), data
, length
);
1734 KRB5_KEY_LENGTH(key
) = length
;
1735 KRB5_KEY_TYPE(key
) = enctype
;
1741 * @brief Simulate a kinit by putting the tgt in the given credential cache.
1743 * This function uses a keyblock rather than needingthe original password.
1745 * @param[in] ctx The library context
1747 * @param[in] cc The credential cache to put the tgt in.
1749 * @param[in] principal The client princial
1751 * @param[in] keyblock The keyblock to use.
1753 * @param[in] target_service The service name of the initial credentials (or NULL).
1755 * @param[in] krb_options Initial credential options.
1757 * @param[in] expire_time A pointer to store the experation time of the
1758 * credentials (or NULL).
1760 * @param[in] kdc_time A pointer to store the time when the ticket becomes
1763 * @return 0 on success, a Kerberos error code otherwise.
1765 krb5_error_code
smb_krb5_kinit_keyblock_ccache(krb5_context ctx
,
1767 krb5_principal principal
,
1768 krb5_keyblock
*keyblock
,
1769 const char *target_service
,
1770 krb5_get_init_creds_opt
*krb_options
,
1771 time_t *expire_time
,
1774 krb5_error_code code
= 0;
1775 krb5_creds my_creds
;
1777 #if defined(HAVE_KRB5_GET_INIT_CREDS_KEYBLOCK)
1778 code
= krb5_get_init_creds_keyblock(ctx
, &my_creds
, principal
,
1779 keyblock
, 0, target_service
,
1781 #elif defined(HAVE_KRB5_GET_INIT_CREDS_KEYTAB)
1783 #define SMB_CREDS_KEYTAB "MEMORY:tmp_smb_creds_XXXXXX"
1784 char tmp_name
[sizeof(SMB_CREDS_KEYTAB
)];
1785 krb5_keytab_entry entry
;
1789 memset(&entry
, 0, sizeof(entry
));
1790 entry
.principal
= principal
;
1791 *(KRB5_KT_KEY(&entry
)) = *keyblock
;
1793 memcpy(tmp_name
, SMB_CREDS_KEYTAB
, sizeof(SMB_CREDS_KEYTAB
));
1794 mask
= umask(S_IRWXO
| S_IRWXG
);
1797 if (tmp_name
[0] == 0) {
1798 return KRB5_KT_BADNAME
;
1800 code
= krb5_kt_resolve(ctx
, tmp_name
, &keytab
);
1805 code
= krb5_kt_add_entry(ctx
, keytab
, &entry
);
1807 (void)krb5_kt_close(ctx
, keytab
);
1811 code
= krb5_get_init_creds_keytab(ctx
, &my_creds
, principal
,
1812 keytab
, 0, target_service
,
1814 (void)krb5_kt_close(ctx
, keytab
);
1817 #error krb5_get_init_creds_keyblock not available!
1823 #ifndef SAMBA4_USES_HEIMDAL /* MIT */
1825 * We need to store the principal as returned from the KDC to the
1826 * credentials cache. If we don't do that the KRB5 library is not
1827 * able to find the tickets it is looking for
1829 principal
= my_creds
.client
;
1831 code
= krb5_cc_initialize(ctx
, cc
, principal
);
1836 code
= krb5_cc_store_cred(ctx
, cc
, &my_creds
);
1842 *expire_time
= (time_t) my_creds
.times
.endtime
;
1846 *kdc_time
= (time_t) my_creds
.times
.starttime
;
1851 krb5_free_cred_contents(ctx
, &my_creds
);
1856 * @brief Simulate a kinit by putting the tgt in the given credential cache.
1858 * @param[in] ctx The library context
1860 * @param[in] cc The credential cache to put the tgt in.
1862 * @param[in] principal The client princial
1864 * @param[in] password The password (or NULL).
1866 * @param[in] target_service The service name of the initial credentials (or NULL).
1868 * @param[in] krb_options Initial credential options.
1870 * @param[in] expire_time A pointer to store the experation time of the
1871 * credentials (or NULL).
1873 * @param[in] kdc_time A pointer to store the time when the ticket becomes
1876 * @return 0 on success, a Kerberos error code otherwise.
1878 krb5_error_code
smb_krb5_kinit_password_ccache(krb5_context ctx
,
1880 krb5_principal principal
,
1881 const char *password
,
1882 const char *target_service
,
1883 krb5_get_init_creds_opt
*krb_options
,
1884 time_t *expire_time
,
1887 krb5_error_code code
= 0;
1888 krb5_creds my_creds
;
1890 code
= krb5_get_init_creds_password(ctx
, &my_creds
, principal
,
1891 password
, NULL
, NULL
, 0,
1892 target_service
, krb_options
);
1897 #ifndef SAMBA4_USES_HEIMDAL /* MIT */
1899 * We need to store the principal as returned from the KDC to the
1900 * credentials cache. If we don't do that the KRB5 library is not
1901 * able to find the tickets it is looking for
1903 principal
= my_creds
.client
;
1905 code
= krb5_cc_initialize(ctx
, cc
, principal
);
1910 code
= krb5_cc_store_cred(ctx
, cc
, &my_creds
);
1916 *expire_time
= (time_t) my_creds
.times
.endtime
;
1920 *kdc_time
= (time_t) my_creds
.times
.starttime
;
1925 krb5_free_cred_contents(ctx
, &my_creds
);
1929 #ifdef SAMBA4_USES_HEIMDAL
1931 * @brief Simulate a kinit by putting the tgt in the given credential cache.
1933 * @param[in] ctx The library context
1935 * @param[in] cc The credential cache to store the tgt in.
1937 * @param[in] principal The initial client princial.
1939 * @param[in] password The password (or NULL).
1941 * @param[in] impersonate_principal The impersonatiion principal (or NULL).
1943 * @param[in] self_service The local service for S4U2Self if
1944 * impersonate_principal is specified).
1946 * @param[in] target_service The service name of the initial credentials
1947 * (kpasswd/REALM or a remote service). It defaults
1948 * to the krbtgt if NULL.
1950 * @param[in] krb_options Initial credential options.
1952 * @param[in] expire_time A pointer to store the experation time of the
1953 * credentials (or NULL).
1955 * @param[in] kdc_time A pointer to store the time when the ticket becomes
1958 * @return 0 on success, a Kerberos error code otherwise.
1960 krb5_error_code
smb_krb5_kinit_s4u2_ccache(krb5_context ctx
,
1961 krb5_ccache store_cc
,
1962 krb5_principal init_principal
,
1963 const char *init_password
,
1964 krb5_principal impersonate_principal
,
1965 const char *self_service
,
1966 const char *target_service
,
1967 krb5_get_init_creds_opt
*krb_options
,
1968 time_t *expire_time
,
1971 krb5_error_code code
= 0;
1972 krb5_get_creds_opt options
;
1973 krb5_principal store_principal
;
1974 krb5_creds store_creds
;
1975 krb5_creds
*s4u2self_creds
;
1976 Ticket s4u2self_ticket
;
1977 size_t s4u2self_ticketlen
;
1978 krb5_creds
*s4u2proxy_creds
;
1979 krb5_principal self_princ
;
1981 krb5_principal target_princ
;
1983 const char *self_realm
;
1984 const char *client_realm
= NULL
;
1985 krb5_principal blacklist_principal
= NULL
;
1986 krb5_principal whitelist_principal
= NULL
;
1988 code
= krb5_get_init_creds_password(ctx
, &store_creds
,
1999 store_principal
= init_principal
;
2002 * We are trying S4U2Self now:
2004 * As we do not want to expose our TGT in the
2005 * krb5_ccache, which is also holds the impersonated creds.
2007 * Some low level krb5/gssapi function might use the TGT
2008 * identity and let the client act as our machine account.
2010 * We need to avoid that and use a temporary krb5_ccache
2011 * in order to pass our TGT to the krb5_get_creds() function.
2013 code
= krb5_cc_new_unique(ctx
, NULL
, NULL
, &tmp_cc
);
2015 krb5_free_cred_contents(ctx
, &store_creds
);
2019 code
= krb5_cc_initialize(ctx
, tmp_cc
, store_creds
.client
);
2021 krb5_cc_destroy(ctx
, tmp_cc
);
2022 krb5_free_cred_contents(ctx
, &store_creds
);
2026 code
= krb5_cc_store_cred(ctx
, tmp_cc
, &store_creds
);
2028 krb5_free_cred_contents(ctx
, &store_creds
);
2029 krb5_cc_destroy(ctx
, tmp_cc
);
2034 * we need to remember the client principal of our
2035 * TGT and make sure the KDC does not return this
2036 * in the impersonated tickets. This can happen
2037 * if the KDC does not support S4U2Self and S4U2Proxy.
2039 blacklist_principal
= store_creds
.client
;
2040 store_creds
.client
= NULL
;
2041 krb5_free_cred_contents(ctx
, &store_creds
);
2044 * Check if we also need S4U2Proxy or if S4U2Self is
2045 * enough in order to get a ticket for the target.
2047 if (target_service
== NULL
) {
2049 } else if (strcmp(target_service
, self_service
) == 0) {
2056 * For S4U2Self we need our own service principal,
2057 * which belongs to our own realm (available on
2058 * our client principal).
2060 self_realm
= krb5_principal_get_realm(ctx
, init_principal
);
2062 code
= krb5_parse_name(ctx
, self_service
, &self_princ
);
2064 krb5_free_principal(ctx
, blacklist_principal
);
2065 krb5_cc_destroy(ctx
, tmp_cc
);
2069 code
= krb5_principal_set_realm(ctx
, self_princ
, self_realm
);
2071 krb5_free_principal(ctx
, blacklist_principal
);
2072 krb5_free_principal(ctx
, self_princ
);
2073 krb5_cc_destroy(ctx
, tmp_cc
);
2077 code
= krb5_get_creds_opt_alloc(ctx
, &options
);
2079 krb5_free_principal(ctx
, blacklist_principal
);
2080 krb5_free_principal(ctx
, self_princ
);
2081 krb5_cc_destroy(ctx
, tmp_cc
);
2087 * If we want S4U2Proxy, we need the forwardable flag
2088 * on the S4U2Self ticket.
2090 krb5_get_creds_opt_set_options(ctx
, options
, KRB5_GC_FORWARDABLE
);
2093 code
= krb5_get_creds_opt_set_impersonate(ctx
, options
,
2094 impersonate_principal
);
2096 krb5_get_creds_opt_free(ctx
, options
);
2097 krb5_free_principal(ctx
, blacklist_principal
);
2098 krb5_free_principal(ctx
, self_princ
);
2099 krb5_cc_destroy(ctx
, tmp_cc
);
2103 code
= krb5_get_creds(ctx
, options
, tmp_cc
,
2104 self_princ
, &s4u2self_creds
);
2105 krb5_get_creds_opt_free(ctx
, options
);
2106 krb5_free_principal(ctx
, self_princ
);
2108 krb5_free_principal(ctx
, blacklist_principal
);
2109 krb5_cc_destroy(ctx
, tmp_cc
);
2114 krb5_cc_destroy(ctx
, tmp_cc
);
2117 * Now make sure we store the impersonated principal
2118 * and creds instead of the TGT related stuff
2119 * in the krb5_ccache of the caller.
2121 code
= krb5_copy_creds_contents(ctx
, s4u2self_creds
,
2123 krb5_free_creds(ctx
, s4u2self_creds
);
2129 * It's important to store the principal the KDC
2130 * returned, as otherwise the caller would not find
2131 * the S4U2Self ticket in the krb5_ccache lookup.
2133 store_principal
= store_creds
.client
;
2138 * We are trying S4U2Proxy:
2140 * We need the ticket from the S4U2Self step
2141 * and our TGT in order to get the delegated ticket.
2143 code
= decode_Ticket((const uint8_t *)s4u2self_creds
->ticket
.data
,
2144 s4u2self_creds
->ticket
.length
,
2146 &s4u2self_ticketlen
);
2148 krb5_free_creds(ctx
, s4u2self_creds
);
2149 krb5_free_principal(ctx
, blacklist_principal
);
2150 krb5_cc_destroy(ctx
, tmp_cc
);
2155 * we need to remember the client principal of the
2156 * S4U2Self stage and as it needs to match the one we
2157 * will get for the S4U2Proxy stage. We need this
2158 * in order to detect KDCs which does not support S4U2Proxy.
2160 whitelist_principal
= s4u2self_creds
->client
;
2161 s4u2self_creds
->client
= NULL
;
2162 krb5_free_creds(ctx
, s4u2self_creds
);
2165 * For S4U2Proxy we also got a target service principal,
2166 * which also belongs to our own realm (available on
2167 * our client principal).
2169 code
= krb5_parse_name(ctx
, target_service
, &target_princ
);
2171 free_Ticket(&s4u2self_ticket
);
2172 krb5_free_principal(ctx
, whitelist_principal
);
2173 krb5_free_principal(ctx
, blacklist_principal
);
2174 krb5_cc_destroy(ctx
, tmp_cc
);
2178 code
= krb5_principal_set_realm(ctx
, target_princ
, self_realm
);
2180 free_Ticket(&s4u2self_ticket
);
2181 krb5_free_principal(ctx
, target_princ
);
2182 krb5_free_principal(ctx
, whitelist_principal
);
2183 krb5_free_principal(ctx
, blacklist_principal
);
2184 krb5_cc_destroy(ctx
, tmp_cc
);
2188 code
= krb5_get_creds_opt_alloc(ctx
, &options
);
2190 free_Ticket(&s4u2self_ticket
);
2191 krb5_free_principal(ctx
, target_princ
);
2192 krb5_free_principal(ctx
, whitelist_principal
);
2193 krb5_free_principal(ctx
, blacklist_principal
);
2194 krb5_cc_destroy(ctx
, tmp_cc
);
2198 krb5_get_creds_opt_set_options(ctx
, options
, KRB5_GC_FORWARDABLE
);
2199 krb5_get_creds_opt_set_options(ctx
, options
, KRB5_GC_CONSTRAINED_DELEGATION
);
2201 code
= krb5_get_creds_opt_set_ticket(ctx
, options
, &s4u2self_ticket
);
2202 free_Ticket(&s4u2self_ticket
);
2204 krb5_get_creds_opt_free(ctx
, options
);
2205 krb5_free_principal(ctx
, target_princ
);
2206 krb5_free_principal(ctx
, whitelist_principal
);
2207 krb5_free_principal(ctx
, blacklist_principal
);
2208 krb5_cc_destroy(ctx
, tmp_cc
);
2212 code
= krb5_get_creds(ctx
, options
, tmp_cc
,
2213 target_princ
, &s4u2proxy_creds
);
2214 krb5_get_creds_opt_free(ctx
, options
);
2215 krb5_free_principal(ctx
, target_princ
);
2216 krb5_cc_destroy(ctx
, tmp_cc
);
2218 krb5_free_principal(ctx
, whitelist_principal
);
2219 krb5_free_principal(ctx
, blacklist_principal
);
2224 * Now make sure we store the impersonated principal
2225 * and creds instead of the TGT related stuff
2226 * in the krb5_ccache of the caller.
2228 code
= krb5_copy_creds_contents(ctx
, s4u2proxy_creds
,
2230 krb5_free_creds(ctx
, s4u2proxy_creds
);
2232 krb5_free_principal(ctx
, whitelist_principal
);
2233 krb5_free_principal(ctx
, blacklist_principal
);
2238 * It's important to store the principal the KDC
2239 * returned, as otherwise the caller would not find
2240 * the S4U2Self ticket in the krb5_ccache lookup.
2242 store_principal
= store_creds
.client
;
2245 if (blacklist_principal
&&
2246 krb5_principal_compare(ctx
, store_creds
.client
, blacklist_principal
)) {
2250 code
= krb5_unparse_name(ctx
, blacklist_principal
, &sp
);
2254 code
= krb5_unparse_name(ctx
, impersonate_principal
, &ip
);
2258 DEBUG(1, ("smb_krb5_kinit_password_cache: "
2259 "KDC returned self principal[%s] while impersonating [%s]\n",
2260 sp
?sp
:"<no memory>",
2261 ip
?ip
:"<no memory>"));
2266 krb5_free_principal(ctx
, whitelist_principal
);
2267 krb5_free_principal(ctx
, blacklist_principal
);
2268 krb5_free_cred_contents(ctx
, &store_creds
);
2269 return KRB5_FWD_BAD_PRINCIPAL
;
2271 if (blacklist_principal
) {
2272 krb5_free_principal(ctx
, blacklist_principal
);
2275 if (whitelist_principal
&&
2276 !krb5_principal_compare(ctx
, store_creds
.client
, whitelist_principal
)) {
2280 code
= krb5_unparse_name(ctx
, store_creds
.client
, &sp
);
2284 code
= krb5_unparse_name(ctx
, whitelist_principal
, &ep
);
2288 DEBUG(1, ("smb_krb5_kinit_password_cache: "
2289 "KDC returned wrong principal[%s] we expected [%s]\n",
2290 sp
?sp
:"<no memory>",
2291 ep
?ep
:"<no memory>"));
2296 krb5_free_principal(ctx
, whitelist_principal
);
2297 krb5_free_cred_contents(ctx
, &store_creds
);
2298 return KRB5_FWD_BAD_PRINCIPAL
;
2300 if (whitelist_principal
) {
2301 krb5_free_principal(ctx
, whitelist_principal
);
2304 code
= krb5_cc_initialize(ctx
, store_cc
, store_principal
);
2306 krb5_free_cred_contents(ctx
, &store_creds
);
2310 code
= krb5_cc_store_cred(ctx
, store_cc
, &store_creds
);
2312 krb5_free_cred_contents(ctx
, &store_creds
);
2316 client_realm
= krb5_principal_get_realm(ctx
, store_creds
.client
);
2317 if (client_realm
!= NULL
) {
2319 * Because the CANON flag doesn't have any impact
2320 * on the impersonate_principal => store_creds.client
2321 * realm mapping. We need to store the credentials twice,
2322 * once with the returned realm and once with the
2323 * realm of impersonate_principal.
2325 code
= krb5_principal_set_realm(ctx
, store_creds
.server
,
2328 krb5_free_cred_contents(ctx
, &store_creds
);
2332 code
= krb5_cc_store_cred(ctx
, store_cc
, &store_creds
);
2334 krb5_free_cred_contents(ctx
, &store_creds
);
2340 *expire_time
= (time_t) store_creds
.times
.endtime
;
2344 *kdc_time
= (time_t) store_creds
.times
.starttime
;
2347 krb5_free_cred_contents(ctx
, &store_creds
);
2353 #if !defined(HAVE_KRB5_MAKE_PRINCIPAL) && defined(HAVE_KRB5_BUILD_PRINCIPAL_ALLOC_VA)
2355 * @brief Create a principal name using a variable argument list.
2357 * @param[in] context The library context.
2359 * @param[inout] principal A pointer to the principal structure.
2361 * @param[in] _realm The realm to use. If NULL then the function will
2362 * get the default realm name.
2364 * @param[in] ... A list of 'char *' components, ending with NULL.
2366 * Use krb5_free_principal() to free the principal when it is no longer needed.
2368 * @return 0 on success, a Kerberos error code otherwise.
2370 krb5_error_code
smb_krb5_make_principal(krb5_context context
,
2371 krb5_principal
*principal
,
2372 const char *_realm
, ...)
2374 krb5_error_code code
;
2380 realm
= discard_const_p(char, _realm
);
2383 code
= krb5_get_default_realm(context
, &realm
);
2390 va_start(ap
, _realm
);
2391 code
= krb5_build_principal_alloc_va(context
, principal
,
2392 strlen(realm
), realm
,
2397 krb5_free_default_realm(context
, realm
);
2404 #if !defined(HAVE_KRB5_CC_GET_LIFETIME) && defined(HAVE_KRB5_CC_RETRIEVE_CRED)
2406 * @brief Get the lifetime of the initial ticket in the cache.
2408 * @param[in] context The kerberos context.
2410 * @param[in] id The credential cache to get the ticket lifetime.
2412 * @param[out] t A pointer to a time value to store the lifetime.
2414 * @return 0 on success, a krb5_error_code on error.
2416 krb5_error_code
smb_krb5_cc_get_lifetime(krb5_context context
,
2420 krb5_cc_cursor cursor
;
2421 krb5_error_code kerr
;
2427 kerr
= krb5_timeofday(context
, &now
);
2432 kerr
= krb5_cc_start_seq_get(context
, id
, &cursor
);
2437 while ((kerr
= krb5_cc_next_cred(context
, id
, &cursor
, &cred
)) == 0) {
2438 #ifndef HAVE_FLAGS_IN_KRB5_CREDS
2439 if (cred
.ticket_flags
& TKT_FLG_INITIAL
) {
2441 if (cred
.flags
.b
.initial
) {
2443 if (now
< cred
.times
.endtime
) {
2444 *t
= (time_t) (cred
.times
.endtime
- now
);
2446 krb5_free_cred_contents(context
, &cred
);
2449 krb5_free_cred_contents(context
, &cred
);
2452 krb5_cc_end_seq_get(context
, id
, &cursor
);
2456 #endif /* HAVE_KRB5_CC_GET_LIFETIME */
2458 #if !defined(HAVE_KRB5_FREE_CHECKSUM_CONTENTS) && defined(HAVE_FREE_CHECKSUM)
2459 void smb_krb5_free_checksum_contents(krb5_context ctx
, krb5_checksum
*cksum
)
2461 free_Checksum(cksum
);
2466 * @brief Compute a checksum operating on a keyblock.
2468 * This function computes a checksum over a PAC using the keyblock for a keyed
2471 * @param[in] mem_ctx A talloc context to alocate the signature on.
2473 * @param[in] pac_data The PAC as input.
2475 * @param[in] context The library context.
2477 * @param[in] keyblock Encryption key for a keyed checksum.
2479 * @param[out] sig_type The checksum type
2481 * @param[out] sig_blob The talloc'ed checksum
2483 * The caller must free the sig_blob with talloc_free() when it is not needed
2486 * @return 0 on success, a Kerberos error code otherwise.
2488 krb5_error_code
smb_krb5_make_pac_checksum(TALLOC_CTX
*mem_ctx
,
2489 DATA_BLOB
*pac_data
,
2490 krb5_context context
,
2491 const krb5_keyblock
*keyblock
,
2493 DATA_BLOB
*sig_blob
)
2495 krb5_error_code ret
;
2496 krb5_checksum cksum
;
2497 #if defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CREATE_CHECKSUM)
2501 ret
= krb5_crypto_init(context
,
2506 DEBUG(0,("krb5_crypto_init() failed: %s\n",
2507 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
2510 ret
= krb5_create_checksum(context
,
2512 KRB5_KU_OTHER_CKSUM
,
2518 DEBUG(2, ("PAC Verification failed: %s\n",
2519 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
2522 krb5_crypto_destroy(context
, crypto
);
2528 *sig_type
= cksum
.cksumtype
;
2529 *sig_blob
= data_blob_talloc(mem_ctx
,
2530 cksum
.checksum
.data
,
2531 cksum
.checksum
.length
);
2532 #elif defined(HAVE_KRB5_C_MAKE_CHECKSUM)
2535 input
.data
= (char *)pac_data
->data
;
2536 input
.length
= pac_data
->length
;
2538 ret
= krb5_c_make_checksum(context
,
2541 KRB5_KEYUSAGE_APP_DATA_CKSUM
,
2545 DEBUG(2, ("PAC Verification failed: %s\n",
2546 smb_get_krb5_error_message(context
, ret
, mem_ctx
)));
2550 *sig_type
= cksum
.checksum_type
;
2551 *sig_blob
= data_blob_talloc(mem_ctx
,
2556 #error krb5_create_checksum or krb5_c_make_checksum not available
2557 #endif /* HAVE_KRB5_C_MAKE_CHECKSUM */
2558 smb_krb5_free_checksum_contents(context
, &cksum
);
2565 * @brief Get realm of a principal
2567 * @param[in] context The library context
2569 * @param[in] principal The principal to get the realm from.
2571 * @return An allocated string with the realm or NULL if an error occurred.
2573 * The caller must free the realm string with free() if not needed anymore.
2575 char *smb_krb5_principal_get_realm(krb5_context context
,
2576 krb5_const_principal principal
)
2578 #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
2579 return strdup(discard_const_p(char, krb5_principal_get_realm(context
, principal
)));
2580 #elif defined(krb5_princ_realm) /* MIT */
2582 realm
= discard_const_p(krb5_data
,
2583 krb5_princ_realm(context
, principal
));
2584 return strndup(realm
->data
, realm
->length
);
2586 #error UNKNOWN_GET_PRINC_REALM_FUNCTIONS
2591 * @brief Get realm of a principal
2593 * @param[in] context The library context
2595 * @param[in] principal The principal to set the realm
2597 * @param[in] realm The realm as a string to set.
2599 * @retur 0 on success, a Kerberos error code otherwise.
2601 krb5_error_code
smb_krb5_principal_set_realm(krb5_context context
,
2602 krb5_principal principal
,
2605 #ifdef HAVE_KRB5_PRINCIPAL_SET_REALM /* Heimdal */
2606 return krb5_principal_set_realm(context
, principal
, realm
);
2607 #elif defined(krb5_princ_realm) && defined(krb5_princ_set_realm) /* MIT */
2608 krb5_error_code ret
;
2610 krb5_data
*old_data
;
2612 old_data
= krb5_princ_realm(context
, principal
);
2614 ret
= smb_krb5_copy_data_contents(&data
,
2621 /* free realm before setting */
2622 free(old_data
->data
);
2624 krb5_princ_set_realm(context
, principal
, &data
);
2628 #error UNKNOWN_PRINC_SET_REALM_FUNCTION
2634 * @brief Get the realm from the service hostname.
2636 * This function will look for a domain realm mapping in the [domain_realm]
2637 * section of the krb5.conf first and fallback to extract the realm from
2638 * the provided service hostname. As a last resort it will return the
2639 * provided client_realm.
2641 * @param[in] mem_ctx The talloc context
2643 * @param[in] hostname The service hostname
2645 * @param[in] client_realm If we can not find a mapping, fall back to
2648 * @return The realm to use for the service hostname, NULL if a fatal error
2651 char *smb_krb5_get_realm_from_hostname(TALLOC_CTX
*mem_ctx
,
2652 const char *hostname
,
2653 const char *client_realm
)
2655 #if defined(HAVE_KRB5_REALM_TYPE)
2657 krb5_realm
*realm_list
= NULL
;
2660 char **realm_list
= NULL
;
2663 krb5_error_code kerr
;
2664 krb5_context ctx
= NULL
;
2666 initialize_krb5_error_table();
2667 if (krb5_init_context(&ctx
)) {
2671 kerr
= krb5_get_host_realm(ctx
, hostname
, &realm_list
);
2673 DEBUG(3,("kerberos_get_realm_from_hostname %s: "
2675 hostname
? hostname
: "(NULL)",
2676 error_message(kerr
) ));
2680 if (realm_list
!= NULL
&&
2681 realm_list
[0] != NULL
&&
2682 realm_list
[0][0] != '\0') {
2683 realm
= talloc_strdup(mem_ctx
, realm_list
[0]);
2684 if (realm
== NULL
) {
2688 const char *p
= NULL
;
2691 * "dc6.samba2003.example.com"
2692 * returns a realm of "SAMBA2003.EXAMPLE.COM"
2694 * "dc6." returns realm as NULL
2696 p
= strchr_m(hostname
, '.');
2697 if (p
!= NULL
&& p
[1] != '\0') {
2698 realm
= talloc_strdup_upper(mem_ctx
, p
+ 1);
2699 if (realm
== NULL
) {
2705 if (realm
== NULL
) {
2706 realm
= talloc_strdup(mem_ctx
, client_realm
);
2713 krb5_free_host_realm(ctx
, realm_list
);
2716 krb5_free_context(ctx
);
2723 * @brief Get an error string from a Kerberos error code.
2725 * @param[in] context The library context.
2727 * @param[in] code The Kerberos error code.
2729 * @param[in] mem_ctx The talloc context to allocate the error string on.
2731 * @return A talloc'ed error string or NULL if an error occurred.
2733 * The caller must free the returned error string with talloc_free() if not
2736 char *smb_get_krb5_error_message(krb5_context context
,
2737 krb5_error_code code
,
2738 TALLOC_CTX
*mem_ctx
)
2742 #if defined(HAVE_KRB5_GET_ERROR_MESSAGE) && defined(HAVE_KRB5_FREE_ERROR_MESSAGE)
2743 const char *context_error
= krb5_get_error_message(context
, code
);
2744 if (context_error
) {
2745 ret
= talloc_asprintf(mem_ctx
, "%s: %s",
2746 error_message(code
), context_error
);
2747 krb5_free_error_message(context
, context_error
);
2751 ret
= talloc_strdup(mem_ctx
, error_message(code
));
2757 * @brief Return the kerberos library setting for: libdefaults:allow_weak_crypto
2759 * @param[in] context The library context
2761 * @return True if weak crypto is allowed, false if not.
2763 krb5_boolean
smb_krb5_get_allowed_weak_crypto(krb5_context context
)
2764 #if defined(HAVE_KRB5_CONFIG_GET_BOOL_DEFAULT)
2766 return krb5_config_get_bool_default(context
,
2770 "allow_weak_crypto",
2773 #elif defined(HAVE_PROFILE_H) && defined(HAVE_KRB5_GET_PROFILE)
2775 #include <profile.h>
2776 krb5_error_code ret
;
2777 krb5_boolean ret_default
= false;
2781 ret
= krb5_get_profile(context
,
2787 ret
= profile_get_boolean(profile
,
2789 "allow_weak_crypto",
2790 NULL
, /* subsubname */
2791 ret_default
, /* def_val */
2792 &ret_profile
/* *ret_default */);
2797 profile_release(profile
);
2802 #error UNKNOWN_KRB5_CONFIG_ROUTINES
2806 * @brief Return the type of a krb5_principal
2808 * @param[in] context The library context.
2810 * @param[in] principal The principal to get the type from.
2812 * @return The integer type of the principal.
2814 int smb_krb5_principal_get_type(krb5_context context
,
2815 krb5_const_principal principal
)
2817 #ifdef HAVE_KRB5_PRINCIPAL_GET_TYPE /* Heimdal */
2818 return krb5_principal_get_type(context
, principal
);
2819 #elif defined(krb5_princ_type) /* MIT */
2820 return krb5_princ_type(context
, principal
);
2822 #error UNKNOWN_PRINC_GET_TYPE_FUNCTION
2827 * @brief Set the type of a principal
2829 * @param[in] context The library context
2831 * @param[inout] principal The principal to set the type for.
2833 * @param[in] type The principal type to set.
2835 void smb_krb5_principal_set_type(krb5_context context
,
2836 krb5_principal principal
,
2839 #ifdef HAVE_KRB5_PRINCIPAL_SET_TYPE /* Heimdal */
2840 krb5_principal_set_type(context
, principal
, type
);
2841 #elif defined(krb5_princ_type) /* MIT */
2842 krb5_princ_type(context
, principal
) = type
;
2844 #error UNKNOWN_PRINC_SET_TYPE_FUNCTION
2848 #if !defined(HAVE_KRB5_WARNX)
2850 * @brief Log a Kerberos message
2852 * It sends the message to com_err.
2854 * @param[in] context The library context
2856 * @param[in] fmt The message format
2858 * @param[in] ... The message arguments
2860 * @return 0 on success.
2862 krb5_error_code
krb5_warnx(krb5_context context
, const char *fmt
, ...)
2866 va_start(args
, fmt
);
2867 com_err_va("samba-kdc", errno
, fmt
, args
);
2875 * @brief Copy a credential cache.
2877 * @param[in] context The library context.
2879 * @param[in] incc Credential cache to be copied.
2881 * @param[inout] outcc Copy of credential cache to be filled in.
2883 * @return 0 on success, a Kerberos error code otherwise.
2885 krb5_error_code
smb_krb5_cc_copy_creds(krb5_context context
,
2886 krb5_ccache incc
, krb5_ccache outcc
)
2888 #ifdef HAVE_KRB5_CC_COPY_CACHE /* Heimdal */
2889 return krb5_cc_copy_cache(context
, incc
, outcc
);
2890 #elif defined(HAVE_KRB5_CC_COPY_CREDS)
2891 krb5_error_code ret
;
2892 krb5_principal princ
= NULL
;
2894 ret
= krb5_cc_get_principal(context
, incc
, &princ
);
2898 ret
= krb5_cc_initialize(context
, outcc
, princ
);
2899 krb5_free_principal(context
, princ
);
2903 return krb5_cc_copy_creds(context
, incc
, outcc
);
2905 #error UNKNOWN_KRB5_CC_COPY_CACHE_OR_CREDS_FUNCTION
2909 /**********************************************************
2911 **********************************************************/
2913 static bool ads_cleanup_expired_creds(krb5_context context
,
2917 krb5_error_code retval
;
2918 const char *cc_type
= krb5_cc_get_type(context
, ccache
);
2920 DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
2921 cc_type
, krb5_cc_get_name(context
, ccache
),
2922 http_timestring(talloc_tos(), credsp
->times
.endtime
)));
2924 /* we will probably need new tickets if the current ones
2925 will expire within 10 seconds.
2927 if (credsp
->times
.endtime
>= (time(NULL
) + 10))
2930 /* heimdal won't remove creds from a file ccache, and
2931 perhaps we shouldn't anyway, since internally we
2932 use memory ccaches, and a FILE one probably means that
2933 we're using creds obtained outside of our exectuable
2935 if (strequal(cc_type
, "FILE")) {
2936 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type
));
2940 retval
= krb5_cc_remove_cred(context
, ccache
, 0, credsp
);
2942 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
2943 error_message(retval
)));
2944 /* If we have an error in this, we want to display it,
2945 but continue as though we deleted it */
2950 /* Allocate and setup the auth context into the state we need. */
2952 static krb5_error_code
ads_setup_auth_context(krb5_context context
,
2953 krb5_auth_context
*auth_context
)
2955 krb5_error_code retval
;
2957 retval
= krb5_auth_con_init(context
, auth_context
);
2959 DEBUG(1,("krb5_auth_con_init failed (%s)\n",
2960 error_message(retval
)));
2964 /* Ensure this is an addressless ticket. */
2965 retval
= krb5_auth_con_setaddrs(context
, *auth_context
, NULL
, NULL
);
2967 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n",
2968 error_message(retval
)));
2974 #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)
2975 static krb5_error_code
ads_create_gss_checksum(krb5_data
*in_data
, /* [inout] */
2978 unsigned int orig_length
= in_data
->length
;
2979 unsigned int base_cksum_size
= GSSAPI_CHECKSUM_SIZE
;
2980 char *gss_cksum
= NULL
;
2983 /* Extra length field for delgated ticket. */
2984 base_cksum_size
+= 4;
2987 if ((unsigned int)base_cksum_size
+ orig_length
<
2988 (unsigned int)base_cksum_size
) {
2992 gss_cksum
= (char *)SMB_MALLOC(base_cksum_size
+ orig_length
);
2993 if (gss_cksum
== NULL
) {
2997 memset(gss_cksum
, '\0', base_cksum_size
+ orig_length
);
2998 SIVAL(gss_cksum
, 0, GSSAPI_BNDLENGTH
);
3001 * GSS_C_NO_CHANNEL_BINDINGS means 16 zero bytes.
3002 * This matches the behavior of heimdal and mit.
3004 * And it is needed to work against some closed source
3009 memset(&gss_cksum
[4], 0x00, GSSAPI_BNDLENGTH
);
3011 SIVAL(gss_cksum
, 20, gss_flags
);
3014 SSVAL(gss_cksum
, 24, 1); /* The Delegation Option identifier */
3015 SSVAL(gss_cksum
, 26, orig_length
);
3016 /* Copy the kerberos KRB_CRED data */
3017 memcpy(gss_cksum
+ 28, in_data
->data
, orig_length
);
3018 free(in_data
->data
);
3019 in_data
->data
= NULL
;
3020 in_data
->length
= 0;
3022 in_data
->data
= gss_cksum
;
3023 in_data
->length
= base_cksum_size
+ orig_length
;
3029 * We can't use krb5_mk_req because w2k wants the service to be in a particular
3032 static krb5_error_code
ads_krb5_mk_req(krb5_context context
,
3033 krb5_auth_context
*auth_context
,
3034 const krb5_flags ap_req_options
,
3035 const char *principal
,
3038 time_t *expire_time
,
3039 const char *impersonate_princ_s
)
3041 krb5_error_code retval
;
3042 krb5_principal server
;
3043 krb5_principal impersonate_princ
= NULL
;
3047 bool creds_ready
= false;
3048 int i
= 0, maxtries
= 3;
3051 ZERO_STRUCT(in_data
);
3053 retval
= smb_krb5_parse_name(context
, principal
, &server
);
3055 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal
));
3059 if (impersonate_princ_s
) {
3060 retval
= smb_krb5_parse_name(context
, impersonate_princ_s
,
3061 &impersonate_princ
);
3063 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", impersonate_princ_s
));
3068 /* obtain ticket & session key */
3070 if ((retval
= krb5_copy_principal(context
, server
, &creds
.server
))) {
3071 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n",
3072 error_message(retval
)));
3076 retval
= krb5_cc_get_principal(context
, ccache
, &creds
.client
);
3078 /* This can commonly fail on smbd startup with no ticket in the cache.
3079 * Report at higher level than 1. */
3080 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
3081 error_message(retval
)));
3085 while (!creds_ready
&& (i
< maxtries
)) {
3087 retval
= smb_krb5_get_credentials(context
,
3094 DBG_WARNING("smb_krb5_get_credentials failed for %s "
3097 error_message(retval
));
3101 /* cope with ticket being in the future due to clock skew */
3102 if ((unsigned)credsp
->times
.starttime
> time(NULL
)) {
3103 time_t t
= time(NULL
);
3104 int time_offset
=(int)((unsigned)credsp
->times
.starttime
-t
);
3105 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset
));
3106 krb5_set_real_time(context
, t
+ time_offset
+ 1, 0);
3109 ok
= ads_cleanup_expired_creds(context
, ccache
, credsp
);
3117 DBG_DEBUG("Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
3119 krb5_cc_get_type(context
, ccache
),
3120 krb5_cc_get_name(context
, ccache
),
3121 http_timestring(talloc_tos(),
3122 (unsigned)credsp
->times
.endtime
),
3123 (unsigned)credsp
->times
.endtime
);
3126 *expire_time
= (time_t)credsp
->times
.endtime
;
3129 /* Allocate the auth_context. */
3130 retval
= ads_setup_auth_context(context
, auth_context
);
3132 DBG_WARNING("ads_setup_auth_context failed (%s)\n",
3133 error_message(retval
));
3137 #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)
3139 uint32_t gss_flags
= 0;
3141 if (credsp
->ticket_flags
& TKT_FLG_OK_AS_DELEGATE
) {
3143 * Fetch a forwarded TGT from the KDC so that we can
3144 * hand off a 2nd ticket as part of the kerberos
3148 DBG_INFO("Server marked as OK to delegate to, building "
3149 "forwardable TGT\n");
3151 retval
= krb5_auth_con_setuseruserkey(context
,
3153 &credsp
->keyblock
);
3155 DBG_WARNING("krb5_auth_con_setuseruserkey "
3157 error_message(retval
));
3161 /* Must use a subkey for forwarded tickets. */
3162 retval
= krb5_auth_con_setflags(context
,
3164 KRB5_AUTH_CONTEXT_USE_SUBKEY
);
3166 DBG_WARNING("krb5_auth_con_setflags failed (%s)\n",
3167 error_message(retval
));
3171 retval
= krb5_fwd_tgt_creds(context
,/* Krb5 context [in] */
3172 *auth_context
, /* Authentication context [in] */
3173 discard_const_p(char, KRB5_TGS_NAME
), /* Ticket service name ("krbtgt") [in] */
3174 credsp
->client
, /* Client principal for the tgt [in] */
3175 credsp
->server
, /* Server principal for the tgt [in] */
3176 ccache
, /* Credential cache to use for storage [in] */
3177 1, /* Turn on for "Forwardable ticket" [in] */
3178 &in_data
); /* Resulting response [out] */
3181 DBG_INFO("krb5_fwd_tgt_creds failed (%s)\n",
3182 error_message(retval
));
3185 * This is not fatal. Delete the *auth_context and continue
3186 * with krb5_mk_req_extended to get a non-forwardable ticket.
3190 free( in_data
.data
);
3191 in_data
.data
= NULL
;
3194 krb5_auth_con_free(context
, *auth_context
);
3195 *auth_context
= NULL
;
3196 retval
= ads_setup_auth_context(context
, auth_context
);
3198 DBG_WARNING("ads_setup_auth_context failed (%s)\n",
3199 error_message(retval
));
3203 /* We got a delegated ticket. */
3204 gss_flags
|= GSS_C_DELEG_FLAG
;
3208 /* Frees and reallocates in_data into a GSS checksum blob. */
3209 retval
= ads_create_gss_checksum(&in_data
, gss_flags
);
3214 /* We always want GSS-checksum types. */
3215 retval
= krb5_auth_con_set_req_cksumtype(context
, *auth_context
, GSSAPI_CHECKSUM
);
3217 DEBUG(1,("krb5_auth_con_set_req_cksumtype failed (%s)\n",
3218 error_message(retval
)));
3224 retval
= krb5_mk_req_extended(context
, auth_context
, ap_req_options
,
3225 &in_data
, credsp
, outbuf
);
3227 DBG_WARNING("krb5_mk_req_extended failed (%s)\n",
3228 error_message(retval
));
3231 #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)
3236 free( in_data
.data
);
3240 krb5_free_creds(context
, credsp
);
3243 krb5_free_cred_contents(context
, &creds
);
3246 krb5_free_principal(context
, server
);
3247 if (impersonate_princ
) {
3248 krb5_free_principal(context
, impersonate_princ
);
3255 get a kerberos5 ticket for the given service
3257 int ads_krb5_cli_get_ticket(TALLOC_CTX
*mem_ctx
,
3258 const char *principal
,
3261 DATA_BLOB
*session_key_krb5
,
3262 uint32_t extra_ap_opts
, const char *ccname
,
3264 const char *impersonate_princ_s
)
3266 krb5_error_code retval
;
3268 krb5_context context
= NULL
;
3269 krb5_ccache ccdef
= NULL
;
3270 krb5_auth_context auth_context
= NULL
;
3271 krb5_enctype enc_types
[] = {
3272 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
3273 ENCTYPE_AES256_CTS_HMAC_SHA1_96
,
3275 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
3276 ENCTYPE_AES128_CTS_HMAC_SHA1_96
,
3278 ENCTYPE_ARCFOUR_HMAC
,
3279 ENCTYPE_DES_CBC_MD5
,
3280 ENCTYPE_DES_CBC_CRC
,
3284 initialize_krb5_error_table();
3285 retval
= krb5_init_context(&context
);
3287 DBG_WARNING("krb5_init_context failed (%s)\n",
3288 error_message(retval
));
3292 if (time_offset
!= 0) {
3293 krb5_set_real_time(context
, time(NULL
) + time_offset
, 0);
3296 retval
= krb5_cc_resolve(context
,
3297 ccname
? ccname
: krb5_cc_default_name(context
),
3300 DBG_WARNING("krb5_cc_default failed (%s)\n",
3301 error_message(retval
));
3305 retval
= krb5_set_default_tgs_ktypes(context
, enc_types
);
3307 DBG_WARNING("krb5_set_default_tgs_ktypes failed (%s)\n",
3308 error_message(retval
));
3312 retval
= ads_krb5_mk_req(context
,
3314 AP_OPTS_USE_SUBKEY
| (krb5_flags
)extra_ap_opts
,
3319 impersonate_princ_s
);
3324 ok
= smb_krb5_get_smb_session_key(mem_ctx
,
3334 *ticket
= data_blob_talloc(mem_ctx
, packet
.data
, packet
.length
);
3336 smb_krb5_free_data_contents(context
, &packet
);
3342 krb5_cc_close(context
, ccdef
);
3345 krb5_auth_con_free(context
, auth_context
);
3347 krb5_free_context(context
);
3353 #else /* HAVE_KRB5 */
3354 /* This saves a few linking headaches */
3355 int ads_krb5_cli_get_ticket(TALLOC_CTX
*mem_ctx
,
3356 const char *principal
,
3359 DATA_BLOB
*session_key_krb5
,
3360 uint32_t extra_ap_opts
, const char *ccname
,
3362 const char *impersonate_princ_s
)
3364 DEBUG(0,("NO KERBEROS SUPPORT\n"));
3368 #endif /* HAVE_KRB5 */