v_add_dns_domain 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/bin/bash
  2. # info: add dns domain
  3. # options: user domain ip [template] [ns1] [ns2] [ns3] [ns4] [restart]
  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 | sed -e 's/\.*$//g' -e 's/^\.*//g')
  17. domain=$(echo $domain | tr '[:upper:]' '[:lower:]')
  18. domain_idn=$(idn -t --quiet -a "$domain")
  19. ip=$3
  20. template=$4
  21. ns1=$5
  22. ns2=$6
  23. ns3=$7
  24. ns4=$8
  25. restart=$9
  26. # Includes
  27. source $VESTA/conf/vesta.conf
  28. source $VESTA/func/main.sh
  29. source $VESTA/func/domain.sh
  30. #----------------------------------------------------------#
  31. # Verifications #
  32. #----------------------------------------------------------#
  33. check_args '3' "$#" 'user domain ip [template] [ns1] [ns2] [ns3] [ns4]'
  34. validate_format 'user' 'domain' 'ip'
  35. is_system_enabled "$DNS_SYSTEM"
  36. is_object_valid 'user' 'USER' "$user"
  37. is_object_unsuspended 'user' 'USER' "$user"
  38. is_domain_new 'dns'
  39. is_package_full 'DNS_DOMAINS'
  40. if [ ! -z "$template" ]; then
  41. validate_format 'template'
  42. is_dns_template_valid
  43. else
  44. template=$(get_user_value '$TEMPLATE')
  45. is_dns_template_valid
  46. fi
  47. if [ ! -z "$ns1" ]; then
  48. ns1=$(echo $5 | sed -e 's/\.*$//g' -e 's/^\.*//g')
  49. validate_format 'ns1'
  50. fi
  51. if [ ! -z "$ns2" ]; then
  52. ns2=$(echo $6 | sed -e 's/\.*$//g' -e 's/^\.*//g')
  53. validate_format 'ns2'
  54. fi
  55. if [ ! -z "$ns3" ]; then
  56. ns3=$(echo $7 | sed -e 's/\.*$//g' -e 's/^\.*//g')
  57. validate_format 'ns3'
  58. fi
  59. if [ ! -z "$ns4" ]; then
  60. ns4=$(echo $8 | sed -e 's/\.*$//g' -e 's/^\.*//g')
  61. validate_format 'ns4'
  62. fi
  63. #----------------------------------------------------------#
  64. # Action #
  65. #----------------------------------------------------------#
  66. # Defining variables
  67. if [ -z $ns2 ]; then
  68. i=1
  69. ns=$(get_user_value '$NS')
  70. for nameserver in ${ns//,/ };do
  71. eval ns$i=$nameserver
  72. (( ++i))
  73. done
  74. fi
  75. soa="$ns1"
  76. exp=$(date +%F -d "+ 1 year")
  77. ttl=14400
  78. # Adding zone to dns dir
  79. cat $DNSTPL/$template.tpl |\
  80. sed -e "s/%ip%/$ip/g" \
  81. -e "s/%domain_idn%/$domain_idn/g" \
  82. -e "s/%domain%/$domain/g" \
  83. -e "s/%ns1%/$ns1/g" \
  84. -e "s/%ns2%/$ns2/g" \
  85. -e "s/%ns3%/$ns3/g" \
  86. -e "s/%ns4%/$ns4/g" \
  87. -e "s/%time%/$TIME/g" \
  88. -e "s/%date%/$DATE/g" > $USER_DATA/dns/$domain.conf
  89. chmod 660 $USER_DATA/dns/$domain.conf
  90. records="$(wc -l $USER_DATA/dns/$domain.conf |cut -f 1 -d ' ')"
  91. # Adding dns.conf record
  92. dns_rec="DOMAIN='$domain' IP='$ip' TPL='$template' TTL='$ttl' EXP='$exp'"
  93. dns_rec="$dns_rec SOA='$soa' RECORDS='$records' SUSPENDED='no' TIME='$TIME'"
  94. dns_rec="$dns_rec DATE='$DATE'"
  95. echo "$dns_rec" >> $USER_DATA/dns.conf
  96. chmod 660 $USER_DATA/dns.conf
  97. # Adding zone in named.conf
  98. named="zone \"$domain_idn\" {type master; file"
  99. named="$named \"$HOMEDIR/$user/conf/dns/$domain.db\";};"
  100. echo "$named" >> /etc/named.conf
  101. # Updating domain dns zone
  102. update_domain_zone
  103. chmod 640 $conf
  104. chown root:named $conf
  105. #----------------------------------------------------------#
  106. # Vesta #
  107. #----------------------------------------------------------#
  108. # Increasing domain value
  109. increase_user_value "$user" '$U_DNS_DOMAINS'
  110. increase_user_value "$user" '$U_DNS_RECORDS' "$records"
  111. # Restart named
  112. if [ "$restart" != 'no' ]; then
  113. $BIN/v_restart_dns "$EVENT"
  114. fi
  115. # Logging
  116. log_history "$EVENT"
  117. log_event "$OK" "$EVENT"
  118. exit