構建Postfix郵件服務器的詳細過程



構建Postfix郵件服務器(一)


電子郵件系統概述


郵件系統角色

在Internet中,電子郵件系統並不是一個孤立的體系,除了需要DNS服務提供郵件域名的解析

可以使用Web服務提供用戶界面以外,郵件系統的內部也是由不同的軟件程序組成的

  • MTA 郵件傳輸代理

  • 負責接受客戶端郵件,並將其傳輸給其他的MTA

    MTA是電子郵件系統的核心部分。例如Sendmail,Postfix和Exchange。

  • MDA 郵件分發代理

  • 負責在服務器中將郵件分發到用戶郵箱。有些MTA集成MDA功能。例如Exchange。

  • MUA 郵件用戶代理

  • 負責提供郵件閱讀,撰寫,發送,接收等管理界面。例如Mutt,Pine等。


郵件系統協議

  • RFC822

  • 電子郵件報文格式,規定了電子郵件由郵件頭和郵件內容兩個部分組成。

  • SMTP

  • 發送和傳輸電子郵件。MUA使用SMTP協議將郵件發送給MTA

    MTA使用SMTP協議將郵件傳輸給其他的MTA。SMTP協議使用端口TCP 25,發信認證部分被稱爲ESMTP

  • POP3

  • 從郵件服務器獲取郵件,然後在離線處理郵件。POP3協議使用端口TCP 110。

  • IMAP4

  • 以聯機的方式處理郵件,可以選擇性下載郵件。IMAP4協議使用端口TCP 143。

  • MIME

  • 現代郵件傳輸格式,讓電子郵件具備傳輸二進制文件的能力。


郵件服務器軟件

  • Exchange

  • 微軟的企業電子郵件方案,多用於企業環境。

  • Notes/Domino

  • 由Lotus公司開發的電子郵件產品,後被IBM收購,多用於政府機構,銀行等大型機構。

  • Sendmail

  • 最早的Unix/Linux電子郵件系統。配置相對複雜,安全性欠佳。

  • Qmail

  • 易於二次開發的Unix/Linux電子郵件系統。企業按照自己的需求,修改Qmail的源代碼實現特定的功能。

  • Postfix

  • Sendmail的替代品。


Postfix基礎

Postfix 採用模塊化設計,由不同的程序實現不同的功能

Postfix的主進程名爲 master ,由主進程根據需求啓動其他程序協同處理郵件。

Postfix可以直接使用本地系統用戶作爲郵件用戶,也可以使用虛擬用戶

虛擬用戶帳號信息通常存儲在MySQL數據庫中。

 wget http://www.postfix.cn/source/official/postfix-2.7.3.tar.gz
 wget http://vda.sourceforge.net/VDA/postfix-vda-2.7.2.patch


編譯安裝Postfix

  • 預處理 

    service sendmail stop
    rpm -qa | grep sendmail | xargs rpm -e --nodeps
  • 創建服務用戶和組 

    groupadd -g 1200 postdrop
    groupadd -g 1000 postfix
    useradd -M -u 1000 -g postfix -G postdrop -s /sbin/nologin postfix
  • 解壓源碼包 

    tar zxvf postfix-2.7.3.tar.gz
    cd postfix-2.7.3
    patch -Np1 < ../postfix-vda-2.7.2.patch
  • 配置源碼包 

    yum install db4-devel
    yum install cyrus-sasl-devel

    rpm -ivh /media/cdrom/Server/db4-devel-4.3.29-9.fc6.i386.rpm
    rpm -ivh /media/cdrom/Server/cyrus-sasl-devel-2.1.22-4.i386.rpm
    make makefiles 'CCARGS=-DHAS_MYSQL -I/usr/local/mysql/include/ -DUSE_CYRUS_SASL -DUSE_SASL_AUTH -I/usr/include/sasl' 'AUXLIBS=-L/usr/local/mysql/lib/ -lmysqlclient -lm -lz -L/usr/lib/sasl2 -lsasl2'
  • 編譯並安裝 

    vim /etc/ld.so.conf
    /usr/local/mysql/lib/
    ldconfig -v
    make && make install


Postfix目錄

  • /etc/postfix/

  • 主配置目錄,包含主配置文件,各類腳本,查詢表等。

    • /etc/postfix/main.cf

    • 主配置文件

    • /etc/postfix/master.cf

    • master程序配置文件

  • /usr/libexec/postfix/

  • 服務器程序目錄

  • /var/spool/postfix/

  • 郵件隊列相關目錄

    • /var/spool/postfix/incoming/

    • 傳入隊列

    • /var/spool/postfix/active/

    • 活動隊列

    • /var/spool/postfix/deferred/

    • 推遲隊列

    • /var/spool/postfix/hold/

    • 約束隊列

    • /var/spool/postfix/corrupt/

    • 錯誤隊列

  • /usr/sbin/

  • 服務管理程序目錄

    • /usr/sbin/postalais

    • 用於構造,修改和查詢別名表

    • /usr/sbin/postconf

    • 用於顯示和編輯主配置文件

    • /usr/sbin/postfix

    • 用於啓動和停止postfix服務

    • /usr/sbin/postmap

    • 用於構造,修改或查詢查詢表

    • /usr/sbin/postqueue

    • 用於一般用戶管理郵件隊列

    • /usr/sbin/postsuper

    • 用於超級用戶管理郵件對列


Postfix配置文件

Postfix系統最主要的配置文件包括: /etc/postfix/main.cf 和 /etc/postfix/master.cf 。

/etc/postfix/main.cf 是Postfix服務的主配置文件。 /etc/postfix/main.cf 是master程序配置文件。

/etc/postfix/main.cf 配置項有300多個,大部分配置都設置了默認值

大多數時候只需要配置少數幾個參數,就可以滿足一般郵件服務器的要求。

