install 7.7 KB

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