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);