ynh-vpnclient 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. ###################################################################################
  19. # Logging helpers #
  20. ###################################################################################
  21. LOGFILE="/var/log/ynh-vpnclient.log"
  22. touch $LOGFILE
  23. chown root:root $LOGFILE
  24. chmod 600 $LOGFILE
  25. function success()
  26. {
  27. echo "[ OK ] $1" | tee -a $LOGFILE
  28. }
  29. function info()
  30. {
  31. echo "[INFO] $1" | tee -a $LOGFILE
  32. }
  33. function warn()
  34. {
  35. echo "[WARN] $1" | tee -a $LOGFILE >&2
  36. }
  37. function error()
  38. {
  39. echo "[FAIL] $1" | tee -a $LOGFILE >&2
  40. }
  41. function critical()
  42. {
  43. echo "[CRIT] $1" | tee -a $LOGFILE >&2
  44. exit 1
  45. }
  46. ###################################################################################
  47. # IPv6 and route config stuff #
  48. ###################################################################################
  49. has_nativeip6() {
  50. ip -6 route | grep -q "default via"
  51. }
  52. has_ip6delegatedprefix() {
  53. [ "${ynh_ip6_addr}" != none ] && [ "${ynh_ip6_addr}" != "" ]
  54. }
  55. is_ip6addr_set() {
  56. ip address show dev tun0 2> /dev/null | grep -q "${ynh_ip6_addr}/"
  57. }
  58. set_ip6addr() {
  59. info "Adding IPv6 from VPN configuration"
  60. ip address add "${ynh_ip6_addr}/128" dev tun0
  61. }
  62. unset_ip6addr() {
  63. info "Removing IPv6 from VPN configuration"
  64. ip address delete "${ynh_ip6_addr}/128" dev tun0
  65. }
  66. #
  67. # Server IPv6 route
  68. #
  69. is_serverip6route_set() {
  70. server_ip6s=${1}
  71. if [ -z "${server_ip6}" ]; then
  72. false
  73. else
  74. for server_ip6 in ${server_ip6s};
  75. do
  76. ip -6 route | grep -q "${server_ip6}/" || return 1
  77. done
  78. fi
  79. }
  80. set_serverip6route() {
  81. server_ip6s=${1}
  82. ip6_gw=${2}
  83. wired_device=${3}
  84. info "Adding IPv6 server route"
  85. for server_ip6 in ${server_ip6s};
  86. do
  87. ip route add "${server_ip6}/128" via "${ip6_gw}" dev "${wired_device}"
  88. done
  89. }
  90. unset_serverip6route() {
  91. server_ip6s=${1}
  92. ip6_gw=${2}
  93. wired_device=${3}
  94. info "Removing IPv6 server route"
  95. for server_ip6 in ${server_ip6s};
  96. do
  97. ip route delete "${server_ip6}/128" via "${ip6_gw}" dev "${wired_device}"
  98. done
  99. }
  100. ###################################################################################
  101. # Hotspot app #
  102. ###################################################################################
  103. has_hotspot_app() {
  104. [ -e /tmp/.ynh-hotspot-started ]
  105. }
  106. is_hotspot_knowme() {
  107. hotspot_vpnclient=$(ynh_setting_get hotspot vpnclient)
  108. [ "${hotspot_vpnclient}" == yes ]
  109. }
  110. ###################################################################################
  111. # DNS rules #
  112. ###################################################################################
  113. is_dns_set() {
  114. if [[ "$ynh_dns_method" == "custom" ]]
  115. then
  116. current_dns=$(grep -o -P '\s*nameserver\s+\K[abcdefabcdef\d.:]+' /etc/resolv.dnsmasq.conf | sort | uniq)
  117. wanted_dns=$(echo "${ynh_dns}" | sed 's/,/\n/g' | sort | uniq)
  118. [ -e /etc/dhcp/dhclient-exit-hooks.d/ynh-vpnclient ]\
  119. && [[ "$current_dns" == "$wanted_dns" ]]
  120. else
  121. true
  122. fi
  123. }
  124. set_dns() {
  125. info "Enforcing custom DNS resolvers from vpnclient"
  126. resolvconf=/etc/resolv.dnsmasq.conf
  127. cp -fa "${resolvconf}" "${resolvconf}.ynh"
  128. if [[ "$ynh_dns_method" == "custom" ]]
  129. then
  130. cat << EOF > /etc/dhcp/dhclient-exit-hooks.d/ynh-vpnclient
  131. echo "${ynh_dns}" | sed 's/,/\n/g' | sort | uniq | sed 's/^/nameserver /g' > ${resolvconf}
  132. EOF
  133. bash /etc/dhcp/dhclient-exit-hooks.d/ynh-vpnclient
  134. fi
  135. }
  136. unset_dns() {
  137. resolvconf=/etc/resolv.dnsmasq.conf
  138. info "Removing custom DNS resolvers from vpnclient"
  139. rm -f /etc/dhcp/dhclient-exit-hooks.d/ynh-vpnclient
  140. [ -e "${resolvconf}.ynh" ] && mv "${resolvconf}.ynh" "${resolvconf}"
  141. # FIXME : this situation happened to a user ...
  142. # We could try to force regen the dns conf
  143. # (though for now it's tightly coupled to dnsmasq)
  144. grep -q "^nameserver\s" "${resolvconf}" || error "${resolvconf} does not have any nameserver line !?"
  145. }
  146. ###################################################################################
  147. # Firewall rules management #
  148. ###################################################################################
  149. is_firewall_set() {
  150. wired_device=$(ip route | awk '/default via/ { print $5; }')
  151. ip6tables -w -nvL OUTPUT | grep vpnclient_out | grep -q "${wired_device}"\
  152. && iptables -w -nvL OUTPUT | grep vpnclient_out | grep -q "${wired_device}"
  153. }
  154. set_firewall() {
  155. info "Adding vpnclient custom rules to the firewall"
  156. cp /etc/yunohost/hooks.d/{90-vpnclient.tpl,post_iptable_rules/90-vpnclient}
  157. info "Restarting yunohost firewall..."
  158. yunohost firewall reload >/dev/null && success "Firewall restarted!"
  159. }
  160. unset_firewall() {
  161. info "Cleaning vpnclient custom rules from the firewall"
  162. rm -f /etc/yunohost/hooks.d/post_iptable_rules/90-vpnclient
  163. info "Restarting yunohost firewall..."
  164. yunohost firewall reload >/dev/null && success "Firewall restarted!"
  165. }
  166. ###################################################################################
  167. # Time sync #
  168. ###################################################################################
  169. sync_time() {
  170. info "Now synchronizing time using ntp..."
  171. systemctl stop ntp
  172. timeout 20 ntpd -qg &> /dev/null
  173. # Some networks drop ntp port (udp 123).
  174. # Try to get the date with an http request on the internetcube web site
  175. if [ $? -ne 0 ]; then
  176. info "ntp synchronization failed, falling back to curl method"
  177. http_date=$(curl --max-time 5 -sD - labriqueinter.net | grep '^Date:' | cut -d' ' -f3-6)
  178. http_date_seconds=$(date -d "${http_date}" +%s)
  179. curr_date_seconds=$(date +%s)
  180. # Set the new date if it's greater than the current date
  181. # So it does if 1970 year or if old fake-hwclock date is used
  182. if [ $http_date_seconds -ge $curr_date_seconds ]; then
  183. date -s "${http_date}"
  184. fi
  185. fi
  186. systemctl start ntp
  187. }
  188. ###################################################################################
  189. # OpenVPN client start/stop procedures #
  190. ###################################################################################
  191. is_openvpn_running() {
  192. systemctl is-active openvpn@client.service &> /dev/null
  193. }
  194. start_openvpn() {
  195. # Unset firewall to let DNS and NTP resolution works
  196. # Firewall is reset after vpn is mounted (more details on #1016)
  197. unset_firewall
  198. sync_time
  199. info "Now actually starting OpenVPN client..."
  200. if systemctl start openvpn@client.service
  201. then
  202. info "OpenVPN client started ... waiting for tun0 interface to show up"
  203. else
  204. tail -n 20 /var/log/openvpn-client.log | tee -a $LOGFILE
  205. critical "Failed to start OpenVPN :/"
  206. fi
  207. for attempt in $(seq 0 20)
  208. do
  209. sleep 1
  210. if ip link show dev tun0 &> /dev/null
  211. then
  212. success "tun0 interface is up!"
  213. return 0
  214. fi
  215. done
  216. error "Tun0 interface did not show up ... most likely an issue happening in OpenVPN client ... below is an extract of the log that might be relevant to pinpoint the issue"
  217. tail -n 20 /var/log/openvpn-client.log | tee -a $LOGFILE
  218. stop_openvpn
  219. critical "Failed to start OpenVPN client : tun0 interface did not show up"
  220. }
  221. stop_openvpn() {
  222. # FIXME : isn't openvpn@client ? (idk)
  223. info "Stopping OpenVPN service"
  224. systemctl stop openvpn.service
  225. for attempt in $(seq 0 20)
  226. do
  227. if ip link show dev tun0 &> /dev/null
  228. then
  229. info "(Waiting for tun0 to disappear if it was up)"
  230. sleep 1
  231. fi
  232. done
  233. }
  234. ###################################################################################
  235. # Yunohost settings interface #
  236. ###################################################################################
  237. ynh_setting_get() {
  238. app=${1}
  239. setting=${2}
  240. grep "^${setting}:" "/etc/yunohost/apps/${app}/settings.yml" | sed s/^[^:]\\+:\\s*[\"\']\\?// | sed s/\\s*[\"\']\$//
  241. # '"
  242. }
  243. ynh_setting_set() {
  244. app=${1}
  245. setting=${2}
  246. value=${3}
  247. yunohost app setting "${app}" "${setting}" -v "${value}"
  248. }
  249. ###################################################################################
  250. # The actual ynh vpnclient management thing #
  251. ###################################################################################
  252. is_running() {
  253. ((has_nativeip6 && is_serverip6route_set "${new_server_ip6}") || ! has_nativeip6)\
  254. && ((! has_hotspot_app && has_ip6delegatedprefix && is_ip6addr_set) || has_hotspot_app || ! has_ip6delegatedprefix)\
  255. && is_dns_set && is_firewall_set && is_openvpn_running
  256. }
  257. if [ "$1" != restart ]; then
  258. # Check configuration consistency
  259. if [[ ! "${1}" =~ stop ]]; then
  260. if [ ! -e /etc/openvpn/keys/ca-server.crt ]; then
  261. critical "You need a CA server (you can add it through the web admin)"
  262. fi
  263. empty=$(find /etc/openvpn/keys/ -empty -name credentials &> /dev/null | wc -l)
  264. if [ "${empty}" -gt 0 -a ! -e /etc/openvpn/keys/user.key ]; then
  265. critical "You need either a client certificate, either a username, or both (you can add one through the web admin)"
  266. fi
  267. fi
  268. # Variables
  269. info "Retrieving Yunohost settings... "
  270. ynh_service_enabled=$(ynh_setting_get vpnclient service_enabled)
  271. ynh_ip6_addr=$(ynh_setting_get vpnclient ip6_addr)
  272. ynh_dns_method=$(ynh_setting_get vpnclient dns_method)
  273. ynh_dns=$(ynh_setting_get vpnclient nameservers)
  274. old_ip6_gw=$(ynh_setting_get vpnclient ip6_gw)
  275. old_wired_device=$(ynh_setting_get vpnclient wired_device)
  276. old_server_ip6=$(ynh_setting_get vpnclient server_ip6)
  277. new_ip6_gw=$(ip -6 route | awk '/default via/ { print $3 }')
  278. new_wired_device=$(ip route | awk '/default via/ { print $5; }')
  279. ynh_server_names=$(grep -o -P '^\s*remote\s+\K([^\s]+)' /etc/openvpn/client.conf | sort | uniq)
  280. new_server_ip6=$(dig AAAA +short $ynh_server_names @127.0.0.1 | grep -v '\.$' | grep -v "timed out" | sort | uniq)
  281. for i in $ynh_server_names; do
  282. if [[ "${i}" =~ : ]] && [[ ! "$new_server_ip6" == *"${i}"* ]] ; then
  283. new_server_ip6+=" ${i}"
  284. fi
  285. done
  286. success "Settings retrieved"
  287. fi
  288. ###################################################################################
  289. # Start / stop / restart / status handling #
  290. ###################################################################################
  291. case "${1}" in
  292. # ########## #
  293. # Starting #
  294. # ########## #
  295. start)
  296. if is_running; then
  297. info "Service is already running"
  298. exit 0
  299. elif [ "${ynh_service_enabled}" -eq 0 ]; then
  300. warn "Service is disabled, not starting it"
  301. exit 0
  302. fi
  303. if [ ! -e /etc/openvpn/keys/user.crt ] && ! cat /etc/openvpn/keys/user.crt | openssl x509 -noout -checkend 0 >/dev/null
  304. then
  305. critical "Failed to start OpenVPN client : user certificate expired"
  306. fi
  307. info "[vpnclient] Starting..."
  308. touch /tmp/.ynh-vpnclient-started
  309. # Run openvpn
  310. if is_openvpn_running;
  311. then
  312. info "(openvpn is already running)"
  313. else
  314. start_openvpn
  315. fi
  316. # Check old state of the server ipv6 route
  317. if [ ! -z "${old_server_ip6}" -a ! -z "${old_ip6_gw}" -a ! -z "${old_wired_device}"\
  318. -a \( "${new_server_ip6}" != "${old_server_ip6}" -o "${new_ip6_gw}" != "${old_ip6_gw}"\
  319. -o "${new_wired_device}" != "${old_wired_device}" \) ]\
  320. && is_serverip6route_set "${old_server_ip6}"
  321. then
  322. unset_serverip6route "${old_server_ip6}" "${old_ip6_gw}" "${old_wired_device}"
  323. fi
  324. # Set the new server ipv6 route
  325. if has_nativeip6 && ! is_serverip6route_set "${new_server_ip6}"
  326. then
  327. set_serverip6route "${new_server_ip6}" "${new_ip6_gw}" "${new_wired_device}"
  328. fi
  329. # Set the ipv6 address
  330. if ! has_hotspot_app && has_ip6delegatedprefix && ! is_ip6addr_set
  331. then
  332. set_ip6addr
  333. fi
  334. # Set host DNS resolvers
  335. if ! is_dns_set
  336. then
  337. set_dns
  338. fi
  339. # Set ipv6/ipv4 firewall
  340. if ! is_firewall_set
  341. then
  342. set_firewall
  343. fi
  344. # Update dynamic settings
  345. info "Saving settings..."
  346. ynh_setting_set vpnclient server_ip6 "${new_server_ip6}"
  347. ynh_setting_set vpnclient ip6_gw "${new_ip6_gw}"
  348. ynh_setting_set vpnclient wired_device "${new_wired_device}"
  349. # Fix configuration
  350. if has_hotspot_app && ! is_hotspot_knowme; then
  351. info "Now starting the hotspot"
  352. ynh-hotspot start
  353. fi
  354. ping -c1 -w5 debian.org >/dev/null
  355. ipv4=$(ping -w3 -c1 ip.yunohost.org >/dev/null 2>&1 && curl --max-time 5 https://ip.yunohost.org --silent)
  356. ipv6=$(ping -w3 -c1 ip6.yunohost.org >/dev/null 2>&1 && curl --max-time 5 https://ip6.yunohost.org --silent)
  357. info "Validating that VPN is up and the server is connected to internet..."
  358. if ip route get 1.2.3.4 | grep -q tun0; then
  359. if ping -c1 -w5 debian.org >/dev/null; then
  360. success "YunoHost VPN client started!"
  361. else
  362. critical "The VPN is up but debian.org cannot be reached, indicating that something is probably misconfigured/blocked."
  363. fi
  364. else
  365. critical "IPv4 routes are misconfigured !?"
  366. fi
  367. ;;
  368. # ########## #
  369. # Stopping #
  370. # ########## #
  371. stop)
  372. info "[vpnclient] Stopping..."
  373. rm -f /tmp/.ynh-vpnclient-started
  374. if ! has_hotspot_app && has_ip6delegatedprefix && is_ip6addr_set; then
  375. unset_ip6addr
  376. fi
  377. if is_serverip6route_set "${old_server_ip6}"; then
  378. unset_serverip6route "${old_server_ip6}" "${old_ip6_gw}" "${old_wired_device}"
  379. fi
  380. is_firewall_set && unset_firewall
  381. is_dns_set && unset_dns
  382. is_openvpn_running && stop_openvpn
  383. # Fix configuration
  384. if has_hotspot_app && is_hotspot_knowme; then
  385. info "Now starting the hotspot"
  386. ynh-hotspot start
  387. fi
  388. ;;
  389. # ########## #
  390. # Restart #
  391. # ########## #
  392. restart)
  393. $0 stop
  394. $0 start
  395. ;;
  396. # ########## #
  397. # Status #
  398. # ########## #
  399. status)
  400. exitcode=0
  401. if [ "${ynh_service_enabled}" -eq 0 ]; then
  402. error "VPN Client Service disabled"
  403. exitcode=1
  404. fi
  405. info "Autodetected internet interface: ${new_wired_device} (last start: ${old_wired_device})"
  406. info "Autodetected IPv6 address for the VPN server: ${new_server_ip6} (last start: ${old_server_ip6})"
  407. if has_ip6delegatedprefix; then
  408. info "IPv6 delegated prefix found"
  409. info "IPv6 address computed from the delegated prefix: ${ynh_ip6_addr}"
  410. if ! has_hotspot_app; then
  411. info "No Hotspot app detected"
  412. if is_ip6addr_set; then
  413. success "IPv6 address correctly set"
  414. else
  415. error "No IPv6 address set"
  416. exitcode=1
  417. fi
  418. else
  419. info "Hotspot app detected"
  420. info "No IPv6 address to set"
  421. fi
  422. else
  423. info "No IPv6 delegated prefix found"
  424. fi
  425. if has_nativeip6; then
  426. info "Native IPv6 detected"
  427. info "Autodetected native IPv6 gateway: ${new_ip6_gw} (last start: ${old_ip6_gw})"
  428. if is_serverip6route_set "${new_server_ip6}"; then
  429. success "IPv6 server route correctly set"
  430. else
  431. error "No IPv6 server route set"
  432. exitcode=1
  433. fi
  434. else
  435. info "No native IPv6 detected"
  436. info "No IPv6 server route to set"
  437. fi
  438. if is_firewall_set; then
  439. success "IPv6/IPv4 firewall set"
  440. else
  441. info "No IPv6/IPv4 firewall set"
  442. exitcode=1
  443. fi
  444. if is_dns_set; then
  445. success "Host DNS correctly set"
  446. else
  447. error "No host DNS set"
  448. exitcode=1
  449. fi
  450. if is_openvpn_running; then
  451. success "Openvpn is running"
  452. else
  453. error "Openvpn is not running"
  454. exitcode=1
  455. fi
  456. exit ${exitcode}
  457. ;;
  458. # ########## #
  459. # Halp #
  460. # ########## #
  461. *)
  462. echo "Usage: $0 {start|stop|restart|status}"
  463. exit 1
  464. ;;
  465. esac
  466. exit 0