File: License.pm

package info (click to toggle)
fusioninventory-agent 1%3A2.3.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 19,636 kB
  • ctags: 1,451
  • sloc: perl: 89,223; xml: 422; sh: 83; python: 26; makefile: 22
file content (104 lines) | stat: -rw-r--r-- 2,961 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
package FusionInventory::Agent::Task::Inventory::Win32::License;

use strict;
use warnings;

use English qw(-no_match_vars);
use Win32::TieRegistry (
    Delimiter   => '/',
    ArrayValues => 0,
    qw/KEY_READ/
);

use FusionInventory::Agent::Tools::Win32;

sub isEnabled {
    return 1;
}

sub doInventory {
    my (%params) = @_;

    my $inventory = $params{inventory};
    my $logger    = $params{logger};

    my $is64bit = is64bit();
    my @licenses;

    if ($is64bit) {
        my $machKey64 = $Registry->Open('LMachine', {
            Access => KEY_READ | KEY_WOW64_64 ## no critic (ProhibitBitwise)
        }) or $logger->error("Can't open HKEY_LOCAL_MACHINE key: $EXTENDED_OS_ERROR");
        my $officeKey64 = $machKey64->{"SOFTWARE/Microsoft/Office"};
        push @licenses, _scanOffice($officeKey64 ) if $officeKey64;

        my $machKey32 = $Registry->Open('LMachine', {
            Access => KEY_READ | KEY_WOW64_32 ## no critic (ProhibitBitwise)
        }) or $logger->error("Can't open HKEY_LOCAL_MACHINE key: $EXTENDED_OS_ERROR");

        my $officeKey32 = $machKey32->{"SOFTWARE/Microsoft/Office"};
        push @licenses, _scanOffice($officeKey32) if $officeKey32;
    } else {
        my $machKey = $Registry->Open('LMachine', {
            Access => KEY_READ ## no critic (ProhibitBitwise)
        }) or $logger->error(
            "Can't open HKEY_LOCAL_MACHINE key: $EXTENDED_OS_ERROR"
        );

        my $officeKey = $machKey->{"SOFTWARE/Microsoft/Office"};
        push @licenses, _scanOffice($officeKey) if $officeKey;
    }

    foreach my $license (@licenses) {
        $params{inventory}->addEntry(
            section => 'LICENSEINFOS',
            entry   => $license
        );
    }
}

sub _scanOffice {
    my ($key) = @_;

    my %license = (
        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->{DigitalProductID}) {
        $license{KEY} = parseProductKey($key->{DigitalProductID});
    }

    if ($key->{TrialType} && $key->{TrialType} =~ /(\d+)$/) {
        $license{TRIAL} = int($1);
    }

    my @products;
    foreach my $entry (keys %$key) {
        next unless $entry =~ s/\/(\w+)NameVersion$//;
        my $product = $1;
        next unless $key->{$product."NameVersion"};
        push @products, $product;
    }
    if (@products) {
        $license{COMPONENTS} = join('/', @products);
    }

    my @licenses;
    push @licenses, \%license if $license{KEY};

    foreach my $subKey (keys %$key) {
        # skip variables
        next if $subKey =~ m{^/};
        push @licenses, _scanOffice($key->{$subKey});
    }

    return @licenses;
}

1;