init_ynh-vpnclient 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #!/bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides: ynh-vpnclient
  4. # Required-Start: $network $remote_fs $syslog $all
  5. # Required-Stop: $network $remote_fs $syslog
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: Start VPN client.
  9. # Description: Start VPN client.
  10. ### END INIT INFO
  11. # Functions
  12. ## State functions
  13. has_nativeip6() {
  14. ip -6 route | grep -q default\ via
  15. }
  16. has_ip6delegatedprefix() {
  17. [ "${ynh_ip6_addr}" != none ]
  18. }
  19. has_hotspot_app() {
  20. [ -e /tmp/.ynh-hotspot-started ]
  21. }
  22. is_hotspot_knowme() {
  23. value=$(yunohost app setting hotspot vpnclient)
  24. if [[ "${value}" =~ "An instance is already running" ]]; then
  25. echo "${value}" >&2
  26. exit 1
  27. fi
  28. [ "${value}" == yes ]
  29. }
  30. is_ip6addr_set() {
  31. ip address show dev tun0 2> /dev/null | grep -q "${ynh_ip6_addr}/128"
  32. }
  33. is_serverip6route_set() {
  34. server_ip6=${1}
  35. if [ -z "${server_ip6}" ]; then
  36. false
  37. else
  38. ip -6 route | grep -q "${server_ip6}/"
  39. fi
  40. }
  41. is_openvpn_running() {
  42. service openvpn status client &> /dev/null
  43. }
  44. is_running() {
  45. ((has_nativeip6 && is_serverip6route_set "${new_server_ip6}") || ! has_nativeip6)\
  46. && ((! has_hotspot_app && has_ip6delegatedprefix && is_ip6addr_set) || has_hotspot_app || ! has_ip6delegatedprefix)\
  47. && is_openvpn_running
  48. }
  49. ## Setters
  50. set_ip6addr() {
  51. ip address add "${ynh_ip6_addr}/128" dev tun0
  52. }
  53. set_serverip6route() {
  54. server_ip6=${1}
  55. ip6_gw=${2}
  56. wired_device=${3}
  57. ip route add "${server_ip6}/128" via "${ip6_gw}" dev "${wired_device}"
  58. }
  59. start_openvpn() {
  60. ip6_gw=${1}
  61. server_ip6=${2}
  62. if [ ! -z "${ip6_gw}" -a ! -z "${server_ip6}" ]; then
  63. proto=udp6
  64. [ "${ynh_server_proto}" == tcp ] && proto=tcp6-client
  65. else
  66. proto=udp
  67. [ "${ynh_server_proto}" == tcp ] && proto=tcp-client
  68. fi
  69. cp /etc/openvpn/client.conf{.tpl,}
  70. sed "s|<TPL:SERVER_NAME>|${ynh_server_name}|g" -i /etc/openvpn/client.conf
  71. sed "s|<TPL:SERVER_PORT>|${ynh_server_port}|g" -i /etc/openvpn/client.conf
  72. sed "s|<TPL:PROTO>|${proto}|g" -i /etc/openvpn/client.conf
  73. if [ -e /etc/openvpn/keys/user.key ]; then
  74. sed 's|^<TPL:CERT_COMMENT>||' -i /etc/openvpn/client.conf
  75. else
  76. sed 's|^<TPL:CERT_COMMENT>|;|' -i /etc/openvpn/client.conf
  77. fi
  78. if [[ "${proto}" =~ udp ]]; then
  79. sed 's|^<TPL:UDP_COMMENT>||' -i /etc/openvpn/client.conf
  80. else
  81. sed 's|^<TPL:UDP_COMMENT>|;|' -i /etc/openvpn/client.conf
  82. fi
  83. if [ -z "${ynh_login_user}" ]; then
  84. sed 's|^<TPL:LOGIN_COMMENT>|;|' -i /etc/openvpn/client.conf
  85. else
  86. sed 's|^<TPL:LOGIN_COMMENT>||' -i /etc/openvpn/client.conf
  87. fi
  88. service openvpn start client
  89. }
  90. ## Unsetters
  91. unset_ip6addr() {
  92. ip address delete "${ynh_ip6_addr}/128" dev tun0
  93. }
  94. unset_serverip6route() {
  95. server_ip6=${1}
  96. ip6_gw=${2}
  97. wired_device=${3}
  98. ip route delete "${server_ip6}/128" via "${ip6_gw}" dev "${wired_device}"
  99. }
  100. stop_openvpn() {
  101. service openvpn stop
  102. }
  103. ## Tools
  104. moulinette_get() {
  105. var=${1}
  106. value=$(yunohost app setting vpnclient "${var}")
  107. if [[ "${value}" =~ "An instance is already running" ]]; then
  108. echo "${value}" >&2
  109. exit 1
  110. fi
  111. echo "${value}"
  112. }
  113. moulinette_set() {
  114. var=${1}
  115. value=${2}
  116. msg=$(yunohost app setting vpnclient "${var}" -v "${value}")
  117. if [ ! $? -eq 0 ]; then
  118. echo "${msg}" >&2
  119. exit 1
  120. fi
  121. }
  122. # Restart php5-fpm at the first start (it needs to be restarted after the slapd start)
  123. if [ ! -e /tmp/.ynh-vpnclient-boot ]; then
  124. touch /tmp/.ynh-vpnclient-boot
  125. service php5-fpm restart
  126. fi
  127. # Check configuration consistency
  128. if [[ ! "${1}" =~ stop ]]; then
  129. exitcode=0
  130. if [ ! -e /etc/openvpn/keys/ca-server.crt ]; then
  131. echo "[WARN] You need a CA server (you can add it through the web admin)"
  132. exitcode=1
  133. fi
  134. empty=$(find /etc/openvpn/keys/ -empty -name credentials &> /dev/null | wc -l)
  135. if [ "${empty}" -gt 0 -a ! -e /etc/openvpn/keys/user.key ]; then
  136. echo "[WARN] You need either a client certificate, either a username, or both (you can add one through the web admin)"
  137. exitcode=1
  138. fi
  139. [ "${exitcode}" -ne 0 ] && exit ${exitcode}
  140. fi
  141. # Variables
  142. echo -n "Retrieving Yunohost settings... "
  143. ynh_server_name=$(moulinette_get server_name)
  144. ynh_server_port=$(moulinette_get server_port)
  145. ynh_server_proto=$(moulinette_get server_proto)
  146. ynh_ip6_addr=$(moulinette_get ip6_addr)
  147. ynh_login_user=$(moulinette_get login_user)
  148. old_ip6_gw=$(moulinette_get ip6_gw)
  149. old_wired_device=$(moulinette_get wired_device)
  150. old_server_ip6=$(moulinette_get server_ip6)
  151. new_ip6_gw=$(ip -6 route | grep default\ via | awk '{ print $3 }')
  152. new_wired_device=$(ip route | awk '/default via/ { print $NF; }')
  153. new_server_ip6=$(host "${ynh_server_name}" | awk '/IPv6/ { print $NF; }')
  154. if [ -z "${new_server_ip6}" ]; then
  155. new_server_ip6=$(host "${ynh_server_name}" 80.67.188.188 | awk '/IPv6/ { print $NF; }')
  156. fi
  157. echo "OK"
  158. # Script
  159. case "${1}" in
  160. start)
  161. if is_running; then
  162. echo "Already started"
  163. else
  164. echo "[vpnclient] Starting..."
  165. touch /tmp/.ynh-vpnclient-started
  166. # Run openvpn
  167. if ! is_openvpn_running; then
  168. echo "Run openvpn"
  169. start_openvpn "${new_ip6_gw}" "${new_server_ip6}"
  170. if [ ! $? -eq 0 ]; then
  171. exit 1
  172. fi
  173. i=0; false || while [ $? -ne 0 ]; do
  174. sleep 1 && (( i++ ))
  175. [ ${i} -gt 20 ] && stop_openvpn
  176. [ ${i} -gt 20 ] && exit 1
  177. ip link show dev tun0 &> /dev/null
  178. done
  179. fi
  180. # Check old state of the server ipv6 route
  181. if [ ! -z "${old_server_ip6}" -a ! -z "${old_ip6_gw}" -a ! -z "${old_wired_device}"\
  182. -a \( "${new_server_ip6}" != "${old_server_ip6}" -o "${new_ip6_gw}" != "${old_ip6_gw}"\
  183. -o "${new_wired_device}" != "${old_wired_device}" \) ]\
  184. && is_serverip6route_set "${old_server_ip6}"; then
  185. unset_serverip6route "${old_server_ip6}" "${old_ip6_gw}" "${old_wired_device}"
  186. fi
  187. # Set the new server ipv6 route
  188. if has_nativeip6 && ! is_serverip6route_set "${new_server_ip6}"; then
  189. echo "Set IPv6 server route"
  190. set_serverip6route "${new_server_ip6}" "${new_ip6_gw}" "${new_wired_device}"
  191. fi
  192. # Set the ipv6 address
  193. if ! has_hotspot_app && has_ip6delegatedprefix && ! is_ip6addr_set; then
  194. echo "Set IPv6 address"
  195. set_ip6addr
  196. fi
  197. # Update dynamic settings
  198. moulinette_set server_ip6 "${new_server_ip6}"
  199. moulinette_set ip6_gw "${new_ip6_gw}"
  200. moulinette_set wired_device "${new_wired_device}"
  201. # Restart hotspot if needed
  202. if has_hotspot_app && ! is_hotspot_knowme; then
  203. service ynh-hotspot start
  204. fi
  205. fi
  206. ;;
  207. stop)
  208. echo "[vpnclient] Stopping..."
  209. rm /tmp/.ynh-vpnclient-started
  210. if ! has_hotspot_app && has_ip6delegatedprefix && is_ip6addr_set; then
  211. echo "Unset IPv6 address"
  212. unset_ip6addr
  213. fi
  214. if is_serverip6route_set "${old_server_ip6}"; then
  215. echo "Unset IPv6 server route"
  216. unset_serverip6route "${old_server_ip6}" "${old_ip6_gw}" "${old_wired_device}"
  217. fi
  218. if is_openvpn_running; then
  219. echo "Stop openvpn"
  220. stop_openvpn
  221. fi
  222. if has_hotspot_app && ! is_hotspot_knowme; then
  223. service ynh-hotspot start
  224. fi
  225. ;;
  226. status)
  227. exitcode=0
  228. if has_ip6delegatedprefix; then
  229. echo "[INFO] IPv6 delegated prefix found"
  230. if ! has_hotspot_app; then
  231. echo "[INFO] No Hotspot app detected"
  232. if is_ip6addr_set; then
  233. echo "[OK] IPv6 address correctly set"
  234. else
  235. echo "[ERR] No IPv6 address set"
  236. exitcode=1
  237. fi
  238. else
  239. echo "[INFO] Hotspot app detected"
  240. echo "[INFO] No IPv6 address to set"
  241. fi
  242. else
  243. echo "[INFO] No IPv6 delegated prefix found"
  244. fi
  245. if has_nativeip6; then
  246. echo "[INFO] Native IPv6 detected"
  247. if is_serverip6route_set "${new_server_ip6}"; then
  248. echo "[OK] IPv6 server route correctly set"
  249. else
  250. echo "[ERR] No IPv6 server route set"
  251. exitcode=1
  252. fi
  253. else
  254. echo "[INFO] No native IPv6 detected"
  255. echo "[INFO] No IPv6 server route to set"
  256. fi
  257. if is_openvpn_running; then
  258. echo "[OK] Openvpn is running"
  259. else
  260. echo "[ERR] Openvpn is not running"
  261. exitcode=1
  262. fi
  263. exit ${exitcode}
  264. ;;
  265. *)
  266. echo "Usage: $0 {start|stop|litestop|status}"
  267. exit 1
  268. ;;
  269. esac
  270. exit 0