Revert "pthreadpool: add pthreadpool_tevent_[current_job_]per_thread_cwd()"
[samba.git] / lib / krb5_wrap / krb5_samba.c
index 6fa0d827cdb48c2f2be876a97ff77af927a152a0..f0dc86b1859572848415ff14657ccba3e6ce463a 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);
@@ -422,6 +423,221 @@ int smb_krb5_get_pw_salt(krb5_context context,
 #error UNKNOWN_SALT_FUNCTIONS
 #endif
 
+/**
+ * @brief This constructs the salt principal used by active directory
+ *
+ * Most Kerberos encryption types require a salt in order to
+ * calculate the long term private key for user/computer object
+ * based on a password.
+ *
+ * The returned _salt_principal is a string in forms like this:
+ * - host/somehost.example.com@EXAMPLE.COM
+ * - SomeAccount@EXAMPLE.COM
+ * - SomePrincipal@EXAMPLE.COM
+ *
+ * This is not the form that's used as salt, it's just
+ * 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.
+ *
+ * @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]  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,
+                           uint32_t uac_flags,
+                           TALLOC_CTX *mem_ctx,
+                           char **_salt_principal)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       char *upper_realm = NULL;
+       const char *principal = NULL;
+       int principal_len = 0;
+
+       *_salt_principal = NULL;
+
+       if (sAMAccountName == NULL) {
+               TALLOC_FREE(frame);
+               return EINVAL;
+       }
+
+       if (realm == NULL) {
+               TALLOC_FREE(frame);
+               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);
+               return ENOMEM;
+       }
+
+       /* Many, many thanks to lukeh@padl.com for this
+        * algorithm, described in his Nov 10 2004 mail to
+        * samba-technical@lists.samba.org */
+
+       /*
+        * Determine a salting principal
+        */
+       if (uac_flags & UF_TRUST_ACCOUNT_MASK) {
+               int computer_len = 0;
+               char *tmp = NULL;
+
+               computer_len = strlen(sAMAccountName);
+               if (sAMAccountName[computer_len-1] == '$') {
+                       computer_len -= 1;
+               }
+
+               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 {
+
+                       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) {
+               char *p;
+
+               principal = userPrincipalName;
+               p = strchr(principal, '@');
+               if (p != NULL) {
+                       principal_len = PTR_DIFF(p, principal);
+               } else {
+                       principal_len = strlen(principal);
+               }
+       } else {
+               principal = sAMAccountName;
+               principal_len = strlen(principal);
+       }
+
+       *_salt_principal = talloc_asprintf(mem_ctx, "%*.*s@%s",
+                                          principal_len, principal_len,
+                                          principal, upper_realm);
+       if (*_salt_principal == NULL) {
+               TALLOC_FREE(frame);
+               return ENOMEM;
+       }
+
+       TALLOC_FREE(frame);
+       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
@@ -716,9 +932,10 @@ krb5_error_code smb_krb5_renew_ticket(const char *ccache_string,
        ZERO_STRUCT(creds);
        ZERO_STRUCT(creds_in);
 
-       initialize_krb5_error_table();
-       ret = krb5_init_context(&context);
+       ret = smb_krb5_init_context_common(&context);
        if (ret) {
+               DBG_ERR("kerberos init context failed (%s)\n",
+                       error_message(ret));
                goto done;
        }
 
@@ -900,7 +1117,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;
                }
 
@@ -1009,7 +1226,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
@@ -1147,7 +1364,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
@@ -1187,6 +1404,8 @@ krb5_error_code smb_krb5_kt_open(krb5_context context,
                goto open_keytab;
        }
 
+       DBG_WARNING("ERROR: Invalid keytab name: %s\n", keytab_name_req);
+
        return KRB5_KT_BADNAME;
 
 open_keytab:
@@ -1247,7 +1466,7 @@ krb5_error_code smb_krb5_kt_get_name(TALLOC_CTX *mem_ctx,
  *
  * @param[in]  princ         The principal as a krb5_principal to search for.
  *
- * @param[in]  flush         Weather to flush the complete keytab.
+ * @param[in]  flush         Whether to flush the complete keytab.
  *
  * @param[in]  keep_old_entries Keep the entry with the previous kvno.
  *
@@ -1360,7 +1579,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] "
@@ -1440,7 +1659,7 @@ out:
  *                            this is only set to false for encryption types
  *                            which do not support salting like RC4.
  *
- * @param[in]  keep_old_entries Wether to keep or delte old keytab entries.
+ * @param[in]  keep_old_entries Whether to keep or delete old keytab entries.
  *
  * @retval 0 on Success
  *
@@ -1738,7 +1957,7 @@ krb5_error_code smb_krb5_keyblock_init_contents(krb5_context context,
 /**
  * @brief Simulate a kinit by putting the tgt in the given credential cache.
  *
- * This function uses a keyblock rather than needingthe original password.
+ * This function uses a keyblock rather than needing the original password.
  *
  * @param[in]  ctx      The library context
  *
@@ -2562,24 +2781,25 @@ krb5_error_code smb_krb5_make_pac_checksum(TALLOC_CTX *mem_ctx,
 /**
  * @brief Get realm of a principal
  *
+ * @param[in] mem_ctx   The talloc ctx to put the result on
+ *
  * @param[in] context   The library context
  *
  * @param[in] principal The principal to get the realm from.
  *
- * @return An allocated string with the realm or NULL if an error occurred.
- *
- * The caller must free the realm string with free() if not needed anymore.
+ * @return A talloced string with the realm or NULL if an error occurred.
  */
-char *smb_krb5_principal_get_realm(krb5_context context,
+char *smb_krb5_principal_get_realm(TALLOC_CTX *mem_ctx,
+                                  krb5_context context,
                                   krb5_const_principal principal)
 {
 #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
-       return strdup(discard_const_p(char, krb5_principal_get_realm(context, principal)));
+       return talloc_strdup(mem_ctx,
+                            krb5_principal_get_realm(context, principal));
 #elif defined(krb5_princ_realm) /* MIT */
-       krb5_data *realm;
-       realm = discard_const_p(krb5_data,
-                               krb5_princ_realm(context, principal));
-       return strndup(realm->data, realm->length);
+       const krb5_data *realm;
+       realm = krb5_princ_realm(context, principal);
+       return talloc_strndup(mem_ctx, realm->data, realm->length);
 #else
 #error UNKNOWN_GET_PRINC_REALM_FUNCTIONS
 #endif
@@ -2661,12 +2881,18 @@ char *smb_krb5_get_realm_from_hostname(TALLOC_CTX *mem_ctx,
        krb5_error_code kerr;
        krb5_context ctx = NULL;
 
-       initialize_krb5_error_table();
-       if (krb5_init_context(&ctx)) {
+       kerr = smb_krb5_init_context_common(&ctx);
+       if (kerr) {
+               DBG_ERR("kerberos init context failed (%s)\n",
+                       error_message(kerr));
                return NULL;
        }
 
        kerr = krb5_get_host_realm(ctx, hostname, &realm_list);
+       if (kerr == KRB5_ERR_HOST_REALM_UNKNOWN) {
+               realm_list = NULL;
+               kerr = 0;
+       }
        if (kerr != 0) {
                DEBUG(3,("kerberos_get_realm_from_hostname %s: "
                        "failed %s\n",
@@ -3279,11 +3505,10 @@ int ads_krb5_cli_get_ticket(TALLOC_CTX *mem_ctx,
                ENCTYPE_NULL};
        bool ok;
 
-       initialize_krb5_error_table();
-       retval = krb5_init_context(&context);
+       retval = smb_krb5_init_context_common(&context);
        if (retval != 0) {
-               DBG_WARNING("krb5_init_context failed (%s)\n",
-                           error_message(retval));
+               DBG_ERR("kerberos init context failed (%s)\n",
+                       error_message(retval));
                goto failed;
        }
 
@@ -3348,6 +3573,45 @@ failed:
        return retval;
 }
 
+#ifndef SAMBA4_USES_HEIMDAL /* MITKRB5 tracing callback */
+static void smb_krb5_trace_cb(krb5_context ctx,
+                             const krb5_trace_info *info,
+                             void *data)
+{
+       if (info != NULL) {
+               DBGC_DEBUG(DBGC_KERBEROS, "%s", info->message);
+       }
+}
+#endif
+
+krb5_error_code smb_krb5_init_context_common(krb5_context *_krb5_context)
+{
+       krb5_error_code ret;
+       krb5_context krb5_ctx;
+
+       initialize_krb5_error_table();
+
+       ret = krb5_init_context(&krb5_ctx);
+       if (ret) {
+               DBG_ERR("Krb5 context initialization failed (%s)\n",
+                        error_message(ret));
+               return ret;
+       }
+
+       /* The MIT Kerberos build relies on using the system krb5.conf file.
+        * If you really want to use another file please set KRB5_CONFIG
+        * accordingly. */
+#ifndef SAMBA4_USES_HEIMDAL
+       ret = krb5_set_trace_callback(krb5_ctx, smb_krb5_trace_cb, NULL);
+       if (ret) {
+               DBG_ERR("Failed to set MIT kerberos trace callback! (%s)\n",
+                       error_message(ret));
+       }
+#endif
+       *_krb5_context = krb5_ctx;
+       return 0;
+}
+
 #else /* HAVE_KRB5 */
 /* This saves a few linking headaches */
 int ads_krb5_cli_get_ticket(TALLOC_CTX *mem_ctx,