Postfix系統提供了 postconf 工具用來輔助配置

不帶任何選項的postconf命令將顯示當前Postfix服務所使用的配置參數, -n 選項則顯示不同於默認配置的參數。

postconf
postconf -n

郵件管理員經常使用postconf命令簡化 /etc/postfix/main.cf 文件

只保留與默認配置不同的參數,提高主配置文件的易讀性。

cd /etc/postfix/
postconf -n > new.cf
cp main.cf main.cf.bak
mv new.cf main.cf
cat main.cf


Postfix日誌文件

Postfix系統的日誌文件位於 /var/log/maillog 文件中,該文件記錄了Postfix服務器運行狀態信息

在安裝調試Postfix郵件系統及日常維護過程中,經常會使用 tail 命令帶 -f 選項實時觀察日誌內容變化。

tail -f /var/log/maillog

如果日誌內容較多,還可以使用 egrep 命令進行過濾

只關注包含 reject warning error fatalpanic 等比較重要的事件記錄。

egrep '(reject|warning|error|fatal|panic):' /var/log/maillog


Postfix啓動控制

Postfix服務的啓動控制主要通過 /usr/sbin/postfix 命令來完成。

postfix (start|stop|check|reload)
postfix start
netstat -antpl | grep :25


基於Postfix構建簡單電子郵件系統

構建基本發信,收信服務器的配置和測試。

http://leiyue.files.wordpress.com/2011/07/wpid-postfix_basic_structure.png

  • IP地址

  • 192.168.1.n

  • 郵件域

  • benet99.com

  • 主機名

  • mail.benet99.com

  • 郵件賬號

  • test

  • POP3/IMAP4服務

  • dovecot

  • LAMP平臺

  • /usr/local/apache2/ /usr/local/mysql/ /usr/local/php5

  • 域名服務器

  • 192.168.1.100


配置並測試Postfix服務器

Postfix主要提供基於SMTP協議的發信服務,其最基本的功能就是存儲和發送郵件。

  • 修改主配置文件 
    Postfix主配置文件內不能使用 單引號 或 雙引號 。

    vim /etc/postfix/main.cf

    內容爲:

    command_directory = /usr/sbin
    config_directory = /etc/postfix
    daemon_directory = /usr/libexec/postfix
    data_directory = /var/lib/postfix
    debug_peer_level = 2
    home_mailbox = Maildir/                 //指定郵件存儲目錄
    html_directory = no
    mail_owner = postfix
    mailq_path = /usr/bin/mailq
    manpage_directory = /usr/local/man
    mydestination = $mydomain, $myhostname  //指定接收郵件域名
    mydomain = benet.com                    //指定郵件域名
    myhostname = mail.benet.com             //指定郵件主機名
    myorigin = $mydomain                    //指定發送郵件域名
    newaliases_path = /usr/bin/newaliases
    queue_directory = /var/spool/postfix
    readme_directory = no
    sample_directory = /etc/postfix
    sendmail_path = /usr/sbin/sendmail
    setgid_group = postdrop
    unknown_local_recipient_reject_code = 550

    用戶的郵箱空間用於保存各自的電子郵件內容。在Postfix服務器中,支持兩種郵箱存儲方式。

    postfix reload
    netstat -antpl | grep :25
    • Mailbox

    • 所有郵件存儲在同一個文件內,默認是 /var/spool/mail/username ,每個郵件之間以特定的標記分割。

    • Maildir/

    • 使用目錄結構來存儲,每一封郵件保存成一個文件,每個文件名稱一般有一定的規律

      例如會包含時間戳、pid及inode節點號等每一封郵件作爲一個獨立的文件保存。

  • 添加郵件用戶賬號 
    使用Linux系統賬號發送郵件。


構建Dovecot服務器

Dovecot是一個安全性較好的POP3/IMAP服務器軟件,響應速度快且擴展性好

Dovecot默認使用Linux系統用戶,通過PAM方式進行身份驗證。

  • 下載軟件 

    wget http://www.dovecot.org/releases/2.0/dovecot-2.0.11.tar.gz
  • 預處理 

    useradd -M -s /sbin/nologin dovecot
    useradd -M -s /sbin/nologin dovenull
  • 解壓源碼包 

    tar zxvf dovecot-2.0.11.tar.gz
    cd dovecot-2.0.11
  • 安裝PAM開發包 

    yum install pam-devel

    rpm -ivh /media/cdrom/Server/pam-devel-0.99.6.2-3.27.el5.i386.rpm
  • 配置編譯安裝 

    ./configure --sysconfdir=/etc --with-mysql --with-pam
    make && make install
  • 修改配置文件 

    vim /etc/dovecot/dovecot.conf

    內容爲:

    ssl = no
    protocols = pop3 imap
    disable_plaintext_auth = no
    mail_location = maildir:~/Maildir
    passdb {
      driver = pam
    }
    userdb {
      driver = passwd
    }
  • 檢查配置文件 

    doveconf -n
  • 編輯PAM文件 

    cp ~/dovecot.pam /etc/pam.d/dovecot

    vim /etc/pam.d/dovecot

    內容爲:

    #%PAM-1.0
    auth       include      system-auth
    account    include      system-auth
    password   include      system-auth
    session    include      system-auth
  • 添加爲系統服務 

    cp ~/dovecot.server /etc/init.d/dovecot
    chmod a+x /etc/init.d/dovecot
    chkconfig --add dovecot
    chkconfig dovecot on
  • 啓動服務 

    service dovecot start
    netstat -antpl | grep dovecot
  • POP3接收郵件測試 

    telnet localhost 110
    user leiyue
    pass pwd@123
    list
    retr 1
    quit


使用Outlook Express郵件客戶端

此處省略xxxx字。


