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 definition
  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. if [[ $rtype =~ NS|CNAME|MX|PTR|SRV ]]; then
  37. trailing_dot=$(echo $dvalue | grep "\.$")
  38. if [ -z $trailing_dot ]; then
  39. dvalue="$dvalue."
  40. fi
  41. fi
  42. #----------------------------------------------------------#
  43. # Verifications #
  44. #----------------------------------------------------------#
  45. check_args '5' "$#" 'USER DOMAIN RECORD TYPE VALUE [PRIORITY] [ID] [RESTART]'
  46. validate_format 'user' 'domain' 'record' 'rtype' 'dvalue'
  47. is_system_enabled "$DNS_SYSTEM" 'DNS_SYSTEM'
  48. is_object_valid 'user' 'USER' "$user"
  49. is_object_unsuspended 'user' 'USER' "$user"
  50. is_object_valid 'dns' 'DOMAIN' "$domain"
  51. is_object_unsuspended 'dns' 'DOMAIN' "$domain"
  52. is_package_full 'DNS_RECORDS'
  53. get_next_dnsrecord
  54. validate_format 'id'
  55. is_object_new "dns/$domain" 'ID' "$id"
  56. is_dns_fqnd "$rtype" "$dvalue"
  57. is_dns_nameserver_valid "$domain" "$rtype" "$dvalue"
  58. #----------------------------------------------------------#
  59. # Action #
  60. #----------------------------------------------------------#
  61. # Adding record
  62. zone="$USER_DATA/dns/$domain.conf"
  63. dns_rec="ID='$id' RECORD='$record' TYPE='$rtype' PRIORITY='$priority'"
  64. dns_rec="$dns_rec VALUE='$dvalue' SUSPENDED='no' TIME='$TIME' DATE='$DATE'"
  65. echo "$dns_rec" >> $zone
  66. chmod 660 $zone
  67. # Sorting records
  68. sort_dns_records
  69. # Updating zone
  70. if [[ "$DNS_SYSTEM" =~ named|bind ]]; then
  71. update_domain_serial
  72. update_domain_zone
  73. fi
  74. # Updating dns-cluster queue
  75. if [ ! -z "$DNS_CLUSTER" ]; then
  76. # Check for first sync
  77. dlock=$(grep "domain $user $domain" $VESTA/data/queue/dns-cluster.pipe)
  78. if [ -z "$dlock" ]; then
  79. cmd="$BIN/v-add-remote-dns-record $user $domain $id"
  80. echo "$cmd" >> $VESTA/data/queue/dns-cluster.pipe
  81. fi
  82. fi
  83. #----------------------------------------------------------#
  84. # Vesta #
  85. #----------------------------------------------------------#
  86. # Update counters
  87. records="$(wc -l $USER_DATA/dns/$domain.conf | cut -f1 -d ' ')"
  88. update_object_value 'dns' 'DOMAIN' "$domain" '$RECORDS' "$records"
  89. increase_user_value "$user" '$U_DNS_RECORDS'
  90. # Restart named
  91. if [ "$restart" != 'no' ]; then
  92. $BIN/v-restart-dns
  93. check_result $? $E_RESTART 'dns failed to restart'
  94. fi
  95. # Logging
  96. log_history "added $rtype dns record $record for $domain"
  97. log_event "$OK" "$EVENT"
  98. exit