init_ynh-hotspot 13 KB

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