2

I'm trying to run a few lines of code as root in my Cocoa-App. I've tried to realize this in getting root privileges with AuthorizationCreate and releasing these privileges after running the code.

Problem is, I do not have root privileges in the //Run my code section and do not know why.

Hope someone can figure out why. Thanks!

   AuthorizationRef myAuthorizationRef;
OSStatus myStatus;

myStatus = AuthorizationCreate (NULL, kAuthorizationEmptyEnvironment,
                                kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed , &myAuthorizationRef);
AuthorizationItem myItems[1];

myItems[0].name = kAuthorizationRightExecute;
myItems[0].valueLength = 0;
myItems[0].value = NULL;
myItems[0].flags = 0;

AuthorizationRights myRights;
myRights.count = sizeof (myItems) / sizeof (myItems[0]);
myRights.items = myItems;

AuthorizationFlags myFlags;
myFlags = kAuthorizationFlagDefaults |
kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagExtendRights;

myStatus = AuthorizationCopyRights (myAuthorizationRef, &myRights,
                                    kAuthorizationEmptyEnvironment, myFlags, NULL);

if (myStatus==errAuthorizationSuccess)
{

  //Run my Code


}

myStatus = AuthorizationFree (myAuthorizationRef, kAuthorizationFlagDestroyRights);
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Mike Nathas
  • 1,247
  • 2
  • 11
  • 29
  • How do you know you don't have the proper rights? – Cristik Dec 09 '15 at 21:46
  • Have you seen [this blog article about this](http://www.michaelvobrien.com/blog/2009/07/authorizationexecutewithprivileges-a-simple-example/)? – Volomike Feb 21 '16 at 05:20
  • Also, I think Apple has a better, recommended way to make a Cocoa app do privileged tasks. This [other StackOverflow post](http://stackoverflow.com/a/35532550/105539) talks about something similar. – Volomike Feb 21 '16 at 05:22

0 Answers0