From e9b92aa62f3f8557d4351f649b702fe3c4cfef4d Mon Sep 17 00:00:00 2001 From: Jesse Norell <jesse@kci.net> Date: Thu, 31 Oct 2019 15:46:25 -0600 Subject: [PATCH] bugfixes and remove/add master.cf templates to effect changes --- install/dist/lib/debian60.lib.php | 4 +- install/lib/install.lib.php | 25 +++++++--- install/lib/installer_base.lib.php | 79 +++++++++++++++++++++++++++--- 3 files changed, 92 insertions(+), 16 deletions(-) diff --git a/install/dist/lib/debian60.lib.php b/install/dist/lib/debian60.lib.php index faac52b227..a8e90f189b 100644 --- a/install/dist/lib/debian60.lib.php +++ b/install/dist/lib/debian60.lib.php @@ -164,12 +164,12 @@ class installer extends installer_base { //* dovecot-lmtpd if($configure_lmtp) { - $dovecot_protocols .= ' lmtp' + $dovecot_protocols .= ' lmtp'; } //* dovecot-managesieved if(is_file('/usr/lib/dovecot/managesieve')) { - $dovecot_protocols .= ' sieve' + $dovecot_protocols .= ' sieve'; } replaceLine($config_dir.'/'.$configfile, 'protocols = imap pop3', "protocols = $dovecot_protocols", 1, 0); diff --git a/install/lib/install.lib.php b/install/lib/install.lib.php index 02ebba2850..ea4e563a6a 100644 --- a/install/lib/install.lib.php +++ b/install/lib/install.lib.php @@ -471,29 +471,38 @@ function rf($file){ } function wf($file, $content){ - mkdirs(dirname($file)); + if(!$ret_val = mkdirs(dirname($file))) return false; if(!$fp = fopen($file, 'wb')){ ilog('WARNING: could not open file '.$file); + // implicitly returned false because the following fwrite and fclose both fail, + // but to be explicit: + $ret_val = false; } - fwrite($fp, $content); - fclose($fp); + fwrite($fp, $content) or $ret_val = false; + fclose($fp) or $ret_val = false; + return $ret_val; } function af($file, $content){ - mkdirs(dirname($file)); + if(!$ret_val = mkdirs(dirname($file))) return false; if(!$fp = fopen($file, 'ab')){ ilog('WARNING: could not open file '.$file); + $ret_val = false; } - fwrite($fp, $content); - fclose($fp); + fwrite($fp, $content) or $ret_val = false; + fclose($fp) or $ret_val = false; + return $ret_val; } function aftsl($file, $content){ + $ret_val = true; if(!$fp = fopen($file, 'ab')){ ilog('WARNING: could not open file '.$file); + $ret_val = false; } - fwrite($fp, $content); - fclose($fp); + fwrite($fp, $content) or $ret_val = false; + fclose($fp) or $ret_val = false; + return $ret_val; } function unix_nl($input){ diff --git a/install/lib/installer_base.lib.php b/install/lib/installer_base.lib.php index e0bf16c6aa..5ca16cc674 100644 --- a/install/lib/installer_base.lib.php +++ b/install/lib/installer_base.lib.php @@ -864,7 +864,7 @@ class installer_base { exec ("postconf -M $service.$type 2> /dev/null", $out, $ret); } $postfix_service = @($out[0]=='')?false:true; - } else { //* fallback - Postfix < 2.9 + } else { //* fallback - Postfix < 2.9 $content = rf($conf['postfix']['config_dir'].'/master.cf'); $regex = "/^((?!#)".$service.".*".$type.".*)$/m"; $postfix_service = @(preg_match($regex, $content))?true:false; @@ -873,6 +873,68 @@ class installer_base { return $postfix_service; } + public function remove_postfix_service( $service, $type ) { + global $conf; + + // nothing to do if the service isn't even defined. + if (! $this->get_postfix_service( $service, $type ) ) { + return true; + } + + $postfix_version = `postconf -d mail_version 2>/dev/null`; + $postfix_version = preg_replace( '/mail_version\s*=\s*(.*)\s*/', '$1', $postfix_version ); + + if ( version_compare( $postfix_version, '2.11', '>=' ) ) { + + exec("postconf -X -M $service/$type 2> /dev/null", $out, $ret); + + # reduce 3 or more newlines to 2 + $content = rf($conf['postfix']['config_dir'].'/master.cf'); + $content = preg_replace( '/(\r?\n){3,}/', '$1$1', $content ); + wf( $conf['postfix']['config_dir'].'/master.cf', $content ); + + } else { //* fallback - Postfix < 2.11 + + if ( ! $cf = fopen( $conf['postfix']['config_dir'].'/master.cf', 'r' ) ) { + return false; + } + + $out = ""; + $reading_service = false; + + while ( !feof( $cf ) ) { + $line = fgets( $cf ); + + if ( $reading_service ) { + # regex matches a new service or "empty" (whitespace) line + if ( preg_match( '/^([^\s#]+.*|\s*)$/', $line ) && + ! preg_match( '/^'.$service.'\s+'.$type.'/', $line ) ) { + $out .= $line; + $reading_service = false; + } + + # $skipped_lines .= $line; + + # regex matches definition matching service to be removed + } else if ( preg_match( '/^'.$service.'\s+'.$type.'/', $line ) ) { + + $reading_service = true; + # $skipped_lines .= $line; + + } else { + $out .= $line; + } + } + fclose( $cf ); + + $out = preg_replace( '/(\r?\n){3,}/', '$1$1', $out ); # reduce 3 or more newlines to 2 + + return wf( $conf['postfix']['config_dir'].'/master.cf', $out ); + } + + return true; + } + public function configure_postfix($options = '') { global $conf,$autoinstall; $cf = $conf['postfix']; @@ -1376,12 +1438,12 @@ class installer_base { //* dovecot-lmtpd if($configure_lmtp) { - $dovecot_protocols .= ' lmtp' + $dovecot_protocols .= ' lmtp'; } //* dovecot-managesieved if(is_file('/usr/lib/dovecot/managesieve')) { - $dovecot_protocols .= ' sieve' + $dovecot_protocols .= ' sieve'; } replaceLine($config_dir.'/'.$configfile, 'protocols = imap pop3', "protocols = $dovecot_protocols", 1, 0); @@ -1458,11 +1520,16 @@ class installer_base { $config_dir = $conf['postfix']['config_dir']; // Adding amavis-services to the master.cf file if the service does not already exists - $add_amavis = !$this->get_postfix_service('amavis','unix'); - $add_amavis_10025 = !$this->get_postfix_service('127.0.0.1:10025','inet'); - $add_amavis_10027 = !$this->get_postfix_service('127.0.0.1:10027','inet'); +// $add_amavis = !$this->get_postfix_service('amavis','unix'); +// $add_amavis_10025 = !$this->get_postfix_service('127.0.0.1:10025','inet'); +// $add_amavis_10027 = !$this->get_postfix_service('127.0.0.1:10027','inet'); //*TODO: check templates against existing postfix-services to make sure we use the template + // Or just remove the old service definitions and add them again? + $add_amavis = $this->remove_postfix_service('amavis','unix'); + $add_amavis_10025 = $this->remove_postfix_service('127.0.0.1:10025','inet'); + $add_amavis_10027 = $this->remove_postfix_service('127.0.0.1:10027','inet'); + if ($add_amavis || $add_amavis_10025 || $add_amavis_10027) { //* backup master.cf if(is_file($config_dir.'/master.cf')) copy($config_dir.'/master.cf', $config_dir.'/master.cf~'); -- GitLab