|
@@ -195,23 +195,6 @@ set__login_passphrase() {
|
|
|
#=================================================
|
|
|
# OVERWRITING VALIDATE STEP
|
|
|
#=================================================
|
|
|
-read_cube() {
|
|
|
- tmp_dir=$(dirname "$1")
|
|
|
- setting_value="$(jq --raw-output ".$2" "$1")"
|
|
|
- if [[ "$setting_value" == "null" ]]
|
|
|
- then
|
|
|
- setting_value=''
|
|
|
- # Save file in tmp dir
|
|
|
- elif [[ "$2" == "crt_"* ]]
|
|
|
- then
|
|
|
- if [ -n "${setting_value}" ]
|
|
|
- then
|
|
|
- echo "${setting_value}" | sed 's/|/\n/g' > $tmp_dir/$2
|
|
|
- setting_value="$tmp_dir/$2"
|
|
|
- fi
|
|
|
- fi
|
|
|
- echo $setting_value
|
|
|
-}
|
|
|
ynh_app_config_validate() {
|
|
|
# At this moment this var is not already set with the old value
|
|
|
if [ -z ${config_file+x} ]
|
|
@@ -224,115 +207,21 @@ ynh_app_config_validate() {
|
|
|
# (i.e. it starts with { ..)
|
|
|
elif [ -f "${config_file}" ] && [[ "$(cat ${config_file} | tr -d ' ' | grep -v "^$" | head -c1)" == "{" ]]
|
|
|
then
|
|
|
- ynh_print_info --message="Transforming .cube into OVPN file"
|
|
|
- server_name="$(read_cube $config_file server_name)"
|
|
|
- server_port="$(read_cube $config_file server_port)"
|
|
|
- server_proto="$(read_cube $config_file server_proto)"
|
|
|
- ip6_net="$(read_cube $config_file ip6_net)"
|
|
|
- ip6_addr="$(read_cube $config_file ip6_addr)"
|
|
|
- login_user="$(read_cube $config_file login_user)"
|
|
|
- login_passphrase="$(read_cube $config_file login_passphrase)"
|
|
|
- dns0="$(read_cube $config_file dns0)"
|
|
|
- dns1="$(read_cube $config_file dns1)"
|
|
|
- crt_server_ca="$(read_cube $config_file crt_server_ca)"
|
|
|
- crt_client="$(read_cube $config_file crt_client)"
|
|
|
- crt_client_key="$(read_cube $config_file crt_client_key)"
|
|
|
- crt_client_ta="$(read_cube $config_file crt_client_ta)"
|
|
|
-
|
|
|
- if [[ -z "$dns0" && -z "$dns1" ]]; then
|
|
|
- dns_method="yunohost"
|
|
|
- else
|
|
|
- dns_method="custom"
|
|
|
- nameservers="$dns0,$dns1"
|
|
|
- fi
|
|
|
-
|
|
|
- # Build specific OVPN template
|
|
|
- tmp_dir=$(dirname "${config_file}")
|
|
|
- cp -f /etc/yunohost/apps/vpnclient/conf/openvpn_client.conf.tpl $tmp_dir/client.conf.tpl
|
|
|
- # Remove some lines
|
|
|
- jq --raw-output '.openvpn_rm[]' "${config_file}" | while read -r rm_regex
|
|
|
- do
|
|
|
- if [ ! -z "${rm_regex}" ] ; then
|
|
|
- sed -i "/${rm_regex/\//\\\/}/d" $tmp_dir/client.conf.tpl
|
|
|
- fi
|
|
|
- done
|
|
|
-
|
|
|
- # Add some other lines
|
|
|
- echo "# Custom additions from .cube" >> $tmp_dir/client.conf.tpl
|
|
|
- jq --raw-output ".openvpn_add[]" "${config_file}" >> $tmp_dir/client.conf.tpl
|
|
|
-
|
|
|
- # Temporarily tweak sever_proto for template hydratation
|
|
|
- [ "$server_proto" == tcp ] && server_proto=tcp-client
|
|
|
-
|
|
|
- # Define other needed vars for template hydratation
|
|
|
- [ -e "$crt_client_key" ] && cert_comment="" || cert_comment="#"
|
|
|
- [ -e "$crt_client_ta" ] && ta_comment="" || ta_comment="#"
|
|
|
- [[ "$server_proto" =~ udp ]] && udp_comment="" || udp_comment="#"
|
|
|
- [ -n "$login_user" ] && login_comment="" || login_comment="#"
|
|
|
-
|
|
|
- # Actually generate/hydrate the final configuration
|
|
|
- ynh_add_config --template="$tmp_dir/client.conf.tpl" --destination="${config_file}"
|
|
|
-
|
|
|
- [ "$server_proto" == tcp-client ] && server_proto=tcp
|
|
|
+ local tmp_dir=$(dirname "$config_file")
|
|
|
|
|
|
+ cube_file="$tmp_dir/client.cube"
|
|
|
+ cp -f "$config_file" "$cube_file"
|
|
|
|
|
|
+ convert_cube_file "$config_file"
|
|
|
# Othewise, assume that it's a .ovpn / .conf
|
|
|
elif [ -f "${config_file}" ]
|
|
|
then
|
|
|
- tmp_dir=$(dirname "${config_file}")
|
|
|
- ynh_print_info --message="Extracting TLS keys from .ovpn file"
|
|
|
- if grep -q '^\s*<ca>' ${config_file}
|
|
|
- then
|
|
|
- grep -Poz '(?<=<ca>)(.*\n)*.*(?=</ca>)' ${config_file} | sed '/^$/d' > $tmp_dir/crt_server_ca
|
|
|
- crt_server_ca=$tmp_dir/crt_server_ca
|
|
|
- sed -i '/^\s*<ca>/,/\s*<\/ca>/d' ${config_file}
|
|
|
- sed -i '/^\s*ca\s/d' ${config_file}
|
|
|
- echo -e "\nca /etc/openvpn/keys/ca-server.crt" >> ${config_file}
|
|
|
- fi
|
|
|
- if grep -q '^\s*<cert>' ${config_file}
|
|
|
- then
|
|
|
- grep -Poz '(?<=<cert>)(.*\n)*.*(?=</cert>)' ${config_file} | sed '/^$/d' > $tmp_dir/crt_client
|
|
|
- crt_client=$tmp_dir/crt_client
|
|
|
- sed -i '/^\s*<cert>/,/\s*<\/cert>/d' ${config_file}
|
|
|
- sed -i '/^\s*cert\s/d' ${config_file}
|
|
|
- echo -e "\ncert /etc/openvpn/keys/user.crt" >> ${config_file}
|
|
|
- elif ! grep -q '^\s*cert\s' ${config_file}
|
|
|
- then
|
|
|
- crt_client=""
|
|
|
- fi
|
|
|
- if grep -q '^\s*<key>' ${config_file}
|
|
|
- then
|
|
|
- grep -Poz '(?<=<key>)(.*\n)*.*(?=</key>)' ${config_file} | sed '/^$/d' > $tmp_dir/crt_client_key
|
|
|
- crt_client_key=$tmp_dir/crt_client_key
|
|
|
- sed -i '/^\s*<key>/,/\s*<\/key>/d' ${config_file}
|
|
|
- sed -i '/^\s*key\s/d' ${config_file}
|
|
|
- echo -e "\nkey /etc/openvpn/keys/user.key" >> ${config_file}
|
|
|
- elif ! grep -q '^\s*key\s' ${config_file}
|
|
|
- then
|
|
|
- crt_client_key=""
|
|
|
- fi
|
|
|
- if grep -q '^\s*<tls-auth>' ${config_file}
|
|
|
- then
|
|
|
- grep -Poz '(?<=<tls-auth>)(.*\n)*.*(?=</tls-auth>)' ${config_file} | sed '/^$/d' > $tmp_dir/crt_client_ta
|
|
|
- crt_client_ta=$tmp_dir/crt_client_ta
|
|
|
- sed -i '/^\s*<tls-auth>/,/\s*<\/tls-auth>/d' ${config_file}
|
|
|
- sed -i '/^\s*tls-auth\s/d' ${config_file}
|
|
|
- echo -e "\ntls-auth /etc/openvpn/keys/user_ta.key 1" >> ${config_file}
|
|
|
- elif ! grep -q '^\s*tls-auth\s' ${config_file}
|
|
|
- then
|
|
|
- crt_client_ta=""
|
|
|
- fi
|
|
|
- sed -i 's@^\s*ca\s.*$@ca /etc/openvpn/keys/ca-server.crt@g' ${config_file}
|
|
|
- sed -i 's@^\s*cert\s.*$@cert /etc/openvpn/keys/user.crt@g' ${config_file}
|
|
|
- sed -i 's@^\s*key\s.*$@key /etc/openvpn/keys/user.key@g' ${config_file}
|
|
|
- sed -i 's@^\s*tls-auth\s.*$@tls-auth /etc/openvpn/keys/user_ta.key 1@g' ${config_file}
|
|
|
- fi
|
|
|
+ local tmp_dir=$(dirname "$config_file")
|
|
|
|
|
|
- # Currently we need root priviledge to create tun0
|
|
|
- if [ -f "${config_file}" ]
|
|
|
- then
|
|
|
- sed -i '/^\s*user\s/d' ${config_file}
|
|
|
- sed -i '/^\s*group\s/d' ${config_file}
|
|
|
+ ovpn_file="$tmp_dir/client.ovpn"
|
|
|
+ cp -f "$config_file" "$ovpn_file"
|
|
|
+
|
|
|
+ convert_ovpn_file "$config_file"
|
|
|
fi
|
|
|
|
|
|
_ynh_app_config_validate
|
|
@@ -359,6 +248,10 @@ ynh_app_config_apply() {
|
|
|
set_permissions /etc/openvpn/keys/user.key
|
|
|
set_permissions /etc/openvpn/keys/user_ta.key
|
|
|
|
|
|
+ # Cleanup previously uploaded config file
|
|
|
+ [[ "$cube_file" == "/etc/openvpn/client.cube" ]] && rm -f "$cube_file"
|
|
|
+ [[ "$ovpn_file" == "/etc/openvpn/client.ovpn" ]] && rm -f "$ovpn_file"
|
|
|
+
|
|
|
# Start vpn client
|
|
|
ynh_print_info --message="Starting vpnclient service if needed"
|
|
|
/usr/local/bin/ynh-vpnclient start
|