Here's a revision of the same. Applies to 0.31's lib/Qpsmtpd.pm.
Functionally identical to the previous version, but I factored out the one
piece which could be factored without making a referential mess of things.
That one piece is the expansion of a directory into a list of files and the
inclusion of each, which I wasn't originally going to implement at all, but
Matt had put it in the original inclusion code in load_plugins() and I'm leery
of taking the regression now.
Tested against 0.31 tip. The original version of this change is in the Debian
qpsmtpd, used to separate the auto-generated configuration from the
admin-edited parts (clobbering an admin's manual configuration is forbidden,
but un-edited configs must be replaced whenever they change.)
diffstat:
Qpsmtpd.pm | 92 +++++++++++++++++++++++++++++++++++++++++++++----------------
1 files changed, 68 insertions(+), 24 deletions(-)
(delta is mostly comments and error checking)
--
Devin \ aqua(at)devin.com, IRC:Requiem; http://www.devin.com
Carraway \ 1024D/E9ABFCD2: 13E7 199E DD1E 65F0 8905 2E43 5395 CA0D E9AB FCD2
Index: lib/Qpsmtpd.pm
===================================================================
--- lib/Qpsmtpd.pm (revision 529)
+++ lib/Qpsmtpd.pm (working copy)
@@ -159,18 +159,84 @@
}
sub _config_from_file {
- my ($self, $configfile, $config) = @_;
+ my ($self, $configfile, $config, $visited) = @_;
return unless -e $configfile;
+
+ $visited ||= [];
+ push @{$visited}, $configfile;
+
open CF, "<$configfile" or warn "$$ could not open configfile $configfile:
$!" and return;
my @config = <CF>;
chomp @config;
@config = grep { length($_) and $_ !~ m/^\s*#/ and $_ =~ m/\S/} @config;
close CF;
- #$self->log(10, "returning get_config for $config
",Data::Dumper->Dump([EMAIL PROTECTED], [qw(config)]));
+
+ 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++;
+ }
+ }
+
$self->{_config_cache}->{$config} = [EMAIL PROTECTED];
+
return wantarray ? @config : $config[0];
}
+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;
+}
+
+
sub load_plugins {
my $self = shift;
@@ -195,28 +261,6 @@
for my $plugin_line (@plugins) {
my ($plugin, @args) = split ' ', $plugin_line;
- if (lc($plugin) eq '$include') {
- my $inc = shift @args;
- my $config_dir = $self->config_dir($inc);
- if (-d "$config_dir/$inc") {
- $self->log(LOGDEBUG, "Loading include dir: $config_dir/$inc");
- opendir(DIR, "$config_dir/$inc") || die "opendir($config_dir/$inc):
$!";
- my @plugconf = sort grep { -f $_ } map { "$config_dir/$inc/$_" } grep
{ !/^\./ } readdir(DIR);
- closedir(DIR);
- foreach my $f (@plugconf) {
- push @ret, $self->_load_plugins($dir, $self->_config_from_file($f,
"plugins"));
- }
- }
- elsif (-f "$config_dir/$inc") {
- $self->log(LOGDEBUG, "Loading include file: $config_dir/$inc");
- push @ret, $self->_load_plugins($dir,
$self->_config_from_file("$config_dir/$inc", "plugins"));
- }
- else {
- $self->log(LOGCRIT, "CRITICAL PLUGIN CONFIG ERROR: Include
$config_dir/$inc not found");
- }
- next;
- }
-
my $plugin_name = $plugin;
$plugin =~ s/:\d+$//; # after this point, only used for filename