| Courier-Filter documentation | Contained in the Courier-Filter distribution. |
Courier::Filter - Purely Perl-based mail filter framework for the Courier MTA
0.200
use Courier::Filter;
use Courier::Filter::Logger::Moo;
use Courier::Filter::Module::Foo;
use Courier::Filter::Module::Bar;
my $filter = Courier::Filter->new(
mandatory => 1,
logger => Courier::Filter::Logger::Moo->new( ... ),
modules => [
Courier::Filter::Module::Foo->new( ... ),
Courier::Filter::Module::Bar->new( ... )
],
testing => 0,
debugging => 0
);
my $exit_code = $filter->run() || 0;
exit($exit_code);
For an architectural and administrative overview of the Courier::Filter framework, see Courier::Filter::Overview.
The Courier::Filter class is the heart of the Courier::Filter framework. To
drive a courierfilter filter process, create a Courier::Filter object,
passing the filter modules and loggers you want to use to the constructor, and
call the run() method.
Courier::Filter will then take care of creating the courierfilter socket in the right place in a safe manner, listening for connections from Courier, asking filter modules for consideration of messages, notifying Courier of whether messages should be accepted or rejected, logging message rejections, catching and logging errors, and finally removing the socket when being terminated by Courier.
The following constructor is provided:
Creates a new Courier::Filter object. Also creates the courierfilter socket
in the right place in a safe manner.
%options is a list of key/value pairs representing any of the following options:
The name of the filter process. Used to build the socket name. Defaults to
the base name of the process ($0).
A boolean value controlling whether the filter process should act as a
mandatory courierfilter. If true, users will not be able to bypass the
filter modules in this filter process from their individual localmailfilter
filters. Technically, this controls whether the courierfilter socket will be
created in the allfilters (true) or the filters (false) directory
in Courier's run-time state directory (see "runtime_dir" in Courier::Config).
Defaults to true.
A Courier::Filter::Logger object that will be used for logging message rejections and error messages. You may override this for individual filter modules for which you do not want the global logger to be used. If no logger is specified, logging is disabled.
Required. A so-called filter module group structure. A module group is a reference to an array that may contain filter module objects (i.e. instances of sub-classes of Courier::Filter::Module), as well as other module groups. Thus, a module group is essentially a tree structure with filter modules as its leaves. When considering messages, Courier::Filter walks the tree in a recursive-descent, depth-first order, asking every filter module for consideration of the message's acceptability.
For instance, given the following filter module group:
[$m1, $m2, [$m3, [$m4, $m5]], $m6]
Courier::Filter queries the filter modules in ascending order from 1 to 6.
The acceptability result returned by each module determines how Courier::Filter proceeds with considering the current message:
For instance, take the nested filter module group from above:
[$m1, $m2, [$m3, [$m4, $m5]], $m6]
| | '---g3---'| |
| '----group 2----' |
'------------group 1-------------'
Let's assume Courier::Filter queries the filter module $m3. If $m3 states an explicit reject, the consideration process is aborted and the current message is rejected. If $m3 states an implicit accept, Courier::Filter proceeds to $m4. If $m3 states an explicit accept, the rest of group 2 (including all of group 3) is skipped and the acceptability result of group 2 is assumed an implicit accept, so Courier::Filter proceeds to $m6.
If no explicit reject has occured when Courier::Filter reaches the end of the main module group, or a module in the main group states an explicit accept, the message is accepted.
Using nested groups of filter modules with normal or inverse polarity, it should be possible to implement sufficiently complex filtering policies to satisfy very most needs.
A boolean value controlling whether the whole filter process should not
apply any filtering to trusted messages. For details on how the trusted status
is determined, see the description of the trusted property in
Courier::Message. In most MTA configurations, this option can be used to
white-list so-called outbound messages. Defaults to false.
A boolean value controlling whether the whole filter process should run in "testing" mode. In testing mode, planned message rejections will be logged as usual, but no messages will actually be rejected. Defaults to false.
NOTE: You may also enable testing mode on individual filter module objects, see "new" in Courier::Filter::Module. Enabling testing mode globally is not the same as individually enabling testing mode on all filter modules, though. When global testing mode is enabled, Courier::Filter only ignores the final result, but still follows the rules of the normal consideration process, e.g. aborting as soon as a filter module states an explicit reject, etc. When an individual filter module is in testing mode, its individual result is ignored, and the consideration process is continued with the next filter module. So individually enabling testing mode on all filter modules allows you to thoroughly test the correctness and performance of all installed filter modules, or even to gather stochastically indepent statistics on the hit/miss rates of your filter modules.
A boolean value controlling whether extra debugging information should be logged by Courier::Filter. Defaults to false. You need to enable debugging mode for filter modules separately.
The following instance methods are provided:
Runs the Courier::Filter. Listens for connections from Courier on the courierfilter socket, asks the configured filter modules for consideration of messages, notifies Courier of whether messages should be accepted or rejected, and logs message rejections. When Courier requests termination of the courierfilter, removes the socket and returns.
Returns the name of the filter process, as set through the constructor's
name option.
Returns a boolean value indicating whether the filter process is a mandatory
courierfilter, as set through the constructor's mandatory option.
If $logger is specified, installs a new global logger. Returns the (newly)
configured global logger.
If \@modules is specified, installs a new filter module group structure.
Returns the (newly) configured filter modules group structure.
Returns a boolean value indicating the trusting mode, as set through the
constructor's trusting option.
Returns a boolean value indicating the global testing mode, as set through the
constructor's testing option.
If $debugging is specified, sets the global debugging mode. Returns a
boolean value indicating the (newly) configured global debugging mode.
courier-filter-perl, Courier::Filter::Overview, Courier::Filter::Module, Courier::Filter::Logger
For AVAILABILITY, SUPPORT, and LICENSE information, see Courier::Filter::Overview.
Julian Mehnle <julian@mehnle.net>
| Courier-Filter documentation | Contained in the Courier-Filter distribution. |
# # Courier::Filter # A purely Perl-based filter framework for the Courier MTA. # # (C) 2003-2008 Julian Mehnle <julian@mehnle.net> # $Id: Filter.pm 210 2008-03-21 19:30:31Z julian $ # ###############################################################################
package Courier::Filter;
use version; our $VERSION = qv('0.200'); use warnings; use strict; #use threads; #BEGIN { require threads if ... } use Error ':try'; use IO::Handle; use IO::Socket::UNIX; use IO::Select; use Courier::Config; use Courier::Message; use Courier::Error; use constant TRUE => (0 == 0); use constant FALSE => not TRUE;
# Implementation: ###############################################################################
sub new { my ($class, %options) = @_; $0 =~ m{([^/]+)$}; my $name = $options{name} || $1; my $mandatory = defined($options{mandatory}) ? $options{mandatory} : TRUE; my $threads = $options{threads}; my $logger = $options{logger}; my $modules = [ @{$options{modules}} ] || []; my $trusting = $options{trusting}; my $testing = $options{testing}; my $debugging = $options{debugging}; my $socket_dir = Courier::Config->runtime_dir . '/' . ( $mandatory ? 'allfilters' : 'filters' ); my $socket_dir_unused = Courier::Config->runtime_dir . '/' . ( !$mandatory ? 'allfilters' : 'filters' ); my $socket_prename = "$socket_dir/.$name"; my $socket_name = "$socket_dir/$name"; my $socket_prename_unused = "$socket_dir_unused/.$name"; my $socket_name_unused = "$socket_dir_unused/$name"; if (-e $socket_name) { -S $socket_name or throw Courier::Error("$socket_name already exists but is not a socket"); # Try to connect to socket to see if it is alive or # if it is left over from a crashed Courier::Filter: my $test_socket = IO::Socket::UNIX->new( Peer => $socket_name ); not defined($test_socket) or throw Courier::Error("Live socket $socket_name found -- is Courier::Filter already running?"); # Socket exists but is dead. Remove it: unlink($socket_name); } unlink($socket_prename); unlink($socket_prename_unused); unlink($socket_name_unused); my $socket = IO::Socket::UNIX->new( Local => $socket_prename, Listen => SOMAXCONN ) or throw Courier::Error("Unable to create socket $socket_prename"); rename($socket_prename, $socket_name) or unlink($socket_prename), throw Courier::Error("Unable to rename socket $socket_prename to $socket_name"); chmod(0660, $socket_name) or throw Courier::Error("Unable to chmod socket $socket_name"); IO::Handle->new_from_fd(3, '>')->close(); my $filter = { name => $name, mandatory => $mandatory, threads => $threads, logger => $logger, modules => $modules, trusting => $trusting, testing => $testing, debugging => $debugging, socket => $socket, socket_name => $socket_name, terminate => FALSE }; return bless($filter, $class); }
sub destroy { my ($filter) = @_; return if not $filter->{terminate}; $filter->{'socket'}->close(); unlink($filter->{socket_name}); # Dissolve worker thread pool: # foreach my $thread (threads->list) { # $thread->join() # if $thread->tid and $thread != threads->self; # } return; }
sub run { my ($filter) = @_; my $class = ref($filter); my $socket = $filter->{'socket'}; my $select = IO::Select->new(\*STDIN, $socket); while (not $filter->{terminate}) { # Wait for incoming connection requests # or EOF from STDIN: ######################################## my @ready_handles = $select->can_read(); foreach my $handle (@ready_handles) { if ($handle == $socket) { # Incoming connection request. $filter->handle_connection($socket); # threads->new(\&handle_connection, $filter, $socket)->detach(); } elsif ($handle == \*STDIN and STDIN->eof()) { # STDIN got closed. $filter->{terminate} = TRUE; } else { # Received data from unknown handle or from STDIN. # This shouldn't happen. throw Courier::Error("Received data from unknown handle or from STDIN"); } } } return; }
sub handle_connection { my ($filter, $socket) = @_; my $class = ref($filter); my $connection = $socket->accept(); my $message_file_name; my @control_file_names; while (my $file_name = <$connection>) { chomp($file_name); last unless $file_name; # Normalize file name: $file_name = Courier::Config->runtime_dir . '/tmp/' . $file_name if $file_name !~ m(^/); if (not defined($message_file_name)) { $message_file_name = $file_name; } else { push(@control_file_names, $file_name); } } return if not defined($message_file_name); my $message = Courier::Message->new( file_name => $message_file_name, control_file_names => \@control_file_names, filter => $filter ); my ($result, $code); # BEGIN XXX #STDERR->print( # "DEBUG: authenticated_user = '" . # ($message->authenticated_user || '(undef)') . # "'\n" #); # END XXX ($result, $code) = $filter->consult_modules($filter->modules, $message) if $filter->testing or not ($filter->trusting and $message->trusted); ($result, $code) = (undef, undef) if $filter->testing or ($filter->trusting and $message->trusted); if ($result) { $code ||= 550; } else { $result = 'Ok'; $code ||= 200; } my @lines = split(/\n/, $result); my $last_line = pop(@lines); $connection->print("$code-$_\n") foreach @lines; $connection->print("$code $last_line\n"); $connection->close(); return ($result, $code); }
sub consult_modules { my ($filter, $modules, $message) = @_; ref($modules) eq 'ARRAY' or throw Courier::Error('Invalid modules group structure, array-ref expected'); foreach my $module (@$modules) { my ($result, @code); if (UNIVERSAL::isa($module, 'Courier::Filter::Module')) { # Single module, make it consider the message: next if $module->trusting and $message->trusted; # ...except when the module trusts the message. my $logger = $module->logger || $filter->logger; ($result, @code) = eval { $module->consider($message) }; if ($@) { $logger->log_error(ref($module) . ': ' . $@) if $logger; ($result, @code) = ('Mail filters temporarily unavailable.', 432); } # Log rejection: $logger->log_rejected_message($message, $result) if $result and $logger; # Ignore result if module is in testing mode: next if $module->testing; } else { # Something else, try to interpret it as a modules group: ($result, @code) = $filter->consult_modules($module, $message); } return $result ? ($result, @code) : undef if defined($result); } return undef; }
sub name { my ($filter) = @_; # Read-only! return $filter->{name}; }
sub mandatory { my ($filter) = @_; # Read-only! return $filter->{mandatory}; }
sub threads { my ($filter) = @_; # Read-only! return $filter->{threads}; }
sub logger { my ($filter, @logger) = @_; $filter->{logger} = $logger[0] if @logger; return $filter->{logger}; }
sub modules { my ($filter, @modules) = @_; $filter->{modules} = $modules[0] if @modules; return $filter->{modules}; }
sub trusting { my ($filter) = @_; # Read-only! return $filter->{trusting}; }
sub testing { my ($filter) = @_; # Read-only! return $filter->{testing}; }
sub debugging { my ($filter, @debugging) = @_; $filter->{debugging} = $debugging[0] if @debugging; return $filter->{debugging}; }
BEGIN { no warnings 'once'; *DESTROY = \&destroy; }
TRUE;