Files
connector-macos/configure.d/lib/sftp.sh
T
David YoungandClaude Opus 5.5 eab26c0888 Stop SSHFS hanging, and name the real reason Gatekeeper refuses an app
With macFUSE's FSKit extension off, sshfs waits forever and ignores TERM;
sftp_mount gives it 30 seconds, then stops it and says to turn macFUSE on.

On a Mac that allows only App Store apps, app_install said the download
"isn't notarized"; it now says the Mac only allows App Store apps, and where
to change that.

The vanilla image is like that and since macOS 15 only System Settings or
MDM can change it, so 70-apps checks the message and skips the installs
there. 85-sftp checks that add_sftp gives up while the extension is off.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 15:01:03 -07:00

134 lines
4.6 KiB
Bash

#!/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 pid i
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
# With macFUSE's extension off, SSHFS waits forever (and ignores TERM), so
# it gets 30 seconds
"$SSHFS" "$SFTP_USER@$SFTP_HOST:$SFTP_PATH" "$point" -o "$opts" &
pid=$!
for i in $(seq 1 30); do
kill -0 "$pid" 2> /dev/null || { wait "$pid"; return; }
sleep 1
done
kill -TERM "$pid" 2> /dev/null
sleep 2
kill -KILL "$pid" 2> /dev/null
wait "$pid" 2> /dev/null
if [ "$backend" = fskit ]; then
echo "SSHFS didn't finish mounting. Is macFUSE turned on in System Settings, under"
echo "General > Login Items & Extensions > File System Extensions?"
fi
return 1
}
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
}