newimageid.php3 5.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
<?php
include("defs.php3");
include("showstuff.php3");

#
# Standard Testbed Header
#
PAGEHEADER("Create a new ImageID");

#
# Only known and logged in users can create an OSID.
#
$uid = GETLOGIN();
LOGGEDINORDIE($uid);
$isadmin = ISADMIN($uid);

#
# First off, sanity check the form to make sure all the required fields
# were provided. I do this on a per field basis so that we can be
# informative. Be sure to correlate these checks with any changes made to
# the project form. 
#
if (!isset($imageid) ||
    strcmp($imageid, "") == 0) {
  FORMERROR("ImageID");
}
if (!isset($pid) ||
    strcmp($pid, "") == 0) {
  FORMERROR("Select Project");
}
if (!isset($description) ||
    strcmp($description, "") == 0) {
  FORMERROR("ImageID Description");
}
if (!isset($loadpart) ||
    strcmp($loadpart, "") == 0) {
  FORMERROR("Starting DOS Slice");
}
if (!isset($loadlength) ||
    strcmp($loadlength, "") == 0) {
  FORMERROR("Number of DOS Slice");
}
if (!isset($path) ||
    strcmp($path, "") == 0) {
  FORMERROR("Path");
}
if (!isset($default_osid) ||
    strcmp($default_osid, "") == 0) {
  FORMERROR("Default OSID");
}
if (isset($loadaddr) &&
    strcmp($loadaddr, "") == 0) {
    unset($loadaddr);
}

#
# Check ID and others for sillyness.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
59
if (! ereg("^[-_a-zA-Z0-9\.]+$", $imageid)) {
60 61 62
    USERERROR("The ImageID must consist of alphanumeric characters and ".
	      "dash, dot, or underscore!", 1);
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
63
if (! ereg("^[-_a-zA-Z0-9\/\.]+$", $path)) {
64 65 66 67
    USERERROR("The path contains invalid characters!", 1);
}

if (isset($loadaddr) &&
Leigh B. Stoller's avatar
Leigh B. Stoller committed
68
    !ereg("^[0-9\.]+$", $loadaddr)) {
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 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 207 208 209 210 211 212 213 214 215 216 217 218 219
	USERERROR("The load address contains invalid characters!", 1);
}

#
# Check sanity of the loadpart and loadlength.
#
if (! ereg("^[0-9]+$", $loadpart) ||
    $loadpart < 0 || $loadpart > 4) {
    USERERROR("The Load Partition must 0, or 1,2,3, or 4!", 1);
}
if (! ereg("^[0-9]+$", $loadlength) ||
    $loadlength < 1 || $loadlength > 4) {
    USERERROR("The Load Length must be 1,2,3, or 4!", 1);
}
if (($loadpart != 0 && $loadlength != 1)) {
    USERERROR("Currently, only single slices or partial disks are allowed. ".
	      "If you specify a non-zero starting load partition, the load ".
	      "load length must be one (a single slice). If you specify zero ".
	      "for the starting load partition, then you can include any or ".
	      "all of the slices (1-4). Note that '0' means to start at the ".
	      "boot sector, while '1' means to start at the beginning of the ".
	      "first slice (typically starting at sector 63).", 1);
}

#
# Check sanity of the OSIDs for each slice. Permission checks not needed.
#
#
# Silently kill off all the extraneous OSIDs.
#
for ($i = 1; $i <= 4; $i++) {
    $foo      = "part${i}_osid";
    $thisosid = $$foo;

    if (($loadpart && $i == $loadpart) ||
	(!$loadpart && $i <= $loadlength)) {

	if (!isset($thisosid) ||
	    strcmp($thisosid, "none") == 0) {
	    USERERROR("You must select an OSID for Slice $i!", 1);
	}
	    
	if (!TBValidOSID($thisosid)) {
	    USERERROR("The OSID '$this_osid' in not a valid OSID!", 1);
	}
	$$foo = "'$thisosid'";
    }
    else {
	$$foo = "NULL";
    }
}

if (!isset($default_osid) ||
    strcmp($default_osid, "none") == 0) {
    USERERROR("You must select a default boot OSID!", 1);
}
if (!TBValidOSID($default_osid)) {
    USERERROR("The Default OSID '$default_osid' in not a valid OSID!", 1);
}

#
# Database limits
#
if (strlen($osid) > $TBDB_IMAGEID_IMAGEIDLEN) {
    USERERROR("The ImageID name is too long! Please select another.", 1);
}

#
# Of course, the ImageID record may not already exist in the DB.
#
if (TBValidImageID($imageid)) {
    USERERROR("The ImageID '$imageid' already exists! ".
              "Please select another.", 1);
}

#
# Certain of these values must be escaped or otherwise sanitized.
# 
$description = addslashes($description);

#
# Only admin types can set the PID to none (for sharing).
#
if (!isset($pid) ||
    strcmp($pid, "none") == 0) {
    if (!$isadmin) {
	USERERROR("Only Emulab Administrators can specify 'None' for the ".
                  "project.", 1);
    }
    unset($pid);
}
else {
    #
    # Verify permission.
    #
    if (!TBProjAccessCheck($uid, $pid, 0, $TB_PROJECT_MAKEIMAGEID)) {
	USERERROR("You do not have permission to create ImageID $imageid!", 1);
    }
}

#
# And insert the record!
#
if (isset($pid)) {
    $pid = "'$pid'";
}
else {
    $pid = "NULL";
}
if (isset($loadaddr)) {
    $loadaddr = "'$loadaddr'";
}
else {
    $loadaddr = "NULL";
}

$query_result =
    DBQueryFatal("INSERT INTO images ".
		 "(imageid, description, loadpart, loadlength, ".
		 " part1_osid, part2_osid, part3_osid, part4_osid, ".
		 " default_osid, path, pid, load_address) ".
		 "VALUES ".
		 "  ('$imageid', '$description', $loadpart, $loadlength, ".
		 "   $part1_osid, $part2_osid, $part3_osid, $part4_osid, ".
		 "   '$default_osid', '$path', $pid, $loadaddr)");

#
# Dump os_info record.
# 
SHOWIMAGEID($imageid, 0);

# Edit option.
$fooid = rawurlencode($imageid);
echo "<p><center>
       Do you want to edit this ImageID?
       <A href='editimageid_form.php3?imageid=$fooid'>Yes</a>
      </center>\n";

echo "<br><br>\n";

# Delete option.
echo "<p><center>
       Do you want to delete this ImageID?
       <A href='deleteimageid.php3?imageid=$fooid'>Yes</a>
      </center>\n";    

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