|
7 | 7 |
|
8 | 8 | import Foundation |
9 | 9 |
|
10 | | -print("Hello, World!") |
| 10 | +// Constants |
| 11 | +let abeProfileIdentifier = "com.apple.profile.mdm" |
| 12 | +let logoPath = "/Library/Application Support/Rocketman/funcfi-logo-small.png" |
11 | 13 |
|
| 14 | +// Check if ABE profile exists |
| 15 | +func checkProfileExists(identifier: String) -> Bool { |
| 16 | + let process = Process() |
| 17 | + let pipe = Pipe() |
| 18 | + |
| 19 | + process.executableURL = URL(fileURLWithPath: "/usr/bin/profiles") |
| 20 | + process.arguments = ["list"] |
| 21 | + process.standardOutput = pipe |
| 22 | + |
| 23 | + do { |
| 24 | + try process.run() |
| 25 | + process.waitUntilExit() |
| 26 | + |
| 27 | + let data = pipe.fileHandleForReading.readDataToEndOfFile() |
| 28 | + if let output = String(data: data, encoding: .utf8) { |
| 29 | + return output.contains(identifier) |
| 30 | + } |
| 31 | + } catch { |
| 32 | + print("Error checking profiles: \(error)") |
| 33 | + } |
| 34 | + |
| 35 | + return false |
| 36 | +} |
| 37 | + |
| 38 | +// Try to remove ABE profile |
| 39 | +func removeProfile(identifier: String) -> Bool { |
| 40 | + let process = Process() |
| 41 | + |
| 42 | + process.executableURL = URL(fileURLWithPath: "/usr/bin/profiles") |
| 43 | + process.arguments = ["remove", "-identifier", identifier] |
| 44 | + |
| 45 | + do { |
| 46 | + try process.run() |
| 47 | + process.waitUntilExit() |
| 48 | + |
| 49 | + // Wait a moment for the system to update |
| 50 | + sleep(2) |
| 51 | + |
| 52 | + // Check if profile was successfully removed |
| 53 | + return !checkProfileExists(identifier: identifier) |
| 54 | + } catch { |
| 55 | + print("Error removing profile: \(error)") |
| 56 | + return false |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// Show dialog to user |
| 61 | +func showDialog(title: String, message: String, buttonText: String, iconPath: String) -> Bool { |
| 62 | + let process = Process() |
| 63 | + let dialogPath = "/usr/local/bin/dialog" |
| 64 | + |
| 65 | + // Check if dialog exists |
| 66 | + if !FileManager.default.fileExists(atPath: dialogPath) { |
| 67 | + print("SwiftDialog not found at \(dialogPath)") |
| 68 | + return false |
| 69 | + } |
| 70 | + |
| 71 | + process.executableURL = URL(fileURLWithPath: dialogPath) |
| 72 | + process.arguments = [ |
| 73 | + "--title", title, |
| 74 | + "--message", message, |
| 75 | + "--icon", iconPath, |
| 76 | + "--button1text", buttonText, |
| 77 | + ] |
| 78 | + |
| 79 | + do { |
| 80 | + try process.run() |
| 81 | + process.waitUntilExit() |
| 82 | + return process.terminationStatus == 0 |
| 83 | + } catch { |
| 84 | + print("Error showing dialog: \(error)") |
| 85 | + return false |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// Start Jamf enrollment |
| 90 | +func startJamfEnrollment() { |
| 91 | + let process = Process() |
| 92 | + |
| 93 | + process.executableURL = URL(fileURLWithPath: "/usr/bin/profiles") |
| 94 | + process.arguments = ["renew", "-type", "enrollment"] |
| 95 | + |
| 96 | + do { |
| 97 | + try process.run() |
| 98 | + process.waitUntilExit() |
| 99 | + } catch { |
| 100 | + print("Error starting Jamf enrollment: \(error)") |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// Check if running with admin privileges |
| 105 | +func isRunningAsAdmin() -> Bool { |
| 106 | + let process = Process() |
| 107 | + let pipe = Pipe() |
| 108 | + |
| 109 | + process.executableURL = URL(fileURLWithPath: "/usr/bin/id") |
| 110 | + process.arguments = ["-u"] |
| 111 | + process.standardOutput = pipe |
| 112 | + |
| 113 | + do { |
| 114 | + try process.run() |
| 115 | + process.waitUntilExit() |
| 116 | + |
| 117 | + let data = pipe.fileHandleForReading.readDataToEndOfFile() |
| 118 | + if let output = String(data: data, encoding: .utf8)?.trimmingCharacters( |
| 119 | + in: .whitespacesAndNewlines), |
| 120 | + let uid = Int(output) |
| 121 | + { |
| 122 | + return uid == 0 |
| 123 | + } |
| 124 | + } catch { |
| 125 | + print("Error checking privileges: \(error)") |
| 126 | + } |
| 127 | + |
| 128 | + return false |
| 129 | +} |
| 130 | + |
| 131 | +// Main function |
| 132 | +func main() { |
| 133 | + print("Starting migration from Apple Business Essentials to Jamf Pro...") |
| 134 | + |
| 135 | + // Check for admin privileges |
| 136 | + guard isRunningAsAdmin() else { |
| 137 | + print("Error: This tool must be run with administrator privileges") |
| 138 | + exit(1) |
| 139 | + } |
| 140 | + |
| 141 | + // Try to remove ABE profile |
| 142 | + let removed = removeProfile(identifier: abeProfileIdentifier) |
| 143 | + |
| 144 | + if removed { |
| 145 | + print("ABE profile successfully removed.") |
| 146 | + |
| 147 | + // Show dialog to user |
| 148 | + let dialogShown = showDialog( |
| 149 | + title: "Migrating to Jamf Pro", |
| 150 | + message: "Your Mac needs to be migrated to Jamf Pro. Please press Continue when ready.", |
| 151 | + buttonText: "Continue", |
| 152 | + iconPath: logoPath |
| 153 | + ) |
| 154 | + |
| 155 | + if dialogShown { |
| 156 | + // Start Jamf enrollment |
| 157 | + print("Starting Jamf enrollment...") |
| 158 | + startJamfEnrollment() |
| 159 | + } else { |
| 160 | + print("Failed to show dialog. Please enroll in Jamf Pro manually.") |
| 161 | + } |
| 162 | + } else { |
| 163 | + print( |
| 164 | + "ABE profile could not be removed directly." |
| 165 | + ) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +// Run the main function |
| 170 | +main() |
0 commit comments