LoginSignup
1
0
お題は不問!Qiita Engineer Festa 2023で記事投稿!

Azure 上の postfix で submission ポートを有効にして外部から SMTP 接続する

Last updated at Posted at 2023-07-12

今まで設定してきた postfix はサーバに ssh でログインしてテストしていましたが、手元の PC から直接 SMTP 接続を行ってみます。

環境

  • Ubuntu 22.04
  • Postfix

「Azure で postfix のインストールを試す」
https://qiita.com/nanbuwks/items/38b54aaf8c3ecfdbb3cc
「Azure/Ubuntu にインストールした postfix / dovecot を Maildir 対応にする」
https://qiita.com/nanbuwks/items/10e6dd1224ce0c3f76af
「Azure にインストールした postfix に SMTP-Auth の設定を追加する」
https://qiita.com/nanbuwks/items/85e63ec9b129138bfd18

で設定したものです。

submission 接続を有効にする


$ sudo vim /etc/postfix/master.cf

として、冒頭を変更します。

smtp      inet  n       -       y       -       -       smtpd
#smtp      inet  n       -       y       -       1       postscreen
#smtpd     pass  -       -       y       -       -       smtpd
#dnsblog   unix  -       -       y       -       0       dnsblog
#tlsproxy  unix  -       -       y       -       0       tlsproxy
#submission inet n       -       y       -       -       smtpd

smtp      inet  n       -       y       -       -       smtpd
#smtp      inet  n       -       y       -       1       postscreen
#smtpd     pass  -       -       y       -       -       smtpd
#dnsblog   unix  -       -       y       -       0       dnsblog
#tlsproxy  unix  -       -       y       -       0       tlsproxy
submission inet n       -       y       -       -       smtpd

変更を反映します。

$ sudo service postfix restart

ss コマンドで待ち受けしているポートを調べます。

$ ss -atn

587が開いていて、設定が有効になっていることがわかります。

State       Recv-Q    Send-Q       Local Address:Port          Peer Address:Port     Process    
LISTEN      0         100                0.0.0.0:587                0.0.0.0:*                   
LISTEN      0         100                0.0.0.0:110                0.0.0.0:*                   
LISTEN      0         4096         127.0.0.53%lo:53                 0.0.0.0:*                   
LISTEN      0         128                0.0.0.0:22                 0.0.0.0:*                   
LISTEN      0         100                0.0.0.0:25                 0.0.0.0:*                   
LISTEN      0         100                0.0.0.0:995                0.0.0.0:*                   
LAST-ACK    1         1                 10.0.0.4:22          177.185.137.78:50992               
ESTAB       0         36                10.0.0.4:22         182.170.165.119:39064               
LISTEN      0         100                   [::]:587                   [::]:*                   
LISTEN      0         100                   [::]:110                   [::]:*                   
LISTEN      0         511                      *:80                       *:*                   
LISTEN      0         128                   [::]:22                    [::]:*                   
LISTEN      0         100                   [::]:25                    [::]:*                   
LISTEN      0         100                   [::]:995                   [::]:*                   

ローカル接続でポート587に接続してみる

telnet で、587ポートを使ってメールを送ってみます。

$ telnet localhost 587

以下のようにして、ローカルexampleユーザにメールを送ります。

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 test-azure-gen.ugx1l5hl2pbu5oitg4vrqao0jd.phxx.internal.cloudapp.net ESMTP Postfix (Ubuntu)
HELO localhost
250 test-azure-gen.ugx1l5hl2pbu5oitg4vrqao0jd.phxx.internal.cloudapp.net
MAIL FROM:azureuser
250 2.1.0 Ok
RCPT TO:example
250 2.1.5 Ok
DATA
354 End data with <CR><LF>.<CR><LF>
Hello submission.
.
250 2.0.0 Ok: queued as 3A4213F4AB
QUIT
221 2.0.0 Bye
Connection closed by foreign host.

