1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

社内メール環境 postfix+dovecot

Last updated at Posted at 2020-02-24

初めに

なかなか、シンプルなpostfix+dovecotの設定手順はない。
非常に単純にローカルLANでメールを使いたいだけなのだが、sslやら認証やらがようわからん。
次の構成となっている。

  • ユーザはvirtual_mailboxなんで簡単にユーザを追加できる。
  • とりあえず、メーラについては今はoutlookは使えないがthunderbirdが使える。

postfixとdovecotについて

利用したのは

  • ubuntu 18.04.4
  • dovecot 2.2.32
  • postfix 3.3.0

postfixとdovecot

postfixはメールを作成したものをmailboxに格納し、dovecotはpopやimapでそれを取り出すという仕組みになっている。
一般的にはunixではユーザがmailboxを持っているが、この方式だといちいちuseraddでpasswordを管理することになる。この方式だと、メールアドレスだけ一気に作成するにはめんどくさいし、へたすればsshでサーバに各メールユーザが入れるということになる。単純にメールだけを追加したいのであれば、virtual_mailboxという仕組みのを使うのが簡単である。

postfixの設定

/etc/posyfix/main.cf

virtual_mailbox以外の設定は以下のようなものである。
ここで重要なのはmydestinationになにも設定していないということだ。
もちろん、通常のメール(unix userを持っている人のメール)も受け付けるというのであれば#にしたmydestinationを有効化すればよい。ただ、virtual_mailboxは全く別のmail_domainを持っているのでそこと混同しないことが重要である。
なお、mynetworks の192.168.0.0/16は192.168.x.xのLANからのメーラ(thundebird)からアクセスする場合を想定しているのでlanの設定によって変更が必要だ。

myhostname = ubuntu.example.com
mydomain = example.com
# alias_maps = hash:/etc/aliases
# alias_database = hash:/etc/aliases
myorigin = $mydomain
# mydestination = $myorigin, localhost, $myhostname
mydestination = 
relayhost = 
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 192.168.0.0/16
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
default_transport = error
relay_transport = error
inet_protocols = all

以下が、virtual_mailboxの設定である。


virtual_mailbox_domains = mysite.org
virtual_mailbox_base = /var/spool/virtualm
virtual_mailbox_maps = hash:/etc/postfix/vmailbox
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000

ここで、mailbox_baseは/var/spool/vairtualmを作成して作っており
そこを操作するのはuid=5000,gid=5000ということにしている。

他のディレクトリ等の作成

https://knowledge4linux.blogspot.com/2013/08/dovecot-passwd-file.html
にあるように

groupadd -g 5000 vmail
useradd -g 5000 -u 5000 -s /user/sbin/nologin vmail
mkdir /var/spool/virtualm
chown vmail:vmail /var/spool/virtualm
chmod 700 /var/spool/virtualm

ということでvmailというユーザを作成し、/var/spool/virtualmを作成ということになる。

さて、virtual_mailbox_maps = hash:/etc/postfix/vmailbox
であるが、/etc/postfix/vmailboxに次のような内容を格納する。


test1@mysite.org MailDir/test1@mysite.org/
test2@mysite.org MailDir/test2@mysite.org/

最初は追加したいメールアドレスであり、次は/var/spool/vairtualmの下のディレクトリ
構成である。できあがったあとで運用しているの内容をtreeで見てみるとこんなことになっている。

/var/spool/virtualm/
├── MailDir
│   ├── test1@mysite.org
│   │   ├── cur
│   │   │   └── 1582449895.V801Ia7428M764347.ubuntu:2,S
│   │   ├── dovecot-uidlist
│   │   ├── dovecot-uidvalidity
│   │   ├── dovecot-uidvalidity.5e523407
│   │   ├── dovecot.index.cache
│   │   ├── dovecot.index.log
│   │   ├── new
│   │   └── tmp

この/etc/postfix/vmailboxを作成したら
postmap /etc/postfix/vmailbox
っで設定する。この構成はdovecotでの定義と合わせる必要がある。

ユーザを作成する時のテンプレートの設定?


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

