Blob Blame History Raw
From 4e04f5df15242cceaba6c60992cfb2347afee457 Mon Sep 17 00:00:00 2001
Message-Id: <4e04f5df15242cceaba6c60992cfb2347afee457.1377873638.git.jdenemar@redhat.com>
From: Dan Walsh <dwalsh@redhat.com>
Date: Tue, 13 Aug 2013 15:20:39 +0100
Subject: [PATCH] util: add virGetUserDirectoryByUID

For https://bugzilla.redhat.com/show_bug.cgi?id=988491

This function is needed for virt-login-shell.  Also modify virGirUserDirectory
to use the new function, to simplify the code.

Signed-off-by: Eric Blake <eblake@redhat.com>
(cherry picked from commit fbd7682706d52445fbc0f6fa1c4b7965256e733f)
---
 src/libvirt_private.syms |  1 +
 src/util/virutil.c       | 22 ++++++++++++++++++----
 src/util/virutil.h       |  1 +
 3 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 9cfb84c..faa67ad 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2047,6 +2047,7 @@ virGetUnprivSGIOSysfsPath;
 virGetUserCacheDirectory;
 virGetUserConfigDirectory;
 virGetUserDirectory;
+virGetUserDirectoryByUID;
 virGetUserID;
 virGetUserName;
 virGetUserRuntimeDirectory;
diff --git a/src/util/virutil.c b/src/util/virutil.c
index adcdb3c..3abcd53 100644
--- a/src/util/virutil.c
+++ b/src/util/virutil.c
@@ -647,6 +647,14 @@ cleanup:
     return result;
 }
 
+
+char *
+virGetUserDirectory(void)
+{
+    return virGetUserDirectoryByUID(geteuid());
+}
+
+
 #ifdef HAVE_GETPWUID_R
 /* Look up fields from the user database for the given user.  On
  * error, set errno, report the error, and return -1.  */
@@ -750,13 +758,16 @@ static char *virGetGroupEnt(gid_t gid)
     return ret;
 }
 
-char *virGetUserDirectory(void)
+
+char *
+virGetUserDirectoryByUID(uid_t uid)
 {
     char *ret;
-    virGetUserEnt(geteuid(), NULL, NULL, &ret);
+    virGetUserEnt(uid, NULL, NULL, &ret);
     return ret;
 }
 
+
 static char *virGetXDGDirectory(const char *xdgenvname, const char *xdgdefdir)
 {
     const char *path = getenv(xdgenvname);
@@ -1092,8 +1103,11 @@ virGetWin32DirectoryRoot(char **path)
 
 
 char *
-virGetUserDirectory(void)
+virGetUserDirectoryByUID(uid_t uid ATTRIBUTE_UNUSED)
 {
+    /* Since Windows lacks setuid binaries, and since we already fake
+     * geteuid(), we can safely assume that this is only called when
+     * querying about the current user */
     const char *dir;
     char *ret;
 
@@ -1177,7 +1191,7 @@ virGetUserRuntimeDirectory(void)
 
 # else /* !HAVE_GETPWUID_R && !WIN32 */
 char *
-virGetUserDirectory(void)
+virGetUserDirectoryByUID(uid_t uid ATTRIBUTE_UNUSED)
 {
     virReportError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("virGetUserDirectory is not available"));
diff --git a/src/util/virutil.h b/src/util/virutil.h
index 526c610..4b06992 100644
--- a/src/util/virutil.h
+++ b/src/util/virutil.h
@@ -111,6 +111,7 @@ static inline int getgid (void) { return 0; }
 char *virGetHostname(void);
 
 char *virGetUserDirectory(void);
+char *virGetUserDirectoryByUID(uid_t uid);
 char *virGetUserConfigDirectory(void);
 char *virGetUserCacheDirectory(void);
 char *virGetUserRuntimeDirectory(void);
-- 
1.8.3.2