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

9
$TMPDIR   = "/tmp/";
Chad Barb's avatar
 
Chad Barb committed
10 11

#
12
# Only known and logged in users.
Chad Barb's avatar
 
Chad Barb committed
13
#
14 15 16
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
Chad Barb's avatar
 
Chad Barb committed
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);
Chad Barb's avatar
 
Chad Barb committed
43 44
}

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

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

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

59 60 61
#
# Put up the modify form on first load.
# 
Chad Barb's avatar
 
Chad Barb committed
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";
Timothy Stack's avatar
 
Timothy Stack committed
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>".
Timothy Stack's avatar
 
Timothy Stack committed
95
	    " - Edit the topology using a Java applet.</font>";
96
	echo "</th></tr>";
Timothy Stack's avatar
 
Timothy Stack committed
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";
    }
Chad Barb's avatar
 
Chad Barb committed
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 
Chad Barb's avatar
 
Chad Barb committed
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'>
Chad Barb's avatar
 
Chad Barb committed
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 177 178 179 180 181 182 183
    $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);
}
184
#
185 186 187 188 189 190 191 192 193 194 195 196 197
# 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.
    #
198 199
    # Do not allow anything outside of the usual directories. I do not think
    # there is a security worry, but good to enforce it anyway.
200 201
    #
    if (!preg_match("/^([-\@\w\.\/]+)$/", $exp_localnsfile)) {
Mike Hibler's avatar
Mike Hibler committed
202
	USERERROR("NS File: Pathname includes illegal characters", 1);
203
    }
204 205 206
    if (!VALIDUSERPATH($exp_localnsfile)) {
	USERERROR("NS File: You must specify a server resident file in " .
		  "one of: ${TBVALIDDIRS}.", 1);
207
    }
208
    
209 210 211 212 213 214 215 216 217 218 219
    $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.
    #
220 221
    $nsfile = $_FILES['exp_nsfile']['tmp_name'];
    chmod($nsfile, 0666);
222
    $nonsfile = 0;
223 224
}
elseif ($specform) {
225 226 227 228 229 230 231 232 233 234 235 236
    #
    # 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();
237

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

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

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

    PAGEFOOTER();
    exit();
266 267 268 269 270
}

# Avoid SIGPROF in child.
set_time_limit(0);

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

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

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

Chad Barb's avatar
 
Chad Barb committed
327 328 329 330 331 332 333
#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>