blib/lib/Net/SSH/Perl/Auth/Password.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 118 SCAR 50.5
CC
12
Cov
0%
authenticate
132.0
enabled
6.0
new
2.0
CRAP 156.0
1 package Net::SSH::Perl::Auth::Password;
2  
3 use strict;
4 use warnings;
5  
6 use Net::SSH::Perl::Constants qw(
7 SSH_CMSG_AUTH_PASSWORD
8 SSH_SMSG_SUCCESS
9 SSH_SMSG_FAILURE
10 SSH2_MSG_USERAUTH_REQUEST
11 PROTOCOL_SSH2 );
12  
13 use Net::SSH::Perl::Packet;
14 use Net::SSH::Perl::Util qw( _read_passphrase );
15 use Net::SSH::Perl::Auth;
16 use base qw( Net::SSH::Perl::Auth );
17  
18 use Scalar::Util qw(weaken);
19  
20 sub new {
21 my $class = shift;
22 my $ssh = shift;
23 my $auth = bless { ssh => $ssh }, $class;
24 weaken $auth->{ssh};
25 $auth->enabled( $ssh->config->get('auth_password') );
26 $auth;
27 }
28  
29 sub enabled {
30 my $auth = shift;
31 $auth->{enabled} = shift if @_;
32 $auth->{enabled};
33 }
34  
35 sub authenticate {
36 my $auth = shift;
37 my $try = shift || 0;
38 my($packet);
39  
40 my $ssh = $auth->{ssh};
41 $ssh->debug("Password authentication is disabled by the client."), return
42 unless $auth->enabled;
43  
44 if ($ssh->protocol == PROTOCOL_SSH2 &&
45 $try >= $ssh->config->get('number_of_password_prompts')) {
46 return;
47 }
48  
49 my $pass = $ssh->config->get('pass');
50 $ssh->debug("Trying password authentication.");
51 if (!$pass) {
52 if ($ssh->config->get('interactive')) {
53 my $prompt;
54 my($prompt_host, $prompt_login) = map $ssh->config->get($_),
55 qw( password_prompt_host password_prompt_login );
56 if ($prompt_host && $prompt_login) {
57 $prompt = sprintf "%s@%s's password: ",
58 $ssh->config->get('user'), $ssh->{host};
59 }
60 elsif (!$prompt_host && !$prompt_login) {
61 $prompt = "Password: ";
62 }
63 elsif ($prompt_login) {
64 $prompt = sprintf "%s's password: ", $ssh->config->get('user');
65 }
66 else {
67 $prompt = sprintf "%s password: ", $ssh->{host};
68 }
69 $pass = _read_passphrase($prompt);
70 }
71 else {
72 $ssh->debug("Will not query passphrase in batch mode.");
73 }
74 }
75  
76 if ($ssh->protocol == PROTOCOL_SSH2) {
77 $packet = $ssh->packet_start(SSH2_MSG_USERAUTH_REQUEST);
78 $packet->put_str($ssh->config->get('user'));
79 $packet->put_str("ssh-connection");
80 $packet->put_str("password");
81 $packet->put_int8(0);
82 $packet->put_str($pass);
83 $packet->send;
84 return 1;
85 }
86 else {
87 $packet = $ssh->packet_start(SSH_CMSG_AUTH_PASSWORD);
88 $packet->put_str($pass);
89 $packet->send;
90  
91 $packet = Net::SSH::Perl::Packet->read($ssh);
92 return 1 if $packet->type == SSH_SMSG_SUCCESS;
93  
94 if ($packet->type != SSH_SMSG_FAILURE) {
95 $ssh->fatal_disconnect(sprintf
96 "Protocol error: got %d in response to SSH_CMSG_AUTH_PASSWORD", $packet->type);
97 }
98 }
99  
100 return 0;
101 }
102  
103 1;
104 __END__