install 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #!/bin/bash
  2. # Wifi Hotspot app for YunoHost
  3. # Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
  4. # Contribute at https://github.com/labriqueinternet/hotspot_ynh
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. # This is an upgrade?
  19. upgrade=$([ "${HOTSPOT_UPGRADE}" == 1 ] && echo true || echo false)
  20. # Retrieve arguments
  21. domain=${1}
  22. url_path=${2}
  23. wifi_ssid=${3}
  24. wifi_passphrase=${4}
  25. firmware_nonfree=${5}
  26. if ! $upgrade; then
  27. source ./helpers
  28. source ./prerequisites
  29. # Check arguments
  30. if [ -z "${wifi_ssid}" -o -z "${wifi_passphrase}" ]; then
  31. echo "ERROR: Your Wifi Hotspot needs a name and a password" >&2
  32. exit 1
  33. fi
  34. wifi_passphrase_length="$(echo -n "${wifi_passphrase}" | wc -c)"
  35. if [ "${wifi_passphrase_length}" -lt 8 -o "${wifi_passphrase_length}" -gt 63 ]; then
  36. echo "ERROR: Your password must from 8 to 63 characters (WPA2 passphrase)" >&2
  37. exit 1
  38. fi
  39. echo "${wifi_passphrase}" | grep -qP '[^[:print:]]'
  40. if [ $? -eq 0 ]; then
  41. echo "ERROR: Only printable ASCII characters are permitted in your password (WPA2 passphrase)" >&2
  42. exit 1
  43. fi
  44. fi
  45. # Check domain/path availability
  46. ynh_webpath_register hotspot $domain $url_path || exit 1
  47. # Install packages
  48. packages='php5-fpm sipcalc hostapd iptables iw dnsmasq'
  49. export DEBIAN_FRONTEND=noninteractive
  50. # Packaged USB Wireless Device firmwares
  51. # Based on https://wiki.debian.org/WiFi#USB_Devices
  52. if [ "${firmware_nonfree}" == yes ]; then
  53. # check if non-free is set on sources.list
  54. if ! grep -q non-free /etc/apt/sources.list ; then
  55. sudo sed '/debian/{s/main/& non-free/}' -i /etc/apt/sources.list
  56. fi
  57. packages="$packages firmware-linux-free firmware-linux-nonfree firmware-atheros firmware-realtek firmware-ralink firmware-libertas atmel-firmware zd1211-firmware"
  58. else
  59. packages="$packages firmware-linux-free"
  60. # Extract from http://packages.trisquel.info/toutatis-updates/open-ath9k-htc-firmware
  61. # https://www.fsf.org/news/ryf-certification-thinkpenguin-usb-with-atheros-chip
  62. # https://wiki.debian.org/ath9k_htc/open_firmware
  63. sudo install -b -o root -g root -m 0644 ../conf/firmware_htc-7010.fw /lib/firmware/htc_7010.fw
  64. sudo install -b -o root -g root -m 0644 ../conf/firmware_htc-9271.fw /lib/firmware/htc_9271.fw
  65. fi
  66. sudo apt-get --assume-yes --force-yes install ${packages}
  67. if [ $? -ne 0 ]; then
  68. sudo apt-get update
  69. sudo apt-get --assume-yes --force-yes install ${packages}
  70. fi
  71. if ! $upgrade; then
  72. # Compute extra arguments
  73. if [ -z "${ip6_net}" ]; then
  74. ip6_net=none
  75. ip6_addr=none
  76. if [ -e /tmp/.ynh-vpnclient-started ]; then
  77. vpnclient_ip6_net=$(sudo yunohost app setting vpnclient ip6_net 2>&1)
  78. vpnclient_ip6_addr=$(sudo yunohost app setting vpnclient ip6_addr 2>&1)
  79. if [[ "${vpnclient_ip6_net}" =~ :: && "${vpnclient_ip6_addr}" =~ :: ]]; then
  80. ip6_net=${vpnclient_ip6_net}
  81. ip6_addr=${vpnclient_ip6_addr}
  82. fi
  83. fi
  84. fi
  85. wifi_device=$(sudo bash ../conf/iw_devices | awk -F\| '{ print $1 }')
  86. # Save arguments
  87. if [ -z "${wifi_device}" ]; then
  88. sudo yunohost app setting hotspot service_enabled -v 0
  89. wifi_device=none
  90. else
  91. sudo yunohost app setting hotspot service_enabled -v 1
  92. fi
  93. sudo yunohost app setting hotspot multissid -v 1
  94. sudo yunohost app setting hotspot wifi_ssid -v "${wifi_ssid}"
  95. sudo yunohost app setting hotspot wifi_secure -v 1
  96. sudo yunohost app setting hotspot wifi_passphrase -v "${wifi_passphrase}"
  97. sudo yunohost app setting hotspot wifi_device -v "${wifi_device}"
  98. sudo yunohost app setting hotspot wifi_channel -v 6
  99. sudo yunohost app setting hotspot ip6_addr -v "${ip6_addr}"
  100. sudo yunohost app setting hotspot ip6_firewall -v 1
  101. sudo yunohost app setting hotspot ip6_net -v "${ip6_net}"
  102. sudo yunohost app setting hotspot ip6_dns0 -v 2001:913::8
  103. sudo yunohost app setting hotspot ip6_dns1 -v 2001:910:800::12
  104. sudo yunohost app setting hotspot ip4_dns0 -v 80.67.188.188
  105. sudo yunohost app setting hotspot ip4_dns1 -v 80.67.169.12
  106. sudo yunohost app setting hotspot ip4_nat_prefix -v 10.0.242
  107. sudo yunohost app setting hotspot vpnclient -v no
  108. fi
  109. # Install custom scripts
  110. sudo install -o root -g root -m 0755 ../conf/iw_multissid /usr/local/bin/
  111. sudo install -o root -g root -m 0755 ../conf/iw_devices /usr/local/bin/
  112. sudo install -o root -g root -m 0755 ../conf/iw_ssids /usr/local/bin/
  113. sudo install -o root -g root -m 0755 ../conf/ipv6_expanded /usr/local/bin/
  114. sudo install -o root -g root -m 0755 ../conf/ipv6_compressed /usr/local/bin/
  115. # Copy confs
  116. sudo mkdir -pm 0755 /var/log/nginx/
  117. sudo mkdir -pm 0755 /etc/dnsmasq.dhcpd/
  118. sudo chown root: /etc/dnsmasq.dhcpd/
  119. sudo install -b -o root -g root -m 0644 ../conf/hostapd.conf.tpl? /etc/hostapd/
  120. sudo install -b -o root -g root -m 0644 ../conf/dnsmasq_dhcpdv6.conf.tpl /etc/dnsmasq.dhcpd/dhcpdv6.conf.tpl
  121. sudo install -b -o root -g root -m 0644 ../conf/dnsmasq_dhcpdv4.conf.tpl /etc/dnsmasq.dhcpd/dhcpdv4.conf.tpl
  122. sudo install -b -o root -g root -m 0644 ../conf/nginx_wifiadmin.conf "/etc/nginx/conf.d/${domain}.d/wifiadmin.conf"
  123. sudo install -b -o root -g root -m 0644 ../conf/phpfpm_wifiadmin.conf /etc/php5/fpm/pool.d/wifiadmin.conf
  124. # Copy web sources
  125. sudo mkdir -pm 0755 /var/www/wifiadmin/
  126. sudo cp -a ../sources/* /var/www/wifiadmin/
  127. sudo chown -R root: /var/www/wifiadmin/
  128. sudo chmod -R 0644 /var/www/wifiadmin/*
  129. sudo find /var/www/wifiadmin/ -type d -exec chmod +x {} \;
  130. # Fix confs
  131. ## hostapd
  132. sudo sed 's|^DAEMON_CONF=$|&/etc/hostapd/hostapd.conf|' -i /etc/init.d/hostapd
  133. ## nginx
  134. sudo sed "s|<TPL:NGINX_LOCATION>|${url_path}|g" -i "/etc/nginx/conf.d/${domain}.d/wifiadmin.conf"
  135. sudo sed 's|<TPL:NGINX_REALPATH>|/var/www/wifiadmin/|g' -i "/etc/nginx/conf.d/${domain}.d/wifiadmin.conf"
  136. sudo sed 's|<TPL:PHP_NAME>|wifiadmin|g' -i "/etc/nginx/conf.d/${domain}.d/wifiadmin.conf"
  137. ## php-fpm
  138. sudo sed 's|<TPL:PHP_NAME>|wifiadmin|g' -i /etc/php5/fpm/pool.d/wifiadmin.conf
  139. sudo sed 's|<TPL:PHP_USER>|admin|g' -i /etc/php5/fpm/pool.d/wifiadmin.conf
  140. sudo sed 's|<TPL:PHP_GROUP>|admins|g' -i /etc/php5/fpm/pool.d/wifiadmin.conf
  141. sudo sed 's|<TPL:NGINX_REALPATH>|/var/www/wifiadmin/|g' -i /etc/php5/fpm/pool.d/wifiadmin.conf
  142. # Fix sources
  143. sudo sed "s|<TPL:NGINX_LOCATION>|${url_path}|g" -i /var/www/wifiadmin/config.php
  144. # Copy init script
  145. sudo install -o root -g root -m 0755 ../conf/ynh-hotspot /usr/local/bin/
  146. sudo install -o root -g root -m 0644 ../conf/ynh-hotspot.service /etc/systemd/system/
  147. # Update firewall for DHCP
  148. sudo yunohost firewall allow --no-upnp --ipv6 UDP 547
  149. sudo yunohost firewall allow --no-upnp UDP 67
  150. # Set default inits
  151. # The boot order of these services are important, so they are disabled by default
  152. # and the ynh-hotspot service handles them.
  153. sudo systemctl disable hostapd
  154. sudo systemctl stop hostapd
  155. sudo systemctl enable php5-fpm
  156. sudo systemctl restart php5-fpm
  157. sudo systemctl reload nginx
  158. # Remove IPv6 address set if there is a VPN installed
  159. if [ "${ip6_addr}" != none ]; then
  160. sudo ip -6 address show dev tun0 2> /dev/null | grep -q "${ip6_addr}/"
  161. if [ "$?" -eq 0 ]; then
  162. sudo ip address delete "${ip6_addr}/128" dev tun0 &> /dev/null
  163. fi
  164. fi
  165. sudo systemctl enable ynh-hotspot
  166. sudo yunohost service add ynh-hotspot
  167. if [ "${wifi_device}" == none ]; then
  168. echo "WARNING: Wifi Hotspot is not started because no wifi device was found (please, check the web admin)" >&2
  169. fi
  170. if ! $upgrade; then
  171. ynh_systemctl start ynh-hotspot
  172. fi
  173. sudo yunohost app ssowatconf
  174. exit 0