makeuser/makeuser

76 lines
2.1 KiB
Bash
Executable File

#!/bin/sh
# ---------------------------------------------------------------------------
# makeuser - tilde.team new user creation
# Usage: makeuser [-h|--help] <username> <email> "<pubkey>"
# ---------------------------------------------------------------------------
PROGNAME=${0##*/}
VERSION="0.2"
error_exit() {
printf "%s: %s\n" "$PROGNAME" "${1:-"Unknown Error"}" >&2
exit 1
}
usage() {
printf "usage: %s %s [-h|--help] <username> <email> \"<pubkey>\"\n" "$PROGNAME" "$VERSION"
}
sub_to_list() {
sudo -u "$1" sendmail tildeteam-join@lists.tildeverse.org << MAIL
From: $1
Subject: subscribe
MAIL
}
case $1 in
-h | --help)
usage; exit ;;
-* | --*)
usage; error_exit "unknown option $1" ;;
*)
if [ $# -ne 3 ]; then
error_exit "not enough args"
fi
if id "$1" > /dev/null 2>&1; then
exit 0
fi
printf "adding new user %s\n" "$1"
newpw=$(pwgen -1B 20)
sudo useradd -m -g 100 -s /bin/bash "$1" \
|| error_exit "couldn't add user"
printf "%s:%s\n" "$1" "$newpw" | sudo chpasswd
printf "sending welcome mail\n"
sed -e "s/newusername/$1/g" \
-e "s/newpassword/$newpw/" \
-e "s/newtoemail/$2/" \
/usr/local/bin/welcome-email.tmpl \
| sendmail "$1" "$2" sudoers@tilde.team
printf "subscribing to mailing list\n"
sub_to_list "$1"
printf "adding ssh pubkey\n"
printf "%s\n" "$3" | sudo tee "/home/$1/.ssh/authorized_keys"
printf "\nannouncing new user on mastodon\n"
/usr/local/bin/toot "welcome new user ~$1!"
printf "cleanup current signup\n"
sudo sed -i"" "/\b$1\b/d" /var/signups_current
printf "fix sorting in /etc/passwd\n"
sudo pwck -s
printf "applying disk quota\n"
sudo setquota -u "$1" 1048576 3145728 0 0 /home
printf "copying user to bsd.tilde.team\n"
newid=$(id -u "$1")
printf %s "$newpw" | sudo ssh bsd.tilde.team -- pw useradd -n "$1" -u "$newid" -g team -md /home/"$1" -h0
;;
esac