init_ynh-hotspot 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. #!/bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides: ynh-hotspot
  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: Set prerequisites for wifi hotspot.
  9. # Description: Set prerequisites for wifi hotspot.
  10. ### END INIT INFO
  11. # Wifi Hotspot app for YunoHost
  12. # Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
  13. # Contribute at https://github.com/jvaubourg/hotspot_ynh
  14. #
  15. # This program is free software: you can redistribute it and/or modify
  16. # it under the terms of the GNU Affero General Public License as published by
  17. # the Free Software Foundation, either version 3 of the License, or
  18. # (at your option) any later version.
  19. #
  20. # This program is distributed in the hope that it will be useful,
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. # GNU Affero General Public License for more details.
  24. #
  25. # You should have received a copy of the GNU Affero General Public License
  26. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. # Functions
  28. ## State functions
  29. has_vpnclient_app() {
  30. [ -e /tmp/.ynh-vpnclient-started ]
  31. }
  32. has_ip6delegatedprefix() {
  33. [ "${ynh_ip6_net}" != none ]
  34. }
  35. is_nat_set() {
  36. internet_device=${1}
  37. iptables -nvt nat -L POSTROUTING | grep MASQUERADE | grep -q "${internet_device}"
  38. }
  39. is_ip4nataddr_set() {
  40. ip address show dev "${ynh_wifi_device}" 2> /dev/null | grep -q "${ynh_ip4_nat_prefix}.1/24"
  41. }
  42. is_ip6addr_set() {
  43. ip address show dev "${ynh_wifi_device}" 2> /dev/null | grep -q "${ynh_ip6_addr}/64"
  44. }
  45. is_forwarding_set() {
  46. ip6=$(sysctl net.ipv6.conf.all.forwarding | awk '{ print $NF; }')
  47. ip4=$(sysctl net.ipv4.conf.all.forwarding | awk '{ print $NF; }')
  48. [ "${ip6}" -eq 1 -a "${ip4}" -eq 1 ]
  49. }
  50. is_dhcpdv6_set() {
  51. [ -e /etc/dnsmasq.d/dhcpdv6.conf ]
  52. }
  53. is_dhcpdv4_set() {
  54. [ -e /etc/dnsmasq.d/dhcpdv4.conf ]
  55. }
  56. is_hostapd_running() {
  57. service hostapd status &> /dev/null
  58. if [ $? -eq 0 ]; then
  59. # If the wifi antenna was unplugged
  60. if ip link show dev "${ynh_wifi_device}" | grep -q DOWN; then
  61. service hostapd stop &> /dev/null
  62. return 1
  63. fi
  64. return 0
  65. fi
  66. return 1
  67. }
  68. is_dnsmasq_running() {
  69. service dnsmasq status &> /dev/null
  70. }
  71. is_running() {
  72. ( has_ip6delegatedprefix && is_ip6addr_set && is_dhcpdv6_set || ! has_ip6delegatedprefix )\
  73. && is_nat_set "${new_internet_device}" && is_ip4nataddr_set && is_forwarding_set && is_hostapd_running\
  74. && is_dhcpdv4_set && is_dnsmasq_running
  75. }
  76. ## Setters
  77. set_nat() {
  78. internet_device=${1}
  79. iptables -t nat -A POSTROUTING -o "${internet_device}" -j MASQUERADE
  80. }
  81. set_ip4nataddr() {
  82. ip address add "${ynh_ip4_nat_prefix}.1/24" dev "${ynh_wifi_device}"
  83. }
  84. set_ip6addr() {
  85. ip address delete "${ynh_ip6_addr}/64" dev tun0 &> /dev/null
  86. ip address add "${ynh_ip6_addr}/64" dev "${ynh_wifi_device}"
  87. }
  88. set_forwarding() {
  89. sysctl -w net.ipv6.conf.all.forwarding=1 > /dev/null
  90. sysctl -w net.ipv4.conf.all.forwarding=1 > /dev/null
  91. }
  92. set_dhcpd() {
  93. if has_ip6delegatedprefix; then
  94. cp /etc/dnsmasq.d.tpl/dhcpdv6.conf.tpl /etc/dnsmasq.d/dhcpdv6.conf
  95. sed "s|<TPL:WIFI_DEVICE>|${ynh_wifi_device}|g" -i /etc/dnsmasq.d/dhcpdv6.conf
  96. sed "s|<TPL:IP6_NET>|${ynh_ip6_net}|g" -i /etc/dnsmasq.d/dhcpdv6.conf
  97. sed "s|<TPL:IP6_DNS0>|${ynh_ip6_dns0}|g" -i /etc/dnsmasq.d/dhcpdv6.conf
  98. sed "s|<TPL:IP6_DNS1>|${ynh_ip6_dns1}|g" -i /etc/dnsmasq.d/dhcpdv6.conf
  99. fi
  100. cp /etc/dnsmasq.d.tpl/dhcpdv4.conf.tpl /etc/dnsmasq.d/dhcpdv4.conf
  101. sed "s|<TPL:IP4_DNS0>|${ynh_ip4_dns0}|g" -i /etc/dnsmasq.d/dhcpdv4.conf
  102. sed "s|<TPL:IP4_DNS1>|${ynh_ip4_dns1}|g" -i /etc/dnsmasq.d/dhcpdv4.conf
  103. sed "s|<TPL:WIFI_DEVICE>|${ynh_wifi_device}|g" -i /etc/dnsmasq.d/dhcpdv4.conf
  104. sed "s|<TPL:IP4_NAT_PREFIX>|${ynh_ip4_nat_prefix}|g" -i /etc/dnsmasq.d/dhcpdv4.conf
  105. }
  106. start_hostapd() {
  107. cp /etc/hostapd/hostapd.conf{.tpl,}
  108. sed "s|<TPL:WIFI_DEVICE>|${ynh_wifi_device}|g" -i /etc/hostapd/hostapd.conf
  109. sed "s|<TPL:WIFI_SSID>|${ynh_wifi_ssid}|g" -i /etc/hostapd/hostapd.conf
  110. sed "s|<TPL:WIFI_PASSPHRASE>|${ynh_wifi_passphrase}|g" -i /etc/hostapd/hostapd.conf
  111. sed "s|<TPL:WIFI_CHANNEL>|${ynh_wifi_channel}|g" -i /etc/hostapd/hostapd.conf
  112. if [ "${ynh_wifi_n}" -eq 1 ]; then
  113. sed "s|<TPL:N_COMMENT>||g" -i /etc/hostapd/hostapd.conf
  114. else
  115. sed "s|<TPL:N_COMMENT>|#|g" -i /etc/hostapd/hostapd.conf
  116. fi
  117. if [ "${ynh_wifi_secure}" -eq 1 ]; then
  118. sed "s|<TPL:SEC_COMMENT>||g" -i /etc/hostapd/hostapd.conf
  119. else
  120. sed "s|<TPL:SEC_COMMENT>|#|g" -i /etc/hostapd/hostapd.conf
  121. fi
  122. service hostapd start
  123. }
  124. ## Unsetters
  125. unset_nat() {
  126. internet_device=${1}
  127. iptables -t nat -D POSTROUTING -o "${internet_device}" -j MASQUERADE
  128. }
  129. unset_ip4nataddr() {
  130. ip address delete "${ynh_ip4_nat_prefix}.1/24" dev "${ynh_wifi_device}"
  131. }
  132. unset_ip6addr() {
  133. ip address delete "${ynh_ip6_addr}/64" dev "${ynh_wifi_device}"
  134. }
  135. unset_dhcpd() {
  136. rm -f /etc/dnsmasq.d/dhcpdv?.conf
  137. }
  138. unset_forwarding() {
  139. sysctl -w net.ipv6.conf.all.forwarding=0 > /dev/null
  140. sysctl -w net.ipv4.conf.all.forwarding=0 > /dev/null
  141. }
  142. stop_hostapd() {
  143. service hostapd stop
  144. }
  145. ## Tools
  146. moulinette_get() {
  147. var=${1}
  148. value=$(yunohost app setting hotspot "${var}")
  149. if [[ "${value}" =~ "An instance is already running" ]]; then
  150. echo "${value}" >&2
  151. exit 1
  152. fi
  153. echo "${value}"
  154. }
  155. moulinette_vpnclient_get() {
  156. var=${1}
  157. value=$(yunohost app setting vpnclient "${var}")
  158. if [[ "${value}" =~ "An instance is already running" ]]; then
  159. echo "${value}" >&2
  160. exit 1
  161. fi
  162. echo "${value}"
  163. }
  164. moulinette_set() {
  165. var=${1}
  166. value=${2}
  167. msg=$(yunohost app setting hotspot "${var}" -v "${value}")
  168. if [ ! $? -eq 0 ]; then
  169. echo "${msg}" >&2
  170. exit 1
  171. fi
  172. }
  173. if [ "$1" != restart ]; then
  174. # Restart php5-fpm at the first start (it needs to be restarted after the slapd start)
  175. if [ ! -e /tmp/.ynh-hotspot-boot ]; then
  176. touch /tmp/.ynh-hotspot-boot
  177. service php5-fpm restart
  178. fi
  179. # Variables
  180. echo -n "Retrieving Yunohost settings... "
  181. ynh_service_enabled=$(moulinette_get service_enabled)
  182. ynh_wifi_device=$(moulinette_get wifi_device)
  183. ynh_wifi_ssid=$(moulinette_get wifi_ssid)
  184. ynh_wifi_secure=$(moulinette_get wifi_secure)
  185. ynh_wifi_passphrase=$(moulinette_get wifi_passphrase)
  186. ynh_wifi_channel=$(moulinette_get wifi_channel)
  187. ynh_wifi_n=$(moulinette_get wifi_n)
  188. ynh_ip6_addr=$(moulinette_get ip6_addr)
  189. ynh_ip6_net=$(moulinette_get ip6_net)
  190. ynh_ip6_dns0=$(moulinette_get ip6_dns0)
  191. ynh_ip6_dns1=$(moulinette_get ip6_dns1)
  192. ynh_ip4_dns0=$(moulinette_get ip4_dns0)
  193. ynh_ip4_dns1=$(moulinette_get ip4_dns1)
  194. ynh_ip4_nat_prefix=$(moulinette_get ip4_nat_prefix)
  195. old_internet_device=$(moulinette_get internet_device)
  196. new_internet_device=$(ip route | awk '/default via/ { print $NF; }')
  197. # Switch the NAT interface if there is a VPN
  198. ip link show dev tun0 &> /dev/null
  199. if [ "$?" -eq 0 ]; then
  200. new_internet_device=tun0
  201. fi
  202. echo "OK"
  203. # Check IPv6 delegated prefix from vpnclient
  204. vpnclient_ip6_net=$(moulinette_vpnclient_get ip6_net)
  205. if [ ! -z "${vpnclient_ip6_addr}" ]; then
  206. if [ "${ynh_ip6_net}" == none ]; then
  207. ynh_ip6_net=$vpnclient_ip6_net
  208. ynh_ip6_addr=$(moulinette_vpnclient_get ip6_addr)
  209. moulinette_set ip6_net "${ynh_ip6_net}"
  210. moulinette_set ip6_addr "${ynh_ip6_addr}"
  211. else
  212. if [ "${ynh_ip6_net}" != "${vpnclient_ip6_net}" ]; then
  213. echo "[WARN] The IPv6 delegated prefix is different from the vpnclient one"
  214. fi
  215. fi
  216. fi
  217. fi
  218. # Script
  219. case "$1" in
  220. start)
  221. if is_running; then
  222. echo "Already started"
  223. elif [ "${ynh_service_enabled}" -eq 0 ]; then
  224. echo "Disabled service"
  225. else
  226. echo "[hotspot] Starting..."
  227. touch /tmp/.ynh-hotspot-started
  228. if [ "${new_internet_device}" == tun0 ]; then
  229. moulinette_set vpnclient yes
  230. else
  231. moulinette_set vpnclient no
  232. fi
  233. # Check old state of the ipv4 NAT settings
  234. if [ ! -z "${old_internet_device}" -a "${new_internet_device}" != "${old_internet_device}" ]\
  235. && is_nat_set "${old_internet_device}"; then
  236. unset_nat "${old_internet_device}"
  237. fi
  238. # Set ipv4 NAT
  239. if ! is_nat_set "${new_internet_device}"; then
  240. echo "Set NAT"
  241. set_nat "${new_internet_device}"
  242. fi
  243. # Set ipv4 NAT address
  244. if ! is_ip4nataddr_set; then
  245. echo "Set IPv4 NAT address"
  246. set_ip4nataddr
  247. fi
  248. # Set the ipv6 address
  249. if has_ip6delegatedprefix && ! is_ip6addr_set; then
  250. echo "Set IPv6 address"
  251. set_ip6addr
  252. fi
  253. # Set forwarding for ipv6 and ipv4
  254. if ! is_forwarding_set; then
  255. echo "Set forwarding"
  256. set_forwarding
  257. fi
  258. # Run hostapd
  259. if ! is_hostapd_running; then
  260. echo "Run hostapd"
  261. start_hostapd
  262. fi
  263. # Run DHCP servers
  264. if ( has_ip6delegatedprefix && ! is_dhcpdv6_set ) || ! is_dhcpdv4_set; then
  265. echo "Set DHCP servers (dnsmasq)"
  266. set_dhcpd
  267. fi
  268. # Restart dhcpd
  269. service bind9 stop &> /dev/null
  270. service dnsmasq restart
  271. # Update dynamic settings
  272. moulinette_set internet_device "${new_internet_device}"
  273. fi
  274. ;;
  275. stop)
  276. echo "[hotspot] Stopping..."
  277. rm -f /tmp/.ynh-hotspot-started
  278. if is_nat_set "${old_internet_device}"; then
  279. echo "Unset NAT"
  280. unset_nat "${old_internet_device}"
  281. fi
  282. if is_ip4nataddr_set; then
  283. echo "Unset IPv4 NAT address"
  284. unset_ip4nataddr
  285. fi
  286. if has_ip6delegatedprefix && is_ip6addr_set; then
  287. echo "Unset IPv6 address"
  288. unset_ip6addr
  289. fi
  290. if is_forwarding_set; then
  291. echo "Unset forwarding"
  292. unset_forwarding
  293. fi
  294. if is_dhcpdv6_set || is_dhcpdv4_set; then
  295. echo "Stop DHCP servers"
  296. unset_dhcpd
  297. fi
  298. if is_hostapd_running; then
  299. echo "Stop hostapd"
  300. stop_hostapd
  301. fi
  302. if has_vpnclient_app; then
  303. service ynh-vpnclient start
  304. fi
  305. service dnsmasq restart
  306. ;;
  307. restart)
  308. $0 stop
  309. $0 start
  310. ;;
  311. status)
  312. exitcode=0
  313. if [ "${ynh_service_enabled}" -eq 0 ]; then
  314. echo "[ERR] Hotspot Service disabled"
  315. exitcode=1
  316. fi
  317. echo "[INFO] Autodetected internet interface: ${new_internet_device} (last start: ${old_internet_device})"
  318. if has_ip6delegatedprefix; then
  319. echo "[INFO] IPv6 delegated prefix found"
  320. echo "[INFO] IPv6 address computed from the delegated prefix: ${ynh_ip6_addr}"
  321. if is_ip6addr_set; then
  322. echo "[OK] IPv6 address set"
  323. else
  324. echo "[ERR] No IPv6 address set"
  325. exitcode=1
  326. fi
  327. if is_dhcpdv6_set; then
  328. echo "[OK] SLAAC & DHCPv6 server set"
  329. else
  330. echo "[ERR] No SLAAC & DHCPv6 server set"
  331. exitcode=1
  332. fi
  333. else
  334. echo "[INFO] No IPv6 delegated prefix found"
  335. fi
  336. if is_dhcpdv4_set; then
  337. echo "[OK] DHCPv4 server set"
  338. else
  339. echo "[ERR] No DHCPv4 server set"
  340. exitcode=1
  341. fi
  342. if is_nat_set "${new_internet_device}"; then
  343. echo "[OK] IPv4 NAT set"
  344. else
  345. echo "[ERR] No IPv4 NAT set"
  346. exitcode=1
  347. fi
  348. if is_ip4nataddr_set; then
  349. echo "[OK] IPv4 NAT address set"
  350. else
  351. echo "[ERR] No IPv4 NAT address set"
  352. exitcode=1
  353. fi
  354. if is_forwarding_set; then
  355. echo "[OK] IPv6/IPv4 forwarding set"
  356. else
  357. echo "[ERR] No IPv6/IPv4 forwarding set"
  358. exitcode=1
  359. fi
  360. if is_dnsmasq_running; then
  361. echo "[OK] Dnsmasq is running"
  362. else
  363. echo "[ERR] Dnsmasq is not running"
  364. exitcode=1
  365. fi
  366. if is_hostapd_running; then
  367. echo "[OK] Hostapd is running"
  368. else
  369. echo "[ERR] Hostapd is not running"
  370. exitcode=1
  371. fi
  372. exit ${exitcode}
  373. ;;
  374. *)
  375. echo "Usage: $0 {start|stop|restart|status}"
  376. exit 1
  377. ;;
  378. esac
  379. exit 0