-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeb.sh
More file actions
257 lines (225 loc) · 7.14 KB
/
Copy pathWeb.sh
File metadata and controls
257 lines (225 loc) · 7.14 KB
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
#!/bin/bash
# La manera de utilizarlo es comentar aquellas lineas indicadas
# para dejar de hacer ciertas tareas por ejemplo
# Comment this line if you want to unable sftp configuration
# Uncomment this line if you want to enable sftp configuration
# sftp_configuration
################################################################################################
LRED="\e[91m"
LGREEN="\e[92m"
LYELLOW="\e[93m"
LBLUE="\e[94m"
LMAGENTA="\e[95m"
LCYAN="\e[96m"
LGREY="\e[97m"
BOLD="\e[1m"
RESET="\e[0m"
apt purge apache2 >/dev/null 2>&1
rm -r /var/www/*
apt install nginx >/dev/null 2>&1
# Execution checkers & Basic functions
execute_flag_ssk=0
function passwd-check() {
if [[ "$passwd" == "$passwd2" ]]; then
return 0
else
return 1
fi
}
function test-email() {
local validezemail="^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
if [[ "$email" =~ $validezemail ]]; then
return 0
else
return 1
fi
}
# Script
function newcomer() {
clear
echo -e "$LMAGENTA"
echo -e "Bienvenido a el script modular de VHOSTING con SFTP hecho por IJR & MOSKA$RESET"
echo -e "Por favor escriba su nombre de usuario"
read -p ">" user
while true; do
unset passwd
unset passwd2
echo -n "Introduzca su contraseña: "
stty -echo
PROMPT=""
CHARCOUNT=0
while IFS= read -p "$PROMPT" -r -s -n 1 CHAR
do
if [[ $CHAR == $'\0' ]] ; then
break
fi
if [[ $CHAR == $'\177' ]] ; then
if [ $CHARCOUNT -gt 0 ] ; then
CHARCOUNT=$((CHARCOUNT-1))
PROMPT=$'\b \b'
passwd="${passwd%?}"
else
PROMPT=''
fi
elif [[ $CHAR == $'\n' ]] ; then
break
else
CHARCOUNT=$((CHARCOUNT+1))
PROMPT='*'
passwd+="$CHAR"
fi
done
echo
echo -n "Vuelve a introducir la contraseña: "
PROMPT=""
CHARCOUNT=0
while IFS= read -p "$PROMPT" -r -s -n 1 CHAR
do
if [[ $CHAR == $'\0' ]] ; then
break
fi
if [[ $CHAR == $'\177' ]] ; then
if [ $CHARCOUNT -gt 0 ] ; then
CHARCOUNT=$((CHARCOUNT-1))
PROMPT=$'\b \b'
passwd2="${passwd2%?}"
else
PROMPT=''
fi
else
CHARCOUNT=$((CHARCOUNT+1))
PROMPT='*'
passwd2+="$CHAR"
fi
done
passwd-check
if [ $? -eq 0 ]; then
stty echo
echo
break
else
echo
echo "La contraseña que has escrito no es correcta, por favor escribela de nuevo."
fi
done
useradd -m "$user" >>/dev/null 2>&1
passwd "$user" <<< "$passwd"$'\n'$passwd >>/dev/null 2>&1
echo -e " Cual es tu dominio?"
read -p ">" domain
while true; do
echo "Por favor escriba su email: "
read email
test-email "$email"
if [ $? -eq 0 ]; then
break
else
echo "El e-mail que has escrito no es correcto, por favor escribalo de nuevo."
fi
done
# Directory creation
mkdir -p /var/www/$domain/html
}
function sftp_configuration() {
chmod 755 /var/www/$domain
chmod -R 775 /var/www/$domain/html/
chown -R root:$user /var/www/$domain/
echo -e "
Match Group $user
ChrootDirectory /var/www/$domain
ForceCommand internal-sftp
PubkeyAuthentication yes
PasswordAuthentication yes
PermitTTY no" >> /etc/ssh/sshd_config
systemctl restart sshd
}
function ssl_ssk() {
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout "/etc/ssl/private/$domain.key" -out "/etc/ssl/certs/$domain.crt" -subj "/C=ES/ST=Catalunya/L=Barcelona/O=IJR & Moska Trust Services/OU=IJR & Moska Trust Services/CN=$domain/emailAddress=$email"
execute_flag_ssk=1
}
function vhost_http_server_config() {
echo -e "
server {
listen 80;
listen [::]:80;
root /var/www/$domain/html;
index index.html index.htm index.nginx-debian.html;
server_name $domain www.$domain;
location / {
try_files \$uri \$uri/ =404;
}
}" > /etc/nginx/sites-available/$domain
cp /etc/nginx/sites-available/$domain /etc/nginx/sites-enabled/
echo -e "<!DOCTYPE html>
<html lang="es">
<body>
<h1>Bienvenido a tu dominio!</h1>
<p>Esta es la pagina default de $user,
para editar tu pagina web entra por SFTP.</p>
</body>
</html>" > /var/www/$domain/html/index.html
}
function vhost_https_server_config() {
echo -e "
server {
listen 443 ssl;
listen [::]:443 ssl;
ssl_certificate /etc/ssl/certs/$domain.crt;
ssl_certificate_key /etc/ssl/private/$domain.key;
include snippets/ssl-params.conf;
root /var/www/$domain/html;
index index.html index.htm index.nginx-debian.html;
server_name $domain www.$domain;
location / {
try_files \$uri \$uri/ =404;
}
}
server {
listen 80;
listen [::]:80;
server_name $domain www.$domain;
return 302 https://\$server_name\$request_uri;
}" > /etc/nginx/sites-available/$domain
cp /etc/nginx/sites-available/$domain /etc/nginx/sites-enabled/
echo -e "<!DOCTYPE html>
<html lang="es">
<title>DHM S.L</title>
<body>
<h1>Bienvenido a tu dominio!</h1>
<p>Esta es la pagina default de $user,
para editar tu pagina web entra por SFTP.</p>
</body>
</html>" > /var/www/$domain/html/index.html
echo -e "
ssl_protocols TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/nginx/dhparam.pem;
ssl_ciphers EECDH+AESGCM:EDH+AESGCM;
ssl_ecdh_curve secp384r1;
ssl_session_timeout 10m;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection \"1; mode=block\";
" > /etc/nginx/snippets/ssl-params.conf
cp dhparam.pem /etc/nginx/
}
# Zona del script modular
# User and passwd creation
newcomer
# Creation of an sftp user and password
sftp_configuration
# Creation of a self-signed certificate
ssl_ssk
# Do not comment this lines (automatic detection of https or http server, it will act based on if ssl_ssk is commented out)
if [ "$execute_flag_ssk" -eq 1 ]; then
vhost_https_server_config
else
vhost_http_server_config
fi
systemctl restart nginx sshd
nginx -t