config 11 KB

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