init_ynh-hotspot 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. #!/bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides: ynh-hotspot
  4. # Required-Start: $network $remote_fs $syslog yunohost-api
  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. i=${1}
  34. [ "${ynh_ip6_net[${i}]}" != none ]
  35. }
  36. is_nat_set() {
  37. internet_device=${1}
  38. iptables -nvt nat -L POSTROUTING | grep MASQUERADE | grep -q "${internet_device}"
  39. }
  40. is_ip4nataddr_set() {
  41. i=${1}
  42. ip address show dev "ssid${i}" 2> /dev/null | grep -q "${ynh_ip4_nat_prefix[${i}]}.1/24"
  43. }
  44. is_ip6addr_set() {
  45. i=${1}
  46. ip address show dev "ssid${i}" 2> /dev/null | grep -q "${ynh_ip6_addr[${i}]}/64"
  47. }
  48. is_forwarding_set() {
  49. ip6=$(sysctl net.ipv6.conf.all.forwarding | awk '{ print $NF; }')
  50. ip4=$(sysctl net.ipv4.conf.all.forwarding | awk '{ print $NF; }')
  51. [ "${ip6}" -eq 1 -a "${ip4}" -eq 1 ]
  52. }
  53. is_dhcpd_running() {
  54. i=${1}
  55. $(ps aux | grep "dhcpdv.-ssid${i}" | grep -qv grep)
  56. }
  57. is_hostapd_running() {
  58. service hostapd status &> /dev/null
  59. }
  60. is_running() {
  61. for i in $(seq 0 $((${ynh_multissid} - 1))); do
  62. ( has_ip6delegatedprefix ${i} && is_ip6addr_set ${i} || ! has_ip6delegatedprefix ${i} )\
  63. && is_ip4nataddr_set ${i} && is_dhcpd_running ${i}
  64. if [ ! $? -eq 0 ]; then
  65. return 1
  66. fi
  67. done
  68. is_hostapd_running && is_forwarding_set && is_nat_set "${new_internet_device}"
  69. }
  70. ## Setters
  71. set_nat() {
  72. internet_device=${1}
  73. iptables -t nat -A POSTROUTING -o "${internet_device}" -j MASQUERADE
  74. }
  75. set_ip4nataddr() {
  76. i=${1}
  77. ip address add "${ynh_ip4_nat_prefix[${i}]}.1/24" dev "ssid${i}"
  78. }
  79. set_ip6addr() {
  80. i=${1}
  81. ip address delete "${ynh_ip6_addr[${i}]}/64" dev tun0 &> /dev/null
  82. ip address add "${ynh_ip6_addr[${i}]}/64" dev "ssid${i}"
  83. }
  84. set_forwarding() {
  85. sysctl -w net.ipv6.conf.all.forwarding=1 > /dev/null
  86. sysctl -w net.ipv4.conf.all.forwarding=1 > /dev/null
  87. }
  88. start_dhcpd() {
  89. i=${1}
  90. rm -f /etc/dnsmasq.dhcpd/dhcpdv?-ssid*.conf
  91. if has_ip6delegatedprefix ${i}; then
  92. cp /etc/dnsmasq.dhcpdhcpd/dhcpdv6{.conf.tpl,-ssid${i}.conf}
  93. sed "s|<TPL:WIFI_DEVICE>|ssid${i}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf
  94. sed "s|<TPL:IP6_NET>|${ynh_ip6_net[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf
  95. sed "s|<TPL:IP6_DNS0>|${ynh_ip6_dns0[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf
  96. sed "s|<TPL:IP6_DNS1>|${ynh_ip6_dns1[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf
  97. dnsmasq -C /etc/dnsmasq.dhcpd/dhcpdv6-ssid${i}.conf -p0
  98. fi
  99. cp /etc/dnsmasq.dhcpd/dhcpdv4{.conf.tpl,-ssid${i}.conf}
  100. sed "s|<TPL:IP4_DNS0>|${ynh_ip4_dns0[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf
  101. sed "s|<TPL:IP4_DNS1>|${ynh_ip4_dns1[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf
  102. sed "s|<TPL:WIFI_DEVICE>|ssid${i}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf
  103. sed "s|<TPL:IP4_NAT_PREFIX>|${ynh_ip4_nat_prefix[${i}]}|g" -i /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf
  104. dnsmasq -C /etc/dnsmasq.dhcpd/dhcpdv4-ssid${i}.conf -p0
  105. }
  106. start_hostapd() {
  107. cp /etc/hostapd/hostapd.conf{.tpl1,}
  108. ip link set addr 02:42:42:13:37:00 dev "${ynh_wifi_device}"
  109. sed "s|<TPL:WIFI_DEVICE>|${ynh_wifi_device}|g" -i /etc/hostapd/hostapd.conf
  110. sed "s|<TPL:WIFI_CHANNEL>|${ynh_wifi_channel}|g" -i /etc/hostapd/hostapd.conf
  111. iwconfig "${ynh_wifi_device}" | grep -q 'n *ESSID'
  112. if [ $? -eq 0 ]; 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. for i in $(seq 0 $((${ynh_multissid} - 1))); do
  118. cp /etc/hostapd/hostapd.conf{.tpl2,.tmp}
  119. sed "s|<TPL:WIFI_INTERFACE>|ssid${i}|g" -i /etc/hostapd/hostapd.conf.tmp
  120. sed "s|<TPL:WIFI_SSID>|${ynh_wifi_ssid[${i}]}|g" -i /etc/hostapd/hostapd.conf.tmp
  121. sed "s|<TPL:WIFI_PASSPHRASE>|${ynh_wifi_passphrase[${i}]}|g" -i /etc/hostapd/hostapd.conf.tmp
  122. if [ "${ynh_wifi_secure[${i}]}" -eq 1 ]; then
  123. sed "s|<TPL:SEC_COMMENT>||g" -i /etc/hostapd/hostapd.conf.tmp
  124. else
  125. sed "s|<TPL:SEC_COMMENT>|#|g" -i /etc/hostapd/hostapd.conf.tmp
  126. fi
  127. if [ "${i}" -eq 0 ]; then
  128. sed "s|<TPL:BSS_COMMENT>|#|g" -i /etc/hostapd/hostapd.conf.tmp
  129. else
  130. sed "s|<TPL:BSS_COMMENT>||g" -i /etc/hostapd/hostapd.conf.tmp
  131. fi
  132. cat /etc/hostapd/hostapd.conf.tmp >> /etc/hostapd/hostapd.conf
  133. rm /etc/hostapd/hostapd.conf.tmp
  134. done
  135. service hostapd start
  136. }
  137. ## Unsetters
  138. unset_nat() {
  139. internet_device=${1}
  140. iptables -t nat -D POSTROUTING -o "${internet_device}" -j MASQUERADE
  141. }
  142. unset_ip4nataddr() {
  143. i=${1}
  144. ip address delete "${ynh_ip4_nat_prefix[${i}]}.1/24" dev "ssid${i}"
  145. }
  146. unset_ip6addr() {
  147. i=${1}
  148. ip address delete "${ynh_ip6_addr[${i}]}/64" dev "ssid${i}"
  149. }
  150. unset_forwarding() {
  151. sysctl -w net.ipv6.conf.all.forwarding=0 > /dev/null
  152. sysctl -w net.ipv4.conf.all.forwarding=0 > /dev/null
  153. }
  154. stop_dhcpd() {
  155. kill $(ps aux | grep 'dhcpdv.-ssid' | grep -v grep | awk '{ print $2 }')
  156. rm -f /etc/dnsmasq.d/dhcpdv?-ssid*.conf
  157. }
  158. stop_hostapd() {
  159. service hostapd stop
  160. }
  161. ## Tools
  162. moulinette_get() {
  163. var=${1}
  164. value=$(yunohost app setting hotspot "${var}")
  165. if [[ "${value}" =~ "An instance is already running" ]]; then
  166. echo "${value}" >&2
  167. exit 1
  168. fi
  169. echo "${value}"
  170. }
  171. moulinette_vpnclient_get() {
  172. var=${1}
  173. value=$(yunohost app setting vpnclient "${var}")
  174. if [[ "${value}" =~ "An instance is already running" ]]; then
  175. echo "${value}" >&2
  176. exit 1
  177. fi
  178. echo "${value}"
  179. }
  180. moulinette_set() {
  181. var=${1}
  182. value=${2}
  183. msg=$(yunohost app setting hotspot "${var}" -v "${value}")
  184. if [ ! $? -eq 0 ]; then
  185. echo "${msg}" >&2
  186. exit 1
  187. fi
  188. }
  189. if [ "$1" != restart ]; then
  190. # Restart php5-fpm at the first start (it needs to be restarted after the slapd start)
  191. if [ ! -e /tmp/.ynh-hotspot-boot ]; then
  192. touch /tmp/.ynh-hotspot-boot
  193. service php5-fpm restart
  194. fi
  195. # Variables
  196. echo -n "Retrieving Yunohost settings... "
  197. ynh_service_enabled=$(moulinette_get service_enabled)
  198. ynh_wifi_device=$(moulinette_get wifi_device)
  199. ynh_wifi_channel=$(moulinette_get wifi_channel)
  200. ynh_multissid=$(moulinette_get multissid)
  201. IFS='|' read -a ynh_wifi_ssid <<< "$(moulinette_get wifi_ssid)"
  202. IFS='|' read -a ynh_wifi_secure <<< "$(moulinette_get wifi_secure)"
  203. IFS='|' read -a ynh_wifi_passphrase <<< "$(moulinette_get wifi_passphrase)"
  204. IFS='|' read -a ynh_ip6_addr <<< "$(moulinette_get ip6_addr)"
  205. IFS='|' read -a ynh_ip6_net <<< "$(moulinette_get ip6_net)"
  206. IFS='|' read -a ynh_ip6_dns0 <<< "$(moulinette_get ip6_dns0)"
  207. IFS='|' read -a ynh_ip6_dns1 <<< "$(moulinette_get ip6_dns1)"
  208. IFS='|' read -a ynh_ip4_dns0 <<< "$(moulinette_get ip4_dns0)"
  209. IFS='|' read -a ynh_ip4_dns1 <<< "$(moulinette_get ip4_dns1)"
  210. IFS='|' read -a ynh_ip4_nat_prefix <<< "$(moulinette_get ip4_nat_prefix)"
  211. old_internet_device=$(moulinette_get internet_device)
  212. new_internet_device=$(ip route | awk '/default via/ { print $NF; }')
  213. # Switch the NAT interface if there is a VPN
  214. ip link show dev tun0 &> /dev/null
  215. if [ "$?" -eq 0 ]; then
  216. new_internet_device=tun0
  217. fi
  218. echo "OK"
  219. # Check IPv6 delegated prefix from vpnclient
  220. # vpnclient_ip6_net=$(moulinette_vpnclient_get ip6_net)
  221. #
  222. # if [ ! -z "${vpnclient_ip6_addr}" ]; then
  223. # if [ "${ynh_ip6_net}" == none ]; then
  224. # ynh_ip6_net=$vpnclient_ip6_net
  225. # ynh_ip6_addr=$(moulinette_vpnclient_get ip6_addr)
  226. #
  227. # moulinette_set ip6_net "${ynh_ip6_net}"
  228. # moulinette_set ip6_addr "${ynh_ip6_addr}"
  229. # else
  230. # if [ "${ynh_ip6_net}" != "${vpnclient_ip6_net}" ]; then
  231. # echo "[WARN] The IPv6 delegated prefix is different from the vpnclient one"
  232. # fi
  233. # fi
  234. # fi
  235. fi
  236. # Script
  237. case "$1" in
  238. start)
  239. if is_running; then
  240. echo "Already started"
  241. elif [ "${ynh_service_enabled}" -eq 0 ]; then
  242. echo "Disabled service"
  243. else
  244. echo "[hotspot] Starting..."
  245. touch /tmp/.ynh-hotspot-started
  246. if [ "${new_internet_device}" == tun0 ]; then
  247. moulinette_set vpnclient yes
  248. else
  249. moulinette_set vpnclient no
  250. fi
  251. # Check old state of the ipv4 NAT settings
  252. if [ ! -z "${old_internet_device}" -a "${new_internet_device}" != "${old_internet_device}" ]\
  253. && is_nat_set "${old_internet_device}"; then
  254. unset_nat "${old_internet_device}"
  255. fi
  256. # Set ipv4 NAT
  257. if ! is_nat_set "${new_internet_device}"; then
  258. echo "Set NAT"
  259. set_nat "${new_internet_device}"
  260. fi
  261. # Set forwarding for ipv6 and ipv4
  262. if ! is_forwarding_set; then
  263. echo "Set forwarding"
  264. set_forwarding
  265. fi
  266. # Run hostapd
  267. if ! is_hostapd_running; then
  268. echo "Run hostapd"
  269. start_hostapd ${i}
  270. if [ ! $? -eq 0 ]; then
  271. exit 1
  272. fi
  273. i=0; false || while [ $? -ne 0 ]; do
  274. sleep 1 && (( i++ ))
  275. [ ${i} -gt 20 ] && stop_hostapd
  276. [ ${i} -gt 20 ] && exit 1
  277. ip link show dev "mon.${ynh_wifi_device}" &> /dev/null
  278. done
  279. fi
  280. # For each registred ssid
  281. for i in $(seq 0 $((${ynh_multissid} - 1))); do
  282. # Set ipv4 NAT address
  283. if ! is_ip4nataddr_set ${i}; then
  284. echo "SSID ${i}: Set IPv4 NAT address"
  285. set_ip4nataddr ${i}
  286. fi
  287. # Set the ipv6 address
  288. if has_ip6delegatedprefix ${i} && ! is_ip6addr_set ${i}; then
  289. echo "SSID ${i}: Set IPv6 address"
  290. set_ip6addr ${i}
  291. fi
  292. # Run DHCP servers
  293. if ! is_dhcpd_running ${i}; then
  294. echo "SSID ${i}: Set DHCP servers (dnsmasq)"
  295. start_dhcpd ${i}
  296. fi
  297. done
  298. # Update dynamic settings
  299. moulinette_set internet_device "${new_internet_device}"
  300. fi
  301. ;;
  302. stop)
  303. echo "[hotspot] Stopping..."
  304. rm -f /tmp/.ynh-hotspot-started
  305. if is_nat_set "${old_internet_device}"; then
  306. echo "Unset NAT"
  307. unset_nat "${old_internet_device}"
  308. fi
  309. if is_forwarding_set; then
  310. echo "Unset forwarding"
  311. unset_forwarding
  312. fi
  313. for i in $(seq 0 $((${ynh_multissid} - 1))); do
  314. if is_ip4nataddr_set ${i}; then
  315. echo "SSID ${i}: Unset IPv4 NAT address"
  316. unset_ip4nataddr ${i}
  317. fi
  318. if has_ip6delegatedprefix ${i} && is_ip6addr_set ${i}; then
  319. echo "SSID ${i}: Unset IPv6 address"
  320. unset_ip6addr ${i}
  321. fi
  322. if is_dhcpd_running ${i}; then
  323. echo "SSID ${i}: Stop DHCP servers"
  324. stop_dhcpd ${i}
  325. fi
  326. done
  327. if is_hostapd_running; then
  328. echo "Stop hostapd"
  329. stop_hostapd
  330. fi
  331. if has_vpnclient_app; then
  332. service ynh-vpnclient start
  333. fi
  334. ;;
  335. restart)
  336. $0 stop
  337. $0 start
  338. ;;
  339. status)
  340. exitcode=0
  341. if [ "${ynh_service_enabled}" -eq 0 ]; then
  342. echo "[ERR] Hotspot Service disabled"
  343. exitcode=1
  344. fi
  345. echo "[INFO] Autodetected internet interface: ${new_internet_device} (last start: ${old_internet_device})"
  346. if is_nat_set "${new_internet_device}"; then
  347. echo "[OK] IPv4 NAT set"
  348. else
  349. echo "[ERR] No IPv4 NAT set"
  350. exitcode=1
  351. fi
  352. if is_forwarding_set; then
  353. echo "[OK] IPv6/IPv4 forwarding set"
  354. else
  355. echo "[ERR] No IPv6/IPv4 forwarding set"
  356. exitcode=1
  357. fi
  358. if is_hostapd_running; then
  359. echo "[OK] Hostapd is running"
  360. else
  361. echo "[ERR] Hostapd is not running"
  362. exitcode=1
  363. fi
  364. for i in $(seq 0 $((${ynh_multissid} - 1))); do
  365. if has_ip6delegatedprefix ${i}; then
  366. echo "[INFO] SSID ${i}: IPv6 delegated prefix found"
  367. echo "[INFO] SSID ${i}: IPv6 address computed from the delegated prefix: ${ynh_ip6_addr}"
  368. if is_ip6addr_set ${i}; then
  369. echo "[OK] SSID ${i}: IPv6 address set"
  370. else
  371. echo "[ERR] SSID ${i}: No IPv6 address set"
  372. exitcode=1
  373. fi
  374. else
  375. echo "[INFO] SSID ${i}: No IPv6 delegated prefix found"
  376. fi
  377. if is_dhcpd_running ${i}; then
  378. echo "[OK] SSID ${i}: DHCP servers set"
  379. else
  380. echo "[ERR] SSID ${i}: No DHCP servers set"
  381. exitcode=1
  382. fi
  383. if is_ip4nataddr_set ${i}; then
  384. echo "[OK] SSID ${i}: IPv4 NAT address set"
  385. else
  386. echo "[ERR] SSID ${i}: No IPv4 NAT address set"
  387. exitcode=1
  388. fi
  389. done
  390. exit ${exitcode}
  391. ;;
  392. *)
  393. echo "Usage: $0 {start|stop|restart|status}"
  394. exit 1
  395. ;;
  396. esac
  397. exit 0