1

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 returns errAuthorizationSuccess.

Here is the code

//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);

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

Dimson
  • 99
  • 11
  • Does the answers to this question help? http://stackoverflow.com/questions/4050687/how-to-use-nstask-as-root – trojanfoe Dec 06 '12 at 13:22
  • Nope:( AuthorizationExecuteWithPrivileges() is deprecated, and STPrivilegedTask I can't use as I wrote:( – Dimson Dec 06 '12 at 21:03
  • Is your app sandboxed? Authorisation services don't work if it is. – sjs Dec 07 '12 at 01:30
  • No, it is not sandboxed. Enable Entitlements checkbox is turned off, so as App Sandbox is. – Dimson Dec 07 '12 at 08:43

1 Answers1

0

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

I didn't like it sooo much.

Dimson
  • 99
  • 11
  • I had the same problem with you, but with different command to execute on shell using escalated privileges. So did you manage to make it work by using NSAppleScript instead of NSTask? – Aryo Jul 03 '13 at 10:44