newosid.php3 7.02 KB
Newer Older
1
<?php
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2007 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
5 6
# All rights reserved.
#
7 8
include("defs.php3");
include("showstuff.php3");
9
include("osiddefs.php3");
10 11 12 13

#
# Standard Testbed Header
#
14
PAGEHEADER("Create a new OS Descriptor");
15 16

#
17
# Only known and logged in users.
18
#
19 20
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
21
$dbid      = $this_user->dbid();
22
$isadmin   = ISADMIN();
23 24 25 26 27 28 29

#
# 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. 
#
30 31 32
if (!isset($osname) ||
    strcmp($osname, "") == 0) {
  FORMERROR("Descriptor Name");
33 34 35 36 37 38 39
}
if (!isset($pid) ||
    strcmp($pid, "") == 0) {
  FORMERROR("Select Project");
}
if (!isset($description) ||
    strcmp($description, "") == 0) {
40
  FORMERROR("Description");
41 42 43 44 45 46 47 48 49 50 51
}
if (isset($os_path) &&
    strcmp($os_path, "") == 0) {
    unset($os_path);
}
if (isset($os_version) &&
    strcmp($os_version, "") == 0) {
    unset($os_version);
}

#
52
# Check OS.
53
#
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
if (!isset($OS) ||
    strcmp($OS, "") == 0 ||
    strcmp($OS, "none") == 0) {
    FORMERROR("Operating System (OS)");
}
elseif (! preg_match("/^[-\w]+$/", $OS)) {
    USERERROR("Operating System (OS) - Illegal Characters", 1);
}
elseif (! array_key_exists($OS, $osid_oslist)) {
    USERERROR("Operating System (OS) - Invalid OS", 1);
}
elseif (! $osid_oslist[$OS] && !$isadmin) {
    USERERROR("Operating System (OS) - Not enough permission", 1);
}

# Check OSid.
70 71 72
if (! ereg("^[-_a-zA-Z0-9\.]+$", $osname)) {
    USERERROR("The Descriptor name must consist of alphanumeric characters ".
	      "and dash, dot, or underscore!", 1);
73 74 75
}

if (isset($os_path)) {
76
    if (strcmp($os_path, "") == 0) {
77 78 79
	unset($os_path);
    }
    else {
80
	if (! ereg("^[-_a-zA-Z0-9\/\.:]+$", $os_path)) {
81 82 83 84 85
	    USERERROR("The path contains invalid characters!", 1);
	}
    }
}

86 87 88
if (! isset($os_version)) {
    if (! $isadmin) {
	USERERROR("You must supply a version string!", 1);
89 90 91
    }
    $os_version = "";
}
92 93 94 95
else if (strcmp($os_version, "") &&
	! ereg("^[-_a-zA-Z0-9\.]+$", $os_version)) {
	USERERROR("The version string contains invalid characters!", 1);
}
96 97 98 99

#
# Database limits
#
100 101
if (strlen($osname) > $TBDB_OSID_OSNAMELEN) {
    USERERROR("The Descriptor name is too long! Please select another.", 1);
102 103 104 105 106 107 108 109 110 111 112 113 114 115
}

#
# Certain of these values must be escaped or otherwise sanitized.
# 
$description = addslashes($description);
if (isset($os_magic)) {
    $os_magic = addslashes($os_magic);
}
else {
    $os_magic = "";
}

#
116
# Verify permission.
117
#
118 119 120 121 122 123 124 125 126 127
if (!TBProjAccessCheck($uid, $pid, 0, $TB_PROJECT_MAKEOSID)) {
    USERERROR("You do not have permission to create OS Descriptors ".
	      "in Project $pid!", 1);
}

