Update makeuser

This commit is contained in:
deepend-tildeclub 2024-01-05 10:38:37 -07:00 committed by GitHub
parent b2cd2f446f
commit 4b577bbf98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 48 additions and 78 deletions

126
makeuser
View File

@ -1,109 +1,79 @@
#!/bin/bash #!/bin/sh
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# makeuser - tilde.club new user creation # makeuser - tilde.club new user creation
# Usage: makeuser [-h|--help] <username> <email> "<pubkey>" # Usage: makeuser [-h|--help] <username> <email> "<pubkey>"
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
PROGNAME=${0##*/} PROGNAME=${0##*/}
VERSION="0.1" VERSION="0.2"
error_exit() { error_exit() {
echo -e "${PROGNAME}: ${1:-"Unknown Error"}" >&2 printf "%s: %s\n" "$PROGNAME" "${1:-"Unknown Error"}" >&2
exit 1 exit 1
} }
usage() { usage() {
echo -e "usage: $PROGNAME [-h|--help] <username> <email> \"<pubkey>\"" printf "usage: %s %s [-h|--help] <username> <email> \"<pubkey>\"\n" "$PROGNAME" "$VERSION"
} }
sub_to_list() { sub_to_list() {
echo " sudo -u "$1" sendmail tildeclub-join@lists.tildeverse.org << MAIL
From: $1 From: $1
Subject: subscribe Subject: subscribe
" | sudo -u $1 sendmail tildeclub-join@lists.tildeverse.org MAIL
}
is_banned() {
while read -r line; do
banned_user=$(echo "$line" | awk '{print $1}')
banned_email=$(echo "$line" | awk '{print $2}')
if [[ "$1" == "$banned_user" || "$2" == "$banned_email" ]]; then
return 0
fi
done < /var/signups_banned
return 1
}
move_to_current() {
exec 200>/var/lock/makeuser.lock
flock -n 200 || { echo "Couldn't acquire lock."; exit 1; }
grep_pattern="makeuser\s+$1\s+$2\s+\"$3\""
if grep -E "$grep_pattern" /var/signups >> /var/signups_current; then
echo "User moved to signups_current."
if grep -q -E "$grep_pattern" /var/signups_current; then
echo "User verified in signups_current."
grep -vE "$grep_pattern" /var/signups > temp && mv temp /var/signups
chown nginx:root /var/signups
chmod 660 /var/signups
chmod 660 /var/signups_banned
chmod 660 /var/signups_current
echo "User removed from signups."
else
echo "User not found in signups_current. Aborting."
fi
else
echo "User not found in signups."
fi
exec 200>&-
} }
case $1 in case $1 in
-h | --help) -h | --help)
usage; exit ;; usage; exit ;;
-* | --*) -* | --*)
usage; error_exit "unknown option $1" ;; usage; error_exit "unknown option $1" ;;
*) *)
[[ $# -ne 3 ]] && error_exit "not enough args" if [ $# -ne 3 ]; then
error_exit "not enough args"
fi
if is_banned "$1" "$2"; then if id "$1" > /dev/null 2>&1; then
error_exit "user or email is banned" exit 0
fi fi
if id $1 > /dev/null 2>&1; then printf "adding new user %s\n" "$1"
exit 0 newpw=$(pwgen -1B 20)
fi sudo useradd -m -g 100 -s /bin/bash "$1" \
|| error_exit "couldn't add user"
printf "%s:%s\n" "$1" "$newpw" | sudo chpasswd
echo "adding new user $1" printf "sending welcome mail\n"
newpw=$(pwgen -1B 10) sed -e "s/newusername/$1/g" \
sudo useradd -m -g 100 -s /usr/bin/bash $1 \ -e "s/newpassword/$newpw/" \
|| error_exit "couldn't add user" -e "s/newtoemail/$2/" \
echo "$1:$newpw" | sudo chpasswd /usr/local/bin/welcome-email.tmpl \
| sendmail "$1" "$2" root@tilde.club
echo "sending welcome mail" printf "subscribing to mailing list\n"
sed -e "s/newusername/$1/g" -e "s/newpassword/$newpw/" /usr/local/bin/welcome-email.tmpl \ sub_to_list "$1"
| sendmail $1 $2 root
echo "subscribing to mailing list" printf "adding ssh pubkey\n"
sub_to_list $1 printf "%s\n" "$3" | sudo tee "/home/$1/.ssh/authorized_keys"
echo "removing .git and README.md from new homedir" printf "\nannouncing new user on mastodon\n"
sudo rm -rf /home/$1/.git /usr/local/bin/toot "welcome new user ~$1!"
sudo rm -rf /home/$1/README.md
# Changing last edit date. printf "cleanup current signup\n"
sudo touch /home/$1/.new_user sudo sed -i"" "/\b$1\b/d" /var/signups_current
printf "removing .git from new homedir\n"
sudo rm -rf /home/$1/.git
echo "adding ssh pubkey" printf "fix sorting in /etc/passwd\n"
echo "$3" | sudo tee /home/$1/.ssh/authorized_keys sudo pwck -s
echo "moving user from signups to signups_current" # printf "applying disk quota\n"
move_to_current $1 $2 "$3" # sudo setquota -u "$1" 1048576 3145728 0 0 /home
echo "making znc user" printf "making znc user\n"
/usr/local/bin/znccreate.py "$1" "$newpw" /usr/local/bin/znccreate.py "$1" "$newpw"
echo "announcing new user on mastodon"
toot "welcome new user ~$1!"
esac esac