\n";
local $onclick = "onClick='return check_clicks(form)'"
if (defined(&check_clicks_function));
print "
\n";
print "
\n";
if (&has_command($config{'sa_learn'})) {
print "
\n";
}
print "
\n";
print "
\n";
print "
\n";
}
# restart_spamd()
# Re-start all SpamAssassin processes, or return an error message
sub restart_spamd
{
if ($config{'restart_cmd'}) {
local $out = &backquote_logged(
"$config{'restart_cmd'} 2>&1 $out";
}
}
else {
local @pids = &get_process_pids();
@pids || return $text{'apply_none'};
local $p;
foreach $p (@pids) {
&kill_logged("HUP", $p->[0]);
}
}
return undef;
}
# find_spam_recipe(&recipes)
# Returns the recipe that runs spamassassin
sub find_spam_recipe
{
local $r;
foreach $r (@{$_[0]}) {
if ($r->{'action'} =~ /spamassassin/i ||
$r->{'action'} =~ /spamc/i) {
return $r;
}
}
return undef;
}
# find_file_recipe(&recipes)
# returns the recipe for delivering mail based on the x-spam-status header
sub find_file_recipe
{
local ($r, $c);
foreach $r (@{$_[0]}) {
foreach $c (@{$r->{'conds'}}) {
if ($c->[1] =~ /x-spam-status/i) {
return $r;
}
}
}
return undef;
}
# find_delete_recipe(&recipes)
# returns the recipe for delete mail based on the x-spam-level header, and
# the level it deletes at.
sub find_delete_recipe
{
local ($r, $c);
foreach $r (grep { $_->{'action'} eq '/dev/null' } @{$_[0]}) {
foreach $c (@{$r->{'conds'}}) {
if ($c->[1] =~ /x-spam-level:\s+((\\\*)+)/i) {
return ($r, length($1)/2);
}
}
}
return ( );
}
# find_virtualmin_recipe(&recipes)
# Returns the recipe that runs the Virtualmin lookup command
sub find_virtualmin_recipe
{
local ($r, $c);
foreach $r (@{$_[0]}) {
if ($r->{'action'} =~ /^VIRTUALMIN=/) {
return $r;
}
}
return undef;
}
# find_force_default_receipe(&recipes)
# Returns the recipe that forces delivery to $DEFAULT, used by Virtualmin and
# others to prevent per-user .procmailrc settings
sub find_force_default_receipe
{
local ($r, $c);
foreach $r (@{$_[0]}) {
if ($r->{'action'} eq '$DEFAULT' && !@{$r->{'conds'}}) {
return $r;
}
}
return undef;
}
# get_simple_tests(&conf)
sub get_simple_tests
{
local ($conf) = @_;
local (@simple, %simple);
foreach my $h (&find("header", $conf)) {
if ($h->{'value'} =~ /^(\S+)\s+(\S+)\s+=~\s+\/(.*)\/(\S*)\s*$/) {
push(@simples, { 'header_dir' => $h,
'name' => $1,
'header' => lc($2),
'regexp' => $3,
'flags' => $4, });
$simples{$1} = $simples[$#simples];
}
}
foreach my $b (&find("body", $conf), &find("full", $conf),
&find("uri", $conf)) {
if ($b->{'value'} =~ /^(\S+)\s+\/(.*)\/(\S*)\s*$/) {
push(@simples, { $b->{'name'}.'_dir' => $b,
'name' => $1,
'header' => $b->{'name'},
'regexp' => $2,
'flags' => $3, });
$simples{$1} = $simples[$#simples];
}
}
foreach my $s (&find("score", $conf)) {
if ($s->{'value'} =~ /^(\S+)\s+(\S+)/ && $simples{$1}) {
$simples{$1}->{'score_dir'} = $s;
$simples{$1}->{'score'} = $2;
}
}
foreach my $d (&find("describe", $conf)) {
if ($d->{'value'} =~ /^(\S+)\s+(\S.*)/ && $simples{$1}) {
$simples{$1}->{'describe_dir'} = $d;
$simples{$1}->{'describe'} = $2;
}
}
return @simples;
}
# get_procmail_command()
# Returns the command that should be used in /etc/procmailrc to call
# spamassassin, such as spamc or the full spamassassin path
sub get_procmail_command
{
if ($config{'procmail_cmd'} eq '*') {
# Is spamd running?
if (&get_process_pids()) {
local $spamc = &has_command("spamc");
return $spamc if ($spamc);
}
return &has_command($config{'spamassassin'});
}
elsif ($config{'procmail_cmd'}) {
return $config{'procmail_cmd'};
}
else {
return &has_command($config{'spamassassin'});
}
}
# execute_before(section)
# If a before-change command is configured, run it. If it fails, call error
sub execute_before
{
local ($section) = @_;
if ($config{'before_cmd'}) {
$ENV{'SPAM_SECTION'} = $section;
local $out;
local $rv = &execute_command(
$config{'before_cmd'}, undef, \$out, \$out);
$rv && &error(&text('before_ecmd',
"
".&html_escape($out)."
"));
}
}
# execute_after(section)
# If a after-change command is configured, run it. If it fails, call error
sub execute_after
{
local ($section) = @_;
if ($config{'after_cmd'}) {
$ENV{'SPAM_SECTION'} = $section;
local $out;
local $rv = &execute_command(
$config{'after_cmd'}, undef, \$out, \$out);
$rv && &error(&text('after_ecmd',
"
".&html_escape($out)."
"));
}
}
# check_spamassassin_db()
# Checks if the LDAP or MySQL backend can be contacted, and if not returns
# an error message.
sub check_spamassassin_db
{
if ($config{'mode'} == 0) {
return undef; # Local files always work
}
elsif ($config{'mode'} == 1 || $config{'mode'} == 2) {
# Connect to a database
local $dbh = &connect_spamassasin_db();
return $dbh if (!ref($dbh));
local $testcmd = $dbh->prepare("select * from userpref limit 1");
if (!$testcmd || !$testcmd->execute()) {
undef($connect_spamassasin_db_cache);
$dbh->disconnect();
return &text('connect_equery', "$config{'db'}",
"userpref");
}
$testcmd->finish();
undef($connect_spamassasin_db_cache);
$dbh->disconnect();
return undef;
}
elsif ($config{'mode'} == 3) {
# Connect to LDAP
local $ldap = &connect_spamassassin_ldap();
return $ldap if (!ref($ldap));
local $rv = $ldap->search(base => $config{'base'},
filter => "(uid=$remote_user)",
sizelimit => 1);
if (!$rv || $rv->code) {
return &text('connect_ebase', "$config{'base'}",
$rv ? $rv->error : "Unknown search error");
}
return undef;
}
else {
return "Unknown config mode $config{'mode'} !";
}
}
# connect_spamassasin_db()
# Attempts to connect to the SpamAssasin MySQL or PostgreSQL database. Returns
# a driver handle on success, or an error message string on failure.
sub connect_spamassasin_db
{
if (defined($connect_spamassasin_db_cache)) {
return $connect_spamassasin_db_cache;
}
local $driver = $config{'mode'} == 1 ? "mysql" : "Pg";
local $drh;
eval <install_driver(\$driver);
EOF
if ($@) {
return &text('connect_edriver', "DBD::$driver");
}
local $dbistr = &make_dbistr($driver, $config{'db'}, $config{'server'});
local $dbh = $drh->connect($dbistr,
$config{'user'}, $config{'pass'}, { });
$dbh || return &text('connect_elogin',
"$config{'db'}", $drh->errstr)."\n";
$connect_spamassasin_db_cache = $dbh;
return $dbh;
}
# connect_spamassassin_ldap()
# Attempts to connect to the configured LDAP DB, and returns the handle on
# success, or an error message on failure.
sub connect_spamassassin_ldap
{
if (defined($connect_spamassasin_ldap_cache)) {
return $connect_spamassasin_ldap_cache;
}
eval "use Net::LDAP";
if ($@) {
return &text('connect_eldapmod', "Net::LDAP");
}
local $port = $config{'port'} || 389;
local $inet6 = !&to_ipaddress($config{'server'}) &&
&to_ip6address($config{'server'});
local $ldap = Net::LDAP->new($config{'server'},
port => $port,
inet6 => $inet6);
if (!$ldap) {
return &text('connect_eldap', "$config{'server'}", $port);
}
local $mesg = $ldap->bind(dn => $config{'user'}, password => $config{'pass'});
if (!$mesg || $mesg->code) {
return &text('connect_eldaplogin', "$config{'server'}",
"$config{'user'}",
$mesg ? $mesg->error : "Unknown error");
}
$connect_spamassasin_ldap_cache = $ldap;
return $ldap;
}
sub make_dbistr
{
local ($driver, $db, $host) = @_;
local $rv;
if ($driver eq "mysql") {
$rv = "database=$db";
}
elsif ($driver eq "Pg") {
$rv = "dbname=$db";
}
else {
$rv = $db;
}
if ($host) {
$rv .= ";host=$host";
}
return $rv;
}
# get_ldap_user(&ldap, [username])
# Returns the LDAP object for a user, or undef if not found
sub get_ldap_user
{
local ($ldap, $user) = @_;
$user ||= $database_userpref_name;
#if (exists($get_ldap_user_cache{$user})) {
# return $get_ldap_user_cache{$user};
# }
local $rv = $ldap->search(base => $config{'base'},
filter => "($ldap_username_attr=$user)",
);
if (!$rv || $rv->code) {
&error(&text('eldap', $rv ? $rv->error : "Search failed"));
}
local ($uinfo) = $rv->all_entries;
$get_ldap_user_cache{$user} = $uinfo;
return $uinfo;
}
# get_auto_whitelist_file([user])
# Returns the base path to the auto whitelist DBM, if any.
sub get_auto_whitelist_file
{
local ($user) = @_;
local @uinfo = $module_info{'usermin'} ? @remote_user_info :
$user ? getpwnam($user) : ( );
local $conf = &get_config();
local $awp = &find("auto_whitelist_path", $conf);
if (!$awp) {
$awp = &find_default("auto_whitelist_path");
}
$awp ||= "~/.spamassassin/auto-whitelist";
if ($awp !~ /^\//) {
# Make absolute
return undef if (scalar(@uinfo) == 0);
$awp =~ s/^(\~|\$HOME)\//$uinfo[7]\//;
if ($awp !~ /^\//) {
$awp = "$uinfo[7]/$awp";
}
}
# Does it exist?
if (!-r $awp) {
local @real = glob("$awp.*");
$awp = undef if (!@real);
}
# Is it under the user's home?
if (!&is_under_directory($uinfo[7], $awp)) {
$awp = undef;
}
return $awp;
}
# open_auto_whitelist_dbm([user])
# Ties the %awl hash to the autowhitelist DBM file. Returns 1 if successful, or
# 0 if it could not be opened, or -1 if empty.
sub open_auto_whitelist_dbm
{
local ($user) = @_;
local $awp = &get_auto_whitelist_file($user);
return 0 if (!$awp);
local $anyok;
foreach my $cls ('DB_File', 'GDBM_File', 'SDBM_File') {
$@ = undef;
eval "use $cls";
next if ($@);
tie(%awl, $cls, $awp, O_RDWR, 0755) || next;
if (scalar(keys %awl)) {
return 1;
}
$anyok = 1;
}
return $anyok ? -1 : 0;
}
# close_auto_whitelist_dbm()
# Disconnects the global %awl hash from the DBM file, flushing changes to disk
sub close_auto_whitelist_dbm
{
untie(%awl);
}
# supports_auto_whitelist()
# Returns 1 if SpamAssassin is doing auto-whitelisting for the current user,
# 2 if for multiple users.
sub supports_auto_whitelist
{
if ($module_info{'usermin'}) {
return &get_auto_whitelist_file() ? 1 : 0;
}
else {
return 2;
}
}
sub can_edit_awl
{
local ($user) = @_;
return 1 if ($module_info{'usermin'}); # Only one user anyway
if ($access{'awl_users'}) {
# Check if on user list
return &indexof($user, split(/\s+/, $access{'awl_users'})) >= 0;
}
elsif ($access{'awl_groups'}) {
# Check if the user is a member of any of the allowed groups
local %ugroups;
local @uinfo = getpwnam($user);
return 0 if (!scalar(@uinfo));
local @ginfo = getgrgid($uinfo[3]);
$ugroups{$ginfo[0]}++ if (scalar(@ginfo));
foreach my $o (&other_groups($user)) {
$ugroups{$o}++;
}
local @can = grep { $ugroups{$_} } split(/\s+/, $access{'awl_groups'});
return @can ? 1 : 0;
}
else {
# No restrictions
return 1;
}
}
# list_spamassassin_languages()
# Returns a list of language codes and descriptions
sub list_spamassassin_languages
{
local @rv;
open(LANGS, "<$module_root_directory/langs");
while() {
if (/^(\S+)\s+(.*)/) {
push(@rv, [ $1, $2 ]);
}
}
close(LANGS);
return @rv;
}
# list_spamassassin_locales()
# Returns a list of locale codes and descriptions
sub list_spamassassin_locales
{
local @rv;
open(LANGS, "<$module_root_directory/locales");
while() {
if (/^(\S+)\s+(.*)/) {
push(@rv, [ $1, $2 ]);
}
}
close(LANGS);
return @rv;
}
# list_spamassassin_plugins()
# Returns a list of plugins enabled, both globally and for this user
sub list_spamassassin_plugins
{
my @rv;
if ($config{'global_cf'}) {
my $gconf = &get_config($config{'global_cf'}, 1);
push(@rv, &find_value("loadplugin", $gconf));
}
my $conf = &get_config();
push(@rv, &find_value("loadplugin", $conf));
return @rv;
}
1;