v-change-dns-record 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/bin/bash
  2. # info: change dns domain record
  3. # options: USER DOMAIN ID VALUE [PRIORITY] [RESTART]
  4. #
  5. # The function for changing DNS record.
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Argument definition
  10. user=$1
  11. domain=$2
  12. id=$3
  13. dvalue=$(idn -t --quiet -u "$4" )
  14. priority=$5
  15. restart=$6
  16. # Includes
  17. source $VESTA/func/main.sh
  18. source $VESTA/func/domain.sh
  19. source $VESTA/conf/vesta.conf
  20. # Additional argument formatting
  21. format_domain
  22. format_domain_idn
  23. #----------------------------------------------------------#
  24. # Verifications #
  25. #----------------------------------------------------------#
  26. check_args '4' "$#" 'USER DOMAIN ID VALUE [PRIORITY] [RESTART]'
  27. is_format_valid 'user' 'domain' 'id' 'dvalue'
  28. is_system_enabled "$DNS_SYSTEM" 'DNS_SYSTEM'
  29. is_object_valid 'user' 'USER' "$user"
  30. is_object_unsuspended 'user' 'USER' "$user"
  31. is_object_valid 'dns' 'DOMAIN' "$domain"
  32. is_object_unsuspended 'dns' 'DOMAIN' "$domain"
  33. is_object_valid "dns/$domain" 'ID' "$id"
  34. #----------------------------------------------------------#
  35. # Action #
  36. #----------------------------------------------------------#
  37. # Parsing domain config
  38. line=$(grep "ID='$id'" $USER_DATA/dns/$domain.conf)
  39. eval $line
  40. # Null priority for none MX/SRV records
  41. if [ "$TYPE" != 'MX' ] && [ "$TYPE" != 'SRV' ]; then
  42. priority=''
  43. fi
  44. # Add trailing dot at the end of NS/CNAME/MX/PTR/SRV record
  45. if [[ $TYPE =~ NS|CNAME|MX|PTR|SRV ]]; then
  46. trailing_dot=$(echo $dvalue | grep "\.$")
  47. if [ -z $trailing_dot ]; then
  48. dvalue="$dvalue."
  49. fi
  50. fi
  51. # Additional verifications
  52. is_dns_fqnd "$TYPE" "$dvalue"
  53. is_dns_nameserver_valid "$domain" "$TYPE" "$dvalue"
  54. # Deleting old record
  55. sed -i "/^ID='$id'/d" $USER_DATA/dns/$domain.conf
  56. # Generating timestamp
  57. time_n_date=$(date +'%T %F')
  58. time=$(echo "$time_n_date" |cut -f 1 -d \ )
  59. date=$(echo "$time_n_date" |cut -f 2 -d \ )
  60. # Adding record
  61. dns_rec="ID='$id' RECORD='$RECORD' TYPE='$TYPE' PRIORITY='$priority'"
  62. dns_rec="$dns_rec VALUE='$dvalue' SUSPENDED='no' TIME='$time' DATE='$date'"
  63. echo "$dns_rec" >> $USER_DATA/dns/$domain.conf
  64. # Sorting records
  65. sort_dns_records
  66. # Updating zone
  67. if [[ "$DNS_SYSTEM" =~ named|bind ]]; then
  68. update_domain_serial
  69. update_domain_zone
  70. fi
  71. # Updating dns-cluster queue
  72. if [ ! -z "$DNS_CLUSTER" ]; then
  73. # Check for first sync
  74. dlock=$(grep "domain $user $domain" $VESTA/data/queue/dns-cluster.pipe)
  75. if [ -z "$dlock" ]; then
  76. cmd="$BIN/v-add-remote-dns-domain $user $domain records"
  77. echo "$cmd" >> $VESTA/data/queue/dns-cluster.pipe
  78. fi
  79. fi
  80. #----------------------------------------------------------#
  81. # Vesta #
  82. #----------------------------------------------------------#
  83. # Restarting named
  84. $BIN/v-restart-dns $restart
  85. check_result $? "DNS restart failed" >/dev/null
  86. # Logging
  87. log_history "changed dns record on $domain to $dvalue"
  88. log_event "$OK" "$ARGUMENTS"
  89. exit