config 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #!/bin/bash
  2. source _common.sh
  3. source /usr/share/yunohost/helpers
  4. #=================================================
  5. # MANAGE SCRIPT FAILURE
  6. #=================================================
  7. # Exit if an error occurs during the execution of the script
  8. #REMOVEME? ynh_abort_if_errors
  9. #=================================================
  10. # RETRIEVE ARGUMENTS
  11. #=================================================
  12. set_permissions() {
  13. local file="$1"
  14. if [ -f $file ]
  15. then
  16. chown $app:$app $file
  17. chmod go=--- $file
  18. fi
  19. }
  20. #=================================================
  21. # SPECIFIC GETTERS FOR TOML SHORT KEY
  22. #=================================================
  23. BACKTICK='`'
  24. TRIPLEBACKTICKS='```'
  25. get__status() {
  26. local service_enabled=$(ynh_app_setting_get $app service_enabled)
  27. ipv4=$(ping -w3 -c1 ip.yunohost.org >/dev/null 2>&1 && curl --max-time 5 https://ip.yunohost.org --silent)
  28. ipv6=$(ping -w3 -c1 ip6.yunohost.org >/dev/null 2>&1 && curl --max-time 5 https://ip6.yunohost.org --silent)
  29. if ip route get 1.2.3.4 | grep -q tun0 && [[ -n "$ipv4" ]]
  30. then
  31. if [ $service_enabled -eq 1 ]
  32. then
  33. cat << EOF
  34. style: success
  35. ask:
  36. en: |-
  37. The VPN is enabled and running ! :)
  38. **IPv4:** $BACKTICK$ipv4$BACKTICK
  39. **IPv6:** $BACKTICK$ipv6$BACKTICK
  40. EOF
  41. else
  42. cat << EOF
  43. style: warning
  44. ask:
  45. en: The VPN is running, but it shouldn't !?
  46. EOF
  47. fi
  48. elif [ $service_enabled -eq 1 ]
  49. then
  50. cat << EOF
  51. style: danger
  52. ask:
  53. en: |-
  54. The VPN is down ! Here are errors logged in the last few minutes
  55. $TRIPLEBACKTICKS
  56. $(journalctl -u ynh-vpnclient -o cat | sed 's/^/ /g' | tail -n 15)
  57. $TRIPLEBACKTICKS
  58. EOF
  59. else
  60. cat << EOF
  61. style: info
  62. ask:
  63. en: The VPN is not enabled
  64. EOF
  65. fi
  66. }
  67. get__login_user() {
  68. if [ -s /etc/openvpn/keys/credentials ]
  69. then
  70. echo "$(sed -n 1p /etc/openvpn/keys/credentials)"
  71. else
  72. echo ""
  73. fi
  74. }
  75. get__login_passphrase() {
  76. if [ -s /etc/openvpn/keys/credentials ]
  77. then
  78. echo "$(sed -n 2p /etc/openvpn/keys/credentials)"
  79. else
  80. echo ""
  81. fi
  82. }
  83. #=================================================
  84. # SPECIFIC VALIDATORS FOR TOML SHORT KEYS
  85. #=================================================
  86. validate__login_user() {
  87. if grep -q '^\s*auth-user-pass' ${config_file}
  88. then
  89. if [[ -z "${login_user}" ]]
  90. then
  91. echo 'A Username is needed with this configuration file'
  92. fi
  93. fi
  94. }
  95. validate__login_passphrase() {
  96. if grep -q '^\s*auth-user-pass' ${config_file}
  97. then
  98. if [[ -z "${login_passphrase}" ]]
  99. then
  100. echo 'A Password is needed with this configuration file'
  101. fi
  102. fi
  103. }
  104. validate__crt_server_ca() {
  105. if grep -q '^\s*ca\s' ${config_file}
  106. then
  107. if [[ ! -e "${crt_server_ca}" ]]
  108. then
  109. echo "A server CA certificate is needed"
  110. fi
  111. fi
  112. }
  113. validate__crt_client() {
  114. if grep -q '^\s*cert\s' ${config_file}
  115. then
  116. if [[ ! -e "${crt_client}" ]]
  117. then
  118. echo "A Client certificate is needed with this configuration file"
  119. fi
  120. fi
  121. }
  122. validate__crt_client_key() {
  123. if grep -q '^\s*key\s' ${config_file}
  124. then
  125. if [[ ! -e "${crt_client_key}" ]]
  126. then
  127. echo "A client private key is needed with this configuration file"
  128. fi
  129. fi
  130. }
  131. validate__crt_client_ta() {
  132. if grep -q '^\s*tls-auth\s' ${config_file}
  133. then
  134. if [[ ! -e "${crt_client_ta}" ]]
  135. then
  136. echo "A TLS auth shared secret is needed with this configuration file"
  137. fi
  138. fi
  139. }
  140. validate__nameservers() {
  141. if [[ "$dns_method" == "custom" ]] && [[ -z "$nameservers" ]]
  142. then
  143. echo "You need to choose DNS resolvers or select an other method to provide DNS resolvers"
  144. fi
  145. }
  146. #=================================================
  147. # SPECIFIC SETTERS FOR TOML SHORT KEYS
  148. #=================================================
  149. set__login_user() {
  150. if [ -n "${login_user}" ]
  151. then
  152. echo "${login_user}" > /etc/openvpn/keys/credentials
  153. echo "${login_passphrase}" >> /etc/openvpn/keys/credentials
  154. set_permissions /etc/openvpn/keys/credentials
  155. else
  156. echo "" > /etc/openvpn/keys/credentials
  157. fi
  158. }
  159. set__login_passphrase() {
  160. :
  161. }
  162. #=================================================
  163. # OVERWRITING VALIDATE STEP
  164. #=================================================
  165. ynh_app_config_validate() {
  166. # At this moment this var is not already set with the old value
  167. if [ -z ${config_file+x} ]
  168. then
  169. config_file="${old[config_file]}"
  170. # Overwrite form response with cube files data before validation process
  171. # We don't have the extension, so we use this ugly hack to check that this is a json-like
  172. # (i.e. it starts with { ..)
  173. elif [ -f "${config_file}" ] && [[ "$(cat ${config_file} | tr -d ' ' | grep -v "^$" | head -c1)" == "{" ]]
  174. then
  175. local tmp_dir=$(dirname "$config_file")
  176. cube_file="$tmp_dir/client.cube"
  177. cp -f "$config_file" "$cube_file"
  178. convert_cube_file "$config_file"
  179. # Othewise, assume that it's a .ovpn / .conf
  180. elif [ -f "${config_file}" ]
  181. then
  182. local tmp_dir=$(dirname "$config_file")
  183. ovpn_file="$tmp_dir/client.ovpn"
  184. cp -f "$config_file" "$ovpn_file"
  185. convert_ovpn_file "$config_file"
  186. fi
  187. _ynh_app_config_validate
  188. }
  189. #=================================================
  190. # OVERWRITING APPLY STEP
  191. #=================================================
  192. ynh_app_config_apply() {
  193. # Stop vpn client
  194. ynh_print_info "Stopping vpnclient in order to edit files"
  195. ynh_systemctl --service="ynh-vpnclient-checker.timer" --action="stop"
  196. ynh_systemctl --service="ynh-vpnclient" --action="stop"
  197. chown $app:$app /etc/openvpn/keys
  198. chmod go=--- /etc/openvpn/keys
  199. _ynh_app_config_apply
  200. set_permissions /etc/openvpn/client.conf
  201. set_permissions /etc/openvpn/keys/ca-server.crt
  202. set_permissions /etc/openvpn/keys/user.crt
  203. set_permissions /etc/openvpn/keys/user.key
  204. set_permissions /etc/openvpn/keys/user_ta.key
  205. # Cleanup previously uploaded config file
  206. [[ "$cube_file" == "/etc/openvpn/client.cube" ]] && rm -f "$cube_file"
  207. [[ "$ovpn_file" == "/etc/openvpn/client.ovpn" ]] && rm -f "$ovpn_file"
  208. # Start vpn client
  209. ynh_print_info "Starting vpnclient service if needed"
  210. ynh_systemctl --service="ynh-vpnclient" --action="start"
  211. ynh_systemctl --service="ynh-vpnclient-checker.timer" --action="start"
  212. }
  213. ynh_app_config_run $1