Skip to content

Commit add7d14

Browse files
committed
additional dns record validation
1 parent f7cc1b9 commit add7d14

File tree

4 files changed

+91
-6
lines changed

4 files changed

+91
-6
lines changed

bin/v-add-dns-record

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ source $VESTA/func/main.sh
3333
source $VESTA/func/domain.sh
3434
source $VESTA/conf/vesta.conf
3535

36+
# Null priority for none MX/SRV records
37+
if [ "$rtype" != 'MX' ] && [ "$rtype" != 'SRV' ]; then
38+
priority=''
39+
fi
40+
41+
# Add trailing dot at the end of NS/CNAME/MX/PTR/SRV record
42+
fqdn_type=$(echo $rtype | grep "[NS|CNAME|MX|PTR|SRV]")
43+
if [ ! -z "$fqdn_type" ]; then
44+
trailing_dot=$(echo $dvalue | grep "\.$")
45+
if [ -z $trailing_dot ]; then
46+
dvalue="$dvalue."
47+
fi
48+
fi
49+
3650

3751
#----------------------------------------------------------#
3852
# Verifications #
@@ -49,16 +63,14 @@ is_package_full 'DNS_RECORDS'
4963
get_next_dnsrecord
5064
validate_format 'id'
5165
is_object_new "dns/$domain" 'ID' "$id"
66+
is_dns_fqnd "$rtype" "$dvalue"
67+
is_dns_nameserver_valid "$domain" "$rtype" "$dvalue"
5268

5369

5470
#----------------------------------------------------------#
5571
# Action #
5672
#----------------------------------------------------------#
5773

58-
if [ "$rtype" != 'MX' ] && [ "$rtype" != 'SRV' ]; then
59-
priority=''
60-
fi
61-
6274
# Adding record
6375
zone="$USER_DATA/dns/$domain.conf"
6476
dns_rec="ID='$id' RECORD='$record' TYPE='$rtype' PRIORITY='$priority'"
@@ -67,7 +79,7 @@ echo "$dns_rec" >> $zone
6779
chmod 660 $zone
6880

6981
# Sorting records
70-
sort_dns_records
82+
sort_dns_records
7183

7284
# Updating zone
7385
update_domain_zone

bin/v-change-dns-record

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,29 @@ is_object_valid "dns/$domain" 'ID' "$id"
4444
# Action #
4545
#----------------------------------------------------------#
4646

47-
# Deleting old record
47+
# Parsing domain config
4848
line=$(grep "ID='$id'" $USER_DATA/dns/$domain.conf)
4949
eval $line
50+
51+
# Null priority for none MX/SRV records
5052
if [ "$TYPE" != 'MX' ] && [ "$TYPE" != 'SRV' ]; then
5153
priority=''
5254
fi
55+
56+
# Add trailing dot at the end of NS/CNAME/MX/PTR/SRV record
57+
fqdn_type=$(echo $TYPE | grep "[NS|CNAME|MX|PTR|SRV]")
58+
if [ ! -z "$fqdn_type" ]; then
59+
trailing_dot=$(echo $dvalue | grep "\.$")
60+
if [ -z $trailing_dot ]; then
61+
dvalue="$dvalue."
62+
fi
63+
fi
64+
65+
# Additional verifications
66+
is_dns_fqnd "$TYPE" "$dvalue"
67+
is_dns_nameserver_valid "$domain" "$TYPE" "$dvalue"
68+
69+
# Deleting old record
5370
sed -i "/^ID='$id'/d" $USER_DATA/dns/$domain.conf
5471

5572
# Adding record

bin/v-delete-dns-record

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ is_object_unsuspended 'user' 'USER' "$user"
3333
is_object_valid 'dns' 'DOMAIN' "$domain"
3434
is_object_unsuspended 'dns' 'DOMAIN' "$domain"
3535
is_object_valid "dns/$domain" 'ID' "$id"
36+
is_dns_record_critical
3637

3738

3839
#----------------------------------------------------------#

func/domain.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,3 +459,58 @@ upd_web_domain_values() {
459459
fi
460460
}
461461

462+
# Check if this is a last record
463+
is_dns_record_critical() {
464+
str=$(grep "ID='$id'" $USER_DATA/dns/$domain.conf)
465+
eval $str
466+
if [ "$TYPE" = 'A' ] || [ "$TYPE" = 'NS' ]; then
467+
records=$(grep "TYPE='$TYPE'" $USER_DATA/dns/$domain.conf| wc -l)
468+
if [ $records -le 1 ]; then
469+
echo "Error: at least one $TYPE record should remain active"
470+
log_event "$E_INVALID" "$EVENT"
471+
exit $E_INVALID
472+
fi
473+
fi
474+
}
475+
476+
# Check if dns record is valid
477+
is_dns_fqnd() {
478+
t=$1
479+
r=$2
480+
fqdn_type=$(echo $t | grep "[NS|CNAME|MX|PTR|SRV]")
481+
tree_length=3
482+
if [ $t = 'CNAME' ]; then
483+
tree_length=2
484+
fi
485+
486+
if [ ! -z "$fqdn_type" ]; then
487+
dots=$(echo $dvalue | grep -o "\." | wc -l)
488+
if [ "$dots" -lt "$tree_length" ]; then
489+
r=$(echo $r|sed -e "s/\.$//")
490+
msg="$t record $r should be a fully qualified domain name (FQDN)"
491+
echo "Error: $msg"
492+
log_event "$E_INVALID" "$EVENT"
493+
exit $E_INVALID
494+
fi
495+
fi
496+
}
497+
498+
# Validate nameserver
499+
is_dns_nameserver_valid() {
500+
d=$1
501+
t=$2
502+
r=$3
503+
if [ "$t" = 'NS' ]; then
504+
remote=$(echo $r |grep ".$domain.$")
505+
if [ ! -z "$remote" ]; then
506+
zone=$USER_DATA/dns/$d.conf
507+
a_record=$(echo $r |cut -f 1 -d '.')
508+
record=$(grep "RECORD='$a_record'" $zone| grep "TYPE='A'")
509+
if [ -z "$record" ]; then
510+
echo "Error: corresponding A record $a_record.$d is not exist"
511+
log_event "$E_NOTEXIST" "$EVENT"
512+
exit $E_NOTEXIST
513+
fi
514+
fi
515+
fi
516+
}

0 commit comments

Comments
 (0)