FedoreCore6 postfix,dovecotよるメール サーバー構築


<< トップページへ

概要


「はじ めに」(2009/01/03修正)

私のISPの回線 は Outbound Port25 Blocking がかっていました。
それを知らずに自宅SMTPサーバを postfixで構築したものの何度テストしても動作しないため、
かなり悩みました。

Outbound port 25 Blocking(以下、OP25B)とは、外向きのTCP 25番ポートをブロックする事です。
プロバイダー加入者パソコンがウイルスに感染、乗っ取られたりして迷惑メールを配信するために利用されるのを防止するた めのものです。
OP25Bが導入された場合であっても、加入プロバイダーのSMTPサーバを加入プロバイダーのドメインを利用してメール送信しているユーザには全く影響 がありません。しかし自宅サーバで独自ドメインを使用してSMTPサーバを 動かしている場合は対策が必要になります。

OP25Bに対する自宅SMTPサーバでの完全対策としては2つの対策が必要です。

1.自宅SMTPサーバから外部へ送信する場合の対策
  自分が契約しているプロバイダーがOP25Bが導入した場合です。

2.外部から自宅SMTPサーバに接続する場合の対策
  自宅SMTPサーバを自宅以外の外部から使用する場合で、かつクライアントが接続されているISP側がOP25Bを導入  した場合です。

どちらもOP25Bにより25番ポートが使用できないという点では同じです。

■  postfix clamav amavisd-newのパッケ−ジをインスト−ル

インストール時にレポジトリの設定はFC6 の場合はextora base updateのみですべて揃うようになりました。
# yum install postfix dovecot  ←これを実行するときのレポジトリはcoreとupdate,extoraのみ
# yum install clamav-* clamav-update amavisd-new ←レポジトリ はcore,update,extoraのみ

設定に入る前に「mxレコ−ドが登録されていて正引き ができること。」をクリアしてから次へ進んでください。逆引きはプロバイダー名でも動作するようです。
$ host -t mx hoge.sytes.net
hoge.sytes.net mail is handled by 5 hoge.sytes.net. ← mxレコ−ドが登録されている

$ dig hoge.sytes.net
 
; <<>> DiG 9.2.3 <<>> hoge.sytes.net
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 13679
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
 
;; QUESTION SECTION:
;hoge.sytes.net.                IN      A
 
;; ANSWER SECTION:
hoge.sytes.net. 38      IN      A       219.106.6.254   ← 正引ができる状態である
 
;; Query time: 120 msec
;; SERVER: 192.168.11.1#53(192.168.11.1)
;; WHEN: Fri Jun 17 00:06:35 2005
;; MSG SIZE  rcvd: 49

$ dig -x 219.106.6.254
 
; <<>> DiG 9.2.3 <<>> -x 219.106.6.254
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 60584
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
 
;; QUESTION SECTION:
;254.6.106.219.in-addr.arpa.    IN      PTR
 
;; ANSWER SECTION:
254.6.106.219.in-addr.arpa. 25  IN      PTR     hoge.sytes.net. ← 逆引ができる状態である
 
;; Query time: 108 msec
;; SERVER: 192.168.11.1#53(192.168.11.1)
;; WHEN: Fri Jun 17 00:17:07 2005
;; MSG SIZE  rcvd: 73

これから設定するのは postfix + Auth + clamd + spamassassin + amavisd-new の環境です。設定ファイルと概要を下に書いてみます。
これからrelayhost + sasl auth で ISPのMTAを経由した送信を行う設定を行います。
設定の際には最悪の場合に元に戻せるように各段階ごとに設定ファイルを別名でコピーした後に行ってくださ い。
#alternatives --config mta ←メ−ルサ−バ−の切り替え

There are 3 programs which provide 'mta'.
 
  Selection    Command
-----------------------------------------------
*+ 1           /usr/sbin/sendmail.sendmail
   2           /usr/sbin/sendmail.postfix
   3           /usr/sbin/sendmail.exim
 
