send email notification to admin for non-successful let's encrypt results; fixes #1162

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann
2023-07-26 10:13:50 +02:00
parent bbd1dca30e
commit 9d113afc83
5 changed files with 41 additions and 33 deletions

View File

@@ -397,6 +397,7 @@ abstract class ApiCommand extends ApiParameter
$nat_fields = [ $nat_fields = [
'`c`.`loginname`', '`c`.`loginname`',
'`c`.`name`',
'`a`.`loginname`', '`a`.`loginname`',
'`adminname`', '`adminname`',
'`databasename`', '`databasename`',

View File

@@ -100,7 +100,7 @@ class Customers extends ApiCommand implements ResourceEntity
AND `id`<> :stdd AND `id`<> :stdd
"); ");
$usages_stmt = Database::prepare(" $usages_stmt = Database::prepare("
SELECT * FROM `" . TABLE_PANEL_DISKSPACE . "` SELECT webspace, mail, mysql FROM `" . TABLE_PANEL_DISKSPACE . "`
WHERE `customerid` = :cid WHERE `customerid` = :cid
ORDER BY `stamp` DESC LIMIT 1 ORDER BY `stamp` DESC LIMIT 1
"); ");
@@ -109,11 +109,10 @@ class Customers extends ApiCommand implements ResourceEntity
while ($row = $result_stmt->fetch(PDO::FETCH_ASSOC)) { while ($row = $result_stmt->fetch(PDO::FETCH_ASSOC)) {
if ($show_usages) { if ($show_usages) {
// get number of domains // get number of domains
Database::pexecute($domains_stmt, [ $domains = Database::pexecute_first($domains_stmt, [
'cid' => $row['customerid'], 'cid' => $row['customerid'],
'stdd' => $row['standardsubdomain'] 'stdd' => $row['standardsubdomain']
]); ]);
$domains = $domains_stmt->fetch(PDO::FETCH_ASSOC);
$row['domains'] = intval($domains['domains']); $row['domains'] = intval($domains['domains']);
// get disk-space usages for web, mysql and mail // get disk-space usages for web, mysql and mail
$usages = Database::pexecute_first($usages_stmt, [ $usages = Database::pexecute_first($usages_stmt, [

View File

@@ -556,6 +556,10 @@ EOC;
Settings::Set('system.le_froxlor_enabled', 0); Settings::Set('system.le_froxlor_enabled', 0);
} }
$cronlog->logAction(FroxlorLogger::CRON_ACTION, LOG_WARNING, "Let's Encrypt deactivated for domain " . $domain); $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"; $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 // debug output of acme.sh run
$cronlog->logAction(FroxlorLogger::CRON_ACTION, LOG_DEBUG, implode("\n", $acme_result)); $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);
}
} }
} }

View File

@@ -219,7 +219,7 @@ class FileDir
} }
// execute the command and return output // execute the command and return output
$return = ''; $return = [];
// ------------------------------------------------------------------------------- // -------------------------------------------------------------------------------
if ($return_value == false) { if ($return_value == false) {

View File

@@ -310,42 +310,37 @@ class Cronjob
} }
/** /**
* Cronjob function to end a cronjob in a critical condition * Send notification to system admin via email
* but not without sending a notification mail to the admin
* *
* @param string $message * @param string $message
* @param string $subject * @param string $subject
* *
* @return void * @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);
$_mail = new Mailer(true); $mailerror = false;
$_mailerror = false; $mailerr_msg = "";
$mailerr_msg = ""; try {
try { $mail->Subject = $subject;
$_mail->Subject = $subject; $mail->AltBody = $message;
$_mail->AltBody = $message; $mail->MsgHTML(nl2br($message));
$_mail->MsgHTML(nl2br($message)); $mail->AddAddress(Settings::Get('panel.adminmail'), Settings::Get('panel.adminmail_defname'));
$_mail->AddAddress(Settings::Get('panel.adminmail'), Settings::Get('panel.adminmail_defname')); $mail->Send();
$_mail->Send(); } catch (\PHPMailer\PHPMailer\Exception $e) {
} catch (\PHPMailer\PHPMailer\Exception $e) { $mailerr_msg = $e->errorMessage();
$mailerr_msg = $e->errorMessage(); $mailerror = true;
$_mailerror = true; } catch (Exception $e) {
} catch (Exception $e) { $mailerr_msg = $e->getMessage();
$mailerr_msg = $e->getMessage(); $mailerror = true;
$_mailerror = true;
}
$_mail->ClearAddresses();
if ($_mailerror) {
echo 'Error sending mail: ' . $mailerr_msg . "\n";
}
} }
die($message); $mail->ClearAddresses();
if ($mailerror) {
echo 'Error sending mail: ' . $mailerr_msg . "\n";
}
} }
/** /**