config 11 KB

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