v-add-dns-record 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. domain_idn="$domain"
  26. if [[ "$domain" = *[![:ascii:]]* ]]; then
  27. domain_idn=$(idn -t --quiet -a $domain)
  28. fi
  29. # Includes
  30. source $VESTA/func/main.sh
  31. source $VESTA/func/domain.sh
  32. source $VESTA/conf/vesta.conf
  33. # Null priority for none MX/SRV records
  34. if [ "$rtype" != 'MX' ] && [ "$rtype" != 'SRV' ]; then
  35. priority=''
  36. fi
  37. # Add trailing dot at the end of NS/CNAME/MX/PTR/SRV record
  38. if [[ $rtype =~ NS|CNAME|MX|PTR|SRV ]]; then
  39. trailing_dot=$(echo $dvalue | grep "\.$")
  40. if [ -z $trailing_dot ]; then
  41. dvalue="$dvalue."
  42. fi
  43. fi
  44. dvalue=${dvalue//\"/}
  45. if [[ "$dvalue" =~ [\;[:space:]] ]]; then
  46. dvalue='"'"$dvalue"'"'
  47. fi
  48. #----------------------------------------------------------#
  49. # Verifications #
  50. #----------------------------------------------------------#
  51. check_args '5' "$#" 'USER DOMAIN RECORD TYPE VALUE [PRIORITY] [ID] [RESTART]'
  52. is_format_valid 'user' 'domain' 'record' 'rtype' 'dvalue'
  53. is_system_enabled "$DNS_SYSTEM" 'DNS_SYSTEM'
  54. is_object_valid 'user' 'USER' "$user"
  55. is_object_unsuspended 'user' 'USER' "$user"
  56. is_object_valid 'dns' 'DOMAIN' "$domain"
  57. is_object_unsuspended 'dns' 'DOMAIN' "$domain"
  58. is_package_full 'DNS_RECORDS'
  59. get_next_dnsrecord
  60. is_format_valid 'id'
  61. is_object_new "dns/$domain" 'ID' "$id"
  62. is_dns_fqnd "$rtype" "$dvalue"
  63. is_dns_nameserver_valid "$domain" "$rtype" "$dvalue"
  64. #----------------------------------------------------------#
  65. # Action #
  66. #----------------------------------------------------------#
  67. # Generating timestamp
  68. time_n_date=$(date +'%T %F')
  69. time=$(echo "$time_n_date" |cut -f 1 -d \ )
  70. date=$(echo "$time_n_date" |cut -f 2 -d \ )
  71. # Adding record
  72. zone="$USER_DATA/dns/$domain.conf"
  73. dns_rec="ID='$id' RECORD='$record' TYPE='$rtype' PRIORITY='$priority'"
  74. dns_rec="$dns_rec VALUE='$dvalue' SUSPENDED='no' TIME='$time' DATE='$date'"
  75. echo "$dns_rec" >> $zone
  76. chmod 660 $zone
  77. # Sorting records
  78. sort_dns_records
  79. # Updating zone
  80. if [[ "$DNS_SYSTEM" =~ named|bind ]]; then
  81. update_domain_serial
  82. update_domain_zone
  83. fi
  84. # Updating dns-cluster queue
  85. if [ ! -z "$DNS_CLUSTER" ]; then
  86. # Check for first sync
  87. dlock=$(grep "domain $user $domain" $VESTA/data/queue/dns-cluster.pipe)
  88. if [ -z "$dlock" ]; then
  89. cmd="$BIN/v-add-remote-dns-record $user $domain $id"
  90. echo "$cmd" >> $VESTA/data/queue/dns-cluster.pipe
  91. fi
  92. fi
  93. #----------------------------------------------------------#
  94. # Vesta #
  95. #----------------------------------------------------------#
  96. # Update counters
  97. records="$(wc -l $USER_DATA/dns/$domain.conf | cut -f1 -d ' ')"
  98. update_object_value 'dns' 'DOMAIN' "$domain" '$RECORDS' "$records"
  99. increase_user_value "$user" '$U_DNS_RECORDS'
  100. # Restart named
  101. if [ "$restart" != 'no' ]; then
  102. $BIN/v-restart-dns
  103. check_result $? $E_RESTART 'dns failed to restart'
  104. fi
  105. # Logging
  106. log_history "added $rtype dns record $record for $domain"
  107. log_event "$OK" "$ARGUMENTS"
  108. exit