-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvsftpd_virtualuser_remove.sh
176 lines (149 loc) · 4.65 KB
/
vsftpd_virtualuser_remove.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
#!/bin/sh
# Chroot FTP with Virtual Users - Remove ftp virtual user
#
# Version 1.0
# August 1, 2005
# Fire Eater <[email protected]>
#
# Version 1.1
# December 14, 2008
# Alain Reguera Delgado <[email protected]>
#
# Released under the GPL License- http://www.fsf.org/licensing/licenses/gpl.txt
#
# Initialization
#
HOMEDIR=/var/ftp/virtual_users
FTPCONF=/etc/vsftpd
LOCALPATH=`pwd`
if [ -f $FTPCONF/accounts.tmp ];then
ACCOUNTDB_TOTALLINES=`grep '.' -c $FTPCONF/accounts.tmp`
else
ACCOUNTDB_TOTALLINES=0
fi
function checkUser_Existence () {
C=1;
if [ "$ACCOUNTDB_TOTALLINES" != "0" ];then
while [ $C -lt $ACCOUNTDB_TOTALLINES ]; do
VALIDUSER=`sed -n -e "$C p" $FTPCONF/accounts.tmp`
if [ "$USERNAME" == "$VALIDUSER" ];then
USERNAMEOK=YES
break;
else
USERNAMEOK=NO
fi
let C=$C+2;
done
fi
}
function checkUser_Homedir () {
# Verify User's Home Directory.
if [ -d $HOMEDIR ];then
for i in `ls $HOMEDIR/`; do
VALIDUSER=$i
if [ "$USERNAME" == "$VALIDUSER" ];then
USERNAMEOK=YES
break;
else
USENAMEOK=NO
fi
done
fi
}
function removeUser () {
# Remove user from accounts.tmp
printf " Updating $FTPCONF/accounts.tmp file ... ";
sed -i -e "/$USERNAME/,+1 d" $FTPCONF/accounts.tmp
printf "done. \n"
# Remove user from account.db
printf " Updating $FTPCONF/accounts.db file ... ";
db_load -T -t hash -f $FTPCONF/accounts.tmp $FTPCONF/accounts.db
printf "done. \n"
# Remove user from denied_users
printf " Updating $FTPCONF/denied_users file ... "
sed -i -e "/$USERNAME/ d" $FTPCONF/denied_users
printf " done.\n"
# Remove user from /etc/passwd and /etc/group. Also
# remove related user information.
printf " Removing user information from the system ... ";
/usr/sbin/userdel -r $USERNAME
printf "done. \n"
# Remove user config from /etc/vsftpd/users
printf " Removing user config file from user config dirs ... ";
rm -f $FTPCONF/users/$USERNAME
printf "done. \n"
# Remove user ftp dir from $HOMEDIR
printf " Removing user ftp root dir from HOMEDIR ... ";
rm -rf $HOMEDIR/$USERNAME
printf "done. \n"
}
clear;
echo " vsftpd 2.0.5 -> Virtual Users -> Remove User"
echo '-------------------------------------------------------------------'
#
# Check dependencies
#
PACKISMISSING=""
PACKDEPENDENCIES="vsftpd libdb-utils"
for i in `echo $PACKDEPENDENCIES`; do
/bin/rpm -q $i > /dev/null
if [ "$?" != "0" ];then
PACKISMISSING="$PACKISMISSING $i"
fi
done
if [ "$PACKISMISSING" != "" ];then
echo " ATTENTION: The following package(s) are needed by this script:"
for i in `echo $PACKISMISSING`; do
echo " - $i"
done
echo '-------------------------------------------------------------------'
exit;
fi
#
# Non-interactive
#
if [ "$1" ];then
for i in $1; do
USERNAME=$i
echo "Removing user $USERNAME: "
checkUser_Existence;
checkUser_Homedir;
if [ "$USERNAMEOK" == "YES" ];then
removeUser;
echo '-------------------------------------------------------------------'
#/sbin/service vsftpd restart
echo '-------------------------------------------------------------------'
else
echo " ATTENTION : This user can't be removed. It is an invalid user."
echo '-------------------------------------------------------------------'
fi
done
exit;
fi
#
# Interactive
#
printf " Current users are: "
${LOCALPATH}/vsftpd_virtualuser_info.sh
printf " [Warning] If the username you specified is not in that list, we will still delete relevant directories at FTP path "
printf " Enter username (lowercase): "
read USERNAME
checkUser_Existence;
checkUser_Homedir;
if [ "$USERNAMEOK" == "YES" ];then
echo ' ****************************************************************** '
echo " * ATTENTION: All data related to the user $USERNAME will be removed."
echo ' ****************************************************************** '
printf ' Are you sure ? (N/y): '
read CONFIRMATION
if [ "$CONFIRMATION" != "y" ];then
exit;
fi
removeUser;
echo '-------------------------------------------------------------------'
#/sbin/service vsftpd restart
echo '-------------------------------------------------------------------'
else
echo " ATTENTION : This user can't be removed. It is an invalid user."
echo '-------------------------------------------------------------------'
fi