LoginSignup
4
2

More than 1 year has passed since last update.

FreeBSD 12 で自宅サーバ作成 メールサーバ編その2

Last updated at Posted at 2019-03-10

はじめに

FreeBSD 12を使用した自宅サーバの構築を行った。FreeBSD 12はリリースされて間もない状態ですが、カーネルに標準でVIMAGEが取り込まれDNSの外向きと、内向きを別々のシステム(jail+VIMAGE)が同一ホストで構築できることから採用することにしました。その時の構築手順を備忘録の意味も含め、具体的にいくつかに渡り説明する。

概要

メールサーバ編その2では、認証及びウイルス/ SPAMコンテンツフィルタを対象にする。postfixとの連携はメールフィルター(Milter)により行う。

  • 認証
    DKIM
    電子署名を使用した送信ドメイン認証
    DMARC
    自分ドメインを詐称するメールの処理方法を指定
  • ウイルス/ SPAMコンテンツ
    ClamAV
    メールサーバ用のアンチウイルス
    Spamassasin
    スパム(迷惑メール)をブロックするためのフィルタ

OpenDKIM

インストール

標準オプションで使用する

jail1 /root # pkg install opendkim
Updating FreeBSD repository catalogue...
FreeBSD repository is up to date.
All repositories are up to date.
The following 3 package(s) will be affected (of 0 checked):

New packages to be INSTALLED:
        opendkim: 2.10.3_11
        lua52: 5.2.4
        unbound: 1.9.0_1

Number of packages to be installed: 3

The process will require 9 MiB more space.
2 MiB to be downloaded.
(以下省略)

設定

使用宣言

/etc/rc.conf
milteropendkim_enable="YES"

opendkim.confの編集

opendkimデーモンとpostfixがsocketで通信するが、各々が違うユーザで実行することからグループで共有させるためUMaskを変更している。

/usr/local/etc/mail/opendkim.conf
##
## opendkim.conf -- configuration file for OpenDKIM filter
##
## Copyright (c) 2010-2015, The Trusted Domain Project.  All rights reserved.
##
Canonicalization        simple/simple
Domain                  example.jp
KeyFile                 /var/db/dkim/example.jp.private
LogWhy                  yes
Mode                    sv
ReportAddress           "DKIM Error Postmaster" <postmaster@example.jp>
Selector                example.jp
SendReports             yes
Socket                  local:/var/run/milteropendkim/socket
SubDomains              yes
Syslog                  Yes
SyslogSuccess           yes
UMask                   002
/etc/group
mailnull:*:26:postfix

認証キーの作成

jail1 /root # mkdir /var/db/dkim
jail1 /root # chmod 700 /var/db/dkim
jail1 /root # opendkim-genkey -D /var/db/dkim -d example.jp -s example.jp
jail1 /root # chown -R mailnull:mailnull /var/db/dkim
jail1 /root # ls -l /var/db/dkim
total 9
-rw-------  1 mailnull  mailnull  887 Mar  9 14:08 example.jp.private
-rw-------  1 mailnull  mailnull  311 Mar  9 14:08 example.jp.txt

公開キー設定

dkim用公開キーなどをDNSのTXTレコードに設定する。(/var/db/dkim/example.jp.txtの内容を設定)

/usr/local/etc/namedb/master/example.jp.zone
_domainkey             IN  TXT     "t=y; o=~"
example.jp._domainkey  IN  TXT     "v=DKIM1;........."                  ←  /var/db/dkim/example.jp.txtの内容
_adsp._domainkey       IN  TXT     "dkim=unknown"

postfixの設定変更

/usr/local/etc/postfix/main.cf
# mail filter
smtpd_milters = unix:/var/run/milteropendkim/socket
non_smtpd_milters = $smtpd_milters
milter_default_action = accept

起動

jail1 /root # service milter-opendkim start
Starting milteropendkim.
jail1 /root # service postfix reload
postfix/postfix-script: refreshing the Postfix mail system

OpenDMARC

インストール

標準オプションで使用するが、mysql56-clientが関連付けられているので、ports collectionからインストールする。
mysql関連以外はpkgでインストールしたいので、mysql80-clientをロックし、まずpkgでインストールする。

