歡迎您光臨本站 註冊首頁

Sendmail SMTP SASL認證詳盡指南

←手機掃碼閱讀     火星人 @ 2014-03-12 , reply:0
  一:概述

在現代網路環境中,mail relay是個討論得非常熱烈的話題,如何在公司的郵件伺服器上
允許外部用戶或者移動用戶進行mail relay則是人們關注的焦點,隨著標準化的SASL認證
的出現並結合開放源軟體Sendmail新版本中對SMTP AUTH的支持,問題已經逐漸變得明朗
而易於解決。

二:目的

本文的主要目的是要在任何一台linux系統上,不論是什麼版本,我們將編譯Cyrus-SASL
和Sendmail 8.11來使系統支持SASL的SMTP認證,詳盡解釋如何編譯和實現該功能及相關
的問題。

三:環境

我所用的編譯和測試環境是Redhat Linux 6.1和VA Linux 6.2.4,使用cyrus-SASL 1.5.24
和Sendmail 8.11.4。在Redhat Linux 7.1上已經不需要再編譯,只需要生成自己的
sendmail.cf即可。

四:步驟

1. 從ftp://ftp.andrew.cmu.edu/pub/cyrus-mail下載最新的cyrus-sasl-1.5.24.tar.gz
放在/home/jephe目錄下

#cd /home/jephe
#tar xvfz cyrus-sasl-1.5.24.tar.gz
#cd cyrus-sasl-1.5.24
#./configure --enable-login (默認配置不支持login認證機制,但對Outlook Express是必需的)
#make
#make install

這將自動安裝所有的SASL庫文件在/usr/local/lib/sasl/目錄和頭文件在/usr/local/include/目錄,
但是Sendmail卻在/usr/lib/sasl中尋找這些文件,因此最簡單的辦法就是創建符號鏈接,做下面的:

#cd /usr/lib
#ln -sf /usr/local/lib/sasl sasl
#cp /usr/local/lib/libsasl* /usr/lib

注意上面最後一步拷貝時把相關的符號鏈接文件也拷貝成了硬鏈接文件,再做下面的:

#cd /usr/lib
#rm -f libsasl.so libsasl.so.7
#ln -sf libsasl.so.7.1.8 libsasl.so
#ln -sf libsasl.so.7.1.8 libsasl.so.7

最後應該看上去象下面:

[jephe@smtp lib]$ ls libsasl* sasl -l
-r-x------ 1 root root 683 Jul 27 16:32 libsasl.la
lrwxrwxrwx 1 root root 16 Jul 27 16:32 libsasl.so -> libsasl.so.7.1.8
lrwxrwxrwx 1 root root 16 Jul 27 16:33 libsasl.so.7 -> libsasl.so.7.1.8
-r-x------ 1 root root 194079 Jul 27 16:32 libsasl.so.7.1.8
lrwxrwxrwx 1 root root 19 Jul 27 16:32 sasl -> /usr/local/lib/sasl

現在更改文件的許可許可權

#chmod -R 500 /usr/local/lib/sasl /usr/local/lib/libsasl* /usr/lib/sasl /usr/lib/libsasl*

再編輯文件/etc/ld.so.conf,加下面的行

#/usr/lib/sasl

然後運行

#/sbin/ldconfig (不應該有任何錯誤警告顯示)

2. 現在編譯Sendmail,從www.sendmail.org下載最新的Sendmail.8.11.4.tar.gz也放於/home/jephe目錄下

#cd /home/jephe
#tar xvfz sendmail.8.11.4.tar.gz
#cd /home/jephe/sendmail-8.11.4

放下面的行到devtools/Site/site.config.m4 (注意文件名不要弄錯,用vi編輯新文件site.config.m4)

