#!/bin/bash
# configure reads TITLE and DESCRIPTION
# shellcheck disable=SC2034
TITLE="Network storage"
# shellcheck disable=SC2034
DESCRIPTION="Mount shared folders from your network"
# What's mounted, one smb://user@host/share per line. It exists once you've
# run this, even if you added nothing, so this isn't "to do" without a server.
STORAGE_CONF="$CONF_DIR/storage"
STORAGE_MOUNT="$CONNECTOR/system/storage-mount"
LABEL="computer.federated.connector.storage"
PLIST="$HOME/Library/LaunchAgents/$LABEL.plist"
source "$CONNECTOR/configure.d/lib/mdns.sh"
source "$CONNECTOR/configure.d/lib/smb.sh"
is_configured() {
[ -f "$STORAGE_CONF" ]
}
# launchd runs system/storage-mount at login and whenever the network changes
# (macOS rewrites SystemConfiguration then), so shares come back on their own
install_job() {
mkdir -p "$(dirname "$PLIST")"
cat > "$PLIST" << EOF
Label$LABEL
ProgramArguments
/bin/bash
$(xml_escape "$STORAGE_MOUNT")
RunAtLoad
WatchPaths
/Library/Preferences/SystemConfiguration
ThrottleInterval30
ProcessTypeBackground
EOF
launchctl bootout "gui/$(id -u)/$LABEL" 2> /dev/null
launchctl bootstrap "gui/$(id -u)" "$PLIST"
}
remove_job() {
launchctl bootout "gui/$(id -u)/$LABEL" 2> /dev/null
rm -f "$PLIST"
}
xml_escape() {
local s="$1"
s="${s//&/\&}"
s="${s//\<}"
s="${s//>/\>}"
printf '%s' "$s"
}
# Mount smb://[user@]host[/share] and record what was mounted. Without a
# share, macOS lists the server's shares to choose from. Its sign-in window
# can keep the password in your keychain, which mounting again later needs.
add_share() {
local url="$1" host before after line new=0 user share entry
host="$(host_of "$url")"
echo "macOS asks you to sign in to $host. Tick \"Remember this password in my"
echo "keychain\", so the share can be mounted again at login."
before="$(smb_mounts)"
if ! mount_share "$url"; then
if mounted_at "$url" > /dev/null; then
echo "$url is already mounted."
else
echo "$host's share wasn't mounted (wrong password, or cancelled)."
return 1
fi
fi
after="$(smb_mounts)"
while IFS= read -r line; do
[ -n "$line" ] || continue
grep -qxF "$line" <<< "$before" && continue
# //user@host/share -> smb://user@/share
entry="${line%%|*}"
entry="${entry#//}"
user=""
[[ "$entry" == *@* ]] && user="${entry%%@*}@"
share="${entry#*/}"
record "smb://$user$host/$share" "${line#*|}"
new=$((new + 1))
done <<< "$after"
if [ $new -eq 0 ] && [[ "${url#smb://}" == */?* ]] && mounted_at "$url" > /dev/null; then
record "$url" "$(mounted_at "$url")"
fi
}
record() {
local url="$1" point="$2" line
while IFS= read -r line; do
[ "$(share_key "$line")" = "$(share_key "$url")" ] && { echo "$url was already added."; return 0; }
done < "$STORAGE_CONF"
echo "$url" >> "$STORAGE_CONF"
[ -f "$PLIST" ] || install_job
echo "$url is at $point (in Finder's sidebar under Locations). It's mounted again at login."
}
# Unmount it and take it out of the list
remove_share() {
local url="$1" point tmp
if point="$(mounted_at "$url")"; then
diskutil unmount "$point" > /dev/null 2>&1 || umount "$point" 2> /dev/null \
|| echo "Couldn't unmount $point; eject it in Finder."
fi
tmp="$(mktemp)" || return 1
grep -vxF "$url" "$STORAGE_CONF" > "$tmp"
mv -f "$tmp" "$STORAGE_CONF"
[ -s "$STORAGE_CONF" ] || remove_job
echo "$url won't be mounted again. Its password stays in your keychain (Keychain Access)."
}
# The SMB servers on the network, other than this Mac: "host|name" per line
find_servers() {
local own name host
own="$(mdns_own_host)"
mdns_services _smb._tcp | while IFS='|' read -r name _ host _; do
[ "$(tr '[:upper:]' '[:lower:]' <<< "$host")" = "$(tr '[:upper:]' '[:lower:]' <<< "$own")" ] && continue
echo "$host|$name"
done | sort -u
}
find_and_add() {
local found=() line host name choice i
echo "Looking for shared folders on your network..."
while IFS= read -r line; do
found+=("$line")
done < <(find_servers)
if [ ${#found[@]} -eq 0 ]; then
echo "Nothing found. Windows PCs, NAS boxes and Macs with File Sharing on share folders"
echo "over SMB, but some don't announce it. Enter the address instead."
return 0
fi
for i in "${!found[@]}"; do
IFS='|' read -r host name <<< "${found[$i]}"
printf ' %2d) %-26s %s\n' $((i + 1)) "$host" "$name"
done
read -rp "Which? " choice
if ! [[ "$choice" =~ ^[0-9]+$ ]] || [ "$choice" -lt 1 ] || [ "$choice" -gt ${#found[@]} ]; then
echo "Choose a number from the list."
return 1
fi
add_share "smb://${found[$((choice - 1))]%%|*}"
}
enter_and_add() {
local address
read -rp "Address, e.g. smb://nas.local/files (or smb://nas.local to choose a share): " address
case "$address" in
"") return 0 ;;
smb://?*) add_share "$address" ;;
*) echo "Start it with smb://. (Folders over SSH need macFUSE, which this doesn't set up.)"
return 1 ;;
esac
}
choose_and_remove() {
local entries=() line choice i
while IFS= read -r line; do
[ -n "$line" ] && entries+=("$line")
done < "$STORAGE_CONF"
for i in "${!entries[@]}"; do
printf ' %2d) %s\n' $((i + 1)) "${entries[$i]}"
done
read -rp "Which? " choice
if ! [[ "$choice" =~ ^[0-9]+$ ]] || [ "$choice" -lt 1 ] || [ "$choice" -gt ${#entries[@]} ]; then
echo "Choose a number from the list."
return 1
fi
remove_share "${entries[$((choice - 1))]}"
}
run_configure() {
local choice url point
mkdir -p "$(dirname "$STORAGE_CONF")" && touch "$STORAGE_CONF" || return 1
echo "This mounts shared folders from other machines on your network (Windows PCs, NAS"
echo "boxes, Macs with File Sharing on) and mounts them again each time you log in."
while true; do
echo
if [ -s "$STORAGE_CONF" ]; then
while IFS='|' read -r url point; do
echo " $url ${point:-(not mounted now)}"
done < <(bash "$STORAGE_MOUNT" --list)
echo
fi
echo " 1) Find shared folders on your network"
echo " 2) Enter an address"
[ -s "$STORAGE_CONF" ] && echo " 3) Remove one"
echo " q) Done"
echo
read -rp "Choose: " choice || return 0
case "$choice" in
1) find_and_add ;;
2) enter_and_add ;;
3) [ -s "$STORAGE_CONF" ] && choose_and_remove ;;
*) return 0 ;;
esac
done
}