install 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 wireless-tools dnsmasq'
  52. # Packaged USB Wireless Device firmwares
  53. # Based on https://wiki.debian.org/WiFi#USB_Devices
  54. if [ "${firmware_nonfree}" == yes ]; then
  55. packages="$packages firmware-atheros atmel-firmware firmware-linux-free firmware-linux-nonfree firmware-realtek firmware-ralink firmware-libertas zd1211-firmware"
  56. else
  57. packages="$packages firmware-linux-free"
  58. fi
  59. sudo apt-get --assume-yes --force-yes install ${packages}
  60. if [ $? -ne 0 ]; then
  61. sudo apt-get update
  62. sudo apt-get --assume-yes --force-yes install ${packages}
  63. fi
  64. # Compute extra arguments
  65. if [ -z "${ip6_net}" ]; then
  66. ip6_net=none
  67. ip6_addr=none
  68. if [ -e /tmp/.ynh-vpnclient-started ]; then
  69. vpnclient_ip6_net=$(sudo yunohost app setting vpnclient ip6_net 2>&1)
  70. vpnclient_ip6_addr=$(sudo yunohost app setting vpnclient ip6_addr 2>&1)
  71. if [[ "${vpnclient_ip6_net}" =~ :: && "${vpnclient_ip6_addr}" =~ :: ]]; then
  72. ip6_net=${vpnclient_ip6_net}
  73. ip6_addr=${vpnclient_ip6_addr}
  74. fi
  75. fi
  76. #else
  77. # ip6_net=$(bash ../conf/ipv6_expanded "${ip6_net}")
  78. #
  79. # if [ -z "${ip6_net}" ]; then
  80. # echo "ERROR: The IPv6 Delegated Prefix format looks bad" >&2
  81. # exit 1
  82. # fi
  83. #
  84. # ip6_addr="$(echo "${ip6_net}" | cut -d: -f1-7):42"
  85. # ip6_net=$(bash ../conf/ipv6_compressed "${ip6_net}")
  86. # ip6_addr=$(bash ../conf/ipv6_compressed "${ip6_addr}")
  87. fi
  88. wifi_device=$(sudo iwconfig 2>&1 | grep 802.11 | head -n1 | awk '{ print $1 }')
  89. wifi_n=0
  90. if [ -z "${wifi_device}" ]; then
  91. echo "ERROR: No wifi interface found" >&2
  92. exit 1
  93. fi
  94. sudo iwconfig "${wifi_device}" | grep -q 'n *ESSID'
  95. if [ $? -eq 0 ]; then
  96. wifi_n=1
  97. fi
  98. # Save arguments
  99. sudo yunohost app setting hotspot service_enabled -v 1
  100. sudo yunohost app setting hotspot wifi_ssid -v "${wifi_ssid}"
  101. sudo yunohost app setting hotspot wifi_secure -v 1
  102. sudo yunohost app setting hotspot wifi_passphrase -v "${wifi_passphrase}"
  103. sudo yunohost app setting hotspot wifi_device -v "${wifi_device}"
  104. sudo yunohost app setting hotspot wifi_channel -v 6
  105. sudo yunohost app setting hotspot wifi_n -v "${wifi_n}"
  106. sudo yunohost app setting hotspot ip6_addr -v "${ip6_addr}"
  107. sudo yunohost app setting hotspot ip6_net -v "${ip6_net}"
  108. sudo yunohost app setting hotspot ip6_dns0 -v 2001:913::8
  109. sudo yunohost app setting hotspot ip6_dns1 -v 2001:910:800::12
  110. sudo yunohost app setting hotspot ip4_dns0 -v 80.67.188.188
  111. sudo yunohost app setting hotspot ip4_dns1 -v 80.67.169.12
  112. sudo yunohost app setting hotspot ip4_nat_prefix -v 10.0.242
  113. sudo yunohost app setting hotspot vpnclient -v no
  114. # Install IPv6 scripts
  115. sudo install -o root -g root -m 0755 ../conf/ipv6_expanded /usr/local/bin/
  116. sudo install -o root -g root -m 0755 ../conf/ipv6_compressed /usr/local/bin/
  117. # Copy confs
  118. sudo mkdir -pm 0755 /etc/dnsmasq.d.tpl/
  119. sudo chown root: /etc/dnsmasq.d.tpl/
  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.d.tpl/dhcpdv6.conf.tpl
  122. sudo install -b -o root -g root -m 0644 ../conf/dnsmasq_dhcpdv4.conf.tpl /etc/dnsmasq.d.tpl/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 web sources
  126. sudo mkdir -pm 0755 /var/www/wifiadmin/
  127. sudo cp -a ../sources/* /var/www/wifiadmin/
  128. sudo chown -R root: /var/www/wifiadmin/
  129. sudo chmod -R 0644 /var/www/wifiadmin/*
  130. sudo find /var/www/wifiadmin/ -type d -exec chmod +x {} \;
  131. # Fix confs
  132. ## hostapd
  133. sudo sed 's|^DAEMON_CONF=$|&/etc/hostapd/hostapd.conf|' -i /etc/init.d/hostapd
  134. ## nginx
  135. sudo sed "s|<TPL:NGINX_LOCATION>|${url_path}|g" -i "/etc/nginx/conf.d/${domain}.d/wifiadmin.conf"
  136. sudo sed 's|<TPL:NGINX_REALPATH>|/var/www/wifiadmin/|g' -i "/etc/nginx/conf.d/${domain}.d/wifiadmin.conf"
  137. sudo sed 's|<TPL:PHP_NAME>|wifiadmin|g' -i "/etc/nginx/conf.d/${domain}.d/wifiadmin.conf"
  138. ## php-fpm
  139. sudo sed 's|<TPL:PHP_NAME>|wifiadmin|g' -i /etc/php5/fpm/pool.d/wifiadmin.conf
  140. sudo sed 's|<TPL:PHP_USER>|admin|g' -i /etc/php5/fpm/pool.d/wifiadmin.conf
  141. sudo sed 's|<TPL:PHP_GROUP>|admins|g' -i /etc/php5/fpm/pool.d/wifiadmin.conf
  142. sudo sed 's|<TPL:NGINX_REALPATH>|/var/www/wifiadmin/|g' -i /etc/php5/fpm/pool.d/wifiadmin.conf
  143. sudo sed 's|^;\?\s*max_execution_time.\+|max_execution_time = 600|' -i /etc/php5/fpm/php.ini
  144. # Fix sources
  145. sudo sed "s|<TPL:NGINX_LOCATION>|${url_path}|g" -i /var/www/wifiadmin/config.php
  146. # Copy init script
  147. sudo install -o root -g root -m 0755 ../conf/init_ynh-hotspot /etc/init.d/ynh-hotspot
  148. # Update firewall for DHCP
  149. sudo yunohost firewall allow --no-upnp --ipv6 UDP 547
  150. sudo yunohost firewall allow --no-upnp UDP 67
  151. # Set default inits
  152. # The boot order of these services are important, so they are disabled by default
  153. # and the ynh-hotspot service handles them.
  154. # All services are registred by yunohost in order to prevent conflicts after the uninstall.
  155. sudo yunohost service add hostapd
  156. sudo yunohost service stop hostapd
  157. sudo yunohost service disable hostapd
  158. sudo yunohost service add php5-fpm
  159. sudo yunohost service enable php5-fpm
  160. sudo service nginx reload
  161. # Remove IPv6 address set if there is a VPN installed
  162. if [ "${ip6_addr}" != none ]; then
  163. sudo ip -6 address show dev tun0 2> /dev/null | grep -q "${ip6_addr}/"
  164. if [ "$?" -eq 0 ]; then
  165. sudo ip address delete "${ip6_addr}/128" dev tun0 &> /dev/null
  166. fi
  167. fi
  168. sudo yunohost service add ynh-hotspot
  169. sudo yunohost service enable ynh-hotspot
  170. sudo service ynh-hotspot start
  171. # Update SSO for wifiadmin
  172. sudo yunohost app ssowatconf
  173. exit 0