You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@spamassassin.apache.org by Raul Dias <ra...@dias.com.br> on 2004/04/13 03:15:18 UTC
Local Eval Support
Hi,
I have seen for some time people wanting to add its own eval functions
to be used as a rule by SA.
So far the only way to do it is to hack EvalTests.pm and append code to
it.
The drawback is obvious. You lose your changes on a SA upgrade.
OTOH, allowing to add custom eval rules allows for higher quality tests
and a much improved Community supported "plugins".
So I decided to add this feature to sa (as I needed it :-).
Below is my patch that adds a new config option:
local_eval_tests_dir
which points to a dir where .pm modules will reside.
For security reasons this dir should be 0755 and owned by root.
The modules should be 0644 and owned by root.
You can use custom rules the same way with built in evals:
e.g. header MY_TEST eval:local_test(BLA)
One requirement is that all local eval tests should be called with a '
'local_' prefix to avoid clash of name space.
When writting the modules you don't need the 'local_' prefix.
Also, the modules should reside in
Mail::SpamAssassin::EvalTests::Local::filename
filename is the module's name without the .pm part.
Also, the modules are checked ordered by sort() when looking for a
custom method. The first one found wins the name space. So, I suggest
to keep you own prefix to methods name (as custom rules have).
Now, remember that you are adding code to SpamAssassin. This feature is
for the advanced user only.
Unchecked, verified, anonymous modules might:
- break sa
- deletes all you mail
- format your HD
- put fire on your room
So, really check what you are adding to your sa installation.
Trust no one (not even my patch).
That said, good luck.
Raul Dias
----- local_eval_patch---------8<----------------------
diff -Nru Mail-SpamAssassin-2.63/lib/Mail/SpamAssassin/Conf.pm
Mail-SpamAssassin-2.63_local_eval/lib/Mail/SpamAssassin/Conf.pm
--- Mail-SpamAssassin-2.63/lib/Mail/SpamAssassin/Conf.pm
2004-01-17 21:56:08.000000000 -0200
+++ Mail-SpamAssassin-2.63_local_eval/lib/Mail/SpamAssassin/Conf.pm
2004-04-12 21:21:44.000000000 -0300
@@ -90,6 +90,7 @@
use vars qw{
@ISA $VERSION
+ @LOCAL_EVAL_MODULES
};
@ISA = qw();
@@ -238,6 +239,9 @@
$self->{user_scores_sql_field_value} = 'value';
$self->{user_scores_sql_field_scope} = 'spamassassin'; # probably
shouldn't change this
+ #Local Eval Tests
+ $self->{local_eval_tests_dir} = '';
+
# for backwards compatibility, we need to set the default headers
# remove this except for X-Spam-Checker-Version in 2.70
$self->add_default_spam_headers(); # always run this first
@@ -2515,6 +2519,28 @@
$self->{user_scores_sql_field_scope} = $1; next;
}
+=item local_eval_tests_dir modules_dir
+
+This option if used, define a directory where local modules will be
placed.
+The modules in this dir should have the package scope of
Mail::SpamAssassin::EvalTests::Local::C<_filename_> .
+Where C<_filename_> is the module's filename without the path and
without the .pm.
+The methods in this modules will be able to be called from eval tests
with C<local_> prepended to its
+name.
+For security reasons, the C<modules_dir> should be set 0755 and owned
by root. The modules should be
+0644 and owned by root.
+Default: C<No local eval tests>.
+
+=cut
+
+ if(/^local_eval_tests_dir\s+(\S+)$/) {
+ $self->{local_eval_tests_dir} = $1;
+ load_eval_modules($1);
+ next;
+ }
+
+
+
+
###########################################################################
failed_line:
@@ -2740,6 +2766,37 @@
return 0;
}
+#load local modules
+sub load_eval_modules {
+ my $path = shift;
+ my $module;
+
+ #security check
+ #path has to be owned by root and 0700
+ unless ( (stat($path))[2] == 16877 && !(stat($path))[4] ) {
+ die "The local module path '$path' is not secure enough.\n";
+ return;
+ }
+
+ foreach $module (sort(glob("$path/*.pm"))) {
+
+ #untaint $module
+ $module = Mail::SpamAssassin::Util::untaint_file_path($module);
+
+ #security check
+ unless ( (stat($module))[2] == 33188 && !(stat($module))[4] ) {
+ warn "The local module '$module' is not secure enough.\n";
+ next;
+ }
+
+ require $module;
+
+ $module =~ s/^$path\/*(.*).pm$/$1/;
+ push @LOCAL_EVAL_MODULES, ($module);
+ }
+
+}
+
###########################################################################
sub dbg { Mail::SpamAssassin::dbg (@_); }
diff -Nru Mail-SpamAssassin-2.63/lib/Mail/SpamAssassin/EvalTests.pm
Mail-SpamAssassin-2.63_local_eval/lib/Mail/SpamAssassin/EvalTests.pm
--- Mail-SpamAssassin-2.63/lib/Mail/SpamAssassin/EvalTests.pm
2004-01-17 21:56:08.000000000 -0200
+++
Mail-SpamAssassin-2.63_local_eval/lib/Mail/SpamAssassin/EvalTests.pm 2004-04-12 21:14:43.000000000 -0300
@@ -25,6 +25,7 @@
$ROUND_THE_WORLD_RELAYERS
$WORD_OBFUSCATION_CHARS
$CHARSETS_LIKELY_TO_FP_AS_CAPS
+ $AUTOLOAD
};
# sad but true. sort it out, sysadmins!
@@ -2920,4 +2923,42 @@
0;
}
+#to avoid catch by AUTOLOAD
+sub DESTROY {}
+
+#We use the AUTOLOAD function to load (if available a local eval test)
+sub AUTOLOAD {
+ my ($self) = @_;
+ my $module;
+ my $path = $self->{conf}->{local_eval_tests_dir};
+ my $real_function = $AUTOLOAD;
+ my $package = __PACKAGE__;
+
+ #package might not always be qualified
+ $real_function =~ s/^${package}:://;
+ $real_function =~ s/local_//;
+
+ #local eval starts with local_
+ #die is caught by eval()
+ if ( ($AUTOLOAD !~ /^(${package}::)local_/) || (! length ($path)) ) {
+ die "Can't locate object method \"$AUTOLOAD\" via package $package
";
+ }
+
+ #try to fund the method, first match wins
+ foreach $module (@Mail::SpamAssassin::Conf::LOCAL_EVAL_MODULES) {
+ *_test_eval_function =
\&{"Mail::SpamAssassin::EvalTests::Local::${module}::${real_function}"};
+
+ if (defined ( &_test_eval_function ) ){
+ #make this function permanent
+ no strict 'refs'; #on this block only
+ *$real_function = \&_test_eval_function;
+ return &_test_eval_function(@_);
+ }
+ }
+
+ #the method does not exist
+ die "The local method '$real_function()' does not exist.\n";
+
+}
+
1;
----------------------------->8------------------------
Re: Local Eval Support
Posted by Raul Dias <ra...@dias.com.br>.
Ouch!
I should have search better for that. :)
OTOH, it can still provide a starting point until 3.0 can be used in a
production enviroment.
Raul Dias
On Mon, 2004-04-12 at 22:51, Fred wrote:
> Hello,
> This feature was already added a month ago or more, it's in the current SVN
> release. The public spoke and the dev's listened ;)
>
>
> Raul Dias wrote:
> > Hi,
> >
> > I have seen for some time people wanting to add its own eval functions
> > to be used as a rule by SA.
> > So far the only way to do it is to hack EvalTests.pm and append code to
> > it.
> > The drawback is obvious. You lose your changes on a SA upgrade.
> >
Re: Local Eval Support
Posted by Fred <sp...@freddyt.com>.
Hello,
This feature was already added a month ago or more, it's in the current SVN
release. The public spoke and the dev's listened ;)
Raul Dias wrote:
> Hi,
>
> I have seen for some time people wanting to add its own eval functions
> to be used as a rule by SA.
> So far the only way to do it is to hack EvalTests.pm and append code to
> it.
> The drawback is obvious. You lose your changes on a SA upgrade.
>