aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/users.cc
diff options
context:
space:
mode:
authorjade <lix@jade.fyi>2024-05-30 02:33:05 +0000
committerGerrit Code Review <gerrit@lix-systems>2024-05-30 02:33:05 +0000
commit562ff516ab27b8e98490646dd30ac96e7e2c36bb (patch)
tree1798a20b33da0101bd91acf7da26c891f0cda620 /src/libutil/users.cc
parentc71f21da3ac4d95ef9a42a26416ccee71639dbd6 (diff)
parenta39ba22ff7112cd3984bbf28d8610d84dd525a0f (diff)
Merge changes from topic "libutil-split" into main
* changes: util.hh: Delete remaining file and clean up headers util.hh: Move nativeSystem to local-derivation-goal.cc util.hh: Move stuff to types.hh util.cc: Delete remaining file util.{hh,cc}: Move ignoreException to error.{hh,cc} util.{hh,cc}: Split out namespaces.{hh,cc} util.{hh,cc}: Split out users.{hh,cc} util.{hh,cc}: Split out strings.{hh,cc} util.{hh,cc}: Split out unix-domain-socket.{hh,cc} util.{hh,cc}: Split out child.{hh,cc} util.{hh,cc}: Split out current-process.{hh,cc} util.{hh,cc}: Split out processes.{hh,cc} util.{hh,cc}: Split out file-descriptor.{hh,cc} util.{hh,cc}: Split out file-system.{hh,cc} util.{hh,cc}: Split out terminal.{hh,cc} util.{hh,cc}: Split out environment-variables.{hh,cc}
Diffstat (limited to 'src/libutil/users.cc')
-rw-r--r--src/libutil/users.cc105
1 files changed, 105 insertions, 0 deletions
diff --git a/src/libutil/users.cc b/src/libutil/users.cc
new file mode 100644
index 000000000..a9a8a7353
--- /dev/null
+++ b/src/libutil/users.cc
@@ -0,0 +1,105 @@
+#include "environment-variables.hh"
+#include "file-system.hh"
+#include "logging.hh"
+#include "strings.hh"
+
+#include <pwd.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+namespace nix {
+
+std::string getUserName()
+{
+ auto pw = getpwuid(geteuid());
+ std::string name = pw ? pw->pw_name : getEnv("USER").value_or("");
+ if (name.empty())
+ throw Error("cannot figure out user name");
+ return name;
+}
+
+Path getHomeOf(uid_t userId)
+{
+ std::vector<char> buf(16384);
+ struct passwd pwbuf;
+ struct passwd * pw;
+ if (getpwuid_r(userId, &pwbuf, buf.data(), buf.size(), &pw) != 0
+ || !pw || !pw->pw_dir || !pw->pw_dir[0])
+ throw Error("cannot determine user's home directory");
+ return pw->pw_dir;
+}
+
+Path getHome()
+{
+ static Path homeDir = []()
+ {
+ std::optional<std::string> unownedUserHomeDir = {};
+ auto homeDir = getEnv("HOME");
+ if (homeDir) {
+ // Only use $HOME if doesn't exist or is owned by the current user.
+ struct stat st;
+ int result = stat(homeDir->c_str(), &st);
+ if (result != 0) {
+ if (errno != ENOENT) {
+ warn("couldn't stat $HOME ('%s') for reason other than not existing ('%d'), falling back to the one defined in the 'passwd' file", *homeDir, errno);
+ homeDir.reset();
+ }
+ } else if (st.st_uid != geteuid()) {
+ unownedUserHomeDir.swap(homeDir);
+ }
+ }
+ if (!homeDir) {
+ homeDir = getHomeOf(geteuid());
+ if (unownedUserHomeDir.has_value() && unownedUserHomeDir != homeDir) {
+ warn("$HOME ('%s') is not owned by you, falling back to the one defined in the 'passwd' file ('%s')", *unownedUserHomeDir, *homeDir);
+ }
+ }
+ return *homeDir;
+ }();
+ return homeDir;
+}
+
+
+Path getCacheDir()
+{
+ auto cacheDir = getEnv("XDG_CACHE_HOME");
+ return cacheDir ? *cacheDir : getHome() + "/.cache";
+}
+
+
+Path getConfigDir()
+{
+ auto configDir = getEnv("XDG_CONFIG_HOME");
+ return configDir ? *configDir : getHome() + "/.config";
+}
+
+std::vector<Path> getConfigDirs()
+{
+ Path configHome = getConfigDir();
+ auto configDirs = getEnv("XDG_CONFIG_DIRS").value_or("/etc/xdg");
+ std::vector<Path> result = tokenizeString<std::vector<std::string>>(configDirs, ":");
+ result.insert(result.begin(), configHome);
+ return result;
+}
+
+
+Path getDataDir()
+{
+ auto dataDir = getEnv("XDG_DATA_HOME");
+ return dataDir ? *dataDir : getHome() + "/.local/share";
+}
+
+Path getStateDir()
+{
+ auto stateDir = getEnv("XDG_STATE_HOME");
+ return stateDir ? *stateDir : getHome() + "/.local/state";
+}
+
+Path createNixStateDir()
+{
+ Path dir = getStateDir() + "/nix";
+ createDirs(dir);
+ return dir;
+}
+
+}