前言
在 Linux 的 Mail Server 中,大多都是使用 Sendmail or Postfix 為主的 MTA Server,但隨著 Sendmail 功能越來越多,也造成了程式肥大且資安漏洞的問題,大多的設定較不易了解。而 Postfix 是目前最簡單設定,且資安防護比Sendmail 好很多,在 CentOS6.x 已從預設的 Sendmail 改為 Postfix。
架構與觀念
Mail Server的整個流程及概念是由幾個專有名詞而組成的
MUA (Mail User Agent):擔任 Client 端 Active 的角色,例如 Microsoft Outlook / Thunderbird 等主動發出收信及寄信的軟體。
MTA (Mail Transfer Agent):協助MUA ( Client ) 將信件寄出和接收信件訊息,例如 Sendmail、Postfix 等 Mail Server。
MDA (Mail Delivery Agent):在 MUA 收到信件後,由 MDA 決定信件該去哪裡,簡單來說就是一個過濾的功能,常見的有 procmail、Mailscanner + Spamassassion、maildrop、dovecot 這些都可以提供 MDA 的實現。
Mailbox / Maildir:就是電子郵件放置的位置。
Mailbox 是早期所使用的儲存格式,將所有電子郵件由一個檔案組成,但在效能、擴充等等能力皆受到考驗,已漸漸由 Maildir 替代。
Maildir 儲存方式為每一封郵件為一個檔案,多數功能都比 Mailbox 優,但對於超大型的 Maildir 搜尋效能將受到考驗,且有早期的 Web Mail 不支援 Maildir ( Openwebmail 2.01 before )
環境 & 目的
需要可收發的 Mail Server,並且需要 Webmail 可使用
OS & Service : CentOS 6.5 64bit、SMTP、POP3、IMAP、HTTP
Package :
postfix-2.6.6-6.el6_5.x86_64
cyrus-sasl-md5-2.1.23-13.el6_3.1.x86_64
cyrus-sasl-2.1.23-13.el6_3.1.x86_64
cyrus-sasl-plain-2.1.23-13.el6_3.1.x86_64
cyrus-sasl-lib-2.1.23-13.el6_3.1.x86_64
dovecot-2.0.9-7.el6.x86_64
openwebmail-data-2.53-3.i386
openwebmail-2.53-3.i386
設定 Postfix & sasl 驗證
Step.1 安裝套件
$ yum install postfix cyrus-sasl-md5 cyrus-sasl-plain cyrus-sasl
SETP.2 設定 postfix , main.cf
$ vim /etc/postfix/main.cf myhostname = shazi.twbbs.org \\ HostName mydomain = shazi.twbbs.org \\ Mail domain , example: eric@shazi.twbbs.org inet_interfaces = all \\ 允許 all 都可以透過 mail server 轉送郵件。 inet_protocols = ipv4 \\ 只接受使用 ipv4 協定 mydestination = $myhostname, localhost.$mydomain, localhost, mydomain \\ 能夠收信的主機名稱,如果 MX 標誌務必加入此行。
# 以下為sasl auth setting
#開啟sasl驗證
smtpd_sasl_auth_enable = yes
#client權限
smtpd_sasl_security_options = noanonymous
#允許client auth
broken_sasl_auth_clients = yes
#限制relay
smtpd_recipient_restrictions = permit_mynetworks,
permit_sasl_authenticated,
reject_unauth_destination,
permit
#限制client
smtpd_client_restrictions = permit_mynetworks,
permit_sasl_authenticated,
reject_rbl_client,
permit
Step.3 加入 smtpd.conf ,讓驗證使用 saslauthd
shell# vim /etc/sasl2/smtpd.conf pwcheck_method: saslauthd mech_list: plain login
step.4 啟動服務
shell# chkconfig saslauthd on && service saslauthd restart shell# chkconfig postfix on && service postfix restart
Step.5 建立 base64.pl 來測試 Postfix 使用 sasl 認證登入
$ vim base64.pl
#!/usr/bin/perl
use strict;
use MIME::Base64;
if ( $#ARGV != 1 ) {
die "Usage: base64.pl <username> <password> \n";
}
print encode_base64("$ARGV[0]\0$ARGV[0]\0$ARGV[1]");
exit 0;
$ chmod +x base64.pl
$ ./base64.pl eric ericpassword
3lpbmcAZHVuZ3l <= 產生md5帳號密碼
shell# telnet localhost 25
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 mail.test.org ESMTP Postfix
ehlo localhost
250-mail.test.org
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
auth plain ZXJpYwBlcmljADIyMTgwMjYy
235 2.7.0 Authentication successful <=驗證sasl可登入成功
debug
Q1: telnet localhost 25 之後 auth plain 無法驗證成功?「535 5.7.8 Error: authentication failed: authentication failure」
Ans: 請確認 smtpd.conf 是否有正確建立,並且 restart postfix & saslauthd.


您好,謝謝您的筆記,提醒您一下,第四步驟的指令有錯字喔,
shell# chkconfig saslauthd on && service saslsuthd restart 的 service 打錯了,
應該是 shell# chkconfig saslauthd on && service saslauthd restart 才對喔!
感謝你唷!! 已修正。
[…] 繼上篇CentOS6.5 安裝Mail Server (一) Postfix & 架構,要提供服務給 Client 端收發信就需要用到 dovecot 。 […]
您好,
我用的環境是AWS的EC2(AWS的免費主機拿來練習用@@)
我按照您的說明
[ec2-user@learn ~]$ sudo ./base64.pl eric ericpassword
sudo: ./base64.pl: command not found
似乎沒有辦法像您的範例這般產出
可否給的除錯的方向?
Dear Tseng Sheng Huang:
你必須先產生 base64.pl 這個 perl script 如 step5,並且提供執行權限,抱歉遺漏
$ chmod +x base64.pl
然後再執行
$ sudo ./base64.pl eric ericpassword