Add a dead man switch login monitoring tool

This commit is contained in:
danwin1210 committed 2026-11-07 13:10:40 +01:00
1 parent 778b3808a8
commit f4464d0e66
3 files changed
+114

No files matched your search

+30
View File
@@ -44,6 +44,36 @@ const INVITE_CODES_ENABLED = false; // Require invite codes for public registrat
const INVITE_CODE_LIMIT = 3; // Number of invite codes a user may have generated before replacements become available const INVITE_CODE_LIMIT = 3; // Number of invite codes a user may have generated before replacements become available
const INVITE_WAIT_PERIOD_MONTHS = 1; // Months to wait after account creation and invite redemption const INVITE_WAIT_PERIOD_MONTHS = 1; // Months to wait after account creation and invite redemption
const INVITE_CODE_EXPIRATION_MONTHS = 6; // Months after which an invite code expires and is removed from the database const INVITE_CODE_EXPIRATION_MONTHS = 6; // Months after which an invite code expires and is removed from the database
const DEAD_MAN_SWITCH_ENABLED = false; // Enable staged inactivity notifications
const DEAD_MAN_SWITCH_MAILBOX = 'admin@example.com'; // Mailbox whose account activity resets the switch
const DEAD_MAN_SWITCH_TRUSTED_CONTACTS = ['trusted@example.com', 'trusted2@example.com']; // Email addresses of trusted contacts to be notified
const DEAD_MAN_SWITCH_CUSTOMER_CONTACTS = ['customer@example.com', 'customer2@example.com']; // Customer email addresses to be notified
const DEAD_MAN_SWITCH_STAGES = [
1 => [
'after' => 24 * 60 * 60,
'recipients' => [ DEAD_MAN_SWITCH_MAILBOX ],
'subject' => 'Dead man switch: please check in',
'body' => "No account activity has been recorded for 24 hours. Please sign in to your monitored mailbox to reset the dead man switch.\n",
],
2 => [
'after' => 48 * 60 * 60,
'recipients' => DEAD_MAN_SWITCH_TRUSTED_CONTACTS,
'subject' => 'Dead man switch: please contact me',
'body' => "No account activity has been recorded for 48 hours. Please try to contact me directly. If I am okay, signing in to " . DEAD_MAN_SWITCH_MAILBOX . " will reset the switch.\n",
],
3 => [
'after' => 72 * 60 * 60,
'recipients' => DEAD_MAN_SWITCH_TRUSTED_CONTACTS,
'subject' => 'Dead man switch: project handover instructions',
'body' => "No account activity has been recorded for 72 hours. The following are my instructions for taking over my projects:\n\nEDIT_ME\n",
],
4 => [
'after' => 96 * 60 * 60,
'recipients' => DEAD_MAN_SWITCH_CUSTOMER_CONTACTS,
'subject' => 'Important: your contact has changed',
'body' => "I have been unavailable for an extended period and something may have happened to me. For ongoing matters, please contact EDIT_ME.\n",
],
];
const LANGUAGES = [ const LANGUAGES = [
'cs' => ['name' => 'čeština', 'locale' => 'cs_CZ', 'flag' => '🇨🇿', 'show_in_menu' => true, 'dir' => 'ltr'], 'cs' => ['name' => 'čeština', 'locale' => 'cs_CZ', 'flag' => '🇨🇿', 'show_in_menu' => true, 'dir' => 'ltr'],
+1
View File
@@ -97,3 +97,4 @@ try {
echo _('Error synchronizing DKIM signing keys:') . PHP_EOL; echo _('Error synchronizing DKIM signing keys:') . PHP_EOL;
echo $exception->getMessage() . PHP_EOL; echo $exception->getMessage() . PHP_EOL;
} }
require_once __DIR__ . '/tools/dead_man_switch.php';
+83
View File
@@ -0,0 +1,83 @@
<?php
require_once __DIR__ . '/../common_config.php';
if ( ! DEAD_MAN_SWITCH_ENABLED ) {
return;
}
function dead_man_switch_validate_email( string $email ): void
{
if ( filter_var( $email, FILTER_VALIDATE_EMAIL ) === false ) {
throw new RuntimeException( sprintf( _('Invalid email address in dead man switch configuration: %s'), $email ) );
}
}
dead_man_switch_validate_email( DEAD_MAN_SWITCH_MAILBOX );
if ( empty(DEAD_MAN_SWITCH_TRUSTED_CONTACTS) ) {
throw new RuntimeException( _('At least one trusted contact must be configured.') );
}
foreach ( DEAD_MAN_SWITCH_TRUSTED_CONTACTS as $contact ) {
dead_man_switch_validate_email( $contact );
}
foreach ( DEAD_MAN_SWITCH_CUSTOMER_CONTACTS as $customer ) {
dead_man_switch_validate_email( $customer );
}
$db = get_db_instance();
$stmt = $db->prepare( 'SELECT `last_login`, UNIX_TIMESTAMP(`created`) AS `created_at` FROM `mailbox` WHERE `username` = ? AND `active` = 1;' );
$stmt->execute( [ DEAD_MAN_SWITCH_MAILBOX ] );
$monitored_mailbox = $stmt->fetch( PDO::FETCH_ASSOC );
if ( ! $monitored_mailbox ) {
throw new RuntimeException( sprintf( _('The configured dead man switch mailbox does not exist or is inactive: %s'), DEAD_MAN_SWITCH_MAILBOX ) );
}
$last_activity = (int) ( $monitored_mailbox['last_login'] ?? $monitored_mailbox['created_at'] );
$settings_key = 'dead_man_switch';
$stmt = $db->prepare( 'SELECT `value` FROM `settings` WHERE `setting` = ?;' );
$stmt->execute( [ $settings_key ] );
$stored_state = $stmt->fetchColumn();
if ( $stored_state === false ) {
$state = [
'mailbox' => DEAD_MAN_SWITCH_MAILBOX,
'last_activity' => $last_activity,
'notified_stage' => 0,
'last_run' => 0,
];
} else {
$state = json_decode( $stored_state, true, 512, JSON_THROW_ON_ERROR );
if ( !is_array($state) || $state['mailbox'] !== DEAD_MAN_SWITCH_MAILBOX || $state['last_activity'] !== $last_activity ) {
$state = [
'mailbox' => DEAD_MAN_SWITCH_MAILBOX,
'last_activity' => $last_activity,
'notified_stage' => 0,
'last_run' => 0,
];
}
}
$notified_stage = $state['notified_stage'];
$elapsed = time() - $last_activity;
$stage = $notified_stage + 1;
$notification = DEAD_MAN_SWITCH_STAGES[$stage] ?? null;
$stage_interval = $notification !== null && $stage > 1
? $notification['after'] - DEAD_MAN_SWITCH_STAGES[$notified_stage]['after']
: 0;
if (
$notification !== null
&& $elapsed >= $notification['after']
&& ( $state['last_run'] === 0 || time() - $state['last_run'] >= $stage_interval )
) {
foreach ( $notification['recipients'] as $recipient ) {
if ( ! mail( $recipient, $notification['subject'], $notification['body'], "Content-Type: text/plain; charset=UTF-8\r\n" ) ) {
throw new RuntimeException( sprintf( _('Unable to send dead man switch notification to %s.'), $recipient ) );
}
}
$state['notified_stage'] = $stage;
$state['last_run'] = time();
}
$stmt = $db->prepare( 'INSERT INTO `settings` (`setting`, `value`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`);' );
$stmt->execute( [ $settings_key, json_encode( $state, JSON_THROW_ON_ERROR ) ] );