Implement sender rewriting scheme in an ISPConfig mailserver part 2

Discussion in 'Tips/Tricks/Mods' started by remkoh, Nov 29, 2022.

  1. remkoh

    remkoh Active Member

    Part 1 of this tutorial can be found here:
    https://forum.howtoforge.com/thread...heme-in-an-ispconfig-mailserver-part-1.89827/
    Where the basic installation of Postsrsd and implementation in Postfix is covered.

    Part 2 covers the recovery of the broken functionality behind ISPConfig's ability to configure a relayhost per domain and entire host.

    For this we need to setup a second Postfix instance.
    I've called mine "postfix-relay" in group "mta" and will be running it on port 2525.

    Create the second instance:
    Code:
    postmulti -e init
    postmulti -I postfix-relay -G mta -e create
    
    Copy /etc/postfix/main.cf to /etc/postfix-relay/main.cf

    Delete lines in /etc/postfix-relay/main.cf:
    Code:
    dovecot_destination_recipient_limit = 1
    smtpd_sasl_type = dovecot
    smtpd_restriction_classes = greylisting
    greylisting = check_policy_service inet:127.0.0.1:10023
    smtpd_milters = inet:localhost:11332
    non_smtpd_milters = $smtpd_milters
    milter_protocol = 6
    milter_mail_macros = i {mail_addr} {client_addr} {client_name} {auth_authen}
    milter_default_action = accept
    multi_instance_wrapper = ${command_directory}/postmulti -p --
    multi_instance_enable = yes
    multi_instance_directories = /etc/postfix-relay
    multi_instance_group = mta
    
    Change lines in /etc/postfix-relay/main.cf:
    Code:
    inet_interfaces = all
    inet_protocols = all
    
    to
    Code:
    inet_interfaces = loopback-only
    inet_protocols = ipv4
    
    Change in /etc/postfix-relay/master.cf
    Code:
    smtpd      inet  n       -       y       -       -       smtpd
    
    to
    Code:
    127.0.0.1:2525      inet  n       -       y       -       -       smtpd
    
    Enable and start the second instance of Postfix:
    Code:
    postmulti -i postfix-relay -e enable
    postmulti -i postfix-relay -p start
    
    I had issues with resolving domains in Postfix because not all chroot related folders where created.
    This was solved with a reboot of the server.

    Now that the second instance of Postfix is up and running we need to relay emails to it after they've gone through Postsrsd.

    Change in /etc/postfix/master.cf
    Code:
    127.0.0.1:10022 inet n - n - - smtpd
    ...
            -o content_filter=smtp:
    ...
    
    to
    Code:
    127.0.0.1:10022 inet n - n - - smtpd
    ...
            -o content_filter=smtp:127.0.0.1:2525
    ...
    
    and restart Postfix.

    Because we copied main.cf from Postfix' primary instance to the secondary instance all mysql hooks to ISPConfig settings are kept intact.
    Including relaying a domain to another host from within ISPConfig's settings!

    Only manual action left for a sysadmin is when you set a relay host for the entire host in ISPConfig.
    Find lines like these in /etc/postfix/main.cf:
    Code:
    relayhost = ...
    smtp_sasl_auth_enable = yes
    smtp_sasl_security_options = ...
    smtp_sasl_tls_security_options = ...
    smtp_sasl_password_maps = ...
    
    and copy to or replace them in /etc/postfix-relay/main.cf.

    [EDIT]
    I've automated this last manual step with a cronjob that runs a simple search&replace script every 5 minutes.
    See the comment below.

    This concludes the tutorials on how to install and implement Sender Rewriting Scheme in an ISPConfig Perfect Server.
     
    Last edited: Dec 2, 2022
    till likes this.
  2. remkoh

    remkoh Active Member

    If I'm not mistaking relayhost settings are available in ISPConfig's database.
    So when a mysql hook can be implemented in Postfix then it replaces the sysadmin manual action and all is automated directly from ISPConfig again.
    Same as sender relayhost already does.
     
  3. remkoh

    remkoh Active Member

    I found that Postfix doesn't support mysql queries in relayhost.
    So I'll create a script to check for changes in relayhost settings in Postfix' primary instance and replicate it to the second instance to restore ISPConfig's function.
     
  4. remkoh

    remkoh Active Member

    Made a quick (and probably dirty ;)) script that:
    • searches for relevant relayhost lines in Postfix' primary instance main.cf
    • searches for the same relevant relayhost lines in Postfix' secundary instance main.cf
    • compares the lines from both
    • replaces a line in Postfix' secundary instance main.cf with the line found in Postfix' primary instance if a difference between the two is found
    • reloads Postfix, only if differences were found
    A cronjob runs the script every 5 minutes.

    The relevant relayhost lines to search for are put as strings in an array after which a loop does the search, compare and replacement if necessary for every string in the array.
     

Share This Page