| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # | 
| 2 |  |  |  |  |  |  | # This file is part of Config-Model-OpenSsh | 
| 3 |  |  |  |  |  |  | # | 
| 4 |  |  |  |  |  |  | # This software is Copyright (c) 2008-2022 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 |  |  | 4 |  | 1998 | use strict; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 118 |  | 
| 11 | 4 |  |  | 4 |  | 18 | use warnings; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 179 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | $Config::Model::Backend::OpenSsh::Role::Reader::VERSION = '2.9.0.1'; | 
| 14 |  |  |  |  |  |  | use 5.10.1; | 
| 15 | 4 |  |  | 4 |  | 43 |  | 
|  | 4 |  |  |  |  | 13 |  | 
| 16 |  |  |  |  |  |  | use Config::Model 2.128; | 
| 17 | 4 |  |  | 4 |  | 22 |  | 
|  | 4 |  |  |  |  | 70 |  | 
|  | 4 |  |  |  |  | 170 |  | 
| 18 |  |  |  |  |  |  | use Mouse::Role ; | 
| 19 | 4 |  |  | 4 |  | 26 | requires qw(read_global_comments associates_comments_with_data); | 
|  | 4 |  |  |  |  | 6 |  | 
|  | 4 |  |  |  |  | 37 |  | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | # sub stub known as "forward" declaration | 
| 22 |  |  |  |  |  |  | # required for Role consistency checks | 
| 23 |  |  |  |  |  |  | # See Moose::Manual::Roles for details | 
| 24 |  |  |  |  |  |  | sub current_node; | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | has 'current_node'  => ( | 
| 27 |  |  |  |  |  |  | is => 'rw', | 
| 28 |  |  |  |  |  |  | isa => 'Config::Model::Node', | 
| 29 |  |  |  |  |  |  | weak_ref => 1 | 
| 30 |  |  |  |  |  |  | ) ; | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | use Carp ; | 
| 33 | 4 |  |  | 4 |  | 1428 | use Log::Log4perl 1.11; | 
|  | 4 |  |  |  |  | 12 |  | 
|  | 4 |  |  |  |  | 257 |  | 
| 34 | 4 |  |  | 4 |  | 26 |  | 
|  | 4 |  |  |  |  | 65 |  | 
|  | 4 |  |  |  |  | 23 |  | 
| 35 |  |  |  |  |  |  | my $logger = Log::Log4perl::get_logger("Backend::OpenSsh"); | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | my @dispatch = ( | 
| 38 |  |  |  |  |  |  | qr/match/i                 => 'match', | 
| 39 |  |  |  |  |  |  | qr/host\b/i                => 'host', | 
| 40 |  |  |  |  |  |  | qr/(local|remote)forward/i => 'forward', | 
| 41 |  |  |  |  |  |  | qr/^PreferredAuthentications$/ => 'comma_list', | 
| 42 |  |  |  |  |  |  | qr/localcommand/i          => 'assign', | 
| 43 |  |  |  |  |  |  | qr/\w/                     => 'assign', | 
| 44 |  |  |  |  |  |  | ); | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | my $self = shift ; | 
| 47 |  |  |  |  |  |  | my %args = @_ ; | 
| 48 | 26 |  |  | 26 | 0 | 2797180 | my $config_root = $args{object} | 
| 49 | 26 |  |  |  |  | 619 | || croak __PACKAGE__," read_ssh_file: undefined config root object"; | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 26 |  | 33 |  |  | 148 | return 0 unless $args{file_path}->is_file; | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 26 | 100 |  |  |  | 116 | $logger->info("loading config file ".$args{file_path}); | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 23 |  |  |  |  | 515 | my @lines = $args{file_path}->lines_utf8 ; | 
| 56 |  |  |  |  |  |  | # try to get global comments (comments before a blank line) | 
| 57 | 23 |  |  |  |  | 376 | $self->read_global_comments(\@lines,'#') ; | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 23 |  |  |  |  | 4111 | # need to reset this when reading user ssh file after system ssh file | 
| 60 |  |  |  |  |  |  | $self->current_node($config_root) ; | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 23 |  |  |  |  | 3799 | my @assoc = $self->associates_comments_with_data( \@lines, '#' ) ; | 
| 63 |  |  |  |  |  |  | foreach my $item (@assoc) { | 
| 64 | 23 |  |  |  |  | 113 | my ( $vdata, $comment ) = @$item; | 
| 65 | 23 |  |  |  |  | 10163 |  | 
| 66 | 314 |  |  |  |  | 848 | my ( $k, @v ) = split /\s+/, $vdata; | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 314 |  |  |  |  | 1929 | my $i = 0; | 
| 69 |  |  |  |  |  |  | while ( $i < @dispatch ) { | 
| 70 | 314 |  |  |  |  | 584 | my ( $regexp, $sub ) = @dispatch[ $i++, $i++ ]; | 
| 71 | 314 |  |  |  |  | 871 | if ( $k =~ $regexp and $self->can($sub)) { | 
| 72 | 1612 |  |  |  |  | 2819 | $logger->trace("read_ssh_file: dispatch calls $sub"); | 
| 73 | 1612 | 100 | 66 |  |  | 7128 | $self->$sub( $config_root, $k, \@v, $comment, $args{check} ); | 
| 74 | 314 |  |  |  |  | 1294 | last; | 
| 75 | 314 |  |  |  |  | 2926 | } | 
| 76 | 314 |  |  |  |  | 173446 |  | 
| 77 |  |  |  |  |  |  | warn __PACKAGE__, " unknown keyword: $k" if $i >= @dispatch; | 
| 78 |  |  |  |  |  |  | } | 
| 79 | 1298 | 50 |  |  |  | 2901 | } | 
| 80 |  |  |  |  |  |  | return 1; | 
| 81 |  |  |  |  |  |  | } | 
| 82 | 23 |  |  |  |  | 462 |  | 
| 83 |  |  |  |  |  |  | my ($self,$root, $raw_key,$arg,$comment, $check) = @_ ; | 
| 84 |  |  |  |  |  |  | $logger->debug("assign: $raw_key @$arg # $comment"); | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 3 |  |  | 3 | 0 | 10 | my @list = map { split /\s*,\s*/ } @$arg; | 
| 87 | 3 |  |  |  |  | 22 | $self->assign($root, $raw_key,\@list,$comment, $check); | 
| 88 |  |  |  |  |  |  | } | 
| 89 | 3 |  |  |  |  | 26 |  | 
|  | 3 |  |  |  |  | 17 |  | 
| 90 | 3 |  |  |  |  | 13 | my ($self,$root, $raw_key,$arg,$comment, $check) = @_ ; | 
| 91 |  |  |  |  |  |  | $logger->debug("assign: $raw_key @$arg # $comment"); | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  |  | 
| 94 | 245 |  |  | 245 | 0 | 581 | # keys are case insensitive, try to find a match | 
| 95 | 245 |  |  |  |  | 1330 | my $key = $self->current_node->find_element ($raw_key, case => 'any') ; | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | if (not defined $key) { | 
| 98 |  |  |  |  |  |  | if ($check eq 'yes') { | 
| 99 | 245 |  |  |  |  | 2207 | # drop if -force is not set | 
| 100 |  |  |  |  |  |  | die "Error: unknown parameter: '$raw_key'. Use -force option to drop this parameter\n"; | 
| 101 | 245 | 50 |  |  |  | 124011 | } | 
| 102 | 0 | 0 |  |  |  | 0 | else { | 
| 103 |  |  |  |  |  |  | say "Dropping parameter '$raw_key'" ; | 
| 104 | 0 |  |  |  |  | 0 | } | 
| 105 |  |  |  |  |  |  | return; | 
| 106 |  |  |  |  |  |  | } | 
| 107 | 0 |  |  |  |  | 0 |  | 
| 108 |  |  |  |  |  |  | my $elt = $self->current_node->fetch_element($key) ; | 
| 109 | 0 |  |  |  |  | 0 | my $type = $elt->get_type; | 
| 110 |  |  |  |  |  |  | #print "got $key type $type and ",join('+',@$arg),"\n"; | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 245 |  |  |  |  | 781 | $elt->annotation($comment) if $comment and $type ne 'hash'; | 
| 113 | 245 |  |  |  |  | 171116 |  | 
| 114 |  |  |  |  |  |  | if ($type eq 'leaf') { | 
| 115 |  |  |  |  |  |  | $elt->store( value => join(' ',@$arg), check => $check ) ; | 
| 116 | 245 | 100 | 66 |  |  | 1426 | } | 
| 117 |  |  |  |  |  |  | elsif ($type eq 'list') { | 
| 118 | 245 | 100 |  |  |  | 2745 | $elt->push_x ( values => $arg, check => $check ) ; | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
| 119 | 196 |  |  |  |  | 739 | } | 
| 120 |  |  |  |  |  |  | elsif ($type eq 'hash') { | 
| 121 |  |  |  |  |  |  | my $hv = $elt->fetch_with_id($arg->[0]); | 
| 122 | 46 |  |  |  |  | 169 | $hv->store( value => $arg->[1], check => $check ); | 
| 123 |  |  |  |  |  |  | $hv->annotation($comment) if $comment; | 
| 124 |  |  |  |  |  |  | } | 
| 125 | 3 |  |  |  |  | 15 | elsif ($type eq 'check_list') { | 
| 126 | 3 |  |  |  |  | 1522 | my @check = split /\s*,\s*/,$arg->[0] ; | 
| 127 | 3 | 50 |  |  |  | 982 | $elt->set_checked_list (\@check, check => 'skip') ; | 
| 128 |  |  |  |  |  |  | } | 
| 129 |  |  |  |  |  |  | else { | 
| 130 | 0 |  |  |  |  |  | die "OpenSsh::assign did not expect $type for $key\n"; | 
| 131 | 0 |  |  |  |  |  | } | 
| 132 |  |  |  |  |  |  | } | 
| 133 |  |  |  |  |  |  |  | 
| 134 | 0 |  |  |  |  |  | no Mouse; | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | 1; | 
| 137 |  |  |  |  |  |  |  | 
| 138 | 4 |  |  | 4 |  | 3308 | # ABSTRACT: Role to read OpenSsh config files | 
|  | 4 |  |  |  |  | 11 |  | 
|  | 4 |  |  |  |  | 32 |  | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | =pod | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | =encoding UTF-8 | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | =head1 NAME | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | Config::Model::Backend::OpenSsh::Role::Reader - Role to read OpenSsh config files | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | =head1 VERSION | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | version 2.9.0.1 | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | None. Consumed by L<Config::Model::Backend::OpenSsh::Ssh> and | 
| 156 |  |  |  |  |  |  | L<Config::Model::Backend::OpenSsh::Sshd>. | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | Read methods used by both L<Config::Model::Backend::OpenSsh::Ssh> and | 
| 161 |  |  |  |  |  |  | L<Config::Model::Backend::OpenSsh::Sshd>. | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | L<cme>, L<Config::Model>, L<Config::Model::OpenSsh> | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | =head1 AUTHOR | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | Dominique Dumont | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | This software is Copyright (c) 2008-2022 by Dominique Dumont. | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | This is free software, licensed under: | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | The GNU Lesser General Public License, Version 2.1, February 1999 | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | =cut |