blib/lib/Langertha/Holmgang/Sim/Linux/Sshd.pm

filter: Statementstmt 72.2% 203 / 281 Branchbran 58.7% 101 / 172 Conditioncond 50.4% 54 / 107 MC/DCmcdc 21.1% 11 / 52 Subroutinesub 85.0% 17 / 20 Podpod 100% 6 / 6 totaltotal 61.4% 392 / 638 CC 140 SCAR 45.2
CC
140
Cov
64%
ssh_keygen
462.0
login_denied
76.1
_match_user
56.0
CRAP 91.7
« blib/lib/Langertha/Holmgang/Sim/Linux/RedisService.pm ↑ Summary blib/lib/Langertha/Holmgang/Sim/Linux/SshAccess.pm »
1 package Langertha::Holmgang::Sim::Linux::Sshd;
2 # ABSTRACT: OpenSSH server on a simulated Linux server — sshd -t/-T, Include, effect of reload/restart, login
3  
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 our $VERSION = '0.001';
11  
12  
13 4 my %YESNO = map { $_ => 1 } qw( passwordauthentication pubkeyauthentication kbdinteractiveauthentication
14 challengeresponseauthentication usepam x11forwarding permitemptypasswords allowagentforwarding printmotd
15 printlastlog tcpkeepalive usedns strictmodes hostbasedauthentication ignorerhosts gssapiauthentication
16 kerberosauthentication disableforwarding x11uselocalhost permittty exposeauthinfo );
17 4 my %ENUM = (
18 permitrootlogin => [qw( yes no prohibit-password without-password forced-commands-only )],
19 allowtcpforwarding => [qw( yes no all local remote )],
20 allowstreamlocalforwarding => [qw( yes no all local remote )],
21 gatewayports => [qw( yes no clientspecified )],
22 compression => [qw( yes no delayed )],
23 permittunnel => [qw( yes no point-to-point ethernet )],
24 permituserenvironment => [qw( yes no )],
25 addressfamily => [qw( any inet inet6 )],
26 loglevel => [qw( quiet fatal error info verbose debug debug1 debug2 debug3 )],
27 fingerprinthash => [qw( md5 sha256 )],
28 );
29 4 my %INT = map { $_ => 1 } qw( port maxauthtries maxsessions clientaliveinterval clientalivecountmax x11displayoffset );
30 4 my %LIST = map { $_ => 1 } qw( allowusers allowgroups denyusers denygroups listenaddress port hostkey acceptenv );
31 4 my %TEXT = map { $_ => 1 } qw( logingracetime syslogfacility ciphers macs kexalgorithms hostkeyalgorithms
32 pubkeyacceptedalgorithms pubkeyacceptedkeytypes casignaturealgorithms banner authorizedkeysfile chrootdirectory
33 forcecommand authenticationmethods pidfile permitlisten permitopen versionaddendum trustedusercakeys revokedkeys
34 authorizedprincipalsfile ipqos rekeylimit maxstartups persourcemaxstartups subsystem authorizedkeyscommand
35 authorizedkeyscommanduser hostcertificate moduli securitykeyprovider setenv streamlocalbindmask
36 streamlocalbindunlink unusedconnectiontimeout channeltimeout xauthlocation );
37  
38 # Vorgaben fuer sshd -T (Auszug, Debian 12 / OpenSSH 9.2)
39 4 my @DEFAULTS = (
40 [ port => '22' ], [ addressfamily => 'any' ], [ listenaddress => '[::]:22' ], [ listenaddress => '0.0.0.0:22' ],
41 [ usepam => 'no' ], [ logingracetime => '120' ], [ x11displayoffset => '10' ], [ maxauthtries => '6' ],
42 [ maxsessions => '10' ], [ clientaliveinterval => '0' ], [ clientalivecountmax => '3' ], [ permitrootlogin => 'without-password' ],
43 [ ignorerhosts => 'yes' ], [ hostbasedauthentication => 'no' ], [ pubkeyauthentication => 'yes' ],
44 [ kerberosauthentication => 'no' ], [ gssapiauthentication => 'no' ], [ passwordauthentication => 'yes' ],
45 [ kbdinteractiveauthentication => 'yes' ], [ printmotd => 'yes' ], [ printlastlog => 'yes' ], [ x11forwarding => 'no' ],
46 [ x11uselocalhost => 'yes' ], [ permittty => 'yes' ], [ permituserenvironment => 'no' ], [ compression => 'yes' ],
47 [ tcpkeepalive => 'yes' ], [ usedns => 'no' ], [ strictmodes => 'yes' ], [ permitemptypasswords => 'no' ],
48 [ allowtcpforwarding => 'yes' ], [ allowagentforwarding => 'yes' ], [ gatewayports => 'no' ], [ permittunnel => 'no' ],
49 [ loglevel => 'INFO' ], [ syslogfacility => 'AUTH' ], [ authorizedkeysfile => '.ssh/authorized_keys .ssh/authorized_keys2' ],
50 [ maxstartups => '10:30:100' ], [ pidfile => '/run/sshd.pid' ],
51 );
52  
53 4 my $MAIN = '/etc/ssh/sshd_config';
54  
55 323 ▶sub _files { my ($dev) = @_; return ref $dev->{files} eq 'HASH' ? $dev->{files} : {} }
Subroutine _files: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $$dev{'files'} eq 'HASH' ? : 311 12 323
56  
57  
58 185 ▶sub active { my ($dev) = @_; return exists _files($dev)->{$MAIN} ? 1 : 0 }
Subroutine active: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
exists _files($dev)->{$MAIN} ? : 167 18 185
59  
60 47 ▶sub _text {
Subroutine _text: called CC=4 SCAR=16.1
61 47 my ( $dev, $p ) = @_;
62 47 my $e = _files($dev)->{$p};
63 47 ▶ return undef unless defined $e;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $e 0 47 47
64 47 ▶ return ref $e eq 'HASH' ? ( $e->{content} // '' ) : $e;
Branch: 1 branch
BranchTrueFalseTotal
ref $e eq 'HASH' ? : 0 47 47
Condition0%0/2
$$e{"content"} // ""
l!l
00
MC/DC0%0/1
$$e{"content"} // ""
$$e{"content"}
Truth table0%0/2
A
A= $$e{"content"}
Aresult
11
00
65 }
66  
67 18 ▶sub _glob {
Subroutine _glob: called CC=3 SCAR=11.1
68 18 my ( $dev, $pat ) = @_;
69 18 ▶ $pat = "/etc/ssh/$pat" unless $pat =~ m{\A/};
Branch: 1 branch
BranchTrueFalseTotal
unless $pat =~ m[\A/] 0 18 18
70 18 ▶ return ($pat) unless $pat =~ /[*?]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $pat =~ /[*?]/ 0 18 18
71 18 ( my $re = quotemeta $pat ) =~ s/\\\*/[^\/]*/g;
72 18 $re =~ s/\\\?/[^\/]/g;
73 18 return sort grep {/\A$re\z/} keys %{ _files($dev) };
74 }
75  
76  
77 19 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.3
Pod documented
78 19 my ( $dev, $file ) = @_;
79 19 ▶ $file //= $MAIN;
Condition66%2/3
$file //= $MAIN
l!l&&r!l&&!r
1090
MC/DC0%0/2
$file //= $MAIN
$file $MAIN
Truth table66%2/3
A //= B
A= $fileB= $MAIN
ABresult
1X1
011
000
80 19 my ( %set, @err );
81 19 my $bad = 0;
82 19 my $read;
83 47 ▶ $read = sub {
Subroutine __ANON__: called CC=31 SCAR=37.4
84 47 my ( $f, $depth ) = @_;
85 47 my $t = _text( $dev, $f );
86 0 ▶ if ( !defined $t ) { push @err, "$f: No such file or directory" if $depth == 0; return }
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $t) 0 47 47
if $depth == 0 0 0 0
87 47 my $n = 0;
88 47 my $in_match = 0;
89 47 for my $line ( split /\n/, $t ) {
90 119 $n++;
91 119 $line =~ s/\A\s+//;
92 119 ▶ next if $line eq '' || $line =~ /\A#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" or $line =~ /\A#/ 14 105 119
Condition66%2/3
$line eq "" or $line =~ /\A#/
l!l&&r!l&&!r
140105
MC/DC50%1/2
$line eq "" or $line =~ /\A#/
$line eq "" $line =~ /\A#/
Truth table66%2/3
A or B
A= $line eq ""B= $line =~ /\A#/
ABresult
1X1
011
000
93 105 my ( $kw, $rest ) = $line =~ /\A([^\s=]+)(?:\s*=\s*|\s+)?(.*?)\s*\z/;
94 105 ▶ next unless defined $kw;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kw 0 105 105
95 105 my $k = lc $kw;
96 105 ▶ if ( $k eq 'include' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "include") 18 87 105
97 0 ▶ if ( $rest eq '' ) { push @err, "$f line $n: missing argument."; $bad++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($rest eq "") 0 18 18
98 18 $read->( $_, $depth + 1 ) for map { _glob( $dev, $_ ) } split /\s+/, $rest;
99 18 next;
100 }
101 87 ▶ if ( $k eq 'match' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "match") 3 84 87
102 0 ▶ if ( $rest eq '' ) { push @err, "$f line $n: missing argument."; $bad++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($rest eq "") 0 3 3
103 3 $in_match = 1;
104 3 next;
105 }
106 84 ▶ if ( !$YESNO{$k} && !$ENUM{$k} && !$INT{$k} && !$LIST{$k} && !$TEXT{$k} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($YESNO{$k} or $ENUM{$k} or $INT{$k} or $LIST{$k} or $TEXT{$k}) 3 81 84
Condition100%12/12
$YESNO{$k} or $ENUM{$k} or $INT{$k} or $LIST{$k} or $TEXT{$k}
l!l&&r!l&&!r
67143
$YESNO{$k} or $ENUM{$k} or $INT{$k} or $LIST{$k}
l!l&&r!l&&!r
521517
$YESNO{$k} or $ENUM{$k} or $INT{$k}
l!l&&r!l&&!r
49332
$YESNO{$k} or $ENUM{$k}
l!l&&r!l&&!r
351435
MC/DC100%5/5
$YESNO{$k} or $ENUM{$k} or $INT{$k} or $LIST{$k} or $TEXT{$k}
$YESNO{$k} $ENUM{$k} $INT{$k} $LIST{$k} $TEXT{$k}
Truth table100%6/6
A or B or C or D or E
A= $YESNO{$k}B= $ENUM{$k}C= $INT{$k}D= $LIST{$k}E= $TEXT{$k}
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
107 3 push @err, "$f: line $n: Bad configuration option: $kw";
108 3 $bad++;
109 3 next;
110 }
111 0 ▶ if ( $rest eq '' ) { push @err, "$f line $n: missing argument."; $bad++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($rest eq "") 0 81 81
112 81 my $v = $rest;
113 81 $v =~ s/\A"(.*)"\z/$1/;
114 81 ▶ if ( ( $YESNO{$k} || $ENUM{$k} || $INT{$k} ) && $v =~ /\A\S+\s+\S/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($YESNO{$k} || $ENUM{$k} || $INT{$k} and $v =~ /\A\S+\s+\S/) 0 81 81
Condition88%8/9
$YESNO{$k} || $ENUM{$k} || $INT{$k} and $v =~ /\A\S+\s+\S/
!ll&&!rl&&r
29520
$YESNO{$k} or $ENUM{$k} or $INT{$k}
l!l&&r!l&&!r
49329
$YESNO{$k} or $ENUM{$k}
l!l&&r!l&&!r
351432
MC/DC0%0/4
$YESNO{$k} || $ENUM{$k} || $INT{$k} and $v =~ /\A\S+\s+\S/
$YESNO{$k} $ENUM{$k} $INT{$k} $v =~ /\A\S+\s+\S/
Truth table57%4/7
A or B or C and D
A= $YESNO{$k}B= $ENUM{$k}C= $INT{$k}D= $v =~ /\A\S+\s+\S/
ABCDresult
000X0
1XX00
01X00
00100
1XX11
01X11
00111
115 0 push @err, "$f line $n: keyword $kw extra arguments at end of line";
116 0 $bad++;
117 0 next;
118 }
119 81 ▶ if ( $YESNO{$k} || $ENUM{$k} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($YESNO{$k} or $ENUM{$k}) 49 32 81
Condition100%3/3
$YESNO{$k} or $ENUM{$k}
l!l&&r!l&&!r
351432
MC/DC100%2/2
$YESNO{$k} or $ENUM{$k}
$YESNO{$k} $ENUM{$k}
Truth table100%3/3
A or B
A= $YESNO{$k}B= $ENUM{$k}
ABresult
1X1
011
000
120 49 ▶ my @ok = $YESNO{$k} ? qw( yes no ) : @{ $ENUM{$k} };
Branch: 1 branch
BranchTrueFalseTotal
$YESNO{$k} ? : 35 14 49
121 49 ▶ if ( !grep { $_ eq lc $v } @ok ) { push @err, "$f line $n: unsupported option \"$v\"."; $bad++; next }
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq lc $v;} @ok) 1 48 49
122 48 $v = lc $v;
123 }
124 0 ▶ if ( $INT{$k} && $v !~ /\A\d+\z/ ) { push @err, "$f line $n: invalid integer value \"$v\"."; $bad++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($INT{$k} and not $v =~ /\A\d+\z/) 0 80 80
Condition66%2/3
$INT{$k} and not $v =~ /\A\d+\z/
!ll&&!rl&&r
7730
MC/DC0%0/2
$INT{$k} and not $v =~ /\A\d+\z/
$INT{$k} not $v =~ /\A\d+\z/
Truth table66%2/3
A and not(B)
A= $INT{$k}B= not $v =~ /\A\d+\z/
ABresult
0X0
100
111
125 80 ▶ next if $in_match;
Branch: 1 branch
BranchTrueFalseTotal
if $in_match 3 77 80
126 77 ▶ if ( $LIST{$k} ) { push @{ $set{$k} }, split /\s+/, $v; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($LIST{$k}) 18 59 77
127 59 ▶ $set{$k} = $v unless exists $set{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $set{$k} 46 13 59
128 }
129 19 };
130 19 $read->( $file, 0 );
131 19 ▶ push @err, "$file: terminating, $bad bad configuration options" if $bad;
Branch: 1 branch
BranchTrueFalseTotal
if $bad 4 15 19
132 19 return ( \%set, \@err );
133 }
134  
135 15 ▶sub _hostkey_errors {
Subroutine _hostkey_errors: called CC=10 SCAR=23.5
136 15 my ( $dev, $set ) = @_;
137 0 ▶ my @keys = $set->{hostkey} ? @{ $set->{hostkey} } : map {"/etc/ssh/ssh_host_${_}_key"} qw( rsa ecdsa ed25519 );
Branch: 1 branch
BranchTrueFalseTotal
$$set{'hostkey'} ? : 0 15 15
138 15 my @present = grep { exists _files($dev)->{$_} } @keys;
139 15 ▶ return () unless grep { m{/ssh_host_} } keys %{ _files($dev) }; # Fall ohne Host-Schluessel: nicht pruefen
Branch: 1 branch
BranchTrueFalseTotal
unless grep {m[/ssh_host_];} keys %{_files($dev);} 2 13 15
140 13 my ( @err, $good );
141 13 for my $k (@present) {
142 13 my $e = _files($dev)->{$k};
143 13 ▶ my $m = ref $e eq 'HASH' ? oct( $e->{mode} // '0600' ) : 0600;
Branch: 1 branch
BranchTrueFalseTotal
ref $e eq 'HASH' ? : 13 0 13
Condition50%1/2
$$e{"mode"} // "0600"
l!l
130
MC/DC0%0/1
$$e{"mode"} // "0600"
$$e{"mode"}
Truth table50%1/2
A
A= $$e{"mode"}
Aresult
11
00
144 13 ▶ if ( $m & 077 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m & 63) 1 12 13
145 1 push @err, '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@',
146 '@ WARNING: UNPROTECTED PRIVATE KEY FILE! @',
147 '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@',
148 sprintf( "Permissions 0%o for '%s' are too open.", $m & 07777, $k ),
149 'It is required that your private key files are NOT accessible by others.',
150 'This private key will be ignored.',
151 "Unable to load host key \"$k\": bad permissions",
152 "Unable to load host key: $k";
153 1 next;
154 }
155 12 $good++;
156 }
157 13 ▶ return ( @err, $good ? () : 'sshd: no hostkeys available -- exiting.' ) if @err || !$good;
Branch: 2 branches
BranchTrueFalseTotal
if @err or not $good 1 12 13
$good ? : 0 1 1
Condition66%2/3
@err or not $good
l!l&&r!l&&!r
1012
MC/DC50%1/2
@err or not $good
@err not $good
Truth table66%2/3
A or not(B)
A= @errB= not $good
ABresult
1X1
011
000
158 12 return ();
159 }
160  
161 # Fehler, die sshd am Start hindern (Konfiguration, kein ladbarer Host-Schluessel)
162 9 ▶sub _fatal {
Subroutine _fatal: called CC=3 SCAR=11.2
163 9 my ($dev) = @_;
164 9 my ( $set, $err ) = parse($dev);
165 9 ▶ return ( $set, @$err ) if @$err;
Branch: 1 branch
BranchTrueFalseTotal
if @$err 2 7 9
166 7 my @hk = _hostkey_errors( $dev, $set );
167 0 ▶ return ( $set, ( grep {/no hostkeys/} @hk ) ? @hk : () );
Branch: 1 branch
BranchTrueFalseTotal
grep({/no hostkeys/;} @hk) ? : 0 7 7
168 }
169  
170  
171 10 ▶sub sshd {
Subroutine sshd: called CC=18 SCAR=30.2
Pod documented
172 10 my ( $dev, $av ) = @_;
173 10 my @a = @$av;
174 10 my ( $test, $dump, $file ) = ( 0, 0, $MAIN );
175 10 ▶ while (@a) {
Branch: 1 branch
BranchTrueFalseTotal
if (@a) 12 10 22
176 12 my $x = shift @a;
177 12 ▶ if ( $x eq '-t' ) { $test = 1 }
Branch: 5 branches
BranchTrueFalseTotal
if ($x eq '-t') { } 5 7 12
elsif ($x eq '-T') { } 5 2 7
elsif ($x eq '-f') { } 0 2 2
elsif ($x =~ /\A-[tT]+\z/) { } 0 2 2
elsif ($x eq '-C') { } 2 0 2
178 5 elsif ( $x eq '-T' ) { $dump = 1 }
179 0 ▶ elsif ( $x eq '-f' ) { $file = shift(@a) // return ( 'option requires an argument -- f', 0 ) }
Condition0%0/2
shift @a // (return "option requires an argument -- f", 0)
l!l
00
MC/DC0%0/1
shift @a // (return "option requires an argument -- f", 0)
shift @a
Truth table0%0/2
A
A= shift @a
Aresult
11
00
180 0 ▶ elsif ( $x =~ /\A-[tT]+\z/ ) { $test = 1; $dump = 1 if $x =~ /T/ }
Branch: 1 branch
BranchTrueFalseTotal
if $x =~ /T/ 0 0 0
181 2 elsif ( $x eq '-C' ) { shift @a }
182 0 else { return ( "unknown option -- " . substr( $x, 1 ), 0 ) }
183 }
184 10 my ( $set, $err ) = parse( $dev, $file );
185 10 ▶ return ( join( "\n", @$err ), 0 ) if @$err;
Branch: 1 branch
BranchTrueFalseTotal
if @$err 2 8 10
186 8 my @hk = _hostkey_errors( $dev, $set );
187 8 ▶ return ( join( "\n", @hk ), 0 ) if grep {/no hostkeys/} @hk;
Branch: 1 branch
BranchTrueFalseTotal
if grep {/no hostkeys/;} @hk 1 7 8
188 7 ▶ if ($dump) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dump) 5 2 7
189 5 my ( @order, %def );
190 5 ▶ for (@DEFAULTS) { push @order, $_->[0] unless $def{ $_->[0] }; push @{ $def{ $_->[0] } }, $_->[1] }
Branch: 1 branch
BranchTrueFalseTotal
unless $def{$$_[0]} 190 5 195
191 5 my @l;
192 5 for my $k ( @order, grep { !$def{$_} } sort keys %$set ) {
193 194 ▶ my @v = exists $set->{$k} ? ( ref $set->{$k} ? @{ $set->{$k} } : $set->{$k} ) : @{ $def{$k} };
Branch: 2 branches
BranchTrueFalseTotal
exists $$set{$k} ? : 14 180 194
ref $$set{$k} ? : 5 9 14
194 194 push @l, map {"$k $_"} @v;
195 }
196 5 return ( join( "\n", @hk, @l ), 1 );
197 }
198 2 ▶ return ( join( "\n", @hk ), 1 ) if $test;
Branch: 1 branch
BranchTrueFalseTotal
if $test 2 0 2
199 0 return ( 'sshd re-exec requires execution with an absolute path', 0 );
200 }
201  
202  
203 9 ▶sub load {
Subroutine load: called CC=15 SCAR=28.6
Pod documented
204 9 my ( $dev, $name, $verb, $host ) = @_;
205 9 ▶ my $svc = $dev->{services}{$name} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $svc = $$dev{"services"}{$name} or return 9 0 9
206 9 my ( $set, @fatal ) = _fatal($dev);
207 9 ▶ if (@fatal) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fatal) 2 7 9
208 2 ▶ $host //= $dev->{hostname} // 'localhost';
Condition25%1/4
$host //= $$dev{"hostname"} // "localhost"
l!l
20
$$dev{"hostname"} // "localhost"
l!l
00
MC/DC0%0/1
$host //= $$dev{"hostname"} // "localhost"
$host
Truth table50%1/2
A
A= $host
Aresult
11
00
209 2 ▶ if ( $verb ne 'reload' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verb ne "reload") 1 1 2
210 1 ▶ $svc->{active} = JSON::MaybeXS::false() if $svc->{active};
Branch: 1 branch
BranchTrueFalseTotal
if $$svc{"active"} 1 0 1
211 1 ▶ $svc->{failed} = JSON::MaybeXS::true() unless $svc->{failed};
Branch: 1 branch
BranchTrueFalseTotal
unless $$svc{"failed"} 1 0 1
212 }
213 2 push @{ $svc->{journal} }, map {"Oct 20 09:14:07 $host sshd[2207]: $_"} grep { !/\A@/ } @fatal;
214 2 die "Job for $name.service failed because the control process exited with error code.\n"
215 . "See \"systemctl status $name.service\" and \"journalctl -xeu $name.service\" for details.\n";
216 }
217 7 ▶ my $old = JSON::MaybeXS->new( canonical => 1 )->encode( $svc->{effective} // {} );
Condition50%1/2
$$svc{"effective"} // {}
l!l
70
MC/DC0%0/1
$$svc{"effective"} // {}
$$svc{"effective"}
Truth table50%1/2
A
A= $$svc{"effective"}
Aresult
11
00
218 7 ▶ $svc->{effective} = $set unless JSON::MaybeXS->new( canonical => 1 )->encode($set) eq $old;
Branch: 1 branch
BranchTrueFalseTotal
unless "JSON::MaybeXS"->new("canonical", 1)->encode($set) eq $old 4 3 7
219 # wie im echten Journal: sshd lauscht wieder, systemd meldet den Start bzw. das Neuladen
220 7 ▶ $host //= $dev->{hostname} // 'localhost';
Condition25%1/4
$host //= $$dev{"hostname"} // "localhost"
l!l
70
$$dev{"hostname"} // "localhost"
l!l
00
MC/DC0%0/1
$host //= $$dev{"hostname"} // "localhost"
$host
Truth table50%1/2
A
A= $host
Aresult
11
00
221 7 ▶ my @ports = @{ $set->{port} || [22] };
Condition50%1/2
$$set{'port'} || [22]
l!l
07
MC/DC0%0/1
$$set{'port'} || [22]
$$set{'port'}
Truth table50%1/2
A
A= $$set{'port'}
Aresult
11
00
222 7 ▶ push @{ $svc->{journal} },
Branch: 1 branch
BranchTrueFalseTotal
$verb eq 'reload' ? : 6 1 7
223 ( $verb eq 'reload' ? "Oct 20 09:14:07 $host sshd[701]: Received SIGHUP; restarting." : "Oct 20 09:14:07 $host systemd[1]: Starting $name.service - OpenBSD Secure Shell server..." ),
224 7 ▶ ( map { ( "Oct 20 09:14:07 $host sshd[2207]: Server listening on 0.0.0.0 port $_.", "Oct 20 09:14:07 $host sshd[2207]: Server listening on :: port $_." ) } @ports ),
Branch: 1 branch
BranchTrueFalseTotal
$verb eq 'reload' ? : 6 1 7
225 "Oct 20 09:14:07 $host systemd[1]: " . ( $verb eq 'reload' ? 'Reloaded' : 'Started' ) . " $name.service - OpenBSD Secure Shell server.";
226 7 return;
227 }
228  
229  
230 0 ▶sub ssh_keygen {
Subroutine ssh_keygen: not called CC=21 SCAR=61.4
Pod documented
231 0 my ( $dev, $av, $host ) = @_;
232 0 my %o;
233 0 my @a = @$av;
234 0 my $file;
235 0 ▶ while (@a) {
Branch: 1 branch
BranchTrueFalseTotal
if (@a) 0 0 0
236 0 my $x = shift @a;
237 0 ▶ if ( $x eq '-f' ) { $file = shift @a; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($x eq "-f") 0 0 0
238 0 ▶ if ( $x =~ /\A-([A-Za-z]+)\z/ ) { $o{$_} = 1 for split //, $1; $file = shift @a if $x =~ /f\z/; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($x =~ /\A-([A-Za-z]+)\z/) 0 0 0
if $x =~ /f\z/ 0 0 0
239 0 return undef;
240 }
241 0 ▶ my $files = $dev->{files} //= {};
Condition0%0/2
$$dev{"files"} //= {}
l!l
00
MC/DC0%0/1
$$dev{"files"} //= {}
$$dev{"files"}
Truth table0%0/2
A
A= $$dev{"files"}
Aresult
11
00
242 0 ▶ if ( $o{A} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o{"A"}) 0 0 0
243 0 my @made;
244 0 for my $t (qw( rsa ecdsa ed25519 )) {
245 0 my $k = "/etc/ssh/ssh_host_${t}_key";
246 0 ▶ next if exists $files->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$files{$k} 0 0 0
247 0 $files->{$k} = { content => "-----BEGIN OPENSSH PRIVATE KEY-----\n(neu erzeugt)\n-----END OPENSSH PRIVATE KEY-----\n", mode => '0600' };
248 0 ▶ $files->{"$k.pub"} = { content => 'ssh-' . ( $t eq 'ecdsa' ? 'ecdsa-sha2-nistp256' : $t ) . " AAAA(neu) root\@$host\n", mode => '0644' };
Branch: 1 branch
BranchTrueFalseTotal
$t eq 'ecdsa' ? : 0 0 0
249 0 push @made, uc $t;
250 }
251 0 ▶ return ( @made ? 'ssh-keygen: generating new host keys: ' . join( ' ', @made ) . ' ' : '', 1 );
Branch: 1 branch
BranchTrueFalseTotal
@made ? : 0 0 0
252 }
253 0 ▶ if ( $o{l} && defined $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o{"l"} and defined $file) 0 0 0
Condition0%0/3
$o{"l"} and defined $file
!ll&&!rl&&r
000
MC/DC0%0/2
$o{"l"} and defined $file
$o{"l"} defined $file
Truth table0%0/3
A and B
A= $o{"l"}B= defined $file
ABresult
0X0
100
111
254 0 ( my $pub = $file ) =~ s/(?<!\.pub)\z/.pub/;
255 0 ▶ my $e = $files->{$pub} // $files->{$file};
Condition0%0/3
$$files{$pub} // $$files{$file}
l!l&&r!l&&!r
000
MC/DC0%0/2
$$files{$pub} // $$files{$file}
$$files{$pub} $$files{$file}
Truth table0%0/3
A // B
A= $$files{$pub}B= $$files{$file}
ABresult
1X1
011
000
256 0 ▶ return ( "ssh-keygen: $file: No such file or directory", 0 ) unless defined $e;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $e 0 0 0
257 0 my ($t) = $file =~ /ssh_host_(\w+)_key/;
258 0 ▶ my $bits = { rsa => 3072, ecdsa => 256, ed25519 => 256 }->{ $t // '' } // 256;
Condition0%0/4
{"rsa", 3072, "ecdsa", 256, "ed25519", 256}->{$t // ""} // 256
l!l
00
$t // ""
l!l
00
MC/DC0%0/1
{"rsa", 3072, "ecdsa", 256, "ed25519", 256}->{$t // ""} // 256
{"rsa", 3072, "ecdsa", 256, "ed25519", 256}->{$t // ""}
MC/DC0%0/1
$t // ""
$t
Truth table0%0/2
A
A= {"rsa", 3072, "ecdsa", 256, "ed25519", 256}->{$t // ""}
Aresult
11
00
Truth table0%0/2
A
A= $t
Aresult
11
00
259 0 ▶ my $c = ref $e eq 'HASH' ? ( $e->{content} // '' ) : $e;
Branch: 1 branch
BranchTrueFalseTotal
ref $e eq 'HASH' ? : 0 0 0
Condition0%0/2
$$e{"content"} // ""
l!l
00
MC/DC0%0/1
$$e{"content"} // ""
$$e{"content"}
Truth table0%0/2
A
A= $$e{"content"}
Aresult
11
00
260 0 my $fp = substr( join( '', map { sprintf '%02x', ord } split //, $c ) . ( '0' x 50 ), 7, 43 );
261 0 ▶ return ( "$bits SHA256:$fp root\@$host (" . uc( $t // 'ED25519' ) . ')', 1 );
Condition0%0/2
$t // "ED25519"
l!l
00
MC/DC0%0/1
$t // "ED25519"
$t
Truth table0%0/2
A
A= $t
Aresult
11
00
262 }
263 0 return undef;
264 }
265  
266 369 ▶sub _match_pat {
Subroutine _match_pat: called CC=2 SCAR=6.9
267 369 my ( $pat, $v ) = @_;
268 369 ( my $re = quotemeta $pat ) =~ s/\\\*/.*/g;
269 369 $re =~ s/\\\?/./g;
270 369 ▶ return $v =~ /\A$re\z/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$v =~ /\A$re\z/ ? : 122 247 369
271 }
272  
273 0 ▶sub _match_user {
Subroutine _match_user: not called CC=7 SCAR=40.3
274 0 my ( $pat, $user, $from ) = @_;
275 0 my ( $u, $h ) = split /@/, $pat, 2;
276 0 ▶ return 0 unless _match_pat( $u, $user );
Branch: 1 branch
BranchTrueFalseTotal
unless _match_pat($u, $user) 0 0 0
277 0 ▶ return 1 unless defined $h;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h 0 0 0
278 0 ▶ return 0 unless defined $from;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from 0 0 0
279 0 ▶ if ( $h =~ m{\A(\d+\.\d+\.\d+\.\d+)/(\d+)\z} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h =~ m[\A(\d+\.\d+\.\d+\.\d+)/(\d+)\z]) 0 0 0
280 0 my ( $net, $len ) = ( $1, $2 );
281 0 ▶ my $n = sub { my $x = 0; $x = $x * 256 + $_ for split /\./, $_[0]; $x };
Subroutine __ANON__: not called CC=2 SCAR=17.9
282 0 ▶ my $mask = $len == 0 ? 0 : ( 0xFFFFFFFF << ( 32 - $len ) ) & 0xFFFFFFFF;
Branch: 1 branch
BranchTrueFalseTotal
$len == 0 ? : 0 0 0
283 0 ▶ return ( ( $n->($net) & $mask ) == ( $n->($from) & $mask ) ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
(&$n($net) & $mask) == (&$n($from) & $mask) ? : 0 0 0
284 }
285 0 return _match_pat( $h, $from );
286 }
287  
288  
289 146 ▶sub login_denied {
Subroutine login_denied: called CC=31 SCAR=43.3
Pod documented
290 146 my ( $dev, $host ) = @_;
291 146 my $l = $dev->{ssh_login};
292 146 ▶ return undef unless ref $l eq 'HASH' && active($dev);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $l eq "HASH" and active($dev) 0 146 146
Condition33%1/3
ref $l eq "HASH" and active($dev)
!ll&&!rl&&r
00146
MC/DC0%0/2
ref $l eq "HASH" and active($dev)
ref $l eq "HASH" active($dev)
Truth table33%1/3
A and B
A= ref $l eq "HASH"B= active($dev)
ABresult
0X0
100
111
293 146 ▶ my $svc = ( $dev->{services} || {} )->{ $l->{service} // 'ssh' } || {};
Condition50%3/6
($$dev{'services'} || {})->{$$l{'service'} // 'ssh'} || {}
l!l
1460
$$dev{'services'} || {}
l!l
1460
$$l{"service"} // "ssh"
l!l
0146
MC/DC0%0/1
($$dev{'services'} || {})->{$$l{'service'} // 'ssh'} || {}
$$dev{'services'}
MC/DC0%0/1
$$l{"service"} // "ssh"
$$l{"service"}
Truth table50%1/2
A
A= $$dev{'services'}
Aresult
11
00
Truth table50%1/2
A
A= $$l{"service"}
Aresult
11
00
294 146 ▶ return "ssh: connect to host $host port 22: Connection refused" unless $svc->{active};
Branch: 1 branch
BranchTrueFalseTotal
unless $$svc{"active"} 3 143 146
295 143 ▶ my $user = $l->{user} // 'root';
Condition50%1/2
$$l{"user"} // "root"
l!l
1430
MC/DC0%0/1
$$l{"user"} // "root"
$$l{"user"}
Truth table50%1/2
A
A= $$l{"user"}
Aresult
11
00
296 143 my $from = $l->{from};
297 143 ▶ my $eff = $svc->{effective} || {};
Condition50%1/2
$$svc{'effective'} || {}
l!l
1430
MC/DC0%0/1
$$svc{'effective'} || {}
$$svc{'effective'}
Truth table50%1/2
A
A= $$svc{'effective'}
Aresult
11
00
298 143 my $no = "$user\@$host: Permission denied (publickey).";
299 143 ▶ return $no if ( $eff->{pubkeyauthentication} // 'yes' ) eq 'no';
Branch: 1 branch
BranchTrueFalseTotal
if ($$eff{"pubkeyauthentication"} // "yes") eq "no" 0 143 143
Condition50%1/2
$$eff{"pubkeyauthentication"} // "yes"
l!l
0143
MC/DC0%0/1
$$eff{"pubkeyauthentication"} // "yes"
$$eff{"pubkeyauthentication"}
Truth table50%1/2
A
A= $$eff{"pubkeyauthentication"}
Aresult
11
00
300 143 ▶ if ( $user eq 'root' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user eq 'root') { } 0 143 143
301 0 ▶ return $no if ( $eff->{permitrootlogin} // 'prohibit-password' ) eq 'no';
Branch: 1 branch
BranchTrueFalseTotal
if ($$eff{"permitrootlogin"} // "prohibit-password") eq "no" 0 0 0
Condition0%0/2
$$eff{"permitrootlogin"} // "prohibit-password"
l!l
00
MC/DC0%0/1
$$eff{"permitrootlogin"} // "prohibit-password"
$$eff{"permitrootlogin"}
Truth table0%0/2
A
A= $$eff{"permitrootlogin"}
Aresult
11
00
302 }
303 else {
304 143 ▶ my $u = ( $dev->{users} || {} )->{$user} or return $no;
Branch: 1 branch
BranchTrueFalseTotal
my $u = ($$dev{"users"} || {})->{$user} or return $no 143 0 143
Condition50%1/2
$$dev{'users'} || {}
l!l
1430
MC/DC0%0/1
$$dev{'users'} || {}
$$dev{'users'}
Truth table50%1/2
A
A= $$dev{'users'}
Aresult
11
00
305 143 ▶ return $no if $u->{expired};
Branch: 1 branch
BranchTrueFalseTotal
if $$u{"expired"} 0 143 143
306 143 ▶ return $no if ( $u->{shell} // '/bin/bash' ) =~ m{/(?:nologin|false)\z};
Branch: 1 branch
BranchTrueFalseTotal
if ($$u{"shell"} // "/bin/bash") =~ m[/(?:nologin|false)\z] 1 142 143
Condition50%1/2
$$u{"shell"} // "/bin/bash"
l!l
1430
MC/DC0%0/1
$$u{"shell"} // "/bin/bash"
$$u{"shell"}
Truth table50%1/2
A
A= $$u{"shell"}
Aresult
11
00
307 }
308 142 ▶ my @groups = $user eq 'root' ? ('root') : @{ ( ( $dev->{users} || {} )->{$user} || {} )->{groups} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$user eq 'root' ? : 0 142 142
Condition50%3/6
(($$dev{'users'} || {})->{$user} || {})->{'groups'} || []
l!l
1420
($$dev{'users'} || {})->{$user} || {}
l!l
1420
$$dev{'users'} || {}
l!l
1420
MC/DC0%0/1
(($$dev{'users'} || {})->{$user} || {})->{'groups'} || []
$$dev{'users'}
Truth table50%1/2
A
A= $$dev{'users'}
Aresult
11
00
309 0 ▶ return $no if grep { _match_user( $_, $user, $from ) } @{ $eff->{denyusers} || [] };
Branch: 1 branch
BranchTrueFalseTotal
if grep {_match_user($_, $user, $from);} @{$$eff{"denyusers"} or [];} 0 142 142
Condition50%1/2
$$eff{'denyusers'} || []
l!l
0142
MC/DC0%0/1
$$eff{'denyusers'} || []
$$eff{'denyusers'}
Truth table50%1/2
A
A= $$eff{'denyusers'}
Aresult
11
00
310 0 ▶ return $no if $eff->{allowusers} && !grep { _match_user( $_, $user, $from ) } @{ $eff->{allowusers} };
Branch: 1 branch
BranchTrueFalseTotal
if $$eff{"allowusers"} and not grep({_match_user($_, $user, $from);} @{$$eff{"allowusers"};}) 0 142 142
Condition33%1/3
$$eff{"allowusers"} and not grep({_match_user($_, $user, $from);} @{$$eff{"allowusers"};})
!ll&&!rl&&r
14200
MC/DC0%0/2
$$eff{"allowusers"} and not grep({_match_user($_, $user, $from);} @{$$eff{"allowusers"};})
$$eff{"allowusers"} not grep({_match_user($_, $user, $from);} @{$$eff{"allowusers"};})
Truth table33%1/3
A and not(B)
A= $$eff{"allowusers"}B= not grep({_match_user($_, $user, $from);} @{$$eff{"allowusers"};})
ABresult
0X0
100
111
311 142 ▶ my $gm = sub { my $p = shift; grep { _match_pat( $p, $_ ) } @groups };
Subroutine __ANON__: called CC=1 SCAR=0.0
312 0 ▶ return $no if grep { $gm->($_) } @{ $eff->{denygroups} || [] };
Branch: 1 branch
BranchTrueFalseTotal
if grep {&$gm($_);} @{$$eff{"denygroups"} or [];} 0 142 142
Condition50%1/2
$$eff{'denygroups'} || []
l!l
0142
MC/DC0%0/1
$$eff{'denygroups'} || []
$$eff{'denygroups'}
Truth table50%1/2
A
A= $$eff{'denygroups'}
Aresult
11
00
313 142 ▶ return $no if $eff->{allowgroups} && !grep { $gm->($_) } @{ $eff->{allowgroups} };
Branch: 1 branch
BranchTrueFalseTotal
if $$eff{"allowgroups"} and not grep({&$gm($_);} @{$$eff{"allowgroups"};}) 1 141 142
Condition100%3/3
$$eff{"allowgroups"} and not grep({&$gm($_);} @{$$eff{"allowgroups"};})
!ll&&!rl&&r
191221
MC/DC100%2/2
$$eff{"allowgroups"} and not grep({&$gm($_);} @{$$eff{"allowgroups"};})
$$eff{"allowgroups"} not grep({&$gm($_);} @{$$eff{"allowgroups"};})
Truth table100%3/3
A and not(B)
A= $$eff{"allowgroups"}B= not grep({&$gm($_);} @{$$eff{"allowgroups"};})
ABresult
0X0
100
111
314 141 return undef;
315 }
316  
317 4 1;
318  
319 __END__
« blib/lib/Langertha/Holmgang/Sim/Linux/RedisService.pm ↑ Summary blib/lib/Langertha/Holmgang/Sim/Linux/SshAccess.pm »