v_add_dns_domain 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. # Importing variables
  25. source $VESTA/conf/vars.conf
  26. source $V_CONF/vesta.conf
  27. source $V_FUNC/shared.func
  28. source $V_FUNC/domain.func
  29. #----------------------------------------------------------#
  30. # Verifications #
  31. #----------------------------------------------------------#
  32. # Checking arg number
  33. check_args '3' "$#" 'user domain ip [template] [exp] [soa] [ttl]'
  34. # Checking argument format
  35. format_validation 'user' 'domain' 'ip' 'template' 'exp' 'ttl'
  36. # Checking dns system is enabled
  37. is_system_enabled 'dns'
  38. # Checking user
  39. is_user_valid
  40. # Checking user is active
  41. is_user_suspended
  42. # Checking domain
  43. is_domain_new 'quiet'
  44. if [ $? -ne 0 ]; then
  45. # Checking domain owner
  46. is_domain_owner
  47. # Checking domain service
  48. is_dns_domain_free
  49. fi
  50. # Checking package
  51. is_package_full 'dns'
  52. # Checking template
  53. is_template_valid 'dns'
  54. #----------------------------------------------------------#
  55. # Action #
  56. #----------------------------------------------------------#
  57. # Defining variables
  58. i=1
  59. ns=$(get_user_value '$NS')
  60. for nameserver in ${ns//,/ };do
  61. eval ns$i=$nameserver
  62. i=$((i + 1))
  63. done
  64. if [ -z "$soa" ]; then
  65. soa="$ns1"
  66. fi
  67. # Adding zone to dns dir
  68. cat $V_DNSTPL/$template.tpl |\
  69. sed -e "s/%ip%/$ip/g" \
  70. -e "s/%domain_idn%/$domain_idn/g" \
  71. -e "s/%domain%/$domain/g" \
  72. -e "s/%ns1%/$ns1/g" \
  73. -e "s/%ns2%/$ns2/g" \
  74. -e "s/%ns3%/$ns3/g" \
  75. -e "s/%ns4%/$ns4/g" \
  76. -e "s/%ns5%/$ns5/g" \
  77. -e "s/%ns6%/$ns6/g" \
  78. -e "s/%ns7%/$ns7/g" \
  79. -e "s/%ns8%/$ns8/g" \
  80. -e "s/%date%/$V_DATE/g" > $V_USERS/$user/dns/$domain
  81. # Adding dns.conf record
  82. dns_rec="DOMAIN='$domain' IP='$ip' TPL='$template' TTL='$ttl' EXP='$exp'"
  83. dns_rec="$dns_rec SOA='$soa' SUSPENDED='no' DATE='$V_DATE'"
  84. echo "$dns_rec" >> $V_USERS/$user/dns.conf
  85. chmod 660 $V_USERS/$user/dns.conf
  86. # Adding zone in named.conf
  87. named="zone \"$domain_idn\" {type master; file"
  88. named="$named \"$V_HOME/$user/conf/dns/$domain.db\";};"
  89. echo "$named" >> /etc/named.conf
  90. # Updating domain dns zone
  91. conf="$V_HOME/$user/conf/dns/$domain.db"
  92. update_domain_zone
  93. chmod 640 $conf
  94. chown root:named $conf
  95. #----------------------------------------------------------#
  96. # Vesta #
  97. #----------------------------------------------------------#
  98. # Increasing domain value
  99. increase_user_value "$user" '$U_DNS_DOMAINS'
  100. # Adding task to the vesta pipe
  101. restart_schedule 'dns'
  102. # Logging
  103. log_history "$V_EVENT" "v_delete_dns_domain $user $domain"
  104. log_event 'system' "$V_EVENT"
  105. exit