2020-10-11 19:35:19 +03:00
|
|
|
#include "lock.hh"
|
2006-09-05 00:06:23 +03:00
|
|
|
#include "globals.hh"
|
2020-10-11 19:35:19 +03:00
|
|
|
#include "pathlocks.hh"
|
2006-09-05 00:06:23 +03:00
|
|
|
|
2020-10-11 19:35:19 +03:00
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
2003-07-20 22:29:38 +03:00
|
|
|
|
2004-05-11 21:05:44 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2009-01-12 18:30:32 +02:00
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
namespace nix {
|
|
|
|
|
2017-01-25 13:45:38 +02:00
|
|
|
UserLock::UserLock()
|
2005-10-20 19:58:34 +03:00
|
|
|
{
|
2012-07-31 02:55:41 +03:00
|
|
|
assert(settings.buildUsersGroup != "");
|
2020-05-05 13:04:36 +03:00
|
|
|
createDirs(settings.nixStateDir + "/userpool");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UserLock::findFreeUser() {
|
2020-05-14 17:00:54 +03:00
|
|
|
if (enabled()) return true;
|
2006-12-06 22:00:15 +02:00
|
|
|
|
|
|
|
/* Get the members of the build-users-group. */
|
2017-04-13 21:53:23 +03:00
|
|
|
struct group * gr = getgrnam(settings.buildUsersGroup.get().c_str());
|
2006-12-06 22:00:15 +02:00
|
|
|
if (!gr)
|
2020-04-22 02:07:07 +03:00
|
|
|
throw Error("the group '%1%' specified in 'build-users-group' does not exist",
|
|
|
|
settings.buildUsersGroup);
|
2006-12-06 22:00:15 +02:00
|
|
|
gid = gr->gr_gid;
|
|
|
|
|
|
|
|
/* Copy the result of getgrnam. */
|
|
|
|
Strings users;
|
|
|
|
for (char * * p = gr->gr_mem; *p; ++p) {
|
2020-05-12 00:52:15 +03:00
|
|
|
debug("found build user '%1%'", *p);
|
2006-12-06 22:00:15 +02:00
|
|
|
users.push_back(*p);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (users.empty())
|
2020-04-22 02:07:07 +03:00
|
|
|
throw Error("the build users group '%1%' has no members",
|
|
|
|
settings.buildUsersGroup);
|
2005-10-20 19:58:34 +03:00
|
|
|
|
2006-12-06 22:00:15 +02:00
|
|
|
/* Find a user account that isn't currently in use for another
|
|
|
|
build. */
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : users) {
|
2020-05-12 00:52:15 +03:00
|
|
|
debug("trying user '%1%'", i);
|
2005-10-20 19:58:34 +03:00
|
|
|
|
2015-07-17 20:24:28 +03:00
|
|
|
struct passwd * pw = getpwnam(i.c_str());
|
2005-10-20 19:58:34 +03:00
|
|
|
if (!pw)
|
2020-04-22 02:07:07 +03:00
|
|
|
throw Error("the user '%1%' in the group '%2%' does not exist",
|
|
|
|
i, settings.buildUsersGroup);
|
2009-09-23 20:05:51 +03:00
|
|
|
|
2012-07-27 16:59:18 +03:00
|
|
|
|
2012-07-31 02:55:41 +03:00
|
|
|
fnUserLock = (format("%1%/userpool/%2%") % settings.nixStateDir % pw->pw_uid).str();
|
2005-10-20 19:58:34 +03:00
|
|
|
|
2019-12-10 00:57:33 +02:00
|
|
|
AutoCloseFD fd = open(fnUserLock.c_str(), O_RDWR | O_CREAT | O_CLOEXEC, 0600);
|
|
|
|
if (!fd)
|
2020-04-22 02:07:07 +03:00
|
|
|
throw SysError("opening user lock '%1%'", fnUserLock);
|
2005-10-20 19:58:34 +03:00
|
|
|
|
2019-12-10 00:57:33 +02:00
|
|
|
if (lockFile(fd.get(), ltWrite, false)) {
|
|
|
|
fdUserLock = std::move(fd);
|
|
|
|
user = i;
|
|
|
|
uid = pw->pw_uid;
|
2006-12-07 02:19:27 +02:00
|
|
|
|
2019-12-10 00:57:33 +02:00
|
|
|
/* Sanity check... */
|
|
|
|
if (uid == getuid() || uid == geteuid())
|
2020-04-22 02:07:07 +03:00
|
|
|
throw Error("the Nix user should not be a member of '%1%'",
|
|
|
|
settings.buildUsersGroup);
|
2012-07-27 16:59:18 +03:00
|
|
|
|
2015-07-21 15:45:24 +03:00
|
|
|
#if __linux__
|
2019-12-10 00:57:33 +02:00
|
|
|
/* Get the list of supplementary groups of this build user. This
|
|
|
|
is usually either empty or contains a group such as "kvm". */
|
|
|
|
supplementaryGIDs.resize(10);
|
|
|
|
int ngroups = supplementaryGIDs.size();
|
|
|
|
int err = getgrouplist(pw->pw_name, pw->pw_gid,
|
|
|
|
supplementaryGIDs.data(), &ngroups);
|
|
|
|
if (err == -1)
|
2020-04-22 02:07:07 +03:00
|
|
|
throw Error("failed to get list of supplementary groups for '%1%'", pw->pw_name);
|
2019-12-10 00:57:33 +02:00
|
|
|
|
|
|
|
supplementaryGIDs.resize(ngroups);
|
2015-07-21 15:45:24 +03:00
|
|
|
#endif
|
2015-07-01 15:56:34 +03:00
|
|
|
|
2020-05-14 17:00:54 +03:00
|
|
|
isEnabled = true;
|
2020-05-05 13:04:36 +03:00
|
|
|
return true;
|
2005-10-20 19:58:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 13:04:36 +03:00
|
|
|
return false;
|
2005-10-20 19:58:34 +03:00
|
|
|
}
|
|
|
|
|
2006-12-07 18:33:31 +02:00
|
|
|
void UserLock::kill()
|
|
|
|
{
|
2013-11-14 12:57:37 +02:00
|
|
|
killUser(uid);
|
2006-12-07 16:14:35 +02:00
|
|
|
}
|
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
}
|