« blib/lib/Monitoring/Sneck/Checks/cert_file.pm
↑ Summary
blib/lib/Monitoring/Sneck/Checks/listening.pm »
| 1 | package Monitoring::Sneck::Checks::file_mode; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use 5.014; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use parent 'Monitoring::Sneck::Checks::base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | Monitoring::Sneck::Checks::file_mode - checks the mode, owner, and group of files | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | Version 0.0.1 | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 9 | our $VERSION = '0.0.1'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | !shadow_mode|control=CIS-6.1.3 framework=CIS|perl:file_mode -f /etc/shadow -M 0640 -o root | ||||||||||||||||||||||||||||
| 23 | !ssh_keys||perl:file_mode -f /etc/ssh/ssh_host_ed25519_key -f /etc/ssh/ssh_host_rsa_key -m 0600 -o root -g 0 | ||||||||||||||||||||||||||||
| 24 | !tmp_sticky||perl:file_mode -f /tmp -m 1777 | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | Checks each file is as given. Any that is not, or is not there, is | ||||||||||||||||||||||||||||
| 29 | CRITICAL, unless --ignore-missing is given. Symlinks are followed. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | --mode needs the mode to be exactly that, while --max-mode allows less, | ||||||||||||||||||||||||||||
| 32 | so 0640 allows 0600 and 0400 but not 0644. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =head1 OPTIONS | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | - -f, --file :: A file. May be given more than once. Required. | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | - -m, --mode :: The mode it must have, in octal, such as 0600. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | - -M, --max-mode :: The most it may have, in octal, such as 0640. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | - -o, --owner :: The user that must own it, by name or UID. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | - -g, --group :: The group that must own it, by name or GID. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | - -i, --ignore-missing :: Files not there are OK. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | One or more of --mode, --max-mode, --owner, or --group must be given. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =cut | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 10 | ▶ | sub option_spec { return ( 'file|f=s@', 'mode|m=s', 'max-mode|M=s', 'owner|o=s', 'group|g=s', 'ignore-missing|i' ) } | ||||||||||||||||||||||||||
|
Subroutine
option_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 10 | ▶ | sub required { return ('file') } | ||||||||||||||||||||||||||
|
Subroutine
required: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 10 | ▶ | sub validate { | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 10 | my ( $self, $options ) = @_; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 10 | my @problems; | |||||||||||||||||||||||||||
| 60 | 10 | foreach my $name ( 'mode', 'max-mode' ) { | |||||||||||||||||||||||||||
| 61 | push( @problems, '--' . $name . ' must be octal, such as 0640' ) | ||||||||||||||||||||||||||||
| 62 | 20 | ▶ | if defined( $options->{$name} ) && $options->{$name} !~ /\A[0-7]{3,4}\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $options->{$name} and not $options->{$name} =~ /\A[0-7]{3,4}\z/u
MC/DC100%2/2
defined $options->{$name} and not $options->{$name} =~ /\A[0-7]{3,4}\z/u
defined $options->{$name}
not $options->{$name} =~ /\A[0-7]{3,4}\z/u
Truth table100%3/3
A and not(B)
A= defined $options->{$name}B= not $options->{$name} =~ /\A[0-7]{3,4}\z/u
| |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | push( @problems, '--owner "' . $options->{owner} . '" is not a user' ) | ||||||||||||||||||||||||||||
| 65 | 10 | ▶ | if defined( $options->{owner} ) && !defined( _id( $options->{owner}, 'user' ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $options->{"owner"} and not defined _id($options->{"owner"}, "user")
MC/DC100%2/2
defined $options->{"owner"} and not defined _id($options->{"owner"}, "user")
defined $options->{"owner"}
not defined _id($options->{"owner"}, "user")
Truth table100%3/3
A and not(B)
A= defined $options->{"owner"}B= not defined _id($options->{"owner"}, "user")
| |||||||||||||||||||||||||||||
| 66 | push( @problems, '--group "' . $options->{group} . '" is not a group' ) | ||||||||||||||||||||||||||||
| 67 | 10 | ▶ | if defined( $options->{group} ) && !defined( _id( $options->{group}, 'group' ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $options->{"group"} and not defined _id($options->{"group"}, "group")
MC/DC0%0/2
defined $options->{"group"} and not defined _id($options->{"group"}, "group")
defined $options->{"group"}
not defined _id($options->{"group"}, "group")
Truth table66%2/3
A and not(B)
A= defined $options->{"group"}B= not defined _id($options->{"group"}, "group")
| |||||||||||||||||||||||||||||
| 68 | push( @problems, 'nothing to check, give --mode, --max-mode, --owner, or --group' ) | ||||||||||||||||||||||||||||
| 69 | 10 | ▶ | if !grep { defined( $options->{$_} ) } ( 'mode', 'max-mode', 'owner', 'group' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 10 | return @problems; | |||||||||||||||||||||||||||
| 72 | } ## end sub validate | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head1 METHODS | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head2 check | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | Does the check. | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =cut | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 7 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=18 SCAR=29.2
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 7 | my ($self) = @_; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 7 | my @codes; | |||||||||||||||||||||||||||
| 86 | my @problems; | ||||||||||||||||||||||||||||
| 87 | 7 | my @seen; | |||||||||||||||||||||||||||
| 88 | 7 | foreach my $file ( @{ $self->opt('file') } ) { | |||||||||||||||||||||||||||
| 89 | 7 | my @stat = stat($file); | |||||||||||||||||||||||||||
| 90 | 7 | ▶ | if ( !defined( $stat[0] ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 2 | ▶ | next if $self->opt('ignore-missing') && !-e $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->opt("ignore-missing") and not -e $file
MC/DC50%1/2
$self->opt("ignore-missing") and not -e $file
$self->opt("ignore-missing")
not -e $file
Truth table66%2/3
A and not(B)
A= $self->opt("ignore-missing")B= not -e $file
| |||||||||||||||||||||||||||||
| 92 | 1 | push( @codes, $self->CRITICAL ); | |||||||||||||||||||||||||||
| 93 | 1 | push( @problems, $file . ', ' . $! ); | |||||||||||||||||||||||||||
| 94 | 1 | next; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 5 | my $mode = $stat[2] & 07777; | |||||||||||||||||||||||||||
| 98 | 5 | ▶ | my $owner = getpwuid( $stat[4] ) // $stat[4]; | ||||||||||||||||||||||||||
|
Condition33%1/3
getpwuid $stat[4] // $stat[4]
MC/DC0%0/2
getpwuid $stat[4] // $stat[4]
getpwuid $stat[4]
$stat[4]
Truth table33%1/3
A // B
A= getpwuid $stat[4]B= $stat[4]
| |||||||||||||||||||||||||||||
| 99 | 5 | ▶ | my $group = getgrgid( $stat[5] ) // $stat[5]; | ||||||||||||||||||||||||||
|
Condition33%1/3
getgrgid $stat[5] // $stat[5]
MC/DC0%0/2
getgrgid $stat[5] // $stat[5]
getgrgid $stat[5]
$stat[5]
Truth table33%1/3
A // B
A= getgrgid $stat[5]B= $stat[5]
| |||||||||||||||||||||||||||||
| 100 | 5 | my @wrong; | |||||||||||||||||||||||||||
| 101 | 5 | ▶ | if ( defined( $self->opt('mode') ) && $mode != oct( $self->opt('mode') ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->opt("mode") and $mode != oct $self->opt("mode")
MC/DC100%2/2
defined $self->opt("mode") and $mode != oct $self->opt("mode")
defined $self->opt("mode")
$mode != oct $self->opt("mode")
Truth table100%3/3
A and B
A= defined $self->opt("mode")B= $mode != oct $self->opt("mode")
| |||||||||||||||||||||||||||||
| 102 | 1 | push( @wrong, 'mode is not ' . $self->opt('mode') ); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 5 | ▶ | if ( defined( $self->opt('max-mode') ) && ( $mode & ~oct( $self->opt('max-mode') ) ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->opt("max-mode") and $mode & ~oct($self->opt("max-mode"))
MC/DC100%2/2
defined $self->opt("max-mode") and $mode & ~oct($self->opt("max-mode"))
defined $self->opt("max-mode")
$mode & ~oct($self->opt("max-mode"))
Truth table100%3/3
A and B
A= defined $self->opt("max-mode")B= $mode & ~oct($self->opt("max-mode"))
| |||||||||||||||||||||||||||||
| 105 | 1 | push( @wrong, 'mode is more than ' . $self->opt('max-mode') ); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 5 | ▶ | if ( defined( $self->opt('owner') ) && $stat[4] != _id( $self->opt('owner'), 'user' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->opt("owner") and $stat[4] != _id($self->opt("owner"), "user")
MC/DC100%2/2
defined $self->opt("owner") and $stat[4] != _id($self->opt("owner"), "user")
defined $self->opt("owner")
$stat[4] != _id($self->opt("owner"), "user")
Truth table100%3/3
A and B
A= defined $self->opt("owner")B= $stat[4] != _id($self->opt("owner"), "user")
| |||||||||||||||||||||||||||||
| 108 | 1 | push( @wrong, 'owner is not ' . $self->opt('owner') ); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 5 | ▶ | if ( defined( $self->opt('group') ) && $stat[5] != _id( $self->opt('group'), 'group' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->opt("group") and $stat[5] != _id($self->opt("group"), "group")
MC/DC0%0/2
defined $self->opt("group") and $stat[5] != _id($self->opt("group"), "group")
defined $self->opt("group")
$stat[5] != _id($self->opt("group"), "group")
Truth table66%2/3
A and B
A= defined $self->opt("group")B= $stat[5] != _id($self->opt("group"), "group")
| |||||||||||||||||||||||||||||
| 111 | 0 | push( @wrong, 'group is not ' . $self->opt('group') ); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 5 | my $is = $file . ' ' . sprintf( '%04o', $mode ) . ' ' . $owner . ':' . $group; | |||||||||||||||||||||||||||
| 115 | 5 | push( @seen, $is ); | |||||||||||||||||||||||||||
| 116 | 5 | ▶ | if ( defined( $wrong[0] ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 2 | push( @codes, $self->CRITICAL ); | |||||||||||||||||||||||||||
| 118 | 2 | push( @problems, $is . ', ' . join( ', ', @wrong ) ); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } ## end foreach my $file ( @{ $self->opt('file') } ) | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 7 | ▶ | return $self->finish( $self->worst(@codes), join( ', ', @problems ) ) if defined( $problems[0] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 4 | ▶ | return $self->finish( $self->OK, defined( $seen[0] ) ? join( ', ', @seen ) : 'none of the files are there' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | } ## end sub check | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # Turns a user or group name into its ID. A number is taken as a ID. | ||||||||||||||||||||||||||||
| 127 | # | ||||||||||||||||||||||||||||
| 128 | # Args... | ||||||||||||||||||||||||||||
| 129 | # | ||||||||||||||||||||||||||||
| 130 | # - name :: The name or ID. | ||||||||||||||||||||||||||||
| 131 | # | ||||||||||||||||||||||||||||
| 132 | # - kind :: 'user' or 'group'. | ||||||||||||||||||||||||||||
| 133 | # | ||||||||||||||||||||||||||||
| 134 | # Returns the ID, or undef if there is no such user or group. | ||||||||||||||||||||||||||||
| 135 | # | ||||||||||||||||||||||||||||
| 136 | # Example... | ||||||||||||||||||||||||||||
| 137 | # | ||||||||||||||||||||||||||||
| 138 | # _id( 'root', 'user' ); # 0 | ||||||||||||||||||||||||||||
| 139 | # _id( '1234', 'group' ); # 1234 | ||||||||||||||||||||||||||||
| 140 | 9 | ▶ | sub _id { | ||||||||||||||||||||||||||
|
Subroutine
_id: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 141 | 9 | my ( $name, $kind ) = @_; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 9 | ▶ | return $name + 0 if $name =~ /\A[0-9]+\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 3 | ▶ | my @entry = $kind eq 'user' ? getpwnam($name) : getgrnam($name); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 3 | return $entry[2]; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 9 | 1; | |||||||||||||||||||||||||||