mail_plugin_dkim.inc.php 11.7 KB
Newer Older
1 2
<?php

Florian Schaal's avatar
Florian Schaal committed
3
/**
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 Copyright (c) 2007 - 2013, Till Brehm, projektfarm Gmbh
 Copyright (c) 2013, Florian Schaal, info@schaal-24.de
 All rights reserved.

 Redistribution and use in source and binary forms, with or without modification,
 are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice,
 this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice,
 this list of conditions and the following disclaimer in the documentation
 and/or other materials provided with the distribution.
 * Neither the name of ISPConfig nor the names of its contributors
 may be used to endorse or promote products derived from this software without
 specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 @author Florian Schaal, info@schaal-24.de
 @copyrighth Florian Schaal, info@schaal-24.de
 */

tbrehm's avatar
tbrehm committed
35 36 37 38 39 40 41 42 43

class mail_plugin_dkim {

	var $plugin_name = 'mail_plugin_dkim';
	var $class_name = 'mail_plugin_dkim';

	// private variables
	var $action = '';

44 45 46 47
	/**
	 * This function is called during ispconfig installation to determine
	 * if a symlink shall be created for this plugin.
	 */
tbrehm's avatar
tbrehm committed
48 49 50 51 52 53 54 55 56 57 58
	function onInstall() {
		global $conf;

		if($conf['services']['mail'] == true) {
			return true;
		} else {
			return false;
		}

	}

Florian Schaal's avatar
Florian Schaal committed
59
	/**
60 61
	 * This function is called when the plugin is loaded
	 */
tbrehm's avatar
tbrehm committed
62
	function onLoad() {
63
		global $app, $conf;
tbrehm's avatar
tbrehm committed
64 65 66
		/*
		Register for the events
		*/
67 68 69
		$app->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');
70 71
	}

72 73 74 75
	/**
	 * This function gets the amavisd-config file
	 * @return string path to the amavisd-config for dkim-keys
	 */
76 77 78 79
	function get_amavis_config() {
		$pos_config=array(
			'/etc/amavisd.conf',
			'/etc/amavisd.conf/50-user',
80 81
			'/etc/amavis/conf.d/50-user',
			'/etc/amavisd/amavisd.conf'
82 83
		);
		$amavis_configfile='';
84
		foreach($pos_config as $conf) {
tbrehm's avatar
tbrehm committed
85 86 87 88
			if (is_file($conf)) {
				$amavis_configfile=$conf;
				break;
			}
89
		}
tbrehm's avatar
tbrehm committed
90 91 92
		return $amavis_configfile;
	}

Florian Schaal's avatar
Florian Schaal committed
93
	/**
94 95 96 97 98
	 * This function checks the relevant configs and disables dkim for the domain
	 * if the directory for dkim is not writeable or does not exist
	 * @param array $data mail-settings
	 * @return boolean - true when the amavis-config and the dkim-dir are writeable
	 */
tbrehm's avatar
tbrehm committed
99
	function check_system($data) {
100 101
		global $app, $mail_config;
		$app->uses('getconf');
tbrehm's avatar
tbrehm committed
102 103 104
		$check=true;
		/* check for amavis-config */
		if ( $this->get_amavis_config() == '' || !is_writeable($this->get_amavis_config()) ) {
105
			$app->log('Amavis-config not found or not writeable.', LOGLEVEL_ERROR);
tbrehm's avatar
tbrehm committed
106 107 108
			$check=false;
		}
		/* dir for dkim-keys writeable? */
109
		$mail_config = $app->getconf->get_server_config($conf['server_id'], 'mail');
110 111 112 113 114 115 116
		if (isset($mail_config['dkim_path']) && (!empty($mail_config['dkim_path'])) && isset($data['new']['dkim_private']) && !empty($data['new']['dkim_private'])) {

            if (!is_dir($mail_config['dkim_path'])) {
                $app->log('DKIM Path '.$mail_config['dkim_path'].' not found - (re)created.', LOGLEVEL_DEBUG);
                mkdir($mail_config['dkim_path'], 0750, true);
            }

tbrehm's avatar
tbrehm committed
117
			if (!is_writeable($mail_config['dkim_path'])) {
118
				$app->log('DKIM Path '.$mail_config['dkim_path'].' not writeable.', LOGLEVEL_ERROR);
tbrehm's avatar
tbrehm committed
119
				$check=false;
120
			}
121

tbrehm's avatar
tbrehm committed
122
		} else {
123
			$app->log('Unable to write DKIM settings; Check your config!', LOGLEVEL_ERROR);
124 125 126 127
			$check=false;
		}
		return $check;
	}
128

Florian Schaal's avatar
Florian Schaal committed
129
	/**
130 131
	 * This function restarts amavis
	 */
tbrehm's avatar
tbrehm committed
132
	function restart_amavis() {
133
		global $app, $conf;
134 135 136 137 138 139 140 141 142 143 144 145 146
		$pos_init=array(
			$conf['init_scripts'].'/amavis',
			$conf['init_scripts'].'/amavisd'
		);
		$initfile='';
		foreach($pos_init as $init) {
			if (is_executable($init)) {
				$initfile=$init;
				break;
				}
		}
		$app->log('Restarting amavis: '.$initfile.'.', LOGLEVEL_DEBUG);
		exec(escapeshellarg($initfile).' restart', $output);
147
		foreach($output as $logline) $app->log($logline, LOGLEVEL_DEBUG);
148 149
	}

Florian Schaal's avatar
Florian Schaal committed
150
	/**
151 152 153 154 155 156 157 158
	 * This function writes the keyfiles (public and private)
	 * @param string $key_file full path to the key-file
	 * @param string $key_value private-key
	 * @param string $key_domain mail-domain
	 * @return bool - true when the key is written to disk
	 */
	function write_dkim_key($key_file, $key_value, $key_domain) {
		global $app, $mailconfig;
tbrehm's avatar
tbrehm committed
159
		$success=false;
160 161
		if (!file_put_contents($key_file.'.private', $key_value) === false) {
			$app->log('Saved DKIM Private-key to '.$key_file.'.private', LOGLEVEL_DEBUG);
162 163
			$success=true;
			/* now we get the DKIM Public-key */
164
			exec('cat '.escapeshellarg($key_file.'.private').'|openssl rsa -pubout', $pubkey, $result);
165 166 167
			$public_key='';
			foreach($pubkey as $values) $public_key=$public_key.$values."\n";
			/* save the DKIM Public-key in dkim-dir */
168 169
			if (!file_put_contents($key_file.'.public', $public_key) === false)
				$app->log('Saved DKIM Public to '.$key_domain.'.', LOGLEVEL_DEBUG);
170
			else $app->log('Unable to save DKIM Public to '.$key_domain.'.', LOGLEVEL_DEBUG);
171
		}
172 173
		return $success;
	}
tbrehm's avatar
tbrehm committed
174

Florian Schaal's avatar
Florian Schaal committed
175
	/**
176 177 178 179 180
	 * This function removes the keyfiles
	 * @param string $key_file full path to the key-file
	 * @param string $key_domain mail-domain
	 */
	function remove_dkim_key($key_file, $key_domain) {
181 182 183
		global $app;
		if (file_exists($key_file.'.private')) {
			exec('rm -f '.escapeshellarg($key_file.'.private'));
184 185
			$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);
186 187
		if (file_exists($key_file.'.public')) {
			exec('rm -f '.escapeshellarg($key_file.'.public'));
188 189
			$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);
190
	}
tbrehm's avatar
tbrehm committed
191

Florian Schaal's avatar
Florian Schaal committed
192
	/**
193 194 195
	 * This function adds the entry to the amavisd-config
	 * @param string $key_domain mail-domain
	 */
tbrehm's avatar
tbrehm committed
196
	function add_to_amavis($key_domain) {
197
		global $app, $mail_config;
198 199 200

		$restart = false;
		$selector = 'default';
tbrehm's avatar
tbrehm committed
201
		$amavis_config = file_get_contents($this->get_amavis_config());
202 203 204 205 206 207
		$key_value="dkim_key('".$key_domain."', '".$selector."', '".$mail_config['dkim_path']."/".$key_domain.".private');\n";
		$amavis_config = preg_replace("/(\n|\r)?dkim_key.*".$key_domain.".*/", '', $amavis_config).$key_value;

		if (file_put_contents($this->get_amavis_config(), $amavis_config)) {
			$app->log('Adding DKIM Private-key to amavis-config.', LOGLEVEL_DEBUG);
			$restart = true;
208
		} else {
209
			$app->log('Unable to add DKIM Private-key for '.$key_domain.' to amavis-config.', LOGLEVEL_ERROR);
tbrehm's avatar
tbrehm committed
210
		}
211 212

		return $restart;
tbrehm's avatar
tbrehm committed
213 214
	}

Florian Schaal's avatar
Florian Schaal committed
215
	/**
216 217 218
	 * This function removes the entry from the amavisd-config
	 * @param string $key_domain mail-domain
	 */
tbrehm's avatar
tbrehm committed
219 220
	function remove_from_amavis($key_domain) {
		global $app;
221 222 223 224 225 226

		$restart = false;
		$amavis_config = file_get_contents($this->get_amavis_config());

		if (preg_match("/(\n|\r)?dkim_key.*".$key_domain.".*/", $amavis_config)) {
			$amavis_config = preg_replace("/(\n|\r)?dkim_key.*".$key_domain.".*(\n|\r)?/", '', $amavis_config);
tbrehm's avatar
tbrehm committed
227
			file_put_contents($this->get_amavis_config(), $amavis_config);
228
			$app->log('Deleted the DKIM settings from amavis-config for '.$key_domain.'.', LOGLEVEL_DEBUG);
229 230 231 232
			$restart = true;
		}

		return $restart;
tbrehm's avatar
tbrehm committed
233 234
	}

Florian Schaal's avatar
Florian Schaal committed
235
	/**
236 237 238
	 * This function controlls new key-files and amavisd-entries
	 * @param array $data mail-settings
	 */
tbrehm's avatar
tbrehm committed
239 240
	function add_dkim($data) {
		global $app;
Florian Schaal's avatar
Florian Schaal committed
241
		if ($data['new']['active'] == 'y') {
242 243 244 245
			$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'])) {
246 247 248 249 250
				if ($this->add_to_amavis($data['new']['domain'])) {
					$this->restart_amavis();
				} else {
					$this->remove_dkim_key($mail_config['dkim_path']."/".$data['new']['domain'], $data['new']['domain']);
				}
Florian Schaal's avatar
Florian Schaal committed
251
			} else {
252
				$app->log('Error saving the DKIM Private-key for '.$data['new']['domain'].' - DKIM is not enabled for the domain.', LOGLEVEL_ERROR);
Florian Schaal's avatar
Florian Schaal committed
253 254 255
			}
		}
		else {
256
			$app->log('DKIM for '.$data['new']['domain'].' not written to disk - domain is inactive', LOGLEVEL_DEBUG);
257 258 259
		}
	}

