config 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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_right() {
  18. if [ -f $1 ]
  19. then
  20. chown $app:$app $1
  21. chmod go=--- $1
  22. fi
  23. }
  24. #=================================================
  25. # SPECIFIC GETTERS FOR TOML SHORT KEY
  26. #=================================================
  27. get__status() {
  28. local service_enabled=$(ynh_app_setting_get $app service_enabled)
  29. if ip route get 1.2.3.4 | grep -q tun0
  30. then
  31. if [ $service_enabled -eq 1 ]
  32. then
  33. cat << EOF
  34. style: success
  35. ask:
  36. en: |-
  37. Your VPN is running :)
  38. **IPv4:** \`$(curl --max-time 5 https://ip.yunohost.org --silent)\`
  39. **IPv6:** \`$(curl --max-time 5 https://ip6.yunohost.org --silent)\`
  40. EOF
  41. else
  42. cat << EOF
  43. style: warning
  44. ask:
  45. en: Your VPN is running, but it shouldn't !
  46. EOF
  47. fi
  48. elif [ $service_enabled -eq 1 ]
  49. then
  50. cat << EOF
  51. style: danger
  52. ask:
  53. en: |-
  54. Your VPN is down ! Here are errors logged in the last 5 minutes
  55. \`\`\`
  56. $(journalctl -u openvpn@client -p0..3 --since "- 20 minutes" -o cat | sed 's/^/ /g' | tail -n 15)
  57. \`\`\`
  58. EOF
  59. else
  60. cat << EOF
  61. style: info
  62. ask:
  63. en: Your VPN is down as expected.
  64. EOF
  65. fi
  66. }
  67. get__login_user() {
  68. if [ -s /etc/openvpn/keys/credentials ]
  69. then
  70. echo "$(sed -n 1p /etc/openvpn/keys/credentials)"
  71. else
  72. echo ""
  73. fi
  74. }
  75. get__login_passphrase() {
  76. if [ -s /etc/openvpn/keys/credentials ]
  77. then
  78. echo "$(sed -n 2p /etc/openvpn/keys/credentials)"
  79. else
  80. echo ""
  81. fi
  82. }
  83. #=================================================
  84. # SPECIFIC VALIDATORS FOR TOML SHORT KEYS
  85. #=================================================
  86. validate__login_user() {
  87. if grep -q '^\s*auth-user-pass' ${config_file}
  88. then
  89. if [[ -z "${login_user}" ]]
  90. then
  91. echo 'A Username is needed with this configuration file'
  92. fi
  93. fi
  94. }
  95. validate__login_passphrase() {
  96. if grep -q '^\s*auth-user-pass' ${config_file}
  97. then
  98. if [[ -z "${login_passphrase}" ]]
  99. then
  100. echo 'A Password is needed with this configuration file'
  101. fi
  102. fi
  103. }
  104. validate__crt_server_ca() {
  105. if grep -q '^\s*ca\s' ${config_file}
  106. then
  107. if [[ ! -e "${crt_server_ca}" ]]
  108. then
  109. echo "A server CA certificate is needed"
  110. fi
  111. fi
  112. }
  113. validate__crt_client() {
  114. if grep -q '^\s*cert\s' ${config_file}
  115. then
  116. if [[ ! -e "${crt_client}" ]]
  117. then
  118. echo "A Client certificate is needed with this configuration file"
  119. fi
  120. fi
  121. }
  122. validate__crt_client_key() {
  123. if grep -q '^\s*key\s' ${config_file}
  124. then
  125. if [[ ! -e "${crt_client_key}" ]]
  126. then
  127. echo "A client private key is needed with this configuration file"
  128. fi
  129. fi
  130. }
  131. validate__crt_client_ta() {
  132. if grep -q '^\s*tls-auth\s' ${config_file}
  133. then
  134. if [[ ! -e "${crt_client_ta}" ]]
  135. then
  136. echo "A TLS auth shared secret is needed with this configuration file"
  137. fi
  138. fi
  139. }
  140. validate__nameservers() {
  141. if [[ "$dns_method" == "custom" ]] && [[ -z "$nameservers" ]]
  142. then
  143. echo "You need to choose DNS resolvers or select an other method to provide DNS resolvers"
  144. fi
  145. }
  146. #=================================================
  147. # SPECIFIC SETTERS FOR TOML SHORT KEYS
  148. #=================================================
  149. set__login_user() {
  150. if [ -n "${login_user}" ]
  151. then
  152. echo "${login_user}\n${login_passphrase}" > /etc/openvpn/keys/credentials
  153. set_right /etc/openvpn/keys/credentials
  154. else
  155. echo "" > /etc/openvpn/keys/credentials
  156. fi
  157. }
  158. set__login_passphrase() {
  159. :
  160. }
  161. #=================================================
  162. # OVERWRITING VALIDATE STEP
  163. #=================================================
  164. read_cube() {
  165. tmp_dir=$(dirname "$1")
  166. setting_value="$(jq --raw-output ".$2" "$1")"
  167. if [[ "$setting_value" == "null" ]]
  168. then
  169. setting_value=''
  170. # Save file in tmp dir
  171. elif [[ "$2" == "crt_"* ]]
  172. then
  173. if [ -n "${setting_value}" ]
  174. then
  175. echo "${setting_value}" | sed 's/|/\n/g' > $tmp_dir/$2
  176. setting_value="$tmp_dir/$2"
  177. fi
  178. fi
  179. echo $setting_value
  180. }
  181. ynh_app_config_validate() {
  182. # At this moment this var is not already set with the old value
  183. if [ -z ${config_file+x} ]
  184. then
  185. config_file="${old[config_file]}"
  186. # Overwrite form response with cube files data before validation process
  187. # We don't have the extension, so we use this ugly hack to check that this is a json-like
  188. # (i.e. it starts with { ..)
  189. elif [ -f "${config_file}" ] && [[ "$(cat ${config_file} | tr -d ' ' | grep -v "^$" | head -c1)" == "{" ]]
  190. then
  191. ynh_print_info --message="Transforming .cube into OVPN file"
  192. server_name="$(read_cube $config_file server_name)"
  193. server_port="$(read_cube $config_file server_port)"
  194. server_proto="$(read_cube $config_file server_proto)"
  195. ip6_net="$(read_cube $config_file ip6_net)"
  196. ip6_addr="$(read_cube $config_file ip6_addr)"
  197. login_user="$(read_cube $config_file login_user)"
  198. login_passphrase="$(read_cube $config_file login_passphrase)"
  199. dns0="$(read_cube $config_file dns0)"
  200. dns1="$(read_cube $config_file dns1)"
  201. crt_server_ca="$(read_cube $config_file crt_server_ca)"
  202. crt_client="$(read_cube $config_file crt_client)"
  203. crt_client_key="$(read_cube $config_file crt_client_key)"
  204. crt_client_ta="$(read_cube $config_file crt_client_ta)"
  205. dns_method="custom"
  206. nameservers="$dns0,$dns1"
  207. # Build specific OVPN template
  208. tmp_dir=$(dirname "${config_file}")
  209. cp -f /etc/openvpn/client.conf.tpl.restore $tmp_dir/client.conf.tpl
  210. # Remove some lines
  211. for rm_regex in "$(jq --raw-output '.openvpn_rm[]' "${config_file}")"
  212. do
  213. if [ ! -z "${rm_regex}" ] ; then
  214. sed -i "/$rm_regex/di" $tmp_dir/client.conf.tpl
  215. fi
  216. done
  217. # Add some other lines
  218. echo "# Custom" >> $tmp_dir/client.conf.tpl
  219. jq --raw-output ".openvpn_add[]" "${config_file}" >> $tmp_dir/client.conf.tpl
  220. # Build directly the OVPN file
  221. cp /etc/openvpn/client.conf.tpl "${config_file}"
  222. [ "$server_proto" == tcp ] && server_proto=tcp-client
  223. sed "s|<TPL:SERVER_NAME>|$server_name|g" -i "${config_file}"
  224. sed "s|<TPL:SERVER_PORT>|$server_port|g" -i "${config_file}"
  225. sed "s|<TPL:PROTO>|$server_proto|g" -i "${config_file}"
  226. if [ -e "$crt_client_key" ]; then
  227. sed 's|^<TPL:CERT_COMMENT>||g' -i "${config_file}"
  228. else
  229. sed 's|^<TPL:CERT_COMMENT>|;|g' -i "${config_file}"
  230. fi
  231. if [ -e "$crt_client_ta" ]; then
  232. sed 's|^<TPL:TA_COMMENT>||' -i "${config_file}"
  233. else
  234. sed 's|^<TPL:TA_COMMENT>|;|' -i "${config_file}"
  235. fi
  236. if [[ "$server_proto" =~ udp ]]; then
  237. sed 's|^<TPL:UDP_COMMENT>||' -i "${config_file}"
  238. else
  239. sed 's|^<TPL:UDP_COMMENT>|;|' -i "${config_file}"
  240. fi
  241. if [ -n "$login_user" ]; then
  242. sed 's|^<TPL:LOGIN_COMMENT>||' -i "${config_file}"
  243. else
  244. sed 's|^<TPL:LOGIN_COMMENT>|;|' -i "${config_file}"
  245. fi
  246. [ "$server_proto" == tcp-client ] && server_proto=tcp
  247. # Othewise, assume that it's a .ovpn / .conf
  248. elif [ -f "${config_file}" ]
  249. then
  250. tmp_dir=$(dirname "${config_file}")
  251. ynh_print_info --message="Extracting TLS keys from .ovpn file"
  252. if grep -q '^\s*<ca>' ${config_file}
  253. then
  254. grep -Poz '(?<=<ca>)(.*\n)*.*(?=</ca>)' ${config_file} | sed '/^$/d' > $tmp_dir/crt_server_ca
  255. crt_server_ca=$tmp_dir/crt_server_ca
  256. sed -i '/^\s*<ca>/,/\s*<\/ca>/d' ${config_file}
  257. sed -i '/^\s*ca\s/d' ${config_file}
  258. echo "ca /etc/openvpn/keys/ca-server.crt" >> ${config_file}
  259. fi
  260. if grep -q '^\s*<cert>' ${config_file}
  261. then
  262. grep -Poz '(?<=<cert>)(.*\n)*.*(?=</cert>)' ${config_file} | sed '/^$/d' > $tmp_dir/crt_client
  263. crt_client=$tmp_dir/crt_client
  264. sed -i '/^\s*<cert>/,/\s*<\/cert>/d' ${config_file}
  265. sed -i '/^\s*cert\s/d' ${config_file}
  266. echo "cert /etc/openvpn/keys/user.crt" >> ${config_file}
  267. elif ! grep -q '^\s*cert\s' ${config_file}
  268. then
  269. crt_client=""
  270. fi
  271. if grep -q '^\s*<key>' ${config_file}
  272. then
  273. grep -Poz '(?<=<key>)(.*\n)*.*(?=</key>)' ${config_file} | sed '/^$/d' > $tmp_dir/crt_client_key
  274. crt_client_key=$tmp_dir/crt_client_key
  275. sed -i '/^\s*<key>/,/\s*<\/key>/d' ${config_file}
  276. sed -i '/^\s*key\s/d' ${config_file}
  277. echo "key /etc/openvpn/keys/user.key" >> ${config_file}
  278. elif ! grep -q '^\s*key\s' ${config_file}
  279. then
  280. crt_client_key=""
  281. fi
  282. if grep -q '^\s*<tls-auth>' ${config_file}
  283. then
  284. grep -Poz '(?<=<tls-auth>)(.*\n)*.*(?=</tls-auth>)' ${config_file} | sed '/^$/d' > $tmp_dir/crt_client_ta
  285. crt_client_ta=$tmp_dir/crt_client_ta
  286. sed -i '/^\s*<tls-auth>/,/\s*<\/tls-auth>/d' ${config_file}
  287. sed -i '/^\s*tls-auth\s/d' ${config_file}
  288. echo "tls-auth /etc/openvpn/keys/user_ta.key 1" >> ${config_file}
  289. elif ! grep -q '^\s*tls-auth\s' ${config_file}
  290. then
  291. crt_client_ta=""
  292. fi
  293. sed -i 's@^\s*ca\s.*$@ca /etc/openvpn/keys/ca-server.crt@g' ${config_file}
  294. sed -i 's@^\s*cert\s.*$@cert /etc/openvpn/keys/user.crt@g' ${config_file}
  295. sed -i 's@^\s*key\s.*$@key /etc/openvpn/keys/user.key@g' ${config_file}
  296. sed -i 's@^\s*tls-auth\s.*$@tls-auth /etc/openvpn/keys/user-ta.key@g' ${config_file}
  297. fi
  298. # Currently we need root priviledge to create tun0
  299. if [ -f "${config_file}" ]
  300. then
  301. sed -i '/^\s*user\s/d' ${config_file}
  302. sed -i '/^\s*group\s/d' ${config_file}
  303. fi
  304. _ynh_app_config_validate
  305. }
  306. #=================================================
  307. # OVERWRITING APPLY STEP
  308. #=================================================
  309. ynh_app_config_apply() {
  310. # Stop vpn client
  311. ynh_print_info --message="Stopping vpnclient in order to edit files"
  312. touch /tmp/.ynh-vpnclient-stopped
  313. /usr/local/bin/ynh-vpnclient stop
  314. chown $app:$app /etc/openvpn/keys
  315. chmod go=--- /etc/openvpn/keys
  316. _ynh_app_config_apply
  317. set_right /etc/openvpn/client.conf
  318. set_right /etc/openvpn/keys/ca-server.crt
  319. set_right /etc/openvpn/keys/user.crt
  320. set_right /etc/openvpn/keys/user.key
  321. set_right /etc/openvpn/keys/user_ta.key
  322. # Start vpn client
  323. ynh_print_info --message="Starting vpnclient service if needed"
  324. /usr/local/bin/ynh-vpnclient start
  325. rm -f /tmp/.ynh-vpnclient-stopped
  326. }
  327. ynh_app_config_run $1