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);
437 /**************************************************************
438 krb5_parse_name that takes a UNIX charset.
439 **************************************************************/
441 krb5_error_code smb_krb5_parse_name(krb5_context context,
442 const char *name, /* in unix charset */
443 krb5_principal *principal)
447 size_t converted_size;
448 TALLOC_CTX *frame = talloc_stackframe();
450 if (!push_utf8_talloc(frame, &utf8_name, name, &converted_size)) {
455 ret = krb5_parse_name(context, utf8_name, principal);
460 /**************************************************************
461 krb5_parse_name that returns a UNIX charset name. Must
462 be freed with talloc_free() call.
463 **************************************************************/
465 krb5_error_code smb_krb5_unparse_name(TALLOC_CTX *mem_ctx,
466 krb5_context context,
467 krb5_const_principal principal,
472 size_t converted_size;
475 ret = krb5_unparse_name(context, principal, &utf8_name);
480 if (!pull_utf8_talloc(mem_ctx, unix_name, utf8_name, &converted_size)) {
481 krb5_free_unparsed_name(context, utf8_name);
484 krb5_free_unparsed_name(context, utf8_name);
488 krb5_error_code smb_krb5_parse_name_norealm(krb5_context context,
490 krb5_principal *principal)
492 /* we are cheating here because parse_name will in fact set the realm.
493 * We don't care as the only caller of smb_krb5_parse_name_norealm
494 * ignores the realm anyway when calling
495 * smb_krb5_principal_compare_any_realm later - Guenther */
497 return smb_krb5_parse_name(context, name, principal);
500 bool smb_krb5_principal_compare_any_realm(krb5_context context,
501 krb5_const_principal princ1,
502 krb5_const_principal princ2)
504 return krb5_principal_compare_any_realm(context, princ1, princ2);
508 * @brief Free the contents of a krb5_data structure and zero the data field.
510 * @param[in] context The krb5 context
512 * @param[in] pdata The data structure to free contents of
514 * This function frees the contents, not the structure itself.
516 void smb_krb5_free_data_contents(krb5_context context, krb5_data *pdata)
518 #if defined(HAVE_KRB5_FREE_DATA_CONTENTS)
520 krb5_free_data_contents(context, pdata);
522 #elif defined(HAVE_KRB5_DATA_FREE)
523 krb5_data_free(context, pdata);
525 SAFE_FREE(pdata->data);
530 * @brief copy a buffer into a krb5_data struct
532 * @param[in] p The krb5_data
533 * @param[in] data The data to copy
534 * @param[in] length The length of the data to copy
535 * @return krb5_error_code
537 * Caller has to free krb5_data with smb_krb5_free_data_contents().
539 krb5_error_code smb_krb5_copy_data_contents(krb5_data *p,
543 #if defined(HAVE_KRB5_DATA_COPY)
544 return krb5_data_copy(p, data, len);
547 p->data = malloc(len);
548 if (p->data == NULL) {
551 memmove(p->data, data, len);
556 p->magic = KV5M_DATA;
561 bool smb_krb5_get_smb_session_key(TALLOC_CTX *mem_ctx,
562 krb5_context context,
563 krb5_auth_context auth_context,
564 DATA_BLOB *session_key,
567 krb5_keyblock *skey = NULL;
568 krb5_error_code err = 0;
572 #ifdef HAVE_KRB5_AUTH_CON_GETRECVSUBKEY
573 err = krb5_auth_con_getrecvsubkey(context,
576 #else /* HAVE_KRB5_AUTH_CON_GETRECVSUBKEY */
577 err = krb5_auth_con_getremotesubkey(context,
578 auth_context, &skey);
579 #endif /* HAVE_KRB5_AUTH_CON_GETRECVSUBKEY */
581 #ifdef HAVE_KRB5_AUTH_CON_GETSENDSUBKEY
582 err = krb5_auth_con_getsendsubkey(context,
585 #else /* HAVE_KRB5_AUTH_CON_GETSENDSUBKEY */
586 err = krb5_auth_con_getlocalsubkey(context,
587 auth_context, &skey);
588 #endif /* HAVE_KRB5_AUTH_CON_GETSENDSUBKEY */
591 if (err || skey == NULL) {
592 DEBUG(10, ("KRB5 error getting session key %d\n", err));
596 DEBUG(10, ("Got KRB5 session key of length %d\n",
597 (int)KRB5_KEY_LENGTH(skey)));
599 *session_key = data_blob_talloc(mem_ctx,
601 KRB5_KEY_LENGTH(skey));
602 dump_data_pw("KRB5 Session Key:\n",
604 session_key->length);
610 krb5_free_keyblock(context, skey);
618 * @brief Get talloced string component of a principal
620 * @param[in] mem_ctx The TALLOC_CTX
621 * @param[in] context The krb5_context
622 * @param[in] principal The principal
623 * @param[in] component The component
624 * @return string component
626 * Caller must talloc_free if the return value is not NULL.
629 char *smb_krb5_principal_get_comp_string(TALLOC_CTX *mem_ctx,
630 krb5_context context,
631 krb5_const_principal principal,
632 unsigned int component)
634 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
635 return talloc_strdup(mem_ctx, krb5_principal_get_comp_string(context, principal, component));
639 if (component >= krb5_princ_size(context, principal)) {
643 data = krb5_princ_component(context, principal, component);
648 return talloc_strndup(mem_ctx, data->data, data->length);
655 * @param[in] ccache_string A string pointing to the cache to renew the ticket
656 * (e.g. FILE:/tmp/krb5cc_0) or NULL. If the principal
657 * ccache has not been specified, the default ccache
660 * @param[in] client_string The client principal string (e.g. user@SAMBA.SITE)
661 * or NULL. If the principal string has not been
662 * specified, the principal from the ccache will be
665 * @param[in] service_string The service ticket string
666 * (e.g. krbtgt/SAMBA.SITE@SAMBA.SITE) or NULL. If
667 * the sevice ticket is specified, it is parsed (
668 * with the realm part ignored) and used as the
669 * server principal of the credential. Otherwise
670 * the ticket-granting service is used.
672 * @param[in] expire_time A pointer to store the credentials end time or
675 * @return 0 on Succes, a Kerberos error code otherwise.
677 krb5_error_code smb_krb5_renew_ticket(const char *ccache_string,
678 const char *client_string,
679 const char *service_string,
683 krb5_context context = NULL;
684 krb5_ccache ccache = NULL;
685 krb5_principal client = NULL;
686 krb5_creds creds, creds_in;
689 ZERO_STRUCT(creds_in);
691 initialize_krb5_error_table();
692 ret = krb5_init_context(&context);
697 if (!ccache_string) {
698 ccache_string = krb5_cc_default_name(context);
701 if (!ccache_string) {
706 DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string));
708 /* FIXME: we should not fall back to defaults */
709 ret = krb5_cc_resolve(context, discard_const_p(char, ccache_string), &ccache);
715 ret = smb_krb5_parse_name(context, client_string, &client);
720 ret = krb5_cc_get_principal(context, ccache, &client);
726 ret = krb5_get_renewed_creds(context, &creds, client, ccache, discard_const_p(char, service_string));
728 DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
732 /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
733 ret = krb5_cc_initialize(context, ccache, client);
738 ret = krb5_cc_store_cred(context, ccache, &creds);
741 *expire_time = (time_t) creds.times.endtime;
745 krb5_free_cred_contents(context, &creds_in);
746 krb5_free_cred_contents(context, &creds);
749 krb5_free_principal(context, client);
752 krb5_cc_close(context, ccache);
755 krb5_free_context(context);
762 * @brief Free the data stored in an smb_krb5_addresses structure.
764 * @param[in] context The library context
766 * @param[in] addr The address structure to free.
768 * @return 0 on success, a Kerberos error code otherwise.
770 krb5_error_code smb_krb5_free_addresses(krb5_context context,
771 smb_krb5_addresses *addr)
773 krb5_error_code ret = 0;
777 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
778 krb5_free_addresses(context, addr->addrs);
779 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
780 ret = krb5_free_addresses(context, addr->addrs);
781 SAFE_FREE(addr->addrs);
788 #define MAX_NETBIOSNAME_LEN 16
791 * @brief Add a netbios name to the array of addresses
793 * @param[in] kerb_addr A pointer to the smb_krb5_addresses to add the
796 * @param[in] netbios_name The netbios name to add.
798 * @return 0 on success, a Kerberos error code otherwise.
800 krb5_error_code smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses **kerb_addr,
801 const char *netbios_name)
803 krb5_error_code ret = 0;
804 char buf[MAX_NETBIOSNAME_LEN];
806 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
807 krb5_address **addrs = NULL;
808 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
809 krb5_addresses *addrs = NULL;
812 *kerb_addr = (smb_krb5_addresses *)SMB_MALLOC(sizeof(smb_krb5_addresses));
813 if (*kerb_addr == NULL) {
817 /* temporarily duplicate put_name() code here to avoid dependency
818 * issues for a 5 lines function */
819 len = strlen(netbios_name);
820 memcpy(buf, netbios_name,
821 (len < MAX_NETBIOSNAME_LEN) ? len : MAX_NETBIOSNAME_LEN - 1);
822 if (len < MAX_NETBIOSNAME_LEN - 1) {
823 memset(buf + len, ' ', MAX_NETBIOSNAME_LEN - 1 - len);
825 buf[MAX_NETBIOSNAME_LEN - 1] = 0x20;
827 #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
831 addrs = (krb5_address **)SMB_MALLOC(sizeof(krb5_address *) * num_addr);
833 SAFE_FREE(*kerb_addr);
837 memset(addrs, 0, sizeof(krb5_address *) * num_addr);
839 addrs[0] = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
840 if (addrs[0] == NULL) {
842 SAFE_FREE(*kerb_addr);
846 addrs[0]->magic = KV5M_ADDRESS;
847 addrs[0]->addrtype = KRB5_ADDR_NETBIOS;
848 addrs[0]->length = MAX_NETBIOSNAME_LEN;
849 addrs[0]->contents = (unsigned char *)SMB_MALLOC(addrs[0]->length);
850 if (addrs[0]->contents == NULL) {
853 SAFE_FREE(*kerb_addr);
857 memcpy(addrs[0]->contents, buf, addrs[0]->length);
861 #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
863 addrs = (krb5_addresses *)SMB_MALLOC(sizeof(krb5_addresses));
865 SAFE_FREE(*kerb_addr);
869 memset(addrs, 0, sizeof(krb5_addresses));
872 addrs->val = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
873 if (addrs->val == NULL) {
875 SAFE_FREE(kerb_addr);
879 addrs->val[0].addr_type = KRB5_ADDR_NETBIOS;
880 addrs->val[0].address.length = MAX_NETBIOSNAME_LEN;
881 addrs->val[0].address.data = (unsigned char *)SMB_MALLOC(addrs->val[0].address.length);
882 if (addrs->val[0].address.data == NULL) {
883 SAFE_FREE(addrs->val);
885 SAFE_FREE(*kerb_addr);
889 memcpy(addrs->val[0].address.data, buf, addrs->val[0].address.length);
892 #error UNKNOWN_KRB5_ADDRESS_FORMAT
894 (*kerb_addr)->addrs = addrs;
900 * @brief Get the enctype from a key table entry
902 * @param[in] kt_entry Key table entry to get the enctype from.
904 * @return The enctype from the entry.
906 krb5_enctype smb_krb5_kt_get_enctype_from_entry(krb5_keytab_entry *kt_entry)
908 return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry));
912 * @brief Free the contents of a key table entry.
914 * @param[in] context The library context.
916 * @param[in] kt_entry The key table entry to free the contents of.
918 * @return 0 on success, a Kerberos error code otherwise.
920 * The pointer itself is not freed.
922 krb5_error_code smb_krb5_kt_free_entry(krb5_context context,
923 krb5_keytab_entry *kt_entry)
925 /* Try krb5_free_keytab_entry_contents first, since
926 * MIT Kerberos >= 1.7 has both krb5_free_keytab_entry_contents and
927 * krb5_kt_free_entry but only has a prototype for the first, while the
928 * second is considered private.
930 #if defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
931 return krb5_free_keytab_entry_contents(context, kt_entry);
932 #elif defined(HAVE_KRB5_KT_FREE_ENTRY)
933 return krb5_kt_free_entry(context, kt_entry);
935 #error UNKNOWN_KT_FREE_FUNCTION
941 * @brief Convert an encryption type to a string.
943 * @param[in] context The library context.
945 * @param[in] enctype The encryption type.
947 * @param[in] etype_s A pointer to store the allocated encryption type as a
950 * @return 0 on success, a Kerberos error code otherwise.
952 * The caller needs to free the allocated string etype_s.
954 krb5_error_code smb_krb5_enctype_to_string(krb5_context context,
955 krb5_enctype enctype,
958 #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
959 return krb5_enctype_to_string(context, enctype, etype_s); /* Heimdal */
960 #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
962 krb5_error_code ret = krb5_enctype_to_string(enctype, buf, 256); /* MIT */
966 *etype_s = SMB_STRDUP(buf);
972 #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
976 /* This MAX_NAME_LEN is a constant defined in krb5.h */
977 #ifndef MAX_KEYTAB_NAME_LEN
978 #define MAX_KEYTAB_NAME_LEN 1100
982 * @brief Open a key table readonly or with readwrite access.
984 * Allows to use a different keytab than the default one using a relative
985 * path to the keytab.
987 * @param[in] context The library context
989 * @param[in] keytab_name_req The path to the key table.
991 * @param[in] write_access Open with readwrite access.
993 * @param[in] keytab A pointer o the opended key table.
995 * The keytab pointer should be freed using krb5_kt_close().
997 * @return 0 on success, a Kerberos error code otherwise.
999 krb5_error_code smb_krb5_kt_open_relative(krb5_context context,
1000 const char *keytab_name_req,
1002 krb5_keytab *keytab)
1004 krb5_error_code ret = 0;
1005 TALLOC_CTX *mem_ctx;
1006 char keytab_string[MAX_KEYTAB_NAME_LEN];
1007 char *kt_str = NULL;
1008 bool found_valid_name = false;
1009 const char *pragma = "FILE";
1010 const char *tmp = NULL;
1012 if (!write_access && !keytab_name_req) {
1013 /* caller just wants to read the default keytab readonly, so be it */
1014 return krb5_kt_default(context, keytab);
1017 mem_ctx = talloc_init("smb_krb5_open_keytab");
1022 #ifdef HAVE_WRFILE_KEYTAB
1028 if (keytab_name_req) {
1030 if (strlen(keytab_name_req) > MAX_KEYTAB_NAME_LEN) {
1031 ret = KRB5_CONFIG_NOTENUFSPACE;
1035 if ((strncmp(keytab_name_req, "WRFILE:/", 8) == 0) ||
1036 (strncmp(keytab_name_req, "FILE:/", 6) == 0)) {
1037 tmp = keytab_name_req;
1041 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, keytab_name_req);
1050 /* we need to handle more complex keytab_strings, like:
1051 * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
1053 ret = krb5_kt_default_name(context, &keytab_string[0], MAX_KEYTAB_NAME_LEN - 2);
1058 DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string));
1060 tmp = talloc_strdup(mem_ctx, keytab_string);
1066 if (strncmp(tmp, "ANY:", 4) == 0) {
1070 memset(&keytab_string, '\0', sizeof(keytab_string));
1072 while (next_token_talloc(mem_ctx, &tmp, &kt_str, ",")) {
1073 if (strncmp(kt_str, "WRFILE:", 7) == 0) {
1074 found_valid_name = true;
1079 if (strncmp(kt_str, "FILE:", 5) == 0) {
1080 found_valid_name = true;
1085 if (tmp[0] == '/') {
1086 /* Treat as a FILE: keytab definition. */
1087 found_valid_name = true;
1090 if (found_valid_name) {
1091 if (tmp[0] != '/') {
1092 ret = KRB5_KT_BADNAME;
1096 tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, tmp);
1105 if (!found_valid_name) {
1106 ret = KRB5_KT_UNKNOWN_TYPE;
1111 DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp));
1112 ret = krb5_kt_resolve(context, tmp, keytab);
1115 TALLOC_FREE(mem_ctx);
1120 * @brief Open a key table readonly or with readwrite access.
1122 * Allows to use a different keytab than the default one. The path needs to be
1123 * an absolute path or an error will be returned.
1125 * @param[in] context The library context
1127 * @param[in] keytab_name_req The path to the key table.
1129 * @param[in] write_access Open with readwrite access.
1131 * @param[in] keytab A pointer o the opended key table.
1133 * The keytab pointer should be freed using krb5_kt_close().
1135 * @return 0 on success, a Kerberos error code otherwise.
1137 krb5_error_code smb_krb5_kt_open(krb5_context context,
1138 const char *keytab_name_req,
1140 krb5_keytab *keytab)
1142 if (keytab_name_req != NULL) {
1143 if (keytab_name_req[0] != '/') {
1144 return KRB5_KT_BADNAME;
1148 return smb_krb5_kt_open_relative(context,
1154 krb5_error_code smb_krb5_keytab_name(TALLOC_CTX *mem_ctx,
1155 krb5_context context,
1157 const char **keytab_name)
1159 char keytab_string[MAX_KEYTAB_NAME_LEN];
1160 krb5_error_code ret = 0;
1162 ret = krb5_kt_get_name(context, keytab,
1163 keytab_string, MAX_KEYTAB_NAME_LEN - 2);
1168 *keytab_name = talloc_strdup(mem_ctx, keytab_string);
1169 if (!*keytab_name) {
1177 * @brief Seek and delete old entries in a keytab based on the passed
1180 * @param[in] context The KRB5 context to use.
1182 * @param[in] keytab The keytab to operate on.
1184 * @param[in] kvno The kvnco to use.
1186 * @param[in] princ_s The principal as a string to search for.
1188 * @param[in] princ The principal as a krb5_principal to search for.
1190 * @param[in] flush Weather to flush the complete keytab.
1192 * @param[in] keep_old_entries Keep the entry with the previous kvno.
1194 * @retval 0 on Sucess
1196 * @return An appropriate KRB5 error code.
1198 krb5_error_code smb_krb5_kt_seek_and_delete_old_entries(krb5_context context,
1201 krb5_enctype enctype,
1202 const char *princ_s,
1203 krb5_principal princ,
1205 bool keep_old_entries)
1207 krb5_error_code ret;
1208 krb5_kt_cursor cursor;
1209 krb5_kt_cursor zero_csr;
1210 krb5_keytab_entry kt_entry;
1211 krb5_keytab_entry zero_kt_entry;
1212 char *ktprinc = NULL;
1213 krb5_kvno old_kvno = kvno - 1;
1214 TALLOC_CTX *tmp_ctx;
1216 ZERO_STRUCT(cursor);
1217 ZERO_STRUCT(zero_csr);
1218 ZERO_STRUCT(kt_entry);
1219 ZERO_STRUCT(zero_kt_entry);
1221 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
1222 if (ret == KRB5_KT_END || ret == ENOENT ) {
1227 tmp_ctx = talloc_new(NULL);
1228 if (tmp_ctx == NULL) {
1232 DEBUG(3, (__location__ ": Will try to delete old keytab entries\n"));
1233 while (!krb5_kt_next_entry(context, keytab, &kt_entry, &cursor)) {
1234 bool name_ok = false;
1235 krb5_enctype kt_entry_enctype =
1236 smb_krb5_kt_get_enctype_from_entry(&kt_entry);
1238 if (!flush && (princ_s != NULL)) {
1239 ret = smb_krb5_unparse_name(tmp_ctx, context,
1243 DEBUG(1, (__location__
1244 ": smb_krb5_unparse_name failed "
1245 "(%s)\n", error_message(ret)));
1249 #ifdef HAVE_KRB5_KT_COMPARE
1250 name_ok = krb5_kt_compare(context, &kt_entry,
1253 name_ok = (strcmp(ktprinc, princ_s) == 0);
1257 DEBUG(10, (__location__ ": ignoring keytab "
1258 "entry principal %s, kvno = %d\n",
1259 ktprinc, kt_entry.vno));
1262 * just free this entry and continue. */
1263 ret = smb_krb5_kt_free_entry(context,
1265 ZERO_STRUCT(kt_entry);
1267 DEBUG(1, (__location__
1268 ": smb_krb5_kt_free_entry "
1270 error_message(ret)));
1274 TALLOC_FREE(ktprinc);
1278 TALLOC_FREE(ktprinc);
1281 /*------------------------------------------------------------
1282 * Save the entries with kvno - 1. This is what microsoft does
1283 * to allow people with existing sessions that have kvno - 1
1284 * to still work. Otherwise, when the password for the machine
1285 * changes, all kerberizied sessions will 'break' until either
1286 * the client reboots or the client's session key expires and
1287 * they get a new session ticket with the new kvno.
1288 * Some keytab files only store the kvno in 8bits, limit
1289 * the compare accordingly.
1292 if (!flush && ((kt_entry.vno & 0xff) == (old_kvno & 0xff))) {
1293 DEBUG(5, (__location__ ": Saving previous (kvno %d) "
1294 "entry for principal: %s.\n",
1295 old_kvno, princ_s));
1299 if (keep_old_entries) {
1300 DEBUG(5, (__location__ ": Saving old (kvno %d) "
1301 "entry for principal: %s.\n",
1307 (kt_entry.vno == kvno) &&
1308 (kt_entry_enctype != enctype))
1310 DEBUG(5, (__location__ ": Saving entry with kvno [%d] "
1311 "enctype [%d] for principal: %s.\n",
1312 kvno, kt_entry_enctype, princ_s));
1316 DEBUG(5, (__location__ ": Found old entry for principal: %s "
1317 "(kvno %d) - trying to remove it.\n",
1318 princ_s, kt_entry.vno));
1320 ret = krb5_kt_end_seq_get(context, keytab, &cursor);
1321 ZERO_STRUCT(cursor);
1323 DEBUG(1, (__location__ ": krb5_kt_end_seq_get() "
1324 "failed (%s)\n", error_message(ret)));
1327 ret = krb5_kt_remove_entry(context, keytab, &kt_entry);
1329 DEBUG(1, (__location__ ": krb5_kt_remove_entry() "
1330 "failed (%s)\n", error_message(ret)));
1334 DEBUG(5, (__location__ ": removed old entry for principal: "
1335 "%s (kvno %d).\n", princ_s, kt_entry.vno));
1337 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
1339 DEBUG(1, (__location__ ": krb5_kt_start_seq() failed "
1340 "(%s)\n", error_message(ret)));
1343 ret = smb_krb5_kt_free_entry(context, &kt_entry);
1344 ZERO_STRUCT(kt_entry);
1346 DEBUG(1, (__location__ ": krb5_kt_remove_entry() "
1347 "failed (%s)\n", error_message(ret)));
1353 talloc_free(tmp_ctx);
1354 if (memcmp(&zero_kt_entry, &kt_entry, sizeof(krb5_keytab_entry))) {
1355 smb_krb5_kt_free_entry(context, &kt_entry);
1357 if (memcmp(&cursor, &zero_csr, sizeof(krb5_kt_cursor)) != 0) {
1358 krb5_kt_end_seq_get(context, keytab, &cursor);
1364 * @brief Add a keytab entry for the given principal
1366 * @param[in] context The krb5 context to use.
1368 * @param[in] keytab The keytab to add the entry to.
1370 * @param[in] kvno The kvno to use.
1372 * @param[in] princ_s The principal as a string.
1374 * @param[in] salt_principal The salt principal to salt the password with.
1375 * Only needed for keys which support salting.
1376 * If no salt is used set no_salt to false and
1379 * @param[in] enctype The encryption type of the keytab entry.
1381 * @param[in] password The password of the keytab entry.
1383 * @param[in] no_salt If the password should not be salted. Normally
1384 * this is only set to false for encryption types
1385 * which do not support salting like RC4.
1387 * @param[in] keep_old_entries Wether to keep or delte old keytab entries.
1389 * @retval 0 on Success
1391 * @return A corresponding KRB5 error code.
1393 * @see smb_krb5_kt_open()
1395 krb5_error_code smb_krb5_kt_add_entry(krb5_context context,
1398 const char *princ_s,
1399 const char *salt_principal,
1400 krb5_enctype enctype,
1401 krb5_data *password,
1403 bool keep_old_entries)
1405 krb5_error_code ret;
1406 krb5_keytab_entry kt_entry;
1407 krb5_principal princ = NULL;
1408 krb5_keyblock *keyp;
1410 ZERO_STRUCT(kt_entry);
1412 ret = smb_krb5_parse_name(context, princ_s, &princ);
1414 DEBUG(1, (__location__ ": smb_krb5_parse_name(%s) "
1415 "failed (%s)\n", princ_s, error_message(ret)));
1419 /* Seek and delete old keytab entries */
1420 ret = smb_krb5_kt_seek_and_delete_old_entries(context,
1432 /* If we get here, we have deleted all the old entries with kvno's
1433 * not equal to the current kvno-1. */
1435 keyp = KRB5_KT_KEY(&kt_entry);
1438 KRB5_KEY_DATA(keyp) = (KRB5_KEY_DATA_CAST *)SMB_MALLOC(password->length);
1439 if (KRB5_KEY_DATA(keyp) == NULL) {
1443 memcpy(KRB5_KEY_DATA(keyp), password->data, password->length);
1444 KRB5_KEY_LENGTH(keyp) = password->length;
1445 KRB5_KEY_TYPE(keyp) = enctype;
1447 krb5_principal salt_princ = NULL;
1449 /* Now add keytab entries for all encryption types */
1450 ret = smb_krb5_parse_name(context, salt_principal, &salt_princ);
1452 DBG_WARNING("krb5_parse_name(%s) failed (%s)\n",
1453 salt_principal, error_message(ret));
1457 ret = smb_krb5_create_key_from_string(context,
1463 krb5_free_principal(context, salt_princ);
1469 kt_entry.principal = princ;
1470 kt_entry.vno = kvno;
1472 DEBUG(3, (__location__ ": adding keytab entry for (%s) with "
1473 "encryption type (%d) and version (%d)\n",
1474 princ_s, enctype, kt_entry.vno));
1475 ret = krb5_kt_add_entry(context, keytab, &kt_entry);
1476 krb5_free_keyblock_contents(context, keyp);
1477 ZERO_STRUCT(kt_entry);
1479 DEBUG(1, (__location__ ": adding entry to keytab "
1480 "failed (%s)\n", error_message(ret)));
1486 krb5_free_principal(context, princ);
1492 #if defined(HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE) && \
1493 defined(HAVE_KRB5_GET_CREDS_OPT_ALLOC) && \
1494 defined(HAVE_KRB5_GET_CREDS)
1495 static krb5_error_code smb_krb5_get_credentials_for_user_opt(krb5_context context,
1498 krb5_principal server,
1499 krb5_principal impersonate_princ,
1500 krb5_creds **out_creds)
1502 krb5_error_code ret;
1503 krb5_get_creds_opt opt;
1505 ret = krb5_get_creds_opt_alloc(context, &opt);
1509 krb5_get_creds_opt_add_options(context, opt, KRB5_GC_FORWARDABLE);
1511 if (impersonate_princ) {
1512 ret = krb5_get_creds_opt_set_impersonate(context, opt,
1519 ret = krb5_get_creds(context, opt, ccache, server, out_creds);
1526 krb5_get_creds_opt_free(context, opt);
1530 #endif /* HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE */
1532 #ifdef HAVE_KRB5_GET_CREDENTIALS_FOR_USER
1534 #if !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER
1535 krb5_error_code KRB5_CALLCONV
1536 krb5_get_credentials_for_user(krb5_context context, krb5_flags options,
1537 krb5_ccache ccache, krb5_creds *in_creds,
1538 krb5_data *subject_cert,
1539 krb5_creds **out_creds);
1540 #endif /* !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER */
1542 static krb5_error_code smb_krb5_get_credentials_for_user(krb5_context context,
1545 krb5_principal server,
1546 krb5_principal impersonate_princ,
1547 krb5_creds **out_creds)
1549 krb5_error_code ret;
1550 krb5_creds in_creds;
1552 ZERO_STRUCT(in_creds);
1554 if (impersonate_princ) {
1556 in_creds.server = me;
1557 in_creds.client = impersonate_princ;
1559 ret = krb5_get_credentials_for_user(context,
1560 0, /* krb5_flags options */
1563 NULL, /* krb5_data *subject_cert */
1566 in_creds.client = me;
1567 in_creds.server = server;
1569 ret = krb5_get_credentials(context, 0, ccache,
1570 &in_creds, out_creds);
1575 #endif /* HAVE_KRB5_GET_CREDENTIALS_FOR_USER */
1578 * smb_krb5_get_credentials
1580 * @brief Get krb5 credentials for a server
1582 * @param[in] context An initialized krb5_context
1583 * @param[in] ccache An initialized krb5_ccache
1584 * @param[in] me The krb5_principal of the caller
1585 * @param[in] server The krb5_principal of the requested service
1586 * @param[in] impersonate_princ The krb5_principal of a user to impersonate as (optional)
1587 * @param[out] out_creds The returned krb5_creds structure
1588 * @return krb5_error_code
1591 krb5_error_code smb_krb5_get_credentials(krb5_context context,
1594 krb5_principal server,
1595 krb5_principal impersonate_princ,
1596 krb5_creds **out_creds)
1598 krb5_error_code ret;
1599 krb5_creds *creds = NULL;
1601 if (out_creds != NULL) {
1605 if (impersonate_princ) {
1606 #ifdef HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE /* Heimdal */
1607 ret = smb_krb5_get_credentials_for_user_opt(context, ccache, me, server, impersonate_princ, &creds);
1608 #elif defined(HAVE_KRB5_GET_CREDENTIALS_FOR_USER) /* MIT */
1609 ret = smb_krb5_get_credentials_for_user(context, ccache, me, server, impersonate_princ, &creds);
1614 krb5_creds in_creds;
1616 ZERO_STRUCT(in_creds);
1618 in_creds.client = me;
1619 in_creds.server = server;
1621 ret = krb5_get_credentials(context, 0, ccache,
1634 krb5_free_creds(context, creds);
1640 krb5_error_code smb_krb5_keyblock_init_contents(krb5_context context,
1641 krb5_enctype enctype,
1646 #if defined(HAVE_KRB5_KEYBLOCK_INIT)
1647 return krb5_keyblock_init(context, enctype, data, length, key);
1649 memset(key, 0, sizeof(krb5_keyblock));
1650 KRB5_KEY_DATA(key) = SMB_MALLOC(length);
1651 if (NULL == KRB5_KEY_DATA(key)) {
1654 memcpy(KRB5_KEY_DATA(key), data, length);
1655 KRB5_KEY_LENGTH(key) = length;
1656 KRB5_KEY_TYPE(key) = enctype;
1662 simulate a kinit, putting the tgt in the given credentials cache.
1663 Orignally by remus@snapserver.com
1665 This version is built to use a keyblock, rather than needing the
1668 The impersonate_principal is the principal if NULL, or the principal
1671 The target_service defaults to the krbtgt if NULL, but could be
1672 kpasswd/realm or the local service (if we are doing s4u2self)
1674 krb5_error_code kerberos_kinit_keyblock_cc(krb5_context ctx, krb5_ccache cc,
1675 krb5_principal principal,
1676 krb5_keyblock *keyblock,
1677 const char *target_service,
1678 krb5_get_init_creds_opt *krb_options,
1679 time_t *expire_time,
1682 krb5_error_code code = 0;
1683 krb5_creds my_creds;
1685 #if defined(HAVE_KRB5_GET_INIT_CREDS_KEYBLOCK)
1686 code = krb5_get_init_creds_keyblock(ctx, &my_creds, principal,
1687 keyblock, 0, target_service,
1689 #elif defined(HAVE_KRB5_GET_INIT_CREDS_KEYTAB)
1691 #define SMB_CREDS_KEYTAB "MEMORY:tmp_smb_creds_XXXXXX"
1692 char tmp_name[sizeof(SMB_CREDS_KEYTAB)];
1693 krb5_keytab_entry entry;
1697 memset(&entry, 0, sizeof(entry));
1698 entry.principal = principal;
1699 *(KRB5_KT_KEY(&entry)) = *keyblock;
1701 memcpy(tmp_name, SMB_CREDS_KEYTAB, sizeof(SMB_CREDS_KEYTAB));
1702 mask = umask(S_IRWXO | S_IRWXG);
1705 if (tmp_name[0] == 0) {
1706 return KRB5_KT_BADNAME;
1708 code = krb5_kt_resolve(ctx, tmp_name, &keytab);
1713 code = krb5_kt_add_entry(ctx, keytab, &entry);
1715 (void)krb5_kt_close(ctx, keytab);
1719 code = krb5_get_init_creds_keytab(ctx, &my_creds, principal,
1720 keytab, 0, target_service,
1722 (void)krb5_kt_close(ctx, keytab);
1725 #error krb5_get_init_creds_keyblock not available!
1731 #ifndef SAMBA4_USES_HEIMDAL /* MIT */
1733 * We need to store the principal as returned from the KDC to the
1734 * credentials cache. If we don't do that the KRB5 library is not
1735 * able to find the tickets it is looking for
1737 principal = my_creds.client;
1739 code = krb5_cc_initialize(ctx, cc, principal);
1744 code = krb5_cc_store_cred(ctx, cc, &my_creds);
1750 *expire_time = (time_t) my_creds.times.endtime;
1754 *kdc_time = (time_t) my_creds.times.starttime;
1759 krb5_free_cred_contents(ctx, &my_creds);
1763 krb5_error_code kerberos_kinit_password_cc(krb5_context ctx, krb5_ccache cc,
1764 krb5_principal principal,
1765 const char *password,
1766 const char *target_service,
1767 krb5_get_init_creds_opt *krb_options,
1768 time_t *expire_time,
1771 krb5_error_code code = 0;
1772 krb5_creds my_creds;
1774 code = krb5_get_init_creds_password(ctx, &my_creds, principal,
1775 password, NULL, NULL, 0,
1776 target_service, krb_options);
1781 #ifndef SAMBA4_USES_HEIMDAL /* MIT */
1783 * We need to store the principal as returned from the KDC to the
1784 * credentials cache. If we don't do that the KRB5 library is not
1785 * able to find the tickets it is looking for
1787 principal = my_creds.client;
1789 code = krb5_cc_initialize(ctx, cc, principal);
1794 code = krb5_cc_store_cred(ctx, cc, &my_creds);
1800 *expire_time = (time_t) my_creds.times.endtime;
1804 *kdc_time = (time_t) my_creds.times.starttime;
1809 krb5_free_cred_contents(ctx, &my_creds);
1813 #ifdef SAMBA4_USES_HEIMDAL
1815 simulate a kinit, putting the tgt in the given credentials cache.
1816 Orignally by remus@snapserver.com
1818 The impersonate_principal is the principal
1820 The self_service, should be the local service (for S4U2Self if
1821 impersonate_principal is given).
1823 The target_service defaults to the krbtgt if NULL, but could be
1824 kpasswd/realm or a remote service (for S4U2Proxy)
1827 krb5_error_code kerberos_kinit_s4u2_cc(krb5_context ctx,
1828 krb5_ccache store_cc,
1829 krb5_principal init_principal,
1830 const char *init_password,
1831 krb5_principal impersonate_principal,
1832 const char *self_service,
1833 const char *target_service,
1834 krb5_get_init_creds_opt *krb_options,
1835 time_t *expire_time,
1838 krb5_error_code code = 0;
1839 krb5_get_creds_opt options;
1840 krb5_principal store_principal;
1841 krb5_creds store_creds;
1842 krb5_creds *s4u2self_creds;
1843 Ticket s4u2self_ticket;
1844 size_t s4u2self_ticketlen;
1845 krb5_creds *s4u2proxy_creds;
1846 krb5_principal self_princ;
1848 krb5_principal target_princ;
1850 const char *self_realm;
1851 krb5_principal blacklist_principal = NULL;
1852 krb5_principal whitelist_principal = NULL;
1854 code = krb5_get_init_creds_password(ctx, &store_creds,
1865 store_principal = init_principal;
1868 * We are trying S4U2Self now:
1870 * As we do not want to expose our TGT in the
1871 * krb5_ccache, which is also holds the impersonated creds.
1873 * Some low level krb5/gssapi function might use the TGT
1874 * identity and let the client act as our machine account.
1876 * We need to avoid that and use a temporary krb5_ccache
1877 * in order to pass our TGT to the krb5_get_creds() function.
1879 code = krb5_cc_new_unique(ctx, NULL, NULL, &tmp_cc);
1881 krb5_free_cred_contents(ctx, &store_creds);
1885 code = krb5_cc_initialize(ctx, tmp_cc, store_creds.client);
1887 krb5_cc_destroy(ctx, tmp_cc);
1888 krb5_free_cred_contents(ctx, &store_creds);
1892 code = krb5_cc_store_cred(ctx, tmp_cc, &store_creds);
1894 krb5_free_cred_contents(ctx, &store_creds);
1895 krb5_cc_destroy(ctx, tmp_cc);
1900 * we need to remember the client principal of our
1901 * TGT and make sure the KDC does not return this
1902 * in the impersonated tickets. This can happen
1903 * if the KDC does not support S4U2Self and S4U2Proxy.
1905 blacklist_principal = store_creds.client;
1906 store_creds.client = NULL;
1907 krb5_free_cred_contents(ctx, &store_creds);
1910 * Check if we also need S4U2Proxy or if S4U2Self is
1911 * enough in order to get a ticket for the target.
1913 if (target_service == NULL) {
1915 } else if (strcmp(target_service, self_service) == 0) {
1922 * For S4U2Self we need our own service principal,
1923 * which belongs to our own realm (available on
1924 * our client principal).
1926 self_realm = krb5_principal_get_realm(ctx, init_principal);
1928 code = krb5_parse_name(ctx, self_service, &self_princ);
1930 krb5_free_principal(ctx, blacklist_principal);
1931 krb5_cc_destroy(ctx, tmp_cc);
1935 code = krb5_principal_set_realm(ctx, self_princ, self_realm);
1937 krb5_free_principal(ctx, blacklist_principal);
1938 krb5_free_principal(ctx, self_princ);
1939 krb5_cc_destroy(ctx, tmp_cc);
1943 code = krb5_get_creds_opt_alloc(ctx, &options);
1945 krb5_free_principal(ctx, blacklist_principal);
1946 krb5_free_principal(ctx, self_princ);
1947 krb5_cc_destroy(ctx, tmp_cc);
1953 * If we want S4U2Proxy, we need the forwardable flag
1954 * on the S4U2Self ticket.
1956 krb5_get_creds_opt_set_options(ctx, options, KRB5_GC_FORWARDABLE);
1959 code = krb5_get_creds_opt_set_impersonate(ctx, options,
1960 impersonate_principal);
1962 krb5_get_creds_opt_free(ctx, options);
1963 krb5_free_principal(ctx, blacklist_principal);
1964 krb5_free_principal(ctx, self_princ);
1965 krb5_cc_destroy(ctx, tmp_cc);
1969 code = krb5_get_creds(ctx, options, tmp_cc,
1970 self_princ, &s4u2self_creds);
1971 krb5_get_creds_opt_free(ctx, options);
1972 krb5_free_principal(ctx, self_princ);
1974 krb5_free_principal(ctx, blacklist_principal);
1975 krb5_cc_destroy(ctx, tmp_cc);
1980 krb5_cc_destroy(ctx, tmp_cc);
1983 * Now make sure we store the impersonated principal
1984 * and creds instead of the TGT related stuff
1985 * in the krb5_ccache of the caller.
1987 code = krb5_copy_creds_contents(ctx, s4u2self_creds,
1989 krb5_free_creds(ctx, s4u2self_creds);
1995 * It's important to store the principal the KDC
1996 * returned, as otherwise the caller would not find
1997 * the S4U2Self ticket in the krb5_ccache lookup.
1999 store_principal = store_creds.client;
2004 * We are trying S4U2Proxy:
2006 * We need the ticket from the S4U2Self step
2007 * and our TGT in order to get the delegated ticket.
2009 code = decode_Ticket((const uint8_t *)s4u2self_creds->ticket.data,
2010 s4u2self_creds->ticket.length,
2012 &s4u2self_ticketlen);
2014 krb5_free_creds(ctx, s4u2self_creds);
2015 krb5_free_principal(ctx, blacklist_principal);
2016 krb5_cc_destroy(ctx, tmp_cc);
2021 * we need to remember the client principal of the
2022 * S4U2Self stage and as it needs to match the one we
2023 * will get for the S4U2Proxy stage. We need this
2024 * in order to detect KDCs which does not support S4U2Proxy.
2026 whitelist_principal = s4u2self_creds->client;
2027 s4u2self_creds->client = NULL;
2028 krb5_free_creds(ctx, s4u2self_creds);
2031 * For S4U2Proxy we also got a target service principal,
2032 * which also belongs to our own realm (available on
2033 * our client principal).
2035 code = krb5_parse_name(ctx, target_service, &target_princ);
2037 free_Ticket(&s4u2self_ticket);
2038 krb5_free_principal(ctx, whitelist_principal);
2039 krb5_free_principal(ctx, blacklist_principal);
2040 krb5_cc_destroy(ctx, tmp_cc);
2044 code = krb5_principal_set_realm(ctx, target_princ, self_realm);
2046 free_Ticket(&s4u2self_ticket);
2047 krb5_free_principal(ctx, target_princ);
2048 krb5_free_principal(ctx, whitelist_principal);
2049 krb5_free_principal(ctx, blacklist_principal);
2050 krb5_cc_destroy(ctx, tmp_cc);
2054 code = krb5_get_creds_opt_alloc(ctx, &options);
2056 free_Ticket(&s4u2self_ticket);
2057 krb5_free_principal(ctx, target_princ);
2058 krb5_free_principal(ctx, whitelist_principal);
2059 krb5_free_principal(ctx, blacklist_principal);
2060 krb5_cc_destroy(ctx, tmp_cc);
2064 krb5_get_creds_opt_set_options(ctx, options, KRB5_GC_FORWARDABLE);
2065 krb5_get_creds_opt_set_options(ctx, options, KRB5_GC_CONSTRAINED_DELEGATION);
2067 code = krb5_get_creds_opt_set_ticket(ctx, options, &s4u2self_ticket);
2068 free_Ticket(&s4u2self_ticket);
2070 krb5_get_creds_opt_free(ctx, options);
2071 krb5_free_principal(ctx, target_princ);
2072 krb5_free_principal(ctx, whitelist_principal);
2073 krb5_free_principal(ctx, blacklist_principal);
2074 krb5_cc_destroy(ctx, tmp_cc);
2078 code = krb5_get_creds(ctx, options, tmp_cc,
2079 target_princ, &s4u2proxy_creds);
2080 krb5_get_creds_opt_free(ctx, options);
2081 krb5_free_principal(ctx, target_princ);
2082 krb5_cc_destroy(ctx, tmp_cc);
2084 krb5_free_principal(ctx, whitelist_principal);
2085 krb5_free_principal(ctx, blacklist_principal);
2090 * Now make sure we store the impersonated principal
2091 * and creds instead of the TGT related stuff
2092 * in the krb5_ccache of the caller.
2094 code = krb5_copy_creds_contents(ctx, s4u2proxy_creds,
2096 krb5_free_creds(ctx, s4u2proxy_creds);
2098 krb5_free_principal(ctx, whitelist_principal);
2099 krb5_free_principal(ctx, blacklist_principal);
2104 * It's important to store the principal the KDC
2105 * returned, as otherwise the caller would not find
2106 * the S4U2Self ticket in the krb5_ccache lookup.
2108 store_principal = store_creds.client;
2111 if (blacklist_principal &&
2112 krb5_principal_compare(ctx, store_creds.client, blacklist_principal)) {
2116 code = krb5_unparse_name(ctx, blacklist_principal, &sp);
2120 code = krb5_unparse_name(ctx, impersonate_principal, &ip);
2124 DEBUG(1, ("kerberos_kinit_password_cc: "
2125 "KDC returned self principal[%s] while impersonating [%s]\n",
2126 sp?sp:"<no memory>",
2127 ip?ip:"<no memory>"));
2132 krb5_free_principal(ctx, whitelist_principal);
2133 krb5_free_principal(ctx, blacklist_principal);
2134 krb5_free_cred_contents(ctx, &store_creds);
2135 return KRB5_FWD_BAD_PRINCIPAL;
2137 if (blacklist_principal) {
2138 krb5_free_principal(ctx, blacklist_principal);
2141 if (whitelist_principal &&
2142 !krb5_principal_compare(ctx, store_creds.client, whitelist_principal)) {
2146 code = krb5_unparse_name(ctx, store_creds.client, &sp);
2150 code = krb5_unparse_name(ctx, whitelist_principal, &ep);
2154 DEBUG(1, ("kerberos_kinit_password_cc: "
2155 "KDC returned wrong principal[%s] we expected [%s]\n",
2156 sp?sp:"<no memory>",
2157 ep?ep:"<no memory>"));
2162 krb5_free_principal(ctx, whitelist_principal);
2163 krb5_free_cred_contents(ctx, &store_creds);
2164 return KRB5_FWD_BAD_PRINCIPAL;
2166 if (whitelist_principal) {
2167 krb5_free_principal(ctx, whitelist_principal);
2170 code = krb5_cc_initialize(ctx, store_cc, store_principal);
2172 krb5_free_cred_contents(ctx, &store_creds);
2176 code = krb5_cc_store_cred(ctx, store_cc, &store_creds);
2178 krb5_free_cred_contents(ctx, &store_creds);
2183 *expire_time = (time_t) store_creds.times.endtime;
2187 *kdc_time = (time_t) store_creds.times.starttime;
2190 krb5_free_cred_contents(ctx, &store_creds);
2196 #if !defined(HAVE_KRB5_MAKE_PRINCIPAL) && defined(HAVE_KRB5_BUILD_PRINCIPAL_ALLOC_VA)
2197 krb5_error_code smb_krb5_make_principal(krb5_context context,
2198 krb5_principal *principal,
2199 const char *_realm, ...)
2201 krb5_error_code code;
2207 realm = discard_const_p(char, _realm);
2210 code = krb5_get_default_realm(context, &realm);
2217 va_start(ap, _realm);
2218 code = krb5_build_principal_alloc_va(context, principal,
2219 strlen(realm), realm,
2224 krb5_free_default_realm(context, realm);
2231 #if !defined(HAVE_KRB5_CC_GET_LIFETIME) && defined(HAVE_KRB5_CC_RETRIEVE_CRED)
2233 * @brief Get the lifetime of the initial ticket in the cache.
2235 * @param[in] context The kerberos context.
2237 * @param[in] id The credential cache to get the ticket lifetime.
2239 * @param[out] t A pointer to a time value to store the lifetime.
2241 * @return 0 on success, a krb5_error_code on error.
2243 krb5_error_code smb_krb5_cc_get_lifetime(krb5_context context,
2247 krb5_cc_cursor cursor;
2248 krb5_error_code kerr;
2254 kerr = krb5_timeofday(context, &now);
2259 kerr = krb5_cc_start_seq_get(context, id, &cursor);
2264 while ((kerr = krb5_cc_next_cred(context, id, &cursor, &cred)) == 0) {
2265 #ifndef HAVE_FLAGS_IN_KRB5_CREDS
2266 if (cred.ticket_flags & TKT_FLG_INITIAL) {
2268 if (cred.flags.b.initial) {
2270 if (now < cred.times.endtime) {
2271 *t = (time_t) (cred.times.endtime - now);
2273 krb5_free_cred_contents(context, &cred);
2276 krb5_free_cred_contents(context, &cred);
2279 krb5_cc_end_seq_get(context, id, &cursor);
2283 #endif /* HAVE_KRB5_CC_GET_LIFETIME */
2285 #if !defined(HAVE_KRB5_FREE_CHECKSUM_CONTENTS) && defined(HAVE_FREE_CHECKSUM)
2286 void smb_krb5_free_checksum_contents(krb5_context ctx, krb5_checksum *cksum)
2288 free_Checksum(cksum);
2292 krb5_error_code smb_krb5_make_pac_checksum(TALLOC_CTX *mem_ctx,
2293 DATA_BLOB *pac_data,
2294 krb5_context context,
2295 const krb5_keyblock *keyblock,
2297 DATA_BLOB *sig_blob)
2299 krb5_error_code ret;
2300 krb5_checksum cksum;
2301 #if defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CREATE_CHECKSUM)
2305 ret = krb5_crypto_init(context,
2310 DEBUG(0,("krb5_crypto_init() failed: %s\n",
2311 smb_get_krb5_error_message(context, ret, mem_ctx)));
2314 ret = krb5_create_checksum(context,
2316 KRB5_KU_OTHER_CKSUM,
2322 DEBUG(2, ("PAC Verification failed: %s\n",
2323 smb_get_krb5_error_message(context, ret, mem_ctx)));
2326 krb5_crypto_destroy(context, crypto);
2332 *sig_type = cksum.cksumtype;
2333 *sig_blob = data_blob_talloc(mem_ctx,
2334 cksum.checksum.data,
2335 cksum.checksum.length);
2336 #elif defined(HAVE_KRB5_C_MAKE_CHECKSUM)
2339 input.data = (char *)pac_data->data;
2340 input.length = pac_data->length;
2342 ret = krb5_c_make_checksum(context,
2345 KRB5_KEYUSAGE_APP_DATA_CKSUM,
2349 DEBUG(2, ("PAC Verification failed: %s\n",
2350 smb_get_krb5_error_message(context, ret, mem_ctx)));
2354 *sig_type = cksum.checksum_type;
2355 *sig_blob = data_blob_talloc(mem_ctx,
2360 #error krb5_create_checksum or krb5_c_make_checksum not available
2361 #endif /* HAVE_KRB5_C_MAKE_CHECKSUM */
2362 smb_krb5_free_checksum_contents(context, &cksum);
2369 * smb_krb5_principal_get_realm
2371 * @brief Get realm of a principal
2373 * @param[in] context The krb5_context
2374 * @param[in] principal The principal
2375 * @return pointer to the realm
2377 * Caller must free if the return value is not NULL.
2381 char *smb_krb5_principal_get_realm(krb5_context context,
2382 krb5_const_principal principal)
2384 #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
2385 return strdup(discard_const_p(char, krb5_principal_get_realm(context, principal)));
2386 #elif defined(krb5_princ_realm) /* MIT */
2388 realm = discard_const_p(krb5_data,
2389 krb5_princ_realm(context, principal));
2390 return strndup(realm->data, realm->length);
2392 #error UNKNOWN_GET_PRINC_REALM_FUNCTIONS
2397 * smb_krb5_principal_set_realm
2399 * @brief Get realm of a principal
2401 * @param[in] context The krb5_context
2402 * @param[in] principal The principal
2403 * @param[in] realm The realm
2404 * @return 0 on success, a krb5_error_code on error.
2408 krb5_error_code smb_krb5_principal_set_realm(krb5_context context,
2409 krb5_principal principal,
2412 #ifdef HAVE_KRB5_PRINCIPAL_SET_REALM /* Heimdal */
2413 return krb5_principal_set_realm(context, principal, realm);
2414 #elif defined(krb5_princ_realm) && defined(krb5_princ_set_realm) /* MIT */
2415 krb5_error_code ret;
2417 krb5_data *old_data;
2419 old_data = krb5_princ_realm(context, principal);
2421 ret = smb_krb5_copy_data_contents(&data,
2428 /* free realm before setting */
2429 free(old_data->data);
2431 krb5_princ_set_realm(context, principal, &data);
2435 #error UNKNOWN_PRINC_SET_REALM_FUNCTION
2440 /************************************************************************
2441 Routine to get the default realm from the kerberos credentials cache.
2442 Caller must free if the return value is not NULL.
2443 ************************************************************************/
2445 static char *smb_krb5_get_default_realm_from_ccache(TALLOC_CTX *mem_ctx)
2448 krb5_context ctx = NULL;
2449 krb5_ccache cc = NULL;
2450 krb5_principal princ = NULL;
2452 initialize_krb5_error_table();
2453 if (krb5_init_context(&ctx)) {
2457 DEBUG(5,("kerberos_get_default_realm_from_ccache: "
2458 "Trying to read krb5 cache: %s\n",
2459 krb5_cc_default_name(ctx)));
2460 if (krb5_cc_default(ctx, &cc)) {
2461 DEBUG(5,("kerberos_get_default_realm_from_ccache: "
2462 "failed to read default cache\n"));
2465 if (krb5_cc_get_principal(ctx, cc, &princ)) {
2466 DEBUG(5,("kerberos_get_default_realm_from_ccache: "
2467 "failed to get default principal\n"));
2471 #if defined(HAVE_KRB5_PRINCIPAL_GET_REALM)
2472 realm = talloc_strdup(mem_ctx, krb5_principal_get_realm(ctx, princ));
2473 #elif defined(HAVE_KRB5_PRINC_REALM)
2475 krb5_data *realm_data = krb5_princ_realm(ctx, princ);
2476 realm = talloc_strndup(mem_ctx, realm_data->data, realm_data->length);
2484 krb5_free_principal(ctx, princ);
2487 krb5_cc_close(ctx, cc);
2489 krb5_free_context(ctx);
2495 /************************************************************************
2496 Routine to get the realm from a given DNS name.
2497 ************************************************************************/
2499 static char *smb_krb5_get_realm_from_hostname(TALLOC_CTX *mem_ctx,
2500 const char *hostname)
2502 #if defined(HAVE_KRB5_REALM_TYPE)
2504 krb5_realm *realm_list = NULL;
2507 char **realm_list = NULL;
2510 krb5_error_code kerr;
2511 krb5_context ctx = NULL;
2513 initialize_krb5_error_table();
2514 if (krb5_init_context(&ctx)) {
2518 kerr = krb5_get_host_realm(ctx, hostname, &realm_list);
2520 DEBUG(3,("kerberos_get_realm_from_hostname %s: "
2522 hostname ? hostname : "(NULL)",
2523 error_message(kerr) ));
2527 if (realm_list && realm_list[0]) {
2528 realm = talloc_strdup(mem_ctx, realm_list[0]);
2535 krb5_free_host_realm(ctx, realm_list);
2538 krb5_free_context(ctx);
2544 char *kerberos_get_principal_from_service_hostname(TALLOC_CTX *mem_ctx,
2545 const char *service,
2546 const char *remote_name,
2547 const char *default_realm)
2552 host = strchr_m(remote_name, '.');
2555 realm = smb_krb5_get_realm_from_hostname(talloc_tos(),
2558 /* NetBIOS name - use our realm. */
2559 realm = smb_krb5_get_default_realm_from_ccache(talloc_tos());
2562 if (realm == NULL || *realm == '\0') {
2563 realm = talloc_strdup(talloc_tos(), default_realm);
2567 DEBUG(3,("kerberos_get_principal_from_service_hostname: "
2568 "cannot get realm from, "
2569 "desthost %s or default ccache. Using default "
2570 "smb.conf realm %s\n",
2575 principal = talloc_asprintf(mem_ctx,
2577 service, remote_name,
2583 char *smb_get_krb5_error_message(krb5_context context,
2584 krb5_error_code code,
2585 TALLOC_CTX *mem_ctx)
2589 #if defined(HAVE_KRB5_GET_ERROR_MESSAGE) && defined(HAVE_KRB5_FREE_ERROR_MESSAGE)
2590 const char *context_error = krb5_get_error_message(context, code);
2591 if (context_error) {
2592 ret = talloc_asprintf(mem_ctx, "%s: %s",
2593 error_message(code), context_error);
2594 krb5_free_error_message(context, context_error);
2598 ret = talloc_strdup(mem_ctx, error_message(code));
2604 * @brief Return the kerberos library setting for "libdefaults:allow_weak_crypto"
2606 * @param context The krb5_context
2608 * @return krb5_boolean
2610 * Function returns true if weak crypto is allowd, false if not
2613 krb5_boolean smb_krb5_get_allowed_weak_crypto(krb5_context context)
2614 #if defined(HAVE_KRB5_CONFIG_GET_BOOL_DEFAULT)
2616 return krb5_config_get_bool_default(context,
2620 "allow_weak_crypto",
2623 #elif defined(HAVE_PROFILE_H) && defined(HAVE_KRB5_GET_PROFILE)
2625 #include <profile.h>
2626 krb5_error_code ret;
2627 krb5_boolean ret_default = false;
2631 ret = krb5_get_profile(context,
2637 ret = profile_get_boolean(profile,
2639 "allow_weak_crypto",
2640 NULL, /* subsubname */
2641 ret_default, /* def_val */
2642 &ret_profile /* *ret_default */);
2647 profile_release(profile);
2652 #error UNKNOWN_KRB5_CONFIG_ROUTINES
2656 * @brief Return the type of a krb5_principal
2658 * @param context The krb5_context
2659 * @param principal The const krb5_principal
2661 * @return integer type of the principal
2663 int smb_krb5_principal_get_type(krb5_context context,
2664 krb5_const_principal principal)
2666 #ifdef HAVE_KRB5_PRINCIPAL_GET_TYPE /* Heimdal */
2667 return krb5_principal_get_type(context, principal);
2668 #elif defined(krb5_princ_type) /* MIT */
2669 return krb5_princ_type(context, principal);
2671 #error UNKNOWN_PRINC_GET_TYPE_FUNCTION
2676 * @brief Set the type of a krb5_principal
2678 * @param context The krb5_context
2679 * @param principal The const krb5_principal
2680 * @param type The principal type
2683 void smb_krb5_principal_set_type(krb5_context context,
2684 krb5_principal principal,
2687 #ifdef HAVE_KRB5_PRINCIPAL_SET_TYPE /* Heimdal */
2688 krb5_principal_set_type(context, principal, type);
2689 #elif defined(krb5_princ_type) /* MIT */
2690 krb5_princ_type(context, principal) = type;
2692 #error UNKNOWN_PRINC_SET_TYPE_FUNCTION
2697 * @brief Generate a krb5 warning, forwarding to com_err
2699 * @param context The krb5_context
2700 * @param fmt The message format
2701 * @param ... The message arguments
2705 #if !defined(HAVE_KRB5_WARNX)
2706 krb5_error_code krb5_warnx(krb5_context context, const char *fmt, ...)
2710 va_start(args, fmt);
2711 com_err_va("kdb_samba", errno, fmt, args);
2718 krb5_error_code smb_krb5_cc_copy_creds(krb5_context context,
2719 krb5_ccache incc, krb5_ccache outcc)
2721 #ifdef HAVE_KRB5_CC_COPY_CACHE /* Heimdal */
2722 return krb5_cc_copy_cache(context, incc, outcc);
2723 #elif defined(HAVE_KRB5_CC_COPY_CREDS)
2724 return krb5_cc_copy_creds(context, incc, outcc);
2726 #error UNKNOWN_KRB5_CC_COPY_CACHE_OR_CREDS_FUNCTION
2730 /**********************************************************
2732 **********************************************************/
2734 static bool ads_cleanup_expired_creds(krb5_context context,
2738 krb5_error_code retval;
2739 const char *cc_type = krb5_cc_get_type(context, ccache);
2741 DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n",
2742 cc_type, krb5_cc_get_name(context, ccache),
2743 http_timestring(talloc_tos(), credsp->times.endtime)));
2745 /* we will probably need new tickets if the current ones
2746 will expire within 10 seconds.
2748 if (credsp->times.endtime >= (time(NULL) + 10))
2751 /* heimdal won't remove creds from a file ccache, and
2752 perhaps we shouldn't anyway, since internally we
2753 use memory ccaches, and a FILE one probably means that
2754 we're using creds obtained outside of our exectuable
2756 if (strequal(cc_type, "FILE")) {
2757 DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type));
2761 retval = krb5_cc_remove_cred(context, ccache, 0, credsp);
2763 DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n",
2764 error_message(retval)));
2765 /* If we have an error in this, we want to display it,
2766 but continue as though we deleted it */
2771 /* Allocate and setup the auth context into the state we need. */
2773 static krb5_error_code ads_setup_auth_context(krb5_context context,
2774 krb5_auth_context *auth_context)
2776 krb5_error_code retval;
2778 retval = krb5_auth_con_init(context, auth_context );
2780 DEBUG(1,("krb5_auth_con_init failed (%s)\n",
2781 error_message(retval)));
2785 /* Ensure this is an addressless ticket. */
2786 retval = krb5_auth_con_setaddrs(context, *auth_context, NULL, NULL);
2788 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n",
2789 error_message(retval)));
2795 #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)
2796 static krb5_error_code ads_create_gss_checksum(krb5_data *in_data, /* [inout] */
2799 unsigned int orig_length = in_data->length;
2800 unsigned int base_cksum_size = GSSAPI_CHECKSUM_SIZE;
2801 char *gss_cksum = NULL;
2804 /* Extra length field for delgated ticket. */
2805 base_cksum_size += 4;
2808 if ((unsigned int)base_cksum_size + orig_length <
2809 (unsigned int)base_cksum_size) {
2813 gss_cksum = (char *)SMB_MALLOC(base_cksum_size + orig_length);
2814 if (gss_cksum == NULL) {
2818 memset(gss_cksum, '\0', base_cksum_size + orig_length);
2819 SIVAL(gss_cksum, 0, GSSAPI_BNDLENGTH);
2822 * GSS_C_NO_CHANNEL_BINDINGS means 16 zero bytes.
2823 * This matches the behavior of heimdal and mit.
2825 * And it is needed to work against some closed source
2830 memset(&gss_cksum[4], 0x00, GSSAPI_BNDLENGTH);
2832 SIVAL(gss_cksum, 20, gss_flags);
2835 SSVAL(gss_cksum, 24, 1); /* The Delegation Option identifier */
2836 SSVAL(gss_cksum, 26, orig_length);
2837 /* Copy the kerberos KRB_CRED data */
2838 memcpy(gss_cksum + 28, in_data->data, orig_length);
2839 free(in_data->data);
2840 in_data->data = NULL;
2841 in_data->length = 0;
2843 in_data->data = gss_cksum;
2844 in_data->length = base_cksum_size + orig_length;
2850 * We can't use krb5_mk_req because w2k wants the service to be in a particular
2853 static krb5_error_code ads_krb5_mk_req(krb5_context context,
2854 krb5_auth_context *auth_context,
2855 const krb5_flags ap_req_options,
2856 const char *principal,
2859 time_t *expire_time,
2860 const char *impersonate_princ_s)
2862 krb5_error_code retval;
2863 krb5_principal server;
2864 krb5_principal impersonate_princ = NULL;
2868 bool creds_ready = false;
2869 int i = 0, maxtries = 3;
2872 ZERO_STRUCT(in_data);
2874 retval = smb_krb5_parse_name(context, principal, &server);
2876 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal));
2880 if (impersonate_princ_s) {
2881 retval = smb_krb5_parse_name(context, impersonate_princ_s,
2882 &impersonate_princ);
2884 DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", impersonate_princ_s));
2889 /* obtain ticket & session key */
2891 if ((retval = krb5_copy_principal(context, server, &creds.server))) {
2892 DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n",
2893 error_message(retval)));
2897 retval = krb5_cc_get_principal(context, ccache, &creds.client);
2899 /* This can commonly fail on smbd startup with no ticket in the cache.
2900 * Report at higher level than 1. */
2901 DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
2902 error_message(retval)));
2906 while (!creds_ready && (i < maxtries)) {
2908 retval = smb_krb5_get_credentials(context,
2915 DBG_WARNING("smb_krb5_get_credentials failed for %s "
2918 error_message(retval));
2922 /* cope with ticket being in the future due to clock skew */
2923 if ((unsigned)credsp->times.starttime > time(NULL)) {
2924 time_t t = time(NULL);
2925 int time_offset =(int)((unsigned)credsp->times.starttime-t);
2926 DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset));
2927 krb5_set_real_time(context, t + time_offset + 1, 0);
2930 ok = ads_cleanup_expired_creds(context, ccache, credsp);
2938 DBG_DEBUG("Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
2940 krb5_cc_get_type(context, ccache),
2941 krb5_cc_get_name(context, ccache),
2942 http_timestring(talloc_tos(),
2943 (unsigned)credsp->times.endtime),
2944 (unsigned)credsp->times.endtime);
2947 *expire_time = (time_t)credsp->times.endtime;
2950 /* Allocate the auth_context. */
2951 retval = ads_setup_auth_context(context, auth_context);
2953 DBG_WARNING("ads_setup_auth_context failed (%s)\n",
2954 error_message(retval));
2958 #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)
2960 uint32_t gss_flags = 0;
2962 if (credsp->ticket_flags & TKT_FLG_OK_AS_DELEGATE) {
2964 * Fetch a forwarded TGT from the KDC so that we can
2965 * hand off a 2nd ticket as part of the kerberos
2969 DBG_INFO("Server marked as OK to delegate to, building "
2970 "forwardable TGT\n");
2972 retval = krb5_auth_con_setuseruserkey(context,
2974 &credsp->keyblock );
2976 DBG_WARNING("krb5_auth_con_setuseruserkey "
2978 error_message(retval)));
2982 /* Must use a subkey for forwarded tickets. */
2983 retval = krb5_auth_con_setflags(context,
2985 KRB5_AUTH_CONTEXT_USE_SUBKEY);
2987 DBG_WARNING("krb5_auth_con_setflags failed (%s)\n",
2988 error_message(retval)));
2992 retval = krb5_fwd_tgt_creds(context,/* Krb5 context [in] */
2993 *auth_context, /* Authentication context [in] */
2994 discard_const_p(char, KRB5_TGS_NAME), /* Ticket service name ("krbtgt") [in] */
2995 credsp->client, /* Client principal for the tgt [in] */
2996 credsp->server, /* Server principal for the tgt [in] */
2997 ccache, /* Credential cache to use for storage [in] */
2998 1, /* Turn on for "Forwardable ticket" [in] */
2999 &in_data ); /* Resulting response [out] */
3002 DBG_INFO("krb5_fwd_tgt_creds failed (%s)\n",
3003 error_message(retval));
3006 * This is not fatal. Delete the *auth_context and continue
3007 * with krb5_mk_req_extended to get a non-forwardable ticket.
3011 free( in_data.data );
3012 in_data.data = NULL;
3015 krb5_auth_con_free(context, *auth_context);
3016 *auth_context = NULL;
3017 retval = ads_setup_auth_context(context, auth_context);
3019 DBG_WARNING("ads_setup_auth_context failed (%s)\n",
3020 error_message(retval)));
3024 /* We got a delegated ticket. */
3025 gss_flags |= GSS_C_DELEG_FLAG;
3029 /* Frees and reallocates in_data into a GSS checksum blob. */
3030 retval = ads_create_gss_checksum(&in_data, gss_flags);
3035 /* We always want GSS-checksum types. */
3036 retval = krb5_auth_con_set_req_cksumtype(context, *auth_context, GSSAPI_CHECKSUM );
3038 DEBUG(1,("krb5_auth_con_set_req_cksumtype failed (%s)\n",
3039 error_message(retval)));
3045 retval = krb5_mk_req_extended(context, auth_context, ap_req_options,
3046 &in_data, credsp, outbuf);
3048 DBG_WARNING("krb5_mk_req_extended failed (%s)\n",
3049 error_message(retval));
3052 #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)
3057 free( in_data.data );
3061 krb5_free_creds(context, credsp);
3064 krb5_free_cred_contents(context, &creds);
3067 krb5_free_principal(context, server);
3068 if (impersonate_princ) {
3069 krb5_free_principal(context, impersonate_princ);
3076 get a kerberos5 ticket for the given service
3078 int ads_krb5_cli_get_ticket(TALLOC_CTX *mem_ctx,
3079 const char *principal,
3082 DATA_BLOB *session_key_krb5,
3083 uint32_t extra_ap_opts, const char *ccname,
3085 const char *impersonate_princ_s)
3087 krb5_error_code retval;
3089 krb5_context context = NULL;
3090 krb5_ccache ccdef = NULL;
3091 krb5_auth_context auth_context = NULL;
3092 krb5_enctype enc_types[] = {
3093 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
3094 ENCTYPE_AES256_CTS_HMAC_SHA1_96,
3096 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
3097 ENCTYPE_AES128_CTS_HMAC_SHA1_96,
3099 ENCTYPE_ARCFOUR_HMAC,
3100 ENCTYPE_DES_CBC_MD5,
3101 ENCTYPE_DES_CBC_CRC,
3105 initialize_krb5_error_table();
3106 retval = krb5_init_context(&context);
3108 DBG_WARNING("krb5_init_context failed (%s)\n",
3109 error_message(retval));
3113 if (time_offset != 0) {
3114 krb5_set_real_time(context, time(NULL) + time_offset, 0);
3117 retval = krb5_cc_resolve(context,
3118 ccname ? ccname : krb5_cc_default_name(context),
3121 DBG_WARNING("krb5_cc_default failed (%s)\n",
3122 error_message(retval));
3126 retval = krb5_set_default_tgs_ktypes(context, enc_types);
3128 DBG_WARNING("krb5_set_default_tgs_ktypes failed (%s)\n",
3129 error_message(retval));
3133 retval = ads_krb5_mk_req(context,
3135 AP_OPTS_USE_SUBKEY | (krb5_flags)extra_ap_opts,
3140 impersonate_princ_s);
3145 ok = smb_krb5_get_smb_session_key(mem_ctx,
3155 *ticket = data_blob_talloc(mem_ctx, packet.data, packet.length);
3157 smb_krb5_free_data_contents(context, &packet);
3163 krb5_cc_close(context, ccdef);
3166 krb5_auth_con_free(context, auth_context);
3168 krb5_free_context(context);
3174 #else /* HAVE_KRB5 */
3175 /* This saves a few linking headaches */
3176 int ads_krb5_cli_get_ticket(TALLOC_CTX *mem_ctx,
3177 const char *principal,
3180 DATA_BLOB *session_key_krb5,
3181 uint32_t extra_ap_opts, const char *ccname,
3183 const char *impersonate_princ_s)
3185 DEBUG(0,("NO KERBEROS SUPPORT\n"));
3189 #endif /* HAVE_KRB5 */