ynh-vpnclient 12 KB

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