-
Notifications
You must be signed in to change notification settings - Fork 0
/
Script_CreacionBorradoUsuariosFTP.sh
333 lines (305 loc) · 11.5 KB
/
Script_CreacionBorradoUsuariosFTP.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#! /bin/bash
#---------------------------------------------------------------------------#
#------------------------------- Funciones ---------------------------------#
#---------------------------------------------------------------------------#
function f_Menu {
echo ""
echo ""
echo ""
echo "------------------ MENU -------------------"
echo " (1) Añadir usuario "
echo " (2) Borrar usuario "
echo " (0) Salir "
echo "-------------------------------------------"
echo ""
echo "¿Que opción quiere?"
read -p "---> " OPCION
while [ $OPCION -lt 0 -o $OPCION -gt 2 ]
do
clear
echo ""
echo ""
echo ""
echo "--------ERROR--------"
echo " Opción no valida "
echo "---------------------"
echo ""
echo ""
echo ""
echo "------------------ MENU -------------------"
echo " (1) Añadir usuario "
echo " (2) Borrar usuario "
echo " (0) Salir "
echo "-------------------------------------------"
echo ""
echo "¿Que opción quiere?"
read -p "---> " OPCION
echo ""
done
return $OPCION
}
#---------------------------------------------------------------------------#
# Función de confirmación para realizar la creación y el borrado de los datos
#---------------------------------------------------------------------------#
function f_Confirmacion {
echo ""
echo "¿Estas seguro que quieres continuar? (Y/N)"
read -p "---> " CONFIRMACION
echo ""
while [ "$CONFIRMACION" != "Y" -a "$CONFIRMACION" != "y" -a "$CONFIRMACION" != "n" -a "$CONFIRMACION" != "N" ]
do
echo ""
echo "-----------ERROR-----------"
echo " Confirmacion no valida "
echo " Pruebe de nuevo "
echo "---------------------------"
echo ""
echo "¿Estas seguro que quieres continuar? (Y/N)"
read -p "---> " CONFIRMACION
echo ""
done
if [ "$CONFIRMACION" == "Y" -o "$CONFIRMACION" == "y" ]
then
CONFIRMACION=1
else
clear
CONFIRMACION=0
fi
return $CONFIRMACION
}
#---------------------------------------------------------------------------#
# Función que pregunta las variables por teclado dependiendo de la opción
#---------------------------------------------------------------------------#
function f_Preguntar {
clear
if [ $1 -eq 1 ]
then
echo "¿Como se llama el usuario que quieres crear?"
read -p "---> " USUARIO
echo "¿Como se llama el sitio web que quieres crear?"
read -p "---> " SITIOWEB
fi
if [ $1 -eq 2 ]
then
echo "Usuario que puedes borrar"
echo "-----------------------------------------------------------------"
echo " "
MOSTRAR_USUARIOS=$(cut -d: -f1 /etc/passwd | egrep "^user_" | cut -d "_" -f2)
echo "$MOSTRAR_USUARIOS"
echo " "
echo "-----------------------------------------------------------------"
echo " "
echo "¿Como se llama el usuario que quieres borrar?"
read -p "---> " USUARIO
return
fi
}
#---------------------------------------------------------------------------#
# Función para rellenar las variables comunes dependiendo de la opción
#---------------------------------------------------------------------------#
function f_RellenarVariables {
if [ $1 -eq 2 ]
then
SITIOWEB=$(sed -n 3p /etc/nginx/conf.d/user_$USUARIO.conf | cut -d " " -f7 | cut -d "." -f1)
fi
USER_FTP="user_$USUARIO"
PASS_FTP=$(openssl rand -base64 5)
USER_MYSQL="my_$USUARIO"
DB_MYSQL="db_$USUARIO"
PASS_MYSQL=$(openssl rand -base64 5)
WEB="$SITIOWEB.amorales.gonzalonazareno.org"
return
}
#---------------------------------------------------------------------------#
# Función de comprobacion de usuarios
#---------------------------------------------------------------------------#
function f_ComprobarUsuario {
PROBAR_USER=$(cut -d: -f1 /etc/passwd | grep "^user_$USUARIO$")
return $PROBAR_USER
}
#---------------------------------------------------------------------------#
# Función que muestra los datos que se van a crear o borrar antes de confirmar
#---------------------------------------------------------------------------#
function f_ExponerVariables {
clear
if [ $1 -eq 1 ]
then
echo " "
echo "----------------------------------"
echo "Se creará lo siguiente:"
echo "----------------------------------"
echo " "
echo " Usuario FTP: $USER_FTP"
echo "Usuario MySQL: $USER_MYSQL"
echo "Base de datos: $DB_MYSQL"
echo " Sitio Web: $WEB"
echo " "
fi
if [ $1 -eq 2 ]
then
echo " "
echo "----------------------------------"
echo "Se borrará lo siguiente:"
echo "----------------------------------"
echo " "
echo " Usuario FTP: $USER_FTP"
echo "Usuario MySQL: $USER_MYSQL"
echo "Base de datos: $DB_MYSQL"
echo " Sitio Web: $WEB"
echo " "
fi
}
#---------------------------------------------------------------------------#
# Función para mostrar los datos finales de cada opción
#---------------------------------------------------------------------------#
function f_MostrarDatos {
clear
if [ $1 -eq 1 ]
then
echo "--------------------------------------------------------"
echo " INFORMACIÓN DEL USUARIO"
echo "--------------------------------------------------------"
echo " Usuario FTP: $USER_FTP"
echo " Contraseña FTP: $PASS_FTP"
echo " Usuario MySQL: $USER_MYSQL"
echo " Contraseña MySQL: $PASS_MYSQL"
echo " Nombre de la Base de Datos: $DB_MYSQL"
echo " Página Web: $WEB"
echo "--------------------------------------------------------"
fi
if [ $1 -eq 2 ]
then
echo "----------------------------------------------------------------"
echo " SCRIPT FINALIZÓ CORRECTAMENTE "
echo "Se ha eliminado todo lo relacionado con el usuario $USER_FTP "
echo "----------------------------------------------------------------"
fi
}
#---------------------------------------------------------------------------#
# Función para crear el fichero del VirtualHosting del usuario.
#---------------------------------------------------------------------------#
function f_CrearVirtualHosting {
echo 'server {
listen 80;
server_name '$WEB';
rewrite ^ https://$server_name$request_uri permanent;
}
server {
listen 443;
server_name '$WEB';
ssl_certificate /etc/pki/tls/certs/amorales.gonzalonazareno.org.crt;
ssl_certificate_key /etc/pki/tls/private/gonzalonazareno.pem;
root /usr/share/nginx/html/'$USER_FTP';
index index.php index.html index.htm info.php;
location / {
try_files $uri $uri/ /index.php?$args;
autoindex on;
disable_symlinks if_not_owner;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}' | sudo tee /etc/nginx/conf.d/$USER_FTP.conf &> /dev/null
}
#---------------------------------------------------------------------------#
#------------------------------- Programa ----------------------------------#
#---------------------------------------------------------------------------#
clear
f_Menu
while [ $OPCION -ne 0 ]
do
if [ $OPCION -eq 1 ]
then
f_Preguntar $OPCION
f_RellenarVariables $OPCION
f_ComprobarUsuario
echo $PROBAR_USER
echo $USER_FTP
if [ -z $PROBAR_USER ]
then
f_ExponerVariables $OPCION
f_Confirmacion
if [ $CONFIRMACION -eq 1 ]
then
#Creamos el usuario del sistema y le asignamos una contraseña aleatoria.
sudo useradd -M $USER_FTP
echo $PASS_FTP | sudo passwd --stdin $USER_FTP
#Creamos el DocumentRoot y le asignamos el propietario y la regla de SELinux.
sudo mkdir /usr/share/nginx/html/$USER_FTP
sudo chown -R $USER_FTP:$USER_FTP /usr/share/nginx/html/$USER_FTP
sudo chcon -t httpd_sys_content_t /usr/share/nginx/html/$USER_FTP -R
#Creamos el fichero '.conf'.
f_CrearVirtualHosting
#Reiniciamos los servicios.
sudo systemctl restart nginx
sudo systemctl restart php-fpm
#Creamos el comando para crear el usuario y la base de datos y lo mandamos por ssh.
CREAR_USUARIO_MYSQL='sudo mysql -e "CREATE USER \"'$USER_MYSQL'\"@\"%\" IDENTIFIED BY \"'$PASS_MYSQL'\";"'
ssh ubuntu@tortilla $CREAR_USUARIO_MYSQL
CREAR_BD_MYSQL='sudo mysql -e "CREATE DATABASE '$DB_MYSQL';"'
ssh ubuntu@tortilla $CREAR_BD_MYSQL
ASIGNAR_PRIVILEGIO='sudo mysql -e "GRANT ALL PRIVILEGES ON '$DB_MYSQL'.* TO \"'$USER_MYSQL'\"@\"%\""'
ssh ubuntu@tortilla $ASIGNAR_PRIVILEGIO
#Creamos el comando para añadir el registro al DNS y lo mandamos por ssh.
INSERTAR_REGISTRO_DNS="sudo sed -i '\$a $SITIOWEB IN CNAME salmorejo' /var/cache/bind/db.amorales.gonzalonazareno.org"
ssh debian@croqueta $INSERTAR_REGISTRO_DNS
ssh debian@croqueta 'sudo rndc reload'
#Mostramos los datos finales.
f_MostrarDatos $OPCION
fi
fi
fi
if [ $OPCION -eq 2 ]
then
f_Preguntar $OPCION
f_RellenarVariables $OPCION
f_ComprobarUsuario
if [ $USER_FTP = $PROBAR_USER ]
then
f_ExponerVariables $OPCION
f_Confirmacion
if [ $CONFIRMACION -eq 1 ]
then
#Eliminamos el fichero '.conf' y el directorio DocumentRoot del usuario.
sudo rm -rf /usr/share/nginx/html/$USER_FTP
sudo rm -rf /etc/nginx/conf.d/$USER_FTP.conf
#Eliminamos el usuario del sistema.
sudo userdel $USER_FTP
#Creamos el comando para eliminar el usuario y la base de datos y lo enviamos por ssh.
BORRAR_USUARIO='sudo mysql -e "DROP USER '$USER_MYSQL';"'
ssh ubuntu@tortilla $BORRAR_USUARIO
BORRAR_BD='sudo mysql -e "DROP DATABASE '$DB_MYSQL';"'
ssh ubuntu@tortilla $BORRAR_BD
#Creamos el comando para eliminar el registro del DNS y lo enviamos por ssh.
BORRAR_REGISTRO_DNS="sudo sed -i '/$SITIOWEB IN CNAME salmorejo/d' /var/cache/bind/db.amorales.gonzalonazareno.org"
ssh debian@croqueta $BORRAR_REGISTRO_DNS
ssh debian@croqueta 'sudo rndc reload'
#Mostramos los datos finales.
f_MostrarDatos $OPCION
fi
else
echo ""
echo "-------------ERROR-------------"
echo " Usuario $USUARIO no valido "
echo "-------------------------------"
echo ""
fi
fi
f_Menu
done
if [ $OPCION -eq 0 ]
then
echo ""
echo ""
echo ""
echo "-----------------------------"
echo " ADIOS, que pase un buen dia "
echo "-----------------------------"
echo ""
echo ""
echo ""
fi