112 lines
3.7 KiB
Bash
Executable File
112 lines
3.7 KiB
Bash
Executable File
#!/bin/bash
|
|
# Configure Stormux
|
|
# A script to configure the system for new users.
|
|
#
|
|
# Copyright 2020, 2025, Storm Dragon, <storm_dragon@stormux.org>
|
|
#
|
|
# This is free software; you can redistribute it and/or modify it under the
|
|
# terms of the GNU General Public License as published by the Free
|
|
# Software Foundation; either version 3, or (at your option) any later
|
|
# version.
|
|
#
|
|
# This software is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
# General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this package; see the file COPYING. If not, write to the Free
|
|
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
# 02110-1301, USA.
|
|
|
|
source ./.includes/functions.sh
|
|
source ./.includes/ui.sh
|
|
|
|
if [[ $# -ne 1 ]]; then
|
|
msgbox "Missing required argument: new username."
|
|
exit 1
|
|
fi
|
|
|
|
if [[ "$(whoami)" == "root" ]]; then
|
|
msgbox "Please run this script as the user you would like to migrate from, not as root."
|
|
exit 1
|
|
fi
|
|
|
|
newUser="$1"
|
|
oldUser="$USER"
|
|
|
|
if ! [[ "$newUser" =~ ^[a-z][-a-z0-9]*$ ]]; then
|
|
msgbox "Username $newUser failed validation. It cannot contain spaces or special characters."
|
|
exit 1
|
|
fi
|
|
|
|
if id "$newUser" &> /dev/null; then
|
|
msgbox "User $newUser already exists. Aborting."
|
|
exit 1
|
|
fi
|
|
|
|
if [[ "$(yesno "This will create a new user named $newUser and migrate your current settings and files. Continue?")" == "No" ]]; then
|
|
exit 0
|
|
fi
|
|
|
|
while : ; do
|
|
password1=$(passwordbox "Enter a password for $newUser:")
|
|
password2=$(passwordbox "Confirm the password:")
|
|
if [[ "$password1" != "$password2" ]]; then
|
|
msgbox "Passwords do not match. Please try again."
|
|
elif [[ -z "$password1" ]]; then
|
|
msgbox "Password cannot be empty."
|
|
else
|
|
break
|
|
fi
|
|
done
|
|
|
|
infobox "Creating user $newUser..."
|
|
|
|
userGroups="$(id -Gn "$oldUser" | tr ' ' ',')"
|
|
if ! sudo "${sudoFlags[@]}" useradd -m -G "$userGroups" "$newUser"; then
|
|
msgbox "Failed to create user $newUser. Aborting."
|
|
exit 1
|
|
fi
|
|
|
|
# Set the password
|
|
echo "$newUser:$password1" | sudo "${sudoFlags[@]}" chpasswd
|
|
|
|
# Change the active nodm user
|
|
sudo "${sudoFlags[@]}" sed -i -e "s#NODM_USER=.*#NODM_USER='$newUser'#" -e "s#NODM_XSESSION=.*#NODM_XSESSION='/home/$newUser/.xinitrc'#" /etc/nodm.conf
|
|
|
|
infobox "Copying your home directory to /home/$newUser, this may take some time..."
|
|
sudo "${sudoFlags[@]}" cp -a "/home/$oldUser/." "/home/$newUser/"
|
|
sudo "${sudoFlags[@]}" chown -R "$newUser:$newUser" "/home/$newUser"
|
|
|
|
infobox "Updating linger settings..."
|
|
sudo "${sudoFlags[@]}" touch "/var/lib/systemd/linger/$newUser"
|
|
sudo "${sudoFlags[@]}" rm -f "/var/lib/systemd/linger/$oldUser"
|
|
|
|
infobox "Replacing references to $oldUser in config files. This may take some time..."
|
|
sudo "${sudoFlags[@]}" find "/home/$newUser" -type f -exec sed -i "s|$oldUser|$newUser|g" {} +
|
|
|
|
# Optionally remove the old user.
|
|
if [[ "$(yesno "Do you want to delete $oldUser on next reboot?")" == "Yes" ]]; then
|
|
# Dynamically create the systemd service to remove the old user at boot
|
|
cat << EOF | sudo tee /etc/systemd/system/remove-olduser.service > /dev/null
|
|
[Unit]
|
|
Description=Remove old user after renaming
|
|
After=multi-user.target
|
|
|
|
[Service]
|
|
Type=oneshot
|
|
ExecStart=/usr/sbin/userdel -r $oldUser
|
|
ExecStartPost=/bin/systemctl disable remove-olduser.service
|
|
ExecStartPost=/bin/rm -f /etc/systemd/system/remove-olduser.service
|
|
RemainAfterExit=true
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
EOF
|
|
|
|
# Enable the service to run at boot
|
|
sudo systemctl daemon-reload
|
|
sudo systemctl enable remove-olduser.service
|
|
fi
|