template_create.php 15.8 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2006-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
#
include("defs.php3");
25
include_once("template_defs.php");
26 27 28 29 30 31 32 33

#
# No PAGEHEADER since we spit out a Location header later. See below.
#

#
# Only known and logged in users.
#
34 35 36
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
37

38 39 40
# This will not return if its a sajax request.
include("showlogfile_sup.php3");

41 42 43 44 45 46
#
# Verify page arguments.
#
$optargs = OptionalPageArguments("create",     PAGEARG_STRING,
				 "formfields", PAGEARG_ARRAY); 

47 48 49 50 51 52 53
#
# Spit the form out using the array of data.
#
function SPITFORM($formfields, $errors)
{
    global $TBDB_PIDLEN, $TBDB_GIDLEN, $TBDB_EIDLEN, $TBDOCBASE;
    global $projlist;
54
    global $TBVALIDDIRS_HTML;
55
    global $WIKIDOCURL;
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

    PAGEHEADER("Create an Experiment Template");

    if ($errors) {
	echo "<table class=nogrid
                     align=center border=0 cellpadding=6 cellspacing=0>
              <tr>
                 <th align=center colspan=2>
                   <font size=+1 color=red>
                      &nbsp;Oops, please fix the following errors!&nbsp;
                   </font>
                 </td>
              </tr>\n";

	while (list ($name, $message) = each ($errors)) {
71 72
            # XSS prevention.
	    $message = CleanString($message);
73 74 75 76 77 78 79 80 81
	    echo "<tr>
                     <td align=right>
                       <font color=red>$name:&nbsp;</font></td>
                     <td align=left>
                       <font color=red>$message</font></td>
                  </tr>\n";
	}
	echo "</table><br>\n";
    }
82 83 84 85
    # XSS prevention.
    while (list ($key, $val) = each ($formfields)) {
	$formfields[$key] = CleanString($val);
    }
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

    echo "<form enctype=multipart/form-data
                action=template_create.php method=post>\n";
    echo "<table align=center border=1>\n";

    #
    # Select Project
    #
    echo "<tr>
	  <td class='pad4'>Select Project:</td>
	  <td class='pad4'><select name=\"formfields[pid]\">\n";

    # If just one project, make sure just the one option.
    if (count($projlist) != 1) {
	echo "<option value=''>Please Select &nbsp</option>\n";
    }

    while (list($project) = each($projlist)) {
	$selected = "";

106
	if (strcmp($formfields["pid"], $project) == 0)
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	    $selected = "selected";

	echo "        <option $selected value=\"$project\">
			     $project </option>\n";
    }
    echo "       </select>";
    echo "    </td>
	  </tr>\n";

    #
    # Select a group
    #
    echo "<tr>
	      <td class='pad4'>Group:</td>
	      <td class='pad4'><select name=\"formfields[gid]\">
	          <option value=''>Default Group </option>\n";

    reset($projlist);
    while (list($project, $grouplist) = each($projlist)) {
	for ($i = 0; $i < count($grouplist); $i++) {
	    $group    = $grouplist[$i];

	    if (strcmp($project, $group)) {
		$selected = "";

132 133 134 135
		if (isset($formfields["gid"]) &&
		    isset($formfields["pid"]) &&
		    strcmp($formfields["pid"], $project) == 0 &&
		    strcmp($formfields["gid"], $group) == 0)
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
		    $selected = "selected";

		echo "<option $selected value=\"$group\">
			   $project/$group</option>\n";
	    }
	}
    }
    echo "     </select>
	   <font size=-1>(Must be default or correspond to selected project)
	   </font>
	  </td>
	 </tr>\n";

    #
    # TID:
    #
    echo "<tr>
              <td class='pad4'>Template ID:
              <br><font size='-1'>(alphanumeric, no blanks)</font></td>
              <td class='pad4' class=left>
                  <input type=text
                         name=\"formfields[tid]\"
158
                         value=\"" . $formfields["tid"] . "\"
159 160 161 162 163 164 165 166
	                 size=$TBDB_EIDLEN
                         maxlength=$TBDB_EIDLEN>
              </td>
          </tr>\n";

    #
    # NS file
    #
167 168
    if (isset($formfields["nsref"])) {
	$nsref = $formfields["nsref"];
169
	
170 171
	if (isset($formfields["guid"])) {
	    $guid = $formfields["guid"];
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
	    
	    echo "<tr>
                  <td class='pad4'>Your auto-generated NS file: &nbsp</td>
                      <input type=hidden name=\"formfields[nsref]\"
                             value=$nsref>
                      <input type=hidden name=\"formfields[guid]\"
                             value=$guid>
                  <td class='pad4'>
                      <a target=_blank
                             href=\"spitnsdata.php3?nsref=$nsref&guid=$guid\">
                      View NS File</a></td>
                  </tr>\n";
        }
	else {
	    echo "<tr>
                   <td class='pad4'>Your auto-generated NS file: &nbsp</td>
                       <input type=hidden name=\"formfields[nsref]\"
                              value=$nsref>
                   <td class='pad4'>
                       <a target=_blank href=spitnsdata.php3?nsref=$nsref>
                       View NS File</a></td>
                 </tr>\n";
        }
    }
    else {
	echo "<tr>
                  <td class='pad4'>Your NS file: </td>
                  <td><table cellspacing=0 cellpadding=0 border=0>
                    <tr>
                      <td class='pad4'>Upload<br>
			<font size='-1'>(500k&nbsp;max)</font></td>
                      <td class='pad4'>
                        <input type=hidden name=MAX_FILE_SIZE value=512000>
	                <input type=file
                               name=nsfile
207
                               value=\"" . $formfields["nsfile"] . "\"
208 209 210 211 212 213
	                       size=30>
                      </td>
                    </tr><tr>
                    <td>&nbsp;&nbsp;<b>or</b></td><td></td>
                    </tr><tr>
                      <td class='pad4'>On Server<br>
214
                              <font size='-1'>($TBVALIDDIRS_HTML)</font></td>
215 216 217
                      <td class='pad4'>
	                <input type=text
                               name=\"formfields[localnsfile]\"
218
                               value=\"" . $formfields["localnsfile"] . "\"
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
	                       size=40>
                      </td>
                     </tr>
                     </table>
                   </td>
               </tr>\n";
    }

    echo "<tr>
              <td colspan=2>
               Use this text area to describe your template:
              </td>
          </tr>
          <tr>
              <td colspan=2 align=center class=left>
                  <textarea name=\"formfields[description]\"
                    rows=10 cols=80>" .
236
	            str_replace("\r", "", $formfields["description"]) .
237 238 239 240 241 242 243 244 245 246 247
	           "</textarea>
              </td>
          </tr>\n";

    echo "<tr>
              <td class='pad4' align=center colspan=2>
                <b><input type=submit name=create value='Create Template'></b>
              </td>
         </tr>
        </form>
        </table>\n";
Leigh Stoller's avatar
Leigh Stoller committed
248 249 250 251

    echo "<blockquote><blockquote>
          <ol>
            <li> Please read this
252
                <a href='$WIKIDOCURL/kb112'>KB entry</a>
Leigh Stoller's avatar
Leigh Stoller committed
253 254 255
                to see what NS extensions are available for templates.
          </ol>
          </blockquote></blockquote>\n";
256 257 258 259 260
}

