2018-09-20 15:18:17 +00:00
|
|
|
#!/bin/bash
|
|
|
|
# ---------------------------------------------------------------------------
|
2019-03-27 18:28:49 +00:00
|
|
|
# makeuser - tilde new user creation
|
2018-09-21 14:10:50 +00:00
|
|
|
# Usage: makeuser [-h|--help] <username> <email> "<pubkey>"
|
2018-09-20 15:18:17 +00:00
|
|
|
# ---------------------------------------------------------------------------
|
2019-03-27 18:28:49 +00:00
|
|
|
#
|
|
|
|
# Forked from tilde.team's make user script (
|
2018-09-20 15:18:17 +00:00
|
|
|
PROGNAME=${0##*/}
|
2019-03-27 18:28:49 +00:00
|
|
|
VERSION="0.4"
|
|
|
|
LIST_NAME="thunix-join@lists.tildeverse.org"
|
2019-12-04 18:06:20 +00:00
|
|
|
EMAIL_TEMPLATE="tmpl/email.tmpl"
|
2019-03-27 18:28:49 +00:00
|
|
|
YAML_FILE="$1.yml"
|
2019-11-05 15:57:19 +00:00
|
|
|
GEN_TDP="./gen_tdp"
|
2019-12-01 01:03:30 +00:00
|
|
|
TILDE_JSON="/var/www/thunix.cf/tilde.json"
|
2019-12-06 13:01:38 +00:00
|
|
|
CONFIG=./setenv
|
2019-11-05 15:57:19 +00:00
|
|
|
|
2019-12-06 13:01:38 +00:00
|
|
|
. $CONFIG
|
2018-09-20 14:04:19 +00:00
|
|
|
|
2018-09-20 15:18:17 +00:00
|
|
|
error_exit() {
|
|
|
|
echo -e "${PROGNAME}: ${1:-"Unknown Error"}" >&2
|
|
|
|
exit 1
|
|
|
|
}
|
2018-09-20 14:04:19 +00:00
|
|
|
|
2018-09-20 15:18:17 +00:00
|
|
|
usage() {
|
2018-09-20 15:40:17 +00:00
|
|
|
echo -e "usage: $PROGNAME [-h|--help] <username> <email> \"<pubkey>\""
|
2018-09-20 15:18:17 +00:00
|
|
|
}
|
|
|
|
|
2018-12-15 01:29:03 +00:00
|
|
|
sub_to_list() {
|
|
|
|
echo "
|
|
|
|
From: $1
|
|
|
|
Subject: subscribe
|
2019-04-01 16:49:06 +00:00
|
|
|
" | sudo -u $1 mail $LIST_NAME
|
2018-12-15 01:29:03 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 18:28:49 +00:00
|
|
|
[[ $(id -u) == 0 ]] && error_exit "Do not run this script as root."
|
2018-09-21 14:10:50 +00:00
|
|
|
|
|
|
|
case $1 in
|
|
|
|
-h | --help)
|
|
|
|
usage; exit ;;
|
|
|
|
-* | --*)
|
|
|
|
usage; error_exit "unknown option $1" ;;
|
|
|
|
*)
|
|
|
|
[[ $# -ne 3 ]] && error_exit "not enough args"
|
|
|
|
echo "adding new user $1"
|
2019-04-01 16:49:06 +00:00
|
|
|
newpw=`pwgen -1B 10`
|
2018-09-21 14:10:50 +00:00
|
|
|
pwcrypt=$(perl -e "print crypt('${newpw}', 'sa');")
|
2019-04-01 16:49:06 +00:00
|
|
|
sudo useradd -m -g 1000 -p $pwcrypt -s /bin/bash $1 || exit 1
|
|
|
|
# This is the welcome for team.
|
|
|
|
# sed -e "s/newusername/$1/g" -e "s/newpassword/$newpw/" $EMAIL_TEMPLATE | sudo mail $1 $2 $ADMIN_EMAIL
|
|
|
|
|
|
|
|
# This is the welcome email for thunix
|
2019-12-04 18:06:20 +00:00
|
|
|
sed -e "s/newusername/$1/g" -e "s/newpassword/$newpw/g" tmpl/email.tmpl | sudo mail -s "Welcome to Thunix!" $2
|
|
|
|
sed -e "s/newusername/$1/g" -e "s/newpassword/$newpw/g" tmpl/email.tmpl | sudo mail -s "Welcome to Thunix!" $ADMIN_EMAIL
|
2018-12-15 01:29:03 +00:00
|
|
|
sub_to_list $1
|
2019-03-27 18:28:49 +00:00
|
|
|
|
2019-04-01 16:49:06 +00:00
|
|
|
# We don't need this for thunix, since ansible will do it
|
|
|
|
# echo "$3" | tee /home/$1/.ssh/authorized_keys
|
2019-03-27 18:28:49 +00:00
|
|
|
|
|
|
|
# If root doesn't have a fediverse account, comment this out
|
2019-04-01 16:49:06 +00:00
|
|
|
# sudo toot "welcome new user ~$1!"
|
2019-03-27 18:28:49 +00:00
|
|
|
|
|
|
|
#Thunix specific section
|
2019-05-07 12:31:33 +00:00
|
|
|
currdir=`pwd`
|
|
|
|
cd $REPO_LOCATION; git pull; cd $currdir
|
2019-03-27 18:28:49 +00:00
|
|
|
echo "---
|
|
|
|
- name: Setting up $1
|
|
|
|
user:
|
|
|
|
name: $1
|
|
|
|
groups: tilde
|
|
|
|
state: present
|
|
|
|
skeleton: /etc/skel
|
|
|
|
shell: /bin/bash
|
|
|
|
system: no
|
|
|
|
createhome: yes
|
|
|
|
home: /home/$1
|
|
|
|
- authorized_key:
|
|
|
|
user: $1
|
|
|
|
state: present
|
2019-04-01 16:49:06 +00:00
|
|
|
key: \"$3\"" > $REPO_LOCATION/roles/shell/tasks/users/$YAML_FILE
|
2019-03-27 18:28:49 +00:00
|
|
|
|
2019-04-01 16:49:06 +00:00
|
|
|
echo "- include: users/$YAML_FILE" >> $REPO_LOCATION/roles/shell/tasks/users.yml
|
2019-03-27 18:28:49 +00:00
|
|
|
place=`pwd`
|
|
|
|
cd $REPO_LOCATION
|
2019-04-04 01:55:01 +00:00
|
|
|
git add $REPO_LOCATION/roles/shell/tasks/users/$1.yml
|
2019-03-27 18:28:49 +00:00
|
|
|
git commit -am "Adding user $1"
|
2019-04-04 01:55:01 +00:00
|
|
|
git push
|
2019-03-27 18:28:49 +00:00
|
|
|
cd $place
|
2019-11-05 15:57:19 +00:00
|
|
|
$GEN_TDP | sudo tee $TILDE_JSON
|
2019-03-27 18:28:49 +00:00
|
|
|
# End Thunix specific section
|
|
|
|
;;
|
|
|
|
|
2018-09-21 14:10:50 +00:00
|
|
|
esac
|
2018-09-21 14:04:48 +00:00
|
|
|
|