0
0

More than 3 years have passed since last update.

【macOS App】macOSアプリで特権モード(launchd)動作するヘルパーツールをビルドする

Last updated at Posted at 2021-07-21

いつも細かい手順を忘れがちなので、macOSアプリで特権モードを利用する際の自分用メモ

プロジェクト作成

プロジェクトを作成し、アプリ本体と特権モードを動かすヘルパーツールを登録。
アプリ本体は通常のAPP、ヘルパーツールはCommandLineToolなど。
AppStoreは使わないので署名は「Developer ID Application」で行う。

追加したヘルパーツールに必要となるplistを追加。

apphelper.plist(ヘルパーツール本体)
<?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>CFBundleIdentifier</key>
    <string>com.sample.apphelper</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>apphelper</string>
    <key>CFBundleVersion</key>
    <string>1.0</string>
</dict>
</plist>

ヘルパーツールとのインタラクティブな制御は古典的プロセス間通信を利用。
そろそろXPC Service化してもいいのかもしれない。

apphelper-Launchd.plist(launchd用)
<?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>ServiceIPC</key>
    <false/>
    <key>OnDemand</key>
    <false/>
    <key>Label</key>
    <string>com.sample.apphelper</string>
    <key>Disabled</key>
    <true/>
</dict>
</plist>

注意点

  • TARGETS->apphelper->TARGETS->apphelper->General->Identity->Identityからapphelper.plistファイルを登録しておく
    そもそもデフォルトで用意してくれても良さそうだが、単に追加しただけでは存在しないらしい。

  • Build SettingsのProduct build IdentifierProductNameを一致させておく
    これを見落としていて1日悩んだ...

ターゲット登録

apphelerをアプリ本体のTarget Membershipにチェック。
アプリ本体のBuild Phasesでヘルパーツールを関連づけ

  • Build Phases->Dependenciesにヘルパーツールを追加
  • Build Phases->Copy Filesに以下の設定を追加
    • Copy Filesがない場合はPane左上の+ボタンからNew Copy Files Phaseしておく
Destination:Wrapper
Subpath:Contents/Library/LaunchServices
追加ボタン(+)を押してヘルパーツールを追加

ヘルパーツール側の設定
- Build Settings->Other Linker Flagsに以下の4行を追加

-sectcreate
__TEXT
__launchd_plist
$(SRCROOT)/${TARGET_NAME}/apphelper-Launchd.plist

ビルド(一回目)

この状態で一度プログラム全体をビルド。(これが重要)

署名交換

アプリ本体とヘルパーツールを連携させるために、互いの署名を設定し合う。
SMJobBlessUtil.pyというツールがサンプルにあるのでこちらを利用する。

使い方は簡単

SMJobBlessUtil.py setreq <アプリファイル名> <アプリのplistファイル名> < ヘルパーツールのplistファイル名>

アプリ本体、ヘルパーツールにそれぞれTools owned after installationSMAuthorizedClientsが追加されたら成功。

Info.plist
anchor apple generic and identifier "<ヘルパーツールのbundleid>" and (certificate leaf[field.1.2.840.113635.100.6.1.9] /* exists */ or certificate 1[field.1.2.840.113635.100.6.2.6] /* exists */ and certificate leaf[field.1.2.840.113635.100.6.1.13] /* exists */ and certificate leaf[subject.OU] = "<AppID>")
apphelper.plist
anchor apple generic and identifier "<APPのbundleid>" and (certificate leaf[field.1.2.840.113635.100.6.1.9] /* exists */ or certificate 1[field.1.2.840.113635.100.6.2.6] /* exists */ and certificate leaf[field.1.2.840.113635.100.6.1.13] /* exists */ and certificate leaf[subject.OU] = "<AppID>")/* exists */

この状態で再度ビルドを行う。
コマンドやビルドが失敗した場合は以下を確認

  • Bundle identifierBundle Nameがあっているかどうか
  • 署名は適切か

さらに追加で、Sandboxが有効になっているとエラーになるので、entitlementsファイルでApp Sandbox:NOとしておくこと。

お疲れさまでした。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0