SMTP-Auth設定

元は先人たちと同様にCyrus SASLでSMTP Authを行うつもりだった。だが、いまPOP3サーバとして使っているDovecotにもSASLが使えると言うことなので、設定してみる。
参考:Postfix SASL How

実は先にCyrus SASL関係のモジュールやら何やら入れたので、先にdpkg --purgeした。

では。
Dovecot SASLが使えるかどうか確認。

degas@debian:~$ sudo postconf -a
cyrus
dovecot

dovecotが出てるのでOK。
念のためPostfixのバージョンを出しておく。2.3以上ならばDovecot SASLが使える。

degas@debian:~$ dpkg -l | grep postfix
ii  postfix         2.3.8-2+b1   A high-performance mail transport agent

次。
/etc/postfix/main.cfに以下の設定を加える。なお、Postfixキューディレクトリ(queue_directoryで指定)が標準の/var/spool/postfixであると仮定されている。

smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth

smtpd_sasl_typeはデフォルトがcyrus。Dovecotを使いたいので明示。
smtpd_sasl_pathはデフォルトがsmtpdらしい。/var/spool/postfix/private/authが作業場所(?)なので、キューディレクトリからの相対位置で指定されている。
それと共にDovecotの設定ファイルである/etc/dovecot/dovecot.confも変更。
元の設定は

  # It's possible to export the authentication interface to other programs:
  #socket listen {
    #master {
      # Master socket is typically used to give Dovecot's local delivery
      # agent access to userdb so it can find mailbox locations. It can
      # however also be used to disturb regular user authentications.
      # WARNING: Giving untrusted users access to master socket may be a 
      # security risk, don't give too wide permissions to it!
      #path = /var/run/dovecot/auth-master
      #mode = 0600
      # Default user/group is the one who started dovecot-auth (root)
      #user = 
      #group = 
    #}
    #client {
      # The client socket is generally safe to export to everyone. Typical use
      # is to export it to your SMTP server so it can do SMTP AUTH lookups
      # using it.
      #path = /var/run/dovecot/auth-client
      #mode = 0660
    #}
  #}

のところをコメントアウトを外して参考記事のように設定。

  # It's possible to export the authentication interface to other programs:
  socket listen {
    #master {
      # Master socket is typically used to give Dovecot's local delivery
      # agent access to userdb so it can find mailbox locations. It can
      # however also be used to disturb regular user authentications.
      # WARNING: Giving untrusted users access to master socket may be a 
      # security risk, don't give too wide permissions to it!
      #path = /var/run/dovecot/auth-master
      #mode = 0600
      # Default user/group is the one who started dovecot-auth (root)
      #user = 
      #group = 
    #}
    client {
      # The client socket is generally safe to export to everyone. Typical use
      # is to export it to your SMTP server so it can do SMTP AUTH lookups
      # using it.
      path = /var/spool/postfix/private/auth
      mode = 0660
      user = postfix
      group = postfix
    }
  }

user = postfixとgroup = postfixの行は追加。
PostfixDovecotの設定を読み込ませる。

これで設定完了…?
外からポート25をたたいてみる。

$ telnet 192.168.***.* 25
Trying 192.168.***.*...
Connected to 192.168.***.*.
Escape character is '^]'.
220 smtp.debian.localdomain ESMTP Postfix (Debian/GNU)
ehlo debian.localdomain
250-smtp.debian.localdomain
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
(どうしようもないのでEnter)
500 5.5.2 Error: bad syntax
quit
221 2.0.0 Bye
Connection closed by foreign host.

どうもSMTP-Authは動いて無さそう。
まだ確認しなきゃ。


何気なく現状の/etc/postfix/main.cfを見直す。

# See /usr/share/postfix/main.cf.dist for a commented, more complete version


# Debian specific:  Specifying a file name will cause the first
# line of that file to be used as the name.  The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname

smtpd_banner = $myhostname ESMTP $mail_name (Debian/GNU)
biff = no

# appending .domain is the MUA's job.
append_dot_mydomain = no

# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h

# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${queue_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${queue_directory}/smtp_scache

# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.

alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = $mydomain
mynetworks = 192.168.***.0/24, 127.0.0.0/8
mailbox_size_limit = 0
recipient_delimiter = +
home_mailbox = Maildir/
mail_spool_directory = 
relayhost = ないしょ
myhostname = ないしょ
mydestination = $myhostname, localhost.$mydomain, $mydomain
mydomain = ないしょ
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth

おや。そう言えばTLS関係の記載がある。
さっきポート25をたたいたときにも

250-STARTTLS

とか言ってきたもんな。いまはSMTP-Authだけが目的なのでこれは外そう。

Webminのサーバ > Postfix の設定 > SMTP Authentication And Encryption
を見ると、「Enable SASL SMTP authentication?」が「いいえ」で「Enable TLS encryption?」が「はい」になっとる。望んでいるのは前者がはいで後者がいいえ。逆ですな。なので逆にして「保存して適用」ボタンを押す。
すると、/etc/postfix/main.cfが

# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_tls_session_cache_database = btree:${queue_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${queue_directory}/smtp_scache
:
smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions = permit_mynetworks reject_unauth_destination

となった。

さてまたポート25をたたいてみる。

220 smtp.debian.localdomain ESMTP Postfix (Debian/GNU)
ehlo debian.localdomain
250-debian.localdomain
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-AUTH PLAIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
AUTH PLAIN ************************
235 2.0.0 Authentication successful
QUIT
221 2.0.0 Bye

おお、ちゃんとAuthentication successfulと出ました!
設定は全く難しくないですな。