The FreeBSD Diary

The FreeBSD Diary (TM)

Providing practical examples since 1998

If you buy from Amazon USA, please support us by using this link.
[ HOME | TOPICS | INDEX | WEB RESOURCES | BOOKS | CONTRIBUTE | SEARCH | FEEDBACK | FAQ | FORUMS ]
virtual hosting with sendmail 27 November 1999
Need more help on this topic? Click here
This article has 2 comments
Show me similar articles
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.
the problem
I host several domains.  Several people have accounts on my box.  The way it's set up now, mail sent to either terry@fakedomain1.org or terry@anotherfake1.edu 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 configuration
I 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 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 spaces.
terry@fakedomain1.org		terry
terry@anotherfake1.edu		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 terry@fakedomain1.org will be delivered to the local user terry.  But mail sent to terry@anotherfake1.edu will fail.  This is good.   However, if I have another domain on my box, say realdomain.com, mail sent to terry@realdomain.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
#
owner-majordomo@firstdomain.org owner-majordomo-nz-freebsd-org
majordomo-owner@firstdomain.org owner-majordomo-nz-freebsd-org
majordomo@firstdomain.org       majordomo-nz-freebsd-org
marc@firstdomain.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 marc@firstdomain.org is delivered locally to marc's mailbox.  All other mail for this domain is rejected with an error message.

#
# fakedomain.com
#
owner-majordomo@fakedomain.com  owner-majordomo-racingsystem-com
majordomo-owner@fakedomain.com  owner-majordomo-racingsystem-com
majordomo@fakedomain.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
#
marc@other.org                           dan
se@other.org                             sue@lather.org
@other.org				 dan

For other.org, mail for marc is delivered locally. Mail for se is delivered to sue@lather.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 test@anotherdomain.org

The recieved message looked this like:

Date sent:          Sat, 27 Nov 1999 18:25:18 +1300 (NZDT)
From:               Terry Paterson <chuck@ducky.nz.freebsd.org><terry@myhost.mydomain.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):

terry		terry@fakedomain1.org

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 <terry@fakedomain1.org>
envelopes
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 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:

MASQUERADE_AS(something.yourdomain.org)

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: <terry@host.mydomain.org>
Delivered-To: test@anotherdomain.org
X-Envelope-To: test@anotherdomain.org
Received: (qmail 24380 invoked by alias); 
  27 Nov 1999 05:44:27 -0000
Delivered-To: test@anotherdomain.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 test@anotherdomain.org; 
        Sat, 27 Nov 1999 18:44:25 +1300 (NZDT)
Date: Sat, 27 Nov 1999 18:44:25 +1300 (NZDT)
From: Terry Patterson <terry@fakedomain1.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.

FEATURE(masquerade_envelope)

This changes the return path to be this:

Return-Path: <terry@fakedomain1.org>
Other virtual sendmail references
http://cybernut.com/guides/virtual.html#v_mail - Does not deal with .mc only .cf

Need more help on this topic? Click here
This article has 2 comments
Show me similar articles