擴展Postfix郵件系統的功能

WEBMAIL郵件界面

Webmail是指提供給郵件用戶發信收信的Web界面。通過訪問郵件系統提供的Web界面,可以和郵件客戶端軟件實現相同的郵件管理功能。使用Webmail簡單,便捷,在Internet郵件服務中得到了廣泛的應用。

Postfix郵件系統支持多種Webmail,例如:OpenWebmail,Extmail,SquirreMail等。


http://leiyue.files.wordpress.com/2011/07/wpid-postfix_webmail_structure.png

  • 下載軟件包 

    wget http://downloads.sourceforge.net/project/squirrelmail/stable/1.4.21/squirrelmail-1.4.21.tar.bz2
    wget http://downloads.sourceforge.net/project/squirrelmail/locales/1.4.18-20090526/zh_CN-1.4.18-20090526.tar.gz
  • 解壓軟件包 

    tar jxvf squirrelmail-1.4.21.tar.bz2 -C /usr/local/apache2/htdocs/
    mv /usr/local/apache2/htdocs/squirrelmail-1.4.21 /usr/local/apache2/htdocs/webmail
    cd /usr/local/apache2/htdocs/webmail
    tar zxvf ~/zh_CN-1.4.18-20090526.tar.gz
  • 調整權限設置 

    mkdir -p attach data
    chown -R daemon:daemon attach/ data/
    chmod 730 attach/
  • 修改配置文件 

    cp config/config_default.php config/config.php
    vim config/config.php

    修改以下內容:

    $domain = 'benet.com';
    $smtpServerAddress = 'localhost';
    $smtpPort = 25;
    ……    
    $imapServerAddress = 'localhost';
    $imapPort = 143;
    $imap_server_type = 'dovecot';
    ……
    $data_dir = '/usr/local/apache2/htdocs/webmail/data/';
    $p_w_upload_dir = '/usr/local/apache2/htdocs/webmail/attach/';
    ……
    $squirrelmail_default_language = 'zh_CN';
    $default_charset = 'zh_CN.UTF-8';


設置SMTP的用戶認證

SMTP發信認證是避免被人當作垃圾發送郵件服務器的必要措施。當用戶通過SMTP協議向外部郵件域發送郵件時

服務器要求用戶提供用戶名和密碼進行身份煙長。只有通過身份驗證的用戶才被允許發送外部郵件,否則將拒絕發信。

在Postfix郵件系統中,可以使用Cyrus SASL軟件來實現基本的SMTP認證機制。


http://leiyue.files.wordpress.com/2011/07/wpid-smtpd_cyrus_sasl.png

rpm -qa | grep cyrus
cyrus-sasl-2.1.22-4
cyrus-sasl-lib-2.1.22-4
cyrus-sasl-devel-2.1.22-4
cyrus-sasl-plain-2.1.22-4
  • 設置Cyrus SASL函數庫 

    vim /usr/lib/sasl2/smtpd.conf

    內容爲:

    pwcheck_method:saslauthd
  • 啓動saslauthd服務 

    service saslauthd start
    chkconfig saslauthd on
  • 修改主配置文件 

    vim /etc/postfix/main.cf

    內容爲:

    command_directory = /usr/sbin
    config_directory = /etc/postfix
    daemon_directory = /usr/libexec/postfix
    data_directory = /var/lib/postfix
    debug_peer_level = 2
    home_mailbox = Maildir/
    html_directory = no
    mail_owner = postfix
    mailq_path = /usr/bin/mailq
    manpage_directory = /usr/local/man
    mydestination = $mydomain, $myhostname
    mydomain = benet.com
    myhostname = mail.benet.com
    myorigin = $mydomain
    newaliases_path = /usr/bin/newaliases
    queue_directory = /var/spool/postfix
    readme_directory = no
    sample_directory = /etc/postfix
    sendmail_path = /usr/sbin/sendmail
    setgid_group = postdrop
    smtpd_recipient_restrictions = permit_sasl_authenticated, reject_unauth_destination //指定smtpd允許和拒絕條件
    smtpd_sasl_auth_enable = yes               //指定smtpd認證方式
    smtpd_sasl_security_options = noanonymous  //拒絕匿名用戶訪問
    unknown_local_recipient_reject_code = 550
  • 測試SMTP發信認證 
    添加了SMTP發信認證就不能作爲發信服務器發送郵件。

    • 使用密文測試SMTP發信認證 

      telnet localhost 25
      Trying 127.0.0.1...
      Connected to localhost.localdomain (127.0.0.1).
      Escape character is '^]'.
      220 mail.benet.com ESMTP Postfix
      helo localhost
      250 mail.benet.com
      mail from: [email protected]
      250 2.1.0 Ok
      rcpt to: [email protected]
      554 5.7.1 <[email protected]>: Relay access denied
      quit
      221 2.0.0 Bye
      Connection closed by foreign host.
      telnet localhost 25
      Trying 127.0.0.1...
      Connected to localhost.localdomain (127.0.0.1).
      Escape character is '^]'.
      220 mail.benet.com ESMTP Postfix
      helo localhost
      250 mail.benet.com      
      auth login
      334 VXNlcm5hbWU6
      bGVpeXVl
      334 UGFzc3dvcmQ6
      cHdkQDEyMw==
      235 2.7.0 Authentication successful
      mail from: [email protected]
      250 2.1.0 Ok
      rcpt to: [email protected]
      250 2.1.5 Ok
      data
      354 End data with <CR><LF>.<CR><LF>
      subject: test mail
      from: [email protected]
      to: [email protected]

      this is a test mail for [email protected]
      .
      250 2.0.0 Ok: queued as 869682F408
      quit
      221 2.0.0 Bye
      Connection closed by foreign host.
    • 獲得用戶名和密碼的加密密文 

      printf "leiyue" | openssl base64

      命令結果爲:

      bGVpeXVl
      printf "pwd@123" | openssl base64

      命令結果爲:

      cHdkQDEyMw==


