独自ドメインを運用するにあたり、メールでの連絡が必要なので、postfix と dovecot で 公開型のimpasサーバだけをipv4/ipv6両対応で構築してみました。
MXレコードが必要なのは昔も今も変わりませんが、今どきのメールサーバはdnsにspfレコードがないとgmailやoutlookではメールを受信できません。メールを送信しても、はじき返されるならまだしも、そのまま破棄されてしまう場合もあります。これに加え、dkimレコードとdmarcレコードも必要になります。作業量はかなり多いので、今回変更部分はなるべくパッチファイルで作業できるようにしてみました。なお環境はDebian 12でメールサーバはmx01.example.com、ドメインはexample.com、サブドメイン運用無しとしています。また、事前にmx01.example.comの正引き・逆引きをIPv4/IPv6ともにできるようにしておいてください。
まず、最初に postfix と dovecot と certbot(let's encrypt)をインストールします。
つづいて、certbotを使ってlet's encryptのssl証明書類を発行してもらいます。
パッチの編集が済んだら、このパッチを、/tmp/postfix-main.cf.diffとして保存します。
続いてパッチを当てるには次の様にします。
また、以下のコマンドでaliasデータベースを作成します。
次はdkimの設定です。まずパッケージをインストールします。
続いて /etc/opendmarc.confを編集します。パッチは以下の通りです。
お疲れさまでした。今回は以上です。それでは。
MXレコードが必要なのは昔も今も変わりませんが、今どきのメールサーバはdnsにspfレコードがないとgmailやoutlookではメールを受信できません。メールを送信しても、はじき返されるならまだしも、そのまま破棄されてしまう場合もあります。これに加え、dkimレコードとdmarcレコードも必要になります。作業量はかなり多いので、今回変更部分はなるべくパッチファイルで作業できるようにしてみました。なお環境はDebian 12でメールサーバはmx01.example.com、ドメインはexample.com、サブドメイン運用無しとしています。また、事前にmx01.example.comの正引き・逆引きをIPv4/IPv6ともにできるようにしておいてください。
まず、最初に postfix と dovecot と certbot(let's encrypt)をインストールします。
apt-get install postfix dovecot-imapd certbotPostfixのコンフィグレーションを聞かれた場合は、No configurationを指定してください。
つづいて、certbotを使ってlet's encryptのssl証明書類を発行してもらいます。
certbot certonly --standalone -d mx01.example.com -m admin@example.com --agree-tos次に/etc/postfix/main.cfのパッチファイルです。
--- main.cf.proto 2024-07-26 23:12:02.216307005 +0900
+++ main.cf 2024-07-27 00:45:20.765008342 +0900
@@ -96,14 +96,14 @@
# other configuration parameters.
#
#myhostname = host.domain.tld
-#myhostname = virtual.domain.tld
+myhostname = mx01.example.com
# 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.tld
+mydomain = example.com
# SENDING MAIL
#
@@ -124,7 +124,7 @@
#
#myorigin = /etc/mailname
#myorigin = $myhostname
-#myorigin = $mydomain
+myorigin = $mydomain
# RECEIVING MAIL
@@ -138,7 +138,7 @@
#
# Note: you need to stop/start Postfix when this parameter changes.
#
-#inet_interfaces = all
+inet_interfaces = all
#inet_interfaces = $myhostname
#inet_interfaces = $myhostname, localhost
@@ -186,7 +186,7 @@
# See also below, section "REJECTING MAIL FOR UNKNOWN LOCAL USERS".
#
#mydestination = $myhostname, localhost.$mydomain, localhost
-#mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
+mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
#mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain,
# mail.$mydomain, www.$mydomain, ftp.$mydomain
@@ -413,7 +413,7 @@
# "postfix reload" to eliminate the delay.
#
#alias_maps = dbm:/etc/aliases
-#alias_maps = hash:/etc/aliases
+alias_maps = hash:/etc/aliases
#alias_maps = hash:/etc/aliases, nis:mail.aliases
#alias_maps = netinfo:/aliases
@@ -424,7 +424,7 @@
#
#alias_database = dbm:/etc/aliases
#alias_database = dbm:/etc/mail/aliases
-#alias_database = hash:/etc/aliases
+alias_database = hash:/etc/aliases
#alias_database = hash:/etc/aliases, hash:/opt/majordomo/aliases
# ADDRESS EXTENSIONS (e.g., user+foo)
@@ -446,7 +446,7 @@
# "Maildir/" for qmail-style delivery (the / is required).
#
#home_mailbox = Mailbox
-#home_mailbox = Maildir/
+home_mailbox = Maildir/
# The mail_spool_directory parameter specifies the directory where
# UNIX-style mailboxes are kept. The default setting depends on the
@@ -582,8 +582,8 @@
#
#smtpd_banner = $myhostname ESMTP $mail_name
#smtpd_banner = $myhostname ESMTP $mail_name ($mail_version)
-smtpd_banner = $myhostname ESMTP $mail_name (Debian/GNU)
-
+#smtpd_banner = $myhostname ESMTP $mail_name (Debian/GNU)
+smtpd_banner = $myhostname ESMTP
# PARALLEL DELIVERY TO THE SAME DESTINATION
#
@@ -656,38 +656,64 @@
# sendmail_path: The full pathname of the Postfix sendmail command.
# This is the Sendmail-compatible mail posting interface.
#
-sendmail_path =
+sendmail_path = /usr/sbin/postfix
# newaliases_path: The full pathname of the Postfix newaliases command.
# This is the Sendmail-compatible command to build alias databases.
#
-newaliases_path =
+newaliases_path = /usr/bin/newaliases
# mailq_path: The full pathname of the Postfix mailq command. This
# is the Sendmail-compatible mail queue listing command.
#
-mailq_path =
+mailq_path = /usr/bin/mailq
# setgid_group: The group for mail submission and queue management
# commands. This must be a group name with a numerical group ID that
# is not shared with other accounts, not even with the Postfix account.
#
-setgid_group =
+setgid_group = postdrop
# html_directory: The location of the Postfix HTML documentation.
#
-html_directory =
+#html_directory =
# manpage_directory: The location of the Postfix on-line manual pages.
#
-manpage_directory =
+#manpage_directory =
# sample_directory: The location of the Postfix sample configuration files.
# This parameter is obsolete as of Postfix 2.1.
#
-sample_directory =
+#sample_directory =
# readme_directory: The location of the Postfix README files.
#
-readme_directory =
-inet_protocols = ipv4
+#readme_directory =
+inet_protocols = all
+
+smtpd_sasl_auth_enable = yes
+smtpd_sasl_type = dovecot
+smtpd_sasl_path = private/auth
+broken_sasl_auth_clients = yes
+
+smtpd_relay_restrictions =
+ permit_mynetworks
+ permit_sasl_authenticated
+ reject_unauth_destination
+
+smtpd_sasl_security_options = noanonymous
+smtpd_sasl_local_domain = $myhostname
+
+smtpd_tls_security_level = may
+smtpd_tls_auth_only = yes
+smtpd_tls_loglevel = 1
+smtpd_tls_cert_file = /etc/letsencrypt/live/mx01.example.com/fullchain.pem
+smtpd_tls_key_file = /etc/letsencrypt/live/mx01.example.com/privkey.pem
+smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
+
+milter_default_action = accept
+milter_protocol = 6
+smtpd_milters = local:opendkim/opendkim.sock,local:opendmarc/opendmarc.sock
+non_smtpd_milters = $smtpd_milters
+
パッチを編集するには、エディタを使いmx01.example.com/example.comの順で置換してください。以下同様です。パッチの編集が済んだら、このパッチを、/tmp/postfix-main.cf.diffとして保存します。
続いてパッチを当てるには次の様にします。
apt-get install patch cd /etc/postfix cp main.cf.proto main.cf patch -p0 < /tmp/postfix-main.cf.diff次は、/etc/postfix/master.cfです。
--- master.cf.proto 2024-07-26 23:12:02.220306985 +0900 +++ master.cf 2024-07-26 23:47:42.930313831 +0900 @@ -16,10 +16,10 @@ #tlsproxy unix - - y - 0 tlsproxy # Choose one: enable submission for loopback clients only, or for any client. #127.0.0.1:submission inet n - y - - smtpd -#submission inet n - y - - smtpd +submission inet n - y - - smtpd # -o syslog_name=postfix/submission -# -o smtpd_tls_security_level=encrypt -# -o smtpd_sasl_auth_enable=yes + -o smtpd_tls_security_level=encrypt + -o smtpd_sasl_auth_enable=yes # -o smtpd_tls_auth_only=yes # -o smtpd_reject_unlisted_recipient=no # Instead of specifying complex smtpd_/tmp/postfix-master.cf.diffとして保存し、パッチを当てます。_restrictions here, @@ -30,7 +30,7 @@ # -o smtpd_helo_restrictions= # -o smtpd_sender_restrictions= # -o smtpd_relay_restrictions= -# -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject + -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject # -o milter_macro_daemon_name=ORIGINATING # Choose one: enable submissions for loopback clients only, or for any client. #127.0.0.1:submissions inet n - y - - smtpd
cd /etc/postfix cp master.cf.proto master.cf patch -p0 < /tmp/postfix-master.cf.diff続いいて dovecotの設定です。 /etc/dovecot/conf.dの内、いくつか編集しますので、先にコピーをしておきます。
cd /etc/dovecot/conf.d cp -a 10-auth.conf 10-auth.conf.orig cp -a 10-mail.conf 10-mail.conf.orig cp -a 10-master.conf 10-master.conf.orig cp -a 10-ssl.conf 10-ssl.conf.origパッチは以下の通りです。
--- 10-auth.conf.orig 2024-07-27 02:06:37.207648287 +0900
+++ 10-auth.conf 2024-07-26 23:34:37.223611218 +0900
@@ -48,7 +48,7 @@
# the standard variables here, eg. %Lu would lowercase the username, %n would
# drop away the domain if it was given, or "%n-AT-%d" would change the '@' into
# "-AT-". This translation is done after auth_username_translation changes.
-#auth_username_format = %Lu
+auth_username_format = %n
# If you want to allow master users to log in by specifying the master
# username within the normal username string (ie. not using SASL mechanism's
@@ -97,7 +97,7 @@
# plain login digest-md5 cram-md5 ntlm rpa apop anonymous gssapi otp
# gss-spnego
# NOTE: See also disable_plaintext_auth setting.
-auth_mechanisms = plain
+auth_mechanisms = plain login
##
## Password and user databases
--- 10-mail.conf.orig 2024-07-27 02:06:37.283647408 +0900
+++ 10-mail.conf 2024-07-26 23:35:07.999247538 +0900
@@ -27,7 +27,8 @@
#
# <doc/wiki/MailLocation.txt>
#
-mail_location = mbox:~/mail:INBOX=/var/mail/%u
+#mail_location = mbox:~/mail:INBOX=/var/mail/%u
+mail_location = maildir:~/Maildir
# If you need to set multiple mailbox locations or want to change default
# namespace settings, you can do it by defining namespace sections.
--- 10-master.conf.orig 2024-07-27 02:06:37.307647130 +0900
+++ 10-master.conf 2024-07-26 21:39:40.687479947 +0900
@@ -16,11 +16,11 @@
service imap-login {
inet_listener imap {
- #port = 143
+ port = 0
}
inet_listener imaps {
- #port = 993
- #ssl = yes
+ port = 993
+ ssl = yes
}
# Number of connections to handle before starting a new process. Typically
@@ -104,9 +104,11 @@
}
# Postfix smtp-auth
- #unix_listener /var/spool/postfix/private/auth {
- # mode = 0666
- #}
+ unix_listener /var/spool/postfix/private/auth {
+ mode = 0666
+ user = postfix
+ user = postfix
+ }
# Auth process is run as this user.
#user = $default_internal_user
--- 10-ssl.conf.orig 2024-07-27 02:06:37.335646806 +0900
+++ 10-ssl.conf 2024-07-26 21:34:59.100585304 +0900
@@ -3,14 +3,14 @@
##
# SSL/TLS support: yes, no, required.
-ssl = yes
+ssl = required
# 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, just make sure to update the domains in dovecot-openssl.cnf
-ssl_cert = </etc/dovecot/private/dovecot.pem
-ssl_key = </etc/dovecot/private/dovecot.key
+ssl_cert = </etc/letsencrypt/live/mx01.example.com/fullchain.pem
+ssl_key = </etc/letsencrypt/live/mx01.example.com/privkey.pem
# If key file is password protected, give the password here. Alternatively
# give it when starting dovecot with -p parameter. Since this file is often
このパッチを、/tmp/dovecot-conf.d.diffとして保存し、以下の様にしてパッチを当てます。
cd /etc/dovecot/conf.d patch -p0 < /tmp/dovecot-conf.d.diff次に、/etc/dovecot/dovecot.confファイルにパッチを当てます。パッチは以下の通りです。
--- dovecot.conf.orig 2023-01-20 15:01:26.000000000 +0900 +++ dovecot.conf 2024-07-25 19:58:34.588232434 +0900 @@ -27,7 +27,7 @@ # "*" listens in all IPv4 interfaces, "::" listens in all IPv6 interfaces. # If you want to specify non-default ports or anything more complex, # edit conf.d/master.conf. -#listen = *, :: +listen = *, :: # Base directory where to store runtime data. #base_dir = /var/run/dovecot/このパッチを、/tmp/dovecot.conf.diffとして保存し、以下の様にしてパッチを当てます。
cd /etc/dovecot patch -p0 < /tmp/dovecot.conf.diff次に、let's encryptが更新された後、postfixとdovecotが再起動されるようにしておきます。
cat <<EOF > /etc/letsencrypt/renewal-hooks/deploy/restart_postfix.sh #!/bin/bash systemctl restart postfix EOF chmod 755 /etc/letsencrypt/renewal-hooks/deploy/restart_postfix.sh cat <<EOF > /etc/letsencrypt/renewal-hooks/deploy/restart_dovecot.sh #!/bin/bash systemctl restart dovecot EOF chmod 755 /etc/letsencrypt/renewal-hooks/deploy/restart_dovecot.sh次に、dnsのmxレコードとspfレコードを更新しておきます。
IN MX 10 mx01.example.com.
IN TXT "v=spf1 +ip4:10.10.10.25 +ip6:2001:0db8:111:2222:10:10:10:25 -all"
参考までに、独自dnsを立てている場合は作業が終わるまでは$TTLは600(10分)程度にしておくといいと思います。また、以下のコマンドでaliasデータベースを作成します。
newaliases続いて受信時のspfチェックを行うために、パッケージをインストールし、設定します。
apt-get install postfix-policyd-spf-python次に、/etc/postfix/master.cfに以下を追加します。
# # policy-spf # policyd-spf unix - n n - 0 spawn user=policyd-spf argv=/usr/bin/policyd-spf次に、/etc/postfix/main.cfの最後の方を以下の様にします。
smtpd_relay_restrictions =
permit_mynetworks
permit_sasl_authenticated
reject_unauth_destination
check_policy_service unix:private/policyd-spf ##追加
policyd-spf_time_limit = 3600
設定が済んだら、systemctl reload postfixとして下さい。次はdkimの設定です。まずパッケージをインストールします。
apt-get install opendkim opendkim-tools続いて /etc/opendkim.confを編集します。パッチは以下の通りです。
--- opendkim.conf.orig 2024-07-27 15:07:29.978648331 +0900 +++ opendkim.conf 2024-07-27 00:27:52.800540869 +0900 @@ -11,8 +11,8 @@ # oversigned, because it is often the identity key used by reputation systems # and thus somewhat security sensitive. Canonicalization relaxed/simple -#Mode sv -#SubDomains no +Mode sv +SubDomains no OversignHeaders From # Signing domain, selector, and key (required). For example, perform signing @@ -34,10 +34,10 @@ # it must be ensured that the socket is accessible. In Debian, Postfix runs in # a chroot in /var/spool/postfix, therefore a Unix socket would have to be # configured as shown on the last line below. -Socket local:/run/opendkim/opendkim.sock +#Socket local:/run/opendkim/opendkim.sock #Socket inet:8891@localhost #Socket inet:8891 -#Socket local:/var/spool/postfix/opendkim/opendkim.sock +Socket local:/var/spool/postfix/opendkim/opendkim.sock PidFile /run/opendkim/opendkim.pid @@ -49,3 +49,8 @@ # by the package dns-root-data. TrustAnchorFile /usr/share/dns/root.key #Nameservers 127.0.0.1 + +KeyTable file:/etc/opendkim/key.table +SigningTable refile:/etc/opendkim/signing.table +ExternalIgnoreList refile:/etc/opendkim/trusted.hosts +InternalHosts refile:/etc/opendkim/trusted.hostsパッチを/tmp/opendkim.conf.diffとして保存してください。パッチを当てるには以下の様にします。
cd /etc cp -a opendkim.conf opendkim.conf.orig patch -p0 < /tmp/opendkim.conf.diff続いてopendkimで使用する鍵を保存する場所などを作成します。
sudo mkdir -p /etc/opendkim/keys/example.com sudo chown -R opendkim:opendkim /etc/opendkim sudo chmod 700 /etc/opendkim/keys sudo mkdir /var/spool/postfix/opendkim sudo chown opendkim:postfix /var/spool/postfix/opendkim次は/etc/default/opendkimでpostfixを使うようにしておきます。
RUNDIR=/var/spool/postfix/run/opendkim続いて /etc/opendkim/signing.table を作成します。
*@example.com default._domainkey.example.com続いて /etc/opendkim/trusted.hosts を作成します。
127.0.0.1 ::1 localhost続いて鍵を生成し、ファイルオーナーの変更を行います。
sudo opendkim-genkey -b 2048 -d example.com -D /etc/opendkim/keys/example.com -s default -v sudo chown opendkim:opendkim /etc/opendkim/keys/example.com/default.txt /etc/opendkim/keys/example.com/default.private続いて、dnsサーバにdkimレコードを追加します。内容は/etc/opendkim/keys/example.com/default.txtをそのまま貼り付ければOKですが、サーバによってはレコードの長さに制限がある場合があるので、その場合は鍵の長さを 2048から変更する必要があり注意してください。bind9で独自dnsを立てている場合は、dkimレコードを追加したら、dnsサーバをrestartしてください。 なお当方の設定では以下のレコードも追加しています。
_domainkey IN TXT "o=-"dkimレコードの追加が済んだら、以下の様にテストします。
# opendkim-testkey -d example.com -s default -vvv opendkim-testkey: using default configfile /etc/opendkim.conf opendkim-testkey: checking key 'default._domainkey.example.com' opendkim-testkey: key not secure opendkim-testkey: key OKkey not secureはdnssecの設定をしていない場合このように表示されますが、dkimの設定としてはこれでOKです。 続いてopendmarcをインストール・設定します。
apt-get install opendmarcdbconfig-commでopendmarc用のdatabaseを設定する旨を聞いてきたら、今回は小規模環境での運用なので、いいえを選択してください。
続いて /etc/opendmarc.confを編集します。パッチは以下の通りです。
--- opendmarc.conf.orig 2024-07-27 15:46:22.406651483 +0900
+++ opendmarc.conf 2024-07-27 00:43:07.105524941 +0900
@@ -10,7 +10,7 @@
## provided, the name of the host running the filter (as returned by the
## gethostname(3) function) will be used.
#
-# AuthservID name
+AuthservID OpenDMARC
## FailureReports { true | false }
## default "false"
@@ -49,7 +49,7 @@
## evaluation of the message. Instead, an Authentication-Results header
## field will be added.
#
-# RejectFailures false
+RejectFailures true
## Socket socketspec
## default (none)
@@ -64,7 +64,8 @@
## either in the configuration file or on the command line. If an IP
## address is used, it must be enclosed in square brackets.
#
-Socket local:/run/opendmarc/opendmarc.sock
+#Socket local:/run/opendmarc/opendmarc.sock
+Socket local:/var/spool/postfix/opendmarc/opendmarc.sock
## Syslog { true | false }
## default "false"
@@ -90,7 +91,7 @@
## with a comma. The key word "HOSTNAME" will be replaced by the name of
## the host running the filter as reported by the gethostname(3) function.
#
-# TrustedAuthservIDs HOSTNAME
+TrustedAuthservIDs mx01.example.com
## UMask mask
## default (none)
@@ -112,3 +113,9 @@
## the named userid unless an alternate group is specified.
#
UserID opendmarc
+
+IgnoreHosts /etc/opendmarc/ignore.hosts
+SPFSelfValidate true
+IgnoreAuthenticatedClients true
+RequiredHeaders true
+
パッチを、/tmp/opendmarc.conf.diffとして保存し、以下の様にしてパッチを当てます。
cd /etc cp -a opendmarc.conf opendmarc.conf.orig patch -p0 < /tmp/opendmarc.conf.diff/etc/opendmarcフォルダを作成し、そこにignore.hostsを以下の内容で作成します。
127.0.0.1 ::1続いてpostfixとのやり取りなどを準備します。
mkdir /var/spool/postfix/opendmarc chown -R opendmarc:opendmarc /var/spool/postfix/opendmarc chmod 750 /var/spool/postfix/opendmarc adduser postfix opendmarc最後に dmarcレコードをdnsに追加します。
_dmarc IN TXT "v=DMARC1; p=reject; adkim=s; aspf=s;"これで設定は完了です。systemctl restart postfix dovecotとしてメールの送受信をテストしてみてください。
お疲れさまでした。今回は以上です。それでは。
コメント
コメントを投稿