modifyexp.php3 9.54 KB
Newer Older
1 2 3
<?php
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2007 University of Utah and the Flux Group.
5 6 7 8
# All rights reserved.
#
include("defs.php3");

9
$TMPDIR   = "/tmp/";
10 11

#
12
# Only known and logged in users.
13
#
14 15 16
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
17

18 19 20 21 22 23 24 25
# This will not return if its a sajax request.
include("showlogfile_sup.php3");

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

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
$reqargs = RequiredPageArguments("experiment",      PAGEARG_EXPERIMENT);
$optargs = OptionalPageArguments("go",              PAGEARG_STRING,
				 "syntax",          PAGEARG_STRING,
				 "reboot",          PAGEARG_BOOLEAN,
				 "eventrestart",    PAGEARG_BOOLEAN,
				 "nsdata",          PAGEARG_ANYTHING,
				 "exp_localnsfile", PAGEARG_STRING,
				 "formfields",      PAGEARG_ARRAY);

# Need these below.
$pid = $experiment->pid();
$eid = $experiment->eid();
$unix_gid = $experiment->UnixGID();
$expstate = $experiment->state();

if (!$experiment->AccessCheck($this_user, $TB_EXPT_MODIFY)) {
42
    USERERROR("You do not have permission to modify this experiment.", 1);
43 44
}

45
if ($experiment->lockdown()) {
46 47 48
    USERERROR("Cannot proceed; experiment is locked down!", 1);
}

49 50
if (strcmp($expstate, $TB_EXPTSTATE_ACTIVE) &&
    strcmp($expstate, $TB_EXPTSTATE_SWAPPED)) {
51
    USERERROR("You cannot modify an experiment in transition.", 1);
52 53
}

54
# Okay, start.
55
echo $experiment->PageHeader();
56 57
echo "<br>\n";
flush();
58

59 60 61
#
# Put up the modify form on first load.
# 
62
if (! isset($go)) {
63
    echo "<a href='faq.php3#swapmod'>".
64 65
	 "Modify Experiment Documentation (FAQ)</a></h3>";

66 67 68 69 70 71 72 73 74 75 76 77 78
    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;

79
              document.form1.action='nscheck.php3?fromform=1';
80 81 82 83 84 85 86 87 88 89
              document.form1.target='nscheck';
              document.form1.submit();

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

    echo "<table align=center border=1>\n";
90
    if (STUDLY()) {
91 92
	$ui_url = CreateURL("clientui", $experiment);
	
93
	echo "<tr><th colspan=2><font size='+1'>".
94
	    "<a href='$ui_url'>GUI Editor</a>".
95
	    " - Edit the topology using a Java applet.</font>";
96
	echo "</th></tr>";
97 98
    }

99 100 101 102 103
    $url = CreateURL("modifyexp", $experiment, "go", 1);
    
    echo "<form name='form1' action='$url' method='post'
             onsubmit=\"return false;\" enctype=multipart/form-data>";
    
104 105
    echo "<tr><th>Upload new NS file: </th>";
    echo "<td><input type=hidden name=MAX_FILE_SIZE value=512000>";
106 107 108
    echo "    <input type=file name=exp_nsfile size=30
                      onchange=\"this.form.syntax.disabled=(this.value=='')\"/>
           </td></tr>\n";
109
    echo "<tr><th><em>or</em> NS file on server: </th> ";
110 111 112
    echo "<td><input type=text name=\"exp_localnsfile\" size=40
                     onchange=\"this.form.syntax.disabled=(this.value=='')\" />
          </td</tr>\n";
113 114
    
    echo "<tr><td colspan=2><b><em>or</em> Edit:</b><br>\n";
115 116 117 118 119
    echo "<textarea cols='100' rows='40' name='nsdata'
                    onchange=\"this.form.syntax.disabled=(this.value=='')\">";

    $nsfile = $experiment->NSFile();
    if ($nsfile) {
120 121 122 123 124
	echo "$nsfile";
    }
    else {
	echo "# There was no stored NS file for $pid/$eid.\n";
    }
125
	
126
    echo "</textarea>";
127
    echo "</td></tr>\n";
128
    if (!strcmp($expstate, $TB_EXPTSTATE_ACTIVE)) {
129
	echo "<tr><td colspan=2><p><b>Note!</b> It is recommended that you 
130
	      reboot all nodes in your experiment by checking the box below.
131 132 133 134
	      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
135 136 137 138
              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>";
139
	echo "<input type='checkbox' name='reboot' value='1' checked='1'>
140
	      Reboot nodes in experiment (Highly Recommended)</input>";
141 142
	echo "<br><input type='checkbox' name='eventrestart'
                         value='1' checked='1'>
143
	      Restart Event System in experiment (Highly Recommended)</input>";
144
      echo "</td></tr>";
145
    }
146
    echo "<tr><th colspan=2><center>";
147 148 149 150
    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";
151 152 153
    echo "<input type='reset'>";
    echo "</center></th></tr>\n";
    echo "</table>\n";
154 155 156 157
    echo "</form>\n";
    PAGEFOOTER();
    exit();
}
Chad Barb's avatar
Chad Barb committed
158

