modifyexp.php3 10.5 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
#
include("defs.php3");

26
$TMPDIR   = "/tmp/";
27 28

#
29
# Only known and logged in users.
30
#
31 32 33
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
34

35 36 37
# This will not return if its a sajax request.
include("showlogfile_sup.php3");

38 39 40 41 42 43
$reqargs = RequiredPageArguments("experiment",      PAGEARG_EXPERIMENT);
$optargs = OptionalPageArguments("go",              PAGEARG_STRING,
				 "syntax",          PAGEARG_STRING,
				 "reboot",          PAGEARG_BOOLEAN,
				 "eventrestart",    PAGEARG_BOOLEAN,
				 "nsdata",          PAGEARG_ANYTHING,
44
				 "exp_localnsfile", PAGEARG_STRING);
45

46 47 48 49 50
#
# Standard Testbed Header
#
PAGEHEADER("Modify Experiment");

51 52 53 54
# Need these below.
$pid = $experiment->pid();
$eid = $experiment->eid();
$unix_gid = $experiment->UnixGID();
55 56
$project  = $experiment->Project();
$unix_pid = $project->unix_gid();
57
$expstate = $experiment->state();
58 59 60 61 62 63
$geniflags = $experiment->geniflags();

# Must go through the geni interfaces.
if ($geniflags) {
    USERERROR("You cannot modify ProtoGeni experiments this way!", 1);
}
64 65

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

69
if ($experiment->lockdown()) {
70 71 72
    USERERROR("Cannot proceed; experiment is locked down!", 1);
}

73 74
if (strcmp($expstate, $TB_EXPTSTATE_ACTIVE) &&
    strcmp($expstate, $TB_EXPTSTATE_SWAPPED)) {
75
    USERERROR("You cannot modify an experiment in transition.", 1);
76 77
}

78
# Okay, start.
79
echo $experiment->PageHeader();
80 81
echo "<br>\n";
flush();
82

83 84 85
#
# Put up the modify form on first load.
# 
86
if (! isset($go)) {
87
    echo "<a href='faq.php3#swapmod'>".
88 89
	 "Modify Experiment Documentation (FAQ)</a></h3>";

90 91 92 93 94 95 96 97 98 99 100 101 102
    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;

103
              document.form1.action='nscheck.php3?fromform=1';
104 105 106 107 108 109 110 111 112 113
              document.form1.target='nscheck';
              document.form1.submit();

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

    echo "<table align=center border=1>\n";
114
    if (STUDLY()) {
115 116
	$ui_url = CreateURL("clientui", $experiment);
	
117
	echo "<tr><th colspan=2><font size='+1'>".
118
	    "<a href='$ui_url'>GUI Editor</a>".
119
	    " - Edit the topology using a Java applet.</font>";
120
	echo "</th></tr>";
121 122
    }

123 124 125 126 127
    $url = CreateURL("modifyexp", $experiment, "go", 1);
    
    echo "<form name='form1' action='$url' method='post'
             onsubmit=\"return false;\" enctype=multipart/form-data>";
    
128 129
    echo "<tr><th>Upload new NS file: </th>";
    echo "<td><input type=hidden name=MAX_FILE_SIZE value=512000>";
130 131 132
    echo "    <input type=file name=exp_nsfile size=30
                      onchange=\"this.form.syntax.disabled=(this.value=='')\"/>
           </td></tr>\n";
133
    echo "<tr><th><em>or</em> NS file on server: </th> ";
134 135
    echo "<td><input type=text name=\"exp_localnsfile\" size=40
                     onchange=\"this.form.syntax.disabled=(this.value=='')\" />
Mike Hibler's avatar
Mike Hibler committed
136
          </td></tr>\n";
137 138
    
    echo "<tr><td colspan=2><b><em>or</em> Edit:</b><br>\n";
139 140 141 142 143
    echo "<textarea cols='100' rows='40' name='nsdata'
                    onchange=\"this.form.syntax.disabled=(this.value=='')\">";

    $nsfile = $experiment->NSFile();
    if ($nsfile) {
144 145 146 147 148
	echo "$nsfile";
    }
    else {
	echo "# There was no stored NS file for $pid/$eid.\n";
    }
149
	
150
    echo "</textarea>";
151
    echo "</td></tr>\n";
152
    if (!strcmp($expstate, $TB_EXPTSTATE_ACTIVE)) {
153
	echo "<tr><td colspan=2><p><b>Note!</b> It is recommended that you 
154
	      reboot all nodes in your experiment by checking the box below.
155 156 157 158
	      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
159 160 161 162
              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>";
163
	echo "<input type='checkbox' name='reboot' value='1' checked='1'>
164
	      Reboot nodes in experiment (Highly Recommended)</input>";
165 166
	echo "<br><input type='checkbox' name='eventrestart'
                         value='1' checked='1'>
167
	      Restart Event System in experiment (Highly Recommended)</input>";
168
      echo "</td></tr>";
169
    }
170
    echo "<tr><th colspan=2><center>";
171 172 173 174
    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";
175 176 177
    echo "<input type='reset'>";
    echo "</center></th></tr>\n";
    echo "</table>\n";
178 179 180 181
    echo "</form>\n";
    PAGEFOOTER();
    exit();
}
182

