blib/lib/Monitoring/Sneck/Checks/accounts.pm

filter: Statementstmt 100% 104 / 104 Branchbran 86.9% 40 / 46 Conditioncond 71.1% 32 / 45 MC/DCmcdc 46.1% 12 / 26 Subroutinesub 100% 11 / 11 Podpod 100% 6 / 6 totaltotal 86.1% 205 / 238 CC 46 SCAR 28.2
CC
46
Cov
90%
check
26.0
passwords
14.7
accounts
9.4
CRAP 16.8
« blib/lib/Monitoring/Sneck/Checks/resolv.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/log.pm »
1 package Monitoring::Sneck::Checks::accounts;
2  
3 7 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Monitoring::Sneck::Checks::accounts - checks local user accounts
12  
13 =head1 VERSION
14  
15 Version 0.0.1
16  
17 =cut
18  
19 7 our $VERSION = '0.0.1';
20  
21 # Where things are. Set by tests.
22 7 our $PASSWD = '/etc/passwd';
23 7 our $SHADOW = '/etc/shadow';
24 7 our $MASTER_PASSWD = '/etc/master.passwd';
25 7 our $SHELLS = '/etc/shells';
26  
27 =head1 SYNOPSIS
28  
29 !accounts|control=CIS-6.2 framework=CIS|perl:accounts -s
30 !no_guest||perl:accounts -a guest -a test -p backup
31  
32 =head1 DESCRIPTION
33  
34 Checks the local accounts, from the files, not through NSS, so those
35 from LDAP and the like are not looked at. Each of these is CRITICAL.
36  
37 - a UID of 0 :: For any account not given by --uid0, by default root,
38 and toor on FreeBSD.
39  
40 - no password :: An account with a empty password field, which can log
41 in without one. Locked accounts, * or !, are fine. This needs reading
42 /etc/shadow, or /etc/master.passwd on FreeBSD, so root. Not being able
43 to is UNKNOWN, unless --no-passwords is given. A empty field in
44 /etc/passwd itself counts too, as that is what is used then, whatever
45 /etc/shadow has.
46  
47 - duplicates :: Two accounts with the same name or UID. UID 0 is left to
48 the check above.
49  
50 - --absent and --present :: Accounts that must not, or must, be there.
51  
52 - --system-shells :: System accounts, those under --min-uid, with a
53 shell from /etc/shells, read as getusershell does, so one that can log
54 in. root, sync, shutdown, halt, and toor are left out, as are those
55 given by --ignore.
56  
57 Other OSes than Linux and FreeBSD are UNKNOWN.
58  
59 =head1 OPTIONS
60  
61 - -u, --uid0 :: A account that may have a UID of 0. May be given more
62 than once.
63 Default :: root, and toor on FreeBSD
64  
65 - -n, --no-passwords :: Do not check for empty passwords.
66  
67 - -s, --system-shells :: Check system accounts do not have a login shell.
68  
69 - -m, --min-uid :: The lowest UID of accounts for people.
70 Default :: 1000
71  
72 - -i, --ignore :: A system account that may have a login shell. May be
73 given more than once.
74  
75 - -a, --absent :: A account that must not be there. May be given more
76 than once.
77  
78 - -p, --present :: A account that must be there. May be given more than
79 once.
80  
81 =cut
82  
83 8 ▶sub option_spec {
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
84 return (
85 8 'uid0|u=s@', 'no-passwords|n', 'system-shells|s', 'min-uid|m=i',
86 'ignore|i=s@', 'absent|a=s@', 'present|p=s@'
87 );
88 }
89  
90 8 ▶sub defaults { return { 'min-uid' => 1000 } }
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
91  
92 =head1 METHODS
93  
94 =head2 check
95  
96 Does the check.
97  
98 =cut
99  
100 7 ▶sub check {
Subroutine check: called CC=26 SCAR=32.6
Pod documented
101 7 my ($self) = @_;
102  
103 7 my $accounts = $self->accounts;
104 7 ▶ my %uid0 = map { $_ => 1 } @{ $self->opt('uid0') // [ 'root', ( $self->os eq 'freebsd' ? 'toor' : () ) ] };
Branch: 1 branch
BranchTrueFalseTotal
$self->os eq 'freebsd' ? : 3 4 7
Condition50%1/2
$self->opt("uid0") // ["root", $self->os eq "freebsd" ? "toor" : ()]
l!l
07
MC/DC0%0/1
$self->opt("uid0") // ["root", $self->os eq "freebsd" ? "toor" : ()]
$self->opt("uid0")
Truth table50%1/2
A
A= $self->opt("uid0")
Aresult
11
00
105 7 my %by_name = map { $_->{name} => $_ } @{$accounts};
106 7 my @problems;
107 my @unknown;
108  
109 7 ▶ my @root_like = grep { $_->{uid} == 0 && !$uid0{ $_->{name} } } @{$accounts};
Condition100%3/3
$_->{'uid'} == 0 && !$uid0{$_->{'name'}}
!ll&&!rl&&r
24101
MC/DC100%2/2
$_->{'uid'} == 0 && !$uid0{$_->{'name'}}
$_->{'uid'} == 0 !$uid0{$_->{'name'}}
Truth table100%3/3
A && not(B)
A= $_->{'uid'} == 0B= !$uid0{$_->{'name'}}
ABresult
0X0
100
111
110 7 ▶ push( @problems, 'UID 0 for ' . join( ', ', map { $_->{name} } @root_like ) ) if defined( $root_like[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $root_like[0] 1 6 7
111  
112 7 foreach my $field ( 'name', 'uid' ) {
113 14 my %count;
114 # UID 0 is left to the check for it above, as toor shares it with root on FreeBSD
115 14 ▶ $count{ $_->{$field} }++ foreach grep { $field ne 'uid' || $_->{uid} != 0 } @{$accounts};
Condition100%3/3
$field ne 'uid' || $_->{'uid'} != 0
l!l&&r!l&&!r
352411
MC/DC100%2/2
$field ne 'uid' || $_->{'uid'} != 0
$field ne 'uid' $_->{'uid'} != 0
Truth table100%3/3
A || B
A= $field ne 'uid'B= $_->{'uid'} != 0
ABresult
1X1
011
000
116 14 my @duplicates = sort( grep { $count{$_} > 1 } keys(%count) );
117 14 ▶ push( @problems, 'more than one account with the ' . $field . ' ' . join( ', ', @duplicates ) )
Branch: 1 branch
BranchTrueFalseTotal
if defined $duplicates[0] 8 6 14
118 if defined( $duplicates[0] );
119 }
120  
121 7 ▶ if ( !$self->opt('no-passwords') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->opt("no-passwords")) 5 2 7
122 5 my $passwords = $self->passwords($accounts);
123 5 ▶ if ( !defined($passwords) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $passwords) { } 3 2 5
124 3 ▶ push( @unknown, 'can not read the passwords, ' . ( $self->os eq 'freebsd' ? $MASTER_PASSWD : $SHADOW ) );
Branch: 1 branch
BranchTrueFalseTotal
$self->os eq 'freebsd' ? : 2 1 3
125 } else {
126 2 my @empty = sort( grep { $passwords->{$_} eq '' } keys( %{$passwords} ) );
127 2 ▶ push( @problems, 'no password for ' . join( ', ', @empty ) ) if defined( $empty[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $empty[0] 1 1 2
128 }
129 }
130  
131 7 ▶ if ( $self->opt('system-shells') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opt("system-shells")) 2 5 7
132 2 my %shells = map { $_ => 1 } $self->shells;
133 2 ▶ my %ignored = map { $_ => 1 } ( 'root', 'sync', 'shutdown', 'halt', 'toor', @{ $self->opt('ignore') // [] } );
Condition50%1/2
$self->opt("ignore") // []
l!l
02
MC/DC0%0/1
$self->opt("ignore") // []
$self->opt("ignore")
Truth table50%1/2
A
A= $self->opt("ignore")
Aresult
11
00
134 1 my @can_log_in = map { $_->{name} }
135 12 ▶ grep { $_->{uid} < $self->opt('min-uid') && !$ignored{ $_->{name} } && $shells{ $_->{shell} } }
Condition100%6/6
$_->{'uid'} < $self->opt('min-uid') && !$ignored{$_->{'name'}} && $shells{$_->{'shell'}}
!ll&&!rl&&r
831
$_->{"uid"} < $self->opt("min-uid") and not $ignored{$_->{"name"}}
!ll&&!rl&&r
444
MC/DC100%3/3
$_->{'uid'} < $self->opt('min-uid') && !$ignored{$_->{'name'}} && $shells{$_->{'shell'}}
$_->{"uid"} < $self->opt("min-uid") not $ignored{$_->{"name"}} $shells{$_->{'shell'}}
Truth table100%4/4
A and not(B) && C
A= $_->{"uid"} < $self->opt("min-uid")B= not $ignored{$_->{"name"}}C= $shells{$_->{'shell'}}
ABCresult
0XX0
10X0
1100
1111
136 2 @{$accounts};
137 2 ▶ push( @problems, 'system accounts with a login shell, ' . join( ', ', @can_log_in ) )
Branch: 1 branch
BranchTrueFalseTotal
if defined $can_log_in[0] 1 1 2
138 if defined( $can_log_in[0] );
139 }
140  
141 7 ▶ my @there = grep { $by_name{$_} } @{ $self->opt('absent') // [] };
Condition100%2/2
$self->opt("absent") // []
l!l
25
MC/DC100%1/1
$self->opt("absent") // []
$self->opt("absent")
Truth table100%2/2
A
A= $self->opt("absent")
Aresult
11
00
142 7 ▶ push( @problems, 'there but should not be, ' . join( ', ', @there ) ) if defined( $there[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $there[0] 1 6 7
143 7 ▶ my @missing = grep { !$by_name{$_} } @{ $self->opt('present') // [] };
Condition100%2/2
$self->opt("present") // []
l!l
25
MC/DC100%1/1
$self->opt("present") // []
$self->opt("present")
Truth table100%2/2
A
A= $self->opt("present")
Aresult
11
00
144 7 ▶ push( @problems, 'missing ' . join( ', ', @missing ) ) if defined( $missing[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $missing[0] 1 6 7
145  
146 7 ▶ my @codes = ( defined( $problems[0] ) ? $self->CRITICAL : $self->OK );
Branch: 1 branch
BranchTrueFalseTotal
defined $problems[0] ? : 4 3 7
147 7 ▶ push( @codes, $self->UNKNOWN ) if defined( $unknown[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $unknown[0] 3 4 7
148 7 my $message = scalar( @{$accounts} ) . ' accounts';
149 7 ▶ $message = $message . ', ' . join( ', ', @problems, @unknown ) if defined( $problems[0] ) || defined( $unknown[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $problems[0] or defined $unknown[0] 5 2 7
Condition100%3/3
defined $problems[0] or defined $unknown[0]
l!l&&r!l&&!r
412
MC/DC100%2/2
defined $problems[0] or defined $unknown[0]
defined $problems[0] defined $unknown[0]
Truth table100%3/3
A or B
A= defined $problems[0]B= defined $unknown[0]
ABresult
1X1
011
000
150  
151 7 return $self->finish( $self->worst(@codes), $message );
152 } ## end sub check
153  
154 =head2 accounts
155  
156 Returns a array ref of the local accounts, as hash refs of name, uid,
157 password, the field as it is in the file, and shell, in the order they
158 are in the file. Dies if it can not be read.
159  
160 =cut
161  
162 7 ▶sub accounts {
Subroutine accounts: called CC=8 SCAR=22.4
Pod documented
163 7 my ($self) = @_;
164  
165 7 ▶ die( 'not supported on ' . $self->os . "\n" ) if $self->os ne 'linux' && $self->os ne 'freebsd';
Branch: 1 branch
BranchTrueFalseTotal
if $self->os ne "linux" and $self->os ne "freebsd" 0 7 7
Condition66%2/3
$self->os ne "linux" and $self->os ne "freebsd"
!ll&&!rl&&r
430
MC/DC0%0/2
$self->os ne "linux" and $self->os ne "freebsd"
$self->os ne "linux" $self->os ne "freebsd"
Truth table66%2/3
A and B
A= $self->os ne "linux"B= $self->os ne "freebsd"
ABresult
0X0
100
111
166 7 my $content = read_file( $PASSWD, err_mode => 'quiet' );
167 7 ▶ die( 'can not read ' . $PASSWD . ', ' . $! . "\n" ) if !defined($content);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 7 7
168  
169 7 my @accounts;
170 7 foreach my $line ( split( /\n/, $content ) ) {
171 35 ▶ next if $line =~ /\A\s*(?:#|\z)/ || $line =~ /\A[+-]/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\s*(?:#|\z)/u or $line =~ /\A[+-]/u 0 35 35
Condition33%1/3
$line =~ /\A\s*(?:#|\z)/u or $line =~ /\A[+-]/u
l!l&&r!l&&!r
0035
MC/DC0%0/2
$line =~ /\A\s*(?:#|\z)/u or $line =~ /\A[+-]/u
$line =~ /\A\s*(?:#|\z)/u $line =~ /\A[+-]/u
Truth table33%1/3
A or B
A= $line =~ /\A\s*(?:#|\z)/uB= $line =~ /\A[+-]/u
ABresult
1X1
011
000
172 35 my @fields = split( /:/, $line, -1 );
173 35 ▶ next if scalar(@fields) < 7;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @fields < 7 0 35 35
174 35 push( @accounts, { name => $fields[0], uid => $fields[2], password => $fields[1], shell => $fields[6] } );
175 }
176  
177 7 return \@accounts;
178 } ## end sub accounts
179  
180 =head2 passwords
181  
182 Returns a hash ref of account names to the password fields that are
183 used for them, or undef if the shadow file can not be read.
184  
185 - accounts :: From L</accounts>, for the fields in passwd itself.
186  
187 The field is the one from /etc/shadow, or /etc/master.passwd on
188 FreeBSD, unless the one in passwd is empty, as the C library and PAM
189 go by passwd then, and a empty one there means no password.
190  
191 my $passwords = $check->passwords( $check->accounts );
192  
193 =cut
194  
195 5 ▶sub passwords {
Subroutine passwords: called CC=12 SCAR=26.9
Pod documented
196 5 my ( $self, $accounts ) = @_;
197  
198 5 ▶ my $file = $self->os eq 'freebsd' ? $MASTER_PASSWD : $SHADOW;
Branch: 1 branch
BranchTrueFalseTotal
$self->os eq 'freebsd' ? : 2 3 5
199 5 ▶ my $content = read_file( $file, err_mode => 'quiet' ) // return undef;
Condition100%2/2
read_file($file, "err_mode", "quiet") // (return undef)
l!l
23
MC/DC100%1/1
read_file($file, "err_mode", "quiet") // (return undef)
read_file($file, "err_mode", "quiet")
Truth table100%2/2
A
A= read_file($file, "err_mode", "quiet")
Aresult
11
00
200  
201 2 ▶ my %passwords = map { $_->{name} => $_->{password} } @{ $accounts // [] };
Condition50%1/2
$accounts // []
l!l
20
MC/DC0%0/1
$accounts // []
$accounts
Truth table50%1/2
A
A= $accounts
Aresult
11
00
202 2 foreach my $line ( split( /\n/, $content ) ) {
203 7 ▶ next if $line =~ /\A\s*(?:#|\z)/ || $line =~ /\A[+-]/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\s*(?:#|\z)/u or $line =~ /\A[+-]/u 0 7 7
Condition33%1/3
$line =~ /\A\s*(?:#|\z)/u or $line =~ /\A[+-]/u
l!l&&r!l&&!r
007
MC/DC0%0/2
$line =~ /\A\s*(?:#|\z)/u or $line =~ /\A[+-]/u
$line =~ /\A\s*(?:#|\z)/u $line =~ /\A[+-]/u
Truth table33%1/3
A or B
A= $line =~ /\A\s*(?:#|\z)/uB= $line =~ /\A[+-]/u
ABresult
1X1
011
000
204 7 my ( $name, $password ) = split( /:/, $line, -1 );
205 7 ▶ next if !defined($name) || $name eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $name or $name eq "" 0 7 7
Condition33%1/3
not defined $name or $name eq ""
l!l&&r!l&&!r
007
MC/DC0%0/2
not defined $name or $name eq ""
not defined $name $name eq ""
Truth table33%1/3
not(A) or B
A= not defined $nameB= $name eq ""
ABresult
1X1
011
000
206 7 ▶ $passwords{$name} = $password // '' if ( $passwords{$name} // 'x' ) ne '';
Branch: 1 branch
BranchTrueFalseTotal
if ($passwords{$name} // "x") ne "" 6 1 7
Condition50%2/4
$passwords{$name} // "x"
l!l
70
$password // ""
l!l
60
MC/DC0%0/1
$passwords{$name} // "x"
$passwords{$name}
MC/DC0%0/1
$password // ""
$password
Truth table50%1/2
A
A= $passwords{$name}
Aresult
11
00
Truth table50%1/2
A
A= $password
Aresult
11
00
207 }
208  
209 2 return \%passwords;
210 } ## end sub passwords
211  
212 =head2 shells
213  
214 Returns the login shells in /etc/shells, read as getusershell does.
215 Each line gives one, the first thing on it starting with a /, and
216 lines with a # before that are comments. None if it can not be read.
217  
218 =cut
219  
220 3 ▶sub shells {
Subroutine shells: called CC=3 SCAR=11.0
Pod documented
221 3 my ($self) = @_;
222  
223 3 ▶ my $content = read_file( $SHELLS, err_mode => 'quiet' ) // '';
Condition50%1/2
read_file($Monitoring::Sneck::Checks::accounts::SHELLS, "err_mode", "quiet") // ""
l!l
30
MC/DC0%0/1
read_file($Monitoring::Sneck::Checks::accounts::SHELLS, "err_mode", "quiet") // ""
read_file($Monitoring::Sneck::Checks::accounts::SHELLS, "err_mode", "quiet")
Truth table50%1/2
A
A= read_file($Monitoring::Sneck::Checks::accounts::SHELLS, "err_mode", "quiet")
Aresult
11
00
224  
225 3 ▶ return map { m{\A[^#/]*(/[^\s#]*)} ? $1 : () } split( /\n/, $content );
Branch: 1 branch
BranchTrueFalseTotal
m[\A[^#/]*(/[^\s#]*)]u ? : 6 6 12
226 }
227  
228 7 1;
« blib/lib/Monitoring/Sneck/Checks/resolv.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/log.pm »