exports_setup.proxy.in 2.7 KB
Newer Older
1 2
#!/usr/bin/perl -wT
use English;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
3
use Errno;
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
use Fcntl ':flock';

#
# Create and /etc/exports file based on current reserved table and project
# members.
#
# usage: exports_setup
#

#
# Configure variables
#
my $TBOPS       = "testbed-ops\@fast.cs.utah.edu";

my $etcdir      = "/etc";
my $exports	= "$etcdir/exports";
my $exportsnew  = "$etcdir/exports.new";
my $exportsback	= "$etcdir/exports.backup";
my $exportshead = "$etcdir/exports.head";
my $exportstail = "$etcdir/exports.tail";
my $dbg		= 0;
my @row;

#
# We don't want to run this script unless its the real version.
#
if ($UID != 0) {
    die("Must be root!");
}

# un-taint path
$ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

$| = 1; #Turn off line buffering on output

40 41 42 43 44 45
#
# Testbed Support libraries
# 
use lib "@prefix@/lib";
use libtestbed;

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
#
# Take our input and write it to the tail file.
#
open(TAIL, ">$exportstail") || fatal("Couldn't open $exportstail\n");
while (<STDIN>) {
    print TAIL $_;
}
close(TAIL);
chmod(0444, $exportstail);

#
# Generate a warning so that no one tries to edit the file by hand
#
open(MAP, ">$exportsnew") || fatal("Couldn't open $exportsnew\n");
print MAP
    "#\n".
    "# ******************************************************************\n".
    "# DO NOT EDIT THIS FILE. IT IS A CREATION, A FIGMENT, A CONTRIVANCE!\n".
    "#\n".
    "# Edit $exportshead, then run exports_setup on paper.\n".
    "# ******************************************************************\n".
    "#\n";
close(MAP);
chmod(0644, $exportsnew);

#
# Now tack on the head part of the file.
#
system("cat $exportshead >> $exportsnew") == 0 or
    fatal("Failed to concat $exportshead to $exportsnew\n");

#
# Now the tail of the file.
# 
system("cat $exportstail >> $exportsnew") == 0 or
    fatal("Failed to concat $exportstail to $exportsnew\n");

#
# Back up the existing exports, and then mv in the new one.
#
system("cp $exports $exportsback") == 0 or
    fatal("Could not back up $exports to $exportsback\n");

system("mv $exportsnew $exports") == 0 or
    fatal("Could not mv $exportsnew to $exports\n");

# Avoid accidental editing.
chmod(0444, $exports);

#
96
# I have little faith in HUPing mountd, but do it anyway.
97 98 99 100 101 102 103
#
$mpid = `cat /var/run/mountd.pid`;
$mpid =~ s/\n//;
# untaint
if ($mpid =~ /^([-\@\w.]+)$/) {
    $mpid = $1;
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
104
if (kill('HUP', $mpid) == 0) {
105
    fatal("Could not kill(HUP) process $mpid (mountd): $!");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
106
}
107

108 109 110 111 112 113
#
# Allow time to react since HUP'ing mountd causes all mounts to briefly
# become invalid, and this causes problems for our scripts (and for users).
#
sleep(1);

114 115 116 117 118
exit(0);

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

119
    SENDMAIL($TBOPS, "Exports Setup Failed", $msg);    
120 121 122
    die($msg);
}