#
# Only admin types can set the shared bit,
#
if (isset($os_shared) &&
    strcmp($os_shared, "Yep") == 0) {
128
    if (!$isadmin) {
129
	USERERROR("Only Emulab Administrators can set the shared flag!", 1);
130
    }
131
    $os_shared = 1;
132
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
133
else {
134
    $os_shared = 0;
135 136
}

137 138 139 140 141 142 143 144 145 146 147 148 149 150
#
# Only admin types can muck with the mustclean bit.
#
if (isset($os_clean) &&
    strcmp($os_clean, "Yep") == 0) {
    if (!$isadmin) {
	USERERROR("Only Emulab Administrators can set the clean flag!", 1);
    }
    $os_mustclean = 0;
}
else {
    $os_mustclean = 1;
}

151
#
152 153 154 155
# OS Features.
# 
# As a side effect of validating, form the os features set as a string
# for the insertion below. 
156 157
#
$os_features_array = array();
158 159 160 161 162 163 164 165 166 167 168 169

while (list ($feature, $userokay) = each($osid_featurelist)) {
    $foo = "os_feature_$feature";
    
    if (isset($$foo) && strcmp($$foo, "checked") == 0) {
	if (!$userokay && !$isadmin) {
	    USERERROR("Feature '$feature' requires admin mode!", 1);
	}
	else {
	    $os_features_array[] = $feature;
	}
    }
170
}
171 172
$os_features = join(",", $os_features_array);

173 174 175
# Check op_mode
if (!isset($op_mode) ||
    strcmp($op_mode, "") == 0 ||
176
    strcmp($op_mode, "none") == 0) {
177 178
    FORMERROR("Operational Mode (op_mode)");
}
179 180 181 182 183 184 185 186 187
elseif (! preg_match("/^[-\w]+$/", $op_mode)) {
    USERERROR("Operational Mode (op_mode) - Illegal Characters", 1);
}
elseif (! array_key_exists($op_mode, $osid_opmodes)) {
    USERERROR("Operational Mode (op_mode) - Invalid op_mode", 1);
}
elseif (! $osid_opmodes[$OS] && !$isadmin) {
    USERERROR("Operational Mode (o_mode) - Not enough permission", 1);
}
188

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
# Check nextosid. Only admin users, and only allowed if no version is given.

if (isset($nextosid)) {
    if ($nextosid == "" || $nextosid == "none") {
	unset($nextosid);
    }
    elseif (!$isadmin) {
	USERERROR("Setting nextosid requires admin mode!", 1);
    }
    elseif (!TBvalid_osid($nextosid)) {
	USERERROR("NextOsid: ". TBFieldErrorString(), 1);
    }
    elseif (! TBValidOSID($nextosid)) {
	USERERROR("NextOsid: $nextosid is not a known osid!", 1);
    }
}

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
#
# Check the reboot_waittime. Only admin users can set this. Grab default
# if not set.
#
if (isset($reboot_waittime) && $reboot_waittime != "") {
    if (!$isadmin) {
	USERERROR("Setting reboot waittime requires admin mode!", 1);
    }
    elseif (!TBvalid_integer($reboot_waittime)) {
	USERERROR("Reboot Waittime: ". TBFieldErrorString(), 1);
    }
}
else {
    if (! array_key_exists($OS, $osid_reboot_waitlist)) {
	USERERROR("Operating System (OS) - No default reboot waittime", 1);
    }
    else {
	$reboot_waittime = $osid_reboot_waitlist[$OS];
    }
}

227 228 229 230 231 232 233 234 235 236
#
# And insert the record!
#
if (isset($os_path)) {
    $os_path = "'$os_path'";
}
else {
    $os_path = "NULL";
}

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
DBQueryFatal("lock tables os_info write");

#
# Of course, the OS record may not already exist in the DB.
#
if (TBValidOS($pid, $osname)) {
    DBQueryFatal("unlock tables");
    USERERROR("The OS Descriptor '$osname' already exists in Project $pid! ".
              "Please select another.", 1);
}

#
# Just concat them to form a unique imageid. 
# 
$osid = "$pid-$osname";
if (TBValidOSID($osid)) {
    DBQueryFatal("unlock tables");
    TBERROR("Could not form a unique osid for $pid/$osname!", 1);
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
257 258
$query_result =
    DBQueryFatal("INSERT INTO os_info ".
259
		 "(osname, osid, description,OS,version,path,magic,op_mode, ".
260 261
		 " osfeatures, pid, shared, creator, creator_idx, mustclean, ".
		 " created, reboot_waittime) ".
262
		 "VALUES ('$osname', '$osid', '$description', '$OS', ".
263
		 "        '$os_version', $os_path, '$os_magic', '$op_mode', ".
264
		 "        '$os_features', '$pid', $os_shared, ".
265 266
	         "        '$uid', '$dbid', $os_mustclean, now(), ".
		 "        $reboot_waittime)");
267 268 269

DBQueryFatal("unlock tables");

270 271 272 273
if (isset($nextosid)) {
    DBQueryFatal("update os_info set nextosid='$nextosid' where osid='$osid'");
}

274 275 276 277 278 279 280
SUBPAGESTART();
SUBMENUSTART("More Options");
WRITESUBMENUBUTTON("Delete this OS Descriptor",
		   "deleteosid.php3?osid=$osid");
WRITESUBMENUBUTTON("Create a new OS Descriptor",
		   "newosid_form.php3");
WRITESUBMENUBUTTON("Create a new Image Descriptor",
281
		   "newimageid_ez.php3");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
282
WRITESUBMENUBUTTON("OS Descriptor list",
283
		   "showosid_list.php3");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
284
WRITESUBMENUBUTTON("Image Descriptor list",
285 286
		   "showimageid_list.php3");
SUBMENUEND();
287 288 289 290 291 292

#
# Dump os_info record.
# 
SHOWOSINFO($osid);

293
SUBPAGEEND();
294 295 296 297 298 299

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