开发者

How to give permission using NSTask - objective-c [duplicate]

开发者 https://www.devze.com 2023-01-12 09:30 出处:网络
This question already has answers here: How to use NSTask as root? (5 answers) 开发者_如何学Go Closed 6 years ago.
This question already has answers here: How to use NSTask as root? (5 answers) 开发者_如何学Go Closed 6 years ago.

I need to basically do a "sudo" but, I need to give that kind of permission to my NSTask code. Is this possible?

Thanks, Elijah


If you're looking for a more lightweight solution, I wrote this generic implementation which should achieve what you want:

- (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;
    }
}

Usage example:

    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
    }

It's very slightly hacky, but IMHO is a satisfactory solution. Credit for discovery to this SO question.


Look here:

  • http://www.sveinbjorn.org/STPrivilegedTask
  • http://www.sheepsystems.com/sourceCode/authTasksCocoa.html

HTH :-)


Some related information is given in the article "Communicating with a Privileged Tool"

See also: http://osx.hyperjeff.net/Reference/CocoaArticles?cat=29

0

精彩评论

暂无评论...
验证码 换一张
取 消