pureftpd_vhost.sh 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+
  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 7+ Debian 9+ and Ubuntu 16+ #
  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. ARG_NUM=$#
  33. Show_Help() {
  34. echo
  35. echo "Usage: $0 command ...[parameters]....
  36. --help, -h Show this help message
  37. --useradd,--add Add username
  38. --usermod Modify directory
  39. --passwd Modify password
  40. --userdel,--delete Delete User
  41. --listalluser,--list List all User
  42. --showuser List User details
  43. --username,-u [ftp username] Ftp username
  44. --password,-p [ftp password] Ftp password
  45. --directory,-d,-D [ftp directory] Ftp home directory
  46. "
  47. }
  48. TEMP=`getopt -o hu:p:d:D: --long help,useradd,add,usermod,passwd,userdel,delete,listalluser,list,showuser,username:,password:,directory: -- "$@" 2>/dev/null`
  49. [ $? != 0 ] && echo "${CWARNING}ERROR: unknown argument! ${CEND}" && Show_Help && exit 1
  50. eval set -- "${TEMP}"
  51. while :; do
  52. [ -z "$1" ] && break;
  53. case "$1" in
  54. -h|--help)
  55. Show_Help; exit 0
  56. ;;
  57. --add|--useradd)
  58. useradd_flag=y; shift 1
  59. ;;
  60. --usermod)
  61. usermod_flag=y; shift 1
  62. ;;
  63. --passwd)
  64. passwd_flag=y; shift 1
  65. ;;
  66. --delete|--userdel)
  67. userdel_flag=y; shift 1
  68. ;;
  69. --list|--listalluser)
  70. listalluser_flag=y; shift 1
  71. ;;
  72. --showuser)
  73. showuser_flag=y; shift 1
  74. ;;
  75. -u|--username)
  76. username_flag=y; User=$2; shift 2
  77. ;;
  78. -p|--password)
  79. password_flag=y; Password=$2; shift 2
  80. ;;
  81. -d|-D|--directory)
  82. directory_flag=y; Directory=$2; shift 2
  83. ;;
  84. --)
  85. shift
  86. ;;
  87. *)
  88. echo "${CWARNING}ERROR: unknown argument! ${CEND}" && Show_Help && exit 1
  89. ;;
  90. esac
  91. done
  92. USER() {
  93. while :; do
  94. if [ "${username_flag}" != 'y' ]; then
  95. echo
  96. read -e -p "Please input a username: " User
  97. fi
  98. if [ -z "${User}" ]; then
  99. echo "${CWARNING}username can't be NULL! ${CEND}"
  100. else
  101. break
  102. fi
  103. done
  104. }
  105. PASSWORD() {
  106. while :; do
  107. if [ "${password_flag}" != 'y' ]; then
  108. echo
  109. read -e -p "Please input the password: " Password
  110. fi
  111. [ -n "`echo ${Password} | grep '[+|&]'`" ] && { echo "${CWARNING}input error,not contain a plus sign (+) and &${CEND}"; continue; }
  112. if (( ${#Password} >= 5 )); then
  113. echo -e "${Password}\n${Password}" > ${FTP_tmp_passfile}
  114. break
  115. else
  116. echo "${CWARNING}Ftp password least 5 characters! ${CEND}"
  117. fi
  118. done
  119. }
  120. DIRECTORY() {
  121. while :; do
  122. if [ "${directory_flag}" != 'y' ]; then
  123. echo
  124. read -e -p "Please input the directory(Default directory: ${wwwroot_dir}): " Directory
  125. fi
  126. Directory=${Directory:-${wwwroot_dir}}
  127. if [ ! -d "${Directory}" ]; then
  128. echo "${CWARNING}The directory does not exist${CEND}"
  129. else
  130. break
  131. fi
  132. done
  133. }
  134. UserAdd() {
  135. USER
  136. [ -e "${Passwdfile}" ] && [ -n "`grep ^${User}: ${Passwdfile}`" ] && { echo "${CQUESTION}[${User}] is already existed! ${CEND}"; exit 1; }
  137. PASSWORD;DIRECTORY
  138. ${FTP_bin} useradd ${User} -f ${Passwdfile} -u ${run_user} -g ${run_group} -d ${Directory} -m < ${FTP_tmp_passfile}
  139. ${FTP_bin} mkdb ${Puredbfile} -f ${Passwdfile} > /dev/null 2>&1
  140. echo "#####################################"
  141. echo
  142. echo "[${User}] create successful! "
  143. echo
  144. echo "You user name is : ${CMSG}${User}${CEND}"
  145. echo "You Password is : ${CMSG}${Password}${CEND}"
  146. echo "You directory is : ${CMSG}${Directory}${CEND}"
  147. echo
  148. }
  149. UserMod() {
  150. USER
  151. [ -e "${Passwdfile}" ] && [ -z "`grep ^${User}: ${Passwdfile}`" ] && { echo "${CQUESTION}[${User}] was not existed! ${CEND}"; exit 1; }
  152. DIRECTORY
  153. ${FTP_bin} usermod ${User} -f ${Passwdfile} -d ${Directory} -m
  154. ${FTP_bin} mkdb ${Puredbfile} -f ${Passwdfile} > /dev/null 2>&1
  155. echo "#####################################"
  156. echo
  157. echo "[${User}] modify a successful! "
  158. echo
  159. echo "You user name is : ${CMSG}${User}${CEND}"
  160. echo "You new directory is : ${CMSG}${Directory}${CEND}"
  161. echo
  162. }
  163. UserPasswd() {
  164. USER
  165. [ -e "${Passwdfile}" ] && [ -z "`grep ^${User}: ${Passwdfile}`" ] && { echo "${CQUESTION}[${User}] was not existed! ${CEND}"; exit 1; }
  166. PASSWORD
  167. ${FTP_bin} passwd ${User} -f ${Passwdfile} -m < ${FTP_tmp_passfile}
  168. ${FTP_bin} mkdb ${Puredbfile} -f ${Passwdfile} > /dev/null 2>&1
  169. echo "#####################################"
  170. echo
  171. echo "[${User}] Password changed successfully! "
  172. echo
  173. echo "You user name is : ${CMSG}${User}${CEND}"
  174. echo "You new password is : ${CMSG}${Password}${CEND}"
  175. echo
  176. }
  177. UserDel() {
  178. if [ ! -e "${Passwdfile}" ]; then
  179. echo "${CQUESTION}User was not existed! ${CEND}"
  180. else
  181. ${FTP_bin} list
  182. fi
  183. USER
  184. [ -e "${Passwdfile}" ] && [ -z "`grep ^${User}: ${Passwdfile}`" ] && { echo "${CQUESTION}[${User}] was not existed! ${CEND}"; exit 1; }
  185. ${FTP_bin} userdel ${User} -f ${Passwdfile} -m
  186. ${FTP_bin} mkdb ${Puredbfile} -f ${Passwdfile} > /dev/null 2>&1
  187. echo
  188. echo "[${User}] have been deleted! "
  189. }
  190. ListAllUser() {
  191. if [ ! -e "${Passwdfile}" ]; then
  192. echo "${CQUESTION}User was not existed! ${CEND}"
  193. else
  194. ${FTP_bin} list
  195. fi
  196. }
  197. ShowUser() {
  198. USER
  199. [ -e "${Passwdfile}" ] && [ -z "`grep ^${User}: ${Passwdfile}`" ] && { echo "${CQUESTION}[${User}] was not existed! ${CEND}"; exit 1; }
  200. ${FTP_bin} show ${User}
  201. }
  202. Menu() {
  203. while :; do
  204. printf "
  205. What Are You Doing?
  206. \t${CMSG}1${CEND}. UserAdd
  207. \t${CMSG}2${CEND}. UserMod
  208. \t${CMSG}3${CEND}. UserPasswd
  209. \t${CMSG}4${CEND}. UserDel
  210. \t${CMSG}5${CEND}. ListAllUser
  211. \t${CMSG}6${CEND}. ShowUser
  212. \t${CMSG}q${CEND}. Exit
  213. "
  214. read -e -p "Please input the correct option: " Number
  215. if [[ ! ${Number} =~ ^[1-6,q]$ ]]; then
  216. echo "${CFAILURE}input error! Please only input 1~6 and q${CEND}"
  217. else
  218. case "${Number}" in
  219. 1)
  220. UserAdd
  221. ;;
  222. 2)
  223. UserMod
  224. ;;
  225. 3)
  226. UserPasswd
  227. ;;
  228. 4)
  229. UserDel
  230. ;;
  231. 5)
  232. ListAllUser
  233. ;;
  234. 6)
  235. ShowUser
  236. ;;
  237. q)
  238. exit
  239. ;;
  240. esac
  241. fi
  242. done
  243. }
  244. if [ ${ARG_NUM} == 0 ]; then
  245. Menu
  246. else
  247. [ "${useradd_flag}" == 'y' ] && UserAdd
  248. [ "${usermod_flag}" == 'y' ] && UserMod
  249. [ "${passwd_flag}" == 'y' ] && UserPasswd
  250. [ "${userdel_flag}" == 'y' ] && UserDel
  251. [ "${listalluser_flag}" == 'y' ] && ListAllUser
  252. [ "${showuser_flag}" == 'y' ] && ShowUser
  253. fi