一.安裝配置dovecotvim
1.dovecot簡介
bash
Dovecot 是一個開源的 IMAP 和 POP3 郵件服務器,支持四種協議:pop3(110/tcp), imap4(143/tcp), pop3s, imaps。POP / IMAP 是 MUA 從郵件服務器中讀取郵件時使用的協議。其中,與 POP3 是從郵件服務器中下載郵件存起來,IMAP4 則是將郵件留在服務器端直接對郵件進行管理、操做。服務器
2.安裝dovecot
網絡
# yum install dovecot -y
3.配置dovecotapp
# vim /etc/dovecot.conf
啓用並修改如下參數dom
!include conf.d/*.conf //該配置項說明conf.d下的因此conf結尾的文件均有效,注意最前面的"!"號tcp
protocols = imap pop3ide
login_trusted_networks = 192.168.50.0/24 //指定容許登陸的網段地址oop
若未設置login_trusted_networks參數值,使用telnet登陸110端口,將會出現以下錯誤,不知道這個值能不能設置爲四個零,這個還有待測試…
post
[root@mail ~]# telnet mail.wybedu.com 110 Trying 192.168.220.111... Connected to mail.wybedu.com. Escape character is '^]'. +OK Dovecot ready. user openstack -ERR Plaintext authentication disallowed on non-secure (SSL/TLS) connections. pass openstack -ERR Plaintext authentication disallowed on non-secure (SSL/TLS) connections. quit +OK Logging out Connection closed by foreign host. [root@mail ~]# tail -f /var/log/maillog Dec 21 16:51:00 rhel6 dovecot: pop3-login: Aborted login (tried to use disabled plaintext auth): rip=192.168.50.211, lip=192.168.50.24, mpid=0
[root@mail ~]# vim /etc/dovecot/conf.d/10-mail.conf
設置/etc/dovecot/conf.d/10-mail.conf配置文件以下:
mail_location = mbox:~/mail:INBOX=/var/mail/%u
mbox_write_locks = fcntl
如未設置mail_location參數值,將會出現以下錯誤:
[root@mail ~]# telnet mail.wybedu.com 110 Trying 192.168.220.111... Connected to mail.wybedu.com. Escape character is '^]'. +OK Dovecot ready. user openstack +OK pass openstack Connection closed by foreign host. [root@mail ~]# tail /var/log/maillog Jul 11 11:57:48 localhost dovecot: pop3-login: Login: user=<openstack>, method=PLAIN, rip=192.168.220.111, lip=192.168.220.111, mpid=34823, secured Jul 11 11:57:48 localhost dovecot: pop3(openstack): Error: user openstack: Initialization failed: mail_location not set and autodetection failed: Mail storage autodetection failed with home=/home/openstack Jul 11 11:57:48 localhost dovecot: pop3(openstack): Error: Invalid user settings. Refer to server log for more information.
設置完mail_location值後,須要重啓dovecot服務,同時須要建立相關的目錄,不然還會出現以下錯誤:
[root@mail ~]# telnet mail.wybedu.com 110 Trying 192.168.220.111... Connected to mail.wybedu.com. Escape character is '^]'. +OK Dovecot ready. USER openstack +OK PASS openstack -ERR [IN-USE] Couldn't open INBOX: Internal error occurred. Refer to server log for more information. [2015-07-11 12:13:35] Connection closed by foreign host. [root@mail ~]# tail /var/log/maillog Jul 11 12:04:27 localhost dovecot: pop3-login: Login: user=<openstack>, method=PLAIN, rip=192.168.220.111, lip=192.168.220.111, mpid=34951, secured Jul 11 12:04:27 localhost dovecot: pop3(openstack): Error: user openstack: Initialization failed: mail_location not set and autodetection failed: Mail storage autodetection failed with home=/home/openstack Jul 11 12:04:27 localhost dovecot: pop3(openstack): Error: Invalid user settings. Refer to server log for more information. Jul 11 12:13:08 localhost dovecot: master: Warning: Killed with signal 15 (by pid=34962 uid=0 code=kill) Jul 11 12:13:09 localhost dovecot: master: Dovecot v2.0.9 starting up (core dumps disabled) Jul 11 12:13:35 localhost dovecot: pop3-login: Login: user=<openstack>, method=PLAIN, rip=192.168.220.111, lip=192.168.220.111, mpid=34984, secured Jul 11 12:13:35 localhost dovecot: pop3(openstack): Error: chown(/home/openstack/mail/.imap/INBOX, -1, 12(mail)) failed: Operation not permitted (egid=2528(openstack), group based on /var/mail/openstack) Jul 11 12:13:35 localhost dovecot: pop3(openstack): Error: mkdir(/home/openstack/mail/.imap/INBOX) failed: Operation not permitted Jul 11 12:13:35 localhost dovecot: pop3(openstack): Error: Couldn't open INBOX: Internal error occurred. Refer to server log for more information. [2015-07-11 12:13:35] Jul 11 12:13:35 localhost dovecot: pop3(openstack): Couldn't open INBOX top=0/0, retr=0/0, del=0/0, size=0 [root@mail ~]# su - openstack [openstack@mail ~]$ mkdir mail/.imap/INBOX [openstack@mail ~]$ exit
4.啓動服務並讓其開機自啓動
# service dovecot start # chkconfig dovecot on
5.測試
[root@mail ~]# telnet mail.wybedu.com 110 Trying 192.168.220.111... Connected to mail.wybedu.com. Escape character is '^]'. +OK Dovecot ready. user openstack +OK pass openstack +OK Logged in. list +OK 1 messages: 1 428 . retr 1 +OK 428 octets Return-Path: <hadoop@wybedu.com> X-Original-To: openstack Delivered-To: openstack@wybedu.com Received: from mail.wybedu.com (ns.wybedu.com [192.168.220.111]) by mail.wybedu.com (Postfix) with SMTP id 265C162B for <openstack>; Sat, 11 Jul 2015 11:54:57 +0800 (CST) Message-Id: <20150711035509.265C162B@mail.wybedu.com> Date: Sat, 11 Jul 2015 11:54:57 +0800 (CST) From: hadoop@wybedu.com SUBJECT how are you! hello . quit +OK Logging out. Connection closed by foreign host.
二.爲postfix開啓基於cyrus-sasl的認證功能
1.啓動sasl服務並讓其開機自啓動
# vim /etc/sysconfig/saslauthd
修改sasl服務配置文件,將sasl認證機制修改成shadow
MECH=shadow
注:saslauthd -v: 顯示當前主機saslauthd服務所支持的認證機制,默認爲pam
# service saslauthd start # chkconfig saslauthd on
2.測試是否能實現用戶認證
# testsaslauthd -u username -p password
3.實現postfix基於客戶端的訪問控制
1)、基於客戶端的訪問控制概覽
postfix內置了多種反垃圾郵件的機制,其中就包括「客戶端」發送郵件限制。客戶端判別機制能夠設定一系列客戶信息的判別條件:
smtpd_client_restrictions:定義客戶端鏈接限定
smtpd_data_restrictions:定義是否容許發送數據(即data階段)
smtpd_helo_restrictions:定義是否容許發送hello信息(即helo階段)
smtpd_recipient_restrictions:定義是否容許向誰發送郵件(即rcpt to階段)
smtpd_sender_restrictions:定義是否容許誰向外發送郵件(即mail from階段)
上面的每一項參數分別用於檢查SMTP會話過程當中的特定階段,即客戶端提供相應信息的階段,如當客戶端發起鏈接請求時,postfix就能夠根據配置文件中定義的smtpd_client_restrictions參數來判別此客戶端IP的訪問權限。相應地,smtpd_helo_restrictions則用於根據用戶的helo信息判別客戶端的訪問能力等等。
若是DATA命令以前的全部內容都被接受,客戶端接着就能夠開始傳送郵件內容了。郵件內容一般由兩部分組成,前半部分是標題(header),其能夠由header_check過濾,後半部分是郵件正文(body),其能夠由check_body過濾。這兩項實現的是郵件「內容檢查」。
postfix的默認配置以下:
smtpd_client_restrictions =
smtpd_data_restrictions =
smtpd_end_of_data_restrictions =
smtpd_etrn_restrictions =
smtpd_helo_restrictions =
smtpd_recipient_restrictions = permit_mynetworks, reject_unauth_destination
smtpd_sender_restrictions =
這限制了只有mynetworks參數中定義的本地網絡中的客戶端才能經過postfix轉發郵件,其它客戶端則不被容許,從而關閉了開放式中繼(open relay)的功能。
Postfix有多個內置的限制條件,如上面的permit_mynetworks和reject_unauth_destination,但管理員也能夠使用訪問表(access map)來自定義限制條件。自定義訪問表的條件一般使用check_client_access, check_helo_access, check_sender_access, check_recipient_access進行,它們後面一般跟上type:mapname格式的訪問表類型和名稱。其中,check_sender_access和check_recipient_access用來檢查客戶端提供的郵件地址,所以,其訪問表中能夠使用完整的郵件地址,如admin@magedu.com;也能夠只使用域名,如magedu.com;還能夠只有用戶名的部分,如marion@。
2)、實現示例1
這裏以禁止172.16.100.200這臺主機經過工做在172.16.100.1上的postfix服務發送郵件爲例演示說明其實現過程。訪問表使用hash的格式。
(1)首先,編輯/etc/postfix/access文件,以之作爲客戶端檢查的控制文件,在裏面定義以下一行:
172.16.100.200 REJECT
(2)將此文件轉換爲hash格式
# postmap /etc/postfix/access
(3)配置postfix使用此文件對客戶端進行檢查
編輯/etc/postfix/main.cf文件,添加以下參數:
smtpd_client_restrictions = check_client_access hash:/etc/postfix/access
(4)讓postfix從新載入配置文件便可進行發信控制的效果測試了。
3)、實現示例2
這裏以禁止經過本服務器向microsoft.com域發送郵件爲例演示其實現過程。訪問表使用hash的格式。
(1)首先,創建/etc/postfix/denydstdomains文件(文件名任取),在裏面定義以下一行:
microsoft.com REJECT
(2)將此文件轉換爲hash格式
# postmap /etc/postfix/denydstdomains
(3)配置postfix使用此文件對客戶端進行檢查
編輯/etc/postfix/main.cf文件,添加以下參數:
smtpd_recipient_restrictions = check_recipient_access hash:/etc/postfix/denydstdomains, permit_mynetworks, reject_unauth_destination(順序對結果會有影響)
(4)讓postfix從新載入配置文件便可進行發信控制的效果測試了。
4)、檢查表格式的說明
hash類的檢查表都使用相似以下的格式:
pattern action
檢查表文件中,空白行、僅包含空白字符的行和以#開頭的行都會被忽略。以空白字符開頭後跟其它非空白字符的行會被認爲是前一行的延續,是一行的組成部分。
(1)關於pattern
其pattern一般有兩類地址:郵件地址和主機名稱/地址。
郵件地址的pattern格式以下:
user@domain 用於匹配指定郵件地址;
domain.tld 用於匹配以此域名做爲郵件地址中的域名部分的全部郵件地址;
user@ 用於匹配以此做爲郵件地址中的用戶名部分的全部郵件地址;
主機名稱/地址的pattern格式以下:
domain.tld 用於匹配指定域及其子域內的全部主機;
.domain.tld 用於匹配指定域的子域內的全部主機;
net.work.addr.ess
net.work.addr
net.work
net 用於匹配特定的IP地址或網絡內的全部主機;
network/mask CIDR格式,匹配指定網絡內的全部主機;
(2)關於action
接受類的動做:
OK 接受其pattern匹配的郵件地址或主機名稱/地址;
所有由數字組成的action 隱式表示OK;
拒絕類的動做(部分):
4NN text
5NN text
其中4NN類表示過一下子重試;5NN類表示嚴重錯誤,將中止重試郵件發送;421和521對於postfix來講有特殊意義,儘可能不要自定義這兩個代碼;
REJECT optional text... 拒絕;text爲可選信息;
DEFER optional text... 拒絕;text爲可選信息;
4.爲postfix開啓基於cyrus-sasl的認證功能
使用如下命令驗正postfix是否支持cyrus風格的sasl認證,若是您的輸出爲如下結果,則是支持的:
# /usr/local/postfix/sbin/postconf -a
cyrus
dovecot
# vim /etc/postfix/main.cf
添加如下內容:
############################CYRUS-SASL############################
broken_sasl_auth_clients = yes:是否經過sasl驗證客戶端身份
smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject_invalid_hostname,reject_non_fqdn_hostname,reject_unknown_sender_domain,reject_non_fqdn_sender,reject_non_fqdn_recipient,reject_unknown_recipient_domain,reject_unauth_pipelining,reject_unauth_destination
smtpd_sasl_auth_enable = yes:啓用sasl認證功能
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_security_options = noanonymous:不支持匿名用戶
smtpd_sasl_application_name = smtpd:哪個應用程序使用sasl功能(2.3版本之後已廢棄,改用smtpd_sasl_path = smtpd)
smtpd_banner = Welcome to our $myhostname ESMTP,Warning: Version not Available!:telnet鏈接時顯示信息
# vim /usr/lib(64)/sasl2/smtpd.conf
添加以下內容:
pwcheck_method: saslauthd
mech_list: PLAIN LOGIN
讓postfix從新加載配置文件
[root@mail ~]# /usr/sbin/postfix reload [root@mail ~]# telnet 192.168.220.111 25 Trying 192.168.220.111... Connected to 192.168.220.111. Escape character is '^]'. 220 Welcome to our mail.wybedu.com ESMTP,Warning: Version not Available! ehlo mail.wybedu.com 250-mail.wybedu.com 250-PIPELINING 250-SIZE 10240000 250-VRFY 250-ETRN 250-AUTH PLAIN LOGIN 250-AUTH=PLAIN LOGIN (請確保您的輸出以相似兩行) 250-ENHANCEDSTATUSCODES 250-8BITMIME 250 DSN
5.測試
[root@mail ~]# telnet 192.168.220.111 25 Trying 192.168.220.111... Connected to 192.168.220.111. Escape character is '^]'. 220 Welcome to our mail.wybedu.com ESMTP,Warning: Version not Available! ehlo mail.wybedu.com 250-mail.wybedu.com 250-PIPELINING 250-SIZE 10240000 250-VRFY 250-ETRN 250-AUTH PLAIN LOGIN 250-AUTH=PLAIN LOGIN 250-ENHANCEDSTATUSCODES 250-8BITMIME 250 DSN auth login 334 VXNlcm5hbWU6 aGFkb29w (用戶名密碼採用base64編碼:echo -n 'hadoop' | openssl base64) 334 UGFzc3dvcmQ6 aGFkb29w 235 2.7.0 Authentication successful mail from:root@wybedu.com 250 2.1.0 Ok rcpt to:hello@aol.com 250 2.1.5 Ok data 354 End data with <CR><LF>.<CR><LF> hello . 250 2.0.0 Ok: queued as B6C9962B quit 221 2.0.0 Bye Connection closed by foreign host.