config 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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_permissions() {
  18. local file="$1"
  19. if [ -f $file ]
  20. then
  21. chown $app:$app $file
  22. chmod go=--- $file
  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 --max-time 5 https://ip.yunohost.org --silent)\`
  40. **IPv6:** \`$(curl --max-time 5 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_permissions /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. # We don't have the extension, so we use this ugly hack to check that this is a json-like
  189. # (i.e. it starts with { ..)
  190. elif [ -f "${config_file}" ] && [[ "$(cat ${config_file} | tr -d ' ' | grep -v "^$" | head -c1)" == "{" ]]
  191. then
  192. ynh_print_info --message="Transforming .cube into OVPN file"
  193. server_name="$(read_cube $config_file server_name)"
  194. server_port="$(read_cube $config_file server_port)"
  195. server_proto="$(read_cube $config_file server_proto)"
  196. ip6_net="$(read_cube $config_file ip6_net)"
  197. ip6_addr="$(read_cube $config_file ip6_addr)"
  198. login_user="$(read_cube $config_file login_user)"
  199. login_passphrase="$(read_cube $config_file login_passphrase)"
  200. dns0="$(read_cube $config_file dns0)"
  201. dns1="$(read_cube $config_file dns1)"
  202. crt_server_ca="$(read_cube $config_file crt_server_ca)"
  203. crt_client="$(read_cube $config_file crt_client)"
  204. crt_client_key="$(read_cube $config_file crt_client_key)"
  205. crt_client_ta="$(read_cube $config_file crt_client_ta)"
  206. dns_method="custom"
  207. nameservers="$dns0,$dns1"
  208. # Build specific OVPN template
  209. tmp_dir=$(dirname "${config_file}")
  210. cp -f /etc/openvpn/client.conf.tpl.restore $tmp_dir/client.conf.tpl
  211. # Remove some lines
  212. for rm_regex in "$(jq --raw-output '.openvpn_rm[]' "${config_file}")"
  213. do
  214. if [ ! -z "${rm_regex}" ] ; then
  215. sed -i "/$rm_regex/di" $tmp_dir/client.conf.tpl
  216. fi
  217. done
  218. # Add some other lines
  219. echo "# Custom" >> $tmp_dir/client.conf.tpl
  220. jq --raw-output ".openvpn_add[]" "${config_file}" >> $tmp_dir/client.conf.tpl
  221. # Build directly the OVPN file
  222. cp /etc/openvpn/client.conf.tpl "${config_file}"
  223. [ "$server_proto" == tcp ] && server_proto=tcp-client
  224. sed "s|<TPL:SERVER_NAME>|$server_name|g" -i "${config_file}"
  225. sed "s|<TPL:SERVER_PORT>|$server_port|g" -i "${config_file}"
  226. sed "s|<TPL:PROTO>|$server_proto|g" -i "${config_file}"
  227. if [ -e "$crt_client_key" ]; then
  228. sed 's|^<TPL:CERT_COMMENT>||g' -i "${config_file}"
  229. else
  230. sed 's|^<TPL:CERT_COMMENT>|;|g' -i "${config_file}"
  231. fi
  232. if [ -e "$crt_client_ta" ]; then
  233. sed 's|^<TPL:TA_COMMENT>||' -i "${config_file}"
  234. else
  235. sed 's|^<TPL:TA_COMMENT>|;|' -i "${config_file}"
  236. fi
  237. if [[ "$server_proto" =~ udp ]]; then
  238. sed 's|^<TPL:UDP_COMMENT>||' -i "${config_file}"
  239. else
  240. sed 's|^<TPL:UDP_COMMENT>|;|' -i "${config_file}"
  241. fi
  242. if [ -n "$login_user" ]; then
  243. sed 's|^<TPL:LOGIN_COMMENT>||' -i "${config_file}"
  244. else
  245. sed 's|^<TPL:LOGIN_COMMENT>|;|' -i "${config_file}"
  246. fi
  247. [ "$server_proto" == tcp-client ] && server_proto=tcp
  248. # Othewise, assume that it's a .ovpn / .conf
  249. elif [ -f "${config_file}" ]
  250. then
  251. tmp_dir=$(dirname "${config_file}")
  252. ynh_print_info --message="Extracting TLS keys from .ovpn file"
  253. if grep -q '^\s*<ca>' ${config_file}
  254. then
  255. grep -Poz '(?<=<ca>)(.*\n)*.*(?=</ca>)' ${config_file} | sed '/^$/d' > $tmp_dir/crt_server_ca
  256. crt_server_ca=$tmp_dir/crt_server_ca
  257. sed -i '/^\s*<ca>/,/\s*<\/ca>/d' ${config_file}
  258. sed -i '/^\s*ca\s/d' ${config_file}
  259. echo "ca /etc/openvpn/keys/ca-server.crt" >> ${config_file}
  260. fi
  261. if grep -q '^\s*<cert>' ${config_file}
  262. then
  263. grep -Poz '(?<=<cert>)(.*\n)*.*(?=</cert>)' ${config_file} | sed '/^$/d' > $tmp_dir/crt_client
  264. crt_client=$tmp_dir/crt_client
  265. sed -i '/^\s*<cert>/,/\s*<\/cert>/d' ${config_file}
  266. sed -i '/^\s*cert\s/d' ${config_file}
  267. echo "cert /etc/openvpn/keys/user.crt" >> ${config_file}
  268. elif ! grep -q '^\s*cert\s' ${config_file}
  269. then
  270. crt_client=""
  271. fi
  272. if grep -q '^\s*<key>' ${config_file}
  273. then
  274. grep -Poz '(?<=<key>)(.*\n)*.*(?=</key>)' ${config_file} | sed '/^$/d' > $tmp_dir/crt_client_key
  275. crt_client_key=$tmp_dir/crt_client_key
  276. sed -i '/^\s*<key>/,/\s*<\/key>/d' ${config_file}
  277. sed -i '/^\s*key\s/d' ${config_file}
  278. echo "key /etc/openvpn/keys/user.key" >> ${config_file}
  279. elif ! grep -q '^\s*key\s' ${config_file}
  280. then
  281. crt_client_key=""
  282. fi
  283. if grep -q '^\s*<tls-auth>' ${config_file}
  284. then
  285. grep -Poz '(?<=<tls-auth>)(.*\n)*.*(?=</tls-auth>)' ${config_file} | sed '/^$/d' > $tmp_dir/crt_client_ta
  286. crt_client_ta=$tmp_dir/crt_client_ta
  287. sed -i '/^\s*<tls-auth>/,/\s*<\/tls-auth>/d' ${config_file}
  288. sed -i '/^\s*tls-auth\s/d' ${config_file}
  289. echo "tls-auth /etc/openvpn/keys/user_ta.key 1" >> ${config_file}
  290. elif ! grep -q '^\s*tls-auth\s' ${config_file}
  291. then
  292. crt_client_ta=""
  293. fi
  294. sed -i 's@^\s*ca\s.*$@ca /etc/openvpn/keys/ca-server.crt@g' ${config_file}
  295. sed -i 's@^\s*cert\s.*$@cert /etc/openvpn/keys/user.crt@g' ${config_file}
  296. sed -i 's@^\s*key\s.*$@key /etc/openvpn/keys/user.key@g' ${config_file}
  297. sed -i 's@^\s*tls-auth\s.*$@tls-auth /etc/openvpn/keys/user-ta.key@g' ${config_file}
  298. fi
  299. # Currently we need root priviledge to create tun0
  300. if [ -f "${config_file}" ]
  301. then
  302. sed -i '/^\s*user\s/d' ${config_file}
  303. sed -i '/^\s*group\s/d' ${config_file}
  304. fi
  305. _ynh_app_config_validate
  306. }
  307. #=================================================
  308. # OVERWRITING APPLY STEP
  309. #=================================================
  310. ynh_app_config_apply() {
  311. # Stop vpn client
  312. ynh_print_info --message="Stopping vpnclient in order to edit files"
  313. touch /tmp/.ynh-vpnclient-stopped
  314. /usr/local/bin/ynh-vpnclient stop
  315. chown $app:$app /etc/openvpn/keys
  316. chmod go=--- /etc/openvpn/keys
  317. _ynh_app_config_apply
  318. set_permissions /etc/openvpn/client.conf
  319. set_permissions /etc/openvpn/keys/ca-server.crt
  320. set_permissions /etc/openvpn/keys/user.crt
  321. set_permissions /etc/openvpn/keys/user.key
  322. set_permissions /etc/openvpn/keys/user_ta.key
  323. # Start vpn client
  324. ynh_print_info --message="Starting vpnclient service if needed"
  325. /usr/local/bin/ynh-vpnclient start
  326. rm -f /tmp/.ynh-vpnclient-stopped
  327. }
  328. ynh_app_config_run $1