NVD/Store/DB_File.pm

filter: Statementstmt 93.3% 28 / 30 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 95.0% 38 / 40 CC 1 SCAR 0.3
CC
1
Cov
93%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« NVD/Update.pm ↑ Summary
1 package NIST::NVD::Store::DB_File;
2  
3 2 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use NIST::NVD::Store::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use base qw{NIST::NVD::Store::Base};
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 our $VERSION = '1.02.01';
12  
13 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use Storable qw(nfreeze thaw);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 0 ▶use DB_File;
Subroutine BEGIN: called CC=1 SCAR=2.6
20  
21 =head2 new
22  
23 my $NVD_Storage_ORACLE = NIST::NVD::Store::DB_File->new(
24 store => 'DB_File',
25 database => '/path/to/nvd.db',
26 cwe => '/path/to/cwe.db',
27 'idx_cpe' => '/path/to/idx_cpe.db'
28 'idx_cwe' => '/path/to/idx_cwe.db'
29 mode => $mode, # perldoc DB_File
30 );
31  
32 =cut
33  
34 sub new {
35 my ( $class, %args ) = @_;
36 $class = ref $class || $class;
37  
38 my $args = {
39 filename => [qw{ database idx_cpe idx_cwe cwe }],
40 database => [qw{ database idx_cpe idx_cwe cwe }],
41 required => [qw{ database }],
42 };
43  
44 unless( exists $args{idx_cpe} ){
45 ($args{idx_cpe} = $args{database}) =~ s/(\.db)$/.idx_cpe$1/;
46 }
47  
48 unless( exists $args{idx_cwe} ){
49 ($args{idx_cwe} = $args{database}) =~ s/(\.db)$/.idx_cwe$1/;
50 }
51  
52 unless( exists $args{cwe} ){
53 ($args{cwe} = $args{database}) =~ s:/(nvd\.db)$:cwec_v2.2.db:;
54 }
55  
56 $args{mode} //= O_RDONLY;
57  
58 my $fail = 0;
59 foreach my $req_arg ( @{ $args->{required} } ) {
60 unless ( $args{$req_arg} ) {
61 carp "'$req_arg' is a required argument to __PACKAGE__::new\n";
62 $fail++;
63 }
64 }
65 return if $fail;
66  
67 my $self = { vuln_software => {} };
68 foreach my $arg ( keys %args ) {
69 if ( grep { $_ eq $arg } @{ $args->{filename} } ) {
70  
71 unless ( ( $args{mode} & O_CREAT ) == O_CREAT || -f $args{$arg} ) {
72 carp "$arg file '$args{$arg}' does not exist\n";
73 $fail++;
74 }
75 }
76 if ( grep { $_ eq $arg } @{ $args->{database} } ) {
77 my %tied_hash;
78 $self->{$arg} = \%tied_hash;
79 $self->{"$arg.db"} = tie %tied_hash, 'DB_File', $args{$arg},
80 $args{mode};
81  
82 unless ( $self->{"$arg.db"} ) {
83 carp "failed to open database '$args{$arg}': $!";
84 $fail++;
85 }
86 }
87 }
88 return if $fail;
89  
90 bless $self, $class;
91  
92 }
93  
94 =head2 get_cve_for_cpe
95  
96 =cut
97  
98 sub get_cve_for_cpe {
99 my ( $self, %args ) = @_;
100 my $frozen;
101  
102 my $result = $self->{'idx_cpe.db'}->get( $args{cpe}, $frozen );
103  
104 my $cve_ids = [];
105  
106 unless ( $result == 0 ) {
107 return $cve_ids;
108 }
109  
110 $cve_ids = eval { thaw $frozen };
111 if (@$) {
112 carp "Storable::thaw had a major malfunction: $@";
113 return;
114 }
115  
116 return $cve_ids;
117 }
118  
119 =head2 get_cve
120  
121  
122 =cut
123  
124 sub get_cve {
125 my ( $self, %args ) = @_;
126  
127 my $compressed;
128  
129 my $result = $self->{'database.db'}->get( $args{cve_id}, $compressed );
130  
131 unless ( $result == 0 ) {
132 carp "failed to retrieve CVE '$args{cve_id}': $!\n";
133 return;
134 }
135  
136 my $frozen;
137  
138 my $status = bunzip2( \$compressed, \$frozen );
139 unless ($status) {
140 carp "bunzip2 failed: $Bunzip2Error\n";
141 return;
142 }
143  
144 my $entry = eval { thaw $frozen };
145 if (@$) {
146 carp "Storable::thaw had a major malfunction.";
147 return;
148 }
149  
150 return $entry;
151 }
152  
153 =head2 put_cve_idx_cpe
154  
155 $NVD_Storage_ORACLE->put_cve_idx_cpe( $cpe_urn, $value )
156  
157 =cut
158  
159 sub put_cve_idx_cpe {
160 my ( $self, $vuln_software ) = @_;
161 foreach my $cpe_urn ( keys %$vuln_software ) {
162 my $frozen;
163  
164 $self->{'idx_cpe.db'}->get( $cpe_urn, $frozen );
165  
166 if ($frozen) {
167 my $thawed = thaw($frozen);
168 next unless ref $thawed eq 'ARRAY';
169  
170 my @vuln_list = ();
171  
172 @vuln_list = @{ $self->{vuln_software}->{$cpe_urn} }
173 if ref $self->{vuln_software}->{$cpe_urn} eq 'ARRAY';
174  
175 # Combine previous results with these results
176 $vuln_software->{$cpe_urn} = [ @vuln_list, @{$thawed} ];
177 }
178  
179 $frozen = nfreeze( $vuln_software->{$cpe_urn} );
180  
181 $self->{'idx_cpe.db'}->put( $cpe_urn, $frozen );
182 }
183 }
184  
185 =head2 put_nvd_entries
186  
187 $NVD_Storage_ORACLE->put_nvd_entries( $entries )
188  
189 =cut
190  
191 sub put_nvd_entries {
192 my ( $self, $entries ) = @_;
193  
194 while ( my ( $cve_id, $entry ) = ( each %$entries ) ) {
195  
196 my $frozen = nfreeze($entry);
197  
198 my $compressed;
199  
200 bzip2 \$frozen => \$compressed
201 or die "bzip2 failed: $Bzip2Error\n";
202  
203 $self->{'database.db'}->put( $cve_id, $compressed );
204 }
205 }
206  
207 sub _get_default_args {
208 return ( mode => O_CREAT | O_RDWR );
209  
210 }
211  
212 1;
« NVD/Update.pm ↑ Summary