2021-04-04 13:28:22 -07:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# vhost-stack
|
|
|
|
# https://git.stack-source.com/msb/vhost-stack
|
|
|
|
# MIT License Copyright (c) 2021 Matthew Saunders Brown
|
|
|
|
|
2021-04-04 14:15:16 -07:00
|
|
|
# load include file
|
|
|
|
source $(dirname $0)/vhost.sh
|
2021-04-04 13:28:22 -07:00
|
|
|
|
2021-09-16 16:21:35 -07:00
|
|
|
help()
|
|
|
|
{
|
|
|
|
thisfilename=$(basename -- "$0")
|
|
|
|
echo "Add system user to server."
|
|
|
|
echo ""
|
|
|
|
echo "usage: $thisfilename username [OPTIONS]"
|
|
|
|
echo ""
|
|
|
|
echo " -h Print this help."
|
|
|
|
echo " -p PASSWORD Password for username. Optional, random password generated if none specified."
|
|
|
|
echo " -u UID Numberic User ID to assign to user. Optional, next available uid set if none specified."
|
2021-10-02 14:59:01 -07:00
|
|
|
echo " -s Save user info to /home/username/.passwd. Warning! This inlcudes the unencrypted password."
|
2021-09-16 16:21:35 -07:00
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
2021-04-04 13:28:22 -07:00
|
|
|
# check for and set username
|
|
|
|
if [ -n "$1" ]; then
|
2021-09-16 16:21:35 -07:00
|
|
|
if [ $1 == "-h" ]; then
|
|
|
|
help
|
|
|
|
else
|
|
|
|
username="${1,,}"
|
|
|
|
fi
|
2021-04-04 13:28:22 -07:00
|
|
|
else
|
|
|
|
echo "username not set"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-10-02 14:59:01 -07:00
|
|
|
while getopts "hp:su:" opt; do
|
2021-09-16 16:21:35 -07:00
|
|
|
case "${opt}" in
|
|
|
|
h )
|
|
|
|
help
|
2021-09-17 11:39:18 -07:00
|
|
|
;;
|
2021-09-16 16:21:35 -07:00
|
|
|
p )
|
|
|
|
password=${OPTARG}
|
|
|
|
;;
|
2021-10-02 14:59:01 -07:00
|
|
|
s )
|
|
|
|
save=true
|
|
|
|
;;
|
2021-09-16 16:21:35 -07:00
|
|
|
u )
|
|
|
|
uid=${OPTARG}
|
|
|
|
;;
|
|
|
|
\? )
|
|
|
|
echo "Invalid option: $OPTARG"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
: )
|
|
|
|
echo "Invalid option: $OPTARG requires an argument"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
# generate password if none specified
|
|
|
|
if [ ! -n "$password" ]; then
|
|
|
|
password=`/usr/bin/pwgen 12 1`
|
2021-04-04 13:28:22 -07:00
|
|
|
fi
|
|
|
|
|
2021-09-16 16:21:35 -07:00
|
|
|
# get next UID if none specified
|
|
|
|
if [ ! -n "$uid" ]; then
|
|
|
|
userid=`awk -F: '{uid[$3]=1}END{for(x=1000; x<=65534; x++) {if(uid[x] != ""){}else{print x; exit;}}}' /etc/passwd`
|
2021-04-04 13:28:22 -07:00
|
|
|
fi
|
|
|
|
|
2021-09-16 16:21:35 -07:00
|
|
|
# user & related files are only added if they don't already exist
|
|
|
|
# in this way it's safe to repeatedly try to add the same user
|
|
|
|
|
2021-04-04 13:28:22 -07:00
|
|
|
if ! /bin/grep -q "^$username:" /etc/passwd; then
|
|
|
|
newusers="$username:$password:$userid:$userid::/home/$username:/bin/bash"
|
|
|
|
echo "$newusers"|newusers
|
|
|
|
pwck -s
|
|
|
|
grpck -s
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ ! -d "/home/$username" ]]; then
|
|
|
|
install -d -o $username -g $username -m 755 /home/$username
|
|
|
|
else
|
|
|
|
chown -R $username:$username /home/$username
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ ! -f "/home/$username/.bash_logout" ]]; then
|
|
|
|
install -o $username -g $username -m 640 /etc/skel/.bash_logout /home/$username
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ ! -f "/home/$username/.bashrc" ]]; then
|
|
|
|
install -o $username -g $username -m 640 /etc/skel/.bashrc /home/$username
|
|
|
|
echo '' >> /home/$username/.bashrc
|
|
|
|
echo '# local settings' >> /home/$username/.bashrc
|
|
|
|
echo '' >> /home/$username/.bashrc
|
|
|
|
echo 'export TERM=xterm-256color' >> /home/$username/.bashrc
|
|
|
|
echo '' >> /home/$username/.bashrc
|
|
|
|
echo 'command_not_found_handle () {' >> /home/$username/.bashrc
|
2021-04-22 13:17:36 -07:00
|
|
|
echo ' /usr/local/libexec/command-not-found-handle $@' >> /home/$username/.bashrc
|
2021-04-04 13:28:22 -07:00
|
|
|
echo ' return 127' >> /home/$username/.bashrc
|
|
|
|
echo '}' >> /home/$username/.bashrc
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ ! -f "/home/$username/.profile" ]]; then
|
|
|
|
install -o $username -g $username -m 640 /etc/skel/.profile /home/$username
|
|
|
|
fi
|
|
|
|
|
2021-10-02 14:59:01 -07:00
|
|
|
if [ -n "$save" ]; then
|
|
|
|
if [[ ! -f "/home/$username/.passwd" ]]; then
|
|
|
|
touch /home/$username/.passwd
|
|
|
|
chmod 640 /home/$username/.passwd
|
|
|
|
chown $username:$username /home/$username/.passwd
|
|
|
|
echo "$newusers" > /home/$username/.passwd
|
|
|
|
fi
|
|
|
|
fi
|