| 1 | package Text::Authinfo; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use File::Copy qw(move); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Text::CSV; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Carp qw(croak carp); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use vars qw($VERSION @EXPORT); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | @EXPORT = qw(readauthinfo writeauthinfo as_string); | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | our $wildcard = 'ANY'; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 3 | our $authinfofile = $ENV{'HOME'} . '/.authinfo'; | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | my $self = {}; | |||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | $self->{FILE} = shift || $authinfofile; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
shift() || $Text::Authinfo::authinfofile
MC/DC0%0/2
shift() || $Text::Authinfo::authinfofile
shift()
$Text::Authinfo::authinfofile
Truth table33%1/3
A || B
A= shift()B= $Text::Authinfo::authinfofile
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | $self->{AUTHINFO} = {}; | |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | bless $self,$class ; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 1 | ▶ | sub readauthinfo { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
readauthinfo: called CC=9 SCAR=24.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 1 | my $ai = {}; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | ▶ | my $csv = Text::CSV->new({sep_char=> ' '}) || croak 'new Text::CSV'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
'Text::CSV'->new({'sep_char', ' '}) || croak('new Text::CSV')
MC/DC0%0/1
'Text::CSV'->new({'sep_char', ' '}) || croak('new Text::CSV')
'Text::CSV'->new({'sep_char', ' '})
Truth table50%1/2
A
A= 'Text::CSV'->new({'sep_char', ' '})
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | ▶ | open(my $fh,'<',$self->{FILE}) || croak "open $self->{FILE}:$!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | ▶ | LINE:while (my $line = <$fh>) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | chomp $line; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 1 | $csv->parse($line); | |||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | my %l = $csv->fields(); | |||||||||||||||||||||||||||||||||||||||||
| 44 | # this package requires at least these fields to be on a valid | ||||||||||||||||||||||||||||||||||||||||||
| 45 | # line: | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | ▶ | if (defined($l{'machine'}) && | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $l{"machine"} and defined $l{"login"} and defined $l{"password"}
defined $l{"machine"} and defined $l{"login"}
MC/DC0%0/3
defined $l{"machine"} and defined $l{"login"} and defined $l{"password"}
defined $l{"machine"}
defined $l{"login"}
defined $l{"password"}
Truth table25%1/4
A and B and C
A= defined $l{"machine"}B= defined $l{"login"}C= defined $l{"password"}
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | defined($l{'login'}) && | ||||||||||||||||||||||||||||||||||||||||||
| 48 | defined($l{'password'})) { | ||||||||||||||||||||||||||||||||||||||||||
| 49 | # allow port to be defined or ANY | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | my $port = $wildcard; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | ▶ | $port = $l{'port'} if (defined($l{'port'})); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | $ai->{$l{'machine'}}->{$port}->{$l{'login'}} = $l{'password'}; | |||||||||||||||||||||||||||||||||||||||||
| 53 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | carp "$line missing some fields? skipping"; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | next LINE; | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | $self->{AUTHINFO} = $ai; | |||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 1 | ▶ | close($fh) || croak "close $authinfofile:$!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 1 | return 1; # caller can now query authinfo data as a perl assoc array | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 2 | ▶ | sub getauth() { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
getauth: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 2 | ▶ | my $machine = shift || croak 'use: getauth(machine,login,[port])'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || croak('use: getauth(machine,login,[port])')
MC/DC0%0/1
shift() || croak('use: getauth(machine,login,[port])')
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 2 | ▶ | my $login = shift || croak 'use: getauth(machine,login,[port])'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || croak('use: getauth(machine,login,[port])')
MC/DC0%0/1
shift() || croak('use: getauth(machine,login,[port])')
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 2 | ▶ | my $port = shift || $wildcard; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
shift() || $Text::Authinfo::wildcard
MC/DC0%0/2
shift() || $Text::Authinfo::wildcard
shift()
$Text::Authinfo::wildcard
Truth table33%1/3
A || B
A= shift()B= $Text::Authinfo::wildcard
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | # retval may be undef | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | return $self->{AUTHINFO}->{$machine}->{$port}->{$login}; | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | sub as_string { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_string: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | my $c = ''; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | for my $machine (keys %{$self->{AUTHINFO}}) { | |||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | for my $port (keys %{$self->{AUTHINFO}->{$machine}}) { | |||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | for my $login (keys %{$self->{AUTHINFO}->{$machine}->{$port}}) { | |||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | my $pass = $self->{AUTHINFO}->{$machine}->{$port}->{$login}; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | $c .= 'machine ' . $machine . ' login ' . $login . | |||||||||||||||||||||||||||||||||||||||||
| 88 | ' password ' . $pass; | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | $c .= ' port ' . $port if ($port ne $wildcard); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | $c .= "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | return $c; | |||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub writeauthinfo { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
writeauthinfo: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | my $ops = shift || undef; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || undef
MC/DC0%0/1
shift() || undef
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if (defined($ops) && (ref($ops) ne 'HASH')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $ops and ref $ops ne "HASH"
MC/DC0%0/2
defined $ops and ref $ops ne "HASH"
defined $ops
ref $ops ne "HASH"
Truth table0%0/3
A and B
A= defined $opsB= ref $ops ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | carp "args are passed to writeauthinfo via a hash ref"; | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if (-w $self->{FILE}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | # there is already a .authinfo file, mv it to .authinfo.bak | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if (defined($ops->{nobackup})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | unlink $self->{FILE} || croak "rm old $self->{FILE}:$!" | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'FILE'} || croak("rm old $self->{'FILE'}:$!")
MC/DC0%0/1
$self->{'FILE'} || croak("rm old $self->{'FILE'}:$!")
$self->{'FILE'}
Truth table0%0/2
A
A= $self->{'FILE'}
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | } else { # by default, make a backup old old authinfo file | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | my $bak = $self->{FILE} . '.bak'; | |||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | move($self->{FILE},$bak) || croak "mv $self->{FILE} $bak:$!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | my $c = $self->as_string(); | |||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | if ($c) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | open(my $fh,'>',$self->{FILE}) || croak "open $self->{FILE}:$!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | print $fh $c; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | close($fh) || croak "close $self->{FILE}:$!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | chmod 0600, $self->{FILE} || croak "chmod fail on $self->{FILE}:$!"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'FILE'} || croak("chmod fail on $self->{'FILE'}:$!")
MC/DC0%0/1
$self->{'FILE'} || croak("chmod fail on $self->{'FILE'}:$!")
$self->{'FILE'}
Truth table0%0/2
A
A= $self->{'FILE'}
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | __END__; | ||||||||||||||||||||||||||||||||||||||||||