Browse Source

fix unbound variable and force set variables stored in cube file

HgO 5 months ago
parent
commit
58a241d502
1 changed files with 34 additions and 23 deletions
  1. 34 23
      scripts/config

+ 34 - 23
scripts/config

@@ -106,6 +106,7 @@ get__login_passphrase() {
 #=================================================
 #=================================================
 # SPECIFIC VALIDATORS FOR TOML SHORT KEYS
 # SPECIFIC VALIDATORS FOR TOML SHORT KEYS
 #=================================================
 #=================================================
+
 validate__login_user() {
 validate__login_user() {
 
 
     if grep -q '^\s*auth-user-pass' ${config_file}
     if grep -q '^\s*auth-user-pass' ${config_file}
@@ -173,9 +174,11 @@ validate__nameservers() {
         echo "You need to choose DNS resolvers or select an other method to provide DNS resolvers"
         echo "You need to choose DNS resolvers or select an other method to provide DNS resolvers"
     fi
     fi
 }
 }
+
 #=================================================
 #=================================================
 # SPECIFIC SETTERS FOR TOML SHORT KEYS
 # SPECIFIC SETTERS FOR TOML SHORT KEYS
 #=================================================
 #=================================================
+
 set__login_user() {
 set__login_user() {
     if [ -n "${login_user}" ]
     if [ -n "${login_user}" ]
     then
     then
@@ -191,37 +194,35 @@ set__login_passphrase() {
     :
     :
 }
 }
 
 
-
 #=================================================
 #=================================================
 # OVERWRITING VALIDATE STEP
 # OVERWRITING VALIDATE STEP
 #=================================================
 #=================================================
+
 ynh_app_config_validate() {
 ynh_app_config_validate() {
     # At this moment this var is not already set with the old value
     # At this moment this var is not already set with the old value
-    if [ -z ${config_file+x} ]
+    if [[ -n "${config_file:-}" ]]; then
-    then
+        # Overwrite form response with cube files data before validation process
-        config_file="${old[config_file]}"
 
 
-    # Overwrite form response with cube files data before validation process
+        # We don't have the extension, so we use this ugly hack to check that this is a json-like
+        # (i.e. it starts with { ..)
+        if [[ -f "${config_file}" ]]; then
+            if [[ "$(cat ${config_file} | tr -d ' ' | grep -v "^$" | head -c1)" == "{" ]]; then
+                local tmp_dir=$(dirname "$config_file")
 
 
-    # We don't have the extension, so we use this ugly hack to check that this is a json-like
+                cube_file="$tmp_dir/client.cube"
-    # (i.e. it starts with { ..)
+                cp -f "$config_file" "$cube_file"
-    elif [ -f "${config_file}" ] && [[ "$(cat ${config_file} | tr -d ' ' | grep -v "^$" | head -c1)" == "{" ]]
-    then
-        local tmp_dir=$(dirname "$config_file")
 
 
-        cube_file="$tmp_dir/client.cube"
+                convert_cube_file "$config_file"
-        cp -f "$config_file" "$cube_file"
+            # Othewise, assume that it's a .ovpn / .conf
+            else
+                local tmp_dir=$(dirname "$config_file")
 
 
-        convert_cube_file "$config_file"
+                ovpn_file="$tmp_dir/client.ovpn"
-    # Othewise, assume that it's a .ovpn / .conf
+                cp -f "$config_file" "$ovpn_file"
-    elif [ -f "${config_file}" ]
-    then
-        local tmp_dir=$(dirname "$config_file")
-
-        ovpn_file="$tmp_dir/client.ovpn"
-        cp -f "$config_file" "$ovpn_file"
 
 
-        convert_ovpn_file "$config_file"
+                convert_ovpn_file "$config_file"
+            fi
+        fi
     fi
     fi
 
 
     _ynh_app_config_validate
     _ynh_app_config_validate
@@ -230,6 +231,7 @@ ynh_app_config_validate() {
 #=================================================
 #=================================================
 # OVERWRITING APPLY STEP
 # OVERWRITING APPLY STEP
 #=================================================
 #=================================================
+
 ynh_app_config_apply() {
 ynh_app_config_apply() {
 
 
     # Stop vpn client
     # Stop vpn client
@@ -242,6 +244,15 @@ ynh_app_config_apply() {
 
 
     _ynh_app_config_apply
     _ynh_app_config_apply
 
 
+    # If we are uploading a cube file, then the file would be in a temporary folder
+    # Otherwise, we aren't uploading a cube file, then the path is either empty 
+    # or takes the value of the previous upload, that is, the target path for the cube file.
+    if [[ -n "${cube_file:-}" && "$cube_file" != "/etc/openvpn/client.cube" ]]; then
+      ynh_app_setting_set $app ip6_addr "$ip6_addr"
+      ynh_app_setting_set $app ip6_net "$ip6_net"
+      ynh_app_setting_set $app ip6_send_over_tun_enabled "$ip6_send_over_tun_enabled"
+    fi
+
     if [[ ${ip6_send_over_tun_enabled} -eq 1 ]]; then
     if [[ ${ip6_send_over_tun_enabled} -eq 1 ]]; then
         install -b -o root -g root -m 0755 ../conf/optional-scripts/route-up.d/50-vpnclient-set-ipv6-send-over-tun /etc/openvpn/scripts/route-up.d/
         install -b -o root -g root -m 0755 ../conf/optional-scripts/route-up.d/50-vpnclient-set-ipv6-send-over-tun /etc/openvpn/scripts/route-up.d/
         install -b -o root -g root -m 0755 ../conf/optional-scripts/route-down.d/50-vpnclient-unset-ipv6-send-over-tun /etc/openvpn/scripts/route-down.d/
         install -b -o root -g root -m 0755 ../conf/optional-scripts/route-down.d/50-vpnclient-unset-ipv6-send-over-tun /etc/openvpn/scripts/route-down.d/
@@ -257,8 +268,8 @@ ynh_app_config_apply() {
     set_permissions /etc/openvpn/keys/user_ta.key
     set_permissions /etc/openvpn/keys/user_ta.key
 
 
     # Cleanup previously uploaded config file
     # Cleanup previously uploaded config file
-    [[ "$cube_file" == "/etc/openvpn/client.cube" ]] && rm -f "$cube_file"
+    [[ -n "${cube_file:-}" && "$cube_file" == "/etc/openvpn/client.cube" ]] && rm -f "$cube_file"
-    [[ "$ovpn_file" == "/etc/openvpn/client.ovpn" ]] && rm -f "$ovpn_file"
+    [[ -n "${ovpn_file:-}" && "$ovpn_file" == "/etc/openvpn/client.ovpn" ]] && rm -f "$ovpn_file"
 
 
     # Start vpn client
     # Start vpn client
     ynh_print_info --message="Starting vpnclient service if needed"
     ynh_print_info --message="Starting vpnclient service if needed"