[Web] Allow to overwrite DKIM keys while importing a new key

master
andryyy 2020-09-26 22:01:15 +02:00
parent 53dd02bc63
commit 23f118a152
No known key found for this signature in database
GPG Key ID: 8EC34FF2794E25EF
1 changed files with 14 additions and 10 deletions

View File

@ -50,10 +50,10 @@ function dkim($_action, $_data = null, $privkey = false) {
if ($keypair_ressource = openssl_pkey_new($config)) { if ($keypair_ressource = openssl_pkey_new($config)) {
$key_details = openssl_pkey_get_details($keypair_ressource); $key_details = openssl_pkey_get_details($keypair_ressource);
$pubKey = implode(array_slice( $pubKey = implode(array_slice(
array_filter( array_filter(
explode(PHP_EOL, $key_details['key']) explode(PHP_EOL, $key_details['key'])
), 1, -1) ), 1, -1)
); );
// Save public key and selector to redis // Save public key and selector to redis
try { try {
$redis->hSet('DKIM_PUB_KEYS', $domain, $pubKey); $redis->hSet('DKIM_PUB_KEYS', $domain, $pubKey);
@ -150,6 +150,7 @@ function dkim($_action, $_data = null, $privkey = false) {
return false; return false;
} }
$private_key_input = trim($_data['private_key_file']); $private_key_input = trim($_data['private_key_file']);
$overwrite_existing = intval($_data['overwrite_existing']);
$private_key_normalized = preg_replace('~\r\n?~', "\n", $private_key_input); $private_key_normalized = preg_replace('~\r\n?~', "\n", $private_key_input);
$private_key = openssl_pkey_get_private($private_key_normalized); $private_key = openssl_pkey_get_private($private_key_normalized);
if ($ssl_error = openssl_error_string()) { if ($ssl_error = openssl_error_string()) {
@ -178,12 +179,14 @@ function dkim($_action, $_data = null, $privkey = false) {
return false; return false;
} }
if ($redis->hGet('DKIM_PUB_KEYS', $domain)) { if ($redis->hGet('DKIM_PUB_KEYS', $domain)) {
$_SESSION['return'][] = array( if ($overwrite_existing == 0) {
'type' => 'danger', $_SESSION['return'][] = array(
'log' => array(__FUNCTION__, $_action, $_data, $privkey), 'type' => 'danger',
'msg' => array('dkim_domain_or_sel_invalid', $domain) 'log' => array(__FUNCTION__, $_action, $_data, $privkey),
); 'msg' => array('dkim_domain_or_sel_exists', $domain)
return false; );
return false;
}
} }
if (!ctype_alnum($dkim_selector)) { if (!ctype_alnum($dkim_selector)) {
$_SESSION['return'][] = array( $_SESSION['return'][] = array(
@ -194,6 +197,7 @@ function dkim($_action, $_data = null, $privkey = false) {
return false; return false;
} }
try { try {
dkim('delete', $domain);
$redis->hSet('DKIM_PUB_KEYS', $domain, $pem_public_key); $redis->hSet('DKIM_PUB_KEYS', $domain, $pem_public_key);
$redis->hSet('DKIM_SELECTORS', $domain, $dkim_selector); $redis->hSet('DKIM_SELECTORS', $domain, $dkim_selector);
$redis->hSet('DKIM_PRIV_KEYS', $dkim_selector . '.' . $domain, $private_key_normalized); $redis->hSet('DKIM_PRIV_KEYS', $dkim_selector . '.' . $domain, $private_key_normalized);