2010年6月3日 星期四

postfix+mysql(auth)+postfixadmin at centos

之前一直找到在其他linux套件上的做法,一直做不成功
(要嘛是debian的,要嘛是ubuntu的,不然就是fedora的)

主要是postfix+dovecot來完成的
(還有一個是搭配courier-imap,但這個我一直做不起來..>.<)
此篇文章主要是參考這一篇
(http://www.campworld.net/thewiki/pmwiki.php/LinuxServersCentOS/Cent5VirtMailServer)

下面的內容僅從中摘錄部份重點

首先要準備的幾個軟件
1.postfix with mysql(這個可以參考http://www.pigo.idv.tw/archives/323)
(centos預設的postfix是不支援mysql的,務必參考上述文章重新安裝)
2.Dovecot
3.mysql
4.httpd
5.postfixadmin
(這個是網頁的程序,麻煩自己去抓回來放在你的web目錄下,之後透過web就可以管理郵件帳號)
6.cyrus-sasl,cyrus-sasl-lib,cyrus-sasl-devel,cyrus-sasl-sql,cyrus-sasl-plain
上面除了1,5之外,其他都可以用centos5.4內建的套件就好(yum install xxxx)

首先把postfixadmin設定好
新版的postfixadmin在安裝上做了一些改良,使用者只要先把資料庫及使用者建立起來
以及修改config.inc.php這個檔案,剩下的就全都可以在網頁上完成了(不必再自己手動建立資料表)
(config.inc.php裡面也都有針對各項參數做說明)
如同下面的步驟
Now we need to setup the mysql database for mysqladmin. We only need to create the database and user. The setup file will create the rest.
> mysql -u root -p -e "CREATE DATABASE postfix;"
> mysql -u root -p -e "CREATE USER postfix@localhost IDENTIFIED BY 'choose_a_password';"
> mysql -u root -p -e "GRANT ALL PRIVILEGES ON postfix . * TO postfix@localhost;"

Now its time to setup the config file. Don't forget to set your password. Find the following items and change them.
> cd /usr/share/postfixadmin
> nano -w config.inc.php


# 這一行要改成true,否則無法安裝
$CONF['configured'] = true;

// Postfix Admin Path
// Set the location to your Postfix Admin installation here.
$CONF['postfix_admin_url'] = '/mailadmin/';

// Database Config
// mysql = MySQL 3.23 and 4.0
// mysqli = MySQL 4.1
// pgsql = PostgreSQL
$CONF['database_type'] = 'mysqli';
$CONF['database_host'] = 'localhost';
$CONF['database_user'] = 'postfixadmin';
$CONF['database_password'] = 'postfixadmin';
$CONF['database_name'] = 'postfix';
$CONF['database_prefix'] = '';


$CONF['domain_path'] = 'YES';
$CONF['domain_in_mailbox'] = 'NO';
$CONF['encrypt'] = 'cleartext';


接下來,換設定一下postfix的main.cf(/etc/postfix/main.cf)
關於postfix原本的一些設定我就不多談了
(如下這些)
# network settings
inet_interfaces = all
mydomain = yourdomain.com
myhostname = host.yourdomain.com
mynetworks = 192.168.0.0/16, 127.0.0.0/24,
mydestination = $myhostname, localhost.$mydomain, localhost
relay_domains = $mydestination
# mail delivery
recipient_delimiter = +
========================
上面這些設定用預設也行,要針對自己的情況修改也行
參考鳥哥的文章可以瞭解詳細內容(簡易 Mail Server 架設 -- Postfix 設定)
或是連到
HOWTO Virtual Mail Hosting on CentOS 5.x - Postfix MySQL Dovecot Postfix Admin
這篇原本的文章中,參考它文中所附上的完整main.cf及master.cf設定檔內容

本文只針對要利用mysql來管理所需增設的一些設定
在/etc/postfix/main.cf新增下述設定

# virtual setup
virtual_alias_maps = mysql:/etc/postfix/mysql-virtual_alias_maps.cf,
regexp:/etc/postfix/virtual_regexp
virtual_gid_maps = static:89 #your postfix gid
virtual_mailbox_base = /home/vmail
virtual_mailbox_domains = mysql:/etc/postfix/mysql-virtual_domains_maps.cf
virtual_mailbox_maps = mysql:/etc/postfix/mysql-virtual_mailbox_maps.cf
virtual_minimum_uid = 89
virtual_transport = virtual
virtual_uid_maps = static:89 #your postfix uid

# authentication
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtpd_sasl_local_domain = $myhostname
broken_sasl_auth_clients = yes

# tls config
smtp_use_tls = yes
smtpd_use_tls = yes
smtp_tls_note_starttls_offer = yes
smtpd_tls_key_file = /etc/postfix/ssl/smtpd.pem
smtpd_tls_cert_file = /etc/postfix/ssl/smtpd.pem
smtpd_tls_CAfile = /etc/postfix/ssl/smtpd.pem
smtpd_tls_loglevel = 1
smtpd_tls_received_header = yes
smtpd_tls_session_cache_timeout = 3600s
tls_random_source = dev:/dev/urandom

# rules restrictions
# smtpd_client_restrictions = reject_rbl_client zen.spamhaus.org
smtpd_helo_restrictions = permit_sasl_authenticated,
permit_mynetworks,
reject_non_fqdn_hostname
smtpd_sender_restrictions = reject_non_fqdn_sender,
reject_unknown_sender_domain
smtpd_recipient_restrictions = permit_sasl_authenticated,
permit_mynetworks,
reject_unauth_destination,
reject_non_fqdn_recipient,
reject_unknown_recipient_domain
smtpd_helo_required = yes
unknown_local_recipient_reject_code = 550
disable_vrfy_command = yes
smtpd_data_restrictions = reject_unauth_pipelining


然後新增下面幾個個檔案
The postfix / mysql config files.

/etc/postfix/mysql-virtual_alias_maps.cf

hosts = localhost
user = postfix
password = postfix
dbname = postfix
table = alias
select_field = goto
where_field = address


/etc/postfix/mysql-virtual_domains_maps.cf

hosts = localhost
user = postfix
password = postfix
dbname = postfix
table = domain
select_field = domain
where_field = domain
additional_conditions = and backupmx = '0' and active = '1'
這裡要注意一下,我在postfixadmin新增網域時,把"備份網域"勾起來了,造成我信件一直被主機退回
上面這一行sql語法,就是會造成找不到允許接收信件網域的原因
看是要把備份網域取消,或是修改這一行,把backupmx = '0' 拿掉都行


/etc/postfix/mysql-virtual_mailbox_maps.cf

hosts = localhost
user = postfix
password = postfix
dbname = postfix
table = mailbox
select_field = maildir
where_field = username


接下來,修改這個檔案(讓smtp在寄信要求驗證時使用的設定檔)
/usr/lib/sasl2/smtpd.conf


pwcheck_method: auxprop
mech_list: PLAIN LOGIN
auxprop_plugin: sql
sql_verbose: yes
sql_engine: mysql
sql_hostnames: localhost
sql_user: postfix
sql_passwd: postfix
sql_database: postfix
sql_select: select password from mailbox where username = '%u@%r'


接下來,產生ssl的key讓postfix可以支援TLS

Now generate an SSL certificate for postfix to have TLS support.
> mkdir /etc/postfix/ssl
> cd /etc/postfix/ssl
> openssl req -new -x509 -nodes -out smtpd.pem -keyout smtpd.pem -days 3650

We need to touch a file. So type the follwoing.
> touch /etc/postfix/virtual_regexp
上面這個動作,就是在main.cf中
virtual_alias_maps = mysql:/etc/postfix/mysql-virtual_alias_maps.cf,
regexp:/etc/postfix/virtual_regexp
這一行要用到的檔案,讓你也可以手動編輯允許收信的電子郵件及網域
可以參考http://www.sympa.org/faq/postfix這篇文章
內文主要就是在講這檔案的用途

接下來是設定一下信件存放的目錄
Finally we'll configure the mail store directory. We put it in the /home directory to make backups and other item easy. So type the following.
> mkdir /home/vmail
> chmod 770 /home/vmail
> chown postfix:postfix /home/vmail

最後,修改dovecot的設定檔(/etc/dovecot.conf)

# Dovecot config file
auth default {
userdb sql {
args = /etc/dovecot-mysql.conf
}
passdb sql {
args = /etc/dovecot-mysql.conf
}
}
first_valid_uid = 89
default_mail_env = maildir:/home/vmail/%d/%n
protocols = imaps imap pop3s pop3
ssl_cert_file = /etc/postfix/ssl/smtpd.pem
ssl_key_file = /etc/postfix/ssl/smtpd.pem


新增/etc/dovecot-mysql.conf ,內容如下

driver = mysql
connect = host=localhost dbname=postfix user=postfix password=yourpassword
default_pass_scheme = PLAIN
password_query = SELECT password FROM mailbox WHERE username = '%u'
user_query = SELECT maildir, 89 AS uid, 89 AS gid FROM mailbox WHERE username = '%u'


到這裡,整個作業就完成了,把postfix,dovecot都重新啟動後
這樣postfix就可以用postfixadmin來做管理了

文章只針對postfix,dovecot(pop3,imap)的部份做重點摘錄

沒有留言: