【问题标题】:Error code 9216 when attempting to access keychain password in LaunchAgent尝试在 LaunchAgent 中访问钥匙串密码时出现错误代码 9216
【发布时间】:2018-08-23 16:47:27
【问题描述】:

还有其他几个问题讨论了从 LaunchAgents 访问钥匙串。

其中一个关键是here,其中joensson 提到您需要在应用plist 中设置<SessionCreate>

我已经这样做了,现在我的应用程序列表看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>com.ionic.python.ionic-fs-watcher.startup</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Applications/IonicFSWatcher.app/Contents/MacOS/ionic-fs-watcher</string>
        <string>--debug</string>
        <string>/Users/timothy/ionicprotected</string>
        <string>--scan</string>
    </array>
    <key>UserName</key>
    <string>timothy</string>
    <key>SessionCreate</key>
    <true />
  </dict>
</plist>

该应用是一个python应用,使用pyinstaller创建,使用pkgbuild打包,安装via the command line

从命令行运行时应用程序运行良好。如果应用程序第一次运行,用户会收到允许访问钥匙串的提示,应用程序会从那里继续。

但是,当它以LaunchAgent 启动时,我在尝试访问钥匙串时得到返回码 9216。这是我用来测试的确切命令序列:

# Window 1
sudo launchctl unload -w ~/Library/LaunchAgents/com.ionic.python.ionic-fs-watcher.startup.plist
sudo launchctl load -w ~/Library/LaunchAgents/com.ionic.python.ionic-fs-watcher.startup.plist
sudo launchctl debug system/com.ionic.python.ionic-fs-watcher.startup --stdout --stderr

# Window 2
sudo launchctl kickstart -k -p system/com.ionic.python.ionic-fs-watcher.startup

在 python 脚本中,我一直在通过运行一些子命令并捕获输出进行调试。

# OK
status, output = commands.getstatusoutput("security list-keychains")
logger.error("Keychain list :%s (retcode = %s)" % (
    output, status
))
# OK
status, output = commands.getstatusoutput("security find-generic-password -a 'Ionic Security' ")
logger.error("Keychain list item :%s (retcode = %s)" % (
    output, status
))
# Fails, with error code: 9216
# Prompts immediately when running from command line
status, output = commands.getstatusoutput("security find-generic-password -a 'Ionic Security' -g")
logger.error("Keychain access :%s (retcode = %s)" % (
    output, status
))

该部分代码的输出如下所示:

# Correctly shows both keychains
ERROR:root:Keychain list :    "/Users/timothy/Library/Keychains/login.keychain"
    "/Library/Keychains/System.keychain" (retcode = 0)
# Correctly lists information about keychain item
ERROR:root:Keychain list item :keychain: "/Users/timothy/Library/Keychains/login.keychain"
<redacted>
# Fail
ERROR:root:Keychain access : (retcode = 9216)

这些相同的命令在命令行中可以正常工作,最后一个 (-g) 会提示允许访问钥匙串。

我还尝试在KeyChain Access 应用程序中打开com.ionicsecurity.client.sdk 的条目,并设置“允许所有应用程序访问此项目”单选按钮。之后,从 cli 中获取值不再导致提示 应用返回相同的错误代码。

我搜索了有关错误代码 9216 的信息,但没有结果。通过security errors 实用程序运行代码只会给出

$ security error 9216
Error: 0x00002400 9216 unknown error 9216=2400

任何有关如何在作为 LaunchAgent 运行时获得对钥匙串的应用程序访问权限的帮助将不胜感激!

【问题讨论】:

  • 可能相关:这是一个用户LaunchAgent,但是plist文件有root:wheel权限。我想弄清楚如何在这里解决这个问题:stackoverflow.com/questions/49290174/…
  • 这是 100% 相关的。当我试图弄清楚如何在没有sudo 的情况下启动代理时,这让我走上了正确的道路。

标签: macos keychain launchd launchdagent ionicsecurity


【解决方案1】:

问题是我用来启动 LaunchAgent 的域。我正在启动到根系统域,而不是启动到我正在为其设置 LaunchAgent 的用户的 gui 域。因为这个

  • LaunchAgent 无权访问用户钥匙串(除非我请求以用户身份运行命令并创建会话)
  • LaunchAgent 无法向用户显示请求访问钥匙串的弹出窗口(我假设这就是我在此处收到奇怪错误代码的原因)

这是从我之前使用的命令(不工作)到我现在使用的命令(工作)的映射。这些命令假定我们为其安装 LaunchAgent 的用户也是当前用户。

激活代理

# Before
sudo launchctl load -w ~/Library/LaunchAgents/com.ionic.python.ionic-fs-watcher.startup.plist

# Now
launchctl enable user/`id -u`/com.ionic.python.ionic-fs-watcher.startup
launchctl bootstrap gui/`id -u` ~/Library/LaunchAgents/com.ionic.python.ionic-fs-watcher.startup.plist

停用代理

# Before
sudo launchctl unload -w ~/Library/LaunchAgents/com.ionic.python.ionic-fs-watcher.startup.plist

# Now
launchctl bootout gui/`id -u`/com.ionic.python.ionic-fs-watcher.startup
launchctl disable user/`id -u`/com.ionic.python.ionic-fs-watcher.startup

为 LaunchAgent 定义(plist 文件)设置权限

# Before
chown root:wheel ~/Library/LaunchAgents/com.ionic.python.ionic-fs-watcher.startup.plist
chmod 644 ~/Library/LaunchAgents/com.ionic.python.ionic-fs-watcher.startup.plist

# Now
chown "`id -un`":"`id -gn`" ~/Library/LaunchAgents/com.ionic.python.ionic-fs-watcher.startup.plist
chmod 644 ~/Library/LaunchAgents/com.ionic.python.ionic-fs-watcher.startup.plist

LaunchAgent plist 文件内容

不需要UserNameSessionCreate 块。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>com.ionic.python.ionic-fs-watcher.startup</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Applications/IonicFSWatcher.app/Contents/MacOS/ionic-fs-watcher</string>
        <string>/Users/timothy/ionicprotected</string>
        <string>--scan</string>
    </array>
  </dict>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    • 2020-03-16
    • 2015-03-07
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多