閱讀152 返回首頁    go 阿裏雲 go 技術社區[雲棲]


OpenSSL 在 Apache 和 Dovecot 下的使用(二)

Postfix 配置

你必須編輯 /etc/postfix/main.cf 以及 /etc/postfix/master.cf。實例的 main.cf 是完整的配置,基於我們先前的教程。替換成你自己的 OpenSSL 密鑰和證書名以及本地網絡地址。


  1. compatibility_level=2
  2. smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu/GNU)
  3. biff = no
  4. append_dot_mydomain = no
  5. myhostname = localhost
  6. alias_maps = hash:/etc/aliases
  7. alias_database = hash:/etc/aliases
  8. myorigin = $myhostname
  9. mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 192.168.0.0/24
  10. mailbox_size_limit = 0
  11. recipient_delimiter = +
  12. inet_interfaces = all
  13. virtual_mailbox_domains = /etc/postfix/vhosts.txt
  14. virtual_mailbox_base = /home/vmail
  15. virtual_mailbox_maps = hash:/etc/postfix/vmaps.txt
  16. virtual_minimum_uid = 1000
  17. virtual_uid_maps = static:5000
  18. virtual_gid_maps = static:5000
  19. virtual_transport = lmtp:unix:private/dovecot-lmtp
  20. smtpd_tls_cert_file=/etc/ssl/certs/test-com.pem
  21. smtpd_tls_key_file=/etc/ssl/private/test-com.key
  22. smtpd_use_tls=yes
  23. smtpd_sasl_auth_enable = yes
  24. smtpd_sasl_type = dovecot
  25. smtpd_sasl_path = private/auth
  26. smtpd_sasl_authenticated_header = yes

在 master.cf 取消 submission inet 部分的注釋,並編輯 smtpd_recipient_restrictions


  1. #submission inet n - y - - smtpd
  2. -o syslog_name=postfix/submission
  3. -o smtpd_tls_security_level=encrypt
  4. -o smtpd_sasl_auth_enable=yes
  5. -o milter_macro_daemon_name=ORIGINATING
  6. -o smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject
  7. -o smtpd_tls_wrappermode=no

完成後重新加載 Postfix:


  1. $ sudo service postfix reload

Dovecot 配置

在我們以前的教程中,我們為 Dovecot 創建了一個單一配置文件 /etc/dovecot/dovecot.conf,而不是使用多個默認配置文件。這是一個基於我們以前的教程的完整配置。再說一次,使用你自己的 OpenSSL 密鑰和證書,以及你自己的 userdb 的 home 文件:


  1. protocols = imap pop3 lmtp
  2. log_path = /var/log/dovecot.log
  3. info_log_path = /var/log/dovecot-info.log
  4. disable_plaintext_auth = no
  5. mail_location = maildir:~/.Mail
  6. pop3_uidl_format = %g
  7. auth_mechanisms = plain
  8. passdb {
  9. driver = passwd-file
  10. args = /etc/dovecot/passwd
  11. }
  12. userdb {
  13. driver = static
  14. args = uid=vmail gid=vmail home=/home/vmail/studio/%u
  15. }
  16. service lmtp {
  17. unix_listener /var/spool/postfix/private/dovecot-lmtp {
  18. group = postfix
  19. mode = 0600
  20. user = postfix
  21. }
  22. }
  23. protocol lmtp {
  24. postmaster_address = postmaster@studio
  25. }
  26. service lmtp {
  27. user = vmail
  28. }
  29. service auth {
  30. unix_listener /var/spool/postfix/private/auth {
  31. mode = 0660
  32. user=postfix
  33. group=postfix
  34. }
  35. }
  36. ssl=required
  37. ssl_cert = </etc/ssl/certs/test-com.pem
  38. ssl_key = </etc/ssl/private/test-com.key

重啟 Dovecot:


  1. $ sudo service postfix reload

用 telnet 測試