設置用戶別名與郵件羣組

郵件別名是郵件系統的重要功能,主要來實現郵件的羣發。郵件別名依賴於 aliases 機制,需要用到查詢表。

  • Postfix查詢表 
    Postfix服務器在處理郵件的過程中經常需要做各種轉換和查詢,需要使用查詢表
    /etc/aliases 來完成。

    postconf | grep aliases
    alias_database = hash:/etc/aliases
    alias_maps = hash:/etc/aliases, nis:mail.aliases
  • 設置郵件別名實現羣發 
    修改別名表。

    vim /etc/aliases

    添加以下內容:

    root:    leiyue
    hr:      leiyue

    使用 newaliases 命令生成 /etc/aliases.db 數據庫文件。Postfix系統將識別並使用/etc/aliases.db 文件中的設置。

    newaliases


設置郵件大小,郵箱空間限制

  • 限制用戶發送郵件大小 
    Postfix系統默認支持10MB大小的郵件,需要注意的是這裏的大小是經過Base64函數編碼後的郵件大小。

    postconf | grep message_size_limit
    message_size_limit = 10240000

    修改發送郵件的默認大小爲20MB。

    vim /etc/postfix/main.cf

    添加以下內容:

    message_size_limit = 20480000
    postfix reload
  • 限制用戶郵箱空間大小 
    在Postfix郵件系統中,根據郵件用戶的類型,郵件存儲的方式不同可採取不同的方法來限制用戶郵箱空間大小

    • 使用 quota 磁盤配額 
      針對使用系統用戶的情況,可直接使用Linux的磁盤配額功能對用Mailbox和Maildir存儲方式的用戶都生效。

    • 使用 virtualmailboxlimit 配置項 
      主要針對使用虛擬用戶的情況,默認限制50MB

      安裝VDA補丁後,該參數對使用Mailbox或Maildir存儲方式的用戶都生效。

      postconf | grep virtual_mailbox_limit
      virtual_mailbox_limit = 51200000
      virtual_mailbox_limit_inbox = no
      virtual_mailbox_limit_maps = 
      virtual_mailbox_limit_override = no
    • 使用 mailboxsizelimit 配置項 
      主要針對使用系統用戶的情況,默認限制是50MB。該參數對使用Mailbox郵件存儲方式的用戶生效。

      postconf | grep mailbox_size_limit
      mailbox_size_limit = 51200000


構建Postfix郵件服務器(二)


配置Postfix支持虛擬用戶

當郵件用戶達到一定數量時,郵件系統將在用戶安全,系統管理以及數據維護等方面遇到巨大的挑戰

爲了提供更好的性能,效率和安全性,使用虛擬郵件用戶將是一個更好的選擇。


http://leiyue.files.wordpress.com/2011/07/wpid-postfix_virtual_user.png


建立虛擬用戶數據庫

ExtMail是由國內開源組織使用Perl語言開發的一套功能強大的中文Webmail平臺

主要包括ExtMail和Extman兩個部分的程序套件。

  • ExtMail

  • 提供給郵件用戶使用郵件系統的Web界面

  • Extman

  • 提供給郵件系統管理員管理郵件系統的Web界面

http://leiyue.files.wordpress.com/2011/07/wpid-extmail.png


訪問站點 http://www.extmail.org/cgi-bin/download.cgi 下載軟件包ExtMail和Extman最新版。

用 Extman 虛擬郵件管理系統在MySQL數據庫中建立初始化數據庫和表,用於保存虛擬郵件域,虛擬郵件用戶相關信息

service mysqld restart
tar zxvf extman-1.1.tar.gz 
cd extman-1.1/docs
mysql -u root -p < extmail.sql

由於數據庫版本不同,導入 extmail.sql 數據庫時報錯:

ERROR 1064 (42000) at line 50: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TYPE=MyISAM COMMENT='Ext/Webman - Admin Accounts'' at line 15

用文本編輯器打開extmail.sql,將數據庫文件內的所有 TYPE=MyISAM 替換爲 ENGINE=MyISAM ,保存退出。

丟棄導入到一半出錯的數據庫 extmail 。

mysql -u root -p
mysql> drop database extmail;

重新導入數據庫。

mysql -u root -p < extmail.sql
mysql -u root -p < init.sql

腳本 extmail.sql 完成的主要數據庫操作:

  1. 創建名爲 extmail 的數據庫,初始化一些表。

  2. 創建數據庫用戶 extmail@localhost ,密碼爲 extmail ,對數據庫extmail的表有select,update權限。

  3. 創建數據庫用戶 webman@localhost ,密碼爲 extman ,對數據庫extmail表有select,insert,delete,update權限。

腳本 init.sql 完成的主要數據庫操作:

  1. 初始化數據庫 extmail

  2. 建立名爲 extmail.org 的虛擬郵件域

  3. 建立數據庫用戶 [email protected] ,密碼爲 extman*123* ,作爲郵件域的管理賬號。

  4. 建立數據庫用戶 [email protected] ,密碼爲 extmail ,作爲基本郵件用戶。


修改Postfix配置

使用虛擬郵件域時,其中郵件域名,郵件用戶名,密碼及郵件別名等信息都存儲在MySQL數據庫中。當Postfix接收到一封新郵件時,首先檢查是不是發往本地域的郵件,如果不是則轉向數據庫查詢是不是發往虛擬域的郵件,確認收件人是否存在、用戶郵箱路徑等信息。


瞭解當前的Postfix系統是否支持MySQL數據庫。

postconf -m | grep mysql

修改Postfix的主配置文件,添加虛擬郵件用戶配置項。

