From 59db788bfb2f5724a27ccba1221d5e795dd18320 Mon Sep 17 00:00:00 2001 From: Bart Reardon Date: Fri, 26 May 2023 17:44:27 +1000 Subject: [PATCH 1/4] update console user code #27 --- Outset.xcodeproj/project.pbxproj | 8 ++++---- Outset/Functions/SystemUtils.swift | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Outset.xcodeproj/project.pbxproj b/Outset.xcodeproj/project.pbxproj index 53df693..9c5a943 100644 --- a/Outset.xcodeproj/project.pbxproj +++ b/Outset.xcodeproj/project.pbxproj @@ -468,7 +468,7 @@ "@executable_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 10.15; - MARKETING_VERSION = 4.0.1; + MARKETING_VERSION = 4.0.2; PRODUCT_BUNDLE_IDENTIFIER = io.macadmins.Outset; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; @@ -505,7 +505,7 @@ "@executable_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 10.15; - MARKETING_VERSION = 4.0.1; + MARKETING_VERSION = 4.0.2; PRODUCT_BUNDLE_IDENTIFIER = io.macadmins.Outset; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; @@ -543,7 +543,7 @@ LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks"; "LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks"; MACOSX_DEPLOYMENT_TARGET = 10.15; - MARKETING_VERSION = 4.0.1; + MARKETING_VERSION = 4.0.2; PRODUCT_BUNDLE_IDENTIFIER = io.macadmins.Outset; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; @@ -585,7 +585,7 @@ LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks"; "LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks"; MACOSX_DEPLOYMENT_TARGET = 10.15; - MARKETING_VERSION = 4.0.1; + MARKETING_VERSION = 4.0.2; PRODUCT_BUNDLE_IDENTIFIER = io.macadmins.Outset; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; diff --git a/Outset/Functions/SystemUtils.swift b/Outset/Functions/SystemUtils.swift index 324ca3b..d6f6b8f 100644 --- a/Outset/Functions/SystemUtils.swift +++ b/Outset/Functions/SystemUtils.swift @@ -109,9 +109,9 @@ func oslogTypeToString(_ type: OSLogType) -> String { func getConsoleUserInfo() -> (username: String, userID: String) { // We need the console user, not the process owner so NSUserName() won't work for our needs when outset runs as root - let consoleUserName = runShellCommand("who | grep 'console' | awk '{print $1}'").output - let consoleUserID = runShellCommand("id -u \(consoleUserName)").output - return (consoleUserName.trimmingCharacters(in: .whitespacesAndNewlines), consoleUserID.trimmingCharacters(in: .whitespacesAndNewlines)) + var uid: uid_t = 0 + let consoleUser = SCDynamicStoreCopyConsoleUser(nil, &uid, nil)! as String + return (consoleUser, "\(uid)") } func writePreferences(prefs: OutsetPreferences) { From 2ca69ce859c08f0f4a10e0592e75fdae20098411 Mon Sep 17 00:00:00 2001 From: Bart Reardon Date: Fri, 26 May 2023 18:06:40 +1000 Subject: [PATCH 2/4] Made a bit safer --- Outset/Functions/SystemUtils.swift | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Outset/Functions/SystemUtils.swift b/Outset/Functions/SystemUtils.swift index d6f6b8f..a0ea6d1 100644 --- a/Outset/Functions/SystemUtils.swift +++ b/Outset/Functions/SystemUtils.swift @@ -110,8 +110,11 @@ func oslogTypeToString(_ type: OSLogType) -> String { func getConsoleUserInfo() -> (username: String, userID: String) { // We need the console user, not the process owner so NSUserName() won't work for our needs when outset runs as root var uid: uid_t = 0 - let consoleUser = SCDynamicStoreCopyConsoleUser(nil, &uid, nil)! as String - return (consoleUser, "\(uid)") + if let consoleUser = SCDynamicStoreCopyConsoleUser(nil, &uid, nil) as? String { + return (consoleUser, "\(uid)") + } else { + return ("", "") + } } func writePreferences(prefs: OutsetPreferences) { From ef428f36b80e26a3b1940cd2f40fe51d7fb3b76c Mon Sep 17 00:00:00 2001 From: Bart Reardon Date: Fri, 26 May 2023 18:32:59 +1000 Subject: [PATCH 3/4] Clean up how login window state is managed --- Outset/Functions/SystemUtils.swift | 29 +++++++++++++++++------------ Outset/Outset.swift | 4 ++-- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/Outset/Functions/SystemUtils.swift b/Outset/Functions/SystemUtils.swift index a0ea6d1..708b831 100644 --- a/Outset/Functions/SystemUtils.swift +++ b/Outset/Functions/SystemUtils.swift @@ -36,6 +36,11 @@ extension String { } } +enum Action { + case enable + case disable +} + func ensureRoot(_ reason: String) { if !isRoot() { writeLog("Must be root to \(reason)", logLevel: .error) @@ -249,18 +254,18 @@ func waitForNetworkUp(timeout: Double) -> Bool { return networkUp } -func loginWindowDisable() { - // Disables the loginwindow process - writeLog("Disabling loginwindow process", logLevel: .debug) - let cmd = "/bin/launchctl unload /System/Library/LaunchDaemons/com.apple.loginwindow.plist" - _ = runShellCommand(cmd) -} - -func loginWindowEnable() { - // Enables the loginwindow process - writeLog("Enabling loginwindow process", logLevel: .debug) - let cmd = "/bin/launchctl load /System/Library/LaunchDaemons/com.apple.loginwindow.plist" - _ = runShellCommand(cmd) +func loginWindowUpdateState(_ action: Action) { + var cmd : String + var loginWindowPlist : String = "/System/Library/LaunchDaemons/com.apple.loginwindow.plist" + switch action { + case .enable: + writeLog("Enabling loginwindow process", logLevel: .debug) + cmd = "/bin/launchctl load \(loginWindowPlist)" + case .disable: + writeLog("Disabling loginwindow process", logLevel: .debug) + cmd = "/bin/launchctl unload \(loginWindowPlist)" + } + _ = runShellCommand(cmd) } func getDeviceHardwareModel() -> String { diff --git a/Outset/Outset.swift b/Outset/Outset.swift index 02e5725..5f3fa66 100644 --- a/Outset/Outset.swift +++ b/Outset/Outset.swift @@ -145,7 +145,7 @@ struct Outset: ParsableCommand { if !folderContents(path: bootOnceDir).isEmpty { if networkWait { loginwindowState = false - loginWindowDisable() + loginWindowUpdateState(.disable) continueFirstBoot = waitForNetworkUp(timeout: floor(Double(networkTimeout) / 10)) } if continueFirstBoot { @@ -155,7 +155,7 @@ struct Outset: ParsableCommand { writeLog("Unable to connect to network. Skipping boot-once scripts...", logLevel: .error) } if !loginwindowState { - loginWindowEnable() + loginWindowUpdateState(.enable) } } From 52b1dfd55609ad09506b4b9fc29460d76800c504 Mon Sep 17 00:00:00 2001 From: Bart Reardon Date: Fri, 26 May 2023 18:45:31 +1000 Subject: [PATCH 4/4] some cleanup of the writeLog function --- Outset/Functions/SystemUtils.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Outset/Functions/SystemUtils.swift b/Outset/Functions/SystemUtils.swift index 708b831..410e4b3 100644 --- a/Outset/Functions/SystemUtils.swift +++ b/Outset/Functions/SystemUtils.swift @@ -58,13 +58,13 @@ func getValueForKey(_ key: String, inArray array: [String: String]) -> String? { } func writeLog(_ message: String, logLevel: OSLogType = .info, log: OSLog = osLog) { - let logMessage = "\(message)" - - os_log("%{public}@", log: log, type: logLevel, logMessage) + // write to the system logs + os_log("%{public}@", log: log, type: logLevel, message) if logLevel == .error || logLevel == .info || (debugMode && logLevel == .debug) { // print info, errors and debug to stdout print("\(oslogTypeToString(logLevel).uppercased()): \(message)") } + // also write to a log file for accessability of those that don't want to manage the system log writeFileLog(message: message, logLevel: logLevel) }