2014-07-11 16:02:19 +02:00
|
|
|
|
package Nix::SSH;
|
|
|
|
|
|
2014-11-10 16:03:51 +01:00
|
|
|
|
use utf8;
|
2010-02-03 16:34:52 +01:00
|
|
|
|
use strict;
|
|
|
|
|
use File::Temp qw(tempdir);
|
2014-07-11 16:02:19 +02:00
|
|
|
|
use IPC::Open2;
|
|
|
|
|
|
|
|
|
|
our @ISA = qw(Exporter);
|
|
|
|
|
our @EXPORT = qw(
|
2014-07-24 16:24:57 +02:00
|
|
|
|
@globalSshOpts
|
2014-07-24 12:24:25 +02:00
|
|
|
|
readN readInt readString readStrings
|
|
|
|
|
writeInt writeString writeStrings
|
2014-07-11 16:02:19 +02:00
|
|
|
|
connectToRemoteNix
|
|
|
|
|
);
|
|
|
|
|
|
2010-02-03 16:34:52 +01:00
|
|
|
|
|
2014-07-24 16:24:57 +02:00
|
|
|
|
our @globalSshOpts = split ' ', ($ENV{"NIX_SSHOPTS"} or "");
|
|
|
|
|
|
|
|
|
|
|
2014-07-11 16:02:19 +02:00
|
|
|
|
sub readN {
|
|
|
|
|
my ($bytes, $from) = @_;
|
|
|
|
|
my $res = "";
|
|
|
|
|
while ($bytes > 0) {
|
|
|
|
|
my $s;
|
|
|
|
|
my $n = sysread($from, $s, $bytes);
|
|
|
|
|
die "I/O error reading from remote side\n" if !defined $n;
|
|
|
|
|
die "got EOF while expecting $bytes bytes from remote side\n" if !$n;
|
|
|
|
|
$bytes -= $n;
|
|
|
|
|
$res .= $s;
|
|
|
|
|
}
|
|
|
|
|
return $res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub readInt {
|
|
|
|
|
my ($from) = @_;
|
|
|
|
|
return unpack("L<x4", readN(8, $from));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-07-24 12:24:25 +02:00
|
|
|
|
sub readString {
|
|
|
|
|
my ($from) = @_;
|
|
|
|
|
my $len = readInt($from);
|
|
|
|
|
my $s = readN($len, $from);
|
|
|
|
|
readN(8 - $len % 8, $from) if $len % 8; # skip padding
|
|
|
|
|
return $s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub readStrings {
|
|
|
|
|
my ($from) = @_;
|
|
|
|
|
my $n = readInt($from);
|
|
|
|
|
my @res;
|
|
|
|
|
push @res, readString($from) while $n--;
|
|
|
|
|
return @res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-07-11 16:02:19 +02:00
|
|
|
|
sub writeInt {
|
|
|
|
|
my ($n, $to) = @_;
|
|
|
|
|
syswrite($to, pack("L<x4", $n)) or die;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub writeString {
|
|
|
|
|
my ($s, $to) = @_;
|
|
|
|
|
my $len = length $s;
|
|
|
|
|
my $req .= pack("L<x4", $len);
|
|
|
|
|
$req .= $s;
|
|
|
|
|
$req .= "\000" x (8 - $len % 8) if $len % 8;
|
|
|
|
|
syswrite($to, $req) or die;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub writeStrings {
|
|
|
|
|
my ($ss, $to) = @_;
|
|
|
|
|
writeInt(scalar(@{$ss}), $to);
|
|
|
|
|
writeString($_, $to) foreach @{$ss};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub connectToRemoteNix {
|
2014-07-12 00:09:43 +02:00
|
|
|
|
my ($sshHost, $sshOpts, $extraFlags) = @_;
|
|
|
|
|
|
|
|
|
|
$extraFlags ||= "";
|
2014-07-11 16:02:19 +02:00
|
|
|
|
|
2016-11-26 00:37:43 +01:00
|
|
|
|
# Start ‘nix-store --serve’ on the remote host.
|
2014-07-11 16:02:19 +02:00
|
|
|
|
my ($from, $to);
|
2014-07-12 00:43:28 +02:00
|
|
|
|
# FIXME: don't start a shell, start ssh directly.
|
2014-07-24 16:24:57 +02:00
|
|
|
|
my $pid = open2($from, $to, "exec ssh -x -a $sshHost @globalSshOpts @{$sshOpts} nix-store --serve --write $extraFlags");
|
2014-07-11 16:02:19 +02:00
|
|
|
|
|
|
|
|
|
# Do the handshake.
|
2014-11-10 16:03:51 +01:00
|
|
|
|
my $magic;
|
|
|
|
|
eval {
|
|
|
|
|
my $SERVE_MAGIC_1 = 0x390c9deb; # FIXME
|
|
|
|
|
my $clientVersion = 0x200;
|
|
|
|
|
syswrite($to, pack("L<x4L<x4", $SERVE_MAGIC_1, $clientVersion)) or die;
|
|
|
|
|
$magic = readInt($from);
|
|
|
|
|
};
|
2017-07-30 13:27:57 +02:00
|
|
|
|
die "unable to connect to '$sshHost'\n" if $@;
|
2014-11-10 16:03:51 +01:00
|
|
|
|
die "did not get valid handshake from remote host\n" if $magic != 0x5452eecb;
|
|
|
|
|
|
2014-07-11 16:02:19 +02:00
|
|
|
|
my $serverVersion = readInt($from);
|
|
|
|
|
die "unsupported server version\n" if $serverVersion < 0x200 || $serverVersion >= 0x300;
|
|
|
|
|
|
|
|
|
|
return ($from, $to, $pid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1;
|