2017年5月23日火曜日

CentOS7(Raspberry Pi 3)でroot宛メールをGmailに転送する

ふとした時にroot宛にメールが飛ぶことがあるけど、
気付くのが遅れたりすることが多いので、
よく見るGmailで確認できたら楽かなと思って、今回対応してみる。


■本日の目標

 Raspberry Pi 3からroot宛メールをGmailに転送する


■主な作業環境

・HW:Raspberry Pi 3 Model B
・OS:CentOS Linux release 7.3.1611 (Core)


■Postfixのインストール

・CentOS7からはデフォルトMTAとしてPostfixがインストール済で、
 先日、初期エラーの対策をして正常稼働できるようにしたので、
 ここでは特に作業なし。
 一応、yumの情報を確認しておく。
$ yum info postfix
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
インストール済みパッケージ
名前                : postfix
アーキテクチャー    : armv7hl
エポック            : 2
バージョン          : 2.10.1
リリース            : 6.el7
容量                : 11 M
リポジトリー        : installed
提供元リポジトリー  : centos-base_rbf
要約                : Postfix Mail Transport Agent
URL                 : http://www.postfix.org
ライセンス          : IBM and GPLv2+
説明                : Postfix is a Mail Transport Agent (MTA), supporting LDAP, SMTP AUTH (SASL),
                    : TLS


■mailxのインストール

・mailコマンドでメールが送れるようにmailxをインストールする。
$ yum info mailx
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
利用可能なパッケージ
名前                : mailx
アーキテクチャー    : armv7hl
バージョン          : 12.5
リリース            : 12.el7
容量                : 219 k
リポジトリー        : base/7/armhfp
要約                : Enhanced implementation of the mailx command
URL                 : http://heirloom.sourceforge.net/mailx.html
ライセンス          : BSD with advertising and MPLv1.1
説明                : Mailx is an enhanced mail command, which provides the functionality
                    : of the POSIX mailx command, as well as SysV mail and Berkeley Mail
                    : (from which it is derived).
                    :
                    : Additionally to the POSIX features, mailx can work with Maildir/ e-mail
                    : storage format (as well as mailboxes), supports IMAP, POP3 and SMTP
                    : protocols (including over SSL) to operate with remote hosts, handles mime
                    : types and different charsets. There are a lot of other useful features,
                    : see mailx(1).
                    :
                    : And as its ancient analogues, mailx can be used as a mail script language,
                    : both for sending and receiving mail.
                    :
                    : Besides the "mailx" command, this package provides "mail" and "Mail"
                    : (which should be compatible with its predecessors from the mailx-8.x source),
                    : as well as "nail" (the initial name of this project).
$ yum install mailx
・・・(中略)・・・
完了しました!


■Postfixの設定

・「/etc/postfix/main.cf」にrelayhostとSMTP認証の設定を書く。
$ vi /etc/postfix/main.cf
# relayhost(relayhostの項目末尾に追加)
relayhost = [smtp.gmail.com]:587
# SMTP認証(ファイル末尾に追加)
smtp_sasl_auth_enable=yes
smtp_sasl_password_maps = hash:/etc/postfix/gmail_auth
smtp_sasl_security_options =
smtp_sasl_tls_security_options = noanonymous
smtp_sasl_mechanism_filter = plain
smtp_tls_eccert_file =
smtp_tls_eckey_file =
smtp_use_tls = yes
smtp_enforce_tls = no
smtp_tls_CAfile = /etc/pki/tls/certs/ca-bundle.crt
smtpd_tls_received_header = yes
tls_random_source = dev:/dev/urandom


■SMTP認証情報を作成

・先ほど追記した「/etc/postfix/main.cf」の『smtp_sasl_password_maps』の項に書いた認証ファイルを作成し、利用するGmailの情報を記載する。
$ echo '[smtp.gmail.com]:587 xxxxx@gmail.com:password' > /etc/postfix/gmail_auth

・認証ファイルをハッシュ化する。
$ chmod 600 /etc/postfix/gmail_auth
$ postmap /etc/postfix/gmail_auth
$ ll /etc/postfix/gmail*
-rw-------. 1 root root    52  5月 14 21:32 /etc/postfix/gmail_auth
-rw-------. 1 root root 12288  5月 14 21:34 /etc/postfix/gmail_auth.db

・Postfixに反映する。
$ systemctl restart postfix
$ systemctl status postfix
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: active (running) since 日 2017-05-14 22:48:10 JST; 5s ago
  Process: 2990 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS)
  Process: 3007 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
  Process: 3004 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
  Process: 3001 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
 Main PID: 3082 (master)
   CGroup: /system.slice/postfix.service
           tq3082 /usr/libexec/postfix/master -w
           tq3083 pickup -l -t unix -u
           mq3084 qmgr -l -t unix -u

 5月 14 22:48:07 WSTD-RPI3 systemd[1]: Starting Postfix Mail Transport Agent...
 5月 14 22:48:10 WSTD-RPI3 postfix/postfix-script[3080]: starting the Postfix mail system
 5月 14 22:48:10 WSTD-RPI3 postfix/master[3082]: daemon started -- version 2.10.1, configuration /etc/postfix
 5月 14 22:48:10 WSTD-RPI3 systemd[1]: Started Postfix Mail Transport Agent.


