2014-07-11 14:02:19 +00:00
|
|
|
|
package Nix::SSH;
|
|
|
|
|
|
2014-11-10 15:03:51 +00:00
|
|
|
|
use utf8;
|
2010-02-03 15:34:52 +00:00
|
|
|
|
use strict;
|
|
|
|
|
use File::Temp qw(tempdir);
|
2014-07-11 14:02:19 +00:00
|
|
|
|
use IPC::Open2;
|
|
|
|
|
|
|
|
|
|
our @ISA = qw(Exporter);
|
|
|
|
|
our @EXPORT = qw(
|
2014-07-24 14:24:57 +00:00
|
|
|
|
@globalSshOpts
|
2014-07-24 10:24:25 +00:00
|
|
|
|
readN readInt readString readStrings
|
|
|
|
|
writeInt writeString writeStrings
|
2014-07-11 14:02:19 +00:00
|
|
|
|
connectToRemoteNix
|
|
|
|
|
);
|
|
|
|
|
|
2010-02-03 15:34:52 +00:00
|
|
|
|
|
2014-07-24 14:24:57 +00:00
|
|
|
|
our @globalSshOpts = split ' ', ($ENV{"NIX_SSHOPTS"} or "");
|
|
|
|
|
|
|
|
|
|
|
2014-07-11 14:02:19 +00: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 10:24:25 +00: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 14:02:19 +00: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-11 22:09:43 +00:00
|
|
|
|
my ($sshHost, $sshOpts, $extraFlags) = @_;
|
|
|
|
|
|
|
|
|
|
$extraFlags ||= "";
|
2014-07-11 14:02:19 +00:00
|
|
|
|
|
2016-11-25 23:37:43 +00:00
|
|
|
|
# Start ‘nix-store --serve’ on the remote host.
|
2014-07-11 14:02:19 +00:00
|
|
|
|
my ($from, $to);
|
2014-07-11 22:43:28 +00:00
|
|
|
|
# FIXME: don't start a shell, start ssh directly.
|
2014-07-24 14:24:57 +00:00
|
|
|
|
my $pid = open2($from, $to, "exec ssh -x -a $sshHost @globalSshOpts @{$sshOpts} nix-store --serve --write $extraFlags");
|
2014-07-11 14:02:19 +00:00
|
|
|
|
|
|
|
|
|
# Do the handshake.
|
2014-11-10 15:03:51 +00: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 11:27:57 +00:00
|
|
|
|
die "unable to connect to '$sshHost'\n" if $@;
|
2014-11-10 15:03:51 +00:00
|
|
|
|
die "did not get valid handshake from remote host\n" if $magic != 0x5452eecb;
|
|
|
|
|
|
2014-07-11 14:02:19 +00:00
|
|
|
|
my $serverVersion = readInt($from);
|
|
|
|
|
die "unsupported server version\n" if $serverVersion < 0x200 || $serverVersion >= 0x300;
|
|
|
|
|
|
|
|
|
|
return ($from, $to, $pid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1;
|