init_ynh-hotspot 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. service bind9 stop &> /dev/null
  106. service dnsmasq restart
  107. }
  108. start_hostapd() {
  109. cp /etc/hostapd/hostapd.conf{.tpl,}
  110. sed "s|<TPL:WIFI_DEVICE>|${ynh_wifi_device}|g" -i /etc/hostapd/hostapd.conf
  111. sed "s|<TPL:WIFI_SSID>|${ynh_wifi_ssid}|g" -i /etc/hostapd/hostapd.conf
  112. sed "s|<TPL:WIFI_PASSPHRASE>|${ynh_wifi_passphrase}|g" -i /etc/hostapd/hostapd.conf
  113. sed "s|<TPL:WIFI_CHANNEL>|${ynh_wifi_channel}|g" -i /etc/hostapd/hostapd.conf
  114. if [ "${ynh_wifi_n}" -eq 1 ]; then
  115. sed "s|<TPL:N_COMMENT>||g" -i /etc/hostapd/hostapd.conf
  116. else
  117. sed "s|<TPL:N_COMMENT>|#|g" -i /etc/hostapd/hostapd.conf
  118. fi
  119. if [ "${ynh_wifi_secure}" -eq 1 ]; then
  120. sed "s|<TPL:SEC_COMMENT>||g" -i /etc/hostapd/hostapd.conf
  121. else
  122. sed "s|<TPL:SEC_COMMENT>|#|g" -i /etc/hostapd/hostapd.conf
  123. fi
  124. service hostapd start
  125. }
  126. ## Unsetters
  127. unset_nat() {
  128. internet_device=${1}
  129. iptables -t nat -D POSTROUTING -o "${internet_device}" -j MASQUERADE
  130. }
  131. unset_ip4nataddr() {
  132. ip address delete "${ynh_ip4_nat_prefix}.1/24" dev "${ynh_wifi_device}"
  133. }
  134. unset_ip6addr() {
  135. ip address delete "${ynh_ip6_addr}/64" dev "${ynh_wifi_device}"
  136. }
  137. unset_dhcpd() {
  138. rm -f /etc/dnsmasq.d/dhcpdv?.conf
  139. service dnsmasq restart
  140. }
  141. unset_forwarding() {
  142. sysctl -w net.ipv6.conf.all.forwarding=0 > /dev/null
  143. sysctl -w net.ipv4.conf.all.forwarding=0 > /dev/null
  144. }
  145. stop_hostapd() {
  146. service hostapd stop
  147. }
  148. ## Tools
  149. moulinette_get() {
  150. var=${1}
  151. value=$(yunohost app setting hotspot "${var}")
  152. if [[ "${value}" =~ "An instance is already running" ]]; then
  153. echo "${value}" >&2
  154. exit 1
  155. fi
  156. echo "${value}"
  157. }
  158. moulinette_vpnclient_get() {
  159. var=${1}
  160. value=$(yunohost app setting vpnclient "${var}")
  161. if [[ "${value}" =~ "An instance is already running" ]]; then
  162. echo "${value}" >&2
  163. exit 1
  164. fi
  165. echo "${value}"
  166. }
  167. moulinette_set() {
  168. var=${1}
  169. value=${2}
  170. msg=$(yunohost app setting hotspot "${var}" -v "${value}")
  171. if [ ! $? -eq 0 ]; then
  172. echo "${msg}" >&2
  173. exit 1
  174. fi
  175. }
  176. if [ "$1" != restart ]; then
  177. # Restart php5-fpm at the first start (it needs to be restarted after the slapd start)
  178. if [ ! -e /tmp/.ynh-hotspot-boot ]; then
  179. touch /tmp/.ynh-hotspot-boot
  180. service php5-fpm restart
  181. fi
  182. # Variables
  183. echo -n "Retrieving Yunohost settings... "
  184. ynh_service_enabled=$(moulinette_get service_enabled)
  185. ynh_wifi_device=$(moulinette_get wifi_device)
  186. ynh_wifi_ssid=$(moulinette_get wifi_ssid)
  187. ynh_wifi_secure=$(moulinette_get wifi_secure)
  188. ynh_wifi_passphrase=$(moulinette_get wifi_passphrase)
  189. ynh_wifi_channel=$(moulinette_get wifi_channel)
  190. ynh_wifi_n=$(moulinette_get wifi_n)
  191. ynh_ip6_addr=$(moulinette_get ip6_addr)
  192. ynh_ip6_net=$(moulinette_get ip6_net)
  193. ynh_ip6_dns0=$(moulinette_get ip6_dns0)
  194. ynh_ip6_dns1=$(moulinette_get ip6_dns1)
  195. ynh_ip4_dns0=$(moulinette_get ip4_dns0)
  196. ynh_ip4_dns1=$(moulinette_get ip4_dns1)
  197. ynh_ip4_nat_prefix=$(moulinette_get ip4_nat_prefix)
  198. old_internet_device=$(moulinette_get internet_device)
  199. new_internet_device=$(ip route | awk '/default via/ { print $NF; }')
  200. # Switch the NAT interface if there is a VPN
  201. ip link show dev tun0 &> /dev/null
  202. if [ "$?" -eq 0 ]; then
  203. new_internet_device=tun0
  204. fi
  205. echo "OK"
  206. # Check IPv6 delegated prefix from vpnclient
  207. vpnclient_ip6_net=$(moulinette_vpnclient_get ip6_net)
  208. if [ ! -z "${vpnclient_ip6_addr}" ]; then
  209. if [ "${ynh_ip6_net}" == none ]; then
  210. ynh_ip6_net=$vpnclient_ip6_net
  211. ynh_ip6_addr=$(moulinette_vpnclient_get ip6_addr)
  212. moulinette_set ip6_net "${ynh_ip6_net}"
  213. moulinette_set ip6_addr "${ynh_ip6_addr}"
  214. else
  215. if [ "${ynh_ip6_net}" != "${vpnclient_ip6_net}" ]; then
  216. echo "[WARN] The IPv6 delegated prefix is different from the vpnclient one"
  217. fi
  218. fi
  219. fi
  220. fi
  221. # Script
  222. case "$1" in
  223. start)
  224. if is_running; then
  225. echo "Already started"
  226. elif [ "${ynh_service_enabled}" -eq 0 ]; then
  227. echo "Disabled service"
  228. else
  229. echo "[hotspot] Starting..."
  230. touch /tmp/.ynh-hotspot-started
  231. if [ "${new_internet_device}" == tun0 ]; then
  232. moulinette_set vpnclient yes
  233. else
  234. moulinette_set vpnclient no
  235. fi
  236. # Check old state of the ipv4 NAT settings
  237. if [ ! -z "${old_internet_device}" -a "${new_internet_device}" != "${old_internet_device}" ]\
  238. && is_nat_set "${old_internet_device}"; then
  239. unset_nat "${old_internet_device}"
  240. fi
  241. # Set ipv4 NAT
  242. if ! is_nat_set "${new_internet_device}"; then
  243. echo "Set NAT"
  244. set_nat "${new_internet_device}"
  245. fi
  246. # Set ipv4 NAT address
  247. if ! is_ip4nataddr_set; then
  248. echo "Set IPv4 NAT address"
  249. set_ip4nataddr
  250. fi
  251. # Set the ipv6 address
  252. if has_ip6delegatedprefix && ! is_ip6addr_set; then
  253. echo "Set IPv6 address"
  254. set_ip6addr
  255. fi
  256. # Set forwarding for ipv6 and ipv4
  257. if ! is_forwarding_set; then
  258. echo "Set forwarding"
  259. set_forwarding
  260. fi
  261. # Run DHCP servers
  262. if ( has_ip6delegatedprefix && ! is_dhcpdv6_set ) || ! is_dhcpdv4_set; then
  263. echo "Set DHCP servers (dnsmasq)"
  264. set_dhcpd
  265. fi
  266. # Run hostapd
  267. if ! is_hostapd_running; then
  268. echo "Run hostapd"
  269. start_hostapd
  270. fi
  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. ;;
  306. restart)
  307. $0 stop
  308. $0 start
  309. ;;
  310. status)
  311. exitcode=0
  312. if [ "${ynh_service_enabled}" -eq 0 ]; then
  313. echo "[ERR] Hotspot Service disabled"
  314. exitcode=1
  315. fi
  316. echo "[INFO] Autodetected internet interface: ${new_internet_device} (last start: ${old_internet_device})"
  317. if has_ip6delegatedprefix; then
  318. echo "[INFO] IPv6 delegated prefix found"
  319. echo "[INFO] IPv6 address computed from the delegated prefix: ${ynh_ip6_addr}"
  320. if is_ip6addr_set; then
  321. echo "[OK] IPv6 address set"
  322. else
  323. echo "[ERR] No IPv6 address set"
  324. exitcode=1
  325. fi
  326. if is_dhcpdv6_set; then
  327. echo "[OK] SLAAC & DHCPv6 server set"
  328. else
  329. echo "[ERR] No SLAAC & DHCPv6 server set"
  330. exitcode=1
  331. fi
  332. else
  333. echo "[INFO] No IPv6 delegated prefix found"
  334. fi
  335. if is_dhcpdv4_set; then
  336. echo "[OK] DHCPv4 server set"
  337. else
  338. echo "[ERR] No DHCPv4 server set"
  339. exitcode=1
  340. fi
  341. if is_nat_set "${new_internet_device}"; then
  342. echo "[OK] IPv4 NAT set"
  343. else
  344. echo "[ERR] No IPv4 NAT set"
  345. exitcode=1
  346. fi
  347. if is_ip4nataddr_set; then
  348. echo "[OK] IPv4 NAT address set"
  349. else
  350. echo "[ERR] No IPv4 NAT address set"
  351. exitcode=1
  352. fi
  353. if is_forwarding_set; then
  354. echo "[OK] IPv6/IPv4 forwarding set"
  355. else
  356. echo "[ERR] No IPv6/IPv4 forwarding set"
  357. exitcode=1
  358. fi
  359. if is_dnsmasq_running; then
  360. echo "[OK] Dnsmasq is running"
  361. else
  362. echo "[ERR] Dnsmasq is not running"
  363. exitcode=1
  364. fi
  365. if is_hostapd_running; then
  366. echo "[OK] Hostapd is running"
  367. else
  368. echo "[ERR] Hostapd is not running"
  369. exitcode=1
  370. fi
  371. exit ${exitcode}
  372. ;;
  373. *)
  374. echo "Usage: $0 {start|stop|restart|status}"
  375. exit 1
  376. ;;
  377. esac
  378. exit 0