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"
29 #endif /* HAVE_COM_ERR_H */
31 #ifndef KRB5_AUTHDATA_WIN2K_PAC
32 #define KRB5_AUTHDATA_WIN2K_PAC 128
35 #ifndef KRB5_AUTHDATA_IF_RELEVANT
36 #define KRB5_AUTHDATA_IF_RELEVANT 1
41 #define GSSAPI_CHECKSUM 0x8003 /* Checksum type value for Kerberos */
42 #define GSSAPI_BNDLENGTH 16 /* Bind Length (rfc-1964 pg.3) */
43 #define GSSAPI_CHECKSUM_SIZE (4+GSSAPI_BNDLENGTH+4) /* Length of bind length,
44 bind field, flags field. */
45 #define GSS_C_DELEG_FLAG 1
47 /* MIT krb5 1.7beta3 (in Ubuntu Karmic) is missing the prototype,
48 but still has the symbol */
49 #if !HAVE_DECL_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE
50 krb5_error_code krb5_auth_con_set_req_cksumtype(
52 krb5_auth_context auth_context,
53 krb5_cksumtype cksumtype);
56 #if !defined(SMB_MALLOC)
58 #define SMB_MALLOC(s) malloc((s))
62 #define SMB_STRDUP(s) strdup(s)
65 /**********************************************************
67 **********************************************************/
69 #if !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES)
71 #if defined(HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES)
73 /* With MIT kerberos, we should use krb5_set_default_tgs_enctypes in preference
74 * to krb5_set_default_tgs_ktypes. See
75 * http://lists.samba.org/archive/samba-technical/2006-July/048271.html
77 * If the MIT libraries are not exporting internal symbols, we will end up in
78 * this branch, which is correct. Otherwise we will continue to use the
81 krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
83 return krb5_set_default_tgs_enctypes(ctx, enc);
86 #elif defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES)
89 krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc)
91 return krb5_set_default_in_tkt_etypes(ctx, enc);
94 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES */
96 #endif /* HAVE_KRB5_SET_DEFAULT_TGS_KTYPES */
99 #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY)
100 krb5_error_code krb5_auth_con_setuseruserkey(krb5_context context,
101 krb5_auth_context auth_context,
102 krb5_keyblock *keyblock)
104 return krb5_auth_con_setkey(context, auth_context, keyblock);
108 #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME)
109 void krb5_free_unparsed_name(krb5_context context, char *val)
115 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
116 const krb5_data *krb5_princ_component(krb5_context context,
117 krb5_principal principal, int i);
119 const krb5_data *krb5_princ_component(krb5_context context,
120 krb5_principal principal, int i)
122 static krb5_data kdata;
124 kdata.data = discard_const_p(char, krb5_principal_get_comp_string(context, principal, i));
125 kdata.length = strlen((const char *)kdata.data);
131 /**********************************************************
133 **********************************************************/
135 #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS)
139 * @brief Stores the address of a 'struct sockaddr_storage' a krb5_address
141 * @param[in] paddr A pointer to a 'struct sockaddr_storage to extract the
144 * @param[out] pkaddr A Kerberos address to store tha address in.
146 * @return True on success, false if an error occured.
148 bool smb_krb5_sockaddr_to_kaddr(struct sockaddr_storage *paddr,
149 krb5_address *pkaddr)
151 memset(pkaddr, '\0', sizeof(krb5_address));
152 #if defined(HAVE_IPV6) && defined(KRB5_ADDRESS_INET6)
153 if (paddr->ss_family == AF_INET6) {
154 pkaddr->addr_type = KRB5_ADDRESS_INET6;
155 pkaddr->address.length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr);
156 pkaddr->address.data = (char *)&(((struct sockaddr_in6 *)paddr)->sin6_addr);
160 if (paddr->ss_family == AF_INET) {
161 pkaddr->addr_type = KRB5_ADDRESS_INET;
162 pkaddr->address.length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
163 pkaddr->address.data = (char *)&(((struct sockaddr_in *)paddr)->sin_addr);
168 #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS)
172 * @brief Stores the address of a 'struct sockaddr_storage' a krb5_address
174 * @param[in] paddr A pointer to a 'struct sockaddr_storage to extract the
177 * @param[in] pkaddr A Kerberos address to store tha address in.
179 * @return True on success, false if an error occured.
181 bool smb_krb5_sockaddr_to_kaddr(struct sockaddr_storage *paddr,
182 krb5_address *pkaddr)
184 memset(pkaddr, '\0', sizeof(krb5_address));
185 #if defined(HAVE_IPV6) && defined(ADDRTYPE_INET6)
186 if (paddr->ss_family == AF_INET6) {
187 pkaddr->addrtype = ADDRTYPE_INET6;
188 pkaddr->length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr);
189 pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in6 *)paddr)->sin6_addr);
193 if (paddr->ss_family == AF_INET) {
194 pkaddr->addrtype = ADDRTYPE_INET;
195 pkaddr->length = sizeof(((struct sockaddr_in *)paddr)->sin_addr);
196 pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in *)paddr)->sin_addr);
202 #error UNKNOWN_ADDRTYPE
205 krb5_error_code smb_krb5_mk_error(krb5_context context,
206 krb5_error_code error_code,
211 krb5_error_code code = EINVAL;
212 #ifdef SAMBA4_USES_HEIMDAL
213 code = krb5_mk_error(context,
219 NULL, /* client_time */
220 NULL, /* client_usec */
223 krb5_error dec_err = {
227 if (e_text != NULL) {
228 dec_err.text.length = strlen(e_text);
229 dec_err.text.data = discard_const_p(char, e_text);
231 if (e_data != NULL) {
232 dec_err.e_data = *e_data;
235 code = krb5_mk_error(context,
243 * @brief Create a keyblock based on input parameters
245 * @param context The krb5_context
246 * @param host_princ The krb5_principal to use
247 * @param salt The optional salt, if omitted, salt is calculated with
248 * the provided principal.
249 * @param password The krb5_data containing the password
250 * @param enctype The krb5_enctype to use for the keyblock generation
251 * @param key The returned krb5_keyblock, caller needs to free with
252 * krb5_free_keyblock().
254 * @return krb5_error_code
256 int smb_krb5_create_key_from_string(krb5_context context,
257 krb5_const_principal host_princ,
260 krb5_enctype enctype,
265 if (host_princ == NULL && salt == NULL) {
269 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_C_STRING_TO_KEY)
274 ret = krb5_principal2salt(context, host_princ, &_salt);
276 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
282 ret = krb5_c_string_to_key(context, enctype, password, &_salt, key);
284 SAFE_FREE(_salt.data);
287 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
292 ret = krb5_get_pw_salt(context, host_princ, &_salt);
294 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret)));
298 _salt.saltvalue = *salt;
299 _salt.salttype = KRB5_PW_SALT;
302 ret = krb5_string_to_key_salt(context, enctype, (const char *)password->data, _salt, key);
304 krb5_free_salt(context, _salt);
308 #error UNKNOWN_CREATE_KEY_FUNCTIONS
314 * @brief Create a salt for a given principal
316 * @param context The initialized krb5_context
317 * @param host_princ The krb5_principal to create the salt for
318 * @param psalt A pointer to a krb5_data struct
320 * caller has to free the contents of psalt with smb_krb5_free_data_contents
321 * when function has succeeded
323 * @return krb5_error_code, returns 0 on success, error code otherwise
326 int smb_krb5_get_pw_salt(krb5_context context,
327 krb5_const_principal host_princ,
329 #if defined(HAVE_KRB5_GET_PW_SALT)
335 ret = krb5_get_pw_salt(context, host_princ, &salt);
340 psalt->data = salt.saltvalue.data;
341 psalt->length = salt.saltvalue.length;
345 #elif defined(HAVE_KRB5_PRINCIPAL2SALT)
348 return krb5_principal2salt(context, host_princ, psalt);
351 #error UNKNOWN_SALT_FUNCTIONS
354 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
356 * @brief Get a list of encryption types allowed for session keys
358 * @param[in] context The library context
360 * @param[in] enctypes An allocated, zero-terminated list of encryption types
362 * This function returns an allocated list of encryption types allowed for
365 * Use free() to free the enctypes when it is no longer needed.
367 * @retval 0 Success; otherwise - Kerberos error codes
369 krb5_error_code smb_krb5_get_allowed_etypes(krb5_context context,
370 krb5_enctype **enctypes)
372 return krb5_get_permitted_enctypes(context, enctypes);
374 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
375 krb5_error_code smb_krb5_get_allowed_etypes(krb5_context context,
376 krb5_enctype **enctypes)
378 #ifdef HAVE_KRB5_PDU_NONE_DECL
379 return krb5_get_default_in_tkt_etypes(context, KRB5_PDU_NONE, enctypes);
381 return krb5_get_default_in_tkt_etypes(context, enctypes);
385 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
390 * @brief Convert a string principal name to a Kerberos principal.
392 * @param[in] context The library context
394 * @param[in] name The principal as a unix charset string.
396 * @param[out] principal The newly allocated principal.
398 * Use krb5_free_principal() to free a principal when it is no longer needed.
400 * @return 0 on success, a Kerberos error code otherwise.
402 krb5_error_code smb_krb5_parse_name(krb5_context context,
404 krb5_principal *principal)
408 size_t converted_size;
409 TALLOC_CTX *frame = talloc_stackframe();
411 if (!push_utf8_talloc(frame, &utf8_name, name, &converted_size)) {
416 ret = krb5_parse_name(context, utf8_name, principal);
422 * @brief Convert a Kerberos principal structure to a string representation.
424 * The resulting string representation will be a unix charset name and is
427 * @param[in] mem_ctx The talloc context to allocate memory on.
429 * @param[in] context The library context.
431 * @param[in] principal The principal.
433 * @param[out] unix_name A string representation of the princpial name as with
436 * Use talloc_free() to free the string representation if it is no longer
439 * @return 0 on success, a Kerberos error code otherwise.
441 krb5_error_code smb_krb5_unparse_name(TALLOC_CTX *mem_ctx,
442 krb5_context context,
443 krb5_const_principal principal,
448 size_t converted_size;
451 ret = krb5_unparse_name(context, principal, &utf8_name);
456 if (!pull_utf8_talloc(mem_ctx, unix_name, utf8_name, &converted_size)) {
457 krb5_free_unparsed_name(context, utf8_name);
460 krb5_free_unparsed_name(context, utf8_name);
465 * @brief Free the contents of a krb5_data structure and zero the data field.
467 * @param[in] context The krb5 context
469 * @param[in] pdata The data structure to free contents of
471 * This function frees the contents, not the structure itself.
473 void smb_krb5_free_data_contents(krb5_context context, krb5_data *pdata)
475 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
477 krb5_free_data_contents(context, pdata);
479 #elif defined(HAVE_KRB5_DATA_FREE)
480 krb5_data_free(context, pdata);
482 SAFE_FREE(pdata->data);
487 * @brief copy a buffer into a krb5_data struct
489 * @param[in] p The krb5_data
490 * @param[in] data The data to copy
491 * @param[in] length The length of the data to copy
492 * @return krb5_error_code
494 * Caller has to free krb5_data with smb_krb5_free_data_contents().
496 krb5_error_code smb_krb5_copy_data_contents(krb5_data *p,
500 #if defined(HAVE_KRB5_DATA_COPY)
501 return krb5_data_copy(p, data, len);
504 p->data = malloc(len);
505 if (p->data == NULL) {
508 memmove(p->data, data, len);
513 p->magic = KV5M_DATA;
518 bool smb_krb5_get_smb_session_key(TALLOC_CTX *mem_ctx,
519 krb5_context context,
520 krb5_auth_context auth_context,
521 DATA_BLOB *session_key,
524 krb5_keyblock *skey = NULL;
525 krb5_error_code err = 0;
529 #ifdef HAVE_KRB5_AUTH_CON_GETRECVSUBKEY
530 err = krb5_auth_con_getrecvsubkey(context,
533 #else /* HAVE_KRB5_AUTH_CON_GETRECVSUBKEY */
534 err = krb5_auth_con_getremotesubkey(context,
535 auth_context, &skey);
536 #endif /* HAVE_KRB5_AUTH_CON_GETRECVSUBKEY */
538 #ifdef HAVE_KRB5_AUTH_CON_GETSENDSUBKEY
539 err = krb5_auth_con_getsendsubkey(context,
542 #else /* HAVE_KRB5_AUTH_CON_GETSENDSUBKEY */
543 err = krb5_auth_con_getlocalsubkey(context,
544 auth_context, &skey);
545 #endif /* HAVE_KRB5_AUTH_CON_GETSENDSUBKEY */
548 if (err || skey == NULL) {
549 DEBUG(10, ("KRB5 error getting session key %d\n", err));
553 DEBUG(10, ("Got KRB5 session key of length %d\n",
554 (int)KRB5_KEY_LENGTH(skey)));
556 *session_key = data_blob_talloc(mem_ctx,
558 KRB5_KEY_LENGTH(skey));
559 dump_data_pw("KRB5 Session Key:\n",
561 session_key->length);
567 krb5_free_keyblock(context, skey);
575 * @brief Get talloced string component of a principal
577 * @param[in] mem_ctx The TALLOC_CTX
578 * @param[in] context The krb5_context
579 * @param[in] principal The principal
580 * @param[in] component The component
581 * @return string component
583 * Caller must talloc_free if the return value is not NULL.
586 char *smb_krb5_principal_get_comp_string(TALLOC_CTX *mem_ctx,
587 krb5_context context,
588 krb5_const_principal principal,
589 unsigned int component)
591 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
592 return talloc_strdup(mem_ctx, krb5_principal_get_comp_string(context, principal, component));
596 if (component >= krb5_princ_size(context, principal)) {
600 data = krb5_princ_component(context, principal, component);
605 return talloc_strndup(mem_ctx, data->data, data->length);
612 * @param[in] ccache_string A string pointing to the cache to renew the ticket
613 * (e.g. FILE:/tmp/krb5cc_0) or NULL. If the principal
614 * ccache has not been specified, the default ccache
617 * @param[in] client_string The client principal string (e.g. user@SAMBA.SITE)
618 * or NULL. If the principal string has not been
619 * specified, the principal from the ccache will be
622 * @param[in] service_string The service ticket string
623 * (e.g. krbtgt/SAMBA.SITE@SAMBA.SITE) or NULL. If
624 * the sevice ticket is specified, it is parsed (
625 * with the realm part ignored) and used as the
626 * server principal of the credential. Otherwise
627 * the ticket-granting service is used.
629 * @param[in] expire_time A pointer to store the credentials end time or
632 * @return 0 on Succes, a Kerberos error code otherwise.
634 krb5_error_code smb_krb5_renew_ticket(const char *ccache_string,
635 const char *client_string,
636 const char *service_string,
640 krb5_context context = NULL;
641 krb5_ccache ccache = NULL;
642 krb5_principal client = NULL;
643 krb5_creds creds, creds_in;
646 ZERO_STRUCT(creds_in);
648 initialize_krb5_error_table();
649 ret = krb5_init_context(&context);
654 if (!ccache_string) {
655 ccache_string = krb5_cc_default_name(context);
658 if (!ccache_string) {
663 DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string));
665 /* FIXME: we should not fall back to defaults */
666 ret = krb5_cc_resolve(context, discard_const_p(char, ccache_string), &ccache);
672 ret = smb_krb5_parse_name(context, client_string, &client);
677 ret = krb5_cc_get_principal(context, ccache, &client);
683 ret = krb5_get_renewed_creds(context, &creds, client, ccache, discard_const_p(char, service_string));
685 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
689 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
690 ret = krb5_cc_initialize(context, ccache, client);
695 ret = krb5_cc_store_cred(context, ccache, &creds);
698 *expire_time = (time_t) creds.times.endtime;
702 krb5_free_cred_contents(context, &creds_in);
703 krb5_free_cred_contents(context, &creds);
706 krb5_free_principal(context, client);
709 krb5_cc_close(context, ccache);
712 krb5_free_context(context);
719 * @brief Free the data stored in an smb_krb5_addresses structure.
721 * @param[in] context The library context
723 * @param[in] addr The address structure to free.
725 * @return 0 on success, a Kerberos error code otherwise.
727 krb5_error_code smb_krb5_free_addresses(krb5_context context,
728 smb_krb5_addresses *addr)
730 krb5_error_code ret = 0;
734 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
735 krb5_free_addresses(context, addr->addrs);
736 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
737 ret = krb5_free_addresses(context, addr->addrs);
738 SAFE_FREE(addr->addrs);
745 #define MAX_NETBIOSNAME_LEN 16
748 * @brief Add a netbios name to the array of addresses
750 * @param[in] kerb_addr A pointer to the smb_krb5_addresses to add the
753 * @param[in] netbios_name The netbios name to add.
755 * @return 0 on success, a Kerberos error code otherwise.
757 krb5_error_code smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses **kerb_addr,
758 const char *netbios_name)
760 krb5_error_code ret = 0;
761 char buf[MAX_NETBIOSNAME_LEN];
763 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
764 krb5_address **addrs = NULL;
765 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
766 krb5_addresses *addrs = NULL;
769 *kerb_addr = (smb_krb5_addresses *)SMB_MALLOC(sizeof(smb_krb5_addresses));
770 if (*kerb_addr == NULL) {
774 /* temporarily duplicate put_name() code here to avoid dependency
775 * issues for a 5 lines function */
776 len = strlen(netbios_name);
777 memcpy(buf, netbios_name,
778 (len < MAX_NETBIOSNAME_LEN) ? len : MAX_NETBIOSNAME_LEN - 1);
779 if (len < MAX_NETBIOSNAME_LEN - 1) {
780 memset(buf + len, ' ', MAX_NETBIOSNAME_LEN - 1 - len);
782 buf[MAX_NETBIOSNAME_LEN - 1] = 0x20;
784 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
788 addrs = (krb5_address **)SMB_MALLOC(sizeof(krb5_address *) * num_addr);
790 SAFE_FREE(*kerb_addr);
794 memset(addrs, 0, sizeof(krb5_address *) * num_addr);
796 addrs[0] = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
797 if (addrs[0] == NULL) {
799 SAFE_FREE(*kerb_addr);
803 addrs[0]->magic = KV5M_ADDRESS;
804 addrs[0]->addrtype = KRB5_ADDR_NETBIOS;
805 addrs[0]->length = MAX_NETBIOSNAME_LEN;
806 addrs[0]->contents = (unsigned char *)SMB_MALLOC(addrs[0]->length);
807 if (addrs[0]->contents == NULL) {
810 SAFE_FREE(*kerb_addr);
814 memcpy(addrs[0]->contents, buf, addrs[0]->length);
818 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
820 addrs = (krb5_addresses *)SMB_MALLOC(sizeof(krb5_addresses));
822 SAFE_FREE(*kerb_addr);
826 memset(addrs, 0, sizeof(krb5_addresses));
829 addrs->val = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
830 if (addrs->val == NULL) {
832 SAFE_FREE(kerb_addr);
836 addrs->val[0].addr_type = KRB5_ADDR_NETBIOS;
837 addrs->val[0].address.length = MAX_NETBIOSNAME_LEN;
838 addrs->val[0].address.data = (unsigned char *)SMB_MALLOC(addrs->val[0].address.length);
839 if (addrs->val[0].address.data == NULL) {
840 SAFE_FREE(addrs->val);
842 SAFE_FREE(*kerb_addr);
846 memcpy(addrs->val[0].address.data, buf, addrs->val[0].address.length);
849 #error UNKNOWN_KRB5_ADDRESS_FORMAT
851 (*kerb_addr)->addrs = addrs;
857 * @brief Get the enctype from a key table entry
859 * @param[in] kt_entry Key table entry to get the enctype from.
861 * @return The enctype from the entry.
863 krb5_enctype smb_krb5_kt_get_enctype_from_entry(krb5_keytab_entry *kt_entry)
865 return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry));
869 * @brief Free the contents of a key table entry.
871 * @param[in] context The library context.
873 * @param[in] kt_entry The key table entry to free the contents of.
875 * @return 0 on success, a Kerberos error code otherwise.
877 * The pointer itself is not freed.
879 krb5_error_code smb_krb5_kt_free_entry(krb5_context context,
880 krb5_keytab_entry *kt_entry)
882 /* Try krb5_free_keytab_entry_contents first, since
883 * MIT Kerberos >= 1.7 has both krb5_free_keytab_entry_contents and
884 * krb5_kt_free_entry but only has a prototype for the first, while the
885 * second is considered private.
887 #if defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
888 return krb5_free_keytab_entry_contents(context, kt_entry);
889 #elif defined(HAVE_KRB5_KT_FREE_ENTRY)
890 return krb5_kt_free_entry(context, kt_entry);
892 #error UNKNOWN_KT_FREE_FUNCTION
898 * @brief Convert an encryption type to a string.
900 * @param[in] context The library context.
902 * @param[in] enctype The encryption type.
904 * @param[in] etype_s A pointer to store the allocated encryption type as a
907 * @return 0 on success, a Kerberos error code otherwise.
909 * The caller needs to free the allocated string etype_s.
911 krb5_error_code smb_krb5_enctype_to_string(krb5_context context,
912 krb5_enctype enctype,
915 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
916 return krb5_enctype_to_string(context, enctype, etype_s); /* Heimdal */
917 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
919 krb5_error_code ret = krb5_enctype_to_string(enctype, buf, 256); /* MIT */
923 *etype_s = SMB_STRDUP(buf);
929 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
933 /* This MAX_NAME_LEN is a constant defined in krb5.h */
934 #ifndef MAX_KEYTAB_NAME_LEN
935 #define MAX_KEYTAB_NAME_LEN 1100
939 * @brief Open a key table readonly or with readwrite access.
941 * Allows to use a different keytab than the default one using a relative
942 * path to the keytab.
944 * @param[in] context The library context
946 * @param[in] keytab_name_req The path to the key table.
948 * @param[in] write_access Open with readwrite access.
950 * @param[in] keytab A pointer o the opended key table.
952 * The keytab pointer should be freed using krb5_kt_close().
954 * @return 0 on success, a Kerberos error code otherwise.
956 krb5_error_code smb_krb5_kt_open_relative(krb5_context context,
957 const char *keytab_name_req,
961 krb5_error_code ret = 0;
963 char keytab_string[MAX_KEYTAB_NAME_LEN];
965 bool found_valid_name = false;
966 const char *pragma = "FILE";
967 const char *tmp = NULL;
969 if (!write_access && !keytab_name_req) {
970 /* caller just wants to read the default keytab readonly, so be it */
971 return krb5_kt_default(context, keytab);
974 mem_ctx = talloc_init("smb_krb5_open_keytab");
979 #ifdef HAVE_WRFILE_KEYTAB
985 if (keytab_name_req) {
987 if (strlen(keytab_name_req) > MAX_KEYTAB_NAME_LEN) {
988 ret = KRB5_CONFIG_NOTENUFSPACE;
992 if ((strncmp(keytab_name_req, "WRFILE:/", 8) == 0) ||
993 (strncmp(keytab_name_req, "FILE:/", 6) == 0)) {
994 tmp = keytab_name_req;
998 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, keytab_name_req);
1007 /* we need to handle more complex keytab_strings, like:
1008 * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
1010 ret = krb5_kt_default_name(context, &keytab_string[0], MAX_KEYTAB_NAME_LEN - 2);
1015 DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string));
1017 tmp = talloc_strdup(mem_ctx, keytab_string);
1023 if (strncmp(tmp, "ANY:", 4) == 0) {
1027 memset(&keytab_string, '\0', sizeof(keytab_string));
1029 while (next_token_talloc(mem_ctx, &tmp, &kt_str, ",")) {
1030 if (strncmp(kt_str, "WRFILE:", 7) == 0) {
1031 found_valid_name = true;
1036 if (strncmp(kt_str, "FILE:", 5) == 0) {
1037 found_valid_name = true;
1042 if (tmp[0] == '/') {
1043 /* Treat as a FILE: keytab definition. */
1044 found_valid_name = true;
1047 if (found_valid_name) {
1048 if (tmp[0] != '/') {
1049 ret = KRB5_KT_BADNAME;
1053 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, tmp);
1062 if (!found_valid_name) {
1063 ret = KRB5_KT_UNKNOWN_TYPE;
1068 DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp));
1069 ret = krb5_kt_resolve(context, tmp, keytab);
1072 TALLOC_FREE(mem_ctx);
1077 * @brief Open a key table readonly or with readwrite access.
1079 * Allows to use a different keytab than the default one. The path needs to be
1080 * an absolute path or an error will be returned.
1082 * @param[in] context The library context
1084 * @param[in] keytab_name_req The path to the key table.
1086 * @param[in] write_access Open with readwrite access.
1088 * @param[in] keytab A pointer o the opended key table.
1090 * The keytab pointer should be freed using krb5_kt_close().
1092 * @return 0 on success, a Kerberos error code otherwise.
1094 krb5_error_code smb_krb5_kt_open(krb5_context context,
1095 const char *keytab_name_req,
1097 krb5_keytab *keytab)
1099 if (keytab_name_req != NULL) {
1100 if (keytab_name_req[0] != '/') {
1101 return KRB5_KT_BADNAME;
1105 return smb_krb5_kt_open_relative(context,
1112 * @brief Get a key table name.
1114 * @param[in] mem_ctx The talloc context to use for allocation.
1116 * @param[in] context The library context.
1118 * @param[in] keytab The key table to get the name from.
1120 * @param[in] keytab_name A talloc'ed string of the key table name.
1122 * The talloc'ed name string needs to be freed with talloc_free().
1124 * @return 0 on success, a Kerberos error code otherwise.
1126 krb5_error_code smb_krb5_kt_get_name(TALLOC_CTX *mem_ctx,
1127 krb5_context context,
1129 const char **keytab_name)
1131 char keytab_string[MAX_KEYTAB_NAME_LEN];
1132 krb5_error_code ret = 0;
1134 ret = krb5_kt_get_name(context, keytab,
1135 keytab_string, MAX_KEYTAB_NAME_LEN - 2);
1140 *keytab_name = talloc_strdup(mem_ctx, keytab_string);
1141 if (!*keytab_name) {
1149 * @brief Seek and delete old entries in a keytab based on the passed
1152 * @param[in] context The KRB5 context to use.
1154 * @param[in] keytab The keytab to operate on.
1156 * @param[in] kvno The kvnco to use.
1158 * @param[in] princ_s The principal as a string to search for.
1160 * @param[in] princ The principal as a krb5_principal to search for.
1162 * @param[in] flush Weather to flush the complete keytab.
1164 * @param[in] keep_old_entries Keep the entry with the previous kvno.
1166 * @retval 0 on Sucess
1168 * @return An appropriate KRB5 error code.
1170 krb5_error_code smb_krb5_kt_seek_and_delete_old_entries(krb5_context context,
1173 krb5_enctype enctype,
1174 const char *princ_s,
1175 krb5_principal princ,
1177 bool keep_old_entries)
1179 krb5_error_code ret;
1180 krb5_kt_cursor cursor;
1181 krb5_kt_cursor zero_csr;
1182 krb5_keytab_entry kt_entry;
1183 krb5_keytab_entry zero_kt_entry;
1184 char *ktprinc = NULL;
1185 krb5_kvno old_kvno = kvno - 1;
1186 TALLOC_CTX *tmp_ctx;
1188 ZERO_STRUCT(cursor);
1189 ZERO_STRUCT(zero_csr);
1190 ZERO_STRUCT(kt_entry);
1191 ZERO_STRUCT(zero_kt_entry);
1193 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
1194 if (ret == KRB5_KT_END || ret == ENOENT ) {
1199 tmp_ctx = talloc_new(NULL);
1200 if (tmp_ctx == NULL) {
1204 DEBUG(3, (__location__ ": Will try to delete old keytab entries\n"));
1205 while (!krb5_kt_next_entry(context, keytab, &kt_entry, &cursor)) {
1206 bool name_ok = false;
1207 krb5_enctype kt_entry_enctype =
1208 smb_krb5_kt_get_enctype_from_entry(&kt_entry);
1210 if (!flush && (princ_s != NULL)) {
1211 ret = smb_krb5_unparse_name(tmp_ctx, context,
1215 DEBUG(1, (__location__
1216 ": smb_krb5_unparse_name failed "
1217 "(%s)\n", error_message(ret)));
1221 #ifdef HAVE_KRB5_KT_COMPARE
1222 name_ok = krb5_kt_compare(context, &kt_entry,
1225 name_ok = (strcmp(ktprinc, princ_s) == 0);
1229 DEBUG(10, (__location__ ": ignoring keytab "
1230 "entry principal %s, kvno = %d\n",
1231 ktprinc, kt_entry.vno));
1234 * just free this entry and continue. */
1235 ret = smb_krb5_kt_free_entry(context,
1237 ZERO_STRUCT(kt_entry);
1239 DEBUG(1, (__location__
1240 ": smb_krb5_kt_free_entry "
1242 error_message(ret)));
1246 TALLOC_FREE(ktprinc);
1250 TALLOC_FREE(ktprinc);
1253 /*------------------------------------------------------------
1254 * Save the entries with kvno - 1. This is what microsoft does
1255 * to allow people with existing sessions that have kvno - 1
1256 * to still work. Otherwise, when the password for the machine
1257 * changes, all kerberizied sessions will 'break' until either
1258 * the client reboots or the client's session key expires and
1259 * they get a new session ticket with the new kvno.
1260 * Some keytab files only store the kvno in 8bits, limit
1261 * the compare accordingly.
1264 if (!flush && ((kt_entry.vno & 0xff) == (old_kvno & 0xff))) {
1265 DEBUG(5, (__location__ ": Saving previous (kvno %d) "
1266 "entry for principal: %s.\n",
1267 old_kvno, princ_s));
1271 if (keep_old_entries) {
1272 DEBUG(5, (__location__ ": Saving old (kvno %d) "
1273 "entry for principal: %s.\n",
1279 (kt_entry.vno == kvno) &&
1280 (kt_entry_enctype != enctype))
1282 DEBUG(5, (__location__ ": Saving entry with kvno [%d] "
1283 "enctype [%d] for principal: %s.\n",
1284 kvno, kt_entry_enctype, princ_s));
1288 DEBUG(5, (__location__ ": Found old entry for principal: %s "
1289 "(kvno %d) - trying to remove it.\n",
1290 princ_s, kt_entry.vno));
1292 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
1293 ZERO_STRUCT(cursor);
1295 DEBUG(1, (__location__ ": krb5_kt_end_seq_get() "
1296 "failed (%s)\n", error_message(ret)));
1299 ret = krb5_kt_remove_entry(context, keytab, &kt_entry);
1301 DEBUG(1, (__location__ ": krb5_kt_remove_entry() "
1302 "failed (%s)\n", error_message(ret)));
1306 DEBUG(5, (__location__ ": removed old entry for principal: "
1307 "%s (kvno %d).\n", princ_s, kt_entry.vno));
1309 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
1311 DEBUG(1, (__location__ ": krb5_kt_start_seq() failed "
1312 "(%s)\n", error_message(ret)));
1315 ret = smb_krb5_kt_free_entry(context, &kt_entry);
1316 ZERO_STRUCT(kt_entry);
1318 DEBUG(1, (__location__ ": krb5_kt_remove_entry() "
1319 "failed (%s)\n", error_message(ret)));
1325 talloc_free(tmp_ctx);
1326 if (memcmp(&zero_kt_entry, &kt_entry, sizeof(krb5_keytab_entry))) {
1327 smb_krb5_kt_free_entry(context, &kt_entry);
1329 if (memcmp(&cursor, &zero_csr, sizeof(krb5_kt_cursor)) != 0) {
1330 krb5_kt_end_seq_get(context, keytab, &cursor);
1336 * @brief Add a keytab entry for the given principal
1338 * @param[in] context The krb5 context to use.
1340 * @param[in] keytab The keytab to add the entry to.
1342 * @param[in] kvno The kvno to use.
1344 * @param[in] princ_s The principal as a string.
1346 * @param[in] salt_principal The salt principal to salt the password with.
1347 * Only needed for keys which support salting.
1348 * If no salt is used set no_salt to false and
1351 * @param[in] enctype The encryption type of the keytab entry.
1353 * @param[in] password The password of the keytab entry.
1355 * @param[in] no_salt If the password should not be salted. Normally
1356 * this is only set to false for encryption types
1357 * which do not support salting like RC4.
1359 * @param[in] keep_old_entries Wether to keep or delte old keytab entries.
1361 * @retval 0 on Success
1363 * @return A corresponding KRB5 error code.
1365 * @see smb_krb5_kt_open()
1367 krb5_error_code smb_krb5_kt_add_entry(krb5_context context,
1370 const char *princ_s,
1371 const char *salt_principal,
1372 krb5_enctype enctype,
1373 krb5_data *password,
1375 bool keep_old_entries)
1377 krb5_error_code ret;
1378 krb5_keytab_entry kt_entry;
1379 krb5_principal princ = NULL;
1380 krb5_keyblock *keyp;
1382 ZERO_STRUCT(kt_entry);
1384 ret = smb_krb5_parse_name(context, princ_s, &princ);
1386 DEBUG(1, (__location__ ": smb_krb5_parse_name(%s) "
1387 "failed (%s)\n", princ_s, error_message(ret)));
1391 /* Seek and delete old keytab entries */
1392 ret = smb_krb5_kt_seek_and_delete_old_entries(context,
1404 /* If we get here, we have deleted all the old entries with kvno's
1405 * not equal to the current kvno-1. */
1407 keyp = KRB5_KT_KEY(&kt_entry);
1410 KRB5_KEY_DATA(keyp) = (KRB5_KEY_DATA_CAST *)SMB_MALLOC(password->length);
1411 if (KRB5_KEY_DATA(keyp) == NULL) {
1415 memcpy(KRB5_KEY_DATA(keyp), password->data, password->length);
1416 KRB5_KEY_LENGTH(keyp) = password->length;
1417 KRB5_KEY_TYPE(keyp) = enctype;
1419 krb5_principal salt_princ = NULL;
1421 /* Now add keytab entries for all encryption types */
1422 ret = smb_krb5_parse_name(context, salt_principal, &salt_princ);
1424 DBG_WARNING("krb5_parse_name(%s) failed (%s)\n",
1425 salt_principal, error_message(ret));
1429 ret = smb_krb5_create_key_from_string(context,
1435 krb5_free_principal(context, salt_princ);
1441 kt_entry.principal = princ;
1442 kt_entry.vno = kvno;
1444 DEBUG(3, (__location__ ": adding keytab entry for (%s) with "
1445 "encryption type (%d) and version (%d)\n",
1446 princ_s, enctype, kt_entry.vno));
1447 ret = krb5_kt_add_entry(context, keytab, &kt_entry);
1448 krb5_free_keyblock_contents(context, keyp);
1449 ZERO_STRUCT(kt_entry);
1451 DEBUG(1, (__location__ ": adding entry to keytab "
1452 "failed (%s)\n", error_message(ret)));
1458 krb5_free_principal(context, princ);
1464 #if defined(HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE) && \
1465 defined(HAVE_KRB5_GET_CREDS_OPT_ALLOC) && \
1466 defined(HAVE_KRB5_GET_CREDS)
1467 static krb5_error_code smb_krb5_get_credentials_for_user_opt(krb5_context context,
1470 krb5_principal server,
1471 krb5_principal impersonate_princ,
1472 krb5_creds **out_creds)
1474 krb5_error_code ret;
1475 krb5_get_creds_opt opt;
1477 ret = krb5_get_creds_opt_alloc(context, &opt);
1481 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_FORWARDABLE);
1483 if (impersonate_princ) {
1484 ret = krb5_get_creds_opt_set_impersonate(context, opt,
1491 ret = krb5_get_creds(context, opt, ccache, server, out_creds);
1498 krb5_get_creds_opt_free(context, opt);
1502 #endif /* HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE */
1504 #ifdef HAVE_KRB5_GET_CREDENTIALS_FOR_USER
1506 #if !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER
1507 krb5_error_code KRB5_CALLCONV
1508 krb5_get_credentials_for_user(krb5_context context, krb5_flags options,
1509 krb5_ccache ccache, krb5_creds *in_creds,
1510 krb5_data *subject_cert,
1511 krb5_creds **out_creds);
1512 #endif /* !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER */
1514 static krb5_error_code smb_krb5_get_credentials_for_user(krb5_context context,
1517 krb5_principal server,
1518 krb5_principal impersonate_princ,
1519 krb5_creds **out_creds)
1521 krb5_error_code ret;
1522 krb5_creds in_creds;
1524 ZERO_STRUCT(in_creds);
1526 if (impersonate_princ) {
1528 in_creds.server = me;
1529 in_creds.client = impersonate_princ;
1531 ret = krb5_get_credentials_for_user(context,
1532 0, /* krb5_flags options */
1535 NULL, /* krb5_data *subject_cert */
1538 in_creds.client = me;
1539 in_creds.server = server;
1541 ret = krb5_get_credentials(context, 0, ccache,
1542 &in_creds, out_creds);
1547 #endif /* HAVE_KRB5_GET_CREDENTIALS_FOR_USER */
1550 * smb_krb5_get_credentials
1552 * @brief Get krb5 credentials for a server
1554 * @param[in] context An initialized krb5_context
1555 * @param[in] ccache An initialized krb5_ccache
1556 * @param[in] me The krb5_principal of the caller
1557 * @param[in] server The krb5_principal of the requested service
1558 * @param[in] impersonate_princ The krb5_principal of a user to impersonate as (optional)
1559 * @param[out] out_creds The returned krb5_creds structure
1560 * @return krb5_error_code
1563 krb5_error_code smb_krb5_get_credentials(krb5_context context,
1566 krb5_principal server,
1567 krb5_principal impersonate_princ,
1568 krb5_creds **out_creds)
1570 krb5_error_code ret;
1571 krb5_creds *creds = NULL;
1573 if (out_creds != NULL) {
1577 if (impersonate_princ) {
1578 #ifdef HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE /* Heimdal */
1579 ret = smb_krb5_get_credentials_for_user_opt(context, ccache, me, server, impersonate_princ, &creds);
1580 #elif defined(HAVE_KRB5_GET_CREDENTIALS_FOR_USER) /* MIT */
1581 ret = smb_krb5_get_credentials_for_user(context, ccache, me, server, impersonate_princ, &creds);
1586 krb5_creds in_creds;
1588 ZERO_STRUCT(in_creds);
1590 in_creds.client = me;
1591 in_creds.server = server;
1593 ret = krb5_get_credentials(context, 0, ccache,
1606 krb5_free_creds(context, creds);
1613 * @brief Initialize a krb5_keyblock with the given data.
1615 * Initialized a new keyblock, allocates the contents fo the key and
1616 * copies the data into the keyblock.
1618 * @param[in] context The library context
1620 * @param[in] enctype The encryption type.
1622 * @param[in] data The date to initialize the keyblock with.
1624 * @param[in] length The length of the keyblock.
1626 * @param[in] key Newly allocated keyblock structure.
1628 * The key date must be freed using krb5_free_keyblock_contents() when it is
1631 * @return 0 on success, a Kerberos error code otherwise.
1633 krb5_error_code smb_krb5_keyblock_init_contents(krb5_context context,
1634 krb5_enctype enctype,
1639 #if defined(HAVE_KRB5_KEYBLOCK_INIT)
1640 return krb5_keyblock_init(context, enctype, data, length, key);
1641 #elif defined(HAVE_KRB5_INIT_KEYBLOCK)
1642 krb5_error_code code;
1644 code = krb5_init_keyblock(context,
1653 memcpy(KRB5_KEY_DATA(key), data, length);
1658 memset(key, 0, sizeof(krb5_keyblock));
1659 KRB5_KEY_DATA(key) = SMB_MALLOC(length);
1660 if (NULL == KRB5_KEY_DATA(key)) {
1663 memcpy(KRB5_KEY_DATA(key), data, length);
1664 KRB5_KEY_LENGTH(key) = length;
1665 KRB5_KEY_TYPE(key) = enctype;
1671 * @brief Simulate a kinit by putting the tgt in the given credential cache.
1673 * This function uses a keyblock rather than needingthe original password.
1675 * @param[in] ctx The library context
1677 * @param[in] cc The credential cache to put the tgt in.
1679 * @param[in] principal The client princial
1681 * @param[in] keyblock The keyblock to use.
1683 * @param[in] target_service The service name of the initial credentials (or NULL).
1685 * @param[in] krb_options Initial credential options.
1687 * @param[in] expire_time A pointer to store the experation time of the
1688 * credentials (or NULL).
1690 * @param[in] kdc_time A pointer to store the time when the ticket becomes
1693 * @return 0 on success, a Kerberos error code otherwise.
1695 krb5_error_code smb_krb5_kinit_keyblock_ccache(krb5_context ctx,
1697 krb5_principal principal,
1698 krb5_keyblock *keyblock,
1699 const char *target_service,
1700 krb5_get_init_creds_opt *krb_options,
1701 time_t *expire_time,
1704 krb5_error_code code = 0;
1705 krb5_creds my_creds;
1707 #if defined(HAVE_KRB5_GET_INIT_CREDS_KEYBLOCK)
1708 code = krb5_get_init_creds_keyblock(ctx, &my_creds, principal,
1709 keyblock, 0, target_service,
1711 #elif defined(HAVE_KRB5_GET_INIT_CREDS_KEYTAB)
1713 #define SMB_CREDS_KEYTAB "MEMORY:tmp_smb_creds_XXXXXX"
1714 char tmp_name[sizeof(SMB_CREDS_KEYTAB)];
1715 krb5_keytab_entry entry;
1719 memset(&entry, 0, sizeof(entry));
1720 entry.principal = principal;
1721 *(KRB5_KT_KEY(&entry)) = *keyblock;
1723 memcpy(tmp_name, SMB_CREDS_KEYTAB, sizeof(SMB_CREDS_KEYTAB));
1724 mask = umask(S_IRWXO | S_IRWXG);
1727 if (tmp_name[0] == 0) {
1728 return KRB5_KT_BADNAME;
1730 code = krb5_kt_resolve(ctx, tmp_name, &keytab);
1735 code = krb5_kt_add_entry(ctx, keytab, &entry);
1737 (void)krb5_kt_close(ctx, keytab);
1741 code = krb5_get_init_creds_keytab(ctx, &my_creds, principal,
1742 keytab, 0, target_service,
1744 (void)krb5_kt_close(ctx, keytab);
1747 #error krb5_get_init_creds_keyblock not available!
1753 #ifndef SAMBA4_USES_HEIMDAL /* MIT */
1755 * We need to store the principal as returned from the KDC to the
1756 * credentials cache. If we don't do that the KRB5 library is not
1757 * able to find the tickets it is looking for
1759 principal = my_creds.client;
1761 code = krb5_cc_initialize(ctx, cc, principal);
1766 code = krb5_cc_store_cred(ctx, cc, &my_creds);
1772 *expire_time = (time_t) my_creds.times.endtime;
1776 *kdc_time = (time_t) my_creds.times.starttime;
1781 krb5_free_cred_contents(ctx, &my_creds);
1786 * @brief Simulate a kinit by putting the tgt in the given credential cache.
1788 * @param[in] ctx The library context
1790 * @param[in] cc The credential cache to put the tgt in.
1792 * @param[in] principal The client princial
1794 * @param[in] password The password (or NULL).
1796 * @param[in] target_service The service name of the initial credentials (or NULL).
1798 * @param[in] krb_options Initial credential options.
1800 * @param[in] expire_time A pointer to store the experation time of the
1801 * credentials (or NULL).
1803 * @param[in] kdc_time A pointer to store the time when the ticket becomes
1806 * @return 0 on success, a Kerberos error code otherwise.
1808 krb5_error_code smb_krb5_kinit_password_ccache(krb5_context ctx,
1810 krb5_principal principal,
1811 const char *password,
1812 const char *target_service,
1813 krb5_get_init_creds_opt *krb_options,
1814 time_t *expire_time,
1817 krb5_error_code code = 0;
1818 krb5_creds my_creds;
1820 code = krb5_get_init_creds_password(ctx, &my_creds, principal,
1821 password, NULL, NULL, 0,
1822 target_service, krb_options);
1827 #ifndef SAMBA4_USES_HEIMDAL /* MIT */
1829 * We need to store the principal as returned from the KDC to the
1830 * credentials cache. If we don't do that the KRB5 library is not
1831 * able to find the tickets it is looking for
1833 principal = my_creds.client;
1835 code = krb5_cc_initialize(ctx, cc, principal);
1840 code = krb5_cc_store_cred(ctx, cc, &my_creds);
1846 *expire_time = (time_t) my_creds.times.endtime;
1850 *kdc_time = (time_t) my_creds.times.starttime;
1855 krb5_free_cred_contents(ctx, &my_creds);
1859 #ifdef SAMBA4_USES_HEIMDAL
1861 * @brief Simulate a kinit by putting the tgt in the given credential cache.
1863 * @param[in] ctx The library context
1865 * @param[in] cc The credential cache to store the tgt in.
1867 * @param[in] principal The initial client princial.
1869 * @param[in] password The password (or NULL).
1871 * @param[in] impersonate_principal The impersonatiion principal (or NULL).
1873 * @param[in] self_service The local service for S4U2Self if
1874 * impersonate_principal is specified).
1876 * @param[in] target_service The service name of the initial credentials
1877 * (kpasswd/REALM or a remote service). It defaults
1878 * to the krbtgt if NULL.
1880 * @param[in] krb_options Initial credential options.
1882 * @param[in] expire_time A pointer to store the experation time of the
1883 * credentials (or NULL).
1885 * @param[in] kdc_time A pointer to store the time when the ticket becomes
1888 * @return 0 on success, a Kerberos error code otherwise.
1890 krb5_error_code smb_krb5_kinit_s4u2_ccache(krb5_context ctx,
1891 krb5_ccache store_cc,
1892 krb5_principal init_principal,
1893 const char *init_password,
1894 krb5_principal impersonate_principal,
1895 const char *self_service,
1896 const char *target_service,
1897 krb5_get_init_creds_opt *krb_options,
1898 time_t *expire_time,
1901 krb5_error_code code = 0;
1902 krb5_get_creds_opt options;
1903 krb5_principal store_principal;
1904 krb5_creds store_creds;
1905 krb5_creds *s4u2self_creds;
1906 Ticket s4u2self_ticket;
1907 size_t s4u2self_ticketlen;
1908 krb5_creds *s4u2proxy_creds;
1909 krb5_principal self_princ;
1911 krb5_principal target_princ;
1913 const char *self_realm;
1914 krb5_principal blacklist_principal = NULL;
1915 krb5_principal whitelist_principal = NULL;
1917 code = krb5_get_init_creds_password(ctx, &store_creds,
1928 store_principal = init_principal;
1931 * We are trying S4U2Self now:
1933 * As we do not want to expose our TGT in the
1934 * krb5_ccache, which is also holds the impersonated creds.
1936 * Some low level krb5/gssapi function might use the TGT
1937 * identity and let the client act as our machine account.
1939 * We need to avoid that and use a temporary krb5_ccache
1940 * in order to pass our TGT to the krb5_get_creds() function.
1942 code = krb5_cc_new_unique(ctx, NULL, NULL, &tmp_cc);
1944 krb5_free_cred_contents(ctx, &store_creds);
1948 code = krb5_cc_initialize(ctx, tmp_cc, store_creds.client);
1950 krb5_cc_destroy(ctx, tmp_cc);
1951 krb5_free_cred_contents(ctx, &store_creds);
1955 code = krb5_cc_store_cred(ctx, tmp_cc, &store_creds);
1957 krb5_free_cred_contents(ctx, &store_creds);
1958 krb5_cc_destroy(ctx, tmp_cc);
1963 * we need to remember the client principal of our
1964 * TGT and make sure the KDC does not return this
1965 * in the impersonated tickets. This can happen
1966 * if the KDC does not support S4U2Self and S4U2Proxy.
1968 blacklist_principal = store_creds.client;
1969 store_creds.client = NULL;
1970 krb5_free_cred_contents(ctx, &store_creds);
1973 * Check if we also need S4U2Proxy or if S4U2Self is
1974 * enough in order to get a ticket for the target.
1976 if (target_service == NULL) {
1978 } else if (strcmp(target_service, self_service) == 0) {
1985 * For S4U2Self we need our own service principal,
1986 * which belongs to our own realm (available on
1987 * our client principal).
1989 self_realm = krb5_principal_get_realm(ctx, init_principal);
1991 code = krb5_parse_name(ctx, self_service, &self_princ);
1993 krb5_free_principal(ctx, blacklist_principal);
1994 krb5_cc_destroy(ctx, tmp_cc);
1998 code = krb5_principal_set_realm(ctx, self_princ, self_realm);
2000 krb5_free_principal(ctx, blacklist_principal);
2001 krb5_free_principal(ctx, self_princ);
2002 krb5_cc_destroy(ctx, tmp_cc);
2006 code = krb5_get_creds_opt_alloc(ctx, &options);
2008 krb5_free_principal(ctx, blacklist_principal);
2009 krb5_free_principal(ctx, self_princ);
2010 krb5_cc_destroy(ctx, tmp_cc);
2016 * If we want S4U2Proxy, we need the forwardable flag
2017 * on the S4U2Self ticket.
2019 krb5_get_creds_opt_set_options(ctx, options, KRB5_GC_FORWARDABLE);
2022 code = krb5_get_creds_opt_set_impersonate(ctx, options,
2023 impersonate_principal);
2025 krb5_get_creds_opt_free(ctx, options);
2026 krb5_free_principal(ctx, blacklist_principal);
2027 krb5_free_principal(ctx, self_princ);
2028 krb5_cc_destroy(ctx, tmp_cc);
2032 code = krb5_get_creds(ctx, options, tmp_cc,
2033 self_princ, &s4u2self_creds);
2034 krb5_get_creds_opt_free(ctx, options);
2035 krb5_free_principal(ctx, self_princ);
2037 krb5_free_principal(ctx, blacklist_principal);
2038 krb5_cc_destroy(ctx, tmp_cc);
2043 krb5_cc_destroy(ctx, tmp_cc);
2046 * Now make sure we store the impersonated principal
2047 * and creds instead of the TGT related stuff
2048 * in the krb5_ccache of the caller.
2050 code = krb5_copy_creds_contents(ctx, s4u2self_creds,
2052 krb5_free_creds(ctx, s4u2self_creds);
2058 * It's important to store the principal the KDC
2059 * returned, as otherwise the caller would not find
2060 * the S4U2Self ticket in the krb5_ccache lookup.
2062 store_principal = store_creds.client;
2067 * We are trying S4U2Proxy:
2069 * We need the ticket from the S4U2Self step
2070 * and our TGT in order to get the delegated ticket.
2072 code = decode_Ticket((const uint8_t *)s4u2self_creds->ticket.data,
2073 s4u2self_creds->ticket.length,
2075 &s4u2self_ticketlen);
2077 krb5_free_creds(ctx, s4u2self_creds);
2078 krb5_free_principal(ctx, blacklist_principal);
2079 krb5_cc_destroy(ctx, tmp_cc);
2084 * we need to remember the client principal of the
2085 * S4U2Self stage and as it needs to match the one we
2086 * will get for the S4U2Proxy stage. We need this
2087 * in order to detect KDCs which does not support S4U2Proxy.
2089 whitelist_principal = s4u2self_creds->client;
2090 s4u2self_creds->client = NULL;
2091 krb5_free_creds(ctx, s4u2self_creds);
2094 * For S4U2Proxy we also got a target service principal,
2095 * which also belongs to our own realm (available on
2096 * our client principal).
2098 code = krb5_parse_name(ctx, target_service, &target_princ);
2100 free_Ticket(&s4u2self_ticket);
2101 krb5_free_principal(ctx, whitelist_principal);
2102 krb5_free_principal(ctx, blacklist_principal);
2103 krb5_cc_destroy(ctx, tmp_cc);
2107 code = krb5_principal_set_realm(ctx, target_princ, self_realm);
2109 free_Ticket(&s4u2self_ticket);
2110 krb5_free_principal(ctx, target_princ);
2111 krb5_free_principal(ctx, whitelist_principal);
2112 krb5_free_principal(ctx, blacklist_principal);
2113 krb5_cc_destroy(ctx, tmp_cc);
2117 code = krb5_get_creds_opt_alloc(ctx, &options);
2119 free_Ticket(&s4u2self_ticket);
2120 krb5_free_principal(ctx, target_princ);
2121 krb5_free_principal(ctx, whitelist_principal);
2122 krb5_free_principal(ctx, blacklist_principal);
2123 krb5_cc_destroy(ctx, tmp_cc);
2127 krb5_get_creds_opt_set_options(ctx, options, KRB5_GC_FORWARDABLE);
2128 krb5_get_creds_opt_set_options(ctx, options, KRB5_GC_CONSTRAINED_DELEGATION);
2130 code = krb5_get_creds_opt_set_ticket(ctx, options, &s4u2self_ticket);
2131 free_Ticket(&s4u2self_ticket);
2133 krb5_get_creds_opt_free(ctx, options);
2134 krb5_free_principal(ctx, target_princ);
2135 krb5_free_principal(ctx, whitelist_principal);
2136 krb5_free_principal(ctx, blacklist_principal);
2137 krb5_cc_destroy(ctx, tmp_cc);
2141 code = krb5_get_creds(ctx, options, tmp_cc,
2142 target_princ, &s4u2proxy_creds);
2143 krb5_get_creds_opt_free(ctx, options);
2144 krb5_free_principal(ctx, target_princ);
2145 krb5_cc_destroy(ctx, tmp_cc);
2147 krb5_free_principal(ctx, whitelist_principal);
2148 krb5_free_principal(ctx, blacklist_principal);
2153 * Now make sure we store the impersonated principal
2154 * and creds instead of the TGT related stuff
2155 * in the krb5_ccache of the caller.
2157 code = krb5_copy_creds_contents(ctx, s4u2proxy_creds,
2159 krb5_free_creds(ctx, s4u2proxy_creds);
2161 krb5_free_principal(ctx, whitelist_principal);
2162 krb5_free_principal(ctx, blacklist_principal);
2167 * It's important to store the principal the KDC
2168 * returned, as otherwise the caller would not find
2169 * the S4U2Self ticket in the krb5_ccache lookup.
2171 store_principal = store_creds.client;
2174 if (blacklist_principal &&
2175 krb5_principal_compare(ctx, store_creds.client, blacklist_principal)) {
2179 code = krb5_unparse_name(ctx, blacklist_principal, &sp);
2183 code = krb5_unparse_name(ctx, impersonate_principal, &ip);
2187 DEBUG(1, ("smb_krb5_kinit_password_cache: "
2188 "KDC returned self principal[%s] while impersonating [%s]\n",
2189 sp?sp:"<no memory>",
2190 ip?ip:"<no memory>"));
2195 krb5_free_principal(ctx, whitelist_principal);
2196 krb5_free_principal(ctx, blacklist_principal);
2197 krb5_free_cred_contents(ctx, &store_creds);
2198 return KRB5_FWD_BAD_PRINCIPAL;
2200 if (blacklist_principal) {
2201 krb5_free_principal(ctx, blacklist_principal);
2204 if (whitelist_principal &&
2205 !krb5_principal_compare(ctx, store_creds.client, whitelist_principal)) {
2209 code = krb5_unparse_name(ctx, store_creds.client, &sp);
2213 code = krb5_unparse_name(ctx, whitelist_principal, &ep);
2217 DEBUG(1, ("smb_krb5_kinit_password_cache: "
2218 "KDC returned wrong principal[%s] we expected [%s]\n",
2219 sp?sp:"<no memory>",
2220 ep?ep:"<no memory>"));
2225 krb5_free_principal(ctx, whitelist_principal);
2226 krb5_free_cred_contents(ctx, &store_creds);
2227 return KRB5_FWD_BAD_PRINCIPAL;
2229 if (whitelist_principal) {
2230 krb5_free_principal(ctx, whitelist_principal);
2233 code = krb5_cc_initialize(ctx, store_cc, store_principal);
2235 krb5_free_cred_contents(ctx, &store_creds);
2239 code = krb5_cc_store_cred(ctx, store_cc, &store_creds);
2241 krb5_free_cred_contents(ctx, &store_creds);
2246 *expire_time = (time_t) store_creds.times.endtime;
2250 *kdc_time = (time_t) store_creds.times.starttime;
2253 krb5_free_cred_contents(ctx, &store_creds);
2259 #if !defined(HAVE_KRB5_MAKE_PRINCIPAL) && defined(HAVE_KRB5_BUILD_PRINCIPAL_ALLOC_VA)
2261 * @brief Create a principal name using a variable argument list.
2263 * @param[in] context The library context.
2265 * @param[inout] principal A pointer to the principal structure.
2267 * @param[in] _realm The realm to use. If NULL then the function will
2268 * get the default realm name.
2270 * @param[in] ... A list of 'char *' components, ending with NULL.
2272 * Use krb5_free_principal() to free the principal when it is no longer needed.
2274 * @return 0 on success, a Kerberos error code otherwise.
2276 krb5_error_code smb_krb5_make_principal(krb5_context context,
2277 krb5_principal *principal,
2278 const char *_realm, ...)
2280 krb5_error_code code;
2286 realm = discard_const_p(char, _realm);
2289 code = krb5_get_default_realm(context, &realm);
2296 va_start(ap, _realm);
2297 code = krb5_build_principal_alloc_va(context, principal,
2298 strlen(realm), realm,
2303 krb5_free_default_realm(context, realm);
2310 #if !defined(HAVE_KRB5_CC_GET_LIFETIME) && defined(HAVE_KRB5_CC_RETRIEVE_CRED)
2312 * @brief Get the lifetime of the initial ticket in the cache.
2314 * @param[in] context The kerberos context.
2316 * @param[in] id The credential cache to get the ticket lifetime.
2318 * @param[out] t A pointer to a time value to store the lifetime.
2320 * @return 0 on success, a krb5_error_code on error.
2322 krb5_error_code smb_krb5_cc_get_lifetime(krb5_context context,
2326 krb5_cc_cursor cursor;
2327 krb5_error_code kerr;
2333 kerr = krb5_timeofday(context, &now);
2338 kerr = krb5_cc_start_seq_get(context, id, &cursor);
2343 while ((kerr = krb5_cc_next_cred(context, id, &cursor, &cred)) == 0) {
2344 #ifndef HAVE_FLAGS_IN_KRB5_CREDS
2345 if (cred.ticket_flags & TKT_FLG_INITIAL) {
2347 if (cred.flags.b.initial) {
2349 if (now < cred.times.endtime) {
2350 *t = (time_t) (cred.times.endtime - now);
2352 krb5_free_cred_contents(context, &cred);
2355 krb5_free_cred_contents(context, &cred);
2358 krb5_cc_end_seq_get(context, id, &cursor);
2362 #endif /* HAVE_KRB5_CC_GET_LIFETIME */
2364 #if !defined(HAVE_KRB5_FREE_CHECKSUM_CONTENTS) && defined(HAVE_FREE_CHECKSUM)
2365 void smb_krb5_free_checksum_contents(krb5_context ctx, krb5_checksum *cksum)
2367 free_Checksum(cksum);
2372 * @brief Compute a checksum operating on a keyblock.
2374 * This function computes a checksum over a PAC using the keyblock for a keyed
2377 * @param[in] mem_ctx A talloc context to alocate the signature on.
2379 * @param[in] pac_data The PAC as input.
2381 * @param[in] context The library context.
2383 * @param[in] keyblock Encryption key for a keyed checksum.
2385 * @param[out] sig_type The checksum type
2387 * @param[out] sig_blob The talloc'ed checksum
2389 * The caller must free the sig_blob with talloc_free() when it is not needed
2392 * @return 0 on success, a Kerberos error code otherwise.
2394 krb5_error_code smb_krb5_make_pac_checksum(TALLOC_CTX *mem_ctx,
2395 DATA_BLOB *pac_data,
2396 krb5_context context,
2397 const krb5_keyblock *keyblock,
2399 DATA_BLOB *sig_blob)
2401 krb5_error_code ret;
2402 krb5_checksum cksum;
2403 #if defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CREATE_CHECKSUM)
2407 ret = krb5_crypto_init(context,
2412 DEBUG(0,("krb5_crypto_init() failed: %s\n",
2413 smb_get_krb5_error_message(context, ret, mem_ctx)));
2416 ret = krb5_create_checksum(context,
2418 KRB5_KU_OTHER_CKSUM,
2424 DEBUG(2, ("PAC Verification failed: %s\n",
2425 smb_get_krb5_error_message(context, ret, mem_ctx)));
2428 krb5_crypto_destroy(context, crypto);
2434 *sig_type = cksum.cksumtype;
2435 *sig_blob = data_blob_talloc(mem_ctx,
2436 cksum.checksum.data,
2437 cksum.checksum.length);
2438 #elif defined(HAVE_KRB5_C_MAKE_CHECKSUM)
2441 input.data = (char *)pac_data->data;
2442 input.length = pac_data->length;
2444 ret = krb5_c_make_checksum(context,
2447 KRB5_KEYUSAGE_APP_DATA_CKSUM,
2451 DEBUG(2, ("PAC Verification failed: %s\n",
2452 smb_get_krb5_error_message(context, ret, mem_ctx)));
2456 *sig_type = cksum.checksum_type;
2457 *sig_blob = data_blob_talloc(mem_ctx,
2462 #error krb5_create_checksum or krb5_c_make_checksum not available
2463 #endif /* HAVE_KRB5_C_MAKE_CHECKSUM */
2464 smb_krb5_free_checksum_contents(context, &cksum);
2471 * @brief Get realm of a principal
2473 * @param[in] context The library context
2475 * @param[in] principal The principal to get the realm from.
2477 * @return An allocated string with the realm or NULL if an error occured.
2479 * The caller must free the realm string with free() if not needed anymore.
2481 char *smb_krb5_principal_get_realm(krb5_context context,
2482 krb5_const_principal principal)
2484 #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
2485 return strdup(discard_const_p(char, krb5_principal_get_realm(context, principal)));
2486 #elif defined(krb5_princ_realm) /* MIT */
2488 realm = discard_const_p(krb5_data,
2489 krb5_princ_realm(context, principal));
2490 return strndup(realm->data, realm->length);
2492 #error UNKNOWN_GET_PRINC_REALM_FUNCTIONS
2497 * @brief Get realm of a principal
2499 * @param[in] context The library context
2501 * @param[in] principal The principal to set the realm
2503 * @param[in] realm The realm as a string to set.
2505 * @retur 0 on success, a Kerberos error code otherwise.
2507 krb5_error_code smb_krb5_principal_set_realm(krb5_context context,
2508 krb5_principal principal,
2511 #ifdef HAVE_KRB5_PRINCIPAL_SET_REALM /* Heimdal */
2512 return krb5_principal_set_realm(context, principal, realm);
2513 #elif defined(krb5_princ_realm) && defined(krb5_princ_set_realm) /* MIT */
2514 krb5_error_code ret;
2516 krb5_data *old_data;
2518 old_data = krb5_princ_realm(context, principal);
2520 ret = smb_krb5_copy_data_contents(&data,
2527 /* free realm before setting */
2528 free(old_data->data);
2530 krb5_princ_set_realm(context, principal, &data);
2534 #error UNKNOWN_PRINC_SET_REALM_FUNCTION
2539 /************************************************************************
2540 Routine to get the default realm from the kerberos credentials cache.
2541 Caller must free if the return value is not NULL.
2542 ************************************************************************/
2544 static char *smb_krb5_get_default_realm_from_ccache(TALLOC_CTX *mem_ctx)
2547 krb5_context ctx = NULL;
2548 krb5_ccache cc = NULL;
2549 krb5_principal princ = NULL;
2551 initialize_krb5_error_table();
2552 if (krb5_init_context(&ctx)) {
2556 DEBUG(5,("kerberos_get_default_realm_from_ccache: "
2557 "Trying to read krb5 cache: %s\n",
2558 krb5_cc_default_name(ctx)));
2559 if (krb5_cc_default(ctx, &cc)) {
2560 DEBUG(5,("kerberos_get_default_realm_from_ccache: "
2561 "failed to read default cache\n"));
2564 if (krb5_cc_get_principal(ctx, cc, &princ)) {
2565 DEBUG(5,("kerberos_get_default_realm_from_ccache: "
2566 "failed to get default principal\n"));
2570 #if defined(HAVE_KRB5_PRINCIPAL_GET_REALM)
2571 realm = talloc_strdup(mem_ctx, krb5_principal_get_realm(ctx, princ));
2572 #elif defined(HAVE_KRB5_PRINC_REALM)
2574 krb5_data *realm_data = krb5_princ_realm(ctx, princ);
2575 realm = talloc_strndup(mem_ctx, realm_data->data, realm_data->length);
2583 krb5_free_principal(ctx, princ);
2586 krb5_cc_close(ctx, cc);
2588 krb5_free_context(ctx);
2594 /************************************************************************
2595 Routine to get the realm from a given DNS name.
2596 ************************************************************************/
2598 static char *smb_krb5_get_realm_from_hostname(TALLOC_CTX *mem_ctx,
2599 const char *hostname)
2601 #if defined(HAVE_KRB5_REALM_TYPE)
2603 krb5_realm *realm_list = NULL;
2606 char **realm_list = NULL;
2609 krb5_error_code kerr;
2610 krb5_context ctx = NULL;
2612 initialize_krb5_error_table();
2613 if (krb5_init_context(&ctx)) {
2617 kerr = krb5_get_host_realm(ctx, hostname, &realm_list);
2619 DEBUG(3,("kerberos_get_realm_from_hostname %s: "
2621 hostname ? hostname : "(NULL)",
2622 error_message(kerr) ));
2626 if (realm_list && realm_list[0]) {
2627 realm = talloc_strdup(mem_ctx, realm_list[0]);
2634 krb5_free_host_realm(ctx, realm_list);
2637 krb5_free_context(ctx);
2644 * @brief Get the principal as a string from the service hostname.
2646 * @param[in] mem_ctx The talloc context
2648 * @param[in] service The service name
2650 * @param[in] remote_name The remote name
2652 * @param[in] default_realm The default_realm if we cannot get it from the
2653 * hostname or netbios name.
2655 * @return A talloc'ed principal string or NULL if an error occured.
2657 * The caller needs to free the principal with talloc_free() if it isn't needed
2660 char *smb_krb5_get_principal_from_service_hostname(TALLOC_CTX *mem_ctx,
2661 const char *service,
2662 const char *remote_name,
2663 const char *default_realm)
2668 host = strchr_m(remote_name, '.');
2671 realm = smb_krb5_get_realm_from_hostname(talloc_tos(),
2674 /* NetBIOS name - use our realm. */
2675 realm = smb_krb5_get_default_realm_from_ccache(talloc_tos());
2678 if (realm == NULL || *realm == '\0') {
2679 realm = talloc_strdup(talloc_tos(), default_realm);
2683 DEBUG(3,("Cannot get realm from, "
2684 "desthost %s or default ccache. Using default "
2685 "smb.conf realm %s\n",
2690 principal = talloc_asprintf(mem_ctx,
2692 service, remote_name,
2699 * @brief Get an error string from a Kerberos error code.
2701 * @param[in] context The library context.
2703 * @param[in] code The Kerberos error code.
2705 * @param[in] mem_ctx The talloc context to allocate the error string on.
2707 * @return A talloc'ed error string or NULL if an error occured.
2709 * The caller must free the returned error string with talloc_free() if not
2712 char *smb_get_krb5_error_message(krb5_context context,
2713 krb5_error_code code,
2714 TALLOC_CTX *mem_ctx)
2718 #if defined(HAVE_KRB5_GET_ERROR_MESSAGE) && defined(HAVE_KRB5_FREE_ERROR_MESSAGE)
2719 const char *context_error = krb5_get_error_message(context, code);
2720 if (context_error) {
2721 ret = talloc_asprintf(mem_ctx, "%s: %s",
2722 error_message(code), context_error);
2723 krb5_free_error_message(context, context_error);
2727 ret = talloc_strdup(mem_ctx, error_message(code));
2733 * @brief Return the kerberos library setting for: libdefaults:allow_weak_crypto
2735 * @param[in] context The library context
2737 * @return True if weak crypto is allowed, false if not.
2739 krb5_boolean smb_krb5_get_allowed_weak_crypto(krb5_context context)
2740 #if defined(HAVE_KRB5_CONFIG_GET_BOOL_DEFAULT)
2742 return krb5_config_get_bool_default(context,
2746 "allow_weak_crypto",
2749 #elif defined(HAVE_PROFILE_H) && defined(HAVE_KRB5_GET_PROFILE)
2751 #include <profile.h>
2752 krb5_error_code ret;
2753 krb5_boolean ret_default = false;
2757 ret = krb5_get_profile(context,
2763 ret = profile_get_boolean(profile,
2765 "allow_weak_crypto",
2766 NULL, /* subsubname */
2767 ret_default, /* def_val */
2768 &ret_profile /* *ret_default */);
2773 profile_release(profile);
2778 #error UNKNOWN_KRB5_CONFIG_ROUTINES
2782 * @brief Return the type of a krb5_principal
2784 * @param[in] context The library context.
2786 * @param[in] principal The principal to get the type from.
2788 * @return The integer type of the principal.
2790 int smb_krb5_principal_get_type(krb5_context context,
2791 krb5_const_principal principal)
2793 #ifdef HAVE_KRB5_PRINCIPAL_GET_TYPE /* Heimdal */
2794 return krb5_principal_get_type(context, principal);
2795 #elif defined(krb5_princ_type) /* MIT */
2796 return krb5_princ_type(context, principal);
2798 #error UNKNOWN_PRINC_GET_TYPE_FUNCTION
2803 * @brief Set the type of a principal
2805 * @param[in] context The library context
2807 * @param[inout] principal The principal to set the type for.
2809 * @param[in] type The principal type to set.
2811 void smb_krb5_principal_set_type(krb5_context context,
2812 krb5_principal principal,
2815 #ifdef HAVE_KRB5_PRINCIPAL_SET_TYPE /* Heimdal */
2816 krb5_principal_set_type(context, principal, type);
2817 #elif defined(krb5_princ_type) /* MIT */
2818 krb5_princ_type(context, principal) = type;
2820 #error UNKNOWN_PRINC_SET_TYPE_FUNCTION
2824 #if !defined(HAVE_KRB5_WARNX)
2826 * @brief Log a Kerberos message
2828 * It sends the message to com_err.
2830 * @param[in] context The library context
2832 * @param[in] fmt The message format
2834 * @param[in] ... The message arguments
2836 * @return 0 on success.
2838 krb5_error_code krb5_warnx(krb5_context context, const char *fmt, ...)
2842 va_start(args, fmt);
2843 com_err_va("samba-kdc", errno, fmt, args);
2851 * @brief Copy a credential cache.
2853 * @param[in] context The library context.
2855 * @param[in] incc Credential cache to be copied.
2857 * @param[inout] outcc Copy of credential cache to be filled in.
2859 * @return 0 on success, a Kerberos error code otherwise.
2861 krb5_error_code smb_krb5_cc_copy_creds(krb5_context context,
2862 krb5_ccache incc, krb5_ccache outcc)
2864 #ifdef HAVE_KRB5_CC_COPY_CACHE /* Heimdal */
2865 return krb5_cc_copy_cache(context, incc, outcc);
2866 #elif defined(HAVE_KRB5_CC_COPY_CREDS)
2867 return krb5_cc_copy_creds(context, incc, outcc);
2869 #error UNKNOWN_KRB5_CC_COPY_CACHE_OR_CREDS_FUNCTION
2873 /**********************************************************
2875 **********************************************************/
2877 static bool ads_cleanup_expired_creds(krb5_context context,
2881 krb5_error_code retval;
2882 const char *cc_type = krb5_cc_get_type(context, ccache);
2884 DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
2885 cc_type, krb5_cc_get_name(context, ccache),
2886 http_timestring(talloc_tos(), credsp->times.endtime)));
2888 /* we will probably need new tickets if the current ones
2889 will expire within 10 seconds.
2891 if (credsp->times.endtime >= (time(NULL) + 10))
2894 /* heimdal won't remove creds from a file ccache, and
2895 perhaps we shouldn't anyway, since internally we
2896 use memory ccaches, and a FILE one probably means that
2897 we're using creds obtained outside of our exectuable
2899 if (strequal(cc_type, "FILE")) {
2900 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type));
2904 retval = krb5_cc_remove_cred(context, ccache, 0, credsp);
2906 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
2907 error_message(retval)));
2908 /* If we have an error in this, we want to display it,
2909 but continue as though we deleted it */
2914 /* Allocate and setup the auth context into the state we need. */
2916 static krb5_error_code ads_setup_auth_context(krb5_context context,
2917 krb5_auth_context *auth_context)
2919 krb5_error_code retval;
2921 retval = krb5_auth_con_init(context, auth_context );
2923 DEBUG(1,("krb5_auth_con_init failed (%s)\n",
2924 error_message(retval)));
2928 /* Ensure this is an addressless ticket. */
2929 retval = krb5_auth_con_setaddrs(context, *auth_context, NULL, NULL);
2931 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n",
2932 error_message(retval)));
2938 #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)
2939 static krb5_error_code ads_create_gss_checksum(krb5_data *in_data, /* [inout] */
2942 unsigned int orig_length = in_data->length;
2943 unsigned int base_cksum_size = GSSAPI_CHECKSUM_SIZE;
2944 char *gss_cksum = NULL;
2947 /* Extra length field for delgated ticket. */
2948 base_cksum_size += 4;
2951 if ((unsigned int)base_cksum_size + orig_length <
2952 (unsigned int)base_cksum_size) {
2956 gss_cksum = (char *)SMB_MALLOC(base_cksum_size + orig_length);
2957 if (gss_cksum == NULL) {
2961 memset(gss_cksum, '\0', base_cksum_size + orig_length);
2962 SIVAL(gss_cksum, 0, GSSAPI_BNDLENGTH);
2965 * GSS_C_NO_CHANNEL_BINDINGS means 16 zero bytes.
2966 * This matches the behavior of heimdal and mit.
2968 * And it is needed to work against some closed source
2973 memset(&gss_cksum[4], 0x00, GSSAPI_BNDLENGTH);
2975 SIVAL(gss_cksum, 20, gss_flags);
2978 SSVAL(gss_cksum, 24, 1); /* The Delegation Option identifier */
2979 SSVAL(gss_cksum, 26, orig_length);
2980 /* Copy the kerberos KRB_CRED data */
2981 memcpy(gss_cksum + 28, in_data->data, orig_length);
2982 free(in_data->data);
2983 in_data->data = NULL;
2984 in_data->length = 0;
2986 in_data->data = gss_cksum;
2987 in_data->length = base_cksum_size + orig_length;
2993 * We can't use krb5_mk_req because w2k wants the service to be in a particular
2996 static krb5_error_code ads_krb5_mk_req(krb5_context context,
2997 krb5_auth_context *auth_context,
2998 const krb5_flags ap_req_options,
2999 const char *principal,
3002 time_t *expire_time,
3003 const char *impersonate_princ_s)
3005 krb5_error_code retval;
3006 krb5_principal server;
3007 krb5_principal impersonate_princ = NULL;
3011 bool creds_ready = false;
3012 int i = 0, maxtries = 3;
3015 ZERO_STRUCT(in_data);
3017 retval = smb_krb5_parse_name(context, principal, &server);
3019 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal));
3023 if (impersonate_princ_s) {
3024 retval = smb_krb5_parse_name(context, impersonate_princ_s,
3025 &impersonate_princ);
3027 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", impersonate_princ_s));
3032 /* obtain ticket & session key */
3034 if ((retval = krb5_copy_principal(context, server, &creds.server))) {
3035 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n",
3036 error_message(retval)));
3040 retval = krb5_cc_get_principal(context, ccache, &creds.client);
3042 /* This can commonly fail on smbd startup with no ticket in the cache.
3043 * Report at higher level than 1. */
3044 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
3045 error_message(retval)));
3049 while (!creds_ready && (i < maxtries)) {
3051 retval = smb_krb5_get_credentials(context,
3058 DBG_WARNING("smb_krb5_get_credentials failed for %s "
3061 error_message(retval));
3065 /* cope with ticket being in the future due to clock skew */
3066 if ((unsigned)credsp->times.starttime > time(NULL)) {
3067 time_t t = time(NULL);
3068 int time_offset =(int)((unsigned)credsp->times.starttime-t);
3069 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset));
3070 krb5_set_real_time(context, t + time_offset + 1, 0);
3073 ok = ads_cleanup_expired_creds(context, ccache, credsp);
3081 DBG_DEBUG("Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
3083 krb5_cc_get_type(context, ccache),
3084 krb5_cc_get_name(context, ccache),
3085 http_timestring(talloc_tos(),
3086 (unsigned)credsp->times.endtime),
3087 (unsigned)credsp->times.endtime);
3090 *expire_time = (time_t)credsp->times.endtime;
3093 /* Allocate the auth_context. */
3094 retval = ads_setup_auth_context(context, auth_context);
3096 DBG_WARNING("ads_setup_auth_context failed (%s)\n",
3097 error_message(retval));
3101 #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)
3103 uint32_t gss_flags = 0;
3105 if (credsp->ticket_flags & TKT_FLG_OK_AS_DELEGATE) {
3107 * Fetch a forwarded TGT from the KDC so that we can
3108 * hand off a 2nd ticket as part of the kerberos
3112 DBG_INFO("Server marked as OK to delegate to, building "
3113 "forwardable TGT\n");
3115 retval = krb5_auth_con_setuseruserkey(context,
3117 &credsp->keyblock );
3119 DBG_WARNING("krb5_auth_con_setuseruserkey "
3121 error_message(retval));
3125 /* Must use a subkey for forwarded tickets. */
3126 retval = krb5_auth_con_setflags(context,
3128 KRB5_AUTH_CONTEXT_USE_SUBKEY);
3130 DBG_WARNING("krb5_auth_con_setflags failed (%s)\n",
3131 error_message(retval));
3135 retval = krb5_fwd_tgt_creds(context,/* Krb5 context [in] */
3136 *auth_context, /* Authentication context [in] */
3137 discard_const_p(char, KRB5_TGS_NAME), /* Ticket service name ("krbtgt") [in] */
3138 credsp->client, /* Client principal for the tgt [in] */
3139 credsp->server, /* Server principal for the tgt [in] */
3140 ccache, /* Credential cache to use for storage [in] */
3141 1, /* Turn on for "Forwardable ticket" [in] */
3142 &in_data ); /* Resulting response [out] */
3145 DBG_INFO("krb5_fwd_tgt_creds failed (%s)\n",
3146 error_message(retval));
3149 * This is not fatal. Delete the *auth_context and continue
3150 * with krb5_mk_req_extended to get a non-forwardable ticket.
3154 free( in_data.data );
3155 in_data.data = NULL;
3158 krb5_auth_con_free(context, *auth_context);
3159 *auth_context = NULL;
3160 retval = ads_setup_auth_context(context, auth_context);
3162 DBG_WARNING("ads_setup_auth_context failed (%s)\n",
3163 error_message(retval));
3167 /* We got a delegated ticket. */
3168 gss_flags |= GSS_C_DELEG_FLAG;
3172 /* Frees and reallocates in_data into a GSS checksum blob. */
3173 retval = ads_create_gss_checksum(&in_data, gss_flags);
3178 /* We always want GSS-checksum types. */
3179 retval = krb5_auth_con_set_req_cksumtype(context, *auth_context, GSSAPI_CHECKSUM );
3181 DEBUG(1,("krb5_auth_con_set_req_cksumtype failed (%s)\n",
3182 error_message(retval)));
3188 retval = krb5_mk_req_extended(context, auth_context, ap_req_options,
3189 &in_data, credsp, outbuf);
3191 DBG_WARNING("krb5_mk_req_extended failed (%s)\n",
3192 error_message(retval));
3195 #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)
3200 free( in_data.data );
3204 krb5_free_creds(context, credsp);
3207 krb5_free_cred_contents(context, &creds);
3210 krb5_free_principal(context, server);
3211 if (impersonate_princ) {
3212 krb5_free_principal(context, impersonate_princ);
3219 get a kerberos5 ticket for the given service
3221 int ads_krb5_cli_get_ticket(TALLOC_CTX *mem_ctx,
3222 const char *principal,
3225 DATA_BLOB *session_key_krb5,
3226 uint32_t extra_ap_opts, const char *ccname,
3228 const char *impersonate_princ_s)
3230 krb5_error_code retval;
3232 krb5_context context = NULL;
3233 krb5_ccache ccdef = NULL;
3234 krb5_auth_context auth_context = NULL;
3235 krb5_enctype enc_types[] = {
3236 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
3237 ENCTYPE_AES256_CTS_HMAC_SHA1_96,
3239 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
3240 ENCTYPE_AES128_CTS_HMAC_SHA1_96,
3242 ENCTYPE_ARCFOUR_HMAC,
3243 ENCTYPE_DES_CBC_MD5,
3244 ENCTYPE_DES_CBC_CRC,
3248 initialize_krb5_error_table();
3249 retval = krb5_init_context(&context);
3251 DBG_WARNING("krb5_init_context failed (%s)\n",
3252 error_message(retval));
3256 if (time_offset != 0) {
3257 krb5_set_real_time(context, time(NULL) + time_offset, 0);
3260 retval = krb5_cc_resolve(context,
3261 ccname ? ccname : krb5_cc_default_name(context),
3264 DBG_WARNING("krb5_cc_default failed (%s)\n",
3265 error_message(retval));
3269 retval = krb5_set_default_tgs_ktypes(context, enc_types);
3271 DBG_WARNING("krb5_set_default_tgs_ktypes failed (%s)\n",
3272 error_message(retval));
3276 retval = ads_krb5_mk_req(context,
3278 AP_OPTS_USE_SUBKEY | (krb5_flags)extra_ap_opts,
3283 impersonate_princ_s);
3288 ok = smb_krb5_get_smb_session_key(mem_ctx,
3298 *ticket = data_blob_talloc(mem_ctx, packet.data, packet.length);
3300 smb_krb5_free_data_contents(context, &packet);
3306 krb5_cc_close(context, ccdef);
3309 krb5_auth_con_free(context, auth_context);
3311 krb5_free_context(context);
3317 #else /* HAVE_KRB5 */
3318 /* This saves a few linking headaches */
3319 int ads_krb5_cli_get_ticket(TALLOC_CTX *mem_ctx,
3320 const char *principal,
3323 DATA_BLOB *session_key_krb5,
3324 uint32_t extra_ap_opts, const char *ccname,
3326 const char *impersonate_princ_s)
3328 DEBUG(0,("NO KERBEROS SUPPORT\n"));
3332 #endif /* HAVE_KRB5 */