2008年12月2日 星期二

Setup Postfix on Debian

為了幫魁老大的Mail Server搬新家,爬了許多文,Postfix是一套相容於Sendmail的郵件伺服器,怎知一開始卻怎麼裝都失敗,後來一忙也就忘了這檔事了,前幾天又被魁老大追殺了,今日總算是搞定了。

Install:
# apt-get install postfix libsasl2-2 libsasl2-modules sasl2-bin dovecot-common dovecot-imapd dovecot-pop3d

Configure:
postfix 的主要設定檔為 /etc/postfix/main.cf,以下列出常用設定值。
# 主機名稱,必須與 DNS 的 MX 主機名稱對應
myhostname = ms.xxx-yyy.com.tw
# 網域名稱
mydomain = xxx-yyy.com.tw
# 接受 relay 的網域
mydestination = $myhostname, $mydomain, localhost

###<下面是從別人的文件看來的,留作參考,我是沒去設>
# 接受 relay 的網域主機
relay_domain = ms.xxx-yyy.com.tw
# 若要 smarthost 時,relay 的主機,一般設定成 isp 的 mail 主機即可。
# 如不設定似乎在寄給某些 mail server 會有編號 550-5.7.1 的問題而無法寄出,如 gmail。
# 下面 ip 為 hinet ms23.hinet.net 主機
relayhost = 168.95.4.23
# 內部網路
mynetworks = 127.0.0.0/8
# 設定信箱存放目錄
mail_spool_directory = /var/mail
# 信箱大小限制,單位 bytes,0 為不限。
mailbox_size_limit = 0
# 單封信件大小限制,單位 bytes
message_size_limit = 10240000
備註:若無設定 DNS MX 紀錄的對應,則無法正常 relay 到某些遠端主機。
###

接著修改 postfix 設定檔 /etc/postfix/main.cf,啟用 SMTP 及 SASL 認證機制。
# 設定 Postfix 使用 SASL 認証。
smtpd_sasl_auth_enable = yes
# 設定 SASL 支援非標準 E-mail Client 的認証動作。
broken_sasl_auth_clients = yes
# 不使用 ANONYMOUS 這個認証。
smtpd_sasl_security_options = noanonymous
# 設定 SASL 的認証方法
# permit_sasl_authenticated 允許通過 SASL 認証者可 relay
# permit_mynetworks mynetworks 內使用者可 relay
# check_relay_domains 檢查可進行 ralay 的 domains
# reject_unauth_destination 未通過認證不予接收
smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, check_relay_domains, reject_unauth_destination
# 限制未經信任的 client 登入就啟動 sasl 認證
smtpd_client_restrictions = permit_sasl_authenticated


設定 SASL 認證支援
SASL 為符合 RFC 2222 標準的認証機制,此作用可防止任意使用者登入使用郵件伺服器亂發郵件。目前幾乎大部分的 MTA Server 內定都關掉 OpenRelay 功能,改用 SMTP 認證來驗證使用者是否可進行 mail relay。

設定 SASL
修改 /etc/default/saslauthd 檔,設定 saslauthd 使用的認證方式與開機自動啟動。
# 讓 saslauthd 一開機就啟動
# This needs to be uncommented before saslauthd will be run automatically
START=yes
# 讓 saslauthd 採用 linux 的系統帳號密碼檔作驗證
# Which authentication mechanisms should saslauthd use? (default: pam)
#
# Available options in this Debian package:
# getpwent -- use the getpwent() library function
# kerberos5 -- use Kerberos 5
# pam -- use PAM
# rimap -- use a remote IMAP server
# shadow -- use the local shadow password file
# sasldb -- use the local sasldb database file
# ldap -- use LDAP (configuration is in /etc/saslauthd.conf)
#
# Only one option may be used at a time. See the saslauthd man page
# for more information.
#
# Example: MECHANISMS="pam"
MECHANISMS="pam"

