2 Unix SMB/CIFS implementation.
3 simple kerberos5 routines for active directory
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Luke Howard 2002-2003
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
7 Copyright (C) Guenther Deschner 2005-2009
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "system/filesys.h"
25 #include "krb5_samba.h"
26 #include "lib/util/asn1.h"
30 #endif /* HAVE_COM_ERR_H */
32 #ifndef KRB5_AUTHDATA_WIN2K_PAC
33 #define KRB5_AUTHDATA_WIN2K_PAC 128
36 #ifndef KRB5_AUTHDATA_IF_RELEVANT
37 #define KRB5_AUTHDATA_IF_RELEVANT 1
42 #define GSSAPI_CHECKSUM 0x8003 /* Checksum type value for Kerberos */
43 #define GSSAPI_BNDLENGTH 16 /* Bind Length (rfc-1964 pg.3) */
44 #define GSSAPI_CHECKSUM_SIZE (4+GSSAPI_BNDLENGTH+4) /* Length of bind length,
45 bind field, flags field. */
46 #define GSS_C_DELEG_FLAG 1
48 /* MIT krb5 1.7beta3 (in Ubuntu Karmic) is missing the prototype,
49 but still has the symbol */
50 #if !HAVE_DECL_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE
51 krb5_error_code krb5_auth_con_set_req_cksumtype(
53 krb5_auth_context auth_context,
54 krb5_cksumtype cksumtype);
57 #if !defined(SMB_MALLOC)
59 #define SMB_MALLOC(s) malloc((s))
63 #define SMB_STRDUP(s) strdup(s)
66 /**********************************************************
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 occured.
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 occured.
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,
212 krb5_error_code code = EINVAL;
213 #ifdef SAMBA4_USES_HEIMDAL
214 code = krb5_mk_error(context,
220 NULL, /* client_time */
221 NULL, /* client_usec */
224 krb5_error dec_err = {
228 if (e_text != NULL) {
229 dec_err.text.length = strlen(e_text);
230 dec_err.text.data = discard_const_p(char, e_text);
232 if (e_data != NULL) {
233 dec_err.e_data = *e_data;
236 code = krb5_mk_error(context,
244 * @brief Create a keyblock based on input parameters
246 * @param context The krb5_context
247 * @param host_princ The krb5_principal to use
248 * @param salt The optional salt, if omitted, salt is calculated with
249 * the provided principal.
250 * @param password The krb5_data containing the password
251 * @param enctype The krb5_enctype to use for the keyblock generation
252 * @param key The returned krb5_keyblock, caller needs to free with
253 * krb5_free_keyblock().
255 * @return krb5_error_code
257 int smb_krb5_create_key_from_string(krb5_context context,
258 krb5_const_principal host_princ,
261 krb5_enctype enctype,
266 if (host_princ == NULL && salt == NULL) {
270 #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_C_STRING_TO_KEY)
275 ret = krb5_principal2salt(context, host_princ, &_salt);
277 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
283 ret = krb5_c_string_to_key(context, enctype, password, &_salt, key);
285 SAFE_FREE(_salt.data);
288 #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT)
293 ret = krb5_get_pw_salt(context, host_princ, &_salt);
295 DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret)));
299 _salt.saltvalue = *salt;
300 _salt.salttype = KRB5_PW_SALT;
303 ret = krb5_string_to_key_salt(context, enctype, (const char *)password->data, _salt, key);
305 krb5_free_salt(context, _salt);
309 #error UNKNOWN_CREATE_KEY_FUNCTIONS
315 * @brief Create a salt for a given principal
317 * @param context The initialized krb5_context
318 * @param host_princ The krb5_principal to create the salt for
319 * @param psalt A pointer to a krb5_data struct
321 * caller has to free the contents of psalt with smb_krb5_free_data_contents
322 * when function has succeeded
324 * @return krb5_error_code, returns 0 on success, error code otherwise
327 int smb_krb5_get_pw_salt(krb5_context context,
328 krb5_const_principal host_princ,
330 #if defined(HAVE_KRB5_GET_PW_SALT)
336 ret = krb5_get_pw_salt(context, host_princ, &salt);
341 psalt->data = salt.saltvalue.data;
342 psalt->length = salt.saltvalue.length;
346 #elif defined(HAVE_KRB5_PRINCIPAL2SALT)
349 return krb5_principal2salt(context, host_princ, psalt);
352 #error UNKNOWN_SALT_FUNCTIONS
355 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
357 * @brief Get a list of encryption types allowed for session keys
359 * @param[in] context The library context
361 * @param[in] enctypes An allocated, zero-terminated list of encryption types
363 * This function returns an allocated list of encryption types allowed for
366 * Use free() to free the enctypes when it is no longer needed.
368 * @retval 0 Success; otherwise - Kerberos error codes
370 krb5_error_code smb_krb5_get_allowed_etypes(krb5_context context,
371 krb5_enctype **enctypes)
373 return krb5_get_permitted_enctypes(context, enctypes);
375 #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES)
376 krb5_error_code smb_krb5_get_allowed_etypes(krb5_context context,
377 krb5_enctype **enctypes)
379 #ifdef HAVE_KRB5_PDU_NONE_DECL
380 return krb5_get_default_in_tkt_etypes(context, KRB5_PDU_NONE, enctypes);
382 return krb5_get_default_in_tkt_etypes(context, enctypes);
386 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
389 bool unwrap_edata_ntstatus(TALLOC_CTX *mem_ctx,
391 DATA_BLOB *edata_out)
393 DATA_BLOB edata_contents;
397 if (!edata->length) {
401 data = asn1_init(mem_ctx);
406 if (!asn1_load(data, *edata)) goto err;
407 if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) goto err;
408 if (!asn1_start_tag(data, ASN1_CONTEXT(1))) goto err;
409 if (!asn1_read_Integer(data, &edata_type)) goto err;
411 if (edata_type != KRB5_PADATA_PW_SALT) {
412 DEBUG(0,("edata is not of required type %d but of type %d\n",
413 KRB5_PADATA_PW_SALT, edata_type));
417 if (!asn1_start_tag(data, ASN1_CONTEXT(2))) goto err;
418 if (!asn1_read_OctetString(data, talloc_tos(), &edata_contents)) goto err;
419 if (!asn1_end_tag(data)) goto err;
420 if (!asn1_end_tag(data)) goto err;
421 if (!asn1_end_tag(data)) goto err;
424 *edata_out = data_blob_talloc(mem_ctx, edata_contents.data, edata_contents.length);
426 data_blob_free(&edata_contents);
438 * @brief Convert a string principal name to a Kerberos principal.
440 * @param[in] context The library context
442 * @param[in] name The principal as a unix charset string.
444 * @param[out] principal The newly allocated principal.
446 * Use krb5_free_principal() to free a principal when it is no longer needed.
448 * @return 0 on success, a Kerberos error code otherwise.
450 krb5_error_code smb_krb5_parse_name(krb5_context context,
452 krb5_principal *principal)
456 size_t converted_size;
457 TALLOC_CTX *frame = talloc_stackframe();
459 if (!push_utf8_talloc(frame, &utf8_name, name, &converted_size)) {
464 ret = krb5_parse_name(context, utf8_name, principal);
470 * @brief Convert a Kerberos principal structure to a string representation.
472 * The resulting string representation will be a unix charset name and is
475 * @param[in] mem_ctx The talloc context to allocate memory on.
477 * @param[in] context The library context.
479 * @param[in] principal The principal.
481 * @param[out] unix_name A string representation of the princpial name as with
484 * Use talloc_free() to free the string representation if it is no longer
487 * @return 0 on success, a Kerberos error code otherwise.
489 krb5_error_code smb_krb5_unparse_name(TALLOC_CTX *mem_ctx,
490 krb5_context context,
491 krb5_const_principal principal,
496 size_t converted_size;
499 ret = krb5_unparse_name(context, principal, &utf8_name);
504 if (!pull_utf8_talloc(mem_ctx, unix_name, utf8_name, &converted_size)) {
505 krb5_free_unparsed_name(context, utf8_name);
508 krb5_free_unparsed_name(context, utf8_name);
512 bool smb_krb5_principal_compare_any_realm(krb5_context context,
513 krb5_const_principal princ1,
514 krb5_const_principal princ2)
516 return krb5_principal_compare_any_realm(context, princ1, princ2);
520 * @brief Free the contents of a krb5_data structure and zero the data field.
522 * @param[in] context The krb5 context
524 * @param[in] pdata The data structure to free contents of
526 * This function frees the contents, not the structure itself.
528 void smb_krb5_free_data_contents(krb5_context context, krb5_data *pdata)
530 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
532 krb5_free_data_contents(context, pdata);
534 #elif defined(HAVE_KRB5_DATA_FREE)
535 krb5_data_free(context, pdata);
537 SAFE_FREE(pdata->data);
542 * @brief copy a buffer into a krb5_data struct
544 * @param[in] p The krb5_data
545 * @param[in] data The data to copy
546 * @param[in] length The length of the data to copy
547 * @return krb5_error_code
549 * Caller has to free krb5_data with smb_krb5_free_data_contents().
551 krb5_error_code smb_krb5_copy_data_contents(krb5_data *p,
555 #if defined(HAVE_KRB5_DATA_COPY)
556 return krb5_data_copy(p, data, len);
559 p->data = malloc(len);
560 if (p->data == NULL) {
563 memmove(p->data, data, len);
568 p->magic = KV5M_DATA;
573 bool smb_krb5_get_smb_session_key(TALLOC_CTX *mem_ctx,
574 krb5_context context,
575 krb5_auth_context auth_context,
576 DATA_BLOB *session_key,
579 krb5_keyblock *skey = NULL;
580 krb5_error_code err = 0;
584 #ifdef HAVE_KRB5_AUTH_CON_GETRECVSUBKEY
585 err = krb5_auth_con_getrecvsubkey(context,
588 #else /* HAVE_KRB5_AUTH_CON_GETRECVSUBKEY */
589 err = krb5_auth_con_getremotesubkey(context,
590 auth_context, &skey);
591 #endif /* HAVE_KRB5_AUTH_CON_GETRECVSUBKEY */
593 #ifdef HAVE_KRB5_AUTH_CON_GETSENDSUBKEY
594 err = krb5_auth_con_getsendsubkey(context,
597 #else /* HAVE_KRB5_AUTH_CON_GETSENDSUBKEY */
598 err = krb5_auth_con_getlocalsubkey(context,
599 auth_context, &skey);
600 #endif /* HAVE_KRB5_AUTH_CON_GETSENDSUBKEY */
603 if (err || skey == NULL) {
604 DEBUG(10, ("KRB5 error getting session key %d\n", err));
608 DEBUG(10, ("Got KRB5 session key of length %d\n",
609 (int)KRB5_KEY_LENGTH(skey)));
611 *session_key = data_blob_talloc(mem_ctx,
613 KRB5_KEY_LENGTH(skey));
614 dump_data_pw("KRB5 Session Key:\n",
616 session_key->length);
622 krb5_free_keyblock(context, skey);
630 * @brief Get talloced string component of a principal
632 * @param[in] mem_ctx The TALLOC_CTX
633 * @param[in] context The krb5_context
634 * @param[in] principal The principal
635 * @param[in] component The component
636 * @return string component
638 * Caller must talloc_free if the return value is not NULL.
641 char *smb_krb5_principal_get_comp_string(TALLOC_CTX *mem_ctx,
642 krb5_context context,
643 krb5_const_principal principal,
644 unsigned int component)
646 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
647 return talloc_strdup(mem_ctx, krb5_principal_get_comp_string(context, principal, component));
651 if (component >= krb5_princ_size(context, principal)) {
655 data = krb5_princ_component(context, principal, component);
660 return talloc_strndup(mem_ctx, data->data, data->length);
667 * @param[in] ccache_string A string pointing to the cache to renew the ticket
668 * (e.g. FILE:/tmp/krb5cc_0) or NULL. If the principal
669 * ccache has not been specified, the default ccache
672 * @param[in] client_string The client principal string (e.g. user@SAMBA.SITE)
673 * or NULL. If the principal string has not been
674 * specified, the principal from the ccache will be
677 * @param[in] service_string The service ticket string
678 * (e.g. krbtgt/SAMBA.SITE@SAMBA.SITE) or NULL. If
679 * the sevice ticket is specified, it is parsed (
680 * with the realm part ignored) and used as the
681 * server principal of the credential. Otherwise
682 * the ticket-granting service is used.
684 * @param[in] expire_time A pointer to store the credentials end time or
687 * @return 0 on Succes, a Kerberos error code otherwise.
689 krb5_error_code smb_krb5_renew_ticket(const char *ccache_string,
690 const char *client_string,
691 const char *service_string,
695 krb5_context context = NULL;
696 krb5_ccache ccache = NULL;
697 krb5_principal client = NULL;
698 krb5_creds creds, creds_in;
701 ZERO_STRUCT(creds_in);
703 initialize_krb5_error_table();
704 ret = krb5_init_context(&context);
709 if (!ccache_string) {
710 ccache_string = krb5_cc_default_name(context);
713 if (!ccache_string) {
718 DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string));
720 /* FIXME: we should not fall back to defaults */
721 ret = krb5_cc_resolve(context, discard_const_p(char, ccache_string), &ccache);
727 ret = smb_krb5_parse_name(context, client_string, &client);
732 ret = krb5_cc_get_principal(context, ccache, &client);
738 ret = krb5_get_renewed_creds(context, &creds, client, ccache, discard_const_p(char, service_string));
740 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
744 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
745 ret = krb5_cc_initialize(context, ccache, client);
750 ret = krb5_cc_store_cred(context, ccache, &creds);
753 *expire_time = (time_t) creds.times.endtime;
757 krb5_free_cred_contents(context, &creds_in);
758 krb5_free_cred_contents(context, &creds);
761 krb5_free_principal(context, client);
764 krb5_cc_close(context, ccache);
767 krb5_free_context(context);
774 * @brief Free the data stored in an smb_krb5_addresses structure.
776 * @param[in] context The library context
778 * @param[in] addr The address structure to free.
780 * @return 0 on success, a Kerberos error code otherwise.
782 krb5_error_code smb_krb5_free_addresses(krb5_context context,
783 smb_krb5_addresses *addr)
785 krb5_error_code ret = 0;
789 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
790 krb5_free_addresses(context, addr->addrs);
791 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
792 ret = krb5_free_addresses(context, addr->addrs);
793 SAFE_FREE(addr->addrs);
800 #define MAX_NETBIOSNAME_LEN 16
803 * @brief Add a netbios name to the array of addresses
805 * @param[in] kerb_addr A pointer to the smb_krb5_addresses to add the
808 * @param[in] netbios_name The netbios name to add.
810 * @return 0 on success, a Kerberos error code otherwise.
812 krb5_error_code smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses **kerb_addr,
813 const char *netbios_name)
815 krb5_error_code ret = 0;
816 char buf[MAX_NETBIOSNAME_LEN];
818 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
819 krb5_address **addrs = NULL;
820 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
821 krb5_addresses *addrs = NULL;
824 *kerb_addr = (smb_krb5_addresses *)SMB_MALLOC(sizeof(smb_krb5_addresses));
825 if (*kerb_addr == NULL) {
829 /* temporarily duplicate put_name() code here to avoid dependency
830 * issues for a 5 lines function */
831 len = strlen(netbios_name);
832 memcpy(buf, netbios_name,
833 (len < MAX_NETBIOSNAME_LEN) ? len : MAX_NETBIOSNAME_LEN - 1);
834 if (len < MAX_NETBIOSNAME_LEN - 1) {
835 memset(buf + len, ' ', MAX_NETBIOSNAME_LEN - 1 - len);
837 buf[MAX_NETBIOSNAME_LEN - 1] = 0x20;
839 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
843 addrs = (krb5_address **)SMB_MALLOC(sizeof(krb5_address *) * num_addr);
845 SAFE_FREE(*kerb_addr);
849 memset(addrs, 0, sizeof(krb5_address *) * num_addr);
851 addrs[0] = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
852 if (addrs[0] == NULL) {
854 SAFE_FREE(*kerb_addr);
858 addrs[0]->magic = KV5M_ADDRESS;
859 addrs[0]->addrtype = KRB5_ADDR_NETBIOS;
860 addrs[0]->length = MAX_NETBIOSNAME_LEN;
861 addrs[0]->contents = (unsigned char *)SMB_MALLOC(addrs[0]->length);
862 if (addrs[0]->contents == NULL) {
865 SAFE_FREE(*kerb_addr);
869 memcpy(addrs[0]->contents, buf, addrs[0]->length);
873 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
875 addrs = (krb5_addresses *)SMB_MALLOC(sizeof(krb5_addresses));
877 SAFE_FREE(*kerb_addr);
881 memset(addrs, 0, sizeof(krb5_addresses));
884 addrs->val = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
885 if (addrs->val == NULL) {
887 SAFE_FREE(kerb_addr);
891 addrs->val[0].addr_type = KRB5_ADDR_NETBIOS;
892 addrs->val[0].address.length = MAX_NETBIOSNAME_LEN;
893 addrs->val[0].address.data = (unsigned char *)SMB_MALLOC(addrs->val[0].address.length);
894 if (addrs->val[0].address.data == NULL) {
895 SAFE_FREE(addrs->val);
897 SAFE_FREE(*kerb_addr);
901 memcpy(addrs->val[0].address.data, buf, addrs->val[0].address.length);
904 #error UNKNOWN_KRB5_ADDRESS_FORMAT
906 (*kerb_addr)->addrs = addrs;
912 * @brief Get the enctype from a key table entry
914 * @param[in] kt_entry Key table entry to get the enctype from.
916 * @return The enctype from the entry.
918 krb5_enctype smb_krb5_kt_get_enctype_from_entry(krb5_keytab_entry *kt_entry)
920 return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry));
924 * @brief Free the contents of a key table entry.
926 * @param[in] context The library context.
928 * @param[in] kt_entry The key table entry to free the contents of.
930 * @return 0 on success, a Kerberos error code otherwise.
932 * The pointer itself is not freed.
934 krb5_error_code smb_krb5_kt_free_entry(krb5_context context,
935 krb5_keytab_entry *kt_entry)
937 /* Try krb5_free_keytab_entry_contents first, since
938 * MIT Kerberos >= 1.7 has both krb5_free_keytab_entry_contents and
939 * krb5_kt_free_entry but only has a prototype for the first, while the
940 * second is considered private.
942 #if defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
943 return krb5_free_keytab_entry_contents(context, kt_entry);
944 #elif defined(HAVE_KRB5_KT_FREE_ENTRY)
945 return krb5_kt_free_entry(context, kt_entry);
947 #error UNKNOWN_KT_FREE_FUNCTION
953 * @brief Convert an encryption type to a string.
955 * @param[in] context The library context.
957 * @param[in] enctype The encryption type.
959 * @param[in] etype_s A pointer to store the allocated encryption type as a
962 * @return 0 on success, a Kerberos error code otherwise.
964 * The caller needs to free the allocated string etype_s.
966 krb5_error_code smb_krb5_enctype_to_string(krb5_context context,
967 krb5_enctype enctype,
970 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
971 return krb5_enctype_to_string(context, enctype, etype_s); /* Heimdal */
972 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
974 krb5_error_code ret = krb5_enctype_to_string(enctype, buf, 256); /* MIT */
978 *etype_s = SMB_STRDUP(buf);
984 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
988 /* This MAX_NAME_LEN is a constant defined in krb5.h */
989 #ifndef MAX_KEYTAB_NAME_LEN
990 #define MAX_KEYTAB_NAME_LEN 1100
994 * @brief Open a key table readonly or with readwrite access.
996 * Allows to use a different keytab than the default one using a relative
997 * path to the keytab.
999 * @param[in] context The library context
1001 * @param[in] keytab_name_req The path to the key table.
1003 * @param[in] write_access Open with readwrite access.
1005 * @param[in] keytab A pointer o the opended key table.
1007 * The keytab pointer should be freed using krb5_kt_close().
1009 * @return 0 on success, a Kerberos error code otherwise.
1011 krb5_error_code smb_krb5_kt_open_relative(krb5_context context,
1012 const char *keytab_name_req,
1014 krb5_keytab *keytab)
1016 krb5_error_code ret = 0;
1017 TALLOC_CTX *mem_ctx;
1018 char keytab_string[MAX_KEYTAB_NAME_LEN];
1019 char *kt_str = NULL;
1020 bool found_valid_name = false;
1021 const char *pragma = "FILE";
1022 const char *tmp = NULL;
1024 if (!write_access && !keytab_name_req) {
1025 /* caller just wants to read the default keytab readonly, so be it */
1026 return krb5_kt_default(context, keytab);
1029 mem_ctx = talloc_init("smb_krb5_open_keytab");
1034 #ifdef HAVE_WRFILE_KEYTAB
1040 if (keytab_name_req) {
1042 if (strlen(keytab_name_req) > MAX_KEYTAB_NAME_LEN) {
1043 ret = KRB5_CONFIG_NOTENUFSPACE;
1047 if ((strncmp(keytab_name_req, "WRFILE:/", 8) == 0) ||
1048 (strncmp(keytab_name_req, "FILE:/", 6) == 0)) {
1049 tmp = keytab_name_req;
1053 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, keytab_name_req);
1062 /* we need to handle more complex keytab_strings, like:
1063 * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
1065 ret = krb5_kt_default_name(context, &keytab_string[0], MAX_KEYTAB_NAME_LEN - 2);
1070 DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string));
1072 tmp = talloc_strdup(mem_ctx, keytab_string);
1078 if (strncmp(tmp, "ANY:", 4) == 0) {
1082 memset(&keytab_string, '\0', sizeof(keytab_string));
1084 while (next_token_talloc(mem_ctx, &tmp, &kt_str, ",")) {
1085 if (strncmp(kt_str, "WRFILE:", 7) == 0) {
1086 found_valid_name = true;
1091 if (strncmp(kt_str, "FILE:", 5) == 0) {
1092 found_valid_name = true;
1097 if (tmp[0] == '/') {
1098 /* Treat as a FILE: keytab definition. */
1099 found_valid_name = true;
1102 if (found_valid_name) {
1103 if (tmp[0] != '/') {
1104 ret = KRB5_KT_BADNAME;
1108 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, tmp);
1117 if (!found_valid_name) {
1118 ret = KRB5_KT_UNKNOWN_TYPE;
1123 DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp));
1124 ret = krb5_kt_resolve(context, tmp, keytab);
1127 TALLOC_FREE(mem_ctx);
1132 * @brief Open a key table readonly or with readwrite access.
1134 * Allows to use a different keytab than the default one. The path needs to be
1135 * an absolute path or an error will be returned.
1137 * @param[in] context The library context
1139 * @param[in] keytab_name_req The path to the key table.
1141 * @param[in] write_access Open with readwrite access.
1143 * @param[in] keytab A pointer o the opended key table.
1145 * The keytab pointer should be freed using krb5_kt_close().
1147 * @return 0 on success, a Kerberos error code otherwise.
1149 krb5_error_code smb_krb5_kt_open(krb5_context context,
1150 const char *keytab_name_req,
1152 krb5_keytab *keytab)
1154 if (keytab_name_req != NULL) {
1155 if (keytab_name_req[0] != '/') {
1156 return KRB5_KT_BADNAME;
1160 return smb_krb5_kt_open_relative(context,
1167 * @brief Get a key table name.
1169 * @param[in] mem_ctx The talloc context to use for allocation.
1171 * @param[in] context The library context.
1173 * @param[in] keytab The key table to get the name from.
1175 * @param[in] keytab_name A talloc'ed string of the key table name.
1177 * The talloc'ed name string needs to be freed with talloc_free().
1179 * @return 0 on success, a Kerberos error code otherwise.
1181 krb5_error_code smb_krb5_kt_get_name(TALLOC_CTX *mem_ctx,
1182 krb5_context context,
1184 const char **keytab_name)
1186 char keytab_string[MAX_KEYTAB_NAME_LEN];
1187 krb5_error_code ret = 0;
1189 ret = krb5_kt_get_name(context, keytab,
1190 keytab_string, MAX_KEYTAB_NAME_LEN - 2);
1195 *keytab_name = talloc_strdup(mem_ctx, keytab_string);
1196 if (!*keytab_name) {
1204 * @brief Seek and delete old entries in a keytab based on the passed
1207 * @param[in] context The KRB5 context to use.
1209 * @param[in] keytab The keytab to operate on.
1211 * @param[in] kvno The kvnco to use.
1213 * @param[in] princ_s The principal as a string to search for.
1215 * @param[in] princ The principal as a krb5_principal to search for.
1217 * @param[in] flush Weather to flush the complete keytab.
1219 * @param[in] keep_old_entries Keep the entry with the previous kvno.
1221 * @retval 0 on Sucess
1223 * @return An appropriate KRB5 error code.
1225 krb5_error_code smb_krb5_kt_seek_and_delete_old_entries(krb5_context context,
1228 krb5_enctype enctype,
1229 const char *princ_s,
1230 krb5_principal princ,
1232 bool keep_old_entries)
1234 krb5_error_code ret;
1235 krb5_kt_cursor cursor;
1236 krb5_kt_cursor zero_csr;
1237 krb5_keytab_entry kt_entry;
1238 krb5_keytab_entry zero_kt_entry;
1239 char *ktprinc = NULL;
1240 krb5_kvno old_kvno = kvno - 1;
1241 TALLOC_CTX *tmp_ctx;
1243 ZERO_STRUCT(cursor);
1244 ZERO_STRUCT(zero_csr);
1245 ZERO_STRUCT(kt_entry);
1246 ZERO_STRUCT(zero_kt_entry);
1248 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
1249 if (ret == KRB5_KT_END || ret == ENOENT ) {
1254 tmp_ctx = talloc_new(NULL);
1255 if (tmp_ctx == NULL) {
1259 DEBUG(3, (__location__ ": Will try to delete old keytab entries\n"));
1260 while (!krb5_kt_next_entry(context, keytab, &kt_entry, &cursor)) {
1261 bool name_ok = false;
1262 krb5_enctype kt_entry_enctype =
1263 smb_krb5_kt_get_enctype_from_entry(&kt_entry);
1265 if (!flush && (princ_s != NULL)) {
1266 ret = smb_krb5_unparse_name(tmp_ctx, context,
1270 DEBUG(1, (__location__
1271 ": smb_krb5_unparse_name failed "
1272 "(%s)\n", error_message(ret)));
1276 #ifdef HAVE_KRB5_KT_COMPARE
1277 name_ok = krb5_kt_compare(context, &kt_entry,
1280 name_ok = (strcmp(ktprinc, princ_s) == 0);
1284 DEBUG(10, (__location__ ": ignoring keytab "
1285 "entry principal %s, kvno = %d\n",
1286 ktprinc, kt_entry.vno));
1289 * just free this entry and continue. */
1290 ret = smb_krb5_kt_free_entry(context,
1292 ZERO_STRUCT(kt_entry);
1294 DEBUG(1, (__location__
1295 ": smb_krb5_kt_free_entry "
1297 error_message(ret)));
1301 TALLOC_FREE(ktprinc);
1305 TALLOC_FREE(ktprinc);
1308 /*------------------------------------------------------------
1309 * Save the entries with kvno - 1. This is what microsoft does
1310 * to allow people with existing sessions that have kvno - 1
1311 * to still work. Otherwise, when the password for the machine
1312 * changes, all kerberizied sessions will 'break' until either
1313 * the client reboots or the client's session key expires and
1314 * they get a new session ticket with the new kvno.
1315 * Some keytab files only store the kvno in 8bits, limit
1316 * the compare accordingly.
1319 if (!flush && ((kt_entry.vno & 0xff) == (old_kvno & 0xff))) {
1320 DEBUG(5, (__location__ ": Saving previous (kvno %d) "
1321 "entry for principal: %s.\n",
1322 old_kvno, princ_s));
1326 if (keep_old_entries) {
1327 DEBUG(5, (__location__ ": Saving old (kvno %d) "
1328 "entry for principal: %s.\n",
1334 (kt_entry.vno == kvno) &&
1335 (kt_entry_enctype != enctype))
1337 DEBUG(5, (__location__ ": Saving entry with kvno [%d] "
1338 "enctype [%d] for principal: %s.\n",
1339 kvno, kt_entry_enctype, princ_s));
1343 DEBUG(5, (__location__ ": Found old entry for principal: %s "
1344 "(kvno %d) - trying to remove it.\n",
1345 princ_s, kt_entry.vno));
1347 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
1348 ZERO_STRUCT(cursor);
1350 DEBUG(1, (__location__ ": krb5_kt_end_seq_get() "
1351 "failed (%s)\n", error_message(ret)));
1354 ret = krb5_kt_remove_entry(context, keytab, &kt_entry);
1356 DEBUG(1, (__location__ ": krb5_kt_remove_entry() "
1357 "failed (%s)\n", error_message(ret)));
1361 DEBUG(5, (__location__ ": removed old entry for principal: "
1362 "%s (kvno %d).\n", princ_s, kt_entry.vno));
1364 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
1366 DEBUG(1, (__location__ ": krb5_kt_start_seq() failed "
1367 "(%s)\n", error_message(ret)));
1370 ret = smb_krb5_kt_free_entry(context, &kt_entry);
1371 ZERO_STRUCT(kt_entry);
1373 DEBUG(1, (__location__ ": krb5_kt_remove_entry() "
1374 "failed (%s)\n", error_message(ret)));
1380 talloc_free(tmp_ctx);
1381 if (memcmp(&zero_kt_entry, &kt_entry, sizeof(krb5_keytab_entry))) {
1382 smb_krb5_kt_free_entry(context, &kt_entry);
1384 if (memcmp(&cursor, &zero_csr, sizeof(krb5_kt_cursor)) != 0) {
1385 krb5_kt_end_seq_get(context, keytab, &cursor);
1391 * @brief Add a keytab entry for the given principal
1393 * @param[in] context The krb5 context to use.
1395 * @param[in] keytab The keytab to add the entry to.
1397 * @param[in] kvno The kvno to use.
1399 * @param[in] princ_s The principal as a string.
1401 * @param[in] salt_principal The salt principal to salt the password with.
1402 * Only needed for keys which support salting.
1403 * If no salt is used set no_salt to false and
1406 * @param[in] enctype The encryption type of the keytab entry.
1408 * @param[in] password The password of the keytab entry.
1410 * @param[in] no_salt If the password should not be salted. Normally
1411 * this is only set to false for encryption types
1412 * which do not support salting like RC4.
1414 * @param[in] keep_old_entries Wether to keep or delte old keytab entries.
1416 * @retval 0 on Success
1418 * @return A corresponding KRB5 error code.
1420 * @see smb_krb5_kt_open()
1422 krb5_error_code smb_krb5_kt_add_entry(krb5_context context,
1425 const char *princ_s,
1426 const char *salt_principal,
1427 krb5_enctype enctype,
1428 krb5_data *password,
1430 bool keep_old_entries)
1432 krb5_error_code ret;
1433 krb5_keytab_entry kt_entry;
1434 krb5_principal princ = NULL;
1435 krb5_keyblock *keyp;
1437 ZERO_STRUCT(kt_entry);
1439 ret = smb_krb5_parse_name(context, princ_s, &princ);
1441 DEBUG(1, (__location__ ": smb_krb5_parse_name(%s) "
1442 "failed (%s)\n", princ_s, error_message(ret)));
1446 /* Seek and delete old keytab entries */
1447 ret = smb_krb5_kt_seek_and_delete_old_entries(context,
1459 /* If we get here, we have deleted all the old entries with kvno's
1460 * not equal to the current kvno-1. */
1462 keyp = KRB5_KT_KEY(&kt_entry);
1465 KRB5_KEY_DATA(keyp) = (KRB5_KEY_DATA_CAST *)SMB_MALLOC(password->length);
1466 if (KRB5_KEY_DATA(keyp) == NULL) {
1470 memcpy(KRB5_KEY_DATA(keyp), password->data, password->length);
1471 KRB5_KEY_LENGTH(keyp) = password->length;
1472 KRB5_KEY_TYPE(keyp) = enctype;
1474 krb5_principal salt_princ = NULL;
1476 /* Now add keytab entries for all encryption types */
1477 ret = smb_krb5_parse_name(context, salt_principal, &salt_princ);
1479 DBG_WARNING("krb5_parse_name(%s) failed (%s)\n",
1480 salt_principal, error_message(ret));
1484 ret = smb_krb5_create_key_from_string(context,
1490 krb5_free_principal(context, salt_princ);
1496 kt_entry.principal = princ;
1497 kt_entry.vno = kvno;
1499 DEBUG(3, (__location__ ": adding keytab entry for (%s) with "
1500 "encryption type (%d) and version (%d)\n",
1501 princ_s, enctype, kt_entry.vno));
1502 ret = krb5_kt_add_entry(context, keytab, &kt_entry);
1503 krb5_free_keyblock_contents(context, keyp);
1504 ZERO_STRUCT(kt_entry);
1506 DEBUG(1, (__location__ ": adding entry to keytab "
1507 "failed (%s)\n", error_message(ret)));
1513 krb5_free_principal(context, princ);
1519 #if defined(HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE) && \
1520 defined(HAVE_KRB5_GET_CREDS_OPT_ALLOC) && \
1521 defined(HAVE_KRB5_GET_CREDS)
1522 static krb5_error_code smb_krb5_get_credentials_for_user_opt(krb5_context context,
1525 krb5_principal server,
1526 krb5_principal impersonate_princ,
1527 krb5_creds **out_creds)
1529 krb5_error_code ret;
1530 krb5_get_creds_opt opt;
1532 ret = krb5_get_creds_opt_alloc(context, &opt);
1536 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_FORWARDABLE);
1538 if (impersonate_princ) {
1539 ret = krb5_get_creds_opt_set_impersonate(context, opt,
1546 ret = krb5_get_creds(context, opt, ccache, server, out_creds);
1553 krb5_get_creds_opt_free(context, opt);
1557 #endif /* HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE */
1559 #ifdef HAVE_KRB5_GET_CREDENTIALS_FOR_USER
1561 #if !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER
1562 krb5_error_code KRB5_CALLCONV
1563 krb5_get_credentials_for_user(krb5_context context, krb5_flags options,
1564 krb5_ccache ccache, krb5_creds *in_creds,
1565 krb5_data *subject_cert,
1566 krb5_creds **out_creds);
1567 #endif /* !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER */
1569 static krb5_error_code smb_krb5_get_credentials_for_user(krb5_context context,
1572 krb5_principal server,
1573 krb5_principal impersonate_princ,
1574 krb5_creds **out_creds)
1576 krb5_error_code ret;
1577 krb5_creds in_creds;
1579 ZERO_STRUCT(in_creds);
1581 if (impersonate_princ) {
1583 in_creds.server = me;
1584 in_creds.client = impersonate_princ;
1586 ret = krb5_get_credentials_for_user(context,
1587 0, /* krb5_flags options */
1590 NULL, /* krb5_data *subject_cert */
1593 in_creds.client = me;
1594 in_creds.server = server;
1596 ret = krb5_get_credentials(context, 0, ccache,
1597 &in_creds, out_creds);
1602 #endif /* HAVE_KRB5_GET_CREDENTIALS_FOR_USER */
1605 * smb_krb5_get_credentials
1607 * @brief Get krb5 credentials for a server
1609 * @param[in] context An initialized krb5_context
1610 * @param[in] ccache An initialized krb5_ccache
1611 * @param[in] me The krb5_principal of the caller
1612 * @param[in] server The krb5_principal of the requested service
1613 * @param[in] impersonate_princ The krb5_principal of a user to impersonate as (optional)
1614 * @param[out] out_creds The returned krb5_creds structure
1615 * @return krb5_error_code
1618 krb5_error_code smb_krb5_get_credentials(krb5_context context,
1621 krb5_principal server,
1622 krb5_principal impersonate_princ,
1623 krb5_creds **out_creds)
1625 krb5_error_code ret;
1626 krb5_creds *creds = NULL;
1628 if (out_creds != NULL) {
1632 if (impersonate_princ) {
1633 #ifdef HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE /* Heimdal */
1634 ret = smb_krb5_get_credentials_for_user_opt(context, ccache, me, server, impersonate_princ, &creds);
1635 #elif defined(HAVE_KRB5_GET_CREDENTIALS_FOR_USER) /* MIT */
1636 ret = smb_krb5_get_credentials_for_user(context, ccache, me, server, impersonate_princ, &creds);
1641 krb5_creds in_creds;
1643 ZERO_STRUCT(in_creds);
1645 in_creds.client = me;
1646 in_creds.server = server;
1648 ret = krb5_get_credentials(context, 0, ccache,
1661 krb5_free_creds(context, creds);
1668 * @brief Initialize a krb5_keyblock with the given data.
1670 * Initialized a new keyblock, allocates the contents fo the key and
1671 * copies the data into the keyblock.
1673 * @param[in] context The library context
1675 * @param[in] enctype The encryption type.
1677 * @param[in] data The date to initialize the keyblock with.
1679 * @param[in] length The length of the keyblock.
1681 * @param[in] key Newly allocated keyblock structure.
1683 * The key date must be freed using krb5_free_keyblock_contents() when it is
1686 * @return 0 on success, a Kerberos error code otherwise.
1688 krb5_error_code smb_krb5_keyblock_init_contents(krb5_context context,
1689 krb5_enctype enctype,
1694 #if defined(HAVE_KRB5_KEYBLOCK_INIT)
1695 return krb5_keyblock_init(context, enctype, data, length, key);
1696 #elif defined(HAVE_KRB5_INIT_KEYBLOCK)
1697 krb5_error_code code;
1699 code = krb5_init_keyblock(context,
1708 memcpy(KRB5_KEY_DATA(key), data, length);
1713 memset(key, 0, sizeof(krb5_keyblock));
1714 KRB5_KEY_DATA(key) = SMB_MALLOC(length);
1715 if (NULL == KRB5_KEY_DATA(key)) {
1718 memcpy(KRB5_KEY_DATA(key), data, length);
1719 KRB5_KEY_LENGTH(key) = length;
1720 KRB5_KEY_TYPE(key) = enctype;
1726 * @brief Simulate a kinit by putting the tgt in the given credential cache.
1728 * This function uses a keyblock rather than needingthe original password.
1730 * @param[in] ctx The library context
1732 * @param[in] cc The credential cache to put the tgt in.
1734 * @param[in] principal The client princial
1736 * @param[in] keyblock The keyblock to use.
1738 * @param[in] target_service The service name of the initial credentials (or NULL).
1740 * @param[in] krb_options Initial credential options.
1742 * @param[in] expire_time A pointer to store the experation time of the
1743 * credentials (or NULL).
1745 * @param[in] kdc_time A pointer to store the time when the ticket becomes
1748 * @return 0 on success, a Kerberos error code otherwise.
1750 krb5_error_code smb_krb5_kinit_keyblock_ccache(krb5_context ctx,
1752 krb5_principal principal,
1753 krb5_keyblock *keyblock,
1754 const char *target_service,
1755 krb5_get_init_creds_opt *krb_options,
1756 time_t *expire_time,
1759 krb5_error_code code = 0;
1760 krb5_creds my_creds;
1762 #if defined(HAVE_KRB5_GET_INIT_CREDS_KEYBLOCK)
1763 code = krb5_get_init_creds_keyblock(ctx, &my_creds, principal,
1764 keyblock, 0, target_service,
1766 #elif defined(HAVE_KRB5_GET_INIT_CREDS_KEYTAB)
1768 #define SMB_CREDS_KEYTAB "MEMORY:tmp_smb_creds_XXXXXX"
1769 char tmp_name[sizeof(SMB_CREDS_KEYTAB)];
1770 krb5_keytab_entry entry;
1774 memset(&entry, 0, sizeof(entry));
1775 entry.principal = principal;
1776 *(KRB5_KT_KEY(&entry)) = *keyblock;
1778 memcpy(tmp_name, SMB_CREDS_KEYTAB, sizeof(SMB_CREDS_KEYTAB));
1779 mask = umask(S_IRWXO | S_IRWXG);
1782 if (tmp_name[0] == 0) {
1783 return KRB5_KT_BADNAME;
1785 code = krb5_kt_resolve(ctx, tmp_name, &keytab);
1790 code = krb5_kt_add_entry(ctx, keytab, &entry);
1792 (void)krb5_kt_close(ctx, keytab);
1796 code = krb5_get_init_creds_keytab(ctx, &my_creds, principal,
1797 keytab, 0, target_service,
1799 (void)krb5_kt_close(ctx, keytab);
1802 #error krb5_get_init_creds_keyblock not available!
1808 #ifndef SAMBA4_USES_HEIMDAL /* MIT */
1810 * We need to store the principal as returned from the KDC to the
1811 * credentials cache. If we don't do that the KRB5 library is not
1812 * able to find the tickets it is looking for
1814 principal = my_creds.client;
1816 code = krb5_cc_initialize(ctx, cc, principal);
1821 code = krb5_cc_store_cred(ctx, cc, &my_creds);
1827 *expire_time = (time_t) my_creds.times.endtime;
1831 *kdc_time = (time_t) my_creds.times.starttime;
1836 krb5_free_cred_contents(ctx, &my_creds);
1841 * @brief Simulate a kinit by putting the tgt in the given credential cache.
1843 * @param[in] ctx The library context
1845 * @param[in] cc The credential cache to put the tgt in.
1847 * @param[in] principal The client princial
1849 * @param[in] password The password (or NULL).
1851 * @param[in] target_service The service name of the initial credentials (or NULL).
1853 * @param[in] krb_options Initial credential options.
1855 * @param[in] expire_time A pointer to store the experation time of the
1856 * credentials (or NULL).
1858 * @param[in] kdc_time A pointer to store the time when the ticket becomes
1861 * @return 0 on success, a Kerberos error code otherwise.
1863 krb5_error_code smb_krb5_kinit_password_ccache(krb5_context ctx,
1865 krb5_principal principal,
1866 const char *password,
1867 const char *target_service,
1868 krb5_get_init_creds_opt *krb_options,
1869 time_t *expire_time,
1872 krb5_error_code code = 0;
1873 krb5_creds my_creds;
1875 code = krb5_get_init_creds_password(ctx, &my_creds, principal,
1876 password, NULL, NULL, 0,
1877 target_service, krb_options);
1882 #ifndef SAMBA4_USES_HEIMDAL /* MIT */
1884 * We need to store the principal as returned from the KDC to the
1885 * credentials cache. If we don't do that the KRB5 library is not
1886 * able to find the tickets it is looking for
1888 principal = my_creds.client;
1890 code = krb5_cc_initialize(ctx, cc, principal);
1895 code = krb5_cc_store_cred(ctx, cc, &my_creds);
1901 *expire_time = (time_t) my_creds.times.endtime;
1905 *kdc_time = (time_t) my_creds.times.starttime;
1910 krb5_free_cred_contents(ctx, &my_creds);
1914 #ifdef SAMBA4_USES_HEIMDAL
1916 * @brief Simulate a kinit by putting the tgt in the given credential cache.
1918 * @param[in] ctx The library context
1920 * @param[in] cc The credential cache to store the tgt in.
1922 * @param[in] principal The initial client princial.
1924 * @param[in] password The password (or NULL).
1926 * @param[in] impersonate_principal The impersonatiion principal (or NULL).
1928 * @param[in] self_service The local service for S4U2Self if
1929 * impersonate_principal is specified).
1931 * @param[in] target_service The service name of the initial credentials
1932 * (kpasswd/REALM or a remote service). It defaults
1933 * to the krbtgt if NULL.
1935 * @param[in] krb_options Initial credential options.
1937 * @param[in] expire_time A pointer to store the experation time of the
1938 * credentials (or NULL).
1940 * @param[in] kdc_time A pointer to store the time when the ticket becomes
1943 * @return 0 on success, a Kerberos error code otherwise.
1945 krb5_error_code smb_krb5_kinit_s4u2_ccache(krb5_context ctx,
1946 krb5_ccache store_cc,
1947 krb5_principal init_principal,
1948 const char *init_password,
1949 krb5_principal impersonate_principal,
1950 const char *self_service,
1951 const char *target_service,
1952 krb5_get_init_creds_opt *krb_options,
1953 time_t *expire_time,
1956 krb5_error_code code = 0;
1957 krb5_get_creds_opt options;
1958 krb5_principal store_principal;
1959 krb5_creds store_creds;
1960 krb5_creds *s4u2self_creds;
1961 Ticket s4u2self_ticket;
1962 size_t s4u2self_ticketlen;
1963 krb5_creds *s4u2proxy_creds;
1964 krb5_principal self_princ;
1966 krb5_principal target_princ;
1968 const char *self_realm;
1969 krb5_principal blacklist_principal = NULL;
1970 krb5_principal whitelist_principal = NULL;
1972 code = krb5_get_init_creds_password(ctx, &store_creds,
1983 store_principal = init_principal;
1986 * We are trying S4U2Self now:
1988 * As we do not want to expose our TGT in the
1989 * krb5_ccache, which is also holds the impersonated creds.
1991 * Some low level krb5/gssapi function might use the TGT
1992 * identity and let the client act as our machine account.
1994 * We need to avoid that and use a temporary krb5_ccache
1995 * in order to pass our TGT to the krb5_get_creds() function.
1997 code = krb5_cc_new_unique(ctx, NULL, NULL, &tmp_cc);
1999 krb5_free_cred_contents(ctx, &store_creds);
2003 code = krb5_cc_initialize(ctx, tmp_cc, store_creds.client);
2005 krb5_cc_destroy(ctx, tmp_cc);
2006 krb5_free_cred_contents(ctx, &store_creds);
2010 code = krb5_cc_store_cred(ctx, tmp_cc, &store_creds);
2012 krb5_free_cred_contents(ctx, &store_creds);
2013 krb5_cc_destroy(ctx, tmp_cc);
2018 * we need to remember the client principal of our
2019 * TGT and make sure the KDC does not return this
2020 * in the impersonated tickets. This can happen
2021 * if the KDC does not support S4U2Self and S4U2Proxy.
2023 blacklist_principal = store_creds.client;
2024 store_creds.client = NULL;
2025 krb5_free_cred_contents(ctx, &store_creds);
2028 * Check if we also need S4U2Proxy or if S4U2Self is
2029 * enough in order to get a ticket for the target.
2031 if (target_service == NULL) {
2033 } else if (strcmp(target_service, self_service) == 0) {
2040 * For S4U2Self we need our own service principal,
2041 * which belongs to our own realm (available on
2042 * our client principal).
2044 self_realm = krb5_principal_get_realm(ctx, init_principal);
2046 code = krb5_parse_name(ctx, self_service, &self_princ);
2048 krb5_free_principal(ctx, blacklist_principal);
2049 krb5_cc_destroy(ctx, tmp_cc);
2053 code = krb5_principal_set_realm(ctx, self_princ, self_realm);
2055 krb5_free_principal(ctx, blacklist_principal);
2056 krb5_free_principal(ctx, self_princ);
2057 krb5_cc_destroy(ctx, tmp_cc);
2061 code = krb5_get_creds_opt_alloc(ctx, &options);
2063 krb5_free_principal(ctx, blacklist_principal);
2064 krb5_free_principal(ctx, self_princ);
2065 krb5_cc_destroy(ctx, tmp_cc);
2071 * If we want S4U2Proxy, we need the forwardable flag
2072 * on the S4U2Self ticket.
2074 krb5_get_creds_opt_set_options(ctx, options, KRB5_GC_FORWARDABLE);
2077 code = krb5_get_creds_opt_set_impersonate(ctx, options,
2078 impersonate_principal);
2080 krb5_get_creds_opt_free(ctx, options);
2081 krb5_free_principal(ctx, blacklist_principal);
2082 krb5_free_principal(ctx, self_princ);
2083 krb5_cc_destroy(ctx, tmp_cc);
2087 code = krb5_get_creds(ctx, options, tmp_cc,
2088 self_princ, &s4u2self_creds);
2089 krb5_get_creds_opt_free(ctx, options);
2090 krb5_free_principal(ctx, self_princ);
2092 krb5_free_principal(ctx, blacklist_principal);
2093 krb5_cc_destroy(ctx, tmp_cc);
2098 krb5_cc_destroy(ctx, tmp_cc);
2101 * Now make sure we store the impersonated principal
2102 * and creds instead of the TGT related stuff
2103 * in the krb5_ccache of the caller.
2105 code = krb5_copy_creds_contents(ctx, s4u2self_creds,
2107 krb5_free_creds(ctx, s4u2self_creds);
2113 * It's important to store the principal the KDC
2114 * returned, as otherwise the caller would not find
2115 * the S4U2Self ticket in the krb5_ccache lookup.
2117 store_principal = store_creds.client;
2122 * We are trying S4U2Proxy:
2124 * We need the ticket from the S4U2Self step
2125 * and our TGT in order to get the delegated ticket.
2127 code = decode_Ticket((const uint8_t *)s4u2self_creds->ticket.data,
2128 s4u2self_creds->ticket.length,
2130 &s4u2self_ticketlen);
2132 krb5_free_creds(ctx, s4u2self_creds);
2133 krb5_free_principal(ctx, blacklist_principal);
2134 krb5_cc_destroy(ctx, tmp_cc);
2139 * we need to remember the client principal of the
2140 * S4U2Self stage and as it needs to match the one we
2141 * will get for the S4U2Proxy stage. We need this
2142 * in order to detect KDCs which does not support S4U2Proxy.
2144 whitelist_principal = s4u2self_creds->client;
2145 s4u2self_creds->client = NULL;
2146 krb5_free_creds(ctx, s4u2self_creds);
2149 * For S4U2Proxy we also got a target service principal,
2150 * which also belongs to our own realm (available on
2151 * our client principal).
2153 code = krb5_parse_name(ctx, target_service, &target_princ);
2155 free_Ticket(&s4u2self_ticket);
2156 krb5_free_principal(ctx, whitelist_principal);
2157 krb5_free_principal(ctx, blacklist_principal);
2158 krb5_cc_destroy(ctx, tmp_cc);
2162 code = krb5_principal_set_realm(ctx, target_princ, self_realm);
2164 free_Ticket(&s4u2self_ticket);
2165 krb5_free_principal(ctx, target_princ);
2166 krb5_free_principal(ctx, whitelist_principal);
2167 krb5_free_principal(ctx, blacklist_principal);
2168 krb5_cc_destroy(ctx, tmp_cc);
2172 code = krb5_get_creds_opt_alloc(ctx, &options);
2174 free_Ticket(&s4u2self_ticket);
2175 krb5_free_principal(ctx, target_princ);
2176 krb5_free_principal(ctx, whitelist_principal);
2177 krb5_free_principal(ctx, blacklist_principal);
2178 krb5_cc_destroy(ctx, tmp_cc);
2182 krb5_get_creds_opt_set_options(ctx, options, KRB5_GC_FORWARDABLE);
2183 krb5_get_creds_opt_set_options(ctx, options, KRB5_GC_CONSTRAINED_DELEGATION);
2185 code = krb5_get_creds_opt_set_ticket(ctx, options, &s4u2self_ticket);
2186 free_Ticket(&s4u2self_ticket);
2188 krb5_get_creds_opt_free(ctx, options);
2189 krb5_free_principal(ctx, target_princ);
2190 krb5_free_principal(ctx, whitelist_principal);
2191 krb5_free_principal(ctx, blacklist_principal);
2192 krb5_cc_destroy(ctx, tmp_cc);
2196 code = krb5_get_creds(ctx, options, tmp_cc,
2197 target_princ, &s4u2proxy_creds);
2198 krb5_get_creds_opt_free(ctx, options);
2199 krb5_free_principal(ctx, target_princ);
2200 krb5_cc_destroy(ctx, tmp_cc);
2202 krb5_free_principal(ctx, whitelist_principal);
2203 krb5_free_principal(ctx, blacklist_principal);
2208 * Now make sure we store the impersonated principal
2209 * and creds instead of the TGT related stuff
2210 * in the krb5_ccache of the caller.
2212 code = krb5_copy_creds_contents(ctx, s4u2proxy_creds,
2214 krb5_free_creds(ctx, s4u2proxy_creds);
2216 krb5_free_principal(ctx, whitelist_principal);
2217 krb5_free_principal(ctx, blacklist_principal);
2222 * It's important to store the principal the KDC
2223 * returned, as otherwise the caller would not find
2224 * the S4U2Self ticket in the krb5_ccache lookup.
2226 store_principal = store_creds.client;
2229 if (blacklist_principal &&
2230 krb5_principal_compare(ctx, store_creds.client, blacklist_principal)) {
2234 code = krb5_unparse_name(ctx, blacklist_principal, &sp);
2238 code = krb5_unparse_name(ctx, impersonate_principal, &ip);
2242 DEBUG(1, ("smb_krb5_kinit_password_cache: "
2243 "KDC returned self principal[%s] while impersonating [%s]\n",
2244 sp?sp:"<no memory>",
2245 ip?ip:"<no memory>"));
2250 krb5_free_principal(ctx, whitelist_principal);
2251 krb5_free_principal(ctx, blacklist_principal);
2252 krb5_free_cred_contents(ctx, &store_creds);
2253 return KRB5_FWD_BAD_PRINCIPAL;
2255 if (blacklist_principal) {
2256 krb5_free_principal(ctx, blacklist_principal);
2259 if (whitelist_principal &&
2260 !krb5_principal_compare(ctx, store_creds.client, whitelist_principal)) {
2264 code = krb5_unparse_name(ctx, store_creds.client, &sp);
2268 code = krb5_unparse_name(ctx, whitelist_principal, &ep);
2272 DEBUG(1, ("smb_krb5_kinit_password_cache: "
2273 "KDC returned wrong principal[%s] we expected [%s]\n",
2274 sp?sp:"<no memory>",
2275 ep?ep:"<no memory>"));
2280 krb5_free_principal(ctx, whitelist_principal);
2281 krb5_free_cred_contents(ctx, &store_creds);
2282 return KRB5_FWD_BAD_PRINCIPAL;
2284 if (whitelist_principal) {
2285 krb5_free_principal(ctx, whitelist_principal);
2288 code = krb5_cc_initialize(ctx, store_cc, store_principal);
2290 krb5_free_cred_contents(ctx, &store_creds);
2294 code = krb5_cc_store_cred(ctx, store_cc, &store_creds);
2296 krb5_free_cred_contents(ctx, &store_creds);
2301 *expire_time = (time_t) store_creds.times.endtime;
2305 *kdc_time = (time_t) store_creds.times.starttime;
2308 krb5_free_cred_contents(ctx, &store_creds);
2314 #if !defined(HAVE_KRB5_MAKE_PRINCIPAL) && defined(HAVE_KRB5_BUILD_PRINCIPAL_ALLOC_VA)
2316 * @brief Create a principal name using a variable argument list.
2318 * @param[in] context The library context.
2320 * @param[inout] principal A pointer to the principal structure.
2322 * @param[in] _realm The realm to use. If NULL then the function will
2323 * get the default realm name.
2325 * @param[in] ... A list of 'char *' components, ending with NULL.
2327 * Use krb5_free_principal() to free the principal when it is no longer needed.
2329 * @return 0 on success, a Kerberos error code otherwise.
2331 krb5_error_code smb_krb5_make_principal(krb5_context context,
2332 krb5_principal *principal,
2333 const char *_realm, ...)
2335 krb5_error_code code;
2341 realm = discard_const_p(char, _realm);
2344 code = krb5_get_default_realm(context, &realm);
2351 va_start(ap, _realm);
2352 code = krb5_build_principal_alloc_va(context, principal,
2353 strlen(realm), realm,
2358 krb5_free_default_realm(context, realm);
2365 #if !defined(HAVE_KRB5_CC_GET_LIFETIME) && defined(HAVE_KRB5_CC_RETRIEVE_CRED)
2367 * @brief Get the lifetime of the initial ticket in the cache.
2369 * @param[in] context The kerberos context.
2371 * @param[in] id The credential cache to get the ticket lifetime.
2373 * @param[out] t A pointer to a time value to store the lifetime.
2375 * @return 0 on success, a krb5_error_code on error.
2377 krb5_error_code smb_krb5_cc_get_lifetime(krb5_context context,
2381 krb5_cc_cursor cursor;
2382 krb5_error_code kerr;
2388 kerr = krb5_timeofday(context, &now);
2393 kerr = krb5_cc_start_seq_get(context, id, &cursor);
2398 while ((kerr = krb5_cc_next_cred(context, id, &cursor, &cred)) == 0) {
2399 #ifndef HAVE_FLAGS_IN_KRB5_CREDS
2400 if (cred.ticket_flags & TKT_FLG_INITIAL) {
2402 if (cred.flags.b.initial) {
2404 if (now < cred.times.endtime) {
2405 *t = (time_t) (cred.times.endtime - now);
2407 krb5_free_cred_contents(context, &cred);
2410 krb5_free_cred_contents(context, &cred);
2413 krb5_cc_end_seq_get(context, id, &cursor);
2417 #endif /* HAVE_KRB5_CC_GET_LIFETIME */
2419 #if !defined(HAVE_KRB5_FREE_CHECKSUM_CONTENTS) && defined(HAVE_FREE_CHECKSUM)
2420 void smb_krb5_free_checksum_contents(krb5_context ctx, krb5_checksum *cksum)
2422 free_Checksum(cksum);
2427 * @brief Compute a checksum operating on a keyblock.
2429 * This function computes a checksum over a PAC using the keyblock for a keyed
2432 * @param[in] mem_ctx A talloc context to alocate the signature on.
2434 * @param[in] pac_data The PAC as input.
2436 * @param[in] context The library context.
2438 * @param[in] keyblock Encryption key for a keyed checksum.
2440 * @param[out] sig_type The checksum type
2442 * @param[out] sig_blob The talloc'ed checksum
2444 * The caller must free the sig_blob with talloc_free() when it is not needed
2447 * @return 0 on success, a Kerberos error code otherwise.
2449 krb5_error_code smb_krb5_make_pac_checksum(TALLOC_CTX *mem_ctx,
2450 DATA_BLOB *pac_data,
2451 krb5_context context,
2452 const krb5_keyblock *keyblock,
2454 DATA_BLOB *sig_blob)
2456 krb5_error_code ret;
2457 krb5_checksum cksum;
2458 #if defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CREATE_CHECKSUM)
2462 ret = krb5_crypto_init(context,
2467 DEBUG(0,("krb5_crypto_init() failed: %s\n",
2468 smb_get_krb5_error_message(context, ret, mem_ctx)));
2471 ret = krb5_create_checksum(context,
2473 KRB5_KU_OTHER_CKSUM,
2479 DEBUG(2, ("PAC Verification failed: %s\n",
2480 smb_get_krb5_error_message(context, ret, mem_ctx)));
2483 krb5_crypto_destroy(context, crypto);
2489 *sig_type = cksum.cksumtype;
2490 *sig_blob = data_blob_talloc(mem_ctx,
2491 cksum.checksum.data,
2492 cksum.checksum.length);
2493 #elif defined(HAVE_KRB5_C_MAKE_CHECKSUM)
2496 input.data = (char *)pac_data->data;
2497 input.length = pac_data->length;
2499 ret = krb5_c_make_checksum(context,
2502 KRB5_KEYUSAGE_APP_DATA_CKSUM,
2506 DEBUG(2, ("PAC Verification failed: %s\n",
2507 smb_get_krb5_error_message(context, ret, mem_ctx)));
2511 *sig_type = cksum.checksum_type;
2512 *sig_blob = data_blob_talloc(mem_ctx,
2517 #error krb5_create_checksum or krb5_c_make_checksum not available
2518 #endif /* HAVE_KRB5_C_MAKE_CHECKSUM */
2519 smb_krb5_free_checksum_contents(context, &cksum);
2526 * @brief Get realm of a principal
2528 * @param[in] context The library context
2530 * @param[in] principal The principal to get the realm from.
2532 * @return An allocated string with the realm or NULL if an error occured.
2534 * The caller must free the realm string with free() if not needed anymore.
2536 char *smb_krb5_principal_get_realm(krb5_context context,
2537 krb5_const_principal principal)
2539 #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
2540 return strdup(discard_const_p(char, krb5_principal_get_realm(context, principal)));
2541 #elif defined(krb5_princ_realm) /* MIT */
2543 realm = discard_const_p(krb5_data,
2544 krb5_princ_realm(context, principal));
2545 return strndup(realm->data, realm->length);
2547 #error UNKNOWN_GET_PRINC_REALM_FUNCTIONS
2552 * @brief Get realm of a principal
2554 * @param[in] context The library context
2556 * @param[in] principal The principal to set the realm
2558 * @param[in] realm The realm as a string to set.
2560 * @retur 0 on success, a Kerberos error code otherwise.
2562 krb5_error_code smb_krb5_principal_set_realm(krb5_context context,
2563 krb5_principal principal,
2566 #ifdef HAVE_KRB5_PRINCIPAL_SET_REALM /* Heimdal */
2567 return krb5_principal_set_realm(context, principal, realm);
2568 #elif defined(krb5_princ_realm) && defined(krb5_princ_set_realm) /* MIT */
2569 krb5_error_code ret;
2571 krb5_data *old_data;
2573 old_data = krb5_princ_realm(context, principal);
2575 ret = smb_krb5_copy_data_contents(&data,
2582 /* free realm before setting */
2583 free(old_data->data);
2585 krb5_princ_set_realm(context, principal, &data);
2589 #error UNKNOWN_PRINC_SET_REALM_FUNCTION
2594 /************************************************************************
2595 Routine to get the default realm from the kerberos credentials cache.
2596 Caller must free if the return value is not NULL.
2597 ************************************************************************/
2599 static char *smb_krb5_get_default_realm_from_ccache(TALLOC_CTX *mem_ctx)
2602 krb5_context ctx = NULL;
2603 krb5_ccache cc = NULL;
2604 krb5_principal princ = NULL;
2606 initialize_krb5_error_table();
2607 if (krb5_init_context(&ctx)) {
2611 DEBUG(5,("kerberos_get_default_realm_from_ccache: "
2612 "Trying to read krb5 cache: %s\n",
2613 krb5_cc_default_name(ctx)));
2614 if (krb5_cc_default(ctx, &cc)) {
2615 DEBUG(5,("kerberos_get_default_realm_from_ccache: "
2616 "failed to read default cache\n"));
2619 if (krb5_cc_get_principal(ctx, cc, &princ)) {
2620 DEBUG(5,("kerberos_get_default_realm_from_ccache: "
2621 "failed to get default principal\n"));
2625 #if defined(HAVE_KRB5_PRINCIPAL_GET_REALM)
2626 realm = talloc_strdup(mem_ctx, krb5_principal_get_realm(ctx, princ));
2627 #elif defined(HAVE_KRB5_PRINC_REALM)
2629 krb5_data *realm_data = krb5_princ_realm(ctx, princ);
2630 realm = talloc_strndup(mem_ctx, realm_data->data, realm_data->length);
2638 krb5_free_principal(ctx, princ);
2641 krb5_cc_close(ctx, cc);
2643 krb5_free_context(ctx);
2649 /************************************************************************
2650 Routine to get the realm from a given DNS name.
2651 ************************************************************************/
2653 static char *smb_krb5_get_realm_from_hostname(TALLOC_CTX *mem_ctx,
2654 const char *hostname)
2656 #if defined(HAVE_KRB5_REALM_TYPE)
2658 krb5_realm *realm_list = NULL;
2661 char **realm_list = NULL;
2664 krb5_error_code kerr;
2665 krb5_context ctx = NULL;
2667 initialize_krb5_error_table();
2668 if (krb5_init_context(&ctx)) {
2672 kerr = krb5_get_host_realm(ctx, hostname, &realm_list);
2674 DEBUG(3,("kerberos_get_realm_from_hostname %s: "
2676 hostname ? hostname : "(NULL)",
2677 error_message(kerr) ));
2681 if (realm_list && realm_list[0]) {
2682 realm = talloc_strdup(mem_ctx, realm_list[0]);
2689 krb5_free_host_realm(ctx, realm_list);
2692 krb5_free_context(ctx);
2699 * @brief Get the principal as a string from the service hostname.
2701 * @param[in] mem_ctx The talloc context
2703 * @param[in] service The service name
2705 * @param[in] remote_name The remote name
2707 * @param[in] default_realm The default_realm if we cannot get it from the
2708 * hostname or netbios name.
2710 * @return A talloc'ed principal string or NULL if an error occured.
2712 * The caller needs to free the principal with talloc_free() if it isn't needed
2715 char *smb_krb5_get_principal_from_service_hostname(TALLOC_CTX *mem_ctx,
2716 const char *service,
2717 const char *remote_name,
2718 const char *default_realm)
2723 host = strchr_m(remote_name, '.');
2726 realm = smb_krb5_get_realm_from_hostname(talloc_tos(),
2729 /* NetBIOS name - use our realm. */
2730 realm = smb_krb5_get_default_realm_from_ccache(talloc_tos());
2733 if (realm == NULL || *realm == '\0') {
2734 realm = talloc_strdup(talloc_tos(), default_realm);
2738 DEBUG(3,("Cannot get realm from, "
2739 "desthost %s or default ccache. Using default "
2740 "smb.conf realm %s\n",
2745 principal = talloc_asprintf(mem_ctx,
2747 service, remote_name,
2754 * @brief Get an error string from a Kerberos error code.
2756 * @param[in] context The library context.
2758 * @param[in] code The Kerberos error code.
2760 * @param[in] mem_ctx The talloc context to allocate the error string on.
2762 * @return A talloc'ed error string or NULL if an error occured.
2764 * The caller must free the returned error string with talloc_free() if not
2767 char *smb_get_krb5_error_message(krb5_context context,
2768 krb5_error_code code,
2769 TALLOC_CTX *mem_ctx)
2773 #if defined(HAVE_KRB5_GET_ERROR_MESSAGE) && defined(HAVE_KRB5_FREE_ERROR_MESSAGE)
2774 const char *context_error = krb5_get_error_message(context, code);
2775 if (context_error) {
2776 ret = talloc_asprintf(mem_ctx, "%s: %s",
2777 error_message(code), context_error);
2778 krb5_free_error_message(context, context_error);
2782 ret = talloc_strdup(mem_ctx, error_message(code));
2788 * @brief Return the kerberos library setting for: libdefaults:allow_weak_crypto
2790 * @param[in] context The library context
2792 * @return True if weak crypto is allowed, false if not.
2794 krb5_boolean smb_krb5_get_allowed_weak_crypto(krb5_context context)
2795 #if defined(HAVE_KRB5_CONFIG_GET_BOOL_DEFAULT)
2797 return krb5_config_get_bool_default(context,
2801 "allow_weak_crypto",
2804 #elif defined(HAVE_PROFILE_H) && defined(HAVE_KRB5_GET_PROFILE)
2806 #include <profile.h>
2807 krb5_error_code ret;
2808 krb5_boolean ret_default = false;
2812 ret = krb5_get_profile(context,
2818 ret = profile_get_boolean(profile,
2820 "allow_weak_crypto",
2821 NULL, /* subsubname */
2822 ret_default, /* def_val */
2823 &ret_profile /* *ret_default */);
2828 profile_release(profile);
2833 #error UNKNOWN_KRB5_CONFIG_ROUTINES
2837 * @brief Return the type of a krb5_principal
2839 * @param[in] context The library context.
2841 * @param[in] principal The principal to get the type from.
2843 * @return The integer type of the principal.
2845 int smb_krb5_principal_get_type(krb5_context context,
2846 krb5_const_principal principal)
2848 #ifdef HAVE_KRB5_PRINCIPAL_GET_TYPE /* Heimdal */
2849 return krb5_principal_get_type(context, principal);
2850 #elif defined(krb5_princ_type) /* MIT */
2851 return krb5_princ_type(context, principal);
2853 #error UNKNOWN_PRINC_GET_TYPE_FUNCTION
2858 * @brief Set the type of a principal
2860 * @param[in] context The library context
2862 * @param[inout] principal The principal to set the type for.
2864 * @param[in] type The principal type to set.
2866 void smb_krb5_principal_set_type(krb5_context context,
2867 krb5_principal principal,
2870 #ifdef HAVE_KRB5_PRINCIPAL_SET_TYPE /* Heimdal */
2871 krb5_principal_set_type(context, principal, type);
2872 #elif defined(krb5_princ_type) /* MIT */
2873 krb5_princ_type(context, principal) = type;
2875 #error UNKNOWN_PRINC_SET_TYPE_FUNCTION
2879 #if !defined(HAVE_KRB5_WARNX)
2881 * @brief Log a Kerberos message
2883 * It sends the message to com_err.
2885 * @param[in] context The library context
2887 * @param[in] fmt The message format
2889 * @param[in] ... The message arguments
2891 * @return 0 on success.
2893 krb5_error_code krb5_warnx(krb5_context context, const char *fmt, ...)
2897 va_start(args, fmt);
2898 com_err_va("samba-kdc", errno, fmt, args);
2906 * @brief Copy a credential cache.
2908 * @param[in] context The library context.
2910 * @param[in] incc Credential cache to be copied.
2912 * @param[inout] outcc Copy of credential cache to be filled in.
2914 * @return 0 on success, a Kerberos error code otherwise.
2916 krb5_error_code smb_krb5_cc_copy_creds(krb5_context context,
2917 krb5_ccache incc, krb5_ccache outcc)
2919 #ifdef HAVE_KRB5_CC_COPY_CACHE /* Heimdal */
2920 return krb5_cc_copy_cache(context, incc, outcc);
2921 #elif defined(HAVE_KRB5_CC_COPY_CREDS)
2922 return krb5_cc_copy_creds(context, incc, outcc);
2924 #error UNKNOWN_KRB5_CC_COPY_CACHE_OR_CREDS_FUNCTION
2928 /**********************************************************
2930 **********************************************************/
2932 static bool ads_cleanup_expired_creds(krb5_context context,
2936 krb5_error_code retval;
2937 const char *cc_type = krb5_cc_get_type(context, ccache);
2939 DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
2940 cc_type, krb5_cc_get_name(context, ccache),
2941 http_timestring(talloc_tos(), credsp->times.endtime)));
2943 /* we will probably need new tickets if the current ones
2944 will expire within 10 seconds.
2946 if (credsp->times.endtime >= (time(NULL) + 10))
2949 /* heimdal won't remove creds from a file ccache, and
2950 perhaps we shouldn't anyway, since internally we
2951 use memory ccaches, and a FILE one probably means that
2952 we're using creds obtained outside of our exectuable
2954 if (strequal(cc_type, "FILE")) {
2955 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type));
2959 retval = krb5_cc_remove_cred(context, ccache, 0, credsp);
2961 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
2962 error_message(retval)));
2963 /* If we have an error in this, we want to display it,
2964 but continue as though we deleted it */
2969 /* Allocate and setup the auth context into the state we need. */
2971 static krb5_error_code ads_setup_auth_context(krb5_context context,
2972 krb5_auth_context *auth_context)
2974 krb5_error_code retval;
2976 retval = krb5_auth_con_init(context, auth_context );
2978 DEBUG(1,("krb5_auth_con_init failed (%s)\n",
2979 error_message(retval)));
2983 /* Ensure this is an addressless ticket. */
2984 retval = krb5_auth_con_setaddrs(context, *auth_context, NULL, NULL);
2986 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n",
2987 error_message(retval)));
2993 #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)
2994 static krb5_error_code ads_create_gss_checksum(krb5_data *in_data, /* [inout] */
2997 unsigned int orig_length = in_data->length;
2998 unsigned int base_cksum_size = GSSAPI_CHECKSUM_SIZE;
2999 char *gss_cksum = NULL;
3002 /* Extra length field for delgated ticket. */
3003 base_cksum_size += 4;
3006 if ((unsigned int)base_cksum_size + orig_length <
3007 (unsigned int)base_cksum_size) {
3011 gss_cksum = (char *)SMB_MALLOC(base_cksum_size + orig_length);
3012 if (gss_cksum == NULL) {
3016 memset(gss_cksum, '\0', base_cksum_size + orig_length);
3017 SIVAL(gss_cksum, 0, GSSAPI_BNDLENGTH);
3020 * GSS_C_NO_CHANNEL_BINDINGS means 16 zero bytes.
3021 * This matches the behavior of heimdal and mit.
3023 * And it is needed to work against some closed source
3028 memset(&gss_cksum[4], 0x00, GSSAPI_BNDLENGTH);
3030 SIVAL(gss_cksum, 20, gss_flags);
3033 SSVAL(gss_cksum, 24, 1); /* The Delegation Option identifier */
3034 SSVAL(gss_cksum, 26, orig_length);
3035 /* Copy the kerberos KRB_CRED data */
3036 memcpy(gss_cksum + 28, in_data->data, orig_length);
3037 free(in_data->data);
3038 in_data->data = NULL;
3039 in_data->length = 0;
3041 in_data->data = gss_cksum;
3042 in_data->length = base_cksum_size + orig_length;
3048 * We can't use krb5_mk_req because w2k wants the service to be in a particular
3051 static krb5_error_code ads_krb5_mk_req(krb5_context context,
3052 krb5_auth_context *auth_context,
3053 const krb5_flags ap_req_options,
3054 const char *principal,
3057 time_t *expire_time,
3058 const char *impersonate_princ_s)
3060 krb5_error_code retval;
3061 krb5_principal server;
3062 krb5_principal impersonate_princ = NULL;
3066 bool creds_ready = false;
3067 int i = 0, maxtries = 3;
3070 ZERO_STRUCT(in_data);
3072 retval = smb_krb5_parse_name(context, principal, &server);
3074 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal));
3078 if (impersonate_princ_s) {
3079 retval = smb_krb5_parse_name(context, impersonate_princ_s,
3080 &impersonate_princ);
3082 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", impersonate_princ_s));
3087 /* obtain ticket & session key */
3089 if ((retval = krb5_copy_principal(context, server, &creds.server))) {
3090 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n",
3091 error_message(retval)));
3095 retval = krb5_cc_get_principal(context, ccache, &creds.client);
3097 /* This can commonly fail on smbd startup with no ticket in the cache.
3098 * Report at higher level than 1. */
3099 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
3100 error_message(retval)));
3104 while (!creds_ready && (i < maxtries)) {
3106 retval = smb_krb5_get_credentials(context,
3113 DBG_WARNING("smb_krb5_get_credentials failed for %s "
3116 error_message(retval));
3120 /* cope with ticket being in the future due to clock skew */
3121 if ((unsigned)credsp->times.starttime > time(NULL)) {
3122 time_t t = time(NULL);
3123 int time_offset =(int)((unsigned)credsp->times.starttime-t);
3124 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset));
3125 krb5_set_real_time(context, t + time_offset + 1, 0);
3128 ok = ads_cleanup_expired_creds(context, ccache, credsp);
3136 DBG_DEBUG("Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
3138 krb5_cc_get_type(context, ccache),
3139 krb5_cc_get_name(context, ccache),
3140 http_timestring(talloc_tos(),
3141 (unsigned)credsp->times.endtime),
3142 (unsigned)credsp->times.endtime);
3145 *expire_time = (time_t)credsp->times.endtime;
3148 /* Allocate the auth_context. */
3149 retval = ads_setup_auth_context(context, auth_context);
3151 DBG_WARNING("ads_setup_auth_context failed (%s)\n",
3152 error_message(retval));
3156 #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)
3158 uint32_t gss_flags = 0;
3160 if (credsp->ticket_flags & TKT_FLG_OK_AS_DELEGATE) {
3162 * Fetch a forwarded TGT from the KDC so that we can
3163 * hand off a 2nd ticket as part of the kerberos
3167 DBG_INFO("Server marked as OK to delegate to, building "
3168 "forwardable TGT\n");
3170 retval = krb5_auth_con_setuseruserkey(context,
3172 &credsp->keyblock );
3174 DBG_WARNING("krb5_auth_con_setuseruserkey "
3176 error_message(retval)));
3180 /* Must use a subkey for forwarded tickets. */
3181 retval = krb5_auth_con_setflags(context,
3183 KRB5_AUTH_CONTEXT_USE_SUBKEY);
3185 DBG_WARNING("krb5_auth_con_setflags failed (%s)\n",
3186 error_message(retval)));
3190 retval = krb5_fwd_tgt_creds(context,/* Krb5 context [in] */
3191 *auth_context, /* Authentication context [in] */
3192 discard_const_p(char, KRB5_TGS_NAME), /* Ticket service name ("krbtgt") [in] */
3193 credsp->client, /* Client principal for the tgt [in] */
3194 credsp->server, /* Server principal for the tgt [in] */
3195 ccache, /* Credential cache to use for storage [in] */
3196 1, /* Turn on for "Forwardable ticket" [in] */
3197 &in_data ); /* Resulting response [out] */
3200 DBG_INFO("krb5_fwd_tgt_creds failed (%s)\n",
3201 error_message(retval));
3204 * This is not fatal. Delete the *auth_context and continue
3205 * with krb5_mk_req_extended to get a non-forwardable ticket.
3209 free( in_data.data );
3210 in_data.data = NULL;
3213 krb5_auth_con_free(context, *auth_context);
3214 *auth_context = NULL;
3215 retval = ads_setup_auth_context(context, auth_context);
3217 DBG_WARNING("ads_setup_auth_context failed (%s)\n",
3218 error_message(retval)));
3222 /* We got a delegated ticket. */
3223 gss_flags |= GSS_C_DELEG_FLAG;
3227 /* Frees and reallocates in_data into a GSS checksum blob. */
3228 retval = ads_create_gss_checksum(&in_data, gss_flags);
3233 /* We always want GSS-checksum types. */
3234 retval = krb5_auth_con_set_req_cksumtype(context, *auth_context, GSSAPI_CHECKSUM );
3236 DEBUG(1,("krb5_auth_con_set_req_cksumtype failed (%s)\n",
3237 error_message(retval)));
3243 retval = krb5_mk_req_extended(context, auth_context, ap_req_options,
3244 &in_data, credsp, outbuf);
3246 DBG_WARNING("krb5_mk_req_extended failed (%s)\n",
3247 error_message(retval));
3250 #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)
3255 free( in_data.data );
3259 krb5_free_creds(context, credsp);
3262 krb5_free_cred_contents(context, &creds);
3265 krb5_free_principal(context, server);
3266 if (impersonate_princ) {
3267 krb5_free_principal(context, impersonate_princ);
3274 get a kerberos5 ticket for the given service
3276 int ads_krb5_cli_get_ticket(TALLOC_CTX *mem_ctx,
3277 const char *principal,
3280 DATA_BLOB *session_key_krb5,
3281 uint32_t extra_ap_opts, const char *ccname,
3283 const char *impersonate_princ_s)
3285 krb5_error_code retval;
3287 krb5_context context = NULL;
3288 krb5_ccache ccdef = NULL;
3289 krb5_auth_context auth_context = NULL;
3290 krb5_enctype enc_types[] = {
3291 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
3292 ENCTYPE_AES256_CTS_HMAC_SHA1_96,
3294 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
3295 ENCTYPE_AES128_CTS_HMAC_SHA1_96,
3297 ENCTYPE_ARCFOUR_HMAC,
3298 ENCTYPE_DES_CBC_MD5,
3299 ENCTYPE_DES_CBC_CRC,
3303 initialize_krb5_error_table();
3304 retval = krb5_init_context(&context);
3306 DBG_WARNING("krb5_init_context failed (%s)\n",
3307 error_message(retval));
3311 if (time_offset != 0) {
3312 krb5_set_real_time(context, time(NULL) + time_offset, 0);
3315 retval = krb5_cc_resolve(context,
3316 ccname ? ccname : krb5_cc_default_name(context),
3319 DBG_WARNING("krb5_cc_default failed (%s)\n",
3320 error_message(retval));
3324 retval = krb5_set_default_tgs_ktypes(context, enc_types);
3326 DBG_WARNING("krb5_set_default_tgs_ktypes failed (%s)\n",
3327 error_message(retval));
3331 retval = ads_krb5_mk_req(context,
3333 AP_OPTS_USE_SUBKEY | (krb5_flags)extra_ap_opts,
3338 impersonate_princ_s);
3343 ok = smb_krb5_get_smb_session_key(mem_ctx,
3353 *ticket = data_blob_talloc(mem_ctx, packet.data, packet.length);
3355 smb_krb5_free_data_contents(context, &packet);
3361 krb5_cc_close(context, ccdef);
3364 krb5_auth_con_free(context, auth_context);
3366 krb5_free_context(context);
3372 #else /* HAVE_KRB5 */
3373 /* This saves a few linking headaches */
3374 int ads_krb5_cli_get_ticket(TALLOC_CTX *mem_ctx,
3375 const char *principal,
3378 DATA_BLOB *session_key_krb5,
3379 uint32_t extra_ap_opts, const char *ccname,
3381 const char *impersonate_princ_s)
3383 DEBUG(0,("NO KERBEROS SUPPORT\n"));
3387 #endif /* HAVE_KRB5 */