ynh-vpnclient 13 KB

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