diff --git a/public/setup.php b/public/setup.php index 586797d9..ea0d9339 100644 --- a/public/setup.php +++ b/public/setup.php @@ -15,437 +15,445 @@ * Used to help ensure a server is setup appropriately during installation/setup. */ -define('POSTFIXADMIN', 1); # by defining it here, common.php will not start a session. - require_once(dirname(__FILE__) . '/common.php'); # make sure correct common.php is used. -$CONF['show_header_text'] = 'NO'; -$CONF['theme_favicon'] = 'images/favicon.ico'; -$CONF['theme_logo'] = 'images/logo-default.png'; -$CONF['theme_css'] = 'css/default.css'; -require(dirname(__FILE__) . '/../templates/header.php'); +$configSetupPassword = Config::read_string('setup_password'); + +$errors = []; + +$configSetupDone = false; +$authenticated = false; + +if ($configSetupPassword != 'changeme' && $configSetupPassword != '') { + $configSetupDone = true; + + $pass = safepost('setup_password', 'invalid'); + + if ($pass != 'invalid') { + if (password_verify(safepost('setup_password', 'invalid'), $configSetupPassword)) { + $authenticated = true; + } else { + $errors['setup_login_password'] = "Password verification failed."; + } + } +} + ?> -
-

Postfix Admin Setup Checker

+ + + + + + -

Running software: -

"; - print "

Checking environment:\n"; - print "

"; + - if ($error != 0) { - print "

Please fix the errors listed above.

"; - } else { - print "

Everything seems fine... attempting to create/update database structure

\n"; - require_once(dirname(__FILE__) . '/upgrade.php'); +
- $tUsername = ''; - $setupMessage = ''; - $lostpw_error = 0; +

Login with setup_password

- $setuppw = ""; - if (isset($CONF['setup_password'])) { - $setuppw = $CONF['setup_password']; - } +

If you've forgotten your super-admin password, you can generate a new one using the Generate + form + and update your config.local.php

- if (safepost("form") === "setuppw") { - # "setup password" form submitted - if (safepost('setup_password') != safepost('setup_password2')) { - $setupMessage = "The two passwords differ!"; - $lostpw_error = 1; - } else { - list($lostpw_error, $lostpw_result) = check_setup_password(safepost('setup_password'), 1); - $setupMessage = $lostpw_result; - $setuppw = "changed"; - } - } elseif (safepost("form") == "createadmin") { - # "create admin" form submitted - list($pw_check_error, $pw_check_result) = check_setup_password(safepost('setup_password')); - if ($pw_check_result != 'pass_OK') { - $error += 1; - $setupMessage = $pw_check_result; - } +
- if ($error == 0 && $pw_check_result == 'pass_OK') { - // XXX need to ensure domains table includes an 'ALL' entry. - $table_domain = table_by_key('domain'); - $rows = db_query_all("SELECT * FROM $table_domain WHERE domain = 'ALL'"); - if (empty($rows)) { - db_insert('domain', array('domain' => 'ALL', 'description' => '', 'transport' => '')); // all other fields should default through the schema. - } +
+ +
+ - $values = array( - 'username' => safepost('username'), - 'password' => safepost('password'), - 'password2' => safepost('password2'), - 'superadmin' => 1, - 'domains' => array(), - 'active' => 1, - ); + - list($error, $setupMessage, $errormsg) = create_admin($values); - - if ($error != 0) { - $tUsername = htmlentities($values['username']); - } else { - $setupMessage .= "

You are done with your basic setup. "; - $setupMessage .= "

You can now login to PostfixAdmin using the account you just created."; - } - } - } - - - if (!isset($_SERVER['HTTPS'])) { - echo "

Warning: connection not secure, switch to https if possible

"; - } ?> - -
- - For a new installation, you need to generate a 'setup_password' to go into your config.local.php file.

-

You can use the form below, or run something like

php -r 'echo "somesalt:" . sha1("somesalt:" . "password");'
in a shell, after changing the salt.

-EOF; - $change = "Generate"; - } ?> - -

$CONF['setup_password']

- -
- - - - - - - - - - - - - - - - -
- +
- - -

Add a SuperAdmin Account

- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - -
-
+
+
+
+
- +
+ + + +
+ For a new installation, you must generate a 'setup_password' to go into your config.local.php file.

+

You can use the form below, or run something like the following in a shell - php -r 'echo password_hash("password", PASSWORD_DEFAULT);'

+EOF; + } + ?> + + + +

Generate setup_password hash

+ + If you want to use the password you entered as setup password, edit config.inc.php or config.local.php and set

'; + $result .= "
\$CONF['setup_password'] = '$hash';
"; + } else { + $form_error = 'has-error'; + $errors['setup_password'] = implode(', ', $msgs); } - } ?> -

Since version 2.3 there is no requirement to delete setup.php

-

Check the config.inc.php file for any other settings that you may need to change.

+ } + } + + ?> + +
+ + +
+ + +
+ + + + +
+ +
+ +
+ +
+ + + + +
+
+ +
+
+ +
+
+
+ + + +
+ + + +
+ +

