ynh-hotspot 14 KB

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