config 7.9 KB

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