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. }
  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. service dnsmasq restart
  138. }
  139. unset_forwarding() {
  140. sysctl -w net.ipv6.conf.all.forwarding=0 > /dev/null
  141. sysctl -w net.ipv4.conf.all.forwarding=0 > /dev/null
  142. }
  143. stop_hostapd() {
  144. service hostapd stop
  145. }
  146. ## Tools
  147. moulinette_get() {
  148. var=${1}
  149. value=$(yunohost app setting hotspot "${var}")
  150. if [[ "${value}" =~ "An instance is already running" ]]; then
  151. echo "${value}" >&2
  152. exit 1
  153. fi
  154. echo "${value}"
  155. }
  156. moulinette_vpnclient_get() {
  157. var=${1}
  158. value=$(yunohost app setting vpnclient "${var}")
  159. if [[ "${value}" =~ "An instance is already running" ]]; then
  160. echo "${value}" >&2
  161. exit 1
  162. fi
  163. echo "${value}"
  164. }
  165. moulinette_set() {
  166. var=${1}
  167. value=${2}
  168. msg=$(yunohost app setting hotspot "${var}" -v "${value}")
  169. if [ ! $? -eq 0 ]; then
  170. echo "${msg}" >&2
  171. exit 1
  172. fi
  173. }
  174. if [ "$1" != restart ]; then
  175. # Restart php5-fpm at the first start (it needs to be restarted after the slapd start)
  176. if [ ! -e /tmp/.ynh-hotspot-boot ]; then
  177. touch /tmp/.ynh-hotspot-boot
  178. service php5-fpm restart
  179. fi
  180. # Variables
  181. echo -n "Retrieving Yunohost settings... "
  182. ynh_service_enabled=$(moulinette_get service_enabled)
  183. ynh_wifi_device=$(moulinette_get wifi_device)
  184. ynh_wifi_ssid=$(moulinette_get wifi_ssid)
  185. ynh_wifi_secure=$(moulinette_get wifi_secure)
  186. ynh_wifi_passphrase=$(moulinette_get wifi_passphrase)
  187. ynh_wifi_channel=$(moulinette_get wifi_channel)
  188. ynh_wifi_n=$(moulinette_get wifi_n)
  189. ynh_ip6_addr=$(moulinette_get ip6_addr)
  190. ynh_ip6_net=$(moulinette_get ip6_net)
  191. ynh_ip6_dns0=$(moulinette_get ip6_dns0)
  192. ynh_ip6_dns1=$(moulinette_get ip6_dns1)
  193. ynh_ip4_dns0=$(moulinette_get ip4_dns0)
  194. ynh_ip4_dns1=$(moulinette_get ip4_dns1)
  195. ynh_ip4_nat_prefix=$(moulinette_get ip4_nat_prefix)
  196. old_internet_device=$(moulinette_get internet_device)
  197. new_internet_device=$(ip route | awk '/default via/ { print $NF; }')
  198. # Switch the NAT interface if there is a VPN
  199. ip link show dev tun0 &> /dev/null
  200. if [ "$?" -eq 0 ]; then
  201. new_internet_device=tun0
  202. fi
  203. echo "OK"
  204. # Check IPv6 delegated prefix from vpnclient
  205. vpnclient_ip6_net=$(moulinette_vpnclient_get ip6_net)
  206. if [ ! -z "${vpnclient_ip6_addr}" ]; then
  207. if [ "${ynh_ip6_net}" == none ]; then
  208. ynh_ip6_net=$vpnclient_ip6_net
  209. ynh_ip6_addr=$(moulinette_vpnclient_get ip6_addr)
  210. moulinette_set ip6_net "${ynh_ip6_net}"
  211. moulinette_set ip6_addr "${ynh_ip6_addr}"
  212. else
  213. if [ "${ynh_ip6_net}" != "${vpnclient_ip6_net}" ]; then
  214. echo "[WARN] The IPv6 delegated prefix is different from the vpnclient one"
  215. fi
  216. fi
  217. fi
  218. fi
  219. # Script
  220. case "$1" in
  221. start)
  222. if is_running; then
  223. echo "Already started"
  224. elif [ "${ynh_service_enabled}" -eq 0 ]; then
  225. echo "Disabled service"
  226. else
  227. echo "[hotspot] Starting..."
  228. touch /tmp/.ynh-hotspot-started
  229. if [ "${new_internet_device}" == tun0 ]; then
  230. moulinette_set vpnclient yes
  231. else
  232. moulinette_set vpnclient no
  233. fi
  234. # Check old state of the ipv4 NAT settings
  235. if [ ! -z "${old_internet_device}" -a "${new_internet_device}" != "${old_internet_device}" ]\
  236. && is_nat_set "${old_internet_device}"; then
  237. unset_nat "${old_internet_device}"
  238. fi
  239. # Set ipv4 NAT
  240. if ! is_nat_set "${new_internet_device}"; then
  241. echo "Set NAT"
  242. set_nat "${new_internet_device}"
  243. fi
  244. # Set ipv4 NAT address
  245. if ! is_ip4nataddr_set; then
  246. echo "Set IPv4 NAT address"
  247. set_ip4nataddr
  248. fi
  249. # Set the ipv6 address
  250. if has_ip6delegatedprefix && ! is_ip6addr_set; then
  251. echo "Set IPv6 address"
  252. set_ip6addr
  253. fi
  254. # Set forwarding for ipv6 and ipv4
  255. if ! is_forwarding_set; then
  256. echo "Set forwarding"
  257. set_forwarding
  258. fi
  259. # Run hostapd
  260. if ! is_hostapd_running; then
  261. echo "Run hostapd"
  262. start_hostapd
  263. fi
  264. # Run DHCP servers
  265. if ( has_ip6delegatedprefix && ! is_dhcpdv6_set ) || ! is_dhcpdv4_set; then
  266. echo "Set DHCP servers (dnsmasq)"
  267. set_dhcpd
  268. fi
  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. ;;
  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