v-delete-sys-ip 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/bin/bash
  2. # info: delete system ip
  3. # options: IP
  4. #
  5. # The function for deleting a system ip. It does not allow to delete first ip
  6. # on interface and do not allow to delete ip which is used by a web domain.
  7. #----------------------------------------------------------#
  8. # Variable&Function #
  9. #----------------------------------------------------------#
  10. # Argument defenition
  11. ip=$1
  12. # Includes
  13. source $VESTA/func/main.sh
  14. source $VESTA/func/ip.sh
  15. source $VESTA/func/domain.sh
  16. source $VESTA/conf/vesta.conf
  17. #----------------------------------------------------------#
  18. # Verifications #
  19. #----------------------------------------------------------#
  20. check_args '1' "$#" 'IP'
  21. validate_format 'ip'
  22. is_ip_valid "$ip"
  23. is_ip_key_empty '$U_WEB_DOMAINS'
  24. is_ip_key_empty '$U_SYS_USERS'
  25. #----------------------------------------------------------#
  26. # Action #
  27. #----------------------------------------------------------#
  28. # Get ip owner
  29. user="$(get_ip_value '$OWNER')"
  30. ip_status="$(get_ip_value '$STATUS')"
  31. # Deleting interface
  32. interface=$(/sbin/ifconfig | grep -B1 "dr:$ip " | head -n1 | cut -f1 -d \ )
  33. if [ ! -z "$interface" ] && [ -z "$(echo $interface |cut -s -f2 -d :)" ]; then
  34. echo "Error: can't delete main IP address"
  35. log_event "$E_FORBIDEN" "$EVENT"
  36. exit $E_FORBIDEN
  37. fi
  38. if [ ! -z "$interface" ]; then
  39. /sbin/ifconfig $interface down
  40. # Deleting startup conf on RHEL/CentOS/Fedora
  41. if [ -e "/etc/sysconfig/network-scripts/ifcfg-$interface" ]; then
  42. rm -f /etc/sysconfig/network-scripts/ifcfg-$interface
  43. fi
  44. # Deleting startup conf on Debian/Ubuntu
  45. if [ -e "/etc/network/interfaces" ]; then
  46. ip_str=$(grep -n $ip$ /etc/network/interfaces |cut -f1 -d:)
  47. if [ ! -z "$ip_str" ]; then
  48. first_str=$((ip_str - 3))
  49. last_str=$((ip_str + 1))
  50. sed -i "$first_str,$last_str d" /etc/network/interfaces
  51. fi
  52. fi
  53. fi
  54. # Deleting vesta ip
  55. rm -f $VESTA/data/ips/$ip
  56. # Deleting web config
  57. if [ ! -z "$WEB_SYSTEM" ]; then
  58. rm -f /etc/$WEB_SYSTEM/conf.d/$ip.conf
  59. fi
  60. # Deleting proxy config
  61. if [ ! -z "$PROXY_SYSTEM" ]; then
  62. rm -f /etc/$PROXY_SYSTEM/conf.d/$ip.conf
  63. # mod_extract_forwarded
  64. fw_conf="/etc/$WEB_SYSTEM/conf.d/mod_extract_forwarded.conf"
  65. if [ -e "$fw_conf" ]; then
  66. ips=$(grep 'MEFaccept 127.0.0.1' $fw_conf)
  67. new_ips=$(echo "$ips" | sed "s/$ip//" )
  68. sed -i "s/$ips/$new_ips/g" $fw_conf
  69. fi
  70. # mod_rpaf
  71. rpaf_conf="/etc/$WEB_SYSTEM/mods-enabled/rpaf.conf"
  72. if [ -e "$rpaf_conf" ]; then
  73. ips=$(grep RPAFproxy_ips $rpaf_conf)
  74. new_ips=$(echo "$rpaf_str" | sed "s/$ip//")
  75. sed -i "s/$ips/$new_ips/g" $rpaf_conf
  76. fi
  77. fi
  78. #----------------------------------------------------------#
  79. # Vesta #
  80. #----------------------------------------------------------#
  81. # Updating user conf
  82. if [ ! -z "$user" ]; then
  83. decrease_user_value "$user" '$IP_OWNED'
  84. fi
  85. if [ "$user" = 'admin' ]; then
  86. if [ "$ip_status" = 'shared' ]; then
  87. for user in $(ls $VESTA/data/users/); do
  88. decrease_user_value "$user" '$IP_AVAIL'
  89. done
  90. else
  91. decrease_user_value 'admin' '$IP_AVAIL'
  92. fi
  93. else
  94. decrease_user_value "$user" '$IP_AVAIL'
  95. decrease_user_value 'admin' '$IP_AVAIL'
  96. fi
  97. # Adding task to the vesta pipe
  98. $BIN/v-restart-web
  99. if [ $? -ne 0 ]; then
  100. exit E_RESTART
  101. fi
  102. $BIN/v-restart-proxy
  103. if [ $? -ne 0 ]; then
  104. exit E_RESTART
  105. fi
  106. # Logging
  107. log_history "deleted system ip address $ip"
  108. log_event "$OK" "$EVENT"
  109. exit