Florian Schaal's avatar
Florian Schaal committed
260
	/**
261 262 263 264
	 * This function controlls the removement of keyfiles (public and private)
	 * and the entry in the amavisd-config
	 * @param array $data mail-settings
	 */
tbrehm's avatar
tbrehm committed
265 266
	function remove_dkim($_data) {
		global $app;
267 268 269 270
		$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']);
271 272
		if ($this->remove_from_amavis($_data['domain']))
			$this->restart_amavis();
tbrehm's avatar
tbrehm committed
273 274
	}

Florian Schaal's avatar
Florian Schaal committed
275
	/**
276 277 278 279
	 * Function called by onLoad
	 * deletes dkim-keys
	 */
	function domain_dkim_delete($event_name, $data) {
280 281
		if (isset($data['old']['dkim']) && $data['old']['dkim'] == 'y' && $data['old']['active'] == 'y')
			$this->remove_dkim($data['old']);
tbrehm's avatar
tbrehm committed
282
	}
283

Florian Schaal's avatar
Florian Schaal committed
284
	/**
285 286 287 288
	 * Function called by onLoad
	 * insert dkim-keys
	 */
	function domain_dkim_insert($event_name, $data) {
289
		if (isset($data['new']['dkim']) && $data['new']['dkim']=='y' && $this->check_system($data))
290 291
			$this->add_dkim($data);
	}
