mail_plugin_dkim.inc.php 13.5 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
		}
90
91
92
93
		//* If we can use seperate config-files with amavis use 60-dkim
		if (substr_compare($amavis_configfile, '50-user', -7) === 0)
			$amavis_configfile = str_replace('50-user', '60-dkim', $amavis_configfile);

tbrehm's avatar
tbrehm committed
94
95
96
		return $amavis_configfile;
	}

Florian Schaal's avatar
Florian Schaal committed
97
	/**
98
99
100
101
102
	 * 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
103
	function check_system($data) {
104
		global $app, $mail_config;
105

106
		$app->uses('getconf');
tbrehm's avatar
tbrehm committed
107
		$check=true;
108

tbrehm's avatar
tbrehm committed
109
		/* check for amavis-config */
110
111
112
113
114
115
116
		$amavis_configfile = $this->get_amavis_config();

		//* When we can use 60-dkim for the dkim-keys create the file if it does not exists.
		if (substr_compare($amavis_configfile, '60-dkim', -7) === 0 && !file_exists($amavis_configfile))
			file_put_contents($amavis_configfile, '');

		if ( $amavis_configfile == '' || !is_writeable($amavis_configfile) ) {
117
			$app->log('Amavis-config not found or not writeable.', LOGLEVEL_ERROR);
tbrehm's avatar
tbrehm committed
118
119
120
			$check=false;
		}
		/* dir for dkim-keys writeable? */
121
		$mail_config = $app->getconf->get_server_config($conf['server_id'], 'mail');
122
123
124
125
126
127
128
		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
129
			if (!is_writeable($mail_config['dkim_path'])) {
130
				$app->log('DKIM Path '.$mail_config['dkim_path'].' not writeable.', LOGLEVEL_ERROR);
tbrehm's avatar
tbrehm committed
131
				$check=false;
132
			}
133

tbrehm's avatar
tbrehm committed
134
		} else {
135
			$app->log('Unable to write DKIM settings; Check your config!', LOGLEVEL_ERROR);
136
137
138
139
			$check=false;
		}
		return $check;
	}
140

Florian Schaal's avatar
Florian Schaal committed
141
	/**
142
143
	 * This function restarts amavis
	 */
tbrehm's avatar
tbrehm committed
144
	function restart_amavis() {
145
		global $app, $conf;
146
147
148
149
150
151
152
153
154
155
156
157
158
		$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);
159
		foreach($output as $logline) $app->log($logline, LOGLEVEL_DEBUG);
160
161
	}

Florian Schaal's avatar
Florian Schaal committed
162
	/**
163
164
165
166
167
168
169
170
	 * 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
171
		$success=false;
172
173
		if (!file_put_contents($key_file.'.private', $key_value) === false) {
			$app->log('Saved DKIM Private-key to '.$key_file.'.private', LOGLEVEL_DEBUG);
174
175
			$success=true;
			/* now we get the DKIM Public-key */
176
			exec('cat '.escapeshellarg($key_file.'.private').'|openssl rsa -pubout', $pubkey, $result);
177
178
179
			$public_key='';
			foreach($pubkey as $values) $public_key=$public_key.$values."\n";
			/* save the DKIM Public-key in dkim-dir */
180
181
			if (!file_put_contents($key_file.'.public', $public_key) === false)
				$app->log('Saved DKIM Public to '.$key_domain.'.', LOGLEVEL_DEBUG);
182
			else $app->log('Unable to save DKIM Public to '.$key_domain.'.', LOGLEVEL_DEBUG);
183
		}
184
185
		return $success;
	}
tbrehm's avatar
tbrehm committed
186

Florian Schaal's avatar
Florian Schaal committed
187
	/**
188
189
190
191
192
	 * 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) {
193
194
195
		global $app;
		if (file_exists($key_file.'.private')) {
			exec('rm -f '.escapeshellarg($key_file.'.private'));
196
197
			$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);
198
199
		if (file_exists($key_file.'.public')) {
			exec('rm -f '.escapeshellarg($key_file.'.public'));
200
201
			$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);
202
	}
tbrehm's avatar
tbrehm committed
203

Florian Schaal's avatar
Florian Schaal committed
204
	/**
205
206
207
	 * This function adds the entry to the amavisd-config
	 * @param string $key_domain mail-domain
	 */
tbrehm's avatar
tbrehm committed
208
	function add_to_amavis($key_domain) {
209
		global $app, $mail_config;
210

211
		if (empty($selector)) $selector = 'default';
212
		$restart = false;
213
214
		$amavis_configfile = $this->get_amavis_config();

215
216
		$search_regex = "/(\n|\r)?dkim_key\(\'".$key_domain."\',\ \'(".$selector."|".$old_selector."){1}?\'.*/";

217
218
219
220
		//* If we are using seperate config-files with amavis remove existing keys from 50-user to avoid duplicate keys
		if (substr_compare($amavis_configfile, '60-dkim', -7) === 0) {
			$temp_configfile = str_replace('60-dkim', '50-user', $amavis_configfile);
			$temp_config = file_get_contents($temp_configfile);
221
222
			if (preg_match($search_regex, $temp_config)) {
				$temp_config = preg_replace($search_regex, '', $temp_config)."\n";
223
224
225
226
227
228
				file_put_contents($temp_configfile, $temp_config);
			}
			unset($temp_configfile);
			unset($temp_config);
		}

229
		$key_value="dkim_key('".$key_domain."', '".$selector."', '".$mail_config['dkim_path']."/".$key_domain.".private');\n";
230
		$amavis_config = file_get_contents($amavis_configfile);
231
		$amavis_config = preg_replace($search_regex, '', $amavis_config).$key_value;
232

233
		if (file_put_contents($amavis_configfile, $amavis_config)) {
234
235
			$app->log('Adding DKIM Private-key to amavis-config.', LOGLEVEL_DEBUG);
			$restart = true;
236
		} else {
237
			$app->log('Unable to add DKIM Private-key for '.$key_domain.' to amavis-config.', LOGLEVEL_ERROR);
tbrehm's avatar
tbrehm committed
238
		}
239
240

		return $restart;
tbrehm's avatar
tbrehm committed
241
242
	}