159 160 161
#
# Okay, form has been submitted.
#
162 163 164 165 166 167 168 169 170
$speclocal  = 0;
$specupload = 0;
$specform   = 0;
$nsfile     = "";
$tmpfile    = 0;

if (isset($exp_localnsfile) && strcmp($exp_localnsfile, "")) {
    $speclocal = 1;
}
171 172 173
if (isset($_FILES['exp_nsfile']) &&
    $_FILES['exp_nsfile']['name'] != "" &&
    $_FILES['exp_nsfile']['tmp_name'] != "") {
174 175 176
    if ($_FILES['exp_nsfile']['size'] == 0) {
        USERERROR("Uploaded NS file does not exist, or is empty");
    }
177 178 179 180 181 182 183 184 185 186
    $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);
}
187
#
188 189 190 191 192 193 194 195 196 197 198 199 200
# 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.
    #
201 202
    # Do not allow anything outside of the usual directories. I do not think
    # there is a security worry, but good to enforce it anyway.
203 204
    #
    if (!preg_match("/^([-\@\w\.\/]+)$/", $exp_localnsfile)) {
Mike Hibler's avatar
Mike Hibler committed
205
	USERERROR("NS File: Pathname includes illegal characters", 1);
206
    }
207 208 209
    if (!VALIDUSERPATH($exp_localnsfile)) {
	USERERROR("NS File: You must specify a server resident file in " .
		  "one of: ${TBVALIDDIRS}.", 1);
210
    }
211
    
212 213 214 215 216 217 218 219 220 221 222
    $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.
    #
223 224
    $nsfile = $_FILES['exp_nsfile']['tmp_name'];
    chmod($nsfile, 0666);
225
    $nonsfile = 0;
226 227
}
elseif ($specform) {
228 229 230 231 232 233 234 235 236 237 238 239
    #
    # 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();
240

241 242 243 244 245
    $nsfile = "/tmp/$uid-$foo.nsfile";
    $handle = fopen($nsfile,"w");
    fwrite($handle,$nsdata);
    fclose($handle);
    chmod($nsfile, 0666);
246
}
247

248 249 250
#
# Do an initial parse test.
#
251
$retval = SUEXEC($uid, "$pid,$unix_gid", "webnscheck $nsfile",
252 253 254
		 SUEXEC_ACTION_IGNORE);

if ($retval != 0) {
255 256 257
    if ($tmpfile) {
        unlink($nsfile);
    }
258 259 260 261
    
    # Send error to tbops.
    if ($retval < 0) {
	SUEXECERROR(SUEXEC_ACTION_CONTINUE);
262
    }
263 264 265 266 267 268
    echo "<br>";
    echo "<h3>Modified NS file contains syntax errors</h3>";
    echo "<blockquote><pre>$suexec_output<pre></blockquote>";

    PAGEFOOTER();
    exit();
269 270 271 272 273
}

# Avoid SIGPROF in child.
set_time_limit(0);

274 275 276 277 278 279 280 281 282
# args.
$optargs = "";
if (isset($reboot) && $reboot) {
     $optargs .= " -r ";
}
if (isset($eventrestart) && $eventrestart) {
     $optargs .= " -e ";
}

283
# Run the script.
284
$retval = SUEXEC($uid, "$pid,$unix_gid",
285
		 "webswapexp $optargs -s modify $pid $eid $nsfile",
286 287 288
		 SUEXEC_ACTION_IGNORE);
		 
# It has been copied out by the program!
289 290 291
if ($tmpfile) {
    unlink($nsfile);
}
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

#
# 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.
    #
317
    echo "<b>Your experiment is being modified!</b> ";
318 319 320 321 322 323 324 325
    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
326
	"modification in realtime:<br><br>\n";
327
    STARTLOG($experiment);    
328 329
}

330 331 332 333 334 335 336
#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>