ynh-hotspot 14 KB

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