]> www.infradead.org Git - users/dwmw2/vpnc-scripts.git/commitdiff
Ensure that vpnc-script-win.js works even if INTERNAL_IP4_{NETADDR,NETMASK} are unset
authorDaniel Lenski <dlenski@gmail.com>
Fri, 24 Sep 2021 15:07:00 +0000 (08:07 -0700)
committerDaniel Lenski <dlenski@gmail.com>
Fri, 24 Sep 2021 22:04:22 +0000 (15:04 -0700)
Not all protocols supported by OpenConnect consistently set the variables
INTERNAL_IP4_{NETADDR,NETMASK} in all cases.

This should be fixed within OpenConnect itself as part of
https://gitlab.com/openconnect/openconnect/-/merge_requests/215, but in the
meantime (and for older versions of OpenConnect/vpnc) we need to ensure that
vpnc-script-win.js works correctly even when these are unset.

Signed-off-by: Daniel Lenski <dlenski@gmail.com>
vpnc-script-win.js

index 3e8043a3c92bd916b13eddd3b92bbda71e654f04..63235c83bb9aec431d40351a1410949f696cb8c0 100644 (file)
@@ -69,14 +69,19 @@ case "connect":
     // Calculate the first legal host address in subnet
     // (identical to the INTERNAL_IP4_ADDRESS if the netmask is
     // 255.255.255.255, otherwise increment the last octet)
-    var internal_gw_array = env("INTERNAL_IP4_NETADDR").split(".");
-    if (env("INTERNAL_IP4_NETMASK").trim() != "255.255.255.255" && env("INTERNAL_IP4_NETMASKLEN") != 32)
+    // We also need to work around the fact that
+    // INTERNAL_IP4_{NETMASK,NETADDR} are not always set for
+    // all protocols.
+    var internal_ip4_netmask = env("INTERNAL_IP4_NETMASK") || "255.255.255.255";
+    var internal_ip4_netaddr = env("INTERNAL_IP4_NETADDR") || env("INTERNAL_IP4_ADDRESS");
+    var internal_gw_array = internal_ip4_netaddr.split(".");
+    if (internal_ip4_netmask.trim() != "255.255.255.255" && env("INTERNAL_IP4_NETMASKLEN") != 32)
         internal_gw_array[3]++;
     var internal_gw = internal_gw_array.join(".");
 
     echo("VPN Gateway: " + env("VPNGATEWAY"));
     echo("Internal Address: " + env("INTERNAL_IP4_ADDRESS"));
-    echo("Internal Netmask: " + env("INTERNAL_IP4_NETMASK"));
+    echo("Internal Netmask: " + internal_ip4_netmask);
     echo("Internal Gateway: " + internal_gw);
     echo("Interface: \"" + env("TUNDEV") + "\" / " + env("TUNIDX"));
 
@@ -101,11 +106,11 @@ case "connect":
 
     if (env("CISCO_SPLIT_INC") || REDIRECT_GATEWAY_METHOD > 0) {
         run("netsh interface ip set address \"" + env("TUNIDX") + "\" static " +
-            env("INTERNAL_IP4_ADDRESS") + " " + env("INTERNAL_IP4_NETMASK") + " store=active");
+            env("INTERNAL_IP4_ADDRESS") + " " + internal_ip4_netmask + " store=active");
     } else {
         // The default route will be added automatically
         run("netsh interface ip set address \"" + env("TUNIDX") + "\" static " +
-            env("INTERNAL_IP4_ADDRESS") + " " + env("INTERNAL_IP4_NETMASK") + " " + internal_gw +
+            env("INTERNAL_IP4_ADDRESS") + " " + internal_ip4_netmask + " " + internal_gw +
             " gwmetric=1 store=active");
     }