Print a warning if an illegal value is used for the spi but continue
[vpnc.git] / vpnc-script-win.js
blob1664d9baa12622a1500d3c4de04d5e1775270bbe
1 // vpnc-script-win.js
2 //
3 // Sets up the Network interface and the routes
4 // needed by vpnc.
6 // --------------------------------------------------------------
7 // Utilities
8 // --------------------------------------------------------------
10 function echo(msg)
12         WScript.echo(msg);
15 function run(cmd)
17         return (ws.Exec(cmd).StdOut.ReadAll());
20 function getDefaultGateway()
22         if (run("route print").match(/Default Gateway: *(.*)/)) {
23                 return (RegExp.$1);
24         }
25         return ("");
28 // --------------------------------------------------------------
29 // Script starts here
30 // --------------------------------------------------------------
32 var internal_ip4_netmask = "255.255.255.0"
34 var ws = WScript.CreateObject("WScript.Shell");
35 var env = ws.Environment("Process");
37 switch (env("reason")) {
38 case "pre-init":
39         break;
40 case "connect":
41         var gw = getDefaultGateway();
42         echo("VPN Gateway: " + env("VPNGATEWAY"));
43         echo("Internal Address: " + env("INTERNAL_IP4_ADDRESS"));
44         echo("Internal Netmask: " + env("INTERNAL_IP4_NETMASK"));
45         echo("Interface: \"" + env("TUNDEV") + "\"");
47         if (env("INTERNAL_IP4_NETMASK")) {
48             internal_ip4_netmask = env("INTERNAL_IP4_NETMASK");
49         }
51         echo("Configuring \"" + env("TUNDEV") + "\" interface...");
52         run("netsh interface ip set address \"" + env("TUNDEV") + "\" static " +
53             env("INTERNAL_IP4_ADDRESS") + " " + internal_ip4_netmask);
55         // Add direct route for the VPN gateway to avoid routing loops
56         run("route add " + env("VPNGATEWAY") +
57             " mask 255.255.255.255 " + gw);
59         if (env("INTERNAL_IP4_NBNS")) {
60                 var wins = env("INTERNAL_IP4_NBNS").split(/ /);
61                 for (var i = 0; i < wins.length; i++) {
62                         run("netsh interface ip add wins \"" +
63                             env("TUNDEV") + "\" " + wins[i]
64                             + " index=" + (i+1));
65                 }
66         }
68         if (env("INTERNAL_IP4_DNS")) {
69                 var dns = env("INTERNAL_IP4_DNS").split(/ /);
70                 for (var i = 0; i < dns.length; i++) {
71                         run("netsh interface ip add dns \"" +
72                             env("TUNDEV") + "\" " + dns[i]
73                             + " index=" + (i+1));
74                 }
75         }
76         echo("done.");
78         // Add internal network routes
79         echo("Configuring networks:");
80         if (env("CISCO_SPLIT_INC")) {
81                 for (var i = 0 ; i < parseInt(env("CISCO_SPLIT_INC")); i++) {
82                         var network = env("CISCO_SPLIT_INC_" + i + "_ADDR");
83                         var netmask = env("CISCO_SPLIT_INC_" + i + "_MASK");
84                         var netmasklen = env("CISCO_SPLIT_INC_" + i +
85                                          "_MASKLEN");
86                         run("route add " + network + " mask " + netmask +
87                              " " + env("INTERNAL_IP4_ADDRESS"));
88                 }
89         } else {
90                 echo("Gateway did not provide network configuration.");
91         }
92         echo("Route configuration done.");
94         if (env("CISCO_BANNER")) {
95                 echo("--------------------------------------------------");
96                 echo(env("CISCO_BANNER"));
97                 echo("--------------------------------------------------");
98         }
99         break;
100 case "disconnect":
101         // Delete direct route for the VPN gateway to avoid
102         run("route delete " + env("VPNGATEWAY") + " mask 255.255.255.255");