vim /etc/postfix/main.cf

修改以下內容:

……
# mydestination = $mydomain, $myhostname
……
virtual_mailbox_base = /var/mailbox
virtual_alias_maps = mysql:/etc/postfix/mysql_virtual_alias_maps.cf
virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf
virtual_mailbox_domains = mysql:/etc/postfix/mysql_virtual_domains_maps.cf
virtual_uid_maps = static:1000
virtual_gid_maps = static:1000

各配置項具體作用如下:

  • virtual_mailbox_base

  • 指定虛擬郵箱存儲目錄

  • virtual_alias_maps

  • 指定虛擬郵件用戶對應別名

  • virtual_mailbox_maps

  • 指定記錄虛擬郵箱和存儲位置的對應關係

  • virtual_mailbox_domains

  • 設置Postfix服務器可以接收的虛擬郵件域域名

  • virtual_uid_maps

  • 設置所有虛擬郵件用戶映射用戶的UID號

  • virtual_gid_maps

  • 設置所有虛擬郵件用戶映射組的GID號

將extman提供的虛擬用戶映射表文件複製到Postfix的配置目錄中。

cp *.cf /etc/postfix/


爲虛擬用戶設置SMTP發信認證

由於虛擬郵件用戶的賬號信息保存在MySQL數據庫中,僅僅使用Cyrus SASL已經無法完成SMTP發信認證

這時需要結合Courier-authlib的 authdaemond 服務才能提供基於MySQL數據庫虛擬用戶的SMTP發信認證


http://leiyue.files.wordpress.com/2011/07/wpid-authdaemond.png

  • 下載源碼包 

    wget http://downloads.sourceforge.net/project/courier/authlib/0.63.0/courier-authlib-0.63.0.tar.bz2
  • 解壓源碼包 

    tar jxvf courier-authlib-0.63.0.tar.bz2
    cd courier-authlib-0.63.0
  • 安裝expat軟件包 

    yum install expect

    rpm -ivh /media/cdrom/Server/expat-1.95.8-8.2.1.i386.rpm
  • 卸載MySQL的rpm包 

    rpm -e mysql
  • 配置軟件包 

    ./configure --prefix=/usr/local/courier-authlib --sysconfdir=/etc --without-stdheaderdir --with-authmysql --with-redhat --with-mysql-libs=/usr/local/mysql/lib --with-mysql-includes=/usr/local/mysql/include
  • 編譯安裝 

    make && make install
  • 生成配置文件 

    make install-configure
  • 修改SMTP發信認證配置 

    • 修改SMTP發信認證配置 

      vim /usr/lib/sasl2/smtpd.conf

      內容爲:

      pwcheck_method:authdaemond
      authdaemond_path:/usr/local/courier-authlib/var/spool/authdaemon/socket
    • 啓動authdaemond服務 

      service authdaemond start
      chkconfig --add authdaemond
      chkconfig authdaemond on
    • 添加authdaemond爲系統服務 

      cp ~/courier-authlib-0.63.0/courier-authlib.sysvinit /etc/init.d/authdaemond
      chmod a+x /etc/init.d/authdaemond
    • 設置authdaemond數據庫配置 

      cp authmysqlrc authmysqlrc.bak
      grep -v "^#" authmysqlrc | grep -v "^$" > newauthmysqlrc
      mv newauthmysqlrc authmysqlrc
      vim authmysqlrc

      內容爲:

      MYSQL_SERVER        localhost
      MYSQL_USERNAME      extmail
      MYSQL_PASSWORD      extmail
      MYSQL_SOCKET        /tmp/mysql.sock
      MYSQL_PORT          0
      MYSQL_OPT           0
      MYSQL_DATABASE      extmail
      MYSQL_USER_TABLE    mailbox
      MYSQL_CRYPT_PWFIELD password
      MYSQL_UID_FIELD     uidnumber
      MYSQL_GID_FIELD     gidnumber
      MYSQL_LOGIN_FIELD   username
      MYSQL_HOME_FIELD    concat('/var/mailbox/', homedir)
      MYSQL_NAME_FIELD    name
      MYSQL_MAILDIR_FIELD concat('/var/mailbox/', maildir)
    • 設置authdaemon服務目錄權限 

      chmod 755 /usr/local/courier-authlib/var/spool/authdaemon
    • 修改authdaemond主配置文件 

      cd /etc/authlib/
      cp authdaemonrc authdaemonrc.bak
      grep -v "^#" authdaemonrc | grep -v "^$" > newauthdaemonrc
      mv newauthdaemonrc authdaemonrc
      vim authdaemonrc

      內容爲:

      authmodulelist="authmysql"
      authmodulelistorig="authmysql"
      daemons=5
      authdaemonvar=/usr/local/courier-authlib/var/spool/authdaemon
      DEBUG_LOGIN=0
      DEFAULTOPTIONS=""
      LOGGEROPTS=""
    • 驗證庫文件路徑 

      ldconfig -v | grep -A 9 courier-authlib

      命令結果爲:

      /usr/local/courier-authlib/lib/courier-authlib:
          libcourierauthsasl.so -> libcourierauthsasl.so.0
          libauthuserdb.so -> libauthuserdb.so.0
          libcourierauth.so -> libcourierauth.so.0
          libauthcustom.so -> libauthcustom.so.0
          libauthmysql.so -> libauthmysql.so.0
          libauthpipe.so -> libauthpipe.so.0
          libcourierauthcommon.so -> libcourierauthcommon.so.0
          libauthpam.so -> libauthpam.so.0
          libcourierauthsaslclient.so -> libcourierauthsaslclient.so.0
    • 添加庫文件路徑 

      echo "/usr/local/courier-authlib/lib/courier-authlib" >> /etc/ld.so.conf


