#!/usr/bin/env bash working_dir="$(dirname $(realpath $0))" if [[ $# -lt 1 ]] ; then echo "" echo " No username given on comand line." echo "" exit fi user_name=$1 #password=$2 #password=${password:="test100"} if [[ -z "$2" ]]; then password_needed=true password="test100" else password_needed=false 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" min_uid=$FIRST_UID skel_dir="$SKEL" else home_base_dir=/home min_uid=1000 skel_dir=/etc/skel fi # - Create user if not exists # - if ! id -u "${user_name}" > /dev/null 2>&1 ; then # - Get uid/gid # - if [[ "$user_name" = 'sysadm' ]]; then user_uid=1050 user_gid=1050 elif [[ "$user_name" = 'back' ]]; then user_uid=1060 user_gid=1060 else declare -i _id=$(expr $min_uid - 1) while true ; do ((_id++)) $(id $_id > /dev/null 2>&1) && continue $(cat /etc/group | cut -d ':' -f3 | grep -q $_id 2> /dev/null) && continue break done user_uid=$_id user_gid=$_id fi # - Add user to system # - echo "$user_name:${password}:${user_uid}:${user_gid}:${user_name},,,:${home_base_dir}/${user_name}:/bin/bash" | newusers if [[ -d "$skel_dir" ]]; then cp -a "${skel_dir}/.bash_logout" "${home_base_dir}/${user_name}/" chown ${user_name}:${user_name} "${home_base_dir}/${user_name}/.bash_logout" cp -a "${skel_dir}/.bashrc" "${home_base_dir}/${user_name}/" chown ${user_name}:${user_name} "${home_base_dir}/${user_name}/.bashrc" cp -a "${skel_dir}/.profile" "${home_base_dir}/${user_name}/" chown ${user_name}:${user_name} "${home_base_dir}/${user_name}/.profile" fi fi # - Set new password # - if $password_needed ; then # - sysadm # - if [[ "$user_name" = 'sysadm' ]]; then perl -i -n -p -e "s#^(${user_name}):[^:]+:(.+)#\1:\\\$6\\\$vvccwrTc\\\$Sz1HaSb3ujObprltiG7D6U1Rr3fpgfjkKuDDWYdHzPkPx/0pEofCWC.vyTn78hcemkntl.6wVUOnJnNloKt/E/:\2#" /etc/shadow # - chris # - elif [[ "$user_name" = 'chris' ]]; then perl -i -n -p -e "s#^(${user_name}):[^:]+:(.+)#\1:\\\$6\\\$5SJpnMml\\\$SKedIm8KbDoG6PPwmoq9jbg4Y5pukPp1lzq9AOgugLGSHMQo3tL6Ih/YvX295mL2svhYi4emf.xZV9t95.72d0:\2#" /etc/shadow # - root # - elif [[ "$user_name" = 'root' ]]; then perl -i -n -p -e "s#^(${user_name}):[^:]+:(.+)#\1:\\\$6\\\$JO92p9nG\\\$r/DiHWgK2dZZkSCDDNeLRQwH3Etp0XZeJY9VfX3Tbka0JOXuqWsXkn98V.pCf5BarZjQ4gND.rLs1ARJ7p/P40:\2#" /etc/shadow # - Other normal users (uid > $min_uid) # - elif [[ $user_uid -ge $min_uid ]] ; then perl -i -n -p -e "s#^(${user_name}):[^:]+:(.+)#\1:\\\$6\\\$R6DYYcau\\\$TZYgG122V1oDl1wddNb8ieQIUMab35xItyFlnlStkEBV3DS5S6x7co6S/FpwbHzX21c1HF3WCUQ5/5s.umi6I.:\2#" /etc/shadow fi fi home_dir="$(cat /etc/passwd | grep ${user_name} | cut -d':' -f6)" # - Backup existing files befor replacing them.. # - if [[ -f "${home_dir}/.bashrc" ]] && [[ ! -f "${home_dir}/.bashrc.ORIG" ]]; then mv "${home_dir}/.bashrc" "${home_dir}/.bashrc.ORIG" fi if [[ -f "${home_dir}/.profile" ]] && [[ ! -f "${home_dir}/.profile.ORIG" ]] ; then mv "${home_dir}/.profile" "${home_dir}/.profile.ORIG" fi if [[ -f "${home_dir}/.bash_logout" ]] && [[ ! -f "${home_dir}/.bash_logout.ORIG" ]] ; then mv "${home_dir}/.bash_logout" "${home_dir}/.bash_logout.ORIG" fi # - Replace/Add files # - for _file in $replace_files ; do if [[ -f "${templates_base_dir}/${user_name}/${_file}" ]] ; then cp -a "${templates_base_dir}/${user_name}/${_file}" "${home_dir}/" chown ${user_name}:${user_name} "${home_dir}/${_file}" elif [[ -f "${templates_all_dir}/${_file}" ]]; then cp -a "${templates_all_dir}/${_file}" "${home_dir}/" chown ${user_name}:${user_name} "${home_dir}/${_file}" else if [[ -f "${home_dir}/${_file}.ORIG" ]]; then mv "${home_dir}/${_file}.ORIG" "${home_dir}/${_file}" fi fi done if [[ -d "${home_dir}/.ssh" ]]; then cp -a "${home_dir}/.ssh" "${home_dir}/.ssh.BAK" else mkdir "${home_dir}/.ssh" fi ssh_auhtorized_file="" if [[ -d "${templates_base_dir}/${user_name}/.ssh" ]]; then if [[ -f "${templates_base_dir}/${user_name}/.ssh/authorized_keys2" ]]; then ssh_auhtorized_file="${templates_base_dir}/${user_name}/.ssh/authorized_keys2" elif [[ -f "${templates_base_dir}/${user_name}/.ssh/authorized_keys" ]]; then ssh_auhtorized_file="${templates_base_dir}/${user_name}/.ssh/authorized_keys" fi elif [[ -d "${templates_all_dir}/.ssh" ]] ; then if [[ -f "${templates_all_dir}/.ssh/authorized_keys2" ]]; then ssh_auhtorized_file="${templates_all_dir}/.ssh/authorized_keys2" elif [[ -f "${templates_all_dir}/.ssh/authorized_keys" ]]; then ssh_auhtorized_file="${templates_all_dir}/.ssh/authorized_keys" fi fi if [[ -n "$ssh_auhtorized_file" ]] ; then cp -a "$ssh_auhtorized_file" "${home_dir}/.ssh/" elif [[ -d "${home_dir}/.ssh.BAK" ]] ; then rm -rf "${home_dir}/.ssh.BAK" fi chmod 700 ${home_dir}/.ssh chown -R ${user_name}:${user_name} ${home_dir}/.ssh if [[ "$user_name" = 'sysadm' ]] || [[ "$user_name" = 'chris' ]] ; then usermod -a -G sudo ${user_name} fi #ls -al ${home_dir}/.ssh echo "" echo -e " User \033[1m${user_name}\033[m added:" echo "" echo -e " Home Dir: \033[1m$home_dir\033[m" echo -e " UID: \033[1m$(id -u ${user_name})\033[m" echo -e " GID: \033[1m$(id -g ${user_name})\033[m" if [[ -n "$2" ]]; then echo -e " Password: \033[1m$2\033[m" else echo -e " Password: \033[1m************\033[m" fi echo "" exit 0