Enter to keep the current selection[+], or type selection number:2 ← postfixを選択する

#alternatives --config mta ←メ−ルサ −バ−の確認

There are 3 programs which provide 'mta'.
 
  Selection    Command
-----------------------------------------------
*  1           /usr/sbin/sendmail.sendmail
 + 2           /usr/sbin/sendmail.postfix ←postfix が選択されている。
   3           /usr/sbin/sendmail.exim
 
Enter to keep the current selection[+], or type selection number:

ここからはmain.cfの設定です。以下を参考に設定してください。
$ grep -v "#" /etc/postfix/main.cf | sed '/^$/ d'  ← /etc/postfix/main.cfの設定内容を確認

queue_directory = /var/spool/postfix
command_directory = /usr/sbin
daemon_directory = /usr/libexec/postfix
mail_owner = postfix
myhostname = hoge.sytes.net
mydomain = hoge.sytes.net
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
unknown_local_recipient_reject_code = 550
mynetworks_style = subnet
mynetworks = 192.168.1.0/24, 127.0.0.0/8
relay_domains = $mydestination
relayhost = [smtp.provider.ne.jp] ←契約ISPの送信メー ルサーバー
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
home_mailbox = Maildir/
debug_peer_level = 2
debugger_command =
         PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
         xxgdb $daemon_directory/$process_name $process_id & sleep 5
sendmail_path = /usr/sbin/sendmail.postfix
newaliases_path = /usr/bin/newaliases.postfix
mailq_path = /usr/bin/mailq.postfix
setgid_group = postdrop
html_directory = no
manpage_directory = /usr/share/man
sample_directory = /usr/share/doc/postfix-2.2.8/samples
readme_directory = /usr/share/doc/postfix-2.2.8/README_FILES

ここから追加分

#クライアント自宅サーバー間

#SMTP Authを有効にする。
smtpd_sasl_auth_enable = yes

#TLSの設定
smtpd_use_tls = yes
smtpd_tls_cert_file = /etc/postfix/certs/server.pem
smtpd_tls_key_file = /etc/postfix/certs/server.key
smtpd_tls_session_cache_database = btree:/etc/postfix/smtpd_scache
smtpd_tls_loglevel = 1

#リレ-制限の設定
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
#------------------------------------------------------------------------
#自宅サーバーISP間

#SMTP Authを有効にする。
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/saslpass
smtp_sasl_type = cyrus

#ISP のSMTPサーバ側のSASLメカニズムと自宅サーバ側のSASLメカニズムを合わせる
#smtp_sasl_mechanism_filter = cram-md5, plain, login
#------------------------------------------------------------------------
#その他

#OutLookなどのためにLOGIN認証を使用する
broken_sasl_auth_clients = yes

#postfix amavis連携
content_filter = smtp-amavis:[127.0.0.1]:10024

#特定ドメイン・IPアドレス受信拒否
smtpd_client_restrictions =
permit_mynetworks
check_client_access hash:/etc/postfix/reject_list, permit

#------------------------------------------------------------------------

ここからはmaster.cfの設定です。以下を参考 に設定してください。
$ cat /etc/postfix/master.cf
#
# Postfix master process configuration file.  For details on the format
# of the file, see the Postfix master(5) manual page.
#
# ==========================================================================
# service type  private unpriv  chroot  wakeup  maxproc command + args
#               (yes)   (yes)   (yes)   (never) (100)
# ==========================================================================
smtp      inet  n       -       n       -       -       smtpd
submission inet n       -       n       -       -       smtpd
        -o smtpd_etrn_restrictions=reject
#       -o smtpd_client_restrictions=permit_sasl_authenticated,reject
smtps    inet  n       -       n       -       -       smtpd
  -o smtpd_tls_wrappermode=yes -o smtpd_sasl_auth_enable=yes