修改Dovecot的配置

  • 啓用Dovecot的數據庫認證 

    vim /etc/dovecot/dovecot.conf

    內容爲:

    ssl = no
    protocols = pop3 imap
    disable_plaintext_auth = no
    mail_location = maildir:~/Maildir
    passdb {
      args = /etc/dovecot/dovecot-mysql.conf.ext
      driver = sql
    }
    userdb {
      args = /etc/dovecot/dovecot-mysql.conf.ext
      driver = sql
    }
  • 建立數據庫查詢配置文件 

    vim /etc/dovecot/dovecot-mysql.conf.ext

    內容爲:

    driver = mysql
    connect = host=localhost dbname=extmail user=extmail password=extmail
    default_pass_scheme = CPYPT
    password_query = select username as user, password from mailbox where username = '%u'
    user_query = select maildir, uidnumber as uid, gidnumber as gid from mailbox where username = '%u'
  • 調整默認郵件存儲目錄權限 

    mkdir -p /var/mailbox/extmail.org/postmaster/Maildir/
    chown -R postfix:postfix /var/mailbox


測試虛擬用戶

  • 測試authdaemond服務 

    /usr/local/courier-authlib/sbin/authtest -s login [email protected] extmail

    命令結果爲:

    Authentication succeeded.

         Authenticated: [email protected]  (uid 1000, gid 1000)
        Home Directory: /var/mailbox/extmail.org/postmaster
               Maildir: /var/mailbox/extmail.org/postmaster/Maildir/
                 Quota: (none)
    Encrypted Password: $1$phz1mRrj$3ok6BjeaoJYWDBsEPZb5C0
    Cleartext Password: extmail
               Options: (none)
  • 測試虛擬用戶的發信認證 

    printf "[email protected]" | openssl base64

    命令結果爲:

    cG9zdG1hc3RlckBleHRtYWlsLm9yZw==
    printf "extmail" | openssl base64

    命令結果爲:

    ZXh0bWFpbA==
    telnet localhost 25
    Trying 127.0.0.1...
    Connected to localhost.localdomain (127.0.0.1).
    Escape character is '^]'.
    220 mail.benet.com ESMTP Postfix
    auth login
    334 VXNlcm5hbWU6
    cG9zdG1hc3RlckBleHRtYWlsLm9yZw==
    334 UGFzc3dvcmQ6
    ZXh0bWFpbA==
    235 2.7.0 Authentication successful


ExMail郵件使用及管理平臺

部署ExtMail郵件使用界面

  • 安裝Perl支持包 

    cpan
    cpan> install Unix::Syslog
    cpan> install DBI
    cpan> install DBD::mysql
  • 安裝並配置ExtMail套件 

    • 登錄並使用ExtMail郵件系統 
      訪問 
      http://mail.benet.com 。

    • 重啓httpd服務 

      /usr/local/apache2/bin/apachectl restart
    • 編譯安裝httpd服務的 mod_suexec 模塊 

      cd /usr/src/httpd-2.2.17/
      ./configure --enable-suexec -with-suexec-caller=daemon
      make suexec
      make install-suexec
      cd modules/generators/
      /usr/local/apache2/bin/apxs -c mod_suexec.c
      /usr/local/apache2/bin/apxs -i -a -n suexec ./.libs/mod_suexec.so
    • 調整httpd服務配置 

      vim /usr/local/apache2/conf/httpd.conf

      修改以下內容:

      ……
      LoadModule suexec_module    modules/mod_suexec.so

      NameVirtualHost     192.168.1.103
      <VirtualHost 192.168.1.103>
          ServerName  mail.benet.com
          DocumentRoot    /usr/local/apache2/htdocs/extmail/html
          ScriptAlias /extmail/cgi /usr/local/apache2/htdocs/extmail/cgi
          Alias       /extmail /usr/local/apache2/htdocs/extmail/html
          SuexecUserGroup postfix postfix
      </VirtualHost>
    • 建立ExtMail配置文件 

      cp webmail.cf.default webmail.cf
      vim /usr/local/apache2/htdocs/extmail/webmail.cf

      修改以下內容:

      SYS_CONFIG = /usr/local/apache2/htdocs/extmail/
      SYS_LANGDIR = /usr/local/apache2/htdocs/extmail/lang
      SYS_TEMPLDIR = /usr/local/apache2/htdocs/extmail/html
      ……
      SYS_MAILDIR_BASE = /var/mailbox
      SYS_AUTH_SCHEMA = virtual
      SYS_CRYPT_TYPE = md5crypt
      SYS_MYSQL_USER = extmail
      SYS_MYSQL_PASS = extmail
      SYS_MYSQL_DB = extmail
      SYS_MYSQL_HOST = localhost
      SYS_MYSQL_SOCKET = /tmp/mysql.sock
      ……
      SYS_AUTHLIB_SOCKET = /usr/local/courier-authlib/var/spool/authdaemon/socket
      ……
    • 調整cgi目錄權限 

      chown -R postfix:postfix cgi
    • 解壓源碼包 

      tar zxvf extmail-1.2.tar.gz -C /usr/local/apache2/htdocs/
      mv /usr/local/apache2/htdocs/extmail-1.2 /usr/local/apache2/htdocs/extmail
      cd /usr/local/apache2/htdocs/extmail


部署ExtMail Web管理界面

部署好ExtMail程序套件後,電子郵件用戶可以通過Web界面登錄和使用郵件服務