就像我們以前一樣,現在我們可以通過使用 telnet 發送消息來測試我們的設置。 但是等等,你說 telnet 不支持 TLS/SSL,那麼這樣怎麼辦呢?首先通過使用 openssl s_client 打開一個加密會話。openssl s_client 的輸出將顯示你的證書及其指紋和大量其它信息,以便你知道你的服務器正在使用正確的證書。會話建立後輸入的命令都是不以數字開頭的:


  1. $ openssl s_client -starttls smtp -connect studio:25
  2. CONNECTED(00000003)
  3. [masses of output snipped]
  4. Verify return code: 0 (ok)
  5. ---
  6. 250 SMTPUTF8
  7. EHLO studio
  8. 250-localhost
  9. 250-PIPELINING
  10. 250-SIZE 10240000
  11. 250-VRFY
  12. 250-ETRN
  13. 250-AUTH PLAIN
  14. 250-ENHANCEDSTATUSCODES
  15. 250-8BITMIME
  16. 250-DSN
  17. 250 SMTPUTF8
  18. mail from: <carla@domain.com>
  19. 250 2.1.0 Ok
  20. rcpt to: <alrac@studio>
  21. 250 2.1.5 Ok
  22. data
  23. 354 End data with .subject: TLS/SSL test
  24. Hello, we are testing TLS/SSL. Looking good so far.
  25. .
  26. 250 2.0.0 Ok: queued as B9B529FE59
  27. quit
  28. 221 2.0.0 Bye

你應該可以在郵件客戶端中看到一條新郵件,並在打開時要求你驗證 SSL 證書。你也可以使用 openssl s_client 來測試 Dovecot 的 POP3 和 IMAP 服務。此示例測試加密的 POP3,第 5 號消息是我們在 telnet(如上)中創建的:


  1. $ openssl s_client -connect studio:995
  2. CONNECTED(00000003)
  3. [masses of output snipped]
  4. Verify return code: 0 (ok)
  5. ---
  6. +OK Dovecot ready
  7. user alrac@studio
  8. +OK
  9. pass password
  10. +OK Logged in.
  11. list
  12. +OK 5 messages:
  13. 1 499
  14. 2 504
  15. 3 514
  16. 4 513
  17. 5 565
  18. .
  19. retr 5
  20. +OK 565 octets
  21. Return-Path: <carla@domain.com>
  22. Delivered-To: alrac@studio
  23. Received: from localhost
  24. by studio.alrac.net (Dovecot) with LMTP id y8G5C8aablgKIQAAYelYQA
  25. for <alrac@studio>; Thu, 05 Jan 2017 11:13:10 -0800
  26. Received: from studio (localhost [127.0.0.1])
  27. by localhost (Postfix) with ESMTPS id B9B529FE59
  28. for <alrac@studio>; Thu, 5 Jan 2017 11:12:13 -0800 (PST)
  29. subject: TLS/SSL test
  30. Message-Id: <20170105191240.B9B529FE59@localhost>
  31. Date: Thu, 5 Jan 2017 11:12:13 -0800 (PST)
  32. From: carla@domain.com
  33. Hello, we are testing TLS/SSL. Looking good so far.
  34. .
  35. quit
  36. +OK Logging out.
  37. closed

現在做什麼?

現在你有一個功能良好的,具有合適的 TLS/SSL 保護的郵件服務器了。我鼓勵你深入學習 Postfix 以及 Dovecot; 這些教程中的示例盡可能地簡單,不包括對安全性、防病毒掃描程序、垃圾郵件過濾器或任何其他高級功能的調整。我認為當你有一個基本工作係統時更容易學習高級功能。

下周回到 openSUSE 包管理備忘錄上。

原文發布時間為:2017-02-03

本文來自雲棲社區合作夥伴“Linux中國”

最後更新:2017-05-27 09:31:28

  上一篇:go  LXD 2.0 係列(六):遠程主機及容器遷移
  下一篇:go  在 Linux 中安裝最新的 Thunderbird 郵件客戶端