jail1 /root # pkg install opendmarc
Updating FreeBSD repository catalogue...
FreeBSD repository is up to date.
All repositories are up to date.
The following 13 package(s) will be affected (of 0 checked):

New packages to be INSTALLED:
        opendmarc: 1.3.2_1
        p5-HTTP-Message: 6.18
        p5-LWP-MediaTypes: 6.02_1
        p5-HTTP-Date: 6.02_1
        p5-URI: 1.76
        p5-IO-HTML: 1.001_1
        p5-Encode-Locale: 1.05
        libspf2: 1.2.10_2
        p5-Switch: 2.17_1
        p5-Filter: 1.59
        p5-DBI: 1.642
        p5-DBD-mysql: 4.050
        mysql56-client: 5.6.43

Number of packages to be installed: 13

The process will require 42 MiB more space.
3 MiB to be downloaded.
(途中省略)
pkg: mysql80-client-8.0.15 conflicts with mysql56-client-5.6.43 (installs files into the same place).  Problematic file: /usr/local/bin/mysql

mysql56-client: 5.6.43がインストールされずにその他はインストールされる。
次にports collectionからインストールする。

jail1 /root # portinstall opendmarc
[Reading data from pkg(8) ... - 166 packages found - done]
--->  Installing 'opendmarc-1.3.2_1' from a port (mail/opendmarc)
--->  Building '/usr/ports/mail/opendmarc'
===>  Cleaning for opendmarc-1.3.2_1
(途中省略)

opendmarc.png

[jail1] Installing opendmarc-1.3.2_1...
===>  Cleaning for p5-DBD-mysql-4.050
===>  Cleaning for p5-Devel-CheckLib-1.13
===>  Cleaning for opendmarc-1.3.2_1

3本のアプリがインストールを完了する。

設定

使用宣言

2行目のソケットの設定は、opendmarc.confで定義するが、rc.dにあるスタートアップスクリプトでinet接続が引数で指定されるためここでも宣言する。

/etc/rc.conf
opendmarc_enable="YES"
opendmarc_socketspec="/var/run/opendmarc/socket"

opendmarc.confの編集

/usr/local/etc/mail/にサンプルファイルがあるのでコピーして編集する。

/usr/local/etc/mail/opendmarc.conf
## opendmarc.conf -- configuration file for OpenDMARC filter
##
## Copyright (c) 2012-2015, The Trusted Domain Project.  All rights reserved.

AutoRestart ture
BaseDirectory /var/run/opendmarc
IgnoreHosts /usr/local/etc/mail/opendmarc_ignore.hosts
IgnoreMailFrom example.jp
Socket local:/var/run/opendmarc/socket
SPFSelfValidate true
UMask 002

IgnoreHostsの編集

/usr/local/etc/mail/opendmarc_ignore.hosts
localhost
::1
2001:db8::/32
127.0.0.0/8
192.168.1.0/24

postfixの設定変更

3行目のみ追加

/usr/local/etc/postfix/main.cf
# mail filter
smtpd_milters =
    unix:/var/run/milteropendkim/socket
    unix:/var/run/opendmarc/socket

non_smtpd_milters = $smtpd_milters
milter_default_action = accept

policyd-spf設定削除

今回、opendmarcのspf評価を使用するためpolicyd-spfの設定を削除する。

/usr/local/etc/postfix/main.cf
# policyd-spf
#policyd-spf_time_limit = 3600
#       check_policy_service unix:private/policyd-spf
/usr/local/etc/postfix/master.cf
#policyd-spf  unix  -    n       n       -       0       spawn
#   user=nobody argv=/usr/local/bin/policyd-spf

DNSにTXTレコード追加

/usr/local/etc/namedb/master/example.jp.zone
_dmarc          IN TXT "v=DMARC1; p=none; sp=none; ri=3600; rua=mailto:postmaster@example.jp;   ruf=mailto:postmaster@example.jp"

起動

jail1 /root # service opendmarc start
Starting opendmarc.
jail1 /root # service postfix restart
postfix/postfix-script: stopping the Postfix mail system
postfix/postfix-script: starting the Postfix mail system

ClamAV

clamav-milterを使用する

インストール

