v-add-dns-record 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 defenition
  13. user=$1
  14. domain=$(idn -t --quiet -u "$2" )
  15. domain=$(echo $domain | tr '[:upper:]' '[:lower:]')
  16. domain_idn=$(idn -t --quiet -a "$domain")
  17. record=$(idn -t --quiet -u "$3" )
  18. record=$(echo "$record" | tr '[:upper:]' '[:lower:]')
  19. rtype=$(echo "$4"| tr '[:lower:]' '[:upper:]')
  20. dvalue=$(idn -t --quiet -u "$5" )
  21. priority=$6
  22. id=$7
  23. restart=$8
  24. if [ -z "$priority" ]; then
  25. priority=10
  26. fi
  27. # Includes
  28. source $VESTA/func/main.sh
  29. source $VESTA/func/domain.sh
  30. source $VESTA/conf/vesta.conf
  31. # Null priority for none MX/SRV records
  32. if [ "$rtype" != 'MX' ] && [ "$rtype" != 'SRV' ]; then
  33. priority=''
  34. fi
  35. # Add trailing dot at the end of NS/CNAME/MX/PTR/SRV record
  36. fqdn_type=$(echo $rtype |grep "NS\|CNAME\|MX\|PTR\|SRV")
  37. if [ ! -z "$fqdn_type" ]; then
  38. trailing_dot=$(echo $dvalue | grep "\.$")
  39. if [ -z $trailing_dot ]; then
  40. dvalue="$dvalue."
  41. fi
  42. fi
  43. #----------------------------------------------------------#
  44. # Verifications #
  45. #----------------------------------------------------------#
  46. check_args '5' "$#" 'USER DOMAIN RECORD TYPE VALUE [PRIORITY] [ID] [RESTART]'
  47. validate_format 'user' 'domain' 'record' 'rtype' 'dvalue'
  48. is_system_enabled "$DNS_SYSTEM" 'DNS_SYSTEM'
  49. is_object_valid 'user' 'USER' "$user"
  50. is_object_unsuspended 'user' 'USER' "$user"
  51. is_object_valid 'dns' 'DOMAIN' "$domain"
  52. is_object_unsuspended 'dns' 'DOMAIN' "$domain"
  53. is_package_full 'DNS_RECORDS'
  54. get_next_dnsrecord
  55. validate_format 'id'
  56. is_object_new "dns/$domain" 'ID' "$id"
  57. is_dns_fqnd "$rtype" "$dvalue"
  58. is_dns_nameserver_valid "$domain" "$rtype" "$dvalue"
  59. #----------------------------------------------------------#
  60. # Action #
  61. #----------------------------------------------------------#
  62. # Adding record
  63. zone="$USER_DATA/dns/$domain.conf"
  64. dns_rec="ID='$id' RECORD='$record' TYPE='$rtype' PRIORITY='$priority'"
  65. dns_rec="$dns_rec VALUE='$dvalue' SUSPENDED='no' TIME='$TIME' DATE='$DATE'"
  66. echo "$dns_rec" >> $zone
  67. chmod 660 $zone
  68. # Sorting records
  69. sort_dns_records
  70. # Updating zone
  71. update_domain_zone
  72. # dns-cluster
  73. if [ ! -z "$DNS_CLUSTER" ]; then
  74. # Check for first sync
  75. dlock=$(grep "domain $user $domain" $VESTA/data/queue/dns-cluster.pipe)
  76. if [ -z "$dlock" ]; then
  77. cmd="$BIN/v-add-remote-dns-record $user $domain $id"
  78. echo "$cmd" >> $VESTA/data/queue/dns-cluster.pipe
  79. fi
  80. fi
  81. #----------------------------------------------------------#
  82. # Vesta #
  83. #----------------------------------------------------------#
  84. # Upddate counters
  85. records="$(wc -l $USER_DATA/dns/$domain.conf | cut -f1 -d ' ')"
  86. update_object_value 'dns' 'DOMAIN' "$domain" '$RECORDS' "$records"
  87. increase_user_value "$user" '$U_DNS_RECORDS'
  88. # Restart named
  89. if [ "$restart" != 'no' ]; then
  90. $BIN/v-restart-dns
  91. if [ $? -ne 0 ]; then
  92. exit E_RESTART
  93. fi
  94. fi
  95. # Logging
  96. log_history "added $rtype dns record $record for $domain"
  97. log_event "$OK" "$EVENT"
  98. exit