config 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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}" > /etc/openvpn/keys/credentials
  158. echo "${login_passphrase}" >> /etc/openvpn/keys/credentials
  159. set_permissions /etc/openvpn/keys/credentials
  160. else
  161. echo "" > /etc/openvpn/keys/credentials
  162. fi
  163. }
  164. set__login_passphrase() {
  165. :
  166. }
  167. #=================================================
  168. # OVERWRITING VALIDATE STEP
  169. #=================================================
  170. read_cube() {
  171. tmp_dir=$(dirname "$1")
  172. setting_value="$(jq --raw-output ".$2" "$1")"
  173. if [[ "$setting_value" == "null" ]]
  174. then
  175. setting_value=''
  176. # Save file in tmp dir
  177. elif [[ "$2" == "crt_"* ]]
  178. then
  179. if [ -n "${setting_value}" ]
  180. then
  181. echo "${setting_value}" | sed 's/|/\n/g' > $tmp_dir/$2
  182. setting_value="$tmp_dir/$2"
  183. fi
  184. fi
  185. echo $setting_value
  186. }
  187. ynh_app_config_validate() {
  188. # At this moment this var is not already set with the old value
  189. if [ -z ${config_file+x} ]
  190. then
  191. config_file="${old[config_file]}"
  192. # Overwrite form response with cube files data before validation process
  193. # We don't have the extension, so we use this ugly hack to check that this is a json-like
  194. # (i.e. it starts with { ..)
  195. elif [ -f "${config_file}" ] && [[ "$(cat ${config_file} | tr -d ' ' | grep -v "^$" | head -c1)" == "{" ]]
  196. then
  197. ynh_print_info --message="Transforming .cube into OVPN file"
  198. server_name="$(read_cube $config_file server_name)"
  199. server_port="$(read_cube $config_file server_port)"
  200. server_proto="$(read_cube $config_file server_proto)"
  201. ip6_net="$(read_cube $config_file ip6_net)"
  202. ip6_addr="$(read_cube $config_file ip6_addr)"
  203. login_user="$(read_cube $config_file login_user)"
  204. login_passphrase="$(read_cube $config_file login_passphrase)"
  205. dns0="$(read_cube $config_file dns0)"
  206. dns1="$(read_cube $config_file dns1)"
  207. crt_server_ca="$(read_cube $config_file crt_server_ca)"
  208. crt_client="$(read_cube $config_file crt_client)"
  209. crt_client_key="$(read_cube $config_file crt_client_key)"
  210. crt_client_ta="$(read_cube $config_file crt_client_ta)"
  211. dns_method="custom"
  212. nameservers="$dns0,$dns1"
  213. # Build specific OVPN template
  214. tmp_dir=$(dirname "${config_file}")
  215. cp -f /etc/openvpn/client.conf.tpl $tmp_dir/client.conf.tpl
  216. # Remove some lines
  217. for rm_regex in "$(jq --raw-output '.openvpn_rm[]' "${config_file}")"
  218. do
  219. if [ ! -z "${rm_regex}" ] ; then
  220. sed -i "/$rm_regex/d" $tmp_dir/client.conf.tpl
  221. fi
  222. done
  223. # Add some other lines
  224. echo "# Custom additions from .cube" >> $tmp_dir/client.conf.tpl
  225. jq --raw-output ".openvpn_add[]" "${config_file}" >> $tmp_dir/client.conf.tpl
  226. # Temporarily tweak sever_proto for template hydratation
  227. [ "$server_proto" == tcp ] && server_proto=tcp-client
  228. # Define other needed vars for template hydratation
  229. [ -e "$crt_client_key" ] && cert_comment="" || cert_comment="#"
  230. [ -e "$crt_client_ta" ] && ta_comment="" || ta_comment="#"
  231. [[ "$server_proto" =~ udp ]] && udp_comment="" || udp_comment="#"
  232. [ -n "$login_user" ] && login_comment="" || login_comment="#"
  233. # Actually generate/hydrate the final configuration
  234. ynh_add_config --template="$tmp_dir/client.conf.tpl" --destination="${config_file}"
  235. [ "$server_proto" == tcp-client ] && server_proto=tcp
  236. # Othewise, assume that it's a .ovpn / .conf
  237. elif [ -f "${config_file}" ]
  238. then
  239. tmp_dir=$(dirname "${config_file}")
  240. ynh_print_info --message="Extracting TLS keys from .ovpn file"
  241. if grep -q '^\s*<ca>' ${config_file}
  242. then
  243. grep -Poz '(?<=<ca>)(.*\n)*.*(?=</ca>)' ${config_file} | sed '/^$/d' > $tmp_dir/crt_server_ca
  244. crt_server_ca=$tmp_dir/crt_server_ca
  245. sed -i '/^\s*<ca>/,/\s*<\/ca>/d' ${config_file}
  246. sed -i '/^\s*ca\s/d' ${config_file}
  247. echo "ca /etc/openvpn/keys/ca-server.crt" >> ${config_file}
  248. fi
  249. if grep -q '^\s*<cert>' ${config_file}
  250. then
  251. grep -Poz '(?<=<cert>)(.*\n)*.*(?=</cert>)' ${config_file} | sed '/^$/d' > $tmp_dir/crt_client
  252. crt_client=$tmp_dir/crt_client
  253. sed -i '/^\s*<cert>/,/\s*<\/cert>/d' ${config_file}
  254. sed -i '/^\s*cert\s/d' ${config_file}
  255. echo "cert /etc/openvpn/keys/user.crt" >> ${config_file}
  256. elif ! grep -q '^\s*cert\s' ${config_file}
  257. then
  258. crt_client=""
  259. fi
  260. if grep -q '^\s*<key>' ${config_file}
  261. then
  262. grep -Poz '(?<=<key>)(.*\n)*.*(?=</key>)' ${config_file} | sed '/^$/d' > $tmp_dir/crt_client_key
  263. crt_client_key=$tmp_dir/crt_client_key
  264. sed -i '/^\s*<key>/,/\s*<\/key>/d' ${config_file}
  265. sed -i '/^\s*key\s/d' ${config_file}
  266. echo "key /etc/openvpn/keys/user.key" >> ${config_file}
  267. elif ! grep -q '^\s*key\s' ${config_file}
  268. then
  269. crt_client_key=""
  270. fi
  271. if grep -q '^\s*<tls-auth>' ${config_file}
  272. then
  273. grep -Poz '(?<=<tls-auth>)(.*\n)*.*(?=</tls-auth>)' ${config_file} | sed '/^$/d' > $tmp_dir/crt_client_ta
  274. crt_client_ta=$tmp_dir/crt_client_ta
  275. sed -i '/^\s*<tls-auth>/,/\s*<\/tls-auth>/d' ${config_file}
  276. sed -i '/^\s*tls-auth\s/d' ${config_file}
  277. echo "tls-auth /etc/openvpn/keys/user_ta.key 1" >> ${config_file}
  278. elif ! grep -q '^\s*tls-auth\s' ${config_file}
  279. then
  280. crt_client_ta=""
  281. fi
  282. sed -i 's@^\s*ca\s.*$@ca /etc/openvpn/keys/ca-server.crt@g' ${config_file}
  283. sed -i 's@^\s*cert\s.*$@cert /etc/openvpn/keys/user.crt@g' ${config_file}
  284. sed -i 's@^\s*key\s.*$@key /etc/openvpn/keys/user.key@g' ${config_file}
  285. sed -i 's@^\s*tls-auth\s.*$@tls-auth /etc/openvpn/keys/user-ta.key@g' ${config_file}
  286. fi
  287. # Currently we need root priviledge to create tun0
  288. if [ -f "${config_file}" ]
  289. then
  290. sed -i '/^\s*user\s/d' ${config_file}
  291. sed -i '/^\s*group\s/d' ${config_file}
  292. fi
  293. _ynh_app_config_validate
  294. }
  295. #=================================================
  296. # OVERWRITING APPLY STEP
  297. #=================================================
  298. ynh_app_config_apply() {
  299. # Stop vpn client
  300. ynh_print_info --message="Stopping vpnclient in order to edit files"
  301. touch /tmp/.ynh-vpnclient-stopped
  302. /usr/local/bin/ynh-vpnclient stop
  303. chown $app:$app /etc/openvpn/keys
  304. chmod go=--- /etc/openvpn/keys
  305. _ynh_app_config_apply
  306. set_permissions /etc/openvpn/client.conf
  307. set_permissions /etc/openvpn/keys/ca-server.crt
  308. set_permissions /etc/openvpn/keys/user.crt
  309. set_permissions /etc/openvpn/keys/user.key
  310. set_permissions /etc/openvpn/keys/user_ta.key
  311. # Start vpn client
  312. ynh_print_info --message="Starting vpnclient service if needed"
  313. /usr/local/bin/ynh-vpnclient start
  314. rm -f /tmp/.ynh-vpnclient-stopped
  315. }
  316. ynh_app_config_run $1