diff --git a/install/sql/incremental/upd_0052.sql b/install/sql/incremental/upd_0052.sql new file mode 100644 index 0000000000000000000000000000000000000000..b8e3829a1f5d987a47fe4d6bc8412a0db53faed7 --- /dev/null +++ b/install/sql/incremental/upd_0052.sql @@ -0,0 +1,3 @@ +ALTER TABLE `mail_domain` ADD `dkim_public` MEDIUMTEXT NOT NULL AFTER `domain`; +ALTER TABLE `mail_domain` ADD `dkim_private` MEDIUMTEXT NOT NULL AFTER `domain`; +ALTER TABLE `mail_domain` ADD `dkim` ENUM( 'n', 'y' ) NOT NULL AFTER `domain`; diff --git a/install/sql/ispconfig3.sql b/install/sql/ispconfig3.sql index be8038f383c6754e997e82f9ef9013b203dd3bbd..2a2d4a0d44c9e7cf59e56a58dd8d1f0810aaa583 100644 --- a/install/sql/ispconfig3.sql +++ b/install/sql/ispconfig3.sql @@ -663,6 +663,9 @@ CREATE TABLE `mail_domain` ( `sys_perm_other` varchar(5) NOT NULL default '', `server_id` int(11) unsigned NOT NULL default '0', `domain` varchar(255) NOT NULL default '', + `dkim` ENUM( 'n', 'y' ) NOT NULL default 'n', + `dkim_private` mediumtext NOT NULL default '', + `dkim_public` mediumtext NOT NULL default '', `active` enum('n','y') NOT NULL, PRIMARY KEY (`domain_id`), KEY `server_id` (`server_id`,`domain`), @@ -2172,4 +2175,4 @@ INSERT INTO `sys_user` (`userid`, `sys_userid`, `sys_groupid`, `sys_perm_user`, INSERT INTO sys_config VALUES ('1','db','db_version','3.0.5.2'); -SET FOREIGN_KEY_CHECKS = 1; \ No newline at end of file +SET FOREIGN_KEY_CHECKS = 1; diff --git a/install/tpl/amavisd_user_config.master b/install/tpl/amavisd_user_config.master index c89f8758833b7dfeb5f7a3ed1ae4860431291759..f37795501dcd1559873c03e037ac94bd8b830f08 100644 --- a/install/tpl/amavisd_user_config.master +++ b/install/tpl/amavisd_user_config.master @@ -75,6 +75,12 @@ $LOGFILE = "/var/log/amavis.log"; # (defaults to empty, no log) # Set the log_level to 5 for debugging $log_level = 0; # (defaults to 0) +# DKIM + +$enable_dkim_verification = 1; +$enable_dkim_signing = 1; # load DKIM signing code, +@dkim_signature_options_bysender_maps = ( +{ '.' => { ttl => 21*24*3600, c => 'relaxed/simple' } } ); #------------ Do not modify anything below this line ------------- 1; # insure a defined return diff --git a/install/tpl/opensuse_amavisd_conf.master b/install/tpl/opensuse_amavisd_conf.master index d0d8a8777314210d030e37b22b639547a2bb2fdf..5f9f867dc3363ac81e59895732d2e9e862845742 100644 --- a/install/tpl/opensuse_amavisd_conf.master +++ b/install/tpl/opensuse_amavisd_conf.master @@ -778,5 +778,11 @@ $spam_admin = undef; $DO_SYSLOG = 1; $LOGFILE = "/var/log/amavis.log"; # (defaults to empty, no log) +# DKIM + +$enable_dkim_verification = 1; +$enable_dkim_signing = 1; # load DKIM signing code, +@dkim_signature_options_bysender_maps = ( +{ '.' => { ttl => 21*24*3600, c => 'relaxed/simple' } } ); 1; # insure a defined return diff --git a/install/tpl/server.ini.master b/install/tpl/server.ini.master index 3f3dc365efea44192c1e6527a621aa3a6273bcc1..007d51bff91adc11808141250923bd20838e3214 100644 --- a/install/tpl/server.ini.master +++ b/install/tpl/server.ini.master @@ -21,6 +21,7 @@ backup_mode=rootgz module=postfix_mysql maildir_path=/var/vmail/[domain]/[localpart] homedir_path=/var/vmail +dkim_path=/etc/postfix/dkim pop3_imap_daemon=courier mail_filter_syntax=maildrop mailuser_uid=5000 diff --git a/interface/lib/classes/validate_dkim.inc.php b/interface/lib/classes/validate_dkim.inc.php new file mode 100644 index 0000000000000000000000000000000000000000..fccd45765db9612b726347ce1ba9cd33d5cef862 --- /dev/null +++ b/interface/lib/classes/validate_dkim.inc.php @@ -0,0 +1,60 @@ +tform->wordbook[$errmsg])) { + return $app->tform->wordbook[$errmsg]."
\r\n"; + } else { + return $errmsg."
\r\n"; + } + } + + /* Validator function for private DKIM-Key */ + function check_private_key($field_name, $field_value, $validator) { + $dkim_enabled=$_POST['dkim']; + if ($dkim_enabled == 'y') { + if (empty($field_value)) return $this->get_error($validator['errmsg']); + exec('echo "'.$field_value.'"|openssl rsa -check',$output,$result); + if($result != 0) return $this->get_error($validator['errmsg']); + } + } + + /* Validator function for DKIM Path */ + function check_dkim_path($field_name, $field_value, $validator) { + if(empty($field_value)) return $this->get_error($validator['errmsg']); + if (substr(sprintf('%o', fileperms($field_value)),-3) <= 600) + return $this->get_error($validator['errmsg']); + } + +} diff --git a/interface/web/admin/form/server_config.tform.php b/interface/web/admin/form/server_config.tform.php index 0f0c38b2e75a822b6bec68204ed3b1f37a4ea0cc..fb5bf87fd57c45571b0985d9f5bcd4200ef86f99 100644 --- a/interface/web/admin/form/server_config.tform.php +++ b/interface/web/admin/form/server_config.tform.php @@ -211,6 +211,19 @@ $form["tabs"]['mail'] = array( 'width' => '40', 'maxlength' => '255' ), + 'dkim_path' => array( + 'datatype' => 'VARCHAR', + 'formtype' => 'TEXT', + 'default' => '/var/db/dkim', + 'validators' => array ( 0 => array ('type' => 'CUSTOM', + 'class' => 'validate_dkim', + 'function' => 'check_dkim_path', + 'errmsg'=> 'dkim_path_error'), + ), + 'value' => '', + 'width' => '40', + 'maxlength' => '255' + ), 'pop3_imap_daemon' => array( 'datatype' => 'VARCHAR', 'formtype' => 'SELECT', diff --git a/interface/web/admin/lib/lang/en_server_config.lng b/interface/web/admin/lib/lang/en_server_config.lng index b39dd0e5387d0d7b70b22a13a846e7e5aaa0775d..b9f270e14eea17fbc0ebbbb5fa0fe7ffa7d1db0d 100644 --- a/interface/web/admin/lib/lang/en_server_config.lng +++ b/interface/web/admin/lib/lang/en_server_config.lng @@ -30,6 +30,8 @@ $wb["fastcgi_bin_txt"] = 'FastCGI Bin'; $wb["module_txt"] = 'Module'; $wb["maildir_path_txt"] = 'Maildir Path'; $wb["homedir_path_txt"] = 'Homedir Path'; +$wb["dkim_path_txt"] = 'DKIM Path'; +$wb["dkim_path_error"] = 'DKIM Path not found or not writeable.'; $wb["mailuser_uid_txt"] = 'Mailuser UID'; $wb["mailuser_gid_txt"] = 'Mailuser GID'; $wb["mailuser_name_txt"] = 'Mailuser Name'; diff --git a/interface/web/admin/templates/server_config_mail_edit.htm b/interface/web/admin/templates/server_config_mail_edit.htm index fba0bf72ec4d704aad6c81de7e172134cd4778ed..03749317ca23e05c743b0e207f8dab697c22d924 100644 --- a/interface/web/admin/templates/server_config_mail_edit.htm +++ b/interface/web/admin/templates/server_config_mail_edit.htm @@ -19,6 +19,10 @@ +
+ + +

{tmpl_var name='pop3_imap_daemon_txt'}

@@ -95,4 +99,4 @@
- \ No newline at end of file + diff --git a/interface/web/mail/form/mail_domain.tform.php b/interface/web/mail/form/mail_domain.tform.php index 506f934e385925a45d51f35704b4b5303bcf53ca..caf48d50b363c809e0b8abcaf89666dd6f41fe29 100644 --- a/interface/web/mail/form/mail_domain.tform.php +++ b/interface/web/mail/form/mail_domain.tform.php @@ -98,6 +98,33 @@ $form["tabs"]['domain'] = array ( 'maxlength' => '255', 'searchable' => 1 ), + 'dkim' => array ( + 'datatype' => 'VARCHAR', + 'formtype' => 'CHECKBOX', + 'default' => 'n', + 'value' => array(0 => 'n',1 => 'y') + ), + 'dkim_private' => array ( + 'datatype' => 'TEXT', + 'formtype' => 'TEXTAREA', + 'default' => '', + 'value' => '', + 'cols' => '30', + 'rows' => '10', + 'validators' => array ( 0 => array ('type' => 'CUSTOM', + 'class' => 'validate_dkim', + 'function' => 'check_private_key', + 'errmsg'=> 'dkim_private_key_error'), + ), + ), + 'dkim_public' => array ( + 'datatype' => 'TEXT', + 'formtype' => 'TEXTAREA', + 'default' => '', + 'value' => '', + 'cols' => '30', + 'rows' => '10' + ), 'active' => array ( 'datatype' => 'VARCHAR', 'formtype' => 'CHECKBOX', @@ -111,4 +138,4 @@ $form["tabs"]['domain'] = array ( ); -?> \ No newline at end of file +?> diff --git a/interface/web/mail/lib/lang/en_mail_domain.lng b/interface/web/mail/lib/lang/en_mail_domain.lng index ad93100696d4c75e62bf26f88ab0ba4c5293e088..00ddf3332bfae0921c950b5ee662c1f8fea7c42b 100644 --- a/interface/web/mail/lib/lang/en_mail_domain.lng +++ b/interface/web/mail/lib/lang/en_mail_domain.lng @@ -3,11 +3,17 @@ $wb["server_id_txt"] = 'Server'; $wb["domain_txt"] = 'Domain'; $wb["type_txt"] = 'Type'; $wb["active_txt"] = 'Active'; +$wb["dkim_txt"] = 'enable DKIM'; +$wb["dkim_private_txt"] = 'DKIM Private-key'; +$wb["dkim_generate_txt"] = 'Generate DKIM Private-key'; +$wb["dkim_dns_txt"] = 'DNS-Record (TYPE TXT)

add this record to your DNS'; +$wb["dkim_private_key_error"] = 'Invalid DKIM-Private key'; $wb["domain_error_empty"] = 'Domain is empty.'; $wb["domain_error_unique"] = 'Duplicate Domain.'; $wb["domain_error_regex"] = 'Invalid domain name.'; +$wb["dkim_settings_txt"] = 'DomainKeys Identified Mail (DKIM)'; $wb["client_txt"] = 'Client'; $wb["limit_maildomain_txt"] = 'The max. number of email domains for your account is reached.'; $wb["policy_txt"] = 'Spamfilter'; $wb["no_policy"] = '- not enabled -'; -?> \ No newline at end of file +?> diff --git a/interface/web/mail/mail_domain_dkim_create.php b/interface/web/mail/mail_domain_dkim_create.php new file mode 100644 index 0000000000000000000000000000000000000000..758d3afb8a09a58f6a80612579abf5caffd88eca --- /dev/null +++ b/interface/web/mail/mail_domain_dkim_create.php @@ -0,0 +1,90 @@ +\n"; +echo "\n"; +echo "".$private_key."\n"; +echo "".$dns_record."\n"; +echo "\n"; +?> diff --git a/interface/web/mail/templates/mail_domain_edit.htm b/interface/web/mail/templates/mail_domain_edit.htm index 5a1b61379237567ab0a18e278bc0b5e0dc25c535..81cdb73b0f494b490aa27fbf34ac85475f882d15 100644 --- a/interface/web/mail/templates/mail_domain_edit.htm +++ b/interface/web/mail/templates/mail_domain_edit.htm @@ -59,6 +59,24 @@ {tmpl_var name='active'} +
{tmpl_var name='dkim_settings_txt'}
+
+
+

{tmpl_var name='dkim_txt'}

+
+ {tmpl_var name='dkim'} +
+
+
+ + +{tmpl_var name='dkim_generate_txt'} +
+
+ + +
+
@@ -70,4 +88,45 @@ - \ No newline at end of file + + + diff --git a/server/plugins-available/mail_plugin_dkim.inc.php b/server/plugins-available/mail_plugin_dkim.inc.php new file mode 100644 index 0000000000000000000000000000000000000000..9ad7348680618f07aa233974816d18d11628b9bb --- /dev/null +++ b/server/plugins-available/mail_plugin_dkim.inc.php @@ -0,0 +1,286 @@ +plugins->registerEvent('mail_domain_delete',$this->plugin_name,'domain_dkim_delete'); + $app->plugins->registerEvent('mail_domain_insert',$this->plugin_name,'domain_dkim_insert'); + $app->plugins->registerEvent('mail_domain_update',$this->plugin_name,'domain_dkim_update'); + + // Register service + $app->services->registerService('amavisd','mail_module','restartAmavisd'); + } + + /* + This function gets the amavisd-config file + */ + function get_amavis_config() { + $pos_config=array( + '/etc/amavisd.conf', + '/etc/amavisd.conf/50-user' + ); + $amavis_configfile=''; + foreach($pos_config as $conf) { + if (is_file($conf)) { + $amavis_configfile=$conf; + break; + } + } + return $amavis_configfile; + } + + /* + This function checks the relevant configs and disables dkim for the domain + if the directory for dkim is not writeable or does not exist + */ + function check_system($data) { + global $app,$mail_config; + $app->uses('getconf'); + $check=true; + /* check for amavis-config */ + if ( $this->get_amavis_config() == '' || !is_writeable($this->get_amavis_config()) ) { + $app->log('Amavis-config not found or not writeable.',LOGLEVEL_ERROR); + $check=false; + } + /* dir for dkim-keys writeable? */ + $mail_config = $app->getconf->get_server_config($conf['server_id'], 'mail'); + if (isset($mail_config['dkim_path']) && isset($data['new']['dkim_private']) && !empty($data['new']['dkim_private'])) { + if (!is_writeable($mail_config['dkim_path'])) { + $app->log('DKIM Path '.$mail_config['dkim_path'].' not found or not writeable.',LOGLEVEL_ERROR); + $check=false; + } + } else { + $app->log('Unable to write DKIM settings; Check your config!',LOGLEVEL_ERROR); + $check=false; + } + if (!$check) { + $app->db->query("UPDATE mail_domain SET dkim = 'n' WHERE domain = '".$data['new']['domain']."'"); + $app->dbmaster->query("UPDATE mail_domain SET dkim = 'n' WHERE domain = '".$data['new']['domain']."'"); + } + return $check; + } + + /* + This function restarts amavis + */ + function restart_amavis() { + global $app,$conf; + $initfile=$conf['init_scripts'].'/amavis'; + $app->log('Restarting amavis.',LOGLEVEL_DEBUG); +// exec($conf['init_scripts'].'/amavis restart',$output); +// foreach($output as $logline) $app->log($logline,LOGLEVEL_DEBUG); + } + + /* + This function writes the keyfiles (public and private) + The public-key is always created and stored into the db and local key-file + */ + function write_dkim_key($key_file,$key_value,$key_domain) { + global $app,$mailconfig; + $success=false; + if (!file_put_contents($key_file.'.private',$key_value) === false) { + $app->log('Saved DKIM Private-key to '.$key_file.'.private',LOGLEVEL_DEBUG); + $success=true; + /* now we get the DKIM Public-key */ + exec('cat "'.$key_file.'.private'.'"|openssl rsa -pubout',$pubkey,$result); + $public_key=''; + foreach($pubkey as $values) $public_key=$public_key.$values."\n"; + /* save the DKIM Public-key in dkim-dir */ + if (!file_put_contents($key_file.'.public',$public_key) === false) + $app->log('Saved DKIM Public to '.$key_domain.'.',LOGLEVEL_DEBUG); + else $app->log('Unable to save DKIM Public to '.$key_domain.'.',LOGLEVEL_WARNING); + /* store the private-key to the databse(s) */ + $app->log('Store the DKIM Public-key in database.',LOGLEVEL_DEBUG); + $app->db->query("UPDATE mail_domain SET dkim_public = '".$public_key."' WHERE domain = '".$ky_domain."'"); + $app->dbmaster->query("UPDATE mail_domain SET dkim_public = '".$public_key."' WHERE domain = '".$key_domain."'"); + } + return $success; + } + + /* + This function removes the keyfiles + */ + function remove_dkim_key($key_file,$key_domain) { + global $app; + if (file_exists($key_file.'.private')) { + exec('rm -f '.$key_file.'.private'); + $app->log('Deleted the DKIM Private-key for '.$key_domain.'.',LOGLEVEL_DEBUG); + } else $app->log('Unable to delete the DKIM Private-key for '.$key_domain.' (not found).',LOGLEVEL_DEBUG); + if (file_exists($key_file.'.public')) { + exec('rm -f '.$key_file.'.public'); + $app->log('Deleted the DKIM Public-key for '.$key_domain.'.',LOGLEVEL_DEBUG); + } else $app->log('Unable to delete the DKIM Public-key for '.$key_domain.' (not found).',LOGLEVEL_DEBUG); + } + + /* + This function adds the entry to the amavisd-config + */ + function add_to_amavis($key_domain) { + global $app,$mail_config; + $amavis_config = file_get_contents($this->get_amavis_config()); + $key_value="dkim_key('".$key_domain."', 'default', '".$mail_config['dkim_path']."/".$key_domain.".private');\n"; + if(strpos($amavis_config, $key_value) !== false) $amavis_config = str_replace($key_value, '', $amavis_config); + if (!file_put_contents($this->get_amavis_config(),$key_value,FILE_APPEND) === false) { + $app->log('Adding DKIM Private-key to amavis-config.',LOGLEVEL_DEBUG); + $this->restart_amavis(); + } + } + + /* + This function removes the entry from the amavisd-config + */ + function remove_from_amavis($key_domain) { + global $app; + $amavis_config = file($this->get_amavis_config()); + $i=0;$found=false; + foreach($amavis_config as $line) { + if (preg_match("/^\bdkim_key\b.*\b".$key_domain."\b/",$line)) { + unset($amavis_config[$i]); + $found=true; + } + $i++; + } + if ($found) { + file_put_contents($this->get_amavis_config(), $amavis_config); + $app->log('Deleted the DKIM settings from amavis-config for '.$key_domain.'.',LOGLEVEL_DEBUG); + $this->restart_amavis(); + } else $app->log('Unable to delete the DKIM settings from amavis-config for '.$key_domain.'.',LOGLEVEL_ERROR); + } + + /* + This function controlls new key-files and amavisd-entries + */ + function add_dkim($data) { + global $app; + $mail_config = $app->getconf->get_server_config($conf['server_id'], 'mail'); + if ( substr($mail_config['dkim_path'],strlen($mail_config['dkim_path'])-1) == '/' ) + $mail_config['dkim_path'] = substr($mail_config['dkim_path'],0,strlen($mail_config['dkim_path'])-1); + if ($this->write_dkim_key($mail_config['dkim_path']."/".$data['new']['domain'],$data['new']['dkim_private'],$data['new']['domain'])) { + $this->add_to_amavis($data['new']['domain']); + } else { + $app->log('Error saving the DKIM Private-key for '.$data['new']['domain'].' - DKIM is now disabled for the domain.',LOGLEVEL_ERROR); + $app->db->query("UPDATE mail_domain SET dkim = 'n' WHERE domain = '".$data['new']['domain']."'"); + $app->dbmaster->query("UPDATE mail_domain SET dkim = 'n' WHERE domain = '".$data['new']['domain']."'"); + } + } + + /* + This function controlls the removement of keyfiles (public and private) + and the entry in the amavisd-config + */ + function remove_dkim($_data) { + global $app; + $mail_config = $app->getconf->get_server_config($conf['server_id'], 'mail'); + if ( substr($mail_config['dkim_path'],strlen($mail_config['dkim_path'])-1) == '/' ) + $mail_config['dkim_path'] = substr($mail_config['dkim_path'],0,strlen($mail_config['dkim_path'])-1); + $this->remove_dkim_key($mail_config['dkim_path']."/".$_data['domain'],$_data['domain']); + $this->remove_from_amavis($_data['domain']); + } + + /* + Functions called by onLoad + */ + function domain_dkim_delete($event_name,$data) { + if (isset($data['old']['dkim']) && $data['old']['dkim'] == 'y') $this->remove_dkim($data['old']); + } + + function domain_dkim_insert($event_name,$data) { + if (isset($data['new']['dkim']) && $data['new']['dkim']=='y' && $this->check_system($data)) { + /* if the domain is already defined, remove from amavis */ + $this->remove_from_amavis($data['new']['domain']); +// $this->remove_from_amavis("dkim_key('".$data['new']['domain']."', 'default', '".$mail_config['dkim_path']."/".$data['new']['domain'].".private');\n",$data['new']['domain']); + $this->add_dkim($data); + } + } + + function domain_dkim_update($event_name,$data) { + global $app; + /* get the config */ + if (isset($data['new']['dkim']) && $data['new']['dkim']=='y') { /* DKIM enabled */ + if ($this->check_system($data)) { + /* new domain-name */ + if ($data['old']['domain'] != $data['new']['domain']) { + $this->remove_dkim($data['old']); + $this->add_dkim($data); + } + /* new key */ + if (($data['old']['dkim_private'] != $data['new']['dkim_private']) || ($data['old']['dkim'] != $data['new']['dkim'])) { + if ($data['new']['dkim_private'] != $data['old']['dkim_private']) $this->remove_dkim($data['new']); + $this->add_dkim($data); + } + /* change active (on / off) */ + if ($data['old']['active'] != $data['new']['active']) { + if ($data['new']['active'] == 'y') { + $this->add_dkim($data); + } else { + $this->remove_dkim($data['new']); + } + } + } + } + if (isset($data['new']['dkim']) && $data['old']['dkim'] != $data['new']['dkim']) + if ($this->check_system($data) && $data['new']['dkim'] == 'n') $this->remove_dkim($data['new']); + } +} +?>