Quote:http://hilinux.com/man/debian/ch10s04.html

Exim4被設計成能高效地、不間斷地在Internet上運行,而且能處理各種混合郵件。Exim4是如何投遞郵件和接收郵件的呢?有何安全机制能确保郵件的安全呢?這節將簡單介紹Exim4的工作原理。

Exim4 處理的每封郵件都以一個16位字符的名稱標識,該標識由三部份組成,以”-“號分隔,如:1GS3YU-0000zG-Nd。這些字符是經過base編碼 的,第一部份的原始信息是接收郵件的時間,第二部份的原始信息是接收郵件的進程id,第三部份的信息與配置文件中localhost_number的設置 相關。標識名與時間和進程相關,有效保証了標識名的唯一性。

Exim4通過基于TCP/IP的SMTP協議從其它主机收取郵件。 Exim4接收郵件后,會把郵件分成兩個文件保存在spool目錄,在我的机器上就是/var/spool/exim4/input/目錄。這兩個文件的 命名規則是郵件標識后加-D和-H。以-D結尾的文件保存著郵件正文的信息,以-H結尾的文件保存郵件的頭信息。在一些操作系統上,同一目錄下包含太多的 文件會造成性能問題,所以Exim4也可通過設置split_spool_directory選項,在input目錄下再創建62個子目錄來保存郵件信 息。這62個子目錄以單個字母和數字命名(26個小寫字母+26個大寫字母+10個數字共62個數字和字母)。這樣的設置,使郵件的排隊只在子目錄下進 行,而不是一個目錄所有郵件。所以即使在郵件量不大的情況下也能效提升郵件系統的性能。

接收的郵件會停留在spool目錄直到郵件被傳遞到接收者或被管理員刪除。如果郵件不能被傳遞,則郵件會被打上”frozen”標記,並且不會再嘗試投遞 該郵件。但管理員可以設置 ignore_bounce_errors_after和timeout_frozen_after選項,指定一定時間后解除郵件的”frozen”狀 態。

當Exim4處理郵件時會把處理過程的日志信息保存在/var/log/exim4目錄下。其中mainlog日志記錄了大量的信息,從中我們可以跟蹤 Exim4處理郵件的過程。在一個大負載的服務器上,生成大量的日志信息會影響服務器的性能,所以我們也可以用 no_message_logs參數禁止日志功能。

router和transport是Exim4傳遞郵件過程中兩個重要的環節, router負責處理郵件地址,並把郵件傳遞給不同的transport作進一步處理,transport負責把spool中的郵件傳遞到目的地,完成投 遞過程。有兩種的transport,一種是本地transport,它的目的地是文件或者本机管道(pipe);另一種transport是遠程 transport,它的目的地是遠程主机。如果一封郵件有多個接收者,則它可能會通過多個transport進行投遞。

router和transport都有不同的driver,實現不同的功能。

router drivers:

*

accept,
*

dnslookup,
*

ipliteral,
*

iplookup,
*

manualroute,
*

queryprogram,
*

redirect,

transport drivers:

*

appendfile,
*

autoreply,
*

lmtp,
*

pipe
*

smtp

安裝與配置

用以下命令安裝exim4郵件系統:

debian:#~ apt-get install exim4 exim4-config exim4-daemon-light

下載完成后會自動運行配置向導,我們也可運行dpkg-reconfigure exim4-confg命令配置Exim4服務器。exim4的配置文件位于/etc/exim4目錄下,update-exim4.conf.conf 文件的配置內容由上面的命令產生,經常改變的命令都集中在這里,而exim4.conf.template文件是Exim4的主配置文件,里面通過 DEBCONFsomethingDEBCONF的方式引用update-exim4.conf.conf文檔的something內容。

exim4.conf.template是Exim4的配置文件,它以分段的格式組織各配置參數。一般的配置參數放在配置文件的最開頭,其它所有的可選配置參數都以”beging+段名”的形式作為段的開始標識。下面是一些可選段:

*

ACL,設置SMTP郵件的ACL(訪問控制列表)。
*

authenticators,設置驗証模式。
*

routers,設置不同的router,處理不同類型的郵件地址,並确定如何發送郵件。
*

transports,設置不同的transport,把信息傳遞到不同的目的地。
*

retry,設置重試的規則,當郵件不能馬上投遞時,我們可設置一些重試規則進行再次投遞。
*

rewrite,定義郵件地址的重寫規則。
*

local_scan,使用local_scan()函數的功能。要用LOCAL_SCAN_HAS_OPTIONS=yes選項開啟。

exim4的配置文件有兩種方式,一種是集中式的,所有的配置都集中在exim4.conf.template文件中,一種是分散式的,配置文件按功能不同分散成很多個文件存放在conf.d目錄下。一般建議新手使用集中式。

下面是一個update-exim4.conf.conf的示例,它可通過debconf程序自動生成,生成后我們可用文本編輯器來修改它。這個示例把exim4配置成通過網上的smarthost來發送Internet郵件。

# /etc/exim4/update-exim4.conf.conf
#
# Edit this file and /etc/mailname by hand and execute update-exim4.conf
# yourself or use ‘dpkg-reconfigure exim4-config’
#
# Please note that this is _not_ a dpkg-conffile and that automatic changes
# to this file might happen. The code handling this will honor your local
# changes, so this is usually fine, but will break local schemes that mess
# around with multiple versions of the file.
#
# update-exim4.conf uses this file to determine variable values to replace
# the DEBCONFsomethingDEBCONF strings in the configuration template files.
#
# Most settings found in here do have corresponding questions in the
# Debconf configuration, but not all of them.
#
# This is a Debian specific file

dc_eximconfig_configtype=’smarthost’           #用smarthost發信;通過SMTP或fetchmail接收信件
dc_other_hostnames=”
dc_local_interfaces=’127.0.0.1′                #只接收本机的SMTP請求,只在本机使用Exim4
dc_readhost=”
dc_relay_domains=”
dc_minimaldns=’false’
dc_relay_nets=”
dc_smarthost=’smtp.21cn.com’                   #遠程的SMTP主机,我們通過它來發送郵件
CFILEMODE=’644′
dc_use_split_config=’false’                    #不使用分散方式的配置文件
dc_hide_mailname=’false’
dc_mailname_in_oh=’true’

編輯/etc/mailname文檔,該文檔記錄你所發郵件的域名后綴。我的mailname只有一條記錄,內容如下:

debian

運行update-exim4.conf命令可以測試/etc/exim4下的配置文件有沒有語法錯誤,如果正常則會生成一個有效簡洁的配置文件備份,保存在/var/lib/exim4/config.autogenerated文件中,方便我們查看。

用以下命令還可測試Exim4的配置有否出錯:

debian:#~ exim4 -bV
Exim version 4.63 #1 built 23-Aug-2006 17:21:47
Copyright (c) University of Cambridge 2006
Berkeley DB: Sleepycat Software: Berkeley DB 4.3.29: (September 6, 2005)
Support for: crypteq iconv() IPv6 GnuTLS move_frozen_messages
Lookups: lsearch wildlsearch nwildlsearch iplsearch cdb dbm dbmnz dsearch nis nis0 passwd
Authenticators: cram_md5 plaintext
Routers: accept dnslookup ipliteral manualroute queryprogram redirect
Transports: appendfile/maildir/mailstore autoreply lmtp pipe smtp
Fixed never_users: 0
Size of off_t: 8
Configuration file is /var/lib/exim4/config.autogenerated

啟動exim4

ebian:/etc/init.d# ./exim4 restart
Stopping MTA for restart: exim4_listener.
Restarting MTA: exim4.

本地郵件發送測試:

debian:~# exim4 -bt fax
R: system_aliases for fax@debian
R: userforward for fax@debian
R: procmail for fax@debian
R: maildrop for fax@debian
R: local_user for fax@debian
fax@debian
router = local_user, transport = maildir_home

外部郵件發送測試:

debian:#~ exim4 -bt jims.yang@gmail.com
R: smarthost for jims.yang@gmail.com
jims.yang@gmail.com
router = smarthost, transport = remote_smtp_smarthost
host smtp.cdn.21cn.com [202.104.32.230]

測試通過后,我們就可正式發送郵件啦。為了清楚顯示郵件的傳送過程,我們使用了-v參數。fax是我系統上的一個用戶,主机名為debian,所以fax用戶的本机郵件地址就是fax@debian

debian:~# exim4 -v fax@debian
from:root
to:fax@debian
subject:local mail test
test will be ok.
.
LOG: MAIN
<= yjnet@21cn.com U=root P=local S=318
debian:~# delivering 1GQemL-0001eD-Pq
R: system_aliases for fax@debian
R: userforward for fax@debian
R: procmail for fax@debian
R: maildrop for fax@debian
R: local_user for fax@debian
T: maildir_home for fax@debian
LOG: MAIN
=> fax <fax@debian> R=local_user T=maildir_home
LOG: MAIN
Completed

出現Completed說明郵件已發送成功,切換到fax用戶我們就可以收到新的郵件啦。下面我們來試試發送Internet。在正式測試之前,我們還要做一些配置。因為通過smtp.21cn.com發送郵件是要經過用戶驗証的。

*

把登錄smtp.21cn.com的用戶名和密碼寫入/etc/exim4/passwd.client文檔。

# password file used when the local exim is authenticating to a remote
# host as a client.
#
# see passwd_client(5) for more documentation
#
# Example:
### target.mail.server.example:login:password
smtp.cdn.21cn.com:yjnet:1234

