made user manipulation functions usable for a list of users

This commit is contained in:
2025-08-11 20:03:46 +02:00
parent 7319aec087
commit bb53e99894

View File

@@ -2,7 +2,7 @@
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Users related functions # Users related functions
# This file is part of the init.sh project # This file is part of the init.sh project
# Copyright (c) 2019-2024 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org> # Copyright (c) 2019-2025 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# This file is distributed under 3-clause BSD license. # This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at: # The complete license agreement can be obtained at:
@@ -14,21 +14,24 @@
# Users (from Ldap) # Users (from Ldap)
add_remote_user() add_remote_user()
{ {
if [[ -n $(grep "^$1:" /etc/passwd) ]]; then local users=$@
prnt W "A local user with name $1 already exists, adding anyway!" for usr in ${users[@]}; do
fi if [[ -n $(grep "^$usr:" /etc/passwd) ]]; then
if [[ -n $(grep "^+$1:" /etc/passwd) ]]; then prnt W "A local user with name $usr already exists, adding anyway!"
prnt W "The remote user $1 is already declared, nothing to do in passwd." fi
else if [[ -n $(grep "^+$usr:" /etc/passwd) ]]; then
echo "+$1::::::" >> /etc/passwd prnt W "The remote user $usr is already declared, nothing to do in passwd."
prnt I "User $1 added to passwd..." else
fi echo "+$usr::::::" >> /etc/passwd
if [[ -n $(grep "^+$1:" /etc/shadow) ]]; then prnt I "User $usr added to passwd..."
prnt W "The remote user $1 is already connectable, nothing to do in shadow." fi
else if [[ -n $(grep "^+$usr:" /etc/shadow) ]]; then
echo "+$1::::::::" >> /etc/shadow prnt W "The remote user $usr is already connectable, nothing to do in shadow."
prnt I "User $1 added to shadow..." else
fi echo "+$usr::::::::" >> /etc/shadow
prnt I "User $usr added to shadow..."
fi
done
} }
export -f add_remote_user export -f add_remote_user
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@@ -38,15 +41,18 @@ export -f add_remote_user
# Remove users # Remove users
remove_user() remove_user()
{ {
if [[ -n $(grep "^$1:" /etc/{passwd,shadow,group,gshadow}) ]]; then local users=$@
# Using sed is more universal than any distro commands - local case for usr in ${users[@]}; do
sed -i -e "/^$1:/d" /etc/{passwd,shadow,group,gshadow} if [[ -n $(grep "^$usr:" /etc/{passwd,shadow,group,gshadow}) ]]; then
elif [[ -n $(grep "^+$1:" /etc/{passwd,shadow,group,gshadow}) ]]; then # Using sed is more universal than any distro commands - local case
# remote case sed -i -e "/^$usr:/d" /etc/{passwd,shadow,group,gshadow}
sed -i -e "/^+$1:/d" /etc/{passwd,shadow,group,gshadow} elif [[ -n $(grep "^+$usr:" /etc/{passwd,shadow,group,gshadow}) ]]; then
else # remote case
prnt W "User $1 don't exists in auth files, nothing to do." sed -i -e "/^+$usr:/d" /etc/{passwd,shadow,group,gshadow}
fi else
prnt W "User $usr don't exists in auth files, nothing to do."
fi
done
} }
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@@ -55,17 +61,21 @@ remove_user()
# Create a local user # Create a local user
create_local_user() create_local_user()
{ {
if [[ $(noerror --noout id $1) != 0 ]]; then local users=$@
prnt I "Creating user $1..." for usr in ${users[@]}; do
if [[ $(directory_exists home_skell) ]]; then if [[ $(noerror --noout id $usr) != 0 ]]; then
useradd --create-home --shell $DEFAULT_SHELL --user-group $1 \ prnt I "Creating user $usr..."
--skell $(select_directory home_skell) if [[ $(directory_exists home_skell) ]]; then
useradd --create-home --shell $DEFAULT_SHELL \
--user-group $usr \
--skell $(select_directory home_skell)
else
useradd --create-home --shell $DEFAULT_SHELL --user-group $usr
fi
else else
useradd --create-home --shell $DEFAULT_SHELL --user-group $1 prnt W "The user $usr already exists. Nothing to do..."
fi fi
else done
prnt W "The user $1 already exists. Nothing to do..."
fi
} }
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------