v-add-dns-record 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/bin/bash
  2. # info: add dns record
  3. # options: USER DOMAIN RECORD TYPE VALUE [PRIORITY] [ID] [RESTART]
  4. #
  5. # The call is used for adding new DNS record. Complex records of TXT, MX and
  6. # SRV types can be used by a filling in the 'value' argument. The function also
  7. # gets an id parameter for definition of certain record identifier or for the
  8. # regulation of records.
  9. #----------------------------------------------------------#
  10. # Variable&Function #
  11. #----------------------------------------------------------#
  12. # Argument definition
  13. user=$1
  14. domain=$2
  15. record=$(idn -t --quiet -u "$3" )
  16. record=$(echo "$record" | tr '[:upper:]' '[:lower:]')
  17. rtype=$(echo "$4"| tr '[:lower:]' '[:upper:]')
  18. dvalue=$(idn -t --quiet -u "$5" )
  19. priority=$6
  20. id=$7
  21. restart=$8
  22. if [ -z "$priority" ]; then
  23. priority=10
  24. fi
  25. # Includes
  26. source $VESTA/func/main.sh
  27. source $VESTA/func/domain.sh
  28. source $VESTA/conf/vesta.conf
  29. # Null priority for none MX/SRV records
  30. if [ "$rtype" != 'MX' ] && [ "$rtype" != 'SRV' ]; then
  31. priority=''
  32. fi
  33. # Add trailing dot at the end of NS/CNAME/MX/PTR/SRV record
  34. if [[ $rtype =~ NS|CNAME|MX|PTR|SRV ]]; then
  35. trailing_dot=$(echo $dvalue | grep "\.$")
  36. if [ -z "$trailing_dot" ]; then
  37. dvalue="$dvalue."
  38. fi
  39. fi
  40. dvalue=${dvalue//\"/}
  41. if [[ "$dvalue" =~ [\;[:space:]] ]]; then
  42. dvalue='"'"$dvalue"'"'
  43. fi
  44. # Additional argument formatting
  45. format_domain
  46. format_domain_idn
  47. #----------------------------------------------------------#
  48. # Verifications #
  49. #----------------------------------------------------------#
  50. check_args '5' "$#" 'USER DOMAIN RECORD TYPE VALUE [PRIORITY] [ID] [RESTART]'
  51. is_format_valid 'user' 'domain' 'record' 'rtype' 'dvalue'
  52. is_system_enabled "$DNS_SYSTEM" 'DNS_SYSTEM'
  53. is_object_valid 'user' 'USER' "$user"
  54. is_object_unsuspended 'user' 'USER' "$user"
  55. is_object_valid 'dns' 'DOMAIN' "$domain"
  56. is_object_unsuspended 'dns' 'DOMAIN' "$domain"
  57. is_package_full 'DNS_RECORDS'
  58. get_next_dnsrecord
  59. is_format_valid 'id'
  60. is_object_new "dns/$domain" 'ID' "$id"
  61. is_dns_fqnd "$rtype" "$dvalue"
  62. is_dns_nameserver_valid "$domain" "$rtype" "$dvalue"
  63. #----------------------------------------------------------#
  64. # Action #
  65. #----------------------------------------------------------#
  66. # Generating timestamp
  67. time_n_date=$(date +'%T %F')
  68. time=$(echo "$time_n_date" |cut -f 1 -d \ )
  69. date=$(echo "$time_n_date" |cut -f 2 -d \ )
  70. # Adding record
  71. zone="$USER_DATA/dns/$domain.conf"
  72. dns_rec="ID='$id' RECORD='$record' TYPE='$rtype' PRIORITY='$priority'"
  73. dns_rec="$dns_rec VALUE='$dvalue' SUSPENDED='no' TIME='$time' DATE='$date'"
  74. echo "$dns_rec" >> $zone
  75. chmod 660 $zone
  76. # Sorting records
  77. sort_dns_records
  78. # Updating zone
  79. if [[ "$DNS_SYSTEM" =~ named|bind ]]; then
  80. update_domain_serial
  81. update_domain_zone
  82. fi
  83. # Updating dns-cluster queue
  84. if [ ! -z "$DNS_CLUSTER" ]; then
  85. # Check for first sync
  86. dlock=$(grep "domain $user $domain" $VESTA/data/queue/dns-cluster.pipe)
  87. if [ -z "$dlock" ]; then
  88. cmd="$BIN/v-add-remote-dns-record $user $domain $id"
  89. echo "$cmd" >> $VESTA/data/queue/dns-cluster.pipe
  90. fi
  91. fi
  92. #----------------------------------------------------------#
  93. # Vesta #
  94. #----------------------------------------------------------#
  95. # Update counters
  96. records="$(wc -l $USER_DATA/dns/$domain.conf | cut -f1 -d ' ')"
  97. update_object_value 'dns' 'DOMAIN' "$domain" '$RECORDS' "$records"
  98. increase_user_value "$user" '$U_DNS_RECORDS'
  99. # Restart named
  100. $BIN/v-restart-dns $restart
  101. check_result $? $E_RESTART 'dns failed to restart'
  102. # Logging
  103. log_history "added $rtype dns record $record for $domain"
  104. log_event "$OK" "$ARGUMENTS"
  105. exit