>From a45ecab0fb67a7e0991fac2a31719952162f61fb Mon Sep 17 00:00:00 2001
From: Pavel Reichl <preichl@redhat.com>
Date: Thu, 28 May 2015 16:28:17 -0400
Subject: [PATCH] IPA: Don't override homedir with subdomain_homedir

Resolves:
https://fedorahosted.org/sssd/ticket/2583
---
 src/providers/ipa/ipa_subdomains_id.c | 30 +++++++++++++++++++++++-------
 src/util/util_errors.c                |  1 +
 src/util/util_errors.h                |  1 +
 3 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/src/providers/ipa/ipa_subdomains_id.c b/src/providers/ipa/ipa_subdomains_id.c
index 617c091d3abc4808da4a279213ffc3e1119001bf..9c7a8ccca0bc7419977eb103971893896c864204 100644
--- a/src/providers/ipa/ipa_subdomains_id.c
+++ b/src/providers/ipa/ipa_subdomains_id.c
@@ -683,7 +683,7 @@ ipa_get_ad_id_ctx(struct ipa_id_ctx *ipa_ctx,
 static errno_t
 get_subdomain_homedir_of_user(TALLOC_CTX *mem_ctx, struct sss_domain_info *dom,
                               const char *fqname, uint32_t uid,
-                              const char **_homedir)
+                              const char* original, const char **_homedir)
 {
     errno_t ret;
     const char *name;
@@ -697,12 +697,20 @@ get_subdomain_homedir_of_user(TALLOC_CTX *mem_ctx, struct sss_domain_info *dom,
         goto done;
     }
 
+    if (strstr(dom->subdomain_homedir, "%o") != NULL && original == NULL) {
+        DEBUG(SSSDBG_TRACE_ALL,
+              "Original home directory for user: %s is empty.\n", fqname);
+        ret = ERR_HOMEDIR_IS_NULL;
+        goto done;
+    }
+
     ZERO_STRUCT(homedir_ctx);
 
     homedir_ctx.uid = uid;
     homedir_ctx.domain = dom->name;
     homedir_ctx.flatname = dom->flat_name;
     homedir_ctx.config_homedir_substr = dom->homedir_substr;
+    homedir_ctx.original = original;
     ret = sss_parse_name_const(tmp_ctx, dom->names, fqname,
                                NULL, &name);
     if (ret != EOK) {
@@ -809,6 +817,7 @@ apply_subdomain_homedir(TALLOC_CTX *mem_ctx, struct sss_domain_info *dom,
     errno_t ret;
     uint32_t uid;
     const char *fqname;
+    const char *original;
     const char *homedir = NULL;
     struct ldb_message_element *msg_el = NULL;
     size_t c;
@@ -833,10 +842,6 @@ apply_subdomain_homedir(TALLOC_CTX *mem_ctx, struct sss_domain_info *dom,
         goto done;
     }
 
-    /*
-     * Homedir is always overriden by subdomain_homedir even if it was
-     * explicitly set by user.
-     */
     fqname = ldb_msg_find_attr_as_string(msg, SYSDB_NAME, NULL);
     if (fqname == NULL) {
         DEBUG(SSSDBG_CRIT_FAILURE, "Missing user name.\n");
@@ -852,11 +857,21 @@ apply_subdomain_homedir(TALLOC_CTX *mem_ctx, struct sss_domain_info *dom,
         goto done;
     }
 
-    ret = get_subdomain_homedir_of_user(mem_ctx, dom, fqname, uid, &homedir);
+    original = ldb_msg_find_attr_as_string(msg, SYSDB_HOMEDIR, NULL);
+    if (original == NULL) {
+        DEBUG(SSSDBG_TRACE_ALL, "Missing homedir of %s.\n", fqname);
+    }
+
+    ret = get_subdomain_homedir_of_user(mem_ctx, dom, fqname, uid, original,
+                                        &homedir);
     if (ret != EOK) {
         DEBUG(SSSDBG_OP_FAILURE,
               "get_subdomain_homedir_of_user failed: [%d]: [%s]\n",
-               ret, sss_strerror(ret));
+              ret, sss_strerror(ret));
+        if (ret == ERR_HOMEDIR_IS_NULL) {
+            /* This is not fatal, fallback_homedir will be used. */
+            ret = EOK;
+        }
         goto done;
     }
 
@@ -887,6 +902,7 @@ errno_t get_object_from_cache(TALLOC_CTX *mem_ctx,
                             SYSDB_OBJECTCLASS,
                             SYSDB_UUID,
                             SYSDB_GHOST,
+                            SYSDB_HOMEDIR,
                             NULL };
     char *name;
 
diff --git a/src/util/util_errors.c b/src/util/util_errors.c
index 97c54a5b3352f7982a129ddea3e65f08d0351ea3..f219d9787206ad9860bb83ca6bf09c80d87c302e 100644
--- a/src/util/util_errors.c
+++ b/src/util/util_errors.c
@@ -74,6 +74,7 @@ struct err_string error_to_str[] = {
     { "Malformed cache entry" }, /* ERR_MALFORMED_ENTRY */
     { "Unexpected cache entry type" }, /* ERR_UNEXPECTED_ENTRY_TYPE */
     { "Failed to resolve one of user groups" }, /* ERR_SIMPLE_GROUPS_MISSING */
+    { "Home directory is NULL" }, /* ERR_HOMEDIR_IS_NULL */
     { "ERR_LAST" } /* ERR_LAST */
 };
 
diff --git a/src/util/util_errors.h b/src/util/util_errors.h
index 2dbf2243c753a7c418665bbff71fde469cec806f..74f8e13db839557af71579f7573a1d589eba73bd 100644
--- a/src/util/util_errors.h
+++ b/src/util/util_errors.h
@@ -96,6 +96,7 @@ enum sssd_errors {
     ERR_MALFORMED_ENTRY,
     ERR_UNEXPECTED_ENTRY_TYPE,
     ERR_SIMPLE_GROUPS_MISSING,
+    ERR_HOMEDIR_IS_NULL,
     ERR_LAST            /* ALWAYS LAST */
 };
 
-- 
2.1.0

