gpt4 book ai didi

objective-c - NSTask 和 AuthorizationCreate

转载 作者:行者123 更新时间:2023-12-03 17:23:37 25 4
gpt4 key购买 nike

在我的 Cocoa 应用程序中,我需要运行另一个具有 root 权限的应用程序,但它不起作用! NSTask 没有所需的权限。AuthorizationCreate 部分是我获得所需权限的地方。运行 NSTask 是我运行另一个应用程序的部分。

myStatus 返回 errAuthorizationSuccess。

这是代码

//Authorization Create
AuthorizationRef myAuthorizationRef;
OSStatus myStatus;
myStatus = AuthorizationCreate (NULL, kAuthorizationEmptyEnvironment,
kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed , &myAuthorizationRef);
AuthorizationItem myItems[1];

myItems[0].name = "com.myOrganization.myProduct.myRight1";
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)
{

//Running NSTask
//setting license
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/opt/cprocsp/sbin/cpconfig"];

NSArray *arguments;
arguments = [NSArray arrayWithObjects:@"-license", @"-set",
@"lalala", nil];
[task setArguments: arguments];

NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];

NSFileHandle *file;
file = [pipe fileHandleForReading];

[task launch];

NSData *data;
data = [file readDataToEndOfFile];

NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog (@"grep returned:\n%@", string);
[_logs insertText:string];
}
myStatus = AuthorizationFree (myAuthorizationRef,
kAuthorizationFlagDestroyRights);

我无法使用 STPrivilege,因为它是 ARC 项目。我做错了什么?谢谢帮助。

最佳答案

我不知道我做错了什么,但通过非常奇怪的解决方案解决了它:(((

NSDictionary *error = [NSDictionary dictionary];
NSAppleScript *run = [[NSAppleScript alloc]initWithSource:@"do shell script \"/opt/cprocsp/sbin/cpconfig -license -set lala\" with administrator privileges"];
[run executeAndReturnError:&error];

我不太喜欢它。

关于objective-c - NSTask 和 AuthorizationCreate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13744413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com