pureftpd_vhost.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://blog.linuxeye.cn
  4. #
  5. # Notes: OneinStack for CentOS/RedHat 6+ Debian 7+ and Ubuntu 12+
  6. #
  7. # Project home page:
  8. # https://oneinstack.com
  9. # https://github.com/oneinstack/oneinstack
  10. export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
  11. clear
  12. printf "
  13. #######################################################################
  14. # OneinStack for CentOS/RedHat 6+ Debian 7+ and Ubuntu 12+ #
  15. # FTP virtual user account management #
  16. # For more information please visit https://oneinstack.com #
  17. #######################################################################
  18. "
  19. # Check if user is root
  20. [ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }
  21. oneinstack_dir=$(dirname "`readlink -f $0`")
  22. pushd ${oneinstack_dir} > /dev/null
  23. . ./options.conf
  24. . ./include/color.sh
  25. [ ! -d "${pureftpd_install_dir}" ] && { echo "${CFAILURE}FTP server does not exist! ${CEND}"; exit 1; }
  26. FTP_conf=${pureftpd_install_dir}/etc/pure-ftpd.conf
  27. FTP_tmp_passfile=${pureftpd_install_dir}/etc/pureftpd_psss.tmp
  28. Puredbfile=${pureftpd_install_dir}/etc/pureftpd.pdb
  29. Passwdfile=${pureftpd_install_dir}/etc/pureftpd.passwd
  30. FTP_bin=${pureftpd_install_dir}/bin/pure-pw
  31. [ -z "`grep ^PureDB $FTP_conf`" ] && { echo "${CFAILURE}pure-ftpd is not own password database${CEND}" ; exit 1; }
  32. USER() {
  33. while :; do echo
  34. read -e -p "Please input a username: " User
  35. if [ -z "${User}" ]; then
  36. echo "${CWARNING}username can't be NULL! ${CEND}"
  37. else
  38. break
  39. fi
  40. done
  41. }
  42. PASSWORD() {
  43. while :; do echo
  44. read -e -p "Please input the password: " Password
  45. [ -n "`echo ${Password} | grep '[+|&]'`" ] && { echo "${CWARNING}input error,not contain a plus sign (+) and &${CEND}"; continue; }
  46. if (( ${#Password} >= 5 ));then
  47. echo -e "${Password}\n${Password}" > ${FTP_tmp_passfile}
  48. break
  49. else
  50. echo "${CWARNING}Ftp password least 5 characters! ${CEND}"
  51. fi
  52. done
  53. }
  54. DIRECTORY() {
  55. while :; do echo
  56. read -e -p "Please input the directory(Default directory: ${wwwroot_dir}): " Directory
  57. Directory=${Directory:-${wwwroot_dir}}
  58. if [ ! -d "${Directory}" ]; then
  59. echo "${CWARNING}The directory does not exist${CEND}"
  60. else
  61. break
  62. fi
  63. done
  64. }
  65. while :; do
  66. printf "
  67. What Are You Doing?
  68. \t${CMSG}1${CEND}. UserAdd
  69. \t${CMSG}2${CEND}. UserMod
  70. \t${CMSG}3${CEND}. UserPasswd
  71. \t${CMSG}4${CEND}. UserDel
  72. \t${CMSG}5${CEND}. ListAllUser
  73. \t${CMSG}6${CEND}. ShowUser
  74. \t${CMSG}q${CEND}. Exit
  75. "
  76. read -e -p "Please input the correct option: " Number
  77. if [[ ! $Number =~ ^[1-6,q]$ ]]; then
  78. echo "${CFAILURE}input error! Please only input 1~6 and q${CEND}"
  79. else
  80. case "$Number" in
  81. 1)
  82. USER
  83. [ -e "${Passwdfile}" ] && [ -n "`grep ^${User}: ${Passwdfile}`" ] && { echo "${CQUESTION}[${User}] is already existed! ${CEND}"; continue; }
  84. PASSWORD;DIRECTORY
  85. ${FTP_bin} useradd ${User} -f ${Passwdfile} -u ${run_user} -g ${run_user} -d ${Directory} -m < ${FTP_tmp_passfile}
  86. ${FTP_bin} mkdb ${Puredbfile} -f ${Passwdfile} > /dev/null 2>&1
  87. echo "#####################################"
  88. echo
  89. echo "[${User}] create successful! "
  90. echo
  91. echo "You user name is : ${CMSG}${User}${CEND}"
  92. echo "You Password is : ${CMSG}${Password}${CEND}"
  93. echo "You directory is : ${CMSG}${Directory}${CEND}"
  94. echo
  95. ;;
  96. 2)
  97. USER
  98. [ -e "${Passwdfile}" ] && [ -z "`grep ^${User}: ${Passwdfile}`" ] && { echo "${CQUESTION}[${User}] was not existed! ${CEND}"; continue; }
  99. DIRECTORY
  100. ${FTP_bin} usermod ${User} -f ${Passwdfile} -d ${Directory} -m
  101. ${FTP_bin} mkdb ${Puredbfile} -f ${Passwdfile} > /dev/null 2>&1
  102. echo "#####################################"
  103. echo
  104. echo "[${User}] modify a successful! "
  105. echo
  106. echo "You user name is : ${CMSG}${User}${CEND}"
  107. echo "You new directory is : ${CMSG}${Directory}${CEND}"
  108. echo
  109. ;;
  110. 3)
  111. USER
  112. [ -e "${Passwdfile}" ] && [ -z "`grep ^${User}: ${Passwdfile}`" ] && { echo "${CQUESTION}[${User}] was not existed! ${CEND}"; continue; }
  113. PASSWORD
  114. ${FTP_bin} passwd ${User} -f ${Passwdfile} -m < ${FTP_tmp_passfile}
  115. ${FTP_bin} mkdb ${Puredbfile} -f ${Passwdfile} > /dev/null 2>&1
  116. echo "#####################################"
  117. echo
  118. echo "[${User}] Password changed successfully! "
  119. echo
  120. echo "You user name is : ${CMSG}${User}${CEND}"
  121. echo "You new password is : ${CMSG}${Password}${CEND}"
  122. echo
  123. ;;
  124. 4)
  125. if [ ! -e "${Passwdfile}" ]; then
  126. echo "${CQUESTION}User was not existed! ${CEND}"
  127. else
  128. ${FTP_bin} list
  129. fi
  130. USER
  131. [ -e "${Passwdfile}" ] && [ -z "`grep ^${User}: ${Passwdfile}`" ] && { echo "${CQUESTION}[${User}] was not existed! ${CEND}"; continue; }
  132. ${FTP_bin} userdel ${User} -f ${Passwdfile} -m
  133. ${FTP_bin} mkdb ${Puredbfile} -f ${Passwdfile} > /dev/null 2>&1
  134. echo
  135. echo "[${User}] have been deleted! "
  136. ;;
  137. 5)
  138. if [ ! -e "${Passwdfile}" ]; then
  139. echo "${CQUESTION}User was not existed! ${CEND}"
  140. else
  141. ${FTP_bin} list
  142. fi
  143. ;;
  144. 6)
  145. USER
  146. [ -e "${Passwdfile}" ] && [ -z "`grep ^${User}: ${Passwdfile}`" ] && { echo "${CQUESTION}[${User}] was not existed! ${CEND}"; continue; }
  147. ${FTP_bin} show ${User}
  148. ;;
  149. q)
  150. exit
  151. ;;
  152. esac
  153. fi
  154. done