krb5-samba: interdomain trust uses different salt principal
[samba.git] / lib / krb5_wrap / krb5_samba.c
index 153221728c14e967216772613176f402a9b536e1..a6ff97640cae80274429c81e6a77b09fb3b1709d 100644 (file)
@@ -24,6 +24,7 @@
 #include "system/filesys.h"
 #include "krb5_samba.h"
 #include "lib/crypto/crypto.h"
+#include "../libds/common/flags.h"
 
 #ifdef HAVE_COM_ERR_H
 #include <com_err.h>
@@ -150,7 +151,7 @@ bool smb_krb5_sockaddr_to_kaddr(struct sockaddr_storage *paddr,
                                krb5_address *pkaddr)
 {
        memset(pkaddr, '\0', sizeof(krb5_address));
-#if defined(HAVE_IPV6) && defined(KRB5_ADDRESS_INET6)
+#ifdef HAVE_IPV6
        if (paddr->ss_family == AF_INET6) {
                pkaddr->addr_type = KRB5_ADDRESS_INET6;
                pkaddr->address.length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr);
@@ -183,7 +184,7 @@ bool smb_krb5_sockaddr_to_kaddr(struct sockaddr_storage *paddr,
                                krb5_address *pkaddr)
 {
        memset(pkaddr, '\0', sizeof(krb5_address));
-#if defined(HAVE_IPV6) && defined(ADDRTYPE_INET6)
+#ifdef HAVE_IPV6
        if (paddr->ss_family == AF_INET6) {
                pkaddr->addrtype = ADDRTYPE_INET6;
                pkaddr->length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr);
@@ -435,7 +436,8 @@ int smb_krb5_get_pw_salt(krb5_context context,
  * - SomePrincipal@EXAMPLE.COM
  *
  * This is not the form that's used as salt, it's just
- * the human readable form.
+ * the human readable form. It needs to be converted by
+ * smb_krb5_salt_principal2data().
  *
  * @param[in]  realm              The realm the user/computer is added too.
  *
@@ -444,19 +446,20 @@ int smb_krb5_get_pw_salt(krb5_context context,
  * @param[in]  userPrincipalName  The userPrincipalName attribute of the object
  *                                or NULL is not available.
  *
- * @param[in]  is_computer        The indication of the object includes
- *                                objectClass=computer.
+ * @param[in]  uac_flags          UF_ACCOUNT_TYPE_MASKed userAccountControl field
  *
  * @param[in]  mem_ctx            The TALLOC_CTX to allocate _salt_principal.
  *
  * @param[out]  _salt_principal   The resulting principal as string.
  *
  * @retval 0 Success; otherwise - Kerberos error codes
+ *
+ * @see smb_krb5_salt_principal2data
  */
 int smb_krb5_salt_principal(const char *realm,
                            const char *sAMAccountName,
                            const char *userPrincipalName,
-                           bool is_computer,
+                           uint32_t uac_flags,
                            TALLOC_CTX *mem_ctx,
                            char **_salt_principal)
 {
@@ -477,6 +480,23 @@ int smb_krb5_salt_principal(const char *realm,
                return EINVAL;
        }
 
+       if (uac_flags & ~UF_ACCOUNT_TYPE_MASK) {
+               /*
+                * catch callers which still
+                * pass 'true'.
+                */
+               TALLOC_FREE(frame);
+               return EINVAL;
+       }
+       if (uac_flags == 0) {
+               /*
+                * catch callers which still
+                * pass 'false'.
+                */
+               TALLOC_FREE(frame);
+               return EINVAL;
+       }
+
        upper_realm = strupper_talloc(frame, realm);
        if (upper_realm == NULL) {
                TALLOC_FREE(frame);
@@ -490,7 +510,7 @@ int smb_krb5_salt_principal(const char *realm,
        /*
         * Determine a salting principal
         */
-       if (is_computer) {
+       if (uac_flags & UF_TRUST_ACCOUNT_MASK) {
                int computer_len = 0;
                char *tmp = NULL;
 
@@ -499,20 +519,32 @@ int smb_krb5_salt_principal(const char *realm,
                        computer_len -= 1;
                }
 
-               tmp = talloc_asprintf(frame, "host/%*.*s.%s",
-                                     computer_len, computer_len,
-                                     sAMAccountName, realm);
-               if (tmp == NULL) {
-                       TALLOC_FREE(frame);
-                       return ENOMEM;
-               }
+               if (uac_flags & UF_INTERDOMAIN_TRUST_ACCOUNT) {
+                       principal = talloc_asprintf(frame, "krbtgt/%*.*s",
+                                                   computer_len, computer_len,
+                                                   sAMAccountName);
+                       if (principal == NULL) {
+                               TALLOC_FREE(frame);
+                               return ENOMEM;
+                       }
+               } else {
 
-               principal = strlower_talloc(frame, tmp);
-               TALLOC_FREE(tmp);
-               if (principal == NULL) {
-                       TALLOC_FREE(frame);
-                       return ENOMEM;
+                       tmp = talloc_asprintf(frame, "host/%*.*s.%s",
+                                             computer_len, computer_len,
+                                             sAMAccountName, realm);
+                       if (tmp == NULL) {
+                               TALLOC_FREE(frame);
+                               return ENOMEM;
+                       }
+
+                       principal = strlower_talloc(frame, tmp);
+                       TALLOC_FREE(tmp);
+                       if (principal == NULL) {
+                               TALLOC_FREE(frame);
+                               return ENOMEM;
+                       }
                }
+
                principal_len = strlen(principal);
 
        } else if (userPrincipalName != NULL) {
@@ -542,6 +574,70 @@ int smb_krb5_salt_principal(const char *realm,
        return 0;
 }
 
+/**
+ * @brief Converts the salt principal string into the salt data blob
+ *
+ * This function takes a salt_principal as string in forms like this:
+ * - host/somehost.example.com@EXAMPLE.COM
+ * - SomeAccount@EXAMPLE.COM
+ * - SomePrincipal@EXAMPLE.COM
+ *
+ * It generates values like:
+ * - EXAMPLE.COMhost/somehost.example.com
+ * - EXAMPLE.COMSomeAccount
+ * - EXAMPLE.COMSomePrincipal
+ *
+ * @param[in]  realm              The realm the user/computer is added too.
+ *
+ * @param[in]  sAMAccountName     The sAMAccountName attribute of the object.
+ *
+ * @param[in]  userPrincipalName  The userPrincipalName attribute of the object
+ *                                or NULL is not available.
+ *
+ * @param[in]  is_computer        The indication of the object includes
+ *                                objectClass=computer.
+ *
+ * @param[in]  mem_ctx            The TALLOC_CTX to allocate _salt_principal.
+ *
+ * @param[out]  _salt_principal   The resulting principal as string.
+ *
+ * @retval 0 Success; otherwise - Kerberos error codes
+ *
+ * @see smb_krb5_salt_principal
+ */
+int smb_krb5_salt_principal2data(krb5_context context,
+                                const char *salt_principal,
+                                TALLOC_CTX *mem_ctx,
+                                char **_salt_data)
+{
+       krb5_error_code ret;
+       krb5_principal salt_princ = NULL;
+       krb5_data salt;
+
+       *_salt_data = NULL;
+
+       ret = krb5_parse_name(context, salt_principal, &salt_princ);
+       if (ret != 0) {
+               return ret;
+       }
+
+       ret = smb_krb5_get_pw_salt(context, salt_princ, &salt);
+       krb5_free_principal(context, salt_princ);
+       if (ret != 0) {
+               return ret;
+       }
+
+       *_salt_data = talloc_strndup(mem_ctx,
+                                    (char *)salt.data,
+                                    salt.length);
+       smb_krb5_free_data_contents(context, &salt);
+       if (*_salt_data == NULL) {
+               return ENOMEM;
+       }
+
+       return 0;
+}
+
 #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES)
 /**
  * @brief Get a list of encryption types allowed for session keys
@@ -1020,7 +1116,7 @@ krb5_error_code smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses **kerb_addr
                addrs->val = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
                if (addrs->val == NULL) {
                        SAFE_FREE(addrs);
-                       SAFE_FREE(kerb_addr);
+                       SAFE_FREE(*kerb_addr);
                        return ENOMEM;
                }
 
@@ -1129,7 +1225,7 @@ krb5_error_code smb_krb5_enctype_to_string(krb5_context context,
 /**
  * @brief Open a key table readonly or with readwrite access.
  *
- * Allows to use a different keytab than the default one using a relative
+ * Allows one to use a different keytab than the default one using a relative
  * path to the keytab.
  *
  * @param[in]  context  The library context
@@ -1267,7 +1363,7 @@ out:
 /**
  * @brief Open a key table readonly or with readwrite access.
  *
- * Allows to use a different keytab than the default one. The path needs to be
+ * Allows one to use a different keytab than the default one. The path needs to be
  * an absolute path or an error will be returned.
  *
  * @param[in]  context  The library context
@@ -1482,7 +1578,7 @@ krb5_error_code smb_krb5_kt_seek_and_delete_old_entries(krb5_context context,
                }
 
                if (!flush &&
-                   (kt_entry.vno == kvno) &&
+                   ((kt_entry.vno & 0xff) == (kvno & 0xff)) &&
                    (kt_entry_enctype != enctype))
                {
                        DEBUG(5, (__location__ ": Saving entry with kvno [%d] "