Created
March 22, 2020 23:36
-
-
Save mlavergn/3dc981b1a7d275f00e8d956cb4b8b82d to your computer and use it in GitHub Desktop.
Objective-C AuthorizationExecuteWithPrivileges skeleton code
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/Foundation.h> | |
#import <Security/Security.h> | |
int main(int argc, const char * argv[]) { | |
@autoreleasepool { | |
AuthorizationRef authorizationRef; | |
OSStatus status; | |
status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &authorizationRef); | |
if (status != errAuthorizationSuccess) { | |
NSLog(@"failed to create auth request"); | |
return -1; | |
} | |
char* tool = "/usr/bin/touch"; | |
char* args[] = { "/etc/hosts", NULL }; | |
FILE* pipe = NULL; | |
status = AuthorizationExecuteWithPrivileges(authorizationRef, tool, kAuthorizationFlagDefaults, args, pipe); | |
if (status != errAuthorizationSuccess) { | |
NSLog(@"failed to obtain auth priviledge %i", status); | |
return -1; | |
} | |
AuthorizationFree(authorizationRef, kAuthorizationFlagDestroyRights); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment