Nix/src/libstore/ssh-store.cc

139 lines
3.6 KiB
C++
Raw Normal View History

2016-09-12 14:03:29 +02:00
#include "store-api.hh"
#include "remote-store.hh"
2016-09-02 20:31:38 +02:00
#include "remote-fs-accessor.hh"
#include "archive.hh"
#include "worker-protocol.hh"
#include "pool.hh"
namespace nix {
2017-02-07 19:20:15 +01:00
static std::string uriScheme = "ssh://";
2016-09-12 14:03:29 +02:00
class SSHStore : public RemoteStore
{
public:
2017-02-07 19:20:15 +01:00
SSHStore(string host, const Params & params, size_t maxConnections = std::numeric_limits<size_t>::max());
2016-09-12 14:03:29 +02:00
std::string getUri() override;
void narFromPath(const Path & path, Sink & sink) override;
ref<FSAccessor> getFSAccessor() override;
private:
struct Connection : RemoteStore::Connection
{
Pid sshPid;
AutoCloseFD out;
AutoCloseFD in;
};
ref<RemoteStore::Connection> openConnection() override;
AutoDelete tmpDir;
Path socketPath;
Pid sshMaster;
2017-02-07 19:20:15 +01:00
string host;
2016-09-12 14:07:50 +02:00
Path key;
bool compress;
2016-09-12 14:03:29 +02:00
};
2017-02-07 19:20:15 +01:00
SSHStore::SSHStore(string host, const Params & params, size_t maxConnections)
2016-09-02 20:31:38 +02:00
: Store(params)
, RemoteStore(params, maxConnections)
, tmpDir(createTempDir("", "nix", true, true, 0700))
, socketPath((Path) tmpDir + "/ssh.sock")
2017-02-07 19:20:15 +01:00
, host(std::move(host))
2016-09-12 14:07:50 +02:00
, key(get(params, "ssh-key", ""))
, compress(get(params, "compress", "") == "true")
2016-09-02 20:31:38 +02:00
{
2016-07-19 00:50:27 +02:00
/* open a connection and perform the handshake to verify all is well */
connections->get();
2016-09-02 20:31:38 +02:00
}
string SSHStore::getUri()
{
2017-02-07 19:20:15 +01:00
return uriScheme + host;
2016-09-02 20:31:38 +02:00
}
class ForwardSource : public Source
{
Source & readSource;
Sink & writeSink;
public:
ForwardSource(Source & readSource, Sink & writeSink) : readSource(readSource), writeSink(writeSink) {}
size_t read(unsigned char * data, size_t len) override
{
auto res = readSource.read(data, len);
writeSink(data, len);
return res;
}
};
void SSHStore::narFromPath(const Path & path, Sink & sink)
{
auto conn(connections->get());
conn->to << wopNarFromPath << path;
conn->processStderr();
ParseSink ps;
auto fwd = ForwardSource(conn->from, sink);
parseDump(ps, fwd);
}
ref<FSAccessor> SSHStore::getFSAccessor()
{
return make_ref<RemoteFSAccessor>(ref<Store>(shared_from_this()));
}
ref<RemoteStore::Connection> SSHStore::openConnection()
{
2016-09-12 14:07:50 +02:00
if ((pid_t) sshMaster == -1) {
sshMaster = startProcess([&]() {
restoreSignals();
2016-09-12 14:07:50 +02:00
if (key.empty())
2017-02-07 19:20:15 +01:00
execlp("ssh", "ssh", "-N", "-M", "-S", socketPath.c_str(), host.c_str(), NULL);
2016-09-12 14:07:50 +02:00
else
2017-02-07 19:20:15 +01:00
execlp("ssh", "ssh", "-N", "-M", "-S", socketPath.c_str(), "-i", key.c_str(), host.c_str(), NULL);
2016-09-12 14:07:50 +02:00
throw SysError("starting ssh master");
});
}
2016-09-02 20:31:38 +02:00
auto conn = make_ref<Connection>();
Pipe in, out;
in.create();
out.create();
conn->sshPid = startProcess([&]() {
if (dup2(in.readSide.get(), STDIN_FILENO) == -1)
throw SysError("duping over STDIN");
if (dup2(out.writeSide.get(), STDOUT_FILENO) == -1)
throw SysError("duping over STDOUT");
2017-02-07 19:20:15 +01:00
execlp("ssh", "ssh", "-S", socketPath.c_str(), host.c_str(), "nix-daemon", "--stdio", NULL);
2016-09-02 20:31:38 +02:00
throw SysError("executing nix-daemon --stdio over ssh");
});
in.readSide = -1;
out.writeSide = -1;
conn->out = std::move(out.readSide);
conn->in = std::move(in.writeSide);
conn->to = FdSink(conn->in.get());
conn->from = FdSource(conn->out.get());
initConnection(*conn);
return conn;
}
static RegisterStoreImplementation regStore([](
const std::string & uri, const Store::Params & params)
-> std::shared_ptr<Store>
{
2017-02-07 19:20:15 +01:00
if (std::string(uri, 0, uriScheme.size()) != uriScheme) return 0;
return std::make_shared<SSHStore>(std::string(uri, uriScheme.size()), params);
2016-09-02 20:31:38 +02:00
});
}