3. Postfix
Postfix 是一个
Wietse Venema 开发的MTA (邮件传输代理) ,做为广为应用的
Sendmail 程序的一个替代品; 它的目标是快速, 易于管理和安全。外表有明显的Sendmail风格, 但是程序内部则完全不同。Postfix还提供了极出色的
文档 以及大量的
howtos 。
我们的邮件服务器的要求很简单: 它最终的目的是要成为一个
canonical domains ,而且它仅中继来自内部网络(尽管我们也会考虑中继那些来自不可信网络的邮件,这里的“不可信”的网络是指
SMTP authentication)的邮件。 Canonical domains 包括主机名 (本例中, "mail.kernel-panic.it") 和运行Postfix主机的IP地址 (172.16.240.150) , 和主机名的父域 ("kernel-panic.it")。
Canonical domains方案通常是采用Postfix
local domain address class , 很遗憾, 对我来说其中有一个很大的缺陷: 它需要每个e-mail帐号对应一个系统内Unix帐号 。 我希望正相反:
- 保持Unix和e-mail帐号各自独立且
- 所有的mailboxes有序地保存在一个目录里。
因此, 我们将使用Postfix
Virtual Domain Hosting, 它可以在同一台服务器上托管多个Internet域名, 但我们仍旧可以实现上面所说的功能。
3.1 配置
在本章节里, 我们将将Postfix配置为独立运行(standalone), 也就是没有后端的数据库。然后在
下一章 里, 等一切就绪, 我们再把Postfix挂到一个MySQL数据库上; 那时我们就可以集中存储访问所需的 Postfix 和
Courier-IMAP 的配置信息。
这是一份需要安装的packages清单:
- mysql-client-x.x.x.tgz
- pcre-x.x.tgz
- postfix-x.x.x-mysql.tgz
注意: 如果你计划使用
SMTP authentication, 你就需要从ports编译Postfix, 因为没有同时包含MySQL和SASL支持的预编译package:
代码: 全选
# cd /usr/ports/mail/postfix/snapshot
# env FLAVOR="mysql sasl2" make install
安装将创建一个 /etc/postfix 目录, 包含了所有的配置文件。Postfix的配置文件在 /etc/postfix/main.cf ,有多达数百个配置参数, 不过,别担心: 对绝大多数参数来说, 默认配置就是最好的选择 (参
postconf(5) ,里面有全部的配置参数清单、及其描述和默认值),而且我们仅修改了其中很少的一部分:
文件 /etc/postfix/main.cf
代码: 全选
# Directory containing all the post* commands
command_directory = /usr/local/sbin
# Directory containing all the Postfix daemon programs
daemon_directory = /usr/local/libexec/postfix
# Location of the Postfix queue and root directory of chrooted Postfix daemons
queue_directory = /var/spool/postfix
# Full pathnames of various Postfix commands
sendmail_path = /usr/local/sbin/sendmail
newaliases_path = /usr/local/sbin/newaliases
mailq_path = /usr/local/sbin/mailq
# Directories containing documentation
html_directory = /usr/local/share/doc/postfix/html
manpage_directory = /usr/local/man
readme_directory = /usr/local/share/doc/postfix/readme
# The owner of the Postfix queue and of most Postfix daemon processes
mail_owner = _postfix
# The group for mail submission and queue management commands
setgid_group = _postdrop
# The myhostname parameter specifies the internet hostname of this mail system. It is
# used as default for many other configuration parameters (default = system's FQDN)
myhostname = mail.kernel-panic.it
# The internet domain name of this mail system. Used as default for many other
# configuration parameters (default = $myhostname minus the first component)
mydomain = kernel-panic.it
# The domain name that locally-posted mail appears to come from, and that locally posted
# mail is delivered to. As you can see, a parameter value may refer to other parameters
myorigin = $myhostname
# Network interface addresses that this mail system receives mail on
inet_interfaces = all
# Network interface addresses that this mail system receives mail on by way of a
# proxy or NAT unit
proxy_interfaces = router.kernel-panic.it
# List of domains that this machine considers itself the final destination for.
# Virtual domains must not be specified here
mydestination = $myhostname, localhost.$mydomain, localhost
# List of "trusted" SMTP clients allowed to relay mail through Postfix.
mynetworks = 127.0.0.0/8, 172.16.0.0/24, 172.16.240.0/24
# What destination (sub)domains this system will relay mail to
relay_domains = $mydestination
# The default host to send mail to when no entry is matched in the optional
# transport(5) table. Square brackets turn off MX lookups
relayhost = [smtp.isp.com]
# List of alias databases used by the local delivery agent
alias_maps = hash:/etc/postfix/aliases
# Alias database(s) built with "newaliases" or "sendmail -bi". This is a separate
# configuration parameter, because alias_maps may specify tables that are not
# necessarily all under control by Postfix
alias_database = hash:/etc/postfix/aliases
# SMTP greeting banner
smtpd_banner = $myhostname ESMTP $mail_name
# Postfix is final destination for the specified list of "virtual" domains
virtual_mailbox_domains = kernel-panic.it
# Virtual mailboxes base directory
virtual_mailbox_base = /var/vmail
# Optional lookup tables with all valid addresses in the domains that match
# $virtual_mailbox_domains.
virtual_mailbox_maps = hash:/etc/postfix/vmailbox
# The minimum user ID value accepted by the virtual(8) delivery agent
virtual_minimum_uid = 2000
# User ID that the virtual(8) delivery agent uses while writing to the recipient's mailbox
virtual_uid_maps = static:2000
# Group ID that the virtual(8) delivery agent uses while writing to the recipient's mailbox
virtual_gid_maps = static:2000
# Optional lookup tables that alias specific mail addresses or domains to other local or
# remote address
virtual_alias_maps = hash:/etc/postfix/virtual
我们详细说一下上面的一些配置参数。
我们原先的一个想法就是避免为每个e-mail帐号创建一个对应的Unix帐号。我们可以通过配置Postfix,让其写入mailboxes时使用 uid 2000 和and gid 2000 (请参看上面的 virtual_uid_maps 和 virtual_gid_maps 配置参数)。现在我们只需创建一个 uid 2000 和and gid 2000 的用户:
代码: 全选
# useradd -d /var/vmail -g =uid -u 2000 -s /sbin/nologin \
> -c "Virtual Mailboxes Owner" -m vmail
我们原先的另一个想法是将所有的 mailboxes 整合进一个单独的目录; 这可以通过将 virtual_mailbox_base 参数设置为某个指定的目录来实现 (在我们配置里是 /var/vmail)。实际, this parameter is a prefix that the
virtual(8) agent prepends to all pathname results from virtual_mailbox_maps table lookups.
在我们的配置里, 参数 virtual_mailbox_maps parameter 指向文件 /etc/postfix/vmailbox , 包含了虚拟域名(virtual_mailbox_domains参数)里所有有效的地址清单以及到相应的 mailboxes 或者 maildirs (每个mailbox是一个包含了所有emails的单个文件; 相反,每个
maildir 是一个具有特殊结构的目录, 里面的每份emails 都保存为单独的文件) 的路径:
文件 /etc/postfix/vmailbox
代码: 全选
info@kernel-panic.it kernel-panic.it/info/
d.mazzocchio@kernel-panic.it kernel-panic.it/d.mazzocchio/
[...]
请注意结尾处的“/”: 它们告诉 Postfix 路径名称指向一个 maildir ,而不是指向一个 mailbox 文件, 而且 maildirs 是我们唯一的选项, 因为
Courier-IMAP 不支持 mailbox 文件。
这里的 virtual_alias_maps 参数允许设置为特定的邮件地址别名或者其它的本地或者远程地址。它的值是到一个文件的路径名 (本例中 /etc/postfix/virtual) ,这个文件包含了别名的映射:
文件 /etc/postfix/virtual
代码: 全选
root@kernel-panic.it root@localhost.kernel-panic.it
postmaster@kernel-panic.it postmaster@localhost.kernel-panic.it
abuse@kernel-panic.it postmaster@localhost.kernel-panic.it
[...]
最后, 这里的 /etc/postfix/aliases 文件包含了一些地址,Postfix将使用这些地址把邮件重定向到本地接收人(看
aliases(5))。因为很多帐号指向root的email地址, 你需要经常检查root的email,或者将所有的邮件转发到另一个帐号。例如:
文件/etc/postfix/aliases
代码: 全选
root: d.mazzocchio@kernel-panic.it
MAILER-DAEMON: postmaster
postmaster: root
bin: root
[...]
现在我们只需更新Postfix的lookup tables:
代码: 全选
# /usr/local/sbin/postmap /etc/postfix/vmailbox
# /usr/local/sbin/postmap /etc/postfix/virtual
# /usr/local/sbin/newaliases
替换掉 Sendmail:
代码: 全选
# /usr/local/sbin/postfix-enable
old /etc/mailer.conf saved as /etc/mailer.conf.pre-postfix
postfix /etc/mailer.conf enabled
NOTE: do not forget to add sendmail_flags="-bd" to
/etc/rc.conf.local to startup postfix correctly.
NOTE: do not forget to add "-a /var/spool/postfix/dev/log" to
syslogd_flags in /etc/rc.conf.local and restart syslogd.
NOTE: do not forget to remove the "sendmail clientmqueue runner"
from root's crontab.
#
然后根据上述提示操作, 只要在root的crontab里,注释掉 "sendmail clientmqueue runner" 这句:
代码: 全选
# sendmail clientmqueue runner
#*/30 * * * * /usr/sbin/sendmail -L sm-msp-queue -Ac -q
然后在
/etc/rc.conf.local(8) 文件里增加一些变量。
文件 /etc/rc.conf.local
代码: 全选
# Specify a location where syslogd(8) should place an additional log socket
# for Postfix
syslogd_flags="-a /var/spool/postfix/dev/log"
# Make Postfix start in background and process queued messages every 30 min
sendmail_flags="-bd"
现在我们可以修改一些权限,然后重新启动相关进程 (或者图简单,直接reboot):
代码: 全选
# chgrp _postdrop /usr/local/sbin/postqueue /usr/local/sbin/postdrop
# chmod 2755 /usr/local/sbin/postqueue /usr/local/sbin/postdrop
# pkill syslogd
# syslogd -a /var/empty/dev/log -a /var/spool/postfix/dev/log
# pkill sendmail
# /usr/local/sbin/sendmail -bd
postfix/postfix-script: starting the Postfix mail system
然后,测试一下我们艰辛劳动的成果!
代码: 全选
# telnet mail.kernel-panic.it 25
Trying 172.16.240.150...
Connected to mail.kernel-panic.it.
Escape character is '^]'.
220 mail.kernel-panic.it ESMTP Postfix
HELO somedomain.org
250 mail.kernel-panic.it
mail from: someone@somedomain.org
250 Ok
rcpt to: d.mazzocchio@kernel-panic.it
250 Ok
data
354 End data with <CR><LF>.<CR><LF>
From: someone@somedomain.org
To: d.mazzocchio@kernel-panic.it
Subject: Test mail
It works!
.
250 Ok: queued as 548D7286
quit
221 Bye
Connection closed by foreign host.
# tail /var/log/maillog
Dec 16 15:26:35 mail postfix/smtpd[29212]: connect from ws1.lan.kernel-panic.it[172.16.0.15]
Dec 16 15:26:53 mail postfix/smtpd[29212]: 57076222: client=ws1.lan.kernel-panic.it[172.16.0.15]
Dec 16 15:27:02 mail postfix/cleanup[13428]: 57076222: message-id=<20070210142653.57076222@mail.kernel-panic.it>
Dec 16 15:27:02 mail postfix/qmgr[26776]: 57076222: from=<someone@somedomain.org>, size=392, nrcpt=1 (queue active)
Dec 16 15:27:02 mail postfix/virtual[14381]: 57076222: to=<d.mazzocchio@kernel-panic.it>, relay=virtual, delay=15,
delays=15/0.28/0/0.03, dsn=2.0.0, status=sent (delivered to maildir)
Dec 16 15:27:02 mail postfix/qmgr[26776]: 57076222: removed
Dec 16 15:27:06 mail postfix/smtpd[29212]: disconnect from ws1.lan.kernel-panic.it[172.16.0.15]
# cat /var/vmail/kernel-panic.it/d.mazzocchio/new/1118146014.V3I9448M811660.mail.kernel-panic.it
Return-Path: <someone@somedomain.org>
X-Original-To: d.mazzocchio@kernel-panic.it
Delivered-To: d.mazzocchio@kernel-panic.it
Received: from somedomain.org (ws1.lan.kernel-panic.it [172.16.0.15])
by mail.kernel-panic.it (Postfix) with SMTP id 57076222
for <d.mazzocchio@kernel-panic.it> Sat, 16 Dec 2007 15:26:47 +0100 (CET)
From: someone@somedomain.org
To: d.mazzocchio@kernel-panic.it
Subject: Test mail
Message-Id: <20070210142653.57076222@mail.kernel-panic.it>
Date: Sat, 16 Dec 2007 15:26:47 +0100 (CET)
It works!
#