| 1 | package Net::Google::SafeBrowsing2::DBI; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'Net::Google::SafeBrowsing2::Storage'; | ||
| 7 | |||
| 8 | use Carp; | ||
| 9 | use DBI; | ||
| 10 | use List::Util qw(first); | ||
| 11 | |||
| 12 | |||
| 13 | our $VERSION = '1.01'; | ||
| 14 | |||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | Net::Google::SafeBrowsing2::DBI - Base class for all DBI-based back-end storage for the Google Safe Browsing v2 database | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | Net::Google::SafeBrowsing2::DBI cannot be used directly. Instead, use a class inheriting Net::Google::SafeBrowsing2::DBI, like L<Net::Google::SafeBrowsing2::MySQL>. | ||
| 23 | |||
| 24 | |||
| 25 | use Net::Google::SafeBrowsing2::MySQL; | ||
| 26 | |||
| 27 | my $storage = Net::Google::SafeBrowsing2::MySQL->new(host => '127.0.0.1', database => 'GoogleSafeBrowsingv2'); | ||
| 28 | ... | ||
| 29 | $storage->close(); | ||
| 30 | |||
| 31 | =head1 DESCRIPTION | ||
| 32 | |||
| 33 | This is a base implementation of L<Net::Google::SafeBrowsing2::Storage> using DBI. | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | |||
| 38 | =head1 CONSTRUCTOR | ||
| 39 | |||
| 40 | =over 4 | ||
| 41 | |||
| 42 | =back | ||
| 43 | |||
| 44 | =head2 new() | ||
| 45 | |||
| 46 | This method should be overwritten. | ||
| 47 | |||
| 48 | Arguments | ||
| 49 | |||
| 50 | =over 4 | ||
| 51 | |||
| 52 | =item keep_all | ||
| 53 | |||
| 54 | Optional. Set to 1 to keep old information (such as expiring full hashes) in the database. 0 (delete) by default. | ||
| 55 | |||
| 56 | |||
| 57 | =back | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | sub new { | ||
| 62 | my ($class, %args) = @_; | ||
| 63 | |||
| 64 | my $self = { # default arguments | ||
| 65 | keep_all => 0, | ||
| 66 | %args, | ||
| 67 | }; | ||
| 68 | |||
| 69 | bless $self, $class or croak "Can't bless $class: $!"; | ||
| 70 | |||
| 71 | |||
| 72 | $self->init(); | ||
| 73 | |||
| 74 | return $self; | ||
| 75 | } | ||
| 76 | |||
| 77 | =head1 PUBLIC FUNCTIONS | ||
| 78 | |||
| 79 | =over 4 | ||
| 80 | |||
| 81 | See L<Net::Google::SafeBrowsing2::Storage> for a complete list of public functions. | ||
| 82 | |||
| 83 | =back | ||
| 84 | |||
| 85 | =head2 close() | ||
| 86 | |||
| 87 | Cleanup old full hashes, and close the connection to the database. | ||
| 88 | |||
| 89 | $storage->close(); | ||
| 90 | |||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub close { | ||
| 95 | my ($self, %args) = @_; | ||
| 96 | |||
| 97 | |||
| 98 | if ($self->{keep_all} == 0) { | ||
| 99 | $self->{dbh}->do('DELETE FROM full_hashes WHERE timestamp < ?', { }, time() - Net::Google::SafeBrowsing2::FULL_HASH_TIME); | ||
| 100 | } | ||
| 101 | |||
| 102 | $self->{dbh}->disconnect; | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | =head2 export() | ||
| 107 | |||
| 108 | Export add chunks and sub chunks to a file. The file content looks like what Google sends in redirections. The file can be used with the C<import_chunks> function from C<Net::Google::SafeBrowsing2>. This is useful when moving from one back-end storage to another | ||
| 109 | |||
| 110 | $storage->export(list => MALWARE); | ||
| 111 | |||
| 112 | Arguments | ||
| 113 | |||
| 114 | =over 4 | ||
| 115 | |||
| 116 | =item list | ||
| 117 | |||
| 118 | Required. The Google Safe Browsing list to export. | ||
| 119 | |||
| 120 | =item file | ||
| 121 | |||
| 122 | Optional. Filename to export to. Uses "$list.dat" by default. | ||
| 123 | |||
| 124 | =back | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | sub export { | ||
| 129 | my ($self, %args) = @_; | ||
| 130 | my $list = $args{list} || ''; | ||
| 131 | my $file = $args{file} || "$list.dat"; | ||
| 132 | |||
| 133 | |||
| 134 | open EXPORT, "> $file" or croak "Cannot open $file: $!\n"; | ||
| 135 | binmode EXPORT; | ||
| 136 | |||
| 137 | # Add chunks | ||
| 138 | my $num = 0; | ||
| 139 | my $chunk_data = ''; | ||
| 140 | my $hostkey = ''; | ||
| 141 | my $prefixes = ''; | ||
| 142 | my $count = 0; | ||
| 143 | |||
| 144 | my $sth = $self->{dbh}->prepare("SELECT * FROM a_chunks WHERE list = ? ORDER BY num ASC"); | ||
| 145 | $sth->execute($list); | ||
| 146 | |||
| 147 | while (my $row = $sth->fetchrow_hashref()) { | ||
| 148 | if (($num != $row->{num} && $num != 0) || $num > 1000) { # if num is too bif, we can not print chr($num) a a single byte | ||
| 149 | $chunk_data .= $hostkey; | ||
| 150 | $chunk_data .= chr($count); | ||
| 151 | $chunk_data .= $prefixes; | ||
| 152 | |||
| 153 | print EXPORT "a:", $num, ":", length($hostkey), ":", length($chunk_data), "\n"; | ||
| 154 | print EXPORT $chunk_data; | ||
| 155 | |||
| 156 | $num = $row->{num}; | ||
| 157 | $chunk_data = ''; | ||
| 158 | $hostkey = ''; | ||
| 159 | $prefixes = ''; | ||
| 160 | $count = 0; | ||
| 161 | } | ||
| 162 | elsif ($num == 0) { | ||
| 163 | $num = $row->{num}; | ||
| 164 | $hostkey = $row->{hostkey}; | ||
| 165 | } | ||
| 166 | if ($hostkey ne $row->{hostkey} && $hostkey ne '') { | ||
| 167 | $chunk_data .= $hostkey; | ||
| 168 | $chunk_data .= chr($count); | ||
| 169 | $chunk_data .= $prefixes; | ||
| 170 | |||
| 171 | $count = 0; | ||
| 172 | $prefixes = ''; | ||
| 173 | } | ||
| 174 | $hostkey = $row->{hostkey}; | ||
| 175 | if (length($row->{prefix}) > 0) { | ||
| 176 | $prefixes .= $row->{prefix}; | ||
| 177 | $count++; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | $sth->finish(); | ||
| 181 | |||
| 182 | $chunk_data .= $hostkey; | ||
| 183 | $chunk_data .= chr($count); | ||
| 184 | $chunk_data .= $prefixes; | ||
| 185 | print EXPORT "a:", $num, ":", length($hostkey), ":", length($chunk_data) , "\n"; | ||
| 186 | print EXPORT $chunk_data; | ||
| 187 | |||
| 188 | |||
| 189 | # sub chunks | ||
| 190 | $num = 0; | ||
| 191 | $chunk_data = ''; | ||
| 192 | $hostkey = ''; | ||
| 193 | $prefixes = ''; | ||
| 194 | $count = 0; | ||
| 195 | |||
| 196 | $sth = $self->{dbh}->prepare("SELECT * FROM s_chunks WHERE list = ? ORDER BY num ASC"); | ||
| 197 | $sth->execute($list); | ||
| 198 | |||
| 199 | |||
| 200 | while (my $row = $sth->fetchrow_hashref()) { | ||
| 201 | if (($num != $row->{num} && $num != 0) || $num > 1000) { # if num is too bif, we can not print chr($num) a a single byte | ||
| 202 | $chunk_data .= $hostkey; | ||
| 203 | $chunk_data .= chr($count) if (length($hostkey) > 0); | ||
| 204 | $chunk_data .= $prefixes; | ||
| 205 | |||
| 206 | print EXPORT "s:", $num, ":", length($hostkey), ":", length($chunk_data), "\n"; | ||
| 207 | print EXPORT $chunk_data; | ||
| 208 | |||
| 209 | $num = $row->{num}; | ||
| 210 | $chunk_data = ''; | ||
| 211 | $hostkey = ''; | ||
| 212 | $prefixes = ''; | ||
| 213 | $count = 0; | ||
| 214 | } | ||
| 215 | elsif ($num == 0) { | ||
| 216 | $num = $row->{num}; | ||
| 217 | $hostkey = $row->{hostkey}; | ||
| 218 | } | ||
| 219 | if ($hostkey ne $row->{hostkey} && $hostkey ne '') { | ||
| 220 | $chunk_data .= $hostkey; | ||
| 221 | $chunk_data .= chr($count) if (length($hostkey) > 0); | ||
| 222 | $chunk_data .= $prefixes; | ||
| 223 | |||
| 224 | $count = 0; | ||
| 225 | $prefixes = ''; | ||
| 226 | } | ||
| 227 | $hostkey = $row->{hostkey}; | ||
| 228 | if ($row->{add_num} > 0) { | ||
| 229 | $prefixes .= $self->ascii_to_hex( sprintf("%08x", $row->{add_num}) ); | ||
| 230 | } | ||
| 231 | |||
| 232 | if (length($row->{prefix}) > 0 && $row->{add_num} > 0) { | ||
| 233 | $prefixes .= $row->{prefix}; | ||
| 234 | $count++; | ||
| 235 | } | ||
| 236 | } | ||
| 237 | $sth->finish(); | ||
| 238 | |||
| 239 | $chunk_data .= $hostkey; | ||
| 240 | $chunk_data .= chr($count) if (length($hostkey) > 0); | ||
| 241 | $chunk_data .= $prefixes; | ||
| 242 | print EXPORT "s:", $num, ":", length($hostkey), ":", length($chunk_data) , "\n"; | ||
| 243 | print EXPORT $chunk_data; | ||
| 244 | |||
| 245 | CORE::close(EXPORT); | ||
| 246 | } | ||
| 247 | |||
| 248 | sub init { | ||
| 249 | my ($self, %args) = @_; | ||
| 250 | |||
| 251 | # Should connect to database | ||
| 252 | # Should check if all tables exist | ||
| 253 | } | ||
| 254 | |||
| 255 | |||
| 256 | sub create_table_updates { | ||
| 257 | my ($self, %args) = @_; | ||
| 258 | |||
| 259 | my $schema = qq{ | ||
| 260 | CREATE TABLE updates ( | ||
| 261 | last INT NOT NULL DEFAULT '0', | ||
| 262 | wait INT NOT NULL DEFAULT '0', | ||
| 263 | errors INT NOT NULL DEFAULT '1800', | ||
| 264 | list VARCHAR( 50 ) NOT NULL | ||
| 265 | ); | ||
| 266 | }; # Need to handle errors | ||
| 267 | |||
| 268 | $self->{dbh}->do($schema); | ||
| 269 | } | ||
| 270 | |||
| 271 | sub create_table_a_chunks { | ||
| 272 | my ($self, %args) = @_; | ||
| 273 | |||
| 274 | my $schema = qq{ | ||
| 275 | CREATE TABLE a_chunks ( | ||
| 276 | hostkey VARCHAR( 8 ), | ||
| 277 | prefix VARCHAR( 8 ), | ||
| 278 | num INT NOT NULL, | ||
| 279 | list VARCHAR( 50 ) NOT NULL | ||
| 280 | ); | ||
| 281 | }; | ||
| 282 | |||
| 283 | $self->{dbh}->do($schema); | ||
| 284 | |||
| 285 | my $index = qq{ | ||
| 286 | CREATE INDEX a_chunks_hostkey ON a_chunks ( | ||
| 287 | hostkey | ||
| 288 | ); | ||
| 289 | }; | ||
| 290 | $self->{dbh}->do($index); | ||
| 291 | |||
| 292 | $index = qq{ | ||
| 293 | CREATE INDEX a_chunks_num_list ON a_chunks ( | ||
| 294 | num, | ||
| 295 | list | ||
| 296 | ); | ||
| 297 | }; | ||
| 298 | $self->{dbh}->do($index); | ||
| 299 | |||
| 300 | $index = qq{ | ||
| 301 | CREATE UNIQUE INDEX a_chunks_unique ON a_chunks ( | ||
| 302 | hostkey, | ||
| 303 | prefix, | ||
| 304 | num, | ||
| 305 | list | ||
| 306 | ); | ||
| 307 | }; | ||
| 308 | $self->{dbh}->do($index); | ||
| 309 | } | ||
| 310 | |||
| 311 | sub create_table_s_chunks { | ||
| 312 | my ($self, %args) = @_; | ||
| 313 | |||
| 314 | my $schema = qq{ | ||
| 315 | CREATE TABLE s_chunks ( | ||
| 316 | hostkey VARCHAR( 8 ), | ||
| 317 | prefix VARCHAR( 8 ), | ||
| 318 | num INT NOT NULL, | ||
| 319 | add_num INT Default '0', | ||
| 320 | list VARCHAR( 50 ) NOT NULL | ||
| 321 | ); | ||
| 322 | }; | ||
| 323 | |||
| 324 | $self->{dbh}->do($schema); | ||
| 325 | |||
| 326 | my $index = qq{ | ||
| 327 | CREATE INDEX s_chunks_hostkey ON s_chunks ( | ||
| 328 | hostkey | ||
| 329 | ); | ||
| 330 | }; | ||
| 331 | $self->{dbh}->do($index); | ||
| 332 | |||
| 333 | $index = qq{ | ||
| 334 | CREATE INDEX s_chunks_num ON s_chunks ( | ||
| 335 | num | ||
| 336 | ); | ||
| 337 | }; | ||
| 338 | $self->{dbh}->do($index); | ||
| 339 | |||
| 340 | $index = qq{ | ||
| 341 | CREATE INDEX s_chunks_num_list ON s_chunks ( | ||
| 342 | num, | ||
| 343 | list | ||
| 344 | ); | ||
| 345 | }; | ||
| 346 | $self->{dbh}->do($index); | ||
| 347 | |||
| 348 | $index = qq{ | ||
| 349 | CREATE UNIQUE INDEX s_chunks_unique ON s_chunks ( | ||
| 350 | hostkey, | ||
| 351 | prefix, | ||
| 352 | num, | ||
| 353 | add_num, | ||
| 354 | list | ||
| 355 | ); | ||
| 356 | }; | ||
| 357 | $self->{dbh}->do($index); | ||
| 358 | } | ||
| 359 | |||
| 360 | sub create_table_full_hashes { | ||
| 361 | my ($self, %args) = @_; | ||
| 362 | |||
| 363 | my $schema = qq{ | ||
| 364 | CREATE TABLE full_hashes ( | ||
| 365 | id INT AUTO_INCREMENT PRIMARY KEY, | ||
| 366 | num INT, | ||
| 367 | hash VARCHAR( 32 ), | ||
| 368 | list VARCHAR( 50 ), | ||
| 369 | timestamp INT Default '0' | ||
| 370 | ); | ||
| 371 | }; | ||
| 372 | |||
| 373 | $self->{dbh}->do($schema); | ||
| 374 | |||
| 375 | my $index = qq{ | ||
| 376 | CREATE UNIQUE INDEX hash ON full_hashes ( | ||
| 377 | num, | ||
| 378 | hash, | ||
| 379 | list | ||
| 380 | ); | ||
| 381 | }; | ||
| 382 | $self->{dbh}->do($index); | ||
| 383 | } | ||
| 384 | |||
| 385 | sub create_table_full_hashes_errors { | ||
| 386 | my ($self, %args) = @_; | ||
| 387 | |||
| 388 | my $schema = qq{ | ||
| 389 | CREATE TABLE full_hashes_errors ( | ||
| 390 | id INT AUTO_INCREMENT PRIMARY KEY, | ||
| 391 | errors INT Default '0', | ||
| 392 | prefix VARCHAR( 8 ), | ||
| 393 | timestamp INT Default '0' | ||
| 394 | ); | ||
| 395 | }; | ||
| 396 | |||
| 397 | $self->{dbh}->do($schema); | ||
| 398 | } | ||
| 399 | |||
| 400 | sub create_table_mac_keys{ | ||
| 401 | my ($self, %args) = @_; | ||
| 402 | |||
| 403 | my $schema = qq{ | ||
| 404 | CREATE TABLE mac_keys ( | ||
| 405 | client_key VARCHAR( 50 ) Default '', | ||
| 406 | wrapped_key VARCHAR( 50 ) Default '' | ||
| 407 | ); | ||
| 408 | }; | ||
| 409 | |||
| 410 | $self->{dbh}->do($schema); | ||
| 411 | } | ||
| 412 | |||
| 413 | |||
| 414 | sub add_chunks { | ||
| 415 | my ($self, %args) = @_; | ||
| 416 | my $type = $args{type} || 'a'; | ||
| 417 | my $chunknum = $args{chunknum} || 0; | ||
| 418 | my $chunks = $args{chunks} || []; | ||
| 419 | my $list = $args{'list'} || ''; | ||
| 420 | |||
| 421 | # $self->{dbh}->do("PRAGMA journal_mode = OFF"); | ||
| 422 | # $self->{dbh}->do("PRAGMA synchronous = OFF"); | ||
| 423 | |||
| 424 | if ($type eq 's') { | ||
| 425 | $self->add_chunks_s(chunknum => $chunknum, chunks => $chunks, list => $list); | ||
| 426 | } | ||
| 427 | elsif ($type eq 'a') { | ||
| 428 | $self->add_chunks_a(chunknum => $chunknum, chunks => $chunks, list => $list); | ||
| 429 | } | ||
| 430 | |||
| 431 | # $self->{dbh}->do("PRAGMA journal_mode = DELETE"); | ||
| 432 | # $self->{dbh}->do("PRAGMA synchronous = FULL"); | ||
| 433 | } | ||
| 434 | |||
| 435 | sub add_chunks_s { | ||
| 436 | my ($self, %args) = @_; | ||
| 437 | my $chunknum = $args{chunknum} || 0; | ||
| 438 | my $chunks = $args{chunks} || []; | ||
| 439 | my $list = $args{'list'} || ''; | ||
| 440 | |||
| 441 | my $add = $self->{dbh}->prepare('INSERT INTO s_chunks (hostkey, prefix, num, add_num, list) VALUES (?, ?, ?, ?, ?)'); | ||
| 442 | my $del = $self->{dbh}->prepare('DELETE FROM s_chunks WHERE hostkey = ? AND prefix = ? AND num = ? AND add_num = ? AND list = ?'); | ||
| 443 | |||
| 444 | foreach my $chunk (@$chunks) { | ||
| 445 | $del->execute( $chunk->{host}, $chunk->{prefix}, $chunknum, $chunk->{add_chunknum}, $list ); | ||
| 446 | $add->execute( $chunk->{host}, $chunk->{prefix}, $chunknum, $chunk->{add_chunknum}, $list ); | ||
| 447 | } | ||
| 448 | |||
| 449 | if (scalar @$chunks == 0) { # keep empty chunks | ||
| 450 | $del->execute( '', '', $chunknum, '', $list ); | ||
| 451 | $add->execute( '', '', $chunknum, '', $list ); | ||
| 452 | } | ||
| 453 | } | ||
| 454 | |||
| 455 | sub add_chunks_a { | ||
| 456 | my ($self, %args) = @_; | ||
| 457 | my $chunknum = $args{chunknum} || 0; | ||
| 458 | my $chunks = $args{chunks} || []; | ||
| 459 | my $list = $args{'list'} || ''; | ||
| 460 | |||
| 461 | my $add = $self->{dbh}->prepare('INSERT INTO a_chunks (hostkey, prefix, num, list) VALUES (?, ?, ?, ?)'); | ||
| 462 | my $del = $self->{dbh}->prepare('DELETE FROM a_chunks WHERE hostkey = ? AND prefix = ? AND num = ? AND list = ?'); | ||
| 463 | |||
| 464 | foreach my $chunk (@$chunks) { | ||
| 465 | $del->execute( $chunk->{host}, $chunk->{prefix}, $chunknum, $list ); | ||
| 466 | $add->execute( $chunk->{host}, $chunk->{prefix}, $chunknum, $list ); | ||
| 467 | } | ||
| 468 | |||
| 469 | if (scalar @$chunks == 0) { # keep empty chunks | ||
| 470 | $del->execute( '', '', $chunknum, $list ); | ||
| 471 | $add->execute( '', '', $chunknum, $list ); | ||
| 472 | } | ||
| 473 | } | ||
| 474 | |||
| 475 | |||
| 476 | sub get_add_chunks { | ||
| 477 | my ($self, %args) = @_; | ||
| 478 | my $hostkey = $args{hostkey} || ''; | ||
| 479 | # my $list = $args{'list'} || ''; | ||
| 480 | |||
| 481 | my @list = (); | ||
| 482 | # my $rows = $self->{dbh}->selectall_arrayref("SELECT * FROM a_chunks WHERE hostkey = ? AND list = ?", { Slice => {} }, $hostkey, $list); | ||
| 483 | my $rows = $self->{dbh}->selectall_arrayref("SELECT * FROM a_chunks WHERE hostkey = ?", { Slice => {} }, $hostkey); | ||
| 484 | |||
| 485 | foreach my $row (@$rows) { | ||
| 486 | push(@list, { chunknum => $row->{num}, prefix => $row->{prefix}, list => $row->{list}, hostkey => $hostkey }); | ||
| 487 | } | ||
| 488 | |||
| 489 | return @list; | ||
| 490 | } | ||
| 491 | |||
| 492 | sub get_sub_chunks { | ||
| 493 | my ($self, %args) = @_; | ||
| 494 | my $hostkey = $args{hostkey} || ''; | ||
| 495 | # my $list = $args{'list'} || ''; | ||
| 496 | |||
| 497 | my @list = (); | ||
| 498 | # my $rows = $self->{dbh}->selectall_arrayref("SELECT * FROM s_chunks WHERE hostkey = ? AND list = ?", { Slice => {} }, $hostkey, $list); | ||
| 499 | my $rows = $self->{dbh}->selectall_arrayref("SELECT * FROM s_chunks WHERE hostkey = ?", { Slice => {} }, $hostkey); | ||
| 500 | |||
| 501 | foreach my $row (@$rows) { | ||
| 502 | push(@list, { chunknum => $row->{num}, prefix => $row->{prefix}, addchunknum => $row->{add_num}, list => $row->{list} }); | ||
| 503 | } | ||
| 504 | |||
| 505 | return @list; | ||
| 506 | } | ||
| 507 | |||
| 508 | sub get_add_chunks_nums { | ||
| 509 | my ($self, %args) = @_; | ||
| 510 | my $list = $args{'list'} || ''; | ||
| 511 | |||
| 512 | my @list = (); | ||
| 513 | my $rows = $self->{dbh}->selectall_arrayref("SELECT DISTINCT(num) FROM a_chunks WHERE list = ? ORDER BY num ASC", { Slice => {} }, $list); | ||
| 514 | foreach my $row (@$rows) { | ||
| 515 | push(@list, $row->{num}); | ||
| 516 | } | ||
| 517 | |||
| 518 | return @list; | ||
| 519 | } | ||
| 520 | |||
| 521 | sub get_sub_chunks_nums { | ||
| 522 | my ($self, %args) = @_; | ||
| 523 | my $list = $args{'list'} || ''; | ||
| 524 | |||
| 525 | my @list = (); | ||
| 526 | my $rows = $self->{dbh}->selectall_arrayref("SELECT DISTINCT(num) FROM s_chunks WHERE list = ? ORDER BY num ASC", { Slice => {} }, $list); | ||
| 527 | foreach my $row (@$rows) { | ||
| 528 | push(@list, $row->{num}); | ||
| 529 | } | ||
| 530 | |||
| 531 | return @list; | ||
| 532 | } | ||
| 533 | |||
| 534 | |||
| 535 | sub delete_add_ckunks { | ||
| 536 | my ($self, %args) = @_; | ||
| 537 | my $chunknums = $args{chunknums} || []; | ||
| 538 | my $list = $args{'list'} || ''; | ||
| 539 | |||
| 540 | my $sth = $self->{dbh}->prepare("DELETE FROM a_chunks WHERE num = ? AND list = ?"); | ||
| 541 | |||
| 542 | foreach my $num (@$chunknums) { | ||
| 543 | $sth->execute($num, $list); | ||
| 544 | } | ||
| 545 | } | ||
| 546 | |||
| 547 | |||
| 548 | sub delete_sub_ckunks { | ||
| 549 | my ($self, %args) = @_; | ||
| 550 | my $chunknums = $args{chunknums} || []; | ||
| 551 | my $list = $args{'list'} || ''; | ||
| 552 | |||
| 553 | my $sth = $self->{dbh}->prepare("DELETE FROM s_chunks WHERE num = ? AND list = ?"); | ||
| 554 | |||
| 555 | foreach my $num (@$chunknums) { | ||
| 556 | $sth->execute($num, $list); | ||
| 557 | } | ||
| 558 | |||
| 559 | |||
| 560 | } | ||
| 561 | |||
| 562 | sub get_full_hashes { | ||
| 563 | my ($self, %args) = @_; | ||
| 564 | my $chunknum = $args{chunknum} || 0; | ||
| 565 | my $timestamp = $args{timestamp} || 0; | ||
| 566 | my $list = $args{list} || ''; | ||
| 567 | |||
| 568 | my @hashes = (); | ||
| 569 | |||
| 570 | my $rows = $self->{dbh}->selectall_arrayref("SELECT hash FROM full_hashes WHERE timestamp >= ? AND num = ? AND list = ?", { Slice => {} }, $timestamp, $chunknum, $list); | ||
| 571 | foreach my $row (@$rows) { | ||
| 572 | push(@hashes, $row->{hash}); | ||
| 573 | } | ||
| 574 | |||
| 575 | return @hashes; | ||
| 576 | } | ||
| 577 | |||
| 578 | |||
| 579 | sub updated { | ||
| 580 | my ($self, %args) = @_; | ||
| 581 | my $time = $args{'time'} || time; | ||
| 582 | my $wait = $args{'wait'} || 1800; | ||
| 583 | my $list = $args{'list'} || ''; | ||
| 584 | |||
| 585 | if ($self->last_update(list => $list)->{'time'} == 0) { | ||
| 586 | $self->{dbh}->do("INSERT INTO updates (last, wait, errors, list) VALUES (?, ?, 0, ?)", undef, $time, $wait, $list); | ||
| 587 | } | ||
| 588 | else { | ||
| 589 | $self->{dbh}->do("UPDATE updates SET last = ?, wait = ?, errors = 0 WHERE list = ?", undef, $time, $wait, $list); | ||
| 590 | } | ||
| 591 | } | ||
| 592 | |||
| 593 | sub update_error { | ||
| 594 | my ($self, %args) = @_; | ||
| 595 | my $time = $args{'time'} || time; | ||
| 596 | my $list = $args{'list'} || ''; | ||
| 597 | my $wait = $args{'wait'} || 60; | ||
| 598 | my $errors = $args{errors} || 1; | ||
| 599 | |||
| 600 | if ($self->last_update(list => $list)->{'time'} == 0) { | ||
| 601 | $self->{dbh}->do("INSERT INTO updates (last, wait, errors, list) VALUES (?, ?, ?, ?)", undef, $time, $wait, $errors, $list); | ||
| 602 | } | ||
| 603 | else { | ||
| 604 | $self->{dbh}->do("UPDATE updates SET last = ?, wait = ?, errors = ?, list = ? WHERE 1", undef, $time, $wait, $errors, $list); | ||
| 605 | } | ||
| 606 | } | ||
| 607 | |||
| 608 | sub last_update { | ||
| 609 | my ($self, %args) = @_; | ||
| 610 | my $list = $args{'list'} || ''; | ||
| 611 | |||
| 612 | my $rows = $self->{dbh}->selectall_arrayref("SELECT last, wait, errors FROM updates WHERE list = ? LIMIT 1", { Slice => {} }, $list); | ||
| 613 | |||
| 614 | foreach my $row (@$rows) { | ||
| 615 | return {'time' => $row->{'last'} || 0, 'wait' => $row->{'wait'} || 1800, errors => $row->{'errors'} || 0}; | ||
| 616 | } | ||
| 617 | |||
| 618 | return {'time' => 0, 'wait' => 1800}; | ||
| 619 | } | ||
| 620 | |||
| 621 | sub add_full_hashes { | ||
| 622 | my ($self, %args) = @_; | ||
| 623 | my $timestamp = $args{timestamp} || time(); | ||
| 624 | my $full_hashes = $args{full_hashes} || []; | ||
| 625 | |||
| 626 | foreach my $hash (@$full_hashes) { | ||
| 627 | # $self->{dbh}->do("INSERT OR REPLACE INTO full_hashes (num, hash, list, timestamp) VALUES (?, ?, ?, ?)", { }, $hash->{chunknum}, $hash->{hash}, $hash->{list}, $timestamp); | ||
| 628 | $self->{dbh}->do("DELETE FROM full_hashes WHERE num = ? AND hash = ? AND list = ?", { }, $hash->{chunknum}, $hash->{hash}, $hash->{list}); | ||
| 629 | $self->{dbh}->do("INSERT INTO full_hashes (num, hash, list, timestamp) VALUES (?, ?, ?, ?)", { }, $hash->{chunknum}, $hash->{hash}, $hash->{list}, $timestamp); | ||
| 630 | } | ||
| 631 | } | ||
| 632 | |||
| 633 | sub delete_full_hashes { | ||
| 634 | my ($self, %args) = @_; | ||
| 635 | my $chunknums = $args{chunknums} || []; | ||
| 636 | my $list = $args{list} || croak "Missing list name\n"; | ||
| 637 | |||
| 638 | my $sth = $self->{dbh}->prepare("DELETE FROM full_hashes WHERE num = ? AND list = ?"); | ||
| 639 | |||
| 640 | foreach my $num (@$chunknums) { | ||
| 641 | $sth->execute($num, $list); | ||
| 642 | } | ||
| 643 | } | ||
| 644 | |||
| 645 | sub full_hash_error { | ||
| 646 | my ($self, %args) = @_; | ||
| 647 | my $timestamp = $args{timestamp} || time(); | ||
| 648 | my $prefix = $args{prefix} || ''; | ||
| 649 | |||
| 650 | my $rows = $self->{dbh}->selectall_arrayref("SELECT id, errors FROM full_hashes_errors WHERE prefix = ? LIMIT 1", { Slice => {} }, $prefix); | ||
| 651 | |||
| 652 | if (scalar @$rows == 0) { | ||
| 653 | $self->{dbh}->do("INSERT INTO full_hashes_errors (prefix, errors, timestamp) VALUES (?, 1, ?)", { }, $prefix, $timestamp); | ||
| 654 | } | ||
| 655 | else { | ||
| 656 | my $errors = $rows->[0]->{errors} + 1; | ||
| 657 | $self->{dbh}->do("UPDATE full_hashes_errors SET errors = ?, timestamp = ? WHERE id = ?", $errors, $timestamp, $rows->[0]->{id}); | ||
| 658 | } | ||
| 659 | } | ||
| 660 | |||
| 661 | sub full_hash_ok { | ||
| 662 | my ($self, %args) = @_; | ||
| 663 | my $timestamp = $args{timestamp} || time(); | ||
| 664 | my $prefix = $args{prefix} || ''; | ||
| 665 | |||
| 666 | my $rows = $self->{dbh}->selectall_arrayref("SELECT id, errors FROM full_hashes_errors WHERE prefix = ? AND errors > 0 LIMIT 1", { Slice => {} }, $prefix); | ||
| 667 | |||
| 668 | if (scalar @$rows > 0) { | ||
| 669 | $self->{dbh}->do("UPDATE full_hashes_errors SET errors = 0, timestamp = ? WHERE id = ?", $timestamp, $rows->[0]->{id}); | ||
| 670 | $self->{dbh}->do("DELETE FROM full_hashes_errors WHERE id = ?", $rows->[0]->{id}); | ||
| 671 | } | ||
| 672 | } | ||
| 673 | |||
| 674 | sub get_full_hash_error { | ||
| 675 | my ($self, %args) = @_; | ||
| 676 | my $prefix = $args{prefix} || ''; | ||
| 677 | |||
| 678 | my $rows = $self->{dbh}->selectall_arrayref("SELECT timestamp, errors FROM full_hashes_errors WHERE prefix = ? LIMIT 1", { Slice => {} }, $prefix); | ||
| 679 | |||
| 680 | if (scalar @$rows == 0) { | ||
| 681 | return undef; | ||
| 682 | } | ||
| 683 | else { | ||
| 684 | return $rows->[0]; | ||
| 685 | } | ||
| 686 | } | ||
| 687 | |||
| 688 | sub get_mac_keys { | ||
| 689 | my ($self, %args) = @_; | ||
| 690 | |||
| 691 | |||
| 692 | my $rows = $self->{dbh}->selectall_arrayref("SELECT client_key, wrapped_key FROM mac_keys LIMIT 1", { Slice => {} }); | ||
| 693 | |||
| 694 | if (scalar @$rows == 0) { | ||
| 695 | return { client_key => '', wrapped_key => '' }; | ||
| 696 | } | ||
| 697 | else { | ||
| 698 | return $rows->[0]; | ||
| 699 | } | ||
| 700 | } | ||
| 701 | |||
| 702 | sub add_mac_keys { | ||
| 703 | my ($self, %args) = @_; | ||
| 704 | my $client_key = $args{client_key} || ''; | ||
| 705 | my $wrapped_key = $args{wrapped_key} || ''; | ||
| 706 | |||
| 707 | |||
| 708 | $self->delete_mac_keys(); | ||
| 709 | |||
| 710 | $self->{dbh}->do("INSERT INTO mac_keys (client_key, wrapped_key) VALUES (?, ?)", { }, $client_key, $wrapped_key); | ||
| 711 | |||
| 712 | } | ||
| 713 | |||
| 714 | sub delete_mac_keys { | ||
| 715 | my ($self, %args) = @_; | ||
| 716 | |||
| 717 | $self->{dbh}->do("DELETE FROM mac_keys WHERE 1"); | ||
| 718 | } | ||
| 719 | |||
| 720 | sub reset { | ||
| 721 | my ($self, %args) = @_; | ||
| 722 | my $list = $args{'list'} || ''; | ||
| 723 | |||
| 724 | my $sth = $self->{dbh}->prepare('DELETE FROM s_chunks WHERE list = ?'); | ||
| 725 | $sth->execute( $list ); | ||
| 726 | |||
| 727 | $sth = $self->{dbh}->prepare('DELETE FROM a_chunks WHERE list = ?'); | ||
| 728 | $sth->execute( $list ); | ||
| 729 | |||
| 730 | $sth = $self->{dbh}->prepare('DELETE FROM full_hashes WHERE list = ?'); | ||
| 731 | $sth->execute( $list ); | ||
| 732 | |||
| 733 | $sth = $self->{dbh}->prepare('DELETE FROM full_hashes_errors'); | ||
| 734 | $sth->execute(); | ||
| 735 | |||
| 736 | $sth = $self->{dbh}->prepare('DELETE FROM updates WHERE list = ?'); | ||
| 737 | $sth->execute( $list ); | ||
| 738 | } | ||
| 739 | |||
| 740 | sub create_range { | ||
| 741 | my ($self, %args) = @_; | ||
| 742 | my $numbers = $args{numbers} || []; # should already be ordered | ||
| 743 | |||
| 744 | return '' if (scalar @$numbers == 0); | ||
| 745 | |||
| 746 | my $range = $$numbers[0]; | ||
| 747 | my $new_range = 0; | ||
| 748 | for(my $i = 1; $i < scalar @$numbers; $i++) { | ||
| 749 | # next if ($$numbers[$i] == $$numbers[$i-1]); # should not happen | ||
| 750 | |||
| 751 | if ($$numbers[$i] != $$numbers[$i-1] + 1) { | ||
| 752 | $range .= $$numbers[$i-1] if ($i > 1 && $new_range == 1); | ||
| 753 | $range .= ',' . $$numbers[$i]; | ||
| 754 | |||
| 755 | $new_range = 0 | ||
| 756 | } | ||
| 757 | elsif ($new_range == 0) { | ||
| 758 | $range .= "-"; | ||
| 759 | $new_range = 1; | ||
| 760 | } | ||
| 761 | } | ||
| 762 | $range .= $$numbers[scalar @$numbers - 1] if ($new_range == 1); | ||
| 763 | |||
| 764 | return $range; | ||
| 765 | } | ||
| 766 | |||
| 767 | =head1 CHANGELOG | ||
| 768 | |||
| 769 | =over 4 | ||
| 770 | |||
| 771 | =item 0.7.1 | ||
| 772 | |||
| 773 | Fix for empty sub chunks. | ||
| 774 | |||
| 775 | =item 0.7 | ||
| 776 | |||
| 777 | New C<export()> function. | ||
| 778 | |||
| 779 | Keep empty sub chunks. | ||
| 780 | |||
| 781 | Fix index for sub chunks. | ||
| 782 | |||
| 783 | =item 0.6 | ||
| 784 | |||
| 785 | Add option keep_all to keep expired full hashes. Useful for debugging. | ||
| 786 | |||
| 787 | =item 0.5 | ||
| 788 | |||
| 789 | Return the hostkey in get_add_chunks. | ||
| 790 | |||
| 791 | =item 0.4 | ||
| 792 | |||
| 793 | Fix duplicate insert of add chunks and sub chunks. | ||
| 794 | |||
| 795 | =item 0.3 | ||
| 796 | |||
| 797 | Add reset function to reset all tables for a given list | ||
| 798 | |||
| 799 | =item 0.2 | ||
| 800 | |||
| 801 | Replace "INSERT OR REPLACE" statements by DELETE + INSERT to work with all databases | ||
| 802 | |||
| 803 | =back | ||
| 804 | |||
| 805 | =head1 SEE ALSO | ||
| 806 | |||
| 807 | See L<Net::Google::SafeBrowsing2> for handling Google Safe Browsing v2. | ||
| 808 | |||
| 809 | See L<Net::Google::SafeBrowsing2::Storage> for the list of public functions. | ||
| 810 | |||
| 811 | See L<Net::Google::SafeBrowsing2::Sqlite> for a back-end using Sqlite. | ||
| 812 | |||
| 813 | Google Safe Browsing v2 API: L<http://code.google.com/apis/safebrowsing/developers_guide_v2.html> | ||
| 814 | |||
| 815 | |||
| 816 | =head1 AUTHOR | ||
| 817 | |||
| 818 | Julien Sobrier, E<lt>jsobrier@zscaler.comE<gt> or E<lt>julien@sobrier.netE<gt> | ||
| 819 | |||
| 820 | =head1 COPYRIGHT AND LICENSE | ||
| 821 | |||
| 822 | Copyright (C) 2015 by Julien Sobrier | ||
| 823 | |||
| 824 | This library is free software; you can redistribute it and/or modify | ||
| 825 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 826 | at your option, any later version of Perl 5 you may have available. | ||
| 827 | |||
| 828 | |||
| 829 | =cut | ||
| 830 | |||
| 831 | 1; |