Meaningless Notebook

我輩は雑記帖である。名はまだない。


CentOS 7.2 に Dovecot + Fetchmail をセットアップする。

複数アドレスのメ-ルを受信して集約し、Dovecot で POP3として公開する (なんでこんなめんど-なことをするかというと Aipo おめ-のためだよ) 。

本設定は、外部公開はせず内部の別サ-バに連携するための設定 (セキュリティは一切考慮していない)。

手順は下記の通り。

  1. 必要なモジュールをインスト-ル
  2. Dovecot をセットアップ
  3. Fetchmail をセットアップ
  4. SELinux にポリシ-を追加

1.必要なモジュ-ルをインスト-ル

必要なモジュ-ルを下記コマンドでインスト-ル

yum install dovecot fetchmail

2.Dovecot をセットアップ

1.設定ファイル修正

『/etc/dovecot/dovecot.conf』を下記の通りに編集。

24行目付近

#protocols = imap pop3 lmtp
protocols = imap pop3 lmtp

31行目付近

#listen = *, ::
listen = *, ::

『/etc/dovecot/conf.d/10-auth.conf』を下記の通りに編集。

10行目付近

#disable_plaintext_auth = yes
disable_plaintext_auth = no

101行目付近

# auth_mechanisms = plain
auth_mechanisms = plain login

『/etc/dovecot/conf.d/10-mail.conf』を下記の通りに編集。

30行目付近

#mail_location =
mail_location = maildir:~/Maildir

『/etc/dovecot/conf.d/10-ssl.conf』を下記の通りに編集。

8行目付近

# ssl = required
ssl = no

2.Dovecot サ-ビスの起動設定

下記コマンドを実行して Dovecot サ-ビス起動設定。

systemctl start dovecot.service
systemctl enable dovecot.service

3.Firewalld をセットアップ。

下記コマンドを実行して Firewalld 設定。

firewall-cmd --add-port={110/tcp,143/tcp} --permanent
firewall-cmd --reload

4.ユ-ザ-追加時『Maildir』自動対応設定

下記コマンドを実行して、ユ-ザ-追加時に『Maildir』が自動で追加されるようにする。

mkdir -p /etc/skel/Maildir/{new,cur,tmp}
chmod -R 700 /etc/skel/Maildir/

5.受信ユ-ザ追加 (省略)

環境 (OpenLDAP有無) に対応したユ-ザ追加を行う。

※追加する受信ユ-ザのユ-ザIDに『@』を使わないこと。
 後述するFetchmail起動のsystemdで支障が出るため。

3.Fetchmail をセットアップ

1.『.fetchmailrc』作成

2.5. で追加した受信ユ-ザにログインしてホ-ムディレクトリに『.fetchmailrc』を作成する。

# 共通設定
set daemon 180                         # チェック間隔:3分
set postmaster root                    # 最終的なメールの送信先
set no bouncemail                      # エラーメールをpostmasterに送る
set syslog                             # ログを/var/log/maillogに記録する

# デフォルト設定
defaults
    protocol auto                      # プロトコル自動設定
    no mimedecode                      # 変換無し
    no fetchall                        # 未読メールのみ取り込む
    keep                               # サーバにメールを残す
    mda "/usr/libexec/dovecot/deliver" # Dovecotに委譲

# メールアカウント設定
    poll pop3.live.com                 # 受信メールサーバ
    protocol pop3                      # プロトコル
    port 995                           # ポート
    username "user@hotmail.co.jp"      # ユーザ
    password "password"                # パスワード
    ssl                                # SSL

作成したらパ-ミッションを『600』にする。

2.Fetchmail 動作確認

Fetchmail を起動させる場合は、下記コマンドを実行する。

fetchmail

Fetchmail を停止させる場合は、下記コマンドを実行する。

fetchmail --quit

3.Fetchmail 起動サ-ビス

