forked from oneinstack/oneinstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pureftpd_vhost.sh
executable file
·271 lines (253 loc) · 7.58 KB
/
pureftpd_vhost.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/bin/bash
# Author: yeho <lj2007331 AT gmail.com>
# BLOG: https://linuxeye.com
#
# Notes: OneinStack for CentOS/RedHat 6+ Debian 8+ and Ubuntu 14+
#
# Project home page:
# https://oneinstack.com
# https://github.com/oneinstack/oneinstack
export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
clear
printf "
#######################################################################
# OneinStack for CentOS/RedHat 6+ Debian 8+ and Ubuntu 14+ #
# FTP virtual user account management #
# For more information please visit https://oneinstack.com #
#######################################################################
"
# Check if user is root
[ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }
oneinstack_dir=$(dirname "`readlink -f $0`")
pushd ${oneinstack_dir} > /dev/null
. ./options.conf
. ./include/color.sh
[ ! -d "${pureftpd_install_dir}" ] && { echo "${CFAILURE}FTP server does not exist! ${CEND}"; exit 1; }
FTP_conf=${pureftpd_install_dir}/etc/pure-ftpd.conf
FTP_tmp_passfile=${pureftpd_install_dir}/etc/pureftpd_psss.tmp
Puredbfile=${pureftpd_install_dir}/etc/pureftpd.pdb
Passwdfile=${pureftpd_install_dir}/etc/pureftpd.passwd
FTP_bin=${pureftpd_install_dir}/bin/pure-pw
[ -z "`grep ^PureDB ${FTP_conf}`" ] && { echo "${CFAILURE}pure-ftpd is not own password database${CEND}" ; exit 1; }
ARG_NUM=$#
Show_Help() {
echo
echo "Usage: $0 command ...[parameters]....
--help, -h Show this help message
--useradd,--add Add username
--usermod Modify directory
--passwd Modify password
--userdel,--delete Delete User
--listalluser,--list List all User
--showuser List User details
--username,-u [ftp username] Ftp username
--password,-p [ftp password] Ftp password
--directory,-d,-D [ftp directory] Ftp home directory
"
}
TEMP=`getopt -o hu:p:d:D: --long help,useradd,add,usermod,passwd,userdel,delete,listalluser,list,showuser,username:,password:,directory: -- "$@" 2>/dev/null`
[ $? != 0 ] && echo "${CWARNING}ERROR: unknown argument! ${CEND}" && Show_Help && exit 1
eval set -- "${TEMP}"
while :; do
[ -z "$1" ] && break;
case "$1" in
-h|--help)
Show_Help; exit 0
;;
--add|--useradd)
useradd_flag=y; shift 1
;;
--usermod)
usermod_flag=y; shift 1
;;
--passwd)
passwd_flag=y; shift 1
;;
--delete|--userdel)
userdel_flag=y; shift 1
;;
--list|--listalluser)
listalluser_flag=y; shift 1
;;
--showuser)
showuser_flag=y; shift 1
;;
-u|--username)
username_flag=y; User=$2; shift 2
;;
-p|--password)
password_flag=y; Password=$2; shift 2
;;
-d|-D|--directory)
directory_flag=y; Directory=$2; shift 2
;;
--)
shift
;;
*)
echo "${CWARNING}ERROR: unknown argument! ${CEND}" && Show_Help && exit 1
;;
esac
done
USER() {
while :; do
if [ "${username_flag}" != 'y' ]; then
echo
read -e -p "Please input a username: " User
fi
if [ -z "${User}" ]; then
echo "${CWARNING}username can't be NULL! ${CEND}"
else
break
fi
done
}
PASSWORD() {
while :; do
if [ "${password_flag}" != 'y' ]; then
echo
read -e -p "Please input the password: " Password
fi
[ -n "`echo ${Password} | grep '[+|&]'`" ] && { echo "${CWARNING}input error,not contain a plus sign (+) and &${CEND}"; continue; }
if (( ${#Password} >= 5 )); then
echo -e "${Password}\n${Password}" > ${FTP_tmp_passfile}
break
else
echo "${CWARNING}Ftp password least 5 characters! ${CEND}"
fi
done
}
DIRECTORY() {
while :; do
if [ "${directory_flag}" != 'y' ]; then
echo
read -e -p "Please input the directory(Default directory: ${wwwroot_dir}): " Directory
fi
Directory=${Directory:-${wwwroot_dir}}
if [ ! -d "${Directory}" ]; then
echo "${CWARNING}The directory does not exist${CEND}"
else
break
fi
done
}
UserAdd() {
USER
[ -e "${Passwdfile}" ] && [ -n "`grep ^${User}: ${Passwdfile}`" ] && { echo "${CQUESTION}[${User}] is already existed! ${CEND}"; exit 1; }
PASSWORD;DIRECTORY
${FTP_bin} useradd ${User} -f ${Passwdfile} -u ${run_user} -g ${run_user} -d ${Directory} -m < ${FTP_tmp_passfile}
${FTP_bin} mkdb ${Puredbfile} -f ${Passwdfile} > /dev/null 2>&1
echo "#####################################"
echo
echo "[${User}] create successful! "
echo
echo "You user name is : ${CMSG}${User}${CEND}"
echo "You Password is : ${CMSG}${Password}${CEND}"
echo "You directory is : ${CMSG}${Directory}${CEND}"
echo
}
UserMod() {
USER
[ -e "${Passwdfile}" ] && [ -z "`grep ^${User}: ${Passwdfile}`" ] && { echo "${CQUESTION}[${User}] was not existed! ${CEND}"; exit 1; }
DIRECTORY
${FTP_bin} usermod ${User} -f ${Passwdfile} -d ${Directory} -m
${FTP_bin} mkdb ${Puredbfile} -f ${Passwdfile} > /dev/null 2>&1
echo "#####################################"
echo
echo "[${User}] modify a successful! "
echo
echo "You user name is : ${CMSG}${User}${CEND}"
echo "You new directory is : ${CMSG}${Directory}${CEND}"
echo
}
UserPasswd() {
USER
[ -e "${Passwdfile}" ] && [ -z "`grep ^${User}: ${Passwdfile}`" ] && { echo "${CQUESTION}[${User}] was not existed! ${CEND}"; exit 1; }
PASSWORD
${FTP_bin} passwd ${User} -f ${Passwdfile} -m < ${FTP_tmp_passfile}
${FTP_bin} mkdb ${Puredbfile} -f ${Passwdfile} > /dev/null 2>&1
echo "#####################################"
echo
echo "[${User}] Password changed successfully! "
echo
echo "You user name is : ${CMSG}${User}${CEND}"
echo "You new password is : ${CMSG}${Password}${CEND}"
echo
}
UserDel() {
if [ ! -e "${Passwdfile}" ]; then
echo "${CQUESTION}User was not existed! ${CEND}"
else
${FTP_bin} list
fi
USER
[ -e "${Passwdfile}" ] && [ -z "`grep ^${User}: ${Passwdfile}`" ] && { echo "${CQUESTION}[${User}] was not existed! ${CEND}"; exit 1; }
${FTP_bin} userdel ${User} -f ${Passwdfile} -m
${FTP_bin} mkdb ${Puredbfile} -f ${Passwdfile} > /dev/null 2>&1
echo
echo "[${User}] have been deleted! "
}
ListAllUser() {
if [ ! -e "${Passwdfile}" ]; then
echo "${CQUESTION}User was not existed! ${CEND}"
else
${FTP_bin} list
fi
}
ShowUser() {
USER
[ -e "${Passwdfile}" ] && [ -z "`grep ^${User}: ${Passwdfile}`" ] && { echo "${CQUESTION}[${User}] was not existed! ${CEND}"; exit 1; }
${FTP_bin} show ${User}
}
Menu() {
while :; do
printf "
What Are You Doing?
\t${CMSG}1${CEND}. UserAdd
\t${CMSG}2${CEND}. UserMod
\t${CMSG}3${CEND}. UserPasswd
\t${CMSG}4${CEND}. UserDel
\t${CMSG}5${CEND}. ListAllUser
\t${CMSG}6${CEND}. ShowUser
\t${CMSG}q${CEND}. Exit
"
read -e -p "Please input the correct option: " Number
if [[ ! ${Number} =~ ^[1-6,q]$ ]]; then
echo "${CFAILURE}input error! Please only input 1~6 and q${CEND}"
else
case "${Number}" in
1)
UserAdd
;;
2)
UserMod
;;
3)
UserPasswd
;;
4)
UserDel
;;
5)
ListAllUser
;;
6)
ShowUser
;;
q)
exit
;;
esac
fi
done
}
if [ ${ARG_NUM} == 0 ]; then
Menu
else
[ "${useradd_flag}" == 'y' ] && UserAdd
[ "${usermod_flag}" == 'y' ] && UserMod
[ "${passwd_flag}" == 'y' ] && UserPasswd
[ "${userdel_flag}" == 'y' ] && UserDel
[ "${listalluser_flag}" == 'y' ] && ListAllUser
[ "${showuser_flag}" == 'y' ] && ShowUser
fi