#!/bin/bash
#vsftpd install script
IP=$(ifconfig eth0 | grep -i "bcast" | awk -F: '{print $2}' | awk '{print $1}')
PORT=2525
VUSER=vuser
FTPUSER=webadm
FTPPW=mypasswd:123.com
ROOTDIR=/test
#iptables selinux
if [ -s /etc/selinux/config ] && grep "SELINUX=enforcing" /etc/selinux/config;
then
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
fi
service iptables stop
chkconfig iptables off
#start install
yum -y install vsftpd db4-utils
cd /etc/vsftpd
cat >> vuser.txt << EOF
$FTPUSER
$FTPPW
EOF
db_load -T -t hash -f vuser.txt vuser.db
chmod 600 vuser.*
cat >> /etc/pam.d/vuser << EOF
#%PAM-1.0
auth required pam_userdb.so db=/etc/vsftpd/vuser
account required pam_userdb.so db=/etc/vsftpd/vuser
EOF
useradd -s /sbin/nologin $VUSER
mv /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.bak
cat >> /etc/vsftpd/vsftpd.conf << EOF
#global config
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=YES
listen_address=$IP
listen_port=$PORT
pasv_enable=YES
pasv_min_port=24500
pasv_max_port=24600
max_clients=20
max_per_ip=5
anon_umask=022
#vuser config
pam_service_name=vuser
userlist_enable=YES
tcp_wrappers=YES
guest_enable=YES
guest_username=$VUSER
user_config_dir=/etc/vsftpd/user_dir
EOF
mkdir $ROOTDIR
/usr/bin/setfacl -R -m d:user:$VUSER:rwx $ROOTDIR
mkdir /etc/vsftpd/user_dir
cat >> /etc/vsftpd/user_dir/$FTPUSER << EOF
local_root=$ROOTDIR
anon_upload_enable=YES
anon_world_readable_only=NO
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
EOF
chkconfig vsftpd on
service vsftpd restart