webcontrol.in 2.8 KB
Newer Older
1
#!/usr/bin/perl -w
Leigh Stoller's avatar
Leigh Stoller committed
2 3

#
4
# Copyright (c) 2000-2016 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 31 32
use English;
use Getopt::Std;

#
# usage: webcontrol [-m message] [-l login | nologin]
#
sub usage()
{
33 34
    print STDOUT "Usage: webcontrol [-m message | -] [-l login | nologin]\n";
    print STDOUT "Specify '-' for the message, to use the default message\n";
35 36 37 38 39 40 41
    exit(-1);
}
my  $optlist = "m:l:";

#
# Configure variables
#
42 43
use lib "@prefix@/lib";
use libdb;
44

45
my $TBROOT         = "@prefix@";
46 47
my $DEFMESSAGE     = "Testbed going offline for maintenance; ".
    "back in a little while";
48 49 50 51

# Untaint the path
$ENV{'PATH'} = "/usr/bin:$TBROOT/libexec:$TBROOT/sbin:$TBROOT/bin";

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#
# Turn off line buffering on output
#
$| = 1;

#
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (@ARGV != 0) {
    usage();
}
my $setmesg = 0;
my $message = "";
if (defined($options{"m"})) {
    $setmesg = 1;
    $message = $options{"m"};
73 74 75
    if ($message eq "-") {
	$message = $DEFMESSAGE;
    }
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
}
my $setlogin = 0;
my $logins   = 1;
if (defined($options{"l"})) {
    $setlogin = 1;
    if ($options{"l"} eq "login") {
	$logins = 1;
    }
    elsif ($options{"l"} eq "nologin") {
	$logins = 0;
    }
    else {
	usage();
    }
}

#
# Only root or admin types!
# 
95
if (($UID != 0) && (!TBAdmin($UID))) {
96 97 98 99 100 101 102
	die("Only root or TB administrators can control the web interface.\n");
}

if ($setmesg) {
    if ($message eq "") {
	print "Clearing Web Message of the Day\n";

103
	system("setsitevar web/message -");
104 105
    }
    else {
106 107 108 109
	print "Setting Web Message of the Day to \"$message\".\n";

	$message =~ s|\&|&amp;|g;
	$message =~ s|\<|&lt;|g;
110

111
	system("setsitevar","web/message", $message);
112 113 114 115 116 117 118
    }
}

if ($setlogin) {
    if ($logins) {
	print "Enabling Web Interface logins\n";

119
	system("setsitevar web/nologins -");
120 121 122 123
    }
    else {
	print "Disabling Web Interface logins\n";

124
	system("setsitevar web/nologins 1");
125 126 127 128

	#
	# Find all non admins and log them out.
	# 
129
	DBQueryFatal("delete from login where adminon=0");
130 131 132
    }
}

133 134