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