-
Notifications
You must be signed in to change notification settings - Fork 1k
/
domain.sh
520 lines (469 loc) · 15.4 KB
/
domain.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# Web template check
is_web_template_valid() {
t="$WEBTPL/$WEB_SYSTEM/$template.tpl"
s="$WEBTPL/$WEB_SYSTEM/$template.stpl"
if [ ! -e $t ] || [ ! -e $s ]; then
echo "Error: web template $template not found"
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
fi
}
# Proxy template check
is_proxy_template_valid() {
t="$WEBTPL/$PROXY_SYSTEM/$template.tpl"
s="$WEBTPL/$PROXY_SYSTEM/$template.stpl"
if [ ! -e $t ] || [ ! -e $s ]; then
echo "Error: proxy template $template not found"
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
fi
}
# DNS template check
is_dns_template_valid() {
t="$DNSTPL/$template.tpl"
if [ ! -e $t ]; then
echo "Error: dns template $template not found"
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
fi
}
# Checking domain existance
is_domain_new() {
type="$1"
dom=${2-$domain}
web=$(grep -F -H "DOMAIN='$dom'" $VESTA/data/users/*/web.conf)
dns=$(grep -F -H "DOMAIN='$dom'" $VESTA/data/users/*/dns.conf)
mail=$(grep -F -H "DOMAIN='$dom'" $VESTA/data/users/*/mail.conf)
# Check web domain
if [ ! -z "$web" ] && [ "$type" == 'web' ]; then
echo "Error: domain $dom exist"
log_event "$E_EXISTS" "$EVENT"
exit $E_EXISTS
fi
if [ ! -z "$web" ]; then
web_user=$(echo "$web" |cut -f 7 -d /)
if [ "$web_user" != "$user" ]; then
echo "Error: domain $dom exist"
log_event "$E_EXISTS" "$EVENT"
exit $E_EXISTS
fi
fi
# Check dns domain
if [ ! -z "$dns" ] && [ "$type" == 'dns' ]; then
echo "Error: domain $dom exist"
log_event "$E_EXISTS" "$EVENT"
exit $E_EXISTS
fi
if [ ! -z "$dns" ]; then
dns_user=$(echo "$dns" |cut -f 7 -d /)
if [ "$dns_user" != "$user" ]; then
echo "Error: domain $dom exist"
log_event "$E_EXISTS" "$EVENT"
exit $E_EXISTS
fi
fi
# Check mail domain
if [ ! -z "$mail" ] && [ "$type" == 'mail' ]; then
echo "Error: domain $dom exist"
log_event "$E_EXISTS" "$EVENT"
exit $E_EXISTS
fi
if [ ! -z "$mail" ]; then
mail_user=$(echo "$mail" |cut -f 7 -d /)
if [ "$mail_user" != "$user" ]; then
echo "Error: domain $dom exist"
log_event "$E_EXISTS" "$EVENT"
exit $E_EXISTS
fi
fi
# Check web aliases
web_alias=$(grep -w $dom $VESTA/data/users/*/web.conf)
if [ ! -z "$web_alias" ]; then
c1=$(grep -H "'$dom'" $VESTA/data/users/*/web.conf | cut -f 7 -d /)
c2=$(grep -H "'$dom," $VESTA/data/users/*/web.conf | cut -f 7 -d /)
c3=$(grep -H ",$dom," $VESTA/data/users/*/web.conf | cut -f 7 -d /)
c4=$(grep -H ",$dom'" $VESTA/data/users/*/web.conf | cut -f 7 -d /)
if [ ! -z "$c1" ] && [ "$type" == "web" ]; then
echo "Error: domain $dom exist"
log_event "$E_EXISTS" "$EVENT"
exit $E_EXISTS
fi
if [ ! -z "$c1" ] && [ "$c1" != "$user" ]; then
echo "Error: domain $dom exist"
log_event "$E_EXISTS" "$EVENT"
exit $E_EXISTS
fi
if [ ! -z "$c2" ] && [ "$type" == "web" ]; then
echo "Error: domain $dom exist"
log_event "$E_EXISTS" "$EVENT"
exit $E_EXISTS
fi
if [ ! -z "$c2" ] && [ "$c2" != "$user" ]; then
echo "Error: domain $dom exist"
log_event "$E_EXISTS" "$EVENT"
exit $E_EXISTS
fi
if [ ! -z "$c3" ] && [ "$type" == "web" ]; then
echo "Error: domain $dom exist"
log_event "$E_EXISTS" "$EVENT"
exit $E_EXISTS
fi
if [ ! -z "$c3" ] && [ "$c3" != "$user" ]; then
echo "Error: domain $dom exist"
log_event "$E_EXISTS" "$EVENT"
exit $E_EXISTS
fi
if [ ! -z "$c4" ] && [ "$type" == "web" ]; then
echo "Error: domain $dom exist"
log_event "$E_EXISTS" "$EVENT"
exit $E_EXISTS
fi
if [ ! -z "$c4" ] && [ "$c4" != "$user" ]; then
echo "Error: domain $dom exist"
log_event "$E_EXISTS" "$EVENT"
exit $E_EXISTS
fi
fi
}
# Checking mail account existance
is_mail_new() {
check_acc=$(grep "ACCOUNT='$1'" $USER_DATA/mail/$domain.conf)
if [ ! -z "$check_acc" ]; then
echo "Error: mail account $1 exist"
log_event "$E_EXISTS" "$EVENT"
exit $E_EXISTS
fi
check_als=$(awk -F "ALIAS='" '{print $2}' $USER_DATA/mail/$domain.conf )
check_als=$(echo "$check_als" | cut -f 1 -d "'" | grep -w $1)
if [ ! -z "$check_als" ]; then
echo "Error: mail alias $1 exist"
log_event "$E_EXISTS" "$EVENT"
exit $E_EXISTS
fi
}
# Update domain zone
update_domain_zone() {
conf="$HOMEDIR/$user/conf/dns/$domain.db"
line=$(grep "DOMAIN='$domain'" $USER_DATA/dns.conf)
fields='$RECORD\t$TTL\tIN\t$TYPE\t$PRIORITY\t$VALUE'
if [ -e $conf ]; then
zn_serial=$(head $conf|grep 'SOA' -A1|tail -n 1|sed "s/ //g")
s_date=$(echo ${zn_serial:0:8})
c_date=$(date +'%Y%m%d')
if [ "$s_date" == "$c_date" ]; then
cur_value=$(echo ${zn_serial:8} )
new_value=$(expr $cur_value + 1 )
len_value=$(expr length $new_value)
if [ 1 -eq "$len_value" ]; then
new_value='0'$new_value
fi
serial="$c_date""$new_value"
else
serial="$(date +'%Y%m%d01')"
fi
else
serial="$(date +'%Y%m%d01')"
fi
eval $line
SOA=$(idn --quiet -a -t "$SOA")
echo "\$TTL $TTL
@ IN SOA $SOA. root.$domain_idn. (
$serial
7200
3600
1209600
180 )
" > $conf
while read line ; do
IFS=$'\n'
for key in $(echo $line|sed "s/' /'\n/g"); do
eval ${key%%=*}="${key#*=}"
done
RECORD=$(idn --quiet -a -t "$RECORD")
if [ "$TYPE" = 'CNAME' ] || [ "$TYPE" = 'MX' ]; then
VALUE=$(idn --quiet -a -t "$VALUE")
fi
if [ "$SUSPENDED" != 'yes' ]; then
eval echo -e "\"$fields\""|sed "s/%quote%/'/g" >> $conf
fi
done < $USER_DATA/dns/$domain.conf
}
# Get next DNS record ID
get_next_dnsrecord(){
if [ -z "$id" ]; then
curr_str=$(grep "ID=" $USER_DATA/dns/$domain.conf | cut -f 2 -d \' |\
sort -n|tail -n1)
id="$((curr_str +1))"
fi
}
# Sort DNS records
sort_dns_records() {
conf="$USER_DATA/dns/$domain.conf"
cat $conf |sort -n -k 2 -t \' >$conf.tmp
mv -f $conf.tmp $conf
}
# Add web config
add_web_config() {
cat $tpl_file | \
sed -e "s/%ip%/$ip/g" \
-e "s/%web_system%/$WEB_SYSTEM/g" \
-e "s/%web_port%/$WEB_PORT/g" \
-e "s/%web_ssl_port%/$WEB_SSL_PORT/g" \
-e "s/%rgroups%/$WEB_RGROUPS/g" \
-e "s/%proxy_system%/$PROXY_SYSTEM/g" \
-e "s/%proxy_port%/$PROXY_PORT/g" \
-e "s/%proxy_ssl_port%/$PROXY_SSL_PORT/g" \
-e "s/%proxy_extentions%/${PROXY_EXT//,/|}/g" \
-e "s/%domain_idn%/$domain_idn/g" \
-e "s/%domain%/$domain/g" \
-e "s/%user%/$user/g" \
-e "s/%group%/$group/g" \
-e "s/%home%/${HOMEDIR////\/}/g" \
-e "s/%docroot%/${docroot////\/}/g" \
-e "s/%sdocroot%/${sdocroot////\/}/g" \
-e "s/%email%/$email/g" \
-e "s/%alias_string%/$alias_string/g" \
-e "s/%alias_idn%/${aliases_idn//,/ }/g" \
-e "s/%alias%/${aliases//,/ }/g" \
-e "s/%ssl_crt%/${ssl_crt////\/}/g" \
-e "s/%ssl_key%/${ssl_key////\/}/g" \
-e "s/%ssl_pem%/${ssl_pem////\/}/g" \
-e "s/%ssl_ca_str%/${ssl_ca_str////\/}/g" \
-e "s/%ssl_ca%/${ssl_ca////\/}/g" \
>> $conf
}
# Get config top and bottom line numbers
get_web_config_brds() {
serv_line=$(grep -ni 'Name %domain_idn%' "$tpl_file" |cut -f 1 -d :)
if [ -z "$serv_line" ]; then
log_event "$E_PARSING" "$EVENT"
return $E_PARSING
fi
last_line=$(wc -l $tpl_file|cut -f 1 -d ' ')
bfr_line=$((serv_line - 1))
aftr_line=$((last_line - serv_line - 1))
str=$(grep -ni "Name $domain_idn" $conf | cut -f 1 -d :)
top_line=$((str - serv_line + 1))
bottom_line=$((top_line + last_line -1))
multi=$(sed -n "$top_line,$bottom_line p" $conf |grep ServerAlias |wc -l)
if [ "$multi" -ge 2 ]; then
bottom_line=$((bottom_line + multi -1))
fi
}
# Change web config
change_web_config() {
get_web_config_brds || exit $?
vhost=$(grep -A $aftr_line -B $bfr_line -ni "Name $domain_idn" $conf)
str=$(echo "$vhost" | grep -F "$search_phrase" | head -n 1)
str_numb=$(echo "$str" | sed "s/-/=/" | cut -f 1 -d '=')
str_cont=$(echo "$str" | sed "s/-/=/" | cut -f 2 -d '=')
str_repl=$(echo "$str_repl" | sed \
-e 's/\\/\\\\/g' \
-e 's/&/\\&/g' \
-e 's/\//\\\//g')
if [ ! -z "$str" ]; then
sed -i "$str_numb s/.*/$str_repl/" $conf
fi
}
# Replace web config
replace_web_config() {
get_web_config_brds || exit $?
clean_new=$(echo "$new" | sed \
-e 's/\\/\\\\/g' \
-e 's/&/\\&/g' \
-e 's/\//\\\//g')
clean_old=$(echo "$old" | sed \
-e 's/\\/\\\\/g' \
-e 's/&/\\&/g' \
-e 's/\//\\\//g')
sed -i "$top_line,$bottom_line s/$clean_old/$clean_new/" $conf
}
# Get domain variables
get_domain_values() {
for line in $(grep "DOMAIN='$domain'" $USER_DATA/$1.conf); do
eval $line
done
}
# SSL certificate verification
is_web_domain_cert_valid() {
if [ ! -e "$ssl_dir/$domain.crt" ]; then
echo "Error: $ssl_dir/$domain.crt not found"
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
fi
if [ ! -e "$ssl_dir/$domain.key" ]; then
echo "Error: $ssl_dir/$domain.key not found"
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
fi
crt_vrf=$(openssl verify $ssl_dir/$domain.crt 2>&1)
if [ ! -z "$(echo $crt_vrf | grep 'unable to load')" ]; then
echo "Error: certificate is not valid"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
if [ ! -z "$(echo $crt_vrf | grep 'unable to get local issuer')" ]; then
if [ ! -e "$ssl_dir/$domain.ca" ]; then
echo "Error: certificate authority not found"
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
fi
fi
if [ -e "$ssl_dir/$domain.ca" ]; then
ca_vrf=$(openssl verify $ssl_dir/$domain.ca 2>/dev/null |grep 'OK')
if [ -z "$ca_vrf" ]; then
echo "Error: ssl certificate authority is not valid"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
crt_vrf=$(openssl verify -untrusted $ssl_dir/$domain.ca \
$ssl_dir/$domain.crt 2>/dev/null |grep 'OK')
if [ -z "$crt_vrf" ]; then
echo "Error: root or/and intermediate cerificate not found"
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
fi
fi
key_vrf=$(grep 'PRIVATE KEY' $ssl_dir/$domain.key | wc -l)
if [ "$key_vrf" -ne 2 ]; then
echo "Error: ssl key is not valid"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
openssl s_server -quiet -cert $ssl_dir/$domain.crt \
-key $ssl_dir/$domain.key >> /dev/null 2>&1 &
pid=$!
sleep 0.5
disown &> /dev/null
kill $pid &> /dev/null
if [ "$?" -ne '0' ]; then
echo "Error: ssl certificate key pair is not valid"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
}
# Delete web configuartion
del_web_config() {
get_web_config_brds || exit $?
sed -i "$top_line,$bottom_line d" $conf
}
# Update web domain values
upd_web_domain_values() {
group="$user"
email="info@$domain"
docroot="$HOMEDIR/$user/web/$domain/public_html"
sdocroot=$docroot
if [ "$SSL_HOME" = 'single' ]; then
sdocroot="$HOMEDIR/$user/web/$domain/public_shtml" ;
fi
i=1
j=1
OLD_IFS="$IFS"
IFS=','
server_alias=''
alias_string=''
aliases_idn=''
for dalias in $ALIAS; do
dalias=$(idn -t --quiet -a $dalias)
check_8k="$server_alias $dalias"
if [ "${#check_8k}" -ge '8100' ]; then
if [ "$j" -eq 1 ]; then
alias_string="ServerAlias $server_alias"
else
alias_string="$alias_string\n ServerAlias $server_alias"
fi
j=2
server_alias=''
fi
if [ "$i" -eq 1 ]; then
aliases_idn="$dalias"
server_alias="$dalias"
alias_string="ServerAlias $server_alias"
else
aliases_idn="$aliases_idn,$dalias"
server_alias="$server_alias $dalias"
fi
i=2
done
if [ $j -gt 1 ]; then
alias_string="$alias_string\n ServerAlias $server_alias"
else
alias_string="ServerAlias $server_alias"
fi
IFS=$OLD_IFS
if [ "$ELOG" = 'no' ]; then
elog='#'
else
elog=''
fi
if [ "$CGI" != 'yes' ]; then
cgi='#'
cgi_option='-ExecCGI'
else
cgi=''
cgi_option='+ExecCGI'
fi
ssl_crt="$HOMEDIR/$user/conf/web/ssl.$domain.crt"
ssl_key="$HOMEDIR/$user/conf/web/ssl.$domain.key"
ssl_pem="$HOMEDIR/$user/conf/web/ssl.$domain.pem"
ssl_ca="$HOMEDIR/$user/conf/web/ssl.$domain.ca"
if [ ! -e "$USER_DATA/ssl/$domain.ca" ]; then
ssl_ca_str='#'
fi
if [ "$SUSPENDED" = 'yes' ]; then
docroot="$VESTA/data/templates/web/suspend"
sdocroot="$VESTA/data/templates/web/suspend"
fi
}
# Check if this is a last record
is_dns_record_critical() {
str=$(grep "ID='$id'" $USER_DATA/dns/$domain.conf)
eval $str
if [ "$TYPE" = 'A' ] || [ "$TYPE" = 'NS' ]; then
records=$(grep "TYPE='$TYPE'" $USER_DATA/dns/$domain.conf| wc -l)
if [ $records -le 1 ]; then
echo "Error: at least one $TYPE record should remain active"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
fi
}
# Check if dns record is valid
is_dns_fqnd() {
t=$1
r=$2
fqdn_type=$(echo $t | grep "NS\|CNAME\|MX\|PTR\|SRV")
tree_length=3
if [ $t = 'CNAME' ]; then
tree_length=2
fi
if [ ! -z "$fqdn_type" ]; then
dots=$(echo $dvalue | grep -o "\." | wc -l)
if [ "$dots" -lt "$tree_length" ]; then
r=$(echo $r|sed -e "s/\.$//")
msg="$t record $r should be a fully qualified domain name (FQDN)"
echo "Error: $msg"
log_event "$E_INVALID" "$EVENT"
exit $E_INVALID
fi
fi
}
# Validate nameserver
is_dns_nameserver_valid() {
d=$1
t=$2
r=$3
if [ "$t" = 'NS' ]; then
remote=$(echo $r |grep ".$domain.$")
if [ ! -z "$remote" ]; then
zone=$USER_DATA/dns/$d.conf
a_record=$(echo $r |cut -f 1 -d '.')
record=$(grep "RECORD='$a_record'" $zone| grep "TYPE='A'")
if [ -z "$record" ]; then
echo "Error: corresponding A record $a_record.$d is not exist"
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
fi
fi
fi
}