歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Unix知識 >> 關於Unix >> 基於Fedora上建立功能強大的郵件服務器

基於Fedora上建立功能強大的郵件服務器

日期:2017/3/6 15:51:22   编辑:關於Unix
一、軟件的准備 A.檢查你的系統是否已安裝了以下軟件 1.pam 2.pam-devel 3.mysql 4.mysql-servel 5.mysql-devel 6.imap-devel 7.sharutils B.下載以下軟件 1.metamail*.i386.rpm 2.pam-mysql 3.cyrus-sasl-2.1.15 4.postfix-2.0.16 5.courier-imap C.說明 my
  一、軟件的准備
  A.檢查你的系統是否已安裝了以下軟件
  1.pam
  2.pam-devel
  3.mysql
  4.mysql-servel
  5.mysql-devel
  6.imap-devel
  7.sharutils
  
  B.下載以下軟件
  1.metamail*.i386.rpm
  2.pam-mysql
  3.cyrus-sasl-2.1.15
  4.postfix-2.0.16
  5.courier-imap
  
  C.說明
  
  mysql-devel在第3個盤。我忘了裝,結果pam-mysql和postfix的編譯過不了。metamail和sharutils是取驗證碼用的。
  
  二、mysql數據庫和表的建立
  A.[root@localhost root]# mysql
  
  Welcome to the MySQL monitor. Commands end with ; or \g.
  Your MySQL connection id is 3 to server version: 3.23.58
  
  Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
  
  mysql> use mysql
  Reading table information for completion of table and column names
  You can turn off this feature to get a quicker startup with -A
  
  Database changed
  mysql> INSERT INTO db (Host,Db,User,Select_priv)VALUES('localhost','postfix','postfix','Y');
  Query OK, 1 row affected (0.00 sec)
  
  mysql> CREATE DATABASE postfix;
  Query OK, 1 row affected (0.00 sec)
  
  mysql> GRANT ALL ON postfix.* TO postfix@localhost
  -> IDENTIFIED BY "postfix";
  Query OK, 0 rows affected (0.00 sec)
  
  mysql> FLUSH PRIVILEGES;
  Query OK, 0 rows affected (0.00 sec)
  
  mysql> quit
  Bye
  
  B.把下列文字復制存為postfix.sql
  
  ########potfix_sql########bigin##################
  CREATE TABLE alias (
  username varchar(255) NOT NULL default',
  goto text NOT NULL,
  domain varchar(255) NOT NULL default',
  PRIMARY KEY (username)
  ) TYPE=MyISAM;
  CREATE TABLE domain (
  domain varchar(255) NOT NULL default',
  description varchar(255) NOT NULL default',
  PRIMARY KEY (domain)
  ) TYPE=MyISAM;
  
  CREATE TABLE mailbox (
  userid char(20) binary NOT NULL default',
  username varchar(255) NOT NULL default',
  password varchar(255) NOT NULL default',
  home varchar(255) NOT NULL default',
  maildir varchar(255) NOT NULL default',
  name varchar(255) NOT NULL default',
  uid smallint(5) unsigned NOT NULL default'12345',
  gid smallint(5) unsigned NOT NULL default'12345',
  quota varchar(255) NOT NULL default',
  domain varchar(255) NOT NULL default',
  create_date date NOT NULL default'0000-00-00',
  create_time time NOT NULL default'00:00:00',
  change_date date NOT NULL default'0000-00-00',
  change_time time NOT NULL default'00:00:00',
  last_aclearcase/" target="_blank" >ccess int(10) unsigned NOT NULL default'0',
  passwd_lastchanged int(10) unsigned NOT NULL default'0',
  status tinyint(4) NOT NULL default'1',
  PRIMARY KEY (username),
  KEY status (status)
  ) TYPE=MyISAM;
  #####################end########################
  
  [root@localhost root]# mysql postfix < /work/postfix.sql
  
  C.對表的內容的一些說明.
  
  mysql> use postfix
  Reading table information for completion of table and column names
  You can turn off this feature to get a quicker startup with -A
  
  Database changed
  mysql> show tables;
  +-------------------+
  | Tables_in_postfix |
  +-------------------+
  | alias       |
  | domain      |
  | mailbox      |
  +-------------------+
  3 rows in set (0.00 sec)
  
  mysql> desc alias;
  +----------+--------------+------+-----+---------+-------+
  | Field  | Type     | Null | Key | Default | Extra |
  +----------+--------------+------+-----+---------+-------+
  | username | varchar(255) |   | PRI |     |    |
  | goto   | text     |   |   |     |    |
  | domain  | varchar(255) |   |   |     |    |
  +----------+--------------+------+-----+---------+-------+
  3 rows in set (0.00 sec)
  
  mysql> desc domain;
  +-------------+--------------+------+-----+---------+-------+
  | Field    | Type     | Null | Key | Default | Extra |
  +-------------+--------------+------+-----+---------+-------+
  | domain   | varchar(255) |   | PRI |     |    |
  | description | varchar(255) |   |   |     |    |
  +-------------+--------------+------+-----+---------+-------+
  2 rows in set (0.00 sec)
  
  mysql> desc mailbox;
  +--------------------+----------------------+------+-----+------------+-------+
  | Field       | Type         | Null | Key | Default  | Extra |
  +--------------------+----------------------+------+-----+------------+-------+
  | userid       | varchar(20) binary  |   |   |      |    |
  | username      | varchar(255)     |   | PRI |      |    |
  | password      | varchar(255)     |   |   |      |    |
  | home        | varchar(255)     |   |   |      |    |
  | maildir      | varchar(255)     |   |   |      |    |
  | name        | varchar(255)     |   |   |      |    |
  | uid        | smallint(5) unsigned |   |   | 12345   |    |
  | gid        | smallint(5) unsigned |   |   | 12345   |    |
  | quota       | varchar(255)     |   |   |      |    |
  | domain       | varchar(255)     |   |   |      |    |
  | create_date    | date         |   |   | 0000-00-00 |    |
  | create_time    | time         |   |   | 00:00:00  |    |
  | change_date    | date         |   |   | 0000-00-00 |    |
  | change_time    | time         |   |   | 00:00:00  |    |
  | last_access    | int(10) unsigned   |   |   | 0     |    |
  | passwd_lastchanged | int(10) unsigned   |   |   | 0     |    |
  | status       | tinyint(4)      |   | MUL | 1     |    |
  +--------------------+----------------------+------+-----+------------+-------+
  17 rows in set (0.00 sec)
  
  mysql>
  domain表的解釋:
  domain 任意的domain.
  description 通常是2種 "local:" 用於本地的 or "virtual:" 用於是虛擬的.
  alias表的解釋:
  username 是虛擬的郵箱[email protected] ,將被轉發到goto下的聯結中.
  goto 可以用簡單的名字,如果是本地用戶名,如(root, postmaster, etc.), 或者是整個的郵箱名字,[email protected]
  
  Mailbox表的內容:
  Userid 使用者編碼
  username 可以是user.domain.tld 或 [email protected]
  password 在mysql中用encrypt('password') 輸入.
  name 使用著的真實姓名。
  uid 虛擬用戶身份uid
  gid 組的gid
  home "/usr/local/virtual/". Maildirs必須在此目錄下。
  domain 這欄可以不要,但有些軟件需要他。
  maildir 是整個地址,必須帶”/”結尾。
  Quota 郵箱的空間或信息量的限制。
  status 0 為禁止。"MYSQL_WHERE_CLAUSE= status" .
  
  三、pam-mysql的編譯
  tar pam-mysql-5.0.tar.gz
  make cp pam_mysql.so /lib/security
  
  四、修改pam
  [root@localhost  pam_mysql]#  cd  /etc/pam.d
  [root@localhost  pam.d]#  cp  /work/postfix  postfix
  [root@localhost  pam.d]#
  postfix的內容如下:
  ##########/etc/pam.d/postfix##################
  echo  auth  sufficient  pam_mysql.so  user=postfix  passwd=postfix  host=localhost  db=postfix  table=mailbox  usercolumn=username  passwdcolumn=password  crypt=1
  echo  account  required  pam_mysql.so  user=postfix  passwd=postfix  host=localhost  db=postfix  table=mailbox  usercolumn=username  passwdcolumn=password  crypt=1
  echo  auth  sufficient  pam_unix_auth.so
  echo  account  sufficient  pam_unix_acct.so
  [root@localhost  pam.d]#  [  -f  imap  ]  &&  mv  imap  imap.orig
  [root@localhost  pam.d]#  [  -f  pop  ]  &&  mv  pop  pop.orig
  [root@localhost  pam.d]#  [  -f  smtp  ]  &&  mv  smtp  smtp.orig
  [root@localhost  pam.d]#  n  -s  postfix  imap
  bash:  n:  command  not  found
  [root@localhost  pam.d]#  ln  -s  postfix  imap
  [root@localhost  pam.d]#  ln  -s  postfix  pop
  [root@localhost  pam.d]#  ln  -s  postfix  smtp
  [root@localhost  pam.d]#  cp  postfix  sieve
  [root@localhost  pam.d]#  cd  ..
  
  五、cyrus-sasl的安裝配置
  我本不想重新編譯cyrus-sasl的,可是fedora自帶的在我測試的時候出問題,還是重編譯吧,雖然fedora帶的也是2.1.15版本。
  
  [root@localhost etc]# cd /ruanjian
  [root@localhost ruanjian]# cd cyrus-sasl-2.1.15
  [root@localhost cyrus-sasl-2.1.15]# ./configure --disable-sample > --disable-pwcheck --disable-gssapi --disable-cram > --disable-digest --disable-krb4 --disable-anon > --with-saslauthd=/var/run/saslauthd > --enable-plain --enable-login
  [root@localhost cyrus-sasl-2.1.15]# [ -d /usr/lib/sasl2 ] && mv /usr/lib/sasl2 /usr/lib/sasl2.orig
  [root@localhost cyrus-sasl-2.1.15]# ln -s /usr/local/lib/sasl2 /usr/lib/sasl2
  [root@localhost cyrus-sasl-2.1.15]# echo /usr/local/lib >> /etc/ld.so.conf
  [root@localhost cyrus-sasl-2.1.15]# echo /usr/local/lib/sasl2 >> /etc/ld.so.conf
  [root@localhost cyrus-sasl-2.1.15]# ldconfig
  [root@localhost cyrus-sasl-2.1.15]#
  [root@localhost cyrus-sasl-2.1.15]# echo pwcheck_method: saslauthd > /usr/lib/sasl2/smtpd.conf
  [root@localhost cyrus-sasl-2.1.15]# /usr/local/sbin/saslauthd -a pam
  [root@localhost cyrus-sasl-2.1.15]#
  
  六、postfix的安裝和配置
  A.先停止sendmail.fedora已經默認安裝了sendmail,去掉它。
  
  [root@localhost ruanjian]# /etc/init.d/sendmail stop
  關閉 sendmail:                [失敗]
  [root@localhost ruanjian]# mv /usr/bin/newaliases /usr/bin/newaliases.orig
  [root@localhost ruanjian]# mv /usr/bin/mailq /usr/bin/mailq.orig
  [root@localhost ruanjian]# mv /usr/sbin/sendmail /usr/sbin/sendmail.orig
  
  B.添加用戶和組
  
  [root@localhost ruanjian]# groupadd -g 12345 postfix
  [root@localhost ruanjian]# groupadd -g 12346 postdrop
  [root@localhost ruanjian]# useradd -u 12345 -g 12345 -c postfix -d /dev/null -s /nologin postfix
  
  C.編譯postfix
  
  [root@mail ruanjian]# cd postfix-2.0.16
  [root@mail postfix-2.0.16]# make -f Makefile.init makefiles > 'CCARGS=-DUSE_SASL_AUTH -DHAS_MYSQL -I/usr/include/mysql -I/usr/include/sasl' > 'AUXLIBS=-L/usr/lib/mysql -L/usr/local/lib/sasl2 -lmysqlclient -lsasl2 -lz -lm'
  [root@mail postfix-2.0.16]# make install
  [root@localhost postfix-2.0.16]# cd /etc
  [root@localhost etc]# mv aliases aliases.orig
  [root@localhost etc]# ln -s postfix/aliases aliases
  [root@localhost etc]# cd postfix
  [root@localhost postfix]# echo 'root: tester' >> /etc/postfix/aliases
  [root@localhost postfix]# postalias /etc/postfix/aliases
  [root@localhost postfix]#
  
  D.配置postfix.
  
  1. 主要是main.cf的配置。
  virtual_alias_maps = mysql:/etc/postfix/mysql_virtual_alias_maps.cf
  virtual_gid_maps = static:12345
  virtual_mailbox_base = /usr/local/virtual
  virtual_mailbox_domains = mysql:/etc/postfix/mysql_virtual_domains_maps.cf
  virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf
  virtual_mailbox_limit = 51200000
  virtual_minimum_uid = 12345
  virtual_transport = virtual
  virtual_uid_maps = static:12345
  
  smtpd_sasl_auth_enable = yes
  #smtpd_sasl_local_domain = $myhostname
  smtpd_recipient_restrictions = permit_mynetworks permit_sasl_authenticated permit_auth_destination reject
  smtpd_sasl_security_options = noanonymous
  smtpd_client_restrictions = permit_sasl_authenticated
  broken_sasl_auth_clients = yes
  
  2. mysql_virtual_alias_maps.cf 如下
  
  user = postfix
  password = postfix
  hosts = localhost
  dbname = postfix
  table = alias
  select_field = goto
  where_field = username
  
  3. mysql_virtual_domains_maps.cf 如下
  
  user = postfix
  password = postfix
  hosts = localhost
  dbname = postfix
  table = domain
  select_field = description
  where_field = domain
  
  4. mysql_virtual_mailbox_maps.cf 如下
  
  user = postfix
  password = postfix
  hosts = localhost
  dbname = postfix
  table = mailbox
  select_field = maildir
  where_field = username
  
  E.建立
  
  [root@localhost root]# mkdir /usr/local/virtual
  [root@localhost root]# chown -R postfix:postfix /usr/local/virtual
  [root@localhost root]# chmod -R 751 /usr/local/virtual
  
  七、測試端口25
  A. mysql> use postfix
  Reading table information for completion of table and column names
  You can turn off this feature to get a quicker startup with -A
  
  Database changed
  mysql> show tables;
  +----------+
  | Tables_in_postfix |
  +----------+
  | alias       |
  | domain      |
  | mailbox      |
  +----------+
  3 rows in set (0.00 sec)
  
  mysql> INSERT INTO alias (username,goto) VALUES ('[email protected]','[email protected]');
  Query OK, 1 row affected (0.08 sec)
  
  mysql> INSERT INTO domain (domain,description) VALUES ('lansemail.com','local:'); Query OK, 1 row affected (0.00 sec)
  
  mysql> INSERT INTO mailbox (username,password,home,maildir,name,domain,create_date,create_time) VALUES('tester','$1$pi.WVgBx$a3dUCzBnbY76jnZlqWQCQ/','/usr/local/virtual/','[email protected]/','tester','lansemail.com','2003-11-19','21:24:00'); Query OK, 1 row affected (0.03 sec)
  
  mysql> select * from mailbox
  -> ;
  +----+-----+---+----+-----+----+----+--+----+--+-+-+----+------+-------+----------+----+
  | userid | username | password              | home        | maildir        | name  | uid  | gid  | quota | domain    | create_date | create_time | change_date | change_time | last_access | passwd_lastchanged | status |
  +----+-----+------------------+-----------+------------+----+----+----+----+--------+-------+-------+-------+-------+-------+----------+----+
  |    | tester  | $1$pi.WVgBx$a3dUCzBnbY76jnZlqWQCQ/ | /usr/local/virtual/ | [email protected]/ | tester | 12345 | 12345 |    | lansemail.com | 2003-11-19 | 21:24:00  | 0000-00-00 | 00:00:00  |      0 |         0 |   1 |
  +----+-----+------------------+-----------+------------+----+----+----+----+--------+-------+-------+-------+-------+-------+----------+----+
  1 row in set (0.11 sec)
  
  mysql>
  
  這裡用的username為tester,只是用來測試端口用的,正式使用一定要用全名[email protected]. 否則信件收不到,信息如下
  
  無法到達以下收件人:
  
  '[email protected]',時間為 21/11/2003 13:05
  450 <[email protected]>: User unknown in local recipient table
  
  B.[root@localhost postfix]# telnet localhost 25
  
  
  Trying 127.0.0.1...
  Connected to localhost.
  Escape character is '^]'.
  220 localhost.localdomain ESMTP Postfix
  ehlo local host
  250-localhost.localdomain
  250-PIPELINING
  250-SIZE 10240000
  250-VRFY
  250-ETRN
  250-AUTH LOGIN PLAIN OTP
  250-AUTH=LOGIN PLAIN OTP
  250-XVERP
  250 8BITMIME
  auth plain dGVzdGVyAHRlc3RlcgB0ZXN0cHc=
  235 Authentication successful
  quit
  221 Bye
  Connection closed by foreign host.
  [root@localhost postfix]# telnet localhost 25
  Trying 127.0.0.1...
  Connected to localhost.
  Escape character is '^]'.
  220 localhost.localdomain ESMTP Postfix
  ehlo localhost
  250-localhost.localdomain
  250-PIPELINING
  250-SIZE 10240000
  250-VRFY
  250-ETRN
  250-AUTH LOGIN PLAIN OTP
  250-AUTH=LOGIN PLAIN OTP
  250-XVERP
  250 8BITMIME
  auth login
  334 VXNlcm5hbWU6
  dGVzdGVy  -->username: tester(用printf 'tester' | mmencode取得。)
  334 UGFzc3dvcmQ6
  dGVzdHB3  -->password: testpw
  235 Authentication successful
  quit
  221 Bye
  Connection closed by foreign host.
  [root@localhost postfix]#
  
  八、Courier-IMAP Install
  A. 先在普通用戶下
  
  #./configure -without-authpwd -without-authpam -without-authuserdb -without-authshadow
  -without-cram -without-chkpw -without-ldap -without-pgsql -without-authdaemon
  -without-authcustom -with-authmysql
  #make
  #make check
  #su root
  #make install
  #make install-configure
  
  B.修改authmysqlrc,這裡是簡單的
  
  #DEFAULT_DOMAIN domain.tld
  MYSQL_CRYPT_PWFIELD password
  MYSQL_DATABASE postfix
  MYSQL_GID_FIELD '12345'
  MYSQL_HOME_FIELD '/usr/local/virtual'
  MYSQL_LOGIN_FIELD username
  MYSQL_MAILDIR_FIELD maildir
  MYSQL_NAME_FIELD name
  MYSQL_OPT 0
  MYSQL_PASSWORD postfix
  #MYSQL_PORT 3306
  #MYSQL_QUOTA_FIELD quota
  MYSQL_SERVER localhost
  MYSQL_SOCKET /var/lib/mysql/mysql.sock
  MYSQL_UID_FIELD '12345'
  MYSQL_USERNAME postfix
  MYSQL_USER_TABLE mailbox
  #MYSQL_WHERE_CLAUSE status
  
  C. [root@localhost root]# /usr/lib/courier-imap/bin/maildirmake
  
  /usr/local/virtual/[email protected]
  [root@localhost root]# chmod -R 700 /usr/local/virtual/[email protected]
  [root@localhost root]# chown -R postfix:postfix /usr/local/virtual/[email protected]
  D. 啟動courier-imap
  [root@localhost root]# /usr/lib/courier-imap/libexec/pop3d.rc start
  [root@localhost root]# /usr/lib/courier-imap/libexec/imapd.rc start
  [root@localhost root]# pstree | grep courier
  |-2*[courierlogger]
  |-2*[couriertcpd]
  [root@localhost root]# netstat -an | grep LISTEN
  tcp    0   0 0.0.0.0:32770      0.0.0.0:*        LISTEN
  tcp    0   0 127.0.0.1:32771     0.0.0.0:*        LISTEN
  tcp    0   0 0.0.0.0:3306      0.0.0.0:*        LISTEN
  tcp    0   0 0.0.0.0:110       0.0.0.0:*        LISTEN
  tcp    0   0 0.0.0.0:143       0.0.0.0:*        LISTEN
  tcp    0   0 0.0.0.0:111       0.0.0.0:*        LISTEN
  tcp    0   0 0.0.0.0:22       0.0.0.0:*        LISTEN
  tcp    0   0 127.0.0.1:631      0.0.0.0:*        LISTEN
  tcp    0   0 0.0.0.0:25       0.0.0.0:*        LISTEN
  
  九、測試端口110
  [root@localhost root]# telnet localhost 110 Trying 127.0.0.1...
  Connected to localhost.
  Escape character is '^]'.
  +OK Hello there.
  user tester
  +OK Password required.
  pass testpw
  +OK logged in.
  list
  +OK POP3 clients that break here, they violate STD53.
  .
  quit
  +OK Bye-bye.
  Connection closed by foreign host.
  [root@localhost root]#
  
  十、用你的outlook試試。。。。記住,添加用戶郵箱,一定要用全名。再去找個webmail吧。

Copyright © Linux教程網 All Rights Reserved