Implement sender rewriting scheme in an ISPConfig mailserver part 1

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

  1. remkoh

    remkoh Active Member

    This tutorial is the result of my struggles in this thread on how to relay emails to a host after they've gone through Postsrsd:
    https://forum.howtoforge.com/threads/postfix-relay-to-host-after-postsrs.89736/
    Postsrsd is used for sender rewriting on forwarded emails to retain a SPF and DKIM pass at the receiver side.

    Almost all tutorials about installing Postsrsd result in a situation where ALL emails are rewritten. Not a desirable situation.
    I found one where Postsrsd rewrites emails that have been forwarded by local mailboxes only.

    Downside is that by default it will break functionality behind ISPConfig's ability to set a relayhost per domain and entire host.
    A workaround for per domain relaying will be implemented in part 2 of this tutorial.
    Unfortunately relaying per entire host will remain broken and needs some manual action by the sysadmin to activate.
    This will also be included in part 2.

    To get started with this tutorial you need to have completed one of ISPConfig's Perfect Server tutorials.
    Make sure you have installed Rspamd for spamfiltering, DKIM signing etc. and NOT Amavis!!
    Follow this tutorial to replace Amavis with Rspamd if you haven't yet installed Rspamd:
    https://www.howtoforge.com/replacing-amavisd-with-rspamd-in-ispconfig/

    Install Postsrsd from source (https://github.com/roehling/postsrsd) or from your linux distribution repository if it includes it.
    After installation set at least SRS_DOMAIN in /etc/sysconfig/postsrsd or /etc/default/postsrsd (depending on you're linux distribution).
    Most other default settings are just fine and can be left as is.

    Implement Postsrsd in Postfix by adding these lines to main.cf
    Code:
    ...
    default_transport = smtp:127.0.0.1:10022
    recipient_canonical_maps=tcp:127.0.0.1:10002
    recipient_canonical_classes=envelope_recipient,header_recipient
    
    and these lines to master.cf:
    Code:
    ...
    cleanup-srs unix n - - - 0 cleanup
            -o syslog_name=postfix/srs
            -o sender_canonical_maps=mysql:/etc/postfix/mysql-virtual_domains_no_srs.cf,tcp:127.0.0.1:10001
            -o sender_canonical_classes=envelope_sender
    
    127.0.0.1:10022 inet n - n - - smtpd
            -o syslog_name=postfix/srs
            -o cleanup_service_name=cleanup-srs
            -o smtpd_milters=
            -o non_smtpd_milters=
            -o content_filter=smtp:
            -o smtpd_tls_security_level=none
            -o smtpd_recipient_restrictions=permit_mynetworks,reject
            -o local_recipient_maps=
            -o relay_recipient_maps=
            -o smtpd_restriction_classes=
            -o smtpd_client_restrictions=
            -o smtpd_helo_restrictions=
            -o smtpd_sender_restrictions=
            -o mynetworks=127.0.0.0/8
            -o strict_rfc821_envelopes=yes
            -o receive_override_options=no_unknown_recipient_checks,no_header_body_checks
            -o smtp_send_xforward_command=yes
            -o disable_dns_lookups=yes
    
    Create file mysql-virtual_domains_no_srs.cf in /etc/postfix
    Code:
    user = <db_user>
    password = <db_password>
    dbname = dbispconfig
    hosts = 127.0.0.1
    query = SELECT '%s' FROM mail_domain WHERE domain='%d' AND active = 'y' AND server_id = <server_id>
    
    This excludes local domains from being rewritten by Postsrsd

    Alter file mysql-virtual_outgoing_bcc.cf in /etc/postfix
    Code:
    user = <db_user>
    password = <db_password>
    dbname = dbispconfig
    hosts = 127.0.0.1
    query = SELECT sender_cc FROM (
                SELECT SUBSTRING_INDEX(sender_cc, ',', 1) AS sender_cc
                  FROM mail_user
                 WHERE email = '%s' AND disablesmtp = 'n' AND sender_cc != '' AND server_id = <server_id>
                   AND EXISTS (SELECT domain_id FROM mail_domain WHERE domain = SUBSTRING_INDEX('%s', '@', -1) AND active = 'y' AND server_id = <server_id>)
                UNION
                SELECT SUBSTRING_INDEX(u.sender_cc, ',', 1) AS sender_cc
                  FROM mail_user u, mail_forwarding f
                 WHERE f.destination REGEXP CONCAT( '((^|\\n)[[:blank:]]*,?|[[:alnum:]][[:blank:]]*,)[[:blank:]]*',
                                                    REPLACE( REPLACE(u.email, '+', '\\+'), '.', '\\.' ),
                                                    '[[:blank:]]*(,[[:blank:]]*[[:alnum:]]|,?[[:blank:]]*(\\r?\\n|$))' )
                   AND u.disablesmtp = 'n' AND u.sender_cc != '' AND u.server_id = <server_id>
                   AND f.source = '%s' AND f.allow_send_as = 'y' AND f.active = 'y' AND f.server_id = <server_id>
                   AND EXISTS (SELECT domain_id FROM mail_domain WHERE domain = SUBSTRING_INDEX('%s', '@', -1) AND active = 'y' AND server_id = <server_id>)
            ) table1 WHERE sender_cc != '' LIMIT 1
    
    Without this alteration sender_bcc_maps will stop working in Postfix after installation of Postsrsd (or similar SRS software).

    Restart
    Postfix and Postsrsd services.

    Finally make sure your SRS_DOMAIN has a proper SPF- and MX-record set in the DNS.

    This concludes the basic installation of Postsrsd and implementation in Postfix.

    But this line added in main.cf, which will send all emails to Postsrsd, has broken the functionality behind ISPConfig's ability to configure a relayhost per domain and entire host:
    Code:
    default_transport = smtp:127.0.0.1:10022
    
    Relay settings will be set in corresponding transport rules, only to be ignored because all transport rules are overruled by the default_transport rule.

    If you're not using any relay settings in ISPConfig then you're good to go!
    When you do use these settings or want to be able to use them in the future then continue to part 2!
    https://forum.howtoforge.com/thread...heme-in-an-ispconfig-mailserver-part-2.89828/
     
    Last edited: Nov 29, 2022
    till likes this.
  2. till

    till Super Moderator Staff Member ISPConfig Developer

    One important thing to mention, do not alter any config files written by making your changes update-safe, otherwise, you'll lose your modifications on the next update. Always copy the matching file template from install/tpl/ folder (in the ISPConfig tar.gz file) to the folder /usr/local/ispconfig/server/conf-custom/install/ and do the modifications in the copied template. See also: https://forum.howtoforge.com/threads/new-handling-for-custom-postfix-and-dovecot-config.86559/
     
  3. remkoh

    remkoh Active Member

    I did ran ISPConfig's update script WITH reconfiguration of the services after switching from Amavis to Rspamd and all of the lines related to the implementation of Postsrsd in main.cf and master.cf remained untouched.
    Also mysql-virtual_outgoing_bcc.cf stayed the same.
     
    Last edited: Dec 1, 2022
    till likes this.

Share This Page