added base pod to all probes
[scpubgit/System-Introspector.git] / lib / System / Introspector / Sudoers.pm
CommitLineData
478358f5 1package System::Introspector::Sudoers;
2use Moo;
3
4has sudoers_file => (
5 is => 'ro',
6 default => sub { '/etc/sudoers' },
7);
8
9has hostname => (
10 is => 'ro',
11 default => sub { scalar `hostname` },
12);
13
14sub gather {
15 my ($self) = @_;
16 my %file = $self->_gather_files($self->sudoers_file);
17 return \%file;
18}
19
20sub _gather_files {
21 my ($self, $file) = @_;
22 open my $fh, '<', $file
23 or return $file => { error => "Unable to read: $!" };
24 my @lines = <$fh>;
25 my %file = ($file => { body => join '', @lines });
26 for my $line (@lines) {
27 chomp $line;
28 if ($line =~ m{^#include\s+(.+)$}) {
29 my $inc_file = $self->_insert_hostname($1);
30 %file = (%file, $self->_gather_files($inc_file));
31 }
32 elsif ($line =~ m{^#includedir\s+(.+)$}) {
33 my $inc_dir = $self->_insert_hostname($1);
34 %file = (%file, $self->_gather_from_dir($inc_dir));
35 }
36 }
37 return %file;
38}
39
40sub _gather_from_dir {
41 my ($self, $dir) = @_;
42 opendir(my $dh, $dir);
43 return $dir => { error => "Unable to read dir $dir: $!" }
44 unless $dh;
45 my %file;
46 while (my $file = readdir $dh) {
47 next if $file =~ m{\.} or $file =~ m{~$};
48 %file = (%file, $self->_gather_files("$dir/$file"));
49 }
50 return %file;
51}
52
53sub _insert_hostname {
54 my ($self, $value) = @_;
55 my $hostname = $self->hostname;
56 $value =~ s{\%h}{$hostname}g;
57 return $value;
58}
59
601;
535e84b6 61
62__END__
63
64=head1 NAME
65
66System::Introspector::Sudoers - Gather sudoer information
67
68=head1 DESCRIPTION
69
70Reads C<sudoers> files to gather information about sudo abilities. This
71probe will also read all included files.
72
73=head1 ATTRIBUTES
74
75=head2 sudoers_file
76
77The path to the original C<sudoers> file that should be read. Includes from this
78file will be followed and provided as well.
79
80=head2 hostname
81
82The hostname used to resolve C<%h> hostname markers in inclusions.
83
84=head1 SEE ALSO
85
86=over
87
88=item L<System::Introspector>
89
90=back
91
92=cut
93