183 184 185
#
# Okay, form has been submitted.
#
186 187 188 189 190 191 192 193 194
$speclocal  = 0;
$specupload = 0;
$specform   = 0;
$nsfile     = "";
$tmpfile    = 0;

if (isset($exp_localnsfile) && strcmp($exp_localnsfile, "")) {
    $speclocal = 1;
}
195 196 197
if (isset($_FILES['exp_nsfile']) &&
    $_FILES['exp_nsfile']['name'] != "" &&
    $_FILES['exp_nsfile']['tmp_name'] != "") {
198 199 200
    if ($_FILES['exp_nsfile']['size'] == 0) {
        USERERROR("Uploaded NS file does not exist, or is empty");
    }
201 202 203 204 205 206 207 208 209 210
    $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);
}
211
#
212 213 214 215 216 217 218 219 220 221 222 223 224
# 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.
    #
225 226
    # Do not allow anything outside of the usual directories. I do not think
    # there is a security worry, but good to enforce it anyway.
227 228
    #
    if (!preg_match("/^([-\@\w\.\/]+)$/", $exp_localnsfile)) {
Mike Hibler's avatar
Mike Hibler committed
229
	USERERROR("NS File: Pathname includes illegal characters", 1);
230
    }
231 232 233
    if (!VALIDUSERPATH($exp_localnsfile)) {
	USERERROR("NS File: You must specify a server resident file in " .
		  "one of: ${TBVALIDDIRS}.", 1);
234
    }
235
    
236 237 238 239 240 241 242 243 244 245 246
    $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.
    #
247 248
    $nsfile = $_FILES['exp_nsfile']['tmp_name'];
    chmod($nsfile, 0666);
249
    $nonsfile = 0;
250 251
}
elseif ($specform) {
252 253 254 255 256 257 258 259 260 261 262 263
    #
    # 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();
264

265 266 267 268 269
    $nsfile = "/tmp/$uid-$foo.nsfile";
    $handle = fopen($nsfile,"w");
    fwrite($handle,$nsdata);
    fclose($handle);
    chmod($nsfile, 0666);
270
}
271

272 273
STARTBUSY("Starting Modify");

274 275 276
#
# Do an initial parse test.
#
277
$retval = SUEXEC($uid, "$unix_pid,$unix_gid", "webnscheck $nsfile",
278 279 280
		 SUEXEC_ACTION_IGNORE);

if ($retval != 0) {
281 282
    HIDEBUSY();
    
283 284 285
    if ($tmpfile) {
        unlink($nsfile);
    }
286 287 288 289
    
    # Send error to tbops.
    if ($retval < 0) {
	SUEXECERROR(SUEXEC_ACTION_CONTINUE);
290
    }
291 292 293 294 295 296
    echo "<br>";
    echo "<h3>Modified NS file contains syntax errors</h3>";
    echo "<blockquote><pre>$suexec_output<pre></blockquote>";

    PAGEFOOTER();
    exit();
297 298 299 300 301
}

# Avoid SIGPROF in child.
set_time_limit(0);

302 303 304 305 306 307 308 309 310
# args.
$optargs = "";
if (isset($reboot) && $reboot) {
     $optargs .= " -r ";
}
if (isset($eventrestart) && $eventrestart) {
     $optargs .= " -e ";
}

311
# Run the script.
312
$retval = SUEXEC($uid, "$unix_pid,$unix_gid",
313
		 "webswapexp $optargs -s modify $pid $eid $nsfile",
314 315 316
		 SUEXEC_ACTION_IGNORE);
		 
# It has been copied out by the program!
317 318 319
if ($tmpfile) {
    unlink($nsfile);
}
320
HIDEBUSY();
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345

#
# 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.
    #
346
    echo "<b>Your experiment is being modified!</b> ";
347 348 349 350 351 352 353 354
    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 ".
355
	"modification in realtime:<br><br>\n";
356
    STARTLOG($experiment);    
357 358
}

359 360 361 362 363 364 365
#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>