Wednesday, March 10, 2010

11. Postfix MailBox


Postfix supports 2 types of Mail Box formats
1. MBOX Format
2. MailDir Format

MBOX (/var/spool/mail/$user)
    MBOX is the default storage method used in the Postfix. This is also the traditional Unix format to store the msgs. This appends the mails to a single file in sequential fashion. This file needs to be locked by any application for writing into it. In a high utilized servers there may be issues of locking and performance if you are using the MBOX format. Because only one application at a time is able to read and write the file same time. By default postfix delivers mail to the file object in the spool directory. (For Eg: For user root the  MBOX file is /var/spool/mail/root). Most of the mail retrieving technologies such as imap and pop3 base servers are following this directory structure by default.
Spooling mails in same MBOX format to users home directory.
    This will results the mail delivery in users home directory. There is a MAIL variable in users shell that defines the default location of the mails for the MUA. The following command shows the mail variable.
# echo $MAIL
# set |grep -i mail
Moving the MBOX to users home
# vim  /etc/postfix/main.cf
home_mailbox = Mailbox
# postfix reload
    The default behavior of postfix is to spool the mail to the /var/spool/mail directory. By defining the home_mailbox postfix will delivers the mail to the users home directory. The file named "Mailbox" will be created by the Postfix daemon.
Now change the mail variable for the user(recommended when localy installed MUA such as mutt, mail etc used).
# export MAIL=~/Mailbox
Make it permanent (following shows for bash shell)
# vim /etc/bashrc
export MAIL=~/Mailbox
Now source the file and check the mail variable
# .   /etc/bashrc
# echo $MAIL
    Now the MUA will be able to get the mail from exact location.

Maildir
    This is newer Unix standard to route the mail to a directory struchure. Maildir provides the superior scaling as well as "no locking issues".
Implementing Maildir
# vim /etc/postfix/main.cf
home_mailbox = Maildir/
# postfix reload
     The above process will create a sub-directory in each users home directory called Maildir. Beneath this directory contains the structure that contains the msgs. Maildir is introduced by Qmail and recognized and supported by almost all the MUAs. Test sending a mail to any user in the system and trace the newly created directory inside the home.
# ls ~/Maildir
cur
new
tmp
    These are the three sub directories created by postfix. When a msg is spooled typically copied in to the "tmp" directory. "new" directory contains the unread mails. The mails containing in the directory "new"  has a typical nomenclature for the identification of the msgs.
Eg:- 2214525412.v80osui654.destinedhost.
    In the above file name the initial prefix (2214525412) is the unique identified that corresponds to the time after the epoc time 1970 (command "date +%s" shows the current epoc time). "v80osui654" is the identified added by postfix and followed by the destination host name of the mailbox."cur" (current) directory contains the read mails
The MAILDIR variable has to be set and MAIL variable has to be unset
# unset MAIL
# export MAILDIR=~/Maildir
    The variable change need to be specified globally. if we are using any MUAs depends upon this variable, else the mails wont be able to process by MUAs.
Set the variables globally
# vim /etc/bashrc
unset MAIL
export MAILDIR=~/Maildir
# . /etc/bashrc

Monday, March 8, 2010

10. Postfix Virtual Domains


    By default postfix has setup to handle few domains, defined by $mydestination. The idea of the virtual domain is to  map the multiple domains to the same server. " hostname -f " shows the FQDN also known as canonical domain used by Postfix.
Note: The IP address also considers as domain. For eg:- A message To: user@[10.0.0.1] (The "[]" is must). So in this case the IP address is also considered as the domain. This domain is also considered as a part of the canonical domain.

Basic Virtual Domain Configuration

# vim /etc/postfix/main.cf
mydestination = $myhostname, localhost, $mydomain, anewdomain.com, someotherdomain.com
relay_domains = $mydestination
# postfix reload
    Now the messages that destined to the domains listed in the $mydestination will be handled by the server. So messages send to a user at domain that defined at $mydestination will be delivered locally.
For Eg:- Mail send to kiran@anewdomain.com and kiran@someotherdomain.com will get deliver to the same user in the host. In other words the list of domains that defined in the $mydestination will be considered to be local and delivers the mail locally.

Virtual Domains Using Maps For Single Domain
    This scenario is used ideally in a Linux mail server where the local users need to share the different domains, (used in ISP environment).
Splitting Local users in to separate domains
    To do so we have to setup the virtual aliases maps
# vim /etc/postfix/main.cf
virtual_alias_domains = example.com
virtual_alias_maps = hash:/etc/postfix/virtual
     virtual_alias_domains tells what domains are needed to be supported by virtaul alias maps
# vim /etc/postfix/virtual
userdd@example.com    kiran
dduser@example.com    jam
# postmap /etc/postfix/virtual
   The format is as same as in the transport table. In the Left Hand Side we mention the address that need to be mapped and in Right Hand Side we mention the local or remote user mail address to which mail has to be delivered.
# postfix reload
Reload the postfix service

# postconf  | grep virtual_alias_
    Now test the setting by composing the msg to the user userdd@example.com and dduser@example.com. The msg will be delivered to the local user kiran and jam respectively.

Virtual Domains Using catch all features.
    In a virtual alias map environment if mail is send to a non-existing Local user in a postfix server the mail will be rejected with the error "recipient address rejected" in log file. This situation could be overcome by defining the catchall address for the domain. But these feature will be subjected to catch all the mails that coming to the domain and obviously the server will be filled up with spam mails. So it is not at all considered to be used at production environment.
Defining the catch all
# vim /etc/postfix/main.cf
virtual_alias_domains = example.com
virtual_alias_maps = hash:/etc/postfix/virtual
 # vim /etc/postfix/virtual
@example.com    kiran
# postmap /etc/postfix/virtual
    Here all the mails that comes to the domain example.com will be routed to user kiran.

The following virtual map file will send the all mails coming to the domain example.com to multiple recipients.
# vim /etc/postfix/virtual
@example.com    kiran, user1, user2, user3

The following virtual map will send the mails coming to user kiran@example.com to remote domain kiran@secureserver.com.
# vim /etc/postfix/virtual
kiran@example.com        kiran@secureserver.com
   
Virtual alias Maps For Multiple Domains
    The following example shows the configuration of the multiple domains
# vim  /etc/postfix/main.cf
virtual_alias_domains = firstdomain.com, seconddomain.net, thirddomain.org, fourthdomain.com
virtual_alias_maps = hash:/etc/postfix/virtual


# vim /etc/postfix/virtual
sales@firstdomain.com             kiran
hr@seconddomain.net               jam
finance@thirddomain.org        jeo
project@fourthdomain.com      paul
abuse@seconddomain.net        abuse

# postmap /etc/postfix/virtual
    This finishes the configuration of the Virtual alias maps
#postmap -q abuse@seconddomain.net /etc/postfix/virtual
    The above command will query the virtual map file for the mapped address.
# postfix reload
    Test the settings by sending mails to each and every users in the new domains.
While testing this configuration make sure that the proper DNS entry in place.