init_ynh-vpnclient 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. # 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. service php5-fpm restart
  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_server_name=$(moulinette_get server_name)
  160. ynh_server_port=$(moulinette_get server_port)
  161. ynh_server_proto=$(moulinette_get server_proto)
  162. ynh_ip6_addr=$(moulinette_get ip6_addr)
  163. ynh_login_user=$(moulinette_get login_user)
  164. old_ip6_gw=$(moulinette_get ip6_gw)
  165. old_wired_device=$(moulinette_get wired_device)
  166. old_server_ip6=$(moulinette_get server_ip6)
  167. new_ip6_gw=$(ip -6 route | grep default\ via | awk '{ print $3 }')
  168. new_wired_device=$(ip route | awk '/default via/ { print $NF; }')
  169. new_server_ip6=$(host "${ynh_server_name}" | awk '/IPv6/ { print $NF; }')
  170. if [ -z "${new_server_ip6}" ]; then
  171. new_server_ip6=$(host "${ynh_server_name}" 80.67.188.188 | awk '/IPv6/ { print $NF; }')
  172. fi
  173. echo "OK"
  174. # Script
  175. case "${1}" in
  176. start)
  177. if is_running; then
  178. echo "Already started"
  179. else
  180. echo "[vpnclient] Starting..."
  181. touch /tmp/.ynh-vpnclient-started
  182. # Run openvpn
  183. if ! is_openvpn_running; then
  184. echo "Run openvpn"
  185. start_openvpn "${new_ip6_gw}" "${new_server_ip6}"
  186. if [ ! $? -eq 0 ]; then
  187. exit 1
  188. fi
  189. i=0; false || while [ $? -ne 0 ]; do
  190. sleep 1 && (( i++ ))
  191. [ ${i} -gt 20 ] && stop_openvpn
  192. [ ${i} -gt 20 ] && exit 1
  193. ip link show dev tun0 &> /dev/null
  194. done
  195. fi
  196. # Check old state of the server ipv6 route
  197. if [ ! -z "${old_server_ip6}" -a ! -z "${old_ip6_gw}" -a ! -z "${old_wired_device}"\
  198. -a \( "${new_server_ip6}" != "${old_server_ip6}" -o "${new_ip6_gw}" != "${old_ip6_gw}"\
  199. -o "${new_wired_device}" != "${old_wired_device}" \) ]\
  200. && is_serverip6route_set "${old_server_ip6}"; then
  201. unset_serverip6route "${old_server_ip6}" "${old_ip6_gw}" "${old_wired_device}"
  202. fi
  203. # Set the new server ipv6 route
  204. if has_nativeip6 && ! is_serverip6route_set "${new_server_ip6}"; then
  205. echo "Set IPv6 server route"
  206. set_serverip6route "${new_server_ip6}" "${new_ip6_gw}" "${new_wired_device}"
  207. fi
  208. # Set the ipv6 address
  209. if ! has_hotspot_app && has_ip6delegatedprefix && ! is_ip6addr_set; then
  210. echo "Set IPv6 address"
  211. set_ip6addr
  212. fi
  213. # Update dynamic settings
  214. moulinette_set server_ip6 "${new_server_ip6}"
  215. moulinette_set ip6_gw "${new_ip6_gw}"
  216. moulinette_set wired_device "${new_wired_device}"
  217. # Restart hotspot if needed
  218. if has_hotspot_app && ! is_hotspot_knowme; then
  219. service ynh-hotspot start
  220. fi
  221. fi
  222. ;;
  223. stop)
  224. echo "[vpnclient] Stopping..."
  225. rm -f /tmp/.ynh-vpnclient-started
  226. if ! has_hotspot_app && has_ip6delegatedprefix && is_ip6addr_set; then
  227. echo "Unset IPv6 address"
  228. unset_ip6addr
  229. fi
  230. if is_serverip6route_set "${old_server_ip6}"; then
  231. echo "Unset IPv6 server route"
  232. unset_serverip6route "${old_server_ip6}" "${old_ip6_gw}" "${old_wired_device}"
  233. fi
  234. if is_openvpn_running; then
  235. echo "Stop openvpn"
  236. stop_openvpn
  237. fi
  238. if has_hotspot_app && ! is_hotspot_knowme; then
  239. service ynh-hotspot start
  240. fi
  241. ;;
  242. status)
  243. exitcode=0
  244. echo "[INFO] Autodetected internet interface: ${new_wired_device} (last start: ${old_wired_device})"
  245. echo "[INFO] Autodetected IPv6 address for the VPN server: ${new_server_ip6} (last start: ${old_server_ip6})"
  246. if has_ip6delegatedprefix; then
  247. echo "[INFO] IPv6 delegated prefix found"
  248. echo "[INFO] IPv6 address computed from the delegated prefix: ${ynh_ip6_addr}"
  249. if ! has_hotspot_app; then
  250. echo "[INFO] No Hotspot app detected"
  251. if is_ip6addr_set; then
  252. echo "[OK] IPv6 address correctly set"
  253. else
  254. echo "[ERR] No IPv6 address set"
  255. exitcode=1
  256. fi
  257. else
  258. echo "[INFO] Hotspot app detected"
  259. echo "[INFO] No IPv6 address to set"
  260. fi
  261. else
  262. echo "[INFO] No IPv6 delegated prefix found"
  263. fi
  264. if has_nativeip6; then
  265. echo "[INFO] Native IPv6 detected"
  266. echo "[INFO] Autodetected native IPv6 gateway: ${new_ip6_gw} (last start: ${old_ip6_gw})"
  267. if is_serverip6route_set "${new_server_ip6}"; then
  268. echo "[OK] IPv6 server route correctly set"
  269. else
  270. echo "[ERR] No IPv6 server route set"
  271. exitcode=1
  272. fi
  273. else
  274. echo "[INFO] No native IPv6 detected"
  275. echo "[INFO] No IPv6 server route to set"
  276. fi
  277. if is_openvpn_running; then
  278. echo "[OK] Openvpn is running"
  279. else
  280. echo "[ERR] Openvpn is not running"
  281. exitcode=1
  282. fi
  283. exit ${exitcode}
  284. ;;
  285. *)
  286. echo "Usage: $0 {start|stop|litestop|status}"
  287. exit 1
  288. ;;
  289. esac
  290. exit 0