| 1 | # -*- Perl -*- | ||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||
| 3 | # representation of authorized_keys entry options, either associated | ||||||||||||||||||||||||||||||||||
| 4 | # with a particular Config::OpenSSH::Authkey::Entry object, or | ||||||||||||||||||||||||||||||||||
| 5 | # standalone | ||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||
| 7 | package Config::OpenSSH::Authkey::Entry::Options; | ||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use 5.006000; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||
| 15 | 3 | our $VERSION = '1.06'; | |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | # delved from sshd(8), auth-options.c of OpenSSH 5.2. Insensitive match | ||||||||||||||||||||||||||||||||||
| 18 | # required, as OpenSSH uses strncasecmp(3). | ||||||||||||||||||||||||||||||||||
| 19 | 3 | my $AUTHKEY_OPTION_NAME_RE = qr/(?i)[a-z0-9_-]+/; | |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||
| 21 | ###################################################################### | ||||||||||||||||||||||||||||||||||
| 22 | # | ||||||||||||||||||||||||||||||||||
| 23 | # Class methods | ||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||
| 25 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 26 | 4 | my $class = shift; | |||||||||||||||||||||||||||||||||
| 27 | 4 | my $option_string = shift; | |||||||||||||||||||||||||||||||||
| 28 | 4 | my $self = { _options => [] }; | |||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||
| 30 | 4 | ▶ | if ( defined $option_string ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 31 | $self->{_options} = | ||||||||||||||||||||||||||||||||||
| 32 | 2 | Config::OpenSSH::Authkey::Entry::Options->split_options($option_string); | |||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||
| 35 | 4 | bless $self, $class; | |||||||||||||||||||||||||||||||||
| 36 | 4 | return $self; | |||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||
| 39 | 7 | ▶ | sub split_options { | ||||||||||||||||||||||||||||||||
|
Subroutine
split_options: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 40 | 7 | my $class = shift; | |||||||||||||||||||||||||||||||||
| 41 | 7 | my $option_string = shift; | |||||||||||||||||||||||||||||||||
| 42 | 7 | my @options; | |||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||
| 44 | # Inspected OpenSSH auth-options.c,v 1.44 to derive this lexer: | ||||||||||||||||||||||||||||||||||
| 45 | # | ||||||||||||||||||||||||||||||||||
| 46 | # In OpenSSH, unparsable options result in a call to bad_options and | ||||||||||||||||||||||||||||||||||
| 47 | # the entry being rejected. This module is more permissive, in that | ||||||||||||||||||||||||||||||||||
| 48 | # any option name is allowed, regardless of whether OpenSSH supports | ||||||||||||||||||||||||||||||||||
| 49 | # such an option or whether the option is the correct type (boolean | ||||||||||||||||||||||||||||||||||
| 50 | # vs. string value). This makes the module more future proof, at the | ||||||||||||||||||||||||||||||||||
| 51 | # cost of allowing garbage through. | ||||||||||||||||||||||||||||||||||
| 52 | # | ||||||||||||||||||||||||||||||||||
| 53 | # Options are stored using a list of hashrefs, which allows for | ||||||||||||||||||||||||||||||||||
| 54 | # duplicate options, and preserves the order of options. Also, an | ||||||||||||||||||||||||||||||||||
| 55 | # index is maintained to speed lookups of the data, and to note if | ||||||||||||||||||||||||||||||||||
| 56 | # duplicate options exist. This is due to inconsistent handling by | ||||||||||||||||||||||||||||||||||
| 57 | # OpenSSH_5.1p1 of command="" vs. from="" vs. environment="" options | ||||||||||||||||||||||||||||||||||
| 58 | # when multiple entries are present. Methods are offered to detect and | ||||||||||||||||||||||||||||||||||
| 59 | # cleanup such (hopefully rare) duplicate options. | ||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||
| 61 | OPTION_LEXER: { | ||||||||||||||||||||||||||||||||||
| 62 | # String Argument Options - value is a perhaps empty string enclosed | ||||||||||||||||||||||||||||||||||
| 63 | # in double quotes. Internal double quotes are allowed, but only if | ||||||||||||||||||||||||||||||||||
| 64 | # these are preceded by a backslash. | ||||||||||||||||||||||||||||||||||
| 65 | 7 | ▶ | if ($option_string =~ m/ \G ($AUTHKEY_OPTION_NAME_RE)="( (?: \\"|[^"] )*? )" | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 66 | (?:,|[ \t]+)? /cgx | ||||||||||||||||||||||||||||||||||
| 67 | ) { | ||||||||||||||||||||||||||||||||||
| 68 | 7 | my $option_name = $1; | |||||||||||||||||||||||||||||||||
| 69 | 7 | ▶ | my $option_value = $2 || q{}; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$2 || ''
MC/DC0%0/1
$2 || ''
$2
Truth table50%1/2
A
A= $2
| |||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||
| 71 | 7 | push @options, { name => $option_name, value => $option_value }; | |||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||
| 73 | 7 | redo OPTION_LEXER; | |||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||
| 76 | # Boolean options - mere presence enables them in OpenSSH | ||||||||||||||||||||||||||||||||||
| 77 | 17 | ▶ | if ( $option_string =~ m/ \G ($AUTHKEY_OPTION_NAME_RE) (?:,|[ \t]+)? /cgx ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 78 | 10 | my $option_name = $1; | |||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||
| 80 | 10 | push @options, { name => $option_name }; | |||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||
| 82 | 10 | redo OPTION_LEXER; | |||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||
| 86 | 7 | ▶ | return wantarray ? @options : \@options; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||
| 89 | ###################################################################### | ||||||||||||||||||||||||||||||||||
| 90 | # | ||||||||||||||||||||||||||||||||||
| 91 | # Instance methods | ||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||
| 93 | 2 | ▶ | sub parse { | ||||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 94 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 95 | 2 | my $option_string = shift; | |||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||
| 97 | $self->{_options} = | ||||||||||||||||||||||||||||||||||
| 98 | 2 | Config::OpenSSH::Authkey::Entry::Options->split_options($option_string); | |||||||||||||||||||||||||||||||||
| 99 | 2 | return scalar @{ $self->{_options} }; | |||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||
| 102 | 10 | ▶ | sub as_string { | ||||||||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 103 | 10 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 104 | 10 | my @options; | |||||||||||||||||||||||||||||||||
| 105 | 10 | for my $options_ref ( @{ $self->{_options} } ) { | |||||||||||||||||||||||||||||||||
| 106 | 18 | ▶ | if ( exists $options_ref->{value} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 107 | 9 | ( my $value = $options_ref->{value} ) =~ s/(?<!\\)"/\\"/g; | |||||||||||||||||||||||||||||||||
| 108 | 9 | push @options, $options_ref->{name} . '="' . $value . '"'; | |||||||||||||||||||||||||||||||||
| 109 | } else { | ||||||||||||||||||||||||||||||||||
| 110 | 9 | push @options, $options_ref->{name}; | |||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||
| 113 | 10 | return join( q{,}, @options ); | |||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||
| 116 | # NOTE - boolean return the name of the option, while string value | ||||||||||||||||||||||||||||||||||
| 117 | # options the string. This may change, depending on how I like how this | ||||||||||||||||||||||||||||||||||
| 118 | # is handled... | ||||||||||||||||||||||||||||||||||
| 119 | 8 | ▶ | sub get_option { | ||||||||||||||||||||||||||||||||
|
Subroutine
get_option: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 120 | 8 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 121 | 8 | ▶ | my $option_name = shift || croak 'get_option requires an option name'; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || croak('get_option requires an option name')
MC/DC0%0/1
shift() || croak('get_option requires an option name')
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||
| 123 | my @values = | ||||||||||||||||||||||||||||||||||
| 124 | 5 | ▶ | map { $_->{value} || $option_name } | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
$_->{'value'} || $option_name
MC/DC0%0/2
$_->{'value'} || $option_name
$_->{'value'}
$option_name
Truth table66%2/3
A || B
A= $_->{'value'}B= $option_name
| |||||||||||||||||||||||||||||||||||
| 125 | 8 | grep { $_->{name} eq $option_name } @{ $self->{_options} }; | |||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||
| 127 | 8 | ▶ | return wantarray ? @values : defined $values[0] ? $values[0] : ''; | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||
| 130 | 4 | ▶ | sub get_options { | ||||||||||||||||||||||||||||||||
|
Subroutine
get_options: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 131 | 4 | map { $_->{name} } @{ shift->{_options} }; | |||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||
| 134 | # Sets an option. To enable a boolean option, only supply the option | ||||||||||||||||||||||||||||||||||
| 135 | # name, and pass no value data. | ||||||||||||||||||||||||||||||||||
| 136 | 6 | ▶ | sub set_option { | ||||||||||||||||||||||||||||||||
|
Subroutine
set_option: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 137 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 138 | 6 | ▶ | my $option_name = shift || croak 'set_option requires an option name'; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || croak('set_option requires an option name')
MC/DC0%0/1
shift() || croak('set_option requires an option name')
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||
| 139 | 6 | my $option_value = shift; | |||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||
| 141 | 6 | my $updated = 0; | |||||||||||||||||||||||||||||||||
| 142 | 6 | my $record_count = @{ $self->{_options} }; | |||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||
| 144 | 6 | for my $options_ref ( @{ $self->{_options} } ) { | |||||||||||||||||||||||||||||||||
| 145 | 8 | ▶ | if ( $options_ref->{name} eq $option_name ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 146 | 2 | ▶ | $options_ref->{value} = $option_value if defined $option_value; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 147 | 2 | ++$updated; | |||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||
| 150 | 6 | ▶ | if ( $updated == 0 ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 151 | 5 | ▶ | push @{ $self->{_options} }, | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 152 | { name => $option_name, | ||||||||||||||||||||||||||||||||||
| 153 | ( defined $option_value ? ( value => $option_value ) : () ) | ||||||||||||||||||||||||||||||||||
| 154 | }; | ||||||||||||||||||||||||||||||||||
| 155 | } elsif ( $updated > 1 ) { | ||||||||||||||||||||||||||||||||||
| 156 | # KLUGE edge-case where duplicate entries exist for this option. Clear | ||||||||||||||||||||||||||||||||||
| 157 | # all duplicates beyond the first entry. | ||||||||||||||||||||||||||||||||||
| 158 | 1 | my $seen = 0; | |||||||||||||||||||||||||||||||||
| 159 | 1 | @{ $self->{_options} } = | |||||||||||||||||||||||||||||||||
| 160 | 3 | ▶ | grep { $_->{name} ne $option_name or $_->{name} eq $option_name && !$seen++ } | ||||||||||||||||||||||||||||||||
|
Condition83%5/6
$_->{'name'} ne $option_name || $_->{'name'} eq $option_name && !$seen++
$_->{"name"} eq $option_name and not $seen++
MC/DC66%2/3
$_->{'name'} ne $option_name || $_->{'name'} eq $option_name && !$seen++
$_->{'name'} ne $option_name
$_->{"name"} eq $option_name
not $seen++
Truth table75%3/4
A || B and not(C)
A= $_->{'name'} ne $option_nameB= $_->{"name"} eq $option_nameC= not $seen++
| |||||||||||||||||||||||||||||||||||
| 161 | 1 | @{ $self->{_options} }; | |||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||
| 164 | 6 | return $record_count - @{ $self->{_options} }; | |||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||
| 167 | 4 | ▶ | sub unset_option { | ||||||||||||||||||||||||||||||||
|
Subroutine
unset_option: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 168 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 169 | 4 | ▶ | my $option_name = shift || croak 'unset_option requires an option name'; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || croak('unset_option requires an option name')
MC/DC0%0/1
shift() || croak('unset_option requires an option name')
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||
| 171 | 4 | my $record_count = @{ $self->{_options} }; | |||||||||||||||||||||||||||||||||
| 172 | 4 | @{ $self->{_options} } = | |||||||||||||||||||||||||||||||||
| 173 | 4 | grep { $_->{name} ne $option_name } @{ $self->{_options} }; | |||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||
| 175 | 4 | return $record_count - @{ $self->{_options} }; | |||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||
| 178 | 2 | ▶ | sub unset_options { | ||||||||||||||||||||||||||||||||
|
Subroutine
unset_options: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 179 | 2 | shift->{_options} = []; | |||||||||||||||||||||||||||||||||
| 180 | 2 | return 1; | |||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||
| 183 | 3 | 1; | |||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||
| 185 | __END__ | ||||||||||||||||||||||||||||||||||