init_ynh-vpnclient 9.6 KB

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