Up Relaydelay Patch Relaydelay Script Addlist Script

This patches relaydelay to run as a daemon, and to log messages to a file instead of to stdout.


diff -c -N relaydelay-0.04-orig/relaydelay relaydelay-0.04/relaydelay
*** relaydelay-0.04-orig/relaydelay 1969-12-31 16:00:00.000000000 -0800
--- relaydelay-0.04/relaydelay 2003-07-29 00:35:08.000000000 -0700
***************
*** 0 ****
--- 1,49 ----
+ #!/bin/sh
+ #
+ # relaydelay This script starts and stops the relaydelay.pl greylisting milter
+ #
+ # chkconfig: 2345 80 30
+ #
+ # description: relaydelay.pl is a Sendmail Milter that is used to tempfail \
+ # incoming SMTP mail if it hasn't previously seen the same \
+ # combination of relay, sender and recipient.
+
+ # Source function library.
+ . /etc/rc.d/init.d/functions
+
+ CONFIG=/etc/mail/relaydelay.conf
+
+ case "$1" in
+ start)
+ echo -n "Starting relaydelay: "
+ /usr/local/sbin/relaydelay.pl $CONFIG
+ RETVAL=$?
+ if [ $RETVAL -eq 0 ]; then
+ success startup
+ touch /var/lock/relaydelay
+ else
+ failure startup
+ fi
+ echo
+ ;;
+
+ stop)
+ echo -n "Shutting down relaydelay: "
+ kill -s QUIT `cat /var/run/relaydelay.pid`
+ RETVAL=$?
+ if [ $RETVAL -eq 0 ]; then
+ success shutdown
+ rm -f /var/lock/relaydelay
+ else
+ failure shutdown
+ fi
+ echo
+ ;;
+
+ *)
+ echo ""
+ echo "Usage: `basename $0` { start | stop }"
+ echo ""
+ ;;
+ esac
+
diff -c -N relaydelay-0.04-orig/relaydelay.conf relaydelay-0.04/relaydelay.conf
*** relaydelay-0.04-orig/relaydelay.conf 2003-07-21 17:30:59.000000000 -0700
--- relaydelay-0.04/relaydelay.conf 2003-07-29 00:21:18.000000000 -0700
***************
*** 23,28 ****
--- 23,30 ----
# If you do/don't want to see debugging messages printed to stdout,
# then set this appropriately.
$verbose = 1;
+ $run_as_daemon = 1;
+ $log_file = '/var/log/relaydelay.log';

# Database connection params
$database_type = 'mysql';
diff -c -N relaydelay-0.04-orig/relaydelay.pl relaydelay-0.04/relaydelay.pl
*** relaydelay-0.04-orig/relaydelay.pl 2003-07-21 17:30:59.000000000 -0700
--- relaydelay-0.04/relaydelay.pl 2003-07-29 00:32:46.000000000 -0700
***************
*** 37,42 ****
--- 37,43 ----
use Errno qw(ENOENT);

use DBI;
+ use FileHandle;

use strict;

***************
*** 54,59 ****
--- 55,63 ----
# If you do/don't want to see debugging messages printed to stdout,
# then set this appropriately.
my $verbose = 1;
+ my $run_as_daemon = 0; # Default is to still run in the foreground
+ my $log_file = ''; # Default is to use STDOUT for messages.
+ # A log file should be used for daemonized operation.

# Database connection params
my $database_type = 'mysql';
***************
*** 921,928 ****
# make sure the config is only loaded once per instance
return if ($config_loaded);

- print "Loading Config File: $config_file\n";
-
# Read and setup our configuration parameters from the config file
my($msg);
my($errn) = stat($config_file) ? 0 : 0+$!;
--- 925,930 ----
***************
*** 935,945 ****
#do $config_file;
if ($@ ne '') { die "Error in config file $config_file: $@" }

! $config_loaded = 1;
! }




my %my_callbacks =
(
--- 937,960 ----
#do $config_file;
if ($@ ne '') { die "Error in config file $config_file: $@" }

! if ($log_file) { # Keep the output in a log file
! open STDOUT, ">>$log_file" or die "Couldn't redirect STDOUT to $log_file: $!";
! STDOUT->autoflush(1);
! }

+ print "Loaded Config File: $config_file\n" if $verbose;

+ $config_loaded = 1;
+ }

+ sub catch_sig {
+ my $signame = shift;
+ print "Got a SIG$signame.\nClosing DB connection.\n" if $verbose;
+ db_disconnect();
+ print "Exiting relaydelay daemon process.\n";
+ close(STDOUT);
+ exit 0;
+ }

