install 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 ./prerequisites
  28. # Check arguments
  29. if [ -z "${wifi_ssid}" -o -z "${wifi_passphrase}" ]; then
  30. echo "ERROR: Your Wifi Hotspot needs a name and a password" >&2
  31. exit 1
  32. fi
  33. wifi_passphrase_length="$(echo -n "${wifi_passphrase}" | wc -c)"
  34. if [ "${wifi_passphrase_length}" -lt 8 -o "${wifi_passphrase_length}" -gt 63 ]; then
  35. echo "ERROR: Your password must from 8 to 63 characters (WPA2 passphrase)" >&2
  36. exit 1
  37. fi
  38. echo "${wifi_passphrase}" | grep -qP '[^[:print:]]'
  39. if [ $? -eq 0 ]; then
  40. echo "ERROR: Only printable ASCII characters are permitted in your password (WPA2 passphrase)" >&2
  41. exit 1
  42. fi
  43. # Check domain/path availability
  44. sudo yunohost app checkurl ${domain}${url_path} -a hotspot
  45. if [ ! $? -eq 0 ]; then
  46. exit 1
  47. fi
  48. fi
  49. # Install packages
  50. packages='php5-fpm sipcalc hostapd iptables iw dnsmasq'
  51. export DEBIAN_FRONTEND=noninteractive
  52. # Packaged USB Wireless Device firmwares
  53. # Based on https://wiki.debian.org/WiFi#USB_Devices
  54. if [ "${firmware_nonfree}" == yes ]; then
  55. # check if non-free is set on sources.list
  56. if ! grep -q non-free /etc/apt/sources.list ; then
  57. sudo sed '/debian/{s/main/& non-free/}' -i /etc/apt/sources.list
  58. fi
  59. packages="$packages firmware-linux-free firmware-linux-nonfree firmware-atheros firmware-realtek firmware-ralink firmware-libertas atmel-firmware zd1211-firmware"
  60. else
  61. packages="$packages firmware-linux-free"
  62. # Extract from http://packages.trisquel.info/toutatis-updates/open-ath9k-htc-firmware
  63. # https://www.fsf.org/news/ryf-certification-thinkpenguin-usb-with-atheros-chip
  64. # https://wiki.debian.org/ath9k_htc/open_firmware
  65. sudo install -b -o root -g root -m 0644 ../conf/firmware_htc-7010.fw /lib/firmware/htc_7010.fw
  66. sudo install -b -o root -g root -m 0644 ../conf/firmware_htc-9271.fw /lib/firmware/htc_9271.fw
  67. fi
  68. sudo apt-get --assume-yes --force-yes install ${packages}
  69. if [ $? -ne 0 ]; then
  70. sudo apt-get update
  71. sudo apt-get --assume-yes --force-yes install ${packages}
  72. fi
  73. if ! $upgrade; then
  74. # Compute extra arguments
  75. if [ -z "${ip6_net}" ]; then
  76. ip6_net=none
  77. ip6_addr=none
  78. if [ -e /tmp/.ynh-vpnclient-started ]; then
  79. vpnclient_ip6_net=$(sudo yunohost app setting vpnclient ip6_net 2>&1)
  80. vpnclient_ip6_addr=$(sudo yunohost app setting vpnclient ip6_addr 2>&1)
  81. if [[ "${vpnclient_ip6_net}" =~ :: && "${vpnclient_ip6_addr}" =~ :: ]]; then
  82. ip6_net=${vpnclient_ip6_net}
  83. ip6_addr=${vpnclient_ip6_addr}
  84. fi
  85. fi
  86. fi
  87. wifi_device=$(sudo bash ../conf/iw_devices | awk -F\| '{ print $1 }')
  88. # Save arguments
  89. if [ -z "${wifi_device}" ]; then
  90. sudo yunohost app setting hotspot service_enabled -v 0
  91. wifi_device=none
  92. else
  93. sudo yunohost app setting hotspot service_enabled -v 1
  94. fi
  95. sudo yunohost app setting hotspot multissid -v 1
  96. sudo yunohost app setting hotspot wifi_ssid -v "${wifi_ssid}"
  97. sudo yunohost app setting hotspot wifi_secure -v 1
  98. sudo yunohost app setting hotspot wifi_passphrase -v "${wifi_passphrase}"
  99. sudo yunohost app setting hotspot wifi_device -v "${wifi_device}"
  100. sudo yunohost app setting hotspot wifi_channel -v 6
  101. sudo yunohost app setting hotspot ip6_addr -v "${ip6_addr}"
  102. sudo yunohost app setting hotspot ip6_firewall -v 1
  103. sudo yunohost app setting hotspot ip6_net -v "${ip6_net}"
  104. sudo yunohost app setting hotspot ip6_dns0 -v 2001:913::8
  105. sudo yunohost app setting hotspot ip6_dns1 -v 2001:910:800::12
  106. sudo yunohost app setting hotspot ip4_dns0 -v 80.67.188.188
  107. sudo yunohost app setting hotspot ip4_dns1 -v 80.67.169.12
  108. sudo yunohost app setting hotspot ip4_nat_prefix -v 10.0.242
  109. sudo yunohost app setting hotspot vpnclient -v no
  110. fi
  111. # Save git commit
  112. gitcommit=$(git rev-parse HEAD)
  113. sudo yunohost app setting hotspot gitcommit -v "${gitcommit}"
  114. # Install custom scripts
  115. sudo install -o root -g root -m 0755 ../conf/iw_multissid /usr/local/bin/
  116. sudo install -o root -g root -m 0755 ../conf/iw_devices /usr/local/bin/
  117. sudo install -o root -g root -m 0755 ../conf/iw_ssids /usr/local/bin/
  118. sudo install -o root -g root -m 0755 ../conf/ipv6_expanded /usr/local/bin/
  119. sudo install -o root -g root -m 0755 ../conf/ipv6_compressed /usr/local/bin/
  120. # Copy confs
  121. sudo mkdir -pm 0755 /var/log/nginx/
  122. sudo mkdir -pm 0755 /etc/dnsmasq.dhcpd/
  123. sudo chown root: /etc/dnsmasq.dhcpd/
  124. sudo install -b -o root -g root -m 0644 ../conf/hostapd.conf.tpl? /etc/hostapd/
  125. sudo install -b -o root -g root -m 0644 ../conf/dnsmasq_dhcpdv6.conf.tpl /etc/dnsmasq.dhcpd/dhcpdv6.conf.tpl
  126. sudo install -b -o root -g root -m 0644 ../conf/dnsmasq_dhcpdv4.conf.tpl /etc/dnsmasq.dhcpd/dhcpdv4.conf.tpl
  127. sudo install -b -o root -g root -m 0644 ../conf/nginx_wifiadmin.conf "/etc/nginx/conf.d/${domain}.d/wifiadmin.conf"
  128. sudo install -b -o root -g root -m 0644 ../conf/phpfpm_wifiadmin.conf /etc/php5/fpm/pool.d/wifiadmin.conf
  129. # Copy web sources
  130. sudo mkdir -pm 0755 /var/www/wifiadmin/
  131. sudo cp -a ../sources/* /var/www/wifiadmin/
  132. sudo chown -R root: /var/www/wifiadmin/
  133. sudo chmod -R 0644 /var/www/wifiadmin/*
  134. sudo find /var/www/wifiadmin/ -type d -exec chmod +x {} \;
  135. # Fix confs
  136. ## hostapd
  137. sudo sed 's|^DAEMON_CONF=$|&/etc/hostapd/hostapd.conf|' -i /etc/init.d/hostapd
  138. ## nginx
  139. sudo sed "s|<TPL:NGINX_LOCATION>|${url_path}|g" -i "/etc/nginx/conf.d/${domain}.d/wifiadmin.conf"
  140. sudo sed 's|<TPL:NGINX_REALPATH>|/var/www/wifiadmin/|g' -i "/etc/nginx/conf.d/${domain}.d/wifiadmin.conf"
  141. sudo sed 's|<TPL:PHP_NAME>|wifiadmin|g' -i "/etc/nginx/conf.d/${domain}.d/wifiadmin.conf"
  142. ## php-fpm
  143. sudo sed 's|<TPL:PHP_NAME>|wifiadmin|g' -i /etc/php5/fpm/pool.d/wifiadmin.conf
  144. sudo sed 's|<TPL:PHP_USER>|admin|g' -i /etc/php5/fpm/pool.d/wifiadmin.conf
  145. sudo sed 's|<TPL:PHP_GROUP>|admins|g' -i /etc/php5/fpm/pool.d/wifiadmin.conf
  146. sudo sed 's|<TPL:NGINX_REALPATH>|/var/www/wifiadmin/|g' -i /etc/php5/fpm/pool.d/wifiadmin.conf
  147. # Fix sources
  148. sudo sed "s|<TPL:NGINX_LOCATION>|${url_path}|g" -i /var/www/wifiadmin/config.php
  149. # Copy init script
  150. sudo install -o root -g root -m 0755 ../conf/ynh-hotspot /usr/local/bin/
  151. sudo install -o root -g root -m 0644 ../conf/ynh-hotspot.service /etc/systemd/system/
  152. # Update firewall for DHCP
  153. sudo yunohost firewall allow --no-upnp --ipv6 UDP 547
  154. sudo yunohost firewall allow --no-upnp UDP 67
  155. # Set default inits
  156. # The boot order of these services are important, so they are disabled by default
  157. # and the ynh-hotspot service handles them.
  158. sudo systemctl disable hostapd
  159. sudo systemctl stop hostapd
  160. sudo systemctl enable php5-fpm
  161. sudo systemctl restart php5-fpm
  162. sudo systemctl reload nginx
  163. # Remove IPv6 address set if there is a VPN installed
  164. if [ "${ip6_addr}" != none ]; then
  165. sudo ip -6 address show dev tun0 2> /dev/null | grep -q "${ip6_addr}/"
  166. if [ "$?" -eq 0 ]; then
  167. sudo ip address delete "${ip6_addr}/128" dev tun0 &> /dev/null
  168. fi
  169. fi
  170. sudo systemctl enable ynh-hotspot
  171. sudo yunohost service add ynh-hotspot
  172. if [ "${wifi_device}" == none ]; then
  173. echo "WARNING: Wifi Hotspot is not started because no wifi device was found (please, check the web admin)" >&2
  174. fi
  175. if ! $upgrade; then
  176. sudo systemctl start ynh-hotspot
  177. fi
  178. sudo yunohost app ssowatconf
  179. exit 0