run_for stopped dns-sd with INT, which bash ignores in background jobs
started from a script, so it never returned. It uses TERM now.
xml_escape's ${s//&/\&} keeps the backslash on bash 3.2; it uses sed
now, in all three copies.
10-core.sh's step() replaced the smoketest runner's step(); it's
core_step now.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
825 lines
28 KiB
Bash
825 lines
28 KiB
Bash
#!/bin/bash
|
|
|
|
# configure reads TITLE and DESCRIPTION
|
|
# shellcheck disable=SC2034
|
|
TITLE="Federated Core"
|
|
# shellcheck disable=SC2034
|
|
DESCRIPTION="Mail, Calendar, Contacts, Superpanel, files and Element"
|
|
|
|
# Each Core is in cores/<domain>.conf, with what you entered last time, so
|
|
# running this again only does what's missing. core.conf is a copy of the main
|
|
# Core's: Element and Tailscale use that one.
|
|
CORE_CONF="$CONF_DIR/core.conf"
|
|
CORES_DIR="$CONF_DIR/cores"
|
|
TAILSCALE_CONF="$CONF_DIR/tailscale.conf"
|
|
BACKUP_CONF="$CONF_DIR/backup.conf"
|
|
BACKUP_KEYCHAIN="federated-connector-backup"
|
|
ELEMENT_CONFIG="$HOME/Library/Application Support/Element/config.json"
|
|
NEXTCLOUD_CFG="$HOME/Library/Preferences/Nextcloud/nextcloud.cfg"
|
|
source "$CONNECTOR/configure.d/lib/json.sh"
|
|
source "$CONNECTOR/configure.d/lib/keychain.sh"
|
|
source "$CONNECTOR/configure.d/lib/nextcloud.sh"
|
|
source "$CONNECTOR/configure.d/lib/apps.sh"
|
|
source "$CONNECTOR/configure.d/lib/profile.sh"
|
|
[ -f "$CORE_CONF" ] && source "$CORE_CONF"
|
|
MAIN_DOMAIN="$CORE_DOMAIN"
|
|
|
|
# Mail, Calendar and Contacts are payloads in one profile per Core
|
|
SERVICES="mail calendar contacts"
|
|
|
|
core_domains() {
|
|
local file
|
|
for file in "$CORES_DIR"/*.conf; do
|
|
[ -f "$file" ] && basename "$file" .conf
|
|
done
|
|
}
|
|
|
|
load_core() {
|
|
CORE_DOMAIN="" CORE_EMAIL="" CORE_NAME="" PROFILE_SERVICES="" PROFILE_UUID=""
|
|
[ -n "$1" ] && source "$CORES_DIR/$1.conf"
|
|
core_names
|
|
}
|
|
|
|
# CORE_DOMAIN, CORE_EMAIL and CORE_NAME come from the Core's conf; the rest
|
|
# follow
|
|
core_names() {
|
|
CORE_USER="${CORE_EMAIL%@*}"
|
|
NEXTCLOUD="https://nextcloud.$CORE_DOMAIN"
|
|
SUPERPANEL="https://superpanel.$CORE_DOMAIN"
|
|
SUPERPANEL_APP="$HOME/Applications/Superpanel ($CORE_DOMAIN).app"
|
|
MATRIX="https://matrix.$CORE_DOMAIN"
|
|
PROFILE_ID="computer.federated.connector.$CORE_DOMAIN"
|
|
PROFILE_NAME="Federated Core ($CORE_DOMAIN)"
|
|
KEYCHAIN_ACCOUNT="$CORE_USER@nextcloud.$CORE_DOMAIN"
|
|
}
|
|
|
|
# in_list <word> <list>
|
|
in_list() {
|
|
[[ " $2 " == *" $1 "* ]]
|
|
}
|
|
|
|
service_title() {
|
|
case "$1" in
|
|
mail) echo "Mail for $CORE_EMAIL" ;;
|
|
calendar) echo "Calendar with your Nextcloud calendars" ;;
|
|
contacts) echo "Contacts with your Nextcloud address books" ;;
|
|
esac
|
|
}
|
|
|
|
superpanel_done() {
|
|
[ -d "$SUPERPANEL_APP" ]
|
|
}
|
|
|
|
# The services in the profile this Mac installed last, if it's still installed
|
|
installed_services() {
|
|
[ -n "$PROFILE_UUID" ] && profile_installed "$PROFILE_ID" "$PROFILE_UUID" && echo "$PROFILE_SERVICES"
|
|
}
|
|
|
|
accounts_done() {
|
|
local installed svc
|
|
installed="$(installed_services)"
|
|
for svc in $SERVICES; do
|
|
in_list "$svc" "$installed" || return 1
|
|
done
|
|
}
|
|
|
|
# The Nextcloud app saves each account as "<n>\url=<server>"
|
|
files_done() {
|
|
grep -qsxE "[0-9]+\\\\url=${NEXTCLOUD//./\\.}/?" "$NEXTCLOUD_CFG"
|
|
}
|
|
|
|
element_installed() {
|
|
app_installed element
|
|
}
|
|
|
|
# Done when Element isn't installed, as there's nothing to set up; setting up
|
|
# the Core offers to install it
|
|
element_done() {
|
|
element_installed || return 0
|
|
[ -f "$ELEMENT_CONFIG" ] || return 1
|
|
json_get 'd.default_server_config["m.homeserver"].server_name === a[0]' \
|
|
"matrix.$CORE_DOMAIN" < "$ELEMENT_CONFIG" > /dev/null
|
|
}
|
|
|
|
# Element only follows the main Core
|
|
core_done() {
|
|
superpanel_done && accounts_done && files_done \
|
|
&& { [ "$CORE_DOMAIN" != "$MAIN_DOMAIN" ] || element_done; }
|
|
}
|
|
|
|
# Done when every Core is
|
|
is_configured() {
|
|
local domain domains
|
|
domains="$(core_domains)"
|
|
[ -n "$domains" ] || return 1
|
|
for domain in $domains; do
|
|
(load_core "$domain" && core_done) || return 1
|
|
done
|
|
}
|
|
|
|
# The first Core you set up is the main one
|
|
save_conf() {
|
|
mkdir -p "$CORES_DIR"
|
|
{
|
|
printf 'CORE_DOMAIN=%q\n' "$CORE_DOMAIN"
|
|
printf 'CORE_EMAIL=%q\n' "$CORE_EMAIL"
|
|
printf 'CORE_NAME=%q\n' "$CORE_NAME"
|
|
printf 'PROFILE_SERVICES=%q\n' "$PROFILE_SERVICES"
|
|
printf 'PROFILE_UUID=%q\n' "$PROFILE_UUID"
|
|
} > "$CORES_DIR/$CORE_DOMAIN.conf"
|
|
if [ -z "$MAIN_DOMAIN" ] || [ "$MAIN_DOMAIN" = "$CORE_DOMAIN" ]; then
|
|
make_main "$CORE_DOMAIN"
|
|
fi
|
|
}
|
|
|
|
make_main() {
|
|
cp "$CORES_DIR/$1.conf" "$CORE_CONF" && MAIN_DOMAIN="$1"
|
|
}
|
|
|
|
# Superpanel as an app in ~/Applications (so in Launchpad and Spotlight) that
|
|
# opens it in your browser. The login page carries Superpanel's icon inline.
|
|
setup_superpanel() {
|
|
local tmp
|
|
mkdir -p "$HOME/Applications"
|
|
rm -rf "$SUPERPANEL_APP"
|
|
osacompile -o "$SUPERPANEL_APP" -e "open location \"$SUPERPANEL/\"" || return 1
|
|
|
|
tmp="$(mktemp -d)"
|
|
if curl -s --max-time 15 "$SUPERPANEL/" | grep -o 'rel="icon"[^>]*base64,[^"]*' | head -n 1 \
|
|
| sed 's/.*base64,//' | base64 -D > "$tmp/icon" 2> /dev/null && [ -s "$tmp/icon" ]; then
|
|
sips -s format png -z 512 512 "$tmp/icon" --out "$tmp/icon.png" > /dev/null 2>&1 \
|
|
&& sips -s format icns "$tmp/icon.png" \
|
|
--out "$SUPERPANEL_APP/Contents/Resources/applet.icns" > /dev/null 2>&1
|
|
fi
|
|
rm -rf "$tmp"
|
|
# Changing the icon breaks osacompile's ad hoc signature, without which the
|
|
# app doesn't open on Apple silicon
|
|
codesign --force --sign - "$SUPERPANEL_APP" > /dev/null 2>&1
|
|
touch "$SUPERPANEL_APP"
|
|
echo "Added Superpanel to your applications (~/Applications)."
|
|
}
|
|
|
|
# Ask for your Core password until Nextcloud takes it (PASSWORD). With "app",
|
|
# also get an app password for Calendar and Contacts (APP_PASSWORD): the one
|
|
# this Mac keeps, if Nextcloud still takes it, or a new one
|
|
# (NEW_APP_PASSWORD=1). Asks only once while they're set.
|
|
get_passwords() {
|
|
local stored
|
|
if [ -z "$PASSWORD" ]; then
|
|
for _ in 1 2 3; do
|
|
read -rsp "${PASSWORD_PROMPT:-Your Core password}: " PASSWORD
|
|
echo
|
|
nextcloud_sign_in
|
|
case $? in
|
|
0) break ;;
|
|
2) echo "Nextcloud says that password is wrong."; PASSWORD="" ;;
|
|
*) echo "Couldn't reach Nextcloud at $NEXTCLOUD."; PASSWORD=""; return 1 ;;
|
|
esac
|
|
done
|
|
[ -n "$PASSWORD" ] || return 1
|
|
fi
|
|
[ "$1" = app ] && [ -z "$APP_PASSWORD" ] || return 0
|
|
stored="$(app_password_load)"
|
|
if [ -n "$stored" ] && nextcloud_sign_in "$stored"; then
|
|
APP_PASSWORD="$stored"
|
|
elif nextcloud_app_password "macOS Calendar and Contacts on $COMPUTER"; then
|
|
NEW_APP_PASSWORD=1
|
|
else
|
|
echo "Nextcloud didn't make an app password for Calendar and Contacts."
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
forget_passwords() {
|
|
unset PASSWORD APP_PASSWORD NEW_APP_PASSWORD
|
|
}
|
|
|
|
# The mail server publishes its settings (Thunderbird's autoconfig format).
|
|
# Sets IMAP_HOST, IMAP_PORT, IMAP_SOCKET, SMTP_HOST, SMTP_PORT, SMTP_SOCKET
|
|
# and MAIL_USER, with the usual ones if the server doesn't say.
|
|
mail_settings() {
|
|
local xml values
|
|
xml="$(curl -sf --max-time 15 \
|
|
"https://autoconfig.$CORE_DOMAIN/mail/config-v1.1.xml?emailaddress=$CORE_EMAIL")"
|
|
xpath() {
|
|
xmllint --xpath "string($1)" - 2> /dev/null <<< "$xml"
|
|
}
|
|
IMAP_HOST="$(xpath '//incomingServer[@type="imap"][1]/hostname')"
|
|
IMAP_PORT="$(xpath '//incomingServer[@type="imap"][1]/port')"
|
|
IMAP_SOCKET="$(xpath '//incomingServer[@type="imap"][1]/socketType')"
|
|
SMTP_HOST="$(xpath '//outgoingServer[@type="smtp"][1]/hostname')"
|
|
SMTP_PORT="$(xpath '//outgoingServer[@type="smtp"][1]/port')"
|
|
SMTP_SOCKET="$(xpath '//outgoingServer[@type="smtp"][1]/socketType')"
|
|
MAIL_USER="$(xpath '//incomingServer[@type="imap"][1]/username')"
|
|
if [ -z "$IMAP_HOST" ] || [ -z "$SMTP_HOST" ] || [ -z "$SMTP_SOCKET" ] \
|
|
|| [[ ! "$IMAP_PORT$SMTP_PORT" =~ ^[0-9]+$ ]]; then
|
|
echo "The mail server didn't give its settings; using mail.$CORE_DOMAIN with the usual ports."
|
|
IMAP_HOST="mail.$CORE_DOMAIN" IMAP_PORT=993 IMAP_SOCKET=SSL
|
|
SMTP_HOST="mail.$CORE_DOMAIN" SMTP_PORT=587 SMTP_SOCKET=STARTTLS
|
|
MAIL_USER="%EMAILADDRESS%"
|
|
fi
|
|
case "$MAIL_USER" in
|
|
%EMAILLOCALPART%) MAIL_USER="$CORE_USER" ;;
|
|
*) MAIL_USER="$CORE_EMAIL" ;;
|
|
esac
|
|
}
|
|
|
|
# Mail uses TLS for SSL and STARTTLS alike, and works out which from the port
|
|
tls() {
|
|
case "$1" in
|
|
SSL|STARTTLS) echo "<true/>" ;;
|
|
*) echo "<false/>" ;;
|
|
esac
|
|
}
|
|
|
|
payload_header() {
|
|
cat << EOF
|
|
<dict>
|
|
<key>PayloadType</key><string>$1</string>
|
|
<key>PayloadVersion</key><integer>1</integer>
|
|
<key>PayloadIdentifier</key><string>$PROFILE_ID.$2</string>
|
|
<key>PayloadUUID</key><string>$(stable_uuid "$PROFILE_ID.$2")</string>
|
|
<key>PayloadDisplayName</key><string>$3</string>
|
|
EOF
|
|
}
|
|
|
|
# Mail keeps your Core password: the mail server doesn't take Nextcloud's app
|
|
# passwords
|
|
payload_mail() {
|
|
payload_header com.apple.mail.managed mail Mail
|
|
cat << EOF
|
|
<key>EmailAccountDescription</key><string>$(xml_escape "$CORE_EMAIL")</string>
|
|
<key>EmailAccountName</key><string>$(xml_escape "$CORE_NAME")</string>
|
|
<key>EmailAccountType</key><string>EmailTypeIMAP</string>
|
|
<key>EmailAddress</key><string>$(xml_escape "$CORE_EMAIL")</string>
|
|
<key>IncomingMailServerAuthentication</key><string>EmailAuthPassword</string>
|
|
<key>IncomingMailServerHostName</key><string>$(xml_escape "$IMAP_HOST")</string>
|
|
<key>IncomingMailServerPortNumber</key><integer>$IMAP_PORT</integer>
|
|
<key>IncomingMailServerUseSSL</key>$(tls "$IMAP_SOCKET")
|
|
<key>IncomingMailServerUsername</key><string>$(xml_escape "$MAIL_USER")</string>
|
|
<key>IncomingPassword</key><string>$(xml_escape "$PASSWORD")</string>
|
|
<key>OutgoingMailServerAuthentication</key><string>EmailAuthPassword</string>
|
|
<key>OutgoingMailServerHostName</key><string>$(xml_escape "$SMTP_HOST")</string>
|
|
<key>OutgoingMailServerPortNumber</key><integer>$SMTP_PORT</integer>
|
|
<key>OutgoingMailServerUseSSL</key>$(tls "$SMTP_SOCKET")
|
|
<key>OutgoingMailServerUsername</key><string>$(xml_escape "$MAIL_USER")</string>
|
|
<key>OutgoingPassword</key><string>$(xml_escape "$PASSWORD")</string>
|
|
</dict>
|
|
EOF
|
|
}
|
|
|
|
# payload_dav <CalDAV|CardDAV> <service> <title>
|
|
payload_dav() {
|
|
payload_header "com.apple.$(echo "$1" | tr "[:upper:]" "[:lower:]").account" "$2" "$3"
|
|
cat << EOF
|
|
<key>$1AccountDescription</key><string>$(xml_escape "$3 ($CORE_DOMAIN)")</string>
|
|
<key>$1HostName</key><string>nextcloud.$CORE_DOMAIN</string>
|
|
<key>$1Port</key><integer>443</integer>
|
|
<key>$1PrincipalURL</key><string>$(xml_escape "$NEXTCLOUD/remote.php/dav/principals/users/$CORE_USER/")</string>
|
|
<key>$1UseSSL</key><true/>
|
|
<key>$1Username</key><string>$(xml_escape "$CORE_USER")</string>
|
|
<key>$1Password</key><string>$(xml_escape "$APP_PASSWORD")</string>
|
|
</dict>
|
|
EOF
|
|
}
|
|
|
|
payload_calendar() {
|
|
payload_dav CalDAV calendar Calendar
|
|
}
|
|
|
|
payload_contacts() {
|
|
payload_dav CardDAV contacts Contacts
|
|
}
|
|
|
|
# write_profile <file> <services> <uuid>
|
|
write_profile() {
|
|
local file="$1" services="$2" uuid="$3" svc
|
|
{
|
|
cat << EOF
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
<plist version="1.0">
|
|
<dict>
|
|
<key>PayloadContent</key>
|
|
<array>
|
|
EOF
|
|
for svc in $services; do
|
|
"payload_$svc"
|
|
done
|
|
cat << EOF
|
|
</array>
|
|
<key>PayloadDisplayName</key><string>$(xml_escape "$PROFILE_NAME")</string>
|
|
<key>PayloadDescription</key><string>$(xml_escape "Your Federated Core's email, calendars and contacts ($CORE_EMAIL).")</string>
|
|
<key>PayloadIdentifier</key><string>$PROFILE_ID</string>
|
|
<key>PayloadOrganization</key><string>$(xml_escape "$CORE_DOMAIN")</string>
|
|
<key>PayloadRemovalDisallowed</key><false/>
|
|
<key>PayloadScope</key><string>User</string>
|
|
<key>PayloadType</key><string>Configuration</string>
|
|
<key>PayloadUUID</key><string>$uuid</string>
|
|
<key>PayloadVersion</key><integer>1</integer>
|
|
</dict>
|
|
</plist>
|
|
EOF
|
|
} > "$file"
|
|
plutil -lint -s "$file"
|
|
}
|
|
|
|
# Install the profile with these services, replacing the one installed. The
|
|
# profile file has your passwords in it, so only you can read it, and it's
|
|
# deleted once macOS has it.
|
|
install_accounts() {
|
|
local services="$1" dir uuid status=0
|
|
if in_list calendar "$services" || in_list contacts "$services"; then
|
|
get_passwords app || return 1
|
|
else
|
|
get_passwords || return 1
|
|
fi
|
|
in_list mail "$services" && mail_settings
|
|
|
|
dir="$(mktemp -d)" || return 1
|
|
uuid="$(uuidgen)"
|
|
if ! (umask 077 && write_profile "$dir/$PROFILE_NAME.mobileconfig" "$services" "$uuid"); then
|
|
echo "Couldn't make the profile."
|
|
status=1
|
|
elif profile_install "$dir/$PROFILE_NAME.mobileconfig" "$PROFILE_ID" "$uuid" "$PROFILE_NAME"; then
|
|
PROFILE_SERVICES="$services" PROFILE_UUID="$uuid"
|
|
save_conf
|
|
if [ -n "$NEW_APP_PASSWORD" ]; then
|
|
app_password_save "$APP_PASSWORD" \
|
|
|| echo "Couldn't keep Calendar and Contacts' app password in your keychain; it still works."
|
|
NEW_APP_PASSWORD=""
|
|
fi
|
|
else
|
|
status=1
|
|
fi
|
|
rm -rf "$dir"
|
|
# An app password nothing uses
|
|
if [ -n "$NEW_APP_PASSWORD" ]; then
|
|
nextcloud_revoke_app_password "$APP_PASSWORD"
|
|
APP_PASSWORD="" NEW_APP_PASSWORD=""
|
|
fi
|
|
return $status
|
|
}
|
|
|
|
# Ask which of Mail, Calendar and Contacts to set up, and install a profile
|
|
# with them and the ones already set up
|
|
setup_accounts() {
|
|
local installed wanted="" svc
|
|
installed="$(installed_services)"
|
|
for svc in $SERVICES; do
|
|
if in_list "$svc" "$installed"; then
|
|
echo " $(service_title "$svc"): already set up"
|
|
wanted="$wanted $svc"
|
|
elif confirm_yes "Set up $(service_title "$svc")?"; then
|
|
wanted="$wanted $svc"
|
|
fi
|
|
done
|
|
wanted="${wanted# }"
|
|
[ -n "$wanted" ] && [ "$wanted" != "$installed" ] || return 0
|
|
install_accounts "$wanted"
|
|
}
|
|
|
|
# The Nextcloud app keeps your files in Finder. It signs in through your
|
|
# browser and makes its own app password; told the server, it skips asking.
|
|
setup_files() {
|
|
local app
|
|
app_ensure nextcloud "It keeps your Nextcloud files in Finder." || return 1
|
|
app="$(app_path nextcloud)"
|
|
if pgrep -x Nextcloud > /dev/null; then
|
|
open -a "$app"
|
|
echo "Nextcloud is already open. Click the cloud in the menu bar, then your account"
|
|
echo "name > Add account, and enter $NEXTCLOUD."
|
|
else
|
|
open -a "$app" --args --overrideserverurl "$NEXTCLOUD"
|
|
echo "Nextcloud opens with $NEXTCLOUD filled in. Choose Log in, then sign in"
|
|
echo "in your browser, and choose where your files go (the default is fine)."
|
|
fi
|
|
if ! wait_until 600 files_done; then
|
|
echo "Nextcloud isn't signed in to $NEXTCLOUD yet. Run this again when it is."
|
|
return 1
|
|
fi
|
|
echo "Nextcloud keeps your files in sync with this Mac."
|
|
}
|
|
|
|
# Element reads config.json in its folder in Application Support over its
|
|
# built-in settings. Pointing it at the Core's Matrix server means you only
|
|
# choose "Sign in" (the Core offers single sign-on through Authelia). The
|
|
# Core's Matrix server is named after its address, matrix.<domain>, and says
|
|
# where its API is.
|
|
setup_element() {
|
|
local base_url
|
|
base_url="$(curl -sf --max-time 15 "$MATRIX/.well-known/matrix/client" \
|
|
| json_get 'd["m.homeserver"].base_url')"
|
|
base_url="${base_url:-$MATRIX}"
|
|
json_edit "$ELEMENT_CONFIG" \
|
|
'd.default_server_config = {"m.homeserver": {base_url: a[0], server_name: a[1]}};' \
|
|
"${base_url%/}" "matrix.$CORE_DOMAIN" || return 1
|
|
echo "Element will use $MATRIX the next time it opens."
|
|
}
|
|
|
|
# Run a step unless it's done; a failed step doesn't stop the ones after it
|
|
core_step() {
|
|
local name="$1" title="$2"
|
|
if "${name}_done"; then
|
|
echo " $title: already set up"
|
|
return 0
|
|
fi
|
|
if ! confirm_yes "Set up $title?"; then
|
|
return 0
|
|
fi
|
|
"setup_$name" || { echo "$RED$title was not set up.$RESET"; STATUS=1; }
|
|
}
|
|
|
|
# Set up the Core loaded with load_core, or a new one
|
|
setup_core() {
|
|
local url name
|
|
STATUS=0
|
|
|
|
ask url "Your Superpanel's address (e.g. https://superpanel.example.com)" \
|
|
"${CORE_DOMAIN:+https://superpanel.$CORE_DOMAIN}"
|
|
url="${url#*://}"
|
|
url="${url%%/*}"
|
|
CORE_DOMAIN="${url#superpanel.}"
|
|
if [[ ! "$CORE_DOMAIN" =~ ^[A-Za-z0-9.-]+$ ]]; then
|
|
echo "That isn't a Superpanel address."
|
|
return 1
|
|
fi
|
|
core_names
|
|
if ! curl -s -o /dev/null --max-time 15 "$SUPERPANEL/"; then
|
|
echo "Couldn't reach $SUPERPANEL."
|
|
return 1
|
|
fi
|
|
|
|
ask CORE_EMAIL "Your Core email" "${CORE_EMAIL:-$USER@$CORE_DOMAIN}"
|
|
name="$(id -F 2> /dev/null)"
|
|
ask CORE_NAME "Your name, as shown on email you send" "${CORE_NAME:-$name}"
|
|
[[ "$CORE_EMAIL" == ?*@?* ]] || { echo "That isn't an email address."; return 1; }
|
|
core_names
|
|
save_conf
|
|
|
|
echo
|
|
core_step superpanel "Superpanel in your applications"
|
|
if accounts_done; then
|
|
echo " Mail, Calendar and Contacts: already set up"
|
|
else
|
|
setup_accounts || { echo "${RED}Mail, Calendar and Contacts were not set up.$RESET"; STATUS=1; }
|
|
fi
|
|
core_step files "Your Nextcloud files in Finder"
|
|
if [ "$CORE_DOMAIN" = "$MAIN_DOMAIN" ]; then
|
|
if ! element_installed; then
|
|
app_ensure element "It's the app for your Core's chat (Matrix)." || true
|
|
fi
|
|
element_installed && core_step element "Element with your Core's Matrix server"
|
|
fi
|
|
forget_passwords
|
|
return $STATUS
|
|
}
|
|
|
|
# After your Core password changes, e.g. in Superpanel. Mail keeps your Core
|
|
# password, so it gets the new one, in a new version of the profile. Calendar,
|
|
# Contacts and the Nextcloud app use app passwords, which stay the same;
|
|
# Nextcloud turns them off until you sign in with the new password, and
|
|
# checking the password here does that.
|
|
change_password() {
|
|
local installed
|
|
load_core "$1"
|
|
PASSWORD_PROMPT="Your new Core password"
|
|
get_passwords || return 1
|
|
echo "Nextcloud has your new password, and Calendar, Contacts and the Nextcloud app work again."
|
|
installed="$(installed_services)"
|
|
if in_list mail "$installed"; then
|
|
echo "Mail needs it too, in a new version of the \"$PROFILE_NAME\" profile."
|
|
install_accounts "$installed" || { forget_passwords; return 1; }
|
|
fi
|
|
forget_passwords
|
|
}
|
|
|
|
# Tailscale and Backup point at one Core, which can't be removed while they do
|
|
backup_on() {
|
|
[[ "$(source "$BACKUP_CONF" 2> /dev/null && echo "$BACKUP_WEBDAV")" == "https://nextcloud.$1/"* ]] \
|
|
&& launchctl print "gui/$(id -u)/computer.federated.connector.backup" > /dev/null 2>&1
|
|
}
|
|
|
|
tailscale_on() {
|
|
[ "$(source "$TAILSCALE_CONF" 2> /dev/null && echo "$TAILSCALE_DOMAIN")" = "$1" ]
|
|
}
|
|
|
|
# Choose one of DOMAINS, other than $2 if given; sets PICKED
|
|
pick_core() {
|
|
local choices=() domain choice i
|
|
for domain in "${DOMAINS[@]}"; do
|
|
[ "$domain" != "$2" ] && choices+=("$domain")
|
|
done
|
|
if [ ${#choices[@]} -eq 1 ]; then
|
|
PICKED="${choices[0]}"
|
|
return 0
|
|
fi
|
|
for i in "${!choices[@]}"; do
|
|
printf ' %d) %s\n' $((i + 1)) "${choices[$i]}"
|
|
done
|
|
read -rp "$1 " choice
|
|
[[ "$choice" =~ ^[0-9]+$ ]] && [ "$choice" -ge 1 ] && [ "$choice" -le ${#choices[@]} ] || return 1
|
|
PICKED="${choices[$((choice - 1))]}"
|
|
}
|
|
|
|
# Point Element at the main Core, and say what else follows it
|
|
main_changed() {
|
|
load_core "$MAIN_DOMAIN"
|
|
echo "$MAIN_DOMAIN is now your main Core."
|
|
if element_installed && ! element_done && confirm_yes "Point Element at $MATRIX?"; then
|
|
setup_element
|
|
fi
|
|
[ -f "$TAILSCALE_CONF" ] && echo "To move this Mac to its Tailscale network: configure tailscale"
|
|
return 0
|
|
}
|
|
|
|
# Undo what setup_core did. Nothing on the Core itself changes.
|
|
remove_core() {
|
|
local domain="$1" new_main="" app_password
|
|
if tailscale_on "$domain"; then
|
|
echo "This Mac is on $domain's Tailscale network. Leave it first: configure tailscale"
|
|
return 1
|
|
fi
|
|
if backup_on "$domain"; then
|
|
echo "Your backups go to $domain. Turn them off first: configure backup"
|
|
return 1
|
|
fi
|
|
if [ "$domain" = "$MAIN_DOMAIN" ] && [ ${#DOMAINS[@]} -gt 1 ]; then
|
|
echo "$domain is your main Core. Which Core should be the main one instead?"
|
|
pick_core "Choose:" "$domain" || { echo "Choose a number from the list."; return 1; }
|
|
new_main="$PICKED"
|
|
fi
|
|
echo "This removes $domain's Mail, Calendar, Contacts and Superpanel from this Mac."
|
|
echo "Your email, calendars, contacts and files stay on the Core."
|
|
confirm "Remove $domain?" || return 0
|
|
|
|
load_core "$domain"
|
|
if ! profile_remove "$PROFILE_ID" "$PROFILE_NAME"; then
|
|
echo "The \"$PROFILE_NAME\" profile is still installed, so $domain wasn't removed."
|
|
return 1
|
|
fi
|
|
app_password="$(app_password_load)"
|
|
[ -n "$app_password" ] && nextcloud_revoke_app_password "$app_password"
|
|
app_password_delete
|
|
rm -rf "$SUPERPANEL_APP"
|
|
|
|
# Element keeps its default server, unless that was this Core
|
|
if [ "$domain" = "$MAIN_DOMAIN" ] && [ -z "$new_main" ] && [ -f "$ELEMENT_CONFIG" ] \
|
|
&& element_installed && element_done; then
|
|
json_edit "$ELEMENT_CONFIG" 'delete d.default_server_config;'
|
|
fi
|
|
|
|
if files_done; then
|
|
echo "The Nextcloud app still syncs your files. To stop, click the cloud in the menu bar,"
|
|
echo "then your account ($CORE_USER) > Remove account. Your files stay on the Core."
|
|
fi
|
|
|
|
rm -f "$CORES_DIR/$domain.conf"
|
|
echo "Removed $domain from this Mac."
|
|
if [ -n "$new_main" ]; then
|
|
make_main "$new_main" && main_changed
|
|
elif [ "$domain" = "$MAIN_DOMAIN" ]; then
|
|
rm -f "$CORE_CONF"
|
|
fi
|
|
}
|
|
|
|
# Testing a Core checks each part this Mac set up for it against the Core
|
|
# itself, then offers to fix what doesn't work. A test that fails adds a fix,
|
|
# if there is one, to FIXES (with its title in FIX_TITLES).
|
|
|
|
passed() {
|
|
echo " $1: ${GREEN}working$RESET"
|
|
}
|
|
|
|
# failed <title> <problem> [fix] [more]
|
|
failed() {
|
|
echo " $RED$1: $2$RESET"
|
|
[ -n "$4" ] && echo " $4"
|
|
PROBLEMS=$((PROBLEMS + 1))
|
|
if [ -n "$3" ]; then
|
|
FIXES+=("$3")
|
|
FIX_TITLES+=("$1")
|
|
fi
|
|
}
|
|
|
|
# The profile, and the app password Calendar and Contacts have. Mail's
|
|
# password can't be checked without macOS asking you to let this see it in
|
|
# your keychain.
|
|
test_accounts() {
|
|
local installed svc missing="" app_password
|
|
installed="$(installed_services)"
|
|
if [ -z "$installed" ]; then
|
|
failed "Mail, Calendar and Contacts" "not set up (the profile isn't installed)" fix_accounts
|
|
return
|
|
fi
|
|
for svc in $SERVICES; do
|
|
in_list "$svc" "$installed" || missing="$missing $svc"
|
|
done
|
|
[ -n "$missing" ] && failed "Profile" "doesn't have${missing}" fix_accounts
|
|
in_list mail "$installed" && passed "Mail (the profile is installed)"
|
|
in_list calendar "$installed" || in_list contacts "$installed" || return 0
|
|
app_password="$(app_password_load)"
|
|
if [ -z "$app_password" ]; then
|
|
failed "Calendar and Contacts" "this Mac doesn't have their app password" fix_accounts
|
|
return
|
|
fi
|
|
nextcloud_sign_in "$app_password"
|
|
case $? in
|
|
0) passed "Calendar and Contacts" ;;
|
|
2) failed "Calendar and Contacts" "Nextcloud doesn't accept their app password" fix_accounts \
|
|
"It may have been revoked in Nextcloud under Settings > Security." ;;
|
|
*) failed "Calendar and Contacts" "couldn't check their app password" ;;
|
|
esac
|
|
}
|
|
|
|
test_files() {
|
|
local title="Your Nextcloud files"
|
|
if ! app_installed nextcloud; then
|
|
failed "$title" "the Nextcloud app isn't installed" setup_files
|
|
elif ! files_done; then
|
|
failed "$title" "the Nextcloud app isn't signed in to $NEXTCLOUD" setup_files
|
|
elif ! pgrep -x Nextcloud > /dev/null; then
|
|
failed "$title" "the Nextcloud app isn't open" open_nextcloud
|
|
else
|
|
passed "$title"
|
|
fi
|
|
}
|
|
|
|
test_element() {
|
|
local title="Element with your Core's Matrix server"
|
|
if ! element_done; then
|
|
failed "$title" "Element uses another server" setup_element
|
|
elif curl -sf -o /dev/null --max-time 15 "$MATRIX/_matrix/client/versions"; then
|
|
passed "$title"
|
|
else
|
|
failed "$title" "can't reach $MATRIX"
|
|
fi
|
|
}
|
|
|
|
# Backups have their own app password, in the keychain
|
|
backup_webdav() {
|
|
(source "$BACKUP_CONF" && echo "$BACKUP_WEBDAV")
|
|
}
|
|
|
|
test_backups() {
|
|
nextcloud_sign_in "$(keychain_load "$BACKUP_KEYCHAIN" "nextcloud:$(backup_webdav)")"
|
|
case $? in
|
|
0) passed "Backups" ;;
|
|
2) failed "Backups" "Nextcloud doesn't accept their app password" fix_backups ;;
|
|
*) failed "Backups" "couldn't check their app password" ;;
|
|
esac
|
|
}
|
|
|
|
# Backups get their own new app password, as configure backup gives them
|
|
fix_backups() {
|
|
local APP_PASSWORD webdav
|
|
get_passwords || return 1
|
|
webdav="$(backup_webdav)"
|
|
nextcloud_app_password "Federated Backup on $COMPUTER" || return 1
|
|
keychain_save "$BACKUP_KEYCHAIN" "nextcloud:$webdav" "$APP_PASSWORD" || return 1
|
|
echo "Backups have a new app password."
|
|
}
|
|
|
|
# Test the Core loaded with load_core. Fails if Nextcloud can't be reached,
|
|
# as then nothing else can be tested.
|
|
test_core() {
|
|
local status
|
|
FIXES=()
|
|
FIX_TITLES=()
|
|
PROBLEMS=0
|
|
status="$(curl -sf --max-time 15 "$NEXTCLOUD/status.php")"
|
|
if [ -z "$status" ]; then
|
|
echo " ${RED}Couldn't reach Nextcloud at $NEXTCLOUD.$RESET"
|
|
echo " Check this Mac's internet connection, or whether your Core is down."
|
|
return 1
|
|
fi
|
|
if json_get 'd.maintenance' <<< "$status" > /dev/null; then
|
|
echo " Nextcloud is being updated (maintenance mode). Test again when it's done."
|
|
return 1
|
|
fi
|
|
|
|
if ! superpanel_done; then
|
|
failed "Superpanel in your applications" "not set up" setup_superpanel
|
|
elif curl -s -o /dev/null --max-time 15 "$SUPERPANEL/"; then
|
|
passed "Superpanel"
|
|
else
|
|
failed "Superpanel" "can't reach $SUPERPANEL"
|
|
fi
|
|
test_accounts
|
|
test_files
|
|
[ "$CORE_DOMAIN" = "$MAIN_DOMAIN" ] && element_installed && test_element
|
|
backup_on "$CORE_DOMAIN" && test_backups
|
|
return 0
|
|
}
|
|
|
|
# A new version of the profile with all of Mail, Calendar and Contacts, and
|
|
# Calendar and Contacts' app password, or a new one if Nextcloud doesn't take
|
|
# it any more
|
|
fix_accounts() {
|
|
install_accounts "$SERVICES"
|
|
}
|
|
|
|
open_nextcloud() {
|
|
open -a "$(app_path nextcloud)"
|
|
}
|
|
|
|
# Run the fixes test_core found. Those that need your Core password ask for
|
|
# it once.
|
|
fix_core() {
|
|
local i
|
|
STATUS=0
|
|
for i in "${!FIXES[@]}"; do
|
|
# Each fix is listed once
|
|
[ "$i" -gt 0 ] && in_list "${FIXES[$i]}" "${FIXES[*]:0:$i}" && continue
|
|
echo
|
|
echo "${FIX_TITLES[$i]}:"
|
|
"${FIXES[$i]}" || { echo "$RED${FIX_TITLES[$i]} was not fixed.$RESET"; STATUS=1; }
|
|
done
|
|
forget_passwords
|
|
return $STATUS
|
|
}
|
|
|
|
check_core() {
|
|
load_core "$1"
|
|
echo "Testing $CORE_DOMAIN..."
|
|
test_core || return 1
|
|
if [ $PROBLEMS -eq 0 ]; then
|
|
echo "Everything works."
|
|
return 0
|
|
fi
|
|
echo
|
|
if [ ${#FIXES[@]} -eq 0 ]; then
|
|
echo "This Mac can't fix that; it's on the Core's side, or the network's."
|
|
return 1
|
|
fi
|
|
confirm_yes "Fix what doesn't work?" || return 1
|
|
fix_core
|
|
|
|
echo
|
|
echo "Testing $CORE_DOMAIN again..."
|
|
test_core || return 1
|
|
if [ $PROBLEMS -eq 0 ]; then
|
|
echo "Everything works now."
|
|
else
|
|
echo "Some of it still doesn't work."
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
run_configure() {
|
|
local choice i state domain
|
|
DOMAINS=()
|
|
while IFS= read -r domain; do
|
|
DOMAINS+=("$domain")
|
|
done < <(core_domains)
|
|
if [ ${#DOMAINS[@]} -eq 0 ]; then
|
|
load_core ""
|
|
setup_core
|
|
return
|
|
fi
|
|
|
|
echo "Your Cores:"
|
|
for i in "${!DOMAINS[@]}"; do
|
|
if (load_core "${DOMAINS[$i]}" && core_done); then
|
|
state="${GREEN}done$RESET "
|
|
else
|
|
state="${YELLOW}to do$RESET"
|
|
fi
|
|
printf " %d) %s %s%s\n" $((i + 1)) "$state" "${DOMAINS[$i]}" \
|
|
"$([ "${DOMAINS[$i]}" = "$MAIN_DOMAIN" ] && echo " (main)")"
|
|
done
|
|
echo
|
|
echo " Choose a number to finish or change its setup, or:"
|
|
echo " a) Add a Core"
|
|
[ ${#DOMAINS[@]} -gt 1 ] && echo " m) Make another Core the main one"
|
|
echo " t) Test a Core, and fix what doesn't work"
|
|
echo " p) Your Core password changed"
|
|
echo " r) Remove a Core"
|
|
echo " q) Leave it as it is"
|
|
echo
|
|
read -rp "Choose: " choice
|
|
case "$choice" in
|
|
a|A)
|
|
load_core ""
|
|
setup_core ;;
|
|
m|M)
|
|
[ ${#DOMAINS[@]} -gt 1 ] || return 0
|
|
pick_core "Which should be the main one?" "$MAIN_DOMAIN" || { echo "Choose a number from the list."; return 1; }
|
|
make_main "$PICKED" && main_changed ;;
|
|
p|P)
|
|
pick_core "Which Core's password changed?" || { echo "Choose a number from the list."; return 1; }
|
|
change_password "$PICKED" ;;
|
|
t|T)
|
|
pick_core "Which Core should be tested?" || { echo "Choose a number from the list."; return 1; }
|
|
check_core "$PICKED" ;;
|
|
r|R)
|
|
pick_core "Which Core should be removed?" || { echo "Choose a number from the list."; return 1; }
|
|
remove_core "$PICKED" ;;
|
|
*)
|
|
if [[ "$choice" =~ ^[0-9]+$ ]] && [ "$choice" -ge 1 ] && [ "$choice" -le ${#DOMAINS[@]} ]; then
|
|
load_core "${DOMAINS[$((choice - 1))]}"
|
|
setup_core
|
|
fi ;;
|
|
esac
|
|
}
|