install 8.2 KB

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