#author("2023-10-05T17:23:28+09:00","default:kuji","kuji") CONTENTS #contents ---- Lastmodified &lastmod; ---- *SMTP認証 [#sa8edd4a] 【参考URL】[[FreeBSD 12 で自宅サーバ作成 メールサーバ編:https://qiita.com/Chun3/items/dc0e176c9de4f6b4bfb8]]~ 【参考URL】[[Postfixのセキュリティ対策:https://www.criterion.sc/sub_notes/Postfix_Security.html]]~ 【参考URL】[[FreeBSD、Apacheでサーバを構築して公開する:http://hobbit.ddo.jp/html/bsd.html]] *cyrus-saslのインストール [#z2a3e92d] SMTP認証にsasl認証を使用する。 # portinstall security/cyrus-sasl2 # portinstall security/cyrus-sasl2-saslauthd cyrus-sasl2 インストール時に、オプションで OTP を外してないと、 postfix/smtpd[27109]: OTP unavailable because can't read/write key database /etc/opiekeys: Permission denied と言うログが残ります。 [[Postfix logging some OTP related permission denied messages:https://lists.freebsd.org/pipermail/freebsd-questions/2008-June/177891.html]] *** Updated user `cyrus'. [#fa9a6d5e] You can use sasldb2 for authentication, to add users use: saslpasswd2 -c username If you want to enable SMTP AUTH with the system Sendmail, read Sendmail.README NOTE: This port has been compiled with a default pwcheck_method of auxprop. If you want to authenticate your user by /etc/passwd, PAM or LDAP, install ports/security/cyrus-sasl2-saslauthd and set sasl_pwcheck_method to saslauthd after installing the Cyrus-IMAPd 2.X port. You should also check the /usr/local/lib/sasl2/*.conf files for the correct pwcheck_method. If you want to use GSSAPI mechanism, install ports/security/cyrus-sasl2-gssapi. If you want to use SRP mechanism, install ports/security/cyrus-sasl2-srp. If you want to use LDAP auxprop plugin, install ports/security/cyrus-sasl2-ldapdb. *** cyrus-sasl2-saslauthd [#oa10f116] ====> Compressing man pages (compress-man) ===> Staging rc.d startup script(s) ===> Installing for cyrus-sasl-saslauthd-2.1.28 ===> Checking if cyrus-sasl-saslauthd is already installed ===> Registering installation for cyrus-sasl-saslauthd-2.1.28 Installing cyrus-sasl-saslauthd-2.1.28... To run saslauthd from startup, add saslauthd_enable="YES" in your /etc/rc.conf. ===> SECURITY REPORT: This port has installed the following files which may act as network servers and may therefore pose a remote security risk to the system. /usr/local/sbin/saslauthd If there are vulnerabilities in these programs there may be a security risk to the system. FreeBSD makes no guarantee about the security of ports included in the Ports Collection. Please type 'make deinstall' to deinstall the port if this is a concern. For more information, and contact details about the security status of this software, see the following webpage: https://www.cyrusimap.org/sasl/ ===> Cleaning for heimdal-7.8.0_6 ===> Cleaning for cyrus-sasl-2.1.28 ===> Cleaning for cyrus-sasl-saslauthd-2.1.28 **/etc/rc.conf [#p4db3ab2] saslauthd_enable="YES" saslauthd_flags="-a sasldb" **saslauthd起動 [#sba0d34c] /usr/local/etc/rc.d/saslauthd start **SMTP AUTH設定 [#va7f222c] /usr/local/lib/sasl2/smtpd.conf pwcheck_method: auxprop 元記事はこうなっているが、現状、/usr/local/lib/sasl2/Sendmail.conf というファイルがあって、中身は pwcheck_method: auxprop となってて、同じ内様。両方設置しておくことにした。 **SMTP AUTHユーザー追加 [#y5e24553] # saslpasswd2 -c -u Host_name user_name Password: Again (for verification): sasldblistusers2 user_name@hobbit.ddo.jp: userPassword この操作で /usr/local/etc/sasldb2.db が生成されるが、Host_name は、# postconf -h myhostname の値を用いると、エラーせず認証されるようである。 **/usr/local/etc/sasldb2.db所有権変更 [#v492d28e] />chown cyrus:mail /usr/local/etc/sasldb2.db />chmod 640 /usr/local/etc/sasldb2.db * Postfix [#i229361a] BDB BLACKLISTD と SASL にチェック追加 #ref(Postfix_show_config.PNG) # portupgrade -rf --batch postfix 【付記】BDBオプションを追加した場合、Postfixの最初のnewaliasesコマンドでDBの作成時に # newaliases BDB0210 /etc/mail/aliases.db: metadata page checksum error BDB1581 File handles still open at environment close BDB1582 Open file handle: /etc/mail/aliases.db BDB1582 Open file handle: /etc/mail/aliases.db BDB1582 Open file handle: /etc/mail/aliases.db BDB1582 Open file handle: /etc/mail/aliases.db : : : BDB1582 Open file handle: /etc/mail/aliases.db BDB0210 /usr/local/mailman/data/aliases.db: metadata page checksum error BDB1581 File handles still open at environment close BDB1582 Open file handle: /usr/local/mailman/data/aliases.db BDB1582 Open file handle: /usr/local/mailman/data/aliases.db BDB1582 Open file handle: /usr/local/mailman/data/aliases.db : : : BDB1582 Open file handle: /usr/local/mailman/data/aliases.db BDB1582 Open file handle: /usr/local/mailman/data/aliases.db BDB1582 Open file handle: /usr/local/mailman/data/aliases.db # のような出力があった。postfix reload して再度 newaliases したら、すぐにプロンプトが戻って来た。 多分、正常な動作だったのだろうけど、ちと焦った。(^_^; **main.cf 設定 [#c8f2cc6e] myhostname = mail.smb.net # コメント解除でホスト名指定 mydomain = smb.net # コメント解除でドメイン名指定 myorigin = $myhostname # コメント解除(送信元メール) inet_interfaces = all # コメント解除(受信するネットワークアドレスを指定) mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain # コメント解除(ローカル配送先) mynetworks = 192.168.1.0/24, 127.0.0.0/8 # リレーを許可するIPアドレス relay_domains = $mydestination # コメント解除(リレーを許可するドメインを指定) alias_maps = hash:/etc/aliases # コメント解除(エイリアスを設定) alias_database = hash:/etc/aliases # コメント解除(エイリアスを設定) # Cyrus-SASL configuration # 下記を追加(SASLによるSMTP認証を使用) smtpd_sasl_auth_enable = yes smtpd_sasl_local_domain = $myhostname smtpd_sasl_security_options = noanonymous smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination broken_sasl_auth_clients = yes *起動 [#c7766114] # service saslauthd start Starting saslauthd. # service postfix start postfix/postfix-script: starting the Postfix mail system *SMTP-AUTH の確認 [#ybe2857a] 【参考URL】[[Postfix で SMTP-AUTH を実現:http://www.miloweb.net/smtpauth.html]] SMTP-AUTH が正常に動作しているかを確認します。 具体的には Telnet で 25 番ポートにアクセスして確認します。 $ telnet localhost 25 <-- 入力し、エンター Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. 220 www.miloweb.net ESMTP Postfix EHLO localhost <-- EHLO localhostと入力 250-zdnet.hoge.org 250-PIPELINING 250-SIZE 10240000 250-ETRN 250-AUTH SCRAM-SHA-512 SCRAM-SHA-384 SCRAM-SHA-256 SCRAM-SHA-224 SCRAM-SHA-1 DIGEST-MD5 CRAM-MD5 NTLM LOGIN PLAIN 250-AUTH=SCRAM-SHA-512 SCRAM-SHA-384 SCRAM-SHA-256 SCRAM-SHA-224 SCRAM-SHA-1 DIGEST-MD5 CRAM-MD5 NTLM LOGIN PLAIN <-- この行があればオッケーです 250 8BITMIME QUIT <-- 確認できたので、QUITコマンドで切断します もし「 250-AUTH PLAIN LOGIN 」と表示されていれば、SMTP-AUTH が上手く行っていないので、 Postfix のインストール手順を見直して、やり直さなければならない。 **更に確認を進める [#ta6718c7] http://linux.kororo.jp/cont/server/smtp_auth.php # printf 'ユーザー名\0ユーザー名\0パスワード' | mmencode で得られるBase64を用いてAUTH PLAIN (平文認証)認証出来るか確認する。~ 予め main.cf で #smtp_sasl_security_options = noplaintext とコメントアウトしておく。 $ telnet localhost 25 <-- 入力し、エンター Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. 220 www.miloweb.net ESMTP Postfix EHLO localhost <-- EHLO localhostと入力 250-zdnet.hoge.org 250-PIPELINING 250-SIZE 10240000 250-ETRN 250-AUTH SCRAM-SHA-512 SCRAM-SHA-384 SCRAM-SHA-256 SCRAM-SHA-224 SCRAM-SHA-1 DIGEST-MD5 CRAM-MD5 NTLM LOGIN PLAIN 250-AUTH=SCRAM-SHA-512 SCRAM-SHA-384 SCRAM-SHA-256 SCRAM-SHA-224 SCRAM-SHA-1 DIGEST-MD5 CRAM-MD5 NTLM LOGIN PLAIN <-- この行があればオッケーです 250 8BITMIME AUTH PLAIN a29yb3JvAGtvcm9ybwBrb3JvMTgxNQ== <-- # printf 'ユーザー名\0ユーザー名\0パスワード' | mmencode の結果を入力 235 Authentication successful QUIT 221 Bye Connection closed by foreign host. ** /var/log/all.log [#w07e9ccb] postfix/smtpd[66015]: E4D824E6061: client=gw.kuji-clinic.net[210.255.122.209], sasl_method=CRAM-MD5, sasl_username=hoge@FQDN ---- Total access &counter(total);:本日 &counter(today);:昨日 &counter(yesterday); #counter([total|today|yesterday]);