| 1 | # Copyright(C) 2006 David Muir Sharnoff <muir@idiom.com> | ||
| 2 | # | ||
| 3 | # This program is free software; you can redistribute it and/or modify | ||
| 4 | # it under the terms of the GNU General Public License as published by | ||
| 5 | # the Free Software Foundation; either version 2 of the License, or | ||
| 6 | # (at your option) any later version. | ||
| 7 | # | ||
| 8 | # This program is distributed in the hope that it will be useful, | ||
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 11 | # GNU General Public License for more details. | ||
| 12 | # | ||
| 13 | # You should have received a copy of the GNU General Public License along | ||
| 14 | # with this program; if not, write to the Free Software Foundation, Inc., | ||
| 15 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 16 | # | ||
| 17 | # This software is available without the GPL: please write if you need | ||
| 18 | # a non-GPL license. All submissions of patches must come with a | ||
| 19 | # copyright grant so that David Sharnoff remains able to change the | ||
| 20 | # license at will. | ||
| 21 | |||
| 22 | |||
| 23 | package Qpsmtpd::Plugin::Quarantine::Common; | ||
| 24 | |||
| 25 | use Time::HiRes; | ||
| 26 | use OOPS; | ||
| 27 | use Digest::MD5 qw(md5_hex); | ||
| 28 | use File::Slurp; | ||
| 29 | use CGI qw(); | ||
| 30 | require Exporter; | ||
| 31 | use Sys::Hostname; | ||
| 32 | use strict; | ||
| 33 | |||
| 34 | our @ISA = qw(Exporter); | ||
| 35 | our @EXPORT = qw(new_sender new_recipient get_oops oops_args %defaults %escape recompute_defaults); | ||
| 36 | our @EXPORT_OK = qw(%base_defaults); | ||
| 37 | |||
| 38 | our $myhostname = hostname(); | ||
| 39 | |||
| 40 | # ------------------------------- begin defaults section ------------------------------------------------ | ||
| 41 | |||
| 42 | # | ||
| 43 | # These defaults can be overridden in the defaults_file (see below) or | ||
| 44 | # in the Qpsmtpd plugins configuration file. The location of the defaults | ||
| 45 | # file can't be overridden. The location of the Qpsmtpd configuration | ||
| 46 | # directory can only be overridden by the defaults_file. | ||
| 47 | # | ||
| 48 | our %base_defaults = ( | ||
| 49 | # | ||
| 50 | # Configuration files | ||
| 51 | # | ||
| 52 | defaults_file => '/etc/default/qpsmtpd-quarantine.pl', | ||
| 53 | templates => "/etc/qpsmtpd/quarantine-templates", | ||
| 54 | qpsmtpd_dir => '/etc/qpsmtpd', | ||
| 55 | admin_passwd_file => "/etc/qpsmtpd/quarantine.access", # htpasswd style user file | ||
| 56 | notify_recipient_only => "/etc/qpsmtpd/recipient.special.db", # notify these recipients instead of senders | ||
| 57 | special_sender_db => "/etc/qpsmtpd/sender.special.db", # always check mail from these senders | ||
| 58 | # $qpsmtpd_dir/filter_domains - list of domain names that might blacklist us | ||
| 59 | # $qpsmtpd_dir/our_domains - list of domain names that are us | ||
| 60 | # $qpsmtpd_dir/our_networks - list of IP addresses a.b.c.d/size that are us | ||
| 61 | # $qpsmtpd_dir/ignore_networks - list of IP addresses a.b.c.d/size that don't count | ||
| 62 | |||
| 63 | # | ||
| 64 | # Data Store | ||
| 65 | # | ||
| 66 | dbi_dsn => $ENV{OOPS_DSN} || 'DBI::SQLite:dbname=/var/spool/qpsmtpd-quarantine.db', | ||
| 67 | username => "biteme", # database user | ||
| 68 | password => "harder", # database password | ||
| 69 | table_prefix => 'q', # see OOPS documentation | ||
| 70 | |||
| 71 | # | ||
| 72 | # Identity | ||
| 73 | # | ||
| 74 | send_from => "root\@$myhostname", | ||
| 75 | # baseurl => "http://$myhostname/perl/quarantine.pl", # mod_perl with Apache::Registry | ||
| 76 | baseurl => "http://$myhostname/quarantine.cgi", | ||
| 77 | bounce_from => "MAILER-DAEMON\@$myhostname", | ||
| 78 | |||
| 79 | # | ||
| 80 | # Spam filtering | ||
| 81 | # | ||
| 82 | spamd3 => { | ||
| 83 | 'spamc -R -d 127.0.0.1 <' => 100, # can use a farm of servers, value is load share weighting | ||
| 84 | }, | ||
| 85 | accessio => '', # see http://www.miavia.com | ||
| 86 | clamd => '/usr/bin/clamdscan --stdout - <', | ||
| 87 | clamav => '/usr/local/bin/clamscan --stdout', | ||
| 88 | virus_content => qr/(?:application|name=.*\.(?:asd|bat|chm|cmd|com|cpl|dll|exe|hlp|hta|js|jse|lnk|ocx|pif|rar|scr|shb|shm|shs|vb|vbe|vbs|vbx|vxd|wsf|wsh|zip))/i, | ||
| 89 | subcommand_timeout => 150, | ||
| 90 | |||
| 91 | # | ||
| 92 | # Bounce message | ||
| 93 | # | ||
| 94 | senderbounce1 => 'Your message is quarantined because we think it is probably spam, if it is not spam, click', | ||
| 95 | senderbounce2 => 'to release your message from quarantine or to choose to have the spam you send silently deleted instead of bounced', | ||
| 96 | senderbounce3 => 'None of the recipients of your email wish to receive mail that is likely to be spam', | ||
| 97 | |||
| 98 | # | ||
| 99 | # Networks | ||
| 100 | # | ||
| 101 | ignore_networks => [ qw(127.0.0.0/8 10.0.0.0/8 172.16/12 192.168/16) ], | ||
| 102 | |||
| 103 | # | ||
| 104 | # Mail configuration | ||
| 105 | # | ||
| 106 | bypass_mailhosts => [qw(127.0.0.1)], # Where to SMTP-inject messages (post-filter) | ||
| 107 | bypass_mailcmd => [qw(/usr/sbin/sendmail -oeml -i)], # ... if that didn't work | ||
| 108 | nobody_address => "nobody\@$myhostname", # this user should silently discard | ||
| 109 | |||
| 110 | |||
| 111 | # | ||
| 112 | # Quarantine behavior | ||
| 113 | # | ||
| 114 | check_all_recipients => 0, # Check mail to non-filtered domains for spam too | ||
| 115 | randomly_check_messages => 1, # check N % of messages regardless | ||
| 116 | check_from_our_domain => 0, # Force a check of mail from our domains even if not to a filtered destination | ||
| 117 | check_not_our_domain => 0, # Force a check of mail not from our domains even if not to a filtered destination | ||
| 118 | check_from_our_ip => 0, # Force a check of mail from our netblock even if not to a filtered destination | ||
| 119 | check_not_from_our_ip => 0, # Force a check of mail not from our netblock even if not to a filtered destination | ||
| 120 | renotify_sender_ip => 10, # Send a bounce every N days (per IP, per sender) | ||
| 121 | notify_recipients => 75, # After N messages, notify the recipient instead (0 = disable) | ||
| 122 | renotify_recipient_days => 10, # Notify recipients every N days (when there is a new message) | ||
| 123 | max_bounces_per_header => 3, # allow N bounes with our URL to bypass the filter (per message URL) | ||
| 124 | notify_other_senders => 0, # send bounces to external senders? | ||
| 125 | |||
| 126 | # | ||
| 127 | # Cron job config | ||
| 128 | # | ||
| 129 | sender_stride_length => 40, # when cleaning, process N senders per transaction | ||
| 130 | recipient_stride_length => 40, # when cleaning, process N recipients per transaction | ||
| 131 | sender_history_to_keep => 20, # how many days spamming history to keep per sender | ||
| 132 | keep_every_nth_message => 200, # quarantine every Nth spam per sender (regardless of sender settings) | ||
| 133 | report_senders_after => 100, # minimum number of spams required to make a report on a sender | ||
| 134 | message_longevity => 30, # how long to keep messages in quarantine (days) | ||
| 135 | delete_batchsize => 50, # how many messages to delete per transaction | ||
| 136 | keep_idle_recipients => 720, # how long to keep idle recipients that have settings (days) | ||
| 137 | message_store_size => 1500, # Megabytes | ||
| 138 | |||
| 139 | # | ||
| 140 | # Internal data structures | ||
| 141 | # | ||
| 142 | size_storage_array_size => 256, # transaction parallism | ||
| 143 | message_size_overhead => 500, # header, etc | ||
| 144 | |||
| 145 | # | ||
| 146 | # Internal Mail Queue | ||
| 147 | # | ||
| 148 | mqueue_stride_length => 50, # when sending mail, process N recipients per transaction | ||
| 149 | mqueue_minimum_attempts => 100, # how many times to try sending something | ||
| 150 | mqueue_minimum_gap => 900, # minimum time between attempts (seconds) | ||
| 151 | mqueue_maximum_keep => 3, # days to keep trying | ||
| 152 | |||
| 153 | # | ||
| 154 | # Form buttons. All values must be distinct. | ||
| 155 | # | ||
| 156 | button_login => 'Login', | ||
| 157 | button_logout => 'Log Out', | ||
| 158 | button_lookup_email => 'Lookup Email Address', | ||
| 159 | button_sender_update => 'Update Sender Settings', | ||
| 160 | button_sender_delete => 'Delete', | ||
| 161 | button_sender_send => 'Send To Recipient', | ||
| 162 | button_sender_url => 'Send Me An Settings Access URL', | ||
| 163 | button_sender_send_checked => 'Send Checked Messages To Recipients', | ||
| 164 | button_sender_delete_all => 'Delete All Messages', | ||
| 165 | button_sender_delete_checked => 'Delete Checked Messages', | ||
| 166 | button_sender_replace_token => 'ReplaceSenderToken', | ||
| 167 | button_sender_reset_timer => 'Reset Bounce Notification Timer', | ||
| 168 | button_recipient_update => 'Update My Settings', | ||
| 169 | button_recipient_url => 'Send Me An Access URL', | ||
| 170 | button_recipient_replace_token => 'ReplaceToken', | ||
| 171 | # button_recipient_reset_timer => 'Reset Notification Timer', | ||
| 172 | |||
| 173 | ); | ||
| 174 | |||
| 175 | # ------------------------------- end defaults section -------------------------------------------------- | ||
| 176 | |||
| 177 | sub FuncT::TIEHASH { my $p = shift; return bless shift, $p } | ||
| 178 | sub FuncT::FETCH { my $f = shift; return &$f(shift) } | ||
| 179 | |||
| 180 | tie our %escape, 'FuncT', \&CGI::escape; | ||
| 181 | |||
| 182 | do $base_defaults{defaults_file} if -e $base_defaults{defaults_file}; | ||
| 183 | |||
| 184 | our %defaults; | ||
| 185 | |||
| 186 | recompute_defaults(); | ||
| 187 | |||
| 188 | sub recompute_defaults | ||
| 189 | { | ||
| 190 | %defaults = %base_defaults; | ||
| 191 | |||
| 192 | my $qpsmtpd_plugins = "$base_defaults{qpsmtpd_dir}/plugins"; | ||
| 193 | |||
| 194 | my ($cl) = grep(s/^quarantine\s+//, read_file($qpsmtpd_plugins)); | ||
| 195 | my (%config) = split(' ', $cl); | ||
| 196 | |||
| 197 | @defaults{keys %config} = values %config; | ||
| 198 | |||
| 199 | @Mail::SendVarious::mail_command = @{$defaults{bypass_mailcmd}}; | ||
| 200 | @Mail::SendVarious::mail_hostlist = @{$defaults{bypass_mailhosts}}; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub new_sender | ||
| 204 | { | ||
| 205 | my ($oops, $hostdomain, $address, $sender_token) = @_; | ||
| 206 | |||
| 207 | $sender_token = md5_hex($$ . Time::HiRes::time() . $hostdomain) | ||
| 208 | unless $sender_token; | ||
| 209 | |||
| 210 | my $psender = $oops->{quarantine}{senders}{$hostdomain} = bless { | ||
| 211 | canonical => $hostdomain, | ||
| 212 | address => $address, | ||
| 213 | token => $sender_token, | ||
| 214 | headers => {}, | ||
| 215 | send_ip_used => {}, | ||
| 216 | }, 'Quarantine::Sender'; | ||
| 217 | $oops->virtual_object($psender->{headers}); | ||
| 218 | $oops->virtual_object($psender->{send_ip_used}); | ||
| 219 | return $psender; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub new_recipient | ||
| 223 | { | ||
| 224 | my ($oops, $address) = @_; | ||
| 225 | my $qd = $oops->{quarantine} || die; | ||
| 226 | my $rd = $qd->{recipients}{$address} = bless { | ||
| 227 | address => $address, | ||
| 228 | mcount => 0, | ||
| 229 | headers => {}, | ||
| 230 | }, 'Quarantine::Recipient'; | ||
| 231 | $oops->virtual_object($rd->{headers}, 1); | ||
| 232 | return $rd; | ||
| 233 | } | ||
| 234 | |||
| 235 | sub get_oops | ||
| 236 | { | ||
| 237 | my ($config, %extra) = @_; | ||
| 238 | my $oops = new OOPS | ||
| 239 | oops_args($config), | ||
| 240 | %extra; | ||
| 241 | return $oops; | ||
| 242 | } | ||
| 243 | |||
| 244 | sub oops_args | ||
| 245 | { | ||
| 246 | return ( | ||
| 247 | dbi_dsn => $defaults{dbi_dsn}, | ||
| 248 | user => $defaults{username}, | ||
| 249 | password => $defaults{password}, | ||
| 250 | table_prefix => $defaults{table_prefix}, | ||
| 251 | auto_initialize => 1, | ||
| 252 | auto_upgrade => 1, | ||
| 253 | ); | ||
| 254 | } | ||
| 255 | |||
| 256 | my $config_pointer; | ||
| 257 | |||
| 258 | #sub get_config | ||
| 259 | #{ | ||
| 260 | # return $config_pointer if $config_pointer; | ||
| 261 | # | ||
| 262 | # my $qpsmtpd_plugins = "$base_defaults{qpsmtpd_dir}/plugins"; | ||
| 263 | # | ||
| 264 | # my ($cl) = grep(s/^quarantine\s+//, read_file($qpsmtpd_plugins)); | ||
| 265 | # my (%config) = split(' ', $cl); | ||
| 266 | # | ||
| 267 | # $config_pointer = \%config; | ||
| 268 | # return \%config; | ||
| 269 | #} | ||
| 270 | |||
| 271 | #sub config_item | ||
| 272 | #{ | ||
| 273 | # my ($item, $config) = @_; | ||
| 274 | # $config = get_config() | ||
| 275 | # unless $config; | ||
| 276 | # return $config->{$item} | ||
| 277 | # if exists $config->{$item}; | ||
| 278 | # return $defaults{$item} | ||
| 279 | # if exists $defaults{$item}; | ||
| 280 | # die "No config or default for '$item'"; | ||
| 281 | #} | ||
| 282 | |||
| 283 | package Quarantine::Sender; | ||
| 284 | |||
| 285 | use strict; | ||
| 286 | use Qpsmtpd::Plugin::Quarantine::Common; | ||
| 287 | |||
| 288 | sub url | ||
| 289 | { | ||
| 290 | my ($psender) = @_; | ||
| 291 | |||
| 292 | die unless ref($psender); | ||
| 293 | return "$defaults{baseurl}/sender/$psender->{token}/$escape{$psender->{canonical}}"; | ||
| 294 | } | ||
| 295 | |||
| 296 | sub has_settings | ||
| 297 | { | ||
| 298 | my ($psender) = @_; | ||
| 299 | return 1 if $psender->{action}; | ||
| 300 | return 1 if $psender->{renotify_days}; | ||
| 301 | return 0; | ||
| 302 | } | ||
| 303 | |||
| 304 | package Quarantine::Recipient; | ||
| 305 | |||
| 306 | use strict; | ||
| 307 | use Digest::MD5 qw(md5_hex); | ||
| 308 | use Qpsmtpd::Plugin::Quarantine::Common; | ||
| 309 | use Carp; | ||
| 310 | |||
| 311 | sub url | ||
| 312 | { | ||
| 313 | my ($rd, $qd) = @_; | ||
| 314 | |||
| 315 | croak unless ref($rd); | ||
| 316 | croak unless ref($qd); | ||
| 317 | croak unless $rd->{address}; | ||
| 318 | my $token = $rd->{token} || md5_hex($qd->{random_token} . $rd->{address}); | ||
| 319 | |||
| 320 | return "$defaults{baseurl}/recipient/$token/$escape{$rd->{address}}"; | ||
| 321 | } | ||
| 322 | |||
| 323 | sub has_settings | ||
| 324 | { | ||
| 325 | my ($psender) = @_; | ||
| 326 | return 1 if $psender->{action}; | ||
| 327 | return 1 if $psender->{token}; | ||
| 328 | return 1 if $psender->{new_address}; | ||
| 329 | return 0; | ||
| 330 | } | ||
| 331 | |||
| 332 | 1; |