v-delete-sys-ip 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 definition
  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. is_format_valid '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. # Import ip variables
  29. source $VESTA/data/ips/$ip
  30. cidr=$(convert_netmask $NETMASK)
  31. # Checking main ip on the interface
  32. interface=$(/sbin/ip addr | grep "$ip/$cidr" | awk '{print $NF}')
  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" "$ARGUMENTS"
  36. exit $E_FORBIDEN
  37. fi
  38. # Deleting system ip
  39. if [ ! -z "$interface" ]; then
  40. /sbin/ip addr del $ip/$cidr dev $INTERFACE
  41. if [ "$?" -ne 0 ]; then
  42. echo "Error: can't delete system ip"
  43. log_event "$E_FORBIDEN" "$ARGUMENTS"
  44. exit $E_FORBIDEN
  45. fi
  46. fi
  47. # Deleting startup conf on RHEL/CentOS/Fedora
  48. if [ -e "/etc/sysconfig/network-scripts/ifcfg-$interface" ]; then
  49. rm -f /etc/sysconfig/network-scripts/ifcfg-$interface
  50. fi
  51. # Deleting startup conf on Debian/Ubuntu
  52. if [ -e "/etc/network/interfaces" ]; then
  53. ip_str=$(grep -n $ip$ /etc/network/interfaces |cut -f1 -d:)
  54. if [ ! -z "$ip_str" ]; then
  55. first_str=$((ip_str - 3))
  56. last_str=$((ip_str + 1))
  57. sed -i "$first_str,$last_str d" /etc/network/interfaces
  58. fi
  59. fi
  60. # Deleting vesta ip
  61. rm -f $VESTA/data/ips/$ip
  62. # Deleting web config
  63. if [ ! -z "$WEB_SYSTEM" ]; then
  64. rm -f /etc/$WEB_SYSTEM/conf.d/$ip.conf
  65. fi
  66. # Deleting proxy config
  67. if [ ! -z "$PROXY_SYSTEM" ]; then
  68. rm -f /etc/$PROXY_SYSTEM/conf.d/$ip.conf
  69. # mod_extract_forwarded
  70. fw_conf="/etc/$WEB_SYSTEM/conf.d/mod_extract_forwarded.conf"
  71. if [ -e "$fw_conf" ]; then
  72. ips=$(grep 'MEFaccept 127.0.0.1' $fw_conf)
  73. new_ips=$(echo "$ips" | sed "s/$ip//" )
  74. sed -i "s/$ips/$new_ips/g" $fw_conf
  75. fi
  76. # mod_rpaf
  77. rpaf_conf="/etc/$WEB_SYSTEM/mods-enabled/rpaf.conf"
  78. if [ -e "$rpaf_conf" ]; then
  79. ips=$(grep RPAFproxy_ips $rpaf_conf)
  80. new_ips=$(echo "$rpaf_str" | sed "s/$ip//")
  81. sed -i "s/$ips/$new_ips/g" $rpaf_conf
  82. fi
  83. #mod_remoteip
  84. remoteip_conf="/etc/$WEB_SYSTEM/mods-enabled/remoteip.conf"
  85. if [ -e "$remoteip_conf" ]; then
  86. sed -i "s/RemoteIPInternalProxy $ip//g" $remoteip_conf
  87. fi
  88. fi
  89. #----------------------------------------------------------#
  90. # Vesta #
  91. #----------------------------------------------------------#
  92. # Updating user conf
  93. if [ ! -z "$OWNER" ]; then
  94. decrease_user_value "$OWNER" '$IP_OWNED'
  95. fi
  96. if [ "$OWNER" = 'admin' ]; then
  97. if [ "$STATUS" = 'shared' ]; then
  98. for user in $(ls $VESTA/data/users/); do
  99. decrease_user_value "$user" '$IP_AVAIL'
  100. done
  101. fi
  102. else
  103. decrease_user_value "$OWNER" '$IP_AVAIL'
  104. fi
  105. # Restarting web server
  106. $BIN/v-restart-web
  107. check_result $? "Web restart failed" >/dev/null
  108. # Restarting proxy server
  109. if [ ! -z "$PROXY_SYSTEM" ]; then
  110. $BIN/v-restart-proxy
  111. check_result $? "Proxy restart failed" >/dev/null
  112. fi
  113. # Restarting firewall
  114. if [ ! -z "$FIREWALL_SYSTEM" ]; then
  115. $BIN/v-update-firewall
  116. fi
  117. # Logging
  118. log_history "deleted system ip address $ip"
  119. log_event "$OK" "$ARGUMENTS"
  120. exit