function processRequestInput() { global $user; $baseaccess = getContinuationVar('baseaccess', 0); $imagingaccess = getContinuationVar('imagingaccess', 0); $serveraccess = getContinuationVar('serveraccess', 0); $openend = getContinuationVar('openend', 0); $nousercheck = getContinuationVar('nousercheck', 0); $return['imaging'] = getContinuationVar('imaging', 0); $maxinitial = getContinuationVar('maxinitial', 0); $noimaging = getContinuationVar('noimaging', array()); $return = array('err' => 0); # type $return['type'] = processInputVar('type', ARG_STRING); if (!preg_match('/^basic|imaging|server$/', $return['type'])) { $return['err'] = 1; $return['errmsg'] = i('Invalid data submitted'); return $return; } if ($return['type'] == 'basic' && !$baseaccess || $return['type'] == 'imaging' && !$imagingaccess || $return['type'] == 'server' && !$serveraccess) { $return['err'] = 1; $return['errmsg'] = i('No access to submitted reservation type'); return $return; } # ending $return['ending'] = processInputVar('ending', ARG_STRING); if (!preg_match('/^indefinite|endat|duration$/', $return['ending'])) { $return['err'] = 1; $return['errmsg'] = i('Invalid data submitted'); return $return; } if ($return['ending'] == 'duration' && !$baseaccess || $return['ending'] == 'indefinite' && !$serveraccess || $return['ending'] == 'endat' && !$openend && !$serveraccess) { $return['err'] = 1; $return['errmsg'] = i('No access to submitted end type'); return $return; } # imageid $return['imageid'] = processInputVar('imageid', ARG_NUMERIC); $resources = getUserResources(array("imageAdmin", "imageCheckOut")); $withnocheckout = $resources['image']; $images = removeNoCheckout($resources["image"]); $extraimages = getServerProfileImages($user['id']); if (!array_key_exists($return['imageid'], $images) && ($return['type'] != 'server' || !array_key_exists($return['imageid'], $extraimages)) && ($return['type'] != 'imaging' || !array_key_exists($return['imageid'], $withnocheckout)) || $return['type'] == 'imaging' && array_key_exists($return['imageid'], $noimaging)) { $return['err'] = 1; $return['errmsg'] = i('No access to submitted environment'); return $return; } # nousercheck $return['nousercheck'] = processInputVar('nousercheck', ARG_NUMERIC); if (!$nousercheck || $return['nousercheck'] != 1) { $return['nousercheck'] = 0; } # revisionid $revids = processInputVar("revisionid", ARG_STRING); $revids = explode(':', $revids); $images = getImages(0, $return['imageid']); $return['revisionids'] = array(); if (array_key_exists('subimages', $images[$return['imageid']])) { $subimages = $images[$return['imageid']]['subimages']; array_unshift($subimages, $return['imageid']); foreach ($subimages as $key => $imgid) { $revisions = getImageRevisions($imgid); if (!array_key_exists($key, $revids) || !is_numeric($revids[$key]) || !array_key_exists($revids[$key], $revisions)) { $revid = getProductionRevisionid($imgid); } else { $revid = $revids[$key]; } if (!array_key_exists($imgid, $return['revisionids'])) { $return['revisionids'][$imgid] = array(); } $return['revisionids'][$imgid][] = $revid; } } elseif ($revids[0] != '' && is_numeric($revids[0])) { $return['revisionids'][$return['imageid']][] = $revids[0]; } else { $return['revisionids'][$return['imageid']][] = getProductionRevisionid($return['imageid']); } # duration if ($return['ending'] == 'duration') { $return['duration'] = processInputVar('duration', ARG_NUMERIC, 0); if ($return['duration'] > $maxinitial) { $return['duration'] = $maxinitial; } } # start/end $return['start'] = processInputVar('start', ARG_NUMERIC); $return['end'] = processInputVar('end', ARG_NUMERIC, 0); $now = time(); if ($return['start'] == 0) { $start = $now; } else { $start = $return['start']; } if ($return['ending'] == 'endat') { $end = $return['end']; } if ($return['ending'] == 'indefinite') { $end = datetimeToUnix('2038-01-01 00:00:00'); } elseif ($return['ending'] == 'duration') { $end = $start + $return['duration'] * 60; } if ($start < $now) { $return['err'] = 1; $return['errmsg'] = i('The submitted start time is in the past.'); return $return; } if ($start + 900 > $end) { $return['err'] = 1; $return['errmsg'] = i('The end time must be at least 15 minutes later than the start time.'); return $return; } $return['ipaddr'] = ''; $return['macaddr'] = ''; # server specific input if ($return['type'] == 'server') { # name $return['name'] = processInputVar('name', ARG_STRING); if (!preg_match('/^([-a-zA-Z0-9_\\. ]){0,255}$/', $return['name'])) { $return['err'] = 1; $return['errmsg'] = i('The reservation name can only contain letters, numbers, spaces, dashes(-), underscores(_), and periods(.) and can be up to 255 characters long'); return $return; } # ipaddr $return['ipaddr'] = processInputVar('ipaddr', ARG_STRING); if ($return['ipaddr'] != '') { # validate fixed IP address if (!validateIPv4addr($return['ipaddr'])) { $return['err'] = 1; $return['errmsg'] = i('Invalid IP address. Must be w.x.y.z with each of w, x, y, and z being between 1 and 255 (inclusive)'); return $return; } # validate netmask $return['netmask'] = processInputVar('netmask', ARG_STRING); $bnetmask = ip2long($return['netmask']); if (!preg_match('/^[1]+0[^1]+$/', sprintf('%032b', $bnetmask))) { $return['err'] = 1; $return['errmsg'] = i('Invalid netmask specified'); return $return; } # validate router $return['router'] = processInputVar('router', ARG_STRING); if (!validateIPv4addr($return['router'])) { $return['err'] = 1; $return['errmsg'] = i('Invalid router address. Must be w.x.y.z with each of w, x, y, and z being between 1 and 255 (inclusive)'); return $return; } $return['network'] = ip2long($return['ipaddr']) & $bnetmask; if ($return['network'] != (ip2long($return['router']) & $bnetmask)) { $return['err'] = 1; $return['errmsg'] = i('IP address and router are not on the same subnet based on the specified netmask.'); return $return; } # validate dns server(s) $dns = processInputVar('dns', ARG_STRING); $tmp = explode(',', $dns); $cnt = 0; $return['dnsArr'] = array(); foreach ($tmp as $dnsaddr) { if ($cnt && $dnsaddr == '') { continue; } if ($cnt == 3) { $return['err'] = 1; $return['errmsg'] = i('Too many DNS servers specified - up to 3 are allowed.'); return $return; } if (!validateIPv4addr($dnsaddr)) { $return['err'] = 1; $return['errmsg'] = i('Invalid DNS server specified.'); return $return; } $return['dnsArr'][] = $dnsaddr; $cnt++; } # check that a management node can handle the network $mappedmns = getMnsFromImage($return['imageid']); $mnnets = checkAvailableNetworks($return['ipaddr']); $intersect = array_intersect($mappedmns, $mnnets); if (empty($intersect)) { $return['err'] = 1; $return['errmsg'] = i('There are no management nodes that can deploy the selected image with the specified IP address.'); return $return; } } # macaddr $return['macaddr'] = processInputVar('macaddr', ARG_STRING); if ($return['macaddr'] != '' && !preg_match('/^(([A-Fa-f0-9]){2}:){5}([A-Fa-f0-9]){2}$/', $return['macaddr'])) { $return['err'] = 1; $return['errmsg'] = i('Invalid MAC address. Must be XX:XX:XX:XX:XX:XX with each pair of XX being from 00 to FF (inclusive)'); return $return; } # profileid $return['profileid'] = processInputVar('profileid', ARG_NUMERIC, 0); $resources = getUserResources(array("serverCheckOut", "serverProfileAdmin"), array("available", "administer")); if (!array_key_exists($return['profileid'], $resources['serverprofile'])) { $return['profileid'] = 0; } elseif ($return['profileid'] != 0) { $tmp = getServerProfiles($return['profileid']); $tmp = $tmp[$return['profileid']]; if ($tmp['imageid'] != $return['imageid'] && ($tmp['fixedIP'] != $return['ipaddr'] && $tmp['fixedMAC'] != $return['macaddr'] || $tmp['fixedIP'] == $return['ipaddr'] && $return['ipaddr'] == '' && $tmp['fixedMAC'] == $return['macaddr'] && $return['macaddr'] == '')) { $return['profileid'] = 0; } } # admingroupid $usergroups = getUserGroups(); $return['admingroupid'] = processInputVar('admingroupid', ARG_NUMERIC); if ($return['admingroupid'] != 0 && !array_key_exists($return['admingroupid'], $usergroups)) { $return['err'] = 1; $return['errmsg'] = i('You do not have access to use the specified admin user group.'); return $return; } # logingroupid $return['logingroupid'] = processInputVar('logingroupid', ARG_NUMERIC); if ($return['logingroupid'] != 0 && !array_key_exists($return['logingroupid'], $usergroups)) { $return['err'] = 1; $return['errmsg'] = i('You do not have access to use the specified access user group.'); return $return; } # monitored $return['monitored'] = processInputVar('monitored', ARG_NUMERIC, 0); if ($return['monitored'] != 0 && $return['monitored'] != 1) { $return['monitored'] = 0; } # configs # TODO configs /*$tmp = getUserResources(array("configAdmin")); $userconfigs = $tmp['config']; $initconfigs = getMappedConfigs($return['imageid']); if(array_key_exists('configdata', $_POST)) { if(get_magic_quotes_gpc()) $_POST['configdata'] = stripslashes($_POST['configdata']); $configdata = json_decode($_POST['configdata']); } if(array_key_exists('configdata', $_POST) && isset($configdata->configs)) $configs = $configdata->configs; else $configs = (object)array(); $return['configs'] = array(); foreach($initconfigs as $id => $config) { if(isset($configs->{$id}) && isset($configs->{$id}->applied) && $configs->{$config['id']}->applied != 'true' && $configs->{$config['id']}->applied != 'false') unset($configs->{$config['id']}); if($config['optional'] && (! isset($configs->{$id}) || ! $configs->{$id}->applied)) continue; $return['configs'][$id] = array('configid' => $config['configid'], 'configmapid' => $config['configmapid'], 'imageid' => $config['subimageid']); if(isset($configs->{$id})) unset($configs->{$id}); } $rescfgmapids = array(); foreach($configs as $id => $config) { if(! array_key_exists($config->configid, $userconfigs)) continue; $return['configs'][$id] = array('configid' => $config->configid, 'configstageid' => $config->configstageid, 'imageid' => $config->imageid); $tmp = explode('/', $id); $rescfgmapids[$tmp[1]] = 1; } # configvars $tmp = array_splice($initconfigs, 0); $initconfigvars = getImageConfigVariables($tmp); if(array_key_exists('configdata', $_POST) && isset($configdata->configvars)) $configvars = $configdata->configvars; else $configvars = (object)array(); #print "/*"; #printArray($initconfigvars); #printArray($configvars); #print "*" . "/"; $return['configvars'] = array(); foreach($initconfigvars as $id => $configvar) { $tmp = explode('/', $id); $cfgid = "{$tmp[0]}/{$tmp[1]}"; $varid = $tmp[2]; if($configvar['ask'] == 0 || ! isset($configvars->{$id}) || ! isset($configvars->{$id}->value)) { $return['configvars'][$cfgid][$varid] = array('value' => $configvar['defaultvalue']); } else { switch($configvar['datatype']) { case 'bool': case 'int': case 'float': $value = processInputData($configvars->{$id}->value, ARG_NUMERIC); break; default: $value = processInputData($configvars->{$id}->value, ARG_STRING); break; } $return['configvars'][$cfgid][$varid] = array('value' => $value); } if(isset($configvars->{$id})) unset($configvars->{$id}); }*/ /*print "/*"; printArray($rescfgmapids); foreach($configvars as $id => $var) { $cfgid = explode('/', $id); print "cfgid: {$cfgid[1]}\n"; if(! array_key_exists($cfgid[1], $rescfgmapids)) continue; // TODO validate based on var type $value = processInputData($configvars->{$id}->value, ARG_STRING); $return['configvars']["{$cfgid[0]}/{$cfgid[1]}"][$cfgid[2]] = array('value' => $value); } printArray($configvars);*/ #print "*/"; } return $return; }
function XMLRPCdeployServer($imageid, $start, $end, $admingroup = '', $logingroup = '', $ipaddr = '', $macaddr = '', $monitored = 0, $foruser = '', $name = '', $userdata = '') { global $user, $remoteIP; if (!in_array("serverCheckOut", $user["privileges"])) { return array('status' => 'error', 'errorcode' => 60, 'errormsg' => "access denied to deploy server"); } $imageid = processInputData($imageid, ARG_NUMERIC); $resources = getUserResources(array("imageAdmin", "imageCheckOut")); $images = removeNoCheckout($resources["image"]); $extraimages = getServerProfileImages($user['id']); if (!array_key_exists($imageid, $images) && !array_key_exists($imageid, $extraimages)) { return array('status' => 'error', 'errorcode' => 3, 'errormsg' => "access denied to {$imageid}"); } if ($admingroup != '') { $admingroup = processInputData($admingroup, ARG_STRING); if (get_magic_quotes_gpc()) { $admingroup = stripslashes($admingroup); } if (preg_match('/@/', $admingroup)) { $tmp = explode('@', $admingroup); $escadmingroup = mysql_real_escape_string($tmp[0]); $affilid = getAffiliationID($tmp[1]); if (is_null($affilid)) { return array('status' => 'error', 'errorcode' => 51, 'errormsg' => "unknown affiliation for admin user group: {$tmp[1]}"); } } else { $escadmingroup = mysql_real_escape_string($admingroup); $affilid = DEFAULT_AFFILID; } $admingroupid = getUserGroupID($escadmingroup, $affilid, 1); if (is_null($admingroupid)) { return array('status' => 'error', 'errorcode' => 52, 'errormsg' => "unknown admin user group: {$admingroup}"); } } else { $admingroupid = ''; } if ($logingroup != '') { $logingroup = processInputData($logingroup, ARG_STRING); if (get_magic_quotes_gpc()) { $logingroup = stripslashes($logingroup); } if (preg_match('/@/', $logingroup)) { $tmp = explode('@', $logingroup); $esclogingroup = mysql_real_escape_string($tmp[0]); $affilid = getAffiliationID($tmp[1]); if (is_null($affilid)) { return array('status' => 'error', 'errorcode' => 54, 'errormsg' => "unknown affiliation for login user group: {$tmp[1]}"); } } else { $esclogingroup = mysql_real_escape_string($logingroup); $affilid = DEFAULT_AFFILID; } $logingroupid = getUserGroupID($esclogingroup, $affilid, 1); if (is_null($logingroupid)) { return array('status' => 'error', 'errorcode' => 55, 'errormsg' => "unknown login user group: {$logingroup}"); } } else { $logingroupid = ''; } $ipaddr = processInputData($ipaddr, ARG_STRING); $ipaddrArr = explode('.', $ipaddr); if ($ipaddr != '' && (!preg_match('/^(([0-9]){1,3}\\.){3}([0-9]){1,3}$/', $ipaddr) || $ipaddrArr[0] < 1 || $ipaddrArr[0] > 255 || $ipaddrArr[1] < 0 || $ipaddrArr[1] > 255 || $ipaddrArr[2] < 0 || $ipaddrArr[2] > 255 || $ipaddrArr[3] < 0 || $ipaddrArr[3] > 255)) { return array('status' => 'error', 'errorcode' => 57, 'errormsg' => "Invalid IP address. Must be w.x.y.z with each of " . "w, x, y, and z being between 1 and 255 (inclusive)"); } $macaddr = processInputData($macaddr, ARG_STRING); if ($macaddr != '' && !preg_match('/^(([A-Fa-f0-9]){2}:){5}([A-Fa-f0-9]){2}$/', $macaddr)) { return array('status' => 'error', 'errorcode' => 58, 'errormsg' => "Invalid MAC address. Must be XX:XX:XX:XX:XX:XX " . "with each pair of XX being from 00 to FF (inclusive)"); } $monitored = processInputData($monitored, ARG_NUMERIC); if ($monitored != 0 && $monitored != 1) { $monitored = 0; } $start = processInputData($start, ARG_STRING, 1); $end = processInputData($end, ARG_STRING, 1); #$foruser = processInputData($foruser, ARG_STRING, 1); $name = processInputData($name, ARG_STRING); if (get_magic_quotes_gpc()) { $name = stripslashes($name); } if (!preg_match('/^([-a-zA-Z0-9_\\. ]){0,255}$/', $name)) { return array('status' => 'error', 'errorcode' => 58, 'errormsg' => "Invalid name. Can only contain letters, numbers, " . "spaces, dashes(-), underscores(_), and periods(.) " . "and be up to 255 characters long"); } $name = mysql_real_escape_string($name); # validate $start if ($start != 'now' && !is_numeric($start)) { return array('status' => 'error', 'errorcode' => 4, 'errormsg' => "received invalid input for start"); } # validate $end if ($end != 'indefinite' && !is_numeric($end)) { return array('status' => 'error', 'errorcode' => 59, 'errormsg' => "received invalid input for end"); } $nowfuture = 'future'; if ($start == 'now') { $start = unixFloor15(time()); $nowfuture = 'now'; } else { if ($start < time() - 30) { return array('status' => 'error', 'errorcode' => 5, 'errormsg' => "start time is in the past"); } } if ($end == 'indefinite') { $end = datetimeToUnix("2038-01-01 00:00:00"); } elseif ($end % (15 * 60)) { $end = unixFloor15($end) + 15 * 60; } elseif ($end < $start + 900) { return array('status' => 'error', 'errorcode' => 88, 'errormsg' => "end time must be at least 15 minutes after start time"); } $max = getMaxOverlap($user['id']); if (checkOverlap($start, $end, $max)) { return array('status' => 'error', 'errorcode' => 7, 'errormsg' => "reservation overlaps with another one you " . "have, and you are allowed {$max} " . "overlapping reservations at a time"); } $images = getImages(); $revisionid = getProductionRevisionid($imageid); $rc = isAvailable($images, $imageid, $revisionid, $start, $end, 1, 0, 0, 0, 0, $ipaddr, $macaddr); if ($rc < 1) { addLogEntry($nowfuture, unixToDatetime($start), unixToDatetime($end), 0, $imageid); return array('status' => 'notavailable'); } $return['requestid'] = addRequest(); $query = "UPDATE reservation " . "SET remoteIP = '{$remoteIP}' " . "WHERE requestid = {$return['requestid']}"; doQuery($query); if ($userdata != '') { if (get_magic_quotes_gpc()) { $userdata = stripslashes($userdata); } $esc_userdata = mysql_real_escape_string($userdata); $query = "INSERT INTO variable " . "(name, " . "serialization, " . "value, " . "setby, " . "timestamp) " . "SELECT CONCAT('userdata|', id), " . "'none', " . "'{$esc_userdata}', " . "'webcode', " . "NOW() " . "FROM reservation " . "WHERE requestid = {$return['requestid']}"; doQuery($query); } $fields = array('requestid'); $values = array($return['requestid']); if ($name != '') { $fields[] = 'name'; $values[] = "'{$name}'"; } if ($ipaddr != '') { $fields[] = 'fixedIP'; $values[] = "'{$ipaddr}'"; } if ($macaddr != '') { $fields[] = 'fixedMAC'; $values[] = "'{$macaddr}'"; } if ($admingroupid != 0) { $fields[] = 'admingroupid'; $values[] = $admingroupid; } if ($logingroupid != 0) { $fields[] = 'logingroupid'; $values[] = $logingroupid; } if ($monitored != 0) { $fields[] = 'monitored'; $values[] = 1; } $allfields = implode(',', $fields); $allvalues = implode(',', $values); $query = "INSERT INTO serverrequest ({$allfields}) VALUES ({$allvalues})"; doQuery($query, 101); $return['status'] = 'success'; return $return; }