#
# See what projects the uid can create experiments in. Must be at least one.
#
261
$projlist = $this_user->ProjectAccessList($TB_PROJECT_CREATEEXPT);
262 263 264 265 266 267 268 269 270 271

if (! count($projlist)) {
    USERERROR("You do not appear to be a member of any Projects in which ".
	      "you have permission to create new experiment templates.", 1);
}

#
# On first load, display virgin form and exit.
#
if (!isset($create)) {
272 273 274 275 276 277 278 279 280 281
    $defaults = array();

    $defaults["pid"]               = "";
    $defaults["gid"]               = "";
    $defaults["tid"]               = "";
    $defaults["pid"]               = "";
    $defaults["description"]       = "";
    $defaults["localnsfile"]       = "";
    $defaults["nsfile"]            = ""; # Multipart data.

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    #
    # For users that are in one project and one subgroup, it is usually
    # the case that they should use the subgroup, and since they also tend
    # to be in the clueless portion of our users, give them some help.
    #
    if (count($projlist) == 1) {
	list($project, $grouplist) = each($projlist);

	if (count($grouplist) <= 2) {
	    $defaults['pid'] = $project;
	    if (count($grouplist) == 1 || strcmp($project, $grouplist[0]))
		$defaults['gid'] = $grouplist[0];
	    else
		$defaults['gid'] = $grouplist[1];
	}
	reset($projlist);
    }

    #
    # Allow formfields that are already set to override defaults
    #
    if (isset($formfields)) {
	while (list ($field, $value) = each ($formfields)) {
	    $defaults[$field] = $formfields[$field];
	}
    }

    SPITFORM($defaults, 0);
310
    PAGEFOOTER();
311 312
    return;
}
313 314 315

