toggle.php 8.47 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2000-2012 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{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/>.
# 
# }}}
23 24 25 26 27 28 29 30 31 32
#
include("defs.php3");

#
# This page is a generic toggle page, like adminmode.php3, but more
# generalized. There are a set of things you can toggle, and each of
# those items has a permission check and a set (pair) of valid values.
#
# Usage: toggle.php?type=swappable&value=1&pid=foo&eid=bar
# (type & value are required, others are optional and vary by type)
33 34 35 36 37
#
# No PAGEHEADER since we spit out a Location header later. See below.
#
# Only known and logged in users can do this.
#
38 39 40
$this_user = CheckLoginOrDie(CHECKLOGIN_USERSTATUS|CHECKLOGIN_WEBONLY);
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
41 42

# List of valid toggles
43
$toggles = array("adminon", "webfreeze", "cvsweb", "lockdown", "stud",
44
		 "cvsrepo_public", "workbench", "hiderun", "widearearoot",
45
		 "imageglobal", "skipvlans");
46 47

# list of valid values for each toggle
48
$values  = array("adminon"        => array(0,1),
49 50
		 "webfreeze"      => array(0,1),
		 "cvsweb"         => array(0,1),
51
		 "stud"           => array(0,1),
52
		 "lockdown"       => array(0,1),
53
		 "skipvlans"      => array(0,1),
54
		 "cvsrepo_public" => array(0,1),
55
		 "workbench"      => array(0,1),
56
		 "widearearoot"   => array(0,1),
57
		 "imageglobal"    => array(0,1),
58
		 "hiderun"        => array(0,1));
59 60

# list of valid extra variables for the each toggle, and mandatory flag.
61
$optargs = array("adminon"        => array(),
62 63
		 "webfreeze"      => array("user" => 1),
		 "cvsweb"         => array("user" => 1),
64
		 "stud"           => array("user" => 1),
65
		 "lockdown"       => array("pid" => 1, "eid" => 1),
66
		 "skipvlans"      => array("pid" => 1, "eid" => 1),
67
		 "cvsrepo_public" => array("pid" => 1),
68
		 "workbench"      => array("pid" => 1),
69
		 "widearearoot"   => array("user" => 1),
70
		 "imageglobal"    => array("imageid" => 1),
71
		 "hiderun"        => array("instance" => 1, "runidx" => 1));
72 73

# Mandatory page arguments.
74 75
$reqargs = RequiredPageArguments("type",  PAGEARG_STRING,
				 "value", PAGEARG_STRING);
76

77 78
# Where we zap to.
$zapurl = null;
79 80

if (! in_array($type, $toggles)) {
81
    PAGEARGERROR("There is no toggle for $type!");
82 83
}
if (! in_array($value, $values[$type])) {
84 85 86 87 88 89 90 91 92 93 94 95 96 97
    PAGEARGERROR("The value '$value' is illegal for the $type toggle!");
}

# Check optional args and bind locally.
while (list ($arg, $required) = each ($optargs[$type])) {
    if (!isset($_GET[$arg])) {
	if ($required)
	    PAGEARGERROR("Toggle '$type' requires argument '$arg'");
	else
	    unset($$arg);
    }
    else {
	$$arg = addslashes($_GET[$arg]);
    }
98 99 100 101 102
}

#
# Permissions checks, and do the toggle...
#
103
if ($type == "adminon") {
104
    # must be admin
105
    # Do not check if they are admin mode (ISADMIN), check if they
106 107
    # have the power to change to admin mode!
    if (! ($CHECKLOGIN_STATUS & CHECKLOGIN_ISADMIN) ) {
108 109
	USERERROR("You do not have permission to toggle $type!", 1);
    }
110
    SETADMINMODE($value);
111
}
112 113
elseif ($type == "webfreeze") {
    # must be admin
114
    if (! $isadmin) {
115 116
	USERERROR("You do not have permission to toggle $type!", 1);
    }
117 118
    if (! ($target_user = User::Lookup($user))) {
	PAGEARGERROR("Target user '$user' is not a valid user!");
119
    }
120
    $zapurl = CreateURL("showuser", $target_user);
121
    $target_user->SetWebFreeze($value);
122
}
123 124 125 126 127
elseif ($type == "cvsweb") {
    # must be admin
    if (! $isadmin) {
	USERERROR("You do not have permission to toggle $type!", 1);
    }
128 129
    if (! ($target_user = User::Lookup($user))) {
	PAGEARGERROR("Target user '$user' is not a valid user!");
130
    }
131
    $zapurl = CreateURL("showuser", $target_user);
132
    $target_user->SetCVSWeb($value);
133
}
134 135 136 137 138 139 140 141 142 143 144
elseif ($type == "stud") {
    # must be admin
    if (! $isadmin) {
	USERERROR("You do not have permission to toggle $type!", 1);
    }
    if (! ($target_user = User::Lookup($user))) {
	PAGEARGERROR("Target user '$user' is not a valid user!");
    }
    $zapurl = CreateURL("showuser", $target_user);
    $target_user->SetStudly($value);
}
145 146 147 148 149 150 151 152 153 154 155
elseif ($type == "widearearoot") {
    # must be admin
    if (! $isadmin) {
	USERERROR("You do not have permission to toggle $type!", 1);
    }
    if (! ($target_user = User::Lookup($user))) {
	PAGEARGERROR("Target user '$user' is not a valid user!");
    }
    $zapurl = CreateURL("showuser", $target_user);
    $target_user->SetWideAreaRoot($value);
}
156 157 158 159 160
elseif ($type == "lockdown") {
    # must be admin
    if (! $isadmin) {
	USERERROR("You do not have permission to toggle $type!", 1);
    }
161
    if (! ($experiment = Experiment::LookupByPidEid($pid, $eid))) {
162 163
	PAGEARGERROR("Experiment $pid/$eid is not a valid experiment!");
    }
164 165
    $zapurl = CreateURL("showexp", $experiment);
    $experiment->SetLockDown($value);
166
}
167
elseif ($type == "skipvlans") {
168 169 170 171 172 173 174 175
    # Must validate the pid,eid since we allow non-admins to do this.
    if (! TBvalid_pid($pid)) {
	PAGEARGERROR("Invalid characters in $pid");
    }
    if (! TBvalid_eid($eid)) {
	PAGEARGERROR("Invalid characters in $eid");
    }
    if (! ($isadmin || STUDLY() || OPSGUY())) {
176 177 178 179 180
	USERERROR("You do not have permission to toggle $type!", 1);
    }
    if (! ($experiment = Experiment::LookupByPidEid($pid, $eid))) {
	PAGEARGERROR("Experiment $pid/$eid is not a valid experiment!");
    }
181 182 183 184
    if (!$isadmin &&
	! TBMinTrust(TBGrpTrust($uid, $pid, $pid), $TBDB_TRUST_LOCALROOT)) {
	USERERROR("You do not have permission to toggle $type!", 1);
    }
185 186 187
    $zapurl = CreateURL("showexp", $experiment);
    $experiment->SetSkipVlans($value);
}
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
elseif ($type == "imageglobal") {
    include("imageid_defs.php");
    
    # Must validate since we allow non-admins to do this.
    if (! TBvalid_imageid($imageid)) {
	PAGEARGERROR("Invalid characters in $imageid");
    }
    if (! ($image = Image::Lookup($imageid))) {
	PAGEARGERROR("Image $image is not a valid image!");
    }
    if (!$isadmin &&
	!$image->AccessCheck($this_user, $TB_IMAGEID_MODIFYINFO)) {
	USERERROR("You do not have permission to toggle $type!", 1);
    }
    $zapurl = CreateURL("showimageid", $image);
    $image->SetGlobal($value);
}
205 206 207 208 209
elseif ($type == "cvsrepo_public") {
    # Must validate the pid since we allow non-admins to do this.
    if (! TBvalid_pid($pid)) {
	PAGEARGERROR("Invalid characters in $pid");
    }
210
    if (! ($project = Project::Lookup($pid))) {
211 212 213 214 215 216 217
	PAGEARGERROR("Project $pid is not a valid project!");
    }
    # Must be admin or project/group root.
    if (!$isadmin &&
	! TBMinTrust(TBGrpTrust($uid, $pid, $pid), $TBDB_TRUST_GROUPROOT)) {
	USERERROR("You do not have permission to toggle $type!", 1);
    }
218 219
    $zapurl = CreateURL("showproject", $project);
    $project->SetCVSRepoPublic($value);
220 221
    $unix_pid = $project->unix_gid();
    SUEXEC($uid, $unix_pid, "webcvsrepo_ctrl $pid", SUEXEC_ACTION_DIE);
222
}
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
elseif ($type == "workbench") {
    # Must validate the pid since we allow non-admins to do this.
    if (! TBvalid_pid($pid)) {
	PAGEARGERROR("Invalid characters in $pid");
    }
    if (! ($project = Project::Lookup($pid))) {
	PAGEARGERROR("Project $pid is not a valid project!");
    }
    # Must be admin
    if (!$isadmin) {
	USERERROR("You do not have permission to toggle $type!", 1);
    }
    $zapurl = CreateURL("showproject", $project);
    $project->SetAllowWorkbench($value);
}
238 239 240 241 242 243 244 245 246
elseif ($type == "hiderun") {
    RequiredPageArguments("instance",  PAGEARG_INSTANCE,
			  "runidx",    PAGEARG_INTEGER);

    if (! $instance->AccessCheck($this_user, $TB_EXPT_MODIFY)) {
	USERERROR("You do not have permission to modify this instance", 1);
    }
    $instance->SetRunHidden($runidx, $value);
}
247
else {
248 249 250 251 252 253
    USERERROR("Nobody has permission to toggle $type!", 1);
}
    
#
# Spit out a redirect 
#
254
if (isset($HTTP_REFERER) && $HTTP_REFERER != "" &&
255
    strpos($HTTP_REFERER,$_SERVER["SCRIPT_NAME"])===false) {
256
    # Make sure the referer is not me!
257 258
    header("Location: $HTTP_REFERER");
}
259 260 261
elseif ($zapurl) {
    header("Location: $zapurl");
}
262
else {
263
    header("Location: $TBBASE/showuser.php3");
264 265 266
}

?>