I've a postfix email server running and everything was fine... well until someone decided to impersonate other users. The current situation is:user1@mydomain.dev can send email with a faked from header saying it is user2@mydomain.dev;At the faked emails headers I can see this:From: User2 <user2@mydomain.dev>(Authenticated sender: user1@mydomain.dev)Clearly the user is using it's legit email address / password to authenticate on the server and then sets the From header to the user he wants to impersonate.I've tested this myself and it works. However, if I change the domain to some domain not on the virtual domains table the email client says the server refused the email.Is there a way I can make sure user1@mydomain.dev is only able to send emails with from header matching his email and not someone else on virtual domains table? -- Give an error like it happens when the domain is not listed.My current smtpd restrictions are:smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination, reject_unauth_pipelining, reject_unknown_sender_domain, reject_unknown_recipient_domainsmtpd_client_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_invalid_hostname, reject_rbl_client zen.spamhaus.org, reject_rbl_client bl.spamcop.net, reject_rbl_client cbl.abuseat.orgsmtpd_sender_restrictions = permit_mynetworks, reject_unknown_address, reject_unknown_sender_domain, reject_non_fqdn_senderNote: On the server there is plus addressing (ex. user1+something@ mydomain.dev will deliver emails to user1@mydomain.dev) and also a virtual alias maps. For someone posting a solution to the problem above can you make sure the user is still able to impersonate is own emails based on plus-addressing or aliases. I need then to be able to send from user1+something@mydomain.dev or alias-f1@mydomain.dev (only if they belong to them). Thank you.
Postfix: Users impersonating other users at same domain
postfix
With precious help from @tarleb (that pointed me on the right direction) I managed to come up with this solution:Add reject_sender_login_mismatch to the end of the smtpd_sender_restrictions section;Since I've virtual_mailbox_domains and virtual_alias_maps based on MySQL queries, add  smtpd_sender_login_maps = mysql:/etc/postfix/mysql-virtual_sender-login-maps.cf. This is the maps used by postfix to make sure the sender login email and from field match;Create /etc/postfix/mysql-virtual_sender-login-maps.cf with the following content:user = emailserveruserpassword = sdfjn1234nshosts = 127.0.0.1dbname = mailstackquery = SELECT * FROM (SELECT email FROM `virtual_users` WHERE email = '%s' UNION SELECT destination FROM `virtual_alias` WHERE source = '%s' ) aHow does it work?Postfix will give you the From email as %s and it excepts to receive as result of a query an address that matches the one used on the SMTP authentication.In this case we first query the virtual_users table and if nothing is returned from there (meaning there's no real user with that email) we query virtual_alias in order to get the destination address (a real user mailbox) that matches a potential email alias (our source field).If there's no match, the query returns nothing, it means that: 1) there's no such user with that email 2) there's no such alias to any user with that email -- Postfix then gives the mail client a Sender address rejected: not owned by user error.Hope this helps someone on the future with a similar setup!