| 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 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | package Config::Model::Backend::OpenSsh::Ssh ; | ||||||||||||||
| 14 | 2 | $Config::Model::Backend::OpenSsh::Ssh::VERSION = '2.10.2.1'; | |||||||||||||
| 15 | 2 | ▶ | use Mouse ; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 2 | ▶ | use 5.10.1; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 2 | extends "Config::Model::Backend::Any" ; | |||||||||||||
| 18 | |||||||||||||||
| 19 | 2 | with ( | |||||||||||||
| 20 | 'Config::Model::Backend::OpenSsh::Role::Reader', | ||||||||||||||
| 21 | 'Config::Model::Backend::OpenSsh::Role::Writer', | ||||||||||||||
| 22 | ); | ||||||||||||||
| 23 | |||||||||||||||
| 24 | |||||||||||||||
| 25 | 2 | ▶ | use Carp ; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 26 | 2 | ▶ | use IO::File ; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 27 | 2 | ▶ | use Log::Log4perl; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 28 | 2 | ▶ | use File::Copy ; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 29 | 2 | ▶ | use File::Path ; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 30 | 2 | ▶ | use File::HomeDir ; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 31 | |||||||||||||||
| 32 | 2 | my $logger = Log::Log4perl::get_logger("Backend::OpenSsh"); | |||||||||||||
| 33 | |||||||||||||||
| 34 | 9 | ▶ | sub write { | ||||||||||||
|
Subroutine
write: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 35 | 9 | my $self = shift; | |||||||||||||
| 36 | 9 | $self->ssh_write(@_, ssh_mode => 'custom') ; | |||||||||||||
| 37 | } | ||||||||||||||
| 38 | |||||||||||||||
| 39 | |||||||||||||||
| 40 | 43 | ▶ | sub host { | ||||||||||||
|
Subroutine
host: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 41 | 43 | my ($self,$root,$key, $patterns,$comment) = @_; | |||||||||||||
| 42 | 43 | $logger->debug("host: pattern @$patterns # $comment"); | |||||||||||||
| 43 | 43 | my $hash_obj = $root->fetch_element('Host'); | |||||||||||||
| 44 | |||||||||||||||
| 45 | 43 | $logger->info("ssh: load host patterns '".join("','", @$patterns)."'"); | |||||||||||||
| 46 | 43 | my $hv = $hash_obj->fetch_with_id("@$patterns") ; | |||||||||||||
| 47 | 43 | ▶ | $hv -> annotation($comment) if $comment ; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 48 | |||||||||||||||
| 49 | 43 | $self->current_node($hv); | |||||||||||||
| 50 | } | ||||||||||||||
| 51 | |||||||||||||||
| 52 | 18 | ▶ | sub forward { | ||||||||||||
|
Subroutine
forward: called CC=10 SCAR=23.0
Pod
undocumented
| |||||||||||||||
| 53 | 18 | my ($self, $root, $key, $args, $comment, $check) = @_; | |||||||||||||
| 54 | 18 | $logger->debug("forward: $key @$args # $comment"); | |||||||||||||
| 55 | 18 | ▶ | $self->current_node = $root unless defined $self->current_node ; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | |||||||||||||||
| 57 | 18 | ▶ | my $elt_name = $key =~ /local/i ? 'Localforward' : 'RemoteForward' ; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | |||||||||||||||
| 59 | 18 | ▶ | my $v6 = ($args->[1] =~ m![/\[\]]!) ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 60 | |||||||||||||||
| 61 | 18 | ▶ | $logger->info("ssh: load $key '".join("','", @$args)."' ". ( $v6 ? 'IPv6' : 'IPv4')); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 62 | |||||||||||||||
| 63 | # cleanup possible square brackets used for IPv6 | ||||||||||||||
| 64 | 18 | foreach (@$args) { | |||||||||||||
| 65 | 36 | s/[\[\]]+//g; | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | |||||||||||||||
| 68 | # reverse enable to assign string to port even if no bind_adress | ||||||||||||||
| 69 | # is specified | ||||||||||||||
| 70 | 18 | ▶ | my $re = $v6 ? qr!/! : qr!:! ; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 71 | 18 | my ($port,$bind_adr ) = reverse split $re,$args->[0] ; | |||||||||||||
| 72 | 18 | my ($host,$host_port) = split $re,$args->[1] ; | |||||||||||||
| 73 | |||||||||||||||
| 74 | 18 | my $fw_list = $self->current_node->fetch_element($key); | |||||||||||||
| 75 | 18 | my $size = $fw_list->fetch_size; | |||||||||||||
| 76 | # this creates a new node in the list | ||||||||||||||
| 77 | 18 | my $fw_obj = $fw_list->fetch_with_id($size); | |||||||||||||
| 78 | |||||||||||||||
| 79 | # $fw_obj->store_element_value( GatewayPorts => 1 ) if $bind_adr ; | ||||||||||||||
| 80 | 18 | ▶ | $fw_obj->annotation($comment) if $comment; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 81 | |||||||||||||||
| 82 | 18 | ▶ | $fw_obj->store_element_value( ipv6 => 1) if $v6 ; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 83 | |||||||||||||||
| 84 | 18 | ▶ | $fw_obj->store_element_value( check => $check, name => 'bind_address', value => $bind_adr) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 85 | if defined $bind_adr ; | ||||||||||||||
| 86 | 18 | $fw_obj->store_element_value( check => $check, name => 'port', value => $port ); | |||||||||||||
| 87 | 18 | $fw_obj->store_element_value( check => $check, name => 'host', value => $host ); | |||||||||||||
| 88 | 18 | $fw_obj->store_element_value( check => $check, name => 'hostport', value => $host_port ); | |||||||||||||
| 89 | |||||||||||||||
| 90 | } | ||||||||||||||
| 91 | |||||||||||||||
| 92 | 9 | ▶ | sub write_all_host_block { | ||||||||||||
|
Subroutine
write_all_host_block: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||
| 93 | 9 | my $self = shift ; | |||||||||||||
| 94 | 9 | my $host_elt = shift ; | |||||||||||||
| 95 | 9 | ▶ | my $mode = shift || ''; | ||||||||||||
|
Condition50%1/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||
| 96 | |||||||||||||||
| 97 | 9 | my $result = '' ; | |||||||||||||
| 98 | |||||||||||||||
| 99 | 9 | foreach my $pattern ( $host_elt->fetch_all_indexes) { | |||||||||||||
| 100 | 19 | my $block_elt = $host_elt->fetch_with_id($pattern) ; | |||||||||||||
| 101 | 19 | $logger->debug("write_all_host_block on ".$block_elt->location." mode $mode"); | |||||||||||||
| 102 | 19 | my $block_data = $self->write_node_content($block_elt,'custom') ; | |||||||||||||
| 103 | |||||||||||||||
| 104 | # write data only if custom pattern or custom data is found this | ||||||||||||||
| 105 | # is necessary to avoid writing data from /etc/ssh/ssh_config that | ||||||||||||||
| 106 | # were entered as 'preset' data | ||||||||||||||
| 107 | 19 | ▶ | if ($block_data) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 108 | 19 | $result .= $self->write_line(Host => $pattern, $block_elt->annotation); | |||||||||||||
| 109 | 19 | $result .= "$block_data\n" ; | |||||||||||||
| 110 | } | ||||||||||||||
| 111 | } | ||||||||||||||
| 112 | 9 | return $result ; | |||||||||||||
| 113 | } | ||||||||||||||
| 114 | |||||||||||||||
| 115 | 8 | ▶ | sub write_forward { | ||||||||||||
|
Subroutine
write_forward: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||
| 116 | 8 | my $self = shift ; | |||||||||||||
| 117 | 8 | my $forward_elt = shift ; | |||||||||||||
| 118 | 8 | ▶ | my $mode = shift || ''; | ||||||||||||
|
Condition50%1/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||
| 119 | |||||||||||||||
| 120 | 8 | my $result = '' ; | |||||||||||||
| 121 | |||||||||||||||
| 122 | 8 | my $v6 = $forward_elt->grab_value('ipv6') ; | |||||||||||||
| 123 | 8 | ▶ | my $sep = $v6 ? '/' : ':'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 124 | |||||||||||||||
| 125 | 8 | my $line = ''; | |||||||||||||
| 126 | 8 | foreach my $name ($forward_elt->get_element_name() ) { | |||||||||||||
| 127 | 40 | ▶ | next if $name eq 'ipv6' ; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 128 | 32 | my $elt = $forward_elt->fetch_element($name) ; | |||||||||||||
| 129 | 32 | my $v = $elt->fetch($mode) ; | |||||||||||||
| 130 | 32 | ▶ | next unless length($v); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 131 | 28 | ▶ | $line | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 132 | .= $name =~ /bind|host$/ ? "$v$sep" | ||||||||||||||
| 133 | : $name eq 'port' ? "$v " | ||||||||||||||
| 134 | : $v ; | ||||||||||||||
| 135 | } | ||||||||||||||
| 136 | |||||||||||||||
| 137 | 8 | return $self->write_line($forward_elt->element_name,$line,$forward_elt->annotation) ; | |||||||||||||
| 138 | } | ||||||||||||||
| 139 | |||||||||||||||
| 140 | 2 | ▶ | no Mouse; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 141 | |||||||||||||||
| 142 | 2 | 1; | |||||||||||||
| 143 | |||||||||||||||
| 144 | # ABSTRACT: Backend for ssh configuration files | ||||||||||||||
| 145 | |||||||||||||||
| 146 | __END__ | ||||||||||||||