shell-base/modules/utility/user.lib

272 lines
7.5 KiB
Plaintext
Raw Permalink Normal View History

#!/bin/bash
user_id () {
local id;local user
echo -n $(id -u ${1:-$USER} 2> /dev/null)
}
user_name () {
echo -n $(id -un -- ${1:-$USER} 2> /dev/null)
}
is_user () {
user_id $1
}
change_user_password () {
local user=${1:-$USER}
[[ ! $(is_user $user) ]] && echo $user is not a system user && return 1
module_load confirm
confirm you are about the change the password for $user, continue? && sudo /bin/passwd $user
}
#group name plus optional username or id
is_user_group () {
# echo id -nG ${2:-$USER}
# id -nG ${2:-$USER} | grep -w "sudo"
if id -nG ${2:-$USER} | grep -qw "$1"; then ${2:-$USER} in echo $1; fi
}
is_user_sudo () {
is_user_group sudo $1
is_user_group wheel $1
}
distro_sudo_group () {
if getent group | grep sudo &> /dev/null ; then
echo sudo
return 0
elif getent group | grep wheel &> /dev/null; then
echo wheel
return 0
else
return 1
fi
}
user_group_add() {
[[ ! $(getent group $1) ]] && echo "no group $1, aborting" && return 1
echo adding group $1 to $USER
sudo usermod -a -G $1 $USER
}
user_group_id_match () {
module_load confirm
local user=${1:-$USER}
local gid=$(id -g $user)
local uid=$(id -u $user)
[[ $uid -eq $gid ]] && echo "ids match nothing to do" && return 0
[[ ! $user == $(id -gn $user) ]] && echo user $user default group name is NOT the same name && return 1
echo DANGER ZONE! changing current default group id for $user from $gid to $uid
if confirm -s continue; then
sudo /bin/find /home/$user -gid $gid -exec chgrp -v $uid '{}' \;
sudo groupmod -g $uid $user
sudo usermod -g $uid $user
echo "only /home/$user directroy group was changed to $uid"
echo "may have to change group from $gid to $uid elsewhere, e.g., sudo chown -R :$user <some dir>"
fi
}
# TODO change a user name
# sudo usermod -d /home/joedoe -m johndoe
# sudo groupmod -n johndoe student1
# sudo usermod -u 5001 johndoe
# clone a user
# adduserid name id#
# add a suer with specific id number
# adduserid () {
# sudo groupadd -g $2 $1
# sudo useradd -d ${/home/$1} -s /bin/bash -u $2 -g $1 $1
# }
adduserid() {
[[ "$#" -lt 1 ]] && echo a user name is requied, aborting && return 1
name=$1
uid=${2:-1000}
gid=${3:-$uid}
echo $name, $uid, $gid
[[ $(getent group $gid) ]] && echo group id $gid already exists, aborting && return 3
[[ $(getent group $name) ]] && echo group name $name already exists, aborting && return 3
[[ $(getent passwd $uid) ]] && echo user id $uid already exists, aborting && return 2
[[ $(getent passwd $name) ]] && echo user name $name already exists, aborting && return 2
# todo use useradd and groupadd
addgroup --gid $gid $name
adduser --uid $uid --ingroup $name --gecos "" --disabled-password --no-create-home $name
# cat /etc/group | grep $name && cat /etc/passwd | grep $name
}
add_recovery_user () {
user=${1:-recovery}
if [[ $(is_user 2000) ]] ; then
echo "a recovery user with uid of 2000 already exists"
else
sudo useradd -u 2000 -N -s /bin/bash -G $(distro_sudo_group) $user
[[ $? -gt 0 ]] && echo error creating recovery user: $user
echo setting the password for recovery user: $user
sudo passwd recovery
fi
}
function clone_user_ () {
2022-03-28 10:02:17 -07:00
module_load confirm
echo "============="
echo "this script will create a new user"
echo "based on an existing user's data"
echo
echo "You will be shown a list of users who can currently log on"
echo "Remember which user you would like to clone."
echo "You will be asked for the new user's name, their password"
echo "and the old user to clone".
echo "============="
echo
echo -n "New user's name: "
read newuser
echo -n "New user's password: "
read newpassword
echo
echo "Current users you can clone:"
echo "----"
awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd
echo
echo -n "Old user to clone: "
read olduser
echo "olduser uid is $(id -u $olduser)"
echo
echo "You have selected: "
echo "----"
echo "new user: $newuser"
echo "new user password: $newpassword"
echo "old user: $olduser"
echo
olduser_GROUPS="$(id -Gn ${olduser} | sed "s/${olduser} //g" | sed "s/ ${olduser}//g" | sed "s/ /,/g"),$olduser"
olduser_SHELL=$(awk -F : -v name=${olduser} '(name == $1) { print $7 }' /etc/passwd)
echo "old user groups: "
echo "----"
echo $olduser_GROUPS
echo "olduser shell: "
echo $olduser_SHELL
confirm "ready to clone user, begin?" || return 1
useradd --groups $olduser_GROUPS --shell $olduser_SHELL $newuser
echo $newuser:$newpassword | chpasswd
read -rsp $'ready to make home direcoty -- ctrl-c to exit...\n' -n1 key
mkdir /home/$newuser
chown -R $newuser:$newuser /home/$newuser
echo
echo "Script should be done now."
echo
echo "Do you see your new users name below?"
echo
awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd
echo
echo "We are now going to copy the old user's home folder to the new user"
echo "then change ownership to the new user"
echo
read -rsp $'Ready to copy home folder --- or ctrl-c to exit...\n' -n1 key
rsync -aPv --exclude 'Downloads' /home/$olduser/. /home/$newuser/
chown -R --from=$olduser $newuser:$newuser /home/$newuser
echo
echo "Now we are going to change the names of files and folders to the new user"
echo
grep -rlI $olduser /home/$newuser/ . | sudo xargs sed -i 's/$olduser/$newuser/g'
echo
echo "Done now."
echo
read -rsp $'Press any key to exit...\n' -n1 key
echo
echo
}
clone_user () {
2022-03-28 10:02:17 -07:00
if [[ $EUID != 0 ]]; then
clone=$(declare -f clone_user_)
module_load confirm
confirm=$(declare -f confirm)
sudo bash -c "$confirm; $clone; clone_user_"
else
echo run as regular user with sudo privliges and it will elevate
fi
}
rename_user_ () {
2022-03-28 10:02:17 -07:00
module_load confirm
local force; local name; local newname; local newhome
[[ $1 = "-f" ]] && { force=true; shift 1; }
[[ $1 = "-h" ]] && { newhome=$2; shift 2; }
# usage: < -f, -h newhome > name newname
# default new user home is /home/newname
name=$1
newname=$2
if [[ ! ($name && $newname) ]]; then
echo "============="
echo "this script will rename an existing user"
echo "user running this script must to root or have sudo priviledges to run"
echo "---- Available Users to Rename ---"
awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd
echo "============="
read -r -p "Enter an available user name: " name
read -r -p "Enter users new name: " newname
read -r -p "Enter users new home directory <enter for default /home/$newname>: " newhome
fi
newhome=${newhome:-/home/$newname}
if [[ ! $force ]]; then
echo "Changing $name to $newname with home $newhome"
echo sudo usermod -l $newname $name
echo sudo groupmod -n $newname $name
echo sudo usermod --d $newhome --m $name
confirm -s "These are the commands that will be run. Do you want to continue?" || return 1
fi
sudo usermod -l $newname $name
echo sudo groupmod -n $newname $name
echo sudo usermod --d $newhome --m $name
}
rename_user () {
if [[ $EUID != 0 ]]; then
rename=$(declare -f rename_user_)
module_load confirm
confirm=$(declare -f confirm)
sudo bash -c "$confirm; $rename; rename_user_"
else
echo run as regular user with sudo privliges and it will elevate
fi
}