Skip to content

Commit 8fa96d3

Browse files
authored
Add check for adding / changing mx record (hestiacp#2559)
* Add check for adding / changing mx record Rename test Add tests for DNS records Fix bug for mx records failure Include tests for CNAME, SERV and MX Fix command Add checks of record has been added * Fix issue with TXT > 255 chars * Change vars
1 parent 36029fa commit 8fa96d3

File tree

5 files changed

+218
-21
lines changed

5 files changed

+218
-21
lines changed

bin/v-change-dns-record

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ domain=$2
1616
domain_idn=$2
1717
id=$3
1818
record=$4
19-
type=$5
19+
rtype=$5
2020
dvalue=$(idn -t --quiet -u "$6" )
2121
priority=$7
2222
restart=$8
@@ -42,7 +42,7 @@ format_domain_idn
4242
#----------------------------------------------------------#
4343

4444
check_args '6' "$#" 'USER DOMAIN ID RECORD TYPE VALUE [PRIORITY] [RESTART] [TTL]'
45-
is_format_valid 'user' 'domain' 'id' 'record' 'type' 'dvalue'
45+
is_format_valid 'user' 'domain' 'id' 'record'
4646
is_system_enabled "$DNS_SYSTEM" 'DNS_SYSTEM'
4747
is_object_valid 'user' 'USER' "$user"
4848
is_object_unsuspended 'user' 'USER' "$user"
@@ -66,31 +66,47 @@ unset TTL
6666
line=$(grep "ID='$id'" $USER_DATA/dns/$domain.conf)
6767
parse_object_kv_list "$line"
6868

69-
if [ -z "$type" ]; then
70-
type=$TYPE
69+
if [ -z "$rtype" ]; then
70+
rtype=$TYPE
71+
fi
72+
73+
if [ -z "$priority" ]; then
74+
priority=$PRIORITY
7175
fi
7276

7377
# Null priority for none MX/SRV records
74-
if [ "$type" != 'MX' ] && [ "$TYPE" != 'SRV' ]; then
78+
if [ "$rtype" != 'MX' ] && [ "$rtype" != 'SRV' ]; then
7579
priority=''
7680
fi
7781

7882
# Add trailing dot at the end of NS/CNAME/MX/PTR/SRV record
79-
if [[ $type =~ NS|CNAME|MX|PTR|SRV ]]; then
83+
if [[ $rtype =~ NS|CNAME|MX|PTR|SRV ]]; then
8084
trailing_dot=$(echo "$dvalue" | grep "\.$")
8185
if [ -z "$trailing_dot" ]; then
8286
dvalue="$dvalue."
8387
fi
8488
fi
8589

90+
if [ "$rtype" != "CAA" ]; then
91+
dvalue=${dvalue//\"/}
92+
93+
if [ "$rtype" != 'SRV' ] && [[ "$dvalue" =~ [\;[:space:]] ]]; then
94+
dvalue='"'"$dvalue"'"'
95+
fi
96+
fi
97+
98+
99+
#RTYPE wasn't checked make sure to do it now correctly
100+
is_format_valid 'user' 'domain' 'id' 'record' 'rtype' 'dvalue'
101+
86102
# Additional verifications
87103
is_dns_fqnd "$TYPE" "$dvalue"
88104
is_dns_nameserver_valid "$domain" "$TYPE" "$dvalue"
89105

90-
if [[ "$RECORD" == "$record" ]] && [[ "$TYPE" == "$type" ]] && [[ "$PRIORITY" -eq "$priority" ]] \
106+
if [[ "$RECORD" == "$record" ]] && [[ "$TYPE" == "$rtype" ]] && [[ "$PRIORITY" -eq "$priority" ]] \
91107
&& [[ "$VALUE" == "$dvalue" ]] && [[ "$SUSPENDED" == 'no' ]] && [[ "$TTL" -eq "$ttl" ]]; then
92108
echo "No pending changes in DNS entry."
93-
exit "$E_EXSIST"
109+
exit "$E_EXISTS"
94110
fi
95111

96112
# Generating timestamp
@@ -99,7 +115,7 @@ time=$(echo "$time_n_date" |cut -f 1 -d \ )
99115
date=$(echo "$time_n_date" |cut -f 2 -d \ )
100116

101117
# Adding record
102-
dns_rec="ID='$id' RECORD='$record' TYPE='$type' PRIORITY='$priority'"
118+
dns_rec="ID='$id' RECORD='$record' TYPE='$rtype' PRIORITY='$priority'"
103119
dns_rec="$dns_rec VALUE='$dvalue' SUSPENDED='no' TIME='$time' DATE='$date'"
104120
[ -n "$ttl" ] && dns_rec="$dns_rec TTL='$ttl'"
105121
# Deleting old record
@@ -135,7 +151,7 @@ $BIN/v-restart-dns "$restart"
135151
check_result $? "DNS restart failed" >/dev/null
136152

137153
# Logging
138-
$BIN/v-log-action "$user" "Info" "DNS" "DNS record value changed (Type: $type, Record: $record, Value: $dvalue, Domain: $domain)."
154+
$BIN/v-log-action "$user" "Info" "DNS" "DNS record value changed (Type: $rtype, Record: $record, Value: $dvalue, Domain: $domain)."
139155
log_event "$OK" "$ARGUMENTS"
140156

141157
exit

func/domain.sh

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,22 @@ update_domain_zone() {
512512
if [ "$TYPE" = 'CNAME' ] || [ "$TYPE" = 'MX' ]; then
513513
VALUE=$(idn --quiet -a -t "$VALUE")
514514
fi
515-
516-
if [ "$TYPE" = 'TXT' ] && [[ ${VALUE:0:1} != '"' ]]; then
517-
VALUE=$(echo $VALUE | fold -w 255 | xargs -I '$' echo -n '"$"')
515+
516+
if [ "$TYPE" = 'TXT' ]; then
517+
txtlength=${#VALUE}
518+
if [ $txtlength -gt 255 ]; then
519+
already_chunked=0
520+
if [[ $VALUE == *"\" \""* ]] || [[ $VALUE == *"\"\""* ]]; then
521+
already_chunked=1
522+
fi
523+
if [ $already_chunked -eq 0 ]; then
524+
if [[ ${VALUE:0:1} = '"' ]]; then
525+
txtlength=$(( $txtlength - 2 ))
526+
VALUE=${VALUE:1:txtlength}
527+
fi
528+
VALUE=$(echo $VALUE | fold -w 255 | xargs -I '$' echo -n '"$"')
529+
fi
530+
fi
518531
fi
519532

520533
if [ "$SUSPENDED" != 'yes' ]; then

func/main.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ is_common_format_valid() {
890890
}
891891

892892
is_no_new_line_format() {
893-
test=$(echo $1 | sed -e 's/\.*$//g' -e 's/^\.*//g');
893+
test=$(echo $1 | head -n1 );
894894
if [[ "$test" != "$1" ]]; then
895895
check_result "$E_INVALID" "invalid value :: $1"
896896
fi

test/checks.bats

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,35 @@ r' "key"
149149
run is_domain_format_valid '..' "key"
150150
assert_failure $E_INVALID
151151
}
152+
153+
@test "is_domain_format_valid hestiacp.com." {
154+
run is_domain_format_valid 'mx.hestiacp.com.' "key"
155+
assert_success
156+
}
157+
152158
@test "is_domain_format_valid LF." {
153159
run is_domain_format_valid 'c
154160
1eshutdown
155161
r' "key"
156162
assert_failure $E_INVALID
157163
}
158164

165+
@test "is_dns_record_format_valid" {
166+
rtype='MX'
167+
priority=1;
168+
run is_dns_record_format_valid 'mx.hestiacp.com.'
169+
assert_success
170+
}
171+
172+
@test "is_dns_record_format_valid test" {
173+
rtype='MX'
174+
priority=1;
175+
run is_dns_record_format_valid 'c
176+
1eshutdown
177+
r'
178+
assert_failure $E_INVALID
179+
}
180+
159181
@test "is_alias_format_valid success" {
160182
run is_domain_format_valid 'hestiacp.com' "key"
161183
assert_success
@@ -223,13 +245,6 @@ r' "key"
223245
assert_failure $E_INVALID
224246
}
225247

226-
@test "is_dns_record_format_valid test" {
227-
run is_dns_record_format_valid 'c
228-
1eshutdown
229-
r' "key"
230-
assert_failure $E_INVALID
231-
}
232-
233248
@test "is_email_format_valid test" {
234249
run is_email_format_valid 'c
235250
1eshutdown
@@ -302,6 +317,22 @@ r' "key"
302317
assert_failure $E_INVALID
303318
}
304319

320+
@test "format_no_quotes .." {
321+
run format_no_quotes '..' "key"
322+
assert_success
323+
}
324+
325+
@test "format_no_quotes text." {
326+
run format_no_quotes 'text.' "key"
327+
assert_success
328+
}
329+
330+
@test "is_common_format_valid text" {
331+
run is_common_format_valid 'text' "key"
332+
assert_success
333+
}
334+
335+
305336
@test "format_no_quotes test" {
306337
run format_no_quotes 'c
307338
1eshutdown

test/test.bats

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,143 @@ function check_ip_not_banned(){
11641164
refute_output
11651165
}
11661166

1167+
@test "DNS: Add domain record MX" {
1168+
run v-add-dns-record $user $domain '@' MX mx.hestiacp.com '' 50
1169+
assert_success
1170+
refute_output
1171+
1172+
assert_file_contains "$HOMEDIR/$user/conf/dns/${domain}.db" "mx.hestiacp.com."
1173+
1174+
run v-change-dns-record $user $domain 50 '@' MX mx.hestia.com
1175+
assert_success
1176+
refute_output
1177+
1178+
assert_file_contains "$HOMEDIR/$user/conf/dns/${domain}.db" "mx.hestia.com."
1179+
1180+
run v-delete-dns-record $user $domain 50
1181+
assert_success
1182+
refute_output
1183+
}
1184+
1185+
@test "DNS: Add domain record NS" {
1186+
run v-delete-dns-record $user $domain 50
1187+
run v-add-dns-record $user $domain '@' NS mx.hestiacp.com '' 50
1188+
assert_success
1189+
refute_output
1190+
1191+
assert_file_contains "$HOMEDIR/$user/conf/dns/${domain}.db" "mx.hestiacp.com."
1192+
1193+
run v-change-dns-record $user $domain 50 '@' NS mx.hestia.com
1194+
assert_success
1195+
refute_output
1196+
1197+
assert_file_contains "$HOMEDIR/$user/conf/dns/${domain}.db" "mx.hestia.com."
1198+
1199+
run v-delete-dns-record $user $domain 50
1200+
assert_success
1201+
refute_output
1202+
}
1203+
1204+
@test "DNS: Add domain record SRV" {
1205+
run v-delete-dns-record $user $domain 50
1206+
run v-add-dns-record $user $domain '_test_domain' SRV mx.hestiacp.com '' 50
1207+
assert_success
1208+
refute_output
1209+
1210+
assert_file_contains "$HOMEDIR/$user/conf/dns/${domain}.db" "mx.hestiacp.com."
1211+
1212+
run v-change-dns-record $user $domain 50 '_test.domain' SRV mx.hestia.com
1213+
assert_success
1214+
refute_output
1215+
1216+
assert_file_contains "$HOMEDIR/$user/conf/dns/${domain}.db" "mx.hestia.com."
1217+
1218+
run v-delete-dns-record $user $domain 50
1219+
assert_success
1220+
refute_output
1221+
}
1222+
1223+
@test "DNS: Add domain record CNAME" {
1224+
run v-delete-dns-record $user $domain 50
1225+
run v-add-dns-record $user $domain 'mail' CNAME mx.hestiacp.com '' 50
1226+
assert_success
1227+
refute_output
1228+
1229+
assert_file_contains "$HOMEDIR/$user/conf/dns/${domain}.db" "mx.hestiacp.com."
1230+
1231+
run v-change-dns-record $user $domain 50 'mail' CNAME mx.hestia.com
1232+
assert_success
1233+
refute_output
1234+
1235+
assert_file_contains "$HOMEDIR/$user/conf/dns/${domain}.db" "mx.hestia.com."
1236+
1237+
run v-delete-dns-record $user $domain 50
1238+
assert_success
1239+
refute_output
1240+
}
1241+
1242+
@test "DNS: Check txt dns records type1" {
1243+
[ -z "$DNS_SYSTEM" ] && skip
1244+
1245+
run v-delete-dns-record $user $domain 50
1246+
1247+
record1_in='v=DMARC1; p=quarantine; pct=100'
1248+
record2_in='v=DMARC1; p=quarantine; pct=90'
1249+
1250+
record1_out='"v=DMARC1; p=quarantine; pct=100"'
1251+
record2_in='"v=DMARC1; p=quarantine; pct=90"'
1252+
1253+
# Test Create
1254+
run v-add-dns-record $user $domain 'test-long-txt' 'TXT' "$record1_in" '' 50
1255+
assert_success
1256+
refute_output
1257+
1258+
assert_file_contains "$HOMEDIR/$user/conf/dns/${domain}.db" "$record1_out"
1259+
1260+
# Test Edit
1261+
run v-change-dns-record $user $domain 50 'test-long-txt' 'TXT' "$record2_in"
1262+
assert_success
1263+
refute_output
1264+
1265+
assert_file_contains "$HOMEDIR/$user/conf/dns/${domain}.db" "$record2_out"
1266+
1267+
# Test Cleanup
1268+
run v-delete-dns-record $user $domain 50
1269+
assert_success
1270+
refute_output
1271+
}
1272+
1273+
@test "DNS: Check txt dns records type2" {
1274+
[ -z "$DNS_SYSTEM" ] && skip
1275+
1276+
run v-delete-dns-record $user $domain 50
1277+
1278+
record3_in='k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4+VEVsoTbl6tYLJlhozqAGju3IgpSVdBAS5LMyzpHP8/L0/PlyVRJnm2xECjVk3DRqCmelyIvmraw1VtFz2aH6DRlDhHsZghj1DmGhwN+7NkwIb4hEvmytMVAz1WyiLH6Rm6Iemm/ZCt1RhrAMUYLxHA9mJgky76YCcf8/cX35xC+1vd4a5U6YofAZeVP9DBvVgQ8ung4gVrOrQrXkU8QfVNAoXz5pfJo74GB7woIBFhZXsU6SKho7Kn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raw1VtFz2aH6DRlDhHsZghj1DmGhwN+7NkwIb4hEvmytMVAz1WyiLH6Rm6Iemm/ZCt1RhrAMUYLxHA9mJgky76YCcf8/cX35xC+1vd4a5U6YofAZeVP9DBvVgQ8ung4g""VrOrQrXkU8QfVNAoXz5pfJo74GB7woIBFhZXsU6SKho7KnzT5inVCIOtWp7L5hyEnbySWQPHT2vAMCCAe2AY/Vv0N3HW14o8P3b4A6OU920wFB2kA7pkQNzO5OwH+HSttwG0PaIiQxYQIDAQA4"'
1283+
1284+
# Test Create
1285+
run v-add-dns-record $user $domain 'test-long-txt' 'TXT' "$record3_in" '' 50
1286+
assert_success
1287+
refute_output
1288+
1289+
assert_file_contains "$HOMEDIR/$user/conf/dns/${domain}.db" "$record3_out"
1290+
1291+
# Test Edit
1292+
run v-change-dns-record $user $domain 50 'test-long-txt' 'TXT' "$record4_in"
1293+
assert_success
1294+
refute_output
1295+
1296+
assert_file_contains "$HOMEDIR/$user/conf/dns/${domain}.db" "$record4_out"
1297+
1298+
# Test Cleanup
1299+
run v-delete-dns-record $user $domain 50
1300+
assert_success
1301+
refute_output
1302+
}
1303+
11671304
@test "DNS: Change domain ip" {
11681305
run v-change-dns-domain-ip $user $domain 127.0.0.1
11691306
assert_success

0 commit comments

Comments
 (0)