| 1 | package Net::Google::SafeBrowsing4::Storage::File; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base qw(Net::Google::SafeBrowsing4::Storage); | ||
| 7 | |||
| 8 | use Carp; | ||
| 9 | use List::Util qw(first); | ||
| 10 | use Path::Tiny; | ||
| 11 | use Storable qw(nstore retrieve); | ||
| 12 | use List::BinarySearch qw(binsearch); | ||
| 13 | |||
| 14 | |||
| 15 | our $VERSION = '0.2'; | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | Net::Google::SafeBrowsing4::Storage::File - File storage for the Google Safe Browsing v4 database | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | use Net::Google::SafeBrowsing4::Storage::File; | ||
| 24 | |||
| 25 | my $storage = Net::Google::SafeBrowsing4::Storage::File->new(path => '.'); | ||
| 26 | $storage->save(...); | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | This is the a file-based implementation of L<Net::Google::SafeBrowsing4::Storage> to manage the Google Safe Browsing v4 local database. | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | |||
| 35 | =head1 CONSTRUCTOR | ||
| 36 | |||
| 37 | =over 4 | ||
| 38 | |||
| 39 | =back | ||
| 40 | |||
| 41 | =head2 new() | ||
| 42 | |||
| 43 | Create a Net::Google::SafeBrowsing4::Storage::File object | ||
| 44 | |||
| 45 | my $storage => Net::Google::SafeBrowsing4::Storage::File->new(path => '.'); | ||
| 46 | |||
| 47 | Arguments | ||
| 48 | |||
| 49 | =over 4 | ||
| 50 | |||
| 51 | =item path | ||
| 52 | |||
| 53 | Optional. Path to store the database files Use current directory by default. | ||
| 54 | |||
| 55 | =item keep_all | ||
| 56 | |||
| 57 | Optional. Set to 1 to keep old information (such as expiring full hashes) in the database. 0 (delete) by default. | ||
| 58 | |||
| 59 | =item sticky | ||
| 60 | |||
| 61 | Optional. Set to 1 to if you are going to do multiple lookup. More memory will be used but lookup will be sped up. 0 by default. | ||
| 62 | |||
| 63 | =item files | ||
| 64 | |||
| 65 | Optional. Hash reference to map file types to file names. Default: | ||
| 66 | |||
| 67 | { | ||
| 68 | updates => "updates.gdb4", | ||
| 69 | full_hashes => "full_hashes.gsb4" | ||
| 70 | } | ||
| 71 | |||
| 72 | =item logger | ||
| 73 | |||
| 74 | Optional. Log4Perl compatible object reference. By default this option is unset, making Net::Google::SafeBrowsing4::Storage::File silent. | ||
| 75 | |||
| 76 | =back | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | sub new { | ||
| 81 | my ($class, %args) = @_; | ||
| 82 | |||
| 83 | my $self = { # default arguments | ||
| 84 | keep_all => 0, | ||
| 85 | path => '.', | ||
| 86 | sticky => 0, | ||
| 87 | files => { | ||
| 88 | updates => "updates.gdb4", | ||
| 89 | full_hashes => "full_hashes.gsb4" | ||
| 90 | }, | ||
| 91 | data => { }, | ||
| 92 | %args, | ||
| 93 | }; | ||
| 94 | |||
| 95 | bless($self, $class) or croak("Can't bless $class: $!"); | ||
| 96 | |||
| 97 | $self->init(); | ||
| 98 | |||
| 99 | return $self; | ||
| 100 | } | ||
| 101 | |||
| 102 | =head1 PUBLIC FUNCTIONS | ||
| 103 | |||
| 104 | =over 4 | ||
| 105 | |||
| 106 | See L<Net::Google::SafeBrowsing4::Storage> for a complete list of public functions. | ||
| 107 | |||
| 108 | =back | ||
| 109 | |||
| 110 | =head2 close() | ||
| 111 | |||
| 112 | Cleanup old full hashes, and close the connection to the database. | ||
| 113 | |||
| 114 | $storage->close(); | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | |||
| 119 | sub init { | ||
| 120 | my ($self, %args) = @_; | ||
| 121 | |||
| 122 | # make sure path exists | ||
| 123 | if (! -d $self->{path}) { | ||
| 124 | mkdir($self->{path}) or croak("Cannot create directory " . $self->{path} . ": $!\n"); | ||
| 125 | } | ||
| 126 | |||
| 127 | # file to hold all updates | ||
| 128 | my $file = path(join("/", $self->{path}, $self->{files}->{updates})); | ||
| 129 | if (! -e $file) { | ||
| 130 | my %update = (last_update => 0, next_update => 0, errors => 0); | ||
| 131 | if ($self->{sticky}) { | ||
| 132 | $self->{data}->{ $self->{files}->{updates} } = { %update }; | ||
| 133 | } | ||
| 134 | |||
| 135 | nstore(\%update, $file) or croak("Cannot store information into $file: $!\n"); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | sub save { | ||
| 140 | my ($self, %args) = @_; | ||
| 141 | my $list = $args{list} || croak("Missing list information"); | ||
| 142 | my $override = $args{override} || 0; | ||
| 143 | my @hashes = @{ $args{add} || [] }; | ||
| 144 | my @remove = @{ $args{remove} || [] }; | ||
| 145 | my $state = $args{'state'} || ''; | ||
| 146 | |||
| 147 | # save the information somewhere | ||
| 148 | my $file = path(join("/", $self->{path}, $self->list_to_file($list))); | ||
| 149 | $self->{logger} && $self->{logger}->debug("Save hashes to $file"); | ||
| 150 | |||
| 151 | my %data = ('state' => $state, hashes => [@hashes]); # hashes are already stored | ||
| 152 | if (-e $file && !$override) { | ||
| 153 | my $db = retrieve($file); | ||
| 154 | $self->{logger} && $self->{logger}->debug("Load $file (save)"); | ||
| 155 | |||
| 156 | $self->{logger} && $self->{logger}->debug("hashes to remove: ", scalar(@remove)); | ||
| 157 | $self->{logger} && $self->{logger}->debug("hashes to add: ", scalar(@hashes)); | ||
| 158 | |||
| 159 | $self->{logger} && $self->{logger}->debug("Number of hashes before removal: ", scalar(@{ $db->{hashes} })); | ||
| 160 | foreach my $index (@remove) { | ||
| 161 | $self->{logger} && $self->{logger}->debug("Remove index $index"); | ||
| 162 | $db->{hashes}->[$index] = ''; | ||
| 163 | } | ||
| 164 | $db->{hashes} = [ grep { $_ ne '' } @{ $db->{hashes} } ]; | ||
| 165 | $self->{logger} && $self->{logger}->debug("Number of hashes after removal: ", scalar(@{ $db->{hashes} })); | ||
| 166 | |||
| 167 | $data{hashes} = [sort { $a cmp $b } (@hashes, @{ $db->{hashes} })]; | ||
| 168 | } | ||
| 169 | |||
| 170 | nstore(\%data, $file) or croak("Cannot save data to $file: $!\n"); | ||
| 171 | if ($self->{sticky}) { | ||
| 172 | $self->{data}->{ $self->list_to_file($list) } = { %data }; | ||
| 173 | } | ||
| 174 | |||
| 175 | # return the list of hashes, sorted, from the new storage | ||
| 176 | $self->{logger} && $self->{logger}->debug("Number of hashes at end: ", scalar(@{ $data{hashes} })); | ||
| 177 | return @{ $data{hashes} }; | ||
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | |||
| 182 | sub reset { | ||
| 183 | my ($self, %args) = @_; | ||
| 184 | my $list = $args{list} || croak("Missing list information"); | ||
| 185 | |||
| 186 | my $file = path(join("/", $self->{path}, $self->list_to_file($list))); | ||
| 187 | unlink($file); | ||
| 188 | |||
| 189 | if ($self->{sticky}) { | ||
| 190 | $self->{data}->{ $self->list_to_file($list) } = { }; | ||
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 194 | |||
| 195 | sub next_update { | ||
| 196 | my ($self, %args) = @_; | ||
| 197 | |||
| 198 | # make sure the file exists | ||
| 199 | $self->init(); | ||
| 200 | |||
| 201 | my $update = { }; | ||
| 202 | if ($self->{sticky} && exists($self->{data}->{ $self->{files}->{updates} })) { | ||
| 203 | $update = $self->{data}->{ $self->{files}->{updates} }; | ||
| 204 | } | ||
| 205 | else { | ||
| 206 | # retrieve information from storage | ||
| 207 | my $file = path(join("/", $self->{path}, $self->{files}->{updates})); | ||
| 208 | $update = retrieve($file); | ||
| 209 | $self->{logger} && $self->{logger}->debug("Load $file (reset)"); | ||
| 210 | |||
| 211 | if ($self->{sticky}) { | ||
| 212 | $self->{data}->{ $self->{files}->{updates} } = $update;; | ||
| 213 | } | ||
| 214 | } | ||
| 215 | |||
| 216 | return $update->{next_update} || 0; | ||
| 217 | } | ||
| 218 | |||
| 219 | sub last_update { | ||
| 220 | my ($self, %args) = @_; | ||
| 221 | |||
| 222 | # make sure the file exists | ||
| 223 | $self->init(); | ||
| 224 | |||
| 225 | my $update = { }; | ||
| 226 | if ($self->{sticky} && exists($self->{data}->{ $self->{files}->{updates} })) { | ||
| 227 | $update = $self->{data}->{ $self->{files}->{updates} }; | ||
| 228 | } | ||
| 229 | else { | ||
| 230 | # retrieve information from storage | ||
| 231 | my $file = path(join("/", $self->{path}, $self->{files}->{updates})); | ||
| 232 | $update = retrieve($file); | ||
| 233 | $self->{logger} && $self->{logger}->debug("Load $file (last_udpate)"); | ||
| 234 | |||
| 235 | if ($self->{sticky}) { | ||
| 236 | $self->{data}->{ $self->{files}->{updates} } = $update; | ||
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | return { last_update => $update->{last_update} || 0, errors => $update->{errors} || 0 }; | ||
| 241 | } | ||
| 242 | |||
| 243 | |||
| 244 | sub get_state { | ||
| 245 | my ($self, %args) = @_; | ||
| 246 | my $list = $args{list} || croak("Missing list information\n"); | ||
| 247 | |||
| 248 | my $update = { }; | ||
| 249 | if ($self->{sticky} && exists($self->{data}->{ $self->list_to_file($list) })) { | ||
| 250 | $update = $self->{data}->{ $self->list_to_file($list) }; | ||
| 251 | } | ||
| 252 | else { | ||
| 253 | my $file = path(join("/", $self->{path}, $self->list_to_file($list))); | ||
| 254 | if (! -e $file) { | ||
| 255 | return ""; | ||
| 256 | } | ||
| 257 | else { | ||
| 258 | $self->{logger} && $self->{logger}->debug("Load $file (get_state)"); | ||
| 259 | $update = retrieve($file); | ||
| 260 | |||
| 261 | if ($self->{sticky}) { | ||
| 262 | $self->{data}->{ $self->list_to_file($list) } = $update; | ||
| 263 | } | ||
| 264 | } | ||
| 265 | } | ||
| 266 | |||
| 267 | return $update->{'state'} || ''; | ||
| 268 | } | ||
| 269 | |||
| 270 | sub updated { | ||
| 271 | my ($self, %args) = @_; | ||
| 272 | my $time = $args{'time'} || time(); | ||
| 273 | my $next = $args{'next'} || time() + 1800; | ||
| 274 | |||
| 275 | # next update applies to all lists, save it | ||
| 276 | # make sure the file exists | ||
| 277 | $self->init(); | ||
| 278 | |||
| 279 | my $file = path(join("/", $self->{path}, $self->{files}->{updates})); | ||
| 280 | my $update = { }; | ||
| 281 | if ($self->{sticky} && exists($self->{data}->{ $self->{files}->{updates} })) { | ||
| 282 | $update = $self->{data}->{ $self->{files}->{updates} }; | ||
| 283 | } | ||
| 284 | else { | ||
| 285 | # retrieve information from storage | ||
| 286 | $self->{logger} && $self->{logger}->debug("Load $file (updated)"); | ||
| 287 | $update = retrieve($file); | ||
| 288 | } | ||
| 289 | |||
| 290 | $update->{next_update} = $next; | ||
| 291 | $update->{last_udpate} = $time; | ||
| 292 | $update->{errors} = 0; | ||
| 293 | |||
| 294 | nstore($update, $file) or croak("Cannot save data to $file: $!\n"); | ||
| 295 | |||
| 296 | if ($self->{sticky}) { | ||
| 297 | $self->{data}->{ $self->{files}->{updates} } = $update; | ||
| 298 | } | ||
| 299 | } | ||
| 300 | |||
| 301 | |||
| 302 | sub update_error { | ||
| 303 | my ($self, %args) = @_; | ||
| 304 | my $time = $args{'time'} || time(); | ||
| 305 | my $wait = $args{'wait'} || 1800; | ||
| 306 | my $errors = $args{errors} || 0; | ||
| 307 | |||
| 308 | # make sure the file exists | ||
| 309 | $self->init(); | ||
| 310 | |||
| 311 | my $file = path(join("/", $self->{path}, $self->{files}->{updates})); | ||
| 312 | my $update = { }; | ||
| 313 | if ($self->{sticky} && exists($self->{data}->{ $self->{files}->{updates} })) { | ||
| 314 | $update = $self->{data}->{ $self->{files}->{updates} }; | ||
| 315 | } | ||
| 316 | else { | ||
| 317 | # retrieve information from storage | ||
| 318 | $self->{logger} && $self->{logger}->debug("Load $file (update_error)"); | ||
| 319 | $update = retrieve($file); | ||
| 320 | } | ||
| 321 | |||
| 322 | $update->{next_update} = $time + $wait; | ||
| 323 | $update->{last_udpate} = $time; | ||
| 324 | $update->{errors} = $errors; | ||
| 325 | |||
| 326 | nstore($update, $file) or croak("Cannot save data to $file: $!\n"); | ||
| 327 | if ($self->{sticky}) { | ||
| 328 | $self->{data}->{ $self->{files}->{updates} } = $update; | ||
| 329 | } | ||
| 330 | } | ||
| 331 | |||
| 332 | |||
| 333 | sub get_prefixes { | ||
| 334 | my ($self, %args) = @_; | ||
| 335 | my @lists = @{ $args{lists} || [] }; | ||
| 336 | my @hashes = @{ $args{hashes} || [] }; | ||
| 337 | my @data = (); | ||
| 338 | |||
| 339 | $self->{logger} && $self->{logger}->debug("Number of lists: ", scalar(@lists)); | ||
| 340 | foreach my $list (@lists) { | ||
| 341 | my $db = { }; | ||
| 342 | if ($self->{sticky} && exists($self->{data}->{ $self->list_to_file($list) })) { | ||
| 343 | $db = $self->{data}->{ $self->list_to_file($list) }; | ||
| 344 | } | ||
| 345 | else { | ||
| 346 | my $file = path(join("/", $self->{path}, $self->list_to_file($list))); | ||
| 347 | if (! -e $file) { | ||
| 348 | $self->{logger} && $self->{logger}->debug("File $file does not exist"); | ||
| 349 | next; | ||
| 350 | } | ||
| 351 | |||
| 352 | $self->{logger} && $self->{logger}->debug("Load $file (get_prefixes)"); | ||
| 353 | $db = retrieve($file); | ||
| 354 | |||
| 355 | if ($self->{sticky}) { | ||
| 356 | $self->{data}->{ $self->list_to_file($list) } = $db; | ||
| 357 | } | ||
| 358 | } | ||
| 359 | |||
| 360 | foreach my $hash (@hashes) { | ||
| 361 | my $prefix = undef; | ||
| 362 | my $index = binsearch {substr($a,0,length($b)) cmp $b} $hash, @{ $db->{hashes} }; | ||
| 363 | if (defined $index) { | ||
| 364 | $prefix = $db->{hashes}->[$index]; | ||
| 365 | push(@data, { prefix => $prefix, list => $list }); | ||
| 366 | } | ||
| 367 | } | ||
| 368 | } | ||
| 369 | |||
| 370 | return @data; | ||
| 371 | } | ||
| 372 | |||
| 373 | |||
| 374 | sub add_full_hashes { | ||
| 375 | my ($self, %args) = @_; | ||
| 376 | my @hashes = @{ $args{hashes} || [] }; | ||
| 377 | my $timestamp = $args{timestamp} || time(); | ||
| 378 | |||
| 379 | |||
| 380 | my $file = path(join("/", $self->{path}, $self->{files}->{full_hashes})); | ||
| 381 | my $db = { hashes => [] }; | ||
| 382 | if ($self->{sticky} && exists($self->{data}->{ $self->{files}->{full_hashes} })) { | ||
| 383 | $db = $self->{data}->{ $self->{files}->{full_hashes} }; | ||
| 384 | } | ||
| 385 | elsif (-e $file) { | ||
| 386 | $db = retrieve($file); | ||
| 387 | } | ||
| 388 | |||
| 389 | foreach my $hash (@hashes) { | ||
| 390 | my $cache = $hash->{cache}; | ||
| 391 | $cache =~ s/s//; | ||
| 392 | $self->{logger} && $self->{logger}->debug("cache: $cache"); | ||
| 393 | |||
| 394 | $hash->{expire} = $cache + $timestamp; | ||
| 395 | push(@{ $db->{hashes} }, $hash); | ||
| 396 | } | ||
| 397 | |||
| 398 | $self->{logger} && $self->{logger}->debug("Save ", scalar(@{ $db->{hashes} }), " full hashes to $file"); | ||
| 399 | nstore($db, $file) or croak("Cannot save data to $file: $!\n"); | ||
| 400 | |||
| 401 | if ($self->{sticky}) { | ||
| 402 | $self->{data}->{ $self->{files}->{full_hashes} } = $db; | ||
| 403 | } | ||
| 404 | |||
| 405 | return (@{ $db->{hashes} }); | ||
| 406 | } | ||
| 407 | |||
| 408 | |||
| 409 | sub get_full_hashes { | ||
| 410 | my ($self, %args) = @_; | ||
| 411 | my @lists = @{ $args{lists} || [] }; | ||
| 412 | my $hash = $args{hash} || return (); | ||
| 413 | |||
| 414 | my $db = { }; | ||
| 415 | if ($self->{sticky} && exists($self->{data}->{ $self->{files}->{full_hashes} })) { | ||
| 416 | $db = $self->{data}->{ $self->{files}->{full_hashes} }; | ||
| 417 | } | ||
| 418 | else { | ||
| 419 | my $file = path(join("/", $self->{path}, $self->{files}->{full_hashes})); | ||
| 420 | if (! -e $file) { | ||
| 421 | return (); | ||
| 422 | } | ||
| 423 | |||
| 424 | $self->{logger} && $self->{logger}->debug("Load $file"); | ||
| 425 | $db = retrieve($file); | ||
| 426 | } | ||
| 427 | |||
| 428 | my @hashes = (); | ||
| 429 | $self->{logger} && $self->{logger}->debug("Number of full hashes on file: ", scalar(@{ $db->{hashes} })); | ||
| 430 | foreach my $list (@lists) { | ||
| 431 | my $result = first { | ||
| 432 | $_->{hash} eq $hash && | ||
| 433 | $_->{list}->{threatEntryType} eq $list->{threatEntryType} && | ||
| 434 | $_->{list}->{threatType} eq $list->{threatType} && | ||
| 435 | $_->{list}->{platformType} eq $list->{platformType} && | ||
| 436 | $_->{expire} > time() | ||
| 437 | } @{ $db->{hashes} }; | ||
| 438 | |||
| 439 | push(@hashes, $result) if (defined($result)); | ||
| 440 | } | ||
| 441 | |||
| 442 | return @hashes; | ||
| 443 | } | ||
| 444 | |||
| 445 | |||
| 446 | sub list_to_file { | ||
| 447 | my ($self, $list) = @_; | ||
| 448 | |||
| 449 | return join("_", $list->{threatType}, $list->{platformType}, $list->{threatEntryType}) . ".gsb4"; | ||
| 450 | } | ||
| 451 | |||
| 452 | |||
| 453 | sub close { | ||
| 454 | my ($self, %args) = @_; | ||
| 455 | |||
| 456 | if ($self->{keep_all} == 0) { | ||
| 457 | return; | ||
| 458 | } | ||
| 459 | |||
| 460 | my $file = path(join("/", $self->{path}, $self->{files}->{full_hashes})); | ||
| 461 | if (! -e $file) { | ||
| 462 | return; | ||
| 463 | } | ||
| 464 | |||
| 465 | my $db = retrieve($file); | ||
| 466 | |||
| 467 | my @results = grep { $_->{expire} > time() } @{ $db->{hashes} }; | ||
| 468 | if (scalar(@results) < scalar(@{ $db->{hashes} })) { | ||
| 469 | $db->{hashes} = [@results]; | ||
| 470 | nstore($db, $file) or croak("Cannot save data to $file: $!\n"); | ||
| 471 | } | ||
| 472 | |||
| 473 | $self->{data} = { }; | ||
| 474 | } | ||
| 475 | |||
| 476 | =head1 CHANGELOG | ||
| 477 | |||
| 478 | =over 4 | ||
| 479 | |||
| 480 | |||
| 481 | =item 0.1 | ||
| 482 | |||
| 483 | Initial release | ||
| 484 | |||
| 485 | =back | ||
| 486 | |||
| 487 | =head1 SEE ALSO | ||
| 488 | |||
| 489 | See L<Net::Google::SafeBrowsing4> for handling Google Safe Browsing v4. | ||
| 490 | |||
| 491 | See L<Net::Google::SafeBrowsing4::Storage> for the list of public functions. | ||
| 492 | |||
| 493 | Google Safe Browsing v4 API: L<https://developers.google.com/safe-browsing/v4/> | ||
| 494 | |||
| 495 | |||
| 496 | =head1 AUTHOR | ||
| 497 | |||
| 498 | Julien Sobrier, E<lt>julien@sobrier.netE<gt> | ||
| 499 | |||
| 500 | =head1 COPYRIGHT AND LICENSE | ||
| 501 | |||
| 502 | Copyright (C) 2016 by Julien Sobrier | ||
| 503 | |||
| 504 | This library is free software; you can redistribute it and/or modify | ||
| 505 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 506 | at your option, any later version of Perl 5 you may have available. | ||
| 507 | |||
| 508 | |||
| 509 | =cut | ||
| 510 | |||
| 511 | 1; |