2002-07-03 15:10:44 +02:00
|
|
|
package Qpsmtpd;
|
|
|
|
use strict;
|
2005-09-22 19:14:20 +02:00
|
|
|
use vars qw($VERSION $Logger $TraceLevel $Spool_dir $Size_threshold);
|
2002-07-03 15:10:44 +02:00
|
|
|
|
2002-09-24 12:56:35 +02:00
|
|
|
use Sys::Hostname;
|
|
|
|
use Qpsmtpd::Constants;
|
2002-07-03 15:10:44 +02:00
|
|
|
|
2007-06-06 19:07:05 +02:00
|
|
|
$VERSION = "0.40_01";
|
2004-03-05 13:46:24 +01:00
|
|
|
|
|
|
|
sub version { $VERSION };
|
2002-07-03 15:10:44 +02:00
|
|
|
|
2005-05-25 22:07:58 +02:00
|
|
|
sub TRACE_LEVEL { $TraceLevel }; # leave for plugin compatibility
|
2005-03-24 22:16:35 +01:00
|
|
|
|
|
|
|
sub load_logging {
|
|
|
|
# need to do this differently that other plugins so as to
|
|
|
|
# not trigger logging activity
|
|
|
|
my $self = shift;
|
|
|
|
return if $self->{hooks}->{"logging"};
|
|
|
|
my $configdir = $self->config_dir("logging");
|
|
|
|
my $configfile = "$configdir/logging";
|
|
|
|
my @loggers = $self->_config_from_file($configfile,'logging');
|
|
|
|
|
2006-11-05 10:54:03 +01:00
|
|
|
$configdir = $self->config_dir('plugin_dirs');
|
|
|
|
$configfile = "$configdir/plugin_dirs";
|
|
|
|
my @plugin_dirs = $self->_config_from_file($configfile,'plugin_dirs');
|
2006-11-08 11:25:45 +01:00
|
|
|
unless (@plugin_dirs) {
|
|
|
|
my ($name) = ($0 =~ m!(.*?)/([^/]+)$!);
|
|
|
|
@plugin_dirs = ( "$name/plugins" );
|
|
|
|
}
|
2006-11-05 10:54:03 +01:00
|
|
|
|
|
|
|
my @loaded;
|
|
|
|
for my $logger (@loggers) {
|
|
|
|
push @loaded, $self->_load_plugin($logger, @plugin_dirs);
|
|
|
|
}
|
2005-03-24 22:16:35 +01:00
|
|
|
|
2006-11-05 10:54:03 +01:00
|
|
|
foreach my $logger (@loaded) {
|
2005-03-24 22:16:35 +01:00
|
|
|
$self->log(LOGINFO, "Loaded $logger");
|
|
|
|
}
|
2006-11-05 10:54:03 +01:00
|
|
|
|
2005-03-24 22:16:35 +01:00
|
|
|
return @loggers;
|
|
|
|
}
|
|
|
|
|
2005-03-29 22:15:53 +02:00
|
|
|
sub trace_level {
|
2005-03-24 22:16:35 +01:00
|
|
|
my $self = shift;
|
2005-03-29 22:15:53 +02:00
|
|
|
return $TraceLevel if $TraceLevel;
|
2005-03-24 22:16:35 +01:00
|
|
|
|
|
|
|
my $configdir = $self->config_dir("loglevel");
|
|
|
|
my $configfile = "$configdir/loglevel";
|
2005-05-25 22:07:58 +02:00
|
|
|
$TraceLevel = $self->_config_from_file($configfile,'loglevel');
|
2005-03-24 22:16:35 +01:00
|
|
|
|
2005-05-25 22:07:58 +02:00
|
|
|
unless (defined($TraceLevel) and $TraceLevel =~ /^\d+$/) {
|
2005-03-29 22:15:53 +02:00
|
|
|
$TraceLevel = LOGWARN; # Default if no loglevel file found.
|
2005-03-24 22:16:35 +01:00
|
|
|
}
|
|
|
|
|
2005-03-29 22:15:53 +02:00
|
|
|
return $TraceLevel;
|
2004-03-05 13:46:24 +01:00
|
|
|
}
|
|
|
|
|
2005-05-25 22:07:58 +02:00
|
|
|
sub init_logger { # needed for compatibility purposes
|
|
|
|
shift->trace_level();
|
|
|
|
}
|
|
|
|
|
2002-09-24 12:56:35 +02:00
|
|
|
sub log {
|
|
|
|
my ($self, $trace, @log) = @_;
|
2005-03-24 22:16:35 +01:00
|
|
|
$self->varlog($trace,join(" ",@log));
|
|
|
|
}
|
|
|
|
|
|
|
|
sub varlog {
|
|
|
|
my ($self, $trace) = (shift,shift);
|
|
|
|
my ($hook, $plugin, @log);
|
|
|
|
if ( $#_ == 0 ) { # log itself
|
|
|
|
(@log) = @_;
|
|
|
|
}
|
|
|
|
elsif ( $#_ == 1 ) { # plus the hook
|
|
|
|
($hook, @log) = @_;
|
|
|
|
}
|
|
|
|
else { # called from plugin
|
|
|
|
($hook, $plugin, @log) = @_;
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->load_logging; # in case we already don't have this loaded yet
|
|
|
|
|
|
|
|
my ($rc) = $self->run_hooks("logging", $trace, $hook, $plugin, @log);
|
|
|
|
|
|
|
|
unless ( $rc and $rc == DECLINED or $rc == OK ) {
|
|
|
|
# no logging plugins registered so fall back to STDERR
|
|
|
|
warn join(" ", $$ .
|
|
|
|
(defined $plugin ? " $plugin plugin:" :
|
|
|
|
defined $hook ? " running plugin ($hook):" : ""),
|
|
|
|
@log), "\n"
|
2005-03-29 22:15:53 +02:00
|
|
|
if $trace <= $self->trace_level();
|
2005-03-24 22:16:35 +01:00
|
|
|
}
|
2002-07-03 15:10:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# method to get the configuration. It just calls get_qmail_config by
|
|
|
|
# default, but it could be overwritten to look configuration up in a
|
|
|
|
# database or whatever.
|
|
|
|
#
|
|
|
|
sub config {
|
2003-03-25 13:50:07 +01:00
|
|
|
my ($self, $c, $type) = @_;
|
2002-07-03 15:10:44 +02:00
|
|
|
|
2002-10-17 09:39:54 +02:00
|
|
|
#warn "SELF->config($c) ", ref $self;
|
2002-09-24 12:56:35 +02:00
|
|
|
|
2002-07-03 15:10:44 +02:00
|
|
|
my %defaults = (
|
|
|
|
me => hostname,
|
|
|
|
timeout => 1200,
|
|
|
|
);
|
|
|
|
|
2002-11-06 07:40:35 +01:00
|
|
|
my ($rc, @config) = $self->run_hooks("config", $c);
|
|
|
|
@config = () unless $rc == OK;
|
|
|
|
|
2002-07-06 04:09:01 +02:00
|
|
|
if (wantarray) {
|
2003-03-25 13:50:07 +01:00
|
|
|
@config = $self->get_qmail_config($c, $type) unless @config;
|
2003-07-24 14:43:46 +02:00
|
|
|
@config = $defaults{$c} if (!@config and $defaults{$c});
|
2002-07-06 04:09:01 +02:00
|
|
|
return @config;
|
|
|
|
}
|
|
|
|
else {
|
2003-03-25 13:50:07 +01:00
|
|
|
return ($config[0] || $self->get_qmail_config($c, $type) || $defaults{$c});
|
2002-07-06 04:09:01 +02:00
|
|
|
}
|
2002-07-03 15:10:44 +02:00
|
|
|
}
|
|
|
|
|
2004-09-08 18:26:33 +02:00
|
|
|
sub config_dir {
|
|
|
|
my ($self, $config) = @_;
|
|
|
|
my $configdir = ($ENV{QMAIL} || '/var/qmail') . '/control';
|
2006-11-22 17:30:37 +01:00
|
|
|
my ($path) = ($ENV{PROCESS} ? $ENV{PROCESS} : $0) =~ m!(.*?)/([^/]+)$!;
|
|
|
|
$configdir = "$path/config" if (-e "$path/config/$config");
|
2005-07-29 10:05:42 +02:00
|
|
|
if (exists $ENV{QPSMTPD_CONFIG}) {
|
|
|
|
$ENV{QPSMTPD_CONFIG} =~ /^(.*)$/; # detaint
|
|
|
|
$configdir = $1 if -e "$1/$config";
|
|
|
|
}
|
2004-09-08 18:26:33 +02:00
|
|
|
return $configdir;
|
|
|
|
}
|
|
|
|
|
2006-11-05 10:54:03 +01:00
|
|
|
sub plugin_dirs {
|
|
|
|
my $self = shift;
|
|
|
|
my @plugin_dirs = $self->config('plugin_dirs');
|
|
|
|
|
|
|
|
unless (@plugin_dirs) {
|
2006-11-22 17:30:37 +01:00
|
|
|
my ($path) = ($ENV{PROCESS} ? $ENV{PROCESS} : $0) =~ m!(.*?)/([^/]+)$!;
|
|
|
|
@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-03 15:10:44 +02:00
|
|
|
sub get_qmail_config {
|
2003-03-25 13:50:07 +01:00
|
|
|
my ($self, $config, $type) = @_;
|
2005-07-07 22:10:03 +02:00
|
|
|
$self->log(LOGDEBUG, "trying to get config for $config");
|
2002-07-03 15:10:44 +02:00
|
|
|
if ($self->{_config_cache}->{$config}) {
|
|
|
|
return wantarray ? @{$self->{_config_cache}->{$config}} : $self->{_config_cache}->{$config}->[0];
|
|
|
|
}
|
2004-09-08 18:26:33 +02:00
|
|
|
my $configdir = $self->config_dir($config);
|
2003-03-25 13:50:07 +01:00
|
|
|
|
|
|
|
my $configfile = "$configdir/$config";
|
|
|
|
|
|
|
|
if ($type and $type eq "map") {
|
2003-08-30 17:14:56 +02:00
|
|
|
return +{} unless -e $configfile . ".cdb";
|
2003-03-25 13:50:07 +01:00
|
|
|
eval { require CDB_File };
|
|
|
|
|
|
|
|
if ($@) {
|
2004-07-18 13:02:08 +02:00
|
|
|
$self->log(LOGERROR, "No CDB Support! Did NOT read $configfile.cdb, could not load CDB_File module: $@");
|
|
|
|
return +{};
|
2003-03-25 13:50:07 +01:00
|
|
|
}
|
2004-07-18 13:02:08 +02:00
|
|
|
|
2003-03-25 13:50:07 +01:00
|
|
|
my %h;
|
|
|
|
unless (tie(%h, 'CDB_File', "$configfile.cdb")) {
|
2004-03-05 13:46:24 +01:00
|
|
|
$self->log(LOGERROR, "tie of $configfile.cdb failed: $!");
|
|
|
|
return +{};
|
2003-03-25 13:50:07 +01:00
|
|
|
}
|
|
|
|
#warn Data::Dumper->Dump([\%h], [qw(h)]);
|
|
|
|
# should we cache this?
|
|
|
|
return \%h;
|
|
|
|
}
|
|
|
|
|
2004-03-05 13:46:24 +01:00
|
|
|
return $self->_config_from_file($configfile, $config);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _config_from_file {
|
2005-07-31 07:54:36 +02:00
|
|
|
my ($self, $configfile, $config, $visited) = @_;
|
2003-06-10 12:06:31 +02:00
|
|
|
return unless -e $configfile;
|
2005-07-31 07:54:36 +02:00
|
|
|
|
|
|
|
$visited ||= [];
|
|
|
|
push @{$visited}, $configfile;
|
|
|
|
|
2003-04-23 05:36:36 +02:00
|
|
|
open CF, "<$configfile" or warn "$$ could not open configfile $configfile: $!" and return;
|
2002-07-03 15:10:44 +02:00
|
|
|
my @config = <CF>;
|
|
|
|
chomp @config;
|
2004-03-05 13:46:24 +01:00
|
|
|
@config = grep { length($_) and $_ !~ m/^\s*#/ and $_ =~ m/\S/} @config;
|
2002-07-03 15:10:44 +02:00
|
|
|
close CF;
|
2005-07-31 07:54:36 +02:00
|
|
|
|
|
|
|
my $pos = 0;
|
|
|
|
while ($pos < @config) {
|
|
|
|
# recursively pursue an $include reference, if found. An inclusion which
|
|
|
|
# begins with a leading slash is interpreted as a path to a file and will
|
|
|
|
# supercede the usual config path resolution. Otherwise, the normal
|
|
|
|
# config_dir() lookup is employed (the location in which the inclusion
|
|
|
|
# appeared receives no special precedence; possibly it should, but it'd
|
|
|
|
# be complicated beyond justifiability for so simple a config system.
|
|
|
|
if ($config[$pos] =~ /^\s*\$include\s+(\S+)\s*$/) {
|
|
|
|
my ($includedir, $inclusion) = ('', $1);
|
|
|
|
|
|
|
|
splice @config, $pos, 1; # remove the $include line
|
|
|
|
if ($inclusion !~ /^\//) {
|
|
|
|
$includedir = $self->config_dir($inclusion);
|
|
|
|
$inclusion = "$includedir/$inclusion";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (grep($_ eq $inclusion, @{$visited})) {
|
|
|
|
$self->log(LOGERROR, "Circular \$include reference in config $config:");
|
|
|
|
$self->log(LOGERROR, "From $visited->[0]:");
|
|
|
|
$self->log(LOGERROR, " includes $_")
|
|
|
|
for (@{$visited}[1..$#{$visited}], $inclusion);
|
|
|
|
return wantarray ? () : undef;
|
|
|
|
}
|
|
|
|
push @{$visited}, $inclusion;
|
|
|
|
|
|
|
|
for my $inc ($self->expand_inclusion_($inclusion, $configfile)) {
|
|
|
|
my @insertion = $self->_config_from_file($inc, $config, $visited);
|
|
|
|
splice @config, $pos, 0, @insertion; # insert the inclusion
|
|
|
|
$pos += @insertion;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$pos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-03 15:10:44 +02:00
|
|
|
$self->{_config_cache}->{$config} = \@config;
|
2005-07-31 07:54:36 +02:00
|
|
|
|
2002-07-03 15:10:44 +02:00
|
|
|
return wantarray ? @config : $config[0];
|
|
|
|
}
|
|
|
|
|
2005-07-31 07:54:36 +02:00
|
|
|
sub expand_inclusion_ {
|
|
|
|
my $self = shift;
|
|
|
|
my $inclusion = shift;
|
|
|
|
my $context = shift;
|
|
|
|
my @includes;
|
|
|
|
|
|
|
|
if (-d $inclusion) {
|
|
|
|
$self->log(LOGDEBUG, "inclusion of directory $inclusion from $context");
|
|
|
|
|
|
|
|
if (opendir(INCD, $inclusion)) {
|
|
|
|
@includes = map { "$inclusion/$_" }
|
|
|
|
(grep { -f "$inclusion/$_" and !/^\./ } readdir INCD);
|
|
|
|
closedir INCD;
|
|
|
|
} else {
|
|
|
|
$self->log(LOGERROR, "Couldn't open directory $inclusion,".
|
|
|
|
" referenced from $context ($!)");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$self->log(LOGDEBUG, "inclusion of file $inclusion from $context");
|
|
|
|
@includes = ( $inclusion );
|
|
|
|
}
|
|
|
|
return @includes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-06 09:16:23 +02:00
|
|
|
sub load_plugins {
|
|
|
|
my $self = shift;
|
2004-08-31 03:58:57 +02:00
|
|
|
|
2005-03-29 22:15:53 +02:00
|
|
|
$self->log(LOGWARN, "Plugins already loaded") if $self->{hooks};
|
2004-09-08 18:26:33 +02:00
|
|
|
$self->{hooks} = {};
|
2004-08-31 03:58:57 +02:00
|
|
|
|
2002-07-06 09:16:23 +02:00
|
|
|
my @plugins = $self->config('plugins');
|
2006-11-05 10:54:03 +01:00
|
|
|
my @loaded;
|
2002-07-06 09:16:23 +02:00
|
|
|
|
2006-11-05 10:54:03 +01:00
|
|
|
for my $plugin_line (@plugins) {
|
2006-11-22 17:30:37 +01:00
|
|
|
my $this_plugin = $self->_load_plugin($plugin_line, $self->plugin_dirs);
|
|
|
|
push @loaded, $this_plugin if $this_plugin;
|
2006-11-05 10:54:03 +01:00
|
|
|
}
|
2004-03-05 13:46:24 +01:00
|
|
|
|
2006-11-05 10:54:03 +01: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 {
|
2004-03-05 13:46:24 +01:00
|
|
|
my $self = shift;
|
2006-11-05 10:54:03 +01:00
|
|
|
my ($plugin_line, @plugin_dirs) = @_;
|
2004-09-08 18:26:33 +02:00
|
|
|
|
2006-11-05 10:54:03 +01:00
|
|
|
my ($plugin, @args) = split ' ', $plugin_line;
|
|
|
|
|
|
|
|
my $package;
|
|
|
|
|
|
|
|
if ($plugin =~ m/::/) {
|
|
|
|
# "full" package plugin (My::Plugin)
|
|
|
|
$package = $plugin;
|
|
|
|
$package =~ s/[^_a-z0-9:]+//gi;
|
|
|
|
my $eval = qq[require $package;\n]
|
|
|
|
.qq[sub ${plugin}::plugin_name { '$plugin' }];
|
|
|
|
$eval =~ m/(.*)/s;
|
|
|
|
$eval = $1;
|
|
|
|
eval $eval;
|
|
|
|
die "Failed loading $package - eval $@" if $@;
|
|
|
|
$self->log(LOGDEBUG, "Loading $package ($plugin_line)")
|
|
|
|
unless $plugin_line =~ /logging/;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
# 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
|
|
|
|
}[
|
|
|
|
"::" . (length $2 ? sprintf("_%2x",unpack("C",$2)) : "")
|
|
|
|
]egx;
|
|
|
|
|
|
|
|
$package = "Qpsmtpd::Plugin::$plugin_name";
|
|
|
|
|
|
|
|
# don't reload plugins if they are already loaded
|
|
|
|
unless ( defined &{"${package}::plugin_name"} ) {
|
|
|
|
PLUGIN_DIR: for my $dir (@plugin_dirs) {
|
|
|
|
if (-e "$dir/$plugin") {
|
|
|
|
Qpsmtpd::Plugin->compile($plugin_name, $package,
|
2007-05-18 00:02:32 +02:00
|
|
|
"$dir/$plugin", $self->{_test_mode}, $plugin);
|
2006-11-05 10:54:03 +01:00
|
|
|
$self->log(LOGDEBUG, "Loading $plugin_line from $dir/$plugin")
|
|
|
|
unless $plugin_line =~ /logging/;
|
|
|
|
last PLUGIN_DIR;
|
|
|
|
}
|
2006-06-28 22:05:04 +02:00
|
|
|
}
|
2005-03-24 22:16:35 +01:00
|
|
|
}
|
2002-07-08 04:30:11 +02:00
|
|
|
}
|
2006-11-05 10:54:03 +01:00
|
|
|
|
|
|
|
my $plug = $package->new();
|
|
|
|
$plug->_register($self, @args);
|
2004-09-08 18:26:33 +02:00
|
|
|
|
2007-01-12 00:52:51 +01:00
|
|
|
return $plug;
|
2002-07-08 04:30:11 +02:00
|
|
|
}
|
2002-07-06 09:16:23 +02:00
|
|
|
|
2005-07-07 22:10:03 +02:00
|
|
|
sub transaction {
|
|
|
|
return {}; # base class implements empty transaction
|
|
|
|
}
|
|
|
|
|
2002-07-08 04:30:11 +02:00
|
|
|
sub run_hooks {
|
|
|
|
my ($self, $hook) = (shift, shift);
|
2004-08-31 03:58:57 +02:00
|
|
|
my $hooks = $self->{hooks};
|
|
|
|
if ($hooks->{$hook}) {
|
2002-07-08 04:30:11 +02:00
|
|
|
my @r;
|
2006-11-30 23:10:55 +01:00
|
|
|
my @local_hooks = @{$hooks->{$hook}};
|
|
|
|
$self->{_continuation} = [$hook, [@_], @local_hooks];
|
|
|
|
return $self->run_continuation();
|
|
|
|
}
|
2006-12-08 20:46:18 +01:00
|
|
|
return $self->hook_responder($hook, [0, ''], [@_]);
|
2006-11-30 23:10:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub run_continuation {
|
|
|
|
my $self = shift;
|
|
|
|
die "No continuation in progress" unless $self->{_continuation};
|
|
|
|
$self->continue_read() if $self->isa('Danga::Client');
|
|
|
|
my $todo = $self->{_continuation};
|
|
|
|
$self->{_continuation} = undef;
|
|
|
|
my $hook = shift @$todo || die "No hook in the continuation";
|
|
|
|
my $args = shift @$todo || die "No hook args in the continuation";
|
|
|
|
my @r;
|
|
|
|
while (@$todo) {
|
|
|
|
my $code = shift @$todo;
|
|
|
|
if ( $hook eq 'logging' ) { # without calling $self->log()
|
|
|
|
eval { (@r) = $code->{code}->($self, $self->transaction, @$args); };
|
|
|
|
$@ and warn("FATAL LOGGING PLUGIN ERROR: ", $@) and next;
|
|
|
|
}
|
|
|
|
else {
|
2006-12-16 10:56:09 +01:00
|
|
|
$self->varlog(LOGDEBUG, $hook, $code->{name});
|
2006-11-30 23:10:55 +01:00
|
|
|
eval { (@r) = $code->{code}->($self, $self->transaction, @$args); };
|
|
|
|
$@ and $self->log(LOGCRIT, "FATAL PLUGIN ERROR: ", $@) and next;
|
|
|
|
|
|
|
|
!defined $r[0]
|
|
|
|
and $self->log(LOGERROR, "plugin ".$code->{name}
|
|
|
|
." running the $hook hook returned undef!")
|
|
|
|
and next;
|
|
|
|
|
|
|
|
if ($self->transaction) {
|
|
|
|
my $tnotes = $self->transaction->notes( $code->{name} );
|
|
|
|
$tnotes->{"hook_$hook"}->{'return'} = $r[0]
|
|
|
|
if (!defined $tnotes || ref $tnotes eq "HASH");
|
2005-03-24 22:16:35 +01:00
|
|
|
}
|
|
|
|
else {
|
2006-11-30 23:10:55 +01:00
|
|
|
my $cnotes = $self->connection->notes( $code->{name} );
|
|
|
|
$cnotes->{"hook_$hook"}->{'return'} = $r[0]
|
|
|
|
if (!defined $cnotes || ref $cnotes eq "HASH");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($r[0] == YIELD) {
|
|
|
|
$self->pause_read() if $self->isa('Danga::Client');
|
|
|
|
$self->{_continuation} = [$hook, $args, @$todo];
|
|
|
|
return @r;
|
|
|
|
}
|
|
|
|
elsif ($r[0] == DENY or $r[0] == DENYSOFT or
|
|
|
|
$r[0] == DENY_DISCONNECT or $r[0] == DENYSOFT_DISCONNECT)
|
|
|
|
{
|
|
|
|
$r[1] = "" if not defined $r[1];
|
|
|
|
$self->log(LOGDEBUG, "Plugin ".$code->{name}.
|
2005-03-29 22:15:53 +02:00
|
|
|
", hook $hook returned ".return_code($r[0]).", $r[1]");
|
2006-11-30 23:10:55 +01:00
|
|
|
$self->run_hooks("deny", $code->{name}, $r[0], $r[1]) unless ($hook eq "deny");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$r[1] = "" if not defined $r[1];
|
|
|
|
$self->log(LOGDEBUG, "Plugin ".$code->{name}.
|
2005-03-29 22:15:53 +02:00
|
|
|
", hook $hook returned ".return_code($r[0]).", $r[1]");
|
2006-11-30 23:10:55 +01:00
|
|
|
$self->run_hooks("ok", $code->{name}, $r[0], $r[1]) unless ($hook eq "ok");
|
2003-01-20 12:02:20 +01:00
|
|
|
}
|
|
|
|
|
2002-07-08 04:30:11 +02:00
|
|
|
}
|
2006-11-30 23:10:55 +01:00
|
|
|
|
|
|
|
last unless $r[0] == DECLINED;
|
2002-07-06 09:16:23 +02:00
|
|
|
}
|
2006-11-30 23:10:55 +01:00
|
|
|
$r[0] = DECLINED if not defined $r[0];
|
|
|
|
@r = map { split /\n/ } @r;
|
|
|
|
return $self->hook_responder($hook, \@r, $args);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub hook_responder {
|
|
|
|
my ($self, $hook, $msg, $args) = @_;
|
|
|
|
|
|
|
|
my $code = shift @$msg;
|
|
|
|
|
|
|
|
my $responder = $hook . '_respond';
|
|
|
|
if (my $meth = $self->can($responder)) {
|
2006-12-08 20:46:18 +01:00
|
|
|
return $meth->($self, $code, $msg, $args);
|
2006-11-30 23:10:55 +01:00
|
|
|
}
|
|
|
|
return $code, @$msg;
|
2002-07-08 04:30:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub _register_hook {
|
|
|
|
my $self = shift;
|
2004-06-11 22:01:17 +02:00
|
|
|
my ($hook, $code, $unshift) = @_;
|
2002-07-06 09:16:23 +02:00
|
|
|
|
2004-08-31 03:58:57 +02:00
|
|
|
my $hooks = $self->{hooks};
|
2004-06-11 22:01:17 +02:00
|
|
|
if ($unshift) {
|
|
|
|
unshift @{$hooks->{$hook}}, $code;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
push @{$hooks->{$hook}}, $code;
|
|
|
|
}
|
2002-07-06 09:16:23 +02:00
|
|
|
}
|
|
|
|
|
2005-02-22 03:47:39 +01:00
|
|
|
sub spool_dir {
|
|
|
|
my $self = shift;
|
|
|
|
|
2005-03-24 22:16:35 +01:00
|
|
|
unless ( $Spool_dir ) { # first time through
|
2005-03-01 15:33:26 +01:00
|
|
|
$self->log(LOGINFO, "Initializing spool_dir");
|
2005-03-24 22:16:35 +01:00
|
|
|
$Spool_dir = $self->config('spool_dir')
|
2005-03-01 15:33:26 +01:00
|
|
|
|| Qpsmtpd::Utils::tildeexp('~/tmp/');
|
2005-02-22 03:47:39 +01:00
|
|
|
|
2005-03-24 22:16:35 +01:00
|
|
|
$Spool_dir .= "/" unless ($Spool_dir =~ m!/$!);
|
2005-03-01 15:33:26 +01:00
|
|
|
|
2005-03-24 22:16:35 +01:00
|
|
|
$Spool_dir =~ /^(.+)$/ or die "spool_dir not configured properly";
|
|
|
|
$Spool_dir = $1; # cleanse the taint
|
2005-02-22 03:47:39 +01:00
|
|
|
|
|
|
|
# Make sure the spool dir has appropriate rights
|
2005-03-24 22:16:35 +01:00
|
|
|
if (-e $Spool_dir) {
|
|
|
|
my $mode = (stat($Spool_dir))[2];
|
2005-03-01 15:33:26 +01:00
|
|
|
$self->log(LOGWARN,
|
2005-03-24 22:16:35 +01:00
|
|
|
"Permissions on spool_dir $Spool_dir are not 0700")
|
2005-03-01 15:33:26 +01:00
|
|
|
if $mode & 07077;
|
2005-02-22 03:47:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# And finally, create it if it doesn't already exist
|
2005-03-24 22:16:35 +01:00
|
|
|
-d $Spool_dir or mkdir($Spool_dir, 0700)
|
|
|
|
or die "Could not create spool_dir $Spool_dir: $!";
|
|
|
|
}
|
2005-03-01 15:33:26 +01:00
|
|
|
|
2005-03-24 22:16:35 +01: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.
|
|
|
|
my $transaction_counter = 0;
|
|
|
|
|
|
|
|
sub temp_file {
|
|
|
|
my $self = shift;
|
|
|
|
my $filename = $self->spool_dir()
|
|
|
|
. join(":", time, $$, $transaction_counter++);
|
|
|
|
return $filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub temp_dir {
|
|
|
|
my $self = shift;
|
|
|
|
my $mask = shift || 0700;
|
|
|
|
my $dirname = $self->temp_file();
|
|
|
|
-d $dirname or mkdir($dirname, $mask)
|
|
|
|
or die "Could not create temporary directory $dirname: $!";
|
|
|
|
return $dirname;
|
|
|
|
}
|
|
|
|
|
2005-09-22 19:14:20 +02:00
|
|
|
sub size_threshold {
|
|
|
|
my $self = shift;
|
|
|
|
unless ( defined $Size_threshold ) {
|
2005-09-22 19:29:13 +02:00
|
|
|
$Size_threshold = $self->config('size_threshold') || 0;
|
2005-09-22 19:14:20 +02:00
|
|
|
$self->log(LOGNOTICE, "size_threshold set to $Size_threshold");
|
|
|
|
}
|
|
|
|
return $Size_threshold;
|
|
|
|
}
|
2005-10-31 18:12:37 +01:00
|
|
|
|
2007-02-07 22:49:45 +01:00
|
|
|
sub authenticated {
|
|
|
|
my $self = shift;
|
|
|
|
return (defined $self->{_auth} ? $self->{_auth} : "" );
|
|
|
|
}
|
|
|
|
|
2005-10-31 18:12:37 +01:00
|
|
|
sub auth_user {
|
|
|
|
my $self = shift;
|
|
|
|
return (defined $self->{_auth_user} ? $self->{_auth_user} : "" );
|
|
|
|
}
|
|
|
|
|
|
|
|
sub auth_mechanism {
|
|
|
|
my $self = shift;
|
|
|
|
return (defined $self->{_auth_mechanism} ? $self->{_auth_mechanism} : "" );
|
|
|
|
}
|
2005-09-22 19:14:20 +02:00
|
|
|
|
2002-07-03 15:10:44 +02:00
|
|
|
1;
|
2005-07-02 04:08:37 +02:00
|
|
|
|
|
|
|
__END__
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
Qpsmtpd
|
|
|
|
|
|
|
|
=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.
|
|
|
|
|
|
|
|
=head1 COPYRIGHT
|
|
|
|
|
|
|
|
Copyright 2001-2005 Ask Bjoern Hansen, Develooper LLC. See the
|
|
|
|
LICENSE file for more information.
|
|
|
|
|
|
|
|
|
|
|
|
|