#!/bin/bash
#Create By  : www.rootop.org
#MailAddress: venus@rootop.org
#Version    : install_vsftpd1.0
#Data       : 2014/10/30
#Desc       : for yum version
#Tested     : CentOS6

# install vsftpd service
yum install -y vsftpd
clear
echo -e "\033[31m vsftpd service install done\033[0m"
echo "----------------------------------------------"
# add system user map to virtual user
#useradd -s /sbin/nologin vuser

cd /etc/vsftpd/
cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.bak

read -p "pls input a username: " user
echo "----------------------------------------------"
read -p "pls input a password: " pass
echo "----------------------------------------------"
echo -e "\033[31m whitch directory would u wanna to lock in? input full path. for example: /usr/local/nginx/html\033[0m"
echo -e "\033[31m the last character don't be end with /. use ctrl+c to go back command line. \033[0m"
echo "----------------------------------------------"
read -p "pls input a full path: " dire
echo "----------------------------------------------"
echo "your username is \"$user\" ,password is \"$pass\" , directory is \"$dire\" ."
echo "----------------------------------------------"

echo "initializaton vsftpd configuraton"
echo "----------------------------------------------"
# add virtual user from user in put
echo $user > vuser && echo $pass >> vuser

# create database file
db_load -T -t hash -f /etc/vsftpd/vuser /etc/vsftpd/vuser.db

# open virtual user
echo guest_enable=yes >> vsftpd.conf
# set map to system user
echo guest_username=www >> vsftpd.conf
# set vuser profile directory
echo user_config_dir=/etc/vsftpd/user_config >> vsftpd.conf
# set passive modu and port
echo pasv_enable=YES >> vsftpd.conf
echo pasv_min_port=30000 >> vsftpd.conf
echo pasv_max_port=30100 >> vsftpd.conf

# set pam
mv /etc/pam.d/vsftpd /etc/pam.d/vsftpd.bak
touch /etc/pam.d/vsftpd

if [ "$(uname -a | grep x86_64 | wc -l)" == "1" ]; then

cat >/etc/pam.d/vsftpd<<EOF
auth          required /lib64/security/pam_userdb.so db=/etc/vsftpd/vuser
account       required /lib64/security/pam_userdb.so db=/etc/vsftpd/vuser
EOF

	else
cat >/etc/pam.d/vsftpd<<EOF
auth          required /lib/security/pam_userdb.so db=/etc/vsftpd/vuser
account       required /lib/security/pam_userdb.so db=/etc/vsftpd/vuser
EOF
fi
# make directory for vuser
mkdir user_config
cd user_config
touch $user

cat >$user<<EOF
local_root=$dire
download_enable=yes
anon_upload_enable=yes
anon_mkdir_write_enable=yes
anon_other_write_enable=yes
local_umask=022
anon_umask=022
file_open_mode=0755
EOF

chown -R www:www $dire 
	if [ "$(echo $?)" == "0" ]; then
		echo "change permission for $dire done"
		echo "----------------------------------------------"
	else
		echo -e "\033[31m change permission for $dire false\033[0m"
		echo -e "\033[31m install ignore\033[0m"
		exit
	fi

echo "config vsftpd done"
echo "----------------------------------------------"
if [ "$(ps aux | grep vsftpd | grep -v "grep" | wc -l)" == "1" ]; then
	service httpd restart && chkconfig vsftpd on
	echo "----------------------------------------------"
else
service vsftpd start && chkconfig vsftpd on
echo "----------------------------------------------"
fi
echo -e "\033[31m if your iptables is open ,pls accept 21,30000-30100 port\033[0m"
echo "----------------------------------------------"
echo "vsftpd install done"
echo "----------------------------------------------"
exit
