install 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #!/bin/bash
  2. # VPN Client app for YunoHost
  3. # Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
  4. # Contribute at https://github.com/jvaubourg/vpnclient_ynh
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. # Retrieve arguments
  19. domain=${1}
  20. url_path=${2}
  21. server_name=${3}
  22. ##
  23. ## These arguments are optional but YunoHost is not yet able to handle them with the web installer
  24. ## See manifest.json.options
  25. ##
  26. #
  27. #crt_client_path=${4}
  28. #crt_client_key_path=${5}
  29. #crt_server_ca_path=${6}
  30. #login_user=${7}
  31. #login_passphrase=${8}
  32. # Check arguments
  33. if [ -z "${server_name}" ]; then
  34. echo "ERROR: You need a VPN server name" >&2
  35. exit 1
  36. fi
  37. #if [ \( -z "${crt_client_path}" -a ! -z "${crt_client_key_path}" \)\
  38. # -o \( ! -z "${crt_client_path}" -a -z "${crt_client_key_path}" \) ]; then
  39. #
  40. # echo "ERROR: A client certificate is needed when you suggest a key (or vice versa)" >&2
  41. # exit 1
  42. #fi
  43. #
  44. #if [ ! -z "${crt_client_key_path}" -a -z "${crt_server_ca_path}" ]; then
  45. # echo "ERROR: If you can suggest a local path for the client certificates, you probably can suggest one other for the (mandatory) CA server" >&2
  46. # exit 1
  47. #fi
  48. #
  49. #if [ \( -z "${login_user}" -a ! -z "${login_passphrase}" \)\
  50. # -o \( ! -z "${login_user}" -a -z "${login_passphrase}" \) ]; then
  51. #
  52. # echo "ERROR: A login password is needed when you suggest a login user (or vice versa)" >&2
  53. # exit 1
  54. #fi
  55. #
  56. #if [ ! -z "${crt_client_path}" -a ! -f "${crt_client_path}" ]; then
  57. # echo "ERROR: The local path <${crt_client_path}> does not exist" >&2
  58. # exit 1
  59. #fi
  60. #
  61. #if [ ! -z "${crt_client_key_path}" -a ! -f "${crt_client_key_path}" ]; then
  62. # echo "ERROR: The local path <${crt_client_key_path}> does not exist" >&2
  63. # exit 1
  64. #fi
  65. #
  66. #if [ ! -z "${crt_server_ca_path}" -a ! -f "${crt_server_ca_path}" ]; then
  67. # echo "ERROR: The local path <${crt_server_ca_path}> does not exist" >&2
  68. # exit 1
  69. #fi
  70. # Check domain/path availability
  71. sudo yunohost app checkurl ${domain}${url_path} -a vpnclient
  72. if [ ! $? -eq 0 ]; then
  73. exit 1
  74. fi
  75. # Install packages
  76. packages='php5-fpm sipcalc openvpn'
  77. sudo apt-get --assume-yes --force-yes install ${packages}
  78. if [ $? -ne 0 ]; then
  79. sudo apt-get update
  80. sudo apt-get --assume-yes --force-yes install ${packages}
  81. fi
  82. # Save arguments
  83. sudo yunohost app setting vpnclient server_name -v "${server_name}"
  84. sudo yunohost app setting vpnclient server_port -v 1194
  85. sudo yunohost app setting vpnclient server_proto -v udp
  86. sudo yunohost app setting vpnclient ip6_addr -v none
  87. sudo yunohost app setting vpnclient ip6_net -v none
  88. sudo yunohost app setting vpnclient login_user -v "${login_user}"
  89. sudo yunohost app setting vpnclient login_passphrase -v "${login_passphrase}"
  90. # Install IPv6 scripts
  91. sudo install -o root -g root -m 0755 ../conf/ipv6_expanded /usr/local/bin/
  92. sudo install -o root -g root -m 0755 ../conf/ipv6_compressed /usr/local/bin/
  93. # Copy confs
  94. sudo chown root:admins /etc/openvpn/
  95. sudo chmod 775 /etc/openvpn/
  96. sudo install -b -o root -g admins -m 0664 ../conf/openvpn_client.conf.tpl /etc/openvpn/client.conf.tpl
  97. sudo install -o root -g root -m 0644 ../conf/openvpn_client.conf.tpl /etc/openvpn/client.conf.tpl.restore
  98. sudo install -b -o root -g root -m 0644 ../conf/nginx_vpnadmin.conf "/etc/nginx/conf.d/${domain}.d/vpnadmin.conf"
  99. sudo install -b -o root -g root -m 0644 ../conf/phpfpm_vpnadmin.conf /etc/php5/fpm/pool.d/vpnadmin.conf
  100. # Copy web sources
  101. sudo mkdir -pm 0755 /var/www/vpnadmin/
  102. sudo cp -a ../sources/* /var/www/vpnadmin/
  103. sudo chown -R root: /var/www/vpnadmin/
  104. sudo chmod -R 0644 /var/www/vpnadmin/*
  105. sudo find /var/www/vpnadmin/ -type d -exec chmod +x {} \;
  106. # Copy certificates
  107. sudo mkdir -pm 0770 /etc/openvpn/keys/
  108. sudo chown root:admins /etc/openvpn/keys/
  109. #[ ! -z "${crt_client_path}" ] &&\
  110. # sudo install -b -o root -g admins -m 0660 "${crt_client_path}" /etc/openvpn/keys/user.crt
  111. #
  112. #[ ! -z "${crt_client_key_path}" ] &&\
  113. # sudo install -b -o root -g admins -m 0660 "${crt_client_key_path}" /etc/openvpn/keys/user.key
  114. #
  115. #[ ! -z "${crt_server_ca_path}" ] &&\
  116. # sudo install -b -o root -g admins -m 0660 "${crt_server_ca_path}" /etc/openvpn/keys/ca-server.crt
  117. #
  118. #sudo rm -f "${crt_client_path}" "${crt_client_key_path}" "${crt_server_ca_path}"
  119. # Credentials file for (optional) login
  120. #sudo cat << EOF > /etc/openvpn/keys/credentials
  121. #${login_user}
  122. #${login_passphrase}
  123. #EOF
  124. #
  125. #sudo chown -R root:admins /etc/openvpn/keys/credentials
  126. #sudo chmod 0460 /etc/openvpn/keys/credentials
  127. # Fix confs
  128. ## nginx
  129. sudo sed "s|<TPL:NGINX_LOCATION>|${url_path}|g" -i "/etc/nginx/conf.d/${domain}.d/vpnadmin.conf"
  130. sudo sed 's|<TPL:NGINX_REALPATH>|/var/www/vpnadmin/|g' -i "/etc/nginx/conf.d/${domain}.d/vpnadmin.conf"
  131. sudo sed 's|<TPL:PHP_NAME>|vpnadmin|g' -i "/etc/nginx/conf.d/${domain}.d/vpnadmin.conf"
  132. ## php-fpm
  133. sudo sed 's|<TPL:PHP_NAME>|vpnadmin|g' -i /etc/php5/fpm/pool.d/vpnadmin.conf
  134. sudo sed 's|<TPL:PHP_USER>|admin|g' -i /etc/php5/fpm/pool.d/vpnadmin.conf
  135. sudo sed 's|<TPL:PHP_GROUP>|admins|g' -i /etc/php5/fpm/pool.d/vpnadmin.conf
  136. sudo sed 's|<TPL:NGINX_REALPATH>|/var/www/vpnadmin/|g' -i /etc/php5/fpm/pool.d/vpnadmin.conf
  137. sudo sed 's|^;\?\s*max_execution_time.\+|max_execution_time = 600|' -i /etc/php5/fpm/php.ini
  138. # Fix sources
  139. sudo sed "s|<TPL:NGINX_LOCATION>|${url_path}|g" -i /var/www/vpnadmin/config.php
  140. # Copy init script
  141. sudo install -o root -g root -m 0755 ../conf/init_ynh-vpnclient /etc/init.d/ynh-vpnclient
  142. # Set default inits
  143. # The openvpn configuration is modified before the start, so the service is disabled by default
  144. # and the ynh-vpnclient service handles it.
  145. sudo yunohost service add openvpn
  146. sudo yunohost service stop openvpn
  147. sudo yunohost service disable openvpn
  148. sudo yunohost service add php5-fpm
  149. sudo yunohost service enable php5-fpm
  150. sudo yunohost service add ynh-vpnclient
  151. sudo yunohost service enable ynh-vpnclient
  152. sudo service ynh-vpnclient start
  153. sudo service nginx reload
  154. # Update SSO for vpnadmin
  155. sudo yunohost app ssowatconf
  156. # Restart hotspot service if installed (and started) to change NAT configuration (now on tun0)
  157. # A new start will fix the interface without unsetting all stuff
  158. if [ -e /tmp/.ynh-hotspot-started ]; then
  159. sudo service ynh-hotspot start
  160. fi
  161. # Check configuration consistency
  162. if [ -z "${crt_server_ca_path}" ]; then
  163. echo "WARNING: VPN Client is not started because you need to define a server CA through the web admin" >&2
  164. fi
  165. if [ -z "${crt_client_key_path}" -a -z "${login_user}" ]; then
  166. echo "WARNING: VPN Client is not started because you need either a client certificate, either a username (or both)" >&2
  167. fi
  168. exit 0