From 9d113afc831f21b643a0dc36ec9e8c5bca4d9e5e Mon Sep 17 00:00:00 2001 From: Michael Kaufmann Date: Wed, 26 Jul 2023 10:13:50 +0200 Subject: [PATCH] send email notification to admin for non-successful let's encrypt results; fixes #1162 Signed-off-by: Michael Kaufmann --- lib/Froxlor/Api/ApiCommand.php | 1 + lib/Froxlor/Api/Commands/Customers.php | 5 +- lib/Froxlor/Cron/Http/LetsEncrypt/AcmeSh.php | 17 ++++++- lib/Froxlor/FileDir.php | 2 +- lib/Froxlor/System/Cronjob.php | 49 +++++++++----------- 5 files changed, 41 insertions(+), 33 deletions(-) diff --git a/lib/Froxlor/Api/ApiCommand.php b/lib/Froxlor/Api/ApiCommand.php index 327a1b4d..c2a17f50 100644 --- a/lib/Froxlor/Api/ApiCommand.php +++ b/lib/Froxlor/Api/ApiCommand.php @@ -397,6 +397,7 @@ abstract class ApiCommand extends ApiParameter $nat_fields = [ '`c`.`loginname`', + '`c`.`name`', '`a`.`loginname`', '`adminname`', '`databasename`', diff --git a/lib/Froxlor/Api/Commands/Customers.php b/lib/Froxlor/Api/Commands/Customers.php index d299b462..d23d3d24 100644 --- a/lib/Froxlor/Api/Commands/Customers.php +++ b/lib/Froxlor/Api/Commands/Customers.php @@ -100,7 +100,7 @@ class Customers extends ApiCommand implements ResourceEntity AND `id`<> :stdd "); $usages_stmt = Database::prepare(" - SELECT * FROM `" . TABLE_PANEL_DISKSPACE . "` + SELECT webspace, mail, mysql FROM `" . TABLE_PANEL_DISKSPACE . "` WHERE `customerid` = :cid ORDER BY `stamp` DESC LIMIT 1 "); @@ -109,11 +109,10 @@ class Customers extends ApiCommand implements ResourceEntity while ($row = $result_stmt->fetch(PDO::FETCH_ASSOC)) { if ($show_usages) { // get number of domains - Database::pexecute($domains_stmt, [ + $domains = Database::pexecute_first($domains_stmt, [ 'cid' => $row['customerid'], 'stdd' => $row['standardsubdomain'] ]); - $domains = $domains_stmt->fetch(PDO::FETCH_ASSOC); $row['domains'] = intval($domains['domains']); // get disk-space usages for web, mysql and mail $usages = Database::pexecute_first($usages_stmt, [ diff --git a/lib/Froxlor/Cron/Http/LetsEncrypt/AcmeSh.php b/lib/Froxlor/Cron/Http/LetsEncrypt/AcmeSh.php index 514dcc3d..659fb551 100644 --- a/lib/Froxlor/Cron/Http/LetsEncrypt/AcmeSh.php +++ b/lib/Froxlor/Cron/Http/LetsEncrypt/AcmeSh.php @@ -556,6 +556,10 @@ EOC; Settings::Set('system.le_froxlor_enabled', 0); } $cronlog->logAction(FroxlorLogger::CRON_ACTION, LOG_WARNING, "Let's Encrypt deactivated for domain " . $domain); + if (!defined('CRON_IS_FORCED') && !defined('CRON_DEBUG_FLAG')) { + // email info to admin that lets encrypt has been disabled for this domain + Cronjob::notifyMailToAdmin("Let's Encrypt has been deactivated for domain '" . $domain . "' due to failed dns validation (wrong or no IP address)"); + } } } } @@ -586,11 +590,20 @@ EOC; $acmesh_cmd .= " --debug"; } - $acme_result = FileDir::safe_exec($acmesh_cmd); + $exit_code = null; + $acme_result = FileDir::safe_exec($acmesh_cmd, $exit_code); // debug output of acme.sh run $cronlog->logAction(FroxlorLogger::CRON_ACTION, LOG_DEBUG, implode("\n", $acme_result)); - self::certToDb($certrow, $cronlog, $acme_result); + if ($exit_code != 0) { + $cronlog->logAction(FroxlorLogger::CRON_ACTION, LOG_DEBUG, "Non-successful exit-code returned :("); + if (!defined('CRON_IS_FORCED') && !defined('CRON_DEBUG_FLAG')) { + Cronjob::notifyMailToAdmin("Let's Encrypt certificate could not be obtained for: " . implode(", ", $domains) . "\n\n" . implode("\n", $acme_result)); + } + } else { + $cronlog->logAction(FroxlorLogger::CRON_ACTION, LOG_DEBUG, "Successful exit-code returned - storing certificate"); + self::certToDb($certrow, $cronlog, $acme_result); + } } } diff --git a/lib/Froxlor/FileDir.php b/lib/Froxlor/FileDir.php index 2502a4a9..41250b1d 100644 --- a/lib/Froxlor/FileDir.php +++ b/lib/Froxlor/FileDir.php @@ -219,7 +219,7 @@ class FileDir } // execute the command and return output - $return = ''; + $return = []; // ------------------------------------------------------------------------------- if ($return_value == false) { diff --git a/lib/Froxlor/System/Cronjob.php b/lib/Froxlor/System/Cronjob.php index 4a765049..a131052c 100644 --- a/lib/Froxlor/System/Cronjob.php +++ b/lib/Froxlor/System/Cronjob.php @@ -310,42 +310,37 @@ class Cronjob } /** - * Cronjob function to end a cronjob in a critical condition - * but not without sending a notification mail to the admin + * Send notification to system admin via email * * @param string $message * @param string $subject * * @return void */ - public static function dieWithMail(string $message, string $subject = "[froxlor] Cronjob error") + public static function notifyMailToAdmin(string $message, string $subject = "[froxlor] Important notice") { - if (Settings::Get('system.send_cron_errors') == '1') { - $_mail = new Mailer(true); - $_mailerror = false; - $mailerr_msg = ""; - try { - $_mail->Subject = $subject; - $_mail->AltBody = $message; - $_mail->MsgHTML(nl2br($message)); - $_mail->AddAddress(Settings::Get('panel.adminmail'), Settings::Get('panel.adminmail_defname')); - $_mail->Send(); - } catch (\PHPMailer\PHPMailer\Exception $e) { - $mailerr_msg = $e->errorMessage(); - $_mailerror = true; - } catch (Exception $e) { - $mailerr_msg = $e->getMessage(); - $_mailerror = true; - } - - $_mail->ClearAddresses(); - - if ($_mailerror) { - echo 'Error sending mail: ' . $mailerr_msg . "\n"; - } + $mail = new Mailer(true); + $mailerror = false; + $mailerr_msg = ""; + try { + $mail->Subject = $subject; + $mail->AltBody = $message; + $mail->MsgHTML(nl2br($message)); + $mail->AddAddress(Settings::Get('panel.adminmail'), Settings::Get('panel.adminmail_defname')); + $mail->Send(); + } catch (\PHPMailer\PHPMailer\Exception $e) { + $mailerr_msg = $e->errorMessage(); + $mailerror = true; + } catch (Exception $e) { + $mailerr_msg = $e->getMessage(); + $mailerror = true; } - die($message); + $mail->ClearAddresses(); + + if ($mailerror) { + echo 'Error sending mail: ' . $mailerr_msg . "\n"; + } } /**