config 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #!/bin/bash
  2. #=================================================
  3. # GENERIC STARTING
  4. #=================================================
  5. # IMPORT GENERIC HELPERS
  6. #=================================================
  7. source _common.sh
  8. source /usr/share/yunohost/helpers
  9. #=================================================
  10. # MANAGE SCRIPT FAILURE
  11. #=================================================
  12. # Exit if an error occurs during the execution of the script
  13. ynh_abort_if_errors
  14. #=================================================
  15. # RETRIEVE ARGUMENTS
  16. #=================================================
  17. final_path=$(ynh_app_setting_get $app final_path)
  18. #=================================================
  19. # SPECIFIC GETTERS FOR TOML SHORT KEY
  20. #=================================================
  21. get__no_antenna() {
  22. if [[ $(iw_devices) == "" ]]
  23. then
  24. echo "value: false"
  25. else
  26. cat << EOF
  27. value: false
  28. ask: Wifi antenna correctly detected
  29. style: info
  30. EOF
  31. fi
  32. }
  33. get__status() {
  34. local service_enabled=$(ynh_app_setting_get $app service_enabled)
  35. if systemctl is-active hostapd -q
  36. then
  37. if [ $service_enabled -eq 1 ]
  38. then
  39. cat << EOF
  40. style: success
  41. ask:
  42. en: |-
  43. Your Hotspot is running :)
  44. EOF
  45. else
  46. cat << EOF
  47. style: warning
  48. ask:
  49. en: Your Hotspot is running, but it shouldn't !
  50. EOF
  51. fi
  52. elif [ $service_enabled -eq 1 ]
  53. then
  54. cat << EOF
  55. style: danger
  56. ask:
  57. en: |-
  58. Your Hotspot is down ! Here are errors logged in the last 5 minutes
  59. \`\`\`
  60. $(journalctl -u hostapd -n10 -o cat | sed 's/^/ /g')
  61. \`\`\`
  62. EOF
  63. else
  64. cat << EOF
  65. style: info
  66. ask:
  67. en: Your Hotspot is down has expected.
  68. EOF
  69. fi
  70. }
  71. get__wifi_device() {
  72. if [[ $(iw_devices) == "" ]]
  73. then
  74. echo "choices: []"
  75. else
  76. cat << EOF
  77. choices:
  78. EOF
  79. for device in $(iw_devices)
  80. do
  81. echo " $device: $device"
  82. done
  83. fi
  84. }
  85. get__array_settings() {
  86. local short_setting="${1%%__*}"
  87. local index="${1#*__}"
  88. IFS='|' read -a values <<< "$(ynh_app_setting_get $app $short_setting)"
  89. echo "value: \"${values[$(($index - 1))]:-}\""
  90. }
  91. #=================================================
  92. # SPECIFIC VALIDATORS FOR TOML SHORT KEYS
  93. #=================================================
  94. is_unique() {
  95. local short_setting="$1"
  96. local short_setting__1="$1__1"
  97. local short_setting__2="$1__2"
  98. local short_setting__3="$1__3"
  99. if [[ "${!short_setting__1}" == "${!short_setting__2}" ]]
  100. then
  101. return 1
  102. elif [ "$multissid" -ge "3" ] && [[ "${!short_setting__1}" == "${!short_setting__3}" ]]
  103. then
  104. return 1
  105. elif [ "$multissid" -ge "3" ] && [[ "${!short_setting__2}" == "${!short_setting__3}" ]]
  106. then
  107. return 1
  108. fi
  109. return 0
  110. }
  111. validate__wifi_ssid() {
  112. local wifi_ssid_var="wifi_ssid__$1"
  113. if [ "$multissid" -ge "$1" ] && [[ -z "${!wifi_ssid_var}" ]]
  114. then
  115. echo 'SSID required'
  116. fi
  117. if ! is_unique wifi_ssid
  118. then
  119. echo 'All Wifi names must be unique'
  120. fi
  121. }
  122. validate__wifi_passphrase() {
  123. local wifi_secure_var="wifi_secure__$1"
  124. local wifi_passphrase_var="wifi_passphrase__$1"
  125. if [ "$multissid" -ge "$1" ] && [[ "${!wifi_secure_var}" == "1" ]] && [[ -z "${!wifi_passphrase_var}" ]]
  126. then
  127. echo 'In WPA2 secure mode, you need to provide a passphrase'
  128. fi
  129. }
  130. validate__ip4_nat_prefix() {
  131. local ip4_nat_prefix_var="ip4_nat_prefix__$1"
  132. if [ "$multissid" -ge "$1" ] && [[ -z "${!ip4_nat_prefix_var}" ]]
  133. then
  134. echo 'Private IPv4 nat prefix required'
  135. fi
  136. if ! is_unique ip4_nat_prefix
  137. then
  138. echo 'All IPv4 prefix must be unique'
  139. fi
  140. }
  141. validate__dns() {
  142. local dns_var="dns__$1"
  143. local ip6_net_var="dns__$1"
  144. if [ "$multissid" -ge "$1" ] && ! echo "${!dns_var}" | grep -q "\."
  145. then
  146. echo 'IPv4 DNS required'
  147. fi
  148. if [ "$multissid" -ge "$1" ] && [[ -n "${!ip6_net_var}" ]] && ! echo "${!dns_var}" | grep -q ":"
  149. then
  150. echo 'IPv6 DNS required'
  151. fi
  152. }
  153. validate__array_settings() {
  154. local short_setting="${1%%__*}"
  155. local index="${1#*__}"
  156. if type -t validate__$short_setting | grep -q '^function$' 2>/dev/null;
  157. then
  158. validate__$short_setting $index
  159. fi
  160. }
  161. #=================================================
  162. # SPECIFIC SETTERS FOR TOML SHORT KEYS
  163. #=================================================
  164. set__array_settings() {
  165. local short_setting="${1%%__*}"
  166. local index="${1#*__}"
  167. local type="${types[$1]}"
  168. local value="${!1}"
  169. if [[ "$type" == "string" ]] && [ "$multissid" -lt "$index" ]
  170. then
  171. value=""
  172. fi
  173. local values="$(ynh_app_setting_get $app $short_setting | awk 'BEGIN{FS=OFS="|"} {$'$index'="'${!1}'"}'1)"
  174. ynh_app_setting_set --app=$app --key=$short_setting --value="$values"
  175. ynh_print_info --message="Configuration key '$short_setting' edited in app settings"
  176. }
  177. #=================================================
  178. # OVERWRITING VALIDATE STEP
  179. #=================================================
  180. ynh_app_config_validate() {
  181. _ynh_app_config_validate
  182. }
  183. #=================================================
  184. # OVERWRITING APPLY STEP
  185. #=================================================
  186. ynh_app_config_apply() {
  187. # Stop vpn client
  188. ynh_print_info --message="Stopping hotspot in order to edit files"
  189. /usr/local/bin/ynh-hotspot stop
  190. _ynh_app_config_apply
  191. # Start vpn client
  192. ynh_print_info --message="Starting hotspot service if needed"
  193. /usr/local/bin/ynh-hotspot start
  194. }
  195. ynh_app_config_run $1