objective-c - OSX 以 root 身份以编程方式启动 launchctl

标签 objective-c macos cocoa shell

我尝试以 root 身份从 OSX 应用程序使用 launchctl 启动 samba 服务,但我收到错误状态 -60031。我可以毫无问题地运行终端中的命令:

sudo launchctl load -F /System/Library/LaunchDaemons/com.apple.smbd.plist

在 objective-c 代码中,我正在使用(我知道它已被弃用,但这真的不应该成为这里的问题)AuthorizationExecuteWithPrivileges 方法。

代码如下:

    NSString *command = @"launchctl";

    // Conversion of NSArray args to char** args here (not relevant part of the code)

    OSStatus authStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &_authRef);
    if (authStatus != errAuthorizationSuccess) {
        NSLog(@"Failed to create application authorization: %d", (int)authStatus);
        return;
    }

    FILE* pipe = NULL;
    AuthorizationFlags flags = kAuthorizationFlagDefaults;
    AuthorizationItem right = {kAuthorizationRightExecute, 0, NULL, 0};
    AuthorizationRights rights = {1, &right};

    // Call AuthorizationCopyRights to determine or extend the allowable rights.
    OSStatus stat = AuthorizationCopyRights(_authRef, &rights, NULL, flags, NULL);
    if (stat != errAuthorizationSuccess) {
        NSLog(@"Copy Rights Unsuccessful: %d", (int)stat);
        return;
    }

    OSStatus status = AuthorizationExecuteWithPrivileges(_authRef,
                                                         command.UTF8String,
                                                         flags,
                                                         args,
                                                         &pipe);
    if (status != errAuthorizationSuccess) {
        NSLog(@"Error executing command %@ with status %d", command, status);
    } else {
        // some other stuff
    }

我也曾尝试使用不同的标志然后使用 kAuthorizationFlagDefaults,但这导致了相同的问题或错误代码 -60011 -> 无效标志。

请问我做错了什么?

最佳答案

我建议使用 STPrivilegedTask - https://github.com/sveinbjornt/STPrivilegedTask

我有类似的问题,我发现上面写得很好的包装器。它简单明了。 如果需要,您可以根据需要修改它,否则就按原样使用它!!!

它对我有用,我希望它对你也有帮助。

谢谢。

更新(2014 年 8 月 28 日): with root权限执行命令和作为 root执行命令是有区别的!

在您的特定情况下,您正在尝试加载/卸载守护进程(它必须属于 root)。在那种情况下,您必须执行命令作为 root。如果您尝试仅以 root 权限 加载/卸载,那么您将在您的用户下运行守护进程! - 不好!

现在,您的代码示例和我对 STPrivilegedTask 的引用都使用相同的代码,并允许用户以 root 权限但不是 root 执行任务!为了以 root 用户身份执行,您有多种选择。首先,您可能想查看 Apple Docs 以了解推荐的方式。就我而言,我无法采用推荐的方式,因为我的应用程序未签名且不会签名 + 它需要在旧 OSX 上运行。

所以我的解决方案很简单。制作一个命令实用程序帮助工具,让它假定为 root 并执行您通过参数传递给它的任何内容。现在请注意(这不是很安全的做事方式)。另请注意,您将使用 root 权限调用 helper 工具,它将采用 root 身份。

代码:

 int main(int argc, const char * argv[])
 {

@autoreleasepool {

    if (argc >= 2)
    {
        setuid(0);  // Here is a key - set user id to 0 - meaning become a root and everything below executes as root.

        NSMutableArray *arguments = [[NSMutableArray alloc] init];
        NSString *command = [[NSString alloc] initWithFormat:@"%s", argv[1]];

        for (int idx = 2; idx < argc; idx++) {
            NSString *tmp = [[NSString alloc] initWithFormat:@"%s", argv[idx]];
            [arguments addObject:tmp];
        }

        NSTask *task = [[NSTask alloc] init];
        [task setLaunchPath:command];
        [task setArguments:arguments];

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

        [task waitUntilExit];

        NSFileHandle * read = [out fileHandleForReading];
        NSData * dataRead = [read readDataToEndOfFile];
        NSString * stringRead = [[NSString alloc] initWithData:dataRead encoding:NSUTF8StringEncoding];

        printf("%s", [stringRead UTF8String]);

        }
     return 0;
     }
 }

关于objective-c - OSX 以 root 身份以编程方式启动 launchctl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25379889/

相关文章:

objective-c - 我如何为 Mac OS X 制作可拖动的菜单栏图标

objective-c - 全屏播放视频时(Xcode 6)UIWebView打破约束

iphone - 如何在工作日的选择列表中设置重复的 UILocal 通知

javascript - encodeURIComponent 等效对象-c

cocoa - Mac App Store 收据验证码?

iphone - 如何检查 WebView URL 是否是本地文件?

iphone - 使用 CCMenuAdvanced - Cocos2d

python - 如何为 python 3 创建一个空列表?

ios - iPhone 应用程序设置只读值

objective-c - Cocoa 子类化的怪异