CentOS7 で Postfix + Dovecot + MariaDBでバーチャルメールボックスを利用してメールを送受信する (2) - dovecot の設定 - Postfix

CentOS7 で Postfix + Dovecot + MariaDBでバーチャルメールボックスを利用してメールを送受信する手順の紹介です。

事前準備

先にPostfixの設定を済ませます。詳しくは「CentOS7でPostfix+ Dovecot +MariaDB(MySQL)でバーチャルメールボックスを利用してメールを送受信する (1) - Postfix の設定 (Postfix Tips & FAQ)」の記事を参照してください。

Dovecotの設定

/etc/dovecot/conf.d/10-auth.conf の編集

/etc/dovecot/conf.d/10-auth.conf ファイルを編集します。
10-auth.confファイルの下部の
#!include auth-deny.conf.ext
#!include auth-master.conf.ext

!include auth-system.conf.ext
#!include auth-sql.conf.ext
#!include auth-ldap.conf.ext
#!include auth-passwdfile.conf.ext
#!include auth-checkpassword.conf.ext
#!include auth-vpopmail.conf.ext
#!include auth-static.conf.ext
とincludeが並んでいる部分を
#!include auth-deny.conf.ext
#!include auth-master.conf.ext

#!include auth-system.conf.ext
!include auth-sql.conf.ext
#!include auth-ldap.conf.ext
#!include auth-passwdfile.conf.ext
#!include auth-checkpassword.conf.ext
#!include auth-vpopmail.conf.ext
#!include auth-static.conf.ext
とします。(!include auth-sql.conf.extを有効にします。!auth-system.conf.ext は無効にします。)

/etc/dovecot/conf.d/auth-sql.conf.ext の編集

/etc/dovecot/conf.d/auth-sql.conf.ext を編集します。
変更前
# Authentication for SQL users. Included from 10-auth.conf.
#
# <doc/wiki/AuthDatabase.SQL.txt>

passdb {
  driver = sql

  # Path for SQL configuration file, see example-config/dovecot-sql.conf.ext
  args = /etc/dovecot/dovecot-sql.conf.ext
}

# "prefetch" user database means that the passdb already provided the
# needed information and there's no need to do a separate userdb lookup.
# <doc/wiki/UserDatabase.Prefetch.txt>
#userdb {
#  driver = prefetch
#}

userdb {
  driver = sql
  args = /etc/dovecot/dovecot-sql.conf.ext
}

# If you don't have any user-specific settings, you can avoid the user_query
# by using userdb static instead of userdb sql, for example:
# <doc/wiki/UserDatabase.Static.txt>
#userdb {
  #driver = static
  #args = uid=vmail gid=vmail home=/var/vmail/%u
#}
変更後
# Authentication for SQL users. Included from 10-auth.conf.
#
# <doc/wiki/AuthDatabase.SQL.txt>

passdb {
  driver = sql

  # Path for SQL configuration file, see example-config/dovecot-sql.conf.ext
  args = /etc/dovecot/dovecot-sql.conf.ext
}

# "prefetch" user database means that the passdb already provided the
# needed information and there's no need to do a separate userdb lookup.
# <doc/wiki/UserDatabase.Prefetch.txt>
#userdb {
#  driver = prefetch
#}

#userdb {
#  driver = sql
#  args = /etc/dovecot/dovecot-sql.conf.ext
#}

# If you don't have any user-specific settings, you can avoid the user_query
# by using userdb static instead of userdb sql, for example:
# <doc/wiki/UserDatabase.Static.txt>
userdb {
  driver = static
  args = uid=5000 gid=5000 home=/home/vmail/%d/%n allow_all_users=yes
}

ユーザーごとに固有の設定をしないため、静的なuser db SQLを利用します。コメントアウトされている下部の userdbを有効にし、上部のuserdb をコメントアウトします。

/etc/dovecot/dovecot-sql.conf.ext の作成と編集

passdb で参照するパスワード照合のSQL設定ファイルを編集します。 /etc/dovecot/dovecot-sql.conf.ext を編集します。ファイルが無い場合は作成します。
下記の内容を記述します。
driver = mysql
connect = host=127.0.0.1 dbname=mail user=mail_admin password=(mail_admin のDBのパスワード)
default_pass_scheme = CRYPT
password_query = SELECT email as user, password FROM users WHERE email='%u';

Plaintextでの認証の有効化

Version2のDovecotでは、デフォルトでPaintTextの認証が無効になっています。/etc/dovecot/conf.d/10-auth.confファイルの
編集前
# Disable LOGIN command and all other plaintext authentications unless
# SSL/TLS is used (LOGINDISABLED capability). Note that if the remote IP
# matches the local IP (ie. you're connecting from the same computer), the
# connection is considered secure and plaintext authentication is allowed.
#disable_plaintext_auth = yes
編集後
# Disable LOGIN command and all other plaintext authentications unless
# SSL/TLS is used (LOGINDISABLED capability). Note that if the remote IP
# matches the local IP (ie. you're connecting from the same computer), the
# connection is considered secure and plaintext authentication is allowed.
#disable_plaintext_auth = yes
disable_plaintext_auth = no
とします。

SSL強制の無効化

当初のDovcot2 では設定の必要がありませんでしたが、新しいバージョンのDovecot2 では、SSL強制設定が有効になっているため、無効化する必要があります。この設定をしないと、10-auth.conf で設定した disable_plaintext_auth = no の設定が有効になりません。詳細についてはこちらの記事も参照してください。

/etc/dovecot/conf.d/10-ssl.conf ファイルを編集します。
編集前
# SSL/TLS support: yes, no, required. <doc/wiki/SSL.txt>
# disable plain pop3 and imap, allowed are only pop3+TLS, pop3s, imap+TLS and imaps
# plain imap and pop3 are still allowed for local connections
ssl = required
編集後
# SSL/TLS support: yes, no, required. <doc/wiki/SSL.txt>
# disable plain pop3 and imap, allowed are only pop3+TLS, pop3s, imap+TLS and imaps
# plain imap and pop3 are still allowed for local connections
ssl = yes
"ssl = required" を "ssl = yes" に変更します。この設定によりsslが必須でなくなります。

/etc/dovecot/conf.d/10-master.conf の編集

変更前
service auth {
  # auth_socket_path points to this userdb socket by default. It's typically
  # used by dovecot-lda, doveadm, possibly imap process, etc. Users that have
  # full permissions to this socket are able to get a list of all usernames and
  # get the results of everyone's userdb lookups.
  #
  # The default 0666 mode allows anyone to connect to the socket, but the
  # userdb lookups will succeed only if the userdb returns an "uid" field that
  # matches the caller process's UID. Also if caller's uid or gid matches the
  # socket's uid or gid the lookup succeeds. Anything else causes a failure.
  #
  # To give the caller full permissions to lookup all users, set the mode to
  # something else than 0666 and Dovecot lets the kernel enforce the
  # permissions (e.g. 0777 allows everyone full permissions).
  unix_listener auth-userdb {
    #mode = 0666
    #user =
    #group =
  }

  # Postfix smtp-auth
  #unix_listener /var/spool/postfix/private/auth {
  #  mode = 0666
  #}

  # Auth process is run as this user.
  #user = $default_internal_user
}
変更後
service auth {
  # auth_socket_path points to this userdb socket by default. It's typically
  # used by dovecot-lda, doveadm, possibly imap process, etc. Users that have
  # full permissions to this socket are able to get a list of all usernames and
  # get the results of everyone's userdb lookups.
  #
  # The default 0666 mode allows anyone to connect to the socket, but the
  # userdb lookups will succeed only if the userdb returns an "uid" field that
  # matches the caller process's UID. Also if caller's uid or gid matches the
  # socket's uid or gid the lookup succeeds. Anything else causes a failure.
  #
  # To give the caller full permissions to lookup all users, set the mode to
  # something else than 0666 and Dovecot lets the kernel enforce the
  # permissions (e.g. 0777 allows everyone full permissions).
  unix_listener auth-userdb {
    #mode = 0666
    #user =
    #group =
  }

  unix_listener auth-master {
    mode = 0600
    user = vmail
  }

  # Postfix smtp-auth
  unix_listener /var/spool/postfix/private/auth {
    mode = 0666
    user = postfix
    group = postfix
  }

  # Auth process is run as this user.
  #user = $default_internal_user
}
unix_listener auth-master の部分を追記します。 unix_listener /var/spool/postfix/private/auth の部分も追記します。

/etc/dovecot/conf.d/10-mail.conf の編集

maildir の明示的指定