■root宛メールの転送設定

・転送先のGmailアドレスを設定する。
$ echo 'root: xxxxx@gmail.com' >> /etc/aliases
$ newaliases


■送信テスト(その1)

・設定ができたのでテスト送信をしてみよう。
$ echo 'Test Send' | mail root

 ・・・Gmailに来ないな・・・ちょっとmaillogを見てみよう。
$ tail -n 20 /var/log/maillog
・・・(中略)・・・
May 14 22:49:05 WSTD-RPI3 postfix/pickup[3083]: 232E04677: uid=0 from=
May 14 22:49:05 WSTD-RPI3 postfix/cleanup[3090]: 232E04677: message-id=<20170514134905 .232e04677="" localdomain="">
May 14 22:49:05 WSTD-RPI3 postfix/qmgr[3084]: 232E04677: from=, size=438, nrcpt=1 (queue active)
May 14 22:49:05 WSTD-RPI3 postfix/cleanup[3090]: 3E4C84675: message-id=<20170514134905 .232e04677="" localdomain="">
May 14 22:49:05 WSTD-RPI3 postfix/local[3092]: 232E04677: to=, orig_to=, relay=local, delay=0.19, delays=0.12/0.06/0/0.01, dsn=2.0.0, status=sent (forwarded as 3E4C84675)
May 14 22:49:05 WSTD-RPI3 postfix/qmgr[3084]: 3E4C84675: from=, size=580, nrcpt=1 (queue active)
May 14 22:49:05 WSTD-RPI3 postfix/qmgr[3084]: 232E04677: removed
May 14 22:49:06 WSTD-RPI3 postfix/smtp[3093]: warning: SASL authentication failure: No worthy mechs found
May 14 22:49:06 WSTD-RPI3 postfix/smtp[3093]: 3E4C84675: SASL authentication failed; cannot authenticate to server smtp.gmail.com[108.177.97.108]: no mechanism available
May 14 22:49:07 WSTD-RPI3 postfix/smtp[3093]: connect to smtp.gmail.com[2404:6800:4008:c00::6d]:587: Cannot assign requested address
May 14 22:49:07 WSTD-RPI3 postfix/smtp[3093]: 3E4C84675: to=, orig_to=, relay=none, delay=2.2, delays=0.01/0.21/2/0, dsn=4.4.1, status=deferred (connect to smtp.gmail.com[2404:6800:4008:c00::6d]:587: Cannot assign requested address)


■(対策1)cyrus-sasl-plainのインストール

・「SASL authentication failure」の出てる行を読むと、何やらSASLの認証に必要な機能が無いようなので追加インストールする。
$ yum info cyrus-sasl-plain
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
利用可能なパッケージ
名前                : cyrus-sasl-plain
アーキテクチャー    : armv7hl
バージョン          : 2.1.26
リリース            : 20.el7
容量                : 37 k
リポジトリー        : base/7/armhfp
要約                : PLAIN and LOGIN authentication support for Cyrus SASL
URL                 : http://asg.web.cmu.edu/sasl/sasl-library.html
ライセンス          : BSD with advertising
説明                : The cyrus-sasl-plain package contains the Cyrus SASL plugins which support
                    : PLAIN and LOGIN authentication schemes.
$ yum install cyrus-sasl-plain
・・・(中略)・・・
完了しました!


■送信テスト(その2)

・「cyrus-sasl-plain」がインストールできたのでもう一度テスト送信をしてみよう。
$ echo 'Test Send' | mail root

 まだ届かないので、またmailogを確認する。
$ tail -n 20 /var/log/maillog
・・・(中略)・・・
May 14 22:58:52 WSTD-RPI3 postfix/pickup[3083]: 986CC4682: uid=0 from=
May 14 22:58:52 WSTD-RPI3 postfix/cleanup[3114]: A830F127: message-id=<20170514135852 .986cc4682="" localdomain="">
May 14 22:58:52 WSTD-RPI3 postfix/qmgr[3084]: A830F127: from=, size=579, nrcpt=1 (queue active)
May 14 22:58:52 WSTD-RPI3 postfix/local[3115]: 986CC4682: to=, orig_to=, relay=local, delay=0.13, delays=0.07/0.05/0/0.02, dsn=2.0.0, status=sent (forwarded as A830F127)
May 14 22:58:52 WSTD-RPI3 postfix/qmgr[3084]: 986CC4682: removed
May 14 22:58:55 WSTD-RPI3 postfix/smtp[3108]: A830F127: SASL authentication failed; server smtp.gmail.com[108.177.97.109] said: 534-5.7.14  Please log in via your web browser and?534-5.7.14 then try again.?534-5.7.14  Learn more at?534 5.7.14  https://support.google.com/mail/answer/78754 t5sm14848004pgo.48 - gsmtp
May 14 22:58:55 WSTD-RPI3 postfix/smtp[3108]: connect to smtp.gmail.com[2404:6800:4008:c00::6d]:587: Cannot assign requested address
May 14 22:58:55 WSTD-RPI3 postfix/smtp[3108]: A830F127: to=, orig_to=, relay=none, delay=3.2, delays=0.01/0/3.2/0, dsn=4.4.1, status=deferred (connect to smtp.gmail.com[2404:6800:4008:c00::6d]:587: Cannot assign requested address)


