install 7.1 KB

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