v_add_dns_domain 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/bin/bash
  2. # info: add dns domain
  3. # options: user domain ip [template] [exp] [soa] [ttl]
  4. #
  5. # The function adds DNS zone with records defined in the template. If the exp
  6. # argument isn't stated, the expiration date value will be set to next year.
  7. # The soa argument is responsible for the relevant record. By default the first
  8. # user's NS server is used. TTL is set as common for the zone and for all of
  9. # its records with a default value of 14400 seconds.
  10. #----------------------------------------------------------#
  11. # Variable&Function #
  12. #----------------------------------------------------------#
  13. # Argument defenition
  14. user=$1
  15. domain=$(idn -t --quiet -u "$2" )
  16. domain=$(echo $domain | tr '[:upper:]' '[:lower:]')
  17. domain_idn=$(idn -t --quiet -a "$domain")
  18. ip=$3
  19. template=${4-default}
  20. next_year=$(date +%F -d "+ 1 year")
  21. exp=${5-$next_year}
  22. soa=$6
  23. ttl=${7-14400}
  24. # Includes
  25. source $VESTA/conf/vesta.conf
  26. source $VESTA/func/main.sh
  27. source $VESTA/func/domain.sh
  28. #----------------------------------------------------------#
  29. # Verifications #
  30. #----------------------------------------------------------#
  31. check_args '3' "$#" 'user domain ip [template] [exp] [soa] [ttl]'
  32. validate_format 'user' 'domain' 'ip' 'template' 'exp' 'ttl'
  33. is_system_enabled "$DNS_SYSTEM"
  34. is_object_valid 'user' 'USER' "$user"
  35. is_object_unsuspended 'user' 'USER' "$user"
  36. is_domain_new 'dns'
  37. is_package_full 'DNS_DOMAINS'
  38. is_dns_template_valid
  39. #----------------------------------------------------------#
  40. # Action #
  41. #----------------------------------------------------------#
  42. # Defining variables
  43. i=1
  44. ns=$(get_user_value '$NS')
  45. for nameserver in ${ns//,/ };do
  46. eval ns$i=$nameserver
  47. (( ++i))
  48. done
  49. # Define soa
  50. if [ -z "$soa" ]; then
  51. soa="$ns1"
  52. fi
  53. # Adding zone to dns dir
  54. cat $DNSTPL/$template.tpl |\
  55. sed -e "s/%ip%/$ip/g" \
  56. -e "s/%domain_idn%/$domain_idn/g" \
  57. -e "s/%domain%/$domain/g" \
  58. -e "s/%ns1%/$ns1/g" \
  59. -e "s/%ns2%/$ns2/g" \
  60. -e "s/%ns3%/$ns3/g" \
  61. -e "s/%ns4%/$ns4/g" \
  62. -e "s/%ns5%/$ns5/g" \
  63. -e "s/%ns6%/$ns6/g" \
  64. -e "s/%ns7%/$ns7/g" \
  65. -e "s/%ns8%/$ns8/g" \
  66. -e "s/%time%/$TIME/g" \
  67. -e "s/%date%/$DATE/g" > $USER_DATA/dns/$domain.conf
  68. chmod 660 $USER_DATA/dns/$domain.conf
  69. records="$(wc -l $USER_DATA/dns/$domain.conf |cut -f 1 -d ' ')"
  70. # Adding dns.conf record
  71. dns_rec="DOMAIN='$domain' IP='$ip' TPL='$template' TTL='$ttl' EXP='$exp'"
  72. dns_rec="$dns_rec SOA='$soa' RECORDS='$records' SUSPENDED='no' TIME='$TIME'"
  73. dns_rec="$dns_rec DATE='$DATE'"
  74. echo "$dns_rec" >> $USER_DATA/dns.conf
  75. chmod 660 $USER_DATA/dns.conf
  76. # Adding zone in named.conf
  77. named="zone \"$domain_idn\" {type master; file"
  78. named="$named \"$HOMEDIR/$user/conf/dns/$domain.db\";};"
  79. echo "$named" >> /etc/named.conf
  80. # Updating domain dns zone
  81. update_domain_zone
  82. chmod 640 $conf
  83. chown root:named $conf
  84. #----------------------------------------------------------#
  85. # Vesta #
  86. #----------------------------------------------------------#
  87. # Increasing domain value
  88. increase_user_value "$user" '$U_DNS_DOMAINS'
  89. increase_user_value "$user" '$U_DNS_RECORDS' "$records"
  90. # Restart named
  91. $BIN/v_restart_web "$EVENT"
  92. # Logging
  93. log_history "$EVENT"
  94. log_event "$OK" "$EVENT"
  95. exit