From 12e45ded373353631ff08ace49f193caf2b1c201 Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Fri, 25 Apr 2025 13:38:44 -0400 Subject: [PATCH] Updated chuser.sh script. It works differently before, coying over the old user to a new user profile instead of renaming. --- .includes/chuser.sh | 114 +++++++++++++++++++++++++++++++------------- 1 file changed, 80 insertions(+), 34 deletions(-) diff --git a/.includes/chuser.sh b/.includes/chuser.sh index ff5e185..92c7ed4 100755 --- a/.includes/chuser.sh +++ b/.includes/chuser.sh @@ -2,7 +2,7 @@ # Configure Stormux # A script to configure the system for new users. # -# Copyright 2020, Storm Dragon, +# Copyright 2020, 2025, Storm Dragon, # # 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 @@ -18,48 +18,94 @@ # 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. -# -#--code-- +source ./.includes/functions.sh +source ./.includes/ui.sh - # The user can not be logged in when the name change occurs. - # Write a file to /etc/cron.d/chuser - # The file will run at boot, change the username, and delete itself. +if [[ $# -ne 1 ]]; then + msgbox "Missing required argument: new username." + exit 1 +fi - if [[ $# -ne 1 ]]; then - msgbox "Missing required argument, username." - exit 1 +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 - if [[ "$(whoami)" == "root" ]]; then - msgbox "Please run this script as the user you would like to rename, not as root." - exit 1 - fi +infobox "Creating user $newUser..." - oldUser="$USER" +userGroups="$(id -Gn "$oldUser" | tr ' ' ',')" +if ! sudo "${sudoFlags[@]}" useradd -m -G "$userGroups" "$newUser"; then + msgbox "Failed to create user $newUser. Aborting." + exit 1 +fi - groups="$(groups "$oldUser")" - groups="${groups// /,}" +# Set the password +echo "$newUser:$password1" | sudo "${sudoFlags[@]}" chpasswd - newUser="$1" - if ! [[ "$newUser" =~ ^[a-z][-a-z0-9]*$ ]]; then - msgbox "Username $newUser failed validation. It cannot contain spaces or some punctuation." - exit 1 - fi +# 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 -# Heredocument left-aligned -cat << EOF | sudo "${sudoFlags[@]}" tee /etc/cron.d/0chuser &> /dev/null -SHELL=/bin/bash -PATH=/sbin:/bin:/usr/sbin:/usr/bin -@reboot root usermod -a -G $groups -m -d /home/$newUser -l $newUser $oldUser && sed -i -e "s#NODM_USER=.*#NODM_USER='$newUser'#" -e "s#NODM_XSESSION=.*#NODM_XSESSION='/home/$newUser/.xinitrc'#" /etc/nodm.conf; rm -f /etc/cron.d/0chuser;reboot +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 -# Heredocument end. - -# Make sure sound services start for the new user. -mkdir -p /var/lib/systemd/linger -sudo mv -v "/var/lib/systemd/linger/$oldUser" "/var/lib/systemd/linger/$newUser" - -# Files in cron.d must be 644 to work. -sudo "${sudoFlags[@]}" chmod 644 /etc/cron.d/0chuser + # Enable the service to run at boot + sudo systemctl daemon-reload + sudo systemctl enable remove-olduser.service +fi