| 1 | package App::FileSync::Transport::SSH; | ||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||
| 3 | # Runs filesync-server on the server host over one ssh connection and speaks | ||||||||||||||||||||||||||||||||||||||||||||
| 4 | # the wire protocol on its pipes. The ssh argument list is built here and | ||||||||||||||||||||||||||||||||||||||||||||
| 5 | # never passed through a local shell, and every value that reaches the remote | ||||||||||||||||||||||||||||||||||||||||||||
| 6 | # shell is restricted to characters that are inert there, so a repo or client | ||||||||||||||||||||||||||||||||||||||||||||
| 7 | # id cannot become a command. | ||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||
| 9 | 327 | ▶ | use App::FileSync::Platform; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||
| 11 | 327 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 12 | 327 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||
| 14 | # Not :all - this class has a close method, and autodie replaces a package's | ||||||||||||||||||||||||||||||||||||||||||||
| 15 | # subs named after the builtins it wraps. | ||||||||||||||||||||||||||||||||||||||||||||
| 16 | 327 | ▶ | use autodie qw(open); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | 327 | ▶ | use File::Temp (); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 19 | 327 | ▶ | use List::Util qw(any); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 20 | 327 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 21 | 327 | ▶ | use Sub::Private { mode => 'enforce' }; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||
| 23 | 327 | ▶ | use App::FileSync qw(validate_client_id validate_repo_name); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 24 | 327 | ▶ | use App::FileSync::Args qw(named); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 25 | 327 | ▶ | use App::FileSync::Constants qw($PROTOCOL_VERSION $ROLE_CLIENT @ROLES $SERVER_PROGRAM); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 26 | 327 | ▶ | use App::FileSync::I18N qw(throw); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 27 | 327 | ▶ | use App::FileSync::IO qw(read_text); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 28 | 327 | ▶ | use App::FileSync::Process; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||
| 30 | 327 | ▶ | use version; our $VERSION = version->declare('0.002.1'); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||
| 32 | # Kept for compatibility: the protocol version sent as --proto. | ||||||||||||||||||||||||||||||||||||||||||||
| 33 | 327 | our $PROTO = $PROTOCOL_VERSION; | |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||
| 35 | # Options every session uses: no prompts, keepalives so a dead link is | ||||||||||||||||||||||||||||||||||||||||||||
| 36 | # noticed, and a bounded connect. | ||||||||||||||||||||||||||||||||||||||||||||
| 37 | 327 | Readonly my @SSH_OPTIONS => ( | |||||||||||||||||||||||||||||||||||||||||||
| 38 | '-T', | ||||||||||||||||||||||||||||||||||||||||||||
| 39 | '-o', 'BatchMode=yes', | ||||||||||||||||||||||||||||||||||||||||||||
| 40 | '-o', 'ServerAliveInterval=30', | ||||||||||||||||||||||||||||||||||||||||||||
| 41 | '-o', 'ConnectTimeout=10', | ||||||||||||||||||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | # Host key policies a repo may choose. | ||||||||||||||||||||||||||||||||||||||||||||
| 45 | 327 | Readonly my @HOST_KEY_CHECKING => qw(yes accept-new); | |||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||
| 47 | # The highest TCP port. | ||||||||||||||||||||||||||||||||||||||||||||
| 48 | 327 | Readonly::Scalar my $MAX_PORT => 65_535; | |||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||
| 50 | # Characters that would end or split an argument. | ||||||||||||||||||||||||||||||||||||||||||||
| 51 | 327 | Readonly my $UNSAFE_ARG => qr/[\0\n]/; | |||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||
| 53 | # user@host, host, or a bracketed IPv6 literal. Both the user and the host | ||||||||||||||||||||||||||||||||||||||||||||
| 54 | # start with a letter or digit, so neither can be read as an ssh option. | ||||||||||||||||||||||||||||||||||||||||||||
| 55 | 327 | Readonly my $SERVER => qr/\A(?:[A-Za-z0-9][A-Za-z0-9._-]*@)? | |||||||||||||||||||||||||||||||||||||||||||
| 56 | (?:[A-Za-z0-9](?:[A-Za-z0-9._-]*[A-Za-z0-9])?|\[[0-9A-Fa-f:]+\])\z/x; | ||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||
| 58 | # A remote program path: only characters inert in a POSIX shell, and not | ||||||||||||||||||||||||||||||||||||||||||||
| 59 | # starting with '-'. | ||||||||||||||||||||||||||||||||||||||||||||
| 60 | 327 | Readonly my $PROGRAM => qr{\A[A-Za-z0-9._/][A-Za-z0-9._/-]*\z}; | |||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||
| 62 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||
| 66 | App::FileSync::Transport::SSH - run the server over ssh | ||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||
| 68 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||
| 70 | my $transport = App::FileSync::Transport::SSH->new( | ||||||||||||||||||||||||||||||||||||||||||||
| 71 | server => 'filesync@files.example.com', repo => 'docs', client => 'laptop', | ||||||||||||||||||||||||||||||||||||||||||||
| 72 | identity => '~/.ssh/filesync_ed25519', | ||||||||||||||||||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||||||||||||||||||
| 74 | ... | ||||||||||||||||||||||||||||||||||||||||||||
| 75 | $transport->close or warn $transport->error_text; | ||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||
| 79 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||
| 81 | Purpose: start C<filesync-server> on the server host for one session. | ||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||
| 83 | Args: C<server> (C<user@host>, C<host> or C<user@[v6]>), C<repo>, C<client>, | ||||||||||||||||||||||||||||||||||||||||||||
| 84 | optional C<role> (default C<client>), C<server_command> (default | ||||||||||||||||||||||||||||||||||||||||||||
| 85 | C<filesync-server>), C<host_key_checking> (C<yes>, the default, or | ||||||||||||||||||||||||||||||||||||||||||||
| 86 | C<accept-new>), C<known_hosts>, C<identity>, C<port>, C<command> (the local | ||||||||||||||||||||||||||||||||||||||||||||
| 87 | ssh program), C<extra_args> (arrayref of extra ssh arguments). | ||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||
| 89 | Returns: the transport. | ||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | Side Effects: runs C<ssh ... SERVER PROGRAM --repo R --client C --role X | ||||||||||||||||||||||||||||||||||||||||||||
| 92 | --proto 2>. ssh's standard error goes to a temporary file, never the | ||||||||||||||||||||||||||||||||||||||||||||
| 93 | protocol pipe, and is available from L</error_text>. On Windows a C<.cmd> or | ||||||||||||||||||||||||||||||||||||||||||||
| 94 | C<.bat> ssh wrapper is started through C<COMSPEC>, since CreateProcess cannot | ||||||||||||||||||||||||||||||||||||||||||||
| 95 | start one directly. | ||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||
| 97 | Usage: | ||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||
| 99 | my $transport = App::FileSync::Transport::SSH->new(%args); | ||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||
| 101 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||
| 103 | my $t = App::FileSync::Transport::SSH->new( | ||||||||||||||||||||||||||||||||||||||||||||
| 104 | server => 'backup', repo => 'photos', client => 'nas', role => 'replica', port => 2222, | ||||||||||||||||||||||||||||||||||||||||||||
| 105 | ); | ||||||||||||||||||||||||||||||||||||||||||||
| 106 | print join(' ', @{ $t->command }), "\n"; | ||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||
| 108 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||
| 110 | =head4 INPUT | ||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||
| 112 | { | ||||||||||||||||||||||||||||||||||||||||||||
| 113 | server => { type => 'string', matches => $SERVER }, | ||||||||||||||||||||||||||||||||||||||||||||
| 114 | repo => { type => 'string', min => 1, max => 64 }, | ||||||||||||||||||||||||||||||||||||||||||||
| 115 | client => { type => 'string', min => 1, max => 64 }, | ||||||||||||||||||||||||||||||||||||||||||||
| 116 | role => { type => 'string', optional => 1, memberof => [ 'client', 'replica' ] }, | ||||||||||||||||||||||||||||||||||||||||||||
| 117 | server_command => { type => 'string', optional => 1, matches => $PROGRAM }, | ||||||||||||||||||||||||||||||||||||||||||||
| 118 | host_key_checking => { type => 'string', optional => 1, memberof => [ 'yes', 'accept-new' ] }, | ||||||||||||||||||||||||||||||||||||||||||||
| 119 | known_hosts => { type => 'string', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||
| 120 | identity => { type => 'string', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||
| 121 | port => { type => 'integer', optional => 1, min => 1, max => 65535 }, | ||||||||||||||||||||||||||||||||||||||||||||
| 122 | command => { type => 'string', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||
| 123 | extra_args => { type => 'arrayref', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||
| 126 | =head4 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||
| 128 | { type => 'object', isa => 'App::FileSync::Transport::SSH' } | ||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||
| 130 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||
| 132 | Key Meaning Resolution | ||||||||||||||||||||||||||||||||||||||||||||
| 133 | transport.missing server, repo or client absent fix the repo config | ||||||||||||||||||||||||||||||||||||||||||||
| 134 | transport.bad_value a value is unsafe or out of range fix the repo config or settings | ||||||||||||||||||||||||||||||||||||||||||||
| 135 | transport.bad_id the repo or client is not a plain id fix the repo config | ||||||||||||||||||||||||||||||||||||||||||||
| 136 | io.write the error file cannot be created check the temp directory | ||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||
| 138 | =head3 FORMAL SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||
| 140 | pre server ∈ SERVER ∧ repo ∈ Id ∧ client ∈ Id ∧ role ∈ ROLES ∧ program ∈ PROGRAM | ||||||||||||||||||||||||||||||||||||||||||||
| 141 | ∧ (port = ⊥ ∨ 0 < port ≤ 65535) ∧ ∀ a : extra_args • a ∈ TEXT ∧ '\n' ∉ a | ||||||||||||||||||||||||||||||||||||||||||||
| 142 | argv = ⟨ssh⟩ ⁀ options ⁀ ⟨server, program, --repo, repo, --client, client, --role, role, --proto, PROTO⟩ | ||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||
| 144 | =head3 PSEUDOCODE | ||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||
| 146 | validate every argument | ||||||||||||||||||||||||||||||||||||||||||||
| 147 | build ssh's options: fixed ones, host key policy, known hosts, identity, | ||||||||||||||||||||||||||||||||||||||||||||
| 148 | port, extra arguments | ||||||||||||||||||||||||||||||||||||||||||||
| 149 | append the server, then the remote command | ||||||||||||||||||||||||||||||||||||||||||||
| 150 | wrap a .cmd/.bat ssh in COMSPEC on Windows | ||||||||||||||||||||||||||||||||||||||||||||
| 151 | start it with standard error going to a temporary file | ||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||
| 153 | =cut | ||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||
| 155 | 10 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=23 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 156 | 10 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||
| 157 | my $args = named(\@_, { | ||||||||||||||||||||||||||||||||||||||||||||
| 158 | 10 | map({ $_ => { type => 'string', optional => 1 } } | |||||||||||||||||||||||||||||||||||||||||||
| 159 | qw(server repo client role server_command host_key_checking known_hosts identity port command)), | ||||||||||||||||||||||||||||||||||||||||||||
| 160 | extra_args => { type => 'arrayref', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||
| 161 | }, 'Transport::SSH->new'); | ||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||
| 163 | 10 | for my $required (qw(server repo client)) { | |||||||||||||||||||||||||||||||||||||||||||
| 164 | throw('transport.missing', { transport => 'SSH', what => $required }) | ||||||||||||||||||||||||||||||||||||||||||||
| 165 | 30 | ▶ | unless defined $args->{$required} && length $args->{$required}; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $args->{$required} and length $args->{$required}
MC/DC0%0/2
defined $args->{$required} and length $args->{$required}
defined $args->{$required}
length $args->{$required}
Truth table33%1/3
A and B
A= defined $args->{$required}B= length $args->{$required}
| |||||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 167 | 10 | _check('server', $args->{server}, valid_server($args->{server})); | |||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||||
| 169 | 10 | for my $check ([ repo => \&validate_repo_name ], [ client => \&validate_client_id ]) { | |||||||||||||||||||||||||||||||||||||||||||
| 170 | 20 | my ($field, $validate) = @{$check}; | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 20 | my ($ok, $reason) = $validate->($args->{$field}); | |||||||||||||||||||||||||||||||||||||||||||
| 172 | 20 | ▶ | throw('transport.bad_id', { reason => $reason }) unless $ok; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||
| 175 | 10 | ▶ | my $role = $args->{role} || $ROLE_CLIENT; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$args->{'role'} || $App::FileSync::Transport::SSH::ROLE_CLIENT
MC/DC0%0/2
$args->{'role'} || $App::FileSync::Transport::SSH::ROLE_CLIENT
$args->{'role'}
$App::FileSync::Transport::SSH::ROLE_CLIENT
Truth table66%2/3
A || B
A= $args->{'role'}B= $App::FileSync::Transport::SSH::ROLE_CLIENT
| |||||||||||||||||||||||||||||||||||||||||||||
| 176 | 10 | ▶ | _check('role', $role, any { $_ eq $role } @ROLES); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||
| 178 | 10 | ▶ | my $program = _given($args->{server_command}) // $SERVER_PROGRAM; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
_given($args->{"server_command"}) // $App::FileSync::Transport::SSH::SERVER_PROGRAM
MC/DC0%0/2
_given($args->{"server_command"}) // $App::FileSync::Transport::SSH::SERVER_PROGRAM
_given($args->{"server_command"})
$App::FileSync::Transport::SSH::SERVER_PROGRAM
Truth table66%2/3
A // B
A= _given($args->{"server_command"})B= $App::FileSync::Transport::SSH::SERVER_PROGRAM
| |||||||||||||||||||||||||||||||||||||||||||||
| 179 | 10 | _check('server_command', $program, valid_program($program)); | |||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||
| 181 | 10 | ▶ | my $checking = _given($args->{host_key_checking}) // $HOST_KEY_CHECKING[0]; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
_given($args->{"host_key_checking"}) // $HOST_KEY_CHECKING[0]
MC/DC0%0/2
_given($args->{"host_key_checking"}) // $HOST_KEY_CHECKING[0]
_given($args->{"host_key_checking"})
$HOST_KEY_CHECKING[0]
Truth table66%2/3
A // B
A= _given($args->{"host_key_checking"})B= $HOST_KEY_CHECKING[0]
| |||||||||||||||||||||||||||||||||||||||||||||
| 182 | 10 | ▶ | _check('host_key_checking', $checking, any { $_ eq $checking } @HOST_KEY_CHECKING); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||
| 184 | 10 | ▶ | my @command = (_given($args->{command}) // 'ssh', @SSH_OPTIONS, '-o', "StrictHostKeyChecking=$checking"); | ||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
_given($args->{"command"}) // "ssh"
MC/DC0%0/1
_given($args->{"command"}) // "ssh"
_given($args->{"command"})
Truth table50%1/2
A
A= _given($args->{"command"})
| |||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||
| 186 | 10 | ▶ | if (defined(my $known = _given($args->{known_hosts}))) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 187 | 2 | _check('known_hosts', $known, $known !~ $UNSAFE_ARG); | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 2 | push @command, '-o', "UserKnownHostsFile=$known"; | |||||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 190 | 10 | ▶ | if (defined(my $identity = _given($args->{identity}))) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 191 | 8 | _check('identity', $identity, $identity !~ $UNSAFE_ARG); | |||||||||||||||||||||||||||||||||||||||||||
| 192 | 8 | push @command, '-i', $identity; | |||||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 194 | 10 | ▶ | if (defined(my $port = _given($args->{port}))) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 195 | 8 | ▶ | _check('port', $port, $port =~ /\A\d{1,5}\z/ && $port > 0 && $port <= $MAX_PORT); | ||||||||||||||||||||||||||||||||||||||||||
|
Condition33%2/6
$port =~ /\A\d{1,5}\z/ && $port > 0 && $port <= $MAX_PORT
$port =~ /\A\d{1,5}\z/ and $port > 0
MC/DC0%0/3
$port =~ /\A\d{1,5}\z/ && $port > 0 && $port <= $MAX_PORT
$port =~ /\A\d{1,5}\z/
$port > 0
$port <= $MAX_PORT
Truth table25%1/4
A and B && C
A= $port =~ /\A\d{1,5}\z/B= $port > 0C= $port <= $MAX_PORT
| |||||||||||||||||||||||||||||||||||||||||||||
| 196 | 8 | push @command, '-p', $port; | |||||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 198 | 10 | ▶ | for my $extra (@{ $args->{extra_args} || [] }) { | ||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args->{'extra_args'} || []
MC/DC100%1/1
$args->{'extra_args'} || []
$args->{'extra_args'}
Truth table100%2/2
A
A= $args->{'extra_args'}
| |||||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | ▶ | _check('extra_args', $extra // '', defined $extra && !ref $extra && $extra !~ $UNSAFE_ARG); | ||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/8
$extra // ""
defined $extra && !ref($extra) && !($extra =~ /$UNSAFE_ARG/)
defined $extra and not ref $extra
MC/DC0%0/1
$extra // ""
$extra
MC/DC0%0/3
defined $extra && !ref($extra) && !($extra =~ /$UNSAFE_ARG/)
defined $extra
not ref $extra
!($extra =~ /$UNSAFE_ARG/)
Truth table0%0/2
A
A= $extra
Truth table0%0/4
A and not(B) && not(C)
A= defined $extraB= not ref $extraC= !($extra =~ /$UNSAFE_ARG/)
| |||||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | push @command, $extra; | |||||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||
| 203 | # The remote shell splits this back into arguments; every value is | ||||||||||||||||||||||||||||||||||||||||||||
| 204 | # restricted (above) to characters that are inert in a POSIX shell. | ||||||||||||||||||||||||||||||||||||||||||||
| 205 | push @command, $args->{server}, $program, | ||||||||||||||||||||||||||||||||||||||||||||
| 206 | 10 | '--repo', $args->{repo}, '--client', $args->{client}, '--role', $role, '--proto', $PROTO; | |||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||
| 208 | 10 | ▶ | if ($^O eq 'MSWin32' && $command[0] =~ /\.(?:cmd|bat)\z/i) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$^O eq "MSWin32" and $command[0] =~ /\.(?:cmd|bat)\z/i
MC/DC0%0/2
$^O eq "MSWin32" and $command[0] =~ /\.(?:cmd|bat)\z/i
$^O eq "MSWin32"
$command[0] =~ /\.(?:cmd|bat)\z/i
Truth table33%1/3
A and B
A= $^O eq "MSWin32"B= $command[0] =~ /\.(?:cmd|bat)\z/i
| |||||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | unshift @command, _given($ENV{COMSPEC}) // 'cmd.exe', '/d', '/c'; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
_given($ENV{"COMSPEC"}) // "cmd.exe"
MC/DC0%0/1
_given($ENV{"COMSPEC"}) // "cmd.exe"
_given($ENV{"COMSPEC"})
Truth table0%0/2
A
A= _given($ENV{"COMSPEC"})
| |||||||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||||
| 212 | 10 | return bless { command => \@command, status => undef, stderr => '', %{ _spawn(\@command) } }, $class; | |||||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||
| 215 | =head2 reader | ||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||
| 217 | Documented with L</exit_status> below. | ||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||
| 219 | =head2 writer | ||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||
| 221 | Documented with L</exit_status> below. | ||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||
| 223 | =head2 command | ||||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||||
| 225 | Documented with L</exit_status> below. | ||||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||||
| 227 | =head2 exit_status | ||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||
| 229 | Purpose: the session's pipes, the argv that was run, and ssh's exit status. | ||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||
| 231 | Args: none. | ||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||
| 233 | Returns: the read and write handles; a copy of the argv (for C<--verbose> and | ||||||||||||||||||||||||||||||||||||||||||||
| 234 | tests); the exit status after L</close> (undef before). | ||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||
| 236 | Side Effects: none. | ||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||
| 238 | Usage: | ||||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||||
| 240 | my $argv = $transport->command; | ||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||
| 242 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||
| 244 | print "ran: @{ $transport->command }\n"; | ||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||
| 246 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||||
| 248 | =head4 INPUT | ||||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||||
| 250 | [ { name => 'self', type => 'object' } ] | ||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||
| 252 | =head4 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||||
| 254 | { type => 'scalar' } | ||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||
| 256 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||
| 258 | None. | ||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||
| 260 | =head3 FORMAL SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||||
| 262 | command = copy(self.command) ; exit_status = self.status | ||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||
| 264 | =cut | ||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||
| 266 | 9 | ▶ | sub reader { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reader: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 267 | 9 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||||
| 269 | 9 | return $self->{reader}; | |||||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||||
| 272 | 9 | ▶ | sub writer { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
writer: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 273 | 9 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||
| 275 | 9 | return $self->{writer}; | |||||||||||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | ▶ | sub command { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
command: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | return [ @{ $self->{command} } ]; | |||||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | ▶ | sub exit_status { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
exit_status: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | return $self->{status}; | |||||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||
| 290 | =head2 close | ||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||
| 292 | Purpose: end the ssh session. | ||||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||||
| 294 | Args: none. | ||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||
| 296 | Returns: 1 when ssh exited 0 by itself, else 0. Repeated calls return 1. | ||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||
| 298 | Side Effects: closes both pipes, reaps ssh, records its exit status and | ||||||||||||||||||||||||||||||||||||||||||||
| 299 | signal, and reads what it wrote to standard error. An ssh that has not | ||||||||||||||||||||||||||||||||||||||||||||
| 300 | exited after C<$App::FileSync::Process::GRACE_SECONDS> is stopped (and C<close> | ||||||||||||||||||||||||||||||||||||||||||||
| 301 | returns 0), so a session whose far end never sees end of file cannot hang | ||||||||||||||||||||||||||||||||||||||||||||
| 302 | the client. | ||||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||||
| 304 | Usage: | ||||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||||
| 306 | $transport->close or warn $transport->error_text, "\n"; | ||||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||||
| 308 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||
| 310 | unless ($transport->close) { | ||||||||||||||||||||||||||||||||||||||||||||
| 311 | printf "ssh exited %d: %s\n", $transport->exit_status, $transport->error_text; | ||||||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||
| 314 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||
| 316 | =head4 INPUT | ||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||
| 318 | [ { name => 'self', type => 'object' } ] | ||||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||||
| 320 | =head4 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||
| 322 | { type => 'integer', memberof => [ 0, 1 ] } | ||||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||
| 324 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||||
| 326 | None. | ||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||
| 328 | =head3 FORMAL SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||
| 330 | close = 1 ⇔ closed ∨ exit(ssh) = 0 | ||||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||||
| 332 | =cut | ||||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||||
| 334 | 10 | ▶ | sub close { ## no critic (Subroutines::ProhibitBuiltinHomonyms, Subroutines::ProhibitAmbiguousNames) -- transport interface | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
close: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 335 | 10 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||||
| 337 | 10 | ▶ | return 1 if $self->{closed}++; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||||
| 339 | 10 | for my $handle (grep { defined } @{$self}{qw(writer reader)}) { | |||||||||||||||||||||||||||||||||||||||||||
| 340 | 20 | CORE::close($handle); | |||||||||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||||
| 343 | 10 | ▶ | if ($self->{pid}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 344 | 10 | my $end = App::FileSync::Process::reap(pid => $self->{pid}); | |||||||||||||||||||||||||||||||||||||||||||
| 345 | 10 | @{$self}{qw(status signal killed pid)} = (@{$end}{qw(status signal killed)}, 0); | |||||||||||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 347 | 10 | $self->{stderr} = _slurp($self->{error_path}); | |||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||||
| 349 | 10 | ▶ | return ($self->{status} || 0) == 0 && !$self->{killed} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
($self->{"status"} || 0) == 0 and not $self->{"killed"}
$self->{'status'} || 0
MC/DC0%0/2
($self->{"status"} || 0) == 0 and not $self->{"killed"}
$self->{'status'}
not $self->{"killed"}
Truth table0%0/3
A and not(B)
A= $self->{'status'}B= not $self->{"killed"}
| |||||||||||||||||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||||
| 352 | =head2 error_text | ||||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||||
| 354 | Purpose: what ssh or the remote shell said, for when the protocol failed. | ||||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||||
| 356 | Args: none. | ||||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||||
| 358 | Returns: the text, trimmed, with blank lines collapsed. | ||||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||||
| 360 | Side Effects: reads the error file if L</close> has not yet. | ||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||
| 362 | Usage: | ||||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||||
| 364 | my $why = $transport->error_text; | ||||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||||
| 366 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||||
| 368 | print $transport->error_text, "\n"; # "Permission denied (publickey)." | ||||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||||
| 370 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||||
| 372 | =head4 INPUT | ||||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||||
| 374 | [ { name => 'self', type => 'object' } ] | ||||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||||
| 376 | =head4 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||||
| 378 | { type => 'string' } | ||||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||||
| 380 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||||
| 382 | None. | ||||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||||
| 384 | =head3 FORMAL SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||||
| 386 | error_text = squeeze(trim(stderr)) | ||||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||||
| 388 | =cut | ||||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||||
| 390 | 2 | ▶ | sub error_text { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
error_text: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 391 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||||
| 393 | 2 | ▶ | my $text = length $self->{stderr} ? $self->{stderr} : _slurp($self->{error_path}); | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 394 | 2 | $text =~ s/\A\s+|\s+\z//g; | |||||||||||||||||||||||||||||||||||||||||||
| 395 | 2 | $text =~ s/\n+/\n/g; | |||||||||||||||||||||||||||||||||||||||||||
| 396 | 2 | return $text; | |||||||||||||||||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||||
| 399 | =head2 valid_server | ||||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||||
| 401 | Documented with L</valid_program> below. | ||||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||||
| 403 | =head2 valid_program | ||||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||||
| 405 | Purpose: check a server address and a remote program path. | ||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||
| 407 | Args: the value. | ||||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||||
| 409 | Returns: 1 or 0. | ||||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||||
| 411 | A server is C<user@host>, C<host> or C<user@[ipv6]>; the user and host must | ||||||||||||||||||||||||||||||||||||||||||||
| 412 | start with a letter or digit, so neither can be read as an ssh option (a | ||||||||||||||||||||||||||||||||||||||||||||
| 413 | leading C<->) and nothing can split into another word. A program is a path of | ||||||||||||||||||||||||||||||||||||||||||||
| 414 | letters, digits, C<.>, C<_>, C</> and C<->, not starting with C<->. | ||||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||||
| 416 | Side Effects: none. | ||||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||||
| 418 | Usage: | ||||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||||
| 420 | die "bad server\n" unless App::FileSync::Transport::SSH::valid_server($server); | ||||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||||
| 422 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||||
| 424 | App::FileSync::Transport::SSH::valid_server('filesync@files.example.com'); # 1 | ||||||||||||||||||||||||||||||||||||||||||||
| 425 | App::FileSync::Transport::SSH::valid_server('-oProxyCommand=sh'); # 0 | ||||||||||||||||||||||||||||||||||||||||||||
| 426 | App::FileSync::Transport::SSH::valid_server('-v@host'); # 0 | ||||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||||
| 428 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||||
| 430 | =head4 INPUT | ||||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||||
| 432 | [ { name => 'value', type => 'string', optional => 1 } ] | ||||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||||
| 434 | =head4 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||||
| 436 | { type => 'integer', memberof => [ 0, 1 ] } | ||||||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||||
| 438 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||||
| 440 | None. | ||||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||||
| 442 | =head3 FORMAL SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||||
| 444 | valid_server(v) ⇔ v ∈ SERVER | ||||||||||||||||||||||||||||||||||||||||||||
| 445 | valid_program(v) ⇔ v ∈ PROGRAM | ||||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||||
| 447 | =cut | ||||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||||
| 449 | 26 | ▶ | sub valid_server { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
valid_server: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 450 | 26 | my ($value) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||||||||
| 452 | 26 | ▶ | return (defined $value && $value =~ $SERVER) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $value and $value =~ /$SERVER/
MC/DC50%1/2
defined $value and $value =~ /$SERVER/
defined $value
$value =~ /$SERVER/
Truth table66%2/3
A and B
A= defined $valueB= $value =~ /$SERVER/
| |||||||||||||||||||||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||||
| 455 | 15 | ▶ | sub valid_program { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
valid_program: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 456 | 15 | my ($value) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||
| 458 | 15 | ▶ | return (defined $value && $value =~ $PROGRAM) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $value and $value =~ /$PROGRAM/
MC/DC100%2/2
defined $value and $value =~ /$PROGRAM/
defined $value
$value =~ /$PROGRAM/
Truth table100%3/3
A and B
A= defined $valueB= $value =~ /$PROGRAM/
| |||||||||||||||||||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||||
| 461 | # _spawn | ||||||||||||||||||||||||||||||||||||||||||||
| 462 | # Purpose: start ssh with stderr going to a temporary file. | ||||||||||||||||||||||||||||||||||||||||||||
| 463 | # Entry: $command is the full argv. | ||||||||||||||||||||||||||||||||||||||||||||
| 464 | # Exit: returns { pid, writer, reader, errors, error_path }. | ||||||||||||||||||||||||||||||||||||||||||||
| 465 | # Side effects: creates the error file (removed when the object goes) and | ||||||||||||||||||||||||||||||||||||||||||||
| 466 | # starts the process. App::FileSync::Process rather than | ||||||||||||||||||||||||||||||||||||||||||||
| 467 | # fork/exec: on Windows fork() is a pseudo-process whose pipe | ||||||||||||||||||||||||||||||||||||||||||||
| 468 | # handles are not always duplicated, and IPC::Open3 alone lets | ||||||||||||||||||||||||||||||||||||||||||||
| 469 | # ssh inherit our end of its stdin, so it never saw end of file. | ||||||||||||||||||||||||||||||||||||||||||||
| 470 | 10 | ▶ | sub _spawn :Private { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_spawn: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||
| 471 | 10 | my ($command) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||||
| 473 | 10 | my $errors = File::Temp->new(UNLINK => 1); | |||||||||||||||||||||||||||||||||||||||||||
| 474 | 10 | my $error_path = $errors->filename; | |||||||||||||||||||||||||||||||||||||||||||
| 475 | 10 | open my $err_fh, '>', $error_path; | |||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||||
| 477 | 10 | my $child = eval { App::FileSync::Process::spawn(command => $command, stderr => fileno($err_fh)) }; | |||||||||||||||||||||||||||||||||||||||||||
| 478 | 10 | my $error = $@; | |||||||||||||||||||||||||||||||||||||||||||
| 479 | 10 | CORE::close($err_fh); | |||||||||||||||||||||||||||||||||||||||||||
| 480 | 10 | ▶ | die $error unless $child; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||||
| 482 | 10 | return { %{$child}, errors => $errors, error_path => $error_path }; | |||||||||||||||||||||||||||||||||||||||||||
| 483 | 327 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||||
| 485 | # _check | ||||||||||||||||||||||||||||||||||||||||||||
| 486 | # Purpose: refuse an unsafe or out-of-range argument. | ||||||||||||||||||||||||||||||||||||||||||||
| 487 | # Entry: $ok is the result of the check. | ||||||||||||||||||||||||||||||||||||||||||||
| 488 | # Exit: returns 1. | ||||||||||||||||||||||||||||||||||||||||||||
| 489 | # Side effects: raises transport.bad_value naming the field. | ||||||||||||||||||||||||||||||||||||||||||||
| 490 | 58 | ▶ | sub _check :Private { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||||
| 491 | 58 | my ($field, $value, $ok) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||||
| 493 | 58 | ▶ | throw('transport.bad_value', { field => $field, value => $value }) unless $ok; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 494 | 58 | return 1; | |||||||||||||||||||||||||||||||||||||||||||
| 495 | 327 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||||
| 497 | # _given | ||||||||||||||||||||||||||||||||||||||||||||
| 498 | # Purpose: treat an empty string like an absent value. | ||||||||||||||||||||||||||||||||||||||||||||
| 499 | # Entry: any scalar. | ||||||||||||||||||||||||||||||||||||||||||||
| 500 | # Exit: returns the value, or undef if undefined or empty. | ||||||||||||||||||||||||||||||||||||||||||||
| 501 | # Side effects: none. | ||||||||||||||||||||||||||||||||||||||||||||
| 502 | 60 | ▶ | sub _given :Private { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_given: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||||
| 503 | 60 | my ($value) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||||
| 505 | 60 | ▶ | return (defined $value && length $value) ? $value : undef; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $value and length $value
MC/DC50%1/2
defined $value and length $value
defined $value
length $value
Truth table66%2/3
A and B
A= defined $valueB= length $value
| |||||||||||||||||||||||||||||||||||||||||||||
| 506 | 327 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||||||||
| 508 | # _slurp | ||||||||||||||||||||||||||||||||||||||||||||
| 509 | # Purpose: the error file's contents. | ||||||||||||||||||||||||||||||||||||||||||||
| 510 | # Entry: $path may be undef or gone. | ||||||||||||||||||||||||||||||||||||||||||||
| 511 | # Exit: returns the text, or ''. | ||||||||||||||||||||||||||||||||||||||||||||
| 512 | # Side effects: reads the file; an unreadable one is treated as empty | ||||||||||||||||||||||||||||||||||||||||||||
| 513 | # (there is nothing better to report). | ||||||||||||||||||||||||||||||||||||||||||||
| 514 | 10 | ▶ | sub _slurp :Private { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_slurp: called CC=4 SCAR=15.6
| |||||||||||||||||||||||||||||||||||||||||||||
| 515 | 10 | my ($path) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||||
| 517 | # Diagnostics only: a file that is missing or unreadable says nothing. | ||||||||||||||||||||||||||||||||||||||||||||
| 518 | 10 | ▶ | my $text = (defined $path && -e $path) ? eval { read_text($path) } : undef; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $path and -e $path
MC/DC0%0/2
defined $path and -e $path
defined $path
-e $path
Truth table33%1/3
A and B
A= defined $pathB= -e $path
| |||||||||||||||||||||||||||||||||||||||||||||
| 519 | 10 | ▶ | return $text // ''; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$text // ""
MC/DC0%0/1
$text // ""
$text
Truth table50%1/2
A
A= $text
| |||||||||||||||||||||||||||||||||||||||||||||
| 520 | 327 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||||||
| 522 | 327 | 1; | |||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||||
| 524 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||