| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package FusionInventory::Agent::Tools::License; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 3 |  |  | 3 |  | 11192635 | use strict; | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 82 |  | 
| 4 | 3 |  |  | 3 |  | 16 | use warnings; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 101 |  | 
| 5 | 3 |  |  | 3 |  | 8 | use base 'Exporter'; | 
|  | 3 |  |  |  |  | 41 |  | 
|  | 3 |  |  |  |  | 293 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 3 |  |  | 3 |  | 394 | use English qw(-no_match_vars); | 
|  | 3 |  |  |  |  | 2407 |  | 
|  | 3 |  |  |  |  | 27 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 3 |  |  | 3 |  | 1857 | use FusionInventory::Agent::Tools; | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 1722 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | our @EXPORT = qw( | 
| 12 |  |  |  |  |  |  | getAdobeLicenses | 
| 13 |  |  |  |  |  |  | decodeMicrosoftKey | 
| 14 |  |  |  |  |  |  | ); | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | # Thanks to Brandon Mulcahy | 
| 17 |  |  |  |  |  |  | # http://www.a1vbcode.com/snippet-4796.asp | 
| 18 |  |  |  |  |  |  | sub _decodeAdobeKey { | 
| 19 | 2 |  |  | 2 |  | 3 | my ($encrypted_key) = @_; | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 2 |  |  |  |  | 10 | my @cipher_key = qw/ | 
| 22 |  |  |  |  |  |  | 0000000001 5038647192 1456053789 2604371895 | 
| 23 |  |  |  |  |  |  | 4753896210 8145962073 0319728564 7901235846 | 
| 24 |  |  |  |  |  |  | 7901235846 0319728564 8145962073 4753896210 | 
| 25 |  |  |  |  |  |  | 2604371895 1426053789 5038647192 3267408951 | 
| 26 |  |  |  |  |  |  | 5038647192 2604371895 8145962073 7901235846 | 
| 27 |  |  |  |  |  |  | 3267408951 1426053789 4753896210 0319728564/; | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 2 |  |  |  |  | 1 | my @decrypted_key_chars; | 
| 30 | 2 |  |  |  |  | 7 | foreach my $char (split(//, $encrypted_key)) { | 
| 31 | 48 |  |  |  |  | 30 | my $sub_cipher_key = shift @cipher_key; | 
| 32 | 48 |  |  |  |  | 97 | push @decrypted_key_chars, (split(//, $sub_cipher_key))[$char]; | 
| 33 |  |  |  |  |  |  | } | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 2 |  |  |  |  | 13 | return sprintf | 
| 36 |  |  |  |  |  |  | '%s%s%s%s-%s%s%s%s-%s%s%s%s-%s%s%s%s-%s%s%s%s-%s%s%s%s', | 
| 37 |  |  |  |  |  |  | @decrypted_key_chars; | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | sub getAdobeLicenses { | 
| 41 | 1 |  |  | 1 | 1 | 372 | my (%params) = @_; | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 1 |  |  |  |  | 17 | my $handle = getFileHandle(%params); | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 1 |  |  |  |  | 1 | my @licenses; | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | my %data; | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 1 |  |  |  |  | 9 | while (my $line = <$handle>) { | 
| 50 | 229 |  |  |  |  | 158 | chomp($line); | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 229 |  |  |  |  | 288 | my @f = split(/ <> /, $line); | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 229 | 100 |  |  |  | 260 | next unless $f[3]; | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 207 |  |  |  |  | 291 | $f[1] =~ s/\{\|\}.*//; | 
| 57 | 207 |  |  |  |  | 155 | $f[2] =~ s/\{\|\}.*//; | 
| 58 | 207 |  |  |  |  | 124 | $f[3] =~ s/\{\|\}.*//; | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 207 | 100 |  |  |  | 396 | if ($f[2] eq 'FLMap') { | 
|  |  | 100 |  |  |  |  |  | 
| 61 | 9 |  |  |  |  | 4 | push @{$data{$f[3]}{with}}, $f[1]; | 
|  | 9 |  |  |  |  | 27 |  | 
| 62 |  |  |  |  |  |  | } elsif ($f[3] ne "unlicensed") { | 
| 63 | 135 |  |  |  |  | 328 | $data{$f[1]}{$f[2]} = $f[3]; | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 1 |  |  |  |  | 3 | foreach my $key (keys %data) { | 
| 68 | 11 | 50 | 66 |  |  | 24 | next unless $data{$key}{SN} || $data{$key}{with}; | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | push @licenses, { | 
| 71 |  |  |  |  |  |  | NAME       => $key, | 
| 72 |  |  |  |  |  |  | FULLNAME   => $data{$key}{ALM_LicInfo_EpicAppName}, | 
| 73 |  |  |  |  |  |  | KEY        => _decodeAdobeKey($data{$key}{SN}), | 
| 74 | 2 |  |  |  |  | 5 | COMPONENTS => join('/', @{$data{$key}{with}}) | 
|  | 2 |  |  |  |  | 9 |  | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 1 |  |  |  |  | 23 | return @licenses; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | # inspired by http://poshcode.org/4363 | 
| 82 |  |  |  |  |  |  | sub decodeMicrosoftKey { | 
| 83 | 5 |  |  | 5 | 1 | 13311 | my ($raw) = @_; | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | ## no critic (ProhibitBitwise) | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 5 | 50 |  |  |  | 12 | return unless $raw; | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 5 |  |  |  |  | 182 | my @key_bytes = unpack 'C*', $raw; | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | # select correct bytes range: | 
| 92 |  |  |  |  |  |  | # - 808 to 822 for new style keys (Office 2010 and later) | 
| 93 |  |  |  |  |  |  | # - 52 to 66 for old style keys | 
| 94 | 5 | 100 |  |  |  | 49 | my $first_byte = defined $key_bytes[808] ? 808 : 52; | 
| 95 | 5 |  |  |  |  | 7 | my $last_byte  = $first_byte + 14; | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | # check for Windows 8/Office 2013 style key (can contains the letter "N") | 
| 98 | 5 |  |  |  |  | 6 | my $containsN  = ($key_bytes[$last_byte] >> 3) & 1; | 
| 99 | 5 |  |  |  |  | 5 | $key_bytes[$last_byte] = $key_bytes[$last_byte] & 0xF7; | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | # length of product key, in chars | 
| 102 | 5 |  |  |  |  | 4 | my $chars_length = 25; | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | # length of product key, in bytes | 
| 105 | 5 |  |  |  |  | 4 | my $bytes_length = 15; | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | # product key available characters | 
| 108 | 5 |  |  |  |  | 14 | my @letters = qw(B C D F G H J K M P Q R T V W X Y 2 3 4 6 7 8 9); | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | # extract relevant bytes | 
| 111 | 5 |  |  |  |  | 11 | my @bytes = @key_bytes[$first_byte .. $last_byte]; | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | # return immediatly for null keys | 
| 114 | 5 | 100 |  | 47 |  | 23 | return if all { $_ == 00 } @bytes; | 
|  | 47 |  |  |  |  | 86 |  | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | # decoded product key | 
| 117 | 2 |  |  |  |  | 5 | my @chars; | 
| 118 |  |  |  |  |  |  |  | 
| 119 | 2 |  |  |  |  | 7 | for (my $i = $chars_length - 1; $i >= 0; $i--) { | 
| 120 | 50 |  |  |  |  | 32 | my $index = 0; | 
| 121 | 50 |  |  |  |  | 57 | for (my $j = $bytes_length - 1; $j >= 0; $j--) { | 
| 122 | 750 |  |  |  |  | 517 | my $value = ($index << 8) | $bytes[$j]; | 
| 123 | 750 |  |  |  |  | 516 | $bytes[$j] = $value / scalar @letters; | 
| 124 | 750 |  |  |  |  | 918 | $index = $value % (scalar @letters); | 
| 125 |  |  |  |  |  |  | } | 
| 126 | 50 |  |  |  |  | 60 | $chars[$i] = $letters[$index]; | 
| 127 |  |  |  |  |  |  | } | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 2 | 50 |  |  |  | 6 | if ($containsN != 0) { | 
| 130 | 2 |  |  |  |  | 2 | my $first_char = shift @chars; | 
| 131 | 2 |  |  |  |  | 3 | my $first_char_index = 0; | 
| 132 | 2 |  |  |  |  | 4 | for (my $index = 0; $index < scalar @letters; $index++) { | 
| 133 | 2 | 50 |  |  |  | 3 | next if $first_char ne $letters[$index]; | 
| 134 | 2 |  |  |  |  | 3 | $first_char_index = $index; | 
| 135 | 2 |  |  |  |  | 2 | last; | 
| 136 |  |  |  |  |  |  | } | 
| 137 |  |  |  |  |  |  |  | 
| 138 | 2 |  |  |  |  | 5 | splice @chars, $first_char_index, 0, 'N'; | 
| 139 |  |  |  |  |  |  | } | 
| 140 |  |  |  |  |  |  |  | 
| 141 | 2 |  |  |  |  | 37 | return sprintf | 
| 142 |  |  |  |  |  |  | '%s%s%s%s%s-%s%s%s%s%s-%s%s%s%s%s-%s%s%s%s%s-%s%s%s%s%s', @chars; | 
| 143 |  |  |  |  |  |  | } | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | 1; | 
| 146 |  |  |  |  |  |  | __END__ |