config 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. set_right() {
  19. if [ -f $1 ]
  20. then
  21. chown $app:$app $1
  22. chmod go=--- $1
  23. fi
  24. }
  25. #=================================================
  26. # SPECIFIC GETTERS FOR TOML SHORT KEY
  27. #=================================================
  28. get__status() {
  29. local service_enabled=$(ynh_app_setting_get $app service_enabled)
  30. if ip route get 1.2.3.4 | grep -q tun0
  31. then
  32. if [ $service_enabled -eq 1 ]
  33. then
  34. cat << EOF
  35. style: success
  36. ask:
  37. en: |-
  38. Your VPN is running :)
  39. **IPv4:** \`$(curl https://ip.yunohost.org --silent)\`
  40. **IPv6:** \`$(curl https://ip6.yunohost.org --silent)\`
  41. EOF
  42. else
  43. cat << EOF
  44. style: warning
  45. ask:
  46. en: Your VPN is running, but it shouldn't !
  47. EOF
  48. fi
  49. elif [ $service_enabled -eq 1 ]
  50. then
  51. cat << EOF
  52. style: danger
  53. ask:
  54. en: |-
  55. Your VPN is down ! Here are errors logged in the last 5 minutes
  56. \`\`\`
  57. $(journalctl -u openvpn@client -p0..3 --since "- 20 minutes" -o cat | sed 's/^/ /g' | tail -n 15)
  58. \`\`\`
  59. EOF
  60. else
  61. cat << EOF
  62. style: info
  63. ask:
  64. en: Your VPN is down has expected.
  65. EOF
  66. fi
  67. }
  68. get__login_user() {
  69. if [ -s /etc/openvpn/keys/credentials ]
  70. then
  71. echo "$(sed -n 1p /etc/openvpn/keys/credentials)"
  72. else
  73. echo ""
  74. fi
  75. }
  76. get__login_passphrase() {
  77. if [ -s /etc/openvpn/keys/credentials ]
  78. then
  79. echo "$(sed -n 2p /etc/openvpn/keys/credentials)"
  80. else
  81. echo ""
  82. fi
  83. }
  84. #=================================================
  85. # SPECIFIC VALIDATORS FOR TOML SHORT KEYS
  86. #=================================================
  87. validate__login_user() {
  88. if grep -q '^\s*auth-user-pass' ${config_file}
  89. then
  90. if [[ -z "${login_user}" ]]
  91. then
  92. echo 'A Username is needed with this configuration file'
  93. fi
  94. fi
  95. }
  96. #=================================================
  97. # SPECIFIC SETTERS FOR TOML SHORT KEYS
  98. #=================================================
  99. set__login_user() {
  100. if [ -n "${login_user}" ]
  101. then
  102. echo "${login_user}\n${login_passphrase}" > /etc/openvpn/keys/credentials
  103. set_right /etc/openvpn/keys/credentials
  104. else
  105. echo "" > /etc/openvpn/keys/credentials
  106. fi
  107. }
  108. set__login_passphrase() {
  109. :
  110. }
  111. #=================================================
  112. # OVERWRITING VALIDATE STEP
  113. #=================================================
  114. read_cube() {
  115. tmp_dir=$(dirname "$1")
  116. setting_value="$(jq --raw-output ".$2" "$1")"
  117. if [[ "$setting_value" == "null" ]]
  118. then
  119. setting_value=''
  120. # Save file in tmp dir
  121. elif [[ "$2" == "crt_"* ]]
  122. then
  123. if [ -n "${setting_value}" ]
  124. then
  125. echo "${setting_value}" | sed 's/|/\n/g' > $tmp_dir/$2
  126. setting_value="$tmp_dir/$2"
  127. fi
  128. fi
  129. echo $setting_value
  130. }
  131. ynh_app_config_validate() {
  132. _ynh_app_config_validate
  133. }
  134. #=================================================
  135. # OVERWRITING APPLY STEP
  136. #=================================================
  137. ynh_app_config_apply() {
  138. # Stop vpn client
  139. ynh_print_info --message="Stopping vpnclient in order to edit files"
  140. touch /tmp/.ynh-vpnclient-stopped
  141. /usr/local/bin/ynh-vpnclient stop
  142. chown $app:$app /etc/openvpn/keys
  143. chmod go=--- /etc/openvpn/keys
  144. _ynh_app_config_apply
  145. set_right /etc/openvpn/client.conf
  146. set_right /etc/openvpn/keys/ca-server.crt
  147. set_right /etc/openvpn/keys/user.crt
  148. set_right /etc/openvpn/keys/user.key
  149. set_right /etc/openvpn/keys/user_ta.key
  150. # Start vpn client
  151. ynh_print_info --message="Starting vpnclient service if needed"
  152. /usr/local/bin/ynh-vpnclient start
  153. rm -f /tmp/.ynh-vpnclient-stopped
  154. }
  155. ynh_app_config_run $1