#!/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/ through FSKit, which makes the # folder itself; with the kernel extension, ~/Volumes/, 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 . 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 }