tbrehm's avatar
tbrehm committed
292

Florian Schaal's avatar
Florian Schaal committed
293
	/**
294 295 296 297
	 * Function called by onLoad
	 * chang dkim-settings
	 */
	function domain_dkim_update($event_name, $data) {
tbrehm's avatar
tbrehm committed
298
		global $app;
299 300
		if ($this->check_system($data)) {
			/* maildomain disabled */
301
			if ($data['new']['active'] == 'n' && $data['old']['active'] == 'y' && $data['new']['dkim']=='y') {
302
				$app->log('Maildomain '.$data['new']['domain'].' disabled - remove DKIM-settings', LOGLEVEL_DEBUG);
303
				$this->remove_dkim($data['new']);
304 305
			}
			/* maildomain re-enabled */
306 307
			if ($data['new']['active'] == 'y' && $data['old']['active'] == 'n' && $data['new']['dkim']=='y') 
				$this->add_dkim($data);
308 309 310 311 312 313 314 315 316

			/* maildomain active - only dkim changes */
			if ($data['new']['active'] == 'y' && $data['old']['active'] == 'y') {
				/* dkim disabled */
				if ($data['new']['dkim'] != $data['old']['dkim'] && $data['new']['dkim'] == 'n') {
					$this->remove_dkim($data['new']);
				}
				/* dkim enabled */
				elseif ($data['new']['dkim'] != $data['old']['dkim'] && $data['new']['dkim'] == 'y') {
tbrehm's avatar
tbrehm committed
317 318
					$this->add_dkim($data);
				}
319 320 321 322 323 324
				/* new private-key */
				if ($data['new']['dkim_private'] != $data['old']['dkim_private'] && $data['new']['dkim'] == 'y') {
					$this->add_dkim($data);
				}
				/* new domain-name */
				if ($data['new']['domain'] != $data['old']['domain']) {
325 326
					$this->remove_dkim($data['old']);
					$this->add_dkim($data);
327 328
				}
			}
329 330 331 332 333

			/* resync */
			if ($data['new']['active'] == 'y' && $data['new'] == $data['old']) {
				$this->add_dkim($data);
			}
tbrehm's avatar
tbrehm committed
334 335
		}
	}
336

tbrehm's avatar
tbrehm committed
337
}
338

tbrehm's avatar
tbrehm committed
339
?>