| 1 | package App::Tel::Passwd::Pass; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 name | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | App::Tel::Passwd::Pass - Tel Passwd module for Pass | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =cut | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Module::Load; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | our $_debug = 0; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 METHODS | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | A note on private subroutines in this file: Anything with two underscores | ||||||||||||||||||||||||||||
| 21 | proceeding it is a private non-method sub. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head2 new | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | my $passwd = App::Tel::Passwd::Pass->new( file => $filename, passwd => $password ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | This will return a Passwd::Pass Object if the module exists and return undef if it doesn't. | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | Requires filename and password for the file. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =cut | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 0 | my $proto = shift; | |||||||||||||||||||||||||||
| 35 | 0 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||
| 36 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | my $self = { debug => $_debug, | |||||||||||||||||||||||||||
| 39 | %args | ||||||||||||||||||||||||||||
| 40 | }; | ||||||||||||||||||||||||||||
| 41 | 0 | $self->{file} = __find_password_store($self->{file}); | |||||||||||||||||||||||||||
| 42 | 0 | ▶ | $self->{gpg} ||= ($^O=~/(freebsd|openbsd|netbsd|solaris)/) ? '/usr/local/bin/gpg' : '/usr/bin/gpg'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"gpg"} ||= $^O =~ /(freebsd|openbsd|netbsd|solaris)/ ? "/usr/local/bin/gpg" : "/usr/bin/gpg"
MC/DC0%0/2
$self->{"gpg"} ||= $^O =~ /(freebsd|openbsd|netbsd|solaris)/ ? "/usr/local/bin/gpg" : "/usr/bin/gpg"
$self->{"gpg"}
$^O =~ /(freebsd|openbsd|netbsd|solaris)/ ? "/usr/local/bin/gpg" : "/usr/bin/gpg"
Truth table0%0/3
A ||= B
A= $self->{"gpg"}B= $^O =~ /(freebsd|openbsd|netbsd|solaris)/ ? "/usr/local/bin/gpg" : "/usr/bin/gpg"
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | if (! -x $self->{gpg}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | croak "$class: gpg executable not found."; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | if (!defined($self->{file}) || ! -r $self->{file} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->{"file"} and -r $self->{"file"}
MC/DC0%0/2
defined $self->{"file"} and -r $self->{"file"}
defined $self->{"file"}
-r $self->{"file"}
Truth table0%0/3
A and B
A= defined $self->{"file"}B= -r $self->{"file"}
| |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | $self->{file} ||= '<undefined>'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"file"} ||= "<undefined>"
MC/DC0%0/1
$self->{"file"} ||= "<undefined>"
$self->{"file"}
Truth table0%0/2
A
A= $self->{"file"}
| |||||||||||||||||||||||||||||
| 50 | 0 | croak "$class: Can't read file $self->{file}"; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | $self->{gnupg} = eval { | |||||||||||||||||||||||||||
| 54 | 0 | load GnuPG::Interface; | |||||||||||||||||||||||||||
| 55 | 0 | return GnuPG::Interface->new(); | |||||||||||||||||||||||||||
| 56 | }; | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | croak $@; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | bless( $self, $class ); | |||||||||||||||||||||||||||
| 63 | 0 | $self->{pass} = $self->_run($self->{gpg}, $self->{file}, $self->{passwd}); | |||||||||||||||||||||||||||
| 64 | 0 | return $self; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | sub _run { | ||||||||||||||||||||||||||
|
Subroutine
_run: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 68 | 0 | my ($self, $call, $file, $passphrase) = @_; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | my $gnupg = $self->{gnupg}; | |||||||||||||||||||||||||||
| 71 | 0 | $gnupg->call($call); | |||||||||||||||||||||||||||
| 72 | 0 | $gnupg->options->no_greeting(1); | |||||||||||||||||||||||||||
| 73 | 0 | $gnupg->options->quiet(1); | |||||||||||||||||||||||||||
| 74 | 0 | $gnupg->options->batch(1); | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # This time we'll catch the standard error for our perusing | ||||||||||||||||||||||||||||
| 77 | # as well as passing in the passphrase manually | ||||||||||||||||||||||||||||
| 78 | # as well as the status information given by GnuPG | ||||||||||||||||||||||||||||
| 79 | 0 | my ( $input, $output, $error, $passphrase_fh, $status_fh ) | |||||||||||||||||||||||||||
| 80 | = ( IO::Handle->new(), | ||||||||||||||||||||||||||||
| 81 | IO::Handle->new(), | ||||||||||||||||||||||||||||
| 82 | IO::Handle->new(), | ||||||||||||||||||||||||||||
| 83 | IO::Handle->new(), | ||||||||||||||||||||||||||||
| 84 | IO::Handle->new(), | ||||||||||||||||||||||||||||
| 85 | ); | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | my $handles = GnuPG::Handles->new( stdin => $input, | |||||||||||||||||||||||||||
| 88 | stdout => $output, | ||||||||||||||||||||||||||||
| 89 | stderr => $error, | ||||||||||||||||||||||||||||
| 90 | passphrase => $passphrase_fh, | ||||||||||||||||||||||||||||
| 91 | status => $status_fh, | ||||||||||||||||||||||||||||
| 92 | ); | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # this time we'll also demonstrate decrypting | ||||||||||||||||||||||||||||
| 95 | # a file written to disk | ||||||||||||||||||||||||||||
| 96 | # Make sure you "use IO::File" if you use this module! | ||||||||||||||||||||||||||||
| 97 | 0 | my $cipher_file = IO::File->new( $file ); | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # this sets up the communication | ||||||||||||||||||||||||||||
| 100 | 0 | my $pid = $gnupg->decrypt( handles => $handles ); | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # This passes in the passphrase | ||||||||||||||||||||||||||||
| 103 | 0 | print $passphrase_fh $passphrase; | |||||||||||||||||||||||||||
| 104 | 0 | close $passphrase_fh; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # this passes in the plaintext | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | print $input $_ while <$cipher_file>; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # this closes the communication channel, | ||||||||||||||||||||||||||||
| 110 | # indicating we are done | ||||||||||||||||||||||||||||
| 111 | 0 | close $input; | |||||||||||||||||||||||||||
| 112 | 0 | close $cipher_file; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | my @plaintext = <$output>; # reading the output | |||||||||||||||||||||||||||
| 115 | 0 | my @error_output = <$error>; # reading the error | |||||||||||||||||||||||||||
| 116 | 0 | my @status_info = <$status_fh>; # read the status info | |||||||||||||||||||||||||||
| 117 | 0 | chomp(@plaintext); | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | for (@status_info) { | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | croak @error_output if (/BAD_PASSPHRASE|DECRYPTION_FAILED/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # clean up... | ||||||||||||||||||||||||||||
| 124 | 0 | close $output; | |||||||||||||||||||||||||||
| 125 | 0 | close $error; | |||||||||||||||||||||||||||
| 126 | 0 | close $status_fh; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | waitpid $pid, 0; # clean up the finished GnuPG process | |||||||||||||||||||||||||||
| 129 | 0 | return $plaintext[0]; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub __find_password_store { | ||||||||||||||||||||||||||
|
Subroutine
__find_password_store: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 133 | 0 | my $file = shift; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | return if (!defined($file)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | if ($file !~ /.gpg$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 0 | $file .= '.gpg'; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | # if it's an absolute path then treat it as-is. | ||||||||||||||||||||||||||||
| 140 | 0 | ▶ | return $file if ($file =~ m#^/#); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | if (defined($ENV{PASSWORD_STORE_DIR})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | return "$ENV{PASSWORD_STORE_DIR}/$file"; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | return "$ENV{HOME}/.password-store/$file"; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head2 passwd | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | $passwd->passwd($entry); | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | This requires a password for the router. It will return a blank line if not | ||||||||||||||||||||||||||||
| 154 | found. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =cut | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | ▶ | sub passwd { | ||||||||||||||||||||||||||
|
Subroutine
passwd: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 159 | 0 | $_[0]->{pass}; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 1 | 1; | |||||||||||||||||||||||||||