Florian Schaal's avatar
Florian Schaal committed
243
	/**
244
245
246
	 * This function removes the entry from the amavisd-config
	 * @param string $key_domain mail-domain
	 */
tbrehm's avatar
tbrehm committed
247
248
	function remove_from_amavis($key_domain) {
		global $app;
249
250

		$restart = false;
251
252
		$amavis_configfile = $this->get_amavis_config();
		$amavis_config = file_get_contents($amavis_configfile);
253

254
255
256
257
		$search_regex = "/(\n|\r)?dkim_key.*".$key_domain.".*(\n|\r)?/";

		if (preg_match($search_regex, $amavis_config)) {
			$amavis_config = preg_replace($search_regex, '', $amavis_config);
258
			file_put_contents($amavis_configfile, $amavis_config);
259
			$app->log('Deleted the DKIM settings from amavis-config for '.$key_domain.'.', LOGLEVEL_DEBUG);
260
261
262
			$restart = true;
		}

263
264
265
266
		//* If we are using seperate config-files with amavis remove existing keys from 50-user, too
		if (substr_compare($amavis_configfile, '60-dkim', -7) === 0) {
			$temp_configfile = str_replace('60-dkim', '50-user', $amavis_configfile);
			$temp_config = file_get_contents($temp_configfile);
267
268
			if (preg_match($search_regex, $temp_config)) {
				$temp_config = preg_replace($search_regex, '', $temp_config);
269
270
271
272
273
274
275
				file_put_contents($temp_configfile, $temp_config);
				$restart = true;
			}
			unset($temp_configfile);
			unset($temp_config);
		}

276
		return $restart;
tbrehm's avatar
tbrehm committed
277
278
	}

Florian Schaal's avatar
Florian Schaal committed
279
	/**
280
281
282
	 * This function controlls new key-files and amavisd-entries
	 * @param array $data mail-settings
	 */
tbrehm's avatar
tbrehm committed
283
284
	function add_dkim($data) {
		global $app;
Florian Schaal's avatar
Florian Schaal committed
285
		if ($data['new']['active'] == 'y') {
286
287
288
289
			$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'])) {
290
291
292
293
294
				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
295
			} else {
296
				$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
297
298
			}
		}
299
300
	}

Florian Schaal's avatar
Florian Schaal committed
301
	/**
302
303
304
305
	 * 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
306
307
	function remove_dkim($_data) {
		global $app;
308
309
310
311
		$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']);
312
313
		if ($this->remove_from_amavis($_data['domain']))
			$this->restart_amavis();
tbrehm's avatar
tbrehm committed
314
315
	}

Florian Schaal's avatar
Florian Schaal committed
316
	/**
317
318
319
320
	 * Function called by onLoad
	 * deletes dkim-keys
	 */
	function domain_dkim_delete($event_name, $data) {
321
322
		if (isset($data['old']['dkim']) && $data['old']['dkim'] == 'y' && $data['old']['active'] == 'y')
			$this->remove_dkim($data['old']);
tbrehm's avatar
tbrehm committed
323
	}
324

Florian Schaal's avatar
Florian Schaal committed
325
	/**
326
327
328
329
	 * Function called by onLoad
	 * insert dkim-keys
	 */
	function domain_dkim_insert($event_name, $data) {
330
		if (isset($data['new']['dkim']) && $data['new']['dkim']=='y' && $this->check_system($data))
331
332
			$this->add_dkim($data);
	}
tbrehm's avatar
tbrehm committed
333

Florian Schaal's avatar
Florian Schaal committed
334
	/**
335
336
337
338
	 * Function called by onLoad
	 * chang dkim-settings
	 */
	function domain_dkim_update($event_name, $data) {
tbrehm's avatar
tbrehm committed
339
		global $app;
340
341
		if ($this->check_system($data)) {
			/* maildomain disabled */
342
			if ($data['new']['active'] == 'n' && $data['old']['active'] == 'y' && $data['new']['dkim']=='y') {
343
				$app->log('Maildomain '.$data['new']['domain'].' disabled - remove DKIM-settings', LOGLEVEL_DEBUG);
344
				$this->remove_dkim($data['new']);
345
346
			}
			/* maildomain re-enabled */
347
348
			if ($data['new']['active'] == 'y' && $data['old']['active'] == 'n' && $data['new']['dkim']=='y') 
				$this->add_dkim($data);
349
350
351
352
353
354
355
356
357

			/* 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
358
359
					$this->add_dkim($data);
				}
360
361
362
363
				/* new private-key */
				if ($data['new']['dkim_private'] != $data['old']['dkim_private'] && $data['new']['dkim'] == 'y') {
					$this->add_dkim($data);
				}
364
365
366
367
				/* new selector */
				if ($data['new']['dkim_selector'] != $data['old']['dkim_selector'] && $data['new']['dkim'] == 'y') {
					$this->add_dkim($data);
				}
368
369
				/* new domain-name */
				if ($data['new']['domain'] != $data['old']['domain']) {
370
371
					$this->remove_dkim($data['old']);
					$this->add_dkim($data);
372
373
				}
			}
374
375
376
377
378

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

tbrehm's avatar
tbrehm committed
382
}
383

tbrehm's avatar
tbrehm committed
384
?>