| 1 | # -*- Perl -*- | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # methods to interact with OpenSSH authorized_keys file data | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # run perldoc(1) on this file for additional documentation | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | package Config::OpenSSH::Authkey; | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use 5.006000; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Config::OpenSSH::Authkey::Entry (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use IO::Handle qw(getline); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | our $VERSION = '1.06'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | ###################################################################### | ||||||||||||||||||||||||||||
| 21 | # | ||||||||||||||||||||||||||||
| 22 | # Utility Methods - Internal | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | { | ||||||||||||||||||||||||||||
| 25 | # Utility class for comments or blank lines in authorized_keys files | ||||||||||||||||||||||||||||
| 26 | package Config::OpenSSH::Authkey::MetaEntry; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 23 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 23 | my $class = shift; | |||||||||||||||||||||||||||
| 30 | 23 | my $entry = shift; | |||||||||||||||||||||||||||
| 31 | 23 | bless \$entry, $class; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | sub as_string { | ||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 1 | ${ $_[0] }; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | ###################################################################### | ||||||||||||||||||||||||||||
| 40 | # | ||||||||||||||||||||||||||||
| 41 | # Class methods | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 3 | my $class = shift; | |||||||||||||||||||||||||||
| 45 | 3 | ▶ | my $options_ref = shift || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || {}
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 3 | my $self = { | |||||||||||||||||||||||||||
| 48 | _fh => undef, | ||||||||||||||||||||||||||||
| 49 | _keys => [], | ||||||||||||||||||||||||||||
| 50 | _seen_keys => {}, | ||||||||||||||||||||||||||||
| 51 | _auto_store => 0, | ||||||||||||||||||||||||||||
| 52 | _tag_dups => 0, | ||||||||||||||||||||||||||||
| 53 | _nostore_nonkey_data => 0 | ||||||||||||||||||||||||||||
| 54 | }; | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 3 | for my $pref (qw/auto_store tag_dups nostore_nonkey_data/) { | |||||||||||||||||||||||||||
| 57 | 9 | ▶ | if ( exists $options_ref->{$pref} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 3 | ▶ | $self->{"_$pref"} = $options_ref->{$pref} ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 3 | bless $self, $class; | |||||||||||||||||||||||||||
| 63 | 3 | return $self; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | ###################################################################### | ||||||||||||||||||||||||||||
| 67 | # | ||||||||||||||||||||||||||||
| 68 | # Instance methods | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | ▶ | sub fh { | ||||||||||||||||||||||||||
|
Subroutine
fh: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 72 | 1 | ▶ | my $fh = shift || croak 'fh requires a filehandle'; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || croak('fh requires a filehandle')
MC/DC0%0/1
shift() || croak('fh requires a filehandle')
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 1 | $self->{_fh} = $fh; | |||||||||||||||||||||||||||
| 75 | 1 | return $self; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | sub file { | ||||||||||||||||||||||||||
|
Subroutine
file: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 80 | 1 | ▶ | my $file = shift || croak 'file requires a file'; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || croak('file requires a file')
MC/DC0%0/1
shift() || croak('file requires a file')
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 1 | my $fh; | |||||||||||||||||||||||||||
| 83 | 1 | ▶ | open( $fh, '<', $file ) or croak $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 1 | $self->{_fh} = $fh; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 1 | return $self; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 18 | ▶ | sub iterate { | ||||||||||||||||||||||||||
|
Subroutine
iterate: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 18 | my $self = shift; | |||||||||||||||||||||||||||
| 91 | 18 | ▶ | croak 'no filehandle to iterate on' if !defined $self->{_fh}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 18 | my $line = $self->{_fh}->getline; | |||||||||||||||||||||||||||
| 94 | 18 | ▶ | return defined $line ? $self->parse($line) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 1 | ▶ | sub consume { | ||||||||||||||||||||||||||
|
Subroutine
consume: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 99 | 1 | ▶ | croak 'no filehandle to consume' if !defined $self->{_fh}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 1 | my $old_auto_store = $self->auto_store(); | |||||||||||||||||||||||||||
| 102 | 1 | $self->auto_store(1); | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 1 | ▶ | while ( my $line = $self->{_fh}->getline ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 17 | $self->parse($line); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 1 | $self->auto_store($old_auto_store); | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 1 | return $self; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 35 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=8 SCAR=21.6
Pod
documented
| |||||||||||||||||||||||||||||
| 114 | 35 | my $self = shift; | |||||||||||||||||||||||||||
| 115 | 35 | ▶ | my $data = shift || croak 'need data to parse'; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || croak('need data to parse')
MC/DC0%0/1
shift() || croak('need data to parse')
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 35 | my $entry; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 35 | ▶ | if ( $data =~ m/^\s*(?:#|$)/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 22 | chomp($data); | |||||||||||||||||||||||||||
| 121 | 22 | $entry = Config::OpenSSH::Authkey::MetaEntry->new($data); | |||||||||||||||||||||||||||
| 122 | 22 | ▶ | if ( $self->{_auto_store} and !$self->{_nostore_nonkey_data} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"_auto_store"} and not $self->{"_nostore_nonkey_data"}
MC/DC0%0/2
$self->{"_auto_store"} and not $self->{"_nostore_nonkey_data"}
$self->{"_auto_store"}
not $self->{"_nostore_nonkey_data"}
Truth table33%1/3
A and not(B)
A= $self->{"_auto_store"}B= not $self->{"_nostore_nonkey_data"}
| |||||||||||||||||||||||||||||
| 123 | 0 | push @{ $self->{_keys} }, $entry; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } else { | ||||||||||||||||||||||||||||
| 126 | 13 | $entry = Config::OpenSSH::Authkey::Entry->new($data); | |||||||||||||||||||||||||||
| 127 | 12 | ▶ | if ( $self->{_tag_dups} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 6 | ▶ | if ( exists $self->{_seen_keys}->{ $entry->key } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 1 | $entry->duplicate_of( $self->{_seen_keys}->{ $entry->key } ); | |||||||||||||||||||||||||||
| 130 | } else { | ||||||||||||||||||||||||||||
| 131 | 5 | $self->{_seen_keys}->{ $entry->key } = $entry; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 12 | ▶ | push @{ $self->{_keys} }, $entry if $self->{_auto_store}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 34 | return $entry; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 7 | ▶ | sub get_stored_keys { | ||||||||||||||||||||||||||
|
Subroutine
get_stored_keys: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 141 | 7 | shift->{_keys}; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 1 | ▶ | sub reset_store { | ||||||||||||||||||||||||||
|
Subroutine
reset_store: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 145 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 146 | 1 | $self->{_seen_keys} = {}; | |||||||||||||||||||||||||||
| 147 | 1 | $self->{_keys} = []; | |||||||||||||||||||||||||||
| 148 | 1 | return $self; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | sub reset_dups { | ||||||||||||||||||||||||||
|
Subroutine
reset_dups: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 152 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 153 | 0 | $self->{_seen_keys} = {}; | |||||||||||||||||||||||||||
| 154 | 0 | return $self; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 7 | ▶ | sub auto_store { | ||||||||||||||||||||||||||
|
Subroutine
auto_store: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 159 | 7 | my $setting = shift; | |||||||||||||||||||||||||||
| 160 | 7 | ▶ | if ( defined $setting ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 3 | ▶ | $self->{_auto_store} = $setting ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 7 | return $self->{_auto_store}; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 3 | ▶ | sub tag_dups { | ||||||||||||||||||||||||||
|
Subroutine
tag_dups: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 167 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 168 | 3 | my $setting = shift; | |||||||||||||||||||||||||||
| 169 | 3 | ▶ | if ( defined $setting ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 1 | ▶ | $self->{_tag_dups} = $setting ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | 3 | return $self->{_tag_dups}; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 3 | ▶ | sub nostore_nonkey_data { | ||||||||||||||||||||||||||
|
Subroutine
nostore_nonkey_data: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 176 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 177 | 3 | my $setting = shift; | |||||||||||||||||||||||||||
| 178 | 3 | ▶ | if ( defined $setting ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 1 | ▶ | $self->{_nostore_nonkey_data} = $setting ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | 3 | return $self->{_nostore_nonkey_data}; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 1 | 1; | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | __END__ | ||||||||||||||||||||||||||||