configure-stormux/.includes/chuser.sh
2023-08-22 02:57:57 -04:00

66 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
# Configure Stormux
# A script to configure the system for new users.
#
# Copyright 2020, Storm Dragon, <storm_dragon@linux-a11y.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.
#
#--code--
# 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, username."
exit 1
fi
if [[ "$(whoami)" == "root" ]]; then
msgbox "Please run this script as the user you would like to rename, not as root."
exit 1
fi
oldUser="$USER"
groups="$(groups "$oldUser")"
groups="${groups// /,}"
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
# 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
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