Switch to using redis for dkim keys
This commit is contained in:
11 files changed
+115
-41
No files matched your search
+16
-1
@@ -4,6 +4,7 @@ use Egulias\EmailValidator\EmailLexer;
|
||||
use Egulias\EmailValidator\EmailParser;
|
||||
|
||||
require_once( '../common_config.php' );
|
||||
require_once( __DIR__ . '/../tools/sync_rspamd_dkim_keys.php' );
|
||||
global $language, $dir, $locale;
|
||||
session_start();
|
||||
if ( empty( $_SESSION[ 'csrf_token' ] ) ) {
|
||||
@@ -270,6 +271,7 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) {
|
||||
$stmt = $db->prepare( 'UPDATE domain SET dkim_key = ?, dkim_selector = ?, modified = NOW() WHERE domain = ?;' );
|
||||
$stmt->execute( [ $dkim[ 'key' ], $dkim[ 'selector' ], $_POST[ 'domain' ] ] );
|
||||
$msg .= '<div class="green" role="alert">'.htmlspecialchars(_('Successfully regenerated DKIM key.')).'</div>';
|
||||
$msg .= sync_admin_dkim_keys_alert();
|
||||
} catch ( RuntimeException $exception ) {
|
||||
error_log( 'Failed to regenerate DKIM key for ' . $_POST[ 'domain' ] . ': ' . $exception->getMessage() );
|
||||
$msg .= '<div class="red" role="alert">'.htmlspecialchars(_('Unable to generate a new DKIM key.')).'</div>';
|
||||
@@ -285,8 +287,9 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) {
|
||||
$stmt = $db->prepare( 'UPDATE domain SET active = ?, modified = NOW() WHERE domain = ?;' );
|
||||
$stmt->execute( [ $active, $_POST[ 'domain' ] ] );
|
||||
$msg .= '<div class="green" role="alert">'.htmlspecialchars(_('Successfully updated domain.')).'</div>';
|
||||
$msg .= sync_admin_dkim_keys_alert();
|
||||
}
|
||||
} elseif ( $_POST[ 'action' ] === 'save_edit_alis_domain' && ! empty( $_POST[ 'alias_domain' ] ) && $_SESSION[ 'email_admin_superadmin' ] ) {
|
||||
} elseif ( $_POST[ 'action' ] === 'save_edit_alias_domain' && ! empty( $_POST[ 'alias_domain' ] ) && $_SESSION[ 'email_admin_superadmin' ] ) {
|
||||
$stmt = $db->prepare( 'SELECT null FROM alias_domain WHERE alias_domain = ?;' );
|
||||
$stmt->execute( [ $_POST[ 'alias_domain' ] ] );
|
||||
if ( ! $stmt->fetch() ) {
|
||||
@@ -321,6 +324,7 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) {
|
||||
$stmt->execute( [ $ascii_domain, $utf8_domain ] );
|
||||
}
|
||||
$msg .= '<div class="green" role="alert">'.htmlspecialchars(_('Successfully created domain.')).'</div>';
|
||||
$msg .= sync_admin_dkim_keys_alert();
|
||||
}
|
||||
}
|
||||
} elseif ( $_POST[ 'action' ] === 'save_new_alias_domain' && ! empty( $_POST[ 'alias_domain' ] ) && $_SESSION[ 'email_admin_superadmin' ] ) {
|
||||
@@ -598,6 +602,17 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) {
|
||||
|
||||
<?php
|
||||
|
||||
function sync_admin_dkim_keys_alert(): string
|
||||
{
|
||||
try {
|
||||
sync_rspamd_dkim_keys( get_db_instance() );
|
||||
return '';
|
||||
} catch ( PDOException | RuntimeException $exception ) {
|
||||
error_log( 'Failed to synchronize DKIM signing keys after domain update: ' . $exception->getMessage() );
|
||||
return '<div class="red" role="alert">' . htmlspecialchars(_('The domain was saved, but DKIM keys could not be synchronized to Redis. The scheduled sync will retry.')) . '</div>';
|
||||
}
|
||||
}
|
||||
|
||||
function get_admin_list_page(): int
|
||||
{
|
||||
$page = isset( $_GET[ 'page' ] ) ? (int) $_GET[ 'page' ] : 1;
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
<?php
|
||||
require_once( '../common_config.php' );
|
||||
|
||||
header( 'Content-Type: text/plain; charset=utf-8' );
|
||||
header( 'Cache-Control: no-store' );
|
||||
header( 'Expires: 600' );
|
||||
|
||||
$token = is_string( $_GET[ 'token' ] ?? null ) ? $_GET[ 'token' ] : '';
|
||||
if ( ! in_array( $_SERVER[ 'REQUEST_METHOD' ], [ 'GET', 'HEAD' ], true ) || ! hash_equals( RSPAMD_SETTINGS_MAP_TOKEN, $token ) ) {
|
||||
http_response_code( 403 );
|
||||
exit;
|
||||
}
|
||||
|
||||
$stmt = get_db_instance()->query( "SELECT domain, dkim_key, dkim_selector FROM domain WHERE active = 1 ORDER BY domain;" );
|
||||
$map_entries = [];
|
||||
while ( $domain = $stmt->fetch( PDO::FETCH_ASSOC ) ) {
|
||||
if(empty($domain['dkim_key']) || str_ends_with($domain['domain'], '.onion')) {
|
||||
continue; // Skip domains without DKIM keys or onion domains
|
||||
}
|
||||
$ascii_domain = idn_to_ascii( $domain[ 'domain' ], IDNA_NONTRANSITIONAL_TO_ASCII );
|
||||
$key = get_rsa_dkim_private_key_base64_der( $domain[ 'dkim_key' ] );
|
||||
if ( ! is_string( $ascii_domain ) || ! preg_match( '/^[a-z0-9.-]+$/iD', $ascii_domain ) || $key === null || ! preg_match( '/^[a-zA-Z0-9_-]+$/D', $domain[ 'dkim_selector' ] ) ) {
|
||||
http_response_code( 500 );
|
||||
error_log( 'Invalid RSA DKIM key, selector, or domain configured for ' . $domain[ 'domain' ] );
|
||||
exit;
|
||||
}
|
||||
$ascii_domain = mb_strtolower( $ascii_domain );
|
||||
$map_entries[] = $ascii_domain . ' %:' . $domain[ 'dkim_selector' ] . ':' . $key;
|
||||
$map_entries[] = '*@' . $ascii_domain . ' ' . $ascii_domain;
|
||||
}
|
||||
|
||||
echo implode( "\n", $map_entries ) . ( $map_entries ? "\n" : '' );
|
||||
Reference in new issue
Block a user