Cách ảo hóa tài khoản người dùng và tên miền ?

nbbnbmn
nbbnbmn
Trả lời 13 năm trước

Kết quả của Postfix server là khả năng hỗ trợ cơ chế xác thực SMTP-AUTH, TLS và quota (quota không được tích hợp cùng với Postfix ở chế độ mặc định), trong đó mật khẩu sẽ được lưu trữ dưới mẫu encrypted trong cơ sở dữ liệu. Bên cạnh đó, bài viết cũng sẽ chỉ ra cách cài đặt Amavisd, SpamAssassin và ClamAV để quét email và phát hiện các hiểm họa đi kèm.

Lưu ý sơ bộ

Hệ thống sử dụng ở đây dựa trên nền tảng Fedora 13 server x86_64, có địa chỉ IP tĩnh192.168.0.100 và hostname là server1.example.com. Thêm vào đó, tắt bỏ tính năng firewall và SELinux.

Cài đặt 1 số phần mềm yêu cầu

Trước tiên, chúng ta cần cập nhật 1 số gói ứng dụng trên hệ thống:

yum update

yum groupinstall 'Development Tools'


yum groupinstall 'Development Libraries'

Cài đặt Apache, MySQL, phpMyAdmin

Chỉ với 1 dòng lệnh duy nhất, có bao gồm các gói cần thiết để xây dựng hệ thống Courier-IMAP sau này:

yum install ntp httpd mysql-server php php-mysql php-mbstring rpm-build gcc mysql-devel openssl-devel cyrus-sasl-devel pkgconfig zlib-devel phpMyAdmin pcre-devel openldap-devel postgresql-devel expect libtool-ltdl-devel openldap-servers libtool gdbm-devel pam-devel gamin-devel

Cài đặt Courier-IMAP, Courier-Authlib, và Maildrop

Thật bất tiện khi không có gói rpm dành cho Courier-IMAP, Courier-Authlib, và Maildrop, do đó chúng ta cần tự xây dựng mã nguồn. Chú ý rằng các gói RPM không nên được dựng bằng tài khoản root, courier-imap sẽ tự động tắt bỏ khỏi quá trình biên dịch nếu phát hiện trình biên dịch đang thực hiện bằng tài khoản root. Do đó, chúng ta cần tạo 1 tài khoản bình thường (trong ví dụ này là falko) và tạo mật khẩu đi kèm:

useradd -m -s /bin/bash falko
passwd falko

Chúng ta sẽ cần đến cú pháp sudo sau này, để tài khoản falco có thể biên dịch và cài đặt các gói rpm. Nhưng trước tiên, chúng ta phải cho phép tài khoản falko để chạy tất cả câu lệnh sử dụng sudo:

visudo

Trong file vừa mở ra, sẽ có dòng root ALL=(ALL) ALL. Hãy thêm 1 dòng tương tự dành cho falko ngay bên dưới:

[...]
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
falko ALL=(ALL) ALL
[...]

Và bây giờ, chúng ta đã sẵn sàng xây dựng các gói rpm. Trước tiên, đăng nhập vào tài khoản falko:

su falko

Tạo các biến môi trường:

mkdir $HOME/rpm
mkdir $HOME/rpm/SOURCES
mkdir $HOME/rpm/SPECS
mkdir $HOME/rpm/BUILD
mkdir $HOME/rpm/BUILDROOT
mkdir $HOME/rpm/SRPMS
mkdir $HOME/rpm/RPMS
mkdir $HOME/rpm/RPMS/i386
mkdir $HOME/rpm/RPMS/x86_64

echo "%_topdir $HOME/rpm" >> $HOME/.rpmmacros

Tạo tiếp thư mục downloads và tải bộ mã nguồn từ đây:

mkdir $HOME/downloads
cd $HOME/downloads


wget https://sourceforge.net/projects/courier/files/authlib/0.63.0/courier-authlib-0.63.0.tar.bz2/download
wget https://sourceforge.net/projects/courier/files/imap/4.8.0/courier-imap-4.8.0.tar.bz2/download
wget https://sourceforge.net/projects/courier/files/maildrop/2.5.0/maildrop-2.5.0.tar.bz2/download

Vẫn ở trong thư mục $HOME/downloads, xây dựng gói courier-authlib:

sudo rpmbuild -ta courier-authlib-0.63.0.tar.bz2

Sau đó, gói rpm sẽ có trong $HOME/rpm/RPMS/x86_64 ($HOME/rpm/RPMS/i386 nếu bạn sử dụng i386):

cd $HOME/rpm/RPMS/x86_64

Sử dụng lệnh:

ls -l

để liệt kê các gói rpm thích hợp:

[falko@server1x86_64]$ls-l
total516
-rw-r--r--1rootroot124304Jun1017:48courier-authlib-0.63.0-1.fc13.x86_64.rpm
-rw-r--r--1rootroot258896Jun1017:48courier-authlib-debuginfo-0.63.0-1.fc13.x86_64.rpm
-rw-r--r--1rootroot35064Jun1017:48courier-authlib-devel-0.63.0-1.fc13.x86_64.rpm
-rw-r--r--1rootroot17424Jun1017:48courier-authlib-ldap-0.63.0-1.fc13.x86_64.rpm
-rw-r--r--1rootroot13956Jun1017:48courier-authlib-mysql-0.63.0-1.fc13.x86_64.rpm
-rw-r--r--1rootroot13120Jun1017:48courier-authlib-pgsql-0.63.0-1.fc13.x86_64.rpm
-rw-r--r--1rootroot8328Jun1017:48courier-authlib-pipe-0.63.0-1.fc13.x86_64.rpm
-rw-r--r--1rootroot34160Jun1017:48courier-authlib-userdb-0.63.0-1.fc13.x86_64.rpm
[falko@server1x86_64]$

Chọn 1 gói thích hợp và cài đặt như sau:

sudo rpm -ivh courier-authlib-0.63.0-1.fc13.x86_64.rpm courier-authlib-mysql-0.63.0-1.fc13.x86_64.rpm courier-authlib-devel-0.63.0-1.fc13.x86_64.rpm

Tiếp theo, quay trở lại thư mục downloads:

cd $HOME/downloads

và chạy lệnh rpmbuild lại lần nữa, nhưng không có sudo nếu không quá trình biên dịch sẽ thất bại:

rpmbuild -ta courier-imap-4.8.0.tar.bz2

Sau quá trình này, gói rpm sẽ được tìm thấy trong thư mục HOME/rpm/RPMS/x86_64 ($HOME/rpm/RPMS/i386 nếu bạn đang sử dụng hệ thống i386):

cd $HOME/rpm/RPMS/x86_64

Dùng lệnh:

ls -l

sẽ liệt kê ra các gói rpm phù hợp:

[falko@server1x86_64]$ls-l
total2300
-rw-r--r--1rootroot124304Jun1017:48courier-authlib-0.63.0-1.fc13.x86_64.rpm
-rw-r--r--1rootroot258896Jun1017:48courier-authlib-debuginfo-0.63.0-1.fc13.x86_64.rpm
-rw-r--r--1rootroot35064Jun1017:48courier-authlib-devel-0.63.0-1.fc13.x86_64.rpm
-rw-r--r--1rootroot17424Jun1017:48courier-authlib-ldap-0.63.0-1.fc13.x86_64.rpm
-rw-r--r--1rootroot13956Jun1017:48courier-authlib-mysql-0.63.0-1.fc13.x86_64.rpm
-rw-r--r--1rootroot13120Jun1017:48courier-authlib-pgsql-0.63.0-1.fc13.x86_64.rpm
-rw-r--r--1rootroot8328Jun1017:48courier-authlib-pipe-0.63.0-1.fc13.x86_64.rpm
-rw-r--r--1rootroot34160Jun1017:48courier-authlib-userdb-0.63.0-1.fc13.x86_64.rpm
-rw-r--r--1falkofalko632296Jun1017:58courier-imap-4.8.0-1.13.x86_64.rpm
-rw-r--r--1falkofalko1188976Jun1017:58courier-imap-debuginfo-4.8.0-1.13.x86_64.rpm
[falko@server1x86_64]$

Và cài đặt courier-imap như sau:

sudo rpm -ivh courier-imap-4.8.0-1.13.x86_64.rpm

Quay trở lại thư mục downloads:

cd $HOME/downloads

tiếp tục chạy lệnh rpmbuild, lần này là để xây dựng gói maildrop:

sudo rpmbuild -ta maildrop-2.5.0.tar.bz2

Khi hoàn thành, gói rpm sẽ có trong thư mục $HOME/rpm/RPMS/x86_64 ($HOME/rpm/RPMS/i386 nếu bạn sử dụng hệ thống i386):

cd $HOME/rpm/RPMS/x86_64

Gõ lệnh:

ls -l

để liệt kê các gói phù hợp:

[falko@server1x86_64]$ls-l
total5400
-rw-r--r--1rootroot124304Jun1017:48courier-authlib-0.63.0-1.fc13.x86_64.rpm
-rw-r--r--1rootroot258896Jun1017:48courier-authlib-debuginfo-0.63.0-1.fc13.x86_64.rpm
-rw-r--r--1rootroot35064Jun1017:48courier-authlib-devel-0.63.0-1.fc13.x86_64.rpm
-rw-r--r--1rootroot17424Jun1017:48courier-authlib-ldap-0.63.0-1.fc13.x86_64.rpm
-rw-r--r--1rootroot13956Jun1017:48courier-authlib-mysql-0.63.0-1.fc13.x86_64.rpm
-rw-r--r--1rootroot13120Jun1017:48courier-authlib-pgsql-0.63.0-1.fc13.x86_64.rpm
-rw-r--r--1rootroot8328Jun1017:48courier-authlib-pipe-0.63.0-1.fc13.x86_64.rpm
-rw-r--r--1rootroot34160Jun1017:48courier-authlib-userdb-0.63.0-1.fc13.x86_64.rpm
-rw-r--r--1falkofalko632296Jun1017:58courier-imap-4.8.0-1.13.x86_64.rpm
-rw-r--r--1falkofalko1188976Jun1017:58courier-imap-debuginfo-4.8.0-1.13.x86_64.rpm
-rw-r--r--1rootroot1759056Jun1018:06maildrop-2.5.0-1.13.x86_64.rpm
-rw-r--r--1rootroot1243400Jun1018:06maildrop-debuginfo-2.5.0-1.13.x86_64.rpm
-rw-r--r--1rootroot99764Jun1018:06maildrop-devel-2.5.0-1.13.x86_64.rpm
-rw-r--r--1rootroot62536Jun1018:06maildrop-man-2.5.0-1.13.x86_64.rpm
[falko@server1x86_64]$

Và cài đặt maildrop như sau:

sudo rpm -ivh maildrop-2.5.0-1.13.x86_64.rpm

Sau khi hoàn tất các quá trình trên, đăng nhập vào hệ thống bằng tài khoản root:

exit

Áp dụng bản vá Quote với Postfix

Ở đây, chúng ta sẽ phải tìm cách patch Quota với Postfix theo cách sau: tải mã nguồn, áp dụng bản vá, xây dựng gói cài đặt rpm của Postfix và cài đặt:

cd /usr/src
wget http://ftp-stud.fht-esslingen.de/pub/Mirrors/fedora/linux/releases/13/Fedora/source/SRPMS/postfix-2.7.0-1.fc13.src.rpm
rpm -ivh postfix-2.7.0-1.fc13.src.rpm

Câu lệnh cuối cùng sẽ chỉ ra 1 vài cảnh báo, bạn có thể bỏ qua những cảnh báo này:

warning: user mockbuild does not exist - using root
warning: group mockbuild does not exist - using root

cd /root/rpmbuild/SOURCES

wget http://vda.sourceforge.net/VDA/postfix-vda-2.7.0.patch
cd /root/rpmbuild/SPECS/

Tiếp theo, chỉnh sửa file postfix.spec:

vi postfix.spec

Thêm cú pháp Patch0: postfix-vda-2.7.0.patch vào vị trí # Patches, và %patch0 -p1 -b .vda vào %setup -q:

[...]
# Patches

Patch0: postfix-vda-2.7.0.patch

Patch1: postfix-2.7.0-config.patch
Patch2: postfix-2.6.1-files.patch
Patch3: postfix-alternatives.patch
Patch8: postfix-large-fs.patch
Patch9: pflogsumm-1.1.2-datecalc.patch
[...]
%prep
%setup -q
# Apply obligatory patches
%patch0 -p1 -b .vda
%patch1 -p1 -b .config
%patch2 -p1 -b .files
%patch3 -p1 -b .alternatives
%patch8 -p1 -b .large-fs
[...]

Sau đó, xây dựng gói cài đặt rpm của Postfix với quota và MySQL hỗ trợ:

rpmbuild -ba postfix.spec

Gói rpm cài đặt Postfix được tạo ra ở thư mục /root/rpmbuild/RPMS/x86_64 (/root/rpmbuild/RPMS/i386 nếu bạn sử dụng hệ thống i386):

cd /root/rpmbuild/RPMS/x86_64

Sử dụng lệnh:

ls -l

để liệt kê các gói phù hợp:

[root@server1x86_64]#ls-l
total8804
-rw-r--r--1rootroot2197708Jun1018:17postfix-2.7.0-1.fc13.x86_64.rpm
-rw-r--r--1rootroot6746304Jun1018:17postfix-debuginfo-2.7.0-1.fc13.x86_64.rpm
-rw-r--r--1rootroot61460Jun1018:17postfix-perl-scripts-2.7.0-1.fc13.x86_64.rpm
[root@server1x86_64]#