smtp.cdn.21cn.com是真正的stmp服務器名,而smtp.21cn.com是別名。在這里一定要用真正的smtp服務器名,否則驗証不通過。如何知道真正的smtp服務器名呢?很簡單,用我們最常用的ping命令即可。

debian:~# ping smtp.21cn.com
PING smtp.cdn.21cn.com (202.104.32.230): 56 data bytes

yjnet是登錄用戶名,1234是登錄密碼。
*

把系統用戶對應的外部郵件地址寫入/etc/exim4/email-addresses文件。

# This is /etc/email-addresses. It is part of the exim package
#
# This file contains email addresses to use for outgoing mail. Any local
# part not in here will be qualified by the system domain as normal.
#
# It should contain lines of the form:
#
#user: someone@isp.com
#otheruser: someoneelse@anotherisp.com
root:yjnet@21cn.com

這里的外網郵件用戶名yjnet要與passwd.client中的登錄用戶名yjnet一致。如果不一致,會造成個別mail client不能正常發送郵件。
*

exim4 默認是不使用明文的驗証方式的,因為明文的密碼很容易在網上被截取。但smtp.cdn.21cn.com是使用明文驗証的,所以我們要打開exim4的 明文驗証模式。修改/etc/exim4/exim4.conf.template,加入 AUTH_CLIENT_ALLOW_NOTLS_PASSWORDS = 1配置參數。添加參數時要注意,一定要把該參數放到本配置小節的最開頭,這樣在后面才能有效引用該參數。

######################################################################
#                   AUTHENTICATION CONFIGURATION                     #
######################################################################

begin authenticators

….

# Because AUTH PLAIN and AUTH LOGIN send the password in clear, we
# only allow these mechanisms over encrypted connections by default.
# You can set AUTH_CLIENT_ALLOW_NOTLS_PASSWORDS to allow unencrypted
# clear text password authentication on all connections.

AUTH_CLIENT_ALLOW_NOTLS_PASSWORDS = 1

….

配置完成要重啟exim4服務器使配置生效。下面我們就可測試用smtp.21cn.com服務器發郵件啦。

debian:~# exim4 -v jims.yang@gmail.com
from:yjnet@21cn.com
to:jims.yang@gmail.com
subject:test from exim4
測試郵件正文。
.
LOG: MAIN
<= yjnet@21cn.com U=root P=local S=327
debian:~# delivering 1GQfnH-0001hE-PY
R: smarthost for jims.yang@gmail.com
T: remote_smtp_smarthost for jims.yang@gmail.com
Connecting to smtp.cdn.21cn.com [202.104.32.230]:25 … connected
SMTP<< 220 21cn.com SMTP Server 5 of AIMC 2.9.5.2 (DELAYED).
SMTP>> EHLO debian
SMTP<< 250-21cn.com, helo (DELAYED)
250-EXPN
250-HELP
250-8BITMIME
250-AUTH=LOGIN PLAIN
250-AUTH LOGIN PLAIN
250 XTMD
SMTP>> AUTH PLAIN ****************
SMTP<< 504 Unknown authentication mechanism
LOG: MAIN
plain authenticator failed H=smtp.cdn.21cn.com [202.104.32.230] 504 Unknown authentication mechanism
SMTP>> AUTH LOGIN
SMTP<< 334 VXNlcm5hbWU6
SMTP>> ********
SMTP<< 334 UGFzc3dvcmQ6
SMTP>> ********
SMTP<< 235 OK Authenticated
SMTP>> MAIL FROM:<yjnet@21cn.com> AUTH=root@debian
SMTP<< 250 Ok
SMTP>> RCPT TO:<jims.yang@gmail.com>
SMTP<< 250 User <jims.yang@gmail.com> is not local, will forward to <64.233.167.114:25>.
SMTP>> DATA
SMTP<< 354 Start mail input; end with <CRLF>.<CRLF>
SMTP>> writing message and terminating “.”
SMTP<< 250 Requested mail action okay, completed. Message-ID=<E1GQfnH-0001hE-PY@debian>
SMTP>> QUIT
LOG: MAIN
=> jims.yang@gmail.com R=smarthost T=remote_smtp_smarthost H=smtp.cdn.21cn.com [202.104.32.230]
LOG: MAIN
Completed

成功發送。

Related posts 相關文章
QUALYS 發現 Linux glibc 有嚴重漏洞,可以遠端控制系統 part2
More...
directadmin + exim 郵件的問題
More...
郵件伺服器 exim 的 log 記錄的名詞解釋
More...
郵件伺服器- exim 加上 postgrey 灰名單來擋垃圾郵件
More...

作者

留言

撰寫回覆或留言

發佈留言必須填寫的電子郵件地址不會公開。