read_password improvements

* Cancel on Ctrl+C (die())
* Finish on Ctrl+D (eof/eot) without appending a newline
* Also finish on \n to be sure.

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
Reviewed-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
Tested-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2017-12-13 11:47:23 +01:00
parent 590b924e19
commit ee29490fe0

View File

@ -194,10 +194,13 @@ sub read_password(;$$) {
syswrite($outfd, $query, length($query)); syswrite($outfd, $query, length($query));
while (($got = sysread($infd, $ch, 1))) { while (($got = sysread($infd, $ch, 1))) {
my ($ord) = unpack('C', $ch); my ($ord) = unpack('C', $ch);
if ($ord == 0xD) { last if $ord == 4; # ^D / EOF
if ($ord == 0xA || $ord == 0xD) {
# newline, we're done # newline, we're done
syswrite($outfd, "\r\n", 2); syswrite($outfd, "\r\n", 2);
last; last;
} elsif ($ord == 3) { # ^C
die "password input aborted\n";
} elsif ($ord == 0x7f) { } elsif ($ord == 0x7f) {
# backspace - if it's the first key disable # backspace - if it's the first key disable
# asterisks # asterisks