modifyexp.php3 9.92 KB
Newer Older
Chad Barb's avatar
 
Chad Barb committed
1 2 3
<?php
#
# EMULAB-COPYRIGHT
Mike Hibler's avatar
Mike Hibler committed
4
# Copyright (c) 2000-2006 University of Utah and the Flux Group.
Chad Barb's avatar
 
Chad Barb committed
5 6 7 8 9 10
# All rights reserved.
#
include("defs.php3");
include("showstuff.php3");

$parser   = "$TB/libexec/ns2ir/parse-ns";
11
$TMPDIR   = "/tmp/";
Chad Barb's avatar
 
Chad Barb committed
12 13 14 15 16 17 18 19

#
# Only known and logged in users can modify experiments.
#
$uid = GETLOGIN();
LOGGEDINORDIE($uid);
$isadmin = ISADMIN($uid);

20 21 22 23
if (isset($formfields['exp_localnsfile'])) {
    $exp_localnsfile = $formfields['exp_localnsfile'];
}

24 25 26 27 28 29 30 31
# This will not return if its a sajax request.
include("showlogfile_sup.php3");

#
# Standard Testbed Header
#
PAGEHEADER("Modify Experiment");

Chad Barb's avatar
 
Chad Barb committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#
# Verify page arguments.
# 
if (!isset($pid) ||
    strcmp($pid, "") == 0) {
    USERERROR("You must provide a Project ID.", 1);
}
if (!isset($eid) ||
    strcmp($eid, "") == 0) {
    USERERROR("You must provide an Experiment ID.", 1);
}

#
# Be paranoid.
#
$pid = addslashes($pid);
$eid = addslashes($eid);

#
51 52 53 54 55
# Check to make sure this is a valid experiment.
#
if (! TBValidExperiment($pid, $eid)) {
    USERERROR("The experiment $eid is not a valid experiment ".
	      "in project $pid.", 1);
Chad Barb's avatar
 
Chad Barb committed
56 57
}

58 59
if (! TBExptAccessCheck($uid, $pid, $eid, $TB_EXPT_MODIFY)) {
    USERERROR("You do not have permission to modify this experiment.", 1);
Chad Barb's avatar
 
Chad Barb committed
60 61
}

62 63 64 65
if (TBExptLockedDown($pid, $eid)) {
    USERERROR("Cannot proceed; experiment is locked down!", 1);
}

66 67
$expstate = TBExptState($pid, $eid);

Chad Barb's avatar
 
Chad Barb committed
68 69
if (strcmp($expstate, $TB_EXPTSTATE_ACTIVE) &&
    strcmp($expstate, $TB_EXPTSTATE_SWAPPED)) {
70
    USERERROR("You cannot modify an experiment in transition.", 1);
Chad Barb's avatar
 
Chad Barb committed
71 72
}

73 74 75 76
# Okay, start.
echo "<font size=+2>Experiment <b>".
     "<a href='showproject.php3?pid=$pid'>$pid</a>/".
     "<a href='showexp.php3?pid=$pid&eid=$eid'>$eid</a></b></font>\n";
77 78
echo "<br>\n";
flush();
Chad Barb's avatar
 
Chad Barb committed
79

80 81 82
#
# Put up the modify form on first load.
# 
Chad Barb's avatar
 
