| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Cwd; |
| 7 |
use Data::Dumper; |
| 8 |
use DBIx::Simple; |
| 9 |
use IO::File; |
| 10 |
use File::stat; |
| 11 |
use Time::TAI64 qw/ tai2unix /; |
| 12 |
|
| 13 |
$Data::Dumper::Sortkeys = 1; |
| 14 |
|
| 15 |
my $logdir = get_log_dir(); |
| 16 |
my @logfiles = get_logfiles($logdir); |
| 17 |
|
| 18 |
my (%plugins, %os, %message_ids); |
| 19 |
my $has_cleanup; |
| 20 |
my $db = get_db(); |
| 21 |
check_plugins_table(); |
| 22 |
|
| 23 |
foreach my $file (@logfiles) { |
| 24 |
my ($fid, $offset) = check_logfile($file); |
| 25 |
$fid or next; |
| 26 |
parse_logfile($file, $fid, $offset); |
| 27 |
} |
| 28 |
|
| 29 |
exit; |
| 30 |
|
| 31 |
sub trim_message { |
| 32 |
my $mess = shift; |
| 33 |
|
| 34 |
return '' if $mess eq 'skip, naughty'; |
| 35 |
return '' if $mess eq 'skip, relay client'; |
| 36 |
return '' if $mess eq 'skip, no match'; |
| 37 |
return '' if $mess eq 'skip: unsigned'; |
| 38 |
return '' if $mess eq 'skip, not a null sender'; |
| 39 |
return '' if $mess eq 'pass'; |
| 40 |
return '' if $mess eq 'pass, no record'; |
| 41 |
return '' if $mess eq 'pass, Deliverable through vpopmail'; |
| 42 |
return '' if $mess eq 'pass, clean'; |
| 43 |
return '' if $mess =~ /^fail. NAUGHTY/; |
| 44 |
return '' if $mess =~ /^PTR:\s/; |
| 45 |
return '' if $mess eq 'TLS setup returning'; |
| 46 |
|
| 47 |
return $mess; |
| 48 |
} |
| 49 |
|
| 50 |
sub get_os_id { |
| 51 |
my $p0f_string = shift or return; |
| 52 |
|
| 53 |
$p0f_string =~ s/\s+$//; |
| 54 |
$p0f_string =~ s/^\s+//; |
| 55 |
return if !$p0f_string; |
| 56 |
return if $p0f_string =~ /no match/; |
| 57 |
return if $p0f_string =~ /^skip/; |
| 58 |
return if $p0f_string =~ /^\d/; |
| 59 |
return if $p0f_string =~ /^\(/; |
| 60 |
return if $p0f_string !~ /\w/; |
| 61 |
return if $p0f_string =~ /no longer in the cache/; |
| 62 |
|
| 63 |
if (!scalar keys %os) { |
| 64 |
my $ref = exec_query('SELECT * FROM os'); |
| 65 |
foreach my $o (@$ref) { |
| 66 |
$os{$o->{name}} = $o->{id}; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
if (!defined $os{$p0f_string}) { |
| 71 |
warn "missing OS for $p0f_string\n"; |
| 72 |
} |
| 73 |
|
| 74 |
return $os{$p0f_string}; |
| 75 |
} |
| 76 |
|
| 77 |
sub get_plugin_id { |
| 78 |
my $plugin = shift; |
| 79 |
|
| 80 |
if (!scalar keys %plugins) { |
| 81 |
my $ref = exec_query('SELECT * FROM plugin'); |
| 82 |
foreach my $p (@$ref) { |
| 83 |
$plugins{$p->{name}} = $p->{id}; |
| 84 |
$plugins{$p->{id}} = $p->{name}; |
| 85 |
} |
| 86 |
$ref = exec_query('SELECT * FROM plugin_aliases'); |
| 87 |
foreach my $pa (@$ref) { |
| 88 |
$plugins{$pa->{name}} = $pa->{plugin_id}; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
if (!defined $plugins{$plugin}) { |
| 93 |
|
| 94 |
#warn Dumper(\%plugins); |
| 95 |
die "missing DB plugin $plugin\n"; |
| 96 |
} |
| 97 |
|
| 98 |
return $plugins{$plugin}; |
| 99 |
} |
| 100 |
|
| 101 |
sub get_msg_id { |
| 102 |
my ($fid, $pid) = @_; |
| 103 |
|
| 104 |
return $message_ids{"$fid-$pid"} if $message_ids{"$fid-$pid"}; |
| 105 |
|
| 106 |
#print "searching for message $pid..."; |
| 107 |
my $msgs = exec_query('SELECT * FROM message WHERE file_id=? AND qp_pid=?', |
| 108 |
[$fid, $pid]); |
| 109 |
|
| 110 |
#print scalar @$msgs ? "y\n" : "n\n"; |
| 111 |
if ($msgs->[0]{id}) { |
| 112 |
$message_ids{"$fid-$pid"} = $msgs->[0]{id}; |
| 113 |
} |
| 114 |
return $msgs->[0]{id}; |
| 115 |
} |
| 116 |
|
| 117 |
sub create_message { |
| 118 |
my ($fid, $ts, $pid, $message) = @_; |
| 119 |
|
| 120 |
my ($host, $ip) = split /\s/, $message; |
| 121 |
$ip = substr $ip, 1, -1; # remove brackets |
| 122 |
|
| 123 |
my $id = exec_query( |
| 124 |
"INSERT INTO message SET file_id=?, connect_start=FROM_UNIXTIME(?), qp_pid=?, ip=INET_ATON(?)", |
| 125 |
[$fid, $ts, $pid, $ip] |
| 126 |
); |
| 127 |
|
| 128 |
if ($host && $host ne 'Unknown') { |
| 129 |
exec_query("UPDATE message SET hostname=? WHERE id=?", [$host, $id]); |
| 130 |
} |
| 131 |
|
| 132 |
#warn "host updated: $host\n"; |
| 133 |
} |
| 134 |
|
| 135 |
sub insert_plugin { |
| 136 |
my ($msg_id, $plugin, $message) = @_; |
| 137 |
|
| 138 |
my $plugin_id = get_plugin_id($plugin); |
| 139 |
|
| 140 |
if ($plugin eq 'ident::geoip') { |
| 141 |
my ($gip, $distance) = $message =~ /(.*?),\s+([\d]+)\skm/; |
| 142 |
if ($distance) { |
| 143 |
exec_query('UPDATE message SET distance=? WHERE id=?', |
| 144 |
[$distance, $msg_id]); |
| 145 |
$message = $gip; |
| 146 |
} |
| 147 |
} |
| 148 |
elsif ($plugin =~ /^ident::p0f/) { |
| 149 |
my $os_id = get_os_id($message); |
| 150 |
if ($os_id) { |
| 151 |
exec_query('UPDATE message SET os_id=? WHERE id=?', |
| 152 |
[$os_id, $msg_id]); |
| 153 |
$message = 'pass'; |
| 154 |
} |
| 155 |
} |
| 156 |
elsif ($plugin eq 'connection_time') { |
| 157 |
my ($seconds) = $message =~ /\s*([\d\.]+)\s/; |
| 158 |
if ($seconds) { |
| 159 |
exec_query('UPDATE message SET time=? WHERE id=?', |
| 160 |
[$seconds, $msg_id]); |
| 161 |
$message = 'pass'; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
my $result = get_score($message); |
| 166 |
if ($result) { |
| 167 |
$message = trim_message($message); |
| 168 |
} |
| 169 |
|
| 170 |
exec_query( |
| 171 |
'INSERT INTO message_plugin SET msg_id=?, plugin_id=?, result=?, string=?', |
| 172 |
[$msg_id, $plugin_id, $result, $message] |
| 173 |
); |
| 174 |
} |
| 175 |
|
| 176 |
sub parse_logfile { |
| 177 |
my $file = shift; |
| 178 |
my $fid = shift; |
| 179 |
my $offset = shift || 0; |
| 180 |
my $path = "$logdir/$file"; |
| 181 |
|
| 182 |
print "parsing file $file (id: $fid) from offset $offset\n"; |
| 183 |
open my $F, '<', $path or die "could not open $path: $!"; |
| 184 |
seek($F, $offset, 0) if $offset; |
| 185 |
|
| 186 |
while (defined(my $line = <$F>)) { |
| 187 |
chomp $line; |
| 188 |
next if !$line; |
| 189 |
my ($type, $pid, $hook, $plugin, $message) = parse_line($line); |
| 190 |
|
| 191 |
next if !$type; |
| 192 |
next if $type eq 'info'; |
| 193 |
next if $type eq 'unknown'; |
| 194 |
next if $type eq 'response'; |
| 195 |
next if $type eq 'init'; # doesn't occur in all deployment models |
| 196 |
next if $type eq 'cleanup'; |
| 197 |
next if $type eq 'error'; |
| 198 |
|
| 199 |
my $ts = tai2unix((split /\s/, $line)[0]); # print "ts: $ts\n"; |
| 200 |
|
| 201 |
my $msg_id = get_msg_id($fid, $pid) or do { |
| 202 |
create_message($fid, $ts, $pid, $message) if $type eq 'connect'; |
| 203 |
next; |
| 204 |
}; |
| 205 |
|
| 206 |
#warn "type: $type\n"; |
| 207 |
if ($type eq 'plugin') { |
| 208 |
next if $plugin eq 'naughty'; # housekeeping only |
| 209 |
next if $plugin eq 'karma' && 'karma adjust' eq substr($message,0,12); |
| 210 |
insert_plugin($msg_id, $plugin, $message); |
| 211 |
} |
| 212 |
elsif ($type eq 'queue') { |
| 213 |
exec_query('UPDATE message SET result=? WHERE id=?', [3, $msg_id]); |
| 214 |
} |
| 215 |
elsif ($type eq 'reject') { |
| 216 |
exec_query('UPDATE message SET result=? WHERE id=?', [-3, $msg_id]); |
| 217 |
} |
| 218 |
elsif ($type eq 'close') { |
| 219 |
if ($message eq 'Connection Timed Out') { |
| 220 |
exec_query('UPDATE message SET result=? WHERE id=?', |
| 221 |
[-1, $msg_id]); |
| 222 |
} |
| 223 |
} |
| 224 |
elsif ($type eq 'connect') { } |
| 225 |
elsif ($type eq 'dispatch') { |
| 226 |
if (substr($message, 0, 21) eq 'dispatching MAIL FROM') { |
| 227 |
my ($from) = $message =~ /<(.*?)>/; |
| 228 |
exec_query('UPDATE message SET mail_from=? WHERE id=?', |
| 229 |
[$from, $msg_id]); |
| 230 |
} |
| 231 |
elsif (substr($message, 0, 19) eq 'dispatching RCPT TO') { |
| 232 |
my ($to) = $message =~ /<(.*?)>/; |
| 233 |
exec_query( |
| 234 |
'UPDATE message SET rcpt_to=? WHERE id=? AND rcpt_to IS NULL', |
| 235 |
[$to, $msg_id] |
| 236 |
); |
| 237 |
} |
| 238 |
elsif ($message =~ m/dispatching (EHLO|HELO) (.*)/) { |
| 239 |
exec_query('UPDATE message SET helo=? WHERE id=?', |
| 240 |
[$2, $msg_id]); |
| 241 |
} |
| 242 |
elsif ($message eq 'dispatching DATA') { } |
| 243 |
elsif ($message eq 'dispatching QUIT') { } |
| 244 |
elsif ($message eq 'dispatching STARTTLS') { } |
| 245 |
elsif ($message eq 'dispatching RSET') { } |
| 246 |
else { |
| 247 |
# anything here is likely an unrecognized command |
| 248 |
#print "$message\n"; |
| 249 |
} |
| 250 |
} |
| 251 |
else { |
| 252 |
print "$type $pid $hook $plugin $message\n"; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
close $F; |
| 257 |
} |
| 258 |
|
| 259 |
sub check_logfile { |
| 260 |
my $file = shift; |
| 261 |
my $path = "$logdir/$file"; |
| 262 |
|
| 263 |
die "missing file $logdir/$file" if !-f "$logdir/$file"; |
| 264 |
|
| 265 |
my $inode = stat($path)->ino or die "unable to get inode for $path\n"; |
| 266 |
my $size = stat($path)->size or die "unable to get size for $path\n"; |
| 267 |
my $exists; |
| 268 |
|
| 269 |
#warn "check if file $file is in the DB as 'current'\n"; |
| 270 |
if ($file =~ /^\@/) { |
| 271 |
$exists = exec_query('SELECT * FROM log WHERE inode=? AND name=?', |
| 272 |
[$inode, 'current']); |
| 273 |
if (@$exists) { |
| 274 |
print "Updating current -> $file\n"; |
| 275 |
exec_query('UPDATE log SET name=? WHERE inode=? AND name=?', |
| 276 |
[$file, $inode, 'current']); |
| 277 |
return ($exists->[0]{id}, $exists->[0]{size}); # continue parsing |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
if ($file eq 'current') { |
| 282 |
$exists = exec_query('SELECT * FROM log WHERE inode=? AND name=?', |
| 283 |
[$inode, $file]); |
| 284 |
if (@$exists) { |
| 285 |
exec_query('UPDATE log SET size=? WHERE inode=? AND name=?', |
| 286 |
[$size, $inode, 'current']); |
| 287 |
return ($exists->[0]{id}, $exists->[0]{size}); # continue parsing |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
$exists = |
| 292 |
exec_query('SELECT * FROM log WHERE name=? AND size=?', [$file, $size]); |
| 293 |
return if @$exists; # log file hasn't changed, ignore it |
| 294 |
#print Dumper($exists); |
| 295 |
|
| 296 |
# file is a new one we haven't seen, add to DB and parse |
| 297 |
my $id = exec_query( |
| 298 |
'INSERT INTO log SET inode=?, size=?, name=?, created=FROM_UNIXTIME(?)', |
| 299 |
[$inode, $size, $file, stat($path)->ctime] |
| 300 |
); |
| 301 |
print "new file id: $id\n"; |
| 302 |
return ($id); |
| 303 |
} |
| 304 |
|
| 305 |
sub get_log_dir { |
| 306 |
|
| 307 |
if (-d "log/main") { |
| 308 |
my $wd = Cwd::cwd(); |
| 309 |
return "$wd/log/main"; |
| 310 |
} |
| 311 |
|
| 312 |
foreach my $user (qw/ qpsmtpd smtpd /) { |
| 313 |
|
| 314 |
my ($homedir) = (getpwnam($user))[7] or next; |
| 315 |
|
| 316 |
if (-d "$homedir/log") { |
| 317 |
return "$homedir/log/main"; |
| 318 |
} |
| 319 |
if (-d "$homedir/smtpd/log") { |
| 320 |
return "$homedir/smtpd/log/main"; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
} |
| 325 |
|
| 326 |
sub get_logfiles { |
| 327 |
my $dir = shift; |
| 328 |
|
| 329 |
opendir my $D, $dir or die "unable to open log dir $dir\n"; |
| 330 |
|
| 331 |
my @files; |
| 332 |
while (defined(my $f = readdir($D))) { |
| 333 |
next if !-f "$dir/$f"; # ignore anything that's not a file |
| 334 |
if ($f =~ /^\@.*s$/) { |
| 335 |
push @files, $f; |
| 336 |
} |
| 337 |
} |
| 338 |
push @files, "current"; # always have this one last |
| 339 |
|
| 340 |
closedir $D; |
| 341 |
return @files; |
| 342 |
} |
| 343 |
|
| 344 |
sub parse_line { |
| 345 |
my $line = shift; |
| 346 |
my ($tai, $pid, $message) = split /\s+/, $line, 3; |
| 347 |
return if !$message; # garbage in the log file |
| 348 |
|
| 349 |
# lines seen many times per connection |
| 350 |
return parse_line_plugin($line) if substr($message, 0, 1) eq '('; |
| 351 |
return ('dispatch', $pid, undef, undef, $message) |
| 352 |
if substr($message, 0, 12) eq 'dispatching '; |
| 353 |
return ('queue', $pid, undef, undef, $message) |
| 354 |
if substr($message, 0, 11) eq '250 Queued!'; |
| 355 |
return ('response', $pid, undef, undef, $message) |
| 356 |
if $message =~ /^[2|3]\d\d/; |
| 357 |
|
| 358 |
# lines seen about once per connection |
| 359 |
return ('init', $pid, undef, undef, $message) |
| 360 |
if substr($message, 0, 19) eq 'Accepted connection'; |
| 361 |
return ('connect', $pid, undef, undef, substr($message, 16)) |
| 362 |
if substr($message, 0, 15) eq 'Connection from'; |
| 363 |
return ('connect', $pid, undef, undef, substr($message, 16)) |
| 364 |
if substr($message, 0, 8) eq 'connect '; |
| 365 |
return ('close', $pid, undef, undef, $message) |
| 366 |
if substr($message, 0, 6) eq 'close '; |
| 367 |
return ('close', $pid, undef, undef, $message) |
| 368 |
if $message eq 'Connection Timed Out'; |
| 369 |
return ('close', $pid, undef, undef, $message) |
| 370 |
if substr($message, 0, 20) eq 'click, disconnecting'; |
| 371 |
return parse_line_cleanup($line) |
| 372 |
if substr($message, 0, 11) eq 'cleaning up'; |
| 373 |
|
| 374 |
# lines seen less than once per connection |
| 375 |
return ('info', $pid, undef, undef, $message) |
| 376 |
if $message eq 'spooling message to disk'; |
| 377 |
return ('reject', $pid, undef, undef, $message) if $message =~ /^[4|5]\d\d/; |
| 378 |
return ('reject', $pid, undef, undef, $message) |
| 379 |
if substr($message, 0, 14) eq 'deny mail from'; |
| 380 |
return ('reject', $pid, undef, undef, $message) |
| 381 |
if substr($message, 0, 18) eq 'denysoft mail from'; |
| 382 |
return ('info', $pid, undef, undef, $message) |
| 383 |
if substr($message, 0, 15) eq 'Lost connection'; |
| 384 |
return ('info', $pid, undef, undef, $message) |
| 385 |
if $message eq 'auth success cleared naughty'; |
| 386 |
return ('info', $pid, undef, undef, $message) |
| 387 |
if substr($message, 0, 15) eq 'Running as user'; |
| 388 |
return ('info', $pid, undef, undef, $message) |
| 389 |
if substr($message, 0, 16) eq 'Loaded Qpsmtpd::'; |
| 390 |
return ('info', $pid, undef, undef, $message) |
| 391 |
if substr($message, 0, 24) eq 'Permissions on spool_dir'; |
| 392 |
return ('info', $pid, undef, undef, $message) |
| 393 |
if substr($message, 0, 13) eq 'Listening on '; |
| 394 |
return ('info', $pid, undef, undef, $message) |
| 395 |
if substr($message, 0, 18) eq 'size_threshold set'; |
| 396 |
return ('info', $pid, undef, undef, $message) |
| 397 |
if substr($message, 0, 12) eq 'tls: ciphers'; |
| 398 |
return ('error', $pid, undef, undef, $message) |
| 399 |
if substr($message, 0, 22) eq 'of uninitialized value'; |
| 400 |
return ('error', $pid, undef, undef, $message) |
| 401 |
if substr($message, 0, 8) eq 'symbol "'; |
| 402 |
return ('error', $pid, undef, undef, $message) |
| 403 |
if substr($message, 0, 9) eq 'error at '; |
| 404 |
return ('error', $pid, undef, undef, $message) |
| 405 |
if substr($message, 0, 15) eq 'Could not print'; |
| 406 |
|
| 407 |
print "UNKNOWN LINE: $line\n"; |
| 408 |
return ('unknown', $pid, undef, undef, $message); |
| 409 |
} |
| 410 |
|
| 411 |
sub parse_line_plugin { |
| 412 |
my ($line) = @_; |
| 413 |
|
| 414 |
# @tai 13486 (connect) ident::p0f: Windows (XP/2000 (RFC1323+, w, tstamp-)) |
| 415 |
# @tai 13681 (connect) dnsbl: fail, NAUGHTY |
| 416 |
# @tai 15787 (connect) karma: pass, no penalty (0 naughty, 3 nice, 3 connects) |
| 417 |
# @tai 27500 (queue) queue::qmail_2dqueue: (for 27481) Queuing to /var/qmail/bin/qmail-queue |
| 418 |
my ($tai, $pid, $hook, $plugin, $message) = split /\s/, $line, 5; |
| 419 |
$plugin =~ s/:$//; |
| 420 |
|
| 421 |
return parse_line_plugin_p0f($line) if $plugin =~ /^ident::p0f/; |
| 422 |
return parse_line_plugin_dspam($line) if $plugin =~ /^dspam/; |
| 423 |
return parse_line_plugin_spamassassin($line) if $plugin =~ /^spamassassin/; |
| 424 |
|
| 425 |
if ($plugin eq 'sender_permitted_from') { |
| 426 |
$message = 'pass' if $message =~ /^pass/; |
| 427 |
$message = 'fail' if $message =~ /^fail/; |
| 428 |
$message = 'skip' if $message =~ /^none/; |
| 429 |
} |
| 430 |
elsif ($plugin eq 'queue::qmail_2dqueue') { |
| 431 |
($pid) = $message =~ /\(for ([\d]+)\)/; |
| 432 |
$message = 'pass' if $message =~ /Queuing/; |
| 433 |
} |
| 434 |
elsif ($plugin =~ /(?:early|karma|helo|rcpt_ok)/) { |
| 435 |
$message = 'pass' if $message =~ /^pass/; |
| 436 |
} |
| 437 |
elsif ($plugin =~ /resolvable_fromhost/) { |
| 438 |
$message = 'pass' if $message =~ /^pass/; |
| 439 |
} |
| 440 |
|
| 441 |
return ('plugin', $pid, $hook, $plugin, $message); |
| 442 |
} |
| 443 |
|
| 444 |
sub parse_line_plugin_dspam { |
| 445 |
my $line = shift; |
| 446 |
|
| 447 |
my ($tai, $pid, $hook, $plugin, $message) = split /\s/, $line, 5; |
| 448 |
$plugin =~ s/:$//; |
| 449 |
|
| 450 |
if ($message =~ /Innocent, (\d\.\d\d c)/) { |
| 451 |
$message = "pass, $1"; |
| 452 |
} |
| 453 |
if ($message =~ /Spam, (\d\.\d\d c)/) { |
| 454 |
$message = "fail, $1"; |
| 455 |
} |
| 456 |
|
| 457 |
return ('plugin', $pid, $hook, $plugin, $message); |
| 458 |
} |
| 459 |
|
| 460 |
sub parse_line_plugin_spamassassin { |
| 461 |
my $line = shift; |
| 462 |
|
| 463 |
my ($tai, $pid, $hook, $plugin, $message) = split /\s/, $line, 5; |
| 464 |
$plugin =~ s/:$//; |
| 465 |
|
| 466 |
if ($message =~ /pass, Ham, ([\d\-\.]+)\s/) { |
| 467 |
$message = "pass, $1"; |
| 468 |
} |
| 469 |
if ($message =~ /^fail, Spam,\s([\d\.]+)\s< 100/) { |
| 470 |
$message = "fail, $1"; |
| 471 |
} |
| 472 |
|
| 473 |
return ('plugin', $pid, $hook, $plugin, $message); |
| 474 |
} |
| 475 |
|
| 476 |
sub parse_line_plugin_p0f { |
| 477 |
my $line = shift; |
| 478 |
|
| 479 |
my ($tai, $pid, $hook, $plugin, $message) = split /\s/, $line, 5; |
| 480 |
$plugin =~ s/:$//; |
| 481 |
|
| 482 |
if (substr($message, -5, 5) eq 'hops)') { |
| 483 |
($message) = split(/\s\(/, $message); |
| 484 |
} |
| 485 |
|
| 486 |
$message = 'iOS' if $message =~ /^iOS/; |
| 487 |
$message = 'Solaris' if $message =~ /^Solaris/; |
| 488 |
$message = 'Mac OS X' if $message =~ /^Mac OS X/; |
| 489 |
$message = 'FreeBSD' if $message =~ /^FreeBSD/; |
| 490 |
$message = 'Linux' if $message =~ /^Linux/; |
| 491 |
$message = 'OpenBSD' if $message =~ /^OpenBSD/; |
| 492 |
$message = 'Windows NT' if $message =~ /^Windows \(?NT/; |
| 493 |
$message = 'Windows 95' if $message =~ /^Windows \(?95/; |
| 494 |
$message = 'Windows 98' if $message =~ /^Windows \(?98/; |
| 495 |
$message = 'Windows XP' if $message =~ /^Windows \(?XP/; |
| 496 |
$message = 'Windows 2000' if $message =~ /^Windows \(?2000/; |
| 497 |
$message = 'Windows 2003' if $message =~ /^Windows \(?2003/; |
| 498 |
$message = 'Windows 7 or 8' if $message =~ /^Windows 7/; |
| 499 |
$message = 'Windows 7 or 8' if $message =~ /^Windows 8/; |
| 500 |
$message = 'Google' if $message =~ /^Google/; |
| 501 |
$message = 'HP-UX' if $message =~ /^HP\-UX/; |
| 502 |
$message = 'NetCache' if $message =~ /^NetCache/i; |
| 503 |
$message = 'Cisco' if $message =~ /^Cisco/i; |
| 504 |
$message = 'Netware' if $message =~ /Netware/i; |
| 505 |
|
| 506 |
return ('plugin', $pid, $hook, $plugin, $message); |
| 507 |
} |
| 508 |
|
| 509 |
sub parse_line_cleanup { |
| 510 |
my ($line) = @_; |
| 511 |
|
| 512 |
# @tai 85931 cleaning up after 3210 |
| 513 |
my $pid = (split /\s+/, $line)[-1]; |
| 514 |
$has_cleanup++; |
| 515 |
return ('cleanup', $pid, undef, undef, $line); |
| 516 |
} |
| 517 |
|
| 518 |
sub get_score { |
| 519 |
my $mess = shift; |
| 520 |
return 3 if $mess eq 'TLS setup returning'; |
| 521 |
return 3 if $mess =~ /^pass/; |
| 522 |
return -3 if $mess =~ /^fail/; |
| 523 |
return -2 if $mess =~ /^negative/; |
| 524 |
return 2 if $mess =~ /^positive/; |
| 525 |
return 1 if $mess =~ /^skip/; |
| 526 |
return 0; |
| 527 |
} |
| 528 |
|
| 529 |
sub get_db { |
| 530 |
|
| 531 |
my %dbv = get_config('log2sql'); |
| 532 |
|
| 533 |
$dbv{dsn} ||= 'DBI:mysql:database=qpsmtpd;host=db;port=3306'; |
| 534 |
$dbv{user} ||= 'qplog'; |
| 535 |
$dbv{pass} ||= 't0ps3cret'; |
| 536 |
|
| 537 |
print Dumper(\%dbv); |
| 538 |
my $db = DBIx::Simple->connect($dbv{dsn}, $dbv{user}, $dbv{pass}) |
| 539 |
or die DBIx::Simple->error; |
| 540 |
|
| 541 |
return $db; |
| 542 |
} |
| 543 |
|
| 544 |
sub get_config { |
| 545 |
my $file = shift or die "missing file name\n"; |
| 546 |
my %values; |
| 547 |
foreach my $line ( get_config_contents( $file ) ) { |
| 548 |
next if $line =~ /^#/; |
| 549 |
chomp $line; |
| 550 |
my ($key,$val) = split /\s*=\s*/, $line, 2; |
| 551 |
$values{$key} = $val; |
| 552 |
}; |
| 553 |
return %values; |
| 554 |
}; |
| 555 |
|
| 556 |
sub get_config_contents { |
| 557 |
my $name = shift; |
| 558 |
|
| 559 |
my @config_dirs = qw[ config ../config log plugins ]; |
| 560 |
foreach my $dir ( @config_dirs ) { |
| 561 |
next if ! -f "$dir/$name"; |
| 562 |
|
| 563 |
my $fh = IO::File->new(); |
| 564 |
if ( ! $fh->open( "$dir/$name", '<' ) ) { |
| 565 |
warn "unable to open config file $dir/$name\n"; |
| 566 |
next; |
| 567 |
}; |
| 568 |
my @contents = <$fh>; |
| 569 |
return @contents; |
| 570 |
}; |
| 571 |
}; |
| 572 |
|
| 573 |
sub check_plugins_table { |
| 574 |
my $rows = exec_query( 'SELECT COUNT(*) FROM plugin'); |
| 575 |
return if scalar @$rows != 0; |
| 576 |
my @lines = get_config_contents('registry.txt'); |
| 577 |
foreach my $line ( @lines ) { |
| 578 |
next if $line =~ /^\s*#/; # ignore comments |
| 579 |
chomp $line; |
| 580 |
next if ! $line; |
| 581 |
my ($id, $name, $abb3, $abb5, $aliases) = split /\s+/, $line, 5; |
| 582 |
my $q = "REPLACE INTO plugin (id,name,abb3,abb5) VALUES (??)"; |
| 583 |
print "query: $q, $id, $name, $abb3, $abb5\n"; |
| 584 |
exec_query($q, [$id, $name, $abb3, $abb5 ]); |
| 585 |
next if ! $aliases; |
| 586 |
foreach my $alias ( split /\s*,\s*/, $aliases ) { |
| 587 |
next if ! $alias; |
| 588 |
my $aq = "REPLACE INTO plugin_aliases (plugin_id,name) VALUES (??)"; |
| 589 |
print "aqury: $aq, $id, $alias\n"; |
| 590 |
exec_query($aq, [$id, $alias]); |
| 591 |
}; |
| 592 |
}; |
| 593 |
}; |
| 594 |
|
| 595 |
sub exec_query { |
| 596 |
my $query = shift; |
| 597 |
my $params = shift; |
| 598 |
die "invalid arguments to exec_query!" if @_; |
| 599 |
my @params; |
| 600 |
if (defined $params) { |
| 601 |
@params = ref $params eq 'ARRAY' ? @$params : $params; |
| 602 |
} |
| 603 |
|
| 604 |
my $err = "query failed: $query\n"; |
| 605 |
if (scalar @params) { |
| 606 |
$err .= join(',', @params); |
| 607 |
} |
| 608 |
|
| 609 |
#warn "err: $err\n"; |
| 610 |
if ($query =~ /(?:REPLACE|INSERT) INTO/) { |
| 611 |
my ($table) = $query =~ /(?:REPLACE|INSERT) INTO (\w+)\s/; |
| 612 |
$db->query($query, @params); |
| 613 |
warn "$db->error\n$err" if $db->error ne 'DBI error: '; |
| 614 |
return if $query =~ /^REPLACE/; |
| 615 |
my $id = $db->last_insert_id(undef, undef, $table, undef) or die $err; |
| 616 |
return $id; |
| 617 |
} |
| 618 |
elsif ($query =~ /^UPDATE/i) { |
| 619 |
return $db->query($query, @params); |
| 620 |
} |
| 621 |
elsif ($query =~ /DELETE/) { |
| 622 |
$db->query($query, @params) or die $err; |
| 623 |
return $db->query("SELECT ROW_COUNT()")->list; |
| 624 |
} |
| 625 |
|
| 626 |
my $r = $db->query($query, @params)->hashes or die $err; |
| 627 |
return $r; |
| 628 |
} |
| 629 |
|