# Make sure we got that formfields array!
if (! isset($formfields)) {
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
    PAGEARGERROR();
}

#
# Okay, validate form arguments.
#
$errors = array();

# Some local variables.
$nsfilelocale    = 0;
$thensfile       = 0;
$deletensfile    = 0;

#
# Project:
#
332
if (!isset($formfields["pid"]) || $formfields["pid"] == "") {
333 334
    $errors["Project"] = "Not Selected";
}
335
elseif (!TBvalid_pid($formfields["pid"])) {
336 337
    $errors["Project"] = TBFieldErrorString();
}
338
elseif (! ($project = Project::Lookup($formfields["pid"]))) {
339 340
    $errors["Project"] = "No such project";
}
341 342 343 344 345 346 347 348 349 350 351 352
else {
    #
    # Group: If none specified, then use default group (see below).
    #
    if (isset($formfields["gid"]) && $formfields["gid"] != "") {
	if (!TBvalid_gid($formfields["gid"])) {
	    $errors["Group"] = TBFieldErrorString();
	}
	elseif (! ($group = Group::LookupByPidGid($formfields["pid"],
						  $formfields["gid"]))) {
	    $errors["Group"] = "No such group in project'";
	}
353
    }
354 355
    else {
	$group = $project->DefaultGroup();
356 357 358 359 360 361
    }
}

#
# TID
#
362
if (!isset($formfields["tid"]) || $formfields["tid"] == "") {
363 364
    $errors["Template ID"] = "Missing Field";
}
365
elseif (!TBvalid_eid($formfields["tid"])) {
366 367 368 369 370 371
    $errors["Template ID"] = TBFieldErrorString();
}

#
# Description:
# 
372
if (!isset($formfields["description"]) || $formfields["description"] == "") {
373 374
    $errors["Description"] = "Missing Field";
}
375
elseif (!TBvalid_template_description($formfields["description"])) {
376 377 378 379 380 381
    $errors["Description"] = TBFieldErrorString();
}

#
# The NS file. There is a bunch of stuff here for Netbuild.
#
382
$formfields["nsfile"] = "";
383

384 385 386
if (isset($formfields["guid"])) {
    if ($formfields["guid"] == "" ||
	!preg_match("/^\d+$/", $formfields["guid"])) {
387 388 389 390
	$errors["NS File GUID"] = "Invalid characters";
    }
    $nsfilelocale = "nsref";
}
391 392 393
elseif (isset($formfields["nsref"])) {
    if ($formfields["nsref"] == "" ||
	!preg_match("/^\d+$/", $formfields["nsref"])) {
394 395 396 397
	$errors["NS File Reference"] = "Invalid characters";
    }
    $nsfilelocale = "nsref";
}
398 399 400
elseif (isset($formfields["localnsfile"]) &&
	$formfields["localnsfile"] != "") {
    if (!preg_match("/^([-\@\w\.\/]+)$/", $formfields["localnsfile"])) {
401 402
	$errors["Server NS File"] = "Pathname includes illegal characters";
    }
403
    elseif (! VALIDUSERPATH($formfields["localnsfile"])) {
404 405
	$errors["Server NS File"] =
		"Must reside in one of: $TBVALIDDIRS";
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
    }
    $nsfilelocale = "local";
}
elseif (isset($_FILES['nsfile']) && $_FILES['nsfile']['size'] != 0) {
    if ($_FILES['nsfile']['size'] > (1024 * 500)) {
	$errors["Local NS File"] = "Too big!";
    }
    elseif ($_FILES['nsfile']['name'] == "") {
	$errors["Local NS File"] =
	    "Local filename does not appear to be valid";
    }
    elseif ($_FILES['nsfile']['tmp_name'] == "") {
	$errors["Local NS File"] =
	    "Temp filename does not appear to be valid";
    }
    elseif (!preg_match("/^([-\@\w\.\/]+)$/",
			$_FILES['nsfile']['tmp_name'])) {
	$errors["Local NS File"] = "Temp path includes illegal characters";
    }
    #
    # For the benefit of the form. Remember to pass back actual filename, not
    # the php temporary file name. Note that there appears to be some kind
    # of breakage, at least in opera; filename has no path.
    #
Leigh Stoller's avatar
Leigh Stoller committed
430
    $formfields["nsfile"] = $_FILES['nsfile']['name'];
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
    $nsfilelocale = "remote";
}
else {
    $errors["NS File"] = "You must provide an NS file";
}

