File: Users.pm

package info (click to toggle)
ocsinventory-agent 2%3A2.10.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,424 kB
  • sloc: perl: 26,492; xml: 773; objc: 528; sh: 386; ansic: 333; makefile: 12
file content (148 lines) | stat: -rw-r--r-- 3,444 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package Ocsinventory::Agent::Backend::OS::Generic::Users;

use strict;
use warnings;

sub check {
    my $params = shift;
    my $common = $params->{common};

    # Useless check for a posix system i guess
    my @who = `who 2>/dev/null`;
    my @last = `last -n 1 2>/dev/null`;

    if (($common->can_read("/etc/passwd") && $common->can_read("/etc/group")) || @who || @last ) {
        return 1;
    } else {
        return 0;
    }
}

# Initialise the distro entry
sub run {
    my $params = shift;
    my $common = $params->{common};

    my %users;

    # Logged on users
    for (`who`){
        my $user = $1 if /^(\S+)./;
        $common->addUser ({ LOGIN => $user });
    }

    # Local users
    foreach my $user (_getLocalUsers()) {
        push @{$users{$user->{GID}}}, $user->{LOGIN};
	    #delete $user->{GID};

        $common->addLocalUser({
            LOGIN   => $user->{LOGIN},
            ID_USER => $user->{ID_USER},
            GID     => $user->{GID},
            NAME    => $user->{NAME},
            HOME    => $user->{HOME},
            SHELL   => $user->{SHELL}
        });
    }

    # Local groups with members
    foreach my $group (_getLocalGroups()) {
        push @{$group->{MEMBER}}, @{$users{$group->{ID_GROUP}}} if $users{$group->{ID_GROUP}};
        my $group_member = join ',', @{$group->{MEMBER}};

        $common->addLocalGroup({
            ID_GROUP    => $group->{ID_GROUP},
            NAME        => $group->{NAME},
            MEMBER      => $group_member
        });
    }

    # last logged user
    $common->setHardware(_getLast());
}

sub _getLocalUsers{

     open(my $fh, '<:encoding(UTF-8)', "/etc/passwd") or warn;
     my @userinfo=<$fh>;
     close($fh);

     my @users;
    
     foreach my $line (@userinfo){
         
         next if $line =~ /^#/;
         next if $line =~ /^[+-]/; # old format for external inclusion
         chomp $line;
         my ($login, undef, $uid, $gid, $gecos, $home, $shell) = split(/:/, $line);

         push @users, {
             LOGIN      => $login,
             ID_USER    => $uid,
             GID        => $gid,
             NAME       => $gecos,
             HOME       => $home,
             SHELL      => $shell,
         };
     }

     return @users;

}

sub _getLocalGroups {

     open(my $fh, '<:encoding(UTF-8)', "/etc/group") or warn;
     my @groupinfo=<$fh>;
     close($fh);

     my @groups;

     foreach my $line (@groupinfo){
         next if $line =~ /^#/;
         chomp $line;
         my ($name, undef, $gid, $members) = split(/:/, $line);
         
         my @members = split(/,/, $members);   
         push @groups, {
             ID_GROUP   => $gid,
             NAME       => $name,
             MEMBER     => \@members,
         };
     }

     return @groups;

}

sub _getLast {
    
    my ($lastuser,$lastlogged);

    my @info=`last -n 50`;

    foreach my $last (@info) {
        chomp $last;
        next if $last =~ /^(reboot|shutdown)/;

        my @last = split(/\s+/, $last);
        next unless (@last);

        $lastuser = shift @last or next;

        # Found time on column starting as week day
        shift @last while ( @last > 3 && $last[0] !~ /^mon|tue|wed|thu|fri|sat|sun/i );
        $lastlogged = @last > 3 ? "@last[0..3]" : undef;
        last;
    }

    return unless $lastuser;

    return {
        LASTLOGGEDUSER     => $lastuser,
        DATELASTLOGGEDUSER => $lastlogged
    };
}

1;