File Coverage

blib/lib/PasswordMonkey/Filler.pm
Criterion Covered Total %
statement 43 45 95.5
branch 8 8 100.0
condition n/a
subroutine 10 11 90.9
pod 4 8 50.0
total 65 72 90.2


line stmt bran cond sub pod time code
1             ###########################################
2             package PasswordMonkey::Filler;
3             ###########################################
4 11     11   1153 use strict;
  11         18  
  11         310  
5 11     11   50 use warnings;
  11         16  
  11         256  
6 11     11   52 use Log::Log4perl qw(:easy);
  11         18  
  11         101  
7              
8             PasswordMonkey::make_accessor( __PACKAGE__, $_ ) for qw(
9             name
10             password
11             dealbreakers
12             );
13              
14             ###########################################
15             sub new {
16             ###########################################
17 10     10 0 5474 my($class, %options) = @_;
18              
19 10         81 my $self = {
20             password => undef,
21             bouncers => [],
22             name => $class,
23             dealbreakers => [],
24             %options,
25             };
26              
27 10         74 bless $self, $class;
28             }
29              
30             ###########################################
31             sub prompt {
32             ###########################################
33 0     0 1 0 my($self) = @_;
34              
35 0         0 die "'prompt' needs to be overridden by the plugin class";
36             }
37              
38             ###########################################
39             sub bouncer_add {
40             ###########################################
41 5     5 0 62 my($self, $bouncer) = @_;
42              
43 5         15 push @{ $self->{bouncers} }, $bouncer;
  5         35  
44             }
45              
46             ###########################################
47             sub bouncers {
48             ###########################################
49 16     16 0 51 my($self) = @_;
50              
51 16         47 return @{ $self->{ bouncers } };
  16         109  
52             }
53              
54             ###########################################
55             sub fill {
56             ###########################################
57 13     13 1 72 my($self, $exp, $monkey) = @_;
58              
59 13         283 DEBUG "$self->{name}: Sending password to '", $exp->match, "' prompt";
60              
61 13         1153 my $password = $self->password();
62            
63 13 100       64 if( ref($password) eq "CODE" ) {
64             # We also accept a coderef which we evaluate here.
65 1         10 $password = $password->();
66             }
67              
68             # To be sure the password doesn't end up in the output if
69             # the collecting program forgot to turn echoing off (or we fell
70             # for something that looked like a password prompt but the
71             # driven program isn't collecting at all), check first and if
72             # echo is on, turn it off on the pty slave manually.
73 13         170 my $stty_settings = $exp->slave->stty("-a");
74 13         16094 DEBUG "stty_settings are $stty_settings";
75              
76 13         147 my $echo_is_on = 1;
77 13 100       207 if( $stty_settings =~ /-echo\b/ ) {
78 12         41 $echo_is_on = 0;
79             }
80              
81 13 100       123 if( $echo_is_on ) {
82 1         3 ERROR "Whoa there! Echo on pty slave is on. ",
83             "Turning it off before sending password.";
84 1         8 $exp->slave->stty(qw(-echo));
85             }
86              
87 13         368 DEBUG "Sending password over to slave pty";
88 13         183 $exp->send( $password, "\n" );
89              
90              
91 13 100       4250 if( $echo_is_on ) {
92 1         4 ERROR "Restoring echo on slave pty.";
93              
94             # Just sending over 'echo' here seems to be too early to suppress
95             # echoing the password we just sent.
96             #
97             # Worse, there doesn't seem to be a reliabe way to wait until the
98             # Pty slave won't echo the password we just sent if we turn on
99             # its echo. I've tried sending another '-echo', sending a '-a'
100             # to retrieve status, but none of them makes sure the Pty slave
101             # will have flushed the data and they're failing in unpredictable
102             # ways based on race conditions.
103             #
104             # This is horrible, but I got best results by sleeping a second
105             # before turning the echo back on, so that's what we're stuck
106             # with right now. What a mess.
107 1         1000202 sleep 1;
108              
109 1         37 $exp->slave->stty(qw(echo));
110             }
111              
112 13         923 1;
113             }
114              
115             ###########################################
116             sub pre_fill {
117             ###########################################
118 12     12 1 38 my($self, $exp, $monkey) = @_;
119              
120 12         179 DEBUG "$self->{name}: Prefill callback (base)";
121             }
122              
123             ###########################################
124             sub post_fill {
125             ###########################################
126 12     12 1 36 my($self, $exp, $monkey) = @_;
127              
128 12         104 DEBUG "$self->{name}: Postfill callback (base)";
129             }
130              
131             ###########################################
132             sub init {
133             ###########################################
134 13     13 0 84 my($self) = @_;
135             }
136              
137             1;
138              
139             __END__