2002-07-03 15:10:44 +02:00
|
|
|
package Qpsmtpd;
|
|
|
|
use strict;
|
2014-09-09 07:58:27 +02:00
|
|
|
#use warnings;
|
2014-09-16 01:58:21 +02:00
|
|
|
|
2014-09-17 09:02:23 +02:00
|
|
|
our $VERSION = '0.95';
|
2014-09-09 07:58:27 +02:00
|
|
|
use vars qw($TraceLevel $Spool_dir $Size_threshold);
|
2002-07-03 15:10:44 +02:00
|
|
|
|
2014-09-16 01:58:21 +02:00
|
|
|
use lib 'lib';
|
2014-09-17 18:11:47 +02:00
|
|
|
use parent 'Qpsmtpd::Base';
|
2014-09-16 01:58:21 +02:00
|
|
|
use Qpsmtpd::Address;
|
2014-09-16 08:41:31 +02:00
|
|
|
use Qpsmtpd::Config;
|
2014-09-16 01:58:21 +02:00
|
|
|
use Qpsmtpd::Constants;
|
2007-11-16 15:51:01 +01:00
|
|
|
|
2014-09-09 07:58:27 +02:00
|
|
|
our $hooks = {};
|
|
|
|
our $LOGGING_LOADED = 0;
|
2014-09-17 09:02:23 +02:00
|
|
|
my $git = git_version();
|
2008-06-15 11:28:02 +02:00
|
|
|
|
2008-12-31 22:46:40 +01:00
|
|
|
sub _restart {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
|
|
|
my %args = @_;
|
|
|
|
if ($args{restart}) {
|
|
|
|
|
|
|
|
# reset all global vars to defaults
|
2014-09-16 08:41:31 +02:00
|
|
|
$self->conf->clear_cache();
|
2014-09-16 18:52:05 +02:00
|
|
|
$hooks = {};
|
|
|
|
$LOGGING_LOADED = 0;
|
|
|
|
$TraceLevel = LOGWARN;
|
|
|
|
$Spool_dir = undef;
|
|
|
|
$Size_threshold = undef;
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
2008-06-15 11:28:02 +02:00
|
|
|
}
|
|
|
|
|
2014-09-17 02:54:26 +02:00
|
|
|
sub version { $VERSION . ($git ? "/$git" : '') }
|
2005-03-24 22:16:35 +01:00
|
|
|
|
2014-09-17 09:02:23 +02:00
|
|
|
sub git_version {
|
|
|
|
return if !-e '.git';
|
|
|
|
{
|
|
|
|
local $ENV{PATH} = "/usr/bin:/usr/local/bin:/opt/local/bin/";
|
|
|
|
$git = `git describe --tags`;
|
|
|
|
$git && chomp $git;
|
|
|
|
}
|
|
|
|
return $git;
|
|
|
|
}
|
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
sub TRACE_LEVEL { $TraceLevel }; # leave for plugin compatibility
|
2008-02-01 21:59:18 +01:00
|
|
|
|
2014-09-17 06:22:36 +02:00
|
|
|
sub hooks {
|
|
|
|
my ($self, $hook) = @_;
|
|
|
|
if ($hook) {
|
|
|
|
if (!defined $hooks->{$hook}) { return wantarray ? () : []; };
|
|
|
|
return wantarray ? @{$hooks->{$hook}} : $hooks->{$hook};
|
|
|
|
};
|
|
|
|
return $hooks;
|
|
|
|
}
|
2008-04-08 12:34:25 +02:00
|
|
|
|
2005-03-24 22:16:35 +01:00
|
|
|
sub load_logging {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
2014-09-09 07:58:27 +02:00
|
|
|
|
2014-09-17 18:11:47 +02:00
|
|
|
return if $LOGGING_LOADED; # already done
|
|
|
|
return if $hooks->{'logging'}; # avoid triggering log activity
|
2014-09-09 07:58:27 +02:00
|
|
|
|
2014-09-17 18:11:47 +02:00
|
|
|
my @plugin_dirs = $self->conf->from_file('plugin_dirs');
|
|
|
|
if (!@plugin_dirs) {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($name) = ($0 =~ m!(.*?)/([^/]+)$!);
|
|
|
|
@plugin_dirs = ("$name/plugins");
|
|
|
|
}
|
|
|
|
|
2014-09-17 18:11:47 +02:00
|
|
|
my @loggers = $self->conf->from_file('logging');
|
2013-04-21 06:08:43 +02:00
|
|
|
for my $logger (@loggers) {
|
2014-09-17 18:11:47 +02:00
|
|
|
$self->_load_plugin($logger, @plugin_dirs);
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->log(LOGINFO, "Loaded $logger");
|
|
|
|
}
|
|
|
|
|
2014-09-17 18:11:47 +02:00
|
|
|
$TraceLevel = $self->conf->from_file('loglevel');
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2014-09-17 18:11:47 +02:00
|
|
|
unless (defined($TraceLevel) && $TraceLevel =~ /^\d+$/) {
|
2013-04-21 06:08:43 +02:00
|
|
|
$TraceLevel = LOGWARN; # Default if no loglevel file found.
|
|
|
|
}
|
|
|
|
|
|
|
|
$LOGGING_LOADED = 1;
|
|
|
|
|
|
|
|
return @loggers;
|
2005-03-24 22:16:35 +01:00
|
|
|
}
|
2008-12-31 22:46:40 +01:00
|
|
|
|
2014-09-09 07:58:27 +02:00
|
|
|
sub trace_level { return $TraceLevel; }
|
2004-03-05 13:46:24 +01:00
|
|
|
|
2014-09-16 08:41:31 +02:00
|
|
|
sub init_logger { # needed for compatibility
|
2013-04-21 06:08:43 +02:00
|
|
|
shift->trace_level();
|
2005-05-25 22:07:58 +02:00
|
|
|
}
|
|
|
|
|
2002-09-24 12:56:35 +02:00
|
|
|
sub log {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $trace, @log) = @_;
|
|
|
|
$self->varlog($trace, join(" ", @log));
|
2005-03-24 22:16:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub varlog {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $trace) = (shift, shift);
|
|
|
|
my ($hook, $plugin, @log);
|
2014-09-16 08:41:31 +02:00
|
|
|
if ($#_ == 0) { (@log) = @_; } # log itself
|
|
|
|
elsif ($#_ == 1) { ($hook, @log) = @_; } # plus the hook
|
|
|
|
else { ($hook, $plugin, @log) = @_; } # from a plugin
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2014-09-16 08:41:31 +02:00
|
|
|
$self->load_logging;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
|
|
|
my ($rc) =
|
|
|
|
$self->run_hooks_no_respond("logging", $trace, $hook, $plugin, @log)
|
|
|
|
or return;
|
|
|
|
|
2014-09-16 18:52:05 +02:00
|
|
|
return if $rc == DECLINED || $rc == OK; # plugin success
|
2012-05-04 20:12:46 +02:00
|
|
|
return if $trace > $TraceLevel;
|
|
|
|
|
|
|
|
# no logging plugins registered, fall back to STDERR
|
2013-04-21 06:08:43 +02:00
|
|
|
my $prefix =
|
|
|
|
defined $plugin && defined $hook ? " ($hook) $plugin:"
|
|
|
|
: defined $plugin ? " $plugin:"
|
|
|
|
: defined $hook ? " ($hook) running plugin:"
|
|
|
|
: '';
|
2012-05-04 20:12:46 +02:00
|
|
|
|
|
|
|
warn join(' ', $$ . $prefix, @log), "\n";
|
2002-07-03 15:10:44 +02:00
|
|
|
}
|
|
|
|
|
2014-09-16 08:41:31 +02:00
|
|
|
sub conf {
|
|
|
|
my $self = shift;
|
|
|
|
if (!$self->{_config}) {
|
|
|
|
$self->{_config} = Qpsmtpd::Config->new();
|
2014-09-16 18:52:05 +02:00
|
|
|
}
|
2014-09-16 08:41:31 +02:00
|
|
|
return $self->{_config};
|
2007-12-03 18:44:14 +01:00
|
|
|
}
|
|
|
|
|
2002-07-03 15:10:44 +02:00
|
|
|
sub config {
|
2014-09-16 18:26:55 +02:00
|
|
|
my $self = shift;
|
|
|
|
return $self->conf->config($self, @_);
|
2014-09-16 18:52:05 +02:00
|
|
|
}
|
2002-07-03 15:10:44 +02:00
|
|
|
|
2004-09-08 18:26:33 +02:00
|
|
|
sub config_dir {
|
2014-09-16 08:41:31 +02:00
|
|
|
my $self = shift;
|
|
|
|
return $self->conf->config_dir(@_);
|
2004-09-08 18:26:33 +02:00
|
|
|
}
|
|
|
|
|
2006-11-05 10:54:03 +01:00
|
|
|
sub plugin_dirs {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
2006-11-05 10:54:03 +01:00
|
|
|
my @plugin_dirs = $self->config('plugin_dirs');
|
2008-12-31 22:46:40 +01:00
|
|
|
|
2006-11-05 10:54:03 +01:00
|
|
|
unless (@plugin_dirs) {
|
2006-11-22 17:30:37 +01:00
|
|
|
my ($path) = ($ENV{PROCESS} ? $ENV{PROCESS} : $0) =~ m!(.*?)/([^/]+)$!;
|
2013-04-21 06:08:43 +02:00
|
|
|
@plugin_dirs = ("$path/plugins");
|
2006-11-05 10:54:03 +01:00
|
|
|
}
|
|
|
|
return @plugin_dirs;
|
2004-09-08 18:26:33 +02:00
|
|
|
}
|
2002-07-08 04:30:11 +02:00
|
|
|
|
2002-07-06 09:16:23 +02:00
|
|
|
sub load_plugins {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
2008-12-31 22:46:40 +01:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
my @plugins = $self->config('plugins');
|
|
|
|
my @loaded;
|
2002-07-06 09:16:23 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
if ($hooks->{queue}) {
|
2008-01-30 19:35:32 +01:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
#$self->log(LOGWARN, "Plugins already loaded");
|
|
|
|
return @plugins;
|
|
|
|
}
|
|
|
|
|
|
|
|
for my $plugin_line (@plugins) {
|
|
|
|
my $this_plugin = $self->_load_plugin($plugin_line, $self->plugin_dirs);
|
|
|
|
push @loaded, $this_plugin if $this_plugin;
|
|
|
|
}
|
2004-03-05 13:46:24 +01:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
return @loaded;
|
2004-03-05 13:46:24 +01:00
|
|
|
}
|
2002-07-06 09:16:23 +02:00
|
|
|
|
2006-11-05 10:54:03 +01:00
|
|
|
sub _load_plugin {
|
2014-09-17 07:32:43 +02:00
|
|
|
my ($self, $plugin_line, @plugin_dirs) = @_;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2013-04-26 00:44:21 +02:00
|
|
|
# untaint the config data before passing it to plugins
|
2014-09-16 00:57:12 +02:00
|
|
|
my ($safe_line) = $plugin_line =~ /^([ -~]+)$/ # all ascii printable
|
|
|
|
or die "unsafe characters in config line: $plugin_line\n";
|
2013-04-26 00:44:21 +02:00
|
|
|
my ($plugin, @args) = split /\s+/, $safe_line;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
|
|
|
if ($plugin =~ m/::/) {
|
2013-04-26 00:44:21 +02:00
|
|
|
return $self->_load_package_plugin($plugin, $safe_line, \@args);
|
2014-09-16 00:57:12 +02:00
|
|
|
}
|
2013-04-26 00:44:21 +02:00
|
|
|
|
|
|
|
# regular plugins/$plugin plugin
|
|
|
|
my $plugin_name = $plugin;
|
|
|
|
$plugin =~ s/:\d+$//; # after this point, only used for filename
|
|
|
|
|
|
|
|
# Escape everything into valid perl identifiers
|
|
|
|
$plugin_name =~ s/([^A-Za-z0-9_\/])/sprintf("_%2x",unpack("C",$1))/eg;
|
|
|
|
|
|
|
|
# second pass cares for slashes and words starting with a digit
|
|
|
|
$plugin_name =~ s{
|
|
|
|
(/+) # directory
|
|
|
|
(\d?) # package's first character
|
|
|
|
}[
|
2014-09-17 02:54:26 +02:00
|
|
|
"::" . (length $2 ? sprintf("_%2x",unpack("C",$2)) : '')
|
2013-04-26 00:44:21 +02:00
|
|
|
]egx;
|
|
|
|
|
|
|
|
my $package = "Qpsmtpd::Plugin::$plugin_name";
|
|
|
|
|
|
|
|
# don't reload plugins if they are already loaded
|
2014-09-16 00:57:12 +02:00
|
|
|
if (!defined &{"${package}::plugin_name"}) {
|
2014-09-16 04:00:07 +02:00
|
|
|
for my $dir (@plugin_dirs) {
|
2014-09-16 00:57:12 +02:00
|
|
|
next if !-e "$dir/$plugin";
|
|
|
|
Qpsmtpd::Plugin->compile($plugin_name, $package,
|
2014-09-16 18:52:05 +02:00
|
|
|
"$dir/$plugin", $self->{_test_mode},
|
|
|
|
$plugin);
|
2014-09-16 00:57:12 +02:00
|
|
|
if ($safe_line !~ /logging/) {
|
|
|
|
$self->log(LOGDEBUG, "Loading $safe_line from $dir/$plugin");
|
2014-09-16 18:52:05 +02:00
|
|
|
}
|
2014-09-16 04:00:07 +02:00
|
|
|
last;
|
2006-11-05 10:54:03 +01:00
|
|
|
}
|
2014-09-16 18:52:05 +02:00
|
|
|
if (!defined &{"${package}::plugin_name"}) {
|
|
|
|
die "Plugin $plugin_name not found in our plugin dirs (",
|
|
|
|
join(', ', @plugin_dirs), ")";
|
|
|
|
}
|
2005-03-24 22:16:35 +01:00
|
|
|
}
|
2006-11-05 10:54:03 +01:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
my $plug = $package->new();
|
|
|
|
$plug->_register($self, @args);
|
2008-12-31 22:46:40 +01:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
return $plug;
|
2002-07-08 04:30:11 +02:00
|
|
|
}
|
2002-07-06 09:16:23 +02:00
|
|
|
|
2013-04-26 00:44:21 +02:00
|
|
|
sub _load_package_plugin {
|
|
|
|
my ($self, $plugin, $plugin_line, $args) = @_;
|
2014-09-16 00:57:12 +02:00
|
|
|
|
2013-04-26 00:44:21 +02:00
|
|
|
# "full" package plugin (My::Plugin)
|
|
|
|
my $package = $plugin;
|
|
|
|
$package =~ s/[^_a-z0-9:]+//gi;
|
2014-09-16 00:57:12 +02:00
|
|
|
my $eval =
|
|
|
|
qq[require $package;\n] . qq[sub ${plugin}::plugin_name { '$plugin' }];
|
2013-04-26 00:44:21 +02:00
|
|
|
$eval =~ m/(.*)/s;
|
|
|
|
$eval = $1;
|
2014-09-16 18:52:05 +02:00
|
|
|
eval $eval; ## no critic (Eval)
|
2013-04-26 00:44:21 +02:00
|
|
|
die "Failed loading $package - eval $@" if $@;
|
2014-09-16 18:52:05 +02:00
|
|
|
|
2014-09-16 00:57:12 +02:00
|
|
|
if ($plugin_line !~ /logging/) {
|
|
|
|
$self->log(LOGDEBUG, "Loading $package ($plugin_line)");
|
2014-09-16 18:52:05 +02:00
|
|
|
}
|
2013-04-26 00:44:21 +02:00
|
|
|
|
|
|
|
my $plug = $package->new();
|
|
|
|
$plug->_register($self, @$args);
|
|
|
|
|
|
|
|
return $plug;
|
2014-09-16 00:57:12 +02:00
|
|
|
}
|
2013-04-26 00:44:21 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
sub transaction { return {}; } # base class implements empty transaction
|
2005-07-07 22:10:03 +02:00
|
|
|
|
2002-07-08 04:30:11 +02:00
|
|
|
sub run_hooks {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $hook) = (shift, shift);
|
2014-09-17 06:22:36 +02:00
|
|
|
if (my @local_hooks = $self->hooks($hook)) {
|
2013-04-21 06:08:43 +02:00
|
|
|
$self->{_continuation} = [$hook, [@_], @local_hooks];
|
|
|
|
return $self->run_continuation();
|
|
|
|
}
|
|
|
|
return $self->hook_responder($hook, [0, ''], [@_]);
|
2006-11-30 23:10:55 +01:00
|
|
|
}
|
|
|
|
|
2008-01-30 19:35:32 +01:00
|
|
|
sub run_hooks_no_respond {
|
|
|
|
my ($self, $hook) = (shift, shift);
|
2014-09-18 03:28:51 +02:00
|
|
|
if (!$hooks->{$hook}) {
|
|
|
|
return 0,'';
|
|
|
|
}
|
2014-09-17 05:30:36 +02:00
|
|
|
|
|
|
|
my @r;
|
|
|
|
for my $code (@{$hooks->{$hook}}) {
|
2014-09-17 07:32:43 +02:00
|
|
|
eval { @r = $code->{code}->($self, $self->transaction, @_); };
|
2014-09-17 05:30:36 +02:00
|
|
|
if ($@) {
|
|
|
|
warn("FATAL PLUGIN ERROR [" . $code->{name} . "]: ", $@);
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
last if $r[0] != DECLINED;
|
2008-01-30 19:35:32 +01:00
|
|
|
}
|
2014-09-17 05:30:36 +02:00
|
|
|
$r[0] = DECLINED if not defined $r[0];
|
|
|
|
return @r;
|
2008-01-30 19:35:32 +01:00
|
|
|
}
|
|
|
|
|
2006-11-30 23:10:55 +01:00
|
|
|
sub run_continuation {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
2008-12-31 22:46:40 +01:00
|
|
|
|
2014-09-17 05:30:36 +02:00
|
|
|
die "No continuation in progress\n" if !$self->{_continuation};
|
2013-04-21 06:08:43 +02:00
|
|
|
my $todo = $self->{_continuation};
|
|
|
|
$self->{_continuation} = undef;
|
2014-09-17 02:54:26 +02:00
|
|
|
my $hook = shift @$todo or die "No hook in the continuation";
|
|
|
|
my $args = shift @$todo or die "No hook args in the continuation";
|
2013-04-21 06:08:43 +02:00
|
|
|
my @r;
|
|
|
|
|
|
|
|
while (@$todo) {
|
|
|
|
my $code = shift @$todo;
|
2014-09-17 02:54:26 +02:00
|
|
|
my $name = $code->{name};
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2014-09-17 02:54:26 +02:00
|
|
|
$self->varlog(LOGDEBUG, $hook, $name);
|
2013-04-21 06:08:43 +02:00
|
|
|
my $tran = $self->transaction;
|
2014-09-17 05:30:36 +02:00
|
|
|
eval { @r = $code->{code}->($self, $tran, @$args); };
|
2014-09-16 04:00:07 +02:00
|
|
|
if ($@) {
|
2015-02-03 23:18:45 +01:00
|
|
|
chomp $@;
|
2014-09-17 02:54:26 +02:00
|
|
|
$self->log(LOGCRIT, "FATAL PLUGIN ERROR [$name]: ", $@);
|
2014-09-16 04:00:07 +02:00
|
|
|
next;
|
|
|
|
}
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2014-09-17 02:54:26 +02:00
|
|
|
my $log_msg = "Plugin $name, hook $hook returned ";
|
|
|
|
if (!defined $r[0]) {
|
|
|
|
$self->log(LOGERROR, $log_msg . "undef!");
|
|
|
|
next;
|
|
|
|
}
|
2013-04-21 06:08:43 +02:00
|
|
|
|
|
|
|
if ($tran) {
|
2014-09-17 02:54:26 +02:00
|
|
|
my $tnotes = $tran->notes($name);
|
|
|
|
if (!defined $tnotes || ref $tnotes eq 'HASH') {
|
|
|
|
$tnotes->{"hook_$hook"}{return} = $r[0];
|
|
|
|
};
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-09-17 02:54:26 +02:00
|
|
|
my $cnotes = $self->connection->notes($name);
|
|
|
|
if (!defined $cnotes || ref $cnotes eq 'HASH') {
|
|
|
|
$cnotes->{"hook_$hook"}{return} = $r[0];
|
|
|
|
};
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
|
|
|
|
2014-09-17 20:38:40 +02:00
|
|
|
if ( $r[0] == DENY
|
2014-09-17 05:30:36 +02:00
|
|
|
|| $r[0] == DENYSOFT
|
|
|
|
|| $r[0] == DENY_DISCONNECT
|
|
|
|
|| $r[0] == DENYSOFT_DISCONNECT)
|
2013-04-21 06:08:43 +02:00
|
|
|
{
|
2014-09-17 02:54:26 +02:00
|
|
|
$r[1] = '' if !defined $r[1];
|
|
|
|
$self->log(LOGDEBUG, $log_msg . return_code($r[0]) . ", $r[1]");
|
|
|
|
if ($hook ne 'deny') {
|
2014-09-17 05:30:36 +02:00
|
|
|
$self->run_hooks_no_respond('deny', $name, $r[0], $r[1]);
|
2014-09-17 02:54:26 +02:00
|
|
|
};
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-09-17 02:54:26 +02:00
|
|
|
$r[1] = '' if not defined $r[1];
|
|
|
|
$self->log(LOGDEBUG, $log_msg . return_code($r[0]) . ", $r[1]");
|
2014-09-17 05:30:36 +02:00
|
|
|
$self->run_hooks_no_respond('ok', $name, $r[0], $r[1]) if $hook ne 'ok';
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
|
|
|
|
2014-09-17 02:54:26 +02:00
|
|
|
last if $r[0] != DECLINED;
|
2008-02-01 20:06:35 +01:00
|
|
|
}
|
2014-09-17 02:54:26 +02:00
|
|
|
$r[0] = DECLINED if ! defined $r[0];
|
2013-04-21 06:08:43 +02:00
|
|
|
|
|
|
|
# hook_*_parse() may return a CODE ref..
|
|
|
|
# ... which breaks when splitting as string:
|
2014-09-17 02:54:26 +02:00
|
|
|
if ('CODE' ne ref $r[1]) {
|
|
|
|
@r = map { split /\n/ } @r;
|
|
|
|
};
|
2013-04-21 06:08:43 +02:00
|
|
|
return $self->hook_responder($hook, \@r, $args);
|
2006-11-30 23:10:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub hook_responder {
|
2013-04-21 06:08:43 +02:00
|
|
|
my ($self, $hook, $msg, $args) = @_;
|
|
|
|
my $code = shift @$msg;
|
2008-12-31 22:46:40 +01:00
|
|
|
|
2014-09-16 23:13:44 +02:00
|
|
|
if (my $meth = $self->can($hook . '_respond')) {
|
2013-04-21 06:08:43 +02:00
|
|
|
return $meth->($self, $code, $msg, $args);
|
|
|
|
}
|
|
|
|
return $code, @$msg;
|
2002-07-08 04:30:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub _register_hook {
|
2014-09-16 03:50:48 +02:00
|
|
|
my ($self, $hook, $code, $unshift) = @_;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
|
|
|
if ($unshift) {
|
|
|
|
unshift @{$hooks->{$hook}}, $code;
|
2014-09-16 03:50:48 +02:00
|
|
|
return;
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
2014-09-16 03:50:48 +02:00
|
|
|
|
|
|
|
push @{$hooks->{$hook}}, $code;
|
2002-07-06 09:16:23 +02:00
|
|
|
}
|
|
|
|
|
2005-02-22 03:47:39 +01:00
|
|
|
sub spool_dir {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
|
|
|
|
2014-09-16 03:50:48 +02:00
|
|
|
return $Spool_dir if $Spool_dir; # already set
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2014-09-16 03:50:48 +02:00
|
|
|
$self->log(LOGDEBUG, "Initializing spool_dir");
|
|
|
|
$Spool_dir = $self->config('spool_dir') || $self->tildeexp('~/tmp/');
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2014-09-16 03:50:48 +02:00
|
|
|
$Spool_dir .= "/" if $Spool_dir !~ m!/$!;
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2014-09-16 03:50:48 +02:00
|
|
|
$Spool_dir =~ /^(.+)$/ or die "spool_dir not configured properly";
|
2014-09-16 18:52:05 +02:00
|
|
|
$Spool_dir = $1; # cleanse the taint
|
2013-04-21 06:08:43 +02:00
|
|
|
|
2014-09-16 03:50:48 +02:00
|
|
|
my $Spool_perms = $self->config('spool_perms') || '0700';
|
|
|
|
|
2014-09-16 18:52:05 +02:00
|
|
|
if (!-d $Spool_dir) { # create if it doesn't exist
|
2014-09-16 03:50:48 +02:00
|
|
|
mkdir($Spool_dir, oct($Spool_perms))
|
2014-09-16 18:52:05 +02:00
|
|
|
or die "Could not create spool_dir $Spool_dir: $!";
|
2013-04-21 06:08:43 +02:00
|
|
|
}
|
|
|
|
|
2014-09-16 03:50:48 +02:00
|
|
|
# Make sure the spool dir has appropriate rights
|
|
|
|
if (((stat $Spool_dir)[2] & oct('07777')) != oct($Spool_perms)) {
|
|
|
|
$self->log(LOGWARN,
|
2014-09-16 18:52:05 +02:00
|
|
|
"Permissions on spool_dir $Spool_dir are not $Spool_perms");
|
|
|
|
}
|
2014-09-16 03:50:48 +02:00
|
|
|
|
2013-04-21 06:08:43 +02:00
|
|
|
return $Spool_dir;
|
2005-02-22 03:47:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# For unique filenames. We write to a local tmp dir so we don't need
|
|
|
|
# to make them unpredictable.
|
2008-12-31 22:46:40 +01:00
|
|
|
my $transaction_counter = 0;
|
2005-02-22 03:47:39 +01:00
|
|
|
|
|
|
|
sub temp_file {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
|
|
|
my $filename =
|
|
|
|
$self->spool_dir() . join(":", time, $$, $transaction_counter++);
|
|
|
|
return $filename;
|
2008-12-31 22:46:40 +01:00
|
|
|
}
|
2005-02-22 03:47:39 +01:00
|
|
|
|
|
|
|
sub temp_dir {
|
2014-09-16 01:58:21 +02:00
|
|
|
my ($self, $mask) = @_;
|
2014-09-16 18:52:05 +02:00
|
|
|
$mask ||= '0700';
|
2013-04-21 06:08:43 +02:00
|
|
|
my $dirname = $self->temp_file();
|
2014-09-16 18:52:05 +02:00
|
|
|
if (!-d $dirname) {
|
|
|
|
mkdir($dirname, $mask)
|
|
|
|
or die "Could not create temporary directory $dirname: $!";
|
|
|
|
}
|
2013-04-21 06:08:43 +02:00
|
|
|
return $dirname;
|
2005-02-22 03:47:39 +01:00
|
|
|
}
|
|
|
|
|
2005-09-22 19:14:20 +02:00
|
|
|
sub size_threshold {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
2014-09-16 01:58:21 +02:00
|
|
|
return $Size_threshold if defined $Size_threshold;
|
|
|
|
|
|
|
|
$Size_threshold = $self->config('size_threshold') || 0;
|
|
|
|
$self->log(LOGDEBUG, "size_threshold set to $Size_threshold");
|
2013-04-21 06:08:43 +02:00
|
|
|
return $Size_threshold;
|
2005-09-22 19:14:20 +02:00
|
|
|
}
|
2005-10-31 18:12:37 +01:00
|
|
|
|
2007-02-07 22:49:45 +01:00
|
|
|
sub authenticated {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
2014-09-18 03:28:51 +02:00
|
|
|
return defined $self->{_auth} ? $self->{_auth} : '';
|
2007-02-07 22:49:45 +01:00
|
|
|
}
|
|
|
|
|
2005-10-31 18:12:37 +01:00
|
|
|
sub auth_user {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
2014-09-18 03:28:51 +02:00
|
|
|
return defined $self->{_auth_user} ? $self->{_auth_user} : '';
|
2005-10-31 18:12:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub auth_mechanism {
|
2013-04-21 06:08:43 +02:00
|
|
|
my $self = shift;
|
2014-09-18 03:28:51 +02:00
|
|
|
return defined $self->{_auth_mechanism} ? $self->{_auth_mechanism} : '';
|
2005-10-31 18:12:37 +01:00
|
|
|
}
|
2008-12-31 22:46:40 +01:00
|
|
|
|
2014-09-15 22:14:32 +02:00
|
|
|
sub address {
|
|
|
|
my $self = shift;
|
|
|
|
my $addr = Qpsmtpd::Address->new(@_);
|
2014-09-15 22:34:58 +02:00
|
|
|
$addr->qp($self);
|
2014-09-15 22:14:32 +02:00
|
|
|
return $addr;
|
|
|
|
}
|
|
|
|
|
2002-07-03 15:10:44 +02:00
|
|
|
1;
|
2005-07-02 04:08:37 +02:00
|
|
|
|
|
|
|
__END__
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
2010-01-12 09:43:51 +01:00
|
|
|
Qpsmtpd - base class for the qpsmtpd mail server
|
2005-07-02 04:08:37 +02:00
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
This is the base class for the qpsmtpd mail server. See
|
|
|
|
L<http://smtpd.develooper.com/> and the I<README> file for more information.
|
|
|
|
|
2014-09-17 08:27:19 +02:00
|
|
|
=encoding UTF8
|
|
|
|
|
2005-07-02 04:08:37 +02:00
|
|
|
=head1 COPYRIGHT
|
|
|
|
|
2012-04-08 08:01:24 +02:00
|
|
|
Copyright 2001-2012 Ask Bjørn Hansen, Develooper LLC. See the
|
2005-07-02 04:08:37 +02:00
|
|
|
LICENSE file for more information.
|
|
|
|
|
2012-04-08 08:01:24 +02:00
|
|
|
=cut
|
2005-07-02 04:08:37 +02:00
|
|
|
|