菜鳥學Linux 第061篇筆記 postfix配置,pop3

菜鳥學Linux 第061篇筆記 postfix配置,pop3



SMTP --> SSMTPS

ESMTP 


POP3: Post Office Protocol v3

IMAP4: Internet Mail Access Protocol


SASL: Simple Authentication Secure Layer

v1, v2


MDA: Mail Delivery Agent

procmail, maildrop


MUA: Mail User Agent

mutt, mail


MTA: Mail Transfer Agent

sendmail(主流地位), qmail, exim

postfix(流行,模塊化設計) master(/etc/postfix/master.cf)

(/etc/postfix/main.cf)

postfix -d(默認選項) -n(修改選項) -M(支持查找表類型) -A(客戶端可用SASL插件)

-a(服務器端支持的SASL插件類型) 



SMTP:

helo

mail from

rcpt to

data 

.

quit



[email protected] --> c.com (MX) --> [email protected]


Mail Relay:




爲postfix提供service 腳本

#!/bin/bash

#

# postfix      Postfix Mail Transfer Agent

#

# chkconfig: 2345 80 30

# description: Postfix is a Mail Transport Agent, which is the program \

#              that moves mail from one machine to another.

# processname: master

# pidfile: /var/spool/postfix/pid/master.pid

# config: /etc/postfix/main.cf

# config: /etc/postfix/master.cf


# Source function library.

. /etc/rc.d/init.d/functions


# Source networking configuration.

. /etc/sysconfig/network


# Check that networking is up.

[ $NETWORKING = "no" ] && exit 3


[ -x /usr/sbin/postfix ] || exit 4

[ -d /etc/postfix ] || exit 5

[ -d /var/spool/postfix ] || exit 6


RETVAL=0

prog="postfix"


start() {

# Start daemons.

echo -n $"Starting postfix: "

        /usr/bin/newaliases >/dev/null 2>&1

/usr/sbin/postfix start 2>/dev/null 1>&2 && success || failure $"$prog start"

RETVAL=$?

[ $RETVAL -eq 0 ] && touch /var/lock/subsys/postfix

        echo

return $RETVAL

}


stop() {

  # Stop daemons.

echo -n $"Shutting down postfix: "

/usr/sbin/postfix stop 2>/dev/null 1>&2 && success || failure $"$prog stop"

RETVAL=$?

[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/postfix

echo

return $RETVAL

}


reload() {

echo -n $"Reloading postfix: "

/usr/sbin/postfix reload 2>/dev/null 1>&2 && success || failure $"$prog reload"

RETVAL=$?

echo

return $RETVAL

}


abort() {

/usr/sbin/postfix abort 2>/dev/null 1>&2 && success || failure $"$prog abort"

return $?

}


flush() {

/usr/sbin/postfix flush 2>/dev/null 1>&2 && success || failure $"$prog flush"

return $?

}


check() {

/usr/sbin/postfix check 2>/dev/null 1>&2 && success || failure $"$prog check"

return $?

}


restart() {

stop

start

}


# See how we were called.

case "$1" in

  start)

start

;;

  stop)

stop

;;

  restart)

stop

start

;;

  reload)

reload

;;

  abort)

abort

;;

  flush)

flush

;;

  check)

check

;;

  status)

  status master

;;

  condrestart)

[ -f /var/lock/subsys/postfix ] && restart || :

;;

  *)

echo $"Usage: $0 {start|stop|restart|reload|abort|flush|check|status|condrestart}"

exit 1

esac


exit $?


# END


爲此腳本賦予執行權限:

# chmod +x /etc/rc.d/init.d/postfix


將postfix服務添加至服務列表:

# chkconfig --add postfix


設置其開機自動啓動:

# chkconfig postfix on


使用此腳本重新啓動服務,以測試其能否正常執行:

# service postfix restart





postfix配置

1. 將主機名配置爲name.域名

例 mail.mysky.com

2. 將DNS 的MX記錄指向mail 服務器


驗證MX記錄和PTR記錄是否設置正確

dig -t MX mysky.com @192.168.11.122

dig -x 192.168.11.122 @192.168.11.122

(說明一下我因爲是在做測試所以郵件服務器和DNS服務器裝在一臺機器上)


3. /etc/postfix/main.cf  配置文件

(此配置文件裏允許使用$parameter 引用相應參數的值)

啓用如下選項

myhostname = mail.mysky.com

mydomain = mysky.com

myorigin = $mydomain

mydestination = $mydomain, $myhostname, localhost, ns.$mydomain

mynetworks = 192.168.11.0/24, 127.0.0.0/8

