gpt4 book ai didi

MACOSX : how to start daemon which need root privileges from cocoa application

转载 作者:行者123 更新时间:2023-12-03 16:20:21 25 4
gpt4 key购买 nike

条件如下:MacOSX 10.7 和用 Objective-c 编写的 Cocoa 图形应用程序。

我遇到了以下问题。我无法从 Cocoa 应用程序的/user/bin 文件夹启动守护程序应用程序。我知道我应该拥有root权限才能做到这一点。

我有安装程序,不是 *.pkg,而是手动编写的安装程序应用程序。安装过程完成后我需要启动我的守护进程。如何提高 Objective-C 应用程序的权限级别?

我有这个代码。但 AuthorizationExecuteWithPrivileges 调用已被弃用。

我们不应该使用它。

AuthorizationItem authItem      = { kSMRightModifySystemDaemons, 0, NULL, 0 };
AuthorizationRights authRights = { 1, &authItem };
AuthorizationFlags flags = kAuthorizationFlagDefaults |
kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagPreAuthorize |
kAuthorizationFlagExtendRights;

AuthorizationRef authRef = NULL;

OSStatus status = AuthorizationCreate(&authRights,
kAuthorizationEmptyEnvironment, flags, &authRef);

status = AuthorizationExecuteWithPrivileges(authorizationRef, tool, kAuthorizationFlagDefaults, args, &pipe);

第二种方法是使用特权助手。对于我的情况来说,这种方式看起来很复杂。我只需要在安装后启动守护进程。

当然,我可以要求用户重新启动 mac。但我想避免它。

另一种可能性是守护进程可执行文件上的 SUID 位。但我认为这并不安全。

我的问题是:还有其他更简单的方法吗?

最佳答案

好吧,这里可能有答案。

您需要:

  1. 使用您的操作创建 shell 脚本。
  2. 创建一个 Apple 脚本 NSString,在其中请求以管理员权限执行 shell 脚本。
  3. 创建 NSAppleScript 实例并执行 Apple 脚本。

以下是更多详细信息:

AuthorizationExecuteWithPrivileges is deprecated

关于MACOSX : how to start daemon which need root privileges from cocoa application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14745334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com