v-add-dns-domain 4.2 KB

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