ynh-vpnclient 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. # Functions
  19. ## State functions
  20. has_nativeip6() {
  21. ip -6 route | grep -q default\ via
  22. }
  23. has_ip6delegatedprefix() {
  24. [ "${ynh_ip6_addr}" != none ]
  25. }
  26. has_hotspot_app() {
  27. [ -e /tmp/.ynh-hotspot-started ]
  28. }
  29. is_hotspot_knowme() {
  30. gotcha=0
  31. while [ "${gotcha}" -eq 0 ]; do
  32. value=$(yunohost app setting hotspot vpnclient)
  33. if [[ "${value}" =~ "An instance is already running" ]]; then
  34. sleep $(($((RANDOM%5)) + 1))
  35. else
  36. gotcha=1
  37. fi
  38. done
  39. [ "${value}" == yes ]
  40. }
  41. is_ip6addr_set() {
  42. ip address show dev tun0 2> /dev/null | grep -q "${ynh_ip6_addr}/128"
  43. }
  44. is_serverip6route_set() {
  45. server_ip6=${1}
  46. if [ -z "${server_ip6}" ]; then
  47. false
  48. else
  49. ip -6 route | grep -q "${server_ip6}/"
  50. fi
  51. }
  52. is_openvpn_running() {
  53. systemctl is-active openvpn@client.service &> /dev/null
  54. }
  55. is_running() {
  56. ((has_nativeip6 && is_serverip6route_set "${new_server_ip6}") || ! has_nativeip6)\
  57. && ((! has_hotspot_app && has_ip6delegatedprefix && is_ip6addr_set) || has_hotspot_app || ! has_ip6delegatedprefix)\
  58. && is_openvpn_running
  59. }
  60. ## Setters
  61. set_ip6addr() {
  62. ip address add "${ynh_ip6_addr}/128" dev tun0
  63. }
  64. set_serverip6route() {
  65. server_ip6=${1}
  66. ip6_gw=${2}
  67. wired_device=${3}
  68. ip route add "${server_ip6}/128" via "${ip6_gw}" dev "${wired_device}"
  69. }
  70. start_openvpn() {
  71. ip6_gw=${1}
  72. server_ip6=${2}
  73. if [ ! -z "${ip6_gw}" -a ! -z "${server_ip6}" ]; then
  74. proto=udp6
  75. [ "${ynh_server_proto}" == tcp ] && proto=tcp6-client
  76. else
  77. proto=udp
  78. [ "${ynh_server_proto}" == tcp ] && proto=tcp-client
  79. fi
  80. cp /etc/openvpn/client.conf{.tpl,}
  81. sed "s|<TPL:SERVER_NAME>|${ynh_server_name}|g" -i /etc/openvpn/client.conf
  82. sed "s|<TPL:SERVER_PORT>|${ynh_server_port}|g" -i /etc/openvpn/client.conf
  83. sed "s|<TPL:PROTO>|${proto}|g" -i /etc/openvpn/client.conf
  84. if [ -e /etc/openvpn/keys/user.key ]; then
  85. sed 's|^<TPL:CERT_COMMENT>||' -i /etc/openvpn/client.conf
  86. else
  87. sed 's|^<TPL:CERT_COMMENT>|;|' -i /etc/openvpn/client.conf
  88. fi
  89. if [[ "${proto}" =~ udp ]]; then
  90. sed 's|^<TPL:UDP_COMMENT>||' -i /etc/openvpn/client.conf
  91. else
  92. sed 's|^<TPL:UDP_COMMENT>|;|' -i /etc/openvpn/client.conf
  93. fi
  94. if [ -z "${ynh_login_user}" ]; then
  95. sed 's|^<TPL:LOGIN_COMMENT>|;|' -i /etc/openvpn/client.conf
  96. else
  97. sed 's|^<TPL:LOGIN_COMMENT>||' -i /etc/openvpn/client.conf
  98. fi
  99. systemctl start openvpn@client.service
  100. }
  101. ## Unsetters
  102. unset_ip6addr() {
  103. ip address delete "${ynh_ip6_addr}/128" dev tun0
  104. }
  105. unset_serverip6route() {
  106. server_ip6=${1}
  107. ip6_gw=${2}
  108. wired_device=${3}
  109. ip route delete "${server_ip6}/128" via "${ip6_gw}" dev "${wired_device}"
  110. }
  111. stop_openvpn() {
  112. systemctl stop openvpn.service
  113. }
  114. ## Tools
  115. moulinette_get() {
  116. var=${1}
  117. gotcha=0
  118. while [ "${gotcha}" -eq 0 ]; do
  119. value=$(yunohost app setting vpnclient "${var}")
  120. if [[ "${value}" =~ "An instance is already running" ]]; then
  121. sleep $(($((RANDOM%5)) + 1))
  122. else
  123. gotcha=1
  124. fi
  125. done
  126. echo "${value}"
  127. }
  128. moulinette_set() {
  129. var=${1}
  130. value=${2}
  131. msg=$(yunohost app setting vpnclient "${var}" -v "${value}")
  132. if [ ! $? -eq 0 ]; then
  133. echo "${msg}" >&2
  134. exit 1
  135. fi
  136. }
  137. if [ "$1" != restart ]; then
  138. # Restart php5-fpm at the first start (it needs to be restarted after the slapd start)
  139. if [ ! -e /tmp/.ynh-vpnclient-boot ]; then
  140. touch /tmp/.ynh-vpnclient-boot
  141. systemctl restart php5-fpm
  142. fi
  143. # Check configuration consistency
  144. if [[ ! "${1}" =~ stop ]]; then
  145. exitcode=0
  146. if [ ! -e /etc/openvpn/keys/ca-server.crt ]; then
  147. echo "[WARN] You need a CA server (you can add it through the web admin)"
  148. exitcode=1
  149. fi
  150. empty=$(find /etc/openvpn/keys/ -empty -name credentials &> /dev/null | wc -l)
  151. if [ "${empty}" -gt 0 -a ! -e /etc/openvpn/keys/user.key ]; then
  152. echo "[WARN] You need either a client certificate, either a username, or both (you can add one through the web admin)"
  153. exitcode=1
  154. fi
  155. [ "${exitcode}" -ne 0 ] && exit ${exitcode}
  156. fi
  157. # Variables
  158. echo -n "Retrieving Yunohost settings... "
  159. ynh_service_enabled=$(moulinette_get service_enabled)
  160. ynh_server_name=$(moulinette_get server_name)
  161. ynh_server_port=$(moulinette_get server_port)
  162. ynh_server_proto=$(moulinette_get server_proto)
  163. ynh_ip6_addr=$(moulinette_get ip6_addr)
  164. ynh_login_user=$(moulinette_get login_user)
  165. old_ip6_gw=$(moulinette_get ip6_gw)
  166. old_wired_device=$(moulinette_get wired_device)
  167. old_server_ip6=$(moulinette_get server_ip6)
  168. new_ip6_gw=$(ip -6 route | grep default\ via | awk '{ print $3 }')
  169. new_wired_device=$(ip route | awk '/default via/ { print $NF; }')
  170. new_server_ip6=$(host "${ynh_server_name}" | awk '/IPv6/ { print $NF; }')
  171. if [ -z "${new_server_ip6}" ]; then
  172. new_server_ip6=$(host "${ynh_server_name}" 80.67.188.188 | awk '/IPv6/ { print $NF; }')
  173. fi
  174. echo "OK"
  175. fi
  176. # Script
  177. case "${1}" in
  178. start)
  179. if is_running; then
  180. echo "Already started"
  181. elif [ "${ynh_service_enabled}" -eq 0 ]; then
  182. echo "Disabled service"
  183. else
  184. echo "[vpnclient] Starting..."
  185. touch /tmp/.ynh-vpnclient-started
  186. # Run openvpn
  187. if ! is_openvpn_running; then
  188. echo "Run openvpn"
  189. start_openvpn "${new_ip6_gw}" "${new_server_ip6}"
  190. if [ ! $? -eq 0 ]; then
  191. exit 1
  192. fi
  193. i=0; false || while [ $? -ne 0 ]; do
  194. sleep 1 && (( i++ ))
  195. [ ${i} -gt 20 ] && stop_openvpn
  196. [ ${i} -gt 20 ] && exit 1
  197. ip link show dev tun0 &> /dev/null
  198. done
  199. fi
  200. # Check old state of the server ipv6 route
  201. if [ ! -z "${old_server_ip6}" -a ! -z "${old_ip6_gw}" -a ! -z "${old_wired_device}"\
  202. -a \( "${new_server_ip6}" != "${old_server_ip6}" -o "${new_ip6_gw}" != "${old_ip6_gw}"\
  203. -o "${new_wired_device}" != "${old_wired_device}" \) ]\
  204. && is_serverip6route_set "${old_server_ip6}"; then
  205. unset_serverip6route "${old_server_ip6}" "${old_ip6_gw}" "${old_wired_device}"
  206. fi
  207. # Set the new server ipv6 route
  208. if has_nativeip6 && ! is_serverip6route_set "${new_server_ip6}"; then
  209. echo "Set IPv6 server route"
  210. set_serverip6route "${new_server_ip6}" "${new_ip6_gw}" "${new_wired_device}"
  211. fi
  212. # Set the ipv6 address
  213. if ! has_hotspot_app && has_ip6delegatedprefix && ! is_ip6addr_set; then
  214. echo "Set IPv6 address"
  215. set_ip6addr
  216. fi
  217. # Update dynamic settings
  218. moulinette_set server_ip6 "${new_server_ip6}"
  219. moulinette_set ip6_gw "${new_ip6_gw}"
  220. moulinette_set wired_device "${new_wired_device}"
  221. # Fix configuration
  222. if has_hotspot_app && ! is_hotspot_knowme; then
  223. ynh-hotspot start
  224. fi
  225. fi
  226. ;;
  227. stop)
  228. echo "[vpnclient] Stopping..."
  229. rm -f /tmp/.ynh-vpnclient-started
  230. if ! has_hotspot_app && has_ip6delegatedprefix && is_ip6addr_set; then
  231. echo "Unset IPv6 address"
  232. unset_ip6addr
  233. fi
  234. if is_serverip6route_set "${old_server_ip6}"; then
  235. echo "Unset IPv6 server route"
  236. unset_serverip6route "${old_server_ip6}" "${old_ip6_gw}" "${old_wired_device}"
  237. fi
  238. if is_openvpn_running; then
  239. echo "Stop openvpn"
  240. stop_openvpn
  241. i=0; true && while [ $? -eq 0 ]; do
  242. sleep 1 && (( i++ ))
  243. [ ${i} -gt 20 ] && exit 1
  244. ip link show dev tun0 &> /dev/null
  245. done
  246. fi
  247. # Fix configuration
  248. if has_hotspot_app && is_hotspot_knowme; then
  249. ynh-hotspot start
  250. fi
  251. ;;
  252. restart)
  253. $0 stop
  254. $0 start
  255. ;;
  256. status)
  257. exitcode=0
  258. if [ "${ynh_service_enabled}" -eq 0 ]; then
  259. echo "[ERR] VPN Client Service disabled"
  260. exitcode=1
  261. fi
  262. echo "[INFO] Autodetected internet interface: ${new_wired_device} (last start: ${old_wired_device})"
  263. echo "[INFO] Autodetected IPv6 address for the VPN server: ${new_server_ip6} (last start: ${old_server_ip6})"
  264. if has_ip6delegatedprefix; then
  265. echo "[INFO] IPv6 delegated prefix found"
  266. echo "[INFO] IPv6 address computed from the delegated prefix: ${ynh_ip6_addr}"
  267. if ! has_hotspot_app; then
  268. echo "[INFO] No Hotspot app detected"
  269. if is_ip6addr_set; then
  270. echo "[OK] IPv6 address correctly set"
  271. else
  272. echo "[ERR] No IPv6 address set"
  273. exitcode=1
  274. fi
  275. else
  276. echo "[INFO] Hotspot app detected"
  277. echo "[INFO] No IPv6 address to set"
  278. fi
  279. else
  280. echo "[INFO] No IPv6 delegated prefix found"
  281. fi
  282. if has_nativeip6; then
  283. echo "[INFO] Native IPv6 detected"
  284. echo "[INFO] Autodetected native IPv6 gateway: ${new_ip6_gw} (last start: ${old_ip6_gw})"
  285. if is_serverip6route_set "${new_server_ip6}"; then
  286. echo "[OK] IPv6 server route correctly set"
  287. else
  288. echo "[ERR] No IPv6 server route set"
  289. exitcode=1
  290. fi
  291. else
  292. echo "[INFO] No native IPv6 detected"
  293. echo "[INFO] No IPv6 server route to set"
  294. fi
  295. if is_openvpn_running; then
  296. echo "[OK] Openvpn is running"
  297. else
  298. echo "[ERR] Openvpn is not running"
  299. exitcode=1
  300. fi
  301. exit ${exitcode}
  302. ;;
  303. *)
  304. echo "Usage: $0 {start|stop|litestop|restart|status}"
  305. exit 1
  306. ;;
  307. esac
  308. exit 0