| 1 | |||
| 2 | package SyslogScan::Daemon::BlacklistDetector::EmailNotify; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use SyslogScan::Daemon::BlacklistDetector::Plugin; | ||
| 7 | use Mail::SendVarious; | ||
| 8 | use Plugins::SimpleConfig; | ||
| 9 | |||
| 10 | our(@ISA) = qw(SyslogScan::Daemon::BlacklistDetector::Plugin); | ||
| 11 | |||
| 12 | my %defaults = ( | ||
| 13 | notify => 'root', | ||
| 14 | renotify_time => 7200, | ||
| 15 | forget_time => 3600, | ||
| 16 | maxkeep => 100, | ||
| 17 | sendfrom => 'root', | ||
| 18 | clean_time => 1800, | ||
| 19 | debug => 0, | ||
| 20 | ); | ||
| 21 | |||
| 22 | sub config_prefix { return 'blden_' } | ||
| 23 | |||
| 24 | sub parse_config_line { simple_config_line(\%defaults, @_); } | ||
| 25 | |||
| 26 | sub new | ||
| 27 | { | ||
| 28 | my $self = simple_new(\%defaults, @_); | ||
| 29 | $self->{last_clean} = 0; | ||
| 30 | return $self; | ||
| 31 | } | ||
| 32 | |||
| 33 | my %notified; | ||
| 34 | my %found; | ||
| 35 | |||
| 36 | sub blacklisted | ||
| 37 | { | ||
| 38 | my ($self, %info) = @_; | ||
| 39 | |||
| 40 | my $sourceip = $info{sourceip}; | ||
| 41 | my $destdomain = $info{destdomain}; | ||
| 42 | |||
| 43 | my $renotify_time = $self->{renotify_time}; | ||
| 44 | my $debug = $self->{debug}; | ||
| 45 | my $notify = $self->{notify}; | ||
| 46 | my $sendfrom = $self->{sendfrom}; | ||
| 47 | |||
| 48 | push(@{$found{$sourceip}{$destdomain}}, { | ||
| 49 | time => time(), | ||
| 50 | source => $sourceip, | ||
| 51 | dest => $destdomain, | ||
| 52 | line => $info{logline}, | ||
| 53 | }); | ||
| 54 | |||
| 55 | if ($notified{$sourceip}{$destdomain} | ||
| 56 | && (time - $notified{$sourceip}{$destdomain} < $renotify_time)) | ||
| 57 | { | ||
| 58 | print STDERR "Not yet time to re-notify\n" if $debug; | ||
| 59 | return; | ||
| 60 | } | ||
| 61 | |||
| 62 | $self->clean_found(); | ||
| 63 | |||
| 64 | my $body = ''; | ||
| 65 | |||
| 66 | $notified{$sourceip}{$destdomain} = time; | ||
| 67 | $body .= "----------------- $sourceip to $destdomain -------------------\n"; | ||
| 68 | while (my $br = pop(@{$found{$sourceip}{$destdomain}})) { | ||
| 69 | $body .= $br->{line}; | ||
| 70 | } | ||
| 71 | delete $found{$sourceip}{$destdomain}; | ||
| 72 | delete $found{$sourceip} unless %{$found{$sourceip}}; | ||
| 73 | for my $source (keys %found) { | ||
| 74 | for my $dest (keys %{$found{$source}}) { | ||
| 75 | $notified{$source}{$dest} = time; | ||
| 76 | $body .= "\n\n----------------- $source to $dest -------------------\n"; | ||
| 77 | while (my $br2 = pop(@{$found{$source}{$dest}})) { | ||
| 78 | $body .= $br2->{line}; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | print STDERR "Sending notification to $notify: $destdomain\n$body\n" if $debug >= 2; | ||
| 84 | sendmail( | ||
| 85 | From => "\u$0", | ||
| 86 | from => $sendfrom, | ||
| 87 | to => $notify, | ||
| 88 | subject => "We are blackholed by $destdomain\n", | ||
| 89 | body => $body, | ||
| 90 | ); | ||
| 91 | } | ||
| 92 | |||
| 93 | sub clean_found | ||
| 94 | { | ||
| 95 | my $self = shift; | ||
| 96 | my $forget_time = $self->{forget_time}; | ||
| 97 | my $debug = $self->{debug}; | ||
| 98 | |||
| 99 | for my $source (keys %found) { | ||
| 100 | for my $dest (keys %{$found{$source}}) { | ||
| 101 | pop(@{$found{$source}{$dest}}) while | ||
| 102 | @{$found{$source}{$dest}} | ||
| 103 | && | ||
| 104 | ( | ||
| 105 | (time - $found{$source}{$dest}[0]{time} >= $forget_time) | ||
| 106 | || | ||
| 107 | @{$found{$source}{$dest}} > $self->{maxkeep} | ||
| 108 | ); | ||
| 109 | delete $found{$source}{$dest} | ||
| 110 | unless @{$found{$source}{$dest}}; | ||
| 111 | } | ||
| 112 | delete $found{$source} | ||
| 113 | unless %{$found{$source}}; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | my $last_clean = 0; | ||
| 118 | |||
| 119 | sub periodic | ||
| 120 | { | ||
| 121 | my $self = shift; | ||
| 122 | my $debug = $self->{debug}; | ||
| 123 | if ($self->{last_clean} + $self->{clean_time} < time) { | ||
| 124 | printf "%s periodic running\n", __PACKAGE__ if $debug; | ||
| 125 | $self->clean_found(); | ||
| 126 | $self->{last_clean} = time; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | 1; | ||
| 131 | |||
| 132 | =head1 NAME | ||
| 133 | |||
| 134 | SyslogScan::Daemon::BlacklistDetector::EmailNotify - send email when blacklisted | ||
| 135 | |||
| 136 | =head1 SYNOPSIS | ||
| 137 | |||
| 138 | bld_plugin SyslogScan::Daemon::BlacklistDetector::EmailNotify | ||
| 139 | debug 0 | ||
| 140 | notify your@email.here | ||
| 141 | renotify_time 7200 | ||
| 142 | forget_time 3600 | ||
| 143 | sendfrom root | ||
| 144 | clean_time 1800 | ||
| 145 | maxkeep 100 | ||
| 146 | |||
| 147 | =head1 DESCRIPTION | ||
| 148 | |||
| 149 | SyslogScan::Daemon::BlacklistDetector::EmailNotify | ||
| 150 | sends email when SyslogScan::Daemon::BlacklistDetector | ||
| 151 | detects blacklisting. | ||
| 152 | |||
| 153 | SyslogScan::Daemon::BlacklistDetector is a plugin for | ||
| 154 | L<SyslogScan::Daemon::BlacklistDetector>. | ||
| 155 | The SYNOPSIS shows the configuration | ||
| 156 | lines you might use in C</etc/syslogscand.conf> to turn on | ||
| 157 | the email notification. | ||
| 158 | |||
| 159 | =head1 CONFIGURATION PARAMETERS | ||
| 160 | |||
| 161 | SyslogScan::Daemon::BlacklistDetector::EmailNotify defines the following configuration | ||
| 162 | parameters which may be given in indented lines that follow | ||
| 163 | C<plugin SyslogScan::Daemon::BlacklistDetector::EmailNotify> or with the | ||
| 164 | confuration prefix (C<blden_>) anywhere in the configuration file after the | ||
| 165 | C<plugin SyslogScan::Daemon::BlacklistDetector::EmailNotify> line. | ||
| 166 | |||
| 167 | =over 15 | ||
| 168 | |||
| 169 | =item debug | ||
| 170 | |||
| 171 | (default 0) Turn on debugging. | ||
| 172 | |||
| 173 | =item notify | ||
| 174 | |||
| 175 | (default C<root>) Where should the notifications be sent? | ||
| 176 | |||
| 177 | =item sendfrom | ||
| 178 | |||
| 179 | (default C<root>) What email address should notifications be | ||
| 180 | sent from? | ||
| 181 | |||
| 182 | =item renotify_time | ||
| 183 | |||
| 184 | (default 7200) Seconds. How often should an additional email | ||
| 185 | be sent regarding the same destination. Before the time is up, | ||
| 186 | extra notifications will be queued. | ||
| 187 | |||
| 188 | =item forget_time | ||
| 189 | |||
| 190 | (default 3600) Seconds. How long should unsent notifications | ||
| 191 | be kept. | ||
| 192 | |||
| 193 | =item maxkeep | ||
| 194 | |||
| 195 | (default 100) How many unsent notifcations can be queued regarding | ||
| 196 | the same destination? | ||
| 197 | |||
| 198 | =item clean_time | ||
| 199 | |||
| 200 | (default 1800) Seconds. How often should the unsent notication | ||
| 201 | queue be cleaned of extra entries? | ||
| 202 | |||
| 203 | =back | ||
| 204 | |||
| 205 | =head1 SEE ALSO | ||
| 206 | |||
| 207 | The context for the blacklist detector: | ||
| 208 | L<SyslogScan::Daemon::BlacklistDetector> | ||
| 209 | |||
| 210 | =head1 LICENSE | ||
| 211 | |||
| 212 | Copyright (C) 2006, David Muir Sharnoff <muir@idiom.com> | ||
| 213 | |||
| 214 | This module may be used and copied on the same terms as Perl | ||
| 215 | itself. | ||
| 216 |