v-add-sys-ip 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #!/bin/bash
  2. # info: add system ip address
  3. # options: IP MASK [INTERFACE] [USER] [IP_STATUS] [IP_NAME] [NAT_IP]
  4. #
  5. # The function adds ip address into a system. It also creates rc scripts. You
  6. # can specify ip 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 ip will automaticaly receive alias $domain.a1.myhosting.com. Of course
  9. # you must have wildcard record *.a1.myhosting.com pointed to ip. This feature
  10. # is very handy when customer wants to test domain before dns migration.
  11. #----------------------------------------------------------#
  12. # Variable&Function #
  13. #----------------------------------------------------------#
  14. # Argument defenition
  15. ip=${1// /}
  16. mask=$2
  17. interface="${3-eth0}"
  18. user="${4-admin}"
  19. ip_status="${5-shared}"
  20. ip_name=$6
  21. nat_ip=$7
  22. # Includes
  23. source $VESTA/func/main.sh
  24. source $VESTA/func/ip.sh
  25. source $VESTA/func/domain.sh
  26. source $VESTA/conf/vesta.conf
  27. #----------------------------------------------------------#
  28. # Verifications #
  29. #----------------------------------------------------------#
  30. check_args '2' "$#" 'IP MASK [INTERFACE] [USER] [IP_STATUS] [IP_NAME] [NAT_IP]'
  31. validate_format 'ip' 'mask' 'interface' 'user' 'ip_status'
  32. is_ip_free
  33. is_object_valid 'user' 'USER' "$user"
  34. is_object_unsuspended 'user' 'USER' "$user"
  35. if [ ! -z "$ip_name" ] ; then
  36. validate_format 'ip_name'
  37. fi
  38. if [ ! -z "$nat_ip" ] ; then
  39. validate_format 'nat_ip'
  40. fi
  41. #----------------------------------------------------------#
  42. # Action #
  43. #----------------------------------------------------------#
  44. get_ip_iface
  45. sys_ip_check=$(/sbin/ifconfig | grep "addr:$ip ")
  46. if [ -z "$sys_ip_check" ]; then
  47. # Adding sys ip
  48. /sbin/ifconfig "$iface" "$ip" netmask "$mask"
  49. # Adding RHEL/CentOS/Fedora startup script
  50. if [ -e "/etc/redhat-release" ]; then
  51. sys_ip="# Added by vesta"
  52. sys_ip="$sys_ip\nDEVICE=$iface"
  53. sys_ip="$sys_ip\nBOOTPROTO=static"
  54. sys_ip="$sys_ip\nONBOOT=yes"
  55. sys_ip="$sys_ip\nIPADDR=$ip"
  56. sys_ip="$sys_ip\nNETMASK=$mask"
  57. echo -e $sys_ip > /etc/sysconfig/network-scripts/ifcfg-$iface
  58. fi
  59. # Adding Debian/Ubuntu startup script
  60. if [ -e "/etc/debian_version" ]; then
  61. sys_ip="\n# Added by vesta"
  62. sys_ip="$sys_ip\nauto $iface"
  63. sys_ip="$sys_ip\niface $iface inet static"
  64. sys_ip="$sys_ip\naddress $ip"
  65. sys_ip="$sys_ip\nnetmask $mask"
  66. echo -e $sys_ip >> /etc/network/interfaces
  67. fi
  68. fi
  69. # Adding vesta ip
  70. echo "OWNER='$user'
  71. STATUS='$ip_status'
  72. NAME='$ip_name'
  73. U_SYS_USERS=''
  74. U_WEB_DOMAINS='0'
  75. INTERFACE='$interface'
  76. NETMASK='$mask'
  77. NAT='$nat_ip'
  78. TIME='$TIME'
  79. DATE='$DATE'" > $VESTA/data/ips/$ip
  80. chmod 660 $VESTA/data/ips/$ip
  81. # WEB support
  82. if [ ! -z "$WEB_SYSTEM" ]; then
  83. web_conf="/etc/$WEB_SYSTEM/conf.d/$ip.conf"
  84. if [ "$WEB_SYSTEM" = 'httpd' ] || [ "$WEB_SYSTEM" = 'apache2' ]; then
  85. echo "NameVirtualHost $ip:$WEB_PORT" > $web_conf
  86. echo "Listen $ip:$WEB_PORT" >> $web_conf
  87. fi
  88. if [ "$WEB_SSL" = 'mod_ssl' ]; then
  89. echo "NameVirtualHost $ip:$WEB_SSL_PORT" >> $web_conf
  90. echo "Listen $ip:$WEB_SSL_PORT" >> $web_conf
  91. fi
  92. fi
  93. # Proxy support
  94. if [ ! -z "$PROXY_SYSTEM" ]; then
  95. cat $WEBTPL/$PROXY_SYSTEM/proxy_ip.tpl |\
  96. sed -e "s/%ip%/$ip/g" \
  97. -e "s/%web_port%/$WEB_PORT/g" \
  98. -e "s/%proxy_port%/$PROXY_PORT/g" \
  99. > /etc/$PROXY_SYSTEM/conf.d/$ip.conf
  100. # mod_extract_forwarded
  101. fw_conf="/etc/$WEB_SYSTEM/conf.d/mod_extract_forwarded.conf"
  102. if [ -e "$fw_conf" ]; then
  103. ips=$(grep 'MEFaccept ' $fw_conf | grep -v '#' | head -n1)
  104. sed -i "s/$ips/$ips $ip/g" $fw_conf
  105. fi
  106. # mod_rpaf
  107. rpaf_conf="/etc/$WEB_SYSTEM/mods-enabled/rpaf.conf"
  108. if [ -e "$rpaf_conf" ]; then
  109. rpaf_str=$(grep RPAFproxy_ips $rpaf_conf)
  110. rpaf_str="$rpaf_str $ip"
  111. sed -i "s/.*RPAFproxy_ips.*/$rpaf_str/" $rpaf_conf
  112. fi
  113. fi
  114. #----------------------------------------------------------#
  115. # Vesta #
  116. #----------------------------------------------------------#
  117. # Updating user counters
  118. increase_user_value "$user" '$IP_OWNED'
  119. if [ "$user" = 'admin' ]; then
  120. if [ "$ip_status" = 'shared' ]; then
  121. for user in $(ls $VESTA/data/users); do
  122. increase_user_value "$user" '$IP_AVAIL'
  123. done
  124. else
  125. increase_user_value 'admin' '$IP_AVAIL'
  126. fi
  127. else
  128. increase_user_value "$user" '$IP_AVAIL'
  129. increase_user_value 'admin' '$IP_AVAIL'
  130. fi
  131. # Restart web server
  132. $BIN/v-restart-web
  133. if [ $? -ne 0 ]; then
  134. exit E_RESTART
  135. fi
  136. $BIN/v-restart-proxy
  137. if [ $? -ne 0 ]; then
  138. exit E_RESTART
  139. fi
  140. # Logging
  141. log_history "added system ip address $ip" '' 'admin'
  142. log_event "$OK" "$EVENT"
  143. exit