Marius Burkard
2016-04-20 4569cae57f127afd093794310ccd290d2d9fdf36
commit | author | age
2d4bd4 1 <?php
T 2
3 /*
4 Copyright (c) 2007, Till Brehm, projektfarm Gmbh
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10     * Redistributions of source code must retain the above copyright notice,
11       this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright notice,
13       this list of conditions and the following disclaimer in the documentation
14       and/or other materials provided with the distribution.
15     * Neither the name of ISPConfig nor the names of its contributors
16       may be used to endorse or promote products derived from this software without
17       specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 class postfix_server_plugin {
b1a6a5 32
2d4bd4 33     var $plugin_name = 'postfix_server_plugin';
T 34     var $class_name = 'postfix_server_plugin';
b1a6a5 35
MC 36
2d4bd4 37     var $postfix_config_dir = '/etc/postfix';
b1a6a5 38
392450 39     //* This function is called during ispconfig installation to determine
T 40     //  if a symlink shall be created for this plugin.
41     function onInstall() {
42         global $conf;
b1a6a5 43
392450 44         if($conf['services']['mail'] == true) {
T 45             return true;
46         } else {
47             return false;
48         }
b1a6a5 49
392450 50     }
b1a6a5 51
2d4bd4 52     /*
T 53          This function is called when the plugin is loaded
54     */
b1a6a5 55
2d4bd4 56     function onLoad() {
T 57         global $app;
b1a6a5 58
2d4bd4 59         /*
T 60         Register for the events
61         */
b1a6a5 62
MC 63         $app->plugins->registerEvent('server_insert', 'postfix_server_plugin', 'insert');
64         $app->plugins->registerEvent('server_update', 'postfix_server_plugin', 'update');
65
66
67
2d4bd4 68     }
b1a6a5 69
MC 70     function insert($event_name, $data) {
2d4bd4 71         global $app, $conf;
b1a6a5 72
MC 73         $this->update($event_name, $data);
74
2d4bd4 75     }
b1a6a5 76
2d4bd4 77     // The purpose of this plugin is to rewrite the main.cf file
b1a6a5 78     function update($event_name, $data) {
2d4bd4 79         global $app, $conf;
b1a6a5 80
2d4bd4 81         // get the config
d7480c 82         $app->uses("getconf,system");
cc6568 83         $old_ini_data = $app->ini_parser->parse_ini_string($data['old']['config']);
ccc7e7 84         $mail_config = $app->getconf->get_server_config($conf['server_id'], 'mail');
b1a6a5 85
MC 86         copy('/etc/postfix/main.cf', '/etc/postfix/main.cf~');
d7480c 87         
885593 88         if ($mail_config['relayhost'].$mail_config['relayhost_user'].$mail_config['relayhost_password'] != $old_ini_data['mail']['relayhost'].$old_ini_data['mail']['relayhost_user'].$old_ini_data['mail']['relayhost_password']) {
( 89             $content = file_exists('/etc/postfix/sasl_passwd') ? file_get_contents('/etc/postfix/sasl_passwd') : '';
90             $content = preg_replace('/^'.preg_quote($old_ini_data['email']['relayhost']).'\s+[^\n]*(:?\n|)/m','',$content);
91
92             if (!empty($mail_config['relayhost']) || !empty($mail_config['relayhost_user']) || !empty($mail_config['relayhost_password'])) {
93                 $content .= "\n".$mail_config['relayhost'].'   '.$mail_config['relayhost_user'].':'.$mail_config['relayhost_password'];
94             }
95             
96             if (preg_replace('/^(#[^\n]*|\s+)(:?\n+|)/m','',$content) != '') {
a59ad3 97                 exec("postconf -e 'smtp_sasl_auth_enable = yes'");
T 98             } else {
99                 exec("postconf -e 'smtp_sasl_auth_enable = no'");
100             }
885593 101             
( 102             exec("postconf -e 'relayhost = ".$mail_config['relayhost']."'");
b1a6a5 103             file_put_contents('/etc/postfix/sasl_passwd', $content);
ccc7e7 104             chmod('/etc/postfix/sasl_passwd', 0600);
J 105             chown('/etc/postfix/sasl_passwd', 'root');
106             chgrp('/etc/postfix/sasl_passwd', 'root');
885593 107             exec("postconf -e 'smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd'");
( 108             exec("postconf -e 'smtp_sasl_security_options ='");
ccc7e7 109             exec('postmap /etc/postfix/sasl_passwd');
J 110             exec($conf['init_scripts'] . '/' . 'postfix restart');
2d4bd4 111         }
a296ae 112
cc6568 113         if($mail_config['realtime_blackhole_list'] != $old_ini_data['mail']['realtime_blackhole_list']) {
f0720e 114             $rbl_updated = false;
cc6568 115             $rbl_hosts = trim(preg_replace('/\s+/', '', $mail_config['realtime_blackhole_list']));
H 116             if($rbl_hosts != ''){
117                 $rbl_hosts = explode(",", $rbl_hosts);
118             }
a296ae 119             $options = explode(", ", exec("postconf -h smtpd_recipient_restrictions"));
b8c989 120             $new_options = array();
a296ae 121             foreach ($options as $key => $value) {
M 122                 if (!preg_match('/reject_rbl_client/', $value)) {
123                     $new_options[] = $value;
f0720e 124                 } else {
FS 125                     if(is_array($rbl_hosts) && !empty($rbl_hosts) && !$rbl_updated){
126                         $rbl_updated = true;
127                         foreach ($rbl_hosts as $key => $value) {
128                             $value = trim($value);
129                             if($value != '') $new_options[] = "reject_rbl_client ".$value;
130                         }
131                     }
a296ae 132                 }
M 133             }
f0720e 134             //* first time add rbl-list
FS 135             if (!$rbl_updated && is_array($rbl_hosts) && !empty($rbl_hosts)) {
cc6568 136                 foreach ($rbl_hosts as $key => $value) {
H 137                     $value = trim($value);
138                     if($value != '') $new_options[] = "reject_rbl_client ".$value;
139                 }
a296ae 140             }
M 141             exec("postconf -e 'smtpd_recipient_restrictions = ".implode(", ", $new_options)."'");
f2c7c2 142             exec('postfix reload');
a296ae 143         }
d7480c 144         
03b633 145         if($mail_config['reject_sender_login_mismatch'] != $old_ini_data['mail']['reject_sender_login_mismatch']) {
D 146             $options = explode(", ", exec("postconf -h smtpd_sender_restrictions"));
b8c989 147             $new_options = array();
03b633 148             foreach ($options as $key => $value) {
a5c046 149                 if (!preg_match('/reject_authenticated_sender_login_mismatch/', $value)) {
03b633 150                     $new_options[] = $value;
D 151                 }
152             }
153                 
154             if ($mail_config['reject_sender_login_mismatch'] == 'y') {
155                 reset($new_options); $i = 0;
156                 // insert after check_sender_access but before permit_...
157                 while (isset($new_options[$i]) && substr($new_options[$i], 0, 19) == 'check_sender_access') ++$i;
4dc989 158                 array_splice($new_options, $i, 0, array('reject_authenticated_sender_login_mismatch'));
03b633 159             }
D 160             exec("postconf -e 'smtpd_sender_restrictions = ".implode(", ", $new_options)."'");
f2c7c2 161             exec('postfix reload');
03b633 162         }        
D 163         
52bbc8 164         if($app->system->is_installed('dovecot')) {
798c41 165             $temp = exec("postconf -n virtual_transport", $out);
52bbc8 166             if ($mail_config["mailbox_virtual_uidgid_maps"] == 'y') {
FS 167                 // If dovecot switch to lmtp
063932 168                 if($out[0] != "virtual_transport = lmtp:unix:private/dovecot-lmtp") {
52bbc8 169                     exec("postconf -e 'virtual_transport = lmtp:unix:private/dovecot-lmtp'");
FS 170                     exec('postfix reload');
171                     $app->system->replaceLine("/etc/dovecot/dovecot.conf", "protocols = imap pop3", "protocols = imap pop3 lmtp");
172                     exec($conf['init_scripts'] . '/' . 'dovecot restart');
173                 }
174             } else {
175                 // If dovecot switch to dovecot
063932 176                 if($out[0] != "virtual_transport = dovecot") {
52bbc8 177                     exec("postconf -e 'virtual_transport = dovecot'");
FS 178                     exec('postfix reload');
179                     $app->system->replaceLine("/etc/dovecot/dovecot.conf", "protocols = imap pop3 lmtp", "protocols = imap pop3");
180                     exec($conf['init_scripts'] . '/' . 'dovecot restart');
063932 181                 }
d7480c 182             }
DM 183         }
a296ae 184
52bbc8 185         exec("postconf -e 'mailbox_size_limit = ".intval($mail_config['mailbox_size_limit']*1024*1024)."'"); //TODO : no reload?
FS 186         exec("postconf -e 'message_size_limit = ".intval($mail_config['message_size_limit']*1024*1024)."'"); //TODO : no reload?
187         
b1a6a5 188
2d4bd4 189     }
T 190
191 } // end class
192
ccc7e7 193 ?>