搭建Sendmail 郵件系統 1 ☆郵件系統
電子郵件系統的工作原理
電子郵件系統的運作方式與其它的網絡應用有着根本上的不同。在其它的絕大多數的網絡應用中,網絡協定直接負責将資料發送到目的
地。而在電子郵件系統中,發送者并不等待發送工作完成,而是僅僅将要發送的内容發送出去。
電子郵件的協定标準是TCP/IP協定族的一部分。它規定了電子郵件的格式和在郵局間交換電子郵件的協定。
每個電子郵件都分為兩部分:郵件頭和郵件内容。TCP/IP對電子郵件的郵件頭的格式作了确切的規定,而将郵件内容的格式讓使用者自定義。
在郵件頭中最重要的兩個組成部分就是發送者和接收者的電子郵件位址。
電子郵件位址的格式如下:
使用者名 @ 電子郵局域名 例
:[email protected] 電子郵件的傳輸協定(也就是在郵局間交換電子郵件的協定)主要有
SMTP (簡單郵件傳輸協定)
、 POP (電子郵局協定),以及現在新興的
IMAP (網際網路郵件應用協定)。
整個電子郵件應用系統由兩大部分構成:
1. 電子郵局系統;
電子郵局行使着像傳統郵局的功能,它在發送者和接收者之間起着一個橋梁作用。它是運作在電子郵局伺服器上的一個伺服器端程式。
最常用的有
Microsoft 的
IIS 和
sendmail 等。
2. 電子郵件發送、接收系統。
電子郵件發送、接收系統則象遍及千家萬戶的郵箱,發送者和接收者通過它将郵件從電腦中發送和接收郵件。這個部分是一個運作在電
腦中的用戶端程式,最常用的有
Microsoft 的
Outlook Express , Netscape , The Bat , Foxmail , 方正飛揚等
。 有3種可提供的郵件服務
MTA ( Message Transfer Agent ): 消息傳輸代理(發送郵件伺服器)
sendmail, postfix, qmail MDA ( Mail Delivery Agents ): 郵件傳遞代理(郵件處理器)
procmail, maildrop MUA ( Mail User Agent ): 郵件使用者代理(郵件用戶端上運作的程式)
Mozilla, elm ☆郵件伺服器 郵件伺服器是電子郵件系統的核心構件,它的主要功能是發送和接收郵件,同時向發件人報告郵件的傳送情況。根據用途的不同,可以将
郵件伺服器分為發送郵件伺服器(
SMTP 伺服器)和接收郵件伺服器(
POP3 伺服器或
IMAP4 伺服器)。
sendmail 和
postfix 。您可以選擇任何一個MTA
Sendmail 并不處理最終的投遞,當然也不會處理如何把郵件送出給最終使用者這樣的任務。一般來說,我們總是在
Windows 客戶機器上處
理各種電子郵件,是以需要一個服務程式負責将
sendmail 存儲的郵件轉交給
Windows 或其他任何客戶機器。有兩種基本的方法,一種是
将郵件傳送到客戶的本地機器上處理,這是通過所謂的郵局協定實作的;另一種是允許使用者遠端操作其郵箱并且實作對郵件的浏覽和管理,
這是通過所謂的
IMAP 協定。
2 ☆ SMTP 協定
SMTP即簡單郵件傳輸協定,它是一組用于由源位址到目的位址傳送郵件的規則,由它來控制信件的中轉方式。SMTP協定屬于TCP/IP協定簇,它幫助每台計算機在發送或中轉信件時找到下一個目的地。通過SMTP協定所指定的伺服器,就可以把Email寄到收件人的伺服器上了。SMTP伺服器則是遵循SMTP協定的發送郵件伺服器,用來發送或中轉發出的電子郵件。
3 ☆ POP3 協定
POP3即郵局協定的第3個版本,它規定怎樣将個人計算機連接配接到Internet的郵件伺服器和下載下傳電子郵件的協定。它是Internet電子郵件的第一個離線協定标準,POP3允許從伺服器上把郵件存儲到本地主機即自己的計算機上,同時删除儲存在郵件伺服器上的郵件。遵循POP3協定來接收電子郵件的伺服器是POP3伺服器。
4 ☆ IMAP4 協定
IMAP4即Internet資訊通路協定的第4個版本,是用于從本地伺服器上通路電子郵件的協定,它是一個客戶/伺服器模型協定,使用者的電子郵件由伺服器負責接收儲存,使用者可以通過浏覽信件頭來決定是否要下載下傳此信。使用者也可以在伺服器上建立或更改檔案夾或郵箱,删除信件或檢索信件的特定部分
。 5 ☆ POP 和 IMAP 差異
雖然POP和IMAP都是處理接收郵件的,但兩者在機制上卻有所不同。在使用者通路電子郵件時,IMAP4需要持續通路伺服器,POP3則是将信件儲存在伺服器上,當使用者閱讀信件時,所有内容都會被立即下載下傳到使用者的機器上。是以,可以把IMAP4看成是一個遠端檔案伺服器,而把POP看成是一個存儲轉發伺服器。就目前情況看,POP3的應用遠比IMAP4廣泛得多。
6 ☆ mail
使用權限:所有使用者
使用方式
:mail [-iInv] [-s subject] [-c cc-addr] [-b bcc-addr] user1 [user 2 ...] 說明:
mail 不僅隻是一個指令,
mail 還 是一個電子郵件程式,不過利用
mail 來讀信的人應該很少吧!對于系統管理者來說
mail 就很有用,因為管理者可以用
mail 寫成
script ,定期寄一些備忘錄提醒系統的使用者。
參數:
i 忽略
tty 的中斷訊号
。 (interrupt) I 強迫設成互動模式。
(Interactive) v 列印出訊息,例如送信的地點、狀态等等。
(verbose) n 不讀入
mail.rc 設定檔。
s 郵件标題。
c cc 郵件位址。
b bcc 郵件位址。
将信件送給一個或以上的電子郵件位址,由于沒有加入其他的選項,使用者必須輸入标題與信件的内容等。而
user2 沒有主機位置,就會送給郵件伺服器的
user2 使用者。
mail [email protected] mail [email protected] user2 将
mail.txt 的内容寄給
user2 同時
cc 給
user1 。如果将這一行指令設成
crontab 就可以定時将備忘錄寄給系統使用者。
mail -s 标題
-c user1 user2 < mail.txt mail 檢視
/var/spool/mail/ 目錄下自已郵箱内容(每個使用者會有一個檔案儲存自己所有檔案)。以
q 退出把看過的郵件儲存在
~/mbox 檔案中。
mail [email protected] 直接發郵件給人。
mail -s 'title text' [email protected] < mail.txt 把文檔中内容郵寄出去。
mail -f ~/mbox 檢視
home 目錄下郵箱内容。
7 ☆ uuencode/uudecode
用
mail 發附件也是可行,要用到
uuencode and uudecode 指令進行編碼。
編碼
:uuencode [file] name uuencode hello >hello.uue default input is stdin;se default output is stdout. 解碼
:uudecode [-o outfile] name uudecode hello.uue 可以用
-o 選項輸出另外一個檔案名。
# uuencode ~/.bashrc -o bashrc | mail -s 'test uuencode' [email protected] 8 ☆ system-switch-mail
切換sendmail和postfix
9 ☆ sendmail
sendmai l提供SMTP服務
10 ☆ sendmail 安裝
如果你在安裝
RHEL5 的時候,選擇了
E-MAIL 服務,
sendmail 就已經安裝在
LINUX 系統中了,并且已經作了一些最基本的設定。
sendmail-8.13.8-2.el5.i386.rpm (第一張CD光牒,預設已裝)
sendmail軟體包是sendmail伺服器程式的安裝包,是最重要的軟體包.
m4-1.4.5-3.el5.1.i386.rpm (第一張CD光牒,預設已裝)m4軟體包中包括了配置sendmail伺服器的必要工具.
procmail-3.22-17.1.i386.rpm (第一張CD光牒,預設已裝)
sendmail-cf-8.13.8-2.el5.i386.rpm (第二張CD光牒,預設未裝)
sendmail-cf軟體包中包括了重新配置sendmail伺服器的必要配置檔案.
sendmail-doc-8.13.8-2.el5.i386.rpm (第三張CD光牒,預設未裝)
sendmail-doc軟體包中包括了sendmail伺服器的說明文檔.
sendmail-devel-8.13.8-2.el5.i386.rpm (第三張CD光牒,預設未裝)
管理者需要手動安裝以下兩個包,才能配置
sendmail 伺服器:
#rpm -ivh sendmail-cf-8.13.8-2.el5.i386.rpm sendmail-doc-8.13.8-2.el5.i386.rpm 設定運作級别:
#chkconfig --level 35 sendmail on 11 ☆ sendmail 使用前配置
sendmail 的
DNS 設定
當
sendmail 程式得到一封待發送的郵件的時候,它需要根據目标位址确定将信件投遞給那一個伺服器,這是通過
DNS 服務實作的。例如,有一封郵件的目标位址是
[email protected] ,那麼,
sendmail 首先确定這個位址是使用者名
(panda)+ 機器名
(panda.com) 的格式,然後,通過查詢
DNS 來确定需要把信件投遞給某個伺服器。
先用
ifconfig 檢視伺服器的
ip, 再把
ip 寫入:
#vi /etc/resolv.conf ; generated by /sbin/dhclient-script search panda.com nameserver 192.168.152.128 确認
/etc/hosts, #vi /etc/hosts # Do not remove the following line, or various programs # that require network functionality will fail. 127.0.0.1 panda.panda.com panda panda 然後在
/etc/named.conf 中加入
zone "panda.com" IN { type master; file "panda.com.zone"; }; 最後生成
/var/named/chroot/var/named/panda.com.zone $TTL 86400 @ IN SOA panda.panda.com. root.panda.panda.com. ( 2006062700 ; Serial 28800 ; Refresh 14400 ; Retry 3600000 ; Expire 86400 ) ; Minimum IN NS panda IN MX 10 panda panda IN A 192.168.152.128 重新開機
DNS. #service named restart DNS 資料中,與電子郵件相關的是
MX 記錄,這可以在查詢
DNS 時設定查詢類型為
mx 來得 到:
[[email protected] ~]# nslookup > panda.panda.com Server: 192.168.152.128 Address: 192.168.152.128#53 Name: panda.panda.com Address: 192.168.152.128 > set q=mx > panda.com Server: 192.168.152.128 Address: 192.168.152.128#53 panda.com mail exchanger = 10 panda.panda.com. 在一般的情況下,
mx 交換器會自動把信件内容轉交給目标主機,不過,也存在這樣的情況 ,目标主機(比如panda.com)可能并不存在,或者不執行
smtp 服務,而是由其
mx 交換器來執行信件的管理,這時候,最終的信件将儲存在
mx 機器上,直到使用者來察看它。
如果
DNS 查詢無法找出對某個位址的
MX 記錄(通常因為對方沒有信件交換主機),那麼
sendmail 将是試圖直接與對方的主機(來自郵件位址)對話并且發送郵件。例如,
[email protected] 在
DNS 中沒有對應的
MX 記錄,是以
sendmail 在确定
MX 交換器失敗後,将從
DNS 取得對方的
IP 位址并直接和對方對話試圖發送郵件。
sendmail 發送郵件時,如果經過設定的時間後仍然未能将信件投遞到目的主機,它将傳回一個錯誤資訊并且休息一段時間,然後重新試圖投遞,如果連續多次失敗,
sendmail 最終将放棄投遞并将錯誤資訊投遞給
postmaster 使用者。在許多機器上,
postmaster 使用者是
root 使用者的一個别名(參考下面關于别名的内容),你應該将它設定為郵件的實際管理者的使用者名。
例如,你可能會用mail程式向某個位址這樣發信:
$mail [email protected] To:[email protected] Subject:test mail test [email protected] 必須準确到某台主機上
那麼,當
sendmail 發信的時候,它是向
[email protected] 發信而不是
[email protected] 。相應地,如果你想向兩個人發信,例如你在
outlook 裡面寫上:"投遞給
[email protected] ,抄送
[email protected] ",那麼
sendmail 應該怎麼做?直接同時向兩個位址發信嗎?否,它試圖構造兩個包裝(稱為信封),每個包裝上隻列出一個投遞位址,各投遞一次。雖然郵件正文的頭部仍然包含兩個位址,但是
sendmail 不會看它。
配置檔案
/etc/sysconfig/sendmail DAEMON=yes QUEUE=1h 每一個小時傳遞隊列中的檔案
12 ☆ sendmail 防火牆
iptables -I INPUT -p tcp --dport 25 -j ACCEPT 13 ☆ sendmail 啟動
啟動用:
#service sendmail start ; #/etc/init.d/sendmail start ; 或
# sendmail -bd -q30m 該指令以背景程序方式
(-bd) 運作,并使其每隔
30 分鐘
(-q30m) 輪詢一次未發送郵件隊列,檢查是否有新郵件。
-q1h 表示每隔一個小時發送一次郵件
停止用:
#service sendmail stop ; #/etc/init.d/sendmail stop ; #service sendmail restart ; 14 ☆ sendmail 主要配置檔案
#ls /etc/mail/ access helpfile Makefile submit.mc access.db local-host-names sendmail.cf trusted-users domaintable mailertable sendmail.mc virtusertable domaintable.db mailertable.db submit.cf virtusertable.db #ls /etc/aliases* /etc/aliases /etc/aliases.db 在
/etc/mail 目錄中儲存了
sendmail 伺服器使用的所有配置檔案,檔案字尾是
".cf" 的是伺服器配置檔案,檔案字尾是
".db" 的是伺服器使用的資料庫檔案,下面是常用的檔案說明:
配置檔案sendmail.cf和sendmail.mc
sendmail 伺服器的主配置檔案是
sendmail.cf ,該檔案的格式是公認的難于了解,是以即使是有經驗的管理者也會避免直接編輯此檔案,
sendmail 開發人員為
sendmail 管理者提供了更加容易了解和配置的
sendmail.mc 檔案。一般情況下,系統管理者對
sendmail.mc 檔案的内容進行設定後,再使用
m4 指令由
sendmail.mc 檔案的内容生成
sendmail.cf 檔案。但由于
sendmail.mc 檔案的配置依然有一定的難度
, 是以在第一次對
sendmail.mc 檔案進行修改前,最好先将
該檔案進行備份,以便配置錯誤時能夠恢複預設配置,操作流程如下:
#cd /etc/mail (進入主配置檔案目錄)
#cp sendmail.mc sendmail.mc- ( 備份配置檔案
sendmail.mc) #vi sendmail.mc ( 編緝配置檔案
sendmail.mc )
#m4 sendmail.mc > sendmail.cf (利用
m4 指令重新生成主配置檔案
sendmail.cf )
#service sendmail restart (重新開機
sendmail 服務使配置生效)
注意: sendmail 伺服器在運作時隻讀取
sendmail.cf 檔案中的設定,是以每次對
sendmail.mc 檔案進行修改後都需要使用
m4 指令生成新的
sendmail.cf 檔案,
而且
m4 指令必須在
/etc/mail/ 目錄中執行。
資料庫配置檔案和資料庫檔案
在
sendmai l伺服器中除了
sendmail.cf 主配置檔案以外,還使用了許多資料庫檔案輔助進行
sendmail 伺服器的配置。在
/etc/mail 目錄中所有擴充名為
".db" 的
檔案都是資料庫檔案,這些資料庫檔案的格式對于使用者是不可讀的。每個
".db" 資料庫檔案在同一目錄下都對應有同名的無擴充名檔案,例如對于
access.db 檔案,對應有名為
access 的文本檔案,
access 檔案是用于系統管理者進行管理的,稱為資料庫配置檔案。當管理者在資料庫配置檔案(文本檔案)中進行了相
應的設定後,需要使用
makemap 指令将資料庫配置檔案生成
".db" 的資料庫檔案,因為
sendmail 伺服器隻能夠識别
".db" 的資料庫檔案中的配置内容。操
作流程如下:
#cd /etc/mail (進入資料庫檔案目錄)
#vi access (編輯資料庫配置檔案)
#makemap hash access.db <access (用makemap指令重新生成資料庫檔案)
#service sendmail restart (重新開機sendmail服務讓配置生效)
☆ sendmail.mc
sendmail.mc檔案中指令的意義:
指令 | 意義 |
divert (-1) | 删除結果檔案中的垃圾 |
divert (0) | 儲存通常的配置檔案 |
Dnl | 注解 |
Include | 包含一個檔案 |
define arg1 arg2 | 定義宏arg1設定為arg2 |
undefine arg1 | 取消arg1的定義 |
OSTYPE ('Linux') | 定義作業系統類型 |
FEATURE | 功能選項,對應/usr/lib/sendmail-cf/feature裡定義的檔案名稱 |
MAILER | 定義所有必要的郵件投遞代理程式 |
ASQUERADE_AS | 用來處理多主機名稱問題 |
divert(-1)dnl
dnl #
dnl # This is the sendmail macro config file for m4. If you make changes to
dnl # /etc/mail/sendmail.mc, you will need to regenerate the
dnl # /etc/mail/sendmail.cf file by confirming that the sendmail-cf package is
dnl # installed and then performing a
dnl # make -C /etc/mail
include(`/usr/share/sendmail-cf/m4/cf.m4')dnl
VERSIONID(`setup for linux')dnl
OSTYPE(`linux')dnl //設定使用的作業系統類别
dnl # Do not advertize sendmail version.
dnl define(`confSMTP_LOGIN_MSG', `$j Sendmail; $b')dnl
dnl # default logging level is 9, you might want to set it higher to
dnl # debug the configuration
dnl define(`confLOG_LEVEL', `9')dnl
dnl # Uncomment and edit the following line if your outgoing mail needs to
dnl # be sent out through an external mail server:
dnl define(`SMART_HOST', `smtp.your.provider')dnl
define(`confDEF_USER_ID', ``8:12'')dnl
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/mail/statistics')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
dnl # The following allows relaying if the user authenticates, and disallows
dnl # plaintext authentication (PLAIN/LOGIN) on non-TLS links
dnl define(`confAUTH_OPTIONS', `A p')dnl
dnl # PLAIN is the preferred plaintext authentication method and used by
dnl # Mozilla Mail and Evolution, though Outlook Express and other MUAs do
dnl # use LOGIN. Other mechanisms should be used if the connection is not
dnl # guaranteed secure.
dnl # Please remember that saslauthd needs to be running for AUTH.
ddnl TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
dnl define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
dnl # Rudimentary information on creating certificates for sendmail TLS:
dnl # cd /usr/share/ssl/certs; make sendmail.pem
dnl # Complete usage:
dnl # make -C /usr/share/ssl/certs usage
dnl define(`confCACERT_PATH', `/etc/pki/tls/certs')dnl
dnl define(`confCACERT', `/etc/pki/tls/certs/ca-bundle.crt')dnl
dnl define(`confSERVER_CERT', `/etc/pki/tls/certs/sendmail.pem')dnl
dnl define(`confSERVER_KEY', `/etc/pki/tls/certs/sendmail.pem')dnl
dnl # This allows sendmail to use a keyfile that is shared with OpenLDAP's
dnl # slapd, which requires the file to be readble by group ldap
dnl define(`confDONT_BLAME_SENDMAIL', `groupreadablekeyfile')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
define(`confTO_IDENT', `0')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.db')dnl
FEATURE(`virtusertable', `hash -o /etc/mail/virtusertable.db')dnl
FEATURE(redirect)dnl
FEATURE(always_add_domain)dnl
FEATURE(use_cw_file)dnl
FEATURE(use_ct_file)dnl
dnl # The following limits the number of processes sendmail can fork to accept
dnl # incoming messages or process its message queues to 20.) sendmail refuses
dnl # to accept connections once it has reached its quota of child processes.
dnl define(`confMAX_DAEMON_CHILDREN', `20')dnl
dnl # Limits the number of new connections per second. This caps the overhead
dnl # incurred due to forking new sendmail processes. May be useful against
dnl # DoS attacks or barrages of spam. (As mentioned below, a per-IP address
dnl # limit would be useful but is not available as an option at this writing.)
dnl define(`confCONNECTION_RATE_THROTTLE', `3')dnl
dnl # The -t option will retry delivery if e.g. the user runs over his quota.
FEATURE(local_procmail, `', `procmail -t -Y -a $h -d $u')dnl
FEATURE(`access_db', `hash -T<TMPF> -o /etc/mail/access.db')dnl
FEATURE(`blacklist_recipients')dnl //允許使用黑名單查禁收件人。是以使用者可以在access檔案中設定黑名單。
EXPOSED_USER(`root')dnl
dnl # For using Cyrus-IMAPd as POP3/IMAP server through LMTP delivery uncomment
dnl # the following 2 definitions and activate below in the MAILER section the
dnl # cyrusv2 mailer.
dnl define(`confLOCAL_MAILER', `cyrusv2')dnl
dnl define(`CYRUSV2_MAILER_ARGS', `FILE /var/lib/imap/socket/lmtp')dnl
dnl # The following causes sendmail to only listen on the IPv4 loopback address
dnl # 127.0.0.1 and not on any other network devices. Remove the loopback
dnl # address restriction to accept email from the internet or intranet.
DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl
dnl # The following causes sendmail to additionally listen to port 587 for
dnl # mail from MUAs that authenticate. Roaming users who can't reach their
dnl # preferred sendmail daemon due to port 25 being blocked or redirected find
dnl # this useful.
dnl DAEMON_OPTIONS(`Port=submission, Name=MSA, M=Ea')dnl
dnl # The following causes sendmail to additionally listen to port 465, but
dnl # starting immediately in TLS mode upon connecting. Port 25 or 587 followed
dnl # by STARTTLS is preferred, but roaming clients using Outlook Express can't
dnl # do STARTTLS on ports other than 25. Mozilla Mail can ONLY use STARTTLS
dnl # and doesn't support the deprecated smtps; Evolution <1.1.1 uses smtps
dnl # when SSL is enabled-- STARTTLS support is available in version 1.1.1.
dnl # For this to work your OpenSSL certificates must be configured.
dnl #
dnl DAEMON_OPTIONS(`Port=smtps, Name=TLSMTA, M=s')dnl
dnl # The following causes sendmail to additionally listen on the IPv6 loopback
dnl # device. Remove the loopback address restriction listen to the network.
dnl DAEMON_OPTIONS(`port=smtp,Addr=::1, Name=MTA-v6, Family=inet6')dnl
dnl # enable both ipv6 and ipv4 in sendmail:
dnl DAEMON_OPTIONS(`Name=MTA-v4, Family=inet, Name=MTA-v6, Family=inet6')
dnl # We strongly recommend not accepting unresolvable domains if you want to
dnl # protect yourself from spam. However, the laptop and users on computers
dnl # that do not have 24x7 DNS do need this.
FEATURE(`accept_unresolvable_domains')dnl //接受無法反向解析的域來的郵件
dnl FEATURE(`relay_based_on_MX')dnl //自動接受DNS中MX記錄來源的郵件轉發
dnl # Also accept email sent to "localhost.localdomain" as local email.
LOCAL_DOMAIN(`localhost.localdomain')dnl
dnl # The following example makes mail from this host and any additional
dnl # specified domains appear to be sent from mydomain.com
dnl MASQUERADE_AS(`mydomain.com')dnl
dnl # masquerade not just the headers, but the envelope as well
dnl FEATURE(masquerade_envelope)dnl
dnl # masquerade not just @mydomainalias.com, but @*.mydomainalias.com as well
dnl FEATURE(masquerade_entire_domain)dnl
dnl MASQUERADE_DOMAIN(localhost)dnl
dnl MASQUERADE_DOMAIN(localhost.localdomain)dnl
dnl MASQUERADE_DOMAIN(mydomainalias.com)dnl
dnl MASQUERADE_DOMAIN(mydomain.lan)dnl
MAILER(smtp)dnl
MAILER(procmail)dnl
dnl MAILER(cyrusv2)dnl
☆ mc 文法
一個
mc 模闆檔案通常可以包含幾個段落:
注意裡面的正反引号,而且不能随便加入空格。
sendmail 宏定義說明
divert(n) 為
m4 定義一個緩沖動作,當n=-1時緩沖被删除,n=0時開始一個新緩沖
OSTYPE 定義宏所使用的作業系統,該宏允許m4程式增加同相關作業系統相關的檔案
Domain 定義MTA将使用哪些域來傳輸郵件
Feature 定義配置檔案中使用的一個特定的功能集
Define 定義配置檔案中的一個特定的選項值
MASQUERADE_AS 定義sendmail來應答郵件的其它主機名
MAILER 定義sendmail使用的郵件傳輸方法
dnl 注釋
divert 通常總是設定為
divert(-1) 讓
m4 在輸出中去掉一些垃圾。
OSTYPE OSTYPE 定義使用的作業系統類型,當然在我們的情況下就是
linux , 但是一定要注意
m4 程式中引号的用法,一個反引号和一個正引号才代表把對應的東西括起來。
define 定義一些全局設定,對于
Linux 系統,設定了
OSTYPE 之後,可以定義下面的一些全局 參數,如果不定義,就使用預設值。例如:
define(ALIAS_FILE , /etc/aliases) 變量名 說明(方括号中為預設值)
ALIAS_FILE [/etc/aliases] 别名檔案的位置。如果有多個别名檔案,需要把它們用引号括起來(别忘了引号規則 !)。
confCR_FILE [/etc/mail/relay-domains] 預設的域定義檔案,在這個域中定義的域中機器可以通過你的伺服器進行郵件發送。
HELP_FILE [/usr/lib/sendmail.hf] 此檔案中含有對
SMTP 的
HELP 指令進行響應時要列出的資訊。
QUEUE_DIR [/var/spool/mqueue] 郵件隊列檔案所在目錄。
STATUS_FILE [/etc/sendmail.st] sendmail 的狀态資訊檔案。
LOCAL_MAILER_PATH [/bin/mail] 用于投遞本地郵件的程式。
LOCAL_MAILER_FLAGS [rmn9] local mailer 要用到的标志,永遠包含标志
lsDFM 。 LOCAL_MAILER_ARGS [mail -d $u] 在投遞本地郵件時所傳送的參數。
LOCAL_MAILER_MAX [沒有]
如定義了此參數,則為此郵件伺服器所能接收最大單個郵件大小。
LOCAL_MAILER_CHARSET [沒有]
如果定義了此參數,則被轉化為
MIME 格式的從其他位址到
local mailer 的含有
8 位字元 的資訊将被标為此字元集。
LOCAL_SHELl_PATH [/bin/sh] 用于投遞利用管道功能處理的郵件的
shell. LOCAL_SHELL_FLAGS [eu9] prog mailer 用到的标志。在此标志中永遠包含标志
lsDFM. LOCAL_SHELL_DIR [$z:/] shell 運作時所要查找的目錄路徑。
USENET_MAILER_PATH [/usr/lib/news/inews] 用于投遞電子新聞討論區的程式名稱。
USENET_MAILER_FLAGS [rlsDFMmn] usenet mailer 的投遞标志。
USENET_MAILER_ARGS [-m -h -n] usenet mailer 的指令行參數。
USENET_MAILER_MAX [100000] usenet mailer 所能接收的最大資訊大小。
SMTP_MAILER_FLAGS [沒有]
SMTP mailer 附加标志。對所有基于
SMTPmailer 其預設标志為
mDFMUX; 基于
esmtp 的郵差
(mailer) 加上
a 标志;而基于
"smtp8" 的郵差則加上
8 。 SMTP_MAILER_MAX [沒有]
使用
smtp,smtp8 或
esmtp 傳輸的單個郵件最大容量。
SMTP_MAILER_CHARSET [沒有]
如果定義了此參數,則被轉化為
MIME 格式的從其他位址到任一個
smtp mailer 的含有
8 位字元的資訊将被标為此字元集。
POP_MAILER_PATH [/usr/lib/mh/spop] pop 郵差的路徑名。
POP_MAILER_FLAGS [Penu] pop 郵差附加标志。同時總是加上标志
lsDFM 。 POP_MAILER_ARGS [pop $u] 傳給
pop 郵差的參數。
PROCMAIL_MAILER_PATH [/usr/local/bin/procmail] procmail 程式的路徑名。此外
FEATURE(local procmail) 也用到此參數。
PROCMAIL_MAILER_FlAGS [SPhnu9] 加給
Procmail 郵差的标志。同時總是加上
"DFM" 标志
。 PROCMAIL_MAILER_MAX [ 沒有
] procmail 所接收的最大單個郵件容量。如果你對某些人發送巨大的郵件感到困擾,啟 用這個選項。
FEATURE 定義
sednamil 的一些運作參數,通常對我們來說最重要的一些選項是:
use_cw_file 讀取檔案
/etc/sendmail.cw 以确定這台機器應該替哪些機器接受郵件。此主機的别名 。當你使用
MX 記錄将此主機定義為其他主機的郵件交換機時需要使用這個特性。例
如 : FEATURE(use_cw_file) relay_hosts_only 通常情況下,
sendmail 為
sendmail.cf 中明确列出的域
( 一般是
localhost) 和
/etc/ mail/relay-domains 中定義的域進行投遞代理。預設下這兩處定義的都是域的名字。如果你定義了這個參數,那麼這兩處的内容将被解釋為主機名字。
use_ct_file 讀取檔案
/etc/sendmail.ct 以取得系統
" 信任
" 的使用者名字,這些使用者可以使用
-f 設定其發信信封上的
from 位址而不産生警告資訊。
redirect 使用
REDIRECT 特性,這個特性允許你對某些已經搬遷的使用者發出重定向資訊。(見下 一節)。
例如:
FEATURE(redirect) mailertable 包含一個用于覆寫到特定域路由
(routing) 的
"mailer table". 此特性參數定義可以是 一個關鍵詞定義。如未指定任何參數, 其定義通常是:
FEATURE(mailertable,`hash -o /etc/mailertable) domaintable 包含一個用于提供域名映象的
"domain table" , 當改變你自已的域名時可能有用
( 如 你公司由
oldname.com 改為
newname.com) 。 其定義通常是:
FEATURE(domiaintable , `hash -o /etc/domaintable) always_add_domain 在本地發送郵件時也加上其主機域名。例如:
FEATURE(always_add_domain) allmasquerade 如果使用了僞裝
(masquerading, 使用
MASQUERADE_AS), 則此特性将使接收者的位址也僞 裝為來自所僞裝為主機。
limitd_masquerade 通常情況下
$w 所列出的所有主機将被僞裝。如果使用了此特性,則隻對那些
$m 所列出的主機進行僞裝。
masquerade_entire_domain 如使用了僞裝且設定了
MASQUERADE_DOMAIN, 此特性将引起 位址重寫,使所要僞裝的網 域整個被隐藏。所有含有被僞裝域名的主機用僞裝域名(通常是
MASQUERADE_AS) 進行重寫 。
masquerade_envelope 用此特性告知
sendmail 将信封和信件頭中上的發送者和接收者進行僞裝。
定義了有關
masq 的選項之後,就可以使用僞裝了,可以直接将僞裝指令寫入
mc 模闆, 示例如下:
MASQUERADE_AS(masq.com) MASQUERADE_DOMAIN(foo.org) 這意味着我們的
[email protected] 發信的時候,
sendmail 将會把它的信封僞裝為
some [email protected] 。 這對于統一整個域的電子郵件是非常重要的。
virtusertable 允許在同一個主機上使用多個虛拟域。參考下一節。例如:
FEATURE(`virtusertable,`hash -o /etc/mail/virtusertable) nullclient 這是一個特殊情況--它生成一個除了支援将所有的郵件通過本地的基于
SMTP 的網絡轉 遞到一個中心郵件
HUB 之外不含任何内容的配置檔案。其參數是此郵
件 HUB 的主機名。唯 一可與
nullclient 一起使用的其他特性是
"nocanonify" (這樣可以使非完全位址可通過
S MTP 連接配接進行發送;通常情況下位址将使用僞裝名字轉變為完全郵件名稱,此僞裝名字默 認值為郵件
HUB 主機的名字)。 在此特性使用是不應定義任何郵差。當然也不進行别名 處理或轉寄。
local_procmail 使用
procmail 作為本地郵差。
smrsh 對到程式的郵件使用使用
sendmail 發行版所帶的
SendMail Restricted SHell (smrsh ) 而不是
/bin/sh 。 由于
sendmail 是以
root 權限執行,某個發送到惡意程式的郵件可以破 壞系統,隻要利用别名轉向使得郵件被轉發到對應的程式,是以預設下
sendmail 用
smrsh 來處理郵件轉發到程式的請求。這可以提高本地系統管理者控制對那些通過郵件運作程 序的行為,例如
FEATURE(`smrsh,`/usr/sbin/smrsh) 注意有些程式無法通過
smrsh 運作(例如
majordomo 的
wrapper 程式),這是出于安全 性的考慮
, smrsh 不準使用者程式使用一些
setuid 功能。如果你一定要使用這些程式,清将
smrsh 定義成其他
shell 程式,如
sh 。 access_db 本地存取控制檔案的名字,預設是
/etc/mail/access.db , 也可以用指令行指出,例 如:
FEATURE(`access_db) 或者 或者
FEATURE(`access_db,`hash -o /etc/mail/access) mailertable 允許使用
mailertable 檔案。這個檔案定義對某确定的域使用什麼樣的郵差。例如:
FEATURE(`mailertable' , `hash -o /etc/mail/mailertable) 。 blacklist_recipients 允許你用前面定義的
access_db 來禁止某個地方來的郵件,或是某個人的郵件,等等 。
relay_based_on_MX 是否允許别人用你的機器當成MX交換器。如果你設定了這個選項,那麼任何人隻要在 域名伺服器中将你的機器設定成為他的MX交換就可以用你的機器轉發電子郵件。這個功 能意味着:你的機器替它接受電子郵件,再送出給它;一般來說這個功能是不必要的; 如果你一定要使用這個選項,記住你可能被龐大的郵件流量吞沒。但是在一種情況下這 個功能又是不可缺少的:假如你的系統有防火牆,隻有郵件伺服器能夠對外連接配接,那麼 這個功能是使網絡内部其他主機能夠接受自己電子郵件的唯一方法。
DOMAIN 這個關鍵字一般用來定義郵件中繼,假如你的系統裡面除了
Internet 互連之外還有類似
Decnet , UUCP 之類的東西,那麼你就需要設定
DOMAIN 來保證非
internet 的郵件被正确 中繼。對于一般的系統,不需要定義這個屬性。
MAILER 定義可以使用的投遞程式(郵差)。
例如:
MAILER(smtp) 定義
smtp 投遞。
MAILER(local) 定義局部投遞。
如果你想做郵件服務,這樣兩個郵差是必須的。
☆ sendmail.cf
#m4 sendmail.mc > /etc/sendmail.cf sendmail.cf文法複雜,内容繁多,它的配置資訊和選項用于限定sendmail守護程序的運作。由于太複雜,很難逐條去建立它,而是通過用m4宏預處理器對
有限而直覺的.mc配置宏檔案進行處理生成的。
(1)sendmail.cf檔案構成
sendmail.cf檔案由定義不同内容的多個小節構成,這些小節的作用見下表:
小節 | 功能 |
Local Information | 定義有關個人主機的資訊 |
General Macros | 定義有關本地網絡的宏 |
Classes | 定義用于特殊郵件傳輸程式的主機名群或域名群 |
Version Number | 辨別sendmail.cf檔案的版本号 |
Special Macros | 定義由sendmail所用的一些特殊的宏 |
Option | 定義sendmail選項 |
Message Precedence | 定義sendmail所用的各種消息的優先級值 |
Trusted Users | 定義在發送郵件時可以忽略發送者位址的使用者 |
Format of Headers | 定義sendmail插入的郵件首部格式 |
Rewriting Rules | 定義用于重寫郵件位址的規則 |
Mailers | 定義sendmail用來調用郵件傳輸程式的指令 |
Ruleset Zero | 定義一組稱為ruleset zero的重寫規則 |
Machine-dependent part of Ruleset Zero | 定義與配置相關的Ruleset Zero部分 |
(2)sendmail.cf檔案的指令
sendmail.cf檔案的每一行都以一個指令字元打頭,後邊是具體的設定内容。指令字元說明了本行的作用,設定内容說明了設定的對象,空行将被忽略。
如果行的第一個字元是空格,說明是上一行的繼續行。所有指令的意義如下:
# :說明了本行是注釋行;
D :宏定義操作符;
用法:DK [字元串],這就将宏K定義成了後面的字元串,可以在配置檔案中對它進行引用,避免了多次輸入某一相同的字元串。
提示:宏的名字應該是單獨的一個大寫字母,因為sendmail用小寫字母定義自己的宏變量。
C 和F :類定義操作符;
用法:CK[類名]或者FK檔案名,這就定義了類K或者定義了從指定的檔案中讀取類K的值。
提示:一個類可以包含一個或多個單詞,也要使用單獨的大寫字母,原因同上。
H :郵件頭定義操作符,一般不用對配置檔案中預設提供的郵件頭做任何修改;
O :設定選項操作符,sendmail具有多種可以設定其操作的選項,還可以通過這些選項來告訴sendmail,所要使用的檔案放在什麼位置。當然這些選項也
可以通過指令行來提供,兩種方法是等價的,人們常常将很少改變的設定在配置檔案中通過O操作符給出;
P :優先級操作符,用來指定郵件的優先級别。另外,sendmail還指定了一些具體的郵件标題,這些特定的郵件标題本身就具有了确定的優先級。例如:
Pspecial-delivery = 100
Pfirst-class = 0
Plist = -30
Pjunk = -100
V :sendmail.cf的版本級别操作符,它能夠使sendmail知道在配置檔案中可以找到哪些特性;
提示:sendmail.cf的版本級别和Sendmail的版本級别不是一回事。
K :關鍵字資料庫操作符,因為sendmail使用了一些關鍵字資料庫,例如别名庫等等。K操作符可以用來告訴sendmail這些可利用的資料庫的位置或類别
等類似資訊,預設支援DBM格式的庫,一般可以支援DBM,BTREE,HASH,NIS等。
M :郵件發送器的操作符,對于每一個目标,可以定義一個專門的郵件發送器。郵件發送器将通過定義在其中的sendmail固定的SMTP傳輸器,把郵件
發送給其他主機。所有郵件發送器都是用M操作符和郵件發送器的名字開頭的一行來定義,舉例如下:
Mlocal, P=/bin/mail, F=lsDFMfSn, S=10, R=20, A=mail-d $ u 在上面本地郵件發送器的定義中:
P操作項用來提供投遞郵件的程式所處的位置的路徑名稱;
F用來為本地郵件發送器提供sendmail标志;
A項用來為運作的程式(這裡是/bin/mail)提供指令行,是以sendmail将運作指令:mail-d $u,其中的宏$u将被替換為郵件應該被發送的使用者的使用者名。
S和R是規則集操作符。規則集用來發現位址中的錯誤,将位址改寫為遠端郵件發送器能夠了解的形式,以及将郵件解析到sendmail内部的某個郵
件發送器。sendmail将按固定的順序向規則集傳送位址,規則集也可以調用其他的規則集。規則集由S來指定,S的後面
是用來表示規則集的編号。
(3)sendmail.cf中的預定義宏
sendmail.cf中的預定義宏參見下表:
宏名 | 意義 |
a | RFCs822格式的原始日期 |
b | RFCs822格式的目前日期 |
c | Hop計數 |
d | UNIX格式的日期 |
e | SMTP資料項資訊 |
f | 郵件發送者位址 |
g | 相對于接收者的發送者位址 |
h | 接收主機 |
i | 隊列辨別 |
j | 節點的正式域名 |
l | UNIX的行格式 |
n | 用于錯誤資訊中的名字 |
o | 位址中的操作符集合 |
p | sendmail的PID |
q | 預設的發送者格式 |
r | 所用的協定 |
s | 發送者的主機名 |
t | 目前時間的數值表示 |
u | 接收方使用者 |
v | sendmail的版本号 |
w | 該節點的主機名 |
X | 發送者全名 |
z | 接收者主目錄 |
☆ acces
限制某些域的郵件,
/etc/mail/access 通路控制清單設定檔案的格式是這樣:
[ 位址 ] [ 操作 ] 中間的分割符是空格鍵。
[ 位址 ] 欄 可以是主機位址或者名字,也可以是統配符,規則是這樣:
yourdomain.com : 代表所有
*.yourdomain.com 的名字。
192.168.12 : 代表所有
192.168.12.* 的位址。
202.135 : 代表所有
202.135.*.* 的位址。
[email protected] : 代表一個特定的郵件發信人
[ 操作 ] 欄通常有 : OK : 正常接受這封郵件,遠端主機可以向你的郵件伺服器發送郵件;
RELAY : 允許SMTP代理投遞,這樣這封郵件就可以從你的機器中轉到别的機器上去; 例如:
127.0.0.1 RELAY 192.168.10 RELAY 表示允許本機和192.168.10.0網段中的機器通過該郵件伺服器轉發郵件。
REJECT : 拒絕接受,不能向你的郵件伺服器發郵件和不能中轉;
DISCARD : 發來的郵件将被丢棄,同時并不向發送者傳回錯誤資訊。忽略這封郵件,這種情況下,郵件看上去是正常投遞了,但是由
于沒有人接受,郵件會自動地"消失"在網絡中。
nnn text-- : 發來的郵件将被丢棄,但
sendmail 将會向發送者傳回
nnn 确定的
smtp 代碼和
text 變量确定的文本描述。
錯誤代碼 + 任何其他字元串 : 将向發信者傳回這個字元串作為出錯資訊。錯誤代碼是RFC 822定義的标準出錯代碼。例如
550 We don like a spammer! 。客戶機器在投遞郵件的時候,就會産生一個
"we don like a spammer" 投遞失敗信
息。比如,你認為
[email protected] 是個專門投遞垃圾郵件的家夥,那麼你可以這樣寫
: [email protected] 550 we don like a spammer , 然後重新啟動sendmail就可以使用這些功能了。
192.168 RELAY panda.NET OK panda.COM REJECT panda.COM 550 SORRY,WE DON'T ALLOW SPAMMERS HERE panda.ORG DISCARD 設定完成後要用
#makemap hash access.db < access 指令生成資料庫。
☆ access.db
一般
/etc/mail/access.db 是一個散清單資料庫,它是用
/etc/mail/access 為模版産生出來的。
☆ aliases
/etc/aliases , 這個檔案用來設定使用者的别名。檔案
/etc/aliases 允許為本地使用者,應用程式,甚至其他别名提供虛拟郵箱:
aliases 檔案的格式是
郵件别名 : 實際使用者名 如果一個别名有多個使用者就用逗号分開 ,每個别名一行。
1. 最簡單的情況是需要作信件分發的情況 : 要把發給
postmaster 的信件發送給
supervisor 和
manager , 需要寫上這樣一行:
postmaster:supervisor,manager 别名還可以用在這樣的情況,即定義自動的郵件轉發。 某個使用者以前在你的系統上接受電子郵件,現在他有了一個新的電子郵件,希望發到你的機器上的郵件自動被轉發到他新的電子郵件位址上,那麼,可以使用類似這樣的别名方式:(假設你的機器是
[email protected] )
panda:[email protected] 以後發給
[email protected] 的電子郵件就自動中轉到
[email protected] 。 注意左邊自動加上你的機器名字,是以左邊隻能是賬号名字,不能是全限定郵件位址。
2. 别名的右側也可以是檔案或程式。 上面的
postmaster 别名可以用這樣方法來設定
: panda: :include: /etc/mail/myaliases include: 關鍵字表示讓
sendmail 去讀取對應的包含檔案。而
/etc/mail/myaliases 的内容要設定成:
pandeng manager 3. 要把郵件重定向到程式,可以使用管道 panda:"|/home/panda/testpg" 那麼,
sendmail 會将發給
panda 的郵件的内容作為
/home/panda/testpg 程式的輸入來執行這個程式。
另一個常用的辦法是重定向。如果你在模闆檔案中定義了
REDIRECT 特性,那麼可以使用這個功能。
某個人在你的機器上開了一個賬戶
user1 , 後來遷移到
[email protected] 。那麼,你可以将其别名寫成
user1: [email protected] 以後當有人向這個位址發信的時候,你的
sendmail 會将其退回,并且傳回一個
551 User not local; please try [email protected] 的資訊。
在使用别名的時候,必須注意的是不要造成循環,例如
user1 轉發給
user2 , user2 又将 其轉發給
user1.... 如此循環。在這種情況下,轉發17次後,
sendmail 将把它退還給發信 人。最常見的錯誤發生在你試圖在轉發郵件的同時在本地保留備份的情況下,例如:
user1: user1,user2 就構成了一個循環。
在修改了别名檔案之後,重新初始化别名資料庫:
[[email protected] mail]# newaliases /etc/aliases: 17 aliases, longest 31 bytes, 241 bytes total
也可以使用
sendmail -bi 指令:
[[email protected] mail]# sendmail -bi /etc/aliases: 17 aliases, longest 31 bytes, 241 bytes total
兩種方式實際是完全一樣的。
目前版本的
sendmail 對各種附加檔案和配置檔案的屬性都有着嚴格的要求,特别是
/etc/aliases 檔案,必須至少為
0644 以避免非授權的修改。
☆ .forward
~/.forward 其實該文檔的作用和
aliases 資料庫的作差不多啦,都是配置别名,做郵件轉發的。因為
alises 隻能由管理者控制,個人使用者不能修改,是以就可以在test個人的目錄下建立一個轉寄文檔。以設定個人的郵件轉寄清單。文檔格式如下:
test test1 test2 test3 [email protected] ...... 這種技術可以讓每個使用者自己管理自己的郵件别名。但由于個人使用者安全意識差,如果設定不當會有安全漏洞,不建議使用。
☆ local-host-names
/etc/mail/local-host-names 将
FEATURE(use_cw_file) 包含在你的
sendmail.mc 檔案當中,
Sendmail 将用本地主機名來作為你的本地别名。
主機名稱檔案
它給出本地主機的别名。如果你的主機有多個名字,或者你的主機是整個域的信件交換主機,你就需要這個檔案了。
# local-host-names - include all aliases for your machine here. panda.com www.panda.com mail.panda.com otherdomain.com 如果
Sendmail 沒有在收件清單中發現相應的主機名,它将拒絕接受對方發來的郵件。
如果你有多個别名或者需要負責的交換域,每個需要單獨寫上一行。
請記住在修改了這個或其他任何配置檔案後你必須重新開機 Sendmail 。 ☆ trusted-users
設定可信任使用者
可以代表其他使用者發郵件的使用者
這個可選的特性在
sendmail.mc 中的名稱為
use_ct_file 。 這個清單中的使用者可以改變電子郵件的寄件人。
在
/etc/mail/trusted-users 檔案中每個使用者名占一行。
這對運作在你的伺服器上的腳本或應用程式給某個人或組發資訊是非常有用的。
它可以更容易的快速識别一個電子郵件的資訊。
☆ virtusertable
改域名後,可以做域名的轉換
舊域 新域
類似于
aliases 檔案,但用于外部使用者
/etc/mail/virtusertable 虛拟使用者表格向你伺服器上的真實郵箱發送虛拟域和郵箱的郵件。這些郵箱可以是本地的,遠端的,或是由
/etc/aliases 定義的别名。這些是在你的
sendmail.mc 檔案中一指令行的形式進行初始配置的。
文法如下:
左位址 右位址 中間用Tab鍵分開。
例如:
[email protected] localuser 這樣一行意味着本來應該發送給
[email protected] 的郵件現在要發送給本機的使用者
localuser 。當然,這意味着:第一,你的
DNS 記錄中,本機應該是
otherdomain.com 的
MX 交換器;第二,你的本機
local-host-names 檔案應該包含
otherdomain.com 這個名字。
當然純粹的這樣的域意義不大,但是
sendmail 還支援郵件虛拟域的參數翻譯。例如:
@testdomain.com [email protected] 意味着所有發往
[email protected] 的郵件都會被發送到
[email protected] 。而
@testdomain.com %[email protected] 則代表參數轉義,例如
[email protected] 的郵件被發送到
[email protected] , [email protected] 被發送到
[email protected] 。同樣,這樣的功能也要通過
MX 記錄和
local-host-names 檔案加上去。
建立
virtusertable 的方法與建立
access 的辦法是一樣的:
#makemap hash virtusertable.db < virtusertable 然後重新啟動s
endmail 。 ☆ virtusertable.db
如同
Apache 一樣,
sendmail 也允許使用虛拟主機功能,這是通過
FEATURE(virtusertable) 功能實作的,而虛拟主機的檔案預設是
/etc/mail/virtusertable.db , 它用
/etc/mail/virtusertable 檔案生成,這個檔案的形式類似于
aliases 檔案,
#makemap hash virtusertable.db < virtusertable ☆ mailertable
定義郵差
/etc/mail/mailertable 檔案(在
FEATURE(`mailertable') 裡定義)用來定義對某個域名或者使用者使用什麼樣的郵差,如
sz.st-anda.com smtp:[10.100.100.252] 等等。一般情況下,并不需要定義這個功能。建立這個檔案的方式與上面的幾個
hash 資料庫相同。
☆ spamassassin
給
/etc/procmailrc 增加:
INCLUDERC=/etc/mail/spamassassin/spamassassin-default.rc 來支援
SpamAssassin ☆ statistics
/var/log/mail/statistics 用
mailstats 來讀取這個檔案,主要記錄sendmail收發信件的相關資訊。
☆ submit.mc
可以讓使用
sendmail 的限定于組
☆ Makefile
/etc/mail/Makefile 中是編譯選項,用
make -C /etc/mail 來處理
mail 目錄中的所有檔案
☆ mqueue
/var/spool/mqueue 是郵件隊列臨時存放的目錄。在
/var/spool/mqueue 下,有不同的隊列。消息存放在不同的隊列中。消息隊列檔案有以下形式: qf* ------消息隊列控制檔案 df* ------資料檔案 tf* -------臨時檔案
nf* -------每個使用者對應的消息隊列 xf* ------目前會話過程的
transcript 檔案
一般來說,
sendmail 守護子程序周期性地處理此隊列,嘗試發送每個消息。/
/etc/init.d/sendmail 腳本啟動
sendmail 程序,使它每
15 分鐘fork出一個子程序,來處理這個
mail 隊列,每次
sendmail 處理這個隊列的時候 ,它就讀取此隊列并排序,然後試圖依次運作各個任務。
15 ☆ mail 日志
Sendmail 很好的利用的它的日志檔案,你可以追捕到大部分問題的所在。
mail.log 和
mail.err 是你需要注意的兩個主要檔案,還有就是
mail.warn 和
mail.info 。 這些檔案有許多共同之處,是以我一般隻看前兩個檔案。一般而言,你可以在
/var/log/ 下找到這些檔案,對特定的系統和
Linux 版本,可能在其他目錄下。如果必要的話,你可以察看
/etc/syslog.conf 檔案。典型的電子郵件成功發送一般是這樣的:
Oct 25 18:22:14 example sendmail[29322]: SAA29322: from=user, size=193, class=0, pri=60193, nrcpts=2, msgid=<[email protected]>, [email protected] Oct 25 18:22:14 example sendmail[29324]: SAA29322: [email protected], ctladdr=user (500/1000), delay=00:00:00, xdelay=00:00:00, mailer=local, stat=Sent 你可以在
mail.log 看到和下面類似的拒絕資訊:
Oct 23 14:23:51 example sendmail[27467]: OAA27467: ruleset=check_rcpt, arg1=, relay=west1.mail-abuse.org [204.152.186.193], reject=550 ... Relaying denied 通常,你所需要的所有資訊都存儲在這些日志檔案中,包括使用者名,主機名,出錯代碼。這些出錯代碼很有用處,可以通過這些讓你對自己有更好的了解。
#tail -f /var/log/maillog 使用
tail 的
-f 指令可以觀察日志檔案内容的實時更新,特别适合于對伺服器進行調試和錯誤檢查。
16 ☆ Sendmail 的常見配置舉例
假設配置的要求如下:
·郵件伺服器的IP位址為192.168.1.2,主機域名為mail.1test.com
·郵件伺服器将為1test.com域中的使用者提供郵件服務
·為了防止垃圾郵件,伺服器需要具有SMTP使用者認證功能
郵件伺服器的基本配置步驟: 1.首先一定要安裝以下兩個軟體包,否則m4,newaliaes,makemap指令都無法使用:
#rpm -ivh sendmail-cf-8.13.8-2.el5.i386.rpm (第二張CD光牒,預設未裝)
#rpm -ivh sendmail-doc-8.13.8-2.el5.i386.rpm (第三張CD光牒,預設未裝)
2.域名設定
郵件伺服器的正常工作需要在DNS伺服器中進行相應的設定,對于主機域名為mail.1test.com的郵件伺服器需要在1test.com域的主DNS伺服器中進行設定:
mail IN A 192.168.1.2
@ IN MX 5 mail.1test.com.
3.設定local-host-names檔案
在sendmail伺服器的配置目錄下,local-host-names檔案用于設定郵件伺服器提供郵件服務的域名。例如,為了對1test.com域提供郵件服務,local-host-names
檔案中需要添加1test.com設定行。
#vi /etc/mail/local-host-names
1test.com //添加郵件伺服器提供郵件服務的域名
在local-host-names檔案中設定域名後,sendmail伺服器将對檔案中設定的域名提供郵件服務。
4.開啟sendmail伺服器的網絡接口
sendmail伺服器出于安全考慮,預設隻對lo回環網絡接口(IP位址為127.0.0.1)提供服務,為了使伺服器能夠為主機的所有網絡接口(位址為0.0.0.0)提供服務
需要在sendmail.mc檔案中進行配置的修改:
#vi /etc/mail/sendmail.mc
DAEMON_OPTIONS('Port=smtp,Addr=127.0.0.1,Name=MTA')dn1 //查找到此行,修改為:
DAEMON_OPTIONS('Port=smtp,Addr=0.0.0.0,Name=MTA')dn1
在sendmail.mc檔案中進行以上修改後,sendmail伺服器将監聽主機所有網絡接口的25端口。
5.sendmail伺服器的使用者管理
(1)設定SMTP的使用者認證
郵件伺服器提供發送郵件的功能是很正常的,但是如果為所有人都能夠無條件地發送(投遞)郵件是很危險的,非常容易造成大量垃圾郵件産生。是以在sendmail
伺服器中需要設定發送郵件的使用者認證,當使用者使用MUA軟體通過SMTP向郵件伺服器發送郵件時,郵件伺服器會要求使用者提供使用者賬号和密碼進行身份認證,隻有
通過身份認證的使用者才能通過郵件伺服器向外部發送郵件。RHEL5系統中提供的sendmail伺服器提供了SMTP的使用者認證功能,但是預設的設定沒有啟用此功能,因
此需要在sendmail.mc檔案中進行相應的配置:
#vi /etc/mail/sendmail.mc
dnl TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl //查找到以下兩行
dnl define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl 修改為:
TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl //去掉前面的注釋字元串dnl
define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl //去掉前面的注釋字元串dnl
特别注意:以上兩行去掉了前面的注釋串 dnl 後,前面一定不能留有空格,否則在啟動 sendmail 服務時會出錯! 在sendmail伺服器中,使用了sasl的第二版sasl2作為SMTP的認證方式,在/usr/lib/sasl2/Sendmail.conf配置檔案中設定了使用saslauthd服務程式提供使用者認
證服務,是以當sendmail伺服器使用SMTP認證功能時,需要確定saslauthd服務程式正确運作:
#cat /usr/lib/sasl2/Sendmail.conf
pwcheck_method:saslauthd
#chkconfig --level 35 saslauthd on
#service saslauthd start
(2)使用者賬号
sendmail伺服器使用linux系統中的使用者賬号作為郵件賬号,是以為使用者添加郵件賬号隻需要添加linux使用者賬号即可。為了便于對郵件使用者進行管理,需要先建
立郵件使用者組mailuser,建立立的郵件使用者賬号應屬于該組賬号。由于郵件賬号通常不需要登入linux系統,是以在adduser指令中可以使用“-s”選項指定使用者
的shell為“/sbin/nologin”,即不允許使用者登入linux系統。在建立郵件使用者賬号之後,需要使用passwd指令為使用者設定密碼,以便使用者發送和收取郵件時進
行身份認證。
#groupadd mailuser //建立郵件使用者組mailuser;
#adduser -g mailuser -s /sbin/nologin name1 //建立郵件使用者賬号name1和name2;
#adduser -g mailuser -s /sbin/nologin name2
#passwd name1 //設定郵件使用者密碼
#passwd name2
(3)設定郵件别名和郵件群發功能
在sendmail伺服器中使用aliases機制實作郵件别名和郵件群發功能,在“/etc”目錄下同時存在名為aliases和aliases.db兩個檔案。aliases檔案是文本文
件,其内容是可閱讀和可編輯的,aliases.db是資料庫檔案,是由aliases檔案生成而來的。對“/etc/aliases”檔案的内容進行修改後,需要執行newaliases
指令,該指令根據aliases檔案的内容重新生成aliases.db檔案。隻有對aliases.db檔案進行了更新,aliases檔案中新的别名設定在系統中才會生效,在aliases
檔案中每行作為一個别名設定記錄,記錄的格式如下:
name: addr_1, addr_2, addr_3,...addr_N
在aliases記錄的格式中name表示虛拟郵件賬号,addr_N表示真實郵件賬号名,aliases記錄可實作郵件别名和郵件群發功能。在RHEL5系統的“/etc/aliases”
檔案中已經預設設定了一些别名記錄,其中大部分都是為root使用者設定的别名。
例1.為郵件使用者name1設定别名admin的記錄如下:
#vi /etc/aliases
admin: name1
#newaliases
sendmail伺服器會根據設定的别名記錄将發往admin使用者的所有郵件轉發到name1使用者的郵箱中,即實作了為name1使用者設定别名。
例2.設定别名記錄,将發往testgroup郵箱的所有郵件都轉發到name1和name2使用者的郵箱中,實作郵件的群發功能:
#vi /etc/aliases
testgroup: name1, name2
#newaliases
為了實作郵件群發功能,需要在别名記錄中設定多個真實郵件賬号的名稱。
6.通路控制的設定
在sendmail伺服器中使用access.db資料庫進行基于主機位址的通路控制,預設開啟了對郵件伺服器本機使用者的郵件投遞,即在sendmail伺服器所在主機中的用
戶可以任意發送郵件,而不需要進行任何身份認證。
#vi /etc/mail/access
localhost.localdomain RELAY
localhost RELAY
127.0.0.1 RELAY
access.db資料庫檔案和SMTP認證功能配合使用,可以保證sendmail伺服器具有較好的安全性。
7.生成sendmail.cf檔案
在對sendmail.mc檔案修改完成了所有的配置後,需要使用m4指令生成新的sendmail.cf配置檔案:
#cd /etc/mail
#m4 sendmail.mc > sendmail.cf
注意:m4指令一定要在/etc/mail目錄下執行!
8.重新啟動sendmail伺服器
當完成對sendmail伺服器的配置之後,需要使用sendmail腳本重新啟動服務程式,使新的配置生效:
#service sendmail restart
使用netstat指令可以查詢到sendmail服務程式已經對所有的網絡接口的25端口進行了郵件服務的監聽:
#netstat -ntpl | grep 25
通過telnet郵件伺服器的25端口可以驗證郵件伺服器的SMTP認證功能:
#telnet localhost 25
.......
ehlo localhost //登入到郵件伺服器的25端口後,輸入ehlo localhost指令會顯示郵件伺服器的狀态資訊;
........
250-AUTH DIGEST-MD5 CRAM-MD5
LOGIN PLAIN //如果在250-AUTH開始的行中出現了LOGIN PLAIN,則說明sendmail伺服器中設定的SMTP認證功能生效。
通過telnet指令與SMTP服務互動通信的步驟,示範SMTP的工作過程:
(1)客戶機通過telnet指令與伺服器的25端口建立一個TCP連接配接,連接配接成功後,伺服器傳回一個表示通信連接配接成功的220應答代碼:
#telnet localhost 25 220 localhost.localdomain ESMTP Sendmail 8.13.8/8.13.8;Sun, 5 0ct 2008 04:14:52 +0800
(2)發送HELO,向伺服器辨別發件人的身份,若成功,則會收到表示郵件系統指令成功的250應答碼:
HELO localhost //輸入此語句
250 localhost.localdomain Hello localhost.localdomain [127.0.0.1],pleased to meet you
(3)使用MAIL FROM:指令給伺服器傳送發信人位址,傳送成功後,收到250應答碼:
MAIL FROM :[email protected] //輸入此語句
250 2.1.0 [email protected] ok
(4)使用RCPT TO:指令傳送收信人位址,可以傳送多行收信人位址到伺服器,伺服器收到有效的收信人位址後,将傳回250成功應答碼:
RCPT TO :root //輸入此語句
250 2.1.5 root.....Recipient ok
(5)向伺服器發送DATA指令,準備開始傳送郵件内容,伺服器若傳回354應答碼,即表示已經準備接收郵件内容,可以在下一行開始輸入郵件内容,并在最後一行,
以隻含一個圓點的行表示輸入結束(即在新行中鍵入圓點字元,然後回車,将結束郵件内容的輸入)。伺服器接受輸入後,傳回250應答碼,開始傳送郵件:
data //輸入此語句,準備開始傳送郵件内容
354 Enter mail ,end with "." on a line by itself
I love your ,LINUX! //此行開始輸入郵件内容
. //結束行中隻輸入一個圓點表示将結束郵件内容的輸入
250 2.0.0 m94KG5YI029186 Message accepted for delivery
(6)使用QUIT指令退出通信過程,相應的使用者将會收到該信件:
QUIT //表示退出通信過程
221 2.0.0 localhost.localdomain closing connection
Connection closed by foreign host.
郵局的基本配置步驟: 在整個郵件系統中sendmail伺服器實作了SMTP功能,是以隻實作了郵件的發送功能,并不為MUA軟體提供收取郵件的功能,收取郵件需要單獨安裝實作POP3或IMAP
功能的伺服器程式,可以使用
dovecot 伺服器實作POP3功能。
1.安裝
dovecot 軟體包
由于安裝dovecot伺服器依賴的軟體包較多,建議使用RHEL5系統的軟體包管理程式來安裝docecot軟體包:#system-config-packages,或依次安裝以下rpm包:
#rpm -ivh perl-DBI-1.52-1.fc6.i386.rpm
(第二張CD光牒)
#rpm -ivh mysql-5.0.22-2.1.i386.rpm
(第二張CD光牒)
#rpm -ivh dovecot-1.0-1.2.rc15.el5.i386.rpm
(第二張CD光牒)
若是RHEL4系統則需依次以下包:
#rpm -ivh perl-DBI-1.40.5.i386.rpm --aid (第二張CD光牒)
#rpm -ivh perl-DBD-MySQL-2.9004.3-1.i386.rpm --aid (第二張CD光牒)
#rpm -ivh mysql-4.1.7-4.RHEL4.1.i386.rpm (第二張CD光牒)
#rpm -ivh postgresql-libs.7.4.6-1.RHEL4.2.i386.rpm (第二張CD光牒)
#rpm -ivh dovecot-0.99.11-2.EL4.1.i386.rpm (第四張CD光牒)
2.設定
dovecot 伺服器
dovecot 伺服器的配置檔案是
/etc/dovecot.conf, dovecot 伺服器可以提供IMAP和POP3服務,但是預設隻提供IMAP服務,是以需要在/etc/dovecot.conf配置
檔案中進行設定的修改:
#vi /etc/dovecot.conf
#protocols = imap imaps //修改此行,去掉注釋“#”;添加pop3,pop3s兩項服務
修改為:
protocols = imap imaps pop3 pop3s
3.啟動
dovecot 服務程式,并設定啟動狀态
對dovecot.conf配置檔案進行設定後,需要重新啟動dovecot服務程式,以便新的設定生效,而且dovecot服務程式安裝後預設設定并不自動啟動,是以需要使用
chkconfig指令設定自動運作,以保障郵件伺服器的正常運作:
#service dovecot restart
#chkconfig --level 35 dovecot on
#netstat -ntpl | grep 110
17 ☆ sendmail 校驗
看sendmail是否啟動:
#netstat -nutlp | grep :25 确定
sendmail 沒有在啟動的時候出現錯誤
, /var/log/maillog 中。檢查此檔案中的最後出現
"starting" 的地方以確定
sendmail 在啟動的時候沒有任何錯誤。
sendmail 可執行檔案位于
/usr/sbin/sendmail, 為了确定
sendmail 是否正确辨別您的主機名稱,通過指令行開關開啟其調試模式并且設定為
0: [[email protected] mail]# sendmail -d0 < /dev/null Version 8.13.1
Compiled with: DNSMAP HESIOD HES_GETMAILHOST LDAPMAP LOG MAP_REGEX
MATCHGECOS MILTER MIME7TO8 MIME8TO7 NAMED_BIND NETINET NETINET6
NETUNIX NEWDB NIS PIPELINING SASLv2 SCANF STARTTLS TCPWRAPPERS
USERDB USE_LDAP_INIT
============ SYSTEM IDENTITY (after readcf) ============
(short domain name) $w = panda
(canonical domain name) $j = panda.panda.com
(subdomain name) $m = panda.com
(node name) $k = panda
========================================================
Recipient names must be specified
如果
sendmail 傳回您的主機名稱為
localhost ,您可能錯誤配置了
/etc/hosts 檔案。檢查您的
/etc/hosts 檔案,删除所有的但記住留下
localhost 的指向。
127.0.0.1 panda.panda.com panda localhost 如果
/etc/hosts 檔案是正确的,那麼檢查一下在
/etc/sysconfig/network 中的
HOSTNAME 的定義。
試圖向
[email protected] 發送簡單的郵件。您可以看到一個合理的您的主機的轉發伺服器的
SMTP 交換。
echo "hello root" | mail -v -s hello [email protected] [[email protected] ~]# echo "hello root" | mail -v -s hello [email protected] [email protected]... Connecting to [127.0.0.1] via relay...
220 panda.panda.com ESMTP Sendmail 8.13.1/8.13.1; Thu, 27 Jul 2006 08:46:11 +0800
>>> EHLO panda.panda.com 250-panda.panda.com Hello panda.panda.com [127.0.0.1], pleased to meet you
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-8BITMIME
250-SIZE
250-DSN
250-ETRN
250-AUTH DIGEST-MD5 CRAM-MD5
250-DELIVERBY
250 HELP
>>> MAIL From:<[email protected]> SIZE=42 [email protected] 250 2.1.0 <[email protected]>... Sender ok
>>> RCPT To:<[email protected]> >>> DATA 250 2.1.5 <[email protected]>... Recipient ok 354 Enter mail, end with "." on a line by itself >>> . 250 2.0.0 k6R0kBQO005444 Message accepted for delivery ro[email protected]... Sent (k6R0kBQO005444 Message accepted for delivery) Closing connection to [127.0.0.1] >>> QUIT 221 2.0.0 panda.panda.com closing connection You have mail in /var/spool/mail/root 如果
SMTP 交換向上面一樣正确,那麼消息将被轉發到您的工作站上的本地的轉發伺服器上,并且
mailq -Ac 将會報告一個空的對列。接下來檢查
mail( 不使用參數
) 來檢查一下消息是否從本地的轉發到
server1 。這樣對列也應該是空的。
[[email protected] public_html]# mailq -Ac /var/spool/clientmqueue is empty Total requests: 0 您的消息是不是在
/var/log/maillog 中正确的記錄呢?在下面的步驟中,監視檔案
/var/log/maillog 。下面的指令将會十分的有用:
#xterm -e tail -f /var/log/maillog & 18 ☆ sendmail 收取郵件
為了安全的原因, sendmail 和 postfix 的預設的配置允許發郵件但是不允許從網絡上接收郵件 ( 預設的它們隻接受從回環接口上的連接配接 ) 。 確定在 Server 上的 sendmail.mc 檔案中的 DAEMON_OPTIONS 被注釋并且重新編譯 sendmail.cf 檔案使得能構接受來自其他主機的電子郵件。 修改 /etc/mail/sendmail.mc 使用 dnl 注釋在下面的行之前,就象這樣 : dnl DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA') 先将您的 sendmail.cf 檔案做一個備份 : cp /etc/mail/sendmail.cf /etc/mail/sendmail.cf.bak 在同一個目錄下,編譯 sendmail.cf m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf 重新啟動 sendmail 19 ☆ sendmail 别名
sendmail 的主要的别名配置檔案是 /etc/aliases 。 在 sendmail 決定消息的接受者的目的地的之前,其先試圖在别名中查找。 為了優化查找, sendmail 為其别名記錄建立了一個哈希表資料庫 /etc/aliases.db 該檔案通過 newaliases 指令産生 ( 該指令是 sendmail -bi 的同名 ) 下列指令将增加使用者 student( 如果不存在的話 ) useradd student 在 /etc/aliases 行加入如下的行 : me: student( 别名 : 實際使用者名 ) wizards: root, me methere: [email protected] 現在運作 newaliases 指令來更新資料庫 嘗試發送郵件給您定義的收件人 : echo "hello there" | mail -s "hello" me echo "hello there" | mail -s "hello" wizards echo "hello there" | mail -s "hello" methere 您是否得到了期望的結果?是否所有的位于 wizards 的收件人都受到了郵件?如果沒有, su - 到不是 root 的使用者再試一次。 如果沒有收到 , 可能需要在 /etc/mail/access 中打開權限 20 ☆ sendmail 允許轉發
通過控制您的機器的混雜轉發,您可以使得任何人都能夠将您的機器作為轉發的主機。 配置 /etc/mail/sendmail.mc, 通過加入如下行使得 m4 前置處理器允許混雜轉發 ( 可以發到任何位址 ): /etc/mail/sendmail.mc 中添加 FEATURE(`promiscuous_relay')dnl 使用 m4 前置處理機通過這個模闆檔案生成一個新的 sendmail 配置檔案,然後将新生成的檔案與通過 sendmail RPM 軟體包提供的進行比較 m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.relay diff /etc/mail/sendmail.relay /etc/mail/sendmail.cf 使用混雜轉發以後會有多大的不同呢?現在将建立立的 sendmail.relay 放置在恰當的位置上,重新啟動 sendmail. mv /etc/mail/sendmail.cf /etc/mail/sendmail.cf.accept-mail cp /etc/mail/sendmail.relay /etc/mail/sendmail.cf service sendmail restart 讓您的夥伴扮演惡意的垃圾郵件的發送者,該人能夠通過 telnet 到您的機器上的 smtp(sendmail) 的 25 号斷口,進行垃圾郵件發送位址的欺騙,在 panda 主機上鍵入如下指令 : [[email protected] ~]# telnet panda 25 Trying 127.0.0.1... Connected to panda.panda.com (127.0.0.1). Escape character is '^]'. 220 panda.panda.com ESMTP Sendmail 8.13.1/8.13.1; Thu, 27 Jul 2006 01:12:37 +0800 helo panda.panda.com 250 panda.panda.com Hello panda.panda.com [127.0.0.1], pleased to meet you mail from: [email protected] 250 2.1.0 [email protected]... Sender ok rcpt to:[email protected] 250 2.1.5 [email protected]... Recipient ok data 354 Enter mail, end with "." on a line by itself Subject: Faked this was faked! 250 2.0.0 k6QHCb8x007709 Message accepted for delivery quit 221 2.0.0 panda.panda.com closing connection Connection closed by foreign host. 垃圾郵件現在送到您的機器上了。下一步,發給其他主機 21 ☆ sendmail 不允許轉發
通過替換新的 sendmail.cf 為接受傳入的信件的配置檔案來恢複預設的 sendmail 的配置,并且重新啟動 sendmail: 注釋掉這行 FEATURE(promiscuous_relay)dnl mv /etc/mail/sendmail.cf.accept-mail /etc/mail/sendmail.cf service sendmail restart 讓您的夥伴再從 stationY 轉發垃圾郵件。您的 sendmail 還是一個轉發器麼?任何一個轉發的都會産生如下的消息 : 550 5.7.1 [email protected]... Relaying denied 但是本機是可以轉發的 (access 檔案中定義 ) 22 ☆ sendmail 選擇性的轉發
對于特定的主機,域或者網絡,編輯 /etc/mail/access 并且重新啟動 sendmail 。 為了允許所有在 panda.com 域中的機器可以把您的機器作為郵件轉發伺服器,你在 /etc/mail/access 中添加如 panda.com 域。 然後用 #makemap hash access.db<access 23 ☆ m4
sendmail 是一個極為複雜的程式,其行為主要地依賴于在 UNIX 界 " 臭名昭著 " 的 /etc/sendmail.cf 配置檔案。實際上,我懷疑會有誰真的從頭去寫一個 sendmail.cf 檔案。一般來說,我們總是用 m4 宏處理來書寫 sendmail.cf 。實際上, m4 程式幾乎和 sendmail.cf 一樣複雜,不過,通常我們隻需要關心一些比較重要的部分。 要使用宏處理程式,必須确定你已經安裝了 m4 和 sendmail-cf 這樣兩個軟體包 用 m4 程式可以生成一個 sendmail.cf: #m4 sendmail.mc > /sendmail.cf 24 ☆ sendmail 總結
配置接收郵件, /etc/mail/sendmail.mc dnl DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA') m4 sendmail.mc > sendmail.cf 添加别名 在 /etc/aliases 加入别名 ,newaliases 不充許轉發:預設 轉發 : 在 sendmail.mc 加入 FEATURE ( promiscuous_relay) dnl 選擇轉發 : /etc/mail/access 使用者和主機通路控制 /etc/mail/access domain.com RELAY USER@ REJECT [email protected] REJECT IP RELAY makemap hash access.db<access 或 makemap hash access <access .db 可以省略 25 ☆建立 E-Mail 帳号
在 LINUX 中,要為一個新的使用者開 E-Mail 帳号是十分簡單的。隻要在 LINUX 系統中新增一個使用者就可以了。那麼這個使用者帳号和密碼就是 E-Mail 的帳号和密碼。 我為一個新使用者 test 開一個 E-Mail 帳号。就用以下指令 :#adduser panda , # passwd panda 這樣,這個新使用者的 E-Mail 位址就是 : [email protected] 密碼當然就是帳号的密碼了。 要防止本地使用者利用 sendmail 伺服器上的漏洞,最好是讓郵件使用者隻使用電子郵件程式來通路 sendmail 伺服器。郵件伺服器上的 Shell 帳号不應該被允許, /etc/passwd 檔案中的所有使用者 shell 都應該被設定為 /sbin/nologin ,在添加系統使用者時可以 useradd -s /sbin/nologin 使用者名,這樣添加的使用者名就沒有了 shell ,隻能收發信而不能登陸到伺服器運作其他程式,安全一點。 26 ☆ sendmail 郵件限額
當一個 E-Mail 伺服器為許多人提供 E-Mail 服務的時候,無限量的電子郵件将很容易塞滿伺服器的硬碟,造成很大的負載。如果你的伺服器不想為使用者提供無限空間的 E-Mail 暫存空間,那麼你就可以使用 " 郵件限額 " 來給使用者一個有限的暫存空間。 其實,它是利用磁盤限額功能來實作的。電子郵件的暫存空間是在 /var/spool/mail 目錄下,隻要通過磁盤限額設定每一個使用者在這個目錄下能使用的最大空間就可以了。 27 ☆ mailstats
郵寄狀态查詢指令,可查詢 sendmail 運作作至今郵件收發總計資料。 M : msgsfr: 發送的郵件數量 bytes_from: 郵件容量 megsto: 收到郵件的數量。 bytes_to: 同上 msgsrej: 郵件 deny 的次數。 msgsdis: 郵件 discard 的次數。 Mailer :esmtp 對外郵件, local 本地郵件。 28 ☆ mailq
#/usr/bin/mailq 郵件隊列查詢指令。 Q-ID 郵件 id 号。 Size 郵件容量。 Q-Time 郵件進入隊列 ( 也就是 /var/spool/mqueue 目錄 ) 的時間和不能郵寄的原因。 Sender/Recipient 發信和收信人的郵箱位址。 29 ☆ sendmail -q
當指令 sendmail -q 發出以後, sendmail 将會試圖仍在隊列中等待的郵件。 可以在後面跟上時間‘ s ’是秒 , ‘ m ’分鐘 ( 預設 ), ‘ h ’小時 , ‘ d ’天 , ‘ w ’周 . 30 ☆ sendmail 管理郵件隊列
一般當你發送一封郵件的時候, sendmail 傾向于立刻發送這一封電子郵件。但是如果 目前網絡忙使得無法立刻投遞信件,或者是目标位址的連接配接速度太慢,無法在短時間内投遞到目标位址處,那麼 sendmail 将把待發送的郵件排入隊列,并在合适的時候重新發送。 隊列檔案通常存放在 /var/spool/mqueue 下面,每個待發送的郵件由幾個檔案構成 例如,我們可以看到下面的目錄檔案清單 : [[email protected] mqueue]# ls dfRAA27175 xfAAA00733 xfBAA00819 xfEAA32763 xfXAA00706 qfRAA27175 xfAAA00784 xfDAA01360 xfFAA01616 檔案名字總是由一個兩字元的字首加上一個随機數字。字首有四鐘 : df: 郵件内容 qf: 郵件頭和一些控制資訊 xf: 一些臨時檔案 tf:qf 檔案的臨時存儲檔案 可以通過看隊列中的 qf 檔案來确定目前隊列資訊,不過通常都可以用 mailq 程式來完成 對隊列狀态的查詢 : [[email protected] mqueue]# mailq Mail Queue (1 request) --Q-ID-- --Size-- -----Q-Time----- ------------Sender/Recipient----------- - RAA27175 22429 Fri Feb 25 17:34 : deferred) [email protected] 這表示隊列中目前隻有一封信,由 zhangfl 發出,收信人是 [email protected] 。 由 sendmail 接受到的信件在使用者取走之前将暫時存儲在 /var/spool/mail 目錄下面,存 儲的方法非常簡單,就是每個有待讀郵件的使用者一個檔案 : [[email protected] spool]# ls -l /var/spool/mail |more total 19364 -rw-rw---- 1 anyi mail 7559035 Feb 28 22:04 anyi -rw-rw---- 1 baixuan mail 514 Nov 7 01:32 baixuan -rw-rw---- 1 cwc mail 515 Feb 28 08:35 cwc 如果一個使用者有多封電子郵件,那麼這些郵件就被簡單地連接配接在一起構成一個大檔案 ( 是以你會看到非常巨大的郵件檔案 ) 。 如果你面對的是一個很龐大的郵件服務系統,有時會在某個郵件隊列中擁塞太多的消 息,你可能想把這個隊列暫停并且在網絡空閑的時候再發送,暫停一個郵件隊列的辦法非常簡單,就是将 /var/spool/mqueue 目錄移走 : killall sendmail mv /var/spool/mqueue /var/spool/mqueue.stop mkdir /var/spool/mqueue sendmail -bd 當網絡空閑的時候,可以用 -oQ 參數立即處理擁塞的隊列 : sendmail -oQ /var/spool/mqueue.stop -q 31 ☆ SMTP 協定的基本指令
在 /etc/mail/helpfile 中有描述 為了使用這種測試技術,你需要了解 SMTP 協定的基本指令,這樣的指令有 14 個,在下面列出 : HELO 标志發起 smtp 請求的主機,例如,從 client1 發起 smtp 會話,可以使用 HELO client1 MAIL FROM: 啟動一個郵件會話,在這個行中需要标志發信人的信封位址,例如,要從 [email protected] 發出郵件,使用 MAIL FROM:[email protected] ,注意尖括号的用法。 RCPT TO: 标志收信人的信封位址,例如,要發送給 [email protected] ,使用 RCPT TO: [email protected] 。在一個 MAIL FROM 之後可以給出多個收信人位址,以便實作多副本的傳送。 VRFY 驗證某個位址,例如,要确定 [email protected] 是一個可以投遞的位址,使用 VRFY [email protected] 。 EXPN 顯示某個收件人位址或者使用者名的實際名字。例如,要顯示 postmaster 使用者的實際投 遞位址,使用 EXPN postmaster 。如果在某個使用者的目錄下有 .forward 檔案,這個檔案的 内容将會被自動使用。 DATA 開始寫信,在 MAIL 和 RCPT 之後可以使用這個指令傳輸信件正文,傳輸完畢之後輸入一 個 . 退出。 QUIT 關閉 smtp 會話 RSET 複位連接配接狀态 HELP 顯示這個指令表。 32 ☆ sendmail 測試
/etc/mail 目錄下配置一些文檔。 # cd /etc/mail # echo 'examply.com' >> local-host-names 接收郵件的主機名 # echo 'localhost RELAY' >> access 用來拒絕或允許來自某個域的郵件,本例允許本地轉發。 # makemap hash access < access 生成 access.db 資料庫 # touch domaintable 用來把舊域名映射互新域名 # makemap hash domaintable < domaintable # touch mailertable 來覆寫向指定域的路由 # makemap hash mailertable < mailertable # touch trusted-users # touch virtusertable 用來把使用者和域名映射到其它位址 # makemap hash virtusertable < virtusertable # chown root:wheel /var/spool/mqueue/ # chmod 700 /var/spool/mqueue # touch aliases 别名資料庫,文本形式。可參照源碼目錄樹下 sendmail/aliases 檔案。 # newaliases 從文本檔案中建立一個新的别名資料庫檔案。 # sendmail -v -bi 調試啟動。 /etc/mail/aliases: 42 aliases, longest 10 bytes, 432 bytes total 如果出現以上提示資訊,則啟動成功。可用以下指令正式啟動 : 打開你喜歡的 Mail Client ,設定 smtp 伺服器為 :localhost ( 假如和伺服器在同一台機子上,也可以設定本機的 IP), 端口為 :25. 然後随便一段測試資訊 , 填寫好接收郵件的位址,按發送。你的 Mail Client 會提示郵件已發送。實際上你的郵件還在計算機上排隊,并未發出!要發送所有排隊的郵件,你需要連上網,然後你 root 身份登陸,并運作 :# sendmail -q . 現在你的郵件就會發送到指定的郵箱中。 33 ☆ postfix 安裝
在安裝 Postfix 前,需要關閉 sendmail 服務和開機時的自啟動,使用如下指令: #service sendmail stop #chkconfig sendmail off 在 http://www.postfix.com/ 上下載下傳需要的 postfix 的 rpm 安裝包,或在 RHEL5 的安裝CD光牒中找到需要安裝的 rpm 包,然後進行安裝: #rpm -ivh postfix-2.3.3-2.i386.rpm ( 第三張CD光牒 ) 34 ☆ postfix 收取郵件
為了安全的原因, sendmail 和 postfix 的預設的配置允許發郵件但是不允許從網絡上接收郵件 ( 預設的它們隻接受從回環接口上的連接配接 ) 。 修改 /etc/postfix/main.cf 找到并注釋如下行 inet_interfaces = localhost 取消注釋該行 : inet_interfaces = all 35 ☆ Postfix 啟動和校驗
運作 #service sendmail start #/etc/init.d/postfix start 使用圖形工具 #system-switch-mail 使得 postfix 成為活躍的 MTA 。 也可以使用如下的指令行 : #alternatives -set mta /usr/sbin/sendmail.postfix 重新啟動指令如下 : #service postfix restart 确定 hostname 指令正确的傳回您的主機名稱。確定 DNS 配置正确應該是您的 FQDN 。 如果 sendmail 傳回您的主機名稱為 localhost, 您可能錯誤配置了 /etc/hosts 檔案。檢查您的 /etc/hosts 檔案,删除所有的但記住留下 localhost 的指向,然後再試一遍。如果 /etc/hosts 檔案是正确的,那麼檢查一下在 /etc/sysconfig/netwoek 中的 HOSTNAME 的定義。當這些值都正确的時候,啟動 postfix 服務。 确定 postfix 在啟動的時候沒有錯誤 Red Hat Linux 的安裝使用提供的 syslog 工具來記錄所有的資訊到檔案 /var/log/maillog 中去。檢查此檔案中的最後查找任何錯誤資訊。 試圖向 [email protected] 發送簡單的郵件并且檢查 /var/log/maillog 的記錄檔案 #mail -s `echo $USER` [email protected] < /etc/redhat-release 應該如下所示 : Jul 26 20:55:10 localhost postfix/pickup[4902]: 717AB335FE: uid=0 from=<root> Jul 26 20:55:10 localhost postfix/cleanup[4943]: 717AB335FE: message-id=<[email protected]> Jul 26 20:55:10 localhost postfix/qmgr[4903]: 717AB335FE: from=<[email protected]>, size=314, nrcpt=1 (queue active) Jul 26 20:55:10 localhost postfix/local[4945]: 717AB335FE: to=<[email protected]>, relay=local, delay=0, status=sent (delivered to mailbox) Jul 26 20:55:10 localhost postfix/qmgr[4903]: 717AB335FE: removed 36 ☆ Postfix 的别名
在 postfix 決定消息的接受者的目的地的之前,其先試圖在别名中查找。 postfix 的主要的别名配置檔案是 /etc/aliases 。為了優化查找, postfix 為其别名記錄建立了一個哈希表别名資料庫 /etc/aliases.db( 和 sendmail 類似 ). 該檔案通過 newaliases 指令産生。 下列指令将增加使用者 student( 如果不存在的話 ) useradd student 在 /etc/aliases 行加入如下的行 : 注意 : 注釋 root 别名的那一行為 postfix me: student wizards: root, me methere: [email protected] 現在運作 newaliases 更新資料庫 嘗試發送郵件給您定義的收件人 : echo "hello there" | mail -s "hello" me echo "hello there" | mail -s "hello" wizards echo "hello there" | mail -s "hello" methere 是否所有的位于 wizards 的收件人都受到了郵件? 要确認 /etc/mail/access 中打開權限 37 ☆ postfix 允許轉發
預設的 postfix 允許在子網上的任何人通過您的機器進行轉發。但是并不是在每一個環境中都安全的。例如,您的機器和其他機器在一起,如果您的本地子網裡有一台機器被其他人控制,那麼其他的機器都會有麻煩。 讓您的夥伴扮演惡意的垃圾郵件的發送者,該人能夠通過 telnet 到您的機器上的 postfix 的 25 号斷口,進行垃圾郵件發送位址的欺騙,在 panda 鍵入如下指令 : [[email protected] mail]# telnet panda 25 Trying 127.0.0.1... Connected to panda.com (127.0.0.1). Escape character is '^]'. 220 panda.com ESMTP Postfix helo root.panda.com 250 panda.com mail from:[email protected] 250 Ok rcpt to: [email protected] 250 Ok data 354 End data with <CR><LF>.<CR><LF> Subject: Faked this was faked! 250 Ok: queued as 9DB2733601 quit 221 Bye Connection closed by foreign host. 垃圾郵件現在送到您的機器上了。下一步,看看您的夥伴能不能從您的機器轉發給第三台機器 : 由于您的機器已經被配置成為允許混雜轉發,垃圾郵件可以通過您的機器進行郵件轉發。 注意 /var/log/maillog 的變化 38 ☆ postfix 不允許轉發
編輯檔案 /etc/postfix/main.cf 取消轉發。 查找并且取消注釋下面的行, mynetworks_style = host 并且重新啟動 postfix 讓您的夥伴再從 stationY 轉發垃圾郵件。您的 postfix 還是一個轉發器麼?任何一個轉發的都會産生如下的消息 : 554 <[email protected]>: Recipient address rejected: Relay access denied 39 ☆ postfix 選擇性的轉發
對于特定的主機,域或者網絡,編輯 /etc/postfix/main.cf 并且重新啟動 postfix 。對于特定的主機允許通過您的機器進行轉發,找到并且取消注釋該行 : mynetworks_style = host 然後添加新行來允許轉發的主機和網絡,在這裡允許 station1 和本地轉發 mynetworks = 192.168.152.128, 127.0.0.0/8 40 ☆ postfix 接收和轉發的條件
(1) 預設情況下, postfix 接收符合以下條件的郵件: 目的地為 $inet interfaces 的郵件; 目的地為 $mydestination 的郵件; 目的地為 $virtual_alias_maps 的郵件。 (2) 預設情況下, postfix 轉發符合以下條件的郵件: 來自用戶端 IP 位址符合 $mynetworks 的郵件; 來自用戶端主機名稱符合 relay_domains 及其子域的郵件; 目的地為 $relay_domains 及其子域的郵件。 41 ☆主配置檔案 main.cf
修改 /etc/postfix/main.cf 的配置 : myhostname = mail.cngnu.org 指定運作 postfix 服務的郵件主機的主機名稱 (FQDN 名 ) mydomain = cngnu.org 指定運作 postfix 服務的郵件主機的域名稱 myorigin = $mydomain 設定由本台郵件主機寄出的每封郵件的郵件頭中 mail from 的位址 inet interfaces = all 預設情況下, inet interfaces 參數的值被設定為 localhost ,這表明隻能在本地郵件主機上寄信。如果郵件主機上有多個網絡接口,而又不想使全部的網絡接口都開放 Postfix 服務,就可以用主機名指定需要開放的網絡接口。不過,通常是将所有的網絡接口都開放,以便接收從任何網絡接口來的郵件,即将 inet interfaces 參數的值設定為“ all ”。 mydestination = $mydomain,$myhostname 隻有當發來的郵件的收件人位址與該參數值相比對時, Postfix 才會将該郵件接收下來。例如,這裡将該參數值設定為 $mydomain,$myhosname ,表明無論來信的收件人位址是 [email protected]( 其中, XXX 表示某使用者的郵件賬戶名 ) ,還是 [email protected] , Postfix 都會接收這些郵件。 mynetworks_style = host mynetworks = 127.0.0.0/8,192.168.1.0/24 設定可轉發 (Relay) 哪些網絡的郵件 . 可以使用 mynetworks 參數來設定。可将該參數值設定為所信任的某台主機的 IP 位址,也可設定為所信任的某個 IP 子網或多個 IP 子網 ( 用“,”或者“”分隔 ) 。這裡,将 mynetworks 參數值設定為 192.168.16.0 / 24 ,則表示這台郵件主機隻轉發子網 192.168.16.0 / 24 中的用戶端所發來的郵件,而拒絕為其他子網轉發郵件。 relay_domains = gdvcp.net mynetworks 參數是針對郵件來源的 IP 來設定的,而 relay_domains 參數則是針對郵件來源的域名或主機名來設定的。例如,将該參數值設定為 gdvcp.net ,則表示任何由域 gdvcp.net 發來的郵件都會被認為是信任的, Postfix 會自動對這些郵件進行轉發。 virtual_alias_domains = dzxx.cn,panda.com 用來指定虛拟别名域的名稱 virtual_maps = hash:/etc/postfix/virtual #,mysql:/etc/postfix/virtual.mysql 含有虛拟别名域定義的檔案路徑。 alias_maps = hash:/etc/aliases 指定含有使用者别名定義的檔案路徑 alias_database = hash:/etc/aliases 指定别名表資料庫檔案路徑。 home_mailbox = Maildir/ mailbox_transport = cyrus fallback_transport = cyrus smtpd_sasl_auth_enable = yes 指定是否要啟用 SASL 作為 SMTP 認證方式。預設不啟用,這裡必須将它啟用,是以要将該參數值設定為“ yes ”。 smtpd_sasl_local_domain = '' 如果采用 Cyrus-SASL V2 版進行認證,那麼這裡不作設定。 smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination 表示通過收件人位址對用戶端發來的郵件進行過濾。通常有以下幾種限制規則。 permit_mynetworks: 表示隻要是收件人位址位于 mynetworks 參數中指定的網段就可以被轉發郵件 permit_sasl_authenticated: 表示允許轉發通過 SASL 認證的郵件。 reject_unauth_destination: 表示拒絕轉發含未信任的目标位址的郵件。 broken_sasl_auth_clients = yes 表示是否相容非标準的 SMTP 認證。有一些 Microsoft 的 SMTP 用戶端 ( 如 Outlook Express 4.x) 采用非标準的 SMTP 認證協定,隻需将該參數設定為 "yes" 就可解決這類不相容問題。 smtpd_client_restrictions = permit_sasl_authenticated 表示限制可以向 postfix 發起 SMTP 連接配接的用戶端。如果要禁止未經過認證的用戶端向 postfix 發起 SMTP 連接配接,則可将該參數值設定為 permit_sasl_authenticated smtpd_sasl_security_options = noanonymous 用來限制某些登入的方式。如果将該參數值設定為 noanonymous ,則表示禁止采用匿名登入方式。 permit_mynetworks, permit_sasl_authenticated, check_recipient_access mysql:/etc/postfix/filter.mysql, 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, permit 如果希望支援更多的虛拟域,可以在 mydestination 參數後面加上你所要支援的域即可。 通過 virtual 和 virtual.mysql 為系統提供了郵箱本地查詢表。 42 ☆ virtual
/etc/postfix/virtual 虛拟别名 實際名 @dzxx.cn @gdvcp.net [email protected] lbt [email protected] st123001,st123002,st123003 [email protected] lbt,[email protected] 43 ☆ master.cf
由 postfix 運作的守護程式 44 ☆ pcre_table
位址改寫的或郵件路由的 perl 表達式 45 ☆ postfix-files
postfix 檔案和權限 46 ☆ postfix-script
管理 postfix, 類似 start,stop 47 ☆ regexp_table
從 postfix 查找賬号的表 用 postconf -m 來檢查 48 ☆ relocated
使用者轉移到新位置 , 用這裡來重定向 49 ☆ transport
指定一個傳輸協定 50 ☆ postconf
postconf 測試配置檔案 postconf -n 看目前配置檔案的路徑 51 ☆重新開機 postfix
修改 main.cf 和 virtual,aliases 後 postmap /etc/postfix/virtual 生成 etc/postfix/virtual.db postalias /etc/aliases 生成 /etc/aliases.db postfix reload 重載 main.cf 52 ☆ SMTP 認證的配置
如果任何人都可以通過一台郵件伺服器來轉發郵件,會有什麼後果呢 ? 很可能這台郵件伺服器就成為了各類廣告與垃圾信件的集結地或中轉站,網絡帶寬也會很快被耗盡。為了避免這種情況的出現, MTA 預設不會對外開放轉發功能,而僅對本機 (1ocalhost) 開放轉發功能。但是,在實際應用中,必須在 MTA 主配置檔案中通過設定 mynetworks 、 relay domains 參數來開放一些所信任的網段或網域,否則該郵件伺服器幾乎沒有什麼用途。在開放了這些所信任的網段或網域後,還可以通過設定 SMTP 認證,對要求轉發郵件的用戶端進行使用者身份 ( 使用者賬戶名與密碼 ) 驗證。隻有通過了驗證,才能接收該使用者寄來的郵件并幫助轉發。 目前,比較常用的 SMTP 認證機制是通過 Cyrus-SASI. 。包來實作的。 53 ☆ Cyrus-SASL
是 Cyrus Simple Authentication and Security Layer 的簡寫,它最大的功能是為應用程式提供了認證函數庫。應用程式可以通過函數庫所提供的功能定義認證方式,并讓 SASL 。通過與郵件伺服器主機的溝通進而提供認證的功能。 54 ☆ Cyrus-SASL 安裝
rpm -qa | grep sasl 55 ☆ Cyrus-SASL 啟動
運作 saslauthd 守護程序 service saslauthd restart 56 ☆ Cyrus-SASL 認證機制
預設情況下, Cyrus-SASL V2 版使用 saslauthd 這個守護程序進行密碼認證,而密碼認證的方法有多種,使用下面的指令可檢視目前系統中的 Cyrus-SASL V2 所支援的密碼驗證機制。 [[email protected] mail]# saslauthd -v saslauthd 2.1.19 authentication mechanisms: getpwent kerberos5 pam rimap shadow ldap 57 ☆ saslauthd 配置
配置 saslauthd 使用 shadow 認證方案 : /etc/sysconfig/saslauthd MECH=shadow 配置 saslauthd 使用 PAM 認證方案 ( 非預設 , 需要配置 pam, 否則測試失敗 ): /etc/sysconfig/saslauthd MECH=pam 指令行方式啟動驗證方案 : saslauthd -a shadow 用 shadow 的使用者和密碼進行驗證 58 ☆測試 Cyrus-SASL
ps aux | grep saslauthd service saslauthd restart testsaslauthd -u userid -p password [[email protected] ~]# testsaslauthd -u student -p '111111' 0: OK "Success." 如果出現以上資訊,就說明 saslauthd 正常運作了。表示認證功能有效 伺服器啟動後,可以用 telnet 連接配接伺服器。 # telnet localhost 25 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. 220 test.tigerhead ESMTP Sendmail 8.12.10/8.12.10; Tue, 30 Mar 2004 14:50:14 +0800 ehlo test (!!!!!!!! 注意指令是 ehlo) 你輸入的指令,按回車結束。 250-test.tigerhead Hello LOCALHOST.localdomain [127.0.0.1], pleased to meet you 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-8BITMIME 250-SIZE 250-DSN 250-ETRN 250-AUTH DIGEST-MD5 CRAM-MD5 LOGIN PLAIN # 就表明 SMTP 認證成功 250-DELIVERBY 250 HELP 以 250- 開頭的為伺服器的響應資訊。注意倒數第三行,這就是成功配置 smtp 驗證的顯示。 輸入 quit 離開。 59 ☆ Cyrus-SASL 配置 MTA
postfix 中 更改 main.cf 中的配置 smtpd_sasl_auth_enable = yes smtpd_sasl_local_domain = smtpd_sasl_security_options = noanonymous broken_sasl_auth_clients = yes 建立 /usr/lib/sasl2/smtpd.conf 設定 Postfix 使用 SASL 的 saslauthd 認證守護程序來支援 smtp auth 認證,并隻打開了 plain 和 login 認證子產品 : [[email protected] root]# echo pwcheck_method:saslauthd > /usr/lib/sasl2/smtpd.conf [[email protected] root]# echo mech_list: plain login >> /usr/lib/sasl2/smtpd.conf sendmail 中 更改 /etc/mail/sendmail.mc 去掉下面倆行的注釋 TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl 如果 sendmail 沒辦法收信 , 就把 DAEMON_OPTIONS(`Port=587, Name=MSA, M=Ea')dnl 改為 DAEMON_OPTIONS(`Port=25, Name=MSA')dnl ( 這一句不能和 DAEMON_OPTIONS(`Port=25, Name=MTA')dnl 同時存在 ) 建立 /usr/lib/sasl2/Sendmail.conf 當 sendmail 要使用 SMTP 認證時,必須建立一個 SASL 的配置檔案來把 MTA 程式定義成一個 SASL 應用。配置檔案名為 Sendmail.conf( 注意是大寫的 S), 位于 /usr/lib/sasl2 目錄中,也就是 /usr/local/sasl2/lib/sasl2 這個目錄,記得上面建立的連結了嗎?在該檔案中你定義你希望使用的認證資料庫方法,以下這個例子使用 saslauthd 來驗證認證請求。 # echo 'pwcheck_method: saslauthd' > /usr/lib/sasl2/Sendmail.conf( 注意大小寫 ) 如果是 pwcheck_method: pam 的話是直接調用 pam 認證 如果是 pwcheck_method: saslauth 的話要啟用 saslauth -a pam ,讓 saslauth 調用 pam 認證 将這個檔案複制成 smtpd.conf ,即 cp Sendmail.conf smtpd.conf ,這個檔案也要在 /usr/lib/sasl2/ 目錄下 在 /etc/pam.d/ 目錄下參照其它檔案建立一個“ smtp ”檔案 (postfix 的是 smtp.postfix) ,内容如下: #%PAM-1.0 auth required pam_stack.so service=system-auth account required pam_stack.so service=system-auth 60 ☆ dovecot 配置
postfix 服務隻是一個 MTA( 郵件傳輸代理 ) ,它隻提供 SMTP 服務,也就是隻提供郵件的轉發及本地的分發功能。要實作郵件的異地接收,還必須安裝 POP 或 IMAP 服務。 dovecot 提供了這些服務 :POP3,POP3S,IMAP,IMAPS 編輯 /etc/dovecot.conf 檔案 在 protocols = 一行,加入你需要的的服務 預設的 ports: imap: 143 imaps: 993 pop3: 110 pop3s: 995 ssl_cert_file 和 ssl_key_file, 可以看到 dovecot 是拿什麼檔案來做 ssl 認證的,可以自己建立新的 pem 檔案。 将 disable_plaintext_auth 配置為 no( 非預設 ) 将 auth_mechanisms 配置為 plain( 預設的 ) 将 auth_userdb 配置為 passwd 也就是說預設為所有 local user 都能登入。 将 auth_passdb 設定為 shadow 或者将 auth_passdb 設定為 pam 增加對 pop3 配置檔案。(預設沒有,要自己建立) /etc/pam.d/pop3 檔案内容如: 代碼 : auth required /lib/security/pam_stack.so service=system-auth auth required pam_unix.so auth required pam_listfile.so item=user sense=deny file=/etc/security/dovecot.deny onerr=fail account required /lib/security/pam_stack.so service=system-auth #account required pam_access.so account required pam_unix.so 用了 pam_listfile.so 子產品,當然也可以用 pam_access.so 子產品來限定。 61 ☆ dovecot.conf
## Dovecot 1.0 configuration file # Default values are shown after each value, it's not required to uncomment # any of the lines. Exception to this are paths, they're just examples # with real defaults being based on configure options. The paths listed here # are for configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var # --with-ssldir=/usr/share/ssl # 運作時存儲資料的目錄 #base_dir = /var/run/dovecot/ # 加入你需要的的服務 : # imap imaps pop3 pop3s #protocols = imap imaps # 監聽的 IP 或者主機位址 . # It's not currently possible to specify multiple addresses. # "*" 是指定監聽所有 IPv4 的 interfaces. # "[::]" 監聽所有 IPv6 的 interfaces, 可能會監聽所有的 IPv4 的 interfaces 但是這個取決于作業系統 # 指定端口的格式是 :"host:port". imap_listen = [::] pop3_listen = [::] # 監聽 SSL 連接配接的 IP 或主機位址 . # Defaults to above non-SSL equilevants if not specified. #imaps_listen = #pop3s_listen = # 禁用 SSL/TLS 支援 . #ssl_disable = no # PEM encoded X.509 SSL/TLS certificate and private key. They're opened before # dropping root privileges, so keep the key file unreadable by anyone but # root. Included doc/mkcert.sh can be used to easily generate self-signed # certificate, 要保證在 dovecot-openssl.cnf 中更新域 #ssl_cert_file = /usr/share/ssl/certs/dovecot.pem #ssl_key_file = /usr/share/ssl/private/dovecot.pem # SSL parameter file. Master process generates this file for login processes. # It contains Diffie Hellman and RSA parameters. #ssl_parameters_file = /var/run/dovecot/ssl-parameters.dat # How often to regenerate the SSL parameters file. Generation is quite CPU # intensive operation. The value is in hours, 0 disables regeneration # entirely. #ssl_parameters_regenerate = 24 # Disable LOGIN command and all other plaintext authentications unless # SSL/TLS is used (LOGINDISABLED capability). Note that 127.*.*.* and # IPv6 ::1 addresses are considered secure, this setting has no effect if # you connect from those addresses. #disable_plaintext_auth = yes # Use this logfile instead of syslog(). /dev/stderr can be used if you want to # use stderr for logging (ONLY /dev/stderr - otherwise it is closed). #log_path = # For informational messages, use this logfile instead of the default #info_log_path = # Prefix for each line written to log file. % codes are in strftime(3) # format. #log_timestamp = "%b %d %H:%M:%S " ## ## Login processes ## # Directory where authentication process places authentication UNIX sockets # which login needs to be able to connect to. The sockets are created when # running as root, so you don't have to worry about permissions. Note that # everything in this directory is deleted when Dovecot is started. login_dir = /var/run/dovecot-login # chroot login process to the login_dir. Only reason not to do this is if you # wish to run the whole Dovecot without roots. #login_chroot = yes ## ## IMAP login process ## login = imap # Executable location. #login_executable = /usr/libexec/dovecot/imap-login # User to use for the login process. Create a completely new user for this, # and don't use it anywhere else. The user must also belong to a group where # only it has access, it's used to control access for authentication process. #login_user = dovecot # Set max. process size in megabytes. If you don't use # login_process_per_connection you might need to grow this. #login_process_size = 32 # Should each login be processed in it's own process (yes), or should one # login process be allowed to process multiple connections (no)? Yes is more # secure, espcially with SSL/TLS enabled. No is faster since there's no need # to create processes all the time. #login_process_per_connection = yes # Number of login processes to create. If login_process_per_user is # yes, this is the number of extra processes waiting for users to log in. #login_processes_count = 3 # Maximum number of extra login processes to create. The extra process count # usually stays at login_processes_count, but when multiple users start logging # in at the same time more extra processes are created. To prevent fork-bombing # we check only once in a second if new processes should be created - if all # of them are used at the time, we double their amount until limit set by this # setting is reached. This setting is used only if login_process_per_use is yes. #login_max_processes_count = 128 # Maximum number of connections allowed in login state. When this limit is # reached, the oldest connections are dropped. If login_process_per_user # is no, this is a per-process value, so the absolute maximum number of users # logging in actually login_processes_count * max_logging_users. #login_max_logging_users = 256 ## ## POP3 login process ## # Settings default to same as above, so you don't have to set anything # unless you want to override them. login = pop3 # Exception to above rule being the executable location. #login_executable = /usr/libexec/dovecot/pop3-login ## ## Mail processes ## # Maximum number of running mail processes. When this limit is reached, # new users aren't allowed to log in. #max_mail_processes = 1024 # Show more verbose process titles (in ps). Currently shows user name and # IP address. Useful for seeing who are actually using the IMAP processes # (eg. shared mailboxes or if same uid is used for multiple accounts). #verbose_proctitle = no # Show protocol level SSL errors. #verbose_ssl = no # Valid UID range for users, defaults to 500 and above. This is mostly # to make sure that users can't log in as daemons or other system users. # Note that denying root logins is hardcoded to dovecot binary and can't # be done even if first_valid_uid is set to 0. #first_valid_uid = 500 #last_valid_uid = 0 # Valid GID range for users, defaults to non-root/wheel. Users having # non-valid GID as primary group ID aren't allowed to log in. If user # belongs to supplementary groups with non-valid GIDs, those groups are # not set. #first_valid_gid = 1 #last_valid_gid = 0 # Grant access to these extra groups for mail processes. Typical use would be # to give "mail" group write access to /var/mail to be able to create dotlocks. #mail_extra_groups = # ':' separated list of directories under which chrooting is allowed for mail # processes (ie. /var/mail will allow chrooting to /var/mail/foo/bar too). # This setting doesn't affect login_chroot or auth_chroot variables. # WARNING: Never add directories here which local users can modify, that # may lead to root exploit. Usually this should be done only if you don't # allow shell access for users. See doc/configuration.txt for more information. #valid_chroot_dirs = # Default chroot directory for mail processes. This can be overridden by # giving /./ in user's home directory (eg. /home/./user chroots into /home). #mail_chroot = # Default MAIL environment to use when it's not set. By leaving this empty # dovecot tries to do some automatic detection as described in # doc/mail-storages.txt. There's a few special variables you can use: # %u - username # %n - user part in [email protected], same as %u if there's no domain # %d - domain part in [email protected], empty if user there's no domain # %h - home directory # You can also limit a width of string by giving the number of max. characters # after the '%' character. For example %1u gives the first character of # username. Some examples: # default_mail_env = maildir:/var/mail/%1u/%u/Maildir # default_mail_env = mbox:~/mail/:INBOX=/var/mail/%u # default_mail_env = mbox:/var/mail/%d/%n/:INDEX=/var/indexes/%d/%n #default_mail_env = # Space-separated list of fields to cache for all mails. Currently these # fields are allowed followed by a list of commands they speed up: ## Envelope - FETCH ENVELOPE and SEARCH FROM, TO, CC, BCC, SUBJECT, # SENTBEFORE, SENTON, SENTSINCE, HEADER MESSAGE-ID, # HEADER IN-REPLY-TO # Body - FETCH BODY # Bodystructure - FETCH BODY, BODYSTRUCTURE # MessagePart - FETCH BODY[1.2.3] (ie. body parts), RFC822.SIZE, # SEARCH SMALLER, LARGER, also speeds up BODY/BODYSTRUCTURE # generation. This is always set with mbox mailboxes, and # also default with Maildir. # Different IMAP clients work in different ways, that's why Dovecot by default # only caches MessagePart which speeds up most operations. Whenever client # does something where caching could be used, the field is automatically marked # to be cached later. For example after FETCH BODY the BODY will be cached # for all new messages. Normally you should leave this alone, unless you know # what most of your IMAP clients are. Caching more fields than needed makes # the index files larger and generate useless I/O. # With maildir there's one extra optimization - if nothing is cached, indexing # the maildir becomes much faster since it's not opening any of the mail files. # This could be useful if your IMAP clients access only new mails. #mail_cache_fields = MessagePart # Space-separated list of fields that Dovecot should never set to be cached. # Useful if you want to save disk space at the cost of more I/O when the fields # needed. #mail_never_cache_fields = # Workarounds for various client bugs: # oe6-fetch-no-newmail: # Never send EXISTS/RECENT when replying to FETCH command. Outlook Express # seems to think they are FETCH replies and gives user "Message no longer # in server" error. Note that OE6 still breaks even with this workaround # if synchronization is set to "Headers Only". # outlook-idle: # Outlook and Outlook Express never abort IDLE command, so if no mail # arrives in half a hour, Dovecot closes the connection. This is still # fine, except Outlook doesn't connect back so you don't see if new mail # arrives. # outlook-pop3-no-nuls: # Outlook and Outlook Express hang if mails contain NUL characters. # This setting replaces them with 0x80 character. #client_workarounds = # Dovecot can notify client of new mail in selected mailbox soon after it's # received. This setting specifies the minimum interval in seconds between # new mail notifications to client - internally they may be checked more or # less often. Setting this to 0 disables the checking. # NOTE: Evolution client breaks with this option when it's trying to APPEND. #mailbox_check_interval = 0 # Like mailbox_check_interval, but used for IDLE command. #mailbox_idle_check_interval = 30 # Allow full filesystem access to clients. There's no access checks other than # what the operating system does for the active UID/GID. It works with both # maildir and mboxes, allowing you to prefix mailboxes names with eg. /path/ # or ~user/. #mail_full_filesystem_access = no # Maximum allowed length for custom flag name. It's only forced when trying # to create new flags. #mail_max_flag_length = 50 # Save mails with CR+LF instead of plain LF. This makes sending those mails # take less CPU, especially with sendfile() syscall with Linux and FreeBSD. # But it also creates a bit more disk I/O which may just make it slower. #mail_save_crlf = no # Use mmap() instead of read() to read mail files. read() seems to be a bit # faster with my Linux/x86 and it's better with NFS, so that's the default. #mail_read_mmaped = no # By default LIST command returns all entries in maildir beginning with dot. # Enabling this option makes Dovecot return only entries which are directories. # This is done by stat()ing each entry, so it causes more disk I/O. # (For systems setting struct dirent->d_type, this check is free and it's # done always regardless of this setting) #maildir_stat_dirs = no # Copy mail to another folders using hard links. This is much faster than # actually copying the file. This is problematic only if something modifies # the mail in one folder but doesn't want it modified in the others. I don't # know any MUA which would modify mail files directly. IMAP protocol also # requires that the mails don't change, so it would be problematic in any case. # If you care about performance, enable it. #maildir_copy_with_hardlinks = no # Check if mails' content has been changed by external programs. This slows # down things as extra stat() needs to be called for each file. If changes are # noticed, the message is treated as a new message, since IMAP protocol # specifies that existing messages are immutable. #maildir_check_content_changes = no # Which locking methods to use for locking mbox. There's three available: # dotlock: Create <mailbox>.lock file. This is the oldest and most NFS-safe # solution. If you want to use /var/mail/ like directory, the users # will need write access to that directory. # fcntl : Use this if possible. Works with NFS too if lockd is used. # flock : May not exist in all systems. Doesn't work with NFS. # You can use both fcntl and flock too; if you do the order they're declared # with is important to avoid deadlocks if other MTAs/MUAs are using both fcntl # and flock. Some operating systems don't allow using both of them # simultaneously, eg. BSDs. If dotlock is used, it's always created first. mbox_locks = fcntl # Should we create dotlock file even when we want only a read-lock? Setting # this to yes hurts the performance when the mailbox is accessed simultaneously # by multiple processes, but it's needed for reliable reading if no other # locking methods are available. #mbox_read_dotlock = no # Maximum time in seconds to wait for lock (all of them) before aborting. #mbox_lock_timeout = 300 # If dotlock exists but the mailbox isn't modified in any way, override the # lock file after this many seconds. #mbox_dotlock_change_timeout = 30 # umask to use for mail files and directories #umask = 0077 # Drop all privileges before exec()ing the mail process. This is mostly # meant for debugging, otherwise you don't get core dumps. Note that setting # this to yes means that log file is opened as the logged in user, which # might not work. It could also be a small security risk if you use single UID # for multiple users, as the users could ptrace() each others processes then. #mail_drop_priv_before_exec = no ## ## IMAP process ## # Executable location #imap_executable = /usr/libexec/dovecot/imap # Set max. process size in megabytes. Most of the memory goes to mmap()ing # files, so it shouldn't harm much even if this limit is set pretty high. #imap_process_size = 256 # Support for dynamically loadable modules. #imap_use_modules = no #imap_modules = /usr/lib/dovecot/imap ## ## POP3 process ## # Executable location #pop3_executable = /usr/libexec/dovecot/pop3 # Set max. process size in megabytes. Most of the memory goes to mmap()ing # files, so it shouldn't harm much even if this limit is set pretty high. #pop3_process_size = 256 # Support for dynamically loadable modules. #pop3_use_modules = no #pop3_modules = /usr/lib/dovecot/pop3 ## ## Authentication processes ## # An Authentication process is a child process used by Dovecot that # handles the authentication steps. The steps cover an authentication # mechanism (auth_mechanisms, how the client authenticates in the IMAP or # POP3 protocol), which password database should be queried (auth_passdb), # and which user database should be queried (auth_userdb, to obtain # UID, GID, and location of the user's mailbox/home directory). # You can have multiple processes, though a typical configuration will # have only one. Each time "auth = xx" is seen, a new process # definition is started. The point of multiple processes is to be able # to set stricter permissions. (See auth_user below.) # Just remember that only one Authentication process is asked for the # password, so you can't have different passwords accessible through # different process definitions (unless they have different # auth_mechanisms, and you're ok with having different password for # each mechanisms). # Authentication process name. auth = default # Specifies how the client authenticates in the IMAP protocol. # Space separated list of permitted authentication mechanisms: # anonymous plain digest-md5 cram-md5 # anonymous - No authentication required. # plain - The password is sent as plain text. All IMAP/POP3 clients # support this, and the password can be encrypted by Dovecot to match # any of the encryption schemes used in password databases. # digest-md5 and cram-md5 - both encrypt the password so it is more # secure in transit, but are not well supported by clients, and # require that the password database use a matching encryption # scheme (or be in plaintext). # See auth.txt for more details. # If you are using SSL there is less benefit to digest-md5 and # cram-md5 as the communication is already encrypted. auth_mechanisms = plain # Space separated list of realms for SASL authentication mechanisms that need # them. You can leave it empty if you don't want to support multiple realms. # Many clients simply use the first one listed here, so keep the default realm # first. #auth_realms = # Default realm/domain to use if none was specified. This is used for both # SASL realms and appending @domain to username in plaintext logins. #auth_default_realm = # Where user database is kept: # passwd: /etc/passwd or similiar, using getpwnam() # passwd-file <path>: passwd-like file with specified location # static uid=<uid> gid=<gid> home=<dir template>: static settings # vpopmail: vpopmail library # ldap <config path>: LDAP, see doc/dovecot-ldap.conf # pgsql <config path>: a PostgreSQL database, see doc/dovecot-pgsql.conf auth_userdb = passwd # Where password database is kept: # passwd: /etc/passwd or similiar, using getpwnam() # shadow: /etc/shadow or similiar, using getspnam() # pam [<service> | *]: PAM authentication # passwd-file <path>: passwd-like file with specified location # vpopmail: vpopmail authentication # ldap <config path>: LDAP, see doc/dovecot-ldap.conf # pgsql <config path>: a PostgreSQL database, see doc/dovecot-pgsql.conf #auth_passdb = pgsql /usr/local/etc/dovecot-pgsql.conf auth_passdb = pam #auth_executable = /usr/libexec/dovecot/dovecot-auth # Set max. process size in megabytes. #auth_process_size = 256 # User to use for the process. This user needs access to only user and # password databases, nothing else. Only shadow and pam authentication # requires roots, so use something else if possible. Note that passwd # authentication with BSDs internally accesses shadow files, which also # requires roots. auth_user = root # Directory where to chroot the process. Most authentication backends don't # work if this is set, and there's no point chrooting if auth_user is root. #auth_chroot = # Number of authentication processes to create #auth_count = 1 # List of allowed characters in username. If the user-given username contains # a character not listed in here, the login automatically fails. This is just # an extra check to make sure user can't exploit any potential quote escaping # vulnerabilities with SQL/LDAP databases. If you want to allow all characters, # set this value to empty. #auth_username_chars = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890.-_@ # Username to use for users logging in with ANONYMOUS SASL mechanism #auth_anonymous_username = anonymous # More verbose logging. Useful for figuring out why authentication isn't # working. #auth_verbose = no # Even more verbose logging for debugging purposes. Shows for example SQL # queries. #auth_debug = no # digest-md5 authentication process. It requires special MD5 passwords which # /etc/shadow and PAM doesn't support, so we never need roots to handle it. # Note that the passwd-file is opened before chrooting and dropping root # privileges, so it may be 0600-root owned file. #auth = digest_md5 #auth_mechanisms = digest-md5 #auth_realms = #auth_userdb = passwd-file /etc/passwd.imap #auth_passdb = passwd-file /etc/passwd.imap #auth_user = imapauth #auth_chroot = # if you plan to use only passwd-file, you don't need the two auth processes, # simply set "auth_methods = plain digest-md5" 62 ☆ Mail 測試
[[email protected] root]# mail [email protected] Subject: test by me this is a test. CC: [[email protected] root]# mailq Mail queue is empty [[email protected] root]# tail /var/log/maillog 使用 mailq 來檢視郵件隊列是否有錯誤,并檢視 /var/log/mail/* 是否有錯誤資訊。如果一切正常,說明信件已經發送到 tester 了。 63 ☆測試 pop/imap
測試收信,先測試 POP3: telnet panda 110 然後輸入: user username +OK pass password ( 注意是 pass) 如果 LOGIN 成功,會有提示。 +OK Logged in. list +OK 2 messages: 1 849 2 823 這樣的傳回資訊說明已經一切正常,可以提供服務了。 STAT +OK 2 1672 TOP 1 +OK Return-Path: <[email protected]> Received: from panda ([192.168.152.1]) by panda.panda.com (8.13.1/8.13.1) with SMTP id k7KJE2DZ004002 for <[email protected]>; Mon, 21 Aug 2006 03:14:02 +0800 Date: Mon, 21 Aug 2006 03:14:02 +0800 From: [email protected] Message-Id: <[email protected]> To: <[email protected]> Subject: =?utf-8?B?TWljcm9zb2Z0IE9mZmljZSBPdXRsb29rIOa1i+ivlea2iOaBrw==?= MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-IMAPbase: 1154223507 6 Status: O X-UID: 5 Content-Length: 120 X-Keywords: DELE 1 +OK Marked to be deleted. QUIT +OK Sayonara Connection closed by foreign host. 再測試 IMAP: imtest -m login -a [email protected] localhost C: C01 CAPABILITY S: * OK mail.cngnu.org Cyrus IMAP4 v2.1.16 server ready S: * CAPABILITY IMAP4 IMAP4rev1 ACL QUOTA LITERAL+ NAMESPACE UIDPLUS ID NO_ATOMIC_RENAME UNSELECT MULTIAPPEND SORT THREAD=ORDEREDSUBJECT THREAD=REFERENCES IDLE S: C01 OK Completed Password: C: L01 LOGIN virtualuser {6} + go ahead C: <omitted> L01 OK User logged in Authenticated. Security strength factor: 0 . select inbox * FLAGS (/Answered /Flagged /Draft /Deleted /Seen) * OK [PERMANENTFLAGS (/Answered /Flagged /Draft /Deleted /Seen /*)] * 1 EXISTS * 1 RECENT * OK [UIDVALIDITY 1021736432] * OK [UIDNEXT 3] . OK [READ-WRITE] Completed . fetch 1:1 (FLAGS BODY[HEADER.FIELDS (DATE FROM)]) * 1 FETCH (FLAGS (/Seen) BODY[HEADER.FIELDS (DATE FROM)] {68} Date: Mon, 20 May 2002 09:26:09 +0800 (CST) From: [email protected] ) . OK Completed . logout * BYE LOGOUT received . OK Completed 64 ☆ procmail
郵件分揀 Linux 的 sendmail 使用 procmail 作為信件的最終投遞代理。這個程式有一些非常有用的 ,對于我們來說,最重要的功能是信件的自動過濾和分揀功能。 信件分揀大概是使用者最希望的功能,它按照郵件的檔案頭 ( 發信位址,收信位址等等 ) 甚至郵件的正文進行歸類,并且可以自動存放在各個檔案中或者轉發給别的使用者賬号 。馬上我們會看到,用 procmail 配置自動分揀是一件非常容易的事情。 通常的 sendmail 配置中已經使用了 procmail 作為郵件最終投遞代理,如果你的 sendmail 已經改亂了,可以使用 FEATURE(local_procmail) 設定這個功能。 procmail 主要依靠使用者宿主目錄下面的 .procmailrc 中的資訊來處理郵件 . 如果這個配 制檔案不存在 , 則 procmail 隻是簡單的将郵件儲存到使用者的信箱中 . 一般來說, .procmailrc 檔案由配置行和行為規則組成,規則的一般格式是 選項 [ 零個或多個條件 , 每個一行 ] [ 動作指令 ] [ 動作指令 ] 表示開始一條規則,後面可以加上一些單字元的選項,選項主要有下面的一些 : H 搜尋比對郵件頭部 B 搜尋比對整個郵件 D 比對時區分大小寫 A 如果前面最近的一個沒有 A 或 a 選項的規則執行 , 執行本規則 a 如果上面一條規則執行 , 則本規則執行 E 同 A 相反 , 前面最近的沒有 E 或 e 選項的規則沒有執行 , 則執行本規則 e 同 a 相反 h 通過管道傳送郵件首部 ( 預設 ) b 通過管道傳送郵件主體 ( 預設 ) c 複制一個郵件 I 忽略所有寫操作中的錯誤 r 原始模式 , 即 procmail 不對 mail 進行任何模式的處理 條件用一個 * 号開始,後面跟上正規表達式 動作指令就是 procmail 在規則成功之後使用的指令,一般的指令有下面一些 : {} 開始一個語句段,表示把一組指令集合成一個動作。如果你要嵌套處理規則, ( 例如 ,對于來自 test 的郵件,區分是含有 computer 字元串還是含有 physics 字元串 ) 那麼必須 使用語句段把子規則括起來。 ! 轉發信件給某個使用者 | 啟用管道将郵件傳送給後面的程式,例如 |auto-reply 表示啟動 auto-reply 程式并且将 郵件内容作為标準輸入傳遞給它。 任意檔案名 将郵件存入某個檔案。如果檔案已經存在,就添加在檔案的末尾。 對于熟悉 perl 或 C 語言的的使用者,很容易用 procmail 的管道功能做出郵件的自動回複程式,這裡不再介紹了,想進一步了解 procmail 的使用者可以用 65 ☆使用 POP 用戶端
所有的現在的郵件使用者代理( MUA ),例如 netscape,elm,Outlook,pine 和 mutt 都是使用 POP 的,可以被用作 POP 的用戶端。每一個的配置都有所不同。同樣有一個流行字元界面的的 POP 用戶端叫做 fetchmail 。 fetchmail 是高度的可配置的,可以查詢多個郵箱,可以作為守護程序運作,這樣使得其每五分鐘查詢使用者的郵箱。 fetchmail 在主機上遞送郵件到郵件傳送代理( MTA ),例如 sendmail 。我們将勾畫出以後如何安裝 fetchmail 和使用其來查詢我們裝過的 POP 伺服器。 注意到有很多選項可以影響 fetchmail 的行為。建立一個 ~/.fetchmailrc 檔案如下所示 : ~student/.fetchmailrc poll stationX.exmaple.com with protocol pop3: user studentXX there is user studentXX here password "password" 由于密碼存儲在該檔案中,是以 fetchmail 将會拒絕運作除非您把該檔案的屬性設定為對于僅僅檔案的所有者隻讀。注意還可以使用 chown 改變由 root 建立的檔案的所有者為 studentXX 。 chmod 600 ~student/.fetchmailrc chown student.student ~student/.fetchmailrc 嘗試使用 studentXX 登陸到 POP3 郵箱 echo "hello student" | mail -s "Hola" student su - student fetchmail -v exit fetchmail 能不能接收到 student 的 POP 郵件?将遞送 student 的郵件到哪裡?比從本地擷取 POP 郵件有意義麼? 讓您的夥伴在另外一台機器上建立相同的 ~/.fetchmailrc 檔案(或者配置其它諸如 mozilla 的 MTA )試圖從您的伺服器上進行收信。 66 ☆ fetchmail
許多使用者有多個郵件賬戶,有些在你的管理範圍之内,有些在别的伺服器上面。管理 所有這些郵件是非常惡心的事情。另外,也許你的系統僅僅是一個撥号代理 ( 參考第八 章 ) ,不可能始終接在 internet 上面。你需要的是在系統連接配接到 internet 的時候發出電 子郵件,同時自動去接收電子郵件。在國内,典型的做法是每人申請一個本地電子郵件 賬戶和一個免費電子郵件賬号,問題就是,如何從另外的 ISP 提供的電子郵件賬号哪裡自 動地接收郵件? 當然你可以讓你的使用者自己解決這個問題,不過這種僵硬的方法不見得合适。一般我 們采用另外的辦法,就是 fetchmail ,它是一個自動的郵件接力程式,可以讓它從遠端的 pop3 賬号處取得郵件,然後扔進使用者的本地郵箱。 ( 關于 pop3 的情況參考下一節 ) 可以直接用指令行調用 fetchmail: $ fetchmail -p [ 協定 ] -u [ 使用者名 ] [ 伺服器 ] fetchmail 得到的資訊将直接投入 localhost 機器,賬号是你啟動 fetchmail 時使用的賬 号。 $ fetchmail -p POP3 -u yuanban mail.asnc.edu.cn Enter password for [email protected]: 使用 pop3 協定從 mail.asnc.edu.cn 取得 yuanban 的信件。 fetchmail 要求你輸入 yuanba n 的密碼,然後進行驗證,成功的話會出現下面的資訊 : 1 message for yuanban at mail.asnc.edu.cn (551840 octets). reading message 1 of 1 (551840 octets) ................................... .....d 表示已經完成了一封信件的轉交。 對于更複雜的情況,建議你使用 fetchmail 的配置檔案,即使用者宿主目錄下面的 .fetc hmailrc 。這個檔案的詳細資料可以參考 fetchmail 的文檔,這裡隻用一個簡單地例子來 介紹它。我們看一個簡單的 .fetchmailrc 範例 : set syslog set postmaster "isee" poll 202.96.44.11 with proto POP3 and options envelope Delivered-To: user "mere" there with password "xxxxxxx" is [email protected] here no keep user "isee" there with password "yyyyyyy" is [email protected] here no keep 這個範例相當簡單。 fetchmail 在讀取配置檔案的時候會忽略所有的 "there","here", "and","with","has","wants","options" 之類的單詞。 第一行 set syslog 程式定義 fetchmail 的記錄檔案使用系統記錄功能。 set postmaste r 設定的是出錯時的管理者賬号,接下來的 poll 行定義了 fetchmail 使用的協定為 POP3 , 連接配接的伺服器是 202.96.44.11 。 envelope Delivered-To: 這個選項比較有趣,它在信封上加上一個 Delivered-To 說明 ,這主要是為了讓 fetchmail 可以把信件投遞到一些使用 qmail( 見下面 ) 的系統中。 接下來是 fetchmail 的主體部分,一個 user 的行定義了一個接力方法,現在的定義是取 得 202.96.44.11 伺服器上 mere 賬号的信件,取信的密碼是 xxxxxxx ,然後投遞到 [email protected] ail.home ,同樣, isee 的信件也被自動投遞到 [email protected] 。 no-keep 選項表示不在 原始伺服器上保留郵件。 下面是配置 fetchmail 的一些常用選項 : set logfile 制定 log 檔案 set syslog 使用系統 log 檔案 via 指定 DNS 機器名來取代 poll 中的機器明 proto 指定協定 port 指定端口 timeout 指定逾時時間 interface 指定網絡界面 user 指定遠端使用者 is 将本地使用者和遠端使用者聯系起來 to 同 is pass 密碼 preconnect 連接配接開始前執行的外部指令 postconnect 連接配接結束後執行的外部指令 keep 在伺服器上保留郵件備份 no keep 不保留郵件備份 在配置了 .fetchmailrc 之後,隻要直接執行 fetchmail 就可以實作自動的信件投遞了。 實際上,你甚至可能使用 fetchmail 作為一個 daemon 程式來實作自動的郵件轉交,或是設 置 fetchmail 讓它支援 qmail 的虛拟域。 在 fetchmail 的發行版本中還提供了一個圖形化的 fetchmail 配置程式 , 稱為 fetchmail conf 。如果你對 fetchmail 的文法感到困難,隻要使用這個程式就可以進行配置。啟動這 個程式的方法很簡單,直接在 xterm 下面執行 fetchmailconf &: 圖 7.1 配置 fetchmail 選擇 configure fetchmail 出現 : 圖 7.2 配置 fetchmail(2) 隻要選擇 Novice Configuration ,然後出現伺服器設定選單 : 圖 7.3 配置 fetchmail(3) 在 New Server 中加入你準備收信的伺服器,然後回車,伺服器名字就會出現在清單框 中,輕按兩下伺服器名字,編輯關于伺服器的設定 : 圖 7.4 配置 fetchmail(4) 設定伺服器的類型和伺服器上的賬戶名字,輕按兩下賬戶名字并且輸入對應的密碼以及和 本地賬戶之間的對應關系, OK 退出就可以了 67 ☆ squirrelmail
rpm -q squirrelmail squirrelmail 的主配置檔案為 /etc/squirrelmail/config.php 更改這個配置用 /usr/share/squirrelmail/config/conf.pl 更友善