exports_setup.proxy.in 3.47 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3 4

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
6 7 8
# All rights reserved.
#

9
use English;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
10
use Errno;
11
use Fcntl ':flock';
12
use Getopt::Std;
13 14 15 16 17 18 19 20

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

21 22 23
my %opts = ();
getopts('S', \%opts);

24 25 26
#
# Configure variables
#
27
my $TBOPS       = "@TBOPSEMAIL@";
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
my $etcdir;
my $exports;
my $exportsnew;
my $exportsback;
my $exportshead;
my $exportstail;
my $pidfile;
my $daemon;

# Are we modifying the Samba config file or the NFS exports?
if (defined($opts{'S'})) {
    $etcdir      = "/usr/local/etc";
    $exports	 = "$etcdir/smb.conf";
    $exportsnew  = "$etcdir/smb.conf.new";
    $exportsback = "$etcdir/smb.conf.backup";
    $exportshead = "$etcdir/smb.conf.head";
    $exportstail = "$etcdir/smb.conf.tail";
    $pidfile     = "/var/run/smbd.pid";
    $daemon      = "smbd";
}
else {
    $etcdir      = "/etc";
    $exports	 = "$etcdir/exports";
    $exportsnew  = "$etcdir/exports.new";
    $exportsback = "$etcdir/exports.backup";
    $exportshead = "$etcdir/exports.head";
    $exportstail = "$etcdir/exports.tail";
    $pidfile     = "/var/run/mountd.pid";
    $daemon      = "mountd";
}

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
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

76 77 78 79 80 81
#
# Testbed Support libraries
# 
use lib "@prefix@/lib";
use libtestbed;

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
#
# 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);

#
132
# I have little faith in HUPing mountd, but do it anyway.
133
#
134 135
my $daemonpid = `cat $pidfile`;
$daemonpid =~ s/\n//;
136
# untaint
137 138
if ($daemonpid =~ /^([-\@\w.]+)$/) {
    $daemonpid = $1;
139
}
140 141
if (kill('HUP', $daemonpid) == 0) {
    fatal("Could not kill(HUP) process $daemonpid ($daemon): $!");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
142
}
143

144 145 146 147 148 149
#
# 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);

150 151 152 153 154
exit(0);

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

155
    SENDMAIL($TBOPS, "Exports Setup Failed", $msg);    
156 157 158
    die($msg);
}