Mount SFTP folders in network storage, as CoreDesktop does
configure storage now finds machines that announce SFTP as well as SMB servers, and takes sftp://[user@]host[:port][/path] addresses. Folders are mounted with SSHFS on macFUSE, as you, with your SSH keys (Bitwarden's SSH agent while it runs one), and never ask for a password. storage-mount mounts them again at login and when the network changes, as it does SMB shares; each line of the list now records its mount point. macFUSE and SSHFS are offered for download, checked against their Team ID (3T5GSNBU6W, read from the packages' signatures). On macOS 26 and later macFUSE mounts through FSKit, whose extension you turn on once in System Settings. Earlier macOS needs its kernel extension, which this uses if it's running but doesn't set up. smoketest: 85-sftp installs both and mounts a folder from the VM itself over SSH, if macFUSE's extension can be turned on there. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+48
-11
@@ -10,11 +10,15 @@
|
||||
#
|
||||
# To check an app's Team ID on a Mac where it's installed:
|
||||
# codesign -dv /Applications/<App>.app 2>&1 | grep TeamIdentifier
|
||||
# or a package's: pkgutil --check-signature <file>.pkg
|
||||
|
||||
# app_info <name>: sets APP_TITLE, APP_BUNDLE, APP_TEAM, APP_KIND (dmg or
|
||||
# pkg) and APP_URL. Nextcloud's URL changes with each release, so app_url
|
||||
# looks it up.
|
||||
# app_info <name>: sets APP_TITLE, APP_BUNDLE, APP_TEAM, APP_KIND and
|
||||
# APP_URL, and APP_PATHS: where it is once installed, separated by colons.
|
||||
# APP_KIND is dmg (an app to copy out of a disk image), pkg (an installer
|
||||
# package) or dmgpkg (an installer package in a disk image). Some URLs change
|
||||
# with each release, so app_url looks them up.
|
||||
app_info() {
|
||||
APP_BUNDLE="" APP_PATHS=""
|
||||
case "$1" in
|
||||
element)
|
||||
APP_TITLE="Element" APP_BUNDLE="Element.app" APP_TEAM="7J4U792NQT" APP_KIND=dmg
|
||||
@@ -25,20 +29,31 @@ app_info() {
|
||||
tailscale)
|
||||
APP_TITLE="Tailscale" APP_BUNDLE="Tailscale.app" APP_TEAM="W5364U7YZB" APP_KIND=pkg
|
||||
APP_URL="https://pkgs.tailscale.com/stable/Tailscale-latest-macos.pkg" ;;
|
||||
# Network storage's SFTP folders. Both are made by macFUSE's author.
|
||||
macfuse)
|
||||
APP_TITLE="macFUSE" APP_TEAM="3T5GSNBU6W" APP_KIND=dmgpkg
|
||||
APP_PATHS="/Library/Filesystems/macfuse.fs"
|
||||
APP_URL="https://api.github.com/repos/macfuse/macfuse/releases/latest" ;;
|
||||
sshfs)
|
||||
APP_TITLE="SSHFS" APP_TEAM="3T5GSNBU6W" APP_KIND=pkg
|
||||
APP_PATHS="/usr/local/bin/sshfs"
|
||||
APP_URL="https://macfuse.github.io/" ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
[ -n "$APP_PATHS" ] || APP_PATHS="/Applications/$APP_BUNDLE:$HOME/Applications/$APP_BUNDLE"
|
||||
}
|
||||
|
||||
# Where the app is installed, if it is
|
||||
app_path() {
|
||||
local path IFS=:
|
||||
app_info "$1" || return 1
|
||||
if [ -d "/Applications/$APP_BUNDLE" ]; then
|
||||
echo "/Applications/$APP_BUNDLE"
|
||||
elif [ -d "$HOME/Applications/$APP_BUNDLE" ]; then
|
||||
echo "$HOME/Applications/$APP_BUNDLE"
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
for path in $APP_PATHS; do
|
||||
if [ -e "$path" ]; then
|
||||
echo "$path"
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
app_installed() {
|
||||
@@ -47,11 +62,19 @@ app_installed() {
|
||||
|
||||
# The download URL for app_info's app
|
||||
app_url() {
|
||||
local ext=pkg url
|
||||
case "$APP_KIND" in dmg|dmgpkg) ext=dmg ;; esac
|
||||
case "$APP_URL" in
|
||||
https://api.github.com/*)
|
||||
curl -sf --max-time 30 "$APP_URL" \
|
||||
| json_get 'd.assets.map(function (x) { return x.browser_download_url; })
|
||||
.filter(function (u) { return /\.pkg$/.test(u); })[0]' ;;
|
||||
.filter(function (u) { return u.slice(-a[0].length - 1) === "." + a[0]; })[0]' "$ext" ;;
|
||||
# SSHFS' macOS package isn't in its latest GitHub release; macFUSE's site
|
||||
# links the current one
|
||||
https://macfuse.github.io/)
|
||||
url="$(curl -sf --max-time 30 "$APP_URL" \
|
||||
| grep -oE 'https://github\.com/libfuse/sshfs/releases/download/[^"]+\.pkg' | head -n 1)"
|
||||
echo "${url:-https://github.com/libfuse/sshfs/releases/download/sshfs-3.7.5/sshfs-3.7.5.pkg}" ;;
|
||||
*) echo "$APP_URL" ;;
|
||||
esac
|
||||
}
|
||||
@@ -110,6 +133,20 @@ install_dmg() {
|
||||
return $status
|
||||
}
|
||||
|
||||
# Install the package in a downloaded .dmg (macFUSE's), once it's checked
|
||||
install_dmgpkg() {
|
||||
local dmg="$1" mount="$2/mount" pkg status=1
|
||||
hdiutil attach -nobrowse -readonly -noautoopen -mountpoint "$mount" "$dmg" > /dev/null || return 1
|
||||
pkg="$(find "$mount" -maxdepth 2 -name '*.pkg' | head -n 1)"
|
||||
if [ -z "$pkg" ]; then
|
||||
echo "The download doesn't have an installer package in it."
|
||||
else
|
||||
install_pkg "$pkg" && status=0
|
||||
fi
|
||||
hdiutil detach -quiet "$mount" || hdiutil detach -quiet -force "$mount"
|
||||
return $status
|
||||
}
|
||||
|
||||
# Download and install an app
|
||||
app_install() {
|
||||
local name="$1" url tmp status
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Purpose : Folders from other machines over SFTP, mounted with SSHFS on
|
||||
# macFUSE. Sourced by configure.d/60-storage.sh and
|
||||
# system/storage-mount.
|
||||
#
|
||||
# macOS can't mount SFTP folders itself, so this needs macFUSE and SSHFS
|
||||
# (configure storage offers to install them). On macOS 26 and later, macFUSE
|
||||
# mounts through FSKit, in user space; you turn its file system extension on
|
||||
# once in System Settings. On earlier macOS it needs its kernel extension,
|
||||
# which on Apple silicon means lowering security in Recovery: this uses it if
|
||||
# you've done that, but doesn't do it for you.
|
||||
#
|
||||
# Like CoreDesktop's sftp-mount, it runs as you and uses your SSH keys (from
|
||||
# Bitwarden's SSH agent while it runs one, otherwise macOS' own agent and
|
||||
# ~/.ssh), and never asks for a password, so the server has to accept a key.
|
||||
|
||||
SSHFS="/usr/local/bin/sshfs"
|
||||
BITWARDEN_SSH_AGENT="$HOME/Library/Containers/com.bitwarden.desktop/Data/.bitwarden-ssh-agent.sock"
|
||||
|
||||
# Which macFUSE backend mounts here: fskit or kernel. Fails if neither can.
|
||||
fuse_backend() {
|
||||
if [ "$(sw_vers -productVersion | cut -d. -f1)" -ge 26 ]; then
|
||||
echo fskit
|
||||
elif kmutil showloaded --list-only 2> /dev/null | grep -q io.macfuse.filesystems.macfuse \
|
||||
|| kextstat 2> /dev/null | grep -q io.macfuse.filesystems.macfuse; then
|
||||
echo kernel
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Whether you've turned on macFUSE's file system extension: 0 yes, 1 no, 2
|
||||
# can't tell. pluginkit marks an extension you've turned on with "+".
|
||||
fskit_enabled() {
|
||||
local line
|
||||
line="$(pluginkit -m -A -D -p com.apple.fskit.fsmodule 2> /dev/null | grep -i macfuse | head -n 1)"
|
||||
[ -n "$line" ] || return 2
|
||||
[[ "$line" == +* ]]
|
||||
}
|
||||
|
||||
# Ready unless pluginkit says it's off
|
||||
fskit_ready() {
|
||||
fskit_enabled
|
||||
[ $? -ne 1 ]
|
||||
}
|
||||
|
||||
# sftp://[user@]host[:port][/path] -> SFTP_USER, SFTP_HOST, SFTP_PORT and
|
||||
# SFTP_PATH (empty for your home folder on the server)
|
||||
sftp_parse() {
|
||||
local rest="${1#sftp://}" hostport
|
||||
SFTP_USER="" SFTP_PATH="" SFTP_PORT=22
|
||||
if [[ "$rest" == *@* ]]; then
|
||||
SFTP_USER="${rest%%@*}"
|
||||
rest="${rest#*@}"
|
||||
fi
|
||||
hostport="${rest%%/*}"
|
||||
[[ "$rest" == */?* ]] && SFTP_PATH="/${rest#*/}"
|
||||
SFTP_HOST="${hostport%%:*}"
|
||||
[[ "$hostport" == *:* ]] && SFTP_PORT="${hostport##*:}"
|
||||
return 0
|
||||
}
|
||||
|
||||
use_ssh_agent() {
|
||||
[ -S "$BITWARDEN_SSH_AGENT" ] && export SSH_AUTH_SOCK="$BITWARDEN_SSH_AGENT"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Whether the server takes one of your keys. Run in a terminal, ssh asks to
|
||||
# accept the server's key the first time, which the mount itself can't.
|
||||
sftp_key_works() {
|
||||
sftp_parse "$1"
|
||||
use_ssh_agent
|
||||
ssh -p "$SFTP_PORT" -o ConnectTimeout=10 -o PasswordAuthentication=no \
|
||||
-o KbdInteractiveAuthentication=no "$SFTP_USER@$SFTP_HOST" true
|
||||
}
|
||||
|
||||
# Where a folder mounts: /Volumes/<name> through FSKit, which makes the
|
||||
# folder itself; with the kernel extension, ~/Volumes/<name>, as only root
|
||||
# can make folders in /Volumes
|
||||
sftp_mount_point() {
|
||||
local backend
|
||||
backend="$(fuse_backend)" || return 1
|
||||
if [ "$backend" = fskit ]; then echo "/Volumes/$1"; else echo "$HOME/Volumes/$1"; fi
|
||||
}
|
||||
|
||||
sftp_mounted() {
|
||||
mount | grep -qF " on $1 ("
|
||||
}
|
||||
|
||||
# sftp_mount <url> <mount point>. SSHFS goes to the background once it's
|
||||
# mounted. reconnect and ServerAlive* bring it back after the network drops
|
||||
# (e.g. sleep); idmap=user shows your files on the server as yours here.
|
||||
sftp_mount() {
|
||||
local url="$1" point="$2" backend opts
|
||||
backend="$(fuse_backend)" || return 1
|
||||
sftp_parse "$url"
|
||||
use_ssh_agent
|
||||
opts="volname=$(basename "$point"),port=$SFTP_PORT,reconnect,ServerAliveInterval=15,ServerAliveCountMax=3"
|
||||
opts="$opts,BatchMode=yes,ConnectTimeout=15,idmap=user"
|
||||
if [ "$backend" = fskit ]; then
|
||||
opts="$opts,backend=fskit"
|
||||
else
|
||||
mkdir -p "$point" || return 1
|
||||
opts="$opts,defer_permissions,noappledouble"
|
||||
fi
|
||||
"$SSHFS" "$SFTP_USER@$SFTP_HOST:$SFTP_PATH" "$point" -o "$opts"
|
||||
}
|
||||
|
||||
sftp_unmount() {
|
||||
local point="$1"
|
||||
if sftp_mounted "$point"; then
|
||||
umount "$point" 2> /dev/null || diskutil unmount force "$point" > /dev/null 2>&1 || return 1
|
||||
fi
|
||||
case "$point" in "$HOME/Volumes/"*) rmdir "$point" 2> /dev/null ;; esac
|
||||
return 0
|
||||
}
|
||||
Reference in New Issue
Block a user