Skip to content

Commit 9e0cb01

Browse files
author
Florian Schaal
committed
updated dkim-handling
1 parent 5f82eef commit 9e0cb01

File tree

5 files changed

+131
-225
lines changed

5 files changed

+131
-225
lines changed

interface/web/js/dns_dkim.js

Lines changed: 0 additions & 72 deletions
This file was deleted.

interface/web/js/mail_domain_dkim.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131
This Javascript is invoked by
3232
* mail/templates/mail_domain_edit.htm to show and/or create the key-pair
3333
*/
34-
var request = false;
35-
3634
$('.subsectiontoggle').on('click', function(){
3735
$(this).children().toggleClass('showing').end().next().slideToggle();
3836
});
3937

40-
function setRequest(action) {
38+
var request = false;
39+
40+
//function setRequest(action) {
41+
function setRequest() {
4142
if (window.XMLHttpRequest) {
42-
request = new XMLHttpRequest();
43+
request = new XMLHttpRequest();
4344
} else if (window.ActiveXObject) {
4445
try {
4546
request = new ActiveXObject('Msxml2.XMLHTTP');
@@ -51,7 +52,6 @@ function setRequest(action) {
5152
catch (e) {}
5253
}
5354
}
54-
5555
if (!request) {
5656
alert("Error creating XMLHTTP-instance");
5757
return false;
@@ -64,12 +64,20 @@ function setRequest(action) {
6464
} else {
6565
var domain = jQuery('#domain').val();
6666
}
67+
68+
// we nedd the client-id to get the dkim-strength of the right mail-server
69+
try {
70+
var clientid = document.getElementById("client_group_id").selectedIndex; // admin and reseller
71+
}
72+
catch (e) {
73+
var clientid = jQuery('#client_id').val();; // user
74+
}
75+
6776
var selector=jQuery('#dkim_selector').val();
6877
var publickey=jQuery('#dkim_public').val();
69-
var privatekey=encodeURIComponent(document.getElementById("dkim_private").value)
7078
request.open('POST', 'mail/mail_domain_dkim_create.php', true);
7179
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
72-
request.send('domain='+domain+'&action='+action+'&dkim_selector='+selector+'&dkim_public='+publickey+'&dkim_private='+privatekey);
80+
request.send('domain='+domain+'&dkim_selector='+selector+'&dkim_public='+publickey+'&client_id='+clientid);
7381
request.onreadystatechange = interpretRequest;
7482
}
7583
}
@@ -86,10 +94,9 @@ function interpretRequest() {
8694
document.getElementsByName('dns_record')[0].value = request.responseXML.getElementsByTagName('dns_record')[0].firstChild.nodeValue;
8795
}
8896
break;
89-
9097
default:
9198
break;
9299
}
93100
}
94101

95-
setRequest('show');
102+
//setRequest('show');

interface/web/mail/mail_domain_dkim_create.php

Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,6 @@ function validate_selector($selector) {
5656
if ( preg_match($regex, $selector) === 1 ) return true; else return false;
5757
}
5858

