2002-07-06 09:18:48 +02:00
|
|
|
package Qpsmtpd::Plugin;
|
2012-05-09 00:04:10 +02:00
|
|
|
|
2002-07-06 09:18:48 +02:00
|
|
|
use strict;
|
2012-05-09 00:04:10 +02:00
|
|
|
use warnings;
|
|
|
|
|
2013-03-28 00:03:13 +01:00
|
|
|
use Net::DNS;
|
|
|
|
|
2012-05-09 00:04:10 +02:00
|
|
|
use Qpsmtpd::Constants;
|
2002-07-06 09:18:48 +02:00
|
|
|
|
2006-03-01 17:46:55 +01:00
|
|
|
# more or less in the order they will fire
|
2005-07-14 04:31:01 +02:00
|
|
|
our @hooks = qw(
|
2013-04-21 06:08:43 +02:00
|
|
|
logging config post-fork pre-connection connect ehlo_parse ehlo
|
|
|
|
helo_parse helo auth_parse auth auth-plain auth-login auth-cram-md5
|
|
|
|
rcpt_parse rcpt_pre rcpt mail_parse mail mail_pre
|
|
|
|
data data_headers_end data_post queue_pre queue queue_post vrfy noop
|
|
|
|
quit reset_transaction disconnect post-connection
|
|
|
|
unrecognized_command deny ok received_line help
|
|
|
|
);
|
2005-07-14 04:31:01 +02:00
|
|
|
our %hooks = map { $_ => 1 } @hooks;
|
2004-03-05 13:46:24 +01:00
|
|
|
|
2002-07-06 09:18:48 +02:00
|
|
|
sub new {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $proto = shift;
|
|
|
|
my $class = ref($proto) || $proto;
|
|
|
|
bless({}, $class);
|
2002-07-06 09:18:48 +02:00
|
|
|
}
|
|
|
|
|
2012-05-09 00:04:10 +02:00
|
|
|
sub hook_name {
|
2013-04-21 06:08:43 +02:00
|
|
|
return shift->{_hook};
|
2006-06-28 22:05:04 +02:00
|
|
|
}
|
|
|
|
|
2002-07-08 04:30:11 +02:00
|
|
|
sub register_hook {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($plugin, $hook, $method, $unshift) = @_;
|
|
|
|
|
|
|
|
die $plugin->plugin_name . " : Invalid hook: $hook" unless $hooks{$hook};
|
|
|
|
|
|
|
|
$plugin->{_qp}->log(LOGDEBUG, $plugin->plugin_name, "hooking", $hook)
|
|
|
|
unless $hook =~ /logging/; # can't log during load_logging()
|
|
|
|
|
|
|
|
# I can't quite decide if it's better to parse this code ref or if
|
|
|
|
# we should pass the plugin object and method name ... hmn.
|
|
|
|
$plugin->qp->_register_hook(
|
|
|
|
$hook,
|
|
|
|
{
|
|
|
|
code => sub {
|
|
|
|
local $plugin->{_qp} = shift;
|
|
|
|
local $plugin->{_hook} = $hook;
|
|
|
|
$plugin->$method(@_);
|
|
|
|
},
|
|
|
|
name => $plugin->plugin_name,
|
|
|
|
},
|
|
|
|
$unshift,
|
|
|
|
);
|
2002-07-08 04:30:11 +02:00
|
|
|
}
|
2002-07-06 09:18:48 +02:00
|
|
|
|
2004-08-31 03:58:57 +02:00
|
|
|
sub _register {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
|
|
|
my $qp = shift;
|
|
|
|
local $self->{_qp} = $qp;
|
|
|
|
$self->init($qp, @_) if $self->can('init');
|
|
|
|
$self->_register_standard_hooks($qp, @_);
|
|
|
|
$self->register($qp, @_) if $self->can('register');
|
2004-08-31 03:58:57 +02:00
|
|
|
}
|
|
|
|
|
2002-07-08 04:30:11 +02:00
|
|
|
sub qp {
|
2013-04-21 06:08:43 +02:00
|
|
|
shift->{_qp};
|
2002-07-08 04:30:11 +02:00
|
|
|
}
|
2002-07-06 09:18:48 +02:00
|
|
|
|
2002-07-08 04:30:11 +02:00
|
|
|
sub log {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
|
|
|
return if defined $self->{_hook} && $self->{_hook} eq 'logging';
|
|
|
|
my $level = $self->adjust_log_level(shift, $self->plugin_name);
|
|
|
|
$self->{_qp}->varlog($level, $self->{_hook}, $self->plugin_name, @_);
|
2002-07-06 09:18:48 +02:00
|
|
|
}
|
|
|
|
|
2012-05-21 11:34:37 +02:00
|
|
|
sub adjust_log_level {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $cur_level, $plugin_name) = @_;
|
2012-05-21 11:34:37 +02:00
|
|
|
|
|
|
|
my $adj = $self->{_args}{loglevel} or return $cur_level;
|
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
return $adj if $adj =~ m/^[01234567]$/; # a raw syslog numeral
|
2012-05-21 11:34:37 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
if ($adj !~ /^[\+\-][\d]$/) {
|
|
|
|
$self->log(LOGERROR,
|
|
|
|
$self - "invalid $plugin_name loglevel setting ($adj)");
|
|
|
|
undef $self->{_args}{loglevel}; # only complain once per plugin
|
2012-05-21 11:34:37 +02:00
|
|
|
return $cur_level;
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
2012-05-21 11:34:37 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
my $operator = substr($adj, 0, 1);
|
|
|
|
my $adjust = substr($adj, -1, 1);
|
2012-05-21 11:34:37 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
my $new_level =
|
|
|
|
$operator eq '+' ? $cur_level + $adjust : $cur_level - $adjust;
|
2012-05-21 11:34:37 +02:00
|
|
|
|
|
|
|
$new_level = 7 if $new_level > 7;
|
|
|
|
$new_level = 0 if $new_level < 0;
|
|
|
|
|
|
|
|
return $new_level;
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
2012-05-21 11:34:37 +02:00
|
|
|
|
2002-07-08 04:30:11 +02:00
|
|
|
sub transaction {
|
2013-04-21 06:08:43 +02:00
|
|
|
|
|
|
|
# not sure if this will work in a non-forking or a threaded daemon
|
|
|
|
shift->qp->transaction;
|
2002-07-08 04:30:11 +02:00
|
|
|
}
|
2002-07-06 09:18:48 +02:00
|
|
|
|
2004-08-31 03:58:57 +02:00
|
|
|
sub connection {
|
2013-04-21 06:08:43 +02:00
|
|
|
shift->qp->connection;
|
2004-08-31 03:58:57 +02:00
|
|
|
}
|
|
|
|
|
2005-02-22 03:47:39 +01:00
|
|
|
sub spool_dir {
|
2013-04-21 06:08:43 +02:00
|
|
|
shift->qp->spool_dir;
|
2005-02-22 03:47:39 +01:00
|
|
|
}
|
|
|
|
|
2005-10-31 18:12:37 +01:00
|
|
|
sub auth_user {
|
|
|
|
shift->qp->auth_user;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub auth_mechanism {
|
|
|
|
shift->qp->auth_mechanism;
|
|
|
|
}
|
|
|
|
|
2005-02-22 03:47:39 +01:00
|
|
|
sub temp_file {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
|
|
|
my $tempfile = $self->qp->temp_file;
|
|
|
|
push @{$self->qp->transaction->{_temp_files}}, $tempfile;
|
|
|
|
return $tempfile;
|
2005-02-22 03:47:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub temp_dir {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
|
|
|
my $tempdir = $self->qp->temp_dir();
|
|
|
|
push @{$self->qp->transaction->{_temp_dirs}}, $tempdir;
|
|
|
|
return $tempdir;
|
2005-02-22 03:47:39 +01:00
|
|
|
}
|
|
|
|
|
2004-09-08 07:14:10 +02:00
|
|
|
# plugin inheritance:
|
|
|
|
# usage:
|
2005-07-07 06:17:39 +02:00
|
|
|
# sub init {
|
2004-09-08 07:14:10 +02:00
|
|
|
# my $self = shift;
|
|
|
|
# $self->isa_plugin("rhsbl");
|
|
|
|
# $self->SUPER::register(@_);
|
|
|
|
# }
|
|
|
|
sub isa_plugin {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $parent) = @_;
|
|
|
|
my ($currentPackage) = caller;
|
|
|
|
|
|
|
|
my $cleanParent = $parent;
|
|
|
|
$cleanParent =~ s/\W/_/g;
|
|
|
|
my $newPackage = $currentPackage . "::_isa_$cleanParent";
|
|
|
|
|
|
|
|
# don't reload plugins if they are already loaded
|
|
|
|
return if defined &{"${newPackage}::plugin_name"};
|
|
|
|
|
|
|
|
# find $parent in plugin_dirs
|
|
|
|
my $parent_dir;
|
|
|
|
for ($self->qp->plugin_dirs) {
|
|
|
|
if (-e "$_/$parent") {
|
|
|
|
$parent_dir = $_;
|
|
|
|
last;
|
|
|
|
}
|
2007-08-31 07:26:04 +02:00
|
|
|
}
|
2013-04-21 06:08:43 +02:00
|
|
|
die "cannot find plugin '$parent'" unless $parent_dir;
|
|
|
|
|
|
|
|
$self->compile($self->plugin_name . "_isa_$cleanParent",
|
|
|
|
$newPackage, "$parent_dir/$parent");
|
|
|
|
warn "---- $newPackage\n";
|
|
|
|
no strict 'refs';
|
|
|
|
push @{"${currentPackage}::ISA"}, $newPackage;
|
2004-09-07 07:07:20 +02:00
|
|
|
}
|
|
|
|
|
2005-07-07 06:17:39 +02:00
|
|
|
# why isn't compile private? it's only called from Plugin and Qpsmtpd.
|
2004-11-18 20:47:10 +01:00
|
|
|
sub compile {
|
2007-05-18 00:02:32 +02:00
|
|
|
my ($class, $plugin, $package, $file, $test_mode, $orig_name) = @_;
|
2012-05-09 00:04:10 +02:00
|
|
|
|
2004-11-18 20:47:10 +01:00
|
|
|
my $sub;
|
|
|
|
open F, $file or die "could not open $file: $!";
|
2012-05-09 00:04:10 +02:00
|
|
|
{
|
2013-04-21 06:08:43 +02:00
|
|
|
local $/ = undef;
|
|
|
|
$sub = <F>;
|
2004-11-18 20:47:10 +01:00
|
|
|
}
|
|
|
|
close F;
|
|
|
|
|
2005-05-05 09:44:34 +02:00
|
|
|
my $line = "\n#line 0 $file\n";
|
2004-11-18 20:47:10 +01:00
|
|
|
|
|
|
|
if ($test_mode) {
|
2007-05-18 00:02:32 +02:00
|
|
|
if (open(F, "t/plugin_tests/$orig_name")) {
|
2004-11-18 20:47:10 +01:00
|
|
|
local $/ = undef;
|
2007-05-18 00:02:32 +02:00
|
|
|
$sub .= "#line 1 t/plugin_tests/$orig_name\n";
|
2004-11-18 20:47:10 +01:00
|
|
|
$sub .= <F>;
|
|
|
|
close F;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my $eval = join(
|
2013-04-21 06:08:43 +02:00
|
|
|
"\n",
|
|
|
|
"package $package;",
|
|
|
|
'use Qpsmtpd::Constants;',
|
|
|
|
"require Qpsmtpd::Plugin;",
|
|
|
|
'use vars qw(@ISA);',
|
|
|
|
'use strict;',
|
|
|
|
'@ISA = qw(Qpsmtpd::Plugin);',
|
|
|
|
($test_mode ? 'use Test::More;' : ''),
|
|
|
|
"sub plugin_name { qq[$plugin] }",
|
|
|
|
$line,
|
|
|
|
$sub,
|
|
|
|
"\n", # last line comment without newline?
|
|
|
|
);
|
2004-11-18 20:47:10 +01:00
|
|
|
|
|
|
|
#warn "eval: $eval";
|
|
|
|
|
|
|
|
$eval =~ m/(.*)/s;
|
|
|
|
$eval = $1;
|
|
|
|
|
|
|
|
eval $eval;
|
|
|
|
die "eval $@" if $@;
|
|
|
|
}
|
|
|
|
|
2012-06-22 11:38:01 +02:00
|
|
|
sub get_reject {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
2012-06-23 00:14:44 +02:00
|
|
|
my $smtp_mess = shift || "why didn't you pass an error message?";
|
2013-04-21 06:08:43 +02:00
|
|
|
my $log_mess = shift || '';
|
2012-06-23 00:14:44 +02:00
|
|
|
$log_mess = ", $log_mess" if $log_mess;
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
my $reject = $self->{_args}{reject};
|
2013-04-21 06:08:43 +02:00
|
|
|
if (defined $reject && !$reject) {
|
2013-04-24 22:25:31 +02:00
|
|
|
$self->log(LOGINFO, "fail, tolerated" . $log_mess);
|
2012-06-22 11:38:01 +02:00
|
|
|
return DECLINED;
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
# the naughty plugin will reject later
|
2013-04-21 06:08:43 +02:00
|
|
|
if ($reject eq 'naughty') {
|
2012-06-23 00:14:44 +02:00
|
|
|
$self->log(LOGINFO, "fail, NAUGHTY" . $log_mess);
|
2013-04-21 06:08:43 +02:00
|
|
|
return $self->store_deferred_reject($smtp_mess);
|
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
# they asked for reject, we give them reject
|
2012-06-23 00:14:44 +02:00
|
|
|
$self->log(LOGINFO, "fail" . $log_mess);
|
2013-04-21 06:08:43 +02:00
|
|
|
return ($self->get_reject_type(), $smtp_mess);
|
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
|
|
|
sub get_reject_type {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
2012-06-22 11:38:01 +02:00
|
|
|
my $default = shift || DENY;
|
2013-04-21 06:08:43 +02:00
|
|
|
my $deny = shift || $self->{_args}{reject_type} or return $default;
|
|
|
|
|
|
|
|
return
|
|
|
|
$deny =~ /^(temp|soft)$/i ? DENYSOFT
|
|
|
|
: $deny =~ /^(perm|hard)$/i ? DENY
|
|
|
|
: $deny eq 'disconnect' ? DENY_DISCONNECT
|
|
|
|
: $deny eq 'temp_disconnect' ? DENYSOFT_DISCONNECT
|
|
|
|
: $default;
|
|
|
|
}
|
2012-06-22 11:38:01 +02:00
|
|
|
|
2013-03-27 23:16:43 +01:00
|
|
|
sub store_deferred_reject {
|
|
|
|
my ($self, $smtp_mess) = @_;
|
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
# store the reject message that the naughty plugin will return later
|
|
|
|
if (!$self->connection->notes('naughty')) {
|
2013-03-27 23:16:43 +01:00
|
|
|
$self->connection->notes('naughty', $smtp_mess);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
# append this reject message to the message
|
|
|
|
my $prev = $self->connection->notes('naughty');
|
|
|
|
$self->connection->notes('naughty', "$prev\015\012$smtp_mess");
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
|
|
|
if (!$self->connection->notes('naughty_reject_type')) {
|
|
|
|
$self->connection->notes('naughty_reject_type',
|
|
|
|
$self->{_args}{reject_type});
|
2013-03-27 23:16:43 +01:00
|
|
|
}
|
|
|
|
return (DECLINED);
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
2013-03-27 23:16:43 +01:00
|
|
|
|
2013-03-28 00:03:13 +01:00
|
|
|
sub init_resolver {
|
|
|
|
my $self = shift;
|
2013-04-21 18:14:27 +02:00
|
|
|
my $timeout = $self->{_args}{dns_timeout} || shift || 5;
|
2013-03-28 00:03:13 +01:00
|
|
|
return $self->{_resolver} if $self->{_resolver};
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->log(LOGDEBUG, "initializing Net::DNS::Resolver");
|
2013-03-28 00:03:13 +01:00
|
|
|
$self->{_resolver} = Net::DNS::Resolver->new(dnsrch => 0);
|
|
|
|
$self->{_resolver}->tcp_timeout($timeout);
|
|
|
|
$self->{_resolver}->udp_timeout($timeout);
|
|
|
|
return $self->{_resolver};
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
2013-03-28 00:03:13 +01:00
|
|
|
|
2012-06-02 20:46:29 +02:00
|
|
|
sub is_immune {
|
|
|
|
my $self = shift;
|
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
if ($self->qp->connection->relay_client()) {
|
|
|
|
|
2012-06-02 20:46:29 +02:00
|
|
|
# set by plugins/relay, or Qpsmtpd::Auth
|
|
|
|
$self->log(LOGINFO, "skip, relay client");
|
|
|
|
return 1;
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
|
|
|
if ($self->qp->connection->notes('whitelisthost')) {
|
|
|
|
|
2012-06-02 20:46:29 +02:00
|
|
|
# set by plugins/dns_whitelist_soft or plugins/whitelist
|
|
|
|
$self->log(LOGINFO, "skip, whitelisted host");
|
|
|
|
return 1;
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
|
|
|
if ($self->qp->transaction->notes('whitelistsender')) {
|
|
|
|
|
2012-06-02 20:46:29 +02:00
|
|
|
# set by plugins/whitelist
|
|
|
|
$self->log(LOGINFO, "skip, whitelisted sender");
|
|
|
|
return 1;
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
2013-04-24 06:23:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub is_naughty {
|
|
|
|
my $self = shift;
|
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
if ($self->connection->notes('naughty')) {
|
|
|
|
|
2012-06-03 23:12:17 +02:00
|
|
|
# see plugins/naughty
|
|
|
|
$self->log(LOGINFO, "skip, naughty");
|
2012-06-02 20:46:29 +02:00
|
|
|
return 1;
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
|
|
|
if ($self->connection->notes('rejected')) {
|
|
|
|
|
2012-06-02 20:46:29 +02:00
|
|
|
# http://www.steve.org.uk/Software/ms-lite/
|
|
|
|
$self->log(LOGINFO, "skip, already rejected");
|
|
|
|
return 1;
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
2012-06-02 20:46:29 +02:00
|
|
|
return;
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
2012-06-02 20:46:29 +02:00
|
|
|
|
2012-06-30 21:37:25 +02:00
|
|
|
sub adjust_karma {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $value) = @_;
|
2012-06-30 21:37:25 +02:00
|
|
|
|
2012-06-30 22:28:54 +02:00
|
|
|
my $karma = $self->connection->notes('karma') || 0;
|
2012-06-30 21:37:25 +02:00
|
|
|
$karma += $value;
|
2013-04-24 06:23:24 +02:00
|
|
|
$self->log(LOGDEBUG, "karma $value ($karma)");
|
2013-03-23 05:51:00 +01:00
|
|
|
$self->connection->notes('karma', $karma);
|
2012-06-30 21:37:25 +02:00
|
|
|
return $value;
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
2012-06-30 21:37:25 +02:00
|
|
|
|
2005-07-07 06:17:39 +02:00
|
|
|
sub _register_standard_hooks {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($plugin, $qp) = @_;
|
2005-07-07 06:17:39 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
for my $hook (@hooks) {
|
|
|
|
my $hooksub = "hook_$hook";
|
|
|
|
$hooksub =~ s/\W/_/g;
|
|
|
|
$plugin->register_hook($hook, $hooksub)
|
|
|
|
if ($plugin->can($hooksub));
|
|
|
|
}
|
|
|
|
}
|
2005-07-07 06:17:39 +02:00
|
|
|
|
2002-07-06 09:18:48 +02:00
|
|
|
1;
|