Chapter 17. Setting up E-Mail Part 1: Postfix SMTP

Next, we'll begin setting up e-mail on our internet. The first step is to set up the SMTP servers. These servers relay e-mail between each other on the internet, kind of a network within the internet that handles just e-mail.

First, clone www-projreality-sam.vdi to mail-isp-sam.vdi, and create a new VM called "mail.isp.sam" using the cloned hard disk. Put Adapter 1 on the "isp customers" internal network, and temporarily set Adapter 2 to NAT.

Start up the VM, and do the usual preliminary setup. Change the hostname to "mail.isp.sam", change eth0 to "dhcp" and add eth0 to the INTERFACES list. Also, remove or comment gateway from the ROUTES list.

Temporarily go online by issuing:

sudo dhcpcd eth1

First, perform a full upgrade:

sudo pacman -Syu

Install postfix using pacman. We will also install Courier for IMAP and squirrelmail for webmail (more on those in the following chapters). The inetutils package is for the telnet client, so we can test our setup:

sudo pacman -Sy postfix courier-imap courier-imap-mysql squirrelmail inetutils

The material on setting up Postfix was mostly taken from [ArchLinuxPostfix10]

Check for the postfix user in /etc/passwd and the postdrop and postfix groups in /etc/group (they should already be there).

Edit /etc/resolv.conf and change the nameserver to 172.16.152.10

Edit /etc/postfix/main.cf

We will keep the myhostname parameter unspecified. This will cause Postfix to use the hostname found using a reverse lookup of the mailserver's IP address.

We will also keep the mydomain parameter unspecified. The domain of the mainserver will be obtained from the hostname lookup.

Set the following parameters:

myorigin = $mydomain

mydestination = localhost

mynetworks_style = host

relay_domains = $mydestination

home_mailbox = Maildir/

Next, we will set up virtual_mail - this involves e-mail not going to a user account on the mailserver (which is generally how we do e-mail now). Add the following lines. As you can see, we will be using MySQL in conjunction with Postfix.

virtual_mailbox_domains = isp.sam
virtual_alias_maps = hash:/etc/postfix/virtual_alias, mysql:/etc/postfix/mysql_virtual_forwards.cf
virtual_mailbox_domains = mysql:/etc/postfix/mysql_virtual_domains.cf
virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailboxes.cf
virtual_mailbox_base = /home/vmailer
virtual_uid_maps = static:5003
virtual_gid_maps = static:5003
virtual_minimum_uid = 5003
virtual_mailbox_limit = 51200000

Next, edit /etc/postfix/aliases. Most of the stuff in here is fine, but uncomment the line for root and change it to sam (or whatever name you want to put in there). This way, when various system processes end up e-mailing root, you don't have to log in as root to read it.

root:		sam

Since we changed the aliases, we need to run the following command (this needs to be repeated if you make any further changes to the aliases in the future):

sudo postalias /etc/postfix/aliases

Next, copy /etc/postfix/aliases to /etc/postfix/virtual_alias, and delete the line redirecting root to sam, then add it at the end (with a slight change):

root:		sam@isp.sam

Now run the postalias command again:

sudo postalias /etc/postfix/virtual_alias

Next, create the following files:

File /etc/postfix/mysql_virtual_domains.cf

user = postfixuser
password = postfixpassword
hosts = localhost
dbname = postfix
table = domains
select_field = 'virtual'
where_field = domain

File /etc/postfix/mysql_virtual_mailboxes.cf

user = postfixuser
password = postfixpassword
hosts = localhost
dbname = postfix
table = users
select_field = concat(domain,'/',email,'/')
where_field = email

File /etc/postfix/mysql_virtual_forwards.cf

user = postfixuser
password = postfixpassword
hosts = localhost
dbname = postfix
table = forwardings
select_field = destination
where_field = source

Next, run the config file check command:

sudo postfix check

Next, edit /etc/rc.conf and add mysqld postfix httpd (in that order) to the DAEMONS list (anywhere after iptables and network is fine)

Finally, create the vmailer user which will own all of the e-mail.

sudo groupadd -g 5003 vmail

sudo useradd -g vmail -u 5003 -d /home/vmailer -s /bin/false vmailer

sudo mkdir /home/vmailer

sudo chown vmailer.vmail /home/vmailer

sudo chmod -R 750 /home/vmailer

sudo passwd vmailer

Next, we'll create the necessary databases in MySQL. First, log in to MySQL as root:

sudo mysql -u root -p

The password was set back in the Chapter 12, and it should be "Password" unless you set it to something else.

Create the postfix data:

CREATE DATABASE postfix;

Next, create the postfix user and give it privileges to the postfix database:

CREATE USER 'postfixuser'@'localhost' IDENTIFIED BY 'postfixpassword';

GRANT ALL PRIVILEGES ON postfix.* TO 'postfix'@'localhost';

Exit MySQL and log back in as the postfix user:

mysql -u postfix -p

Issue the following commands to create the Postifx-related tables:

CREATE TABLE `domains`
  (
  `domain` VARCHAR(50) NOT NULL default '',
  PRIMARY KEY (`domain`),
  UNIQUE KEY `domain` (`domain`)
  );
CREATE TABLE `forwardings`
  (
  `source` VARCHAR(80) NOT NULL default '',
  `destination` TEXT NOT NULL,
  PRIMARY KEY(`source`)
  );
CREATE TABLE `users`
  (
  `email` VARCHAR(80) NOT NULL default '',
  `password` VARCHAR(20) NOT NULL default '',
  `quota` VARCHAR(20) NOT NULL default '20971520',
  `domain` VARCHAR(255) NOT NULL default '',
  UNIQUE KEY `email` (`email`)
  );

Next, create an entry for the isp.sam domain:

INSERT INTO `domains` VALUES ('isp.sam');

Next, create an entry for the sam user (or whatever username you used as the forwarding destination for root's mail earlier:

INSERT INTO `users` VALUES ('sam@isp.sam', 'sampassword', '20971520', 'isp.sam');

Finall, reboot the VM (if you haven't already) to let all of the configuration changes in /etc/rc.conf take effect. Postfix should also start on boot.

Manually connect to the mail server, and try sending sam@isp.sam an e-mail:

telnet localhost 25

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'
220 mail.isp.sam ESMTP Postfix

ehlo isp.sam

250-mail.isp.sam
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN

mail from:<sam@isp.sam>

250 2.1.0 Ok

rcpt to:<sam@isp.sam>

250 2.1.5 Ok

data

354 End data with <CR><LF>.<CR><LF>

Testing

[empty line]

.

250 2.0.0 Ok: queued as [some ID]

quit

221 2.0.0 Bye

Now, check in /home/vmailer/isp.sam/sam@isp.sam/new - there should be a file that was just created. Look at its contents - it should match the contents of the e-mail you just sent. If so, then SMTP is working!