v-add-sys-ipv6 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/bin/bash
  2. # info: add system ip address
  3. # options: IPV6 NETMASK [INTERFACE] [USER] [IP_STATUS] [IP_NAME]
  4. #
  5. # The function adds ipv6 address into a system. It also creates rc scripts. You
  6. # can specify ipv6 name which will be used as root domain for temporary aliases.
  7. # For example, if you set a1.myhosting.com as name, each new domain created on
  8. # this ipv6 will automatically receive alias $domain.a1.myhosting.com. Of course
  9. # you must have wildcard record *.a1.myhosting.com pointed to ipv6. This feature
  10. # is very handy when customer wants to test domain before dns migration.
  11. #----------------------------------------------------------#
  12. # Variable&Function #
  13. #----------------------------------------------------------#
  14. # Argument definition
  15. ipv6=${1// /}
  16. netmask=$2
  17. interface="${3-eth0}"
  18. user="${4-admin}"
  19. ip_status="${5-shared}"
  20. ip_name=$6
  21. # Includes
  22. source $HESTIA/func/main.sh
  23. source $HESTIA/func/ipv6.sh
  24. source $HESTIA/func/domain.sh
  25. source $HESTIA/conf/hestia.conf
  26. #----------------------------------------------------------#
  27. # Verifications #
  28. #----------------------------------------------------------#
  29. check_args '2' "$#" 'IPV6 NETMASK [INTERFACE] [USER] [STATUS] [NAME]'
  30. is_format_valid 'ipv6' 'netmaskv6' 'interface' 'user' 'ip_status'
  31. is_ipv6_free
  32. is_object_valid 'user' 'USER' "$user"
  33. is_object_unsuspended 'user' 'USER' "$user"
  34. if [ ! -z "$ip_name" ] ; then
  35. is_format_valid 'ip_name'
  36. fi
  37. #----------------------------------------------------------#
  38. # Action #
  39. #----------------------------------------------------------#
  40. iface=$(get_ipv6_iface)
  41. #cidr=$(convert_netmaskv6 $netmask)
  42. cidr=$netmask
  43. sys_ip_check=$(/sbin/ip -6 addr | grep "$ipv6")
  44. if [ -z "$sys_ip_check" ]; then
  45. # Adding sys ip
  46. /sbin/ip addr add $ipv6/$cidr dev $interface
  47. # Adding RHEL/CentOS/Fedora startup script
  48. if [ -e "/etc/redhat-release" ]; then
  49. sys_ip="# Added by hestia"
  50. sys_ip="$sys_ip\nIPV6INIT=yes"
  51. sys_ip="$sys_ip\nIPV6ADDR=$ipv6/$cidr"
  52. sys_ip="$sys_ip\nIPV6_DEFAULTGW=$interface"
  53. sys_ip="$sys_ip\nIPV6_AUTOCONF=no"
  54. #sys_ip="$sys_ip\nIPV6ADDR_SECONDARIES="""
  55. echo -e $sys_ip > /etc/sysconfig/network-scripts/ifcfg-$interface
  56. fi
  57. # Adding Debian/Ubuntu startup script
  58. if [ -e "/etc/debian_version" ]; then
  59. sys_ip="\n# Added by hestia"
  60. sys_ip="$sys_ip\niface $interface inet6 static"
  61. sys_ip="$sys_ip\naddress $ipv6"
  62. sys_ip="$sys_ip\nnetmask $cidr"
  63. echo -e $sys_ip >> /etc/network/interfaces
  64. fi
  65. fi
  66. # Generating timestamp
  67. time_n_date=$(date +'%T %F')
  68. time=$(echo "$time_n_date" |cut -f 1 -d \ )
  69. date=$(echo "$time_n_date" |cut -f 2 -d \ )
  70. # Adding hestia ip
  71. echo "OWNER='$user'
  72. STATUS='$ip_status'
  73. NAME='$ip_name'
  74. U_SYS_USERS=''
  75. U_WEB_DOMAINS='0'
  76. INTERFACE='$interface'
  77. NETMASK='$netmask'
  78. NAT=''
  79. TIME='$time'
  80. DATE='$date'
  81. VERSION='6'" > $HESTIA/data/ips/$ipv6
  82. chmod 660 $HESTIA/data/ips/$ipv6
  83. # WEB support
  84. if [ ! -z "$WEB_SYSTEM" ]; then
  85. web_conf="/etc/$WEB_SYSTEM/conf.d/$ipv6.conf"
  86. rm -f $web_conf
  87. if [ "$WEB_SYSTEM" = 'httpd' ] || [ "$WEB_SYSTEM" = 'apache2' ]; then
  88. if [ -z "$(/usr/sbin/apachectl -v | grep Apache/2.4)" ]; then
  89. echo "NameVirtualHost [$ipv6]:$WEB_PORT" > $web_conf
  90. fi
  91. echo "Listen [$ipv6]:$WEB_PORT" >> $web_conf
  92. fi
  93. if [ "$WEB_SSL" = 'mod_ssl' ]; then
  94. if [ -z "$(/usr/sbin/apachectl -v | grep Apache/2.4)" ]; then
  95. echo "NameVirtualHost [$ipv6]:$WEB_SSL_PORT" >> $web_conf
  96. fi
  97. echo "Listen [$ipv6]:$WEB_SSL_PORT" >> $web_conf
  98. fi
  99. fi
  100. # Proxy support
  101. if [ ! -z "$PROXY_SYSTEM" ]; then
  102. cat $WEBTPL/$PROXY_SYSTEM/proxy_ip.tpl |\
  103. sed -e "s/%ip%/[$ipv6]/g" \
  104. -e "s/%web_port%/$WEB_PORT/g" \
  105. -e "s/%proxy_port%/$PROXY_PORT/g" \
  106. > /etc/$PROXY_SYSTEM/conf.d/$ipv6.conf
  107. # mod_rpaf
  108. rpaf_conf="/etc/$WEB_SYSTEM/mods-enabled/rpaf.conf"
  109. if [ -e "$rpaf_conf" ]; then
  110. rpaf_str=$(grep RPAFproxy_ips $rpaf_conf)
  111. rpaf_str="$rpaf_str $ipv6"
  112. sed -i "s/.*RPAFproxy_ips.*/$rpaf_str/" $rpaf_conf
  113. fi
  114. #mod_remoteip
  115. remoteip_conf="/etc/$WEB_SYSTEM/mods-enabled/remoteip.conf"
  116. if [ -e "$remoteip_conf" ]; then
  117. if [ $( grep -ic "$ipv6" $remoteip_conf ) -eq 0 ]; then
  118. sed -i "s/<\/IfModule>/RemoteIPInternalProxy $ipv6\n<\/IfModule>/g" $remoteip_conf
  119. fi
  120. fi
  121. fi
  122. #----------------------------------------------------------#
  123. # Hestia #
  124. #----------------------------------------------------------#
  125. # Updating user counters
  126. increase_user_value "$user" '$IPV6_OWNED'
  127. if [ "$user" = 'admin' ]; then
  128. if [ "$ip_status" = 'shared' ]; then
  129. for user in $(ls $HESTIA/data/users); do
  130. increase_user_value "$user" '$IPV6_AVAIL'
  131. done
  132. else
  133. increase_user_value 'admin' '$IPV6_AVAIL'
  134. fi
  135. else
  136. increase_user_value "$user" '$IPV6_AVAIL'
  137. increase_user_value 'admin' '$IPV6_AVAIL'
  138. fi
  139. # Restarting web server
  140. $BIN/v-restart-web
  141. check_result $? "Web restart failed" >/dev/null
  142. # Restarting proxy server
  143. if [ ! -z "$PROXY_SYSTEM" ]; then
  144. $BIN/v-restart-proxy
  145. check_result $? "Proxy restart failed" >/dev/null
  146. fi
  147. # Restarting firewall
  148. if [ ! -z "$FIREWALL_SYSTEM" ]; then
  149. $BIN/v-update-firewall
  150. fi
  151. # Logging
  152. log_history "added system ipv6 address $ipv6" '' 'admin'
  153. log_event "$OK" "$ARGUMENTS"
  154. exit