genelists.proxy.in 3.14 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh Stoller's avatar
Leigh Stoller committed
2 3

#
4
# Copyright (c) 2000-2002, 2005 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file 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 Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
Leigh Stoller's avatar
Leigh Stoller committed
24
#
25 26 27 28 29 30
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
31
# from the control node. We create a new alias in the aliases file as well.
32
#
33 34 35
# NB: We do not prune dead lists yet.
#
# usage: genelists.proxy <list name>
36 37
#
sub usage() {
38 39
    print STDOUT "Usage: genelists.proxy <list name>\n".
	"Generate email list file after things change!\n";
40 41 42 43 44 45 46 47
    exit(-1);
}

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

48 49 50 51 52
my $maildir     = "/etc/mail";
my $listdir     = "$maildir/lists";
my $tempfile    = "$listdir/tempfile.$$";
my $aliasfile   = "$maildir/aliases";
my $listname;
53 54 55 56 57 58
my $thelist;

#
# We don't want to run this script unless its the real version.
#
if ($UID != 0) {
59 60
    die("*** $0:\n".
	"    Must be root!\n");
61 62 63 64 65 66 67 68 69 70 71
}

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

72 73 74
# Protos
sub fatal($);

75 76 77 78
# Load the Testbed support stuff.
use lib "@prefix@/lib";
use libtestbed;

79
if (@ARGV != 1) {
80 81
    usage();
}
82 83 84 85 86 87 88
$listname = $ARGV[0];

#
# Untaint the arguments.
#
if ($listname =~ /^([-\@\w]+)$/) {
    $listname = $1;
89 90
}
else {
91
    die("Tainted argument $listname!\n");
92
}
93
$thelist = "$listdir/$listname";
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

#
# 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: $!");

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
#
# 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!");
}

131 132
exit(0);

133 134
sub fatal($) {
    my ($msg) = @_;
135

136
    SENDMAIL($TBOPS, "genelists.proxy failure", $msg);
137 138
    die("*** $0:\n".
	"    $msg\n");
139 140
}