Elevating apps rights on a mac

590 views Asked by At

I am writing an app that requires writing to system protected folders like the /Applications. How to do that properly? Tried this code, but that doesn't seem to work on an account without Admin rights (prompt for the password appears).

AuthorizationRef authorizationRef = NULL;

AuthorizationItem authItem      = { "", 0, NULL, 0 };
AuthorizationRights authRights  = { 1, &authItem };
AuthorizationFlags flags        =   kAuthorizationFlagDefaults              |
                                    kAuthorizationFlagInteractionAllowed    |
                                    kAuthorizationFlagPreAuthorize          |
                                    kAuthorizationFlagExtendRights;

OSStatus status = AuthorizationCreate(&authRights, kAuthorizationEmptyEnvironment, flags, &authorizationRef);
status = AuthorizationCopyRights(authorizationRef, &authRights, kAuthorizationEmptyEnvironment, flags, NULL);

if (status != errAuthorizationSuccess)
    Logger::logErrorF("Unable to elevate permissions");

Poco::File file("/Applications/Test");
file.createDirectory();

AuthorizationFree(authorizationRef, flags);

What am I doing wrong? I don't want to create any helper tools for that, just want to elevate rights when necessary, do what I want and lower rights afterwards.

Also what's the best way to determine if the currently running process has admin rights?

0

There are 0 answers