v-delete-sys-ip 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #!/bin/bash
  2. # info: delete system ip
  3. # options: IP
  4. #
  5. # example: v-delete-sys-ip 212.42.76.210
  6. #
  7. # This function for deleting a system ip. It does not allow to delete first ip
  8. # on interface and do not allow to delete ip which is used by a web domain.
  9. #----------------------------------------------------------#
  10. # Variables & Functions #
  11. #----------------------------------------------------------#
  12. # Argument definition
  13. ip=$1
  14. # Includes
  15. # shellcheck source=/etc/hestiacp/hestia.conf
  16. source /etc/hestiacp/hestia.conf
  17. # shellcheck source=/usr/local/hestia/func/main.sh
  18. source $HESTIA/func/main.sh
  19. # shellcheck source=/usr/local/hestia/func/ip.sh
  20. source $HESTIA/func/ip.sh
  21. # shellcheck source=/usr/local/hestia/func/domain.sh
  22. source $HESTIA/func/domain.sh
  23. # load config file
  24. source_conf "$HESTIA/conf/hestia.conf"
  25. #----------------------------------------------------------#
  26. # Verifications #
  27. #----------------------------------------------------------#
  28. check_args '1' "$#" 'IP'
  29. is_format_valid 'ip'
  30. is_ip_valid "$ip"
  31. is_ip_key_empty '$U_WEB_DOMAINS'
  32. is_ip_key_empty '$U_SYS_USERS'
  33. # Perform verification if read-only mode is enabled
  34. check_hestia_demo_mode
  35. #----------------------------------------------------------#
  36. # Action #
  37. #----------------------------------------------------------#
  38. # Import ip variables
  39. source $HESTIA/data/ips/$ip
  40. cidr=$(convert_netmask "$NETMASK")
  41. main_ip=$(hostname -i)
  42. # Checking main ip on the interface
  43. interface=$(/sbin/ip addr | grep "$ip$cidr" | awk '{print $NF}')
  44. if [ -n "$interface" ] && [ "$ip" = "$main_ip" ]; then
  45. echo "Error: can't delete main IP address"
  46. log_event "$E_FORBIDEN" "$ARGUMENTS"
  47. exit "$E_FORBIDEN"
  48. fi
  49. # Deleting system ip
  50. if [ -n "$interface" ]; then
  51. /sbin/ip addr del "$ip$cidr" dev "$INTERFACE"
  52. if [ "$?" -ne 0 ]; then
  53. echo "Error: can't delete system ip"
  54. log_event "$E_FORBIDEN" "$ARGUMENTS"
  55. exit $E_FORBIDEN
  56. fi
  57. fi
  58. # Deleting startup conf on RHEL/CentOS/Fedora
  59. if [ -e "/etc/sysconfig/network-scripts/ifcfg-$interface" ]; then
  60. rm -f /etc/sysconfig/network-scripts/ifcfg-$interface
  61. fi
  62. # Deleting startup conf on Debian/Ubuntu
  63. if [ -f "/etc/netplan/60-hestia.yaml" ]; then
  64. sed -i "/$ip/d" /etc/netplan/60-hestia.yaml
  65. if ! grep -q '-' /etc/netplan/60-hestia.yaml; then
  66. rm /etc/netplan/60-hestia.yaml
  67. fi
  68. elif [ -e "/etc/network/interfaces" ]; then
  69. ip_str=$(grep -n $ip$ /etc/network/interfaces | cut -f1 -d:)
  70. if [ -n "$ip_str" ]; then
  71. first_str=$((ip_str - 3))
  72. last_str=$((ip_str + 1))
  73. sed -i "$first_str,$last_str d" /etc/network/interfaces
  74. fi
  75. fi
  76. # Deleting hestia ip
  77. rm -f $HESTIA/data/ips/$ip
  78. # Deleting web config
  79. if [ -n "$WEB_SYSTEM" ]; then
  80. rm -f /etc/$WEB_SYSTEM/conf.d/$ip.conf
  81. fi
  82. # Deleting proxy config
  83. if [ -n "$PROXY_SYSTEM" ]; then
  84. rm -f /etc/$PROXY_SYSTEM/conf.d/$ip.conf
  85. # mod_extract_forwarded
  86. fw_conf="/etc/$WEB_SYSTEM/conf.d/mod_extract_forwarded.conf"
  87. if [ -e "$fw_conf" ]; then
  88. ips=$(grep 'MEFaccept 127.0.0.1' $fw_conf)
  89. new_ips=$(echo "$ips" | sed "s/$ip//")
  90. sed -i "s/$ips/$new_ips/g" $fw_conf
  91. fi
  92. # mod_rpaf
  93. rpaf_conf="/etc/$WEB_SYSTEM/mods-enabled/rpaf.conf"
  94. if [ -e "$rpaf_conf" ]; then
  95. ips=$(grep RPAFproxy_ips "$rpaf_conf")
  96. new_ips=$(echo "$ips" | sed "s/ $ip//")
  97. sed -i "s/$ips/$new_ips/g" "$rpaf_conf"
  98. # Remove RPAFproxy_ips line when ip list is empty
  99. [ "$(grep RPAFproxy_ips $rpaf_conf | sed 's/^[[:space:]]*//g')" = "RPAFproxy_ips" ] && sed -i "/RPAFproxy_ips/d" $rpaf_conf
  100. fi
  101. #mod_remoteip
  102. remoteip_conf="/etc/$WEB_SYSTEM/mods-enabled/remoteip.conf"
  103. if [ -e "$remoteip_conf" ]; then
  104. sed -i "/RemoteIPInternalProxy $ip\$/d" "$remoteip_conf"
  105. fi
  106. fi
  107. #----------------------------------------------------------#
  108. # Hestia #
  109. #----------------------------------------------------------#
  110. # Updating user conf
  111. if [ -n "$OWNER" ]; then
  112. decrease_user_value "$OWNER" '$IP_OWNED'
  113. fi
  114. if [ "$OWNER" = 'admin' ]; then
  115. if [ "$STATUS" = 'shared' ]; then
  116. for hestia_user in $($BIN/v-list-sys-users plain); do
  117. decrease_user_value "$hestia_user" '$IP_AVAIL'
  118. done
  119. else
  120. decrease_user_value "$OWNER" '$IP_AVAIL'
  121. fi
  122. else
  123. decrease_user_value "$OWNER" '$IP_AVAIL'
  124. fi
  125. # Restarting web server
  126. $BIN/v-restart-web
  127. check_result $? "Web restart failed" > /dev/null
  128. # Restarting proxy server
  129. if [ -n "$PROXY_SYSTEM" ]; then
  130. $BIN/v-restart-proxy
  131. check_result $? "Proxy restart failed" > /dev/null
  132. fi
  133. # Restarting firewall
  134. if [ -n "$FIREWALL_SYSTEM" ]; then
  135. $BIN/v-update-firewall
  136. fi
  137. # Logging
  138. $BIN/v-log-action "system" "Info" "System" "IP address deleted (IP: $ip)."
  139. log_event "$OK" "$ARGUMENTS"
  140. exit