NSTask和AuthorizationCreate [英] NSTask and AuthorizationCreate

查看:742
本文介绍了NSTask和AuthorizationCreate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my Cocoa application I need to run another application with root permissions, but it doens't work! NSTask doesn't have needed rights. AuthorizationCreate section is where i get needed permissions. Running NSTask is the section where I running another application.

myStatus返回errAuthorizationSuccess。

myStatus returns 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项目。我做错了什么? Thx帮助。

I can't use STPrivilege because its an ARC project. What i'm doing wrong? Thx for help.

推荐答案

我不知道我做错了什么,但解决它由VERY STRANGE解决方案: (

I don't know what I was doing wrong, but solved it by VERY STRANGE solution:(((

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

我不喜欢太多。

这篇关于NSTask和AuthorizationCreate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