0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

virtual_mail_box 平文認証 構築

Last updated at Posted at 2025-05-22

postfix

◆基本設定

/etc/postfix/main.cf
myhostname = mail.example123.local
mydomain = example123.local
#virtual_mailbox_domainsに設定する1ドメインを指定。
myorigin = virtual.local
inet_interfaces = all
inet_protocols = ipv4
#空にする
mydestination =
mynetworks = 192.168.10.0/24, 127.0.0.0/8
virtual_mailbox_domains = virtual.local
virtual_mailbox_base = /var/mail/vhosts
virtual_mailbox_maps = hash:/etc/postfix/vmailbox
#virtual_mailbox用のLinuxユーザのID設定
virtual_uid_maps = static:150
virtual_gid_maps = static:150
#virtual_mailbox_domains へのメールは、Postfix内部のvirtual配送エージェントで処理するよう設定
virtual_transport = virtual
virtual_minimum_uid = 150
virtual_mailbox_limit = 51200000
#sasl認証設定
smtpd_sasl_type = dovecot
#unixソケット/var/spool/postfix/private/authで認証
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
#smtp受信設定
smtpd_recipient_restrictions =
    permit_sasl_authenticated,
    permit_mynetworks,
    reject_unauth_destination
/etc/postfix/master.cf
smtp      inet  n       -       n       -       -       smtpd
virtual   unix  -       n       n       -       -       virtual

◆仮想メールユーザ用のlinuxユーザを作成

groupadd -g 150 vmail
useradd -r -u 150 -g vmail -s /sbin/nologin vmail

◆仮想メールのベースディレクトリを作成

(自動でメールボックスは作られる)

mkdir -p /var/mail/vhosts/
chown -R vmail:vmail /var/mail/vhosts
chmod -R 700 /var/mail/vhosts

◆ユーザ作成

  • virtual_mailbox_mapsで指定したファイルにユーザを記載する。
    • pathの最後に/をつける。
    • 相対パスにする。
/etc/postfix/vmailbox
user1@virtual.local virtual.local/user1/
user2@virtual.local virtual.local/user1/
postmap /etc/postfix/vmailbox

◆確認

postconf -n
postconf -d
postfix check

◆反映

systemctl reload postfix

Dovecot

◆Dovecot設定ファイルで使用される変数

%d = ドメイン名
%n = ローカルパート(例:user1@virtual.local の user1)
%u = メールアドレス

◆基本設定

/etc/dovecot/dovecot.conf
#protocols = submissionは削除。postfixに干渉するため。
protocols = imap pop3
listen = *
!include conf.d/*.conf
!include_try local.conf
#必要に応じてデバッグモードにする。
mail_debug = yes
/etc/dovecot/conf.d/10-mail.conf
#Dovecot がアクセスを許可するユーザーID(UID)の範囲。
first_valid_uid = 150
last_valid_uid = 150
#postfixのmain.cfのvirtual_mailbox_mapsで指定するファイル記載のパスに合わせる。
mail_location = maildir:/var/mail/vhosts/%d/%n
mail_uid = vmail
mail_gid = vmail
/etc/dovecot/conf.d/10-auth.conf
#暗号化されていない接続(非TLS)でも、プレーンテキスト認証(ユーザー名とパスワードをそのまま送る方式)を許可する
disable_plaintext_auth = yes
#ログインユーザー名(= メールアドレス全体)を小文字に変換したもの
auth_username_format = %Lu
auth_mechanisms = plain login
!include auth-passwdfile.conf.ext
  • 今回は、「ファイルベースの仮想ユーザー認証」を行うため、「!include auth-system.conf.ext」をコメントアウトする。
    • auth-passwdfile.conf.ext:ファイルベースの仮想ユーザー認証(例:/etc/dovecot/users)
    • auth-system.conf.ext:UNIXユーザー認証(PAMや /etc/passwd)など、システムユーザーを使った認証
/etc/dovecot/conf.d/auth-passwdfile.conf.ext
passdb {
  driver = passwd-file
  args = /etc/dovecot/users
}

userdb {
  driver = static
  #postfixのmain.cfのvirtual_mailbox_mapsで指定するファイル記載のパスに合わせる。
  args = uid=vmail gid=vmail home=/var/mail/vhosts/%d/%n
}
/etc/dovecot/conf.d/10-master.conf
service pop3-login {
  inet_listener pop3 {
    port = 110
  }
  inet_listener pop3s {
    #port = 995
    #ssl = yes
  }
}

service auth {
#postfixのmain.cfのsmtpd_sasl_pathに合わせる
  unix_listener /var/spool/postfix/private/auth {
    mode = 0660
    user = postfix
    group = postfix
  }
}
/etc/dovecot/conf.d/10-ssl.conf
ssl = no
ssl_cert = </etc/pki/tls/certs/postfix.pem
ssl_key = </etc/pki/tls/certs/postfix.pem

◆鍵作成

cd /etc/pki/tls/certs/
openssl genrsa 2048 > server.key
openssl req -new -key server.key > server.csr
openssl x509 -in server.csr -days 365000 -req -signkey server.key > server.crt
cat server.key > postfix.pem
cat server.crt >> postfix.pem

◆ユーザ作成

doveadm pw -s SHA1 >> /etc/dovecot/users
sed -Ei 's/^\{SHA1\}/user1@virtual.local:{SHA1}/' /etc/dovecot/users
/etc/dovecot/users
user1@virtual.local:{SHA1}password_hash

◆ログイン確認

doveadm auth test user1@virtual.local 'password'

◆確認

dovecot -n

◆thunderbird

■受信
プロトコル: pop3
ホスト名: mail.example123.local
ポート番号: 110
接続の保護: なし
認証方式: 通常のパスワード認証
ユーザ名: user1@virtual.local

■送信
ホスト名: mail.example123.local
ポート番号: 25
接続の保護: なし
認証方式: 通常のパスワード認証
ユーザ名: user1@virtual.local

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?