247 lines
7.9 KiB
Bash
Executable File
247 lines
7.9 KiB
Bash
Executable File
#!/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
|
|
home_dir="${home_base_dir}/${user_name}"
|
|
|
|
|
|
# - Create user if not exists
|
|
# -
|
|
if ! id -u "${user_name}" > /dev/null 2>&1 ; then
|
|
|
|
# - Get uid/gid
|
|
# -
|
|
if [[ "$user_name" = 'webadmin' ]]; then
|
|
user_uid=1040
|
|
user_gid=1040
|
|
home_dir="/var/www"
|
|
elif [[ "$user_name" = 'sysadm' ]]; then
|
|
user_uid=1050
|
|
user_gid=1050
|
|
elif [[ "$user_name" = 'back' ]]; then
|
|
user_uid=1060
|
|
user_gid=1060
|
|
elif [[ "$user_name" = 'cryptpad' ]]; then
|
|
user_uid=1033
|
|
user_gid=1033
|
|
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_dir}:/bin/bash" | newusers
|
|
|
|
if [[ -d "$skel_dir" ]]; then
|
|
cp -a "${skel_dir}/.bash_logout" "${home_dir}/"
|
|
chown ${user_name}:${user_name} "${home_dir}/.bash_logout"
|
|
|
|
cp -a "${skel_dir}/.bashrc" "${home_dir}/"
|
|
chown ${user_name}:${user_name} "${home_dir}/.bashrc"
|
|
|
|
cp -a "${skel_dir}/.profile" "${home_dir}/"
|
|
chown ${user_name}:${user_name} "${home_dir}/.profile"
|
|
fi
|
|
else
|
|
user_uid=$(id -u ${user_name})
|
|
user_gid=$(id -g ${user_name})
|
|
fi
|
|
|
|
# - Set new password
|
|
# -
|
|
if $password_needed ; then
|
|
|
|
# - webadmin
|
|
# -
|
|
if [[ "$user_name" = 'webadmin' ]]; then
|
|
perl -i -n -p -e "s#^(${user_name}):[^:]+:(.+)#\1:\\\$6\\\$P1UvKv/0\\\$0hWHiUXQTrNl5SFf52iAY1qDYsJFhjHaVC6F6BuvazcNXkzjAcnuX5PMml5DLh4tyFpqVdPpCyAyd92YypNbu/:\2#" /etc/shadow
|
|
|
|
# - sysadm
|
|
# -
|
|
elif [[ "$user_name" = 'sysadm' ]]; then
|
|
perl -i -n -p -e "s#^(${user_name}):[^:]+:(.+)#\1:\\\$6\\\$vvccwrTc\\\$Sz1HaSb3ujObprltiG7D6U1Rr3fpgfjkKuDDWYdHzPkPx/0pEofCWC.vyTn78hcemkntl.6wVUOnJnNloKt/E/:\2#" /etc/shadow
|
|
|
|
# - axel
|
|
# -
|
|
elif [[ "$user_name" = 'axel' ]]; then
|
|
perl -i -n -p -e "s#^(${user_name}):[^:]+:(.+)#\1:\\\$6\\\$zUWC465e\\\$XblctxwnBIOa7mPcN6foEQrwChjpwoY7lLtacXJrSsvjZS3I6Ox1mYUtN3/gzkvpbzOPx/9PlRJV.mbl939mD.:\2#" /etc/shadow
|
|
|
|
# - kaya
|
|
# -
|
|
elif [[ "$user_name" = 'kaya' ]]; then
|
|
perl -i -n -p -e "s#^(${user_name}):[^:]+:(.+)#\1:\\\$6\\\$t9gheUvd\\\$hFTJ5mp0bdu4Hc5zGmS6HuSAfFOc4QRROLX4wnCauLjwTxUtvhgeLDlL5YkjGfiWOCEe84krH4op0DdKjTJWG/:\2#" /etc/shadow
|
|
|
|
# - felix
|
|
# -
|
|
elif [[ "$user_name" = 'felix' ]]; then
|
|
perl -i -n -p -e "s#^(${user_name}):[^:]+:(.+)#\1:\\\$6\\\$RKaAbxOz\\\$LxvjoVclfkgo99VwmXi9cIhVmUdTNT.T0/pZAH9GNtFEQn5NoOak/DHTHHG9kn58soDRuhEAup1x4T2jG.Js0/:\2#" /etc/shadow
|
|
|
|
# - mariette
|
|
# -
|
|
elif [[ "$user_name" = 'mariette' ]]; then
|
|
perl -i -n -p -e "s#^(${user_name}):[^:]+:(.+)#\1:\\\$6\\\$QiYW4uMG\\\$9bOGhsON8SnOSIC4.UTXhVpCpebcJNInI3t1JPhR7979D3CpoKroK7dXPAxGfDigrkxBwcqju..PuvCUqtshw1:\2#" /etc/shadow
|
|
|
|
# - christian
|
|
# -
|
|
elif [[ "$user_name" = 'christian' ]]; then
|
|
perl -i -n -p -e "s#^(${user_name}):[^:]+:(.+)#\1:\\\$6\\\$2paWmEea\\\$G51JZDzjjDNE75aBl/xuM1dyH.FWYHwNCRHeKWkHhxjUmRRC/v.hhNh5jOk5EbVWDeVh7r5dz1tO2HTZUMftb1:\2#" /etc/shadow
|
|
|
|
# - chris
|
|
# -
|
|
elif [[ "$user_name" = 'chris' ]]; then
|
|
perl -i -n -p -e "s#^(${user_name}):[^:]+:(.+)#\1:\\\$6\\\$RWYCSssQ\\\$mtcJJELuc3.onwAhyVk4zdFLGaKw.XNfpM/mEZEJfZIyZZwUApwgoMcWkly7dwv0X4FpJVQ.oxJNnitHp7Ar70:\2#" /etc/shadow
|
|
|
|
|
|
# - wadmin
|
|
# -
|
|
elif [[ "$user_name" = 'wadmin' ]]; then
|
|
perl -i -n -p -e "s#^(${user_name}):[^:]+:(.+)#\1:\\\$6\\\$vaVNOPYp\\\$RbV6/phKDZRSa4TQGlJVOF/HQqyxAK7BeQmir8Kr8pwy7ATlHGvcPzJkuldNNp4O4t2q3EJRW2uVpjsng4ZhH1:\2#" /etc/shadow
|
|
|
|
# - root
|
|
# -
|
|
elif [[ "$user_name" = 'root' ]]; then
|
|
perl -i -n -p -e "s#^(${user_name}):[^:]+:(.+)#\1:\\\$6\\\$shJF/I7l\\\$Z9H3on8zNLS3AxR3TtYU1lmMW7sHnpn9ylW2.758uOIDBl0Sku2x7xYfml8ty0JJrhZMXLrGnikprqqtmgAoq1:\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 -E "^${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
|
|
|
|
|
|
if [[ -f "${templates_base_dir}/${user_name}/.ssh/config" ]]; then
|
|
ssh_config_dir="${templates_base_dir}/${user_name}/.ssh/config"
|
|
elif [[ -f "${templates_all_dir}/.ssh/config" ]] ; then
|
|
ssh_config_dir="${templates_base_dir}/${user_name}/.ssh/config"
|
|
fi
|
|
if [[ -n "$ssh_config_dir" ]] ; then
|
|
cp -a "$ssh_config_dir" "${home_dir}/.ssh/"
|
|
fi
|
|
|
|
|
|
chmod 700 ${home_dir}/.ssh
|
|
chown -R ${user_name}:${user_name} ${home_dir}/.ssh
|
|
|
|
if [[ "$user_name" = 'sysadm' ]] || [[ "$user_name" = 'chris' ]] || [[ "$user_name" = 'wadmin' ]] ; 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
|