updated Config::defaults() to behave as before

and added additional tests for it
This commit is contained in:
Matt Simerson 2014-09-16 09:52:05 -07:00
parent b0c3d715cc
commit a473c2732d
3 changed files with 49 additions and 37 deletions

View File

@ -1,5 +1,6 @@
package Qpsmtpd; package Qpsmtpd;
use strict; use strict;
#use warnings; #use warnings;
our $VERSION = "0.95"; our $VERSION = "0.95";
@ -30,11 +31,11 @@ sub _restart {
# reset all global vars to defaults # reset all global vars to defaults
$self->conf->clear_cache(); $self->conf->clear_cache();
$hooks = {}; $hooks = {};
$LOGGING_LOADED = 0; $LOGGING_LOADED = 0;
$TraceLevel = LOGWARN; $TraceLevel = LOGWARN;
$Spool_dir = undef; $Spool_dir = undef;
$Size_threshold = undef; $Size_threshold = undef;
} }
} }
@ -108,7 +109,7 @@ sub varlog {
$self->run_hooks_no_respond("logging", $trace, $hook, $plugin, @log) $self->run_hooks_no_respond("logging", $trace, $hook, $plugin, @log)
or return; or return;
return if $rc == DECLINED || $rc == OK; # plugin success return if $rc == DECLINED || $rc == OK; # plugin success
return if $trace > $TraceLevel; return if $trace > $TraceLevel;
# no logging plugins registered, fall back to STDERR # no logging plugins registered, fall back to STDERR
@ -125,14 +126,14 @@ sub conf {
my $self = shift; my $self = shift;
if (!$self->{_config}) { if (!$self->{_config}) {
$self->{_config} = Qpsmtpd::Config->new(); $self->{_config} = Qpsmtpd::Config->new();
}; }
return $self->{_config}; return $self->{_config};
} }
sub config { sub config {
my $self = shift; my $self = shift;
return $self->conf->config($self, @_); return $self->conf->config($self, @_);
}; }
sub config_dir { sub config_dir {
my $self = shift; my $self = shift;
@ -205,15 +206,17 @@ sub _load_plugin {
for my $dir (@plugin_dirs) { for my $dir (@plugin_dirs) {
next if !-e "$dir/$plugin"; next if !-e "$dir/$plugin";
Qpsmtpd::Plugin->compile($plugin_name, $package, Qpsmtpd::Plugin->compile($plugin_name, $package,
"$dir/$plugin", $self->{_test_mode}, $plugin); "$dir/$plugin", $self->{_test_mode},
$plugin);
if ($safe_line !~ /logging/) { if ($safe_line !~ /logging/) {
$self->log(LOGDEBUG, "Loading $safe_line from $dir/$plugin"); $self->log(LOGDEBUG, "Loading $safe_line from $dir/$plugin");
}; }
last; last;
} }
if (! defined &{"${package}::plugin_name"}) { if (!defined &{"${package}::plugin_name"}) {
die "Plugin $plugin_name not found in our plugin dirs (", join(', ', @plugin_dirs), ")"; die "Plugin $plugin_name not found in our plugin dirs (",
}; join(', ', @plugin_dirs), ")";
}
} }
my $plug = $package->new(); my $plug = $package->new();
@ -232,11 +235,12 @@ sub _load_package_plugin {
qq[require $package;\n] . qq[sub ${plugin}::plugin_name { '$plugin' }]; qq[require $package;\n] . qq[sub ${plugin}::plugin_name { '$plugin' }];
$eval =~ m/(.*)/s; $eval =~ m/(.*)/s;
$eval = $1; $eval = $1;
eval $eval; ## no critic (Eval) eval $eval; ## no critic (Eval)
die "Failed loading $package - eval $@" if $@; die "Failed loading $package - eval $@" if $@;
if ($plugin_line !~ /logging/) { if ($plugin_line !~ /logging/) {
$self->log(LOGDEBUG, "Loading $package ($plugin_line)"); $self->log(LOGDEBUG, "Loading $package ($plugin_line)");
}; }
my $plug = $package->new(); my $plug = $package->new();
$plug->_register($self, @$args); $plug->_register($self, @$args);
@ -299,7 +303,8 @@ sub run_continuation {
my $tran = $self->transaction; my $tran = $self->transaction;
eval { (@r) = $code->{code}->($self, $tran, @$args); }; eval { (@r) = $code->{code}->($self, $tran, @$args); };
if ($@) { if ($@) {
$self->log(LOGCRIT, "FATAL PLUGIN ERROR [" . $code->{name} . "]: ", $@); $self->log(LOGCRIT, "FATAL PLUGIN ERROR [" . $code->{name} . "]: ",
$@);
next; next;
} }
@ -402,20 +407,20 @@ sub spool_dir {
$Spool_dir .= "/" if $Spool_dir !~ m!/$!; $Spool_dir .= "/" if $Spool_dir !~ m!/$!;
$Spool_dir =~ /^(.+)$/ or die "spool_dir not configured properly"; $Spool_dir =~ /^(.+)$/ or die "spool_dir not configured properly";
$Spool_dir = $1; # cleanse the taint $Spool_dir = $1; # cleanse the taint
my $Spool_perms = $self->config('spool_perms') || '0700'; my $Spool_perms = $self->config('spool_perms') || '0700';
if (!-d $Spool_dir) { # create if it doesn't exist if (!-d $Spool_dir) { # create if it doesn't exist
mkdir($Spool_dir, oct($Spool_perms)) mkdir($Spool_dir, oct($Spool_perms))
or die "Could not create spool_dir $Spool_dir: $!"; or die "Could not create spool_dir $Spool_dir: $!";
} }
# Make sure the spool dir has appropriate rights # Make sure the spool dir has appropriate rights
if (((stat $Spool_dir)[2] & oct('07777')) != oct($Spool_perms)) { if (((stat $Spool_dir)[2] & oct('07777')) != oct($Spool_perms)) {
$self->log(LOGWARN, $self->log(LOGWARN,
"Permissions on spool_dir $Spool_dir are not $Spool_perms") "Permissions on spool_dir $Spool_dir are not $Spool_perms");
}; }
return $Spool_dir; return $Spool_dir;
} }
@ -433,10 +438,12 @@ sub temp_file {
sub temp_dir { sub temp_dir {
my ($self, $mask) = @_; my ($self, $mask) = @_;
$mask ||= '0700';
my $dirname = $self->temp_file(); my $dirname = $self->temp_file();
-d $dirname if (!-d $dirname) {
or mkdir($dirname, $mask || '0700') mkdir($dirname, $mask)
or die "Could not create temporary directory $dirname: $!"; or die "Could not create temporary directory $dirname: $!";
}
return $dirname; return $dirname;
} }

View File

@ -11,12 +11,13 @@ use Qpsmtpd::Constants;
our %config_cache = (); our %config_cache = ();
our %dir_memo; our %dir_memo;
our %defaults = ( our %defaults = (
me => hostname, me => hostname,
timeout => 1200, timeout => 1200,
); );
sub log { sub log {
my ($self, $trace, @log) = @_; my ($self, $trace, @log) = @_;
# logging methods attempt to read config files, this log() prevents that # logging methods attempt to read config files, this log() prevents that
# until after logging has fully loaded # until after logging has fully loaded
return if $trace > LOGWARN; return if $trace > LOGWARN;
@ -38,19 +39,19 @@ sub config {
($rc, @config) = $qp->run_hooks_no_respond('user_config', $type, $c); ($rc, @config) = $qp->run_hooks_no_respond('user_config', $type, $c);
if (defined $rc && $rc == OK) { if (defined $rc && $rc == OK) {
return wantarray ? @config : $config[0]; return wantarray ? @config : $config[0];
}; }
}; }
# then run the config hooks # then run the config hooks
($rc, @config) = $qp->run_hooks_no_respond('config', $c); ($rc, @config) = $qp->run_hooks_no_respond('config', $c);
$qp->log(LOGDEBUG, $qp->log(LOGDEBUG,
"config($c): hook returned (" "config($c): hook returned ("
. join(',', map { defined $_ ? $_ : 'undef' } ($rc, @config)) . join(',', map { defined $_ ? $_ : 'undef' } ($rc, @config))
. ")" . ")"
); );
if (defined $rc && $rc == OK) { if (defined $rc && $rc == OK) {
return wantarray ? @config : $config[0]; return wantarray ? @config : $config[0];
}; }
# then qmail # then qmail
@config = $self->get_qmail($c, $type); @config = $self->get_qmail($c, $type);
@ -77,12 +78,12 @@ sub config_dir {
sub clear_cache { sub clear_cache {
%config_cache = (); %config_cache = ();
%dir_memo = (); %dir_memo = ();
} }
sub default { sub default {
my ($self, $def) = @_; my ($self, $def) = @_;
return if ! $defaults{$def}; return if !exists $defaults{$def};
return wantarray ? ($defaults{$def}) : $defaults{$def}; return wantarray ? ($defaults{$def}) : $defaults{$def};
} }

View File

@ -79,7 +79,11 @@ sub __clear_cache {
sub __default { sub __default {
is($config->default('me'), hostname, "default, my hostname"); is($config->default('me'), hostname, "default, my hostname");
is($config->default('timeout'), 1200, "default timeout is 1200"); is($config->default('timeout'), 1200, "default timeout is 1200");
ok(!$config->default('undefined-test'), "default, undefined");
is($config->default('undefined-test'), undef, "default, undefined");
$Qpsmtpd::Config::defaults{'zero-test'} = 0;
is($config->default('zero-test'), 0, "default, zero");
} }
sub __get_qmail { sub __get_qmail {