App-FileSync
view release on metacpan or search on metacpan
lib/App/FileSync/Transport/SSH.pm view on Meta::CPAN
=cut
sub new {
my $class = shift;
my $args = named(\@_, {
map({ $_ => { type => 'string', optional => 1 } }
qw(server repo client role server_command host_key_checking known_hosts identity port command)),
extra_args => { type => 'arrayref', optional => 1 },
}, 'Transport::SSH->new');
for my $required (qw(server repo client)) {
throw('transport.missing', { transport => 'SSH', what => $required })
unless defined $args->{$required} && length $args->{$required};
}
_check('server', $args->{server}, valid_server($args->{server}));
for my $check ([ repo => \&validate_repo_name ], [ client => \&validate_client_id ]) {
my ($field, $validate) = @{$check};
my ($ok, $reason) = $validate->($args->{$field});
throw('transport.bad_id', { reason => $reason }) unless $ok;
}
my $role = $args->{role} || $ROLE_CLIENT;
_check('role', $role, any { $_ eq $role } @ROLES);
my $program = _given($args->{server_command}) // $SERVER_PROGRAM;
_check('server_command', $program, valid_program($program));
my $checking = _given($args->{host_key_checking}) // $HOST_KEY_CHECKING[0];
_check('host_key_checking', $checking, any { $_ eq $checking } @HOST_KEY_CHECKING);
my @command = (_given($args->{command}) // 'ssh', @SSH_OPTIONS, '-o', "StrictHostKeyChecking=$checking");
if (defined(my $known = _given($args->{known_hosts}))) {
_check('known_hosts', $known, $known !~ $UNSAFE_ARG);
push @command, '-o', "UserKnownHostsFile=$known";
}
if (defined(my $identity = _given($args->{identity}))) {
_check('identity', $identity, $identity !~ $UNSAFE_ARG);
push @command, '-i', $identity;
}
if (defined(my $port = _given($args->{port}))) {
_check('port', $port, $port =~ /\A\d{1,5}\z/ && $port > 0 && $port <= $MAX_PORT);
push @command, '-p', $port;
}
for my $extra (@{ $args->{extra_args} || [] }) {
_check('extra_args', $extra // '', defined $extra && !ref $extra && $extra !~ $UNSAFE_ARG);
push @command, $extra;
}
# The remote shell splits this back into arguments; every value is
# restricted (above) to characters that are inert in a POSIX shell.
push @command, $args->{server}, $program,
'--repo', $args->{repo}, '--client', $args->{client}, '--role', $role, '--proto', $PROTO;
if ($^O eq 'MSWin32' && $command[0] =~ /\.(?:cmd|bat)\z/i) {
unshift @command, _given($ENV{COMSPEC}) // 'cmd.exe', '/d', '/c';
}
return bless { command => \@command, status => undef, stderr => '', %{ _spawn(\@command) } }, $class;
}
=head2 reader
Documented with L</exit_status> below.
=head2 writer
Documented with L</exit_status> below.
=head2 command
Documented with L</exit_status> below.
=head2 exit_status
Purpose: the session's pipes, the argv that was run, and ssh's exit status.
Args: none.
Returns: the read and write handles; a copy of the argv (for C<--verbose> and
tests); the exit status after L</close> (undef before).
Side Effects: none.
Usage:
my $argv = $transport->command;
=head3 EXAMPLE
print "ran: @{ $transport->command }\n";
=head3 API SPECIFICATION
=head4 INPUT
[ { name => 'self', type => 'object' } ]
=head4 OUTPUT
{ type => 'scalar' }
=head3 MESSAGES
None.
=head3 FORMAL SPECIFICATION
command = copy(self.command) ; exit_status = self.status
=cut
sub reader {
my ($self) = @_;
return $self->{reader};
}
sub writer {
lib/App/FileSync/Transport/SSH.pm view on Meta::CPAN
return $self->{status};
}
=head2 close
Purpose: end the ssh session.
Args: none.
Returns: 1 when ssh exited 0 by itself, else 0. Repeated calls return 1.
Side Effects: closes both pipes, reaps ssh, records its exit status and
signal, and reads what it wrote to standard error. An ssh that has not
exited after C<$App::FileSync::Process::GRACE_SECONDS> is stopped (and C<close>
returns 0), so a session whose far end never sees end of file cannot hang
the client.
Usage:
$transport->close or warn $transport->error_text, "\n";
=head3 EXAMPLE
unless ($transport->close) {
printf "ssh exited %d: %s\n", $transport->exit_status, $transport->error_text;
}
=head3 API SPECIFICATION
=head4 INPUT
[ { name => 'self', type => 'object' } ]
=head4 OUTPUT
{ type => 'integer', memberof => [ 0, 1 ] }
=head3 MESSAGES
None.
=head3 FORMAL SPECIFICATION
close = 1 â closed ⨠exit(ssh) = 0
=cut
sub close { ## no critic (Subroutines::ProhibitBuiltinHomonyms, Subroutines::ProhibitAmbiguousNames) -- transport interface
my ($self) = @_;
return 1 if $self->{closed}++;
for my $handle (grep { defined } @{$self}{qw(writer reader)}) {
CORE::close($handle);
}
if ($self->{pid}) {
my $end = App::FileSync::Process::reap(pid => $self->{pid});
@{$self}{qw(status signal killed pid)} = (@{$end}{qw(status signal killed)}, 0);
}
$self->{stderr} = _slurp($self->{error_path});
return ($self->{status} || 0) == 0 && !$self->{killed} ? 1 : 0;
}
=head2 error_text
Purpose: what ssh or the remote shell said, for when the protocol failed.
Args: none.
Returns: the text, trimmed, with blank lines collapsed.
Side Effects: reads the error file if L</close> has not yet.
Usage:
my $why = $transport->error_text;
=head3 EXAMPLE
print $transport->error_text, "\n"; # "Permission denied (publickey)."
=head3 API SPECIFICATION
=head4 INPUT
[ { name => 'self', type => 'object' } ]
=head4 OUTPUT
{ type => 'string' }
=head3 MESSAGES
None.
=head3 FORMAL SPECIFICATION
error_text = squeeze(trim(stderr))
=cut
sub error_text {
my ($self) = @_;
my $text = length $self->{stderr} ? $self->{stderr} : _slurp($self->{error_path});
$text =~ s/\A\s+|\s+\z//g;
$text =~ s/\n+/\n/g;
return $text;
}
=head2 valid_server
Documented with L</valid_program> below.
=head2 valid_program
Purpose: check a server address and a remote program path.
Args: the value.
Returns: 1 or 0.
A server is C<user@host>, C<host> or C<user@[ipv6]>; the user and host must
start with a letter or digit, so neither can be read as an ssh option (a
leading C<->) and nothing can split into another word. A program is a path of
letters, digits, C<.>, C<_>, C</> and C<->, not starting with C<->.
Side Effects: none.
Usage:
die "bad server\n" unless App::FileSync::Transport::SSH::valid_server($server);
=head3 EXAMPLE
App::FileSync::Transport::SSH::valid_server('filesync@files.example.com'); # 1
App::FileSync::Transport::SSH::valid_server('-oProxyCommand=sh'); # 0
App::FileSync::Transport::SSH::valid_server('-v@host'); # 0
=head3 API SPECIFICATION
=head4 INPUT
[ { name => 'value', type => 'string', optional => 1 } ]
=head4 OUTPUT
{ type => 'integer', memberof => [ 0, 1 ] }
=head3 MESSAGES
None.
=head3 FORMAL SPECIFICATION
valid_server(v) â v â SERVER
valid_program(v) â v â PROGRAM
=cut
sub valid_server {
my ($value) = @_;
return (defined $value && $value =~ $SERVER) ? 1 : 0;
}
sub valid_program {
my ($value) = @_;
return (defined $value && $value =~ $PROGRAM) ? 1 : 0;
}
# _spawn
# Purpose: start ssh with stderr going to a temporary file.
# Entry: $command is the full argv.
# Exit: returns { pid, writer, reader, errors, error_path }.
# Side effects: creates the error file (removed when the object goes) and
# starts the process. App::FileSync::Process rather than
# fork/exec: on Windows fork() is a pseudo-process whose pipe
# handles are not always duplicated, and IPC::Open3 alone lets
# ssh inherit our end of its stdin, so it never saw end of file.
sub _spawn :Private {
my ($command) = @_;
my $errors = File::Temp->new(UNLINK => 1);
my $error_path = $errors->filename;
open my $err_fh, '>', $error_path;
my $child = eval { App::FileSync::Process::spawn(command => $command, stderr => fileno($err_fh)) };
my $error = $@;
CORE::close($err_fh);
die $error unless $child;
return { %{$child}, errors => $errors, error_path => $error_path };
}
# _check
# Purpose: refuse an unsafe or out-of-range argument.
# Entry: $ok is the result of the check.
# Exit: returns 1.
# Side effects: raises transport.bad_value naming the field.
sub _check :Private {
my ($field, $value, $ok) = @_;
throw('transport.bad_value', { field => $field, value => $value }) unless $ok;
return 1;
}
# _given
# Purpose: treat an empty string like an absent value.
# Entry: any scalar.
# Exit: returns the value, or undef if undefined or empty.
# Side effects: none.
sub _given :Private {
my ($value) = @_;
return (defined $value && length $value) ? $value : undef;
}
# _slurp
# Purpose: the error file's contents.
# Entry: $path may be undef or gone.
# Exit: returns the text, or ''.
# Side effects: reads the file; an unreadable one is treated as empty
# (there is nothing better to report).
sub _slurp :Private {
my ($path) = @_;
# Diagnostics only: a file that is missing or unreadable says nothing.
my $text = (defined $path && -e $path) ? eval { read_text($path) } : undef;
return $text // '';
}
1;
__END__
=head1 LIMITATIONS
=over 4
=item * C<extra_args> are passed to ssh unchecked apart from embedded NULs and
newlines: they are the user's own options, and can therefore do anything ssh
options can (C<-o ProxyCommand=...> included). Only trusted configuration
should set them.
=item * C<known_hosts> and C<identity> are not tilde-expanded here; ssh expands
C<~> in C<-i> but not in C<UserKnownHostsFile> on every version.
( run in 1.424 second using v1.01-cache-2.11-cpan-60884a8df76 )