krb5_wrap: let smb_krb5_kinit_s4u2_ccache() work if store_creds.client and server...
[samba.git] / lib / krb5_wrap / krb5_samba.c
index 11851846d0c43c8869b3dfc62f9aba2d9cc3c4c1..bb0b5dfa6209c40751aa46a94e9a40646b22d167 100644 (file)
@@ -23,7 +23,6 @@
 #include "includes.h"
 #include "system/filesys.h"
 #include "krb5_samba.h"
 #include "includes.h"
 #include "system/filesys.h"
 #include "krb5_samba.h"
-#include "lib/util/asn1.h"
 
 #ifdef HAVE_COM_ERR_H
 #include <com_err.h>
 
 #ifdef HAVE_COM_ERR_H
 #include <com_err.h>
@@ -207,6 +206,8 @@ krb5_error_code smb_krb5_mk_error(krb5_context context,
                                  krb5_error_code error_code,
                                  const char *e_text,
                                  krb5_data *e_data,
                                  krb5_error_code error_code,
                                  const char *e_text,
                                  krb5_data *e_data,
+                                 const krb5_principal client,
+                                 const krb5_principal server,
                                  krb5_data *enc_err)
 {
        krb5_error_code code = EINVAL;
                                  krb5_data *enc_err)
 {
        krb5_error_code code = EINVAL;
@@ -215,27 +216,59 @@ krb5_error_code smb_krb5_mk_error(krb5_context context,
                             error_code,
                             e_text,
                             e_data,
                             error_code,
                             e_text,
                             e_data,
-                            NULL, /* client */
-                            NULL, /* server */
+                            client,
+                            server,
                             NULL, /* client_time */
                             NULL, /* client_usec */
                             enc_err);
 #else
                             NULL, /* client_time */
                             NULL, /* client_usec */
                             enc_err);
 #else
-       krb5_error dec_err = {
-               .error = error_code,
-       };
+       krb5_principal unspec_server = NULL;
+       krb5_error errpkt;
 
 
+       errpkt.ctime = 0;
+       errpkt.cusec = 0;
+
+       code = krb5_us_timeofday(context,
+                                &errpkt.stime,
+                                &errpkt.susec);
+       if (code != 0) {
+               return code;
+       }
+
+       errpkt.error = error_code;
+
+       errpkt.text.length = 0;
        if (e_text != NULL) {
        if (e_text != NULL) {
-               dec_err.text.length = strlen(e_text);
-               dec_err.text.data = discard_const_p(char, e_text);
+               errpkt.text.length = strlen(e_text);
+               errpkt.text.data = discard_const_p(char, e_text);
        }
        }
+
+       errpkt.e_data.magic = KV5M_DATA;
+       errpkt.e_data.length = 0;
+       errpkt.e_data.data = NULL;
        if (e_data != NULL) {
        if (e_data != NULL) {
-               dec_err.e_data = *e_data;
+               errpkt.e_data = *e_data;
+       }
+
+       errpkt.client = client;
+
+       if (server != NULL) {
+               errpkt.server = server;
+       } else {
+               code = smb_krb5_make_principal(context,
+                                              &unspec_server,
+                                              "<unspecified realm>",
+                                              NULL);
+               if (code != 0) {
+                       return code;
+               }
+               errpkt.server = unspec_server;
        }
 
        code = krb5_mk_error(context,
        }
 
        code = krb5_mk_error(context,
-                            &dec_err,
+                            &errpkt,
                             enc_err);
                             enc_err);
+       krb5_free_principal(context, unspec_server);
 #endif
        return code;
 }
 #endif
        return code;
 }