APPENDDEF(`confENVDEF', `-DSASL')
APPENDDEF(`conf_sendmail_LIBS', `-lsasl')
APPENDDEF(`confLIBDIRS', `-L/usr/lib/sasl')
APPENDDEF(`confINCDIRS', `-I/usr/local/include')

再用vi編輯一個新的配置文件放在cf/cf/config.mc,內容如下:(注意沒有行號,此處的行號是
為了下面對每行進行解釋的方便)

divert(-1) #
# Copyright (c) 1998, 1999 Sendmail, Inc. and its suppliers.
# All rights reserved.
# Copyright (c) 1983 Eric P. Allman. All rights reserved.
# Copyright (c) 1988, 1993
# The Regents of the University of California. All rights reserved.
#

# By using this file, you agree to the terms and conditions set
# forth in the LICENSE file which can be found at the top level of
# the sendmail distribution.
#
#


1 VERSIONID(`$Id: generic-linux.mc,v 8.1 1999/09/24 22:48:05 gshapiro Exp $')
2 OSTYPE(linux)dnl
3 DOMAIN(generic)dnl
4 MAILER(local)dnl
5 MAILER(smtp)dnl

6 define(`confPRIVACY_FLAGS', `authwarnings,needmailhelo,novrfy,noexpn')dnl
7 define(QUEUE_DIR,`/var/spool/mqueue/q*')


8 FEATURE(`use_cw_file')dnl
9 FEATURE(`mailertable',`hash -o /etc/mail/mailertable')dnl
10 FEATURE(`domaintable',`hash -o /etc/mail/domaintable')dnl
11 FEATURE(`virtusertable',`hash -o /etc/mail/virtusertable')dnl

12 dnl FEATURE(`genericstable',`hash -o /etc/mail/genericstable')dnl
13 dnl GENERICS_DOMAIN_FILE(`/etc/mail/genericsdomain')dnl
14 dnl FEATURE(allmasquerade)dnl
15 dnl FEATURE(masquerade_envelope)dnl
16 dnl MASQUERADE_AS(yourdomain.com)dnl

17 FEATURE(redirect)dnl
18 FEATURE(always_add_domain)dnl
19 FEATURE(`access_db')dnl
20 FEATURE(`blacklist_recipients')dnl

21 define(`confAUTH_MECHANISMS', `LOGIN PLAIN GSSAPI KERBEROS_V4 DIGEST-MD5 CRAM-MD5')dnl
22 TRUST_AUTH_MECH(`LOGIN PLAIN GSSAPI KERBEROS_V4 DIGEST-MD5 CRAM-MD5')dnl
23 DAEMON_OPTIONS(`Port=smtp, Name=MTA')dnl
24 FEATURE(`no_default_msa')dnl
25 DAEMON_OPTIONS(`Port=587, Name=MSA, M=Ea')dnl

註: 第6行 增強sendmail的安全性
第7行 使用多個郵件隊列增加性能
第12-16行 此處被註釋了,如果你需要這些高級設置你可以去掉前面的dnl來使用它,
但你必須清楚地知道你在做什麼。
第20-25行 這些行最重要,因為它是用於SMTP AUTH的必須設置


現在你能編譯Sendmail.

#cd /home/jephe/sendmail-8.11.4
#sh ./Build (如果你已經不是第一次編譯,則要sh ./Build -c 清除上一次編譯的內容)
#cd cf/cf
#sh ./Build config.cf

現在準備編譯Sendmail,編譯之前,請備份你原先版本的sendmail執行文件和配置文件,一般為
/usr/sbin/sendmail 和 /etc/sendmail.cf

#cd ../../
#sh ./Build install
#cp -f config.cf /etc/mail/sendmail.cf
#cd /var/spool/mqueue
#mkdir q1 q2 q3 q4 q5 ( 你可以隨時創建更多的目錄,只要以q開頭即可被立刻當作隊列目錄使用)
#/etc/rc.d/init.d/sendmail restart

確信在編譯過程中你能看見下面的行:

...-I /usr/lib/include -DNEWDB -DSASL


新版本的sendmail放所有的配置文件在/etc/mail下,你可能需要做下面的事情:

a. #cp /etc/aliases /etc/mail/aliases

b. #cp /etc/sendmail.cw /etc/mail/local-host-names
c. #/etc/rc.d/init.d/sendmail restart

現在,編譯應該結束,試著運行下面的命令檢查輸出

#/usr/sbin/sendmail -d0.1 -bv root |grep SASL

你應該看到象下面的行

NETUNIX NEWDB QUEUE SASL SCANF SMTP USERDB XDEBUG


3. 準備設置SASL認證

用vi編輯新文件/usr/lib/sasl/Sendmail.conf放下面的行

pwcheck_method:pam

既然Redhat Linux普遍使用PAM,我們就用PAM 認證,後面再講SASLDB方式的認證。


現在放下面的行到/etc/pam.d/smtp (用vi編輯新文件smtp)

#%PAM-1.0

1 #auth required /lib/security/pam_deny
2 #auth required /lib/security/pam_shells.so

3 auth required /lib/security/pam_pwdb.so shadow md5

4 auth sufficient /lib/security/pam_listfile.so item=user sense=allow file=/etc/m
ail/smtpsuperusers.allow

5 auth required /lib/security/pam_listfile.so item=user sense=deny file=/etc/mail
/smtpusers.deny
6 auth required /lib/security/pam_listfile.so item=group sense=allow file=/etc/ma
il/smtpgroup.allow


如果你有大量的POP3用戶,且想使用與POP3同樣的密碼,則可使用PAM認證方式,上面的各行解釋如下

第1行 如果你想禁止所有人mail relay時,則簡單地去掉前面的註釋即可
第2行 如果打開了,則如果用戶的登錄shell不在/etc/shells中,則禁止mail relay
第3-6行 使用PAM_listfile模塊通過外部文件控制認證

第3行 必須包括此行來使用文件/etc/shadow密碼認證,放在最前面使得任何用戶首先必須通過
密碼認證,再檢查後面的規則

第4行 只要用戶被列在文件/etc/mail/smtpsuperusers.allow中,則允許relay
注意這裡用的是sufficient不是required,你必須放這一行在最前面,這樣
只要檢測到用戶被列在該文件中,則不再往下檢查,無論如何允許mail relay

第5行 如果用戶列在/etc/mail/smtpusers.deny,一行一個用戶名,則禁止relay

第6行 如果用戶所在的組列在/etc/mail/smtpgroup.allow且不在/etc/mail/smtpusers.deny中,
則允許relay



現在可以用telnet來測試:

#telnet localhost 25
Trying 127.0.0.1...
Connected to test.domain.com.
Escape character is '^]'.
220 test.domain.com ESMTP Sendmail 8.11.4/8.11.4; Sun, 10 Dec 2000 17:56:54 -0800
EHLO localhost
250-test.domain.com Hello IDENT:root@test.domain.com [127.0.0.1], pleased to meet you
250-ENHANCEDSTATUSCODES
250-EXPN
250-VERB
250-8BITMIME
250-SIZE
250-DSN
250-ONEX
250-ETRN
250-XUSR
250-AUTH LOGIN PLAIN
250 HELP
quit
221 2.0.0 test.domain.com closing connection
Connection closed by foreign host.

你需要看見login和plain在那裡,否則不能進行smtp認證。

Outlook Express使用LOGIN認證,Netscape Mail使用PLAIN認證,Foxmail 3.11 一般使用LOGIN認證。

既然上面這些我們最常用的郵件客戶程序Outlook Express和Netscape Mail不支持加密的認證方式如
DIGEST-MD5和CRAM-MD5,那麼我們最好也就不要enable加密認證方式,加密的認證方式必須要設置

pwcheck_method:sasldb,用PAM只能用於login和plain兩種非加密方式。如果你的大多數客戶都是outlook
Express和Netscape Mail用戶,假設你的pwcheck_method設成PAM,但是又enable了加密認證,這樣當郵件
客戶端程序如The bat(支持cram-md5加密認證)向伺服器連接進行認證時,它根據伺服器端的可接受的認證
機制類型的響應而自動使用cram-md5認證,這樣反而不能通過認證,因為你的pwcheck_method是PAM。然而,
如果你沒有enable加密認證,則The Bat根據伺服器的響應自動使用login的認證機制。也就是說,它會優先
使用加密的認證,只要伺服器宣稱支持的話。

(Foxmail 3.11宣稱支持cram-md5,仍待測試)

下面說明SASLDB認證,當你僅有很少的用戶,且想用另一個資料庫不用/etc/shadow來驗證,則可用
SASLDB的方式,在/usr/lib/sasl/Sendmail.conf中設置

pwcheck_method:sasldb

然後進行/usr/local/sbin以saslpasswd命令創建至少一個新用戶,如

./saslpasswd jephe
Password: *******
Again (for verification): *******

這將創建文件/etc/sasldb,你不得不改變許可許可權至400

#chmod 400 /etc/sasldb

然後可以檢查哪些用戶在資料庫中

./sasldblistusers
user: jephe realm: smtp.domain.com mech: DIGEST-MD5
user: jephe realm: smtp.domain.com mech: PLAIN
user: jephe realm: smtp.domain.com mech: CRAM-MD5

如果有錯誤,檢查/var/log/mailog和/var/log/messages.

現在再次用telnet檢查,現在應該出現Digest-md5和cram-md5加密認證。

#telnet localhost 25
Trying 127.0.0.1...
Connected to test.domain.com.
Escape character is '^]'.
220 test.domain.com ESMTP Sendmail 8.11.4/8.11.4; Sun, 10 Dec 2000 17:56:54 -0800
EHLO localhost
250-test.domain.com Hello IDENT:root@test.domain.com [127.0.0.1], pleased to meet you
250-ENHANCEDSTATUSCODES
250-EXPN
250-VERB
250-8BITMIME
250-SIZE
250-DSN
250-ONEX
250-ETRN
250-XUSR
250-AUTH LOGIN PLAIN DIGEST-MD5 CRAM-MD5
250 HELP
quit
221 2.0.0 test.domain.com closing connection
Connection closed by foreign host.

4. 設置你的Outlook Express and Netscape Mail

在『工具』-『帳號』-『屬性』-『伺服器『-
』發送郵件伺服器』中選中『我的伺服器需要認證』

然後在『登錄用戶帳號』上填上

用戶名和密碼即可,如果不工作,則試著在用戶名欄填入
你的用戶名@你的realm冒號後面的伺服器全稱域名

如上面我的情況則用戶名欄為jephe@smtp.domain.com

對Netscape Mail,則在edit/preferences/mail servers/outgoing mail server username
中填入用戶名即可。

5. 如何簡單地只是使用telnet來測試SMTP AUTH?

是的,你能快速地測試是否哪個用戶允許認證,就用#telnet localhost 25 能做到

a. 下載base64編碼和解碼器C語言源程序

http://www.sendmail.org/~ca/email/prgs/ed64.c

然後用下面的命令編譯成linux上的執行文件

#gcc -o ed64 ed64.c -I/usr/include

命令可選項 -e ,象下面的用法

#[root@test /root]# ./ed64 -e jephe (把字元串jephe編碼成base64格式的可列印字元串)
'jephe ->
'amVwaGU='

#[root@test /root]# ./ed64 VXNlcm5hbWU6 (把該base64字元串解碼成ASCII字元串)
'VXNlcm5hbWU6 ->
'Username:'

b. #telnet localhost 25

250-smtp.domain.com Hello localhost.localdomain [127.0.0.1], pleased to meet you
250-ENHANCEDSTATUSCODES
250-8BITMIME
250-SIZE
250-DSN
250-ONEX
250-ETRN

250-XUSR
250-AUTH LOGIN PLAIN
250 HELP
auth login
334 VXNlcm5hbWU6
amVwaGU=
334 UGFzc3dvcmQ6
bXlwYXNzd29yZA==
235 2.0.0 OK Authenticated


6. 常見問題解答

a. 當你每次改pwcheck_method:後面的參數時需要重啟Sendmail,用killall -HUP sendmail

b. 為了臨時禁止cram-md5和digest-md5加密認證,可以用#chmod 440 /etc/sasldb改變許可權
然後killall -HUP sendmail,則加密認證自動禁止;
也可以編譯時用--disable-cram --disable-digest永久禁止

c. 為什麼不需要用pwcheck後台程序檢測密碼?

因為Sendmail本身就以root運行,pwcheck後台程序是為那些不是以root身份運行的應用程序使用的
當這些應用程序運行時,它通過pwcheck來驗證用戶身份。

d. 如果你想在用PAM的同時,每當用戶成功認證后更改SASL資料庫,可在/usr/lib/sasl/Sendmail.conf
中使用
pwcheck_method:pam
auto_transition:true


這樣你必須象上面介紹的一樣設置好SASL,且確保/etc/sasldb許可權為400,這樣當每次用戶通過PAM成功
認證后,/etc/sasldb資料庫同時被更新。

e. 如果不工作,也許你需要試著加與/usr/lib/sasl/Sendmail.conf中同樣的行
到/usr/lib/sasl/saslpassword.conf
pwcheck_method:pam

f. 如果你想在/var/log/maillog中log住用戶認證的用戶名,則在/etc/mail/sendmail.cf
中改loglevel=10。

g. 如果你用的是Redhat 7.1,你不需要編譯,因為Redhat已經做過了編譯工作,你要作的就是改
/usr/share/sendmail-cf/cf下的redhat.mc成下面的,然後生成自己的/etc/mail/sendmail.cf,
再根據上面的配置設置就可以了。

divert(-1)
dnl This is the sendmail macro config file. If you make changes to this file,
dnl you need the sendmail-cf rpm installed and then have to generate a
dnl new /etc/sendmail.cf by running the following command:
dnl
dnl m4 /etc/mail/sendmail.mc > /etc/sendmail.cf
dnl
include(`../m4/cf.m4')
VERSIONID(`linux setup for Red Hat Linux')dnl
OSTYPE(`linux')
define(`confDEF_USER_ID',``8:12'')dnl
undefine(`UUCP_RELAY')dnl
undefine(`BITNET_RELAY')dnl
define(`confAUTO_REBUILD')dnl
define(`confTO_CONNECT', `1m')dnl
define(`confTRY_NULL_MX_LIST',true)dnl
define(`confDONT_PROBE_INTERFACES',true)dnl
define(`PROCMAIL_MAILER_PATH',`/usr/bin/procmail')dnl
define(`ALIAS_FILE', `/etc/aliases')dnl
define(`STATUS_FILE', `/var/log/sendmail.st')dnl
define(`UUCP_MAILER_MAX', `2000000')dnl
define(`confUSERDB_SPEC', `/etc/mail/userdb.db')dnl
define(`confPRIVACY_FLAGS', `authwarnings,novrfy,noexpn,restrictqrun')dnl
define(`confAUTH_OPTIONS', `A')dnl
define(QUEUE_DIR,`/var/spool/mqueue/q*')
TRUST_AUTH_MECH(`DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
define(`confAUTH_MECHANISMS', `DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
DAEMON_OPTIONS(`Port=25,Name=MTA')dnl
DAEMON_OPTIONS(`Port=587,Name=MSA,M=Ea')dnl
dnl define(`confTO_QUEUEWARN', `4h')dnl
dnl define(`confTO_QUEUERETURN', `5d')dnl
dnl define(`confQUEUE_LA', `12')dnl
dnl define(`confREFUSE_LA', `18')dnl
dnl FEATURE(delay_checks)dnl
FEATURE(`no_default_msa',`dnl')dnl
FEATURE(`smrsh',`/usr/sbin/smrsh')dnl
FEATURE(`mailertable',`hash -o /etc/mail/mailertable')dnl
FEATURE(`virtusertable',`hash -o /etc/mail/virtusertable')dnl
FEATURE(redirect)dnl
FEATURE(always_add_domain)dnl
FEATURE(use_cw_file)dnl

FEATURE(use_ct_file)dnl
FEATURE(local_procmail)dnl
FEATURE(`access_db')dnl
FEATURE(`blacklist_recipients')dnl

EXPOSED_USER(`root')dnl
dnl This changes sendmail to only listen on the loopback device 127.0.0.1
dnl and not on any other network devices. Comment this out if you want
dnl to accept email over the network.
dnl DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')
dnl We strongly recommend to comment this one out if you want to protect
dnl yourself from spam. However, the laptop and users on computers that do
dnl not have 24x7 DNS do need this.

dnl FEATURE(`accept_unresolvable_domains')dnl
dnl FEATURE(`relay_based_on_MX')dnl
MAILER(smtp)dnl
MAILER(procmail)dnl

吳阿亭 ( Jephe Wu )


[火星人 ] Sendmail SMTP SASL認證詳盡指南已經有510次圍觀

http://coctec.com/docs/security/show-post-72813.html