init_ynh-hotspot 14 KB

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