#submission   inet    n       -       n       -       -       smtpd
#  -o smtpd_etrn_restrictions=reject
#  -o smtpd_enforce_tls=yes -o smtpd_sasl_auth_enable=yes
#628      inet  n       -       n       -       -       qmqpd
pickup    fifo  n       -       n       60      1       pickup
cleanup   unix  n       -       n       -       0       cleanup
qmgr      fifo  n       -       n       300     1       qmgr
#qmgr     fifo  n       -       n       300     1       oqmgr
tlsmgr    unix  -       -       n       1000?   1       tlsmgr
rewrite   unix  -       -       n       -       -       trivial-rewrite
bounce    unix  -       -       n       -       0       bounce
defer     unix  -       -       n       -       0       bounce
trace     unix  -       -       n       -       0       bounce
verify    unix  -       -       n       -       1       verify
flush     unix  n       -       n       1000?   0       flush
proxymap  unix  -       -       n       -       -       proxymap
smtp      unix  -       -       n       -       -       smtp
# When relaying mail as backup MX, disable fallback_relay to avoid MX loops
relay     unix  -       -       n       -       -       smtp
        -o fallback_relay=
#       -o smtp_helo_timeout=5 -o smtp_connect_timeout=5
showq     unix  n       -       n       -       -       showq
error     unix  -       -       n       -       -       error
discard   unix  -       -       n       -       -       discard
local     unix  -       n       n       -       -       local
virtual   unix  -       n       n       -       -       virtual
lmtp      unix  -       -       n       -       -       lmtp
anvil     unix  -       -       n       -       1       anvil
scache    unix  -       -       n       -       1       scache
#
# ====================================================================
# Interfaces to non-Postfix software. Be sure to examine the manual
# pages of the non-Postfix software to find out what options it wants.
#
# Many of the following services use the Postfix pipe(8) delivery
# agent.  See the pipe(8) man page for information about ${recipient}
# and other message envelope options.
# ====================================================================
#
# maildrop. See the Postfix MAILDROP_README file for details.
# Also specify in main.cf: maildrop_destination_recipient_limit=1
#
maildrop  unix  -       n       n       -       -       pipe
  flags=DRhu user=vmail argv=/usr/local/bin/maildrop -d ${recipient}
#
# The Cyrus deliver program has changed incompatibly, multiple times.
#
old-cyrus unix  -       n       n       -       -       pipe
  flags=R user=cyrus argv=/usr/lib/cyrus-imapd/deliver -e -m ${extension} ${user}
# Cyrus 2.1.5 (Amos Gouaux)
# Also specify in main.cf: cyrus_destination_recipient_limit=1
cyrus     unix  -       n       n       -       -       pipe
  user=cyrus argv=/usr/lib/cyrus-imapd/deliver -e -r ${sender} -m ${extension} ${user}
#
# See the Postfix UUCP_README file for configuration details.
#
uucp      unix  -       n       n       -       -       pipe
  flags=Fqhu user=uucp argv=uux -r -n -z -a$sender - $nexthop!rmail ($recipient)
#
# Other external delivery methods.
#
ifmail    unix  -       n       n       -       -       pipe
  flags=F user=ftn argv=/usr/lib/ifmail/ifmail -r $nexthop ($recipient)
bsmtp     unix  -       n       n       -       -       pipe
  flags=Fq. user=foo argv=/usr/local/sbin/bsmtp -f $sender $nexthop $recipient

##############################################################################
ここから追加分
smtp-amavis unix -    -    n    -    2  smtp
    -o smtp_data_done_timeout=1200
    -o smtp_send_xforward_command=yes
    -o disable_dns_lookups=yes

