【问题标题】:NSTask and AuthorizationCreateNSTask 和 AuthorizationCreate
【发布时间】:2012-12-06 13:16:00
【问题描述】:

在我的 Cocoa 应用程序中,我需要以 root 权限运行另一个应用程序,但它不起作用! NSTask 没有所需的权限。 AuthorizationCreate 部分是我获得所需权限的地方。 Running 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 项目。我做错了什么?谢谢帮忙。

【问题讨论】:

  • Nope:( AuthorizationExecuteWithPrivileges() 已弃用,我写的 STPrivilegedTask 我不能使用:(
  • 你的应用是沙盒的吗?如果是,授权服务将不起作用。
  • 不,它不是沙盒。 Enable Entitlements 复选框已关闭,App Sandbox 也是如此。

标签: objective-c macos cocoa


【解决方案1】:

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

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

我不太喜欢它。

【讨论】:

  • 我遇到了同样的问题,但使用升级权限在 shell 上执行的命令不同。那么您是否设法通过使用 NSAppleScript 而不是 NSTask 使其工作?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-07
  • 2011-08-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多