dovecotでCRAM-MD5認証を使えるようにする


1年ほど前から自宅サーバpop3/imapサーバをdovecotにしているのだが、
こいつの認証方式が長い間plainになっていたのを今日発見したので
暇を見て暗号化される認証方式にチェンジすることにした。
ちなみにAPOPでも良かったのだが、qpopperを導入していた時にそっちは試してしまったので
この際だからCRAM-MD5で。……と言っても別段難しい作業はない。


今回はdovecot.conf内でpassdbにpasswd-fileを使うように設定し、
そいつのためのpasswd-fileを作成してやれば設定は完了だ。
また、これに関する情報は全て公式のwikiに載っているので
情報を探し回る必要もない。postfix同様この辺の情報がすっきりしているのは非常に助かる。


まずCRAM-MD5用のpasswdファイルを作成する作業を行う。
公式のWikiによると……

This file is compatible with a normal /etc/passwd file, and a password file used by libpam-pwdfile PAM plugin. It's in the following format:

user:password:uid:gid:(gecos):home:(shell):extra_fields

For a password database it's enough to have only the user and password fields. For a user database, you need to set also uid, gid and preferably also home (see VirtualUsers). (gecos) and (shell) fields are unused by Dovecot.

The password field can be in four formats:

password: Assume CRYPT password scheme.

{SCHEME}password: The password is in the given scheme.

password[13]: libpam-passwd file compatible format for CRYPT scheme.

password[34]: libpam-passwd file compatible format for MD5 scheme.

extra_fields is a space-separated list of key=value pairs which can be used to set various passdb settings and userdb settings. Keys which begin with a userdb_ prefix are used for userdb, others are used for passdb. So for example if you wish to override mail_location setting for one user, use userdb_mail=mbox:~/mail.

Empty lines and lines beginning with '#' character are ignored.

http://wiki.dovecot.org/AuthDatabase/PasswdFile

……と言うことなので、これを踏まえてまずはCRAM-MD5で暗号化されたパスワード文字列を生成する。
dovecotpwというコマンドで暗号化が可能なので……

# dovecotpw -s CRAM-MD5
Enter new password:
Retype new password:
{CRAM-MD5}9bde2235538debe003(適当。実際はもっと長い)

暗号化した文字列を取得する。蛇足だがデフォルトではHMAC-MD5で暗号化を行うらしい。
で、この暗号化した文字列を先ほど引用したサイトの内容を踏まえて……

user:{CRAM-MD5}9bde2235538debe003(適当。実際はもっと長い):1001:1001::/home/user

こんな感じで/usr/local/etc/dvc_passwdとしてエディタで作成。
ちなみに今回はバーチャルユーザではなくUNIXアカウントでCRAM-MD5を使えるようにするため
uid・gid・homeは/etc/passwdの該当アカウントの情報を丸ごと頂くことにした。
これでpasswd-fileの作成は完了。


さて、後は設定ファイルを書き換えて再起動するだけだ。
/usr/local/etc/dovecot.confをエディタで開いて……

auth default {
  # Space separated list of wanted authentication mechanisms:
  #   plain login digest-md5 cram-md5 ntlm rpa apop anonymous gssapi
  # NOTE: See also disable_plaintext_auth setting.
  #mechanisms = plain
  mechanisms = cram-md5 plain

 ...

  # passwd-like file with specified location
  # 
  passdb passwd-file {
    # Path for passwd-file
    args = /usr/local/etc/dvc_passwd
  }

こんな感じに編集してdovecotを再起動。
後はメーラ側の認証方式をCRAM-MD5にして接続を確認、設定作業は完了だ。


こんなに簡単に済むんならもっと早いうちにCRAM-MD5を使うようにすれば良かった……