127.0.0.1:10025 inet n    -    n    -    -  smtpd
    -o content_filter=
    -o local_recipient_maps=
    -o relay_recipient_maps=
    -o smtpd_restriction_classes=
    -o smtpd_client_restrictions=
    -o smtpd_helo_restrictions=
    -o smtpd_sender_restrictions=
    -o smtpd_recipient_restrictions=permit_mynetworks,reject
    -o mynetworks=127.0.0.0/8
    -o strict_rfc821_envelopes=yes
    -o smtpd_error_sleep_time=0
    -o smtpd_soft_error_limit=1001
    -o smtpd_hard_error_limit=1000

その他設定ファイルを編集します。
設定ファイル
設定内容
/usr/lib/sasl2/smtpd.conf
pwcheck_method: saslauthd
mech_list: plain login ←追加する
/etc/aliases root:           hoge ← 追加
virusalert:     hoge ← 追加

# postalias hash:aliases ← デ−タ−ベ −スを更新する
# cp /usr/share/doc/clamav-server-0.88.5
/clamd.conf

/etc/clamd.conf

LogFile /var/log/clamd.smtp
PidFile /var/run/clamd.smtp/clamd.pid
LocalSocket /var/run/clamd.smtp/clamd.sock
FixStaleSocket
User amavis
ScanMail

/etc/crontab # vi crontab

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
25 22 * * * root /root/clamav.sh  ←追加する
*/5 * * * * root /var/video/cgi-bin/prog_gen.cgi 2&>1 /dev/null

/etc/hosts root@localhost.localdomain宛 のメール対策
# Do not remove the following line, or various programs
# that require network functionality will fail.
#127.0.0.1      localhost.localdomain   localhost
127.0.0.1       hoge.sytes.net         localhost
192.168.1.3     hoge

/etc/freshclam.conf #Exampleとしてコメント行とする。 (ウイルスデー ターの更新)
/etc/amavisd/amavisd.conf # ### http://www.clamav.net/
['ClamAV-clamd',
  \&ask_daemon, ["CONTSCAN {}\n", "/var/spool/amavisd/clamd.sock"],
  qr/\bOK$/, qr/\bFOUND$/,
  qr/^.*?: (?!Infected Archive)(.*) FOUND$/ ],
# # NOTE: the easiest is to run clamd under the same user as amavisd; match
$myhostname = 'hoge.sytes.net';  # must be a fully-qualified domain name!

#$mydomain = 'example.com';   # a convenient default for other settings
$mydomain = 'hoge.sytes.net';   # a convenient default for other settings



#$virus_admin               = undef;  # notifications recip.
$virus_admin               = "virusalert\@$mydomain";

/etc/clamd.d/amavisd.conf /etc/clamd.d/amavisd.confと LocalSocketの場所を同じにする。
LogSyslog
LogFacility LOG_MAIL
PidFile /var/run/amavisd/clamd.pid
FixStaleSocket
User amavis
LocalSocket /var/spool/amavisd/clamd.sock
/etc/postfix/saslpass ISPのメールサーバに対してClient SASLの 設定をして認証可能にする。
#SMTPサーバ名 ユーザー名:パスワード
[mail.boke.ne.jp]  hoge:xxxxx

[mail.boke.ne.jp]:587  hoge:xxxxxとすると、Submission ポートへも転送できる。
/etc/sysconfig/network hostnameの 設定
NETWORKING=yes
HOSTNAME=hoge
/root/clamav.sh
# cd /root
# vi clamav.sh

#!/bin/bash

PATH=/usr/bin:/bin
CLAMSCANTMP=`mktemp`
freshclam > /dev/null
#clamscan --recursive --no-archive --exclude=iptables --exclude-dir=/sys --remove / >
$CLAMSCANTMP
clamscan --recursive --remove / 2>/dev/null > $CLAMSCANTMP
[ ! -z "$(grep FOUND$ $CLAMSCANTMP)" ] && \
grep FOUND$ $CLAMSCANTMP | mail -s "Virus Found in `hostname`" root
rm -f $CLAMSCANTMP

# chmod +x clamav.sh

