genelists.proxy.in 2.32 KB
Newer Older
1 2 3 4 5 6 7
#!/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
8
# from the control node. We create a new alias in the aliases file as well.
9
#
10 11 12
# NB: We do not prune dead lists yet.
#
# usage: genelists.proxy <list name>
13 14
#
sub usage() {
15 16
    print STDOUT "Usage: genelists.proxy <list name>\n".
	"Generate email list file after things change!\n";
17 18 19 20 21 22 23 24
    exit(-1);
}

#
# Configure variables
#
my $TBOPS       = "@TBOPSEMAIL@";

25 26 27 28 29
my $maildir     = "/etc/mail";
my $listdir     = "$maildir/lists";
my $tempfile    = "$listdir/tempfile.$$";
my $aliasfile   = "$maildir/aliases";
my $listname;
30 31 32 33 34 35
my $thelist;

#
# We don't want to run this script unless its the real version.
#
if ($UID != 0) {
36 37
    die("*** $0:\n".
	"    Must be root!\n");
38 39 40 41 42 43 44 45 46 47 48
}

# 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;

49 50 51 52
# Load the Testbed support stuff.
use lib "@prefix@/lib";
use libtestbed;

53
if (@ARGV != 1) {
54 55
    usage();
}
56 57 58 59 60 61 62
$listname = $ARGV[0];

#
# Untaint the arguments.
#
if ($listname =~ /^([-\@\w]+)$/) {
    $listname = $1;
63 64
}
else {
65
    die("Tainted argument $listname!\n");
66
}
67
$thelist = "$listdir/$listname";
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

#
# Take our input and write it to the temp file.
#
open(TMP, ">$tempfile") ||
    fatal("Couldn't open $tempfile\n");
while (<STDIN>) {
    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: $!");

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#
# 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!");
}

105 106 107 108 109
exit(0);

sub fatal {
    local($msg) = $_[0];

110
    SENDMAIL($TBOPS, "genelists.proxy failure", $msg);
111 112
    die("*** $0:\n".
	"    $msg\n");
113 114
}