#!/usr/bin/perl -w
# $id: installRDJ, v 1.1.0 2006/11/13
# Script to simplify the redundant install of Rules Du Jour
# Copyright (C) 2005,2006 BJ Dierkes <[email protected]>
# Modified to work with DirectAdmin by Enrique Mertins <[email protected]>
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
#
# INFORMATION:
#
# There is nothing to be said here really
#
# SpamAssassin: [url]http://spamassassin.apache.org/[/url]
# Rules Du Jour: [url]http://www.exit0.us/index.php?pagename=RulesDuJour[/url]
# Rules Emporium: [url]http://www.rulesemporium.com/[/url]
# Directadmin: [url]http://www.directadmin.com/[/url]
# Please note that you need to have Directadmin's SA version installed for this script to work,
# Refer to the How to Install SA in Rules Du Jour
# Although you can use it for any version by using the --sa_dir option , it has not been tested!
use strict;
use Getopt::Long;
my $error = "";
my %configOptions = ();
my %inputOptions = ();
# Set shell commands
my %CMD = (
"wget" => "/usr/bin/wget -q",
);
# Set Default Options
&setDefaultOptions();
# Get Options from the command line
GetOptions(
"install" => \$inputOptions{install},
"directadmin" => \$inputOptions{directadmin},
"directadmin_debian" => \$inputOptions{directadmin_debian},
"admin_email=s" => \$inputOptions{admin_email},
"sa_dir=s" => \$inputOptions{sa_dir},
"sa_restart=s" => \$inputOptions{sa_restart},
"rdj_url=s" => \$inputOptions{rdj_url},
"rdj_config_dir=s" => \$inputOptions{rdj_config_dir},
"rdj_script_path=s" => \$inputOptions{rdj_script_path},
"cron_file=s" => \$inputOptions{cron_file},
"cron_hour=s" => \$inputOptions{cron_hour},
"list_defaults" => \$inputOptions{list_defaults},
"help" => \$inputOptions{help}
);
# Do It
if (defined $inputOptions{help}) {
&helpMenu();
} elsif (defined $inputOptions{list_defaults}) {
&setDefaultOptions;
foreach (keys %configOptions) {
print $_ . " = " . $configOptions{$_} . "\n";
}
} elsif (defined $inputOptions{install}) {
if(defined $inputOptions{directadmin}) {
$configOptions{sa_dir} = "/etc/mail/spamassassin/";
$configOptions{sa_restart} = "/sbin/service exim restart";
}
if(defined $inputOptions{directadmin_debian}) {
$configOptions{sa_dir} = "/etc/mail/spamassassin/";
$configOptions{sa_restart} = "/etc/init.d/exim restart";
}
if(&setFinalOptions()) {
if(&checkVars()) {
print "\n";
if(&install()) {
print "\n\n";
print "[FAILURE] There was an error during installation\n";
print $error;
print "\n\n";
exit 1;
} else {
print "\n";
print "[SUMMARY] The script completed successfully\n";
print " + RDJ SCRIPT: $configOptions{rdj_script_path}\n";
print " + RDJ CONFIG: " . $configOptions{rdj_config_dir} . "/config\n";
print " + RDJ CRONFILE: " . $configOptions{cron_file} . "\n";
print " + RDJ CRONHOUR: " . $configOptions{cron_hour} . "\n";
print " + SA RULES DIR: " . $configOptions{sa_dir} . "\n";
print "\n";
print "[CONFIGURATION NOTE] Please read the following\n";
print " + Please note that Rules Du Jour has been configured with\n";
print " + fairly strict TRUSTED_RULESETS. Please edit the configuration\n";
print " + file to modify this setting\n\n";
print "\n\n";
print " + Would you like to run rules_du_jour now? (type \"yes\"): ";
my $response = <>;
chomp $response;
if ($response eq "yes") {
system($configOptions{rdj_script_path});
} else {
print "\n\n[WHAT?] Invalid response\n";
print " + You did not respond \"yes\". Please run rules_du_jour manually\n";
print " + Command: $configOptions{rdj_script_path}\n";
print " + Thank you, come again\n\n";
}
}
} else {
print "\n";
print "[FAILURE] There was a failure before installation\n";
print $error;
print "\n\n";
exit 1;
}
} else {
print "\n";
print "[FAILURE] There was a failure before installation\n";
print " + Unknown error\n";
print "\n\n";
exit 1;
}
} else {
&helpMenu();
}
#
# Sub Routines
#
sub helpMenu() {
my $option = "";
my $description = "";
my %helpTable;
print "\n";
print "USAGE:\n\n";
print "./installRDJ.pl --install [--flag] [--option=\"value\"]\n";
print "\n----------------------------------------------------------------\n";
format STDOUT =
@<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$option, $description
.
%helpTable = (
"OPTION" => "DESCRIPTION",
"--install" => "Install Rules Du Jour",
"--directadmin" => "Overrides Directadmin Specific Options (--sa_dir, --sa_restart)",
"--directadmin_debian" => "Overrides Directadmin Specific Options For Debian(--sa_dir, --sa_restart)",
"--admin_email" => "Email where reports are sent to (default: root)",
"--sa_dir" => "SpamAssassin Config Dir (default: /etc/spamassassin)",
"--sa_restart" => "Command executed after update",
"--rdj_url" => "URL to the Rules Du Jour script",
"--rdj_config_dir" => "Config dir for Rules Du Jour (default: /etc/rulesdujour)",
"--rdj_script_path" => "Rules Du Jour script path",
"--cron_file" => "File that cronjob is created in",
"--cron_hour" => "Hour that Rules Du Jour runs during the day",
"--list_defaults" => "List The default options to this script",
"--help" => "You're looking at it"
);
foreach (keys %helpTable) {
$option = $_;
$description = $helpTable{$_};
write();
}
print "\n----------------------------------------------------------------\n";
print "\n";
print "Example 1: ./installRDJ.pl --install\n\n";
print "Example 2: ./installRDJ.pl --install --directadmin --admin_email=johnny\@domain.com\n\n";
print "\n----------------------------------------------------------------\n\n";
print "Original Author maybe contacted at:BJ Dierkes <wdierkes\@5dollarwhitebox.org>\n\n";
print "Directadmin Mod questions can be directed to fastattack in the Directadmin Forum \n\n";
}
sub install() {
print "[INSTALL] We are now continuing with installation . . .\n";
# Get the script
my $cmd = $CMD{wget} . " " . $configOptions{rdj_url};
print " + \"$cmd\": ";
if(system($cmd)) {
print "[FAILED]\n";
$error .= " + Command Failed: \"${cmd}\"\n";
return 1;
} else {
print "OK\n";
}
# Install Script
$cmd = "mv rules_du_jour " . $configOptions{rdj_script_path};
print " + \"$cmd\": ";
unless(rename "rules_du_jour", $configOptions{rdj_script_path}) {
print "[FAILED]\n";
$error .= " + Command Failed: \"${cmd}\"\n";
return 1;
} else {
print "OK\n";
}
# Chmod Script
$cmd = "chmod 750 " . $configOptions{rdj_script_path};
print " + \"$cmd\": ";
unless(chmod 0750, $configOptions{rdj_script_path}) {
print "[FAILED]\n";
$error .= " + Command Failed: \"${cmd}\"\n";
return 1;
} else {
print "OK\n";
}
# Create the config
my $config_file = $configOptions{rdj_config_dir} . "/config";
if(!-e $configOptions{rdj_config_dir}) {
print " + \"mkdir $configOptions{rdj_config_dir}\": ";
unless(mkdir $configOptions{rdj_config_dir}, 750) {
print "FAILED";
$error .= " + Failed to create directory: $configOptions{rdj_config_dir}\n";
return 1;
} else {
print "OK\n";
}
} else {
print " + Config dir \"$configOptions{rdj_config_dir}\" already exists\n";
}
if(-e $config_file) {
print " + $config_file exists, moving to ${config_file}.bak: ";
unless(rename "$config_file", "${config_file}.bak") {
print "FAILED\n";
$error .= " + Error backing up \"$config_file\"\n";
return 1;
} else {
print "OK\n";
&createConfigFile();
}
} else {
&createConfigFile();
}
# Create Cron Job
my $cron_file = $configOptions{cron_file};
if (-e $cron_file) {
print " + Cron file \"$cron_file\" exists, moving to \"/root/rules_du_jour_cronjob.bak\": ";
unless(rename "$cron_file", "/root/rules_du_jour_cronjob.bak") {
print "FAILED\n";
$error .= " + Error backing up \"$cron_file\"\n";
return 1;
} else {
print "OK\n";
if(&createCronFile()) {
return 1;
} else {
return 0;
}
}
} else {
if(&createCronFile()) {
return 1;
} else {
return 0;
}
}
}
sub createCronFile() {
my $cron_file = $configOptions{cron_file};
print " + Creating Cronjob File: \"$configOptions{cron_file}\": ";
unless(open(CRONFILE, ">$cron_file")) {
print "FAILED\n";
$error .= " + Error creating cronjob file: \"$cron_file\"\n";
return 1;
} else {
print CRONFILE "1 " . $configOptions{cron_hour} . " * * * root " . $configOptions{rdj_script_path} . " 2>&1 >> /dev/null\n";
close (CRONFILE);
print "OK\n";
return 0;
}
}
sub createConfigFile() {
my $config_file = $configOptions{rdj_config_dir} . "/config";
print " + Creating config file: \"$configOptions{rdj_config_dir}" . "/config\": ";
unless(open(CONFIGFILE, ">$config_file")) {
print "FAILED\n";
$error .= " + Error creating config file: $config_file\n";
return 1;
} else {
print CONFIGFILE "SA_DIR=\"" . $configOptions{sa_dir} . "\"\n";
print CONFIGFILE "MAIL_ADDRESS=\"" . $configOptions{admin_email} . "\"\n";
print CONFIGFILE "SINGLE_EMAIL_ONLY=\"true\"\n";
print CONFIGFILE "SA_RESTART=\"" . $configOptions{sa_restart} . "\"\n\n";
print CONFIGFILE "# Ruleset descriptions found at [url]http://www.rulesemporium.com\n[/url]";
print CONFIGFILE "TRUSTED_RULESETS=\"TRIPWIRE ANTIDRUG SARE_EVILNUMBERS0 RANDOMVAL SARE_ADULT SARE_FRAUD SARE_BML SARE_SPOOF SARE_BAYES_POISON_NXM SARE_OEM SARE_RANDOM SARE_OBFU0 SARE_SPAMCOP_TOP200\"\n";
close (CONFIGFILE);
print "OK\n";
}
}
sub setDefaultOptions() {
# Set the defaults
%configOptions = (
"admin_email" => "root\@localhost",
"sa_dir" => "/etc/mail/spamassassin",
"sa_restart" => "/etc/init.d/spamassassin restart",
"rdj_url" => "http:\/\/sandgnat.com/rdj/rules_du_jour",
"rdj_config_dir" => "/etc/rulesdujour",
"rdj_script_path" => "/usr/local/sbin/rules_du_jour",
"cron_hour" => "1",
"cron_file" => "/etc/cron.d/rules_du_jour"
);
}
# Put the user supplied options into effect
sub setFinalOptions() {
foreach (keys %inputOptions) {
$configOptions{$_} = $inputOptions{$_} if (defined $inputOptions{$_});
}
return 1;
}
# Check variables and settings
sub checkVars () {
print "[CHECK] Checking Vars to avoid error\n";
# Make sure we have a SpamAssassin Installation
print " + " . $configOptions{sa_dir} . " Exist: ";
if (-e $configOptions{sa_dir}) {
print "OK\n";
} else {
print "FAILED\n";
$error .= " + SpamAssassin Dir \"$configOptions{sa_dir}\" doesn't exists\n";
}
# Make sure we have commands
foreach (keys %CMD) {
my @line = split(' ', $CMD{$_});
my $tmp_command = $line[0];
print " + " . $tmp_command . " Exists: ";
if (-e $tmp_command) {
print "OK\n";
} else {
print "FAILED\n";
$error .= " + Command \"$tmp_command\" doesn't exists\n";
}
}
# Make sure the SA_RESTART command exists
my @line = split(' ', $configOptions{sa_restart});
my $tmp_command = $line[0];
print " + " . $tmp_command . " Exists: ";
if (-e $tmp_command) {
print "OK\n";
} else {
print "FAILED\n";
$error .= " + Command \"$tmp_command\" doesn't exists\n";
}
if ($error ne "") {
return 0;
} else {
return 1;
}
}