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

9 10 11 12 13 14 15 16 17
#
# Define a stripped-down view of the web interface - less clutter
#
$view = array(
    'hide_banner' => 1,
    'hide_sidebar' => 1,
    'hide_copyright' => 1
);

18 19 20
#
# Only known and logged in users can begin experiments.
#
21 22 23
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
24

Russ Fish's avatar
Russ Fish committed
25 26 27
#
# Verify page arguments.
#
28 29 30 31 32 33 34 35 36 37
$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));

38
#
39 40 41 42
# Not allowed to specify both a local and an upload!
#
$speclocal  = 0;
$specupload = 0;
43
$specform   = 0;
44
$nsfile     = "";
45
$tmpfile    = 0;
46 47 48 49

if (isset($formfields)) {
    $exp_localnsfile = $formfields['exp_localnsfile'];
}
50 51 52 53

if (isset($exp_localnsfile) && strcmp($exp_localnsfile, "")) {
    $speclocal = 1;
}
54 55 56
if (isset($_FILES['exp_nsfile']) &&
    $_FILES['exp_nsfile']['name'] != "" &&
    $_FILES['exp_nsfile']['tmp_name'] != "") {
57 58 59
    if ($_FILES['exp_nsfile']['size'] == 0) {
        USERERROR("Uploaded NS file does not exist, or is empty");
    }
60 61
    $specupload = 1;
}
62 63 64
if (!$speclocal && !$specupload && isset($nsdata))  {
    $specform = 1;
}
65

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

130 131 132 133 134
STARTBUSY("Starting NS syntax check from " .
	  ($speclocal ? "local file" :
	   ($specupload ? "uploaded file" :
	    ($specform ? "form" : "???"))));
$retval = SUEXEC($uid, "nobody", "webnscheck $nsfile", SUEXEC_ACTION_IGNORE);
135

136 137 138 139
if ($tmpfile) {
    unlink($nsfile);
}

140 141 142 143 144 145 146 147 148 149 150
#
# 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("");
}
151
STOPBUSY();
152

153
# Parse Error.	 
154 155 156 157 158
if ($retval) {
    echo "<br><br><h2>
          Parse Failure($retval): Output as follows:
          </h2>
          <br>
159
          <XMP>$suexec_output</XMP>\n";
160
    PAGEFOOTER();
161 162 163
    die("");
}

164
echo "<center>";
165 166 167 168 169 170 171 172 173
echo "<br>";
echo "<h2>Your NS file looks good!</h2>";
echo "</center>\n";

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