ynh-vpnclient-loadcubefile.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 YunoHost username (user with permissions on VPN Client)"
  36. echo "-p User password"
  37. echo "-c Dot cube file path"
  38. echo "-h This help"
  39. exit 0
  40. ;;
  41. \?)
  42. echo "[ERR] Invalid option (-h for help)" >&2
  43. exit 1
  44. ;;
  45. esac
  46. done
  47. if [ -z "${ynh_user}" ]; then
  48. echo "[ERR] Option -u is mandatory (-h for help)" >&2
  49. exit 1
  50. fi
  51. if [ -z "${ynh_password}" ]; then
  52. echo "[ERR] Option -p is mandatory (-h for help)" >&2
  53. exit 1
  54. fi
  55. if [ -z "${cubefile_path}" ]; then
  56. echo "[ERR] Option -c is mandatory (-h for help)" >&2
  57. exit 1
  58. fi
  59. # Other variables
  60. ynh_setting() {
  61. app=${1}
  62. setting=${2}
  63. sudo grep "^${setting}:" "/etc/yunohost/apps/${app}/settings.yml" | sed s/^[^:]\\+:\\s*[\"\']\\?// | sed s/\\s*[\"\']\$//
  64. }
  65. tmpdir=$(mktemp -dp /tmp/ vpnclient-loadcubefile-XXXXX)
  66. cubefile_ip6=$(sed -n '/ip6_net/ { s/.*"\([0-9a-zA-Z:]\+\)".*/\1/p }' "${cubefile_path}")
  67. ynh_domain=$(ynh_setting vpnclient domain)
  68. ynh_path=$(ynh_setting vpnclient path)
  69. ynh_service_enabled=$(ynh_setting vpnclient service_enabled)
  70. # SSO login
  71. curl -D - -skLe "https://${ynh_domain}/yunohost/sso/" --data-urlencode "user=${ynh_user}" --data-urlencode "password=${ynh_password}" "https://${ynh_domain}/yunohost/sso/" --resolve "${ynh_domain}:443:127.0.0.1" -o /dev/null -c "${tmpdir}/cookies" 2> /dev/null | grep -q "set-cookie: SSOwAuthUser=${ynh_user}"
  72. if [ $? -ne 0 ]; then
  73. echo "[ERROR] SSO login failed" >&2
  74. exit 1
  75. fi
  76. # Upload cube file
  77. output=$(curl -kL -H "X-Requested-With: yunohost-config" -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')
  78. # Configure IPv6 Delegated Prefix on Hotspot
  79. if [ ! -z "${cubefile_ip6}" ] && (sudo yunohost app info hotspot | grep -q Hotspot); then
  80. ynh_multissid=$(ynh_setting hotspot multissid)
  81. if [ "${ynh_multissid}" -eq 1 ]; then
  82. ynh_ip6_net=$(ynh_setting vpnclient ip6_net)
  83. ynh_ip6_addr=$(ynh_setting vpnclient ip6_addr)
  84. sudo systemctl stop ynh-hotspot &> /dev/null
  85. sudo yunohost app setting hotspot ip6_net -v "${ynh_ip6_net}"
  86. sudo yunohost app setting hotspot ip6_addr -v "${ynh_ip6_addr}"
  87. sudo systemctl start ynh-hotspot &> /dev/null
  88. echo "[INFO] Wifi Hotspot found with only one SSID: IPv6 Delegated Prefix automatically configured" >&2
  89. fi
  90. fi
  91. # Done!
  92. echo [VPN] $output
  93. (echo $output | grep -q Error) && exit 1
  94. rm -r "${tmpdir}/"
  95. exit 0