dovecotの設定

設定の概要

sslをnoにし、認証をpwファイルだけで簡単に設定する。

/etc/dovecot/dovecot.conf

以下を変更追加。その下も追加変更部分だけを記述してある。


auth_verbose = yes #追加
auth_debug = yes #追加
mail_debug = yes #追加
listen = * #変更

/etc/dovecot/conf.d/10-auth.conf


disable_plaintext_auth = no
auth_mechanisms = cram-md5 plain login
!include auth-passwdfile.conf.ext
!include auth-static.conf.ext

/etc/dovecot/conf.d/10-mail.conf


mail_location = maildir:/var/spool/virtualm/MailDir/%u

/etc/dovecot/conf.d/10-ssl.conf

'ssl=no'

/etc/dovecot/conf.d/auth-passwdfile.conf.ext


passdb {
  driver = passwd-file
  args = scheme=CRAM-MD5 username_format=%u /etc/dovecot/passwd
}
userdb {
  driver = passwd-file
  args = /etc/dovecot/passwd
}

/etc/dovecot/conf.d/auth-passwdfile.conf.ext

すべてコメント

最終的なdoveconf -nの出力

ここは上の結果 'doveconf -n'を実行するところだが、途中authも設定し始めて
ちょと変な設定もでているかもしれない。service authあたりはいらないはずである。

# 2.2.33.2 (d6601f4ec): /etc/dovecot/dovecot.conf
# Pigeonhole version 0.4.21 (92477967)
# OS: Linux 4.15.0-76-generic x86_64 Ubuntu 18.04.4 LTS ext4
auth_debug = yes
auth_mechanisms = cram-md5 plain login
auth_verbose = yes
disable_plaintext_auth = no
listen = *
mail_debug = yes
mail_location = maildir:/var/spool/virtualm/MailDir/%u
mail_privileged_group = mail
namespace inbox {
  inbox = yes
  location = 
  mailbox Drafts {
    special_use = \Drafts
  }
  mailbox Junk {
    special_use = \Junk
  }
  mailbox Sent {
    special_use = \Sent
  }
  mailbox "Sent Messages" {
    special_use = \Sent
  }
  mailbox Trash {
    special_use = \Trash
  }
  prefix = 
}
passdb {
  args = scheme=CRAM-MD5 username_format=%u /etc/dovecot/passwd
  driver = passwd-file
}
protocols = " pop3"
service auth-worker {
  user = root
}
service auth {
  unix_listener /var/spool/postfix/private/auth {
    group = postfix
    mode = 0666
    user = postfix
  }
  unix_listener auth-userdb {
    group = vmail
    mode = 0666
    user = vmail
  }
}
service imap-login {
  inet_listener imap {
    port = 143
  }
  inet_listener imaps {
    port = 993
    ssl = yes
  }
}
service pop3-login {
  inet_listener pop3 {
    port = 110
  }
  inet_listener pop3s {
    port = 995
    ssl = yes
  }
}
ssl = no
ssl_cert = </etc/dovecot/private/dovecot.pem
ssl_client_ca_dir = /etc/ssl/certs
ssl_key =  # hidden, use -P to show it
userdb {
  args = /etc/dovecot/passwd
  driver = passwd-file
}

/etc/dovecot/passwd

これで動いた、{plain}の次は生のパスワードである。まあ、セキュリティ的には
最悪かもしれないが、これが言いたくて、ここまで書いた。


test1@mysite.org:{plain}test1:5000:5000
test2@mysite.org:{plain}test2:5000:5000

一般のwebの参考文章ではdoveadm pwで{CRAM-MD5}のハッシュを作成し、コピーしろとかなっているが、単に上の設定を入れておけば大丈夫である。ということは管理者にはパスワードがバレバレなので、メールを利用するメンバーは「メール間の秘密は守られない」という前提である。まあ、社内で使うには問題ないのではと思う。

ということで、このファイルと/etc/postfix/vmailboxをメンテしていけばどんどんメンバーを追加できるということである。ああ、よかったよかった。しかしここまでたどり着くのは長かった。

1
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?