デフォルトの状態では mbox 形式と認識して動作してしまうため、 mail_location に追記します。
変更前
# See doc/wiki/Variables.txt for full list. Some examples:
#
#   mail_location = maildir:~/Maildir
#   mail_location = mbox:~/mail:INBOX=/var/mail/%u
#   mail_location = mbox:/var/mail/%d/%1n/%n:INDEX=/var/indexes/%d/%1n/%n
#
# <doc/wiki/MailLocation.txt>
#
#mail_location =
変更後
# See doc/wiki/Variables.txt for full list. Some examples:
#
#   mail_location = maildir:~/Maildir
#   mail_location = mbox:~/mail:INBOX=/var/mail/%u
#   mail_location = mbox:/var/mail/%d/%1n/%n:INDEX=/var/indexes/%d/%1n/%n
#
# <doc/wiki/MailLocation.txt>
#
#mail_location =
mail_location = maildir:/home/vmail/%d/%n
mail_location = maildir:/home/vmail/%d/%n
を追記します。"/home/vmail/%d/%n" のパスは、/etc/dovecot/conf.d/auth-sql.conf.ext の userdb に記述した
userdb {
  driver = static
  args = uid=5000 gid=5000 home=/home/vmail/%d/%n allow_all_users=yes
}
"/home/vmail/%d/%n" の部分と一致するようにします。

サービスの起動

以上で設定は完了です。サービスを起動します。
systemctl enable postfix
systemctl start postfix
systemctl enable dovecot
systemctl start dovecot
コマンドを実行します。

テスト

SMTP起動の確認

telnet を利用してローカルマシンからSMTPが動作しているか確認します。
telnet localhost 25
コマンドを実行します。
[root@MailServer-CentOS-72 ipentec.net]# telnet localhost 25
Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 MailServer-CentOS-72.localdomain ESMTP Postfix
EHLO localhost
コマンドを入力します。

下記の結果が返ります。
EHLO localhost
250-MailServer-CentOS-72.localdomain
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
QUIT
コマンドを入力して終了します。

SMTP サブミッションポート起動の確認

SMTPサブミッションポートが動作しているか確認します。
telnet を利用してローカルマシンからSMTP サブミッションポートが動作しているか確認します。
telnet localhost 587
コマンドを実行します。
[root@MailServer-CentOS-72 ipentec.net]# telnet localhost 587
Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 MailServer-CentOS-72.localdomain ESMTP Postfix
EHLO localhost
コマンドを入力します。

下記の結果が返ります。
EHLO localhost
250-MailServer-CentOS-72.localdomain
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-AUTH PLAIN
250-AUTH=PLAIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
SMTP-Auth が利用できること、PLAIN認証が利用できることなどを確認します。
QUIT
コマンドを入力して終了します。

テストユーザーの作成

テスト用のユーザーを作成します。
mysql -u root -p でSQLコンソールを表示します。
下記のSQLコマンドを実行しテストユーザーを作成します。
use mail;
insert into domains (domain) values ('[受信するドメイン]');
insert into users (email,password) values('[作成するメールアドレス]', ENCRYPT('[パスワード]'));
コマンド実行後、
quit;
または
exit;
コマンドで抜けます。
コマンド例
use mail;
insert into domains (domain) values ('ipentec.net');
insert into users (email,password) values('test@ipentec.net', ENCRYPT('mypassword'));

自分自身にメール送信

メーラーなどを利用して、テストユーザーのアカウントで、自分自身にメールを送信して受け取れるか確認します。

テストメールの送信

メールをテストユーザーに向けて送信します。

運用アカウントの追加

mysql -u root -p でSQLコンソールを表示します。
下記のSQLを実行し、メールユーザーを追加します。

ドメインの追加

use mail;
insert into domains (domain) values ('[受信するドメイン]');

メールユーザーの追加

use mail;
insert into users (email,password) values('[作成するメールアドレス]', ENCRYPT('[パスワード]'));

コマンド実行例

USE mail;
INSERT INTO domains (domain) VALUES ('ipentec.net');
INSERT INTO users (email, password) VALUES ('user-01@ipentec.net', ENCRYPT('password'));
INSERT INTO users (email, password) VALUES ('user-02@ipentec.net', ENCRYPT('password'));
INSERT INTO users (email, password) VALUES ('user-03@ipentec.net', ENCRYPT('password'));
INSERT INTO users (email, password) VALUES ('user-04@ipentec.net', ENCRYPT('password'));
INSERT INTO users (email, password) VALUES ('user-05@ipentec.net', ENCRYPT('password'));
quit

運用アカウントの削除

mysql -u root -p でSQLコンソールを表示します。
下記のSQLを実行し、メールユーザーを追加します。

メールユーザーの削除

use mail;
delete from users where email='[削除するメールアドレス]';

コマンド実行例

USE mail;
delete from users where email='user-01@ipentec.net';

著者
かつて日本を代表したスーパーエンジニア (自称)
ハードウェア、電気設備、ガーデニング関係の記事を担当。
最終更新日: 2024-01-06
作成日: 2016-09-03
iPentec all rights reserverd.