-
Notifications
You must be signed in to change notification settings - Fork 125
/
License.pm
201 lines (164 loc) · 6.48 KB
/
License.pm
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package FusionInventory::Agent::Task::Inventory::Win32::License;
use strict;
use warnings;
use parent 'FusionInventory::Agent::Task::Inventory::Module';
use English qw(-no_match_vars);
use FusionInventory::Agent::Tools::License;
use FusionInventory::Agent::Tools::Win32;
use FusionInventory::Agent::Tools;
my $seenProducts;
sub isEnabled {
my (%params) = @_;
return 0 if $params{no_category}->{licenseinfo};
return 1;
}
sub isEnabledForRemote {
my (%params) = @_;
return 0 if $params{no_category}->{licenseinfo};
return 1;
}
sub _resetSeenProducts {
$seenProducts = {};
}
sub doInventory {
my (%params) = @_;
my $inventory = $params{inventory};
my @licenses;
my $officeKey = getRegistryKey(
path => "HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Office"
);
_scanOfficeLicences($officeKey) if $officeKey;
my $fileAdobe = 'C:\Program Files\Common Files\Adobe\Adobe PCD\cache\cache.db';
if (is64bit()) {
$fileAdobe = 'C:\Program Files (x86)\Common Files\Adobe\Adobe PCD\cache\cache.db';
my $officeKey32 = getRegistryKey(
path => "HKEY_LOCAL_MACHINE/SOFTWARE/Wow6432Node/Microsoft/Office"
);
_scanOfficeLicences($officeKey32) if $officeKey32;
}
push @licenses, getAdobeLicensesWithoutSqlite($fileAdobe) if (-e $fileAdobe);
_scanWmiSoftwareLicensingProducts();
push @licenses, _getSeenProducts();
foreach my $license (@licenses) {
$inventory->addEntry(
section => 'LICENSEINFOS',
entry => $license
);
}
_resetSeenProducts();
}
sub _getSeenProducts {
return unless $seenProducts;
return grep { defined $_->{KEY} } values(%{$seenProducts});
}
sub _scanWmiSoftwareLicensingProducts {
foreach my $object (getWMIObjects(
moniker => 'winmgmts:\\\\.\\root\\CIMV2',
class => 'SoftwareLicensingProduct',
properties => [ qw/
Name Description LicenseStatus PartialProductKey ID
ProductKeyChannel ProductKeyID ProductKeyID2 ApplicationID
/ ]
)) {
next unless $object->{'PartialProductKey'} && $object->{'LicenseStatus'};
# Skip operating system license as still set from OS module
next if ($object->{'Description'} && $object->{'Description'} =~ /Operating System/i);
if ($object->{'ID'}) {
my $wmiLicence = _getWmiLicense($object);
my $uiidLC = lc($object->{'ID'});
if (!defined $seenProducts->{$uiidLC}) {
$seenProducts->{$uiidLC} = $wmiLicence;
} else {
$wmiLicence->{'FULLNAME'} = $seenProducts->{$uiidLC}->{'FULLNAME'}
if $seenProducts->{$uiidLC}->{'FULLNAME'};
$wmiLicence->{'TRIAL'} = $seenProducts->{$uiidLC}->{'TRIAL'}
if $seenProducts->{$uiidLC}->{'TRIAL'};
my $uiidToDelete = $uiidLC;
if ($seenProducts->{$uiidLC}->{PRODUCTCODE}) {
# Change key Target
$uiidLC = $seenProducts->{$uiidLC}->{PRODUCTCODE};
if ($seenProducts->{$uiidLC} && $seenProducts->{$uiidLC}->{KEY}) {
my $wmiKey = substr $wmiLicence->{KEY}, -5;
if ($seenProducts->{$uiidLC}->{'KEY'} =~ m/$wmiKey$/) {
# Skip this licence - Registry give more information
next;
}
}
}
delete $seenProducts->{$uiidToDelete};
$seenProducts->{$uiidLC} = $wmiLicence;
}
}
}
}
sub _scanOfficeLicences {
my ($key) = @_;
# registry data structure:
# SOFTWARE/Microsoft/Office
# └── x.y
# └── Registration
# └── UUID
# └── DigitalProductID:value
# └── ProductID:value
# └── ...
foreach my $versionKey (keys %{$key}) {
my $registrationKey = $key->{$versionKey}->{'Registration/'};
next unless $registrationKey;
foreach my $uuidKey (keys %{$registrationKey}) {
my $cleanUuidKey = lc( $uuidKey =~ /([-\w]+)/ && $1 );
# Keep in memory seen product with ProductCode value or DigitalProductID
$seenProducts->{$cleanUuidKey} = _getOfficeLicense($registrationKey->{$uuidKey})
if $registrationKey->{$uuidKey}->{'/DigitalProductID'};
if ($registrationKey->{$uuidKey}->{'/ProductCode'} && $registrationKey->{$uuidKey}->{'/ProductName'}) {
$seenProducts->{$cleanUuidKey} = {
PRODUCTCODE => lc($registrationKey->{$uuidKey}->{'/ProductCode'} =~ /([-\w]+)/ && $1),
FULLNAME => encodeFromRegistry($registrationKey->{$uuidKey}->{'/ProductName'}),
};
$seenProducts->{$cleanUuidKey}->{'TRIAL'} = 1
if $registrationKey->{$uuidKey}->{'/ProductNameBrand'} && $registrationKey->{$uuidKey}->{'/ProductNameBrand'} =~ /trial/i;
}
}
}
}
sub _getWmiLicense {
my ($wmi) = @_;
my $key = $wmi->{'PartialProductKey'};
if ($key && length($key) == 5) {
$key = sprintf("XXXXX-XXXXX-XXXXX-XXXXX-%s", $key);
}
my $channel = $wmi->{'ProductKeyChannel'} || '';
my $license = {
KEY => $key,
PRODUCTID => $wmi->{'ProductKeyID2'} || $wmi->{'ApplicationID'} || $wmi->{'ProductKeyID'},
OEM => $channel =~ /OEM/i ? 1 : 0,
FULLNAME => $wmi->{'Description'},
NAME => $wmi->{'Name'}
};
return $license;
}
sub _getOfficeLicense {
my ($key) = @_;
my $license = {
KEY => decodeMicrosoftKey($key->{'/DigitalProductID'}),
PRODUCTID => $key->{'/ProductID'},
UPDATE => $key->{'/SPLevel'},
OEM => $key->{'/OEM'},
FULLNAME => encodeFromRegistry($key->{'/ProductName'}) ||
encodeFromRegistry($key->{'/ConvertToEdition'}),
NAME => encodeFromRegistry($key->{'/ProductNameNonQualified'}) ||
encodeFromRegistry($key->{'/ProductNameVersion'})
};
if ($key->{'/TrialType'} && $key->{'/TrialType'} =~ /(\d+)$/) {
$license->{TRIAL} = int($1);
}
my @products;
foreach my $variable (keys %$key) {
next unless $variable =~ m/\/(\w+)NameVersion$/;
push @products, $1;
}
if (@products) {
$license->{COMPONENTS} = join('/', sort @products);
}
return $license;
}
1;