The FreeBSD Diary
Providing practical examples since 1998If you buy from Amazon USA, please support us by using this link.
virtual hosting with sendmail 27 November 1999
|Virtual 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.|
|I host several domains. Several people have accounts on my box. The way
it's set up now, mail sent to either firstname.lastname@example.org or email@example.com 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.
|I already had my DNS configured. So, for me, the first step was sendmail
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
/etc/mail/virtusertable - incoming mail
|Here 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
firstname.lastname@example.org terry email@example.com 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 firstname.lastname@example.org will be delivered to the local user terry. But mail sent to email@example.com will fail. This is good. However, if I have another domain on my box, say realdomain.com, mail sent to firstname.lastname@example.org 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 # email@example.com owner-majordomo-nz-freebsd-org firstname.lastname@example.org owner-majordomo-nz-freebsd-org email@example.com majordomo-nz-freebsd-org firstname.lastname@example.org 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 email@example.com is delivered locally to marc's mailbox. All other mail for this domain is rejected with an error message.
# # fakedomain.com # firstname.lastname@example.org owner-majordomo-racingsystem-com email@example.com owner-majordomo-racingsystem-com firstname.lastname@example.org 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 # email@example.com dan firstname.lastname@example.org email@example.com @other.org dan
For other.org, mail for marc is delivered locally. Mail for se is delivered to firstname.lastname@example.org. All other mail for this domain is sent to mar.
genericstable - outgoing mail
|NOTE: 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 email@example.com
The recieved message looked this like:
Date sent: Sat, 27 Nov 1999 18:25:18 +1300 (NZDT) From: Terry Paterson <firstname.lastname@example.org><email@example.com> 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 <firstname.lastname@example.org>
|This 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
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: <email@example.com> Delivered-To: firstname.lastname@example.org X-Envelope-To: email@example.com Received: (qmail 24380 invoked by alias); 27 Nov 1999 05:44:27 -0000 Delivered-To: firstname.lastname@example.org 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 email@example.com; Sat, 27 Nov 1999 18:44:25 +1300 (NZDT) Date: Sat, 27 Nov 1999 18:44:25 +1300 (NZDT) From: Terry Patterson <firstname.lastname@example.org> 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:
Other virtual sendmail references
|http://cybernut.com/guides/virtual.html#v_mail - Does not deal with .mc only .cf|