jail1 /root # pkg install clamav-milter
Updating FreeBSD repository catalogue...
FreeBSD repository is up to date.
All repositories are up to date.
Updating database digests format: 100%
The following 7 package(s) will be affected (of 0 checked):

New packages to be INSTALLED:
        clamav-milter: 0.101.1,1
        curl: 7.64.0_1
        libltdl: 2.4.6
        unzoo: 4.4_2
        libmspack: 0.9.1
        arj: 3.10.22_7
        arc: 5.21p

Number of packages to be installed: 7

The process will require 9 MiB more space.
3 MiB to be downloaded.
(以下省略)

設定

使用宣言

/etc/rc.conf
clamav_clamd_enable="YES"
clamav_freshclam_enable="YES"
clamav_milter_enable="YES"

freshclam.confの設定

/usr/local/etc/freshclam.conf
##
## Example config file for freshclam
## Please read the freshclam.conf(5) manual before editing this file.
##
DatabaseDirectory /var/db/clamav
UpdateLogFile /var/log/clamav/freshclam.log
LogFacility LOG_MAIL
PidFile /var/run/clamav/freshclam.pid
DatabaseOwner clamav
DatabaseMirror database.clamav.net
NotifyClamd /usr/local/etc/clamd.conf

clamd.confの設定

/usr/local/etc/clamd.conf
##
## Example config file for the Clam AV daemon
## Please read the clamd.conf(5) manual before editing this file.
##
LogFile /var/log/clamav/clamd.log
PidFile /var/run/clamav/clamd.pid
DatabaseDirectory /var/db/clamav
LocalSocket /var/run/clamav/clamd.sock
FixStaleSocket yes
User clamav
ScanMail yes

clamav-milter.confの設定

/usr/local/etc/clamav-milter.conf
##
## Example config file for clamav-milter
##
MilterSocket /var/run/clamav/clmilter.sock
FixStaleSocket yes
User clamav
PidFile /var/run/clamav/clamav-milter.pid
ClamdSocket unix:/var/run/clamav/clamd.sock
MaxFileSize 100M
AddHeader Replace
ReportHostname mail.example.jp
VirusAction /usr/local/bin/my_infected_message_handler
LogFacility LOG_MAIL
LogInfected Basic

my_infected_message_handlerの作成

/usr/local/sbin/my_infected_message_handler
#!/bin/sh
# REF: http://d.hatena.ne.jp/tak_yah/touch/20120229/1330481208
#

cat << EOM | mail -s "Virus Found by clamav-milter" root

-------------------------------------------------------
Virus Mail Information
-------------------------------------------------------
Queue-id: $2
Message-id: $6
Date:  $7
Subject: $5
Sender:  $3
Destination:  $4
Virus Name: $1
-------------------------------------------------------

EOM

postfixの設定変更

5行目のみ追加

/usr/local/etc/postfix/main.cf
# mail filter
smtpd_milters =
        unix:/var/run/milteropendkim/socket
        unix:/var/run/opendmarc/socket
        unix:/var/run/clamav/clamav-milter.sock

non_smtpd_milters = $smtpd_milters
milter_default_action = accept

最新の定義ファイルを取得

jail1 /root # freshclam
ClamAV update process started at Sat Mar  9 20:22:04 2019
Downloading main.cvd [100%]
main.cvd updated (version: 58, sigs: 4566249, f-level: 60, builder: sigmgr)
Downloading daily.cvd [100%]
daily.cvd updated (version: 25383, sigs: 1507935, f-level: 63, builder: raynman)
Downloading bytecode.cvd [100%]
bytecode.cvd updated (version: 328, sigs: 94, f-level: 63, builder: neo)
Database updated (6074278 signatures) from database.clamav.net (IP: 104.16.218.84)
WARNING: Clamd was NOT notified: Can't connect to clamd through /var/run/clamav/clamd.sock: No such file or directory

起動

jail1 /root # service clamav-clamd start
Starting clamav_clamd.
jail1 /root # service clamav-milter start
Waiting for clamd socket..
Starting clamav_milter.
Waiting for clamav-milter socket..
jail1 /root # service clamav-freshclam start
Starting clamav_freshclam.
jail1 /root # service postfix reload
postfix/postfix-script: refreshing the Postfix mail system

