init_ynh-hotspot 15 KB

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