#!/bin/bash
#本脚本适用于CentOS/Redhat(release5&6版本)
#
#ftpadmin 密码请查看/root/ftppasswd.txt
#
vsftpd=/etc/vsftpd/vsftpd.conf
ftppasswd=CHANGE_ME /dev/urandom | head -n 10 | md5sum | head -c 10`
echo "it-arch Auto Install Vsftp for CentOS"
echo
echo "##########################################"
echo "Auto Install Vsftp, only use new Server ##"
echo "Press Ctrl + C to cancel                ##"
echo "Any key to continue                     ##"
echo "##########################################"
read -n 1 
#Network Test
ping -c 1 -t 1 www.baidu.com >/dev/null 2>&1
if [[ $? == 2 ]]; then
    echo "nameserver 114.114.114.114
nameserver 8.8.8.8" > /etc/resolv.conf
fi
ping -c 1 -t 1 www.baidu.com >/dev/null 2>&1
if [[ $? == 2 ]]; then
    echo "Network problems,Please check!"
    exit
fi
OS=`cat /etc/redhat-release |awk ' {print $1$(NF-1)}'|cut -d . -f 1`
modify_rhel5_yum()
{
cat >/etc/yum.repos.d/CentOS-Base.repo  <<EOF
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the 
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-\$releasever - Base - 163.com
baseurl=http://mirrors.163.com/centos/5/os/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=\$releasever&arch=\$basearch&repo=os
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5

#released updates 
[updates]
name=CentOS-\$releasever - Updates - 163.com
baseurl=http://mirrors.163.com/centos/5/updates/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=\$releasever&arch=\$basearch&repo=updates
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5

#packages used/produced in the build but not released
[addons]
name=CentOS-\$releasever - Addons - 163.com
baseurl=http://mirrors.163.com/centos/5/addons/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=\$releasever&arch=\$basearch&repo=addons
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5

#additional packages that may be useful
[extras]
name=CentOS-\$releasever - Extras - 163.com
baseurl=http://mirrors.163.com/centos/5/extras/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=\$releasever&arch=\$basearch&repo=extras
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-\$releasever - Plus - 163.com
baseurl=http://mirrors.163.com/centos/5/centosplus/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=\$releasever&arch=\$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5

#contrib - packages by Centos Users
[contrib]
name=CentOS-\$releasever - Contrib - 163.com
baseurl=http://mirrors.163.com/centos/5/contrib/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=\$releasever&arch=\$basearch&repo=contrib
gpgcheck=1
enabled=0
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
EOF
yum clean metadata
yum makecache
cd ~
}

modify_rhel6_yum()
{
cat >/etc/yum.repos.d/CentOS-Base.repo  <<EOF
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the 
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-\$releasever - Base - 163.com
baseurl=http://mirrors.163.com/centos/6/os/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=\$releasever&arch=\$basearch&repo=os
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

#released updates 
[updates]
name=CentOS-\$releasever - Updates - 163.com
baseurl=http://mirrors.163.com/centos/6/updates/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=\$releasever&arch=\$basearch&repo=updates
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

#additional packages that may be useful
[extras]
name=CentOS-\\$releasever - Extras - 163.com
baseurl=http://mirrors.163.com/centos/6/extras/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=\$releasever&arch=\$basearch&repo=extras
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-\\$releasever - Plus - 163.com
baseurl=http://mirrors.163.com/centos/6/centosplus/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=\$releasever&arch=\$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

#contrib - packages by Centos Users
[contrib]
name=CentOS-\\$releasever - Contrib - 163.com
baseurl=http://mirrors.163.com/centos/6/contrib/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=\$releasever&arch=\$basearch&repo=contrib
gpgcheck=1
enabled=0
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6
EOF
yum clean metadata
yum makecache
cd ~
}
case "$OS" in
Red5|CentOS5)
modify_rhel5_yum
;;
Red6|CentOS6)
modify_rhel6_yum
;;
*)
echo "Unknown system"
;;
esac
#Install Vsftp
case "$OS" in
Red5|CentOS5)
yum install -y vsftpd &> /tmp/vsftp_install
;;
Red6|CentOS6)
yum install -y vsftpd &> /tmp/vsftp_install
;;
*)
echo "Unknown system"
;;
esac
yum install -y vsftpd &> /tmp/vsftp_install
mv $vsftpd $vsftpd.bak
cat > $vsftpd <<EOF
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
ascii_upload_enable=YES
ascii_download_enable=YES
ftpd_banner=Welcome to blah FTP service.
chroot_local_user=YES
#chroot_list_enable=YES
# (default follows)
#chroot_list_file=/etc/vsftpd/chroot_list
listen=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
EOF

chmod 600 $vsftpd
service vsftpd start
chkconfig vsftpd on
service iptables stop
#Add firewall rules
#iptables -A INPUT -p tcp --dport 22 -j ACCEPT
#iptables -A INPUT -p tcp --dport 21 -j ACCEPT
#iptables -A INPUT -p tcp --dport 20 -j ACCEPT
#/etc/init.d/iptables save
#Disabled SELINUX
sed -i "s/SELINUX=enforcing/SELINUX=disabled/"  /etc/selinux/config
setenforce 0
mkdir  -p /var/www/html
useradd  -d /var/www/html -s /sbin/nologin  ftpadmin 
echo "ftpadmin:$ftppasswd" | chpasswd
echo "The user was successfully created, the initial password is $ftppasswd" > /root/ftppasswd.txt
#sed -i "s/home\/ftpadmin\:\/bin\/bash/var\/www\/html\:\/sbin\/nologin/" /etc/passwd
#mkdir  -p /var/www/html
#chown -R ftpadmin:ftpadmin /var/www/html
chown -R ftpadmin /var/www/html
chmod 755  /var/www/html
rm -rf  AutoInstallVsftpd.sh
exit 0