Spamassasin

spamass-milterを使用する

インストール

jail1 /root # pkg install spamass-milter
Updating FreeBSD repository catalogue...
FreeBSD repository is up to date.
All repositories are up to date.
The following 29 package(s) will be affected (of 0 checked):

New packages to be INSTALLED:
        spamass-milter: 0.4.0_3
        spamassassin: 3.4.2_3
        p5-HTML-Parser: 3.72
        p5-HTML-Tagset: 3.20_1
        p5-Net-IDN-Encode: 2.500
        p5-IO-Socket-SSL: 2.060
        p5-Mozilla-CA: 20180117
        p5-Net-SSLeay: 1.85
        p5-IO-Socket-INET6: 2.72_1
        p5-Socket6: 0.29
        p5-Crypt-OpenSSL-RSA: 0.31
        p5-Crypt-OpenSSL-Random: 0.15
        p5-Crypt-OpenSSL-Bignum: 0.09
        gnupg1: 1.4.23_1
        p5-Net-CIDR-Lite: 0.21_1
        p5-NetAddr-IP: 4.079
        p5-Mail-SPF: 2.9.0_4
        p5-Error: 0.17027
        p5-Net-DNS-Resolver-Programmable: 0.009
        p5-Net-DNS: 1.19,1
        p5-Digest-HMAC: 1.03_1
        p5-Mail-DKIM: 0.54
        p5-Mail-Tools: 2.19
        p5-TimeDate: 2.30_2,1
        p5-Net-DNS-Resolver-Mock: 1.20171219
        p5-Net-LibIDN: 0.12_4
        libidn: 1.34_1
        re2c: 0.14.3
        p5-Encode-Detect: 1.01_1

Number of packages to be installed: 29

The process will require 15 MiB more space.
4 MiB to be downloaded.
(以下省略)

設定

使用宣言

/etc/rc.conf
spamd_enable=YES
spamass_milter_enable="YES"
spamass_milter_user="spamd"
spamass_milter_group="spamd"
spamass_milter_socket="/var/run/spamass-milter/spamass-milter.sock"
spamass_milter_socket_owner="spamd"
spamass_milter_socket_group="mail"
spamass_milter_socket_mode="660"
spamass_milter_localflags="-u spamd -- -u spamd"

postfixの設定変更

6行目のみ追加

/usr/local/etc/postfix/main.cf
# mail filter
smtpd_milters =
    unix:/var/run/milteropendkim/socket
    unix:/var/run/opendmarc/socket
    unix:/var/run/clamav/clamav-milter.sock
    unix:/var/run/spamass-milter/spamass-milter.sock

non_smtpd_milters = $smtpd_milters
milter_default_action = accept

ルールファイルの更新

jail1 /root # sa-update
gpg: WARNING: using insecure memory!
gpg: please see http://www.gnupg.org/documentation/faqs.html for more information

起動

jail1 /root # service sa-spamd start
Starting spamd.
jail1 /root # mkdir /var/run/spamass-milter
jail1 /root # chown spamd:spamd /var/run/spamass-milter
jail1 /root # service spamass-milter start
Starting spamass_milter.
jail1 /root # service postfix reload
postfix/postfix-script: refreshing the Postfix mail system

以下2022年1月15日追記
spamassasinが更新された時に以下のエラーが出るようになり調査をした

connect to spamd on ::1 failed, retrying (#1 of 3): Connection refused
connect to spamd on 127.0.0.1 failed, retrying (#1 of 3): Connection refused

spamdが起動時にエラーを出して停止していたことが原因でした。

jail1 /root # service sa-spamd status
spamd is not running.
jail1 /root # service sa-spamd start
Starting spamd.
child process [39390] exited or timed out without signaling production of a PID file: exit 255 at /usr/local/bin/spamd
/usr/local/etc/rc.d/sa-spamd: WARNING: failed to start spamd

内部のデータ構造が変化したようです。
そこで以下の操作で無事起動しました

jail1 /root # sa-update
gpg: WARNING: using insecure memory!
gpg: please see http://www.gnupg.org/documentation/faqs.html for more information
jail1 /root # service sa-spamd start
Starting spamd.

2022年1月15日追記終了

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2