2010-06-16 18:21:58 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Contains all common PHP functions needed by most pages
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Retain session variables across page requests
|
|
|
|
session_start();
|
2011-11-15 00:47:09 +00:00
|
|
|
session_write_close(); // Do not block HTTP requests
|
2010-06-16 18:21:58 +00:00
|
|
|
|
|
|
|
// The settings below display error on the screen,
|
|
|
|
// instead of giving blank pages.
|
|
|
|
error_reporting(E_ALL);
|
|
|
|
ini_set('display_errors', true);
|
|
|
|
|
|
|
|
/**
|
2011-04-12 19:46:01 +00:00
|
|
|
* Run a command using the xCAT client/server protocol
|
2010-06-16 18:21:58 +00:00
|
|
|
*
|
2011-04-12 19:46:01 +00:00
|
|
|
* @param $cmd The xCAT command
|
2011-04-15 20:46:23 +00:00
|
|
|
* @param $nr Node range or group
|
|
|
|
* @param $args_array Command arguments
|
|
|
|
* @param $opts_array Command options
|
2010-06-16 18:21:58 +00:00
|
|
|
* @return A tree of SimpleXML objects.
|
|
|
|
* See perl-xCAT/xCAT/Client.pm for the format
|
|
|
|
*/
|
2011-04-12 19:46:01 +00:00
|
|
|
function docmd($cmd, $nr, $args_array, $opts_array){
|
2010-06-16 18:21:58 +00:00
|
|
|
// If we are not logged in,
|
|
|
|
// do not try to communicate with xcatd
|
|
|
|
if (!is_logged()) {
|
2011-04-12 19:46:01 +00:00
|
|
|
echo "<p>You are not logged in! Failed to run command.</p>";
|
2010-06-16 18:21:58 +00:00
|
|
|
return simplexml_load_string('<xcat></xcat>', 'SimpleXMLElement', LIBXML_NOCDATA);
|
|
|
|
}
|
|
|
|
|
2010-08-05 15:39:19 +00:00
|
|
|
// Create xCAT request
|
|
|
|
// Add command, node range, and arguments to request
|
2010-06-16 18:21:58 +00:00
|
|
|
$request = simplexml_load_string('<xcatrequest></xcatrequest>');
|
|
|
|
$request->addChild('command', $cmd);
|
|
|
|
if(!empty($nr)) { $request->addChild('noderange', $nr); }
|
2011-04-12 19:46:01 +00:00
|
|
|
if (!empty($args_array)) {
|
|
|
|
foreach ($args_array as $a) {
|
2010-06-16 18:21:58 +00:00
|
|
|
$request->addChild('arg',$a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-05 15:39:19 +00:00
|
|
|
// Add user and password to request
|
2010-06-16 18:21:58 +00:00
|
|
|
$usernode=$request->addChild('becomeuser');
|
|
|
|
$usernode->addChild('username',$_SESSION["username"]);
|
|
|
|
$usernode->addChild('password',getpassword());
|
|
|
|
|
2011-04-12 19:46:01 +00:00
|
|
|
$xml = submit_request($request, 0, $opts_array);
|
2010-06-16 18:21:58 +00:00
|
|
|
return $xml;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-04-12 19:46:01 +00:00
|
|
|
* Used by docmd() to submit request to xCAT
|
2010-06-16 18:21:58 +00:00
|
|
|
*
|
2011-04-12 19:46:01 +00:00
|
|
|
* @param $req Tree of SimpleXML objects
|
|
|
|
* @param $opts_array Request options
|
2010-06-16 18:21:58 +00:00
|
|
|
* @return A tree of SimpleXML objects
|
|
|
|
*/
|
2011-04-12 19:46:01 +00:00
|
|
|
function submit_request($req, $skipVerify, $opts_array){
|
2010-06-16 18:21:58 +00:00
|
|
|
$xcathost = "localhost";
|
|
|
|
$port = "3001";
|
|
|
|
$rsp = FALSE;
|
|
|
|
$response = '';
|
|
|
|
$cleanexit = 0;
|
2010-09-09 21:12:47 +00:00
|
|
|
|
2011-04-12 19:46:01 +00:00
|
|
|
// Determine whether to flush output or not
|
|
|
|
$flush = false;
|
2011-04-13 11:48:34 +00:00
|
|
|
if ($opts_array && in_array("flush", $opts_array)) {
|
2011-04-12 19:46:01 +00:00
|
|
|
$flush = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine how to handle the flush output
|
|
|
|
// You can specify a function name, in place of TBD, to handle the flush output
|
|
|
|
$flush_format = "";
|
2011-04-13 11:48:34 +00:00
|
|
|
if ($opts_array && in_array("flush-format=TBD", $opts_array)) {
|
2011-04-12 19:46:01 +00:00
|
|
|
$flush_format = "TBD";
|
|
|
|
}
|
|
|
|
|
2010-09-09 21:12:47 +00:00
|
|
|
// Open syslog, include the process ID and also send
|
|
|
|
// the log to standard error, and use a user defined
|
|
|
|
// logging mechanism
|
|
|
|
openlog("xCAT-UI", LOG_PID | LOG_PERROR, LOG_LOCAL0);
|
2010-06-16 18:21:58 +00:00
|
|
|
|
|
|
|
// Open a socket to xcatd
|
2010-09-09 21:12:47 +00:00
|
|
|
syslog(LOG_INFO, "Opening socket to xcatd...");
|
2010-06-16 18:21:58 +00:00
|
|
|
if($fp = stream_socket_client('ssl://'.$xcathost.':'.$port, $errno, $errstr, 30, STREAM_CLIENT_CONNECT)){
|
2010-09-09 21:12:47 +00:00
|
|
|
$reqXML = $req->asXML();
|
|
|
|
$nr = $req->noderange;
|
|
|
|
$cmd = $req->command;
|
|
|
|
|
|
|
|
syslog(LOG_INFO, "Sending request: $cmd $nr");
|
|
|
|
stream_set_blocking($fp, 0); // Set as non-blocking
|
2010-06-16 18:21:58 +00:00
|
|
|
fwrite($fp,$req->asXML()); // Send XML to xcatd
|
2010-09-09 21:12:47 +00:00
|
|
|
set_time_limit(900); // Set 15 minutes timeout (for long running requests)
|
|
|
|
// The default is 30 seconds which is too short for some requests
|
|
|
|
|
2011-08-02 14:05:56 +00:00
|
|
|
// Turn on output buffering
|
|
|
|
ob_start();
|
2010-09-09 21:12:47 +00:00
|
|
|
while(!feof($fp)) { // Read until there is no more
|
2010-06-16 18:21:58 +00:00
|
|
|
// Remove newlines and add it to the response
|
2010-09-09 21:12:47 +00:00
|
|
|
$str = fread($fp, 8192);
|
|
|
|
if ($str) {
|
|
|
|
$response .= preg_replace('/>\n\s*</', '><', $str);
|
2011-04-12 19:46:01 +00:00
|
|
|
|
|
|
|
// Flush output to browser
|
|
|
|
if ($flush) {
|
|
|
|
// Strip HTML tags from output
|
|
|
|
if ($tmp = trim(strip_tags($str))) {
|
2011-04-13 15:34:36 +00:00
|
|
|
// Format the output based on what was given for $flush_format
|
2011-04-14 15:02:06 +00:00
|
|
|
if ($flush_format == "TDB") {
|
|
|
|
format_TBD($tmp);
|
|
|
|
} else {
|
|
|
|
// Print out output by default
|
2011-10-12 18:27:12 +00:00
|
|
|
echo '<pre style="font-size: 10px;">' . $tmp . '</pre>';
|
2011-06-28 08:37:16 +00:00
|
|
|
ob_flush();
|
2011-04-14 15:02:06 +00:00
|
|
|
flush();
|
|
|
|
}
|
2011-04-12 19:46:01 +00:00
|
|
|
}
|
2011-08-02 14:05:56 +00:00
|
|
|
}
|
2010-09-09 21:12:47 +00:00
|
|
|
}
|
|
|
|
|
2010-06-16 18:21:58 +00:00
|
|
|
// Look for serverdone response
|
|
|
|
$fullpattern = '/<xcatresponse>\s*<serverdone>\s*<\/serverdone>\s*<\/xcatresponse>/';
|
|
|
|
$mixedpattern = '/<serverdone>\s*<\/serverdone>.*<\/xcatresponse>/';
|
2011-01-07 08:58:46 +00:00
|
|
|
$recentpattern = '/<\/xcatresponse>/';
|
|
|
|
if(preg_match($recentpattern,$str) && preg_match($mixedpattern,$response)) {
|
2010-06-16 18:21:58 +00:00
|
|
|
// Transaction is done,
|
|
|
|
// Package up XML and return it
|
|
|
|
// Remove the serverdone response and put an xcat tag around the rest
|
|
|
|
$count = 0;
|
|
|
|
$response = preg_replace($fullpattern,'', $response, -1, $count); // 1st try to remove the long pattern
|
|
|
|
if (!$count) { $response = preg_replace($mixedpattern,'', $response) . '</xcatresponse>/'; } // if its not there, then remove the short pattern
|
|
|
|
$response = "<xcat>$response</xcat>";
|
2010-08-03 09:06:09 +00:00
|
|
|
//delete the \n between '>' and '<'
|
|
|
|
$response = preg_replace('/>\n\s*</', '><', $response);
|
|
|
|
//replace the '\n' by ':|:' in the data area.
|
|
|
|
$response = preg_replace('/\n/', ':|:', $response);
|
2010-06-16 18:21:58 +00:00
|
|
|
$rsp = simplexml_load_string($response,'SimpleXMLElement', LIBXML_NOCDATA);
|
|
|
|
$cleanexit = 1;
|
|
|
|
break;
|
|
|
|
}
|
2010-09-09 21:12:47 +00:00
|
|
|
} // End of while(!feof($fp))
|
|
|
|
|
2010-11-12 20:14:31 +00:00
|
|
|
syslog(LOG_INFO, "($cmd $nr) Sending response");
|
2010-06-16 18:21:58 +00:00
|
|
|
fclose($fp);
|
2010-09-09 21:12:47 +00:00
|
|
|
} else {
|
2010-06-16 18:21:58 +00:00
|
|
|
echo "<p>xCAT submit request socket error: $errno - $errstr</p>";
|
|
|
|
}
|
2011-08-02 14:05:56 +00:00
|
|
|
|
|
|
|
// Flush (send) the output buffer and turn off output buffering
|
|
|
|
ob_end_flush();
|
2010-06-16 18:21:58 +00:00
|
|
|
|
2010-09-09 21:12:47 +00:00
|
|
|
// Close syslog
|
|
|
|
closelog();
|
|
|
|
|
|
|
|
if(! $cleanexit) {
|
2010-06-16 18:21:58 +00:00
|
|
|
if (preg_match('/^\s*<xcatresponse>.*<\/xcatresponse>\s*$/',$response)) {
|
|
|
|
// Probably an error message
|
|
|
|
$response = "<xcat>$response</xcat>";
|
|
|
|
$rsp = simplexml_load_string($response,'SimpleXMLElement', LIBXML_NOCDATA);
|
|
|
|
}
|
2010-09-09 21:12:47 +00:00
|
|
|
elseif(!$skipVerify) {
|
2010-06-16 18:21:58 +00:00
|
|
|
echo "<p>(Error) xCAT response ended prematurely: ", htmlentities($response), "</p>";
|
|
|
|
$rsp = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $rsp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable password storage to split between cookie and session variable
|
|
|
|
*
|
|
|
|
* @param $data
|
2011-04-15 20:46:23 +00:00
|
|
|
* @param $key
|
2010-06-16 18:21:58 +00:00
|
|
|
* @return
|
|
|
|
*/
|
2010-07-19 18:28:31 +00:00
|
|
|
function xorcrypt($data, $key) {
|
2010-06-16 18:21:58 +00:00
|
|
|
$datalen = strlen($data);
|
|
|
|
$keylen = strlen($key);
|
|
|
|
for ($i=0;$i<$datalen;$i++) {
|
|
|
|
$data[$i] = chr(ord($data[$i])^ord($key[$i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get password
|
|
|
|
*
|
|
|
|
* @param Nothing
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
function getpassword() {
|
|
|
|
if (isset($GLOBALS['xcatauthsecret'])) {
|
|
|
|
$cryptext = $GLOBALS['xcatauthsecret'];
|
|
|
|
} else if (isset($_COOKIE["xcatauthsecret"])) {
|
|
|
|
$cryptext = $_COOKIE["xcatauthsecret"];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return xorcrypt($_SESSION["secretkey"], base64_decode($cryptext));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the password splitting knowledge between server
|
|
|
|
* and client side persistant storage. Caller should regenerate
|
2010-07-19 18:28:31 +00:00
|
|
|
* session ID when contemplating a new user/password, to preclude
|
2010-06-16 18:21:58 +00:00
|
|
|
* session fixation, though fixation is limited without the secret.
|
|
|
|
*
|
|
|
|
* @param $password Password
|
|
|
|
* @return Nothing
|
|
|
|
*/
|
|
|
|
function setpassword($password) {
|
|
|
|
$randlen = strlen($password);
|
|
|
|
$key = getrandchars($randlen);
|
|
|
|
$cryptext = xorcrypt($password,$key);
|
|
|
|
|
|
|
|
// Non-ascii characters, encode it in base64
|
|
|
|
$cryptext = base64_encode($cryptext);
|
|
|
|
setcookie("xcatauthsecret",$cryptext,0,'/');
|
|
|
|
$GLOBALS["xcatauthsecret"] = $cryptext;
|
|
|
|
$_SESSION["secretkey"] = $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-19 18:28:31 +00:00
|
|
|
* Get RAND characters
|
2010-06-16 18:21:58 +00:00
|
|
|
*
|
|
|
|
* @param $length Length of characters
|
|
|
|
* @return RAND characters
|
|
|
|
*/
|
|
|
|
function getrandchars($length) {
|
|
|
|
$charset = '0123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*';
|
|
|
|
$charsize = strlen($charset);
|
|
|
|
srand();
|
|
|
|
$chars = '';
|
|
|
|
for ($i=0;$i<$length;$i++) {
|
|
|
|
$num=rand()%$charsize;
|
|
|
|
$chars=$chars.substr($charset,$num,1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $chars;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if a user/password session exists
|
|
|
|
*
|
|
|
|
* @param Nothing
|
|
|
|
* @return True If user has a session.
|
|
|
|
* False Otherwise
|
|
|
|
*/
|
|
|
|
function is_logged() {
|
|
|
|
if (isset($_SESSION["username"]) and !is_bool(getpassword())) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if a user is currently logged in successfully
|
|
|
|
*
|
|
|
|
* @param Nothing
|
|
|
|
* @return True If the user is currently logged in successfully
|
|
|
|
* False Otherwise
|
|
|
|
*/
|
|
|
|
function isAuthenticated() {
|
|
|
|
if (is_logged()) {
|
|
|
|
if ($_SESSION["xcatpassvalid"] != 1) {
|
2011-04-13 11:48:34 +00:00
|
|
|
$testcred = docmd("authcheck", "", NULL, NULL);
|
2010-06-16 18:21:58 +00:00
|
|
|
if (isset($testcred->{'xcatresponse'}->{'data'})) {
|
|
|
|
$result = "".$testcred->{'xcatresponse'}->{'data'};
|
|
|
|
if (is_numeric(strpos("Authenticated",$result))) {
|
|
|
|
// Logged in successfully
|
|
|
|
$_SESSION["xcatpassvalid"] = 1;
|
|
|
|
} else {
|
|
|
|
// Not logged in
|
|
|
|
$_SESSION["xcatpassvalid"] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($_SESSION["xcatpassvalid"]) and $_SESSION["xcatpassvalid"]==1) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 18:22:33 +00:00
|
|
|
/**
|
|
|
|
* Determine if a user has root access
|
|
|
|
*
|
|
|
|
* @param Nothing
|
|
|
|
* @return True If the user has root access
|
|
|
|
* False Otherwise
|
|
|
|
*/
|
|
|
|
function isRootAcess() {
|
|
|
|
if (is_logged() && $_SESSION["xcatpassvalid"]) {
|
|
|
|
$testacc = docmd('tabdump', '', array('policy', '-w', "name==" . $_SESSION["username"]), array());
|
|
|
|
if (isset($testacc->{'xcatresponse'}->{'data'}->{1})) {
|
|
|
|
$result = $testacc->{'xcatresponse'}->{'data'}->{1};
|
|
|
|
$result = str_replace('"', '', $result);
|
|
|
|
$args = array();
|
|
|
|
$args = explode(",", $result);
|
|
|
|
|
|
|
|
// Get the comments which contains the privilege
|
|
|
|
$comments = $args[8];
|
|
|
|
$args = explode(";", $comments);
|
|
|
|
// Default privilege is guest
|
|
|
|
$privilege = 'guest';
|
|
|
|
$_SESSION["xcatpassvalid"] = 0;
|
|
|
|
foreach ($args as $arg) {
|
|
|
|
// Get user privilege
|
|
|
|
if ($arg && is_numeric(strpos($arg, "privilege"))) {
|
|
|
|
if (is_numeric(strpos($arg, "root"))) {
|
|
|
|
// Set privilege to root
|
|
|
|
$privilege = 'root';
|
|
|
|
$_SESSION["xcatpassvalid"] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp($_SESSION["username"], 'root') == 0) {
|
|
|
|
$_SESSION["xcatpassvalid"] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($_SESSION["xcatpassvalid"]) and $_SESSION["xcatpassvalid"]==1) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-16 18:21:58 +00:00
|
|
|
/**
|
|
|
|
* Log out of the current user session
|
|
|
|
*
|
|
|
|
* @param Nothing
|
|
|
|
* @return Nothing
|
|
|
|
*/
|
|
|
|
function logout() {
|
|
|
|
// Clear the secret cookie from browser
|
|
|
|
if (isset($_COOKIE["xcatauthsecret"])) {
|
|
|
|
setcookie("xcatauthsecret",'',time()-86400*7,'/');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expire session cookie
|
|
|
|
if (isset($_COOKIE[session_name()])) {
|
|
|
|
setcookie(session_name(),"",time()-86400*7,"/");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear server store of data
|
|
|
|
$_SESSION=array();
|
|
|
|
}
|
2011-04-14 15:02:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Format a given string and echo it back to the browser
|
|
|
|
*
|
|
|
|
* @param $str String
|
|
|
|
* @return Nothing
|
|
|
|
*/
|
|
|
|
function format_TBD($str) {
|
|
|
|
// Format a given string however you want it
|
|
|
|
echo $tmp . '<br/>';
|
|
|
|
flush();
|
|
|
|
}
|
2010-06-16 18:21:58 +00:00
|
|
|
?>
|