ZigでGTK4 SSH-askpassを実装
A GTK4 SSH-askpass in Zig (xn--gckvb8fzb.com)
要約
Gentoo Linuxユーザーが、依存関係が重い既存のSSHパスフレーズ入力ツールに不満を持ち、Zig言語とGTK4を用いて軽量な代替ツールを自作した。このツールはX11に依存せず、Goのプライベートモジュール取得時など、ターミナルがない環境でのSSH認証に役立つ。
全文翻訳
ZigでGTK4 SSH-askpassを作成
Gentooのツリーにあるssh-askpassは、すべてX11または完全なKDEスタックを引き込みます。そのため、私はZig 0.16とGTK4で独自のものを書きました。手書きのバインディングにより、ビルドにXが入らないようにしています。
私はラップトップでhardened Gentooを実行していますが、ほとんどの場合-skキーを使用しているためssh-askpassに触れることはありません。ただし、通常のED25519キーのSSHキーパスフレーズをプログラムが要求するものの、読み取るためのターミナルがないという状況が一つあります。典型的なケースはgo get、またはgoツールチェーン全般が、TTYなしで実行されるビルド中にSSH経由でプライベートモジュールを取得する場合です。OpenSSHはパイプでプロンプトを表示できないため、SSH_ASKPASSが指すものを実行し、パスフレーズプロンプトをウィンドウに表示します。長年、私はそのためのものを何もインストールしておらず、これらのシナリオを回避しなければなりませんでした。その主な理由は、GentooのPortageが提供するものにあります。
~ emerge -s ssh-askpass
[ Results for search key : ssh-askpass ]
Searching...
* kde-plasma/ksshaskpass
Latest version available: 6.6.6
Latest version installed: [ Not Installed ]
Size of files: 32 KiB
Homepage: https://kde.org/plasma-desktop https://invent.kde.org/plasma/ksshaskpass
Description: Implementation of ssh-askpass with KDE Wallet integration
License: GPL-2
* lxqt-base/lxqt-openssh-askpass
Latest version available: 2.4.0
Latest version installed: [ Not Installed ]
Size of files: 19 KiB
Homepage: https://lxqt-project.org/ https://github.com/lxqt/lxqt-openssh-askpass/
Description: LXQt OpenSSH user password prompt tool
License: LGPL-2.1 LGPL-2.1+
* net-misc/gnome-ssh-askpass
Latest version available: 10.0_p1
Latest version installed: [ Not Installed ]
Size of files: 1,927 KiB
Homepage: https://www.openssh.com/
Description: GTK-based passphrase dialog for use with OpenSSH
License: BSD GPL-2
* net-misc/ssh-askpass-fullscreen
Latest version available: 1.2
Latest version installed: [ Not Installed ]
Size of files: 81 KiB
Homepage: https://github.com/atj/ssh-askpass-fullscreen
Description: A small SSH Askpass replacement written with GTK2
License: GPL-2+
* net-misc/x11-ssh-askpass
Latest version available: 1.2.4.1-r2
Latest version installed: [ Not Installed ]
Size of files: 30 KiB
Homepage: https://github.com/sigmavirus24/x11-ssh-askpass
Description: X11-based passphrase dialog for use with OpenSSH
License: HPND
[ Applications found : 5 ]
これらのそれぞれには、私が我慢したくない不便が少なくとも一つありました。私のシステムはグローバルな-X USEフラグで動作するため、X11を必要とするものは、さらに調べる前に除外されます。5つのうち、kde-plasma/ksshaskpassだけがX11依存関係を一切持っていませんが、それが明白な選択肢となるはずでした。しかし、問題はそれに付随してくる他のすべてです。私はSwayユーザーなので、たまにパスフレーズを入力するためだけにマシンに完全なKDEスタックを導入したくありませんでした。それがksshaskpassのインストールが引き込むものです。
~ # emerge -a kde-plasma/ksshaskpass
These are the packages that would be merged, in order:
Calculating dependencies... done!
Dependency resolution took 1.18 s (backtrack: 0/20).
[ebuild N ] kde-frameworks/kf-env-6
[ebuild N ] dev-libs/plasma-wayland-protocols-1.21.0 USE="-test"
[ebuild N ] dev-libs/qtkeychain-0.16.0 USE="-keyring -test"
[ebuild N ] kde-frameworks/ksecretd-services-6.27.0 USE="(-systemd)"
[ebuild N ] kde-frameworks/breeze-icons-6.27.0 USE="-test"
[ebuild N ] kde-frameworks/ki18n-6.27.0 USE="-debug -test" PYTHON_SINGLE_TARGET="python3_14 -python3_12 -python3_13"
[ebuild N ] kde-frameworks/kconfig-6.27.0 USE="dbus -debug -qml -test"
[ebuild N ] kde-frameworks/kcoreaddons-6.27.0 USE="dbus -debug -test"
[ebuild N ] kde-frameworks/kwidgetsaddons-6.27.0 USE="-debug -designer -test"
[ebuild N ] kde-frameworks/kdbusaddons-6.27.0 USE="-X -debug -test"
[ebuild N ] kde-frameworks/kwindowsystem-6.27.0 USE="wayland -X -debug -test"
[ebuild N ] app-crypt/qca-2.3.10-r1 USE="ssl -botan -debug -doc -examples -gcrypt -gpg -logger -nss -pkcs11 -sasl -softstore -test"
[ebuild N ] kde-frameworks/karchive-6.27.0 USE="crypt zstd -debug -test"
[ebuild N ] kde-frameworks/kguiaddons-6.27.0 USE="dbus wayland -X -debug -test"
[ebuild N ] kde-frameworks/kdoctools-6.27.0 USE="nls -debug -test"
[ebuild N ] kde-frameworks/kcolorscheme-6.27.0 USE="-debug"
[ebuild N ] kde-frameworks/kcrash-6.27.0 USE="-X -debug -test"
[ebuild N ] kde-frameworks/knotifications-6.27.0 USE="-debug"
[ebuild N ] kde-frameworks/kservice-6.27.0 USE="man -debug -test"
[ebuild N ] kde-frameworks/kwallet-6.27.0 USE="-debug -minimal -test"
[ebuild N ] kde-frameworks/kwallet-runtime-6.27.0 USE="(keyring) (legacy-kwallet) man -X -debug -gpg -test"
[ebuild N ] kde-plasma/ksshaskpass-6.6.6 USE="-debug"
Would you like to merge these packages? [Yes/No]
lxqt-base/lxqt-openssh-askpassが次ですが、これは直接Xを必要とします。さらに、いくつかのKDEフレームワークパッケージとXサポート付きでビルドされたQtを引き込みます。これは、私のシステムに既に存在する-Xでコンパイルされたdev-qt/qtbaseとスロット競合を起こすため、Portageは停止します。
~ # emerge -a lxqt-base/lxqt-openssh-askpass
These are the packages that would be merged, in order:
Calculating dependencies... done!
Dependency resolution took 3.10 s (backtrack: 0/20).
[ebuild R ] x11-libs/libxkbcommon-1.13.2 USE="X*"
[ebuild N ] dev-util/lxqt-build-tools-2.4.0
[ebuild N ] x11-libs/xcb-util-renderutil-0.3.10 ABI_X86="(64) -32 (-x32)"
[ebuild N ] x11-libs/libXScrnSaver-1.2.5 USE="-doc" ABI_X86="(64) -32 (-x32)"
[ebuild N ] dev-libs/plasma-wayland-protocols-1.21.0 USE="-test"
[ebuild N ] x11-libs/xcb-util-wm-0.4.2 ABI_X86="(64) -32 (-x32)"
[ebuild N ] x11-libs/xcb-util-0.4.1 ABI_X86="(64) -32 (-x32)"
[ebuild N ] x11-libs/xcb-util-image-0.4.1 ABI_X86="(64) -32 (-x32)"
[ebuild N ] x11-libs/xcb-util-cursor-0.1.6 ABI_X86="(64) -32 (-x32)"
[ebuild R ] dev-qt/qtbase-6.11.1 USE="X*"
[ebuild N ] sys-auth/polkit-qt-0.201.1
[ebuild N ] dev-libs/libqtxdg-4.4.0 USE="-test"
[ebuild N ] kde-frameworks/kf-env-6
[ebuild N ] kde-frameworks/kwindowsystem-6.27.0 USE="X wayland -debug -test"
[ebuild N ] lxqt-base/liblxqt-2.4.0 USE="backlight"
[ebuild N ] lxqt-base/lxqt-openssh-askpass-2.4.0
!!! Multiple package instances within a single package slot have been pulled
!!! into the dependency graph, resulting in a slot conflict:
dev-qt/qtbase:6
(dev-qt/qtbase-6.11.1:6/6.11.1::gentoo, installed) USE="concurrent dbus gui icu libinput libproxy network nls opengl sql sqlite ssl syslog udev vulkan wayland widgets xml (zstd) -X -accessibility -brotli -cups -custom-cflags -eglfs -evdev -gles2-only -gssapi -gtk -io-uring (-journald) -mysql -oci8 -odbc -postgres -renderdoc -sctp -test -tslib" ABI_X86="(64)" pulled in by
~dev-qt/qtmultimedia-6.11.1:6/6.11.1::gentoo, installed) USE="alsa dbus ffmpeg gstreamer opengl pipewire qml v4l vaapi vulkan wayland -X -custom-cflags -eglfs -pulseaudio -test" ABI_X86="(64)"
^^
(dev-qt/qtbase-6.11.1:6/6.11.1::gentoo, ebuild scheduled for merge) USE="X concurrent dbus gui icu libinput libproxy network nls opengl sql sqlite ssl syslog udev vulkan wayland widgets xml (zstd) -accessibility -brotli -cups -custom-cflags -eglfs -evdev -gles2-only -gssapi -gtk -io-uring (-journald) -mysql -oci8 -odbc -postgres -renderdoc -sctp -test -tslib" ABI_X86="(64)" pulled in by
>=dev-qt/qtbase-6.10.1:6=[gui,X] required by (kde-frameworks/kwindowsystem-6.27.0:6/6.27::gentoo, ebuild scheduled for merge) USE="X wayland -debug -test" ABI_X86="(64)"
^^
It may be possible to solve this problem by using package.mask to
prevent one of those packages from being selected. However, it is also
possible that conflicting dependencies exist such that they are
impossible to satisfy simultaneously. If such a conflict exists in
the dependencies of two different packages, then those packages can
not be installed simultaneously.
For more information, see MASKED PACKAGES section in the emerge man
page or refer to the Gentoo Handbook.
The following USE changes are necessary to proceed:
(see "package.use" in the portage(5) man page for more details)
# required by lxqt-base/liblxqt-2.4.0::gentoo
# required by lxqt-base/lxqt-openssh-askpass-2.4.0::gentoo
# required by lxqt-base/lxqt-openssh-askpass (argument)
>=kde-frameworks/kwindowsystem-6.27.0 X
# required by kde-frameworks/kwindowsystem-6.27.0::gentoo[X]
# required by lxqt-base/liblxqt-2.4.0::gentoo
# required by lxqt-base/lxqt-openssh-askpass-2.4.0::gentoo
# required by lxqt-base/lxqt-openssh-askpass (argument)
>=dev-qt/qtbase-6.11.1 X
# required by dev-qt/qtbase-6.11.1::gentoo[gui]
# required by kde-frameworks/kwindowsystem-6.27.0::gentoo[X]
# required by lxqt-base/liblxqt-2.4.0::gentoo
# required by lxqt-base/lxqt-openssh-askpass-2.4.0::gentoo
# required by lxqt-base/lxqt-openssh-askpass (argument)
>=x11-libs/libxkbcommon-1.13.2 X
Would you like to add these changes to your config files? [Yes/No]
net-misc/ssh-askpass-fullscreenもXを必要とします。これはGTK2とXサポート付きでビルドされたCairoを経由します。
~ # emerge -a net-misc/ssh-askpass-fullscreen
These are the packages that would be merged, in order:
Calculating dependencies... done!
Dependency resolution took 2.98 s (backtrack: 0/20).
[ebuild R ] x11-libs/cairo-1.18.4-r1 USE="X*"
[ebuild N ] x11-libs/libXcomposite-0.4.7 USE="-doc" ABI_X86="(64) -32 (-x32)"
[ebuild N ] x11-libs/libXcursor-1.2.3 USE="-doc" ABI_X86="(64) -32 (-x32)"
[ebuild N ] x11-libs/libXdamage-1.1.7 ABI_X86="(64) -32 (-x32)"
[ebuild N ] x11-libs/libXi-1.8.3 USE="-doc" ABI_X86="(64) -32 (-x32)"
[ebuild N ] x11-themes/gnome-themes-standard-3.28-r1
[ebuild NS ] x11-libs/gtk+-2.24.33-r3 [3.24.52]
[ebuild N ] x11-themes/gtk-engines-adwaita-3.28-r1 ABI_X86="(64) -32 (-x32)"
[ebuild N ] net-misc/ssh-askpass-fullscreen-1.2
The following USE changes are necessary to proceed:
(see "package.use" in the portage(5) man page for more details)
# required by x11-libs/gtk+-2.24.33-r3::gentoo
# required by x11-themes/gtk-engines-adwaita-3.28-r1::gentoo
>=x11-libs/cairo-1.18.4-r1 X
Would you like to add these changes to your config files? [Yes/No]
net-misc/x11-ssh-askpassは名前の通りX11であり、驚くことではありません。また、コンパイルするためには古いimakeビルドシステム、すなわちx11-misc/xorg-cf-filesとx11-misc/imakeを必要とします。
~ # emerge -a net-misc/x11-ssh-askpass
These are the packages that would be merged, in order:
Calculating dependencies... done!
Dependency resolution took 1.00 s (backtrack: 0/20).
[ebuild N ] app-text/rman-3.2-r2
[ebuild N ] x11-misc/xorg-cf-files-1.0.9
[ebuild N ] x11-misc/imake-1.0.10
[ebuild N ] net-misc/x11-ssh-askpass-1.2.4.1-r2
Would you like to merge these packages? [Yes/No]
残ったのはnet-misc/gnome-ssh-askpassです。一見、Xを全く必要としない唯一の選択肢のように見えましたが、それは間違いでした。X11を必要とし、ebuildはその点で壊れているようです。ビルドはpkg-config --libs gtk+-3.0 x11を呼び出し、ソースはX専用のGDKヘッダーであるgdk/gdkx.hを含んでいます。そのため、Xなしでコンパイルされたシステムではビルドに失敗します。
~ # emerge -a net-misc/gnome-ssh-askpass
These are the packages that would be merged, in order:
Calculating dependencies... done!
Dependency resolution took 0.89 s (backtrack: 0/20).
[ebuild N ~] net-misc/gnome-ssh-askpass-10.0_p1 USE="-verify-sig"
Would you like to merge these packages? [Yes/No] Yes
>>> Verifying ebuild manifests
>>> Emerging (1 of 1) net-misc/gnome-ssh-askpass-10.0_p1::guru
>>> Failed to emerge net-misc/gnome-ssh-askpass-10.0_p1, Log file:
>>> '/var/tmp/portage/net-misc/gnome-ssh-askpass-10.0_p1/temp/build.log'
>>> Jobs: 0 of 1 complete, 1 failed Load avg: 0.79, 0.57, 0.61
* Package: net-misc/gnome-ssh-askpass-10.0_p1:0
* Repository: guru
* USE: abi_x86_64 amd64 elibc_glibc kernel_linux
* FEATURES: network-sandbox preserve-libs sandbox selinux sesandbox userpriv usersandbox
>>> Unpacking source...
>>> Unpacking 'openssh-10.0p1.tar.gz' to /var/tmp/portage/net-misc/gnome-ssh-askpass-10.0_p1/work
>>> Source unpacked in /var/tmp/portage/net-misc/gnome-ssh-askpass-10.0_p1/work
>>> Preparing source in /var/tmp/portage/net-misc/gnome-ssh-askpass-10.0_p1/work/openssh-10.0p1/contrib ...
>>> Source prepared.
>>> Configuring source in /var/tmp/portage/net-misc/gnome-ssh-askpass-10.0_p1/work/openssh-10.0p1/contrib ...
>>> Source configured.
>>> Compiling source in /var/tmp/portage/net-misc/gnome-ssh-askpass-10.0_p1/work/openssh-10.0p1/contrib ...
make -j17 -l15.95 gnome-ssh-askpass3
x86_64-pc-linux-gnu-gcc -O2 -pipe -march=native `pkg-config --cflags gtk+-3.0` \
gnome-ssh-askpass3.c -o gnome-ssh-askpass3 \
`pkg-config --libs gtk+-3.0 x11`
gnome-ssh-askpass3.c:62:10: fatal error: gdk/gdkx.h: No such file or directory
62 | #include <gdk/gdkx.h>
| ^~~~~~~~~~~~
compilation terminated.
make: *** [Makefile:17: gnome-ssh-askpass3] Error 1
* ERROR: net-misc/gnome-ssh-askpass-10.0_p1::guru failed (compile phase):
* emake failed
*
* If you need support, post the output of `emerge --info '=net-misc/gnome-ssh-askpass-10.0_p1::guru'`,
* the complete build log and the output of `emerge -pqv '=net-misc/gnome-ssh-askpass-10.0_p1::guru'`.
* The complete build log is located at '/var/tmp/portage/net-misc/gnome-ssh-askpass-10.0_p1/temp/build.log'.
* The ebuild environment file is located at '/var/tmp/portage/net-misc/gnome-ssh-askpass-10.0_p1/temp/environment'.
* Working directory: '/var/tmp/portage/net-misc/gnome-ssh-askpass-10.0_p1/work/openssh-10.0p1/contrib'
* S: '/var/tmp/portage/net-misc/gnome-ssh-askpass-10.0_p1/work/openssh-10.0p1/contrib'
* Messages for package net-misc/gnome-ssh-askpass-10.0_p1:
* ERROR: net-misc/gnome-ssh-askpass-10.0_p1::guru failed (compile phase):
* emake failed
* If you need support, post the output of `emerge --info '=net-misc/gnome-ssh-askpass-10.0_p1::guru'`,
* the complete build log and the output of `emerge -pqv '=net-misc/gnome-ssh-askpass-10.0_p1::guru'`.
* The complete build log is located at '/var/tmp/portage/net-misc/gnome-ssh-askpass-10.0_p1/temp/build.log'.
* The ebuild environment file is located at '/var/tmp/portage/net-misc/gnome-ssh-askpass-10.0_p1/temp/environment'.
* Working directory: '/var/tmp/portage/net-misc/gnome-ssh-askpass-10.0_p1/work/openssh-10.0p1/contrib'
* S: '/var/tmp/portage/net-misc/gnome-ssh-askpass-10.0_p1/work/openssh-10.0p1/contrib'
*
* The following package has failed to build, install, or execute postinst:
*
* (net-misc/gnome-ssh-askpass-10.0_p1:0/0::guru, ebuild scheduled for merge), Log file:
* '/var/tmp/portage/net-misc/gnome-ssh-askpass-10.0_p1/temp/build.log'
*
ここで、パッケージ化されたオプションは諦めました。X11の問題を脇に置いても、これらのすべてがGTK2またはGTK3(最大)を使用しています。しかし、私は長い間GTK4で何かをビルドしたいと思っていたので、既存の実装のいずれかをパッチする代わりに(それらはほとんどC言語でした)、Zig 0.16とGTK4で独自のものを書き、それをssh-askpass-zigtkと名付けました。
X11の回避
GTKヘルパーが私のシステムで失敗する理由はヘッダーです。標準的なGTKの呼び出し方にはGTK4ヘッダーが含まれますが、これはGDKを引き込みます。そしてGDKはほとんどのインストールでまだgdk/gdkx.hを出荷しているため、望むかどうかにかかわらずX11ヘッダーが取り込まれます。Zigの@cImportは、Cライブラリを呼び出す明らかな方法ですが、それらのヘッダーを正確に引き込むため、同じことをします。そのため、ssh-askpass-zigtkは何も@cImportしません。src/gtk.zigは、プログラムが手動で呼び出す30個ほどのGTKおよびGLib関数を、単純なexternプロトタイプとして宣言しています。
pub const Widget = opaque {};
pub extern fn gtk_window_new() *Widget;
pub extern fn gtk_password_entry_new() *Widget;
pub extern fn gtk_editable_get_text(editable: *Widget) [*:0]const u8;
pub extern fn gtk_button_new_with_label(label: [*:0]const u8) *Widget;
そのファイルにはgdk/gdkx.hやX11/Xlib.hのシンボルを名前で指定しているものはないため、コンパイラはXヘッダーを見ることはなく、バイナリはX11なしでコンパイルされたGTK4に対してビルドされ、実行されます。必要なX関連の値であるEscapeキーシンボルは、gdk/gdkkeysyms.hから取得するのではなく、0xff1bとしてハードコードされています。
GTKはGObject上に構築されており、親構造体を子構造体の最初のメンバーとして配置することで単一継承を行います。そのため、ウィンドウ、ボックス、ラベル、パスワード入力フィールド、ボタンはすべて、C ABIが既にフラット化しているものをモデル化するためのラッパー型の階層なしに、ABI境界ではGtkWidget *とレイアウト互換性があります。Zig側では、opaque {}型がそれらすべてを代用し、すべてのウィジェット関数は同じ*Widgetを受け取り、返します。
GTKに触れない部分、すなわちSSH_ASKPASS_PROMPTをダイアログタイプにマッピングすることや、GNOME_SSH_ASKPASS_*_COLOR変数の解析は、単体テストとともにsrc/root.zigにあり、ディスプレイやGTKなしでzig build testの下で実行されます。GTK4はgtk_widget_modify_fgと_bgを削除したため、再着色は小さなCSSプロバイダーを通じて行われます。
クロスコンパイル
バインディングは手書きのexternであり、GTKヘッダーがビルドに入らないため、Zigはターゲット用のGTK4ツールチェーンなしで、任意のLinuxアーキテクチャに対してバイナリをクロスコンパイルできます。リンク時に欠けているのはGTK4共有ライブラリ自体だけですが、-Dgtk-stubがそれをカバーします。これは、エクスポートされたシンボルがすべて no-op である小さなスタブlibgtk-4.so.1をビルドし、実行可能ファイルをそれにリンクし、ターゲットの実際のGTK4が実行時に解決されるようにします。リリースワークフローはこれを使用して、GTK4がインストールされていない1台のマシンから、x86_64、aarch64、armv7、riscv64、powerpc64le、i386、loongarch64、s390xのバイナリを生成します。
注意: ssh-askpass-zigtkは、他のaskpass実装が通常行うようにキーボードをグラブしません。GTK3ヘルパーはgdk_seat_grabを呼び出すため、他のクライアントは入力中のパスフレーズを読み取れません。しかし、私の見たところでは、GTK4はそのインターフェースを廃止し、Waylandはクライアントがキーボードをグラブすることを許可しないと信じているため、X11なしではそれを実現するポータブルな方法はありません。したがって、GNOME_SSH_ASKPASS_GRAB_SERVERおよびGNOME_SSH_ASKPASS_GRAB_POINTER変数も効果がありません。
コードはtty.failにあり、GitHubにもミラーリングされています。そこでは、タグ付けされた各リリースがアーキテクチャごとにプリビルドされたLinuxバイナリを出荷しています。使用するには、バイナリをPATH上のどこかに置き、SSH_ASKPASSをそれにポイントしてください。ターミナルでは、~/.profileまたはシェルの起動ファイル(例: 私のZshユーザーの皆さんのための~/.zshrc)に2行を追加します。
export SSH_ASKPASS=~/.local/bin/ssh-askpass-zigtk
export SSH_ASKPASS_REQUIRE=prefer
SSH_ASKPASS_REQUIRE=preferは、OpenSSH 8.4以降では、ターミナルが利用可能な場合でも、グラフィカルセッションが存在する限りダイアログを使用するようにOpenSSHに指示します。systemdユーザーセッションでは、同じ2行を変数として~/.config/environment.d/ssh-askpass.confに記述します。絶対パスを使用し、~の展開やシェルの実行は行いません。ログアウトして再度ログインすると、ターミナルなしでパスフレーズを必要とする次のssh-add、git pull、またはgo getでダイアログが表示されます。