aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/installer/customactions.js
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2019-05-06 22:44:22 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2019-05-07 08:26:22 +0200
commit17c97fef973d5953586d83291e5bfe5d8ff2fc68 (patch)
tree71d0a5d673be863605bff25c444d029bb78784c8 /installer/customactions.js
parentringlogger: export R/O handle for UI process (diff)
downloadwireguard-windows-17c97fef973d5953586d83291e5bfe5d8ff2fc68.tar.xz
wireguard-windows-17c97fef973d5953586d83291e5bfe5d8ff2fc68.zip
installer: delete config file with custom action
Diffstat (limited to 'installer/customactions.js')
-rw-r--r--installer/customactions.js97
1 files changed, 97 insertions, 0 deletions
diff --git a/installer/customactions.js b/installer/customactions.js
new file mode 100644
index 00000000..9ca93ca5
--- /dev/null
+++ b/installer/customactions.js
@@ -0,0 +1,97 @@
+/* SPDX-License-Identifier: MIT
+ *
+ * Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
+ */
+
+var wsh = new ActiveXObject("WScript.Shell");
+var shl = new ActiveXObject("Shell.Application");
+var fso = new ActiveXObject("Scripting.FileSystemObject");
+
+function logMessage(msg) {
+ var record = Installer.CreateRecord(1);
+ record.StringData(0) = "Custom action: [1]";
+ record.StringData(1) = msg.toString();
+ Session.Message(0x04000000, record);
+}
+
+// I'd rather use wsh.Exec, so that we can just do ".Stdout.ReadAll()", but
+// this results in a scary flashing command window. The below is the best
+// workaround we can find yet. We'll keep searching for more clever tricks.
+function runWithNoWindowFlash(command) {
+ //TODO: Seems pretty unlikely that this temp file is secure...
+ var tmpfile = fso.BuildPath(fso.GetSpecialFolder(2), fso.GetTempName());
+ try {
+ //TODO: Obviously cmd and tmpfile are unescaped here...
+ var cmd = fso.BuildPath(fso.GetSpecialFolder(1), "cmd.exe") + " /c " + command + " > " + tmpfile;
+ var ret = wsh.Run(cmd, 0, true);
+ if (ret != 0) {
+ logMessage("Command " + cmd + " exited with error " + ret.toString());
+ return "";
+ }
+ var txt;
+ try {
+ var file = fso.OpenTextFile(tmpfile, 1);
+ txt = file.ReadAll();
+ file.Close();
+ } catch (e) {
+ logMessage("Unable to read temporary file " + tmpfile + " for command " + cmd + ": " + e.message);
+ return "";
+ }
+ return txt;
+ } finally {
+ try {
+ fso.DeleteFile(tmpfile);
+ } catch (e) {}
+ }
+}
+
+function EvaluateWireGuardServices() {
+ var inst = Session.Installer;
+ var db = Session.Database;
+ var view = db.OpenView("INSERT INTO `ServiceControl` (`ServiceControl`, `Name`, `Event`, `Component_`) VALUES(?, ?, ?, ?) TEMPORARY");
+ var rec = inst.CreateRecord(4);
+ var serviceKey = "HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Services";
+ var servicePrefix = "WireGuardTunnel$";
+ var serviceKeyPrefix = serviceKey + "\\" + servicePrefix;
+ var allowedNameFormat = new RegExp("^[a-zA-Z0-9_=+.-]{1,32}$");
+ var msiOperators = new RegExp("[=+-]", "g");
+ var index = 0;
+
+ function insertServiceControl(serviceName) {
+ var flags = 0x2/*msidbServiceControlEventStop*/ | 0x20/*msidbServiceControlEventUninstallStop*/ | 0x80/*msidbServiceControlEventUninstallDelete*/;
+
+ if (shl.IsServiceRunning(serviceName)) {
+ flags |= 0x1/*msidbServiceControlEventStart*/;
+ logMessage("Scheduling stop on upgrade/uninstall and removal on uninstall of service " + serviceName);
+ } else {
+ logMessage("Scheduling removal on uninstall of service " + serviceName);
+ }
+
+ rec.StringData (1/*ServiceControl*/) = serviceName.replace(msiOperators, "_") + (index++).toString();
+ rec.StringData (2/*Name */) = serviceName;
+ rec.IntegerData(3/*Event */) = flags;
+ rec.StringData (4/*Component_ */) = "WireGuardExecutable";
+
+ view.Execute(rec);
+ }
+
+ insertServiceControl("WireGuardManager");
+
+ var txt = runWithNoWindowFlash(fso.BuildPath(fso.GetSpecialFolder(1), "reg.exe") + " query \"" + serviceKey + "\"");
+ var lines = txt.split(new RegExp("\r?\n", "g"));
+ for (var i = 0; i < lines.length; ++i) {
+ if (lines[i].length > serviceKeyPrefix.length && lines[i].substring(0, serviceKeyPrefix.length) == serviceKeyPrefix) {
+ var tunnelName = lines[i].substring(serviceKeyPrefix.length);
+ if (tunnelName.match(allowedNameFormat) != null)
+ insertServiceControl(servicePrefix + tunnelName);
+ }
+ }
+}
+
+function RemoveConfigFolder() {
+ try {
+ fso.DeleteFolder(fso.BuildPath(fso.GetSpecialFolder(1), "config\\systemprofile\\AppData\\Local\\WireGuard"), true);
+ } catch(e) {
+ logMessage("Failed to remove configuration on uninstall: " + e.message);
+ }
+}