2023-04-07 15:44:34 +00:00
|
|
|
///@file
|
|
|
|
|
2021-01-13 23:05:04 +00:00
|
|
|
#include "command.hh"
|
2006-11-30 19:19:59 +00:00
|
|
|
#include "shared.hh"
|
|
|
|
#include "local-store.hh"
|
2020-07-17 21:24:02 +00:00
|
|
|
#include "remote-store.hh"
|
2023-04-17 17:40:46 +00:00
|
|
|
#include "remote-store-connection.hh"
|
2006-11-30 19:19:59 +00:00
|
|
|
#include "util.hh"
|
2006-11-30 19:54:43 +00:00
|
|
|
#include "serialise.hh"
|
2006-11-30 20:45:20 +00:00
|
|
|
#include "archive.hh"
|
2006-12-03 16:25:19 +00:00
|
|
|
#include "globals.hh"
|
2015-09-03 10:56:59 +00:00
|
|
|
#include "derivations.hh"
|
2017-08-28 12:17:07 +00:00
|
|
|
#include "finally.hh"
|
2021-01-26 11:22:24 +00:00
|
|
|
#include "legacy.hh"
|
2024-03-10 06:36:47 +00:00
|
|
|
#include "signals.hh"
|
2018-09-24 11:53:44 +00:00
|
|
|
#include "daemon.hh"
|
2006-11-30 19:19:59 +00:00
|
|
|
|
2014-07-17 14:57:07 +00:00
|
|
|
#include <algorithm>
|
2019-11-06 15:53:02 +00:00
|
|
|
#include <climits>
|
2010-06-24 17:51:04 +00:00
|
|
|
#include <cstring>
|
2019-11-06 15:53:02 +00:00
|
|
|
|
2006-12-03 03:03:36 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
2006-12-04 17:17:13 +00:00
|
|
|
#include <sys/types.h>
|
2006-12-05 17:21:42 +00:00
|
|
|
#include <sys/wait.h>
|
2006-12-04 17:17:13 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2023-06-06 03:18:24 +00:00
|
|
|
#include <sys/select.h>
|
2006-12-05 17:21:42 +00:00
|
|
|
#include <errno.h>
|
2014-07-17 13:49:33 +00:00
|
|
|
#include <pwd.h>
|
2014-07-17 14:57:07 +00:00
|
|
|
#include <grp.h>
|
2016-09-02 17:30:28 +00:00
|
|
|
#include <fcntl.h>
|
2006-12-02 14:27:24 +00:00
|
|
|
|
2014-10-31 09:08:59 +00:00
|
|
|
#if __APPLE__ || __FreeBSD__
|
|
|
|
#include <sys/ucred.h>
|
|
|
|
#endif
|
|
|
|
|
2006-11-30 19:19:59 +00:00
|
|
|
using namespace nix;
|
2018-09-24 11:53:44 +00:00
|
|
|
using namespace nix::daemon;
|
2006-11-30 19:19:59 +00:00
|
|
|
|
2023-04-07 15:44:34 +00:00
|
|
|
/**
|
|
|
|
* Settings related to authenticating clients for the Nix daemon.
|
|
|
|
*
|
|
|
|
* For pipes we have little good information about the client side, but
|
|
|
|
* for Unix domain sockets we do. So currently these options implemented
|
|
|
|
* mandatory access control based on user names and group names (looked
|
|
|
|
* up and translated to UID/GIDs in the CLI process that runs the code
|
|
|
|
* in this file).
|
|
|
|
*
|
|
|
|
* No code outside of this file knows about these settings (this is not
|
|
|
|
* exposed in a header); all authentication and authorization happens in
|
|
|
|
* `daemon.cc`.
|
|
|
|
*/
|
2023-02-10 13:35:04 +00:00
|
|
|
struct AuthorizationSettings : Config {
|
Move `trustedUsers` and `allowedUsers` to separate config struct
These settings are not needed for libstore at all, they are just used by
the nix daemon *command* for authorization on unix domain sockets. My
moving them to a new configuration struct just in that file, we avoid
them leaking anywhere else.
Also, it is good to break up the mammoth `Settings` struct in general.
Issue #5638 tracks this.
The message is not changed because I do not want to regress in
convenience to the user. Just saying "this connection is not trusted"
doesn't tell them out to fix the issue. The ideal thing to do would be
to somehow parameterize `processCommand` on how the error should be
displayed, so different sorts of connections can display different
information to the user based on how authentication is performed for the
connection in question. This, however, is a good bit more work, so it is
left for the future.
This came up with me thinking about the tcp:// store (#5265). The larger
project is not TCP *per se*, but the idea that it should be possible for
something else to manage access control to services like the Nix Daemon,
and those services simply trust or trust the incoming connection as they
are told. This is a more capability-oriented way of thinking about trust
than "every server implements its own auth separately" as we are used to today.
Its very great that libstore itself already implements just this model,
and so via this refactor I basically want to "enshrine" that so it
continues to be the case.
2023-02-02 18:20:44 +00:00
|
|
|
|
|
|
|
Setting<Strings> trustedUsers{
|
|
|
|
this, {"root"}, "trusted-users",
|
|
|
|
R"(
|
2023-03-05 01:36:26 +00:00
|
|
|
A list of user names, separated by whitespace.
|
2023-06-16 12:33:31 +00:00
|
|
|
These users will have additional rights when connecting to the Nix daemon, such as the ability to specify additional [substituters](#conf-substituters), or to import unsigned [NARs](@docroot@/glossary.md#gloss-nar).
|
2023-03-05 01:36:26 +00:00
|
|
|
|
|
|
|
You can also specify groups by prefixing names with `@`.
|
|
|
|
For instance, `@wheel` means all users in the `wheel` group.
|
Move `trustedUsers` and `allowedUsers` to separate config struct
These settings are not needed for libstore at all, they are just used by
the nix daemon *command* for authorization on unix domain sockets. My
moving them to a new configuration struct just in that file, we avoid
them leaking anywhere else.
Also, it is good to break up the mammoth `Settings` struct in general.
Issue #5638 tracks this.
The message is not changed because I do not want to regress in
convenience to the user. Just saying "this connection is not trusted"
doesn't tell them out to fix the issue. The ideal thing to do would be
to somehow parameterize `processCommand` on how the error should be
displayed, so different sorts of connections can display different
information to the user based on how authentication is performed for the
connection in question. This, however, is a good bit more work, so it is
left for the future.
This came up with me thinking about the tcp:// store (#5265). The larger
project is not TCP *per se*, but the idea that it should be possible for
something else to manage access control to services like the Nix Daemon,
and those services simply trust or trust the incoming connection as they
are told. This is a more capability-oriented way of thinking about trust
than "every server implements its own auth separately" as we are used to today.
Its very great that libstore itself already implements just this model,
and so via this refactor I basically want to "enshrine" that so it
continues to be the case.
2023-02-02 18:20:44 +00:00
|
|
|
|
|
|
|
> **Warning**
|
|
|
|
>
|
2023-03-05 01:36:26 +00:00
|
|
|
> Adding a user to `trusted-users` is essentially equivalent to giving that user root access to the system.
|
2023-06-20 10:23:53 +00:00
|
|
|
> For example, the user can access or replace store path contents that are critical for system security.
|
Move `trustedUsers` and `allowedUsers` to separate config struct
These settings are not needed for libstore at all, they are just used by
the nix daemon *command* for authorization on unix domain sockets. My
moving them to a new configuration struct just in that file, we avoid
them leaking anywhere else.
Also, it is good to break up the mammoth `Settings` struct in general.
Issue #5638 tracks this.
The message is not changed because I do not want to regress in
convenience to the user. Just saying "this connection is not trusted"
doesn't tell them out to fix the issue. The ideal thing to do would be
to somehow parameterize `processCommand` on how the error should be
displayed, so different sorts of connections can display different
information to the user based on how authentication is performed for the
connection in question. This, however, is a good bit more work, so it is
left for the future.
This came up with me thinking about the tcp:// store (#5265). The larger
project is not TCP *per se*, but the idea that it should be possible for
something else to manage access control to services like the Nix Daemon,
and those services simply trust or trust the incoming connection as they
are told. This is a more capability-oriented way of thinking about trust
than "every server implements its own auth separately" as we are used to today.
Its very great that libstore itself already implements just this model,
and so via this refactor I basically want to "enshrine" that so it
continues to be the case.
2023-02-02 18:20:44 +00:00
|
|
|
)"};
|
|
|
|
|
2023-04-07 15:44:34 +00:00
|
|
|
/**
|
|
|
|
* Who we trust to use the daemon in safe ways
|
|
|
|
*/
|
Move `trustedUsers` and `allowedUsers` to separate config struct
These settings are not needed for libstore at all, they are just used by
the nix daemon *command* for authorization on unix domain sockets. My
moving them to a new configuration struct just in that file, we avoid
them leaking anywhere else.
Also, it is good to break up the mammoth `Settings` struct in general.
Issue #5638 tracks this.
The message is not changed because I do not want to regress in
convenience to the user. Just saying "this connection is not trusted"
doesn't tell them out to fix the issue. The ideal thing to do would be
to somehow parameterize `processCommand` on how the error should be
displayed, so different sorts of connections can display different
information to the user based on how authentication is performed for the
connection in question. This, however, is a good bit more work, so it is
left for the future.
This came up with me thinking about the tcp:// store (#5265). The larger
project is not TCP *per se*, but the idea that it should be possible for
something else to manage access control to services like the Nix Daemon,
and those services simply trust or trust the incoming connection as they
are told. This is a more capability-oriented way of thinking about trust
than "every server implements its own auth separately" as we are used to today.
Its very great that libstore itself already implements just this model,
and so via this refactor I basically want to "enshrine" that so it
continues to be the case.
2023-02-02 18:20:44 +00:00
|
|
|
Setting<Strings> allowedUsers{
|
|
|
|
this, {"*"}, "allowed-users",
|
|
|
|
R"(
|
2023-03-05 01:36:26 +00:00
|
|
|
A list user names, separated by whitespace.
|
|
|
|
These users are allowed to connect to the Nix daemon.
|
|
|
|
|
2023-06-16 12:36:53 +00:00
|
|
|
You can specify groups by prefixing names with `@`.
|
|
|
|
For instance, `@wheel` means all users in the `wheel` group.
|
2023-03-05 01:36:26 +00:00
|
|
|
Also, you can allow all users by specifying `*`.
|
Move `trustedUsers` and `allowedUsers` to separate config struct
These settings are not needed for libstore at all, they are just used by
the nix daemon *command* for authorization on unix domain sockets. My
moving them to a new configuration struct just in that file, we avoid
them leaking anywhere else.
Also, it is good to break up the mammoth `Settings` struct in general.
Issue #5638 tracks this.
The message is not changed because I do not want to regress in
convenience to the user. Just saying "this connection is not trusted"
doesn't tell them out to fix the issue. The ideal thing to do would be
to somehow parameterize `processCommand` on how the error should be
displayed, so different sorts of connections can display different
information to the user based on how authentication is performed for the
connection in question. This, however, is a good bit more work, so it is
left for the future.
This came up with me thinking about the tcp:// store (#5265). The larger
project is not TCP *per se*, but the idea that it should be possible for
something else to manage access control to services like the Nix Daemon,
and those services simply trust or trust the incoming connection as they
are told. This is a more capability-oriented way of thinking about trust
than "every server implements its own auth separately" as we are used to today.
Its very great that libstore itself already implements just this model,
and so via this refactor I basically want to "enshrine" that so it
continues to be the case.
2023-02-02 18:20:44 +00:00
|
|
|
|
2023-03-05 01:36:26 +00:00
|
|
|
> **Note**
|
|
|
|
>
|
2023-06-16 12:37:08 +00:00
|
|
|
> Trusted users (set in [`trusted-users`](#conf-trusted-users)) can always connect to the Nix daemon.
|
Move `trustedUsers` and `allowedUsers` to separate config struct
These settings are not needed for libstore at all, they are just used by
the nix daemon *command* for authorization on unix domain sockets. My
moving them to a new configuration struct just in that file, we avoid
them leaking anywhere else.
Also, it is good to break up the mammoth `Settings` struct in general.
Issue #5638 tracks this.
The message is not changed because I do not want to regress in
convenience to the user. Just saying "this connection is not trusted"
doesn't tell them out to fix the issue. The ideal thing to do would be
to somehow parameterize `processCommand` on how the error should be
displayed, so different sorts of connections can display different
information to the user based on how authentication is performed for the
connection in question. This, however, is a good bit more work, so it is
left for the future.
This came up with me thinking about the tcp:// store (#5265). The larger
project is not TCP *per se*, but the idea that it should be possible for
something else to manage access control to services like the Nix Daemon,
and those services simply trust or trust the incoming connection as they
are told. This is a more capability-oriented way of thinking about trust
than "every server implements its own auth separately" as we are used to today.
Its very great that libstore itself already implements just this model,
and so via this refactor I basically want to "enshrine" that so it
continues to be the case.
2023-02-02 18:20:44 +00:00
|
|
|
)"};
|
|
|
|
};
|
|
|
|
|
2023-02-10 13:35:04 +00:00
|
|
|
AuthorizationSettings authorizationSettings;
|
Move `trustedUsers` and `allowedUsers` to separate config struct
These settings are not needed for libstore at all, they are just used by
the nix daemon *command* for authorization on unix domain sockets. My
moving them to a new configuration struct just in that file, we avoid
them leaking anywhere else.
Also, it is good to break up the mammoth `Settings` struct in general.
Issue #5638 tracks this.
The message is not changed because I do not want to regress in
convenience to the user. Just saying "this connection is not trusted"
doesn't tell them out to fix the issue. The ideal thing to do would be
to somehow parameterize `processCommand` on how the error should be
displayed, so different sorts of connections can display different
information to the user based on how authentication is performed for the
connection in question. This, however, is a good bit more work, so it is
left for the future.
This came up with me thinking about the tcp:// store (#5265). The larger
project is not TCP *per se*, but the idea that it should be possible for
something else to manage access control to services like the Nix Daemon,
and those services simply trust or trust the incoming connection as they
are told. This is a more capability-oriented way of thinking about trust
than "every server implements its own auth separately" as we are used to today.
Its very great that libstore itself already implements just this model,
and so via this refactor I basically want to "enshrine" that so it
continues to be the case.
2023-02-02 18:20:44 +00:00
|
|
|
|
2023-02-10 13:35:04 +00:00
|
|
|
static GlobalConfig::Register rSettings(&authorizationSettings);
|
Move `trustedUsers` and `allowedUsers` to separate config struct
These settings are not needed for libstore at all, they are just used by
the nix daemon *command* for authorization on unix domain sockets. My
moving them to a new configuration struct just in that file, we avoid
them leaking anywhere else.
Also, it is good to break up the mammoth `Settings` struct in general.
Issue #5638 tracks this.
The message is not changed because I do not want to regress in
convenience to the user. Just saying "this connection is not trusted"
doesn't tell them out to fix the issue. The ideal thing to do would be
to somehow parameterize `processCommand` on how the error should be
displayed, so different sorts of connections can display different
information to the user based on how authentication is performed for the
connection in question. This, however, is a good bit more work, so it is
left for the future.
This came up with me thinking about the tcp:// store (#5265). The larger
project is not TCP *per se*, but the idea that it should be possible for
something else to manage access control to services like the Nix Daemon,
and those services simply trust or trust the incoming connection as they
are told. This is a more capability-oriented way of thinking about trust
than "every server implements its own auth separately" as we are used to today.
Its very great that libstore itself already implements just this model,
and so via this refactor I basically want to "enshrine" that so it
continues to be the case.
2023-02-02 18:20:44 +00:00
|
|
|
|
2016-09-02 17:30:28 +00:00
|
|
|
#ifndef __linux__
|
|
|
|
#define SPLICE_F_MOVE 0
|
2016-11-17 13:10:12 +00:00
|
|
|
static ssize_t splice(int fd_in, void *off_in, int fd_out, void *off_out, size_t len, unsigned int flags)
|
2016-09-02 17:30:28 +00:00
|
|
|
{
|
2020-06-15 12:12:39 +00:00
|
|
|
// We ignore most parameters, we just have them for conformance with the linux syscall
|
2018-03-01 21:00:58 +00:00
|
|
|
std::vector<char> buf(8192);
|
|
|
|
auto read_count = read(fd_in, buf.data(), buf.size());
|
2016-09-02 17:30:28 +00:00
|
|
|
if (read_count == -1)
|
|
|
|
return read_count;
|
2016-11-17 13:10:12 +00:00
|
|
|
auto write_count = decltype(read_count)(0);
|
2016-09-02 17:30:28 +00:00
|
|
|
while (write_count < read_count) {
|
2018-03-01 21:00:58 +00:00
|
|
|
auto res = write(fd_out, buf.data() + write_count, read_count - write_count);
|
2016-09-02 17:30:28 +00:00
|
|
|
if (res == -1)
|
|
|
|
return res;
|
|
|
|
write_count += res;
|
|
|
|
}
|
|
|
|
return read_count;
|
|
|
|
}
|
|
|
|
#endif
|
2006-11-30 19:19:59 +00:00
|
|
|
|
|
|
|
|
2006-12-05 17:21:42 +00:00
|
|
|
static void sigChldHandler(int sigNo)
|
|
|
|
{
|
2018-03-15 02:39:01 +00:00
|
|
|
// Ensure we don't modify errno of whatever we've interrupted
|
|
|
|
auto saved_errno = errno;
|
2020-06-15 12:12:39 +00:00
|
|
|
// Reap all dead children.
|
2008-11-14 16:50:01 +00:00
|
|
|
while (waitpid(-1, 0, WNOHANG) > 0) ;
|
2018-03-15 02:39:01 +00:00
|
|
|
errno = saved_errno;
|
2006-12-05 17:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void setSigChldAction(bool autoReap)
|
2006-12-04 17:17:13 +00:00
|
|
|
{
|
|
|
|
struct sigaction act, oact;
|
2006-12-05 17:21:42 +00:00
|
|
|
act.sa_handler = autoReap ? sigChldHandler : SIG_DFL;
|
2006-12-04 17:17:13 +00:00
|
|
|
sigfillset(&act.sa_mask);
|
|
|
|
act.sa_flags = 0;
|
|
|
|
if (sigaction(SIGCHLD, &act, &oact))
|
|
|
|
throw SysError("setting SIGCHLD handler");
|
|
|
|
}
|
|
|
|
|
2023-04-07 15:44:34 +00:00
|
|
|
/**
|
|
|
|
* @return Is the given user a member of this group?
|
|
|
|
*
|
|
|
|
* @param user User specified by username.
|
|
|
|
*
|
|
|
|
* @param group Group the user might be a member of.
|
|
|
|
*/
|
|
|
|
static bool matchUser(std::string_view user, const struct group & gr)
|
|
|
|
{
|
|
|
|
for (char * * mem = gr.gr_mem; *mem; mem++)
|
|
|
|
if (user == std::string_view(*mem)) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-12-04 17:17:13 +00:00
|
|
|
|
2023-04-07 15:44:34 +00:00
|
|
|
/**
|
|
|
|
* Does the given user (specified by user name and primary group name)
|
|
|
|
* match the given user/group whitelist?
|
|
|
|
*
|
|
|
|
* If the list allows all users: Yes.
|
|
|
|
*
|
|
|
|
* If the username is in the set: Yes.
|
|
|
|
*
|
|
|
|
* If the groupname is in the set: Yes.
|
|
|
|
*
|
|
|
|
* If the user is in another group which is in the set: yes.
|
|
|
|
*
|
|
|
|
* Otherwise: No.
|
|
|
|
*/
|
|
|
|
static bool matchUser(const std::string & user, const std::string & group, const Strings & users)
|
2014-07-17 14:57:07 +00:00
|
|
|
{
|
|
|
|
if (find(users.begin(), users.end(), "*") != users.end())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (find(users.begin(), users.end(), user) != users.end())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (auto & i : users)
|
2022-02-25 15:00:00 +00:00
|
|
|
if (i.substr(0, 1) == "@") {
|
|
|
|
if (group == i.substr(1)) return true;
|
2014-07-17 14:57:07 +00:00
|
|
|
struct group * gr = getgrnam(i.c_str() + 1);
|
|
|
|
if (!gr) continue;
|
2023-04-07 15:44:34 +00:00
|
|
|
if (matchUser(user, *gr)) return true;
|
2014-07-17 14:57:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-31 09:08:59 +00:00
|
|
|
struct PeerInfo
|
|
|
|
{
|
|
|
|
bool pidKnown;
|
|
|
|
pid_t pid;
|
|
|
|
bool uidKnown;
|
|
|
|
uid_t uid;
|
|
|
|
bool gidKnown;
|
|
|
|
gid_t gid;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-04-07 15:44:34 +00:00
|
|
|
/**
|
|
|
|
* Get the identity of the caller, if possible.
|
|
|
|
*/
|
2014-10-31 09:08:59 +00:00
|
|
|
static PeerInfo getPeerInfo(int remote)
|
|
|
|
{
|
|
|
|
PeerInfo peer = { false, 0, false, 0, false, 0 };
|
|
|
|
|
|
|
|
#if defined(SO_PEERCRED)
|
|
|
|
|
|
|
|
ucred cred;
|
|
|
|
socklen_t credLen = sizeof(cred);
|
|
|
|
if (getsockopt(remote, SOL_SOCKET, SO_PEERCRED, &cred, &credLen) == -1)
|
|
|
|
throw SysError("getting peer credentials");
|
|
|
|
peer = { true, cred.pid, true, cred.uid, true, cred.gid };
|
|
|
|
|
|
|
|
#elif defined(LOCAL_PEERCRED)
|
|
|
|
|
2015-10-04 11:53:23 +00:00
|
|
|
#if !defined(SOL_LOCAL)
|
|
|
|
#define SOL_LOCAL 0
|
|
|
|
#endif
|
|
|
|
|
2014-10-31 09:08:59 +00:00
|
|
|
xucred cred;
|
|
|
|
socklen_t credLen = sizeof(cred);
|
|
|
|
if (getsockopt(remote, SOL_LOCAL, LOCAL_PEERCRED, &cred, &credLen) == -1)
|
|
|
|
throw SysError("getting peer credentials");
|
|
|
|
peer = { false, 0, true, cred.cr_uid, false, 0 };
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return peer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-19 03:01:46 +00:00
|
|
|
#define SD_LISTEN_FDS_START 3
|
|
|
|
|
|
|
|
|
2023-04-07 15:44:34 +00:00
|
|
|
/**
|
|
|
|
* Open a store without a path info cache.
|
|
|
|
*/
|
2018-09-25 10:49:20 +00:00
|
|
|
static ref<Store> openUncachedStore()
|
|
|
|
{
|
|
|
|
Store::Params params; // FIXME: get params from somewhere
|
|
|
|
// Disable caching since the client already does that.
|
|
|
|
params["path-info-cache-size"] = "0";
|
|
|
|
return openStore(settings.storeUri, params);
|
|
|
|
}
|
|
|
|
|
2023-04-07 15:44:34 +00:00
|
|
|
/**
|
|
|
|
* Authenticate a potential client
|
|
|
|
*
|
|
|
|
* @param peer Information about other end of the connection, the client which
|
|
|
|
* wants to communicate with us.
|
|
|
|
*
|
|
|
|
* @return A pair of a `TrustedFlag`, whether the potential client is trusted,
|
|
|
|
* and the name of the user (useful for printing messages).
|
|
|
|
*
|
|
|
|
* If the potential client is not allowed to talk to us, we throw an `Error`.
|
|
|
|
*/
|
|
|
|
static std::pair<TrustedFlag, std::string> authPeer(const PeerInfo & peer)
|
|
|
|
{
|
|
|
|
TrustedFlag trusted = NotTrusted;
|
|
|
|
|
|
|
|
struct passwd * pw = peer.uidKnown ? getpwuid(peer.uid) : 0;
|
|
|
|
std::string user = pw ? pw->pw_name : std::to_string(peer.uid);
|
|
|
|
|
|
|
|
struct group * gr = peer.gidKnown ? getgrgid(peer.gid) : 0;
|
|
|
|
std::string group = gr ? gr->gr_name : std::to_string(peer.gid);
|
|
|
|
|
|
|
|
const Strings & trustedUsers = authorizationSettings.trustedUsers;
|
|
|
|
const Strings & allowedUsers = authorizationSettings.allowedUsers;
|
|
|
|
|
|
|
|
if (matchUser(user, group, trustedUsers))
|
|
|
|
trusted = Trusted;
|
|
|
|
|
|
|
|
if ((!trusted && !matchUser(user, group, allowedUsers)) || group == settings.buildUsersGroup)
|
|
|
|
throw Error("user '%1%' is not allowed to connect to the Nix daemon", user);
|
|
|
|
|
|
|
|
return { trusted, std::move(user) };
|
|
|
|
}
|
|
|
|
|
2018-09-25 10:49:20 +00:00
|
|
|
|
2023-04-07 15:44:34 +00:00
|
|
|
/**
|
|
|
|
* Run a server. The loop opens a socket and accepts new connections from that
|
|
|
|
* socket.
|
2023-04-17 13:41:39 +00:00
|
|
|
*
|
|
|
|
* @param forceTrustClientOpt If present, force trusting or not trusted
|
|
|
|
* the client. Otherwise, decide based on the authentication settings
|
|
|
|
* and user credentials (from the unix domain socket).
|
2023-04-07 15:44:34 +00:00
|
|
|
*/
|
2023-04-17 13:41:39 +00:00
|
|
|
static void daemonLoop(std::optional<TrustedFlag> forceTrustClientOpt)
|
2006-12-04 17:17:13 +00:00
|
|
|
{
|
2014-12-12 16:14:28 +00:00
|
|
|
if (chdir("/") == -1)
|
|
|
|
throw SysError("cannot change current directory");
|
2014-08-13 01:50:44 +00:00
|
|
|
|
2012-06-19 03:01:46 +00:00
|
|
|
AutoCloseFD fdSocket;
|
|
|
|
|
2020-06-15 12:12:39 +00:00
|
|
|
// Handle socket-based activation by systemd.
|
2019-11-22 15:06:44 +00:00
|
|
|
auto listenFds = getEnv("LISTEN_FDS");
|
|
|
|
if (listenFds) {
|
|
|
|
if (getEnv("LISTEN_PID") != std::to_string(getpid()) || listenFds != "1")
|
2012-06-19 03:01:46 +00:00
|
|
|
throw Error("unexpected systemd environment variables");
|
2024-03-18 13:52:04 +00:00
|
|
|
fdSocket = AutoCloseFD{SD_LISTEN_FDS_START};
|
2018-09-25 10:36:11 +00:00
|
|
|
closeOnExec(fdSocket.get());
|
2012-06-19 03:01:46 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 12:12:39 +00:00
|
|
|
// Otherwise, create and bind to a Unix domain socket.
|
2012-06-19 03:01:46 +00:00
|
|
|
else {
|
2018-09-25 10:36:11 +00:00
|
|
|
createDirs(dirOf(settings.nixDaemonSocketFile));
|
|
|
|
fdSocket = createUnixDomainSocket(settings.nixDaemonSocketFile, 0666);
|
2012-06-19 03:01:46 +00:00
|
|
|
}
|
2006-12-04 17:17:13 +00:00
|
|
|
|
2021-10-05 11:23:16 +00:00
|
|
|
// Get rid of children automatically; don't let them become zombies.
|
|
|
|
setSigChldAction(true);
|
|
|
|
|
2020-06-15 12:12:39 +00:00
|
|
|
// Loop accepting connections.
|
2006-12-04 17:17:13 +00:00
|
|
|
while (1) {
|
|
|
|
|
|
|
|
try {
|
2020-06-15 12:12:39 +00:00
|
|
|
// Accept a connection.
|
2006-12-04 17:17:13 +00:00
|
|
|
struct sockaddr_un remoteAddr;
|
|
|
|
socklen_t remoteAddrLen = sizeof(remoteAddr);
|
|
|
|
|
2024-03-18 13:52:04 +00:00
|
|
|
AutoCloseFD remote{accept(fdSocket.get(),
|
|
|
|
(struct sockaddr *) &remoteAddr, &remoteAddrLen)};
|
2006-12-04 17:17:13 +00:00
|
|
|
checkInterrupt();
|
2016-07-11 19:44:44 +00:00
|
|
|
if (!remote) {
|
2014-07-23 17:21:00 +00:00
|
|
|
if (errno == EINTR) continue;
|
|
|
|
throw SysError("accepting connection");
|
2009-09-30 11:32:04 +00:00
|
|
|
}
|
2006-12-04 17:17:13 +00:00
|
|
|
|
2016-07-11 19:44:44 +00:00
|
|
|
closeOnExec(remote.get());
|
2012-07-30 21:13:25 +00:00
|
|
|
|
2023-04-17 13:41:39 +00:00
|
|
|
PeerInfo peer { .pidKnown = false };
|
|
|
|
TrustedFlag trusted;
|
|
|
|
std::string user;
|
|
|
|
|
|
|
|
if (forceTrustClientOpt)
|
|
|
|
trusted = *forceTrustClientOpt;
|
|
|
|
else {
|
|
|
|
peer = getPeerInfo(remote.get());
|
|
|
|
auto [_trusted, _user] = authPeer(peer);
|
|
|
|
trusted = _trusted;
|
|
|
|
user = _user;
|
|
|
|
};
|
2014-07-17 13:49:33 +00:00
|
|
|
|
2023-03-02 14:44:19 +00:00
|
|
|
printInfo((std::string) "accepted connection from pid %1%, user %2%" + (trusted ? " (trusted)" : ""),
|
|
|
|
peer.pidKnown ? std::to_string(peer.pid) : "<unknown>",
|
|
|
|
peer.uidKnown ? user : "<unknown>");
|
2012-07-30 21:13:25 +00:00
|
|
|
|
2020-06-15 12:12:39 +00:00
|
|
|
// Fork a child to handle the connection.
|
2014-12-10 15:35:42 +00:00
|
|
|
ProcessOptions options;
|
|
|
|
options.errorPrefix = "unexpected Nix daemon error: ";
|
|
|
|
options.dieWithParent = false;
|
|
|
|
options.runExitHandlers = true;
|
2014-07-10 14:50:51 +00:00
|
|
|
startProcess([&]() {
|
2024-03-18 13:52:04 +00:00
|
|
|
fdSocket.reset();
|
2014-09-25 16:45:43 +00:00
|
|
|
|
2020-06-15 12:12:39 +00:00
|
|
|
// Background the daemon.
|
2014-07-10 14:50:51 +00:00
|
|
|
if (setsid() == -1)
|
2020-04-21 23:07:07 +00:00
|
|
|
throw SysError("creating a new session");
|
2014-07-10 14:50:51 +00:00
|
|
|
|
2020-06-15 12:12:39 +00:00
|
|
|
// Restore normal handling of SIGCHLD.
|
2014-07-10 14:50:51 +00:00
|
|
|
setSigChldAction(false);
|
|
|
|
|
2020-06-15 12:12:39 +00:00
|
|
|
// For debugging, stuff the pid into argv[1].
|
2021-01-13 23:05:04 +00:00
|
|
|
if (peer.pidKnown && savedArgv[1]) {
|
2022-02-25 15:00:00 +00:00
|
|
|
auto processName = std::to_string(peer.pid);
|
2021-01-13 23:05:04 +00:00
|
|
|
strncpy(savedArgv[1], processName.c_str(), strlen(savedArgv[1]));
|
2014-07-10 14:50:51 +00:00
|
|
|
}
|
2012-07-30 21:13:25 +00:00
|
|
|
|
2020-06-15 12:12:39 +00:00
|
|
|
// Handle the connection.
|
2018-09-24 11:53:44 +00:00
|
|
|
FdSource from(remote.get());
|
|
|
|
FdSink to(remote.get());
|
2023-02-02 17:02:03 +00:00
|
|
|
processConnection(openUncachedStore(), from, to, trusted, NotRecursive);
|
2012-07-30 21:13:25 +00:00
|
|
|
|
2014-11-19 16:09:27 +00:00
|
|
|
exit(0);
|
2014-12-10 15:35:42 +00:00
|
|
|
}, options);
|
2006-12-04 17:17:13 +00:00
|
|
|
|
|
|
|
} catch (Interrupted & e) {
|
2018-04-09 12:05:54 +00:00
|
|
|
return;
|
2020-05-04 22:19:57 +00:00
|
|
|
} catch (Error & error) {
|
2022-12-07 11:58:58 +00:00
|
|
|
auto ei = error.info();
|
2021-01-20 23:27:36 +00:00
|
|
|
// FIXME: add to trace?
|
2024-03-08 07:10:05 +00:00
|
|
|
ei.msg = HintFmt("error processing connection: %1%", ei.msg.str());
|
2020-05-04 22:19:57 +00:00
|
|
|
logError(ei);
|
2006-12-04 17:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-07 15:44:34 +00:00
|
|
|
/**
|
|
|
|
* Forward a standard IO connection to the given remote store.
|
|
|
|
*
|
|
|
|
* We just act as a middleman blindly ferry output between the standard
|
|
|
|
* input/output and the remote store connection, not processing anything.
|
|
|
|
*
|
|
|
|
* Loops until standard input disconnects, or an error is encountered.
|
|
|
|
*/
|
|
|
|
static void forwardStdioConnection(RemoteStore & store) {
|
|
|
|
auto conn = store.openConnectionWrapper();
|
|
|
|
int from = conn->from.fd;
|
|
|
|
int to = conn->to.fd;
|
|
|
|
|
|
|
|
auto nfds = std::max(from, STDIN_FILENO) + 1;
|
|
|
|
while (true) {
|
|
|
|
fd_set fds;
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(from, &fds);
|
|
|
|
FD_SET(STDIN_FILENO, &fds);
|
|
|
|
if (select(nfds, &fds, nullptr, nullptr, nullptr) == -1)
|
|
|
|
throw SysError("waiting for data from client or server");
|
|
|
|
if (FD_ISSET(from, &fds)) {
|
|
|
|
auto res = splice(from, nullptr, STDOUT_FILENO, nullptr, SSIZE_MAX, SPLICE_F_MOVE);
|
|
|
|
if (res == -1)
|
|
|
|
throw SysError("splicing data from daemon socket to stdout");
|
|
|
|
else if (res == 0)
|
|
|
|
throw EndOfFile("unexpected EOF from daemon socket");
|
|
|
|
}
|
|
|
|
if (FD_ISSET(STDIN_FILENO, &fds)) {
|
|
|
|
auto res = splice(STDIN_FILENO, nullptr, to, nullptr, SSIZE_MAX, SPLICE_F_MOVE);
|
|
|
|
if (res == -1)
|
|
|
|
throw SysError("splicing data from stdin to daemon socket");
|
|
|
|
else if (res == 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process a client connecting to us via standard input/output
|
|
|
|
*
|
|
|
|
* Unlike `forwardStdioConnection()` we do process commands ourselves in
|
|
|
|
* this case, not delegating to another daemon.
|
|
|
|
*
|
2023-04-17 13:41:39 +00:00
|
|
|
* @param trustClient Whether to trust the client. Forwarded directly to
|
|
|
|
* `processConnection()`.
|
2023-04-07 15:44:34 +00:00
|
|
|
*/
|
2023-04-17 13:41:39 +00:00
|
|
|
static void processStdioConnection(ref<Store> store, TrustedFlag trustClient)
|
2023-04-07 15:44:34 +00:00
|
|
|
{
|
|
|
|
FdSource from(STDIN_FILENO);
|
|
|
|
FdSink to(STDOUT_FILENO);
|
2023-04-17 13:41:39 +00:00
|
|
|
processConnection(store, from, to, trustClient, NotRecursive);
|
2023-04-07 15:44:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Entry point shared between the new CLI `nix daemon` and old CLI
|
|
|
|
* `nix-daemon`.
|
2023-04-17 13:41:39 +00:00
|
|
|
*
|
|
|
|
* @param forceTrustClientOpt See `daemonLoop()` and the parameter with
|
|
|
|
* the same name over there for details.
|
2023-04-07 15:44:34 +00:00
|
|
|
*/
|
2023-04-17 13:41:39 +00:00
|
|
|
static void runDaemon(bool stdio, std::optional<TrustedFlag> forceTrustClientOpt)
|
2021-01-13 23:05:04 +00:00
|
|
|
{
|
|
|
|
if (stdio) {
|
2023-04-07 15:44:34 +00:00
|
|
|
auto store = openUncachedStore();
|
|
|
|
|
2023-04-17 13:41:39 +00:00
|
|
|
// If --force-untrusted is passed, we cannot forward the connection and
|
|
|
|
// must process it ourselves (before delegating to the next store) to
|
|
|
|
// force untrusting the client.
|
|
|
|
if (auto remoteStore = store.dynamic_pointer_cast<RemoteStore>(); remoteStore && (!forceTrustClientOpt || *forceTrustClientOpt != NotTrusted))
|
2023-04-07 15:44:34 +00:00
|
|
|
forwardStdioConnection(*remoteStore);
|
|
|
|
else
|
2023-04-17 13:41:39 +00:00
|
|
|
// `Trusted` is passed in the auto (no override case) because we
|
|
|
|
// cannot see who is on the other side of a plain pipe. Limiting
|
|
|
|
// access to those is explicitly not `nix-daemon`'s responsibility.
|
|
|
|
processStdioConnection(store, forceTrustClientOpt.value_or(Trusted));
|
2021-01-13 23:05:04 +00:00
|
|
|
} else
|
2023-04-17 13:41:39 +00:00
|
|
|
daemonLoop(forceTrustClientOpt);
|
2021-01-13 23:05:04 +00:00
|
|
|
}
|
2006-12-04 17:17:13 +00:00
|
|
|
|
2020-10-06 11:36:55 +00:00
|
|
|
static int main_nix_daemon(int argc, char * * argv)
|
2006-11-30 19:19:59 +00:00
|
|
|
{
|
2018-10-26 09:35:46 +00:00
|
|
|
{
|
2016-09-02 17:30:28 +00:00
|
|
|
auto stdio = false;
|
2023-04-17 13:41:39 +00:00
|
|
|
std::optional<TrustedFlag> isTrustedOpt = std::nullopt;
|
2016-09-02 17:30:28 +00:00
|
|
|
|
2014-08-13 01:50:44 +00:00
|
|
|
parseCmdLine(argc, argv, [&](Strings::iterator & arg, const Strings::iterator & end) {
|
|
|
|
if (*arg == "--daemon")
|
2020-06-15 12:12:39 +00:00
|
|
|
; // ignored for backwards compatibility
|
2014-08-13 01:50:44 +00:00
|
|
|
else if (*arg == "--help")
|
|
|
|
showManPage("nix-daemon");
|
|
|
|
else if (*arg == "--version")
|
|
|
|
printVersion("nix-daemon");
|
2016-09-02 17:30:28 +00:00
|
|
|
else if (*arg == "--stdio")
|
|
|
|
stdio = true;
|
2023-04-17 13:41:39 +00:00
|
|
|
else if (*arg == "--force-trusted") {
|
|
|
|
experimentalFeatureSettings.require(Xp::DaemonTrustOverride);
|
|
|
|
isTrustedOpt = Trusted;
|
|
|
|
} else if (*arg == "--force-untrusted") {
|
|
|
|
experimentalFeatureSettings.require(Xp::DaemonTrustOverride);
|
|
|
|
isTrustedOpt = NotTrusted;
|
|
|
|
} else if (*arg == "--default-trust") {
|
|
|
|
experimentalFeatureSettings.require(Xp::DaemonTrustOverride);
|
|
|
|
isTrustedOpt = std::nullopt;
|
|
|
|
} else return false;
|
2014-08-13 01:50:44 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2023-04-17 13:41:39 +00:00
|
|
|
runDaemon(stdio, isTrustedOpt);
|
2018-10-26 09:35:46 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2006-11-30 19:19:59 +00:00
|
|
|
}
|
2018-10-26 09:35:46 +00:00
|
|
|
|
2020-10-06 11:36:55 +00:00
|
|
|
static RegisterLegacyCommand r_nix_daemon("nix-daemon", main_nix_daemon);
|
2021-01-13 23:05:04 +00:00
|
|
|
|
|
|
|
struct CmdDaemon : StoreCommand
|
|
|
|
{
|
2023-08-28 13:43:34 +00:00
|
|
|
bool stdio = false;
|
|
|
|
std::optional<TrustedFlag> isTrustedOpt = std::nullopt;
|
|
|
|
|
|
|
|
CmdDaemon()
|
|
|
|
{
|
|
|
|
addFlag({
|
|
|
|
.longName = "stdio",
|
|
|
|
.description = "Attach to standard I/O, instead of trying to bind to a UNIX socket.",
|
|
|
|
.handler = {&stdio, true},
|
|
|
|
});
|
|
|
|
|
|
|
|
addFlag({
|
|
|
|
.longName = "force-trusted",
|
|
|
|
.description = "Force the daemon to trust connecting clients.",
|
|
|
|
.handler = {[&]() {
|
|
|
|
isTrustedOpt = Trusted;
|
|
|
|
}},
|
|
|
|
.experimentalFeature = Xp::DaemonTrustOverride,
|
|
|
|
});
|
|
|
|
|
|
|
|
addFlag({
|
|
|
|
.longName = "force-untrusted",
|
|
|
|
.description = "Force the daemon to not trust connecting clients. The connection will be processed by the receiving daemon before forwarding commands.",
|
|
|
|
.handler = {[&]() {
|
|
|
|
isTrustedOpt = NotTrusted;
|
|
|
|
}},
|
|
|
|
.experimentalFeature = Xp::DaemonTrustOverride,
|
|
|
|
});
|
|
|
|
|
|
|
|
addFlag({
|
|
|
|
.longName = "default-trust",
|
|
|
|
.description = "Use Nix's default trust.",
|
|
|
|
.handler = {[&]() {
|
|
|
|
isTrustedOpt = std::nullopt;
|
|
|
|
}},
|
|
|
|
.experimentalFeature = Xp::DaemonTrustOverride,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-13 23:05:04 +00:00
|
|
|
std::string description() override
|
|
|
|
{
|
|
|
|
return "daemon to perform store operations on behalf of non-root clients";
|
|
|
|
}
|
|
|
|
|
|
|
|
Category category() override { return catUtility; }
|
|
|
|
|
|
|
|
std::string doc() override
|
|
|
|
{
|
|
|
|
return
|
|
|
|
#include "daemon.md"
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
void run(ref<Store> store) override
|
|
|
|
{
|
2023-08-28 13:43:34 +00:00
|
|
|
runDaemon(stdio, isTrustedOpt);
|
2021-01-13 23:05:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static auto rCmdDaemon = registerCommand2<CmdDaemon>({"daemon"});
|