Chọn gói Postfix phù hợp và cài đặt bằng lệnh sau:

rpm -ivh postfix-2.7.0-1.fc13.x86_64.rpm

Thiết lập mật khẩu cho MySQL và thiết lập phpMyAdmin

Khởi động MySQL:

chkconfig --levels 235 mysqld on
/etc/init.d/mysqld start

Thiết lập mật khẩu cho tài khoản MySQL root:

mysql_secure_installation

[root@server1~]#mysql_secure_installation





NOTE:RUNNINGALLPARTSOFTHISSCRIPTISRECOMMENDEDFORALLMySQL

SERVERSINPRODUCTIONUSE!PLEASEREADEACHSTEPCAREFULLY!


InordertologintoMySQLtosecureit,we'llneedthecurrent

passwordfortherootuser.Ifyou'vejustinstalledMySQL,and
youhaven'tsettherootpasswordyet,thepasswordwillbeblank,
soyoushouldjustpressenterhere.

Entercurrentpasswordforroot(enterfornone):<--ENTER

OK,successfullyusedpassword,movingon...

SettingtherootpasswordensuresthatnobodycanlogintotheMySQL

rootuserwithouttheproperauthorisation.

Setrootpassword?[Y/n]<--ENTER

Newpassword:← đặt mật khẩu root sql
Re-enternewpassword:<-- đặt mật khẩu root sql
Passwordupdatedsuccessfully!
Reloadingprivilegetables..
...Success!


Bydefault,aMySQLinstallationhasananonymoususer,allowinganyone

tologintoMySQLwithouthavingtohaveauseraccountcreatedfor
them.Thisisintendedonlyfortesting,andtomaketheinstallation
goabitsmoother.Youshouldremovethembeforemovingintoa
productionenvironment.

Removeanonymoususers?[Y/n]<--ENTER

...Success!

Normally,rootshouldonlybeallowedtoconnectfrom'localhost'.This

ensuresthatsomeonecannotguessattherootpasswordfromthenetwork.

Disallowrootloginremotely?[Y/n]<--ENTER

...Success!

Bydefault,MySQLcomeswithadatabasenamed'test'thatanyonecan

access.Thisisalsointendedonlyfortesting,andshouldberemoved
beforemovingintoaproductionenvironment.

Removetestdatabaseandaccesstoit?[Y/n]<--ENTER

-Droppingtestdatabase...
...Success!
-Removingprivilegesontestdatabase...
...Success!

Reloadingtheprivilegetableswillensurethatallchangesmadesofar

willtakeeffectimmediately.

Reloadprivilegetablesnow?[Y/n]<--ENTER

...Success!

Cleaningup...




Alldone!Ifyou'vecompletedalloftheabovesteps,yourMySQL

installationshouldnowbesecure.

ThanksforusingMySQL!



[root@server1~]#


Tiếp theo, chúng ta phải thiết lập phpMyAdmin, thay đổi Apache để phpMyAdmin chấp nhận các kết nối không chỉ từ localhost:

vi /etc/httpd/conf.d/phpMyAdmin.conf

# phpMyAdmin - Web based MySQL browser written in php

#
# Allows only localhost by default
#
# But allowing phpMyAdmin to anyone other than localhost should be considered
# dangerous unless properly secured by SSL

Alias /phpMyAdmin /usr/share/phpMyAdmin

Alias /phpmyadmin /usr/share/phpMyAdmin
#<Directory /usr/share/phpMyAdmin/>
# order deny,allow
# deny from all
# allow from 127.0.0.1
# allow from ::1
#</Directory>

# This directory does not require access over HTTP - taken from the original

# phpMyAdmin upstream tarball
#
<Directory /usr/share/phpMyAdmin/libraries>
Order Deny,Allow
Deny from All
Allow from None
</Directory>

# This configuration prevents mod_security at phpMyAdmin directories from

# filtering SQL etc. This may break your mod_security implementation.
#
#<IfModule mod_security.c>
# <Directory /usr/share/phpMyAdmin>
# SecRuleInheritance Off
# </Directory>
#</IfModule>

Tạo đường dẫn khởi động cho Apache và kích hoạt:

chkconfig --levels 235 httpd on
/etc/init.d/httpd start

Mở trình duyệt và gõ địa chỉ http://server1.example.com/phpMyAdmin/ hoặc http://192.168.0.100/phpMyAdmin/, đăng nhập bằng tài khoản root và mật khẩu MySQL khai báo ở phía trên.