目标C - 在cocoa app中获取root权限以修改系统文件

时间:2018-05-17 10:30:27

标签: objective-c cocoa authorization macos-high-sierra

我将在High Sierra版本10.13.4中的系统目录中创建/更新文件。

对于它,应该获得mac os的root权限。 我尝试使用此代码启动特权进程。

OSStatus LaunchPrivilegedProcess(NSString *path) {

    OSStatus status;
    AuthorizationRef authorizationRef;

    status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
                                 kAuthorizationFlagDefaults, &authorizationRef);

    if (status != errAuthorizationSuccess)
    {
        NSLog(@"Error Creating Initial Authorization: %d", status);
        return status;
    }


    AuthorizationItem right = {kAuthorizationRightExecute, 0, NULL, 0};
    AuthorizationRights rights = {1, &right};
    AuthorizationFlags flags = kAuthorizationFlagDefaults |
    kAuthorizationFlagInteractionAllowed |
    kAuthorizationFlagPreAuthorize |
    kAuthorizationFlagExtendRights;

    status = AuthorizationCopyRights(authorizationRef, &rights, NULL, flags, NULL);
    if (status != errAuthorizationSuccess)
    {
        NSLog(@"Copy Rights Unsuccessful: %d", status);
        return status;
    }


    char *tool = path.UTF8String;
    char *args[] = {NULL};
    FILE *pipe = NULL;

    status = AuthorizationExecuteWithPrivileges(authorizationRef, tool,
                                                kAuthorizationFlagDefaults, args, &pipe);
    if (status != errAuthorizationSuccess)
    {
        NSLog(@"Error: %d", status);
        return status;
    }

    status = AuthorizationFree(authorizationRef, kAuthorizationFlagDestroyRights);

    return status;
}

但它在AuthorizationExecuteWithPrivileges失败,错误代码为:-60031(errAuthorizationToolExecuteFailure)。

我该如何解决这个问题?

是否有其他方法可以获得更新系统文件的root权限?

0 个答案:

没有答案