| 1 | # | ||||||||||||||||||||||||||||
| 2 | # This file is part of Config-Model-OpenSsh | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This software is Copyright (c) 2008-2025 by Dominique Dumont. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # This is free software, licensed under: | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # The GNU Lesser General Public License, Version 2.1, February 1999 | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | package Config::Model::Backend::OpenSsh::Role::Reader ; | ||||||||||||||||||||||||||||
| 14 | 4 | $Config::Model::Backend::OpenSsh::Role::Reader::VERSION = '2.10.2.1'; | |||||||||||||||||||||||||||
| 15 | 4 | ▶ | use 5.10.1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use Config::Model 2.128; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use Mouse::Role ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 4 | requires qw(read_global_comments associates_comments_with_data); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # sub stub known as "forward" declaration | ||||||||||||||||||||||||||||
| 23 | # required for Role consistency checks | ||||||||||||||||||||||||||||
| 24 | # See Moose::Manual::Roles for details | ||||||||||||||||||||||||||||
| 25 | sub current_node; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 4 | has 'current_node' => ( | |||||||||||||||||||||||||||
| 28 | is => 'rw', | ||||||||||||||||||||||||||||
| 29 | isa => 'Config::Model::Node', | ||||||||||||||||||||||||||||
| 30 | weak_ref => 1 | ||||||||||||||||||||||||||||
| 31 | ) ; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 4 | ▶ | use Carp ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 4 | ▶ | use Log::Log4perl 1.11; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 4 | my $logger = Log::Log4perl::get_logger("Backend::OpenSsh"); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 4 | my @dispatch = ( | |||||||||||||||||||||||||||
| 39 | qr/match/i => 'match', | ||||||||||||||||||||||||||||
| 40 | qr/host\b/i => 'host', | ||||||||||||||||||||||||||||
| 41 | qr/(local|remote)forward/i => 'forward', | ||||||||||||||||||||||||||||
| 42 | qr/^PreferredAuthentications$/ => 'comma_list', | ||||||||||||||||||||||||||||
| 43 | qr/localcommand/i => 'assign', | ||||||||||||||||||||||||||||
| 44 | qr/\w/ => 'assign', | ||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 26 | ▶ | sub read { | ||||||||||||||||||||||||||
|
Subroutine
read: called CC=8 SCAR=20.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | 26 | my $self = shift ; | |||||||||||||||||||||||||||
| 49 | 26 | my %args = @_ ; | |||||||||||||||||||||||||||
| 50 | my $config_root = $args{object} | ||||||||||||||||||||||||||||
| 51 | 26 | ▶ | || croak __PACKAGE__," read_ssh_file: undefined config root object"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'object'} || croak(__PACKAGE__, ' read_ssh_file: undefined config root object')
MC/DC0%0/1
$args{'object'} || croak(__PACKAGE__, ' read_ssh_file: undefined config root object')
$args{'object'}
Truth table50%1/2
A
A= $args{'object'}
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 26 | ▶ | return 0 unless $args{file_path}->is_file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 23 | $logger->info("loading config file ".$args{file_path}); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 23 | my @lines = $args{file_path}->lines_utf8 ; | |||||||||||||||||||||||||||
| 58 | # try to get global comments (comments before a blank line) | ||||||||||||||||||||||||||||
| 59 | 23 | $self->read_global_comments(\@lines,'#') ; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # need to reset this when reading user ssh file after system ssh file | ||||||||||||||||||||||||||||
| 62 | 23 | $self->current_node($config_root) ; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 23 | my @assoc = $self->associates_comments_with_data( \@lines, '#' ) ; | |||||||||||||||||||||||||||
| 65 | 23 | foreach my $item (@assoc) { | |||||||||||||||||||||||||||
| 66 | 316 | my ( $vdata, $comment ) = @$item; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 316 | my ( $k, @v ) = split /\s+/, $vdata; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 316 | my $i = 0; | |||||||||||||||||||||||||||
| 71 | 316 | ▶ | while ( $i < @dispatch ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 1624 | my ( $regexp, $sub ) = @dispatch[ $i++, $i++ ]; | |||||||||||||||||||||||||||
| 73 | 1624 | ▶ | if ( $k =~ $regexp and $self->can($sub)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$k =~ /$regexp/ and $self->can($sub)
MC/DC50%1/2
$k =~ /$regexp/ and $self->can($sub)
$k =~ /$regexp/
$self->can($sub)
Truth table66%2/3
A and B
A= $k =~ /$regexp/B= $self->can($sub)
| |||||||||||||||||||||||||||||
| 74 | 316 | $logger->trace("read_ssh_file: dispatch calls $sub"); | |||||||||||||||||||||||||||
| 75 | 316 | $self->$sub( $config_root, $k, \@v, $comment, $args{check} ); | |||||||||||||||||||||||||||
| 76 | 316 | last; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 1308 | ▶ | warn __PACKAGE__, " unknown keyword: $k" if $i >= @dispatch; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 23 | return 1; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 3 | ▶ | sub comma_list { | ||||||||||||||||||||||||||
|
Subroutine
comma_list: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 86 | 3 | my ($self,$root, $raw_key,$arg,$comment, $check) = @_ ; | |||||||||||||||||||||||||||
| 87 | 3 | $logger->debug("assign: $raw_key @$arg # $comment"); | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 3 | my @list = map { split /\s*,\s*/ } @$arg; | |||||||||||||||||||||||||||
| 90 | 3 | $self->assign($root, $raw_key,\@list,$comment, $check); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 247 | ▶ | sub assign { | ||||||||||||||||||||||||||
|
Subroutine
assign: called CC=10 SCAR=27.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 94 | 247 | my ($self,$root, $raw_key,$arg,$comment, $check) = @_ ; | |||||||||||||||||||||||||||
| 95 | 247 | $logger->debug("assign: $raw_key @$arg # $comment"); | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # keys are case insensitive, try to find a match | ||||||||||||||||||||||||||||
| 99 | 247 | my $key = $self->current_node->find_element ($raw_key, case => 'any') ; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 247 | ▶ | if (not defined $key) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if ($check eq 'yes') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | # drop if -force is not set | ||||||||||||||||||||||||||||
| 104 | 0 | die "Error: unknown parameter: '$raw_key'. Use -force option to drop this parameter\n"; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | else { | ||||||||||||||||||||||||||||
| 107 | 0 | say "Dropping parameter '$raw_key'" ; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | 0 | return; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 247 | my $elt = $self->current_node->fetch_element($key) ; | |||||||||||||||||||||||||||
| 113 | 247 | my $type = $elt->get_type; | |||||||||||||||||||||||||||
| 114 | #print "got $key type $type and ",join('+',@$arg),"\n"; | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 247 | ▶ | $elt->annotation($comment) if $comment and $type ne 'hash'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$comment and $type ne "hash"
MC/DC50%1/2
$comment and $type ne "hash"
$comment
$type ne "hash"
Truth table66%2/3
A and B
A= $commentB= $type ne "hash"
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 247 | ▶ | if ($type eq 'leaf') { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 119 | 196 | $elt->store( value => join(' ',@$arg), check => $check ) ; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | elsif ($type eq 'list') { | ||||||||||||||||||||||||||||
| 122 | 48 | $elt->push_x ( values => $arg, check => $check ) ; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | elsif ($type eq 'hash') { | ||||||||||||||||||||||||||||
| 125 | 3 | my $hv = $elt->fetch_with_id($arg->[0]); | |||||||||||||||||||||||||||
| 126 | 3 | $hv->store( value => $arg->[1], check => $check ); | |||||||||||||||||||||||||||
| 127 | 3 | ▶ | $hv->annotation($comment) if $comment; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | elsif ($type eq 'check_list') { | ||||||||||||||||||||||||||||
| 130 | 0 | my @check = split /\s*,\s*/,$arg->[0] ; | |||||||||||||||||||||||||||
| 131 | 0 | $elt->set_checked_list (\@check, check => 'skip') ; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | else { | ||||||||||||||||||||||||||||
| 134 | 0 | die "OpenSsh::assign did not expect $type for $key\n"; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 4 | ▶ | no Mouse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 4 | 1; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | # ABSTRACT: Role to read OpenSsh config files | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | __END__ | ||||||||||||||||||||||||||||