| 1 | package CSAF::Downloader; | ||
| 2 | |||
| 3 | use 5.010001; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use utf8; | ||
| 7 | |||
| 8 | use CSAF::Options::Downloader; | ||
| 9 | use CSAF::Util qw(file_read gpg_verify); | ||
| 10 | use CSAF; | ||
| 11 | |||
| 12 | use Cpanel::JSON::XS; | ||
| 13 | use File::Basename; | ||
| 14 | use File::Path qw(make_path); | ||
| 15 | use File::Spec::Functions qw(catfile); | ||
| 16 | use LWP::UserAgent; | ||
| 17 | use Parallel::ForkManager; | ||
| 18 | use URI::URL; | ||
| 19 | |||
| 20 | use Moo; | ||
| 21 | with 'CSAF::Util::Log'; | ||
| 22 | |||
| 23 | has options => ( | ||
| 24 | isa => sub { Carp::croak 'Invalid configurator' unless ref($_[0]) eq 'CSAF::Options::Downloader' }, | ||
| 25 | is => 'lazy', | ||
| 26 | build => 1, | ||
| 27 | ); | ||
| 28 | |||
| 29 | sub _build_options { CSAF::Options::Downloader->new } | ||
| 30 | |||
| 31 | |||
| 32 | sub ua { | ||
| 33 | |||
| 34 | my $self = shift; | ||
| 35 | |||
| 36 | my $ua = LWP::UserAgent->new( | ||
| 37 | ssl_opts => {verify_hostname => ($self->options->insecure ? !!0 : !!1)}, | ||
| 38 | agent => sprintf('CSAF/%s', $CSAF::VERSION), | ||
| 39 | protocols_allowed => ['http', 'https'] | ||
| 40 | ); | ||
| 41 | |||
| 42 | $ua->add_handler( | ||
| 43 | 'request_send' => sub { | ||
| 44 | $self->log->trace('[HTTP Request]', $_[0]->method, $_[0]->uri); | ||
| 45 | return; | ||
| 46 | } | ||
| 47 | ); | ||
| 48 | |||
| 49 | $ua->add_handler( | ||
| 50 | 'response_done' => sub { | ||
| 51 | $self->log->trace('[HTTP Response]', $_[0]->status_line); | ||
| 52 | return; | ||
| 53 | } | ||
| 54 | ); | ||
| 55 | |||
| 56 | return $ua; | ||
| 57 | |||
| 58 | } | ||
| 59 | |||
| 60 | sub mirror { | ||
| 61 | |||
| 62 | my ($self, $url) = @_; | ||
| 63 | |||
| 64 | my $is_provider_metadata = 0; | ||
| 65 | my $is_index_txt = 0; | ||
| 66 | my $is_rolie_feed = 0; | ||
| 67 | my $is_base_url = 0; | ||
| 68 | |||
| 69 | $is_index_txt = 1 if ($url =~ /\/index\.txt$/); | ||
| 70 | $is_provider_metadata = 1 if ($url =~ /\/provider-metadata\.json$/); | ||
| 71 | |||
| 72 | my $ua = $self->ua; | ||
| 73 | my $log = $self->log; | ||
| 74 | |||
| 75 | $log->info("Check: $url"); | ||
| 76 | |||
| 77 | if (my $res = $ua->head($url)) { | ||
| 78 | if (!$res->is_success) { | ||
| 79 | $log->error($res->status_line); | ||
| 80 | Carp::croak $res->message; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | $log->info('Include pattern =>', $self->options->include_pattern) if $self->options->include_pattern; | ||
| 85 | $log->info('Exclude pattern =>', $self->options->exclude_pattern) if $self->options->exclude_pattern; | ||
| 86 | |||
| 87 | my $base_url = $url; | ||
| 88 | $base_url =~ s/\/index\.txt$//; | ||
| 89 | $base_url =~ s/\/provider-metadata\.json$//; | ||
| 90 | |||
| 91 | return $self->_mirror_via_index_txt($base_url) if ($is_index_txt); | ||
| 92 | return $self->_mirror_via_provider_metadata($base_url) if ($is_provider_metadata); | ||
| 93 | |||
| 94 | return $self->_mirror_via_rolie_feed($url); | ||
| 95 | |||
| 96 | } | ||
| 97 | |||
| 98 | sub _mirror_via_rolie_feed { | ||
| 99 | |||
| 100 | my ($self, $url) = @_; | ||
| 101 | |||
| 102 | my $ua = $self->ua; | ||
| 103 | my $log = $self->log; | ||
| 104 | |||
| 105 | my $rolie_file = catfile($self->options->directory, URI::URL->new($url)->path); | ||
| 106 | my $rolie_base_dir = dirname($rolie_file); | ||
| 107 | |||
| 108 | make_path($rolie_base_dir) unless -e $rolie_base_dir; | ||
| 109 | |||
| 110 | $log->debug("Download ROLIE: $url => $rolie_file"); | ||
| 111 | |||
| 112 | $ua->mirror($url, $rolie_file); | ||
| 113 | |||
| 114 | my $rolie = eval { Cpanel::JSON::XS->new->decode(file_read($rolie_file)) }; | ||
| 115 | |||
| 116 | my $after_date = $self->options->after_date; | ||
| 117 | my $before_date = $self->options->before_date; | ||
| 118 | |||
| 119 | my $idx = 0; | ||
| 120 | my $pm = Parallel::ForkManager->new($self->options->parallel_downloads); | ||
| 121 | |||
| 122 | ENTRY: | ||
| 123 | foreach my $entry (@{$rolie->{feed}->{entry}}) { | ||
| 124 | |||
| 125 | my $options = {signature => 0, integrity => {sha256 => 0, sha512 => 0}}; | ||
| 126 | my $csaf_url = undef; | ||
| 127 | my $last_update = undef; | ||
| 128 | |||
| 129 | foreach my $link (@{$entry->{link}}) { | ||
| 130 | |||
| 131 | next unless defined $link->{rel}; | ||
| 132 | |||
| 133 | $options->{signature} = 1 if ($link->{rel} eq 'signature'); | ||
| 134 | |||
| 135 | $options->{integrity}->{sha256} = 1 if ($link->{rel} eq 'hash' & $link->{href} =~ /sha256/); | ||
| 136 | $options->{integrity}->{sha512} = 1 if ($link->{rel} eq 'hash' & $link->{href} =~ /sha512/); | ||
| 137 | |||
| 138 | $csaf_url = $link->{href} if ($link->{rel} eq 'self'); | ||
| 139 | |||
| 140 | } | ||
| 141 | |||
| 142 | if (defined $entry->{updated}) { | ||
| 143 | |||
| 144 | $last_update = Time::Piece->strptime(substr($entry->{updated}, 0, 19), '%Y-%m-%dT%H:%M:%S'); | ||
| 145 | |||
| 146 | my $skip = undef; | ||
| 147 | |||
| 148 | $skip = 1 if ($before_date && $before_date < $last_update); | ||
| 149 | $skip = 1 if ($after_date && $after_date > $last_update); | ||
| 150 | |||
| 151 | if ($skip) { | ||
| 152 | $log->debug(sprintf("[#$idx] Skip Download CSAF document: $csaf_url (last updated: %s)", | ||
| 153 | $last_update->datetime)); | ||
| 154 | next; | ||
| 155 | } | ||
| 156 | |||
| 157 | } | ||
| 158 | |||
| 159 | $idx++; | ||
| 160 | |||
| 161 | $pm->start and next ENTRY; | ||
| 162 | |||
| 163 | my $csaf_file = catfile($self->options->directory, URI::URL->new($csaf_url)->path); | ||
| 164 | |||
| 165 | $log->debug("[#$idx] Download CSAF document: $csaf_url => $csaf_file"); | ||
| 166 | $self->_download_csaf_document($csaf_url, $csaf_file, $options); | ||
| 167 | |||
| 168 | $pm->finish; | ||
| 169 | |||
| 170 | } | ||
| 171 | |||
| 172 | $pm->wait_all_children; | ||
| 173 | |||
| 174 | } | ||
| 175 | |||
| 176 | sub _mirror_via_provider_metadata { | ||
| 177 | |||
| 178 | my ($self, $base_url) = @_; | ||
| 179 | |||
| 180 | my $ua = $self->ua; | ||
| 181 | my $log = $self->log; | ||
| 182 | |||
| 183 | my $provider_metadata_file = catfile($self->options->directory, 'provider-metadata.json'); | ||
| 184 | |||
| 185 | $log->debug("Download: $base_url/provider-metadata.json => $provider_metadata_file"); | ||
| 186 | $ua->mirror("$base_url/provider-metadata.json", $provider_metadata_file); | ||
| 187 | |||
| 188 | my $provider_metadata = eval { Cpanel::JSON::XS->new->decode(file_read($provider_metadata_file)) }; | ||
| 189 | |||
| 190 | foreach my $distribution (@{$provider_metadata->{distributions}}) { | ||
| 191 | |||
| 192 | if (defined $distribution->{directory_url}) { | ||
| 193 | $self->_mirror_via_index_txt($distribution->{directory_url}); | ||
| 194 | } | ||
| 195 | |||
| 196 | if (defined $distribution->{rolie} && defined $distribution->{rolie}->{feeds}) { | ||
| 197 | |||
| 198 | $log->debug("Use ROLIE feeds"); | ||
| 199 | |||
| 200 | foreach my $feed (@{$distribution->{rolie}->{feeds}}) { | ||
| 201 | my $rolie_url = $feed->{url}; | ||
| 202 | $self->_mirror_via_rolie_feed($rolie_url); | ||
| 203 | } | ||
| 204 | } | ||
| 205 | |||
| 206 | } | ||
| 207 | |||
| 208 | } | ||
| 209 | |||
| 210 | sub _mirror_via_index_txt { | ||
| 211 | |||
| 212 | my ($self, $base_url) = @_; | ||
| 213 | |||
| 214 | $base_url =~ s{/$}{}; | ||
| 215 | |||
| 216 | my $ua = $self->ua; | ||
| 217 | my $log = $self->log; | ||
| 218 | |||
| 219 | my $base_dir = catfile($self->options->directory, URI::URL->new($base_url)->path); | ||
| 220 | make_path($base_dir) unless -e $base_dir; | ||
| 221 | |||
| 222 | my $index_file = catfile($base_dir, 'index.txt'); | ||
| 223 | my $changes_file = catfile($base_dir, 'changes.csv'); | ||
| 224 | my $provider_metadata_file = catfile($base_dir, 'provider-metadata.json'); | ||
| 225 | |||
| 226 | $log->debug("Download: $base_url/index.txt => $index_file"); | ||
| 227 | $ua->mirror("$base_url/index.txt", $index_file); | ||
| 228 | |||
| 229 | $log->debug("Download: $base_url/changes.csv => $changes_file"); | ||
| 230 | $ua->mirror("$base_url/changes.csv", $changes_file); | ||
| 231 | |||
| 232 | $log->debug("Download: $base_url/provider-metadata.json => $provider_metadata_file"); | ||
| 233 | $ua->mirror("$base_url/provider-metadata.json", $provider_metadata_file); | ||
| 234 | |||
| 235 | my $content = file_read($index_file); | ||
| 236 | my @files = split(/\n/, $content); | ||
| 237 | my $total_files = scalar @files; | ||
| 238 | |||
| 239 | $log->info("Total $total_files CSAF documents"); | ||
| 240 | |||
| 241 | my $idx = 0; | ||
| 242 | my $pm = Parallel::ForkManager->new($self->options->parallel_downloads); | ||
| 243 | |||
| 244 | FILES: | ||
| 245 | foreach my $file (@files) { | ||
| 246 | |||
| 247 | chomp($file); | ||
| 248 | next if $file =~ /^$/; | ||
| 249 | |||
| 250 | if (my $include_pattern = $self->options->include_pattern) { | ||
| 251 | next unless $file =~ qr/$include_pattern/; | ||
| 252 | } | ||
| 253 | |||
| 254 | if (my $exclude_pattern = $self->options->exclude_pattern) { | ||
| 255 | next if $file =~ qr/$exclude_pattern/; | ||
| 256 | } | ||
| 257 | |||
| 258 | $idx++; | ||
| 259 | |||
| 260 | $pm->start and next FILES; | ||
| 261 | |||
| 262 | my $csaf_file = catfile($base_dir, $file); | ||
| 263 | my $csaf_url = "$base_url/$file"; | ||
| 264 | |||
| 265 | $log->debug("[#$idx] Download CSAF document: $csaf_url => $csaf_file"); | ||
| 266 | |||
| 267 | $self->_download_csaf_document($csaf_url, $csaf_file); | ||
| 268 | |||
| 269 | $pm->finish; | ||
| 270 | |||
| 271 | } | ||
| 272 | |||
| 273 | $pm->wait_all_children; | ||
| 274 | |||
| 275 | } | ||
| 276 | |||
| 277 | sub _download_csaf_document { | ||
| 278 | |||
| 279 | my ($self, $csaf_url, $csaf_file, $options) = @_; | ||
| 280 | |||
| 281 | my $ua = $self->ua; | ||
| 282 | my $log = $self->log; | ||
| 283 | |||
| 284 | $options //= {signature => 1, integrity => {sha256 => 1, sha512 => 1}}; | ||
| 285 | |||
| 286 | my $csaf_base_dir = dirname($csaf_file); | ||
| 287 | make_path($csaf_base_dir) unless -e $csaf_base_dir; | ||
| 288 | |||
| 289 | if (my $res = $ua->mirror($csaf_url, $csaf_file)) { | ||
| 290 | |||
| 291 | if ($res->code == 200) { | ||
| 292 | |||
| 293 | $log->debug("Download signature and/or integrity files"); | ||
| 294 | |||
| 295 | $ua->mirror("$csaf_url.asc", "$csaf_file.asc") if (defined $options->{signature} && $options->{signature}); | ||
| 296 | |||
| 297 | if (defined $options->{integrity}) { | ||
| 298 | $ua->mirror("$csaf_url.sha256", "$csaf_file.sha256") if ($options->{integrity}->{sha256}); | ||
| 299 | $ua->mirror("$csaf_url.sha512", "$csaf_file.sha512") if ($options->{integrity}->{sha512}); | ||
| 300 | } | ||
| 301 | |||
| 302 | $self->_check_document($csaf_file); | ||
| 303 | |||
| 304 | } | ||
| 305 | |||
| 306 | } | ||
| 307 | |||
| 308 | } | ||
| 309 | |||
| 310 | sub _check_document { | ||
| 311 | |||
| 312 | my ($self, $csaf_file) = @_; | ||
| 313 | |||
| 314 | my $log = $self->log; | ||
| 315 | |||
| 316 | if ($self->options->validate) { | ||
| 317 | |||
| 318 | $log->info("Validate CSAF document"); | ||
| 319 | |||
| 320 | my $parser = CSAF::Parser->new(file => $csaf_file); | ||
| 321 | my $csaf = $parser->parse; | ||
| 322 | |||
| 323 | if (my @errors = $csaf->validate($self->options->validate)) { | ||
| 324 | $log->error($_) for (@errors); | ||
| 325 | } | ||
| 326 | |||
| 327 | } | ||
| 328 | |||
| 329 | if ($self->options->integrity_check) { | ||
| 330 | |||
| 331 | my @algo = (256, 512); | ||
| 332 | |||
| 333 | foreach my $algo (@algo) { | ||
| 334 | if (-e "$csaf_file.sha$algo") { | ||
| 335 | |||
| 336 | $log->info("Check integrity of CSAF document ($algo)"); | ||
| 337 | |||
| 338 | my $sha = Digest::SHA->new($algo); | ||
| 339 | $sha->addfile($csaf_file); | ||
| 340 | my $digest = $sha->hexdigest; | ||
| 341 | |||
| 342 | my ($verify) = split /\s+/, file_read("$csaf_file.sha$algo"); | ||
| 343 | |||
| 344 | if ($verify eq $digest) { | ||
| 345 | $log->info("Integrity check OK: $algo"); | ||
| 346 | } | ||
| 347 | else { | ||
| 348 | $log->error("Integrity check KO: $algo"); | ||
| 349 | Carp::croak "Integrity check failed for '$csaf_file'"; | ||
| 350 | } | ||
| 351 | |||
| 352 | } | ||
| 353 | } | ||
| 354 | |||
| 355 | } | ||
| 356 | |||
| 357 | if ($self->options->signature_check) { | ||
| 358 | |||
| 359 | if (-e "$csaf_file.asc") { | ||
| 360 | |||
| 361 | $log->info("Check signature of CSAF document"); | ||
| 362 | |||
| 363 | my $result = gpg_verify(signed => "$csaf_file.asc", file => $csaf_file); | ||
| 364 | |||
| 365 | if ($result->{exit_code} == 0) { | ||
| 366 | $log->info("Signature check OK"); | ||
| 367 | $log->trace($result->{status}); | ||
| 368 | } | ||
| 369 | else { | ||
| 370 | $log->error("Signature check FAILED"); | ||
| 371 | $log->trace($result->{status}); | ||
| 372 | Carp::croak "Signature check FAILED for '$csaf_file'"; | ||
| 373 | } | ||
| 374 | |||
| 375 | } | ||
| 376 | |||
| 377 | } | ||
| 378 | |||
| 379 | } | ||
| 380 | |||
| 381 | 1; | ||
| 382 | |||
| 383 | __END__ |