Skip to content

Commit

Permalink
Updated the home lookup in Linux to fallback to pwd if HOME is not se…
Browse files Browse the repository at this point in the history
…t and ignore HOME if root
  • Loading branch information
sago007 committed Sep 21, 2015
1 parent 63627d7 commit dcb0fec
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions sago/platform_folders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ static std::string GetAppDataLocal() {
#else
#include <map>
#include <fstream>
#include <pwd.h>
#include <unistd.h>
#include <sys/types.h>
//Typically Linux. For easy reading the comments will just say Linux but should work with most *nixes

static void throwOnRelative(const char* envName, const char* envValue) {
Expand All @@ -71,11 +74,28 @@ static void throwOnRelative(const char* envName, const char* envValue) {
}
}

/**
* Retrives the effective user's home dir.
* If the user is running as root we ignore the HOME environment. It works badly with sudo.
* Writing to $HOME as root implies security concerns that a multiplatform program cannot be assumed to handle.
* @return The home directory. HOME environment is respected for non-root users if it exists.
*/
static std::string getHome() {
std::string res;
const char* tempRes = getenv("HOME");
int uid = getuid();
const char* homeEnv = getenv("HOME");
if ( uid != 0 && homeEnv) {
//We only acknowlegde HOME if not root.
res = homeEnv;
return res;
}
struct passwd *pw = getpwuid(uid);
if (!pw) {
throw std::runtime_error("Unable to get passwd struct.");
}
const char* tempRes = pw->pw_dir;
if (!tempRes) {
throw std::runtime_error("The environment HOME is not defined");
throw std::runtime_error("User has no home directory");
}
res = tempRes;
return res;
Expand Down

0 comments on commit dcb0fec

Please sign in to comment.