sudo apt-get install postfix
SMTP AUTH (使用Dovecot)
Dovecot安裝
sudo apt-get install dovecot-core
編輯檔案/etc/dovecot/conf.d/10-master,設定Dovecot接受client端提出的授權認證要求。找到「service auth {」這個段落,修改如下設定:
# Postfix smtp-auth
unix_listener /var/spool/postfix/private/auth {
mode = 0666
user = postfix
group = postfix
}
再編輯/etc/dovecot/conf.d/10-auth.conf,設定SMTP Client端與SASL Server連線時協商的機制。
auth_mechanisms = plain login
Dovecot預設禁用明碼驗證,disable_plaintext_auth = yes;但若有開啟TLS加密則不在限制內!所以得在Postfix內再加上TLS的連線設定!
設定Postfix SMTP AUTH
#SMTP AUTH
#告知Postfix使用的SASL類型是dovecot
smtpd_sasl_type = dovecot
#SASL認證的相對路徑,是在/var/spool/postfix下
smtpd_sasl_path = private/auth
#啟用SMTP AUTH
smtpd_sasl_auth_enable = yes
#在信件表頭「Received:」留下SASL登入帳號。可省略
smtpd_sasl_authenticated_header = yes
#不可使用暱名授權
smtpd_sasl_security_options = noanonymous
安裝dovecot-pop3d dovecot-imapd
sudo apt-get install dovecot-pop3d dovecot-imapd
設定Dovecot的TLS
編輯/etc/dovecot/conf.d/10-ssl.conf
ssl = yes
ssl_cert = </etc/dovecot/private/dovecot.pem
ssl_key = </etc/dovecot/private/dovecot.key
sudo apt-get install dovecot-pop3d dovecot-imapd
設定Postfix使用TLS加密
編輯/etc/postfix/main.cf,新增以下內容:
編輯/etc/postfix/main.cf,新增以下內容:
## TLS parameters
#default: empty;none:TLS will not be used;may:Opportunistic TLS;encrypt:Mandatory TLS encryption
#2.3以後版本使用,取代2.2以前的smtpd_use_tls=yes
#user client smtp to server
smtpd_tls_security_level = encrypt
#mail server smtp to mail server
smtp_tls_security_level = encrypt
#記錄遠端伺服器是否支援STARTTLS
smtp_tls_note_starttls_offer = yes
#default: empty;Name of the file containing the optional Postfix SMTP server TLS session cache.
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
#default: empty;Name of the file containing the optional Postfix SMTP client TLS session cache.
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
#憑證位置
#postfix <= 3.3
#自訂 letsencrypt
#smtpd_tls_cert_file = /etc/path/cert.pem
#smtpd_tls_key_file = /etc/path/privkey.pem
#smtp_tls_CApath = /etc/path/
#postfix >= 3.4
#letsencrypt
smtpd_tls_chain_files = /etc/path/privkey.pem,
/etc/path/fullchain.pem
#在mail的header加上SSL/TLS 相關資
smtpd_tls_received_header = yes
#客戶端/客戶發送郵件到我的服務器時,是否強制加密
smtpd_tls_auth_only = yes
#Log
smtpd_tls_loglevel = 1
設定Dovecot的TLS
編輯/etc/dovecot/conf.d/10-ssl.conf
ssl = yes
ssl_cert = </etc/dovecot/private/dovecot.pem
ssl_key = </etc/dovecot/private/dovecot.key
#要求 客户端 在连接时提供 有效的 SSL 证书,并进行身份验证
ssl_verify_client_cert = yes
#上述設定為yes時的搭配設定
ssl_ca = /etc/path/fullchain.pem
此時需注意系統是否已經自動產生
ssl_cert = </etc/dovecot/private/dovecot.pem
ssl_key = </etc/dovecot/private/dovecot.key
若無,需至/usr/share/dovecot下先編輯dovecot-openssl.cnf檔案內容,如果不想做其他變更,只需修改[ req_distinguished_name ]項目下的設定值:
[ req_distinguished_name ]
organizationName = Dovecot mail server
organizationalUnitName = @commonName@<-----單位名稱
commonName = @commonName@ #<-----此處需改成自己主機的名稱
emailAddress = @emailAddress@
countryName = TW
檔案內容設定可以參閱:
http://dovecot.org/doc/dovecot-openssl.cnf
http://www-01.ibm.com/support/knowledgecenter/SSB23S_1.1.0.12/com.ibm.ztpf-ztpfdf.doc_put.12/gtps7/cfgcert.html
編輯完成後再用管理員權限執行/usr/share/dovecot/mkcert.sh,即可產生需要的兩個檔案。
dovecot預設使用mbox格式,若有設定postfix使用maildir格式,須修改/etc/dovecot/conf.d/10-mail.conf,將信箱位置與格式改為:
#mail_location = maildir:~/Maildir
# Minimum SSL protocol version to use. Potentially recognized values are SSLv3,
# TLSv1, TLSv1.1, TLSv1.2 and TLSv1.3, depending on the OpenSSL version used.
#
# Dovecot also recognizes values ANY and LATEST. ANY matches with any protocol
# version, and LATEST matches with the latest version supported by library.
ssl_min_protocol = TLSv1.2
此時需注意系統是否已經自動產生
ssl_cert = </etc/dovecot/private/dovecot.pem
ssl_key = </etc/dovecot/private/dovecot.key
若無,需至/usr/share/dovecot下先編輯dovecot-openssl.cnf檔案內容,如果不想做其他變更,只需修改[ req_distinguished_name ]項目下的設定值:
[ req_distinguished_name ]
organizationName = Dovecot mail server
organizationalUnitName = @commonName@<-----單位名稱
commonName = @commonName@ #<-----此處需改成自己主機的名稱
emailAddress = @emailAddress@
countryName = TW
檔案內容設定可以參閱:
http://dovecot.org/doc/dovecot-openssl.cnf
http://www-01.ibm.com/support/knowledgecenter/SSB23S_1.1.0.12/com.ibm.ztpf-ztpfdf.doc_put.12/gtps7/cfgcert.html
編輯完成後再用管理員權限執行/usr/share/dovecot/mkcert.sh,即可產生需要的兩個檔案。
dovecot預設使用mbox格式,若有設定postfix使用maildir格式,須修改/etc/dovecot/conf.d/10-mail.conf,將信箱位置與格式改為:
#mail_location = maildir:~/Maildir
#指定Maildir用UTF-8編碼
mail_location = maildir:~/Maildir:UTF-8
存檔後須重新啟動dovecot。
設定dovecot開機啟動
sudo systemctl enable dovecot
啟動dovecot
sudo systemctl start dovecot
設定投遞程式
要將郵件存入~/Maildir,需明確指定『投遞程式』,由Postfix指定投遞程式將郵件存入指定目錄。
1.使用Postfix內建提供的local程式,這個方式只要在main.cf內設定好
home_mailbox = Maildir/
即可運作
2.使用procmail,除了需在main.cf內設定
home_mailbox = Maildir/
mailbox_command = procmail -a "$EXTENSION"
還需在/etc/procmailrc檔案內加入以下內容:
SHELL="/bin/bash"
SENDMAIL="/usr/sbin/sendmail -oi -t"
LOGFILE="/var/log/procmail.log"
DEFAULT="$HOME/Maildir/"
MAILDIR="$HOME/Maildir/"
:0
* ^X-Spam-Status: Yes
.spam/
另外,若系統尚未安裝procmail得再安裝
sudo apt-get install procmail
設定postscreen
編輯/etc/postfix/main.cf:
postscreen_access_list = permit_mynetworks,
cidr:/etc/postfix/postscreen_access.cidr
postscreen_access_list是設定黑白名單檔案,預設值permit_mynetworks,是指mynetworks所設定的網段不必測試,直接略過postscreen。
postscreen_access.cidr檔案內容如下:
192.168.1.1 permit
192.168.0.0/24 reject
postscreen_access.cidr檔案內容採循環評估方式,先符合者就先執行,上述規則為192.168.1.1傳回permit給postscreen,表示允許略過postscreen,而192.168.0.0/24則直接拒絕連線!
編輯/etc/postfix/master.cf,註解下列這行:
#smtp inet n - - - - smtpd
並開啟下列幾行
smtp inet n - - - 1 postscreen
smtpd pass - - - - - smtpd
dnsblog unix - - - - 0 dnsblog
tlsproxy unix - - - - 0 tlsproxy
submission inet n - - - - smtpd
-o syslog_name=postfix/submission
-o smtpd_tls_security_level=encrypt #(may)
-o smtpd_sasl_auth_enable=yes
-o smtpd_reject_unlisted_recipient=no
postscreen_access.cidr檔案內容如下:
192.168.1.1 permit
192.168.0.0/24 reject
postscreen_access.cidr檔案內容採循環評估方式,先符合者就先執行,上述規則為192.168.1.1傳回permit給postscreen,表示允許略過postscreen,而192.168.0.0/24則直接拒絕連線!
編輯/etc/postfix/master.cf,註解下列這行:
#smtp inet n - - - - smtpd
並開啟下列幾行
smtp inet n - - - 1 postscreen
smtpd pass - - - - - smtpd
dnsblog unix - - - - 0 dnsblog
tlsproxy unix - - - - 0 tlsproxy
submission inet n - - - - smtpd
-o syslog_name=postfix/submission
-o smtpd_tls_security_level=encrypt #(may)
-o smtpd_sasl_auth_enable=yes
-o smtpd_reject_unlisted_recipient=no
另外在/etc/postfix/main.cf也可加入以下兩行,啟用垃圾郵件黑名單的機制:
#阻擋mial的分數門檻
postscreen_dnsbl_threshold = 2
# dnsbl_sites*權重分數
postscreen_dnsbl_sites = zen.spamhaus.org*2
bl.spamcop.net*1 b.barracudacentral.org*1
以上述為例,阻擋的分數門檻是2,在zen.spamhaus.org發現時佔2分,在bl.spamcop.net、 b.barracudacentral.org發現時各佔1分,所以在zen.spamhaus.org發現時立即阻擋信件,但須在bl.spamcop.net、 b.barracudacentral.org同時發現,才達到阻擋郵件的2分門檻!
另外再加入以下三行,設定postscreen測試失敗時,如何回應client:
#寄件者被列在黑名單內(postscreen_access.cidr)的回應
postscreen_blacklist_action = drop
#postscreen_greet_wait (default: normal: 6s, overload: 2s),寄件者在等候時間內再次連線時的動作
postscreen_greet_action = drop
#寄件者被列在DNSBL名單內時的動作
postscreen_dnsbl_action = drop
上述三行的預設值為ignore,只會記錄和收集統計資料,不會真的拒絕連線!若設為enforce,則會回應550的拒絕訊息,並在記錄檔內留下helo/寄件者/收件者的資訊;drop則回應代碼521,並立即中斷連線!下次客戶端連接時重複此測試。
#阻擋mial的分數門檻
postscreen_dnsbl_threshold = 2
# dnsbl_sites*權重分數
postscreen_dnsbl_sites = zen.spamhaus.org*2
bl.spamcop.net*1 b.barracudacentral.org*1
以上述為例,阻擋的分數門檻是2,在zen.spamhaus.org發現時佔2分,在bl.spamcop.net、 b.barracudacentral.org發現時各佔1分,所以在zen.spamhaus.org發現時立即阻擋信件,但須在bl.spamcop.net、 b.barracudacentral.org同時發現,才達到阻擋郵件的2分門檻!
另外再加入以下三行,設定postscreen測試失敗時,如何回應client:
#寄件者被列在黑名單內(postscreen_access.cidr)的回應
postscreen_blacklist_action = drop
#postscreen_greet_wait (default: normal: 6s, overload: 2s),寄件者在等候時間內再次連線時的動作
postscreen_greet_action = drop
#寄件者被列在DNSBL名單內時的動作
postscreen_dnsbl_action = drop
上述三行的預設值為ignore,只會記錄和收集統計資料,不會真的拒絕連線!若設為enforce,則會回應550的拒絕訊息,並在記錄檔內留下helo/寄件者/收件者的資訊;drop則回應代碼521,並立即中斷連線!下次客戶端連接時重複此測試。
郵件記錄檔分析
安裝pflogsumm與mailutils套件(會用到mail指令)
sudo apt-get update
sudo apt-get update
sudo apt-get install pflogsumm mailutils
建立一個script,例如/etc/postfix/gen-pflog.sh,內容如下:
#!/bin/bash
#管理員郵件位址或本機帳號名稱
mailto=it@demo.tw
#產生報表,寄給管理員
/usr/sbin/pflogsumm -u 5 -h 5 --problems_first \
-d today /var/log/mail.log \
| mail -s "pflogsumm report $(date)" $mailto
存檔後給予執行的權限
接著用crontab排程,在指定時間執行:
59 23 * * * /etc/postfix/gen-pflog.sh &> /dev/null
建立一個script,例如/etc/postfix/gen-pflog.sh,內容如下:
#!/bin/bash
#管理員郵件位址或本機帳號名稱
mailto=it@demo.tw
#產生報表,寄給管理員
/usr/sbin/pflogsumm -u 5 -h 5 --problems_first \
-d today /var/log/mail.log \
| mail -s "pflogsumm report $(date)" $mailto
存檔後給予執行的權限
接著用crontab排程,在指定時間執行:
59 23 * * * /etc/postfix/gen-pflog.sh &> /dev/null