Skip to content

Instantly share code, notes, and snippets.

@mlavergn
Created March 22, 2020 23:36
Show Gist options
  • Save mlavergn/3dc981b1a7d275f00e8d956cb4b8b82d to your computer and use it in GitHub Desktop.
Save mlavergn/3dc981b1a7d275f00e8d956cb4b8b82d to your computer and use it in GitHub Desktop.
Objective-C AuthorizationExecuteWithPrivileges skeleton code
#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