使用Objective C中的提升权限运行AppleScript

时间:2010-04-19 22:00:13

标签: objective-c macos authorization applescript osx-leopard

我正在尝试通过AuthorizationExecuteWithPrivileges执行卸载程序(用AppleScript编写)。我在创建一个空的auth ref之后设置了我的权利:

    char *tool = "/usr/bin/osascript";
    AuthorizationItem items = {kAuthorizationRightExecute, strlen(tool), tool, 0};
    AuthorizationRights rights = {sizeof(items)/sizeof(AuthorizationItem), &items};
    AuthorizationFlags flags = kAuthorizationFlagDefaults |
                               kAuthorizationFlagExtendRights |
                               kAuthorizationFlagPreAuthorize |
                               kAuthorizationFlagInteractionAllowed;
    status = AuthorizationCopyRights(authorizationRef, &rights, NULL, flags, NULL);

后来我打来电话:

    status = AuthorizationExecuteWithPrivileges(authorizationRef, tool, kAuthorizationFlagDefaults, (char *const *)args, NULL);

在Snow Leopard上这很好用,但在Leopard上我在syslog.log中得到以下内容:

Apr 19 15:30:09 hostname /usr/bin/osascript[39226]: OpenScripting.framework - 'gdut' event blocked in process with mixed credentials (issetugid=0 uid=501 euid=0 gid=20 egid=20)
Apr 19 15:30:12: --- last message repeated 1 time ---
...
Apr 19 15:30:12 hostname [0x0-0x2e92e9].com.example.uninstaller[39219]: /var/folders/vm/vmkIi0nYG8mHMrllaXaTgk+++TI/-Tmp-/TestApp_tmpfiles/Uninstall.scpt: 
Apr 19 15:30:12 hostname [0x0-0x2e92e9].com.example.uninstaller[39219]: execution error: «constant afdmasup» doesn’t understand the «event earsffdr» message. (-1708)

经过几个小时的研究后,我的第一个猜测是Leopard不知何故不想做我正在做的事情,因为它知道它处于一个固定状态并阻止询问有关AppleScript中用户特定事物的调用。

我是否认为这一切都错了?我只想运行相当于“sudo / usr / bin / osascript ...”

编辑:

FWIW,导致“执行错误”的第一行是:

set userAppSupportPath to (POSIX path of (path to application support folder from user domain))

然而,即使有一个空脚本(在运行argv,结束运行,那就是它)我仍然得到'gdut'消息。

1 个答案:

答案 0 :(得分:2)

根据这个帖子。 http://forums.macosxhints.com/showthread.php?t=90952&page=3似乎对OS X进行了安全更新,阻止通过AppleScript访问setuid根脚本。

我怀疑这种机制也阻止了你的代码。

不幸的是,我猜这意味着这不是“按设计”工作。

相关问题