AuthorizationExecuteWithPrivileges
已经被弃用。有人能建议一种方法让我的应用程序写入一个没有权限的目录吗?
AuthorizationExecuteWithPrivileges
已经被弃用。我知道这听起来很疯狂,但是这个确实有效:
NSDictionary *error = [NSDictionary new];
NSString *script = @"do shell script \"whoami > /tmp/me\" with administrator privileges";
NSAppleScript *appleScript = [[NSAppleScript alloc] initWithSource:script];
if ([appleScript executeAndReturnError:&error]) {
NSLog(@"success!");
} else {
NSLog(@"failure!");
}
我从Objective C执行一个Applescript。唯一的劣势是你不能用它获得永久的root权限。每次运行此操作时,它都会要求输入密码。
实际上,AuthorizationExecuteWithPrivileges()
已经被弃用很长时间了,只是最近头文件才跟上了这个事实。
您可以创建一个特权辅助工具作为应用程序的一部分。您可以使用 ServiceManagement.framework
的 SMJobBless()
函数将助手部署到系统的 launchd
上下文中:然后当您需要执行特权任务时,只需向特权助手发送消息即可完成该工作。
有一点隐藏的复杂性,即应用程序和助手必须在 SMJobBless()
之前声明彼此的签名身份,而且您需要让链接器将助手工具的 Info.plist
文件写入二进制文件中。所有这些都由 Apple's Documentation 所涵盖,而 Apple 也提供了 a sample project。
我编写了一个示例应用程序,使用SMJobBless()
来部署其特权助手。
AuthorizationExecuteWithPrivileges()
,它是一个可怕的安全漏洞。实现你想要做的事情的正确方式是使用SMJobBless()
。 - user23743AuthorizationExecuteWithPrivileges
呢?参考文档表示“您应该仅将此函数用于允许安装程序作为根运行,并允许 setuid 工具在其 setuid 位丢失时修复其 setuid 位”,并且《Authorization Services Programming Guide》建议使用此函数启动安装程序(尽管它有些过时)。那么在像命令行工具这样的简单应用程序中启动安装程序是否有任何替代方法? - cody根据用户950473的发现,我已经将其发现实现为一个方法。我觉得分享这段代码可能会有帮助。
- (BOOL) runProcessAsAdministrator:(NSString*)scriptPath
withArguments:(NSArray *)arguments
output:(NSString **)output
errorDescription:(NSString **)errorDescription {
NSString * allArgs = [arguments componentsJoinedByString:@" "];
NSString * fullScript = [NSString stringWithFormat:@"'%@' %@", scriptPath, allArgs];
NSDictionary *errorInfo = [NSDictionary new];
NSString *script = [NSString stringWithFormat:@"do shell script \"%@\" with administrator privileges", fullScript];
NSAppleScript *appleScript = [[NSAppleScript new] initWithSource:script];
NSAppleEventDescriptor * eventResult = [appleScript executeAndReturnError:&errorInfo];
// Check errorInfo
if (! eventResult)
{
// Describe common errors
*errorDescription = nil;
if ([errorInfo valueForKey:NSAppleScriptErrorNumber])
{
NSNumber * errorNumber = (NSNumber *)[errorInfo valueForKey:NSAppleScriptErrorNumber];
if ([errorNumber intValue] == -128)
*errorDescription = @"The administrator password is required to do this.";
}
// Set error message from provided message
if (*errorDescription == nil)
{
if ([errorInfo valueForKey:NSAppleScriptErrorMessage])
*errorDescription = (NSString *)[errorInfo valueForKey:NSAppleScriptErrorMessage];
}
return NO;
}
else
{
// Set output to the AppleScript's output
*output = [eventResult stringValue];
return YES;
}
}
使用示例:
NSString * output = nil;
NSString * processErrorDescription = nil;
BOOL success = [self runProcessAsAdministrator:@"/usr/bin/id"
withArguments:[NSArray arrayWithObjects:@"-un", nil]
output:&output
errorDescription:&processErrorDescription];
if (!success) // Process failed to run
{
// ...look at errorDescription
}
else
{
// ...process output
}
这个方法有些取巧,但在我看来是一个令人满意的解决方案。
AuthorizationExecuteWithPrivileges已经被弃用了。
但是,幸运的是,现在有一个新的推荐方法可供使用。
从10.6开始,有了新API,并且建议安装一个帮助程序来执行特权操作。苹果提供了一个代码示例,清楚地演示了如何管理它。
请确保查看他们的readme.txt,因为与其他代码示例相比,还需要做更多的工作,而不仅仅是下载项目并运行它。
来自SMJobBless示例介绍
SMJobBless演示了如何安全地安装执行特权操作的帮助工具,以及如何将该工具与调用它的应用程序相关联。
从Snow Leopard开始,在Mac OS X上管理权限升级的首选方法是使用此方法,而不是以前的方法,如BetterAuthorizationSample或直接调用AuthorizationExecuteWithPrivileges。
SMJobBless使用在Mac OS X v10.6 Snow Leopard中引入的ServiceManagement.framework。