lib/Rapi/Blog/Util/Term/ReadPassword.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 153 CC 21 SCAR 59.2
CC
21
Cov
0%
read_password
462.0
BEGIN
2.0
BEGIN
2.0
CRAP 373.5
1 package Rapi::Blog::Util::Term::ReadPassword;
2  
3 # This is a copy of Term::ReadPassword v0.11 from CPAN
4 # I am makig this copy because that module by default
5 # requires an interactive test script which is unacceptable
6 # (2017-06-17 by @vanstyn)
7  
8 use strict;
9 use Term::ReadLine;
10 use POSIX qw(:termios_h);
11 my %CC_FIELDS = (
12 VEOF => VEOF,
13 VEOL => VEOL,
14 VERASE => VERASE,
15 VINTR => VINTR,
16 VKILL => VKILL,
17 VQUIT => VQUIT,
18 VSUSP => VSUSP,
19 VSTART => VSTART,
20 VSTOP => VSTOP,
21 VMIN => VMIN,
22 VTIME => VTIME,
23 );
24  
25 use vars qw(
26 $VERSION @ISA @EXPORT @EXPORT_OK
27 $ALLOW_STDIN %SPECIAL $SUPPRESS_NEWLINE $INPUT_LIMIT
28 $USE_STARS $STAR_STRING $UNSTAR_STRING
29 );
30  
31 require Exporter;
32  
33 @ISA = qw(Exporter);
34 @EXPORT = qw(
35 read_password
36 );
37 $VERSION = '0.11';
38  
39 # The special characters in the input stream
40 %SPECIAL = (
41 "\x03" => 'INT', # Control-C, Interrupt
42 "\x15" => 'NAK', # Control-U, NAK (clear buffer)
43 "\x08" => 'DEL', # Backspace
44 "\x7f" => 'DEL', # Delete
45 "\x0d" => 'ENT', # CR, Enter
46 "\x0a" => 'ENT', # LF, Enter
47 );
48  
49 # The maximum amount of data for the input buffer to hold
50 $INPUT_LIMIT = 1000;
51  
52 sub read_password {
53 my($prompt, $idle_limit, $interruptable) = @_;
54 $prompt = '' unless defined $prompt;
55 $idle_limit = 0 unless defined $idle_limit;
56 $interruptable = 0 unless defined $interruptable;
57  
58 # Let's open the TTY (rather than STDIN) if we can
59 local(*TTY, *TTYOUT);
60 my($in, $out) = Term::ReadLine->findConsole;
61 die "No console available" unless $in;
62 if (open TTY, "+<$in") {
63 # Cool
64 } elsif ($ALLOW_STDIN) {
65 open TTY, "<&STDIN"
66 or die "Can't re-open STDIN: $!";
67 } else {
68 die "Can't open '$in' read/write: $!";
69 }
70  
71 # And let's send the output to the TTY as well
72 if (open TTYOUT, ">>$out") {
73 # Cool
74 } elsif ($ALLOW_STDIN) {
75 # Well, let's allow STDOUT as well
76 open TTYOUT, ">>&STDOUT"
77 or die "Can't re-open STDOUT: $!";
78 } else {
79 die "Can't open '$out' for output: $!";
80 }
81  
82 # Don't buffer it!
83 select( (select(TTYOUT), $|=1)[0] );
84 print TTYOUT $prompt;
85  
86 # Okay, now remember where everything was, so we can put it back when
87 # we're done
88 my $fd_tty = fileno(TTY);
89 my $term = POSIX::Termios->new();
90 $term->getattr($fd_tty);
91 my $original_flags = $term->getlflag();
92 my %original_cc;
93 for my $field_name (keys %CC_FIELDS) {
94 $original_cc{$field_name} = $term->getcc($CC_FIELDS{$field_name});
95 }
96  
97 # What makes this setup different from the ordinary?
98 # No keyboard-generated signals, no echoing, no canonical input
99 # processing (like backspace handling)
100 my $flags = $original_flags & ~(ISIG | ECHO | ICANON);
101 $term->setlflag($flags);
102 if ($idle_limit) {
103 # $idle_limit is in seconds, so multiply by ten
104 $term->setcc(VTIME, 10 * $idle_limit);
105 # Continue running the program after that time, even if there
106 # weren't any characters typed
107 $term->setcc(VMIN, 0);
108 } else {
109 # No time limit, but...
110 $term->setcc(VTIME, 0);
111 # Continue as soon as one character has been struck
112 $term->setcc(VMIN, 1);
113 }
114  
115 # Optionally echo stars in place of password characters. The
116 # $unstar_string uses backspace characters.
117 my $star_string = $USE_STARS ? ($STAR_STRING || '*') : '';
118 my $unstar_string = $USE_STARS ? ($UNSTAR_STRING || "\b*\b \b") : '';
119  
120 # If there's anything already buffered, we should throw it out. This
121 # is to discourage users from typing their password before they see
122 # the prompt, since their keystrokes may be echoing on the screen.
123 #
124 # So this statement supposedly makes sure the prompt goes out, the
125 # unread input buffer is discarded, and _then_ the changes take
126 # effect. Thus, everything they typed ahead is (probably) echoed.
127 $term->setattr($fd_tty, TCSAFLUSH);
128  
129 my $input = '';
130 my $return_value;
131 KEYSTROKE:
132 while (1) {
133 my $new_keys = '';
134 my $count = sysread(TTY, $new_keys, 99);
135 # We're here, so either the idle_limit expired, or the user typed
136 # something.
137 if ($count) {
138 for my $new_key (split //, $new_keys) {
139 if (my $meaning = $SPECIAL{$new_key}) {
140 if ($meaning eq 'ENT') {
141 # Enter/return key
142 # Return what we have so far
143 $return_value = $input;
144 last KEYSTROKE;
145 } elsif ($meaning eq 'DEL') {
146 # Delete/backspace key
147 # Take back one char, if possible
148 if (length $input) {
149 $input = substr $input, 0, length($input)-1;
150 print TTYOUT $unstar_string;
151 }
152 } elsif ($meaning eq 'NAK') {
153 # Control-U (NAK)
154 # Clear what we have read so far
155 for (1..length $input) {
156 print TTYOUT $unstar_string;
157 }
158 $input = '';
159 } elsif ($interruptable and $meaning eq 'INT') {
160 # Breaking out of the program
161 # Return early
162 last KEYSTROKE;
163 } else {
164 # Just an ordinary keystroke
165 $input .= $new_key;
166 print TTYOUT $star_string;
167 }
168 } else {
169 # Not special
170 $input .= $new_key;
171 print TTYOUT $star_string;
172 }
173 }
174 # Just in case someone sends a lot of data
175 $input = substr($input, 0, $INPUT_LIMIT)
176 if length($input) > $INPUT_LIMIT;
177 } else {
178 # No count, so something went wrong. Assume timeout.
179 # Return early
180 last KEYSTROKE;
181 }
182 }
183  
184 # Done with waiting for input. Let's not leave the cursor sitting
185 # there, after the prompt.
186 print TTYOUT "\n" unless $SUPPRESS_NEWLINE;
187  
188 # Let's put everything back where we found it.
189 $term->setlflag($original_flags);
190 while (my($field, $value) = each %original_cc) {
191 $term->setcc($CC_FIELDS{$field}, $value);
192 }
193 $term->setattr($fd_tty, TCSAFLUSH);
194 close(TTY);
195 close(TTYOUT);
196 $return_value;
197 }
198  
199 1;
200 __END__