| 1 |
package Qpsmtpd::Plugin; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Net::DNS; |
| 7 |
|
| 8 |
use Qpsmtpd::Constants; |
| 9 |
|
| 10 |
# more or less in the order they will fire |
| 11 |
our @hooks = qw( |
| 12 |
logging config post-fork pre-connection connect ehlo_parse ehlo |
| 13 |
helo_parse helo auth_parse auth auth-plain auth-login auth-cram-md5 |
| 14 |
rcpt_parse rcpt_pre rcpt mail_parse mail mail_pre |
| 15 |
data data_headers_end data_post queue_pre queue queue_post vrfy noop |
| 16 |
quit reset_transaction disconnect post-connection |
| 17 |
unrecognized_command deny ok received_line help |
| 18 |
); |
| 19 |
our %hooks = map { $_ => 1 } @hooks; |
| 20 |
|
| 21 |
sub new { |
| 22 |
my $proto = shift; |
| 23 |
my $class = ref($proto) || $proto; |
| 24 |
bless({}, $class); |
| 25 |
} |
| 26 |
|
| 27 |
sub hook_name { |
| 28 |
return shift->{_hook}; |
| 29 |
} |
| 30 |
|
| 31 |
sub register_hook { |
| 32 |
my ($plugin, $hook, $method, $unshift) = @_; |
| 33 |
|
| 34 |
die $plugin->plugin_name . " : Invalid hook: $hook" unless $hooks{$hook}; |
| 35 |
|
| 36 |
$plugin->{_qp}->log(LOGDEBUG, $plugin->plugin_name, "hooking", $hook) |
| 37 |
unless $hook =~ /logging/; # can't log during load_logging() |
| 38 |
|
| 39 |
# I can't quite decide if it's better to parse this code ref or if |
| 40 |
# we should pass the plugin object and method name ... hmn. |
| 41 |
$plugin->qp->_register_hook( |
| 42 |
$hook, |
| 43 |
{ |
| 44 |
code => sub { |
| 45 |
local $plugin->{_qp} = shift; |
| 46 |
local $plugin->{_hook} = $hook; |
| 47 |
$plugin->$method(@_); |
| 48 |
}, |
| 49 |
name => $plugin->plugin_name, |
| 50 |
}, |
| 51 |
$unshift, |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
sub _register { |
| 56 |
my $self = shift; |
| 57 |
my $qp = shift; |
| 58 |
local $self->{_qp} = $qp; |
| 59 |
$self->init($qp, @_) if $self->can('init'); |
| 60 |
$self->_register_standard_hooks($qp, @_); |
| 61 |
$self->register($qp, @_) if $self->can('register'); |
| 62 |
} |
| 63 |
|
| 64 |
sub qp { |
| 65 |
shift->{_qp}; |
| 66 |
} |
| 67 |
|
| 68 |
sub log { |
| 69 |
my $self = shift; |
| 70 |
return if defined $self->{_hook} && $self->{_hook} eq 'logging'; |
| 71 |
my $level = $self->adjust_log_level(shift, $self->plugin_name); |
| 72 |
$self->{_qp}->varlog($level, $self->{_hook}, $self->plugin_name, @_); |
| 73 |
} |
| 74 |
|
| 75 |
sub adjust_log_level { |
| 76 |
my ($self, $cur_level, $plugin_name) = @_; |
| 77 |
|
| 78 |
my $adj = $self->{_args}{loglevel} or return $cur_level; |
| 79 |
|
| 80 |
return $adj if $adj =~ m/^[01234567]$/; # a raw syslog numeral |
| 81 |
|
| 82 |
if ($adj !~ /^[\+\-][\d]$/) { |
| 83 |
$self->log(LOGERROR, |
| 84 |
$self - "invalid $plugin_name loglevel setting ($adj)"); |
| 85 |
undef $self->{_args}{loglevel}; # only complain once per plugin |
| 86 |
return $cur_level; |
| 87 |
} |
| 88 |
|
| 89 |
my $operator = substr($adj, 0, 1); |
| 90 |
my $adjust = substr($adj, -1, 1); |
| 91 |
|
| 92 |
my $new_level = |
| 93 |
$operator eq '+' ? $cur_level + $adjust : $cur_level - $adjust; |
| 94 |
|
| 95 |
$new_level = 7 if $new_level > 7; |
| 96 |
$new_level = 0 if $new_level < 0; |
| 97 |
|
| 98 |
return $new_level; |
| 99 |
} |
| 100 |
|
| 101 |
sub transaction { |
| 102 |
|
| 103 |
# not sure if this will work in a non-forking or a threaded daemon |
| 104 |
shift->qp->transaction; |
| 105 |
} |
| 106 |
|
| 107 |
sub connection { |
| 108 |
shift->qp->connection; |
| 109 |
} |
| 110 |
|
| 111 |
sub spool_dir { |
| 112 |
shift->qp->spool_dir; |
| 113 |
} |
| 114 |
|
| 115 |
sub auth_user { |
| 116 |
shift->qp->auth_user; |
| 117 |
} |
| 118 |
|
| 119 |
sub auth_mechanism { |
| 120 |
shift->qp->auth_mechanism; |
| 121 |
} |
| 122 |
|
| 123 |
sub temp_file { |
| 124 |
my $self = shift; |
| 125 |
my $tempfile = $self->qp->temp_file; |
| 126 |
push @{$self->qp->transaction->{_temp_files}}, $tempfile; |
| 127 |
return $tempfile; |
| 128 |
} |
| 129 |
|
| 130 |
sub temp_dir { |
| 131 |
my $self = shift; |
| 132 |
my $tempdir = $self->qp->temp_dir(); |
| 133 |
push @{$self->qp->transaction->{_temp_dirs}}, $tempdir; |
| 134 |
return $tempdir; |
| 135 |
} |
| 136 |
|
| 137 |
# plugin inheritance: |
| 138 |
# usage: |
| 139 |
# sub init { |
| 140 |
# my $self = shift; |
| 141 |
# $self->isa_plugin("rhsbl"); |
| 142 |
# $self->SUPER::register(@_); |
| 143 |
# } |
| 144 |
sub isa_plugin { |
| 145 |
my ($self, $parent) = @_; |
| 146 |
my ($currentPackage) = caller; |
| 147 |
|
| 148 |
my $cleanParent = $parent; |
| 149 |
$cleanParent =~ s/\W/_/g; |
| 150 |
my $newPackage = $currentPackage . "::_isa_$cleanParent"; |
| 151 |
|
| 152 |
# don't reload plugins if they are already loaded |
| 153 |
return if defined &{"${newPackage}::plugin_name"}; |
| 154 |
|
| 155 |
# find $parent in plugin_dirs |
| 156 |
my $parent_dir; |
| 157 |
for ($self->qp->plugin_dirs) { |
| 158 |
if (-e "$_/$parent") { |
| 159 |
$parent_dir = $_; |
| 160 |
last; |
| 161 |
} |
| 162 |
} |
| 163 |
die "cannot find plugin '$parent'" unless $parent_dir; |
| 164 |
|
| 165 |
$self->compile($self->plugin_name . "_isa_$cleanParent", |
| 166 |
$newPackage, "$parent_dir/$parent"); |
| 167 |
warn "---- $newPackage\n"; |
| 168 |
no strict 'refs'; |
| 169 |
push @{"${currentPackage}::ISA"}, $newPackage; |
| 170 |
} |
| 171 |
|
| 172 |
# why isn't compile private? it's only called from Plugin and Qpsmtpd. |
| 173 |
sub compile { |
| 174 |
my ($class, $plugin, $package, $file, $test_mode, $orig_name) = @_; |
| 175 |
|
| 176 |
my $sub; |
| 177 |
open F, $file or die "could not open $file: $!"; |
| 178 |
{ |
| 179 |
local $/ = undef; |
| 180 |
$sub = <F>; |
| 181 |
} |
| 182 |
close F; |
| 183 |
|
| 184 |
my $line = "\n#line 0 $file\n"; |
| 185 |
|
| 186 |
if ($test_mode) { |
| 187 |
if (open(F, "t/plugin_tests/$orig_name")) { |
| 188 |
local $/ = undef; |
| 189 |
$sub .= "#line 1 t/plugin_tests/$orig_name\n"; |
| 190 |
$sub .= <F>; |
| 191 |
close F; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
my $eval = join( |
| 196 |
"\n", |
| 197 |
"package $package;", |
| 198 |
'use Qpsmtpd::Constants;', |
| 199 |
"require Qpsmtpd::Plugin;", |
| 200 |
'use vars qw(@ISA);', |
| 201 |
'use strict;', |
| 202 |
'@ISA = qw(Qpsmtpd::Plugin);', |
| 203 |
($test_mode ? 'use Test::More;' : ''), |
| 204 |
"sub plugin_name { qq[$plugin] }", |
| 205 |
$line, |
| 206 |
$sub, |
| 207 |
"\n", # last line comment without newline? |
| 208 |
); |
| 209 |
|
| 210 |
#warn "eval: $eval"; |
| 211 |
|
| 212 |
$eval =~ m/(.*)/s; |
| 213 |
$eval = $1; |
| 214 |
|
| 215 |
eval $eval; |
| 216 |
die "eval $@" if $@; |
| 217 |
} |
| 218 |
|
| 219 |
sub get_reject { |
| 220 |
my $self = shift; |
| 221 |
my $smtp_mess = shift || "unspecified error"; |
| 222 |
my $log_mess = shift || ''; |
| 223 |
$log_mess = ", $log_mess" if $log_mess; |
| 224 |
|
| 225 |
my $reject = $self->{_args}{reject}; |
| 226 |
if (defined $reject && !$reject) { |
| 227 |
$self->log(LOGINFO, "fail, tolerated" . $log_mess); |
| 228 |
return DECLINED; |
| 229 |
} |
| 230 |
|
| 231 |
# the naughty plugin will reject later |
| 232 |
if ($reject eq 'naughty') { |
| 233 |
$self->log(LOGINFO, "fail, NAUGHTY" . $log_mess); |
| 234 |
return $self->store_deferred_reject($smtp_mess); |
| 235 |
} |
| 236 |
|
| 237 |
# they asked for reject, we give them reject |
| 238 |
$self->log(LOGINFO, "fail" . $log_mess); |
| 239 |
return ($self->get_reject_type(), $smtp_mess); |
| 240 |
} |
| 241 |
|
| 242 |
sub get_reject_type { |
| 243 |
my $self = shift; |
| 244 |
my $default = shift || DENY; |
| 245 |
my $deny = shift || $self->{_args}{reject_type} or return $default; |
| 246 |
|
| 247 |
return |
| 248 |
$deny =~ /^(temp|soft)$/i ? DENYSOFT |
| 249 |
: $deny =~ /^(perm|hard)$/i ? DENY |
| 250 |
: $deny eq 'disconnect' ? DENY_DISCONNECT |
| 251 |
: $deny eq 'temp_disconnect' ? DENYSOFT_DISCONNECT |
| 252 |
: $default; |
| 253 |
} |
| 254 |
|
| 255 |
sub store_deferred_reject { |
| 256 |
my ($self, $smtp_mess) = @_; |
| 257 |
|
| 258 |
# store the reject message that the naughty plugin will return later |
| 259 |
if (!$self->connection->notes('naughty')) { |
| 260 |
$self->connection->notes('naughty', $smtp_mess); |
| 261 |
} |
| 262 |
else { |
| 263 |
# append this reject message to the message |
| 264 |
my $prev = $self->connection->notes('naughty'); |
| 265 |
$self->connection->notes('naughty', "$prev\015\012$smtp_mess"); |
| 266 |
} |
| 267 |
if (!$self->connection->notes('naughty_reject_type')) { |
| 268 |
$self->connection->notes('naughty_reject_type', |
| 269 |
$self->{_args}{reject_type}); |
| 270 |
} |
| 271 |
return (DECLINED); |
| 272 |
} |
| 273 |
|
| 274 |
sub store_auth_results { |
| 275 |
my ($self, $result) = @_; |
| 276 |
my $auths = $self->qp->connection->notes('authentication_results') or do { |
| 277 |
$self->qp->connection->notes('authentication_results', $result); |
| 278 |
return; |
| 279 |
}; |
| 280 |
my $ar = join('; ', $auths, $result); |
| 281 |
$self->log(LOGDEBUG, "auth-results: $ar"); |
| 282 |
$self->qp->connection->notes('authentication_results', $ar ); |
| 283 |
}; |
| 284 |
|
| 285 |
sub init_resolver { |
| 286 |
my $self = shift; |
| 287 |
my $timeout = $self->{_args}{dns_timeout} || shift || 5; |
| 288 |
return $self->{_resolver} if $self->{_resolver}; |
| 289 |
$self->log(LOGDEBUG, "initializing Net::DNS::Resolver"); |
| 290 |
$self->{_resolver} = Net::DNS::Resolver->new(dnsrch => 0); |
| 291 |
$self->{_resolver}->tcp_timeout($timeout); |
| 292 |
$self->{_resolver}->udp_timeout($timeout); |
| 293 |
return $self->{_resolver}; |
| 294 |
} |
| 295 |
|
| 296 |
sub is_immune { |
| 297 |
my $self = shift; |
| 298 |
|
| 299 |
if ($self->qp->connection->relay_client()) { |
| 300 |
|
| 301 |
# set by plugins/relay, or Qpsmtpd::Auth |
| 302 |
$self->log(LOGINFO, "skip, relay client"); |
| 303 |
return 1; |
| 304 |
} |
| 305 |
if ($self->qp->connection->notes('whitelisthost')) { |
| 306 |
|
| 307 |
# set by plugins/dns_whitelist_soft or plugins/whitelist |
| 308 |
$self->log(LOGINFO, "skip, whitelisted host"); |
| 309 |
return 1; |
| 310 |
} |
| 311 |
if ($self->qp->transaction->notes('whitelistsender')) { |
| 312 |
|
| 313 |
# set by plugins/whitelist |
| 314 |
$self->log(LOGINFO, "skip, whitelisted sender"); |
| 315 |
return 1; |
| 316 |
} |
| 317 |
return; |
| 318 |
} |
| 319 |
|
| 320 |
sub is_naughty { |
| 321 |
my ($self, $setit) = @_; |
| 322 |
|
| 323 |
# see plugins/naughty |
| 324 |
return $self->connection->notes('naughty') if ! defined $setit; |
| 325 |
|
| 326 |
$self->connection->notes('naughty', $setit); |
| 327 |
$self->connection->notes('rejected', $setit); |
| 328 |
|
| 329 |
if ($self->connection->notes('naughty')) { |
| 330 |
$self->log(LOGINFO, "skip, naughty"); |
| 331 |
return 1; |
| 332 |
} |
| 333 |
|
| 334 |
if ($self->connection->notes('rejected')) { |
| 335 |
|
| 336 |
# http://www.steve.org.uk/Software/ms-lite/ |
| 337 |
$self->log(LOGINFO, "skip, already rejected"); |
| 338 |
return 1; |
| 339 |
} |
| 340 |
return; |
| 341 |
} |
| 342 |
|
| 343 |
sub adjust_karma { |
| 344 |
my ($self, $value) = @_; |
| 345 |
|
| 346 |
my $karma = $self->connection->notes('karma') || 0; |
| 347 |
$karma += $value; |
| 348 |
$self->log(LOGINFO, "karma $value ($karma)"); |
| 349 |
$self->connection->notes('karma', $karma); |
| 350 |
return $value; |
| 351 |
} |
| 352 |
|
| 353 |
sub _register_standard_hooks { |
| 354 |
my ($plugin, $qp) = @_; |
| 355 |
|
| 356 |
for my $hook (@hooks) { |
| 357 |
my $hooksub = "hook_$hook"; |
| 358 |
$hooksub =~ s/\W/_/g; |
| 359 |
$plugin->register_hook($hook, $hooksub) |
| 360 |
if ($plugin->can($hooksub)); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
1; |