nscheck.php3 4.9 KB
Newer Older
1
<?php
Leigh Stoller's avatar
Leigh Stoller committed
2
#
3
# Copyright (c) 2000-2007, 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/>.
# 
# }}}
Leigh Stoller's avatar
Leigh Stoller committed
23
#
24 25
include("defs.php3");

26 27 28 29 30 31 32 33 34
#
# Define a stripped-down view of the web interface - less clutter
#
$view = array(
    'hide_banner' => 1,
    'hide_sidebar' => 1,
    'hide_copyright' => 1
);

35 36 37
#
# Only known and logged in users can begin experiments.
#
38 39 40
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
41

Russ Fish's avatar
Russ Fish committed
42 43 44
#
# Verify page arguments.
#
45 46 47 48 49 50 51 52 53 54
$optargs = OptionalPageArguments("nsdata",          PAGEARG_ANYTHING,
				 "formfields",      PAGEARG_ARRAY,
				 "fromform",        PAGEARG_BOOLEAN);

#
# Standard Testbed Header (not that we know if want the stripped version).
#
PAGEHEADER("Syntax Check an NS File",
	   (isset($fromform) || isset($nsdata) ? $view : null));

55
#
56 57 58 59
# Not allowed to specify both a local and an upload!
#
$speclocal  = 0;
$specupload = 0;
60
$specform   = 0;
61
$nsfile     = "";
62
$tmpfile    = 0;
63 64 65 66

if (isset($formfields)) {
    $exp_localnsfile = $formfields['exp_localnsfile'];
}
67 68 69 70

if (isset($exp_localnsfile) && strcmp($exp_localnsfile, "")) {
    $speclocal = 1;
}
71 72 73
if (isset($_FILES['exp_nsfile']) &&
    $_FILES['exp_nsfile']['name'] != "" &&
    $_FILES['exp_nsfile']['tmp_name'] != "") {
74 75 76
    if ($_FILES['exp_nsfile']['size'] == 0) {
        USERERROR("Uploaded NS file does not exist, or is empty");
    }
77 78
    $specupload = 1;
}
79 80 81
if (!$speclocal && !$specupload && isset($nsdata))  {
    $specform = 1;
}
82

83
if ($speclocal + $specupload + $specform > 1) {
84 85 86
    USERERROR("You may not specify both an uploaded NS file and an ".
	      "NS file that is located on the Emulab server", 1);
}
87
#
88
# Gotta be one of them!
89
#
90
if (!$speclocal && !$specupload && !$specform) {
91 92 93 94 95 96 97 98 99 100
    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.
    #
101 102
    # Do not allow anything outside of the usual directories. I do not think
    # there is a security worry, but good to enforce it anyway.
103
    #
104
    if (!preg_match("/^([-\@\w\.\/]+)$/", $exp_localnsfile)) {
105
	USERERROR("NS File: Pathname includes illegal characters", 1);
106
    }
107 108 109
    if (!VALIDUSERPATH($exp_localnsfile)) {
	USERERROR("NS File: You must specify a server resident file in " .
		  "one of: ${TBVALIDDIRS}.", 1);
110 111 112 113 114
    }
    
    $nsfile = $exp_localnsfile;
    $nonsfile = 0;
}
115
elseif ($specupload) {
116 117 118 119 120 121 122
    #
    # 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.
    #
123 124
    $nsfile = $_FILES['exp_nsfile']['tmp_name'];
    chmod($nsfile, 0666);
125
    $nonsfile = 0;
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
} else # $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();

    $nsfile = "/tmp/$uid-$foo.nsfile";
    $handle = fopen($nsfile,"w");
    fwrite($handle,$nsdata);
    fclose($handle);
145
}
146

147 148 149 150
STARTBUSY("Starting NS syntax check from " .
	  ($speclocal ? "local file" :
	   ($specupload ? "uploaded file" :
	    ($specform ? "form" : "???"))));
151 152
$retval = SUEXEC($uid, "nobody", "webnscheck " . escapeshellarg($nsfile),
		 SUEXEC_ACTION_IGNORE);
153

154 155 156 157
if ($tmpfile) {
    unlink($nsfile);
}

158 159 160 161 162 163 164 165 166 167 168
#
# 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("");
}
169
STOPBUSY();
170

171
# Parse Error.	 
172 173 174 175 176
if ($retval) {
    echo "<br><br><h2>
          Parse Failure($retval): Output as follows:
          </h2>
          <br>
177
          <XMP>$suexec_output</XMP>\n";
178
    PAGEFOOTER();
179 180 181
    die("");
}

182
echo "<center>";
183 184 185 186 187 188 189 190 191
echo "<br>";
echo "<h2>Your NS file looks good!</h2>";
echo "</center>\n";

#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>