blib/lib/Net/SFTP/Foreign/Backend/Net_SSH2.pm

filter: Statementstmt 90.4% 19 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.8% 26 / 28 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Net::SFTP::Foreign::Backend::Net_SSH2;
2  
3 our $VERSION = '0.11';
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Time::HiRes qw(sleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 our @CARP_NOT = qw(Net::SFTP::Foreign);
11  
12 1 ▶use Net::SFTP::Foreign::Helpers;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Net::SFTP::Foreign::Constants qw(SSH2_FX_BAD_MESSAGE
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 SFTP_ERR_REMOTE_BAD_MESSAGE);
15 0 ▶use Net::SSH2;
Subroutine BEGIN: called CC=1 SCAR=2.6
16  
17 my $eagain_error = do {
18 local ($@, $SIG{__DIE__}, $SIG{__WARN__});
19 eval { Net::SSH2::LIBSSH2_ERROR_EAGAIN() };
20 };
21 unless (defined $eagain_error) {
22 $eagain_error = -1;
23 $debug and $debug & 131072 and _debug "The installed version of Net::SSH2 does not support LIBSSH2_ERROR_EGAIN";
24 }
25  
26 sub _new {
27 $debug and
28 _debug "Using Net_SSH2 backend, Net::SSH2 version $Net::SSH2::VERSION compiled against libssh2 "
29 . Net::SSH2->version;
30 my $class = shift;
31 my $self = {};
32 bless $self, $class;
33 }
34  
35 sub _defaults {
36 ( queue_size => 32 )
37 }
38  
39 sub _make_error_string {
40 my ($self, $msg) = @_;
41 my ($err_code, $err_name, $err_str) = $self->{_ssh2}->error;
42 if ($err_code) {
43 return sprintf("%s: %s (%d): %s", $msg, $err_name, $err_code, $err_str)
44 }
45 else {
46 return $msg
47 }
48 }
49  
50 sub _conn_failed {
51 my ($self, $sftp, $msg) = @_;
52 $sftp->_conn_failed($self->_make_error_string($msg))
53 }
54  
55 sub _conn_lost {
56 my ($self, $sftp, $msg) = @_;
57 $sftp->_conn_lost(undef, undef, $self->_make_error_string($msg))
58 }
59  
60 my %auth_arg_map = qw(host hostname
61 user username
62 passphrase password
63 local_user local_username
64 key_path privatekey);
65  
66 sub _init_transport {
67 my ($self, $sftp, $opts) = @_;
68 my $ssh2 = delete $opts->{ssh2};
69 if (defined $ssh2) {
70 $self->{_ssh2} = $ssh2;
71 $debug and $debug & 131072 and $ssh2->debug(1);
72 unless ($ssh2->auth_ok) {
73 $sftp->_conn_failed("Net::SSH2 object is not authenticated");
74 return;
75 }
76 }
77 else {
78 my %auth_args;
79 for (qw(rank username passphrase password publickey privatekey
80 hostname key_path local_user local_username interact
81 cb_keyboard cb_password user host)) {
82 my $map = $auth_arg_map{$_} || $_;
83 next if defined $auth_args{$map};
84 $auth_args{$map} = delete $opts->{$_} if exists $opts->{$_}
85 }
86  
87 if (defined $auth_args{privatekey} and not defined $auth_args{publickey}) {
88 $auth_args{publickey} = "$auth_args{privatekey}.pub";
89 }
90  
91 my $host = $auth_args{hostname};
92 defined $host or croak "sftp target host not defined";
93 my $port = delete $opts->{port} || 22;
94 %$opts and return;
95  
96 unless (defined $auth_args{username}) {
97 local $SIG{__DIE__};
98 $auth_args{username} = eval { scalar getpwuid $< };
99 defined $auth_args{username} or croak "required option 'user' missing";
100 }
101  
102 $ssh2 = $self->{_ssh2} = Net::SSH2->new();
103 $debug and $debug & 131072 and $ssh2->debug(1);
104  
105 unless ($ssh2->connect($host, $port)) {
106 $self->_conn_failed($sftp, "Connection to remote host $host failed");
107 return;
108 }
109  
110 unless ($ssh2->auth(%auth_args)) {
111 $self->_conn_failed($sftp, "Authentication failed");
112 return;
113 }
114 }
115  
116 my $channel = $self->{_channel} = $ssh2->channel;
117 unless (defined $channel) {
118 $self->_conn_failed($sftp, "Unable to create new session channel");
119 return;
120 }
121 $channel->ext_data('ignore');
122 $channel->subsystem('sftp');
123 }
124  
125 sub _sysreadn {
126 my ($self, $sftp, $n) = @_;
127 my $channel = $self->{_channel};
128 my $bin = \$sftp->{_bin};
129 while (1) {
130 my $len = length $$bin;
131 return 1 if $len >= $n;
132 my $buf = '';
133 my $read = $channel->read($buf, $n - $len);
134 unless (defined $read) {
135 $debug and $debug & 32 and _debug("read failed: " . $self->{_ssh2}->error . ", n: $n, len: $len");
136 if ($self->{_ssh2}->error == $eagain_error) {
137 $debug and $debug & 32 and _debug "read error: EAGAIN, delaying before retrying";
138 sleep 0.01;
139 redo;
140 }
141 $self->_conn_lost($sftp, "Read failed");
142 return undef;
143 }
144 $sftp->{_read_total} += $read;
145 if ($debug and $debug & 32) {
146 _debug "$read bytes read from SSH channel, total $sftp->{_read_total}";
147 $debug & 2048 and $read and _hexdump($buf);
148 }
149 $$bin .= $buf;
150 }
151 return $n;
152 }
153  
154 sub _do_io {
155 my ($self, $sftp, $timeout) = @_;
156 my $channel = $self->{_channel};
157 return undef unless $sftp->{_connected};
158  
159 my $bin = \$sftp->{_bin};
160 my $bout = \$sftp->{_bout};
161  
162 while (length $$bout) {
163 my $buf = substr($$bout, 0, 20480);
164 my $written = $channel->write($buf);
165 unless ($written) {
166 if ($self->{_ssh2}->error == Net::SSH2::LIBSSH2_ERROR_EAGAIN()) {
167 $debug and $debug & 32 and _debug "write error: EAGAIN, delaying before retrying";
168 sleep 0.01;
169 redo;
170 }
171 $self->_conn_lost($sftp, "Write failed");
172 return undef;
173 }
174 $sftp->{_written_total} += $written;
175 if ($debug and $debug & 32) {
176 _debug("$written bytes written to SSH channel, total $sftp->{_written_total}");
177 $debug & 2048 and $written and _hexdump($$bout, 0, $written);
178 }
179 substr($$bout, 0, $written, "");
180 }
181  
182 defined $timeout and $timeout <= 0 and return;
183  
184 $self->_sysreadn($sftp, 4) or return undef;
185  
186 my $len = 4 + unpack N => $$bin;
187 if ($len > 256 * 1024) {
188 $sftp->_conn_lost(SSH2_FX_BAD_MESSAGE, SFTP_ERR_REMOTE_BAD_MESSAGE,
189 "Bad remote SFTP message received, len=$len");
190 return undef;
191 }
192 $self->_sysreadn($sftp, $len);
193 }
194  
195 sub _after_init {};
196  
197 sub DESTROY {
198 my $self = shift;
199 local ($@, $!, $?, $SIG{__DIE__});
200 eval {
201 $self->{_channel}->close;
202 undef $self->{_channel};
203 };
204 }
205  
206 1;
207  
208 __END__
↑ Summary