Files
connector-popos/configure.d/10-core.sh
T
davidandClaude Opus 5.5 b609252e1c Pin srt to a tested version; support only Pop!_OS 26.04 and later
srt is pinned to 0.0.83, the version the smoketest passed with. A computer
with another version sees the agents item as to do in configure, which
installs the pinned one. Move the pin only after smoketest/run 50 80 passes.

The installer and configure stop on Pop!_OS (or Ubuntu) older than 26.04.
The smoketest builds on Ubuntu 26.04, with checks that 24.04 is refused.
26.04's own AppArmor profile lets bubblewrap make user namespaces, so the
connector's profile for it, and the code that offered to add it, are gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 20:49:54 -07:00

920 lines
32 KiB
Bash
Executable File

#!/bin/bash
# configure reads TITLE and DESCRIPTION
# shellcheck disable=SC2034
TITLE="Federated Core"
# shellcheck disable=SC2034
DESCRIPTION="Email, calendars, 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, Tailscale and Backup 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"
GOA_ACCOUNT="$CONNECTOR/configure.d/lib/goa-account"
EMAIL_WATCH="$CONNECTOR/system/email-watch"
UNITS="$HOME/.config/systemd/user"
source "$CONNECTOR/configure.d/lib/nextcloud.sh"
source "$CONNECTOR/configure.d/lib/sidebar.sh"
[ -f "$CORE_CONF" ] && source "$CORE_CONF"
MAIN_DOMAIN="$CORE_DOMAIN"
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=""
[ -n "$1" ] && source "$CORES_DIR/$1.conf"
core_names
}
# CORE_DOMAIN, CORE_EMAIL and CORE_NAME come from core.conf; the rest follow
core_names() {
CORE_USER="${CORE_EMAIL%@*}"
NEXTCLOUD="https://nextcloud.$CORE_DOMAIN"
WEBDAV="$NEXTCLOUD/remote.php/dav/files/$CORE_USER"
MOUNT="/mnt/$CORE_DOMAIN"
SUPERPANEL_DESKTOP="$HOME/.local/share/applications/core-$CORE_DOMAIN-superpanel.desktop"
MATRIX="https://matrix.$CORE_DOMAIN"
ELEMENT_CONFIG="$(element_config)"
PLAUSIBLE="plausible.$CORE_DOMAIN"
}
superpanel_done() {
[ -f "$SUPERPANEL_DESKTOP" ]
}
nextcloud_done() {
"$GOA_ACCOUNT" exists owncloud "$CORE_USER@nextcloud.$CORE_DOMAIN" 2> /dev/null
}
email_done() {
"$GOA_ACCOUNT" exists imap_smtp "$CORE_EMAIL" 2> /dev/null
}
files_done() {
grep -qs " $MOUNT davfs " /etc/fstab && grep -qs "^$MOUNT " "$HOME/.davfs2/secrets"
}
# Element from Flathub (as the COSMIC Store installs it), or the deb from
# packages.element.io
ELEMENT_FLATPAK=im.riot.Riot
element_installed() {
flatpak_installed "$ELEMENT_FLATPAK" || command -v element-desktop > /dev/null
}
# Element reads config.json in its own settings folder, which Flatpak keeps
# in the app's folder under ~/.var/app
element_config() {
if flatpak_installed "$ELEMENT_FLATPAK"; then
echo "$HOME/.var/app/$ELEMENT_FLATPAK/config/Element/config.json"
else
echo "$HOME/.config/Element/config.json"
fi
}
# Done when Element isn't installed, as there's nothing to set up
element_done() {
element_installed || return 0
jq -e --arg url "$MATRIX" \
'.default_server_config["m.homeserver"].base_url | rtrimstr("/") == $url' \
"$ELEMENT_CONFIG" &> /dev/null
}
# Tally for Plausible, a Flatpak, shows one Plausible Analytics. It follows the
# main Core, if that runs Plausible.
TALLY=com.cassidyjames.plausible
tally_installed() {
flatpak info "$TALLY" &> /dev/null
}
tally_setting() {
flatpak run --command=gsettings "$TALLY" "$@" 2> /dev/null
}
tally_done() {
[ "$(tally_setting get "$TALLY" domain)" = "'$PLAUSIBLE'" ]
}
plausible_on_core() {
curl -sf -o /dev/null --max-time 15 "https://$PLAUSIBLE/api/health"
}
# Element only follows the main Core
core_done() {
superpanel_done && nextcloud_done && email_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"
} > "$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"
}
desktop_entry() {
local file="$1" name="$2" comment="$3" url="$4" icon="$5"
mkdir -p "$(dirname "$file")"
cat > "$file" << EOF
[Desktop Entry]
Type=Application
Name=$name
Comment=$comment
Exec=xdg-open $url
Icon=$icon
Categories=Network;
EOF
}
setup_superpanel() {
local url="https://superpanel.$CORE_DOMAIN"
local icon="$HOME/.local/share/icons/core-$CORE_DOMAIN-superpanel.png"
# The login page carries Superpanel's icon inline
mkdir -p "$(dirname "$icon")"
if ! curl -s "$url/" | grep -o 'rel="icon"[^>]*base64,[^"]*' | head -n 1 \
| sed 's/.*base64,//' | base64 -d > "$icon" 2> /dev/null || [ ! -s "$icon" ]; then
rm -f "$icon"
icon="applications-internet"
fi
desktop_entry "$SUPERPANEL_DESKTOP" "Superpanel" "Manage your Federated Core ($CORE_DOMAIN)" "$url" "$icon"
echo "Added Superpanel to your applications."
}
# COSMIC has no Online Accounts of its own, so GNOME's is used: it keeps the
# accounts and their passwords (in your keyring, as COSMIC does), and
# Evolution shows your email, calendars and contacts from it. goa-account adds
# accounts through it the way GNOME Settings does.
goa_ready() {
pkg_ensure "Email, calendars and contacts" gnome-online-accounts evolution \
python3-gi gir1.2-secret-1
}
setup_nextcloud() {
[ -n "$APP_PASSWORD" ] || { echo "This needs your Core password, and Nextcloud didn't accept it."; return 1; }
goa_ready || return 1
"$GOA_ACCOUNT" add << EOF || return 1
$(jq -n --arg user "$CORE_USER" --arg host "nextcloud.$CORE_DOMAIN" --arg url "$NEXTCLOUD" \
--arg password "$APP_PASSWORD" '{
provider: "owncloud",
identity: $user,
presentation_identity: "\($user)@\($host)",
credentials: {password: $password},
details: {
Uri: "\($url)/remote.php/webdav",
FilesEnabled: "true",
CalendarEnabled: "true",
CalDavUri: "\($url)/remote.php/dav",
ContactsEnabled: "true",
CardDavUri: "\($url)/remote.php/dav",
AcceptSslErrors: "false"
}}')
EOF
echo "Added Nextcloud's calendars and contacts; Evolution shows them."
}
# The mail server publishes its settings (Thunderbird's autoconfig format).
# Prints: imap_host imap_port imap_socket smtp_host smtp_port smtp_socket username
mail_settings() {
curl -sf --max-time 15 \
"https://autoconfig.$CORE_DOMAIN/mail/config-v1.1.xml?emailaddress=$CORE_EMAIL" \
| python3 -I -c '
import sys, xml.etree.ElementTree as ET
provider = ET.parse(sys.stdin).getroot().find("emailProvider")
imap = provider.find("incomingServer[@type=\"imap\"]")
smtp = provider.find("outgoingServer[@type=\"smtp\"]")
print(*(s.findtext(f) for s in (imap, smtp) for f in ("hostname", "port", "socketType")),
imap.findtext("username"))'
}
# Online Accounts takes host:port, and only the host for the usual port
host_port() {
if [ "$2" = "$3" ]; then echo "$1"; else echo "$1:$2"; fi
}
setup_email() {
[ -n "$PASSWORD" ] || { echo "This needs your Core password."; return 1; }
goa_ready || return 1
local imap_host imap_port imap_socket smtp_host smtp_port smtp_socket username
if ! read -r imap_host imap_port imap_socket smtp_host smtp_port smtp_socket username < <(mail_settings) \
|| [ -z "$smtp_socket" ]; 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
username="%EMAILADDRESS%"
fi
case "$username" in
%EMAILLOCALPART%) username="$CORE_USER" ;;
*) username="$CORE_EMAIL" ;;
esac
[ "$imap_socket" = SSL ] && imap_std=993 || imap_std=143
[ "$smtp_socket" = SSL ] && smtp_std=465 || smtp_std=587
"$GOA_ACCOUNT" add << EOF || return 1
$(jq -n --arg email "$CORE_EMAIL" --arg name "$CORE_NAME" --arg user "$username" \
--arg password "$PASSWORD" \
--arg imap "$(host_port "$imap_host" "$imap_port" "$imap_std")" --arg imap_socket "$imap_socket" \
--arg smtp "$(host_port "$smtp_host" "$smtp_port" "$smtp_std")" --arg smtp_socket "$smtp_socket" '{
provider: "imap_smtp",
identity: $email,
presentation_identity: $email,
credentials: {"imap-password": $password, "smtp-password": $password},
details: {
Enabled: "true",
EmailAddress: $email,
Name: $name,
ImapHost: $imap,
ImapUserName: $user,
ImapUseSsl: (if $imap_socket == "SSL" then "true" else "false" end),
ImapUseTls: (if $imap_socket == "STARTTLS" then "true" else "false" end),
ImapAcceptSslErrors: "false",
SmtpHost: $smtp,
SmtpUseAuth: "true",
SmtpUserName: $user,
SmtpAuthLogin: "false",
SmtpAuthPlain: "true",
SmtpUseSsl: (if $smtp_socket == "SSL" then "true" else "false" end),
SmtpUseTls: (if $smtp_socket == "STARTTLS" then "true" else "false" end),
SmtpAcceptSslErrors: "false"
}}')
EOF
echo "Added $CORE_EMAIL; Evolution shows it."
}
# Save APP_PASSWORD for davfs2 to mount your files with. davfs2 only reads
# this file if no one else can.
save_files_secret() {
local secret
mkdir -p -m 700 "$HOME/.davfs2"
touch "$HOME/.davfs2/secrets"
chmod 600 "$HOME/.davfs2/secrets"
secret="${APP_PASSWORD//\\/\\\\}"
secret="${secret//\"/\\\"}"
sed -i "\\#^$MOUNT #d" "$HOME/.davfs2/secrets"
printf '%s %s "%s"\n' "$MOUNT" "$CORE_USER" "$secret" >> "$HOME/.davfs2/secrets"
}
# davfs2 lets you mount without sudo only if mount.davfs is setuid root, which
# Ubuntu's package asks about when it's installed (no, by default)
davfs2_ready() {
if ! pkg_installed davfs2; then
echo "Mounting your files needs davfs2."
confirm_yes "Install it now (with apt; asks for your password)?" || return 1
echo "davfs2 davfs2/suid_file boolean true" | sudo debconf-set-selections || return 1
pkg_install davfs2 || return 1
fi
if [ ! -u /usr/sbin/mount.davfs ]; then
echo "davfs2 davfs2/suid_file boolean true" | sudo debconf-set-selections \
&& sudo dpkg-reconfigure -f noninteractive davfs2 || return 1
fi
}
# Mount your Nextcloud files at /mnt/<domain> with davfs2. You can mount it
# without sudo once it's in /etc/fstab with "user" and you're in Ubuntu's
# davfs2 group. It's mounted now and each time you log in.
setup_files() {
[ -n "$APP_PASSWORD" ] || { echo "This needs your Core password, and Nextcloud didn't accept it."; return 1; }
davfs2_ready || return 1
sudo install -d -o "$USER" -g "$(id -gn)" "$MOUNT" || return 1
if ! grep -qs " $MOUNT davfs " /etc/fstab; then
echo "$WEBDAV $MOUNT davfs rw,user,noauto,uid=$USER,_netdev 0 0" | sudo tee -a /etc/fstab > /dev/null || return 1
# Otherwise mount warns that systemd is still using the old fstab
sudo systemctl daemon-reload
fi
if ! id -nG "$USER" | grep -qw davfs2; then
sudo usermod -aG davfs2 "$USER" || return 1
fi
save_files_secret || return 1
mkdir -p "$HOME/.config/autostart"
cat > "$HOME/.config/autostart/core-$CORE_DOMAIN-files.desktop" << EOF
[Desktop Entry]
Type=Application
Name=Nextcloud files ($CORE_DOMAIN)
Comment=Mount your Nextcloud files at $MOUNT
Exec=sh -c 'mountpoint -q $MOUNT || mount $MOUNT'
X-GNOME-Autostart-enabled=true
NoDisplay=true
EOF
# Shown in the Files sidebar as the domain
sidebar_add "$MOUNT" "$CORE_DOMAIN"
# A new session through sudo picks up the davfs2 group now, without
# logging out and in again
if ! mountpoint -q "$MOUNT"; then
sudo -u "$USER" mount "$MOUNT" || return 1
fi
echo "Your Nextcloud files are at $MOUNT (\"$CORE_DOMAIN\" in the Files sidebar)."
}
# Element Desktop reads config.json in its settings folder (element_config)
# 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 config
base_url="$(curl -sf --max-time 15 "$MATRIX/.well-known/matrix/client" \
| jq -r '.["m.homeserver"].base_url // empty' 2> /dev/null)"
base_url="${base_url:-$MATRIX}"
mkdir -p "$(dirname "$ELEMENT_CONFIG")"
config="$(cat "$ELEMENT_CONFIG" 2> /dev/null)"
[ -n "$config" ] || config='{}'
jq --arg url "${base_url%/}" --arg name "matrix.$CORE_DOMAIN" \
'.default_server_config = {"m.homeserver": {base_url: $url, server_name: $name}}' \
<<< "$config" > "$ELEMENT_CONFIG.tmp" && mv -f "$ELEMENT_CONFIG.tmp" "$ELEMENT_CONFIG" || return 1
echo "Element will use $MATRIX the next time it starts."
}
# Tally opens the page it showed last, so forget that one too
setup_tally() {
tally_setting set "$TALLY" domain "$PLAUSIBLE" || return 1
tally_setting set "$TALLY" current-url "" || return 1
echo "Tally for Plausible will show https://$PLAUSIBLE the next time it starts. Sign in there."
}
# Pauses email if the mail server stops taking its password (system/email-watch)
email_watch_on() {
mkdir -p "$UNITS" \
&& cp "$CONNECTOR/system/systemd/federated-connector-email-watch.service" "$UNITS/" \
&& systemctl --user daemon-reload \
&& systemctl --user enable -q federated-connector-email-watch.service 2> /dev/null \
&& systemctl --user restart federated-connector-email-watch.service 2> /dev/null
}
# Offer to install Element for the main Core, as the app for its chat
element_offer() {
element_installed && return 0
flatpak_ensure "$ELEMENT_FLATPAK" Element "It's the app for your Core's chat (Matrix)." || return 0
ELEMENT_CONFIG="$(element_config)"
}
# 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 -e "\e[31m$title was not set up.\e[0m"; STATUS=1; }
}
# Ask for your Core password until Nextcloud takes it, and with a name ($1),
# create an app password by that name (APP_PASSWORD). PASSWORD is left empty
# if Nextcloud says it's wrong three times, and kept, unchecked, if Nextcloud
# can't be reached.
ask_password() {
local tries
for _ in 1 2 3; do
read -rsp "Your Core password: " PASSWORD
echo
if [ -n "$1" ]; then nextcloud_app_password "$1"; else nextcloud_sign_in; fi
case $? in
0) return 0 ;;
2) echo "Nextcloud says that password is wrong."; PASSWORD="" ;;
*) echo "Couldn't reach Nextcloud at $NEXTCLOUD; trying the password for email anyway."; return 1 ;;
esac
done
return 1
}
# Set up the Core loaded with load_core, or a new one
setup_core() {
local url
STATUS=0
read -erp "Your Superpanel's address (e.g. https://superpanel.example.com): " \
-i "${CORE_DOMAIN:+https://superpanel.$CORE_DOMAIN}" url
url="${url#*://}"
url="${url%%/*}"
CORE_DOMAIN="${url#superpanel.}"
if [ -z "$CORE_DOMAIN" ] || ! curl -s -o /dev/null --max-time 15 "https://superpanel.$CORE_DOMAIN/"; then
echo "Couldn't reach https://superpanel.$CORE_DOMAIN."
return 1
fi
read -erp "Your Core email: " -i "${CORE_EMAIL:-$USER@$CORE_DOMAIN}" CORE_EMAIL
read -erp "Your name, as shown on email you send: " \
-i "${CORE_NAME:-$(getent passwd "$USER" | cut -d: -f5 | cut -d, -f1)}" CORE_NAME
[[ "$CORE_EMAIL" == *@* ]] || { echo "That isn't an email address."; return 1; }
core_names
save_conf
# Nextcloud, email and files need your Core password; Nextcloud checks it
if ! nextcloud_done || ! email_done || ! files_done; then
ask_password "Pop!_OS on $HOSTNAME"
fi
echo
core_step superpanel "Superpanel in your applications"
core_step nextcloud "Nextcloud calendars and contacts"
core_step email "Email"
core_step files "Nextcloud files at $MOUNT"
[ "$CORE_DOMAIN" = "$MAIN_DOMAIN" ] && element_offer && element_installed \
&& core_step element "Element with your Core's Matrix server"
[ "$CORE_DOMAIN" = "$MAIN_DOMAIN" ] && tally_installed && plausible_on_core \
&& core_step tally "Tally for Plausible with your Core's Plausible"
unset PASSWORD APP_PASSWORD
email_done && email_watch_on
return $STATUS
}
# Check an app password this computer keeps for the Core
app_password_works() {
local title="$1" app_password="$2"
[ -n "$app_password" ] || return 0
case "$(nextcloud_sign_in "$app_password"; echo $?)" in
0) echo " $title: working" ;;
2) echo -e " \e[31m$title: Nextcloud doesn't accept its app password.\e[0m"
echo " It may have been revoked in Nextcloud under Settings > Security."
return 1 ;;
*) echo " $title: couldn't reach Nextcloud to check" ;;
esac
}
# After your Core password changes, e.g. in Superpanel. Email keeps your Core
# password, so it gets the new one. Nextcloud, files and backups 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 tries reached=1
load_core "$1"
STATUS=0
for _ in 1 2 3; do
read -rsp "Your new 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; trying the password for email anyway."
reached=0; break ;;
esac
done
[ -n "$PASSWORD" ] || return 1
echo
if email_done; then
local was_paused=""
"$GOA_ACCOUNT" paused imap_smtp "$CORE_EMAIL" 2> /dev/null && was_paused=", and isn't paused any more"
jq -n --arg password "$PASSWORD" '{"imap-password": $password, "smtp-password": $password}' \
| "$GOA_ACCOUNT" update imap_smtp "$CORE_EMAIL" 2> /dev/null
case $? in
0) echo " Email: has your new password$was_paused" ;;
3) echo -e " \e[31mEmail: saved your new password, but the mail server didn't accept it.\e[0m"
STATUS=1 ;;
*) echo -e " \e[31mEmail: couldn't save your new password.\e[0m"; STATUS=1 ;;
esac
fi
unset PASSWORD
[ $reached = 1 ] || return 1
if nextcloud_done; then
if "$GOA_ACCOUNT" check owncloud "$CORE_USER@nextcloud.$CORE_DOMAIN" 2> /dev/null; then
echo " Nextcloud calendars and contacts: working"
else
echo -e " \e[31mNextcloud calendars and contacts: can't sign in.\e[0m"
echo " To fix it: configure core, then choose t) Test a Core"
STATUS=1
fi
fi
files_done && { app_password_works "Files at $MOUNT" "$(files_app_password)" || STATUS=1; }
app_password_works "Backups" \
"$(secret-tool lookup federated-connector backup item nextcloud url "$WEBDAV" 2> /dev/null)" || STATUS=1
return $STATUS
}
# Tailscale and Backup point at one Core, which can't be removed while they do
tailscale_on() {
[ "$(source "$TAILSCALE_CONF" 2> /dev/null && echo "$TAILSCALE_DOMAIN")" = "$1" ] \
&& tailscale status --json 2> /dev/null | jq -e '.BackendState == "Running"' > /dev/null
}
backup_on() {
[[ "$(source "$BACKUP_CONF" 2> /dev/null && echo "$BACKUP_WEBDAV")" == "https://nextcloud.$1/"* ]] \
&& systemctl --user is-enabled -q federated-connector-backup.timer 2> /dev/null
}
# 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 >= 1 && choice <= ${#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
if tally_installed && ! tally_done && plausible_on_core \
&& confirm_yes "Point Tally for Plausible at https://$PLAUSIBLE?"; then
setup_tally
fi
[ -f "$TAILSCALE_CONF" ] && echo "To move this computer to its Tailscale network: configure tailscale"
[ -f "$BACKUP_CONF" ] && echo "To back up to it instead: configure backup"
return 0
}
# The app password this computer uses for the Core, from davfs2's secrets
files_app_password() {
grep -s "^$MOUNT " "$HOME/.davfs2/secrets" \
| python3 -I -c 'import shlex, sys; print(shlex.split(sys.stdin.read())[2])' 2> /dev/null
}
# Undo what setup_core did. Nothing on the Core itself changes.
remove_core() {
local domain="$1" new_main="" app_password mount_re
if tailscale_on "$domain"; then
echo "This computer 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 Superpanel, Nextcloud, email and files from this computer."
echo "Your files, email and everything else stay on the Core."
confirm "Remove $domain?" || return 0
load_core "$domain"
app_password="$(files_app_password)"
[ -n "$app_password" ] && nextcloud_revoke_app_password "$app_password"
mountpoint -q "$MOUNT" && { umount "$MOUNT" 2> /dev/null || sudo umount "$MOUNT"; }
mount_re="${MOUNT//./\\.}"
if grep -qs " $MOUNT davfs " /etc/fstab; then
sudo sed -i "\\# $mount_re davfs #d" /etc/fstab && sudo systemctl daemon-reload
fi
[ -d "$MOUNT" ] && sudo rmdir "$MOUNT" 2> /dev/null
sed -i "\\#^$mount_re #d" "$HOME/.davfs2/secrets" 2> /dev/null
sidebar_remove "$MOUNT"
rm -f "$HOME/.config/autostart/core-$CORE_DOMAIN-files.desktop"
"$GOA_ACCOUNT" remove owncloud "$CORE_USER@nextcloud.$CORE_DOMAIN"
"$GOA_ACCOUNT" remove imap_smtp "$CORE_EMAIL"
rm -f "$SUPERPANEL_DESKTOP" "$HOME/.local/share/icons/core-$CORE_DOMAIN-superpanel.png"
# Element keeps its default server, unless that was this Core
if [ "$domain" = "$MAIN_DOMAIN" ] && [ -z "$new_main" ] && [ -f "$ELEMENT_CONFIG" ] \
&& jq -e --arg name "matrix.$domain" '.default_server_config["m.homeserver"].server_name == $name' \
"$ELEMENT_CONFIG" &> /dev/null; then
jq 'del(.default_server_config)' "$ELEMENT_CONFIG" > "$ELEMENT_CONFIG.tmp" \
&& mv -f "$ELEMENT_CONFIG.tmp" "$ELEMENT_CONFIG"
fi
rm -f "$CORES_DIR/$domain.conf"
echo "Removed $domain from this computer."
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 computer 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 -e " $1: \e[32mworking\e[0m"
}
# failed <title> <problem> [fix] [more]
failed() {
echo -e " \e[31m$1: $2\e[0m"
[ -n "$4" ] && echo " $4"
PROBLEMS=$((PROBLEMS + 1))
[ -n "$3" ] && FIXES+=("$3") FIX_TITLES+=("$1")
}
# Test an Online Accounts account, and that none of what this set it up
# for is turned off (e.g. with GNOME Settings, if it was ever installed):
# <title> <provider> <identity> <setup> <fix> <enable>
# Email's password is checked with Nextcloud before it's tried on the mail
# server: each wrong password counts towards the mail server blocking this
# computer. Online Accounts can mark an account as needing attention when the
# server can't be reached too, so that isn't
# taken to mean a wrong password.
test_account() {
local error off works=1
if ! "$GOA_ACCOUNT" exists "$2" "$3" 2> /dev/null; then
failed "$1" "not set up" "$4"
return
fi
if "$GOA_ACCOUNT" paused "$2" "$3" 2> /dev/null; then
failed "$1" "paused, as the server stopped taking its password" "$5" \
"So this computer doesn't keep trying it. Did your Core password change?"
works=0
elif [ "$2" = imap_smtp ] && { "$EMAIL_WATCH" --check-password "$3"; [ $? -eq 2 ]; }; then
failed "$1" "has an old password: Nextcloud doesn't take it" "$5" "Did your Core password change?"
works=0
elif ! error="$("$GOA_ACCOUNT" check "$2" "$3" 2>&1)"; then
failed "$1" "can't sign in" "$5" "$error"
works=0
fi
off="$("$GOA_ACCOUNT" disabled "$2" "$3" 2> /dev/null)"
if [ -n "$off" ]; then
failed "$1" "turned off: $off" "$6"
elif [ $works -eq 1 ]; then
passed "$1"
fi
}
test_files() {
local title="Nextcloud files at $MOUNT"
files_done || { failed "$title" "not set up" setup_files; return; }
nextcloud_sign_in "$(files_app_password)"
case $? in
0) if mountpoint -q "$MOUNT" && timeout 15 ls "$MOUNT" &> /dev/null; then
passed "$title"
else
failed "$title" "not mounted" remount_files
fi ;;
2) failed "$title" "Nextcloud doesn't accept its app password" fix_files \
"It may have been revoked in Nextcloud under Settings > Security." ;;
*) failed "$title" "couldn't check its app password" ;;
esac
}
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
}
test_tally() {
local title="Tally for Plausible with your Core's Plausible"
plausible_on_core || return 0
if tally_done; then
passed "$title"
else
failed "$title" "Tally shows another Plausible" setup_tally
fi
}
test_backups() {
local webdav
webdav="$(source "$BACKUP_CONF" && echo "$BACKUP_WEBDAV")"
nextcloud_sign_in "$(secret-tool lookup federated-connector backup item nextcloud url "$webdav" 2> /dev/null)"
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
}
# 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 -e " \e[31mCouldn't reach Nextcloud at $NEXTCLOUD.\e[0m"
echo " Check this computer's internet connection, or whether your Core is down."
return 1
fi
if jq -e '.maintenance' <<< "$status" > /dev/null 2>&1; 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 "https://superpanel.$CORE_DOMAIN/"; then
passed "Superpanel"
else
failed "Superpanel" "can't reach https://superpanel.$CORE_DOMAIN"
fi
test_account "Nextcloud calendars and contacts" owncloud "$CORE_USER@nextcloud.$CORE_DOMAIN" \
setup_nextcloud fix_nextcloud enable_nextcloud
test_account "Email" imap_smtp "$CORE_EMAIL" setup_email fix_email enable_email
test_files
[ "$CORE_DOMAIN" = "$MAIN_DOMAIN" ] && element_installed && test_element
[ "$CORE_DOMAIN" = "$MAIN_DOMAIN" ] && tally_installed && test_tally
backup_on "$CORE_DOMAIN" && test_backups
return 0
}
# Calendars and contacts get a new app password
fix_nextcloud() {
[ -n "$APP_PASSWORD" ] || { echo "This needs your Core password, and Nextcloud didn't accept it."; return 1; }
jq -n --arg password "$APP_PASSWORD" '{password: $password}' \
| "$GOA_ACCOUNT" update owncloud "$CORE_USER@nextcloud.$CORE_DOMAIN" || return 1
echo "Nextcloud calendars and contacts have a new app password."
}
enable_nextcloud() {
"$GOA_ACCOUNT" enable owncloud "$CORE_USER@nextcloud.$CORE_DOMAIN" || return 1
echo "Turned Nextcloud's calendar, contacts and files back on."
}
enable_email() {
"$GOA_ACCOUNT" enable imap_smtp "$CORE_EMAIL" || return 1
echo "Turned email back on."
}
# Email gets your Core password. If the mail server still says no, its
# settings may have changed, so add it again with the server's settings now.
fix_email() {
[ -n "$PASSWORD" ] || { echo "This needs your Core password."; return 1; }
jq -n --arg password "$PASSWORD" '{"imap-password": $password, "smtp-password": $password}' \
| "$GOA_ACCOUNT" update imap_smtp "$CORE_EMAIL" 2> /dev/null
case $? in
0) echo "Email has your Core password." ;;
3) echo "The mail server still says no; adding your email again with its current settings."
"$GOA_ACCOUNT" remove imap_smtp "$CORE_EMAIL" && setup_email ;;
*) return 1 ;;
esac
}
# Unmount if it's stuck, then mount
remount_files() {
if mountpoint -q "$MOUNT"; then
umount "$MOUNT" 2> /dev/null || sudo umount "$MOUNT" || return 1
fi
mount "$MOUNT" 2> /dev/null || sudo -u "$USER" mount "$MOUNT" || return 1
echo "Your Nextcloud files are at $MOUNT."
}
# davfs2 gets a new app password
fix_files() {
[ -n "$APP_PASSWORD" ] || { echo "This needs your Core password, and Nextcloud didn't accept it."; return 1; }
save_files_secret && remount_files
}
# Backups get their own new app password, as configure backup gives them
fix_backups() {
local APP_PASSWORD webdav
[ -n "$PASSWORD" ] || { echo "This needs your Core password."; return 1; }
webdav="$(source "$BACKUP_CONF" && echo "$BACKUP_WEBDAV")"
nextcloud_app_password "Federated Backup on $HOSTNAME" || return 1
printf '%s' "$APP_PASSWORD" | secret-tool store --label "Federated Connector backup nextcloud ($webdav)" \
federated-connector backup item nextcloud url "$webdav" || return 1
echo "Backups have a new app password."
}
# Run the fixes test_core found, asking for your Core password once if they
# need it
fix_core() {
local fix i needs=""
for fix in "${FIXES[@]}"; do
case "$fix" in
setup_nextcloud|fix_nextcloud|setup_files|fix_files) needs=app ;;
setup_email|fix_email|fix_backups) needs="${needs:-password}" ;;
esac
done
case "$needs" in
app) ask_password "Pop!_OS on $HOSTNAME" ;;
password) ask_password ;;
esac
STATUS=0
for i in "${!FIXES[@]}"; do
echo
echo "${FIX_TITLES[$i]}:"
"${FIXES[$i]}" || { echo -e "\e[31m${FIX_TITLES[$i]} was not fixed.\e[0m"; STATUS=1; }
done
unset PASSWORD APP_PASSWORD
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 computer 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
mapfile -t DOMAINS < <(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="\e[32mdone\e[0m "
else
state="\e[33mto do\e[0m"
fi
printf " %d) $state %s%s\n" $((i + 1)) "${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 >= 1 && choice <= ${#DOMAINS[@]} )); then
load_core "${DOMAINS[$((choice - 1))]}"
setup_core
fi ;;
esac
}