postfix - act as a relay but don't deliver

I would like my postfix server to act as a relay/smarthost for some clients, but would like to deliver all mail to a local (Maildir-style) mailbox instead of sending on to the intended recipient. Sounds a bit weird, I know, but it is intended for testing purposes on our test environments.

For our live environment, I would like to deliver mail normally.

My current main.cf looks like this:

myhostname = mymail.mydomain.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
mydestination = pcre:/etc/postfix/mydestinations
relayhost = 
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 192.168.5.0/24
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = 192.168.5.43, 127.0.0.1
inet_protocols = all
virtual_alias_maps = hash:/etc/postfix/virtual

default_process_limit = 100
smtpd_client_connection_count_limit = 10
smtpd_client_connection_rate_limit = 30
queue_minfree = 20971520
header_size_limit = 51200
message_size_limit = 10485760
smtpd_recipient_limit = 100

smtpd_tls_cert_file=/etc/ssl/certs/mailcert.crt 
smtpd_tls_key_file=/etc/ssl/private/mailcert.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
smtpd_tls_security_level=may
smtpd_tls_protocols = !SSLv2, !SSLv3

/etc/postfix/mydestinations:

/^mymail.mydomain.com$/          ACCEPT
/^localhost.localdomain$/           ACCEPT
/^localhost$/                       ACCEPT
/^.*\.mydomain\.com$/       ACCEPT

/etc/postfix/virtual:

@mydomain.com catchall

I've been told this is possible, but can't really find any specific info as to how to achieve it..


Solution 1:

I see two different problems with your current configuration:

  • Your domains are in mydestination while you are trying to implement your catch-all address using virtual_alias_maps, which would require the domains to be in virtual_alias_domains.

  • You only have @example.com in your virtual_alias_maps, while the regular expression ^.*\.example\.com$/ suggests you would need this catch-all for every *.example.com. In order to do that you'd need to be able to use regular expression on your virtual, too. Since this is not possible with hash:, you'd need to use pcre: here, too.

You need to modify these settings in your main.cf:

mydestination = localhost
virtual_alias_domain = pcre:/etc/postfix/mydestinations
virtual_alias_maps = pcre:/etc/postfix/virtual

And your /etc/postfix/virtual would have this regular expression:

/@((\w[\w\-]*)\.)+example\.com/ catchall

If you use pcre: make sure that your Postfix is built with the PCRE support. You also have an option to use regexp:, instead, but you won't be able to use the Perl style regular expressions. See Postfix Lookup Table Overview:

pcre (read-only)

A lookup table based on Perl Compatible Regular Expressions. The file format is described in pcre_table(5). The lookup table name as used in pcre:table is the name of the regular expression file.

regexp (read-only)

A lookup table based on regular expressions. The file format is described in regexp_table(5). The lookup table name as used in regexp:table is the name of the regular expression file.