LPI Linux 認證/LPIC2 考試 202/郵件和新聞
|
|
- 配置郵件列表
- 使用 sendmail
- 使用 Postfix
- 管理郵件流量
- 提供新聞
- 配置郵件列表
Majordomo 是一個郵件列表管理程式。它的目標是處理傳送到特定電子郵件地址的所有郵件,並將它們重新分發到一個電子郵件地址列表。Majordomo 還處理向列表新增和刪除電子郵件地址。
由於 Majordomo 負責管理電子郵件列表,因此它嚴重依賴於 MTA,例如 Sendmail、Smail、Qmail 或 Postfix。
aliases 檔案(通常為 /etc/aliases)用於為電子郵件地址建立別名。例如,一旦 Majordomo 安裝完成,aliases 檔案中通常會有一個條目:
majordomo-owner: jarchie
此條目表示所有傳送到 majordomo-owner@host.com 的郵件實際上將傳送到 jarchie@host.com。注意,沒有必要在 jarchie 後新增 @host.com,因為兩個使用者都在同一個主機上。如果需要將郵件重定向到不同主機上的不同使用者,則需要新增 @host.com 部分。
aliases 檔案中的另一種型別的條目允許將電子郵件重定向到檔案中列出的多個地址:
testlist: :include:/usr/local/majordomo-1.94.5/lists/testlist
此條目指出,傳送到 testlist@host.com 的任何郵件將被重定向到 /usr/local/majordomo-1.94.5/lists/testlist 檔案中列出的所有地址。testlist 檔案可能類似於以下內容:
johnarchie@emeraldis.com srobirds@yahoo.com acreswell@geocities.com
Majordomo 能夠透過利用此功能來新增或刪除列表中的地址。當處理訂閱請求時,使用者的電子郵件地址將被附加到 testlist 檔案;當處理取消訂閱請求時,使用者的電子郵件地址將從 testlist 檔案中刪除。還可以透過使用文字編輯器(如 vi)編輯檔案來手動新增或刪除地址。
由於 Majordomo 需要能夠處理透過電子郵件傳送給它的命令,因此 Sendmail 必須能夠執行 Majordomo 程式並將郵件傳遞給它。這是透過在 aliases 檔案中新增另一種型別的條目來完成的:
majordomo: "|/usr/local/majordomo-1.94.5/wrapper majordomo"
/usr/local/majordomo-1.94.5/wrapper "majordomo" |
程式 /usr/local/majordomo-1.94.5/wrapper 是一個包裝器(根據配置,是 SUID 和 SGID majordomo 或守護程序),它執行 Majordomo 程式。別名條目第二部分周圍的引號用於告訴 Sendmail 此條目的這部分是一個語句;如果包裝器和 majordomo 之間沒有空格,引號將是不必要的。| 被稱為“管道”;它用於告訴 Sendmail 透過標準輸入將電子郵件傳送給包裝器。(由於包裝器在這裡只調用 majordomo,因此電子郵件實際上是傳送給 Majordomo 的。)包裝器接受一個引數——它要執行的程式的引數。(第一個引數之後的任何引數都將被傳遞給包裝器正在執行的程式。)出於安全原因,包裝器只執行位於 Majordomo 目錄 /usr/local/majordomo-1.94.5/ 中的程式。此限制防止程式設計師使用包裝器執行不應該具有 Majordomo 許可權的程式。(例如,wrapper /bin/vi 將允許任何使用者編輯任何 Majordomo 配置檔案。)當郵件傳送到 majordomo@host.com 時,Sendmail 啟動包裝器,包裝器又啟動 majordomo,然後 Sendmail 透過標準輸入將郵件傳送到 majordomo 指令碼。然後,Majordomo 從郵件中提取命令並做出相應的響應。
Majordomo 當然是我們文件的主題程式碼;它由一系列 Perl 指令碼組成,其唯一目的是管理郵件列表。
Majordomo 必須在特定的 UID 和 GID 下執行,以便在執行任何指令碼時,它們都將在 Majordomo 的 UID 下執行。因此,有必要確定 Majordomo 應該在哪個 UID 和 GID 下執行。此外,Majordomo 必須是 Sendmail 可信使用者。
majordomo:x:16:16:Majordomo List Manager:/usr/local/majordomo-1.94.5:
檢查 /etc/passwd 和 /etc/group 檔案以找到未被使用的 UID 和 GID。在本示例中,選擇了 UID 16 和 GID 16。您必須決定 Majordomo 指令碼將駐留在哪個位置。如果您使用的是陰影密碼檔案,請新增類似於以下內容的條目:
majordomo:*:10883:0:88888:7:::
到您的 /etc/passwd 檔案中,並在 /etc/shadow 中新增相應的條目。
以這些檔案中的其他條目為指導,瞭解應該新增的確切內容。這些僅僅是我係統的值。如果您沒有使用陰影密碼,則只需要在 /etc/passwd 檔案中新增一個條目。
majordomo:x:16:jarchie
要建立 Majordomo 組,請新增類似於以下內容的行:
到您的 /etc/group 檔案中。在行末新增您的使用者名稱將使您能夠訪問 Majordomo 檔案,這些檔案是組可寫的。
PERL = /bin/perl CC = cc W_HOME = /usr/test/majordomo-$(VERSION) MAN = $(W_HOME)/man W_USER = 123 W_GROUP = 45
Makefile 包含安裝 Majordomo 所需的所有資訊;通常有必要編輯 Makefile 中引用系統特定設定的行,以便 Majordomo 能夠在您的系統上乾淨地安裝。大多數預設設定是正確的;但是,以下設定幾乎總是需要根據系統進行更改。
PERL = /usr/bin/perl CC = gcc W_HOME = /usr/local/majordomo-1.94.5 MAN = /usr/man W_USER = 16 W_GROUP = 16
應更改為更適合您的系統的內容。例如,在我的設定中,這些值已更改為:
此外,必須建立 majordomo.cf 檔案。建立此檔案的一種簡單方法是將提供的 sample.cf 檔案複製到 majordomo.cf 並對其進行編輯。
$whereami = "example.com"; $whoami = "Majordomo\@$whereami"; $whoami_owner = "Majordomo-Owner\@$whereami"; $homedir = "/usr/test/majordomo"; $digest_work_dir = "/usr/local/mail/digest"; $sendmail_command = "/usr/lib/sendmail";
同樣,大多數設定預設情況下是正確的,但是以下行可能需要根據您的系統進行更改,從:
$whereami = "kes.emeraldis.com"; $whoami = "majordomo\@$whereami"; $whoami_owner = "majordomo-owner\@$whereami"; $homedir = "/usr/local/majordomo-1.94.5"; $digest_work_dir = "/usr/local/majordomo-1.94.5/digest"; $sendmail_command = "/usr/sbin/sendmail";
更改為更適合您的系統的內容,例如:
$whoami 和 $whoami_owner 不需要更改即可使 Majordomo 工作;但是,我更改了它們,因為我喜歡避免輸入大寫字母。$digest_work_dir 是一個臨時目錄,應該將摘要檔案放置在該目錄中;此目錄應分配到您希望儲存摘要的位置。如果您不打算使用摘要列表,則不必擔心此選項。$whereami、$homedir 和 $sendmail_command 應更改為適合您的系統的值。與 Makefile 不同,這些選項始終可以在 Majordomo 安裝後透過編輯 Majordomo 安裝目錄中的 majordomo.cf 來更改。(配置檔案在安裝過程中只是被複制了。)
$ make wrapper
下一步是編譯 Majordomo 包裝器。包裝器是唯一需要編譯的 Majordomo 元件,因為所有其他元件都是一系列 perl 指令碼,因此不需要編譯。
# make install # make install-wrapper
要安裝 Majordomo 檔案,請執行以下命令:
第一個命令可以以 Majordomo 使用者身份執行(假設 majordomo 可以建立或訪問 $home_dir),但第二個命令需要以 root 身份執行,以便安裝指令碼可以將 Majordomo 包裝器設定為 SUID root。(由於 majordomo 在建立時沒有登入 shell 或密碼,因此如果您想以 majordomo 身份執行第一個命令,則需要以 root 身份使用 su majordomo 來成為 majordomo。)
majordomo: "|/usr/local/majordomo-1.94.5/wrapper majordomo" owner-majordomo: jarchie majordomo-owner: jarchie
必須為 Majordomo 建立 Sendmail 別名,以便傳送到 Majordomo 的命令可以由 majordomo 處理,並且必須為 Majordomo 所有者建立一個別名,以便人們可以透過標準的 owner-majordomo 地址傳送電子郵件給您。在 aliases 檔案中新增以下條目:
$ /usr/local/majordomo-1.94.5/wrapper config-test
然後測試您的配置,以普通使用者(不是 majordomo 或 root)身份執行:
此程式可以檢測 Majordomo 安裝中的大多數問題。
[root@kes /]# su majordomo [majordomo@kes /]$ touch /usr/local/majordomo-1.94.5/lists/test
要建立列表,請在 Majordomo 列表目錄中建立一個與列表同名的檔案。例如,要建立一個名為 test 的列表,請以 Majordomo 身份建立一個 test 檔案:
test: :include:/usr/local/majordomo-1.94.5/lists/test owner-test: jarchie test-request: "|/usr/local/majordomo-1.94.5/wrapper request-answer test" test-approval: jarchie
並新增相關的別名:
[jarchie@kes jarchie]$ echo lists | mail majordomo
現在透過向 Majordomo 傳送 lists 命令來測試列表的操作:
[jarchie@kes jarchie]$ echo help | mail majordomo
majordomo 應該只需要一秒鐘就能回覆一條訊息,其中包含當前設定的所有列表。接下來,嘗試發出 help 命令。
Majordomo 應該回復所有 Majordomo 接受的命令列表。儲存此訊息以供將來參考可能是一個好主意。
[jarchie@kes jarchie]$ echo subscribe test | mail majordomo
要檢視別名是否正常工作,請嘗試訂閱和取消訂閱您自己到列表中:
您將收到一封包含有關如何確認您的訂閱的說明以及確認您的命令已成功的郵件。在傳送回您的確認資訊後,Majordomo 應該傳送回兩封郵件——一封郵件說明您的訂閱請求已成功,另一封郵件歡迎您加入 test 列表。列表所有者也將收到一封郵件,說明您已訂閱該列表。
[jarchie@kes jarchie]$ echo unsubscribe test | mail majordomo
要取消訂閱列表,請傳送取消訂閱命令:
您應該收到一封郵件,說明您的命令已成功。
test: "|/usr/local/majordomo-1.94.5/wrapper resend -l test test-list" test-list: :include:/usr/local/majordomo-1.94.5/lists/test owner-test: jarchie test-owner: jarchie test-request: "|/usr/local/majordomo-1.94.5/wrapper majordomo -l test"
對於某些列表,可能希望 Majordomo 在訊息到達列表之前對其進行處理。例如,Majordomo 具有 resend 指令碼,可以根據內容(例如禁忌詞)自動過濾訊息,以防止人們向列表傳送 Majordomo 命令,以及其他功能。要使用這些選項,有必要使用一組更好的別名,例如:
Majordomo 許可權設定錯誤很常見,會導致 Majordomo 無法正常工作。幸運的是,Sendmail 和 Majordomo 通常會給出比較清晰的錯誤資訊,提示出現問題。例如,lists 目錄必須對 sendmail 設定的 UID 使用者可執行,通常是 mail 或 daemon。如果 sendmail 無法執行 lists,則需要放寬許可權。
[root@kes root]# chmod +x /usr/local/majordomo-1.94.5/lists
另一個常見問題是 lists 目錄具有組可寫許可權。要解決這個問題,可以清除組可寫位,或者使用 sendmail 選項 IncludeFileInGroupWritableDirPath。
Majordomo 旨在執行在隔離的系統上;指令碼中存在幾個眾所周知的安全漏洞,允許任何能夠執行 wrapper 的本地使用者以 majordomo 使用者身份執行程式碼。如果必須在提供使用者 shell 訪問許可權的系統上執行 Majordomo,建議收緊 wrapper 的許可權。這可以透過清除世界可執行位並將 wrapper 的所有者更改為需要執行 Majordomo 指令碼的使用者來實現。例如,如果 Sendmail 和 MajorCool 都用來執行 wrapper,可以使用以下命令:
[root@kes root]# cp /usr/local/majordomo-1.94.5/wrapper /etc/smrsh/wrapper [root@kes root]# chmod 4750 /usr/local/majordomo-1.94.5/wrapper [root@kes root]# chown root:nobody /usr/local/majordomo-1.94.5/wrapper [root@kes root]# chmod 4750 /etc/smrsh/wrapper [root@kes root]# chown root:mail /etc/smrsh/wrapper
來保護系統。這將允許 sendmail(在 mail 身份下執行)執行 /etc/smrsh/wrapper,同時允許 web 伺服器的 MajorCool(在 nobody 身份下執行)執行 /usr/local/majordomo-1.94.5/wrapper。但是,這種解決方案將允許任何擁有 mail 或 nobody 的 UID 或 GID 的使用者訪問 majordomo 帳戶。為了保護 nobody 帳戶,重要的是不允許普通使用者使用伺服器端包含或 cgi 指令碼,除非這些服務不在 nobody 身份下執行。
關鍵詞、檔案和工具:Majordomo MTA
=== 練習 ===.
Postfix 由 Wietse Venema 編寫和維護,他也是 tcp_wrappers 和 Satan 的作者。Postfix 最初是 VMailer,但 Wietse 在 IBM GPL 下發布了該軟體,IBM 的律師發現 VMailer 與現有的商標過於相似,因此必須更改名稱。Postfix 被編寫為 sendmail 的直接替代品,並且它非常接近於達到目標。有一些“陷阱”可能會讓你陷入困境,但並不嚴重。Wietse 透過 postfix-users 郵件列表積極支援 Postfix,並且還有一個開發人員郵件列表。您可以透過以下方式訂閱 postfix-users 郵件列表
echo subscribe postfix-users | mail majordomo@postfix.org.
您可以透過以下方式訂閱開發人員列表
echo subscribe postfix-testers | mail majordomo@postfix.org.
我們應該提到的最後一個列表是 announce 列表。您可以透過以下方式加入 announce 列表:echo subscribe postfix-announce | mail majordomo@postfix.org。Postfix 開發正在進行中,截至本文撰寫時,這些郵件列表非常活躍。郵件列表的存檔可以在以下位置找到:http://www.egroups.com/group/postfix-users/ 和:http://msgs.SecurePoint.com/postfix/.
當郵件進入 Postfix 郵件系統時,內部的第一站是傳入佇列。下圖顯示了與新郵件相關的主要元件。
該圖顯示了主要的 Postfix 系統元件,以及它們之間主要的資訊流。黃色橢圓形是郵件程式,黃色方框是郵件佇列或檔案,藍色方框是查詢表。
大框中的程式在 Postfix 常駐主守護程序的控制下執行。大框中的資料是 Postfix 郵件系統的財產。
郵件在本地釋出。Postfix sendmail 程式呼叫特權 postdrop 程式,將郵件存入 maildrop 目錄,然後由 pickup 守護程序獲取。此守護程序會進行一些完整性檢查,以保護其餘的 Postfix 系統。
郵件透過網路進入。Postfix SMTP 伺服器接收郵件並進行一些完整性檢查,以保護其餘的 Postfix 系統。SMTP 伺服器可以配置為基於本地或網路黑名單、DNS 查詢和其他客戶端請求資訊來實施 UCE 控制。郵件是由 Postfix 系統本身內部生成的,用於將無法投遞的郵件退回給發件人。bounce 或 defer 守護程序會帶來壞訊息。
郵件由本地投遞代理轉發,要麼透過系統範圍別名資料庫中的條目轉發,要麼透過每個使用者的 .forward 檔案中的條目轉發。這用無標籤箭頭表示。
郵件是由 Postfix 系統本身內部生成的,用於通知郵政管理員出現問題(此路徑也用無標籤箭頭表示)。Postfix 系統可以配置為將 SMTP 協議問題、UCE 策略違規等通知郵政管理員。
cleanup 守護程序實現了新郵件的最終處理階段。它新增缺失的 From: 和其他郵件頭,安排將地址重寫為標準 user@fully.qualified.domain 格式,並可選地從郵件頭中提取收件人地址。cleanup 守護程序將結果作為單個佇列檔案插入傳入佇列,並通知佇列管理器新郵件的到達。cleanup 守護程序可以配置為基於規範和虛擬表查詢來轉換地址。
根據 cleanup 守護程序的請求,trivial-rewrite 守護程序將地址重寫為標準 user@fully.qualified.domain 格式。最初的 Postfix 版本沒有實現重寫語言。實現一個需要很多工作量,而且大多數站點不需要它。相反,Postfix 大量使用表查詢。
Postfix 的主要配置檔案(相當於 /etc/sendmail.cf 的工作副本)是 main.cf。install.cf 檔案包含 Postfix 在 RPM 安裝期間設定的初始設定。master.cf 檔案是 Postfix 的主程序配置檔案。master 檔案中的每一行都描述瞭如何執行郵件器元件程式。在除錯部分,我們將更多地討論這個檔案。postfix-script 是 Postfix 用於安全地執行 Postfix 命令的包裝器,用於 Linux 環境。讓我們仔細看看 install.cf 檔案,因為它包含一些資料,我們將在使用 main.cf 配置 Postfix 時需要這些資料。
install.cf 檔案實際上只是 RPM 中內建的安裝程式使用的預設設定列表。
以下是 main.cf 檔案,其中包含 Wietse Venema 的註釋以及我們建議的更改穿插其中
# Global Postfix configuration file. This file lists only a subset
# of all 100+ parameters. See the sample-xxx.cf files for a full list.
#
# The sample files mentioned above are located in /usr/doc/postfix-19990906_pl06/
# The general format is lines with parameter = value pairs. Lines
# that begin with whitespace continue the previous line. A value can
# contain references to other $names or ${name}s.
# LOCAL PATHNAME INFORMATION
#
# The queue_directory specifies the location of the Postfix queue.
# This is also the root directory of Postfix daemons that run chrooted.
# See the files in examples/chroot-setup for setting up Postfix chroot
# environments on different UNIX systems.
#
queue_directory = /var/spool/postfix
這與 sendmail 用於傳入郵件佇列的目錄相同。
# The program_directory parameter specifies the default location of # Postfix support programs and daemons. This setting can be overruled # with the command_directory and daemon_directory parameters. # program_directory = /some/where/postfix/bin
上面的行必須更正。RPM 預設將 Postfix 二進位制檔案安裝到 /usr/libexec/postfix 中。
# The command_directory parameter specifies the location of all # postXXX commands. The default value is $program_directory. # command_directory = /usr/sbin
上面的行是正確的,可以保留。
# The daemon_directory parameter specifies the location of all Postfix # daemon programs (i.e. programs listed in the master.cf file). The # default value is $program_directory. This directory must be owned # by root. # daemon_directory = /usr/libexec/postfix
上面的行是正確的,可以保留。
# QUEUE AND PROCESS OWNERSHIP # # The mail_owner parameter specifies the owner of the Postfix queue # and of most Postfix daemon processes. Specify the name of a user # account THAT DOES NOT SHARE A GROUP WITH OTHER ACCOUNTS AND THAT # OWNS NO OTHER FILES OR PROCESSES ON THE SYSTEM. In particular, # don't specify nobody or daemon. PLEASE USE A DEDICATED USER. # mail_owner = postfix
上面的行是正確的,可以保留。
# The default_privs parameter specifies the default rights used by # the local delivery agent for delivery to external file or command. # These rights are used in the absence of a recipient user context. # DO NOT SPECIFY A PRIVILEGED USER OR THE POSTFIX OWNER. # #default_privs = nobody
上面的行是正確的,可以保留,但應該取消註釋(例如,刪除開頭的井號)。
# INTERNET HOST AND DOMAIN NAMES # # The myhostname parameter specifies the Internet hostname of this # mail system. The default is to use the fully-qualified domain name # from gethostname(). $myhostname is used as a default value for many # other configuration parameters. # #myhostname = host.domain.name
將上面的行中的值設定為你的機器的完全限定域名 (FQDN)。例如,如果你的主機名為 turkey 且你的域名是 trot.com,那麼你的 FQDN 將是“turkey.trot.com”。你還要取消註釋此行。
#myhostname = virtual.domain.name
上面的行對於大多數配置來說是多餘的,通常可以保留註釋。
# The mydomain parameter specifies the local Internet domain name. # The default is to use $myhostname minus the first component. # $mydomain is used as a default value for many other configuration # parameters. # #mydomain = domain.name
上面的行應該是你的域名,不包括前面新增的主機名。如我們上面給出的示例,這裡正確的 value 應該是 trot.com。不要忘記取消註釋此行。
# SENDING MAIL # # The myorigin parameter specifies the domain that locally-posted # mail appears to come from. The default is to append $myhostname, # which is fine for small sites. If you run a domain with multiple # machines, you should (1) change this to $mydomain and (2) set up # a domain-wide alias database that aliases each user to # user@that.users.mailhost. # #myorigin = $myhostname #myorigin = $mydomain
這裡的說明相當不錯。通常,這裡所做的是讓它預設設定為 $mydomain。確保取消註釋你的選擇。
# RECEIVING MAIL # The inet_interfaces parameter specifies the network interface # addresses that this mail system receives mail on. By default, # the software claims all active interfaces on the machine. The # parameter also controls delivery of mail to user@[ip.address]. # #inet_interfaces = all
同樣,這裡的說明也不錯。只需取消註釋上面列出的行,你應該沒問題。除非你有特殊需求,否則接下來的兩個條目可以保留註釋。你應該不需要它們。
#inet_interfaces = $myhostname #inet_interfaces = $myhostname, localhost # The mydestination parameter specifies the list of domains that this # machine considers itself the final destination for. # The default is $myhostname + localhost.$mydomain. On a mail domain # gateway, you should also include $mydomain. Do not specify the # names of domains that this machine is backup MX host for. Specify # those names via the relay_domains or permit_mx_backup settings for # the SMTP server (see sample-smtpd.cf. # The local machine is always the final destination for mail addressed # to user@[the.net.work.address] of an interface that the mail system # receives mail on (see the inet_interfaces parameter). # Specify a list of host or domain names, /file/name or type:table # patterns, separated by commas and/or whitespace. A /file/name # pattern is replaced by its contents; a type:table is matched when # a name matches a lookup key. Continue long lines by starting the # next line with whitespace. # #mydestination = $myhostname, localhost.$mydomain #mydestination = $myhostname, localhost.$mydomain $mydomain
最常見的做法是選擇上面緊挨著的那一行作為你在這裡的選擇。確保取消註釋它,並在最後兩個條目之間新增一個逗號,因為似乎遺漏了逗號。
#mydestination = $myhostname, localhost.$mydomain, $mydomain, # mail.$mydomain, www.$mydomain, ftp.$mydomain # INTERNET VERSUS INTRANET # The relayhost parameter specifies the default host to send mail to # when no entry is matched in the optional transport(5) table. When # no relayhost is given, mail is routed directly to the destination. # # On an intranet, specify the organizational domain name. If your # internal DNS uses no MX records, specify the name of the intranet # gateway host instead. # # Specify a domain, host, host:port, [address] or [address:port]. # Use the form [destination] to turn off MX lookups. See also the # default_transport parameter if you're connected via UUCP. # #relayhost = $mydomain #relayhost = gateway.my.domain #relayhost = uucphost #relayhost = [mail.$mydomain:9999]
如果你在某種防火牆後面,或者需要偽造信封(將在本文件的後面介紹),則將“relayhost”值設定為你的域名的 MTA。如果此主機將成為域名的 *the* MTA,則保留所有這些註釋。
# DEFAULT TRANSPORT # # The default_transport parameter specifies the default message # delivery transport to use when no transport is explicitly given in # the optional transport(5) table. # #default_transport = smtp
在大多數情況下,上面的行應該取消註釋並保留原樣。
#default_transport = uucp # ADDRESS REWRITING # # Insert text from sample-rewrite.cf if you need to do address # masquerading. # # Insert text from sample-canonical.cf if you need to do address # rewriting, or if you need username->Firstname.Lastname mapping. # ADDRESS REDIRECTION (VIRTUAL DOMAIN) # # Insert text from sample-virtual.cf if you need virtual domain support. # "USER HAS MOVED" BOUNCE MESSAGES # # Insert text from sample-relocated.cf if you need "user has moved" # style bounce messages. Alternatively, you can bounce recipients # with an SMTP server access table. See sample-smtpd.cf. # TRANSPORT MAP # # Insert text from sample-transport.cf if you need explicit routing.
# ALIAS DATABASE # # The alias_maps parameter specifies the list of alias databases used # by the local delivery agent. The default list is system dependent. # On systems with NIS, the default is to search the local alias # database, then the NIS alias database. See aliases(5) for syntax # details. # # If you change the alias database, run "postalias /etc/aliases" (or # wherever your system stores the mail alias file), or simply run # "newaliases" to build the necessary DBM or DB file. # # It will take a minute or so before changes become visible. Use # "postfix reload" to eliminate the delay. # #alias_maps = dbm:/etc/aliases alias_maps = hash:/etc/aliases
alias_maps 行指向 /etc/aliases 檔案,我們在刪除 sendmail 之前保留了該檔案。最佳實踐(推薦)通常更傾向於將所有 Postfix 配置檔案放在一起,因此將此行更改為
alias_maps = hash:/etc/postfix/aliases
可能是個好主意,並且確保你將 aliases 檔案放在 /etc/postfix 中。否則,Postfix 會在啟動時報錯並無法執行。Red Hat Linux 上的預設 db 型別是 hash,因此請確保使用它,就像我們這裡使用的一樣。人們遇到的一個常見錯誤是他們使用 dbm 而不是 hash。不要陷入這個陷阱。
#alias_maps = hash:/etc/aliases, nis:mail.aliases #alias_maps = netinfo:/aliases # The alias_database parameter specifies the alias database(s) that # are built with "newaliases" or "sendmail -bi". This is a separate # configuration parameter, because alias_maps (see above) may specify # tables that are not necessarily all under control by Postfix. # #alias_database = dbm:/etc/aliases #alias_database = dbm:/etc/mail/aliases #alias_database = hash:/etc/aliases
正如說明中所說,如果你想使用 newaliases 命令來處理 aliases 檔案(推薦),你應該取消註釋上面的行,但確保(如果你進行了我們推薦的 alias_maps 部分中的路徑更改)並將其更改為
alias_database = hash:/etc/postfix/aliases
然後確保取消註釋此行,並在啟動 Postfix 之前執行 newaliases 命令。
#alias_database = hash:/etc/aliases, hash:/opt/majordomo/aliases
如果你恰好運行了 majordomo,那麼你應該使用上面的行,而不是僅僅使用 aliases 行。確保 majordomo 檔案的路徑正確。最佳實踐約定是將其放在 /etc/postfix 中。大多數 Red Hat Linux sendmail 安裝都將它放在 /etc/mail/ 中。當我們進入本文件的 listserv 部分時,我們將對此進行更詳細的討論。
# DELIVERED-TO # # The prepend_delivered_header controls when Postfix should prepend # a Delivered-To: message header. # # By default, Postfix prepends a Delivered-To: header when forwarding # mail and when delivering to file (mailbox) or command. Turning off # the Delivered-To: header when forwarding mail is not recommended. # # prepend_delivered_header = command, file, forward # prepend_delivered_header = forward
預設設定可以正常工作,因此你可以保留此部分註釋,除非你有特殊需求或偏好。
# ADDRESS EXTENSIONS (e.g., user+foo) # # The recipient_delimiter parameter specifies the separator between # user names and address extensions (user+foo). See canonical(5), # local(8), relocated(5) and virtual(5) for the effects this has on # aliases, canonical, virtual, relocated and .forward file lookups. # Basically, the software tries user+foo and .forward+foo before # trying user and .forward. # # recipient_delimiter = +
這個也可以保留註釋,除非你有特殊需求或偏好。
# DELIVERY TO MAILBOX # # The home_mailbox parameter specifies the optional pathname of a # mailbox relative to a user's home directory. The default is to # deliver to the UNIX-style /var/spool/mail/user or /var/mail/user. # Specify "Maildir/" for qmail-style delivery (the / is required). # #home_mailbox = Mailbox #home_mailbox = Maildir/
在 Red Hat Linux 系統上,除非你知道自己在做什麼,否則不要修改它。如果你正在從 qmail 轉換到 Postfix(不太可能),那麼它可能會有用。
# The mail_spool_directory parameter specifies the directory where # UNIX-style mailboxes are kept. The default setting depends on the # system type. # # mail_spool_directory = /var/mail # mail_spool_directory = /var/spool/mail
前一行對於 Red Hat Linux 預設值是正確的,因此應該取消註釋並保持原樣。
# The mailbox_command parameter specifies the optional external # command to use instead of mailbox delivery. The command is run as # the recipient with proper HOME, SHELL and LOGNAME environment settings. # Exception: delivery for root is done as $default_user. # # Other environment variables of interest: USER (recipient username), # EXTENSION (address extension), DOMAIN (domain part of address), # and LOCAL (the address localpart). # # Unlike other Postfix configuration parameters, the mailbox_command # parameter is not subjected to $parameter substitutions. This is to # make it easier to specify shell syntax (see example below). # # Avoid shell meta characters because they will force Postfix to run # an expensive shell process. Procmail alone is expensive enough. # #mailbox_command = /some/where/procmail
Red Hat Linux 系統上的預設 MDA 是 procmail。你可以使用命令“which procmail”來驗證路徑,但除非你更改了 procmail 的位置,否則它位於“/usr/bin/procmail”中。不要忘記取消註釋該行。
#mailbox_command = /some/where/procmail -a "$EXTENSION" # The mailbox_transport specifies the optional transport in master.cf # to use after processing aliases and .forward files. This parameter # has precedence over the mailbox_command, fallback_transport and # luser_relay parameters. # #mailbox_transport = cyrus
在預設的 Red Hat Linux 系統上,你應該保持上面的行不變。
# The fallback_transport specifies the optional transport in master.cf # to use for recipients that are not found in the UNIX passwd database. # This parameter has precedence over the luser_relay parameter. # #fallback_transport =
在預設的 Red Hat Linux 系統上,你應該保持上面的行不變。
# The luser_relay parameter specifies an optional destination address
# for unknown recipients. By default, mail for unknown local recipients
# is bounced.
#
# The following expansions are done on luser_relay: $user (recipient
# username), $shell (recipient shell), $home (recipient home directory),
# $recipient (full recipient address), $extension (recipient address
# extension), $domain (recipient domain), $local (entire recipient
# localpart), $recipient_delimiter. Specify ${name?value} or
# ${name:value} to expand value only when $name does (does not) exist.
#
# luser_relay = $user@other.host
# luser_relay = $local@other.host
# luser_relay = admin+$local
你可以選擇在這裡做什麼,但每天收到數百萬封退信可能會很煩人。保持不變(推薦)。
# JUNK MAIL CONTROLS # # The controls listed here are only a very small subset. See the file # sample-smtpd.cf for an elaborate list of anti-UCE controls. # The header_checks parameter restricts what may appear in message # headers. This requires that POSIX or PCRE regular expression support # is built-in. Specify "/^header-name: stuff you do not want/ REJECT" # in the pattern file. Patterns are case-insensitive by default. Note: # specify only patterns ending in REJECT. Patterns ending in OK are # mostly a waste of cycles. # #header_checks = regexp:/etc/postfix/filename #header_checks = pcre:/etc/postfix/filename
以上部分啟用了一個過濾器,你可以使用它來檢測和“退回”與特定正則表示式(REGEXP)匹配的郵件。使用 procmail 和 regexp 或 PCRE 之間的區別在於,這兩個在郵件傳遞之前捕獲郵件,並可以在 SMTP 埠有效地阻止不需要的郵件。
# The relay_domains parameter restricts what domains (and subdomains # thereof) this mail system will relay mail from or to. See the # smtpd_recipient_restrictions restriction in the file sample-smtpd.cf. # # By default, Postfix relays mail only from or to sites in or below # $mydestination, or in the optional virtual domain list. # # Specify a list of hosts or domains, /file/name patterns or type:name # lookup tables, separated by commas and/or whitespace. Continue # long lines by starting the next line with whitespace. A file name # is replaced by its contents; a type:name table is matched when a # (parent) domain appears as lookup key. # # NOTE: Postfix will not automatically forward mail for domains that # list this system as their primary or backup MX host. See the # permit_mx_backup restriction in the file sample-smtpd.cf. # #relay_domains = $mydestination, $virtual_maps
對於任何已經瞭解 MX 記錄工作原理的人來說,這是 Postfix 配置中的一個關鍵元件。家庭使用者可能不需要這一行,但任何處理多個域郵件的人都會需要。
以下是一個示例說明它的用法
relay_domains = $mydestination, /etc/postfix/relay-domains
在這個示例中,你想要為其中繼的域將被放置在 /etc/postfix/relay-domains 檔案中。一行一個,如下所示
here.com mail.here.com there.org mail.there.org
注意:此檔案*不*被雜湊或對映。它是一個簡單的文字檔案。你也可以使用 IP 地址而不是名稱。
# The mynetworks parameter specifies the list of networks that are # local to this machine. The list is used by the anti-UCE software # to distinguish local clients from strangers. See permit_mynetworks # and smtpd_recipient_restrictions in the file sample-smtpd.cf file. # # The default is a list of all networks attached to the machine: a # complete class A network (X.0.0.0/8), a complete class B network # (X.X.0.0/16), and so on. If you want stricter control, specify a # list of network/mask patterns, where the mask specifies the number # of bits in the network part of a host address. You can also specify # the absolute pathname of a pattern file instead of listing the # patterns here. # #mynetworks = 168.100.189.0/28, 127.0.0.0/8
上面的行是 Postfix 配置中的另一個關鍵元件。正如說明中所說,它指定了此主機本地的網路列表。對於不熟悉所用語法的使用者,它被稱為無類別域間路由 (CIDR) 或超級網路。對於熟悉網路類別(A、B、C 等)的使用者,它是一種無需參考類別即可劃分 IP 地址的方法。
#mynetworks = $config_directory/mynetworks # SHOW SOFTWARE VERSION OR NOT # # The smtpd_banner parameter specifies the text that follows the 220 # status code in the SMTP greeting banner. Some people like to see # the mail version advertised. By default, Postfix shows no version. # # You MUST specify the $myhostname at the start of the text. When # the SMTP client sees its own hostname at the start of an SMTP # greeting banner it will report a mailer loop. That's better than # having a machine meltdown. # #smtpd_banner = $myhostname ESMTP $mail_name #smtpd_banner = $myhostname ESMTP $mail_name ($mail_version)
上面的配置條目是個人偏好的問題。它不是必需的,管理員可以選擇。
# PARALLEL DELIVERY TO THE SAME DESTINATION # # How many parallel deliveries to the same user or domain? With local # delivery, it does not make sense to do massively parallel delivery # to the same user, because mailbox updates must happen sequentially, # and expensive pipelines in .forward files can cause disasters when # too many are run at the same time. With SMTP deliveries, 10 # simultaneous connections to the same domain could be sufficient to # raise eyebrows. # # Each message delivery transport has its XXX_destination_concurrency_limit # parameter. The default is $default_destination_concurrency_limit. local_destination_concurrency_limit = 2 default_destination_concurrency_limit = 10
正如上面的文字所說,這一部分實際上是關於速率限制的。本質上,它是 Postfix 的油門。除非你有充分的理由更改這些預設值,否則應該保持不變。在執行 Postfix 一段時間後(特別是那些在專業環境中使用它的人),你可能會更好地瞭解如何在你的環境中設定它。
# DEBUGGING CONTROL # # The debug_peer_level parameter specifies the increment in verbose # logging level when an SMTP client or server host name or address # matches a pattern in the debug_peer_list parameter. # debug_peer_level = 2
我們建議使用這裡的預設值,除非有充分的理由進行更改。除錯將在本文件的後面章節中介紹。值得一提的是,除非啟用下一部分,否則這一部分沒有實際意義。
# The debug_peer_list parameter specifies an optional list of domain # or network patterns, /file/name patterns or type:name tables. When # an SMTP client or server host name or address matches a pattern, # increase the verbose logging level by the amount specified in the # debug_peer_level parameter. # # debug_peer_list = 127.0.0.1 # debug_peer_list = some.domain
此部分與 debug_peer_level 結合使用,因此如果未啟用它,則此部分毫無意義。這實際上是 Postfix 的一個非常巧妙的功能。想一分鐘。如果一切正常,但有一個主機似乎在接收或傳送郵件到或從你的主機時遇到問題,那麼你可以使用此功能來提高僅針對該主機的日誌級別。
# The debugger_command specifies the external command that is executed
# when a Postfix daemon program is run with the -D option.
#
# Use "command .. & sleep 5" so that the debugger can attach before
# the process marches on. If you use an X-based debugger, be sure to
# set up your XAUTHORITY environment variable before starting Postfix.
#
debugger_command =
PATH=/usr/bin:/usr/X11R6/bin
xxgdb $daemon_directory/$process_name $process_id & sleep 5
現在保持此部分不變。我們將在本文件的後面部分詳細介紹除錯。就是這樣。我們已經完成了 main.cf 檔案,並且幾乎可以開始啟動它了。
主守護程序是一個監督應用程式,它控制和監視所有其他 Postfix 程序。master.cf 檔案是主守護程序的配置檔案。master.cf 檔案是 Postfix 的節流閥。在這裡,你可以設定所有守護程序程序計數限制。一個有用的限制的例子是設定可以同時執行的 SMTP 程序數量的限制,畢竟,你可能不希望同時接收 50 條入站訊息。這裡要理解的關鍵是,任何沒有明確限制的程序預設限制為 50 個程序。
一般來說,master.cf 檔案的預設值就很好,所以你可以保持不變。
這僅僅是預設的別名檔案,它可以與你在 sendmail 中使用的檔案完全相同(推薦),並且它與使用 newaliases 命令時的工作方式相同。如果你使用 majordomo,你的 majordomo 別名將以與以前相同的方式工作,並且它們也將與 newaliases 命令一起工作。
postfix 伺服器的控制透過 init.d 指令碼完成。更改配置後,不要忘記發出 postfix reload 命令!如果你修改了別名資料庫 (/etc/aliases),不要忘記透過發出 newaliases 命令(如 sendmail 中一樣)啟用更改。
關鍵術語、檔案和實用程式:/etc/aliases /etc/postfix/main.cf /etc/postfix/master.cf /var/spool/postfix
procmail 是由德國的 Stephen van den Berg 編寫的郵件處理實用程式語言。本文為中級 Unix 使用者提供了有關如何使用 procmail 的一些背景資訊。作為一個“小型”語言(使用學術術語),procmail 缺乏許多傳統通用語言的功能和結構。它沒有“while”或“for”迴圈。但是它“瞭解”許多有關 Unix 郵件傳遞約定和檔案/目錄許可權的資訊——尤其是關於檔案鎖的資訊。雖然可以使用大多數 Unix 系統上安裝的工具,用任何程式語言編寫自定義郵件過濾指令碼是可能的——我們將證明 procmail 是系統管理員和高階 Unix 使用者的首選工具。
Unix 郵件系統由 MTA(郵件傳輸代理,如 sendmail、smail、qmail mmdf 等)、MDA(交付代理,如 sendmail、deliver 和 procmail)和 MUA(使用者代理,如 elm、pine、/bin/mail、mh、Eudora 和 Pegasus)組成。
在網際網路上的大多數 Unix 系統上,sendmail 被用作整合傳輸和交付代理。sendmail 和相容的 MTA 具有透過兩種機制中的任何一種,透過自定義過濾器或程式排程郵件的能力:別名和 .forwards。
別名機制使用單個檔案(通常為 /etc/aliases 或 /usr/lib/aliases)來重定向郵件。此檔案由系統管理員擁有和維護。因此你(作為使用者)無法修改它。“.forward”機制是分散的。系統上的每個使用者都可以在其主目錄中建立一個名為 .forward 的檔案,其中包含地址、檔名或程式(過濾器)。通常,該檔案*必須*由使用者或 root 擁有,並且*不能*被其他使用者“寫入”(出於安全原因,sendmail 的良好版本會檢查這些因素)。
使用 sendmail 的某些版本,你還可以透過逗號分隔指定多個地址、程式或檔案。但是,我們將跳過這方面的細節。管理郵件流量
你可以透過 .forward 中包含的任何任意程式轉發你的郵件,該程式包含一行,例如
"|$HOME/bin/your.program -and some arguments"
注意引號和“管道”字元。它們是必需的。“Your.program”可以是 Bourne shell 指令碼、awk 或 perl 指令碼、編譯的 C 程式或你想要編寫的任何其他型別的過濾器。
但是,必須編寫“your.program”來處理有關 sendmail 如何將訊息(標題和正文)傳遞給它的許多細節,如何將返回值返回給 sendmail,如何處理檔案鎖定(如果郵件在“your.program”仍在處理一個郵件時到達,等等)。這就是 procmail 給我們的東西。
到目前為止,我們所看到的是適用於所有與 sendmail 相容的 MTA/MDA 的一般資訊。管理郵件流量
因此,為了確保郵件被傳遞到 procmail 以供處理,第一步是建立 .forward 檔案。(這在配置 procmail 本身之前進行是安全的——假設該軟體包的二進位制檔案已安裝)。以下是 procmail 手冊頁中貼上的規範示例
"|IFS=' '&&exec /usr/local/bin/procmail -f-||exit 75 #YOUR_USERNAME"
如果你這樣做了,並且什麼都沒做,你的郵件基本上不會受到影響。procmail 只會查詢其預設的配方檔案(.procmailrc),如果沒有找到——它會對每條訊息執行其預設操作。換句話說,它會將新訊息追加到你的正常 spool 檔案中。
你可以在 sendmail/postfix 中將 procmail 設定為系統範圍的本地交付代理。完成此操作後,你可以跳過有關使用 .forward 檔案的整個部分——或者你可以照常使用它。例如,在 sendmail 中,可以透過使用以下內容更改 sendmail.mc 來實現
MAILER_DEFINITIONS dnl # MAILER(`local')dnl <- comment this one out with dnl MAILER(`procmail')dnl MAILER(`smtp')dnl
在 postfix 中,可以根據postfix 常見問題解答來完成。基本上,只需編輯 /etc/postfix/main.cf,並使用以下內容並重新載入 postfix 即可。
/etc/postfix/mail.cf: mailbox_command = /path/to/procmail
無論哪種情況,自動處理郵件的下一步是在你的主目錄中建立一個 .procmailrc 檔案。你實際上可以將此檔案命名為任何你想要的名稱——但你需要將該名稱顯式地插入 .forward 檔案中(在“||”運算子之前)。幾乎每個人都使用預設名稱。
到目前為止,我們只討論瞭如何將所有內容路由到 procmail——這主要涉及 sendmail 和 Bourne shell 的語法。幾乎所有 sendmail 都配置為使用 /bin/sh(Bourne shell)來解釋別名和 .forward“管道”。
因此,以下是一個非常簡單的 .procmailrc 檔案
:0c: $HOME/mail.backup
這只是將所有傳入郵件的額外副本追加到你的主目錄中的名為“mail.backup”的檔案中。請注意,為你預設了許多環境變數。有人建議你應該顯式地設定 SHELL=/bin/sh(或你係統上可用的最接近 Bourne Shell 的衍生版本)。我一直沒有為此擔心,因為我在大多數系統上使用的 shell 已經是 Bourne 相容的。
但是,csh 和其他 shell 使用者應該注意,我見過的所有 procmail 食譜示例都使用 Bourne 語法。
:0 行標誌著“食譜”(過程、子句,無論什麼)的開始。:0 後面可以是許多“標誌”中的任何一個。組合這些標誌的方法數不勝數。我們在本示例中使用的唯一標誌是“c”,表示“複製”。
這行中的第二個冒號標誌著標誌的結束以及鎖檔名稱的開始。由於沒有給出名稱,procmail 將自動選擇一個。
這一部分有點複雜。郵件可能會成批到達。如果在您的指令碼仍在處理上一封郵件時收到了一封新郵件,您將擁有多個 sendmail 程序。每個程序都會處理一封郵件。這本身不是問題。但是,如果這兩個程序試圖同時寫入同一個檔案,它們很可能會以不可預測的方式混亂(結果將不是格式正確的郵件資料夾)。
因此,我們提示 procmail 它需要檢查並建立鎖檔案。在這個特定情況下,我們不關心鎖檔案的名稱是什麼(因為我們不會讓其他程式寫入備份檔案)。因此,我們保留最後一個欄位(冒號之後)為空。然後 procmail 將選擇它自己的鎖檔名。
如果我們從配方標題行中刪除 :(完全省略最後一個欄位),則不使用鎖檔案。當我們只打算從配方中的檔案讀取時,或者當我們只打算以非特定順序向檔案寫入短的單行條目時(例如日誌檔案條目),這很合適。procmail 的工作方式是
它從 sendmail(或某個與 sendmail 相容的 MTA/MDA)接收一條郵件。由於新郵件可能比處理速度快,因此可能有多個 procmail 正在執行。它開啟其配方檔案(預設情況下為 .procmailrc 或在其命令列上指定)並從第一個配方到最後一個配方解析每個配方,直到郵件被“傳遞”(或“處置”,具體情況而定)。
任何配方都可以是郵件的“處置”或“傳遞”。一旦郵件被“傳遞”,procmail 就會關閉其檔案,刪除其鎖並退出。
如果 procmail 到了其 rc 檔案的末尾(以及所有 INCLUDE 的檔案),但沒有“處置”郵件,則郵件將被附加到您的 spool 檔案(這對您和所有“郵件使用者代理”來說看起來像是一次正常的傳遞,比如 Eudora、elm 等)。
這解釋了為什麼 procmail 對您沒有 *no* .procmailrc 如此寬容。它只是將您的郵件傳遞到 spool,因為它已經到達了所有配方的末尾(沒有配方)。'c' 標誌使配方在郵件的“副本”上工作,這意味著該配方採取的任何操作都不會被視為郵件的“處置”。
沒有 'c' 標誌,此配方將捕獲所有傳入郵件,並且所有郵件最終都會進入 mail.backup。沒有郵件會進入您的 spool 檔案,並且不會解析其他任何配方。
此示例配方中的下一行只是一個檔名。與 sendmail 的別名和 .forward 檔案一樣,procmail 識別三種類型的郵件處置。您可以將其附加到檔案、將其轉發到其他郵件地址,或將其透過程式過濾。
實際上,procmail 處理了一種特殊形式的“傳遞”或“處置”。如果您提供一個目錄名稱(而不是檔名),它會將郵件作為單獨的檔案新增到該目錄中。該檔案的名稱將基於幾個相當複雜的因素,除非您使用 Rand MH 系統或其他相對模糊和“奇特”的郵件代理,否則您不必擔心這些因素。
一個 procmail配方通常由三部分組成——開始行(:0 帶有一些標誌)、一些條件(以 * 開頭的行 - 星號 - 字元)和一個“傳遞”行,可以是檔案/目錄名稱,也可以是以 ! 開頭的行 - 歎號 - 字元,或者是以 | 開頭的行 - 管道字元。
以下是一個示例
:0 * ^From.*someone.i.dont.like@somewhere.org /dev/null
這是一個簡單的示例,沒有標誌,一個條件和一個簡單的檔案傳遞。它只是丟棄來自“我不喜歡的人”的所有郵件。(Unix 下的 /dev/null 是一個“位桶”——一個無底的井,用於丟棄不需要的輸出。DOS 也有類似的概念,但並不那麼方便)。
以下是一個更復雜的示例
:0 * !^FROM_DAEMON * !^FROM_MAILER * !^X-Loop: myaddress@myhost.mydomain.org | $HOME/bin/my.script
它由一組否定條件組成(請注意,所有條件都以 ! 字元開頭)。這意味著:對於任何沒有來自“daemon”(某些自動化程序)並且沒有來自“mailer”(某些其他自動化程序)並且不包含以下形式的標題行的郵件:“X-Loop: myadd...” 將其透過我的 bin 目錄中的指令碼。
我可以將指令碼直接放在 rc 檔案中(大多數 procmail 使用者大多數時候都這樣做)。這個指令碼可以對郵件做任何事情。在這種情況下,無論它做什麼都必須是好的,因為 procmail 方式將認為所有此類郵件都被傳遞了,而此後的任何配方都將只由來自 DAEMON、MAILER 的郵件以及標題中包含特定 X-Loop: 行的郵件訪問。
這兩個特殊的 FROM_ 條件實際上是“特殊的”。它們由 procmail 預設,實際上指的是一些相當複雜的正則表示式,這些正則表示式專門匹配大多數來自守護程式和郵件程式的郵件頭中發現的東西。
X-Loop: 行是正常的 procmail 條件。在 RFC822 文件(定義了網際網路上電子郵件頭應該是什麼樣子)中,任何以 X- 開頭的行都是“自定義”頭。這意味著任何想要新增電子郵件程式都可以新增幾乎任何它想要的 X- 行。
一個常見的 procmail 習慣用法是,在傳送出去的任何郵件的標題中新增一個 X-Loop: 行,並在傳送任何東西之前檢查我們自己的 X-Loop: 行。這是為了防止“郵件迴圈”——我們的郵件被轉發或“反彈”回我們,我們無休止地回覆它。
因此,以下是如何使用 procmail 自動回覆來自特定人的郵件的詳細示例。我們從配方標題開始。
:0
...然後我們新增一個條件(郵件似乎來自此人)
* ^FROMharasser@spamhome.com
FROM 是 procmail 的一個“神奇”值——它檢查 from、resent-by 和類似的標題行。您也可以使用 ^From:——它將只匹配以字串“From:”開頭的標題行。
^(打嗝或更準確地說“插入符號”)是一個“正則表示式錨點”(一個技術術語,意思是“它指定了模式必須出現在哪裡才能匹配”。有一整本書都是關於正則表示式(O'Reilly & Associates)。“正則表示式”遍佈許多 Unix 實用程式、指令碼語言和其他程式。每個應用程式的“正則表示式”語法略有不同。但是,'grep' 或 'egrep' 的手冊頁是學習更多內容的絕佳場所。
在這種情況下,打嗝表示模式必須出現在行的開頭(這是它在 grep、ed/sed、awk 和其他上下文中通常的含義)。
...然後我們新增幾個條件來避免迴圈並避免回覆自動系統
* !^FROM_DAEMON * !^FROM_MAILER
(這些是幾個更“神奇”的值。手冊頁顯示了分配給這些關鍵字的確切正則表示式——如果您好奇或需要調整與其中一個或兩個類似的特殊條件)。
...再加一個來防止一些棘手的迴圈
* !^X-Loop: myaddress@myhost.mydomain.org
(所有這些模式都以“歎號”(驚歎號)開頭,因為條件是標題中的*沒有*行以這些模式中的任何一個開頭。在這種情況下(以及大多數其他正則表示式上下文中),'歎號'“否定”或“反轉”了模式的含義)。
...現在我們新增一個“處置”——自動回覆。
| (formail -rk \ -A "X-Loop: yourname@youraddress.com" \ -A "Precendence: junk"; \ echo "Please don't send me any more mail";\ echo "This is an automated response";\ echo "I'll never see your message";\ echo "So, GO AWAY" ) | $SENDMAIL -t -oi
這非常複雜,但以下是它的工作原理:| 字元告訴 procmail 它應該啟動一個程式並將其輸入到該程式。左括號是一個 Bourne shell 結構,它以這樣一種方式對一組命令進行分組,將所有命令的輸出合併成一個“流”。
formail 命令是一個隨 procmail 包附帶的便捷程式。它根據其命令列開關及其輸入“格式化”郵件頭。-rk 告訴 formail 格式化“回覆”並“保留”郵件正文。使用這些開關,formail 期待一個標題和一個正文作為輸入。
-A 引數告訴 formail 將下一個引數新增為標題行。提供給 -A 開關的引數必須用引號括起來,以便 shell 將整個字串(包括空格)視為單個引數。每一行末尾的斜槓告訴 procmail 郵件將下一行視為此行的一部分。因此,所有以斜槓結尾的行都作為一行傳遞給 shell。
此“尾隨反斜槓”或“行延續”字元是一個常見的 Unix 習慣用法,在許多程式語言和配置檔案格式中都有發現。分號告訴 shell 執行另一個命令——它們允許在同一個命令列上發出多個命令。
每個 echo 命令都應該相當容易理解。如果我們想使用 'cat' 命令並將我們的文字放入檔案,我們也可以這樣做。我們也可以在這裡呼叫其他程式,例如 'fortune' 或 'date',它們的輸出將與其他內容合併)。
現在我們到了右括號。它標誌著我們組合的命令塊的結束。所有這些命令的輸出都輸入到下一個管道中——它啟動 sendmail 的本地副本(請注意,這是一個 procmail 為我們細心地預設的另一個變數)。
sendmail 上的 -t 開關告訴它從其輸入的標題(formail -r 將其放在那裡)中獲取“To:”地址,而 -oi 開關使 sendmail “選項”能夠“忽略”只包含一個點(不必擔心其中的細節)的行。
理解 procmail 的大多數困難與 procmail 本身無關。正則表示式的複雜性(那些在 * 上的奇怪東西——條件行)以及 shell 引號和命令語法,以及如何格式化一個 sendmail 可以接受的回覆標題(formail 和 sendmail 的東西)是需要如此多解釋的部分。
有關 procmail 的更多資訊,請參閱 Era Eriksson 的“迷你常見問題解答”。在 http://www.iki.fi/~era/procmail/mini-faq.html 或幾個映象之一,例如 http://www.zer0.org/procmail/mini-faq.html http://www.dcs.ed.ac.uk/home/procmail/faq/mini-faq.html
關鍵術語、檔案和實用程式:Procmail .procmailrc
=== 練習 ===.
提供新聞
[edit | edit source]INND 守護程序是最常用的新聞伺服器程式之一,它提供網路新聞傳輸協議 (NNTP) 服務。主要的新聞組包括:alt、comp、gnu、misc、news、rec、sci、soc 和 talk。新聞組以分層方式配置。預設情況下,INND 使用 NNTP 埠 TCP 119。
配置
配置檔案的位置為 /etc/news/。最小的葉節點設定需要修改以下檔案。
inn.conf
設定以下選項。其餘選項的預設值應該沒問題。
organization: MyOrganization domain: mydomain.com server: news.mydomain.com incoming.conf :
將您的 ISP 的新聞伺服器資訊放在這裡。
# Peer definition
# MyISP.com (800) 555-1212 news@MyISP.com
peer myisp.com {
hostname: news.myisp.com
}
如果您想釋出文章,您需要修改新聞源。 news.myisp.com:comp.*,!comp.sources.*,comp.sources.unix/!foo:Tf,Wnm:news.myisp.com 上面的冒號是使用的欄位分隔符。上面這一行的格式是:sitename[/exclude,exclude,...]:pattern,pattern,...[/distrib,distrib,..]:flag,flag,...:param
選項
- sitename
- 命名與該新聞源相關的站點。可以隨意命名,不必是站點的域名。
- pattern
- 指示哪些新聞組要傳送到該站點。預設情況下,傳送所有組(如果需要,留空)。上面的例子將導致所有“comp”組被接收,但不包括“comp.sources”下的任何組,除了“comp.sources.unix”。
- distribution
- 如果指定,並且文章有“Distribution”標題,則會檢查該值。如果指定的分佈與文章中的分佈標題匹配,則會發送。但是,如果指定的分佈以感嘆號開頭,並且文章中的分佈標題匹配,則不會發送。在上面的示例中,任何包含“foo”的分佈標題的文章都不會被髮送。
- flag
- 指定關於新聞源的各種選項。上面的選項指定這是一個檔案新聞源型別 (Tf),並且只應該寫入“message-id”和“token” (Wmn) 文章。
param - 含義根據新聞源型別而變化。當新聞源型別為“file”時,如上面的示例所示,它指定在接收文章時寫入條目的檔案。如果路徑不是絕對路徑,則相對於 inn.conf 中的“pathoutgoing”選項。
readers.conf:如果您想允許其他計算機上的使用者閱讀,請編輯此檔案。motd.news:如果您允許使用者閱讀,建議在這個檔案中放置一個橫幅,將您的使用策略傳達給您的讀者。
執行 inncheck 來更正任何許可權問題並捕獲任何配置檔案錯誤。執行 makehistory 來初始化 INN 歷史資料庫。執行 makedbz 來重建 dbz 資料庫檔案。執行 innd 並使用新聞客戶端進行測試。
innd 無法啟動 使用 inncheck。檢查 /var/log/news 下的日誌。讀者無法閱讀:驗證讀者是否被允許訪問,方法是檢查 nnrp.access。確保 innd 正在執行。檢查 /var/log/news 下的日誌。telnet 到埠 119 並檢視是否出現了橫幅。釋出者無法釋出:確認釋出者被允許釋出,方法是檢查 nnrp.access。檢查 /var/log/news 下的日誌。telnet 到埠 119 並檢視是否出現了包含(允許釋出)的橫幅。
關鍵詞、檔案和工具:Innd