#!/bin/bash # ------------------------------------------------------------------------------ # Users related functions # This file is part of the init.sh project # Copyright (c) 2019-2025 Geoffray Levasseur # ------------------------------------------------------------------------------ # This file is distributed under 3-clause BSD license. # The complete license agreement can be obtained at: # https://opensource.org/licenses/BSD-3-Clause # ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ # Users (from Ldap) add_remote_user() { local users=$@ for usr in ${users[@]}; do if [[ -n $(grep "^$usr:" /etc/passwd) ]]; then prnt W "A local user with name $usr already exists, adding anyway!" fi if [[ -n $(grep "^+$usr:" /etc/passwd) ]]; then prnt W "The remote user $usr is already declared, nothing to do in passwd." else echo "+$usr::::::" >> /etc/passwd prnt I "User $usr added to passwd..." fi if [[ -n $(grep "^+$usr:" /etc/shadow) ]]; then prnt W "The remote user $usr is already connectable, nothing to do in shadow." else echo "+$usr::::::::" >> /etc/shadow prnt I "User $usr added to shadow..." fi done } export -f add_remote_user # ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ # Remove users remove_user() { local users=$@ for usr in ${users[@]}; do if [[ -n $(grep "^$usr:" /etc/{passwd,shadow,group,gshadow}) ]]; then # Using sed is more universal than any distro commands - local case sed -i -e "/^$usr:/d" /etc/{passwd,shadow,group,gshadow} elif [[ -n $(grep "^+$usr:" /etc/{passwd,shadow,group,gshadow}) ]]; then # remote case sed -i -e "/^+$usr:/d" /etc/{passwd,shadow,group,gshadow} else prnt W "User $usr don't exists in auth files, nothing to do." fi done } # ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ # Create a local user create_local_user() { local users=$@ for usr in ${users[@]}; do if [[ $(noerror --noout id $usr) != 0 ]]; then prnt I "Creating user $usr..." 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 prnt W "The user $usr already exists. Nothing to do..." fi done } # ------------------------------------------------------------------------------ # EOF