config 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 as 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. validate__login_passphrase() {
  97. if grep -q '^\s*auth-user-pass' ${config_file}
  98. then
  99. if [[ -z "${login_passphrase}" ]]
  100. then
  101. echo 'A Password is needed with this configuration file'
  102. fi
  103. fi
  104. }
  105. validate__crt_server_ca() {
  106. if grep -q '^\s*ca\s' ${config_file}
  107. then
  108. if [[ ! -e "${crt_server_ca}" ]]
  109. then
  110. echo "A server CA certificate is needed"
  111. fi
  112. fi
  113. }
  114. validate__crt_client() {
  115. if grep -q '^\s*cert\s' ${config_file}
  116. then
  117. if [[ ! -e "${crt_client}" ]]
  118. then
  119. echo "A Client certificate is needed with this configuration file"
  120. fi
  121. fi
  122. }
  123. validate__crt_client_key() {
  124. if grep -q '^\s*key\s' ${config_file}
  125. then
  126. if [[ ! -e "${crt_client_key}" ]]
  127. then
  128. echo "A client private key is needed with this configuration file"
  129. fi
  130. fi
  131. }
  132. validate__crt_client_ta() {
  133. if grep -q '^\s*tls-auth\s' ${config_file}
  134. then
  135. if [[ ! -e "${crt_client_ta}" ]]
  136. then
  137. echo "A TLS auth shared secret is needed with this configuration file"
  138. fi
  139. fi
  140. }
  141. validate__nameservers() {
  142. if [[ "$dns_method" == "custom" ]] && [[ -z "$nameservers" ]]
  143. then
  144. echo "You need to choose DNS resolvers or select an other method to provide DNS resolvers"
  145. fi
  146. }
  147. #=================================================
  148. # SPECIFIC SETTERS FOR TOML SHORT KEYS
  149. #=================================================
  150. set__login_user() {
  151. if [ -n "${login_user}" ]
  152. then
  153. echo "${login_user}\n${login_passphrase}" > /etc/openvpn/keys/credentials
  154. set_right /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. read_cube() {
  166. tmp_dir=$(dirname "$1")
  167. setting_value="$(jq --raw-output ".$2" "$1")"
  168. if [[ "$setting_value" == "null" ]]
  169. then
  170. setting_value=''
  171. # Save file in tmp dir
  172. elif [[ "$2" == "crt_"* ]]
  173. then
  174. if [ -n "${setting_value}" ]
  175. then
  176. echo "${setting_value}" | sed 's/|/\n/g' > $tmp_dir/$2
  177. setting_value="$tmp_dir/$2"
  178. fi
  179. fi
  180. echo $setting_value
  181. }
  182. ynh_app_config_validate() {
  183. # At this moment this var is not already set with the old value
  184. if [ -z ${config_file+x} ]
  185. then
  186. config_file="${old[config_file]}"
  187. # Overwrite form response with cube files data before validation process
  188. elif [ -f "${config_file}" ] && [[ $config_file == *.cube ]]
  189. then
  190. ynh_print_info --message="Transforming .cube into OVPN file"
  191. server_name="$(read_cube $config_file server_name)"
  192. server_port="$(read_cube $config_file server_port)"
  193. server_proto="$(read_cube $config_file server_proto)"
  194. ip6_net="$(read_cube $config_file ip6_net)"
  195. ip6_addr="$(read_cube $config_file ip6_addr)"
  196. login_user="$(read_cube $config_file login_user)"
  197. login_passphrase="$(read_cube $config_file login_passphrase)"
  198. dns0="$(read_cube $config_file dns0)"
  199. dns1="$(read_cube $config_file dns1)"
  200. crt_server_ca="$(read_cube $config_file crt_server_ca)"
  201. crt_client="$(read_cube $config_file crt_client)"
  202. crt_client_key="$(read_cube $config_file crt_client_key)"
  203. crt_client_ta="$(read_cube $config_file crt_client_ta)"
  204. dns_method="custom"
  205. nameservers="$dns0,$dns1"
  206. # Build specific OVPN template
  207. tmp_dir=$(dirname "${config_file}")
  208. cp -f /etc/openvpn/client.conf.tpl.restore $tmp_dir/client.conf.tpl
  209. # Remove some lines
  210. for rm_regex in "$(jq --raw-output '.openvpn_rm[]' "${config_file}")"
  211. do
  212. if [ ! -z "${rm_regex}" ] ; then
  213. sed -i "/$rm_regex/di" $tmp_dir/client.conf.tpl
  214. fi
  215. done
  216. # Add some other lines
  217. echo "# Custom" >> $tmp_dir/client.conf.tpl
  218. jq --raw-output ".openvpn_add[]" "${config_file}" >> $tmp_dir/client.conf.tpl
  219. # Build directly the OVPN file
  220. cp /etc/openvpn/client.conf.tpl "${config_file}"
  221. [ "$server_proto" == tcp ] && server_proto=tcp-client
  222. sed "s|<TPL:SERVER_NAME>|$server_name|g" -i "${config_file}"
  223. sed "s|<TPL:SERVER_PORT>|$server_port|g" -i "${config_file}"
  224. sed "s|<TPL:PROTO>|$server_proto|g" -i "${config_file}"
  225. if [ -e "$crt_client_key" ]; then
  226. sed 's|^<TPL:CERT_COMMENT>||g' -i "${config_file}"
  227. else
  228. sed 's|^<TPL:CERT_COMMENT>|;|g' -i "${config_file}"
  229. fi
  230. if [ -e "$crt_client_ta" ]; then
  231. sed 's|^<TPL:TA_COMMENT>||' -i "${config_file}"
  232. else
  233. sed 's|^<TPL:TA_COMMENT>|;|' -i "${config_file}"
  234. fi
  235. if [[ "$server_proto" =~ udp ]]; then
  236. sed 's|^<TPL:UDP_COMMENT>||' -i "${config_file}"
  237. else
  238. sed 's|^<TPL:UDP_COMMENT>|;|' -i "${config_file}"
  239. fi
  240. if [ -n "$login_user" ]; then
  241. sed 's|^<TPL:LOGIN_COMMENT>||' -i "${config_file}"
  242. else
  243. sed 's|^<TPL:LOGIN_COMMENT>|;|' -i "${config_file}"
  244. fi
  245. [ "$server_proto" == tcp-client ] && server_proto=tcp
  246. elif [ -f "${config_file}" ] && [[ "${config_file}" =~ ^.*\.(ovpn|conf)$ ]]
  247. then
  248. tmp_dir=$(dirname "${config_file}")
  249. ynh_print_info --message="Extracting TLS keys from .ovpn file"
  250. if grep -q '^\s*<ca>' ${config_file}
  251. then
  252. grep -Poz '(?<=<ca>)(.*\n)*.*(?=</ca>)' ${config_file} | sed '/^$/d' > $tmp_dir/crt_server_ca
  253. crt_server_ca=$tmp_dir/crt_server_ca
  254. sed -i '/^\s*<ca>/,/\s*<\/ca>/d' ${config_file}
  255. sed -i '/^\s*ca\s/d' ${config_file}
  256. echo "ca /etc/openvpn/keys/ca-server.crt" >> ${config_file}
  257. fi
  258. if grep -q '^\s*<cert>' ${config_file}
  259. then
  260. grep -Poz '(?<=<cert>)(.*\n)*.*(?=</cert>)' ${config_file} | sed '/^$/d' > $tmp_dir/crt_client
  261. crt_client=$tmp_dir/crt_client
  262. sed -i '/^\s*<cert>/,/\s*<\/cert>/d' ${config_file}
  263. sed -i '/^\s*cert\s/d' ${config_file}
  264. echo "cert /etc/openvpn/keys/user.crt" >> ${config_file}
  265. elif ! grep -q '^\s*cert\s' ${config_file}
  266. then
  267. crt_client=""
  268. fi
  269. if grep -q '^\s*<key>' ${config_file}
  270. then
  271. grep -Poz '(?<=<key>)(.*\n)*.*(?=</key>)' ${config_file} | sed '/^$/d' > $tmp_dir/crt_client_key
  272. crt_client_key=$tmp_dir/crt_client_key
  273. sed -i '/^\s*<key>/,/\s*<\/key>/d' ${config_file}
  274. sed -i '/^\s*key\s/d' ${config_file}
  275. echo "key /etc/openvpn/keys/user.key" >> ${config_file}
  276. elif ! grep -q '^\s*key\s' ${config_file}
  277. then
  278. crt_client_key=""
  279. fi
  280. if grep -q '^\s*<tls-auth>' ${config_file}
  281. then
  282. grep -Poz '(?<=<tls-auth>)(.*\n)*.*(?=</tls-auth>)' ${config_file} | sed '/^$/d' > $tmp_dir/crt_client_ta
  283. crt_client_ta=$tmp_dir/crt_client_ta
  284. sed -i '/^\s*<tls-auth>/,/\s*<\/tls-auth>/d' ${config_file}
  285. sed -i '/^\s*tls-auth\s/d' ${config_file}
  286. echo "tls-auth /etc/openvpn/keys/user_ta.key 1" >> ${config_file}
  287. elif ! grep -q '^\s*tls-auth\s' ${config_file}
  288. then
  289. crt_client_ta=""
  290. fi
  291. sed -i 's@^\s*ca\s.*$@ca /etc/openvpn/keys/ca-server.crt@g' ${config_file}
  292. sed -i 's@^\s*cert\s.*$@cert /etc/openvpn/keys/user.crt@g' ${config_file}
  293. sed -i 's@^\s*key\s.*$@key /etc/openvpn/keys/user.key@g' ${config_file}
  294. sed -i 's@^\s*tls-auth\s.*$@tls-auth /etc/openvpn/keys/user-ta.key@g' ${config_file}
  295. fi
  296. # Currently we need root priviledge to create tun0
  297. if [ -f "${config_file}" ]
  298. then
  299. sed -i '/^\s*user\s/d' ${config_file}
  300. sed -i '/^\s*group\s/d' ${config_file}
  301. fi
  302. _ynh_app_config_validate
  303. }
  304. #=================================================
  305. # OVERWRITING APPLY STEP
  306. #=================================================
  307. ynh_app_config_apply() {
  308. # Stop vpn client
  309. ynh_print_info --message="Stopping vpnclient in order to edit files"
  310. touch /tmp/.ynh-vpnclient-stopped
  311. /usr/local/bin/ynh-vpnclient stop
  312. chown $app:$app /etc/openvpn/keys
  313. chmod go=--- /etc/openvpn/keys
  314. _ynh_app_config_apply
  315. set_right /etc/openvpn/client.conf
  316. set_right /etc/openvpn/keys/ca-server.crt
  317. set_right /etc/openvpn/keys/user.crt
  318. set_right /etc/openvpn/keys/user.key
  319. set_right /etc/openvpn/keys/user_ta.key
  320. # Start vpn client
  321. ynh_print_info --message="Starting vpnclient service if needed"
  322. /usr/local/bin/ynh-vpnclient start
  323. rm -f /tmp/.ynh-vpnclient-stopped
  324. }
  325. ynh_app_config_run $1