Should works. This could be improved but it's ok right now. Some fixes below:
Perl
- ########################################################
- # i-MSCP Listener::Sender::Login::Maps listener file
- # Copyright (C) 2015 by Sven Jantzen
- #
- # This program is free software; you can redistribute it and/or
- # modify it under the terms of the GNU General Public License
- # as published by the Free Software Foundation; either version 2
- # of the License, or (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software
- # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- #
- #########################################################
- # Listener activate smtpd_sender_login_maps in postfix
- # for all Mail Accounts without forward
- #
- #
- # @category iMSCP
- # @package iMSCP_Listener
- # @copyright 2014 by Sven Jantzen <info@svenjantzen.de>
- # @author Sven Jantzen <info@svenjantzen.de>
- # @link http://www.i-mscp.net i-MSCP Home Site
- # @license http://www.gnu.org/licenses/gpl-2.0.html GPL v2
- #########################################################
- package Listener::Sender::Login::Maps;
- use iMSCP::EventManager;
- use iMSCP::Debug;
- use iMSCP::File;
- use iMSCP::Execute;
- use iMSCP::Database;
- use parent 'Common::SingletonClass';
- sub create_sender_login_maps
- {
- my $db = iMSCP::Database->factory();
- my $rdata = $db->doQuery(
- 'mail_addr',
- "
- SELECT
- mail_addr
- FROM
- mail_users
- WHERE
- status = 'ok' AND po_active = 'yes' AND mail_type = 'normal_mail'
- "
- );
- unless(ref $rdata eq 'HASH') {
- error($rdata);
- return 1;
- }
- # Save the result in sender_login_maps
- my $sender_login_mapsFile = "/etc/postfix/imscp/sender_login_maps";
- my $file = iMSCP::File->new( filename => $sender_login_mapsFile );
- my $mailAdress = '';
- if(%{$rdata}) {
- for(keys %{$rdata}) {
- $mailAdress .= $rdata->{$_}->{'mail_addr'};
- $mailAdress .= "\t";
- $mailAdress .= $rdata->{$_}->{'mail_addr'};
- $mailAdress .= "\n";
- }
- }
- my $rs = $file->set($mailAdress);
- return $rs if $rs;
- $rs = $file->save();
- return $rs if $rs;
- $rs = $file->mode(0640);
- return $rs if $rs;
- $rs = $file->owner('root', 'imscp');
- return $rs if $rs;
- # Create the hash file for postfix (sender_login_maps.db)
- my ($stdout, $stderr);
- $rs = execute("postmap $sender_login_mapsFile", \$stdout, \$stderr);
- debug($stdout) if $stdout;
- error($stderr) if $stderr && $rs;
- $rs;
- }
- sub main_cf_sender_login_maps
- {
- # Postfix main.cf file
- my $postfixMainCf = '/etc/postfix/main.cf';
- my $file = iMSCP::File->new( filename => $postfixMainCf );
- my $fileContent = $file->get();
- unless (defined $fileContent) {
- error("Unable to read $postfixMainCf");
- return 1;
- }
- my $replace = "smtpd_sender_login_maps = hash:/etc/postfix/imscp/sender_login_maps\n\n";
- $replace .= "smtpd_sender_restrictions = ";
- $replace .= "reject_authenticated_sender_login_mismatch,\n";
- $replace .= "\t\t\t\t";
- $fileContent =~ s/^(smtpd_sender_restrictions\s+=\s)/$replace/sgm;
- my $rs = $file->set($fileContent);
- return $rs if $rs;
- $rs = $file->save();
- return $rs if $rs;
- $rs;
- }
- # Register event listeners on the event manager
- my $eventManager = iMSCP::EventManager->getInstance();
- $eventManager->register('beforeMtaPostmap', \&create_sender_login_maps);
- $eventManager->register('afterMtaBuildConf', \&main_cf_sender_login_maps);
- 1;
- _END_