Hosting Environment Check

+ +

Errors were found with your environment. These will be displayed once you've configured a setup_password and confirmed it.

"; + } + + if (!empty($check['info'])) { + echo "

Information

    "; + foreach ($check['info'] as $msg) { + echo "
  • {$tick} {$msg}
  • "; + } + echo "
"; + } + + if (!empty($check['warn'])) { + echo "

Warnings

    "; + foreach ($check['warn'] as $msg) { + echo "
  • ⚠ {$msg}
  • "; + } + echo "
"; + } + if (!empty($check['error'])) { + echo "

Errors (MUST be fixed)

    "; + foreach ($check['error'] as $msg) { + echo "
  • ⛔{$msg}
  • "; + } + echo "
"; + } + } else { + if (!empty($check['error'])) { + echo '

Hosting Environment errors found. Login to see details.

'; + } + if (!empty($check['warn'])) { + echo '

Hosting Environment warnings found. Login to see details.

'; + } + } + + ?> + +
+ +
+ +

Database Update

+ + Everything seems fine... attempting to create/update database structure

\n"; + require_once(dirname(__FILE__) . '/upgrade.php'); + } else { + echo "

Please login to see perform database update.

"; + } + ?> + +
+ + +
+

Add Superadmin Account

+ + +
+ + +
+ +
+ + +
+
+ + +
+ +
+ + + + +
+
+ + +
+ +
+ + +
+
+ +
+ +
+ + + + +
+
+ + +
+
+ +
+
+ +
+
+ + 'ALL', 'description' => '', 'transport' => '')); + } + + $values = array( + 'username' => safepost('username'), + 'password' => safepost('password'), + 'password2' => safepost('password2'), + 'superadmin' => 1, + 'domains' => array(), + 'active' => 1, + ); + + list($error, $setupMessage, $errormsg) = create_admin($values); + + if ($error == 1) { + $tUsername = htmlentities($values['username']); + } else { +// all good!. + $setupMessage .= "

You are done with your basic setup. "; + $setupMessage .= "

You can now login to PostfixAdmin using the account you just created."; + } + + } + ?> +

+ + + + + {$errors[$key]}"; + return "{$errors[$key]}"; } -function generate_setup_password_salt() { - $salt = time() . '*' . $_SERVER['REMOTE_ADDR'] . '*' . mt_rand(0, 60000); - $salt = md5($salt); - return $salt; -} - -function encrypt_setup_password($password, $salt) { - return $salt . ':' . sha1($salt . ':' . $password); -} - - -/* - returns: array( - 'error' => 0 (or 1), - 'message => text - ) -*/ -function check_setup_password($password, $lostpw_mode = 0) { - global $CONF; - $error = 1; # be pessimistic - - $setuppw = ""; - if (isset($CONF['setup_password'])) { - $setuppw = $CONF['setup_password']; - } - - list($confsalt, $confpass, $trash) = explode(':', $setuppw . '::'); - $pass = encrypt_setup_password($password, $confsalt); - - $validpass = validate_password($password); - - if ($password == "") { # no password specified? - $result = "Setup password must be specified
If you didn't set up a setup password yet, enter the password you want to use."; - } elseif (count($validpass) > 0) { - $result = $validpass[0]; # TODO: honor all error messages, not only the first one - } elseif ($pass == $setuppw && $lostpw_mode == 0) { # correct passsword (and not asking for a new password) - $result = "pass_OK"; - $error = 0; - } else { - $pass = encrypt_setup_password($password, generate_setup_password_salt()); - $result = ""; - if ($lostpw_mode == 1) { - $error = 0; # non-matching password is expected when the user asks for a new password - } else { - $result = '

Setup password not specified correctly

'; - } - $result .= '

If you want to use the password you entered as setup password, edit config.inc.php or config.local.php and set

'; - $result .= "
\$CONF['setup_password'] = '$pass';
"; - } - return array($error, $result); -} - -function create_admin($values) { - DEFINE('POSTFIXADMIN_SETUP', 1); # avoids instant redirect to login.php after creating the admin +function create_admin($values) +{ + define('POSTFIXADMIN_SETUP', 1); # avoids instant redirect to login.php after creating the admin $handler = new AdminHandler(1, 'setup.php'); $formconf = $handler->webformConfig(); @@ -469,6 +477,173 @@ function create_admin($values) { ); } +/** + * @return array['info' => string[], 'warn' => string[], 'error' => string[] ] + */ +function do_software_environment_check() +{ + $CONF = Config::getInstance()->getAll(); + + $warn = []; + $error = []; + $info = []; + + +// +// Check for availability functions +// + $f_phpversion = function_exists("phpversion"); + $f_apache_get_version = function_exists("apache_get_version"); + + $m_pdo = extension_loaded("PDO"); + $m_pdo_mysql = extension_loaded("pdo_mysql"); + $m_pdo_pgsql = extension_loaded('pdo_pgsql'); + $m_pdo_sqlite = extension_loaded("pdo_sqlite"); + + $f_session_start = function_exists("session_start"); + $f_preg_match = function_exists("preg_match"); + $f_mb_encode_mimeheader = function_exists("mb_encode_mimeheader"); + $f_imap_open = function_exists("imap_open"); + + $file_local_config = file_exists(realpath("./../config.local.php")); + +// Fall back to looking in /etc/postfixadmin for config.local.php (Debian etc) + if (!$file_local_config && is_dir('/etc/postfixadmin')) { + $file_local_config = file_exists('/etc/postfixadmin/config.local.php'); + } + +// +// Check for PHP version +// + $phpversion = 'unknown-version'; + + if ($f_phpversion == 1) { + if (version_compare(phpversion(), '5', '<')) { + $error[] = "Error: Depends on: PHP v5+"; + } elseif (version_compare(phpversion(), '7.0') < 0) { + $phpversion = 5; + $info[] = "Recommended PHP version: >= 7.0, you have " . phpversion() . "; you should upgrade."; + } else { + $info[] = "PHP version " . phpversion(); + } + } else { + $error[] = "Unable to check for PHP version. (missing function: phpversion())"; + } + +// +// Check for Apache version +// + if ($f_apache_get_version == 1) { + $info[] = apache_get_version(); + } + +// +// Check for config.local.php +// + if ($file_local_config == 1) { + $info[] = "Depends on: presence config.local.php - Found"; + } else { + $warn[] = "Warning: config.local.php - NOT FOUND - It's Recommended to store your own settings in config.local.php instead of editing config.inc.php"; + } + +// Check if there is support for at least 1 database + if (($m_pdo == 0) and ($m_pdo_mysql == 0) and ($m_pdo_sqlite == 0) and ($m_pdo_pgsql == 0)) { + $error[] = "There is no database (PDO) support in your PHP setup, you MUST install a suitable PHP PDO extension (e.g. pdo_pgsql, pdo_mysql or pdo_sqlite)."; + } + + if ($m_pdo_mysql == 1) { + $info[] = "Database - PDO MySQL - Found"; + } else { + $info[] = "Database - MySQL (pdo_mysql) extension not found"; + } + +// +// PostgreSQL functions +// + if ($m_pdo_pgsql == 1) { + $info[] = "Database support : PDO PostgreSQL - Found "; + if (Config::read_string('database_type') != 'pgsql') { + $warn[] = "Change the database_type to 'pgsql' in config.local.php if you want to use PostgreSQL"; + } + } else { + $warn[] = "Database - PostgreSQL (pdo_pgsql) extension not found"; + } + + if ($m_pdo_sqlite == 1) { + $info[] = "Database support : PDO SQLite - Found"; + if (Config::read_string('database_type') != 'sqlite') { + $warn[] = "Change the database_type to 'sqlite' in config.local.php if you want to use SQLite"; + } + } else { + $warn[] = "Database support - SQLite (pdo_sqlite) extension not found"; + } + + $link = null; + $error_text = null; + + try { + $link = db_connect(); + } catch (Exception $e) { + $error_text = $e->getMessage(); + } + + + if (!empty($link) && $error_text == "") { + $info[] = "Testing database connection (using {$CONF['database_type']}) - Success"; + } else { + $error[] = "Error: Can't connect to database - please check the \$CONF['database_*'] parameters in config.local.php : $error_text"; + } + +// +// Session functions +// + if ($f_session_start == 1) { + $info[] = "Depends on: session - OK"; + } else { + $error[] = "Error: Depends on: session - NOT FOUND. (FreeBSD: portinstall php$phpversion-session ?)."; + } + +// +// PCRE functions +// + if ($f_preg_match == 1) { + $info[] = "Depends on: pcre - Found"; + } else { + $error[] = "Error: Depends on: pcre - NOT FOUND. (FreeBSD: portinstall php$phpversion-pcre)"; + } + +// +// Multibyte functions +// + if ($f_mb_encode_mimeheader == 1) { + $info[] = "Depends on: multibyte string - Found"; + } else { + $error[] = "Error: Depends on: multibyte string - mbstring extension missing. (FreeBSD: portinstall php$phpversion-mbstring?)"; + } + + +// +// Imap functions +// + if ($f_imap_open == 1) { + $info[] = "IMAP functions - Found"; + } else { + $warn[] = "Warning: Optional dependency 'imap' extension missing, without this you may not be able to automcate creation of subfolders for new mailboxes)"; + } + +// +// If PHP <7.0, require random_compat works. Currently we bundle it via the Phar extension. +// + if (version_compare(phpversion(), "7.0", '<') + && !extension_loaded('Phar') + && $CONF['configured'] + && $CONF['encrypt'] == 'php_crypt') { + $error[] = "PHP before 7.0 requires 'Phar' extension support for secure random_int() function fallback. Either enable the 'Phar' extension, or install the random_compat library files from https://github.com/paragonie/random_compat and include/require them from functions.inc.php"; + $error[] = "PostfixAdmin has bundled lib/random_compat.phar but it's not usable on your installation due to the missing Phar extension."; + } + + + return ['error' => $error, 'warn' => $warn, 'info' => $info]; +} -/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ ?>