From c0ff8d3061a8f5589d067a5deebf8fe2d88ebe7d Mon Sep 17 00:00:00 2001 From: Christoph Date: Mon, 20 Dec 2021 13:04:28 +0100 Subject: [PATCH] add_new_user.sh: add user 'marcus'. some other changes.. --- add_new_user.sh | 127 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 117 insertions(+), 10 deletions(-) diff --git a/add_new_user.sh b/add_new_user.sh index f8ab3fa..10ee2e1 100755 --- a/add_new_user.sh +++ b/add_new_user.sh @@ -1,7 +1,86 @@ #!/usr/bin/env bash +script_name="$(basename $(realpath $0))" working_dir="$(dirname $(realpath $0))" +conf_file="${working_dir}/conf/${script_name%%.*}.conf" + +LOCK_DIR="/tmp/$(basename $0).$$.LOCK" +log_file="${LOCK_DIR}/${script_name%%.*}.log" + + + +# ------------- +# --- Some Variables +# ------------- + +replace_files=".bashrc .bash_logout .profile .vimrc" + +templates_base_dir="${working_dir}/supported-files/user_templates" +templates_all_dir="${templates_base_dir}/all" + +sudo_users=" + sysadm + localadmin + chris + wadmin + ilker + ckubu + alex + alis + defa + init + marcus +" + + + +# ------------- +# --- Some functions +# ------------- + +clean_up() { + + # Perform program exit housekeeping + rm -rf "$LOCK_DIR" + blank_line + exit $1 +} + +blank_line() { + if $terminal ; then + echo "" + fi +} + +# - Check if a given array (parameter 2) contains a given string (parameter 1) +# - +containsElement () { + local e + for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done + return 1 +} + + + +# ---------- +# - Jobhandling +# ---------- + +# - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM +# - +trap clean_up SIGHUP SIGINT SIGTERM + +# - Create lock directory '$LOCK_DIR" +# +mkdir "$LOCK_DIR" + + + +# ---------- +# - Some checks .. +# ---------- + if [[ $# -lt 1 ]] ; then echo "" echo " No username given on comand line." @@ -9,9 +88,21 @@ if [[ $# -lt 1 ]] ; then exit fi +# - Running in a terminal? +# - +if [[ -t 1 ]] ; then + terminal=true +else + terminal=false +fi + + + +# ---------- +# - Some pre-script tasks .. +# ---------- + user_name=$1 -#password=$2 -#password=${password:="test100"} if [[ -z "$2" ]]; then password_needed=true @@ -21,11 +112,6 @@ else password=$2 fi -replace_files=".bashrc .bash_logout .profile .vimrc" - -templates_base_dir="${working_dir}/supported-files/user_templates" -templates_all_dir="${templates_base_dir}/all" - if [[ -f "/etc/adduser.conf" ]]; then . /etc/adduser.conf home_base_dir="$DHOME" @@ -42,6 +128,17 @@ if [[ $min_uid -lt 1001 ]] ; then min_uid=1001 fi +# array of authorized sudo users +# +declare -a sudo_users_arr +for _val in $sudo_users ; do + sudo_users_arr+=("$_val") +done + + +# ========== +# - Begin Main Script +# ========== # - Create user if not exists # - @@ -211,6 +308,12 @@ if $password_needed ; then elif [[ "$user_name" = 'wadmin' ]]; then perl -i -n -p -e "s#^(${user_name}):[^:]+:(.+)#\1:\\\$6\\\$vaVNOPYp\\\$RbV6/phKDZRSa4TQGlJVOF/HQqyxAK7BeQmir8Kr8pwy7ATlHGvcPzJkuldNNp4O4t2q3EJRW2uVpjsng4ZhH1:\2#" /etc/shadow + + # - marcus Opferperspektive + # - + elif [[ "$user_name" = 'marcus' ]]; then + perl -i -n -p -e "s#^(${user_name}):[^:]+:(.+)#\1:\\\$6\\\$SPnbZ2Gcf.4iHz93\\\$Vp7fg/xCGls4cPJIiLR5Ogxm2U38RPpPgHLaLIJiOe.PekJCuRsvRLBZLITec2JQhdVTvqpmKURUZrKdXs62z0:\2#" /etc/shadow + # - root # - elif [[ "$user_name" = 'root' ]]; then @@ -296,9 +399,13 @@ fi chmod 700 ${home_dir}/.ssh chown -R ${user_name}:${user_name} ${home_dir}/.ssh -if [[ "$user_name" = 'sysadm' ]] || [[ "$user_name" = 'localadmin' ]] || [[ "$user_name" = 'chris' ]] || [[ "$user_name" = 'wadmin' ]] || [[ "$user_name" = 'ilker' ]] || [[ "$user_name" = 'ckubu' ]] || [[ "$user_name" = 'alex' ]] || [[ "$user_name" = 'alis' ]] || [[ "$user_name" = 'defa' ]] || [[ "$user_name" = 'init' ]] ; then - usermod -a -G sudo ${user_name} -fi + +# Add authorized users to the sudoers group +# +for __user in ${sudo_users_arr[@]} ; do + usermod -a -G sudo ${__user} +done + #ls -al ${home_dir}/.ssh echo "" echo -e " User \033[1m${user_name}\033[m added:"