ynh-vpnclient 13 KB

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