| 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 |  | 1889 | use strict; | 
|  | 4 |  |  |  |  | 9 |  | 
|  | 4 |  |  |  |  | 122 |  | 
| 11 | 4 |  |  | 4 |  | 19 | use warnings; | 
|  | 4 |  |  |  |  | 10 |  | 
|  | 4 |  |  |  |  | 189 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | $Config::Model::Backend::OpenSsh::Role::Writer::VERSION = '2.9.0.2'; | 
| 14 |  |  |  |  |  |  | use Mouse::Role ; | 
| 15 | 4 |  |  | 4 |  | 21 |  | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 24 |  | 
| 16 |  |  |  |  |  |  | with 'Config::Model::Backend::OpenSsh::Role::MatchBlock'; | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | requires qw(write_global_comments write_data_and_comments); | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | use 5.10.1; | 
| 21 | 4 |  |  | 4 |  | 1307 |  | 
|  | 4 |  |  |  |  | 12 |  | 
| 22 |  |  |  |  |  |  | use Config::Model 2.128; | 
| 23 | 4 |  |  | 4 |  | 24 |  | 
|  | 4 |  |  |  |  | 52 |  | 
|  | 4 |  |  |  |  | 146 |  | 
| 24 |  |  |  |  |  |  | use Carp ; | 
| 25 | 4 |  |  | 4 |  | 20 | use IO::File ; | 
|  | 4 |  |  |  |  | 6 |  | 
|  | 4 |  |  |  |  | 235 |  | 
| 26 | 4 |  |  | 4 |  | 21 | use Log::Log4perl 1.11; | 
|  | 4 |  |  |  |  | 13 |  | 
|  | 4 |  |  |  |  | 573 |  | 
| 27 | 4 |  |  | 4 |  | 25 |  | 
|  | 4 |  |  |  |  | 53 |  | 
|  | 4 |  |  |  |  | 32 |  | 
| 28 |  |  |  |  |  |  | my $logger = Log::Log4perl::get_logger("Backend::OpenSsh"); | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | my $self = shift ; | 
| 31 |  |  |  |  |  |  | my %args = @_ ; | 
| 32 | 12 |  |  | 12 | 0 | 30 |  | 
| 33 | 12 |  |  |  |  | 62 | my $config_root = $args{object} | 
| 34 |  |  |  |  |  |  | || croak __PACKAGE__," ssh_write: undefined config root object"; | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 12 |  | 33 |  |  | 73 | $logger->info("writing config file $args{file_path}"); | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 12 |  |  |  |  | 63 | my $comment = $self->write_global_comment('#') ; | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 12 |  |  |  |  | 225 | my $result = $self->write_node_content($config_root,$args{ssh_mode}); | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 12 |  |  |  |  | 642 | if ($result) { | 
| 43 |  |  |  |  |  |  | $args{file_path}->spew_utf8($comment.$result); | 
| 44 | 12 | 100 |  |  |  | 52 | return 1; | 
| 45 | 10 |  |  |  |  | 148 | } | 
| 46 | 10 |  |  |  |  | 6915 |  | 
| 47 |  |  |  |  |  |  | return 0; | 
| 48 |  |  |  |  |  |  | } | 
| 49 | 2 |  |  |  |  | 17 |  | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | my ($self, $k, $v, $note) = @_ ; | 
| 52 |  |  |  |  |  |  | return '' unless length($v) ; | 
| 53 |  |  |  |  |  |  | return $self->write_data_and_comments('#',sprintf("%-20s %s",$k,$v),$note) ; | 
| 54 | 2572 |  |  | 2572 | 0 | 9112 | } | 
| 55 | 2572 | 100 |  |  |  | 8392 |  | 
| 56 | 116 |  |  |  |  | 920 | my ($self,$name,$mode,$elt) = @_; | 
| 57 |  |  |  |  |  |  | my @r = map { $self->write_line($name,$_->fetch($mode), $_->annotation) ;} $elt->fetch_all() ; | 
| 58 |  |  |  |  |  |  | return join('',@r) ; | 
| 59 |  |  |  |  |  |  | } | 
| 60 | 240 |  |  | 240 | 0 | 515 |  | 
| 61 | 240 |  |  |  |  | 793 |  | 
|  | 21 |  |  |  |  | 1765 |  | 
| 62 | 240 |  |  |  |  | 9692 | my ($self,$name,$mode,$elt) = @_; | 
| 63 |  |  |  |  |  |  | my @v = $elt->fetch_all_values(mode => $mode) ; | 
| 64 |  |  |  |  |  |  | return $self->write_line($name,join(' ',@v)) ; | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 8 |  |  | 8 | 0 | 25 | # list there list element that must be written on one line with items | 
| 68 | 8 |  |  |  |  | 36 | # separated by a white space | 
| 69 | 8 |  |  |  |  | 1227 | my %list_as_one_line = ( | 
| 70 |  |  |  |  |  |  | 'AuthorizedKeysFile' => 1 , | 
| 71 |  |  |  |  |  |  | ) ; | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | my $self= shift ; | 
| 74 |  |  |  |  |  |  | my $node = shift ; | 
| 75 |  |  |  |  |  |  | my $mode = shift || ''; | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | my $result = '' ; | 
| 78 |  |  |  |  |  |  | my $match  = '' ; | 
| 79 | 30 |  |  | 30 | 0 | 64 |  | 
| 80 | 30 |  |  |  |  | 72 | foreach my $name ($node->get_element_name() ) { | 
| 81 | 30 |  | 100 |  |  | 121 | next unless $node->is_element_defined($name) ; | 
| 82 |  |  |  |  |  |  | my $elt = $node->fetch_element($name) ; | 
| 83 | 30 |  |  |  |  | 67 | my $type = $elt->get_type; | 
| 84 | 30 |  |  |  |  | 60 | my $note = $elt->annotation ; | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 30 |  |  |  |  | 124 | #print "got $key type $type and ",join('+',@arg),"\n"; | 
| 87 | 2833 | 50 |  |  |  | 48873 | if ($name eq 'Match') { | 
| 88 | 2833 |  |  |  |  | 23722 | $match .= $self->write_all_match_block($elt,$mode) ; | 
| 89 | 2833 |  |  |  |  | 173017 | } | 
| 90 | 2833 |  |  |  |  | 9775 | elsif ($name eq 'Host') { | 
| 91 |  |  |  |  |  |  | $match .= $self->write_all_host_block($elt,$mode) ; | 
| 92 |  |  |  |  |  |  | } | 
| 93 | 2833 | 100 |  |  |  | 24377 | elsif ($name =~ /^(Local|Remote)Forward$/) { | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
| 94 | 12 |  |  |  |  | 70 | foreach ($elt->fetch_all()) { | 
| 95 |  |  |  |  |  |  | $result .= $self->write_forward($_,$mode); | 
| 96 |  |  |  |  |  |  | } | 
| 97 | 8 |  |  |  |  | 42 | } | 
| 98 |  |  |  |  |  |  | elsif ($type eq 'leaf') { | 
| 99 |  |  |  |  |  |  | my $v = $elt->fetch($mode) ; | 
| 100 | 44 |  |  |  |  | 198 | $result .= $self->write_line($name,$v,$note); | 
| 101 | 6 |  |  |  |  | 544 | } | 
| 102 |  |  |  |  |  |  | elsif ($type eq 'check_list') { | 
| 103 |  |  |  |  |  |  | my $v = $elt->fetch($mode) ; | 
| 104 |  |  |  |  |  |  | $result .= $self->write_line($name,$v,$note); | 
| 105 | 2517 |  |  |  |  | 5368 | } | 
| 106 | 2517 |  |  |  |  | 440547 | elsif ($type eq 'list') { | 
| 107 |  |  |  |  |  |  | $result .= $self->write_data_and_comments('#', undef, $note) ; | 
| 108 |  |  |  |  |  |  | $result .= $list_as_one_line{$name} ? $self->write_list_in_one_line($name,$mode,$elt) | 
| 109 | 0 |  |  |  |  | 0 | :                             $self->write_list($name,$mode,$elt) ; | 
| 110 | 0 |  |  |  |  | 0 | } | 
| 111 |  |  |  |  |  |  | elsif ($type eq 'hash') { | 
| 112 |  |  |  |  |  |  | foreach my $k ( $elt->fetch_all_indexes ) { | 
| 113 | 248 |  |  |  |  | 808 | my $o = $elt->fetch_with_id($k); | 
| 114 | 248 | 100 |  |  |  | 4391 | my $v = $o->fetch($mode) ; | 
| 115 |  |  |  |  |  |  | $result .=  $self->write_line($name,"$k $v", $o->annotation) ; | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  | } | 
| 118 | 4 |  |  |  |  | 22 | else { | 
| 119 | 2 |  |  |  |  | 65 | die "OpenSsh::write did not expect $type for $name\n"; | 
| 120 | 2 |  |  |  |  | 120 | } | 
| 121 | 2 |  |  |  |  | 359 | } | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | return $result.$match ; | 
| 124 |  |  |  |  |  |  | } | 
| 125 | 0 |  |  |  |  | 0 |  | 
| 126 |  |  |  |  |  |  | no Mouse; | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | 1; | 
| 129 | 30 |  |  |  |  | 640 |  | 
| 130 |  |  |  |  |  |  | # ABSTRACT: Role to write OpenSsh config files | 
| 131 |  |  |  |  |  |  |  | 
| 132 | 4 |  |  | 4 |  | 2841 |  | 
|  | 4 |  |  |  |  | 11 |  | 
|  | 4 |  |  |  |  | 25 |  | 
| 133 |  |  |  |  |  |  | =pod | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | =encoding UTF-8 | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | =head1 NAME | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | Config::Model::Backend::OpenSsh::Role::Writer - Role to write OpenSsh config files | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | =head1 VERSION | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | version 2.9.0.2 | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | None. Consumed by L<Config::Model::Backend::OpenSsh::Ssh> and | 
| 148 |  |  |  |  |  |  | L<Config::Model::Backend::OpenSsh::Sshd>. | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | Write methods used by both L<Config::Model::Backend::OpenSsh::Ssh> and | 
| 153 |  |  |  |  |  |  | L<Config::Model::Backend::OpenSsh::Sshd>. | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | L<cme>, L<Config::Model>, L<Config::Model::OpenSsh> | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | =head1 AUTHOR | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | Dominique Dumont | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | This software is Copyright (c) 2008-2022 by Dominique Dumont. | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | This is free software, licensed under: | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | The GNU Lesser General Public License, Version 2.1, February 1999 | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | =cut |