@@ -386,61 +419,23 @@ krb5_error_code smb_krb5_get_allowed_etypes(krb5_context context,
 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
 #endif
 
 #error UNKNOWN_GET_ENCTYPES_FUNCTIONS
 #endif
 
-bool unwrap_edata_ntstatus(TALLOC_CTX *mem_ctx,
-                          DATA_BLOB *edata,
-                          DATA_BLOB *edata_out)
-{
-       DATA_BLOB edata_contents;
-       ASN1_DATA *data;
-       int edata_type;
-
-       if (!edata->length) {
-               return false;
-       }
-
-       data = asn1_init(mem_ctx);
-       if (data == NULL) {
-               return false;
-       }
-
-       if (!asn1_load(data, *edata)) goto err;
-       if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) goto err;
-       if (!asn1_start_tag(data, ASN1_CONTEXT(1))) goto err;
-       if (!asn1_read_Integer(data, &edata_type)) goto err;
-
-       if (edata_type != KRB5_PADATA_PW_SALT) {
-               DEBUG(0,("edata is not of required type %d but of type %d\n",
-                       KRB5_PADATA_PW_SALT, edata_type));
-               goto err;
-       }
-
-       if (!asn1_start_tag(data, ASN1_CONTEXT(2))) goto err;
-       if (!asn1_read_OctetString(data, talloc_tos(), &edata_contents)) goto err;
-       if (!asn1_end_tag(data)) goto err;
-       if (!asn1_end_tag(data)) goto err;
-       if (!asn1_end_tag(data)) goto err;
-       asn1_free(data);
-
-       *edata_out = data_blob_talloc(mem_ctx, edata_contents.data, edata_contents.length);
-
-       data_blob_free(&edata_contents);
-
-       return true;
-
-  err:
-
-       asn1_free(data);
-       return false;
-}
-
-
-/**************************************************************
- krb5_parse_name that takes a UNIX charset.
-**************************************************************/
 
 
+/**
+ * @brief Convert a string principal name to a Kerberos principal.
+ *
+ * @param[in]  context  The library context
+ *
+ * @param[in]  name     The principal as a unix charset string.
+ *
+ * @param[out] principal The newly allocated principal.
+ *
+ * Use krb5_free_principal() to free a principal when it is no longer needed.
+ *
+ * @return 0 on success, a Kerberos error code otherwise.
+ */
 krb5_error_code smb_krb5_parse_name(krb5_context context,
 krb5_error_code smb_krb5_parse_name(krb5_context context,
-                               const char *name, /* in unix charset */
-                               krb5_principal *principal)
+                                   const char *name,
+                                   krb5_principal *principal)
 {
        krb5_error_code ret;
        char *utf8_name;
 {
        krb5_error_code ret;
        char *utf8_name;
@@ -457,11 +452,26 @@ krb5_error_code smb_krb5_parse_name(krb5_context context,
        return ret;
 }
 
        return ret;
 }
 
-/**************************************************************
- krb5_parse_name that returns a UNIX charset name. Must
- be freed with talloc_free() call.
-**************************************************************/
-
+/**
+ * @brief Convert a Kerberos principal structure to a string representation.
+ *
+ * The resulting string representation will be a unix charset name and is
+ * talloc'ed.
+ *
+ * @param[in]  mem_ctx  The talloc context to allocate memory on.
+ *
+ * @param[in]  context  The library context.
+ *
+ * @param[in]  principal The principal.
+ *
+ * @param[out] unix_name A string representation of the princpial name as with
+ *                       unix charset.
+ *
+ * Use talloc_free() to free the string representation if it is no longer
+ * needed.
+ *
+ * @return 0 on success, a Kerberos error code otherwise.
+ */
 krb5_error_code smb_krb5_unparse_name(TALLOC_CTX *mem_ctx,
                                      krb5_context context,
                                      krb5_const_principal principal,
 krb5_error_code smb_krb5_unparse_name(TALLOC_CTX *mem_ctx,
                                      krb5_context context,
                                      krb5_const_principal principal,
@@ -485,25 +495,6 @@ krb5_error_code smb_krb5_unparse_name(TALLOC_CTX *mem_ctx,
        return 0;
 }
 
        return 0;
 }
 
-krb5_error_code smb_krb5_parse_name_norealm(krb5_context context, 
-                                           const char *name, 
-                                           krb5_principal *principal)
-{
-       /* we are cheating here because parse_name will in fact set the realm.
-        * We don't care as the only caller of smb_krb5_parse_name_norealm
-        * ignores the realm anyway when calling
-        * smb_krb5_principal_compare_any_realm later - Guenther */
-
-       return smb_krb5_parse_name(context, name, principal);
-}
-
-bool smb_krb5_principal_compare_any_realm(krb5_context context, 
-                                         krb5_const_principal princ1, 
-                                         krb5_const_principal princ2)
-{
-       return krb5_principal_compare_any_realm(context, princ1, princ2);
-}
-
 /**
  * @brief Free the contents of a krb5_data structure and zero the data field.
  *
 /**
  * @brief Free the contents of a krb5_data structure and zero the data field.
  *
@@ -1032,8 +1023,8 @@ krb5_error_code smb_krb5_kt_open_relative(krb5_context context,
                        goto out;
                }
 
                        goto out;
                }
 
-               if ((strncmp(keytab_name_req, "WRFILE:/", 8) == 0) ||
-                   (strncmp(keytab_name_req, "FILE:/", 6) == 0)) {
+               if ((strncmp(keytab_name_req, "WRFILE:", 7) == 0) ||
+                   (strncmp(keytab_name_req, "FILE:", 5) == 0)) {
                        tmp = keytab_name_req;
                        goto resolve;
                }
                        tmp = keytab_name_req;
                        goto resolve;
                }
@@ -1139,12 +1130,29 @@ krb5_error_code smb_krb5_kt_open(krb5_context context,
                                 bool write_access,
                                 krb5_keytab *keytab)
 {
                                 bool write_access,
                                 krb5_keytab *keytab)
 {
-       if (keytab_name_req != NULL) {
-               if (keytab_name_req[0] != '/') {
-                       return KRB5_KT_BADNAME;
-               }
+       int cmp;
+
+       if (keytab_name_req == NULL) {
+               return KRB5_KT_BADNAME;
+       }
+
+       if (keytab_name_req[0] == '/') {
+               goto open_keytab;
+       }
+
+       cmp = strncmp(keytab_name_req, "FILE:/", 6);
+       if (cmp == 0) {
+               goto open_keytab;
        }
 
        }
 
+       cmp = strncmp(keytab_name_req, "WRFILE:/", 8);
+       if (cmp == 0) {
+               goto open_keytab;
+       }
+
+       return KRB5_KT_BADNAME;
+
+open_keytab:
        return smb_krb5_kt_open_relative(context,
                                         keytab_name_req,
                                         write_access,
        return smb_krb5_kt_open_relative(context,
                                         keytab_name_req,
                                         write_access,
@@ -1221,17 +1229,13 @@ krb5_error_code smb_krb5_kt_seek_and_delete_old_entries(krb5_context context,
 {
        krb5_error_code ret;
        krb5_kt_cursor cursor;
 {
        krb5_error_code ret;
        krb5_kt_cursor cursor;
-       krb5_kt_cursor zero_csr;
        krb5_keytab_entry kt_entry;
        krb5_keytab_entry kt_entry;
-       krb5_keytab_entry zero_kt_entry;
        char *ktprinc = NULL;
        krb5_kvno old_kvno = kvno - 1;
        TALLOC_CTX *tmp_ctx;
 
        ZERO_STRUCT(cursor);
        char *ktprinc = NULL;
        krb5_kvno old_kvno = kvno - 1;
        TALLOC_CTX *tmp_ctx;
 
        ZERO_STRUCT(cursor);
-       ZERO_STRUCT(zero_csr);
        ZERO_STRUCT(kt_entry);
        ZERO_STRUCT(kt_entry);
-       ZERO_STRUCT(zero_kt_entry);
 
        ret = krb5_kt_start_seq_get(context, keytab, &cursor);
        if (ret == KRB5_KT_END || ret == ENOENT ) {
 
        ret = krb5_kt_start_seq_get(context, keytab, &cursor);
        if (ret == KRB5_KT_END || ret == ENOENT ) {
@@ -1366,10 +1370,10 @@ krb5_error_code smb_krb5_kt_seek_and_delete_old_entries(krb5_context context,
 
 out:
        talloc_free(tmp_ctx);
 
 out:
        talloc_free(tmp_ctx);
-       if (memcmp(&zero_kt_entry, &kt_entry, sizeof(krb5_keytab_entry))) {
+       if (!all_zero((uint8_t *)&kt_entry, sizeof(kt_entry))) {
                smb_krb5_kt_free_entry(context, &kt_entry);
        }
                smb_krb5_kt_free_entry(context, &kt_entry);
        }
-       if (memcmp(&cursor, &zero_csr, sizeof(krb5_kt_cursor)) != 0) {
+       if (!all_zero((uint8_t *)&cursor, sizeof(cursor))) {
                krb5_kt_end_seq_get(context, keytab, &cursor);
        }
        return ret;
                krb5_kt_end_seq_get(context, keytab, &cursor);
        }
        return ret;
@@ -1681,22 +1685,6 @@ krb5_error_code smb_krb5_keyblock_init_contents(krb5_context context,
 {
 #if defined(HAVE_KRB5_KEYBLOCK_INIT)
        return krb5_keyblock_init(context, enctype, data, length, key);
 {
 #if defined(HAVE_KRB5_KEYBLOCK_INIT)
        return krb5_keyblock_init(context, enctype, data, length, key);
-#elif defined(HAVE_KRB5_INIT_KEYBLOCK)
-       krb5_error_code code;
-
-       code = krb5_init_keyblock(context,
-                                 enctype,
-                                 length,
-                                 key);
-       if (code != 0) {
-               return code;
-       }
-
-       if (length != 0) {
-               memcpy(KRB5_KEY_DATA(key), data, length);
-       }
-
-       return 0;
 #else
        memset(key, 0, sizeof(krb5_keyblock));
        KRB5_KEY_DATA(key) = SMB_MALLOC(length);
 #else
        memset(key, 0, sizeof(krb5_keyblock));
        KRB5_KEY_DATA(key) = SMB_MALLOC(length);
@@ -1954,6 +1942,7 @@ krb5_error_code smb_krb5_kinit_s4u2_ccache(krb5_context ctx,
        krb5_principal target_princ;
        krb5_ccache tmp_cc;
        const char *self_realm;
        krb5_principal target_princ;
        krb5_ccache tmp_cc;
        const char *self_realm;
+       const char *client_realm = NULL;
        krb5_principal blacklist_principal = NULL;
        krb5_principal whitelist_principal = NULL;
 
        krb5_principal blacklist_principal = NULL;
        krb5_principal whitelist_principal = NULL;
 
@@ -2285,6 +2274,29 @@ krb5_error_code smb_krb5_kinit_s4u2_ccache(krb5_context ctx,
                return code;
        }
 
                return code;
        }
 
+       client_realm = krb5_principal_get_realm(ctx, store_creds.client);
+       if (client_realm != NULL) {
+               /*
+                * Because the CANON flag doesn't have any impact
+                * on the impersonate_principal => store_creds.client
+                * realm mapping. We need to store the credentials twice,
+                * once with the returned realm and once with the
+                * realm of impersonate_principal.
+                */
+               code = krb5_principal_set_realm(ctx, store_creds.server,
+                                               client_realm);
+               if (code != 0) {
+                       krb5_free_cred_contents(ctx, &store_creds);
+                       return code;
+               }
+
+               code = krb5_cc_store_cred(ctx, store_cc, &store_creds);
+               if (code != 0) {
+                       krb5_free_cred_contents(ctx, &store_creds);
+                       return code;
+               }
+       }
+
        if (expire_time) {
                *expire_time = (time_t) store_creds.times.endtime;
        }
        if (expire_time) {
                *expire_time = (time_t) store_creds.times.endtime;
        }
@@ -2773,15 +2785,12 @@ char *smb_get_krb5_error_message(krb5_context context,
 
 
 /**
 
 
 /**
-* @brief Return the kerberos library setting for "libdefaults:allow_weak_crypto"
-*
-* @param context       The krb5_context
-*
-* @return krb5_boolean
-*
-* Function returns true if weak crypto is allowd, false if not
-*/
-
+ * @brief Return the kerberos library setting for: libdefaults:allow_weak_crypto
+ *
+ * @param[in]  context  The library context
+ *
+ * @return True if weak crypto is allowed, false if not.
+ */
 krb5_boolean smb_krb5_get_allowed_weak_crypto(krb5_context context)
 #if defined(HAVE_KRB5_CONFIG_GET_BOOL_DEFAULT)
 {
 krb5_boolean smb_krb5_get_allowed_weak_crypto(krb5_context context)
 #if defined(HAVE_KRB5_CONFIG_GET_BOOL_DEFAULT)
 {
@@ -2825,13 +2834,14 @@ krb5_boolean smb_krb5_get_allowed_weak_crypto(krb5_context context)
 #endif
 
 /**
 #endif
 
 /**
-* @brief Return the type of a krb5_principal
-*
-* @param context       The krb5_context
-* @param principal     The const krb5_principal
-*
-* @return integer type of the principal
-*/
+ * @brief Return the type of a krb5_principal
+ *
+ * @param[in]  context  The library context.
+ *
+ * @param[in]  principal The principal to get the type from.
+ *
+ * @return The integer type of the principal.
+ */
 int smb_krb5_principal_get_type(krb5_context context,
                                krb5_const_principal principal)
 {
 int smb_krb5_principal_get_type(krb5_context context,
                                krb5_const_principal principal)
 {
@@ -2845,13 +2855,14 @@ int smb_krb5_principal_get_type(krb5_context context,
 }
 
 /**
 }
 
 /**
-* @brief Set the type of a krb5_principal
-*
-* @param context       The krb5_context
-* @param principal     The const krb5_principal
-* @param type          The principal type
-*
-*/
+ * @brief Set the type of a principal
+ *
+ * @param[in]  context  The library context
+ *
+ * @param[inout] principal The principal to set the type for.
+ *
+ * @param[in]  type     The principal type to set.
+ */
 void smb_krb5_principal_set_type(krb5_context context,
                                 krb5_principal principal,
                                 int type)
 void smb_krb5_principal_set_type(krb5_context context,
                                 krb5_principal principal,
                                 int type)
@@ -2865,34 +2876,61 @@ void smb_krb5_principal_set_type(krb5_context context,
 #endif
 }
 
 #endif
 }
 
-/**
-* @brief Generate a krb5 warning, forwarding to com_err
-*
-* @param context       The krb5_context
-* @param fmt           The message format
-* @param ...           The message arguments
-*
-* @return
-*/
 #if !defined(HAVE_KRB5_WARNX)
 #if !defined(HAVE_KRB5_WARNX)
+/**
+ * @brief Log a Kerberos message
+ *
+ * It sends the message to com_err.
+ *
+ * @param[in]  context  The library context
+ *
+ * @param[in]  fmt      The message format
+ *
+ * @param[in]  ...      The message arguments
+ *
+ * @return 0 on success.
+ */
 krb5_error_code krb5_warnx(krb5_context context, const char *fmt, ...)
 {
        va_list args;
 
        va_start(args, fmt);
 krb5_error_code krb5_warnx(krb5_context context, const char *fmt, ...)
 {
        va_list args;
 
        va_start(args, fmt);
-       com_err_va("kdb_samba", errno, fmt, args);
+       com_err_va("samba-kdc", errno, fmt, args);
        va_end(args);
 
        return 0;
 }
 #endif
 
        va_end(args);
 
        return 0;
 }
 #endif
 
+/**
+ * @brief Copy a credential cache.
+ *
+ * @param[in]  context  The library context.
+ *
+ * @param[in]  incc     Credential cache to be copied.
+ *
+ * @param[inout] outcc  Copy of credential cache to be filled in.
+ *
+ * @return 0 on success, a Kerberos error code otherwise.
+ */
 krb5_error_code smb_krb5_cc_copy_creds(krb5_context context,
                                       krb5_ccache incc, krb5_ccache outcc)
 {
 #ifdef HAVE_KRB5_CC_COPY_CACHE /* Heimdal */
        return krb5_cc_copy_cache(context, incc, outcc);
 #elif defined(HAVE_KRB5_CC_COPY_CREDS)
 krb5_error_code smb_krb5_cc_copy_creds(krb5_context context,
                                       krb5_ccache incc, krb5_ccache outcc)
 {
 #ifdef HAVE_KRB5_CC_COPY_CACHE /* Heimdal */
        return krb5_cc_copy_cache(context, incc, outcc);
 #elif defined(HAVE_KRB5_CC_COPY_CREDS)
+       krb5_error_code ret;
+       krb5_principal princ = NULL;
+
+       ret = krb5_cc_get_principal(context, incc, &princ);
+       if (ret != 0) {
+               return ret;
+       }
+       ret = krb5_cc_initialize(context, outcc, princ);
+       krb5_free_principal(context, princ);
+       if (ret != 0) {
+               return ret;
+       }
        return krb5_cc_copy_creds(context, incc, outcc);
 #else
 #error UNKNOWN_KRB5_CC_COPY_CACHE_OR_CREDS_FUNCTION
        return krb5_cc_copy_creds(context, incc, outcc);
 #else
 #error UNKNOWN_KRB5_CC_COPY_CACHE_OR_CREDS_FUNCTION
@@ -3147,7 +3185,7 @@ static krb5_error_code ads_krb5_mk_req(krb5_context context,
                        if (retval != 0) {
                                DBG_WARNING("krb5_auth_con_setuseruserkey "
                                            "failed (%s)\n",
                        if (retval != 0) {
                                DBG_WARNING("krb5_auth_con_setuseruserkey "
                                            "failed (%s)\n",
-                                           error_message(retval)));
+                                           error_message(retval));
                                goto cleanup_creds;
                        }
 
                                goto cleanup_creds;
                        }
 
@@ -3157,7 +3195,7 @@ static krb5_error_code ads_krb5_mk_req(krb5_context context,
                                                        KRB5_AUTH_CONTEXT_USE_SUBKEY);
                        if (retval != 0) {
                                DBG_WARNING("krb5_auth_con_setflags failed (%s)\n",
                                                        KRB5_AUTH_CONTEXT_USE_SUBKEY);
                        if (retval != 0) {
                                DBG_WARNING("krb5_auth_con_setflags failed (%s)\n",
-                                           error_message(retval)));
+                                           error_message(retval));
                                goto cleanup_creds;
                        }
 
                                goto cleanup_creds;
                        }
 
@@ -3189,7 +3227,7 @@ static krb5_error_code ads_krb5_mk_req(krb5_context context,
                                retval = ads_setup_auth_context(context, auth_context);
                                if (retval != 0) {
                                        DBG_WARNING("ads_setup_auth_context failed (%s)\n",
                                retval = ads_setup_auth_context(context, auth_context);
                                if (retval != 0) {
                                        DBG_WARNING("ads_setup_auth_context failed (%s)\n",
-                                                   error_message(retval)));
+                                                   error_message(retval));
                                        goto cleanup_creds;
                                }
                        } else {
                                        goto cleanup_creds;
                                }
                        } else {