====== 打造安全的 SMTP Server - Postfix + SASL + TLS + SpamAssassin + DNSBL ====== FreeBSD 轉載自: http://blog.bojack.idv.tw/index.php/archives/389 ----------- 用 Postfix 用蠻久了,這次將過去的筆記都整理上來,以備不時之需。目前的環境是 FreeBSD 6.2 PRELEASE,要將本來的 Sendmail 換成 Postfix ,並且讓它支援身份驗證、加密傳輸與垃圾郵件過濾的功能。 1. 安裝 Postfix 主程式 首先我們要安裝 Postfix ,先更新 ports 後,直接切換到目錄後安裝 # cd /usr/ports/mail/postfix # make install clean 在 Postfix configureation options 的選項我是勾選 SASL、TLS、OPENLDAP 這三個選項。SASL 是用來做身份驗證,TLS 是支援加密傳輸,而 OPENLDAP 是未來要試著將它們二者結合在一起,未來完成後會將筆記寫上來。選好後就按 OK 吧,而在安裝 Cyrus-sasl 時,只要勾選 pwcheck 就行了,然後再繼續往下跑,安裝到後面,會出現一行 Would you like to activate Postfix in /etc/mail/mailer.conf [n]? 在這邊我是選 y 然後按 Enter 下去,大致上 Postfix 安裝就到此結束了。接下來我們要設定一下,開機後是使用 Postfix「>Postfix 來當我們主要的 MTA,必須到 /etc/rc.conf 加入下面幾行。 sendmail_enable="YES" sendmail_flags="-bd" sendmail_outbound_enable="NO" sendmail_submit_enable="NO" sendmail_msp_queue_enable="NO" 然後再去 /etc 目錄下新增一個 periodic.conf 檔案,加入下面四行設定並存檔 daily_clean_hoststat_enable="NO" daily_status_mail_rejects_enable="NO" daily_status_include_submit_mailq="NO" daily_submit_queuerun="NO" 2. 設定 pwcheck 完成之後,我們要來設定 pwcheck 了。我們要讓 Postfix「>Postfix 的 smtpd 可以 access 到 /var/pwcheck/pwcheck,必須修改/etc/group,讓 postfix 這個 user 成為 cyrus 這個 group 的一員。 用你習慣用的 editor 修改 /etc/group 這個檔案,將 cryus 最後面以逗號為分隔,增加 postfix 這個 user,像這樣: cyrus:*:60:postfix 3. 設定 cryus-sasl 新增 /usr/local/lib/sasl/smtpd.conf 這個檔案,放入下面這行: pwcheck_method: pwcheck 4. 設定 postfix 裡面 SASL 與 TLS 的參數 這些是我慣用的參數,請視情況調整自己該用的參數喔! # # Basic Config # myhostname = dns.bojack.idv.tw mydomain = bojack.idv.tw mydestination = $myhostname, localhost.$mydomain $mydomain relay_domains = $mydestination alias_maps = hash:/usr/local/etc/postfix/aliases message_size_limit = 250000000 mailbox_size_limit = 400000000 # # SASL Config & DNSBL Config # smtpd_recipient_restrictions = permit_sasl_autheNticated,permit_mynetworks,reject_unauth_de stination,reject_rbl_client dialup.ecenter.idv.tw, reject_rbl_client relays.ordb.org,reject _rbl_client spam.ecenter.idv.tw, reject_rbl_client or.ecenter.idv.tw smtpd_client_restrictions = permit_sasl_authenticated smtpd_sasl_auth_enable= yes broken_sasl_auth_clients = yes smtpd_sasl_security_options= noanonymous # # TLS Config # smtp_use_tls = yes smtpd_use_tls = yes smtp_tls_note_starttls_offer = yes smtpd_tls_key_file = /path/to/your/key smtpd_tls_cert_file = /path/to/your/crt smtpd_tls_CAfile = /path/to/your/ca smtpd_tls_loglevel = 1 smtpd_tls_received_header = yes smtpd_tls_session_cache_timeout = 3600s tls_random_source = dev:/dev/urandom 5. 安裝 SpamAssassin # cd /usr/ports/mail/p5-Mail-SpamAssassin/ # make install clean 接下來去設定 SpamAssassin 的設定檔,把它放在 /usr/local/etc/mail/spamassassin 裡面並且命名為 local.cf 即可,參考下面的文字。 #多少分才判為 SPAM required_hits 6 #若為 SPAM,是否設修改主旨\ rewrite_subject 1 #修改主旨為 (當 rewrite_subject 是 1 的時候才會有用) rewrite_header Subject ***SPAM*** # Encapsulate spam in an attachment report_safe 1 # Use terse version of the spam report use_terse_report 1 # Enable the Bayes system use_bayes 1 # Enable Bayes auto-learning auto_learn 1 # Enable or disable network checks skip_rbl_checks 0 use_razor2 1 use_dcc 1 use_pyzor 1 # Mail using languages used in these country codes will not be marked # as being possibly spam in a foreign language. # - chinese english japanese ok_languages zh en # Mail using locales used in these country codes will not be marked # as being possibly spam in a foreign language. ok_locales zh en # Whitelist whitelist_from *@yahoo.com.tw *@gmail.com *@msn.com *@hotmail.com score HEADER_8BITS 0 score HTML_COMMENT_8BITS 0 score SUBJ_FULL_OF_8BITS 0 score UPPERCASE_25_50 0 score UPPERCASE_50_75 0 score UPPERCASE_75_100 0 score HEAD_ILLEGAL_CHARS 0 score SUBJ_ILLEGAL_CHARS 0 然後我們要來新增一個 spamfilter 使用者,主要是要讓 spamd 來使用,把這個帳號設為不能登入,再來我們要產生一個 filter.sh 去處理,也是一樣參考下面這些資訊,並且放在 /usr/local/sbin 裡面。 #!/bin/sh exec /usr/local/bin/spamc -d 127.0.0.1 -f -p 783 -t 30 -e /usr/sbin/sendmail -i "$@" exit $? 記得要把它的權限設定為 755 chmod 755 /usr/local/sbin/filter.sh 6. 設定 master.cf 接下來來設定 Postfix 的部份,若先前有加 header_checks 或 body_checks 的話,請記得在 main.cf 加上註解。再設定 master.cf,主要是修改二行設定,並且再加入一行過濾的設定。 修改的部份如下: smtp inet n - n - - smtpd -o content_filter=postfixfilter smtp unix - - n - - smtp -o content_filter=postfixfilter smtps inet n - n - - smtpd -o smtpd_tls_wrappermode=yes -o smtpd_sasl_auth_enable=yes 新增這一行: postfixfilter unix - n n - - pipe flags=Rq user=spamfilter argv=/usr/local/sbin/filter.sh -f ${sender} — ${recipient} 7. 啟動所有的 Service 吧 在 /etc/rc.conf 加入一行,在開機的時候啟動 SpamAssassin。 spamd_enable=」YES」 馬上啟動這些 Service: /usr/local/etc/rc.d/sa-spamd start /usr/local/etc/rc.d/cyrus_pwcheck.sh start postfix start