mirror of
https://github.com/itflow-org/itflow
synced 2026-12-02 00:59:03 +00:00
138 lines
5.7 KiB
PHP
138 lines
5.7 KiB
PHP
<?php
|
|
|
|
/*
|
|
* ITFlow - the mail passwords, OAuth secret and tokens, encrypted in the database (database 4.3.1). They were kept
|
|
* as typed, so anyone who could read the database - a copy of it, a backup, a leaked dump - could sign in to the
|
|
* mailboxes ITFlow sends and reads from.
|
|
*
|
|
* Sealed as an RMM's API key and an identity provider's secret are (secretSeal(), functions/security.php): AES-256-GCM
|
|
* under a key made from config.php's backup key - the one secret ITFlow keeps outside the database, which restoring a
|
|
* backup needs already. A value saved before is read as it is, and sealed the next time it is saved, by the update and
|
|
* by the nightly job (mailSecretsSealAll()). Unlike theirs, one that can't be sealed - no backup key, config.php not
|
|
* writable - is kept as typed rather than lost, as mail would stop; Admin > Settings > Mail says so. One that can't be
|
|
* opened - the backup key was changed - reads as empty, and Admin > Settings > Mail says to enter it again.
|
|
*/
|
|
|
|
/** The columns that hold one. */
|
|
const MAIL_SECRET_COLUMNS = ['config_smtp_password', 'config_imap_password', 'config_mail_oauth_client_secret',
|
|
'config_mail_oauth_refresh_token', 'config_mail_oauth_access_token', 'config_mail_graph_token'];
|
|
|
|
/* Whether there is a key to seal them with - making the backup key when there is none and config.php can be written. */
|
|
function mailSecretKeyThere() {
|
|
return secretSealKey('mail') !== '';
|
|
}
|
|
|
|
/* Whether a stored value is sealed (secretSeal()'s "enc1:"). */
|
|
function mailSecretSealed($stored) {
|
|
return str_starts_with((string) $stored, 'enc1:');
|
|
}
|
|
|
|
/* $plain sealed to store - '' stays '', one sealed already stays as it is, and without a key it goes as it is. */
|
|
function mailSecretSeal($plain) {
|
|
$plain = (string) $plain;
|
|
if ($plain === '' || mailSecretSealed($plain)) {
|
|
return $plain;
|
|
}
|
|
$sealed = secretSeal($plain, 'mail');
|
|
return $sealed !== '' ? $sealed : $plain;
|
|
}
|
|
|
|
/* A stored value as it was typed - one from before as it is; one that can't be opened '' (mailSecretUnreadable()). */
|
|
function mailSecretOpen($stored) {
|
|
$stored = (string) $stored;
|
|
if (!mailSecretSealed($stored)) {
|
|
return $stored;
|
|
}
|
|
$plain = secretOpen($stored, 'mail');
|
|
if ($plain === '') {
|
|
mailSecretUnreadable(true);
|
|
}
|
|
return $plain;
|
|
}
|
|
|
|
/* Whether one couldn't be opened this request - $set true says one couldn't. */
|
|
function mailSecretUnreadable($set = null) {
|
|
static $unreadable = false;
|
|
if ($set !== null) {
|
|
$unreadable = (bool) $set;
|
|
}
|
|
return $unreadable;
|
|
}
|
|
|
|
/* Whether the 4.3.1 update has run - the columns are wide enough for a sealed value. */
|
|
function mailSecretReady() {
|
|
global $mysqli;
|
|
static $ready = null;
|
|
if ($ready === null) {
|
|
$version = defined('CURRENT_DATABASE_VERSION') ? CURRENT_DATABASE_VERSION
|
|
: (string) (mysqli_fetch_row(mysqli_query($mysqli, "SELECT config_current_database_version FROM settings WHERE company_id = 1"))[0] ?? '0');
|
|
$ready = version_compare($version, '4.3.1', '>=');
|
|
}
|
|
return $ready;
|
|
}
|
|
|
|
/* SQL for $column set to $plain, sealed - for the places that save a token as it comes. */
|
|
function mailSecretSql($column, $plain) {
|
|
global $mysqli;
|
|
$value = mailSecretReady() ? mailSecretSeal($plain) : (string) $plain;
|
|
return '`' . preg_replace('/[^a-z0-9_]/', '', $column) . "` = '" . mysqli_real_escape_string($mysqli, $value) . "'";
|
|
}
|
|
|
|
/*
|
|
* Seal every one still as typed - by the 4.3.1 update ($updating: its columns are made, though this request started
|
|
* before it) and the nightly job, for those saved before, or while there was no backup key. How many were sealed; -1
|
|
* when there is no key to seal them with.
|
|
*/
|
|
function mailSecretsSealAll($updating = false) {
|
|
global $mysqli;
|
|
if (!$updating && !mailSecretReady()) {
|
|
return 0;
|
|
}
|
|
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT " . implode(', ', MAIL_SECRET_COLUMNS) . " FROM settings WHERE company_id = 1")) ?: [];
|
|
$sets = [];
|
|
foreach (MAIL_SECRET_COLUMNS as $column) {
|
|
$value = (string) ($row[$column] ?? '');
|
|
if ($value !== '' && !mailSecretSealed($value)) {
|
|
if (!mailSecretKeyThere()) {
|
|
return -1;
|
|
}
|
|
$sets[] = '`' . $column . "` = '" . mysqli_real_escape_string($mysqli, mailSecretSeal($value)) . "'";
|
|
}
|
|
}
|
|
if ($sets) {
|
|
mysqli_query($mysqli, "UPDATE settings SET " . implode(', ', $sets) . " WHERE company_id = 1");
|
|
}
|
|
return count($sets);
|
|
}
|
|
|
|
/*
|
|
* What Admin > Settings > Mail says about them, or '' when all is well: kept as typed, as there is no backup key in
|
|
* config.php; or one saved that can't be opened, as the backup key changed.
|
|
*/
|
|
function mailSecretProblem() {
|
|
global $mysqli;
|
|
if (!mailSecretReady()) {
|
|
return '';
|
|
}
|
|
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT " . implode(', ', MAIL_SECRET_COLUMNS) . " FROM settings WHERE company_id = 1")) ?: [];
|
|
$plain = $unreadable = 0;
|
|
foreach (MAIL_SECRET_COLUMNS as $column) {
|
|
$value = (string) ($row[$column] ?? '');
|
|
if ($value === '') {
|
|
continue;
|
|
}
|
|
if (!mailSecretSealed($value)) {
|
|
$plain++;
|
|
} elseif (mailSecretOpen($value) === '') {
|
|
$unreadable++;
|
|
}
|
|
}
|
|
if ($unreadable) {
|
|
return 'A saved password or token can\'t be read - the backup key in config.php isn\'t the one it was saved with. Enter it again, or put the old key back in config.php.';
|
|
}
|
|
if ($plain && !mailSecretKeyThere()) {
|
|
return 'Passwords and tokens are kept as typed, as ITFlow couldn\'t add a backup key to config.php to encrypt them with. Make config.php writable, or open Maintenance > Backup for the line to add.';
|
|
}
|
|
return '';
|
|
}
|