2 Unix SMB/CIFS implementation.
3 kerberos utility library
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Remus Koos 2001
6 Copyright (C) Nalin Dahyabhai <nalin@redhat.com> 2004.
7 Copyright (C) Jeremy Allison 2004.
8 Copyright (C) Gerald Carter 2006.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "libsmb/namequery.h"
26 #include "system/filesys.h"
28 #include "../librpc/gen_ndr/ndr_misc.h"
29 #include "libads/kerberos_proto.h"
30 #include "libads/cldap.h"
32 #include "../lib/tsocket/tsocket.h"
33 #include "lib/util/asn1.h"
37 #define LIBADS_CCACHE_NAME "MEMORY:libads"
40 we use a prompter to avoid a crash bug in the kerberos libs when
41 dealing with empty passwords
42 this prompter is just a string copy ...
44 static krb5_error_code
45 kerb_prompter(krb5_context ctx, void *data,
49 krb5_prompt prompts[])
51 if (num_prompts == 0) return 0;
52 if (num_prompts == 2) {
54 * only heimdal has a prompt type and we need to deal with it here to
57 * removing the prompter completely is not an option as at least these
58 * versions would crash: heimdal-1.0.2 and heimdal-1.1. Later heimdal
59 * version have looping detection and return with a proper error code.
62 #if defined(HAVE_KRB5_PROMPT_TYPE) /* Heimdal */
63 if (prompts[0].type == KRB5_PROMPT_TYPE_NEW_PASSWORD &&
64 prompts[1].type == KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN) {
66 * We don't want to change passwords here. We're
67 * called from heimal when the KDC returns
68 * KRB5KDC_ERR_KEY_EXPIRED, but at this point we don't
69 * have the chance to ask the user for a new
70 * password. If we return 0 (i.e. success), we will be
71 * spinning in the endless for-loop in
72 * change_password() in
73 * source4/heimdal/lib/krb5/init_creds_pw.c:526ff
75 return KRB5KDC_ERR_KEY_EXPIRED;
77 #elif defined(HAVE_KRB5_GET_PROMPT_TYPES) /* MIT */
78 krb5_prompt_type *prompt_types = NULL;
80 prompt_types = krb5_get_prompt_types(ctx);
81 if (prompt_types != NULL) {
82 if (prompt_types[0] == KRB5_PROMPT_TYPE_NEW_PASSWORD &&
83 prompt_types[1] == KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN) {
84 return KRB5KDC_ERR_KEY_EXP;
90 memset(prompts[0].reply->data, '\0', prompts[0].reply->length);
91 if (prompts[0].reply->length > 0) {
93 strncpy((char *)prompts[0].reply->data, (const char *)data,
94 prompts[0].reply->length-1);
95 prompts[0].reply->length = strlen((const char *)prompts[0].reply->data);
97 prompts[0].reply->length = 0;
104 simulate a kinit, putting the tgt in the given cache location. If cache_name == NULL
105 place in default cache location.
108 int kerberos_kinit_password_ext(const char *given_principal,
109 const char *password,
112 time_t *renew_till_time,
113 const char *cache_name,
115 bool add_netbios_addr,
116 time_t renewable_time,
118 char **_canon_principal,
122 TALLOC_CTX *frame = talloc_stackframe();
123 krb5_context ctx = NULL;
124 krb5_error_code code = 0;
125 krb5_ccache cc = NULL;
126 krb5_principal me = NULL;
127 krb5_principal canon_princ = NULL;
129 krb5_get_init_creds_opt *opt = NULL;
130 smb_krb5_addresses *addr = NULL;
131 char *canon_principal = NULL;
132 char *canon_realm = NULL;
134 ZERO_STRUCT(my_creds);
136 code = smb_krb5_init_context_common(&ctx);
138 DBG_ERR("kerberos init context failed (%s)\n",
139 error_message(code));
144 if (time_offset != 0) {
145 krb5_set_real_time(ctx, time(NULL) + time_offset, 0);
148 DBG_DEBUG("as %s using [%s] as ccache and config [%s]\n",
150 cache_name ? cache_name: krb5_cc_default_name(ctx),
151 getenv("KRB5_CONFIG"));
153 if ((code = krb5_cc_resolve(ctx, cache_name ? cache_name : krb5_cc_default_name(ctx), &cc))) {
157 if ((code = smb_krb5_parse_name(ctx, given_principal, &me))) {
161 if ((code = krb5_get_init_creds_opt_alloc(ctx, &opt))) {
165 krb5_get_init_creds_opt_set_renew_life(opt, renewable_time);
166 krb5_get_init_creds_opt_set_forwardable(opt, True);
168 /* Turn on canonicalization for lower case realm support */
169 #ifdef SAMBA4_USES_HEIMDAL
170 krb5_get_init_creds_opt_set_win2k(ctx, opt, true);
171 krb5_get_init_creds_opt_set_canonicalize(ctx, opt, true);
173 krb5_get_init_creds_opt_set_canonicalize(opt, true);
177 krb5_get_init_creds_opt_set_tkt_life(opt, 60);
180 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_SET_PAC_REQUEST
182 if ((code = krb5_get_init_creds_opt_set_pac_request(ctx, opt, (krb5_boolean)request_pac))) {
187 if (add_netbios_addr) {
188 if ((code = smb_krb5_gen_netbios_krb5_address(&addr,
189 lp_netbios_name()))) {
192 krb5_get_init_creds_opt_set_address_list(opt, addr->addrs);
195 if ((code = krb5_get_init_creds_password(ctx, &my_creds, me, discard_const_p(char,password),
196 kerb_prompter, discard_const_p(char, password),
201 canon_princ = my_creds.client;
203 code = smb_krb5_unparse_name(frame,
211 DBG_DEBUG("%s mapped to %s\n", given_principal, canon_principal);
213 canon_realm = smb_krb5_principal_get_realm(frame, ctx, canon_princ);
214 if (canon_realm == NULL) {
219 if ((code = krb5_cc_initialize(ctx, cc, canon_princ))) {
223 if ((code = krb5_cc_store_cred(ctx, cc, &my_creds))) {
228 *expire_time = (time_t) my_creds.times.endtime;
231 if (renew_till_time) {
232 *renew_till_time = (time_t) my_creds.times.renew_till;
235 if (_canon_principal != NULL) {
236 *_canon_principal = talloc_move(mem_ctx, &canon_principal);
238 if (_canon_realm != NULL) {
239 *_canon_realm = talloc_move(mem_ctx, &canon_realm);
245 *ntstatus = NT_STATUS_OK;
249 /* fall back to self-made-mapping */
250 *ntstatus = krb5_to_nt_status(code);
254 krb5_free_cred_contents(ctx, &my_creds);
256 krb5_free_principal(ctx, me);
259 smb_krb5_free_addresses(ctx, addr);
262 krb5_get_init_creds_opt_free(ctx, opt);
265 krb5_cc_close(ctx, cc);
268 krb5_free_context(ctx);
274 int ads_kdestroy(const char *cc_name)
276 krb5_error_code code;
277 krb5_context ctx = NULL;
278 krb5_ccache cc = NULL;
280 code = smb_krb5_init_context_common(&ctx);
282 DBG_ERR("kerberos init context failed (%s)\n",
283 error_message(code));
288 if ((code = krb5_cc_default(ctx, &cc))) {
289 krb5_free_context(ctx);
293 if ((code = krb5_cc_resolve(ctx, cc_name, &cc))) {
294 DEBUG(3, ("ads_kdestroy: krb5_cc_resolve failed: %s\n",
295 error_message(code)));
296 krb5_free_context(ctx);
301 if ((code = krb5_cc_destroy (ctx, cc))) {
302 DEBUG(3, ("ads_kdestroy: krb5_cc_destroy failed: %s\n",
303 error_message(code)));
306 krb5_free_context (ctx);
310 int create_kerberos_key_from_string(krb5_context context,
311 krb5_principal host_princ,
312 krb5_principal salt_princ,
315 krb5_enctype enctype,
320 * Check if we've determined that the KDC is salting keys for this
321 * principal/enctype in a non-obvious way. If it is, try to match
325 KRB5_KEY_DATA(key) = (KRB5_KEY_DATA_CAST *)SMB_MALLOC(password->length);
326 if (!KRB5_KEY_DATA(key)) {
329 memcpy(KRB5_KEY_DATA(key), password->data, password->length);
330 KRB5_KEY_LENGTH(key) = password->length;
331 KRB5_KEY_TYPE(key) = enctype;
334 ret = smb_krb5_create_key_from_string(context,
335 salt_princ ? salt_princ : host_princ,
343 /************************************************************************
344 ************************************************************************/
346 int kerberos_kinit_password(const char *principal,
347 const char *password,
349 const char *cache_name)
351 return kerberos_kinit_password_ext(principal,
366 /************************************************************************
367 ************************************************************************/
369 /************************************************************************
370 Create a string list of available kdc's, possibly searching by sitename.
373 If "sitename" is given, the DC's in that site are listed first.
375 ************************************************************************/
377 static void add_sockaddr_unique(struct sockaddr_storage *addrs, size_t *num_addrs,
378 const struct sockaddr_storage *addr)
382 for (i=0; i<*num_addrs; i++) {
383 if (sockaddr_equal((const struct sockaddr *)&addrs[i],
384 (const struct sockaddr *)addr)) {
392 /* print_canonical_sockaddr prints an ipv6 addr in the form of
393 * [ipv6.addr]. This string, when put in a generated krb5.conf file is not
394 * always properly dealt with by some older krb5 libraries. Adding the hard-coded
395 * portnumber workarounds the issue. - gd */
397 static char *print_canonical_sockaddr_with_port(TALLOC_CTX *mem_ctx,
398 const struct sockaddr_storage *pss)
402 str = print_canonical_sockaddr(mem_ctx, pss);
407 if (pss->ss_family != AF_INET6) {
411 #if defined(HAVE_IPV6)
412 str = talloc_asprintf_append(str, ":88");
417 static char *get_kdc_ip_string(char *mem_ctx,
419 const char *sitename,
420 const struct sockaddr_storage *pss)
422 TALLOC_CTX *frame = talloc_stackframe();
424 struct samba_sockaddr *ip_sa_site = NULL;
425 struct samba_sockaddr *ip_sa_nonsite = NULL;
426 struct samba_sockaddr sa = {0};
427 size_t count_site = 0;
428 size_t count_nonsite;
430 struct sockaddr_storage *dc_addrs = NULL;
431 struct tsocket_address **dc_addrs2 = NULL;
432 const struct tsocket_address * const *dc_addrs3 = NULL;
434 struct netlogon_samlogon_response **responses = NULL;
437 char *kdc_str = NULL;
439 SMB_ASSERT(pss != NULL);
441 kdc_str = talloc_asprintf(mem_ctx,
443 print_canonical_sockaddr_with_port(mem_ctx,
445 if (kdc_str == NULL) {
450 ok = sockaddr_storage_to_samba_sockaddr(&sa, pss);
452 TALLOC_FREE(kdc_str);
457 * First get the KDC's only in this site, the rest will be
462 status = get_kdc_list(talloc_tos(),
467 if (!NT_STATUS_IS_OK(status)) {
468 DBG_ERR("get_kdc_list fail %s\n",
470 TALLOC_FREE(kdc_str);
473 DBG_DEBUG("got %zu addresses from site %s search\n",
480 status = get_kdc_list(talloc_tos(),
485 if (!NT_STATUS_IS_OK(status)) {
486 DBG_ERR("get_kdc_list (site-less) fail %s\n",
488 TALLOC_FREE(kdc_str);
491 DBG_DEBUG("got %zu addresses from site-less search\n", count_nonsite);
493 if (count_site + count_nonsite < count_site) {
495 DBG_ERR("get_kdc_list_talloc (site-less) fail wrap error\n");
496 TALLOC_FREE(kdc_str);
501 dc_addrs = talloc_array(talloc_tos(), struct sockaddr_storage,
502 count_site + count_nonsite);
503 if (dc_addrs == NULL) {
504 TALLOC_FREE(kdc_str);
510 for (i = 0; i < count_site; i++) {
511 if (!sockaddr_equal(&sa.u.sa, &ip_sa_site[i].u.sa)) {
512 add_sockaddr_unique(dc_addrs, &num_dcs,
513 &ip_sa_site[i].u.ss);
517 for (i = 0; i < count_nonsite; i++) {
518 if (!sockaddr_equal(&sa.u.sa, &ip_sa_nonsite[i].u.sa)) {
519 add_sockaddr_unique(dc_addrs, &num_dcs,
520 &ip_sa_nonsite[i].u.ss);
524 DBG_DEBUG("%zu additional KDCs to test\n", num_dcs);
526 TALLOC_FREE(kdc_str);
530 dc_addrs2 = talloc_zero_array(talloc_tos(),
531 struct tsocket_address *,
533 if (dc_addrs2 == NULL) {
534 TALLOC_FREE(kdc_str);
538 for (i=0; i<num_dcs; i++) {
539 char addr[INET6_ADDRSTRLEN];
542 print_sockaddr(addr, sizeof(addr), &dc_addrs[i]);
544 ret = tsocket_address_inet_from_strings(dc_addrs2, "ip",
548 status = map_nt_error_from_unix(errno);
549 DEBUG(2,("Failed to create tsocket_address for %s - %s\n",
550 addr, nt_errstr(status)));
551 TALLOC_FREE(kdc_str);
556 dc_addrs3 = (const struct tsocket_address * const *)dc_addrs2;
558 status = cldap_multi_netlogon(talloc_tos(),
560 realm, lp_netbios_name(),
561 NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX,
562 MIN(num_dcs, 3), timeval_current_ofs(3, 0), &responses);
563 TALLOC_FREE(dc_addrs2);
566 if (!NT_STATUS_IS_OK(status)) {
567 DEBUG(10,("get_kdc_ip_string: cldap_multi_netlogon failed: "
568 "%s\n", nt_errstr(status)));
569 TALLOC_FREE(kdc_str);
573 for (i=0; i<num_dcs; i++) {
576 if (responses[i] == NULL) {
580 /* Append to the string - inefficient but not done often. */
581 new_kdc_str = talloc_asprintf(mem_ctx, "%s\t\tkdc = %s\n",
583 print_canonical_sockaddr_with_port(mem_ctx, &dc_addrs[i]));
584 TALLOC_FREE(kdc_str);
585 if (new_kdc_str == NULL) {
588 kdc_str = new_kdc_str;
593 if (result != NULL) {
594 DBG_DEBUG("Returning\n%s\n", kdc_str);
596 DBG_NOTICE("Failed to get KDC ip address\n");
599 TALLOC_FREE(ip_sa_site);
600 TALLOC_FREE(ip_sa_nonsite);
605 /************************************************************************
606 Create a specific krb5.conf file in the private directory pointing
607 at a specific kdc for a realm. Keyed off domain name. Sets
608 KRB5_CONFIG environment variable to point to this file. Must be
609 run as root or will fail (which is a good thing :-).
610 ************************************************************************/
612 #if !defined(SAMBA4_USES_HEIMDAL) /* MIT version */
613 static char *get_enctypes(TALLOC_CTX *mem_ctx)
615 char *aes_enctypes = NULL;
616 const char *legacy_enctypes = "";
617 char *enctypes = NULL;
619 aes_enctypes = talloc_strdup(mem_ctx, "");
620 if (aes_enctypes == NULL) {
624 if (lp_kerberos_encryption_types() == KERBEROS_ETYPES_ALL ||
625 lp_kerberos_encryption_types() == KERBEROS_ETYPES_STRONG) {
626 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
627 aes_enctypes = talloc_asprintf_append(
628 aes_enctypes, "%s", "aes256-cts-hmac-sha1-96 ");
629 if (aes_enctypes == NULL) {
633 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
634 aes_enctypes = talloc_asprintf_append(
635 aes_enctypes, "%s", "aes128-cts-hmac-sha1-96");
636 if (aes_enctypes == NULL) {
642 if (lp_weak_crypto() == SAMBA_WEAK_CRYPTO_ALLOWED &&
643 (lp_kerberos_encryption_types() == KERBEROS_ETYPES_ALL ||
644 lp_kerberos_encryption_types() == KERBEROS_ETYPES_LEGACY)) {
645 legacy_enctypes = "RC4-HMAC";
649 talloc_asprintf(mem_ctx, "\tdefault_tgs_enctypes = %s %s\n"
650 "\tdefault_tkt_enctypes = %s %s\n"
651 "\tpreferred_enctypes = %s %s\n",
652 aes_enctypes, legacy_enctypes, aes_enctypes,
653 legacy_enctypes, aes_enctypes, legacy_enctypes);
655 TALLOC_FREE(aes_enctypes);
658 #else /* Heimdal version */
659 static char *get_enctypes(TALLOC_CTX *mem_ctx)
661 const char *aes_enctypes = "";
662 const char *legacy_enctypes = "";
663 char *enctypes = NULL;
665 if (lp_kerberos_encryption_types() == KERBEROS_ETYPES_ALL ||
666 lp_kerberos_encryption_types() == KERBEROS_ETYPES_STRONG) {
668 "aes256-cts-hmac-sha1-96 aes128-cts-hmac-sha1-96";
671 if (lp_kerberos_encryption_types() == KERBEROS_ETYPES_ALL ||
672 lp_kerberos_encryption_types() == KERBEROS_ETYPES_LEGACY) {
673 legacy_enctypes = "arcfour-hmac-md5 des-cbc-crc des-cbc-md5";
676 enctypes = talloc_asprintf(mem_ctx, "\tdefault_etypes = %s %s\n",
677 aes_enctypes, legacy_enctypes);
683 bool create_local_private_krb5_conf_for_domain(const char *realm,
685 const char *sitename,
686 const struct sockaddr_storage *pss)
689 char *tmpname = NULL;
691 char *file_contents = NULL;
692 char *kdc_ip_string = NULL;
696 char *realm_upper = NULL;
698 char *enctypes = NULL;
699 const char *include_system_krb5 = "";
702 if (!lp_create_krb5_conf()) {
707 DEBUG(0, ("No realm has been specified! Do you really want to "
708 "join an Active Directory server?\n"));
712 if (domain == NULL || pss == NULL) {
716 dname = lock_path(talloc_tos(), "smb_krb5");
720 if ((mkdir(dname, 0755)==-1) && (errno != EEXIST)) {
721 DEBUG(0,("create_local_private_krb5_conf_for_domain: "
722 "failed to create directory %s. Error was %s\n",
723 dname, strerror(errno) ));
727 tmpname = lock_path(talloc_tos(), "smb_tmp_krb5.XXXXXX");
732 fname = talloc_asprintf(dname, "%s/krb5.conf.%s", dname, domain);
737 DEBUG(10,("create_local_private_krb5_conf_for_domain: fname = %s, realm = %s, domain = %s\n",
738 fname, realm, domain ));
740 realm_upper = talloc_strdup(fname, realm);
741 if (!strupper_m(realm_upper)) {
745 kdc_ip_string = get_kdc_ip_string(dname, realm, sitename, pss);
746 if (!kdc_ip_string) {
750 enctypes = get_enctypes(fname);
751 if (enctypes == NULL) {
755 #if !defined(SAMBA4_USES_HEIMDAL)
756 if (lp_include_system_krb5_conf()) {
757 include_system_krb5 = "include /etc/krb5.conf";
762 * We are setting 'dns_lookup_kdc' to true, because we want to lookup
763 * KDCs which are not configured via DNS SRV records, eg. if we do:
765 * net ads join -Uadmin@otherdomain
768 talloc_asprintf(fname,
770 "\tdefault_realm = %s\n"
772 "\tdns_lookup_realm = false\n"
773 "\tdns_lookup_kdc = true\n\n"
774 "[realms]\n\t%s = {\n"
785 include_system_krb5);
787 if (!file_contents) {
791 flen = strlen(file_contents);
793 mask = umask(S_IRWXO | S_IRWXG);
794 fd = mkstemp(tmpname);
797 DEBUG(0,("create_local_private_krb5_conf_for_domain: smb_mkstemp failed,"
798 " for file %s. Errno %s\n",
799 tmpname, strerror(errno) ));
803 if (fchmod(fd, 0644)==-1) {
804 DEBUG(0,("create_local_private_krb5_conf_for_domain: fchmod failed for %s."
806 tmpname, strerror(errno) ));
812 ret = write(fd, file_contents, flen);
814 DEBUG(0,("create_local_private_krb5_conf_for_domain: write failed,"
815 " returned %d (should be %u). Errno %s\n",
816 (int)ret, (unsigned int)flen, strerror(errno) ));
822 DEBUG(0,("create_local_private_krb5_conf_for_domain: close failed."
823 " Errno %s\n", strerror(errno) ));
828 if (rename(tmpname, fname) == -1) {
829 DEBUG(0,("create_local_private_krb5_conf_for_domain: rename "
830 "of %s to %s failed. Errno %s\n",
831 tmpname, fname, strerror(errno) ));
836 DBG_INFO("wrote file %s with realm %s KDC list:\n%s\n",
837 fname, realm_upper, kdc_ip_string);
839 /* Set the environment variable to this file. */
840 setenv("KRB5_CONFIG", fname, 1);
844 #if defined(OVERWRITE_SYSTEM_KRB5_CONF)
846 #define SYSTEM_KRB5_CONF_PATH "/etc/krb5.conf"
847 /* Insanity, sheer insanity..... */
849 if (strequal(realm, lp_realm())) {
850 SMB_STRUCT_STAT sbuf;
852 if (sys_lstat(SYSTEM_KRB5_CONF_PATH, &sbuf, false) == 0) {
853 if (S_ISLNK(sbuf.st_ex_mode) && sbuf.st_ex_size) {
855 size_t alloc_size = sbuf.st_ex_size + 1;
856 char *linkpath = talloc_array(talloc_tos(), char,
861 lret = readlink(SYSTEM_KRB5_CONF_PATH, linkpath,
864 TALLOC_FREE(linkpath);
867 linkpath[lret] = '\0';
869 if (strcmp(linkpath, fname) == 0) {
870 /* Symlink already exists. */
871 TALLOC_FREE(linkpath);
874 TALLOC_FREE(linkpath);
878 /* Try and replace with a symlink. */
879 if (symlink(fname, SYSTEM_KRB5_CONF_PATH) == -1) {
880 const char *newpath = SYSTEM_KRB5_CONF_PATH ".saved";
881 if (errno != EEXIST) {
882 DEBUG(0,("create_local_private_krb5_conf_for_domain: symlink "
883 "of %s to %s failed. Errno %s\n",
884 fname, SYSTEM_KRB5_CONF_PATH, strerror(errno) ));
885 goto done; /* Not a fatal error. */
888 /* Yes, this is a race conditon... too bad. */
889 if (rename(SYSTEM_KRB5_CONF_PATH, newpath) == -1) {
890 DEBUG(0,("create_local_private_krb5_conf_for_domain: rename "
891 "of %s to %s failed. Errno %s\n",
892 SYSTEM_KRB5_CONF_PATH, newpath,
894 goto done; /* Not a fatal error. */
897 if (symlink(fname, SYSTEM_KRB5_CONF_PATH) == -1) {
898 DEBUG(0,("create_local_private_krb5_conf_for_domain: "
899 "forced symlink of %s to /etc/krb5.conf failed. Errno %s\n",
900 fname, strerror(errno) ));
901 goto done; /* Not a fatal error. */
908 TALLOC_FREE(tmpname);