# 讓 saslauthd 支援 postfix 預設安裝的 chroot 模式
# Other options (default: -c)
# See the saslauthd man page for information about these options.
#
# Example for postfix users: "-c -m /var/spool/postfix/var/run/saslauthd"
# Note: See /usr/share/doc/sasl2-bin/README.Debian
#OPTIONS="-c -m /var/spool/postfix/var/run/saslauthd"
OPTIONS="-c -m /var/run/saslauthd"

最後使用 dpkg-statoverride 來建立 saslauthd 的執行時期目錄
host:~# dpkg-statoverride --add root sasl 710 /var/run/saslauthd


設定 Postfix 使用 SASL
編輯 /etc/postfix/sasl/smtpd.conf 讓 postfix 使用 saslauthd 來認證。
# log 檔的紀錄等級
log_level: 3
# 設定密碼檢驗使用哪種服務
pwcheck_method: saslauthd
# 設定所使用的認證機制
mech_list: PLAIN LOGIN

接著把 postfix 加入到 sasl 的群組中,才能使用 saslauthd 來認證。
$sudo addgroup postfix sasl


重新啟動服務
做好上述設定後,最後重新啟動各項服務或是重開機以使設定生效。
host:~# /etc/init.d/saslauthd restart
host:~# /etc/init.d/postfix restart

最後測試
在重新啟動服務後,可用以下方式來測試 saslauthd 服務是否正常運作。
host:~# testsaslauthd -u 使用者名稱 -p 密碼
回傳為「0: OK “Success.“」代表正常,反之則檢查 saslauthd 服務是否啟動;
接著登入 postfix 測試 sasl 是否運作。
host:~# perl -MMIME::Base64 -e 'print encode_base64("\0使用者帳號\0密碼");'
執行之後會產生一組 base64 編碼,稍後會用到;接著 telnet postfix。
host:~# telnet 127.0.0.1 25
出現下面訊息:
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
220 aaa.bbb.ccc.ddd ESMTP Postfix (Debian/GNU)
輸入以下指令:
ehlo ms ==> ms 為前面設定的hostname
出現下面訊息:
250-ms.xxx-yyy.com.tw
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
輸入以下指令:
auth plain xxxxxxxxxxxxxxxxxx <-剛剛用 perl 算出的 base64 值
若出現「235 2.0.0 Authentication successful」代表 sasl 認證成功,
反之如果驗證失敗則檢查設定步驟是否有誤。

設定 dovecot
dovecot 設定檔為 /etc/dovecot/dovecot.conf
# 設定支援哪些協定,在此為 IMAP 與 POP3 兩個
# Protocols we want to be serving: imap imaps pop3 pop3s
# If you only want to use dovecot-auth, you can set this to "none".
#protocols = imap imaps
protocols = imap pop3
# 設定監聽的 ip 與 port,可個別指定 imap 與 pop3 的監聽介面
# If you want to specify ports for each service, you will need to configure
# these settings inside the protocol imap/pop3 { ... } section, so you can
# specify different ports for IMAP/POP3. For example:
# protocol imap {
# listen = *:10143
# ssl_listen = *:10943
# ..
# }
# protocol pop3 {
# listen = *:10100
# ..
# }
listen = *
# 設定啟動認證
# Disable LOGIN command and all other plaintext authentications unless
# SSL/TLS is used (LOGINDISABLED capability). Note that if the remote IP
# matches the local IP (ie. you're connecting from the same computer), the
# connection is considered secure and plaintext authentication is allowed.
disable_plaintext_auth = no
# 認證機制
# Space separated list of wanted authentication mechanisms:
# plain login digest-md5 cram-md5 ntlm rpa apop anonymous gssapi
# NOTE: See also disable_plaintext_auth setting.
mechanisms = plain login

上述設定及完成Debian Mail Server的架設,Client端的帳號設定部份要記得在外寄伺服器要勾選驗證,POP3要選擇安全性連線(SSL)。

沒有留言: