Simple SMTP server for alias based forwarding

Solution 1:

  1. Use Postfix

    On ubuntu, do following

    apt-get install postfix
    

    I am doing the exact same thing with my vps email setup. check up my blog post Tiny VPS Postfix. I am copying the example below

    /etc/postfix/main.cf

    # See /usr/share/postfix/main.cf.dist for a commented, more complete version
    
    # Debian specific:  Specifying a file name will cause the first
    # line of that file to be used as the name.  The Debian default
    # is /etc/mailname.
    #myorigin = /etc/mailname
    
    smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
    biff = no
    
    # appending .domain is the MUA's job.
    append_dot_mydomain = no
    
    # Uncomment the next line to generate "delayed mail" warnings
    #delay_warning_time = 4h
    
    readme_directory = no
    
    # TLS parameters
    smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
    smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
    smtpd_use_tls=yes
    smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
    smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
    
    # See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
    # information on enabling SSL in the smtp client.
    
    myhostname = <YOUR HOSTNAME>
    alias_maps = hash:/etc/aliases
    alias_database = hash:/etc/aliases
    myorigin = /etc/mailname
    mydestination = <YOUR DOMAIN NAME>, localhost.domain, localhost
    relayhost =
    mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
    mailbox_command = procmail -a "$EXTENSION"
    mailbox_size_limit = 0
    recipient_delimiter = +
    inet_interfaces = all
    
    smtpd_recipient_restrictions =
        permit_mynetworks,
        permit_sasl_authenticated,
        reject_invalid_hostname,
        reject_non_fqdn_hostname,
        reject_non_fqdn_sender,
        reject_non_fqdn_recipient,
        reject_unknown_recipient_domain,
        reject_unlisted_recipient,
        reject_unauth_destination,
        reject_rbl_client cbl.abuseat.org,
        reject_rbl_client bl.spamcop.net,
        reject_rbl_client relays.mail-abuse.org,
        reject_rbl_client dnsbl.proxybl.org,
        reject_rbl_client truncate.gbudb.net,
        reject_rbl_client dnsbl.njabl.org,
        permit
    

    Remeber to change <YOUR HOSTNAME> and <YOUR DOMAIN NAME>

  2. Alias file

    Your /etc/aliases file should be like the following

    foo: [email protected]
    bar: [email protected]
    

    The left hand side should have no domain name, only username. The domain is control by your postfix configuration. then do following

    cd /etc
    postalias aliases
    service postfix restart
    
  3. Single host restriction

    To allow only email from a single(or a few) host, I am going to use a very lazy way to do it.

    Assuming the IP of the allowed incoming host has IP 192.168.1.100, add it to mynetworks

    mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 192.168.1.100
    

    Change smtpd_recipient_restrictions to following

    smtpd_recipient_restrictions =
        permit_mynetworks,
        reject_unlisted_recipient
    

    Postfix only (and always) accept email from host(s) listed in mynetworks. And reject everything else.

  4. DNS Configuration

    Remember to setup MX record and spf record.

Solution 2:

I use Postfix in a similar fashion to forward to another server. Using the three configuration options, but for your config there is an easier way.

  • relay_recipient_maps = ldap:/etc/postfix/ldap_relay_recipients_maps.cf
  • transport_maps = ldap:/etc/postfix/ldap_transport_maps.cf
  • relay_domains = ldap:/etc/postfix/ldap_relay_domains.cf

One of my LDAP files looks like this for querying an LDAP source on Zimbra...

server_host=ldap://[mail.domain.com]:389
server_port=389
search_base=
query_filter = (&(|(zimbraMailDeliveryAddress=%s)(zimbraMailAlias=%s)(zimbraMailCatchAllAddress=%s))(zimbraMailStatus=enabled))
result_attribute = zimbraMailDeliveryAddress,zimbraMailAlias
version = 3
ldap_cache = yes
ldap_cache_expiry = 600
ldap_cache_size = 64256
bind = yes
bind_dn = uid=[valid login],cn=[valid cn],cn=[valid cn]
bind_pw = [a valid password]
timeout = 30

However, based on what you are looking for, it would be easier to keep the information in local hash tables that can be used for this purpose. The only thing you need to remember is that when you make changes to the files, you need to re-run postmap to build the postfix friendly hash table.

  • relay_recipient_maps = hash:/etc/postfix/relay_recipients_maps
  • relay_domains = fwddomain.com
  • transport_maps = hash:/etc/postfix/transport_maps

You will need to create a text file in the above location with the following entry pairs: {[email address] OK}

[email protected] OK
[email protected] OK
[email protected] OK

Run postmap on the file above to generate the actual hash file which is then created in the same folder as relay_recipients_maps.db. Postfix will now check this file for valid recipients for delivery.

You then need to tell Postfix where to send email for this domain when it is received. Doing the same thing with /etc/postfix/transport_maps file, you can enter vaild pairs which are the domain the email is going to and the host to forward it on to.

fwddomain.com      smtp:mail.fwddomain.com

Hopefully this helps point you the right direction. There is alot more information on how to do these types of config files on the internet and others can even use DB's for lookups for these configuations options.