more enctypes...
[metze/samba/wip.git] / source3 / libads / kerberos.c
index 13c48ca40236b21aa86bea8966e073faf4336890..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"
@@ -30,6 +31,7 @@
 #include "secrets.h"
 #include "../lib/tsocket/tsocket.h"
 #include "lib/util/asn1.h"
+#include "krb5_errs.h"
 
 #ifdef HAVE_KRB5
 
@@ -58,7 +60,7 @@ kerb_prompter(krb5_context ctx, void *data,
                 * version have looping detection and return with a proper error code.
                 */
 
-#if HAVE_KRB5_PROMPT_TYPE /* Heimdal */
+#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) {
                        /*
@@ -126,9 +128,12 @@ int kerberos_kinit_password_ext(const char *principal,
 
        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);
@@ -242,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;
        }
 
@@ -272,134 +277,6 @@ 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.
- ************************************************************************/
-
-char *kerberos_fetch_salt_princ_for_host_princ(krb5_context context,
-                                              const char *host_princ_s,
-                                              int enctype)
-{
-       char *salt_princ_s;
-       /* lookup new key first */
-
-       salt_princ_s = kerberos_secrets_fetch_des_salt();
-       if (salt_princ_s == NULL) {
-
-               /* look under the old key.  If this fails, just use the standard key */
-               salt_princ_s = kerberos_secrets_fetch_salting_principal(host_princ_s,
-                                                                       enctype);
-               if (salt_princ_s == NULL) {
-                       /* fall back to host/machine.realm@REALM */
-                       salt_princ_s = kerberos_standard_des_salt();
-               }
-       }
-
-       return salt_princ_s;
-}
-
 int create_kerberos_key_from_string(krb5_context context,
                                        krb5_principal host_princ,
                                        krb5_principal salt_princ,
@@ -433,78 +310,6 @@ int create_kerberos_key_from_string(krb5_context context,
        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;
-}
-
-
 /************************************************************************
 ************************************************************************/
 
@@ -536,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],
@@ -582,12 +387,12 @@ static char *get_kdc_ip_string(char *mem_ctx,
                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;
@@ -648,7 +453,7 @@ static char *get_kdc_ip_string(char *mem_ctx,
                                      struct tsocket_address *,
                                      num_dcs);
 
-       DEBUG(10, ("%d additional KDCs to test\n", num_dcs));
+       DBG_DEBUG("%zu additional KDCs to test\n", num_dcs);
        if (num_dcs == 0) {
                goto out;
        }
@@ -794,6 +599,28 @@ static char *get_enctypes(TALLOC_CTX *mem_ctx)
 }
 #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,
@@ -827,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;
        }
@@ -838,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;
        }