I'm setting up a postfix cloud server and I'd like to restrict communication inside the domain. That is, users can only communicate with other users who own an address in the email domain - no incoming or outgoing messages to other domains (like Gmail, Hotmail, etc):
YES: joe@domain.com <----> jane@domain.com
NO: joe@domain.com <----> jane@gmail.com
What's a simple way to do this? I'm using postfix/courier. Thanks.
UPDATE - how to do this:
In /etc/postfix/main.cf
:
# first rule makes sure users cannot sent to people outside the domain
# (check_recipient_access is the one you want)
smtpd_recipient_restrictions =
check_recipient_access regexp:/etc/postfix/recipient-access,
permit_sasl_authenticated,
permit_mynetworks,
reject_unauth_destination,
permit
# block sends from external users
# (who cannot be authenticated by the system)
smtpd_sender_restrictions =
permit_sasl_authenticated,
permit_mynetworks,
reject_authenticated_sender_login_mismatch,
reject
# use mysql to find authenticated addresses
smtpd_sender_login_maps = mysql:/etc/postfix/mysql-sender-login-maps.cf
# (could also use pcre or some other method)
#smtpd_sender_login_maps = pcre:/etc/postfix/sender-login-maps.pcre
In /etc/postfix/mysql-sender-login-maps.cf
:
user = dbuser
password = dbpassword
hosts = 127.0.0.1
dbname = dbname
# this will depend on your db/table structure
query = SELECT email FROM users WHERE email='%s' and enabled=1;
Test with:
$ postmap -q user@domain.com mysql:/etc/postfix/mysql-sender-login-maps.cf
Should return the user@domain.com
if it exists in the users table, or nothing if it isn't.
If you decided to use pcre (apt-get install postfix-pcre
in Ubuntu), then in /etc/postfix/sender-login-maps.pcre
:
/^(.*@domain.com)$/ ${1}
Test with:
$ postmap -q user@domain.com pcre:/etc/postfix/sender-login-maps.pcre
Should return the user@domain.com
if the domain matches, or nothing if it doesn't.
Finally, in /etc/postfix/recipient-access
:
!/@domain.com/ REJECT
Thanks @NickW !