59-
/**
60-
* This function fix PHP's messing up POST input containing characters space, dot,
61-
* open square bracket and others to be compatible with with the deprecated register_globals
62-
* @return array POST
63-
*/
64-
function getRealPOST() {
65-
$pairs = explode("&", file_get_contents("php://input"));
66-
$vars = array();
67-
foreach ($pairs as $pair) {
68-
$nv = explode("=", $pair, 2);
69-
$name = urldecode($nv[0]);
70-
$value = $nv[1];
71-
$vars[$name] = $value;
72-
}
73-
return $vars;
74-
}
75-
7659
/**
7760
* This function formats the public-key
7861
* @param array $pubkey
@@ -101,57 +84,68 @@ function get_public_key($private_key, $dkim_strength) {
10184
* @param string $old_selector
10285
* @return string selector
10386
*/
104-
function new_selector ($old_selector, $domain) {
87+
function new_selector ($old_selector, $domain, $client_id = -1) {
10588
global $app;
10689
//* validate post-values
10790
if ( validate_domain($domain) && validate_selector($old_selector) ) {
10891
//* get active selectors from dns
109-
$soa_rec = $app->db->queryOneRecord("SELECT * FROM dns_soa WHERE active = 'Y' AND origin = ?", $domain.'.');
92+
$soa_rec = $app->db->queryOneRecord("SELECT * FROM dns_soa WHERE active = 'Y' AND origin = ?");
11093
if ( isset($soa_rec) && !empty($soa_rec) ) {
11194
//* check for a dkim-record in the dns?
112-
$dns_data = $app->db->queryOneRecord("SELECT name FROM dns_rr WHERE name = ? AND active = 'Y'", $old_selector.'._domainkey.'.$domain.'.');
113-
$selector = str_replace( '._domainkey.'.$domain.'.', '', $dns_data['name']);
114-
if ( $old_selector == $selector) {
115-
$selector = substr($old_selector, 0, 53) . time(); //* add unix-timestamp to delimiter to allow old and new key in the dns
116-
} else {
117-
$selector = $old_selector;
95+
$dns_data = $app->db->queryOneRecord("SELECT name FROM dns_rr WHERE name = ? AND active = 'Y''", $old_selector.'._domainkey.'.$domain.'.');
96+
if ( !empty($dns_data) ){
97+
$selector = str_replace( '._domainkey.'.$domain.'.', '', $dns_data['name']);
98+
} else {
99+
}
100+
} else { //* no dns-zone found - check for existing mail-domain to create a new selector (we need this if a external dns is used)
101+
if ( $client_id >= 0 ) {
102+
$sql = "SELECT * from mail_domain WHERE dkim = 'y' AND domain = ? AND dkim_selector = ?";
103+
$maildomain = $app->db->queryOneRecord($sql, $domain, $old_selector);
104+
if ( !empty($maildomain) ) {
105+
$selector = $maildomain['selector'];
118106
}
107+
}
108+
}
109+
if ( $old_selector == $selector) {
110+
$selector = substr($old_selector, 0, 53) . time(); //* add unix-timestamp to delimiter to allow old and new key in the dns
111+
} else {
112+
$selector = $old_selector;
119113
}
120114
} else {
121115
$selector = 'invalid domain or selector';
122116
}
123117
return $selector;
124118
}
125119

120+
$client_id = $app->functions->intval($_POST['client_id']);
121+
126122
//* get dkim-strength for server_id
127-
//$mail_server_id = $app->functions->intval( $app->db->queryOneRecord("SELECT server_id from mail_domain WHERE domain = ?", $_POST['domain']) );
128-
//$dkim_strength = $app->functions->intval( $app->getconf->get_server_config($mail_server_id, 'mail')['dkim_strength'] );
129-
$rec = $app->db->queryOneRecord("SELECT server_id from mail_domain WHERE domain = ?", $_POST['domain']);
130-
$mail_server_id = $app->functions->intval($rec['server_id']);
131-
unset ($rec);
132-
$rec = $app->getconf->get_server_config($mail_server_id, 'mail');
133-
$dkim_strength = $app->functions->intval($rec['dkim_strength']);
134-
unset ($rec);
135-
if ( empty($dkim_strength) ) $dkim_strength = 1024;
136-
137-
switch ($_POST['action']) {
138-
case 'create': /* create DKIM Private-key */
139-
$_POST=getRealPOST();
140-
$rnd_val = $dkim_strength * 10;
141-
exec('openssl rand -out ../../temp/random-data.bin '.$rnd_val.' 2> /dev/null', $output, $result);
142-
exec('openssl genrsa -rand ../../temp/random-data.bin '.$dkim_strength.' 2> /dev/null', $privkey, $result);
143-
unlink('../../temp/random-data.bin');
144-
foreach($privkey as $values) $private_key=$private_key.$values."\n";
145-
//* check the selector for updated dkim-settings only
146-
if ( isset($_POST['dkim_public']) && !empty($_POST['dkim_public']) ) $selector = new_selector($_POST['dkim_selector'], $_POST['domain']);
147-
break;
148-
149-
case 'show': /* show the DNS-Record onLoad */
150-
$private_key=$_POST['dkim_private'];
151-
break;
123+
$sql = "SELECT server_id from mail_domain WHERE domain = ?";
124+
$mail_server = $app->db->queryOneRecord($sql, $_POST['domain']);
125+
if ( is_array($mail_server) ) { //* we are adding an existing mail-domain
126+
$mail_server_id = $app->functions->intval( $mail_server['server_id'] );
127+
} else {
128+
$sql = "SELECT default_mailserver FROM client WHERE client_id = ?";
129+
$mail_server = $app->db->queryOneRecord($sql, $client_id);
130+
$mail_server_id = $app->functions->intval( $mail_server['default_mailserver'] );
152131
}
132+
unset($mail_server);
133+
$mail_config = $app->getconf->get_server_config($mail_server_id, 'mail');
134+
$dkim_strength = $app->functions->intval($mail_config['dkim_strength']);
135+
unset($mail_config);
136+
137+
if ( empty($dkim_strength) ) $dkim_strength = 2048;
138+
139+
$rnd_val = $dkim_strength * 10;
140+
exec('openssl rand -out ../../temp/random-data.bin '.$rnd_val.' 2> /dev/null', $output, $result);
141+
exec('openssl genrsa -rand ../../temp/random-data.bin '.$dkim_strength.' 2> /dev/null', $privkey, $result);
142+
unlink("../../temp/random-data.bin");
143+
foreach($privkey as $values) $private_key=$private_key.$values."\n";
144+
//* check the selector for updated dkim-settings only
145+
if ( isset($_POST['dkim_public']) && !empty($_POST['dkim_public']) ) $selector = new_selector($_POST['dkim_selector'], $_POST['domain'], $client_id);
146+
147+
if ( !isset($public_key) ) $public_key=get_public_key($private_key, $dkim_strength);
153148

154-
$public_key=get_public_key($private_key, $dkim_strength);
155149
$dns_record=str_replace(array('-----BEGIN PUBLIC KEY-----','-----END PUBLIC KEY-----',"\r","\n"),'',$public_key);
156150

157151
if ( !isset($selector) ) {

0 commit comments

Comments
 (0)