config 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. app=$YNH_APP_INSTANCE_NAME
  18. final_path=$(ynh_app_setting_get $app final_path)
  19. #=================================================
  20. # SPECIFIC GETTERS FOR TOML SHORT KEY
  21. #=================================================
  22. get__status() {
  23. if [ -f "/sys/class/net/tun0/operstate" ] && [ "$(cat /sys/class/net/tun0/operstate)" == "up" ]
  24. then
  25. if [ $old[service_enabled] -eq 1 ]
  26. then
  27. cat << EOF
  28. style: success
  29. ask:
  30. en: Your VPN is running :)
  31. EOF
  32. else
  33. cat << EOF
  34. style: warning
  35. ask:
  36. en: Your VPN is running, but it shouldn't !
  37. EOF
  38. fi
  39. elif [ $old[service_enabled] -eq 1 ]
  40. then
  41. cat << EOF
  42. style: danger
  43. ask:
  44. en: Your VPN is down !
  45. EOF
  46. else
  47. cat << EOF
  48. style: info
  49. ask:
  50. en: Your VPN is down has expected.
  51. EOF
  52. fi
  53. }
  54. get__login_user() {
  55. if [ -s /etc/openvpn/keys/credentials ]
  56. then
  57. sed -n 1p /etc/openvpn/keys/credentials
  58. fi
  59. }
  60. get__login_passphrase() {
  61. if [ -s /etc/openvpn/keys/credentials ]
  62. then
  63. sed -n 2p /etc/openvpn/keys/credentials
  64. fi
  65. }
  66. #=================================================
  67. # SPECIFIC VALIDATORS FOR TOML SHORT KEYS
  68. #=================================================
  69. validate__login_user() {
  70. if grep -q '^\s*auth-user-pass' ${config_file}
  71. then
  72. [[ -z "${login_user}" ]] &&
  73. echo 'A Username is needed with this configuration file'
  74. fi
  75. }
  76. validate__login_passphrase() {
  77. if grep -q '^\s*auth-user-pass' ${config_file}
  78. then
  79. [[ -z "${login_passphrase}" ]] &&
  80. echo 'A Password is needed with this configuration file'
  81. fi
  82. }
  83. validate__crt_server_ca() {
  84. if grep -q '^\s*ca\s' ${config_file}
  85. then
  86. [[ ! -e "${crt_server_ca}" ]] &&
  87. echo "A server CA certificate is needed"
  88. fi
  89. }
  90. validate__crt_client() {
  91. if grep -q '^\s*cert\s' ${config_file}
  92. then
  93. [[ ! -e "${crt_client}" ]] &&
  94. echo "A Client certificate is needed with this configuration file"
  95. fi
  96. }
  97. validate__crt_client_key() {
  98. if grep -q '^\s*key\s' ${config_file}
  99. then
  100. [[ ! -e "${crt_client_key}" ]] &&
  101. echo "A client private key is needed with this configuration file"
  102. fi
  103. }
  104. validate__crt_client_ta() {
  105. if grep -q '^\s*tls-auth\s' ${config_file}
  106. then
  107. [[ ! -e "${crt_client_ta}" ]] &&
  108. echo "A TLS auth shared secret is needed with this configuration file"
  109. fi
  110. }
  111. validate__nameservers() {
  112. [[ "$dns_method" == "custom" ]] && [[ -z "$nameservers" ]]
  113. echo "You need to choose DNS resolvers or select an other method to provide DNS resolvers"
  114. }
  115. #=================================================
  116. # SPECIFIC SETTERS FOR TOML SHORT KEYS
  117. #=================================================
  118. set__login_user() {
  119. if [ -n "${login_user}" ]
  120. then
  121. echo "${login_user}\n${login_passphrase}" > /etc/openvpn/keys/credentials
  122. else
  123. echo "" > /etc/openvpn/keys/credentials
  124. fi
  125. }
  126. set__login_passphrase() {
  127. :
  128. }
  129. #=================================================
  130. # OVERWRITING VALIDATE STEP
  131. #=================================================
  132. ynh_panel_validate() {
  133. tmp_dir=$(dirname "${config_file}")
  134. # Overwrite form response with cube files data before validation process
  135. if [ -f "${config_file}" ] && [[ $config_file == *.cube ]]
  136. then
  137. declare -A settings
  138. settings[server_name]=""
  139. settings[server_port]=""
  140. settings[server_proto]=""
  141. settings[ip6_net]=""
  142. settings[login_user]=""
  143. settings[login_passphrase]=""
  144. settings[dns0]=""
  145. settings[dns1]=""
  146. settings[crt_server_ca]="file"
  147. settings[crt_client]="file"
  148. settings[crt_client_key]="file"
  149. settings[crt_client_ta]="file"
  150. for setting_name in "${!settings[@]}"
  151. do
  152. setting_value="$(jq --raw-output ".$setting_name" "${config_file}")"
  153. if [[ "$setting_value" == "null" ]]
  154. then
  155. setting_value=''
  156. # Save file in tmp dir
  157. elif [[ "${settings[$setting_name]}" == "file" ]]
  158. then
  159. if [ -n "${settings_value}" ]
  160. then
  161. echo "${setting_value}" | sed 's/|/\n/g' > $tmp_dir/$setting_name
  162. setting_value="$tmp_dir/$setting_name"
  163. fi
  164. fi
  165. $setting_name="$setting_value"
  166. done
  167. dns_method="custom"
  168. nameservers="$dns0,$dns1"
  169. # Build specific OVPN template
  170. cp -f /etc/openvpn/client.conf.tpl.restore $tmp_dir/client.conf.tpl
  171. # Remove some lines
  172. for rm_regex in "$(jq --raw-output '.openvpn_rm[]' "${config_file}")"
  173. do
  174. if [ ! -z "${rm_regex}" ] ; then
  175. sed -i "/$rm_regex/di" $tmp_dir/client.conf.tpl
  176. fi
  177. done
  178. # Add some other lines
  179. echo "# Custom" >> $tmp_dir/client.conf.tpl
  180. jq --raw-output ".openvpn_add[]" "${config_file}" >> $tmp_dir/client.conf.tpl
  181. # Build directly the OVPN file
  182. cp /etc/openvpn/client.conf.tpl "${config_file}"
  183. sed "s|<TPL:SERVER_NAME>|${settings[server_name]}|g" -i "${config_file}"
  184. sed "s|<TPL:SERVER_PORT>|${settings[server_port]}|g" -i "${config_file}"
  185. sed "s|<TPL:PROTO>|${settings[server_proto]}|g" -i "${config_file}"
  186. if [ -e "${settings[crt_client_key]}" ]; then
  187. sed 's|^<TPL:CERT_COMMENT>||g' -i "${config_file}"
  188. else
  189. sed 's|^<TPL:CERT_COMMENT>|;|g' -i "${config_file}"
  190. fi
  191. if [ -e "${settings[crt_client_ta]}" ]; then
  192. sed 's|^<TPL:TA_COMMENT>||' -i "${config_file}"
  193. else
  194. sed 's|^<TPL:TA_COMMENT>|;|' -i "${config_file}"
  195. fi
  196. if [[ "${settings[server_proto]}" =~ udp ]]; then
  197. sed 's|^<TPL:UDP_COMMENT>||' -i "${config_file}"
  198. else
  199. sed 's|^<TPL:UDP_COMMENT>|;|' -i "${config_file}"
  200. fi
  201. if [ -n "${settings[login_user]}" ]; then
  202. sed 's|^<TPL:LOGIN_COMMENT>||' -i "${config_file}"
  203. else
  204. sed 's|^<TPL:LOGIN_COMMENT>|;|' -i "${config_file}"
  205. fi
  206. elif [ -f "${config_file}" ] && [[ "${config_file}" =~ ^.*\.(ovpn|conf)$ ]]
  207. then
  208. if grep -q '^\s*<ca>' ${config_file}
  209. then
  210. grep -Poz '(?<=<ca>)(.*\n)*.*(?=</ca>)' ${config_file} > $tmp_dir/crt_server_ca
  211. crt_server_ca=$tmp_dir/crt_server_ca
  212. sed -i '/^\s*<ca>/,/\s*<\/ca>/d' ${config_file}
  213. sed -i '/^\s*ca\s/d' ${config_file}
  214. echo "ca /etc/openvpn/keys/ca-server.crt" >> ${config_file}
  215. fi
  216. if grep -q '^\s*<cert>' ${config_file}
  217. then
  218. grep -Poz '(?<=<cert>)(.*\n)*.*(?=</cert>)' ${config_file} > $tmp_dir/crt_client
  219. crt_client=$tmp_dir/crt_client
  220. sed -i '/^\s*<cert>/,/\s*<\/cert>/d' ${config_file}
  221. sed -i '/^\s*cert\s/d' ${config_file}
  222. echo "cert /etc/openvpn/keys/user.crt" >> ${config_file}
  223. fi
  224. if grep -q '^\s*<key>' ${config_file}
  225. then
  226. grep -Poz '(?<=<key>)(.*\n)*.*(?=</key>)' ${config_file} > $tmp_dir/crt_client_key
  227. crt_client_key=$tmp_dir/crt_client_key
  228. sed -i '/^\s*<key>/,/\s*<\/key>/d' ${config_file}
  229. sed -i '/^\s*key\s/d' ${config_file}
  230. echo "key /etc/openvpn/keys/user.key" >> ${config_file}
  231. fi
  232. if grep -q '^\s*<tls-auth>' ${config_file}
  233. then
  234. grep -Poz '(?<=<tls-auth>)(.*\n)*.*(?=</tls-auth>)' ${config_file} > $tmp_dir/crt_client_ta
  235. crt_client_ta=$tmp_dir/crt_client_ta
  236. sed -i '/^\s*<tls-auth>/,/\s*<\/tls-auth>/d' ${config_file}
  237. sed -i '/^\s*tls-auth\s/d' ${config_file}
  238. echo "tls-auth /etc/openvpn/keys/user_ta.key 1" >> ${config_file}
  239. fi
  240. sed -i 's@^\s*ca\s.*$@ca /etc/openvpn/keys/ca-server.crt@g' ${config_file}
  241. sed -i 's@^\s*cert\s.*$@cert /etc/openvpn/keys/user.crt@g' ${config_file}
  242. sed -i 's@^\s*key\s.*$@key /etc/openvpn/keys/user.key@g' ${config_file}
  243. sed -i 's@^\s*tls-auth\s.*$@tls-auth /etc/openvpn/keys/user-ta.key@g' ${config_file}
  244. fi
  245. _ynh_panel_validate
  246. }
  247. #=================================================
  248. # OVERWRITING APPLY STEP
  249. #=================================================
  250. ynh_panel_apply() {
  251. # Stop vpn client
  252. touch /tmp/.ynh-vpnclient-stopped
  253. systemctl stop ynh-vpnclient
  254. _ynh_panel_apply
  255. # Start vpn client
  256. systemctl start ynh-vpnclient
  257. rm -f /tmp/.ynh-vpnclient-stopped
  258. }
  259. ynh_panel_run $1