Unit定義ファイルを作成し、『/etc/systemd/system/』へ配備。

『fetchmail-start.service』

[Unit]
Description=Fetchmail Auto Start
After=network.target dovecot.service

[Service]
Type=oneshot
ExecStart=/bin/sh /usr/local/bin/fetchmail-start.sh
RestartSec=1

[Install]
WantedBy=multi-user.target

4.Fetchmail 起動スクリプト

下記スクリプトを作成し、『/usr/local/bin』へ配備。

『fetchmail-start.sh』

#!/bin/sh

for user in `ls /home/`
do
 if [ -f /home/$user/.fetchmailrc ]; then
 echo "fetchmail for $user starting..."
 systemctl start fetchmail@$user
 fi
done

5.Fetchmail (受信ユ-ザ) 起動サ-ビス

Unit定義ファイルを作成し、『/etc/systemd/system/』へ配備。

『fetchmail@.service』

[Unit]
Description=A remote-mail retrieval utility
After=network.target dovecot.service

[Service]
User=%i
ExecStart=/usr/bin/fetchmail -s
RestartSec=1

[Install]
WantedBy=multi-user.target

[Service] 部の User には、Fetchmail 起動スクリプトで対象となったユ-ザID が設定される。

具体的には、fetchmail@の後ろに続く文字列 (Fetchmail 起動スクリプトで付与される文字列) が設定されるのだが『@』で分割されるためユ-ザID に『@』が含まれると正しく動作しない (1敗)。

6.SELinux を設定する

以下のポリシ-をOnにする。

daemons_enable_cluster_mode

ポリシ-のパラメ-タ値を確認するコマンド

getsebool -a | grep キ-ワ-ド

ポリシ-のパラメ-タ値を設定するコマンド

setsebool -P ポリシ-名=on

7.Fetchmail サ-ビス化の確認 及び 起動

下記コマンドを実行して Fetchmail サ-ビス起動設定

systemctl list-unit-files --type=service
systemctl start fetchmail-start.service
systemctl enable fetchmail-start.service

4.SELinux にポリシ-を追加

1.下記コマンドでポリシ-追加

vi fetchmailstartup.te
module fetchmailstartup 1.0;

require {
        type dovecot_etc_t;
        type fetchmail_t;
        type user_home_dir_t;
        type user_home_t;
        type mail_home_rw_t;
        type dovecot_deliver_exec_t;
        class dir { add_name getattr open read remove_name search write };
        class file { append create execute execute_no_trans getattr lock open read rename unlink write };
}

#============= fetchmail_t ==============
allow fetchmail_t dovecot_deliver_exec_t:file { execute execute_no_trans open read };
allow fetchmail_t dovecot_etc_t:dir { open read search };
allow fetchmail_t dovecot_etc_t:file { getattr open read };
allow fetchmail_t mail_home_rw_t:dir { add_name getattr open read remove_name write };
allow fetchmail_t mail_home_rw_t:file { append create getattr lock open read rename unlink write };

#!!!! This avc is allowed in the current policy
allow fetchmail_t user_home_dir_t:dir write;
allow fetchmail_t user_home_dir_t:dir { add_name remove_name };
allow fetchmail_t user_home_dir_t:file { create getattr open read rename unlink write };

#!!!! This avc is allowed in the current policy
allow fetchmail_t user_home_t:file read;
allow fetchmail_t user_home_t:file { open unlink };
checkmodule -m -M -o fetchmailstartup.mod fetchmailstartup.te
semodule_package --outfile fetchmailstartup.pp --module
fetchmailstartup.mod
semodule -i fetchmailstartup.pp

参考元

  1. Server World
  2. メールサーバー構築(Postfix+Dovecot)
  3. はじめての自宅サーバ構築 – Fedora/CentOS –
  4. mk-mode BLOG
  5. wadahiroの日記
  6. redhat
  7. man page of fetchmail
  8. gentoo linxu wiki

Amazon