#!/usr/bin/perl -wT use English; use Fcntl ':flock'; # # Insert new version of testbed emails lists into place on operations node. # The single argument indicates which list is being piped into the script # from the control node. We create a new alias in the aliases file as well. # # NB: We do not prune dead lists yet. # # usage: genelists.proxy # sub usage() { print STDOUT "Usage: genelists.proxy \n". "Generate email list file after things change!\n"; exit(-1); } # # Configure variables # my $TBOPS = "@TBOPSEMAIL@"; my $maildir = "/etc/mail"; my $listdir = "$maildir/lists"; my $tempfile = "$listdir/tempfile.$$"; my $aliasfile = "$maildir/aliases"; my $listname; my $thelist; # # We don't want to run this script unless its the real version. # if ($UID != 0) { die("*** $0:\n". " Must be root!\n"); } # un-taint path $ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin'; delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'}; # # Turn off line buffering on output # $| = 1; # Load the Testbed support stuff. use lib "@prefix@/lib"; use libtestbed; if (@ARGV != 1) { usage(); } $listname = $ARGV[0]; # # Untaint the arguments. # if ($listname =~ /^([-\@\w]+)$/) { $listname = $1; } else { die("Tainted argument $listname!\n"); } $thelist = "$listdir/$listname"; # # Take our input and write it to the temp file. # open(TMP, ">$tempfile") || fatal("Couldn't open $tempfile\n"); while () { print TMP $_; } close(TMP); chmod(0644, $tempfile); # # Now overwrite the real file. # system("/bin/mv $tempfile $thelist") == 0 || fatal("Could not move $tempfile to $thelist: $!"); # # See if the aliases exists in the alias file. If not, append it and # rebuild with newaliases. # if (system("egrep -q -s '^${listname}:' $aliasfile")) { print "Adding new alias $listname to aliases file\n"; open(ALIASES, ">>$aliasfile") or fatal("opening $aliasfile to append new alias: $!"); printf ALIASES "%-24s%s\n", "${listname}:", ":include:${thelist}"; close(ALIASES) or fatal("Could not close $aliasfile after modification: $!"); system("newaliases") == 0 or fatal("Could not rebuild the aliases database!"); } exit(0); sub fatal { local($msg) = $_[0]; SENDMAIL($TBOPS, "genelists.proxy failure", $msg); die("*** $0:\n". " $msg\n"); }