config 7.5 KB

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