blib/lib/Net/SSH/Perl/AuthMgr.pm untested

filter: Statementstmt 0.0% 0 / 122 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 179 SCAR 50.5
CC
12
Cov
0%
auth_failure
42.0
init
20.0
authenticate
12.0
CRAP 156.0
1 package Net::SSH::Perl::AuthMgr;
2 use strict;
3 use warnings;
4  
5 use Carp qw( croak );
6  
7 use Net::SSH::Perl::Agent;
8 use Net::SSH::Perl::Auth;
9 use Net::SSH::Perl::Constants qw(
10 SSH2_MSG_SERVICE_REQUEST
11 SSH2_MSG_SERVICE_ACCEPT
12 SSH2_MSG_EXT_INFO
13 SSH2_MSG_USERAUTH_BANNER
14 SSH2_MSG_USERAUTH_REQUEST
15 SSH2_MSG_USERAUTH_SUCCESS
16 SSH2_MSG_USERAUTH_FAILURE );
17  
18 use Scalar::Util qw(weaken);
19  
20 use vars qw( %AUTH_MAP );
21 %AUTH_MAP = ( password => 'Password',
22 publickey => 'PublicKey',
23 'keyboard-interactive' => 'KeyboardInt',
24 );
25  
26 sub new {
27 my $class = shift;
28 my $ssh = shift;
29 my $amgr = bless { ssh => $ssh }, $class;
30 weaken $amgr->{ssh};
31 $amgr->init(@_);
32 }
33  
34 sub init {
35 my $amgr = shift;
36 my $ssh = $amgr->{ssh};
37 my($packet);
38  
39 $ssh->debug("Sending request for user-authentication service.");
40 $packet = $ssh->packet_start(SSH2_MSG_SERVICE_REQUEST);
41 $packet->put_str("ssh-userauth");
42 $packet->send;
43  
44 $packet = Net::SSH::Perl::Packet->read($ssh);
45 if ($packet->type == SSH2_MSG_EXT_INFO) {
46 $ssh->debug("SSH2_MSG_EXT_INFO received");
47 my $num_ext = $packet->get_int32;
48 while ($num_ext) {
49 my $name = $packet->get_str;
50 my $value = $packet->get_str;
51 $ssh->debug("SSH Extension activated: $name=$value");
52 $ssh->{$name} = $value;
53 $num_ext--;
54 }
55 $packet = Net::SSH::Perl::Packet->read($ssh);
56 }
57 croak "Server denied SSH2_MSG_SERVICE_ACCEPT: ", $packet->type
58 unless $packet->type == SSH2_MSG_SERVICE_ACCEPT;
59 $ssh->debug("Service accepted: " . $packet->get_str . ".");
60  
61 if (!$ssh->config->get('identities_only')) {
62 $amgr->{agent} = Net::SSH::Perl::Agent->new(2);
63 }
64  
65 $amgr->{service} = "ssh-connection";
66  
67 $amgr->send_auth_none;
68  
69 $amgr;
70 }
71  
72 sub agent { $_[0]->{agent} }
73  
74 sub send_auth_none {
75 my $amgr = shift;
76 my $ssh = $amgr->{ssh};
77 $ssh->debug("Trying empty user-authentication request.");
78 my $packet = $ssh->packet_start(SSH2_MSG_USERAUTH_REQUEST);
79 $packet->put_str($ssh->config->get('user'));
80 $packet->put_str("ssh-connection");
81 $packet->put_str("none");
82 $packet->send;
83 }
84  
85 sub authenticate {
86 my $amgr = shift;
87 my $ssh = $amgr->{ssh};
88 my($packet);
89  
90 my $valid = 0;
91 $amgr->{_done} = 0;
92 $amgr->register_handler(SSH2_MSG_USERAUTH_SUCCESS, sub {
93 $valid++;
94 $amgr->{_done}++
95 });
96 $amgr->register_handler(SSH2_MSG_USERAUTH_BANNER, sub {
97 my $amgr = shift;
98 my($packet) = @_;
99 if ($amgr->{ssh}->config->get('interactive')) {
100 print $packet->get_str, "\n";
101 }
102 });
103 $amgr->register_handler(SSH2_MSG_USERAUTH_FAILURE, \&auth_failure);
104 $amgr->register_error(
105 sub { croak "userauth error: bad message during auth" } );
106 $amgr->run( \$amgr->{_done} );
107  
108 $amgr->{agent}->close_socket if $amgr->{agent};
109  
110 $valid;
111 }
112  
113 sub auth_failure {
114 my $amgr = shift;
115 my($packet) = @_;
116 my $ssh = $amgr->{ssh};
117  
118 my $authlist = $packet->get_str;
119 my $partial = $packet->get_int8;
120 $ssh->debug("Authentication methods that can continue: $authlist.");
121  
122 my($found);
123 for my $meth ( split /,/, $authlist ) {
124 $found = 0;
125 next if !exists $AUTH_MAP{$meth};
126 my $auth = $amgr->{_auth_objects}{$meth};
127 unless ($auth) {
128 $auth = $amgr->{_auth_objects}{$meth} =
129 Net::SSH::Perl::Auth->new($AUTH_MAP{$meth}, $ssh);
130 $auth->mgr($amgr);
131 }
132 next unless $auth->enabled;
133 $ssh->debug("Next method to try is $meth.");
134 $found++;
135 if ($auth->authenticate($amgr->{_auth_tried}{$meth}++)) {
136 last;
137 }
138 else {
139 $auth->enabled(0);
140 delete $amgr->{_auth_objects}{$meth};
141 $found = 0;
142 }
143 }
144  
145 $amgr->{_done} = 1 unless $found;
146 }
147  
148 sub register_handler { $_[0]->{__handlers}{$_[1]} = $_[2] }
149 sub remove_handler { delete $_[0]->{__handlers}{$_[1]} }
150 sub register_error { $_[0]->{__error_handler} = $_[1] }
151 sub handler_for { $_[0]->{__handlers}{$_[1]} }
152 sub error_handler { $_[0]->{__error_handler} }
153  
154 sub run {
155 my $amgr = shift;
156 my($end, @args) = @_;
157 until ($$end) {
158 my $packet = Net::SSH::Perl::Packet->read($amgr->{ssh});
159 my $code = $amgr->handler_for($packet->type);
160 unless (defined $code) {
161 $code = $amgr->error_handler ||
162 sub { croak "Protocol error: received type ", $packet->type };
163 }
164 $code->($amgr, $packet, @args);
165 }
166 }
167  
168 1;
169 __END__