aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/ssh.cc
blob: 7c3de4a4827187ed60a8cd3c418c091947d90e6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "ssh.hh"

namespace nix {

std::unique_ptr<SSHMaster::Connection> SSHMaster::startCommand(const std::string & command)
{
    startMaster();

    Pipe in, out;
    in.create();
    out.create();

    auto conn = std::make_unique<Connection>();
    conn->sshPid = startProcess([&]() {
        restoreSignals();

        close(in.writeSide.get());
        close(out.readSide.get());

        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");

        Strings args = { "ssh", host.c_str(), "-x", "-a" };
        if (!keyFile.empty())
            args.insert(args.end(), {"-i", keyFile});
        if (compress)
            args.push_back("-C");
        if (useMaster)
            args.insert(args.end(), {"-S", socketPath});
        args.push_back(command);
        execvp(args.begin()->c_str(), stringsToCharPtrs(args).data());

        throw SysError("executing ‘%s’ on ‘%s’", command, host);
    });


    in.readSide = -1;
    out.writeSide = -1;

    conn->out = std::move(out.readSide);
    conn->in = std::move(in.writeSide);

    return conn;
}

void SSHMaster::startMaster()
{
    if (!useMaster || sshMaster != -1) return;

    tmpDir = std::make_unique<AutoDelete>(createTempDir("", "nix", true, true, 0700));

    socketPath = (Path) *tmpDir + "/ssh.sock";

    Pipe out;
    out.create();

    sshMaster = startProcess([&]() {
        restoreSignals();

        close(out.readSide.get());

        if (dup2(out.writeSide.get(), STDOUT_FILENO) == -1)
            throw SysError("duping over stdout");

        Strings args =
            { "ssh", host.c_str(), "-M", "-N", "-S", socketPath
            , "-o", "LocalCommand=echo started"
            , "-o", "PermitLocalCommand=yes"
            };
        if (!keyFile.empty())
            args.insert(args.end(), {"-i", keyFile});
        if (compress)
            args.push_back("-C");

        execvp(args.begin()->c_str(), stringsToCharPtrs(args).data());

        throw SysError("starting SSH master");
    });

    out.writeSide = -1;

    std::string reply;
    try {
        reply = readLine(out.readSide.get());
    } catch (EndOfFile & e) { }

    if (reply != "started")
        throw Error("failed to start SSH master connection to ‘%s’", host);
}

}