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"
34 #include "krb5_errs.h"
38 #define LIBADS_CCACHE_NAME "MEMORY:libads"
41 we use a prompter to avoid a crash bug in the kerberos libs when
42 dealing with empty passwords
43 this prompter is just a string copy ...
45 static krb5_error_code
46 kerb_prompter(krb5_context ctx, void *data,
50 krb5_prompt prompts[])
52 if (num_prompts == 0) return 0;
53 if (num_prompts == 2) {
55 * only heimdal has a prompt type and we need to deal with it here to
58 * removing the prompter completely is not an option as at least these
59 * versions would crash: heimdal-1.0.2 and heimdal-1.1. Later heimdal
60 * version have looping detection and return with a proper error code.
63 #if defined(HAVE_KRB5_PROMPT_TYPE) /* Heimdal */
64 if (prompts[0].type == KRB5_PROMPT_TYPE_NEW_PASSWORD &&
65 prompts[1].type == KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN) {
67 * We don't want to change passwords here. We're
68 * called from heimal when the KDC returns
69 * KRB5KDC_ERR_KEY_EXPIRED, but at this point we don't
70 * have the chance to ask the user for a new
71 * password. If we return 0 (i.e. success), we will be
72 * spinning in the endless for-loop in
73 * change_password() in
74 * source4/heimdal/lib/krb5/init_creds_pw.c:526ff
76 return KRB5KDC_ERR_KEY_EXPIRED;
78 #elif defined(HAVE_KRB5_GET_PROMPT_TYPES) /* MIT */
79 krb5_prompt_type *prompt_types = NULL;
81 prompt_types = krb5_get_prompt_types(ctx);
82 if (prompt_types != NULL) {
83 if (prompt_types[0] == KRB5_PROMPT_TYPE_NEW_PASSWORD &&
84 prompt_types[1] == KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN) {
85 return KRB5KDC_ERR_KEY_EXP;
91 memset(prompts[0].reply->data, '\0', prompts[0].reply->length);
92 if (prompts[0].reply->length > 0) {
94 strncpy((char *)prompts[0].reply->data, (const char *)data,
95 prompts[0].reply->length-1);
96 prompts[0].reply->length = strlen((const char *)prompts[0].reply->data);
98 prompts[0].reply->length = 0;
105 simulate a kinit, putting the tgt in the given cache location. If cache_name == NULL
106 place in default cache location.
109 int kerberos_kinit_password_ext(const char *given_principal,
110 const char *password,
113 time_t *renew_till_time,
114 const char *cache_name,
116 bool add_netbios_addr,
117 time_t renewable_time,
119 char **_canon_principal,
123 TALLOC_CTX *frame = talloc_stackframe();
124 krb5_context ctx = NULL;
125 krb5_error_code code = 0;
126 krb5_ccache cc = NULL;
127 krb5_principal me = NULL;
128 krb5_principal canon_princ = NULL;
130 krb5_get_init_creds_opt *opt = NULL;
131 smb_krb5_addresses *addr = NULL;
132 char *canon_principal = NULL;
133 char *canon_realm = NULL;
135 ZERO_STRUCT(my_creds);
137 code = smb_krb5_init_context_common(&ctx);
139 DBG_ERR("kerberos init context failed (%s)\n",
140 error_message(code));
145 if (time_offset != 0) {
146 krb5_set_real_time(ctx, time(NULL) + time_offset, 0);
149 DBG_DEBUG("as %s using [%s] as ccache and config [%s]\n",
151 cache_name ? cache_name: krb5_cc_default_name(ctx),
152 getenv("KRB5_CONFIG"));
154 if ((code = krb5_cc_resolve(ctx, cache_name ? cache_name : krb5_cc_default_name(ctx), &cc))) {
158 if ((code = smb_krb5_parse_name(ctx, given_principal, &me))) {
162 if ((code = krb5_get_init_creds_opt_alloc(ctx, &opt))) {
166 krb5_get_init_creds_opt_set_renew_life(opt, renewable_time);
167 krb5_get_init_creds_opt_set_forwardable(opt, True);
169 /* Turn on canonicalization for lower case realm support */
170 #ifdef SAMBA4_USES_HEIMDAL
171 krb5_get_init_creds_opt_set_win2k(ctx, opt, true);
172 krb5_get_init_creds_opt_set_canonicalize(ctx, opt, true);
174 krb5_get_init_creds_opt_set_canonicalize(opt, true);
178 krb5_get_init_creds_opt_set_tkt_life(opt, 60);
181 #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_SET_PAC_REQUEST
183 if ((code = krb5_get_init_creds_opt_set_pac_request(ctx, opt, (krb5_boolean)request_pac))) {
188 if (add_netbios_addr) {
189 if ((code = smb_krb5_gen_netbios_krb5_address(&addr,
190 lp_netbios_name()))) {
193 krb5_get_init_creds_opt_set_address_list(opt, addr->addrs);
196 if ((code = krb5_get_init_creds_password(ctx, &my_creds, me, discard_const_p(char,password),
197 kerb_prompter, discard_const_p(char, password),
202 canon_princ = my_creds.client;
204 code = smb_krb5_unparse_name(frame,
212 DBG_DEBUG("%s mapped to %s\n", given_principal, canon_principal);
214 canon_realm = smb_krb5_principal_get_realm(frame, ctx, canon_princ);
215 if (canon_realm == NULL) {
220 if ((code = krb5_cc_initialize(ctx, cc, canon_princ))) {
224 if ((code = krb5_cc_store_cred(ctx, cc, &my_creds))) {
229 *expire_time = (time_t) my_creds.times.endtime;
232 if (renew_till_time) {
233 *renew_till_time = (time_t) my_creds.times.renew_till;
236 if (_canon_principal != NULL) {
237 *_canon_principal = talloc_move(mem_ctx, &canon_principal);
239 if (_canon_realm != NULL) {
240 *_canon_realm = talloc_move(mem_ctx, &canon_realm);
246 *ntstatus = NT_STATUS_OK;
250 /* fall back to self-made-mapping */
251 *ntstatus = krb5_to_nt_status(code);
255 krb5_free_cred_contents(ctx, &my_creds);
257 krb5_free_principal(ctx, me);
260 smb_krb5_free_addresses(ctx, addr);
263 krb5_get_init_creds_opt_free(ctx, opt);
266 krb5_cc_close(ctx, cc);
269 krb5_free_context(ctx);
275 int ads_kdestroy(const char *cc_name)
277 krb5_error_code code;
278 krb5_context ctx = NULL;
279 krb5_ccache cc = NULL;
281 code = smb_krb5_init_context_common(&ctx);
283 DBG_ERR("kerberos init context failed (%s)\n",
284 error_message(code));
289 if ((code = krb5_cc_default(ctx, &cc))) {
290 krb5_free_context(ctx);
294 if ((code = krb5_cc_resolve(ctx, cc_name, &cc))) {
295 DEBUG(3, ("ads_kdestroy: krb5_cc_resolve failed: %s\n",
296 error_message(code)));
297 krb5_free_context(ctx);
302 if ((code = krb5_cc_destroy (ctx, cc))) {
303 DEBUG(3, ("ads_kdestroy: krb5_cc_destroy failed: %s\n",
304 error_message(code)));
307 krb5_free_context (ctx);
311 int create_kerberos_key_from_string(krb5_context context,
312 krb5_principal host_princ,
313 krb5_principal salt_princ,
316 krb5_enctype enctype,
321 * Check if we've determined that the KDC is salting keys for this
322 * principal/enctype in a non-obvious way. If it is, try to match
326 KRB5_KEY_DATA(key) = (KRB5_KEY_DATA_CAST *)SMB_MALLOC(password->length);
327 if (!KRB5_KEY_DATA(key)) {
330 memcpy(KRB5_KEY_DATA(key), password->data, password->length);
331 KRB5_KEY_LENGTH(key) = password->length;
332 KRB5_KEY_TYPE(key) = enctype;
335 ret = smb_krb5_create_key_from_string(context,
336 salt_princ ? salt_princ : host_princ,
344 /************************************************************************
345 ************************************************************************/
347 int kerberos_kinit_password(const char *principal,
348 const char *password,
350 const char *cache_name)
352 return kerberos_kinit_password_ext(principal,
367 /************************************************************************
368 ************************************************************************/
370 /************************************************************************
371 Create a string list of available kdc's, possibly searching by sitename.
374 If "sitename" is given, the DC's in that site are listed first.
376 ************************************************************************/
378 static void add_sockaddr_unique(struct sockaddr_storage *addrs, size_t *num_addrs,
379 const struct sockaddr_storage *addr)
383 for (i=0; i<*num_addrs; i++) {
384 if (sockaddr_equal((const struct sockaddr *)&addrs[i],
385 (const struct sockaddr *)addr)) {
393 /* print_canonical_sockaddr prints an ipv6 addr in the form of
394 * [ipv6.addr]. This string, when put in a generated krb5.conf file is not
395 * always properly dealt with by some older krb5 libraries. Adding the hard-coded
396 * portnumber workarounds the issue. - gd */
398 static char *print_canonical_sockaddr_with_port(TALLOC_CTX *mem_ctx,
399 const struct sockaddr_storage *pss)
403 str = print_canonical_sockaddr(mem_ctx, pss);
408 if (pss->ss_family != AF_INET6) {
412 #if defined(HAVE_IPV6)
413 str = talloc_asprintf_append(str, ":88");
418 static char *get_kdc_ip_string(char *mem_ctx,
420 const char *sitename,
421 const struct sockaddr_storage *pss)
423 TALLOC_CTX *frame = talloc_stackframe();
425 struct ip_service *ip_srv_site = NULL;
426 struct ip_service *ip_srv_nonsite = NULL;
430 struct sockaddr_storage *dc_addrs;
431 struct tsocket_address **dc_addrs2 = NULL;
432 const struct tsocket_address * const *dc_addrs3 = NULL;
434 struct netlogon_samlogon_response **responses = NULL;
436 char *kdc_str = talloc_asprintf(mem_ctx, "%s\t\tkdc = %s\n", "",
437 print_canonical_sockaddr_with_port(mem_ctx, pss));
439 if (kdc_str == NULL) {
445 * First get the KDC's only in this site, the rest will be
450 get_kdc_list(realm, sitename, &ip_srv_site, &count_site);
451 DEBUG(10, ("got %d addresses from site %s search\n", count_site,
457 get_kdc_list(realm, NULL, &ip_srv_nonsite, &count_nonsite);
458 DEBUG(10, ("got %d addresses from site-less search\n", count_nonsite));
460 dc_addrs = talloc_array(talloc_tos(), struct sockaddr_storage,
461 count_site + count_nonsite);
462 if (dc_addrs == NULL) {
468 for (i = 0; i < count_site; i++) {
470 (const struct sockaddr *)pss,
471 (const struct sockaddr *)&ip_srv_site[i].ss)) {
472 add_sockaddr_unique(dc_addrs, &num_dcs,
477 for (i = 0; i < count_nonsite; i++) {
479 (const struct sockaddr *)pss,
480 (const struct sockaddr *)&ip_srv_nonsite[i].ss)) {
481 add_sockaddr_unique(dc_addrs, &num_dcs,
482 &ip_srv_nonsite[i].ss);
486 dc_addrs2 = talloc_zero_array(talloc_tos(),
487 struct tsocket_address *,
490 DBG_DEBUG("%zu additional KDCs to test\n", num_dcs);
494 if (dc_addrs2 == NULL) {
498 for (i=0; i<num_dcs; i++) {
499 char addr[INET6_ADDRSTRLEN];
502 print_sockaddr(addr, sizeof(addr), &dc_addrs[i]);
504 ret = tsocket_address_inet_from_strings(dc_addrs2, "ip",
508 status = map_nt_error_from_unix(errno);
509 DEBUG(2,("Failed to create tsocket_address for %s - %s\n",
510 addr, nt_errstr(status)));
515 dc_addrs3 = (const struct tsocket_address * const *)dc_addrs2;
517 status = cldap_multi_netlogon(talloc_tos(),
519 realm, lp_netbios_name(),
520 NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX,
521 MIN(num_dcs, 3), timeval_current_ofs(3, 0), &responses);
522 TALLOC_FREE(dc_addrs2);
525 if (!NT_STATUS_IS_OK(status)) {
526 DEBUG(10,("get_kdc_ip_string: cldap_multi_netlogon failed: "
527 "%s\n", nt_errstr(status)));
531 for (i=0; i<num_dcs; i++) {
534 if (responses[i] == NULL) {
538 /* Append to the string - inefficient but not done often. */
539 new_kdc_str = talloc_asprintf(mem_ctx, "%s\t\tkdc = %s\n",
541 print_canonical_sockaddr_with_port(mem_ctx, &dc_addrs[i]));
542 if (new_kdc_str == NULL) {
545 TALLOC_FREE(kdc_str);
546 kdc_str = new_kdc_str;
550 DEBUG(10, ("get_kdc_ip_string: Returning %s\n", kdc_str));
553 SAFE_FREE(ip_srv_site);
554 SAFE_FREE(ip_srv_nonsite);
559 /************************************************************************
560 Create a specific krb5.conf file in the private directory pointing
561 at a specific kdc for a realm. Keyed off domain name. Sets
562 KRB5_CONFIG environment variable to point to this file. Must be
563 run as root or will fail (which is a good thing :-).
564 ************************************************************************/
566 #if !defined(SAMBA4_USES_HEIMDAL) /* MIT version */
567 static char *get_enctypes(TALLOC_CTX *mem_ctx)
569 char *aes_enctypes = NULL;
570 const char *legacy_enctypes = "";
571 char *enctypes = NULL;
573 aes_enctypes = talloc_strdup(mem_ctx, "");
574 if (aes_enctypes == NULL) {
578 if (lp_kerberos_encryption_types() == KERBEROS_ETYPES_ALL ||
579 lp_kerberos_encryption_types() == KERBEROS_ETYPES_STRONG) {
580 #ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
581 aes_enctypes = talloc_asprintf_append(
582 aes_enctypes, "%s", "aes256-cts-hmac-sha1-96 ");
583 if (aes_enctypes == NULL) {
587 #ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
588 aes_enctypes = talloc_asprintf_append(
589 aes_enctypes, "%s", "aes128-cts-hmac-sha1-96");
590 if (aes_enctypes == NULL) {
596 if (lp_kerberos_encryption_types() == KERBEROS_ETYPES_ALL ||
597 lp_kerberos_encryption_types() == KERBEROS_ETYPES_LEGACY) {
598 legacy_enctypes = "RC4-HMAC DES-CBC-CRC DES-CBC-MD5";
602 talloc_asprintf(mem_ctx, "\tdefault_tgs_enctypes = %s %s\n"
603 "\tdefault_tkt_enctypes = %s %s\n"
604 "\tpreferred_enctypes = %s %s\n",
605 aes_enctypes, legacy_enctypes, aes_enctypes,
606 legacy_enctypes, aes_enctypes, legacy_enctypes);
608 TALLOC_FREE(aes_enctypes);
611 #else /* Heimdal version */
612 static char *get_enctypes(TALLOC_CTX *mem_ctx)
614 const char *aes_enctypes = "";
615 const char *legacy_enctypes = "";
616 char *enctypes = NULL;
618 if (lp_kerberos_encryption_types() == KERBEROS_ETYPES_ALL ||
619 lp_kerberos_encryption_types() == KERBEROS_ETYPES_STRONG) {
621 "aes256-cts-hmac-sha1-96 aes128-cts-hmac-sha1-96";
624 if (lp_kerberos_encryption_types() == KERBEROS_ETYPES_ALL ||
625 lp_kerberos_encryption_types() == KERBEROS_ETYPES_LEGACY) {
626 legacy_enctypes = "arcfour-hmac-md5 des-cbc-crc des-cbc-md5";
629 enctypes = talloc_asprintf(mem_ctx, "\tdefault_etypes = %s %s\n",
630 aes_enctypes, legacy_enctypes);
636 bool create_local_private_krb5_conf_for_domain(const char *realm,
638 const char *sitename,
639 const struct sockaddr_storage *pss)
642 char *tmpname = NULL;
644 char *file_contents = NULL;
645 char *kdc_ip_string = NULL;
649 char *realm_upper = NULL;
651 char *enctypes = NULL;
652 const char *include_system_krb5 = "";
655 if (!lp_create_krb5_conf()) {
660 DEBUG(0, ("No realm has been specified! Do you really want to "
661 "join an Active Directory server?\n"));
665 if (domain == NULL || pss == NULL) {
669 dname = lock_path(talloc_tos(), "smb_krb5");
673 if ((mkdir(dname, 0755)==-1) && (errno != EEXIST)) {
674 DEBUG(0,("create_local_private_krb5_conf_for_domain: "
675 "failed to create directory %s. Error was %s\n",
676 dname, strerror(errno) ));
680 tmpname = lock_path(talloc_tos(), "smb_tmp_krb5.XXXXXX");
685 fname = talloc_asprintf(dname, "%s/krb5.conf.%s", dname, domain);
690 DEBUG(10,("create_local_private_krb5_conf_for_domain: fname = %s, realm = %s, domain = %s\n",
691 fname, realm, domain ));
693 realm_upper = talloc_strdup(fname, realm);
694 if (!strupper_m(realm_upper)) {
698 kdc_ip_string = get_kdc_ip_string(dname, realm, sitename, pss);
699 if (!kdc_ip_string) {
703 enctypes = get_enctypes(fname);
704 if (enctypes == NULL) {
708 #if !defined(SAMBA4_USES_HEIMDAL)
709 if (lp_include_system_krb5_conf()) {
710 include_system_krb5 = "include /etc/krb5.conf";
715 * We are setting 'dns_lookup_kdc' to true, because we want to lookup
716 * KDCs which are not configured via DNS SRV records, eg. if we do:
718 * net ads join -Uadmin@otherdomain
721 talloc_asprintf(fname,
723 "\tdefault_realm = %s\n"
725 "\tdns_lookup_realm = false\n"
726 "\tdns_lookup_kdc = true\n\n"
727 "[realms]\n\t%s = {\n"
734 include_system_krb5);
736 if (!file_contents) {
740 flen = strlen(file_contents);
742 mask = umask(S_IRWXO | S_IRWXG);
743 fd = mkstemp(tmpname);
746 DEBUG(0,("create_local_private_krb5_conf_for_domain: smb_mkstemp failed,"
747 " for file %s. Errno %s\n",
748 tmpname, strerror(errno) ));
752 if (fchmod(fd, 0644)==-1) {
753 DEBUG(0,("create_local_private_krb5_conf_for_domain: fchmod failed for %s."
755 tmpname, strerror(errno) ));
761 ret = write(fd, file_contents, flen);
763 DEBUG(0,("create_local_private_krb5_conf_for_domain: write failed,"
764 " returned %d (should be %u). Errno %s\n",
765 (int)ret, (unsigned int)flen, strerror(errno) ));
771 DEBUG(0,("create_local_private_krb5_conf_for_domain: close failed."
772 " Errno %s\n", strerror(errno) ));
777 if (rename(tmpname, fname) == -1) {
778 DEBUG(0,("create_local_private_krb5_conf_for_domain: rename "
779 "of %s to %s failed. Errno %s\n",
780 tmpname, fname, strerror(errno) ));
785 DEBUG(5,("create_local_private_krb5_conf_for_domain: wrote "
786 "file %s with realm %s KDC list = %s\n",
787 fname, realm_upper, kdc_ip_string));
789 /* Set the environment variable to this file. */
790 setenv("KRB5_CONFIG", fname, 1);
794 #if defined(OVERWRITE_SYSTEM_KRB5_CONF)
796 #define SYSTEM_KRB5_CONF_PATH "/etc/krb5.conf"
797 /* Insanity, sheer insanity..... */
799 if (strequal(realm, lp_realm())) {
800 SMB_STRUCT_STAT sbuf;
802 if (sys_lstat(SYSTEM_KRB5_CONF_PATH, &sbuf, false) == 0) {
803 if (S_ISLNK(sbuf.st_ex_mode) && sbuf.st_ex_size) {
805 size_t alloc_size = sbuf.st_ex_size + 1;
806 char *linkpath = talloc_array(talloc_tos(), char,
811 lret = readlink(SYSTEM_KRB5_CONF_PATH, linkpath,
814 TALLOC_FREE(linkpath);
817 linkpath[lret] = '\0';
819 if (strcmp(linkpath, fname) == 0) {
820 /* Symlink already exists. */
821 TALLOC_FREE(linkpath);
824 TALLOC_FREE(linkpath);
828 /* Try and replace with a symlink. */
829 if (symlink(fname, SYSTEM_KRB5_CONF_PATH) == -1) {
830 const char *newpath = SYSTEM_KRB5_CONF_PATH ".saved";
831 if (errno != EEXIST) {
832 DEBUG(0,("create_local_private_krb5_conf_for_domain: symlink "
833 "of %s to %s failed. Errno %s\n",
834 fname, SYSTEM_KRB5_CONF_PATH, strerror(errno) ));
835 goto done; /* Not a fatal error. */
838 /* Yes, this is a race conditon... too bad. */
839 if (rename(SYSTEM_KRB5_CONF_PATH, newpath) == -1) {
840 DEBUG(0,("create_local_private_krb5_conf_for_domain: rename "
841 "of %s to %s failed. Errno %s\n",
842 SYSTEM_KRB5_CONF_PATH, newpath,
844 goto done; /* Not a fatal error. */
847 if (symlink(fname, SYSTEM_KRB5_CONF_PATH) == -1) {
848 DEBUG(0,("create_local_private_krb5_conf_for_domain: "
849 "forced symlink of %s to /etc/krb5.conf failed. Errno %s\n",
850 fname, strerror(errno) ));
851 goto done; /* Not a fatal error. */
858 TALLOC_FREE(tmpname);