ynh-vpnclient 9.8 KB

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