if (count($errors)) {
    SPITFORM($formfields, $errors);
    PAGEFOOTER();
    exit(1);
}

#
# Okay, we can run the backend script.
#
446 447 448 449
$description  = escapeshellarg($formfields["description"]);
$pid          = $formfields["pid"];
$gid          = $formfields["gid"];
$tid          = $formfields["tid"];
450 451 452 453 454 455 456 457 458

# Default to pid=gid;
if (!isset($gid) || $gid == "") {
    $gid = $pid;
}

#
# Verify permissions. We do this here since pid/eid/gid could be bogus above.
#
459
if (! $group->AccessCheck($this_user, $TB_PROJECT_CREATEEXPT)) {
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
    $errors["Project/Group"] = "Not enough permission to create template";
    SPITFORM($formfields, $errors);
    PAGEFOOTER();
    exit(1);
}

#
# Figure out the NS file to give to the script. 
#
if ($nsfilelocale == "local") {
    #
    # 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 so the user will get immediate feedback if the filename
    # is bogus.
    #
476
    $thensfile = $formfields["localnsfile"];
477 478
}
elseif ($nsfilelocale == "nsref") {
479
    $nsref = $formfields["nsref"];
480
    
481 482
    if (isset($formfields["guid"])) {
	$guid      = $formfields["guid"];
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
	$thensfile = "/tmp/$guid-$nsref.nsfile";
    }
    else {
	$thensfile = "/tmp/$uid-$nsref.nsfile";
    }
    if (! file_exists($thensfile)) {
	$errors["NS File"] = "Temp file no longer exists on server";
	SPITFORM($formfields, $errors);
	PAGEFOOTER();
	exit(1);
    }
    $deletensfile = 1;
}
elseif ($nsfilelocale == "remote") {
    #
    # The NS file was uploaded to a temp file.
    #
    $thensfile = $_FILES['nsfile']['tmp_name'];

    if (! file_exists($thensfile)) {
	$errors["NS File"] = "Temp file no longer exists on server";
	SPITFORM($formfields, $errors);
	PAGEFOOTER();
	exit(1);
    }
    chmod($thensfile, 0666);
}

#
# Grab the unix GID for running scripts.
#
514
$unix_gid = $group->unix_gid();
515
$unix_pid = $project->unix_gid();
516 517 518 519

# Okay, we can spit back a header now that there is no worry of redirect.
PAGEHEADER("Create an Experiment Template");

Leigh Stoller's avatar
Leigh Stoller committed
520 521 522 523
echo "<script type='text/javascript' language='javascript' ".
     "        src='template_sup.js'>\n";
echo "</script>\n";

524
STARTBUSY("Starting template creation!");
525 526

# And run that script!
527
$retval = SUEXEC($uid, "$unix_pid,$unix_gid",
528
		 "webtemplate_create -E $description ".
529 530 531 532 533 534
		 "-g $gid $pid $tid $thensfile",
		 SUEXEC_ACTION_IGNORE);

if ($deletensfile) {
    unlink($thensfile);
}
Leigh Stoller's avatar
Leigh Stoller committed
535
/* Clear the various 'loading' indicators. */
536
HIDEBUSY();
Leigh Stoller's avatar
Leigh Stoller committed
537

538
if ($retval) {
539 540 541 542 543 544 545 546
    #
    # 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_CONTINUE);
    }
    # User error. Tell user and exit.
547 548 549 550
    SUEXECERROR(SUEXEC_ACTION_USERERROR);
    return;
}

551 552 553
#
# Parse the last line of output. Ick.
#
554
if (preg_match("/^Template\s+(\w+)\/(\w+)\s+is being/",
555 556 557 558
	       $suexec_output_array[count($suexec_output_array)-1],
	       $matches)) {
    $guid = $matches[1];
    $vers = $matches[2];
Leigh Stoller's avatar
Leigh Stoller committed
559

560 561 562 563
    $template = Template::Lookup($guid, $vers);
    if (! $template) {
	TBERROR("Could not lookup template object for $guid/$vers", 1);
	return;
564
    }
565 566 567
    echo $template->PageHeader();
    echo "<br><br>\n";
    STARTLOG($template);
568
}
569 570 571
else {
    SUEXECERROR(SUEXEC_ACTION_DIE);
}    
572 573 574 575 576 577

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