init_ynh-hotspot 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. #!/bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides: ynh-hotspot
  4. # Required-Start: $network $remote_fs $syslog $all
  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. cat /etc/hostapd/hostapd.conf.tmp >> /etc/hostapd/hostapd.conf
  128. rm /etc/hostapd/hostapd.conf.tmp
  129. done
  130. service hostapd start
  131. }
  132. ## Unsetters
  133. unset_nat() {
  134. internet_device=${1}
  135. iptables -t nat -D POSTROUTING -o "${internet_device}" -j MASQUERADE
  136. }
  137. unset_ip4nataddr() {
  138. i=${1}
  139. ip address delete "${ynh_ip4_nat_prefix[${i}]}.1/24" dev "ssid${i}"
  140. }
  141. unset_ip6addr() {
  142. i=${1}
  143. ip address delete "${ynh_ip6_addr[${i}]}/64" dev "ssid${i}"
  144. }
  145. unset_forwarding() {
  146. sysctl -w net.ipv6.conf.all.forwarding=0 > /dev/null
  147. sysctl -w net.ipv4.conf.all.forwarding=0 > /dev/null
  148. }
  149. stop_dhcpd() {
  150. kill $(ps aux | grep 'dhcpdv.-ssid' | grep -v grep | awk '{ print $2 }')
  151. rm -f /etc/dnsmasq.d/dhcpdv?-ssid*.conf
  152. }
  153. stop_hostapd() {
  154. service hostapd stop
  155. }
  156. ## Tools
  157. moulinette_get() {
  158. var=${1}
  159. value=$(yunohost app setting hotspot "${var}")
  160. if [[ "${value}" =~ "An instance is already running" ]]; then
  161. echo "${value}" >&2
  162. exit 1
  163. fi
  164. echo "${value}"
  165. }
  166. moulinette_vpnclient_get() {
  167. var=${1}
  168. value=$(yunohost app setting vpnclient "${var}")
  169. if [[ "${value}" =~ "An instance is already running" ]]; then
  170. echo "${value}" >&2
  171. exit 1
  172. fi
  173. echo "${value}"
  174. }
  175. moulinette_set() {
  176. var=${1}
  177. value=${2}
  178. msg=$(yunohost app setting hotspot "${var}" -v "${value}")
  179. if [ ! $? -eq 0 ]; then
  180. echo "${msg}" >&2
  181. exit 1
  182. fi
  183. }
  184. if [ "$1" != restart ]; then
  185. # Restart php5-fpm at the first start (it needs to be restarted after the slapd start)
  186. if [ ! -e /tmp/.ynh-hotspot-boot ]; then
  187. touch /tmp/.ynh-hotspot-boot
  188. service php5-fpm restart
  189. fi
  190. # Variables
  191. echo -n "Retrieving Yunohost settings... "
  192. ynh_service_enabled=$(moulinette_get service_enabled)
  193. ynh_wifi_device=$(moulinette_get wifi_device)
  194. ynh_wifi_channel=$(moulinette_get wifi_channel)
  195. ynh_multissid=$(moulinette_get multissid)
  196. IFS='|' read -a ynh_wifi_ssid <<< "$(moulinette_get wifi_ssid)"
  197. IFS='|' read -a ynh_wifi_secure <<< "$(moulinette_get wifi_secure)"
  198. IFS='|' read -a ynh_wifi_passphrase <<< "$(moulinette_get wifi_passphrase)"
  199. IFS='|' read -a ynh_ip6_addr <<< "$(moulinette_get ip6_addr)"
  200. IFS='|' read -a ynh_ip6_net <<< "$(moulinette_get ip6_net)"
  201. IFS='|' read -a ynh_ip6_dns0 <<< "$(moulinette_get ip6_dns0)"
  202. IFS='|' read -a ynh_ip6_dns1 <<< "$(moulinette_get ip6_dns1)"
  203. IFS='|' read -a ynh_ip4_dns0 <<< "$(moulinette_get ip4_dns0)"
  204. IFS='|' read -a ynh_ip4_dns1 <<< "$(moulinette_get ip4_dns1)"
  205. IFS='|' read -a ynh_ip4_nat_prefix <<< "$(moulinette_get ip4_nat_prefix)"
  206. old_internet_device=$(moulinette_get internet_device)
  207. new_internet_device=$(ip route | awk '/default via/ { print $NF; }')
  208. # Switch the NAT interface if there is a VPN
  209. ip link show dev tun0 &> /dev/null
  210. if [ "$?" -eq 0 ]; then
  211. new_internet_device=tun0
  212. fi
  213. echo "OK"
  214. # Check IPv6 delegated prefix from vpnclient
  215. # vpnclient_ip6_net=$(moulinette_vpnclient_get ip6_net)
  216. #
  217. # if [ ! -z "${vpnclient_ip6_addr}" ]; then
  218. # if [ "${ynh_ip6_net}" == none ]; then
  219. # ynh_ip6_net=$vpnclient_ip6_net
  220. # ynh_ip6_addr=$(moulinette_vpnclient_get ip6_addr)
  221. #
  222. # moulinette_set ip6_net "${ynh_ip6_net}"
  223. # moulinette_set ip6_addr "${ynh_ip6_addr}"
  224. # else
  225. # if [ "${ynh_ip6_net}" != "${vpnclient_ip6_net}" ]; then
  226. # echo "[WARN] The IPv6 delegated prefix is different from the vpnclient one"
  227. # fi
  228. # fi
  229. # fi
  230. fi
  231. # Script
  232. case "$1" in
  233. start)
  234. if is_running; then
  235. echo "Already started"
  236. elif [ "${ynh_service_enabled}" -eq 0 ]; then
  237. echo "Disabled service"
  238. else
  239. echo "[hotspot] Starting..."
  240. touch /tmp/.ynh-hotspot-started
  241. if [ "${new_internet_device}" == tun0 ]; then
  242. moulinette_set vpnclient yes
  243. else
  244. moulinette_set vpnclient no
  245. fi
  246. # Check old state of the ipv4 NAT settings
  247. if [ ! -z "${old_internet_device}" -a "${new_internet_device}" != "${old_internet_device}" ]\
  248. && is_nat_set "${old_internet_device}"; then
  249. unset_nat "${old_internet_device}"
  250. fi
  251. # Set ipv4 NAT
  252. if ! is_nat_set "${new_internet_device}"; then
  253. echo "Set NAT"
  254. set_nat "${new_internet_device}"
  255. fi
  256. # Set forwarding for ipv6 and ipv4
  257. if ! is_forwarding_set; then
  258. echo "Set forwarding"
  259. set_forwarding
  260. fi
  261. # Run hostapd
  262. if ! is_hostapd_running; then
  263. echo "Run hostapd"
  264. start_hostapd ${i}
  265. if [ ! $? -eq 0 ]; then
  266. exit 1
  267. fi
  268. i=0; false || while [ $? -ne 0 ]; do
  269. sleep 1 && (( i++ ))
  270. [ ${i} -gt 20 ] && stop_hostapd
  271. [ ${i} -gt 20 ] && exit 1
  272. ip link show dev ssid0 &> /dev/null
  273. done
  274. fi
  275. # For each registred ssid
  276. for i in $(seq 0 $((${ynh_multissid} - 1))); do
  277. # Set ipv4 NAT address
  278. if ! is_ip4nataddr_set ${i}; then
  279. echo "Set IPv4 NAT address"
  280. set_ip4nataddr ${i}
  281. fi
  282. # Set the ipv6 address
  283. if has_ip6delegatedprefix ${i} && ! is_ip6addr_set ${i}; then
  284. echo "Set IPv6 address"
  285. set_ip6addr ${i}
  286. fi
  287. # Run DHCP servers
  288. if ! is_dhcpd_running ${i}; then
  289. echo "Set DHCP servers (dnsmasq)"
  290. start_dhcpd ${i}
  291. fi
  292. done
  293. # Update dynamic settings
  294. moulinette_set internet_device "${new_internet_device}"
  295. fi
  296. ;;
  297. stop)
  298. echo "[hotspot] Stopping..."
  299. rm -f /tmp/.ynh-hotspot-started
  300. if is_nat_set "${old_internet_device}"; then
  301. echo "Unset NAT"
  302. unset_nat "${old_internet_device}"
  303. fi
  304. if is_forwarding_set; then
  305. echo "Unset forwarding"
  306. unset_forwarding
  307. fi
  308. for i in $(seq 0 $((${ynh_multissid} - 1))); do
  309. if is_ip4nataddr_set ${i}; then
  310. echo "Unset IPv4 NAT address"
  311. unset_ip4nataddr ${i}
  312. fi
  313. if has_ip6delegatedprefix ${i} && is_ip6addr_set ${i}; then
  314. echo "Unset IPv6 address"
  315. unset_ip6addr ${i}
  316. fi
  317. if is_dhcpd_running ${i}; then
  318. echo "Stop DHCP servers"
  319. stop_dhcpd ${i}
  320. fi
  321. done
  322. if is_hostapd_running; then
  323. echo "Stop hostapd"
  324. stop_hostapd
  325. fi
  326. if has_vpnclient_app; then
  327. service ynh-vpnclient start
  328. fi
  329. ;;
  330. restart)
  331. $0 stop
  332. $0 start
  333. ;;
  334. status)
  335. exitcode=0
  336. if [ "${ynh_service_enabled}" -eq 0 ]; then
  337. echo "[ERR] Hotspot Service disabled"
  338. exitcode=1
  339. fi
  340. echo "[INFO] Autodetected internet interface: ${new_internet_device} (last start: ${old_internet_device})"
  341. if is_nat_set "${new_internet_device}"; then
  342. echo "[OK] IPv4 NAT set"
  343. else
  344. echo "[ERR] No IPv4 NAT set"
  345. exitcode=1
  346. fi
  347. if is_forwarding_set; then
  348. echo "[OK] IPv6/IPv4 forwarding set"
  349. else
  350. echo "[ERR] No IPv6/IPv4 forwarding set"
  351. exitcode=1
  352. fi
  353. if is_hostapd_running; then
  354. echo "[OK] Hostapd is running"
  355. else
  356. echo "[ERR] Hostapd is not running"
  357. exitcode=1
  358. fi
  359. for i in $(seq 0 $((${ynh_multissid} - 1))); do
  360. if has_ip6delegatedprefix ${i}; then
  361. echo "[INFO] SSID ${i}: IPv6 delegated prefix found"
  362. echo "[INFO] SSID ${i}: IPv6 address computed from the delegated prefix: ${ynh_ip6_addr}"
  363. if is_ip6addr_set ${i}; then
  364. echo "[OK] SSID ${i}: IPv6 address set"
  365. else
  366. echo "[ERR] SSID ${i}: No IPv6 address set"
  367. exitcode=1
  368. fi
  369. else
  370. echo "[INFO] SSID ${i}: No IPv6 delegated prefix found"
  371. fi
  372. if is_dhcpd_running ${i}; then
  373. echo "[OK] SSID ${i}: DHCP servers set"
  374. else
  375. echo "[ERR] SSID ${i}: No DHCP servers set"
  376. exitcode=1
  377. fi
  378. if is_ip4nataddr_set ${i}; then
  379. echo "[OK] SSID ${i}: IPv4 NAT address set"
  380. else
  381. echo "[ERR] SSID ${i}: No IPv4 NAT address set"
  382. exitcode=1
  383. fi
  384. done
  385. exit ${exitcode}
  386. ;;
  387. *)
  388. echo "Usage: $0 {start|stop|restart|status}"
  389. exit 1
  390. ;;
  391. esac
  392. exit 0