config 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 --key="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 [[ -n "${config_file:-}" ]]; then
  168. # Overwrite form response with cube files data before validation process
  169. # We don't have the extension, so we use this ugly hack to check that this is a json-like
  170. # (i.e. it starts with { ..)
  171. if [[ -f "${config_file}" ]]; then
  172. if [[ "$(cat ${config_file} | tr -d ' ' | grep -v "^$" | head -c1)" == "{" ]]; then
  173. local tmp_dir=$(dirname "$config_file")
  174. cube_file="$tmp_dir/client.cube"
  175. cp -f "$config_file" "$cube_file"
  176. convert_cube_file "$config_file"
  177. # Othewise, assume that it's a .ovpn / .conf
  178. else
  179. local tmp_dir=$(dirname "$config_file")
  180. ovpn_file="$tmp_dir/client.ovpn"
  181. cp -f "$config_file" "$ovpn_file"
  182. convert_ovpn_file "$config_file"
  183. fi
  184. fi
  185. fi
  186. _ynh_app_config_validate
  187. }
  188. #=================================================
  189. # OVERWRITING APPLY STEP
  190. #=================================================
  191. ynh_app_config_apply() {
  192. # Stop vpn client
  193. ynh_print_info "Stopping vpnclient in order to edit files"
  194. ynh_systemctl --service="ynh-vpnclient-checker.timer" --action="stop"
  195. ynh_systemctl --service="ynh-vpnclient" --action="stop"
  196. chown $app:$app /etc/openvpn/keys
  197. chmod go=--- /etc/openvpn/keys
  198. _ynh_app_config_apply
  199. # If we are uploading a cube file, then the file would be in a temporary folder
  200. # Otherwise, we aren't uploading a cube file, then the path is either empty
  201. # or takes the value of the previous upload, that is, the target path for the cube file.
  202. if [[ -n "${cube_file:-}" && "$cube_file" != "/etc/openvpn/client.cube" ]]; then
  203. ynh_app_setting_set --key="ip6_addr" --value="$ip6_addr"
  204. ynh_app_setting_set --key="ip6_net" --value="$ip6_net"
  205. ynh_app_setting_set --key="ip6_send_over_tun_enabled" --value"$ip6_send_over_tun_enabled"
  206. fi
  207. if [[ ${ip6_send_over_tun_enabled} -eq 1 ]]; then
  208. install -b -o root -g root -m 0755 ../conf/optional-scripts/route-up.d/50-vpnclient-set-ipv6-send-over-tun /etc/openvpn/scripts/route-up.d/
  209. install -b -o root -g root -m 0755 ../conf/optional-scripts/route-down.d/50-vpnclient-unset-ipv6-send-over-tun /etc/openvpn/scripts/route-down.d/
  210. else
  211. ynh_safe_rm /etc/openvpn/scripts/route-up.d/50-vpnclient-set-ipv6-send-over-tun
  212. ynh_safe_rm /etc/openvpn/scripts/route-down.d/50-vpnclient-unset-ipv6-send-over-tun
  213. fi
  214. set_permissions /etc/openvpn/client.conf
  215. set_permissions /etc/openvpn/keys/ca-server.crt
  216. set_permissions /etc/openvpn/keys/user.crt
  217. set_permissions /etc/openvpn/keys/user.key
  218. set_permissions /etc/openvpn/keys/user_ta.key
  219. # Cleanup previously uploaded config file
  220. [[ -n "${cube_file:-}" && "$cube_file" == "/etc/openvpn/client.cube" ]] && rm -f "$cube_file"
  221. [[ -n "${ovpn_file:-}" && "$ovpn_file" == "/etc/openvpn/client.ovpn" ]] && rm -f "$ovpn_file"
  222. # Start vpn client
  223. ynh_print_info "Starting vpnclient service if needed"
  224. ynh_systemctl --service="ynh-vpnclient" --action="start"
  225. ynh_systemctl --service="ynh-vpnclient-checker.timer" --action="start"
  226. }
  227. ynh_app_config_run $1