Created
August 8, 2018 21:51
-
-
Save KyNorthstar/6619bbba8ebb09e6252459521e01ec4b to your computer and use it in GitHub Desktop.
Elevates permissions in non-sandboxed Swift apps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
private func gainPermissions() { | |
var authorizationRef: AuthorizationRef? = nil | |
var authItem = AuthorizationItem(name: kSMRightBlessPrivilegedHelper, valueLength: 0, value: nil, flags: 0) | |
var authRights = AuthorizationRights(count: 1, items: &authItem) | |
let flags: AuthorizationFlags = [.interactionAllowed, .preAuthorize, .extendRights] | |
var environment = AuthorizationEnvironment() | |
var authStatus = AuthorizationCreate(&authRights, &environment, flags, &authorizationRef) | |
guard let authRef = authorizationRef else { | |
NSLog("Failed to get authorization! \(authStatus) - \(String(authStatus: authStatus))") | |
return | |
} | |
authStatus = AuthorizationCopyRights(authRef, &authRights, &environment, flags, nil) | |
NSLog("Auth status #\(authStatus) - \(String(authStatus: authStatus))") | |
} | |
private extension String { | |
init(authStatus: OSStatus) { | |
switch authStatus { | |
case errAuthorizationSuccess: | |
self = "Success" | |
case errAuthorizationDenied: | |
self = "Denied" | |
case errAuthorizationCanceled: | |
self = "Cancelled" | |
case errAuthorizationInternal: | |
self = "Internal error" | |
case errAuthorizationBadAddress: | |
self = "Bad address" | |
case errAuthorizationInvalidRef: | |
self = "Invalid reference" | |
case errAuthorizationInvalidSet: | |
self = "Invalid set" | |
case errAuthorizationInvalidTag: | |
self = "Invalid tag" | |
case errAuthorizationInvalidFlags: | |
self = "Invalid flags" | |
case errAuthorizationInvalidPointer: | |
self = "Invalid pointer" | |
case errAuthorizationToolExecuteFailure: | |
self = "Tool execution failure" | |
case errAuthorizationToolEnvironmentError: | |
self = "Tool environment error" | |
case errAuthorizationExternalizeNotAllowed: | |
self = "Reference externalization not allowed" | |
case errAuthorizationInteractionNotAllowed: | |
self = "Interaction not allowed" | |
case errAuthorizationInternalizeNotAllowed: | |
self = "Reference internalization not allowed" | |
default: | |
self = "Unknown auth failure" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment