install 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. domain=$YNH_APP_ARG_DOMAIN
  34. path_url=$YNH_APP_ARG_PATH
  35. wifi_ssid=$YNH_APP_ARG_WIFI_SSID
  36. wifi_passphrase=$YNH_APP_ARG_WIFI_PASSPHRASE
  37. firmware_nonfree=$YNH_APP_ARG_FIRMWARE_NONFREE
  38. app=$YNH_APP_INSTANCE_NAME
  39. # the service name must match the service template files
  40. service_name='ynh-hotspot'
  41. #=================================================
  42. # CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
  43. #=================================================
  44. ynh_script_progression --message="Validating installation parameters..."
  45. final_path=/var/www/$app
  46. test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
  47. # Register (book) web path
  48. ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url
  49. if [ $firmware_nonfree = "no" ]; then
  50. firmware_nonfree=0
  51. elif [ $firmware_nonfree = "yes" ]; then
  52. firmware_nonfree=1
  53. fi
  54. # Check arguments
  55. if [[ -z $wifi_ssid || -z $wifi_passphrase ]]; then
  56. ynh_die --message="Your Wifi Hotspot needs a name and a password"
  57. fi
  58. # Check passphrase length
  59. wifi_passphrase_length="$(echo -n "${wifi_passphrase}" | wc -c)"
  60. if [[ $wifi_passphrase_length -lt 8 || $wifi_passphrase_length -gt 63 ]]; then
  61. ynh_die --message="Your password must from 8 to 63 characters (WPA2 passphrase)"
  62. fi
  63. # Check no special characters are present in the passphrase
  64. if [[ $wifi_passphrase =~ [^[:print:]] ]]; then
  65. ynh_die --message="Only printable ASCII characters are permitted in your password (WPA2 passphrase)"
  66. fi
  67. #=================================================
  68. # STORE SETTINGS FROM MANIFEST
  69. #=================================================
  70. ynh_script_progression --message="Storing installation settings..."
  71. ynh_app_setting_set --app=$app --key=domain --value="$domain"
  72. ynh_app_setting_set --app=$app --key=wifi_ssid --value="$wifi_ssid"
  73. ynh_app_setting_set --app=$app --key=wifi_passphrase --value="$wifi_passphrase"
  74. ynh_app_setting_set --app=$app --key=firmware_nonfree --value="$firmware_nonfree"
  75. ynh_app_setting_set --app=$app --key=service_name --value=$service_name
  76. #=================================================
  77. # STANDARD MODIFICATIONS
  78. #=================================================
  79. # FIND AND OPEN A PORT
  80. #=================================================
  81. ynh_script_progression --message="Configuring firewall..."
  82. # Update firewall for DHCP
  83. ynh_exec_warn_less yunohost firewall allow --no-upnp --ipv6 UDP 547
  84. ynh_exec_warn_less yunohost firewall allow --no-upnp UDP 67
  85. #=================================================
  86. # INSTALL NONFREE FIRWARE IF REQUESTED
  87. #=================================================
  88. ynh_script_progression --message="Installing firmware..."
  89. export DEBIAN_FRONTEND=noninteractive
  90. # Packaged USB Wireless Device firmwares
  91. # Based on https://wiki.debian.org/WiFi#USB_Devices
  92. if [[ $firmware_nonfree -eq 1 ]]; then
  93. check_armbian_nonfree_conflict
  94. ynh_install_extra_app_dependencies --repo="deb http://deb.debian.org/debian $(ynh_get_debian_release) non-free" --package="$nonfree_firmware_packages"
  95. else
  96. pkg_dependencies="$pkg_dependencies $free_firmware_packages"
  97. fi
  98. #=================================================
  99. # INSTALL DEPENDENCIES
  100. #=================================================
  101. ynh_script_progression --message="Installing dependencies"
  102. ynh_install_app_dependencies "$pkg_dependencies"
  103. #=================================================
  104. # CREATE DEDICATED USER
  105. #=================================================
  106. ynh_script_progression --message="Configuring system user..."
  107. # Create a system user
  108. ynh_system_user_create --username=$app
  109. # Ensure the system user has enough sudo permissions
  110. install -b -o root -g root -m 0440 ../conf/sudoers.conf /etc/sudoers.d/${app}_ynh
  111. ynh_replace_string --match_string="__HOTSPOT_SYSUSER__" --replace_string="${app}" --target_file="/etc/sudoers.d/${app}_ynh"
  112. #=================================================
  113. # INSTALL CUSTOM SCRIPTS
  114. #=================================================
  115. ynh_script_progression --message="Installing custom script..."
  116. install -o root -g root -m 0755 ../conf/iw_multissid /usr/local/bin/
  117. install -o root -g root -m 0755 ../conf/iw_devices /usr/local/bin/
  118. install -o root -g root -m 0755 ../conf/iw_ssids /usr/local/bin/
  119. install -o root -g root -m 0755 ../conf/ipv6_expanded /usr/local/bin/
  120. install -o root -g root -m 0755 ../conf/ipv6_compressed /usr/local/bin/
  121. if [[ ! -v ip6_net ]]; then # if ip6_net not set
  122. ip6_net=none
  123. ip6_addr=none
  124. if [[ -e /tmp/.ynh-vpnclient-started ]]; then
  125. vpnclient_ip6_net=$(ynh_app_setting_get vpnclient ip6_net 2>&1)
  126. vpnclient_ip6_addr=$(ynh_app_setting_get vpnclient ip6_addr 2>&1)
  127. if [[ $vpnclient_ip6_net =~ :: && $vpnclient_ip6_addr =~ :: ]]; then
  128. ip6_net=${vpnclient_ip6_net}
  129. ip6_addr=${vpnclient_ip6_addr}
  130. fi
  131. fi
  132. fi
  133. hot_reload_usb_wifi_cards
  134. wifi_device=$(bash ../conf/iw_devices | awk -F\| '{ print $1 }')
  135. ynh_app_setting_set --app=$app --key=multissid --value=1
  136. ynh_app_setting_set --app=$app --key=wifi_ssid --value="${wifi_ssid}"
  137. ynh_app_setting_set --app=$app --key=wifi_secure --value=1
  138. ynh_app_setting_set --app=$app --key=wifi_passphrase --value="${wifi_passphrase}"
  139. ynh_app_setting_set --app=$app --key=wifi_device --value="${wifi_device}"
  140. ynh_app_setting_set --app=$app --key=wifi_channel --value=6
  141. ynh_app_setting_set --app=$app --key=ip6_addr --value="${ip6_addr}"
  142. ynh_app_setting_set --app=$app --key=ip6_firewall --value=1
  143. ynh_app_setting_set --app=$app --key=ip6_net --value="${ip6_net}"
  144. ynh_app_setting_set --app=$app --key=ip6_dns0 --value=2001:913::8
  145. ynh_app_setting_set --app=$app --key=ip6_dns1 --value=2001:910:800::12
  146. ynh_app_setting_set --app=$app --key=ip4_dns0 --value=80.67.188.188
  147. ynh_app_setting_set --app=$app --key=ip4_dns1 --value=80.67.169.12
  148. ynh_app_setting_set --app=$app --key=ip4_nat_prefix --value=10.0.242
  149. ynh_app_setting_set --app=$app --key=vpnclient --value=no
  150. if [[ -z $wifi_device ]]; then
  151. ynh_app_setting_set --app=$app --key=service_enabled --value=0
  152. wifi_device=none
  153. else
  154. ynh_app_setting_set --app=$app --key=service_enabled --value=1
  155. fi
  156. #=================================================
  157. # COPY CONFIGS
  158. #=================================================
  159. ynh_script_progression --message="Copying configuration..."
  160. mkdir -pm 0755 /etc/dnsmasq.dhcpd/
  161. chown root: /etc/dnsmasq.dhcpd/
  162. install -b -o root -g root -m 0644 ../conf/hostapd.conf.tpl? /etc/hostapd/
  163. install -b -o root -g root -m 0644 ../conf/dnsmasq_dhcpdv6.conf.tpl /etc/dnsmasq.dhcpd/dhcpdv6.conf.tpl
  164. install -b -o root -g root -m 0644 ../conf/dnsmasq_dhcpdv4.conf.tpl /etc/dnsmasq.dhcpd/dhcpdv4.conf.tpl
  165. # Copy init script
  166. install -o root -g root -m 0755 ../conf/$service_name /usr/local/bin/
  167. ynh_replace_string --match_string="__PHPVERSION__" --replace_string="${YNH_PHP_VERSION}" --target_file="/usr/local/bin/$service_name"
  168. #=================================================
  169. # COPY WEB SOURCES
  170. #=================================================
  171. ynh_script_progression --message="Setting up source files..."
  172. ynh_app_setting_set --app=$app --key=final_path --value=$final_path
  173. mkdir -pm 0755 ${final_path}/
  174. cp -a ../sources/* ${final_path}/
  175. chown -R $app: ${final_path}/
  176. chmod -R 0644 ${final_path}/*
  177. find ${final_path}/ -type d -exec chmod +x {} \;
  178. #=================================================
  179. # NGINX CONFIGURATION
  180. #=================================================
  181. ynh_script_progression --message="Configuring nginx web server..."
  182. # Create a dedicated nginx config
  183. ynh_add_nginx_config
  184. #=================================================
  185. # PHP-FPM CONFIGURATION
  186. #=================================================
  187. ynh_script_progression --message="Configuring php-fpm..."
  188. # Create a dedicated php-fpm config
  189. ynh_add_fpm_config
  190. #=================================================
  191. # CONFIGURE HOSTAPD
  192. #=================================================
  193. ynh_script_progression --message="Configuring hostapd..."
  194. ## hostapd
  195. ynh_replace_string --match_string="^DAEMON_CONF=$" --replace_string="&/etc/hostapd/hostapd.conf" --target_file=/etc/init.d/hostapd
  196. # We also need to put this in /etc/default/hostapd because on some setup
  197. # like RPi, the version of hostapd is different and /etc/init.d/hostapd
  198. # isnt used ... instead the service is "pure systemd" ...
  199. echo "DAEMON_CONF=/etc/hostapd/hostapd.conf" > /etc/default/hostapd
  200. # Apply configuration
  201. ynh_add_config --template="config.php.tpl" --destination="$final_path/config.php"
  202. # Set default inits
  203. # The boot order of these services are important, so they are disabled by default
  204. # and the ynh-hotspot service handles them.
  205. systemctl disable hostapd --quiet
  206. systemctl stop hostapd
  207. systemctl unmask hostapd # On some system e.g. RPi, for some reason hostapd is masked after install ...
  208. #=================================================
  209. # STORE THE CONFIG FILE CHECKSUM
  210. #=================================================
  211. ynh_script_progression --message="Storing the config file checksum..."
  212. # Calculate and store the config file checksum into the app settings
  213. ynh_store_file_checksum --file="/etc/init.d/hostapd"
  214. #=================================================
  215. # SETUP SYSTEMD
  216. #=================================================
  217. ynh_script_progression --message="Configuring a systemd service..."
  218. # Create a dedicated systemd config
  219. ynh_add_systemd_config --service=$service_name
  220. # Remove IPv6 address set if there is a VPN installed
  221. if [[ $ip6_addr != none ]]; then
  222. if ip -6 address show dev tun0 2> /dev/null | grep -q "${ip6_addr}/"; then
  223. ip address delete "${ip6_addr}/128" dev tun0 &> /dev/null
  224. fi
  225. fi
  226. #=================================================
  227. # INTEGRATE SERVICE IN YUNOHOST
  228. #=================================================
  229. ynh_script_progression --message="Integrating service in YunoHost..."
  230. yunohost service add $service_name --description "Creates a Wi-Fi access point" --test_status "systemctl is-active hostapd"
  231. #=================================================
  232. # START SYSTEMD SERVICE
  233. #=================================================
  234. ynh_script_progression --message="Starting a systemd service..."
  235. # Start a systemd service if device is present
  236. if [[ $wifi_device == none ]]; then
  237. echo "WARNING: Wifi Hotspot is not started because no wifi device was found (please, check the web admin)" >&2
  238. else
  239. ynh_systemd_action --service_name=$service_name --action="start" --log_path=systemd #--line_match="Started YunoHost Wifi Hotspot"
  240. fi
  241. #=================================================
  242. # END OF SCRIPT
  243. #=================================================
  244. ynh_script_progression --message="Installation of $app completed"