genelists.proxy.in 2.43 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2
3
4
5
6
7

#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2002 University of Utah and the Flux Group.
# All rights reserved.
#
8
9
10
11
12
13
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
14
# from the control node. We create a new alias in the aliases file as well.
15
#
16
17
18
# NB: We do not prune dead lists yet.
#
# usage: genelists.proxy <list name>
19
20
#
sub usage() {
21
22
    print STDOUT "Usage: genelists.proxy <list name>\n".
	"Generate email list file after things change!\n";
23
24
25
26
27
28
29
30
    exit(-1);
}

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

31
32
33
34
35
my $maildir     = "/etc/mail";
my $listdir     = "$maildir/lists";
my $tempfile    = "$listdir/tempfile.$$";
my $aliasfile   = "$maildir/aliases";
my $listname;
36
37
38
39
40
41
my $thelist;

#
# We don't want to run this script unless its the real version.
#
if ($UID != 0) {
42
43
    die("*** $0:\n".
	"    Must be root!\n");
44
45
46
47
48
49
50
51
52
53
54
}

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

55
56
57
58
# Load the Testbed support stuff.
use lib "@prefix@/lib";
use libtestbed;

59
if (@ARGV != 1) {
60
61
    usage();
}
62
63
64
65
66
67
68
$listname = $ARGV[0];

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

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

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#
# 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!");
}

111
112
113
114
115
exit(0);

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

116
    SENDMAIL($TBOPS, "genelists.proxy failure", $msg);
117
118
    die("*** $0:\n".
	"    $msg\n");
119
120
}