example ユーザにメールが届いているでしょうか?

$ su - example

example ユーザになって mail を読んでみます。

$ mail
"/home/example/Maildir": 5 messages 1 new 4 unread
 U   1 Ubuntu                               10/634   
 U   2 Ubuntu                               10/632   
 U   3 Ubuntu                               10/634   
 U   4 Ubuntu                               10/634   
>N   5 azureuser@test-azu                   11/675   
? 5
Return-Path: <azureuser@test-azure-gen.ugx1l5hl2pbu5oitg4vrqao0jd.phxx.internal.cloudapp.net>
X-Original-To: example
Delivered-To: example@test-azure-gen.ugx1l5hl2pbu5oitg4vrqao0jd.phxx.internal.cloudapp.net
Received: from localhost (localhost [127.0.0.1])
	by test-azure-gen.ugx1l5hl2pbu5oitg4vrqao0jd.phxx.internal.cloudapp.net (Postfix) with SMTP id 3A4213F4AB
	for <example>; Wed, 12 Jul 2023 12:44:04 +0000 (UTC)
Message-Id: <20230712124410.3A4213F4AB@test-azure-gen.ugx1l5hl2pbu5oitg4vrqao0jd.phxx.internal.cloudapp.net>
Date: Wed, 12 Jul 2023 12:44:04 +0000 (UTC)
From: azureuser@test-azure-gen.ugx1l5hl2pbu5oitg4vrqao0jd.phxx.internal.cloudapp.net

Hello submission.
? q
Saved 1 message in /home/example/mbox
Held 4 messages in /home/example/Maildir
$ exit

無事、届いていることが確認できました。

外部からメール送信を受けてみる

先程はローカル接続で、ポート 587 を使い SMTP で接続してみました。この挙動はポート 25 でも同様でした。
ローカルではなく、リモート接続ではどうでしょう?

ACL を操作して TCP 25,587 番ポートでのアクセスを許可する

Azure 画面より、受信ポートに 25 および 587 を加えます。
image.png

telnet でポート 25 に接続してみる

まずポート25で接続してみます。

$ telnet example.com 25
Trying 123.456.789.123...
telnet: Unable to connect to remote host: Connection timed out


接続できませんでした。一般的な ISP 経由でのインターネット接続において、外部サーバへのポート25はブロックされるからです。これが OB25P (Outbound Port 25 Blocking) と呼ばれる SPAM 対策の仕組みです。

また、Azure サーバでは Enterprise 契約でないと ポート25はブロックされるとありますが、
「Troubleshoot outbound SMTP connectivity in Azure」
https://learn.microsoft.com/en-us/azure/virtual-network/troubleshoot-outbound-smtp-connectivity

しかしながら調査してみると、実験を行った 2023/07/12 現在において、上記の ACL を解除するだけで Azure サーバにポート 25 は到達するようで、OB25P ISP を用いないインターネット接続においては問題なく接続ができました。

しかしながら以降は、テストする環境により左右されるポート 25 ではなくポート 587 でテストを進めることにします。

telnet でポート587 に接続してみる

$ telnet example.com 587

とすると、localhost と同じようにローカルユーザ example にメールを送信することができました。

Trying 123.456.789.123...
Connected to example.com.
Escape character is '^]'.
220 test-azure-gen.ugx1l5hl2pbu5oitg4vrqao0jd.phxx.internal.cloudapp.net ESMTP Postfix (Ubuntu)
HELO invalid.com
250 test-azure-gen.ugx1l5hl2pbu5oitg4vrqao0jd.phxx.internal.cloudapp.net
MAIL FROM:azureuser
250 2.1.0 Ok
RCPT TO:example
250 2.1.5 Ok
DATA
354 End data with <CR><LF>.<CR><LF>
Hello incoming hosts.
.
250 2.0.0 Ok: queued as 8D9353F4AB
QUIT
221 2.0.0 Bye
Connection closed by foreign host.

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