1 package System::Introspector::Users;
3 use IPC::Run qw( run );
7 my $fh = $self->_open_passwd_fh;
9 while (defined( my $line = <$fh> )) {
10 my $data = $self->_deparse_htpasswd_line($line);
11 my $user = $data->{username};
12 my $home = $data->{home};
13 $data->{groups} = $self->_gather_user_groups($user);
14 $data->{ssh}{keys} = $self->_gather_ssh_keys($user, $home);
15 $data->{crontab} = $self->_gather_crontab($user);
16 $user{ $data->{uid} } = $data;
22 my ($self, $user) = @_;
23 my ($in, $out, $err) = (('') x 3);
24 run(['crontab', '-u', $user, '-l'], \$in, \$out, \$err);
27 if $err =~ m{^no crontab}i;
28 return { error => $err };
30 return { body => $out };
33 sub _gather_ssh_keys {
34 my ($self, $user, $home) = @_;
35 my $ssh_dir = "$home/.ssh/";
40 or return { error => $! };
42 while (defined( my $item = readdir $dh )) {
43 next unless $item =~ m{\.pub$};
44 my $item_path = "$ssh_dir/$item";
45 open my $key_fh, '<', $item_path;
47 $key{ $item }{body} = do { local $/; <$key_fh> };
50 $key{ $item }{error} = $!;
53 return { files => \%key };
56 sub _gather_user_groups {
57 my ($self, $user) = @_;
58 return [split m{\s+}, `groups $user`];
61 sub _deparse_htpasswd_line {
62 my ($self, $line) = @_;
65 @value{qw( username uid gid comment home shell )}
66 = (split m{:}, $line)[0, 2..6];
72 open my $fh, '<', '/etc/passwd'
73 or die "Unable to open /etc/passwd: $!\n";