line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package FusionInventory::Agent::Task::Inventory::Win32::Users; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
73919557
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
62
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
93
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
11
|
use constant wbemFlagReturnImmediately => 0x10; |
|
1
|
|
|
|
|
91
|
|
|
1
|
|
|
|
|
170
|
|
7
|
1
|
|
|
1
|
|
5
|
use constant wbemFlagForwardOnly => 0x20; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
87
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
5
|
use English qw(-no_match_vars); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
10
|
1
|
|
|
1
|
|
1835
|
use Win32::OLE qw(in); |
|
1
|
|
|
|
|
358
|
|
|
1
|
|
|
|
|
5
|
|
11
|
1
|
|
|
1
|
|
54
|
use Win32::OLE::Variant; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
16
|
|
12
|
|
|
|
|
|
|
use Win32::TieRegistry ( |
13
|
1
|
|
|
|
|
6
|
Delimiter => '/', |
14
|
|
|
|
|
|
|
ArrayValues => 0, |
15
|
|
|
|
|
|
|
qw/KEY_READ/ |
16
|
1
|
|
|
1
|
|
732
|
); |
|
1
|
|
|
|
|
152
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Win32::OLE->Option(CP => Win32::OLE::CP_UTF8); |
19
|
|
|
|
|
|
|
|
20
|
1
|
|
|
1
|
|
767
|
use FusionInventory::Agent::Tools::Win32; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
943
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub isEnabled { |
23
|
0
|
|
|
0
|
0
|
|
my (%params) = @_; |
24
|
0
|
0
|
|
|
|
|
return 0 if $params{no_category}->{user}; |
25
|
0
|
|
|
|
|
|
return 1; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub doInventory { |
29
|
0
|
|
|
0
|
0
|
|
my (%params) = @_; |
30
|
|
|
|
|
|
|
|
31
|
0
|
|
|
|
|
|
my $inventory = $params{inventory}; |
32
|
0
|
|
|
|
|
|
my $logger = $params{logger}; |
33
|
|
|
|
|
|
|
|
34
|
0
|
0
|
|
|
|
|
if (!$params{no_category}->{local_user}) { |
35
|
0
|
|
|
|
|
|
foreach my $user (_getLocalUsers(logger => $logger)) { |
36
|
0
|
|
|
|
|
|
$inventory->addEntry( |
37
|
|
|
|
|
|
|
section => 'LOCAL_USERS', |
38
|
|
|
|
|
|
|
entry => $user |
39
|
|
|
|
|
|
|
); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
0
|
0
|
|
|
|
|
if (!$params{no_category}->{local_group}) { |
44
|
0
|
|
|
|
|
|
foreach my $group (_getLocalGroups(logger => $logger)) { |
45
|
0
|
|
|
|
|
|
$inventory->addEntry( |
46
|
|
|
|
|
|
|
section => 'LOCAL_GROUPS', |
47
|
|
|
|
|
|
|
entry => $group |
48
|
|
|
|
|
|
|
); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
foreach my $user (_getLoggedUsers(logger => $logger)) { |
53
|
0
|
|
|
|
|
|
$inventory->addEntry( |
54
|
|
|
|
|
|
|
section => 'USERS', |
55
|
|
|
|
|
|
|
entry => $user |
56
|
|
|
|
|
|
|
); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
$inventory->setHardware({ |
60
|
0
|
|
|
|
|
|
LASTLOGGEDUSER => _getLastUser(logger => $logger) |
61
|
|
|
|
|
|
|
}); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub _getLocalUsers { |
65
|
|
|
|
|
|
|
|
66
|
0
|
0
|
|
0
|
|
|
my $WMIService = Win32::OLE->GetObject("winmgmts:\\\\.\\root\\CIMV2") |
67
|
|
|
|
|
|
|
or die "WMI connection failed: " . Win32::OLE->LastError(); |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
my $query = |
70
|
|
|
|
|
|
|
"SELECT * FROM Win32_UserAccount " . |
71
|
|
|
|
|
|
|
"WHERE LocalAccount='True' AND Disabled='False' and Lockout='False'"; |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
my @users; |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
foreach my $object (in $WMIService->ExecQuery($query)) { |
76
|
|
|
|
|
|
|
my $user = { |
77
|
|
|
|
|
|
|
NAME => $object->{Name}, |
78
|
|
|
|
|
|
|
ID => $object->{SID}, |
79
|
0
|
|
|
|
|
|
}; |
80
|
0
|
|
|
|
|
|
utf8::upgrade($user->{NAME}); |
81
|
0
|
|
|
|
|
|
push @users, $user; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
|
return @users; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub _getLocalGroups { |
88
|
|
|
|
|
|
|
|
89
|
0
|
0
|
|
0
|
|
|
my $WMIService = Win32::OLE->GetObject("winmgmts:\\\\.\\root\\CIMV2") |
90
|
|
|
|
|
|
|
or die "WMI connection failed: " . Win32::OLE->LastError(); |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
my $query = |
93
|
|
|
|
|
|
|
"SELECT * FROM Win32_Group " . |
94
|
|
|
|
|
|
|
"WHERE LocalAccount='True'"; |
95
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
my @groups; |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
foreach my $object (in $WMIService->ExecQuery($query)) { |
99
|
|
|
|
|
|
|
my $group = { |
100
|
|
|
|
|
|
|
NAME => $object->{Name}, |
101
|
|
|
|
|
|
|
ID => $object->{SID}, |
102
|
0
|
|
|
|
|
|
}; |
103
|
0
|
|
|
|
|
|
utf8::upgrade($group->{NAME}); |
104
|
0
|
|
|
|
|
|
push @groups, $group; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
return @groups; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub _getLoggedUsers { |
111
|
|
|
|
|
|
|
|
112
|
0
|
0
|
|
0
|
|
|
my $WMIService = Win32::OLE->GetObject("winmgmts:\\\\.\\root\\CIMV2") |
113
|
|
|
|
|
|
|
or die "WMI connection failed: " . Win32::OLE->LastError(); |
114
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
my $processes = $WMIService->ExecQuery( |
116
|
|
|
|
|
|
|
"SELECT * FROM Win32_Process", "WQL", |
117
|
|
|
|
|
|
|
wbemFlagReturnImmediately | wbemFlagForwardOnly ## no critic (ProhibitBitwise) |
118
|
|
|
|
|
|
|
); |
119
|
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
|
my @users; |
121
|
|
|
|
|
|
|
my $seen; |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
foreach my $process (in $processes) { |
124
|
|
|
|
|
|
|
next unless |
125
|
|
|
|
|
|
|
$process->{ExecutablePath} && |
126
|
0
|
0
|
0
|
|
|
|
$process->{ExecutablePath} =~ /\\Explorer\.exe$/i; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
## no critic (ProhibitBitwise) |
129
|
0
|
|
|
|
|
|
my $name = Variant(VT_BYREF | VT_BSTR, ''); |
130
|
0
|
|
|
|
|
|
my $domain = Variant(VT_BYREF | VT_BSTR, ''); |
131
|
|
|
|
|
|
|
|
132
|
0
|
|
|
|
|
|
$process->GetOwner($name, $domain); |
133
|
|
|
|
|
|
|
|
134
|
0
|
|
|
|
|
|
my $user = { |
135
|
|
|
|
|
|
|
LOGIN => $name->Get(), |
136
|
|
|
|
|
|
|
DOMAIN => $domain->Get() |
137
|
|
|
|
|
|
|
}; |
138
|
|
|
|
|
|
|
|
139
|
0
|
|
|
|
|
|
utf8::upgrade($user->{LOGIN}); |
140
|
0
|
|
|
|
|
|
utf8::upgrade($user->{DOMAIN}); |
141
|
|
|
|
|
|
|
|
142
|
0
|
0
|
|
|
|
|
next if $seen->{$user->{LOGIN}}++; |
143
|
|
|
|
|
|
|
|
144
|
0
|
|
|
|
|
|
push @users, $user; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
0
|
|
|
|
|
|
return @users; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub _getLastUser { |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
# ensure native registry access, not the 32 bit view |
153
|
0
|
0
|
|
0
|
|
|
my $flags = is64bit() ? KEY_READ | KEY_WOW64_64 : KEY_READ; |
154
|
|
|
|
|
|
|
|
155
|
0
|
0
|
|
|
|
|
my $machKey = $Registry->Open('LMachine', { |
156
|
|
|
|
|
|
|
Access => $flags |
157
|
|
|
|
|
|
|
}) or die "Can't open HKEY_LOCAL_MACHINE key: $EXTENDED_OS_ERROR"; |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
my $user = |
160
|
|
|
|
|
|
|
encodeFromRegistry($machKey->{"SOFTWARE/Microsoft/Windows/CurrentVersion/Authentication/LogonUI/LastLoggedOnUser"}) || |
161
|
0
|
|
0
|
|
|
|
encodeFromRegistry($machKey->{"SOFTWARE/Microsoft/Windows NT/CurrentVersion/Winlogon/DefaultUserName"}); |
162
|
0
|
0
|
|
|
|
|
return unless $user; |
163
|
|
|
|
|
|
|
|
164
|
0
|
|
|
|
|
|
$user =~ s,.*\\,,; |
165
|
0
|
|
|
|
|
|
return $user; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
1; |