init_ynh-vpnclient 10 KB

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