ynh-vpnclient 11 KB

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