From 211fc457329b074fd4331aec0c4fc5d765e9023f Mon Sep 17 00:00:00 2001
From: kali <kali@leap.se>
Date: Thu, 23 Jul 2020 18:29:53 +0200
Subject: [PATCH] [pkg] post-installer script

---
 installer/bitmask-installer.pro               |  1 +
 .../packages/riseupvpn/data/post-install.py   | 80 +++++++++++++++++++
 installer/packages/riseupvpn/meta/install.js  | 26 +++---
 3 files changed, 95 insertions(+), 12 deletions(-)
 create mode 100755 installer/packages/riseupvpn/data/post-install.py

diff --git a/installer/bitmask-installer.pro b/installer/bitmask-installer.pro
index c91a618a..1435e4c6 100644
--- a/installer/bitmask-installer.pro
+++ b/installer/bitmask-installer.pro
@@ -21,6 +21,7 @@ OTHER_FILES += \
 macx {
     OTHER_FILES += "packages/riseupvpn/data/riseup-vpn.app"
     OTHER_FILES += "packages/riseupvpn/data/bitmask-helper"
+    OTHER_FILES += "packages/riseupvpn/data/installer.py"
 }
 linux {
     OTHER_FILES += "packages/riseupvpn/data/riseup-vpn"
diff --git a/installer/packages/riseupvpn/data/post-install.py b/installer/packages/riseupvpn/data/post-install.py
new file mode 100755
index 00000000..1e1addd6
--- /dev/null
+++ b/installer/packages/riseupvpn/data/post-install.py
@@ -0,0 +1,80 @@
+#!/usr/bin/env python
+
+import os
+import sys
+import subprocess
+
+HELPER = "bitmask-helper"
+HELPER_PLIST = "/Library/LaunchDaemons/se.leap.bitmask-helper.plist"
+
+def main():
+    _dir = os.path.dirname(os.path.realpath(__file__))
+    log = open(os.path.join(_dir, 'post-install.log'), 'w')
+    log.write('Checking for admin privileges...')
+
+    _id = os.getuid()
+    if _id != 0:
+      err  = "error: need to run as root. UID: %s\n" % str(_id)
+      logErr(log, msg)
+    
+    # failure: sys.exit(1)
+    
+    if isHelperRunning():
+        log.write("Trying to stop bitmask-helper...")
+	# if this fail, we can check if the HELPER_PLIST is there
+        ok = unloadHelper()
+        log.write("success: %s \n" % str(ok))
+
+    ok = makeHelperExecutable()
+    log.write("chmod +x helper: %s \n" % str(ok))
+
+    # 3. cp se.leap.bitmask-helper.plist /Library/LaunchDaemons/
+    copyLaunchDaemon()
+
+    # 4. launchctl load /Library/LaunchDaemons/se.leap.bitmask-helper.plist
+    launchHelper()
+
+    # 5. chown admin:wheel /Applications/$applicationName.app/Contents/helper # is this the folder?
+    grantPermissionsOnLogFolder()
+    
+    # all good
+    log.write('post-install script: done')
+    sys.exit(0)
+
+
+def logErr(log, msg):
+    log.write(err)
+    sys.exit(1)
+
+def isHelperRunning():
+    ps = _getProcessList()
+    return HELPER in ps 
+
+def unloadHelper():
+    out = subprocess.call(["launchctl", "unload", HELPER_PLIST])
+    return out == 0
+
+def makeHelperExecutable():
+    out = subprocess.call(["chmod", "+x", HELPER])
+    return out == 0
+
+def copyLaunchDaemon():
+    pass
+
+def launchHelper():
+    pass
+
+def grantPermissionsOnLogFolder():
+    pass
+
+def _getProcessList():
+    _out = []
+    output = subprocess.Popen(["ps", "-ceA"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+    stdout, stderr = output.communicate()
+    for line  in stdout.split('\n'):
+        cmd = line.split(' ')[-1]
+        _out.append(cmd.strip())
+    return _out
+
+if __name__ == "__main__":
+    main()
diff --git a/installer/packages/riseupvpn/meta/install.js b/installer/packages/riseupvpn/meta/install.js
index e5174e17..f9c85f2b 100644
--- a/installer/packages/riseupvpn/meta/install.js
+++ b/installer/packages/riseupvpn/meta/install.js
@@ -18,34 +18,36 @@ Component.prototype.createOperations = function ()
     // We can also use this to register different components (different architecture for instance)
     // See https://doc.qt.io/qtinstallerframework/qt-installer-framework-systeminfo-packages-root-meta-installscript-qs.html
 
-    console.log("Post installation. Checking platform...")
     if (systemInfo.productType === "windows") {
-        console.log("Platform: windows");
         postInstallWindows();
     } else if (systemInfo.productType === "osx") {
-        console.log("Platform: osx");
         postInstallOSX();
     } else {
-        console.log("Platform: linux");
         postInstallLinux();
     }
 }
 
 function postInstallWindows() {
-    component.addOperation("CreateShortcut",
-                   "@TargetDir@/README.txt",
-                   "@StartMenuDir@/README.lnk",
-                   "workingDirectory=@TargetDir@",
-                   "iconPath=%SystemRoot%/system32/SHELL32.dll",
-                   "iconId=2");
+    component.addOperation(
+	"CreateShortcut",
+	"@TargetDir@/README.txt",
+	"@StartMenuDir@/README.lnk",
+	"workingDirectory=@TargetDir@",
+	"iconPath=%SystemRoot%/system32/SHELL32.dll",
+	"iconId=2");
 }
 
 function postInstallOSX() {
-    console.log("TODO: should do osx post-installation");
+    console.log("Post-installation for OSX");
+    // TODO add UNDOEXECUTE for the uninstaller
+    component.addElevatedOperation(
+	"Execute", "{0}",
+   	"@TargetDir@/post-install.py",
+	"errormessage=There was an error during the post-installation script, things might be broken. Please report this error and attach the post-install.log file.");
 }
 
 function postInstallLinux() {
-    console.log("TODO: should do linux post-installation");
+    console.log("Post-installation for GNU/Linux");
     console.log("Maybe you want to use your package manager instead?");
     component.addOperation("AppendFile", "/tmp/riseupvpn.log", "this is a test - written from the installer");
 }
-- 
GitLab