install 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #=================================================
  19. # GENERIC START
  20. #=================================================
  21. # IMPORT GENERIC HELPERS
  22. #=================================================
  23. source _common.sh
  24. source /usr/share/yunohost/helpers
  25. #=================================================
  26. # MANAGE SCRIPT FAILURE
  27. #=================================================
  28. # Exit if an error occurs during the execution of the script
  29. ynh_abort_if_errors
  30. #=================================================
  31. # RETRIEVE ARGUMENTS FROM THE MANIFEST
  32. #=================================================
  33. # Retrieve arguments
  34. domain=$YNH_APP_ARG_DOMAIN
  35. path_url=$YNH_APP_ARG_PATH
  36. wifi_ssid=$YNH_APP_ARG_WIFI_SSID
  37. wifi_passphrase=$YNH_APP_ARG_WIFI_PASSPHRASE
  38. firmware_nonfree=$YNH_APP_ARG_FIRMWARE_NONFREE
  39. app=$YNH_APP_INSTANCE_NAME
  40. # the service name must match the service template files
  41. service_name='ynh-hotspot'
  42. #=================================================
  43. # CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
  44. #=================================================
  45. # Check destination directory
  46. final_path="/var/www/$app"
  47. test ! -e "$final_path" || ynh_die "This path already contains a folder"
  48. # Normalize the url path syntax
  49. path_url=$(ynh_normalize_url_path "$path_url")
  50. # Check web path availability
  51. ynh_webpath_available "$domain" "$path_url"
  52. # Register (book) web path
  53. ynh_webpath_register "$app" "$domain" "$path_url"
  54. #=================================================
  55. # STORE SETTINGS FROM MANIFEST
  56. #=================================================
  57. ynh_app_setting_set "$app" domain "$domain"
  58. ynh_app_setting_set "$app" final_path "$final_path"
  59. ynh_app_setting_set "$app" wifi_ssid "$wifi_ssid"
  60. ynh_app_setting_set "$app" wifi_passphrase "$wifi_passphrase"
  61. ynh_app_setting_set "$app" firmware_nonfree "$firmware_nonfree"
  62. #=================================================
  63. # STANDARD MODIFICATIONS
  64. #=================================================
  65. # INSTALL DEPENDENCIES
  66. #=================================================
  67. ynh_install_app_dependencies "$pkg_dependencies"
  68. #=================================================
  69. # SPECIFIC SETUP
  70. #=================================================
  71. # RUN PREREQUISITES
  72. #=================================================
  73. source ./prerequisites
  74. #=================================================
  75. # CHECK PARAMETERS
  76. #=================================================
  77. # Check arguments
  78. if [[ -z $wifi_ssid || -z $wifi_passphrase ]]; then
  79. ynh_die "Your Wifi Hotspot needs a name and a password"
  80. fi
  81. # Check passphrase length
  82. wifi_passphrase_length="$(echo -n "${wifi_passphrase}" | wc -c)"
  83. if [[ $wifi_passphrase_length -lt 8 || $wifi_passphrase_length -gt 63 ]]; then
  84. ynh_die "Your password must from 8 to 63 characters (WPA2 passphrase)"
  85. fi
  86. # Check no special characters are present in the passphrase
  87. if [[ $wifi_passphrase =~ [^[:print:]] ]]; then
  88. ynh_die "Only printable ASCII characters are permitted in your password (WPA2 passphrase)"
  89. fi
  90. #=================================================
  91. # INSTALL NONFREE FIRWARE IF REQUESTED
  92. #=================================================
  93. export DEBIAN_FRONTEND=noninteractive
  94. # Packaged USB Wireless Device firmwares
  95. # Based on https://wiki.debian.org/WiFi#USB_Devices
  96. if [[ $firmware_nonfree == yes ]]; then
  97. # check if non-free is set on sources.list
  98. if ! grep -q non-free /etc/apt/sources.list ; then
  99. sed '/debian/{s/main/& non-free/}' -i /etc/apt/sources.list
  100. fi
  101. packages=$nonfree_packages
  102. else
  103. packages=$free_packages
  104. # Extract from http://packages.trisquel.info/toutatis-updates/open-ath9k-htc-firmware
  105. # https://www.fsf.org/news/ryf-certification-thinkpenguin-usb-with-atheros-chip
  106. # https://wiki.debian.org/ath9k_htc/open_firmware
  107. mkdir -p /lib/firmware
  108. install -b -o root -g root -m 0644 ../conf/firmware_htc-7010.fw /lib/firmware/htc_7010.fw
  109. install -b -o root -g root -m 0644 ../conf/firmware_htc-9271.fw /lib/firmware/htc_9271.fw
  110. fi
  111. apt-get --assume-yes --force-yes install ${packages}
  112. if [[ $? -ne 0 ]]; then
  113. apt-get update
  114. apt-get --assume-yes --force-yes install ${packages}
  115. fi
  116. #=================================================
  117. # CHECK PARAMETERS
  118. #=================================================
  119. if [[ ! -v ip6_net ]]; then # if ip6_net not set
  120. ip6_net=none
  121. ip6_addr=none
  122. if [[ -e /tmp/.ynh-vpnclient-started ]]; then
  123. vpnclient_ip6_net=$(sudo ynh_app_setting_set vpnclient ip6_net 2>&1)
  124. vpnclient_ip6_addr=$(sudo ynh_app_setting_set vpnclient ip6_addr 2>&1)
  125. if [[ $vpnclient_ip6_net =~ :: && $vpnclient_ip6_addr =~ :: ]]; then
  126. ip6_net=${vpnclient_ip6_net}
  127. ip6_addr=${vpnclient_ip6_addr}
  128. fi
  129. fi
  130. fi
  131. wifi_device=$(sudo bash ../conf/iw_devices | awk -F\| '{ print $1 }')
  132. # Save arguments
  133. if [[ -z $wifi_device ]]; then
  134. ynh_systemctl disable $service_name
  135. ynh_app_setting_set $app service_enabled 0
  136. wifi_device=none
  137. else
  138. ynh_systemctl enable $service_name
  139. ynh_app_setting_set $app service_enabled 1
  140. fi
  141. #=================================================
  142. # SAVE SETTINGS
  143. #=================================================
  144. ynh_app_setting_set $app multissid 1
  145. ynh_app_setting_set $app wifi_ssid "${wifi_ssid}"
  146. ynh_app_setting_set $app wifi_secure 1
  147. ynh_app_setting_set $app wifi_passphrase "${wifi_passphrase}"
  148. ynh_app_setting_set $app wifi_device "${wifi_device}"
  149. ynh_app_setting_set $app wifi_channel 6
  150. ynh_app_setting_set $app ip6_addr "${ip6_addr}"
  151. ynh_app_setting_set $app ip6_firewall 1
  152. ynh_app_setting_set $app ip6_net "${ip6_net}"
  153. ynh_app_setting_set $app ip6_dns0 2001:913::8
  154. ynh_app_setting_set $app ip6_dns1 2001:910:800::12
  155. ynh_app_setting_set $app ip4_dns0 80.67.188.188
  156. ynh_app_setting_set $app ip4_dns1 80.67.169.12
  157. ynh_app_setting_set $app ip4_nat_prefix 10.0.242
  158. ynh_app_setting_set $app vpnclient no
  159. ynh_app_setting_set $app service_name $service_name
  160. #=================================================
  161. # INSTALL CUSTOM SCRIPTS
  162. #=================================================
  163. install -o root -g root -m 0755 ../conf/iw_multissid /usr/local/bin/
  164. install -o root -g root -m 0755 ../conf/iw_devices /usr/local/bin/
  165. install -o root -g root -m 0755 ../conf/iw_ssids /usr/local/bin/
  166. install -o root -g root -m 0755 ../conf/ipv6_expanded /usr/local/bin/
  167. install -o root -g root -m 0755 ../conf/ipv6_compressed /usr/local/bin/
  168. #=================================================
  169. # COPY CONFIGS
  170. #=================================================
  171. mkdir -pm 0755 /var/log/nginx/
  172. mkdir -pm 0755 /etc/dnsmasq.dhcpd/
  173. chown root: /etc/dnsmasq.dhcpd/
  174. install -b -o root -g root -m 0644 ../conf/hostapd.conf.tpl? /etc/hostapd/
  175. install -b -o root -g root -m 0644 ../conf/dnsmasq_dhcpdv6.conf.tpl /etc/dnsmasq.dhcpd/dhcpdv6.conf.tpl
  176. install -b -o root -g root -m 0644 ../conf/dnsmasq_dhcpdv4.conf.tpl /etc/dnsmasq.dhcpd/dhcpdv4.conf.tpl
  177. install -b -o root -g root -m 0644 ../conf/nginx_wifiadmin.conf "/etc/nginx/conf.d/${domain}.d/wifiadmin.conf"
  178. install -b -o root -g root -m 0644 ../conf/phpfpm_wifiadmin.conf /etc/php5/fpm/pool.d/wifiadmin.conf
  179. #=================================================
  180. # COPY WEB SOURCES
  181. #=================================================
  182. mkdir -pm 0755 /var/www/wifiadmin/
  183. cp -a ../sources/* /var/www/wifiadmin/
  184. chown -R root: /var/www/wifiadmin/
  185. chmod -R 0644 /var/www/wifiadmin/*
  186. find /var/www/wifiadmin/ -type d -exec chmod +x {} \;
  187. #=================================================
  188. # FIX CONFIGS
  189. #=================================================
  190. ## hostapd
  191. sed 's|^DAEMON_CONF=$|&/etc/hostapd/hostapd.conf|' -i /etc/init.d/hostapd
  192. ## nginx
  193. sed "s|<TPL:NGINX_LOCATION>|${path_url}|g" -i "/etc/nginx/conf.d/${domain}.d/wifiadmin.conf"
  194. sed 's|<TPL:NGINX_REALPATH>|/var/www/wifiadmin/|g' -i "/etc/nginx/conf.d/${domain}.d/wifiadmin.conf"
  195. sed 's|<TPL:PHP_NAME>|wifiadmin|g' -i "/etc/nginx/conf.d/${domain}.d/wifiadmin.conf"
  196. ## php-fpm
  197. sed 's|<TPL:PHP_NAME>|wifiadmin|g' -i /etc/php5/fpm/pool.d/wifiadmin.conf
  198. sed 's|<TPL:PHP_USER>|admin|g' -i /etc/php5/fpm/pool.d/wifiadmin.conf
  199. sed 's|<TPL:PHP_GROUP>|admins|g' -i /etc/php5/fpm/pool.d/wifiadmin.conf
  200. sed 's|<TPL:NGINX_REALPATH>|/var/www/wifiadmin/|g' -i /etc/php5/fpm/pool.d/wifiadmin.conf
  201. # Fix sources
  202. sed "s|<TPL:NGINX_LOCATION>|${path_url}|g" -i /var/www/wifiadmin/config.php
  203. # Copy init script
  204. install -o root -g root -m 0755 ../conf/$service_name /usr/local/bin/
  205. install -o root -g root -m 0644 ../conf/$service_name.service /etc/systemd/system/
  206. # Update firewall for DHCP
  207. yunohost firewall allow --no-upnp --ipv6 UDP 547
  208. yunohost firewall allow --no-upnp UDP 67
  209. # Set default inits
  210. # The boot order of these services are important, so they are disabled by default
  211. # and the ynh-hotspot service handles them.
  212. systemctl disable hostapd
  213. systemctl stop hostapd
  214. systemctl enable php5-fpm
  215. systemctl restart php5-fpm
  216. systemctl reload nginx
  217. # Remove IPv6 address set if there is a VPN installed
  218. if [[ $ip6_addr != none ]]; then
  219. ip -6 address show dev tun0 2> /dev/null | grep -q "${ip6_addr}/"
  220. if [[ "$?" -eq 0 ]]; then
  221. ip address delete "${ip6_addr}/128" dev tun0 &> /dev/null
  222. fi
  223. fi
  224. # register the service
  225. yunohost service add $service_name --description "creates a Wi-Fi access point"
  226. # start the service if device is present
  227. if [[ $wifi_device == none ]]; then
  228. echo "WARNING: Wifi Hotspot is not started because no wifi device was found (please, check the web admin)" >&2
  229. else
  230. ynh_systemctl start $service_name
  231. fi
  232. # Reload SSOwat config
  233. yunohost app ssowatconf