ynh-vpnclient-loadcubefile.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/bin/bash
  2. # VPN Client app for YunoHost
  3. # Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
  4. # Contribute at https://github.com/labriqueinternet/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. # Options
  19. while getopts "u:p:c:h" opt; do
  20. case $opt in
  21. u)
  22. ynh_user=$OPTARG
  23. ;;
  24. p)
  25. ynh_password=$OPTARG
  26. ;;
  27. c)
  28. cubefile_path=$OPTARG
  29. if [ ! -r "${cubefile_path}" ]; then
  30. echo "[ERR] Cube file does not exist or is unreadable" >&2
  31. exit 1
  32. fi
  33. ;;
  34. h)
  35. echo "-u SSO username (user with permissions on VPN Client)"
  36. echo "-p SSO password"
  37. echo "-c Dot cube file path"
  38. echo "-h This help"
  39. exit 0
  40. ;;
  41. \?)
  42. echo "[ERR] Invalid option: -$OPTARG (-h for help)" >&2
  43. exit 1
  44. ;;
  45. :)
  46. echo "[ERR] Option -$OPTARG requires an argument" >&2
  47. exit 1
  48. ;;
  49. esac
  50. done
  51. if [ -z "${ynh_user}" ]; then
  52. echo "[ERR] Option -u is mandatory (-h for help)" >&2
  53. exit 1
  54. fi
  55. if [ -z "${ynh_password}" ]; then
  56. echo "[ERR] Option -p is mandatory (-h for help)" >&2
  57. exit 1
  58. fi
  59. if [ -z "${cubefile_path}" ]; then
  60. echo "[ERR] Option -c is mandatory (-h for help)" >&2
  61. exit 1
  62. fi
  63. # Other variables
  64. ynh_setting() {
  65. app=${1}
  66. setting=${2}
  67. sudo grep "^${setting}:" "/etc/yunohost/apps/${app}/settings.yml" | sed s/^[^:]\\+:\\s*[\"\']\\?// | sed s/\\s*[\"\']\$//
  68. }
  69. tmpdir=$(mktemp -dp /tmp/ vpnclient-loadcubefile-XXXXX)
  70. cubefile_ip6=$(sed -n '/ip6_net/ { s/.*"\([0-9a-zA-Z:]\+\)".*/\1/p }' "${cubefile_path}")
  71. ynh_domain=$(ynh_setting vpnclient domain)
  72. ynh_path=$(ynh_setting vpnclient path)
  73. ynh_service_enabled=$(ynh_setting vpnclient service_enabled)
  74. # SSO login
  75. curl -kLe "https://${ynh_domain}/yunohost/sso/" -d "user=${ynh_user}" -d "password=${ynh_password}" "https://${ynh_domain}/yunohost/sso/" --resolve "${ynh_domain}:443:127.0.0.1" -c "${tmpdir}/cookies" 2> /dev/null | grep -q Logout
  76. if [ $? -ne 0 ]; then
  77. echo "[ERROR] SSO login failed" >&2
  78. exit 1
  79. fi
  80. # Upload cube file
  81. output=$(curl -kL -F "service_enabled=${ynh_service_enabled}" -F _method=put -F "cubefile=@${cubefile_path}" "https://${ynh_domain}/${ynh_path}/?/settings" --resolve "${ynh_domain}:443:127.0.0.1" -b "${tmpdir}/cookies" 2> /dev/null | grep RETURN_MSG | sed 's/<!-- RETURN_MSG -->//' | sed 's/<\/?[^>]\+>//g' | sed 's/^ \+//g')
  82. # Configure IPv6 Delegated Prefix on Hotspot
  83. if [ ! -z "${cubefile_ip6}" ] && (sudo yunohost app info hotspot | grep -q Hotspot); then
  84. ynh_multissid=$(ynh_setting hotspot multissid)
  85. if [ "${ynh_multissid}" -eq 1 ]; then
  86. ynh_ip6_net=$(ynh_setting vpnclient ip6_net)
  87. ynh_ip6_addr=$(ynh_setting vpnclient ip6_addr)
  88. sudo systemctl stop ynh-hotspot &> /dev/null
  89. sudo yunohost app setting hotspot ip6_net -v "${ynh_ip6_net}"
  90. sudo yunohost app setting hotspot ip6_addr -v "${ynh_ip6_addr}"
  91. sudo systemctl start ynh-hotspot &> /dev/null
  92. echo "[INFO] Wifi Hotspot found with only one SSID: IPv6 Delegated Prefix automatically configured" >&2
  93. fi
  94. fi
  95. # Done!
  96. echo [VPN] $output
  97. (echo $output | grep -q Error) && exit 1
  98. rm -r "${tmpdir}/"
  99. exit 0