virtual hosting with sendmailVirtual mail is the ability of a box to handle email for more than one domain. I’m already running several domains and websites on this box. Now I’m looking at how I can manage the mail in a more efficient manner.
the problemI host several domains. Several people have accounts on my box. The way it’s set up now, mail sent to either email@example.com or firstname.lastname@example.org both arrive in Eric’s mailbox. In reality, I want only one domain to work for Terry. All others should bounce.
I first searched the FreeBSD Mailing list archives to see what I could find. With a search string of "mail and virtual" I found a reference to the sendmail FAQ. DOH! That’s where I should have started in the first place. Right there on the homepage there is a mention of Virtual Hosting. It refers to http://www.sendmail.org/virtual-hosting.html which is what I used as the basic for this document. Please read this article in conjunction with that URL.
sendmail configurationI already had my DNS configured. So, for me, the first step was sendmail configuration.
Here is what I added to my .mc file. I was already using the virtusertable feature. So I didn’t have to add that. See the sendmail page for details.
# # Things for virtual domains. see # http://www.sendmail.org/virtual-hosting.html # FEATURE(genericstable, hash -o /etc/mail/genericstable)dnl GENERICS_DOMAIN_FILE(/etc/sendmail.cG)dnl
Note that the above path for genericstable differs from that supplied in the sendmail example. Then I did the following:# cd /usr/ports/mail/sendmail-8.9.3/cf/cf # m4 ../m4/cf.m4 ducky.mc > ducky.cf # cp ducky.cf /etc/sendmail.cf # killall -hup sendmail
This compiles the .mc file, copies it to /etc, then HUPs sendmail so it reads it.
/etc/mail/virtusertable – incoming mailHere are the entries I added to /etc/mail/virtusertable. Note that there is a tab between the address on the left and the text on the right. Do not use spaces.
email@example.com terry firstname.lastname@example.org error:nouser No such user here
After saving these changes, I issued the following command:
[root@ducky:/etc/mail] # makemap hash virtusertable < virtusertable
In this case, mail sent to email@example.com will be delivered to the local user terry. But mail sent to firstname.lastname@example.org will fail. This is good. However, if I have another domain on my box, say realdomain.com, mail sent to email@example.com will be delivered to terry’s mailbox. This is not good. Apart from adding an "error:nouser" line for each domain/user combination, I don’t know how to get around this. Check back later.
Well, it’s now later. Here’s what I’m doing for my incoming mail. I use /etc/mail/virtusertable and I have a section within that file for each domain. So I have something like this:
# # firstdomain.org # firstname.lastname@example.org owner-majordomo-nz-freebsd-org email@example.com owner-majordomo-nz-freebsd-org firstname.lastname@example.org majordomo-nz-freebsd-org email@example.com marc @firstdomain.org error:nouser No such user here
For firstdomain.org, the first three entires are for are majordomo (the names on the right hand side will in turn be resolved by some other aliases, but that is beyond the scope of this example). The mail for firstname.lastname@example.org is delivered locally to marc’s mailbox. All other mail for this domain is rejected with an error message.
# # fakedomain.com # email@example.com owner-majordomo-racingsystem-com firstname.lastname@example.org owner-majordomo-racingsystem-com email@example.com majordomo-racingsystem-com
For fakedomain.com, only these aliases are allowed. I’m not sure what would happen for other mail for this domain. But if it matched a local user, it would be delivered to them. It’s far better to have a wildcard alias for each domain (such as @fakedomain.com).
# # other.org # firstname.lastname@example.org dan email@example.com firstname.lastname@example.org @other.org dan
For other.org, mail for marc is delivered locally. Mail for se is delivered to email@example.com. All other mail for this domain is sent to mar.
genericstable – outgoing mailNOTE: this section applies only to messages which do not explicitly set the "From:" field. So if you are using a mail client which sets this field, sendmail will not override it using genericstable.
The genericstable file controls the "from address" on outgoing mail [if it has not already been set]. Here is an example of the mail headers for terry before I added genericstable support. I first sent a test message to someone at another domain:
echo 'test msg' | sendmail firstname.lastname@example.org
The recieved message looked this like:
Date sent: Sat, 27 Nov 1999 18:25:18 +1300 (NZDT) From: Terry Paterson <email@example.com><firstname.lastname@example.org> test msg
Well, terry isn’t part of mydomain.org, although he does have an account on ns.mydomain.org. But I don’t want him looking as if he’s part of my domain. So tere’s what I put in /etc/mail/genericstable (remember to use tabs, not spaces):
Note that the my path for genericstable differs from that supplied in the sendmail example. I put my genericstable database in /etc/mail instead of /etc. Therefore, when I make my genericstable database, I do so in the indicated directory:
[root@ducky:/etc/mail] # makemap hash genericstable < genericstable
Then I used the same test message. This time, the message arrived looking like this:
Date sent: Sat, 27 Nov 1999 18:37:23 +1300 (NZDT) From: Terry Patterson <email@example.com>
envelopesThis is from observation. It may well be wrong. Confirmation would be appreciated. Unless otherwise specified, the "From:" field contains the fully qualified host name of the box on which sendmail is running. For example, host.mydomain.org. I don’t like that. I’d rather just have the domain name.
Under normal conditions, the mail headers contain the fully qualified host name of the box on which sendmail is running. That is, the output from hostname. To change this, you can use this feature:
In which case, the "From" and "Send reply to:" fields of outgoing mail will appear to be from something.yourdomain.org.
The above test message looks pretty good. But closer inspection reveals something else. If you look at the mail headers for the last test message, it will look like this:
Return-Path: <firstname.lastname@example.org> Delivered-To: email@example.com X-Envelope-To: firstname.lastname@example.org Received: (qmail 24380 invoked by alias); 27 Nov 1999 05:44:27 -0000 Delivered-To: email@example.com Received: (qmail 24375 invoked by alias); 27 Nov 1999 05:44:27 -0000 Received: from host.mydomain.org (my.ip.add.ress) by renee.anotherdomain.org with SMTP; 27 Nov 1999 05:44:27 -0000 Received: (from chuck@localhost) by host.mydomain.org (8.9.3/8.9.3) id SAA84718 for firstname.lastname@example.org; Sat, 27 Nov 1999 18:44:25 +1300 (NZDT) Date: Sat, 27 Nov 1999 18:44:25 +1300 (NZDT) From: Terry Patterson <email@example.com> Message-Id: <199911270544.SAA84718@host.mydomain.org> X-PMFLAGS: 33554560 0 1 P5C750.CNM test msg
So I added the following line to my .mc file and recompiled it. Don’t forget to HUP sendmail.
This changes the return path to be this: