From b749c2cadc2f32e2efffa69302861f9a7d4a4e5f Mon Sep 17 00:00:00 2001 From: Daniel Lenski Date: Fri, 24 Sep 2021 08:07:00 -0700 Subject: [PATCH] Ensure that vpnc-script-win.js works even if INTERNAL_IP4_{NETADDR,NETMASK} are unset 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 --- vpnc-script-win.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/vpnc-script-win.js b/vpnc-script-win.js index 3e8043a..63235c8 100644 --- a/vpnc-script-win.js +++ b/vpnc-script-win.js @@ -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"); } -- 2.49.0