system/adapters/firstboot.sh

52 lines
2.2 KiB
Bash

[[ $EUID == 0 ]] || { echo 'Run as root' >&2; exit 1; }
[[ -e /run/ostree-booted ]] || { echo 'Not an installed OSTree deployment' >&2; exit 1; }
[[ ! -e /usr/lib/archipelago/gnome-firstboot ]] || { echo 'This deployment uses GNOME Initial Setup through GDM' >&2; exit 1; }
[[ ! -e /usr/lib/archipelago/kde-firstboot ]] || { echo 'This deployment uses Plasma Setup' >&2; exit 1; }
exec 9>/run/archipelago-provision.lock
state=/etc/archipelago
mkdir -p "$state"
echo 'Archipelago Core — first setup'
echo 'Press Enter to begin Core setup on this console.'
# Both console listeners may wait here, but only the selected one mutates accounts.
# Timed reads let the other console yield to getty when setup completes elsewhere.
while [[ ! -f "$state/provisioned" ]]; do
if read -r -t 1; then break; else
result=$?
(( result > 128 )) || exit 1
fi
done
if ! flock -n 9; then
echo 'Setup is active on another console; waiting for it to finish.'
flock 9
fi
if [[ -f "$state/provisioned" ]]; then
user=$(cat "$state/provisioned")
id "$user"
echo 'ARCHIPELAGO_PROVISION_COMPLETE'
exit 0
fi
if [[ -f "$state/provision-user" ]]; then
user=$(cat "$state/provision-user")
echo "Resuming setup for $user"
else
read -r -p 'Username: ' user
[[ $user =~ ^[a-z][a-z0-9_-]{0,30}$ ]] || { echo 'Invalid username'; exit 1; }
! getent passwd "$user" >/dev/null || { echo 'Account already exists'; exit 1; }
[[ ! -e /var/home/$user ]] || { echo 'Home already exists'; exit 1; }
printf '%s\n' "$user" > "$state/provision-user.tmp"
mv "$state/provision-user.tmp" "$state/provision-user"
fi
if ! getent passwd "$user" >/dev/null; then
useradd -m -d "/home/$user" -G wheel -s /bin/bash "$user"
fi
[[ $(getent passwd "$user" | cut -d: -f6) == "/home/$user" ]]
[[ $(stat -c %u "/var/home/$user") == "$(id -u "$user")" ]]
passwd "$user"
install -d -m 0750 /etc/sudoers.d
printf '%%wheel ALL=(ALL:ALL) ALL\n' > /etc/sudoers.d/10-archipelago-wheel
chmod 0440 /etc/sudoers.d/10-archipelago-wheel
visudo -cf /etc/sudoers.d/10-archipelago-wheel
tty > "$state/provision-console"
printf '%s\n' "$user" > "$state/provisioned.tmp"
mv "$state/provisioned.tmp" "$state/provisioned"
echo 'ARCHIPELAGO_PROVISION_COMPLETE'