V8.00.000.2025.06.17
All checks were successful
🛡️ Shell Script Linting / 🛡️ Shell Script Linting (push) Successful in 1m25s
All checks were successful
🛡️ Shell Script Linting / 🛡️ Shell Script Linting (push) Successful in 1m25s
Signed-off-by: Marc S. Weidner <msw@coresecret.dev>
This commit is contained in:
@@ -12,105 +12,133 @@
|
||||
|
||||
guard_sourcing
|
||||
|
||||
###########################################################################################
|
||||
# Updating user accounts
|
||||
###########################################################################################
|
||||
#######################################
|
||||
# Updating user accounts.
|
||||
# Globals:
|
||||
# TARGET
|
||||
# VAR_USER_MAX
|
||||
# user_root_authentication_access_ssh
|
||||
# user_root_password
|
||||
# user_root_shell
|
||||
# user_root_sshpubkey
|
||||
# Arguments:
|
||||
# None
|
||||
# Returns:
|
||||
# 0: on success
|
||||
#######################################
|
||||
setup_accounts() {
|
||||
|
||||
declare -a ary_user_accounts=()
|
||||
ary_user_accounts+=("")
|
||||
#######################################
|
||||
# Declare Variables
|
||||
#######################################
|
||||
declare -i i
|
||||
declare tmp_username="" tmp_fullname="" tmp_uid="" tmp_gid="" tmp_shell="" tmp_password="" tmp_sshpubkey="" tmp_sudo="" \
|
||||
tmp_restricted=""
|
||||
declare var_username="" var_fullname="" var_uid="" var_gid="" var_shell="" var_password="" var_sshpubkey="" var_sudo="" \
|
||||
var_restricted="" var_chpasswd="" var_sshdir=""
|
||||
|
||||
if [[ ${accounts_root_login,,} == "true" ]]; then
|
||||
|
||||
do_in_target "${TARGET}" /bin/bash -c "echo 'root:${accounts_root_password_crypted}' | chpasswd -e"
|
||||
do_log "info" "false" "Account 'root' password inserted."
|
||||
|
||||
if [[ ! -d ${TARGET}/root/.ssh ]]; then
|
||||
|
||||
mkdir "${TARGET}"/root/.ssh
|
||||
chown root:root "${TARGET}"/root/.ssh
|
||||
chmod 0700 "${TARGET}"/root/.ssh
|
||||
|
||||
else
|
||||
|
||||
chown root:root "${TARGET}"/root/.ssh
|
||||
chmod 0700 "${TARGET}"/root/.ssh
|
||||
|
||||
fi
|
||||
|
||||
if [[ ! -f ${TARGET}/root/.ssh/authorized_keys ]]; then
|
||||
|
||||
touch "${TARGET}"/root/.ssh/authorized_keys
|
||||
chown root:root "${TARGET}"/root/.ssh/authorized_keys
|
||||
chmod 0600 "${TARGET}"/root/.ssh/authorized_keys
|
||||
printf "%s\n" "$accounts_root_ssh_pub_key" >> "${TARGET}"/root/.ssh/authorized_keys
|
||||
do_log "info" "false" "Account 'root' SSH public key '/root/.ssh/authorized_keys' inserted."
|
||||
|
||||
else
|
||||
|
||||
chown root:root "${TARGET}"/root/.ssh/authorized_keys
|
||||
chmod 0600 "${TARGET}"/root/.ssh/authorized_keys
|
||||
printf "%s\n" "$accounts_root_ssh_pub_key" >> "${TARGET}"/root/.ssh/authorized_keys
|
||||
do_log "info" "false" "Account 'root' SSH public key '/root/.ssh/authorized_keys' inserted."
|
||||
|
||||
fi
|
||||
|
||||
|
||||
elif [[ ${accounts_root_login,,} == "false" ]]; then
|
||||
|
||||
do_log "info" "false" "Skipped creation of 'root' password."
|
||||
### Preparing the root account
|
||||
chown root:root "${TARGET}/etc/passwd" "${TARGET}/etc/shadow" "${TARGET}/etc/group" "${TARGET}/etc/gshadow"
|
||||
chmod 644 "${TARGET}/etc/passwd" "${TARGET}/etc/group"
|
||||
chmod 600 "${TARGET}/etc/shadow" "${TARGET}/etc/gshadow"
|
||||
|
||||
if [[ -x "${TARGET}${user_root_shell}" ]]; then
|
||||
do_in_target "${TARGET}" chsh -s "${user_root_shell}" root
|
||||
else
|
||||
|
||||
do_log "error" "true" "Invalid value for 'accounts_root_login': '${accounts_root_login}'. Expected value: 'true' or 'false'."
|
||||
|
||||
do_log "warn" "true" "Shell: '${user_root_shell}' not found for: 'root'. Using '/bin/bash' instead."
|
||||
fi
|
||||
|
||||
if [[ ${accounts_user_login,,} == "true" ]]; then
|
||||
var_chpasswd="root:${user_root_password}"
|
||||
do_in_target_script "${TARGET}" "echo \"${var_chpasswd}\" | chpasswd -e"
|
||||
var_chpasswd=""
|
||||
|
||||
echo "${accounts_user_name}:${accounts_user_password_crypted}" | chpasswd -e
|
||||
do_log "info" "false" "Account '${accounts_user_name}' password inserted."
|
||||
install -d -m 0700 -o root -g root "${TARGET}/root/.ssh"
|
||||
install -m 0600 -o root -g root /dev/null "${TARGET}/root/.ssh/authorized_keys"
|
||||
|
||||
if [[ ! -d ${TARGET}/home/${accounts_user_name}/.ssh ]]; then
|
||||
|
||||
mkdir "${TARGET}"/home/"${accounts_user_name}"/.ssh
|
||||
chown "${accounts_user_name}":"${accounts_user_name}" "${TARGET}"/home/"${accounts_user_name}"/.ssh
|
||||
chmod 0700 "${TARGET}"/home/"${accounts_user_name}"/.ssh
|
||||
grep -qxF "${user_root_sshpubkey}" "${TARGET}/root/.ssh/authorized_keys" || \
|
||||
printf "%s\n" "${user_root_sshpubkey}" >> "${TARGET}/root/.ssh/authorized_keys"
|
||||
|
||||
if [[ "${user_root_authentication_access_ssh}" == "false" ]]; then
|
||||
if grep -q '^\s*PermitRootLogin' "${TARGET}/etc/ssh/sshd_config"; then
|
||||
sed -i 's/^\s*PermitRootLogin\s\+.*/PermitRootLogin no/' "${TARGET}/etc/ssh/sshd_config"
|
||||
else
|
||||
|
||||
chown "${accounts_user_name}":"${accounts_user_name}" "${TARGET}"/home/"${accounts_user_name}"/.ssh
|
||||
chmod 0700 "${TARGET}"/home/"${accounts_user_name}"/.ssh
|
||||
|
||||
echo 'PermitRootLogin no' >> "${TARGET}/etc/ssh/sshd_config"
|
||||
fi
|
||||
|
||||
if [[ ! -f ${TARGET}/home/${accounts_user_name}/.ssh/authorized_keys ]]; then
|
||||
|
||||
touch "${TARGET}"/home/"${accounts_user_name}"/.ssh/authorized_keys
|
||||
chown "${accounts_user_name}":"${accounts_user_name}" "${TARGET}"/home/"${accounts_user_name}"/.ssh/authorized_keys
|
||||
chmod 0600 "${TARGET}"/home/"${accounts_user_name}"/.ssh/authorized_keys
|
||||
printf "%s\n" "$accounts_user_ssh_pub_key" >> "${TARGET}"/home/"${accounts_user_name}"/.ssh/authorized_keys
|
||||
do_log "info" "false" "Account '${accounts_user_name}' SSH public key '${TARGET}/home/${accounts_user_name}/.ssh/authorized_keys' inserted."
|
||||
|
||||
else
|
||||
|
||||
chown "${accounts_user_name}":"${accounts_user_name}" "${TARGET}"/home/"${accounts_user_name}"/.ssh/authorized_keys
|
||||
chmod 0600 "${TARGET}"/home/"${accounts_user_name}"/.ssh/authorized_keys
|
||||
printf "%s\n" "$accounts_user_ssh_pub_key" >> "${TARGET}"/home/"${accounts_user_name}"/.ssh/authorized_keys
|
||||
do_log "info" "false" "Account '${accounts_user_name}' SSH public key '${TARGET}/home/${accounts_user_name}/.ssh/authorized_keys' inserted."
|
||||
|
||||
fi
|
||||
|
||||
elif [[ ${accounts_user_login,,} == "false" ]]; then
|
||||
|
||||
do_log "info" "false" "Skipped creation of account '${accounts_user_name}'."
|
||||
|
||||
else
|
||||
|
||||
do_log "error" "true" "Invalid value for 'accounts_user_login': '${accounts_user_login}'. Expected 'true' or 'false'."
|
||||
|
||||
fi
|
||||
|
||||
### Install all user accounts.
|
||||
for ((i = 0; i <= VAR_USER_MAX; i++)); do
|
||||
tmp_username="user_user${i}_name"
|
||||
tmp_fullname="user_user${i}_fullname"
|
||||
tmp_uid="user_user${i}_uid"
|
||||
tmp_gid="user_user${i}_gid"
|
||||
tmp_shell="user_user${i}_shell"
|
||||
tmp_password="user_user${i}_password"
|
||||
tmp_sshpubkey="user_user${i}_sshpubkey"
|
||||
tmp_sudo="user_user${i}_privileges_sudo"
|
||||
tmp_restricted="user_user${i}_privileges_restricted"
|
||||
|
||||
var_username="${!tmp_username}"
|
||||
var_fullname="${!tmp_fullname}"
|
||||
var_uid="${!tmp_uid}"
|
||||
var_gid="${!tmp_gid}"
|
||||
var_shell="${!tmp_shell}"
|
||||
var_password="${!tmp_password}"
|
||||
var_sshpubkey="${!tmp_sshpubkey}"
|
||||
var_sudo="${!tmp_sudo}"
|
||||
var_restricted="${!tmp_restricted}"
|
||||
|
||||
do_in_target "${TARGET}" getent group "${var_username}" >/dev/null || \
|
||||
do_in_target "${TARGET}" groupadd --gid "${var_gid}" "${var_username}"
|
||||
|
||||
if [[ "${var_restricted}" == "false" ]]; then
|
||||
|
||||
do_in_target "${TARGET}" useradd \
|
||||
--comment "${var_fullname}" \
|
||||
--create-home \
|
||||
--expiredate 2102-12-31 \
|
||||
--gid "${var_gid}" \
|
||||
--home-dir /home/"${var_username}" \
|
||||
--inactive 0 \
|
||||
--shell "${var_shell}" \
|
||||
--uid "${var_uid}" \
|
||||
"${var_username}"
|
||||
|
||||
else
|
||||
|
||||
do_in_target "${TARGET}" useradd \
|
||||
--comment "${var_fullname}" \
|
||||
--expiredate 2102-12-31 \
|
||||
--gid "${var_gid}" \
|
||||
--home-dir /home/"${var_username}" \
|
||||
--inactive 0 \
|
||||
--no-create-home \
|
||||
--shell "${var_shell}" \
|
||||
--uid "${var_uid}" \
|
||||
"${var_username}"
|
||||
|
||||
fi
|
||||
|
||||
var_chpasswd="${var_username}:${var_password}"
|
||||
do_in_target_script "${TARGET}" "echo \"${var_chpasswd}\" | chpasswd -e"
|
||||
var_chpasswd=""
|
||||
|
||||
if [[ "${var_sudo}" == "true" ]]; then
|
||||
do_in_target "${TARGET}" usermod -aG sudo "${var_username}"
|
||||
fi
|
||||
|
||||
if [[ -n "${var_sshpubkey}" ]]; then
|
||||
var_sshdir="${TARGET}/home/${var_username}/.ssh"
|
||||
install -d -m 0700 -o "${var_username}" -g "${var_username}" "${var_sshdir}"
|
||||
install -m 0600 -o "${var_username}" -g "${var_username}" /dev/null "${var_sshdir}/authorized_keys"
|
||||
grep -qxF "${var_sshpubkey}" "${var_sshdir}/authorized_keys" || \
|
||||
printf "%s\n" "${var_sshpubkey}" >> "${var_sshdir}/authorized_keys"
|
||||
fi
|
||||
|
||||
do_log "info" "true" "Created user: [${var_username}] UID: [${var_uid}], GID: [${var_gid}]"
|
||||
|
||||
done
|
||||
|
||||
return 0
|
||||
}
|
||||
# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh
|
||||
|
||||
Reference in New Issue
Block a user