#!/bin/bash
yum install vsftpd pam pam-* db4 db4-* -y
cd /etc/vsftpd/
touch virtual_login
read -p "请输入用户名:" username
read -p "请输入密码:" password
echo $username >> virtual_login
echo $password >> virtual_login
db_load -T -t hash -f /etc/vsftpd/virtual_login /etc/vsftpd/virtual_login.db
sed -ir 's/^/#/g' /etc/pam.d/vsftpd
echo -n ' 
auth    required   /lib64/security/pam_userdb.so   db=/etc/vsftpd/virtual_login 
account required   /lib64/security/pam_userdb.so   db=/etc/vsftpd/virtual_login 
' >> /etc/pam.d/vsftpd
useradd -s /sbin/nologin vsftpd
useradd -d /var/ftp/$username -s /sbin/nologin $username
cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.bak
awk '! /^(#|$)/' /etc/vsftpd/vsftpd.conf.bak
echo -n '
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
anon_upload_enable=NO
anon_mkdir_write_enable=NO
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
chown_uploads=NO
xferlog_file=/var/log/vsftpd.log
xferlog_std_format=YES
nopriv_user=vsftpd
async_abor_enable=YES
ascii_upload_enable=YES
ascii_download_enable=YES
ftpd_banner=Welcome to $username FTP service
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list
listen=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
guest_enable=YES
virtual_use_local_privs=YES
user_config_dir=/etc/vsftpd/vsftpd_config
'>/etc/vsftpd/vsftpd.conf
mkdir /etc/vsftpd/vsftpd_config/
touch /var/log/vsftpd.log
chmod 600 /var/log/vsftpd.log
chown vsftpd.vsftpd /var/log/vsftpd.log
touch /etc/vsftpd/chroot_list
cd /etc/vsftpd/vsftpd_config/
cat > $username <<EOF
guest_enable=yes
guest_username=$username
local_root=/var/ftp/$username
idle_session_timeout=600
data_connection_timeout=120
max_clients=10
max_per_ip=5
local_max_rate=0
EOF
sed -ir 's/[ ]*$//g'  /etc/vsftpd/vsftpd.conf
sed -ir 's/[ ]*$//g'  /etc/vsftpd/vsftpd_config/$username
echo $username >> /etc/vsftpd/chroot_list
setsebool ftp_home_dir 1
/etc/init.d/vsftpd start
