Browse Source

Add check for adding / changing mx record (#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
Jaap Marcus 3 years ago
parent
commit
dc0dd89f0b
5 changed files with 218 additions and 21 deletions
  1. 26 10
      bin/v-change-dns-record
  2. 16 3
      func/domain.sh
  3. 1 1
      func/main.sh
  4. 38 7
      test/checks.bats
  5. 137 0
      test/test.bats

+ 26 - 10
bin/v-change-dns-record

@@ -16,7 +16,7 @@ domain=$2
 domain_idn=$2
 domain_idn=$2
 id=$3
 id=$3
 record=$4
 record=$4
-type=$5
+rtype=$5
 dvalue=$(idn -t --quiet -u "$6" )
 dvalue=$(idn -t --quiet -u "$6" )
 priority=$7
 priority=$7
 restart=$8
 restart=$8
@@ -42,7 +42,7 @@ format_domain_idn
 #----------------------------------------------------------#
 #----------------------------------------------------------#
 
 
 check_args '6' "$#" 'USER DOMAIN ID RECORD TYPE VALUE [PRIORITY] [RESTART] [TTL]'
 check_args '6' "$#" 'USER DOMAIN ID RECORD TYPE VALUE [PRIORITY] [RESTART] [TTL]'
-is_format_valid 'user' 'domain' 'id' 'record' 'type' 'dvalue'
+is_format_valid 'user' 'domain' 'id' 'record'
 is_system_enabled "$DNS_SYSTEM" 'DNS_SYSTEM'
 is_system_enabled "$DNS_SYSTEM" 'DNS_SYSTEM'
 is_object_valid 'user' 'USER' "$user"
 is_object_valid 'user' 'USER' "$user"
 is_object_unsuspended 'user' 'USER' "$user"
 is_object_unsuspended 'user' 'USER' "$user"
@@ -66,31 +66,47 @@ unset TTL
 line=$(grep "ID='$id'" $USER_DATA/dns/$domain.conf)
 line=$(grep "ID='$id'" $USER_DATA/dns/$domain.conf)
 parse_object_kv_list "$line"
 parse_object_kv_list "$line"
 
 
-if [ -z "$type" ]; then
-    type=$TYPE
+if [ -z "$rtype" ]; then
+    rtype=$TYPE
+fi
+
+if [ -z "$priority" ]; then
+    priority=$PRIORITY
 fi
 fi
 
 
 # Null priority for none MX/SRV records
 # Null priority for none MX/SRV records
-if [ "$type" != 'MX' ] && [ "$TYPE" != 'SRV' ]; then
+if [ "$rtype" != 'MX' ] && [ "$rtype" != 'SRV' ]; then
     priority=''
     priority=''
 fi
 fi
 
 
 # Add trailing dot at the end of NS/CNAME/MX/PTR/SRV record
 # Add trailing dot at the end of NS/CNAME/MX/PTR/SRV record
-if [[ $type =~ NS|CNAME|MX|PTR|SRV ]]; then
+if [[ $rtype =~ NS|CNAME|MX|PTR|SRV ]]; then
     trailing_dot=$(echo "$dvalue" | grep "\.$")
     trailing_dot=$(echo "$dvalue" | grep "\.$")
     if [ -z "$trailing_dot" ]; then
     if [ -z "$trailing_dot" ]; then
         dvalue="$dvalue."
         dvalue="$dvalue."
     fi
     fi
 fi
 fi
 
 
+if [ "$rtype" != "CAA" ]; then
+    dvalue=${dvalue//\"/}
+
+    if [ "$rtype" != 'SRV' ] && [[ "$dvalue" =~ [\;[:space:]] ]]; then
+        dvalue='"'"$dvalue"'"'
+    fi
+fi
+
+
+#RTYPE wasn't checked make sure to do it now correctly 
+is_format_valid 'user' 'domain' 'id' 'record' 'rtype' 'dvalue'
+
 # Additional verifications
 # Additional verifications
 is_dns_fqnd "$TYPE" "$dvalue"
 is_dns_fqnd "$TYPE" "$dvalue"
 is_dns_nameserver_valid "$domain" "$TYPE" "$dvalue"
 is_dns_nameserver_valid "$domain" "$TYPE" "$dvalue"
 
 
-if [[ "$RECORD" == "$record" ]] && [[ "$TYPE" == "$type" ]] && [[ "$PRIORITY" -eq "$priority" ]] \
+if [[ "$RECORD" == "$record" ]] && [[ "$TYPE" == "$rtype" ]] && [[ "$PRIORITY" -eq "$priority" ]] \
         && [[ "$VALUE" == "$dvalue" ]] && [[ "$SUSPENDED" == 'no' ]] && [[ "$TTL" -eq "$ttl" ]]; then
         && [[ "$VALUE" == "$dvalue" ]] && [[ "$SUSPENDED" == 'no' ]] && [[ "$TTL" -eq "$ttl" ]]; then
     echo "No pending changes in DNS entry."
     echo "No pending changes in DNS entry."
-    exit "$E_EXSIST"
+    exit "$E_EXISTS"
 fi
 fi
 
 
 # Generating timestamp
 # Generating timestamp
@@ -99,7 +115,7 @@ time=$(echo "$time_n_date" |cut -f 1 -d \ )
 date=$(echo "$time_n_date" |cut -f 2 -d \ )
 date=$(echo "$time_n_date" |cut -f 2 -d \ )
 
 
 # Adding record
 # Adding record
-dns_rec="ID='$id' RECORD='$record' TYPE='$type' PRIORITY='$priority'"
+dns_rec="ID='$id' RECORD='$record' TYPE='$rtype' PRIORITY='$priority'"
 dns_rec="$dns_rec VALUE='$dvalue' SUSPENDED='no' TIME='$time' DATE='$date'"
 dns_rec="$dns_rec VALUE='$dvalue' SUSPENDED='no' TIME='$time' DATE='$date'"
 [ -n "$ttl" ] && dns_rec="$dns_rec TTL='$ttl'"
 [ -n "$ttl" ] && dns_rec="$dns_rec TTL='$ttl'"
 # Deleting old record
 # Deleting old record
@@ -135,7 +151,7 @@ $BIN/v-restart-dns "$restart"
 check_result $? "DNS restart failed" >/dev/null
 check_result $? "DNS restart failed" >/dev/null
 
 
 # Logging
 # Logging
-$BIN/v-log-action "$user" "Info" "DNS" "DNS record value changed (Type: $type, Record: $record, Value: $dvalue, Domain: $domain)."
+$BIN/v-log-action "$user" "Info" "DNS" "DNS record value changed (Type: $rtype, Record: $record, Value: $dvalue, Domain: $domain)."
 log_event "$OK" "$ARGUMENTS"
 log_event "$OK" "$ARGUMENTS"
 
 
 exit
 exit

+ 16 - 3
func/domain.sh

@@ -512,9 +512,22 @@ update_domain_zone() {
         if [ "$TYPE" = 'CNAME' ] || [ "$TYPE" = 'MX' ]; then
         if [ "$TYPE" = 'CNAME' ] || [ "$TYPE" = 'MX' ]; then
             VALUE=$(idn --quiet -a -t "$VALUE")
             VALUE=$(idn --quiet -a -t "$VALUE")
         fi
         fi
-
-        if [ "$TYPE" = 'TXT' ] && [[ ${VALUE:0:1} != '"' ]]; then
-            VALUE=$(echo $VALUE | fold -w 255 | xargs -I '$' echo -n '"$"')
+        
+        if [ "$TYPE" = 'TXT' ]; then
+            txtlength=${#VALUE}
+            if [ $txtlength -gt 255 ]; then
+                already_chunked=0
+                if [[ $VALUE == *"\" \""* ]] || [[ $VALUE == *"\"\""* ]]; then
+                    already_chunked=1
+                fi
+                if [ $already_chunked -eq 0 ]; then
+                    if [[ ${VALUE:0:1} = '"' ]]; then
+                        txtlength=$(( $txtlength - 2 ))
+                        VALUE=${VALUE:1:txtlength}
+                    fi
+                    VALUE=$(echo $VALUE | fold -w 255 | xargs -I '$' echo -n '"$"')
+                fi
+            fi
         fi
         fi
 
 
         if [ "$SUSPENDED" != 'yes' ]; then
         if [ "$SUSPENDED" != 'yes' ]; then

+ 1 - 1
func/main.sh

@@ -876,7 +876,7 @@ is_common_format_valid() {
 }
 }
 
 
 is_no_new_line_format() {
 is_no_new_line_format() {
-    test=$(echo $1 | sed -e 's/\.*$//g' -e 's/^\.*//g');
+    test=$(echo $1 | head -n1 );
     if [[ "$test" != "$1" ]]; then
     if [[ "$test" != "$1" ]]; then
       check_result "$E_INVALID" "invalid value :: $1"
       check_result "$E_INVALID" "invalid value :: $1"
     fi
     fi

+ 38 - 7
test/checks.bats

@@ -149,6 +149,12 @@ r' "key"
      run is_domain_format_valid '..' "key"
      run is_domain_format_valid '..' "key"
     assert_failure $E_INVALID
     assert_failure $E_INVALID
 }
 }
+
+@test "is_domain_format_valid hestiacp.com." {
+     run is_domain_format_valid 'mx.hestiacp.com.' "key"
+    assert_success
+}
+
 @test "is_domain_format_valid LF." {
 @test "is_domain_format_valid LF." {
      run is_domain_format_valid 'c
      run is_domain_format_valid 'c
 1eshutdown
 1eshutdown
@@ -156,6 +162,22 @@ r' "key"
     assert_failure $E_INVALID
     assert_failure $E_INVALID
 }
 }
 
 
+@test "is_dns_record_format_valid" {
+    rtype='MX'
+    priority=1; 
+    run is_dns_record_format_valid 'mx.hestiacp.com.'  
+    assert_success
+}
+
+@test "is_dns_record_format_valid test" {
+    rtype='MX'
+priority=1; 
+     run is_dns_record_format_valid 'c
+1eshutdown
+r' 
+    assert_failure $E_INVALID
+}
+
 @test "is_alias_format_valid success" {
 @test "is_alias_format_valid success" {
      run is_domain_format_valid 'hestiacp.com' "key"
      run is_domain_format_valid 'hestiacp.com' "key"
     assert_success
     assert_success
@@ -223,13 +245,6 @@ r' "key"
     assert_failure $E_INVALID
     assert_failure $E_INVALID
 }
 }
 
 
-@test "is_dns_record_format_valid test" {
-     run is_dns_record_format_valid 'c
-1eshutdown
-r' "key"
-    assert_failure $E_INVALID
-}
-
 @test "is_email_format_valid test" {
 @test "is_email_format_valid test" {
      run is_email_format_valid 'c
      run is_email_format_valid 'c
 1eshutdown
 1eshutdown
@@ -302,6 +317,22 @@ r' "key"
     assert_failure $E_INVALID
     assert_failure $E_INVALID
 }
 }
 
 
+@test "format_no_quotes .." {
+     run format_no_quotes '..' "key"
+    assert_success
+}
+
+@test "format_no_quotes text." {
+     run format_no_quotes 'text.' "key"
+    assert_success
+}
+
+@test "is_common_format_valid text" {
+     run is_common_format_valid 'text' "key"
+    assert_success
+}
+
+
 @test "format_no_quotes test" {
 @test "format_no_quotes test" {
      run format_no_quotes 'c
      run format_no_quotes 'c
 1eshutdown
 1eshutdown

+ 137 - 0
test/test.bats

@@ -1164,6 +1164,143 @@ function check_ip_not_banned(){
     refute_output
     refute_output
 }
 }
 
 
+@test "DNS: Add domain record MX" {
+    run v-add-dns-record $user $domain '@' MX mx.hestiacp.com  '' 50
+    assert_success
+    refute_output
+    
+    assert_file_contains "$HOMEDIR/$user/conf/dns/${domain}.db" "mx.hestiacp.com."
+    
+    run v-change-dns-record $user $domain 50 '@' MX mx.hestia.com
+    assert_success
+    refute_output
+    
+    assert_file_contains "$HOMEDIR/$user/conf/dns/${domain}.db" "mx.hestia.com."
+    
+    run v-delete-dns-record $user $domain 50
+    assert_success
+    refute_output
+}
+
+@test "DNS: Add domain record NS" {
+    run v-delete-dns-record $user $domain 50
+    run v-add-dns-record $user $domain '@' NS mx.hestiacp.com  '' 50
+    assert_success
+    refute_output
+    
+    assert_file_contains "$HOMEDIR/$user/conf/dns/${domain}.db" "mx.hestiacp.com."
+    
+    run v-change-dns-record $user $domain 50 '@' NS mx.hestia.com
+    assert_success
+    refute_output
+    
+    assert_file_contains "$HOMEDIR/$user/conf/dns/${domain}.db" "mx.hestia.com."
+    
+    run v-delete-dns-record $user $domain 50
+    assert_success
+    refute_output
+}
+
+@test "DNS: Add domain record SRV" {
+    run v-delete-dns-record $user $domain 50
+    run v-add-dns-record $user $domain '_test_domain' SRV mx.hestiacp.com  '' 50
+    assert_success
+    refute_output
+    
+    assert_file_contains "$HOMEDIR/$user/conf/dns/${domain}.db" "mx.hestiacp.com."
+    
+    run v-change-dns-record $user $domain 50 '_test.domain' SRV mx.hestia.com
+    assert_success
+    refute_output
+    
+    assert_file_contains "$HOMEDIR/$user/conf/dns/${domain}.db" "mx.hestia.com."
+    
+    run v-delete-dns-record $user $domain 50
+    assert_success
+    refute_output
+}
+
+@test "DNS: Add domain record CNAME" {    
+    run v-delete-dns-record $user $domain 50
+    run v-add-dns-record $user $domain 'mail' CNAME mx.hestiacp.com  '' 50
+    assert_success
+    refute_output
+    
+    assert_file_contains "$HOMEDIR/$user/conf/dns/${domain}.db" "mx.hestiacp.com."
+    
+    run v-change-dns-record $user $domain 50 'mail' CNAME mx.hestia.com
+    assert_success
+    refute_output
+    
+    assert_file_contains "$HOMEDIR/$user/conf/dns/${domain}.db" "mx.hestia.com."
+    
+    run v-delete-dns-record $user $domain 50
+    assert_success
+    refute_output
+}
+
+@test "DNS: Check txt dns records type1" {
+    [ -z "$DNS_SYSTEM" ] && skip
+
+    run v-delete-dns-record $user $domain 50
+
+    record1_in='v=DMARC1; p=quarantine; pct=100'
+    record2_in='v=DMARC1; p=quarantine; pct=90'
+        
+    record1_out='"v=DMARC1; p=quarantine; pct=100"'
+    record2_in='"v=DMARC1; p=quarantine; pct=90"'
+
+    # Test Create
+    run v-add-dns-record $user $domain 'test-long-txt' 'TXT' "$record1_in" '' 50
+    assert_success
+    refute_output
+
+    assert_file_contains "$HOMEDIR/$user/conf/dns/${domain}.db" "$record1_out"
+
+    # Test Edit
+    run v-change-dns-record $user $domain 50 'test-long-txt' 'TXT' "$record2_in"
+    assert_success
+    refute_output
+
+    assert_file_contains "$HOMEDIR/$user/conf/dns/${domain}.db" "$record2_out"
+
+    # Test Cleanup
+    run v-delete-dns-record $user $domain 50
+    assert_success
+    refute_output
+}
+
+@test "DNS: Check txt dns records type2" {
+    [ -z "$DNS_SYSTEM" ] && skip
+
+    run v-delete-dns-record $user $domain 50
+
+    record3_in='k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4+VEVsoTbl6tYLJlhozqAGju3IgpSVdBAS5LMyzpHP8/L0/PlyVRJnm2xECjVk3DRqCmelyIvmraw1VtFz2aH6DRlDhHsZghj1DmGhwN+7NkwIb4hEvmytMVAz1WyiLH6Rm6Iemm/ZCt1RhrAMUYLxHA9mJgky76YCcf8/cX35xC+1vd4a5U6YofAZeVP9DBvVgQ8ung4gVrOrQrXkU8QfVNAoXz5pfJo74GB7woIBFhZXsU6SKho7KnzT5inVCIOtWp7L5hyEnbySWQPHT2vAMCCAe2AY/Vv0N3HW14o8P3b4A6OU920wFB2kA7pkQNzO5OwH+HSttwG0PaIiQxYQIDAQAB'
+    record3_out='"k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4+VEVsoTbl6tYLJlhozqAGju3IgpSVdBAS5LMyzpHP8/L0/PlyVRJnm2xECjVk3DRqCmelyIvmraw1VtFz2aH6DRlDhHsZghj1DmGhwN+7NkwIb4hEvmytMVAz1WyiLH6Rm6Iemm/ZCt1RhrAMUYLxHA9mJgky76YCcf8/cX35xC+1vd4a5U6YofAZeVP9DBvVgQ8ung4g""VrOrQrXkU8QfVNAoXz5pfJo74GB7woIBFhZXsU6SKho7KnzT5inVCIOtWp7L5hyEnbySWQPHT2vAMCCAe2AY/Vv0N3HW14o8P3b4A6OU920wFB2kA7pkQNzO5OwH+HSttwG0PaIiQxYQIDAQAB"'
+    
+    record4_in='k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4+VEVsoTbl6tYLJlhozqAGju3IgpSVdBAS5LMyzpHP8/L0/PlyVRJnm2xECjVk3DRqCmelyIvmraw1VtFz2aH6DRlDhHsZghj1DmGhwN+7NkwIb4hEvmytMVAz1WyiLH6Rm6Iemm/ZCt1RhrAMUYLxHA9mJgky76YCcf8/cX35xC+1vd4a5U6YofAZeVP9DBvVgQ8ung4gVrOrQrXkU8QfVNAoXz5pfJo74GB7woIBFhZXsU6SKho7KnzT5inVCIOtWp7L5hyEnbySWQPHT2vAMCCAe2AY/Vv0N3HW14o8P3b4A6OU920wFB2kA7pkQNzO5OwH+HSttwG0PaIiQxYQIDAQA4'
+    record4_out='"k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4+VEVsoTbl6tYLJlhozqAGju3IgpSVdBAS5LMyzpHP8/L0/PlyVRJnm2xECjVk3DRqCmelyIvmraw1VtFz2aH6DRlDhHsZghj1DmGhwN+7NkwIb4hEvmytMVAz1WyiLH6Rm6Iemm/ZCt1RhrAMUYLxHA9mJgky76YCcf8/cX35xC+1vd4a5U6YofAZeVP9DBvVgQ8ung4g""VrOrQrXkU8QfVNAoXz5pfJo74GB7woIBFhZXsU6SKho7KnzT5inVCIOtWp7L5hyEnbySWQPHT2vAMCCAe2AY/Vv0N3HW14o8P3b4A6OU920wFB2kA7pkQNzO5OwH+HSttwG0PaIiQxYQIDAQA4"'
+
+    # Test Create
+    run v-add-dns-record $user $domain 'test-long-txt' 'TXT' "$record3_in" '' 50
+    assert_success
+    refute_output
+
+    assert_file_contains "$HOMEDIR/$user/conf/dns/${domain}.db" "$record3_out"
+
+    # Test Edit
+    run v-change-dns-record $user $domain 50 'test-long-txt' 'TXT' "$record4_in"
+    assert_success
+    refute_output
+
+    assert_file_contains "$HOMEDIR/$user/conf/dns/${domain}.db" "$record4_out"
+
+    # Test Cleanup
+    run v-delete-dns-record $user $domain 50
+    assert_success
+    refute_output
+}
+
 @test "DNS: Change domain ip" {
 @test "DNS: Change domain ip" {
     run v-change-dns-domain-ip $user $domain 127.0.0.1
     run v-change-dns-domain-ip $user $domain 127.0.0.1
     assert_success
     assert_success