(注意原先這幾項都是前邊加#號的,後邊的參數自行定義)


說明:

myhostname  參數指定運行postfix郵件系統的主機的主機名,

默認情況下,其值被設定爲本地機器名;

mydomain 參數指定您的域名,默認情況下,

postfix將myhostname的第一部分刪除而作爲mydomain的值;

myorigin 參數用來指明發件人所在的域名,即做發件地址僞裝;

mydestination 參數指定postfix接收郵件時收件人的域名,

 即您的postfix系統要接收到哪個域名的郵件;

mynetworks 參數指定你所在的網絡的網絡地址,postfix系統根據其值來區別用戶

  是遠程的還是本地的,如果是本地網絡用戶則允許其訪問;

(配置完成後即可正常發送郵件)





配置郵件接收服務器(pop3, imap4)

MRA Mail Retrival Agent:cyrus-imap, dovecot

pop3 110/tcp

imap4 143/tcp

以明文方式工作



郵箱格式:

mbox 一個文件存儲所有郵件

maildir 一個文件存儲一封郵件,所有郵件存儲在一個目錄中;



dovecot rpm包依賴mysql客戶端 

SSL (Secure Socket Layer)

IMAP (Internet Message Access Protocol)

支持四種協議:pop3, imap4, pops, imaps


安裝 dovecot 

# yum install dovecot


配置文件/etc/dovecot.conf

將protocol選項的#去掉,保留 imap, pop3

#protocols = imap imaps pop3 pop3s(原型)

protocols = imap pop3


開啓dovecot 

# service dovecot start


檢查dovecot 是否正常工作(查看監聽端口是否打開)

# netstat -tnlp | grep dovecot



測試可否接收郵件

telnet mail.mysky.com 110

USER username

PASS password

LIST (列出郵件)

RETR number (Retrieval 取回)


(至此我們的郵件服務器即可簡單的接收和發送郵件,但無認證功能)




爲郵件服務器實現用戶認證功能


1、啓動sasl, 啓動sasl服務

/etc/rc.d/init.d/saslauthd (啓動腳本)

/etc/sysconfig/saslauthd (配置文件)


saslauthd -v (顯示當前主機saslauthd服務所支持的認證機制,默認爲pam)


配置其配置文件/etc/sysconfig/saslauthd

將MECH=pam 改爲 MECH=shadow


啓動sasl服務

# service saslauthd start

設置爲開機啓動

# chkconfig saslauthd on

檢查是否開機啓動

# chkconfig --list saslauthd


測試sasl是否可以進行用戶認證

testsaslauthd -u username -p password

(注意這裏的username和password是存在於shadow文件中的用戶)

例如:(這個是我的主機裏的用戶)

# testsaslauthd -u tomcat -p tomcat

0: OK "Success."


2、使postfix配置sasl認證

如果只有postfix軟件,默認客戶端只要可以和安裝postfix服務器連接即可發送郵件,

無論你mail地址是什麼,要發往哪裏,都給中繼,而加上sasl認證機制後即可控制

postfix爲哪些用戶或者IP進行中繼


查看postfix可用SASL服務器插件類型

postconf -a


(postfix編譯之前確保已經安裝cyrus-sasl-devel和cyrus-sasl-plain)


控制smtp中繼功能

connection smtpd_client_restrictions = check_client_access hash:/PATH

helo smtpd_helo_restrictions = check_helo_access hash:/PATH

mail from smtpd_sender_restrictions = check_sender_access hash:/PATH

rcpt to smtpd_recipient_restrictions = check_recipient_access hash:/PATH

permit_mynetworks, reject_unauth_destination 這兩項必須添加

data smtpd_data_restrictions = check_data_access hash:/PATH



實現示例1


這裏以禁止192.168.11.2這臺主機通過工作在192.168.11.122上的postfix服務發送郵件爲例演示說明其實現過程。訪問表使用hash的格式。


(1)首先,編輯/etc/postfix/access文件,以之做爲客戶端檢查的控制文件,在裏面定義如下一行:

172.16.100.200 REJECT


(2)將此文件轉換爲hash格式

# postmap /etc/postfix/access


(3)配置postfix使用此文件對客戶端進行檢查

編輯/etc/postfix/main.cf文件,添加如下參數:

smtpd_client_restrictions = check_client_access hash:/etc/postfix/access


(4)讓postfix重新載入配置文件即可進行發信控制的效果測試了。

# service postsfix {reload|restart}





真正設置postfix認證


#vim /etc/postfix/main.cf

去除mynetworks = 裏的192.168.11.0/24 只保留127.0.0.0/8

添加以下內容:

############################CYRUS-SASL############################

broken_sasl_auth_clients = yes

smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject_invalid_hostname,reject_non_fqdn_hostname,reject_unknown_sender_domain,reject_non_fqdn_sender,reject_non_fqdn_recipient,reject_unknown_recipient_domain,reject_unauth_pipelining,reject_unauth_destination

smtpd_sasl_auth_enable = yes

smtpd_sasl_local_domain = $myhostname

smtpd_sasl_security_options = noanonymous

smtpd_sasl_path = smtpd

smtpd_banner = Welcome to our $myhostname ESMTP,Warning: Version not Available!



# vim /usr/lib/sasl2/smtpd.conf

添加如下內容:

pwcheck_method: saslauthd

mech_list: PLAIN LOGIN


讓postfix重新加載配置文件

#/usr/sbin/postfix reload



# telnet localhost 25

Trying 127.0.0.1...

Connected to localhost.localdomain (127.0.0.1).

Escape character is '^]'.

220 Welcome to our mail.mysky.com ESMTP,Warning: Version not Available!

ehlo mail.mysky.com

250-mail.mysky.com

250-PIPELINING

250-SIZE 10240000

250-VRFY

250-ETRN

250-AUTH PLAIN LOGIN

250-AUTH=PLAIN LOGIN               (請確保您的輸出以類似兩行)

250-ENHANCEDSTATUSCODES

250-8BITMIME

250 DSN



驗證是否是隻有shadow裏有的用戶纔可能進行登錄 (這裏沒研究明白用時2天待以後處理)

說是更換版本,,,可是我已經最新版本了!!!!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章