1 package System::Introspector::Probe::Users;
4 use System::Introspector::Util qw(
13 has passwd_file => (is => 'ro', default => sub { '/etc/passwd' });
17 return transform_exceptions {
18 my $fh = $self->_open_passwd_fh;
20 while (defined( my $line = <$fh> )) {
21 my $data = $self->_deparse_htpasswd_line($line);
22 my $user = $data->{username};
23 my $home = $data->{home};
24 $data->{groups} = transform_exceptions {
25 $self->_gather_user_groups($user);
27 $data->{ssh}{keys} = transform_exceptions {
28 $self->_gather_ssh_keys($user, $home);
30 $data->{crontab} = transform_exceptions {
31 $self->_gather_crontab($user);
33 $user{ $data->{username} } = $data;
35 return { users => \%user };
40 my ($self, $user) = @_;
41 my ($out, $err, $ok) = output_from_command
42 ['crontab', '-u', $user, '-l'];
45 if $err =~ m{^no crontab}i;
46 return { error => $err };
48 return { body => $out };
51 sub _gather_ssh_keys {
52 my ($self, $user, $home) = @_;
53 my $ssh_dir = "$home/.ssh";
54 my $ssh_authkeys = "$ssh_dir/authorized_keys";
57 authorized => { file_name => $ssh_authkeys, body => '' }
60 for my $item (files_from_dir $ssh_dir) {
61 next unless $item =~ m{\.pub$};
62 $key{ $item } = transform_exceptions {
64 file_name => "$ssh_dir/$item",
65 body => scalar output_from_file "$ssh_dir/$item",
69 my $auth_keys = (-e $ssh_authkeys) ? (transform_exceptions {
71 file_name => $ssh_authkeys,
72 body => scalar output_from_file $ssh_authkeys,
74 }) : { file_name => $ssh_authkeys, body => '' };
75 return { files => \%key, authorized => $auth_keys };
78 sub _gather_user_groups {
79 my ($self, $user) = @_;
80 my $groups = output_from_command [groups => $user];
81 return { list => [split m{\s+}, $groups] };
84 sub _deparse_htpasswd_line {
85 my ($self, $line) = @_;
88 @value{qw( username uid gid comment home shell )}
89 = (split m{:}, $line)[0, 2..6];
95 return handle_from_file $self->passwd_file;
104 System::Introspector::Users - Gather user information
108 Gathers information for all users in C</etc/passwd>, including cronjobs and
109 installed SSH public keys.
115 =item L<System::Introspector>