ynh-vpnclient 13 KB

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