my %my_callbacks =
(
***************
*** 975,1007 ****
# Make sure there are no errors in the config file before we start, and load the socket info
load_config();

! # Record pid to file
! if (defined $relaydelay_pid_file) {
! open(PIDF, ">$relaydelay_pid_file") ||
! die "Unable to record PID to '$relaydelay_pid_file': $!\n";
! print PIDF "$$\n";
! close PIDF;
! }
!
! print "Using connection '$milter_socket_connection' for filter $milter_filter_name\n";

if ($milter_socket_connection =~ /^local:(.+)$/i) {
my $unix_socket = $1;

if (-e $unix_socket) {
! print "Attempting to unlink local UNIX socket '$unix_socket' ... ";

if (unlink($unix_socket) == 0) {
! print "failed.\n";
exit;
}
! print "successful.\n";
}
}

if (not Sendmail::Milter::setconn("$milter_socket_connection")) {
! print "Failed to set up connection: $?\n";
! exit;
}

# Make sure we can connect to the database
--- 990,1014 ----
# Make sure there are no errors in the config file before we start, and load the socket info
load_config();

! print "Using connection '$milter_socket_connection' for filter $milter_filter_name\n" if $verbose;

if ($milter_socket_connection =~ /^local:(.+)$/i) {
my $unix_socket = $1;

if (-e $unix_socket) {
! print "Attempting to unlink local UNIX socket '$unix_socket' ... " if $verbose;

if (unlink($unix_socket) == 0) {
! print "failed.\n" if $verbose;
exit;
}
! print "successful.\n" if $verbose;
}
}

if (not Sendmail::Milter::setconn("$milter_socket_connection")) {
! die "Failed to set up connection: $?\n";
! # exit;
}

# Make sure we can connect to the database
***************
*** 1016,1023 ****
#

if (not Sendmail::Milter::register("$milter_filter_name", \%my_callbacks, SMFI_CURR_ACTS)) {
! print "Failed to register callbacks for $milter_filter_name.\n";
! exit;
}

print "Starting Sendmail::Milter $Sendmail::Milter::VERSION engine.\n";
--- 1023,1063 ----
#

if (not Sendmail::Milter::register("$milter_filter_name", \%my_callbacks, SMFI_CURR_ACTS)) {
! die "Failed to register callbacks for $milter_filter_name.\n";
! # exit;
! }
!
! if ($run_as_daemon) {
! if (not $log_file) {print "Warning: Running as a daemon, but output has not been redirected to a log file.\n";}
! # I don't think we have to worry about reaping zombies since we're only
! # spawning a single child process then exiting:
! # $SIG{CHLD} = 'IGNORE'; # Automatically reap children
! defined(my $child_pid = fork()) or die "Couldn't fork daemon process:$!";
! if ($child_pid) {
! # I must be the parent:
! print "Spawned relaydelay daemon process $child_pid.\n" if $verbose;
! exit 0;
! }
! }
!
! # I must be the child (or using foreground operation):
! # Record pid to file
! if (defined $relaydelay_pid_file) {
! open(PIDF, ">$relaydelay_pid_file") ||
! die "Unable to record PID to '$relaydelay_pid_file': $!\n";
! print PIDF "$$\n";
! close PIDF;
! }
!
! if ($run_as_daemon) {
! # Be a nice daemon:
! POSIX::setsid or die "Couldn't start a new session: $!";
! chdir '/' or die "Couldn't chdir to /: $!";
! open STDIN, '/dev/null' or die "Couldn't redirect STDIN from /dev/null: $!";
! open STDERR, '>&STDOUT' or die "Couldn't dup STDOUT: $!";
! my $sigset = POSIX::SigSet->new();
! my $action = POSIX::SigAction->new('catch_sig',$sigset,&POSIX::SA_NODEFER);
! POSIX::sigaction(&POSIX::SIGQUIT, $action);
}

print "Starting Sendmail::Milter $Sendmail::Milter::VERSION engine.\n";
***************
*** 1035,1042 ****

# Make sure when threads are recycled that we release the global db connection
END {
! print "Closing DB connection.\n";
! db_disconnect();
}


--- 1075,1084 ----

# Make sure when threads are recycled that we release the global db connection
END {
! if (not $run_as_daemon) { # Signal handler does this in daemon mode
! print "Closing DB connection.\n" if $verbose;
! db_disconnect();
! }
}

Mayn Idea and the M-Light logo are trademarks of Mayn Idea Inc.
Copyright (C) 2009 Mayn Idea, Inc.
All rights reserved.

Last modified 01 Aug 2009