/etc/dovecot.conf
# vi /etc/dovecot.conf
protocols = imap imaps pop3 pop3s ← #を削除
# Disable SSL/TLS support.
ssl_disable = no
ssl_cert_file = /etc/postfix/certs/server.pem 
ssl_key_file = /etc/postfix/certs/server.key 
サーバ証明書、秘密鍵
/etc/postfix/certs/server.pem
/etc/postfix/certs/server.key 

##cd /etc/postfix/
# mkdir certs
# cd certs

サーバ用秘密鍵作成
# openssl genrsa -des3 1024 > server.key

.......
Enter pass phrase: *******
Verifying - Enter pass phrase: *******

SMTP起動時にパスワードを要求されないようにサーバー用秘密鍵からパスワードを削除
# openssl rsa -in server.key -out server.key

Enter pass phrase for server.key: *******
writing RSA key

サーバ用公開鍵作成
# openssl req -new -key server.key -out server.csr 
You are about to be asked ......
.........
Country Name (2 letter code) [GB]:JP 
State or Province Name (full name) [Berkshire]:Tokyo
Locality Name (eg, city) [Newbury]:Chofu-shi
Organization Name (eg, company) [My Company Ltd]:hoge.sytes.net 
Organizational Unit Name (eg, section) []: 
Common Name(eg, your name or your server's hostname)[]:hoge.sytes.net ホスト名
Email Address []:postmaster@hoge.sytes.net   メールアドレス
.......
A challenge password []: 
An optional company name []:

サーバ証明書作成
# openssl x509 -in server.csr -out server.pem -req -signkey server.key -days 365

Signature ok
subject=/C=JP/ST=Tokyo/L=Chofu-shi/O=hoge.sytes.net/CN=hoge.sytes.net/emailAddress=
postmaster@hoge.sytes.net Getting Private key

# chmod 400 server.*
# 特定ドメイン・IPアドレス受信拒否
smtpd_client_restrictions =
permit_mynetworks
check_client_access hash:/etc/postfix/reject_list, permit
受信拒否リスト作成
/etc/postfix/reject_list というファイルを作成

# vi
/etc/postfix/reject_list
hoge.com REJECT
72.70.52.145 REJECT

reject_listをDB形式に変換
/usr/sbin/postmap /etc/postfix/reject_list
← デ−タ−ベ −スを更新する

Postfixをリロードして終了。
/usr/sbin/postfix reload

/etc/postfix/main.cf
上記参照
/etc/postfix/master.cf 上記参照
ユ−ザ−アカウントを作成します。
# saslpasswd2 -u hoge.sytes.net hoge ← ユーザ追加
Password:
Again (for verification):

# sasldblistusers2 ← SMTP-Auth用ユーザ/パスワード確認
hoge@hoge.sytes.net: userPassword

# chgrp postfix /etc/sasldb2 ← 所属 グループをpostfixに変更

※SMTP-Auth用ユーザ/パスワードを削除する場合
# saslpasswd2 -d hoge -u hoge.hoge.sytes.net
あとはル−タ−で25番、110番、587番、465番、995番のポ −トを開けてテストで す。 ルーターでのポ−トの開け方はメ−カー機種によって違いますので取扱説明書に従ってください。 ポートが開いたかどうか確認する方法としてポ−トスキャンをしてくれるサイトがあります。 https://image.grc.com/に行ってShieldsUP!の ボタンをクリックして
次に Proceedのボタンをクリック、次にALL Service Portのボタンをクリックすると 開いているポートを表示してくれます。ルーターを設定したのにポ−トが開いていない場合は システム設定ーセキュリティーレベルを確認してください。 たぶんファイヤーウオールが有効になっているはずなので無効にすればいけるはずです。


▲このページのトップへ戻る

▲トップへ戻 る





Copyright©2005-2006  熊の巣  All Right Reserved.  管理人へメール