Commit 6a471131 authored by Marius Burkard's avatar Marius Burkard
Browse files

- first version of autoinstaller for debian 9

parents
<?php
define('APP_DIR', realpath(dirname(__FILE__)));
define('LIB_DIR', APP_DIR . '/lib');
define('LOG_DIR', APP_DIR . '/var/log');
define('TMP_DIR', APP_DIR . '/var/tmp');
define('CACHE_DIR', APP_DIR . '/var/cache');
require_once LIB_DIR . '/class.ISPConfig.inc.php';
try {
ISPConfigLog::setLogPriority(ISPConfigLog::PRIO_DEBUG);
ISPConfig::run();
} catch(Exception $e) {
ISPConfigLog::error('Exception occured: ' . get_class($e) . ' -> ' . $e->getMessage(), true);
//var_dump($e);
exit;
}
\ No newline at end of file
<?php
if(function_exists('spl_autoload_register')) {
spl_autoload_register('ISPConfig::autoload');
}
/**
* Main controller class
*
* @author croydon
*/
class ISPConfig {
private static $is_cli_run = false;
private static $cli_script = false;
private static $autoload_files = array(
'PXBashColor' => LIB_DIR . '/libbashcolor.inc.php'
);
private static function init() {
if(php_sapi_name() == 'cli') {
self::$is_cli_run = true;
$argc = 0;
$argv = array();
if(isset($_SERVER['argc'])) {
$argc = $_SERVER['argc'];
}
if(isset($_SERVER['argv'])) {
$argv = $_SERVER['argv'];
}
if(isset($argv[0])) {
self::$cli_script = basename($argv[0]);
}
for($a = 1; $a < $argc; $a++) {
if(substr($argv[$a], 0, 2) == '--') {
$sArg = substr($argv[$a], 2);
if(strpos($sArg, '=') !== false) {
list($sKey, $sValue) = explode('=', $sArg);
} else {
$sKey = $sArg;
$sValue = true;
}
if($sKey != 'module' && $sKey != 'method') {
$_GET[$sKey] = $sValue;
}
} elseif(!array_key_exists('module', $_GET)) {
$_GET['module'] = $argv[$a];
} elseif(!array_key_exists('method', $_GET)) {
$_GET['method'] = $argv[$a];
}
}
}
}
/**
* @param string $class_name
* @throws ISPConfigClassException
*/
public static function autoload($class_name) {
if(preg_match('/^\w+$/', $class_name) === false) {
throw new ISPConfigClassException($class_name . ' is not a valid class name.');
}
$class_dir = LIB_DIR;
if(preg_match('/Exception$/', $class_name)) {
$class_dir .= '/exceptions';
} elseif(preg_match('/Module$/', $class_name)) {
$class_dir .= '/modules';
} elseif(preg_match('/API$/', $class_name)) {
$class_dir .= '/api';
} elseif(preg_match('/OS$/', $class_name)) {
$class_dir .= '/os';
}
$use_file = null;
if(isset(self::$autoload_files[$class_name])) {
$use_file = self::$autoload_files[$class_name];
} elseif(file_exists($class_dir . '/class.' . $class_name . '.inc.php')) {
$use_file = $class_dir . '/class.' . $class_name . '.inc.php';
} elseif(file_exists($class_dir . '/class.' . strtolower($class_name) . '.inc.php')) {
$use_file = $class_dir . '/class.' . strtolower($class_name) . '.inc.php';
} elseif(preg_match('/^ISPConfig\w+Exception$/', $class_name)) {
$use_file = LIB_DIR . '/exceptions/class.ISPConfigException.inc.php';
} else {
throw new ISPConfigClassException('No class file for ' . $class_name . ' found.');
}
if($class_name != 'ISPConfigLog') {
ISPConfigLog::debug('Trying to autoload class file "' . $use_file . '" for class "' . $class_name . '"');
}
if(!file_exists($use_file)) {
throw new ISPConfigClassException('File ' . $use_file . ' not found for class ' . $class_name . '.');
}
include_once $use_file;
if(!class_exists($class_name)) {
throw new ISPConfigClassException($class_name . ' not found in file ' . LIB_DIR . '/class.' . $class_name . '.inc.php.');
}
}
/**
* @return boolean
*/
public static function isCLI() {
return self::$is_cli_run;
}
/**
* @return string
*/
public static function getScriptName() {
return self::$cli_script;
}
/**
* @throws ISPConfigModuleException
*/
public static function run() {
self::init();
// get operating system
try {
$os = ISPConfigBaseOS::getOSVersion();
ISPConfigLog::info('Starting perfect server setup for ' . $os['NAME'], true);
$installer = ISPConfigBaseOS::getOSInstance();
$installer->runPerfectSetup(true); // dry run for testing
} catch(Exception $ex) {
throw $ex;
}
exit;
}
}
<?php
/**
* Main controller class
*
* @author croydon
*/
class ISPConfigConnector {
private static $db_objects = array();
private static $ispc_config = null;
public static function parseConfig($code) {
$ispc_config = array();
$matches = array();
preg_match_all('/(?:^|;\s*)\$conf\[(["\'])(.*?)\\1\]\s*=\s*("(?:\\\\.|[^\\\\"])*"|\'(?:\\\\.|[^\\\\\'])*\'|\d+(?:\.\d+)?|[a-zA-Z_]+|\$\w+|\w+\s*\()/is', $code, $matches, PREG_SET_ORDER);
for($i = 0; $i < count($matches); $i++) {
$key = $matches[$i][2];
$val = $matches[$i][3];
if(substr($val, 0, 1) === '"' || substr($val, 0, 1) === "'") {
$val = substr($val, 1, -1);
}
$ispc_config[$key] = $val;
}
unset($code);
return $ispc_config;
}
/**
* This function is far from being a php parser, it is optimized for parsing the config.inc.php of ISPConfig
*
* @return array|boolean
*/
public static function getLocalConfig() {
if(!file_exists('/usr/local/ispconfig/server/lib/config.inc.php')) {
return false;
}
if(is_array(self::$ispc_config) && !empty(self::$ispc_config)) return self::$ispc_config;
$code = php_strip_whitespace('/usr/local/ispconfig/server/lib/config.inc.php');
$ispc_config = self::parseConfig($code);
if(is_array($ispc_config) && !empty($ispc_config)) {
self::$ispc_config = $ispc_config;
}
return $ispc_config;
}
/**
* @return ISPConfigDatabase
* @throws ISPConfigDatabaseException
*/
public static function getDatabaseInstance() {
$conf = self::getLocalConfig();
if(empty($conf)) {
throw new ISPConfigDatabaseException('Database config could not be read from local instance config.');
} elseif(!isset($conf['db_host']) || !$conf['db_host']) {
throw new ISPConfigDatabaseException('Database config is missing db_host setting.');
} elseif(!isset($conf['db_database']) || !$conf['db_database']) {
throw new ISPConfigDatabaseException('Database config is missing db_database setting.');
} elseif(!isset($conf['db_user']) || !$conf['db_user']) {
throw new ISPConfigDatabaseException('Database config is missing db_user setting.');
} elseif(!isset($conf['db_password']) || !$conf['db_password']) {
throw new ISPConfigDatabaseException('Database config is missing db_password setting.');
}
if(!isset($conf['db_port']) || !$conf['db_port']) {
$conf['db_port'] = 3306;
}
$ident = sha1(implode('::', array($conf['db_host'], $conf['db_database'], $conf['db_user'], $conf['db_password'], $conf['db_port'])));
if(!isset(self::$db_objects[$ident]) || !is_object(self::$db_objects[$ident])) {
self::$db_objects[$ident] = new ISPConfigDatabase($conf['db_database'], $conf['db_host'], $conf['db_user'], $conf['db_password'], 0, $conf['db_port']);
}
return self::$db_objects[$ident];
}
/**
* @param string $username
* @return int|boolean
*/
public static function getClientIdByUsername($username) {
$DB = self::getDatabaseInstance();
$qrystr = 'SELECT `client_id` FROM `client` WHERE `username` = ?';
$client = $DB->query_one($qrystr, $username);
if(!$client) {
return false;
} else {
return $client['client_id'];
}
}
public static function generateKeyPair() {
if(!@is_dir(TMP_DIR)) {
if(!@mkdir(TMP_DIR, 0777, true)) {
throw new ISPConfigLogException('Temp path ' . TMP_DIR . ' could not be created.');
}
}
if(!function_exists('openssl_pkey_get_private')) {
throw new ISPConfigException('OpenSSL extension missing. Cannot generate keys.');
}
$program = explode("\n", shell_exec('which ssh-keygen'));
$program = reset($program);
if(!$program || !is_executable($program)) {
throw new ISPConfigException('Could not generate key pair. Missing ssh-keygen.');
}
$trys = 0;
while(true) {
$trys++;
$file_name = TMP_DIR . '/' . sha1(uniqid('ssh-', true));
if(file_exists($file_name)) {
if($trys < 25) {
continue;
}
throw new ISPConfigException('Could not generate key pair unique file name.');
}
break;
}
$out = null;
$retval = 0;
exec('echo | ' . $program . ' -b 4096 -t rsa -f ' . escapeshellarg($file_name) . ' -q -N "" >/dev/null 2>&1', $out, $retval);
if($retval != 0) {
throw new ISPConfigException('Could not generate key pair. ssh-keygen returned non-zero code: ' . $retval);
} elseif(!file_exists($file_name) || !file_exists($file_name . '.pub')) {
throw new ISPConfigException('Could not generate key pair. Key files missing.');
}
$fprint = trim(shell_exec($program . ' -E md5 -lf ' . escapeshellarg($file_name . '.pub') . ' | awk \'{print $2}\''));
if(substr($fprint, 0, 4) === 'MD5:') {
$fprint = substr($fprint, 4);
}
$key = array(
'private' => trim(file_get_contents($file_name)),
'public' => trim(file_get_contents($file_name . '.pub')),
'fingerprint' => $fprint
);
if(!$key['private']) {
throw new ISPConfigException('Could not read private key.');
}
$res = openssl_pkey_get_private($key['private']);
if(!$res) {
throw new ISPConfigException('Could not verify private key.');
}
openssl_pkey_free($res);
if(!$key['public']) {
throw new ISPConfigException('Could not read public key.');
}
unlink($file_name);
unlink($file_name . '.pub');
return $key;
}
}
\ No newline at end of file
This diff is collapsed.
<?php
/**
* Main controller class
*
* @author croydon
*/
class ISPConfigFunctions {
/**
* @param string $value
* @return string
*/
public static function fromCamelCase($value) {
$result = '';
$prev_type = 'char';
for($s = 0; $s < strlen($value); $s++) {
$char = $value[$s];
if($s === 0) {
$result .= strtolower($char);
continue;
}
$ord = ord($char);
if(($ord >= 65 && $ord <= 90) || ($ord >= 48 && $ord <= 57 && $prev_type == 'char')) {
$result .= '_';
}
$result .= strtolower($char);
if($ord >= 48 && $ord <= 57) {
$prev_type = 'number';
} else {
$prev_type = 'char';
}
}
return $result;
}
/**
* @param string $value
* @return string
*/
public static function toCamelCase($value) {
$result = '';
$ucase = true;
for($s = 0; $s < strlen($value); $s++) {
$char = $value[$s];
if($char === '_') {
$ucase = true;
continue;
} elseif($ucase === true) {
$result .= strtoupper($char);
} else {
$result .= strtolower($char);
}
$ucase = false;
}
return $result;
}
/**
* check if a utf8 string is valid
*
* @access public
* @param string $str the string to check
* @return bool true if it is valid utf8, false otherwise
*/
public static function check_utf8($str) {
$len = strlen($str);
for($i = 0; $i < $len; $i++) {
$c = ord($str[$i]);
if($c > 128) {
if(($c > 247)) {
return false;
} elseif($c > 239) {
$bytes = 4;
} elseif($c > 223) {
$bytes = 3;
} elseif($c > 191) {
$bytes = 2;
} else {
return false;
}
if(($i + $bytes) > $len) {
return false;
}
while($bytes > 1) {
$i++;
$b = ord($str[$i]);
if($b < 128 || $b > 191) {
return false;
}
$bytes--;
}
}
}
return true;
}
/**
* Gzipped equivalent to file_get_contents
*
* @param string $file_name
* @return boolean|string
*/
public static function gz_file_get_contents($file_name) {
$fp = @gzopen($file_name, 'r');
if(!$fp) {
return false;
}
$data = '';
while(!gzeof($fp) && ($line = gzgets($fp)) !== false) {
$data .= $line;
}
gzclose($fp);
return $data;
}
/**
* Gzipped equivalent to file_put_contents
*
* @param string $file_name
* @param string $data
* @return boolean
*/
public static function gz_file_put_contents($file_name, $data) {
$fp = @gzopen($file_name, 'w');
if(!$fp) {
return false;
}
if(!gzwrite($fp, $data)) {
return false;
}
gzclose($fp);
return true;
}
/**
* create a password (random string)
*
* Creates a random string of the chars a-z, A-Z, 1-9 in a given length
*
* @access public
* @param int $length amount of chars to create
* @return string password/random string
*/
public static function generatePassword($length = 8, $use_special = false) {
// Verfügbare Zeichen für Passwort
$available = "abcdefghjkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789";
if($use_special == true) {
$available .= '%_/-_&+.<>';
}
if($length < 1) {
$length = 8;
}
$passwd = "";
$force_special = ($use_special == true ? mt_rand(1, $length) : 0);
for($i=1;$i<=$length;$i++) {
// Passwort mit zufälligen Zeichen füllen bis Länge erreicht
if($i == $force_special) {
$passwd .= substr($available, mt_rand(57, strlen($available) - 1), 1);
} else {
$passwd .= substr($available, mt_rand(0, strlen($available) - 1), 1);
}
}
// kreiertes Passwort zurückgeben
return $passwd;
}
public static function ini_read($file, $initial_sect = '', $has_variables = false) {
if(!is_file($file)) {
return false;
}
$fp = @fopen($file, 'r');
$ini = array();
$match = array();
$sect = $initial_sect;
$sect_data = array();
if(!$fp) {
return false;
}
while(!feof($fp)) {
$line = trim(fgets($fp));
if($line == '') {
continue;
} elseif(substr($line, 0, 1) === '#' || substr($line, 0, 1) === ';') {
// comments
continue;
} elseif(substr($line, 0, 2) === '//') {
// comments
continue;
} elseif(preg_match('/^\s*\[(\w+)\]\s*$/', $line, $match)) {
if($sect != '') {
$ini[$sect] = $sect_data;
}
$sect = $match[1];
$sect_data = array();
continue;
}
if(strpos($line, '=') === false) {
continue;
} // invalid setting
list($key, $value) = explode('=', $line);
$key = trim($key);
$value = trim($value);
if($has_variables == true) {
if(substr($key, 0, 1) === '$') {
$key = substr($key, 1);
}
if(preg_match('/\w+\s*\[\s*(["\'])([^\]]*?)\\1\s*\]/', $key, $match)) {
$key = $match[2];
}
if(substr($key, 0, 1) === "'" || substr($key, 0, 1) === '"') {
$key = substr($key, 1, -1);
}
if(!$key) {
continue;
} // error or unsupported type
$pos = strpos($value, '//');
if($pos !== false) {
$value = trim(substr($value, 0, $pos));
}
if(substr($value, -1) === ';') {
$value = substr($value, 0, -1);
}
if(substr($value, 0, 1) === "'" || substr($value, 0, 1) === '"') {
$value = substr($value, 1, -1);
} elseif(!preg_match('/^[0-9](?:\.[0-9]*)$/', $value)) {
// not string and not numeric -> unsupported
continue;
}
} elseif(preg_match('/^".*"$/', $value)) {
$value = preg_replace('/^"(.*)"$/', '$1', $value);
}
$sect_data[$key] = $value;
}
fclose($fp);
if($sect != '') {
$ini[$sect] = $sect_data;
}
return $ini;
}
}
\ No newline at end of file
<?php
/**
* HTTP methods
*
* @author croydon
*/
class ISPConfigHTTP {
private static $options = array(
'follow_redirects' => false,