« blib/lib/Monitoring/Sneck/Checks/http_json.pm
↑ Summary
blib/lib/Monitoring/Sneck/Checks/pressure.pm »
| 1 | package Monitoring::Sneck::Checks::sshd_config; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.014; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use parent 'Monitoring::Sneck::Checks::base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use File::Basename qw(dirname); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use File::Glob qw(bsd_glob); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use File::Slurp qw(read_file); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | Monitoring::Sneck::Checks::sshd_config - checks settings in sshd_config | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 VERSION | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | Version 0.0.1 | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =cut | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 3 | our $VERSION = '0.0.1'; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # How deep Include may go, the same as sshd. | ||||||||||||||||||||||||||||
| 24 | 3 | our $MAX_DEPTH = 16; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | !ssh_no_root|control=CIS-5.2.10 framework=CIS|perl:sshd_config -e PermitRootLogin=no | ||||||||||||||||||||||||||||
| 29 | !ssh_auth||perl:sshd_config -e PasswordAuthentication=no -d PasswordAuthentication=yes -e 'MaxAuthTries=3|4' | ||||||||||||||||||||||||||||
| 30 | !ssh_no_x11||perl:sshd_config -f /usr/local/etc/ssh/sshd_config -e X11Forwarding=no -d X11Forwarding=no | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | Reads sshd_config, along with any files it includes, and checks the | ||||||||||||||||||||||||||||
| 35 | value of each setting given. A value that is not one of those allowed | ||||||||||||||||||||||||||||
| 36 | is CRITICAL. So is a setting that is not set and has no --default, or | ||||||||||||||||||||||||||||
| 37 | the file not being readable. | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | The settings are read the way sshd does. Names are not case sensitive, | ||||||||||||||||||||||||||||
| 40 | and the first value wins. Only the global settings are looked at, so | ||||||||||||||||||||||||||||
| 41 | everything from the first Match on is left out. Values are compared | ||||||||||||||||||||||||||||
| 42 | without case and with runs of spaces squashed, so extra spaces in the | ||||||||||||||||||||||||||||
| 43 | file do not matter. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | sshd's own defaults vary by version and OS, so they are not known. Give | ||||||||||||||||||||||||||||
| 46 | --default for any that may be unset, such as -d PermitRootLogin=prohibit-password. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head1 OPTIONS | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | - -f, --file :: The file. | ||||||||||||||||||||||||||||
| 51 | Default :: /etc/ssh/sshd_config | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | - -e, --expect :: "Name=value", a setting and the value it must have. | ||||||||||||||||||||||||||||
| 54 | Several values may be allowed, split by |, such as "MaxAuthTries=3|4". | ||||||||||||||||||||||||||||
| 55 | May be given more than once. Required. | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | - -d, --default :: "Name=value", what sshd uses if it is not set. May | ||||||||||||||||||||||||||||
| 58 | be given more than once. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | - -I, --include-dir :: The directory relative Include paths are under. | ||||||||||||||||||||||||||||
| 61 | sshd takes them from its own config directory, wherever the file it | ||||||||||||||||||||||||||||
| 62 | was given is, so give this when checking a copy kept elsewhere. | ||||||||||||||||||||||||||||
| 63 | Default :: the directory of --file | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =cut | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 9 | ▶ | sub option_spec { return ( 'file|f=s', 'expect|e=s@', 'default|d=s@', 'include-dir|I=s' ) } | ||||||||||||||||||||||||||
|
Subroutine
option_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 9 | ▶ | sub defaults { return { file => '/etc/ssh/sshd_config' } } | ||||||||||||||||||||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 9 | ▶ | sub required { return ('expect') } | ||||||||||||||||||||||||||
|
Subroutine
required: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 9 | ▶ | sub validate { | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 9 | my ( $self, $options ) = @_; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 9 | my @problems; | |||||||||||||||||||||||||||
| 77 | 9 | foreach my $name ( 'expect', 'default' ) { | |||||||||||||||||||||||||||
| 78 | 18 | ▶ | foreach my $setting ( @{ $options->{$name} // [] } ) { | ||||||||||||||||||||||||||
|
Condition100%2/2
$options->{$name} // []
MC/DC100%1/1
$options->{$name} // []
$options->{$name}
Truth table100%2/2
A
A= $options->{$name}
| |||||||||||||||||||||||||||||
| 79 | 14 | ▶ | push( @problems, '--' . $name . ' "' . $setting . '" is not Name=value' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | if $setting !~ /\A[A-Za-z0-9]+=\S/; | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 9 | return @problems; | |||||||||||||||||||||||||||
| 85 | } ## end sub validate | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =head1 METHODS | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =head2 check | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | Does the check. | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =cut | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 7 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | 7 | my ($self) = @_; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 7 | my %settings; | |||||||||||||||||||||||||||
| 99 | 7 | my $problem = $self->read_config( $self->opt('file'), \%settings, 0 ); | |||||||||||||||||||||||||||
| 100 | 7 | ▶ | return $self->finish( $self->CRITICAL, $problem ) if defined($problem); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 5 | my %defaults; | |||||||||||||||||||||||||||
| 103 | 5 | ▶ | foreach my $default ( @{ $self->opt('default') // [] } ) { | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->opt("default") // []
MC/DC100%1/1
$self->opt("default") // []
$self->opt("default")
Truth table100%2/2
A
A= $self->opt("default")
| |||||||||||||||||||||||||||||
| 104 | 2 | my ( $name, $value ) = split( /=/, $default, 2 ); | |||||||||||||||||||||||||||
| 105 | 2 | $defaults{ lc($name) } = _normal($value); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 5 | my @codes; | |||||||||||||||||||||||||||
| 109 | my @problems; | ||||||||||||||||||||||||||||
| 110 | 5 | my @seen; | |||||||||||||||||||||||||||
| 111 | 5 | foreach my $expect ( @{ $self->opt('expect') } ) { | |||||||||||||||||||||||||||
| 112 | 9 | my ( $name, $allowed ) = split( /=/, $expect, 2 ); | |||||||||||||||||||||||||||
| 113 | 9 | my $value = $settings{ lc($name) }; | |||||||||||||||||||||||||||
| 114 | 9 | my $source = ''; | |||||||||||||||||||||||||||
| 115 | 9 | ▶ | if ( !defined($value) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 3 | $value = $defaults{ lc($name) }; | |||||||||||||||||||||||||||
| 117 | 3 | $source = ' by default'; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 9 | ▶ | if ( !defined($value) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 1 | push( @codes, $self->CRITICAL ); | |||||||||||||||||||||||||||
| 121 | 1 | push( @problems, $name . ' is not set' ); | |||||||||||||||||||||||||||
| 122 | 1 | next; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 8 | my %allowed = map { _normal($_) => 1 } split( /\|/, $allowed ); | |||||||||||||||||||||||||||
| 125 | 8 | push( @seen, $name . ' ' . $value . $source ); | |||||||||||||||||||||||||||
| 126 | 8 | ▶ | next if $allowed{$value}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 3 | push( @codes, $self->CRITICAL ); | |||||||||||||||||||||||||||
| 128 | 3 | push( @problems, $name . ' is ' . $value . $source . ', not ' . join( ' or ', split( /\|/, $allowed ) ) ); | |||||||||||||||||||||||||||
| 129 | } ## end foreach my $expect ( @{ $self->opt('expect') } ) | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 5 | ▶ | return $self->finish( $self->worst(@codes), join( ', ', @problems ) ) if defined( $problems[0] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 2 | return $self->finish( $self->OK, join( ', ', @seen ) ); | |||||||||||||||||||||||||||
| 133 | } ## end sub check | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =head2 read_config | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | Reads a sshd_config file into a hash of settings, following Include. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | - file :: The file. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | - settings :: Hash ref of lower cased names to normalized values, | ||||||||||||||||||||||||||||
| 142 | added to. The first value of each wins. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | - depth :: How many Includes deep this is. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | Returns undef, or what went wrong. Once a Match is found, the settings | ||||||||||||||||||||||||||||
| 147 | hash gets the key " match", so nothing after is read. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =cut | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 28 | ▶ | sub read_config { | ||||||||||||||||||||||||||
|
Subroutine
read_config: called CC=15 SCAR=27.3
Pod
documented
| |||||||||||||||||||||||||||||
| 152 | 28 | my ( $self, $file, $settings, $depth ) = @_; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 28 | ▶ | return 'Include goes over ' . $MAX_DEPTH . ' deep at ' . $file if $depth > $MAX_DEPTH; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 27 | my $content = read_file( $file, err_mode => 'quiet' ); | |||||||||||||||||||||||||||
| 156 | 27 | ▶ | return 'can not read ' . $file . ', ' . $! if !defined($content); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 26 | foreach my $line ( split( /\r?\n/, $content ) ) { | |||||||||||||||||||||||||||
| 159 | 56 | ▶ | return undef if $settings->{' match'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 56 | $line =~ s/\A\s+|\s+\z//g; | |||||||||||||||||||||||||||
| 161 | 56 | ▶ | next if $line eq '' || $line =~ /\A#/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$line eq "" or $line =~ /\A#/u
MC/DC50%1/2
$line eq "" or $line =~ /\A#/u
$line eq ""
$line =~ /\A#/u
Truth table66%2/3
A or B
A= $line eq ""B= $line =~ /\A#/u
| |||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 51 | my ( $name, $value ) = split( /(?:\s*=\s*|\s+)/, $line, 2 ); | |||||||||||||||||||||||||||
| 164 | 51 | ▶ | $value //= ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$value //= ""
MC/DC0%0/1
$value //= ""
$value
Truth table50%1/2
A
A= $value
| |||||||||||||||||||||||||||||
| 165 | 51 | ▶ | if ( lc($name) eq 'match' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 5 | $settings->{' match'} = 1; | |||||||||||||||||||||||||||
| 167 | 5 | return undef; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | 46 | ▶ | if ( lc($name) eq 'include' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 22 | foreach my $pattern ( split( /\s+/, $value ) ) { | |||||||||||||||||||||||||||
| 171 | 22 | ▶ | $pattern = $self->include_dir . '/' . $pattern if $pattern !~ m{\A/}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 22 | foreach my $included ( sort( bsd_glob($pattern) ) ) { | |||||||||||||||||||||||||||
| 173 | 21 | my $problem = $self->read_config( $included, $settings, $depth + 1 ); | |||||||||||||||||||||||||||
| 174 | 21 | ▶ | return $problem if defined($problem); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | 5 | next; | |||||||||||||||||||||||||||
| 178 | } ## end if ( lc($name) eq 'include' ) | ||||||||||||||||||||||||||||
| 179 | 24 | ▶ | $settings->{ lc($name) } //= _normal($value); | ||||||||||||||||||||||||||
|
Condition66%2/3
$$settings{lc $name} //= _normal($value)
MC/DC0%0/2
$$settings{lc $name} //= _normal($value)
$$settings{lc $name}
_normal($value)
Truth table66%2/3
A //= B
A= $$settings{lc $name}B= _normal($value)
| |||||||||||||||||||||||||||||
| 180 | } ## end foreach my $line ( split( /\r?\n/, $content ) ) | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 4 | return undef; | |||||||||||||||||||||||||||
| 183 | } ## end sub read_config | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =head2 include_dir | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | Returns the directory relative Include paths are under, --include-dir, | ||||||||||||||||||||||||||||
| 188 | or the directory of --file. | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =cut | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 22 | ▶ | sub include_dir { | ||||||||||||||||||||||||||
|
Subroutine
include_dir: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 193 | 22 | my ($self) = @_; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 22 | ▶ | return $self->opt('include-dir') // dirname( $self->opt('file') ); | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->opt("include-dir") // dirname($self->opt("file"))
MC/DC0%0/2
$self->opt("include-dir") // dirname($self->opt("file"))
$self->opt("include-dir")
dirname($self->opt("file"))
Truth table66%2/3
A // B
A= $self->opt("include-dir")B= dirname($self->opt("file"))
| |||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | # Normalizes a value for comparing, lower casing it, dropping quotes | ||||||||||||||||||||||||||||
| 199 | # around it, and squashing runs of space. | ||||||||||||||||||||||||||||
| 200 | # | ||||||||||||||||||||||||||||
| 201 | # Args... | ||||||||||||||||||||||||||||
| 202 | # | ||||||||||||||||||||||||||||
| 203 | # - value :: The value. | ||||||||||||||||||||||||||||
| 204 | # | ||||||||||||||||||||||||||||
| 205 | # Returns the normalized value. | ||||||||||||||||||||||||||||
| 206 | # | ||||||||||||||||||||||||||||
| 207 | # Example... | ||||||||||||||||||||||||||||
| 208 | # | ||||||||||||||||||||||||||||
| 209 | # _normal('"Prohibit-Password"'); | ||||||||||||||||||||||||||||
| 210 | # # 'prohibit-password' | ||||||||||||||||||||||||||||
| 211 | 31 | ▶ | sub _normal { | ||||||||||||||||||||||||||
|
Subroutine
_normal: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 212 | 31 | my ($value) = @_; | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 31 | $value = lc($value); | |||||||||||||||||||||||||||
| 215 | 31 | $value =~ s/\A"(.*)"\z/$1/; | |||||||||||||||||||||||||||
| 216 | 31 | $value =~ s/\s+/ /g; | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 31 | return $value; | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 3 | 1; | |||||||||||||||||||||||||||