但郵件域名爲extmail.org,還需要安裝Extman程序套件,通過Web界面對ExtMail郵件系統進行管理


  • 安裝gd-devel軟件包 

    • 使用YUM安裝gd-devel軟件包 

      yum install gd-devel -y
    • 配置YUM支持DVD源 

      cp ~/cdrom.repo /etc/yum.repos.d/
      yum update
  • 安裝Perl支持包 
    如果不安裝 gd-devel 軟件包,安裝Perl支持包的過程中會出現找不到 gdlib-config 命令的錯誤。

    cpan
    cpan> install GD
    cpan> install File::Tail
    cpan> exit
  • 安裝rrdtool繪圖引擎 

    wget ftp://ftp.pbone.net/mirror/ftp.pramberger.at/systems/linux/contrib/rhel5/i386/rrdtool-1.4.5-1.el5.pp.i386.rpm
    wget ftp://ftp.pbone.net/mirror/ftp.pramberger.at/systems/linux/contrib/rhel5/i386/perl-rrdtool-1.4.5-1.el5.pp.i386.rpm
    yum install libdbi
    rpm -ivh rrdtool-1.4.5-1.el5.pp.i386.rpm
    rpm -ivh perl-rrdtool-1.4.5-1.el5.pp.i386.rpm
  • 安裝並配置Extman套件 

    • 調整臨時會話目錄的權限 

      chown -R postfix:postfix /tmp/extman
    • 創建臨時會話目錄 

      mkdir /tmp/extman
    • 修改配置文件 

      cp webman.cf.default webman.cf
      vim webman.cf

      修改以下內容:

      SYS_CONFIG = /usr/local/apache2/htdocs/extman/
      SYS_LANGDIR = /usr/local/apache2/htdocs/extman/lang
      SYS_TEMPLDIR = /usr/local/apache2/htdocs/extman/html
      SYS_MAILDIR_BASE = /var/mailbox
      ……
      SYS_SESS_DIR = /tmp/extman/
      ……
      SYS_BACKEND_TYPE = mysql
      SYS_CRYPT_TYPE = md5crypt
      SYS_MYSQL_USER = webman
      SYS_MYSQL_PASS = webman
      SYS_MYSQL_DB = extmail
      SYS_MYSQL_HOST = localhost
      SYS_MYSQL_SOCKET = /tmp/mysql.sock
      ……
    • 調整cgi目錄權限 

      chown -R postfix:postfix cgi
    • 解壓源碼包 

      tar zxvf extman-1.1.tar.gz -C /usr/local/apache2/htdocs/
      mv /usr/local/apache2/htdocs/extman-1.1 /usr/local/apache2/htdocs/extman
      cd /usr/local/apache2/htdocs/extman
  • 調整httpd服務配置 

    vim /usr/local/apache2/conf/httpd.conf

    修改以下內容:

    ……
    LoadModule suexec_module        modules/mod_suexec.so

    NameVirtualHost         192.168.1.103
    <VirtualHost 192.168.1.103>
            ServerName      mail.benet.com
            DocumentRoot    /usr/local/apache2/htdocs/extmail/html
            ScriptAlias     /extmail/cgi /usr/local/apache2/htdocs/extmail/cgi
            Alias           /extmail /usr/local/apache2/htdocs/extmail/html
            ScriptAlias     /extman/cgi /usr/local/apache2/htdocs/extman/cgi
            Alias           /extman /usr/local/apache2/htdocs/extman/html
            SuexecUserGroup postfix postfix
    </VirtualHost>
  • 重啓httpd服務 

    /usr/local/apache2/bin/apachectl restart

    service httpd restart
  • 登錄並管理ExtMail郵件系統 
    訪問 
    http://mail.benet.com/extman/

    • 設置郵件別名或羣組 

    • 添加郵件域的管理用戶 

    • 添加虛擬郵件用戶,設置用戶密碼,可使用的郵件服務等 

    • 添加虛擬郵件域,設置虛擬域用戶容量,郵箱大小等 

  • 查看郵件系統的圖形化信息 

    cp -r /usr/local/apache2/htdocs/extman/addon/mailgraph_ext /usr/local/
    cd /usr/local/mailgraph_ext/
    ./mailgraph-init start
    echo "/usr/local/mailgraph_ext/mailgraph-init start" >> /etc/rc.d/rc.local


郵件郵件過濾和防病毒

對於運行在Internet環境中的企業電子郵件系統來說,如何有效的防範垃圾郵件和病毒郵件是一個必須解決的問題。


基於郵件地址的過濾

在SMTP會話過程中,客戶端程序在每個階段分別向服務器提供不同的地址信息

Postfix系統可以針對這些信息進行檢查,並通過主配置文件的配置項決定如何處理郵件。


檢查配置參數和檢查對象如下:

  • smtpd_client_restrictions

  • 檢查客戶端的主機名/地址

  • smtpd_helo_restrictions

  • 檢查客戶端HELO信息中的主機名

  • smtpd_sender_restrictions

  • 檢查發件人地址

  • smtpd_recipient_restrictions

  • 檢查收件人地址

http://leiyue.files.wordpress.com/2011/07/wpid-smtpd_restrictions.png

  • 根據客戶端主機名/地址過濾 
    通過設置限制條件 
    check_client_access 檢查郵件客戶端是否滿足訪問策略要求。

    vim /etc/postfix/main.cf
    smtpd_client_restrictions = check_client_access hash:/etc/postfix/access

    建立 /etc/postfix/access 文本文件,根據需要設置訪問策略

    客戶端地址可以使用ip地址,網絡地址,主機名或域名等形式。常見的過濾工作有:

    vim /etc/postfix/access

    添加以下內容:

    127.0.0.1     OK
    192.168.1     OK
    192.168.1.100 REJECT

    設置完畢後,使用 postmap 命令將 /etc/postfix/access 文件轉換爲hash數據庫/etc/postfix/access.db 。

    postmap /etc/postfix/access
    • REJECT

    • 拒絕

    • OK

    • 允許

    • DISCARD

    • 丟棄

  • 根據客戶端HELO主機名過濾 

    vim /etc/postfix/main.cf

    添加以下內容:

    smtpd_helo_required = yes
    smtpd_helo_restrictions = reject_invalid_hostname
  • 根據發件人地址過濾 

    vim /etc/postfix/main.cf

    添加以下內容:

    smtpd_sender_login_maps = mysql:/etc/postfix/mysql_virtual_sender_maps.cf, mysql:/etc/postfix/mysql_virtual_alias_maps.cf
    smtpd_sender_restrictions = reject_sender_login_mismatch, reject_non_fqdn_sender, reject_unknown_sender_domain, check_sender_access hash:/etc/postfix/sender_access
    vim /etc/postfix/sender_access

    添加以下內容:

    [email protected] REJECT
    cracker@         REJECT
    163.com          REJECT
    portmap /etc/postfix/sender_accessF
  • 根據收件人地址過濾 

    vim /etc/postfix/main.cf

    添加以下內容:

    smtpd_recipient_restrictions = permit_sasl_authenticated, reject_unauth_destination, reject_non_fqdn_recipient, reject_unknown_recipient_domain


