install 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. ynh_print_info "Validating installation parameters..."
  46. # Check destination directory
  47. final_path="/var/www/$app"
  48. test ! -e "$final_path" || ynh_die "This path already contains a folder"
  49. # Normalize the url path syntax
  50. path_url=$(ynh_normalize_url_path $path_url)
  51. # Register (book) web path
  52. ynh_webpath_register $app $domain $path_url
  53. # If we're on armbian, force $firmware_nonfree
  54. # because armbian-firmware conflicts with the non-free packages ...
  55. if dpkg --list | grep -q armbian-firmware; then
  56. echo "You are running Armbian and non-free firmware are known to conflict with armbian-firwmare. " >&2
  57. firmware_nonfree=0
  58. echo "Variable firmware_non_free has been forced to 'no'" >&2
  59. fi
  60. if [ $firmware_nonfree = "no" ]; then
  61. firmware_nonfree=0
  62. elif [ $firmware_nonfree = "yes" ]; then
  63. firmware_nonfree=1
  64. fi
  65. # Check arguments
  66. if [[ -z $wifi_ssid || -z $wifi_passphrase ]]; then
  67. ynh_die "Your Wifi Hotspot needs a name and a password"
  68. fi
  69. # Check passphrase length
  70. wifi_passphrase_length="$(echo -n "${wifi_passphrase}" | wc -c)"
  71. if [[ $wifi_passphrase_length -lt 8 || $wifi_passphrase_length -gt 63 ]]; then
  72. ynh_die "Your password must from 8 to 63 characters (WPA2 passphrase)"
  73. fi
  74. # Check no special characters are present in the passphrase
  75. if [[ $wifi_passphrase =~ [^[:print:]] ]]; then
  76. ynh_die "Only printable ASCII characters are permitted in your password (WPA2 passphrase)"
  77. fi
  78. #=================================================
  79. # RUN PREREQUISITES
  80. #=================================================
  81. source ./prerequisites
  82. #=================================================
  83. # STORE SETTINGS FROM MANIFEST
  84. #=================================================
  85. ynh_print_info "Storing installation settings..."
  86. ynh_app_setting_set "$app" domain "$domain"
  87. ynh_app_setting_set "$app" final_path "$final_path"
  88. ynh_app_setting_set "$app" wifi_ssid "$wifi_ssid"
  89. ynh_app_setting_set "$app" wifi_passphrase "$wifi_passphrase"
  90. ynh_app_setting_set "$app" firmware_nonfree "$firmware_nonfree"
  91. #=================================================
  92. # STANDARD MODIFICATIONS
  93. #=================================================
  94. # FIND AND OPEN A PORT
  95. #=================================================
  96. ynh_print_info "Configuring firewall..."
  97. # Update firewall for DHCP
  98. ynh_exec_warn_less yunohost firewall allow --no-upnp --ipv6 UDP 547
  99. ynh_exec_warn_less yunohost firewall allow --no-upnp UDP 67
  100. #=================================================
  101. # INSTALL NONFREE FIRWARE IF REQUESTED
  102. #=================================================
  103. export DEBIAN_FRONTEND=noninteractive
  104. # Packaged USB Wireless Device firmwares
  105. # Based on https://wiki.debian.org/WiFi#USB_Devices
  106. if [[ $firmware_nonfree -eq 1 ]]; then
  107. # check if non-free is set on sources.list
  108. if ! grep -q non-free /etc/apt/sources.list ; then
  109. sed '/debian/{s/main/& non-free/}' -i /etc/apt/sources.list
  110. fi
  111. packages=$nonfree_packages
  112. else
  113. packages=$free_packages
  114. # Extract from http://packages.trisquel.info/toutatis-updates/open-ath9k-htc-firmware
  115. # https://www.fsf.org/news/ryf-certification-thinkpenguin-usb-with-atheros-chip
  116. # https://wiki.debian.org/ath9k_htc/open_firmware
  117. mkdir -p /lib/firmware
  118. install -b -o root -g root -m 0644 ../conf/firmware_htc-7010.fw /lib/firmware/htc_7010.fw
  119. install -b -o root -g root -m 0644 ../conf/firmware_htc-9271.fw /lib/firmware/htc_9271.fw
  120. fi
  121. #=================================================
  122. # INSTALL DEPENDENCIES
  123. #=================================================
  124. ynh_print_info "Installing dependencies..."
  125. ynh_install_app_dependencies "$pkg_dependencies" "$packages"
  126. #=================================================
  127. # NGINX CONFIGURATION
  128. #=================================================
  129. ynh_print_info "Configuring nginx web server..."
  130. # Create a dedicated nginx config
  131. ynh_add_nginx_config
  132. #=================================================
  133. # CREATE DEDICATED USER
  134. #=================================================
  135. ynh_print_info "Configuring system user..."
  136. # Create a system user
  137. ynh_system_user_create $app
  138. # Ensure the system user has enough sudo permissions
  139. install -b -o root -g root -m 0440 ../conf/sudoers.conf /etc/sudoers.d/${app}_ynh
  140. ynh_replace_string "__HOTSPOT_SYSUSER__" "${app}" /etc/sudoers.d/${app}_ynh
  141. #=================================================
  142. # PHP-FPM CONFIGURATION
  143. #=================================================
  144. ynh_print_info "Configuring php-fpm..."
  145. # Create a dedicated php-fpm config
  146. ynh_add_fpm_config
  147. #=================================================
  148. # INSTALL CUSTOM SCRIPTS
  149. #=================================================
  150. install -o root -g root -m 0755 ../conf/iw_multissid /usr/local/bin/
  151. install -o root -g root -m 0755 ../conf/iw_devices /usr/local/bin/
  152. install -o root -g root -m 0755 ../conf/iw_ssids /usr/local/bin/
  153. install -o root -g root -m 0755 ../conf/ipv6_expanded /usr/local/bin/
  154. install -o root -g root -m 0755 ../conf/ipv6_compressed /usr/local/bin/
  155. if [[ ! -v ip6_net ]]; then # if ip6_net not set
  156. ip6_net=none
  157. ip6_addr=none
  158. if [[ -e /tmp/.ynh-vpnclient-started ]]; then
  159. vpnclient_ip6_net=$(ynh_app_setting_get vpnclient ip6_net 2>&1)
  160. vpnclient_ip6_addr=$(ynh_app_setting_get vpnclient ip6_addr 2>&1)
  161. if [[ $vpnclient_ip6_net =~ :: && $vpnclient_ip6_addr =~ :: ]]; then
  162. ip6_net=${vpnclient_ip6_net}
  163. ip6_addr=${vpnclient_ip6_addr}
  164. fi
  165. fi
  166. fi
  167. wifi_device=$(bash ../conf/iw_devices | awk -F\| '{ print $1 }')
  168. ynh_app_setting_set $app multissid 1
  169. ynh_app_setting_set $app wifi_ssid "${wifi_ssid}"
  170. ynh_app_setting_set $app wifi_secure 1
  171. ynh_app_setting_set $app wifi_passphrase "${wifi_passphrase}"
  172. ynh_app_setting_set $app wifi_device "${wifi_device}"
  173. ynh_app_setting_set $app wifi_channel 6
  174. ynh_app_setting_set $app ip6_addr "${ip6_addr}"
  175. ynh_app_setting_set $app ip6_firewall 1
  176. ynh_app_setting_set $app ip6_net "${ip6_net}"
  177. ynh_app_setting_set $app ip6_dns0 2001:913::8
  178. ynh_app_setting_set $app ip6_dns1 2001:910:800::12
  179. ynh_app_setting_set $app ip4_dns0 80.67.188.188
  180. ynh_app_setting_set $app ip4_dns1 80.67.169.12
  181. ynh_app_setting_set $app ip4_nat_prefix 10.0.242
  182. ynh_app_setting_set $app vpnclient no
  183. ynh_app_setting_set $app service_name $service_name
  184. if [[ -z $wifi_device ]]; then
  185. ynh_app_setting_set $app service_enabled 0
  186. wifi_device=none
  187. else
  188. ynh_app_setting_set $app service_enabled 1
  189. fi
  190. #=================================================
  191. # COPY CONFIGS
  192. #=================================================
  193. mkdir -pm 0755 /etc/dnsmasq.dhcpd/
  194. chown root: /etc/dnsmasq.dhcpd/
  195. install -b -o root -g root -m 0644 ../conf/hostapd.conf.tpl? /etc/hostapd/
  196. install -b -o root -g root -m 0644 ../conf/dnsmasq_dhcpdv6.conf.tpl /etc/dnsmasq.dhcpd/dhcpdv6.conf.tpl
  197. install -b -o root -g root -m 0644 ../conf/dnsmasq_dhcpdv4.conf.tpl /etc/dnsmasq.dhcpd/dhcpdv4.conf.tpl
  198. # Copy init script
  199. install -o root -g root -m 0755 ../conf/$service_name /usr/local/bin/
  200. #=================================================
  201. # COPY WEB SOURCES
  202. #=================================================
  203. mkdir -pm 0755 ${final_path}/
  204. cp -a ../sources/* ${final_path}/
  205. chown -R $app: ${final_path}/
  206. chmod -R 0644 ${final_path}/*
  207. find ${final_path}/ -type d -exec chmod +x {} \;
  208. #=================================================
  209. # FIX CONFIGS
  210. #=================================================
  211. ## hostapd
  212. ynh_replace_string "^DAEMON_CONF=$" "&/etc/hostapd/hostapd.conf" /etc/init.d/hostapd
  213. # We also need to put this in /etc/default/hostapd because on some setup
  214. # like RPi, the version of hostapd is different and /etc/init.d/hostapd
  215. # isnt used ... instead the service is "pure systemd" ...
  216. echo "DAEMON_CONF=/etc/hostapd/hostapd.conf" > /etc/default/hostapd
  217. # Fix sources
  218. ynh_replace_string "__PATH__" "${path_url}" "$final_path/config.php"
  219. #=================================================
  220. # STORE THE CONFIG FILE CHECKSUM
  221. #=================================================
  222. # Calculate and store the config file checksum into the app settings
  223. ynh_store_file_checksum "$final_path/config.php"
  224. ynh_store_file_checksum "/etc/init.d/hostapd"
  225. #=================================================
  226. # SETUP SYSTEMD
  227. #=================================================
  228. ynh_print_info "Configuring a systemd service..."
  229. # Create a dedicated systemd config
  230. ynh_add_systemd_config $service_name
  231. # Set default inits
  232. # The boot order of these services are important, so they are disabled by default
  233. # and the ynh-hotspot service handles them.
  234. systemctl disable hostapd
  235. systemctl stop hostapd
  236. systemctl unmask hostapd # On some system e.g. RPi, for some reason hostapd is masked after install ...
  237. #=================================================
  238. # RELOAD NGINX AND PHP-FPM
  239. #=================================================
  240. ynh_print_info "Reloading nginx web server..."
  241. systemctl restart php7.0-fpm
  242. systemctl reload nginx
  243. # Remove IPv6 address set if there is a VPN installed
  244. if [[ $ip6_addr != none ]]; then
  245. if ip -6 address show dev tun0 2> /dev/null | grep -q "${ip6_addr}/"; then
  246. ip address delete "${ip6_addr}/128" dev tun0 &> /dev/null
  247. fi
  248. fi
  249. # register the service
  250. yunohost service add $service_name --description "Creates a Wi-Fi access point" --need_lock
  251. # enable and start the service if device is present
  252. if [[ $wifi_device == none ]]; then
  253. echo "WARNING: Wifi Hotspot is not started because no wifi device was found (please, check the web admin)" >&2
  254. else
  255. yunohost service enable $service_name
  256. yunohost service start $service_name
  257. fi
  258. #=================================================
  259. # END OF SCRIPT
  260. #=================================================
  261. ynh_print_info "Installation of $app completed"