Chad Barb committed
83
if (! isset($go)) {
84
    echo "<a href='faq.php3#swapmod'>".
85 86
	 "Modify Experiment Documentation (FAQ)</a></h3>";

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    echo "<script language=JavaScript>
          <!--
          function NormalSubmit() {
              document.form1.target='_self';
              document.form1.submit();
          }
          function SyntaxCheck() {
              window.open('','nscheck','width=650,height=400,toolbar=no,".
                              "resizeable=yes,scrollbars=yes,status=yes,".
                              "menubar=yes');
              var action = document.form1.action;
              var target = document.form1.target;

              document.form1.action='nscheck.php3';
              document.form1.target='nscheck';
              document.form1.submit();

              document.form1.action=action;
              document.form1.target=target;
          }
          //-->
          </script>\n";

    echo "<table align=center border=1>\n";
Timothy Stack's avatar
 
Timothy Stack committed
111
    if (STUDLY()) {
112
	echo "<tr><th colspan=2><font size='+1'>".
Timothy Stack's avatar
 
Timothy Stack committed
113 114
	    "<a href='clientui.php3?pid=$pid&eid=$eid'>GUI Editor</a>".
	    " - Edit the topology using a Java applet.</font>";
115
	echo "</th></tr>";
Timothy Stack's avatar
 
Timothy Stack committed
116 117
    }

118 119 120 121 122 123 124 125 126
    echo "<form name='form1' action='modifyexp.php3?pid=$pid&eid=$eid&go=1' method='post' onsubmit=\"return false;\" enctype=multipart/form-data>";
    echo "<tr><th>Upload new NS file: </th>";
    echo "<td><input type=hidden name=MAX_FILE_SIZE value=512000>";
    echo "<input type=file name=exp_nsfile size=30 onchange=\"this.form.syntax.disabled=(this.value=='')\" /></td></tr>\n";
    echo "<tr><th><em>or</em> NS file on server: </th> ";
    echo "<td><input type=text name=\"formfields[exp_localnsfile]\" size=40 onchange=\"this.form.syntax.disabled=(this.value=='')\" /> </td</tr>\n";
    
    echo "<tr><td colspan=2><b><em>or</em> Edit:</b><br>\n";
    echo "<textarea cols='100' rows='40' name='nsdata' onchange=\"this.form.syntax.disabled=(this.value=='')\">";
Chad Barb's avatar
 
Chad Barb committed
127

128 129 130 131 132
    $query_result =
	DBQueryFatal("SELECT nsfile from nsfiles ".
		     "where pid='$pid' and eid='$eid'");
    if (mysql_num_rows($query_result)) {
	$row    = mysql_fetch_array($query_result);
133
	$nsfile = $row[nsfile];
Chad Barb's avatar
 
Chad Barb committed
134
	    
135 136 137 138 139
	echo "$nsfile";
    }
    else {
	echo "# There was no stored NS file for $pid/$eid.\n";
    }
Chad Barb's avatar
 
Chad Barb committed
140
	
141
    echo "</textarea>";
142
    echo "</td></tr>\n";
143
    if (!strcmp($expstate, $TB_EXPTSTATE_ACTIVE)) {
144
	echo "<tr><td colspan=2><p><b>Note!</b> It is recommended that you 
Chad Barb's avatar
 
Chad Barb committed
145
	      reboot all nodes in your experiment by checking the box below.
146 147 148 149
	      This is especially important if changing your experiment
              topology (adding or removing nodes, links, and LANs).
	      If adding/removing a delay to/from an existing link, or
              replacing a lost node <i>without modifying the experiment
150 151 152 153
              topology</i>, this won't be necessary. Restarting the
	      event system is also highly recommended since the same nodes
	      in your virtual topology may get mapped to different physical
	      nodes.</p>";
154
	echo "<input type='checkbox' name='reboot' value='1' checked='1'>
Chad Barb's avatar
 
Chad Barb committed
155
	      Reboot nodes in experiment (Highly Recommended)</input>";
156 157
	echo "<br><input type='checkbox' name='eventrestart' value='1' checked='1'>
	      Restart Event System in experiment (Highly Recommended)</input>";
158
      echo "</td></tr>";
159
    }
160 161 162 163 164 165
    echo "<tr><th colspan=2><center>";
    echo "<input type=submit disabled id=syntax name=syntax value='Syntax Check' onclick=\"SyntaxCheck();\"> ";
    echo "<input type=button name='go' value='Modify' onclick='NormalSubmit();'>\n";
    echo "<input type='reset'>";
    echo "</center></th></tr>\n";
    echo "</table>\n";
166 167 168 169
    echo "</form>\n";
    PAGEFOOTER();
    exit();
}
Chad Barb's avatar
 
Chad Barb committed
170

171 172 173
#
# Okay, form has been submitted.
#
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
$speclocal  = 0;
$specupload = 0;
$specform   = 0;
$nsfile     = "";
$tmpfile    = 0;

if (isset($exp_localnsfile) && strcmp($exp_localnsfile, "")) {
    $speclocal = 1;
}
if (isset($exp_nsfile) && strcmp($exp_nsfile, "") &&
    strcmp($exp_nsfile, "none")) {
    $specupload = 1;
}
if (!$speclocal && !$specupload && isset($nsdata))  {
    $specform = 1;
}

if ($speclocal + $specupload + $specform > 1) {
    USERERROR("You may not specify both an uploaded NS file and an ".
	      "NS file that is located on the Emulab server", 1);
}
if (!$specupload && strcmp($exp_nsfile_name, "")) {
    #
    # Catch an invalid filename.
    #
    USERERROR("The NS file '$exp_nsfile_name' does not appear to be a ".
	      "valid filename. Please go back and try again.", 1);
201
}
Chad Barb's avatar
 
Chad Barb committed
202

203
#
204 205 206 207 208 209 210 211 212 213 214 215 216
# Gotta be one of them!
#
if (!$speclocal && !$specupload && !$specform) {
    USERERROR("You must supply an NS file!", 1);
}

if ($speclocal) {
    #
    # No way to tell from here if this file actually exists, since
    # the web server runs as user nobody. The startexp script checks
    # for the file before going to ground, so the user will get immediate
    # feedback if the filename is bogus.
    #
217 218
    # Do not allow anything outside of the usual directories. I do not think
    # there is a security worry, but good to enforce it anyway.
219 220
    #
    if (!preg_match("/^([-\@\w\.\/]+)$/", $exp_localnsfile)) {
Mike Hibler's avatar
Mike Hibler committed
221
	USERERROR("NS File: Pathname includes illegal characters", 1);
222
    }
223 224 225
    if (!VALIDUSERPATH($exp_localnsfile)) {
	USERERROR("NS File: You must specify a server resident file in " .
		  "one of: ${TBVALIDDIRS}.", 1);
226
    }
227
    
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    $nsfile = $exp_localnsfile;
    $nonsfile = 0;
}
elseif ($specupload) {
    #
    # XXX
    # Set the permissions on the NS file so that the scripts can get to it.
    # It is owned by nobody, and most likely protected. This leaves the
    # script open for a short time. A potential security hazard we should
    # deal with at some point.
    #
    chmod($exp_nsfile, 0666);
    $nsfile = $exp_nsfile;
    $nonsfile = 0;
} else # $specform
if ($specform) {
    #
    # Take the NS file passed in from the form and write it out to a file
    #
    $tmpfile = 1;

    #
    # Generate a hopefully unique filename that is hard to guess.
    # See backend scripts.
    # 
    list($usec, $sec) = explode(' ', microtime());
    srand((float) $sec + ((float) $usec * 100000));
    $foo = rand();
256

257 258 259 260 261
    $nsfile = "/tmp/$uid-$foo.nsfile";
    $handle = fopen($nsfile,"w");
    fwrite($handle,$nsdata);
    fclose($handle);
    chmod($nsfile, 0666);
262
}
263

264 265 266 267 268 269 270 271 272
#
# Get exp group so we can get the unix_gid.
#
TBExptGroup($pid, $eid, $gid);
TBGroupUnixInfo($pid, $gid, $unix_gid, $unix_name);

#
# Do an initial parse test.
#
273
$retval = SUEXEC($uid, "$pid,$unix_gid", "webnscheck $nsfile",
274 275 276
		 SUEXEC_ACTION_IGNORE);

if ($retval != 0) {
277 278 279
    if ($tmpfile) {
        unlink($nsfile);
    }
280 281 282 283
    
    # Send error to tbops.
    if ($retval < 0) {
	SUEXECERROR(SUEXEC_ACTION_CONTINUE);
Chad Barb's avatar
 
Chad Barb committed
284
    }
285 286 287 288 289 290
    echo "<br>";
    echo "<h3>Modified NS file contains syntax errors</h3>";
    echo "<blockquote><pre>$suexec_output<pre></blockquote>";

    PAGEFOOTER();
    exit();
291 292 293 294 295 296 297 298
}

#	
# Avoid SIGPROF in child.
# 
set_time_limit(0);

# Run the script.
299
$retval = SUEXEC($uid, "$pid,$unix_gid",
300
		 "webswapexp $rebootswitch " . ($reboot ? "-r " : "") .
301
		 ($eventrestart ? "-e " : "") .
302 303 304 305
		 "-s modify $pid $eid $nsfile",
		 SUEXEC_ACTION_IGNORE);
		 
# It has been copied out by the program!
306 307 308
if ($tmpfile) {
    unlink($nsfile);
}
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333

#
# Fatal Error. Report to the user, even though there is not much he can
# do with the error. Also reports to tbops.
# 
if ($retval < 0) {
    SUEXECERROR(SUEXEC_ACTION_DIE);
    #
    # Never returns ...
    #
    die("");
}

#
# Exit status 0 means the experiment is swapping, or will be.
#
echo "<br>\n";
if ($retval) {
    echo "<h3>Experiment modify could not proceed</h3>";
    echo "<blockquote><pre>$suexec_output<pre></blockquote>";
}
else {
    #
    # Exit status 0 means the experiment is modifying.
    #
334
    echo "<b>Your experiment is being modified!</b> ";
335 336 337 338 339 340 341 342
    echo "You will be notified via email when the experiment has ".
	"finished modifying and you are able to proceed. This ".
	"typically takes less than 10 minutes, depending on the ".
	"number of nodes in the experiment. ".
	"If you do not receive email notification within a ".
	"reasonable amount time, please contact $TBMAILADDR. ".
	"<br><br>".
	"While you are waiting, you can watch the log of experiment ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
343
	"modification in realtime:<br><br>\n";
344
    STARTLOG($pid, $eid);    
345 346
}

Chad Barb's avatar
 
Chad Barb committed
347 348 349 350 351 352 353
#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>