基於郵件內容的過濾

在Postfix郵件系統中,針對郵件內容的過濾及反病毒郵件功能

可以由 MailScanner 調度SpamAssassin 和 F-Prot 軟件來實現

  • MailScanner

  • 協調調度郵件過濾軟件俺

  • SpamAssassin

  • 根據郵件內容過濾垃圾郵件

  • F-Prot

  • 提供病毒郵件查殺功能

http://leiyue.files.wordpress.com/2011/07/wpid-mailscanner.png

其主要工作流程:

  • 由Postfix將接收的郵件全部放到 hold 隊列中

  • MailScanner對 hold 隊列的郵件進行處理

    • 調用SpamAssassin對郵件內容進行檢查

    • 調用F-Prot對郵件內容進行掃描

  • 檢查完畢的郵件轉入 incoming 隊列中交給Postfix投遞

  • 安裝並配置MailScanner 

    • 啓動MailScanner服務 

      service MailScanner start
      chkconfig MailScanner on
    • 調整郵件過濾隊列的目錄的屬主,屬組 

      chown -R postfix:postfix /var/spool/MailScanner/incoming
      chown -R postfix:postfix /var/spool/MailScanner/quarantine
    • 配置Postfix支持Mailscanner 

      vim /etc/postfix/main.cf

      添加以下內容:

      header_checks = regexp:/etc/postfix/header_checks
      vim /etc/postfix/header_checks

      添加以下內容:

      /^Received:/    HOLD
    • 配置軟件包 

      vim /etc/MailScanner/MailScanner.conf

      修改以下內容:

      ……
      Run As User = postfix
      Run As Group = postfix
      Queue Scan Interval = 6
      Incoming Queue Dir = /var/spool/postfix/hold
      Outgoing Queue Dir = /var/spool/postfix/incoming
      Incoming Work Dir = /var/spool/MailScanner/incoming
      Quarantine Dir = /var/spool/MailScanner/quarantine
      PID file = /var/run/MailScanner.pid
      Restart Every = 7200
      MTA = postfix
      Sendmail = /usr/sbin/sendmail
      Sendmail2 = /usr/sbin/sendmail
      ……
      Use SpamAssassin = yes
      Max SpamAssassin Size = 200k
      Required SpamAssassin Score = 6
      High SpamAssassin Score = 10
      SpamAssassin Auto Whitelist = yes
      SpamAssassin Timeout = 75
      Max SpamAssassin Timeouts = 10
      SpamAssassin Timeouts History = 30
      Check SpamAssassin If On Spam List = yes
      Include Binary Attachments In SpamAssassin = no
      Spam Score = yes
      Cache SpamAssassin Results = yes
      SpamAssassin Cache Database File = /var/spool/MailScanner/incoming/SpamAssassin.cache.db
      Rebuild Bayes Every = 0
      Wait During Bayes Rebuild = no
      Use Custom Spam Scanner = no
      Max Custom Spam Scanner Size = 20k
      Custom Spam Scanner Timeout = 20
      Max Custom Spam Scanner Timeouts = 10
      Custom Spam Scanner Timeout History = 20
      Spam Actions = deliver header "X-Spam-Status: Yes"
      High Scoring Spam Actions = delete forward [email protected]
      Non Spam Actions = deliver header "X-Spam-Status: No"
      ……
      Debug = no
      Debug SpamAssassin = no
      ……
      include /etc/MailScanner/conf.d/*
    • 安裝軟件包 

      tar zxvf MailScanner-4.83.4-1.rpm.tar.gz
      cd MailScanner-4.83.4-1
      yum install rpm-build
      ./install.sh nodeps
      The important ones are HTML-Parser and MIME-tools.
    • 下載軟件包 

      wget http://www.mailscanner.info/files/4/rpm/MailScanner-4.83.4-1.rpm.tar.gz
  • 安裝並配置SpamAssassin 

    yum install spamassassin
    service spamassassin start
    chkconfig spamassassin on
  • 安裝並配置F-Prot 

    wget http://files.f-prot.com/files/unix-trial/fp-Linux-i686-ws.tar.gz
    tar zxvf fp-Linux-i686-ws.tar.gz -C /opt/
    cd /opt/f-prot/
    ./install-f-prot.pl
    /opt/f-prot/fpupdate
    crontab -e

    添加以下內容:

    38 * * * * root /opt/f-prot/fpupdate > /dev/null
    crontab -l
    service crond start
    chkconfig crond on


郵件內容過濾與防病毒測試

  • 測試郵件內容過濾 
    發送測試郵件的文本內容如下:

    XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X
    tail /var/log/maillog
  • 測試反病毒郵件 
    發送測試郵件的文本內容如下:

    X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
    tail /var/log/maillog


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章