■(対策2)Googleアカウントのセキュリティ設定を変更

・ログ中の「https://support.google.com/mail/answer/78754」にアクセスするとヒントが書かれていた。
 パスワードは問題無さそうなので、「安全性の低いアプリがアカウントにアクセスすることを許可」の対策を行う。
 ⇒ページ内の方法2を実施
  1.[アカウント情報] の [安全性の低いアプリ] に移動
  2.[安全性の低いアプリのアクセス] の横にある [有効にする] を選択


■送信テスト(その3)

・三度目の正直ということで・・・。
$ echo 'Test Send' | mail root

 Gmailに届いた!

 念のため、mailogを確認してみよう。
$ tail -n 20 /var/log/maillog
・・・(中略)・・・
May 14 23:12:21 WSTD-RPI3 postfix/smtp[3148]: connect to smtp.gmail.com[2404:6800:4008:c00::6d]:587: Cannot assign requested address
May 14 23:12:24 WSTD-RPI3 postfix/smtp[3148]: 1F83F127: to=, orig_to=, relay=smtp.gmail.com[108.177.97.108]:587, delay=2.9, delays=0.01/0.16/1.2/1.5, dsn=2.0.0, status=sent (250 2.0.0 OK 1494771143 134sm13394508pgh.43 - gsmtp)
May 14 23:12:24 WSTD-RPI3 postfix/qmgr[3084]: 1F83F127: removed

 うーむ・・・「Cannot assign requested address」の行が気になるな。


■「/etc/postfix/main.cf」の追加設定(IPv6無効化)

・「Cannot assign requested address」のちょっと前の出てるアドレスってIPv6か・・・。
 さっきviで編集してるときmain.cfで使用IPアドレスのバージョン設定の箇所があったのが気になったんだよね。
 「inet_protocols」の項目。
 このRaspberry Pi 3の端末はIPv6は無効にしてあるから、「そんなアドレスに繋げられないよ」と怒られているんだろう。
 ・・・というわけで、IPv4固定に変更しよう。
$ vi /etc/postfix/main.cf
# inet_protocolsをIPv4指定に変更
inet_protocols = ipv4

 設定内容を反映する。
$ systemctl reload postfix
$ systemctl status postfix
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: active (running) since 日 2017-05-14 23:23:41 JST; 14min ago
  Process: 750 ExecReload=/usr/sbin/postfix reload (code=exited, status=0/SUCCESS)
  Process: 592 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
  Process: 578 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
  Process: 552 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
 Main PID: 696 (master)
   CGroup: /system.slice/postfix.service
           tq696 /usr/libexec/postfix/master -w
           tq762 pickup -l -t unix -u
           mq763 qmgr -l -t unix -u

 1月 01 09:00:16 WSTD-RPI3 systemd[1]: Starting Postfix Mail Transport Agent...
 5月 14 23:23:41 WSTD-RPI3 postfix/master[696]: daemon started -- version 2.10.1, configuration /etc/postfix
 5月 14 23:23:41 WSTD-RPI3 systemd[1]: Started Postfix Mail Transport Agent.
 5月 14 23:37:41 WSTD-RPI3 postfix/master[696]: reload -- version 2.10.1, configuration /etc/postfix
 5月 14 23:37:41 WSTD-RPI3 postfix/master[696]: warning: ignoring inet_protocols parameter value change
 5月 14 23:37:41 WSTD-RPI3 postfix/master[696]: warning: old value: "all", new value: "ipv4"
 5月 14 23:37:41 WSTD-RPI3 systemd[1]: Reloaded Postfix Mail Transport Agent.
 5月 14 23:37:41 WSTD-RPI3 postfix/master[696]: warning: to change inet_protocols, stop and start Postfix


■送信テスト(その4)

・最後にもう一度。
$ echo 'Test Send' | mail root

 念のため、mailogを確認してみよう。
$ tail -n 20 /var/log/maillog
・・・(中略)・・・
May 14 23:39:56 WSTD-RPI3 postfix/smtp[776]: 5EC25144: to=, orig_to=, relay=smtp.gmail.com[108.177.97.109]:587, delay=3.9, delays=0.01/0.26/1.7/1.9, dsn=2.0.0, status=sent (250 2.0.0 OK 1494772796 r13sm15980358pgn.16 - gsmtp)
May 14 23:39:56 WSTD-RPI3 postfix/qmgr[763]: 5EC25144: removed

 「Cannot assign requested address」が消えた。



これで何か問題があっても検知できそうだ。

Previous Post
Next Post

post written by: