more enctypes...
[metze/samba/wip.git] / source3 / libads / kerberos.c
index 649e5681a493f7c4a5fd4ce19ee02149163b4666..bb6b91ee95e4066e14d44ef1cd8e2435e1df5460 100644 (file)
@@ -22,6 +22,7 @@
 */
 
 #include "includes.h"
+#include "libsmb/namequery.h"
 #include "system/filesys.h"
 #include "smb_krb5.h"
 #include "../librpc/gen_ndr/ndr_misc.h"
 #include "libads/cldap.h"
 #include "secrets.h"
 #include "../lib/tsocket/tsocket.h"
+#include "lib/util/asn1.h"
+#include "krb5_errs.h"
 
 #ifdef HAVE_KRB5
 
-#define DEFAULT_KRB5_PORT 88
-
 #define LIBADS_CCACHE_NAME "MEMORY:libads"
 
 /*
@@ -49,6 +50,43 @@ kerb_prompter(krb5_context ctx, void *data,
               krb5_prompt prompts[])
 {
        if (num_prompts == 0) return 0;
+       if (num_prompts == 2) {
+               /*
+                * only heimdal has a prompt type and we need to deal with it here to
+                * avoid loops.
+                *
+                * removing the prompter completely is not an option as at least these
+                * versions would crash: heimdal-1.0.2 and heimdal-1.1. Later heimdal
+                * version have looping detection and return with a proper error code.
+                */
+
+#if defined(HAVE_KRB5_PROMPT_TYPE) /* Heimdal */
+                if (prompts[0].type == KRB5_PROMPT_TYPE_NEW_PASSWORD &&
+                    prompts[1].type == KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN) {
+                       /*
+                        * We don't want to change passwords here. We're
+                        * called from heimal when the KDC returns
+                        * KRB5KDC_ERR_KEY_EXPIRED, but at this point we don't
+                        * have the chance to ask the user for a new
+                        * password. If we return 0 (i.e. success), we will be
+                        * spinning in the endless for-loop in
+                        * change_password() in
+                        * source4/heimdal/lib/krb5/init_creds_pw.c:526ff
+                        */
+                       return KRB5KDC_ERR_KEY_EXPIRED;
+               }
+#elif defined(HAVE_KRB5_GET_PROMPT_TYPES) /* MIT */
+               krb5_prompt_type *prompt_types = NULL;
+
+               prompt_types = krb5_get_prompt_types(ctx);
+               if (prompt_types != NULL) {
+                       if (prompt_types[0] == KRB5_PROMPT_TYPE_NEW_PASSWORD &&
+                           prompt_types[1] == KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN) {
+                               return KRB5KDC_ERR_KEY_EXP;
+                       }
+               }
+#endif
+       }
 
        memset(prompts[0].reply->data, '\0', prompts[0].reply->length);
        if (prompts[0].reply->length > 0) {
@@ -63,96 +101,6 @@ kerb_prompter(krb5_context ctx, void *data,
        return 0;
 }
 
- static bool smb_krb5_get_ntstatus_from_krb5_error(krb5_error *error,
-                                                  NTSTATUS *nt_status)
-{
-       DATA_BLOB edata;
-       DATA_BLOB unwrapped_edata;
-       TALLOC_CTX *mem_ctx;
-       struct KRB5_EDATA_NTSTATUS parsed_edata;
-       enum ndr_err_code ndr_err;
-
-#ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR
-       edata = data_blob(error->e_data->data, error->e_data->length);
-#else
-       edata = data_blob(error->e_data.data, error->e_data.length);
-#endif /* HAVE_E_DATA_POINTER_IN_KRB5_ERROR */
-
-#ifdef DEVELOPER
-       dump_data(10, edata.data, edata.length);
-#endif /* DEVELOPER */
-
-       mem_ctx = talloc_init("smb_krb5_get_ntstatus_from_krb5_error");
-       if (mem_ctx == NULL) {
-               data_blob_free(&edata);
-               return False;
-       }
-
-       if (!unwrap_edata_ntstatus(mem_ctx, &edata, &unwrapped_edata)) {
-               data_blob_free(&edata);
-               TALLOC_FREE(mem_ctx);
-               return False;
-       }
-
-       data_blob_free(&edata);
-
-       ndr_err = ndr_pull_struct_blob_all(&unwrapped_edata, mem_ctx, 
-               &parsed_edata, (ndr_pull_flags_fn_t)ndr_pull_KRB5_EDATA_NTSTATUS);
-       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
-               data_blob_free(&unwrapped_edata);
-               TALLOC_FREE(mem_ctx);
-               return False;
-       }
-
-       data_blob_free(&unwrapped_edata);
-
-       if (nt_status) {
-               *nt_status = parsed_edata.ntstatus;
-       }
-
-       TALLOC_FREE(mem_ctx);
-
-       return True;
-}
-
- static bool smb_krb5_get_ntstatus_from_krb5_error_init_creds_opt(krb5_context ctx, 
-                                                                 krb5_get_init_creds_opt *opt, 
-                                                                 NTSTATUS *nt_status)
-{
-       bool ret = False;
-       krb5_error *error = NULL;
-
-#ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_GET_ERROR
-       ret = krb5_get_init_creds_opt_get_error(ctx, opt, &error);
-       if (ret) {
-               DEBUG(1,("krb5_get_init_creds_opt_get_error gave: %s\n", 
-                       error_message(ret)));
-               return False;
-       }
-#endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_GET_ERROR */
-
-       if (!error) {
-               DEBUG(1,("no krb5_error\n"));
-               return False;
-       }
-
-#ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR
-       if (!error->e_data) {
-#else
-       if (error->e_data.data == NULL) {
-#endif /* HAVE_E_DATA_POINTER_IN_KRB5_ERROR */
-               DEBUG(1,("no edata in krb5_error\n")); 
-               krb5_free_error(ctx, error);
-               return False;
-       }
-
-       ret = smb_krb5_get_ntstatus_from_krb5_error(error, nt_status);
-
-       krb5_free_error(ctx, error);
-
-       return ret;
-}
-
 /*
   simulate a kinit, putting the tgt in the given cache location. If cache_name == NULL
   place in default cache location.
@@ -173,15 +121,19 @@ int kerberos_kinit_password_ext(const char *principal,
        krb5_error_code code = 0;
        krb5_ccache cc = NULL;
        krb5_principal me = NULL;
+       krb5_principal canon_princ = NULL;
        krb5_creds my_creds;
        krb5_get_init_creds_opt *opt = NULL;
        smb_krb5_addresses *addr = NULL;
 
        ZERO_STRUCT(my_creds);
 
-       initialize_krb5_error_table();
-       if ((code = krb5_init_context(&ctx)))
-               goto out;
+       code = smb_krb5_init_context_common(&ctx);
+       if (code != 0) {
+               DBG_ERR("kerberos init context failed (%s)\n",
+                       error_message(code));
+               return code;
+       }
 
        if (time_offset != 0) {
                krb5_set_real_time(ctx, time(NULL) + time_offset, 0);
@@ -200,12 +152,17 @@ int kerberos_kinit_password_ext(const char *principal,
                goto out;
        }
 
-       if ((code = smb_krb5_get_init_creds_opt_alloc(ctx, &opt))) {
+       if ((code = krb5_get_init_creds_opt_alloc(ctx, &opt))) {
                goto out;
        }
 
        krb5_get_init_creds_opt_set_renew_life(opt, renewable_time);
        krb5_get_init_creds_opt_set_forwardable(opt, True);
+
+       /* Turn on canonicalization for lower case realm support */
+#ifndef SAMBA4_USES_HEIMDAL /* MIT */
+       krb5_get_init_creds_opt_set_canonicalize(opt, true);
+#endif /* MIT */
 #if 0
        /* insane testing */
        krb5_get_init_creds_opt_set_tkt_life(opt, 60);
@@ -232,7 +189,12 @@ int kerberos_kinit_password_ext(const char *principal,
                goto out;
        }
 
-       if ((code = krb5_cc_initialize(ctx, cc, me))) {
+       canon_princ = me;
+#ifndef SAMBA4_USES_HEIMDAL /* MIT */
+       canon_princ = my_creds.client;
+#endif /* MIT */
+
+       if ((code = krb5_cc_initialize(ctx, cc, canon_princ))) {
                goto out;
        }
 
@@ -249,23 +211,12 @@ int kerberos_kinit_password_ext(const char *principal,
        }
  out:
        if (ntstatus) {
-
-               NTSTATUS status;
-
                /* fast path */
                if (code == 0) {
                        *ntstatus = NT_STATUS_OK;
                        goto cleanup;
                }
 
-               /* try to get ntstatus code out of krb5_error when we have it
-                * inside the krb5_get_init_creds_opt - gd */
-
-               if (opt && smb_krb5_get_ntstatus_from_krb5_error_init_creds_opt(ctx, opt, &status)) {
-                       *ntstatus = status;
-                       goto cleanup;
-               }
-
                /* fall back to self-made-mapping */
                *ntstatus = krb5_to_nt_status(code);
        }
@@ -278,8 +229,8 @@ int kerberos_kinit_password_ext(const char *principal,
        if (addr) {
                smb_krb5_free_addresses(ctx, addr);
        }
-       if (opt) {
-               smb_krb5_get_init_creds_opt_free(ctx, opt);
+       if (opt) {
+               krb5_get_init_creds_opt_free(ctx, opt);
        }
        if (cc) {
                krb5_cc_close(ctx, cc);
@@ -296,10 +247,10 @@ int ads_kdestroy(const char *cc_name)
        krb5_context ctx = NULL;
        krb5_ccache cc = NULL;
 
-       initialize_krb5_error_table();
-       if ((code = krb5_init_context (&ctx))) {
-               DEBUG(3, ("ads_kdestroy: kdb5_init_context failed: %s\n", 
-                       error_message(code)));
+       code = smb_krb5_init_context_common(&ctx);
+       if (code != 0) {
+               DBG_ERR("kerberos init context failed (%s)\n",
+                       error_message(code));
                return code;
        }
 
@@ -326,153 +277,14 @@ int ads_kdestroy(const char *cc_name)
        return code;
 }
 
-/************************************************************************
- Routine to fetch the salting principal for a service.  Active
- Directory may use a non-obvious principal name to generate the salt
- when it determines the key to use for encrypting tickets for a service,
- and hopefully we detected that when we joined the domain.
- ************************************************************************/
-
-static char *kerberos_secrets_fetch_salting_principal(const char *service, int enctype)
-{
-       char *key = NULL;
-       char *ret = NULL;
-
-       if (asprintf(&key, "%s/%s/enctype=%d",
-                    SECRETS_SALTING_PRINCIPAL, service, enctype) == -1) {
-               return NULL;
-       }
-       ret = (char *)secrets_fetch(key, NULL);
-       SAFE_FREE(key);
-       return ret;
-}
-
-/************************************************************************
- Return the standard DES salt key
-************************************************************************/
-
-char* kerberos_standard_des_salt( void )
-{
-       fstring salt;
-
-       fstr_sprintf( salt, "host/%s.%s@", lp_netbios_name(), lp_realm() );
-       (void)strlower_m( salt );
-       fstrcat( salt, lp_realm() );
-
-       return SMB_STRDUP( salt );
-}
-
-/************************************************************************
-************************************************************************/
-
-static char* des_salt_key( void )
-{
-       char *key;
-
-       if (asprintf(&key, "%s/DES/%s", SECRETS_SALTING_PRINCIPAL,
-                    lp_realm()) == -1) {
-               return NULL;
-       }
-
-       return key;
-}
-
-/************************************************************************
-************************************************************************/
-
-bool kerberos_secrets_store_des_salt( const char* salt )
-{
-       char* key;
-       bool ret;
-
-       if ( (key = des_salt_key()) == NULL ) {
-               DEBUG(0,("kerberos_secrets_store_des_salt: failed to generate key!\n"));
-               return False;
-       }
-
-       if ( !salt ) {
-               DEBUG(8,("kerberos_secrets_store_des_salt: deleting salt\n"));
-               secrets_delete( key );
-               return True;
-       }
-
-       DEBUG(3,("kerberos_secrets_store_des_salt: Storing salt \"%s\"\n", salt));
-
-       ret = secrets_store( key, salt, strlen(salt)+1 );
-
-       SAFE_FREE( key );
-
-       return ret;
-}
-
-/************************************************************************
-************************************************************************/
-
-static
-char* kerberos_secrets_fetch_des_salt( void )
-{
-       char *salt, *key;
-
-       if ( (key = des_salt_key()) == NULL ) {
-               DEBUG(0,("kerberos_secrets_fetch_des_salt: failed to generate key!\n"));
-               return NULL;
-       }
-
-       salt = (char*)secrets_fetch( key, NULL );
-
-       SAFE_FREE( key );
-
-       return salt;
-}
-
-/************************************************************************
- Routine to get the salting principal for this service.  This is 
- maintained for backwards compatibilty with releases prior to 3.0.24.
- Since we store the salting principal string only at join, we may have 
- to look for the older tdb keys.  Caller must free if return is not null.
- ************************************************************************/
-
-static
-krb5_principal kerberos_fetch_salt_princ_for_host_princ(krb5_context context,
-                                                       krb5_principal host_princ,
-                                                       int enctype)
-{
-       char *unparsed_name = NULL, *salt_princ_s = NULL;
-       krb5_principal ret_princ = NULL;
-
-       /* lookup new key first */
-
-       if ( (salt_princ_s = kerberos_secrets_fetch_des_salt()) == NULL ) {
-
-               /* look under the old key.  If this fails, just use the standard key */
-
-               if (smb_krb5_unparse_name(talloc_tos(), context, host_princ, &unparsed_name) != 0) {
-                       return (krb5_principal)NULL;
-               }
-               if ((salt_princ_s = kerberos_secrets_fetch_salting_principal(unparsed_name, enctype)) == NULL) {
-                       /* fall back to host/machine.realm@REALM */
-                       salt_princ_s = kerberos_standard_des_salt();
-               }
-       }
-
-       if (smb_krb5_parse_name(context, salt_princ_s, &ret_princ) != 0) {
-               ret_princ = NULL;
-       }
-
-       TALLOC_FREE(unparsed_name);
-       SAFE_FREE(salt_princ_s);
-
-       return ret_princ;
-}
-
 int create_kerberos_key_from_string(krb5_context context,
                                        krb5_principal host_princ,
+                                       krb5_principal salt_princ,
                                        krb5_data *password,
                                        krb5_keyblock *key,
                                        krb5_enctype enctype,
                                        bool no_salt)
 {
-       krb5_principal salt_princ = NULL;
        int ret;
        /*
         * Check if we've determined that the KDC is salting keys for this
@@ -489,86 +301,15 @@ int create_kerberos_key_from_string(krb5_context context,
                KRB5_KEY_TYPE(key) = enctype;
                return 0;
        }
-       salt_princ = kerberos_fetch_salt_princ_for_host_princ(context, host_princ, enctype);
-       ret = create_kerberos_key_from_string_direct(context, salt_princ ? salt_princ : host_princ, password, key, enctype);
-       if (salt_princ) {
-               krb5_free_principal(context, salt_princ);
-       }
+       ret = smb_krb5_create_key_from_string(context,
+                                             salt_princ ? salt_princ : host_princ,
+                                             NULL,
+                                             password,
+                                             enctype,
+                                             key);
        return ret;
 }
 
-/************************************************************************
- Routine to set the salting principal for this service.  Active
- Directory may use a non-obvious principal name to generate the salt
- when it determines the key to use for encrypting tickets for a service,
- and hopefully we detected that when we joined the domain.
- Setting principal to NULL deletes this entry.
- ************************************************************************/
-
-bool kerberos_secrets_store_salting_principal(const char *service,
-                                             int enctype,
-                                             const char *principal)
-{
-       char *key = NULL;
-       bool ret = False;
-       krb5_context context = NULL;
-       krb5_principal princ = NULL;
-       char *princ_s = NULL;
-       char *unparsed_name = NULL;
-       krb5_error_code code;
-
-       if (((code = krb5_init_context(&context)) != 0) || (context == NULL)) {
-               DEBUG(5, ("kerberos_secrets_store_salting_pricipal: kdb5_init_context failed: %s\n",
-                         error_message(code)));
-               return False;
-       }
-       if (strchr_m(service, '@')) {
-               if (asprintf(&princ_s, "%s", service) == -1) {
-                       goto out;
-               }
-       } else {
-               if (asprintf(&princ_s, "%s@%s", service, lp_realm()) == -1) {
-                       goto out;
-               }
-       }
-
-       if (smb_krb5_parse_name(context, princ_s, &princ) != 0) {
-               goto out;
-       }
-       if (smb_krb5_unparse_name(talloc_tos(), context, princ, &unparsed_name) != 0) {
-               goto out;
-       }
-
-       if (asprintf(&key, "%s/%s/enctype=%d",
-                    SECRETS_SALTING_PRINCIPAL, unparsed_name, enctype)
-           == -1) {
-               goto out;
-       }
-
-       if ((principal != NULL) && (strlen(principal) > 0)) {
-               ret = secrets_store(key, principal, strlen(principal) + 1);
-       } else {
-               ret = secrets_delete(key);
-       }
-
- out:
-
-       SAFE_FREE(key);
-       SAFE_FREE(princ_s);
-       TALLOC_FREE(unparsed_name);
-
-       if (princ) {
-               krb5_free_principal(context, princ);
-       }
-
-       if (context) {
-               krb5_free_context(context);
-       }
-
-       return ret;
-}
-
-
 /************************************************************************
 ************************************************************************/
 
@@ -600,10 +341,10 @@ int kerberos_kinit_password(const char *principal,
 
 ************************************************************************/
 
-static void add_sockaddr_unique(struct sockaddr_storage *addrs, int *num_addrs,
+static void add_sockaddr_unique(struct sockaddr_storage *addrs, size_t *num_addrs,
                                const struct sockaddr_storage *addr)
 {
-       int i;
+       size_t i;
 
        for (i=0; i<*num_addrs; i++) {
                if (sockaddr_equal((const struct sockaddr *)&addrs[i],
@@ -615,26 +356,51 @@ static void add_sockaddr_unique(struct sockaddr_storage *addrs, int *num_addrs,
        *num_addrs += 1;
 }
 
+/* print_canonical_sockaddr prints an ipv6 addr in the form of
+* [ipv6.addr]. This string, when put in a generated krb5.conf file is not
+* always properly dealt with by some older krb5 libraries. Adding the hard-coded
+* portnumber workarounds the issue. - gd */
+
+static char *print_canonical_sockaddr_with_port(TALLOC_CTX *mem_ctx,
+                                               const struct sockaddr_storage *pss)
+{
+       char *str = NULL;
+
+       str = print_canonical_sockaddr(mem_ctx, pss);
+       if (str == NULL) {
+               return NULL;
+       }
+
+       if (pss->ss_family != AF_INET6) {
+               return str;
+       }
+
+#if defined(HAVE_IPV6)
+       str = talloc_asprintf_append(str, ":88");
+#endif
+       return str;
+}
+
 static char *get_kdc_ip_string(char *mem_ctx,
                const char *realm,
                const char *sitename,
                const struct sockaddr_storage *pss)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       int i;
+       size_t i;
        struct ip_service *ip_srv_site = NULL;
        struct ip_service *ip_srv_nonsite = NULL;
        int count_site = 0;
        int count_nonsite;
-       int num_dcs;
+       size_t num_dcs;
        struct sockaddr_storage *dc_addrs;
        struct tsocket_address **dc_addrs2 = NULL;
        const struct tsocket_address * const *dc_addrs3 = NULL;
        char *result = NULL;
        struct netlogon_samlogon_response **responses = NULL;
        NTSTATUS status;
-       char *kdc_str = talloc_asprintf(mem_ctx, "%s\tkdc = %s\n", "",
-                                       print_canonical_sockaddr(mem_ctx, pss));
+       char *kdc_str = talloc_asprintf(mem_ctx, "%s\t\tkdc = %s\n", "",
+                                       print_canonical_sockaddr_with_port(mem_ctx, pss));
 
        if (kdc_str == NULL) {
                TALLOC_FREE(frame);
@@ -648,34 +414,51 @@ static char *get_kdc_ip_string(char *mem_ctx,
 
        if (sitename) {
                get_kdc_list(realm, sitename, &ip_srv_site, &count_site);
+               DEBUG(10, ("got %d addresses from site %s search\n", count_site,
+                          sitename));
        }
 
        /* Get all KDC's. */
 
        get_kdc_list(realm, NULL, &ip_srv_nonsite, &count_nonsite);
+       DEBUG(10, ("got %d addresses from site-less search\n", count_nonsite));
 
        dc_addrs = talloc_array(talloc_tos(), struct sockaddr_storage,
-                               1 + count_site + count_nonsite);
+                               count_site + count_nonsite);
        if (dc_addrs == NULL) {
-               goto fail;
+               goto out;
        }
 
-       dc_addrs[0] = *pss;
-       num_dcs = 1;
+       num_dcs = 0;
 
-       for (i=0; i<count_site; i++) {
-               add_sockaddr_unique(dc_addrs, &num_dcs, &ip_srv_site[i].ss);
+       for (i = 0; i < count_site; i++) {
+               if (!sockaddr_equal(
+                       (const struct sockaddr *)pss,
+                       (const struct sockaddr *)&ip_srv_site[i].ss)) {
+                       add_sockaddr_unique(dc_addrs, &num_dcs,
+                                           &ip_srv_site[i].ss);
+               }
        }
 
-       for (i=0; i<count_nonsite; i++) {
-               add_sockaddr_unique(dc_addrs, &num_dcs, &ip_srv_nonsite[i].ss);
+       for (i = 0; i < count_nonsite; i++) {
+               if (!sockaddr_equal(
+                       (const struct sockaddr *)pss,
+                       (const struct sockaddr *)&ip_srv_nonsite[i].ss)) {
+                       add_sockaddr_unique(dc_addrs, &num_dcs,
+                                           &ip_srv_nonsite[i].ss);
+               }
        }
 
        dc_addrs2 = talloc_zero_array(talloc_tos(),
                                      struct tsocket_address *,
                                      num_dcs);
+
+       DBG_DEBUG("%zu additional KDCs to test\n", num_dcs);
+       if (num_dcs == 0) {
+               goto out;
+       }
        if (dc_addrs2 == NULL) {
-               goto fail;
+               goto out;
        }
 
        for (i=0; i<num_dcs; i++) {
@@ -691,7 +474,7 @@ static char *get_kdc_ip_string(char *mem_ctx,
                        status = map_nt_error_from_unix(errno);
                        DEBUG(2,("Failed to create tsocket_address for %s - %s\n",
                                 addr, nt_errstr(status)));
-                       goto fail;
+                       goto out;
                }
        }
 
@@ -708,12 +491,7 @@ static char *get_kdc_ip_string(char *mem_ctx,
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(10,("get_kdc_ip_string: cldap_multi_netlogon failed: "
                          "%s\n", nt_errstr(status)));
-               goto fail;
-       }
-
-       kdc_str = talloc_strdup(mem_ctx, "");
-       if (kdc_str == NULL) {
-               goto fail;
+               goto out;
        }
 
        for (i=0; i<num_dcs; i++) {
@@ -724,21 +502,20 @@ static char *get_kdc_ip_string(char *mem_ctx,
                }
 
                /* Append to the string - inefficient but not done often. */
-               new_kdc_str = talloc_asprintf(mem_ctx, "%s\tkdc = %s\n",
+               new_kdc_str = talloc_asprintf(mem_ctx, "%s\t\tkdc = %s\n",
                                              kdc_str,
-                                             print_canonical_sockaddr(mem_ctx, &dc_addrs[i]));
+                                             print_canonical_sockaddr_with_port(mem_ctx, &dc_addrs[i]));
                if (new_kdc_str == NULL) {
-                       goto fail;
+                       goto out;
                }
                TALLOC_FREE(kdc_str);
                kdc_str = new_kdc_str;
        }
 
-       DEBUG(10,("get_kdc_ip_string: Returning %s\n",
-               kdc_str ));
+out:
+       DEBUG(10, ("get_kdc_ip_string: Returning %s\n", kdc_str));
 
        result = kdc_str;
-fail:
        SAFE_FREE(ip_srv_site);
        SAFE_FREE(ip_srv_nonsite);
        TALLOC_FREE(frame);
@@ -752,6 +529,98 @@ fail:
  run as root or will fail (which is a good thing :-).
 ************************************************************************/
 
+#if !defined(SAMBA4_USES_HEIMDAL) /* MIT version */
+static char *get_enctypes(TALLOC_CTX *mem_ctx)
+{
+       char *aes_enctypes = NULL;
+       const char *legacy_enctypes = "";
+       char *enctypes = NULL;
+
+       aes_enctypes = talloc_strdup(mem_ctx, "");
+       if (aes_enctypes == NULL) {
+               goto done;
+       }
+
+       if (lp_kerberos_encryption_types() == KERBEROS_ETYPES_ALL ||
+           lp_kerberos_encryption_types() == KERBEROS_ETYPES_STRONG) {
+#ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
+               aes_enctypes = talloc_asprintf_append(
+                   aes_enctypes, "%s", "aes256-cts-hmac-sha1-96 ");
+               if (aes_enctypes == NULL) {
+                       goto done;
+               }
+#endif
+#ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
+               aes_enctypes = talloc_asprintf_append(
+                   aes_enctypes, "%s", "aes128-cts-hmac-sha1-96");
+               if (aes_enctypes == NULL) {
+                       goto done;
+               }
+#endif
+       }
+
+       if (lp_kerberos_encryption_types() == KERBEROS_ETYPES_ALL ||
+           lp_kerberos_encryption_types() == KERBEROS_ETYPES_LEGACY) {
+               legacy_enctypes = "RC4-HMAC DES-CBC-CRC DES-CBC-MD5";
+       }
+
+       enctypes =
+           talloc_asprintf(mem_ctx, "\tdefault_tgs_enctypes = %s %s\n"
+                                    "\tdefault_tkt_enctypes = %s %s\n"
+                                    "\tpreferred_enctypes = %s %s\n",
+                           aes_enctypes, legacy_enctypes, aes_enctypes,
+                           legacy_enctypes, aes_enctypes, legacy_enctypes);
+done:
+       TALLOC_FREE(aes_enctypes);
+       return enctypes;
+}
+#else /* Heimdal version */
+static char *get_enctypes(TALLOC_CTX *mem_ctx)
+{
+       const char *aes_enctypes = "";
+       const char *legacy_enctypes = "";
+       char *enctypes = NULL;
+
+       if (lp_kerberos_encryption_types() == KERBEROS_ETYPES_ALL ||
+           lp_kerberos_encryption_types() == KERBEROS_ETYPES_STRONG) {
+               aes_enctypes =
+                   "aes256-cts-hmac-sha1-96 aes128-cts-hmac-sha1-96";
+       }
+
+       if (lp_kerberos_encryption_types() == KERBEROS_ETYPES_ALL ||
+           lp_kerberos_encryption_types() == KERBEROS_ETYPES_LEGACY) {
+               legacy_enctypes = "arcfour-hmac-md5 des-cbc-crc des-cbc-md5";
+       }
+
+       enctypes = talloc_asprintf(mem_ctx, "\tdefault_etypes = %s %s\n",
+                                  aes_enctypes, legacy_enctypes);
+
+       return enctypes;
+}
+#endif
+
+uint32_t kerberos_supported_encryption_types(void)
+{
+       uint32_t encryption_types = 0;
+
+       if (lp_kerberos_encryption_types() == KERBEROS_ETYPES_ALL ||
+           lp_kerberos_encryption_types() == KERBEROS_ETYPES_STRONG) {
+#ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
+               encryption_types |= ENC_HMAC_SHA1_96_AES128;
+#endif
+#ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
+               encryption_types |= ENC_HMAC_SHA1_96_AES256;
+#endif
+       }
+
+       if (lp_kerberos_encryption_types() == KERBEROS_ETYPES_ALL ||
+           lp_kerberos_encryption_types() == KERBEROS_ETYPES_LEGACY) {
+               encryption_types |= ENC_CRC32 | ENC_RSA_MD5 | ENC_RC4_HMAC_MD5;
+       }
+
+       return encryption_types;
+}
+
 bool create_local_private_krb5_conf_for_domain(const char *realm,
                                                const char *domain,
                                                const char *sitename,
@@ -767,7 +636,8 @@ bool create_local_private_krb5_conf_for_domain(const char *realm,
        int fd;
        char *realm_upper = NULL;
        bool result = false;
-       char *aes_enctypes = NULL;
+       char *enctypes = NULL;
+       const char *include_system_krb5 = "";
        mode_t mask;
 
        if (!lp_create_krb5_conf()) {
@@ -784,7 +654,7 @@ bool create_local_private_krb5_conf_for_domain(const char *realm,
                return false;
        }
 
-       dname = lock_path("smb_krb5");
+       dname = lock_path(talloc_tos(), "smb_krb5");
        if (!dname) {
                return false;
        }
@@ -795,7 +665,7 @@ bool create_local_private_krb5_conf_for_domain(const char *realm,
                goto done;
        }
 
-       tmpname = lock_path("smb_tmp_krb5.XXXXXX");
+       tmpname = lock_path(talloc_tos(), "smb_tmp_krb5.XXXXXX");
        if (!tmpname) {
                goto done;
        }
@@ -818,33 +688,30 @@ bool create_local_private_krb5_conf_for_domain(const char *realm,
                goto done;
        }
 
-       aes_enctypes = talloc_strdup(fname, "");
-       if (aes_enctypes == NULL) {
+       enctypes = get_enctypes(fname);
+       if (enctypes == NULL) {
                goto done;
        }
 
-#ifdef HAVE_ENCTYPE_AES256_CTS_HMAC_SHA1_96
-       aes_enctypes = talloc_asprintf_append(aes_enctypes, "%s", "aes256-cts-hmac-sha1-96 ");
-       if (aes_enctypes == NULL) {
-               goto done;
-       }
-#endif
-#ifdef HAVE_ENCTYPE_AES128_CTS_HMAC_SHA1_96
-       aes_enctypes = talloc_asprintf_append(aes_enctypes, "%s", "aes128-cts-hmac-sha1-96");
-       if (aes_enctypes == NULL) {
-               goto done;
+#if !defined(SAMBA4_USES_HEIMDAL)
+       if (lp_include_system_krb5_conf()) {
+               include_system_krb5 = "include /etc/krb5.conf";
        }
 #endif
 
-       file_contents = talloc_asprintf(fname,
-                                       "[libdefaults]\n\tdefault_realm = %s\n"
-                                       "\tdefault_tgs_enctypes = %s RC4-HMAC DES-CBC-CRC DES-CBC-MD5\n"
-                                       "\tdefault_tkt_enctypes = %s RC4-HMAC DES-CBC-CRC DES-CBC-MD5\n"
-                                       "\tpreferred_enctypes = %s RC4-HMAC DES-CBC-CRC DES-CBC-MD5\n\n"
-                                       "[realms]\n\t%s = {\n"
-                                       "\t%s\t}\n",
-                                       realm_upper, aes_enctypes, aes_enctypes, aes_enctypes,
-                                       realm_upper, kdc_ip_string);
+       file_contents =
+           talloc_asprintf(fname,
+                           "[libdefaults]\n\tdefault_realm = %s\n"
+                           "%s"
+                           "\tdns_lookup_realm = false\n\n"
+                           "[realms]\n\t%s = {\n"
+                           "%s\t}\n"
+                           "%s\n",
+                           realm_upper,
+                           enctypes,
+                           realm_upper,
+                           kdc_ip_string,
+                           include_system_krb5);
 
        if (!file_contents) {
                goto done;